xref: /linux/mm/swap.c (revision ac35a490237446b71e3b4b782b1596967edd0aa8)
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 
461da177e4SLinus Torvalds /* How many pages do we try to swap or page in/out together? */
471da177e4SLinus Torvalds int page_cluster;
481da177e4SLinus Torvalds 
49c2bc1681SMatthew Wilcox (Oracle) /* Protecting only lru_rotate.fbatch which requires disabling interrupts */
50b01b2141SIngo Molnar struct lru_rotate {
51b01b2141SIngo Molnar 	local_lock_t lock;
52c2bc1681SMatthew Wilcox (Oracle) 	struct folio_batch fbatch;
53b01b2141SIngo Molnar };
54b01b2141SIngo Molnar static DEFINE_PER_CPU(struct lru_rotate, lru_rotate) = {
55b01b2141SIngo Molnar 	.lock = INIT_LOCAL_LOCK(lock),
56b01b2141SIngo Molnar };
57b01b2141SIngo Molnar 
58b01b2141SIngo Molnar /*
5982ac64d8SMatthew Wilcox (Oracle)  * The following folio batches are grouped together because they are protected
60b01b2141SIngo Molnar  * by disabling preemption (and interrupts remain enabled).
61b01b2141SIngo Molnar  */
6282ac64d8SMatthew Wilcox (Oracle) struct cpu_fbatches {
63b01b2141SIngo Molnar 	local_lock_t lock;
6470dea534SMatthew Wilcox (Oracle) 	struct folio_batch lru_add;
657a3dbfe8SMatthew Wilcox (Oracle) 	struct folio_batch lru_deactivate_file;
6685cd7791SMatthew Wilcox (Oracle) 	struct folio_batch lru_deactivate;
67cec394baSMatthew Wilcox (Oracle) 	struct folio_batch lru_lazyfree;
68a4a921aaSMing Li #ifdef CONFIG_SMP
693a44610bSMatthew Wilcox (Oracle) 	struct folio_batch activate;
70a4a921aaSMing Li #endif
71b01b2141SIngo Molnar };
7282ac64d8SMatthew Wilcox (Oracle) static DEFINE_PER_CPU(struct cpu_fbatches, cpu_fbatches) = {
73b01b2141SIngo Molnar 	.lock = INIT_LOCAL_LOCK(lock),
74b01b2141SIngo Molnar };
75902aaed0SHisashi Hifumi 
76b221385bSAdrian Bunk /*
77b109b870SHugh Dickins  * This path almost never happens for VM activity - pages are normally freed
78b109b870SHugh Dickins  * via pagevecs.  But it gets used by networking - and for compound pages.
79b221385bSAdrian Bunk  */
80188e8caeSMatthew Wilcox (Oracle) static void __page_cache_release(struct folio *folio)
81b221385bSAdrian Bunk {
82188e8caeSMatthew Wilcox (Oracle) 	if (folio_test_lru(folio)) {
83fa9add64SHugh Dickins 		struct lruvec *lruvec;
84fa9add64SHugh Dickins 		unsigned long flags;
85b221385bSAdrian Bunk 
86e809c3feSMatthew Wilcox (Oracle) 		lruvec = folio_lruvec_lock_irqsave(folio, &flags);
87188e8caeSMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
88188e8caeSMatthew Wilcox (Oracle) 		__folio_clear_lru_flags(folio);
896168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
90b221385bSAdrian Bunk 	}
91188e8caeSMatthew Wilcox (Oracle) 	/* See comment on folio_test_mlocked in release_pages() */
92188e8caeSMatthew Wilcox (Oracle) 	if (unlikely(folio_test_mlocked(folio))) {
93188e8caeSMatthew Wilcox (Oracle) 		long nr_pages = folio_nr_pages(folio);
94b109b870SHugh Dickins 
95188e8caeSMatthew Wilcox (Oracle) 		__folio_clear_mlocked(folio);
96188e8caeSMatthew Wilcox (Oracle) 		zone_stat_mod_folio(folio, NR_MLOCK, -nr_pages);
97b109b870SHugh Dickins 		count_vm_events(UNEVICTABLE_PGCLEARED, nr_pages);
98b109b870SHugh Dickins 	}
9991807063SAndrea Arcangeli }
10091807063SAndrea Arcangeli 
10183d99659SMatthew Wilcox (Oracle) static void __folio_put_small(struct folio *folio)
10291807063SAndrea Arcangeli {
103188e8caeSMatthew Wilcox (Oracle) 	__page_cache_release(folio);
10483d99659SMatthew Wilcox (Oracle) 	mem_cgroup_uncharge(folio);
10583d99659SMatthew Wilcox (Oracle) 	free_unref_page(&folio->page, 0);
106b221385bSAdrian Bunk }
107b221385bSAdrian Bunk 
1085ef82fe7SMatthew Wilcox (Oracle) static void __folio_put_large(struct folio *folio)
10991807063SAndrea Arcangeli {
110822fc613SNaoya Horiguchi 	/*
111822fc613SNaoya Horiguchi 	 * __page_cache_release() is supposed to be called for thp, not for
112822fc613SNaoya Horiguchi 	 * hugetlb. This is because hugetlb page does never have PageLRU set
113822fc613SNaoya Horiguchi 	 * (it's never listed to any LRU lists) and no memcg routines should
114822fc613SNaoya Horiguchi 	 * be called for hugetlb (it has a separate hugetlb_cgroup.)
115822fc613SNaoya Horiguchi 	 */
1165ef82fe7SMatthew Wilcox (Oracle) 	if (!folio_test_hugetlb(folio))
117188e8caeSMatthew Wilcox (Oracle) 		__page_cache_release(folio);
1185375336cSMatthew Wilcox (Oracle) 	destroy_large_folio(folio);
11991807063SAndrea Arcangeli }
12091807063SAndrea Arcangeli 
1218d29c703SMatthew Wilcox (Oracle) void __folio_put(struct folio *folio)
122c747ce79SJianyu Zhan {
1238d29c703SMatthew Wilcox (Oracle) 	if (unlikely(folio_is_zone_device(folio)))
1248d29c703SMatthew Wilcox (Oracle) 		free_zone_device_page(&folio->page);
1258d29c703SMatthew Wilcox (Oracle) 	else if (unlikely(folio_test_large(folio)))
1265ef82fe7SMatthew Wilcox (Oracle) 		__folio_put_large(folio);
12726296ad2SAndrew Morton 	else
12883d99659SMatthew Wilcox (Oracle) 		__folio_put_small(folio);
12926296ad2SAndrew Morton }
1308d29c703SMatthew Wilcox (Oracle) EXPORT_SYMBOL(__folio_put);
13170b50f94SAndrea Arcangeli 
1321d7ea732SAlexander Zarochentsev /**
1337682486bSRandy Dunlap  * put_pages_list() - release a list of pages
1347682486bSRandy Dunlap  * @pages: list of pages threaded on page->lru
1351d7ea732SAlexander Zarochentsev  *
136988c69f1SMatthew Wilcox (Oracle)  * Release a list of pages which are strung together on page.lru.
1371d7ea732SAlexander Zarochentsev  */
1381d7ea732SAlexander Zarochentsev void put_pages_list(struct list_head *pages)
1391d7ea732SAlexander Zarochentsev {
1402f58e5deSMatthew Wilcox (Oracle) 	struct folio *folio, *next;
1411d7ea732SAlexander Zarochentsev 
1422f58e5deSMatthew Wilcox (Oracle) 	list_for_each_entry_safe(folio, next, pages, lru) {
1432f58e5deSMatthew Wilcox (Oracle) 		if (!folio_put_testzero(folio)) {
1442f58e5deSMatthew Wilcox (Oracle) 			list_del(&folio->lru);
145988c69f1SMatthew Wilcox (Oracle) 			continue;
1461d7ea732SAlexander Zarochentsev 		}
1472f58e5deSMatthew Wilcox (Oracle) 		if (folio_test_large(folio)) {
1482f58e5deSMatthew Wilcox (Oracle) 			list_del(&folio->lru);
1495ef82fe7SMatthew Wilcox (Oracle) 			__folio_put_large(folio);
150988c69f1SMatthew Wilcox (Oracle) 			continue;
151988c69f1SMatthew Wilcox (Oracle) 		}
1522f58e5deSMatthew Wilcox (Oracle) 		/* LRU flag must be clear because it's passed using the lru */
153988c69f1SMatthew Wilcox (Oracle) 	}
154988c69f1SMatthew Wilcox (Oracle) 
155988c69f1SMatthew Wilcox (Oracle) 	free_unref_page_list(pages);
1563cd018b4SMatthew Wilcox 	INIT_LIST_HEAD(pages);
1571d7ea732SAlexander Zarochentsev }
1581d7ea732SAlexander Zarochentsev EXPORT_SYMBOL(put_pages_list);
1591d7ea732SAlexander Zarochentsev 
16018022c5dSMel Gorman /*
16118022c5dSMel Gorman  * get_kernel_pages() - pin kernel pages in memory
16218022c5dSMel Gorman  * @kiov:	An array of struct kvec structures
16318022c5dSMel Gorman  * @nr_segs:	number of segments to pin
16418022c5dSMel Gorman  * @write:	pinning for read/write, currently ignored
16518022c5dSMel Gorman  * @pages:	array that receives pointers to the pages pinned.
16618022c5dSMel Gorman  *		Should be at least nr_segs long.
16718022c5dSMel Gorman  *
168133d2743SMiaohe Lin  * Returns number of pages pinned. This may be fewer than the number requested.
169133d2743SMiaohe Lin  * If nr_segs is 0 or negative, returns 0.  If no pages were pinned, returns 0.
170133d2743SMiaohe Lin  * Each page returned must be released with a put_page() call when it is
171133d2743SMiaohe Lin  * finished with.
17218022c5dSMel Gorman  */
17318022c5dSMel Gorman int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
17418022c5dSMel Gorman 		struct page **pages)
17518022c5dSMel Gorman {
17618022c5dSMel Gorman 	int seg;
17718022c5dSMel Gorman 
17818022c5dSMel Gorman 	for (seg = 0; seg < nr_segs; seg++) {
17918022c5dSMel Gorman 		if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
18018022c5dSMel Gorman 			return seg;
18118022c5dSMel Gorman 
1825a178119SMel Gorman 		pages[seg] = kmap_to_page(kiov[seg].iov_base);
18309cbfeafSKirill A. Shutemov 		get_page(pages[seg]);
18418022c5dSMel Gorman 	}
18518022c5dSMel Gorman 
18618022c5dSMel Gorman 	return seg;
18718022c5dSMel Gorman }
18818022c5dSMel Gorman EXPORT_SYMBOL_GPL(get_kernel_pages);
18918022c5dSMel Gorman 
190c2bc1681SMatthew Wilcox (Oracle) typedef void (*move_fn_t)(struct lruvec *lruvec, struct folio *folio);
191c2bc1681SMatthew Wilcox (Oracle) 
19270dea534SMatthew Wilcox (Oracle) static void lru_add_fn(struct lruvec *lruvec, struct folio *folio)
1937d80dd09SMatthew Wilcox (Oracle) {
1947d80dd09SMatthew Wilcox (Oracle) 	int was_unevictable = folio_test_clear_unevictable(folio);
1957d80dd09SMatthew Wilcox (Oracle) 	long nr_pages = folio_nr_pages(folio);
1967d80dd09SMatthew Wilcox (Oracle) 
1977d80dd09SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
1987d80dd09SMatthew Wilcox (Oracle) 
1997d80dd09SMatthew Wilcox (Oracle) 	/*
2007d80dd09SMatthew Wilcox (Oracle) 	 * Is an smp_mb__after_atomic() still required here, before
201188e8caeSMatthew Wilcox (Oracle) 	 * folio_evictable() tests the mlocked flag, to rule out the possibility
2027d80dd09SMatthew Wilcox (Oracle) 	 * of stranding an evictable folio on an unevictable LRU?  I think
203188e8caeSMatthew Wilcox (Oracle) 	 * not, because __munlock_page() only clears the mlocked flag
204188e8caeSMatthew Wilcox (Oracle) 	 * while the LRU lock is held.
2057d80dd09SMatthew Wilcox (Oracle) 	 *
2067d80dd09SMatthew Wilcox (Oracle) 	 * (That is not true of __page_cache_release(), and not necessarily
207188e8caeSMatthew Wilcox (Oracle) 	 * true of release_pages(): but those only clear the mlocked flag after
208188e8caeSMatthew Wilcox (Oracle) 	 * folio_put_testzero() has excluded any other users of the folio.)
2097d80dd09SMatthew Wilcox (Oracle) 	 */
2107d80dd09SMatthew Wilcox (Oracle) 	if (folio_evictable(folio)) {
2117d80dd09SMatthew Wilcox (Oracle) 		if (was_unevictable)
2127d80dd09SMatthew Wilcox (Oracle) 			__count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages);
2137d80dd09SMatthew Wilcox (Oracle) 	} else {
2147d80dd09SMatthew Wilcox (Oracle) 		folio_clear_active(folio);
2157d80dd09SMatthew Wilcox (Oracle) 		folio_set_unevictable(folio);
2167d80dd09SMatthew Wilcox (Oracle) 		/*
2177d80dd09SMatthew Wilcox (Oracle) 		 * folio->mlock_count = !!folio_test_mlocked(folio)?
2187d80dd09SMatthew Wilcox (Oracle) 		 * But that leaves __mlock_page() in doubt whether another
2197d80dd09SMatthew Wilcox (Oracle) 		 * actor has already counted the mlock or not.  Err on the
2207d80dd09SMatthew Wilcox (Oracle) 		 * safe side, underestimate, let page reclaim fix it, rather
2217d80dd09SMatthew Wilcox (Oracle) 		 * than leaving a page on the unevictable LRU indefinitely.
2227d80dd09SMatthew Wilcox (Oracle) 		 */
2237d80dd09SMatthew Wilcox (Oracle) 		folio->mlock_count = 0;
2247d80dd09SMatthew Wilcox (Oracle) 		if (!was_unevictable)
2257d80dd09SMatthew Wilcox (Oracle) 			__count_vm_events(UNEVICTABLE_PGCULLED, nr_pages);
2267d80dd09SMatthew Wilcox (Oracle) 	}
2277d80dd09SMatthew Wilcox (Oracle) 
2287d80dd09SMatthew Wilcox (Oracle) 	lruvec_add_folio(lruvec, folio);
2297d80dd09SMatthew Wilcox (Oracle) 	trace_mm_lru_insertion(folio);
2307d80dd09SMatthew Wilcox (Oracle) }
2317d80dd09SMatthew Wilcox (Oracle) 
232c2bc1681SMatthew Wilcox (Oracle) static void folio_batch_move_lru(struct folio_batch *fbatch, move_fn_t move_fn)
233902aaed0SHisashi Hifumi {
234902aaed0SHisashi Hifumi 	int i;
2356168d0daSAlex Shi 	struct lruvec *lruvec = NULL;
2363dd7ae8eSShaohua Li 	unsigned long flags = 0;
237902aaed0SHisashi Hifumi 
238c2bc1681SMatthew Wilcox (Oracle) 	for (i = 0; i < folio_batch_count(fbatch); i++) {
239c2bc1681SMatthew Wilcox (Oracle) 		struct folio *folio = fbatch->folios[i];
2403dd7ae8eSShaohua Li 
241c2bc1681SMatthew Wilcox (Oracle) 		/* block memcg migration while the folio moves between lru */
24270dea534SMatthew Wilcox (Oracle) 		if (move_fn != lru_add_fn && !folio_test_clear_lru(folio))
243fc574c23SAlex Shi 			continue;
244fc574c23SAlex Shi 
2450de340cbSMatthew Wilcox (Oracle) 		lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags);
246c2bc1681SMatthew Wilcox (Oracle) 		move_fn(lruvec, folio);
247fc574c23SAlex Shi 
248c2bc1681SMatthew Wilcox (Oracle) 		folio_set_lru(folio);
2493dd7ae8eSShaohua Li 	}
250c2bc1681SMatthew Wilcox (Oracle) 
2516168d0daSAlex Shi 	if (lruvec)
2526168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
253c2bc1681SMatthew Wilcox (Oracle) 	folios_put(fbatch->folios, folio_batch_count(fbatch));
254c2bc1681SMatthew Wilcox (Oracle) 	folio_batch_init(fbatch);
2553dd7ae8eSShaohua Li }
2563dd7ae8eSShaohua Li 
257c2bc1681SMatthew Wilcox (Oracle) static void folio_batch_add_and_move(struct folio_batch *fbatch,
258c2bc1681SMatthew Wilcox (Oracle) 		struct folio *folio, move_fn_t move_fn)
2593dd7ae8eSShaohua Li {
260c2bc1681SMatthew Wilcox (Oracle) 	if (folio_batch_add(fbatch, folio) && !folio_test_large(folio) &&
261c2bc1681SMatthew Wilcox (Oracle) 	    !lru_cache_disabled())
262c2bc1681SMatthew Wilcox (Oracle) 		return;
263c2bc1681SMatthew Wilcox (Oracle) 	folio_batch_move_lru(fbatch, move_fn);
264c2bc1681SMatthew Wilcox (Oracle) }
265575ced1cSMatthew Wilcox (Oracle) 
266c2bc1681SMatthew Wilcox (Oracle) static void lru_move_tail_fn(struct lruvec *lruvec, struct folio *folio)
267c2bc1681SMatthew Wilcox (Oracle) {
268575ced1cSMatthew Wilcox (Oracle) 	if (!folio_test_unevictable(folio)) {
269575ced1cSMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
270575ced1cSMatthew Wilcox (Oracle) 		folio_clear_active(folio);
271575ced1cSMatthew Wilcox (Oracle) 		lruvec_add_folio_tail(lruvec, folio);
272575ced1cSMatthew Wilcox (Oracle) 		__count_vm_events(PGROTATED, folio_nr_pages(folio));
273902aaed0SHisashi Hifumi 	}
274902aaed0SHisashi Hifumi }
2753dd7ae8eSShaohua Li 
2763dd7ae8eSShaohua Li /*
277575ced1cSMatthew Wilcox (Oracle)  * Writeback is about to end against a folio which has been marked for
278575ced1cSMatthew Wilcox (Oracle)  * immediate reclaim.  If it still appears to be reclaimable, move it
279575ced1cSMatthew Wilcox (Oracle)  * to the tail of the inactive list.
280c7c7b80cSAlex Shi  *
281575ced1cSMatthew Wilcox (Oracle)  * folio_rotate_reclaimable() must disable IRQs, to prevent nasty races.
2821da177e4SLinus Torvalds  */
283575ced1cSMatthew Wilcox (Oracle) void folio_rotate_reclaimable(struct folio *folio)
2841da177e4SLinus Torvalds {
285575ced1cSMatthew Wilcox (Oracle) 	if (!folio_test_locked(folio) && !folio_test_dirty(folio) &&
286575ced1cSMatthew Wilcox (Oracle) 	    !folio_test_unevictable(folio) && folio_test_lru(folio)) {
287c2bc1681SMatthew Wilcox (Oracle) 		struct folio_batch *fbatch;
2881da177e4SLinus Torvalds 		unsigned long flags;
2891da177e4SLinus Torvalds 
290575ced1cSMatthew Wilcox (Oracle) 		folio_get(folio);
291b01b2141SIngo Molnar 		local_lock_irqsave(&lru_rotate.lock, flags);
292c2bc1681SMatthew Wilcox (Oracle) 		fbatch = this_cpu_ptr(&lru_rotate.fbatch);
293c2bc1681SMatthew Wilcox (Oracle) 		folio_batch_add_and_move(fbatch, folio, lru_move_tail_fn);
294b01b2141SIngo Molnar 		local_unlock_irqrestore(&lru_rotate.lock, flags);
295ac6aadb2SMiklos Szeredi 	}
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
29896f8bf4fSJohannes Weiner void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages)
2993e2f41f1SKOSAKI Motohiro {
3007cf111bcSJohannes Weiner 	do {
3017cf111bcSJohannes Weiner 		unsigned long lrusize;
3027cf111bcSJohannes Weiner 
3036168d0daSAlex Shi 		/*
3046168d0daSAlex Shi 		 * Hold lruvec->lru_lock is safe here, since
3056168d0daSAlex Shi 		 * 1) The pinned lruvec in reclaim, or
3066168d0daSAlex Shi 		 * 2) From a pre-LRU page during refault (which also holds the
3076168d0daSAlex Shi 		 *    rcu lock, so would be safe even if the page was on the LRU
3086168d0daSAlex Shi 		 *    and could move simultaneously to a new lruvec).
3096168d0daSAlex Shi 		 */
3106168d0daSAlex Shi 		spin_lock_irq(&lruvec->lru_lock);
3117cf111bcSJohannes Weiner 		/* Record cost event */
31296f8bf4fSJohannes Weiner 		if (file)
31396f8bf4fSJohannes Weiner 			lruvec->file_cost += nr_pages;
3141431d4d1SJohannes Weiner 		else
31596f8bf4fSJohannes Weiner 			lruvec->anon_cost += nr_pages;
3167cf111bcSJohannes Weiner 
3177cf111bcSJohannes Weiner 		/*
3187cf111bcSJohannes Weiner 		 * Decay previous events
3197cf111bcSJohannes Weiner 		 *
3207cf111bcSJohannes Weiner 		 * Because workloads change over time (and to avoid
3217cf111bcSJohannes Weiner 		 * overflow) we keep these statistics as a floating
3227cf111bcSJohannes Weiner 		 * average, which ends up weighing recent refaults
3237cf111bcSJohannes Weiner 		 * more than old ones.
3247cf111bcSJohannes Weiner 		 */
3257cf111bcSJohannes Weiner 		lrusize = lruvec_page_state(lruvec, NR_INACTIVE_ANON) +
3267cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_ACTIVE_ANON) +
3277cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_INACTIVE_FILE) +
3287cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_ACTIVE_FILE);
3297cf111bcSJohannes Weiner 
3307cf111bcSJohannes Weiner 		if (lruvec->file_cost + lruvec->anon_cost > lrusize / 4) {
3317cf111bcSJohannes Weiner 			lruvec->file_cost /= 2;
3327cf111bcSJohannes Weiner 			lruvec->anon_cost /= 2;
3337cf111bcSJohannes Weiner 		}
3346168d0daSAlex Shi 		spin_unlock_irq(&lruvec->lru_lock);
3357cf111bcSJohannes Weiner 	} while ((lruvec = parent_lruvec(lruvec)));
3363e2f41f1SKOSAKI Motohiro }
3373e2f41f1SKOSAKI Motohiro 
3380995d7e5SMatthew Wilcox (Oracle) void lru_note_cost_folio(struct folio *folio)
33996f8bf4fSJohannes Weiner {
3400995d7e5SMatthew Wilcox (Oracle) 	lru_note_cost(folio_lruvec(folio), folio_is_file_lru(folio),
3410995d7e5SMatthew Wilcox (Oracle) 			folio_nr_pages(folio));
34296f8bf4fSJohannes Weiner }
34396f8bf4fSJohannes Weiner 
3443a44610bSMatthew Wilcox (Oracle) static void folio_activate_fn(struct lruvec *lruvec, struct folio *folio)
345744ed144SShaohua Li {
346f2d27392SMatthew Wilcox (Oracle) 	if (!folio_test_active(folio) && !folio_test_unevictable(folio)) {
347f2d27392SMatthew Wilcox (Oracle) 		long nr_pages = folio_nr_pages(folio);
348744ed144SShaohua Li 
349f2d27392SMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
350f2d27392SMatthew Wilcox (Oracle) 		folio_set_active(folio);
351f2d27392SMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
352f2d27392SMatthew Wilcox (Oracle) 		trace_mm_lru_activate(folio);
3537a608572SLinus Torvalds 
35421e330fcSShakeel Butt 		__count_vm_events(PGACTIVATE, nr_pages);
35521e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGACTIVATE,
35621e330fcSShakeel Butt 				     nr_pages);
357744ed144SShaohua Li 	}
358eb709b0dSShaohua Li }
359eb709b0dSShaohua Li 
360eb709b0dSShaohua Li #ifdef CONFIG_SMP
3613a44610bSMatthew Wilcox (Oracle) static void folio_activate_drain(int cpu)
362f2d27392SMatthew Wilcox (Oracle) {
36382ac64d8SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch = &per_cpu(cpu_fbatches.activate, cpu);
364f2d27392SMatthew Wilcox (Oracle) 
3653a44610bSMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
3663a44610bSMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, folio_activate_fn);
3675fbc4616SChris Metcalf }
3685fbc4616SChris Metcalf 
369f2d27392SMatthew Wilcox (Oracle) static void folio_activate(struct folio *folio)
370eb709b0dSShaohua Li {
371f2d27392SMatthew Wilcox (Oracle) 	if (folio_test_lru(folio) && !folio_test_active(folio) &&
372f2d27392SMatthew Wilcox (Oracle) 	    !folio_test_unevictable(folio)) {
3733a44610bSMatthew Wilcox (Oracle) 		struct folio_batch *fbatch;
374eb709b0dSShaohua Li 
375f2d27392SMatthew Wilcox (Oracle) 		folio_get(folio);
37682ac64d8SMatthew Wilcox (Oracle) 		local_lock(&cpu_fbatches.lock);
37782ac64d8SMatthew Wilcox (Oracle) 		fbatch = this_cpu_ptr(&cpu_fbatches.activate);
3783a44610bSMatthew Wilcox (Oracle) 		folio_batch_add_and_move(fbatch, folio, folio_activate_fn);
37982ac64d8SMatthew Wilcox (Oracle) 		local_unlock(&cpu_fbatches.lock);
380eb709b0dSShaohua Li 	}
381eb709b0dSShaohua Li }
382eb709b0dSShaohua Li 
383eb709b0dSShaohua Li #else
3843a44610bSMatthew Wilcox (Oracle) static inline void folio_activate_drain(int cpu)
385eb709b0dSShaohua Li {
386eb709b0dSShaohua Li }
387eb709b0dSShaohua Li 
388f2d27392SMatthew Wilcox (Oracle) static void folio_activate(struct folio *folio)
389eb709b0dSShaohua Li {
3906168d0daSAlex Shi 	struct lruvec *lruvec;
391eb709b0dSShaohua Li 
392f2d27392SMatthew Wilcox (Oracle) 	if (folio_test_clear_lru(folio)) {
393e809c3feSMatthew Wilcox (Oracle) 		lruvec = folio_lruvec_lock_irq(folio);
3943a44610bSMatthew Wilcox (Oracle) 		folio_activate_fn(lruvec, folio);
3956168d0daSAlex Shi 		unlock_page_lruvec_irq(lruvec);
396f2d27392SMatthew Wilcox (Oracle) 		folio_set_lru(folio);
3976168d0daSAlex Shi 	}
3981da177e4SLinus Torvalds }
399eb709b0dSShaohua Li #endif
4001da177e4SLinus Torvalds 
40176580b65SMatthew Wilcox (Oracle) static void __lru_cache_activate_folio(struct folio *folio)
402059285a2SMel Gorman {
40370dea534SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch;
404059285a2SMel Gorman 	int i;
405059285a2SMel Gorman 
40682ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
40782ac64d8SMatthew Wilcox (Oracle) 	fbatch = this_cpu_ptr(&cpu_fbatches.lru_add);
408b01b2141SIngo Molnar 
409059285a2SMel Gorman 	/*
41070dea534SMatthew Wilcox (Oracle) 	 * Search backwards on the optimistic assumption that the folio being
41170dea534SMatthew Wilcox (Oracle) 	 * activated has just been added to this batch. Note that only
41270dea534SMatthew Wilcox (Oracle) 	 * the local batch is examined as a !LRU folio could be in the
413059285a2SMel Gorman 	 * process of being released, reclaimed, migrated or on a remote
41470dea534SMatthew Wilcox (Oracle) 	 * batch that is currently being drained. Furthermore, marking
41570dea534SMatthew Wilcox (Oracle) 	 * a remote batch's folio active potentially hits a race where
41670dea534SMatthew Wilcox (Oracle) 	 * a folio is marked active just after it is added to the inactive
417059285a2SMel Gorman 	 * list causing accounting errors and BUG_ON checks to trigger.
418059285a2SMel Gorman 	 */
41970dea534SMatthew Wilcox (Oracle) 	for (i = folio_batch_count(fbatch) - 1; i >= 0; i--) {
42070dea534SMatthew Wilcox (Oracle) 		struct folio *batch_folio = fbatch->folios[i];
421059285a2SMel Gorman 
42270dea534SMatthew Wilcox (Oracle) 		if (batch_folio == folio) {
42376580b65SMatthew Wilcox (Oracle) 			folio_set_active(folio);
424059285a2SMel Gorman 			break;
425059285a2SMel Gorman 		}
426059285a2SMel Gorman 	}
427059285a2SMel Gorman 
42882ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
429059285a2SMel Gorman }
430059285a2SMel Gorman 
431*ac35a490SYu Zhao #ifdef CONFIG_LRU_GEN
432*ac35a490SYu Zhao static void folio_inc_refs(struct folio *folio)
433*ac35a490SYu Zhao {
434*ac35a490SYu Zhao 	unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
435*ac35a490SYu Zhao 
436*ac35a490SYu Zhao 	if (folio_test_unevictable(folio))
437*ac35a490SYu Zhao 		return;
438*ac35a490SYu Zhao 
439*ac35a490SYu Zhao 	if (!folio_test_referenced(folio)) {
440*ac35a490SYu Zhao 		folio_set_referenced(folio);
441*ac35a490SYu Zhao 		return;
442*ac35a490SYu Zhao 	}
443*ac35a490SYu Zhao 
444*ac35a490SYu Zhao 	if (!folio_test_workingset(folio)) {
445*ac35a490SYu Zhao 		folio_set_workingset(folio);
446*ac35a490SYu Zhao 		return;
447*ac35a490SYu Zhao 	}
448*ac35a490SYu Zhao 
449*ac35a490SYu Zhao 	/* see the comment on MAX_NR_TIERS */
450*ac35a490SYu Zhao 	do {
451*ac35a490SYu Zhao 		new_flags = old_flags & LRU_REFS_MASK;
452*ac35a490SYu Zhao 		if (new_flags == LRU_REFS_MASK)
453*ac35a490SYu Zhao 			break;
454*ac35a490SYu Zhao 
455*ac35a490SYu Zhao 		new_flags += BIT(LRU_REFS_PGOFF);
456*ac35a490SYu Zhao 		new_flags |= old_flags & ~LRU_REFS_MASK;
457*ac35a490SYu Zhao 	} while (!try_cmpxchg(&folio->flags, &old_flags, new_flags));
458*ac35a490SYu Zhao }
459*ac35a490SYu Zhao #else
460*ac35a490SYu Zhao static void folio_inc_refs(struct folio *folio)
461*ac35a490SYu Zhao {
462*ac35a490SYu Zhao }
463*ac35a490SYu Zhao #endif /* CONFIG_LRU_GEN */
464*ac35a490SYu Zhao 
4651da177e4SLinus Torvalds /*
4661da177e4SLinus Torvalds  * Mark a page as having seen activity.
4671da177e4SLinus Torvalds  *
4681da177e4SLinus Torvalds  * inactive,unreferenced	->	inactive,referenced
4691da177e4SLinus Torvalds  * inactive,referenced		->	active,unreferenced
4701da177e4SLinus Torvalds  * active,unreferenced		->	active,referenced
471eb39d618SHugh Dickins  *
472eb39d618SHugh Dickins  * When a newly allocated page is not yet visible, so safe for non-atomic ops,
473eb39d618SHugh Dickins  * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
4741da177e4SLinus Torvalds  */
47576580b65SMatthew Wilcox (Oracle) void folio_mark_accessed(struct folio *folio)
4761da177e4SLinus Torvalds {
477*ac35a490SYu Zhao 	if (lru_gen_enabled()) {
478*ac35a490SYu Zhao 		folio_inc_refs(folio);
479*ac35a490SYu Zhao 		return;
480*ac35a490SYu Zhao 	}
481*ac35a490SYu Zhao 
48276580b65SMatthew Wilcox (Oracle) 	if (!folio_test_referenced(folio)) {
48376580b65SMatthew Wilcox (Oracle) 		folio_set_referenced(folio);
48476580b65SMatthew Wilcox (Oracle) 	} else if (folio_test_unevictable(folio)) {
485a1100a74SFengguang Wu 		/*
486a1100a74SFengguang Wu 		 * Unevictable pages are on the "LRU_UNEVICTABLE" list. But,
487a1100a74SFengguang Wu 		 * this list is never rotated or maintained, so marking an
488914c32e4SBang Li 		 * unevictable page accessed has no effect.
489a1100a74SFengguang Wu 		 */
49076580b65SMatthew Wilcox (Oracle) 	} else if (!folio_test_active(folio)) {
491059285a2SMel Gorman 		/*
4923a44610bSMatthew Wilcox (Oracle) 		 * If the folio is on the LRU, queue it for activation via
49382ac64d8SMatthew Wilcox (Oracle) 		 * cpu_fbatches.activate. Otherwise, assume the folio is in a
4943a44610bSMatthew Wilcox (Oracle) 		 * folio_batch, mark it active and it'll be moved to the active
495059285a2SMel Gorman 		 * LRU on the next drain.
496059285a2SMel Gorman 		 */
49776580b65SMatthew Wilcox (Oracle) 		if (folio_test_lru(folio))
49876580b65SMatthew Wilcox (Oracle) 			folio_activate(folio);
499059285a2SMel Gorman 		else
50076580b65SMatthew Wilcox (Oracle) 			__lru_cache_activate_folio(folio);
50176580b65SMatthew Wilcox (Oracle) 		folio_clear_referenced(folio);
50276580b65SMatthew Wilcox (Oracle) 		workingset_activation(folio);
5031da177e4SLinus Torvalds 	}
50476580b65SMatthew Wilcox (Oracle) 	if (folio_test_idle(folio))
50576580b65SMatthew Wilcox (Oracle) 		folio_clear_idle(folio);
5061da177e4SLinus Torvalds }
50776580b65SMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_mark_accessed);
5081da177e4SLinus Torvalds 
509f04e9ebbSKOSAKI Motohiro /**
5100d31125dSMatthew Wilcox (Oracle)  * folio_add_lru - Add a folio to an LRU list.
5110d31125dSMatthew Wilcox (Oracle)  * @folio: The folio to be added to the LRU.
5122329d375SJianyu Zhan  *
5130d31125dSMatthew Wilcox (Oracle)  * Queue the folio for addition to the LRU. The decision on whether
5142329d375SJianyu Zhan  * to add the page to the [in]active [file|anon] list is deferred until the
51582ac64d8SMatthew Wilcox (Oracle)  * folio_batch is drained. This gives a chance for the caller of folio_add_lru()
5160d31125dSMatthew Wilcox (Oracle)  * have the folio added to the active list using folio_mark_accessed().
517f04e9ebbSKOSAKI Motohiro  */
5180d31125dSMatthew Wilcox (Oracle) void folio_add_lru(struct folio *folio)
5191da177e4SLinus Torvalds {
52070dea534SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch;
5216058eaecSJohannes Weiner 
52270dea534SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_active(folio) &&
52370dea534SMatthew Wilcox (Oracle) 			folio_test_unevictable(folio), folio);
5240d31125dSMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
5256058eaecSJohannes Weiner 
526ec1c86b2SYu Zhao 	/* see the comment in lru_gen_add_folio() */
527ec1c86b2SYu Zhao 	if (lru_gen_enabled() && !folio_test_unevictable(folio) &&
528ec1c86b2SYu Zhao 	    lru_gen_in_fault() && !(current->flags & PF_MEMALLOC))
529ec1c86b2SYu Zhao 		folio_set_active(folio);
530ec1c86b2SYu Zhao 
5310d31125dSMatthew Wilcox (Oracle) 	folio_get(folio);
53282ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
53382ac64d8SMatthew Wilcox (Oracle) 	fbatch = this_cpu_ptr(&cpu_fbatches.lru_add);
53470dea534SMatthew Wilcox (Oracle) 	folio_batch_add_and_move(fbatch, folio, lru_add_fn);
53582ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
5361da177e4SLinus Torvalds }
5370d31125dSMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_add_lru);
5381da177e4SLinus Torvalds 
539894bc310SLee Schermerhorn /**
540b518154eSJoonsoo Kim  * lru_cache_add_inactive_or_unevictable
54100501b53SJohannes Weiner  * @page:  the page to be added to LRU
54200501b53SJohannes Weiner  * @vma:   vma in which page is mapped for determining reclaimability
54300501b53SJohannes Weiner  *
544b518154eSJoonsoo Kim  * Place @page on the inactive or unevictable LRU list, depending on its
54512eab428SMiaohe Lin  * evictability.
54600501b53SJohannes Weiner  */
547b518154eSJoonsoo Kim void lru_cache_add_inactive_or_unevictable(struct page *page,
54800501b53SJohannes Weiner 					 struct vm_area_struct *vma)
54900501b53SJohannes Weiner {
55000501b53SJohannes Weiner 	VM_BUG_ON_PAGE(PageLRU(page), page);
55100501b53SJohannes Weiner 
5522fbb0c10SHugh Dickins 	if (unlikely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED))
5532fbb0c10SHugh Dickins 		mlock_new_page(page);
5542fbb0c10SHugh Dickins 	else
5559c4e6b1aSShakeel Butt 		lru_cache_add(page);
55600501b53SJohannes Weiner }
55700501b53SJohannes Weiner 
558902aaed0SHisashi Hifumi /*
5597a3dbfe8SMatthew Wilcox (Oracle)  * If the folio cannot be invalidated, it is moved to the
56031560180SMinchan Kim  * inactive list to speed up its reclaim.  It is moved to the
56131560180SMinchan Kim  * head of the list, rather than the tail, to give the flusher
56231560180SMinchan Kim  * threads some time to write it out, as this is much more
56331560180SMinchan Kim  * effective than the single-page writeout from reclaim.
564278df9f4SMinchan Kim  *
5657a3dbfe8SMatthew Wilcox (Oracle)  * If the folio isn't mapped and dirty/writeback, the folio
5667a3dbfe8SMatthew Wilcox (Oracle)  * could be reclaimed asap using the reclaim flag.
567278df9f4SMinchan Kim  *
5687a3dbfe8SMatthew Wilcox (Oracle)  * 1. active, mapped folio -> none
5697a3dbfe8SMatthew Wilcox (Oracle)  * 2. active, dirty/writeback folio -> inactive, head, reclaim
5707a3dbfe8SMatthew Wilcox (Oracle)  * 3. inactive, mapped folio -> none
5717a3dbfe8SMatthew Wilcox (Oracle)  * 4. inactive, dirty/writeback folio -> inactive, head, reclaim
572278df9f4SMinchan Kim  * 5. inactive, clean -> inactive, tail
573278df9f4SMinchan Kim  * 6. Others -> none
574278df9f4SMinchan Kim  *
5757a3dbfe8SMatthew Wilcox (Oracle)  * In 4, it moves to the head of the inactive list so the folio is
5767a3dbfe8SMatthew Wilcox (Oracle)  * written out by flusher threads as this is much more efficient
577278df9f4SMinchan Kim  * than the single-page writeout from reclaim.
57831560180SMinchan Kim  */
5797a3dbfe8SMatthew Wilcox (Oracle) static void lru_deactivate_file_fn(struct lruvec *lruvec, struct folio *folio)
58031560180SMinchan Kim {
5817a3dbfe8SMatthew Wilcox (Oracle) 	bool active = folio_test_active(folio);
5827a3dbfe8SMatthew Wilcox (Oracle) 	long nr_pages = folio_nr_pages(folio);
58331560180SMinchan Kim 
5847a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_test_unevictable(folio))
585bad49d9cSMinchan Kim 		return;
586bad49d9cSMinchan Kim 
5877a3dbfe8SMatthew Wilcox (Oracle) 	/* Some processes are using the folio */
5887a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_mapped(folio))
58931560180SMinchan Kim 		return;
59031560180SMinchan Kim 
5917a3dbfe8SMatthew Wilcox (Oracle) 	lruvec_del_folio(lruvec, folio);
5927a3dbfe8SMatthew Wilcox (Oracle) 	folio_clear_active(folio);
5937a3dbfe8SMatthew Wilcox (Oracle) 	folio_clear_referenced(folio);
59431560180SMinchan Kim 
5957a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_test_writeback(folio) || folio_test_dirty(folio)) {
596278df9f4SMinchan Kim 		/*
5977a3dbfe8SMatthew Wilcox (Oracle) 		 * Setting the reclaim flag could race with
5987a3dbfe8SMatthew Wilcox (Oracle) 		 * folio_end_writeback() and confuse readahead.  But the
5997a3dbfe8SMatthew Wilcox (Oracle) 		 * race window is _really_ small and  it's not a critical
6007a3dbfe8SMatthew Wilcox (Oracle) 		 * problem.
601278df9f4SMinchan Kim 		 */
6027a3dbfe8SMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
6037a3dbfe8SMatthew Wilcox (Oracle) 		folio_set_reclaim(folio);
604278df9f4SMinchan Kim 	} else {
605278df9f4SMinchan Kim 		/*
6067a3dbfe8SMatthew Wilcox (Oracle) 		 * The folio's writeback ended while it was in the batch.
6077a3dbfe8SMatthew Wilcox (Oracle) 		 * We move that folio to the tail of the inactive list.
608278df9f4SMinchan Kim 		 */
6097a3dbfe8SMatthew Wilcox (Oracle) 		lruvec_add_folio_tail(lruvec, folio);
6105d91f31fSShakeel Butt 		__count_vm_events(PGROTATED, nr_pages);
611278df9f4SMinchan Kim 	}
612278df9f4SMinchan Kim 
61321e330fcSShakeel Butt 	if (active) {
6145d91f31fSShakeel Butt 		__count_vm_events(PGDEACTIVATE, nr_pages);
61521e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
61621e330fcSShakeel Butt 				     nr_pages);
61721e330fcSShakeel Butt 	}
61831560180SMinchan Kim }
61931560180SMinchan Kim 
62085cd7791SMatthew Wilcox (Oracle) static void lru_deactivate_fn(struct lruvec *lruvec, struct folio *folio)
6219c276cc6SMinchan Kim {
622ec1c86b2SYu Zhao 	if (!folio_test_unevictable(folio) && (folio_test_active(folio) || lru_gen_enabled())) {
62385cd7791SMatthew Wilcox (Oracle) 		long nr_pages = folio_nr_pages(folio);
6249c276cc6SMinchan Kim 
62585cd7791SMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
62685cd7791SMatthew Wilcox (Oracle) 		folio_clear_active(folio);
62785cd7791SMatthew Wilcox (Oracle) 		folio_clear_referenced(folio);
62885cd7791SMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
6299c276cc6SMinchan Kim 
63021e330fcSShakeel Butt 		__count_vm_events(PGDEACTIVATE, nr_pages);
63121e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
63221e330fcSShakeel Butt 				     nr_pages);
6339c276cc6SMinchan Kim 	}
6349c276cc6SMinchan Kim }
63510853a03SMinchan Kim 
636cec394baSMatthew Wilcox (Oracle) static void lru_lazyfree_fn(struct lruvec *lruvec, struct folio *folio)
63710853a03SMinchan Kim {
638cec394baSMatthew Wilcox (Oracle) 	if (folio_test_anon(folio) && folio_test_swapbacked(folio) &&
639cec394baSMatthew Wilcox (Oracle) 	    !folio_test_swapcache(folio) && !folio_test_unevictable(folio)) {
640cec394baSMatthew Wilcox (Oracle) 		long nr_pages = folio_nr_pages(folio);
64110853a03SMinchan Kim 
642cec394baSMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
643cec394baSMatthew Wilcox (Oracle) 		folio_clear_active(folio);
644cec394baSMatthew Wilcox (Oracle) 		folio_clear_referenced(folio);
645f7ad2a6cSShaohua Li 		/*
646cec394baSMatthew Wilcox (Oracle) 		 * Lazyfree folios are clean anonymous folios.  They have
647cec394baSMatthew Wilcox (Oracle) 		 * the swapbacked flag cleared, to distinguish them from normal
648cec394baSMatthew Wilcox (Oracle) 		 * anonymous folios
649f7ad2a6cSShaohua Li 		 */
650cec394baSMatthew Wilcox (Oracle) 		folio_clear_swapbacked(folio);
651cec394baSMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
65210853a03SMinchan Kim 
65321e330fcSShakeel Butt 		__count_vm_events(PGLAZYFREE, nr_pages);
65421e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE,
65521e330fcSShakeel Butt 				     nr_pages);
65610853a03SMinchan Kim 	}
65710853a03SMinchan Kim }
65810853a03SMinchan Kim 
65931560180SMinchan Kim /*
66082ac64d8SMatthew Wilcox (Oracle)  * Drain pages out of the cpu's folio_batch.
661902aaed0SHisashi Hifumi  * Either "cpu" is the current CPU, and preemption has already been
662902aaed0SHisashi Hifumi  * disabled; or "cpu" is being hot-unplugged, and is already dead.
663902aaed0SHisashi Hifumi  */
664f0cb3c76SKonstantin Khlebnikov void lru_add_drain_cpu(int cpu)
6651da177e4SLinus Torvalds {
666a2d33b5dSMatthew Wilcox (Oracle) 	struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
667a2d33b5dSMatthew Wilcox (Oracle) 	struct folio_batch *fbatch = &fbatches->lru_add;
6681da177e4SLinus Torvalds 
66970dea534SMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
67070dea534SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_add_fn);
671902aaed0SHisashi Hifumi 
672c2bc1681SMatthew Wilcox (Oracle) 	fbatch = &per_cpu(lru_rotate.fbatch, cpu);
6737e0cc01eSQian Cai 	/* Disabling interrupts below acts as a compiler barrier. */
674c2bc1681SMatthew Wilcox (Oracle) 	if (data_race(folio_batch_count(fbatch))) {
675902aaed0SHisashi Hifumi 		unsigned long flags;
676902aaed0SHisashi Hifumi 
677902aaed0SHisashi Hifumi 		/* No harm done if a racing interrupt already did this */
678b01b2141SIngo Molnar 		local_lock_irqsave(&lru_rotate.lock, flags);
679c2bc1681SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_move_tail_fn);
680b01b2141SIngo Molnar 		local_unlock_irqrestore(&lru_rotate.lock, flags);
681902aaed0SHisashi Hifumi 	}
68231560180SMinchan Kim 
683a2d33b5dSMatthew Wilcox (Oracle) 	fbatch = &fbatches->lru_deactivate_file;
6847a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
6857a3dbfe8SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_deactivate_file_fn);
686eb709b0dSShaohua Li 
687a2d33b5dSMatthew Wilcox (Oracle) 	fbatch = &fbatches->lru_deactivate;
68885cd7791SMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
68985cd7791SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_deactivate_fn);
6909c276cc6SMinchan Kim 
691a2d33b5dSMatthew Wilcox (Oracle) 	fbatch = &fbatches->lru_lazyfree;
692cec394baSMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
693cec394baSMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_lazyfree_fn);
69410853a03SMinchan Kim 
6953a44610bSMatthew Wilcox (Oracle) 	folio_activate_drain(cpu);
69631560180SMinchan Kim }
69731560180SMinchan Kim 
69831560180SMinchan Kim /**
6997a3dbfe8SMatthew Wilcox (Oracle)  * deactivate_file_folio() - Deactivate a file folio.
700261b6840SMatthew Wilcox (Oracle)  * @folio: Folio to deactivate.
70131560180SMinchan Kim  *
702261b6840SMatthew Wilcox (Oracle)  * This function hints to the VM that @folio is a good reclaim candidate,
703261b6840SMatthew Wilcox (Oracle)  * for example if its invalidation fails due to the folio being dirty
70431560180SMinchan Kim  * or under writeback.
705261b6840SMatthew Wilcox (Oracle)  *
7067a3dbfe8SMatthew Wilcox (Oracle)  * Context: Caller holds a reference on the folio.
70731560180SMinchan Kim  */
708261b6840SMatthew Wilcox (Oracle) void deactivate_file_folio(struct folio *folio)
70931560180SMinchan Kim {
7107a3dbfe8SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch;
711b01b2141SIngo Molnar 
7127a3dbfe8SMatthew Wilcox (Oracle) 	/* Deactivating an unevictable folio will not accelerate reclaim */
713261b6840SMatthew Wilcox (Oracle) 	if (folio_test_unevictable(folio))
714261b6840SMatthew Wilcox (Oracle) 		return;
715261b6840SMatthew Wilcox (Oracle) 
716261b6840SMatthew Wilcox (Oracle) 	folio_get(folio);
71782ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
71882ac64d8SMatthew Wilcox (Oracle) 	fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate_file);
7197a3dbfe8SMatthew Wilcox (Oracle) 	folio_batch_add_and_move(fbatch, folio, lru_deactivate_file_fn);
72082ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
72131560180SMinchan Kim }
72280bfed90SAndrew Morton 
7239c276cc6SMinchan Kim /*
7249c276cc6SMinchan Kim  * deactivate_page - deactivate a page
7259c276cc6SMinchan Kim  * @page: page to deactivate
7269c276cc6SMinchan Kim  *
7279c276cc6SMinchan Kim  * deactivate_page() moves @page to the inactive list if @page was on the active
7289c276cc6SMinchan Kim  * list and was not an unevictable page.  This is done to accelerate the reclaim
7299c276cc6SMinchan Kim  * of @page.
7309c276cc6SMinchan Kim  */
7319c276cc6SMinchan Kim void deactivate_page(struct page *page)
7329c276cc6SMinchan Kim {
73385cd7791SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
7349c276cc6SMinchan Kim 
735ec1c86b2SYu Zhao 	if (folio_test_lru(folio) && !folio_test_unevictable(folio) &&
736ec1c86b2SYu Zhao 	    (folio_test_active(folio) || lru_gen_enabled())) {
73785cd7791SMatthew Wilcox (Oracle) 		struct folio_batch *fbatch;
73885cd7791SMatthew Wilcox (Oracle) 
73985cd7791SMatthew Wilcox (Oracle) 		folio_get(folio);
74082ac64d8SMatthew Wilcox (Oracle) 		local_lock(&cpu_fbatches.lock);
74182ac64d8SMatthew Wilcox (Oracle) 		fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate);
74285cd7791SMatthew Wilcox (Oracle) 		folio_batch_add_and_move(fbatch, folio, lru_deactivate_fn);
74382ac64d8SMatthew Wilcox (Oracle) 		local_unlock(&cpu_fbatches.lock);
7449c276cc6SMinchan Kim 	}
7459c276cc6SMinchan Kim }
7469c276cc6SMinchan Kim 
74710853a03SMinchan Kim /**
748f7ad2a6cSShaohua Li  * mark_page_lazyfree - make an anon page lazyfree
74910853a03SMinchan Kim  * @page: page to deactivate
75010853a03SMinchan Kim  *
751f7ad2a6cSShaohua Li  * mark_page_lazyfree() moves @page to the inactive file list.
752f7ad2a6cSShaohua Li  * This is done to accelerate the reclaim of @page.
75310853a03SMinchan Kim  */
754f7ad2a6cSShaohua Li void mark_page_lazyfree(struct page *page)
75510853a03SMinchan Kim {
756cec394baSMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
75710853a03SMinchan Kim 
758cec394baSMatthew Wilcox (Oracle) 	if (folio_test_lru(folio) && folio_test_anon(folio) &&
759cec394baSMatthew Wilcox (Oracle) 	    folio_test_swapbacked(folio) && !folio_test_swapcache(folio) &&
760cec394baSMatthew Wilcox (Oracle) 	    !folio_test_unevictable(folio)) {
761cec394baSMatthew Wilcox (Oracle) 		struct folio_batch *fbatch;
762cec394baSMatthew Wilcox (Oracle) 
763cec394baSMatthew Wilcox (Oracle) 		folio_get(folio);
76482ac64d8SMatthew Wilcox (Oracle) 		local_lock(&cpu_fbatches.lock);
76582ac64d8SMatthew Wilcox (Oracle) 		fbatch = this_cpu_ptr(&cpu_fbatches.lru_lazyfree);
766cec394baSMatthew Wilcox (Oracle) 		folio_batch_add_and_move(fbatch, folio, lru_lazyfree_fn);
76782ac64d8SMatthew Wilcox (Oracle) 		local_unlock(&cpu_fbatches.lock);
76810853a03SMinchan Kim 	}
76910853a03SMinchan Kim }
77010853a03SMinchan Kim 
77180bfed90SAndrew Morton void lru_add_drain(void)
77280bfed90SAndrew Morton {
77382ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
774b01b2141SIngo Molnar 	lru_add_drain_cpu(smp_processor_id());
77582ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
776adb11e78SSebastian Andrzej Siewior 	mlock_page_drain_local();
777b01b2141SIngo Molnar }
778b01b2141SIngo Molnar 
779243418e3SMinchan Kim /*
780243418e3SMinchan Kim  * It's called from per-cpu workqueue context in SMP case so
781243418e3SMinchan Kim  * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on
782243418e3SMinchan Kim  * the same cpu. It shouldn't be a problem in !SMP case since
783243418e3SMinchan Kim  * the core is only one and the locks will disable preemption.
784243418e3SMinchan Kim  */
785243418e3SMinchan Kim static void lru_add_and_bh_lrus_drain(void)
786243418e3SMinchan Kim {
78782ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
788243418e3SMinchan Kim 	lru_add_drain_cpu(smp_processor_id());
78982ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
790243418e3SMinchan Kim 	invalidate_bh_lrus_cpu();
791adb11e78SSebastian Andrzej Siewior 	mlock_page_drain_local();
792243418e3SMinchan Kim }
793243418e3SMinchan Kim 
794b01b2141SIngo Molnar void lru_add_drain_cpu_zone(struct zone *zone)
795b01b2141SIngo Molnar {
79682ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
797b01b2141SIngo Molnar 	lru_add_drain_cpu(smp_processor_id());
798b01b2141SIngo Molnar 	drain_local_pages(zone);
79982ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
800adb11e78SSebastian Andrzej Siewior 	mlock_page_drain_local();
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds 
8036ea183d6SMichal Hocko #ifdef CONFIG_SMP
8046ea183d6SMichal Hocko 
8056ea183d6SMichal Hocko static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
8066ea183d6SMichal Hocko 
807c4028958SDavid Howells static void lru_add_drain_per_cpu(struct work_struct *dummy)
808053837fcSNick Piggin {
809243418e3SMinchan Kim 	lru_add_and_bh_lrus_drain();
810053837fcSNick Piggin }
811053837fcSNick Piggin 
8124864545aSMatthew Wilcox (Oracle) static bool cpu_needs_drain(unsigned int cpu)
8134864545aSMatthew Wilcox (Oracle) {
8144864545aSMatthew Wilcox (Oracle) 	struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
8154864545aSMatthew Wilcox (Oracle) 
8164864545aSMatthew Wilcox (Oracle) 	/* Check these in order of likelihood that they're not zero */
8174864545aSMatthew Wilcox (Oracle) 	return folio_batch_count(&fbatches->lru_add) ||
8184864545aSMatthew Wilcox (Oracle) 		data_race(folio_batch_count(&per_cpu(lru_rotate.fbatch, cpu))) ||
8194864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->lru_deactivate_file) ||
8204864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->lru_deactivate) ||
8214864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->lru_lazyfree) ||
8224864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->activate) ||
8234864545aSMatthew Wilcox (Oracle) 		need_mlock_page_drain(cpu) ||
8244864545aSMatthew Wilcox (Oracle) 		has_bh_in_lru(cpu, NULL);
8254864545aSMatthew Wilcox (Oracle) }
8264864545aSMatthew Wilcox (Oracle) 
8279852a721SMichal Hocko /*
8289852a721SMichal Hocko  * Doesn't need any cpu hotplug locking because we do rely on per-cpu
8299852a721SMichal Hocko  * kworkers being shut down before our page_alloc_cpu_dead callback is
8309852a721SMichal Hocko  * executed on the offlined cpu.
8319852a721SMichal Hocko  * Calling this function with cpu hotplug locks held can actually lead
8329852a721SMichal Hocko  * to obscure indirect dependencies via WQ context.
8339852a721SMichal Hocko  */
8343db3264dSMiaohe Lin static inline void __lru_add_drain_all(bool force_all_cpus)
835053837fcSNick Piggin {
8366446a513SAhmed S. Darwish 	/*
8376446a513SAhmed S. Darwish 	 * lru_drain_gen - Global pages generation number
8386446a513SAhmed S. Darwish 	 *
8396446a513SAhmed S. Darwish 	 * (A) Definition: global lru_drain_gen = x implies that all generations
8406446a513SAhmed S. Darwish 	 *     0 < n <= x are already *scheduled* for draining.
8416446a513SAhmed S. Darwish 	 *
8426446a513SAhmed S. Darwish 	 * This is an optimization for the highly-contended use case where a
8436446a513SAhmed S. Darwish 	 * user space workload keeps constantly generating a flow of pages for
8446446a513SAhmed S. Darwish 	 * each CPU.
8456446a513SAhmed S. Darwish 	 */
8466446a513SAhmed S. Darwish 	static unsigned int lru_drain_gen;
8475fbc4616SChris Metcalf 	static struct cpumask has_work;
8486446a513SAhmed S. Darwish 	static DEFINE_MUTEX(lock);
8496446a513SAhmed S. Darwish 	unsigned cpu, this_gen;
8505fbc4616SChris Metcalf 
851ce612879SMichal Hocko 	/*
852ce612879SMichal Hocko 	 * Make sure nobody triggers this path before mm_percpu_wq is fully
853ce612879SMichal Hocko 	 * initialized.
854ce612879SMichal Hocko 	 */
855ce612879SMichal Hocko 	if (WARN_ON(!mm_percpu_wq))
856ce612879SMichal Hocko 		return;
857ce612879SMichal Hocko 
8586446a513SAhmed S. Darwish 	/*
85982ac64d8SMatthew Wilcox (Oracle) 	 * Guarantee folio_batch counter stores visible by this CPU
86082ac64d8SMatthew Wilcox (Oracle) 	 * are visible to other CPUs before loading the current drain
86182ac64d8SMatthew Wilcox (Oracle) 	 * generation.
8626446a513SAhmed S. Darwish 	 */
8636446a513SAhmed S. Darwish 	smp_mb();
8646446a513SAhmed S. Darwish 
8656446a513SAhmed S. Darwish 	/*
8666446a513SAhmed S. Darwish 	 * (B) Locally cache global LRU draining generation number
8676446a513SAhmed S. Darwish 	 *
8686446a513SAhmed S. Darwish 	 * The read barrier ensures that the counter is loaded before the mutex
8696446a513SAhmed S. Darwish 	 * is taken. It pairs with smp_mb() inside the mutex critical section
8706446a513SAhmed S. Darwish 	 * at (D).
8716446a513SAhmed S. Darwish 	 */
8726446a513SAhmed S. Darwish 	this_gen = smp_load_acquire(&lru_drain_gen);
873eef1a429SKonstantin Khlebnikov 
8745fbc4616SChris Metcalf 	mutex_lock(&lock);
875eef1a429SKonstantin Khlebnikov 
876eef1a429SKonstantin Khlebnikov 	/*
8776446a513SAhmed S. Darwish 	 * (C) Exit the draining operation if a newer generation, from another
8786446a513SAhmed S. Darwish 	 * lru_add_drain_all(), was already scheduled for draining. Check (A).
879eef1a429SKonstantin Khlebnikov 	 */
880d479960eSMinchan Kim 	if (unlikely(this_gen != lru_drain_gen && !force_all_cpus))
881eef1a429SKonstantin Khlebnikov 		goto done;
882eef1a429SKonstantin Khlebnikov 
8836446a513SAhmed S. Darwish 	/*
8846446a513SAhmed S. Darwish 	 * (D) Increment global generation number
8856446a513SAhmed S. Darwish 	 *
8866446a513SAhmed S. Darwish 	 * Pairs with smp_load_acquire() at (B), outside of the critical
88782ac64d8SMatthew Wilcox (Oracle) 	 * section. Use a full memory barrier to guarantee that the
88882ac64d8SMatthew Wilcox (Oracle) 	 * new global drain generation number is stored before loading
88982ac64d8SMatthew Wilcox (Oracle) 	 * folio_batch counters.
8906446a513SAhmed S. Darwish 	 *
8916446a513SAhmed S. Darwish 	 * This pairing must be done here, before the for_each_online_cpu loop
8926446a513SAhmed S. Darwish 	 * below which drains the page vectors.
8936446a513SAhmed S. Darwish 	 *
8946446a513SAhmed S. Darwish 	 * Let x, y, and z represent some system CPU numbers, where x < y < z.
895cb152a1aSShijie Luo 	 * Assume CPU #z is in the middle of the for_each_online_cpu loop
8966446a513SAhmed S. Darwish 	 * below and has already reached CPU #y's per-cpu data. CPU #x comes
8976446a513SAhmed S. Darwish 	 * along, adds some pages to its per-cpu vectors, then calls
8986446a513SAhmed S. Darwish 	 * lru_add_drain_all().
8996446a513SAhmed S. Darwish 	 *
9006446a513SAhmed S. Darwish 	 * If the paired barrier is done at any later step, e.g. after the
9016446a513SAhmed S. Darwish 	 * loop, CPU #x will just exit at (C) and miss flushing out all of its
9026446a513SAhmed S. Darwish 	 * added pages.
9036446a513SAhmed S. Darwish 	 */
9046446a513SAhmed S. Darwish 	WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1);
9056446a513SAhmed S. Darwish 	smp_mb();
906eef1a429SKonstantin Khlebnikov 
9075fbc4616SChris Metcalf 	cpumask_clear(&has_work);
9085fbc4616SChris Metcalf 	for_each_online_cpu(cpu) {
9095fbc4616SChris Metcalf 		struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
9105fbc4616SChris Metcalf 
9114864545aSMatthew Wilcox (Oracle) 		if (cpu_needs_drain(cpu)) {
9125fbc4616SChris Metcalf 			INIT_WORK(work, lru_add_drain_per_cpu);
913ce612879SMichal Hocko 			queue_work_on(cpu, mm_percpu_wq, work);
9146446a513SAhmed S. Darwish 			__cpumask_set_cpu(cpu, &has_work);
9155fbc4616SChris Metcalf 		}
9165fbc4616SChris Metcalf 	}
9175fbc4616SChris Metcalf 
9185fbc4616SChris Metcalf 	for_each_cpu(cpu, &has_work)
9195fbc4616SChris Metcalf 		flush_work(&per_cpu(lru_add_drain_work, cpu));
9205fbc4616SChris Metcalf 
921eef1a429SKonstantin Khlebnikov done:
9225fbc4616SChris Metcalf 	mutex_unlock(&lock);
923053837fcSNick Piggin }
924d479960eSMinchan Kim 
925d479960eSMinchan Kim void lru_add_drain_all(void)
926d479960eSMinchan Kim {
927d479960eSMinchan Kim 	__lru_add_drain_all(false);
928d479960eSMinchan Kim }
9296ea183d6SMichal Hocko #else
9306ea183d6SMichal Hocko void lru_add_drain_all(void)
9316ea183d6SMichal Hocko {
9326ea183d6SMichal Hocko 	lru_add_drain();
9336ea183d6SMichal Hocko }
9346446a513SAhmed S. Darwish #endif /* CONFIG_SMP */
935053837fcSNick Piggin 
936d479960eSMinchan Kim atomic_t lru_disable_count = ATOMIC_INIT(0);
937d479960eSMinchan Kim 
938d479960eSMinchan Kim /*
939d479960eSMinchan Kim  * lru_cache_disable() needs to be called before we start compiling
940d479960eSMinchan Kim  * a list of pages to be migrated using isolate_lru_page().
941d479960eSMinchan Kim  * It drains pages on LRU cache and then disable on all cpus until
942d479960eSMinchan Kim  * lru_cache_enable is called.
943d479960eSMinchan Kim  *
944d479960eSMinchan Kim  * Must be paired with a call to lru_cache_enable().
945d479960eSMinchan Kim  */
946d479960eSMinchan Kim void lru_cache_disable(void)
947d479960eSMinchan Kim {
948d479960eSMinchan Kim 	atomic_inc(&lru_disable_count);
949d479960eSMinchan Kim 	/*
950ff042f4aSMarcelo Tosatti 	 * Readers of lru_disable_count are protected by either disabling
951ff042f4aSMarcelo Tosatti 	 * preemption or rcu_read_lock:
952ff042f4aSMarcelo Tosatti 	 *
953ff042f4aSMarcelo Tosatti 	 * preempt_disable, local_irq_disable  [bh_lru_lock()]
954ff042f4aSMarcelo Tosatti 	 * rcu_read_lock		       [rt_spin_lock CONFIG_PREEMPT_RT]
955ff042f4aSMarcelo Tosatti 	 * preempt_disable		       [local_lock !CONFIG_PREEMPT_RT]
956ff042f4aSMarcelo Tosatti 	 *
957ff042f4aSMarcelo Tosatti 	 * Since v5.1 kernel, synchronize_rcu() is guaranteed to wait on
958ff042f4aSMarcelo Tosatti 	 * preempt_disable() regions of code. So any CPU which sees
959ff042f4aSMarcelo Tosatti 	 * lru_disable_count = 0 will have exited the critical
960ff042f4aSMarcelo Tosatti 	 * section when synchronize_rcu() returns.
961d479960eSMinchan Kim 	 */
96231733463SMarcelo Tosatti 	synchronize_rcu_expedited();
963ff042f4aSMarcelo Tosatti #ifdef CONFIG_SMP
964d479960eSMinchan Kim 	__lru_add_drain_all(true);
965d479960eSMinchan Kim #else
966243418e3SMinchan Kim 	lru_add_and_bh_lrus_drain();
967d479960eSMinchan Kim #endif
968d479960eSMinchan Kim }
969d479960eSMinchan Kim 
970aabfb572SMichal Hocko /**
971ea1754a0SKirill A. Shutemov  * release_pages - batched put_page()
972aabfb572SMichal Hocko  * @pages: array of pages to release
973aabfb572SMichal Hocko  * @nr: number of pages
9741da177e4SLinus Torvalds  *
975aabfb572SMichal Hocko  * Decrement the reference count on all the pages in @pages.  If it
976aabfb572SMichal Hocko  * fell to zero, remove the page from the LRU and free it.
9771da177e4SLinus Torvalds  */
978c6f92f9fSMel Gorman void release_pages(struct page **pages, int nr)
9791da177e4SLinus Torvalds {
9801da177e4SLinus Torvalds 	int i;
981cc59850eSKonstantin Khlebnikov 	LIST_HEAD(pages_to_free);
9826168d0daSAlex Shi 	struct lruvec *lruvec = NULL;
9830de340cbSMatthew Wilcox (Oracle) 	unsigned long flags = 0;
9843f649ab7SKees Cook 	unsigned int lock_batch;
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	for (i = 0; i < nr; i++) {
987ab5e653eSMatthew Wilcox (Oracle) 		struct folio *folio = page_folio(pages[i]);
9881da177e4SLinus Torvalds 
989aabfb572SMichal Hocko 		/*
990aabfb572SMichal Hocko 		 * Make sure the IRQ-safe lock-holding time does not get
991aabfb572SMichal Hocko 		 * excessive with a continuous string of pages from the
9926168d0daSAlex Shi 		 * same lruvec. The lock is held only if lruvec != NULL.
993aabfb572SMichal Hocko 		 */
9946168d0daSAlex Shi 		if (lruvec && ++lock_batch == SWAP_CLUSTER_MAX) {
9956168d0daSAlex Shi 			unlock_page_lruvec_irqrestore(lruvec, flags);
9966168d0daSAlex Shi 			lruvec = NULL;
997aabfb572SMichal Hocko 		}
998aabfb572SMichal Hocko 
999ab5e653eSMatthew Wilcox (Oracle) 		if (is_huge_zero_page(&folio->page))
1000aa88b68cSKirill A. Shutemov 			continue;
1001aa88b68cSKirill A. Shutemov 
1002ab5e653eSMatthew Wilcox (Oracle) 		if (folio_is_zone_device(folio)) {
10036168d0daSAlex Shi 			if (lruvec) {
10046168d0daSAlex Shi 				unlock_page_lruvec_irqrestore(lruvec, flags);
10056168d0daSAlex Shi 				lruvec = NULL;
1006df6ad698SJérôme Glisse 			}
1007ab5e653eSMatthew Wilcox (Oracle) 			if (put_devmap_managed_page(&folio->page))
1008df6ad698SJérôme Glisse 				continue;
1009ab5e653eSMatthew Wilcox (Oracle) 			if (folio_put_testzero(folio))
1010ab5e653eSMatthew Wilcox (Oracle) 				free_zone_device_page(&folio->page);
101143fbdeb3SRalph Campbell 			continue;
101207d80269SJohn Hubbard 		}
1013df6ad698SJérôme Glisse 
1014ab5e653eSMatthew Wilcox (Oracle) 		if (!folio_put_testzero(folio))
10151da177e4SLinus Torvalds 			continue;
10161da177e4SLinus Torvalds 
1017ab5e653eSMatthew Wilcox (Oracle) 		if (folio_test_large(folio)) {
10186168d0daSAlex Shi 			if (lruvec) {
10196168d0daSAlex Shi 				unlock_page_lruvec_irqrestore(lruvec, flags);
10206168d0daSAlex Shi 				lruvec = NULL;
1021ddc58f27SKirill A. Shutemov 			}
10225ef82fe7SMatthew Wilcox (Oracle) 			__folio_put_large(folio);
1023ddc58f27SKirill A. Shutemov 			continue;
1024ddc58f27SKirill A. Shutemov 		}
1025ddc58f27SKirill A. Shutemov 
1026ab5e653eSMatthew Wilcox (Oracle) 		if (folio_test_lru(folio)) {
10272a5e4e34SAlexander Duyck 			struct lruvec *prev_lruvec = lruvec;
1028894bc310SLee Schermerhorn 
10290de340cbSMatthew Wilcox (Oracle) 			lruvec = folio_lruvec_relock_irqsave(folio, lruvec,
10302a5e4e34SAlexander Duyck 									&flags);
10312a5e4e34SAlexander Duyck 			if (prev_lruvec != lruvec)
1032aabfb572SMichal Hocko 				lock_batch = 0;
1033fa9add64SHugh Dickins 
1034ab5e653eSMatthew Wilcox (Oracle) 			lruvec_del_folio(lruvec, folio);
1035ab5e653eSMatthew Wilcox (Oracle) 			__folio_clear_lru_flags(folio);
103646453a6eSNick Piggin 		}
103746453a6eSNick Piggin 
1038b109b870SHugh Dickins 		/*
1039b109b870SHugh Dickins 		 * In rare cases, when truncation or holepunching raced with
1040b109b870SHugh Dickins 		 * munlock after VM_LOCKED was cleared, Mlocked may still be
1041b109b870SHugh Dickins 		 * found set here.  This does not indicate a problem, unless
1042b109b870SHugh Dickins 		 * "unevictable_pgs_cleared" appears worryingly large.
1043b109b870SHugh Dickins 		 */
1044ab5e653eSMatthew Wilcox (Oracle) 		if (unlikely(folio_test_mlocked(folio))) {
1045ab5e653eSMatthew Wilcox (Oracle) 			__folio_clear_mlocked(folio);
1046ab5e653eSMatthew Wilcox (Oracle) 			zone_stat_sub_folio(folio, NR_MLOCK);
1047b109b870SHugh Dickins 			count_vm_event(UNEVICTABLE_PGCLEARED);
1048b109b870SHugh Dickins 		}
1049b109b870SHugh Dickins 
1050ab5e653eSMatthew Wilcox (Oracle) 		list_add(&folio->lru, &pages_to_free);
10511da177e4SLinus Torvalds 	}
10526168d0daSAlex Shi 	if (lruvec)
10536168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
10541da177e4SLinus Torvalds 
1055747db954SJohannes Weiner 	mem_cgroup_uncharge_list(&pages_to_free);
10562d4894b5SMel Gorman 	free_unref_page_list(&pages_to_free);
10571da177e4SLinus Torvalds }
10580be8557bSMiklos Szeredi EXPORT_SYMBOL(release_pages);
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds /*
10611da177e4SLinus Torvalds  * The pages which we're about to release may be in the deferred lru-addition
10621da177e4SLinus Torvalds  * queues.  That would prevent them from really being freed right now.  That's
10631da177e4SLinus Torvalds  * OK from a correctness point of view but is inefficient - those pages may be
10641da177e4SLinus Torvalds  * cache-warm and we want to give them back to the page allocator ASAP.
10651da177e4SLinus Torvalds  *
106670dea534SMatthew Wilcox (Oracle)  * So __pagevec_release() will drain those queues here.
106770dea534SMatthew Wilcox (Oracle)  * folio_batch_move_lru() calls folios_put() directly to avoid
10681da177e4SLinus Torvalds  * mutual recursion.
10691da177e4SLinus Torvalds  */
10701da177e4SLinus Torvalds void __pagevec_release(struct pagevec *pvec)
10711da177e4SLinus Torvalds {
10727f0b5fb9SMel Gorman 	if (!pvec->percpu_pvec_drained) {
10731da177e4SLinus Torvalds 		lru_add_drain();
10747f0b5fb9SMel Gorman 		pvec->percpu_pvec_drained = true;
1075d9ed0d08SMel Gorman 	}
1076c6f92f9fSMel Gorman 	release_pages(pvec->pages, pagevec_count(pvec));
10771da177e4SLinus Torvalds 	pagevec_reinit(pvec);
10781da177e4SLinus Torvalds }
10797f285701SSteve French EXPORT_SYMBOL(__pagevec_release);
10807f285701SSteve French 
10811da177e4SLinus Torvalds /**
10821613fac9SMatthew Wilcox (Oracle)  * folio_batch_remove_exceptionals() - Prune non-folios from a batch.
10831613fac9SMatthew Wilcox (Oracle)  * @fbatch: The batch to prune
10840cd6144aSJohannes Weiner  *
10851613fac9SMatthew Wilcox (Oracle)  * find_get_entries() fills a batch with both folios and shadow/swap/DAX
10861613fac9SMatthew Wilcox (Oracle)  * entries.  This function prunes all the non-folio entries from @fbatch
10871613fac9SMatthew Wilcox (Oracle)  * without leaving holes, so that it can be passed on to folio-only batch
10881613fac9SMatthew Wilcox (Oracle)  * operations.
10890cd6144aSJohannes Weiner  */
10901613fac9SMatthew Wilcox (Oracle) void folio_batch_remove_exceptionals(struct folio_batch *fbatch)
10910cd6144aSJohannes Weiner {
10921613fac9SMatthew Wilcox (Oracle) 	unsigned int i, j;
10930cd6144aSJohannes Weiner 
10941613fac9SMatthew Wilcox (Oracle) 	for (i = 0, j = 0; i < folio_batch_count(fbatch); i++) {
10951613fac9SMatthew Wilcox (Oracle) 		struct folio *folio = fbatch->folios[i];
10961613fac9SMatthew Wilcox (Oracle) 		if (!xa_is_value(folio))
10971613fac9SMatthew Wilcox (Oracle) 			fbatch->folios[j++] = folio;
10980cd6144aSJohannes Weiner 	}
10991613fac9SMatthew Wilcox (Oracle) 	fbatch->nr = j;
11000cd6144aSJohannes Weiner }
11010cd6144aSJohannes Weiner 
110272b045aeSJan Kara unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
110372b045aeSJan Kara 		struct address_space *mapping, pgoff_t *index, pgoff_t end,
110410bbd235SMatthew Wilcox 		xa_mark_t tag)
11051da177e4SLinus Torvalds {
110672b045aeSJan Kara 	pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
110767fd707fSJan Kara 					PAGEVEC_SIZE, pvec->pages);
11081da177e4SLinus Torvalds 	return pagevec_count(pvec);
11091da177e4SLinus Torvalds }
111072b045aeSJan Kara EXPORT_SYMBOL(pagevec_lookup_range_tag);
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds /*
11131da177e4SLinus Torvalds  * Perform any setup for the swap system
11141da177e4SLinus Torvalds  */
11151da177e4SLinus Torvalds void __init swap_setup(void)
11161da177e4SLinus Torvalds {
1117ca79b0c2SArun KS 	unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
1118e0bf68ddSPeter Zijlstra 
11191da177e4SLinus Torvalds 	/* Use a smaller cluster for small-memory machines */
11201da177e4SLinus Torvalds 	if (megs < 16)
11211da177e4SLinus Torvalds 		page_cluster = 2;
11221da177e4SLinus Torvalds 	else
11231da177e4SLinus Torvalds 		page_cluster = 3;
11241da177e4SLinus Torvalds 	/*
11251da177e4SLinus Torvalds 	 * Right now other parts of the system means that we
11261da177e4SLinus Torvalds 	 * _really_ don't want to cluster much more
11271da177e4SLinus Torvalds 	 */
11281da177e4SLinus Torvalds }
1129