swap.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/mm/swap.c
  4. *
  5. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  6. */
  7. /*
  8. * This file contains the default values for the operation of the
  9. * Linux VM subsystem. Fine-tuning documentation can be found in
  10. * Documentation/admin-guide/sysctl/vm.rst.
  11. * Started 18.12.91
  12. * Swap aging added 23.2.95, Stephen Tweedie.
  13. * Buffermem limits added 12.3.98, Rik van Riel.
  14. */
  15. #include <linux/mm.h>
  16. #include <linux/sched.h>
  17. #include <linux/kernel_stat.h>
  18. #include <linux/swap.h>
  19. #include <linux/mman.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/pagevec.h>
  22. #include <linux/init.h>
  23. #include <linux/export.h>
  24. #include <linux/mm_inline.h>
  25. #include <linux/percpu_counter.h>
  26. #include <linux/memremap.h>
  27. #include <linux/percpu.h>
  28. #include <linux/cpu.h>
  29. #include <linux/notifier.h>
  30. #include <linux/backing-dev.h>
  31. #include <linux/memcontrol.h>
  32. #include <linux/gfp.h>
  33. #include <linux/uio.h>
  34. #include <linux/hugetlb.h>
  35. #include <linux/page_idle.h>
  36. #include <linux/local_lock.h>
  37. #include <linux/buffer_head.h>
  38. #include "internal.h"
  39. #define CREATE_TRACE_POINTS
  40. #include <trace/events/pagemap.h>
  41. /* How many pages do we try to swap or page in/out together? */
  42. int page_cluster;
  43. /* Protecting only lru_rotate.fbatch which requires disabling interrupts */
  44. struct lru_rotate {
  45. local_lock_t lock;
  46. struct folio_batch fbatch;
  47. };
  48. static DEFINE_PER_CPU(struct lru_rotate, lru_rotate) = {
  49. .lock = INIT_LOCAL_LOCK(lock),
  50. };
  51. /*
  52. * The following folio batches are grouped together because they are protected
  53. * by disabling preemption (and interrupts remain enabled).
  54. */
  55. struct cpu_fbatches {
  56. local_lock_t lock;
  57. struct folio_batch lru_add;
  58. struct folio_batch lru_deactivate_file;
  59. struct folio_batch lru_deactivate;
  60. struct folio_batch lru_lazyfree;
  61. #ifdef CONFIG_SMP
  62. struct folio_batch activate;
  63. #endif
  64. };
  65. static DEFINE_PER_CPU(struct cpu_fbatches, cpu_fbatches) = {
  66. .lock = INIT_LOCAL_LOCK(lock),
  67. };
  68. /*
  69. * This path almost never happens for VM activity - pages are normally freed
  70. * via pagevecs. But it gets used by networking - and for compound pages.
  71. */
  72. static void __page_cache_release(struct folio *folio)
  73. {
  74. if (folio_test_lru(folio)) {
  75. struct lruvec *lruvec;
  76. unsigned long flags;
  77. lruvec = folio_lruvec_lock_irqsave(folio, &flags);
  78. lruvec_del_folio(lruvec, folio);
  79. __folio_clear_lru_flags(folio);
  80. unlock_page_lruvec_irqrestore(lruvec, flags);
  81. }
  82. /* See comment on folio_test_mlocked in release_pages() */
  83. if (unlikely(folio_test_mlocked(folio))) {
  84. long nr_pages = folio_nr_pages(folio);
  85. __folio_clear_mlocked(folio);
  86. zone_stat_mod_folio(folio, NR_MLOCK, -nr_pages);
  87. count_vm_events(UNEVICTABLE_PGCLEARED, nr_pages);
  88. }
  89. }
  90. static void __folio_put_small(struct folio *folio)
  91. {
  92. __page_cache_release(folio);
  93. mem_cgroup_uncharge(folio);
  94. free_unref_page(&folio->page, 0);
  95. }
  96. static void __folio_put_large(struct folio *folio)
  97. {
  98. /*
  99. * __page_cache_release() is supposed to be called for thp, not for
  100. * hugetlb. This is because hugetlb page does never have PageLRU set
  101. * (it's never listed to any LRU lists) and no memcg routines should
  102. * be called for hugetlb (it has a separate hugetlb_cgroup.)
  103. */
  104. if (!folio_test_hugetlb(folio))
  105. __page_cache_release(folio);
  106. destroy_large_folio(folio);
  107. }
  108. void __folio_put(struct folio *folio)
  109. {
  110. if (unlikely(folio_is_zone_device(folio)))
  111. free_zone_device_page(&folio->page);
  112. else if (unlikely(folio_test_large(folio)))
  113. __folio_put_large(folio);
  114. else
  115. __folio_put_small(folio);
  116. }
  117. EXPORT_SYMBOL(__folio_put);
  118. /**
  119. * put_pages_list() - release a list of pages
  120. * @pages: list of pages threaded on page->lru
  121. *
  122. * Release a list of pages which are strung together on page.lru.
  123. */
  124. void put_pages_list(struct list_head *pages)
  125. {
  126. struct folio *folio, *next;
  127. list_for_each_entry_safe(folio, next, pages, lru) {
  128. if (!folio_put_testzero(folio)) {
  129. list_del(&folio->lru);
  130. continue;
  131. }
  132. if (folio_test_large(folio)) {
  133. list_del(&folio->lru);
  134. __folio_put_large(folio);
  135. continue;
  136. }
  137. /* LRU flag must be clear because it's passed using the lru */
  138. }
  139. free_unref_page_list(pages);
  140. INIT_LIST_HEAD(pages);
  141. }
  142. EXPORT_SYMBOL(put_pages_list);
  143. /*
  144. * get_kernel_pages() - pin kernel pages in memory
  145. * @kiov: An array of struct kvec structures
  146. * @nr_segs: number of segments to pin
  147. * @write: pinning for read/write, currently ignored
  148. * @pages: array that receives pointers to the pages pinned.
  149. * Should be at least nr_segs long.
  150. *
  151. * Returns number of pages pinned. This may be fewer than the number requested.
  152. * If nr_segs is 0 or negative, returns 0. If no pages were pinned, returns 0.
  153. * Each page returned must be released with a put_page() call when it is
  154. * finished with.
  155. */
  156. int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
  157. struct page **pages)
  158. {
  159. int seg;
  160. for (seg = 0; seg < nr_segs; seg++) {
  161. if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
  162. return seg;
  163. pages[seg] = kmap_to_page(kiov[seg].iov_base);
  164. get_page(pages[seg]);
  165. }
  166. return seg;
  167. }
  168. EXPORT_SYMBOL_GPL(get_kernel_pages);
  169. typedef void (*move_fn_t)(struct lruvec *lruvec, struct folio *folio);
  170. static void lru_add_fn(struct lruvec *lruvec, struct folio *folio)
  171. {
  172. int was_unevictable = folio_test_clear_unevictable(folio);
  173. long nr_pages = folio_nr_pages(folio);
  174. VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
  175. /*
  176. * Is an smp_mb__after_atomic() still required here, before
  177. * folio_evictable() tests the mlocked flag, to rule out the possibility
  178. * of stranding an evictable folio on an unevictable LRU? I think
  179. * not, because __munlock_page() only clears the mlocked flag
  180. * while the LRU lock is held.
  181. *
  182. * (That is not true of __page_cache_release(), and not necessarily
  183. * true of release_pages(): but those only clear the mlocked flag after
  184. * folio_put_testzero() has excluded any other users of the folio.)
  185. */
  186. if (folio_evictable(folio)) {
  187. if (was_unevictable)
  188. __count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages);
  189. } else {
  190. folio_clear_active(folio);
  191. folio_set_unevictable(folio);
  192. /*
  193. * folio->mlock_count = !!folio_test_mlocked(folio)?
  194. * But that leaves __mlock_page() in doubt whether another
  195. * actor has already counted the mlock or not. Err on the
  196. * safe side, underestimate, let page reclaim fix it, rather
  197. * than leaving a page on the unevictable LRU indefinitely.
  198. */
  199. folio->mlock_count = 0;
  200. if (!was_unevictable)
  201. __count_vm_events(UNEVICTABLE_PGCULLED, nr_pages);
  202. }
  203. lruvec_add_folio(lruvec, folio);
  204. trace_mm_lru_insertion(folio);
  205. }
  206. static void folio_batch_move_lru(struct folio_batch *fbatch, move_fn_t move_fn)
  207. {
  208. int i;
  209. struct lruvec *lruvec = NULL;
  210. unsigned long flags = 0;
  211. for (i = 0; i < folio_batch_count(fbatch); i++) {
  212. struct folio *folio = fbatch->folios[i];
  213. /* block memcg migration while the folio moves between lru */
  214. if (move_fn != lru_add_fn && !folio_test_clear_lru(folio))
  215. continue;
  216. lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags);
  217. move_fn(lruvec, folio);
  218. folio_set_lru(folio);
  219. }
  220. if (lruvec)
  221. unlock_page_lruvec_irqrestore(lruvec, flags);
  222. folios_put(fbatch->folios, folio_batch_count(fbatch));
  223. folio_batch_init(fbatch);
  224. }
  225. static void folio_batch_add_and_move(struct folio_batch *fbatch,
  226. struct folio *folio, move_fn_t move_fn)
  227. {
  228. if (folio_batch_add(fbatch, folio) && !folio_test_large(folio) &&
  229. !lru_cache_disabled())
  230. return;
  231. folio_batch_move_lru(fbatch, move_fn);
  232. }
  233. static void lru_move_tail_fn(struct lruvec *lruvec, struct folio *folio)
  234. {
  235. if (!folio_test_unevictable(folio)) {
  236. lruvec_del_folio(lruvec, folio);
  237. folio_clear_active(folio);
  238. lruvec_add_folio_tail(lruvec, folio);
  239. __count_vm_events(PGROTATED, folio_nr_pages(folio));
  240. }
  241. }
  242. /*
  243. * Writeback is about to end against a folio which has been marked for
  244. * immediate reclaim. If it still appears to be reclaimable, move it
  245. * to the tail of the inactive list.
  246. *
  247. * folio_rotate_reclaimable() must disable IRQs, to prevent nasty races.
  248. */
  249. void folio_rotate_reclaimable(struct folio *folio)
  250. {
  251. if (!folio_test_locked(folio) && !folio_test_dirty(folio) &&
  252. !folio_test_unevictable(folio) && folio_test_lru(folio)) {
  253. struct folio_batch *fbatch;
  254. unsigned long flags;
  255. folio_get(folio);
  256. local_lock_irqsave(&lru_rotate.lock, flags);
  257. fbatch = this_cpu_ptr(&lru_rotate.fbatch);
  258. folio_batch_add_and_move(fbatch, folio, lru_move_tail_fn);
  259. local_unlock_irqrestore(&lru_rotate.lock, flags);
  260. }
  261. }
  262. void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages)
  263. {
  264. do {
  265. unsigned long lrusize;
  266. /*
  267. * Hold lruvec->lru_lock is safe here, since
  268. * 1) The pinned lruvec in reclaim, or
  269. * 2) From a pre-LRU page during refault (which also holds the
  270. * rcu lock, so would be safe even if the page was on the LRU
  271. * and could move simultaneously to a new lruvec).
  272. */
  273. spin_lock_irq(&lruvec->lru_lock);
  274. /* Record cost event */
  275. if (file)
  276. lruvec->file_cost += nr_pages;
  277. else
  278. lruvec->anon_cost += nr_pages;
  279. /*
  280. * Decay previous events
  281. *
  282. * Because workloads change over time (and to avoid
  283. * overflow) we keep these statistics as a floating
  284. * average, which ends up weighing recent refaults
  285. * more than old ones.
  286. */
  287. lrusize = lruvec_page_state(lruvec, NR_INACTIVE_ANON) +
  288. lruvec_page_state(lruvec, NR_ACTIVE_ANON) +
  289. lruvec_page_state(lruvec, NR_INACTIVE_FILE) +
  290. lruvec_page_state(lruvec, NR_ACTIVE_FILE);
  291. if (lruvec->file_cost + lruvec->anon_cost > lrusize / 4) {
  292. lruvec->file_cost /= 2;
  293. lruvec->anon_cost /= 2;
  294. }
  295. spin_unlock_irq(&lruvec->lru_lock);
  296. } while ((lruvec = parent_lruvec(lruvec)));
  297. }
  298. void lru_note_cost_folio(struct folio *folio)
  299. {
  300. lru_note_cost(folio_lruvec(folio), folio_is_file_lru(folio),
  301. folio_nr_pages(folio));
  302. }
  303. static void folio_activate_fn(struct lruvec *lruvec, struct folio *folio)
  304. {
  305. if (!folio_test_active(folio) && !folio_test_unevictable(folio)) {
  306. long nr_pages = folio_nr_pages(folio);
  307. lruvec_del_folio(lruvec, folio);
  308. folio_set_active(folio);
  309. lruvec_add_folio(lruvec, folio);
  310. trace_mm_lru_activate(folio);
  311. __count_vm_events(PGACTIVATE, nr_pages);
  312. __count_memcg_events(lruvec_memcg(lruvec), PGACTIVATE,
  313. nr_pages);
  314. }
  315. }
  316. #ifdef CONFIG_SMP
  317. static void folio_activate_drain(int cpu)
  318. {
  319. struct folio_batch *fbatch = &per_cpu(cpu_fbatches.activate, cpu);
  320. if (folio_batch_count(fbatch))
  321. folio_batch_move_lru(fbatch, folio_activate_fn);
  322. }
  323. void folio_activate(struct folio *folio)
  324. {
  325. if (folio_test_lru(folio) && !folio_test_active(folio) &&
  326. !folio_test_unevictable(folio)) {
  327. struct folio_batch *fbatch;
  328. folio_get(folio);
  329. local_lock(&cpu_fbatches.lock);
  330. fbatch = this_cpu_ptr(&cpu_fbatches.activate);
  331. folio_batch_add_and_move(fbatch, folio, folio_activate_fn);
  332. local_unlock(&cpu_fbatches.lock);
  333. }
  334. }
  335. #else
  336. static inline void folio_activate_drain(int cpu)
  337. {
  338. }
  339. void folio_activate(struct folio *folio)
  340. {
  341. struct lruvec *lruvec;
  342. if (folio_test_clear_lru(folio)) {
  343. lruvec = folio_lruvec_lock_irq(folio);
  344. folio_activate_fn(lruvec, folio);
  345. unlock_page_lruvec_irq(lruvec);
  346. folio_set_lru(folio);
  347. }
  348. }
  349. #endif
  350. static void __lru_cache_activate_folio(struct folio *folio)
  351. {
  352. struct folio_batch *fbatch;
  353. int i;
  354. local_lock(&cpu_fbatches.lock);
  355. fbatch = this_cpu_ptr(&cpu_fbatches.lru_add);
  356. /*
  357. * Search backwards on the optimistic assumption that the folio being
  358. * activated has just been added to this batch. Note that only
  359. * the local batch is examined as a !LRU folio could be in the
  360. * process of being released, reclaimed, migrated or on a remote
  361. * batch that is currently being drained. Furthermore, marking
  362. * a remote batch's folio active potentially hits a race where
  363. * a folio is marked active just after it is added to the inactive
  364. * list causing accounting errors and BUG_ON checks to trigger.
  365. */
  366. for (i = folio_batch_count(fbatch) - 1; i >= 0; i--) {
  367. struct folio *batch_folio = fbatch->folios[i];
  368. if (batch_folio == folio) {
  369. folio_set_active(folio);
  370. break;
  371. }
  372. }
  373. local_unlock(&cpu_fbatches.lock);
  374. }
  375. #ifdef CONFIG_LRU_GEN
  376. static void folio_inc_refs(struct folio *folio)
  377. {
  378. unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
  379. if (folio_test_unevictable(folio))
  380. return;
  381. if (!folio_test_referenced(folio)) {
  382. folio_set_referenced(folio);
  383. return;
  384. }
  385. if (!folio_test_workingset(folio)) {
  386. folio_set_workingset(folio);
  387. return;
  388. }
  389. /* see the comment on MAX_NR_TIERS */
  390. do {
  391. new_flags = old_flags & LRU_REFS_MASK;
  392. if (new_flags == LRU_REFS_MASK)
  393. break;
  394. new_flags += BIT(LRU_REFS_PGOFF);
  395. new_flags |= old_flags & ~LRU_REFS_MASK;
  396. } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags));
  397. }
  398. #else
  399. static void folio_inc_refs(struct folio *folio)
  400. {
  401. }
  402. #endif /* CONFIG_LRU_GEN */
  403. /*
  404. * Mark a page as having seen activity.
  405. *
  406. * inactive,unreferenced -> inactive,referenced
  407. * inactive,referenced -> active,unreferenced
  408. * active,unreferenced -> active,referenced
  409. *
  410. * When a newly allocated page is not yet visible, so safe for non-atomic ops,
  411. * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
  412. */
  413. void folio_mark_accessed(struct folio *folio)
  414. {
  415. if (lru_gen_enabled()) {
  416. folio_inc_refs(folio);
  417. return;
  418. }
  419. if (!folio_test_referenced(folio)) {
  420. folio_set_referenced(folio);
  421. } else if (folio_test_unevictable(folio)) {
  422. /*
  423. * Unevictable pages are on the "LRU_UNEVICTABLE" list. But,
  424. * this list is never rotated or maintained, so marking an
  425. * unevictable page accessed has no effect.
  426. */
  427. } else if (!folio_test_active(folio)) {
  428. /*
  429. * If the folio is on the LRU, queue it for activation via
  430. * cpu_fbatches.activate. Otherwise, assume the folio is in a
  431. * folio_batch, mark it active and it'll be moved to the active
  432. * LRU on the next drain.
  433. */
  434. if (folio_test_lru(folio))
  435. folio_activate(folio);
  436. else
  437. __lru_cache_activate_folio(folio);
  438. folio_clear_referenced(folio);
  439. workingset_activation(folio);
  440. }
  441. if (folio_test_idle(folio))
  442. folio_clear_idle(folio);
  443. }
  444. EXPORT_SYMBOL(folio_mark_accessed);
  445. /**
  446. * folio_add_lru - Add a folio to an LRU list.
  447. * @folio: The folio to be added to the LRU.
  448. *
  449. * Queue the folio for addition to the LRU. The decision on whether
  450. * to add the page to the [in]active [file|anon] list is deferred until the
  451. * folio_batch is drained. This gives a chance for the caller of folio_add_lru()
  452. * have the folio added to the active list using folio_mark_accessed().
  453. */
  454. void folio_add_lru(struct folio *folio)
  455. {
  456. struct folio_batch *fbatch;
  457. VM_BUG_ON_FOLIO(folio_test_active(folio) &&
  458. folio_test_unevictable(folio), folio);
  459. VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
  460. /* see the comment in lru_gen_add_folio() */
  461. if (lru_gen_enabled() && !folio_test_unevictable(folio) &&
  462. lru_gen_in_fault() && !(current->flags & PF_MEMALLOC))
  463. folio_set_active(folio);
  464. folio_get(folio);
  465. local_lock(&cpu_fbatches.lock);
  466. fbatch = this_cpu_ptr(&cpu_fbatches.lru_add);
  467. folio_batch_add_and_move(fbatch, folio, lru_add_fn);
  468. local_unlock(&cpu_fbatches.lock);
  469. }
  470. EXPORT_SYMBOL(folio_add_lru);
  471. /**
  472. * folio_add_lru_vma() - Add a folio to the appropate LRU list for this VMA.
  473. * @folio: The folio to be added to the LRU.
  474. * @vma: VMA in which the folio is mapped.
  475. *
  476. * If the VMA is mlocked, @folio is added to the unevictable list.
  477. * Otherwise, it is treated the same way as folio_add_lru().
  478. */
  479. void folio_add_lru_vma(struct folio *folio, struct vm_area_struct *vma)
  480. {
  481. VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
  482. if (unlikely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED))
  483. mlock_new_page(&folio->page);
  484. else
  485. folio_add_lru(folio);
  486. }
  487. /*
  488. * If the folio cannot be invalidated, it is moved to the
  489. * inactive list to speed up its reclaim. It is moved to the
  490. * head of the list, rather than the tail, to give the flusher
  491. * threads some time to write it out, as this is much more
  492. * effective than the single-page writeout from reclaim.
  493. *
  494. * If the folio isn't mapped and dirty/writeback, the folio
  495. * could be reclaimed asap using the reclaim flag.
  496. *
  497. * 1. active, mapped folio -> none
  498. * 2. active, dirty/writeback folio -> inactive, head, reclaim
  499. * 3. inactive, mapped folio -> none
  500. * 4. inactive, dirty/writeback folio -> inactive, head, reclaim
  501. * 5. inactive, clean -> inactive, tail
  502. * 6. Others -> none
  503. *
  504. * In 4, it moves to the head of the inactive list so the folio is
  505. * written out by flusher threads as this is much more efficient
  506. * than the single-page writeout from reclaim.
  507. */
  508. static void lru_deactivate_file_fn(struct lruvec *lruvec, struct folio *folio)
  509. {
  510. bool active = folio_test_active(folio);
  511. long nr_pages = folio_nr_pages(folio);
  512. if (folio_test_unevictable(folio))
  513. return;
  514. /* Some processes are using the folio */
  515. if (folio_mapped(folio))
  516. return;
  517. lruvec_del_folio(lruvec, folio);
  518. folio_clear_active(folio);
  519. folio_clear_referenced(folio);
  520. if (folio_test_writeback(folio) || folio_test_dirty(folio)) {
  521. /*
  522. * Setting the reclaim flag could race with
  523. * folio_end_writeback() and confuse readahead. But the
  524. * race window is _really_ small and it's not a critical
  525. * problem.
  526. */
  527. lruvec_add_folio(lruvec, folio);
  528. folio_set_reclaim(folio);
  529. } else {
  530. /*
  531. * The folio's writeback ended while it was in the batch.
  532. * We move that folio to the tail of the inactive list.
  533. */
  534. lruvec_add_folio_tail(lruvec, folio);
  535. __count_vm_events(PGROTATED, nr_pages);
  536. }
  537. if (active) {
  538. __count_vm_events(PGDEACTIVATE, nr_pages);
  539. __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
  540. nr_pages);
  541. }
  542. }
  543. static void lru_deactivate_fn(struct lruvec *lruvec, struct folio *folio)
  544. {
  545. if (!folio_test_unevictable(folio) && (folio_test_active(folio) || lru_gen_enabled())) {
  546. long nr_pages = folio_nr_pages(folio);
  547. lruvec_del_folio(lruvec, folio);
  548. folio_clear_active(folio);
  549. folio_clear_referenced(folio);
  550. lruvec_add_folio(lruvec, folio);
  551. __count_vm_events(PGDEACTIVATE, nr_pages);
  552. __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
  553. nr_pages);
  554. }
  555. }
  556. static void lru_lazyfree_fn(struct lruvec *lruvec, struct folio *folio)
  557. {
  558. if (folio_test_anon(folio) && folio_test_swapbacked(folio) &&
  559. !folio_test_swapcache(folio) && !folio_test_unevictable(folio)) {
  560. long nr_pages = folio_nr_pages(folio);
  561. lruvec_del_folio(lruvec, folio);
  562. folio_clear_active(folio);
  563. folio_clear_referenced(folio);
  564. /*
  565. * Lazyfree folios are clean anonymous folios. They have
  566. * the swapbacked flag cleared, to distinguish them from normal
  567. * anonymous folios
  568. */
  569. folio_clear_swapbacked(folio);
  570. lruvec_add_folio(lruvec, folio);
  571. __count_vm_events(PGLAZYFREE, nr_pages);
  572. __count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE,
  573. nr_pages);
  574. }
  575. }
  576. /*
  577. * Drain pages out of the cpu's folio_batch.
  578. * Either "cpu" is the current CPU, and preemption has already been
  579. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  580. */
  581. void lru_add_drain_cpu(int cpu)
  582. {
  583. struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
  584. struct folio_batch *fbatch = &fbatches->lru_add;
  585. if (folio_batch_count(fbatch))
  586. folio_batch_move_lru(fbatch, lru_add_fn);
  587. fbatch = &per_cpu(lru_rotate.fbatch, cpu);
  588. /* Disabling interrupts below acts as a compiler barrier. */
  589. if (data_race(folio_batch_count(fbatch))) {
  590. unsigned long flags;
  591. /* No harm done if a racing interrupt already did this */
  592. local_lock_irqsave(&lru_rotate.lock, flags);
  593. folio_batch_move_lru(fbatch, lru_move_tail_fn);
  594. local_unlock_irqrestore(&lru_rotate.lock, flags);
  595. }
  596. fbatch = &fbatches->lru_deactivate_file;
  597. if (folio_batch_count(fbatch))
  598. folio_batch_move_lru(fbatch, lru_deactivate_file_fn);
  599. fbatch = &fbatches->lru_deactivate;
  600. if (folio_batch_count(fbatch))
  601. folio_batch_move_lru(fbatch, lru_deactivate_fn);
  602. fbatch = &fbatches->lru_lazyfree;
  603. if (folio_batch_count(fbatch))
  604. folio_batch_move_lru(fbatch, lru_lazyfree_fn);
  605. folio_activate_drain(cpu);
  606. }
  607. /**
  608. * deactivate_file_folio() - Deactivate a file folio.
  609. * @folio: Folio to deactivate.
  610. *
  611. * This function hints to the VM that @folio is a good reclaim candidate,
  612. * for example if its invalidation fails due to the folio being dirty
  613. * or under writeback.
  614. *
  615. * Context: Caller holds a reference on the folio.
  616. */
  617. void deactivate_file_folio(struct folio *folio)
  618. {
  619. struct folio_batch *fbatch;
  620. /* Deactivating an unevictable folio will not accelerate reclaim */
  621. if (folio_test_unevictable(folio))
  622. return;
  623. folio_get(folio);
  624. local_lock(&cpu_fbatches.lock);
  625. fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate_file);
  626. folio_batch_add_and_move(fbatch, folio, lru_deactivate_file_fn);
  627. local_unlock(&cpu_fbatches.lock);
  628. }
  629. /*
  630. * deactivate_page - deactivate a page
  631. * @page: page to deactivate
  632. *
  633. * deactivate_page() moves @page to the inactive list if @page was on the active
  634. * list and was not an unevictable page. This is done to accelerate the reclaim
  635. * of @page.
  636. */
  637. void deactivate_page(struct page *page)
  638. {
  639. struct folio *folio = page_folio(page);
  640. if (folio_test_lru(folio) && !folio_test_unevictable(folio) &&
  641. (folio_test_active(folio) || lru_gen_enabled())) {
  642. struct folio_batch *fbatch;
  643. folio_get(folio);
  644. local_lock(&cpu_fbatches.lock);
  645. fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate);
  646. folio_batch_add_and_move(fbatch, folio, lru_deactivate_fn);
  647. local_unlock(&cpu_fbatches.lock);
  648. }
  649. }
  650. /**
  651. * mark_page_lazyfree - make an anon page lazyfree
  652. * @page: page to deactivate
  653. *
  654. * mark_page_lazyfree() moves @page to the inactive file list.
  655. * This is done to accelerate the reclaim of @page.
  656. */
  657. void mark_page_lazyfree(struct page *page)
  658. {
  659. struct folio *folio = page_folio(page);
  660. if (folio_test_lru(folio) && folio_test_anon(folio) &&
  661. folio_test_swapbacked(folio) && !folio_test_swapcache(folio) &&
  662. !folio_test_unevictable(folio)) {
  663. struct folio_batch *fbatch;
  664. folio_get(folio);
  665. local_lock(&cpu_fbatches.lock);
  666. fbatch = this_cpu_ptr(&cpu_fbatches.lru_lazyfree);
  667. folio_batch_add_and_move(fbatch, folio, lru_lazyfree_fn);
  668. local_unlock(&cpu_fbatches.lock);
  669. }
  670. }
  671. void lru_add_drain(void)
  672. {
  673. local_lock(&cpu_fbatches.lock);
  674. lru_add_drain_cpu(smp_processor_id());
  675. local_unlock(&cpu_fbatches.lock);
  676. mlock_page_drain_local();
  677. }
  678. /*
  679. * It's called from per-cpu workqueue context in SMP case so
  680. * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on
  681. * the same cpu. It shouldn't be a problem in !SMP case since
  682. * the core is only one and the locks will disable preemption.
  683. */
  684. static void lru_add_and_bh_lrus_drain(void)
  685. {
  686. local_lock(&cpu_fbatches.lock);
  687. lru_add_drain_cpu(smp_processor_id());
  688. local_unlock(&cpu_fbatches.lock);
  689. invalidate_bh_lrus_cpu();
  690. mlock_page_drain_local();
  691. }
  692. void lru_add_drain_cpu_zone(struct zone *zone)
  693. {
  694. local_lock(&cpu_fbatches.lock);
  695. lru_add_drain_cpu(smp_processor_id());
  696. drain_local_pages(zone);
  697. local_unlock(&cpu_fbatches.lock);
  698. mlock_page_drain_local();
  699. }
  700. #ifdef CONFIG_SMP
  701. static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  702. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  703. {
  704. lru_add_and_bh_lrus_drain();
  705. }
  706. static bool cpu_needs_drain(unsigned int cpu)
  707. {
  708. struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
  709. /* Check these in order of likelihood that they're not zero */
  710. return folio_batch_count(&fbatches->lru_add) ||
  711. data_race(folio_batch_count(&per_cpu(lru_rotate.fbatch, cpu))) ||
  712. folio_batch_count(&fbatches->lru_deactivate_file) ||
  713. folio_batch_count(&fbatches->lru_deactivate) ||
  714. folio_batch_count(&fbatches->lru_lazyfree) ||
  715. folio_batch_count(&fbatches->activate) ||
  716. need_mlock_page_drain(cpu) ||
  717. has_bh_in_lru(cpu, NULL);
  718. }
  719. /*
  720. * Doesn't need any cpu hotplug locking because we do rely on per-cpu
  721. * kworkers being shut down before our page_alloc_cpu_dead callback is
  722. * executed on the offlined cpu.
  723. * Calling this function with cpu hotplug locks held can actually lead
  724. * to obscure indirect dependencies via WQ context.
  725. */
  726. static inline void __lru_add_drain_all(bool force_all_cpus)
  727. {
  728. /*
  729. * lru_drain_gen - Global pages generation number
  730. *
  731. * (A) Definition: global lru_drain_gen = x implies that all generations
  732. * 0 < n <= x are already *scheduled* for draining.
  733. *
  734. * This is an optimization for the highly-contended use case where a
  735. * user space workload keeps constantly generating a flow of pages for
  736. * each CPU.
  737. */
  738. static unsigned int lru_drain_gen;
  739. static struct cpumask has_work;
  740. static DEFINE_MUTEX(lock);
  741. unsigned cpu, this_gen;
  742. /*
  743. * Make sure nobody triggers this path before mm_percpu_wq is fully
  744. * initialized.
  745. */
  746. if (WARN_ON(!mm_percpu_wq))
  747. return;
  748. /*
  749. * Guarantee folio_batch counter stores visible by this CPU
  750. * are visible to other CPUs before loading the current drain
  751. * generation.
  752. */
  753. smp_mb();
  754. /*
  755. * (B) Locally cache global LRU draining generation number
  756. *
  757. * The read barrier ensures that the counter is loaded before the mutex
  758. * is taken. It pairs with smp_mb() inside the mutex critical section
  759. * at (D).
  760. */
  761. this_gen = smp_load_acquire(&lru_drain_gen);
  762. mutex_lock(&lock);
  763. /*
  764. * (C) Exit the draining operation if a newer generation, from another
  765. * lru_add_drain_all(), was already scheduled for draining. Check (A).
  766. */
  767. if (unlikely(this_gen != lru_drain_gen && !force_all_cpus))
  768. goto done;
  769. /*
  770. * (D) Increment global generation number
  771. *
  772. * Pairs with smp_load_acquire() at (B), outside of the critical
  773. * section. Use a full memory barrier to guarantee that the
  774. * new global drain generation number is stored before loading
  775. * folio_batch counters.
  776. *
  777. * This pairing must be done here, before the for_each_online_cpu loop
  778. * below which drains the page vectors.
  779. *
  780. * Let x, y, and z represent some system CPU numbers, where x < y < z.
  781. * Assume CPU #z is in the middle of the for_each_online_cpu loop
  782. * below and has already reached CPU #y's per-cpu data. CPU #x comes
  783. * along, adds some pages to its per-cpu vectors, then calls
  784. * lru_add_drain_all().
  785. *
  786. * If the paired barrier is done at any later step, e.g. after the
  787. * loop, CPU #x will just exit at (C) and miss flushing out all of its
  788. * added pages.
  789. */
  790. WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1);
  791. smp_mb();
  792. cpumask_clear(&has_work);
  793. for_each_online_cpu(cpu) {
  794. struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  795. if (cpu_needs_drain(cpu)) {
  796. INIT_WORK(work, lru_add_drain_per_cpu);
  797. queue_work_on(cpu, mm_percpu_wq, work);
  798. __cpumask_set_cpu(cpu, &has_work);
  799. }
  800. }
  801. for_each_cpu(cpu, &has_work)
  802. flush_work(&per_cpu(lru_add_drain_work, cpu));
  803. done:
  804. mutex_unlock(&lock);
  805. }
  806. void lru_add_drain_all(void)
  807. {
  808. __lru_add_drain_all(false);
  809. }
  810. #else
  811. void lru_add_drain_all(void)
  812. {
  813. lru_add_drain();
  814. }
  815. #endif /* CONFIG_SMP */
  816. atomic_t lru_disable_count = ATOMIC_INIT(0);
  817. EXPORT_SYMBOL_GPL(lru_disable_count);
  818. /*
  819. * lru_cache_disable() needs to be called before we start compiling
  820. * a list of pages to be migrated using isolate_lru_page().
  821. * It drains pages on LRU cache and then disable on all cpus until
  822. * lru_cache_enable is called.
  823. *
  824. * Must be paired with a call to lru_cache_enable().
  825. */
  826. void lru_cache_disable(void)
  827. {
  828. /*
  829. * If someone is already disabled lru_cache, just return with
  830. * increasing the lru_disable_count.
  831. */
  832. if (atomic_inc_not_zero(&lru_disable_count))
  833. return;
  834. /*
  835. * Readers of lru_disable_count are protected by either disabling
  836. * preemption or rcu_read_lock:
  837. *
  838. * preempt_disable, local_irq_disable [bh_lru_lock()]
  839. * rcu_read_lock [rt_spin_lock CONFIG_PREEMPT_RT]
  840. * preempt_disable [local_lock !CONFIG_PREEMPT_RT]
  841. *
  842. * Since v5.1 kernel, synchronize_rcu() is guaranteed to wait on
  843. * preempt_disable() regions of code. So any CPU which sees
  844. * lru_disable_count = 0 will have exited the critical
  845. * section when synchronize_rcu() returns.
  846. */
  847. synchronize_rcu_expedited();
  848. #ifdef CONFIG_SMP
  849. __lru_add_drain_all(true);
  850. #else
  851. lru_add_and_bh_lrus_drain();
  852. #endif
  853. atomic_inc(&lru_disable_count);
  854. }
  855. EXPORT_SYMBOL_GPL(lru_cache_disable);
  856. /**
  857. * release_pages - batched put_page()
  858. * @pages: array of pages to release
  859. * @nr: number of pages
  860. *
  861. * Decrement the reference count on all the pages in @pages. If it
  862. * fell to zero, remove the page from the LRU and free it.
  863. */
  864. void release_pages(struct page **pages, int nr)
  865. {
  866. int i;
  867. LIST_HEAD(pages_to_free);
  868. struct lruvec *lruvec = NULL;
  869. unsigned long flags = 0;
  870. unsigned int lock_batch;
  871. for (i = 0; i < nr; i++) {
  872. struct folio *folio = page_folio(pages[i]);
  873. /*
  874. * Make sure the IRQ-safe lock-holding time does not get
  875. * excessive with a continuous string of pages from the
  876. * same lruvec. The lock is held only if lruvec != NULL.
  877. */
  878. if (lruvec && ++lock_batch == SWAP_CLUSTER_MAX) {
  879. unlock_page_lruvec_irqrestore(lruvec, flags);
  880. lruvec = NULL;
  881. }
  882. if (is_huge_zero_page(&folio->page))
  883. continue;
  884. if (folio_is_zone_device(folio)) {
  885. if (lruvec) {
  886. unlock_page_lruvec_irqrestore(lruvec, flags);
  887. lruvec = NULL;
  888. }
  889. if (put_devmap_managed_page(&folio->page))
  890. continue;
  891. if (folio_put_testzero(folio))
  892. free_zone_device_page(&folio->page);
  893. continue;
  894. }
  895. if (!folio_put_testzero(folio))
  896. continue;
  897. if (folio_test_large(folio)) {
  898. if (lruvec) {
  899. unlock_page_lruvec_irqrestore(lruvec, flags);
  900. lruvec = NULL;
  901. }
  902. __folio_put_large(folio);
  903. continue;
  904. }
  905. if (folio_test_lru(folio)) {
  906. struct lruvec *prev_lruvec = lruvec;
  907. lruvec = folio_lruvec_relock_irqsave(folio, lruvec,
  908. &flags);
  909. if (prev_lruvec != lruvec)
  910. lock_batch = 0;
  911. lruvec_del_folio(lruvec, folio);
  912. __folio_clear_lru_flags(folio);
  913. }
  914. /*
  915. * In rare cases, when truncation or holepunching raced with
  916. * munlock after VM_LOCKED was cleared, Mlocked may still be
  917. * found set here. This does not indicate a problem, unless
  918. * "unevictable_pgs_cleared" appears worryingly large.
  919. */
  920. if (unlikely(folio_test_mlocked(folio))) {
  921. __folio_clear_mlocked(folio);
  922. zone_stat_sub_folio(folio, NR_MLOCK);
  923. count_vm_event(UNEVICTABLE_PGCLEARED);
  924. }
  925. list_add(&folio->lru, &pages_to_free);
  926. }
  927. if (lruvec)
  928. unlock_page_lruvec_irqrestore(lruvec, flags);
  929. mem_cgroup_uncharge_list(&pages_to_free);
  930. free_unref_page_list(&pages_to_free);
  931. }
  932. EXPORT_SYMBOL(release_pages);
  933. /*
  934. * The pages which we're about to release may be in the deferred lru-addition
  935. * queues. That would prevent them from really being freed right now. That's
  936. * OK from a correctness point of view but is inefficient - those pages may be
  937. * cache-warm and we want to give them back to the page allocator ASAP.
  938. *
  939. * So __pagevec_release() will drain those queues here.
  940. * folio_batch_move_lru() calls folios_put() directly to avoid
  941. * mutual recursion.
  942. */
  943. void __pagevec_release(struct pagevec *pvec)
  944. {
  945. if (!pvec->percpu_pvec_drained) {
  946. lru_add_drain();
  947. pvec->percpu_pvec_drained = true;
  948. }
  949. release_pages(pvec->pages, pagevec_count(pvec));
  950. pagevec_reinit(pvec);
  951. }
  952. EXPORT_SYMBOL(__pagevec_release);
  953. /**
  954. * folio_batch_remove_exceptionals() - Prune non-folios from a batch.
  955. * @fbatch: The batch to prune
  956. *
  957. * find_get_entries() fills a batch with both folios and shadow/swap/DAX
  958. * entries. This function prunes all the non-folio entries from @fbatch
  959. * without leaving holes, so that it can be passed on to folio-only batch
  960. * operations.
  961. */
  962. void folio_batch_remove_exceptionals(struct folio_batch *fbatch)
  963. {
  964. unsigned int i, j;
  965. for (i = 0, j = 0; i < folio_batch_count(fbatch); i++) {
  966. struct folio *folio = fbatch->folios[i];
  967. if (!xa_is_value(folio))
  968. fbatch->folios[j++] = folio;
  969. }
  970. fbatch->nr = j;
  971. }
  972. unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
  973. struct address_space *mapping, pgoff_t *index, pgoff_t end,
  974. xa_mark_t tag)
  975. {
  976. pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
  977. PAGEVEC_SIZE, pvec->pages);
  978. return pagevec_count(pvec);
  979. }
  980. EXPORT_SYMBOL(pagevec_lookup_range_tag);
  981. /*
  982. * Perform any setup for the swap system
  983. */
  984. void __init swap_setup(void)
  985. {
  986. unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
  987. /* Use a smaller cluster for small-memory machines */
  988. if (megs < 16)
  989. page_cluster = 2;
  990. else
  991. page_cluster = 3;
  992. /*
  993. * Right now other parts of the system means that we
  994. * _really_ don't want to cluster much more
  995. */
  996. }