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> |
| 25 | #include <linux/vmpressure.h> |
| 26 | |
| 27 | /* |
| 28 | * The window size (vmpressure_win) is the number of scanned pages before |
| 29 | * we try to analyze scanned/reclaimed ratio. So the window is used as a |
| 30 | * rate-limit tunable for the "low" level notification, and also for |
| 31 | * averaging the ratio for medium/critical levels. Using small window |
| 32 | * sizes can cause lot of false positives, but too big window size will |
| 33 | * delay the notifications. |
| 34 | * |
| 35 | * As the vmscan reclaimer logic works with chunks which are multiple of |
| 36 | * SWAP_CLUSTER_MAX, it makes sense to use it for the window size as well. |
| 37 | * |
| 38 | * TODO: Make the window size depend on machine size, as we do for vmstat |
| 39 | * thresholds. Currently we set it to 512 pages (2MB for 4KB pages). |
| 40 | */ |
| 41 | static const unsigned long vmpressure_win = SWAP_CLUSTER_MAX * 16; |
| 42 | |
| 43 | /* |
| 44 | * These thresholds are used when we account memory pressure through |
| 45 | * scanned/reclaimed ratio. The current values were chosen empirically. In |
| 46 | * essence, they are percents: the higher the value, the more number |
| 47 | * unsuccessful reclaims there were. |
| 48 | */ |
| 49 | static const unsigned int vmpressure_level_med = 60; |
| 50 | static const unsigned int vmpressure_level_critical = 95; |
| 51 | |
| 52 | /* |
| 53 | * When there are too little pages left to scan, vmpressure() may miss the |
| 54 | * critical pressure as number of pages will be less than "window size". |
| 55 | * However, in that case the vmscan priority will raise fast as the |
| 56 | * reclaimer will try to scan LRUs more deeply. |
| 57 | * |
| 58 | * The vmscan logic considers these special priorities: |
| 59 | * |
| 60 | * prio == DEF_PRIORITY (12): reclaimer starts with that value |
| 61 | * prio <= DEF_PRIORITY - 2 : kswapd becomes somewhat overwhelmed |
| 62 | * prio == 0 : close to OOM, kernel scans every page in an lru |
| 63 | * |
| 64 | * Any value in this range is acceptable for this tunable (i.e. from 12 to |
| 65 | * 0). Current value for the vmpressure_level_critical_prio is chosen |
| 66 | * empirically, but the number, in essence, means that we consider |
| 67 | * critical level when scanning depth is ~10% of the lru size (vmscan |
| 68 | * scans 'lru_size >> prio' pages, so it is actually 12.5%, or one |
| 69 | * eights). |
| 70 | */ |
| 71 | static const unsigned int vmpressure_level_critical_prio = ilog2(100 / 10); |
| 72 | |
| 73 | static struct vmpressure *work_to_vmpressure(struct work_struct *work) |
| 74 | { |
| 75 | return container_of(work, struct vmpressure, work); |
| 76 | } |
| 77 | |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 78 | static struct vmpressure *vmpressure_parent(struct vmpressure *vmpr) |
| 79 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 80 | struct cgroup_subsys_state *css = vmpressure_to_css(vmpr); |
| 81 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 82 | |
| 83 | memcg = parent_mem_cgroup(memcg); |
| 84 | if (!memcg) |
| 85 | return NULL; |
| 86 | return memcg_to_vmpressure(memcg); |
| 87 | } |
| 88 | |
| 89 | enum vmpressure_levels { |
| 90 | VMPRESSURE_LOW = 0, |
| 91 | VMPRESSURE_MEDIUM, |
| 92 | VMPRESSURE_CRITICAL, |
| 93 | VMPRESSURE_NUM_LEVELS, |
| 94 | }; |
| 95 | |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 96 | enum vmpressure_modes { |
| 97 | VMPRESSURE_NO_PASSTHROUGH = 0, |
| 98 | VMPRESSURE_HIERARCHY, |
| 99 | VMPRESSURE_LOCAL, |
| 100 | VMPRESSURE_NUM_MODES, |
| 101 | }; |
| 102 | |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 103 | static const char * const vmpressure_str_levels[] = { |
| 104 | [VMPRESSURE_LOW] = "low", |
| 105 | [VMPRESSURE_MEDIUM] = "medium", |
| 106 | [VMPRESSURE_CRITICAL] = "critical", |
| 107 | }; |
| 108 | |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 109 | static const char * const vmpressure_str_modes[] = { |
| 110 | [VMPRESSURE_NO_PASSTHROUGH] = "default", |
| 111 | [VMPRESSURE_HIERARCHY] = "hierarchy", |
| 112 | [VMPRESSURE_LOCAL] = "local", |
| 113 | }; |
| 114 | |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 115 | static enum vmpressure_levels vmpressure_level(unsigned long pressure) |
| 116 | { |
| 117 | if (pressure >= vmpressure_level_critical) |
| 118 | return VMPRESSURE_CRITICAL; |
| 119 | else if (pressure >= vmpressure_level_med) |
| 120 | return VMPRESSURE_MEDIUM; |
| 121 | return VMPRESSURE_LOW; |
| 122 | } |
| 123 | |
| 124 | static enum vmpressure_levels vmpressure_calc_level(unsigned long scanned, |
| 125 | unsigned long reclaimed) |
| 126 | { |
| 127 | unsigned long scale = scanned + reclaimed; |
Vinayak Menon | e1587a4 | 2017-02-24 14:59:39 -0800 | [diff] [blame] | 128 | unsigned long pressure = 0; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 129 | |
| 130 | /* |
zhongjiang | d7143e3 | 2017-06-16 14:02:40 -0700 | [diff] [blame] | 131 | * reclaimed can be greater than scanned for things such as reclaimed |
| 132 | * slab pages. shrink_node() just adds reclaimed pages without a |
| 133 | * related increment to scanned pages. |
Vinayak Menon | e1587a4 | 2017-02-24 14:59:39 -0800 | [diff] [blame] | 134 | */ |
| 135 | if (reclaimed >= scanned) |
| 136 | goto out; |
| 137 | /* |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 138 | * We calculate the ratio (in percents) of how many pages were |
| 139 | * scanned vs. reclaimed in a given time frame (window). Note that |
| 140 | * time is in VM reclaimer's "ticks", i.e. number of pages |
| 141 | * scanned. This makes it possible to set desired reaction time |
| 142 | * and serves as a ratelimit. |
| 143 | */ |
| 144 | pressure = scale - (reclaimed * scale / scanned); |
| 145 | pressure = pressure * 100 / scale; |
| 146 | |
Vinayak Menon | e1587a4 | 2017-02-24 14:59:39 -0800 | [diff] [blame] | 147 | out: |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 148 | pr_debug("%s: %3lu (s: %lu r: %lu)\n", __func__, pressure, |
| 149 | scanned, reclaimed); |
| 150 | |
| 151 | return vmpressure_level(pressure); |
| 152 | } |
| 153 | |
| 154 | struct vmpressure_event { |
| 155 | struct eventfd_ctx *efd; |
| 156 | enum vmpressure_levels level; |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 157 | enum vmpressure_modes mode; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 158 | struct list_head node; |
| 159 | }; |
| 160 | |
| 161 | static bool vmpressure_event(struct vmpressure *vmpr, |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 162 | const enum vmpressure_levels level, |
| 163 | bool ancestor, bool signalled) |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 164 | { |
| 165 | struct vmpressure_event *ev; |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 166 | bool ret = false; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 167 | |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 168 | mutex_lock(&vmpr->events_lock); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 169 | list_for_each_entry(ev, &vmpr->events, node) { |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 170 | if (ancestor && ev->mode == VMPRESSURE_LOCAL) |
| 171 | continue; |
| 172 | if (signalled && ev->mode == VMPRESSURE_NO_PASSTHROUGH) |
| 173 | continue; |
| 174 | if (level < ev->level) |
| 175 | continue; |
| 176 | eventfd_signal(ev->efd, 1); |
| 177 | ret = true; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 178 | } |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 179 | mutex_unlock(&vmpr->events_lock); |
| 180 | |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 181 | return ret; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static void vmpressure_work_fn(struct work_struct *work) |
| 185 | { |
| 186 | struct vmpressure *vmpr = work_to_vmpressure(work); |
| 187 | unsigned long scanned; |
| 188 | unsigned long reclaimed; |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 189 | enum vmpressure_levels level; |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 190 | bool ancestor = false; |
| 191 | bool signalled = false; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 192 | |
Andrew Morton | 91b5719 | 2014-12-02 15:59:28 -0800 | [diff] [blame] | 193 | spin_lock(&vmpr->sr_lock); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 194 | /* |
| 195 | * Several contexts might be calling vmpressure(), so it is |
| 196 | * possible that the work was rescheduled again before the old |
| 197 | * work context cleared the counters. In that case we will run |
| 198 | * just after the old work returns, but then scanned might be zero |
| 199 | * here. No need for any locks here since we don't care if |
| 200 | * vmpr->reclaimed is in sync. |
| 201 | */ |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 202 | scanned = vmpr->tree_scanned; |
Andrew Morton | 91b5719 | 2014-12-02 15:59:28 -0800 | [diff] [blame] | 203 | if (!scanned) { |
| 204 | spin_unlock(&vmpr->sr_lock); |
| 205 | return; |
| 206 | } |
| 207 | |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 208 | reclaimed = vmpr->tree_reclaimed; |
| 209 | vmpr->tree_scanned = 0; |
| 210 | vmpr->tree_reclaimed = 0; |
Michal Hocko | 22f2020 | 2013-07-31 13:53:48 -0700 | [diff] [blame] | 211 | spin_unlock(&vmpr->sr_lock); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 212 | |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 213 | level = vmpressure_calc_level(scanned, reclaimed); |
| 214 | |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 215 | do { |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 216 | if (vmpressure_event(vmpr, level, ancestor, signalled)) |
| 217 | signalled = true; |
| 218 | ancestor = true; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 219 | } while ((vmpr = vmpressure_parent(vmpr))); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * vmpressure() - Account memory pressure through scanned/reclaimed ratio |
| 224 | * @gfp: reclaimer's gfp mask |
| 225 | * @memcg: cgroup memory controller handle |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 226 | * @tree: legacy subtree mode |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 227 | * @scanned: number of pages scanned |
| 228 | * @reclaimed: number of pages reclaimed |
| 229 | * |
| 230 | * This function should be called from the vmscan reclaim path to account |
| 231 | * "instantaneous" memory pressure (scanned/reclaimed ratio). The raw |
| 232 | * pressure index is then further refined and averaged over time. |
| 233 | * |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 234 | * If @tree is set, vmpressure is in traditional userspace reporting |
| 235 | * mode: @memcg is considered the pressure root and userspace is |
| 236 | * notified of the entire subtree's reclaim efficiency. |
| 237 | * |
| 238 | * If @tree is not set, reclaim efficiency is recorded for @memcg, and |
| 239 | * only in-kernel users are notified. |
| 240 | * |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 241 | * This function does not return any value. |
| 242 | */ |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 243 | void vmpressure(gfp_t gfp, struct mem_cgroup *memcg, bool tree, |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 244 | unsigned long scanned, unsigned long reclaimed) |
| 245 | { |
| 246 | struct vmpressure *vmpr = memcg_to_vmpressure(memcg); |
| 247 | |
| 248 | /* |
| 249 | * Here we only want to account pressure that userland is able to |
| 250 | * help us with. For example, suppose that DMA zone is under |
| 251 | * pressure; if we notify userland about that kind of pressure, |
| 252 | * then it will be mostly a waste as it will trigger unnecessary |
| 253 | * freeing of memory by userland (since userland is more likely to |
| 254 | * have HIGHMEM/MOVABLE pages instead of the DMA fallback). That |
| 255 | * is why we include only movable, highmem and FS/IO pages. |
| 256 | * Indirect reclaim (kswapd) sets sc->gfp_mask to GFP_KERNEL, so |
| 257 | * we account it too. |
| 258 | */ |
| 259 | if (!(gfp & (__GFP_HIGHMEM | __GFP_MOVABLE | __GFP_IO | __GFP_FS))) |
| 260 | return; |
| 261 | |
| 262 | /* |
| 263 | * If we got here with no pages scanned, then that is an indicator |
| 264 | * that reclaimer was unable to find any shrinkable LRUs at the |
| 265 | * current scanning depth. But it does not mean that we should |
| 266 | * report the critical pressure, yet. If the scanning priority |
| 267 | * (scanning depth) goes too high (deep), we will be notified |
| 268 | * through vmpressure_prio(). But so far, keep calm. |
| 269 | */ |
| 270 | if (!scanned) |
| 271 | return; |
| 272 | |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 273 | if (tree) { |
| 274 | spin_lock(&vmpr->sr_lock); |
Vladimir Davydov | 3c1da7b | 2016-02-02 16:57:49 -0800 | [diff] [blame] | 275 | scanned = vmpr->tree_scanned += scanned; |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 276 | vmpr->tree_reclaimed += reclaimed; |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 277 | spin_unlock(&vmpr->sr_lock); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 278 | |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 279 | if (scanned < vmpressure_win) |
| 280 | return; |
| 281 | schedule_work(&vmpr->work); |
| 282 | } else { |
| 283 | enum vmpressure_levels level; |
| 284 | |
| 285 | /* For now, no users for root-level efficiency */ |
Hugh Dickins | 686739f | 2016-01-14 15:21:37 -0800 | [diff] [blame] | 286 | if (!memcg || memcg == root_mem_cgroup) |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 287 | return; |
| 288 | |
| 289 | spin_lock(&vmpr->sr_lock); |
| 290 | scanned = vmpr->scanned += scanned; |
| 291 | reclaimed = vmpr->reclaimed += reclaimed; |
| 292 | if (scanned < vmpressure_win) { |
| 293 | spin_unlock(&vmpr->sr_lock); |
| 294 | return; |
| 295 | } |
| 296 | vmpr->scanned = vmpr->reclaimed = 0; |
| 297 | spin_unlock(&vmpr->sr_lock); |
| 298 | |
| 299 | level = vmpressure_calc_level(scanned, reclaimed); |
| 300 | |
| 301 | if (level > VMPRESSURE_LOW) { |
| 302 | /* |
| 303 | * Let the socket buffer allocator know that |
| 304 | * we are having trouble reclaiming LRU pages. |
| 305 | * |
| 306 | * For hysteresis keep the pressure state |
| 307 | * asserted for a second in which subsequent |
| 308 | * pressure events can occur. |
| 309 | */ |
| 310 | memcg->socket_pressure = jiffies + HZ; |
| 311 | } |
| 312 | } |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** |
| 316 | * vmpressure_prio() - Account memory pressure through reclaimer priority level |
| 317 | * @gfp: reclaimer's gfp mask |
| 318 | * @memcg: cgroup memory controller handle |
| 319 | * @prio: reclaimer's priority |
| 320 | * |
| 321 | * This function should be called from the reclaim path every time when |
| 322 | * the vmscan's reclaiming priority (scanning depth) changes. |
| 323 | * |
| 324 | * This function does not return any value. |
| 325 | */ |
| 326 | void vmpressure_prio(gfp_t gfp, struct mem_cgroup *memcg, int prio) |
| 327 | { |
| 328 | /* |
| 329 | * We only use prio for accounting critical level. For more info |
| 330 | * see comment for vmpressure_level_critical_prio variable above. |
| 331 | */ |
| 332 | if (prio > vmpressure_level_critical_prio) |
| 333 | return; |
| 334 | |
| 335 | /* |
| 336 | * OK, the prio is below the threshold, updating vmpressure |
| 337 | * information before shrinker dives into long shrinking of long |
| 338 | * range vmscan. Passing scanned = vmpressure_win, reclaimed = 0 |
| 339 | * to the vmpressure() basically means that we signal 'critical' |
| 340 | * level. |
| 341 | */ |
Johannes Weiner | 8e8ae64 | 2016-01-14 15:21:32 -0800 | [diff] [blame] | 342 | vmpressure(gfp, memcg, true, vmpressure_win, 0); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 343 | } |
| 344 | |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 345 | #define MAX_VMPRESSURE_ARGS_LEN (strlen("critical") + strlen("hierarchy") + 2) |
| 346 | |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 347 | /** |
| 348 | * vmpressure_register_event() - Bind vmpressure notifications to an eventfd |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 349 | * @memcg: memcg that is interested in vmpressure notifications |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 350 | * @eventfd: eventfd context to link notifications with |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 351 | * @args: event arguments (pressure level threshold, optional mode) |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 352 | * |
| 353 | * This function associates eventfd context with the vmpressure |
| 354 | * infrastructure, so that the notifications will be delivered to the |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 355 | * @eventfd. The @args parameter is a comma-delimited string that denotes a |
| 356 | * pressure level threshold (one of vmpressure_str_levels, i.e. "low", "medium", |
| 357 | * or "critical") and an optional mode (one of vmpressure_str_modes, i.e. |
| 358 | * "hierarchy" or "local"). |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 359 | * |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 360 | * To be used as memcg event method. |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 361 | */ |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 362 | int vmpressure_register_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 363 | struct eventfd_ctx *eventfd, const char *args) |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 364 | { |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 365 | struct vmpressure *vmpr = memcg_to_vmpressure(memcg); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 366 | struct vmpressure_event *ev; |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 367 | enum vmpressure_modes mode = VMPRESSURE_NO_PASSTHROUGH; |
| 368 | enum vmpressure_levels level = -1; |
| 369 | char *spec, *spec_orig; |
| 370 | char *token; |
| 371 | int ret = 0; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 372 | |
Andy Shevchenko | d62ff36 | 2018-06-07 17:07:50 -0700 | [diff] [blame] | 373 | spec_orig = spec = kstrndup(args, MAX_VMPRESSURE_ARGS_LEN, GFP_KERNEL); |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 374 | if (!spec) { |
| 375 | ret = -ENOMEM; |
| 376 | goto out; |
| 377 | } |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 378 | |
| 379 | /* Find required level */ |
| 380 | token = strsep(&spec, ","); |
Andy Shevchenko | 3cadfa2 | 2018-06-07 17:07:53 -0700 | [diff] [blame] | 381 | level = match_string(vmpressure_str_levels, VMPRESSURE_NUM_LEVELS, token); |
| 382 | if (level < 0) { |
| 383 | ret = level; |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 384 | goto out; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 385 | } |
| 386 | |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 387 | /* Find optional mode */ |
| 388 | token = strsep(&spec, ","); |
| 389 | if (token) { |
Andy Shevchenko | 3cadfa2 | 2018-06-07 17:07:53 -0700 | [diff] [blame] | 390 | mode = match_string(vmpressure_str_modes, VMPRESSURE_NUM_MODES, token); |
| 391 | if (mode < 0) { |
| 392 | ret = mode; |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 393 | goto out; |
| 394 | } |
| 395 | } |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 396 | |
| 397 | ev = kzalloc(sizeof(*ev), GFP_KERNEL); |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 398 | if (!ev) { |
| 399 | ret = -ENOMEM; |
| 400 | goto out; |
| 401 | } |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 402 | |
| 403 | ev->efd = eventfd; |
| 404 | ev->level = level; |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 405 | ev->mode = mode; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 406 | |
| 407 | mutex_lock(&vmpr->events_lock); |
| 408 | list_add(&ev->node, &vmpr->events); |
| 409 | mutex_unlock(&vmpr->events_lock); |
David Rientjes | b6bb981 | 2017-07-10 15:47:59 -0700 | [diff] [blame] | 410 | out: |
| 411 | kfree(spec_orig); |
| 412 | return ret; |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /** |
| 416 | * vmpressure_unregister_event() - Unbind eventfd from vmpressure |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 417 | * @memcg: memcg handle |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 418 | * @eventfd: eventfd context that was used to link vmpressure with the @cg |
| 419 | * |
| 420 | * This function does internal manipulations to detach the @eventfd from |
| 421 | * the vmpressure notifications, and then frees internal resources |
| 422 | * associated with the @eventfd (but the @eventfd itself is not freed). |
| 423 | * |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 424 | * To be used as memcg event method. |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 425 | */ |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 426 | void vmpressure_unregister_event(struct mem_cgroup *memcg, |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 427 | struct eventfd_ctx *eventfd) |
| 428 | { |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 429 | struct vmpressure *vmpr = memcg_to_vmpressure(memcg); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 430 | struct vmpressure_event *ev; |
| 431 | |
| 432 | mutex_lock(&vmpr->events_lock); |
| 433 | list_for_each_entry(ev, &vmpr->events, node) { |
| 434 | if (ev->efd != eventfd) |
| 435 | continue; |
| 436 | list_del(&ev->node); |
| 437 | kfree(ev); |
| 438 | break; |
| 439 | } |
| 440 | mutex_unlock(&vmpr->events_lock); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * vmpressure_init() - Initialize vmpressure control structure |
| 445 | * @vmpr: Structure to be initialized |
| 446 | * |
| 447 | * This function should be called on every allocated vmpressure structure |
| 448 | * before any usage. |
| 449 | */ |
| 450 | void vmpressure_init(struct vmpressure *vmpr) |
| 451 | { |
Michal Hocko | 22f2020 | 2013-07-31 13:53:48 -0700 | [diff] [blame] | 452 | spin_lock_init(&vmpr->sr_lock); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 453 | mutex_init(&vmpr->events_lock); |
| 454 | INIT_LIST_HEAD(&vmpr->events); |
| 455 | INIT_WORK(&vmpr->work, vmpressure_work_fn); |
| 456 | } |
Michal Hocko | 33cb876 | 2013-07-31 13:53:51 -0700 | [diff] [blame] | 457 | |
| 458 | /** |
| 459 | * vmpressure_cleanup() - shuts down vmpressure control structure |
| 460 | * @vmpr: Structure to be cleaned up |
| 461 | * |
| 462 | * This function should be called before the structure in which it is |
| 463 | * embedded is cleaned up. |
| 464 | */ |
| 465 | void vmpressure_cleanup(struct vmpressure *vmpr) |
| 466 | { |
| 467 | /* |
| 468 | * Make sure there is no pending work before eventfd infrastructure |
| 469 | * goes away. |
| 470 | */ |
| 471 | flush_work(&vmpr->work); |
| 472 | } |