blob: 1cfef34ff820b4e25300a951aa3a473aadfffd96 [file] [log] [blame]
Todd Poynor3948f802013-07-09 19:35:14 -07001/*
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 Beneac47f2992017-08-21 15:18:31 -070020#include <inttypes.h>
Suren Baghdasaryan4311d1e2018-03-20 16:03:29 -070021#include <pwd.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070022#include <sched.h>
Todd Poynor3948f802013-07-09 19:35:14 -070023#include <signal.h>
Suren Baghdasaryan1ffa2462018-03-20 13:53:17 -070024#include <stdbool.h>
Todd Poynor3948f802013-07-09 19:35:14 -070025#include <stdlib.h>
26#include <string.h>
Mark Salyzyne6ed68b2014-04-30 13:36:35 -070027#include <sys/cdefs.h>
Todd Poynor3948f802013-07-09 19:35:14 -070028#include <sys/epoll.h>
29#include <sys/eventfd.h>
Colin Crossb28ff912014-07-11 17:15:44 -070030#include <sys/mman.h>
Todd Poynor3948f802013-07-09 19:35:14 -070031#include <sys/socket.h>
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -080032#include <sys/sysinfo.h>
Mark Salyzyn721d7c72018-03-21 12:24:58 -070033#include <sys/types.h>
Mark Salyzyne6ed68b2014-04-30 13:36:35 -070034#include <unistd.h>
35
Robert Benea58891d52017-07-31 17:15:20 -070036#include <cutils/properties.h>
Todd Poynor3948f802013-07-09 19:35:14 -070037#include <cutils/sockets.h>
Suren Baghdasaryana92de712018-03-07 12:27:50 -080038#include <lmkd.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080039#include <log/log.h>
Mark Salyzyne6ed68b2014-04-30 13:36:35 -070040
Suren Baghdasaryane1217c02018-01-04 10:43:58 -080041/*
42 * Define LMKD_TRACE_KILLS to record lmkd kills in kernel traces
43 * to profile and correlate with OOM kills
44 */
45#ifdef LMKD_TRACE_KILLS
46
47#define ATRACE_TAG ATRACE_TAG_ALWAYS
48#include <cutils/trace.h>
49
50#define TRACE_KILL_START(pid) ATRACE_INT(__FUNCTION__, pid);
51#define TRACE_KILL_END() ATRACE_INT(__FUNCTION__, 0);
52
53#else /* LMKD_TRACE_KILLS */
54
55#define TRACE_KILL_START(pid)
56#define TRACE_KILL_END()
57
58#endif /* LMKD_TRACE_KILLS */
59
Mark Salyzyne6ed68b2014-04-30 13:36:35 -070060#ifndef __unused
61#define __unused __attribute__((__unused__))
62#endif
Todd Poynor3948f802013-07-09 19:35:14 -070063
64#define MEMCG_SYSFS_PATH "/dev/memcg/"
Robert Beneac47f2992017-08-21 15:18:31 -070065#define MEMCG_MEMORY_USAGE "/dev/memcg/memory.usage_in_bytes"
66#define MEMCG_MEMORYSW_USAGE "/dev/memcg/memory.memsw.usage_in_bytes"
Todd Poynor3948f802013-07-09 19:35:14 -070067#define ZONEINFO_PATH "/proc/zoneinfo"
Suren Baghdasaryanceffb412018-04-13 13:11:51 -070068#define MEMINFO_PATH "/proc/meminfo"
Todd Poynor3948f802013-07-09 19:35:14 -070069#define LINE_MAX 128
70
Mark Salyzyn64d97d82018-04-09 09:50:32 -070071/* gid containing AID_SYSTEM required */
Todd Poynor3948f802013-07-09 19:35:14 -070072#define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree"
73#define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj"
74
75#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
Robert Benea673e2762017-06-01 16:32:31 -070076#define EIGHT_MEGA (1 << 23)
Todd Poynor3948f802013-07-09 19:35:14 -070077
Suren Baghdasaryan4311d1e2018-03-20 16:03:29 -070078/* Defined as ProcessList.SYSTEM_ADJ in ProcessList.java */
79#define SYSTEM_ADJ (-900)
80
Todd Poynor3948f802013-07-09 19:35:14 -070081/* default to old in-kernel interface if no memory pressure events */
Mark Salyzyn721d7c72018-03-21 12:24:58 -070082static bool use_inkernel_interface = true;
Robert Benea164baeb2017-09-11 16:53:28 -070083static bool has_inkernel_module;
Todd Poynor3948f802013-07-09 19:35:14 -070084
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -080085/* memory pressure levels */
86enum vmpressure_level {
87 VMPRESS_LEVEL_LOW = 0,
88 VMPRESS_LEVEL_MEDIUM,
89 VMPRESS_LEVEL_CRITICAL,
90 VMPRESS_LEVEL_COUNT
91};
Todd Poynor3948f802013-07-09 19:35:14 -070092
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -080093static const char *level_name[] = {
94 "low",
95 "medium",
96 "critical"
97};
98
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -080099struct {
Suren Baghdasaryan45c9e0b2018-04-13 13:41:12 -0700100 int64_t min_nr_free_pages; /* recorded but not used yet */
101 int64_t max_nr_free_pages;
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800102} low_pressure_mem = { -1, -1 };
103
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800104static int level_oomadj[VMPRESS_LEVEL_COUNT];
Suren Baghdasaryan1a2589e2018-01-04 09:16:21 -0800105static int mpevfd[VMPRESS_LEVEL_COUNT] = { -1, -1, -1 };
Robert Beneac47f2992017-08-21 15:18:31 -0700106static bool debug_process_killing;
107static bool enable_pressure_upgrade;
108static int64_t upgrade_pressure;
Robert Benea6e8e7102017-09-13 15:20:30 -0700109static int64_t downgrade_pressure;
Suren Baghdasaryanff61afb2018-04-13 11:45:38 -0700110static bool low_ram_device;
Suren Baghdasaryanb93764d2017-12-08 13:17:06 -0800111static bool kill_heaviest_task;
Suren Baghdasaryan63dadcf2018-01-17 17:28:01 -0800112static unsigned long kill_timeout_ms;
Suren Baghdasaryanc09b53d2018-04-13 13:53:43 -0700113static bool use_minfree_levels;
Robert Benea58891d52017-07-31 17:15:20 -0700114
Suren Baghdasaryan06f75702018-01-26 12:51:19 -0800115/* data required to handle events */
116struct event_handler_info {
117 int data;
118 void (*handler)(int data, uint32_t events);
119};
Todd Poynor3948f802013-07-09 19:35:14 -0700120
Suren Baghdasaryan06f75702018-01-26 12:51:19 -0800121/* data required to handle socket events */
122struct sock_event_handler_info {
123 int sock;
124 struct event_handler_info handler_info;
125};
126
127/* max supported number of data connections */
128#define MAX_DATA_CONN 2
129
130/* socket event handler data */
131static struct sock_event_handler_info ctrl_sock;
132static struct sock_event_handler_info data_sock[MAX_DATA_CONN];
133
134/* vmpressure event handler data */
135static struct event_handler_info vmpressure_hinfo[VMPRESS_LEVEL_COUNT];
136
137/* 3 memory pressure levels, 1 ctrl listen socket, 2 ctrl data socket */
138#define MAX_EPOLL_EVENTS (1 + MAX_DATA_CONN + VMPRESS_LEVEL_COUNT)
Todd Poynor3948f802013-07-09 19:35:14 -0700139static int epollfd;
140static int maxevents;
141
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700142/* OOM score values used by both kernel and framework */
Todd Poynor16b60992013-09-16 19:26:47 -0700143#define OOM_SCORE_ADJ_MIN (-1000)
144#define OOM_SCORE_ADJ_MAX 1000
145
Todd Poynor3948f802013-07-09 19:35:14 -0700146static int lowmem_adj[MAX_TARGETS];
147static int lowmem_minfree[MAX_TARGETS];
148static int lowmem_targets_size;
149
Suren Baghdasaryanceffb412018-04-13 13:11:51 -0700150/* Fields to parse in /proc/zoneinfo */
151enum zoneinfo_field {
152 ZI_NR_FREE_PAGES = 0,
153 ZI_NR_FILE_PAGES,
154 ZI_NR_SHMEM,
155 ZI_NR_UNEVICTABLE,
156 ZI_WORKINGSET_REFAULT,
157 ZI_HIGH,
158 ZI_FIELD_COUNT
159};
160
161static const char* const zoneinfo_field_names[ZI_FIELD_COUNT] = {
162 "nr_free_pages",
163 "nr_file_pages",
164 "nr_shmem",
165 "nr_unevictable",
166 "workingset_refault",
167 "high",
168};
169
170union zoneinfo {
171 struct {
172 int64_t nr_free_pages;
173 int64_t nr_file_pages;
174 int64_t nr_shmem;
175 int64_t nr_unevictable;
176 int64_t workingset_refault;
177 int64_t high;
178 /* fields below are calculated rather than read from the file */
179 int64_t totalreserve_pages;
180 } field;
181 int64_t arr[ZI_FIELD_COUNT];
182};
183
184/* Fields to parse in /proc/meminfo */
185enum meminfo_field {
186 MI_NR_FREE_PAGES = 0,
187 MI_CACHED,
188 MI_SWAP_CACHED,
189 MI_BUFFERS,
190 MI_SHMEM,
191 MI_UNEVICTABLE,
192 MI_FREE_SWAP,
193 MI_DIRTY,
194 MI_FIELD_COUNT
195};
196
197static const char* const meminfo_field_names[MI_FIELD_COUNT] = {
198 "MemFree:",
199 "Cached:",
200 "SwapCached:",
201 "Buffers:",
202 "Shmem:",
203 "Unevictable:",
204 "SwapFree:",
205 "Dirty:",
206};
207
208union meminfo {
209 struct {
210 int64_t nr_free_pages;
211 int64_t cached;
212 int64_t swap_cached;
213 int64_t buffers;
214 int64_t shmem;
215 int64_t unevictable;
216 int64_t free_swap;
217 int64_t dirty;
218 /* fields below are calculated rather than read from the file */
219 int64_t nr_file_pages;
220 } field;
221 int64_t arr[MI_FIELD_COUNT];
222};
223
224enum field_match_result {
225 NO_MATCH,
226 PARSE_FAIL,
227 PARSE_SUCCESS
228};
229
Todd Poynor3948f802013-07-09 19:35:14 -0700230struct adjslot_list {
231 struct adjslot_list *next;
232 struct adjslot_list *prev;
233};
234
235struct proc {
236 struct adjslot_list asl;
237 int pid;
Colin Crossfbb78c62014-06-13 14:52:43 -0700238 uid_t uid;
Todd Poynor3948f802013-07-09 19:35:14 -0700239 int oomadj;
240 struct proc *pidhash_next;
241};
242
Suren Baghdasaryan6499e5e2018-04-13 12:43:41 -0700243struct reread_data {
244 const char* const filename;
245 int fd;
246};
247
Todd Poynor3948f802013-07-09 19:35:14 -0700248#define PIDHASH_SZ 1024
249static struct proc *pidhash[PIDHASH_SZ];
250#define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))
251
Chih-Hung Hsiehdaa13ea2016-05-19 16:02:22 -0700252#define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN)
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700253static struct adjslot_list procadjslot_list[ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1];
Todd Poynor3948f802013-07-09 19:35:14 -0700254
Todd Poynor3948f802013-07-09 19:35:14 -0700255/* PAGE_SIZE / 1024 */
256static long page_k;
257
Suren Baghdasaryan6499e5e2018-04-13 12:43:41 -0700258static bool parse_int64(const char* str, int64_t* ret) {
259 char* endptr;
260 long long val = strtoll(str, &endptr, 10);
261 if (str == endptr || val > INT64_MAX) {
262 return false;
263 }
264 *ret = (int64_t)val;
265 return true;
266}
267
Suren Baghdasaryanceffb412018-04-13 13:11:51 -0700268static enum field_match_result match_field(const char* cp, const char* ap,
269 const char* const field_names[],
270 int field_count, int64_t* field,
271 int *field_idx) {
272 int64_t val;
273 int i;
274
275 for (i = 0; i < field_count; i++) {
276 if (!strcmp(cp, field_names[i])) {
277 *field_idx = i;
278 return parse_int64(ap, field) ? PARSE_SUCCESS : PARSE_FAIL;
279 }
280 }
281 return NO_MATCH;
282}
283
Suren Baghdasaryan6499e5e2018-04-13 12:43:41 -0700284/*
285 * Read file content from the beginning up to max_len bytes or EOF
286 * whichever happens first.
287 */
Colin Crossce85d952014-07-11 17:53:27 -0700288static ssize_t read_all(int fd, char *buf, size_t max_len)
289{
290 ssize_t ret = 0;
Suren Baghdasaryan6499e5e2018-04-13 12:43:41 -0700291 off_t offset = 0;
Colin Crossce85d952014-07-11 17:53:27 -0700292
293 while (max_len > 0) {
Suren Baghdasaryan6499e5e2018-04-13 12:43:41 -0700294 ssize_t r = TEMP_FAILURE_RETRY(pread(fd, buf, max_len, offset));
Colin Crossce85d952014-07-11 17:53:27 -0700295 if (r == 0) {
296 break;
297 }
298 if (r == -1) {
299 return -1;
300 }
301 ret += r;
302 buf += r;
Suren Baghdasaryan6499e5e2018-04-13 12:43:41 -0700303 offset += r;
Colin Crossce85d952014-07-11 17:53:27 -0700304 max_len -= r;
305 }
306
307 return ret;
308}
309
Suren Baghdasaryan6499e5e2018-04-13 12:43:41 -0700310/*
311 * Read a new or already opened file from the beginning.
312 * If the file has not been opened yet data->fd should be set to -1.
313 * To be used with files which are read often and possibly during high
314 * memory pressure to minimize file opening which by itself requires kernel
315 * memory allocation and might result in a stall on memory stressed system.
316 */
317static int reread_file(struct reread_data *data, char *buf, size_t buf_size) {
318 ssize_t size;
319
320 if (data->fd == -1) {
321 data->fd = open(data->filename, O_RDONLY | O_CLOEXEC);
322 if (data->fd == -1) {
323 ALOGE("%s open: %s", data->filename, strerror(errno));
324 return -1;
325 }
326 }
327
328 size = read_all(data->fd, buf, buf_size - 1);
329 if (size < 0) {
330 ALOGE("%s read: %s", data->filename, strerror(errno));
331 close(data->fd);
332 data->fd = -1;
333 return -1;
334 }
335 ALOG_ASSERT((size_t)size < buf_size - 1, data->filename " too large");
336 buf[size] = 0;
337
338 return 0;
339}
340
Todd Poynor3948f802013-07-09 19:35:14 -0700341static struct proc *pid_lookup(int pid) {
342 struct proc *procp;
343
344 for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid;
345 procp = procp->pidhash_next)
346 ;
347
348 return procp;
349}
350
351static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new)
352{
353 struct adjslot_list *next = head->next;
354 new->prev = head;
355 new->next = next;
356 next->prev = new;
357 head->next = new;
358}
359
360static void adjslot_remove(struct adjslot_list *old)
361{
362 struct adjslot_list *prev = old->prev;
363 struct adjslot_list *next = old->next;
364 next->prev = prev;
365 prev->next = next;
366}
367
368static struct adjslot_list *adjslot_tail(struct adjslot_list *head) {
369 struct adjslot_list *asl = head->prev;
370
371 return asl == head ? NULL : asl;
372}
373
374static void proc_slot(struct proc *procp) {
375 int adjslot = ADJTOSLOT(procp->oomadj);
376
377 adjslot_insert(&procadjslot_list[adjslot], &procp->asl);
378}
379
380static void proc_unslot(struct proc *procp) {
381 adjslot_remove(&procp->asl);
382}
383
384static void proc_insert(struct proc *procp) {
385 int hval = pid_hashfn(procp->pid);
386
387 procp->pidhash_next = pidhash[hval];
388 pidhash[hval] = procp;
389 proc_slot(procp);
390}
391
392static int pid_remove(int pid) {
393 int hval = pid_hashfn(pid);
394 struct proc *procp;
395 struct proc *prevp;
396
397 for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid;
398 procp = procp->pidhash_next)
399 prevp = procp;
400
401 if (!procp)
402 return -1;
403
404 if (!prevp)
405 pidhash[hval] = procp->pidhash_next;
406 else
407 prevp->pidhash_next = procp->pidhash_next;
408
409 proc_unslot(procp);
410 free(procp);
411 return 0;
412}
413
Suren Baghdasaryan1ffa2462018-03-20 13:53:17 -0700414/*
415 * Write a string to a file.
416 * Returns false if the file does not exist.
417 */
418static bool writefilestring(const char *path, const char *s,
419 bool err_if_missing) {
Nick Kralevichc68c8862015-12-18 20:52:37 -0800420 int fd = open(path, O_WRONLY | O_CLOEXEC);
Suren Baghdasaryan1ffa2462018-03-20 13:53:17 -0700421 ssize_t len = strlen(s);
422 ssize_t ret;
Todd Poynor3948f802013-07-09 19:35:14 -0700423
424 if (fd < 0) {
Suren Baghdasaryan1ffa2462018-03-20 13:53:17 -0700425 if (err_if_missing) {
426 ALOGE("Error opening %s; errno=%d", path, errno);
427 }
428 return false;
Todd Poynor3948f802013-07-09 19:35:14 -0700429 }
430
Suren Baghdasaryan1ffa2462018-03-20 13:53:17 -0700431 ret = TEMP_FAILURE_RETRY(write(fd, s, len));
Todd Poynor3948f802013-07-09 19:35:14 -0700432 if (ret < 0) {
433 ALOGE("Error writing %s; errno=%d", path, errno);
434 } else if (ret < len) {
Suren Baghdasaryan1ffa2462018-03-20 13:53:17 -0700435 ALOGE("Short write on %s; length=%zd", path, ret);
Todd Poynor3948f802013-07-09 19:35:14 -0700436 }
437
438 close(fd);
Suren Baghdasaryan1ffa2462018-03-20 13:53:17 -0700439 return true;
Todd Poynor3948f802013-07-09 19:35:14 -0700440}
441
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800442static void cmd_procprio(LMKD_CTRL_PACKET packet) {
Todd Poynor3948f802013-07-09 19:35:14 -0700443 struct proc *procp;
444 char path[80];
445 char val[20];
Robert Benea673e2762017-06-01 16:32:31 -0700446 int soft_limit_mult;
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800447 struct lmk_procprio params;
Suren Baghdasaryan4311d1e2018-03-20 16:03:29 -0700448 bool is_system_server;
449 struct passwd *pwdrec;
Todd Poynor3948f802013-07-09 19:35:14 -0700450
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800451 lmkd_pack_get_procprio(packet, &params);
452
453 if (params.oomadj < OOM_SCORE_ADJ_MIN ||
454 params.oomadj > OOM_SCORE_ADJ_MAX) {
455 ALOGE("Invalid PROCPRIO oomadj argument %d", params.oomadj);
Todd Poynor3948f802013-07-09 19:35:14 -0700456 return;
457 }
458
Mark Salyzyn64d97d82018-04-09 09:50:32 -0700459 /* gid containing AID_READPROC required */
460 /* CAP_SYS_RESOURCE required */
461 /* CAP_DAC_OVERRIDE required */
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800462 snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", params.pid);
463 snprintf(val, sizeof(val), "%d", params.oomadj);
Suren Baghdasaryan1ffa2462018-03-20 13:53:17 -0700464 if (!writefilestring(path, val, false)) {
465 ALOGW("Failed to open %s; errno=%d: process %d might have been killed",
466 path, errno, params.pid);
467 /* If this file does not exist the process is dead. */
468 return;
469 }
Todd Poynor3948f802013-07-09 19:35:14 -0700470
Mark Salyzyn721d7c72018-03-21 12:24:58 -0700471 if (use_inkernel_interface) {
Todd Poynor3948f802013-07-09 19:35:14 -0700472 return;
Mark Salyzyn721d7c72018-03-21 12:24:58 -0700473 }
Todd Poynor3948f802013-07-09 19:35:14 -0700474
Suren Baghdasaryanfe26dfb2018-05-18 14:42:00 -0700475 if (low_ram_device) {
476 if (params.oomadj >= 900) {
477 soft_limit_mult = 0;
478 } else if (params.oomadj >= 800) {
479 soft_limit_mult = 0;
480 } else if (params.oomadj >= 700) {
481 soft_limit_mult = 0;
482 } else if (params.oomadj >= 600) {
483 // Launcher should be perceptible, don't kill it.
484 params.oomadj = 200;
485 soft_limit_mult = 1;
486 } else if (params.oomadj >= 500) {
487 soft_limit_mult = 0;
488 } else if (params.oomadj >= 400) {
489 soft_limit_mult = 0;
490 } else if (params.oomadj >= 300) {
491 soft_limit_mult = 1;
492 } else if (params.oomadj >= 200) {
493 soft_limit_mult = 2;
494 } else if (params.oomadj >= 100) {
495 soft_limit_mult = 10;
496 } else if (params.oomadj >= 0) {
497 soft_limit_mult = 20;
498 } else {
499 // Persistent processes will have a large
500 // soft limit 512MB.
501 soft_limit_mult = 64;
502 }
503
504 snprintf(path, sizeof(path), MEMCG_SYSFS_PATH
505 "apps/uid_%d/pid_%d/memory.soft_limit_in_bytes",
506 params.uid, params.pid);
507 snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA);
508
509 /*
510 * system_server process has no memcg under /dev/memcg/apps but should be
511 * registered with lmkd. This is the best way so far to identify it.
512 */
513 is_system_server = (params.oomadj == SYSTEM_ADJ &&
514 (pwdrec = getpwnam("system")) != NULL &&
515 params.uid == pwdrec->pw_uid);
516 writefilestring(path, val, !is_system_server);
Robert Benea673e2762017-06-01 16:32:31 -0700517 }
518
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800519 procp = pid_lookup(params.pid);
Todd Poynor3948f802013-07-09 19:35:14 -0700520 if (!procp) {
521 procp = malloc(sizeof(struct proc));
522 if (!procp) {
523 // Oh, the irony. May need to rebuild our state.
524 return;
525 }
526
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800527 procp->pid = params.pid;
528 procp->uid = params.uid;
529 procp->oomadj = params.oomadj;
Todd Poynor3948f802013-07-09 19:35:14 -0700530 proc_insert(procp);
531 } else {
532 proc_unslot(procp);
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800533 procp->oomadj = params.oomadj;
Todd Poynor3948f802013-07-09 19:35:14 -0700534 proc_slot(procp);
535 }
536}
537
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800538static void cmd_procremove(LMKD_CTRL_PACKET packet) {
539 struct lmk_procremove params;
540
Mark Salyzyn721d7c72018-03-21 12:24:58 -0700541 if (use_inkernel_interface) {
Todd Poynor3948f802013-07-09 19:35:14 -0700542 return;
Mark Salyzyn721d7c72018-03-21 12:24:58 -0700543 }
Todd Poynor3948f802013-07-09 19:35:14 -0700544
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800545 lmkd_pack_get_procremove(packet, &params);
546 pid_remove(params.pid);
Todd Poynor3948f802013-07-09 19:35:14 -0700547}
548
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800549static void cmd_target(int ntargets, LMKD_CTRL_PACKET packet) {
Todd Poynor3948f802013-07-09 19:35:14 -0700550 int i;
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800551 struct lmk_target target;
Todd Poynor3948f802013-07-09 19:35:14 -0700552
553 if (ntargets > (int)ARRAY_SIZE(lowmem_adj))
554 return;
555
556 for (i = 0; i < ntargets; i++) {
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800557 lmkd_pack_get_target(packet, i, &target);
558 lowmem_minfree[i] = target.minfree;
559 lowmem_adj[i] = target.oom_adj_score;
Todd Poynor3948f802013-07-09 19:35:14 -0700560 }
561
562 lowmem_targets_size = ntargets;
563
Robert Benea164baeb2017-09-11 16:53:28 -0700564 if (has_inkernel_module) {
Todd Poynor3948f802013-07-09 19:35:14 -0700565 char minfreestr[128];
566 char killpriostr[128];
567
568 minfreestr[0] = '\0';
569 killpriostr[0] = '\0';
570
571 for (i = 0; i < lowmem_targets_size; i++) {
572 char val[40];
573
574 if (i) {
575 strlcat(minfreestr, ",", sizeof(minfreestr));
576 strlcat(killpriostr, ",", sizeof(killpriostr));
577 }
578
Robert Benea164baeb2017-09-11 16:53:28 -0700579 snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_minfree[i] : 0);
Todd Poynor3948f802013-07-09 19:35:14 -0700580 strlcat(minfreestr, val, sizeof(minfreestr));
Robert Benea164baeb2017-09-11 16:53:28 -0700581 snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_adj[i] : 0);
Todd Poynor3948f802013-07-09 19:35:14 -0700582 strlcat(killpriostr, val, sizeof(killpriostr));
583 }
584
Suren Baghdasaryan1ffa2462018-03-20 13:53:17 -0700585 writefilestring(INKERNEL_MINFREE_PATH, minfreestr, true);
586 writefilestring(INKERNEL_ADJ_PATH, killpriostr, true);
Todd Poynor3948f802013-07-09 19:35:14 -0700587 }
588}
589
Suren Baghdasaryan06f75702018-01-26 12:51:19 -0800590static void ctrl_data_close(int dsock_idx) {
591 struct epoll_event epev;
592
593 ALOGI("closing lmkd data connection");
594 if (epoll_ctl(epollfd, EPOLL_CTL_DEL, data_sock[dsock_idx].sock, &epev) == -1) {
595 // Log a warning and keep going
596 ALOGW("epoll_ctl for data connection socket failed; errno=%d", errno);
597 }
Todd Poynor3948f802013-07-09 19:35:14 -0700598 maxevents--;
Suren Baghdasaryan06f75702018-01-26 12:51:19 -0800599
600 close(data_sock[dsock_idx].sock);
601 data_sock[dsock_idx].sock = -1;
Todd Poynor3948f802013-07-09 19:35:14 -0700602}
603
Suren Baghdasaryan06f75702018-01-26 12:51:19 -0800604static int ctrl_data_read(int dsock_idx, char *buf, size_t bufsz) {
Todd Poynor3948f802013-07-09 19:35:14 -0700605 int ret = 0;
606
Suren Baghdasaryan6499e5e2018-04-13 12:43:41 -0700607 ret = TEMP_FAILURE_RETRY(read(data_sock[dsock_idx].sock, buf, bufsz));
Todd Poynor3948f802013-07-09 19:35:14 -0700608
609 if (ret == -1) {
610 ALOGE("control data socket read failed; errno=%d", errno);
611 } else if (ret == 0) {
612 ALOGE("Got EOF on control data socket");
613 ret = -1;
614 }
615
616 return ret;
617}
618
Suren Baghdasaryan06f75702018-01-26 12:51:19 -0800619static void ctrl_command_handler(int dsock_idx) {
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800620 LMKD_CTRL_PACKET packet;
Todd Poynor3948f802013-07-09 19:35:14 -0700621 int len;
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800622 enum lmk_cmd cmd;
Todd Poynor3948f802013-07-09 19:35:14 -0700623 int nargs;
624 int targets;
625
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800626 len = ctrl_data_read(dsock_idx, (char *)packet, CTRL_PACKET_MAX_SIZE);
Todd Poynor3948f802013-07-09 19:35:14 -0700627 if (len <= 0)
628 return;
629
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800630 if (len < (int)sizeof(int)) {
631 ALOGE("Wrong control socket read length len=%d", len);
632 return;
633 }
634
635 cmd = lmkd_pack_get_cmd(packet);
Todd Poynor3948f802013-07-09 19:35:14 -0700636 nargs = len / sizeof(int) - 1;
637 if (nargs < 0)
638 goto wronglen;
639
Todd Poynor3948f802013-07-09 19:35:14 -0700640 switch(cmd) {
641 case LMK_TARGET:
642 targets = nargs / 2;
643 if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj))
644 goto wronglen;
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800645 cmd_target(targets, packet);
Todd Poynor3948f802013-07-09 19:35:14 -0700646 break;
647 case LMK_PROCPRIO:
Colin Crossfbb78c62014-06-13 14:52:43 -0700648 if (nargs != 3)
Todd Poynor3948f802013-07-09 19:35:14 -0700649 goto wronglen;
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800650 cmd_procprio(packet);
Todd Poynor3948f802013-07-09 19:35:14 -0700651 break;
652 case LMK_PROCREMOVE:
653 if (nargs != 1)
654 goto wronglen;
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800655 cmd_procremove(packet);
Todd Poynor3948f802013-07-09 19:35:14 -0700656 break;
657 default:
658 ALOGE("Received unknown command code %d", cmd);
659 return;
660 }
661
662 return;
663
664wronglen:
665 ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len);
666}
667
Suren Baghdasaryan06f75702018-01-26 12:51:19 -0800668static void ctrl_data_handler(int data, uint32_t events) {
669 if (events & EPOLLIN) {
670 ctrl_command_handler(data);
Todd Poynor3948f802013-07-09 19:35:14 -0700671 }
672}
673
Suren Baghdasaryan06f75702018-01-26 12:51:19 -0800674static int get_free_dsock() {
675 for (int i = 0; i < MAX_DATA_CONN; i++) {
676 if (data_sock[i].sock < 0) {
677 return i;
678 }
679 }
680 return -1;
681}
Todd Poynor3948f802013-07-09 19:35:14 -0700682
Suren Baghdasaryan06f75702018-01-26 12:51:19 -0800683static void ctrl_connect_handler(int data __unused, uint32_t events __unused) {
684 struct epoll_event epev;
685 int free_dscock_idx = get_free_dsock();
686
687 if (free_dscock_idx < 0) {
688 /*
689 * Number of data connections exceeded max supported. This should not
690 * happen but if it does we drop all existing connections and accept
691 * the new one. This prevents inactive connections from monopolizing
692 * data socket and if we drop ActivityManager connection it will
693 * immediately reconnect.
694 */
695 for (int i = 0; i < MAX_DATA_CONN; i++) {
696 ctrl_data_close(i);
697 }
698 free_dscock_idx = 0;
Todd Poynor3948f802013-07-09 19:35:14 -0700699 }
700
Suren Baghdasaryan06f75702018-01-26 12:51:19 -0800701 data_sock[free_dscock_idx].sock = accept(ctrl_sock.sock, NULL, NULL);
702 if (data_sock[free_dscock_idx].sock < 0) {
Todd Poynor3948f802013-07-09 19:35:14 -0700703 ALOGE("lmkd control socket accept failed; errno=%d", errno);
704 return;
705 }
706
Suren Baghdasaryan06f75702018-01-26 12:51:19 -0800707 ALOGI("lmkd data connection established");
708 /* use data to store data connection idx */
709 data_sock[free_dscock_idx].handler_info.data = free_dscock_idx;
710 data_sock[free_dscock_idx].handler_info.handler = ctrl_data_handler;
Todd Poynor3948f802013-07-09 19:35:14 -0700711 epev.events = EPOLLIN;
Suren Baghdasaryan06f75702018-01-26 12:51:19 -0800712 epev.data.ptr = (void *)&(data_sock[free_dscock_idx].handler_info);
713 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, data_sock[free_dscock_idx].sock, &epev) == -1) {
Todd Poynor3948f802013-07-09 19:35:14 -0700714 ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno);
Suren Baghdasaryan06f75702018-01-26 12:51:19 -0800715 ctrl_data_close(free_dscock_idx);
Todd Poynor3948f802013-07-09 19:35:14 -0700716 return;
717 }
Suren Baghdasaryan06f75702018-01-26 12:51:19 -0800718 maxevents++;
Todd Poynor3948f802013-07-09 19:35:14 -0700719}
720
Suren Baghdasaryanceffb412018-04-13 13:11:51 -0700721/* /prop/zoneinfo parsing routines */
722static int64_t zoneinfo_parse_protection(char *cp) {
723 int64_t max = 0;
724 long long zoneval;
Colin Crossce85d952014-07-11 17:53:27 -0700725 char *save_ptr;
Todd Poynor3948f802013-07-09 19:35:14 -0700726
Suren Baghdasaryanceffb412018-04-13 13:11:51 -0700727 for (cp = strtok_r(cp, "(), ", &save_ptr); cp;
728 cp = strtok_r(NULL, "), ", &save_ptr)) {
729 zoneval = strtoll(cp, &cp, 0);
730 if (zoneval > max) {
731 max = (zoneval > INT64_MAX) ? INT64_MAX : zoneval;
732 }
Colin Crossce85d952014-07-11 17:53:27 -0700733 }
Todd Poynor3948f802013-07-09 19:35:14 -0700734
735 return max;
736}
737
Suren Baghdasaryanceffb412018-04-13 13:11:51 -0700738static bool zoneinfo_parse_line(char *line, union zoneinfo *zi) {
Todd Poynor3948f802013-07-09 19:35:14 -0700739 char *cp = line;
740 char *ap;
Colin Crossce85d952014-07-11 17:53:27 -0700741 char *save_ptr;
Suren Baghdasaryanceffb412018-04-13 13:11:51 -0700742 int64_t val;
743 int field_idx;
Todd Poynor3948f802013-07-09 19:35:14 -0700744
Colin Crossce85d952014-07-11 17:53:27 -0700745 cp = strtok_r(line, " ", &save_ptr);
Suren Baghdasaryanceffb412018-04-13 13:11:51 -0700746 if (!cp) {
747 return true;
748 }
Todd Poynor3948f802013-07-09 19:35:14 -0700749
Suren Baghdasaryanceffb412018-04-13 13:11:51 -0700750 if (!strcmp(cp, "protection:")) {
751 ap = strtok_r(NULL, ")", &save_ptr);
752 } else {
753 ap = strtok_r(NULL, " ", &save_ptr);
754 }
Todd Poynor3948f802013-07-09 19:35:14 -0700755
Suren Baghdasaryanceffb412018-04-13 13:11:51 -0700756 if (!ap) {
757 return true;
758 }
759
760 switch (match_field(cp, ap, zoneinfo_field_names,
761 ZI_FIELD_COUNT, &val, &field_idx)) {
762 case (PARSE_SUCCESS):
763 zi->arr[field_idx] += val;
764 break;
765 case (NO_MATCH):
766 if (!strcmp(cp, "protection:")) {
767 zi->field.totalreserve_pages +=
768 zoneinfo_parse_protection(ap);
769 }
770 break;
771 case (PARSE_FAIL):
772 default:
773 return false;
774 }
775 return true;
Todd Poynor3948f802013-07-09 19:35:14 -0700776}
777
Suren Baghdasaryanceffb412018-04-13 13:11:51 -0700778static int zoneinfo_parse(union zoneinfo *zi) {
779 static struct reread_data file_data = {
780 .filename = ZONEINFO_PATH,
781 .fd = -1,
782 };
Colin Crossce85d952014-07-11 17:53:27 -0700783 char buf[PAGE_SIZE];
784 char *save_ptr;
785 char *line;
Todd Poynor3948f802013-07-09 19:35:14 -0700786
Suren Baghdasaryanceffb412018-04-13 13:11:51 -0700787 memset(zi, 0, sizeof(union zoneinfo));
Colin Crossce85d952014-07-11 17:53:27 -0700788
Suren Baghdasaryanceffb412018-04-13 13:11:51 -0700789 if (reread_file(&file_data, buf, sizeof(buf)) < 0) {
Todd Poynor3948f802013-07-09 19:35:14 -0700790 return -1;
791 }
792
Suren Baghdasaryanceffb412018-04-13 13:11:51 -0700793 for (line = strtok_r(buf, "\n", &save_ptr); line;
794 line = strtok_r(NULL, "\n", &save_ptr)) {
795 if (!zoneinfo_parse_line(line, zi)) {
796 ALOGE("%s parse error", file_data.filename);
797 return -1;
798 }
799 }
800 zi->field.totalreserve_pages += zi->field.high;
801
802 return 0;
803}
804
805/* /prop/meminfo parsing routines */
806static bool meminfo_parse_line(char *line, union meminfo *mi) {
807 char *cp = line;
808 char *ap;
809 char *save_ptr;
810 int64_t val;
811 int field_idx;
812 enum field_match_result match_res;
813
814 cp = strtok_r(line, " ", &save_ptr);
815 if (!cp) {
816 return false;
817 }
818
819 ap = strtok_r(NULL, " ", &save_ptr);
820 if (!ap) {
821 return false;
822 }
823
824 match_res = match_field(cp, ap, meminfo_field_names, MI_FIELD_COUNT,
825 &val, &field_idx);
826 if (match_res == PARSE_SUCCESS) {
827 mi->arr[field_idx] = val / page_k;
828 }
829 return (match_res != PARSE_FAIL);
830}
831
832static int meminfo_parse(union meminfo *mi) {
833 static struct reread_data file_data = {
834 .filename = MEMINFO_PATH,
835 .fd = -1,
836 };
837 char buf[PAGE_SIZE];
838 char *save_ptr;
839 char *line;
840
841 memset(mi, 0, sizeof(union meminfo));
842
843 if (reread_file(&file_data, buf, sizeof(buf)) < 0) {
Colin Crossce85d952014-07-11 17:53:27 -0700844 return -1;
845 }
Colin Crossce85d952014-07-11 17:53:27 -0700846
Suren Baghdasaryanceffb412018-04-13 13:11:51 -0700847 for (line = strtok_r(buf, "\n", &save_ptr); line;
848 line = strtok_r(NULL, "\n", &save_ptr)) {
849 if (!meminfo_parse_line(line, mi)) {
850 ALOGE("%s parse error", file_data.filename);
851 return -1;
852 }
853 }
854 mi->field.nr_file_pages = mi->field.cached + mi->field.swap_cached +
855 mi->field.buffers;
Todd Poynor3948f802013-07-09 19:35:14 -0700856
Todd Poynor3948f802013-07-09 19:35:14 -0700857 return 0;
858}
859
860static int proc_get_size(int pid) {
861 char path[PATH_MAX];
862 char line[LINE_MAX];
Colin Crossce85d952014-07-11 17:53:27 -0700863 int fd;
Todd Poynor3948f802013-07-09 19:35:14 -0700864 int rss = 0;
865 int total;
Colin Crossce85d952014-07-11 17:53:27 -0700866 ssize_t ret;
Todd Poynor3948f802013-07-09 19:35:14 -0700867
Mark Salyzyn64d97d82018-04-09 09:50:32 -0700868 /* gid containing AID_READPROC required */
Todd Poynor3948f802013-07-09 19:35:14 -0700869 snprintf(path, PATH_MAX, "/proc/%d/statm", pid);
Nick Kralevichc68c8862015-12-18 20:52:37 -0800870 fd = open(path, O_RDONLY | O_CLOEXEC);
Colin Crossce85d952014-07-11 17:53:27 -0700871 if (fd == -1)
Todd Poynor3948f802013-07-09 19:35:14 -0700872 return -1;
Colin Crossce85d952014-07-11 17:53:27 -0700873
874 ret = read_all(fd, line, sizeof(line) - 1);
875 if (ret < 0) {
876 close(fd);
Todd Poynor3948f802013-07-09 19:35:14 -0700877 return -1;
878 }
879
880 sscanf(line, "%d %d ", &total, &rss);
Colin Crossce85d952014-07-11 17:53:27 -0700881 close(fd);
Todd Poynor3948f802013-07-09 19:35:14 -0700882 return rss;
883}
884
885static char *proc_get_name(int pid) {
886 char path[PATH_MAX];
887 static char line[LINE_MAX];
Colin Crossce85d952014-07-11 17:53:27 -0700888 int fd;
Todd Poynor3948f802013-07-09 19:35:14 -0700889 char *cp;
Colin Crossce85d952014-07-11 17:53:27 -0700890 ssize_t ret;
Todd Poynor3948f802013-07-09 19:35:14 -0700891
Mark Salyzyn64d97d82018-04-09 09:50:32 -0700892 /* gid containing AID_READPROC required */
Todd Poynor3948f802013-07-09 19:35:14 -0700893 snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid);
Nick Kralevichc68c8862015-12-18 20:52:37 -0800894 fd = open(path, O_RDONLY | O_CLOEXEC);
Colin Crossce85d952014-07-11 17:53:27 -0700895 if (fd == -1)
Todd Poynor3948f802013-07-09 19:35:14 -0700896 return NULL;
Colin Crossce85d952014-07-11 17:53:27 -0700897 ret = read_all(fd, line, sizeof(line) - 1);
898 close(fd);
899 if (ret < 0) {
Todd Poynor3948f802013-07-09 19:35:14 -0700900 return NULL;
901 }
902
903 cp = strchr(line, ' ');
904 if (cp)
905 *cp = '\0';
906
907 return line;
908}
909
910static struct proc *proc_adj_lru(int oomadj) {
911 return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]);
912}
913
Suren Baghdasaryanb93764d2017-12-08 13:17:06 -0800914static struct proc *proc_get_heaviest(int oomadj) {
915 struct adjslot_list *head = &procadjslot_list[ADJTOSLOT(oomadj)];
916 struct adjslot_list *curr = head->next;
917 struct proc *maxprocp = NULL;
918 int maxsize = 0;
919 while (curr != head) {
920 int pid = ((struct proc *)curr)->pid;
921 int tasksize = proc_get_size(pid);
922 if (tasksize <= 0) {
923 struct adjslot_list *next = curr->next;
924 pid_remove(pid);
925 curr = next;
926 } else {
927 if (tasksize > maxsize) {
928 maxsize = tasksize;
929 maxprocp = (struct proc *)curr;
930 }
931 curr = curr->next;
932 }
933 }
934 return maxprocp;
935}
936
Colin Cross16b09462014-07-14 12:39:56 -0700937/* Kill one process specified by procp. Returns the size of the process killed */
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800938static int kill_one_process(struct proc* procp, int min_score_adj,
939 enum vmpressure_level level) {
Colin Cross16b09462014-07-14 12:39:56 -0700940 int pid = procp->pid;
941 uid_t uid = procp->uid;
942 char *taskname;
943 int tasksize;
944 int r;
945
946 taskname = proc_get_name(pid);
947 if (!taskname) {
948 pid_remove(pid);
949 return -1;
950 }
951
952 tasksize = proc_get_size(pid);
953 if (tasksize <= 0) {
954 pid_remove(pid);
955 return -1;
956 }
957
Suren Baghdasaryane1217c02018-01-04 10:43:58 -0800958 TRACE_KILL_START(pid);
959
Mark Salyzyn64d97d82018-04-09 09:50:32 -0700960 /* CAP_KILL required */
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800961 r = kill(pid, SIGKILL);
Robert Beneacaeaa652017-08-11 16:03:20 -0700962 ALOGI(
963 "Killing '%s' (%d), uid %d, adj %d\n"
Suren Baghdasaryand07a94f2018-03-20 16:25:54 -0700964 " to free %ldkB because system is under %s memory pressure (min_oom_adj=%d)\n",
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -0800965 taskname, pid, uid, procp->oomadj, tasksize * page_k,
966 level_name[level], min_score_adj);
Colin Cross16b09462014-07-14 12:39:56 -0700967 pid_remove(pid);
968
Suren Baghdasaryane1217c02018-01-04 10:43:58 -0800969 TRACE_KILL_END();
970
Colin Cross16b09462014-07-14 12:39:56 -0700971 if (r) {
Mark Salyzyn919f5382018-02-04 15:27:23 -0800972 ALOGE("kill(%d): errno=%d", pid, errno);
Colin Cross16b09462014-07-14 12:39:56 -0700973 return -1;
Colin Cross16b09462014-07-14 12:39:56 -0700974 }
Mark Salyzyn919f5382018-02-04 15:27:23 -0800975
976 return tasksize;
Colin Cross16b09462014-07-14 12:39:56 -0700977}
978
979/*
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800980 * Find processes to kill to free required number of pages.
981 * If pages_to_free is set to 0 only one process will be killed.
982 * Returns the size of the killed processes.
Colin Cross16b09462014-07-14 12:39:56 -0700983 */
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800984static int find_and_kill_processes(enum vmpressure_level level,
Suren Baghdasaryanc09b53d2018-04-13 13:53:43 -0700985 int min_score_adj, int pages_to_free) {
Colin Cross16b09462014-07-14 12:39:56 -0700986 int i;
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800987 int killed_size;
988 int pages_freed = 0;
Colin Cross16b09462014-07-14 12:39:56 -0700989
Chong Zhang0a4acdf2015-10-14 16:19:53 -0700990 for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) {
Colin Cross16b09462014-07-14 12:39:56 -0700991 struct proc *procp;
992
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800993 while (true) {
Suren Baghdasaryan818b59b2018-04-13 11:49:54 -0700994 procp = kill_heaviest_task ?
995 proc_get_heaviest(i) : proc_adj_lru(i);
Colin Cross16b09462014-07-14 12:39:56 -0700996
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -0800997 if (!procp)
998 break;
999
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001000 killed_size = kill_one_process(procp, min_score_adj, level);
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001001 if (killed_size >= 0) {
1002 pages_freed += killed_size;
1003 if (pages_freed >= pages_to_free) {
1004 return pages_freed;
1005 }
Colin Cross16b09462014-07-14 12:39:56 -07001006 }
1007 }
1008 }
1009
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001010 return pages_freed;
Colin Cross16b09462014-07-14 12:39:56 -07001011}
1012
Suren Baghdasaryan6499e5e2018-04-13 12:43:41 -07001013static int64_t get_memory_usage(struct reread_data *file_data) {
Robert Beneac47f2992017-08-21 15:18:31 -07001014 int ret;
1015 int64_t mem_usage;
1016 char buf[32];
Suren Baghdasaryan6499e5e2018-04-13 12:43:41 -07001017
1018 if (reread_file(file_data, buf, sizeof(buf)) < 0) {
Robert Beneac47f2992017-08-21 15:18:31 -07001019 return -1;
1020 }
1021
Suren Baghdasaryan6499e5e2018-04-13 12:43:41 -07001022 if (!parse_int64(buf, &mem_usage)) {
1023 ALOGE("%s parse error", file_data->filename);
Robert Beneac47f2992017-08-21 15:18:31 -07001024 return -1;
1025 }
Robert Beneac47f2992017-08-21 15:18:31 -07001026 if (mem_usage == 0) {
1027 ALOGE("No memory!");
1028 return -1;
1029 }
1030 return mem_usage;
1031}
1032
Suren Baghdasaryan45c9e0b2018-04-13 13:41:12 -07001033void record_low_pressure_levels(union meminfo *mi) {
1034 if (low_pressure_mem.min_nr_free_pages == -1 ||
1035 low_pressure_mem.min_nr_free_pages > mi->field.nr_free_pages) {
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001036 if (debug_process_killing) {
Suren Baghdasaryan45c9e0b2018-04-13 13:41:12 -07001037 ALOGI("Low pressure min memory update from %" PRId64 " to %" PRId64,
1038 low_pressure_mem.min_nr_free_pages, mi->field.nr_free_pages);
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001039 }
Suren Baghdasaryan45c9e0b2018-04-13 13:41:12 -07001040 low_pressure_mem.min_nr_free_pages = mi->field.nr_free_pages;
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001041 }
1042 /*
1043 * Free memory at low vmpressure events occasionally gets spikes,
1044 * possibly a stale low vmpressure event with memory already
1045 * freed up (no memory pressure should have been reported).
Suren Baghdasaryan45c9e0b2018-04-13 13:41:12 -07001046 * Ignore large jumps in max_nr_free_pages that would mess up our stats.
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001047 */
Suren Baghdasaryan45c9e0b2018-04-13 13:41:12 -07001048 if (low_pressure_mem.max_nr_free_pages == -1 ||
1049 (low_pressure_mem.max_nr_free_pages < mi->field.nr_free_pages &&
1050 mi->field.nr_free_pages - low_pressure_mem.max_nr_free_pages <
1051 low_pressure_mem.max_nr_free_pages * 0.1)) {
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001052 if (debug_process_killing) {
Suren Baghdasaryan45c9e0b2018-04-13 13:41:12 -07001053 ALOGI("Low pressure max memory update from %" PRId64 " to %" PRId64,
1054 low_pressure_mem.max_nr_free_pages, mi->field.nr_free_pages);
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001055 }
Suren Baghdasaryan45c9e0b2018-04-13 13:41:12 -07001056 low_pressure_mem.max_nr_free_pages = mi->field.nr_free_pages;
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001057 }
1058}
1059
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001060enum vmpressure_level upgrade_level(enum vmpressure_level level) {
1061 return (enum vmpressure_level)((level < VMPRESS_LEVEL_CRITICAL) ?
1062 level + 1 : level);
1063}
1064
1065enum vmpressure_level downgrade_level(enum vmpressure_level level) {
1066 return (enum vmpressure_level)((level > VMPRESS_LEVEL_LOW) ?
1067 level - 1 : level);
1068}
1069
Suren Baghdasaryan63dadcf2018-01-17 17:28:01 -08001070static inline unsigned long get_time_diff_ms(struct timeval *from,
1071 struct timeval *to) {
1072 return (to->tv_sec - from->tv_sec) * 1000 +
1073 (to->tv_usec - from->tv_usec) / 1000;
1074}
1075
Suren Baghdasaryan06f75702018-01-26 12:51:19 -08001076static void mp_event_common(int data, uint32_t events __unused) {
Todd Poynor3948f802013-07-09 19:35:14 -07001077 int ret;
1078 unsigned long long evcount;
Robert Beneac47f2992017-08-21 15:18:31 -07001079 int64_t mem_usage, memsw_usage;
Robert Benea6e8e7102017-09-13 15:20:30 -07001080 int64_t mem_pressure;
Suren Baghdasaryan1a2589e2018-01-04 09:16:21 -08001081 enum vmpressure_level lvl;
Suren Baghdasaryan45c9e0b2018-04-13 13:41:12 -07001082 union meminfo mi;
Suren Baghdasaryanc09b53d2018-04-13 13:53:43 -07001083 union zoneinfo zi;
Suren Baghdasaryan63dadcf2018-01-17 17:28:01 -08001084 static struct timeval last_report_tm;
1085 static unsigned long skip_count = 0;
Suren Baghdasaryan06f75702018-01-26 12:51:19 -08001086 enum vmpressure_level level = (enum vmpressure_level)data;
Suren Baghdasaryanc09b53d2018-04-13 13:53:43 -07001087 long other_free = 0, other_file = 0;
1088 int min_score_adj;
1089 int pages_to_free = 0;
1090 int minfree = 0;
Suren Baghdasaryan6499e5e2018-04-13 12:43:41 -07001091 static struct reread_data mem_usage_file_data = {
1092 .filename = MEMCG_MEMORY_USAGE,
1093 .fd = -1,
1094 };
1095 static struct reread_data memsw_usage_file_data = {
1096 .filename = MEMCG_MEMORYSW_USAGE,
1097 .fd = -1,
1098 };
Todd Poynor3948f802013-07-09 19:35:14 -07001099
Suren Baghdasaryan1a2589e2018-01-04 09:16:21 -08001100 /*
1101 * Check all event counters from low to critical
1102 * and upgrade to the highest priority one. By reading
1103 * eventfd we also reset the event counters.
1104 */
1105 for (lvl = VMPRESS_LEVEL_LOW; lvl < VMPRESS_LEVEL_COUNT; lvl++) {
1106 if (mpevfd[lvl] != -1 &&
Suren Baghdasaryan6499e5e2018-04-13 12:43:41 -07001107 TEMP_FAILURE_RETRY(read(mpevfd[lvl],
1108 &evcount, sizeof(evcount))) > 0 &&
Suren Baghdasaryan1a2589e2018-01-04 09:16:21 -08001109 evcount > 0 && lvl > level) {
1110 level = lvl;
1111 }
1112 }
Todd Poynor3948f802013-07-09 19:35:14 -07001113
Suren Baghdasaryan63dadcf2018-01-17 17:28:01 -08001114 if (kill_timeout_ms) {
1115 struct timeval curr_tm;
1116 gettimeofday(&curr_tm, NULL);
1117 if (get_time_diff_ms(&last_report_tm, &curr_tm) < kill_timeout_ms) {
1118 skip_count++;
1119 return;
1120 }
1121 }
1122
1123 if (skip_count > 0) {
1124 if (debug_process_killing) {
1125 ALOGI("%lu memory pressure events were skipped after a kill!",
1126 skip_count);
1127 }
1128 skip_count = 0;
1129 }
1130
Suren Baghdasaryanc09b53d2018-04-13 13:53:43 -07001131 if (meminfo_parse(&mi) < 0 || zoneinfo_parse(&zi) < 0) {
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001132 ALOGE("Failed to get free memory!");
1133 return;
1134 }
1135
Suren Baghdasaryanc09b53d2018-04-13 13:53:43 -07001136 if (use_minfree_levels) {
1137 int i;
1138
1139 other_free = mi.field.nr_free_pages - zi.field.totalreserve_pages;
1140 if (mi.field.nr_file_pages > (mi.field.shmem + mi.field.unevictable + mi.field.swap_cached)) {
1141 other_file = (mi.field.nr_file_pages - mi.field.shmem -
1142 mi.field.unevictable - mi.field.swap_cached);
1143 } else {
1144 other_file = 0;
1145 }
1146
1147 min_score_adj = OOM_SCORE_ADJ_MAX + 1;
1148 for (i = 0; i < lowmem_targets_size; i++) {
1149 minfree = lowmem_minfree[i];
1150 if (other_free < minfree && other_file < minfree) {
1151 min_score_adj = lowmem_adj[i];
1152 break;
1153 }
1154 }
1155
Suren Baghdasaryanfe26dfb2018-05-18 14:42:00 -07001156 if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
1157 if (debug_process_killing) {
1158 ALOGI("Ignore %s memory pressure event "
1159 "(free memory=%ldkB, cache=%ldkB, limit=%ldkB)",
1160 level_name[level], other_free * page_k, other_file * page_k,
1161 (long)lowmem_minfree[lowmem_targets_size - 1] * page_k);
1162 }
Suren Baghdasaryanc09b53d2018-04-13 13:53:43 -07001163 return;
Suren Baghdasaryanfe26dfb2018-05-18 14:42:00 -07001164 }
Suren Baghdasaryanc09b53d2018-04-13 13:53:43 -07001165
1166 /* Free up enough pages to push over the highest minfree level */
1167 pages_to_free = lowmem_minfree[lowmem_targets_size - 1] -
1168 ((other_free < other_file) ? other_free : other_file);
1169 goto do_kill;
1170 }
1171
Suren Baghdasaryan45c9e0b2018-04-13 13:41:12 -07001172 if (level == VMPRESS_LEVEL_LOW) {
1173 record_low_pressure_levels(&mi);
1174 }
1175
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001176 if (level_oomadj[level] > OOM_SCORE_ADJ_MAX) {
1177 /* Do not monitor this pressure level */
1178 return;
1179 }
1180
Suren Baghdasaryan6499e5e2018-04-13 12:43:41 -07001181 if ((mem_usage = get_memory_usage(&mem_usage_file_data)) < 0) {
1182 goto do_kill;
1183 }
1184 if ((memsw_usage = get_memory_usage(&memsw_usage_file_data)) < 0) {
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001185 goto do_kill;
Robert Benea6e8e7102017-09-13 15:20:30 -07001186 }
Robert Beneac47f2992017-08-21 15:18:31 -07001187
Robert Benea6e8e7102017-09-13 15:20:30 -07001188 // Calculate percent for swappinness.
1189 mem_pressure = (mem_usage * 100) / memsw_usage;
1190
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001191 if (enable_pressure_upgrade && level != VMPRESS_LEVEL_CRITICAL) {
Robert Benea6e8e7102017-09-13 15:20:30 -07001192 // We are swapping too much.
1193 if (mem_pressure < upgrade_pressure) {
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001194 level = upgrade_level(level);
1195 if (debug_process_killing) {
1196 ALOGI("Event upgraded to %s", level_name[level]);
1197 }
Robert Beneac47f2992017-08-21 15:18:31 -07001198 }
1199 }
1200
Robert Benea6e8e7102017-09-13 15:20:30 -07001201 // If the pressure is larger than downgrade_pressure lmk will not
1202 // kill any process, since enough memory is available.
1203 if (mem_pressure > downgrade_pressure) {
1204 if (debug_process_killing) {
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001205 ALOGI("Ignore %s memory pressure", level_name[level]);
Robert Benea6e8e7102017-09-13 15:20:30 -07001206 }
1207 return;
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001208 } else if (level == VMPRESS_LEVEL_CRITICAL &&
1209 mem_pressure > upgrade_pressure) {
Robert Benea6e8e7102017-09-13 15:20:30 -07001210 if (debug_process_killing) {
1211 ALOGI("Downgrade critical memory pressure");
1212 }
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001213 // Downgrade event, since enough memory available.
1214 level = downgrade_level(level);
Robert Benea6e8e7102017-09-13 15:20:30 -07001215 }
1216
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001217do_kill:
Suren Baghdasaryanff61afb2018-04-13 11:45:38 -07001218 if (low_ram_device) {
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001219 /* For Go devices kill only one task */
Suren Baghdasaryanc09b53d2018-04-13 13:53:43 -07001220 if (find_and_kill_processes(level, level_oomadj[level], 0) == 0) {
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001221 if (debug_process_killing) {
1222 ALOGI("Nothing to kill");
1223 }
1224 }
1225 } else {
Suren Baghdasaryanc09b53d2018-04-13 13:53:43 -07001226 int pages_freed;
1227
1228 if (!use_minfree_levels) {
1229 /* If pressure level is less than critical and enough free swap then ignore */
1230 if (level < VMPRESS_LEVEL_CRITICAL &&
1231 mi.field.free_swap > low_pressure_mem.max_nr_free_pages) {
1232 if (debug_process_killing) {
1233 ALOGI("Ignoring pressure since %" PRId64
1234 " swap pages are available ",
1235 mi.field.free_swap);
1236 }
1237 return;
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001238 }
Suren Baghdasaryanc09b53d2018-04-13 13:53:43 -07001239 /* Free up enough memory to downgrate the memory pressure to low level */
1240 if (mi.field.nr_free_pages < low_pressure_mem.max_nr_free_pages) {
1241 pages_to_free = low_pressure_mem.max_nr_free_pages -
1242 mi.field.nr_free_pages;
1243 } else {
1244 if (debug_process_killing) {
1245 ALOGI("Ignoring pressure since more memory is "
1246 "available (%" PRId64 ") than watermark (%" PRId64 ")",
1247 mi.field.nr_free_pages, low_pressure_mem.max_nr_free_pages);
1248 }
1249 return;
1250 }
1251 min_score_adj = level_oomadj[level];
1252 } else {
1253 if (debug_process_killing) {
1254 ALOGI("Killing because cache %ldkB is below "
1255 "limit %ldkB for oom_adj %d\n"
1256 " Free memory is %ldkB %s reserved",
1257 other_file * page_k, minfree * page_k, min_score_adj,
1258 other_free * page_k, other_free >= 0 ? "above" : "below");
1259 }
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001260 }
1261
Suren Baghdasaryanc09b53d2018-04-13 13:53:43 -07001262 if (debug_process_killing) {
1263 ALOGI("Trying to free %d pages", pages_to_free);
1264 }
1265 pages_freed = find_and_kill_processes(level, min_score_adj, pages_to_free);
1266 if (pages_freed < pages_to_free) {
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001267 if (debug_process_killing) {
Suren Baghdasaryanc09b53d2018-04-13 13:53:43 -07001268 ALOGI("Unable to free enough memory (pages freed=%d)", pages_freed);
Suren Baghdasaryanaa73baf2018-01-17 17:17:44 -08001269 }
Suren Baghdasaryanc09b53d2018-04-13 13:53:43 -07001270 } else {
1271 gettimeofday(&last_report_tm, NULL);
Robert Beneacaeaa652017-08-11 16:03:20 -07001272 }
Colin Crossf8857cc2014-07-11 17:16:56 -07001273 }
Todd Poynor3948f802013-07-09 19:35:14 -07001274}
1275
Suren Baghdasaryan06f75702018-01-26 12:51:19 -08001276static bool init_mp_common(enum vmpressure_level level) {
Todd Poynor3948f802013-07-09 19:35:14 -07001277 int mpfd;
1278 int evfd;
1279 int evctlfd;
1280 char buf[256];
1281 struct epoll_event epev;
1282 int ret;
Suren Baghdasaryan06f75702018-01-26 12:51:19 -08001283 int level_idx = (int)level;
1284 const char *levelstr = level_name[level_idx];
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001285
Mark Salyzyn64d97d82018-04-09 09:50:32 -07001286 /* gid containing AID_SYSTEM required */
Nick Kralevichc68c8862015-12-18 20:52:37 -08001287 mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC);
Todd Poynor3948f802013-07-09 19:35:14 -07001288 if (mpfd < 0) {
1289 ALOGI("No kernel memory.pressure_level support (errno=%d)", errno);
1290 goto err_open_mpfd;
1291 }
1292
Nick Kralevichc68c8862015-12-18 20:52:37 -08001293 evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY | O_CLOEXEC);
Todd Poynor3948f802013-07-09 19:35:14 -07001294 if (evctlfd < 0) {
1295 ALOGI("No kernel memory cgroup event control (errno=%d)", errno);
1296 goto err_open_evctlfd;
1297 }
1298
Nick Kralevichc68c8862015-12-18 20:52:37 -08001299 evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
Todd Poynor3948f802013-07-09 19:35:14 -07001300 if (evfd < 0) {
1301 ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno);
1302 goto err_eventfd;
1303 }
1304
1305 ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr);
1306 if (ret >= (ssize_t)sizeof(buf)) {
1307 ALOGE("cgroup.event_control line overflow for level %s", levelstr);
1308 goto err;
1309 }
1310
Suren Baghdasaryan06f75702018-01-26 12:51:19 -08001311 ret = TEMP_FAILURE_RETRY(write(evctlfd, buf, strlen(buf) + 1));
Todd Poynor3948f802013-07-09 19:35:14 -07001312 if (ret == -1) {
1313 ALOGE("cgroup.event_control write failed for level %s; errno=%d",
1314 levelstr, errno);
1315 goto err;
1316 }
1317
1318 epev.events = EPOLLIN;
Suren Baghdasaryan06f75702018-01-26 12:51:19 -08001319 /* use data to store event level */
1320 vmpressure_hinfo[level_idx].data = level_idx;
1321 vmpressure_hinfo[level_idx].handler = mp_event_common;
1322 epev.data.ptr = (void *)&vmpressure_hinfo[level_idx];
Todd Poynor3948f802013-07-09 19:35:14 -07001323 ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev);
1324 if (ret == -1) {
1325 ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno);
1326 goto err;
1327 }
1328 maxevents++;
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001329 mpevfd[level] = evfd;
Suren Baghdasaryanab05d672018-01-04 08:54:53 -08001330 close(evctlfd);
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001331 return true;
Todd Poynor3948f802013-07-09 19:35:14 -07001332
1333err:
1334 close(evfd);
1335err_eventfd:
1336 close(evctlfd);
1337err_open_evctlfd:
1338 close(mpfd);
1339err_open_mpfd:
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001340 return false;
Robert Benea673e2762017-06-01 16:32:31 -07001341}
1342
Todd Poynor3948f802013-07-09 19:35:14 -07001343static int init(void) {
1344 struct epoll_event epev;
1345 int i;
1346 int ret;
1347
1348 page_k = sysconf(_SC_PAGESIZE);
1349 if (page_k == -1)
1350 page_k = PAGE_SIZE;
1351 page_k /= 1024;
1352
1353 epollfd = epoll_create(MAX_EPOLL_EVENTS);
1354 if (epollfd == -1) {
1355 ALOGE("epoll_create failed (errno=%d)", errno);
1356 return -1;
1357 }
1358
Suren Baghdasaryan06f75702018-01-26 12:51:19 -08001359 // mark data connections as not connected
1360 for (int i = 0; i < MAX_DATA_CONN; i++) {
1361 data_sock[i].sock = -1;
1362 }
1363
1364 ctrl_sock.sock = android_get_control_socket("lmkd");
1365 if (ctrl_sock.sock < 0) {
Todd Poynor3948f802013-07-09 19:35:14 -07001366 ALOGE("get lmkd control socket failed");
1367 return -1;
1368 }
1369
Suren Baghdasaryan06f75702018-01-26 12:51:19 -08001370 ret = listen(ctrl_sock.sock, MAX_DATA_CONN);
Todd Poynor3948f802013-07-09 19:35:14 -07001371 if (ret < 0) {
1372 ALOGE("lmkd control socket listen failed (errno=%d)", errno);
1373 return -1;
1374 }
1375
1376 epev.events = EPOLLIN;
Suren Baghdasaryan06f75702018-01-26 12:51:19 -08001377 ctrl_sock.handler_info.handler = ctrl_connect_handler;
1378 epev.data.ptr = (void *)&(ctrl_sock.handler_info);
1379 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_sock.sock, &epev) == -1) {
Todd Poynor3948f802013-07-09 19:35:14 -07001380 ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno);
1381 return -1;
1382 }
1383 maxevents++;
1384
Robert Benea164baeb2017-09-11 16:53:28 -07001385 has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK);
Suren Baghdasaryan2088f6a2018-01-18 17:27:30 -08001386 use_inkernel_interface = has_inkernel_module;
Todd Poynor3948f802013-07-09 19:35:14 -07001387
1388 if (use_inkernel_interface) {
1389 ALOGI("Using in-kernel low memory killer interface");
1390 } else {
Suren Baghdasaryan06f75702018-01-26 12:51:19 -08001391 if (!init_mp_common(VMPRESS_LEVEL_LOW) ||
1392 !init_mp_common(VMPRESS_LEVEL_MEDIUM) ||
1393 !init_mp_common(VMPRESS_LEVEL_CRITICAL)) {
Todd Poynor3948f802013-07-09 19:35:14 -07001394 ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer");
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001395 return -1;
1396 }
Todd Poynor3948f802013-07-09 19:35:14 -07001397 }
1398
Chong Zhang0a4acdf2015-10-14 16:19:53 -07001399 for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) {
Todd Poynor3948f802013-07-09 19:35:14 -07001400 procadjslot_list[i].next = &procadjslot_list[i];
1401 procadjslot_list[i].prev = &procadjslot_list[i];
1402 }
1403
1404 return 0;
1405}
1406
1407static void mainloop(void) {
Suren Baghdasaryan06f75702018-01-26 12:51:19 -08001408 struct event_handler_info* handler_info;
1409 struct epoll_event *evt;
1410
Todd Poynor3948f802013-07-09 19:35:14 -07001411 while (1) {
1412 struct epoll_event events[maxevents];
1413 int nevents;
1414 int i;
1415
Todd Poynor3948f802013-07-09 19:35:14 -07001416 nevents = epoll_wait(epollfd, events, maxevents, -1);
1417
1418 if (nevents == -1) {
1419 if (errno == EINTR)
1420 continue;
1421 ALOGE("epoll_wait failed (errno=%d)", errno);
1422 continue;
1423 }
1424
Suren Baghdasaryan06f75702018-01-26 12:51:19 -08001425 /*
1426 * First pass to see if any data socket connections were dropped.
1427 * Dropped connection should be handled before any other events
1428 * to deallocate data connection and correctly handle cases when
1429 * connection gets dropped and reestablished in the same epoll cycle.
1430 * In such cases it's essential to handle connection closures first.
1431 */
1432 for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) {
1433 if ((evt->events & EPOLLHUP) && evt->data.ptr) {
1434 ALOGI("lmkd data connection dropped");
1435 handler_info = (struct event_handler_info*)evt->data.ptr;
1436 ctrl_data_close(handler_info->data);
1437 }
1438 }
1439
1440 /* Second pass to handle all other events */
1441 for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) {
1442 if (evt->events & EPOLLERR)
Todd Poynor3948f802013-07-09 19:35:14 -07001443 ALOGD("EPOLLERR on event #%d", i);
Suren Baghdasaryan06f75702018-01-26 12:51:19 -08001444 if (evt->events & EPOLLHUP) {
1445 /* This case was handled in the first pass */
1446 continue;
1447 }
1448 if (evt->data.ptr) {
1449 handler_info = (struct event_handler_info*)evt->data.ptr;
1450 handler_info->handler(handler_info->data, evt->events);
1451 }
Todd Poynor3948f802013-07-09 19:35:14 -07001452 }
1453 }
1454}
1455
Mark Salyzyne6ed68b2014-04-30 13:36:35 -07001456int main(int argc __unused, char **argv __unused) {
Colin Cross1a0d9be2014-07-14 14:31:15 -07001457 struct sched_param param = {
1458 .sched_priority = 1,
1459 };
1460
Suren Baghdasaryancd7ad2f2017-12-08 12:58:52 -08001461 /* By default disable low level vmpressure events */
1462 level_oomadj[VMPRESS_LEVEL_LOW] =
1463 property_get_int32("ro.lmk.low", OOM_SCORE_ADJ_MAX + 1);
1464 level_oomadj[VMPRESS_LEVEL_MEDIUM] =
1465 property_get_int32("ro.lmk.medium", 800);
1466 level_oomadj[VMPRESS_LEVEL_CRITICAL] =
1467 property_get_int32("ro.lmk.critical", 0);
Robert Beneacaeaa652017-08-11 16:03:20 -07001468 debug_process_killing = property_get_bool("ro.lmk.debug", false);
Suren Baghdasaryane5c964312017-12-08 13:08:41 -08001469
1470 /* By default disable upgrade/downgrade logic */
1471 enable_pressure_upgrade =
1472 property_get_bool("ro.lmk.critical_upgrade", false);
1473 upgrade_pressure =
1474 (int64_t)property_get_int32("ro.lmk.upgrade_pressure", 100);
1475 downgrade_pressure =
1476 (int64_t)property_get_int32("ro.lmk.downgrade_pressure", 100);
Suren Baghdasaryanb93764d2017-12-08 13:17:06 -08001477 kill_heaviest_task =
Suren Baghdasaryan818b59b2018-04-13 11:49:54 -07001478 property_get_bool("ro.lmk.kill_heaviest_task", false);
Suren Baghdasaryanff61afb2018-04-13 11:45:38 -07001479 low_ram_device = property_get_bool("ro.config.low_ram", false);
Suren Baghdasaryan63dadcf2018-01-17 17:28:01 -08001480 kill_timeout_ms =
1481 (unsigned long)property_get_int32("ro.lmk.kill_timeout_ms", 0);
Suren Baghdasaryanc09b53d2018-04-13 13:53:43 -07001482 use_minfree_levels =
1483 property_get_bool("ro.lmk.use_minfree_levels", false);
Robert Benea58891d52017-07-31 17:15:20 -07001484
Mark Salyzyn721d7c72018-03-21 12:24:58 -07001485 if (!init()) {
1486 if (!use_inkernel_interface) {
1487 /*
1488 * MCL_ONFAULT pins pages as they fault instead of loading
1489 * everything immediately all at once. (Which would be bad,
1490 * because as of this writing, we have a lot of mapped pages we
1491 * never use.) Old kernels will see MCL_ONFAULT and fail with
1492 * EINVAL; we ignore this failure.
1493 *
1494 * N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT
1495 * pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault
1496 * in pages.
1497 */
Mark Salyzyn64d97d82018-04-09 09:50:32 -07001498 /* CAP_IPC_LOCK required */
Mark Salyzyn721d7c72018-03-21 12:24:58 -07001499 if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && (errno != EINVAL)) {
1500 ALOGW("mlockall failed %s", strerror(errno));
1501 }
Daniel Colascione4dd5d002018-01-03 12:01:02 -08001502
Mark Salyzyn64d97d82018-04-09 09:50:32 -07001503 /* CAP_NICE required */
1504 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1505 ALOGW("set SCHED_FIFO failed %s", strerror(errno));
1506 }
Mark Salyzyn721d7c72018-03-21 12:24:58 -07001507 }
1508
Todd Poynor3948f802013-07-09 19:35:14 -07001509 mainloop();
Mark Salyzyn721d7c72018-03-21 12:24:58 -07001510 }
Todd Poynor3948f802013-07-09 19:35:14 -07001511
1512 ALOGI("exiting");
1513 return 0;
1514}