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