xref: /linux/mm/swap.c (revision 9c276cc65a58faf98be8e56962745ec99ab87636)
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>
381da177e4SLinus Torvalds 
3964d6519dSLee Schermerhorn #include "internal.h"
4064d6519dSLee Schermerhorn 
41c6286c98SMel Gorman #define CREATE_TRACE_POINTS
42c6286c98SMel Gorman #include <trace/events/pagemap.h>
43c6286c98SMel Gorman 
441da177e4SLinus Torvalds /* How many pages do we try to swap or page in/out together? */
451da177e4SLinus Torvalds int page_cluster;
461da177e4SLinus Torvalds 
4713f7f789SMel Gorman static DEFINE_PER_CPU(struct pagevec, lru_add_pvec);
48f84f9504SVegard Nossum static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
49cc5993bdSMinchan Kim static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
50*9c276cc6SMinchan Kim static DEFINE_PER_CPU(struct pagevec, lru_deactivate_pvecs);
51f7ad2a6cSShaohua Li static DEFINE_PER_CPU(struct pagevec, lru_lazyfree_pvecs);
52a4a921aaSMing Li #ifdef CONFIG_SMP
53a4a921aaSMing Li static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
54a4a921aaSMing Li #endif
55902aaed0SHisashi Hifumi 
56b221385bSAdrian Bunk /*
57b221385bSAdrian Bunk  * This path almost never happens for VM activity - pages are normally
58b221385bSAdrian Bunk  * freed via pagevecs.  But it gets used by networking.
59b221385bSAdrian Bunk  */
60920c7a5dSHarvey Harrison static void __page_cache_release(struct page *page)
61b221385bSAdrian Bunk {
62b221385bSAdrian Bunk 	if (PageLRU(page)) {
63f4b7e272SAndrey Ryabinin 		pg_data_t *pgdat = page_pgdat(page);
64fa9add64SHugh Dickins 		struct lruvec *lruvec;
65fa9add64SHugh Dickins 		unsigned long flags;
66b221385bSAdrian Bunk 
67f4b7e272SAndrey Ryabinin 		spin_lock_irqsave(&pgdat->lru_lock, flags);
68f4b7e272SAndrey Ryabinin 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
69309381feSSasha Levin 		VM_BUG_ON_PAGE(!PageLRU(page), page);
70b221385bSAdrian Bunk 		__ClearPageLRU(page);
71fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, page_off_lru(page));
72f4b7e272SAndrey Ryabinin 		spin_unlock_irqrestore(&pgdat->lru_lock, flags);
73b221385bSAdrian Bunk 	}
7462906027SNicholas Piggin 	__ClearPageWaiters(page);
7591807063SAndrea Arcangeli }
7691807063SAndrea Arcangeli 
7791807063SAndrea Arcangeli static void __put_single_page(struct page *page)
7891807063SAndrea Arcangeli {
7991807063SAndrea Arcangeli 	__page_cache_release(page);
807ae88534SYang Shi 	mem_cgroup_uncharge(page);
812d4894b5SMel Gorman 	free_unref_page(page);
82b221385bSAdrian Bunk }
83b221385bSAdrian Bunk 
8491807063SAndrea Arcangeli static void __put_compound_page(struct page *page)
8591807063SAndrea Arcangeli {
8691807063SAndrea Arcangeli 	compound_page_dtor *dtor;
8791807063SAndrea Arcangeli 
88822fc613SNaoya Horiguchi 	/*
89822fc613SNaoya Horiguchi 	 * __page_cache_release() is supposed to be called for thp, not for
90822fc613SNaoya Horiguchi 	 * hugetlb. This is because hugetlb page does never have PageLRU set
91822fc613SNaoya Horiguchi 	 * (it's never listed to any LRU lists) and no memcg routines should
92822fc613SNaoya Horiguchi 	 * be called for hugetlb (it has a separate hugetlb_cgroup.)
93822fc613SNaoya Horiguchi 	 */
94822fc613SNaoya Horiguchi 	if (!PageHuge(page))
9591807063SAndrea Arcangeli 		__page_cache_release(page);
9691807063SAndrea Arcangeli 	dtor = get_compound_page_dtor(page);
9791807063SAndrea Arcangeli 	(*dtor)(page);
9891807063SAndrea Arcangeli }
9991807063SAndrea Arcangeli 
100ddc58f27SKirill A. Shutemov void __put_page(struct page *page)
101c747ce79SJianyu Zhan {
10271389703SDan Williams 	if (is_zone_device_page(page)) {
10371389703SDan Williams 		put_dev_pagemap(page->pgmap);
10471389703SDan Williams 
10571389703SDan Williams 		/*
10671389703SDan Williams 		 * The page belongs to the device that created pgmap. Do
10771389703SDan Williams 		 * not return it to page allocator.
10871389703SDan Williams 		 */
10971389703SDan Williams 		return;
11071389703SDan Williams 	}
11171389703SDan Williams 
112ddc58f27SKirill A. Shutemov 	if (unlikely(PageCompound(page)))
11326296ad2SAndrew Morton 		__put_compound_page(page);
11426296ad2SAndrew Morton 	else
11526296ad2SAndrew Morton 		__put_single_page(page);
11626296ad2SAndrew Morton }
117ddc58f27SKirill A. Shutemov EXPORT_SYMBOL(__put_page);
11870b50f94SAndrea Arcangeli 
1191d7ea732SAlexander Zarochentsev /**
1207682486bSRandy Dunlap  * put_pages_list() - release a list of pages
1217682486bSRandy Dunlap  * @pages: list of pages threaded on page->lru
1221d7ea732SAlexander Zarochentsev  *
1231d7ea732SAlexander Zarochentsev  * Release a list of pages which are strung together on page.lru.  Currently
1241d7ea732SAlexander Zarochentsev  * used by read_cache_pages() and related error recovery code.
1251d7ea732SAlexander Zarochentsev  */
1261d7ea732SAlexander Zarochentsev void put_pages_list(struct list_head *pages)
1271d7ea732SAlexander Zarochentsev {
1281d7ea732SAlexander Zarochentsev 	while (!list_empty(pages)) {
1291d7ea732SAlexander Zarochentsev 		struct page *victim;
1301d7ea732SAlexander Zarochentsev 
131f86196eaSNikolay Borisov 		victim = lru_to_page(pages);
1321d7ea732SAlexander Zarochentsev 		list_del(&victim->lru);
13309cbfeafSKirill A. Shutemov 		put_page(victim);
1341d7ea732SAlexander Zarochentsev 	}
1351d7ea732SAlexander Zarochentsev }
1361d7ea732SAlexander Zarochentsev EXPORT_SYMBOL(put_pages_list);
1371d7ea732SAlexander Zarochentsev 
13818022c5dSMel Gorman /*
13918022c5dSMel Gorman  * get_kernel_pages() - pin kernel pages in memory
14018022c5dSMel Gorman  * @kiov:	An array of struct kvec structures
14118022c5dSMel Gorman  * @nr_segs:	number of segments to pin
14218022c5dSMel Gorman  * @write:	pinning for read/write, currently ignored
14318022c5dSMel Gorman  * @pages:	array that receives pointers to the pages pinned.
14418022c5dSMel Gorman  *		Should be at least nr_segs long.
14518022c5dSMel Gorman  *
14618022c5dSMel Gorman  * Returns number of pages pinned. This may be fewer than the number
14718022c5dSMel Gorman  * requested. If nr_pages is 0 or negative, returns 0. If no pages
14818022c5dSMel Gorman  * were pinned, returns -errno. Each page returned must be released
14918022c5dSMel Gorman  * with a put_page() call when it is finished with.
15018022c5dSMel Gorman  */
15118022c5dSMel Gorman int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
15218022c5dSMel Gorman 		struct page **pages)
15318022c5dSMel Gorman {
15418022c5dSMel Gorman 	int seg;
15518022c5dSMel Gorman 
15618022c5dSMel Gorman 	for (seg = 0; seg < nr_segs; seg++) {
15718022c5dSMel Gorman 		if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
15818022c5dSMel Gorman 			return seg;
15918022c5dSMel Gorman 
1605a178119SMel Gorman 		pages[seg] = kmap_to_page(kiov[seg].iov_base);
16109cbfeafSKirill A. Shutemov 		get_page(pages[seg]);
16218022c5dSMel Gorman 	}
16318022c5dSMel Gorman 
16418022c5dSMel Gorman 	return seg;
16518022c5dSMel Gorman }
16618022c5dSMel Gorman EXPORT_SYMBOL_GPL(get_kernel_pages);
16718022c5dSMel Gorman 
16818022c5dSMel Gorman /*
16918022c5dSMel Gorman  * get_kernel_page() - pin a kernel page in memory
17018022c5dSMel Gorman  * @start:	starting kernel address
17118022c5dSMel Gorman  * @write:	pinning for read/write, currently ignored
17218022c5dSMel Gorman  * @pages:	array that receives pointer to the page pinned.
17318022c5dSMel Gorman  *		Must be at least nr_segs long.
17418022c5dSMel Gorman  *
17518022c5dSMel Gorman  * Returns 1 if page is pinned. If the page was not pinned, returns
17618022c5dSMel Gorman  * -errno. The page returned must be released with a put_page() call
17718022c5dSMel Gorman  * when it is finished with.
17818022c5dSMel Gorman  */
17918022c5dSMel Gorman int get_kernel_page(unsigned long start, int write, struct page **pages)
18018022c5dSMel Gorman {
18118022c5dSMel Gorman 	const struct kvec kiov = {
18218022c5dSMel Gorman 		.iov_base = (void *)start,
18318022c5dSMel Gorman 		.iov_len = PAGE_SIZE
18418022c5dSMel Gorman 	};
18518022c5dSMel Gorman 
18618022c5dSMel Gorman 	return get_kernel_pages(&kiov, 1, write, pages);
18718022c5dSMel Gorman }
18818022c5dSMel Gorman EXPORT_SYMBOL_GPL(get_kernel_page);
18918022c5dSMel Gorman 
1903dd7ae8eSShaohua Li static void pagevec_lru_move_fn(struct pagevec *pvec,
191fa9add64SHugh Dickins 	void (*move_fn)(struct page *page, struct lruvec *lruvec, void *arg),
1923dd7ae8eSShaohua Li 	void *arg)
193902aaed0SHisashi Hifumi {
194902aaed0SHisashi Hifumi 	int i;
19568eb0731SMel Gorman 	struct pglist_data *pgdat = NULL;
196fa9add64SHugh Dickins 	struct lruvec *lruvec;
1973dd7ae8eSShaohua Li 	unsigned long flags = 0;
198902aaed0SHisashi Hifumi 
199902aaed0SHisashi Hifumi 	for (i = 0; i < pagevec_count(pvec); i++) {
200902aaed0SHisashi Hifumi 		struct page *page = pvec->pages[i];
20168eb0731SMel Gorman 		struct pglist_data *pagepgdat = page_pgdat(page);
202902aaed0SHisashi Hifumi 
20368eb0731SMel Gorman 		if (pagepgdat != pgdat) {
20468eb0731SMel Gorman 			if (pgdat)
20568eb0731SMel Gorman 				spin_unlock_irqrestore(&pgdat->lru_lock, flags);
20668eb0731SMel Gorman 			pgdat = pagepgdat;
20768eb0731SMel Gorman 			spin_lock_irqsave(&pgdat->lru_lock, flags);
208902aaed0SHisashi Hifumi 		}
2093dd7ae8eSShaohua Li 
21068eb0731SMel Gorman 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
211fa9add64SHugh Dickins 		(*move_fn)(page, lruvec, arg);
2123dd7ae8eSShaohua Li 	}
21368eb0731SMel Gorman 	if (pgdat)
21468eb0731SMel Gorman 		spin_unlock_irqrestore(&pgdat->lru_lock, flags);
215c6f92f9fSMel Gorman 	release_pages(pvec->pages, pvec->nr);
2163dd7ae8eSShaohua Li 	pagevec_reinit(pvec);
2173dd7ae8eSShaohua Li }
2183dd7ae8eSShaohua Li 
219fa9add64SHugh Dickins static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec,
220fa9add64SHugh Dickins 				 void *arg)
2213dd7ae8eSShaohua Li {
2223dd7ae8eSShaohua Li 	int *pgmoved = arg;
2233dd7ae8eSShaohua Li 
224c55e8d03SJohannes Weiner 	if (PageLRU(page) && !PageUnevictable(page)) {
225c55e8d03SJohannes Weiner 		del_page_from_lru_list(page, lruvec, page_lru(page));
226c55e8d03SJohannes Weiner 		ClearPageActive(page);
227c55e8d03SJohannes Weiner 		add_page_to_lru_list_tail(page, lruvec, page_lru(page));
2283dd7ae8eSShaohua Li 		(*pgmoved)++;
229902aaed0SHisashi Hifumi 	}
230902aaed0SHisashi Hifumi }
2313dd7ae8eSShaohua Li 
2323dd7ae8eSShaohua Li /*
2333dd7ae8eSShaohua Li  * pagevec_move_tail() must be called with IRQ disabled.
2343dd7ae8eSShaohua Li  * Otherwise this may cause nasty races.
2353dd7ae8eSShaohua Li  */
2363dd7ae8eSShaohua Li static void pagevec_move_tail(struct pagevec *pvec)
2373dd7ae8eSShaohua Li {
2383dd7ae8eSShaohua Li 	int pgmoved = 0;
2393dd7ae8eSShaohua Li 
2403dd7ae8eSShaohua Li 	pagevec_lru_move_fn(pvec, pagevec_move_tail_fn, &pgmoved);
241902aaed0SHisashi Hifumi 	__count_vm_events(PGROTATED, pgmoved);
242902aaed0SHisashi Hifumi }
243902aaed0SHisashi Hifumi 
244902aaed0SHisashi Hifumi /*
2451da177e4SLinus Torvalds  * Writeback is about to end against a page which has been marked for immediate
2461da177e4SLinus Torvalds  * reclaim.  If it still appears to be reclaimable, move it to the tail of the
247902aaed0SHisashi Hifumi  * inactive list.
2481da177e4SLinus Torvalds  */
249ac6aadb2SMiklos Szeredi void rotate_reclaimable_page(struct page *page)
2501da177e4SLinus Torvalds {
251c55e8d03SJohannes Weiner 	if (!PageLocked(page) && !PageDirty(page) &&
252894bc310SLee Schermerhorn 	    !PageUnevictable(page) && PageLRU(page)) {
253902aaed0SHisashi Hifumi 		struct pagevec *pvec;
2541da177e4SLinus Torvalds 		unsigned long flags;
2551da177e4SLinus Torvalds 
25609cbfeafSKirill A. Shutemov 		get_page(page);
257902aaed0SHisashi Hifumi 		local_irq_save(flags);
2587c8e0181SChristoph Lameter 		pvec = this_cpu_ptr(&lru_rotate_pvecs);
2598f182270SLukasz Odzioba 		if (!pagevec_add(pvec, page) || PageCompound(page))
260902aaed0SHisashi Hifumi 			pagevec_move_tail(pvec);
261902aaed0SHisashi Hifumi 		local_irq_restore(flags);
262ac6aadb2SMiklos Szeredi 	}
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds 
265fa9add64SHugh Dickins static void update_page_reclaim_stat(struct lruvec *lruvec,
2663e2f41f1SKOSAKI Motohiro 				     int file, int rotated)
2673e2f41f1SKOSAKI Motohiro {
268fa9add64SHugh Dickins 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
2693e2f41f1SKOSAKI Motohiro 
2703e2f41f1SKOSAKI Motohiro 	reclaim_stat->recent_scanned[file]++;
2713e2f41f1SKOSAKI Motohiro 	if (rotated)
2723e2f41f1SKOSAKI Motohiro 		reclaim_stat->recent_rotated[file]++;
2733e2f41f1SKOSAKI Motohiro }
2743e2f41f1SKOSAKI Motohiro 
275fa9add64SHugh Dickins static void __activate_page(struct page *page, struct lruvec *lruvec,
276fa9add64SHugh Dickins 			    void *arg)
277744ed144SShaohua Li {
2787a608572SLinus Torvalds 	if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
279744ed144SShaohua Li 		int file = page_is_file_cache(page);
280744ed144SShaohua Li 		int lru = page_lru_base_type(page);
281744ed144SShaohua Li 
282fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, lru);
283744ed144SShaohua Li 		SetPageActive(page);
284744ed144SShaohua Li 		lru += LRU_ACTIVE;
285fa9add64SHugh Dickins 		add_page_to_lru_list(page, lruvec, lru);
28624b7e581SMel Gorman 		trace_mm_lru_activate(page);
2877a608572SLinus Torvalds 
288fa9add64SHugh Dickins 		__count_vm_event(PGACTIVATE);
289fa9add64SHugh Dickins 		update_page_reclaim_stat(lruvec, file, 1);
290744ed144SShaohua Li 	}
291eb709b0dSShaohua Li }
292eb709b0dSShaohua Li 
293eb709b0dSShaohua Li #ifdef CONFIG_SMP
294eb709b0dSShaohua Li static void activate_page_drain(int cpu)
295eb709b0dSShaohua Li {
296eb709b0dSShaohua Li 	struct pagevec *pvec = &per_cpu(activate_page_pvecs, cpu);
297eb709b0dSShaohua Li 
298eb709b0dSShaohua Li 	if (pagevec_count(pvec))
299eb709b0dSShaohua Li 		pagevec_lru_move_fn(pvec, __activate_page, NULL);
300eb709b0dSShaohua Li }
301eb709b0dSShaohua Li 
3025fbc4616SChris Metcalf static bool need_activate_page_drain(int cpu)
3035fbc4616SChris Metcalf {
3045fbc4616SChris Metcalf 	return pagevec_count(&per_cpu(activate_page_pvecs, cpu)) != 0;
3055fbc4616SChris Metcalf }
3065fbc4616SChris Metcalf 
307eb709b0dSShaohua Li void activate_page(struct page *page)
308eb709b0dSShaohua Li {
309800d8c63SKirill A. Shutemov 	page = compound_head(page);
310eb709b0dSShaohua Li 	if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
311eb709b0dSShaohua Li 		struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
312eb709b0dSShaohua Li 
31309cbfeafSKirill A. Shutemov 		get_page(page);
3148f182270SLukasz Odzioba 		if (!pagevec_add(pvec, page) || PageCompound(page))
315eb709b0dSShaohua Li 			pagevec_lru_move_fn(pvec, __activate_page, NULL);
316eb709b0dSShaohua Li 		put_cpu_var(activate_page_pvecs);
317eb709b0dSShaohua Li 	}
318eb709b0dSShaohua Li }
319eb709b0dSShaohua Li 
320eb709b0dSShaohua Li #else
321eb709b0dSShaohua Li static inline void activate_page_drain(int cpu)
322eb709b0dSShaohua Li {
323eb709b0dSShaohua Li }
324eb709b0dSShaohua Li 
325eb709b0dSShaohua Li void activate_page(struct page *page)
326eb709b0dSShaohua Li {
327f4b7e272SAndrey Ryabinin 	pg_data_t *pgdat = page_pgdat(page);
328eb709b0dSShaohua Li 
329800d8c63SKirill A. Shutemov 	page = compound_head(page);
330f4b7e272SAndrey Ryabinin 	spin_lock_irq(&pgdat->lru_lock);
331f4b7e272SAndrey Ryabinin 	__activate_page(page, mem_cgroup_page_lruvec(page, pgdat), NULL);
332f4b7e272SAndrey Ryabinin 	spin_unlock_irq(&pgdat->lru_lock);
3331da177e4SLinus Torvalds }
334eb709b0dSShaohua Li #endif
3351da177e4SLinus Torvalds 
336059285a2SMel Gorman static void __lru_cache_activate_page(struct page *page)
337059285a2SMel Gorman {
338059285a2SMel Gorman 	struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
339059285a2SMel Gorman 	int i;
340059285a2SMel Gorman 
341059285a2SMel Gorman 	/*
342059285a2SMel Gorman 	 * Search backwards on the optimistic assumption that the page being
343059285a2SMel Gorman 	 * activated has just been added to this pagevec. Note that only
344059285a2SMel Gorman 	 * the local pagevec is examined as a !PageLRU page could be in the
345059285a2SMel Gorman 	 * process of being released, reclaimed, migrated or on a remote
346059285a2SMel Gorman 	 * pagevec that is currently being drained. Furthermore, marking
347059285a2SMel Gorman 	 * a remote pagevec's page PageActive potentially hits a race where
348059285a2SMel Gorman 	 * a page is marked PageActive just after it is added to the inactive
349059285a2SMel Gorman 	 * list causing accounting errors and BUG_ON checks to trigger.
350059285a2SMel Gorman 	 */
351059285a2SMel Gorman 	for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
352059285a2SMel Gorman 		struct page *pagevec_page = pvec->pages[i];
353059285a2SMel Gorman 
354059285a2SMel Gorman 		if (pagevec_page == page) {
355059285a2SMel Gorman 			SetPageActive(page);
356059285a2SMel Gorman 			break;
357059285a2SMel Gorman 		}
358059285a2SMel Gorman 	}
359059285a2SMel Gorman 
360059285a2SMel Gorman 	put_cpu_var(lru_add_pvec);
361059285a2SMel Gorman }
362059285a2SMel Gorman 
3631da177e4SLinus Torvalds /*
3641da177e4SLinus Torvalds  * Mark a page as having seen activity.
3651da177e4SLinus Torvalds  *
3661da177e4SLinus Torvalds  * inactive,unreferenced	->	inactive,referenced
3671da177e4SLinus Torvalds  * inactive,referenced		->	active,unreferenced
3681da177e4SLinus Torvalds  * active,unreferenced		->	active,referenced
369eb39d618SHugh Dickins  *
370eb39d618SHugh Dickins  * When a newly allocated page is not yet visible, so safe for non-atomic ops,
371eb39d618SHugh Dickins  * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
3721da177e4SLinus Torvalds  */
373920c7a5dSHarvey Harrison void mark_page_accessed(struct page *page)
3741da177e4SLinus Torvalds {
375e90309c9SKirill A. Shutemov 	page = compound_head(page);
376894bc310SLee Schermerhorn 	if (!PageActive(page) && !PageUnevictable(page) &&
377059285a2SMel Gorman 			PageReferenced(page)) {
378059285a2SMel Gorman 
379059285a2SMel Gorman 		/*
380059285a2SMel Gorman 		 * If the page is on the LRU, queue it for activation via
381059285a2SMel Gorman 		 * activate_page_pvecs. Otherwise, assume the page is on a
382059285a2SMel Gorman 		 * pagevec, mark it active and it'll be moved to the active
383059285a2SMel Gorman 		 * LRU on the next drain.
384059285a2SMel Gorman 		 */
385059285a2SMel Gorman 		if (PageLRU(page))
3861da177e4SLinus Torvalds 			activate_page(page);
387059285a2SMel Gorman 		else
388059285a2SMel Gorman 			__lru_cache_activate_page(page);
3891da177e4SLinus Torvalds 		ClearPageReferenced(page);
390a528910eSJohannes Weiner 		if (page_is_file_cache(page))
391a528910eSJohannes Weiner 			workingset_activation(page);
3921da177e4SLinus Torvalds 	} else if (!PageReferenced(page)) {
3931da177e4SLinus Torvalds 		SetPageReferenced(page);
3941da177e4SLinus Torvalds 	}
39533c3fc71SVladimir Davydov 	if (page_is_idle(page))
39633c3fc71SVladimir Davydov 		clear_page_idle(page);
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds EXPORT_SYMBOL(mark_page_accessed);
3991da177e4SLinus Torvalds 
4002329d375SJianyu Zhan static void __lru_cache_add(struct page *page)
4011da177e4SLinus Torvalds {
40213f7f789SMel Gorman 	struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
40313f7f789SMel Gorman 
40409cbfeafSKirill A. Shutemov 	get_page(page);
4058f182270SLukasz Odzioba 	if (!pagevec_add(pvec, page) || PageCompound(page))
406a0b8cab3SMel Gorman 		__pagevec_lru_add(pvec);
40713f7f789SMel Gorman 	put_cpu_var(lru_add_pvec);
4081da177e4SLinus Torvalds }
4092329d375SJianyu Zhan 
4102329d375SJianyu Zhan /**
411e02a9f04SRandy Dunlap  * lru_cache_add_anon - add a page to the page lists
4122329d375SJianyu Zhan  * @page: the page to add
4132329d375SJianyu Zhan  */
4142329d375SJianyu Zhan void lru_cache_add_anon(struct page *page)
4152329d375SJianyu Zhan {
4166fb81a17SMel Gorman 	if (PageActive(page))
4172329d375SJianyu Zhan 		ClearPageActive(page);
4182329d375SJianyu Zhan 	__lru_cache_add(page);
4192329d375SJianyu Zhan }
4202329d375SJianyu Zhan 
4212329d375SJianyu Zhan void lru_cache_add_file(struct page *page)
4222329d375SJianyu Zhan {
4236fb81a17SMel Gorman 	if (PageActive(page))
4242329d375SJianyu Zhan 		ClearPageActive(page);
4252329d375SJianyu Zhan 	__lru_cache_add(page);
4262329d375SJianyu Zhan }
4272329d375SJianyu Zhan EXPORT_SYMBOL(lru_cache_add_file);
4281da177e4SLinus Torvalds 
429f04e9ebbSKOSAKI Motohiro /**
430c53954a0SMel Gorman  * lru_cache_add - add a page to a page list
431f04e9ebbSKOSAKI Motohiro  * @page: the page to be added to the LRU.
4322329d375SJianyu Zhan  *
4332329d375SJianyu Zhan  * Queue the page for addition to the LRU via pagevec. The decision on whether
4342329d375SJianyu Zhan  * to add the page to the [in]active [file|anon] list is deferred until the
4352329d375SJianyu Zhan  * pagevec is drained. This gives a chance for the caller of lru_cache_add()
4362329d375SJianyu Zhan  * have the page added to the active list using mark_page_accessed().
437f04e9ebbSKOSAKI Motohiro  */
438c53954a0SMel Gorman void lru_cache_add(struct page *page)
4391da177e4SLinus Torvalds {
440309381feSSasha Levin 	VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
441309381feSSasha Levin 	VM_BUG_ON_PAGE(PageLRU(page), page);
442c53954a0SMel Gorman 	__lru_cache_add(page);
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds 
445894bc310SLee Schermerhorn /**
44600501b53SJohannes Weiner  * lru_cache_add_active_or_unevictable
44700501b53SJohannes Weiner  * @page:  the page to be added to LRU
44800501b53SJohannes Weiner  * @vma:   vma in which page is mapped for determining reclaimability
44900501b53SJohannes Weiner  *
45000501b53SJohannes Weiner  * Place @page on the active or unevictable LRU list, depending on its
45100501b53SJohannes Weiner  * evictability.  Note that if the page is not evictable, it goes
45200501b53SJohannes Weiner  * directly back onto it's zone's unevictable list, it does NOT use a
45300501b53SJohannes Weiner  * per cpu pagevec.
45400501b53SJohannes Weiner  */
45500501b53SJohannes Weiner void lru_cache_add_active_or_unevictable(struct page *page,
45600501b53SJohannes Weiner 					 struct vm_area_struct *vma)
45700501b53SJohannes Weiner {
45800501b53SJohannes Weiner 	VM_BUG_ON_PAGE(PageLRU(page), page);
45900501b53SJohannes Weiner 
4609c4e6b1aSShakeel Butt 	if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED))
46100501b53SJohannes Weiner 		SetPageActive(page);
4629c4e6b1aSShakeel Butt 	else if (!TestSetPageMlocked(page)) {
46300501b53SJohannes Weiner 		/*
46400501b53SJohannes Weiner 		 * We use the irq-unsafe __mod_zone_page_stat because this
46500501b53SJohannes Weiner 		 * counter is not modified from interrupt context, and the pte
46600501b53SJohannes Weiner 		 * lock is held(spinlock), which implies preemption disabled.
46700501b53SJohannes Weiner 		 */
46800501b53SJohannes Weiner 		__mod_zone_page_state(page_zone(page), NR_MLOCK,
46900501b53SJohannes Weiner 				    hpage_nr_pages(page));
47000501b53SJohannes Weiner 		count_vm_event(UNEVICTABLE_PGMLOCKED);
47100501b53SJohannes Weiner 	}
4729c4e6b1aSShakeel Butt 	lru_cache_add(page);
47300501b53SJohannes Weiner }
47400501b53SJohannes Weiner 
475902aaed0SHisashi Hifumi /*
47631560180SMinchan Kim  * If the page can not be invalidated, it is moved to the
47731560180SMinchan Kim  * inactive list to speed up its reclaim.  It is moved to the
47831560180SMinchan Kim  * head of the list, rather than the tail, to give the flusher
47931560180SMinchan Kim  * threads some time to write it out, as this is much more
48031560180SMinchan Kim  * effective than the single-page writeout from reclaim.
481278df9f4SMinchan Kim  *
482278df9f4SMinchan Kim  * If the page isn't page_mapped and dirty/writeback, the page
483278df9f4SMinchan Kim  * could reclaim asap using PG_reclaim.
484278df9f4SMinchan Kim  *
485278df9f4SMinchan Kim  * 1. active, mapped page -> none
486278df9f4SMinchan Kim  * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
487278df9f4SMinchan Kim  * 3. inactive, mapped page -> none
488278df9f4SMinchan Kim  * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
489278df9f4SMinchan Kim  * 5. inactive, clean -> inactive, tail
490278df9f4SMinchan Kim  * 6. Others -> none
491278df9f4SMinchan Kim  *
492278df9f4SMinchan Kim  * In 4, why it moves inactive's head, the VM expects the page would
493278df9f4SMinchan Kim  * be write it out by flusher threads as this is much more effective
494278df9f4SMinchan Kim  * than the single-page writeout from reclaim.
49531560180SMinchan Kim  */
496cc5993bdSMinchan Kim static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
497fa9add64SHugh Dickins 			      void *arg)
49831560180SMinchan Kim {
49931560180SMinchan Kim 	int lru, file;
500278df9f4SMinchan Kim 	bool active;
50131560180SMinchan Kim 
502278df9f4SMinchan Kim 	if (!PageLRU(page))
50331560180SMinchan Kim 		return;
50431560180SMinchan Kim 
505bad49d9cSMinchan Kim 	if (PageUnevictable(page))
506bad49d9cSMinchan Kim 		return;
507bad49d9cSMinchan Kim 
50831560180SMinchan Kim 	/* Some processes are using the page */
50931560180SMinchan Kim 	if (page_mapped(page))
51031560180SMinchan Kim 		return;
51131560180SMinchan Kim 
512278df9f4SMinchan Kim 	active = PageActive(page);
51331560180SMinchan Kim 	file = page_is_file_cache(page);
51431560180SMinchan Kim 	lru = page_lru_base_type(page);
515fa9add64SHugh Dickins 
516fa9add64SHugh Dickins 	del_page_from_lru_list(page, lruvec, lru + active);
51731560180SMinchan Kim 	ClearPageActive(page);
51831560180SMinchan Kim 	ClearPageReferenced(page);
51931560180SMinchan Kim 
520278df9f4SMinchan Kim 	if (PageWriteback(page) || PageDirty(page)) {
521278df9f4SMinchan Kim 		/*
522278df9f4SMinchan Kim 		 * PG_reclaim could be raced with end_page_writeback
523278df9f4SMinchan Kim 		 * It can make readahead confusing.  But race window
524278df9f4SMinchan Kim 		 * is _really_ small and  it's non-critical problem.
525278df9f4SMinchan Kim 		 */
526e7a1aaf2SYu Zhao 		add_page_to_lru_list(page, lruvec, lru);
527278df9f4SMinchan Kim 		SetPageReclaim(page);
528278df9f4SMinchan Kim 	} else {
529278df9f4SMinchan Kim 		/*
530278df9f4SMinchan Kim 		 * The page's writeback ends up during pagevec
531278df9f4SMinchan Kim 		 * We moves tha page into tail of inactive.
532278df9f4SMinchan Kim 		 */
533e7a1aaf2SYu Zhao 		add_page_to_lru_list_tail(page, lruvec, lru);
534278df9f4SMinchan Kim 		__count_vm_event(PGROTATED);
535278df9f4SMinchan Kim 	}
536278df9f4SMinchan Kim 
537278df9f4SMinchan Kim 	if (active)
538278df9f4SMinchan Kim 		__count_vm_event(PGDEACTIVATE);
539fa9add64SHugh Dickins 	update_page_reclaim_stat(lruvec, file, 0);
54031560180SMinchan Kim }
54131560180SMinchan Kim 
542*9c276cc6SMinchan Kim static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec,
543*9c276cc6SMinchan Kim 			    void *arg)
544*9c276cc6SMinchan Kim {
545*9c276cc6SMinchan Kim 	if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
546*9c276cc6SMinchan Kim 		int file = page_is_file_cache(page);
547*9c276cc6SMinchan Kim 		int lru = page_lru_base_type(page);
548*9c276cc6SMinchan Kim 
549*9c276cc6SMinchan Kim 		del_page_from_lru_list(page, lruvec, lru + LRU_ACTIVE);
550*9c276cc6SMinchan Kim 		ClearPageActive(page);
551*9c276cc6SMinchan Kim 		ClearPageReferenced(page);
552*9c276cc6SMinchan Kim 		add_page_to_lru_list(page, lruvec, lru);
553*9c276cc6SMinchan Kim 
554*9c276cc6SMinchan Kim 		__count_vm_events(PGDEACTIVATE, hpage_nr_pages(page));
555*9c276cc6SMinchan Kim 		update_page_reclaim_stat(lruvec, file, 0);
556*9c276cc6SMinchan Kim 	}
557*9c276cc6SMinchan Kim }
55810853a03SMinchan Kim 
559f7ad2a6cSShaohua Li static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec,
56010853a03SMinchan Kim 			    void *arg)
56110853a03SMinchan Kim {
562f7ad2a6cSShaohua Li 	if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
56324c92eb7SShaohua Li 	    !PageSwapCache(page) && !PageUnevictable(page)) {
564f7ad2a6cSShaohua Li 		bool active = PageActive(page);
56510853a03SMinchan Kim 
566f7ad2a6cSShaohua Li 		del_page_from_lru_list(page, lruvec,
567f7ad2a6cSShaohua Li 				       LRU_INACTIVE_ANON + active);
56810853a03SMinchan Kim 		ClearPageActive(page);
56910853a03SMinchan Kim 		ClearPageReferenced(page);
570f7ad2a6cSShaohua Li 		/*
571f7ad2a6cSShaohua Li 		 * lazyfree pages are clean anonymous pages. They have
572f7ad2a6cSShaohua Li 		 * SwapBacked flag cleared to distinguish normal anonymous
573f7ad2a6cSShaohua Li 		 * pages
574f7ad2a6cSShaohua Li 		 */
575f7ad2a6cSShaohua Li 		ClearPageSwapBacked(page);
576f7ad2a6cSShaohua Li 		add_page_to_lru_list(page, lruvec, LRU_INACTIVE_FILE);
57710853a03SMinchan Kim 
578f7ad2a6cSShaohua Li 		__count_vm_events(PGLAZYFREE, hpage_nr_pages(page));
5792262185cSRoman Gushchin 		count_memcg_page_event(page, PGLAZYFREE);
580f7ad2a6cSShaohua Li 		update_page_reclaim_stat(lruvec, 1, 0);
58110853a03SMinchan Kim 	}
58210853a03SMinchan Kim }
58310853a03SMinchan Kim 
58431560180SMinchan Kim /*
585902aaed0SHisashi Hifumi  * Drain pages out of the cpu's pagevecs.
586902aaed0SHisashi Hifumi  * Either "cpu" is the current CPU, and preemption has already been
587902aaed0SHisashi Hifumi  * disabled; or "cpu" is being hot-unplugged, and is already dead.
588902aaed0SHisashi Hifumi  */
589f0cb3c76SKonstantin Khlebnikov void lru_add_drain_cpu(int cpu)
5901da177e4SLinus Torvalds {
59113f7f789SMel Gorman 	struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	if (pagevec_count(pvec))
594a0b8cab3SMel Gorman 		__pagevec_lru_add(pvec);
595902aaed0SHisashi Hifumi 
596902aaed0SHisashi Hifumi 	pvec = &per_cpu(lru_rotate_pvecs, cpu);
597902aaed0SHisashi Hifumi 	if (pagevec_count(pvec)) {
598902aaed0SHisashi Hifumi 		unsigned long flags;
599902aaed0SHisashi Hifumi 
600902aaed0SHisashi Hifumi 		/* No harm done if a racing interrupt already did this */
601902aaed0SHisashi Hifumi 		local_irq_save(flags);
602902aaed0SHisashi Hifumi 		pagevec_move_tail(pvec);
603902aaed0SHisashi Hifumi 		local_irq_restore(flags);
604902aaed0SHisashi Hifumi 	}
60531560180SMinchan Kim 
606cc5993bdSMinchan Kim 	pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
60731560180SMinchan Kim 	if (pagevec_count(pvec))
608cc5993bdSMinchan Kim 		pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
609eb709b0dSShaohua Li 
610*9c276cc6SMinchan Kim 	pvec = &per_cpu(lru_deactivate_pvecs, cpu);
611*9c276cc6SMinchan Kim 	if (pagevec_count(pvec))
612*9c276cc6SMinchan Kim 		pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
613*9c276cc6SMinchan Kim 
614f7ad2a6cSShaohua Li 	pvec = &per_cpu(lru_lazyfree_pvecs, cpu);
61510853a03SMinchan Kim 	if (pagevec_count(pvec))
616f7ad2a6cSShaohua Li 		pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
61710853a03SMinchan Kim 
618eb709b0dSShaohua Li 	activate_page_drain(cpu);
61931560180SMinchan Kim }
62031560180SMinchan Kim 
62131560180SMinchan Kim /**
622cc5993bdSMinchan Kim  * deactivate_file_page - forcefully deactivate a file page
62331560180SMinchan Kim  * @page: page to deactivate
62431560180SMinchan Kim  *
62531560180SMinchan Kim  * This function hints the VM that @page is a good reclaim candidate,
62631560180SMinchan Kim  * for example if its invalidation fails due to the page being dirty
62731560180SMinchan Kim  * or under writeback.
62831560180SMinchan Kim  */
629cc5993bdSMinchan Kim void deactivate_file_page(struct page *page)
63031560180SMinchan Kim {
631821ed6bbSMinchan Kim 	/*
632cc5993bdSMinchan Kim 	 * In a workload with many unevictable page such as mprotect,
633cc5993bdSMinchan Kim 	 * unevictable page deactivation for accelerating reclaim is pointless.
634821ed6bbSMinchan Kim 	 */
635821ed6bbSMinchan Kim 	if (PageUnevictable(page))
636821ed6bbSMinchan Kim 		return;
637821ed6bbSMinchan Kim 
63831560180SMinchan Kim 	if (likely(get_page_unless_zero(page))) {
639cc5993bdSMinchan Kim 		struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
64031560180SMinchan Kim 
6418f182270SLukasz Odzioba 		if (!pagevec_add(pvec, page) || PageCompound(page))
642cc5993bdSMinchan Kim 			pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
643cc5993bdSMinchan Kim 		put_cpu_var(lru_deactivate_file_pvecs);
64431560180SMinchan Kim 	}
64580bfed90SAndrew Morton }
64680bfed90SAndrew Morton 
647*9c276cc6SMinchan Kim /*
648*9c276cc6SMinchan Kim  * deactivate_page - deactivate a page
649*9c276cc6SMinchan Kim  * @page: page to deactivate
650*9c276cc6SMinchan Kim  *
651*9c276cc6SMinchan Kim  * deactivate_page() moves @page to the inactive list if @page was on the active
652*9c276cc6SMinchan Kim  * list and was not an unevictable page.  This is done to accelerate the reclaim
653*9c276cc6SMinchan Kim  * of @page.
654*9c276cc6SMinchan Kim  */
655*9c276cc6SMinchan Kim void deactivate_page(struct page *page)
656*9c276cc6SMinchan Kim {
657*9c276cc6SMinchan Kim 	if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
658*9c276cc6SMinchan Kim 		struct pagevec *pvec = &get_cpu_var(lru_deactivate_pvecs);
659*9c276cc6SMinchan Kim 
660*9c276cc6SMinchan Kim 		get_page(page);
661*9c276cc6SMinchan Kim 		if (!pagevec_add(pvec, page) || PageCompound(page))
662*9c276cc6SMinchan Kim 			pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
663*9c276cc6SMinchan Kim 		put_cpu_var(lru_deactivate_pvecs);
664*9c276cc6SMinchan Kim 	}
665*9c276cc6SMinchan Kim }
666*9c276cc6SMinchan Kim 
66710853a03SMinchan Kim /**
668f7ad2a6cSShaohua Li  * mark_page_lazyfree - make an anon page lazyfree
66910853a03SMinchan Kim  * @page: page to deactivate
67010853a03SMinchan Kim  *
671f7ad2a6cSShaohua Li  * mark_page_lazyfree() moves @page to the inactive file list.
672f7ad2a6cSShaohua Li  * This is done to accelerate the reclaim of @page.
67310853a03SMinchan Kim  */
674f7ad2a6cSShaohua Li void mark_page_lazyfree(struct page *page)
67510853a03SMinchan Kim {
676f7ad2a6cSShaohua Li 	if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
67724c92eb7SShaohua Li 	    !PageSwapCache(page) && !PageUnevictable(page)) {
678f7ad2a6cSShaohua Li 		struct pagevec *pvec = &get_cpu_var(lru_lazyfree_pvecs);
67910853a03SMinchan Kim 
68009cbfeafSKirill A. Shutemov 		get_page(page);
6818f182270SLukasz Odzioba 		if (!pagevec_add(pvec, page) || PageCompound(page))
682f7ad2a6cSShaohua Li 			pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
683f7ad2a6cSShaohua Li 		put_cpu_var(lru_lazyfree_pvecs);
68410853a03SMinchan Kim 	}
68510853a03SMinchan Kim }
68610853a03SMinchan Kim 
68780bfed90SAndrew Morton void lru_add_drain(void)
68880bfed90SAndrew Morton {
689f0cb3c76SKonstantin Khlebnikov 	lru_add_drain_cpu(get_cpu());
69080bfed90SAndrew Morton 	put_cpu();
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
6936ea183d6SMichal Hocko #ifdef CONFIG_SMP
6946ea183d6SMichal Hocko 
6956ea183d6SMichal Hocko static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
6966ea183d6SMichal Hocko 
697c4028958SDavid Howells static void lru_add_drain_per_cpu(struct work_struct *dummy)
698053837fcSNick Piggin {
699053837fcSNick Piggin 	lru_add_drain();
700053837fcSNick Piggin }
701053837fcSNick Piggin 
7029852a721SMichal Hocko /*
7039852a721SMichal Hocko  * Doesn't need any cpu hotplug locking because we do rely on per-cpu
7049852a721SMichal Hocko  * kworkers being shut down before our page_alloc_cpu_dead callback is
7059852a721SMichal Hocko  * executed on the offlined cpu.
7069852a721SMichal Hocko  * Calling this function with cpu hotplug locks held can actually lead
7079852a721SMichal Hocko  * to obscure indirect dependencies via WQ context.
7089852a721SMichal Hocko  */
7099852a721SMichal Hocko void lru_add_drain_all(void)
710053837fcSNick Piggin {
7115fbc4616SChris Metcalf 	static DEFINE_MUTEX(lock);
7125fbc4616SChris Metcalf 	static struct cpumask has_work;
7135fbc4616SChris Metcalf 	int cpu;
7145fbc4616SChris Metcalf 
715ce612879SMichal Hocko 	/*
716ce612879SMichal Hocko 	 * Make sure nobody triggers this path before mm_percpu_wq is fully
717ce612879SMichal Hocko 	 * initialized.
718ce612879SMichal Hocko 	 */
719ce612879SMichal Hocko 	if (WARN_ON(!mm_percpu_wq))
720ce612879SMichal Hocko 		return;
721ce612879SMichal Hocko 
7225fbc4616SChris Metcalf 	mutex_lock(&lock);
7235fbc4616SChris Metcalf 	cpumask_clear(&has_work);
7245fbc4616SChris Metcalf 
7255fbc4616SChris Metcalf 	for_each_online_cpu(cpu) {
7265fbc4616SChris Metcalf 		struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
7275fbc4616SChris Metcalf 
7285fbc4616SChris Metcalf 		if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
7295fbc4616SChris Metcalf 		    pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
730cc5993bdSMinchan Kim 		    pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
731*9c276cc6SMinchan Kim 		    pagevec_count(&per_cpu(lru_deactivate_pvecs, cpu)) ||
732f7ad2a6cSShaohua Li 		    pagevec_count(&per_cpu(lru_lazyfree_pvecs, cpu)) ||
7335fbc4616SChris Metcalf 		    need_activate_page_drain(cpu)) {
7345fbc4616SChris Metcalf 			INIT_WORK(work, lru_add_drain_per_cpu);
735ce612879SMichal Hocko 			queue_work_on(cpu, mm_percpu_wq, work);
7365fbc4616SChris Metcalf 			cpumask_set_cpu(cpu, &has_work);
7375fbc4616SChris Metcalf 		}
7385fbc4616SChris Metcalf 	}
7395fbc4616SChris Metcalf 
7405fbc4616SChris Metcalf 	for_each_cpu(cpu, &has_work)
7415fbc4616SChris Metcalf 		flush_work(&per_cpu(lru_add_drain_work, cpu));
7425fbc4616SChris Metcalf 
7435fbc4616SChris Metcalf 	mutex_unlock(&lock);
744053837fcSNick Piggin }
7456ea183d6SMichal Hocko #else
7466ea183d6SMichal Hocko void lru_add_drain_all(void)
7476ea183d6SMichal Hocko {
7486ea183d6SMichal Hocko 	lru_add_drain();
7496ea183d6SMichal Hocko }
7506ea183d6SMichal Hocko #endif
751053837fcSNick Piggin 
752aabfb572SMichal Hocko /**
753ea1754a0SKirill A. Shutemov  * release_pages - batched put_page()
754aabfb572SMichal Hocko  * @pages: array of pages to release
755aabfb572SMichal Hocko  * @nr: number of pages
7561da177e4SLinus Torvalds  *
757aabfb572SMichal Hocko  * Decrement the reference count on all the pages in @pages.  If it
758aabfb572SMichal Hocko  * fell to zero, remove the page from the LRU and free it.
7591da177e4SLinus Torvalds  */
760c6f92f9fSMel Gorman void release_pages(struct page **pages, int nr)
7611da177e4SLinus Torvalds {
7621da177e4SLinus Torvalds 	int i;
763cc59850eSKonstantin Khlebnikov 	LIST_HEAD(pages_to_free);
764599d0c95SMel Gorman 	struct pglist_data *locked_pgdat = NULL;
765fa9add64SHugh Dickins 	struct lruvec *lruvec;
766902aaed0SHisashi Hifumi 	unsigned long uninitialized_var(flags);
767aabfb572SMichal Hocko 	unsigned int uninitialized_var(lock_batch);
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	for (i = 0; i < nr; i++) {
7701da177e4SLinus Torvalds 		struct page *page = pages[i];
7711da177e4SLinus Torvalds 
772aabfb572SMichal Hocko 		/*
773aabfb572SMichal Hocko 		 * Make sure the IRQ-safe lock-holding time does not get
774aabfb572SMichal Hocko 		 * excessive with a continuous string of pages from the
775599d0c95SMel Gorman 		 * same pgdat. The lock is held only if pgdat != NULL.
776aabfb572SMichal Hocko 		 */
777599d0c95SMel Gorman 		if (locked_pgdat && ++lock_batch == SWAP_CLUSTER_MAX) {
778599d0c95SMel Gorman 			spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
779599d0c95SMel Gorman 			locked_pgdat = NULL;
780aabfb572SMichal Hocko 		}
781aabfb572SMichal Hocko 
7826fcb52a5SAaron Lu 		if (is_huge_zero_page(page))
783aa88b68cSKirill A. Shutemov 			continue;
784aa88b68cSKirill A. Shutemov 
785c5d6c45eSIra Weiny 		if (is_zone_device_page(page)) {
786df6ad698SJérôme Glisse 			if (locked_pgdat) {
787df6ad698SJérôme Glisse 				spin_unlock_irqrestore(&locked_pgdat->lru_lock,
788df6ad698SJérôme Glisse 						       flags);
789df6ad698SJérôme Glisse 				locked_pgdat = NULL;
790df6ad698SJérôme Glisse 			}
791c5d6c45eSIra Weiny 			/*
792c5d6c45eSIra Weiny 			 * ZONE_DEVICE pages that return 'false' from
793c5d6c45eSIra Weiny 			 * put_devmap_managed_page() do not require special
794c5d6c45eSIra Weiny 			 * processing, and instead, expect a call to
795c5d6c45eSIra Weiny 			 * put_page_testzero().
796c5d6c45eSIra Weiny 			 */
797c5d6c45eSIra Weiny 			if (put_devmap_managed_page(page))
798df6ad698SJérôme Glisse 				continue;
799df6ad698SJérôme Glisse 		}
800df6ad698SJérôme Glisse 
801ddc58f27SKirill A. Shutemov 		page = compound_head(page);
802b5810039SNick Piggin 		if (!put_page_testzero(page))
8031da177e4SLinus Torvalds 			continue;
8041da177e4SLinus Torvalds 
805ddc58f27SKirill A. Shutemov 		if (PageCompound(page)) {
806599d0c95SMel Gorman 			if (locked_pgdat) {
807599d0c95SMel Gorman 				spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
808599d0c95SMel Gorman 				locked_pgdat = NULL;
809ddc58f27SKirill A. Shutemov 			}
810ddc58f27SKirill A. Shutemov 			__put_compound_page(page);
811ddc58f27SKirill A. Shutemov 			continue;
812ddc58f27SKirill A. Shutemov 		}
813ddc58f27SKirill A. Shutemov 
81446453a6eSNick Piggin 		if (PageLRU(page)) {
815599d0c95SMel Gorman 			struct pglist_data *pgdat = page_pgdat(page);
816894bc310SLee Schermerhorn 
817599d0c95SMel Gorman 			if (pgdat != locked_pgdat) {
818599d0c95SMel Gorman 				if (locked_pgdat)
819599d0c95SMel Gorman 					spin_unlock_irqrestore(&locked_pgdat->lru_lock,
820902aaed0SHisashi Hifumi 									flags);
821aabfb572SMichal Hocko 				lock_batch = 0;
822599d0c95SMel Gorman 				locked_pgdat = pgdat;
823599d0c95SMel Gorman 				spin_lock_irqsave(&locked_pgdat->lru_lock, flags);
8241da177e4SLinus Torvalds 			}
825fa9add64SHugh Dickins 
826599d0c95SMel Gorman 			lruvec = mem_cgroup_page_lruvec(page, locked_pgdat);
827309381feSSasha Levin 			VM_BUG_ON_PAGE(!PageLRU(page), page);
82867453911SNick Piggin 			__ClearPageLRU(page);
829fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, page_off_lru(page));
83046453a6eSNick Piggin 		}
83146453a6eSNick Piggin 
832c53954a0SMel Gorman 		/* Clear Active bit in case of parallel mark_page_accessed */
833e3741b50SMel Gorman 		__ClearPageActive(page);
83462906027SNicholas Piggin 		__ClearPageWaiters(page);
835c53954a0SMel Gorman 
836cc59850eSKonstantin Khlebnikov 		list_add(&page->lru, &pages_to_free);
8371da177e4SLinus Torvalds 	}
838599d0c95SMel Gorman 	if (locked_pgdat)
839599d0c95SMel Gorman 		spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
8401da177e4SLinus Torvalds 
841747db954SJohannes Weiner 	mem_cgroup_uncharge_list(&pages_to_free);
8422d4894b5SMel Gorman 	free_unref_page_list(&pages_to_free);
8431da177e4SLinus Torvalds }
8440be8557bSMiklos Szeredi EXPORT_SYMBOL(release_pages);
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds /*
8471da177e4SLinus Torvalds  * The pages which we're about to release may be in the deferred lru-addition
8481da177e4SLinus Torvalds  * queues.  That would prevent them from really being freed right now.  That's
8491da177e4SLinus Torvalds  * OK from a correctness point of view but is inefficient - those pages may be
8501da177e4SLinus Torvalds  * cache-warm and we want to give them back to the page allocator ASAP.
8511da177e4SLinus Torvalds  *
8521da177e4SLinus Torvalds  * So __pagevec_release() will drain those queues here.  __pagevec_lru_add()
8531da177e4SLinus Torvalds  * and __pagevec_lru_add_active() call release_pages() directly to avoid
8541da177e4SLinus Torvalds  * mutual recursion.
8551da177e4SLinus Torvalds  */
8561da177e4SLinus Torvalds void __pagevec_release(struct pagevec *pvec)
8571da177e4SLinus Torvalds {
8587f0b5fb9SMel Gorman 	if (!pvec->percpu_pvec_drained) {
8591da177e4SLinus Torvalds 		lru_add_drain();
8607f0b5fb9SMel Gorman 		pvec->percpu_pvec_drained = true;
861d9ed0d08SMel Gorman 	}
862c6f92f9fSMel Gorman 	release_pages(pvec->pages, pagevec_count(pvec));
8631da177e4SLinus Torvalds 	pagevec_reinit(pvec);
8641da177e4SLinus Torvalds }
8657f285701SSteve French EXPORT_SYMBOL(__pagevec_release);
8667f285701SSteve French 
86712d27107SHugh Dickins #ifdef CONFIG_TRANSPARENT_HUGEPAGE
86871e3aac0SAndrea Arcangeli /* used by __split_huge_page_refcount() */
869fa9add64SHugh Dickins void lru_add_page_tail(struct page *page, struct page *page_tail,
8705bc7b8acSShaohua Li 		       struct lruvec *lruvec, struct list_head *list)
87171e3aac0SAndrea Arcangeli {
87271e3aac0SAndrea Arcangeli 	const int file = 0;
87371e3aac0SAndrea Arcangeli 
874309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageHead(page), page);
875309381feSSasha Levin 	VM_BUG_ON_PAGE(PageCompound(page_tail), page);
876309381feSSasha Levin 	VM_BUG_ON_PAGE(PageLRU(page_tail), page);
87735f3aa39SLance Roy 	lockdep_assert_held(&lruvec_pgdat(lruvec)->lru_lock);
87871e3aac0SAndrea Arcangeli 
8795bc7b8acSShaohua Li 	if (!list)
88071e3aac0SAndrea Arcangeli 		SetPageLRU(page_tail);
88171e3aac0SAndrea Arcangeli 
88212d27107SHugh Dickins 	if (likely(PageLRU(page)))
88312d27107SHugh Dickins 		list_add_tail(&page_tail->lru, &page->lru);
8845bc7b8acSShaohua Li 	else if (list) {
8855bc7b8acSShaohua Li 		/* page reclaim is reclaiming a huge page */
8865bc7b8acSShaohua Li 		get_page(page_tail);
8875bc7b8acSShaohua Li 		list_add_tail(&page_tail->lru, list);
8885bc7b8acSShaohua Li 	} else {
88912d27107SHugh Dickins 		/*
89012d27107SHugh Dickins 		 * Head page has not yet been counted, as an hpage,
89112d27107SHugh Dickins 		 * so we must account for each subpage individually.
89212d27107SHugh Dickins 		 *
893e7a1aaf2SYu Zhao 		 * Put page_tail on the list at the correct position
894e7a1aaf2SYu Zhao 		 * so they all end up in order.
89512d27107SHugh Dickins 		 */
896e7a1aaf2SYu Zhao 		add_page_to_lru_list_tail(page_tail, lruvec,
897e7a1aaf2SYu Zhao 					  page_lru(page_tail));
89871e3aac0SAndrea Arcangeli 	}
8997512102cSHugh Dickins 
9007512102cSHugh Dickins 	if (!PageUnevictable(page))
901e180cf80SKirill A. Shutemov 		update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
90271e3aac0SAndrea Arcangeli }
90312d27107SHugh Dickins #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
90471e3aac0SAndrea Arcangeli 
905fa9add64SHugh Dickins static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
906fa9add64SHugh Dickins 				 void *arg)
9073dd7ae8eSShaohua Li {
9089c4e6b1aSShakeel Butt 	enum lru_list lru;
9099c4e6b1aSShakeel Butt 	int was_unevictable = TestClearPageUnevictable(page);
9103dd7ae8eSShaohua Li 
911309381feSSasha Levin 	VM_BUG_ON_PAGE(PageLRU(page), page);
9123dd7ae8eSShaohua Li 
9133dd7ae8eSShaohua Li 	SetPageLRU(page);
9149c4e6b1aSShakeel Butt 	/*
9159c4e6b1aSShakeel Butt 	 * Page becomes evictable in two ways:
916dae966dcSPeng Fan 	 * 1) Within LRU lock [munlock_vma_page() and __munlock_pagevec()].
9179c4e6b1aSShakeel Butt 	 * 2) Before acquiring LRU lock to put the page to correct LRU and then
9189c4e6b1aSShakeel Butt 	 *   a) do PageLRU check with lock [check_move_unevictable_pages]
9199c4e6b1aSShakeel Butt 	 *   b) do PageLRU check before lock [clear_page_mlock]
9209c4e6b1aSShakeel Butt 	 *
9219c4e6b1aSShakeel Butt 	 * (1) & (2a) are ok as LRU lock will serialize them. For (2b), we need
9229c4e6b1aSShakeel Butt 	 * following strict ordering:
9239c4e6b1aSShakeel Butt 	 *
9249c4e6b1aSShakeel Butt 	 * #0: __pagevec_lru_add_fn		#1: clear_page_mlock
9259c4e6b1aSShakeel Butt 	 *
9269c4e6b1aSShakeel Butt 	 * SetPageLRU()				TestClearPageMlocked()
9279c4e6b1aSShakeel Butt 	 * smp_mb() // explicit ordering	// above provides strict
9289c4e6b1aSShakeel Butt 	 *					// ordering
9299c4e6b1aSShakeel Butt 	 * PageMlocked()			PageLRU()
9309c4e6b1aSShakeel Butt 	 *
9319c4e6b1aSShakeel Butt 	 *
9329c4e6b1aSShakeel Butt 	 * if '#1' does not observe setting of PG_lru by '#0' and fails
9339c4e6b1aSShakeel Butt 	 * isolation, the explicit barrier will make sure that page_evictable
9349c4e6b1aSShakeel Butt 	 * check will put the page in correct LRU. Without smp_mb(), SetPageLRU
9359c4e6b1aSShakeel Butt 	 * can be reordered after PageMlocked check and can make '#1' to fail
9369c4e6b1aSShakeel Butt 	 * the isolation of the page whose Mlocked bit is cleared (#0 is also
9379c4e6b1aSShakeel Butt 	 * looking at the same page) and the evictable page will be stranded
9389c4e6b1aSShakeel Butt 	 * in an unevictable LRU.
9399c4e6b1aSShakeel Butt 	 */
9409c4e6b1aSShakeel Butt 	smp_mb();
9419c4e6b1aSShakeel Butt 
9429c4e6b1aSShakeel Butt 	if (page_evictable(page)) {
9439c4e6b1aSShakeel Butt 		lru = page_lru(page);
9449c4e6b1aSShakeel Butt 		update_page_reclaim_stat(lruvec, page_is_file_cache(page),
9459c4e6b1aSShakeel Butt 					 PageActive(page));
9469c4e6b1aSShakeel Butt 		if (was_unevictable)
9479c4e6b1aSShakeel Butt 			count_vm_event(UNEVICTABLE_PGRESCUED);
9489c4e6b1aSShakeel Butt 	} else {
9499c4e6b1aSShakeel Butt 		lru = LRU_UNEVICTABLE;
9509c4e6b1aSShakeel Butt 		ClearPageActive(page);
9519c4e6b1aSShakeel Butt 		SetPageUnevictable(page);
9529c4e6b1aSShakeel Butt 		if (!was_unevictable)
9539c4e6b1aSShakeel Butt 			count_vm_event(UNEVICTABLE_PGCULLED);
9549c4e6b1aSShakeel Butt 	}
9559c4e6b1aSShakeel Butt 
956fa9add64SHugh Dickins 	add_page_to_lru_list(page, lruvec, lru);
95724b7e581SMel Gorman 	trace_mm_lru_insertion(page, lru);
9583dd7ae8eSShaohua Li }
9593dd7ae8eSShaohua Li 
9601da177e4SLinus Torvalds /*
9611da177e4SLinus Torvalds  * Add the passed pages to the LRU, then drop the caller's refcount
9621da177e4SLinus Torvalds  * on them.  Reinitialises the caller's pagevec.
9631da177e4SLinus Torvalds  */
964a0b8cab3SMel Gorman void __pagevec_lru_add(struct pagevec *pvec)
9651da177e4SLinus Torvalds {
966a0b8cab3SMel Gorman 	pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
9671da177e4SLinus Torvalds }
9685095ae83SHugh Dickins EXPORT_SYMBOL(__pagevec_lru_add);
969f04e9ebbSKOSAKI Motohiro 
9701da177e4SLinus Torvalds /**
9710cd6144aSJohannes Weiner  * pagevec_lookup_entries - gang pagecache lookup
9720cd6144aSJohannes Weiner  * @pvec:	Where the resulting entries are placed
9730cd6144aSJohannes Weiner  * @mapping:	The address_space to search
9740cd6144aSJohannes Weiner  * @start:	The starting entry index
975cb6f0f34SMike Rapoport  * @nr_entries:	The maximum number of pages
9760cd6144aSJohannes Weiner  * @indices:	The cache indices corresponding to the entries in @pvec
9770cd6144aSJohannes Weiner  *
9780cd6144aSJohannes Weiner  * pagevec_lookup_entries() will search for and return a group of up
979f144c390SMike Rapoport  * to @nr_pages pages and shadow entries in the mapping.  All
9800cd6144aSJohannes Weiner  * entries are placed in @pvec.  pagevec_lookup_entries() takes a
9810cd6144aSJohannes Weiner  * reference against actual pages in @pvec.
9820cd6144aSJohannes Weiner  *
9830cd6144aSJohannes Weiner  * The search returns a group of mapping-contiguous entries with
9840cd6144aSJohannes Weiner  * ascending indexes.  There may be holes in the indices due to
9850cd6144aSJohannes Weiner  * not-present entries.
9860cd6144aSJohannes Weiner  *
9870cd6144aSJohannes Weiner  * pagevec_lookup_entries() returns the number of entries which were
9880cd6144aSJohannes Weiner  * found.
9890cd6144aSJohannes Weiner  */
9900cd6144aSJohannes Weiner unsigned pagevec_lookup_entries(struct pagevec *pvec,
9910cd6144aSJohannes Weiner 				struct address_space *mapping,
992e02a9f04SRandy Dunlap 				pgoff_t start, unsigned nr_entries,
9930cd6144aSJohannes Weiner 				pgoff_t *indices)
9940cd6144aSJohannes Weiner {
995e02a9f04SRandy Dunlap 	pvec->nr = find_get_entries(mapping, start, nr_entries,
9960cd6144aSJohannes Weiner 				    pvec->pages, indices);
9970cd6144aSJohannes Weiner 	return pagevec_count(pvec);
9980cd6144aSJohannes Weiner }
9990cd6144aSJohannes Weiner 
10000cd6144aSJohannes Weiner /**
10010cd6144aSJohannes Weiner  * pagevec_remove_exceptionals - pagevec exceptionals pruning
10020cd6144aSJohannes Weiner  * @pvec:	The pagevec to prune
10030cd6144aSJohannes Weiner  *
10040cd6144aSJohannes Weiner  * pagevec_lookup_entries() fills both pages and exceptional radix
10050cd6144aSJohannes Weiner  * tree entries into the pagevec.  This function prunes all
10060cd6144aSJohannes Weiner  * exceptionals from @pvec without leaving holes, so that it can be
10070cd6144aSJohannes Weiner  * passed on to page-only pagevec operations.
10080cd6144aSJohannes Weiner  */
10090cd6144aSJohannes Weiner void pagevec_remove_exceptionals(struct pagevec *pvec)
10100cd6144aSJohannes Weiner {
10110cd6144aSJohannes Weiner 	int i, j;
10120cd6144aSJohannes Weiner 
10130cd6144aSJohannes Weiner 	for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
10140cd6144aSJohannes Weiner 		struct page *page = pvec->pages[i];
10153159f943SMatthew Wilcox 		if (!xa_is_value(page))
10160cd6144aSJohannes Weiner 			pvec->pages[j++] = page;
10170cd6144aSJohannes Weiner 	}
10180cd6144aSJohannes Weiner 	pvec->nr = j;
10190cd6144aSJohannes Weiner }
10200cd6144aSJohannes Weiner 
10210cd6144aSJohannes Weiner /**
1022b947cee4SJan Kara  * pagevec_lookup_range - gang pagecache lookup
10231da177e4SLinus Torvalds  * @pvec:	Where the resulting pages are placed
10241da177e4SLinus Torvalds  * @mapping:	The address_space to search
10251da177e4SLinus Torvalds  * @start:	The starting page index
1026b947cee4SJan Kara  * @end:	The final page index
10271da177e4SLinus Torvalds  *
1028e02a9f04SRandy Dunlap  * pagevec_lookup_range() will search for & return a group of up to PAGEVEC_SIZE
1029b947cee4SJan Kara  * pages in the mapping starting from index @start and upto index @end
1030b947cee4SJan Kara  * (inclusive).  The pages are placed in @pvec.  pagevec_lookup() takes a
10311da177e4SLinus Torvalds  * reference against the pages in @pvec.
10321da177e4SLinus Torvalds  *
10331da177e4SLinus Torvalds  * The search returns a group of mapping-contiguous pages with ascending
1034d72dc8a2SJan Kara  * indexes.  There may be holes in the indices due to not-present pages. We
1035d72dc8a2SJan Kara  * also update @start to index the next page for the traversal.
10361da177e4SLinus Torvalds  *
1037b947cee4SJan Kara  * pagevec_lookup_range() returns the number of pages which were found. If this
1038e02a9f04SRandy Dunlap  * number is smaller than PAGEVEC_SIZE, the end of specified range has been
1039b947cee4SJan Kara  * reached.
10401da177e4SLinus Torvalds  */
1041b947cee4SJan Kara unsigned pagevec_lookup_range(struct pagevec *pvec,
1042397162ffSJan Kara 		struct address_space *mapping, pgoff_t *start, pgoff_t end)
10431da177e4SLinus Torvalds {
1044397162ffSJan Kara 	pvec->nr = find_get_pages_range(mapping, start, end, PAGEVEC_SIZE,
1045b947cee4SJan Kara 					pvec->pages);
10461da177e4SLinus Torvalds 	return pagevec_count(pvec);
10471da177e4SLinus Torvalds }
1048b947cee4SJan Kara EXPORT_SYMBOL(pagevec_lookup_range);
104978539fdfSChristoph Hellwig 
105072b045aeSJan Kara unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
105172b045aeSJan Kara 		struct address_space *mapping, pgoff_t *index, pgoff_t end,
105210bbd235SMatthew Wilcox 		xa_mark_t tag)
10531da177e4SLinus Torvalds {
105472b045aeSJan Kara 	pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
105567fd707fSJan Kara 					PAGEVEC_SIZE, pvec->pages);
10561da177e4SLinus Torvalds 	return pagevec_count(pvec);
10571da177e4SLinus Torvalds }
105872b045aeSJan Kara EXPORT_SYMBOL(pagevec_lookup_range_tag);
10591da177e4SLinus Torvalds 
106093d3b714SJan Kara unsigned pagevec_lookup_range_nr_tag(struct pagevec *pvec,
106193d3b714SJan Kara 		struct address_space *mapping, pgoff_t *index, pgoff_t end,
106210bbd235SMatthew Wilcox 		xa_mark_t tag, unsigned max_pages)
106393d3b714SJan Kara {
106493d3b714SJan Kara 	pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
106593d3b714SJan Kara 		min_t(unsigned int, max_pages, PAGEVEC_SIZE), pvec->pages);
106693d3b714SJan Kara 	return pagevec_count(pvec);
106793d3b714SJan Kara }
106893d3b714SJan Kara EXPORT_SYMBOL(pagevec_lookup_range_nr_tag);
10691da177e4SLinus Torvalds /*
10701da177e4SLinus Torvalds  * Perform any setup for the swap system
10711da177e4SLinus Torvalds  */
10721da177e4SLinus Torvalds void __init swap_setup(void)
10731da177e4SLinus Torvalds {
1074ca79b0c2SArun KS 	unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
1075e0bf68ddSPeter Zijlstra 
10761da177e4SLinus Torvalds 	/* Use a smaller cluster for small-memory machines */
10771da177e4SLinus Torvalds 	if (megs < 16)
10781da177e4SLinus Torvalds 		page_cluster = 2;
10791da177e4SLinus Torvalds 	else
10801da177e4SLinus Torvalds 		page_cluster = 3;
10811da177e4SLinus Torvalds 	/*
10821da177e4SLinus Torvalds 	 * Right now other parts of the system means that we
10831da177e4SLinus Torvalds 	 * _really_ don't want to cluster much more
10841da177e4SLinus Torvalds 	 */
10851da177e4SLinus Torvalds }
1086