workingset.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Workingset detection
  4. *
  5. * Copyright (C) 2013 Red Hat, Inc., Johannes Weiner
  6. */
  7. #include <linux/memcontrol.h>
  8. #include <linux/mm_inline.h>
  9. #include <linux/writeback.h>
  10. #include <linux/shmem_fs.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/atomic.h>
  13. #include <linux/module.h>
  14. #include <linux/swap.h>
  15. #include <linux/dax.h>
  16. #include <linux/fs.h>
  17. #include <linux/mm.h>
  18. /*
  19. * Double CLOCK lists
  20. *
  21. * Per node, two clock lists are maintained for file pages: the
  22. * inactive and the active list. Freshly faulted pages start out at
  23. * the head of the inactive list and page reclaim scans pages from the
  24. * tail. Pages that are accessed multiple times on the inactive list
  25. * are promoted to the active list, to protect them from reclaim,
  26. * whereas active pages are demoted to the inactive list when the
  27. * active list grows too big.
  28. *
  29. * fault ------------------------+
  30. * |
  31. * +--------------+ | +-------------+
  32. * reclaim <- | inactive | <-+-- demotion | active | <--+
  33. * +--------------+ +-------------+ |
  34. * | |
  35. * +-------------- promotion ------------------+
  36. *
  37. *
  38. * Access frequency and refault distance
  39. *
  40. * A workload is thrashing when its pages are frequently used but they
  41. * are evicted from the inactive list every time before another access
  42. * would have promoted them to the active list.
  43. *
  44. * In cases where the average access distance between thrashing pages
  45. * is bigger than the size of memory there is nothing that can be
  46. * done - the thrashing set could never fit into memory under any
  47. * circumstance.
  48. *
  49. * However, the average access distance could be bigger than the
  50. * inactive list, yet smaller than the size of memory. In this case,
  51. * the set could fit into memory if it weren't for the currently
  52. * active pages - which may be used more, hopefully less frequently:
  53. *
  54. * +-memory available to cache-+
  55. * | |
  56. * +-inactive------+-active----+
  57. * a b | c d e f g h i | J K L M N |
  58. * +---------------+-----------+
  59. *
  60. * It is prohibitively expensive to accurately track access frequency
  61. * of pages. But a reasonable approximation can be made to measure
  62. * thrashing on the inactive list, after which refaulting pages can be
  63. * activated optimistically to compete with the existing active pages.
  64. *
  65. * Approximating inactive page access frequency - Observations:
  66. *
  67. * 1. When a page is accessed for the first time, it is added to the
  68. * head of the inactive list, slides every existing inactive page
  69. * towards the tail by one slot, and pushes the current tail page
  70. * out of memory.
  71. *
  72. * 2. When a page is accessed for the second time, it is promoted to
  73. * the active list, shrinking the inactive list by one slot. This
  74. * also slides all inactive pages that were faulted into the cache
  75. * more recently than the activated page towards the tail of the
  76. * inactive list.
  77. *
  78. * Thus:
  79. *
  80. * 1. The sum of evictions and activations between any two points in
  81. * time indicate the minimum number of inactive pages accessed in
  82. * between.
  83. *
  84. * 2. Moving one inactive page N page slots towards the tail of the
  85. * list requires at least N inactive page accesses.
  86. *
  87. * Combining these:
  88. *
  89. * 1. When a page is finally evicted from memory, the number of
  90. * inactive pages accessed while the page was in cache is at least
  91. * the number of page slots on the inactive list.
  92. *
  93. * 2. In addition, measuring the sum of evictions and activations (E)
  94. * at the time of a page's eviction, and comparing it to another
  95. * reading (R) at the time the page faults back into memory tells
  96. * the minimum number of accesses while the page was not cached.
  97. * This is called the refault distance.
  98. *
  99. * Because the first access of the page was the fault and the second
  100. * access the refault, we combine the in-cache distance with the
  101. * out-of-cache distance to get the complete minimum access distance
  102. * of this page:
  103. *
  104. * NR_inactive + (R - E)
  105. *
  106. * And knowing the minimum access distance of a page, we can easily
  107. * tell if the page would be able to stay in cache assuming all page
  108. * slots in the cache were available:
  109. *
  110. * NR_inactive + (R - E) <= NR_inactive + NR_active
  111. *
  112. * which can be further simplified to
  113. *
  114. * (R - E) <= NR_active
  115. *
  116. * Put into words, the refault distance (out-of-cache) can be seen as
  117. * a deficit in inactive list space (in-cache). If the inactive list
  118. * had (R - E) more page slots, the page would not have been evicted
  119. * in between accesses, but activated instead. And on a full system,
  120. * the only thing eating into inactive list space is active pages.
  121. *
  122. *
  123. * Refaulting inactive pages
  124. *
  125. * All that is known about the active list is that the pages have been
  126. * accessed more than once in the past. This means that at any given
  127. * time there is actually a good chance that pages on the active list
  128. * are no longer in active use.
  129. *
  130. * So when a refault distance of (R - E) is observed and there are at
  131. * least (R - E) active pages, the refaulting page is activated
  132. * optimistically in the hope that (R - E) active pages are actually
  133. * used less frequently than the refaulting page - or even not used at
  134. * all anymore.
  135. *
  136. * That means if inactive cache is refaulting with a suitable refault
  137. * distance, we assume the cache workingset is transitioning and put
  138. * pressure on the current active list.
  139. *
  140. * If this is wrong and demotion kicks in, the pages which are truly
  141. * used more frequently will be reactivated while the less frequently
  142. * used once will be evicted from memory.
  143. *
  144. * But if this is right, the stale pages will be pushed out of memory
  145. * and the used pages get to stay in cache.
  146. *
  147. * Refaulting active pages
  148. *
  149. * If on the other hand the refaulting pages have recently been
  150. * deactivated, it means that the active list is no longer protecting
  151. * actively used cache from reclaim. The cache is NOT transitioning to
  152. * a different workingset; the existing workingset is thrashing in the
  153. * space allocated to the page cache.
  154. *
  155. *
  156. * Implementation
  157. *
  158. * For each node's LRU lists, a counter for inactive evictions and
  159. * activations is maintained (node->nonresident_age).
  160. *
  161. * On eviction, a snapshot of this counter (along with some bits to
  162. * identify the node) is stored in the now empty page cache
  163. * slot of the evicted page. This is called a shadow entry.
  164. *
  165. * On cache misses for which there are shadow entries, an eligible
  166. * refault distance will immediately activate the refaulting page.
  167. */
  168. #define EVICTION_SHIFT ((BITS_PER_LONG - BITS_PER_XA_VALUE) + \
  169. 1 + NODES_SHIFT + MEM_CGROUP_ID_SHIFT)
  170. #define EVICTION_MASK (~0UL >> EVICTION_SHIFT)
  171. /*
  172. * Eviction timestamps need to be able to cover the full range of
  173. * actionable refaults. However, bits are tight in the xarray
  174. * entry, and after storing the identifier for the lruvec there might
  175. * not be enough left to represent every single actionable refault. In
  176. * that case, we have to sacrifice granularity for distance, and group
  177. * evictions into coarser buckets by shaving off lower timestamp bits.
  178. */
  179. static unsigned int bucket_order __read_mostly;
  180. static void *pack_shadow(int memcgid, pg_data_t *pgdat, unsigned long eviction,
  181. bool workingset)
  182. {
  183. eviction >>= bucket_order;
  184. eviction &= EVICTION_MASK;
  185. eviction = (eviction << MEM_CGROUP_ID_SHIFT) | memcgid;
  186. eviction = (eviction << NODES_SHIFT) | pgdat->node_id;
  187. eviction = (eviction << 1) | workingset;
  188. return xa_mk_value(eviction);
  189. }
  190. static void unpack_shadow(void *shadow, int *memcgidp, pg_data_t **pgdat,
  191. unsigned long *evictionp, bool *workingsetp)
  192. {
  193. unsigned long entry = xa_to_value(shadow);
  194. int memcgid, nid;
  195. bool workingset;
  196. workingset = entry & 1;
  197. entry >>= 1;
  198. nid = entry & ((1UL << NODES_SHIFT) - 1);
  199. entry >>= NODES_SHIFT;
  200. memcgid = entry & ((1UL << MEM_CGROUP_ID_SHIFT) - 1);
  201. entry >>= MEM_CGROUP_ID_SHIFT;
  202. *memcgidp = memcgid;
  203. *pgdat = NODE_DATA(nid);
  204. *evictionp = entry << bucket_order;
  205. *workingsetp = workingset;
  206. }
  207. /**
  208. * workingset_age_nonresident - age non-resident entries as LRU ages
  209. * @lruvec: the lruvec that was aged
  210. * @nr_pages: the number of pages to count
  211. *
  212. * As in-memory pages are aged, non-resident pages need to be aged as
  213. * well, in order for the refault distances later on to be comparable
  214. * to the in-memory dimensions. This function allows reclaim and LRU
  215. * operations to drive the non-resident aging along in parallel.
  216. */
  217. void workingset_age_nonresident(struct lruvec *lruvec, unsigned long nr_pages)
  218. {
  219. /*
  220. * Reclaiming a cgroup means reclaiming all its children in a
  221. * round-robin fashion. That means that each cgroup has an LRU
  222. * order that is composed of the LRU orders of its child
  223. * cgroups; and every page has an LRU position not just in the
  224. * cgroup that owns it, but in all of that group's ancestors.
  225. *
  226. * So when the physical inactive list of a leaf cgroup ages,
  227. * the virtual inactive lists of all its parents, including
  228. * the root cgroup's, age as well.
  229. */
  230. do {
  231. atomic_long_add(nr_pages, &lruvec->nonresident_age);
  232. } while ((lruvec = parent_lruvec(lruvec)));
  233. }
  234. /**
  235. * workingset_eviction - note the eviction of a page from memory
  236. * @target_memcg: the cgroup that is causing the reclaim
  237. * @page: the page being evicted
  238. *
  239. * Returns a shadow entry to be stored in @page->mapping->i_pages in place
  240. * of the evicted @page so that a later refault can be detected.
  241. */
  242. void *workingset_eviction(struct page *page, struct mem_cgroup *target_memcg)
  243. {
  244. struct pglist_data *pgdat = page_pgdat(page);
  245. unsigned long eviction;
  246. struct lruvec *lruvec;
  247. int memcgid;
  248. /* Page is fully exclusive and pins page->mem_cgroup */
  249. VM_BUG_ON_PAGE(PageLRU(page), page);
  250. VM_BUG_ON_PAGE(page_count(page), page);
  251. VM_BUG_ON_PAGE(!PageLocked(page), page);
  252. lruvec = mem_cgroup_lruvec(target_memcg, pgdat);
  253. workingset_age_nonresident(lruvec, thp_nr_pages(page));
  254. /* XXX: target_memcg can be NULL, go through lruvec */
  255. memcgid = mem_cgroup_id(lruvec_memcg(lruvec));
  256. eviction = atomic_long_read(&lruvec->nonresident_age);
  257. return pack_shadow(memcgid, pgdat, eviction, PageWorkingset(page));
  258. }
  259. /**
  260. * workingset_refault - evaluate the refault of a previously evicted page
  261. * @page: the freshly allocated replacement page
  262. * @shadow: shadow entry of the evicted page
  263. *
  264. * Calculates and evaluates the refault distance of the previously
  265. * evicted page in the context of the node and the memcg whose memory
  266. * pressure caused the eviction.
  267. */
  268. void workingset_refault(struct page *page, void *shadow)
  269. {
  270. bool file = page_is_file_lru(page);
  271. struct mem_cgroup *eviction_memcg;
  272. struct lruvec *eviction_lruvec;
  273. unsigned long refault_distance;
  274. unsigned long workingset_size;
  275. struct pglist_data *pgdat;
  276. struct mem_cgroup *memcg;
  277. unsigned long eviction;
  278. struct lruvec *lruvec;
  279. unsigned long refault;
  280. bool workingset;
  281. int memcgid;
  282. unpack_shadow(shadow, &memcgid, &pgdat, &eviction, &workingset);
  283. rcu_read_lock();
  284. /*
  285. * Look up the memcg associated with the stored ID. It might
  286. * have been deleted since the page's eviction.
  287. *
  288. * Note that in rare events the ID could have been recycled
  289. * for a new cgroup that refaults a shared page. This is
  290. * impossible to tell from the available data. However, this
  291. * should be a rare and limited disturbance, and activations
  292. * are always speculative anyway. Ultimately, it's the aging
  293. * algorithm's job to shake out the minimum access frequency
  294. * for the active cache.
  295. *
  296. * XXX: On !CONFIG_MEMCG, this will always return NULL; it
  297. * would be better if the root_mem_cgroup existed in all
  298. * configurations instead.
  299. */
  300. eviction_memcg = mem_cgroup_from_id(memcgid);
  301. if (!mem_cgroup_disabled() && !eviction_memcg)
  302. goto out;
  303. eviction_lruvec = mem_cgroup_lruvec(eviction_memcg, pgdat);
  304. refault = atomic_long_read(&eviction_lruvec->nonresident_age);
  305. /*
  306. * Calculate the refault distance
  307. *
  308. * The unsigned subtraction here gives an accurate distance
  309. * across nonresident_age overflows in most cases. There is a
  310. * special case: usually, shadow entries have a short lifetime
  311. * and are either refaulted or reclaimed along with the inode
  312. * before they get too old. But it is not impossible for the
  313. * nonresident_age to lap a shadow entry in the field, which
  314. * can then result in a false small refault distance, leading
  315. * to a false activation should this old entry actually
  316. * refault again. However, earlier kernels used to deactivate
  317. * unconditionally with *every* reclaim invocation for the
  318. * longest time, so the occasional inappropriate activation
  319. * leading to pressure on the active list is not a problem.
  320. */
  321. refault_distance = (refault - eviction) & EVICTION_MASK;
  322. /*
  323. * The activation decision for this page is made at the level
  324. * where the eviction occurred, as that is where the LRU order
  325. * during page reclaim is being determined.
  326. *
  327. * However, the cgroup that will own the page is the one that
  328. * is actually experiencing the refault event.
  329. */
  330. memcg = page_memcg(page);
  331. lruvec = mem_cgroup_lruvec(memcg, pgdat);
  332. inc_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + file);
  333. /*
  334. * Compare the distance to the existing workingset size. We
  335. * don't activate pages that couldn't stay resident even if
  336. * all the memory was available to the workingset. Whether
  337. * workingset competition needs to consider anon or not depends
  338. * on having swap.
  339. */
  340. workingset_size = lruvec_page_state(eviction_lruvec, NR_ACTIVE_FILE);
  341. if (!file) {
  342. workingset_size += lruvec_page_state(eviction_lruvec,
  343. NR_INACTIVE_FILE);
  344. }
  345. if (mem_cgroup_get_nr_swap_pages(memcg) > 0) {
  346. workingset_size += lruvec_page_state(eviction_lruvec,
  347. NR_ACTIVE_ANON);
  348. if (file) {
  349. workingset_size += lruvec_page_state(eviction_lruvec,
  350. NR_INACTIVE_ANON);
  351. }
  352. }
  353. if (refault_distance > workingset_size)
  354. goto out;
  355. SetPageActive(page);
  356. workingset_age_nonresident(lruvec, thp_nr_pages(page));
  357. inc_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + file);
  358. /* Page was active prior to eviction */
  359. if (workingset) {
  360. SetPageWorkingset(page);
  361. /* XXX: Move to lru_cache_add() when it supports new vs putback */
  362. spin_lock_irq(&page_pgdat(page)->lru_lock);
  363. lru_note_cost_page(page);
  364. spin_unlock_irq(&page_pgdat(page)->lru_lock);
  365. inc_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + file);
  366. }
  367. out:
  368. rcu_read_unlock();
  369. }
  370. /**
  371. * workingset_activation - note a page activation
  372. * @page: page that is being activated
  373. */
  374. void workingset_activation(struct page *page)
  375. {
  376. struct mem_cgroup *memcg;
  377. struct lruvec *lruvec;
  378. rcu_read_lock();
  379. /*
  380. * Filter non-memcg pages here, e.g. unmap can call
  381. * mark_page_accessed() on VDSO pages.
  382. *
  383. * XXX: See workingset_refault() - this should return
  384. * root_mem_cgroup even for !CONFIG_MEMCG.
  385. */
  386. memcg = page_memcg_rcu(page);
  387. if (!mem_cgroup_disabled() && !memcg)
  388. goto out;
  389. lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page));
  390. workingset_age_nonresident(lruvec, thp_nr_pages(page));
  391. out:
  392. rcu_read_unlock();
  393. }
  394. /*
  395. * Shadow entries reflect the share of the working set that does not
  396. * fit into memory, so their number depends on the access pattern of
  397. * the workload. In most cases, they will refault or get reclaimed
  398. * along with the inode, but a (malicious) workload that streams
  399. * through files with a total size several times that of available
  400. * memory, while preventing the inodes from being reclaimed, can
  401. * create excessive amounts of shadow nodes. To keep a lid on this,
  402. * track shadow nodes and reclaim them when they grow way past the
  403. * point where they would still be useful.
  404. */
  405. static struct list_lru shadow_nodes;
  406. void workingset_update_node(struct xa_node *node)
  407. {
  408. /*
  409. * Track non-empty nodes that contain only shadow entries;
  410. * unlink those that contain pages or are being freed.
  411. *
  412. * Avoid acquiring the list_lru lock when the nodes are
  413. * already where they should be. The list_empty() test is safe
  414. * as node->private_list is protected by the i_pages lock.
  415. */
  416. VM_WARN_ON_ONCE(!irqs_disabled()); /* For __inc_lruvec_page_state */
  417. if (node->count && node->count == node->nr_values) {
  418. if (list_empty(&node->private_list)) {
  419. list_lru_add(&shadow_nodes, &node->private_list);
  420. __inc_lruvec_slab_state(node, WORKINGSET_NODES);
  421. }
  422. } else {
  423. if (!list_empty(&node->private_list)) {
  424. list_lru_del(&shadow_nodes, &node->private_list);
  425. __dec_lruvec_slab_state(node, WORKINGSET_NODES);
  426. }
  427. }
  428. }
  429. static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  430. struct shrink_control *sc)
  431. {
  432. unsigned long max_nodes;
  433. unsigned long nodes;
  434. unsigned long pages;
  435. nodes = list_lru_shrink_count(&shadow_nodes, sc);
  436. /*
  437. * Approximate a reasonable limit for the nodes
  438. * containing shadow entries. We don't need to keep more
  439. * shadow entries than possible pages on the active list,
  440. * since refault distances bigger than that are dismissed.
  441. *
  442. * The size of the active list converges toward 100% of
  443. * overall page cache as memory grows, with only a tiny
  444. * inactive list. Assume the total cache size for that.
  445. *
  446. * Nodes might be sparsely populated, with only one shadow
  447. * entry in the extreme case. Obviously, we cannot keep one
  448. * node for every eligible shadow entry, so compromise on a
  449. * worst-case density of 1/8th. Below that, not all eligible
  450. * refaults can be detected anymore.
  451. *
  452. * On 64-bit with 7 xa_nodes per page and 64 slots
  453. * each, this will reclaim shadow entries when they consume
  454. * ~1.8% of available memory:
  455. *
  456. * PAGE_SIZE / xa_nodes / node_entries * 8 / PAGE_SIZE
  457. */
  458. #ifdef CONFIG_MEMCG
  459. if (sc->memcg) {
  460. struct lruvec *lruvec;
  461. int i;
  462. lruvec = mem_cgroup_lruvec(sc->memcg, NODE_DATA(sc->nid));
  463. for (pages = 0, i = 0; i < NR_LRU_LISTS; i++)
  464. pages += lruvec_page_state_local(lruvec,
  465. NR_LRU_BASE + i);
  466. pages += lruvec_page_state_local(
  467. lruvec, NR_SLAB_RECLAIMABLE_B) >> PAGE_SHIFT;
  468. pages += lruvec_page_state_local(
  469. lruvec, NR_SLAB_UNRECLAIMABLE_B) >> PAGE_SHIFT;
  470. } else
  471. #endif
  472. pages = node_present_pages(sc->nid);
  473. max_nodes = pages >> (XA_CHUNK_SHIFT - 3);
  474. if (!nodes)
  475. return SHRINK_EMPTY;
  476. if (nodes <= max_nodes)
  477. return 0;
  478. return nodes - max_nodes;
  479. }
  480. static enum lru_status shadow_lru_isolate(struct list_head *item,
  481. struct list_lru_one *lru,
  482. spinlock_t *lru_lock,
  483. void *arg) __must_hold(lru_lock)
  484. {
  485. struct xa_node *node = container_of(item, struct xa_node, private_list);
  486. struct address_space *mapping;
  487. int ret;
  488. /*
  489. * Page cache insertions and deletions synchronously maintain
  490. * the shadow node LRU under the i_pages lock and the
  491. * lru_lock. Because the page cache tree is emptied before
  492. * the inode can be destroyed, holding the lru_lock pins any
  493. * address_space that has nodes on the LRU.
  494. *
  495. * We can then safely transition to the i_pages lock to
  496. * pin only the address_space of the particular node we want
  497. * to reclaim, take the node off-LRU, and drop the lru_lock.
  498. */
  499. mapping = container_of(node->array, struct address_space, i_pages);
  500. /* Coming from the list, invert the lock order */
  501. if (!xa_trylock(&mapping->i_pages)) {
  502. spin_unlock_irq(lru_lock);
  503. ret = LRU_RETRY;
  504. goto out;
  505. }
  506. list_lru_isolate(lru, item);
  507. __dec_lruvec_slab_state(node, WORKINGSET_NODES);
  508. spin_unlock(lru_lock);
  509. /*
  510. * The nodes should only contain one or more shadow entries,
  511. * no pages, so we expect to be able to remove them all and
  512. * delete and free the empty node afterwards.
  513. */
  514. if (WARN_ON_ONCE(!node->nr_values))
  515. goto out_invalid;
  516. if (WARN_ON_ONCE(node->count != node->nr_values))
  517. goto out_invalid;
  518. mapping->nrexceptional -= node->nr_values;
  519. xa_delete_node(node, workingset_update_node);
  520. __inc_lruvec_slab_state(node, WORKINGSET_NODERECLAIM);
  521. out_invalid:
  522. xa_unlock_irq(&mapping->i_pages);
  523. ret = LRU_REMOVED_RETRY;
  524. out:
  525. cond_resched();
  526. spin_lock_irq(lru_lock);
  527. return ret;
  528. }
  529. static unsigned long scan_shadow_nodes(struct shrinker *shrinker,
  530. struct shrink_control *sc)
  531. {
  532. /* list_lru lock nests inside the IRQ-safe i_pages lock */
  533. return list_lru_shrink_walk_irq(&shadow_nodes, sc, shadow_lru_isolate,
  534. NULL);
  535. }
  536. static struct shrinker workingset_shadow_shrinker = {
  537. .count_objects = count_shadow_nodes,
  538. .scan_objects = scan_shadow_nodes,
  539. .seeks = 0, /* ->count reports only fully expendable nodes */
  540. .flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE,
  541. };
  542. /*
  543. * Our list_lru->lock is IRQ-safe as it nests inside the IRQ-safe
  544. * i_pages lock.
  545. */
  546. static struct lock_class_key shadow_nodes_key;
  547. static int __init workingset_init(void)
  548. {
  549. unsigned int timestamp_bits;
  550. unsigned int max_order;
  551. int ret;
  552. BUILD_BUG_ON(BITS_PER_LONG < EVICTION_SHIFT);
  553. /*
  554. * Calculate the eviction bucket size to cover the longest
  555. * actionable refault distance, which is currently half of
  556. * memory (totalram_pages/2). However, memory hotplug may add
  557. * some more pages at runtime, so keep working with up to
  558. * double the initial memory by using totalram_pages as-is.
  559. */
  560. timestamp_bits = BITS_PER_LONG - EVICTION_SHIFT;
  561. max_order = fls_long(totalram_pages() - 1);
  562. if (max_order > timestamp_bits)
  563. bucket_order = max_order - timestamp_bits;
  564. pr_info("workingset: timestamp_bits=%d max_order=%d bucket_order=%u\n",
  565. timestamp_bits, max_order, bucket_order);
  566. ret = prealloc_shrinker(&workingset_shadow_shrinker);
  567. if (ret)
  568. goto err;
  569. ret = __list_lru_init(&shadow_nodes, true, &shadow_nodes_key,
  570. &workingset_shadow_shrinker);
  571. if (ret)
  572. goto err_list_lru;
  573. register_shrinker_prepared(&workingset_shadow_shrinker);
  574. return 0;
  575. err_list_lru:
  576. free_prealloced_shrinker(&workingset_shadow_shrinker);
  577. err:
  578. return ret;
  579. }
  580. module_init(workingset_init);