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