blob: 64051875ab89708dd2e91ce02b74152694a66921 [file] [log] [blame]
Todd Poynorc58c5142013-07-09 19:35:14 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "lowmemorykiller"
18
19#include <errno.h>
Robert Beneac72b2932017-08-21 15:18:31 -070020#include <inttypes.h>
Suren Baghdasaryanbb7747b2018-03-20 16:03:29 -070021#include <pwd.h>
Mark Salyzyna1f5b862016-10-17 14:28:00 -070022#include <sched.h>
Suren Baghdasaryanf584fff2018-03-20 13:53:17 -070023#include <stdbool.h>
Todd Poynorc58c5142013-07-09 19:35:14 -070024#include <stdlib.h>
25#include <string.h>
Mark Salyzyneb062742014-04-30 13:36:35 -070026#include <sys/cdefs.h>
Todd Poynorc58c5142013-07-09 19:35:14 -070027#include <sys/epoll.h>
28#include <sys/eventfd.h>
Colin Crossc4059002014-07-11 17:15:44 -070029#include <sys/mman.h>
Josh Gao84623be2021-03-18 17:16:08 -070030#include <sys/pidfd.h>
Todd Poynorc58c5142013-07-09 19:35:14 -070031#include <sys/socket.h>
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -070032#include <sys/syscall.h>
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -080033#include <sys/sysinfo.h>
Suren Baghdasaryan1ed0db12018-07-24 17:13:06 -070034#include <time.h>
Mark Salyzyneb062742014-04-30 13:36:35 -070035#include <unistd.h>
36
Bart Van Assche80a3dba2022-02-02 23:51:35 +000037#include <algorithm>
Bart Van Assche54506792022-02-02 23:57:01 +000038#include <array>
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -080039#include <shared_mutex>
40
Robert Benea57397dc2017-07-31 17:15:20 -070041#include <cutils/properties.h>
Todd Poynorc58c5142013-07-09 19:35:14 -070042#include <cutils/sockets.h>
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -070043#include <liblmkd_utils.h>
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -080044#include <lmkd.h>
Mark Salyzyn6a63fde2017-01-10 13:19:54 -080045#include <log/log.h>
Suren Baghdasaryan08bfa982018-07-26 16:34:27 -070046#include <log/log_event_list.h>
Suren Baghdasaryan1ed0db12018-07-24 17:13:06 -070047#include <log/log_time.h>
Suren Baghdasaryan945658a2019-10-18 11:16:52 -070048#include <private/android_filesystem_config.h>
Bart Van Asscheb4d26bb2022-02-17 21:31:51 +000049#include <processgroup/processgroup.h>
Suren Baghdasaryan55e31502019-01-08 12:54:48 -080050#include <psi/psi.h>
Mark Salyzyneb062742014-04-30 13:36:35 -070051
Suren Baghdasaryan7c3addb2021-06-11 12:12:56 -070052#include "reaper.h"
Yao Chen337606c2018-05-02 11:19:27 -070053#include "statslog.h"
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -080054#include "watchdog.h"
Rajeev Kumar4aba9152018-01-31 17:54:56 -080055
Suren Baghdasaryan940e7cf2021-05-27 18:15:44 -070056#define BPF_FD_JUST_USE_INT
57#include "BpfSyscallWrappers.h"
58
Suren Baghdasaryan03e19872018-01-04 10:43:58 -080059/*
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
Suren Baghdasaryan34928bb2021-07-29 17:02:51 -070068static inline void trace_kill_start(int pid, const char *desc) {
69 ATRACE_INT("kill_one_process", pid);
70 ATRACE_BEGIN(desc);
71}
72
73static inline void trace_kill_end() {
74 ATRACE_END();
75 ATRACE_INT("kill_one_process", 0);
76}
Suren Baghdasaryan03e19872018-01-04 10:43:58 -080077
78#else /* LMKD_TRACE_KILLS */
79
Suren Baghdasaryan34928bb2021-07-29 17:02:51 -070080static inline void trace_kill_start(int, const char *) {}
81static inline void trace_kill_end() {}
Suren Baghdasaryan03e19872018-01-04 10:43:58 -080082
83#endif /* LMKD_TRACE_KILLS */
84
Mark Salyzyneb062742014-04-30 13:36:35 -070085#ifndef __unused
86#define __unused __attribute__((__unused__))
87#endif
Todd Poynorc58c5142013-07-09 19:35:14 -070088
Suren Baghdasaryand28a9732018-04-13 13:11:51 -070089#define ZONEINFO_PATH "/proc/zoneinfo"
90#define MEMINFO_PATH "/proc/meminfo"
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -070091#define VMSTAT_PATH "/proc/vmstat"
Suren Baghdasaryan3ee11d42019-07-02 15:52:07 -070092#define PROC_STATUS_TGID_FIELD "Tgid:"
Ioannis Ilkos279268a2020-08-01 10:50:40 +010093#define PROC_STATUS_RSS_FIELD "VmRSS:"
94#define PROC_STATUS_SWAP_FIELD "VmSwap:"
Todd Poynorc58c5142013-07-09 19:35:14 -070095#define LINE_MAX 128
96
Suren Baghdasaryan970a26a2019-09-19 15:27:21 -070097#define PERCEPTIBLE_APP_ADJ 200
98
Suren Baghdasaryan08bfa982018-07-26 16:34:27 -070099/* Android Logger event logtags (see event.logtags) */
Suren Baghdasaryan12cacae2019-09-16 12:06:30 -0700100#define KILLINFO_LOG_TAG 10195355
Suren Baghdasaryan08bfa982018-07-26 16:34:27 -0700101
Mark Salyzyna00ccd82018-04-09 09:50:32 -0700102/* gid containing AID_SYSTEM required */
Todd Poynorc58c5142013-07-09 19:35:14 -0700103#define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree"
104#define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj"
105
Robert Benea58d6a132017-06-01 16:32:31 -0700106#define EIGHT_MEGA (1 << 23)
Todd Poynorc58c5142013-07-09 19:35:14 -0700107
Suren Baghdasaryan1ed0db12018-07-24 17:13:06 -0700108#define TARGET_UPDATE_MIN_INTERVAL_MS 1000
Martin Liu1f72f5f2020-08-21 13:18:50 +0800109#define THRASHING_RESET_INTERVAL_MS 1000
Suren Baghdasaryan1ed0db12018-07-24 17:13:06 -0700110
111#define NS_PER_MS (NS_PER_SEC / MS_PER_SEC)
Suren Baghdasaryan55e31502019-01-08 12:54:48 -0800112#define US_PER_MS (US_PER_SEC / MS_PER_SEC)
Suren Baghdasaryan1ed0db12018-07-24 17:13:06 -0700113
Suren Baghdasaryanbb7747b2018-03-20 16:03:29 -0700114/* Defined as ProcessList.SYSTEM_ADJ in ProcessList.java */
115#define SYSTEM_ADJ (-900)
116
Greg Kaiserf5b1d142018-03-23 14:16:12 -0700117#define STRINGIFY(x) STRINGIFY_INTERNAL(x)
118#define STRINGIFY_INTERNAL(x) #x
119
Suren Baghdasaryan55e31502019-01-08 12:54:48 -0800120/*
Suren Baghdasaryand0a80042021-08-03 15:40:23 -0700121 * 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/*
Suren Baghdasaryan55e31502019-01-08 12:54:48 -0800131 * 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
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -0700137/* 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
Suren Baghdasaryan55e31502019-01-08 12:54:48 -0800141
Suren Baghdasaryan53be36e2018-09-05 15:46:32 -0700142#define FAIL_REPORT_RLIMIT_MS 1000
143
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -0700144/*
145 * System property defaults
146 */
147/* ro.lmk.swap_free_low_percentage property defaults */
Suren Baghdasaryanfb1f5922020-05-19 13:07:23 -0700148#define DEF_LOW_SWAP 10
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -0700149/* 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
Suren Baghdasaryan2f88e152019-07-15 15:42:09 -0700155/* 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
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -0700160
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -0700161#define LMKD_REINIT_PROP "lmkd.reinit"
162
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -0800163#define WATCHDOG_TIMEOUT_SEC 2
164
Todd Poynorc58c5142013-07-09 19:35:14 -0700165/* default to old in-kernel interface if no memory pressure events */
Mark Salyzyn5cc80b32018-03-21 12:24:58 -0700166static bool use_inkernel_interface = true;
Robert Benea7878c9b2017-09-11 16:53:28 -0700167static bool has_inkernel_module;
Todd Poynorc58c5142013-07-09 19:35:14 -0700168
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -0800169/* memory pressure levels */
170enum vmpressure_level {
171 VMPRESS_LEVEL_LOW = 0,
172 VMPRESS_LEVEL_MEDIUM,
173 VMPRESS_LEVEL_CRITICAL,
174 VMPRESS_LEVEL_COUNT
175};
Todd Poynorc58c5142013-07-09 19:35:14 -0700176
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -0800177static const char *level_name[] = {
178 "low",
179 "medium",
180 "critical"
181};
182
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -0800183struct {
Suren Baghdasaryane0f3dd12018-04-13 13:41:12 -0700184 int64_t min_nr_free_pages; /* recorded but not used yet */
185 int64_t max_nr_free_pages;
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -0800186} low_pressure_mem = { -1, -1 };
187
Suren Baghdasaryan55e31502019-01-08 12:54:48 -0800188struct psi_threshold {
189 enum psi_stall_type stall_type;
190 int threshold_ms;
191};
192
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -0800193static int level_oomadj[VMPRESS_LEVEL_COUNT];
Suren Baghdasaryan3e1a8492018-01-04 09:16:21 -0800194static int mpevfd[VMPRESS_LEVEL_COUNT] = { -1, -1, -1 };
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -0700195static bool pidfd_supported;
196static int last_kill_pid_or_fd = -1;
197static struct timespec last_kill_tm;
198
199/* lmkd configurable parameters */
Robert Beneac72b2932017-08-21 15:18:31 -0700200static bool debug_process_killing;
201static bool enable_pressure_upgrade;
202static int64_t upgrade_pressure;
Robert Benea3be16142017-09-13 15:20:30 -0700203static int64_t downgrade_pressure;
Suren Baghdasaryand1d59f82018-04-13 11:45:38 -0700204static bool low_ram_device;
Suren Baghdasaryaneb7c5492017-12-08 13:17:06 -0800205static bool kill_heaviest_task;
Suren Baghdasaryan30854e72018-01-17 17:28:01 -0800206static unsigned long kill_timeout_ms;
Suren Baghdasaryan2c4611a2018-04-13 13:53:43 -0700207static bool use_minfree_levels;
Suren Baghdasaryan8389fdb2018-06-19 18:38:12 -0700208static bool per_app_memcg;
Vic Yang65680692018-08-07 10:18:22 -0700209static int swap_free_low_percentage;
Suren Baghdasaryan2f88e152019-07-15 15:42:09 -0700210static int psi_partial_stall_ms;
211static int psi_complete_stall_ms;
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -0700212static int thrashing_limit_pct;
213static int thrashing_limit_decay_pct;
Suren Baghdasaryan0142b3c2021-03-03 11:11:09 -0800214static int thrashing_critical_pct;
Suren Baghdasaryan51ee4c52020-04-21 12:27:01 -0700215static int swap_util_max;
Suren Baghdasaryan11221d42021-07-14 17:57:52 -0700216static int64_t filecache_min_kb;
Suren Baghdasaryan5ae47a92022-02-10 21:10:23 -0800217static int64_t stall_limit_critical;
Suren Baghdasaryan55e31502019-01-08 12:54:48 -0800218static bool use_psi_monitors = false;
Jing Ji5c480962019-12-04 09:22:05 -0800219static int kpoll_fd;
Suren Baghdasaryan55e31502019-01-08 12:54:48 -0800220static 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};
Robert Benea57397dc2017-07-31 17:15:20 -0700225
Suren Baghdasaryan08bfa982018-07-26 16:34:27 -0700226static android_log_context ctx;
Suren Baghdasaryan7c3addb2021-06-11 12:12:56 -0700227static Reaper reaper;
228static int reaper_comm_fd[2];
Suren Baghdasaryan08bfa982018-07-26 16:34:27 -0700229
Suren Baghdasaryane12a0672019-07-15 14:50:49 -0700230enum polling_update {
231 POLLING_DO_NOT_CHANGE,
232 POLLING_START,
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -0700233 POLLING_PAUSE,
234 POLLING_RESUME,
Suren Baghdasaryane12a0672019-07-15 14:50:49 -0700235};
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 */
243struct polling_params {
244 struct event_handler_info* poll_handler;
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -0700245 struct event_handler_info* paused_handler;
Suren Baghdasaryane12a0672019-07-15 14:50:49 -0700246 struct timespec poll_start_tm;
247 struct timespec last_poll_tm;
248 int polling_interval_ms;
249 enum polling_update update;
250};
251
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -0800252/* data required to handle events */
253struct event_handler_info {
254 int data;
Suren Baghdasaryane12a0672019-07-15 14:50:49 -0700255 void (*handler)(int data, uint32_t events, struct polling_params *poll_params);
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -0800256};
Todd Poynorc58c5142013-07-09 19:35:14 -0700257
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -0800258/* data required to handle socket events */
259struct sock_event_handler_info {
260 int sock;
Suren Baghdasaryan945658a2019-10-18 11:16:52 -0700261 pid_t pid;
Suren Baghdasaryan36baf442019-12-23 11:37:34 -0800262 uint32_t async_event_mask;
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -0800263 struct event_handler_info handler_info;
264};
265
Suren Baghdasaryanf2cbefd2019-10-21 17:59:22 -0700266/* max supported number of data connections (AMS, init, tests) */
267#define MAX_DATA_CONN 3
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -0800268
269/* socket event handler data */
270static struct sock_event_handler_info ctrl_sock;
271static struct sock_event_handler_info data_sock[MAX_DATA_CONN];
272
273/* vmpressure event handler data */
274static struct event_handler_info vmpressure_hinfo[VMPRESS_LEVEL_COUNT];
275
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -0700276/*
Suren Baghdasaryanf2cbefd2019-10-21 17:59:22 -0700277 * 1 ctrl listen socket, 3 ctrl data socket, 3 memory pressure levels,
Suren Baghdasaryan7c3addb2021-06-11 12:12:56 -0700278 * 1 lmk events + 1 fd to wait for process death + 1 fd to receive kill failure notifications
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -0700279 */
Suren Baghdasaryan7c3addb2021-06-11 12:12:56 -0700280#define MAX_EPOLL_EVENTS (1 + MAX_DATA_CONN + VMPRESS_LEVEL_COUNT + 1 + 1 + 1)
Todd Poynorc58c5142013-07-09 19:35:14 -0700281static int epollfd;
282static int maxevents;
283
Chong Zhang1cd12b52015-10-14 16:19:53 -0700284/* OOM score values used by both kernel and framework */
Todd Poynora08c1722013-09-16 19:26:47 -0700285#define OOM_SCORE_ADJ_MIN (-1000)
286#define OOM_SCORE_ADJ_MAX 1000
287
Bart Van Assche54506792022-02-02 23:57:01 +0000288static std::array<int, MAX_TARGETS> lowmem_adj;
289static std::array<int, MAX_TARGETS> lowmem_minfree;
Todd Poynorc58c5142013-07-09 19:35:14 -0700290static int lowmem_targets_size;
291
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700292/* Fields to parse in /proc/zoneinfo */
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -0700293/* zoneinfo per-zone fields */
294enum 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
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700302};
303
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -0700304static const char* const zoneinfo_zone_field_names[ZI_ZONE_FIELD_COUNT] = {
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700305 "nr_free_pages",
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -0700306 "min",
307 "low",
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700308 "high",
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -0700309 "present",
310 "nr_free_cma",
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700311};
312
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -0700313/* zoneinfo per-zone special fields */
314enum zoneinfo_zone_spec_field {
315 ZI_ZONE_SPEC_PROTECTION = 0,
316 ZI_ZONE_SPEC_PAGESETS,
317 ZI_ZONE_SPEC_FIELD_COUNT,
318};
319
320static 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
328union zoneinfo_zone_fields {
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700329 struct {
330 int64_t nr_free_pages;
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -0700331 int64_t min;
332 int64_t low;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700333 int64_t high;
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -0700334 int64_t present;
335 int64_t nr_free_cma;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700336 } field;
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -0700337 int64_t arr[ZI_ZONE_FIELD_COUNT];
338};
339
340struct 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 */
347enum zoneinfo_node_field {
348 ZI_NODE_NR_INACTIVE_FILE = 0,
349 ZI_NODE_NR_ACTIVE_FILE,
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -0700350 ZI_NODE_FIELD_COUNT
351};
352
353static const char* const zoneinfo_node_field_names[ZI_NODE_FIELD_COUNT] = {
354 "nr_inactive_file",
355 "nr_active_file",
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -0700356};
357
358union zoneinfo_node_fields {
359 struct {
360 int64_t nr_inactive_file;
361 int64_t nr_active_file;
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -0700362 } field;
363 int64_t arr[ZI_NODE_FIELD_COUNT];
364};
365
366struct 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
376struct 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;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700382};
383
384/* Fields to parse in /proc/meminfo */
385enum meminfo_field {
386 MI_NR_FREE_PAGES = 0,
387 MI_CACHED,
388 MI_SWAP_CACHED,
389 MI_BUFFERS,
390 MI_SHMEM,
391 MI_UNEVICTABLE,
Vic Yang65680692018-08-07 10:18:22 -0700392 MI_TOTAL_SWAP,
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700393 MI_FREE_SWAP,
Suren Baghdasaryan08bfa982018-07-26 16:34:27 -0700394 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,
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700405 MI_FIELD_COUNT
406};
407
408static const char* const meminfo_field_names[MI_FIELD_COUNT] = {
409 "MemFree:",
410 "Cached:",
411 "SwapCached:",
412 "Buffers:",
413 "Shmem:",
414 "Unevictable:",
Vic Yang65680692018-08-07 10:18:22 -0700415 "SwapTotal:",
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700416 "SwapFree:",
Suren Baghdasaryan08bfa982018-07-26 16:34:27 -0700417 "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:",
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700428};
429
430union 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;
Vic Yang65680692018-08-07 10:18:22 -0700438 int64_t total_swap;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700439 int64_t free_swap;
Suren Baghdasaryan08bfa982018-07-26 16:34:27 -0700440 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;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700451 /* fields below are calculated rather than read from the file */
452 int64_t nr_file_pages;
Suren Baghdasaryan940e7cf2021-05-27 18:15:44 -0700453 int64_t total_gpu_kb;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700454 } field;
455 int64_t arr[MI_FIELD_COUNT];
456};
457
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -0700458/* Fields to parse in /proc/vmstat */
459enum vmstat_field {
460 VS_FREE_PAGES,
461 VS_INACTIVE_FILE,
462 VS_ACTIVE_FILE,
463 VS_WORKINGSET_REFAULT,
Suren Baghdasaryandc60f972020-12-14 13:38:48 -0800464 VS_WORKINGSET_REFAULT_FILE,
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -0700465 VS_PGSCAN_KSWAPD,
466 VS_PGSCAN_DIRECT,
467 VS_PGSCAN_DIRECT_THROTTLE,
468 VS_FIELD_COUNT
469};
470
471static const char* const vmstat_field_names[MI_FIELD_COUNT] = {
472 "nr_free_pages",
473 "nr_inactive_file",
474 "nr_active_file",
475 "workingset_refault",
Suren Baghdasaryandc60f972020-12-14 13:38:48 -0800476 "workingset_refault_file",
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -0700477 "pgscan_kswapd",
478 "pgscan_direct",
479 "pgscan_direct_throttle",
480};
481
482union 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;
Suren Baghdasaryandc60f972020-12-14 13:38:48 -0800488 int64_t workingset_refault_file;
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -0700489 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
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700496enum field_match_result {
497 NO_MATCH,
498 PARSE_FAIL,
499 PARSE_SUCCESS
500};
501
Todd Poynorc58c5142013-07-09 19:35:14 -0700502struct adjslot_list {
503 struct adjslot_list *next;
504 struct adjslot_list *prev;
505};
506
507struct proc {
508 struct adjslot_list asl;
509 int pid;
Suren Baghdasaryana10157c2019-07-19 10:55:39 -0700510 int pidfd;
Colin Cross748d2182014-06-13 14:52:43 -0700511 uid_t uid;
Todd Poynorc58c5142013-07-09 19:35:14 -0700512 int oomadj;
Suren Baghdasaryan945658a2019-10-18 11:16:52 -0700513 pid_t reg_pid; /* PID of the process that registered this record */
Suren Baghdasaryana3802f12022-09-27 14:30:34 -0700514 bool valid;
Todd Poynorc58c5142013-07-09 19:35:14 -0700515 struct proc *pidhash_next;
516};
517
Suren Baghdasaryan87966742018-04-13 12:43:41 -0700518struct reread_data {
519 const char* const filename;
520 int fd;
521};
522
Todd Poynorc58c5142013-07-09 19:35:14 -0700523#define PIDHASH_SZ 1024
524static struct proc *pidhash[PIDHASH_SZ];
525#define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))
526
Chih-Hung Hsieheefa2462016-05-19 16:02:22 -0700527#define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN)
Suren Baghdasaryana7394ea2018-10-12 11:07:40 -0700528#define ADJTOSLOT_COUNT (ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1)
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -0800529
530// protects procadjslot_list from concurrent access
531static 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.
Suren Baghdasaryana7394ea2018-10-12 11:07:40 -0700534static 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 */
543static uint8_t killcnt_idx[ADJTOSLOT_COUNT];
544static uint16_t killcnt[MAX_DISTINCT_OOM_ADJ];
545static int killcnt_free_idx = 0;
546static uint32_t killcnt_total = 0;
Todd Poynorc58c5142013-07-09 19:35:14 -0700547
Todd Poynorc58c5142013-07-09 19:35:14 -0700548/* PAGE_SIZE / 1024 */
549static long page_k;
550
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -0700551static void update_props();
552static bool init_monitors();
553static void destroy_monitors();
554
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -0700555static int clamp(int low, int high, int value) {
Bart Van Assche80a3dba2022-02-02 23:51:35 +0000556 return std::max(std::min(value, high), low);
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -0700557}
558
Suren Baghdasaryan87966742018-04-13 12:43:41 -0700559static 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
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -0700569static 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
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700578static 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) {
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -0700582 int i = find_field(cp, field_names, field_count);
583 if (i < 0) {
584 return NO_MATCH;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700585 }
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -0700586 *field_idx = i;
587 return parse_int64(ap, field) ? PARSE_SUCCESS : PARSE_FAIL;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -0700588}
589
Suren Baghdasaryan87966742018-04-13 12:43:41 -0700590/*
591 * Read file content from the beginning up to max_len bytes or EOF
592 * whichever happens first.
593 */
Colin Crossdba1cc62014-07-11 17:53:27 -0700594static ssize_t read_all(int fd, char *buf, size_t max_len)
595{
596 ssize_t ret = 0;
Suren Baghdasaryan87966742018-04-13 12:43:41 -0700597 off_t offset = 0;
Colin Crossdba1cc62014-07-11 17:53:27 -0700598
599 while (max_len > 0) {
Suren Baghdasaryan87966742018-04-13 12:43:41 -0700600 ssize_t r = TEMP_FAILURE_RETRY(pread(fd, buf, max_len, offset));
Colin Crossdba1cc62014-07-11 17:53:27 -0700601 if (r == 0) {
602 break;
603 }
604 if (r == -1) {
605 return -1;
606 }
607 ret += r;
608 buf += r;
Suren Baghdasaryan87966742018-04-13 12:43:41 -0700609 offset += r;
Colin Crossdba1cc62014-07-11 17:53:27 -0700610 max_len -= r;
611 }
612
613 return ret;
614}
615
Suren Baghdasaryan87966742018-04-13 12:43:41 -0700616/*
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 */
Suren Baghdasaryan03cb8362019-07-15 13:35:04 -0700623static 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;
Suren Baghdasaryan87966742018-04-13 12:43:41 -0700627 ssize_t size;
628
629 if (data->fd == -1) {
Suren Baghdasaryan03cb8362019-07-15 13:35:04 -0700630 /* First-time buffer initialization */
Tom Cherry43f3d2b2019-12-04 12:46:57 -0800631 if (!buf && (buf = static_cast<char*>(malloc(buf_size))) == nullptr) {
Suren Baghdasaryan03cb8362019-07-15 13:35:04 -0700632 return NULL;
633 }
634
635 data->fd = TEMP_FAILURE_RETRY(open(data->filename, O_RDONLY | O_CLOEXEC));
636 if (data->fd < 0) {
Suren Baghdasaryan87966742018-04-13 12:43:41 -0700637 ALOGE("%s open: %s", data->filename, strerror(errno));
Suren Baghdasaryan03cb8362019-07-15 13:35:04 -0700638 return NULL;
Suren Baghdasaryan87966742018-04-13 12:43:41 -0700639 }
640 }
641
Suren Baghdasaryan03cb8362019-07-15 13:35:04 -0700642 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 */
Tom Cherry43f3d2b2019-12-04 12:46:57 -0800657 if ((new_buf = static_cast<char*>(realloc(buf, buf_size * 2))) == nullptr) {
Suren Baghdasaryan03cb8362019-07-15 13:35:04 -0700658 errno = ENOMEM;
659 return NULL;
660 }
661 buf = new_buf;
662 buf_size *= 2;
Suren Baghdasaryan87966742018-04-13 12:43:41 -0700663 }
Suren Baghdasaryan87966742018-04-13 12:43:41 -0700664 buf[size] = 0;
665
Suren Baghdasaryan03cb8362019-07-15 13:35:04 -0700666 return buf;
Suren Baghdasaryan87966742018-04-13 12:43:41 -0700667}
668
Jing Ji5c480962019-12-04 09:22:05 -0800669static 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
683static 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
697static 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
714static 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
756static 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 */
775static 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++) {
Suren Baghdasaryan36baf442019-12-23 11:37:34 -0800780 if (data_sock[i].sock >= 0 && data_sock[i].async_event_mask & 1 << LMK_ASYNC_EVENT_KILL) {
Jing Ji5c480962019-12-04 09:22:05 -0800781 ctrl_data_write(i, (char*)packet, len);
782 }
783 }
784}
785
Vova Sharaienkoa92b76b2021-04-24 00:30:06 +0000786/*
787 * Write the kill_stat/memory_stat over the data socket to be propagated via AMS to statsd
788 */
789static 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
805static 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 */
816static 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
Jing Ji5c480962019-12-04 09:22:05 -0800829static 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];
Bart Van Assche5ebc4e82022-02-03 00:05:19 +0000837 int bytes_read = TEMP_FAILURE_RETRY(pread(poll_fd, (void*)rd_buf, sizeof(rd_buf) - 1, 0));
Jing Ji5c480962019-12-04 09:22:05 -0800838 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;
Suren Baghdasaryan3cc1f132020-09-09 20:19:02 -0700863
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);
Jing Ji5c480962019-12-04 09:22:05 -0800873 }
874
875 free(taskname);
876 }
877}
878
879static 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
Todd Poynorc58c5142013-07-09 19:35:14 -0700890static 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
Tom Cherry43f3d2b2019-12-04 12:46:57 -0800900static void adjslot_insert(struct adjslot_list *head, struct adjslot_list *new_element)
Todd Poynorc58c5142013-07-09 19:35:14 -0700901{
902 struct adjslot_list *next = head->next;
Tom Cherry43f3d2b2019-12-04 12:46:57 -0800903 new_element->prev = head;
904 new_element->next = next;
905 next->prev = new_element;
906 head->next = new_element;
Todd Poynorc58c5142013-07-09 19:35:14 -0700907}
908
909static 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
917static 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
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -0800923// Should be modified only from the main thread.
Todd Poynorc58c5142013-07-09 19:35:14 -0700924static void proc_slot(struct proc *procp) {
925 int adjslot = ADJTOSLOT(procp->oomadj);
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -0800926 std::scoped_lock lock(adjslot_list_lock);
Todd Poynorc58c5142013-07-09 19:35:14 -0700927
928 adjslot_insert(&procadjslot_list[adjslot], &procp->asl);
929}
930
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -0800931// Should be modified only from the main thread.
Todd Poynorc58c5142013-07-09 19:35:14 -0700932static void proc_unslot(struct proc *procp) {
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -0800933 std::scoped_lock lock(adjslot_list_lock);
934
Todd Poynorc58c5142013-07-09 19:35:14 -0700935 adjslot_remove(&procp->asl);
936}
937
938static 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
Suren Baghdasaryana3802f12022-09-27 14:30:34 -0700946// Can be called only from the main thread.
Todd Poynorc58c5142013-07-09 19:35:14 -0700947static 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);
Suren Baghdasaryana10157c2019-07-19 10:55:39 -0700965 /*
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 }
Todd Poynorc58c5142013-07-09 19:35:14 -0700972 free(procp);
973 return 0;
974}
975
Suren Baghdasaryana3802f12022-09-27 14:30:34 -0700976static 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
Suren Baghdasaryanf584fff2018-03-20 13:53:17 -0700985/*
986 * Write a string to a file.
987 * Returns false if the file does not exist.
988 */
989static bool writefilestring(const char *path, const char *s,
990 bool err_if_missing) {
Nick Kralevich148d8dd2015-12-18 20:52:37 -0800991 int fd = open(path, O_WRONLY | O_CLOEXEC);
Suren Baghdasaryanf584fff2018-03-20 13:53:17 -0700992 ssize_t len = strlen(s);
993 ssize_t ret;
Todd Poynorc58c5142013-07-09 19:35:14 -0700994
995 if (fd < 0) {
Suren Baghdasaryanf584fff2018-03-20 13:53:17 -0700996 if (err_if_missing) {
997 ALOGE("Error opening %s; errno=%d", path, errno);
998 }
999 return false;
Todd Poynorc58c5142013-07-09 19:35:14 -07001000 }
1001
Suren Baghdasaryanf584fff2018-03-20 13:53:17 -07001002 ret = TEMP_FAILURE_RETRY(write(fd, s, len));
Todd Poynorc58c5142013-07-09 19:35:14 -07001003 if (ret < 0) {
1004 ALOGE("Error writing %s; errno=%d", path, errno);
1005 } else if (ret < len) {
Suren Baghdasaryanf584fff2018-03-20 13:53:17 -07001006 ALOGE("Short write on %s; length=%zd", path, ret);
Todd Poynorc58c5142013-07-09 19:35:14 -07001007 }
1008
1009 close(fd);
Suren Baghdasaryanf584fff2018-03-20 13:53:17 -07001010 return true;
Todd Poynorc58c5142013-07-09 19:35:14 -07001011}
1012
Suren Baghdasaryan1ed0db12018-07-24 17:13:06 -07001013static 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
Ioannis Ilkos279268a2020-08-01 10:50:40 +01001019/* Reads /proc/pid/status into buf. */
1020static bool read_proc_status(int pid, char *buf, size_t buf_sz) {
Suren Baghdasaryan3ee11d42019-07-02 15:52:07 -07001021 char path[PATH_MAX];
Suren Baghdasaryan3ee11d42019-07-02 15:52:07 -07001022 int fd;
1023 ssize_t size;
Suren Baghdasaryan3ee11d42019-07-02 15:52:07 -07001024
1025 snprintf(path, PATH_MAX, "/proc/%d/status", pid);
1026 fd = open(path, O_RDONLY | O_CLOEXEC);
1027 if (fd < 0) {
Ioannis Ilkos279268a2020-08-01 10:50:40 +01001028 return false;
Suren Baghdasaryan3ee11d42019-07-02 15:52:07 -07001029 }
1030
Ioannis Ilkos279268a2020-08-01 10:50:40 +01001031 size = read_all(fd, buf, buf_sz - 1);
1032 close(fd);
Suren Baghdasaryan3ee11d42019-07-02 15:52:07 -07001033 if (size < 0) {
Ioannis Ilkos279268a2020-08-01 10:50:40 +01001034 return false;
Suren Baghdasaryan3ee11d42019-07-02 15:52:07 -07001035 }
1036 buf[size] = 0;
Ioannis Ilkos279268a2020-08-01 10:50:40 +01001037 return true;
1038}
Suren Baghdasaryan3ee11d42019-07-02 15:52:07 -07001039
Ioannis Ilkos279268a2020-08-01 10:50:40 +01001040/* Looks for tag in buf and parses the first integer */
1041static bool parse_status_tag(char *buf, const char *tag, int64_t *out) {
1042 char *pos = buf;
Suren Baghdasaryan3ee11d42019-07-02 15:52:07 -07001043 while (true) {
Ioannis Ilkos279268a2020-08-01 10:50:40 +01001044 pos = strstr(pos, tag);
1045 /* Stop if tag not found or found at the line beginning */
Suren Baghdasaryan3ee11d42019-07-02 15:52:07 -07001046 if (pos == NULL || pos == buf || pos[-1] == '\n') {
1047 break;
1048 }
1049 pos++;
1050 }
1051
1052 if (pos == NULL) {
Ioannis Ilkos279268a2020-08-01 10:50:40 +01001053 return false;
Suren Baghdasaryan3ee11d42019-07-02 15:52:07 -07001054 }
1055
Ioannis Ilkos279268a2020-08-01 10:50:40 +01001056 pos += strlen(tag);
1057 while (*pos == ' ') ++pos;
1058 return parse_int64(pos, out);
Suren Baghdasaryan3ee11d42019-07-02 15:52:07 -07001059}
1060
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07001061static 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 }
Suren Baghdasaryan632f1c52019-10-04 16:06:55 -07001080 line[ret] = '\0';
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07001081
1082 sscanf(line, "%d %d ", &total, &rss);
1083 close(fd);
1084 return rss;
1085}
1086
Suren Baghdasaryan632f1c52019-10-04 16:06:55 -07001087static char *proc_get_name(int pid, char *buf, size_t buf_size) {
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07001088 char path[PATH_MAX];
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07001089 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 }
Suren Baghdasaryan632f1c52019-10-04 16:06:55 -07001099 ret = read_all(fd, buf, buf_size - 1);
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07001100 close(fd);
1101 if (ret < 0) {
1102 return NULL;
1103 }
Suren Baghdasaryan632f1c52019-10-04 16:06:55 -07001104 buf[ret] = '\0';
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07001105
Suren Baghdasaryan632f1c52019-10-04 16:06:55 -07001106 cp = strchr(buf, ' ');
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07001107 if (cp) {
1108 *cp = '\0';
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07001109 }
1110
Suren Baghdasaryan632f1c52019-10-04 16:06:55 -07001111 return buf;
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07001112}
1113
Suren Baghdasaryana93503e2019-10-22 17:12:01 -07001114static void cmd_procprio(LMKD_CTRL_PACKET packet, int field_count, struct ucred *cred) {
Todd Poynorc58c5142013-07-09 19:35:14 -07001115 struct proc *procp;
Suren Baghdasaryan632f1c52019-10-04 16:06:55 -07001116 char path[LINE_MAX];
Todd Poynorc58c5142013-07-09 19:35:14 -07001117 char val[20];
Robert Benea58d6a132017-06-01 16:32:31 -07001118 int soft_limit_mult;
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -08001119 struct lmk_procprio params;
Suren Baghdasaryanbb7747b2018-03-20 16:03:29 -07001120 bool is_system_server;
1121 struct passwd *pwdrec;
Ioannis Ilkos279268a2020-08-01 10:50:40 +01001122 int64_t tgid;
1123 char buf[PAGE_SIZE];
Todd Poynorc58c5142013-07-09 19:35:14 -07001124
Suren Baghdasaryana93503e2019-10-22 17:12:01 -07001125 lmkd_pack_get_procprio(packet, field_count, &params);
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -08001126
1127 if (params.oomadj < OOM_SCORE_ADJ_MIN ||
1128 params.oomadj > OOM_SCORE_ADJ_MAX) {
1129 ALOGE("Invalid PROCPRIO oomadj argument %d", params.oomadj);
Todd Poynorc58c5142013-07-09 19:35:14 -07001130 return;
1131 }
1132
Suren Baghdasaryana93503e2019-10-22 17:12:01 -07001133 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
Suren Baghdasaryan3ee11d42019-07-02 15:52:07 -07001138 /* Check if registered process is a thread group leader */
Ioannis Ilkos279268a2020-08-01 10:50:40 +01001139 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 }
Suren Baghdasaryan3ee11d42019-07-02 15:52:07 -07001145 }
1146
Mark Salyzyna00ccd82018-04-09 09:50:32 -07001147 /* gid containing AID_READPROC required */
1148 /* CAP_SYS_RESOURCE required */
1149 /* CAP_DAC_OVERRIDE required */
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -08001150 snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", params.pid);
1151 snprintf(val, sizeof(val), "%d", params.oomadj);
Suren Baghdasaryanf584fff2018-03-20 13:53:17 -07001152 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 }
Todd Poynorc58c5142013-07-09 19:35:14 -07001158
Mark Salyzyn5cc80b32018-03-21 12:24:58 -07001159 if (use_inkernel_interface) {
Jing Ji5c480962019-12-04 09:22:05 -08001160 stats_store_taskname(params.pid, proc_get_name(params.pid, path, sizeof(path)));
Todd Poynorc58c5142013-07-09 19:35:14 -07001161 return;
Mark Salyzyn5cc80b32018-03-21 12:24:58 -07001162 }
Todd Poynorc58c5142013-07-09 19:35:14 -07001163
Suren Baghdasaryana93503e2019-10-22 17:12:01 -07001164 /* lmkd should not change soft limits for services */
1165 if (params.ptype == PROC_TYPE_APP && per_app_memcg) {
Suren Baghdasaryanb0bda552018-05-18 14:42:00 -07001166 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) {
Srinivas Paladugua453f0b2018-10-09 14:21:10 -07001183 soft_limit_mult = 8;
Suren Baghdasaryanb0bda552018-05-18 14:42:00 -07001184 } 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 }
Robert Benea58d6a132017-06-01 16:32:31 -07001193
Bart Van Asscheb4d26bb2022-02-17 21:31:51 +00001194 std::string path;
1195 if (!CgroupGetAttributePathForTask("MemSoftLimit", params.pid, &path)) {
1196 ALOGE("Querying MemSoftLimit path failed");
1197 return;
1198 }
1199
Suren Baghdasaryanb0bda552018-05-18 14:42:00 -07001200 snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA);
Suren Baghdasaryanbf919ff2018-05-21 19:48:47 -07001201
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);
Bart Van Asscheb4d26bb2022-02-17 21:31:51 +00001209 writefilestring(path.c_str(), val, !is_system_server);
Robert Benea58d6a132017-06-01 16:32:31 -07001210 }
1211
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -08001212 procp = pid_lookup(params.pid);
Todd Poynorc58c5142013-07-09 19:35:14 -07001213 if (!procp) {
Suren Baghdasaryana10157c2019-07-19 10:55:39 -07001214 int pidfd = -1;
1215
1216 if (pidfd_supported) {
Josh Gao84623be2021-03-18 17:16:08 -07001217 pidfd = TEMP_FAILURE_RETRY(pidfd_open(params.pid, 0));
Suren Baghdasaryana10157c2019-07-19 10:55:39 -07001218 if (pidfd < 0) {
1219 ALOGE("pidfd_open for pid %d failed; errno=%d", params.pid, errno);
Todd Poynorc58c5142013-07-09 19:35:14 -07001220 return;
1221 }
Suren Baghdasaryana10157c2019-07-19 10:55:39 -07001222 }
Todd Poynorc58c5142013-07-09 19:35:14 -07001223
Tom Cherry43f3d2b2019-12-04 12:46:57 -08001224 procp = static_cast<struct proc*>(calloc(1, sizeof(struct proc)));
Suren Baghdasaryana10157c2019-07-19 10:55:39 -07001225 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;
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001233 procp->reg_pid = cred->pid;
Suren Baghdasaryana10157c2019-07-19 10:55:39 -07001234 procp->oomadj = params.oomadj;
Suren Baghdasaryana3802f12022-09-27 14:30:34 -07001235 procp->valid = true;
Suren Baghdasaryana10157c2019-07-19 10:55:39 -07001236 proc_insert(procp);
Todd Poynorc58c5142013-07-09 19:35:14 -07001237 } else {
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001238 if (!claim_record(procp, cred->pid)) {
1239 char buf[LINE_MAX];
Suren Baghdasaryan9f1be122021-04-23 13:39:37 -07001240 char *taskname = proc_get_name(cred->pid, buf, sizeof(buf));
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001241 /* Only registrant of the record can remove it */
1242 ALOGE("%s (%d, %d) attempts to modify a process registered by another client",
Suren Baghdasaryan9f1be122021-04-23 13:39:37 -07001243 taskname ? taskname : "A process ", cred->uid, cred->pid);
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001244 return;
1245 }
Todd Poynorc58c5142013-07-09 19:35:14 -07001246 proc_unslot(procp);
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -08001247 procp->oomadj = params.oomadj;
Todd Poynorc58c5142013-07-09 19:35:14 -07001248 proc_slot(procp);
1249 }
1250}
1251
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001252static void cmd_procremove(LMKD_CTRL_PACKET packet, struct ucred *cred) {
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -08001253 struct lmk_procremove params;
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001254 struct proc *procp;
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -08001255
George Burgess IV3b36b902019-10-02 11:22:55 -07001256 lmkd_pack_get_procremove(packet, &params);
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001257
Mark Salyzyn5cc80b32018-03-21 12:24:58 -07001258 if (use_inkernel_interface) {
Jing Ji5c480962019-12-04 09:22:05 -08001259 /*
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);
Todd Poynorc58c5142013-07-09 19:35:14 -07001268 return;
Mark Salyzyn5cc80b32018-03-21 12:24:58 -07001269 }
Todd Poynorc58c5142013-07-09 19:35:14 -07001270
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001271 procp = pid_lookup(params.pid);
1272 if (!procp) {
1273 return;
1274 }
1275
1276 if (!claim_record(procp, cred->pid)) {
1277 char buf[LINE_MAX];
Suren Baghdasaryan9f1be122021-04-23 13:39:37 -07001278 char *taskname = proc_get_name(cred->pid, buf, sizeof(buf));
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001279 /* Only registrant of the record can remove it */
1280 ALOGE("%s (%d, %d) attempts to unregister a process registered by another client",
Suren Baghdasaryan9f1be122021-04-23 13:39:37 -07001281 taskname ? taskname : "A process ", cred->uid, cred->pid);
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001282 return;
1283 }
1284
Suren Baghdasaryan8b00c6d2018-10-12 11:28:33 -07001285 /*
1286 * WARNING: After pid_remove() procp is freed and can't be used!
1287 * Therefore placed at the end of the function.
1288 */
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -08001289 pid_remove(params.pid);
Todd Poynorc58c5142013-07-09 19:35:14 -07001290}
1291
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001292static void cmd_procpurge(struct ucred *cred) {
Suren Baghdasaryan83df0082018-10-10 14:17:17 -07001293 int i;
1294 struct proc *procp;
1295 struct proc *next;
1296
1297 if (use_inkernel_interface) {
Jim Blackler90853b62019-09-10 15:30:05 +01001298 stats_purge_tasknames();
Suren Baghdasaryan83df0082018-10-10 14:17:17 -07001299 return;
1300 }
1301
Suren Baghdasaryan83df0082018-10-10 14:17:17 -07001302 for (i = 0; i < PIDHASH_SZ; i++) {
1303 procp = pidhash[i];
1304 while (procp) {
1305 next = procp->pidhash_next;
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001306 /* Purge only records created by the requestor */
1307 if (claim_record(procp, cred->pid)) {
1308 pid_remove(procp->pid);
1309 }
Suren Baghdasaryan83df0082018-10-10 14:17:17 -07001310 procp = next;
1311 }
1312 }
Suren Baghdasaryan83df0082018-10-10 14:17:17 -07001313}
1314
Suren Baghdasaryan36baf442019-12-23 11:37:34 -08001315static 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
Suren Baghdasaryana7394ea2018-10-12 11:07:40 -07001322static 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
1347static 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
1370static 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
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -08001383static void cmd_target(int ntargets, LMKD_CTRL_PACKET packet) {
Todd Poynorc58c5142013-07-09 19:35:14 -07001384 int i;
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -08001385 struct lmk_target target;
Suren Baghdasaryan1ed0db12018-07-24 17:13:06 -07001386 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;
Todd Poynorc58c5142013-07-09 19:35:14 -07001391
Bart Van Assche54506792022-02-02 23:57:01 +00001392 if (ntargets < 1 || ntargets > (int)lowmem_adj.size()) {
Todd Poynorc58c5142013-07-09 19:35:14 -07001393 return;
Bart Van Assche54506792022-02-02 23:57:01 +00001394 }
Todd Poynorc58c5142013-07-09 19:35:14 -07001395
Suren Baghdasaryan1ed0db12018-07-24 17:13:06 -07001396 /*
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
Todd Poynorc58c5142013-07-09 19:35:14 -07001413 for (i = 0; i < ntargets; i++) {
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -08001414 lmkd_pack_get_target(packet, i, &target);
1415 lowmem_minfree[i] = target.minfree;
1416 lowmem_adj[i] = target.oom_adj_score;
Suren Baghdasaryan1ed0db12018-07-24 17:13:06 -07001417
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 }
Todd Poynorc58c5142013-07-09 19:35:14 -07001425 }
1426
1427 lowmem_targets_size = ntargets;
1428
Suren Baghdasaryan1ed0db12018-07-24 17:13:06 -07001429 /* Override the last extra comma */
1430 pstr[-1] = '\0';
1431 property_set("sys.lmk.minfree_levels", minfree_str);
1432
Robert Benea7878c9b2017-09-11 16:53:28 -07001433 if (has_inkernel_module) {
Todd Poynorc58c5142013-07-09 19:35:14 -07001434 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
Robert Benea7878c9b2017-09-11 16:53:28 -07001448 snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_minfree[i] : 0);
Todd Poynorc58c5142013-07-09 19:35:14 -07001449 strlcat(minfreestr, val, sizeof(minfreestr));
Robert Benea7878c9b2017-09-11 16:53:28 -07001450 snprintf(val, sizeof(val), "%d", use_inkernel_interface ? lowmem_adj[i] : 0);
Todd Poynorc58c5142013-07-09 19:35:14 -07001451 strlcat(killpriostr, val, sizeof(killpriostr));
1452 }
1453
Suren Baghdasaryanf584fff2018-03-20 13:53:17 -07001454 writefilestring(INKERNEL_MINFREE_PATH, minfreestr, true);
1455 writefilestring(INKERNEL_ADJ_PATH, killpriostr, true);
Todd Poynorc58c5142013-07-09 19:35:14 -07001456 }
1457}
1458
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08001459static void ctrl_command_handler(int dsock_idx) {
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -08001460 LMKD_CTRL_PACKET packet;
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001461 struct ucred cred;
Todd Poynorc58c5142013-07-09 19:35:14 -07001462 int len;
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -08001463 enum lmk_cmd cmd;
Todd Poynorc58c5142013-07-09 19:35:14 -07001464 int nargs;
1465 int targets;
Suren Baghdasaryana7394ea2018-10-12 11:07:40 -07001466 int kill_cnt;
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07001467 int result;
Todd Poynorc58c5142013-07-09 19:35:14 -07001468
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001469 len = ctrl_data_read(dsock_idx, (char *)packet, CTRL_PACKET_MAX_SIZE, &cred);
Todd Poynorc58c5142013-07-09 19:35:14 -07001470 if (len <= 0)
1471 return;
1472
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -08001473 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);
Todd Poynorc58c5142013-07-09 19:35:14 -07001479 nargs = len / sizeof(int) - 1;
1480 if (nargs < 0)
1481 goto wronglen;
1482
Todd Poynorc58c5142013-07-09 19:35:14 -07001483 switch(cmd) {
1484 case LMK_TARGET:
1485 targets = nargs / 2;
Bart Van Assche54506792022-02-02 23:57:01 +00001486 if (nargs & 0x1 || targets > (int)lowmem_adj.size()) {
Todd Poynorc58c5142013-07-09 19:35:14 -07001487 goto wronglen;
Bart Van Assche54506792022-02-02 23:57:01 +00001488 }
Suren Baghdasaryanf7932e52018-01-24 16:51:41 -08001489 cmd_target(targets, packet);
Todd Poynorc58c5142013-07-09 19:35:14 -07001490 break;
1491 case LMK_PROCPRIO:
Suren Baghdasaryana93503e2019-10-22 17:12:01 -07001492 /* process type field is optional for backward compatibility */
1493 if (nargs < 3 || nargs > 4)
Todd Poynorc58c5142013-07-09 19:35:14 -07001494 goto wronglen;
Suren Baghdasaryana93503e2019-10-22 17:12:01 -07001495 cmd_procprio(packet, nargs, &cred);
Todd Poynorc58c5142013-07-09 19:35:14 -07001496 break;
1497 case LMK_PROCREMOVE:
1498 if (nargs != 1)
1499 goto wronglen;
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001500 cmd_procremove(packet, &cred);
Todd Poynorc58c5142013-07-09 19:35:14 -07001501 break;
Suren Baghdasaryan83df0082018-10-10 14:17:17 -07001502 case LMK_PROCPURGE:
1503 if (nargs != 0)
1504 goto wronglen;
Suren Baghdasaryan945658a2019-10-18 11:16:52 -07001505 cmd_procpurge(&cred);
Suren Baghdasaryan83df0082018-10-10 14:17:17 -07001506 break;
Suren Baghdasaryana7394ea2018-10-12 11:07:40 -07001507 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;
Suren Baghdasaryan36baf442019-12-23 11:37:34 -08001515 case LMK_SUBSCRIBE:
1516 if (nargs != 1)
1517 goto wronglen;
1518 cmd_subscribe(dsock_idx, packet);
1519 break;
Jing Ji5c480962019-12-04 09:22:05 -08001520 case LMK_PROCKILL:
1521 /* This command code is NOT expected at all */
1522 ALOGE("Received unexpected command code %d", cmd);
1523 break;
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07001524 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;
Todd Poynorc58c5142013-07-09 19:35:14 -07001547 default:
1548 ALOGE("Received unknown command code %d", cmd);
1549 return;
1550 }
1551
1552 return;
1553
1554wronglen:
1555 ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len);
1556}
1557
Suren Baghdasaryane12a0672019-07-15 14:50:49 -07001558static void ctrl_data_handler(int data, uint32_t events,
1559 struct polling_params *poll_params __unused) {
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08001560 if (events & EPOLLIN) {
1561 ctrl_command_handler(data);
Todd Poynorc58c5142013-07-09 19:35:14 -07001562 }
1563}
1564
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08001565static 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}
Todd Poynorc58c5142013-07-09 19:35:14 -07001573
Suren Baghdasaryane12a0672019-07-15 14:50:49 -07001574static void ctrl_connect_handler(int data __unused, uint32_t events __unused,
1575 struct polling_params *poll_params __unused) {
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08001576 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;
Todd Poynorc58c5142013-07-09 19:35:14 -07001591 }
1592
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08001593 data_sock[free_dscock_idx].sock = accept(ctrl_sock.sock, NULL, NULL);
1594 if (data_sock[free_dscock_idx].sock < 0) {
Todd Poynorc58c5142013-07-09 19:35:14 -07001595 ALOGE("lmkd control socket accept failed; errno=%d", errno);
1596 return;
1597 }
1598
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08001599 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;
Suren Baghdasaryan36baf442019-12-23 11:37:34 -08001603 data_sock[free_dscock_idx].async_event_mask = 0;
Todd Poynorc58c5142013-07-09 19:35:14 -07001604 epev.events = EPOLLIN;
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08001605 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) {
Todd Poynorc58c5142013-07-09 19:35:14 -07001607 ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno);
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08001608 ctrl_data_close(free_dscock_idx);
Todd Poynorc58c5142013-07-09 19:35:14 -07001609 return;
1610 }
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08001611 maxevents++;
Todd Poynorc58c5142013-07-09 19:35:14 -07001612}
1613
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001614/*
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07001615 * /proc/zoneinfo parsing routines
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001616 * 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 */
1630static void zoneinfo_parse_protection(char *buf, struct zoneinfo_zone *zone) {
1631 int zone_idx;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001632 int64_t max = 0;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001633 char *save_ptr;
1634
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001635 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);
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001639 if (zoneval > max) {
1640 max = (zoneval > INT64_MAX) ? INT64_MAX : zoneval;
1641 }
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001642 zone->protection[zone_idx] = zoneval;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001643 }
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001644 zone->max_protection = max;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001645}
1646
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001647static 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;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001656
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001657 cp = strtok_r(line, " ", &save_ptr);
1658 if (!cp) {
1659 return false;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001660 }
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001661
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 }
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001695 }
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001696 return false;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001697}
1698
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001699static 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
1737static int zoneinfo_parse(struct zoneinfo *zi) {
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001738 static struct reread_data file_data = {
1739 .filename = ZONEINFO_PATH,
1740 .fd = -1,
1741 };
Suren Baghdasaryan03cb8362019-07-15 13:35:04 -07001742 char *buf;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001743 char *save_ptr;
1744 char *line;
Greg Kaiser259984f2019-10-02 07:07:32 -07001745 char zone_name[LINE_MAX + 1];
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001746 struct zoneinfo_node *node = NULL;
1747 int node_idx = 0;
1748 int zone_idx = 0;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001749
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001750 memset(zi, 0, sizeof(struct zoneinfo));
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001751
Suren Baghdasaryan03cb8362019-07-15 13:35:04 -07001752 if ((buf = reread_file(&file_data)) == NULL) {
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001753 return -1;
1754 }
1755
1756 for (line = strtok_r(buf, "\n", &save_ptr); line;
1757 line = strtok_r(NULL, "\n", &save_ptr)) {
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001758 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 }
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001786 }
1787 }
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001788 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;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001794
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001795 /* 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;
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07001804 }
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001805 return 0;
1806}
1807
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07001808/* /proc/meminfo parsing routines */
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001809static 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
Suren Baghdasaryan940e7cf2021-05-27 18:15:44 -07001835static 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
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001850static int meminfo_parse(union meminfo *mi) {
1851 static struct reread_data file_data = {
1852 .filename = MEMINFO_PATH,
1853 .fd = -1,
1854 };
Suren Baghdasaryan03cb8362019-07-15 13:35:04 -07001855 char *buf;
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001856 char *save_ptr;
1857 char *line;
1858
1859 memset(mi, 0, sizeof(union meminfo));
1860
Suren Baghdasaryan03cb8362019-07-15 13:35:04 -07001861 if ((buf = reread_file(&file_data)) == NULL) {
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001862 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;
Suren Baghdasaryan940e7cf2021-05-27 18:15:44 -07001874 mi->field.total_gpu_kb = read_gpu_total_kb();
Suren Baghdasaryand28a9732018-04-13 13:11:51 -07001875
1876 return 0;
1877}
1878
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07001879/* /proc/vmstat parsing routines */
1880static 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
1906static 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
Suren Baghdasaryan5ae47a92022-02-10 21:10:23 -08001932static 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
1955static 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
Suren Baghdasaryan014dd712022-02-18 12:51:15 -08001963static 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
1971static 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
Suren Baghdasaryand7b4fcb2020-07-23 18:00:43 -07001979enum wakeup_reason {
1980 Event,
1981 Polling
1982};
1983
1984struct 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 */
2001static 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
Suren Baghdasaryan39b54802021-08-09 15:10:25 -07002014struct kill_info {
2015 enum kill_reasons kill_reason;
2016 const char *kill_desc;
2017 int thrashing;
2018 int max_thrashing;
2019};
2020
Ioannis Ilkos279268a2020-08-01 10:50:40 +01002021static void killinfo_log(struct proc* procp, int min_oom_score, int rss_kb,
Suren Baghdasaryan39b54802021-08-09 15:10:25 -07002022 int swap_kb, struct kill_info *ki, union meminfo *mi,
Suren Baghdasaryan014dd712022-02-18 12:51:15 -08002023 struct wakeup_info *wi, struct timespec *tm, struct psi_data *pd) {
Suren Baghdasaryan12cacae2019-09-16 12:06:30 -07002024 /* 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);
Bart Van Assche80a3dba2022-02-02 23:51:35 +00002029 android_log_write_int32(ctx, std::min(rss_kb, (int)INT32_MAX));
Suren Baghdasaryan39b54802021-08-09 15:10:25 -07002030 android_log_write_int32(ctx, ki ? ki->kill_reason : NONE);
Suren Baghdasaryan12cacae2019-09-16 12:06:30 -07002031
2032 /* log meminfo fields */
Suren Baghdasaryan08bfa982018-07-26 16:34:27 -07002033 for (int field_idx = 0; field_idx < MI_FIELD_COUNT; field_idx++) {
Bart Van Assche80a3dba2022-02-02 23:51:35 +00002034 android_log_write_int32(ctx,
2035 mi ? std::min(mi->arr[field_idx] * page_k, (int64_t)INT32_MAX) : 0);
Suren Baghdasaryan08bfa982018-07-26 16:34:27 -07002036 }
2037
Suren Baghdasaryand7b4fcb2020-07-23 18:00:43 -07002038 /* log lmkd wakeup information */
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -08002039 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
Bart Van Assche80a3dba2022-02-02 23:51:35 +00002051 android_log_write_int32(ctx, std::min(swap_kb, (int)INT32_MAX));
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -08002052 android_log_write_int32(ctx, mi ? (int32_t)mi->field.total_gpu_kb : 0);
Suren Baghdasaryan39b54802021-08-09 15:10:25 -07002053 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 }
Suren Baghdasaryand7b4fcb2020-07-23 18:00:43 -07002060
Suren Baghdasaryan014dd712022-02-18 12:51:15 -08002061 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
Suren Baghdasaryan08bfa982018-07-26 16:34:27 -07002073 android_log_write_list(ctx, LOG_ID_EVENTS);
2074 android_log_reset(ctx);
2075}
2076
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -08002077// 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.
2079static 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.
2084static struct proc *proc_adj_tail(int oomadj) {
Todd Poynorc58c5142013-07-09 19:35:14 -07002085 return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]);
2086}
2087
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -08002088// When called from a non-main thread, adjslot_list_lock read lock should be taken.
2089static 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
Suren Baghdasaryana3802f12022-09-27 14:30:34 -07002103// Can be called only from the main thread.
Suren Baghdasaryaneb7c5492017-12-08 13:17:06 -08002104static 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);
Suren Baghdasaryan5263aa72021-04-29 15:28:57 -07002112 if (tasksize < 0) {
Suren Baghdasaryaneb7c5492017-12-08 13:17:06 -08002113 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
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -08002127static 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
2152static 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
Suren Baghdasaryana3802f12022-09-27 14:30:34 -07002165 if (target.valid && reaper.kill({ target.pidfd, target.pid, target.uid }, true) == 0) {
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -08002166 ALOGW("lmkd watchdog killed process %d, oom_score_adj %d", target.pid, oom_score);
Suren Baghdasaryan014dd712022-02-18 12:51:15 -08002167 killinfo_log(&target, 0, 0, 0, NULL, NULL, NULL, NULL, NULL);
Suren Baghdasaryana3802f12022-09-27 14:30:34 -07002168 // Can't call pid_remove() from non-main thread, therefore just invalidate the record
2169 pid_invalidate(target.pid);
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -08002170 break;
2171 }
2172 prev_pid = target.pid;
2173 }
2174}
2175
2176static Watchdog watchdog(WATCHDOG_TIMEOUT_SEC, watchdog_callback);
2177
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07002178static 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
2198static bool is_waiting_for_kill(void) {
2199 return pidfd_supported && last_kill_pid_or_fd >= 0;
2200}
2201
2202static 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 */
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07002231 if (epoll_ctl(epollfd, EPOLL_CTL_DEL, last_kill_pid_or_fd, &epev)) {
2232 // Log an error and keep going
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07002233 ALOGE("epoll_ctl for last killed process failed; errno=%d", errno);
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07002234 }
2235 maxevents--;
2236 close(last_kill_pid_or_fd);
2237 }
2238
2239 last_kill_pid_or_fd = -1;
2240}
2241
2242static 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
Suren Baghdasaryan7c3addb2021-06-11 12:12:56 -07002248static 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
Suren Baghdasaryana10157c2019-07-19 10:55:39 -07002261static void start_wait_for_proc_kill(int pid_or_fd) {
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07002262 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
Suren Baghdasaryana10157c2019-07-19 10:55:39 -07002271 last_kill_pid_or_fd = pid_or_fd;
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07002272
Suren Baghdasaryana10157c2019-07-19 10:55:39 -07002273 if (!pidfd_supported) {
2274 /* If pidfd is not supported just store PID and exit */
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07002275 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}
Tim Murraya79ec0f2018-10-25 17:05:41 -07002288
Ioannis Ilkos279268a2020-08-01 10:50:40 +01002289/* Kill one process specified by procp. Returns the size (in pages) of the process killed */
Suren Baghdasaryane1604752021-07-22 16:21:21 -07002290static int kill_one_process(struct proc* procp, int min_oom_score, struct kill_info *ki,
Suren Baghdasaryan014dd712022-02-18 12:51:15 -08002291 union meminfo *mi, struct wakeup_info *wi, struct timespec *tm,
2292 struct psi_data *pd) {
Colin Cross3d57a512014-07-14 12:39:56 -07002293 int pid = procp->pid;
Suren Baghdasaryana10157c2019-07-19 10:55:39 -07002294 int pidfd = procp->pidfd;
Colin Cross3d57a512014-07-14 12:39:56 -07002295 uid_t uid = procp->uid;
2296 char *taskname;
Suren Baghdasaryan7c3addb2021-06-11 12:12:56 -07002297 int kill_result;
Suren Baghdasaryan8b00c6d2018-10-12 11:28:33 -07002298 int result = -1;
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07002299 struct memory_stat *mem_st;
Suren Baghdasaryan3cc1f132020-09-09 20:19:02 -07002300 struct kill_stat kill_st;
Ioannis Ilkos279268a2020-08-01 10:50:40 +01002301 int64_t tgid;
2302 int64_t rss_kb;
2303 int64_t swap_kb;
2304 char buf[PAGE_SIZE];
Suren Baghdasaryan34928bb2021-07-29 17:02:51 -07002305 char desc[LINE_MAX];
Rajeev Kumar4aba9152018-01-31 17:54:56 -08002306
Suren Baghdasaryana3802f12022-09-27 14:30:34 -07002307 if (!procp->valid || !read_proc_status(pid, buf, sizeof(buf))) {
Ioannis Ilkos279268a2020-08-01 10:50:40 +01002308 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)) {
Suren Baghdasaryan3ee11d42019-07-02 15:52:07 -07002323 goto out;
2324 }
2325
Suren Baghdasaryan632f1c52019-10-04 16:06:55 -07002326 taskname = proc_get_name(pid, buf, sizeof(buf));
Ioannis Ilkos279268a2020-08-01 10:50:40 +01002327 // taskname will point inside buf, do not reuse buf onwards.
Colin Cross3d57a512014-07-14 12:39:56 -07002328 if (!taskname) {
Suren Baghdasaryan8b00c6d2018-10-12 11:28:33 -07002329 goto out;
Colin Cross3d57a512014-07-14 12:39:56 -07002330 }
2331
Ioannis Ilkos279268a2020-08-01 10:50:40 +01002332 mem_st = stats_read_memory_stat(per_app_memcg, pid, uid, rss_kb * 1024, swap_kb * 1024);
Rajeev Kumar4aba9152018-01-31 17:54:56 -08002333
George Burgess IVe849f142021-08-05 06:59:42 +00002334 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);
Suren Baghdasaryan7c3addb2021-06-11 12:12:56 -07002336
Suren Baghdasaryan34928bb2021-07-29 17:02:51 -07002337 trace_kill_start(pid, desc);
Suren Baghdasaryan03e19872018-01-04 10:43:58 -08002338
Suren Baghdasaryan7c3addb2021-06-11 12:12:56 -07002339 start_wait_for_proc_kill(pidfd < 0 ? pid : pidfd);
Suren Baghdasaryan2bdf7f02022-01-20 18:48:52 -08002340 kill_result = reaper.kill({ pidfd, pid, uid }, false);
Wei Wangf1ee2e12018-11-21 00:11:44 -08002341
Suren Baghdasaryan34928bb2021-07-29 17:02:51 -07002342 trace_kill_end();
Suren Baghdasaryanc2e05b62019-09-04 16:44:47 -07002343
Suren Baghdasaryan7c3addb2021-06-11 12:12:56 -07002344 if (kill_result) {
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07002345 stop_wait_for_proc_kill(false);
Suren Baghdasaryanc2e05b62019-09-04 16:44:47 -07002346 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
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07002351 last_kill_tm = *tm;
2352
Suren Baghdasaryana7394ea2018-10-12 11:07:40 -07002353 inc_killcnt(procp->oomadj);
Suren Baghdasaryan12cacae2019-09-16 12:06:30 -07002354
Suren Baghdasaryane1604752021-07-22 16:21:21 -07002355 if (ki) {
2356 kill_st.kill_reason = ki->kill_reason;
2357 kill_st.thrashing = ki->thrashing;
2358 kill_st.max_thrashing = ki->max_thrashing;
Ioannis Ilkos48848902021-02-18 19:53:33 +00002359 ALOGI("Kill '%s' (%d), uid %d, oom_score_adj %d to free %" PRId64 "kB rss, %" PRId64
Suren Baghdasaryane1604752021-07-22 16:21:21 -07002360 "kB swap; reason: %s", taskname, pid, uid, procp->oomadj, rss_kb, swap_kb,
2361 ki->kill_desc);
Suren Baghdasaryan89454e62019-07-15 15:50:17 -07002362 } else {
Suren Baghdasaryane1604752021-07-22 16:21:21 -07002363 kill_st.kill_reason = NONE;
2364 kill_st.thrashing = 0;
2365 kill_st.max_thrashing = 0;
Ioannis Ilkos48848902021-02-18 19:53:33 +00002366 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);
Suren Baghdasaryan89454e62019-07-15 15:50:17 -07002368 }
Suren Baghdasaryan014dd712022-02-18 12:51:15 -08002369 killinfo_log(procp, min_oom_score, rss_kb, swap_kb, ki, mi, wi, tm, pd);
Colin Cross3d57a512014-07-14 12:39:56 -07002370
Suren Baghdasaryan3cc1f132020-09-09 20:19:02 -07002371 kill_st.uid = static_cast<int32_t>(uid);
2372 kill_st.taskname = taskname;
Suren Baghdasaryan3cc1f132020-09-09 20:19:02 -07002373 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);
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07002378
Jing Ji5c480962019-12-04 09:22:05 -08002379 ctrl_data_write_lmk_kill_occurred((pid_t)pid, uid);
2380
Ioannis Ilkos279268a2020-08-01 10:50:40 +01002381 result = rss_kb / page_k;
Mark Salyzyn1d5fdf32018-02-04 15:27:23 -08002382
Suren Baghdasaryan8b00c6d2018-10-12 11:28:33 -07002383out:
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;
Colin Cross3d57a512014-07-14 12:39:56 -07002390}
2391
2392/*
Chris Morin74b4df92021-02-26 00:00:35 -08002393 * Find one process to kill at or above the given oom_score_adj level.
Suren Baghdasaryan85c31b52018-10-26 11:32:15 -07002394 * Returns size of the killed process.
Colin Cross3d57a512014-07-14 12:39:56 -07002395 */
Suren Baghdasaryane1604752021-07-22 16:21:21 -07002396static int find_and_kill_process(int min_score_adj, struct kill_info *ki, union meminfo *mi,
Suren Baghdasaryan014dd712022-02-18 12:51:15 -08002397 struct wakeup_info *wi, struct timespec *tm,
2398 struct psi_data *pd) {
Colin Cross3d57a512014-07-14 12:39:56 -07002399 int i;
Suren Baghdasaryan85c31b52018-10-26 11:32:15 -07002400 int killed_size = 0;
Yang Lu5dfffbc2018-05-15 04:59:44 +00002401 bool lmk_state_change_start = false;
Suren Baghdasaryan858e8c62021-03-03 11:05:09 -08002402 bool choose_heaviest_task = kill_heaviest_task;
Rajeev Kumar4aba9152018-01-31 17:54:56 -08002403
Chong Zhang1cd12b52015-10-14 16:19:53 -07002404 for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) {
Colin Cross3d57a512014-07-14 12:39:56 -07002405 struct proc *procp;
2406
Suren Baghdasaryan858e8c62021-03-03 11:05:09 -08002407 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
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -08002415 while (true) {
Suren Baghdasaryan858e8c62021-03-03 11:05:09 -08002416 procp = choose_heaviest_task ?
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -08002417 proc_get_heaviest(i) : proc_adj_tail(i);
Colin Cross3d57a512014-07-14 12:39:56 -07002418
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -08002419 if (!procp)
2420 break;
2421
Suren Baghdasaryan014dd712022-02-18 12:51:15 -08002422 killed_size = kill_one_process(procp, min_score_adj, ki, mi, wi, tm, pd);
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -08002423 if (killed_size >= 0) {
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07002424 if (!lmk_state_change_start) {
Yang Lu5dfffbc2018-05-15 04:59:44 +00002425 lmk_state_change_start = true;
Vova Sharaienkoa92b76b2021-04-24 00:30:06 +00002426 stats_write_lmk_state_changed(STATE_START);
Yang Lu5dfffbc2018-05-15 04:59:44 +00002427 }
Suren Baghdasaryan85c31b52018-10-26 11:32:15 -07002428 break;
Colin Cross3d57a512014-07-14 12:39:56 -07002429 }
2430 }
Suren Baghdasaryan85c31b52018-10-26 11:32:15 -07002431 if (killed_size) {
2432 break;
2433 }
Colin Cross3d57a512014-07-14 12:39:56 -07002434 }
2435
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07002436 if (lmk_state_change_start) {
Vova Sharaienkoa92b76b2021-04-24 00:30:06 +00002437 stats_write_lmk_state_changed(STATE_STOP);
Rajeev Kumar4aba9152018-01-31 17:54:56 -08002438 }
Rajeev Kumar4aba9152018-01-31 17:54:56 -08002439
Suren Baghdasaryan85c31b52018-10-26 11:32:15 -07002440 return killed_size;
Colin Cross3d57a512014-07-14 12:39:56 -07002441}
2442
Suren Baghdasaryan87966742018-04-13 12:43:41 -07002443static int64_t get_memory_usage(struct reread_data *file_data) {
Robert Beneac72b2932017-08-21 15:18:31 -07002444 int64_t mem_usage;
Suren Baghdasaryan03cb8362019-07-15 13:35:04 -07002445 char *buf;
Suren Baghdasaryan87966742018-04-13 12:43:41 -07002446
Suren Baghdasaryan03cb8362019-07-15 13:35:04 -07002447 if ((buf = reread_file(file_data)) == NULL) {
Robert Beneac72b2932017-08-21 15:18:31 -07002448 return -1;
2449 }
2450
Suren Baghdasaryan87966742018-04-13 12:43:41 -07002451 if (!parse_int64(buf, &mem_usage)) {
2452 ALOGE("%s parse error", file_data->filename);
Robert Beneac72b2932017-08-21 15:18:31 -07002453 return -1;
2454 }
Robert Beneac72b2932017-08-21 15:18:31 -07002455 if (mem_usage == 0) {
2456 ALOGE("No memory!");
2457 return -1;
2458 }
2459 return mem_usage;
2460}
2461
Suren Baghdasaryane0f3dd12018-04-13 13:41:12 -07002462void 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) {
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -08002465 if (debug_process_killing) {
Suren Baghdasaryane0f3dd12018-04-13 13:41:12 -07002466 ALOGI("Low pressure min memory update from %" PRId64 " to %" PRId64,
2467 low_pressure_mem.min_nr_free_pages, mi->field.nr_free_pages);
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -08002468 }
Suren Baghdasaryane0f3dd12018-04-13 13:41:12 -07002469 low_pressure_mem.min_nr_free_pages = mi->field.nr_free_pages;
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -08002470 }
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).
Suren Baghdasaryane0f3dd12018-04-13 13:41:12 -07002475 * Ignore large jumps in max_nr_free_pages that would mess up our stats.
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -08002476 */
Suren Baghdasaryane0f3dd12018-04-13 13:41:12 -07002477 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)) {
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -08002481 if (debug_process_killing) {
Suren Baghdasaryane0f3dd12018-04-13 13:41:12 -07002482 ALOGI("Low pressure max memory update from %" PRId64 " to %" PRId64,
2483 low_pressure_mem.max_nr_free_pages, mi->field.nr_free_pages);
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -08002484 }
Suren Baghdasaryane0f3dd12018-04-13 13:41:12 -07002485 low_pressure_mem.max_nr_free_pages = mi->field.nr_free_pages;
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -08002486 }
2487}
2488
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -08002489enum vmpressure_level upgrade_level(enum vmpressure_level level) {
2490 return (enum vmpressure_level)((level < VMPRESS_LEVEL_CRITICAL) ?
2491 level + 1 : level);
2492}
2493
2494enum vmpressure_level downgrade_level(enum vmpressure_level level) {
2495 return (enum vmpressure_level)((level > VMPRESS_LEVEL_LOW) ?
2496 level - 1 : level);
2497}
2498
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002499enum zone_watermark {
2500 WMARK_MIN = 0,
2501 WMARK_LOW,
2502 WMARK_HIGH,
2503 WMARK_NONE
2504};
2505
Suren Baghdasaryan81c75b22019-08-14 09:48:35 -07002506struct zone_watermarks {
2507 long high_wmark;
2508 long low_wmark;
2509 long min_wmark;
2510};
2511
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002512/*
2513 * Returns lowest breached watermark or WMARK_NONE.
2514 */
Suren Baghdasaryan81c75b22019-08-14 09:48:35 -07002515static enum zone_watermark get_lowest_watermark(union meminfo *mi,
2516 struct zone_watermarks *watermarks)
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002517{
Suren Baghdasaryan81c75b22019-08-14 09:48:35 -07002518 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
2532void calc_zone_watermarks(struct zoneinfo *zi, struct zone_watermarks *watermarks) {
2533 memset(watermarks, 0, sizeof(struct zone_watermarks));
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002534
2535 for (int node_idx = 0; node_idx < zi->node_count; node_idx++) {
2536 struct zoneinfo_node *node = &zi->nodes[node_idx];
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002537 for (int zone_idx = 0; zone_idx < node->zone_count; zone_idx++) {
2538 struct zoneinfo_zone *zone = &node->zones[zone_idx];
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002539
2540 if (!zone->fields.field.present) {
2541 continue;
2542 }
2543
Suren Baghdasaryan81c75b22019-08-14 09:48:35 -07002544 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;
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002547 }
2548 }
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002549}
2550
Suren Baghdasaryan51ee4c52020-04-21 12:27:01 -07002551static 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
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002558static void mp_event_psi(int data, uint32_t events, struct polling_params *poll_params) {
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002559 enum reclaim_state {
2560 NO_RECLAIM = 0,
2561 KSWAPD_RECLAIM,
2562 DIRECT_RECLAIM,
2563 };
2564 static int64_t init_ws_refault;
Martin Liu1f72f5f2020-08-21 13:18:50 +08002565 static int64_t prev_workingset_refault;
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002566 static int64_t base_file_lru;
2567 static int64_t init_pgscan_kswapd;
2568 static int64_t init_pgscan_direct;
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002569 static bool killing;
Martin Liu1f72f5f2020-08-21 13:18:50 +08002570 static int thrashing_limit = thrashing_limit_pct;
Suren Baghdasaryan81c75b22019-08-14 09:48:35 -07002571 static struct zone_watermarks watermarks;
2572 static struct timespec wmark_update_tm;
Suren Baghdasaryand7b4fcb2020-07-23 18:00:43 -07002573 static struct wakeup_info wi;
Martin Liu1f72f5f2020-08-21 13:18:50 +08002574 static struct timespec thrashing_reset_tm;
2575 static int64_t prev_thrash_growth = 0;
Suren Baghdasaryan11221d42021-07-14 17:57:52 -07002576 static bool check_filecache = false;
Suren Baghdasaryane1604752021-07-22 16:21:21 -07002577 static int max_thrashing = 0;
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002578
2579 union meminfo mi;
2580 union vmstat vs;
Suren Baghdasaryan5ae47a92022-02-10 21:10:23 -08002581 struct psi_data psi_data;
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002582 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;
Suren Baghdasaryan89454e62019-07-15 15:50:17 -07002590 char kill_desc[LINE_MAX];
Suren Baghdasaryan970a26a2019-09-19 15:27:21 -07002591 bool cut_thrashing_limit = false;
2592 int min_score_adj = 0;
Suren Baghdasaryan51ee4c52020-04-21 12:27:01 -07002593 int swap_util = 0;
Suren Baghdasaryan6e6d14b2021-10-28 13:30:08 -07002594 int64_t swap_low_threshold;
Martin Liu1f72f5f2020-08-21 13:18:50 +08002595 long since_thrashing_reset_ms;
Suren Baghdasaryandc60f972020-12-14 13:38:48 -08002596 int64_t workingset_refault_file;
Suren Baghdasaryan5ae47a92022-02-10 21:10:23 -08002597 bool critical_stall = false;
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002598
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002599 if (clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm) != 0) {
2600 ALOGE("Failed to get current time");
2601 return;
2602 }
2603
Suren Baghdasaryand7b4fcb2020-07-23 18:00:43 -07002604 record_wakeup_time(&curr_tm, events ? Event : Polling, &wi);
2605
Suren Baghdasaryan03dccf32020-04-28 16:02:13 -07002606 bool kill_pending = is_kill_pending();
Suren Baghdasaryaned715a32020-05-11 16:31:57 -07002607 if (kill_pending && (kill_timeout_ms == 0 ||
2608 get_time_diff_ms(&last_kill_tm, &curr_tm) < static_cast<long>(kill_timeout_ms))) {
Suren Baghdasaryan03dccf32020-04-28 16:02:13 -07002609 /* Skip while still killing a process */
Suren Baghdasaryand7b4fcb2020-07-23 18:00:43 -07002610 wi.skipped_wakeups++;
Suren Baghdasaryan03dccf32020-04-28 16:02:13 -07002611 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
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002619 if (vmstat_parse(&vs) < 0) {
2620 ALOGE("Failed to parse vmstat!");
2621 return;
2622 }
Suren Baghdasaryandc60f972020-12-14 13:38:48 -08002623 /* 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;
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002625
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;
Suren Baghdasaryandc60f972020-12-14 13:38:48 -08002637 init_ws_refault = workingset_refault_file;
Martin Liu1f72f5f2020-08-21 13:18:50 +08002638 thrashing_reset_tm = curr_tm;
2639 prev_thrash_growth = 0;
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002640 }
2641
2642 /* Check free swap levels */
2643 if (swap_free_low_percentage) {
Suren Baghdasaryan6e6d14b2021-10-28 13:30:08 -07002644 swap_low_threshold = mi.field.total_swap * swap_free_low_percentage / 100;
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002645 swap_is_low = mi.field.free_swap < swap_low_threshold;
Suren Baghdasaryan6e6d14b2021-10-28 13:30:08 -07002646 } else {
2647 swap_low_threshold = 0;
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002648 }
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;
Suren Baghdasaryandc60f972020-12-14 13:38:48 -08002658 } else if (workingset_refault_file == prev_workingset_refault) {
Suren Baghdasaryan11221d42021-07-14 17:57:52 -07002659 /*
2660 * Device is not thrashing and not reclaiming, bail out early until we see these stats
2661 * changing
2662 */
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002663 goto no_kill;
2664 }
2665
Suren Baghdasaryandc60f972020-12-14 13:38:48 -08002666 prev_workingset_refault = workingset_refault_file;
Martin Liu1f72f5f2020-08-21 13:18:50 +08002667
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
Suren Baghdasaryan11221d42021-07-14 17:57:52 -07002675 * counter by window counts. If the counter is still greater than thrashing limit,
Martin Liu1f72f5f2020-08-21 13:18:50 +08002676 * 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 */
Suren Baghdasaryandc60f972020-12-14 13:38:48 -08002683 prev_thrash_growth = (workingset_refault_file - init_ws_refault) * 100
Martin Liuc3108412020-09-03 22:12:14 +08002684 / (base_file_lru + 1);
Martin Liu1f72f5f2020-08-21 13:18:50 +08002685 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 */
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002696 base_file_lru = vs.field.nr_inactive_file + vs.field.nr_active_file;
Suren Baghdasaryandc60f972020-12-14 13:38:48 -08002697 init_ws_refault = workingset_refault_file;
Martin Liu1f72f5f2020-08-21 13:18:50 +08002698 thrashing_reset_tm = curr_tm;
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002699 thrashing_limit = thrashing_limit_pct;
2700 } else {
2701 /* Calculate what % of the file-backed pagecache refaulted so far */
Suren Baghdasaryandc60f972020-12-14 13:38:48 -08002702 thrashing = (workingset_refault_file - init_ws_refault) * 100 / (base_file_lru + 1);
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002703 }
Martin Liu1f72f5f2020-08-21 13:18:50 +08002704 /* Add previous cycle's decayed thrashing amount */
2705 thrashing += prev_thrash_growth;
Suren Baghdasaryane1604752021-07-22 16:21:21 -07002706 if (max_thrashing < thrashing) {
2707 max_thrashing = thrashing;
2708 }
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002709
Suren Baghdasaryan81c75b22019-08-14 09:48:35 -07002710 /*
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;
Martin Liu1f72f5f2020-08-21 13:18:50 +08002725 }
Suren Baghdasaryan81c75b22019-08-14 09:48:35 -07002726
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002727 /* Find out which watermark is breached if any */
Suren Baghdasaryan81c75b22019-08-14 09:48:35 -07002728 wmark = get_lowest_watermark(&mi, &watermarks);
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002729
Suren Baghdasaryan5ae47a92022-02-10 21:10:23 -08002730 if (!psi_parse_mem(&psi_data)) {
2731 critical_stall = psi_data.mem_stats[PSI_FULL].avg10 > (float)stall_limit_critical;
2732 }
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002733 /*
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;
Suren Baghdasaryan89454e62019-07-15 15:50:17 -07002744 strncpy(kill_desc, "min watermark is breached even after kill", sizeof(kill_desc));
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002745 } 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;
Suren Baghdasaryan89454e62019-07-15 15:50:17 -07002752 strncpy(kill_desc, "device is not responding", sizeof(kill_desc));
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002753 } 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;
Suren Baghdasaryan89454e62019-07-15 15:50:17 -07002756 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);
Suren Baghdasaryan0142b3c2021-03-03 11:11:09 -08002759 /* Do not kill perceptible apps unless below min watermark or heavily thrashing */
2760 if (wmark > WMARK_MIN && thrashing < thrashing_critical_pct) {
Suren Baghdasaryan48135c42020-05-19 12:59:18 -07002761 min_score_adj = PERCEPTIBLE_APP_ADJ + 1;
2762 }
Suren Baghdasaryan11221d42021-07-14 17:57:52 -07002763 check_filecache = true;
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002764 } else if (swap_is_low && wmark < WMARK_HIGH) {
2765 /* Both free memory and swap are low */
2766 kill_reason = LOW_MEM_AND_SWAP;
Suren Baghdasaryan89454e62019-07-15 15:50:17 -07002767 snprintf(kill_desc, sizeof(kill_desc), "%s watermark is breached and swap is low (%"
Suren Baghdasaryan23678182021-03-02 18:33:09 -08002768 PRId64 "kB < %" PRId64 "kB)", wmark < WMARK_LOW ? "min" : "low",
Suren Baghdasaryan89454e62019-07-15 15:50:17 -07002769 mi.field.free_swap * page_k, swap_low_threshold * page_k);
Suren Baghdasaryan0142b3c2021-03-03 11:11:09 -08002770 /* Do not kill perceptible apps unless below min watermark or heavily thrashing */
2771 if (wmark > WMARK_MIN && thrashing < thrashing_critical_pct) {
Suren Baghdasaryan48135c42020-05-19 12:59:18 -07002772 min_score_adj = PERCEPTIBLE_APP_ADJ + 1;
2773 }
Suren Baghdasaryan51ee4c52020-04-21 12:27:01 -07002774 } 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"
Suren Baghdasaryan23678182021-03-02 18:33:09 -08002782 " is high (%d%% > %d%%)", wmark < WMARK_LOW ? "min" : "low",
Suren Baghdasaryan51ee4c52020-04-21 12:27:01 -07002783 swap_util, swap_util_max);
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002784 } else if (wmark < WMARK_HIGH && thrashing > thrashing_limit) {
2785 /* Page cache is thrashing while memory is low */
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002786 kill_reason = LOW_MEM_AND_THRASHING;
Suren Baghdasaryan89454e62019-07-15 15:50:17 -07002787 snprintf(kill_desc, sizeof(kill_desc), "%s watermark is breached and thrashing (%"
Suren Baghdasaryan23678182021-03-02 18:33:09 -08002788 PRId64 "%%)", wmark < WMARK_LOW ? "min" : "low", thrashing);
Suren Baghdasaryan970a26a2019-09-19 15:27:21 -07002789 cut_thrashing_limit = true;
Suren Baghdasaryan0142b3c2021-03-03 11:11:09 -08002790 /* 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 }
Suren Baghdasaryan11221d42021-07-14 17:57:52 -07002794 check_filecache = true;
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002795 } else if (reclaim == DIRECT_RECLAIM && thrashing > thrashing_limit) {
2796 /* Page cache is thrashing while in direct reclaim (mostly happens on lowram devices) */
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002797 kill_reason = DIRECT_RECL_AND_THRASHING;
Suren Baghdasaryan89454e62019-07-15 15:50:17 -07002798 snprintf(kill_desc, sizeof(kill_desc), "device is in direct reclaim and thrashing (%"
2799 PRId64 "%%)", thrashing);
Suren Baghdasaryan970a26a2019-09-19 15:27:21 -07002800 cut_thrashing_limit = true;
Suren Baghdasaryan0142b3c2021-03-03 11:11:09 -08002801 /* 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 }
Suren Baghdasaryan11221d42021-07-14 17:57:52 -07002805 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 }
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002820 }
2821
2822 /* Kill a process if necessary */
2823 if (kill_reason != NONE) {
Suren Baghdasaryane1604752021-07-22 16:21:21 -07002824 struct kill_info ki = {
2825 .kill_reason = kill_reason,
2826 .kill_desc = kill_desc,
2827 .thrashing = (int)thrashing,
2828 .max_thrashing = max_thrashing,
2829 };
Suren Baghdasaryan5ae47a92022-02-10 21:10:23 -08002830
2831 /* Allow killing perceptible apps if the system is stalled */
2832 if (critical_stall) {
2833 min_score_adj = 0;
2834 }
Suren Baghdasaryan014dd712022-02-18 12:51:15 -08002835 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);
Suren Baghdasaryan970a26a2019-09-19 15:27:21 -07002838 if (pages_freed > 0) {
2839 killing = true;
Suren Baghdasaryane1604752021-07-22 16:21:21 -07002840 max_thrashing = 0;
Suren Baghdasaryan970a26a2019-09-19 15:27:21 -07002841 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 }
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002849 }
2850
2851no_kill:
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07002852 /* 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
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002859 /*
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
Bart Van Asscheb4d26bb2022-02-17 21:31:51 +00002879static 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.
Suren Baghdasaryane12a0672019-07-15 14:50:49 -07002889static void mp_event_common(int data, uint32_t events, struct polling_params *poll_params) {
Todd Poynorc58c5142013-07-09 19:35:14 -07002890 unsigned long long evcount;
Robert Beneac72b2932017-08-21 15:18:31 -07002891 int64_t mem_usage, memsw_usage;
Robert Benea3be16142017-09-13 15:20:30 -07002892 int64_t mem_pressure;
Suren Baghdasaryane0f3dd12018-04-13 13:41:12 -07002893 union meminfo mi;
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07002894 struct zoneinfo zi;
Suren Baghdasaryan53be36e2018-09-05 15:46:32 -07002895 struct timespec curr_tm;
Suren Baghdasaryan1ed0db12018-07-24 17:13:06 -07002896 static unsigned long kill_skip_count = 0;
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08002897 enum vmpressure_level level = (enum vmpressure_level)data;
Suren Baghdasaryan2c4611a2018-04-13 13:53:43 -07002898 long other_free = 0, other_file = 0;
2899 int min_score_adj;
Suren Baghdasaryan2c4611a2018-04-13 13:53:43 -07002900 int minfree = 0;
Bart Van Asscheb4d26bb2022-02-17 21:31:51 +00002901 static const std::string mem_usage_path = GetCgroupAttributePath("MemUsage");
Suren Baghdasaryan87966742018-04-13 12:43:41 -07002902 static struct reread_data mem_usage_file_data = {
Bart Van Asscheb4d26bb2022-02-17 21:31:51 +00002903 .filename = mem_usage_path.c_str(),
Suren Baghdasaryan87966742018-04-13 12:43:41 -07002904 .fd = -1,
2905 };
Bart Van Asscheb4d26bb2022-02-17 21:31:51 +00002906 static const std::string memsw_usage_path = GetCgroupAttributePath("MemAndSwapUsage");
Suren Baghdasaryan87966742018-04-13 12:43:41 -07002907 static struct reread_data memsw_usage_file_data = {
Bart Van Asscheb4d26bb2022-02-17 21:31:51 +00002908 .filename = memsw_usage_path.c_str(),
Suren Baghdasaryan87966742018-04-13 12:43:41 -07002909 .fd = -1,
2910 };
Suren Baghdasaryand7b4fcb2020-07-23 18:00:43 -07002911 static struct wakeup_info wi;
Todd Poynorc58c5142013-07-09 19:35:14 -07002912
Suren Baghdasaryan55e31502019-01-08 12:54:48 -08002913 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 */
Tom Cherry43f3d2b2019-12-04 12:46:57 -08002923 for (int lvl = VMPRESS_LEVEL_LOW; lvl < VMPRESS_LEVEL_COUNT; lvl++) {
Suren Baghdasaryan55e31502019-01-08 12:54:48 -08002924 if (mpevfd[lvl] != -1 &&
2925 TEMP_FAILURE_RETRY(read(mpevfd[lvl],
2926 &evcount, sizeof(evcount))) > 0 &&
2927 evcount > 0 && lvl > level) {
Tom Cherry43f3d2b2019-12-04 12:46:57 -08002928 level = static_cast<vmpressure_level>(lvl);
Suren Baghdasaryan55e31502019-01-08 12:54:48 -08002929 }
Suren Baghdasaryan3e1a8492018-01-04 09:16:21 -08002930 }
2931 }
Todd Poynorc58c5142013-07-09 19:35:14 -07002932
Suren Baghdasaryane12a0672019-07-15 14:50:49 -07002933 /* 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) {
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07002937 poll_params->polling_interval_ms = PSI_POLL_PERIOD_SHORT_MS;
Suren Baghdasaryane12a0672019-07-15 14:50:49 -07002938 poll_params->update = POLLING_START;
2939 }
2940 }
2941
Suren Baghdasaryan53be36e2018-09-05 15:46:32 -07002942 if (clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm) != 0) {
2943 ALOGE("Failed to get current time");
2944 return;
2945 }
2946
Suren Baghdasaryand7b4fcb2020-07-23 18:00:43 -07002947 record_wakeup_time(&curr_tm, events ? Event : Polling, &wi);
2948
Suren Baghdasaryaned715a32020-05-11 16:31:57 -07002949 if (kill_timeout_ms &&
2950 get_time_diff_ms(&last_kill_tm, &curr_tm) < static_cast<long>(kill_timeout_ms)) {
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07002951 /*
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()) {
Suren Baghdasaryan1ed0db12018-07-24 17:13:06 -07002956 kill_skip_count++;
Suren Baghdasaryand7b4fcb2020-07-23 18:00:43 -07002957 wi.skipped_wakeups++;
Suren Baghdasaryan30854e72018-01-17 17:28:01 -08002958 return;
2959 }
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07002960 /*
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);
Suren Baghdasaryan30854e72018-01-17 17:28:01 -08002971 }
2972
Suren Baghdasaryan1ed0db12018-07-24 17:13:06 -07002973 if (kill_skip_count > 0) {
Suren Baghdasaryaneff82332018-05-10 16:10:56 -07002974 ALOGI("%lu memory pressure events were skipped after a kill!",
Suren Baghdasaryan1ed0db12018-07-24 17:13:06 -07002975 kill_skip_count);
2976 kill_skip_count = 0;
Suren Baghdasaryan30854e72018-01-17 17:28:01 -08002977 }
2978
Suren Baghdasaryan2c4611a2018-04-13 13:53:43 -07002979 if (meminfo_parse(&mi) < 0 || zoneinfo_parse(&zi) < 0) {
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -08002980 ALOGE("Failed to get free memory!");
2981 return;
2982 }
2983
Suren Baghdasaryan2c4611a2018-04-13 13:53:43 -07002984 if (use_minfree_levels) {
2985 int i;
2986
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07002987 other_free = mi.field.nr_free_pages - zi.totalreserve_pages;
Suren Baghdasaryan2c4611a2018-04-13 13:53:43 -07002988 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
Suren Baghdasaryanb0bda552018-05-18 14:42:00 -07003004 if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
liuhailongcf8af502021-12-04 16:29:52 +08003005 if (debug_process_killing && lowmem_targets_size) {
Suren Baghdasaryanb0bda552018-05-18 14:42:00 -07003006 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 }
Suren Baghdasaryan2c4611a2018-04-13 13:53:43 -07003011 return;
Suren Baghdasaryanb0bda552018-05-18 14:42:00 -07003012 }
Suren Baghdasaryan2c4611a2018-04-13 13:53:43 -07003013
Suren Baghdasaryan2c4611a2018-04-13 13:53:43 -07003014 goto do_kill;
3015 }
3016
Suren Baghdasaryane0f3dd12018-04-13 13:41:12 -07003017 if (level == VMPRESS_LEVEL_LOW) {
3018 record_low_pressure_levels(&mi);
3019 }
3020
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -08003021 if (level_oomadj[level] > OOM_SCORE_ADJ_MAX) {
3022 /* Do not monitor this pressure level */
3023 return;
3024 }
3025
Suren Baghdasaryan87966742018-04-13 12:43:41 -07003026 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) {
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -08003030 goto do_kill;
Robert Benea3be16142017-09-13 15:20:30 -07003031 }
Robert Beneac72b2932017-08-21 15:18:31 -07003032
Robert Benea3be16142017-09-13 15:20:30 -07003033 // Calculate percent for swappinness.
3034 mem_pressure = (mem_usage * 100) / memsw_usage;
3035
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -08003036 if (enable_pressure_upgrade && level != VMPRESS_LEVEL_CRITICAL) {
Robert Benea3be16142017-09-13 15:20:30 -07003037 // We are swapping too much.
3038 if (mem_pressure < upgrade_pressure) {
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -08003039 level = upgrade_level(level);
3040 if (debug_process_killing) {
3041 ALOGI("Event upgraded to %s", level_name[level]);
3042 }
Robert Beneac72b2932017-08-21 15:18:31 -07003043 }
3044 }
3045
Vic Yang65680692018-08-07 10:18:22 -07003046 // 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);
Robert Benea3be16142017-09-13 15:20:30 -07003063 }
Robert Benea3be16142017-09-13 15:20:30 -07003064 }
3065
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -08003066do_kill:
Suren Baghdasaryand1d59f82018-04-13 11:45:38 -07003067 if (low_ram_device) {
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -08003068 /* For Go devices kill only one task */
Suren Baghdasaryan014dd712022-02-18 12:51:15 -08003069 if (find_and_kill_process(level_oomadj[level], NULL, &mi, &wi, &curr_tm, NULL) == 0) {
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -08003070 if (debug_process_killing) {
3071 ALOGI("Nothing to kill");
3072 }
3073 }
3074 } else {
Suren Baghdasaryan2c4611a2018-04-13 13:53:43 -07003075 int pages_freed;
Suren Baghdasaryan53be36e2018-09-05 15:46:32 -07003076 static struct timespec last_report_tm;
3077 static unsigned long report_skip_count = 0;
Suren Baghdasaryan2c4611a2018-04-13 13:53:43 -07003078
3079 if (!use_minfree_levels) {
Suren Baghdasaryan2c4611a2018-04-13 13:53:43 -07003080 /* Free up enough memory to downgrate the memory pressure to low level */
Suren Baghdasaryan85c31b52018-10-26 11:32:15 -07003081 if (mi.field.nr_free_pages >= low_pressure_mem.max_nr_free_pages) {
Suren Baghdasaryan2c4611a2018-04-13 13:53:43 -07003082 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];
Suren Baghdasaryan94ccd722018-01-17 17:17:44 -08003090 }
3091
Suren Baghdasaryan014dd712022-02-18 12:51:15 -08003092 pages_freed = find_and_kill_process(min_score_adj, NULL, &mi, &wi, &curr_tm, NULL);
Suren Baghdasaryaneff82332018-05-10 16:10:56 -07003093
Suren Baghdasaryan53be36e2018-09-05 15:46:32 -07003094 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++;
Suren Baghdasaryan1ed0db12018-07-24 17:13:06 -07003098 return;
3099 }
Robert Benea7f68a3f2017-08-11 16:03:20 -07003100 }
Suren Baghdasaryan53be36e2018-09-05 15:46:32 -07003101
Suren Baghdasaryan12cacae2019-09-16 12:06:30 -07003102 /* Log whenever we kill or when report rate limit allows */
Suren Baghdasaryan53be36e2018-09-05 15:46:32 -07003103 if (use_minfree_levels) {
Chris Morin74b4df92021-02-26 00:00:35 -08003104 ALOGI("Reclaimed %ldkB, cache(%ldkB) and free(%" PRId64 "kB)-reserved(%" PRId64 "kB) "
3105 "below min(%ldkB) for oom_score_adj %d",
Suren Baghdasaryan85c31b52018-10-26 11:32:15 -07003106 pages_freed * page_k,
Suren Baghdasaryan53be36e2018-09-05 15:46:32 -07003107 other_file * page_k, mi.field.nr_free_pages * page_k,
Suren Baghdasaryan92d0eec2019-07-15 13:54:20 -07003108 zi.totalreserve_pages * page_k,
Suren Baghdasaryan53be36e2018-09-05 15:46:32 -07003109 minfree * page_k, min_score_adj);
3110 } else {
Chris Morin74b4df92021-02-26 00:00:35 -08003111 ALOGI("Reclaimed %ldkB at oom_score_adj %d", pages_freed * page_k, min_score_adj);
Suren Baghdasaryan53be36e2018-09-05 15:46:32 -07003112 }
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;
Colin Cross01db2712014-07-11 17:16:56 -07003120 }
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003121 if (is_waiting_for_kill()) {
3122 /* pause polling if we are waiting for process death notification */
3123 poll_params->update = POLLING_PAUSE;
3124 }
Todd Poynorc58c5142013-07-09 19:35:14 -07003125}
3126
Suren Baghdasaryan2f88e152019-07-15 15:42:09 -07003127static 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,
Suren Baghdasaryan55e31502019-01-08 12:54:48 -08003136 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
Suren Baghdasaryan2f88e152019-07-15 15:42:09 -07003143 vmpressure_hinfo[level].handler = use_new_strategy ? mp_event_psi : mp_event_common;
Suren Baghdasaryan55e31502019-01-08 12:54:48 -08003144 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
3155static void destroy_mp_psi(enum vmpressure_level level) {
3156 int fd = mpevfd[level];
3157
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07003158 if (fd < 0) {
3159 return;
3160 }
3161
Suren Baghdasaryan55e31502019-01-08 12:54:48 -08003162 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 }
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07003166 maxevents--;
Suren Baghdasaryan55e31502019-01-08 12:54:48 -08003167 destroy_psi_monitor(fd);
3168 mpevfd[level] = -1;
3169}
3170
Bart Van Assche75994362022-02-15 23:45:04 +00003171enum class MemcgVersion {
3172 kNotFound,
3173 kV1,
3174 kV2,
3175};
3176
3177static 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
3189static MemcgVersion memcg_version() {
3190 static MemcgVersion version = __memcg_version();
3191
3192 return version;
3193}
3194
Suren Baghdasaryan55e31502019-01-08 12:54:48 -08003195static bool init_psi_monitors() {
Suren Baghdasaryan2f88e152019-07-15 15:42:09 -07003196 /*
3197 * When PSI is used on low-ram devices or on high-end devices without memfree levels
Bart Van Assche75994362022-02-15 23:45:04 +00003198 * 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.
Suren Baghdasaryan2f88e152019-07-15 15:42:09 -07003202 */
3203 bool use_new_strategy =
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003204 GET_LMK_PROPERTY(bool, "use_new_strategy", low_ram_device || !use_minfree_levels);
Bart Van Assche75994362022-02-15 23:45:04 +00003205 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 }
Suren Baghdasaryan2f88e152019-07-15 15:42:09 -07003209 /* 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)) {
Suren Baghdasaryan55e31502019-01-08 12:54:48 -08003218 return false;
3219 }
Suren Baghdasaryan2f88e152019-07-15 15:42:09 -07003220 if (!init_mp_psi(VMPRESS_LEVEL_MEDIUM, use_new_strategy)) {
Suren Baghdasaryan55e31502019-01-08 12:54:48 -08003221 destroy_mp_psi(VMPRESS_LEVEL_LOW);
3222 return false;
3223 }
Suren Baghdasaryan2f88e152019-07-15 15:42:09 -07003224 if (!init_mp_psi(VMPRESS_LEVEL_CRITICAL, use_new_strategy)) {
Suren Baghdasaryan55e31502019-01-08 12:54:48 -08003225 destroy_mp_psi(VMPRESS_LEVEL_MEDIUM);
3226 destroy_mp_psi(VMPRESS_LEVEL_LOW);
3227 return false;
3228 }
3229 return true;
3230}
3231
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08003232static bool init_mp_common(enum vmpressure_level level) {
Bart Van Assche75994362022-02-15 23:45:04 +00003233 // 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
Todd Poynorc58c5142013-07-09 19:35:14 -07003240 int mpfd;
3241 int evfd;
3242 int evctlfd;
3243 char buf[256];
3244 struct epoll_event epev;
3245 int ret;
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08003246 int level_idx = (int)level;
3247 const char *levelstr = level_name[level_idx];
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -08003248
Mark Salyzyna00ccd82018-04-09 09:50:32 -07003249 /* gid containing AID_SYSTEM required */
Bart Van Asscheb4d26bb2022-02-17 21:31:51 +00003250 mpfd = open(GetCgroupAttributePath("MemPressureLevel").c_str(), O_RDONLY | O_CLOEXEC);
Todd Poynorc58c5142013-07-09 19:35:14 -07003251 if (mpfd < 0) {
3252 ALOGI("No kernel memory.pressure_level support (errno=%d)", errno);
3253 goto err_open_mpfd;
3254 }
3255
Bart Van Asscheb4d26bb2022-02-17 21:31:51 +00003256 evctlfd = open(GetCgroupAttributePath("CgroupEventControl").c_str(), O_WRONLY | O_CLOEXEC);
Todd Poynorc58c5142013-07-09 19:35:14 -07003257 if (evctlfd < 0) {
3258 ALOGI("No kernel memory cgroup event control (errno=%d)", errno);
3259 goto err_open_evctlfd;
3260 }
3261
Nick Kralevich148d8dd2015-12-18 20:52:37 -08003262 evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
Todd Poynorc58c5142013-07-09 19:35:14 -07003263 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
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08003274 ret = TEMP_FAILURE_RETRY(write(evctlfd, buf, strlen(buf) + 1));
Todd Poynorc58c5142013-07-09 19:35:14 -07003275 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;
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08003282 /* 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];
Todd Poynorc58c5142013-07-09 19:35:14 -07003286 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++;
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -08003292 mpevfd[level] = evfd;
Suren Baghdasaryanceffaf22018-01-04 08:54:53 -08003293 close(evctlfd);
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -08003294 return true;
Todd Poynorc58c5142013-07-09 19:35:14 -07003295
3296err:
3297 close(evfd);
3298err_eventfd:
3299 close(evctlfd);
3300err_open_evctlfd:
3301 close(mpfd);
3302err_open_mpfd:
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -08003303 return false;
Robert Benea58d6a132017-06-01 16:32:31 -07003304}
3305
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07003306static 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
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07003323static void kernel_event_handler(int data __unused, uint32_t events __unused,
3324 struct polling_params *poll_params __unused) {
Jing Ji5c480962019-12-04 09:22:05 -08003325 poll_kernel(kpoll_fd);
Jim Blackler700b7192019-04-26 11:18:29 +01003326}
3327
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07003328static bool init_monitors() {
3329 /* Try to use psi monitor first if kernel has it */
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003330 use_psi_monitors = GET_LMK_PROPERTY(bool, "use_psi", true) &&
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07003331 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
3348static 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
Suren Baghdasaryan7c3addb2021-06-11 12:12:56 -07003360static void drop_reaper_comm() {
3361 close(reaper_comm_fd[0]);
3362 close(reaper_comm_fd[1]);
3363}
3364
3365static 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
3382static 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
Todd Poynorc58c5142013-07-09 19:35:14 -07003417static int init(void) {
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07003418 static struct event_handler_info kernel_poll_hinfo = { 0, kernel_event_handler };
Suren Baghdasaryan03cb8362019-07-15 13:35:04 -07003419 struct reread_data file_data = {
3420 .filename = ZONEINFO_PATH,
3421 .fd = -1,
3422 };
Todd Poynorc58c5142013-07-09 19:35:14 -07003423 struct epoll_event epev;
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003424 int pidfd;
Todd Poynorc58c5142013-07-09 19:35:14 -07003425 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
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08003439 // 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) {
Todd Poynorc58c5142013-07-09 19:35:14 -07003446 ALOGE("get lmkd control socket failed");
3447 return -1;
3448 }
3449
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08003450 ret = listen(ctrl_sock.sock, MAX_DATA_CONN);
Todd Poynorc58c5142013-07-09 19:35:14 -07003451 if (ret < 0) {
3452 ALOGE("lmkd control socket listen failed (errno=%d)", errno);
3453 return -1;
3454 }
3455
3456 epev.events = EPOLLIN;
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08003457 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) {
Todd Poynorc58c5142013-07-09 19:35:14 -07003460 ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno);
3461 return -1;
3462 }
3463 maxevents++;
3464
Robert Benea7878c9b2017-09-11 16:53:28 -07003465 has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK);
Suren Baghdasaryane6613ea2018-01-18 17:27:30 -08003466 use_inkernel_interface = has_inkernel_module;
Todd Poynorc58c5142013-07-09 19:35:14 -07003467
3468 if (use_inkernel_interface) {
3469 ALOGI("Using in-kernel low memory killer interface");
Jing Ji5c480962019-12-04 09:22:05 -08003470 if (init_poll_kernel()) {
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07003471 epev.events = EPOLLIN;
3472 epev.data.ptr = (void*)&kernel_poll_hinfo;
Jing Ji5c480962019-12-04 09:22:05 -08003473 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, kpoll_fd, &epev) != 0) {
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07003474 ALOGE("epoll_ctl for lmk events failed (errno=%d)", errno);
Jing Ji5c480962019-12-04 09:22:05 -08003475 close(kpoll_fd);
3476 kpoll_fd = -1;
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07003477 } else {
3478 maxevents++;
Jing Ji5c480962019-12-04 09:22:05 -08003479 /* let the others know it does support reporting kills */
3480 property_set("sys.lmk.reportkills", "1");
Suren Baghdasaryan8b016be2019-09-04 19:12:29 -07003481 }
Jim Blackler700b7192019-04-26 11:18:29 +01003482 }
Todd Poynorc58c5142013-07-09 19:35:14 -07003483 } else {
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07003484 if (!init_monitors()) {
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -08003485 return -1;
3486 }
Jing Ji5c480962019-12-04 09:22:05 -08003487 /* let the others know it does support reporting kills */
3488 property_set("sys.lmk.reportkills", "1");
Todd Poynorc58c5142013-07-09 19:35:14 -07003489 }
3490
Chong Zhang1cd12b52015-10-14 16:19:53 -07003491 for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) {
Todd Poynorc58c5142013-07-09 19:35:14 -07003492 procadjslot_list[i].next = &procadjslot_list[i];
3493 procadjslot_list[i].prev = &procadjslot_list[i];
3494 }
3495
Suren Baghdasaryana7394ea2018-10-12 11:07:40 -07003496 memset(killcnt_idx, KILLCNT_INVALID_IDX, sizeof(killcnt_idx));
3497
Suren Baghdasaryan03cb8362019-07-15 13:35:04 -07003498 /*
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
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003506 /* check if kernel supports pidfd_open syscall */
Josh Gao84623be2021-03-18 17:16:08 -07003507 pidfd = TEMP_FAILURE_RETRY(pidfd_open(getpid(), 0));
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003508 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
Todd Poynorc58c5142013-07-09 19:35:14 -07003516 return 0;
3517}
3518
Suren Baghdasaryan03dccf32020-04-28 16:02:13 -07003519static bool polling_paused(struct polling_params *poll_params) {
3520 return poll_params->paused_handler != NULL;
3521}
3522
3523static 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;
Martin Liu589b5752020-09-02 23:15:18 +08003526 poll_params->polling_interval_ms = PSI_POLL_PERIOD_SHORT_MS;
3527 poll_params->paused_handler = NULL;
Suren Baghdasaryan03dccf32020-04-28 16:02:13 -07003528}
3529
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003530static void call_handler(struct event_handler_info* handler_info,
3531 struct polling_params *poll_params, uint32_t events) {
3532 struct timespec curr_tm;
3533
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -08003534 watchdog.start();
Suren Baghdasaryan9ca53342020-04-24 16:54:55 -07003535 poll_params->update = POLLING_DO_NOT_CHANGE;
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003536 handler_info->handler(handler_info->data, events, poll_params);
3537 clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm);
Suren Baghdasaryan9ca53342020-04-24 16:54:55 -07003538 if (poll_params->poll_handler == handler_info) {
3539 poll_params->last_poll_tm = curr_tm;
3540 }
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003541
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;
Greg Kaiser5e80ed52019-10-10 06:52:23 -07003550 poll_params->poll_handler = handler_info;
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003551 break;
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003552 case POLLING_PAUSE:
3553 poll_params->paused_handler = handler_info;
3554 poll_params->poll_handler = NULL;
3555 break;
3556 case POLLING_RESUME:
Suren Baghdasaryan03dccf32020-04-28 16:02:13 -07003557 resume_polling(poll_params, curr_tm);
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003558 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 }
Suren Baghdasaryan9ca53342020-04-24 16:54:55 -07003564 break;
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003565 }
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -08003566 watchdog.stop();
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003567}
3568
Todd Poynorc58c5142013-07-09 19:35:14 -07003569static void mainloop(void) {
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08003570 struct event_handler_info* handler_info;
Suren Baghdasaryane12a0672019-07-15 14:50:49 -07003571 struct polling_params poll_params;
3572 struct timespec curr_tm;
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08003573 struct epoll_event *evt;
Suren Baghdasaryan55e31502019-01-08 12:54:48 -08003574 long delay = -1;
Suren Baghdasaryane12a0672019-07-15 14:50:49 -07003575
3576 poll_params.poll_handler = NULL;
Suren Baghdasaryan9ca53342020-04-24 16:54:55 -07003577 poll_params.paused_handler = NULL;
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08003578
Todd Poynorc58c5142013-07-09 19:35:14 -07003579 while (1) {
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07003580 struct epoll_event events[MAX_EPOLL_EVENTS];
Todd Poynorc58c5142013-07-09 19:35:14 -07003581 int nevents;
3582 int i;
3583
Suren Baghdasaryane12a0672019-07-15 14:50:49 -07003584 if (poll_params.poll_handler) {
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003585 bool poll_now;
Suren Baghdasaryan55e31502019-01-08 12:54:48 -08003586
3587 clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm);
Martin Liu589b5752020-09-02 23:15:18 +08003588 if (poll_params.update == POLLING_RESUME) {
3589 /* Just transitioned into POLLING_RESUME, poll immediately. */
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003590 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;
Suren Baghdasaryane12a0672019-07-15 14:50:49 -07003597
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003598 /* 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);
Suren Baghdasaryan55e31502019-01-08 12:54:48 -08003608 }
3609 } else {
Suren Baghdasaryan03dccf32020-04-28 16:02:13 -07003610 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);
Martin Liu589b5752020-09-02 23:15:18 +08003620 poll_params.update = POLLING_RESUME;
Suren Baghdasaryan03dccf32020-04-28 16:02:13 -07003621 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 }
Suren Baghdasaryan55e31502019-01-08 12:54:48 -08003628 }
Todd Poynorc58c5142013-07-09 19:35:14 -07003629
3630 if (nevents == -1) {
3631 if (errno == EINTR)
3632 continue;
3633 ALOGE("epoll_wait failed (errno=%d)", errno);
3634 continue;
3635 }
3636
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08003637 /*
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;
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -08003648 watchdog.start();
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08003649 ctrl_data_close(handler_info->data);
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -08003650 watchdog.stop();
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08003651 }
3652 }
3653
3654 /* Second pass to handle all other events */
3655 for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) {
Suren Baghdasaryane12a0672019-07-15 14:50:49 -07003656 if (evt->events & EPOLLERR) {
Todd Poynorc58c5142013-07-09 19:35:14 -07003657 ALOGD("EPOLLERR on event #%d", i);
Suren Baghdasaryane12a0672019-07-15 14:50:49 -07003658 }
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08003659 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;
Suren Baghdasaryanbc99e1b2019-06-26 17:56:01 -07003665 call_handler(handler_info, &poll_params, evt->events);
Suren Baghdasaryanef8e7012018-01-26 12:51:19 -08003666 }
Todd Poynorc58c5142013-07-09 19:35:14 -07003667 }
3668 }
3669}
3670
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07003671int issue_reinit() {
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07003672 int sock;
Colin Crossd5b510e2014-07-14 14:31:15 -07003673
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07003674 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
3703static void update_props() {
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -08003704 /* By default disable low level vmpressure events */
3705 level_oomadj[VMPRESS_LEVEL_LOW] =
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003706 GET_LMK_PROPERTY(int32, "low", OOM_SCORE_ADJ_MAX + 1);
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -08003707 level_oomadj[VMPRESS_LEVEL_MEDIUM] =
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003708 GET_LMK_PROPERTY(int32, "medium", 800);
Suren Baghdasaryanb2e00602017-12-08 12:58:52 -08003709 level_oomadj[VMPRESS_LEVEL_CRITICAL] =
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003710 GET_LMK_PROPERTY(int32, "critical", 0);
3711 debug_process_killing = GET_LMK_PROPERTY(bool, "debug", false);
Suren Baghdasaryan3faa3032017-12-08 13:08:41 -08003712
3713 /* By default disable upgrade/downgrade logic */
3714 enable_pressure_upgrade =
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003715 GET_LMK_PROPERTY(bool, "critical_upgrade", false);
Suren Baghdasaryan3faa3032017-12-08 13:08:41 -08003716 upgrade_pressure =
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003717 (int64_t)GET_LMK_PROPERTY(int32, "upgrade_pressure", 100);
Suren Baghdasaryan3faa3032017-12-08 13:08:41 -08003718 downgrade_pressure =
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003719 (int64_t)GET_LMK_PROPERTY(int32, "downgrade_pressure", 100);
Suren Baghdasaryaneb7c5492017-12-08 13:17:06 -08003720 kill_heaviest_task =
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003721 GET_LMK_PROPERTY(bool, "kill_heaviest_task", false);
Suren Baghdasaryand1d59f82018-04-13 11:45:38 -07003722 low_ram_device = property_get_bool("ro.config.low_ram", false);
Suren Baghdasaryan30854e72018-01-17 17:28:01 -08003723 kill_timeout_ms =
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003724 (unsigned long)GET_LMK_PROPERTY(int32, "kill_timeout_ms", 100);
Suren Baghdasaryan2c4611a2018-04-13 13:53:43 -07003725 use_minfree_levels =
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003726 GET_LMK_PROPERTY(bool, "use_minfree_levels", false);
Suren Baghdasaryan8389fdb2018-06-19 18:38:12 -07003727 per_app_memcg =
3728 property_get_bool("ro.config.per_app_memcg", low_ram_device);
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003729 swap_free_low_percentage = clamp(0, 100, GET_LMK_PROPERTY(int32, "swap_free_low_percentage",
Suren Baghdasaryanfb1f5922020-05-19 13:07:23 -07003730 DEF_LOW_SWAP));
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003731 psi_partial_stall_ms = GET_LMK_PROPERTY(int32, "psi_partial_stall_ms",
Suren Baghdasaryan2f88e152019-07-15 15:42:09 -07003732 low_ram_device ? DEF_PARTIAL_STALL_LOWRAM : DEF_PARTIAL_STALL);
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003733 psi_complete_stall_ms = GET_LMK_PROPERTY(int32, "psi_complete_stall_ms",
Suren Baghdasaryan2f88e152019-07-15 15:42:09 -07003734 DEF_COMPLETE_STALL);
Bart Van Assche80a3dba2022-02-02 23:51:35 +00003735 thrashing_limit_pct =
3736 std::max(0, GET_LMK_PROPERTY(int32, "thrashing_limit",
3737 low_ram_device ? DEF_THRASHING_LOWRAM : DEF_THRASHING));
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003738 thrashing_limit_decay_pct = clamp(0, 100, GET_LMK_PROPERTY(int32, "thrashing_limit_decay",
Suren Baghdasaryanaf2be4c2019-07-15 15:35:44 -07003739 low_ram_device ? DEF_THRASHING_DECAY_LOWRAM : DEF_THRASHING_DECAY));
Bart Van Assche80a3dba2022-02-02 23:51:35 +00003740 thrashing_critical_pct = std::max(
3741 0, GET_LMK_PROPERTY(int32, "thrashing_limit_critical", thrashing_limit_pct * 2));
Suren Baghdasaryand0a80042021-08-03 15:40:23 -07003742 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);
Suren Baghdasaryan5ae47a92022-02-10 21:10:23 -08003744 stall_limit_critical = GET_LMK_PROPERTY(int64, "stall_limit_critical", 100);
Suren Baghdasaryan7c3addb2021-06-11 12:12:56 -07003745
3746 reaper.enable_debug(debug_process_killing);
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07003747}
3748
3749int main(int argc, char **argv) {
3750 if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
Suren Baghdasaryan0e64ead2021-09-01 00:49:51 -07003751 if (property_set(LMKD_REINIT_PROP, "")) {
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07003752 ALOGE("Failed to reset " LMKD_REINIT_PROP " property");
3753 }
3754 return issue_reinit();
3755 }
3756
3757 update_props();
Robert Benea57397dc2017-07-31 17:15:20 -07003758
Suren Baghdasaryan12cacae2019-09-16 12:06:30 -07003759 ctx = create_android_logger(KILLINFO_LOG_TAG);
Suren Baghdasaryan08bfa982018-07-26 16:34:27 -07003760
Mark Salyzyn5cc80b32018-03-21 12:24:58 -07003761 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 */
Mark Salyzyna00ccd82018-04-09 09:50:32 -07003774 /* CAP_IPC_LOCK required */
Mark Salyzyn5cc80b32018-03-21 12:24:58 -07003775 if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && (errno != EINVAL)) {
3776 ALOGW("mlockall failed %s", strerror(errno));
3777 }
Daniel Colascione46648332018-01-03 12:01:02 -08003778
Mark Salyzyna00ccd82018-04-09 09:50:32 -07003779 /* CAP_NICE required */
Suren Baghdasaryan1d0ebea2020-04-28 15:52:29 -07003780 struct sched_param param = {
3781 .sched_priority = 1,
3782 };
Mark Salyzyna00ccd82018-04-09 09:50:32 -07003783 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
3784 ALOGW("set SCHED_FIFO failed %s", strerror(errno));
3785 }
Mark Salyzyn5cc80b32018-03-21 12:24:58 -07003786 }
3787
Suren Baghdasaryan7c3addb2021-06-11 12:12:56 -07003788 if (init_reaper()) {
3789 ALOGI("Process reaper initialized with %d threads in the pool",
3790 reaper.thread_cnt());
3791 }
3792
Suren Baghdasaryanaf1b0e02021-11-16 11:50:29 -08003793 if (!watchdog.init()) {
3794 ALOGE("Failed to initialize the watchdog");
3795 }
3796
Todd Poynorc58c5142013-07-09 19:35:14 -07003797 mainloop();
Mark Salyzyn5cc80b32018-03-21 12:24:58 -07003798 }
Todd Poynorc58c5142013-07-09 19:35:14 -07003799
Suren Baghdasaryan08bfa982018-07-26 16:34:27 -07003800 android_log_destroy(&ctx);
3801
Todd Poynorc58c5142013-07-09 19:35:14 -07003802 ALOGI("exiting");
3803 return 0;
3804}