• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
20 #include <inttypes.h>
21 #include <pwd.h>
22 #include <sched.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/cdefs.h>
27 #include <sys/epoll.h>
28 #include <sys/eventfd.h>
29 #include <sys/mman.h>
30 #include <sys/pidfd.h>
31 #include <sys/socket.h>
32 #include <sys/syscall.h>
33 #include <sys/sysinfo.h>
34 #include <time.h>
35 #include <unistd.h>
36 
37 #include <algorithm>
38 #include <array>
39 #include <shared_mutex>
40 
41 #include <cutils/properties.h>
42 #include <cutils/sockets.h>
43 #include <liblmkd_utils.h>
44 #include <lmkd.h>
45 #include <log/log.h>
46 #include <log/log_event_list.h>
47 #include <log/log_time.h>
48 #include <private/android_filesystem_config.h>
49 #include <processgroup/processgroup.h>
50 #include <psi/psi.h>
51 
52 #include "reaper.h"
53 #include "statslog.h"
54 #include "watchdog.h"
55 
56 #define BPF_FD_JUST_USE_INT
57 #include "BpfSyscallWrappers.h"
58 
59 /*
60  * Define LMKD_TRACE_KILLS to record lmkd kills in kernel traces
61  * to profile and correlate with OOM kills
62  */
63 #ifdef LMKD_TRACE_KILLS
64 
65 #define ATRACE_TAG ATRACE_TAG_ALWAYS
66 #include <cutils/trace.h>
67 
trace_kill_start(int pid,const char * desc)68 static inline void trace_kill_start(int pid, const char *desc) {
69     ATRACE_INT("kill_one_process", pid);
70     ATRACE_BEGIN(desc);
71 }
72 
trace_kill_end()73 static inline void trace_kill_end() {
74     ATRACE_END();
75     ATRACE_INT("kill_one_process", 0);
76 }
77 
78 #else /* LMKD_TRACE_KILLS */
79 
trace_kill_start(int,const char *)80 static inline void trace_kill_start(int, const char *) {}
trace_kill_end()81 static inline void trace_kill_end() {}
82 
83 #endif /* LMKD_TRACE_KILLS */
84 
85 #ifndef __unused
86 #define __unused __attribute__((__unused__))
87 #endif
88 
89 #define ZONEINFO_PATH "/proc/zoneinfo"
90 #define MEMINFO_PATH "/proc/meminfo"
91 #define VMSTAT_PATH "/proc/vmstat"
92 #define PROC_STATUS_TGID_FIELD "Tgid:"
93 #define PROC_STATUS_RSS_FIELD "VmRSS:"
94 #define PROC_STATUS_SWAP_FIELD "VmSwap:"
95 #define LINE_MAX 128
96 
97 #define PERCEPTIBLE_APP_ADJ 200
98 
99 /* Android Logger event logtags (see event.logtags) */
100 #define KILLINFO_LOG_TAG 10195355
101 
102 /* gid containing AID_SYSTEM required */
103 #define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree"
104 #define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj"
105 
106 #define EIGHT_MEGA (1 << 23)
107 
108 #define TARGET_UPDATE_MIN_INTERVAL_MS 1000
109 #define THRASHING_RESET_INTERVAL_MS 1000
110 
111 #define NS_PER_MS (NS_PER_SEC / MS_PER_SEC)
112 #define US_PER_MS (US_PER_SEC / MS_PER_SEC)
113 
114 /* Defined as ProcessList.SYSTEM_ADJ in ProcessList.java */
115 #define SYSTEM_ADJ (-900)
116 
117 #define STRINGIFY(x) STRINGIFY_INTERNAL(x)
118 #define STRINGIFY_INTERNAL(x) #x
119 
120 /*
121  * Read lmk property with persist.device_config.lmkd_native.<name> overriding ro.lmk.<name>
122  * persist.device_config.lmkd_native.* properties are being set by experiments. If a new property
123  * can be controlled by an experiment then use GET_LMK_PROPERTY instead of property_get_xxx and
124  * add "on property" triggers in lmkd.rc to react to the experiment flag changes.
125  */
126 #define GET_LMK_PROPERTY(type, name, def) \
127     property_get_##type("persist.device_config.lmkd_native." name, \
128         property_get_##type("ro.lmk." name, def))
129 
130 /*
131  * PSI monitor tracking window size.
132  * PSI monitor generates events at most once per window,
133  * therefore we poll memory state for the duration of
134  * PSI_WINDOW_SIZE_MS after the event happens.
135  */
136 #define PSI_WINDOW_SIZE_MS 1000
137 /* Polling period after PSI signal when pressure is high */
138 #define PSI_POLL_PERIOD_SHORT_MS 10
139 /* Polling period after PSI signal when pressure is low */
140 #define PSI_POLL_PERIOD_LONG_MS 100
141 
142 #define FAIL_REPORT_RLIMIT_MS 1000
143 
144 /*
145  * System property defaults
146  */
147 /* ro.lmk.swap_free_low_percentage property defaults */
148 #define DEF_LOW_SWAP 10
149 /* ro.lmk.thrashing_limit property defaults */
150 #define DEF_THRASHING_LOWRAM 30
151 #define DEF_THRASHING 100
152 /* ro.lmk.thrashing_limit_decay property defaults */
153 #define DEF_THRASHING_DECAY_LOWRAM 50
154 #define DEF_THRASHING_DECAY 10
155 /* ro.lmk.psi_partial_stall_ms property defaults */
156 #define DEF_PARTIAL_STALL_LOWRAM 200
157 #define DEF_PARTIAL_STALL 70
158 /* ro.lmk.psi_complete_stall_ms property defaults */
159 #define DEF_COMPLETE_STALL 700
160 
161 #define LMKD_REINIT_PROP "lmkd.reinit"
162 
163 #define WATCHDOG_TIMEOUT_SEC 2
164 
165 /* default to old in-kernel interface if no memory pressure events */
166 static bool use_inkernel_interface = true;
167 static bool has_inkernel_module;
168 
169 /* memory pressure levels */
170 enum vmpressure_level {
171     VMPRESS_LEVEL_LOW = 0,
172     VMPRESS_LEVEL_MEDIUM,
173     VMPRESS_LEVEL_CRITICAL,
174     VMPRESS_LEVEL_COUNT
175 };
176 
177 static const char *level_name[] = {
178     "low",
179     "medium",
180     "critical"
181 };
182 
183 struct {
184     int64_t min_nr_free_pages; /* recorded but not used yet */
185     int64_t max_nr_free_pages;
186 } low_pressure_mem = { -1, -1 };
187 
188 struct psi_threshold {
189     enum psi_stall_type stall_type;
190     int threshold_ms;
191 };
192 
193 static int level_oomadj[VMPRESS_LEVEL_COUNT];
194 static int mpevfd[VMPRESS_LEVEL_COUNT] = { -1, -1, -1 };
195 static bool pidfd_supported;
196 static int last_kill_pid_or_fd = -1;
197 static struct timespec last_kill_tm;
198 
199 /* lmkd configurable parameters */
200 static bool debug_process_killing;
201 static bool enable_pressure_upgrade;
202 static int64_t upgrade_pressure;
203 static int64_t downgrade_pressure;
204 static bool low_ram_device;
205 static bool kill_heaviest_task;
206 static unsigned long kill_timeout_ms;
207 static bool use_minfree_levels;
208 static bool per_app_memcg;
209 static int swap_free_low_percentage;
210 static int psi_partial_stall_ms;
211 static int psi_complete_stall_ms;
212 static int thrashing_limit_pct;
213 static int thrashing_limit_decay_pct;
214 static int thrashing_critical_pct;
215 static int swap_util_max;
216 static int64_t filecache_min_kb;
217 static int64_t stall_limit_critical;
218 static bool use_psi_monitors = false;
219 static int kpoll_fd;
220 static struct psi_threshold psi_thresholds[VMPRESS_LEVEL_COUNT] = {
221     { PSI_SOME, 70 },    /* 70ms out of 1sec for partial stall */
222     { PSI_SOME, 100 },   /* 100ms out of 1sec for partial stall */
223     { PSI_FULL, 70 },    /* 70ms out of 1sec for complete stall */
224 };
225 
226 static android_log_context ctx;
227 static Reaper reaper;
228 static int reaper_comm_fd[2];
229 
230 enum polling_update {
231     POLLING_DO_NOT_CHANGE,
232     POLLING_START,
233     POLLING_PAUSE,
234     POLLING_RESUME,
235 };
236 
237 /*
238  * Data used for periodic polling for the memory state of the device.
239  * Note that when system is not polling poll_handler is set to NULL,
240  * when polling starts poll_handler gets set and is reset back to
241  * NULL when polling stops.
242  */
243 struct polling_params {
244     struct event_handler_info* poll_handler;
245     struct event_handler_info* paused_handler;
246     struct timespec poll_start_tm;
247     struct timespec last_poll_tm;
248     int polling_interval_ms;
249     enum polling_update update;
250 };
251 
252 /* data required to handle events */
253 struct event_handler_info {
254     int data;
255     void (*handler)(int data, uint32_t events, struct polling_params *poll_params);
256 };
257 
258 /* data required to handle socket events */
259 struct sock_event_handler_info {
260     int sock;
261     pid_t pid;
262     uint32_t async_event_mask;
263     struct event_handler_info handler_info;
264 };
265 
266 /* max supported number of data connections (AMS, init, tests) */
267 #define MAX_DATA_CONN 3
268 
269 /* socket event handler data */
270 static struct sock_event_handler_info ctrl_sock;
271 static struct sock_event_handler_info data_sock[MAX_DATA_CONN];
272 
273 /* vmpressure event handler data */
274 static struct event_handler_info vmpressure_hinfo[VMPRESS_LEVEL_COUNT];
275 
276 /*
277  * 1 ctrl listen socket, 3 ctrl data socket, 3 memory pressure levels,
278  * 1 lmk events + 1 fd to wait for process death + 1 fd to receive kill failure notifications
279  */
280 #define MAX_EPOLL_EVENTS (1 + MAX_DATA_CONN + VMPRESS_LEVEL_COUNT + 1 + 1 + 1)
281 static int epollfd;
282 static int maxevents;
283 
284 /* OOM score values used by both kernel and framework */
285 #define OOM_SCORE_ADJ_MIN       (-1000)
286 #define OOM_SCORE_ADJ_MAX       1000
287 
288 static std::array<int, MAX_TARGETS> lowmem_adj;
289 static std::array<int, MAX_TARGETS> lowmem_minfree;
290 static int lowmem_targets_size;
291 
292 /* Fields to parse in /proc/zoneinfo */
293 /* zoneinfo per-zone fields */
294 enum zoneinfo_zone_field {
295     ZI_ZONE_NR_FREE_PAGES = 0,
296     ZI_ZONE_MIN,
297     ZI_ZONE_LOW,
298     ZI_ZONE_HIGH,
299     ZI_ZONE_PRESENT,
300     ZI_ZONE_NR_FREE_CMA,
301     ZI_ZONE_FIELD_COUNT
302 };
303 
304 static const char* const zoneinfo_zone_field_names[ZI_ZONE_FIELD_COUNT] = {
305     "nr_free_pages",
306     "min",
307     "low",
308     "high",
309     "present",
310     "nr_free_cma",
311 };
312 
313 /* zoneinfo per-zone special fields */
314 enum zoneinfo_zone_spec_field {
315     ZI_ZONE_SPEC_PROTECTION = 0,
316     ZI_ZONE_SPEC_PAGESETS,
317     ZI_ZONE_SPEC_FIELD_COUNT,
318 };
319 
320 static const char* const zoneinfo_zone_spec_field_names[ZI_ZONE_SPEC_FIELD_COUNT] = {
321     "protection:",
322     "pagesets",
323 };
324 
325 /* see __MAX_NR_ZONES definition in kernel mmzone.h */
326 #define MAX_NR_ZONES 6
327 
328 union zoneinfo_zone_fields {
329     struct {
330         int64_t nr_free_pages;
331         int64_t min;
332         int64_t low;
333         int64_t high;
334         int64_t present;
335         int64_t nr_free_cma;
336     } field;
337     int64_t arr[ZI_ZONE_FIELD_COUNT];
338 };
339 
340 struct zoneinfo_zone {
341     union zoneinfo_zone_fields fields;
342     int64_t protection[MAX_NR_ZONES];
343     int64_t max_protection;
344 };
345 
346 /* zoneinfo per-node fields */
347 enum zoneinfo_node_field {
348     ZI_NODE_NR_INACTIVE_FILE = 0,
349     ZI_NODE_NR_ACTIVE_FILE,
350     ZI_NODE_FIELD_COUNT
351 };
352 
353 static const char* const zoneinfo_node_field_names[ZI_NODE_FIELD_COUNT] = {
354     "nr_inactive_file",
355     "nr_active_file",
356 };
357 
358 union zoneinfo_node_fields {
359     struct {
360         int64_t nr_inactive_file;
361         int64_t nr_active_file;
362     } field;
363     int64_t arr[ZI_NODE_FIELD_COUNT];
364 };
365 
366 struct zoneinfo_node {
367     int id;
368     int zone_count;
369     struct zoneinfo_zone zones[MAX_NR_ZONES];
370     union zoneinfo_node_fields fields;
371 };
372 
373 /* for now two memory nodes is more than enough */
374 #define MAX_NR_NODES 2
375 
376 struct zoneinfo {
377     int node_count;
378     struct zoneinfo_node nodes[MAX_NR_NODES];
379     int64_t totalreserve_pages;
380     int64_t total_inactive_file;
381     int64_t total_active_file;
382 };
383 
384 /* Fields to parse in /proc/meminfo */
385 enum meminfo_field {
386     MI_NR_FREE_PAGES = 0,
387     MI_CACHED,
388     MI_SWAP_CACHED,
389     MI_BUFFERS,
390     MI_SHMEM,
391     MI_UNEVICTABLE,
392     MI_TOTAL_SWAP,
393     MI_FREE_SWAP,
394     MI_ACTIVE_ANON,
395     MI_INACTIVE_ANON,
396     MI_ACTIVE_FILE,
397     MI_INACTIVE_FILE,
398     MI_SRECLAIMABLE,
399     MI_SUNRECLAIM,
400     MI_KERNEL_STACK,
401     MI_PAGE_TABLES,
402     MI_ION_HELP,
403     MI_ION_HELP_POOL,
404     MI_CMA_FREE,
405     MI_FIELD_COUNT
406 };
407 
408 static const char* const meminfo_field_names[MI_FIELD_COUNT] = {
409     "MemFree:",
410     "Cached:",
411     "SwapCached:",
412     "Buffers:",
413     "Shmem:",
414     "Unevictable:",
415     "SwapTotal:",
416     "SwapFree:",
417     "Active(anon):",
418     "Inactive(anon):",
419     "Active(file):",
420     "Inactive(file):",
421     "SReclaimable:",
422     "SUnreclaim:",
423     "KernelStack:",
424     "PageTables:",
425     "ION_heap:",
426     "ION_heap_pool:",
427     "CmaFree:",
428 };
429 
430 union meminfo {
431     struct {
432         int64_t nr_free_pages;
433         int64_t cached;
434         int64_t swap_cached;
435         int64_t buffers;
436         int64_t shmem;
437         int64_t unevictable;
438         int64_t total_swap;
439         int64_t free_swap;
440         int64_t active_anon;
441         int64_t inactive_anon;
442         int64_t active_file;
443         int64_t inactive_file;
444         int64_t sreclaimable;
445         int64_t sunreclaimable;
446         int64_t kernel_stack;
447         int64_t page_tables;
448         int64_t ion_heap;
449         int64_t ion_heap_pool;
450         int64_t cma_free;
451         /* fields below are calculated rather than read from the file */
452         int64_t nr_file_pages;
453         int64_t total_gpu_kb;
454     } field;
455     int64_t arr[MI_FIELD_COUNT];
456 };
457 
458 /* Fields to parse in /proc/vmstat */
459 enum vmstat_field {
460     VS_FREE_PAGES,
461     VS_INACTIVE_FILE,
462     VS_ACTIVE_FILE,
463     VS_WORKINGSET_REFAULT,
464     VS_WORKINGSET_REFAULT_FILE,
465     VS_PGSCAN_KSWAPD,
466     VS_PGSCAN_DIRECT,
467     VS_PGSCAN_DIRECT_THROTTLE,
468     VS_FIELD_COUNT
469 };
470 
471 static const char* const vmstat_field_names[MI_FIELD_COUNT] = {
472     "nr_free_pages",
473     "nr_inactive_file",
474     "nr_active_file",
475     "workingset_refault",
476     "workingset_refault_file",
477     "pgscan_kswapd",
478     "pgscan_direct",
479     "pgscan_direct_throttle",
480 };
481 
482 union vmstat {
483     struct {
484         int64_t nr_free_pages;
485         int64_t nr_inactive_file;
486         int64_t nr_active_file;
487         int64_t workingset_refault;
488         int64_t workingset_refault_file;
489         int64_t pgscan_kswapd;
490         int64_t pgscan_direct;
491         int64_t pgscan_direct_throttle;
492     } field;
493     int64_t arr[VS_FIELD_COUNT];
494 };
495 
496 enum field_match_result {
497     NO_MATCH,
498     PARSE_FAIL,
499     PARSE_SUCCESS
500 };
501 
502 struct adjslot_list {
503     struct adjslot_list *next;
504     struct adjslot_list *prev;
505 };
506 
507 struct proc {
508     struct adjslot_list asl;
509     int pid;
510     int pidfd;
511     uid_t uid;
512     int oomadj;
513     pid_t reg_pid; /* PID of the process that registered this record */
514     bool valid;
515     struct proc *pidhash_next;
516 };
517 
518 struct reread_data {
519     const char* const filename;
520     int fd;
521 };
522 
523 #define PIDHASH_SZ 1024
524 static struct proc *pidhash[PIDHASH_SZ];
525 #define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))
526 
527 #define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN)
528 #define ADJTOSLOT_COUNT (ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1)
529 
530 // protects procadjslot_list from concurrent access
531 static std::shared_mutex adjslot_list_lock;
532 // procadjslot_list should be modified only from the main thread while exclusively holding
533 // adjslot_list_lock. Readers from non-main threads should hold adjslot_list_lock shared lock.
534 static struct adjslot_list procadjslot_list[ADJTOSLOT_COUNT];
535 
536 #define MAX_DISTINCT_OOM_ADJ 32
537 #define KILLCNT_INVALID_IDX 0xFF
538 /*
539  * Because killcnt array is sparse a two-level indirection is used
540  * to keep the size small. killcnt_idx stores index of the element in
541  * killcnt array. Index KILLCNT_INVALID_IDX indicates an unused slot.
542  */
543 static uint8_t killcnt_idx[ADJTOSLOT_COUNT];
544 static uint16_t killcnt[MAX_DISTINCT_OOM_ADJ];
545 static int killcnt_free_idx = 0;
546 static uint32_t killcnt_total = 0;
547 
548 /* PAGE_SIZE / 1024 */
549 static long page_k;
550 
551 static void update_props();
552 static bool init_monitors();
553 static void destroy_monitors();
554 
clamp(int low,int high,int value)555 static int clamp(int low, int high, int value) {
556     return std::max(std::min(value, high), low);
557 }
558 
parse_int64(const char * str,int64_t * ret)559 static bool parse_int64(const char* str, int64_t* ret) {
560     char* endptr;
561     long long val = strtoll(str, &endptr, 10);
562     if (str == endptr || val > INT64_MAX) {
563         return false;
564     }
565     *ret = (int64_t)val;
566     return true;
567 }
568 
find_field(const char * name,const char * const field_names[],int field_count)569 static int find_field(const char* name, const char* const field_names[], int field_count) {
570     for (int i = 0; i < field_count; i++) {
571         if (!strcmp(name, field_names[i])) {
572             return i;
573         }
574     }
575     return -1;
576 }
577 
match_field(const char * cp,const char * ap,const char * const field_names[],int field_count,int64_t * field,int * field_idx)578 static enum field_match_result match_field(const char* cp, const char* ap,
579                                    const char* const field_names[],
580                                    int field_count, int64_t* field,
581                                    int *field_idx) {
582     int i = find_field(cp, field_names, field_count);
583     if (i < 0) {
584         return NO_MATCH;
585     }
586     *field_idx = i;
587     return parse_int64(ap, field) ? PARSE_SUCCESS : PARSE_FAIL;
588 }
589 
590 /*
591  * Read file content from the beginning up to max_len bytes or EOF
592  * whichever happens first.
593  */
read_all(int fd,char * buf,size_t max_len)594 static ssize_t read_all(int fd, char *buf, size_t max_len)
595 {
596     ssize_t ret = 0;
597     off_t offset = 0;
598 
599     while (max_len > 0) {
600         ssize_t r = TEMP_FAILURE_RETRY(pread(fd, buf, max_len, offset));
601         if (r == 0) {
602             break;
603         }
604         if (r == -1) {
605             return -1;
606         }
607         ret += r;
608         buf += r;
609         offset += r;
610         max_len -= r;
611     }
612 
613     return ret;
614 }
615 
616 /*
617  * Read a new or already opened file from the beginning.
618  * If the file has not been opened yet data->fd should be set to -1.
619  * To be used with files which are read often and possibly during high
620  * memory pressure to minimize file opening which by itself requires kernel
621  * memory allocation and might result in a stall on memory stressed system.
622  */
reread_file(struct reread_data * data)623 static char *reread_file(struct reread_data *data) {
624     /* start with page-size buffer and increase if needed */
625     static ssize_t buf_size = PAGE_SIZE;
626     static char *new_buf, *buf = NULL;
627     ssize_t size;
628 
629     if (data->fd == -1) {
630         /* First-time buffer initialization */
631         if (!buf && (buf = static_cast<char*>(malloc(buf_size))) == nullptr) {
632             return NULL;
633         }
634 
635         data->fd = TEMP_FAILURE_RETRY(open(data->filename, O_RDONLY | O_CLOEXEC));
636         if (data->fd < 0) {
637             ALOGE("%s open: %s", data->filename, strerror(errno));
638             return NULL;
639         }
640     }
641 
642     while (true) {
643         size = read_all(data->fd, buf, buf_size - 1);
644         if (size < 0) {
645             ALOGE("%s read: %s", data->filename, strerror(errno));
646             close(data->fd);
647             data->fd = -1;
648             return NULL;
649         }
650         if (size < buf_size - 1) {
651             break;
652         }
653         /*
654          * Since we are reading /proc files we can't use fstat to find out
655          * the real size of the file. Double the buffer size and keep retrying.
656          */
657         if ((new_buf = static_cast<char*>(realloc(buf, buf_size * 2))) == nullptr) {
658             errno = ENOMEM;
659             return NULL;
660         }
661         buf = new_buf;
662         buf_size *= 2;
663     }
664     buf[size] = 0;
665 
666     return buf;
667 }
668 
claim_record(struct proc * procp,pid_t pid)669 static bool claim_record(struct proc* procp, pid_t pid) {
670     if (procp->reg_pid == pid) {
671         /* Record already belongs to the registrant */
672         return true;
673     }
674     if (procp->reg_pid == 0) {
675         /* Old registrant is gone, claim the record */
676         procp->reg_pid = pid;
677         return true;
678     }
679     /* The record is owned by another registrant */
680     return false;
681 }
682 
remove_claims(pid_t pid)683 static void remove_claims(pid_t pid) {
684     int i;
685 
686     for (i = 0; i < PIDHASH_SZ; i++) {
687         struct proc* procp = pidhash[i];
688         while (procp) {
689             if (procp->reg_pid == pid) {
690                 procp->reg_pid = 0;
691             }
692             procp = procp->pidhash_next;
693         }
694     }
695 }
696 
ctrl_data_close(int dsock_idx)697 static void ctrl_data_close(int dsock_idx) {
698     struct epoll_event epev;
699 
700     ALOGI("closing lmkd data connection");
701     if (epoll_ctl(epollfd, EPOLL_CTL_DEL, data_sock[dsock_idx].sock, &epev) == -1) {
702         // Log a warning and keep going
703         ALOGW("epoll_ctl for data connection socket failed; errno=%d", errno);
704     }
705     maxevents--;
706 
707     close(data_sock[dsock_idx].sock);
708     data_sock[dsock_idx].sock = -1;
709 
710     /* Mark all records of the old registrant as unclaimed */
711     remove_claims(data_sock[dsock_idx].pid);
712 }
713 
ctrl_data_read(int dsock_idx,char * buf,size_t bufsz,struct ucred * sender_cred)714 static ssize_t ctrl_data_read(int dsock_idx, char* buf, size_t bufsz, struct ucred* sender_cred) {
715     struct iovec iov = {buf, bufsz};
716     char control[CMSG_SPACE(sizeof(struct ucred))];
717     struct msghdr hdr = {
718             NULL, 0, &iov, 1, control, sizeof(control), 0,
719     };
720     ssize_t ret;
721     ret = TEMP_FAILURE_RETRY(recvmsg(data_sock[dsock_idx].sock, &hdr, 0));
722     if (ret == -1) {
723         ALOGE("control data socket read failed; %s", strerror(errno));
724         return -1;
725     }
726     if (ret == 0) {
727         ALOGE("Got EOF on control data socket");
728         return -1;
729     }
730 
731     struct ucred* cred = NULL;
732     struct cmsghdr* cmsg = CMSG_FIRSTHDR(&hdr);
733     while (cmsg != NULL) {
734         if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_CREDENTIALS) {
735             cred = (struct ucred*)CMSG_DATA(cmsg);
736             break;
737         }
738         cmsg = CMSG_NXTHDR(&hdr, cmsg);
739     }
740 
741     if (cred == NULL) {
742         ALOGE("Failed to retrieve sender credentials");
743         /* Close the connection */
744         ctrl_data_close(dsock_idx);
745         return -1;
746     }
747 
748     memcpy(sender_cred, cred, sizeof(struct ucred));
749 
750     /* Store PID of the peer */
751     data_sock[dsock_idx].pid = cred->pid;
752 
753     return ret;
754 }
755 
ctrl_data_write(int dsock_idx,char * buf,size_t bufsz)756 static int ctrl_data_write(int dsock_idx, char* buf, size_t bufsz) {
757     int ret = 0;
758 
759     ret = TEMP_FAILURE_RETRY(write(data_sock[dsock_idx].sock, buf, bufsz));
760 
761     if (ret == -1) {
762         ALOGE("control data socket write failed; errno=%d", errno);
763     } else if (ret == 0) {
764         ALOGE("Got EOF on control data socket");
765         ret = -1;
766     }
767 
768     return ret;
769 }
770 
771 /*
772  * Write the pid/uid pair over the data socket, note: all active clients
773  * will receive this unsolicited notification.
774  */
ctrl_data_write_lmk_kill_occurred(pid_t pid,uid_t uid)775 static void ctrl_data_write_lmk_kill_occurred(pid_t pid, uid_t uid) {
776     LMKD_CTRL_PACKET packet;
777     size_t len = lmkd_pack_set_prockills(packet, pid, uid);
778 
779     for (int i = 0; i < MAX_DATA_CONN; i++) {
780         if (data_sock[i].sock >= 0 && data_sock[i].async_event_mask & 1 << LMK_ASYNC_EVENT_KILL) {
781             ctrl_data_write(i, (char*)packet, len);
782         }
783     }
784 }
785 
786 /*
787  * Write the kill_stat/memory_stat over the data socket to be propagated via AMS to statsd
788  */
stats_write_lmk_kill_occurred(struct kill_stat * kill_st,struct memory_stat * mem_st)789 static void stats_write_lmk_kill_occurred(struct kill_stat *kill_st,
790                                           struct memory_stat *mem_st) {
791     LMK_KILL_OCCURRED_PACKET packet;
792     const size_t len = lmkd_pack_set_kill_occurred(packet, kill_st, mem_st);
793     if (len == 0) {
794         return;
795     }
796 
797     for (int i = 0; i < MAX_DATA_CONN; i++) {
798         if (data_sock[i].sock >= 0 && data_sock[i].async_event_mask & 1 << LMK_ASYNC_EVENT_STAT) {
799             ctrl_data_write(i, packet, len);
800         }
801     }
802 
803 }
804 
stats_write_lmk_kill_occurred_pid(int pid,struct kill_stat * kill_st,struct memory_stat * mem_st)805 static void stats_write_lmk_kill_occurred_pid(int pid, struct kill_stat *kill_st,
806                                               struct memory_stat *mem_st) {
807     kill_st->taskname = stats_get_task_name(pid);
808     if (kill_st->taskname != NULL) {
809         stats_write_lmk_kill_occurred(kill_st, mem_st);
810     }
811 }
812 
813 /*
814  * Write the state_changed over the data socket to be propagated via AMS to statsd
815  */
stats_write_lmk_state_changed(enum lmk_state state)816 static void stats_write_lmk_state_changed(enum lmk_state state) {
817     LMKD_CTRL_PACKET packet_state_changed;
818     const size_t len = lmkd_pack_set_state_changed(packet_state_changed, state);
819     if (len == 0) {
820         return;
821     }
822     for (int i = 0; i < MAX_DATA_CONN; i++) {
823         if (data_sock[i].sock >= 0 && data_sock[i].async_event_mask & 1 << LMK_ASYNC_EVENT_STAT) {
824             ctrl_data_write(i, (char*)packet_state_changed, len);
825         }
826     }
827 }
828 
poll_kernel(int poll_fd)829 static void poll_kernel(int poll_fd) {
830     if (poll_fd == -1) {
831         // not waiting
832         return;
833     }
834 
835     while (1) {
836         char rd_buf[256];
837         int bytes_read = TEMP_FAILURE_RETRY(pread(poll_fd, (void*)rd_buf, sizeof(rd_buf) - 1, 0));
838         if (bytes_read <= 0) break;
839         rd_buf[bytes_read] = '\0';
840 
841         int64_t pid;
842         int64_t uid;
843         int64_t group_leader_pid;
844         int64_t rss_in_pages;
845         struct memory_stat mem_st = {};
846         int16_t oom_score_adj;
847         int16_t min_score_adj;
848         int64_t starttime;
849         char* taskname = 0;
850 
851         int fields_read =
852                 sscanf(rd_buf,
853                        "%" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64
854                        " %" SCNd16 " %" SCNd16 " %" SCNd64 "\n%m[^\n]",
855                        &pid, &uid, &group_leader_pid, &mem_st.pgfault, &mem_st.pgmajfault,
856                        &rss_in_pages, &oom_score_adj, &min_score_adj, &starttime, &taskname);
857 
858         /* only the death of the group leader process is logged */
859         if (fields_read == 10 && group_leader_pid == pid) {
860             ctrl_data_write_lmk_kill_occurred((pid_t)pid, (uid_t)uid);
861             mem_st.process_start_time_ns = starttime * (NS_PER_SEC / sysconf(_SC_CLK_TCK));
862             mem_st.rss_in_bytes = rss_in_pages * PAGE_SIZE;
863 
864             struct kill_stat kill_st = {
865                 .uid = static_cast<int32_t>(uid),
866                 .kill_reason = NONE,
867                 .oom_score = oom_score_adj,
868                 .min_oom_score = min_score_adj,
869                 .free_mem_kb = 0,
870                 .free_swap_kb = 0,
871             };
872             stats_write_lmk_kill_occurred_pid(pid, &kill_st, &mem_st);
873         }
874 
875         free(taskname);
876     }
877 }
878 
init_poll_kernel()879 static bool init_poll_kernel() {
880     kpoll_fd = TEMP_FAILURE_RETRY(open("/proc/lowmemorykiller", O_RDONLY | O_NONBLOCK | O_CLOEXEC));
881 
882     if (kpoll_fd < 0) {
883         ALOGE("kernel lmk event file could not be opened; errno=%d", errno);
884         return false;
885     }
886 
887     return true;
888 }
889 
pid_lookup(int pid)890 static struct proc *pid_lookup(int pid) {
891     struct proc *procp;
892 
893     for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid;
894          procp = procp->pidhash_next)
895             ;
896 
897     return procp;
898 }
899 
adjslot_insert(struct adjslot_list * head,struct adjslot_list * new_element)900 static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new_element)
901 {
902     struct adjslot_list *next = head->next;
903     new_element->prev = head;
904     new_element->next = next;
905     next->prev = new_element;
906     head->next = new_element;
907 }
908 
adjslot_remove(struct adjslot_list * old)909 static void adjslot_remove(struct adjslot_list *old)
910 {
911     struct adjslot_list *prev = old->prev;
912     struct adjslot_list *next = old->next;
913     next->prev = prev;
914     prev->next = next;
915 }
916 
adjslot_tail(struct adjslot_list * head)917 static struct adjslot_list *adjslot_tail(struct adjslot_list *head) {
918     struct adjslot_list *asl = head->prev;
919 
920     return asl == head ? NULL : asl;
921 }
922 
923 // Should be modified only from the main thread.
proc_slot(struct proc * procp)924 static void proc_slot(struct proc *procp) {
925     int adjslot = ADJTOSLOT(procp->oomadj);
926     std::scoped_lock lock(adjslot_list_lock);
927 
928     adjslot_insert(&procadjslot_list[adjslot], &procp->asl);
929 }
930 
931 // Should be modified only from the main thread.
proc_unslot(struct proc * procp)932 static void proc_unslot(struct proc *procp) {
933     std::scoped_lock lock(adjslot_list_lock);
934 
935     adjslot_remove(&procp->asl);
936 }
937 
proc_insert(struct proc * procp)938 static void proc_insert(struct proc *procp) {
939     int hval = pid_hashfn(procp->pid);
940 
941     procp->pidhash_next = pidhash[hval];
942     pidhash[hval] = procp;
943     proc_slot(procp);
944 }
945 
946 // Can be called only from the main thread.
pid_remove(int pid)947 static int pid_remove(int pid) {
948     int hval = pid_hashfn(pid);
949     struct proc *procp;
950     struct proc *prevp;
951 
952     for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid;
953          procp = procp->pidhash_next)
954             prevp = procp;
955 
956     if (!procp)
957         return -1;
958 
959     if (!prevp)
960         pidhash[hval] = procp->pidhash_next;
961     else
962         prevp->pidhash_next = procp->pidhash_next;
963 
964     proc_unslot(procp);
965     /*
966      * Close pidfd here if we are not waiting for corresponding process to die,
967      * in which case stop_wait_for_proc_kill() will close the pidfd later
968      */
969     if (procp->pidfd >= 0 && procp->pidfd != last_kill_pid_or_fd) {
970         close(procp->pidfd);
971     }
972     free(procp);
973     return 0;
974 }
975 
pid_invalidate(int pid)976 static void pid_invalidate(int pid) {
977     std::shared_lock lock(adjslot_list_lock);
978     struct proc *procp = pid_lookup(pid);
979 
980     if (procp) {
981         procp->valid = false;
982     }
983 }
984 
985 /*
986  * Write a string to a file.
987  * Returns false if the file does not exist.
988  */
writefilestring(const char * path,const char * s,bool err_if_missing)989 static bool writefilestring(const char *path, const char *s,
990                             bool err_if_missing) {
991     int fd = open(path, O_WRONLY | O_CLOEXEC);
992     ssize_t len = strlen(s);
993     ssize_t ret;
994 
995     if (fd < 0) {
996         if (err_if_missing) {
997             ALOGE("Error opening %s; errno=%d", path, errno);
998         }
999         return false;
1000     }
1001 
1002     ret = TEMP_FAILURE_RETRY(write(fd, s, len));
1003     if (ret < 0) {
1004         ALOGE("Error writing %s; errno=%d", path, errno);
1005     } else if (ret < len) {
1006         ALOGE("Short write on %s; length=%zd", path, ret);
1007     }
1008 
1009     close(fd);
1010     return true;
1011 }
1012 
get_time_diff_ms(struct timespec * from,struct timespec * to)1013 static inline long get_time_diff_ms(struct timespec *from,
1014                                     struct timespec *to) {
1015     return (to->tv_sec - from->tv_sec) * (long)MS_PER_SEC +
1016            (to->tv_nsec - from->tv_nsec) / (long)NS_PER_MS;
1017 }
1018 
1019 /* Reads /proc/pid/status into buf. */
read_proc_status(int pid,char * buf,size_t buf_sz)1020 static bool read_proc_status(int pid, char *buf, size_t buf_sz) {
1021     char path[PATH_MAX];
1022     int fd;
1023     ssize_t size;
1024 
1025     snprintf(path, PATH_MAX, "/proc/%d/status", pid);
1026     fd = open(path, O_RDONLY | O_CLOEXEC);
1027     if (fd < 0) {
1028         return false;
1029     }
1030 
1031     size = read_all(fd, buf, buf_sz - 1);
1032     close(fd);
1033     if (size < 0) {
1034         return false;
1035     }
1036     buf[size] = 0;
1037     return true;
1038 }
1039 
1040 /* Looks for tag in buf and parses the first integer */
parse_status_tag(char * buf,const char * tag,int64_t * out)1041 static bool parse_status_tag(char *buf, const char *tag, int64_t *out) {
1042     char *pos = buf;
1043     while (true) {
1044         pos = strstr(pos, tag);
1045         /* Stop if tag not found or found at the line beginning */
1046         if (pos == NULL || pos == buf || pos[-1] == '\n') {
1047             break;
1048         }
1049         pos++;
1050     }
1051 
1052     if (pos == NULL) {
1053         return false;
1054     }
1055 
1056     pos += strlen(tag);
1057     while (*pos == ' ') ++pos;
1058     return parse_int64(pos, out);
1059 }
1060 
proc_get_size(int pid)1061 static int proc_get_size(int pid) {
1062     char path[PATH_MAX];
1063     char line[LINE_MAX];
1064     int fd;
1065     int rss = 0;
1066     int total;
1067     ssize_t ret;
1068 
1069     /* gid containing AID_READPROC required */
1070     snprintf(path, PATH_MAX, "/proc/%d/statm", pid);
1071     fd = open(path, O_RDONLY | O_CLOEXEC);
1072     if (fd == -1)
1073         return -1;
1074 
1075     ret = read_all(fd, line, sizeof(line) - 1);
1076     if (ret < 0) {
1077         close(fd);
1078         return -1;
1079     }
1080     line[ret] = '\0';
1081 
1082     sscanf(line, "%d %d ", &total, &rss);
1083     close(fd);
1084     return rss;
1085 }
1086 
proc_get_name(int pid,char * buf,size_t buf_size)1087 static char *proc_get_name(int pid, char *buf, size_t buf_size) {
1088     char path[PATH_MAX];
1089     int fd;
1090     char *cp;
1091     ssize_t ret;
1092 
1093     /* gid containing AID_READPROC required */
1094     snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid);
1095     fd = open(path, O_RDONLY | O_CLOEXEC);
1096     if (fd == -1) {
1097         return NULL;
1098     }
1099     ret = read_all(fd, buf, buf_size - 1);
1100     close(fd);
1101     if (ret < 0) {
1102         return NULL;
1103     }
1104     buf[ret] = '\0';
1105 
1106     cp = strchr(buf, ' ');
1107     if (cp) {
1108         *cp = '\0';
1109     }
1110 
1111     return buf;
1112 }
1113 
cmd_procprio(LMKD_CTRL_PACKET packet,int field_count,struct ucred * cred)1114 static void cmd_procprio(LMKD_CTRL_PACKET packet, int field_count, struct ucred *cred) {
1115     struct proc *procp;
1116     char path[LINE_MAX];
1117     char val[20];
1118     int soft_limit_mult;
1119     struct lmk_procprio params;
1120     bool is_system_server;
1121     struct passwd *pwdrec;
1122     int64_t tgid;
1123     char buf[PAGE_SIZE];
1124 
1125     lmkd_pack_get_procprio(packet, field_count, &params);
1126 
1127     if (params.oomadj < OOM_SCORE_ADJ_MIN ||
1128         params.oomadj > OOM_SCORE_ADJ_MAX) {
1129         ALOGE("Invalid PROCPRIO oomadj argument %d", params.oomadj);
1130         return;
1131     }
1132 
1133     if (params.ptype < PROC_TYPE_FIRST || params.ptype >= PROC_TYPE_COUNT) {
1134         ALOGE("Invalid PROCPRIO process type argument %d", params.ptype);
1135         return;
1136     }
1137 
1138     /* Check if registered process is a thread group leader */
1139     if (read_proc_status(params.pid, buf, sizeof(buf))) {
1140         if (parse_status_tag(buf, PROC_STATUS_TGID_FIELD, &tgid) && tgid != params.pid) {
1141             ALOGE("Attempt to register a task that is not a thread group leader "
1142                   "(tid %d, tgid %" PRId64 ")", params.pid, tgid);
1143             return;
1144         }
1145     }
1146 
1147     /* gid containing AID_READPROC required */
1148     /* CAP_SYS_RESOURCE required */
1149     /* CAP_DAC_OVERRIDE required */
1150     snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", params.pid);
1151     snprintf(val, sizeof(val), "%d", params.oomadj);
1152     if (!writefilestring(path, val, false)) {
1153         ALOGW("Failed to open %s; errno=%d: process %d might have been killed",
1154               path, errno, params.pid);
1155         /* If this file does not exist the process is dead. */
1156         return;
1157     }
1158 
1159     if (use_inkernel_interface) {
1160         stats_store_taskname(params.pid, proc_get_name(params.pid, path, sizeof(path)));
1161         return;
1162     }
1163 
1164     /* lmkd should not change soft limits for services */
1165     if (params.ptype == PROC_TYPE_APP && per_app_memcg) {
1166         if (params.oomadj >= 900) {
1167             soft_limit_mult = 0;
1168         } else if (params.oomadj >= 800) {
1169             soft_limit_mult = 0;
1170         } else if (params.oomadj >= 700) {
1171             soft_limit_mult = 0;
1172         } else if (params.oomadj >= 600) {
1173             // Launcher should be perceptible, don't kill it.
1174             params.oomadj = 200;
1175             soft_limit_mult = 1;
1176         } else if (params.oomadj >= 500) {
1177             soft_limit_mult = 0;
1178         } else if (params.oomadj >= 400) {
1179             soft_limit_mult = 0;
1180         } else if (params.oomadj >= 300) {
1181             soft_limit_mult = 1;
1182         } else if (params.oomadj >= 200) {
1183             soft_limit_mult = 8;
1184         } else if (params.oomadj >= 100) {
1185             soft_limit_mult = 10;
1186         } else if (params.oomadj >=   0) {
1187             soft_limit_mult = 20;
1188         } else {
1189             // Persistent processes will have a large
1190             // soft limit 512MB.
1191             soft_limit_mult = 64;
1192         }
1193 
1194         std::string path;
1195         if (!CgroupGetAttributePathForTask("MemSoftLimit", params.pid, &path)) {
1196             ALOGE("Querying MemSoftLimit path failed");
1197             return;
1198         }
1199 
1200         snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA);
1201 
1202         /*
1203          * system_server process has no memcg under /dev/memcg/apps but should be
1204          * registered with lmkd. This is the best way so far to identify it.
1205          */
1206         is_system_server = (params.oomadj == SYSTEM_ADJ &&
1207                             (pwdrec = getpwnam("system")) != NULL &&
1208                             params.uid == pwdrec->pw_uid);
1209         writefilestring(path.c_str(), val, !is_system_server);
1210     }
1211 
1212     procp = pid_lookup(params.pid);
1213     if (!procp) {
1214         int pidfd = -1;
1215 
1216         if (pidfd_supported) {
1217             pidfd = TEMP_FAILURE_RETRY(pidfd_open(params.pid, 0));
1218             if (pidfd < 0) {
1219                 ALOGE("pidfd_open for pid %d failed; errno=%d", params.pid, errno);
1220                 return;
1221             }
1222         }
1223 
1224         procp = static_cast<struct proc*>(calloc(1, sizeof(struct proc)));
1225         if (!procp) {
1226             // Oh, the irony.  May need to rebuild our state.
1227             return;
1228         }
1229 
1230         procp->pid = params.pid;
1231         procp->pidfd = pidfd;
1232         procp->uid = params.uid;
1233         procp->reg_pid = cred->pid;
1234         procp->oomadj = params.oomadj;
1235         procp->valid = true;
1236         proc_insert(procp);
1237     } else {
1238         if (!claim_record(procp, cred->pid)) {
1239             char buf[LINE_MAX];
1240             char *taskname = proc_get_name(cred->pid, buf, sizeof(buf));
1241             /* Only registrant of the record can remove it */
1242             ALOGE("%s (%d, %d) attempts to modify a process registered by another client",
1243                 taskname ? taskname : "A process ", cred->uid, cred->pid);
1244             return;
1245         }
1246         proc_unslot(procp);
1247         procp->oomadj = params.oomadj;
1248         proc_slot(procp);
1249     }
1250 }
1251 
cmd_procremove(LMKD_CTRL_PACKET packet,struct ucred * cred)1252 static void cmd_procremove(LMKD_CTRL_PACKET packet, struct ucred *cred) {
1253     struct lmk_procremove params;
1254     struct proc *procp;
1255 
1256     lmkd_pack_get_procremove(packet, &params);
1257 
1258     if (use_inkernel_interface) {
1259         /*
1260          * Perform an extra check before the pid is removed, after which it
1261          * will be impossible for poll_kernel to get the taskname. poll_kernel()
1262          * is potentially a long-running blocking function; however this method
1263          * handles AMS requests but does not block AMS.
1264          */
1265         poll_kernel(kpoll_fd);
1266 
1267         stats_remove_taskname(params.pid);
1268         return;
1269     }
1270 
1271     procp = pid_lookup(params.pid);
1272     if (!procp) {
1273         return;
1274     }
1275 
1276     if (!claim_record(procp, cred->pid)) {
1277         char buf[LINE_MAX];
1278         char *taskname = proc_get_name(cred->pid, buf, sizeof(buf));
1279         /* Only registrant of the record can remove it */
1280         ALOGE("%s (%d, %d) attempts to unregister a process registered by another client",
1281             taskname ? taskname : "A process ", cred->uid, cred->pid);
1282         return;
1283     }
1284 
1285     /*
1286      * WARNING: After pid_remove() procp is freed and can't be used!
1287      * Therefore placed at the end of the function.
1288      */
1289     pid_remove(params.pid);
1290 }
1291 
cmd_procpurge(struct ucred * cred)1292 static void cmd_procpurge(struct ucred *cred) {
1293     int i;
1294     struct proc *procp;
1295     struct proc *next;
1296 
1297     if (use_inkernel_interface) {
1298         stats_purge_tasknames();
1299         return;
1300     }
1301 
1302     for (i = 0; i < PIDHASH_SZ; i++) {
1303         procp = pidhash[i];
1304         while (procp) {
1305             next = procp->pidhash_next;
1306             /* Purge only records created by the requestor */
1307             if (claim_record(procp, cred->pid)) {
1308                 pid_remove(procp->pid);
1309             }
1310             procp = next;
1311         }
1312     }
1313 }
1314 
cmd_subscribe(int dsock_idx,LMKD_CTRL_PACKET packet)1315 static void cmd_subscribe(int dsock_idx, LMKD_CTRL_PACKET packet) {
1316     struct lmk_subscribe params;
1317 
1318     lmkd_pack_get_subscribe(packet, &params);
1319     data_sock[dsock_idx].async_event_mask |= 1 << params.evt_type;
1320 }
1321 
inc_killcnt(int oomadj)1322 static void inc_killcnt(int oomadj) {
1323     int slot = ADJTOSLOT(oomadj);
1324     uint8_t idx = killcnt_idx[slot];
1325 
1326     if (idx == KILLCNT_INVALID_IDX) {
1327         /* index is not assigned for this oomadj */
1328         if (killcnt_free_idx < MAX_DISTINCT_OOM_ADJ) {
1329             killcnt_idx[slot] = killcnt_free_idx;
1330             killcnt[killcnt_free_idx] = 1;
1331             killcnt_free_idx++;
1332         } else {
1333             ALOGW("Number of distinct oomadj levels exceeds %d",
1334                 MAX_DISTINCT_OOM_ADJ);
1335         }
1336     } else {
1337         /*
1338          * wraparound is highly unlikely and is detectable using total
1339          * counter because it has to be equal to the sum of all counters
1340          */
1341         killcnt[idx]++;
1342     }
1343     /* increment total kill counter */
1344     killcnt_total++;
1345 }
1346 
get_killcnt(int min_oomadj,int max_oomadj)1347 static int get_killcnt(int min_oomadj, int max_oomadj) {
1348     int slot;
1349     int count = 0;
1350 
1351     if (min_oomadj > max_oomadj)
1352         return 0;
1353 
1354     /* special case to get total kill count */
1355     if (min_oomadj > OOM_SCORE_ADJ_MAX)
1356         return killcnt_total;
1357 
1358     while (min_oomadj <= max_oomadj &&
1359            (slot = ADJTOSLOT(min_oomadj)) < ADJTOSLOT_COUNT) {
1360         uint8_t idx = killcnt_idx[slot];
1361         if (idx != KILLCNT_INVALID_IDX) {
1362             count += killcnt[idx];
1363         }
1364         min_oomadj++;
1365     }
1366 
1367     return count;
1368 }
1369 
cmd_getkillcnt(LMKD_CTRL_PACKET packet)1370 static int cmd_getkillcnt(LMKD_CTRL_PACKET packet) {
1371     struct lmk_getkillcnt params;
1372 
1373     if (use_inkernel_interface) {
1374         /* kernel driver does not expose this information */
1375         return 0;
1376     }
1377 
1378     lmkd_pack_get_getkillcnt(packet, &params);
1379 
1380     return get_killcnt(params.min_oomadj, params.max_oomadj);
1381 }
1382 
cmd_target(int ntargets,LMKD_CTRL_PACKET packet)1383 static void cmd_target(int ntargets, LMKD_CTRL_PACKET packet) {
1384     int i;
1385     struct lmk_target target;
1386     char minfree_str[PROPERTY_VALUE_MAX];
1387     char *pstr = minfree_str;
1388     char *pend = minfree_str + sizeof(minfree_str);
1389     static struct timespec last_req_tm;
1390     struct timespec curr_tm;
1391 
1392     if (ntargets < 1 || ntargets > (int)lowmem_adj.size()) {
1393         return;
1394     }
1395 
1396     /*
1397      * Ratelimit minfree updates to once per TARGET_UPDATE_MIN_INTERVAL_MS
1398      * to prevent DoS attacks
1399      */
1400     if (clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm) != 0) {
1401         ALOGE("Failed to get current time");
1402         return;
1403     }
1404 
1405     if (get_time_diff_ms(&last_req_tm, &curr_tm) <
1406         TARGET_UPDATE_MIN_INTERVAL_MS) {
1407         ALOGE("Ignoring frequent updated to lmkd limits");
1408         return;
1409     }
1410 
1411     last_req_tm = curr_tm;
1412 
1413     for (i = 0; i < ntargets; i++) {
1414         lmkd_pack_get_target(packet, i, &target);
1415         lowmem_minfree[i] = target.minfree;
1416         lowmem_adj[i] = target.oom_adj_score;
1417 
1418         pstr += snprintf(pstr, pend - pstr, "%d:%d,", target.minfree,
1419             target.oom_adj_score);
1420         if (pstr >= pend) {
1421             /* if no more space in the buffer then terminate the loop */
1422             pstr = pend;
1423             break;
1424         }
1425     }
1426 
1427     lowmem_targets_size = ntargets;
1428 
1429     /* Override the last extra comma */
1430     pstr[-1] = '\0';
1431     property_set("sys.lmk.minfree_levels", minfree_str);
1432 
1433     if (has_inkernel_module) {
1434         char minfreestr[128];
1435         char killpriostr[128];
1436 
1437         minfreestr[0] = '\0';
1438         killpriostr[0] = '\0';
1439 
1440         for (i = 0; i < lowmem_targets_size; i++) {
1441             char val[40];
1442 
1443             if (i) {
1444                 strlcat(minfreestr, ",", sizeof(minfreestr));
1445                 strlcat(killpriostr, ",", sizeof(killpriostr));
1446             }
1447 
1448             snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_minfree[i] : 0);
1449             strlcat(minfreestr, val, sizeof(minfreestr));
1450             snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_adj[i] : 0);
1451             strlcat(killpriostr, val, sizeof(killpriostr));
1452         }
1453 
1454         writefilestring(INKERNEL_MINFREE_PATH, minfreestr, true);
1455         writefilestring(INKERNEL_ADJ_PATH, killpriostr, true);
1456     }
1457 }
1458 
ctrl_command_handler(int dsock_idx)1459 static void ctrl_command_handler(int dsock_idx) {
1460     LMKD_CTRL_PACKET packet;
1461     struct ucred cred;
1462     int len;
1463     enum lmk_cmd cmd;
1464     int nargs;
1465     int targets;
1466     int kill_cnt;
1467     int result;
1468 
1469     len = ctrl_data_read(dsock_idx, (char *)packet, CTRL_PACKET_MAX_SIZE, &cred);
1470     if (len <= 0)
1471         return;
1472 
1473     if (len < (int)sizeof(int)) {
1474         ALOGE("Wrong control socket read length len=%d", len);
1475         return;
1476     }
1477 
1478     cmd = lmkd_pack_get_cmd(packet);
1479     nargs = len / sizeof(int) - 1;
1480     if (nargs < 0)
1481         goto wronglen;
1482 
1483     switch(cmd) {
1484     case LMK_TARGET:
1485         targets = nargs / 2;
1486         if (nargs & 0x1 || targets > (int)lowmem_adj.size()) {
1487             goto wronglen;
1488         }
1489         cmd_target(targets, packet);
1490         break;
1491     case LMK_PROCPRIO:
1492         /* process type field is optional for backward compatibility */
1493         if (nargs < 3 || nargs > 4)
1494             goto wronglen;
1495         cmd_procprio(packet, nargs, &cred);
1496         break;
1497     case LMK_PROCREMOVE:
1498         if (nargs != 1)
1499             goto wronglen;
1500         cmd_procremove(packet, &cred);
1501         break;
1502     case LMK_PROCPURGE:
1503         if (nargs != 0)
1504             goto wronglen;
1505         cmd_procpurge(&cred);
1506         break;
1507     case LMK_GETKILLCNT:
1508         if (nargs != 2)
1509             goto wronglen;
1510         kill_cnt = cmd_getkillcnt(packet);
1511         len = lmkd_pack_set_getkillcnt_repl(packet, kill_cnt);
1512         if (ctrl_data_write(dsock_idx, (char *)packet, len) != len)
1513             return;
1514         break;
1515     case LMK_SUBSCRIBE:
1516         if (nargs != 1)
1517             goto wronglen;
1518         cmd_subscribe(dsock_idx, packet);
1519         break;
1520     case LMK_PROCKILL:
1521         /* This command code is NOT expected at all */
1522         ALOGE("Received unexpected command code %d", cmd);
1523         break;
1524     case LMK_UPDATE_PROPS:
1525         if (nargs != 0)
1526             goto wronglen;
1527         update_props();
1528         if (!use_inkernel_interface) {
1529             /* Reinitialize monitors to apply new settings */
1530             destroy_monitors();
1531             result = init_monitors() ? 0 : -1;
1532         } else {
1533             result = 0;
1534         }
1535         len = lmkd_pack_set_update_props_repl(packet, result);
1536         if (ctrl_data_write(dsock_idx, (char *)packet, len) != len) {
1537             ALOGE("Failed to report operation results");
1538         }
1539         if (!result) {
1540             ALOGI("Properties reinitilized");
1541         } else {
1542             /* New settings can't be supported, crash to be restarted */
1543             ALOGE("New configuration is not supported. Exiting...");
1544             exit(1);
1545         }
1546         break;
1547     default:
1548         ALOGE("Received unknown command code %d", cmd);
1549         return;
1550     }
1551 
1552     return;
1553 
1554 wronglen:
1555     ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len);
1556 }
1557 
ctrl_data_handler(int data,uint32_t events,struct polling_params * poll_params __unused)1558 static void ctrl_data_handler(int data, uint32_t events,
1559                               struct polling_params *poll_params __unused) {
1560     if (events & EPOLLIN) {
1561         ctrl_command_handler(data);
1562     }
1563 }
1564 
get_free_dsock()1565 static int get_free_dsock() {
1566     for (int i = 0; i < MAX_DATA_CONN; i++) {
1567         if (data_sock[i].sock < 0) {
1568             return i;
1569         }
1570     }
1571     return -1;
1572 }
1573 
ctrl_connect_handler(int data __unused,uint32_t events __unused,struct polling_params * poll_params __unused)1574 static void ctrl_connect_handler(int data __unused, uint32_t events __unused,
1575                                  struct polling_params *poll_params __unused) {
1576     struct epoll_event epev;
1577     int free_dscock_idx = get_free_dsock();
1578 
1579     if (free_dscock_idx < 0) {
1580         /*
1581          * Number of data connections exceeded max supported. This should not
1582          * happen but if it does we drop all existing connections and accept
1583          * the new one. This prevents inactive connections from monopolizing
1584          * data socket and if we drop ActivityManager connection it will
1585          * immediately reconnect.
1586          */
1587         for (int i = 0; i < MAX_DATA_CONN; i++) {
1588             ctrl_data_close(i);
1589         }
1590         free_dscock_idx = 0;
1591     }
1592 
1593     data_sock[free_dscock_idx].sock = accept(ctrl_sock.sock, NULL, NULL);
1594     if (data_sock[free_dscock_idx].sock < 0) {
1595         ALOGE("lmkd control socket accept failed; errno=%d", errno);
1596         return;
1597     }
1598 
1599     ALOGI("lmkd data connection established");
1600     /* use data to store data connection idx */
1601     data_sock[free_dscock_idx].handler_info.data = free_dscock_idx;
1602     data_sock[free_dscock_idx].handler_info.handler = ctrl_data_handler;
1603     data_sock[free_dscock_idx].async_event_mask = 0;
1604     epev.events = EPOLLIN;
1605     epev.data.ptr = (void *)&(data_sock[free_dscock_idx].handler_info);
1606     if (epoll_ctl(epollfd, EPOLL_CTL_ADD, data_sock[free_dscock_idx].sock, &epev) == -1) {
1607         ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno);
1608         ctrl_data_close(free_dscock_idx);
1609         return;
1610     }
1611     maxevents++;
1612 }
1613 
1614 /*
1615  * /proc/zoneinfo parsing routines
1616  * Expected file format is:
1617  *
1618  *   Node <node_id>, zone   <zone_name>
1619  *   (
1620  *    per-node stats
1621  *       (<per-node field name> <value>)+
1622  *   )?
1623  *   (pages free     <value>
1624  *       (<per-zone field name> <value>)+
1625  *    pagesets
1626  *       (<unused fields>)*
1627  *   )+
1628  *   ...
1629  */
zoneinfo_parse_protection(char * buf,struct zoneinfo_zone * zone)1630 static void zoneinfo_parse_protection(char *buf, struct zoneinfo_zone *zone) {
1631     int zone_idx;
1632     int64_t max = 0;
1633     char *save_ptr;
1634 
1635     for (buf = strtok_r(buf, "(), ", &save_ptr), zone_idx = 0;
1636          buf && zone_idx < MAX_NR_ZONES;
1637          buf = strtok_r(NULL, "), ", &save_ptr), zone_idx++) {
1638         long long zoneval = strtoll(buf, &buf, 0);
1639         if (zoneval > max) {
1640             max = (zoneval > INT64_MAX) ? INT64_MAX : zoneval;
1641         }
1642         zone->protection[zone_idx] = zoneval;
1643     }
1644     zone->max_protection = max;
1645 }
1646 
zoneinfo_parse_zone(char ** buf,struct zoneinfo_zone * zone)1647 static int zoneinfo_parse_zone(char **buf, struct zoneinfo_zone *zone) {
1648     for (char *line = strtok_r(NULL, "\n", buf); line;
1649          line = strtok_r(NULL, "\n", buf)) {
1650         char *cp;
1651         char *ap;
1652         char *save_ptr;
1653         int64_t val;
1654         int field_idx;
1655         enum field_match_result match_res;
1656 
1657         cp = strtok_r(line, " ", &save_ptr);
1658         if (!cp) {
1659             return false;
1660         }
1661 
1662         field_idx = find_field(cp, zoneinfo_zone_spec_field_names, ZI_ZONE_SPEC_FIELD_COUNT);
1663         if (field_idx >= 0) {
1664             /* special field */
1665             if (field_idx == ZI_ZONE_SPEC_PAGESETS) {
1666                 /* no mode fields we are interested in */
1667                 return true;
1668             }
1669 
1670             /* protection field */
1671             ap = strtok_r(NULL, ")", &save_ptr);
1672             if (ap) {
1673                 zoneinfo_parse_protection(ap, zone);
1674             }
1675             continue;
1676         }
1677 
1678         ap = strtok_r(NULL, " ", &save_ptr);
1679         if (!ap) {
1680             continue;
1681         }
1682 
1683         match_res = match_field(cp, ap, zoneinfo_zone_field_names, ZI_ZONE_FIELD_COUNT,
1684             &val, &field_idx);
1685         if (match_res == PARSE_FAIL) {
1686             return false;
1687         }
1688         if (match_res == PARSE_SUCCESS) {
1689             zone->fields.arr[field_idx] = val;
1690         }
1691         if (field_idx == ZI_ZONE_PRESENT && val == 0) {
1692             /* zone is not populated, stop parsing it */
1693             return true;
1694         }
1695     }
1696     return false;
1697 }
1698 
zoneinfo_parse_node(char ** buf,struct zoneinfo_node * node)1699 static int zoneinfo_parse_node(char **buf, struct zoneinfo_node *node) {
1700     int fields_to_match = ZI_NODE_FIELD_COUNT;
1701 
1702     for (char *line = strtok_r(NULL, "\n", buf); line;
1703          line = strtok_r(NULL, "\n", buf)) {
1704         char *cp;
1705         char *ap;
1706         char *save_ptr;
1707         int64_t val;
1708         int field_idx;
1709         enum field_match_result match_res;
1710 
1711         cp = strtok_r(line, " ", &save_ptr);
1712         if (!cp) {
1713             return false;
1714         }
1715 
1716         ap = strtok_r(NULL, " ", &save_ptr);
1717         if (!ap) {
1718             return false;
1719         }
1720 
1721         match_res = match_field(cp, ap, zoneinfo_node_field_names, ZI_NODE_FIELD_COUNT,
1722             &val, &field_idx);
1723         if (match_res == PARSE_FAIL) {
1724             return false;
1725         }
1726         if (match_res == PARSE_SUCCESS) {
1727             node->fields.arr[field_idx] = val;
1728             fields_to_match--;
1729             if (!fields_to_match) {
1730                 return true;
1731             }
1732         }
1733     }
1734     return false;
1735 }
1736 
zoneinfo_parse(struct zoneinfo * zi)1737 static int zoneinfo_parse(struct zoneinfo *zi) {
1738     static struct reread_data file_data = {
1739         .filename = ZONEINFO_PATH,
1740         .fd = -1,
1741     };
1742     char *buf;
1743     char *save_ptr;
1744     char *line;
1745     char zone_name[LINE_MAX + 1];
1746     struct zoneinfo_node *node = NULL;
1747     int node_idx = 0;
1748     int zone_idx = 0;
1749 
1750     memset(zi, 0, sizeof(struct zoneinfo));
1751 
1752     if ((buf = reread_file(&file_data)) == NULL) {
1753         return -1;
1754     }
1755 
1756     for (line = strtok_r(buf, "\n", &save_ptr); line;
1757          line = strtok_r(NULL, "\n", &save_ptr)) {
1758         int node_id;
1759         if (sscanf(line, "Node %d, zone %" STRINGIFY(LINE_MAX) "s", &node_id, zone_name) == 2) {
1760             if (!node || node->id != node_id) {
1761                 /* new node is found */
1762                 if (node) {
1763                     node->zone_count = zone_idx + 1;
1764                     node_idx++;
1765                     if (node_idx == MAX_NR_NODES) {
1766                         /* max node count exceeded */
1767                         ALOGE("%s parse error", file_data.filename);
1768                         return -1;
1769                     }
1770                 }
1771                 node = &zi->nodes[node_idx];
1772                 node->id = node_id;
1773                 zone_idx = 0;
1774                 if (!zoneinfo_parse_node(&save_ptr, node)) {
1775                     ALOGE("%s parse error", file_data.filename);
1776                     return -1;
1777                 }
1778             } else {
1779                 /* new zone is found */
1780                 zone_idx++;
1781             }
1782             if (!zoneinfo_parse_zone(&save_ptr, &node->zones[zone_idx])) {
1783                 ALOGE("%s parse error", file_data.filename);
1784                 return -1;
1785             }
1786         }
1787     }
1788     if (!node) {
1789         ALOGE("%s parse error", file_data.filename);
1790         return -1;
1791     }
1792     node->zone_count = zone_idx + 1;
1793     zi->node_count = node_idx + 1;
1794 
1795     /* calculate totals fields */
1796     for (node_idx = 0; node_idx < zi->node_count; node_idx++) {
1797         node = &zi->nodes[node_idx];
1798         for (zone_idx = 0; zone_idx < node->zone_count; zone_idx++) {
1799             struct zoneinfo_zone *zone = &zi->nodes[node_idx].zones[zone_idx];
1800             zi->totalreserve_pages += zone->max_protection + zone->fields.field.high;
1801         }
1802         zi->total_inactive_file += node->fields.field.nr_inactive_file;
1803         zi->total_active_file += node->fields.field.nr_active_file;
1804     }
1805     return 0;
1806 }
1807 
1808 /* /proc/meminfo parsing routines */
meminfo_parse_line(char * line,union meminfo * mi)1809 static bool meminfo_parse_line(char *line, union meminfo *mi) {
1810     char *cp = line;
1811     char *ap;
1812     char *save_ptr;
1813     int64_t val;
1814     int field_idx;
1815     enum field_match_result match_res;
1816 
1817     cp = strtok_r(line, " ", &save_ptr);
1818     if (!cp) {
1819         return false;
1820     }
1821 
1822     ap = strtok_r(NULL, " ", &save_ptr);
1823     if (!ap) {
1824         return false;
1825     }
1826 
1827     match_res = match_field(cp, ap, meminfo_field_names, MI_FIELD_COUNT,
1828         &val, &field_idx);
1829     if (match_res == PARSE_SUCCESS) {
1830         mi->arr[field_idx] = val / page_k;
1831     }
1832     return (match_res != PARSE_FAIL);
1833 }
1834 
read_gpu_total_kb()1835 static int64_t read_gpu_total_kb() {
1836     static int fd = android::bpf::bpfFdGet(
1837             "/sys/fs/bpf/map_gpu_mem_gpu_mem_total_map", BPF_F_RDONLY);
1838     static constexpr uint64_t kBpfKeyGpuTotalUsage = 0;
1839     uint64_t value;
1840 
1841     if (fd < 0) {
1842         return 0;
1843     }
1844 
1845     return android::bpf::findMapEntry(fd, &kBpfKeyGpuTotalUsage, &value)
1846             ? 0
1847             : (int32_t)(value / 1024);
1848 }
1849 
meminfo_parse(union meminfo * mi)1850 static int meminfo_parse(union meminfo *mi) {
1851     static struct reread_data file_data = {
1852         .filename = MEMINFO_PATH,
1853         .fd = -1,
1854     };
1855     char *buf;
1856     char *save_ptr;
1857     char *line;
1858 
1859     memset(mi, 0, sizeof(union meminfo));
1860 
1861     if ((buf = reread_file(&file_data)) == NULL) {
1862         return -1;
1863     }
1864 
1865     for (line = strtok_r(buf, "\n", &save_ptr); line;
1866          line = strtok_r(NULL, "\n", &save_ptr)) {
1867         if (!meminfo_parse_line(line, mi)) {
1868             ALOGE("%s parse error", file_data.filename);
1869             return -1;
1870         }
1871     }
1872     mi->field.nr_file_pages = mi->field.cached + mi->field.swap_cached +
1873         mi->field.buffers;
1874     mi->field.total_gpu_kb = read_gpu_total_kb();
1875 
1876     return 0;
1877 }
1878 
1879 /* /proc/vmstat parsing routines */
vmstat_parse_line(char * line,union vmstat * vs)1880 static bool vmstat_parse_line(char *line, union vmstat *vs) {
1881     char *cp;
1882     char *ap;
1883     char *save_ptr;
1884     int64_t val;
1885     int field_idx;
1886     enum field_match_result match_res;
1887 
1888     cp = strtok_r(line, " ", &save_ptr);
1889     if (!cp) {
1890         return false;
1891     }
1892 
1893     ap = strtok_r(NULL, " ", &save_ptr);
1894     if (!ap) {
1895         return false;
1896     }
1897 
1898     match_res = match_field(cp, ap, vmstat_field_names, VS_FIELD_COUNT,
1899         &val, &field_idx);
1900     if (match_res == PARSE_SUCCESS) {
1901         vs->arr[field_idx] = val;
1902     }
1903     return (match_res != PARSE_FAIL);
1904 }
1905 
vmstat_parse(union vmstat * vs)1906 static int vmstat_parse(union vmstat *vs) {
1907     static struct reread_data file_data = {
1908         .filename = VMSTAT_PATH,
1909         .fd = -1,
1910     };
1911     char *buf;
1912     char *save_ptr;
1913     char *line;
1914 
1915     memset(vs, 0, sizeof(union vmstat));
1916 
1917     if ((buf = reread_file(&file_data)) == NULL) {
1918         return -1;
1919     }
1920 
1921     for (line = strtok_r(buf, "\n", &save_ptr); line;
1922          line = strtok_r(NULL, "\n", &save_ptr)) {
1923         if (!vmstat_parse_line(line, vs)) {
1924             ALOGE("%s parse error", file_data.filename);
1925             return -1;
1926         }
1927     }
1928 
1929     return 0;
1930 }
1931 
psi_parse(struct reread_data * file_data,struct psi_stats stats[],bool full)1932 static int psi_parse(struct reread_data *file_data, struct psi_stats stats[], bool full) {
1933     char *buf;
1934     char *save_ptr;
1935     char *line;
1936 
1937     if ((buf = reread_file(file_data)) == NULL) {
1938         return -1;
1939     }
1940 
1941     line = strtok_r(buf, "\n", &save_ptr);
1942     if (parse_psi_line(line, PSI_SOME, stats)) {
1943         return -1;
1944     }
1945     if (full) {
1946         line = strtok_r(NULL, "\n", &save_ptr);
1947         if (parse_psi_line(line, PSI_FULL, stats)) {
1948             return -1;
1949         }
1950     }
1951 
1952     return 0;
1953 }
1954 
psi_parse_mem(struct psi_data * psi_data)1955 static int psi_parse_mem(struct psi_data *psi_data) {
1956     static struct reread_data file_data = {
1957         .filename = PSI_PATH_MEMORY,
1958         .fd = -1,
1959     };
1960     return psi_parse(&file_data, psi_data->mem_stats, true);
1961 }
1962 
psi_parse_io(struct psi_data * psi_data)1963 static int psi_parse_io(struct psi_data *psi_data) {
1964     static struct reread_data file_data = {
1965         .filename = PSI_PATH_IO,
1966         .fd = -1,
1967     };
1968     return psi_parse(&file_data, psi_data->io_stats, true);
1969 }
1970 
psi_parse_cpu(struct psi_data * psi_data)1971 static int psi_parse_cpu(struct psi_data *psi_data) {
1972     static struct reread_data file_data = {
1973         .filename = PSI_PATH_CPU,
1974         .fd = -1,
1975     };
1976     return psi_parse(&file_data, psi_data->cpu_stats, false);
1977 }
1978 
1979 enum wakeup_reason {
1980     Event,
1981     Polling
1982 };
1983 
1984 struct wakeup_info {
1985     struct timespec wakeup_tm;
1986     struct timespec prev_wakeup_tm;
1987     struct timespec last_event_tm;
1988     int wakeups_since_event;
1989     int skipped_wakeups;
1990 };
1991 
1992 /*
1993  * After the initial memory pressure event is received lmkd schedules periodic wakeups to check
1994  * the memory conditions and kill if needed (polling). This is done because pressure events are
1995  * rate-limited and memory conditions can change in between events. Therefore after the initial
1996  * event there might be multiple wakeups. This function records the wakeup information such as the
1997  * timestamps of the last event and the last wakeup, the number of wakeups since the last event
1998  * and how many of those wakeups were skipped (some wakeups are skipped if previously killed
1999  * process is still freeing its memory).
2000  */
record_wakeup_time(struct timespec * tm,enum wakeup_reason reason,struct wakeup_info * wi)2001 static void record_wakeup_time(struct timespec *tm, enum wakeup_reason reason,
2002                                struct wakeup_info *wi) {
2003     wi->prev_wakeup_tm = wi->wakeup_tm;
2004     wi->wakeup_tm = *tm;
2005     if (reason == Event) {
2006         wi->last_event_tm = *tm;
2007         wi->wakeups_since_event = 0;
2008         wi->skipped_wakeups = 0;
2009     } else {
2010         wi->wakeups_since_event++;
2011     }
2012 }
2013 
2014 struct kill_info {
2015     enum kill_reasons kill_reason;
2016     const char *kill_desc;
2017     int thrashing;
2018     int max_thrashing;
2019 };
2020 
killinfo_log(struct proc * procp,int min_oom_score,int rss_kb,int swap_kb,struct kill_info * ki,union meminfo * mi,struct wakeup_info * wi,struct timespec * tm,struct psi_data * pd)2021 static void killinfo_log(struct proc* procp, int min_oom_score, int rss_kb,
2022                          int swap_kb, struct kill_info *ki, union meminfo *mi,
2023                          struct wakeup_info *wi, struct timespec *tm, struct psi_data *pd) {
2024     /* log process information */
2025     android_log_write_int32(ctx, procp->pid);
2026     android_log_write_int32(ctx, procp->uid);
2027     android_log_write_int32(ctx, procp->oomadj);
2028     android_log_write_int32(ctx, min_oom_score);
2029     android_log_write_int32(ctx, std::min(rss_kb, (int)INT32_MAX));
2030     android_log_write_int32(ctx, ki ? ki->kill_reason : NONE);
2031 
2032     /* log meminfo fields */
2033     for (int field_idx = 0; field_idx < MI_FIELD_COUNT; field_idx++) {
2034         android_log_write_int32(ctx,
2035                                 mi ? std::min(mi->arr[field_idx] * page_k, (int64_t)INT32_MAX) : 0);
2036     }
2037 
2038     /* log lmkd wakeup information */
2039     if (wi) {
2040         android_log_write_int32(ctx, (int32_t)get_time_diff_ms(&wi->last_event_tm, tm));
2041         android_log_write_int32(ctx, (int32_t)get_time_diff_ms(&wi->prev_wakeup_tm, tm));
2042         android_log_write_int32(ctx, wi->wakeups_since_event);
2043         android_log_write_int32(ctx, wi->skipped_wakeups);
2044     } else {
2045         android_log_write_int32(ctx, 0);
2046         android_log_write_int32(ctx, 0);
2047         android_log_write_int32(ctx, 0);
2048         android_log_write_int32(ctx, 0);
2049     }
2050 
2051     android_log_write_int32(ctx, std::min(swap_kb, (int)INT32_MAX));
2052     android_log_write_int32(ctx, mi ? (int32_t)mi->field.total_gpu_kb : 0);
2053     if (ki) {
2054         android_log_write_int32(ctx, ki->thrashing);
2055         android_log_write_int32(ctx, ki->max_thrashing);
2056     } else {
2057         android_log_write_int32(ctx, 0);
2058         android_log_write_int32(ctx, 0);
2059     }
2060 
2061     if (pd) {
2062         android_log_write_float32(ctx, pd->mem_stats[PSI_SOME].avg10);
2063         android_log_write_float32(ctx, pd->mem_stats[PSI_FULL].avg10);
2064         android_log_write_float32(ctx, pd->io_stats[PSI_SOME].avg10);
2065         android_log_write_float32(ctx, pd->io_stats[PSI_FULL].avg10);
2066         android_log_write_float32(ctx, pd->cpu_stats[PSI_SOME].avg10);
2067     } else {
2068         for (int i = 0; i < 5; i++) {
2069             android_log_write_float32(ctx, 0);
2070         }
2071     }
2072 
2073     android_log_write_list(ctx, LOG_ID_EVENTS);
2074     android_log_reset(ctx);
2075 }
2076 
2077 // Note: returned entry is only an anchor and does not hold a valid process info.
2078 // When called from a non-main thread, adjslot_list_lock read lock should be taken.
proc_adj_head(int oomadj)2079 static struct proc *proc_adj_head(int oomadj) {
2080     return (struct proc *)&procadjslot_list[ADJTOSLOT(oomadj)];
2081 }
2082 
2083 // When called from a non-main thread, adjslot_list_lock read lock should be taken.
proc_adj_tail(int oomadj)2084 static struct proc *proc_adj_tail(int oomadj) {
2085     return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]);
2086 }
2087 
2088 // When called from a non-main thread, adjslot_list_lock read lock should be taken.
proc_adj_prev(int oomadj,int pid)2089 static struct proc *proc_adj_prev(int oomadj, int pid) {
2090     struct adjslot_list *head = &procadjslot_list[ADJTOSLOT(oomadj)];
2091     struct adjslot_list *curr = adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]);
2092 
2093     while (curr != head) {
2094         if (((struct proc *)curr)->pid == pid) {
2095             return (struct proc *)curr->prev;
2096         }
2097         curr = curr->prev;
2098     }
2099 
2100     return NULL;
2101 }
2102 
2103 // Can be called only from the main thread.
proc_get_heaviest(int oomadj)2104 static struct proc *proc_get_heaviest(int oomadj) {
2105     struct adjslot_list *head = &procadjslot_list[ADJTOSLOT(oomadj)];
2106     struct adjslot_list *curr = head->next;
2107     struct proc *maxprocp = NULL;
2108     int maxsize = 0;
2109     while (curr != head) {
2110         int pid = ((struct proc *)curr)->pid;
2111         int tasksize = proc_get_size(pid);
2112         if (tasksize < 0) {
2113             struct adjslot_list *next = curr->next;
2114             pid_remove(pid);
2115             curr = next;
2116         } else {
2117             if (tasksize > maxsize) {
2118                 maxsize = tasksize;
2119                 maxprocp = (struct proc *)curr;
2120             }
2121             curr = curr->next;
2122         }
2123     }
2124     return maxprocp;
2125 }
2126 
find_victim(int oom_score,int prev_pid,struct proc & target_proc)2127 static bool find_victim(int oom_score, int prev_pid, struct proc &target_proc) {
2128     struct proc *procp;
2129     std::shared_lock lock(adjslot_list_lock);
2130 
2131     if (!prev_pid) {
2132         procp = proc_adj_tail(oom_score);
2133     } else {
2134         procp = proc_adj_prev(oom_score, prev_pid);
2135         if (!procp) {
2136             // pid was removed, restart at the tail
2137             procp = proc_adj_tail(oom_score);
2138         }
2139     }
2140 
2141     // the list is empty at this oom_score or we looped through it
2142     if (!procp || procp == proc_adj_head(oom_score)) {
2143         return false;
2144     }
2145 
2146     // make a copy because original might be destroyed after adjslot_list_lock is released
2147     target_proc = *procp;
2148 
2149     return true;
2150 }
2151 
watchdog_callback()2152 static void watchdog_callback() {
2153     int prev_pid = 0;
2154 
2155     ALOGW("lmkd watchdog timed out!");
2156     for (int oom_score = OOM_SCORE_ADJ_MAX; oom_score >= 0;) {
2157         struct proc target;
2158 
2159         if (!find_victim(oom_score, prev_pid, target)) {
2160             oom_score--;
2161             prev_pid = 0;
2162             continue;
2163         }
2164 
2165         if (target.valid && reaper.kill({ target.pidfd, target.pid, target.uid }, true) == 0) {
2166             ALOGW("lmkd watchdog killed process %d, oom_score_adj %d", target.pid, oom_score);
2167             killinfo_log(&target, 0, 0, 0, NULL, NULL, NULL, NULL, NULL);
2168             // Can't call pid_remove() from non-main thread, therefore just invalidate the record
2169             pid_invalidate(target.pid);
2170             break;
2171         }
2172         prev_pid = target.pid;
2173     }
2174 }
2175 
2176 static Watchdog watchdog(WATCHDOG_TIMEOUT_SEC, watchdog_callback);
2177 
is_kill_pending(void)2178 static bool is_kill_pending(void) {
2179     char buf[24];
2180 
2181     if (last_kill_pid_or_fd < 0) {
2182         return false;
2183     }
2184 
2185     if (pidfd_supported) {
2186         return true;
2187     }
2188 
2189     /* when pidfd is not supported base the decision on /proc/<pid> existence */
2190     snprintf(buf, sizeof(buf), "/proc/%d/", last_kill_pid_or_fd);
2191     if (access(buf, F_OK) == 0) {
2192         return true;
2193     }
2194 
2195     return false;
2196 }
2197 
is_waiting_for_kill(void)2198 static bool is_waiting_for_kill(void) {
2199     return pidfd_supported && last_kill_pid_or_fd >= 0;
2200 }
2201 
stop_wait_for_proc_kill(bool finished)2202 static void stop_wait_for_proc_kill(bool finished) {
2203     struct epoll_event epev;
2204 
2205     if (last_kill_pid_or_fd < 0) {
2206         return;
2207     }
2208 
2209     if (debug_process_killing) {
2210         struct timespec curr_tm;
2211 
2212         if (clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm) != 0) {
2213             /*
2214              * curr_tm is used here merely to report kill duration, so this failure is not fatal.
2215              * Log an error and continue.
2216              */
2217             ALOGE("Failed to get current time");
2218         }
2219 
2220         if (finished) {
2221             ALOGI("Process got killed in %ldms",
2222                 get_time_diff_ms(&last_kill_tm, &curr_tm));
2223         } else {
2224             ALOGI("Stop waiting for process kill after %ldms",
2225                 get_time_diff_ms(&last_kill_tm, &curr_tm));
2226         }
2227     }
2228 
2229     if (pidfd_supported) {
2230         /* unregister fd */
2231         if (epoll_ctl(epollfd, EPOLL_CTL_DEL, last_kill_pid_or_fd, &epev)) {
2232             // Log an error and keep going
2233             ALOGE("epoll_ctl for last killed process failed; errno=%d", errno);
2234         }
2235         maxevents--;
2236         close(last_kill_pid_or_fd);
2237     }
2238 
2239     last_kill_pid_or_fd = -1;
2240 }
2241 
kill_done_handler(int data __unused,uint32_t events __unused,struct polling_params * poll_params)2242 static void kill_done_handler(int data __unused, uint32_t events __unused,
2243                               struct polling_params *poll_params) {
2244     stop_wait_for_proc_kill(true);
2245     poll_params->update = POLLING_RESUME;
2246 }
2247 
kill_fail_handler(int data __unused,uint32_t events __unused,struct polling_params * poll_params)2248 static void kill_fail_handler(int data __unused, uint32_t events __unused,
2249                               struct polling_params *poll_params) {
2250     int pid;
2251 
2252     // Extract pid from the communication pipe. Clearing the pipe this way allows further
2253     // epoll_wait calls to sleep until the next event.
2254     if (TEMP_FAILURE_RETRY(read(reaper_comm_fd[0], &pid, sizeof(pid))) != sizeof(pid)) {
2255         ALOGE("thread communication read failed: %s", strerror(errno));
2256     }
2257     stop_wait_for_proc_kill(false);
2258     poll_params->update = POLLING_RESUME;
2259 }
2260 
start_wait_for_proc_kill(int pid_or_fd)2261 static void start_wait_for_proc_kill(int pid_or_fd) {
2262     static struct event_handler_info kill_done_hinfo = { 0, kill_done_handler };
2263     struct epoll_event epev;
2264 
2265     if (last_kill_pid_or_fd >= 0) {
2266         /* Should not happen but if it does we should stop previous wait */
2267         ALOGE("Attempt to wait for a kill while another wait is in progress");
2268         stop_wait_for_proc_kill(false);
2269     }
2270 
2271     last_kill_pid_or_fd = pid_or_fd;
2272 
2273     if (!pidfd_supported) {
2274         /* If pidfd is not supported just store PID and exit */
2275         return;
2276     }
2277 
2278     epev.events = EPOLLIN;
2279     epev.data.ptr = (void *)&kill_done_hinfo;
2280     if (epoll_ctl(epollfd, EPOLL_CTL_ADD, last_kill_pid_or_fd, &epev) != 0) {
2281         ALOGE("epoll_ctl for last kill failed; errno=%d", errno);
2282         close(last_kill_pid_or_fd);
2283         last_kill_pid_or_fd = -1;
2284         return;
2285     }
2286     maxevents++;
2287 }
2288 
2289 /* Kill one process specified by procp.  Returns the size (in pages) of the process killed */
kill_one_process(struct proc * procp,int min_oom_score,struct kill_info * ki,union meminfo * mi,struct wakeup_info * wi,struct timespec * tm,struct psi_data * pd)2290 static int kill_one_process(struct proc* procp, int min_oom_score, struct kill_info *ki,
2291                             union meminfo *mi, struct wakeup_info *wi, struct timespec *tm,
2292                             struct psi_data *pd) {
2293     int pid = procp->pid;
2294     int pidfd = procp->pidfd;
2295     uid_t uid = procp->uid;
2296     char *taskname;
2297     int kill_result;
2298     int result = -1;
2299     struct memory_stat *mem_st;
2300     struct kill_stat kill_st;
2301     int64_t tgid;
2302     int64_t rss_kb;
2303     int64_t swap_kb;
2304     char buf[PAGE_SIZE];
2305     char desc[LINE_MAX];
2306 
2307     if (!procp->valid || !read_proc_status(pid, buf, sizeof(buf))) {
2308         goto out;
2309     }
2310     if (!parse_status_tag(buf, PROC_STATUS_TGID_FIELD, &tgid)) {
2311         ALOGE("Unable to parse tgid from /proc/%d/status", pid);
2312         goto out;
2313     }
2314     if (tgid != pid) {
2315         ALOGE("Possible pid reuse detected (pid %d, tgid %" PRId64 ")!", pid, tgid);
2316         goto out;
2317     }
2318     // Zombie processes will not have RSS / Swap fields.
2319     if (!parse_status_tag(buf, PROC_STATUS_RSS_FIELD, &rss_kb)) {
2320         goto out;
2321     }
2322     if (!parse_status_tag(buf, PROC_STATUS_SWAP_FIELD, &swap_kb)) {
2323         goto out;
2324     }
2325 
2326     taskname = proc_get_name(pid, buf, sizeof(buf));
2327     // taskname will point inside buf, do not reuse buf onwards.
2328     if (!taskname) {
2329         goto out;
2330     }
2331 
2332     mem_st = stats_read_memory_stat(per_app_memcg, pid, uid, rss_kb * 1024, swap_kb * 1024);
2333 
2334     snprintf(desc, sizeof(desc), "lmk,%d,%d,%d,%d,%d", pid, ki ? (int)ki->kill_reason : -1,
2335              procp->oomadj, min_oom_score, ki ? ki->max_thrashing : -1);
2336 
2337     trace_kill_start(pid, desc);
2338 
2339     start_wait_for_proc_kill(pidfd < 0 ? pid : pidfd);
2340     kill_result = reaper.kill({ pidfd, pid, uid }, false);
2341 
2342     trace_kill_end();
2343 
2344     if (kill_result) {
2345         stop_wait_for_proc_kill(false);
2346         ALOGE("kill(%d): errno=%d", pid, errno);
2347         /* Delete process record even when we fail to kill so that we don't get stuck on it */
2348         goto out;
2349     }
2350 
2351     last_kill_tm = *tm;
2352 
2353     inc_killcnt(procp->oomadj);
2354 
2355     if (ki) {
2356         kill_st.kill_reason = ki->kill_reason;
2357         kill_st.thrashing = ki->thrashing;
2358         kill_st.max_thrashing = ki->max_thrashing;
2359         ALOGI("Kill '%s' (%d), uid %d, oom_score_adj %d to free %" PRId64 "kB rss, %" PRId64
2360               "kB swap; reason: %s", taskname, pid, uid, procp->oomadj, rss_kb, swap_kb,
2361               ki->kill_desc);
2362     } else {
2363         kill_st.kill_reason = NONE;
2364         kill_st.thrashing = 0;
2365         kill_st.max_thrashing = 0;
2366         ALOGI("Kill '%s' (%d), uid %d, oom_score_adj %d to free %" PRId64 "kB rss, %" PRId64
2367               "kb swap", taskname, pid, uid, procp->oomadj, rss_kb, swap_kb);
2368     }
2369     killinfo_log(procp, min_oom_score, rss_kb, swap_kb, ki, mi, wi, tm, pd);
2370 
2371     kill_st.uid = static_cast<int32_t>(uid);
2372     kill_st.taskname = taskname;
2373     kill_st.oom_score = procp->oomadj;
2374     kill_st.min_oom_score = min_oom_score;
2375     kill_st.free_mem_kb = mi->field.nr_free_pages * page_k;
2376     kill_st.free_swap_kb = mi->field.free_swap * page_k;
2377     stats_write_lmk_kill_occurred(&kill_st, mem_st);
2378 
2379     ctrl_data_write_lmk_kill_occurred((pid_t)pid, uid);
2380 
2381     result = rss_kb / page_k;
2382 
2383 out:
2384     /*
2385      * WARNING: After pid_remove() procp is freed and can't be used!
2386      * Therefore placed at the end of the function.
2387      */
2388     pid_remove(pid);
2389     return result;
2390 }
2391 
2392 /*
2393  * Find one process to kill at or above the given oom_score_adj level.
2394  * Returns size of the killed process.
2395  */
find_and_kill_process(int min_score_adj,struct kill_info * ki,union meminfo * mi,struct wakeup_info * wi,struct timespec * tm,struct psi_data * pd)2396 static int find_and_kill_process(int min_score_adj, struct kill_info *ki, union meminfo *mi,
2397                                  struct wakeup_info *wi, struct timespec *tm,
2398                                  struct psi_data *pd) {
2399     int i;
2400     int killed_size = 0;
2401     bool lmk_state_change_start = false;
2402     bool choose_heaviest_task = kill_heaviest_task;
2403 
2404     for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) {
2405         struct proc *procp;
2406 
2407         if (!choose_heaviest_task && i <= PERCEPTIBLE_APP_ADJ) {
2408             /*
2409              * If we have to choose a perceptible process, choose the heaviest one to
2410              * hopefully minimize the number of victims.
2411              */
2412             choose_heaviest_task = true;
2413         }
2414 
2415         while (true) {
2416             procp = choose_heaviest_task ?
2417                 proc_get_heaviest(i) : proc_adj_tail(i);
2418 
2419             if (!procp)
2420                 break;
2421 
2422             killed_size = kill_one_process(procp, min_score_adj, ki, mi, wi, tm, pd);
2423             if (killed_size >= 0) {
2424                 if (!lmk_state_change_start) {
2425                     lmk_state_change_start = true;
2426                     stats_write_lmk_state_changed(STATE_START);
2427                 }
2428                 break;
2429             }
2430         }
2431         if (killed_size) {
2432             break;
2433         }
2434     }
2435 
2436     if (lmk_state_change_start) {
2437         stats_write_lmk_state_changed(STATE_STOP);
2438     }
2439 
2440     return killed_size;
2441 }
2442 
get_memory_usage(struct reread_data * file_data)2443 static int64_t get_memory_usage(struct reread_data *file_data) {
2444     int64_t mem_usage;
2445     char *buf;
2446 
2447     if ((buf = reread_file(file_data)) == NULL) {
2448         return -1;
2449     }
2450 
2451     if (!parse_int64(buf, &mem_usage)) {
2452         ALOGE("%s parse error", file_data->filename);
2453         return -1;
2454     }
2455     if (mem_usage == 0) {
2456         ALOGE("No memory!");
2457         return -1;
2458     }
2459     return mem_usage;
2460 }
2461 
record_low_pressure_levels(union meminfo * mi)2462 void record_low_pressure_levels(union meminfo *mi) {
2463     if (low_pressure_mem.min_nr_free_pages == -1 ||
2464         low_pressure_mem.min_nr_free_pages > mi->field.nr_free_pages) {
2465         if (debug_process_killing) {
2466             ALOGI("Low pressure min memory update from %" PRId64 " to %" PRId64,
2467                 low_pressure_mem.min_nr_free_pages, mi->field.nr_free_pages);
2468         }
2469         low_pressure_mem.min_nr_free_pages = mi->field.nr_free_pages;
2470     }
2471     /*
2472      * Free memory at low vmpressure events occasionally gets spikes,
2473      * possibly a stale low vmpressure event with memory already
2474      * freed up (no memory pressure should have been reported).
2475      * Ignore large jumps in max_nr_free_pages that would mess up our stats.
2476      */
2477     if (low_pressure_mem.max_nr_free_pages == -1 ||
2478         (low_pressure_mem.max_nr_free_pages < mi->field.nr_free_pages &&
2479          mi->field.nr_free_pages - low_pressure_mem.max_nr_free_pages <
2480          low_pressure_mem.max_nr_free_pages * 0.1)) {
2481         if (debug_process_killing) {
2482             ALOGI("Low pressure max memory update from %" PRId64 " to %" PRId64,
2483                 low_pressure_mem.max_nr_free_pages, mi->field.nr_free_pages);
2484         }
2485         low_pressure_mem.max_nr_free_pages = mi->field.nr_free_pages;
2486     }
2487 }
2488 
upgrade_level(enum vmpressure_level level)2489 enum vmpressure_level upgrade_level(enum vmpressure_level level) {
2490     return (enum vmpressure_level)((level < VMPRESS_LEVEL_CRITICAL) ?
2491         level + 1 : level);
2492 }
2493 
downgrade_level(enum vmpressure_level level)2494 enum vmpressure_level downgrade_level(enum vmpressure_level level) {
2495     return (enum vmpressure_level)((level > VMPRESS_LEVEL_LOW) ?
2496         level - 1 : level);
2497 }
2498 
2499 enum zone_watermark {
2500     WMARK_MIN = 0,
2501     WMARK_LOW,
2502     WMARK_HIGH,
2503     WMARK_NONE
2504 };
2505 
2506 struct zone_watermarks {
2507     long high_wmark;
2508     long low_wmark;
2509     long min_wmark;
2510 };
2511 
2512 /*
2513  * Returns lowest breached watermark or WMARK_NONE.
2514  */
get_lowest_watermark(union meminfo * mi,struct zone_watermarks * watermarks)2515 static enum zone_watermark get_lowest_watermark(union meminfo *mi,
2516                                                 struct zone_watermarks *watermarks)
2517 {
2518     int64_t nr_free_pages = mi->field.nr_free_pages - mi->field.cma_free;
2519 
2520     if (nr_free_pages < watermarks->min_wmark) {
2521         return WMARK_MIN;
2522     }
2523     if (nr_free_pages < watermarks->low_wmark) {
2524         return WMARK_LOW;
2525     }
2526     if (nr_free_pages < watermarks->high_wmark) {
2527         return WMARK_HIGH;
2528     }
2529     return WMARK_NONE;
2530 }
2531 
calc_zone_watermarks(struct zoneinfo * zi,struct zone_watermarks * watermarks)2532 void calc_zone_watermarks(struct zoneinfo *zi, struct zone_watermarks *watermarks) {
2533     memset(watermarks, 0, sizeof(struct zone_watermarks));
2534 
2535     for (int node_idx = 0; node_idx < zi->node_count; node_idx++) {
2536         struct zoneinfo_node *node = &zi->nodes[node_idx];
2537         for (int zone_idx = 0; zone_idx < node->zone_count; zone_idx++) {
2538             struct zoneinfo_zone *zone = &node->zones[zone_idx];
2539 
2540             if (!zone->fields.field.present) {
2541                 continue;
2542             }
2543 
2544             watermarks->high_wmark += zone->max_protection + zone->fields.field.high;
2545             watermarks->low_wmark += zone->max_protection + zone->fields.field.low;
2546             watermarks->min_wmark += zone->max_protection + zone->fields.field.min;
2547         }
2548     }
2549 }
2550 
calc_swap_utilization(union meminfo * mi)2551 static int calc_swap_utilization(union meminfo *mi) {
2552     int64_t swap_used = mi->field.total_swap - mi->field.free_swap;
2553     int64_t total_swappable = mi->field.active_anon + mi->field.inactive_anon +
2554                               mi->field.shmem + swap_used;
2555     return total_swappable > 0 ? (swap_used * 100) / total_swappable : 0;
2556 }
2557 
mp_event_psi(int data,uint32_t events,struct polling_params * poll_params)2558 static void mp_event_psi(int data, uint32_t events, struct polling_params *poll_params) {
2559     enum reclaim_state {
2560         NO_RECLAIM = 0,
2561         KSWAPD_RECLAIM,
2562         DIRECT_RECLAIM,
2563     };
2564     static int64_t init_ws_refault;
2565     static int64_t prev_workingset_refault;
2566     static int64_t base_file_lru;
2567     static int64_t init_pgscan_kswapd;
2568     static int64_t init_pgscan_direct;
2569     static bool killing;
2570     static int thrashing_limit = thrashing_limit_pct;
2571     static struct zone_watermarks watermarks;
2572     static struct timespec wmark_update_tm;
2573     static struct wakeup_info wi;
2574     static struct timespec thrashing_reset_tm;
2575     static int64_t prev_thrash_growth = 0;
2576     static bool check_filecache = false;
2577     static int max_thrashing = 0;
2578 
2579     union meminfo mi;
2580     union vmstat vs;
2581     struct psi_data psi_data;
2582     struct timespec curr_tm;
2583     int64_t thrashing = 0;
2584     bool swap_is_low = false;
2585     enum vmpressure_level level = (enum vmpressure_level)data;
2586     enum kill_reasons kill_reason = NONE;
2587     bool cycle_after_kill = false;
2588     enum reclaim_state reclaim = NO_RECLAIM;
2589     enum zone_watermark wmark = WMARK_NONE;
2590     char kill_desc[LINE_MAX];
2591     bool cut_thrashing_limit = false;
2592     int min_score_adj = 0;
2593     int swap_util = 0;
2594     int64_t swap_low_threshold;
2595     long since_thrashing_reset_ms;
2596     int64_t workingset_refault_file;
2597     bool critical_stall = false;
2598 
2599     if (clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm) != 0) {
2600         ALOGE("Failed to get current time");
2601         return;
2602     }
2603 
2604     record_wakeup_time(&curr_tm, events ? Event : Polling, &wi);
2605 
2606     bool kill_pending = is_kill_pending();
2607     if (kill_pending && (kill_timeout_ms == 0 ||
2608         get_time_diff_ms(&last_kill_tm, &curr_tm) < static_cast<long>(kill_timeout_ms))) {
2609         /* Skip while still killing a process */
2610         wi.skipped_wakeups++;
2611         goto no_kill;
2612     }
2613     /*
2614      * Process is dead or kill timeout is over, stop waiting. This has no effect if pidfds are
2615      * supported and death notification already caused waiting to stop.
2616      */
2617     stop_wait_for_proc_kill(!kill_pending);
2618 
2619     if (vmstat_parse(&vs) < 0) {
2620         ALOGE("Failed to parse vmstat!");
2621         return;
2622     }
2623     /* Starting 5.9 kernel workingset_refault vmstat field was renamed workingset_refault_file */
2624     workingset_refault_file = vs.field.workingset_refault ? : vs.field.workingset_refault_file;
2625 
2626     if (meminfo_parse(&mi) < 0) {
2627         ALOGE("Failed to parse meminfo!");
2628         return;
2629     }
2630 
2631     /* Reset states after process got killed */
2632     if (killing) {
2633         killing = false;
2634         cycle_after_kill = true;
2635         /* Reset file-backed pagecache size and refault amounts after a kill */
2636         base_file_lru = vs.field.nr_inactive_file + vs.field.nr_active_file;
2637         init_ws_refault = workingset_refault_file;
2638         thrashing_reset_tm = curr_tm;
2639         prev_thrash_growth = 0;
2640     }
2641 
2642     /* Check free swap levels */
2643     if (swap_free_low_percentage) {
2644         swap_low_threshold = mi.field.total_swap * swap_free_low_percentage / 100;
2645         swap_is_low = mi.field.free_swap < swap_low_threshold;
2646     } else {
2647         swap_low_threshold = 0;
2648     }
2649 
2650     /* Identify reclaim state */
2651     if (vs.field.pgscan_direct > init_pgscan_direct) {
2652         init_pgscan_direct = vs.field.pgscan_direct;
2653         init_pgscan_kswapd = vs.field.pgscan_kswapd;
2654         reclaim = DIRECT_RECLAIM;
2655     } else if (vs.field.pgscan_kswapd > init_pgscan_kswapd) {
2656         init_pgscan_kswapd = vs.field.pgscan_kswapd;
2657         reclaim = KSWAPD_RECLAIM;
2658     } else if (workingset_refault_file == prev_workingset_refault) {
2659         /*
2660          * Device is not thrashing and not reclaiming, bail out early until we see these stats
2661          * changing
2662          */
2663         goto no_kill;
2664     }
2665 
2666     prev_workingset_refault = workingset_refault_file;
2667 
2668      /*
2669      * It's possible we fail to find an eligible process to kill (ex. no process is
2670      * above oom_adj_min). When this happens, we should retry to find a new process
2671      * for a kill whenever a new eligible process is available. This is especially
2672      * important for a slow growing refault case. While retrying, we should keep
2673      * monitoring new thrashing counter as someone could release the memory to mitigate
2674      * the thrashing. Thus, when thrashing reset window comes, we decay the prev thrashing
2675      * counter by window counts. If the counter is still greater than thrashing limit,
2676      * we preserve the current prev_thrash counter so we will retry kill again. Otherwise,
2677      * we reset the prev_thrash counter so we will stop retrying.
2678      */
2679     since_thrashing_reset_ms = get_time_diff_ms(&thrashing_reset_tm, &curr_tm);
2680     if (since_thrashing_reset_ms > THRASHING_RESET_INTERVAL_MS) {
2681         long windows_passed;
2682         /* Calculate prev_thrash_growth if we crossed THRASHING_RESET_INTERVAL_MS */
2683         prev_thrash_growth = (workingset_refault_file - init_ws_refault) * 100
2684                             / (base_file_lru + 1);
2685         windows_passed = (since_thrashing_reset_ms / THRASHING_RESET_INTERVAL_MS);
2686         /*
2687          * Decay prev_thrashing unless over-the-limit thrashing was registered in the window we
2688          * just crossed, which means there were no eligible processes to kill. We preserve the
2689          * counter in that case to ensure a kill if a new eligible process appears.
2690          */
2691         if (windows_passed > 1 || prev_thrash_growth < thrashing_limit) {
2692             prev_thrash_growth >>= windows_passed;
2693         }
2694 
2695         /* Record file-backed pagecache size when crossing THRASHING_RESET_INTERVAL_MS */
2696         base_file_lru = vs.field.nr_inactive_file + vs.field.nr_active_file;
2697         init_ws_refault = workingset_refault_file;
2698         thrashing_reset_tm = curr_tm;
2699         thrashing_limit = thrashing_limit_pct;
2700     } else {
2701         /* Calculate what % of the file-backed pagecache refaulted so far */
2702         thrashing = (workingset_refault_file - init_ws_refault) * 100 / (base_file_lru + 1);
2703     }
2704     /* Add previous cycle's decayed thrashing amount */
2705     thrashing += prev_thrash_growth;
2706     if (max_thrashing < thrashing) {
2707         max_thrashing = thrashing;
2708     }
2709 
2710     /*
2711      * Refresh watermarks once per min in case user updated one of the margins.
2712      * TODO: b/140521024 replace this periodic update with an API for AMS to notify LMKD
2713      * that zone watermarks were changed by the system software.
2714      */
2715     if (watermarks.high_wmark == 0 || get_time_diff_ms(&wmark_update_tm, &curr_tm) > 60000) {
2716         struct zoneinfo zi;
2717 
2718         if (zoneinfo_parse(&zi) < 0) {
2719             ALOGE("Failed to parse zoneinfo!");
2720             return;
2721         }
2722 
2723         calc_zone_watermarks(&zi, &watermarks);
2724         wmark_update_tm = curr_tm;
2725     }
2726 
2727     /* Find out which watermark is breached if any */
2728     wmark = get_lowest_watermark(&mi, &watermarks);
2729 
2730     if (!psi_parse_mem(&psi_data)) {
2731         critical_stall = psi_data.mem_stats[PSI_FULL].avg10 > (float)stall_limit_critical;
2732     }
2733     /*
2734      * TODO: move this logic into a separate function
2735      * Decide if killing a process is necessary and record the reason
2736      */
2737     if (cycle_after_kill && wmark < WMARK_LOW) {
2738         /*
2739          * Prevent kills not freeing enough memory which might lead to OOM kill.
2740          * This might happen when a process is consuming memory faster than reclaim can
2741          * free even after a kill. Mostly happens when running memory stress tests.
2742          */
2743         kill_reason = PRESSURE_AFTER_KILL;
2744         strncpy(kill_desc, "min watermark is breached even after kill", sizeof(kill_desc));
2745     } else if (level == VMPRESS_LEVEL_CRITICAL && events != 0) {
2746         /*
2747          * Device is too busy reclaiming memory which might lead to ANR.
2748          * Critical level is triggered when PSI complete stall (all tasks are blocked because
2749          * of the memory congestion) breaches the configured threshold.
2750          */
2751         kill_reason = NOT_RESPONDING;
2752         strncpy(kill_desc, "device is not responding", sizeof(kill_desc));
2753     } else if (swap_is_low && thrashing > thrashing_limit_pct) {
2754         /* Page cache is thrashing while swap is low */
2755         kill_reason = LOW_SWAP_AND_THRASHING;
2756         snprintf(kill_desc, sizeof(kill_desc), "device is low on swap (%" PRId64
2757             "kB < %" PRId64 "kB) and thrashing (%" PRId64 "%%)",
2758             mi.field.free_swap * page_k, swap_low_threshold * page_k, thrashing);
2759         /* Do not kill perceptible apps unless below min watermark or heavily thrashing */
2760         if (wmark > WMARK_MIN && thrashing < thrashing_critical_pct) {
2761             min_score_adj = PERCEPTIBLE_APP_ADJ + 1;
2762         }
2763         check_filecache = true;
2764     } else if (swap_is_low && wmark < WMARK_HIGH) {
2765         /* Both free memory and swap are low */
2766         kill_reason = LOW_MEM_AND_SWAP;
2767         snprintf(kill_desc, sizeof(kill_desc), "%s watermark is breached and swap is low (%"
2768             PRId64 "kB < %" PRId64 "kB)", wmark < WMARK_LOW ? "min" : "low",
2769             mi.field.free_swap * page_k, swap_low_threshold * page_k);
2770         /* Do not kill perceptible apps unless below min watermark or heavily thrashing */
2771         if (wmark > WMARK_MIN && thrashing < thrashing_critical_pct) {
2772             min_score_adj = PERCEPTIBLE_APP_ADJ + 1;
2773         }
2774     } else if (wmark < WMARK_HIGH && swap_util_max < 100 &&
2775                (swap_util = calc_swap_utilization(&mi)) > swap_util_max) {
2776         /*
2777          * Too much anon memory is swapped out but swap is not low.
2778          * Non-swappable allocations created memory pressure.
2779          */
2780         kill_reason = LOW_MEM_AND_SWAP_UTIL;
2781         snprintf(kill_desc, sizeof(kill_desc), "%s watermark is breached and swap utilization"
2782             " is high (%d%% > %d%%)", wmark < WMARK_LOW ? "min" : "low",
2783             swap_util, swap_util_max);
2784     } else if (wmark < WMARK_HIGH && thrashing > thrashing_limit) {
2785         /* Page cache is thrashing while memory is low */
2786         kill_reason = LOW_MEM_AND_THRASHING;
2787         snprintf(kill_desc, sizeof(kill_desc), "%s watermark is breached and thrashing (%"
2788             PRId64 "%%)", wmark < WMARK_LOW ? "min" : "low", thrashing);
2789         cut_thrashing_limit = true;
2790         /* Do not kill perceptible apps unless thrashing at critical levels */
2791         if (thrashing < thrashing_critical_pct) {
2792             min_score_adj = PERCEPTIBLE_APP_ADJ + 1;
2793         }
2794         check_filecache = true;
2795     } else if (reclaim == DIRECT_RECLAIM && thrashing > thrashing_limit) {
2796         /* Page cache is thrashing while in direct reclaim (mostly happens on lowram devices) */
2797         kill_reason = DIRECT_RECL_AND_THRASHING;
2798         snprintf(kill_desc, sizeof(kill_desc), "device is in direct reclaim and thrashing (%"
2799             PRId64 "%%)", thrashing);
2800         cut_thrashing_limit = true;
2801         /* Do not kill perceptible apps unless thrashing at critical levels */
2802         if (thrashing < thrashing_critical_pct) {
2803             min_score_adj = PERCEPTIBLE_APP_ADJ + 1;
2804         }
2805         check_filecache = true;
2806     } else if (check_filecache) {
2807         int64_t file_lru_kb = (vs.field.nr_inactive_file + vs.field.nr_active_file) * page_k;
2808 
2809         if (file_lru_kb < filecache_min_kb) {
2810             /* File cache is too low after thrashing, keep killing background processes */
2811             kill_reason = LOW_FILECACHE_AFTER_THRASHING;
2812             snprintf(kill_desc, sizeof(kill_desc),
2813                 "filecache is low (%" PRId64 "kB < %" PRId64 "kB) after thrashing",
2814                 file_lru_kb, filecache_min_kb);
2815             min_score_adj = PERCEPTIBLE_APP_ADJ + 1;
2816         } else {
2817             /* File cache is big enough, stop checking */
2818             check_filecache = false;
2819         }
2820     }
2821 
2822     /* Kill a process if necessary */
2823     if (kill_reason != NONE) {
2824         struct kill_info ki = {
2825             .kill_reason = kill_reason,
2826             .kill_desc = kill_desc,
2827             .thrashing = (int)thrashing,
2828             .max_thrashing = max_thrashing,
2829         };
2830 
2831         /* Allow killing perceptible apps if the system is stalled */
2832         if (critical_stall) {
2833             min_score_adj = 0;
2834         }
2835         psi_parse_io(&psi_data);
2836         psi_parse_cpu(&psi_data);
2837         int pages_freed = find_and_kill_process(min_score_adj, &ki, &mi, &wi, &curr_tm, &psi_data);
2838         if (pages_freed > 0) {
2839             killing = true;
2840             max_thrashing = 0;
2841             if (cut_thrashing_limit) {
2842                 /*
2843                  * Cut thrasing limit by thrashing_limit_decay_pct percentage of the current
2844                  * thrashing limit until the system stops thrashing.
2845                  */
2846                 thrashing_limit = (thrashing_limit * (100 - thrashing_limit_decay_pct)) / 100;
2847             }
2848         }
2849     }
2850 
2851 no_kill:
2852     /* Do not poll if kernel supports pidfd waiting */
2853     if (is_waiting_for_kill()) {
2854         /* Pause polling if we are waiting for process death notification */
2855         poll_params->update = POLLING_PAUSE;
2856         return;
2857     }
2858 
2859     /*
2860      * Start polling after initial PSI event;
2861      * extend polling while device is in direct reclaim or process is being killed;
2862      * do not extend when kswapd reclaims because that might go on for a long time
2863      * without causing memory pressure
2864      */
2865     if (events || killing || reclaim == DIRECT_RECLAIM) {
2866         poll_params->update = POLLING_START;
2867     }
2868 
2869     /* Decide the polling interval */
2870     if (swap_is_low || killing) {
2871         /* Fast polling during and after a kill or when swap is low */
2872         poll_params->polling_interval_ms = PSI_POLL_PERIOD_SHORT_MS;
2873     } else {
2874         /* By default use long intervals */
2875         poll_params->polling_interval_ms = PSI_POLL_PERIOD_LONG_MS;
2876     }
2877 }
2878 
GetCgroupAttributePath(const char * attr)2879 static std::string GetCgroupAttributePath(const char* attr) {
2880     std::string path;
2881     if (!CgroupGetAttributePath(attr, &path)) {
2882         ALOGE("Unknown cgroup attribute %s", attr);
2883     }
2884     return path;
2885 }
2886 
2887 // The implementation of this function relies on memcg statistics that are only available in the
2888 // v1 cgroup hierarchy.
mp_event_common(int data,uint32_t events,struct polling_params * poll_params)2889 static void mp_event_common(int data, uint32_t events, struct polling_params *poll_params) {
2890     unsigned long long evcount;
2891     int64_t mem_usage, memsw_usage;
2892     int64_t mem_pressure;
2893     union meminfo mi;
2894     struct zoneinfo zi;
2895     struct timespec curr_tm;
2896     static unsigned long kill_skip_count = 0;
2897     enum vmpressure_level level = (enum vmpressure_level)data;
2898     long other_free = 0, other_file = 0;
2899     int min_score_adj;
2900     int minfree = 0;
2901     static const std::string mem_usage_path = GetCgroupAttributePath("MemUsage");
2902     static struct reread_data mem_usage_file_data = {
2903         .filename = mem_usage_path.c_str(),
2904         .fd = -1,
2905     };
2906     static const std::string memsw_usage_path = GetCgroupAttributePath("MemAndSwapUsage");
2907     static struct reread_data memsw_usage_file_data = {
2908         .filename = memsw_usage_path.c_str(),
2909         .fd = -1,
2910     };
2911     static struct wakeup_info wi;
2912 
2913     if (debug_process_killing) {
2914         ALOGI("%s memory pressure event is triggered", level_name[level]);
2915     }
2916 
2917     if (!use_psi_monitors) {
2918         /*
2919          * Check all event counters from low to critical
2920          * and upgrade to the highest priority one. By reading
2921          * eventfd we also reset the event counters.
2922          */
2923         for (int lvl = VMPRESS_LEVEL_LOW; lvl < VMPRESS_LEVEL_COUNT; lvl++) {
2924             if (mpevfd[lvl] != -1 &&
2925                 TEMP_FAILURE_RETRY(read(mpevfd[lvl],
2926                                    &evcount, sizeof(evcount))) > 0 &&
2927                 evcount > 0 && lvl > level) {
2928                 level = static_cast<vmpressure_level>(lvl);
2929             }
2930         }
2931     }
2932 
2933     /* Start polling after initial PSI event */
2934     if (use_psi_monitors && events) {
2935         /* Override polling params only if current event is more critical */
2936         if (!poll_params->poll_handler || data > poll_params->poll_handler->data) {
2937             poll_params->polling_interval_ms = PSI_POLL_PERIOD_SHORT_MS;
2938             poll_params->update = POLLING_START;
2939         }
2940     }
2941 
2942     if (clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm) != 0) {
2943         ALOGE("Failed to get current time");
2944         return;
2945     }
2946 
2947     record_wakeup_time(&curr_tm, events ? Event : Polling, &wi);
2948 
2949     if (kill_timeout_ms &&
2950         get_time_diff_ms(&last_kill_tm, &curr_tm) < static_cast<long>(kill_timeout_ms)) {
2951         /*
2952          * If we're within the no-kill timeout, see if there's pending reclaim work
2953          * from the last killed process. If so, skip killing for now.
2954          */
2955         if (is_kill_pending()) {
2956             kill_skip_count++;
2957             wi.skipped_wakeups++;
2958             return;
2959         }
2960         /*
2961          * Process is dead, stop waiting. This has no effect if pidfds are supported and
2962          * death notification already caused waiting to stop.
2963          */
2964         stop_wait_for_proc_kill(true);
2965     } else {
2966         /*
2967          * Killing took longer than no-kill timeout. Stop waiting for the last process
2968          * to die because we are ready to kill again.
2969          */
2970         stop_wait_for_proc_kill(false);
2971     }
2972 
2973     if (kill_skip_count > 0) {
2974         ALOGI("%lu memory pressure events were skipped after a kill!",
2975               kill_skip_count);
2976         kill_skip_count = 0;
2977     }
2978 
2979     if (meminfo_parse(&mi) < 0 || zoneinfo_parse(&zi) < 0) {
2980         ALOGE("Failed to get free memory!");
2981         return;
2982     }
2983 
2984     if (use_minfree_levels) {
2985         int i;
2986 
2987         other_free = mi.field.nr_free_pages - zi.totalreserve_pages;
2988         if (mi.field.nr_file_pages > (mi.field.shmem + mi.field.unevictable + mi.field.swap_cached)) {
2989             other_file = (mi.field.nr_file_pages - mi.field.shmem -
2990                           mi.field.unevictable - mi.field.swap_cached);
2991         } else {
2992             other_file = 0;
2993         }
2994 
2995         min_score_adj = OOM_SCORE_ADJ_MAX + 1;
2996         for (i = 0; i < lowmem_targets_size; i++) {
2997             minfree = lowmem_minfree[i];
2998             if (other_free < minfree && other_file < minfree) {
2999                 min_score_adj = lowmem_adj[i];
3000                 break;
3001             }
3002         }
3003 
3004         if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
3005             if (debug_process_killing && lowmem_targets_size) {
3006                 ALOGI("Ignore %s memory pressure event "
3007                       "(free memory=%ldkB, cache=%ldkB, limit=%ldkB)",
3008                       level_name[level], other_free * page_k, other_file * page_k,
3009                       (long)lowmem_minfree[lowmem_targets_size - 1] * page_k);
3010             }
3011             return;
3012         }
3013 
3014         goto do_kill;
3015     }
3016 
3017     if (level == VMPRESS_LEVEL_LOW) {
3018         record_low_pressure_levels(&mi);
3019     }
3020 
3021     if (level_oomadj[level] > OOM_SCORE_ADJ_MAX) {
3022         /* Do not monitor this pressure level */
3023         return;
3024     }
3025 
3026     if ((mem_usage = get_memory_usage(&mem_usage_file_data)) < 0) {
3027         goto do_kill;
3028     }
3029     if ((memsw_usage = get_memory_usage(&memsw_usage_file_data)) < 0) {
3030         goto do_kill;
3031     }
3032 
3033     // Calculate percent for swappinness.
3034     mem_pressure = (mem_usage * 100) / memsw_usage;
3035 
3036     if (enable_pressure_upgrade && level != VMPRESS_LEVEL_CRITICAL) {
3037         // We are swapping too much.
3038         if (mem_pressure < upgrade_pressure) {
3039             level = upgrade_level(level);
3040             if (debug_process_killing) {
3041                 ALOGI("Event upgraded to %s", level_name[level]);
3042             }
3043         }
3044     }
3045 
3046     // If we still have enough swap space available, check if we want to
3047     // ignore/downgrade pressure events.
3048     if (mi.field.free_swap >=
3049         mi.field.total_swap * swap_free_low_percentage / 100) {
3050         // If the pressure is larger than downgrade_pressure lmk will not
3051         // kill any process, since enough memory is available.
3052         if (mem_pressure > downgrade_pressure) {
3053             if (debug_process_killing) {
3054                 ALOGI("Ignore %s memory pressure", level_name[level]);
3055             }
3056             return;
3057         } else if (level == VMPRESS_LEVEL_CRITICAL && mem_pressure > upgrade_pressure) {
3058             if (debug_process_killing) {
3059                 ALOGI("Downgrade critical memory pressure");
3060             }
3061             // Downgrade event, since enough memory available.
3062             level = downgrade_level(level);
3063         }
3064     }
3065 
3066 do_kill:
3067     if (low_ram_device) {
3068         /* For Go devices kill only one task */
3069         if (find_and_kill_process(level_oomadj[level], NULL, &mi, &wi, &curr_tm, NULL) == 0) {
3070             if (debug_process_killing) {
3071                 ALOGI("Nothing to kill");
3072             }
3073         }
3074     } else {
3075         int pages_freed;
3076         static struct timespec last_report_tm;
3077         static unsigned long report_skip_count = 0;
3078 
3079         if (!use_minfree_levels) {
3080             /* Free up enough memory to downgrate the memory pressure to low level */
3081             if (mi.field.nr_free_pages >= low_pressure_mem.max_nr_free_pages) {
3082                 if (debug_process_killing) {
3083                     ALOGI("Ignoring pressure since more memory is "
3084                         "available (%" PRId64 ") than watermark (%" PRId64 ")",
3085                         mi.field.nr_free_pages, low_pressure_mem.max_nr_free_pages);
3086                 }
3087                 return;
3088             }
3089             min_score_adj = level_oomadj[level];
3090         }
3091 
3092         pages_freed = find_and_kill_process(min_score_adj, NULL, &mi, &wi, &curr_tm, NULL);
3093 
3094         if (pages_freed == 0) {
3095             /* Rate limit kill reports when nothing was reclaimed */
3096             if (get_time_diff_ms(&last_report_tm, &curr_tm) < FAIL_REPORT_RLIMIT_MS) {
3097                 report_skip_count++;
3098                 return;
3099             }
3100         }
3101 
3102         /* Log whenever we kill or when report rate limit allows */
3103         if (use_minfree_levels) {
3104             ALOGI("Reclaimed %ldkB, cache(%ldkB) and free(%" PRId64 "kB)-reserved(%" PRId64 "kB) "
3105                 "below min(%ldkB) for oom_score_adj %d",
3106                 pages_freed * page_k,
3107                 other_file * page_k, mi.field.nr_free_pages * page_k,
3108                 zi.totalreserve_pages * page_k,
3109                 minfree * page_k, min_score_adj);
3110         } else {
3111             ALOGI("Reclaimed %ldkB at oom_score_adj %d", pages_freed * page_k, min_score_adj);
3112         }
3113 
3114         if (report_skip_count > 0) {
3115             ALOGI("Suppressed %lu failed kill reports", report_skip_count);
3116             report_skip_count = 0;
3117         }
3118 
3119         last_report_tm = curr_tm;
3120     }
3121     if (is_waiting_for_kill()) {
3122         /* pause polling if we are waiting for process death notification */
3123         poll_params->update = POLLING_PAUSE;
3124     }
3125 }
3126 
init_mp_psi(enum vmpressure_level level,bool use_new_strategy)3127 static bool init_mp_psi(enum vmpressure_level level, bool use_new_strategy) {
3128     int fd;
3129 
3130     /* Do not register a handler if threshold_ms is not set */
3131     if (!psi_thresholds[level].threshold_ms) {
3132         return true;
3133     }
3134 
3135     fd = init_psi_monitor(psi_thresholds[level].stall_type,
3136         psi_thresholds[level].threshold_ms * US_PER_MS,
3137         PSI_WINDOW_SIZE_MS * US_PER_MS);
3138 
3139     if (fd < 0) {
3140         return false;
3141     }
3142 
3143     vmpressure_hinfo[level].handler = use_new_strategy ? mp_event_psi : mp_event_common;
3144     vmpressure_hinfo[level].data = level;
3145     if (register_psi_monitor(epollfd, fd, &vmpressure_hinfo[level]) < 0) {
3146         destroy_psi_monitor(fd);
3147         return false;
3148     }
3149     maxevents++;
3150     mpevfd[level] = fd;
3151 
3152     return true;
3153 }
3154 
destroy_mp_psi(enum vmpressure_level level)3155 static void destroy_mp_psi(enum vmpressure_level level) {
3156     int fd = mpevfd[level];
3157 
3158     if (fd < 0) {
3159         return;
3160     }
3161 
3162     if (unregister_psi_monitor(epollfd, fd) < 0) {
3163         ALOGE("Failed to unregister psi monitor for %s memory pressure; errno=%d",
3164             level_name[level], errno);
3165     }
3166     maxevents--;
3167     destroy_psi_monitor(fd);
3168     mpevfd[level] = -1;
3169 }
3170 
3171 enum class MemcgVersion {
3172     kNotFound,
3173     kV1,
3174     kV2,
3175 };
3176 
__memcg_version()3177 static MemcgVersion __memcg_version() {
3178     std::string cgroupv2_path, memcg_path;
3179 
3180     if (!CgroupGetControllerPath("memory", &memcg_path)) {
3181         return MemcgVersion::kNotFound;
3182     }
3183     return CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cgroupv2_path) &&
3184                            cgroupv2_path == memcg_path
3185                    ? MemcgVersion::kV2
3186                    : MemcgVersion::kV1;
3187 }
3188 
memcg_version()3189 static MemcgVersion memcg_version() {
3190     static MemcgVersion version = __memcg_version();
3191 
3192     return version;
3193 }
3194 
init_psi_monitors()3195 static bool init_psi_monitors() {
3196     /*
3197      * When PSI is used on low-ram devices or on high-end devices without memfree levels
3198      * use new kill strategy based on zone watermarks, free swap and thrashing stats.
3199      * Also use the new strategy if memcg has not been mounted in the v1 cgroups hiearchy since
3200      * the old strategy relies on memcg attributes that are available only in the v1 cgroups
3201      * hiearchy.
3202      */
3203     bool use_new_strategy =
3204         GET_LMK_PROPERTY(bool, "use_new_strategy", low_ram_device || !use_minfree_levels);
3205     if (!use_new_strategy && memcg_version() != MemcgVersion::kV1) {
3206         ALOGE("Old kill strategy can only be used with v1 cgroup hierarchy");
3207         return false;
3208     }
3209     /* In default PSI mode override stall amounts using system properties */
3210     if (use_new_strategy) {
3211         /* Do not use low pressure level */
3212         psi_thresholds[VMPRESS_LEVEL_LOW].threshold_ms = 0;
3213         psi_thresholds[VMPRESS_LEVEL_MEDIUM].threshold_ms = psi_partial_stall_ms;
3214         psi_thresholds[VMPRESS_LEVEL_CRITICAL].threshold_ms = psi_complete_stall_ms;
3215     }
3216 
3217     if (!init_mp_psi(VMPRESS_LEVEL_LOW, use_new_strategy)) {
3218         return false;
3219     }
3220     if (!init_mp_psi(VMPRESS_LEVEL_MEDIUM, use_new_strategy)) {
3221         destroy_mp_psi(VMPRESS_LEVEL_LOW);
3222         return false;
3223     }
3224     if (!init_mp_psi(VMPRESS_LEVEL_CRITICAL, use_new_strategy)) {
3225         destroy_mp_psi(VMPRESS_LEVEL_MEDIUM);
3226         destroy_mp_psi(VMPRESS_LEVEL_LOW);
3227         return false;
3228     }
3229     return true;
3230 }
3231 
init_mp_common(enum vmpressure_level level)3232 static bool init_mp_common(enum vmpressure_level level) {
3233     // The implementation of this function relies on memcg statistics that are only available in the
3234     // v1 cgroup hierarchy.
3235     if (memcg_version() != MemcgVersion::kV1) {
3236         ALOGE("%s: global monitoring is only available for the v1 cgroup hierarchy", __func__);
3237         return false;
3238     }
3239 
3240     int mpfd;
3241     int evfd;
3242     int evctlfd;
3243     char buf[256];
3244     struct epoll_event epev;
3245     int ret;
3246     int level_idx = (int)level;
3247     const char *levelstr = level_name[level_idx];
3248 
3249     /* gid containing AID_SYSTEM required */
3250     mpfd = open(GetCgroupAttributePath("MemPressureLevel").c_str(), O_RDONLY | O_CLOEXEC);
3251     if (mpfd < 0) {
3252         ALOGI("No kernel memory.pressure_level support (errno=%d)", errno);
3253         goto err_open_mpfd;
3254     }
3255 
3256     evctlfd = open(GetCgroupAttributePath("CgroupEventControl").c_str(), O_WRONLY | O_CLOEXEC);
3257     if (evctlfd < 0) {
3258         ALOGI("No kernel memory cgroup event control (errno=%d)", errno);
3259         goto err_open_evctlfd;
3260     }
3261 
3262     evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
3263     if (evfd < 0) {
3264         ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno);
3265         goto err_eventfd;
3266     }
3267 
3268     ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr);
3269     if (ret >= (ssize_t)sizeof(buf)) {
3270         ALOGE("cgroup.event_control line overflow for level %s", levelstr);
3271         goto err;
3272     }
3273 
3274     ret = TEMP_FAILURE_RETRY(write(evctlfd, buf, strlen(buf) + 1));
3275     if (ret == -1) {
3276         ALOGE("cgroup.event_control write failed for level %s; errno=%d",
3277               levelstr, errno);
3278         goto err;
3279     }
3280 
3281     epev.events = EPOLLIN;
3282     /* use data to store event level */
3283     vmpressure_hinfo[level_idx].data = level_idx;
3284     vmpressure_hinfo[level_idx].handler = mp_event_common;
3285     epev.data.ptr = (void *)&vmpressure_hinfo[level_idx];
3286     ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev);
3287     if (ret == -1) {
3288         ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno);
3289         goto err;
3290     }
3291     maxevents++;
3292     mpevfd[level] = evfd;
3293     close(evctlfd);
3294     return true;
3295 
3296 err:
3297     close(evfd);
3298 err_eventfd:
3299     close(evctlfd);
3300 err_open_evctlfd:
3301     close(mpfd);
3302 err_open_mpfd:
3303     return false;
3304 }
3305 
destroy_mp_common(enum vmpressure_level level)3306 static void destroy_mp_common(enum vmpressure_level level) {
3307     struct epoll_event epev;
3308     int fd = mpevfd[level];
3309 
3310     if (fd < 0) {
3311         return;
3312     }
3313 
3314     if (epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &epev)) {
3315         // Log an error and keep going
3316         ALOGE("epoll_ctl for level %s failed; errno=%d", level_name[level], errno);
3317     }
3318     maxevents--;
3319     close(fd);
3320     mpevfd[level] = -1;
3321 }
3322 
kernel_event_handler(int data __unused,uint32_t events __unused,struct polling_params * poll_params __unused)3323 static void kernel_event_handler(int data __unused, uint32_t events __unused,
3324                                  struct polling_params *poll_params __unused) {
3325     poll_kernel(kpoll_fd);
3326 }
3327 
init_monitors()3328 static bool init_monitors() {
3329     /* Try to use psi monitor first if kernel has it */
3330     use_psi_monitors = GET_LMK_PROPERTY(bool, "use_psi", true) &&
3331         init_psi_monitors();
3332     /* Fall back to vmpressure */
3333     if (!use_psi_monitors &&
3334         (!init_mp_common(VMPRESS_LEVEL_LOW) ||
3335         !init_mp_common(VMPRESS_LEVEL_MEDIUM) ||
3336         !init_mp_common(VMPRESS_LEVEL_CRITICAL))) {
3337         ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer");
3338         return false;
3339     }
3340     if (use_psi_monitors) {
3341         ALOGI("Using psi monitors for memory pressure detection");
3342     } else {
3343         ALOGI("Using vmpressure for memory pressure detection");
3344     }
3345     return true;
3346 }
3347 
destroy_monitors()3348 static void destroy_monitors() {
3349     if (use_psi_monitors) {
3350         destroy_mp_psi(VMPRESS_LEVEL_CRITICAL);
3351         destroy_mp_psi(VMPRESS_LEVEL_MEDIUM);
3352         destroy_mp_psi(VMPRESS_LEVEL_LOW);
3353     } else {
3354         destroy_mp_common(VMPRESS_LEVEL_CRITICAL);
3355         destroy_mp_common(VMPRESS_LEVEL_MEDIUM);
3356         destroy_mp_common(VMPRESS_LEVEL_LOW);
3357     }
3358 }
3359 
drop_reaper_comm()3360 static void drop_reaper_comm() {
3361     close(reaper_comm_fd[0]);
3362     close(reaper_comm_fd[1]);
3363 }
3364 
setup_reaper_comm()3365 static bool setup_reaper_comm() {
3366     if (pipe(reaper_comm_fd)) {
3367         ALOGE("pipe failed: %s", strerror(errno));
3368         return false;
3369     }
3370 
3371     // Ensure main thread never blocks on read
3372     int flags = fcntl(reaper_comm_fd[0], F_GETFL);
3373     if (fcntl(reaper_comm_fd[0], F_SETFL, flags | O_NONBLOCK)) {
3374         ALOGE("fcntl failed: %s", strerror(errno));
3375         drop_reaper_comm();
3376         return false;
3377     }
3378 
3379     return true;
3380 }
3381 
init_reaper()3382 static bool init_reaper() {
3383     if (!reaper.is_reaping_supported()) {
3384         ALOGI("Process reaping is not supported");
3385         return false;
3386     }
3387 
3388     if (!setup_reaper_comm()) {
3389         ALOGE("Failed to create thread communication channel");
3390         return false;
3391     }
3392 
3393     // Setup epoll handler
3394     struct epoll_event epev;
3395     static struct event_handler_info kill_failed_hinfo = { 0, kill_fail_handler };
3396     epev.events = EPOLLIN;
3397     epev.data.ptr = (void *)&kill_failed_hinfo;
3398     if (epoll_ctl(epollfd, EPOLL_CTL_ADD, reaper_comm_fd[0], &epev)) {
3399         ALOGE("epoll_ctl failed: %s", strerror(errno));
3400         drop_reaper_comm();
3401         return false;
3402     }
3403 
3404     if (!reaper.init(reaper_comm_fd[1])) {
3405         ALOGE("Failed to initialize reaper object");
3406         if (epoll_ctl(epollfd, EPOLL_CTL_DEL, reaper_comm_fd[0], &epev)) {
3407             ALOGE("epoll_ctl failed: %s", strerror(errno));
3408         }
3409         drop_reaper_comm();
3410         return false;
3411     }
3412     maxevents++;
3413 
3414     return true;
3415 }
3416 
init(void)3417 static int init(void) {
3418     static struct event_handler_info kernel_poll_hinfo = { 0, kernel_event_handler };
3419     struct reread_data file_data = {
3420         .filename = ZONEINFO_PATH,
3421         .fd = -1,
3422     };
3423     struct epoll_event epev;
3424     int pidfd;
3425     int i;
3426     int ret;
3427 
3428     page_k = sysconf(_SC_PAGESIZE);
3429     if (page_k == -1)
3430         page_k = PAGE_SIZE;
3431     page_k /= 1024;
3432 
3433     epollfd = epoll_create(MAX_EPOLL_EVENTS);
3434     if (epollfd == -1) {
3435         ALOGE("epoll_create failed (errno=%d)", errno);
3436         return -1;
3437     }
3438 
3439     // mark data connections as not connected
3440     for (int i = 0; i < MAX_DATA_CONN; i++) {
3441         data_sock[i].sock = -1;
3442     }
3443 
3444     ctrl_sock.sock = android_get_control_socket("lmkd");
3445     if (ctrl_sock.sock < 0) {
3446         ALOGE("get lmkd control socket failed");
3447         return -1;
3448     }
3449 
3450     ret = listen(ctrl_sock.sock, MAX_DATA_CONN);
3451     if (ret < 0) {
3452         ALOGE("lmkd control socket listen failed (errno=%d)", errno);
3453         return -1;
3454     }
3455 
3456     epev.events = EPOLLIN;
3457     ctrl_sock.handler_info.handler = ctrl_connect_handler;
3458     epev.data.ptr = (void *)&(ctrl_sock.handler_info);
3459     if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_sock.sock, &epev) == -1) {
3460         ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno);
3461         return -1;
3462     }
3463     maxevents++;
3464 
3465     has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK);
3466     use_inkernel_interface = has_inkernel_module;
3467 
3468     if (use_inkernel_interface) {
3469         ALOGI("Using in-kernel low memory killer interface");
3470         if (init_poll_kernel()) {
3471             epev.events = EPOLLIN;
3472             epev.data.ptr = (void*)&kernel_poll_hinfo;
3473             if (epoll_ctl(epollfd, EPOLL_CTL_ADD, kpoll_fd, &epev) != 0) {
3474                 ALOGE("epoll_ctl for lmk events failed (errno=%d)", errno);
3475                 close(kpoll_fd);
3476                 kpoll_fd = -1;
3477             } else {
3478                 maxevents++;
3479                 /* let the others know it does support reporting kills */
3480                 property_set("sys.lmk.reportkills", "1");
3481             }
3482         }
3483     } else {
3484         if (!init_monitors()) {
3485             return -1;
3486         }
3487         /* let the others know it does support reporting kills */
3488         property_set("sys.lmk.reportkills", "1");
3489     }
3490 
3491     for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) {
3492         procadjslot_list[i].next = &procadjslot_list[i];
3493         procadjslot_list[i].prev = &procadjslot_list[i];
3494     }
3495 
3496     memset(killcnt_idx, KILLCNT_INVALID_IDX, sizeof(killcnt_idx));
3497 
3498     /*
3499      * Read zoneinfo as the biggest file we read to create and size the initial
3500      * read buffer and avoid memory re-allocations during memory pressure
3501      */
3502     if (reread_file(&file_data) == NULL) {
3503         ALOGE("Failed to read %s: %s", file_data.filename, strerror(errno));
3504     }
3505 
3506     /* check if kernel supports pidfd_open syscall */
3507     pidfd = TEMP_FAILURE_RETRY(pidfd_open(getpid(), 0));
3508     if (pidfd < 0) {
3509         pidfd_supported = (errno != ENOSYS);
3510     } else {
3511         pidfd_supported = true;
3512         close(pidfd);
3513     }
3514     ALOGI("Process polling is %s", pidfd_supported ? "supported" : "not supported" );
3515 
3516     return 0;
3517 }
3518 
polling_paused(struct polling_params * poll_params)3519 static bool polling_paused(struct polling_params *poll_params) {
3520     return poll_params->paused_handler != NULL;
3521 }
3522 
resume_polling(struct polling_params * poll_params,struct timespec curr_tm)3523 static void resume_polling(struct polling_params *poll_params, struct timespec curr_tm) {
3524     poll_params->poll_start_tm = curr_tm;
3525     poll_params->poll_handler = poll_params->paused_handler;
3526     poll_params->polling_interval_ms = PSI_POLL_PERIOD_SHORT_MS;
3527     poll_params->paused_handler = NULL;
3528 }
3529 
call_handler(struct event_handler_info * handler_info,struct polling_params * poll_params,uint32_t events)3530 static void call_handler(struct event_handler_info* handler_info,
3531                          struct polling_params *poll_params, uint32_t events) {
3532     struct timespec curr_tm;
3533 
3534     watchdog.start();
3535     poll_params->update = POLLING_DO_NOT_CHANGE;
3536     handler_info->handler(handler_info->data, events, poll_params);
3537     clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm);
3538     if (poll_params->poll_handler == handler_info) {
3539         poll_params->last_poll_tm = curr_tm;
3540     }
3541 
3542     switch (poll_params->update) {
3543     case POLLING_START:
3544         /*
3545          * Poll for the duration of PSI_WINDOW_SIZE_MS after the
3546          * initial PSI event because psi events are rate-limited
3547          * at one per sec.
3548          */
3549         poll_params->poll_start_tm = curr_tm;
3550         poll_params->poll_handler = handler_info;
3551         break;
3552     case POLLING_PAUSE:
3553         poll_params->paused_handler = handler_info;
3554         poll_params->poll_handler = NULL;
3555         break;
3556     case POLLING_RESUME:
3557         resume_polling(poll_params, curr_tm);
3558         break;
3559     case POLLING_DO_NOT_CHANGE:
3560         if (get_time_diff_ms(&poll_params->poll_start_tm, &curr_tm) > PSI_WINDOW_SIZE_MS) {
3561             /* Polled for the duration of PSI window, time to stop */
3562             poll_params->poll_handler = NULL;
3563         }
3564         break;
3565     }
3566     watchdog.stop();
3567 }
3568 
mainloop(void)3569 static void mainloop(void) {
3570     struct event_handler_info* handler_info;
3571     struct polling_params poll_params;
3572     struct timespec curr_tm;
3573     struct epoll_event *evt;
3574     long delay = -1;
3575 
3576     poll_params.poll_handler = NULL;
3577     poll_params.paused_handler = NULL;
3578 
3579     while (1) {
3580         struct epoll_event events[MAX_EPOLL_EVENTS];
3581         int nevents;
3582         int i;
3583 
3584         if (poll_params.poll_handler) {
3585             bool poll_now;
3586 
3587             clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm);
3588             if (poll_params.update == POLLING_RESUME) {
3589                 /* Just transitioned into POLLING_RESUME, poll immediately. */
3590                 poll_now = true;
3591                 nevents = 0;
3592             } else {
3593                 /* Calculate next timeout */
3594                 delay = get_time_diff_ms(&poll_params.last_poll_tm, &curr_tm);
3595                 delay = (delay < poll_params.polling_interval_ms) ?
3596                     poll_params.polling_interval_ms - delay : poll_params.polling_interval_ms;
3597 
3598                 /* Wait for events until the next polling timeout */
3599                 nevents = epoll_wait(epollfd, events, maxevents, delay);
3600 
3601                 /* Update current time after wait */
3602                 clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm);
3603                 poll_now = (get_time_diff_ms(&poll_params.last_poll_tm, &curr_tm) >=
3604                     poll_params.polling_interval_ms);
3605             }
3606             if (poll_now) {
3607                 call_handler(poll_params.poll_handler, &poll_params, 0);
3608             }
3609         } else {
3610             if (kill_timeout_ms && is_waiting_for_kill()) {
3611                 clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm);
3612                 delay = kill_timeout_ms - get_time_diff_ms(&last_kill_tm, &curr_tm);
3613                 /* Wait for pidfds notification or kill timeout to expire */
3614                 nevents = (delay > 0) ? epoll_wait(epollfd, events, maxevents, delay) : 0;
3615                 if (nevents == 0) {
3616                     /* Kill notification timed out */
3617                     stop_wait_for_proc_kill(false);
3618                     if (polling_paused(&poll_params)) {
3619                         clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm);
3620                         poll_params.update = POLLING_RESUME;
3621                         resume_polling(&poll_params, curr_tm);
3622                     }
3623                 }
3624             } else {
3625                 /* Wait for events with no timeout */
3626                 nevents = epoll_wait(epollfd, events, maxevents, -1);
3627             }
3628         }
3629 
3630         if (nevents == -1) {
3631             if (errno == EINTR)
3632                 continue;
3633             ALOGE("epoll_wait failed (errno=%d)", errno);
3634             continue;
3635         }
3636 
3637         /*
3638          * First pass to see if any data socket connections were dropped.
3639          * Dropped connection should be handled before any other events
3640          * to deallocate data connection and correctly handle cases when
3641          * connection gets dropped and reestablished in the same epoll cycle.
3642          * In such cases it's essential to handle connection closures first.
3643          */
3644         for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) {
3645             if ((evt->events & EPOLLHUP) && evt->data.ptr) {
3646                 ALOGI("lmkd data connection dropped");
3647                 handler_info = (struct event_handler_info*)evt->data.ptr;
3648                 watchdog.start();
3649                 ctrl_data_close(handler_info->data);
3650                 watchdog.stop();
3651             }
3652         }
3653 
3654         /* Second pass to handle all other events */
3655         for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) {
3656             if (evt->events & EPOLLERR) {
3657                 ALOGD("EPOLLERR on event #%d", i);
3658             }
3659             if (evt->events & EPOLLHUP) {
3660                 /* This case was handled in the first pass */
3661                 continue;
3662             }
3663             if (evt->data.ptr) {
3664                 handler_info = (struct event_handler_info*)evt->data.ptr;
3665                 call_handler(handler_info, &poll_params, evt->events);
3666             }
3667         }
3668     }
3669 }
3670 
issue_reinit()3671 int issue_reinit() {
3672     int sock;
3673 
3674     sock = lmkd_connect();
3675     if (sock < 0) {
3676         ALOGE("failed to connect to lmkd: %s", strerror(errno));
3677         return -1;
3678     }
3679 
3680     enum update_props_result res = lmkd_update_props(sock);
3681     switch (res) {
3682     case UPDATE_PROPS_SUCCESS:
3683         ALOGI("lmkd updated properties successfully");
3684         break;
3685     case UPDATE_PROPS_SEND_ERR:
3686         ALOGE("failed to send lmkd request: %s", strerror(errno));
3687         break;
3688     case UPDATE_PROPS_RECV_ERR:
3689         ALOGE("failed to receive lmkd reply: %s", strerror(errno));
3690         break;
3691     case UPDATE_PROPS_FORMAT_ERR:
3692         ALOGE("lmkd reply is invalid");
3693         break;
3694     case UPDATE_PROPS_FAIL:
3695         ALOGE("lmkd failed to update its properties");
3696         break;
3697     }
3698 
3699     close(sock);
3700     return res == UPDATE_PROPS_SUCCESS ? 0 : -1;
3701 }
3702 
update_props()3703 static void update_props() {
3704     /* By default disable low level vmpressure events */
3705     level_oomadj[VMPRESS_LEVEL_LOW] =
3706         GET_LMK_PROPERTY(int32, "low", OOM_SCORE_ADJ_MAX + 1);
3707     level_oomadj[VMPRESS_LEVEL_MEDIUM] =
3708         GET_LMK_PROPERTY(int32, "medium", 800);
3709     level_oomadj[VMPRESS_LEVEL_CRITICAL] =
3710         GET_LMK_PROPERTY(int32, "critical", 0);
3711     debug_process_killing = GET_LMK_PROPERTY(bool, "debug", false);
3712 
3713     /* By default disable upgrade/downgrade logic */
3714     enable_pressure_upgrade =
3715         GET_LMK_PROPERTY(bool, "critical_upgrade", false);
3716     upgrade_pressure =
3717         (int64_t)GET_LMK_PROPERTY(int32, "upgrade_pressure", 100);
3718     downgrade_pressure =
3719         (int64_t)GET_LMK_PROPERTY(int32, "downgrade_pressure", 100);
3720     kill_heaviest_task =
3721         GET_LMK_PROPERTY(bool, "kill_heaviest_task", false);
3722     low_ram_device = property_get_bool("ro.config.low_ram", false);
3723     kill_timeout_ms =
3724         (unsigned long)GET_LMK_PROPERTY(int32, "kill_timeout_ms", 100);
3725     use_minfree_levels =
3726         GET_LMK_PROPERTY(bool, "use_minfree_levels", false);
3727     per_app_memcg =
3728         property_get_bool("ro.config.per_app_memcg", low_ram_device);
3729     swap_free_low_percentage = clamp(0, 100, GET_LMK_PROPERTY(int32, "swap_free_low_percentage",
3730         DEF_LOW_SWAP));
3731     psi_partial_stall_ms = GET_LMK_PROPERTY(int32, "psi_partial_stall_ms",
3732         low_ram_device ? DEF_PARTIAL_STALL_LOWRAM : DEF_PARTIAL_STALL);
3733     psi_complete_stall_ms = GET_LMK_PROPERTY(int32, "psi_complete_stall_ms",
3734         DEF_COMPLETE_STALL);
3735     thrashing_limit_pct =
3736             std::max(0, GET_LMK_PROPERTY(int32, "thrashing_limit",
3737                                          low_ram_device ? DEF_THRASHING_LOWRAM : DEF_THRASHING));
3738     thrashing_limit_decay_pct = clamp(0, 100, GET_LMK_PROPERTY(int32, "thrashing_limit_decay",
3739         low_ram_device ? DEF_THRASHING_DECAY_LOWRAM : DEF_THRASHING_DECAY));
3740     thrashing_critical_pct = std::max(
3741             0, GET_LMK_PROPERTY(int32, "thrashing_limit_critical", thrashing_limit_pct * 2));
3742     swap_util_max = clamp(0, 100, GET_LMK_PROPERTY(int32, "swap_util_max", 100));
3743     filecache_min_kb = GET_LMK_PROPERTY(int64, "filecache_min_kb", 0);
3744     stall_limit_critical = GET_LMK_PROPERTY(int64, "stall_limit_critical", 100);
3745 
3746     reaper.enable_debug(debug_process_killing);
3747 }
3748 
main(int argc,char ** argv)3749 int main(int argc, char **argv) {
3750     if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
3751         if (property_set(LMKD_REINIT_PROP, "")) {
3752             ALOGE("Failed to reset " LMKD_REINIT_PROP " property");
3753         }
3754         return issue_reinit();
3755     }
3756 
3757     update_props();
3758 
3759     ctx = create_android_logger(KILLINFO_LOG_TAG);
3760 
3761     if (!init()) {
3762         if (!use_inkernel_interface) {
3763             /*
3764              * MCL_ONFAULT pins pages as they fault instead of loading
3765              * everything immediately all at once. (Which would be bad,
3766              * because as of this writing, we have a lot of mapped pages we
3767              * never use.) Old kernels will see MCL_ONFAULT and fail with
3768              * EINVAL; we ignore this failure.
3769              *
3770              * N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT
3771              * pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault
3772              * in pages.
3773              */
3774             /* CAP_IPC_LOCK required */
3775             if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && (errno != EINVAL)) {
3776                 ALOGW("mlockall failed %s", strerror(errno));
3777             }
3778 
3779             /* CAP_NICE required */
3780             struct sched_param param = {
3781                     .sched_priority = 1,
3782             };
3783             if (sched_setscheduler(0, SCHED_FIFO, &param)) {
3784                 ALOGW("set SCHED_FIFO failed %s", strerror(errno));
3785             }
3786         }
3787 
3788         if (init_reaper()) {
3789             ALOGI("Process reaper initialized with %d threads in the pool",
3790                 reaper.thread_cnt());
3791         }
3792 
3793         if (!watchdog.init()) {
3794             ALOGE("Failed to initialize the watchdog");
3795         }
3796 
3797         mainloop();
3798     }
3799 
3800     android_log_destroy(&ctx);
3801 
3802     ALOGI("exiting");
3803     return 0;
3804 }
3805