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