xref: /linux/mm/swap.c (revision 575ced1c8b0d3b578b933a68ce67ddaff3df9506)
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 /*
77b221385bSAdrian Bunk  * This path almost never happens for VM activity - pages are normally
78b221385bSAdrian Bunk  * freed via pagevecs.  But it gets used by networking.
79b221385bSAdrian Bunk  */
80920c7a5dSHarvey Harrison static void __page_cache_release(struct page *page)
81b221385bSAdrian Bunk {
82b221385bSAdrian Bunk 	if (PageLRU(page)) {
83fa9add64SHugh Dickins 		struct lruvec *lruvec;
84fa9add64SHugh Dickins 		unsigned long flags;
85b221385bSAdrian Bunk 
866168d0daSAlex Shi 		lruvec = lock_page_lruvec_irqsave(page, &flags);
8746ae6b2cSYu Zhao 		del_page_from_lru_list(page, lruvec);
8887560179SYu Zhao 		__clear_page_lru_flags(page);
896168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
90b221385bSAdrian Bunk 	}
9162906027SNicholas Piggin 	__ClearPageWaiters(page);
9291807063SAndrea Arcangeli }
9391807063SAndrea Arcangeli 
9491807063SAndrea Arcangeli static void __put_single_page(struct page *page)
9591807063SAndrea Arcangeli {
9691807063SAndrea Arcangeli 	__page_cache_release(page);
977ae88534SYang Shi 	mem_cgroup_uncharge(page);
9844042b44SMel Gorman 	free_unref_page(page, 0);
99b221385bSAdrian Bunk }
100b221385bSAdrian Bunk 
10191807063SAndrea Arcangeli static void __put_compound_page(struct page *page)
10291807063SAndrea Arcangeli {
103822fc613SNaoya Horiguchi 	/*
104822fc613SNaoya Horiguchi 	 * __page_cache_release() is supposed to be called for thp, not for
105822fc613SNaoya Horiguchi 	 * hugetlb. This is because hugetlb page does never have PageLRU set
106822fc613SNaoya Horiguchi 	 * (it's never listed to any LRU lists) and no memcg routines should
107822fc613SNaoya Horiguchi 	 * be called for hugetlb (it has a separate hugetlb_cgroup.)
108822fc613SNaoya Horiguchi 	 */
109822fc613SNaoya Horiguchi 	if (!PageHuge(page))
11091807063SAndrea Arcangeli 		__page_cache_release(page);
111ff45fc3cSMatthew Wilcox (Oracle) 	destroy_compound_page(page);
11291807063SAndrea Arcangeli }
11391807063SAndrea Arcangeli 
114ddc58f27SKirill A. Shutemov void __put_page(struct page *page)
115c747ce79SJianyu Zhan {
11671389703SDan Williams 	if (is_zone_device_page(page)) {
11771389703SDan Williams 		put_dev_pagemap(page->pgmap);
11871389703SDan Williams 
11971389703SDan Williams 		/*
12071389703SDan Williams 		 * The page belongs to the device that created pgmap. Do
12171389703SDan Williams 		 * not return it to page allocator.
12271389703SDan Williams 		 */
12371389703SDan Williams 		return;
12471389703SDan Williams 	}
12571389703SDan Williams 
126ddc58f27SKirill A. Shutemov 	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  *
1371d7ea732SAlexander Zarochentsev  * Release a list of pages which are strung together on page.lru.  Currently
1381d7ea732SAlexander Zarochentsev  * used by read_cache_pages() and related error recovery code.
1391d7ea732SAlexander Zarochentsev  */
1401d7ea732SAlexander Zarochentsev void put_pages_list(struct list_head *pages)
1411d7ea732SAlexander Zarochentsev {
1421d7ea732SAlexander Zarochentsev 	while (!list_empty(pages)) {
1431d7ea732SAlexander Zarochentsev 		struct page *victim;
1441d7ea732SAlexander Zarochentsev 
145f86196eaSNikolay Borisov 		victim = lru_to_page(pages);
1461d7ea732SAlexander Zarochentsev 		list_del(&victim->lru);
14709cbfeafSKirill A. Shutemov 		put_page(victim);
1481d7ea732SAlexander Zarochentsev 	}
1491d7ea732SAlexander Zarochentsev }
1501d7ea732SAlexander Zarochentsev EXPORT_SYMBOL(put_pages_list);
1511d7ea732SAlexander Zarochentsev 
15218022c5dSMel Gorman /*
15318022c5dSMel Gorman  * get_kernel_pages() - pin kernel pages in memory
15418022c5dSMel Gorman  * @kiov:	An array of struct kvec structures
15518022c5dSMel Gorman  * @nr_segs:	number of segments to pin
15618022c5dSMel Gorman  * @write:	pinning for read/write, currently ignored
15718022c5dSMel Gorman  * @pages:	array that receives pointers to the pages pinned.
15818022c5dSMel Gorman  *		Should be at least nr_segs long.
15918022c5dSMel Gorman  *
16018022c5dSMel Gorman  * Returns number of pages pinned. This may be fewer than the number
16118022c5dSMel Gorman  * requested. If nr_pages is 0 or negative, returns 0. If no pages
16218022c5dSMel Gorman  * were pinned, returns -errno. Each page returned must be released
16318022c5dSMel Gorman  * with a put_page() call when it is finished with.
16418022c5dSMel Gorman  */
16518022c5dSMel Gorman int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
16618022c5dSMel Gorman 		struct page **pages)
16718022c5dSMel Gorman {
16818022c5dSMel Gorman 	int seg;
16918022c5dSMel Gorman 
17018022c5dSMel Gorman 	for (seg = 0; seg < nr_segs; seg++) {
17118022c5dSMel Gorman 		if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
17218022c5dSMel Gorman 			return seg;
17318022c5dSMel Gorman 
1745a178119SMel Gorman 		pages[seg] = kmap_to_page(kiov[seg].iov_base);
17509cbfeafSKirill A. Shutemov 		get_page(pages[seg]);
17618022c5dSMel Gorman 	}
17718022c5dSMel Gorman 
17818022c5dSMel Gorman 	return seg;
17918022c5dSMel Gorman }
18018022c5dSMel Gorman EXPORT_SYMBOL_GPL(get_kernel_pages);
18118022c5dSMel Gorman 
1823dd7ae8eSShaohua Li static void pagevec_lru_move_fn(struct pagevec *pvec,
183c7c7b80cSAlex Shi 	void (*move_fn)(struct page *page, struct lruvec *lruvec))
184902aaed0SHisashi Hifumi {
185902aaed0SHisashi Hifumi 	int i;
1866168d0daSAlex Shi 	struct lruvec *lruvec = NULL;
1873dd7ae8eSShaohua Li 	unsigned long flags = 0;
188902aaed0SHisashi Hifumi 
189902aaed0SHisashi Hifumi 	for (i = 0; i < pagevec_count(pvec); i++) {
190902aaed0SHisashi Hifumi 		struct page *page = pvec->pages[i];
1913dd7ae8eSShaohua Li 
192fc574c23SAlex Shi 		/* block memcg migration during page moving between lru */
193fc574c23SAlex Shi 		if (!TestClearPageLRU(page))
194fc574c23SAlex Shi 			continue;
195fc574c23SAlex Shi 
1962a5e4e34SAlexander Duyck 		lruvec = relock_page_lruvec_irqsave(page, lruvec, &flags);
197c7c7b80cSAlex Shi 		(*move_fn)(page, lruvec);
198fc574c23SAlex Shi 
199fc574c23SAlex Shi 		SetPageLRU(page);
2003dd7ae8eSShaohua Li 	}
2016168d0daSAlex Shi 	if (lruvec)
2026168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
203c6f92f9fSMel Gorman 	release_pages(pvec->pages, pvec->nr);
2043dd7ae8eSShaohua Li 	pagevec_reinit(pvec);
2053dd7ae8eSShaohua Li }
2063dd7ae8eSShaohua Li 
207c7c7b80cSAlex Shi static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec)
2083dd7ae8eSShaohua Li {
209*575ced1cSMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
210*575ced1cSMatthew Wilcox (Oracle) 
211*575ced1cSMatthew Wilcox (Oracle) 	if (!folio_test_unevictable(folio)) {
212*575ced1cSMatthew Wilcox (Oracle) 		lruvec_del_folio(lruvec, folio);
213*575ced1cSMatthew Wilcox (Oracle) 		folio_clear_active(folio);
214*575ced1cSMatthew Wilcox (Oracle) 		lruvec_add_folio_tail(lruvec, folio);
215*575ced1cSMatthew Wilcox (Oracle) 		__count_vm_events(PGROTATED, folio_nr_pages(folio));
216902aaed0SHisashi Hifumi 	}
217902aaed0SHisashi Hifumi }
2183dd7ae8eSShaohua Li 
219d479960eSMinchan Kim /* return true if pagevec needs to drain */
220d479960eSMinchan Kim static bool pagevec_add_and_need_flush(struct pagevec *pvec, struct page *page)
221d479960eSMinchan Kim {
222d479960eSMinchan Kim 	bool ret = false;
223d479960eSMinchan Kim 
224d479960eSMinchan Kim 	if (!pagevec_add(pvec, page) || PageCompound(page) ||
225d479960eSMinchan Kim 			lru_cache_disabled())
226d479960eSMinchan Kim 		ret = true;
227d479960eSMinchan Kim 
228d479960eSMinchan Kim 	return ret;
229d479960eSMinchan Kim }
230d479960eSMinchan Kim 
2313dd7ae8eSShaohua Li /*
232*575ced1cSMatthew Wilcox (Oracle)  * Writeback is about to end against a folio which has been marked for
233*575ced1cSMatthew Wilcox (Oracle)  * immediate reclaim.  If it still appears to be reclaimable, move it
234*575ced1cSMatthew Wilcox (Oracle)  * to the tail of the inactive list.
235c7c7b80cSAlex Shi  *
236*575ced1cSMatthew Wilcox (Oracle)  * folio_rotate_reclaimable() must disable IRQs, to prevent nasty races.
2371da177e4SLinus Torvalds  */
238*575ced1cSMatthew Wilcox (Oracle) void folio_rotate_reclaimable(struct folio *folio)
2391da177e4SLinus Torvalds {
240*575ced1cSMatthew Wilcox (Oracle) 	if (!folio_test_locked(folio) && !folio_test_dirty(folio) &&
241*575ced1cSMatthew Wilcox (Oracle) 	    !folio_test_unevictable(folio) && folio_test_lru(folio)) {
242902aaed0SHisashi Hifumi 		struct pagevec *pvec;
2431da177e4SLinus Torvalds 		unsigned long flags;
2441da177e4SLinus Torvalds 
245*575ced1cSMatthew Wilcox (Oracle) 		folio_get(folio);
246b01b2141SIngo Molnar 		local_lock_irqsave(&lru_rotate.lock, flags);
247b01b2141SIngo Molnar 		pvec = this_cpu_ptr(&lru_rotate.pvec);
248*575ced1cSMatthew Wilcox (Oracle) 		if (pagevec_add_and_need_flush(pvec, &folio->page))
249c7c7b80cSAlex Shi 			pagevec_lru_move_fn(pvec, pagevec_move_tail_fn);
250b01b2141SIngo Molnar 		local_unlock_irqrestore(&lru_rotate.lock, flags);
251ac6aadb2SMiklos Szeredi 	}
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
25496f8bf4fSJohannes Weiner void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages)
2553e2f41f1SKOSAKI Motohiro {
2567cf111bcSJohannes Weiner 	do {
2577cf111bcSJohannes Weiner 		unsigned long lrusize;
2587cf111bcSJohannes Weiner 
2596168d0daSAlex Shi 		/*
2606168d0daSAlex Shi 		 * Hold lruvec->lru_lock is safe here, since
2616168d0daSAlex Shi 		 * 1) The pinned lruvec in reclaim, or
2626168d0daSAlex Shi 		 * 2) From a pre-LRU page during refault (which also holds the
2636168d0daSAlex Shi 		 *    rcu lock, so would be safe even if the page was on the LRU
2646168d0daSAlex Shi 		 *    and could move simultaneously to a new lruvec).
2656168d0daSAlex Shi 		 */
2666168d0daSAlex Shi 		spin_lock_irq(&lruvec->lru_lock);
2677cf111bcSJohannes Weiner 		/* Record cost event */
26896f8bf4fSJohannes Weiner 		if (file)
26996f8bf4fSJohannes Weiner 			lruvec->file_cost += nr_pages;
2701431d4d1SJohannes Weiner 		else
27196f8bf4fSJohannes Weiner 			lruvec->anon_cost += nr_pages;
2727cf111bcSJohannes Weiner 
2737cf111bcSJohannes Weiner 		/*
2747cf111bcSJohannes Weiner 		 * Decay previous events
2757cf111bcSJohannes Weiner 		 *
2767cf111bcSJohannes Weiner 		 * Because workloads change over time (and to avoid
2777cf111bcSJohannes Weiner 		 * overflow) we keep these statistics as a floating
2787cf111bcSJohannes Weiner 		 * average, which ends up weighing recent refaults
2797cf111bcSJohannes Weiner 		 * more than old ones.
2807cf111bcSJohannes Weiner 		 */
2817cf111bcSJohannes Weiner 		lrusize = lruvec_page_state(lruvec, NR_INACTIVE_ANON) +
2827cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_ACTIVE_ANON) +
2837cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_INACTIVE_FILE) +
2847cf111bcSJohannes Weiner 			  lruvec_page_state(lruvec, NR_ACTIVE_FILE);
2857cf111bcSJohannes Weiner 
2867cf111bcSJohannes Weiner 		if (lruvec->file_cost + lruvec->anon_cost > lrusize / 4) {
2877cf111bcSJohannes Weiner 			lruvec->file_cost /= 2;
2887cf111bcSJohannes Weiner 			lruvec->anon_cost /= 2;
2897cf111bcSJohannes Weiner 		}
2906168d0daSAlex Shi 		spin_unlock_irq(&lruvec->lru_lock);
2917cf111bcSJohannes Weiner 	} while ((lruvec = parent_lruvec(lruvec)));
2923e2f41f1SKOSAKI Motohiro }
2933e2f41f1SKOSAKI Motohiro 
29496f8bf4fSJohannes Weiner void lru_note_cost_page(struct page *page)
29596f8bf4fSJohannes Weiner {
296a984226fSMuchun Song 	lru_note_cost(mem_cgroup_page_lruvec(page),
2976c357848SMatthew Wilcox (Oracle) 		      page_is_file_lru(page), thp_nr_pages(page));
29896f8bf4fSJohannes Weiner }
29996f8bf4fSJohannes Weiner 
300c7c7b80cSAlex Shi static void __activate_page(struct page *page, struct lruvec *lruvec)
301744ed144SShaohua Li {
302fc574c23SAlex Shi 	if (!PageActive(page) && !PageUnevictable(page)) {
3036c357848SMatthew Wilcox (Oracle) 		int nr_pages = thp_nr_pages(page);
304744ed144SShaohua Li 
30546ae6b2cSYu Zhao 		del_page_from_lru_list(page, lruvec);
306744ed144SShaohua Li 		SetPageActive(page);
3073a9c9788SYu Zhao 		add_page_to_lru_list(page, lruvec);
30824b7e581SMel Gorman 		trace_mm_lru_activate(page);
3097a608572SLinus Torvalds 
31021e330fcSShakeel Butt 		__count_vm_events(PGACTIVATE, nr_pages);
31121e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGACTIVATE,
31221e330fcSShakeel Butt 				     nr_pages);
313744ed144SShaohua Li 	}
314eb709b0dSShaohua Li }
315eb709b0dSShaohua Li 
316eb709b0dSShaohua Li #ifdef CONFIG_SMP
317eb709b0dSShaohua Li static void activate_page_drain(int cpu)
318eb709b0dSShaohua Li {
319b01b2141SIngo Molnar 	struct pagevec *pvec = &per_cpu(lru_pvecs.activate_page, cpu);
320eb709b0dSShaohua Li 
321eb709b0dSShaohua Li 	if (pagevec_count(pvec))
322c7c7b80cSAlex Shi 		pagevec_lru_move_fn(pvec, __activate_page);
323eb709b0dSShaohua Li }
324eb709b0dSShaohua Li 
3255fbc4616SChris Metcalf static bool need_activate_page_drain(int cpu)
3265fbc4616SChris Metcalf {
327b01b2141SIngo Molnar 	return pagevec_count(&per_cpu(lru_pvecs.activate_page, cpu)) != 0;
3285fbc4616SChris Metcalf }
3295fbc4616SChris Metcalf 
330cc2828b2SYu Zhao static void activate_page(struct page *page)
331eb709b0dSShaohua Li {
332800d8c63SKirill A. Shutemov 	page = compound_head(page);
333eb709b0dSShaohua Li 	if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
334b01b2141SIngo Molnar 		struct pagevec *pvec;
335eb709b0dSShaohua Li 
336b01b2141SIngo Molnar 		local_lock(&lru_pvecs.lock);
337b01b2141SIngo Molnar 		pvec = this_cpu_ptr(&lru_pvecs.activate_page);
33809cbfeafSKirill A. Shutemov 		get_page(page);
339d479960eSMinchan Kim 		if (pagevec_add_and_need_flush(pvec, page))
340c7c7b80cSAlex Shi 			pagevec_lru_move_fn(pvec, __activate_page);
341b01b2141SIngo Molnar 		local_unlock(&lru_pvecs.lock);
342eb709b0dSShaohua Li 	}
343eb709b0dSShaohua Li }
344eb709b0dSShaohua Li 
345eb709b0dSShaohua Li #else
346eb709b0dSShaohua Li static inline void activate_page_drain(int cpu)
347eb709b0dSShaohua Li {
348eb709b0dSShaohua Li }
349eb709b0dSShaohua Li 
350cc2828b2SYu Zhao static void activate_page(struct page *page)
351eb709b0dSShaohua Li {
3526168d0daSAlex Shi 	struct lruvec *lruvec;
353eb709b0dSShaohua Li 
354800d8c63SKirill A. Shutemov 	page = compound_head(page);
3556168d0daSAlex Shi 	if (TestClearPageLRU(page)) {
3566168d0daSAlex Shi 		lruvec = lock_page_lruvec_irq(page);
3576168d0daSAlex Shi 		__activate_page(page, lruvec);
3586168d0daSAlex Shi 		unlock_page_lruvec_irq(lruvec);
3596168d0daSAlex Shi 		SetPageLRU(page);
3606168d0daSAlex Shi 	}
3611da177e4SLinus Torvalds }
362eb709b0dSShaohua Li #endif
3631da177e4SLinus Torvalds 
364059285a2SMel Gorman static void __lru_cache_activate_page(struct page *page)
365059285a2SMel Gorman {
366b01b2141SIngo Molnar 	struct pagevec *pvec;
367059285a2SMel Gorman 	int i;
368059285a2SMel Gorman 
369b01b2141SIngo Molnar 	local_lock(&lru_pvecs.lock);
370b01b2141SIngo Molnar 	pvec = this_cpu_ptr(&lru_pvecs.lru_add);
371b01b2141SIngo Molnar 
372059285a2SMel Gorman 	/*
373059285a2SMel Gorman 	 * Search backwards on the optimistic assumption that the page being
374059285a2SMel Gorman 	 * activated has just been added to this pagevec. Note that only
375059285a2SMel Gorman 	 * the local pagevec is examined as a !PageLRU page could be in the
376059285a2SMel Gorman 	 * process of being released, reclaimed, migrated or on a remote
377059285a2SMel Gorman 	 * pagevec that is currently being drained. Furthermore, marking
378059285a2SMel Gorman 	 * a remote pagevec's page PageActive potentially hits a race where
379059285a2SMel Gorman 	 * a page is marked PageActive just after it is added to the inactive
380059285a2SMel Gorman 	 * list causing accounting errors and BUG_ON checks to trigger.
381059285a2SMel Gorman 	 */
382059285a2SMel Gorman 	for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
383059285a2SMel Gorman 		struct page *pagevec_page = pvec->pages[i];
384059285a2SMel Gorman 
385059285a2SMel Gorman 		if (pagevec_page == page) {
386059285a2SMel Gorman 			SetPageActive(page);
387059285a2SMel Gorman 			break;
388059285a2SMel Gorman 		}
389059285a2SMel Gorman 	}
390059285a2SMel Gorman 
391b01b2141SIngo Molnar 	local_unlock(&lru_pvecs.lock);
392059285a2SMel Gorman }
393059285a2SMel Gorman 
3941da177e4SLinus Torvalds /*
3951da177e4SLinus Torvalds  * Mark a page as having seen activity.
3961da177e4SLinus Torvalds  *
3971da177e4SLinus Torvalds  * inactive,unreferenced	->	inactive,referenced
3981da177e4SLinus Torvalds  * inactive,referenced		->	active,unreferenced
3991da177e4SLinus Torvalds  * active,unreferenced		->	active,referenced
400eb39d618SHugh Dickins  *
401eb39d618SHugh Dickins  * When a newly allocated page is not yet visible, so safe for non-atomic ops,
402eb39d618SHugh Dickins  * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
4031da177e4SLinus Torvalds  */
404920c7a5dSHarvey Harrison void mark_page_accessed(struct page *page)
4051da177e4SLinus Torvalds {
406e90309c9SKirill A. Shutemov 	page = compound_head(page);
407059285a2SMel Gorman 
408a1100a74SFengguang Wu 	if (!PageReferenced(page)) {
409a1100a74SFengguang Wu 		SetPageReferenced(page);
410a1100a74SFengguang Wu 	} else if (PageUnevictable(page)) {
411a1100a74SFengguang Wu 		/*
412a1100a74SFengguang Wu 		 * Unevictable pages are on the "LRU_UNEVICTABLE" list. But,
413a1100a74SFengguang Wu 		 * this list is never rotated or maintained, so marking an
414a1100a74SFengguang Wu 		 * evictable page accessed has no effect.
415a1100a74SFengguang Wu 		 */
416a1100a74SFengguang Wu 	} else if (!PageActive(page)) {
417059285a2SMel Gorman 		/*
418059285a2SMel Gorman 		 * If the page is on the LRU, queue it for activation via
419b01b2141SIngo Molnar 		 * lru_pvecs.activate_page. Otherwise, assume the page is on a
420059285a2SMel Gorman 		 * pagevec, mark it active and it'll be moved to the active
421059285a2SMel Gorman 		 * LRU on the next drain.
422059285a2SMel Gorman 		 */
423059285a2SMel Gorman 		if (PageLRU(page))
4241da177e4SLinus Torvalds 			activate_page(page);
425059285a2SMel Gorman 		else
426059285a2SMel Gorman 			__lru_cache_activate_page(page);
4271da177e4SLinus Torvalds 		ClearPageReferenced(page);
428a528910eSJohannes Weiner 		workingset_activation(page);
4291da177e4SLinus Torvalds 	}
43033c3fc71SVladimir Davydov 	if (page_is_idle(page))
43133c3fc71SVladimir Davydov 		clear_page_idle(page);
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds EXPORT_SYMBOL(mark_page_accessed);
4341da177e4SLinus Torvalds 
435f04e9ebbSKOSAKI Motohiro /**
436c53954a0SMel Gorman  * lru_cache_add - add a page to a page list
437f04e9ebbSKOSAKI Motohiro  * @page: the page to be added to the LRU.
4382329d375SJianyu Zhan  *
4392329d375SJianyu Zhan  * Queue the page for addition to the LRU via pagevec. The decision on whether
4402329d375SJianyu Zhan  * to add the page to the [in]active [file|anon] list is deferred until the
4412329d375SJianyu Zhan  * pagevec is drained. This gives a chance for the caller of lru_cache_add()
4422329d375SJianyu Zhan  * have the page added to the active list using mark_page_accessed().
443f04e9ebbSKOSAKI Motohiro  */
444c53954a0SMel Gorman void lru_cache_add(struct page *page)
4451da177e4SLinus Torvalds {
4466058eaecSJohannes Weiner 	struct pagevec *pvec;
4476058eaecSJohannes Weiner 
448309381feSSasha Levin 	VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
449309381feSSasha Levin 	VM_BUG_ON_PAGE(PageLRU(page), page);
4506058eaecSJohannes Weiner 
4516058eaecSJohannes Weiner 	get_page(page);
4526058eaecSJohannes Weiner 	local_lock(&lru_pvecs.lock);
4536058eaecSJohannes Weiner 	pvec = this_cpu_ptr(&lru_pvecs.lru_add);
454d479960eSMinchan Kim 	if (pagevec_add_and_need_flush(pvec, page))
4556058eaecSJohannes Weiner 		__pagevec_lru_add(pvec);
4566058eaecSJohannes Weiner 	local_unlock(&lru_pvecs.lock);
4571da177e4SLinus Torvalds }
4586058eaecSJohannes Weiner EXPORT_SYMBOL(lru_cache_add);
4591da177e4SLinus Torvalds 
460894bc310SLee Schermerhorn /**
461b518154eSJoonsoo Kim  * lru_cache_add_inactive_or_unevictable
46200501b53SJohannes Weiner  * @page:  the page to be added to LRU
46300501b53SJohannes Weiner  * @vma:   vma in which page is mapped for determining reclaimability
46400501b53SJohannes Weiner  *
465b518154eSJoonsoo Kim  * Place @page on the inactive or unevictable LRU list, depending on its
46612eab428SMiaohe Lin  * evictability.
46700501b53SJohannes Weiner  */
468b518154eSJoonsoo Kim void lru_cache_add_inactive_or_unevictable(struct page *page,
46900501b53SJohannes Weiner 					 struct vm_area_struct *vma)
47000501b53SJohannes Weiner {
471b518154eSJoonsoo Kim 	bool unevictable;
472b518154eSJoonsoo Kim 
47300501b53SJohannes Weiner 	VM_BUG_ON_PAGE(PageLRU(page), page);
47400501b53SJohannes Weiner 
475b518154eSJoonsoo Kim 	unevictable = (vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED;
476b518154eSJoonsoo Kim 	if (unlikely(unevictable) && !TestSetPageMlocked(page)) {
4770964730bSHugh Dickins 		int nr_pages = thp_nr_pages(page);
47800501b53SJohannes Weiner 		/*
479cb152a1aSShijie Luo 		 * We use the irq-unsafe __mod_zone_page_state because this
48000501b53SJohannes Weiner 		 * counter is not modified from interrupt context, and the pte
48100501b53SJohannes Weiner 		 * lock is held(spinlock), which implies preemption disabled.
48200501b53SJohannes Weiner 		 */
4830964730bSHugh Dickins 		__mod_zone_page_state(page_zone(page), NR_MLOCK, nr_pages);
4840964730bSHugh Dickins 		count_vm_events(UNEVICTABLE_PGMLOCKED, nr_pages);
48500501b53SJohannes Weiner 	}
4869c4e6b1aSShakeel Butt 	lru_cache_add(page);
48700501b53SJohannes Weiner }
48800501b53SJohannes Weiner 
489902aaed0SHisashi Hifumi /*
49031560180SMinchan Kim  * If the page can not be invalidated, it is moved to the
49131560180SMinchan Kim  * inactive list to speed up its reclaim.  It is moved to the
49231560180SMinchan Kim  * head of the list, rather than the tail, to give the flusher
49331560180SMinchan Kim  * threads some time to write it out, as this is much more
49431560180SMinchan Kim  * effective than the single-page writeout from reclaim.
495278df9f4SMinchan Kim  *
496278df9f4SMinchan Kim  * If the page isn't page_mapped and dirty/writeback, the page
497278df9f4SMinchan Kim  * could reclaim asap using PG_reclaim.
498278df9f4SMinchan Kim  *
499278df9f4SMinchan Kim  * 1. active, mapped page -> none
500278df9f4SMinchan Kim  * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
501278df9f4SMinchan Kim  * 3. inactive, mapped page -> none
502278df9f4SMinchan Kim  * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
503278df9f4SMinchan Kim  * 5. inactive, clean -> inactive, tail
504278df9f4SMinchan Kim  * 6. Others -> none
505278df9f4SMinchan Kim  *
506278df9f4SMinchan Kim  * In 4, why it moves inactive's head, the VM expects the page would
507278df9f4SMinchan Kim  * be write it out by flusher threads as this is much more effective
508278df9f4SMinchan Kim  * than the single-page writeout from reclaim.
50931560180SMinchan Kim  */
510c7c7b80cSAlex Shi static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec)
51131560180SMinchan Kim {
51246ae6b2cSYu Zhao 	bool active = PageActive(page);
5136c357848SMatthew Wilcox (Oracle) 	int nr_pages = thp_nr_pages(page);
51431560180SMinchan Kim 
515bad49d9cSMinchan Kim 	if (PageUnevictable(page))
516bad49d9cSMinchan Kim 		return;
517bad49d9cSMinchan Kim 
51831560180SMinchan Kim 	/* Some processes are using the page */
51931560180SMinchan Kim 	if (page_mapped(page))
52031560180SMinchan Kim 		return;
52131560180SMinchan Kim 
52246ae6b2cSYu Zhao 	del_page_from_lru_list(page, lruvec);
52331560180SMinchan Kim 	ClearPageActive(page);
52431560180SMinchan Kim 	ClearPageReferenced(page);
52531560180SMinchan Kim 
526278df9f4SMinchan Kim 	if (PageWriteback(page) || PageDirty(page)) {
527278df9f4SMinchan Kim 		/*
528278df9f4SMinchan Kim 		 * PG_reclaim could be raced with end_page_writeback
529278df9f4SMinchan Kim 		 * It can make readahead confusing.  But race window
530278df9f4SMinchan Kim 		 * is _really_ small and  it's non-critical problem.
531278df9f4SMinchan Kim 		 */
5323a9c9788SYu Zhao 		add_page_to_lru_list(page, lruvec);
533278df9f4SMinchan Kim 		SetPageReclaim(page);
534278df9f4SMinchan Kim 	} else {
535278df9f4SMinchan Kim 		/*
536278df9f4SMinchan Kim 		 * The page's writeback ends up during pagevec
537c4ffefd1SHyeonggon Yoo 		 * We move that page into tail of inactive.
538278df9f4SMinchan Kim 		 */
5393a9c9788SYu Zhao 		add_page_to_lru_list_tail(page, lruvec);
5405d91f31fSShakeel Butt 		__count_vm_events(PGROTATED, nr_pages);
541278df9f4SMinchan Kim 	}
542278df9f4SMinchan Kim 
54321e330fcSShakeel Butt 	if (active) {
5445d91f31fSShakeel Butt 		__count_vm_events(PGDEACTIVATE, nr_pages);
54521e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
54621e330fcSShakeel Butt 				     nr_pages);
54721e330fcSShakeel Butt 	}
54831560180SMinchan Kim }
54931560180SMinchan Kim 
550c7c7b80cSAlex Shi static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec)
5519c276cc6SMinchan Kim {
552fc574c23SAlex Shi 	if (PageActive(page) && !PageUnevictable(page)) {
5536c357848SMatthew Wilcox (Oracle) 		int nr_pages = thp_nr_pages(page);
5549c276cc6SMinchan Kim 
55546ae6b2cSYu Zhao 		del_page_from_lru_list(page, lruvec);
5569c276cc6SMinchan Kim 		ClearPageActive(page);
5579c276cc6SMinchan Kim 		ClearPageReferenced(page);
5583a9c9788SYu Zhao 		add_page_to_lru_list(page, lruvec);
5599c276cc6SMinchan Kim 
56021e330fcSShakeel Butt 		__count_vm_events(PGDEACTIVATE, nr_pages);
56121e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
56221e330fcSShakeel Butt 				     nr_pages);
5639c276cc6SMinchan Kim 	}
5649c276cc6SMinchan Kim }
56510853a03SMinchan Kim 
566c7c7b80cSAlex Shi static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec)
56710853a03SMinchan Kim {
568fc574c23SAlex Shi 	if (PageAnon(page) && PageSwapBacked(page) &&
56924c92eb7SShaohua Li 	    !PageSwapCache(page) && !PageUnevictable(page)) {
5706c357848SMatthew Wilcox (Oracle) 		int nr_pages = thp_nr_pages(page);
57110853a03SMinchan Kim 
57246ae6b2cSYu Zhao 		del_page_from_lru_list(page, lruvec);
57310853a03SMinchan Kim 		ClearPageActive(page);
57410853a03SMinchan Kim 		ClearPageReferenced(page);
575f7ad2a6cSShaohua Li 		/*
5769de4f22aSHuang Ying 		 * Lazyfree pages are clean anonymous pages.  They have
5779de4f22aSHuang Ying 		 * PG_swapbacked flag cleared, to distinguish them from normal
5789de4f22aSHuang Ying 		 * anonymous pages
579f7ad2a6cSShaohua Li 		 */
580f7ad2a6cSShaohua Li 		ClearPageSwapBacked(page);
5813a9c9788SYu Zhao 		add_page_to_lru_list(page, lruvec);
58210853a03SMinchan Kim 
58321e330fcSShakeel Butt 		__count_vm_events(PGLAZYFREE, nr_pages);
58421e330fcSShakeel Butt 		__count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE,
58521e330fcSShakeel Butt 				     nr_pages);
58610853a03SMinchan Kim 	}
58710853a03SMinchan Kim }
58810853a03SMinchan Kim 
58931560180SMinchan Kim /*
590902aaed0SHisashi Hifumi  * Drain pages out of the cpu's pagevecs.
591902aaed0SHisashi Hifumi  * Either "cpu" is the current CPU, and preemption has already been
592902aaed0SHisashi Hifumi  * disabled; or "cpu" is being hot-unplugged, and is already dead.
593902aaed0SHisashi Hifumi  */
594f0cb3c76SKonstantin Khlebnikov void lru_add_drain_cpu(int cpu)
5951da177e4SLinus Torvalds {
596b01b2141SIngo Molnar 	struct pagevec *pvec = &per_cpu(lru_pvecs.lru_add, cpu);
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	if (pagevec_count(pvec))
599a0b8cab3SMel Gorman 		__pagevec_lru_add(pvec);
600902aaed0SHisashi Hifumi 
601b01b2141SIngo Molnar 	pvec = &per_cpu(lru_rotate.pvec, cpu);
6027e0cc01eSQian Cai 	/* Disabling interrupts below acts as a compiler barrier. */
6037e0cc01eSQian Cai 	if (data_race(pagevec_count(pvec))) {
604902aaed0SHisashi Hifumi 		unsigned long flags;
605902aaed0SHisashi Hifumi 
606902aaed0SHisashi Hifumi 		/* No harm done if a racing interrupt already did this */
607b01b2141SIngo Molnar 		local_lock_irqsave(&lru_rotate.lock, flags);
608c7c7b80cSAlex Shi 		pagevec_lru_move_fn(pvec, pagevec_move_tail_fn);
609b01b2141SIngo Molnar 		local_unlock_irqrestore(&lru_rotate.lock, flags);
610902aaed0SHisashi Hifumi 	}
61131560180SMinchan Kim 
612b01b2141SIngo Molnar 	pvec = &per_cpu(lru_pvecs.lru_deactivate_file, cpu);
61331560180SMinchan Kim 	if (pagevec_count(pvec))
614c7c7b80cSAlex Shi 		pagevec_lru_move_fn(pvec, lru_deactivate_file_fn);
615eb709b0dSShaohua Li 
616b01b2141SIngo Molnar 	pvec = &per_cpu(lru_pvecs.lru_deactivate, cpu);
6179c276cc6SMinchan Kim 	if (pagevec_count(pvec))
618c7c7b80cSAlex Shi 		pagevec_lru_move_fn(pvec, lru_deactivate_fn);
6199c276cc6SMinchan Kim 
620b01b2141SIngo Molnar 	pvec = &per_cpu(lru_pvecs.lru_lazyfree, cpu);
62110853a03SMinchan Kim 	if (pagevec_count(pvec))
622c7c7b80cSAlex Shi 		pagevec_lru_move_fn(pvec, lru_lazyfree_fn);
62310853a03SMinchan Kim 
624eb709b0dSShaohua Li 	activate_page_drain(cpu);
62531560180SMinchan Kim }
62631560180SMinchan Kim 
62731560180SMinchan Kim /**
628cc5993bdSMinchan Kim  * deactivate_file_page - forcefully deactivate a file page
62931560180SMinchan Kim  * @page: page to deactivate
63031560180SMinchan Kim  *
63131560180SMinchan Kim  * This function hints the VM that @page is a good reclaim candidate,
63231560180SMinchan Kim  * for example if its invalidation fails due to the page being dirty
63331560180SMinchan Kim  * or under writeback.
63431560180SMinchan Kim  */
635cc5993bdSMinchan Kim void deactivate_file_page(struct page *page)
63631560180SMinchan Kim {
637821ed6bbSMinchan Kim 	/*
638cc5993bdSMinchan Kim 	 * In a workload with many unevictable page such as mprotect,
639cc5993bdSMinchan Kim 	 * unevictable page deactivation for accelerating reclaim is pointless.
640821ed6bbSMinchan Kim 	 */
641821ed6bbSMinchan Kim 	if (PageUnevictable(page))
642821ed6bbSMinchan Kim 		return;
643821ed6bbSMinchan Kim 
64431560180SMinchan Kim 	if (likely(get_page_unless_zero(page))) {
645b01b2141SIngo Molnar 		struct pagevec *pvec;
646b01b2141SIngo Molnar 
647b01b2141SIngo Molnar 		local_lock(&lru_pvecs.lock);
648b01b2141SIngo Molnar 		pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate_file);
64931560180SMinchan Kim 
650d479960eSMinchan Kim 		if (pagevec_add_and_need_flush(pvec, page))
651c7c7b80cSAlex Shi 			pagevec_lru_move_fn(pvec, lru_deactivate_file_fn);
652b01b2141SIngo Molnar 		local_unlock(&lru_pvecs.lock);
65331560180SMinchan Kim 	}
65480bfed90SAndrew Morton }
65580bfed90SAndrew Morton 
6569c276cc6SMinchan Kim /*
6579c276cc6SMinchan Kim  * deactivate_page - deactivate a page
6589c276cc6SMinchan Kim  * @page: page to deactivate
6599c276cc6SMinchan Kim  *
6609c276cc6SMinchan Kim  * deactivate_page() moves @page to the inactive list if @page was on the active
6619c276cc6SMinchan Kim  * list and was not an unevictable page.  This is done to accelerate the reclaim
6629c276cc6SMinchan Kim  * of @page.
6639c276cc6SMinchan Kim  */
6649c276cc6SMinchan Kim void deactivate_page(struct page *page)
6659c276cc6SMinchan Kim {
6669c276cc6SMinchan Kim 	if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
667b01b2141SIngo Molnar 		struct pagevec *pvec;
6689c276cc6SMinchan Kim 
669b01b2141SIngo Molnar 		local_lock(&lru_pvecs.lock);
670b01b2141SIngo Molnar 		pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate);
6719c276cc6SMinchan Kim 		get_page(page);
672d479960eSMinchan Kim 		if (pagevec_add_and_need_flush(pvec, page))
673c7c7b80cSAlex Shi 			pagevec_lru_move_fn(pvec, lru_deactivate_fn);
674b01b2141SIngo Molnar 		local_unlock(&lru_pvecs.lock);
6759c276cc6SMinchan Kim 	}
6769c276cc6SMinchan Kim }
6779c276cc6SMinchan Kim 
67810853a03SMinchan Kim /**
679f7ad2a6cSShaohua Li  * mark_page_lazyfree - make an anon page lazyfree
68010853a03SMinchan Kim  * @page: page to deactivate
68110853a03SMinchan Kim  *
682f7ad2a6cSShaohua Li  * mark_page_lazyfree() moves @page to the inactive file list.
683f7ad2a6cSShaohua Li  * This is done to accelerate the reclaim of @page.
68410853a03SMinchan Kim  */
685f7ad2a6cSShaohua Li void mark_page_lazyfree(struct page *page)
68610853a03SMinchan Kim {
687f7ad2a6cSShaohua Li 	if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
68824c92eb7SShaohua Li 	    !PageSwapCache(page) && !PageUnevictable(page)) {
689b01b2141SIngo Molnar 		struct pagevec *pvec;
69010853a03SMinchan Kim 
691b01b2141SIngo Molnar 		local_lock(&lru_pvecs.lock);
692b01b2141SIngo Molnar 		pvec = this_cpu_ptr(&lru_pvecs.lru_lazyfree);
69309cbfeafSKirill A. Shutemov 		get_page(page);
694d479960eSMinchan Kim 		if (pagevec_add_and_need_flush(pvec, page))
695c7c7b80cSAlex Shi 			pagevec_lru_move_fn(pvec, lru_lazyfree_fn);
696b01b2141SIngo Molnar 		local_unlock(&lru_pvecs.lock);
69710853a03SMinchan Kim 	}
69810853a03SMinchan Kim }
69910853a03SMinchan Kim 
70080bfed90SAndrew Morton void lru_add_drain(void)
70180bfed90SAndrew Morton {
702b01b2141SIngo Molnar 	local_lock(&lru_pvecs.lock);
703b01b2141SIngo Molnar 	lru_add_drain_cpu(smp_processor_id());
704b01b2141SIngo Molnar 	local_unlock(&lru_pvecs.lock);
705b01b2141SIngo Molnar }
706b01b2141SIngo Molnar 
707243418e3SMinchan Kim /*
708243418e3SMinchan Kim  * It's called from per-cpu workqueue context in SMP case so
709243418e3SMinchan Kim  * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on
710243418e3SMinchan Kim  * the same cpu. It shouldn't be a problem in !SMP case since
711243418e3SMinchan Kim  * the core is only one and the locks will disable preemption.
712243418e3SMinchan Kim  */
713243418e3SMinchan Kim static void lru_add_and_bh_lrus_drain(void)
714243418e3SMinchan Kim {
715243418e3SMinchan Kim 	local_lock(&lru_pvecs.lock);
716243418e3SMinchan Kim 	lru_add_drain_cpu(smp_processor_id());
717243418e3SMinchan Kim 	local_unlock(&lru_pvecs.lock);
718243418e3SMinchan Kim 	invalidate_bh_lrus_cpu();
719243418e3SMinchan Kim }
720243418e3SMinchan Kim 
721b01b2141SIngo Molnar void lru_add_drain_cpu_zone(struct zone *zone)
722b01b2141SIngo Molnar {
723b01b2141SIngo Molnar 	local_lock(&lru_pvecs.lock);
724b01b2141SIngo Molnar 	lru_add_drain_cpu(smp_processor_id());
725b01b2141SIngo Molnar 	drain_local_pages(zone);
726b01b2141SIngo Molnar 	local_unlock(&lru_pvecs.lock);
7271da177e4SLinus Torvalds }
7281da177e4SLinus Torvalds 
7296ea183d6SMichal Hocko #ifdef CONFIG_SMP
7306ea183d6SMichal Hocko 
7316ea183d6SMichal Hocko static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
7326ea183d6SMichal Hocko 
733c4028958SDavid Howells static void lru_add_drain_per_cpu(struct work_struct *dummy)
734053837fcSNick Piggin {
735243418e3SMinchan Kim 	lru_add_and_bh_lrus_drain();
736053837fcSNick Piggin }
737053837fcSNick Piggin 
7389852a721SMichal Hocko /*
7399852a721SMichal Hocko  * Doesn't need any cpu hotplug locking because we do rely on per-cpu
7409852a721SMichal Hocko  * kworkers being shut down before our page_alloc_cpu_dead callback is
7419852a721SMichal Hocko  * executed on the offlined cpu.
7429852a721SMichal Hocko  * Calling this function with cpu hotplug locks held can actually lead
7439852a721SMichal Hocko  * to obscure indirect dependencies via WQ context.
7449852a721SMichal Hocko  */
745d479960eSMinchan Kim inline void __lru_add_drain_all(bool force_all_cpus)
746053837fcSNick Piggin {
7476446a513SAhmed S. Darwish 	/*
7486446a513SAhmed S. Darwish 	 * lru_drain_gen - Global pages generation number
7496446a513SAhmed S. Darwish 	 *
7506446a513SAhmed S. Darwish 	 * (A) Definition: global lru_drain_gen = x implies that all generations
7516446a513SAhmed S. Darwish 	 *     0 < n <= x are already *scheduled* for draining.
7526446a513SAhmed S. Darwish 	 *
7536446a513SAhmed S. Darwish 	 * This is an optimization for the highly-contended use case where a
7546446a513SAhmed S. Darwish 	 * user space workload keeps constantly generating a flow of pages for
7556446a513SAhmed S. Darwish 	 * each CPU.
7566446a513SAhmed S. Darwish 	 */
7576446a513SAhmed S. Darwish 	static unsigned int lru_drain_gen;
7585fbc4616SChris Metcalf 	static struct cpumask has_work;
7596446a513SAhmed S. Darwish 	static DEFINE_MUTEX(lock);
7606446a513SAhmed S. Darwish 	unsigned cpu, this_gen;
7615fbc4616SChris Metcalf 
762ce612879SMichal Hocko 	/*
763ce612879SMichal Hocko 	 * Make sure nobody triggers this path before mm_percpu_wq is fully
764ce612879SMichal Hocko 	 * initialized.
765ce612879SMichal Hocko 	 */
766ce612879SMichal Hocko 	if (WARN_ON(!mm_percpu_wq))
767ce612879SMichal Hocko 		return;
768ce612879SMichal Hocko 
7696446a513SAhmed S. Darwish 	/*
7706446a513SAhmed S. Darwish 	 * Guarantee pagevec counter stores visible by this CPU are visible to
7716446a513SAhmed S. Darwish 	 * other CPUs before loading the current drain generation.
7726446a513SAhmed S. Darwish 	 */
7736446a513SAhmed S. Darwish 	smp_mb();
7746446a513SAhmed S. Darwish 
7756446a513SAhmed S. Darwish 	/*
7766446a513SAhmed S. Darwish 	 * (B) Locally cache global LRU draining generation number
7776446a513SAhmed S. Darwish 	 *
7786446a513SAhmed S. Darwish 	 * The read barrier ensures that the counter is loaded before the mutex
7796446a513SAhmed S. Darwish 	 * is taken. It pairs with smp_mb() inside the mutex critical section
7806446a513SAhmed S. Darwish 	 * at (D).
7816446a513SAhmed S. Darwish 	 */
7826446a513SAhmed S. Darwish 	this_gen = smp_load_acquire(&lru_drain_gen);
783eef1a429SKonstantin Khlebnikov 
7845fbc4616SChris Metcalf 	mutex_lock(&lock);
785eef1a429SKonstantin Khlebnikov 
786eef1a429SKonstantin Khlebnikov 	/*
7876446a513SAhmed S. Darwish 	 * (C) Exit the draining operation if a newer generation, from another
7886446a513SAhmed S. Darwish 	 * lru_add_drain_all(), was already scheduled for draining. Check (A).
789eef1a429SKonstantin Khlebnikov 	 */
790d479960eSMinchan Kim 	if (unlikely(this_gen != lru_drain_gen && !force_all_cpus))
791eef1a429SKonstantin Khlebnikov 		goto done;
792eef1a429SKonstantin Khlebnikov 
7936446a513SAhmed S. Darwish 	/*
7946446a513SAhmed S. Darwish 	 * (D) Increment global generation number
7956446a513SAhmed S. Darwish 	 *
7966446a513SAhmed S. Darwish 	 * Pairs with smp_load_acquire() at (B), outside of the critical
7976446a513SAhmed S. Darwish 	 * section. Use a full memory barrier to guarantee that the new global
7986446a513SAhmed S. Darwish 	 * drain generation number is stored before loading pagevec counters.
7996446a513SAhmed S. Darwish 	 *
8006446a513SAhmed S. Darwish 	 * This pairing must be done here, before the for_each_online_cpu loop
8016446a513SAhmed S. Darwish 	 * below which drains the page vectors.
8026446a513SAhmed S. Darwish 	 *
8036446a513SAhmed S. Darwish 	 * Let x, y, and z represent some system CPU numbers, where x < y < z.
804cb152a1aSShijie Luo 	 * Assume CPU #z is in the middle of the for_each_online_cpu loop
8056446a513SAhmed S. Darwish 	 * below and has already reached CPU #y's per-cpu data. CPU #x comes
8066446a513SAhmed S. Darwish 	 * along, adds some pages to its per-cpu vectors, then calls
8076446a513SAhmed S. Darwish 	 * lru_add_drain_all().
8086446a513SAhmed S. Darwish 	 *
8096446a513SAhmed S. Darwish 	 * If the paired barrier is done at any later step, e.g. after the
8106446a513SAhmed S. Darwish 	 * loop, CPU #x will just exit at (C) and miss flushing out all of its
8116446a513SAhmed S. Darwish 	 * added pages.
8126446a513SAhmed S. Darwish 	 */
8136446a513SAhmed S. Darwish 	WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1);
8146446a513SAhmed S. Darwish 	smp_mb();
815eef1a429SKonstantin Khlebnikov 
8165fbc4616SChris Metcalf 	cpumask_clear(&has_work);
8175fbc4616SChris Metcalf 	for_each_online_cpu(cpu) {
8185fbc4616SChris Metcalf 		struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
8195fbc4616SChris Metcalf 
820d479960eSMinchan Kim 		if (force_all_cpus ||
821d479960eSMinchan Kim 		    pagevec_count(&per_cpu(lru_pvecs.lru_add, cpu)) ||
8227e0cc01eSQian Cai 		    data_race(pagevec_count(&per_cpu(lru_rotate.pvec, cpu))) ||
823b01b2141SIngo Molnar 		    pagevec_count(&per_cpu(lru_pvecs.lru_deactivate_file, cpu)) ||
824b01b2141SIngo Molnar 		    pagevec_count(&per_cpu(lru_pvecs.lru_deactivate, cpu)) ||
825b01b2141SIngo Molnar 		    pagevec_count(&per_cpu(lru_pvecs.lru_lazyfree, cpu)) ||
8268cc621d2SMinchan Kim 		    need_activate_page_drain(cpu) ||
8278cc621d2SMinchan Kim 		    has_bh_in_lru(cpu, NULL)) {
8285fbc4616SChris Metcalf 			INIT_WORK(work, lru_add_drain_per_cpu);
829ce612879SMichal Hocko 			queue_work_on(cpu, mm_percpu_wq, work);
8306446a513SAhmed S. Darwish 			__cpumask_set_cpu(cpu, &has_work);
8315fbc4616SChris Metcalf 		}
8325fbc4616SChris Metcalf 	}
8335fbc4616SChris Metcalf 
8345fbc4616SChris Metcalf 	for_each_cpu(cpu, &has_work)
8355fbc4616SChris Metcalf 		flush_work(&per_cpu(lru_add_drain_work, cpu));
8365fbc4616SChris Metcalf 
837eef1a429SKonstantin Khlebnikov done:
8385fbc4616SChris Metcalf 	mutex_unlock(&lock);
839053837fcSNick Piggin }
840d479960eSMinchan Kim 
841d479960eSMinchan Kim void lru_add_drain_all(void)
842d479960eSMinchan Kim {
843d479960eSMinchan Kim 	__lru_add_drain_all(false);
844d479960eSMinchan Kim }
8456ea183d6SMichal Hocko #else
8466ea183d6SMichal Hocko void lru_add_drain_all(void)
8476ea183d6SMichal Hocko {
8486ea183d6SMichal Hocko 	lru_add_drain();
8496ea183d6SMichal Hocko }
8506446a513SAhmed S. Darwish #endif /* CONFIG_SMP */
851053837fcSNick Piggin 
852d479960eSMinchan Kim atomic_t lru_disable_count = ATOMIC_INIT(0);
853d479960eSMinchan Kim 
854d479960eSMinchan Kim /*
855d479960eSMinchan Kim  * lru_cache_disable() needs to be called before we start compiling
856d479960eSMinchan Kim  * a list of pages to be migrated using isolate_lru_page().
857d479960eSMinchan Kim  * It drains pages on LRU cache and then disable on all cpus until
858d479960eSMinchan Kim  * lru_cache_enable is called.
859d479960eSMinchan Kim  *
860d479960eSMinchan Kim  * Must be paired with a call to lru_cache_enable().
861d479960eSMinchan Kim  */
862d479960eSMinchan Kim void lru_cache_disable(void)
863d479960eSMinchan Kim {
864d479960eSMinchan Kim 	atomic_inc(&lru_disable_count);
865d479960eSMinchan Kim #ifdef CONFIG_SMP
866d479960eSMinchan Kim 	/*
867d479960eSMinchan Kim 	 * lru_add_drain_all in the force mode will schedule draining on
868d479960eSMinchan Kim 	 * all online CPUs so any calls of lru_cache_disabled wrapped by
869d479960eSMinchan Kim 	 * local_lock or preemption disabled would be ordered by that.
870d479960eSMinchan Kim 	 * The atomic operation doesn't need to have stronger ordering
871d479960eSMinchan Kim 	 * requirements because that is enforeced by the scheduling
872d479960eSMinchan Kim 	 * guarantees.
873d479960eSMinchan Kim 	 */
874d479960eSMinchan Kim 	__lru_add_drain_all(true);
875d479960eSMinchan Kim #else
876243418e3SMinchan Kim 	lru_add_and_bh_lrus_drain();
877d479960eSMinchan Kim #endif
878d479960eSMinchan Kim }
879d479960eSMinchan Kim 
880aabfb572SMichal Hocko /**
881ea1754a0SKirill A. Shutemov  * release_pages - batched put_page()
882aabfb572SMichal Hocko  * @pages: array of pages to release
883aabfb572SMichal Hocko  * @nr: number of pages
8841da177e4SLinus Torvalds  *
885aabfb572SMichal Hocko  * Decrement the reference count on all the pages in @pages.  If it
886aabfb572SMichal Hocko  * fell to zero, remove the page from the LRU and free it.
8871da177e4SLinus Torvalds  */
888c6f92f9fSMel Gorman void release_pages(struct page **pages, int nr)
8891da177e4SLinus Torvalds {
8901da177e4SLinus Torvalds 	int i;
891cc59850eSKonstantin Khlebnikov 	LIST_HEAD(pages_to_free);
8926168d0daSAlex Shi 	struct lruvec *lruvec = NULL;
8933f649ab7SKees Cook 	unsigned long flags;
8943f649ab7SKees Cook 	unsigned int lock_batch;
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	for (i = 0; i < nr; i++) {
8971da177e4SLinus Torvalds 		struct page *page = pages[i];
8981da177e4SLinus Torvalds 
899aabfb572SMichal Hocko 		/*
900aabfb572SMichal Hocko 		 * Make sure the IRQ-safe lock-holding time does not get
901aabfb572SMichal Hocko 		 * excessive with a continuous string of pages from the
9026168d0daSAlex Shi 		 * same lruvec. The lock is held only if lruvec != NULL.
903aabfb572SMichal Hocko 		 */
9046168d0daSAlex Shi 		if (lruvec && ++lock_batch == SWAP_CLUSTER_MAX) {
9056168d0daSAlex Shi 			unlock_page_lruvec_irqrestore(lruvec, flags);
9066168d0daSAlex Shi 			lruvec = NULL;
907aabfb572SMichal Hocko 		}
908aabfb572SMichal Hocko 
909a9b576f7SRalph Campbell 		page = compound_head(page);
9106fcb52a5SAaron Lu 		if (is_huge_zero_page(page))
911aa88b68cSKirill A. Shutemov 			continue;
912aa88b68cSKirill A. Shutemov 
913c5d6c45eSIra Weiny 		if (is_zone_device_page(page)) {
9146168d0daSAlex Shi 			if (lruvec) {
9156168d0daSAlex Shi 				unlock_page_lruvec_irqrestore(lruvec, flags);
9166168d0daSAlex Shi 				lruvec = NULL;
917df6ad698SJérôme Glisse 			}
918c5d6c45eSIra Weiny 			/*
919c5d6c45eSIra Weiny 			 * ZONE_DEVICE pages that return 'false' from
920a3e7bea0SMiaohe Lin 			 * page_is_devmap_managed() do not require special
921c5d6c45eSIra Weiny 			 * processing, and instead, expect a call to
922c5d6c45eSIra Weiny 			 * put_page_testzero().
923c5d6c45eSIra Weiny 			 */
92407d80269SJohn Hubbard 			if (page_is_devmap_managed(page)) {
92507d80269SJohn Hubbard 				put_devmap_managed_page(page);
926df6ad698SJérôme Glisse 				continue;
927df6ad698SJérôme Glisse 			}
92843fbdeb3SRalph Campbell 			if (put_page_testzero(page))
92943fbdeb3SRalph Campbell 				put_dev_pagemap(page->pgmap);
93043fbdeb3SRalph Campbell 			continue;
93107d80269SJohn Hubbard 		}
932df6ad698SJérôme Glisse 
933b5810039SNick Piggin 		if (!put_page_testzero(page))
9341da177e4SLinus Torvalds 			continue;
9351da177e4SLinus Torvalds 
936ddc58f27SKirill A. Shutemov 		if (PageCompound(page)) {
9376168d0daSAlex Shi 			if (lruvec) {
9386168d0daSAlex Shi 				unlock_page_lruvec_irqrestore(lruvec, flags);
9396168d0daSAlex Shi 				lruvec = NULL;
940ddc58f27SKirill A. Shutemov 			}
941ddc58f27SKirill A. Shutemov 			__put_compound_page(page);
942ddc58f27SKirill A. Shutemov 			continue;
943ddc58f27SKirill A. Shutemov 		}
944ddc58f27SKirill A. Shutemov 
94546453a6eSNick Piggin 		if (PageLRU(page)) {
9462a5e4e34SAlexander Duyck 			struct lruvec *prev_lruvec = lruvec;
947894bc310SLee Schermerhorn 
9482a5e4e34SAlexander Duyck 			lruvec = relock_page_lruvec_irqsave(page, lruvec,
9492a5e4e34SAlexander Duyck 									&flags);
9502a5e4e34SAlexander Duyck 			if (prev_lruvec != lruvec)
951aabfb572SMichal Hocko 				lock_batch = 0;
952fa9add64SHugh Dickins 
95346ae6b2cSYu Zhao 			del_page_from_lru_list(page, lruvec);
95487560179SYu Zhao 			__clear_page_lru_flags(page);
95546453a6eSNick Piggin 		}
95646453a6eSNick Piggin 
95762906027SNicholas Piggin 		__ClearPageWaiters(page);
958c53954a0SMel Gorman 
959cc59850eSKonstantin Khlebnikov 		list_add(&page->lru, &pages_to_free);
9601da177e4SLinus Torvalds 	}
9616168d0daSAlex Shi 	if (lruvec)
9626168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
9631da177e4SLinus Torvalds 
964747db954SJohannes Weiner 	mem_cgroup_uncharge_list(&pages_to_free);
9652d4894b5SMel Gorman 	free_unref_page_list(&pages_to_free);
9661da177e4SLinus Torvalds }
9670be8557bSMiklos Szeredi EXPORT_SYMBOL(release_pages);
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds /*
9701da177e4SLinus Torvalds  * The pages which we're about to release may be in the deferred lru-addition
9711da177e4SLinus Torvalds  * queues.  That would prevent them from really being freed right now.  That's
9721da177e4SLinus Torvalds  * OK from a correctness point of view but is inefficient - those pages may be
9731da177e4SLinus Torvalds  * cache-warm and we want to give them back to the page allocator ASAP.
9741da177e4SLinus Torvalds  *
9751da177e4SLinus Torvalds  * So __pagevec_release() will drain those queues here.  __pagevec_lru_add()
9761da177e4SLinus Torvalds  * and __pagevec_lru_add_active() call release_pages() directly to avoid
9771da177e4SLinus Torvalds  * mutual recursion.
9781da177e4SLinus Torvalds  */
9791da177e4SLinus Torvalds void __pagevec_release(struct pagevec *pvec)
9801da177e4SLinus Torvalds {
9817f0b5fb9SMel Gorman 	if (!pvec->percpu_pvec_drained) {
9821da177e4SLinus Torvalds 		lru_add_drain();
9837f0b5fb9SMel Gorman 		pvec->percpu_pvec_drained = true;
984d9ed0d08SMel Gorman 	}
985c6f92f9fSMel Gorman 	release_pages(pvec->pages, pagevec_count(pvec));
9861da177e4SLinus Torvalds 	pagevec_reinit(pvec);
9871da177e4SLinus Torvalds }
9887f285701SSteve French EXPORT_SYMBOL(__pagevec_release);
9897f285701SSteve French 
990c7c7b80cSAlex Shi static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec)
9913dd7ae8eSShaohua Li {
9929c4e6b1aSShakeel Butt 	int was_unevictable = TestClearPageUnevictable(page);
9936c357848SMatthew Wilcox (Oracle) 	int nr_pages = thp_nr_pages(page);
9943dd7ae8eSShaohua Li 
995309381feSSasha Levin 	VM_BUG_ON_PAGE(PageLRU(page), page);
9963dd7ae8eSShaohua Li 
9979c4e6b1aSShakeel Butt 	/*
9989c4e6b1aSShakeel Butt 	 * Page becomes evictable in two ways:
999dae966dcSPeng Fan 	 * 1) Within LRU lock [munlock_vma_page() and __munlock_pagevec()].
10009c4e6b1aSShakeel Butt 	 * 2) Before acquiring LRU lock to put the page to correct LRU and then
10019c4e6b1aSShakeel Butt 	 *   a) do PageLRU check with lock [check_move_unevictable_pages]
10029c4e6b1aSShakeel Butt 	 *   b) do PageLRU check before lock [clear_page_mlock]
10039c4e6b1aSShakeel Butt 	 *
10049c4e6b1aSShakeel Butt 	 * (1) & (2a) are ok as LRU lock will serialize them. For (2b), we need
10059c4e6b1aSShakeel Butt 	 * following strict ordering:
10069c4e6b1aSShakeel Butt 	 *
10079c4e6b1aSShakeel Butt 	 * #0: __pagevec_lru_add_fn		#1: clear_page_mlock
10089c4e6b1aSShakeel Butt 	 *
10099c4e6b1aSShakeel Butt 	 * SetPageLRU()				TestClearPageMlocked()
10109c4e6b1aSShakeel Butt 	 * smp_mb() // explicit ordering	// above provides strict
10119c4e6b1aSShakeel Butt 	 *					// ordering
10129c4e6b1aSShakeel Butt 	 * PageMlocked()			PageLRU()
10139c4e6b1aSShakeel Butt 	 *
10149c4e6b1aSShakeel Butt 	 *
10159c4e6b1aSShakeel Butt 	 * if '#1' does not observe setting of PG_lru by '#0' and fails
10169c4e6b1aSShakeel Butt 	 * isolation, the explicit barrier will make sure that page_evictable
10179c4e6b1aSShakeel Butt 	 * check will put the page in correct LRU. Without smp_mb(), SetPageLRU
10189c4e6b1aSShakeel Butt 	 * can be reordered after PageMlocked check and can make '#1' to fail
10199c4e6b1aSShakeel Butt 	 * the isolation of the page whose Mlocked bit is cleared (#0 is also
10209c4e6b1aSShakeel Butt 	 * looking at the same page) and the evictable page will be stranded
10219c4e6b1aSShakeel Butt 	 * in an unevictable LRU.
10229c4e6b1aSShakeel Butt 	 */
10239a9b6cceSYang Shi 	SetPageLRU(page);
10249a9b6cceSYang Shi 	smp_mb__after_atomic();
10259c4e6b1aSShakeel Butt 
10269c4e6b1aSShakeel Butt 	if (page_evictable(page)) {
10279c4e6b1aSShakeel Butt 		if (was_unevictable)
10285d91f31fSShakeel Butt 			__count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages);
10299c4e6b1aSShakeel Butt 	} else {
10309c4e6b1aSShakeel Butt 		ClearPageActive(page);
10319c4e6b1aSShakeel Butt 		SetPageUnevictable(page);
10329c4e6b1aSShakeel Butt 		if (!was_unevictable)
10335d91f31fSShakeel Butt 			__count_vm_events(UNEVICTABLE_PGCULLED, nr_pages);
10349c4e6b1aSShakeel Butt 	}
10359c4e6b1aSShakeel Butt 
10363a9c9788SYu Zhao 	add_page_to_lru_list(page, lruvec);
103786140453SYu Zhao 	trace_mm_lru_insertion(page);
10383dd7ae8eSShaohua Li }
10393dd7ae8eSShaohua Li 
10401da177e4SLinus Torvalds /*
10411da177e4SLinus Torvalds  * Add the passed pages to the LRU, then drop the caller's refcount
10421da177e4SLinus Torvalds  * on them.  Reinitialises the caller's pagevec.
10431da177e4SLinus Torvalds  */
1044a0b8cab3SMel Gorman void __pagevec_lru_add(struct pagevec *pvec)
10451da177e4SLinus Torvalds {
1046fc574c23SAlex Shi 	int i;
10476168d0daSAlex Shi 	struct lruvec *lruvec = NULL;
1048fc574c23SAlex Shi 	unsigned long flags = 0;
1049fc574c23SAlex Shi 
1050fc574c23SAlex Shi 	for (i = 0; i < pagevec_count(pvec); i++) {
1051fc574c23SAlex Shi 		struct page *page = pvec->pages[i];
1052fc574c23SAlex Shi 
10532a5e4e34SAlexander Duyck 		lruvec = relock_page_lruvec_irqsave(page, lruvec, &flags);
1054fc574c23SAlex Shi 		__pagevec_lru_add_fn(page, lruvec);
1055fc574c23SAlex Shi 	}
10566168d0daSAlex Shi 	if (lruvec)
10576168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(lruvec, flags);
1058fc574c23SAlex Shi 	release_pages(pvec->pages, pvec->nr);
1059fc574c23SAlex Shi 	pagevec_reinit(pvec);
10601da177e4SLinus Torvalds }
1061f04e9ebbSKOSAKI Motohiro 
10621da177e4SLinus Torvalds /**
10630cd6144aSJohannes Weiner  * pagevec_remove_exceptionals - pagevec exceptionals pruning
10640cd6144aSJohannes Weiner  * @pvec:	The pagevec to prune
10650cd6144aSJohannes Weiner  *
1066a656a202SMatthew Wilcox (Oracle)  * find_get_entries() fills both pages and XArray value entries (aka
1067a656a202SMatthew Wilcox (Oracle)  * exceptional entries) into the pagevec.  This function prunes all
10680cd6144aSJohannes Weiner  * exceptionals from @pvec without leaving holes, so that it can be
10690cd6144aSJohannes Weiner  * passed on to page-only pagevec operations.
10700cd6144aSJohannes Weiner  */
10710cd6144aSJohannes Weiner void pagevec_remove_exceptionals(struct pagevec *pvec)
10720cd6144aSJohannes Weiner {
10730cd6144aSJohannes Weiner 	int i, j;
10740cd6144aSJohannes Weiner 
10750cd6144aSJohannes Weiner 	for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
10760cd6144aSJohannes Weiner 		struct page *page = pvec->pages[i];
10773159f943SMatthew Wilcox 		if (!xa_is_value(page))
10780cd6144aSJohannes Weiner 			pvec->pages[j++] = page;
10790cd6144aSJohannes Weiner 	}
10800cd6144aSJohannes Weiner 	pvec->nr = j;
10810cd6144aSJohannes Weiner }
10820cd6144aSJohannes Weiner 
10830cd6144aSJohannes Weiner /**
1084b947cee4SJan Kara  * pagevec_lookup_range - gang pagecache lookup
10851da177e4SLinus Torvalds  * @pvec:	Where the resulting pages are placed
10861da177e4SLinus Torvalds  * @mapping:	The address_space to search
10871da177e4SLinus Torvalds  * @start:	The starting page index
1088b947cee4SJan Kara  * @end:	The final page index
10891da177e4SLinus Torvalds  *
1090e02a9f04SRandy Dunlap  * pagevec_lookup_range() will search for & return a group of up to PAGEVEC_SIZE
1091b947cee4SJan Kara  * pages in the mapping starting from index @start and upto index @end
1092b947cee4SJan Kara  * (inclusive).  The pages are placed in @pvec.  pagevec_lookup() takes a
10931da177e4SLinus Torvalds  * reference against the pages in @pvec.
10941da177e4SLinus Torvalds  *
10951da177e4SLinus Torvalds  * The search returns a group of mapping-contiguous pages with ascending
1096d72dc8a2SJan Kara  * indexes.  There may be holes in the indices due to not-present pages. We
1097d72dc8a2SJan Kara  * also update @start to index the next page for the traversal.
10981da177e4SLinus Torvalds  *
1099b947cee4SJan Kara  * pagevec_lookup_range() returns the number of pages which were found. If this
1100e02a9f04SRandy Dunlap  * number is smaller than PAGEVEC_SIZE, the end of specified range has been
1101b947cee4SJan Kara  * reached.
11021da177e4SLinus Torvalds  */
1103b947cee4SJan Kara unsigned pagevec_lookup_range(struct pagevec *pvec,
1104397162ffSJan Kara 		struct address_space *mapping, pgoff_t *start, pgoff_t end)
11051da177e4SLinus Torvalds {
1106397162ffSJan Kara 	pvec->nr = find_get_pages_range(mapping, start, end, PAGEVEC_SIZE,
1107b947cee4SJan Kara 					pvec->pages);
11081da177e4SLinus Torvalds 	return pagevec_count(pvec);
11091da177e4SLinus Torvalds }
1110b947cee4SJan Kara EXPORT_SYMBOL(pagevec_lookup_range);
111178539fdfSChristoph Hellwig 
111272b045aeSJan Kara unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
111372b045aeSJan Kara 		struct address_space *mapping, pgoff_t *index, pgoff_t end,
111410bbd235SMatthew Wilcox 		xa_mark_t tag)
11151da177e4SLinus Torvalds {
111672b045aeSJan Kara 	pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
111767fd707fSJan Kara 					PAGEVEC_SIZE, pvec->pages);
11181da177e4SLinus Torvalds 	return pagevec_count(pvec);
11191da177e4SLinus Torvalds }
112072b045aeSJan Kara EXPORT_SYMBOL(pagevec_lookup_range_tag);
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds /*
11231da177e4SLinus Torvalds  * Perform any setup for the swap system
11241da177e4SLinus Torvalds  */
11251da177e4SLinus Torvalds void __init swap_setup(void)
11261da177e4SLinus Torvalds {
1127ca79b0c2SArun KS 	unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
1128e0bf68ddSPeter Zijlstra 
11291da177e4SLinus Torvalds 	/* Use a smaller cluster for small-memory machines */
11301da177e4SLinus Torvalds 	if (megs < 16)
11311da177e4SLinus Torvalds 		page_cluster = 2;
11321da177e4SLinus Torvalds 	else
11331da177e4SLinus Torvalds 		page_cluster = 3;
11341da177e4SLinus Torvalds 	/*
11351da177e4SLinus Torvalds 	 * Right now other parts of the system means that we
11361da177e4SLinus Torvalds 	 * _really_ don't want to cluster much more
11371da177e4SLinus Torvalds 	 */
11381da177e4SLinus Torvalds }
113907d80269SJohn Hubbard 
114007d80269SJohn Hubbard #ifdef CONFIG_DEV_PAGEMAP_OPS
114107d80269SJohn Hubbard void put_devmap_managed_page(struct page *page)
114207d80269SJohn Hubbard {
114307d80269SJohn Hubbard 	int count;
114407d80269SJohn Hubbard 
114507d80269SJohn Hubbard 	if (WARN_ON_ONCE(!page_is_devmap_managed(page)))
114607d80269SJohn Hubbard 		return;
114707d80269SJohn Hubbard 
114807d80269SJohn Hubbard 	count = page_ref_dec_return(page);
114907d80269SJohn Hubbard 
115007d80269SJohn Hubbard 	/*
115107d80269SJohn Hubbard 	 * devmap page refcounts are 1-based, rather than 0-based: if
115207d80269SJohn Hubbard 	 * refcount is 1, then the page is free and the refcount is
115307d80269SJohn Hubbard 	 * stable because nobody holds a reference on the page.
115407d80269SJohn Hubbard 	 */
115507d80269SJohn Hubbard 	if (count == 1)
115607d80269SJohn Hubbard 		free_devmap_managed_page(page);
115707d80269SJohn Hubbard 	else if (!count)
115807d80269SJohn Hubbard 		__put_page(page);
115907d80269SJohn Hubbard }
116007d80269SJohn Hubbard EXPORT_SYMBOL(put_devmap_managed_page);
116107d80269SJohn Hubbard #endif
1162