Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Linux VM pressure |
| 3 | * |
| 4 | * Copyright 2012 Linaro Ltd. |
| 5 | * Anton Vorontsov <anton.vorontsov@linaro.org> |
| 6 | * |
| 7 | * Based on ideas from Andrew Morton, David Rientjes, KOSAKI Motohiro, |
| 8 | * Leonid Moiseichuk, Mel Gorman, Minchan Kim and Pekka Enberg. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License version 2 as published |
| 12 | * by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/cgroup.h> |
| 16 | #include <linux/fs.h> |
| 17 | #include <linux/log2.h> |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/vmstat.h> |
| 21 | #include <linux/eventfd.h> |
Tejun Heo | 1ff6bbf | 2014-01-28 18:10:37 -0500 | [diff] [blame] | 22 | #include <linux/slab.h> |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 23 | #include <linux/swap.h> |
| 24 | #include <linux/printk.h> |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 25 | #include <linux/notifier.h> |
| 26 | #include <linux/init.h> |
Vinayak Menon | 5809109 | 2015-03-31 11:06:29 +0530 | [diff] [blame] | 27 | #include <linux/module.h> |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 28 | #include <linux/vmpressure.h> |
| 29 | |
| 30 | /* |
| 31 | * The window size (vmpressure_win) is the number of scanned pages before |
| 32 | * we try to analyze scanned/reclaimed ratio. So the window is used as a |
| 33 | * rate-limit tunable for the "low" level notification, and also for |
| 34 | * averaging the ratio for medium/critical levels. Using small window |
| 35 | * sizes can cause lot of false positives, but too big window size will |
| 36 | * delay the notifications. |
| 37 | * |
| 38 | * As the vmscan reclaimer logic works with chunks which are multiple of |
| 39 | * SWAP_CLUSTER_MAX, it makes sense to use it for the window size as well. |
| 40 | * |
| 41 | * TODO: Make the window size depend on machine size, as we do for vmstat |
| 42 | * thresholds. Currently we set it to 512 pages (2MB for 4KB pages). |
| 43 | */ |
Vinayak Menon | 9cd3302 | 2016-09-19 12:44:15 +0530 | [diff] [blame] | 44 | static unsigned long vmpressure_win = SWAP_CLUSTER_MAX * 16; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * These thresholds are used when we account memory pressure through |
| 48 | * scanned/reclaimed ratio. The current values were chosen empirically. In |
| 49 | * essence, they are percents: the higher the value, the more number |
| 50 | * unsuccessful reclaims there were. |
| 51 | */ |
| 52 | static const unsigned int vmpressure_level_med = 60; |
| 53 | static const unsigned int vmpressure_level_critical = 95; |
| 54 | |
Vinayak Menon | 5809109 | 2015-03-31 11:06:29 +0530 | [diff] [blame] | 55 | static unsigned long vmpressure_scale_max = 100; |
| 56 | module_param_named(vmpressure_scale_max, vmpressure_scale_max, |
| 57 | ulong, 0644); |
| 58 | |
Vinayak Menon | 44778e5 | 2015-08-19 16:16:39 +0530 | [diff] [blame] | 59 | /* vmpressure values >= this will be scaled based on allocstalls */ |
| 60 | static unsigned long allocstall_threshold = 70; |
| 61 | module_param_named(allocstall_threshold, allocstall_threshold, |
| 62 | ulong, 0644); |
| 63 | |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 64 | static struct vmpressure global_vmpressure; |
| 65 | static BLOCKING_NOTIFIER_HEAD(vmpressure_notifier); |
| 66 | |
| 67 | int vmpressure_notifier_register(struct notifier_block *nb) |
| 68 | { |
| 69 | return blocking_notifier_chain_register(&vmpressure_notifier, nb); |
| 70 | } |
| 71 | |
| 72 | int vmpressure_notifier_unregister(struct notifier_block *nb) |
| 73 | { |
| 74 | return blocking_notifier_chain_unregister(&vmpressure_notifier, nb); |
| 75 | } |
| 76 | |
| 77 | static void vmpressure_notify(unsigned long pressure) |
| 78 | { |
| 79 | blocking_notifier_call_chain(&vmpressure_notifier, pressure, NULL); |
| 80 | } |
| 81 | |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 82 | /* |
| 83 | * When there are too little pages left to scan, vmpressure() may miss the |
| 84 | * critical pressure as number of pages will be less than "window size". |
| 85 | * However, in that case the vmscan priority will raise fast as the |
| 86 | * reclaimer will try to scan LRUs more deeply. |
| 87 | * |
| 88 | * The vmscan logic considers these special priorities: |
| 89 | * |
| 90 | * prio == DEF_PRIORITY (12): reclaimer starts with that value |
| 91 | * prio <= DEF_PRIORITY - 2 : kswapd becomes somewhat overwhelmed |
| 92 | * prio == 0 : close to OOM, kernel scans every page in an lru |
| 93 | * |
| 94 | * Any value in this range is acceptable for this tunable (i.e. from 12 to |
| 95 | * 0). Current value for the vmpressure_level_critical_prio is chosen |
| 96 | * empirically, but the number, in essence, means that we consider |
| 97 | * critical level when scanning depth is ~10% of the lru size (vmscan |
| 98 | * scans 'lru_size >> prio' pages, so it is actually 12.5%, or one |
| 99 | * eights). |
| 100 | */ |
| 101 | static const unsigned int vmpressure_level_critical_prio = ilog2(100 / 10); |
| 102 | |
| 103 | static struct vmpressure *work_to_vmpressure(struct work_struct *work) |
| 104 | { |
| 105 | return container_of(work, struct vmpressure, work); |
| 106 | } |
| 107 | |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 108 | #ifdef CONFIG_MEMCG |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 109 | static struct vmpressure *vmpressure_parent(struct vmpressure *vmpr) |
| 110 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 111 | struct cgroup_subsys_state *css = vmpressure_to_css(vmpr); |
| 112 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 113 | |
| 114 | memcg = parent_mem_cgroup(memcg); |
| 115 | if (!memcg) |
| 116 | return NULL; |
| 117 | return memcg_to_vmpressure(memcg); |
| 118 | } |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 119 | #else |
| 120 | static struct vmpressure *vmpressure_parent(struct vmpressure *vmpr) |
| 121 | { |
| 122 | return NULL; |
| 123 | } |
| 124 | #endif |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 125 | |
| 126 | enum vmpressure_levels { |
| 127 | VMPRESSURE_LOW = 0, |
| 128 | VMPRESSURE_MEDIUM, |
| 129 | VMPRESSURE_CRITICAL, |
| 130 | VMPRESSURE_NUM_LEVELS, |
| 131 | }; |
| 132 | |
| 133 | static const char * const vmpressure_str_levels[] = { |
| 134 | [VMPRESSURE_LOW] = "low", |
| 135 | [VMPRESSURE_MEDIUM] = "medium", |
| 136 | [VMPRESSURE_CRITICAL] = "critical", |
| 137 | }; |
| 138 | |
| 139 | static enum vmpressure_levels vmpressure_level(unsigned long pressure) |
| 140 | { |
| 141 | if (pressure >= vmpressure_level_critical) |
| 142 | return VMPRESSURE_CRITICAL; |
| 143 | else if (pressure >= vmpressure_level_med) |
| 144 | return VMPRESSURE_MEDIUM; |
| 145 | return VMPRESSURE_LOW; |
| 146 | } |
| 147 | |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 148 | static unsigned long vmpressure_calc_pressure(unsigned long scanned, |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 149 | unsigned long reclaimed) |
| 150 | { |
| 151 | unsigned long scale = scanned + reclaimed; |
Vinayak Menon | 58d1dbb | 2017-02-24 14:59:39 -0800 | [diff] [blame] | 152 | unsigned long pressure = 0; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 153 | |
| 154 | /* |
Vinayak Menon | 58d1dbb | 2017-02-24 14:59:39 -0800 | [diff] [blame] | 155 | * reclaimed can be greater than scanned in cases |
| 156 | * like THP, where the scanned is 1 and reclaimed |
| 157 | * could be 512 |
| 158 | */ |
| 159 | if (reclaimed >= scanned) |
| 160 | goto out; |
| 161 | /* |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 162 | * We calculate the ratio (in percents) of how many pages were |
| 163 | * scanned vs. reclaimed in a given time frame (window). Note that |
| 164 | * time is in VM reclaimer's "ticks", i.e. number of pages |
| 165 | * scanned. This makes it possible to set desired reaction time |
| 166 | * and serves as a ratelimit. |
| 167 | */ |
| 168 | pressure = scale - (reclaimed * scale / scanned); |
| 169 | pressure = pressure * 100 / scale; |
| 170 | |
Vinayak Menon | 58d1dbb | 2017-02-24 14:59:39 -0800 | [diff] [blame] | 171 | out: |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 172 | pr_debug("%s: %3lu (s: %lu r: %lu)\n", __func__, pressure, |
| 173 | scanned, reclaimed); |
| 174 | |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 175 | return pressure; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Vinayak Menon | 5809109 | 2015-03-31 11:06:29 +0530 | [diff] [blame] | 178 | static unsigned long vmpressure_account_stall(unsigned long pressure, |
| 179 | unsigned long stall, unsigned long scanned) |
| 180 | { |
Vinayak Menon | 44778e5 | 2015-08-19 16:16:39 +0530 | [diff] [blame] | 181 | unsigned long scale; |
| 182 | |
| 183 | if (pressure < allocstall_threshold) |
| 184 | return pressure; |
| 185 | |
| 186 | scale = ((vmpressure_scale_max - pressure) * stall) / scanned; |
Vinayak Menon | 5809109 | 2015-03-31 11:06:29 +0530 | [diff] [blame] | 187 | |
| 188 | return pressure + scale; |
| 189 | } |
| 190 | |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 191 | struct vmpressure_event { |
| 192 | struct eventfd_ctx *efd; |
| 193 | enum vmpressure_levels level; |
| 194 | struct list_head node; |
| 195 | }; |
| 196 | |
| 197 | static bool vmpressure_event(struct vmpressure *vmpr, |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 198 | enum vmpressure_levels level) |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 199 | { |
| 200 | struct vmpressure_event *ev; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 201 | bool signalled = false; |
| 202 | |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 203 | mutex_lock(&vmpr->events_lock); |
| 204 | |
| 205 | list_for_each_entry(ev, &vmpr->events, node) { |
| 206 | if (level >= ev->level) { |
| 207 | eventfd_signal(ev->efd, 1); |
| 208 | signalled = true; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | mutex_unlock(&vmpr->events_lock); |
| 213 | |
| 214 | return signalled; |
| 215 | } |
| 216 | |
| 217 | static void vmpressure_work_fn(struct work_struct *work) |
| 218 | { |
| 219 | struct vmpressure *vmpr = work_to_vmpressure(work); |
| 220 | unsigned long scanned; |
| 221 | unsigned long reclaimed; |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 222 | unsigned long pressure; |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 223 | enum vmpressure_levels level; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 224 | |
Andrew Morton | 91b5719 | 2014-12-02 15:59:28 -0800 | [diff] [blame] | 225 | spin_lock(&vmpr->sr_lock); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 226 | /* |
| 227 | * Several contexts might be calling vmpressure(), so it is |
| 228 | * possible that the work was rescheduled again before the old |
| 229 | * work context cleared the counters. In that case we will run |
| 230 | * just after the old work returns, but then scanned might be zero |
| 231 | * here. No need for any locks here since we don't care if |
| 232 | * vmpr->reclaimed is in sync. |
| 233 | */ |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 234 | scanned = vmpr->tree_scanned; |
Andrew Morton | 91b5719 | 2014-12-02 15:59:28 -0800 | [diff] [blame] | 235 | if (!scanned) { |
| 236 | spin_unlock(&vmpr->sr_lock); |
| 237 | return; |
| 238 | } |
| 239 | |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 240 | reclaimed = vmpr->tree_reclaimed; |
| 241 | vmpr->tree_scanned = 0; |
| 242 | vmpr->tree_reclaimed = 0; |
Michal Hocko | 22f2020 | 2013-07-31 13:53:48 -0700 | [diff] [blame] | 243 | spin_unlock(&vmpr->sr_lock); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 244 | |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 245 | pressure = vmpressure_calc_pressure(scanned, reclaimed); |
| 246 | level = vmpressure_level(pressure); |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 247 | |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 248 | do { |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 249 | if (vmpressure_event(vmpr, level)) |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 250 | break; |
| 251 | /* |
| 252 | * If not handled, propagate the event upward into the |
| 253 | * hierarchy. |
| 254 | */ |
| 255 | } while ((vmpr = vmpressure_parent(vmpr))); |
| 256 | } |
| 257 | |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 258 | #ifdef CONFIG_MEMCG |
| 259 | static void vmpressure_memcg(gfp_t gfp, struct mem_cgroup *memcg, bool tree, |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 260 | unsigned long scanned, unsigned long reclaimed) |
| 261 | { |
| 262 | struct vmpressure *vmpr = memcg_to_vmpressure(memcg); |
| 263 | |
| 264 | /* |
| 265 | * Here we only want to account pressure that userland is able to |
| 266 | * help us with. For example, suppose that DMA zone is under |
| 267 | * pressure; if we notify userland about that kind of pressure, |
| 268 | * then it will be mostly a waste as it will trigger unnecessary |
| 269 | * freeing of memory by userland (since userland is more likely to |
| 270 | * have HIGHMEM/MOVABLE pages instead of the DMA fallback). That |
| 271 | * is why we include only movable, highmem and FS/IO pages. |
| 272 | * Indirect reclaim (kswapd) sets sc->gfp_mask to GFP_KERNEL, so |
| 273 | * we account it too. |
| 274 | */ |
| 275 | if (!(gfp & (__GFP_HIGHMEM | __GFP_MOVABLE | __GFP_IO | __GFP_FS))) |
| 276 | return; |
| 277 | |
| 278 | /* |
| 279 | * If we got here with no pages scanned, then that is an indicator |
| 280 | * that reclaimer was unable to find any shrinkable LRUs at the |
| 281 | * current scanning depth. But it does not mean that we should |
| 282 | * report the critical pressure, yet. If the scanning priority |
| 283 | * (scanning depth) goes too high (deep), we will be notified |
| 284 | * through vmpressure_prio(). But so far, keep calm. |
| 285 | */ |
| 286 | if (!scanned) |
| 287 | return; |
| 288 | |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 289 | if (tree) { |
| 290 | spin_lock(&vmpr->sr_lock); |
Vladimir Davydov | 3c1da7b | 2016-02-02 16:57:49 -0800 | [diff] [blame] | 291 | scanned = vmpr->tree_scanned += scanned; |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 292 | vmpr->tree_reclaimed += reclaimed; |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 293 | spin_unlock(&vmpr->sr_lock); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 294 | |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 295 | if (scanned < vmpressure_win) |
| 296 | return; |
| 297 | schedule_work(&vmpr->work); |
| 298 | } else { |
| 299 | enum vmpressure_levels level; |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 300 | unsigned long pressure; |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 301 | |
| 302 | /* For now, no users for root-level efficiency */ |
Hugh Dickins | 686739f | 2016-01-14 15:21:37 -0800 | [diff] [blame] | 303 | if (!memcg || memcg == root_mem_cgroup) |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 304 | return; |
| 305 | |
| 306 | spin_lock(&vmpr->sr_lock); |
| 307 | scanned = vmpr->scanned += scanned; |
| 308 | reclaimed = vmpr->reclaimed += reclaimed; |
| 309 | if (scanned < vmpressure_win) { |
| 310 | spin_unlock(&vmpr->sr_lock); |
| 311 | return; |
| 312 | } |
| 313 | vmpr->scanned = vmpr->reclaimed = 0; |
| 314 | spin_unlock(&vmpr->sr_lock); |
| 315 | |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 316 | pressure = vmpressure_calc_pressure(scanned, reclaimed); |
| 317 | level = vmpressure_level(pressure); |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 318 | |
| 319 | if (level > VMPRESSURE_LOW) { |
| 320 | /* |
| 321 | * Let the socket buffer allocator know that |
| 322 | * we are having trouble reclaiming LRU pages. |
| 323 | * |
| 324 | * For hysteresis keep the pressure state |
| 325 | * asserted for a second in which subsequent |
| 326 | * pressure events can occur. |
| 327 | */ |
| 328 | memcg->socket_pressure = jiffies + HZ; |
| 329 | } |
| 330 | } |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 331 | } |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 332 | #else |
| 333 | static void vmpressure_memcg(gfp_t gfp, struct mem_cgroup *memcg, bool tree, |
| 334 | unsigned long scanned, unsigned long reclaimed) { } |
| 335 | #endif |
| 336 | |
Vinayak Menon | 9cd3302 | 2016-09-19 12:44:15 +0530 | [diff] [blame] | 337 | static void calculate_vmpressure_win(void) |
| 338 | { |
| 339 | long x; |
| 340 | |
| 341 | x = global_node_page_state(NR_FILE_PAGES) - |
| 342 | global_node_page_state(NR_SHMEM) - |
| 343 | total_swapcache_pages() + |
| 344 | global_page_state(NR_FREE_PAGES); |
| 345 | if (x < 1) |
| 346 | x = 1; |
| 347 | /* |
| 348 | * For low (free + cached), vmpressure window should be |
| 349 | * small, and high for higher values of (free + cached). |
| 350 | * But it should not be linear as well. This ensures |
| 351 | * timely vmpressure notifications when system is under |
| 352 | * memory pressure, and optimal number of events when |
| 353 | * cached is high. The sqaure root function is empirically |
| 354 | * found to serve the purpose. |
| 355 | */ |
| 356 | x = int_sqrt(x); |
| 357 | vmpressure_win = x; |
| 358 | } |
| 359 | |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 360 | static void vmpressure_global(gfp_t gfp, unsigned long scanned, |
| 361 | unsigned long reclaimed) |
| 362 | { |
| 363 | struct vmpressure *vmpr = &global_vmpressure; |
| 364 | unsigned long pressure; |
Vinayak Menon | 5809109 | 2015-03-31 11:06:29 +0530 | [diff] [blame] | 365 | unsigned long stall; |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 366 | |
| 367 | if (!(gfp & (__GFP_HIGHMEM | __GFP_MOVABLE | __GFP_IO | __GFP_FS))) |
| 368 | return; |
| 369 | |
| 370 | if (!scanned) |
| 371 | return; |
| 372 | |
| 373 | spin_lock(&vmpr->sr_lock); |
Vinayak Menon | 9cd3302 | 2016-09-19 12:44:15 +0530 | [diff] [blame] | 374 | if (!vmpr->scanned) |
| 375 | calculate_vmpressure_win(); |
| 376 | |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 377 | vmpr->scanned += scanned; |
| 378 | vmpr->reclaimed += reclaimed; |
Vinayak Menon | 5809109 | 2015-03-31 11:06:29 +0530 | [diff] [blame] | 379 | |
| 380 | if (!current_is_kswapd()) |
| 381 | vmpr->stall += scanned; |
| 382 | |
| 383 | stall = vmpr->stall; |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 384 | scanned = vmpr->scanned; |
| 385 | reclaimed = vmpr->reclaimed; |
| 386 | spin_unlock(&vmpr->sr_lock); |
| 387 | |
| 388 | if (scanned < vmpressure_win) |
| 389 | return; |
| 390 | |
| 391 | spin_lock(&vmpr->sr_lock); |
| 392 | vmpr->scanned = 0; |
| 393 | vmpr->reclaimed = 0; |
Vinayak Menon | 5809109 | 2015-03-31 11:06:29 +0530 | [diff] [blame] | 394 | vmpr->stall = 0; |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 395 | spin_unlock(&vmpr->sr_lock); |
| 396 | |
| 397 | pressure = vmpressure_calc_pressure(scanned, reclaimed); |
Vinayak Menon | 5809109 | 2015-03-31 11:06:29 +0530 | [diff] [blame] | 398 | pressure = vmpressure_account_stall(pressure, stall, scanned); |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 399 | vmpressure_notify(pressure); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * vmpressure() - Account memory pressure through scanned/reclaimed ratio |
| 404 | * @gfp: reclaimer's gfp mask |
| 405 | * @memcg: cgroup memory controller handle |
| 406 | * @tree: legacy subtree mode |
| 407 | * @scanned: number of pages scanned |
| 408 | * @reclaimed: number of pages reclaimed |
| 409 | * |
| 410 | * This function should be called from the vmscan reclaim path to account |
| 411 | * "instantaneous" memory pressure (scanned/reclaimed ratio). The raw |
| 412 | * pressure index is then further refined and averaged over time. |
| 413 | * |
| 414 | * If @tree is set, vmpressure is in traditional userspace reporting |
| 415 | * mode: @memcg is considered the pressure root and userspace is |
| 416 | * notified of the entire subtree's reclaim efficiency. |
| 417 | * |
| 418 | * If @tree is not set, reclaim efficiency is recorded for @memcg, and |
| 419 | * only in-kernel users are notified. |
| 420 | * |
| 421 | * This function does not return any value. |
| 422 | */ |
| 423 | void vmpressure(gfp_t gfp, struct mem_cgroup *memcg, bool tree, |
| 424 | unsigned long scanned, unsigned long reclaimed) |
| 425 | { |
| 426 | if (!memcg) |
| 427 | vmpressure_global(gfp, scanned, reclaimed); |
| 428 | |
| 429 | if (IS_ENABLED(CONFIG_MEMCG)) |
| 430 | vmpressure_memcg(gfp, memcg, tree, scanned, reclaimed); |
| 431 | } |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 432 | |
| 433 | /** |
| 434 | * vmpressure_prio() - Account memory pressure through reclaimer priority level |
| 435 | * @gfp: reclaimer's gfp mask |
| 436 | * @memcg: cgroup memory controller handle |
| 437 | * @prio: reclaimer's priority |
| 438 | * |
| 439 | * This function should be called from the reclaim path every time when |
| 440 | * the vmscan's reclaiming priority (scanning depth) changes. |
| 441 | * |
| 442 | * This function does not return any value. |
| 443 | */ |
| 444 | void vmpressure_prio(gfp_t gfp, struct mem_cgroup *memcg, int prio) |
| 445 | { |
| 446 | /* |
| 447 | * We only use prio for accounting critical level. For more info |
| 448 | * see comment for vmpressure_level_critical_prio variable above. |
| 449 | */ |
| 450 | if (prio > vmpressure_level_critical_prio) |
| 451 | return; |
| 452 | |
| 453 | /* |
| 454 | * OK, the prio is below the threshold, updating vmpressure |
| 455 | * information before shrinker dives into long shrinking of long |
| 456 | * range vmscan. Passing scanned = vmpressure_win, reclaimed = 0 |
| 457 | * to the vmpressure() basically means that we signal 'critical' |
| 458 | * level. |
| 459 | */ |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 460 | vmpressure(gfp, memcg, true, vmpressure_win, 0); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /** |
| 464 | * vmpressure_register_event() - Bind vmpressure notifications to an eventfd |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 465 | * @memcg: memcg that is interested in vmpressure notifications |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 466 | * @eventfd: eventfd context to link notifications with |
| 467 | * @args: event arguments (used to set up a pressure level threshold) |
| 468 | * |
| 469 | * This function associates eventfd context with the vmpressure |
| 470 | * infrastructure, so that the notifications will be delivered to the |
| 471 | * @eventfd. The @args parameter is a string that denotes pressure level |
| 472 | * threshold (one of vmpressure_str_levels, i.e. "low", "medium", or |
| 473 | * "critical"). |
| 474 | * |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 475 | * To be used as memcg event method. |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 476 | */ |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 477 | int vmpressure_register_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 478 | struct eventfd_ctx *eventfd, const char *args) |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 479 | { |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 480 | struct vmpressure *vmpr = memcg_to_vmpressure(memcg); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 481 | struct vmpressure_event *ev; |
| 482 | int level; |
| 483 | |
| 484 | for (level = 0; level < VMPRESSURE_NUM_LEVELS; level++) { |
| 485 | if (!strcmp(vmpressure_str_levels[level], args)) |
| 486 | break; |
| 487 | } |
| 488 | |
| 489 | if (level >= VMPRESSURE_NUM_LEVELS) |
| 490 | return -EINVAL; |
| 491 | |
| 492 | ev = kzalloc(sizeof(*ev), GFP_KERNEL); |
| 493 | if (!ev) |
| 494 | return -ENOMEM; |
| 495 | |
| 496 | ev->efd = eventfd; |
| 497 | ev->level = level; |
| 498 | |
| 499 | mutex_lock(&vmpr->events_lock); |
| 500 | list_add(&ev->node, &vmpr->events); |
| 501 | mutex_unlock(&vmpr->events_lock); |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * vmpressure_unregister_event() - Unbind eventfd from vmpressure |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 508 | * @memcg: memcg handle |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 509 | * @eventfd: eventfd context that was used to link vmpressure with the @cg |
| 510 | * |
| 511 | * This function does internal manipulations to detach the @eventfd from |
| 512 | * the vmpressure notifications, and then frees internal resources |
| 513 | * associated with the @eventfd (but the @eventfd itself is not freed). |
| 514 | * |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 515 | * To be used as memcg event method. |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 516 | */ |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 517 | void vmpressure_unregister_event(struct mem_cgroup *memcg, |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 518 | struct eventfd_ctx *eventfd) |
| 519 | { |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 520 | struct vmpressure *vmpr = memcg_to_vmpressure(memcg); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 521 | struct vmpressure_event *ev; |
| 522 | |
| 523 | mutex_lock(&vmpr->events_lock); |
| 524 | list_for_each_entry(ev, &vmpr->events, node) { |
| 525 | if (ev->efd != eventfd) |
| 526 | continue; |
| 527 | list_del(&ev->node); |
| 528 | kfree(ev); |
| 529 | break; |
| 530 | } |
| 531 | mutex_unlock(&vmpr->events_lock); |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * vmpressure_init() - Initialize vmpressure control structure |
| 536 | * @vmpr: Structure to be initialized |
| 537 | * |
| 538 | * This function should be called on every allocated vmpressure structure |
| 539 | * before any usage. |
| 540 | */ |
| 541 | void vmpressure_init(struct vmpressure *vmpr) |
| 542 | { |
Michal Hocko | 22f2020 | 2013-07-31 13:53:48 -0700 | [diff] [blame] | 543 | spin_lock_init(&vmpr->sr_lock); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 544 | mutex_init(&vmpr->events_lock); |
| 545 | INIT_LIST_HEAD(&vmpr->events); |
| 546 | INIT_WORK(&vmpr->work, vmpressure_work_fn); |
| 547 | } |
Michal Hocko | 33cb876 | 2013-07-31 13:53:51 -0700 | [diff] [blame] | 548 | |
| 549 | /** |
| 550 | * vmpressure_cleanup() - shuts down vmpressure control structure |
| 551 | * @vmpr: Structure to be cleaned up |
| 552 | * |
| 553 | * This function should be called before the structure in which it is |
| 554 | * embedded is cleaned up. |
| 555 | */ |
| 556 | void vmpressure_cleanup(struct vmpressure *vmpr) |
| 557 | { |
| 558 | /* |
| 559 | * Make sure there is no pending work before eventfd infrastructure |
| 560 | * goes away. |
| 561 | */ |
| 562 | flush_work(&vmpr->work); |
| 563 | } |
Vinayak Menon | 13088ad | 2015-03-04 16:38:28 +0530 | [diff] [blame] | 564 | |
| 565 | static int vmpressure_global_init(void) |
| 566 | { |
| 567 | vmpressure_init(&global_vmpressure); |
| 568 | return 0; |
| 569 | } |
| 570 | late_initcall(vmpressure_global_init); |