vmpressure.c 13 KB

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