xref: /openbmc/linux/mm/swap.c (revision 278002edb19bce2c628fafb0af936e77000f3a5b)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/mm/swap.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds /*
9183ff22bSSimon Arlott  * This file contains the default values for the operation of the
101da177e4SLinus Torvalds  * Linux VM subsystem. Fine-tuning documentation can be found in
1157043247SMauro Carvalho Chehab  * Documentation/admin-guide/sysctl/vm.rst.
121da177e4SLinus Torvalds  * Started 18.12.91
131da177e4SLinus Torvalds  * Swap aging added 23.2.95, Stephen Tweedie.
141da177e4SLinus Torvalds  * Buffermem limits added 12.3.98, Rik van Riel.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/mm.h>
181da177e4SLinus Torvalds #include <linux/sched.h>
191da177e4SLinus Torvalds #include <linux/kernel_stat.h>
201da177e4SLinus Torvalds #include <linux/swap.h>
211da177e4SLinus Torvalds #include <linux/mman.h>
221da177e4SLinus Torvalds #include <linux/pagemap.h>
231da177e4SLinus Torvalds #include <linux/pagevec.h>
241da177e4SLinus Torvalds #include <linux/init.h>
25b95f1b31SPaul Gortmaker #include <linux/export.h>
261da177e4SLinus Torvalds #include <linux/mm_inline.h>
271da177e4SLinus Torvalds #include <linux/percpu_counter.h>
283565fce3SDan Williams #include <linux/memremap.h>
291da177e4SLinus Torvalds #include <linux/percpu.h>
301da177e4SLinus Torvalds #include <linux/cpu.h>
311da177e4SLinus Torvalds #include <linux/notifier.h>
32e0bf68ddSPeter Zijlstra #include <linux/backing-dev.h>
3366e1707bSBalbir Singh #include <linux/memcontrol.h>
345a0e3ad6STejun Heo #include <linux/gfp.h>
35a27bb332SKent Overstreet #include <linux/uio.h>
36822fc613SNaoya Horiguchi #include <linux/hugetlb.h>
3733c3fc71SVladimir Davydov #include <linux/page_idle.h>
38b01b2141SIngo Molnar #include <linux/local_lock.h>
398cc621d2SMinchan Kim #include <linux/buffer_head.h>
401da177e4SLinus Torvalds 
4164d6519dSLee Schermerhorn #include "internal.h"
4264d6519dSLee Schermerhorn 
43c6286c98SMel Gorman #define CREATE_TRACE_POINTS
44c6286c98SMel Gorman #include <trace/events/pagemap.h>
45c6286c98SMel Gorman 
46ea0ffd0cSKairui Song /* How many pages do we try to swap or page in/out together? As a power of 2 */
471da177e4SLinus Torvalds int page_cluster;
48ea0ffd0cSKairui Song const int page_cluster_max = 31;
491da177e4SLinus Torvalds 
50c2bc1681SMatthew Wilcox (Oracle) /* Protecting only lru_rotate.fbatch which requires disabling interrupts */
51b01b2141SIngo Molnar struct lru_rotate {
52b01b2141SIngo Molnar 	local_lock_t lock;
53c2bc1681SMatthew Wilcox (Oracle) 	struct folio_batch fbatch;
54b01b2141SIngo Molnar };
55b01b2141SIngo Molnar static DEFINE_PER_CPU(struct lru_rotate, lru_rotate) = {
56b01b2141SIngo Molnar 	.lock = INIT_LOCAL_LOCK(lock),
57b01b2141SIngo Molnar };
58b01b2141SIngo Molnar 
59b01b2141SIngo Molnar /*
6082ac64d8SMatthew Wilcox (Oracle)  * The following folio batches are grouped together because they are protected
61b01b2141SIngo Molnar  * by disabling preemption (and interrupts remain enabled).
62b01b2141SIngo Molnar  */
6382ac64d8SMatthew Wilcox (Oracle) struct cpu_fbatches {
64b01b2141SIngo Molnar 	local_lock_t lock;
6570dea534SMatthew Wilcox (Oracle) 	struct folio_batch lru_add;
667a3dbfe8SMatthew Wilcox (Oracle) 	struct folio_batch lru_deactivate_file;
6785cd7791SMatthew Wilcox (Oracle) 	struct folio_batch lru_deactivate;
68cec394baSMatthew Wilcox (Oracle) 	struct folio_batch lru_lazyfree;
69a4a921aaSMing Li #ifdef CONFIG_SMP
703a44610bSMatthew Wilcox (Oracle) 	struct folio_batch activate;
71a4a921aaSMing Li #endif
72b01b2141SIngo Molnar };
7382ac64d8SMatthew Wilcox (Oracle) static DEFINE_PER_CPU(struct cpu_fbatches, cpu_fbatches) = {
74b01b2141SIngo Molnar 	.lock = INIT_LOCAL_LOCK(lock),
75b01b2141SIngo Molnar };
76902aaed0SHisashi Hifumi 
77b221385bSAdrian Bunk /*
78b109b870SHugh Dickins  * This path almost never happens for VM activity - pages are normally freed
79*1fec6890SMatthew Wilcox (Oracle)  * in batches.  But it gets used by networking - and for compound pages.
80b221385bSAdrian Bunk  */
__page_cache_release(struct folio * folio)81188e8caeSMatthew Wilcox (Oracle) static void __page_cache_release(struct folio *folio)
82b221385bSAdrian Bunk {
83188e8caeSMatthew Wilcox (Oracle) 	if (folio_test_lru(folio)) {
84fa9add64SHugh Dickins 		struct lruvec *lruvec;
85fa9add64SHugh Dickins 		unsigned long flags;
86b221385bSAdrian Bunk 
87e809c3feSMatthew Wilcox (Oracle) 		lruvec = folio_lruvec_lock_irqsave(folio, &flags);
88188e8caeSMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
89188e8caeSMatthew Wilcox (Oracle) 		__folio_clear_lru_flags(folio);
906168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
91b221385bSAdrian Bunk 	}
9291807063SAndrea Arcangeli }
9391807063SAndrea Arcangeli 
__folio_put_small(struct folio * folio)9483d99659SMatthew Wilcox (Oracle) static void __folio_put_small(struct folio *folio)
9591807063SAndrea Arcangeli {
96188e8caeSMatthew Wilcox (Oracle) 	__page_cache_release(folio);
9783d99659SMatthew Wilcox (Oracle) 	mem_cgroup_uncharge(folio);
9883d99659SMatthew Wilcox (Oracle) 	free_unref_page(&folio->page, 0);
99b221385bSAdrian Bunk }
100b221385bSAdrian Bunk 
__folio_put_large(struct folio * folio)1015ef82fe7SMatthew Wilcox (Oracle) static void __folio_put_large(struct folio *folio)
10291807063SAndrea Arcangeli {
103822fc613SNaoya Horiguchi 	/*
104822fc613SNaoya Horiguchi 	 * __page_cache_release() is supposed to be called for thp, not for
105822fc613SNaoya Horiguchi 	 * hugetlb. This is because hugetlb page does never have PageLRU set
106822fc613SNaoya Horiguchi 	 * (it's never listed to any LRU lists) and no memcg routines should
107822fc613SNaoya Horiguchi 	 * be called for hugetlb (it has a separate hugetlb_cgroup.)
108822fc613SNaoya Horiguchi 	 */
1095ef82fe7SMatthew Wilcox (Oracle) 	if (!folio_test_hugetlb(folio))
110188e8caeSMatthew Wilcox (Oracle) 		__page_cache_release(folio);
1115375336cSMatthew Wilcox (Oracle) 	destroy_large_folio(folio);
11291807063SAndrea Arcangeli }
11391807063SAndrea Arcangeli 
__folio_put(struct folio * folio)1148d29c703SMatthew Wilcox (Oracle) void __folio_put(struct folio *folio)
115c747ce79SJianyu Zhan {
1168d29c703SMatthew Wilcox (Oracle) 	if (unlikely(folio_is_zone_device(folio)))
1178d29c703SMatthew Wilcox (Oracle) 		free_zone_device_page(&folio->page);
1188d29c703SMatthew Wilcox (Oracle) 	else if (unlikely(folio_test_large(folio)))
1195ef82fe7SMatthew Wilcox (Oracle) 		__folio_put_large(folio);
12026296ad2SAndrew Morton 	else
12183d99659SMatthew Wilcox (Oracle) 		__folio_put_small(folio);
12226296ad2SAndrew Morton }
1238d29c703SMatthew Wilcox (Oracle) EXPORT_SYMBOL(__folio_put);
12470b50f94SAndrea Arcangeli 
1251d7ea732SAlexander Zarochentsev /**
1267682486bSRandy Dunlap  * put_pages_list() - release a list of pages
1277682486bSRandy Dunlap  * @pages: list of pages threaded on page->lru
1281d7ea732SAlexander Zarochentsev  *
129988c69f1SMatthew Wilcox (Oracle)  * Release a list of pages which are strung together on page.lru.
1301d7ea732SAlexander Zarochentsev  */
put_pages_list(struct list_head * pages)1311d7ea732SAlexander Zarochentsev void put_pages_list(struct list_head *pages)
1321d7ea732SAlexander Zarochentsev {
1332f58e5deSMatthew Wilcox (Oracle) 	struct folio *folio, *next;
1341d7ea732SAlexander Zarochentsev 
1352f58e5deSMatthew Wilcox (Oracle) 	list_for_each_entry_safe(folio, next, pages, lru) {
1362f58e5deSMatthew Wilcox (Oracle) 		if (!folio_put_testzero(folio)) {
1372f58e5deSMatthew Wilcox (Oracle) 			list_del(&folio->lru);
138988c69f1SMatthew Wilcox (Oracle) 			continue;
1391d7ea732SAlexander Zarochentsev 		}
1402f58e5deSMatthew Wilcox (Oracle) 		if (folio_test_large(folio)) {
1412f58e5deSMatthew Wilcox (Oracle) 			list_del(&folio->lru);
1425ef82fe7SMatthew Wilcox (Oracle) 			__folio_put_large(folio);
143988c69f1SMatthew Wilcox (Oracle) 			continue;
144988c69f1SMatthew Wilcox (Oracle) 		}
1452f58e5deSMatthew Wilcox (Oracle) 		/* LRU flag must be clear because it's passed using the lru */
146988c69f1SMatthew Wilcox (Oracle) 	}
147988c69f1SMatthew Wilcox (Oracle) 
148988c69f1SMatthew Wilcox (Oracle) 	free_unref_page_list(pages);
1493cd018b4SMatthew Wilcox 	INIT_LIST_HEAD(pages);
1501d7ea732SAlexander Zarochentsev }
1511d7ea732SAlexander Zarochentsev EXPORT_SYMBOL(put_pages_list);
1521d7ea732SAlexander Zarochentsev 
153c2bc1681SMatthew Wilcox (Oracle) typedef void (*move_fn_t)(struct lruvec *lruvec, struct folio *folio);
154c2bc1681SMatthew Wilcox (Oracle) 
lru_add_fn(struct lruvec * lruvec,struct folio * folio)15570dea534SMatthew Wilcox (Oracle) static void lru_add_fn(struct lruvec *lruvec, struct folio *folio)
1567d80dd09SMatthew Wilcox (Oracle) {
1577d80dd09SMatthew Wilcox (Oracle) 	int was_unevictable = folio_test_clear_unevictable(folio);
1587d80dd09SMatthew Wilcox (Oracle) 	long nr_pages = folio_nr_pages(folio);
1597d80dd09SMatthew Wilcox (Oracle) 
1607d80dd09SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
1617d80dd09SMatthew Wilcox (Oracle) 
1627d80dd09SMatthew Wilcox (Oracle) 	/*
1637d80dd09SMatthew Wilcox (Oracle) 	 * Is an smp_mb__after_atomic() still required here, before
164188e8caeSMatthew Wilcox (Oracle) 	 * folio_evictable() tests the mlocked flag, to rule out the possibility
1657d80dd09SMatthew Wilcox (Oracle) 	 * of stranding an evictable folio on an unevictable LRU?  I think
166e0650a41SMatthew Wilcox (Oracle) 	 * not, because __munlock_folio() only clears the mlocked flag
167188e8caeSMatthew Wilcox (Oracle) 	 * while the LRU lock is held.
1687d80dd09SMatthew Wilcox (Oracle) 	 *
1697d80dd09SMatthew Wilcox (Oracle) 	 * (That is not true of __page_cache_release(), and not necessarily
170188e8caeSMatthew Wilcox (Oracle) 	 * true of release_pages(): but those only clear the mlocked flag after
171188e8caeSMatthew Wilcox (Oracle) 	 * folio_put_testzero() has excluded any other users of the folio.)
1727d80dd09SMatthew Wilcox (Oracle) 	 */
1737d80dd09SMatthew Wilcox (Oracle) 	if (folio_evictable(folio)) {
1747d80dd09SMatthew Wilcox (Oracle) 		if (was_unevictable)
1757d80dd09SMatthew Wilcox (Oracle) 			__count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages);
1767d80dd09SMatthew Wilcox (Oracle) 	} else {
1777d80dd09SMatthew Wilcox (Oracle) 		folio_clear_active(folio);
1787d80dd09SMatthew Wilcox (Oracle) 		folio_set_unevictable(folio);
1797d80dd09SMatthew Wilcox (Oracle) 		/*
1807d80dd09SMatthew Wilcox (Oracle) 		 * folio->mlock_count = !!folio_test_mlocked(folio)?
181e0650a41SMatthew Wilcox (Oracle) 		 * But that leaves __mlock_folio() in doubt whether another
1827d80dd09SMatthew Wilcox (Oracle) 		 * actor has already counted the mlock or not.  Err on the
1837d80dd09SMatthew Wilcox (Oracle) 		 * safe side, underestimate, let page reclaim fix it, rather
1847d80dd09SMatthew Wilcox (Oracle) 		 * than leaving a page on the unevictable LRU indefinitely.
1857d80dd09SMatthew Wilcox (Oracle) 		 */
1867d80dd09SMatthew Wilcox (Oracle) 		folio->mlock_count = 0;
1877d80dd09SMatthew Wilcox (Oracle) 		if (!was_unevictable)
1887d80dd09SMatthew Wilcox (Oracle) 			__count_vm_events(UNEVICTABLE_PGCULLED, nr_pages);
1897d80dd09SMatthew Wilcox (Oracle) 	}
1907d80dd09SMatthew Wilcox (Oracle) 
1917d80dd09SMatthew Wilcox (Oracle) 	lruvec_add_folio(lruvec, folio);
1927d80dd09SMatthew Wilcox (Oracle) 	trace_mm_lru_insertion(folio);
1937d80dd09SMatthew Wilcox (Oracle) }
1947d80dd09SMatthew Wilcox (Oracle) 
folio_batch_move_lru(struct folio_batch * fbatch,move_fn_t move_fn)195c2bc1681SMatthew Wilcox (Oracle) static void folio_batch_move_lru(struct folio_batch *fbatch, move_fn_t move_fn)
196902aaed0SHisashi Hifumi {
197902aaed0SHisashi Hifumi 	int i;
1986168d0daSAlex Shi 	struct lruvec *lruvec = NULL;
1993dd7ae8eSShaohua Li 	unsigned long flags = 0;
200902aaed0SHisashi Hifumi 
201c2bc1681SMatthew Wilcox (Oracle) 	for (i = 0; i < folio_batch_count(fbatch); i++) {
202c2bc1681SMatthew Wilcox (Oracle) 		struct folio *folio = fbatch->folios[i];
2033dd7ae8eSShaohua Li 
204c2bc1681SMatthew Wilcox (Oracle) 		/* block memcg migration while the folio moves between lru */
20570dea534SMatthew Wilcox (Oracle) 		if (move_fn != lru_add_fn && !folio_test_clear_lru(folio))
206fc574c23SAlex Shi 			continue;
207fc574c23SAlex Shi 
2080de340cbSMatthew Wilcox (Oracle) 		lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags);
209c2bc1681SMatthew Wilcox (Oracle) 		move_fn(lruvec, folio);
210fc574c23SAlex Shi 
211c2bc1681SMatthew Wilcox (Oracle) 		folio_set_lru(folio);
2123dd7ae8eSShaohua Li 	}
213c2bc1681SMatthew Wilcox (Oracle) 
2146168d0daSAlex Shi 	if (lruvec)
2156168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
216c2bc1681SMatthew Wilcox (Oracle) 	folios_put(fbatch->folios, folio_batch_count(fbatch));
217998ad18bSQi Zheng 	folio_batch_reinit(fbatch);
2183dd7ae8eSShaohua Li }
2193dd7ae8eSShaohua Li 
folio_batch_add_and_move(struct folio_batch * fbatch,struct folio * folio,move_fn_t move_fn)220c2bc1681SMatthew Wilcox (Oracle) static void folio_batch_add_and_move(struct folio_batch *fbatch,
221c2bc1681SMatthew Wilcox (Oracle) 		struct folio *folio, move_fn_t move_fn)
2223dd7ae8eSShaohua Li {
223c2bc1681SMatthew Wilcox (Oracle) 	if (folio_batch_add(fbatch, folio) && !folio_test_large(folio) &&
224c2bc1681SMatthew Wilcox (Oracle) 	    !lru_cache_disabled())
225c2bc1681SMatthew Wilcox (Oracle) 		return;
226c2bc1681SMatthew Wilcox (Oracle) 	folio_batch_move_lru(fbatch, move_fn);
227c2bc1681SMatthew Wilcox (Oracle) }
228575ced1cSMatthew Wilcox (Oracle) 
lru_move_tail_fn(struct lruvec * lruvec,struct folio * folio)229c2bc1681SMatthew Wilcox (Oracle) static void lru_move_tail_fn(struct lruvec *lruvec, struct folio *folio)
230c2bc1681SMatthew Wilcox (Oracle) {
231575ced1cSMatthew Wilcox (Oracle) 	if (!folio_test_unevictable(folio)) {
232575ced1cSMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
233575ced1cSMatthew Wilcox (Oracle) 		folio_clear_active(folio);
234575ced1cSMatthew Wilcox (Oracle) 		lruvec_add_folio_tail(lruvec, folio);
235575ced1cSMatthew Wilcox (Oracle) 		__count_vm_events(PGROTATED, folio_nr_pages(folio));
236902aaed0SHisashi Hifumi 	}
237902aaed0SHisashi Hifumi }
2383dd7ae8eSShaohua Li 
2393dd7ae8eSShaohua Li /*
240575ced1cSMatthew Wilcox (Oracle)  * Writeback is about to end against a folio which has been marked for
241575ced1cSMatthew Wilcox (Oracle)  * immediate reclaim.  If it still appears to be reclaimable, move it
242575ced1cSMatthew Wilcox (Oracle)  * to the tail of the inactive list.
243c7c7b80cSAlex Shi  *
244575ced1cSMatthew Wilcox (Oracle)  * folio_rotate_reclaimable() must disable IRQs, to prevent nasty races.
2451da177e4SLinus Torvalds  */
folio_rotate_reclaimable(struct folio * folio)246575ced1cSMatthew Wilcox (Oracle) void folio_rotate_reclaimable(struct folio *folio)
2471da177e4SLinus Torvalds {
248575ced1cSMatthew Wilcox (Oracle) 	if (!folio_test_locked(folio) && !folio_test_dirty(folio) &&
249575ced1cSMatthew Wilcox (Oracle) 	    !folio_test_unevictable(folio) && folio_test_lru(folio)) {
250c2bc1681SMatthew Wilcox (Oracle) 		struct folio_batch *fbatch;
2511da177e4SLinus Torvalds 		unsigned long flags;
2521da177e4SLinus Torvalds 
253575ced1cSMatthew Wilcox (Oracle) 		folio_get(folio);
254b01b2141SIngo Molnar 		local_lock_irqsave(&lru_rotate.lock, flags);
255c2bc1681SMatthew Wilcox (Oracle) 		fbatch = this_cpu_ptr(&lru_rotate.fbatch);
256c2bc1681SMatthew Wilcox (Oracle) 		folio_batch_add_and_move(fbatch, folio, lru_move_tail_fn);
257b01b2141SIngo Molnar 		local_unlock_irqrestore(&lru_rotate.lock, flags);
258ac6aadb2SMiklos Szeredi 	}
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
lru_note_cost(struct lruvec * lruvec,bool file,unsigned int nr_io,unsigned int nr_rotated)2610538a82cSJohannes Weiner void lru_note_cost(struct lruvec *lruvec, bool file,
2620538a82cSJohannes Weiner 		   unsigned int nr_io, unsigned int nr_rotated)
2633e2f41f1SKOSAKI Motohiro {
2640538a82cSJohannes Weiner 	unsigned long cost;
2650538a82cSJohannes Weiner 
2660538a82cSJohannes Weiner 	/*
2670538a82cSJohannes Weiner 	 * Reflect the relative cost of incurring IO and spending CPU
2680538a82cSJohannes Weiner 	 * time on rotations. This doesn't attempt to make a precise
2690538a82cSJohannes Weiner 	 * comparison, it just says: if reloads are about comparable
2700538a82cSJohannes Weiner 	 * between the LRU lists, or rotations are overwhelmingly
2710538a82cSJohannes Weiner 	 * different between them, adjust scan balance for CPU work.
2720538a82cSJohannes Weiner 	 */
2730538a82cSJohannes Weiner 	cost = nr_io * SWAP_CLUSTER_MAX + nr_rotated;
2740538a82cSJohannes Weiner 
2757cf111bcSJohannes Weiner 	do {
2767cf111bcSJohannes Weiner 		unsigned long lrusize;
2777cf111bcSJohannes Weiner 
2786168d0daSAlex Shi 		/*
2796168d0daSAlex Shi 		 * Hold lruvec->lru_lock is safe here, since
2806168d0daSAlex Shi 		 * 1) The pinned lruvec in reclaim, or
2816168d0daSAlex Shi 		 * 2) From a pre-LRU page during refault (which also holds the
2826168d0daSAlex Shi 		 *    rcu lock, so would be safe even if the page was on the LRU
2836168d0daSAlex Shi 		 *    and could move simultaneously to a new lruvec).
2846168d0daSAlex Shi 		 */
2856168d0daSAlex Shi 		spin_lock_irq(&lruvec->lru_lock);
2867cf111bcSJohannes Weiner 		/* Record cost event */
28796f8bf4fSJohannes Weiner 		if (file)
2880538a82cSJohannes Weiner 			lruvec->file_cost += cost;
2891431d4d1SJohannes Weiner 		else
2900538a82cSJohannes Weiner 			lruvec->anon_cost += cost;
2917cf111bcSJohannes Weiner 
2927cf111bcSJohannes Weiner 		/*
2937cf111bcSJohannes Weiner 		 * Decay previous events
2947cf111bcSJohannes Weiner 		 *
2957cf111bcSJohannes Weiner 		 * Because workloads change over time (and to avoid
2967cf111bcSJohannes Weiner 		 * overflow) we keep these statistics as a floating
2977cf111bcSJohannes Weiner 		 * average, which ends up weighing recent refaults
2987cf111bcSJohannes Weiner 		 * more than old ones.
2997cf111bcSJohannes Weiner 		 */
3007cf111bcSJohannes Weiner 		lrusize = lruvec_page_state(lruvec, NR_INACTIVE_ANON) +
3017cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_ACTIVE_ANON) +
3027cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_INACTIVE_FILE) +
3037cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_ACTIVE_FILE);
3047cf111bcSJohannes Weiner 
3057cf111bcSJohannes Weiner 		if (lruvec->file_cost + lruvec->anon_cost > lrusize / 4) {
3067cf111bcSJohannes Weiner 			lruvec->file_cost /= 2;
3077cf111bcSJohannes Weiner 			lruvec->anon_cost /= 2;
3087cf111bcSJohannes Weiner 		}
3096168d0daSAlex Shi 		spin_unlock_irq(&lruvec->lru_lock);
3107cf111bcSJohannes Weiner 	} while ((lruvec = parent_lruvec(lruvec)));
3113e2f41f1SKOSAKI Motohiro }
3123e2f41f1SKOSAKI Motohiro 
lru_note_cost_refault(struct folio * folio)3130538a82cSJohannes Weiner void lru_note_cost_refault(struct folio *folio)
31496f8bf4fSJohannes Weiner {
3150995d7e5SMatthew Wilcox (Oracle) 	lru_note_cost(folio_lruvec(folio), folio_is_file_lru(folio),
3160538a82cSJohannes Weiner 		      folio_nr_pages(folio), 0);
31796f8bf4fSJohannes Weiner }
31896f8bf4fSJohannes Weiner 
folio_activate_fn(struct lruvec * lruvec,struct folio * folio)3193a44610bSMatthew Wilcox (Oracle) static void folio_activate_fn(struct lruvec *lruvec, struct folio *folio)
320744ed144SShaohua Li {
321f2d27392SMatthew Wilcox (Oracle) 	if (!folio_test_active(folio) && !folio_test_unevictable(folio)) {
322f2d27392SMatthew Wilcox (Oracle) 		long nr_pages = folio_nr_pages(folio);
323744ed144SShaohua Li 
324f2d27392SMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
325f2d27392SMatthew Wilcox (Oracle) 		folio_set_active(folio);
326f2d27392SMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
327f2d27392SMatthew Wilcox (Oracle) 		trace_mm_lru_activate(folio);
3287a608572SLinus Torvalds 
32921e330fcSShakeel Butt 		__count_vm_events(PGACTIVATE, nr_pages);
33021e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGACTIVATE,
33121e330fcSShakeel Butt 				     nr_pages);
332744ed144SShaohua Li 	}
333eb709b0dSShaohua Li }
334eb709b0dSShaohua Li 
335eb709b0dSShaohua Li #ifdef CONFIG_SMP
folio_activate_drain(int cpu)3363a44610bSMatthew Wilcox (Oracle) static void folio_activate_drain(int cpu)
337f2d27392SMatthew Wilcox (Oracle) {
33882ac64d8SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch = &per_cpu(cpu_fbatches.activate, cpu);
339f2d27392SMatthew Wilcox (Oracle) 
3403a44610bSMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
3413a44610bSMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, folio_activate_fn);
3425fbc4616SChris Metcalf }
3435fbc4616SChris Metcalf 
folio_activate(struct folio * folio)344018ee47fSYu Zhao void folio_activate(struct folio *folio)
345eb709b0dSShaohua Li {
346f2d27392SMatthew Wilcox (Oracle) 	if (folio_test_lru(folio) && !folio_test_active(folio) &&
347f2d27392SMatthew Wilcox (Oracle) 	    !folio_test_unevictable(folio)) {
3483a44610bSMatthew Wilcox (Oracle) 		struct folio_batch *fbatch;
349eb709b0dSShaohua Li 
350f2d27392SMatthew Wilcox (Oracle) 		folio_get(folio);
35182ac64d8SMatthew Wilcox (Oracle) 		local_lock(&cpu_fbatches.lock);
35282ac64d8SMatthew Wilcox (Oracle) 		fbatch = this_cpu_ptr(&cpu_fbatches.activate);
3533a44610bSMatthew Wilcox (Oracle) 		folio_batch_add_and_move(fbatch, folio, folio_activate_fn);
35482ac64d8SMatthew Wilcox (Oracle) 		local_unlock(&cpu_fbatches.lock);
355eb709b0dSShaohua Li 	}
356eb709b0dSShaohua Li }
357eb709b0dSShaohua Li 
358eb709b0dSShaohua Li #else
folio_activate_drain(int cpu)3593a44610bSMatthew Wilcox (Oracle) static inline void folio_activate_drain(int cpu)
360eb709b0dSShaohua Li {
361eb709b0dSShaohua Li }
362eb709b0dSShaohua Li 
folio_activate(struct folio * folio)363018ee47fSYu Zhao void folio_activate(struct folio *folio)
364eb709b0dSShaohua Li {
3656168d0daSAlex Shi 	struct lruvec *lruvec;
366eb709b0dSShaohua Li 
367f2d27392SMatthew Wilcox (Oracle) 	if (folio_test_clear_lru(folio)) {
368e809c3feSMatthew Wilcox (Oracle) 		lruvec = folio_lruvec_lock_irq(folio);
3693a44610bSMatthew Wilcox (Oracle) 		folio_activate_fn(lruvec, folio);
3706168d0daSAlex Shi 		unlock_page_lruvec_irq(lruvec);
371f2d27392SMatthew Wilcox (Oracle) 		folio_set_lru(folio);
3726168d0daSAlex Shi 	}
3731da177e4SLinus Torvalds }
374eb709b0dSShaohua Li #endif
3751da177e4SLinus Torvalds 
__lru_cache_activate_folio(struct folio * folio)37676580b65SMatthew Wilcox (Oracle) static void __lru_cache_activate_folio(struct folio *folio)
377059285a2SMel Gorman {
37870dea534SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch;
379059285a2SMel Gorman 	int i;
380059285a2SMel Gorman 
38182ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
38282ac64d8SMatthew Wilcox (Oracle) 	fbatch = this_cpu_ptr(&cpu_fbatches.lru_add);
383b01b2141SIngo Molnar 
384059285a2SMel Gorman 	/*
38570dea534SMatthew Wilcox (Oracle) 	 * Search backwards on the optimistic assumption that the folio being
38670dea534SMatthew Wilcox (Oracle) 	 * activated has just been added to this batch. Note that only
38770dea534SMatthew Wilcox (Oracle) 	 * the local batch is examined as a !LRU folio could be in the
388059285a2SMel Gorman 	 * process of being released, reclaimed, migrated or on a remote
38970dea534SMatthew Wilcox (Oracle) 	 * batch that is currently being drained. Furthermore, marking
39070dea534SMatthew Wilcox (Oracle) 	 * a remote batch's folio active potentially hits a race where
39170dea534SMatthew Wilcox (Oracle) 	 * a folio is marked active just after it is added to the inactive
392059285a2SMel Gorman 	 * list causing accounting errors and BUG_ON checks to trigger.
393059285a2SMel Gorman 	 */
39470dea534SMatthew Wilcox (Oracle) 	for (i = folio_batch_count(fbatch) - 1; i >= 0; i--) {
39570dea534SMatthew Wilcox (Oracle) 		struct folio *batch_folio = fbatch->folios[i];
396059285a2SMel Gorman 
39770dea534SMatthew Wilcox (Oracle) 		if (batch_folio == folio) {
39876580b65SMatthew Wilcox (Oracle) 			folio_set_active(folio);
399059285a2SMel Gorman 			break;
400059285a2SMel Gorman 		}
401059285a2SMel Gorman 	}
402059285a2SMel Gorman 
40382ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
404059285a2SMel Gorman }
405059285a2SMel Gorman 
406ac35a490SYu Zhao #ifdef CONFIG_LRU_GEN
folio_inc_refs(struct folio * folio)407ac35a490SYu Zhao static void folio_inc_refs(struct folio *folio)
408ac35a490SYu Zhao {
409ac35a490SYu Zhao 	unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
410ac35a490SYu Zhao 
411ac35a490SYu Zhao 	if (folio_test_unevictable(folio))
412ac35a490SYu Zhao 		return;
413ac35a490SYu Zhao 
414ac35a490SYu Zhao 	if (!folio_test_referenced(folio)) {
415ac35a490SYu Zhao 		folio_set_referenced(folio);
416ac35a490SYu Zhao 		return;
417ac35a490SYu Zhao 	}
418ac35a490SYu Zhao 
419ac35a490SYu Zhao 	if (!folio_test_workingset(folio)) {
420ac35a490SYu Zhao 		folio_set_workingset(folio);
421ac35a490SYu Zhao 		return;
422ac35a490SYu Zhao 	}
423ac35a490SYu Zhao 
424ac35a490SYu Zhao 	/* see the comment on MAX_NR_TIERS */
425ac35a490SYu Zhao 	do {
426ac35a490SYu Zhao 		new_flags = old_flags & LRU_REFS_MASK;
427ac35a490SYu Zhao 		if (new_flags == LRU_REFS_MASK)
428ac35a490SYu Zhao 			break;
429ac35a490SYu Zhao 
430ac35a490SYu Zhao 		new_flags += BIT(LRU_REFS_PGOFF);
431ac35a490SYu Zhao 		new_flags |= old_flags & ~LRU_REFS_MASK;
432ac35a490SYu Zhao 	} while (!try_cmpxchg(&folio->flags, &old_flags, new_flags));
433ac35a490SYu Zhao }
434ac35a490SYu Zhao #else
folio_inc_refs(struct folio * folio)435ac35a490SYu Zhao static void folio_inc_refs(struct folio *folio)
436ac35a490SYu Zhao {
437ac35a490SYu Zhao }
438ac35a490SYu Zhao #endif /* CONFIG_LRU_GEN */
439ac35a490SYu Zhao 
4401da177e4SLinus Torvalds /*
4411da177e4SLinus Torvalds  * Mark a page as having seen activity.
4421da177e4SLinus Torvalds  *
4431da177e4SLinus Torvalds  * inactive,unreferenced	->	inactive,referenced
4441da177e4SLinus Torvalds  * inactive,referenced		->	active,unreferenced
4451da177e4SLinus Torvalds  * active,unreferenced		->	active,referenced
446eb39d618SHugh Dickins  *
447eb39d618SHugh Dickins  * When a newly allocated page is not yet visible, so safe for non-atomic ops,
448eb39d618SHugh Dickins  * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
4491da177e4SLinus Torvalds  */
folio_mark_accessed(struct folio * folio)45076580b65SMatthew Wilcox (Oracle) void folio_mark_accessed(struct folio *folio)
4511da177e4SLinus Torvalds {
452ac35a490SYu Zhao 	if (lru_gen_enabled()) {
453ac35a490SYu Zhao 		folio_inc_refs(folio);
454ac35a490SYu Zhao 		return;
455ac35a490SYu Zhao 	}
456ac35a490SYu Zhao 
45776580b65SMatthew Wilcox (Oracle) 	if (!folio_test_referenced(folio)) {
45876580b65SMatthew Wilcox (Oracle) 		folio_set_referenced(folio);
45976580b65SMatthew Wilcox (Oracle) 	} else if (folio_test_unevictable(folio)) {
460a1100a74SFengguang Wu 		/*
461a1100a74SFengguang Wu 		 * Unevictable pages are on the "LRU_UNEVICTABLE" list. But,
462a1100a74SFengguang Wu 		 * this list is never rotated or maintained, so marking an
463914c32e4SBang Li 		 * unevictable page accessed has no effect.
464a1100a74SFengguang Wu 		 */
46576580b65SMatthew Wilcox (Oracle) 	} else if (!folio_test_active(folio)) {
466059285a2SMel Gorman 		/*
4673a44610bSMatthew Wilcox (Oracle) 		 * If the folio is on the LRU, queue it for activation via
46882ac64d8SMatthew Wilcox (Oracle) 		 * cpu_fbatches.activate. Otherwise, assume the folio is in a
4693a44610bSMatthew Wilcox (Oracle) 		 * folio_batch, mark it active and it'll be moved to the active
470059285a2SMel Gorman 		 * LRU on the next drain.
471059285a2SMel Gorman 		 */
47276580b65SMatthew Wilcox (Oracle) 		if (folio_test_lru(folio))
47376580b65SMatthew Wilcox (Oracle) 			folio_activate(folio);
474059285a2SMel Gorman 		else
47576580b65SMatthew Wilcox (Oracle) 			__lru_cache_activate_folio(folio);
47676580b65SMatthew Wilcox (Oracle) 		folio_clear_referenced(folio);
47776580b65SMatthew Wilcox (Oracle) 		workingset_activation(folio);
4781da177e4SLinus Torvalds 	}
47976580b65SMatthew Wilcox (Oracle) 	if (folio_test_idle(folio))
48076580b65SMatthew Wilcox (Oracle) 		folio_clear_idle(folio);
4811da177e4SLinus Torvalds }
48276580b65SMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_mark_accessed);
4831da177e4SLinus Torvalds 
484f04e9ebbSKOSAKI Motohiro /**
4850d31125dSMatthew Wilcox (Oracle)  * folio_add_lru - Add a folio to an LRU list.
4860d31125dSMatthew Wilcox (Oracle)  * @folio: The folio to be added to the LRU.
4872329d375SJianyu Zhan  *
4880d31125dSMatthew Wilcox (Oracle)  * Queue the folio for addition to the LRU. The decision on whether
4892329d375SJianyu Zhan  * to add the page to the [in]active [file|anon] list is deferred until the
49082ac64d8SMatthew Wilcox (Oracle)  * folio_batch is drained. This gives a chance for the caller of folio_add_lru()
4910d31125dSMatthew Wilcox (Oracle)  * have the folio added to the active list using folio_mark_accessed().
492f04e9ebbSKOSAKI Motohiro  */
folio_add_lru(struct folio * folio)4930d31125dSMatthew Wilcox (Oracle) void folio_add_lru(struct folio *folio)
4941da177e4SLinus Torvalds {
49570dea534SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch;
4966058eaecSJohannes Weiner 
49770dea534SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_active(folio) &&
49870dea534SMatthew Wilcox (Oracle) 			folio_test_unevictable(folio), folio);
4990d31125dSMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
5006058eaecSJohannes Weiner 
501ec1c86b2SYu Zhao 	/* see the comment in lru_gen_add_folio() */
502ec1c86b2SYu Zhao 	if (lru_gen_enabled() && !folio_test_unevictable(folio) &&
503ec1c86b2SYu Zhao 	    lru_gen_in_fault() && !(current->flags & PF_MEMALLOC))
504ec1c86b2SYu Zhao 		folio_set_active(folio);
505ec1c86b2SYu Zhao 
5060d31125dSMatthew Wilcox (Oracle) 	folio_get(folio);
50782ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
50882ac64d8SMatthew Wilcox (Oracle) 	fbatch = this_cpu_ptr(&cpu_fbatches.lru_add);
50970dea534SMatthew Wilcox (Oracle) 	folio_batch_add_and_move(fbatch, folio, lru_add_fn);
51082ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
5111da177e4SLinus Torvalds }
5120d31125dSMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_add_lru);
5131da177e4SLinus Torvalds 
514894bc310SLee Schermerhorn /**
515681ecf63SMatthew Wilcox (Oracle)  * folio_add_lru_vma() - Add a folio to the appropate LRU list for this VMA.
516681ecf63SMatthew Wilcox (Oracle)  * @folio: The folio to be added to the LRU.
517681ecf63SMatthew Wilcox (Oracle)  * @vma: VMA in which the folio is mapped.
51800501b53SJohannes Weiner  *
519681ecf63SMatthew Wilcox (Oracle)  * If the VMA is mlocked, @folio is added to the unevictable list.
520681ecf63SMatthew Wilcox (Oracle)  * Otherwise, it is treated the same way as folio_add_lru().
52100501b53SJohannes Weiner  */
folio_add_lru_vma(struct folio * folio,struct vm_area_struct * vma)522681ecf63SMatthew Wilcox (Oracle) void folio_add_lru_vma(struct folio *folio, struct vm_area_struct *vma)
52300501b53SJohannes Weiner {
524681ecf63SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
52500501b53SJohannes Weiner 
5262fbb0c10SHugh Dickins 	if (unlikely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED))
52796f97c43SLorenzo Stoakes 		mlock_new_folio(folio);
5282fbb0c10SHugh Dickins 	else
529681ecf63SMatthew Wilcox (Oracle) 		folio_add_lru(folio);
53000501b53SJohannes Weiner }
53100501b53SJohannes Weiner 
532902aaed0SHisashi Hifumi /*
5337a3dbfe8SMatthew Wilcox (Oracle)  * If the folio cannot be invalidated, it is moved to the
53431560180SMinchan Kim  * inactive list to speed up its reclaim.  It is moved to the
53531560180SMinchan Kim  * head of the list, rather than the tail, to give the flusher
53631560180SMinchan Kim  * threads some time to write it out, as this is much more
53731560180SMinchan Kim  * effective than the single-page writeout from reclaim.
538278df9f4SMinchan Kim  *
5397a3dbfe8SMatthew Wilcox (Oracle)  * If the folio isn't mapped and dirty/writeback, the folio
5407a3dbfe8SMatthew Wilcox (Oracle)  * could be reclaimed asap using the reclaim flag.
541278df9f4SMinchan Kim  *
5427a3dbfe8SMatthew Wilcox (Oracle)  * 1. active, mapped folio -> none
5437a3dbfe8SMatthew Wilcox (Oracle)  * 2. active, dirty/writeback folio -> inactive, head, reclaim
5447a3dbfe8SMatthew Wilcox (Oracle)  * 3. inactive, mapped folio -> none
5457a3dbfe8SMatthew Wilcox (Oracle)  * 4. inactive, dirty/writeback folio -> inactive, head, reclaim
546278df9f4SMinchan Kim  * 5. inactive, clean -> inactive, tail
547278df9f4SMinchan Kim  * 6. Others -> none
548278df9f4SMinchan Kim  *
5497a3dbfe8SMatthew Wilcox (Oracle)  * In 4, it moves to the head of the inactive list so the folio is
5507a3dbfe8SMatthew Wilcox (Oracle)  * written out by flusher threads as this is much more efficient
551278df9f4SMinchan Kim  * than the single-page writeout from reclaim.
55231560180SMinchan Kim  */
lru_deactivate_file_fn(struct lruvec * lruvec,struct folio * folio)5537a3dbfe8SMatthew Wilcox (Oracle) static void lru_deactivate_file_fn(struct lruvec *lruvec, struct folio *folio)
55431560180SMinchan Kim {
5557a3dbfe8SMatthew Wilcox (Oracle) 	bool active = folio_test_active(folio);
5567a3dbfe8SMatthew Wilcox (Oracle) 	long nr_pages = folio_nr_pages(folio);
55731560180SMinchan Kim 
5587a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_test_unevictable(folio))
559bad49d9cSMinchan Kim 		return;
560bad49d9cSMinchan Kim 
5617a3dbfe8SMatthew Wilcox (Oracle) 	/* Some processes are using the folio */
5627a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_mapped(folio))
56331560180SMinchan Kim 		return;
56431560180SMinchan Kim 
5657a3dbfe8SMatthew Wilcox (Oracle) 	lruvec_del_folio(lruvec, folio);
5667a3dbfe8SMatthew Wilcox (Oracle) 	folio_clear_active(folio);
5677a3dbfe8SMatthew Wilcox (Oracle) 	folio_clear_referenced(folio);
56831560180SMinchan Kim 
5697a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_test_writeback(folio) || folio_test_dirty(folio)) {
570278df9f4SMinchan Kim 		/*
5717a3dbfe8SMatthew Wilcox (Oracle) 		 * Setting the reclaim flag could race with
5727a3dbfe8SMatthew Wilcox (Oracle) 		 * folio_end_writeback() and confuse readahead.  But the
5737a3dbfe8SMatthew Wilcox (Oracle) 		 * race window is _really_ small and  it's not a critical
5747a3dbfe8SMatthew Wilcox (Oracle) 		 * problem.
575278df9f4SMinchan Kim 		 */
5767a3dbfe8SMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
5777a3dbfe8SMatthew Wilcox (Oracle) 		folio_set_reclaim(folio);
578278df9f4SMinchan Kim 	} else {
579278df9f4SMinchan Kim 		/*
5807a3dbfe8SMatthew Wilcox (Oracle) 		 * The folio's writeback ended while it was in the batch.
5817a3dbfe8SMatthew Wilcox (Oracle) 		 * We move that folio to the tail of the inactive list.
582278df9f4SMinchan Kim 		 */
5837a3dbfe8SMatthew Wilcox (Oracle) 		lruvec_add_folio_tail(lruvec, folio);
5845d91f31fSShakeel Butt 		__count_vm_events(PGROTATED, nr_pages);
585278df9f4SMinchan Kim 	}
586278df9f4SMinchan Kim 
58721e330fcSShakeel Butt 	if (active) {
5885d91f31fSShakeel Butt 		__count_vm_events(PGDEACTIVATE, nr_pages);
58921e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
59021e330fcSShakeel Butt 				     nr_pages);
59121e330fcSShakeel Butt 	}
59231560180SMinchan Kim }
59331560180SMinchan Kim 
lru_deactivate_fn(struct lruvec * lruvec,struct folio * folio)59485cd7791SMatthew Wilcox (Oracle) static void lru_deactivate_fn(struct lruvec *lruvec, struct folio *folio)
5959c276cc6SMinchan Kim {
596ec1c86b2SYu Zhao 	if (!folio_test_unevictable(folio) && (folio_test_active(folio) || lru_gen_enabled())) {
59785cd7791SMatthew Wilcox (Oracle) 		long nr_pages = folio_nr_pages(folio);
5989c276cc6SMinchan Kim 
59985cd7791SMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
60085cd7791SMatthew Wilcox (Oracle) 		folio_clear_active(folio);
60185cd7791SMatthew Wilcox (Oracle) 		folio_clear_referenced(folio);
60285cd7791SMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
6039c276cc6SMinchan Kim 
60421e330fcSShakeel Butt 		__count_vm_events(PGDEACTIVATE, nr_pages);
60521e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
60621e330fcSShakeel Butt 				     nr_pages);
6079c276cc6SMinchan Kim 	}
6089c276cc6SMinchan Kim }
60910853a03SMinchan Kim 
lru_lazyfree_fn(struct lruvec * lruvec,struct folio * folio)610cec394baSMatthew Wilcox (Oracle) static void lru_lazyfree_fn(struct lruvec *lruvec, struct folio *folio)
61110853a03SMinchan Kim {
612cec394baSMatthew Wilcox (Oracle) 	if (folio_test_anon(folio) && folio_test_swapbacked(folio) &&
613cec394baSMatthew Wilcox (Oracle) 	    !folio_test_swapcache(folio) && !folio_test_unevictable(folio)) {
614cec394baSMatthew Wilcox (Oracle) 		long nr_pages = folio_nr_pages(folio);
61510853a03SMinchan Kim 
616cec394baSMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
617cec394baSMatthew Wilcox (Oracle) 		folio_clear_active(folio);
618cec394baSMatthew Wilcox (Oracle) 		folio_clear_referenced(folio);
619f7ad2a6cSShaohua Li 		/*
620cec394baSMatthew Wilcox (Oracle) 		 * Lazyfree folios are clean anonymous folios.  They have
621cec394baSMatthew Wilcox (Oracle) 		 * the swapbacked flag cleared, to distinguish them from normal
622cec394baSMatthew Wilcox (Oracle) 		 * anonymous folios
623f7ad2a6cSShaohua Li 		 */
624cec394baSMatthew Wilcox (Oracle) 		folio_clear_swapbacked(folio);
625cec394baSMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
62610853a03SMinchan Kim 
62721e330fcSShakeel Butt 		__count_vm_events(PGLAZYFREE, nr_pages);
62821e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE,
62921e330fcSShakeel Butt 				     nr_pages);
63010853a03SMinchan Kim 	}
63110853a03SMinchan Kim }
63210853a03SMinchan Kim 
63331560180SMinchan Kim /*
63482ac64d8SMatthew Wilcox (Oracle)  * Drain pages out of the cpu's folio_batch.
635902aaed0SHisashi Hifumi  * Either "cpu" is the current CPU, and preemption has already been
636902aaed0SHisashi Hifumi  * disabled; or "cpu" is being hot-unplugged, and is already dead.
637902aaed0SHisashi Hifumi  */
lru_add_drain_cpu(int cpu)638f0cb3c76SKonstantin Khlebnikov void lru_add_drain_cpu(int cpu)
6391da177e4SLinus Torvalds {
640a2d33b5dSMatthew Wilcox (Oracle) 	struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
641a2d33b5dSMatthew Wilcox (Oracle) 	struct folio_batch *fbatch = &fbatches->lru_add;
6421da177e4SLinus Torvalds 
64370dea534SMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
64470dea534SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_add_fn);
645902aaed0SHisashi Hifumi 
646c2bc1681SMatthew Wilcox (Oracle) 	fbatch = &per_cpu(lru_rotate.fbatch, cpu);
6477e0cc01eSQian Cai 	/* Disabling interrupts below acts as a compiler barrier. */
648c2bc1681SMatthew Wilcox (Oracle) 	if (data_race(folio_batch_count(fbatch))) {
649902aaed0SHisashi Hifumi 		unsigned long flags;
650902aaed0SHisashi Hifumi 
651902aaed0SHisashi Hifumi 		/* No harm done if a racing interrupt already did this */
652b01b2141SIngo Molnar 		local_lock_irqsave(&lru_rotate.lock, flags);
653c2bc1681SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_move_tail_fn);
654b01b2141SIngo Molnar 		local_unlock_irqrestore(&lru_rotate.lock, flags);
655902aaed0SHisashi Hifumi 	}
65631560180SMinchan Kim 
657a2d33b5dSMatthew Wilcox (Oracle) 	fbatch = &fbatches->lru_deactivate_file;
6587a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
6597a3dbfe8SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_deactivate_file_fn);
660eb709b0dSShaohua Li 
661a2d33b5dSMatthew Wilcox (Oracle) 	fbatch = &fbatches->lru_deactivate;
66285cd7791SMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
66385cd7791SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_deactivate_fn);
6649c276cc6SMinchan Kim 
665a2d33b5dSMatthew Wilcox (Oracle) 	fbatch = &fbatches->lru_lazyfree;
666cec394baSMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
667cec394baSMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_lazyfree_fn);
66810853a03SMinchan Kim 
6693a44610bSMatthew Wilcox (Oracle) 	folio_activate_drain(cpu);
67031560180SMinchan Kim }
67131560180SMinchan Kim 
67231560180SMinchan Kim /**
6737a3dbfe8SMatthew Wilcox (Oracle)  * deactivate_file_folio() - Deactivate a file folio.
674261b6840SMatthew Wilcox (Oracle)  * @folio: Folio to deactivate.
67531560180SMinchan Kim  *
676261b6840SMatthew Wilcox (Oracle)  * This function hints to the VM that @folio is a good reclaim candidate,
677261b6840SMatthew Wilcox (Oracle)  * for example if its invalidation fails due to the folio being dirty
67831560180SMinchan Kim  * or under writeback.
679261b6840SMatthew Wilcox (Oracle)  *
6807a3dbfe8SMatthew Wilcox (Oracle)  * Context: Caller holds a reference on the folio.
68131560180SMinchan Kim  */
deactivate_file_folio(struct folio * folio)682261b6840SMatthew Wilcox (Oracle) void deactivate_file_folio(struct folio *folio)
68331560180SMinchan Kim {
6847a3dbfe8SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch;
685b01b2141SIngo Molnar 
6867a3dbfe8SMatthew Wilcox (Oracle) 	/* Deactivating an unevictable folio will not accelerate reclaim */
687261b6840SMatthew Wilcox (Oracle) 	if (folio_test_unevictable(folio))
688261b6840SMatthew Wilcox (Oracle) 		return;
689261b6840SMatthew Wilcox (Oracle) 
690261b6840SMatthew Wilcox (Oracle) 	folio_get(folio);
69182ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
69282ac64d8SMatthew Wilcox (Oracle) 	fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate_file);
6937a3dbfe8SMatthew Wilcox (Oracle) 	folio_batch_add_and_move(fbatch, folio, lru_deactivate_file_fn);
69482ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
69531560180SMinchan Kim }
69680bfed90SAndrew Morton 
6979c276cc6SMinchan Kim /*
6985a9e3474SVishal Moola (Oracle)  * folio_deactivate - deactivate a folio
6995a9e3474SVishal Moola (Oracle)  * @folio: folio to deactivate
7009c276cc6SMinchan Kim  *
7015a9e3474SVishal Moola (Oracle)  * folio_deactivate() moves @folio to the inactive list if @folio was on the
7025a9e3474SVishal Moola (Oracle)  * active list and was not unevictable. This is done to accelerate the
7035a9e3474SVishal Moola (Oracle)  * reclaim of @folio.
7049c276cc6SMinchan Kim  */
folio_deactivate(struct folio * folio)7055a9e3474SVishal Moola (Oracle) void folio_deactivate(struct folio *folio)
7069c276cc6SMinchan Kim {
707ec1c86b2SYu Zhao 	if (folio_test_lru(folio) && !folio_test_unevictable(folio) &&
708ec1c86b2SYu Zhao 	    (folio_test_active(folio) || lru_gen_enabled())) {
70985cd7791SMatthew Wilcox (Oracle) 		struct folio_batch *fbatch;
71085cd7791SMatthew Wilcox (Oracle) 
71185cd7791SMatthew Wilcox (Oracle) 		folio_get(folio);
71282ac64d8SMatthew Wilcox (Oracle) 		local_lock(&cpu_fbatches.lock);
71382ac64d8SMatthew Wilcox (Oracle) 		fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate);
71485cd7791SMatthew Wilcox (Oracle) 		folio_batch_add_and_move(fbatch, folio, lru_deactivate_fn);
71582ac64d8SMatthew Wilcox (Oracle) 		local_unlock(&cpu_fbatches.lock);
7169c276cc6SMinchan Kim 	}
7179c276cc6SMinchan Kim }
7189c276cc6SMinchan Kim 
71910853a03SMinchan Kim /**
7206a6fe9ebSKefeng Wang  * folio_mark_lazyfree - make an anon folio lazyfree
7216a6fe9ebSKefeng Wang  * @folio: folio to deactivate
72210853a03SMinchan Kim  *
7236a6fe9ebSKefeng Wang  * folio_mark_lazyfree() moves @folio to the inactive file list.
7246a6fe9ebSKefeng Wang  * This is done to accelerate the reclaim of @folio.
72510853a03SMinchan Kim  */
folio_mark_lazyfree(struct folio * folio)7266a6fe9ebSKefeng Wang void folio_mark_lazyfree(struct folio *folio)
72710853a03SMinchan Kim {
728cec394baSMatthew Wilcox (Oracle) 	if (folio_test_lru(folio) && folio_test_anon(folio) &&
729cec394baSMatthew Wilcox (Oracle) 	    folio_test_swapbacked(folio) && !folio_test_swapcache(folio) &&
730cec394baSMatthew Wilcox (Oracle) 	    !folio_test_unevictable(folio)) {
731cec394baSMatthew Wilcox (Oracle) 		struct folio_batch *fbatch;
732cec394baSMatthew Wilcox (Oracle) 
733cec394baSMatthew Wilcox (Oracle) 		folio_get(folio);
73482ac64d8SMatthew Wilcox (Oracle) 		local_lock(&cpu_fbatches.lock);
73582ac64d8SMatthew Wilcox (Oracle) 		fbatch = this_cpu_ptr(&cpu_fbatches.lru_lazyfree);
736cec394baSMatthew Wilcox (Oracle) 		folio_batch_add_and_move(fbatch, folio, lru_lazyfree_fn);
73782ac64d8SMatthew Wilcox (Oracle) 		local_unlock(&cpu_fbatches.lock);
73810853a03SMinchan Kim 	}
73910853a03SMinchan Kim }
74010853a03SMinchan Kim 
lru_add_drain(void)74180bfed90SAndrew Morton void lru_add_drain(void)
74280bfed90SAndrew Morton {
74382ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
744b01b2141SIngo Molnar 	lru_add_drain_cpu(smp_processor_id());
74582ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
74696f97c43SLorenzo Stoakes 	mlock_drain_local();
747b01b2141SIngo Molnar }
748b01b2141SIngo Molnar 
749243418e3SMinchan Kim /*
750243418e3SMinchan Kim  * It's called from per-cpu workqueue context in SMP case so
751243418e3SMinchan Kim  * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on
752243418e3SMinchan Kim  * the same cpu. It shouldn't be a problem in !SMP case since
753243418e3SMinchan Kim  * the core is only one and the locks will disable preemption.
754243418e3SMinchan Kim  */
lru_add_and_bh_lrus_drain(void)755243418e3SMinchan Kim static void lru_add_and_bh_lrus_drain(void)
756243418e3SMinchan Kim {
75782ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
758243418e3SMinchan Kim 	lru_add_drain_cpu(smp_processor_id());
75982ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
760243418e3SMinchan Kim 	invalidate_bh_lrus_cpu();
76196f97c43SLorenzo Stoakes 	mlock_drain_local();
762243418e3SMinchan Kim }
763243418e3SMinchan Kim 
lru_add_drain_cpu_zone(struct zone * zone)764b01b2141SIngo Molnar void lru_add_drain_cpu_zone(struct zone *zone)
765b01b2141SIngo Molnar {
76682ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
767b01b2141SIngo Molnar 	lru_add_drain_cpu(smp_processor_id());
768b01b2141SIngo Molnar 	drain_local_pages(zone);
76982ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
77096f97c43SLorenzo Stoakes 	mlock_drain_local();
7711da177e4SLinus Torvalds }
7721da177e4SLinus Torvalds 
7736ea183d6SMichal Hocko #ifdef CONFIG_SMP
7746ea183d6SMichal Hocko 
7756ea183d6SMichal Hocko static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
7766ea183d6SMichal Hocko 
lru_add_drain_per_cpu(struct work_struct * dummy)777c4028958SDavid Howells static void lru_add_drain_per_cpu(struct work_struct *dummy)
778053837fcSNick Piggin {
779243418e3SMinchan Kim 	lru_add_and_bh_lrus_drain();
780053837fcSNick Piggin }
781053837fcSNick Piggin 
cpu_needs_drain(unsigned int cpu)7824864545aSMatthew Wilcox (Oracle) static bool cpu_needs_drain(unsigned int cpu)
7834864545aSMatthew Wilcox (Oracle) {
7844864545aSMatthew Wilcox (Oracle) 	struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
7854864545aSMatthew Wilcox (Oracle) 
7864864545aSMatthew Wilcox (Oracle) 	/* Check these in order of likelihood that they're not zero */
7874864545aSMatthew Wilcox (Oracle) 	return folio_batch_count(&fbatches->lru_add) ||
7884864545aSMatthew Wilcox (Oracle) 		data_race(folio_batch_count(&per_cpu(lru_rotate.fbatch, cpu))) ||
7894864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->lru_deactivate_file) ||
7904864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->lru_deactivate) ||
7914864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->lru_lazyfree) ||
7924864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->activate) ||
79396f97c43SLorenzo Stoakes 		need_mlock_drain(cpu) ||
7944864545aSMatthew Wilcox (Oracle) 		has_bh_in_lru(cpu, NULL);
7954864545aSMatthew Wilcox (Oracle) }
7964864545aSMatthew Wilcox (Oracle) 
7979852a721SMichal Hocko /*
7989852a721SMichal Hocko  * Doesn't need any cpu hotplug locking because we do rely on per-cpu
7999852a721SMichal Hocko  * kworkers being shut down before our page_alloc_cpu_dead callback is
8009852a721SMichal Hocko  * executed on the offlined cpu.
8019852a721SMichal Hocko  * Calling this function with cpu hotplug locks held can actually lead
8029852a721SMichal Hocko  * to obscure indirect dependencies via WQ context.
8039852a721SMichal Hocko  */
__lru_add_drain_all(bool force_all_cpus)8043db3264dSMiaohe Lin static inline void __lru_add_drain_all(bool force_all_cpus)
805053837fcSNick Piggin {
8066446a513SAhmed S. Darwish 	/*
8076446a513SAhmed S. Darwish 	 * lru_drain_gen - Global pages generation number
8086446a513SAhmed S. Darwish 	 *
8096446a513SAhmed S. Darwish 	 * (A) Definition: global lru_drain_gen = x implies that all generations
8106446a513SAhmed S. Darwish 	 *     0 < n <= x are already *scheduled* for draining.
8116446a513SAhmed S. Darwish 	 *
8126446a513SAhmed S. Darwish 	 * This is an optimization for the highly-contended use case where a
8136446a513SAhmed S. Darwish 	 * user space workload keeps constantly generating a flow of pages for
8146446a513SAhmed S. Darwish 	 * each CPU.
8156446a513SAhmed S. Darwish 	 */
8166446a513SAhmed S. Darwish 	static unsigned int lru_drain_gen;
8175fbc4616SChris Metcalf 	static struct cpumask has_work;
8186446a513SAhmed S. Darwish 	static DEFINE_MUTEX(lock);
8196446a513SAhmed S. Darwish 	unsigned cpu, this_gen;
8205fbc4616SChris Metcalf 
821ce612879SMichal Hocko 	/*
822ce612879SMichal Hocko 	 * Make sure nobody triggers this path before mm_percpu_wq is fully
823ce612879SMichal Hocko 	 * initialized.
824ce612879SMichal Hocko 	 */
825ce612879SMichal Hocko 	if (WARN_ON(!mm_percpu_wq))
826ce612879SMichal Hocko 		return;
827ce612879SMichal Hocko 
8286446a513SAhmed S. Darwish 	/*
82982ac64d8SMatthew Wilcox (Oracle) 	 * Guarantee folio_batch counter stores visible by this CPU
83082ac64d8SMatthew Wilcox (Oracle) 	 * are visible to other CPUs before loading the current drain
83182ac64d8SMatthew Wilcox (Oracle) 	 * generation.
8326446a513SAhmed S. Darwish 	 */
8336446a513SAhmed S. Darwish 	smp_mb();
8346446a513SAhmed S. Darwish 
8356446a513SAhmed S. Darwish 	/*
8366446a513SAhmed S. Darwish 	 * (B) Locally cache global LRU draining generation number
8376446a513SAhmed S. Darwish 	 *
8386446a513SAhmed S. Darwish 	 * The read barrier ensures that the counter is loaded before the mutex
8396446a513SAhmed S. Darwish 	 * is taken. It pairs with smp_mb() inside the mutex critical section
8406446a513SAhmed S. Darwish 	 * at (D).
8416446a513SAhmed S. Darwish 	 */
8426446a513SAhmed S. Darwish 	this_gen = smp_load_acquire(&lru_drain_gen);
843eef1a429SKonstantin Khlebnikov 
8445fbc4616SChris Metcalf 	mutex_lock(&lock);
845eef1a429SKonstantin Khlebnikov 
846eef1a429SKonstantin Khlebnikov 	/*
8476446a513SAhmed S. Darwish 	 * (C) Exit the draining operation if a newer generation, from another
8486446a513SAhmed S. Darwish 	 * lru_add_drain_all(), was already scheduled for draining. Check (A).
849eef1a429SKonstantin Khlebnikov 	 */
850d479960eSMinchan Kim 	if (unlikely(this_gen != lru_drain_gen && !force_all_cpus))
851eef1a429SKonstantin Khlebnikov 		goto done;
852eef1a429SKonstantin Khlebnikov 
8536446a513SAhmed S. Darwish 	/*
8546446a513SAhmed S. Darwish 	 * (D) Increment global generation number
8556446a513SAhmed S. Darwish 	 *
8566446a513SAhmed S. Darwish 	 * Pairs with smp_load_acquire() at (B), outside of the critical
85782ac64d8SMatthew Wilcox (Oracle) 	 * section. Use a full memory barrier to guarantee that the
85882ac64d8SMatthew Wilcox (Oracle) 	 * new global drain generation number is stored before loading
85982ac64d8SMatthew Wilcox (Oracle) 	 * folio_batch counters.
8606446a513SAhmed S. Darwish 	 *
8616446a513SAhmed S. Darwish 	 * This pairing must be done here, before the for_each_online_cpu loop
8626446a513SAhmed S. Darwish 	 * below which drains the page vectors.
8636446a513SAhmed S. Darwish 	 *
8646446a513SAhmed S. Darwish 	 * Let x, y, and z represent some system CPU numbers, where x < y < z.
865cb152a1aSShijie Luo 	 * Assume CPU #z is in the middle of the for_each_online_cpu loop
8666446a513SAhmed S. Darwish 	 * below and has already reached CPU #y's per-cpu data. CPU #x comes
8676446a513SAhmed S. Darwish 	 * along, adds some pages to its per-cpu vectors, then calls
8686446a513SAhmed S. Darwish 	 * lru_add_drain_all().
8696446a513SAhmed S. Darwish 	 *
8706446a513SAhmed S. Darwish 	 * If the paired barrier is done at any later step, e.g. after the
8716446a513SAhmed S. Darwish 	 * loop, CPU #x will just exit at (C) and miss flushing out all of its
8726446a513SAhmed S. Darwish 	 * added pages.
8736446a513SAhmed S. Darwish 	 */
8746446a513SAhmed S. Darwish 	WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1);
8756446a513SAhmed S. Darwish 	smp_mb();
876eef1a429SKonstantin Khlebnikov 
8775fbc4616SChris Metcalf 	cpumask_clear(&has_work);
8785fbc4616SChris Metcalf 	for_each_online_cpu(cpu) {
8795fbc4616SChris Metcalf 		struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
8805fbc4616SChris Metcalf 
8814864545aSMatthew Wilcox (Oracle) 		if (cpu_needs_drain(cpu)) {
8825fbc4616SChris Metcalf 			INIT_WORK(work, lru_add_drain_per_cpu);
883ce612879SMichal Hocko 			queue_work_on(cpu, mm_percpu_wq, work);
8846446a513SAhmed S. Darwish 			__cpumask_set_cpu(cpu, &has_work);
8855fbc4616SChris Metcalf 		}
8865fbc4616SChris Metcalf 	}
8875fbc4616SChris Metcalf 
8885fbc4616SChris Metcalf 	for_each_cpu(cpu, &has_work)
8895fbc4616SChris Metcalf 		flush_work(&per_cpu(lru_add_drain_work, cpu));
8905fbc4616SChris Metcalf 
891eef1a429SKonstantin Khlebnikov done:
8925fbc4616SChris Metcalf 	mutex_unlock(&lock);
893053837fcSNick Piggin }
894d479960eSMinchan Kim 
lru_add_drain_all(void)895d479960eSMinchan Kim void lru_add_drain_all(void)
896d479960eSMinchan Kim {
897d479960eSMinchan Kim 	__lru_add_drain_all(false);
898d479960eSMinchan Kim }
8996ea183d6SMichal Hocko #else
lru_add_drain_all(void)9006ea183d6SMichal Hocko void lru_add_drain_all(void)
9016ea183d6SMichal Hocko {
9026ea183d6SMichal Hocko 	lru_add_drain();
9036ea183d6SMichal Hocko }
9046446a513SAhmed S. Darwish #endif /* CONFIG_SMP */
905053837fcSNick Piggin 
906d479960eSMinchan Kim atomic_t lru_disable_count = ATOMIC_INIT(0);
907d479960eSMinchan Kim 
908d479960eSMinchan Kim /*
909d479960eSMinchan Kim  * lru_cache_disable() needs to be called before we start compiling
910d479960eSMinchan Kim  * a list of pages to be migrated using isolate_lru_page().
911d479960eSMinchan Kim  * It drains pages on LRU cache and then disable on all cpus until
912d479960eSMinchan Kim  * lru_cache_enable is called.
913d479960eSMinchan Kim  *
914d479960eSMinchan Kim  * Must be paired with a call to lru_cache_enable().
915d479960eSMinchan Kim  */
lru_cache_disable(void)916d479960eSMinchan Kim void lru_cache_disable(void)
917d479960eSMinchan Kim {
918d479960eSMinchan Kim 	atomic_inc(&lru_disable_count);
919d479960eSMinchan Kim 	/*
920ff042f4aSMarcelo Tosatti 	 * Readers of lru_disable_count are protected by either disabling
921ff042f4aSMarcelo Tosatti 	 * preemption or rcu_read_lock:
922ff042f4aSMarcelo Tosatti 	 *
923ff042f4aSMarcelo Tosatti 	 * preempt_disable, local_irq_disable  [bh_lru_lock()]
924ff042f4aSMarcelo Tosatti 	 * rcu_read_lock		       [rt_spin_lock CONFIG_PREEMPT_RT]
925ff042f4aSMarcelo Tosatti 	 * preempt_disable		       [local_lock !CONFIG_PREEMPT_RT]
926ff042f4aSMarcelo Tosatti 	 *
927ff042f4aSMarcelo Tosatti 	 * Since v5.1 kernel, synchronize_rcu() is guaranteed to wait on
928ff042f4aSMarcelo Tosatti 	 * preempt_disable() regions of code. So any CPU which sees
929ff042f4aSMarcelo Tosatti 	 * lru_disable_count = 0 will have exited the critical
930ff042f4aSMarcelo Tosatti 	 * section when synchronize_rcu() returns.
931d479960eSMinchan Kim 	 */
93231733463SMarcelo Tosatti 	synchronize_rcu_expedited();
933ff042f4aSMarcelo Tosatti #ifdef CONFIG_SMP
934d479960eSMinchan Kim 	__lru_add_drain_all(true);
935d479960eSMinchan Kim #else
936243418e3SMinchan Kim 	lru_add_and_bh_lrus_drain();
937d479960eSMinchan Kim #endif
938d479960eSMinchan Kim }
939d479960eSMinchan Kim 
940aabfb572SMichal Hocko /**
941ea1754a0SKirill A. Shutemov  * release_pages - batched put_page()
942449c7967SLinus Torvalds  * @arg: array of pages to release
943aabfb572SMichal Hocko  * @nr: number of pages
9441da177e4SLinus Torvalds  *
945449c7967SLinus Torvalds  * Decrement the reference count on all the pages in @arg.  If it
946aabfb572SMichal Hocko  * fell to zero, remove the page from the LRU and free it.
947449c7967SLinus Torvalds  *
948449c7967SLinus Torvalds  * Note that the argument can be an array of pages, encoded pages,
949449c7967SLinus Torvalds  * or folio pointers. We ignore any encoded bits, and turn any of
950449c7967SLinus Torvalds  * them into just a folio that gets free'd.
9511da177e4SLinus Torvalds  */
release_pages(release_pages_arg arg,int nr)952449c7967SLinus Torvalds void release_pages(release_pages_arg arg, int nr)
9531da177e4SLinus Torvalds {
9541da177e4SLinus Torvalds 	int i;
955449c7967SLinus Torvalds 	struct encoded_page **encoded = arg.encoded_pages;
956cc59850eSKonstantin Khlebnikov 	LIST_HEAD(pages_to_free);
9576168d0daSAlex Shi 	struct lruvec *lruvec = NULL;
9580de340cbSMatthew Wilcox (Oracle) 	unsigned long flags = 0;
9593f649ab7SKees Cook 	unsigned int lock_batch;
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	for (i = 0; i < nr; i++) {
962449c7967SLinus Torvalds 		struct folio *folio;
963449c7967SLinus Torvalds 
964449c7967SLinus Torvalds 		/* Turn any of the argument types into a folio */
965449c7967SLinus Torvalds 		folio = page_folio(encoded_page_ptr(encoded[i]));
9661da177e4SLinus Torvalds 
967aabfb572SMichal Hocko 		/*
968aabfb572SMichal Hocko 		 * Make sure the IRQ-safe lock-holding time does not get
969aabfb572SMichal Hocko 		 * excessive with a continuous string of pages from the
9706168d0daSAlex Shi 		 * same lruvec. The lock is held only if lruvec != NULL.
971aabfb572SMichal Hocko 		 */
9726168d0daSAlex Shi 		if (lruvec && ++lock_batch == SWAP_CLUSTER_MAX) {
9736168d0daSAlex Shi 			unlock_page_lruvec_irqrestore(lruvec, flags);
9746168d0daSAlex Shi 			lruvec = NULL;
975aabfb572SMichal Hocko 		}
976aabfb572SMichal Hocko 
977ab5e653eSMatthew Wilcox (Oracle) 		if (is_huge_zero_page(&folio->page))
978aa88b68cSKirill A. Shutemov 			continue;
979aa88b68cSKirill A. Shutemov 
980ab5e653eSMatthew Wilcox (Oracle) 		if (folio_is_zone_device(folio)) {
9816168d0daSAlex Shi 			if (lruvec) {
9826168d0daSAlex Shi 				unlock_page_lruvec_irqrestore(lruvec, flags);
9836168d0daSAlex Shi 				lruvec = NULL;
984df6ad698SJérôme Glisse 			}
985ab5e653eSMatthew Wilcox (Oracle) 			if (put_devmap_managed_page(&folio->page))
986df6ad698SJérôme Glisse 				continue;
987ab5e653eSMatthew Wilcox (Oracle) 			if (folio_put_testzero(folio))
988ab5e653eSMatthew Wilcox (Oracle) 				free_zone_device_page(&folio->page);
98943fbdeb3SRalph Campbell 			continue;
99007d80269SJohn Hubbard 		}
991df6ad698SJérôme Glisse 
992ab5e653eSMatthew Wilcox (Oracle) 		if (!folio_put_testzero(folio))
9931da177e4SLinus Torvalds 			continue;
9941da177e4SLinus Torvalds 
995ab5e653eSMatthew Wilcox (Oracle) 		if (folio_test_large(folio)) {
9966168d0daSAlex Shi 			if (lruvec) {
9976168d0daSAlex Shi 				unlock_page_lruvec_irqrestore(lruvec, flags);
9986168d0daSAlex Shi 				lruvec = NULL;
999ddc58f27SKirill A. Shutemov 			}
10005ef82fe7SMatthew Wilcox (Oracle) 			__folio_put_large(folio);
1001ddc58f27SKirill A. Shutemov 			continue;
1002ddc58f27SKirill A. Shutemov 		}
1003ddc58f27SKirill A. Shutemov 
1004ab5e653eSMatthew Wilcox (Oracle) 		if (folio_test_lru(folio)) {
10052a5e4e34SAlexander Duyck 			struct lruvec *prev_lruvec = lruvec;
1006894bc310SLee Schermerhorn 
10070de340cbSMatthew Wilcox (Oracle) 			lruvec = folio_lruvec_relock_irqsave(folio, lruvec,
10082a5e4e34SAlexander Duyck 									&flags);
10092a5e4e34SAlexander Duyck 			if (prev_lruvec != lruvec)
1010aabfb572SMichal Hocko 				lock_batch = 0;
1011fa9add64SHugh Dickins 
1012ab5e653eSMatthew Wilcox (Oracle) 			lruvec_del_folio(lruvec, folio);
1013ab5e653eSMatthew Wilcox (Oracle) 			__folio_clear_lru_flags(folio);
101446453a6eSNick Piggin 		}
101546453a6eSNick Piggin 
1016ab5e653eSMatthew Wilcox (Oracle) 		list_add(&folio->lru, &pages_to_free);
10171da177e4SLinus Torvalds 	}
10186168d0daSAlex Shi 	if (lruvec)
10196168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
10201da177e4SLinus Torvalds 
1021747db954SJohannes Weiner 	mem_cgroup_uncharge_list(&pages_to_free);
10222d4894b5SMel Gorman 	free_unref_page_list(&pages_to_free);
10231da177e4SLinus Torvalds }
10240be8557bSMiklos Szeredi EXPORT_SYMBOL(release_pages);
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds /*
10271e0877d5SMatthew Wilcox (Oracle)  * The folios which we're about to release may be in the deferred lru-addition
10281da177e4SLinus Torvalds  * queues.  That would prevent them from really being freed right now.  That's
10291e0877d5SMatthew Wilcox (Oracle)  * OK from a correctness point of view but is inefficient - those folios may be
10301da177e4SLinus Torvalds  * cache-warm and we want to give them back to the page allocator ASAP.
10311da177e4SLinus Torvalds  *
10321e0877d5SMatthew Wilcox (Oracle)  * So __folio_batch_release() will drain those queues here.
103370dea534SMatthew Wilcox (Oracle)  * folio_batch_move_lru() calls folios_put() directly to avoid
10341da177e4SLinus Torvalds  * mutual recursion.
10351da177e4SLinus Torvalds  */
__folio_batch_release(struct folio_batch * fbatch)10361e0877d5SMatthew Wilcox (Oracle) void __folio_batch_release(struct folio_batch *fbatch)
10371da177e4SLinus Torvalds {
10381e0877d5SMatthew Wilcox (Oracle) 	if (!fbatch->percpu_pvec_drained) {
10391da177e4SLinus Torvalds 		lru_add_drain();
10401e0877d5SMatthew Wilcox (Oracle) 		fbatch->percpu_pvec_drained = true;
1041d9ed0d08SMel Gorman 	}
10421e0877d5SMatthew Wilcox (Oracle) 	release_pages(fbatch->folios, folio_batch_count(fbatch));
10431e0877d5SMatthew Wilcox (Oracle) 	folio_batch_reinit(fbatch);
10441da177e4SLinus Torvalds }
10451e0877d5SMatthew Wilcox (Oracle) EXPORT_SYMBOL(__folio_batch_release);
10467f285701SSteve French 
10471da177e4SLinus Torvalds /**
10481613fac9SMatthew Wilcox (Oracle)  * folio_batch_remove_exceptionals() - Prune non-folios from a batch.
10491613fac9SMatthew Wilcox (Oracle)  * @fbatch: The batch to prune
10500cd6144aSJohannes Weiner  *
10511613fac9SMatthew Wilcox (Oracle)  * find_get_entries() fills a batch with both folios and shadow/swap/DAX
10521613fac9SMatthew Wilcox (Oracle)  * entries.  This function prunes all the non-folio entries from @fbatch
10531613fac9SMatthew Wilcox (Oracle)  * without leaving holes, so that it can be passed on to folio-only batch
10541613fac9SMatthew Wilcox (Oracle)  * operations.
10550cd6144aSJohannes Weiner  */
folio_batch_remove_exceptionals(struct folio_batch * fbatch)10561613fac9SMatthew Wilcox (Oracle) void folio_batch_remove_exceptionals(struct folio_batch *fbatch)
10570cd6144aSJohannes Weiner {
10581613fac9SMatthew Wilcox (Oracle) 	unsigned int i, j;
10590cd6144aSJohannes Weiner 
10601613fac9SMatthew Wilcox (Oracle) 	for (i = 0, j = 0; i < folio_batch_count(fbatch); i++) {
10611613fac9SMatthew Wilcox (Oracle) 		struct folio *folio = fbatch->folios[i];
10621613fac9SMatthew Wilcox (Oracle) 		if (!xa_is_value(folio))
10631613fac9SMatthew Wilcox (Oracle) 			fbatch->folios[j++] = folio;
10640cd6144aSJohannes Weiner 	}
10651613fac9SMatthew Wilcox (Oracle) 	fbatch->nr = j;
10660cd6144aSJohannes Weiner }
10670cd6144aSJohannes Weiner 
10681da177e4SLinus Torvalds /*
10691da177e4SLinus Torvalds  * Perform any setup for the swap system
10701da177e4SLinus Torvalds  */
swap_setup(void)10711da177e4SLinus Torvalds void __init swap_setup(void)
10721da177e4SLinus Torvalds {
1073ca79b0c2SArun KS 	unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
1074e0bf68ddSPeter Zijlstra 
10751da177e4SLinus Torvalds 	/* Use a smaller cluster for small-memory machines */
10761da177e4SLinus Torvalds 	if (megs < 16)
10771da177e4SLinus Torvalds 		page_cluster = 2;
10781da177e4SLinus Torvalds 	else
10791da177e4SLinus Torvalds 		page_cluster = 3;
10801da177e4SLinus Torvalds 	/*
10811da177e4SLinus Torvalds 	 * Right now other parts of the system means that we
10821da177e4SLinus Torvalds 	 * _really_ don't want to cluster much more
10831da177e4SLinus Torvalds 	 */
10841da177e4SLinus Torvalds }
1085