Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "lowmemorykiller" |
| 18 | |
| 19 | #include <errno.h> |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 20 | #include <inttypes.h> |
Suren Baghdasaryan | 4311d1e | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 21 | #include <pwd.h> |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 22 | #include <sched.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 23 | #include <signal.h> |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 24 | #include <stdbool.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 27 | #include <sys/cdefs.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 28 | #include <sys/epoll.h> |
| 29 | #include <sys/eventfd.h> |
Colin Cross | b28ff91 | 2014-07-11 17:15:44 -0700 | [diff] [blame] | 30 | #include <sys/mman.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 31 | #include <sys/socket.h> |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 32 | #include <sys/sysinfo.h> |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 33 | #include <sys/types.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 36 | #include <cutils/properties.h> |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 37 | #include <cutils/sockets.h> |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 38 | #include <lmkd.h> |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 39 | #include <log/log.h> |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 40 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 41 | #ifdef LMKD_LOG_STATS |
Yao Chen | 389aee1 | 2018-05-02 11:19:27 -0700 | [diff] [blame] | 42 | #include "statslog.h" |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 43 | #endif |
| 44 | |
Suren Baghdasaryan | c713559 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 45 | /* |
| 46 | * Define LMKD_TRACE_KILLS to record lmkd kills in kernel traces |
| 47 | * to profile and correlate with OOM kills |
| 48 | */ |
| 49 | #ifdef LMKD_TRACE_KILLS |
| 50 | |
| 51 | #define ATRACE_TAG ATRACE_TAG_ALWAYS |
| 52 | #include <cutils/trace.h> |
| 53 | |
| 54 | #define TRACE_KILL_START(pid) ATRACE_INT(__FUNCTION__, pid); |
| 55 | #define TRACE_KILL_END() ATRACE_INT(__FUNCTION__, 0); |
| 56 | |
| 57 | #else /* LMKD_TRACE_KILLS */ |
| 58 | |
Daniel Colascione | 347f6b4 | 2018-02-12 11:24:47 -0800 | [diff] [blame] | 59 | #define TRACE_KILL_START(pid) ((void)(pid)) |
| 60 | #define TRACE_KILL_END() ((void)0) |
Suren Baghdasaryan | c713559 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 61 | |
| 62 | #endif /* LMKD_TRACE_KILLS */ |
| 63 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 64 | #ifndef __unused |
| 65 | #define __unused __attribute__((__unused__)) |
| 66 | #endif |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 67 | |
| 68 | #define MEMCG_SYSFS_PATH "/dev/memcg/" |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 69 | #define MEMCG_MEMORY_USAGE "/dev/memcg/memory.usage_in_bytes" |
| 70 | #define MEMCG_MEMORYSW_USAGE "/dev/memcg/memory.memsw.usage_in_bytes" |
Suren Baghdasaryan | da0bc05 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 71 | #define ZONEINFO_PATH "/proc/zoneinfo" |
| 72 | #define MEMINFO_PATH "/proc/meminfo" |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 73 | #define LINE_MAX 128 |
| 74 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 75 | /* gid containing AID_SYSTEM required */ |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 76 | #define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree" |
| 77 | #define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj" |
| 78 | |
| 79 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 80 | #define EIGHT_MEGA (1 << 23) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 81 | |
Suren Baghdasaryan | 4311d1e | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 82 | /* Defined as ProcessList.SYSTEM_ADJ in ProcessList.java */ |
| 83 | #define SYSTEM_ADJ (-900) |
| 84 | |
Greg Kaiser | 6bbd521 | 2018-03-23 14:16:12 -0700 | [diff] [blame] | 85 | #define STRINGIFY(x) STRINGIFY_INTERNAL(x) |
| 86 | #define STRINGIFY_INTERNAL(x) #x |
| 87 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 88 | /* default to old in-kernel interface if no memory pressure events */ |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 89 | static bool use_inkernel_interface = true; |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 90 | static bool has_inkernel_module; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 91 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 92 | /* memory pressure levels */ |
| 93 | enum vmpressure_level { |
| 94 | VMPRESS_LEVEL_LOW = 0, |
| 95 | VMPRESS_LEVEL_MEDIUM, |
| 96 | VMPRESS_LEVEL_CRITICAL, |
| 97 | VMPRESS_LEVEL_COUNT |
| 98 | }; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 99 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 100 | static const char *level_name[] = { |
| 101 | "low", |
| 102 | "medium", |
| 103 | "critical" |
| 104 | }; |
| 105 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 106 | struct { |
Suren Baghdasaryan | 9ac54eb | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 107 | int64_t min_nr_free_pages; /* recorded but not used yet */ |
| 108 | int64_t max_nr_free_pages; |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 109 | } low_pressure_mem = { -1, -1 }; |
| 110 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 111 | static int level_oomadj[VMPRESS_LEVEL_COUNT]; |
Suren Baghdasaryan | e82e15c | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 112 | static int mpevfd[VMPRESS_LEVEL_COUNT] = { -1, -1, -1 }; |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 113 | static bool debug_process_killing; |
| 114 | static bool enable_pressure_upgrade; |
| 115 | static int64_t upgrade_pressure; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 116 | static int64_t downgrade_pressure; |
Suren Baghdasaryan | ff61afb | 2018-04-13 11:45:38 -0700 | [diff] [blame] | 117 | static bool low_ram_device; |
Suren Baghdasaryan | 662492a | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 118 | static bool kill_heaviest_task; |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 119 | static unsigned long kill_timeout_ms; |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 120 | static bool use_minfree_levels; |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 121 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 122 | /* data required to handle events */ |
| 123 | struct event_handler_info { |
| 124 | int data; |
| 125 | void (*handler)(int data, uint32_t events); |
| 126 | }; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 127 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 128 | /* data required to handle socket events */ |
| 129 | struct sock_event_handler_info { |
| 130 | int sock; |
| 131 | struct event_handler_info handler_info; |
| 132 | }; |
| 133 | |
| 134 | /* max supported number of data connections */ |
| 135 | #define MAX_DATA_CONN 2 |
| 136 | |
| 137 | /* socket event handler data */ |
| 138 | static struct sock_event_handler_info ctrl_sock; |
| 139 | static struct sock_event_handler_info data_sock[MAX_DATA_CONN]; |
| 140 | |
| 141 | /* vmpressure event handler data */ |
| 142 | static struct event_handler_info vmpressure_hinfo[VMPRESS_LEVEL_COUNT]; |
| 143 | |
| 144 | /* 3 memory pressure levels, 1 ctrl listen socket, 2 ctrl data socket */ |
| 145 | #define MAX_EPOLL_EVENTS (1 + MAX_DATA_CONN + VMPRESS_LEVEL_COUNT) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 146 | static int epollfd; |
| 147 | static int maxevents; |
| 148 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 149 | /* OOM score values used by both kernel and framework */ |
Todd Poynor | 16b6099 | 2013-09-16 19:26:47 -0700 | [diff] [blame] | 150 | #define OOM_SCORE_ADJ_MIN (-1000) |
| 151 | #define OOM_SCORE_ADJ_MAX 1000 |
| 152 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 153 | static int lowmem_adj[MAX_TARGETS]; |
| 154 | static int lowmem_minfree[MAX_TARGETS]; |
| 155 | static int lowmem_targets_size; |
| 156 | |
Suren Baghdasaryan | da0bc05 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 157 | /* Fields to parse in /proc/zoneinfo */ |
| 158 | enum zoneinfo_field { |
| 159 | ZI_NR_FREE_PAGES = 0, |
| 160 | ZI_NR_FILE_PAGES, |
| 161 | ZI_NR_SHMEM, |
| 162 | ZI_NR_UNEVICTABLE, |
| 163 | ZI_WORKINGSET_REFAULT, |
| 164 | ZI_HIGH, |
| 165 | ZI_FIELD_COUNT |
| 166 | }; |
| 167 | |
| 168 | static const char* const zoneinfo_field_names[ZI_FIELD_COUNT] = { |
| 169 | "nr_free_pages", |
| 170 | "nr_file_pages", |
| 171 | "nr_shmem", |
| 172 | "nr_unevictable", |
| 173 | "workingset_refault", |
| 174 | "high", |
| 175 | }; |
| 176 | |
| 177 | union zoneinfo { |
| 178 | struct { |
| 179 | int64_t nr_free_pages; |
| 180 | int64_t nr_file_pages; |
| 181 | int64_t nr_shmem; |
| 182 | int64_t nr_unevictable; |
| 183 | int64_t workingset_refault; |
| 184 | int64_t high; |
| 185 | /* fields below are calculated rather than read from the file */ |
| 186 | int64_t totalreserve_pages; |
| 187 | } field; |
| 188 | int64_t arr[ZI_FIELD_COUNT]; |
| 189 | }; |
| 190 | |
| 191 | /* Fields to parse in /proc/meminfo */ |
| 192 | enum meminfo_field { |
| 193 | MI_NR_FREE_PAGES = 0, |
| 194 | MI_CACHED, |
| 195 | MI_SWAP_CACHED, |
| 196 | MI_BUFFERS, |
| 197 | MI_SHMEM, |
| 198 | MI_UNEVICTABLE, |
| 199 | MI_FREE_SWAP, |
| 200 | MI_DIRTY, |
| 201 | MI_FIELD_COUNT |
| 202 | }; |
| 203 | |
| 204 | static const char* const meminfo_field_names[MI_FIELD_COUNT] = { |
| 205 | "MemFree:", |
| 206 | "Cached:", |
| 207 | "SwapCached:", |
| 208 | "Buffers:", |
| 209 | "Shmem:", |
| 210 | "Unevictable:", |
| 211 | "SwapFree:", |
| 212 | "Dirty:", |
| 213 | }; |
| 214 | |
| 215 | union meminfo { |
| 216 | struct { |
| 217 | int64_t nr_free_pages; |
| 218 | int64_t cached; |
| 219 | int64_t swap_cached; |
| 220 | int64_t buffers; |
| 221 | int64_t shmem; |
| 222 | int64_t unevictable; |
| 223 | int64_t free_swap; |
| 224 | int64_t dirty; |
| 225 | /* fields below are calculated rather than read from the file */ |
| 226 | int64_t nr_file_pages; |
| 227 | } field; |
| 228 | int64_t arr[MI_FIELD_COUNT]; |
| 229 | }; |
| 230 | |
| 231 | enum field_match_result { |
| 232 | NO_MATCH, |
| 233 | PARSE_FAIL, |
| 234 | PARSE_SUCCESS |
| 235 | }; |
| 236 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 237 | struct adjslot_list { |
| 238 | struct adjslot_list *next; |
| 239 | struct adjslot_list *prev; |
| 240 | }; |
| 241 | |
| 242 | struct proc { |
| 243 | struct adjslot_list asl; |
| 244 | int pid; |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 245 | uid_t uid; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 246 | int oomadj; |
| 247 | struct proc *pidhash_next; |
| 248 | }; |
| 249 | |
Suren Baghdasaryan | d716fe3 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 250 | struct reread_data { |
| 251 | const char* const filename; |
| 252 | int fd; |
| 253 | }; |
| 254 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 255 | #ifdef LMKD_LOG_STATS |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 256 | static bool enable_stats_log; |
| 257 | static android_log_context log_ctx; |
| 258 | #endif |
| 259 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 260 | #define PIDHASH_SZ 1024 |
| 261 | static struct proc *pidhash[PIDHASH_SZ]; |
| 262 | #define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1)) |
| 263 | |
Chih-Hung Hsieh | daa13ea | 2016-05-19 16:02:22 -0700 | [diff] [blame] | 264 | #define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN) |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 265 | static struct adjslot_list procadjslot_list[ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1]; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 266 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 267 | /* PAGE_SIZE / 1024 */ |
| 268 | static long page_k; |
| 269 | |
Suren Baghdasaryan | d716fe3 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 270 | static bool parse_int64(const char* str, int64_t* ret) { |
| 271 | char* endptr; |
| 272 | long long val = strtoll(str, &endptr, 10); |
| 273 | if (str == endptr || val > INT64_MAX) { |
| 274 | return false; |
| 275 | } |
| 276 | *ret = (int64_t)val; |
| 277 | return true; |
| 278 | } |
| 279 | |
Suren Baghdasaryan | da0bc05 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 280 | static enum field_match_result match_field(const char* cp, const char* ap, |
| 281 | const char* const field_names[], |
| 282 | int field_count, int64_t* field, |
| 283 | int *field_idx) { |
| 284 | int64_t val; |
| 285 | int i; |
| 286 | |
| 287 | for (i = 0; i < field_count; i++) { |
| 288 | if (!strcmp(cp, field_names[i])) { |
| 289 | *field_idx = i; |
| 290 | return parse_int64(ap, field) ? PARSE_SUCCESS : PARSE_FAIL; |
| 291 | } |
| 292 | } |
| 293 | return NO_MATCH; |
| 294 | } |
| 295 | |
Suren Baghdasaryan | d716fe3 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 296 | /* |
| 297 | * Read file content from the beginning up to max_len bytes or EOF |
| 298 | * whichever happens first. |
| 299 | */ |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 300 | static ssize_t read_all(int fd, char *buf, size_t max_len) |
| 301 | { |
| 302 | ssize_t ret = 0; |
Suren Baghdasaryan | d716fe3 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 303 | off_t offset = 0; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 304 | |
| 305 | while (max_len > 0) { |
Suren Baghdasaryan | d716fe3 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 306 | ssize_t r = TEMP_FAILURE_RETRY(pread(fd, buf, max_len, offset)); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 307 | if (r == 0) { |
| 308 | break; |
| 309 | } |
| 310 | if (r == -1) { |
| 311 | return -1; |
| 312 | } |
| 313 | ret += r; |
| 314 | buf += r; |
Suren Baghdasaryan | d716fe3 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 315 | offset += r; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 316 | max_len -= r; |
| 317 | } |
| 318 | |
| 319 | return ret; |
| 320 | } |
| 321 | |
Suren Baghdasaryan | d716fe3 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 322 | /* |
| 323 | * Read a new or already opened file from the beginning. |
| 324 | * If the file has not been opened yet data->fd should be set to -1. |
| 325 | * To be used with files which are read often and possibly during high |
| 326 | * memory pressure to minimize file opening which by itself requires kernel |
| 327 | * memory allocation and might result in a stall on memory stressed system. |
| 328 | */ |
| 329 | static int reread_file(struct reread_data *data, char *buf, size_t buf_size) { |
| 330 | ssize_t size; |
| 331 | |
| 332 | if (data->fd == -1) { |
| 333 | data->fd = open(data->filename, O_RDONLY | O_CLOEXEC); |
| 334 | if (data->fd == -1) { |
| 335 | ALOGE("%s open: %s", data->filename, strerror(errno)); |
| 336 | return -1; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | size = read_all(data->fd, buf, buf_size - 1); |
| 341 | if (size < 0) { |
| 342 | ALOGE("%s read: %s", data->filename, strerror(errno)); |
| 343 | close(data->fd); |
| 344 | data->fd = -1; |
| 345 | return -1; |
| 346 | } |
| 347 | ALOG_ASSERT((size_t)size < buf_size - 1, data->filename " too large"); |
| 348 | buf[size] = 0; |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 353 | static struct proc *pid_lookup(int pid) { |
| 354 | struct proc *procp; |
| 355 | |
| 356 | for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid; |
| 357 | procp = procp->pidhash_next) |
| 358 | ; |
| 359 | |
| 360 | return procp; |
| 361 | } |
| 362 | |
| 363 | static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new) |
| 364 | { |
| 365 | struct adjslot_list *next = head->next; |
| 366 | new->prev = head; |
| 367 | new->next = next; |
| 368 | next->prev = new; |
| 369 | head->next = new; |
| 370 | } |
| 371 | |
| 372 | static void adjslot_remove(struct adjslot_list *old) |
| 373 | { |
| 374 | struct adjslot_list *prev = old->prev; |
| 375 | struct adjslot_list *next = old->next; |
| 376 | next->prev = prev; |
| 377 | prev->next = next; |
| 378 | } |
| 379 | |
| 380 | static struct adjslot_list *adjslot_tail(struct adjslot_list *head) { |
| 381 | struct adjslot_list *asl = head->prev; |
| 382 | |
| 383 | return asl == head ? NULL : asl; |
| 384 | } |
| 385 | |
| 386 | static void proc_slot(struct proc *procp) { |
| 387 | int adjslot = ADJTOSLOT(procp->oomadj); |
| 388 | |
| 389 | adjslot_insert(&procadjslot_list[adjslot], &procp->asl); |
| 390 | } |
| 391 | |
| 392 | static void proc_unslot(struct proc *procp) { |
| 393 | adjslot_remove(&procp->asl); |
| 394 | } |
| 395 | |
| 396 | static void proc_insert(struct proc *procp) { |
| 397 | int hval = pid_hashfn(procp->pid); |
| 398 | |
| 399 | procp->pidhash_next = pidhash[hval]; |
| 400 | pidhash[hval] = procp; |
| 401 | proc_slot(procp); |
| 402 | } |
| 403 | |
| 404 | static int pid_remove(int pid) { |
| 405 | int hval = pid_hashfn(pid); |
| 406 | struct proc *procp; |
| 407 | struct proc *prevp; |
| 408 | |
| 409 | for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid; |
| 410 | procp = procp->pidhash_next) |
| 411 | prevp = procp; |
| 412 | |
| 413 | if (!procp) |
| 414 | return -1; |
| 415 | |
| 416 | if (!prevp) |
| 417 | pidhash[hval] = procp->pidhash_next; |
| 418 | else |
| 419 | prevp->pidhash_next = procp->pidhash_next; |
| 420 | |
| 421 | proc_unslot(procp); |
| 422 | free(procp); |
| 423 | return 0; |
| 424 | } |
| 425 | |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 426 | /* |
| 427 | * Write a string to a file. |
| 428 | * Returns false if the file does not exist. |
| 429 | */ |
| 430 | static bool writefilestring(const char *path, const char *s, |
| 431 | bool err_if_missing) { |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 432 | int fd = open(path, O_WRONLY | O_CLOEXEC); |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 433 | ssize_t len = strlen(s); |
| 434 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 435 | |
| 436 | if (fd < 0) { |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 437 | if (err_if_missing) { |
| 438 | ALOGE("Error opening %s; errno=%d", path, errno); |
| 439 | } |
| 440 | return false; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 443 | ret = TEMP_FAILURE_RETRY(write(fd, s, len)); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 444 | if (ret < 0) { |
| 445 | ALOGE("Error writing %s; errno=%d", path, errno); |
| 446 | } else if (ret < len) { |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 447 | ALOGE("Short write on %s; length=%zd", path, ret); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | close(fd); |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 451 | return true; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 454 | static void cmd_procprio(LMKD_CTRL_PACKET packet) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 455 | struct proc *procp; |
| 456 | char path[80]; |
| 457 | char val[20]; |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 458 | int soft_limit_mult; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 459 | struct lmk_procprio params; |
Suren Baghdasaryan | 4311d1e | 2018-03-20 16:03:29 -0700 | [diff] [blame] | 460 | bool is_system_server; |
| 461 | struct passwd *pwdrec; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 462 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 463 | lmkd_pack_get_procprio(packet, ¶ms); |
| 464 | |
| 465 | if (params.oomadj < OOM_SCORE_ADJ_MIN || |
| 466 | params.oomadj > OOM_SCORE_ADJ_MAX) { |
| 467 | ALOGE("Invalid PROCPRIO oomadj argument %d", params.oomadj); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 468 | return; |
| 469 | } |
| 470 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 471 | /* gid containing AID_READPROC required */ |
| 472 | /* CAP_SYS_RESOURCE required */ |
| 473 | /* CAP_DAC_OVERRIDE required */ |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 474 | snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", params.pid); |
| 475 | snprintf(val, sizeof(val), "%d", params.oomadj); |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 476 | if (!writefilestring(path, val, false)) { |
| 477 | ALOGW("Failed to open %s; errno=%d: process %d might have been killed", |
| 478 | path, errno, params.pid); |
| 479 | /* If this file does not exist the process is dead. */ |
| 480 | return; |
| 481 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 482 | |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 483 | if (use_inkernel_interface) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 484 | return; |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 485 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 486 | |
Suren Baghdasaryan | 1683024 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 487 | if (low_ram_device) { |
| 488 | if (params.oomadj >= 900) { |
| 489 | soft_limit_mult = 0; |
| 490 | } else if (params.oomadj >= 800) { |
| 491 | soft_limit_mult = 0; |
| 492 | } else if (params.oomadj >= 700) { |
| 493 | soft_limit_mult = 0; |
| 494 | } else if (params.oomadj >= 600) { |
| 495 | // Launcher should be perceptible, don't kill it. |
| 496 | params.oomadj = 200; |
| 497 | soft_limit_mult = 1; |
| 498 | } else if (params.oomadj >= 500) { |
| 499 | soft_limit_mult = 0; |
| 500 | } else if (params.oomadj >= 400) { |
| 501 | soft_limit_mult = 0; |
| 502 | } else if (params.oomadj >= 300) { |
| 503 | soft_limit_mult = 1; |
| 504 | } else if (params.oomadj >= 200) { |
| 505 | soft_limit_mult = 2; |
| 506 | } else if (params.oomadj >= 100) { |
| 507 | soft_limit_mult = 10; |
| 508 | } else if (params.oomadj >= 0) { |
| 509 | soft_limit_mult = 20; |
| 510 | } else { |
| 511 | // Persistent processes will have a large |
| 512 | // soft limit 512MB. |
| 513 | soft_limit_mult = 64; |
| 514 | } |
| 515 | |
| 516 | snprintf(path, sizeof(path), MEMCG_SYSFS_PATH |
| 517 | "apps/uid_%d/pid_%d/memory.soft_limit_in_bytes", |
| 518 | params.uid, params.pid); |
| 519 | snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA); |
| 520 | |
| 521 | /* |
| 522 | * system_server process has no memcg under /dev/memcg/apps but should be |
| 523 | * registered with lmkd. This is the best way so far to identify it. |
| 524 | */ |
| 525 | is_system_server = (params.oomadj == SYSTEM_ADJ && |
| 526 | (pwdrec = getpwnam("system")) != NULL && |
| 527 | params.uid == pwdrec->pw_uid); |
| 528 | writefilestring(path, val, !is_system_server); |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 531 | procp = pid_lookup(params.pid); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 532 | if (!procp) { |
| 533 | procp = malloc(sizeof(struct proc)); |
| 534 | if (!procp) { |
| 535 | // Oh, the irony. May need to rebuild our state. |
| 536 | return; |
| 537 | } |
| 538 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 539 | procp->pid = params.pid; |
| 540 | procp->uid = params.uid; |
| 541 | procp->oomadj = params.oomadj; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 542 | proc_insert(procp); |
| 543 | } else { |
| 544 | proc_unslot(procp); |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 545 | procp->oomadj = params.oomadj; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 546 | proc_slot(procp); |
| 547 | } |
| 548 | } |
| 549 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 550 | static void cmd_procremove(LMKD_CTRL_PACKET packet) { |
| 551 | struct lmk_procremove params; |
| 552 | |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 553 | if (use_inkernel_interface) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 554 | return; |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 555 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 556 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 557 | lmkd_pack_get_procremove(packet, ¶ms); |
| 558 | pid_remove(params.pid); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 561 | static void cmd_target(int ntargets, LMKD_CTRL_PACKET packet) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 562 | int i; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 563 | struct lmk_target target; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 564 | |
| 565 | if (ntargets > (int)ARRAY_SIZE(lowmem_adj)) |
| 566 | return; |
| 567 | |
| 568 | for (i = 0; i < ntargets; i++) { |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 569 | lmkd_pack_get_target(packet, i, &target); |
| 570 | lowmem_minfree[i] = target.minfree; |
| 571 | lowmem_adj[i] = target.oom_adj_score; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | lowmem_targets_size = ntargets; |
| 575 | |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 576 | if (has_inkernel_module) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 577 | char minfreestr[128]; |
| 578 | char killpriostr[128]; |
| 579 | |
| 580 | minfreestr[0] = '\0'; |
| 581 | killpriostr[0] = '\0'; |
| 582 | |
| 583 | for (i = 0; i < lowmem_targets_size; i++) { |
| 584 | char val[40]; |
| 585 | |
| 586 | if (i) { |
| 587 | strlcat(minfreestr, ",", sizeof(minfreestr)); |
| 588 | strlcat(killpriostr, ",", sizeof(killpriostr)); |
| 589 | } |
| 590 | |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 591 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_minfree[i] : 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 592 | strlcat(minfreestr, val, sizeof(minfreestr)); |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 593 | snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_adj[i] : 0); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 594 | strlcat(killpriostr, val, sizeof(killpriostr)); |
| 595 | } |
| 596 | |
Suren Baghdasaryan | 1ffa246 | 2018-03-20 13:53:17 -0700 | [diff] [blame] | 597 | writefilestring(INKERNEL_MINFREE_PATH, minfreestr, true); |
| 598 | writefilestring(INKERNEL_ADJ_PATH, killpriostr, true); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 602 | static void ctrl_data_close(int dsock_idx) { |
| 603 | struct epoll_event epev; |
| 604 | |
| 605 | ALOGI("closing lmkd data connection"); |
| 606 | if (epoll_ctl(epollfd, EPOLL_CTL_DEL, data_sock[dsock_idx].sock, &epev) == -1) { |
| 607 | // Log a warning and keep going |
| 608 | ALOGW("epoll_ctl for data connection socket failed; errno=%d", errno); |
| 609 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 610 | maxevents--; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 611 | |
| 612 | close(data_sock[dsock_idx].sock); |
| 613 | data_sock[dsock_idx].sock = -1; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 616 | static int ctrl_data_read(int dsock_idx, char *buf, size_t bufsz) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 617 | int ret = 0; |
| 618 | |
Suren Baghdasaryan | d716fe3 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 619 | ret = TEMP_FAILURE_RETRY(read(data_sock[dsock_idx].sock, buf, bufsz)); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 620 | |
| 621 | if (ret == -1) { |
| 622 | ALOGE("control data socket read failed; errno=%d", errno); |
| 623 | } else if (ret == 0) { |
| 624 | ALOGE("Got EOF on control data socket"); |
| 625 | ret = -1; |
| 626 | } |
| 627 | |
| 628 | return ret; |
| 629 | } |
| 630 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 631 | static void ctrl_command_handler(int dsock_idx) { |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 632 | LMKD_CTRL_PACKET packet; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 633 | int len; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 634 | enum lmk_cmd cmd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 635 | int nargs; |
| 636 | int targets; |
| 637 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 638 | len = ctrl_data_read(dsock_idx, (char *)packet, CTRL_PACKET_MAX_SIZE); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 639 | if (len <= 0) |
| 640 | return; |
| 641 | |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 642 | if (len < (int)sizeof(int)) { |
| 643 | ALOGE("Wrong control socket read length len=%d", len); |
| 644 | return; |
| 645 | } |
| 646 | |
| 647 | cmd = lmkd_pack_get_cmd(packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 648 | nargs = len / sizeof(int) - 1; |
| 649 | if (nargs < 0) |
| 650 | goto wronglen; |
| 651 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 652 | switch(cmd) { |
| 653 | case LMK_TARGET: |
| 654 | targets = nargs / 2; |
| 655 | if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj)) |
| 656 | goto wronglen; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 657 | cmd_target(targets, packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 658 | break; |
| 659 | case LMK_PROCPRIO: |
Colin Cross | fbb78c6 | 2014-06-13 14:52:43 -0700 | [diff] [blame] | 660 | if (nargs != 3) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 661 | goto wronglen; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 662 | cmd_procprio(packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 663 | break; |
| 664 | case LMK_PROCREMOVE: |
| 665 | if (nargs != 1) |
| 666 | goto wronglen; |
Suren Baghdasaryan | 0f10051 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 667 | cmd_procremove(packet); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 668 | break; |
| 669 | default: |
| 670 | ALOGE("Received unknown command code %d", cmd); |
| 671 | return; |
| 672 | } |
| 673 | |
| 674 | return; |
| 675 | |
| 676 | wronglen: |
| 677 | ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len); |
| 678 | } |
| 679 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 680 | static void ctrl_data_handler(int data, uint32_t events) { |
| 681 | if (events & EPOLLIN) { |
| 682 | ctrl_command_handler(data); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 686 | static int get_free_dsock() { |
| 687 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 688 | if (data_sock[i].sock < 0) { |
| 689 | return i; |
| 690 | } |
| 691 | } |
| 692 | return -1; |
| 693 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 694 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 695 | static void ctrl_connect_handler(int data __unused, uint32_t events __unused) { |
| 696 | struct epoll_event epev; |
| 697 | int free_dscock_idx = get_free_dsock(); |
| 698 | |
| 699 | if (free_dscock_idx < 0) { |
| 700 | /* |
| 701 | * Number of data connections exceeded max supported. This should not |
| 702 | * happen but if it does we drop all existing connections and accept |
| 703 | * the new one. This prevents inactive connections from monopolizing |
| 704 | * data socket and if we drop ActivityManager connection it will |
| 705 | * immediately reconnect. |
| 706 | */ |
| 707 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 708 | ctrl_data_close(i); |
| 709 | } |
| 710 | free_dscock_idx = 0; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 713 | data_sock[free_dscock_idx].sock = accept(ctrl_sock.sock, NULL, NULL); |
| 714 | if (data_sock[free_dscock_idx].sock < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 715 | ALOGE("lmkd control socket accept failed; errno=%d", errno); |
| 716 | return; |
| 717 | } |
| 718 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 719 | ALOGI("lmkd data connection established"); |
| 720 | /* use data to store data connection idx */ |
| 721 | data_sock[free_dscock_idx].handler_info.data = free_dscock_idx; |
| 722 | data_sock[free_dscock_idx].handler_info.handler = ctrl_data_handler; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 723 | epev.events = EPOLLIN; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 724 | epev.data.ptr = (void *)&(data_sock[free_dscock_idx].handler_info); |
| 725 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, data_sock[free_dscock_idx].sock, &epev) == -1) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 726 | ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno); |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 727 | ctrl_data_close(free_dscock_idx); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 728 | return; |
| 729 | } |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 730 | maxevents++; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 733 | #ifdef LMKD_LOG_STATS |
| 734 | static void memory_stat_parse_line(char *line, struct memory_stat *mem_st) { |
Greg Kaiser | 6bbd521 | 2018-03-23 14:16:12 -0700 | [diff] [blame] | 735 | char key[LINE_MAX + 1]; |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 736 | int64_t value; |
| 737 | |
Greg Kaiser | 6bbd521 | 2018-03-23 14:16:12 -0700 | [diff] [blame] | 738 | sscanf(line, "%" STRINGIFY(LINE_MAX) "s %" SCNd64 "", key, &value); |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 739 | |
| 740 | if (strcmp(key, "total_") < 0) { |
| 741 | return; |
| 742 | } |
| 743 | |
| 744 | if (!strcmp(key, "total_pgfault")) |
| 745 | mem_st->pgfault = value; |
| 746 | else if (!strcmp(key, "total_pgmajfault")) |
| 747 | mem_st->pgmajfault = value; |
| 748 | else if (!strcmp(key, "total_rss")) |
| 749 | mem_st->rss_in_bytes = value; |
| 750 | else if (!strcmp(key, "total_cache")) |
| 751 | mem_st->cache_in_bytes = value; |
| 752 | else if (!strcmp(key, "total_swap")) |
| 753 | mem_st->swap_in_bytes = value; |
| 754 | } |
| 755 | |
| 756 | static int memory_stat_parse(struct memory_stat *mem_st, int pid, uid_t uid) { |
| 757 | FILE *fp; |
| 758 | char buf[PATH_MAX]; |
| 759 | |
| 760 | snprintf(buf, sizeof(buf), MEMCG_PROCESS_MEMORY_STAT_PATH, uid, pid); |
| 761 | |
| 762 | fp = fopen(buf, "r"); |
| 763 | |
| 764 | if (fp == NULL) { |
Rajeev Kumar | 92b659b | 2018-02-21 19:08:15 -0800 | [diff] [blame] | 765 | ALOGE("%s open failed: %s", buf, strerror(errno)); |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 766 | return -1; |
| 767 | } |
| 768 | |
| 769 | while (fgets(buf, PAGE_SIZE, fp) != NULL ) { |
| 770 | memory_stat_parse_line(buf, mem_st); |
| 771 | } |
| 772 | fclose(fp); |
| 773 | |
| 774 | return 0; |
| 775 | } |
| 776 | #endif |
| 777 | |
Suren Baghdasaryan | da0bc05 | 2018-04-13 13:11:51 -0700 | [diff] [blame] | 778 | /* /prop/zoneinfo parsing routines */ |
| 779 | static int64_t zoneinfo_parse_protection(char *cp) { |
| 780 | int64_t max = 0; |
| 781 | long long zoneval; |
| 782 | char *save_ptr; |
| 783 | |
| 784 | for (cp = strtok_r(cp, "(), ", &save_ptr); cp; |
| 785 | cp = strtok_r(NULL, "), ", &save_ptr)) { |
| 786 | zoneval = strtoll(cp, &cp, 0); |
| 787 | if (zoneval > max) { |
| 788 | max = (zoneval > INT64_MAX) ? INT64_MAX : zoneval; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | return max; |
| 793 | } |
| 794 | |
| 795 | static bool zoneinfo_parse_line(char *line, union zoneinfo *zi) { |
| 796 | char *cp = line; |
| 797 | char *ap; |
| 798 | char *save_ptr; |
| 799 | int64_t val; |
| 800 | int field_idx; |
| 801 | |
| 802 | cp = strtok_r(line, " ", &save_ptr); |
| 803 | if (!cp) { |
| 804 | return true; |
| 805 | } |
| 806 | |
| 807 | if (!strcmp(cp, "protection:")) { |
| 808 | ap = strtok_r(NULL, ")", &save_ptr); |
| 809 | } else { |
| 810 | ap = strtok_r(NULL, " ", &save_ptr); |
| 811 | } |
| 812 | |
| 813 | if (!ap) { |
| 814 | return true; |
| 815 | } |
| 816 | |
| 817 | switch (match_field(cp, ap, zoneinfo_field_names, |
| 818 | ZI_FIELD_COUNT, &val, &field_idx)) { |
| 819 | case (PARSE_SUCCESS): |
| 820 | zi->arr[field_idx] += val; |
| 821 | break; |
| 822 | case (NO_MATCH): |
| 823 | if (!strcmp(cp, "protection:")) { |
| 824 | zi->field.totalreserve_pages += |
| 825 | zoneinfo_parse_protection(ap); |
| 826 | } |
| 827 | break; |
| 828 | case (PARSE_FAIL): |
| 829 | default: |
| 830 | return false; |
| 831 | } |
| 832 | return true; |
| 833 | } |
| 834 | |
| 835 | static int zoneinfo_parse(union zoneinfo *zi) { |
| 836 | static struct reread_data file_data = { |
| 837 | .filename = ZONEINFO_PATH, |
| 838 | .fd = -1, |
| 839 | }; |
| 840 | char buf[PAGE_SIZE]; |
| 841 | char *save_ptr; |
| 842 | char *line; |
| 843 | |
| 844 | memset(zi, 0, sizeof(union zoneinfo)); |
| 845 | |
| 846 | if (reread_file(&file_data, buf, sizeof(buf)) < 0) { |
| 847 | return -1; |
| 848 | } |
| 849 | |
| 850 | for (line = strtok_r(buf, "\n", &save_ptr); line; |
| 851 | line = strtok_r(NULL, "\n", &save_ptr)) { |
| 852 | if (!zoneinfo_parse_line(line, zi)) { |
| 853 | ALOGE("%s parse error", file_data.filename); |
| 854 | return -1; |
| 855 | } |
| 856 | } |
| 857 | zi->field.totalreserve_pages += zi->field.high; |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | /* /prop/meminfo parsing routines */ |
| 863 | static bool meminfo_parse_line(char *line, union meminfo *mi) { |
| 864 | char *cp = line; |
| 865 | char *ap; |
| 866 | char *save_ptr; |
| 867 | int64_t val; |
| 868 | int field_idx; |
| 869 | enum field_match_result match_res; |
| 870 | |
| 871 | cp = strtok_r(line, " ", &save_ptr); |
| 872 | if (!cp) { |
| 873 | return false; |
| 874 | } |
| 875 | |
| 876 | ap = strtok_r(NULL, " ", &save_ptr); |
| 877 | if (!ap) { |
| 878 | return false; |
| 879 | } |
| 880 | |
| 881 | match_res = match_field(cp, ap, meminfo_field_names, MI_FIELD_COUNT, |
| 882 | &val, &field_idx); |
| 883 | if (match_res == PARSE_SUCCESS) { |
| 884 | mi->arr[field_idx] = val / page_k; |
| 885 | } |
| 886 | return (match_res != PARSE_FAIL); |
| 887 | } |
| 888 | |
| 889 | static int meminfo_parse(union meminfo *mi) { |
| 890 | static struct reread_data file_data = { |
| 891 | .filename = MEMINFO_PATH, |
| 892 | .fd = -1, |
| 893 | }; |
| 894 | char buf[PAGE_SIZE]; |
| 895 | char *save_ptr; |
| 896 | char *line; |
| 897 | |
| 898 | memset(mi, 0, sizeof(union meminfo)); |
| 899 | |
| 900 | if (reread_file(&file_data, buf, sizeof(buf)) < 0) { |
| 901 | return -1; |
| 902 | } |
| 903 | |
| 904 | for (line = strtok_r(buf, "\n", &save_ptr); line; |
| 905 | line = strtok_r(NULL, "\n", &save_ptr)) { |
| 906 | if (!meminfo_parse_line(line, mi)) { |
| 907 | ALOGE("%s parse error", file_data.filename); |
| 908 | return -1; |
| 909 | } |
| 910 | } |
| 911 | mi->field.nr_file_pages = mi->field.cached + mi->field.swap_cached + |
| 912 | mi->field.buffers; |
| 913 | |
| 914 | return 0; |
| 915 | } |
| 916 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 917 | static int proc_get_size(int pid) { |
| 918 | char path[PATH_MAX]; |
| 919 | char line[LINE_MAX]; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 920 | int fd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 921 | int rss = 0; |
| 922 | int total; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 923 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 924 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 925 | /* gid containing AID_READPROC required */ |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 926 | snprintf(path, PATH_MAX, "/proc/%d/statm", pid); |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 927 | fd = open(path, O_RDONLY | O_CLOEXEC); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 928 | if (fd == -1) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 929 | return -1; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 930 | |
| 931 | ret = read_all(fd, line, sizeof(line) - 1); |
| 932 | if (ret < 0) { |
| 933 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 934 | return -1; |
| 935 | } |
| 936 | |
| 937 | sscanf(line, "%d %d ", &total, &rss); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 938 | close(fd); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 939 | return rss; |
| 940 | } |
| 941 | |
| 942 | static char *proc_get_name(int pid) { |
| 943 | char path[PATH_MAX]; |
| 944 | static char line[LINE_MAX]; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 945 | int fd; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 946 | char *cp; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 947 | ssize_t ret; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 948 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 949 | /* gid containing AID_READPROC required */ |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 950 | snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid); |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 951 | fd = open(path, O_RDONLY | O_CLOEXEC); |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 952 | if (fd == -1) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 953 | return NULL; |
Colin Cross | ce85d95 | 2014-07-11 17:53:27 -0700 | [diff] [blame] | 954 | ret = read_all(fd, line, sizeof(line) - 1); |
| 955 | close(fd); |
| 956 | if (ret < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 957 | return NULL; |
| 958 | } |
| 959 | |
| 960 | cp = strchr(line, ' '); |
| 961 | if (cp) |
| 962 | *cp = '\0'; |
| 963 | |
| 964 | return line; |
| 965 | } |
| 966 | |
| 967 | static struct proc *proc_adj_lru(int oomadj) { |
| 968 | return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]); |
| 969 | } |
| 970 | |
Suren Baghdasaryan | 662492a | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 971 | static struct proc *proc_get_heaviest(int oomadj) { |
| 972 | struct adjslot_list *head = &procadjslot_list[ADJTOSLOT(oomadj)]; |
| 973 | struct adjslot_list *curr = head->next; |
| 974 | struct proc *maxprocp = NULL; |
| 975 | int maxsize = 0; |
| 976 | while (curr != head) { |
| 977 | int pid = ((struct proc *)curr)->pid; |
| 978 | int tasksize = proc_get_size(pid); |
| 979 | if (tasksize <= 0) { |
| 980 | struct adjslot_list *next = curr->next; |
| 981 | pid_remove(pid); |
| 982 | curr = next; |
| 983 | } else { |
| 984 | if (tasksize > maxsize) { |
| 985 | maxsize = tasksize; |
| 986 | maxprocp = (struct proc *)curr; |
| 987 | } |
| 988 | curr = curr->next; |
| 989 | } |
| 990 | } |
| 991 | return maxprocp; |
| 992 | } |
| 993 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 994 | /* Kill one process specified by procp. Returns the size of the process killed */ |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 995 | static int kill_one_process(struct proc* procp, int min_score_adj, |
| 996 | enum vmpressure_level level) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 997 | int pid = procp->pid; |
| 998 | uid_t uid = procp->uid; |
| 999 | char *taskname; |
| 1000 | int tasksize; |
| 1001 | int r; |
| 1002 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1003 | #ifdef LMKD_LOG_STATS |
Rajeev Kumar | 92b659b | 2018-02-21 19:08:15 -0800 | [diff] [blame] | 1004 | struct memory_stat mem_st = {}; |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1005 | int memory_stat_parse_result = -1; |
| 1006 | #endif |
| 1007 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1008 | taskname = proc_get_name(pid); |
| 1009 | if (!taskname) { |
| 1010 | pid_remove(pid); |
| 1011 | return -1; |
| 1012 | } |
| 1013 | |
| 1014 | tasksize = proc_get_size(pid); |
| 1015 | if (tasksize <= 0) { |
| 1016 | pid_remove(pid); |
| 1017 | return -1; |
| 1018 | } |
| 1019 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1020 | #ifdef LMKD_LOG_STATS |
| 1021 | if (enable_stats_log) { |
| 1022 | memory_stat_parse_result = memory_stat_parse(&mem_st, pid, uid); |
| 1023 | } |
| 1024 | #endif |
| 1025 | |
Suren Baghdasaryan | c713559 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 1026 | TRACE_KILL_START(pid); |
| 1027 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1028 | /* CAP_KILL required */ |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1029 | r = kill(pid, SIGKILL); |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 1030 | ALOGI( |
| 1031 | "Killing '%s' (%d), uid %d, adj %d\n" |
Suren Baghdasaryan | d07a94f | 2018-03-20 16:25:54 -0700 | [diff] [blame] | 1032 | " to free %ldkB because system is under %s memory pressure (min_oom_adj=%d)\n", |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1033 | taskname, pid, uid, procp->oomadj, tasksize * page_k, |
| 1034 | level_name[level], min_score_adj); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1035 | pid_remove(pid); |
| 1036 | |
Suren Baghdasaryan | c713559 | 2018-01-04 10:43:58 -0800 | [diff] [blame] | 1037 | TRACE_KILL_END(); |
| 1038 | |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1039 | if (r) { |
Mark Salyzyn | 919f538 | 2018-02-04 15:27:23 -0800 | [diff] [blame] | 1040 | ALOGE("kill(%d): errno=%d", pid, errno); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1041 | return -1; |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1042 | } else { |
| 1043 | #ifdef LMKD_LOG_STATS |
| 1044 | if (memory_stat_parse_result == 0) { |
| 1045 | stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname, |
| 1046 | procp->oomadj, mem_st.pgfault, mem_st.pgmajfault, mem_st.rss_in_bytes, |
| 1047 | mem_st.cache_in_bytes, mem_st.swap_in_bytes); |
| 1048 | } |
| 1049 | #endif |
| 1050 | return tasksize; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1051 | } |
Mark Salyzyn | 919f538 | 2018-02-04 15:27:23 -0800 | [diff] [blame] | 1052 | |
| 1053 | return tasksize; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | /* |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1057 | * Find processes to kill to free required number of pages. |
| 1058 | * If pages_to_free is set to 0 only one process will be killed. |
| 1059 | * Returns the size of the killed processes. |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1060 | */ |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1061 | static int find_and_kill_processes(enum vmpressure_level level, |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1062 | int min_score_adj, int pages_to_free) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1063 | int i; |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1064 | int killed_size; |
| 1065 | int pages_freed = 0; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1066 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1067 | #ifdef LMKD_LOG_STATS |
Yang Lu | 5564f4e | 2018-05-15 04:59:44 +0000 | [diff] [blame] | 1068 | bool lmk_state_change_start = false; |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1069 | #endif |
| 1070 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 1071 | for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) { |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1072 | struct proc *procp; |
| 1073 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1074 | while (true) { |
Suren Baghdasaryan | 818b59b | 2018-04-13 11:49:54 -0700 | [diff] [blame] | 1075 | procp = kill_heaviest_task ? |
| 1076 | proc_get_heaviest(i) : proc_adj_lru(i); |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1077 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1078 | if (!procp) |
| 1079 | break; |
| 1080 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1081 | killed_size = kill_one_process(procp, min_score_adj, level); |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1082 | if (killed_size >= 0) { |
Yang Lu | 5564f4e | 2018-05-15 04:59:44 +0000 | [diff] [blame] | 1083 | #ifdef LMKD_LOG_STATS |
| 1084 | if (enable_stats_log && !lmk_state_change_start) { |
| 1085 | lmk_state_change_start = true; |
| 1086 | stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED, |
| 1087 | LMK_STATE_CHANGE_START); |
| 1088 | } |
| 1089 | #endif |
| 1090 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1091 | pages_freed += killed_size; |
| 1092 | if (pages_freed >= pages_to_free) { |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1093 | |
| 1094 | #ifdef LMKD_LOG_STATS |
Yang Lu | 5564f4e | 2018-05-15 04:59:44 +0000 | [diff] [blame] | 1095 | if (enable_stats_log && lmk_state_change_start) { |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1096 | stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED, |
| 1097 | LMK_STATE_CHANGE_STOP); |
| 1098 | } |
| 1099 | #endif |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1100 | return pages_freed; |
| 1101 | } |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | } |
| 1105 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1106 | #ifdef LMKD_LOG_STATS |
Yang Lu | 5564f4e | 2018-05-15 04:59:44 +0000 | [diff] [blame] | 1107 | if (enable_stats_log && lmk_state_change_start) { |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1108 | stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED, LMK_STATE_CHANGE_STOP); |
| 1109 | } |
| 1110 | #endif |
| 1111 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1112 | return pages_freed; |
Colin Cross | 16b0946 | 2014-07-14 12:39:56 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Suren Baghdasaryan | d716fe3 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1115 | static int64_t get_memory_usage(struct reread_data *file_data) { |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1116 | int ret; |
| 1117 | int64_t mem_usage; |
| 1118 | char buf[32]; |
Suren Baghdasaryan | d716fe3 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1119 | |
| 1120 | if (reread_file(file_data, buf, sizeof(buf)) < 0) { |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1121 | return -1; |
| 1122 | } |
| 1123 | |
Suren Baghdasaryan | d716fe3 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1124 | if (!parse_int64(buf, &mem_usage)) { |
| 1125 | ALOGE("%s parse error", file_data->filename); |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1126 | return -1; |
| 1127 | } |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1128 | if (mem_usage == 0) { |
| 1129 | ALOGE("No memory!"); |
| 1130 | return -1; |
| 1131 | } |
| 1132 | return mem_usage; |
| 1133 | } |
| 1134 | |
Suren Baghdasaryan | 9ac54eb | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1135 | void record_low_pressure_levels(union meminfo *mi) { |
| 1136 | if (low_pressure_mem.min_nr_free_pages == -1 || |
| 1137 | low_pressure_mem.min_nr_free_pages > mi->field.nr_free_pages) { |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1138 | if (debug_process_killing) { |
Suren Baghdasaryan | 9ac54eb | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1139 | ALOGI("Low pressure min memory update from %" PRId64 " to %" PRId64, |
| 1140 | low_pressure_mem.min_nr_free_pages, mi->field.nr_free_pages); |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1141 | } |
Suren Baghdasaryan | 9ac54eb | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1142 | low_pressure_mem.min_nr_free_pages = mi->field.nr_free_pages; |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1143 | } |
| 1144 | /* |
| 1145 | * Free memory at low vmpressure events occasionally gets spikes, |
| 1146 | * possibly a stale low vmpressure event with memory already |
| 1147 | * freed up (no memory pressure should have been reported). |
Suren Baghdasaryan | 9ac54eb | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1148 | * Ignore large jumps in max_nr_free_pages that would mess up our stats. |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1149 | */ |
Suren Baghdasaryan | 9ac54eb | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1150 | if (low_pressure_mem.max_nr_free_pages == -1 || |
| 1151 | (low_pressure_mem.max_nr_free_pages < mi->field.nr_free_pages && |
| 1152 | mi->field.nr_free_pages - low_pressure_mem.max_nr_free_pages < |
| 1153 | low_pressure_mem.max_nr_free_pages * 0.1)) { |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1154 | if (debug_process_killing) { |
Suren Baghdasaryan | 9ac54eb | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1155 | ALOGI("Low pressure max memory update from %" PRId64 " to %" PRId64, |
| 1156 | low_pressure_mem.max_nr_free_pages, mi->field.nr_free_pages); |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1157 | } |
Suren Baghdasaryan | 9ac54eb | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1158 | low_pressure_mem.max_nr_free_pages = mi->field.nr_free_pages; |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1162 | enum vmpressure_level upgrade_level(enum vmpressure_level level) { |
| 1163 | return (enum vmpressure_level)((level < VMPRESS_LEVEL_CRITICAL) ? |
| 1164 | level + 1 : level); |
| 1165 | } |
| 1166 | |
| 1167 | enum vmpressure_level downgrade_level(enum vmpressure_level level) { |
| 1168 | return (enum vmpressure_level)((level > VMPRESS_LEVEL_LOW) ? |
| 1169 | level - 1 : level); |
| 1170 | } |
| 1171 | |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1172 | static inline unsigned long get_time_diff_ms(struct timeval *from, |
| 1173 | struct timeval *to) { |
| 1174 | return (to->tv_sec - from->tv_sec) * 1000 + |
| 1175 | (to->tv_usec - from->tv_usec) / 1000; |
| 1176 | } |
| 1177 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1178 | static void mp_event_common(int data, uint32_t events __unused) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1179 | int ret; |
| 1180 | unsigned long long evcount; |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1181 | int64_t mem_usage, memsw_usage; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1182 | int64_t mem_pressure; |
Suren Baghdasaryan | e82e15c | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 1183 | enum vmpressure_level lvl; |
Suren Baghdasaryan | 9ac54eb | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1184 | union meminfo mi; |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1185 | union zoneinfo zi; |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1186 | static struct timeval last_report_tm; |
| 1187 | static unsigned long skip_count = 0; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1188 | enum vmpressure_level level = (enum vmpressure_level)data; |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1189 | long other_free = 0, other_file = 0; |
| 1190 | int min_score_adj; |
| 1191 | int pages_to_free = 0; |
| 1192 | int minfree = 0; |
Suren Baghdasaryan | d716fe3 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1193 | static struct reread_data mem_usage_file_data = { |
| 1194 | .filename = MEMCG_MEMORY_USAGE, |
| 1195 | .fd = -1, |
| 1196 | }; |
| 1197 | static struct reread_data memsw_usage_file_data = { |
| 1198 | .filename = MEMCG_MEMORYSW_USAGE, |
| 1199 | .fd = -1, |
| 1200 | }; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1201 | |
Suren Baghdasaryan | e82e15c | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 1202 | /* |
| 1203 | * Check all event counters from low to critical |
| 1204 | * and upgrade to the highest priority one. By reading |
| 1205 | * eventfd we also reset the event counters. |
| 1206 | */ |
| 1207 | for (lvl = VMPRESS_LEVEL_LOW; lvl < VMPRESS_LEVEL_COUNT; lvl++) { |
| 1208 | if (mpevfd[lvl] != -1 && |
Suren Baghdasaryan | d716fe3 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1209 | TEMP_FAILURE_RETRY(read(mpevfd[lvl], |
| 1210 | &evcount, sizeof(evcount))) > 0 && |
Suren Baghdasaryan | e82e15c | 2018-01-04 09:16:21 -0800 | [diff] [blame] | 1211 | evcount > 0 && lvl > level) { |
| 1212 | level = lvl; |
| 1213 | } |
| 1214 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1215 | |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1216 | if (kill_timeout_ms) { |
| 1217 | struct timeval curr_tm; |
| 1218 | gettimeofday(&curr_tm, NULL); |
| 1219 | if (get_time_diff_ms(&last_report_tm, &curr_tm) < kill_timeout_ms) { |
| 1220 | skip_count++; |
| 1221 | return; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | if (skip_count > 0) { |
Suren Baghdasaryan | da88b24 | 2018-05-10 16:10:56 -0700 | [diff] [blame] | 1226 | ALOGI("%lu memory pressure events were skipped after a kill!", |
| 1227 | skip_count); |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1228 | skip_count = 0; |
| 1229 | } |
| 1230 | |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1231 | if (meminfo_parse(&mi) < 0 || zoneinfo_parse(&zi) < 0) { |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1232 | ALOGE("Failed to get free memory!"); |
| 1233 | return; |
| 1234 | } |
| 1235 | |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1236 | if (use_minfree_levels) { |
| 1237 | int i; |
| 1238 | |
| 1239 | other_free = mi.field.nr_free_pages - zi.field.totalreserve_pages; |
| 1240 | if (mi.field.nr_file_pages > (mi.field.shmem + mi.field.unevictable + mi.field.swap_cached)) { |
| 1241 | other_file = (mi.field.nr_file_pages - mi.field.shmem - |
| 1242 | mi.field.unevictable - mi.field.swap_cached); |
| 1243 | } else { |
| 1244 | other_file = 0; |
| 1245 | } |
| 1246 | |
| 1247 | min_score_adj = OOM_SCORE_ADJ_MAX + 1; |
| 1248 | for (i = 0; i < lowmem_targets_size; i++) { |
| 1249 | minfree = lowmem_minfree[i]; |
| 1250 | if (other_free < minfree && other_file < minfree) { |
| 1251 | min_score_adj = lowmem_adj[i]; |
| 1252 | break; |
| 1253 | } |
| 1254 | } |
| 1255 | |
Suren Baghdasaryan | 1683024 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 1256 | if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) { |
| 1257 | if (debug_process_killing) { |
| 1258 | ALOGI("Ignore %s memory pressure event " |
| 1259 | "(free memory=%ldkB, cache=%ldkB, limit=%ldkB)", |
| 1260 | level_name[level], other_free * page_k, other_file * page_k, |
| 1261 | (long)lowmem_minfree[lowmem_targets_size - 1] * page_k); |
| 1262 | } |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1263 | return; |
Suren Baghdasaryan | 1683024 | 2018-05-18 14:42:00 -0700 | [diff] [blame] | 1264 | } |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1265 | |
| 1266 | /* Free up enough pages to push over the highest minfree level */ |
| 1267 | pages_to_free = lowmem_minfree[lowmem_targets_size - 1] - |
| 1268 | ((other_free < other_file) ? other_free : other_file); |
| 1269 | goto do_kill; |
| 1270 | } |
| 1271 | |
Suren Baghdasaryan | 9ac54eb | 2018-04-13 13:41:12 -0700 | [diff] [blame] | 1272 | if (level == VMPRESS_LEVEL_LOW) { |
| 1273 | record_low_pressure_levels(&mi); |
| 1274 | } |
| 1275 | |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1276 | if (level_oomadj[level] > OOM_SCORE_ADJ_MAX) { |
| 1277 | /* Do not monitor this pressure level */ |
| 1278 | return; |
| 1279 | } |
| 1280 | |
Suren Baghdasaryan | d716fe3 | 2018-04-13 12:43:41 -0700 | [diff] [blame] | 1281 | if ((mem_usage = get_memory_usage(&mem_usage_file_data)) < 0) { |
| 1282 | goto do_kill; |
| 1283 | } |
| 1284 | if ((memsw_usage = get_memory_usage(&memsw_usage_file_data)) < 0) { |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1285 | goto do_kill; |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1286 | } |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1287 | |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1288 | // Calculate percent for swappinness. |
| 1289 | mem_pressure = (mem_usage * 100) / memsw_usage; |
| 1290 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1291 | if (enable_pressure_upgrade && level != VMPRESS_LEVEL_CRITICAL) { |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1292 | // We are swapping too much. |
| 1293 | if (mem_pressure < upgrade_pressure) { |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1294 | level = upgrade_level(level); |
| 1295 | if (debug_process_killing) { |
| 1296 | ALOGI("Event upgraded to %s", level_name[level]); |
| 1297 | } |
Robert Benea | c47f299 | 2017-08-21 15:18:31 -0700 | [diff] [blame] | 1298 | } |
| 1299 | } |
| 1300 | |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1301 | // If the pressure is larger than downgrade_pressure lmk will not |
| 1302 | // kill any process, since enough memory is available. |
| 1303 | if (mem_pressure > downgrade_pressure) { |
| 1304 | if (debug_process_killing) { |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1305 | ALOGI("Ignore %s memory pressure", level_name[level]); |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1306 | } |
| 1307 | return; |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1308 | } else if (level == VMPRESS_LEVEL_CRITICAL && |
| 1309 | mem_pressure > upgrade_pressure) { |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1310 | if (debug_process_killing) { |
| 1311 | ALOGI("Downgrade critical memory pressure"); |
| 1312 | } |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1313 | // Downgrade event, since enough memory available. |
| 1314 | level = downgrade_level(level); |
Robert Benea | 6e8e710 | 2017-09-13 15:20:30 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1317 | do_kill: |
Suren Baghdasaryan | ff61afb | 2018-04-13 11:45:38 -0700 | [diff] [blame] | 1318 | if (low_ram_device) { |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1319 | /* For Go devices kill only one task */ |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1320 | if (find_and_kill_processes(level, level_oomadj[level], 0) == 0) { |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1321 | if (debug_process_killing) { |
| 1322 | ALOGI("Nothing to kill"); |
| 1323 | } |
| 1324 | } |
| 1325 | } else { |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1326 | int pages_freed; |
| 1327 | |
| 1328 | if (!use_minfree_levels) { |
| 1329 | /* If pressure level is less than critical and enough free swap then ignore */ |
| 1330 | if (level < VMPRESS_LEVEL_CRITICAL && |
| 1331 | mi.field.free_swap > low_pressure_mem.max_nr_free_pages) { |
| 1332 | if (debug_process_killing) { |
| 1333 | ALOGI("Ignoring pressure since %" PRId64 |
| 1334 | " swap pages are available ", |
| 1335 | mi.field.free_swap); |
| 1336 | } |
| 1337 | return; |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1338 | } |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1339 | /* Free up enough memory to downgrate the memory pressure to low level */ |
| 1340 | if (mi.field.nr_free_pages < low_pressure_mem.max_nr_free_pages) { |
| 1341 | pages_to_free = low_pressure_mem.max_nr_free_pages - |
| 1342 | mi.field.nr_free_pages; |
| 1343 | } else { |
| 1344 | if (debug_process_killing) { |
| 1345 | ALOGI("Ignoring pressure since more memory is " |
| 1346 | "available (%" PRId64 ") than watermark (%" PRId64 ")", |
| 1347 | mi.field.nr_free_pages, low_pressure_mem.max_nr_free_pages); |
| 1348 | } |
| 1349 | return; |
| 1350 | } |
| 1351 | min_score_adj = level_oomadj[level]; |
Suren Baghdasaryan | 65f54a2 | 2018-01-17 17:17:44 -0800 | [diff] [blame] | 1352 | } |
| 1353 | |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1354 | pages_freed = find_and_kill_processes(level, min_score_adj, pages_to_free); |
Suren Baghdasaryan | da88b24 | 2018-05-10 16:10:56 -0700 | [diff] [blame] | 1355 | |
| 1356 | if (use_minfree_levels) { |
| 1357 | ALOGI("Killing because cache %ldkB is below " |
| 1358 | "limit %ldkB for oom_adj %d\n" |
| 1359 | " Free memory is %ldkB %s reserved", |
| 1360 | other_file * page_k, minfree * page_k, min_score_adj, |
| 1361 | other_free * page_k, other_free >= 0 ? "above" : "below"); |
| 1362 | } |
| 1363 | |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1364 | if (pages_freed < pages_to_free) { |
Suren Baghdasaryan | da88b24 | 2018-05-10 16:10:56 -0700 | [diff] [blame] | 1365 | ALOGI("Unable to free enough memory (pages to free=%d, pages freed=%d)", |
| 1366 | pages_to_free, pages_freed); |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1367 | } else { |
Suren Baghdasaryan | da88b24 | 2018-05-10 16:10:56 -0700 | [diff] [blame] | 1368 | ALOGI("Reclaimed enough memory (pages to free=%d, pages freed=%d)", |
| 1369 | pages_to_free, pages_freed); |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1370 | gettimeofday(&last_report_tm, NULL); |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 1371 | } |
Colin Cross | f8857cc | 2014-07-11 17:16:56 -0700 | [diff] [blame] | 1372 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1375 | static bool init_mp_common(enum vmpressure_level level) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1376 | int mpfd; |
| 1377 | int evfd; |
| 1378 | int evctlfd; |
| 1379 | char buf[256]; |
| 1380 | struct epoll_event epev; |
| 1381 | int ret; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1382 | int level_idx = (int)level; |
| 1383 | const char *levelstr = level_name[level_idx]; |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1384 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1385 | /* gid containing AID_SYSTEM required */ |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1386 | mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1387 | if (mpfd < 0) { |
| 1388 | ALOGI("No kernel memory.pressure_level support (errno=%d)", errno); |
| 1389 | goto err_open_mpfd; |
| 1390 | } |
| 1391 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1392 | evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY | O_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1393 | if (evctlfd < 0) { |
| 1394 | ALOGI("No kernel memory cgroup event control (errno=%d)", errno); |
| 1395 | goto err_open_evctlfd; |
| 1396 | } |
| 1397 | |
Nick Kralevich | c68c886 | 2015-12-18 20:52:37 -0800 | [diff] [blame] | 1398 | evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1399 | if (evfd < 0) { |
| 1400 | ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno); |
| 1401 | goto err_eventfd; |
| 1402 | } |
| 1403 | |
| 1404 | ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr); |
| 1405 | if (ret >= (ssize_t)sizeof(buf)) { |
| 1406 | ALOGE("cgroup.event_control line overflow for level %s", levelstr); |
| 1407 | goto err; |
| 1408 | } |
| 1409 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1410 | ret = TEMP_FAILURE_RETRY(write(evctlfd, buf, strlen(buf) + 1)); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1411 | if (ret == -1) { |
| 1412 | ALOGE("cgroup.event_control write failed for level %s; errno=%d", |
| 1413 | levelstr, errno); |
| 1414 | goto err; |
| 1415 | } |
| 1416 | |
| 1417 | epev.events = EPOLLIN; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1418 | /* use data to store event level */ |
| 1419 | vmpressure_hinfo[level_idx].data = level_idx; |
| 1420 | vmpressure_hinfo[level_idx].handler = mp_event_common; |
| 1421 | epev.data.ptr = (void *)&vmpressure_hinfo[level_idx]; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1422 | ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev); |
| 1423 | if (ret == -1) { |
| 1424 | ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno); |
| 1425 | goto err; |
| 1426 | } |
| 1427 | maxevents++; |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1428 | mpevfd[level] = evfd; |
Suren Baghdasaryan | 1bd2fc4 | 2018-01-04 08:54:53 -0800 | [diff] [blame] | 1429 | close(evctlfd); |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1430 | return true; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1431 | |
| 1432 | err: |
| 1433 | close(evfd); |
| 1434 | err_eventfd: |
| 1435 | close(evctlfd); |
| 1436 | err_open_evctlfd: |
| 1437 | close(mpfd); |
| 1438 | err_open_mpfd: |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1439 | return false; |
Robert Benea | 673e276 | 2017-06-01 16:32:31 -0700 | [diff] [blame] | 1440 | } |
| 1441 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1442 | static int init(void) { |
| 1443 | struct epoll_event epev; |
| 1444 | int i; |
| 1445 | int ret; |
| 1446 | |
| 1447 | page_k = sysconf(_SC_PAGESIZE); |
| 1448 | if (page_k == -1) |
| 1449 | page_k = PAGE_SIZE; |
| 1450 | page_k /= 1024; |
| 1451 | |
| 1452 | epollfd = epoll_create(MAX_EPOLL_EVENTS); |
| 1453 | if (epollfd == -1) { |
| 1454 | ALOGE("epoll_create failed (errno=%d)", errno); |
| 1455 | return -1; |
| 1456 | } |
| 1457 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1458 | // mark data connections as not connected |
| 1459 | for (int i = 0; i < MAX_DATA_CONN; i++) { |
| 1460 | data_sock[i].sock = -1; |
| 1461 | } |
| 1462 | |
| 1463 | ctrl_sock.sock = android_get_control_socket("lmkd"); |
| 1464 | if (ctrl_sock.sock < 0) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1465 | ALOGE("get lmkd control socket failed"); |
| 1466 | return -1; |
| 1467 | } |
| 1468 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1469 | ret = listen(ctrl_sock.sock, MAX_DATA_CONN); |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1470 | if (ret < 0) { |
| 1471 | ALOGE("lmkd control socket listen failed (errno=%d)", errno); |
| 1472 | return -1; |
| 1473 | } |
| 1474 | |
| 1475 | epev.events = EPOLLIN; |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1476 | ctrl_sock.handler_info.handler = ctrl_connect_handler; |
| 1477 | epev.data.ptr = (void *)&(ctrl_sock.handler_info); |
| 1478 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_sock.sock, &epev) == -1) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1479 | ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno); |
| 1480 | return -1; |
| 1481 | } |
| 1482 | maxevents++; |
| 1483 | |
Robert Benea | 164baeb | 2017-09-11 16:53:28 -0700 | [diff] [blame] | 1484 | has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK); |
Suren Baghdasaryan | 979591b | 2018-01-18 17:27:30 -0800 | [diff] [blame] | 1485 | use_inkernel_interface = has_inkernel_module; |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1486 | |
| 1487 | if (use_inkernel_interface) { |
| 1488 | ALOGI("Using in-kernel low memory killer interface"); |
| 1489 | } else { |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1490 | if (!init_mp_common(VMPRESS_LEVEL_LOW) || |
| 1491 | !init_mp_common(VMPRESS_LEVEL_MEDIUM) || |
| 1492 | !init_mp_common(VMPRESS_LEVEL_CRITICAL)) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1493 | ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer"); |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1494 | return -1; |
| 1495 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
Chong Zhang | 0a4acdf | 2015-10-14 16:19:53 -0700 | [diff] [blame] | 1498 | for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) { |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1499 | procadjslot_list[i].next = &procadjslot_list[i]; |
| 1500 | procadjslot_list[i].prev = &procadjslot_list[i]; |
| 1501 | } |
| 1502 | |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | static void mainloop(void) { |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1507 | struct event_handler_info* handler_info; |
| 1508 | struct epoll_event *evt; |
| 1509 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1510 | while (1) { |
| 1511 | struct epoll_event events[maxevents]; |
| 1512 | int nevents; |
| 1513 | int i; |
| 1514 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1515 | nevents = epoll_wait(epollfd, events, maxevents, -1); |
| 1516 | |
| 1517 | if (nevents == -1) { |
| 1518 | if (errno == EINTR) |
| 1519 | continue; |
| 1520 | ALOGE("epoll_wait failed (errno=%d)", errno); |
| 1521 | continue; |
| 1522 | } |
| 1523 | |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1524 | /* |
| 1525 | * First pass to see if any data socket connections were dropped. |
| 1526 | * Dropped connection should be handled before any other events |
| 1527 | * to deallocate data connection and correctly handle cases when |
| 1528 | * connection gets dropped and reestablished in the same epoll cycle. |
| 1529 | * In such cases it's essential to handle connection closures first. |
| 1530 | */ |
| 1531 | for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) { |
| 1532 | if ((evt->events & EPOLLHUP) && evt->data.ptr) { |
| 1533 | ALOGI("lmkd data connection dropped"); |
| 1534 | handler_info = (struct event_handler_info*)evt->data.ptr; |
| 1535 | ctrl_data_close(handler_info->data); |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | /* Second pass to handle all other events */ |
| 1540 | for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) { |
| 1541 | if (evt->events & EPOLLERR) |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1542 | ALOGD("EPOLLERR on event #%d", i); |
Suren Baghdasaryan | 3cfb2c8 | 2018-01-26 12:51:19 -0800 | [diff] [blame] | 1543 | if (evt->events & EPOLLHUP) { |
| 1544 | /* This case was handled in the first pass */ |
| 1545 | continue; |
| 1546 | } |
| 1547 | if (evt->data.ptr) { |
| 1548 | handler_info = (struct event_handler_info*)evt->data.ptr; |
| 1549 | handler_info->handler(handler_info->data, evt->events); |
| 1550 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | |
Mark Salyzyn | e6ed68b | 2014-04-30 13:36:35 -0700 | [diff] [blame] | 1555 | int main(int argc __unused, char **argv __unused) { |
Colin Cross | 1a0d9be | 2014-07-14 14:31:15 -0700 | [diff] [blame] | 1556 | struct sched_param param = { |
| 1557 | .sched_priority = 1, |
| 1558 | }; |
| 1559 | |
Suren Baghdasaryan | 96bf3a6 | 2017-12-08 12:58:52 -0800 | [diff] [blame] | 1560 | /* By default disable low level vmpressure events */ |
| 1561 | level_oomadj[VMPRESS_LEVEL_LOW] = |
| 1562 | property_get_int32("ro.lmk.low", OOM_SCORE_ADJ_MAX + 1); |
| 1563 | level_oomadj[VMPRESS_LEVEL_MEDIUM] = |
| 1564 | property_get_int32("ro.lmk.medium", 800); |
| 1565 | level_oomadj[VMPRESS_LEVEL_CRITICAL] = |
| 1566 | property_get_int32("ro.lmk.critical", 0); |
Robert Benea | caeaa65 | 2017-08-11 16:03:20 -0700 | [diff] [blame] | 1567 | debug_process_killing = property_get_bool("ro.lmk.debug", false); |
Suren Baghdasaryan | ad2fd91 | 2017-12-08 13:08:41 -0800 | [diff] [blame] | 1568 | |
| 1569 | /* By default disable upgrade/downgrade logic */ |
| 1570 | enable_pressure_upgrade = |
| 1571 | property_get_bool("ro.lmk.critical_upgrade", false); |
| 1572 | upgrade_pressure = |
| 1573 | (int64_t)property_get_int32("ro.lmk.upgrade_pressure", 100); |
| 1574 | downgrade_pressure = |
| 1575 | (int64_t)property_get_int32("ro.lmk.downgrade_pressure", 100); |
Suren Baghdasaryan | 662492a | 2017-12-08 13:17:06 -0800 | [diff] [blame] | 1576 | kill_heaviest_task = |
Suren Baghdasaryan | 818b59b | 2018-04-13 11:49:54 -0700 | [diff] [blame] | 1577 | property_get_bool("ro.lmk.kill_heaviest_task", false); |
Suren Baghdasaryan | ff61afb | 2018-04-13 11:45:38 -0700 | [diff] [blame] | 1578 | low_ram_device = property_get_bool("ro.config.low_ram", false); |
Suren Baghdasaryan | caa2dc5 | 2018-01-17 17:28:01 -0800 | [diff] [blame] | 1579 | kill_timeout_ms = |
| 1580 | (unsigned long)property_get_int32("ro.lmk.kill_timeout_ms", 0); |
Suren Baghdasaryan | d273b66 | 2018-04-13 13:53:43 -0700 | [diff] [blame] | 1581 | use_minfree_levels = |
| 1582 | property_get_bool("ro.lmk.use_minfree_levels", false); |
Robert Benea | 58891d5 | 2017-07-31 17:15:20 -0700 | [diff] [blame] | 1583 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1584 | #ifdef LMKD_LOG_STATS |
Rajeev Kumar | fb25ddd | 2018-03-09 15:20:56 -0800 | [diff] [blame] | 1585 | statslog_init(&log_ctx, &enable_stats_log); |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1586 | #endif |
| 1587 | |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1588 | if (!init()) { |
| 1589 | if (!use_inkernel_interface) { |
| 1590 | /* |
| 1591 | * MCL_ONFAULT pins pages as they fault instead of loading |
| 1592 | * everything immediately all at once. (Which would be bad, |
| 1593 | * because as of this writing, we have a lot of mapped pages we |
| 1594 | * never use.) Old kernels will see MCL_ONFAULT and fail with |
| 1595 | * EINVAL; we ignore this failure. |
| 1596 | * |
| 1597 | * N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT |
| 1598 | * pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault |
| 1599 | * in pages. |
| 1600 | */ |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1601 | /* CAP_IPC_LOCK required */ |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1602 | if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && (errno != EINVAL)) { |
| 1603 | ALOGW("mlockall failed %s", strerror(errno)); |
| 1604 | } |
Daniel Colascione | 4dd5d00 | 2018-01-03 12:01:02 -0800 | [diff] [blame] | 1605 | |
Mark Salyzyn | 64d97d8 | 2018-04-09 09:50:32 -0700 | [diff] [blame] | 1606 | /* CAP_NICE required */ |
| 1607 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
| 1608 | ALOGW("set SCHED_FIFO failed %s", strerror(errno)); |
| 1609 | } |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1612 | mainloop(); |
Mark Salyzyn | 721d7c7 | 2018-03-21 12:24:58 -0700 | [diff] [blame] | 1613 | } |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1614 | |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1615 | #ifdef LMKD_LOG_STATS |
Rajeev Kumar | fb25ddd | 2018-03-09 15:20:56 -0800 | [diff] [blame] | 1616 | statslog_destroy(&log_ctx); |
Rajeev Kumar | 7045003 | 2018-01-31 17:54:56 -0800 | [diff] [blame] | 1617 | #endif |
| 1618 | |
Todd Poynor | 3948f80 | 2013-07-09 19:35:14 -0700 | [diff] [blame] | 1619 | ALOGI("exiting"); |
| 1620 | return 0; |
| 1621 | } |