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 <arpa/inet.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <sched.h>
23 #include <signal.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/socket.h>
31 #include <sys/types.h>
32 #include <time.h>
33 #include <unistd.h>
34
35 #include <cutils/properties.h>
36 #include <cutils/sockets.h>
37 #include <log/log.h>
38 #include <processgroup/processgroup.h>
39
40 #ifndef __unused
41 #define __unused __attribute__((__unused__))
42 #endif
43
44 #define MEMCG_SYSFS_PATH "/dev/memcg/"
45 #define MEMCG_MEMORY_USAGE "/dev/memcg/memory.usage_in_bytes"
46 #define MEMCG_MEMORYSW_USAGE "/dev/memcg/memory.memsw.usage_in_bytes"
47 #define MEMPRESSURE_WATCH_MEDIUM_LEVEL "medium"
48 #define MEMPRESSURE_WATCH_CRITICAL_LEVEL "critical"
49 #define ZONEINFO_PATH "/proc/zoneinfo"
50 #define LINE_MAX 128
51
52 #define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree"
53 #define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj"
54
55 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
56 #define EIGHT_MEGA (1 << 23)
57
58 enum lmk_cmd {
59 LMK_TARGET,
60 LMK_PROCPRIO,
61 LMK_PROCREMOVE,
62 };
63
64 #define MAX_TARGETS 6
65 /*
66 * longest is LMK_TARGET followed by MAX_TARGETS each minfree and minkillprio
67 * values
68 */
69 #define CTRL_PACKET_MAX (sizeof(int) * (MAX_TARGETS * 2 + 1))
70
71 /* default to old in-kernel interface if no memory pressure events */
72 static int use_inkernel_interface = 1;
73 static bool has_inkernel_module;
74
75 /* memory pressure level medium event */
76 static int mpevfd[2];
77 #define CRITICAL_INDEX 1
78 #define MEDIUM_INDEX 0
79
80 static int medium_oomadj;
81 static int critical_oomadj;
82 static bool debug_process_killing;
83 static bool enable_pressure_upgrade;
84 static int64_t upgrade_pressure;
85 static int64_t downgrade_pressure;
86 static bool is_go_device;
87
88 /* control socket listen and data */
89 static int ctrl_lfd;
90 static int ctrl_dfd = -1;
91 static int ctrl_dfd_reopened; /* did we reopen ctrl conn on this loop? */
92
93 /* 2 memory pressure levels, 1 ctrl listen socket, 1 ctrl data socket */
94 #define MAX_EPOLL_EVENTS 4
95 static int epollfd;
96 static int maxevents;
97
98 /* OOM score values used by both kernel and framework */
99 #define OOM_SCORE_ADJ_MIN (-1000)
100 #define OOM_SCORE_ADJ_MAX 1000
101
102 static int lowmem_adj[MAX_TARGETS];
103 static int lowmem_minfree[MAX_TARGETS];
104 static int lowmem_targets_size;
105
106 struct sysmeminfo {
107 int nr_free_pages;
108 int nr_file_pages;
109 int nr_shmem;
110 int totalreserve_pages;
111 };
112
113 struct adjslot_list {
114 struct adjslot_list *next;
115 struct adjslot_list *prev;
116 };
117
118 struct proc {
119 struct adjslot_list asl;
120 int pid;
121 uid_t uid;
122 int oomadj;
123 struct proc *pidhash_next;
124 };
125
126 #define PIDHASH_SZ 1024
127 static struct proc *pidhash[PIDHASH_SZ];
128 #define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))
129
130 #define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN)
131 static struct adjslot_list procadjslot_list[ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1];
132
133 /* PAGE_SIZE / 1024 */
134 static long page_k;
135
read_all(int fd,char * buf,size_t max_len)136 static ssize_t read_all(int fd, char *buf, size_t max_len)
137 {
138 ssize_t ret = 0;
139
140 while (max_len > 0) {
141 ssize_t r = read(fd, buf, max_len);
142 if (r == 0) {
143 break;
144 }
145 if (r == -1) {
146 return -1;
147 }
148 ret += r;
149 buf += r;
150 max_len -= r;
151 }
152
153 return ret;
154 }
155
pid_lookup(int pid)156 static struct proc *pid_lookup(int pid) {
157 struct proc *procp;
158
159 for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid;
160 procp = procp->pidhash_next)
161 ;
162
163 return procp;
164 }
165
adjslot_insert(struct adjslot_list * head,struct adjslot_list * new)166 static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new)
167 {
168 struct adjslot_list *next = head->next;
169 new->prev = head;
170 new->next = next;
171 next->prev = new;
172 head->next = new;
173 }
174
adjslot_remove(struct adjslot_list * old)175 static void adjslot_remove(struct adjslot_list *old)
176 {
177 struct adjslot_list *prev = old->prev;
178 struct adjslot_list *next = old->next;
179 next->prev = prev;
180 prev->next = next;
181 }
182
adjslot_tail(struct adjslot_list * head)183 static struct adjslot_list *adjslot_tail(struct adjslot_list *head) {
184 struct adjslot_list *asl = head->prev;
185
186 return asl == head ? NULL : asl;
187 }
188
proc_slot(struct proc * procp)189 static void proc_slot(struct proc *procp) {
190 int adjslot = ADJTOSLOT(procp->oomadj);
191
192 adjslot_insert(&procadjslot_list[adjslot], &procp->asl);
193 }
194
proc_unslot(struct proc * procp)195 static void proc_unslot(struct proc *procp) {
196 adjslot_remove(&procp->asl);
197 }
198
proc_insert(struct proc * procp)199 static void proc_insert(struct proc *procp) {
200 int hval = pid_hashfn(procp->pid);
201
202 procp->pidhash_next = pidhash[hval];
203 pidhash[hval] = procp;
204 proc_slot(procp);
205 }
206
pid_remove(int pid)207 static int pid_remove(int pid) {
208 int hval = pid_hashfn(pid);
209 struct proc *procp;
210 struct proc *prevp;
211
212 for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid;
213 procp = procp->pidhash_next)
214 prevp = procp;
215
216 if (!procp)
217 return -1;
218
219 if (!prevp)
220 pidhash[hval] = procp->pidhash_next;
221 else
222 prevp->pidhash_next = procp->pidhash_next;
223
224 proc_unslot(procp);
225 free(procp);
226 return 0;
227 }
228
writefilestring(char * path,char * s)229 static void writefilestring(char *path, char *s) {
230 int fd = open(path, O_WRONLY | O_CLOEXEC);
231 int len = strlen(s);
232 int ret;
233
234 if (fd < 0) {
235 ALOGE("Error opening %s; errno=%d", path, errno);
236 return;
237 }
238
239 ret = write(fd, s, len);
240 if (ret < 0) {
241 ALOGE("Error writing %s; errno=%d", path, errno);
242 } else if (ret < len) {
243 ALOGE("Short write on %s; length=%d", path, ret);
244 }
245
246 close(fd);
247 }
248
cmd_procprio(int pid,int uid,int oomadj)249 static void cmd_procprio(int pid, int uid, int oomadj) {
250 struct proc *procp;
251 char path[80];
252 char val[20];
253 int soft_limit_mult;
254
255 if (oomadj < OOM_SCORE_ADJ_MIN || oomadj > OOM_SCORE_ADJ_MAX) {
256 ALOGE("Invalid PROCPRIO oomadj argument %d", oomadj);
257 return;
258 }
259
260 snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", pid);
261 snprintf(val, sizeof(val), "%d", oomadj);
262 writefilestring(path, val);
263
264 if (use_inkernel_interface)
265 return;
266
267 if (oomadj >= 900) {
268 soft_limit_mult = 0;
269 } else if (oomadj >= 800) {
270 soft_limit_mult = 0;
271 } else if (oomadj >= 700) {
272 soft_limit_mult = 0;
273 } else if (oomadj >= 600) {
274 // Launcher should be perceptible, don't kill it.
275 oomadj = 200;
276 soft_limit_mult = 1;
277 } else if (oomadj >= 500) {
278 soft_limit_mult = 0;
279 } else if (oomadj >= 400) {
280 soft_limit_mult = 0;
281 } else if (oomadj >= 300) {
282 soft_limit_mult = 1;
283 } else if (oomadj >= 200) {
284 soft_limit_mult = 2;
285 } else if (oomadj >= 100) {
286 soft_limit_mult = 10;
287 } else if (oomadj >= 0) {
288 soft_limit_mult = 20;
289 } else {
290 // Persistent processes will have a large
291 // soft limit 512MB.
292 soft_limit_mult = 64;
293 }
294
295 snprintf(path, sizeof(path), "/dev/memcg/apps/uid_%d/pid_%d/memory.soft_limit_in_bytes", uid, pid);
296 snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA);
297 writefilestring(path, val);
298
299 procp = pid_lookup(pid);
300 if (!procp) {
301 procp = malloc(sizeof(struct proc));
302 if (!procp) {
303 // Oh, the irony. May need to rebuild our state.
304 return;
305 }
306
307 procp->pid = pid;
308 procp->uid = uid;
309 procp->oomadj = oomadj;
310 proc_insert(procp);
311 } else {
312 proc_unslot(procp);
313 procp->oomadj = oomadj;
314 proc_slot(procp);
315 }
316 }
317
cmd_procremove(int pid)318 static void cmd_procremove(int pid) {
319 if (use_inkernel_interface)
320 return;
321
322 pid_remove(pid);
323 }
324
cmd_target(int ntargets,int * params)325 static void cmd_target(int ntargets, int *params) {
326 int i;
327
328 if (ntargets > (int)ARRAY_SIZE(lowmem_adj))
329 return;
330
331 for (i = 0; i < ntargets; i++) {
332 lowmem_minfree[i] = ntohl(*params++);
333 lowmem_adj[i] = ntohl(*params++);
334 }
335
336 lowmem_targets_size = ntargets;
337
338 if (has_inkernel_module) {
339 char minfreestr[128];
340 char killpriostr[128];
341
342 minfreestr[0] = '\0';
343 killpriostr[0] = '\0';
344
345 for (i = 0; i < lowmem_targets_size; i++) {
346 char val[40];
347
348 if (i) {
349 strlcat(minfreestr, ",", sizeof(minfreestr));
350 strlcat(killpriostr, ",", sizeof(killpriostr));
351 }
352
353 snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_minfree[i] : 0);
354 strlcat(minfreestr, val, sizeof(minfreestr));
355 snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_adj[i] : 0);
356 strlcat(killpriostr, val, sizeof(killpriostr));
357 }
358
359 writefilestring(INKERNEL_MINFREE_PATH, minfreestr);
360 writefilestring(INKERNEL_ADJ_PATH, killpriostr);
361 }
362 }
363
ctrl_data_close(void)364 static void ctrl_data_close(void) {
365 ALOGI("Closing Activity Manager data connection");
366 close(ctrl_dfd);
367 ctrl_dfd = -1;
368 maxevents--;
369 }
370
ctrl_data_read(char * buf,size_t bufsz)371 static int ctrl_data_read(char *buf, size_t bufsz) {
372 int ret = 0;
373
374 ret = read(ctrl_dfd, buf, bufsz);
375
376 if (ret == -1) {
377 ALOGE("control data socket read failed; errno=%d", errno);
378 } else if (ret == 0) {
379 ALOGE("Got EOF on control data socket");
380 ret = -1;
381 }
382
383 return ret;
384 }
385
ctrl_command_handler(void)386 static void ctrl_command_handler(void) {
387 int ibuf[CTRL_PACKET_MAX / sizeof(int)];
388 int len;
389 int cmd = -1;
390 int nargs;
391 int targets;
392
393 len = ctrl_data_read((char *)ibuf, CTRL_PACKET_MAX);
394 if (len <= 0)
395 return;
396
397 nargs = len / sizeof(int) - 1;
398 if (nargs < 0)
399 goto wronglen;
400
401 cmd = ntohl(ibuf[0]);
402
403 switch(cmd) {
404 case LMK_TARGET:
405 targets = nargs / 2;
406 if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj))
407 goto wronglen;
408 cmd_target(targets, &ibuf[1]);
409 break;
410 case LMK_PROCPRIO:
411 if (nargs != 3)
412 goto wronglen;
413 cmd_procprio(ntohl(ibuf[1]), ntohl(ibuf[2]), ntohl(ibuf[3]));
414 break;
415 case LMK_PROCREMOVE:
416 if (nargs != 1)
417 goto wronglen;
418 cmd_procremove(ntohl(ibuf[1]));
419 break;
420 default:
421 ALOGE("Received unknown command code %d", cmd);
422 return;
423 }
424
425 return;
426
427 wronglen:
428 ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len);
429 }
430
ctrl_data_handler(uint32_t events)431 static void ctrl_data_handler(uint32_t events) {
432 if (events & EPOLLHUP) {
433 ALOGI("ActivityManager disconnected");
434 if (!ctrl_dfd_reopened)
435 ctrl_data_close();
436 } else if (events & EPOLLIN) {
437 ctrl_command_handler();
438 }
439 }
440
ctrl_connect_handler(uint32_t events __unused)441 static void ctrl_connect_handler(uint32_t events __unused) {
442 struct epoll_event epev;
443
444 if (ctrl_dfd >= 0) {
445 ctrl_data_close();
446 ctrl_dfd_reopened = 1;
447 }
448
449 ctrl_dfd = accept(ctrl_lfd, NULL, NULL);
450
451 if (ctrl_dfd < 0) {
452 ALOGE("lmkd control socket accept failed; errno=%d", errno);
453 return;
454 }
455
456 ALOGI("ActivityManager connected");
457 maxevents++;
458 epev.events = EPOLLIN;
459 epev.data.ptr = (void *)ctrl_data_handler;
460 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_dfd, &epev) == -1) {
461 ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno);
462 ctrl_data_close();
463 return;
464 }
465 }
466
zoneinfo_parse_protection(char * cp)467 static int zoneinfo_parse_protection(char *cp) {
468 int max = 0;
469 int zoneval;
470 char *save_ptr;
471
472 for (cp = strtok_r(cp, "(), ", &save_ptr); cp; cp = strtok_r(NULL, "), ", &save_ptr)) {
473 zoneval = strtol(cp, &cp, 0);
474 if (zoneval > max)
475 max = zoneval;
476 }
477
478 return max;
479 }
480
zoneinfo_parse_line(char * line,struct sysmeminfo * mip)481 static void zoneinfo_parse_line(char *line, struct sysmeminfo *mip) {
482 char *cp = line;
483 char *ap;
484 char *save_ptr;
485
486 cp = strtok_r(line, " ", &save_ptr);
487 if (!cp)
488 return;
489
490 ap = strtok_r(NULL, " ", &save_ptr);
491 if (!ap)
492 return;
493
494 if (!strcmp(cp, "nr_free_pages"))
495 mip->nr_free_pages += strtol(ap, NULL, 0);
496 else if (!strcmp(cp, "nr_file_pages"))
497 mip->nr_file_pages += strtol(ap, NULL, 0);
498 else if (!strcmp(cp, "nr_shmem"))
499 mip->nr_shmem += strtol(ap, NULL, 0);
500 else if (!strcmp(cp, "high"))
501 mip->totalreserve_pages += strtol(ap, NULL, 0);
502 else if (!strcmp(cp, "protection:"))
503 mip->totalreserve_pages += zoneinfo_parse_protection(ap);
504 }
505
zoneinfo_parse(struct sysmeminfo * mip)506 static int zoneinfo_parse(struct sysmeminfo *mip) {
507 int fd;
508 ssize_t size;
509 char buf[PAGE_SIZE];
510 char *save_ptr;
511 char *line;
512
513 memset(mip, 0, sizeof(struct sysmeminfo));
514
515 fd = open(ZONEINFO_PATH, O_RDONLY | O_CLOEXEC);
516 if (fd == -1) {
517 ALOGE("%s open: errno=%d", ZONEINFO_PATH, errno);
518 return -1;
519 }
520
521 size = read_all(fd, buf, sizeof(buf) - 1);
522 if (size < 0) {
523 ALOGE("%s read: errno=%d", ZONEINFO_PATH, errno);
524 close(fd);
525 return -1;
526 }
527 ALOG_ASSERT((size_t)size < sizeof(buf) - 1, "/proc/zoneinfo too large");
528 buf[size] = 0;
529
530 for (line = strtok_r(buf, "\n", &save_ptr); line; line = strtok_r(NULL, "\n", &save_ptr))
531 zoneinfo_parse_line(line, mip);
532
533 close(fd);
534 return 0;
535 }
536
proc_get_size(int pid)537 static int proc_get_size(int pid) {
538 char path[PATH_MAX];
539 char line[LINE_MAX];
540 int fd;
541 int rss = 0;
542 int total;
543 ssize_t ret;
544
545 snprintf(path, PATH_MAX, "/proc/%d/statm", pid);
546 fd = open(path, O_RDONLY | O_CLOEXEC);
547 if (fd == -1)
548 return -1;
549
550 ret = read_all(fd, line, sizeof(line) - 1);
551 if (ret < 0) {
552 close(fd);
553 return -1;
554 }
555
556 sscanf(line, "%d %d ", &total, &rss);
557 close(fd);
558 return rss;
559 }
560
proc_get_name(int pid)561 static char *proc_get_name(int pid) {
562 char path[PATH_MAX];
563 static char line[LINE_MAX];
564 int fd;
565 char *cp;
566 ssize_t ret;
567
568 snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid);
569 fd = open(path, O_RDONLY | O_CLOEXEC);
570 if (fd == -1)
571 return NULL;
572 ret = read_all(fd, line, sizeof(line) - 1);
573 close(fd);
574 if (ret < 0) {
575 return NULL;
576 }
577
578 cp = strchr(line, ' ');
579 if (cp)
580 *cp = '\0';
581
582 return line;
583 }
584
proc_adj_lru(int oomadj)585 static struct proc *proc_adj_lru(int oomadj) {
586 return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]);
587 }
588
589 /* Kill one process specified by procp. Returns the size of the process killed */
kill_one_process(struct proc * procp,int min_score_adj,bool is_critical)590 static int kill_one_process(struct proc* procp, int min_score_adj, bool is_critical) {
591 int pid = procp->pid;
592 uid_t uid = procp->uid;
593 char *taskname;
594 int tasksize;
595 int r;
596
597 taskname = proc_get_name(pid);
598 if (!taskname) {
599 pid_remove(pid);
600 return -1;
601 }
602
603 tasksize = proc_get_size(pid);
604 if (tasksize <= 0) {
605 pid_remove(pid);
606 return -1;
607 }
608
609 ALOGI(
610 "Killing '%s' (%d), uid %d, adj %d\n"
611 " to free %ldkB because system is under %s memory pressure oom_adj %d\n",
612 taskname, pid, uid, procp->oomadj, tasksize * page_k, is_critical ? "critical" : "medium",
613 min_score_adj);
614 r = kill(pid, SIGKILL);
615 pid_remove(pid);
616
617 if (r) {
618 ALOGE("kill(%d): errno=%d", procp->pid, errno);
619 return -1;
620 } else {
621 return tasksize;
622 }
623 }
624
625 /*
626 * Find a process to kill based on the current (possibly estimated) free memory
627 * and cached memory sizes. Returns the size of the killed processes.
628 */
find_and_kill_process(bool is_critical)629 static int find_and_kill_process(bool is_critical) {
630 int i;
631 int killed_size = 0;
632 int min_score_adj = is_critical ? critical_oomadj : medium_oomadj;
633
634 for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) {
635 struct proc *procp;
636
637 retry:
638 procp = proc_adj_lru(i);
639
640 if (procp) {
641 killed_size = kill_one_process(procp, min_score_adj, is_critical);
642 if (killed_size < 0) {
643 goto retry;
644 } else {
645 return killed_size;
646 }
647 }
648 }
649
650 return 0;
651 }
652
get_memory_usage(const char * path)653 static int64_t get_memory_usage(const char* path) {
654 int ret;
655 int64_t mem_usage;
656 char buf[32];
657 int fd = open(path, O_RDONLY | O_CLOEXEC);
658 if (fd == -1) {
659 ALOGE("%s open: errno=%d", path, errno);
660 return -1;
661 }
662
663 ret = read_all(fd, buf, sizeof(buf) - 1);
664 close(fd);
665 if (ret < 0) {
666 ALOGE("%s error: errno=%d", path, errno);
667 return -1;
668 }
669 sscanf(buf, "%" SCNd64, &mem_usage);
670 if (mem_usage == 0) {
671 ALOGE("No memory!");
672 return -1;
673 }
674 return mem_usage;
675 }
676
mp_event_common(bool is_critical)677 static void mp_event_common(bool is_critical) {
678 int ret;
679 unsigned long long evcount;
680 int index = is_critical ? CRITICAL_INDEX : MEDIUM_INDEX;
681 int64_t mem_usage, memsw_usage;
682 int64_t mem_pressure;
683
684 ret = read(mpevfd[index], &evcount, sizeof(evcount));
685 if (ret < 0)
686 ALOGE("Error reading memory pressure event fd; errno=%d",
687 errno);
688
689 mem_usage = get_memory_usage(MEMCG_MEMORY_USAGE);
690 memsw_usage = get_memory_usage(MEMCG_MEMORYSW_USAGE);
691 if (memsw_usage < 0 || mem_usage < 0) {
692 find_and_kill_process(is_critical);
693 return;
694 }
695
696 // Calculate percent for swappinness.
697 mem_pressure = (mem_usage * 100) / memsw_usage;
698
699 if (enable_pressure_upgrade && !is_critical) {
700 // We are swapping too much.
701 if (mem_pressure < upgrade_pressure) {
702 ALOGI("Event upgraded to critical.");
703 is_critical = true;
704 }
705 }
706
707 // If the pressure is larger than downgrade_pressure lmk will not
708 // kill any process, since enough memory is available.
709 if (mem_pressure > downgrade_pressure) {
710 if (debug_process_killing) {
711 ALOGI("Ignore %s memory pressure", is_critical ? "critical" : "medium");
712 }
713 return;
714 } else if (is_critical && mem_pressure > upgrade_pressure) {
715 if (debug_process_killing) {
716 ALOGI("Downgrade critical memory pressure");
717 }
718 // Downgrade event to medium, since enough memory available.
719 is_critical = false;
720 }
721
722 if (find_and_kill_process(is_critical) == 0) {
723 if (debug_process_killing) {
724 ALOGI("Nothing to kill");
725 }
726 }
727 }
728
mp_event(uint32_t events __unused)729 static void mp_event(uint32_t events __unused) {
730 mp_event_common(false);
731 }
732
mp_event_critical(uint32_t events __unused)733 static void mp_event_critical(uint32_t events __unused) {
734 mp_event_common(true);
735 }
736
init_mp_common(char * levelstr,void * event_handler,bool is_critical)737 static int init_mp_common(char *levelstr, void *event_handler, bool is_critical)
738 {
739 int mpfd;
740 int evfd;
741 int evctlfd;
742 char buf[256];
743 struct epoll_event epev;
744 int ret;
745 int mpevfd_index = is_critical ? CRITICAL_INDEX : MEDIUM_INDEX;
746
747 mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC);
748 if (mpfd < 0) {
749 ALOGI("No kernel memory.pressure_level support (errno=%d)", errno);
750 goto err_open_mpfd;
751 }
752
753 evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY | O_CLOEXEC);
754 if (evctlfd < 0) {
755 ALOGI("No kernel memory cgroup event control (errno=%d)", errno);
756 goto err_open_evctlfd;
757 }
758
759 evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
760 if (evfd < 0) {
761 ALOGE("eventfd failed for level %s; errno=%d", levelstr, errno);
762 goto err_eventfd;
763 }
764
765 ret = snprintf(buf, sizeof(buf), "%d %d %s", evfd, mpfd, levelstr);
766 if (ret >= (ssize_t)sizeof(buf)) {
767 ALOGE("cgroup.event_control line overflow for level %s", levelstr);
768 goto err;
769 }
770
771 ret = write(evctlfd, buf, strlen(buf) + 1);
772 if (ret == -1) {
773 ALOGE("cgroup.event_control write failed for level %s; errno=%d",
774 levelstr, errno);
775 goto err;
776 }
777
778 epev.events = EPOLLIN;
779 epev.data.ptr = event_handler;
780 ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev);
781 if (ret == -1) {
782 ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno);
783 goto err;
784 }
785 maxevents++;
786 mpevfd[mpevfd_index] = evfd;
787 return 0;
788
789 err:
790 close(evfd);
791 err_eventfd:
792 close(evctlfd);
793 err_open_evctlfd:
794 close(mpfd);
795 err_open_mpfd:
796 return -1;
797 }
798
init_mp_medium()799 static int init_mp_medium()
800 {
801 return init_mp_common(MEMPRESSURE_WATCH_MEDIUM_LEVEL, (void *)&mp_event, false);
802 }
803
init_mp_critical()804 static int init_mp_critical()
805 {
806 return init_mp_common(MEMPRESSURE_WATCH_CRITICAL_LEVEL, (void *)&mp_event_critical, true);
807 }
808
init(void)809 static int init(void) {
810 struct epoll_event epev;
811 int i;
812 int ret;
813
814 page_k = sysconf(_SC_PAGESIZE);
815 if (page_k == -1)
816 page_k = PAGE_SIZE;
817 page_k /= 1024;
818
819 epollfd = epoll_create(MAX_EPOLL_EVENTS);
820 if (epollfd == -1) {
821 ALOGE("epoll_create failed (errno=%d)", errno);
822 return -1;
823 }
824
825 ctrl_lfd = android_get_control_socket("lmkd");
826 if (ctrl_lfd < 0) {
827 ALOGE("get lmkd control socket failed");
828 return -1;
829 }
830
831 ret = listen(ctrl_lfd, 1);
832 if (ret < 0) {
833 ALOGE("lmkd control socket listen failed (errno=%d)", errno);
834 return -1;
835 }
836
837 epev.events = EPOLLIN;
838 epev.data.ptr = (void *)ctrl_connect_handler;
839 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_lfd, &epev) == -1) {
840 ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno);
841 return -1;
842 }
843 maxevents++;
844
845 has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK);
846 use_inkernel_interface = has_inkernel_module && !is_go_device;
847
848 if (use_inkernel_interface) {
849 ALOGI("Using in-kernel low memory killer interface");
850 } else {
851 ret = init_mp_medium();
852 ret |= init_mp_critical();
853 if (ret)
854 ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer");
855 }
856
857 for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) {
858 procadjslot_list[i].next = &procadjslot_list[i];
859 procadjslot_list[i].prev = &procadjslot_list[i];
860 }
861
862 return 0;
863 }
864
mainloop(void)865 static void mainloop(void) {
866 while (1) {
867 struct epoll_event events[maxevents];
868 int nevents;
869 int i;
870
871 ctrl_dfd_reopened = 0;
872 nevents = epoll_wait(epollfd, events, maxevents, -1);
873
874 if (nevents == -1) {
875 if (errno == EINTR)
876 continue;
877 ALOGE("epoll_wait failed (errno=%d)", errno);
878 continue;
879 }
880
881 for (i = 0; i < nevents; ++i) {
882 if (events[i].events & EPOLLERR)
883 ALOGD("EPOLLERR on event #%d", i);
884 if (events[i].data.ptr)
885 (*(void (*)(uint32_t))events[i].data.ptr)(events[i].events);
886 }
887 }
888 }
889
main(int argc __unused,char ** argv __unused)890 int main(int argc __unused, char **argv __unused) {
891 struct sched_param param = {
892 .sched_priority = 1,
893 };
894
895 medium_oomadj = property_get_int32("ro.lmk.medium", 800);
896 critical_oomadj = property_get_int32("ro.lmk.critical", 0);
897 debug_process_killing = property_get_bool("ro.lmk.debug", false);
898 enable_pressure_upgrade = property_get_bool("ro.lmk.critical_upgrade", false);
899 upgrade_pressure = (int64_t)property_get_int32("ro.lmk.upgrade_pressure", 50);
900 downgrade_pressure = (int64_t)property_get_int32("ro.lmk.downgrade_pressure", 60);
901 is_go_device = property_get_bool("ro.config.low_ram", false);
902
903 mlockall(MCL_FUTURE);
904 sched_setscheduler(0, SCHED_FIFO, ¶m);
905 if (!init())
906 mainloop();
907
908 ALOGI("exiting");
909 return 0;
910 }
911