swap.c 30 KB

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