vmpressure.c 12 KB

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