xref: /linux/mm/swap.c (revision 8d29c7036f5ff360ea1f51b9fed5d909be7c8094)
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  */
80920c7a5dSHarvey Harrison static void __page_cache_release(struct page *page)
81b221385bSAdrian Bunk {
82b221385bSAdrian Bunk 	if (PageLRU(page)) {
83e809c3feSMatthew Wilcox (Oracle) 		struct folio *folio = page_folio(page);
84fa9add64SHugh Dickins 		struct lruvec *lruvec;
85fa9add64SHugh Dickins 		unsigned long flags;
86b221385bSAdrian Bunk 
87e809c3feSMatthew Wilcox (Oracle) 		lruvec = folio_lruvec_lock_irqsave(folio, &flags);
8846ae6b2cSYu Zhao 		del_page_from_lru_list(page, lruvec);
8987560179SYu Zhao 		__clear_page_lru_flags(page);
906168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
91b221385bSAdrian Bunk 	}
92b109b870SHugh Dickins 	/* See comment on PageMlocked in release_pages() */
93b109b870SHugh Dickins 	if (unlikely(PageMlocked(page))) {
94b109b870SHugh Dickins 		int nr_pages = thp_nr_pages(page);
95b109b870SHugh Dickins 
96b109b870SHugh Dickins 		__ClearPageMlocked(page);
97b109b870SHugh Dickins 		mod_zone_page_state(page_zone(page), NR_MLOCK, -nr_pages);
98b109b870SHugh Dickins 		count_vm_events(UNEVICTABLE_PGCLEARED, nr_pages);
99b109b870SHugh Dickins 	}
10091807063SAndrea Arcangeli }
10191807063SAndrea Arcangeli 
10291807063SAndrea Arcangeli static void __put_single_page(struct page *page)
10391807063SAndrea Arcangeli {
10491807063SAndrea Arcangeli 	__page_cache_release(page);
105bbc6b703SMatthew Wilcox (Oracle) 	mem_cgroup_uncharge(page_folio(page));
10644042b44SMel Gorman 	free_unref_page(page, 0);
107b221385bSAdrian Bunk }
108b221385bSAdrian Bunk 
10991807063SAndrea Arcangeli static void __put_compound_page(struct page *page)
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 	 */
117822fc613SNaoya Horiguchi 	if (!PageHuge(page))
11891807063SAndrea Arcangeli 		__page_cache_release(page);
119ff45fc3cSMatthew Wilcox (Oracle) 	destroy_compound_page(page);
12091807063SAndrea Arcangeli }
12191807063SAndrea Arcangeli 
122*8d29c703SMatthew Wilcox (Oracle) void __folio_put(struct folio *folio)
123c747ce79SJianyu Zhan {
124*8d29c703SMatthew Wilcox (Oracle) 	if (unlikely(folio_is_zone_device(folio)))
125*8d29c703SMatthew Wilcox (Oracle) 		free_zone_device_page(&folio->page);
126*8d29c703SMatthew Wilcox (Oracle) 	else if (unlikely(folio_test_large(folio)))
127*8d29c703SMatthew Wilcox (Oracle) 		__put_compound_page(&folio->page);
12826296ad2SAndrew Morton 	else
129*8d29c703SMatthew Wilcox (Oracle) 		__put_single_page(&folio->page);
13026296ad2SAndrew Morton }
131*8d29c703SMatthew 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);
1502f58e5deSMatthew Wilcox (Oracle) 			__put_compound_page(&folio->page);
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
2027d80dd09SMatthew Wilcox (Oracle) 	 * folio_evictable() tests PageMlocked, to rule out the possibility
2037d80dd09SMatthew Wilcox (Oracle) 	 * of stranding an evictable folio on an unevictable LRU?  I think
2047d80dd09SMatthew Wilcox (Oracle) 	 * not, because __munlock_page() only clears PageMlocked while the LRU
2057d80dd09SMatthew Wilcox (Oracle) 	 * lock is held.
2067d80dd09SMatthew Wilcox (Oracle) 	 *
2077d80dd09SMatthew Wilcox (Oracle) 	 * (That is not true of __page_cache_release(), and not necessarily
2087d80dd09SMatthew Wilcox (Oracle) 	 * true of release_pages(): but those only clear PageMlocked after
2097d80dd09SMatthew Wilcox (Oracle) 	 * put_page_testzero() has excluded any other users of the page.)
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)
234c2bc1681SMatthew Wilcox (Oracle) {
235c2bc1681SMatthew Wilcox (Oracle) 	int i;
236c2bc1681SMatthew Wilcox (Oracle) 	struct lruvec *lruvec = NULL;
237c2bc1681SMatthew Wilcox (Oracle) 	unsigned long flags = 0;
238c2bc1681SMatthew Wilcox (Oracle) 
239c2bc1681SMatthew Wilcox (Oracle) 	for (i = 0; i < folio_batch_count(fbatch); i++) {
240c2bc1681SMatthew Wilcox (Oracle) 		struct folio *folio = fbatch->folios[i];
241c2bc1681SMatthew Wilcox (Oracle) 
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))
244c2bc1681SMatthew Wilcox (Oracle) 			continue;
245c2bc1681SMatthew Wilcox (Oracle) 
246c2bc1681SMatthew Wilcox (Oracle) 		lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags);
247c2bc1681SMatthew Wilcox (Oracle) 		move_fn(lruvec, folio);
248c2bc1681SMatthew Wilcox (Oracle) 
249c2bc1681SMatthew Wilcox (Oracle) 		folio_set_lru(folio);
250c2bc1681SMatthew Wilcox (Oracle) 	}
251c2bc1681SMatthew Wilcox (Oracle) 
252c2bc1681SMatthew Wilcox (Oracle) 	if (lruvec)
253c2bc1681SMatthew Wilcox (Oracle) 		unlock_page_lruvec_irqrestore(lruvec, flags);
254c2bc1681SMatthew Wilcox (Oracle) 	folios_put(fbatch->folios, folio_batch_count(fbatch));
255c2bc1681SMatthew Wilcox (Oracle) 	folio_batch_init(fbatch);
256c2bc1681SMatthew Wilcox (Oracle) }
257c2bc1681SMatthew Wilcox (Oracle) 
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)
260c2bc1681SMatthew Wilcox (Oracle) {
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) }
266c2bc1681SMatthew Wilcox (Oracle) 
267c2bc1681SMatthew Wilcox (Oracle) static void lru_move_tail_fn(struct lruvec *lruvec, struct folio *folio)
268c2bc1681SMatthew Wilcox (Oracle) {
269c2bc1681SMatthew Wilcox (Oracle) 	if (!folio_test_unevictable(folio)) {
270c2bc1681SMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
271c2bc1681SMatthew Wilcox (Oracle) 		folio_clear_active(folio);
272c2bc1681SMatthew Wilcox (Oracle) 		lruvec_add_folio_tail(lruvec, folio);
273c2bc1681SMatthew Wilcox (Oracle) 		__count_vm_events(PGROTATED, folio_nr_pages(folio));
274c2bc1681SMatthew Wilcox (Oracle) 	}
275c2bc1681SMatthew Wilcox (Oracle) }
276c2bc1681SMatthew Wilcox (Oracle) 
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 
29996f8bf4fSJohannes Weiner void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages)
3003e2f41f1SKOSAKI Motohiro {
3017cf111bcSJohannes Weiner 	do {
3027cf111bcSJohannes Weiner 		unsigned long lrusize;
3037cf111bcSJohannes Weiner 
3046168d0daSAlex Shi 		/*
3056168d0daSAlex Shi 		 * Hold lruvec->lru_lock is safe here, since
3066168d0daSAlex Shi 		 * 1) The pinned lruvec in reclaim, or
3076168d0daSAlex Shi 		 * 2) From a pre-LRU page during refault (which also holds the
3086168d0daSAlex Shi 		 *    rcu lock, so would be safe even if the page was on the LRU
3096168d0daSAlex Shi 		 *    and could move simultaneously to a new lruvec).
3106168d0daSAlex Shi 		 */
3116168d0daSAlex Shi 		spin_lock_irq(&lruvec->lru_lock);
3127cf111bcSJohannes Weiner 		/* Record cost event */
31396f8bf4fSJohannes Weiner 		if (file)
31496f8bf4fSJohannes Weiner 			lruvec->file_cost += nr_pages;
3151431d4d1SJohannes Weiner 		else
31696f8bf4fSJohannes Weiner 			lruvec->anon_cost += nr_pages;
3177cf111bcSJohannes Weiner 
3187cf111bcSJohannes Weiner 		/*
3197cf111bcSJohannes Weiner 		 * Decay previous events
3207cf111bcSJohannes Weiner 		 *
3217cf111bcSJohannes Weiner 		 * Because workloads change over time (and to avoid
3227cf111bcSJohannes Weiner 		 * overflow) we keep these statistics as a floating
3237cf111bcSJohannes Weiner 		 * average, which ends up weighing recent refaults
3247cf111bcSJohannes Weiner 		 * more than old ones.
3257cf111bcSJohannes Weiner 		 */
3267cf111bcSJohannes Weiner 		lrusize = lruvec_page_state(lruvec, NR_INACTIVE_ANON) +
3277cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_ACTIVE_ANON) +
3287cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_INACTIVE_FILE) +
3297cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_ACTIVE_FILE);
3307cf111bcSJohannes Weiner 
3317cf111bcSJohannes Weiner 		if (lruvec->file_cost + lruvec->anon_cost > lrusize / 4) {
3327cf111bcSJohannes Weiner 			lruvec->file_cost /= 2;
3337cf111bcSJohannes Weiner 			lruvec->anon_cost /= 2;
3347cf111bcSJohannes Weiner 		}
3356168d0daSAlex Shi 		spin_unlock_irq(&lruvec->lru_lock);
3367cf111bcSJohannes Weiner 	} while ((lruvec = parent_lruvec(lruvec)));
3373e2f41f1SKOSAKI Motohiro }
3383e2f41f1SKOSAKI Motohiro 
3390995d7e5SMatthew Wilcox (Oracle) void lru_note_cost_folio(struct folio *folio)
34096f8bf4fSJohannes Weiner {
3410995d7e5SMatthew Wilcox (Oracle) 	lru_note_cost(folio_lruvec(folio), folio_is_file_lru(folio),
3420995d7e5SMatthew Wilcox (Oracle) 			folio_nr_pages(folio));
34396f8bf4fSJohannes Weiner }
34496f8bf4fSJohannes Weiner 
3453a44610bSMatthew Wilcox (Oracle) static void folio_activate_fn(struct lruvec *lruvec, struct folio *folio)
346744ed144SShaohua Li {
347f2d27392SMatthew Wilcox (Oracle) 	if (!folio_test_active(folio) && !folio_test_unevictable(folio)) {
348f2d27392SMatthew Wilcox (Oracle) 		long nr_pages = folio_nr_pages(folio);
349744ed144SShaohua Li 
350f2d27392SMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
351f2d27392SMatthew Wilcox (Oracle) 		folio_set_active(folio);
352f2d27392SMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
353f2d27392SMatthew Wilcox (Oracle) 		trace_mm_lru_activate(folio);
3547a608572SLinus Torvalds 
35521e330fcSShakeel Butt 		__count_vm_events(PGACTIVATE, nr_pages);
35621e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGACTIVATE,
35721e330fcSShakeel Butt 				     nr_pages);
358744ed144SShaohua Li 	}
359eb709b0dSShaohua Li }
360eb709b0dSShaohua Li 
361eb709b0dSShaohua Li #ifdef CONFIG_SMP
3623a44610bSMatthew Wilcox (Oracle) static void folio_activate_drain(int cpu)
363f2d27392SMatthew Wilcox (Oracle) {
36482ac64d8SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch = &per_cpu(cpu_fbatches.activate, cpu);
365f2d27392SMatthew Wilcox (Oracle) 
3663a44610bSMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
3673a44610bSMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, folio_activate_fn);
3685fbc4616SChris Metcalf }
3695fbc4616SChris Metcalf 
370f2d27392SMatthew Wilcox (Oracle) static void folio_activate(struct folio *folio)
371eb709b0dSShaohua Li {
372f2d27392SMatthew Wilcox (Oracle) 	if (folio_test_lru(folio) && !folio_test_active(folio) &&
373f2d27392SMatthew Wilcox (Oracle) 	    !folio_test_unevictable(folio)) {
3743a44610bSMatthew Wilcox (Oracle) 		struct folio_batch *fbatch;
375eb709b0dSShaohua Li 
376f2d27392SMatthew Wilcox (Oracle) 		folio_get(folio);
37782ac64d8SMatthew Wilcox (Oracle) 		local_lock(&cpu_fbatches.lock);
37882ac64d8SMatthew Wilcox (Oracle) 		fbatch = this_cpu_ptr(&cpu_fbatches.activate);
3793a44610bSMatthew Wilcox (Oracle) 		folio_batch_add_and_move(fbatch, folio, folio_activate_fn);
38082ac64d8SMatthew Wilcox (Oracle) 		local_unlock(&cpu_fbatches.lock);
381eb709b0dSShaohua Li 	}
382eb709b0dSShaohua Li }
383eb709b0dSShaohua Li 
384eb709b0dSShaohua Li #else
3853a44610bSMatthew Wilcox (Oracle) static inline void folio_activate_drain(int cpu)
386eb709b0dSShaohua Li {
387eb709b0dSShaohua Li }
388eb709b0dSShaohua Li 
389f2d27392SMatthew Wilcox (Oracle) static void folio_activate(struct folio *folio)
390eb709b0dSShaohua Li {
3916168d0daSAlex Shi 	struct lruvec *lruvec;
392eb709b0dSShaohua Li 
393f2d27392SMatthew Wilcox (Oracle) 	if (folio_test_clear_lru(folio)) {
394e809c3feSMatthew Wilcox (Oracle) 		lruvec = folio_lruvec_lock_irq(folio);
3953a44610bSMatthew Wilcox (Oracle) 		folio_activate_fn(lruvec, folio);
3966168d0daSAlex Shi 		unlock_page_lruvec_irq(lruvec);
397f2d27392SMatthew Wilcox (Oracle) 		folio_set_lru(folio);
3986168d0daSAlex Shi 	}
3991da177e4SLinus Torvalds }
400eb709b0dSShaohua Li #endif
4011da177e4SLinus Torvalds 
40276580b65SMatthew Wilcox (Oracle) static void __lru_cache_activate_folio(struct folio *folio)
403059285a2SMel Gorman {
40470dea534SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch;
405059285a2SMel Gorman 	int i;
406059285a2SMel Gorman 
40782ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
40882ac64d8SMatthew Wilcox (Oracle) 	fbatch = this_cpu_ptr(&cpu_fbatches.lru_add);
409b01b2141SIngo Molnar 
410059285a2SMel Gorman 	/*
41170dea534SMatthew Wilcox (Oracle) 	 * Search backwards on the optimistic assumption that the folio being
41270dea534SMatthew Wilcox (Oracle) 	 * activated has just been added to this batch. Note that only
41370dea534SMatthew Wilcox (Oracle) 	 * the local batch is examined as a !LRU folio could be in the
414059285a2SMel Gorman 	 * process of being released, reclaimed, migrated or on a remote
41570dea534SMatthew Wilcox (Oracle) 	 * batch that is currently being drained. Furthermore, marking
41670dea534SMatthew Wilcox (Oracle) 	 * a remote batch's folio active potentially hits a race where
41770dea534SMatthew Wilcox (Oracle) 	 * a folio is marked active just after it is added to the inactive
418059285a2SMel Gorman 	 * list causing accounting errors and BUG_ON checks to trigger.
419059285a2SMel Gorman 	 */
42070dea534SMatthew Wilcox (Oracle) 	for (i = folio_batch_count(fbatch) - 1; i >= 0; i--) {
42170dea534SMatthew Wilcox (Oracle) 		struct folio *batch_folio = fbatch->folios[i];
422059285a2SMel Gorman 
42370dea534SMatthew Wilcox (Oracle) 		if (batch_folio == folio) {
42476580b65SMatthew Wilcox (Oracle) 			folio_set_active(folio);
425059285a2SMel Gorman 			break;
426059285a2SMel Gorman 		}
427059285a2SMel Gorman 	}
428059285a2SMel Gorman 
42982ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
430059285a2SMel Gorman }
431059285a2SMel Gorman 
4321da177e4SLinus Torvalds /*
4331da177e4SLinus Torvalds  * Mark a page as having seen activity.
4341da177e4SLinus Torvalds  *
4351da177e4SLinus Torvalds  * inactive,unreferenced	->	inactive,referenced
4361da177e4SLinus Torvalds  * inactive,referenced		->	active,unreferenced
4371da177e4SLinus Torvalds  * active,unreferenced		->	active,referenced
438eb39d618SHugh Dickins  *
439eb39d618SHugh Dickins  * When a newly allocated page is not yet visible, so safe for non-atomic ops,
440eb39d618SHugh Dickins  * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
4411da177e4SLinus Torvalds  */
44276580b65SMatthew Wilcox (Oracle) void folio_mark_accessed(struct folio *folio)
4431da177e4SLinus Torvalds {
44476580b65SMatthew Wilcox (Oracle) 	if (!folio_test_referenced(folio)) {
44576580b65SMatthew Wilcox (Oracle) 		folio_set_referenced(folio);
44676580b65SMatthew Wilcox (Oracle) 	} else if (folio_test_unevictable(folio)) {
447a1100a74SFengguang Wu 		/*
448a1100a74SFengguang Wu 		 * Unevictable pages are on the "LRU_UNEVICTABLE" list. But,
449a1100a74SFengguang Wu 		 * this list is never rotated or maintained, so marking an
450914c32e4SBang Li 		 * unevictable page accessed has no effect.
451a1100a74SFengguang Wu 		 */
45276580b65SMatthew Wilcox (Oracle) 	} else if (!folio_test_active(folio)) {
453059285a2SMel Gorman 		/*
4543a44610bSMatthew Wilcox (Oracle) 		 * If the folio is on the LRU, queue it for activation via
45582ac64d8SMatthew Wilcox (Oracle) 		 * cpu_fbatches.activate. Otherwise, assume the folio is in a
4563a44610bSMatthew Wilcox (Oracle) 		 * folio_batch, mark it active and it'll be moved to the active
457059285a2SMel Gorman 		 * LRU on the next drain.
458059285a2SMel Gorman 		 */
45976580b65SMatthew Wilcox (Oracle) 		if (folio_test_lru(folio))
46076580b65SMatthew Wilcox (Oracle) 			folio_activate(folio);
461059285a2SMel Gorman 		else
46276580b65SMatthew Wilcox (Oracle) 			__lru_cache_activate_folio(folio);
46376580b65SMatthew Wilcox (Oracle) 		folio_clear_referenced(folio);
46476580b65SMatthew Wilcox (Oracle) 		workingset_activation(folio);
4651da177e4SLinus Torvalds 	}
46676580b65SMatthew Wilcox (Oracle) 	if (folio_test_idle(folio))
46776580b65SMatthew Wilcox (Oracle) 		folio_clear_idle(folio);
4681da177e4SLinus Torvalds }
46976580b65SMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_mark_accessed);
4701da177e4SLinus Torvalds 
471f04e9ebbSKOSAKI Motohiro /**
4720d31125dSMatthew Wilcox (Oracle)  * folio_add_lru - Add a folio to an LRU list.
4730d31125dSMatthew Wilcox (Oracle)  * @folio: The folio to be added to the LRU.
4742329d375SJianyu Zhan  *
4750d31125dSMatthew Wilcox (Oracle)  * Queue the folio for addition to the LRU. The decision on whether
4762329d375SJianyu Zhan  * to add the page to the [in]active [file|anon] list is deferred until the
47782ac64d8SMatthew Wilcox (Oracle)  * folio_batch is drained. This gives a chance for the caller of folio_add_lru()
4780d31125dSMatthew Wilcox (Oracle)  * have the folio added to the active list using folio_mark_accessed().
479f04e9ebbSKOSAKI Motohiro  */
4800d31125dSMatthew Wilcox (Oracle) void folio_add_lru(struct folio *folio)
4811da177e4SLinus Torvalds {
48270dea534SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch;
4836058eaecSJohannes Weiner 
48470dea534SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_active(folio) &&
48570dea534SMatthew Wilcox (Oracle) 			folio_test_unevictable(folio), folio);
4860d31125dSMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
4876058eaecSJohannes Weiner 
4880d31125dSMatthew Wilcox (Oracle) 	folio_get(folio);
48982ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
49082ac64d8SMatthew Wilcox (Oracle) 	fbatch = this_cpu_ptr(&cpu_fbatches.lru_add);
49170dea534SMatthew Wilcox (Oracle) 	folio_batch_add_and_move(fbatch, folio, lru_add_fn);
49282ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
4931da177e4SLinus Torvalds }
4940d31125dSMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_add_lru);
4951da177e4SLinus Torvalds 
496894bc310SLee Schermerhorn /**
497b518154eSJoonsoo Kim  * lru_cache_add_inactive_or_unevictable
49800501b53SJohannes Weiner  * @page:  the page to be added to LRU
49900501b53SJohannes Weiner  * @vma:   vma in which page is mapped for determining reclaimability
50000501b53SJohannes Weiner  *
501b518154eSJoonsoo Kim  * Place @page on the inactive or unevictable LRU list, depending on its
50212eab428SMiaohe Lin  * evictability.
50300501b53SJohannes Weiner  */
504b518154eSJoonsoo Kim void lru_cache_add_inactive_or_unevictable(struct page *page,
50500501b53SJohannes Weiner 					 struct vm_area_struct *vma)
50600501b53SJohannes Weiner {
50700501b53SJohannes Weiner 	VM_BUG_ON_PAGE(PageLRU(page), page);
50800501b53SJohannes Weiner 
5092fbb0c10SHugh Dickins 	if (unlikely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED))
5102fbb0c10SHugh Dickins 		mlock_new_page(page);
5112fbb0c10SHugh Dickins 	else
5129c4e6b1aSShakeel Butt 		lru_cache_add(page);
51300501b53SJohannes Weiner }
51400501b53SJohannes Weiner 
515902aaed0SHisashi Hifumi /*
5167a3dbfe8SMatthew Wilcox (Oracle)  * If the folio cannot be invalidated, it is moved to the
51731560180SMinchan Kim  * inactive list to speed up its reclaim.  It is moved to the
51831560180SMinchan Kim  * head of the list, rather than the tail, to give the flusher
51931560180SMinchan Kim  * threads some time to write it out, as this is much more
52031560180SMinchan Kim  * effective than the single-page writeout from reclaim.
521278df9f4SMinchan Kim  *
5227a3dbfe8SMatthew Wilcox (Oracle)  * If the folio isn't mapped and dirty/writeback, the folio
5237a3dbfe8SMatthew Wilcox (Oracle)  * could be reclaimed asap using the reclaim flag.
524278df9f4SMinchan Kim  *
5257a3dbfe8SMatthew Wilcox (Oracle)  * 1. active, mapped folio -> none
5267a3dbfe8SMatthew Wilcox (Oracle)  * 2. active, dirty/writeback folio -> inactive, head, reclaim
5277a3dbfe8SMatthew Wilcox (Oracle)  * 3. inactive, mapped folio -> none
5287a3dbfe8SMatthew Wilcox (Oracle)  * 4. inactive, dirty/writeback folio -> inactive, head, reclaim
529278df9f4SMinchan Kim  * 5. inactive, clean -> inactive, tail
530278df9f4SMinchan Kim  * 6. Others -> none
531278df9f4SMinchan Kim  *
5327a3dbfe8SMatthew Wilcox (Oracle)  * In 4, it moves to the head of the inactive list so the folio is
5337a3dbfe8SMatthew Wilcox (Oracle)  * written out by flusher threads as this is much more efficient
534278df9f4SMinchan Kim  * than the single-page writeout from reclaim.
53531560180SMinchan Kim  */
5367a3dbfe8SMatthew Wilcox (Oracle) static void lru_deactivate_file_fn(struct lruvec *lruvec, struct folio *folio)
53731560180SMinchan Kim {
5387a3dbfe8SMatthew Wilcox (Oracle) 	bool active = folio_test_active(folio);
5397a3dbfe8SMatthew Wilcox (Oracle) 	long nr_pages = folio_nr_pages(folio);
54031560180SMinchan Kim 
5417a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_test_unevictable(folio))
542bad49d9cSMinchan Kim 		return;
543bad49d9cSMinchan Kim 
5447a3dbfe8SMatthew Wilcox (Oracle) 	/* Some processes are using the folio */
5457a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_mapped(folio))
54631560180SMinchan Kim 		return;
54731560180SMinchan Kim 
5487a3dbfe8SMatthew Wilcox (Oracle) 	lruvec_del_folio(lruvec, folio);
5497a3dbfe8SMatthew Wilcox (Oracle) 	folio_clear_active(folio);
5507a3dbfe8SMatthew Wilcox (Oracle) 	folio_clear_referenced(folio);
55131560180SMinchan Kim 
5527a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_test_writeback(folio) || folio_test_dirty(folio)) {
553278df9f4SMinchan Kim 		/*
5547a3dbfe8SMatthew Wilcox (Oracle) 		 * Setting the reclaim flag could race with
5557a3dbfe8SMatthew Wilcox (Oracle) 		 * folio_end_writeback() and confuse readahead.  But the
5567a3dbfe8SMatthew Wilcox (Oracle) 		 * race window is _really_ small and  it's not a critical
5577a3dbfe8SMatthew Wilcox (Oracle) 		 * problem.
558278df9f4SMinchan Kim 		 */
5597a3dbfe8SMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
5607a3dbfe8SMatthew Wilcox (Oracle) 		folio_set_reclaim(folio);
561278df9f4SMinchan Kim 	} else {
562278df9f4SMinchan Kim 		/*
5637a3dbfe8SMatthew Wilcox (Oracle) 		 * The folio's writeback ended while it was in the batch.
5647a3dbfe8SMatthew Wilcox (Oracle) 		 * We move that folio to the tail of the inactive list.
565278df9f4SMinchan Kim 		 */
5667a3dbfe8SMatthew Wilcox (Oracle) 		lruvec_add_folio_tail(lruvec, folio);
5675d91f31fSShakeel Butt 		__count_vm_events(PGROTATED, nr_pages);
568278df9f4SMinchan Kim 	}
569278df9f4SMinchan Kim 
57021e330fcSShakeel Butt 	if (active) {
5715d91f31fSShakeel Butt 		__count_vm_events(PGDEACTIVATE, nr_pages);
57221e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
57321e330fcSShakeel Butt 				     nr_pages);
57421e330fcSShakeel Butt 	}
57531560180SMinchan Kim }
57631560180SMinchan Kim 
57785cd7791SMatthew Wilcox (Oracle) static void lru_deactivate_fn(struct lruvec *lruvec, struct folio *folio)
5789c276cc6SMinchan Kim {
57985cd7791SMatthew Wilcox (Oracle) 	if (folio_test_active(folio) && !folio_test_unevictable(folio)) {
58085cd7791SMatthew Wilcox (Oracle) 		long nr_pages = folio_nr_pages(folio);
5819c276cc6SMinchan Kim 
58285cd7791SMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
58385cd7791SMatthew Wilcox (Oracle) 		folio_clear_active(folio);
58485cd7791SMatthew Wilcox (Oracle) 		folio_clear_referenced(folio);
58585cd7791SMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
5869c276cc6SMinchan Kim 
58721e330fcSShakeel Butt 		__count_vm_events(PGDEACTIVATE, nr_pages);
58821e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
58921e330fcSShakeel Butt 				     nr_pages);
5909c276cc6SMinchan Kim 	}
5919c276cc6SMinchan Kim }
59210853a03SMinchan Kim 
593cec394baSMatthew Wilcox (Oracle) static void lru_lazyfree_fn(struct lruvec *lruvec, struct folio *folio)
59410853a03SMinchan Kim {
595cec394baSMatthew Wilcox (Oracle) 	if (folio_test_anon(folio) && folio_test_swapbacked(folio) &&
596cec394baSMatthew Wilcox (Oracle) 	    !folio_test_swapcache(folio) && !folio_test_unevictable(folio)) {
597cec394baSMatthew Wilcox (Oracle) 		long nr_pages = folio_nr_pages(folio);
59810853a03SMinchan Kim 
599cec394baSMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
600cec394baSMatthew Wilcox (Oracle) 		folio_clear_active(folio);
601cec394baSMatthew Wilcox (Oracle) 		folio_clear_referenced(folio);
602f7ad2a6cSShaohua Li 		/*
603cec394baSMatthew Wilcox (Oracle) 		 * Lazyfree folios are clean anonymous folios.  They have
604cec394baSMatthew Wilcox (Oracle) 		 * the swapbacked flag cleared, to distinguish them from normal
605cec394baSMatthew Wilcox (Oracle) 		 * anonymous folios
606f7ad2a6cSShaohua Li 		 */
607cec394baSMatthew Wilcox (Oracle) 		folio_clear_swapbacked(folio);
608cec394baSMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
60910853a03SMinchan Kim 
61021e330fcSShakeel Butt 		__count_vm_events(PGLAZYFREE, nr_pages);
61121e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE,
61221e330fcSShakeel Butt 				     nr_pages);
61310853a03SMinchan Kim 	}
61410853a03SMinchan Kim }
61510853a03SMinchan Kim 
61631560180SMinchan Kim /*
61782ac64d8SMatthew Wilcox (Oracle)  * Drain pages out of the cpu's folio_batch.
618902aaed0SHisashi Hifumi  * Either "cpu" is the current CPU, and preemption has already been
619902aaed0SHisashi Hifumi  * disabled; or "cpu" is being hot-unplugged, and is already dead.
620902aaed0SHisashi Hifumi  */
621f0cb3c76SKonstantin Khlebnikov void lru_add_drain_cpu(int cpu)
6221da177e4SLinus Torvalds {
623a2d33b5dSMatthew Wilcox (Oracle) 	struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
624a2d33b5dSMatthew Wilcox (Oracle) 	struct folio_batch *fbatch = &fbatches->lru_add;
6251da177e4SLinus Torvalds 
62670dea534SMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
62770dea534SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_add_fn);
628902aaed0SHisashi Hifumi 
629c2bc1681SMatthew Wilcox (Oracle) 	fbatch = &per_cpu(lru_rotate.fbatch, cpu);
6307e0cc01eSQian Cai 	/* Disabling interrupts below acts as a compiler barrier. */
631c2bc1681SMatthew Wilcox (Oracle) 	if (data_race(folio_batch_count(fbatch))) {
632902aaed0SHisashi Hifumi 		unsigned long flags;
633902aaed0SHisashi Hifumi 
634902aaed0SHisashi Hifumi 		/* No harm done if a racing interrupt already did this */
635b01b2141SIngo Molnar 		local_lock_irqsave(&lru_rotate.lock, flags);
636c2bc1681SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_move_tail_fn);
637b01b2141SIngo Molnar 		local_unlock_irqrestore(&lru_rotate.lock, flags);
638902aaed0SHisashi Hifumi 	}
63931560180SMinchan Kim 
640a2d33b5dSMatthew Wilcox (Oracle) 	fbatch = &fbatches->lru_deactivate_file;
6417a3dbfe8SMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
6427a3dbfe8SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_deactivate_file_fn);
643eb709b0dSShaohua Li 
644a2d33b5dSMatthew Wilcox (Oracle) 	fbatch = &fbatches->lru_deactivate;
64585cd7791SMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
64685cd7791SMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_deactivate_fn);
6479c276cc6SMinchan Kim 
648a2d33b5dSMatthew Wilcox (Oracle) 	fbatch = &fbatches->lru_lazyfree;
649cec394baSMatthew Wilcox (Oracle) 	if (folio_batch_count(fbatch))
650cec394baSMatthew Wilcox (Oracle) 		folio_batch_move_lru(fbatch, lru_lazyfree_fn);
65110853a03SMinchan Kim 
6523a44610bSMatthew Wilcox (Oracle) 	folio_activate_drain(cpu);
65331560180SMinchan Kim }
65431560180SMinchan Kim 
65531560180SMinchan Kim /**
6567a3dbfe8SMatthew Wilcox (Oracle)  * deactivate_file_folio() - Deactivate a file folio.
657261b6840SMatthew Wilcox (Oracle)  * @folio: Folio to deactivate.
65831560180SMinchan Kim  *
659261b6840SMatthew Wilcox (Oracle)  * This function hints to the VM that @folio is a good reclaim candidate,
660261b6840SMatthew Wilcox (Oracle)  * for example if its invalidation fails due to the folio being dirty
66131560180SMinchan Kim  * or under writeback.
662261b6840SMatthew Wilcox (Oracle)  *
6637a3dbfe8SMatthew Wilcox (Oracle)  * Context: Caller holds a reference on the folio.
66431560180SMinchan Kim  */
665261b6840SMatthew Wilcox (Oracle) void deactivate_file_folio(struct folio *folio)
66631560180SMinchan Kim {
6677a3dbfe8SMatthew Wilcox (Oracle) 	struct folio_batch *fbatch;
668b01b2141SIngo Molnar 
6697a3dbfe8SMatthew Wilcox (Oracle) 	/* Deactivating an unevictable folio will not accelerate reclaim */
670261b6840SMatthew Wilcox (Oracle) 	if (folio_test_unevictable(folio))
671261b6840SMatthew Wilcox (Oracle) 		return;
672261b6840SMatthew Wilcox (Oracle) 
673261b6840SMatthew Wilcox (Oracle) 	folio_get(folio);
67482ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
67582ac64d8SMatthew Wilcox (Oracle) 	fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate_file);
6767a3dbfe8SMatthew Wilcox (Oracle) 	folio_batch_add_and_move(fbatch, folio, lru_deactivate_file_fn);
67782ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
67831560180SMinchan Kim }
67980bfed90SAndrew Morton 
6809c276cc6SMinchan Kim /*
6819c276cc6SMinchan Kim  * deactivate_page - deactivate a page
6829c276cc6SMinchan Kim  * @page: page to deactivate
6839c276cc6SMinchan Kim  *
6849c276cc6SMinchan Kim  * deactivate_page() moves @page to the inactive list if @page was on the active
6859c276cc6SMinchan Kim  * list and was not an unevictable page.  This is done to accelerate the reclaim
6869c276cc6SMinchan Kim  * of @page.
6879c276cc6SMinchan Kim  */
6889c276cc6SMinchan Kim void deactivate_page(struct page *page)
6899c276cc6SMinchan Kim {
69085cd7791SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
6919c276cc6SMinchan Kim 
69285cd7791SMatthew Wilcox (Oracle) 	if (folio_test_lru(folio) && folio_test_active(folio) &&
69385cd7791SMatthew Wilcox (Oracle) 	    !folio_test_unevictable(folio)) {
69485cd7791SMatthew Wilcox (Oracle) 		struct folio_batch *fbatch;
69585cd7791SMatthew Wilcox (Oracle) 
69685cd7791SMatthew Wilcox (Oracle) 		folio_get(folio);
69782ac64d8SMatthew Wilcox (Oracle) 		local_lock(&cpu_fbatches.lock);
69882ac64d8SMatthew Wilcox (Oracle) 		fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate);
69985cd7791SMatthew Wilcox (Oracle) 		folio_batch_add_and_move(fbatch, folio, lru_deactivate_fn);
70082ac64d8SMatthew Wilcox (Oracle) 		local_unlock(&cpu_fbatches.lock);
7019c276cc6SMinchan Kim 	}
7029c276cc6SMinchan Kim }
7039c276cc6SMinchan Kim 
70410853a03SMinchan Kim /**
705f7ad2a6cSShaohua Li  * mark_page_lazyfree - make an anon page lazyfree
70610853a03SMinchan Kim  * @page: page to deactivate
70710853a03SMinchan Kim  *
708f7ad2a6cSShaohua Li  * mark_page_lazyfree() moves @page to the inactive file list.
709f7ad2a6cSShaohua Li  * This is done to accelerate the reclaim of @page.
71010853a03SMinchan Kim  */
711f7ad2a6cSShaohua Li void mark_page_lazyfree(struct page *page)
71210853a03SMinchan Kim {
713cec394baSMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
71410853a03SMinchan Kim 
715cec394baSMatthew Wilcox (Oracle) 	if (folio_test_lru(folio) && folio_test_anon(folio) &&
716cec394baSMatthew Wilcox (Oracle) 	    folio_test_swapbacked(folio) && !folio_test_swapcache(folio) &&
717cec394baSMatthew Wilcox (Oracle) 	    !folio_test_unevictable(folio)) {
718cec394baSMatthew Wilcox (Oracle) 		struct folio_batch *fbatch;
719cec394baSMatthew Wilcox (Oracle) 
720cec394baSMatthew Wilcox (Oracle) 		folio_get(folio);
72182ac64d8SMatthew Wilcox (Oracle) 		local_lock(&cpu_fbatches.lock);
72282ac64d8SMatthew Wilcox (Oracle) 		fbatch = this_cpu_ptr(&cpu_fbatches.lru_lazyfree);
723cec394baSMatthew Wilcox (Oracle) 		folio_batch_add_and_move(fbatch, folio, lru_lazyfree_fn);
72482ac64d8SMatthew Wilcox (Oracle) 		local_unlock(&cpu_fbatches.lock);
72510853a03SMinchan Kim 	}
72610853a03SMinchan Kim }
72710853a03SMinchan Kim 
72880bfed90SAndrew Morton void lru_add_drain(void)
72980bfed90SAndrew Morton {
73082ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
731b01b2141SIngo Molnar 	lru_add_drain_cpu(smp_processor_id());
73282ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
733adb11e78SSebastian Andrzej Siewior 	mlock_page_drain_local();
734b01b2141SIngo Molnar }
735b01b2141SIngo Molnar 
736243418e3SMinchan Kim /*
737243418e3SMinchan Kim  * It's called from per-cpu workqueue context in SMP case so
738243418e3SMinchan Kim  * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on
739243418e3SMinchan Kim  * the same cpu. It shouldn't be a problem in !SMP case since
740243418e3SMinchan Kim  * the core is only one and the locks will disable preemption.
741243418e3SMinchan Kim  */
742243418e3SMinchan Kim static void lru_add_and_bh_lrus_drain(void)
743243418e3SMinchan Kim {
74482ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
745243418e3SMinchan Kim 	lru_add_drain_cpu(smp_processor_id());
74682ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
747243418e3SMinchan Kim 	invalidate_bh_lrus_cpu();
748adb11e78SSebastian Andrzej Siewior 	mlock_page_drain_local();
749243418e3SMinchan Kim }
750243418e3SMinchan Kim 
751b01b2141SIngo Molnar void lru_add_drain_cpu_zone(struct zone *zone)
752b01b2141SIngo Molnar {
75382ac64d8SMatthew Wilcox (Oracle) 	local_lock(&cpu_fbatches.lock);
754b01b2141SIngo Molnar 	lru_add_drain_cpu(smp_processor_id());
755b01b2141SIngo Molnar 	drain_local_pages(zone);
75682ac64d8SMatthew Wilcox (Oracle) 	local_unlock(&cpu_fbatches.lock);
757adb11e78SSebastian Andrzej Siewior 	mlock_page_drain_local();
7581da177e4SLinus Torvalds }
7591da177e4SLinus Torvalds 
7606ea183d6SMichal Hocko #ifdef CONFIG_SMP
7616ea183d6SMichal Hocko 
7626ea183d6SMichal Hocko static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
7636ea183d6SMichal Hocko 
764c4028958SDavid Howells static void lru_add_drain_per_cpu(struct work_struct *dummy)
765053837fcSNick Piggin {
766243418e3SMinchan Kim 	lru_add_and_bh_lrus_drain();
767053837fcSNick Piggin }
768053837fcSNick Piggin 
7694864545aSMatthew Wilcox (Oracle) static bool cpu_needs_drain(unsigned int cpu)
7704864545aSMatthew Wilcox (Oracle) {
7714864545aSMatthew Wilcox (Oracle) 	struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
7724864545aSMatthew Wilcox (Oracle) 
7734864545aSMatthew Wilcox (Oracle) 	/* Check these in order of likelihood that they're not zero */
7744864545aSMatthew Wilcox (Oracle) 	return folio_batch_count(&fbatches->lru_add) ||
7754864545aSMatthew Wilcox (Oracle) 		data_race(folio_batch_count(&per_cpu(lru_rotate.fbatch, cpu))) ||
7764864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->lru_deactivate_file) ||
7774864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->lru_deactivate) ||
7784864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->lru_lazyfree) ||
7794864545aSMatthew Wilcox (Oracle) 		folio_batch_count(&fbatches->activate) ||
7804864545aSMatthew Wilcox (Oracle) 		need_mlock_page_drain(cpu) ||
7814864545aSMatthew Wilcox (Oracle) 		has_bh_in_lru(cpu, NULL);
7824864545aSMatthew Wilcox (Oracle) }
7834864545aSMatthew Wilcox (Oracle) 
7849852a721SMichal Hocko /*
7859852a721SMichal Hocko  * Doesn't need any cpu hotplug locking because we do rely on per-cpu
7869852a721SMichal Hocko  * kworkers being shut down before our page_alloc_cpu_dead callback is
7879852a721SMichal Hocko  * executed on the offlined cpu.
7889852a721SMichal Hocko  * Calling this function with cpu hotplug locks held can actually lead
7899852a721SMichal Hocko  * to obscure indirect dependencies via WQ context.
7909852a721SMichal Hocko  */
7913db3264dSMiaohe Lin static inline void __lru_add_drain_all(bool force_all_cpus)
792053837fcSNick Piggin {
7936446a513SAhmed S. Darwish 	/*
7946446a513SAhmed S. Darwish 	 * lru_drain_gen - Global pages generation number
7956446a513SAhmed S. Darwish 	 *
7966446a513SAhmed S. Darwish 	 * (A) Definition: global lru_drain_gen = x implies that all generations
7976446a513SAhmed S. Darwish 	 *     0 < n <= x are already *scheduled* for draining.
7986446a513SAhmed S. Darwish 	 *
7996446a513SAhmed S. Darwish 	 * This is an optimization for the highly-contended use case where a
8006446a513SAhmed S. Darwish 	 * user space workload keeps constantly generating a flow of pages for
8016446a513SAhmed S. Darwish 	 * each CPU.
8026446a513SAhmed S. Darwish 	 */
8036446a513SAhmed S. Darwish 	static unsigned int lru_drain_gen;
8045fbc4616SChris Metcalf 	static struct cpumask has_work;
8056446a513SAhmed S. Darwish 	static DEFINE_MUTEX(lock);
8066446a513SAhmed S. Darwish 	unsigned cpu, this_gen;
8075fbc4616SChris Metcalf 
808ce612879SMichal Hocko 	/*
809ce612879SMichal Hocko 	 * Make sure nobody triggers this path before mm_percpu_wq is fully
810ce612879SMichal Hocko 	 * initialized.
811ce612879SMichal Hocko 	 */
812ce612879SMichal Hocko 	if (WARN_ON(!mm_percpu_wq))
813ce612879SMichal Hocko 		return;
814ce612879SMichal Hocko 
8156446a513SAhmed S. Darwish 	/*
81682ac64d8SMatthew Wilcox (Oracle) 	 * Guarantee folio_batch counter stores visible by this CPU
81782ac64d8SMatthew Wilcox (Oracle) 	 * are visible to other CPUs before loading the current drain
81882ac64d8SMatthew Wilcox (Oracle) 	 * generation.
8196446a513SAhmed S. Darwish 	 */
8206446a513SAhmed S. Darwish 	smp_mb();
8216446a513SAhmed S. Darwish 
8226446a513SAhmed S. Darwish 	/*
8236446a513SAhmed S. Darwish 	 * (B) Locally cache global LRU draining generation number
8246446a513SAhmed S. Darwish 	 *
8256446a513SAhmed S. Darwish 	 * The read barrier ensures that the counter is loaded before the mutex
8266446a513SAhmed S. Darwish 	 * is taken. It pairs with smp_mb() inside the mutex critical section
8276446a513SAhmed S. Darwish 	 * at (D).
8286446a513SAhmed S. Darwish 	 */
8296446a513SAhmed S. Darwish 	this_gen = smp_load_acquire(&lru_drain_gen);
830eef1a429SKonstantin Khlebnikov 
8315fbc4616SChris Metcalf 	mutex_lock(&lock);
832eef1a429SKonstantin Khlebnikov 
833eef1a429SKonstantin Khlebnikov 	/*
8346446a513SAhmed S. Darwish 	 * (C) Exit the draining operation if a newer generation, from another
8356446a513SAhmed S. Darwish 	 * lru_add_drain_all(), was already scheduled for draining. Check (A).
836eef1a429SKonstantin Khlebnikov 	 */
837d479960eSMinchan Kim 	if (unlikely(this_gen != lru_drain_gen && !force_all_cpus))
838eef1a429SKonstantin Khlebnikov 		goto done;
839eef1a429SKonstantin Khlebnikov 
8406446a513SAhmed S. Darwish 	/*
8416446a513SAhmed S. Darwish 	 * (D) Increment global generation number
8426446a513SAhmed S. Darwish 	 *
8436446a513SAhmed S. Darwish 	 * Pairs with smp_load_acquire() at (B), outside of the critical
84482ac64d8SMatthew Wilcox (Oracle) 	 * section. Use a full memory barrier to guarantee that the
84582ac64d8SMatthew Wilcox (Oracle) 	 * new global drain generation number is stored before loading
84682ac64d8SMatthew Wilcox (Oracle) 	 * folio_batch counters.
8476446a513SAhmed S. Darwish 	 *
8486446a513SAhmed S. Darwish 	 * This pairing must be done here, before the for_each_online_cpu loop
8496446a513SAhmed S. Darwish 	 * below which drains the page vectors.
8506446a513SAhmed S. Darwish 	 *
8516446a513SAhmed S. Darwish 	 * Let x, y, and z represent some system CPU numbers, where x < y < z.
852cb152a1aSShijie Luo 	 * Assume CPU #z is in the middle of the for_each_online_cpu loop
8536446a513SAhmed S. Darwish 	 * below and has already reached CPU #y's per-cpu data. CPU #x comes
8546446a513SAhmed S. Darwish 	 * along, adds some pages to its per-cpu vectors, then calls
8556446a513SAhmed S. Darwish 	 * lru_add_drain_all().
8566446a513SAhmed S. Darwish 	 *
8576446a513SAhmed S. Darwish 	 * If the paired barrier is done at any later step, e.g. after the
8586446a513SAhmed S. Darwish 	 * loop, CPU #x will just exit at (C) and miss flushing out all of its
8596446a513SAhmed S. Darwish 	 * added pages.
8606446a513SAhmed S. Darwish 	 */
8616446a513SAhmed S. Darwish 	WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1);
8626446a513SAhmed S. Darwish 	smp_mb();
863eef1a429SKonstantin Khlebnikov 
8645fbc4616SChris Metcalf 	cpumask_clear(&has_work);
8655fbc4616SChris Metcalf 	for_each_online_cpu(cpu) {
8665fbc4616SChris Metcalf 		struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
8675fbc4616SChris Metcalf 
8684864545aSMatthew Wilcox (Oracle) 		if (cpu_needs_drain(cpu)) {
8695fbc4616SChris Metcalf 			INIT_WORK(work, lru_add_drain_per_cpu);
870ce612879SMichal Hocko 			queue_work_on(cpu, mm_percpu_wq, work);
8716446a513SAhmed S. Darwish 			__cpumask_set_cpu(cpu, &has_work);
8725fbc4616SChris Metcalf 		}
8735fbc4616SChris Metcalf 	}
8745fbc4616SChris Metcalf 
8755fbc4616SChris Metcalf 	for_each_cpu(cpu, &has_work)
8765fbc4616SChris Metcalf 		flush_work(&per_cpu(lru_add_drain_work, cpu));
8775fbc4616SChris Metcalf 
878eef1a429SKonstantin Khlebnikov done:
8795fbc4616SChris Metcalf 	mutex_unlock(&lock);
880053837fcSNick Piggin }
881d479960eSMinchan Kim 
882d479960eSMinchan Kim void lru_add_drain_all(void)
883d479960eSMinchan Kim {
884d479960eSMinchan Kim 	__lru_add_drain_all(false);
885d479960eSMinchan Kim }
8866ea183d6SMichal Hocko #else
8876ea183d6SMichal Hocko void lru_add_drain_all(void)
8886ea183d6SMichal Hocko {
8896ea183d6SMichal Hocko 	lru_add_drain();
8906ea183d6SMichal Hocko }
8916446a513SAhmed S. Darwish #endif /* CONFIG_SMP */
892053837fcSNick Piggin 
893d479960eSMinchan Kim atomic_t lru_disable_count = ATOMIC_INIT(0);
894d479960eSMinchan Kim 
895d479960eSMinchan Kim /*
896d479960eSMinchan Kim  * lru_cache_disable() needs to be called before we start compiling
897d479960eSMinchan Kim  * a list of pages to be migrated using isolate_lru_page().
898d479960eSMinchan Kim  * It drains pages on LRU cache and then disable on all cpus until
899d479960eSMinchan Kim  * lru_cache_enable is called.
900d479960eSMinchan Kim  *
901d479960eSMinchan Kim  * Must be paired with a call to lru_cache_enable().
902d479960eSMinchan Kim  */
903d479960eSMinchan Kim void lru_cache_disable(void)
904d479960eSMinchan Kim {
905d479960eSMinchan Kim 	atomic_inc(&lru_disable_count);
906d479960eSMinchan Kim 	/*
907ff042f4aSMarcelo Tosatti 	 * Readers of lru_disable_count are protected by either disabling
908ff042f4aSMarcelo Tosatti 	 * preemption or rcu_read_lock:
909ff042f4aSMarcelo Tosatti 	 *
910ff042f4aSMarcelo Tosatti 	 * preempt_disable, local_irq_disable  [bh_lru_lock()]
911ff042f4aSMarcelo Tosatti 	 * rcu_read_lock		       [rt_spin_lock CONFIG_PREEMPT_RT]
912ff042f4aSMarcelo Tosatti 	 * preempt_disable		       [local_lock !CONFIG_PREEMPT_RT]
913ff042f4aSMarcelo Tosatti 	 *
914ff042f4aSMarcelo Tosatti 	 * Since v5.1 kernel, synchronize_rcu() is guaranteed to wait on
915ff042f4aSMarcelo Tosatti 	 * preempt_disable() regions of code. So any CPU which sees
916ff042f4aSMarcelo Tosatti 	 * lru_disable_count = 0 will have exited the critical
917ff042f4aSMarcelo Tosatti 	 * section when synchronize_rcu() returns.
918d479960eSMinchan Kim 	 */
91931733463SMarcelo Tosatti 	synchronize_rcu_expedited();
920ff042f4aSMarcelo Tosatti #ifdef CONFIG_SMP
921d479960eSMinchan Kim 	__lru_add_drain_all(true);
922d479960eSMinchan Kim #else
923243418e3SMinchan Kim 	lru_add_and_bh_lrus_drain();
924d479960eSMinchan Kim #endif
925d479960eSMinchan Kim }
926d479960eSMinchan Kim 
927aabfb572SMichal Hocko /**
928ea1754a0SKirill A. Shutemov  * release_pages - batched put_page()
929aabfb572SMichal Hocko  * @pages: array of pages to release
930aabfb572SMichal Hocko  * @nr: number of pages
9311da177e4SLinus Torvalds  *
932aabfb572SMichal Hocko  * Decrement the reference count on all the pages in @pages.  If it
933aabfb572SMichal Hocko  * fell to zero, remove the page from the LRU and free it.
9341da177e4SLinus Torvalds  */
935c6f92f9fSMel Gorman void release_pages(struct page **pages, int nr)
9361da177e4SLinus Torvalds {
9371da177e4SLinus Torvalds 	int i;
938cc59850eSKonstantin Khlebnikov 	LIST_HEAD(pages_to_free);
9396168d0daSAlex Shi 	struct lruvec *lruvec = NULL;
9400de340cbSMatthew Wilcox (Oracle) 	unsigned long flags = 0;
9413f649ab7SKees Cook 	unsigned int lock_batch;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	for (i = 0; i < nr; i++) {
944ab5e653eSMatthew Wilcox (Oracle) 		struct folio *folio = page_folio(pages[i]);
9451da177e4SLinus Torvalds 
946aabfb572SMichal Hocko 		/*
947aabfb572SMichal Hocko 		 * Make sure the IRQ-safe lock-holding time does not get
948aabfb572SMichal Hocko 		 * excessive with a continuous string of pages from the
9496168d0daSAlex Shi 		 * same lruvec. The lock is held only if lruvec != NULL.
950aabfb572SMichal Hocko 		 */
9516168d0daSAlex Shi 		if (lruvec && ++lock_batch == SWAP_CLUSTER_MAX) {
9526168d0daSAlex Shi 			unlock_page_lruvec_irqrestore(lruvec, flags);
9536168d0daSAlex Shi 			lruvec = NULL;
954aabfb572SMichal Hocko 		}
955aabfb572SMichal Hocko 
956ab5e653eSMatthew Wilcox (Oracle) 		if (is_huge_zero_page(&folio->page))
957aa88b68cSKirill A. Shutemov 			continue;
958aa88b68cSKirill A. Shutemov 
959ab5e653eSMatthew Wilcox (Oracle) 		if (folio_is_zone_device(folio)) {
9606168d0daSAlex Shi 			if (lruvec) {
9616168d0daSAlex Shi 				unlock_page_lruvec_irqrestore(lruvec, flags);
9626168d0daSAlex Shi 				lruvec = NULL;
963df6ad698SJérôme Glisse 			}
964ab5e653eSMatthew Wilcox (Oracle) 			if (put_devmap_managed_page(&folio->page))
965df6ad698SJérôme Glisse 				continue;
966ab5e653eSMatthew Wilcox (Oracle) 			if (folio_put_testzero(folio))
967ab5e653eSMatthew Wilcox (Oracle) 				free_zone_device_page(&folio->page);
96843fbdeb3SRalph Campbell 			continue;
96907d80269SJohn Hubbard 		}
970df6ad698SJérôme Glisse 
971ab5e653eSMatthew Wilcox (Oracle) 		if (!folio_put_testzero(folio))
9721da177e4SLinus Torvalds 			continue;
9731da177e4SLinus Torvalds 
974ab5e653eSMatthew Wilcox (Oracle) 		if (folio_test_large(folio)) {
9756168d0daSAlex Shi 			if (lruvec) {
9766168d0daSAlex Shi 				unlock_page_lruvec_irqrestore(lruvec, flags);
9776168d0daSAlex Shi 				lruvec = NULL;
978ddc58f27SKirill A. Shutemov 			}
979ab5e653eSMatthew Wilcox (Oracle) 			__put_compound_page(&folio->page);
980ddc58f27SKirill A. Shutemov 			continue;
981ddc58f27SKirill A. Shutemov 		}
982ddc58f27SKirill A. Shutemov 
983ab5e653eSMatthew Wilcox (Oracle) 		if (folio_test_lru(folio)) {
9842a5e4e34SAlexander Duyck 			struct lruvec *prev_lruvec = lruvec;
985894bc310SLee Schermerhorn 
9860de340cbSMatthew Wilcox (Oracle) 			lruvec = folio_lruvec_relock_irqsave(folio, lruvec,
9872a5e4e34SAlexander Duyck 									&flags);
9882a5e4e34SAlexander Duyck 			if (prev_lruvec != lruvec)
989aabfb572SMichal Hocko 				lock_batch = 0;
990fa9add64SHugh Dickins 
991ab5e653eSMatthew Wilcox (Oracle) 			lruvec_del_folio(lruvec, folio);
992ab5e653eSMatthew Wilcox (Oracle) 			__folio_clear_lru_flags(folio);
99346453a6eSNick Piggin 		}
99446453a6eSNick Piggin 
995b109b870SHugh Dickins 		/*
996b109b870SHugh Dickins 		 * In rare cases, when truncation or holepunching raced with
997b109b870SHugh Dickins 		 * munlock after VM_LOCKED was cleared, Mlocked may still be
998b109b870SHugh Dickins 		 * found set here.  This does not indicate a problem, unless
999b109b870SHugh Dickins 		 * "unevictable_pgs_cleared" appears worryingly large.
1000b109b870SHugh Dickins 		 */
1001ab5e653eSMatthew Wilcox (Oracle) 		if (unlikely(folio_test_mlocked(folio))) {
1002ab5e653eSMatthew Wilcox (Oracle) 			__folio_clear_mlocked(folio);
1003ab5e653eSMatthew Wilcox (Oracle) 			zone_stat_sub_folio(folio, NR_MLOCK);
1004b109b870SHugh Dickins 			count_vm_event(UNEVICTABLE_PGCLEARED);
1005b109b870SHugh Dickins 		}
1006b109b870SHugh Dickins 
1007ab5e653eSMatthew Wilcox (Oracle) 		list_add(&folio->lru, &pages_to_free);
10081da177e4SLinus Torvalds 	}
10096168d0daSAlex Shi 	if (lruvec)
10106168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
10111da177e4SLinus Torvalds 
1012747db954SJohannes Weiner 	mem_cgroup_uncharge_list(&pages_to_free);
10132d4894b5SMel Gorman 	free_unref_page_list(&pages_to_free);
10141da177e4SLinus Torvalds }
10150be8557bSMiklos Szeredi EXPORT_SYMBOL(release_pages);
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds /*
10181da177e4SLinus Torvalds  * The pages which we're about to release may be in the deferred lru-addition
10191da177e4SLinus Torvalds  * queues.  That would prevent them from really being freed right now.  That's
10201da177e4SLinus Torvalds  * OK from a correctness point of view but is inefficient - those pages may be
10211da177e4SLinus Torvalds  * cache-warm and we want to give them back to the page allocator ASAP.
10221da177e4SLinus Torvalds  *
102370dea534SMatthew Wilcox (Oracle)  * So __pagevec_release() will drain those queues here.
102470dea534SMatthew Wilcox (Oracle)  * folio_batch_move_lru() calls folios_put() directly to avoid
10251da177e4SLinus Torvalds  * mutual recursion.
10261da177e4SLinus Torvalds  */
10271da177e4SLinus Torvalds void __pagevec_release(struct pagevec *pvec)
10281da177e4SLinus Torvalds {
10297f0b5fb9SMel Gorman 	if (!pvec->percpu_pvec_drained) {
10301da177e4SLinus Torvalds 		lru_add_drain();
10317f0b5fb9SMel Gorman 		pvec->percpu_pvec_drained = true;
1032d9ed0d08SMel Gorman 	}
1033c6f92f9fSMel Gorman 	release_pages(pvec->pages, pagevec_count(pvec));
10341da177e4SLinus Torvalds 	pagevec_reinit(pvec);
10351da177e4SLinus Torvalds }
10367f285701SSteve French EXPORT_SYMBOL(__pagevec_release);
10377f285701SSteve French 
10381da177e4SLinus Torvalds /**
10391613fac9SMatthew Wilcox (Oracle)  * folio_batch_remove_exceptionals() - Prune non-folios from a batch.
10401613fac9SMatthew Wilcox (Oracle)  * @fbatch: The batch to prune
10410cd6144aSJohannes Weiner  *
10421613fac9SMatthew Wilcox (Oracle)  * find_get_entries() fills a batch with both folios and shadow/swap/DAX
10431613fac9SMatthew Wilcox (Oracle)  * entries.  This function prunes all the non-folio entries from @fbatch
10441613fac9SMatthew Wilcox (Oracle)  * without leaving holes, so that it can be passed on to folio-only batch
10451613fac9SMatthew Wilcox (Oracle)  * operations.
10460cd6144aSJohannes Weiner  */
10471613fac9SMatthew Wilcox (Oracle) void folio_batch_remove_exceptionals(struct folio_batch *fbatch)
10480cd6144aSJohannes Weiner {
10491613fac9SMatthew Wilcox (Oracle) 	unsigned int i, j;
10500cd6144aSJohannes Weiner 
10511613fac9SMatthew Wilcox (Oracle) 	for (i = 0, j = 0; i < folio_batch_count(fbatch); i++) {
10521613fac9SMatthew Wilcox (Oracle) 		struct folio *folio = fbatch->folios[i];
10531613fac9SMatthew Wilcox (Oracle) 		if (!xa_is_value(folio))
10541613fac9SMatthew Wilcox (Oracle) 			fbatch->folios[j++] = folio;
10550cd6144aSJohannes Weiner 	}
10561613fac9SMatthew Wilcox (Oracle) 	fbatch->nr = j;
10570cd6144aSJohannes Weiner }
10580cd6144aSJohannes Weiner 
10590cd6144aSJohannes Weiner /**
1060b947cee4SJan Kara  * pagevec_lookup_range - gang pagecache lookup
10611da177e4SLinus Torvalds  * @pvec:	Where the resulting pages are placed
10621da177e4SLinus Torvalds  * @mapping:	The address_space to search
10631da177e4SLinus Torvalds  * @start:	The starting page index
1064b947cee4SJan Kara  * @end:	The final page index
10651da177e4SLinus Torvalds  *
1066e02a9f04SRandy Dunlap  * pagevec_lookup_range() will search for & return a group of up to PAGEVEC_SIZE
1067b947cee4SJan Kara  * pages in the mapping starting from index @start and upto index @end
1068b947cee4SJan Kara  * (inclusive).  The pages are placed in @pvec.  pagevec_lookup() takes a
10691da177e4SLinus Torvalds  * reference against the pages in @pvec.
10701da177e4SLinus Torvalds  *
10711da177e4SLinus Torvalds  * The search returns a group of mapping-contiguous pages with ascending
1072d72dc8a2SJan Kara  * indexes.  There may be holes in the indices due to not-present pages. We
1073d72dc8a2SJan Kara  * also update @start to index the next page for the traversal.
10741da177e4SLinus Torvalds  *
1075b947cee4SJan Kara  * pagevec_lookup_range() returns the number of pages which were found. If this
1076e02a9f04SRandy Dunlap  * number is smaller than PAGEVEC_SIZE, the end of specified range has been
1077b947cee4SJan Kara  * reached.
10781da177e4SLinus Torvalds  */
1079b947cee4SJan Kara unsigned pagevec_lookup_range(struct pagevec *pvec,
1080397162ffSJan Kara 		struct address_space *mapping, pgoff_t *start, pgoff_t end)
10811da177e4SLinus Torvalds {
1082397162ffSJan Kara 	pvec->nr = find_get_pages_range(mapping, start, end, PAGEVEC_SIZE,
1083b947cee4SJan Kara 					pvec->pages);
10841da177e4SLinus Torvalds 	return pagevec_count(pvec);
10851da177e4SLinus Torvalds }
1086b947cee4SJan Kara EXPORT_SYMBOL(pagevec_lookup_range);
108778539fdfSChristoph Hellwig 
108872b045aeSJan Kara unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
108972b045aeSJan Kara 		struct address_space *mapping, pgoff_t *index, pgoff_t end,
109010bbd235SMatthew Wilcox 		xa_mark_t tag)
10911da177e4SLinus Torvalds {
109272b045aeSJan Kara 	pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
109367fd707fSJan Kara 					PAGEVEC_SIZE, pvec->pages);
10941da177e4SLinus Torvalds 	return pagevec_count(pvec);
10951da177e4SLinus Torvalds }
109672b045aeSJan Kara EXPORT_SYMBOL(pagevec_lookup_range_tag);
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds /*
10991da177e4SLinus Torvalds  * Perform any setup for the swap system
11001da177e4SLinus Torvalds  */
11011da177e4SLinus Torvalds void __init swap_setup(void)
11021da177e4SLinus Torvalds {
1103ca79b0c2SArun KS 	unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
1104e0bf68ddSPeter Zijlstra 
11051da177e4SLinus Torvalds 	/* Use a smaller cluster for small-memory machines */
11061da177e4SLinus Torvalds 	if (megs < 16)
11071da177e4SLinus Torvalds 		page_cluster = 2;
11081da177e4SLinus Torvalds 	else
11091da177e4SLinus Torvalds 		page_cluster = 3;
11101da177e4SLinus Torvalds 	/*
11111da177e4SLinus Torvalds 	 * Right now other parts of the system means that we
11121da177e4SLinus Torvalds 	 * _really_ don't want to cluster much more
11131da177e4SLinus Torvalds 	 */
11141da177e4SLinus Torvalds }
1115