xref: /linux/mm/swap.c (revision 96f97c438f61ddba94117dcd1a1eb0aaafa22309)
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
79b109b870SHugh Dickins  * via pagevecs.  But it gets used by networking - and for compound pages.
80b221385bSAdrian Bunk  */
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 	}
92188e8caeSMatthew Wilcox (Oracle) 	/* See comment on folio_test_mlocked in release_pages() */
93188e8caeSMatthew Wilcox (Oracle) 	if (unlikely(folio_test_mlocked(folio))) {
94188e8caeSMatthew Wilcox (Oracle) 		long nr_pages = folio_nr_pages(folio);
95b109b870SHugh Dickins 
96188e8caeSMatthew Wilcox (Oracle) 		__folio_clear_mlocked(folio);
97188e8caeSMatthew Wilcox (Oracle) 		zone_stat_mod_folio(folio, NR_MLOCK, -nr_pages);
98b109b870SHugh Dickins 		count_vm_events(UNEVICTABLE_PGCLEARED, nr_pages);
99b109b870SHugh Dickins 	}
10091807063SAndrea Arcangeli }
10191807063SAndrea Arcangeli 
10283d99659SMatthew Wilcox (Oracle) static void __folio_put_small(struct folio *folio)
10391807063SAndrea Arcangeli {
104188e8caeSMatthew Wilcox (Oracle) 	__page_cache_release(folio);
10583d99659SMatthew Wilcox (Oracle) 	mem_cgroup_uncharge(folio);
10683d99659SMatthew Wilcox (Oracle) 	free_unref_page(&folio->page, 0);
107b221385bSAdrian Bunk }
108b221385bSAdrian Bunk 
1095ef82fe7SMatthew Wilcox (Oracle) static void __folio_put_large(struct folio *folio)
11091807063SAndrea Arcangeli {
111822fc613SNaoya Horiguchi 	/*
112822fc613SNaoya Horiguchi 	 * __page_cache_release() is supposed to be called for thp, not for
113822fc613SNaoya Horiguchi 	 * hugetlb. This is because hugetlb page does never have PageLRU set
114822fc613SNaoya Horiguchi 	 * (it's never listed to any LRU lists) and no memcg routines should
115822fc613SNaoya Horiguchi 	 * be called for hugetlb (it has a separate hugetlb_cgroup.)
116822fc613SNaoya Horiguchi 	 */
1175ef82fe7SMatthew Wilcox (Oracle) 	if (!folio_test_hugetlb(folio))
118188e8caeSMatthew Wilcox (Oracle) 		__page_cache_release(folio);
1195375336cSMatthew Wilcox (Oracle) 	destroy_large_folio(folio);
12091807063SAndrea Arcangeli }
12191807063SAndrea Arcangeli 
1228d29c703SMatthew Wilcox (Oracle) void __folio_put(struct folio *folio)
123c747ce79SJianyu Zhan {
1248d29c703SMatthew Wilcox (Oracle) 	if (unlikely(folio_is_zone_device(folio)))
1258d29c703SMatthew Wilcox (Oracle) 		free_zone_device_page(&folio->page);
1268d29c703SMatthew Wilcox (Oracle) 	else if (unlikely(folio_test_large(folio)))
1275ef82fe7SMatthew Wilcox (Oracle) 		__folio_put_large(folio);
12826296ad2SAndrew Morton 	else
12983d99659SMatthew Wilcox (Oracle) 		__folio_put_small(folio);
13026296ad2SAndrew Morton }
1318d29c703SMatthew Wilcox (Oracle) EXPORT_SYMBOL(__folio_put);
13270b50f94SAndrea Arcangeli 
1331d7ea732SAlexander Zarochentsev /**
1347682486bSRandy Dunlap  * put_pages_list() - release a list of pages
1357682486bSRandy Dunlap  * @pages: list of pages threaded on page->lru
1361d7ea732SAlexander Zarochentsev  *
137988c69f1SMatthew Wilcox (Oracle)  * Release a list of pages which are strung together on page.lru.
1381d7ea732SAlexander Zarochentsev  */
1391d7ea732SAlexander Zarochentsev void put_pages_list(struct list_head *pages)
1401d7ea732SAlexander Zarochentsev {
1412f58e5deSMatthew Wilcox (Oracle) 	struct folio *folio, *next;
1421d7ea732SAlexander Zarochentsev 
1432f58e5deSMatthew Wilcox (Oracle) 	list_for_each_entry_safe(folio, next, pages, lru) {
1442f58e5deSMatthew Wilcox (Oracle) 		if (!folio_put_testzero(folio)) {
1452f58e5deSMatthew Wilcox (Oracle) 			list_del(&folio->lru);
146988c69f1SMatthew Wilcox (Oracle) 			continue;
1471d7ea732SAlexander Zarochentsev 		}
1482f58e5deSMatthew Wilcox (Oracle) 		if (folio_test_large(folio)) {
1492f58e5deSMatthew Wilcox (Oracle) 			list_del(&folio->lru);
1505ef82fe7SMatthew Wilcox (Oracle) 			__folio_put_large(folio);
151988c69f1SMatthew Wilcox (Oracle) 			continue;
152988c69f1SMatthew Wilcox (Oracle) 		}
1532f58e5deSMatthew Wilcox (Oracle) 		/* LRU flag must be clear because it's passed using the lru */
154988c69f1SMatthew Wilcox (Oracle) 	}
155988c69f1SMatthew Wilcox (Oracle) 
156988c69f1SMatthew Wilcox (Oracle) 	free_unref_page_list(pages);
1573cd018b4SMatthew Wilcox 	INIT_LIST_HEAD(pages);
1581d7ea732SAlexander Zarochentsev }
1591d7ea732SAlexander Zarochentsev EXPORT_SYMBOL(put_pages_list);
1601d7ea732SAlexander Zarochentsev 
16118022c5dSMel Gorman /*
16218022c5dSMel Gorman  * get_kernel_pages() - pin kernel pages in memory
16318022c5dSMel Gorman  * @kiov:	An array of struct kvec structures
16418022c5dSMel Gorman  * @nr_segs:	number of segments to pin
16518022c5dSMel Gorman  * @write:	pinning for read/write, currently ignored
16618022c5dSMel Gorman  * @pages:	array that receives pointers to the pages pinned.
16718022c5dSMel Gorman  *		Should be at least nr_segs long.
16818022c5dSMel Gorman  *
169133d2743SMiaohe Lin  * Returns number of pages pinned. This may be fewer than the number requested.
170133d2743SMiaohe Lin  * If nr_segs is 0 or negative, returns 0.  If no pages were pinned, returns 0.
171133d2743SMiaohe Lin  * Each page returned must be released with a put_page() call when it is
172133d2743SMiaohe Lin  * finished with.
17318022c5dSMel Gorman  */
17418022c5dSMel Gorman int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
17518022c5dSMel Gorman 		struct page **pages)
17618022c5dSMel Gorman {
17718022c5dSMel Gorman 	int seg;
17818022c5dSMel Gorman 
17918022c5dSMel Gorman 	for (seg = 0; seg < nr_segs; seg++) {
18018022c5dSMel Gorman 		if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
18118022c5dSMel Gorman 			return seg;
18218022c5dSMel Gorman 
1835a178119SMel Gorman 		pages[seg] = kmap_to_page(kiov[seg].iov_base);
18409cbfeafSKirill A. Shutemov 		get_page(pages[seg]);
18518022c5dSMel Gorman 	}
18618022c5dSMel Gorman 
18718022c5dSMel Gorman 	return seg;
18818022c5dSMel Gorman }
18918022c5dSMel Gorman EXPORT_SYMBOL_GPL(get_kernel_pages);
19018022c5dSMel Gorman 
191c2bc1681SMatthew Wilcox (Oracle) typedef void (*move_fn_t)(struct lruvec *lruvec, struct folio *folio);
192c2bc1681SMatthew Wilcox (Oracle) 
19370dea534SMatthew Wilcox (Oracle) static void lru_add_fn(struct lruvec *lruvec, struct folio *folio)
1947d80dd09SMatthew Wilcox (Oracle) {
1957d80dd09SMatthew Wilcox (Oracle) 	int was_unevictable = folio_test_clear_unevictable(folio);
1967d80dd09SMatthew Wilcox (Oracle) 	long nr_pages = folio_nr_pages(folio);
1977d80dd09SMatthew Wilcox (Oracle) 
1987d80dd09SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
1997d80dd09SMatthew Wilcox (Oracle) 
2007d80dd09SMatthew Wilcox (Oracle) 	/*
2017d80dd09SMatthew Wilcox (Oracle) 	 * Is an smp_mb__after_atomic() still required here, before
202188e8caeSMatthew Wilcox (Oracle) 	 * folio_evictable() tests the mlocked flag, to rule out the possibility
2037d80dd09SMatthew Wilcox (Oracle) 	 * of stranding an evictable folio on an unevictable LRU?  I think
204188e8caeSMatthew Wilcox (Oracle) 	 * not, because __munlock_page() only clears the mlocked flag
205188e8caeSMatthew Wilcox (Oracle) 	 * while the LRU lock is held.
2067d80dd09SMatthew Wilcox (Oracle) 	 *
2077d80dd09SMatthew Wilcox (Oracle) 	 * (That is not true of __page_cache_release(), and not necessarily
208188e8caeSMatthew Wilcox (Oracle) 	 * true of release_pages(): but those only clear the mlocked flag after
209188e8caeSMatthew Wilcox (Oracle) 	 * folio_put_testzero() has excluded any other users of the folio.)
2107d80dd09SMatthew Wilcox (Oracle) 	 */
2117d80dd09SMatthew Wilcox (Oracle) 	if (folio_evictable(folio)) {
2127d80dd09SMatthew Wilcox (Oracle) 		if (was_unevictable)
2137d80dd09SMatthew Wilcox (Oracle) 			__count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages);
2147d80dd09SMatthew Wilcox (Oracle) 	} else {
2157d80dd09SMatthew Wilcox (Oracle) 		folio_clear_active(folio);
2167d80dd09SMatthew Wilcox (Oracle) 		folio_set_unevictable(folio);
2177d80dd09SMatthew Wilcox (Oracle) 		/*
2187d80dd09SMatthew Wilcox (Oracle) 		 * folio->mlock_count = !!folio_test_mlocked(folio)?
2197d80dd09SMatthew Wilcox (Oracle) 		 * But that leaves __mlock_page() in doubt whether another
2207d80dd09SMatthew Wilcox (Oracle) 		 * actor has already counted the mlock or not.  Err on the
2217d80dd09SMatthew Wilcox (Oracle) 		 * safe side, underestimate, let page reclaim fix it, rather
2227d80dd09SMatthew Wilcox (Oracle) 		 * than leaving a page on the unevictable LRU indefinitely.
2237d80dd09SMatthew Wilcox (Oracle) 		 */
2247d80dd09SMatthew Wilcox (Oracle) 		folio->mlock_count = 0;
2257d80dd09SMatthew Wilcox (Oracle) 		if (!was_unevictable)
2267d80dd09SMatthew Wilcox (Oracle) 			__count_vm_events(UNEVICTABLE_PGCULLED, nr_pages);
2277d80dd09SMatthew Wilcox (Oracle) 	}
2287d80dd09SMatthew Wilcox (Oracle) 
2297d80dd09SMatthew Wilcox (Oracle) 	lruvec_add_folio(lruvec, folio);
2307d80dd09SMatthew Wilcox (Oracle) 	trace_mm_lru_insertion(folio);
2317d80dd09SMatthew Wilcox (Oracle) }
2327d80dd09SMatthew Wilcox (Oracle) 
233c2bc1681SMatthew Wilcox (Oracle) static void folio_batch_move_lru(struct folio_batch *fbatch, move_fn_t move_fn)
234902aaed0SHisashi Hifumi {
235902aaed0SHisashi Hifumi 	int i;
2366168d0daSAlex Shi 	struct lruvec *lruvec = NULL;
2373dd7ae8eSShaohua Li 	unsigned long flags = 0;
238902aaed0SHisashi Hifumi 
239c2bc1681SMatthew Wilcox (Oracle) 	for (i = 0; i < folio_batch_count(fbatch); i++) {
240c2bc1681SMatthew Wilcox (Oracle) 		struct folio *folio = fbatch->folios[i];
2413dd7ae8eSShaohua Li 
242c2bc1681SMatthew Wilcox (Oracle) 		/* block memcg migration while the folio moves between lru */
24370dea534SMatthew Wilcox (Oracle) 		if (move_fn != lru_add_fn && !folio_test_clear_lru(folio))
244fc574c23SAlex Shi 			continue;
245fc574c23SAlex Shi 
2460de340cbSMatthew Wilcox (Oracle) 		lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags);
247c2bc1681SMatthew Wilcox (Oracle) 		move_fn(lruvec, folio);
248fc574c23SAlex Shi 
249c2bc1681SMatthew Wilcox (Oracle) 		folio_set_lru(folio);
2503dd7ae8eSShaohua Li 	}
251c2bc1681SMatthew Wilcox (Oracle) 
2526168d0daSAlex Shi 	if (lruvec)
2536168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
254c2bc1681SMatthew Wilcox (Oracle) 	folios_put(fbatch->folios, folio_batch_count(fbatch));
255c2bc1681SMatthew Wilcox (Oracle) 	folio_batch_init(fbatch);
2563dd7ae8eSShaohua Li }
2573dd7ae8eSShaohua Li 
258c2bc1681SMatthew Wilcox (Oracle) static void folio_batch_add_and_move(struct folio_batch *fbatch,
259c2bc1681SMatthew Wilcox (Oracle) 		struct folio *folio, move_fn_t move_fn)
2603dd7ae8eSShaohua Li {
261c2bc1681SMatthew Wilcox (Oracle) 	if (folio_batch_add(fbatch, folio) && !folio_test_large(folio) &&
262c2bc1681SMatthew Wilcox (Oracle) 	    !lru_cache_disabled())
263c2bc1681SMatthew Wilcox (Oracle) 		return;
264c2bc1681SMatthew Wilcox (Oracle) 	folio_batch_move_lru(fbatch, move_fn);
265c2bc1681SMatthew Wilcox (Oracle) }
266575ced1cSMatthew Wilcox (Oracle) 
267c2bc1681SMatthew Wilcox (Oracle) static void lru_move_tail_fn(struct lruvec *lruvec, struct folio *folio)
268c2bc1681SMatthew Wilcox (Oracle) {
269575ced1cSMatthew Wilcox (Oracle) 	if (!folio_test_unevictable(folio)) {
270575ced1cSMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
271575ced1cSMatthew Wilcox (Oracle) 		folio_clear_active(folio);
272575ced1cSMatthew Wilcox (Oracle) 		lruvec_add_folio_tail(lruvec, folio);
273575ced1cSMatthew Wilcox (Oracle) 		__count_vm_events(PGROTATED, folio_nr_pages(folio));
274902aaed0SHisashi Hifumi 	}
275902aaed0SHisashi Hifumi }
2763dd7ae8eSShaohua Li 
2773dd7ae8eSShaohua Li /*
278575ced1cSMatthew Wilcox (Oracle)  * Writeback is about to end against a folio which has been marked for
279575ced1cSMatthew Wilcox (Oracle)  * immediate reclaim.  If it still appears to be reclaimable, move it
280575ced1cSMatthew Wilcox (Oracle)  * to the tail of the inactive list.
281c7c7b80cSAlex Shi  *
282575ced1cSMatthew Wilcox (Oracle)  * folio_rotate_reclaimable() must disable IRQs, to prevent nasty races.
2831da177e4SLinus Torvalds  */
284575ced1cSMatthew Wilcox (Oracle) void folio_rotate_reclaimable(struct folio *folio)
2851da177e4SLinus Torvalds {
286575ced1cSMatthew Wilcox (Oracle) 	if (!folio_test_locked(folio) && !folio_test_dirty(folio) &&
287575ced1cSMatthew Wilcox (Oracle) 	    !folio_test_unevictable(folio) && folio_test_lru(folio)) {
288c2bc1681SMatthew Wilcox (Oracle) 		struct folio_batch *fbatch;
2891da177e4SLinus Torvalds 		unsigned long flags;
2901da177e4SLinus Torvalds 
291575ced1cSMatthew Wilcox (Oracle) 		folio_get(folio);
292b01b2141SIngo Molnar 		local_lock_irqsave(&lru_rotate.lock, flags);
293c2bc1681SMatthew Wilcox (Oracle) 		fbatch = this_cpu_ptr(&lru_rotate.fbatch);
294c2bc1681SMatthew Wilcox (Oracle) 		folio_batch_add_and_move(fbatch, folio, lru_move_tail_fn);
295b01b2141SIngo Molnar 		local_unlock_irqrestore(&lru_rotate.lock, flags);
296ac6aadb2SMiklos Szeredi 	}
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2990538a82cSJohannes Weiner void lru_note_cost(struct lruvec *lruvec, bool file,
3000538a82cSJohannes Weiner 		   unsigned int nr_io, unsigned int nr_rotated)
3013e2f41f1SKOSAKI Motohiro {
3020538a82cSJohannes Weiner 	unsigned long cost;
3030538a82cSJohannes Weiner 
3040538a82cSJohannes Weiner 	/*
3050538a82cSJohannes Weiner 	 * Reflect the relative cost of incurring IO and spending CPU
3060538a82cSJohannes Weiner 	 * time on rotations. This doesn't attempt to make a precise
3070538a82cSJohannes Weiner 	 * comparison, it just says: if reloads are about comparable
3080538a82cSJohannes Weiner 	 * between the LRU lists, or rotations are overwhelmingly
3090538a82cSJohannes Weiner 	 * different between them, adjust scan balance for CPU work.
3100538a82cSJohannes Weiner 	 */
3110538a82cSJohannes Weiner 	cost = nr_io * SWAP_CLUSTER_MAX + nr_rotated;
3120538a82cSJohannes Weiner 
3137cf111bcSJohannes Weiner 	do {
3147cf111bcSJohannes Weiner 		unsigned long lrusize;
3157cf111bcSJohannes Weiner 
3166168d0daSAlex Shi 		/*
3176168d0daSAlex Shi 		 * Hold lruvec->lru_lock is safe here, since
3186168d0daSAlex Shi 		 * 1) The pinned lruvec in reclaim, or
3196168d0daSAlex Shi 		 * 2) From a pre-LRU page during refault (which also holds the
3206168d0daSAlex Shi 		 *    rcu lock, so would be safe even if the page was on the LRU
3216168d0daSAlex Shi 		 *    and could move simultaneously to a new lruvec).
3226168d0daSAlex Shi 		 */
3236168d0daSAlex Shi 		spin_lock_irq(&lruvec->lru_lock);
3247cf111bcSJohannes Weiner 		/* Record cost event */
32596f8bf4fSJohannes Weiner 		if (file)
3260538a82cSJohannes Weiner 			lruvec->file_cost += cost;
3271431d4d1SJohannes Weiner 		else
3280538a82cSJohannes Weiner 			lruvec->anon_cost += cost;
3297cf111bcSJohannes Weiner 
3307cf111bcSJohannes Weiner 		/*
3317cf111bcSJohannes Weiner 		 * Decay previous events
3327cf111bcSJohannes Weiner 		 *
3337cf111bcSJohannes Weiner 		 * Because workloads change over time (and to avoid
3347cf111bcSJohannes Weiner 		 * overflow) we keep these statistics as a floating
3357cf111bcSJohannes Weiner 		 * average, which ends up weighing recent refaults
3367cf111bcSJohannes Weiner 		 * more than old ones.
3377cf111bcSJohannes Weiner 		 */
3387cf111bcSJohannes Weiner 		lrusize = lruvec_page_state(lruvec, NR_INACTIVE_ANON) +
3397cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_ACTIVE_ANON) +
3407cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_INACTIVE_FILE) +
3417cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_ACTIVE_FILE);
3427cf111bcSJohannes Weiner 
3437cf111bcSJohannes Weiner 		if (lruvec->file_cost + lruvec->anon_cost > lrusize / 4) {
3447cf111bcSJohannes Weiner 			lruvec->file_cost /= 2;
3457cf111bcSJohannes Weiner 			lruvec->anon_cost /= 2;
3467cf111bcSJohannes Weiner 		}
3476168d0daSAlex Shi 		spin_unlock_irq(&lruvec->lru_lock);
3487cf111bcSJohannes Weiner 	} while ((lruvec = parent_lruvec(lruvec)));
3493e2f41f1SKOSAKI Motohiro }
3503e2f41f1SKOSAKI Motohiro 
3510538a82cSJohannes Weiner void lru_note_cost_refault(struct folio *folio)
35296f8bf4fSJohannes Weiner {
3530995d7e5SMatthew Wilcox (Oracle) 	lru_note_cost(folio_lruvec(folio), folio_is_file_lru(folio),
3540538a82cSJohannes Weiner 		      folio_nr_pages(folio), 0);
35596f8bf4fSJohannes Weiner }
35696f8bf4fSJohannes Weiner 
3573a44610bSMatthew Wilcox (Oracle) static void folio_activate_fn(struct lruvec *lruvec, struct folio *folio)
358744ed144SShaohua Li {
359f2d27392SMatthew Wilcox (Oracle) 	if (!folio_test_active(folio) && !folio_test_unevictable(folio)) {
360f2d27392SMatthew Wilcox (Oracle) 		long nr_pages = folio_nr_pages(folio);
361744ed144SShaohua Li 
362f2d27392SMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
363f2d27392SMatthew Wilcox (Oracle) 		folio_set_active(folio);
364f2d27392SMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
365f2d27392SMatthew Wilcox (Oracle) 		trace_mm_lru_activate(folio);
3667a608572SLinus Torvalds 
36721e330fcSShakeel Butt 		__count_vm_events(PGACTIVATE, nr_pages);
36821e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGACTIVATE,
36921e330fcSShakeel Butt 				     nr_pages);
370744ed144SShaohua Li 	}
371eb709b0dSShaohua Li }
372eb709b0dSShaohua Li 
373eb709b0dSShaohua Li #ifdef CONFIG_SMP
3743a44610bSMatthew Wilcox (Oracle) static void folio_activate_drain(int cpu)
375f2d27392SMatthew Wilcox (Oracle) {
37682ac64d8SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch = &per_cpu(cpu_fbatches.activate, cpu);
377f2d27392SMatthew Wilcox (Oracle) 
3783a44610bSMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
3793a44610bSMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, folio_activate_fn);
3805fbc4616SChris Metcalf }
3815fbc4616SChris Metcalf 
382018ee47fSYu Zhao void folio_activate(struct folio *folio)
383eb709b0dSShaohua Li {
384f2d27392SMatthew Wilcox (Oracle) 	if (folio_test_lru(folio) && !folio_test_active(folio) &&
385f2d27392SMatthew Wilcox (Oracle) 	    !folio_test_unevictable(folio)) {
3863a44610bSMatthew Wilcox (Oracle) 		struct folio_batch *fbatch;
387eb709b0dSShaohua Li 
388f2d27392SMatthew Wilcox (Oracle) 		folio_get(folio);
38982ac64d8SMatthew Wilcox (Oracle) 		local_lock(&cpu_fbatches.lock);
39082ac64d8SMatthew Wilcox (Oracle) 		fbatch = this_cpu_ptr(&cpu_fbatches.activate);
3913a44610bSMatthew Wilcox (Oracle) 		folio_batch_add_and_move(fbatch, folio, folio_activate_fn);
39282ac64d8SMatthew Wilcox (Oracle) 		local_unlock(&cpu_fbatches.lock);
393eb709b0dSShaohua Li 	}
394eb709b0dSShaohua Li }
395eb709b0dSShaohua Li 
396eb709b0dSShaohua Li #else
3973a44610bSMatthew Wilcox (Oracle) static inline void folio_activate_drain(int cpu)
398eb709b0dSShaohua Li {
399eb709b0dSShaohua Li }
400eb709b0dSShaohua Li 
401018ee47fSYu Zhao void folio_activate(struct folio *folio)
402eb709b0dSShaohua Li {
4036168d0daSAlex Shi 	struct lruvec *lruvec;
404eb709b0dSShaohua Li 
405f2d27392SMatthew Wilcox (Oracle) 	if (folio_test_clear_lru(folio)) {
406e809c3feSMatthew Wilcox (Oracle) 		lruvec = folio_lruvec_lock_irq(folio);
4073a44610bSMatthew Wilcox (Oracle) 		folio_activate_fn(lruvec, folio);
4086168d0daSAlex Shi 		unlock_page_lruvec_irq(lruvec);
409f2d27392SMatthew Wilcox (Oracle) 		folio_set_lru(folio);
4106168d0daSAlex Shi 	}
4111da177e4SLinus Torvalds }
412eb709b0dSShaohua Li #endif
4131da177e4SLinus Torvalds 
41476580b65SMatthew Wilcox (Oracle) static void __lru_cache_activate_folio(struct folio *folio)
415059285a2SMel Gorman {
41670dea534SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch;
417059285a2SMel Gorman 	int i;
418059285a2SMel Gorman 
41982ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
42082ac64d8SMatthew Wilcox (Oracle) 	fbatch = this_cpu_ptr(&cpu_fbatches.lru_add);
421b01b2141SIngo Molnar 
422059285a2SMel Gorman 	/*
42370dea534SMatthew Wilcox (Oracle) 	 * Search backwards on the optimistic assumption that the folio being
42470dea534SMatthew Wilcox (Oracle) 	 * activated has just been added to this batch. Note that only
42570dea534SMatthew Wilcox (Oracle) 	 * the local batch is examined as a !LRU folio could be in the
426059285a2SMel Gorman 	 * process of being released, reclaimed, migrated or on a remote
42770dea534SMatthew Wilcox (Oracle) 	 * batch that is currently being drained. Furthermore, marking
42870dea534SMatthew Wilcox (Oracle) 	 * a remote batch's folio active potentially hits a race where
42970dea534SMatthew Wilcox (Oracle) 	 * a folio is marked active just after it is added to the inactive
430059285a2SMel Gorman 	 * list causing accounting errors and BUG_ON checks to trigger.
431059285a2SMel Gorman 	 */
43270dea534SMatthew Wilcox (Oracle) 	for (i = folio_batch_count(fbatch) - 1; i >= 0; i--) {
43370dea534SMatthew Wilcox (Oracle) 		struct folio *batch_folio = fbatch->folios[i];
434059285a2SMel Gorman 
43570dea534SMatthew Wilcox (Oracle) 		if (batch_folio == folio) {
43676580b65SMatthew Wilcox (Oracle) 			folio_set_active(folio);
437059285a2SMel Gorman 			break;
438059285a2SMel Gorman 		}
439059285a2SMel Gorman 	}
440059285a2SMel Gorman 
44182ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
442059285a2SMel Gorman }
443059285a2SMel Gorman 
444ac35a490SYu Zhao #ifdef CONFIG_LRU_GEN
445ac35a490SYu Zhao static void folio_inc_refs(struct folio *folio)
446ac35a490SYu Zhao {
447ac35a490SYu Zhao 	unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
448ac35a490SYu Zhao 
449ac35a490SYu Zhao 	if (folio_test_unevictable(folio))
450ac35a490SYu Zhao 		return;
451ac35a490SYu Zhao 
452ac35a490SYu Zhao 	if (!folio_test_referenced(folio)) {
453ac35a490SYu Zhao 		folio_set_referenced(folio);
454ac35a490SYu Zhao 		return;
455ac35a490SYu Zhao 	}
456ac35a490SYu Zhao 
457ac35a490SYu Zhao 	if (!folio_test_workingset(folio)) {
458ac35a490SYu Zhao 		folio_set_workingset(folio);
459ac35a490SYu Zhao 		return;
460ac35a490SYu Zhao 	}
461ac35a490SYu Zhao 
462ac35a490SYu Zhao 	/* see the comment on MAX_NR_TIERS */
463ac35a490SYu Zhao 	do {
464ac35a490SYu Zhao 		new_flags = old_flags & LRU_REFS_MASK;
465ac35a490SYu Zhao 		if (new_flags == LRU_REFS_MASK)
466ac35a490SYu Zhao 			break;
467ac35a490SYu Zhao 
468ac35a490SYu Zhao 		new_flags += BIT(LRU_REFS_PGOFF);
469ac35a490SYu Zhao 		new_flags |= old_flags & ~LRU_REFS_MASK;
470ac35a490SYu Zhao 	} while (!try_cmpxchg(&folio->flags, &old_flags, new_flags));
471ac35a490SYu Zhao }
472ac35a490SYu Zhao #else
473ac35a490SYu Zhao static void folio_inc_refs(struct folio *folio)
474ac35a490SYu Zhao {
475ac35a490SYu Zhao }
476ac35a490SYu Zhao #endif /* CONFIG_LRU_GEN */
477ac35a490SYu Zhao 
4781da177e4SLinus Torvalds /*
4791da177e4SLinus Torvalds  * Mark a page as having seen activity.
4801da177e4SLinus Torvalds  *
4811da177e4SLinus Torvalds  * inactive,unreferenced	->	inactive,referenced
4821da177e4SLinus Torvalds  * inactive,referenced		->	active,unreferenced
4831da177e4SLinus Torvalds  * active,unreferenced		->	active,referenced
484eb39d618SHugh Dickins  *
485eb39d618SHugh Dickins  * When a newly allocated page is not yet visible, so safe for non-atomic ops,
486eb39d618SHugh Dickins  * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
4871da177e4SLinus Torvalds  */
48876580b65SMatthew Wilcox (Oracle) void folio_mark_accessed(struct folio *folio)
4891da177e4SLinus Torvalds {
490ac35a490SYu Zhao 	if (lru_gen_enabled()) {
491ac35a490SYu Zhao 		folio_inc_refs(folio);
492ac35a490SYu Zhao 		return;
493ac35a490SYu Zhao 	}
494ac35a490SYu Zhao 
49576580b65SMatthew Wilcox (Oracle) 	if (!folio_test_referenced(folio)) {
49676580b65SMatthew Wilcox (Oracle) 		folio_set_referenced(folio);
49776580b65SMatthew Wilcox (Oracle) 	} else if (folio_test_unevictable(folio)) {
498a1100a74SFengguang Wu 		/*
499a1100a74SFengguang Wu 		 * Unevictable pages are on the "LRU_UNEVICTABLE" list. But,
500a1100a74SFengguang Wu 		 * this list is never rotated or maintained, so marking an
501914c32e4SBang Li 		 * unevictable page accessed has no effect.
502a1100a74SFengguang Wu 		 */
50376580b65SMatthew Wilcox (Oracle) 	} else if (!folio_test_active(folio)) {
504059285a2SMel Gorman 		/*
5053a44610bSMatthew Wilcox (Oracle) 		 * If the folio is on the LRU, queue it for activation via
50682ac64d8SMatthew Wilcox (Oracle) 		 * cpu_fbatches.activate. Otherwise, assume the folio is in a
5073a44610bSMatthew Wilcox (Oracle) 		 * folio_batch, mark it active and it'll be moved to the active
508059285a2SMel Gorman 		 * LRU on the next drain.
509059285a2SMel Gorman 		 */
51076580b65SMatthew Wilcox (Oracle) 		if (folio_test_lru(folio))
51176580b65SMatthew Wilcox (Oracle) 			folio_activate(folio);
512059285a2SMel Gorman 		else
51376580b65SMatthew Wilcox (Oracle) 			__lru_cache_activate_folio(folio);
51476580b65SMatthew Wilcox (Oracle) 		folio_clear_referenced(folio);
51576580b65SMatthew Wilcox (Oracle) 		workingset_activation(folio);
5161da177e4SLinus Torvalds 	}
51776580b65SMatthew Wilcox (Oracle) 	if (folio_test_idle(folio))
51876580b65SMatthew Wilcox (Oracle) 		folio_clear_idle(folio);
5191da177e4SLinus Torvalds }
52076580b65SMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_mark_accessed);
5211da177e4SLinus Torvalds 
522f04e9ebbSKOSAKI Motohiro /**
5230d31125dSMatthew Wilcox (Oracle)  * folio_add_lru - Add a folio to an LRU list.
5240d31125dSMatthew Wilcox (Oracle)  * @folio: The folio to be added to the LRU.
5252329d375SJianyu Zhan  *
5260d31125dSMatthew Wilcox (Oracle)  * Queue the folio for addition to the LRU. The decision on whether
5272329d375SJianyu Zhan  * to add the page to the [in]active [file|anon] list is deferred until the
52882ac64d8SMatthew Wilcox (Oracle)  * folio_batch is drained. This gives a chance for the caller of folio_add_lru()
5290d31125dSMatthew Wilcox (Oracle)  * have the folio added to the active list using folio_mark_accessed().
530f04e9ebbSKOSAKI Motohiro  */
5310d31125dSMatthew Wilcox (Oracle) void folio_add_lru(struct folio *folio)
5321da177e4SLinus Torvalds {
53370dea534SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch;
5346058eaecSJohannes Weiner 
53570dea534SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_active(folio) &&
53670dea534SMatthew Wilcox (Oracle) 			folio_test_unevictable(folio), folio);
5370d31125dSMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
5386058eaecSJohannes Weiner 
539ec1c86b2SYu Zhao 	/* see the comment in lru_gen_add_folio() */
540ec1c86b2SYu Zhao 	if (lru_gen_enabled() && !folio_test_unevictable(folio) &&
541ec1c86b2SYu Zhao 	    lru_gen_in_fault() && !(current->flags & PF_MEMALLOC))
542ec1c86b2SYu Zhao 		folio_set_active(folio);
543ec1c86b2SYu Zhao 
5440d31125dSMatthew Wilcox (Oracle) 	folio_get(folio);
54582ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
54682ac64d8SMatthew Wilcox (Oracle) 	fbatch = this_cpu_ptr(&cpu_fbatches.lru_add);
54770dea534SMatthew Wilcox (Oracle) 	folio_batch_add_and_move(fbatch, folio, lru_add_fn);
54882ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
5491da177e4SLinus Torvalds }
5500d31125dSMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_add_lru);
5511da177e4SLinus Torvalds 
552894bc310SLee Schermerhorn /**
553681ecf63SMatthew Wilcox (Oracle)  * folio_add_lru_vma() - Add a folio to the appropate LRU list for this VMA.
554681ecf63SMatthew Wilcox (Oracle)  * @folio: The folio to be added to the LRU.
555681ecf63SMatthew Wilcox (Oracle)  * @vma: VMA in which the folio is mapped.
55600501b53SJohannes Weiner  *
557681ecf63SMatthew Wilcox (Oracle)  * If the VMA is mlocked, @folio is added to the unevictable list.
558681ecf63SMatthew Wilcox (Oracle)  * Otherwise, it is treated the same way as folio_add_lru().
55900501b53SJohannes Weiner  */
560681ecf63SMatthew Wilcox (Oracle) void folio_add_lru_vma(struct folio *folio, struct vm_area_struct *vma)
56100501b53SJohannes Weiner {
562681ecf63SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
56300501b53SJohannes Weiner 
5642fbb0c10SHugh Dickins 	if (unlikely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED))
565*96f97c43SLorenzo Stoakes 		mlock_new_folio(folio);
5662fbb0c10SHugh Dickins 	else
567681ecf63SMatthew Wilcox (Oracle) 		folio_add_lru(folio);
56800501b53SJohannes Weiner }
56900501b53SJohannes Weiner 
570902aaed0SHisashi Hifumi /*
5717a3dbfe8SMatthew Wilcox (Oracle)  * If the folio cannot be invalidated, it is moved to the
57231560180SMinchan Kim  * inactive list to speed up its reclaim.  It is moved to the
57331560180SMinchan Kim  * head of the list, rather than the tail, to give the flusher
57431560180SMinchan Kim  * threads some time to write it out, as this is much more
57531560180SMinchan Kim  * effective than the single-page writeout from reclaim.
576278df9f4SMinchan Kim  *
5777a3dbfe8SMatthew Wilcox (Oracle)  * If the folio isn't mapped and dirty/writeback, the folio
5787a3dbfe8SMatthew Wilcox (Oracle)  * could be reclaimed asap using the reclaim flag.
579278df9f4SMinchan Kim  *
5807a3dbfe8SMatthew Wilcox (Oracle)  * 1. active, mapped folio -> none
5817a3dbfe8SMatthew Wilcox (Oracle)  * 2. active, dirty/writeback folio -> inactive, head, reclaim
5827a3dbfe8SMatthew Wilcox (Oracle)  * 3. inactive, mapped folio -> none
5837a3dbfe8SMatthew Wilcox (Oracle)  * 4. inactive, dirty/writeback folio -> inactive, head, reclaim
584278df9f4SMinchan Kim  * 5. inactive, clean -> inactive, tail
585278df9f4SMinchan Kim  * 6. Others -> none
586278df9f4SMinchan Kim  *
5877a3dbfe8SMatthew Wilcox (Oracle)  * In 4, it moves to the head of the inactive list so the folio is
5887a3dbfe8SMatthew Wilcox (Oracle)  * written out by flusher threads as this is much more efficient
589278df9f4SMinchan Kim  * than the single-page writeout from reclaim.
59031560180SMinchan Kim  */
5917a3dbfe8SMatthew Wilcox (Oracle) static void lru_deactivate_file_fn(struct lruvec *lruvec, struct folio *folio)
59231560180SMinchan Kim {
5937a3dbfe8SMatthew Wilcox (Oracle) 	bool active = folio_test_active(folio);
5947a3dbfe8SMatthew Wilcox (Oracle) 	long nr_pages = folio_nr_pages(folio);
59531560180SMinchan Kim 
5967a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_test_unevictable(folio))
597bad49d9cSMinchan Kim 		return;
598bad49d9cSMinchan Kim 
5997a3dbfe8SMatthew Wilcox (Oracle) 	/* Some processes are using the folio */
6007a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_mapped(folio))
60131560180SMinchan Kim 		return;
60231560180SMinchan Kim 
6037a3dbfe8SMatthew Wilcox (Oracle) 	lruvec_del_folio(lruvec, folio);
6047a3dbfe8SMatthew Wilcox (Oracle) 	folio_clear_active(folio);
6057a3dbfe8SMatthew Wilcox (Oracle) 	folio_clear_referenced(folio);
60631560180SMinchan Kim 
6077a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_test_writeback(folio) || folio_test_dirty(folio)) {
608278df9f4SMinchan Kim 		/*
6097a3dbfe8SMatthew Wilcox (Oracle) 		 * Setting the reclaim flag could race with
6107a3dbfe8SMatthew Wilcox (Oracle) 		 * folio_end_writeback() and confuse readahead.  But the
6117a3dbfe8SMatthew Wilcox (Oracle) 		 * race window is _really_ small and  it's not a critical
6127a3dbfe8SMatthew Wilcox (Oracle) 		 * problem.
613278df9f4SMinchan Kim 		 */
6147a3dbfe8SMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
6157a3dbfe8SMatthew Wilcox (Oracle) 		folio_set_reclaim(folio);
616278df9f4SMinchan Kim 	} else {
617278df9f4SMinchan Kim 		/*
6187a3dbfe8SMatthew Wilcox (Oracle) 		 * The folio's writeback ended while it was in the batch.
6197a3dbfe8SMatthew Wilcox (Oracle) 		 * We move that folio to the tail of the inactive list.
620278df9f4SMinchan Kim 		 */
6217a3dbfe8SMatthew Wilcox (Oracle) 		lruvec_add_folio_tail(lruvec, folio);
6225d91f31fSShakeel Butt 		__count_vm_events(PGROTATED, nr_pages);
623278df9f4SMinchan Kim 	}
624278df9f4SMinchan Kim 
62521e330fcSShakeel Butt 	if (active) {
6265d91f31fSShakeel Butt 		__count_vm_events(PGDEACTIVATE, nr_pages);
62721e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
62821e330fcSShakeel Butt 				     nr_pages);
62921e330fcSShakeel Butt 	}
63031560180SMinchan Kim }
63131560180SMinchan Kim 
63285cd7791SMatthew Wilcox (Oracle) static void lru_deactivate_fn(struct lruvec *lruvec, struct folio *folio)
6339c276cc6SMinchan Kim {
634ec1c86b2SYu Zhao 	if (!folio_test_unevictable(folio) && (folio_test_active(folio) || lru_gen_enabled())) {
63585cd7791SMatthew Wilcox (Oracle) 		long nr_pages = folio_nr_pages(folio);
6369c276cc6SMinchan Kim 
63785cd7791SMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
63885cd7791SMatthew Wilcox (Oracle) 		folio_clear_active(folio);
63985cd7791SMatthew Wilcox (Oracle) 		folio_clear_referenced(folio);
64085cd7791SMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
6419c276cc6SMinchan Kim 
64221e330fcSShakeel Butt 		__count_vm_events(PGDEACTIVATE, nr_pages);
64321e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
64421e330fcSShakeel Butt 				     nr_pages);
6459c276cc6SMinchan Kim 	}
6469c276cc6SMinchan Kim }
64710853a03SMinchan Kim 
648cec394baSMatthew Wilcox (Oracle) static void lru_lazyfree_fn(struct lruvec *lruvec, struct folio *folio)
64910853a03SMinchan Kim {
650cec394baSMatthew Wilcox (Oracle) 	if (folio_test_anon(folio) && folio_test_swapbacked(folio) &&
651cec394baSMatthew Wilcox (Oracle) 	    !folio_test_swapcache(folio) && !folio_test_unevictable(folio)) {
652cec394baSMatthew Wilcox (Oracle) 		long nr_pages = folio_nr_pages(folio);
65310853a03SMinchan Kim 
654cec394baSMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
655cec394baSMatthew Wilcox (Oracle) 		folio_clear_active(folio);
656cec394baSMatthew Wilcox (Oracle) 		folio_clear_referenced(folio);
657f7ad2a6cSShaohua Li 		/*
658cec394baSMatthew Wilcox (Oracle) 		 * Lazyfree folios are clean anonymous folios.  They have
659cec394baSMatthew Wilcox (Oracle) 		 * the swapbacked flag cleared, to distinguish them from normal
660cec394baSMatthew Wilcox (Oracle) 		 * anonymous folios
661f7ad2a6cSShaohua Li 		 */
662cec394baSMatthew Wilcox (Oracle) 		folio_clear_swapbacked(folio);
663cec394baSMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
66410853a03SMinchan Kim 
66521e330fcSShakeel Butt 		__count_vm_events(PGLAZYFREE, nr_pages);
66621e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE,
66721e330fcSShakeel Butt 				     nr_pages);
66810853a03SMinchan Kim 	}
66910853a03SMinchan Kim }
67010853a03SMinchan Kim 
67131560180SMinchan Kim /*
67282ac64d8SMatthew Wilcox (Oracle)  * Drain pages out of the cpu's folio_batch.
673902aaed0SHisashi Hifumi  * Either "cpu" is the current CPU, and preemption has already been
674902aaed0SHisashi Hifumi  * disabled; or "cpu" is being hot-unplugged, and is already dead.
675902aaed0SHisashi Hifumi  */
676f0cb3c76SKonstantin Khlebnikov void lru_add_drain_cpu(int cpu)
6771da177e4SLinus Torvalds {
678a2d33b5dSMatthew Wilcox (Oracle) 	struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
679a2d33b5dSMatthew Wilcox (Oracle) 	struct folio_batch *fbatch = &fbatches->lru_add;
6801da177e4SLinus Torvalds 
68170dea534SMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
68270dea534SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_add_fn);
683902aaed0SHisashi Hifumi 
684c2bc1681SMatthew Wilcox (Oracle) 	fbatch = &per_cpu(lru_rotate.fbatch, cpu);
6857e0cc01eSQian Cai 	/* Disabling interrupts below acts as a compiler barrier. */
686c2bc1681SMatthew Wilcox (Oracle) 	if (data_race(folio_batch_count(fbatch))) {
687902aaed0SHisashi Hifumi 		unsigned long flags;
688902aaed0SHisashi Hifumi 
689902aaed0SHisashi Hifumi 		/* No harm done if a racing interrupt already did this */
690b01b2141SIngo Molnar 		local_lock_irqsave(&lru_rotate.lock, flags);
691c2bc1681SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_move_tail_fn);
692b01b2141SIngo Molnar 		local_unlock_irqrestore(&lru_rotate.lock, flags);
693902aaed0SHisashi Hifumi 	}
69431560180SMinchan Kim 
695a2d33b5dSMatthew Wilcox (Oracle) 	fbatch = &fbatches->lru_deactivate_file;
6967a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
6977a3dbfe8SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_deactivate_file_fn);
698eb709b0dSShaohua Li 
699a2d33b5dSMatthew Wilcox (Oracle) 	fbatch = &fbatches->lru_deactivate;
70085cd7791SMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
70185cd7791SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_deactivate_fn);
7029c276cc6SMinchan Kim 
703a2d33b5dSMatthew Wilcox (Oracle) 	fbatch = &fbatches->lru_lazyfree;
704cec394baSMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
705cec394baSMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_lazyfree_fn);
70610853a03SMinchan Kim 
7073a44610bSMatthew Wilcox (Oracle) 	folio_activate_drain(cpu);
70831560180SMinchan Kim }
70931560180SMinchan Kim 
71031560180SMinchan Kim /**
7117a3dbfe8SMatthew Wilcox (Oracle)  * deactivate_file_folio() - Deactivate a file folio.
712261b6840SMatthew Wilcox (Oracle)  * @folio: Folio to deactivate.
71331560180SMinchan Kim  *
714261b6840SMatthew Wilcox (Oracle)  * This function hints to the VM that @folio is a good reclaim candidate,
715261b6840SMatthew Wilcox (Oracle)  * for example if its invalidation fails due to the folio being dirty
71631560180SMinchan Kim  * or under writeback.
717261b6840SMatthew Wilcox (Oracle)  *
7187a3dbfe8SMatthew Wilcox (Oracle)  * Context: Caller holds a reference on the folio.
71931560180SMinchan Kim  */
720261b6840SMatthew Wilcox (Oracle) void deactivate_file_folio(struct folio *folio)
72131560180SMinchan Kim {
7227a3dbfe8SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch;
723b01b2141SIngo Molnar 
7247a3dbfe8SMatthew Wilcox (Oracle) 	/* Deactivating an unevictable folio will not accelerate reclaim */
725261b6840SMatthew Wilcox (Oracle) 	if (folio_test_unevictable(folio))
726261b6840SMatthew Wilcox (Oracle) 		return;
727261b6840SMatthew Wilcox (Oracle) 
728261b6840SMatthew Wilcox (Oracle) 	folio_get(folio);
72982ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
73082ac64d8SMatthew Wilcox (Oracle) 	fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate_file);
7317a3dbfe8SMatthew Wilcox (Oracle) 	folio_batch_add_and_move(fbatch, folio, lru_deactivate_file_fn);
73282ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
73331560180SMinchan Kim }
73480bfed90SAndrew Morton 
7359c276cc6SMinchan Kim /*
7365a9e3474SVishal Moola (Oracle)  * folio_deactivate - deactivate a folio
7375a9e3474SVishal Moola (Oracle)  * @folio: folio to deactivate
7389c276cc6SMinchan Kim  *
7395a9e3474SVishal Moola (Oracle)  * folio_deactivate() moves @folio to the inactive list if @folio was on the
7405a9e3474SVishal Moola (Oracle)  * active list and was not unevictable. This is done to accelerate the
7415a9e3474SVishal Moola (Oracle)  * reclaim of @folio.
7429c276cc6SMinchan Kim  */
7435a9e3474SVishal Moola (Oracle) void folio_deactivate(struct folio *folio)
7449c276cc6SMinchan Kim {
745ec1c86b2SYu Zhao 	if (folio_test_lru(folio) && !folio_test_unevictable(folio) &&
746ec1c86b2SYu Zhao 	    (folio_test_active(folio) || lru_gen_enabled())) {
74785cd7791SMatthew Wilcox (Oracle) 		struct folio_batch *fbatch;
74885cd7791SMatthew Wilcox (Oracle) 
74985cd7791SMatthew Wilcox (Oracle) 		folio_get(folio);
75082ac64d8SMatthew Wilcox (Oracle) 		local_lock(&cpu_fbatches.lock);
75182ac64d8SMatthew Wilcox (Oracle) 		fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate);
75285cd7791SMatthew Wilcox (Oracle) 		folio_batch_add_and_move(fbatch, folio, lru_deactivate_fn);
75382ac64d8SMatthew Wilcox (Oracle) 		local_unlock(&cpu_fbatches.lock);
7549c276cc6SMinchan Kim 	}
7559c276cc6SMinchan Kim }
7569c276cc6SMinchan Kim 
75710853a03SMinchan Kim /**
7586a6fe9ebSKefeng Wang  * folio_mark_lazyfree - make an anon folio lazyfree
7596a6fe9ebSKefeng Wang  * @folio: folio to deactivate
76010853a03SMinchan Kim  *
7616a6fe9ebSKefeng Wang  * folio_mark_lazyfree() moves @folio to the inactive file list.
7626a6fe9ebSKefeng Wang  * This is done to accelerate the reclaim of @folio.
76310853a03SMinchan Kim  */
7646a6fe9ebSKefeng Wang void folio_mark_lazyfree(struct folio *folio)
76510853a03SMinchan Kim {
766cec394baSMatthew Wilcox (Oracle) 	if (folio_test_lru(folio) && folio_test_anon(folio) &&
767cec394baSMatthew Wilcox (Oracle) 	    folio_test_swapbacked(folio) && !folio_test_swapcache(folio) &&
768cec394baSMatthew Wilcox (Oracle) 	    !folio_test_unevictable(folio)) {
769cec394baSMatthew Wilcox (Oracle) 		struct folio_batch *fbatch;
770cec394baSMatthew Wilcox (Oracle) 
771cec394baSMatthew Wilcox (Oracle) 		folio_get(folio);
77282ac64d8SMatthew Wilcox (Oracle) 		local_lock(&cpu_fbatches.lock);
77382ac64d8SMatthew Wilcox (Oracle) 		fbatch = this_cpu_ptr(&cpu_fbatches.lru_lazyfree);
774cec394baSMatthew Wilcox (Oracle) 		folio_batch_add_and_move(fbatch, folio, lru_lazyfree_fn);
77582ac64d8SMatthew Wilcox (Oracle) 		local_unlock(&cpu_fbatches.lock);
77610853a03SMinchan Kim 	}
77710853a03SMinchan Kim }
77810853a03SMinchan Kim 
77980bfed90SAndrew Morton void lru_add_drain(void)
78080bfed90SAndrew Morton {
78182ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
782b01b2141SIngo Molnar 	lru_add_drain_cpu(smp_processor_id());
78382ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
784*96f97c43SLorenzo Stoakes 	mlock_drain_local();
785b01b2141SIngo Molnar }
786b01b2141SIngo Molnar 
787243418e3SMinchan Kim /*
788243418e3SMinchan Kim  * It's called from per-cpu workqueue context in SMP case so
789243418e3SMinchan Kim  * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on
790243418e3SMinchan Kim  * the same cpu. It shouldn't be a problem in !SMP case since
791243418e3SMinchan Kim  * the core is only one and the locks will disable preemption.
792243418e3SMinchan Kim  */
793243418e3SMinchan Kim static void lru_add_and_bh_lrus_drain(void)
794243418e3SMinchan Kim {
79582ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
796243418e3SMinchan Kim 	lru_add_drain_cpu(smp_processor_id());
79782ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
798243418e3SMinchan Kim 	invalidate_bh_lrus_cpu();
799*96f97c43SLorenzo Stoakes 	mlock_drain_local();
800243418e3SMinchan Kim }
801243418e3SMinchan Kim 
802b01b2141SIngo Molnar void lru_add_drain_cpu_zone(struct zone *zone)
803b01b2141SIngo Molnar {
80482ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
805b01b2141SIngo Molnar 	lru_add_drain_cpu(smp_processor_id());
806b01b2141SIngo Molnar 	drain_local_pages(zone);
80782ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
808*96f97c43SLorenzo Stoakes 	mlock_drain_local();
8091da177e4SLinus Torvalds }
8101da177e4SLinus Torvalds 
8116ea183d6SMichal Hocko #ifdef CONFIG_SMP
8126ea183d6SMichal Hocko 
8136ea183d6SMichal Hocko static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
8146ea183d6SMichal Hocko 
815c4028958SDavid Howells static void lru_add_drain_per_cpu(struct work_struct *dummy)
816053837fcSNick Piggin {
817243418e3SMinchan Kim 	lru_add_and_bh_lrus_drain();
818053837fcSNick Piggin }
819053837fcSNick Piggin 
8204864545aSMatthew Wilcox (Oracle) static bool cpu_needs_drain(unsigned int cpu)
8214864545aSMatthew Wilcox (Oracle) {
8224864545aSMatthew Wilcox (Oracle) 	struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
8234864545aSMatthew Wilcox (Oracle) 
8244864545aSMatthew Wilcox (Oracle) 	/* Check these in order of likelihood that they're not zero */
8254864545aSMatthew Wilcox (Oracle) 	return folio_batch_count(&fbatches->lru_add) ||
8264864545aSMatthew Wilcox (Oracle) 		data_race(folio_batch_count(&per_cpu(lru_rotate.fbatch, cpu))) ||
8274864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->lru_deactivate_file) ||
8284864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->lru_deactivate) ||
8294864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->lru_lazyfree) ||
8304864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->activate) ||
831*96f97c43SLorenzo Stoakes 		need_mlock_drain(cpu) ||
8324864545aSMatthew Wilcox (Oracle) 		has_bh_in_lru(cpu, NULL);
8334864545aSMatthew Wilcox (Oracle) }
8344864545aSMatthew Wilcox (Oracle) 
8359852a721SMichal Hocko /*
8369852a721SMichal Hocko  * Doesn't need any cpu hotplug locking because we do rely on per-cpu
8379852a721SMichal Hocko  * kworkers being shut down before our page_alloc_cpu_dead callback is
8389852a721SMichal Hocko  * executed on the offlined cpu.
8399852a721SMichal Hocko  * Calling this function with cpu hotplug locks held can actually lead
8409852a721SMichal Hocko  * to obscure indirect dependencies via WQ context.
8419852a721SMichal Hocko  */
8423db3264dSMiaohe Lin static inline void __lru_add_drain_all(bool force_all_cpus)
843053837fcSNick Piggin {
8446446a513SAhmed S. Darwish 	/*
8456446a513SAhmed S. Darwish 	 * lru_drain_gen - Global pages generation number
8466446a513SAhmed S. Darwish 	 *
8476446a513SAhmed S. Darwish 	 * (A) Definition: global lru_drain_gen = x implies that all generations
8486446a513SAhmed S. Darwish 	 *     0 < n <= x are already *scheduled* for draining.
8496446a513SAhmed S. Darwish 	 *
8506446a513SAhmed S. Darwish 	 * This is an optimization for the highly-contended use case where a
8516446a513SAhmed S. Darwish 	 * user space workload keeps constantly generating a flow of pages for
8526446a513SAhmed S. Darwish 	 * each CPU.
8536446a513SAhmed S. Darwish 	 */
8546446a513SAhmed S. Darwish 	static unsigned int lru_drain_gen;
8555fbc4616SChris Metcalf 	static struct cpumask has_work;
8566446a513SAhmed S. Darwish 	static DEFINE_MUTEX(lock);
8576446a513SAhmed S. Darwish 	unsigned cpu, this_gen;
8585fbc4616SChris Metcalf 
859ce612879SMichal Hocko 	/*
860ce612879SMichal Hocko 	 * Make sure nobody triggers this path before mm_percpu_wq is fully
861ce612879SMichal Hocko 	 * initialized.
862ce612879SMichal Hocko 	 */
863ce612879SMichal Hocko 	if (WARN_ON(!mm_percpu_wq))
864ce612879SMichal Hocko 		return;
865ce612879SMichal Hocko 
8666446a513SAhmed S. Darwish 	/*
86782ac64d8SMatthew Wilcox (Oracle) 	 * Guarantee folio_batch counter stores visible by this CPU
86882ac64d8SMatthew Wilcox (Oracle) 	 * are visible to other CPUs before loading the current drain
86982ac64d8SMatthew Wilcox (Oracle) 	 * generation.
8706446a513SAhmed S. Darwish 	 */
8716446a513SAhmed S. Darwish 	smp_mb();
8726446a513SAhmed S. Darwish 
8736446a513SAhmed S. Darwish 	/*
8746446a513SAhmed S. Darwish 	 * (B) Locally cache global LRU draining generation number
8756446a513SAhmed S. Darwish 	 *
8766446a513SAhmed S. Darwish 	 * The read barrier ensures that the counter is loaded before the mutex
8776446a513SAhmed S. Darwish 	 * is taken. It pairs with smp_mb() inside the mutex critical section
8786446a513SAhmed S. Darwish 	 * at (D).
8796446a513SAhmed S. Darwish 	 */
8806446a513SAhmed S. Darwish 	this_gen = smp_load_acquire(&lru_drain_gen);
881eef1a429SKonstantin Khlebnikov 
8825fbc4616SChris Metcalf 	mutex_lock(&lock);
883eef1a429SKonstantin Khlebnikov 
884eef1a429SKonstantin Khlebnikov 	/*
8856446a513SAhmed S. Darwish 	 * (C) Exit the draining operation if a newer generation, from another
8866446a513SAhmed S. Darwish 	 * lru_add_drain_all(), was already scheduled for draining. Check (A).
887eef1a429SKonstantin Khlebnikov 	 */
888d479960eSMinchan Kim 	if (unlikely(this_gen != lru_drain_gen && !force_all_cpus))
889eef1a429SKonstantin Khlebnikov 		goto done;
890eef1a429SKonstantin Khlebnikov 
8916446a513SAhmed S. Darwish 	/*
8926446a513SAhmed S. Darwish 	 * (D) Increment global generation number
8936446a513SAhmed S. Darwish 	 *
8946446a513SAhmed S. Darwish 	 * Pairs with smp_load_acquire() at (B), outside of the critical
89582ac64d8SMatthew Wilcox (Oracle) 	 * section. Use a full memory barrier to guarantee that the
89682ac64d8SMatthew Wilcox (Oracle) 	 * new global drain generation number is stored before loading
89782ac64d8SMatthew Wilcox (Oracle) 	 * folio_batch counters.
8986446a513SAhmed S. Darwish 	 *
8996446a513SAhmed S. Darwish 	 * This pairing must be done here, before the for_each_online_cpu loop
9006446a513SAhmed S. Darwish 	 * below which drains the page vectors.
9016446a513SAhmed S. Darwish 	 *
9026446a513SAhmed S. Darwish 	 * Let x, y, and z represent some system CPU numbers, where x < y < z.
903cb152a1aSShijie Luo 	 * Assume CPU #z is in the middle of the for_each_online_cpu loop
9046446a513SAhmed S. Darwish 	 * below and has already reached CPU #y's per-cpu data. CPU #x comes
9056446a513SAhmed S. Darwish 	 * along, adds some pages to its per-cpu vectors, then calls
9066446a513SAhmed S. Darwish 	 * lru_add_drain_all().
9076446a513SAhmed S. Darwish 	 *
9086446a513SAhmed S. Darwish 	 * If the paired barrier is done at any later step, e.g. after the
9096446a513SAhmed S. Darwish 	 * loop, CPU #x will just exit at (C) and miss flushing out all of its
9106446a513SAhmed S. Darwish 	 * added pages.
9116446a513SAhmed S. Darwish 	 */
9126446a513SAhmed S. Darwish 	WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1);
9136446a513SAhmed S. Darwish 	smp_mb();
914eef1a429SKonstantin Khlebnikov 
9155fbc4616SChris Metcalf 	cpumask_clear(&has_work);
9165fbc4616SChris Metcalf 	for_each_online_cpu(cpu) {
9175fbc4616SChris Metcalf 		struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
9185fbc4616SChris Metcalf 
9194864545aSMatthew Wilcox (Oracle) 		if (cpu_needs_drain(cpu)) {
9205fbc4616SChris Metcalf 			INIT_WORK(work, lru_add_drain_per_cpu);
921ce612879SMichal Hocko 			queue_work_on(cpu, mm_percpu_wq, work);
9226446a513SAhmed S. Darwish 			__cpumask_set_cpu(cpu, &has_work);
9235fbc4616SChris Metcalf 		}
9245fbc4616SChris Metcalf 	}
9255fbc4616SChris Metcalf 
9265fbc4616SChris Metcalf 	for_each_cpu(cpu, &has_work)
9275fbc4616SChris Metcalf 		flush_work(&per_cpu(lru_add_drain_work, cpu));
9285fbc4616SChris Metcalf 
929eef1a429SKonstantin Khlebnikov done:
9305fbc4616SChris Metcalf 	mutex_unlock(&lock);
931053837fcSNick Piggin }
932d479960eSMinchan Kim 
933d479960eSMinchan Kim void lru_add_drain_all(void)
934d479960eSMinchan Kim {
935d479960eSMinchan Kim 	__lru_add_drain_all(false);
936d479960eSMinchan Kim }
9376ea183d6SMichal Hocko #else
9386ea183d6SMichal Hocko void lru_add_drain_all(void)
9396ea183d6SMichal Hocko {
9406ea183d6SMichal Hocko 	lru_add_drain();
9416ea183d6SMichal Hocko }
9426446a513SAhmed S. Darwish #endif /* CONFIG_SMP */
943053837fcSNick Piggin 
944d479960eSMinchan Kim atomic_t lru_disable_count = ATOMIC_INIT(0);
945d479960eSMinchan Kim 
946d479960eSMinchan Kim /*
947d479960eSMinchan Kim  * lru_cache_disable() needs to be called before we start compiling
948d479960eSMinchan Kim  * a list of pages to be migrated using isolate_lru_page().
949d479960eSMinchan Kim  * It drains pages on LRU cache and then disable on all cpus until
950d479960eSMinchan Kim  * lru_cache_enable is called.
951d479960eSMinchan Kim  *
952d479960eSMinchan Kim  * Must be paired with a call to lru_cache_enable().
953d479960eSMinchan Kim  */
954d479960eSMinchan Kim void lru_cache_disable(void)
955d479960eSMinchan Kim {
956d479960eSMinchan Kim 	atomic_inc(&lru_disable_count);
957d479960eSMinchan Kim 	/*
958ff042f4aSMarcelo Tosatti 	 * Readers of lru_disable_count are protected by either disabling
959ff042f4aSMarcelo Tosatti 	 * preemption or rcu_read_lock:
960ff042f4aSMarcelo Tosatti 	 *
961ff042f4aSMarcelo Tosatti 	 * preempt_disable, local_irq_disable  [bh_lru_lock()]
962ff042f4aSMarcelo Tosatti 	 * rcu_read_lock		       [rt_spin_lock CONFIG_PREEMPT_RT]
963ff042f4aSMarcelo Tosatti 	 * preempt_disable		       [local_lock !CONFIG_PREEMPT_RT]
964ff042f4aSMarcelo Tosatti 	 *
965ff042f4aSMarcelo Tosatti 	 * Since v5.1 kernel, synchronize_rcu() is guaranteed to wait on
966ff042f4aSMarcelo Tosatti 	 * preempt_disable() regions of code. So any CPU which sees
967ff042f4aSMarcelo Tosatti 	 * lru_disable_count = 0 will have exited the critical
968ff042f4aSMarcelo Tosatti 	 * section when synchronize_rcu() returns.
969d479960eSMinchan Kim 	 */
97031733463SMarcelo Tosatti 	synchronize_rcu_expedited();
971ff042f4aSMarcelo Tosatti #ifdef CONFIG_SMP
972d479960eSMinchan Kim 	__lru_add_drain_all(true);
973d479960eSMinchan Kim #else
974243418e3SMinchan Kim 	lru_add_and_bh_lrus_drain();
975d479960eSMinchan Kim #endif
976d479960eSMinchan Kim }
977d479960eSMinchan Kim 
978aabfb572SMichal Hocko /**
979ea1754a0SKirill A. Shutemov  * release_pages - batched put_page()
980449c7967SLinus Torvalds  * @arg: array of pages to release
981aabfb572SMichal Hocko  * @nr: number of pages
9821da177e4SLinus Torvalds  *
983449c7967SLinus Torvalds  * Decrement the reference count on all the pages in @arg.  If it
984aabfb572SMichal Hocko  * fell to zero, remove the page from the LRU and free it.
985449c7967SLinus Torvalds  *
986449c7967SLinus Torvalds  * Note that the argument can be an array of pages, encoded pages,
987449c7967SLinus Torvalds  * or folio pointers. We ignore any encoded bits, and turn any of
988449c7967SLinus Torvalds  * them into just a folio that gets free'd.
9891da177e4SLinus Torvalds  */
990449c7967SLinus Torvalds void release_pages(release_pages_arg arg, int nr)
9911da177e4SLinus Torvalds {
9921da177e4SLinus Torvalds 	int i;
993449c7967SLinus Torvalds 	struct encoded_page **encoded = arg.encoded_pages;
994cc59850eSKonstantin Khlebnikov 	LIST_HEAD(pages_to_free);
9956168d0daSAlex Shi 	struct lruvec *lruvec = NULL;
9960de340cbSMatthew Wilcox (Oracle) 	unsigned long flags = 0;
9973f649ab7SKees Cook 	unsigned int lock_batch;
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	for (i = 0; i < nr; i++) {
1000449c7967SLinus Torvalds 		struct folio *folio;
1001449c7967SLinus Torvalds 
1002449c7967SLinus Torvalds 		/* Turn any of the argument types into a folio */
1003449c7967SLinus Torvalds 		folio = page_folio(encoded_page_ptr(encoded[i]));
10041da177e4SLinus Torvalds 
1005aabfb572SMichal Hocko 		/*
1006aabfb572SMichal Hocko 		 * Make sure the IRQ-safe lock-holding time does not get
1007aabfb572SMichal Hocko 		 * excessive with a continuous string of pages from the
10086168d0daSAlex Shi 		 * same lruvec. The lock is held only if lruvec != NULL.
1009aabfb572SMichal Hocko 		 */
10106168d0daSAlex Shi 		if (lruvec && ++lock_batch == SWAP_CLUSTER_MAX) {
10116168d0daSAlex Shi 			unlock_page_lruvec_irqrestore(lruvec, flags);
10126168d0daSAlex Shi 			lruvec = NULL;
1013aabfb572SMichal Hocko 		}
1014aabfb572SMichal Hocko 
1015ab5e653eSMatthew Wilcox (Oracle) 		if (is_huge_zero_page(&folio->page))
1016aa88b68cSKirill A. Shutemov 			continue;
1017aa88b68cSKirill A. Shutemov 
1018ab5e653eSMatthew Wilcox (Oracle) 		if (folio_is_zone_device(folio)) {
10196168d0daSAlex Shi 			if (lruvec) {
10206168d0daSAlex Shi 				unlock_page_lruvec_irqrestore(lruvec, flags);
10216168d0daSAlex Shi 				lruvec = NULL;
1022df6ad698SJérôme Glisse 			}
1023ab5e653eSMatthew Wilcox (Oracle) 			if (put_devmap_managed_page(&folio->page))
1024df6ad698SJérôme Glisse 				continue;
1025ab5e653eSMatthew Wilcox (Oracle) 			if (folio_put_testzero(folio))
1026ab5e653eSMatthew Wilcox (Oracle) 				free_zone_device_page(&folio->page);
102743fbdeb3SRalph Campbell 			continue;
102807d80269SJohn Hubbard 		}
1029df6ad698SJérôme Glisse 
1030ab5e653eSMatthew Wilcox (Oracle) 		if (!folio_put_testzero(folio))
10311da177e4SLinus Torvalds 			continue;
10321da177e4SLinus Torvalds 
1033ab5e653eSMatthew Wilcox (Oracle) 		if (folio_test_large(folio)) {
10346168d0daSAlex Shi 			if (lruvec) {
10356168d0daSAlex Shi 				unlock_page_lruvec_irqrestore(lruvec, flags);
10366168d0daSAlex Shi 				lruvec = NULL;
1037ddc58f27SKirill A. Shutemov 			}
10385ef82fe7SMatthew Wilcox (Oracle) 			__folio_put_large(folio);
1039ddc58f27SKirill A. Shutemov 			continue;
1040ddc58f27SKirill A. Shutemov 		}
1041ddc58f27SKirill A. Shutemov 
1042ab5e653eSMatthew Wilcox (Oracle) 		if (folio_test_lru(folio)) {
10432a5e4e34SAlexander Duyck 			struct lruvec *prev_lruvec = lruvec;
1044894bc310SLee Schermerhorn 
10450de340cbSMatthew Wilcox (Oracle) 			lruvec = folio_lruvec_relock_irqsave(folio, lruvec,
10462a5e4e34SAlexander Duyck 									&flags);
10472a5e4e34SAlexander Duyck 			if (prev_lruvec != lruvec)
1048aabfb572SMichal Hocko 				lock_batch = 0;
1049fa9add64SHugh Dickins 
1050ab5e653eSMatthew Wilcox (Oracle) 			lruvec_del_folio(lruvec, folio);
1051ab5e653eSMatthew Wilcox (Oracle) 			__folio_clear_lru_flags(folio);
105246453a6eSNick Piggin 		}
105346453a6eSNick Piggin 
1054b109b870SHugh Dickins 		/*
1055b109b870SHugh Dickins 		 * In rare cases, when truncation or holepunching raced with
1056b109b870SHugh Dickins 		 * munlock after VM_LOCKED was cleared, Mlocked may still be
1057b109b870SHugh Dickins 		 * found set here.  This does not indicate a problem, unless
1058b109b870SHugh Dickins 		 * "unevictable_pgs_cleared" appears worryingly large.
1059b109b870SHugh Dickins 		 */
1060ab5e653eSMatthew Wilcox (Oracle) 		if (unlikely(folio_test_mlocked(folio))) {
1061ab5e653eSMatthew Wilcox (Oracle) 			__folio_clear_mlocked(folio);
1062ab5e653eSMatthew Wilcox (Oracle) 			zone_stat_sub_folio(folio, NR_MLOCK);
1063b109b870SHugh Dickins 			count_vm_event(UNEVICTABLE_PGCLEARED);
1064b109b870SHugh Dickins 		}
1065b109b870SHugh Dickins 
1066ab5e653eSMatthew Wilcox (Oracle) 		list_add(&folio->lru, &pages_to_free);
10671da177e4SLinus Torvalds 	}
10686168d0daSAlex Shi 	if (lruvec)
10696168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
10701da177e4SLinus Torvalds 
1071747db954SJohannes Weiner 	mem_cgroup_uncharge_list(&pages_to_free);
10722d4894b5SMel Gorman 	free_unref_page_list(&pages_to_free);
10731da177e4SLinus Torvalds }
10740be8557bSMiklos Szeredi EXPORT_SYMBOL(release_pages);
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds /*
10771da177e4SLinus Torvalds  * The pages which we're about to release may be in the deferred lru-addition
10781da177e4SLinus Torvalds  * queues.  That would prevent them from really being freed right now.  That's
10791da177e4SLinus Torvalds  * OK from a correctness point of view but is inefficient - those pages may be
10801da177e4SLinus Torvalds  * cache-warm and we want to give them back to the page allocator ASAP.
10811da177e4SLinus Torvalds  *
108270dea534SMatthew Wilcox (Oracle)  * So __pagevec_release() will drain those queues here.
108370dea534SMatthew Wilcox (Oracle)  * folio_batch_move_lru() calls folios_put() directly to avoid
10841da177e4SLinus Torvalds  * mutual recursion.
10851da177e4SLinus Torvalds  */
10861da177e4SLinus Torvalds void __pagevec_release(struct pagevec *pvec)
10871da177e4SLinus Torvalds {
10887f0b5fb9SMel Gorman 	if (!pvec->percpu_pvec_drained) {
10891da177e4SLinus Torvalds 		lru_add_drain();
10907f0b5fb9SMel Gorman 		pvec->percpu_pvec_drained = true;
1091d9ed0d08SMel Gorman 	}
1092c6f92f9fSMel Gorman 	release_pages(pvec->pages, pagevec_count(pvec));
10931da177e4SLinus Torvalds 	pagevec_reinit(pvec);
10941da177e4SLinus Torvalds }
10957f285701SSteve French EXPORT_SYMBOL(__pagevec_release);
10967f285701SSteve French 
10971da177e4SLinus Torvalds /**
10981613fac9SMatthew Wilcox (Oracle)  * folio_batch_remove_exceptionals() - Prune non-folios from a batch.
10991613fac9SMatthew Wilcox (Oracle)  * @fbatch: The batch to prune
11000cd6144aSJohannes Weiner  *
11011613fac9SMatthew Wilcox (Oracle)  * find_get_entries() fills a batch with both folios and shadow/swap/DAX
11021613fac9SMatthew Wilcox (Oracle)  * entries.  This function prunes all the non-folio entries from @fbatch
11031613fac9SMatthew Wilcox (Oracle)  * without leaving holes, so that it can be passed on to folio-only batch
11041613fac9SMatthew Wilcox (Oracle)  * operations.
11050cd6144aSJohannes Weiner  */
11061613fac9SMatthew Wilcox (Oracle) void folio_batch_remove_exceptionals(struct folio_batch *fbatch)
11070cd6144aSJohannes Weiner {
11081613fac9SMatthew Wilcox (Oracle) 	unsigned int i, j;
11090cd6144aSJohannes Weiner 
11101613fac9SMatthew Wilcox (Oracle) 	for (i = 0, j = 0; i < folio_batch_count(fbatch); i++) {
11111613fac9SMatthew Wilcox (Oracle) 		struct folio *folio = fbatch->folios[i];
11121613fac9SMatthew Wilcox (Oracle) 		if (!xa_is_value(folio))
11131613fac9SMatthew Wilcox (Oracle) 			fbatch->folios[j++] = folio;
11140cd6144aSJohannes Weiner 	}
11151613fac9SMatthew Wilcox (Oracle) 	fbatch->nr = j;
11160cd6144aSJohannes Weiner }
11170cd6144aSJohannes Weiner 
111872b045aeSJan Kara unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
111972b045aeSJan Kara 		struct address_space *mapping, pgoff_t *index, pgoff_t end,
112010bbd235SMatthew Wilcox 		xa_mark_t tag)
11211da177e4SLinus Torvalds {
112272b045aeSJan Kara 	pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
112367fd707fSJan Kara 					PAGEVEC_SIZE, pvec->pages);
11241da177e4SLinus Torvalds 	return pagevec_count(pvec);
11251da177e4SLinus Torvalds }
112672b045aeSJan Kara EXPORT_SYMBOL(pagevec_lookup_range_tag);
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds /*
11291da177e4SLinus Torvalds  * Perform any setup for the swap system
11301da177e4SLinus Torvalds  */
11311da177e4SLinus Torvalds void __init swap_setup(void)
11321da177e4SLinus Torvalds {
1133ca79b0c2SArun KS 	unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
1134e0bf68ddSPeter Zijlstra 
11351da177e4SLinus Torvalds 	/* Use a smaller cluster for small-memory machines */
11361da177e4SLinus Torvalds 	if (megs < 16)
11371da177e4SLinus Torvalds 		page_cluster = 2;
11381da177e4SLinus Torvalds 	else
11391da177e4SLinus Torvalds 		page_cluster = 3;
11401da177e4SLinus Torvalds 	/*
11411da177e4SLinus Torvalds 	 * Right now other parts of the system means that we
11421da177e4SLinus Torvalds 	 * _really_ don't want to cluster much more
11431da177e4SLinus Torvalds 	 */
11441da177e4SLinus Torvalds }
1145