xref: /linux/mm/swap.c (revision 31733463372e8d88ea54bfa1e35178aad9b2ffd2)
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 
49b01b2141SIngo Molnar /* Protecting only lru_rotate.pvec which requires disabling interrupts */
50b01b2141SIngo Molnar struct lru_rotate {
51b01b2141SIngo Molnar 	local_lock_t lock;
52b01b2141SIngo Molnar 	struct pagevec pvec;
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 /*
59b01b2141SIngo Molnar  * The following struct pagevec are grouped together because they are protected
60b01b2141SIngo Molnar  * by disabling preemption (and interrupts remain enabled).
61b01b2141SIngo Molnar  */
62b01b2141SIngo Molnar struct lru_pvecs {
63b01b2141SIngo Molnar 	local_lock_t lock;
64b01b2141SIngo Molnar 	struct pagevec lru_add;
65b01b2141SIngo Molnar 	struct pagevec lru_deactivate_file;
66b01b2141SIngo Molnar 	struct pagevec lru_deactivate;
67b01b2141SIngo Molnar 	struct pagevec lru_lazyfree;
68a4a921aaSMing Li #ifdef CONFIG_SMP
69b01b2141SIngo Molnar 	struct pagevec activate_page;
70a4a921aaSMing Li #endif
71b01b2141SIngo Molnar };
72b01b2141SIngo Molnar static DEFINE_PER_CPU(struct lru_pvecs, lru_pvecs) = {
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 
122ddc58f27SKirill A. Shutemov void __put_page(struct page *page)
123c747ce79SJianyu Zhan {
12427674ef6SChristoph Hellwig 	if (unlikely(is_zone_device_page(page)))
12527674ef6SChristoph Hellwig 		free_zone_device_page(page);
12627674ef6SChristoph Hellwig 	else if (unlikely(PageCompound(page)))
12726296ad2SAndrew Morton 		__put_compound_page(page);
12826296ad2SAndrew Morton 	else
12926296ad2SAndrew Morton 		__put_single_page(page);
13026296ad2SAndrew Morton }
131ddc58f27SKirill A. Shutemov EXPORT_SYMBOL(__put_page);
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 {
141988c69f1SMatthew Wilcox (Oracle) 	struct page *page, *next;
1421d7ea732SAlexander Zarochentsev 
143988c69f1SMatthew Wilcox (Oracle) 	list_for_each_entry_safe(page, next, pages, lru) {
144988c69f1SMatthew Wilcox (Oracle) 		if (!put_page_testzero(page)) {
145988c69f1SMatthew Wilcox (Oracle) 			list_del(&page->lru);
146988c69f1SMatthew Wilcox (Oracle) 			continue;
1471d7ea732SAlexander Zarochentsev 		}
148988c69f1SMatthew Wilcox (Oracle) 		if (PageHead(page)) {
149988c69f1SMatthew Wilcox (Oracle) 			list_del(&page->lru);
150988c69f1SMatthew Wilcox (Oracle) 			__put_compound_page(page);
151988c69f1SMatthew Wilcox (Oracle) 			continue;
152988c69f1SMatthew Wilcox (Oracle) 		}
153988c69f1SMatthew Wilcox (Oracle) 		/* Cannot be PageLRU because it's passed to us 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 
1913dd7ae8eSShaohua Li static void pagevec_lru_move_fn(struct pagevec *pvec,
192c7c7b80cSAlex Shi 	void (*move_fn)(struct page *page, struct lruvec *lruvec))
193902aaed0SHisashi Hifumi {
194902aaed0SHisashi Hifumi 	int i;
1956168d0daSAlex Shi 	struct lruvec *lruvec = NULL;
1963dd7ae8eSShaohua Li 	unsigned long flags = 0;
197902aaed0SHisashi Hifumi 
198902aaed0SHisashi Hifumi 	for (i = 0; i < pagevec_count(pvec); i++) {
199902aaed0SHisashi Hifumi 		struct page *page = pvec->pages[i];
2000de340cbSMatthew Wilcox (Oracle) 		struct folio *folio = page_folio(page);
2013dd7ae8eSShaohua Li 
202fc574c23SAlex Shi 		/* block memcg migration during page moving between lru */
203fc574c23SAlex Shi 		if (!TestClearPageLRU(page))
204fc574c23SAlex Shi 			continue;
205fc574c23SAlex Shi 
2060de340cbSMatthew Wilcox (Oracle) 		lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags);
207c7c7b80cSAlex Shi 		(*move_fn)(page, lruvec);
208fc574c23SAlex Shi 
209fc574c23SAlex Shi 		SetPageLRU(page);
2103dd7ae8eSShaohua Li 	}
2116168d0daSAlex Shi 	if (lruvec)
2126168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
213c6f92f9fSMel Gorman 	release_pages(pvec->pages, pvec->nr);
2143dd7ae8eSShaohua Li 	pagevec_reinit(pvec);
2153dd7ae8eSShaohua Li }
2163dd7ae8eSShaohua Li 
217c7c7b80cSAlex Shi static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec)
2183dd7ae8eSShaohua Li {
219575ced1cSMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
220575ced1cSMatthew Wilcox (Oracle) 
221575ced1cSMatthew Wilcox (Oracle) 	if (!folio_test_unevictable(folio)) {
222575ced1cSMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
223575ced1cSMatthew Wilcox (Oracle) 		folio_clear_active(folio);
224575ced1cSMatthew Wilcox (Oracle) 		lruvec_add_folio_tail(lruvec, folio);
225575ced1cSMatthew Wilcox (Oracle) 		__count_vm_events(PGROTATED, folio_nr_pages(folio));
226902aaed0SHisashi Hifumi 	}
227902aaed0SHisashi Hifumi }
2283dd7ae8eSShaohua Li 
229d479960eSMinchan Kim /* return true if pagevec needs to drain */
230d479960eSMinchan Kim static bool pagevec_add_and_need_flush(struct pagevec *pvec, struct page *page)
231d479960eSMinchan Kim {
232d479960eSMinchan Kim 	bool ret = false;
233d479960eSMinchan Kim 
234d479960eSMinchan Kim 	if (!pagevec_add(pvec, page) || PageCompound(page) ||
235d479960eSMinchan Kim 			lru_cache_disabled())
236d479960eSMinchan Kim 		ret = true;
237d479960eSMinchan Kim 
238d479960eSMinchan Kim 	return ret;
239d479960eSMinchan Kim }
240d479960eSMinchan Kim 
2413dd7ae8eSShaohua Li /*
242575ced1cSMatthew Wilcox (Oracle)  * Writeback is about to end against a folio which has been marked for
243575ced1cSMatthew Wilcox (Oracle)  * immediate reclaim.  If it still appears to be reclaimable, move it
244575ced1cSMatthew Wilcox (Oracle)  * to the tail of the inactive list.
245c7c7b80cSAlex Shi  *
246575ced1cSMatthew Wilcox (Oracle)  * folio_rotate_reclaimable() must disable IRQs, to prevent nasty races.
2471da177e4SLinus Torvalds  */
248575ced1cSMatthew Wilcox (Oracle) void folio_rotate_reclaimable(struct folio *folio)
2491da177e4SLinus Torvalds {
250575ced1cSMatthew Wilcox (Oracle) 	if (!folio_test_locked(folio) && !folio_test_dirty(folio) &&
251575ced1cSMatthew Wilcox (Oracle) 	    !folio_test_unevictable(folio) && folio_test_lru(folio)) {
252902aaed0SHisashi Hifumi 		struct pagevec *pvec;
2531da177e4SLinus Torvalds 		unsigned long flags;
2541da177e4SLinus Torvalds 
255575ced1cSMatthew Wilcox (Oracle) 		folio_get(folio);
256b01b2141SIngo Molnar 		local_lock_irqsave(&lru_rotate.lock, flags);
257b01b2141SIngo Molnar 		pvec = this_cpu_ptr(&lru_rotate.pvec);
258575ced1cSMatthew Wilcox (Oracle) 		if (pagevec_add_and_need_flush(pvec, &folio->page))
259c7c7b80cSAlex Shi 			pagevec_lru_move_fn(pvec, pagevec_move_tail_fn);
260b01b2141SIngo Molnar 		local_unlock_irqrestore(&lru_rotate.lock, flags);
261ac6aadb2SMiklos Szeredi 	}
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds 
26496f8bf4fSJohannes Weiner void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages)
2653e2f41f1SKOSAKI Motohiro {
2667cf111bcSJohannes Weiner 	do {
2677cf111bcSJohannes Weiner 		unsigned long lrusize;
2687cf111bcSJohannes Weiner 
2696168d0daSAlex Shi 		/*
2706168d0daSAlex Shi 		 * Hold lruvec->lru_lock is safe here, since
2716168d0daSAlex Shi 		 * 1) The pinned lruvec in reclaim, or
2726168d0daSAlex Shi 		 * 2) From a pre-LRU page during refault (which also holds the
2736168d0daSAlex Shi 		 *    rcu lock, so would be safe even if the page was on the LRU
2746168d0daSAlex Shi 		 *    and could move simultaneously to a new lruvec).
2756168d0daSAlex Shi 		 */
2766168d0daSAlex Shi 		spin_lock_irq(&lruvec->lru_lock);
2777cf111bcSJohannes Weiner 		/* Record cost event */
27896f8bf4fSJohannes Weiner 		if (file)
27996f8bf4fSJohannes Weiner 			lruvec->file_cost += nr_pages;
2801431d4d1SJohannes Weiner 		else
28196f8bf4fSJohannes Weiner 			lruvec->anon_cost += nr_pages;
2827cf111bcSJohannes Weiner 
2837cf111bcSJohannes Weiner 		/*
2847cf111bcSJohannes Weiner 		 * Decay previous events
2857cf111bcSJohannes Weiner 		 *
2867cf111bcSJohannes Weiner 		 * Because workloads change over time (and to avoid
2877cf111bcSJohannes Weiner 		 * overflow) we keep these statistics as a floating
2887cf111bcSJohannes Weiner 		 * average, which ends up weighing recent refaults
2897cf111bcSJohannes Weiner 		 * more than old ones.
2907cf111bcSJohannes Weiner 		 */
2917cf111bcSJohannes Weiner 		lrusize = lruvec_page_state(lruvec, NR_INACTIVE_ANON) +
2927cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_ACTIVE_ANON) +
2937cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_INACTIVE_FILE) +
2947cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_ACTIVE_FILE);
2957cf111bcSJohannes Weiner 
2967cf111bcSJohannes Weiner 		if (lruvec->file_cost + lruvec->anon_cost > lrusize / 4) {
2977cf111bcSJohannes Weiner 			lruvec->file_cost /= 2;
2987cf111bcSJohannes Weiner 			lruvec->anon_cost /= 2;
2997cf111bcSJohannes Weiner 		}
3006168d0daSAlex Shi 		spin_unlock_irq(&lruvec->lru_lock);
3017cf111bcSJohannes Weiner 	} while ((lruvec = parent_lruvec(lruvec)));
3023e2f41f1SKOSAKI Motohiro }
3033e2f41f1SKOSAKI Motohiro 
3040995d7e5SMatthew Wilcox (Oracle) void lru_note_cost_folio(struct folio *folio)
30596f8bf4fSJohannes Weiner {
3060995d7e5SMatthew Wilcox (Oracle) 	lru_note_cost(folio_lruvec(folio), folio_is_file_lru(folio),
3070995d7e5SMatthew Wilcox (Oracle) 			folio_nr_pages(folio));
30896f8bf4fSJohannes Weiner }
30996f8bf4fSJohannes Weiner 
310f2d27392SMatthew Wilcox (Oracle) static void __folio_activate(struct folio *folio, struct lruvec *lruvec)
311744ed144SShaohua Li {
312f2d27392SMatthew Wilcox (Oracle) 	if (!folio_test_active(folio) && !folio_test_unevictable(folio)) {
313f2d27392SMatthew Wilcox (Oracle) 		long nr_pages = folio_nr_pages(folio);
314744ed144SShaohua Li 
315f2d27392SMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
316f2d27392SMatthew Wilcox (Oracle) 		folio_set_active(folio);
317f2d27392SMatthew Wilcox (Oracle) 		lruvec_add_folio(lruvec, folio);
318f2d27392SMatthew Wilcox (Oracle) 		trace_mm_lru_activate(folio);
3197a608572SLinus Torvalds 
32021e330fcSShakeel Butt 		__count_vm_events(PGACTIVATE, nr_pages);
32121e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGACTIVATE,
32221e330fcSShakeel Butt 				     nr_pages);
323744ed144SShaohua Li 	}
324eb709b0dSShaohua Li }
325eb709b0dSShaohua Li 
326eb709b0dSShaohua Li #ifdef CONFIG_SMP
327f2d27392SMatthew Wilcox (Oracle) static void __activate_page(struct page *page, struct lruvec *lruvec)
328f2d27392SMatthew Wilcox (Oracle) {
329f2d27392SMatthew Wilcox (Oracle) 	return __folio_activate(page_folio(page), lruvec);
330f2d27392SMatthew Wilcox (Oracle) }
331f2d27392SMatthew Wilcox (Oracle) 
332eb709b0dSShaohua Li static void activate_page_drain(int cpu)
333eb709b0dSShaohua Li {
334b01b2141SIngo Molnar 	struct pagevec *pvec = &per_cpu(lru_pvecs.activate_page, cpu);
335eb709b0dSShaohua Li 
336eb709b0dSShaohua Li 	if (pagevec_count(pvec))
337c7c7b80cSAlex Shi 		pagevec_lru_move_fn(pvec, __activate_page);
338eb709b0dSShaohua Li }
339eb709b0dSShaohua Li 
3405fbc4616SChris Metcalf static bool need_activate_page_drain(int cpu)
3415fbc4616SChris Metcalf {
342b01b2141SIngo Molnar 	return pagevec_count(&per_cpu(lru_pvecs.activate_page, cpu)) != 0;
3435fbc4616SChris Metcalf }
3445fbc4616SChris Metcalf 
345f2d27392SMatthew Wilcox (Oracle) static void folio_activate(struct folio *folio)
346eb709b0dSShaohua Li {
347f2d27392SMatthew Wilcox (Oracle) 	if (folio_test_lru(folio) && !folio_test_active(folio) &&
348f2d27392SMatthew Wilcox (Oracle) 	    !folio_test_unevictable(folio)) {
349b01b2141SIngo Molnar 		struct pagevec *pvec;
350eb709b0dSShaohua Li 
351f2d27392SMatthew Wilcox (Oracle) 		folio_get(folio);
352b01b2141SIngo Molnar 		local_lock(&lru_pvecs.lock);
353b01b2141SIngo Molnar 		pvec = this_cpu_ptr(&lru_pvecs.activate_page);
354f2d27392SMatthew Wilcox (Oracle) 		if (pagevec_add_and_need_flush(pvec, &folio->page))
355c7c7b80cSAlex Shi 			pagevec_lru_move_fn(pvec, __activate_page);
356b01b2141SIngo Molnar 		local_unlock(&lru_pvecs.lock);
357eb709b0dSShaohua Li 	}
358eb709b0dSShaohua Li }
359eb709b0dSShaohua Li 
360eb709b0dSShaohua Li #else
361eb709b0dSShaohua Li static inline void activate_page_drain(int cpu)
362eb709b0dSShaohua Li {
363eb709b0dSShaohua Li }
364eb709b0dSShaohua Li 
365f2d27392SMatthew Wilcox (Oracle) static void folio_activate(struct folio *folio)
366eb709b0dSShaohua Li {
3676168d0daSAlex Shi 	struct lruvec *lruvec;
368eb709b0dSShaohua Li 
369f2d27392SMatthew Wilcox (Oracle) 	if (folio_test_clear_lru(folio)) {
370e809c3feSMatthew Wilcox (Oracle) 		lruvec = folio_lruvec_lock_irq(folio);
371f2d27392SMatthew Wilcox (Oracle) 		__folio_activate(folio, lruvec);
3726168d0daSAlex Shi 		unlock_page_lruvec_irq(lruvec);
373f2d27392SMatthew Wilcox (Oracle) 		folio_set_lru(folio);
3746168d0daSAlex Shi 	}
3751da177e4SLinus Torvalds }
376eb709b0dSShaohua Li #endif
3771da177e4SLinus Torvalds 
37876580b65SMatthew Wilcox (Oracle) static void __lru_cache_activate_folio(struct folio *folio)
379059285a2SMel Gorman {
380b01b2141SIngo Molnar 	struct pagevec *pvec;
381059285a2SMel Gorman 	int i;
382059285a2SMel Gorman 
383b01b2141SIngo Molnar 	local_lock(&lru_pvecs.lock);
384b01b2141SIngo Molnar 	pvec = this_cpu_ptr(&lru_pvecs.lru_add);
385b01b2141SIngo Molnar 
386059285a2SMel Gorman 	/*
387059285a2SMel Gorman 	 * Search backwards on the optimistic assumption that the page being
388059285a2SMel Gorman 	 * activated has just been added to this pagevec. Note that only
389059285a2SMel Gorman 	 * the local pagevec is examined as a !PageLRU page could be in the
390059285a2SMel Gorman 	 * process of being released, reclaimed, migrated or on a remote
391059285a2SMel Gorman 	 * pagevec that is currently being drained. Furthermore, marking
392059285a2SMel Gorman 	 * a remote pagevec's page PageActive potentially hits a race where
393059285a2SMel Gorman 	 * a page is marked PageActive just after it is added to the inactive
394059285a2SMel Gorman 	 * list causing accounting errors and BUG_ON checks to trigger.
395059285a2SMel Gorman 	 */
396059285a2SMel Gorman 	for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
397059285a2SMel Gorman 		struct page *pagevec_page = pvec->pages[i];
398059285a2SMel Gorman 
39976580b65SMatthew Wilcox (Oracle) 		if (pagevec_page == &folio->page) {
40076580b65SMatthew Wilcox (Oracle) 			folio_set_active(folio);
401059285a2SMel Gorman 			break;
402059285a2SMel Gorman 		}
403059285a2SMel Gorman 	}
404059285a2SMel Gorman 
405b01b2141SIngo Molnar 	local_unlock(&lru_pvecs.lock);
406059285a2SMel Gorman }
407059285a2SMel Gorman 
4081da177e4SLinus Torvalds /*
4091da177e4SLinus Torvalds  * Mark a page as having seen activity.
4101da177e4SLinus Torvalds  *
4111da177e4SLinus Torvalds  * inactive,unreferenced	->	inactive,referenced
4121da177e4SLinus Torvalds  * inactive,referenced		->	active,unreferenced
4131da177e4SLinus Torvalds  * active,unreferenced		->	active,referenced
414eb39d618SHugh Dickins  *
415eb39d618SHugh Dickins  * When a newly allocated page is not yet visible, so safe for non-atomic ops,
416eb39d618SHugh Dickins  * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
4171da177e4SLinus Torvalds  */
41876580b65SMatthew Wilcox (Oracle) void folio_mark_accessed(struct folio *folio)
4191da177e4SLinus Torvalds {
42076580b65SMatthew Wilcox (Oracle) 	if (!folio_test_referenced(folio)) {
42176580b65SMatthew Wilcox (Oracle) 		folio_set_referenced(folio);
42276580b65SMatthew Wilcox (Oracle) 	} else if (folio_test_unevictable(folio)) {
423a1100a74SFengguang Wu 		/*
424a1100a74SFengguang Wu 		 * Unevictable pages are on the "LRU_UNEVICTABLE" list. But,
425a1100a74SFengguang Wu 		 * this list is never rotated or maintained, so marking an
426914c32e4SBang Li 		 * unevictable page accessed has no effect.
427a1100a74SFengguang Wu 		 */
42876580b65SMatthew Wilcox (Oracle) 	} else if (!folio_test_active(folio)) {
429059285a2SMel Gorman 		/*
430059285a2SMel Gorman 		 * If the page is on the LRU, queue it for activation via
431b01b2141SIngo Molnar 		 * lru_pvecs.activate_page. Otherwise, assume the page is on a
432059285a2SMel Gorman 		 * pagevec, mark it active and it'll be moved to the active
433059285a2SMel Gorman 		 * LRU on the next drain.
434059285a2SMel Gorman 		 */
43576580b65SMatthew Wilcox (Oracle) 		if (folio_test_lru(folio))
43676580b65SMatthew Wilcox (Oracle) 			folio_activate(folio);
437059285a2SMel Gorman 		else
43876580b65SMatthew Wilcox (Oracle) 			__lru_cache_activate_folio(folio);
43976580b65SMatthew Wilcox (Oracle) 		folio_clear_referenced(folio);
44076580b65SMatthew Wilcox (Oracle) 		workingset_activation(folio);
4411da177e4SLinus Torvalds 	}
44276580b65SMatthew Wilcox (Oracle) 	if (folio_test_idle(folio))
44376580b65SMatthew Wilcox (Oracle) 		folio_clear_idle(folio);
4441da177e4SLinus Torvalds }
44576580b65SMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_mark_accessed);
4461da177e4SLinus Torvalds 
447f04e9ebbSKOSAKI Motohiro /**
4480d31125dSMatthew Wilcox (Oracle)  * folio_add_lru - Add a folio to an LRU list.
4490d31125dSMatthew Wilcox (Oracle)  * @folio: The folio to be added to the LRU.
4502329d375SJianyu Zhan  *
4510d31125dSMatthew Wilcox (Oracle)  * Queue the folio for addition to the LRU. The decision on whether
4522329d375SJianyu Zhan  * to add the page to the [in]active [file|anon] list is deferred until the
4530d31125dSMatthew Wilcox (Oracle)  * pagevec is drained. This gives a chance for the caller of folio_add_lru()
4540d31125dSMatthew Wilcox (Oracle)  * have the folio added to the active list using folio_mark_accessed().
455f04e9ebbSKOSAKI Motohiro  */
4560d31125dSMatthew Wilcox (Oracle) void folio_add_lru(struct folio *folio)
4571da177e4SLinus Torvalds {
4586058eaecSJohannes Weiner 	struct pagevec *pvec;
4596058eaecSJohannes Weiner 
4600d31125dSMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_active(folio) && folio_test_unevictable(folio), folio);
4610d31125dSMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
4626058eaecSJohannes Weiner 
4630d31125dSMatthew Wilcox (Oracle) 	folio_get(folio);
4646058eaecSJohannes Weiner 	local_lock(&lru_pvecs.lock);
4656058eaecSJohannes Weiner 	pvec = this_cpu_ptr(&lru_pvecs.lru_add);
4660d31125dSMatthew Wilcox (Oracle) 	if (pagevec_add_and_need_flush(pvec, &folio->page))
4676058eaecSJohannes Weiner 		__pagevec_lru_add(pvec);
4686058eaecSJohannes Weiner 	local_unlock(&lru_pvecs.lock);
4691da177e4SLinus Torvalds }
4700d31125dSMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_add_lru);
4711da177e4SLinus Torvalds 
472894bc310SLee Schermerhorn /**
473b518154eSJoonsoo Kim  * lru_cache_add_inactive_or_unevictable
47400501b53SJohannes Weiner  * @page:  the page to be added to LRU
47500501b53SJohannes Weiner  * @vma:   vma in which page is mapped for determining reclaimability
47600501b53SJohannes Weiner  *
477b518154eSJoonsoo Kim  * Place @page on the inactive or unevictable LRU list, depending on its
47812eab428SMiaohe Lin  * evictability.
47900501b53SJohannes Weiner  */
480b518154eSJoonsoo Kim void lru_cache_add_inactive_or_unevictable(struct page *page,
48100501b53SJohannes Weiner 					 struct vm_area_struct *vma)
48200501b53SJohannes Weiner {
48300501b53SJohannes Weiner 	VM_BUG_ON_PAGE(PageLRU(page), page);
48400501b53SJohannes Weiner 
4852fbb0c10SHugh Dickins 	if (unlikely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED))
4862fbb0c10SHugh Dickins 		mlock_new_page(page);
4872fbb0c10SHugh Dickins 	else
4889c4e6b1aSShakeel Butt 		lru_cache_add(page);
48900501b53SJohannes Weiner }
49000501b53SJohannes Weiner 
491902aaed0SHisashi Hifumi /*
49231560180SMinchan Kim  * If the page can not be invalidated, it is moved to the
49331560180SMinchan Kim  * inactive list to speed up its reclaim.  It is moved to the
49431560180SMinchan Kim  * head of the list, rather than the tail, to give the flusher
49531560180SMinchan Kim  * threads some time to write it out, as this is much more
49631560180SMinchan Kim  * effective than the single-page writeout from reclaim.
497278df9f4SMinchan Kim  *
498278df9f4SMinchan Kim  * If the page isn't page_mapped and dirty/writeback, the page
499278df9f4SMinchan Kim  * could reclaim asap using PG_reclaim.
500278df9f4SMinchan Kim  *
501278df9f4SMinchan Kim  * 1. active, mapped page -> none
502278df9f4SMinchan Kim  * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
503278df9f4SMinchan Kim  * 3. inactive, mapped page -> none
504278df9f4SMinchan Kim  * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
505278df9f4SMinchan Kim  * 5. inactive, clean -> inactive, tail
506278df9f4SMinchan Kim  * 6. Others -> none
507278df9f4SMinchan Kim  *
508278df9f4SMinchan Kim  * In 4, why it moves inactive's head, the VM expects the page would
509278df9f4SMinchan Kim  * be write it out by flusher threads as this is much more effective
510278df9f4SMinchan Kim  * than the single-page writeout from reclaim.
51131560180SMinchan Kim  */
512c7c7b80cSAlex Shi static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec)
51331560180SMinchan Kim {
51446ae6b2cSYu Zhao 	bool active = PageActive(page);
5156c357848SMatthew Wilcox (Oracle) 	int nr_pages = thp_nr_pages(page);
51631560180SMinchan Kim 
517bad49d9cSMinchan Kim 	if (PageUnevictable(page))
518bad49d9cSMinchan Kim 		return;
519bad49d9cSMinchan Kim 
52031560180SMinchan Kim 	/* Some processes are using the page */
52131560180SMinchan Kim 	if (page_mapped(page))
52231560180SMinchan Kim 		return;
52331560180SMinchan Kim 
52446ae6b2cSYu Zhao 	del_page_from_lru_list(page, lruvec);
52531560180SMinchan Kim 	ClearPageActive(page);
52631560180SMinchan Kim 	ClearPageReferenced(page);
52731560180SMinchan Kim 
528278df9f4SMinchan Kim 	if (PageWriteback(page) || PageDirty(page)) {
529278df9f4SMinchan Kim 		/*
530278df9f4SMinchan Kim 		 * PG_reclaim could be raced with end_page_writeback
531278df9f4SMinchan Kim 		 * It can make readahead confusing.  But race window
532278df9f4SMinchan Kim 		 * is _really_ small and  it's non-critical problem.
533278df9f4SMinchan Kim 		 */
5343a9c9788SYu Zhao 		add_page_to_lru_list(page, lruvec);
535278df9f4SMinchan Kim 		SetPageReclaim(page);
536278df9f4SMinchan Kim 	} else {
537278df9f4SMinchan Kim 		/*
538278df9f4SMinchan Kim 		 * The page's writeback ends up during pagevec
539c4ffefd1SHyeonggon Yoo 		 * We move that page into tail of inactive.
540278df9f4SMinchan Kim 		 */
5413a9c9788SYu Zhao 		add_page_to_lru_list_tail(page, lruvec);
5425d91f31fSShakeel Butt 		__count_vm_events(PGROTATED, nr_pages);
543278df9f4SMinchan Kim 	}
544278df9f4SMinchan Kim 
54521e330fcSShakeel Butt 	if (active) {
5465d91f31fSShakeel Butt 		__count_vm_events(PGDEACTIVATE, nr_pages);
54721e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
54821e330fcSShakeel Butt 				     nr_pages);
54921e330fcSShakeel Butt 	}
55031560180SMinchan Kim }
55131560180SMinchan Kim 
552c7c7b80cSAlex Shi static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec)
5539c276cc6SMinchan Kim {
554fc574c23SAlex Shi 	if (PageActive(page) && !PageUnevictable(page)) {
5556c357848SMatthew Wilcox (Oracle) 		int nr_pages = thp_nr_pages(page);
5569c276cc6SMinchan Kim 
55746ae6b2cSYu Zhao 		del_page_from_lru_list(page, lruvec);
5589c276cc6SMinchan Kim 		ClearPageActive(page);
5599c276cc6SMinchan Kim 		ClearPageReferenced(page);
5603a9c9788SYu Zhao 		add_page_to_lru_list(page, lruvec);
5619c276cc6SMinchan Kim 
56221e330fcSShakeel Butt 		__count_vm_events(PGDEACTIVATE, nr_pages);
56321e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
56421e330fcSShakeel Butt 				     nr_pages);
5659c276cc6SMinchan Kim 	}
5669c276cc6SMinchan Kim }
56710853a03SMinchan Kim 
568c7c7b80cSAlex Shi static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec)
56910853a03SMinchan Kim {
570fc574c23SAlex Shi 	if (PageAnon(page) && PageSwapBacked(page) &&
57124c92eb7SShaohua Li 	    !PageSwapCache(page) && !PageUnevictable(page)) {
5726c357848SMatthew Wilcox (Oracle) 		int nr_pages = thp_nr_pages(page);
57310853a03SMinchan Kim 
57446ae6b2cSYu Zhao 		del_page_from_lru_list(page, lruvec);
57510853a03SMinchan Kim 		ClearPageActive(page);
57610853a03SMinchan Kim 		ClearPageReferenced(page);
577f7ad2a6cSShaohua Li 		/*
5789de4f22aSHuang Ying 		 * Lazyfree pages are clean anonymous pages.  They have
5799de4f22aSHuang Ying 		 * PG_swapbacked flag cleared, to distinguish them from normal
5809de4f22aSHuang Ying 		 * anonymous pages
581f7ad2a6cSShaohua Li 		 */
582f7ad2a6cSShaohua Li 		ClearPageSwapBacked(page);
5833a9c9788SYu Zhao 		add_page_to_lru_list(page, lruvec);
58410853a03SMinchan Kim 
58521e330fcSShakeel Butt 		__count_vm_events(PGLAZYFREE, nr_pages);
58621e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE,
58721e330fcSShakeel Butt 				     nr_pages);
58810853a03SMinchan Kim 	}
58910853a03SMinchan Kim }
59010853a03SMinchan Kim 
59131560180SMinchan Kim /*
592902aaed0SHisashi Hifumi  * Drain pages out of the cpu's pagevecs.
593902aaed0SHisashi Hifumi  * Either "cpu" is the current CPU, and preemption has already been
594902aaed0SHisashi Hifumi  * disabled; or "cpu" is being hot-unplugged, and is already dead.
595902aaed0SHisashi Hifumi  */
596f0cb3c76SKonstantin Khlebnikov void lru_add_drain_cpu(int cpu)
5971da177e4SLinus Torvalds {
598b01b2141SIngo Molnar 	struct pagevec *pvec = &per_cpu(lru_pvecs.lru_add, cpu);
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	if (pagevec_count(pvec))
601a0b8cab3SMel Gorman 		__pagevec_lru_add(pvec);
602902aaed0SHisashi Hifumi 
603b01b2141SIngo Molnar 	pvec = &per_cpu(lru_rotate.pvec, cpu);
6047e0cc01eSQian Cai 	/* Disabling interrupts below acts as a compiler barrier. */
6057e0cc01eSQian Cai 	if (data_race(pagevec_count(pvec))) {
606902aaed0SHisashi Hifumi 		unsigned long flags;
607902aaed0SHisashi Hifumi 
608902aaed0SHisashi Hifumi 		/* No harm done if a racing interrupt already did this */
609b01b2141SIngo Molnar 		local_lock_irqsave(&lru_rotate.lock, flags);
610c7c7b80cSAlex Shi 		pagevec_lru_move_fn(pvec, pagevec_move_tail_fn);
611b01b2141SIngo Molnar 		local_unlock_irqrestore(&lru_rotate.lock, flags);
612902aaed0SHisashi Hifumi 	}
61331560180SMinchan Kim 
614b01b2141SIngo Molnar 	pvec = &per_cpu(lru_pvecs.lru_deactivate_file, cpu);
61531560180SMinchan Kim 	if (pagevec_count(pvec))
616c7c7b80cSAlex Shi 		pagevec_lru_move_fn(pvec, lru_deactivate_file_fn);
617eb709b0dSShaohua Li 
618b01b2141SIngo Molnar 	pvec = &per_cpu(lru_pvecs.lru_deactivate, cpu);
6199c276cc6SMinchan Kim 	if (pagevec_count(pvec))
620c7c7b80cSAlex Shi 		pagevec_lru_move_fn(pvec, lru_deactivate_fn);
6219c276cc6SMinchan Kim 
622b01b2141SIngo Molnar 	pvec = &per_cpu(lru_pvecs.lru_lazyfree, cpu);
62310853a03SMinchan Kim 	if (pagevec_count(pvec))
624c7c7b80cSAlex Shi 		pagevec_lru_move_fn(pvec, lru_lazyfree_fn);
62510853a03SMinchan Kim 
626eb709b0dSShaohua Li 	activate_page_drain(cpu);
62731560180SMinchan Kim }
62831560180SMinchan Kim 
62931560180SMinchan Kim /**
630261b6840SMatthew Wilcox (Oracle)  * deactivate_file_folio() - Forcefully deactivate a file folio.
631261b6840SMatthew Wilcox (Oracle)  * @folio: Folio to deactivate.
63231560180SMinchan Kim  *
633261b6840SMatthew Wilcox (Oracle)  * This function hints to the VM that @folio is a good reclaim candidate,
634261b6840SMatthew Wilcox (Oracle)  * for example if its invalidation fails due to the folio being dirty
63531560180SMinchan Kim  * or under writeback.
636261b6840SMatthew Wilcox (Oracle)  *
637261b6840SMatthew Wilcox (Oracle)  * Context: Caller holds a reference on the page.
63831560180SMinchan Kim  */
639261b6840SMatthew Wilcox (Oracle) void deactivate_file_folio(struct folio *folio)
64031560180SMinchan Kim {
641b01b2141SIngo Molnar 	struct pagevec *pvec;
642b01b2141SIngo Molnar 
643261b6840SMatthew Wilcox (Oracle) 	/*
644261b6840SMatthew Wilcox (Oracle) 	 * In a workload with many unevictable pages such as mprotect,
645261b6840SMatthew Wilcox (Oracle) 	 * unevictable folio deactivation for accelerating reclaim is pointless.
646261b6840SMatthew Wilcox (Oracle) 	 */
647261b6840SMatthew Wilcox (Oracle) 	if (folio_test_unevictable(folio))
648261b6840SMatthew Wilcox (Oracle) 		return;
649261b6840SMatthew Wilcox (Oracle) 
650261b6840SMatthew Wilcox (Oracle) 	folio_get(folio);
651b01b2141SIngo Molnar 	local_lock(&lru_pvecs.lock);
652b01b2141SIngo Molnar 	pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate_file);
65331560180SMinchan Kim 
654261b6840SMatthew Wilcox (Oracle) 	if (pagevec_add_and_need_flush(pvec, &folio->page))
655c7c7b80cSAlex Shi 		pagevec_lru_move_fn(pvec, lru_deactivate_file_fn);
656b01b2141SIngo Molnar 	local_unlock(&lru_pvecs.lock);
65731560180SMinchan Kim }
65880bfed90SAndrew Morton 
6599c276cc6SMinchan Kim /*
6609c276cc6SMinchan Kim  * deactivate_page - deactivate a page
6619c276cc6SMinchan Kim  * @page: page to deactivate
6629c276cc6SMinchan Kim  *
6639c276cc6SMinchan Kim  * deactivate_page() moves @page to the inactive list if @page was on the active
6649c276cc6SMinchan Kim  * list and was not an unevictable page.  This is done to accelerate the reclaim
6659c276cc6SMinchan Kim  * of @page.
6669c276cc6SMinchan Kim  */
6679c276cc6SMinchan Kim void deactivate_page(struct page *page)
6689c276cc6SMinchan Kim {
6699c276cc6SMinchan Kim 	if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
670b01b2141SIngo Molnar 		struct pagevec *pvec;
6719c276cc6SMinchan Kim 
672b01b2141SIngo Molnar 		local_lock(&lru_pvecs.lock);
673b01b2141SIngo Molnar 		pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate);
6749c276cc6SMinchan Kim 		get_page(page);
675d479960eSMinchan Kim 		if (pagevec_add_and_need_flush(pvec, page))
676c7c7b80cSAlex Shi 			pagevec_lru_move_fn(pvec, lru_deactivate_fn);
677b01b2141SIngo Molnar 		local_unlock(&lru_pvecs.lock);
6789c276cc6SMinchan Kim 	}
6799c276cc6SMinchan Kim }
6809c276cc6SMinchan Kim 
68110853a03SMinchan Kim /**
682f7ad2a6cSShaohua Li  * mark_page_lazyfree - make an anon page lazyfree
68310853a03SMinchan Kim  * @page: page to deactivate
68410853a03SMinchan Kim  *
685f7ad2a6cSShaohua Li  * mark_page_lazyfree() moves @page to the inactive file list.
686f7ad2a6cSShaohua Li  * This is done to accelerate the reclaim of @page.
68710853a03SMinchan Kim  */
688f7ad2a6cSShaohua Li void mark_page_lazyfree(struct page *page)
68910853a03SMinchan Kim {
690f7ad2a6cSShaohua Li 	if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
69124c92eb7SShaohua Li 	    !PageSwapCache(page) && !PageUnevictable(page)) {
692b01b2141SIngo Molnar 		struct pagevec *pvec;
69310853a03SMinchan Kim 
694b01b2141SIngo Molnar 		local_lock(&lru_pvecs.lock);
695b01b2141SIngo Molnar 		pvec = this_cpu_ptr(&lru_pvecs.lru_lazyfree);
69609cbfeafSKirill A. Shutemov 		get_page(page);
697d479960eSMinchan Kim 		if (pagevec_add_and_need_flush(pvec, page))
698c7c7b80cSAlex Shi 			pagevec_lru_move_fn(pvec, lru_lazyfree_fn);
699b01b2141SIngo Molnar 		local_unlock(&lru_pvecs.lock);
70010853a03SMinchan Kim 	}
70110853a03SMinchan Kim }
70210853a03SMinchan Kim 
70380bfed90SAndrew Morton void lru_add_drain(void)
70480bfed90SAndrew Morton {
705b01b2141SIngo Molnar 	local_lock(&lru_pvecs.lock);
706b01b2141SIngo Molnar 	lru_add_drain_cpu(smp_processor_id());
707b01b2141SIngo Molnar 	local_unlock(&lru_pvecs.lock);
708adb11e78SSebastian Andrzej Siewior 	mlock_page_drain_local();
709b01b2141SIngo Molnar }
710b01b2141SIngo Molnar 
711243418e3SMinchan Kim /*
712243418e3SMinchan Kim  * It's called from per-cpu workqueue context in SMP case so
713243418e3SMinchan Kim  * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on
714243418e3SMinchan Kim  * the same cpu. It shouldn't be a problem in !SMP case since
715243418e3SMinchan Kim  * the core is only one and the locks will disable preemption.
716243418e3SMinchan Kim  */
717243418e3SMinchan Kim static void lru_add_and_bh_lrus_drain(void)
718243418e3SMinchan Kim {
719243418e3SMinchan Kim 	local_lock(&lru_pvecs.lock);
720243418e3SMinchan Kim 	lru_add_drain_cpu(smp_processor_id());
721243418e3SMinchan Kim 	local_unlock(&lru_pvecs.lock);
722243418e3SMinchan Kim 	invalidate_bh_lrus_cpu();
723adb11e78SSebastian Andrzej Siewior 	mlock_page_drain_local();
724243418e3SMinchan Kim }
725243418e3SMinchan Kim 
726b01b2141SIngo Molnar void lru_add_drain_cpu_zone(struct zone *zone)
727b01b2141SIngo Molnar {
728b01b2141SIngo Molnar 	local_lock(&lru_pvecs.lock);
729b01b2141SIngo Molnar 	lru_add_drain_cpu(smp_processor_id());
730b01b2141SIngo Molnar 	drain_local_pages(zone);
731b01b2141SIngo Molnar 	local_unlock(&lru_pvecs.lock);
732adb11e78SSebastian Andrzej Siewior 	mlock_page_drain_local();
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
7356ea183d6SMichal Hocko #ifdef CONFIG_SMP
7366ea183d6SMichal Hocko 
7376ea183d6SMichal Hocko static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
7386ea183d6SMichal Hocko 
739c4028958SDavid Howells static void lru_add_drain_per_cpu(struct work_struct *dummy)
740053837fcSNick Piggin {
741243418e3SMinchan Kim 	lru_add_and_bh_lrus_drain();
742053837fcSNick Piggin }
743053837fcSNick Piggin 
7449852a721SMichal Hocko /*
7459852a721SMichal Hocko  * Doesn't need any cpu hotplug locking because we do rely on per-cpu
7469852a721SMichal Hocko  * kworkers being shut down before our page_alloc_cpu_dead callback is
7479852a721SMichal Hocko  * executed on the offlined cpu.
7489852a721SMichal Hocko  * Calling this function with cpu hotplug locks held can actually lead
7499852a721SMichal Hocko  * to obscure indirect dependencies via WQ context.
7509852a721SMichal Hocko  */
7513db3264dSMiaohe Lin static inline void __lru_add_drain_all(bool force_all_cpus)
752053837fcSNick Piggin {
7536446a513SAhmed S. Darwish 	/*
7546446a513SAhmed S. Darwish 	 * lru_drain_gen - Global pages generation number
7556446a513SAhmed S. Darwish 	 *
7566446a513SAhmed S. Darwish 	 * (A) Definition: global lru_drain_gen = x implies that all generations
7576446a513SAhmed S. Darwish 	 *     0 < n <= x are already *scheduled* for draining.
7586446a513SAhmed S. Darwish 	 *
7596446a513SAhmed S. Darwish 	 * This is an optimization for the highly-contended use case where a
7606446a513SAhmed S. Darwish 	 * user space workload keeps constantly generating a flow of pages for
7616446a513SAhmed S. Darwish 	 * each CPU.
7626446a513SAhmed S. Darwish 	 */
7636446a513SAhmed S. Darwish 	static unsigned int lru_drain_gen;
7645fbc4616SChris Metcalf 	static struct cpumask has_work;
7656446a513SAhmed S. Darwish 	static DEFINE_MUTEX(lock);
7666446a513SAhmed S. Darwish 	unsigned cpu, this_gen;
7675fbc4616SChris Metcalf 
768ce612879SMichal Hocko 	/*
769ce612879SMichal Hocko 	 * Make sure nobody triggers this path before mm_percpu_wq is fully
770ce612879SMichal Hocko 	 * initialized.
771ce612879SMichal Hocko 	 */
772ce612879SMichal Hocko 	if (WARN_ON(!mm_percpu_wq))
773ce612879SMichal Hocko 		return;
774ce612879SMichal Hocko 
7756446a513SAhmed S. Darwish 	/*
7766446a513SAhmed S. Darwish 	 * Guarantee pagevec counter stores visible by this CPU are visible to
7776446a513SAhmed S. Darwish 	 * other CPUs before loading the current drain generation.
7786446a513SAhmed S. Darwish 	 */
7796446a513SAhmed S. Darwish 	smp_mb();
7806446a513SAhmed S. Darwish 
7816446a513SAhmed S. Darwish 	/*
7826446a513SAhmed S. Darwish 	 * (B) Locally cache global LRU draining generation number
7836446a513SAhmed S. Darwish 	 *
7846446a513SAhmed S. Darwish 	 * The read barrier ensures that the counter is loaded before the mutex
7856446a513SAhmed S. Darwish 	 * is taken. It pairs with smp_mb() inside the mutex critical section
7866446a513SAhmed S. Darwish 	 * at (D).
7876446a513SAhmed S. Darwish 	 */
7886446a513SAhmed S. Darwish 	this_gen = smp_load_acquire(&lru_drain_gen);
789eef1a429SKonstantin Khlebnikov 
7905fbc4616SChris Metcalf 	mutex_lock(&lock);
791eef1a429SKonstantin Khlebnikov 
792eef1a429SKonstantin Khlebnikov 	/*
7936446a513SAhmed S. Darwish 	 * (C) Exit the draining operation if a newer generation, from another
7946446a513SAhmed S. Darwish 	 * lru_add_drain_all(), was already scheduled for draining. Check (A).
795eef1a429SKonstantin Khlebnikov 	 */
796d479960eSMinchan Kim 	if (unlikely(this_gen != lru_drain_gen && !force_all_cpus))
797eef1a429SKonstantin Khlebnikov 		goto done;
798eef1a429SKonstantin Khlebnikov 
7996446a513SAhmed S. Darwish 	/*
8006446a513SAhmed S. Darwish 	 * (D) Increment global generation number
8016446a513SAhmed S. Darwish 	 *
8026446a513SAhmed S. Darwish 	 * Pairs with smp_load_acquire() at (B), outside of the critical
8036446a513SAhmed S. Darwish 	 * section. Use a full memory barrier to guarantee that the new global
8046446a513SAhmed S. Darwish 	 * drain generation number is stored before loading pagevec counters.
8056446a513SAhmed S. Darwish 	 *
8066446a513SAhmed S. Darwish 	 * This pairing must be done here, before the for_each_online_cpu loop
8076446a513SAhmed S. Darwish 	 * below which drains the page vectors.
8086446a513SAhmed S. Darwish 	 *
8096446a513SAhmed S. Darwish 	 * Let x, y, and z represent some system CPU numbers, where x < y < z.
810cb152a1aSShijie Luo 	 * Assume CPU #z is in the middle of the for_each_online_cpu loop
8116446a513SAhmed S. Darwish 	 * below and has already reached CPU #y's per-cpu data. CPU #x comes
8126446a513SAhmed S. Darwish 	 * along, adds some pages to its per-cpu vectors, then calls
8136446a513SAhmed S. Darwish 	 * lru_add_drain_all().
8146446a513SAhmed S. Darwish 	 *
8156446a513SAhmed S. Darwish 	 * If the paired barrier is done at any later step, e.g. after the
8166446a513SAhmed S. Darwish 	 * loop, CPU #x will just exit at (C) and miss flushing out all of its
8176446a513SAhmed S. Darwish 	 * added pages.
8186446a513SAhmed S. Darwish 	 */
8196446a513SAhmed S. Darwish 	WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1);
8206446a513SAhmed S. Darwish 	smp_mb();
821eef1a429SKonstantin Khlebnikov 
8225fbc4616SChris Metcalf 	cpumask_clear(&has_work);
8235fbc4616SChris Metcalf 	for_each_online_cpu(cpu) {
8245fbc4616SChris Metcalf 		struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
8255fbc4616SChris Metcalf 
826ff042f4aSMarcelo Tosatti 		if (pagevec_count(&per_cpu(lru_pvecs.lru_add, cpu)) ||
8277e0cc01eSQian Cai 		    data_race(pagevec_count(&per_cpu(lru_rotate.pvec, cpu))) ||
828b01b2141SIngo Molnar 		    pagevec_count(&per_cpu(lru_pvecs.lru_deactivate_file, cpu)) ||
829b01b2141SIngo Molnar 		    pagevec_count(&per_cpu(lru_pvecs.lru_deactivate, cpu)) ||
830b01b2141SIngo Molnar 		    pagevec_count(&per_cpu(lru_pvecs.lru_lazyfree, cpu)) ||
8318cc621d2SMinchan Kim 		    need_activate_page_drain(cpu) ||
8322fbb0c10SHugh Dickins 		    need_mlock_page_drain(cpu) ||
8338cc621d2SMinchan Kim 		    has_bh_in_lru(cpu, NULL)) {
8345fbc4616SChris Metcalf 			INIT_WORK(work, lru_add_drain_per_cpu);
835ce612879SMichal Hocko 			queue_work_on(cpu, mm_percpu_wq, work);
8366446a513SAhmed S. Darwish 			__cpumask_set_cpu(cpu, &has_work);
8375fbc4616SChris Metcalf 		}
8385fbc4616SChris Metcalf 	}
8395fbc4616SChris Metcalf 
8405fbc4616SChris Metcalf 	for_each_cpu(cpu, &has_work)
8415fbc4616SChris Metcalf 		flush_work(&per_cpu(lru_add_drain_work, cpu));
8425fbc4616SChris Metcalf 
843eef1a429SKonstantin Khlebnikov done:
8445fbc4616SChris Metcalf 	mutex_unlock(&lock);
845053837fcSNick Piggin }
846d479960eSMinchan Kim 
847d479960eSMinchan Kim void lru_add_drain_all(void)
848d479960eSMinchan Kim {
849d479960eSMinchan Kim 	__lru_add_drain_all(false);
850d479960eSMinchan Kim }
8516ea183d6SMichal Hocko #else
8526ea183d6SMichal Hocko void lru_add_drain_all(void)
8536ea183d6SMichal Hocko {
8546ea183d6SMichal Hocko 	lru_add_drain();
8556ea183d6SMichal Hocko }
8566446a513SAhmed S. Darwish #endif /* CONFIG_SMP */
857053837fcSNick Piggin 
858d479960eSMinchan Kim atomic_t lru_disable_count = ATOMIC_INIT(0);
859d479960eSMinchan Kim 
860d479960eSMinchan Kim /*
861d479960eSMinchan Kim  * lru_cache_disable() needs to be called before we start compiling
862d479960eSMinchan Kim  * a list of pages to be migrated using isolate_lru_page().
863d479960eSMinchan Kim  * It drains pages on LRU cache and then disable on all cpus until
864d479960eSMinchan Kim  * lru_cache_enable is called.
865d479960eSMinchan Kim  *
866d479960eSMinchan Kim  * Must be paired with a call to lru_cache_enable().
867d479960eSMinchan Kim  */
868d479960eSMinchan Kim void lru_cache_disable(void)
869d479960eSMinchan Kim {
870d479960eSMinchan Kim 	atomic_inc(&lru_disable_count);
871d479960eSMinchan Kim 	/*
872ff042f4aSMarcelo Tosatti 	 * Readers of lru_disable_count are protected by either disabling
873ff042f4aSMarcelo Tosatti 	 * preemption or rcu_read_lock:
874ff042f4aSMarcelo Tosatti 	 *
875ff042f4aSMarcelo Tosatti 	 * preempt_disable, local_irq_disable  [bh_lru_lock()]
876ff042f4aSMarcelo Tosatti 	 * rcu_read_lock		       [rt_spin_lock CONFIG_PREEMPT_RT]
877ff042f4aSMarcelo Tosatti 	 * preempt_disable		       [local_lock !CONFIG_PREEMPT_RT]
878ff042f4aSMarcelo Tosatti 	 *
879ff042f4aSMarcelo Tosatti 	 * Since v5.1 kernel, synchronize_rcu() is guaranteed to wait on
880ff042f4aSMarcelo Tosatti 	 * preempt_disable() regions of code. So any CPU which sees
881ff042f4aSMarcelo Tosatti 	 * lru_disable_count = 0 will have exited the critical
882ff042f4aSMarcelo Tosatti 	 * section when synchronize_rcu() returns.
883d479960eSMinchan Kim 	 */
884*31733463SMarcelo Tosatti 	synchronize_rcu_expedited();
885ff042f4aSMarcelo Tosatti #ifdef CONFIG_SMP
886d479960eSMinchan Kim 	__lru_add_drain_all(true);
887d479960eSMinchan Kim #else
888243418e3SMinchan Kim 	lru_add_and_bh_lrus_drain();
889d479960eSMinchan Kim #endif
890d479960eSMinchan Kim }
891d479960eSMinchan Kim 
892aabfb572SMichal Hocko /**
893ea1754a0SKirill A. Shutemov  * release_pages - batched put_page()
894aabfb572SMichal Hocko  * @pages: array of pages to release
895aabfb572SMichal Hocko  * @nr: number of pages
8961da177e4SLinus Torvalds  *
897aabfb572SMichal Hocko  * Decrement the reference count on all the pages in @pages.  If it
898aabfb572SMichal Hocko  * fell to zero, remove the page from the LRU and free it.
8991da177e4SLinus Torvalds  */
900c6f92f9fSMel Gorman void release_pages(struct page **pages, int nr)
9011da177e4SLinus Torvalds {
9021da177e4SLinus Torvalds 	int i;
903cc59850eSKonstantin Khlebnikov 	LIST_HEAD(pages_to_free);
9046168d0daSAlex Shi 	struct lruvec *lruvec = NULL;
9050de340cbSMatthew Wilcox (Oracle) 	unsigned long flags = 0;
9063f649ab7SKees Cook 	unsigned int lock_batch;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	for (i = 0; i < nr; i++) {
9091da177e4SLinus Torvalds 		struct page *page = pages[i];
9100de340cbSMatthew Wilcox (Oracle) 		struct folio *folio = page_folio(page);
9111da177e4SLinus Torvalds 
912aabfb572SMichal Hocko 		/*
913aabfb572SMichal Hocko 		 * Make sure the IRQ-safe lock-holding time does not get
914aabfb572SMichal Hocko 		 * excessive with a continuous string of pages from the
9156168d0daSAlex Shi 		 * same lruvec. The lock is held only if lruvec != NULL.
916aabfb572SMichal Hocko 		 */
9176168d0daSAlex Shi 		if (lruvec && ++lock_batch == SWAP_CLUSTER_MAX) {
9186168d0daSAlex Shi 			unlock_page_lruvec_irqrestore(lruvec, flags);
9196168d0daSAlex Shi 			lruvec = NULL;
920aabfb572SMichal Hocko 		}
921aabfb572SMichal Hocko 
9220de340cbSMatthew Wilcox (Oracle) 		page = &folio->page;
9236fcb52a5SAaron Lu 		if (is_huge_zero_page(page))
924aa88b68cSKirill A. Shutemov 			continue;
925aa88b68cSKirill A. Shutemov 
926c5d6c45eSIra Weiny 		if (is_zone_device_page(page)) {
9276168d0daSAlex Shi 			if (lruvec) {
9286168d0daSAlex Shi 				unlock_page_lruvec_irqrestore(lruvec, flags);
9296168d0daSAlex Shi 				lruvec = NULL;
930df6ad698SJérôme Glisse 			}
93189574945SChristoph Hellwig 			if (put_devmap_managed_page(page))
932df6ad698SJérôme Glisse 				continue;
93343fbdeb3SRalph Campbell 			if (put_page_testzero(page))
93427674ef6SChristoph Hellwig 				free_zone_device_page(page);
93543fbdeb3SRalph Campbell 			continue;
93607d80269SJohn Hubbard 		}
937df6ad698SJérôme Glisse 
938b5810039SNick Piggin 		if (!put_page_testzero(page))
9391da177e4SLinus Torvalds 			continue;
9401da177e4SLinus Torvalds 
941ddc58f27SKirill A. Shutemov 		if (PageCompound(page)) {
9426168d0daSAlex Shi 			if (lruvec) {
9436168d0daSAlex Shi 				unlock_page_lruvec_irqrestore(lruvec, flags);
9446168d0daSAlex Shi 				lruvec = NULL;
945ddc58f27SKirill A. Shutemov 			}
946ddc58f27SKirill A. Shutemov 			__put_compound_page(page);
947ddc58f27SKirill A. Shutemov 			continue;
948ddc58f27SKirill A. Shutemov 		}
949ddc58f27SKirill A. Shutemov 
95046453a6eSNick Piggin 		if (PageLRU(page)) {
9512a5e4e34SAlexander Duyck 			struct lruvec *prev_lruvec = lruvec;
952894bc310SLee Schermerhorn 
9530de340cbSMatthew Wilcox (Oracle) 			lruvec = folio_lruvec_relock_irqsave(folio, lruvec,
9542a5e4e34SAlexander Duyck 									&flags);
9552a5e4e34SAlexander Duyck 			if (prev_lruvec != lruvec)
956aabfb572SMichal Hocko 				lock_batch = 0;
957fa9add64SHugh Dickins 
95846ae6b2cSYu Zhao 			del_page_from_lru_list(page, lruvec);
95987560179SYu Zhao 			__clear_page_lru_flags(page);
96046453a6eSNick Piggin 		}
96146453a6eSNick Piggin 
962b109b870SHugh Dickins 		/*
963b109b870SHugh Dickins 		 * In rare cases, when truncation or holepunching raced with
964b109b870SHugh Dickins 		 * munlock after VM_LOCKED was cleared, Mlocked may still be
965b109b870SHugh Dickins 		 * found set here.  This does not indicate a problem, unless
966b109b870SHugh Dickins 		 * "unevictable_pgs_cleared" appears worryingly large.
967b109b870SHugh Dickins 		 */
968b109b870SHugh Dickins 		if (unlikely(PageMlocked(page))) {
969b109b870SHugh Dickins 			__ClearPageMlocked(page);
970b109b870SHugh Dickins 			dec_zone_page_state(page, NR_MLOCK);
971b109b870SHugh Dickins 			count_vm_event(UNEVICTABLE_PGCLEARED);
972b109b870SHugh Dickins 		}
973b109b870SHugh Dickins 
974cc59850eSKonstantin Khlebnikov 		list_add(&page->lru, &pages_to_free);
9751da177e4SLinus Torvalds 	}
9766168d0daSAlex Shi 	if (lruvec)
9776168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
9781da177e4SLinus Torvalds 
979747db954SJohannes Weiner 	mem_cgroup_uncharge_list(&pages_to_free);
9802d4894b5SMel Gorman 	free_unref_page_list(&pages_to_free);
9811da177e4SLinus Torvalds }
9820be8557bSMiklos Szeredi EXPORT_SYMBOL(release_pages);
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds /*
9851da177e4SLinus Torvalds  * The pages which we're about to release may be in the deferred lru-addition
9861da177e4SLinus Torvalds  * queues.  That would prevent them from really being freed right now.  That's
9871da177e4SLinus Torvalds  * OK from a correctness point of view but is inefficient - those pages may be
9881da177e4SLinus Torvalds  * cache-warm and we want to give them back to the page allocator ASAP.
9891da177e4SLinus Torvalds  *
9901da177e4SLinus Torvalds  * So __pagevec_release() will drain those queues here.  __pagevec_lru_add()
9911da177e4SLinus Torvalds  * and __pagevec_lru_add_active() call release_pages() directly to avoid
9921da177e4SLinus Torvalds  * mutual recursion.
9931da177e4SLinus Torvalds  */
9941da177e4SLinus Torvalds void __pagevec_release(struct pagevec *pvec)
9951da177e4SLinus Torvalds {
9967f0b5fb9SMel Gorman 	if (!pvec->percpu_pvec_drained) {
9971da177e4SLinus Torvalds 		lru_add_drain();
9987f0b5fb9SMel Gorman 		pvec->percpu_pvec_drained = true;
999d9ed0d08SMel Gorman 	}
1000c6f92f9fSMel Gorman 	release_pages(pvec->pages, pagevec_count(pvec));
10011da177e4SLinus Torvalds 	pagevec_reinit(pvec);
10021da177e4SLinus Torvalds }
10037f285701SSteve French EXPORT_SYMBOL(__pagevec_release);
10047f285701SSteve French 
1005934387c9SMatthew Wilcox (Oracle) static void __pagevec_lru_add_fn(struct folio *folio, struct lruvec *lruvec)
10063dd7ae8eSShaohua Li {
1007934387c9SMatthew Wilcox (Oracle) 	int was_unevictable = folio_test_clear_unevictable(folio);
1008934387c9SMatthew Wilcox (Oracle) 	long nr_pages = folio_nr_pages(folio);
10093dd7ae8eSShaohua Li 
1010934387c9SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
10113dd7ae8eSShaohua Li 
1012934387c9SMatthew Wilcox (Oracle) 	folio_set_lru(folio);
10132262ace6SHugh Dickins 	/*
10142262ace6SHugh Dickins 	 * Is an smp_mb__after_atomic() still required here, before
10152262ace6SHugh Dickins 	 * folio_evictable() tests PageMlocked, to rule out the possibility
10162262ace6SHugh Dickins 	 * of stranding an evictable folio on an unevictable LRU?  I think
10172fbb0c10SHugh Dickins 	 * not, because __munlock_page() only clears PageMlocked while the LRU
10182262ace6SHugh Dickins 	 * lock is held.
10192262ace6SHugh Dickins 	 *
10202262ace6SHugh Dickins 	 * (That is not true of __page_cache_release(), and not necessarily
10212262ace6SHugh Dickins 	 * true of release_pages(): but those only clear PageMlocked after
10222262ace6SHugh Dickins 	 * put_page_testzero() has excluded any other users of the page.)
10232262ace6SHugh Dickins 	 */
1024934387c9SMatthew Wilcox (Oracle) 	if (folio_evictable(folio)) {
10259c4e6b1aSShakeel Butt 		if (was_unevictable)
10265d91f31fSShakeel Butt 			__count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages);
10279c4e6b1aSShakeel Butt 	} else {
1028934387c9SMatthew Wilcox (Oracle) 		folio_clear_active(folio);
1029934387c9SMatthew Wilcox (Oracle) 		folio_set_unevictable(folio);
10302fbb0c10SHugh Dickins 		/*
10312fbb0c10SHugh Dickins 		 * folio->mlock_count = !!folio_test_mlocked(folio)?
10322fbb0c10SHugh Dickins 		 * But that leaves __mlock_page() in doubt whether another
10332fbb0c10SHugh Dickins 		 * actor has already counted the mlock or not.  Err on the
10342fbb0c10SHugh Dickins 		 * safe side, underestimate, let page reclaim fix it, rather
10352fbb0c10SHugh Dickins 		 * than leaving a page on the unevictable LRU indefinitely.
10362fbb0c10SHugh Dickins 		 */
10372fbb0c10SHugh Dickins 		folio->mlock_count = 0;
10389c4e6b1aSShakeel Butt 		if (!was_unevictable)
10395d91f31fSShakeel Butt 			__count_vm_events(UNEVICTABLE_PGCULLED, nr_pages);
10409c4e6b1aSShakeel Butt 	}
10419c4e6b1aSShakeel Butt 
1042934387c9SMatthew Wilcox (Oracle) 	lruvec_add_folio(lruvec, folio);
1043934387c9SMatthew Wilcox (Oracle) 	trace_mm_lru_insertion(folio);
10443dd7ae8eSShaohua Li }
10453dd7ae8eSShaohua Li 
10461da177e4SLinus Torvalds /*
10471da177e4SLinus Torvalds  * Add the passed pages to the LRU, then drop the caller's refcount
10481da177e4SLinus Torvalds  * on them.  Reinitialises the caller's pagevec.
10491da177e4SLinus Torvalds  */
1050a0b8cab3SMel Gorman void __pagevec_lru_add(struct pagevec *pvec)
10511da177e4SLinus Torvalds {
1052fc574c23SAlex Shi 	int i;
10536168d0daSAlex Shi 	struct lruvec *lruvec = NULL;
1054fc574c23SAlex Shi 	unsigned long flags = 0;
1055fc574c23SAlex Shi 
1056fc574c23SAlex Shi 	for (i = 0; i < pagevec_count(pvec); i++) {
1057934387c9SMatthew Wilcox (Oracle) 		struct folio *folio = page_folio(pvec->pages[i]);
1058fc574c23SAlex Shi 
10590de340cbSMatthew Wilcox (Oracle) 		lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags);
1060934387c9SMatthew Wilcox (Oracle) 		__pagevec_lru_add_fn(folio, lruvec);
1061fc574c23SAlex Shi 	}
10626168d0daSAlex Shi 	if (lruvec)
10636168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
1064fc574c23SAlex Shi 	release_pages(pvec->pages, pvec->nr);
1065fc574c23SAlex Shi 	pagevec_reinit(pvec);
10661da177e4SLinus Torvalds }
1067f04e9ebbSKOSAKI Motohiro 
10681da177e4SLinus Torvalds /**
10691613fac9SMatthew Wilcox (Oracle)  * folio_batch_remove_exceptionals() - Prune non-folios from a batch.
10701613fac9SMatthew Wilcox (Oracle)  * @fbatch: The batch to prune
10710cd6144aSJohannes Weiner  *
10721613fac9SMatthew Wilcox (Oracle)  * find_get_entries() fills a batch with both folios and shadow/swap/DAX
10731613fac9SMatthew Wilcox (Oracle)  * entries.  This function prunes all the non-folio entries from @fbatch
10741613fac9SMatthew Wilcox (Oracle)  * without leaving holes, so that it can be passed on to folio-only batch
10751613fac9SMatthew Wilcox (Oracle)  * operations.
10760cd6144aSJohannes Weiner  */
10771613fac9SMatthew Wilcox (Oracle) void folio_batch_remove_exceptionals(struct folio_batch *fbatch)
10780cd6144aSJohannes Weiner {
10791613fac9SMatthew Wilcox (Oracle) 	unsigned int i, j;
10800cd6144aSJohannes Weiner 
10811613fac9SMatthew Wilcox (Oracle) 	for (i = 0, j = 0; i < folio_batch_count(fbatch); i++) {
10821613fac9SMatthew Wilcox (Oracle) 		struct folio *folio = fbatch->folios[i];
10831613fac9SMatthew Wilcox (Oracle) 		if (!xa_is_value(folio))
10841613fac9SMatthew Wilcox (Oracle) 			fbatch->folios[j++] = folio;
10850cd6144aSJohannes Weiner 	}
10861613fac9SMatthew Wilcox (Oracle) 	fbatch->nr = j;
10870cd6144aSJohannes Weiner }
10880cd6144aSJohannes Weiner 
10890cd6144aSJohannes Weiner /**
1090b947cee4SJan Kara  * pagevec_lookup_range - gang pagecache lookup
10911da177e4SLinus Torvalds  * @pvec:	Where the resulting pages are placed
10921da177e4SLinus Torvalds  * @mapping:	The address_space to search
10931da177e4SLinus Torvalds  * @start:	The starting page index
1094b947cee4SJan Kara  * @end:	The final page index
10951da177e4SLinus Torvalds  *
1096e02a9f04SRandy Dunlap  * pagevec_lookup_range() will search for & return a group of up to PAGEVEC_SIZE
1097b947cee4SJan Kara  * pages in the mapping starting from index @start and upto index @end
1098b947cee4SJan Kara  * (inclusive).  The pages are placed in @pvec.  pagevec_lookup() takes a
10991da177e4SLinus Torvalds  * reference against the pages in @pvec.
11001da177e4SLinus Torvalds  *
11011da177e4SLinus Torvalds  * The search returns a group of mapping-contiguous pages with ascending
1102d72dc8a2SJan Kara  * indexes.  There may be holes in the indices due to not-present pages. We
1103d72dc8a2SJan Kara  * also update @start to index the next page for the traversal.
11041da177e4SLinus Torvalds  *
1105b947cee4SJan Kara  * pagevec_lookup_range() returns the number of pages which were found. If this
1106e02a9f04SRandy Dunlap  * number is smaller than PAGEVEC_SIZE, the end of specified range has been
1107b947cee4SJan Kara  * reached.
11081da177e4SLinus Torvalds  */
1109b947cee4SJan Kara unsigned pagevec_lookup_range(struct pagevec *pvec,
1110397162ffSJan Kara 		struct address_space *mapping, pgoff_t *start, pgoff_t end)
11111da177e4SLinus Torvalds {
1112397162ffSJan Kara 	pvec->nr = find_get_pages_range(mapping, start, end, PAGEVEC_SIZE,
1113b947cee4SJan Kara 					pvec->pages);
11141da177e4SLinus Torvalds 	return pagevec_count(pvec);
11151da177e4SLinus Torvalds }
1116b947cee4SJan Kara EXPORT_SYMBOL(pagevec_lookup_range);
111778539fdfSChristoph Hellwig 
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