swap.c 30 KB

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