vmpressure.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. #include <linux/cgroup.h>
  15. #include <linux/fs.h>
  16. #include <linux/log2.h>
  17. #include <linux/sched.h>
  18. #include <linux/mm.h>
  19. #include <linux/vmstat.h>
  20. #include <linux/eventfd.h>
  21. #include <linux/swap.h>
  22. #include <linux/printk.h>
  23. #include <linux/slab.h>
  24. #include <linux/notifier.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/vmpressure.h>
  28. /*
  29. * The window size (vmpressure_win) is the number of scanned pages before
  30. * we try to analyze scanned/reclaimed ratio. So the window is used as a
  31. * rate-limit tunable for the "low" level notification, and also for
  32. * averaging the ratio for medium/critical levels. Using small window
  33. * sizes can cause lot of false positives, but too big window size will
  34. * delay the notifications.
  35. *
  36. * As the vmscan reclaimer logic works with chunks which are multiple of
  37. * SWAP_CLUSTER_MAX, it makes sense to use it for the window size as well.
  38. *
  39. * TODO: Make the window size depend on machine size, as we do for vmstat
  40. * thresholds. Currently we set it to 512 pages (2MB for 4KB pages).
  41. */
  42. static const unsigned long vmpressure_win = SWAP_CLUSTER_MAX * 16;
  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. static unsigned long vmpressure_scale_max = 100;
  52. module_param_named(vmpressure_scale_max, vmpressure_scale_max,
  53. ulong, S_IRUGO | S_IWUSR);
  54. static struct vmpressure global_vmpressure;
  55. BLOCKING_NOTIFIER_HEAD(vmpressure_notifier);
  56. int vmpressure_notifier_register(struct notifier_block *nb)
  57. {
  58. return blocking_notifier_chain_register(&vmpressure_notifier, nb);
  59. }
  60. int vmpressure_notifier_unregister(struct notifier_block *nb)
  61. {
  62. return blocking_notifier_chain_unregister(&vmpressure_notifier, nb);
  63. }
  64. void vmpressure_notify(unsigned long pressure)
  65. {
  66. blocking_notifier_call_chain(&vmpressure_notifier, pressure, NULL);
  67. }
  68. /*
  69. * When there are too little pages left to scan, vmpressure() may miss the
  70. * critical pressure as number of pages will be less than "window size".
  71. * However, in that case the vmscan priority will raise fast as the
  72. * reclaimer will try to scan LRUs more deeply.
  73. *
  74. * The vmscan logic considers these special priorities:
  75. *
  76. * prio == DEF_PRIORITY (12): reclaimer starts with that value
  77. * prio <= DEF_PRIORITY - 2 : kswapd becomes somewhat overwhelmed
  78. * prio == 0 : close to OOM, kernel scans every page in an lru
  79. *
  80. * Any value in this range is acceptable for this tunable (i.e. from 12 to
  81. * 0). Current value for the vmpressure_level_critical_prio is chosen
  82. * empirically, but the number, in essence, means that we consider
  83. * critical level when scanning depth is ~10% of the lru size (vmscan
  84. * scans 'lru_size >> prio' pages, so it is actually 12.5%, or one
  85. * eights).
  86. */
  87. static const unsigned int vmpressure_level_critical_prio = ilog2(100 / 10);
  88. static struct vmpressure *work_to_vmpressure(struct work_struct *work)
  89. {
  90. return container_of(work, struct vmpressure, work);
  91. }
  92. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  93. static struct vmpressure *cg_to_vmpressure(struct cgroup *cg)
  94. {
  95. return css_to_vmpressure(cgroup_subsys_state(cg, mem_cgroup_subsys_id));
  96. }
  97. static struct vmpressure *vmpressure_parent(struct vmpressure *vmpr)
  98. {
  99. struct cgroup *cg = vmpressure_to_css(vmpr)->cgroup;
  100. struct mem_cgroup *memcg = mem_cgroup_from_cont(cg);
  101. memcg = parent_mem_cgroup(memcg);
  102. if (!memcg)
  103. return NULL;
  104. return memcg_to_vmpressure(memcg);
  105. }
  106. #else
  107. static struct vmpressure *cg_to_vmpressure(struct cgroup *cg)
  108. {
  109. return NULL;
  110. }
  111. static struct vmpressure *vmpressure_parent(struct vmpressure *vmpr)
  112. {
  113. return NULL;
  114. }
  115. #endif
  116. enum vmpressure_levels {
  117. VMPRESSURE_LOW = 0,
  118. VMPRESSURE_MEDIUM,
  119. VMPRESSURE_CRITICAL,
  120. VMPRESSURE_NUM_LEVELS,
  121. };
  122. static const char * const vmpressure_str_levels[] = {
  123. [VMPRESSURE_LOW] = "low",
  124. [VMPRESSURE_MEDIUM] = "medium",
  125. [VMPRESSURE_CRITICAL] = "critical",
  126. };
  127. static enum vmpressure_levels vmpressure_level(unsigned long pressure)
  128. {
  129. if (pressure >= vmpressure_level_critical)
  130. return VMPRESSURE_CRITICAL;
  131. else if (pressure >= vmpressure_level_med)
  132. return VMPRESSURE_MEDIUM;
  133. return VMPRESSURE_LOW;
  134. }
  135. static unsigned long vmpressure_calc_pressure(unsigned long scanned,
  136. unsigned long reclaimed)
  137. {
  138. unsigned long scale = scanned + reclaimed;
  139. unsigned long pressure;
  140. /*
  141. * We calculate the ratio (in percents) of how many pages were
  142. * scanned vs. reclaimed in a given time frame (window). Note that
  143. * time is in VM reclaimer's "ticks", i.e. number of pages
  144. * scanned. This makes it possible to set desired reaction time
  145. * and serves as a ratelimit.
  146. */
  147. pressure = scale - (reclaimed * scale / scanned);
  148. pressure = pressure * 100 / scale;
  149. pr_debug("%s: %3lu (s: %lu r: %lu)\n", __func__, pressure,
  150. scanned, reclaimed);
  151. return pressure;
  152. }
  153. static unsigned long vmpressure_account_stall(unsigned long pressure,
  154. unsigned long stall, unsigned long scanned)
  155. {
  156. unsigned long scale;
  157. if (pressure < 70)
  158. return pressure;
  159. scale = ((vmpressure_scale_max - pressure) * stall) / scanned;
  160. return pressure + scale;
  161. }
  162. struct vmpressure_event {
  163. struct eventfd_ctx *efd;
  164. enum vmpressure_levels level;
  165. struct list_head node;
  166. };
  167. static bool vmpressure_event(struct vmpressure *vmpr,
  168. unsigned long scanned, unsigned long reclaimed)
  169. {
  170. struct vmpressure_event *ev;
  171. enum vmpressure_levels level;
  172. unsigned long pressure;
  173. bool signalled = false;
  174. pressure = vmpressure_calc_pressure(scanned, reclaimed);
  175. level = vmpressure_level(pressure);
  176. mutex_lock(&vmpr->events_lock);
  177. list_for_each_entry(ev, &vmpr->events, node) {
  178. if (level >= ev->level) {
  179. eventfd_signal(ev->efd, 1);
  180. signalled = true;
  181. }
  182. }
  183. mutex_unlock(&vmpr->events_lock);
  184. return signalled;
  185. }
  186. static void vmpressure_work_fn(struct work_struct *work)
  187. {
  188. struct vmpressure *vmpr = work_to_vmpressure(work);
  189. unsigned long scanned;
  190. unsigned long reclaimed;
  191. spin_lock(&vmpr->sr_lock);
  192. /*
  193. * Several contexts might be calling vmpressure(), so it is
  194. * possible that the work was rescheduled again before the old
  195. * work context cleared the counters. In that case we will run
  196. * just after the old work returns, but then scanned might be zero
  197. * here. No need for any locks here since we don't care if
  198. * vmpr->reclaimed is in sync.
  199. */
  200. scanned = vmpr->scanned;
  201. if (!scanned) {
  202. spin_unlock(&vmpr->sr_lock);
  203. return;
  204. }
  205. reclaimed = vmpr->reclaimed;
  206. vmpr->scanned = 0;
  207. vmpr->reclaimed = 0;
  208. spin_unlock(&vmpr->sr_lock);
  209. do {
  210. if (vmpressure_event(vmpr, scanned, reclaimed))
  211. break;
  212. /*
  213. * If not handled, propagate the event upward into the
  214. * hierarchy.
  215. */
  216. } while ((vmpr = vmpressure_parent(vmpr)));
  217. }
  218. void vmpressure_memcg(gfp_t gfp, struct mem_cgroup *memcg,
  219. unsigned long scanned, unsigned long reclaimed)
  220. {
  221. struct vmpressure *vmpr = memcg_to_vmpressure(memcg);
  222. BUG_ON(!vmpr);
  223. /*
  224. * Here we only want to account pressure that userland is able to
  225. * help us with. For example, suppose that DMA zone is under
  226. * pressure; if we notify userland about that kind of pressure,
  227. * then it will be mostly a waste as it will trigger unnecessary
  228. * freeing of memory by userland (since userland is more likely to
  229. * have HIGHMEM/MOVABLE pages instead of the DMA fallback). That
  230. * is why we include only movable, highmem and FS/IO pages.
  231. * Indirect reclaim (kswapd) sets sc->gfp_mask to GFP_KERNEL, so
  232. * we account it too.
  233. */
  234. if (!(gfp & (__GFP_HIGHMEM | __GFP_MOVABLE | __GFP_IO | __GFP_FS)))
  235. return;
  236. /*
  237. * If we got here with no pages scanned, then that is an indicator
  238. * that reclaimer was unable to find any shrinkable LRUs at the
  239. * current scanning depth. But it does not mean that we should
  240. * report the critical pressure, yet. If the scanning priority
  241. * (scanning depth) goes too high (deep), we will be notified
  242. * through vmpressure_prio(). But so far, keep calm.
  243. */
  244. if (!scanned)
  245. return;
  246. spin_lock(&vmpr->sr_lock);
  247. vmpr->scanned += scanned;
  248. vmpr->reclaimed += reclaimed;
  249. scanned = vmpr->scanned;
  250. spin_unlock(&vmpr->sr_lock);
  251. if (scanned < vmpressure_win)
  252. return;
  253. schedule_work(&vmpr->work);
  254. }
  255. void vmpressure_global(gfp_t gfp, unsigned long scanned,
  256. unsigned long reclaimed)
  257. {
  258. struct vmpressure *vmpr = &global_vmpressure;
  259. unsigned long pressure;
  260. unsigned long stall;
  261. if (!(gfp & (__GFP_HIGHMEM | __GFP_MOVABLE | __GFP_IO | __GFP_FS)))
  262. return;
  263. if (!scanned)
  264. return;
  265. spin_lock(&vmpr->sr_lock);
  266. vmpr->scanned += scanned;
  267. vmpr->reclaimed += reclaimed;
  268. if (!current_is_kswapd())
  269. vmpr->stall += scanned;
  270. stall = vmpr->stall;
  271. scanned = vmpr->scanned;
  272. reclaimed = vmpr->reclaimed;
  273. spin_unlock(&vmpr->sr_lock);
  274. if (scanned < vmpressure_win)
  275. return;
  276. spin_lock(&vmpr->sr_lock);
  277. vmpr->scanned = 0;
  278. vmpr->reclaimed = 0;
  279. vmpr->stall = 0;
  280. spin_unlock(&vmpr->sr_lock);
  281. pressure = vmpressure_calc_pressure(scanned, reclaimed);
  282. pressure = vmpressure_account_stall(pressure, stall, scanned);
  283. vmpressure_notify(pressure);
  284. }
  285. /**
  286. * vmpressure() - Account memory pressure through scanned/reclaimed ratio
  287. * @gfp: reclaimer's gfp mask
  288. * @memcg: cgroup memory controller handle
  289. * @scanned: number of pages scanned
  290. * @reclaimed: number of pages reclaimed
  291. *
  292. * This function should be called from the vmscan reclaim path to account
  293. * "instantaneous" memory pressure (scanned/reclaimed ratio). The raw
  294. * pressure index is then further refined and averaged over time.
  295. *
  296. * This function does not return any value.
  297. */
  298. void vmpressure(gfp_t gfp, struct mem_cgroup *memcg,
  299. unsigned long scanned, unsigned long reclaimed)
  300. {
  301. if (!memcg)
  302. vmpressure_global(gfp, scanned, reclaimed);
  303. if (IS_ENABLED(CONFIG_CGROUP_MEM_RES_CTLR))
  304. vmpressure_memcg(gfp, memcg, scanned, reclaimed);
  305. }
  306. /**
  307. * vmpressure_prio() - Account memory pressure through reclaimer priority level
  308. * @gfp: reclaimer's gfp mask
  309. * @memcg: cgroup memory controller handle
  310. * @prio: reclaimer's priority
  311. *
  312. * This function should be called from the reclaim path every time when
  313. * the vmscan's reclaiming priority (scanning depth) changes.
  314. *
  315. * This function does not return any value.
  316. */
  317. void vmpressure_prio(gfp_t gfp, struct mem_cgroup *memcg, int prio)
  318. {
  319. /*
  320. * We only use prio for accounting critical level. For more info
  321. * see comment for vmpressure_level_critical_prio variable above.
  322. */
  323. if (prio > vmpressure_level_critical_prio)
  324. return;
  325. /*
  326. * OK, the prio is below the threshold, updating vmpressure
  327. * information before shrinker dives into long shrinking of long
  328. * range vmscan. Passing scanned = vmpressure_win, reclaimed = 0
  329. * to the vmpressure() basically means that we signal 'critical'
  330. * level.
  331. */
  332. vmpressure(gfp, memcg, vmpressure_win, 0);
  333. }
  334. /**
  335. * vmpressure_register_event() - Bind vmpressure notifications to an eventfd
  336. * @cg: cgroup that is interested in vmpressure notifications
  337. * @cft: cgroup control files handle
  338. * @eventfd: eventfd context to link notifications with
  339. * @args: event arguments (used to set up a pressure level threshold)
  340. *
  341. * This function associates eventfd context with the vmpressure
  342. * infrastructure, so that the notifications will be delivered to the
  343. * @eventfd. The @args parameter is a string that denotes pressure level
  344. * threshold (one of vmpressure_str_levels, i.e. "low", "medium", or
  345. * "critical").
  346. *
  347. * This function should not be used directly, just pass it to (struct
  348. * cftype).register_event, and then cgroup core will handle everything by
  349. * itself.
  350. */
  351. int vmpressure_register_event(struct cgroup *cg, struct cftype *cft,
  352. struct eventfd_ctx *eventfd, const char *args)
  353. {
  354. struct vmpressure *vmpr = cg_to_vmpressure(cg);
  355. struct vmpressure_event *ev;
  356. int level;
  357. BUG_ON(!vmpr);
  358. for (level = 0; level < VMPRESSURE_NUM_LEVELS; level++) {
  359. if (!strcmp(vmpressure_str_levels[level], args))
  360. break;
  361. }
  362. if (level >= VMPRESSURE_NUM_LEVELS)
  363. return -EINVAL;
  364. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  365. if (!ev)
  366. return -ENOMEM;
  367. ev->efd = eventfd;
  368. ev->level = level;
  369. mutex_lock(&vmpr->events_lock);
  370. list_add(&ev->node, &vmpr->events);
  371. mutex_unlock(&vmpr->events_lock);
  372. return 0;
  373. }
  374. /**
  375. * vmpressure_unregister_event() - Unbind eventfd from vmpressure
  376. * @cg: cgroup handle
  377. * @cft: cgroup control files handle
  378. * @eventfd: eventfd context that was used to link vmpressure with the @cg
  379. *
  380. * This function does internal manipulations to detach the @eventfd from
  381. * the vmpressure notifications, and then frees internal resources
  382. * associated with the @eventfd (but the @eventfd itself is not freed).
  383. *
  384. * This function should not be used directly, just pass it to (struct
  385. * cftype).unregister_event, and then cgroup core will handle everything
  386. * by itself.
  387. */
  388. void vmpressure_unregister_event(struct cgroup *cg, struct cftype *cft,
  389. struct eventfd_ctx *eventfd)
  390. {
  391. struct vmpressure *vmpr = cg_to_vmpressure(cg);
  392. struct vmpressure_event *ev;
  393. if (!vmpr)
  394. BUG();
  395. mutex_lock(&vmpr->events_lock);
  396. list_for_each_entry(ev, &vmpr->events, node) {
  397. if (ev->efd != eventfd)
  398. continue;
  399. list_del(&ev->node);
  400. kfree(ev);
  401. break;
  402. }
  403. mutex_unlock(&vmpr->events_lock);
  404. }
  405. /**
  406. * vmpressure_init() - Initialize vmpressure control structure
  407. * @vmpr: Structure to be initialized
  408. *
  409. * This function should be called on every allocated vmpressure structure
  410. * before any usage.
  411. */
  412. void vmpressure_init(struct vmpressure *vmpr)
  413. {
  414. spin_lock_init(&vmpr->sr_lock);
  415. mutex_init(&vmpr->events_lock);
  416. INIT_LIST_HEAD(&vmpr->events);
  417. INIT_WORK(&vmpr->work, vmpressure_work_fn);
  418. }
  419. int vmpressure_global_init(void)
  420. {
  421. vmpressure_init(&global_vmpressure);
  422. return 0;
  423. }
  424. late_initcall(vmpressure_global_init);