xref: /linux/mm/swap.c (revision 570432470275c3da15b85362bc1461945b9c1919)
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
11*57043247SMauro 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);
50f7ad2a6cSShaohua Li static DEFINE_PER_CPU(struct pagevec, lru_lazyfree_pvecs);
51a4a921aaSMing Li #ifdef CONFIG_SMP
52a4a921aaSMing Li static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
53a4a921aaSMing Li #endif
54902aaed0SHisashi Hifumi 
55b221385bSAdrian Bunk /*
56b221385bSAdrian Bunk  * This path almost never happens for VM activity - pages are normally
57b221385bSAdrian Bunk  * freed via pagevecs.  But it gets used by networking.
58b221385bSAdrian Bunk  */
59920c7a5dSHarvey Harrison static void __page_cache_release(struct page *page)
60b221385bSAdrian Bunk {
61b221385bSAdrian Bunk 	if (PageLRU(page)) {
62f4b7e272SAndrey Ryabinin 		pg_data_t *pgdat = page_pgdat(page);
63fa9add64SHugh Dickins 		struct lruvec *lruvec;
64fa9add64SHugh Dickins 		unsigned long flags;
65b221385bSAdrian Bunk 
66f4b7e272SAndrey Ryabinin 		spin_lock_irqsave(&pgdat->lru_lock, flags);
67f4b7e272SAndrey Ryabinin 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
68309381feSSasha Levin 		VM_BUG_ON_PAGE(!PageLRU(page), page);
69b221385bSAdrian Bunk 		__ClearPageLRU(page);
70fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, page_off_lru(page));
71f4b7e272SAndrey Ryabinin 		spin_unlock_irqrestore(&pgdat->lru_lock, flags);
72b221385bSAdrian Bunk 	}
7362906027SNicholas Piggin 	__ClearPageWaiters(page);
740a31bc97SJohannes Weiner 	mem_cgroup_uncharge(page);
7591807063SAndrea Arcangeli }
7691807063SAndrea Arcangeli 
7791807063SAndrea Arcangeli static void __put_single_page(struct page *page)
7891807063SAndrea Arcangeli {
7991807063SAndrea Arcangeli 	__page_cache_release(page);
802d4894b5SMel Gorman 	free_unref_page(page);
81b221385bSAdrian Bunk }
82b221385bSAdrian Bunk 
8391807063SAndrea Arcangeli static void __put_compound_page(struct page *page)
8491807063SAndrea Arcangeli {
8591807063SAndrea Arcangeli 	compound_page_dtor *dtor;
8691807063SAndrea Arcangeli 
87822fc613SNaoya Horiguchi 	/*
88822fc613SNaoya Horiguchi 	 * __page_cache_release() is supposed to be called for thp, not for
89822fc613SNaoya Horiguchi 	 * hugetlb. This is because hugetlb page does never have PageLRU set
90822fc613SNaoya Horiguchi 	 * (it's never listed to any LRU lists) and no memcg routines should
91822fc613SNaoya Horiguchi 	 * be called for hugetlb (it has a separate hugetlb_cgroup.)
92822fc613SNaoya Horiguchi 	 */
93822fc613SNaoya Horiguchi 	if (!PageHuge(page))
9491807063SAndrea Arcangeli 		__page_cache_release(page);
9591807063SAndrea Arcangeli 	dtor = get_compound_page_dtor(page);
9691807063SAndrea Arcangeli 	(*dtor)(page);
9791807063SAndrea Arcangeli }
9891807063SAndrea Arcangeli 
99ddc58f27SKirill A. Shutemov void __put_page(struct page *page)
100c747ce79SJianyu Zhan {
10171389703SDan Williams 	if (is_zone_device_page(page)) {
10271389703SDan Williams 		put_dev_pagemap(page->pgmap);
10371389703SDan Williams 
10471389703SDan Williams 		/*
10571389703SDan Williams 		 * The page belongs to the device that created pgmap. Do
10671389703SDan Williams 		 * not return it to page allocator.
10771389703SDan Williams 		 */
10871389703SDan Williams 		return;
10971389703SDan Williams 	}
11071389703SDan Williams 
111ddc58f27SKirill A. Shutemov 	if (unlikely(PageCompound(page)))
11226296ad2SAndrew Morton 		__put_compound_page(page);
11326296ad2SAndrew Morton 	else
11426296ad2SAndrew Morton 		__put_single_page(page);
11526296ad2SAndrew Morton }
116ddc58f27SKirill A. Shutemov EXPORT_SYMBOL(__put_page);
11770b50f94SAndrea Arcangeli 
1181d7ea732SAlexander Zarochentsev /**
1197682486bSRandy Dunlap  * put_pages_list() - release a list of pages
1207682486bSRandy Dunlap  * @pages: list of pages threaded on page->lru
1211d7ea732SAlexander Zarochentsev  *
1221d7ea732SAlexander Zarochentsev  * Release a list of pages which are strung together on page.lru.  Currently
1231d7ea732SAlexander Zarochentsev  * used by read_cache_pages() and related error recovery code.
1241d7ea732SAlexander Zarochentsev  */
1251d7ea732SAlexander Zarochentsev void put_pages_list(struct list_head *pages)
1261d7ea732SAlexander Zarochentsev {
1271d7ea732SAlexander Zarochentsev 	while (!list_empty(pages)) {
1281d7ea732SAlexander Zarochentsev 		struct page *victim;
1291d7ea732SAlexander Zarochentsev 
130f86196eaSNikolay Borisov 		victim = lru_to_page(pages);
1311d7ea732SAlexander Zarochentsev 		list_del(&victim->lru);
13209cbfeafSKirill A. Shutemov 		put_page(victim);
1331d7ea732SAlexander Zarochentsev 	}
1341d7ea732SAlexander Zarochentsev }
1351d7ea732SAlexander Zarochentsev EXPORT_SYMBOL(put_pages_list);
1361d7ea732SAlexander Zarochentsev 
13718022c5dSMel Gorman /*
13818022c5dSMel Gorman  * get_kernel_pages() - pin kernel pages in memory
13918022c5dSMel Gorman  * @kiov:	An array of struct kvec structures
14018022c5dSMel Gorman  * @nr_segs:	number of segments to pin
14118022c5dSMel Gorman  * @write:	pinning for read/write, currently ignored
14218022c5dSMel Gorman  * @pages:	array that receives pointers to the pages pinned.
14318022c5dSMel Gorman  *		Should be at least nr_segs long.
14418022c5dSMel Gorman  *
14518022c5dSMel Gorman  * Returns number of pages pinned. This may be fewer than the number
14618022c5dSMel Gorman  * requested. If nr_pages is 0 or negative, returns 0. If no pages
14718022c5dSMel Gorman  * were pinned, returns -errno. Each page returned must be released
14818022c5dSMel Gorman  * with a put_page() call when it is finished with.
14918022c5dSMel Gorman  */
15018022c5dSMel Gorman int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
15118022c5dSMel Gorman 		struct page **pages)
15218022c5dSMel Gorman {
15318022c5dSMel Gorman 	int seg;
15418022c5dSMel Gorman 
15518022c5dSMel Gorman 	for (seg = 0; seg < nr_segs; seg++) {
15618022c5dSMel Gorman 		if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
15718022c5dSMel Gorman 			return seg;
15818022c5dSMel Gorman 
1595a178119SMel Gorman 		pages[seg] = kmap_to_page(kiov[seg].iov_base);
16009cbfeafSKirill A. Shutemov 		get_page(pages[seg]);
16118022c5dSMel Gorman 	}
16218022c5dSMel Gorman 
16318022c5dSMel Gorman 	return seg;
16418022c5dSMel Gorman }
16518022c5dSMel Gorman EXPORT_SYMBOL_GPL(get_kernel_pages);
16618022c5dSMel Gorman 
16718022c5dSMel Gorman /*
16818022c5dSMel Gorman  * get_kernel_page() - pin a kernel page in memory
16918022c5dSMel Gorman  * @start:	starting kernel address
17018022c5dSMel Gorman  * @write:	pinning for read/write, currently ignored
17118022c5dSMel Gorman  * @pages:	array that receives pointer to the page pinned.
17218022c5dSMel Gorman  *		Must be at least nr_segs long.
17318022c5dSMel Gorman  *
17418022c5dSMel Gorman  * Returns 1 if page is pinned. If the page was not pinned, returns
17518022c5dSMel Gorman  * -errno. The page returned must be released with a put_page() call
17618022c5dSMel Gorman  * when it is finished with.
17718022c5dSMel Gorman  */
17818022c5dSMel Gorman int get_kernel_page(unsigned long start, int write, struct page **pages)
17918022c5dSMel Gorman {
18018022c5dSMel Gorman 	const struct kvec kiov = {
18118022c5dSMel Gorman 		.iov_base = (void *)start,
18218022c5dSMel Gorman 		.iov_len = PAGE_SIZE
18318022c5dSMel Gorman 	};
18418022c5dSMel Gorman 
18518022c5dSMel Gorman 	return get_kernel_pages(&kiov, 1, write, pages);
18618022c5dSMel Gorman }
18718022c5dSMel Gorman EXPORT_SYMBOL_GPL(get_kernel_page);
18818022c5dSMel Gorman 
1893dd7ae8eSShaohua Li static void pagevec_lru_move_fn(struct pagevec *pvec,
190fa9add64SHugh Dickins 	void (*move_fn)(struct page *page, struct lruvec *lruvec, void *arg),
1913dd7ae8eSShaohua Li 	void *arg)
192902aaed0SHisashi Hifumi {
193902aaed0SHisashi Hifumi 	int i;
19468eb0731SMel Gorman 	struct pglist_data *pgdat = NULL;
195fa9add64SHugh Dickins 	struct lruvec *lruvec;
1963dd7ae8eSShaohua Li 	unsigned long flags = 0;
197902aaed0SHisashi Hifumi 
198902aaed0SHisashi Hifumi 	for (i = 0; i < pagevec_count(pvec); i++) {
199902aaed0SHisashi Hifumi 		struct page *page = pvec->pages[i];
20068eb0731SMel Gorman 		struct pglist_data *pagepgdat = page_pgdat(page);
201902aaed0SHisashi Hifumi 
20268eb0731SMel Gorman 		if (pagepgdat != pgdat) {
20368eb0731SMel Gorman 			if (pgdat)
20468eb0731SMel Gorman 				spin_unlock_irqrestore(&pgdat->lru_lock, flags);
20568eb0731SMel Gorman 			pgdat = pagepgdat;
20668eb0731SMel Gorman 			spin_lock_irqsave(&pgdat->lru_lock, flags);
207902aaed0SHisashi Hifumi 		}
2083dd7ae8eSShaohua Li 
20968eb0731SMel Gorman 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
210fa9add64SHugh Dickins 		(*move_fn)(page, lruvec, arg);
2113dd7ae8eSShaohua Li 	}
21268eb0731SMel Gorman 	if (pgdat)
21368eb0731SMel Gorman 		spin_unlock_irqrestore(&pgdat->lru_lock, flags);
214c6f92f9fSMel Gorman 	release_pages(pvec->pages, pvec->nr);
2153dd7ae8eSShaohua Li 	pagevec_reinit(pvec);
2163dd7ae8eSShaohua Li }
2173dd7ae8eSShaohua Li 
218fa9add64SHugh Dickins static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec,
219fa9add64SHugh Dickins 				 void *arg)
2203dd7ae8eSShaohua Li {
2213dd7ae8eSShaohua Li 	int *pgmoved = arg;
2223dd7ae8eSShaohua Li 
223c55e8d03SJohannes Weiner 	if (PageLRU(page) && !PageUnevictable(page)) {
224c55e8d03SJohannes Weiner 		del_page_from_lru_list(page, lruvec, page_lru(page));
225c55e8d03SJohannes Weiner 		ClearPageActive(page);
226c55e8d03SJohannes Weiner 		add_page_to_lru_list_tail(page, lruvec, page_lru(page));
2273dd7ae8eSShaohua Li 		(*pgmoved)++;
228902aaed0SHisashi Hifumi 	}
229902aaed0SHisashi Hifumi }
2303dd7ae8eSShaohua Li 
2313dd7ae8eSShaohua Li /*
2323dd7ae8eSShaohua Li  * pagevec_move_tail() must be called with IRQ disabled.
2333dd7ae8eSShaohua Li  * Otherwise this may cause nasty races.
2343dd7ae8eSShaohua Li  */
2353dd7ae8eSShaohua Li static void pagevec_move_tail(struct pagevec *pvec)
2363dd7ae8eSShaohua Li {
2373dd7ae8eSShaohua Li 	int pgmoved = 0;
2383dd7ae8eSShaohua Li 
2393dd7ae8eSShaohua Li 	pagevec_lru_move_fn(pvec, pagevec_move_tail_fn, &pgmoved);
240902aaed0SHisashi Hifumi 	__count_vm_events(PGROTATED, pgmoved);
241902aaed0SHisashi Hifumi }
242902aaed0SHisashi Hifumi 
243902aaed0SHisashi Hifumi /*
2441da177e4SLinus Torvalds  * Writeback is about to end against a page which has been marked for immediate
2451da177e4SLinus Torvalds  * reclaim.  If it still appears to be reclaimable, move it to the tail of the
246902aaed0SHisashi Hifumi  * inactive list.
2471da177e4SLinus Torvalds  */
248ac6aadb2SMiklos Szeredi void rotate_reclaimable_page(struct page *page)
2491da177e4SLinus Torvalds {
250c55e8d03SJohannes Weiner 	if (!PageLocked(page) && !PageDirty(page) &&
251894bc310SLee Schermerhorn 	    !PageUnevictable(page) && PageLRU(page)) {
252902aaed0SHisashi Hifumi 		struct pagevec *pvec;
2531da177e4SLinus Torvalds 		unsigned long flags;
2541da177e4SLinus Torvalds 
25509cbfeafSKirill A. Shutemov 		get_page(page);
256902aaed0SHisashi Hifumi 		local_irq_save(flags);
2577c8e0181SChristoph Lameter 		pvec = this_cpu_ptr(&lru_rotate_pvecs);
2588f182270SLukasz Odzioba 		if (!pagevec_add(pvec, page) || PageCompound(page))
259902aaed0SHisashi Hifumi 			pagevec_move_tail(pvec);
260902aaed0SHisashi Hifumi 		local_irq_restore(flags);
261ac6aadb2SMiklos Szeredi 	}
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds 
264fa9add64SHugh Dickins static void update_page_reclaim_stat(struct lruvec *lruvec,
2653e2f41f1SKOSAKI Motohiro 				     int file, int rotated)
2663e2f41f1SKOSAKI Motohiro {
267fa9add64SHugh Dickins 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
2683e2f41f1SKOSAKI Motohiro 
2693e2f41f1SKOSAKI Motohiro 	reclaim_stat->recent_scanned[file]++;
2703e2f41f1SKOSAKI Motohiro 	if (rotated)
2713e2f41f1SKOSAKI Motohiro 		reclaim_stat->recent_rotated[file]++;
2723e2f41f1SKOSAKI Motohiro }
2733e2f41f1SKOSAKI Motohiro 
274fa9add64SHugh Dickins static void __activate_page(struct page *page, struct lruvec *lruvec,
275fa9add64SHugh Dickins 			    void *arg)
276744ed144SShaohua Li {
2777a608572SLinus Torvalds 	if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
278744ed144SShaohua Li 		int file = page_is_file_cache(page);
279744ed144SShaohua Li 		int lru = page_lru_base_type(page);
280744ed144SShaohua Li 
281fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, lru);
282744ed144SShaohua Li 		SetPageActive(page);
283744ed144SShaohua Li 		lru += LRU_ACTIVE;
284fa9add64SHugh Dickins 		add_page_to_lru_list(page, lruvec, lru);
28524b7e581SMel Gorman 		trace_mm_lru_activate(page);
2867a608572SLinus Torvalds 
287fa9add64SHugh Dickins 		__count_vm_event(PGACTIVATE);
288fa9add64SHugh Dickins 		update_page_reclaim_stat(lruvec, file, 1);
289744ed144SShaohua Li 	}
290eb709b0dSShaohua Li }
291eb709b0dSShaohua Li 
292eb709b0dSShaohua Li #ifdef CONFIG_SMP
293eb709b0dSShaohua Li static void activate_page_drain(int cpu)
294eb709b0dSShaohua Li {
295eb709b0dSShaohua Li 	struct pagevec *pvec = &per_cpu(activate_page_pvecs, cpu);
296eb709b0dSShaohua Li 
297eb709b0dSShaohua Li 	if (pagevec_count(pvec))
298eb709b0dSShaohua Li 		pagevec_lru_move_fn(pvec, __activate_page, NULL);
299eb709b0dSShaohua Li }
300eb709b0dSShaohua Li 
3015fbc4616SChris Metcalf static bool need_activate_page_drain(int cpu)
3025fbc4616SChris Metcalf {
3035fbc4616SChris Metcalf 	return pagevec_count(&per_cpu(activate_page_pvecs, cpu)) != 0;
3045fbc4616SChris Metcalf }
3055fbc4616SChris Metcalf 
306eb709b0dSShaohua Li void activate_page(struct page *page)
307eb709b0dSShaohua Li {
308800d8c63SKirill A. Shutemov 	page = compound_head(page);
309eb709b0dSShaohua Li 	if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
310eb709b0dSShaohua Li 		struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
311eb709b0dSShaohua Li 
31209cbfeafSKirill A. Shutemov 		get_page(page);
3138f182270SLukasz Odzioba 		if (!pagevec_add(pvec, page) || PageCompound(page))
314eb709b0dSShaohua Li 			pagevec_lru_move_fn(pvec, __activate_page, NULL);
315eb709b0dSShaohua Li 		put_cpu_var(activate_page_pvecs);
316eb709b0dSShaohua Li 	}
317eb709b0dSShaohua Li }
318eb709b0dSShaohua Li 
319eb709b0dSShaohua Li #else
320eb709b0dSShaohua Li static inline void activate_page_drain(int cpu)
321eb709b0dSShaohua Li {
322eb709b0dSShaohua Li }
323eb709b0dSShaohua Li 
324eb709b0dSShaohua Li void activate_page(struct page *page)
325eb709b0dSShaohua Li {
326f4b7e272SAndrey Ryabinin 	pg_data_t *pgdat = page_pgdat(page);
327eb709b0dSShaohua Li 
328800d8c63SKirill A. Shutemov 	page = compound_head(page);
329f4b7e272SAndrey Ryabinin 	spin_lock_irq(&pgdat->lru_lock);
330f4b7e272SAndrey Ryabinin 	__activate_page(page, mem_cgroup_page_lruvec(page, pgdat), NULL);
331f4b7e272SAndrey Ryabinin 	spin_unlock_irq(&pgdat->lru_lock);
3321da177e4SLinus Torvalds }
333eb709b0dSShaohua Li #endif
3341da177e4SLinus Torvalds 
335059285a2SMel Gorman static void __lru_cache_activate_page(struct page *page)
336059285a2SMel Gorman {
337059285a2SMel Gorman 	struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
338059285a2SMel Gorman 	int i;
339059285a2SMel Gorman 
340059285a2SMel Gorman 	/*
341059285a2SMel Gorman 	 * Search backwards on the optimistic assumption that the page being
342059285a2SMel Gorman 	 * activated has just been added to this pagevec. Note that only
343059285a2SMel Gorman 	 * the local pagevec is examined as a !PageLRU page could be in the
344059285a2SMel Gorman 	 * process of being released, reclaimed, migrated or on a remote
345059285a2SMel Gorman 	 * pagevec that is currently being drained. Furthermore, marking
346059285a2SMel Gorman 	 * a remote pagevec's page PageActive potentially hits a race where
347059285a2SMel Gorman 	 * a page is marked PageActive just after it is added to the inactive
348059285a2SMel Gorman 	 * list causing accounting errors and BUG_ON checks to trigger.
349059285a2SMel Gorman 	 */
350059285a2SMel Gorman 	for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
351059285a2SMel Gorman 		struct page *pagevec_page = pvec->pages[i];
352059285a2SMel Gorman 
353059285a2SMel Gorman 		if (pagevec_page == page) {
354059285a2SMel Gorman 			SetPageActive(page);
355059285a2SMel Gorman 			break;
356059285a2SMel Gorman 		}
357059285a2SMel Gorman 	}
358059285a2SMel Gorman 
359059285a2SMel Gorman 	put_cpu_var(lru_add_pvec);
360059285a2SMel Gorman }
361059285a2SMel Gorman 
3621da177e4SLinus Torvalds /*
3631da177e4SLinus Torvalds  * Mark a page as having seen activity.
3641da177e4SLinus Torvalds  *
3651da177e4SLinus Torvalds  * inactive,unreferenced	->	inactive,referenced
3661da177e4SLinus Torvalds  * inactive,referenced		->	active,unreferenced
3671da177e4SLinus Torvalds  * active,unreferenced		->	active,referenced
368eb39d618SHugh Dickins  *
369eb39d618SHugh Dickins  * When a newly allocated page is not yet visible, so safe for non-atomic ops,
370eb39d618SHugh Dickins  * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
3711da177e4SLinus Torvalds  */
372920c7a5dSHarvey Harrison void mark_page_accessed(struct page *page)
3731da177e4SLinus Torvalds {
374e90309c9SKirill A. Shutemov 	page = compound_head(page);
375894bc310SLee Schermerhorn 	if (!PageActive(page) && !PageUnevictable(page) &&
376059285a2SMel Gorman 			PageReferenced(page)) {
377059285a2SMel Gorman 
378059285a2SMel Gorman 		/*
379059285a2SMel Gorman 		 * If the page is on the LRU, queue it for activation via
380059285a2SMel Gorman 		 * activate_page_pvecs. Otherwise, assume the page is on a
381059285a2SMel Gorman 		 * pagevec, mark it active and it'll be moved to the active
382059285a2SMel Gorman 		 * LRU on the next drain.
383059285a2SMel Gorman 		 */
384059285a2SMel Gorman 		if (PageLRU(page))
3851da177e4SLinus Torvalds 			activate_page(page);
386059285a2SMel Gorman 		else
387059285a2SMel Gorman 			__lru_cache_activate_page(page);
3881da177e4SLinus Torvalds 		ClearPageReferenced(page);
389a528910eSJohannes Weiner 		if (page_is_file_cache(page))
390a528910eSJohannes Weiner 			workingset_activation(page);
3911da177e4SLinus Torvalds 	} else if (!PageReferenced(page)) {
3921da177e4SLinus Torvalds 		SetPageReferenced(page);
3931da177e4SLinus Torvalds 	}
39433c3fc71SVladimir Davydov 	if (page_is_idle(page))
39533c3fc71SVladimir Davydov 		clear_page_idle(page);
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds EXPORT_SYMBOL(mark_page_accessed);
3981da177e4SLinus Torvalds 
3992329d375SJianyu Zhan static void __lru_cache_add(struct page *page)
4001da177e4SLinus Torvalds {
40113f7f789SMel Gorman 	struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
40213f7f789SMel Gorman 
40309cbfeafSKirill A. Shutemov 	get_page(page);
4048f182270SLukasz Odzioba 	if (!pagevec_add(pvec, page) || PageCompound(page))
405a0b8cab3SMel Gorman 		__pagevec_lru_add(pvec);
40613f7f789SMel Gorman 	put_cpu_var(lru_add_pvec);
4071da177e4SLinus Torvalds }
4082329d375SJianyu Zhan 
4092329d375SJianyu Zhan /**
410e02a9f04SRandy Dunlap  * lru_cache_add_anon - add a page to the page lists
4112329d375SJianyu Zhan  * @page: the page to add
4122329d375SJianyu Zhan  */
4132329d375SJianyu Zhan void lru_cache_add_anon(struct page *page)
4142329d375SJianyu Zhan {
4156fb81a17SMel Gorman 	if (PageActive(page))
4162329d375SJianyu Zhan 		ClearPageActive(page);
4172329d375SJianyu Zhan 	__lru_cache_add(page);
4182329d375SJianyu Zhan }
4192329d375SJianyu Zhan 
4202329d375SJianyu Zhan void lru_cache_add_file(struct page *page)
4212329d375SJianyu Zhan {
4226fb81a17SMel Gorman 	if (PageActive(page))
4232329d375SJianyu Zhan 		ClearPageActive(page);
4242329d375SJianyu Zhan 	__lru_cache_add(page);
4252329d375SJianyu Zhan }
4262329d375SJianyu Zhan EXPORT_SYMBOL(lru_cache_add_file);
4271da177e4SLinus Torvalds 
428f04e9ebbSKOSAKI Motohiro /**
429c53954a0SMel Gorman  * lru_cache_add - add a page to a page list
430f04e9ebbSKOSAKI Motohiro  * @page: the page to be added to the LRU.
4312329d375SJianyu Zhan  *
4322329d375SJianyu Zhan  * Queue the page for addition to the LRU via pagevec. The decision on whether
4332329d375SJianyu Zhan  * to add the page to the [in]active [file|anon] list is deferred until the
4342329d375SJianyu Zhan  * pagevec is drained. This gives a chance for the caller of lru_cache_add()
4352329d375SJianyu Zhan  * have the page added to the active list using mark_page_accessed().
436f04e9ebbSKOSAKI Motohiro  */
437c53954a0SMel Gorman void lru_cache_add(struct page *page)
4381da177e4SLinus Torvalds {
439309381feSSasha Levin 	VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
440309381feSSasha Levin 	VM_BUG_ON_PAGE(PageLRU(page), page);
441c53954a0SMel Gorman 	__lru_cache_add(page);
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
444894bc310SLee Schermerhorn /**
44500501b53SJohannes Weiner  * lru_cache_add_active_or_unevictable
44600501b53SJohannes Weiner  * @page:  the page to be added to LRU
44700501b53SJohannes Weiner  * @vma:   vma in which page is mapped for determining reclaimability
44800501b53SJohannes Weiner  *
44900501b53SJohannes Weiner  * Place @page on the active or unevictable LRU list, depending on its
45000501b53SJohannes Weiner  * evictability.  Note that if the page is not evictable, it goes
45100501b53SJohannes Weiner  * directly back onto it's zone's unevictable list, it does NOT use a
45200501b53SJohannes Weiner  * per cpu pagevec.
45300501b53SJohannes Weiner  */
45400501b53SJohannes Weiner void lru_cache_add_active_or_unevictable(struct page *page,
45500501b53SJohannes Weiner 					 struct vm_area_struct *vma)
45600501b53SJohannes Weiner {
45700501b53SJohannes Weiner 	VM_BUG_ON_PAGE(PageLRU(page), page);
45800501b53SJohannes Weiner 
4599c4e6b1aSShakeel Butt 	if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED))
46000501b53SJohannes Weiner 		SetPageActive(page);
4619c4e6b1aSShakeel Butt 	else if (!TestSetPageMlocked(page)) {
46200501b53SJohannes Weiner 		/*
46300501b53SJohannes Weiner 		 * We use the irq-unsafe __mod_zone_page_stat because this
46400501b53SJohannes Weiner 		 * counter is not modified from interrupt context, and the pte
46500501b53SJohannes Weiner 		 * lock is held(spinlock), which implies preemption disabled.
46600501b53SJohannes Weiner 		 */
46700501b53SJohannes Weiner 		__mod_zone_page_state(page_zone(page), NR_MLOCK,
46800501b53SJohannes Weiner 				    hpage_nr_pages(page));
46900501b53SJohannes Weiner 		count_vm_event(UNEVICTABLE_PGMLOCKED);
47000501b53SJohannes Weiner 	}
4719c4e6b1aSShakeel Butt 	lru_cache_add(page);
47200501b53SJohannes Weiner }
47300501b53SJohannes Weiner 
474902aaed0SHisashi Hifumi /*
47531560180SMinchan Kim  * If the page can not be invalidated, it is moved to the
47631560180SMinchan Kim  * inactive list to speed up its reclaim.  It is moved to the
47731560180SMinchan Kim  * head of the list, rather than the tail, to give the flusher
47831560180SMinchan Kim  * threads some time to write it out, as this is much more
47931560180SMinchan Kim  * effective than the single-page writeout from reclaim.
480278df9f4SMinchan Kim  *
481278df9f4SMinchan Kim  * If the page isn't page_mapped and dirty/writeback, the page
482278df9f4SMinchan Kim  * could reclaim asap using PG_reclaim.
483278df9f4SMinchan Kim  *
484278df9f4SMinchan Kim  * 1. active, mapped page -> none
485278df9f4SMinchan Kim  * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
486278df9f4SMinchan Kim  * 3. inactive, mapped page -> none
487278df9f4SMinchan Kim  * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
488278df9f4SMinchan Kim  * 5. inactive, clean -> inactive, tail
489278df9f4SMinchan Kim  * 6. Others -> none
490278df9f4SMinchan Kim  *
491278df9f4SMinchan Kim  * In 4, why it moves inactive's head, the VM expects the page would
492278df9f4SMinchan Kim  * be write it out by flusher threads as this is much more effective
493278df9f4SMinchan Kim  * than the single-page writeout from reclaim.
49431560180SMinchan Kim  */
495cc5993bdSMinchan Kim static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
496fa9add64SHugh Dickins 			      void *arg)
49731560180SMinchan Kim {
49831560180SMinchan Kim 	int lru, file;
499278df9f4SMinchan Kim 	bool active;
50031560180SMinchan Kim 
501278df9f4SMinchan Kim 	if (!PageLRU(page))
50231560180SMinchan Kim 		return;
50331560180SMinchan Kim 
504bad49d9cSMinchan Kim 	if (PageUnevictable(page))
505bad49d9cSMinchan Kim 		return;
506bad49d9cSMinchan Kim 
50731560180SMinchan Kim 	/* Some processes are using the page */
50831560180SMinchan Kim 	if (page_mapped(page))
50931560180SMinchan Kim 		return;
51031560180SMinchan Kim 
511278df9f4SMinchan Kim 	active = PageActive(page);
51231560180SMinchan Kim 	file = page_is_file_cache(page);
51331560180SMinchan Kim 	lru = page_lru_base_type(page);
514fa9add64SHugh Dickins 
515fa9add64SHugh Dickins 	del_page_from_lru_list(page, lruvec, lru + active);
51631560180SMinchan Kim 	ClearPageActive(page);
51731560180SMinchan Kim 	ClearPageReferenced(page);
518fa9add64SHugh Dickins 	add_page_to_lru_list(page, lruvec, lru);
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 		 */
526278df9f4SMinchan Kim 		SetPageReclaim(page);
527278df9f4SMinchan Kim 	} else {
528278df9f4SMinchan Kim 		/*
529278df9f4SMinchan Kim 		 * The page's writeback ends up during pagevec
530278df9f4SMinchan Kim 		 * We moves tha page into tail of inactive.
531278df9f4SMinchan Kim 		 */
532925b7673SJohannes Weiner 		list_move_tail(&page->lru, &lruvec->lists[lru]);
533278df9f4SMinchan Kim 		__count_vm_event(PGROTATED);
534278df9f4SMinchan Kim 	}
535278df9f4SMinchan Kim 
536278df9f4SMinchan Kim 	if (active)
537278df9f4SMinchan Kim 		__count_vm_event(PGDEACTIVATE);
538fa9add64SHugh Dickins 	update_page_reclaim_stat(lruvec, file, 0);
53931560180SMinchan Kim }
54031560180SMinchan Kim 
54110853a03SMinchan Kim 
542f7ad2a6cSShaohua Li static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec,
54310853a03SMinchan Kim 			    void *arg)
54410853a03SMinchan Kim {
545f7ad2a6cSShaohua Li 	if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
54624c92eb7SShaohua Li 	    !PageSwapCache(page) && !PageUnevictable(page)) {
547f7ad2a6cSShaohua Li 		bool active = PageActive(page);
54810853a03SMinchan Kim 
549f7ad2a6cSShaohua Li 		del_page_from_lru_list(page, lruvec,
550f7ad2a6cSShaohua Li 				       LRU_INACTIVE_ANON + active);
55110853a03SMinchan Kim 		ClearPageActive(page);
55210853a03SMinchan Kim 		ClearPageReferenced(page);
553f7ad2a6cSShaohua Li 		/*
554f7ad2a6cSShaohua Li 		 * lazyfree pages are clean anonymous pages. They have
555f7ad2a6cSShaohua Li 		 * SwapBacked flag cleared to distinguish normal anonymous
556f7ad2a6cSShaohua Li 		 * pages
557f7ad2a6cSShaohua Li 		 */
558f7ad2a6cSShaohua Li 		ClearPageSwapBacked(page);
559f7ad2a6cSShaohua Li 		add_page_to_lru_list(page, lruvec, LRU_INACTIVE_FILE);
56010853a03SMinchan Kim 
561f7ad2a6cSShaohua Li 		__count_vm_events(PGLAZYFREE, hpage_nr_pages(page));
5622262185cSRoman Gushchin 		count_memcg_page_event(page, PGLAZYFREE);
563f7ad2a6cSShaohua Li 		update_page_reclaim_stat(lruvec, 1, 0);
56410853a03SMinchan Kim 	}
56510853a03SMinchan Kim }
56610853a03SMinchan Kim 
56731560180SMinchan Kim /*
568902aaed0SHisashi Hifumi  * Drain pages out of the cpu's pagevecs.
569902aaed0SHisashi Hifumi  * Either "cpu" is the current CPU, and preemption has already been
570902aaed0SHisashi Hifumi  * disabled; or "cpu" is being hot-unplugged, and is already dead.
571902aaed0SHisashi Hifumi  */
572f0cb3c76SKonstantin Khlebnikov void lru_add_drain_cpu(int cpu)
5731da177e4SLinus Torvalds {
57413f7f789SMel Gorman 	struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	if (pagevec_count(pvec))
577a0b8cab3SMel Gorman 		__pagevec_lru_add(pvec);
578902aaed0SHisashi Hifumi 
579902aaed0SHisashi Hifumi 	pvec = &per_cpu(lru_rotate_pvecs, cpu);
580902aaed0SHisashi Hifumi 	if (pagevec_count(pvec)) {
581902aaed0SHisashi Hifumi 		unsigned long flags;
582902aaed0SHisashi Hifumi 
583902aaed0SHisashi Hifumi 		/* No harm done if a racing interrupt already did this */
584902aaed0SHisashi Hifumi 		local_irq_save(flags);
585902aaed0SHisashi Hifumi 		pagevec_move_tail(pvec);
586902aaed0SHisashi Hifumi 		local_irq_restore(flags);
587902aaed0SHisashi Hifumi 	}
58831560180SMinchan Kim 
589cc5993bdSMinchan Kim 	pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
59031560180SMinchan Kim 	if (pagevec_count(pvec))
591cc5993bdSMinchan Kim 		pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
592eb709b0dSShaohua Li 
593f7ad2a6cSShaohua Li 	pvec = &per_cpu(lru_lazyfree_pvecs, cpu);
59410853a03SMinchan Kim 	if (pagevec_count(pvec))
595f7ad2a6cSShaohua Li 		pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
59610853a03SMinchan Kim 
597eb709b0dSShaohua Li 	activate_page_drain(cpu);
59831560180SMinchan Kim }
59931560180SMinchan Kim 
60031560180SMinchan Kim /**
601cc5993bdSMinchan Kim  * deactivate_file_page - forcefully deactivate a file page
60231560180SMinchan Kim  * @page: page to deactivate
60331560180SMinchan Kim  *
60431560180SMinchan Kim  * This function hints the VM that @page is a good reclaim candidate,
60531560180SMinchan Kim  * for example if its invalidation fails due to the page being dirty
60631560180SMinchan Kim  * or under writeback.
60731560180SMinchan Kim  */
608cc5993bdSMinchan Kim void deactivate_file_page(struct page *page)
60931560180SMinchan Kim {
610821ed6bbSMinchan Kim 	/*
611cc5993bdSMinchan Kim 	 * In a workload with many unevictable page such as mprotect,
612cc5993bdSMinchan Kim 	 * unevictable page deactivation for accelerating reclaim is pointless.
613821ed6bbSMinchan Kim 	 */
614821ed6bbSMinchan Kim 	if (PageUnevictable(page))
615821ed6bbSMinchan Kim 		return;
616821ed6bbSMinchan Kim 
61731560180SMinchan Kim 	if (likely(get_page_unless_zero(page))) {
618cc5993bdSMinchan Kim 		struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
61931560180SMinchan Kim 
6208f182270SLukasz Odzioba 		if (!pagevec_add(pvec, page) || PageCompound(page))
621cc5993bdSMinchan Kim 			pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
622cc5993bdSMinchan Kim 		put_cpu_var(lru_deactivate_file_pvecs);
62331560180SMinchan Kim 	}
62480bfed90SAndrew Morton }
62580bfed90SAndrew Morton 
62610853a03SMinchan Kim /**
627f7ad2a6cSShaohua Li  * mark_page_lazyfree - make an anon page lazyfree
62810853a03SMinchan Kim  * @page: page to deactivate
62910853a03SMinchan Kim  *
630f7ad2a6cSShaohua Li  * mark_page_lazyfree() moves @page to the inactive file list.
631f7ad2a6cSShaohua Li  * This is done to accelerate the reclaim of @page.
63210853a03SMinchan Kim  */
633f7ad2a6cSShaohua Li void mark_page_lazyfree(struct page *page)
63410853a03SMinchan Kim {
635f7ad2a6cSShaohua Li 	if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
63624c92eb7SShaohua Li 	    !PageSwapCache(page) && !PageUnevictable(page)) {
637f7ad2a6cSShaohua Li 		struct pagevec *pvec = &get_cpu_var(lru_lazyfree_pvecs);
63810853a03SMinchan Kim 
63909cbfeafSKirill A. Shutemov 		get_page(page);
6408f182270SLukasz Odzioba 		if (!pagevec_add(pvec, page) || PageCompound(page))
641f7ad2a6cSShaohua Li 			pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
642f7ad2a6cSShaohua Li 		put_cpu_var(lru_lazyfree_pvecs);
64310853a03SMinchan Kim 	}
64410853a03SMinchan Kim }
64510853a03SMinchan Kim 
64680bfed90SAndrew Morton void lru_add_drain(void)
64780bfed90SAndrew Morton {
648f0cb3c76SKonstantin Khlebnikov 	lru_add_drain_cpu(get_cpu());
64980bfed90SAndrew Morton 	put_cpu();
6501da177e4SLinus Torvalds }
6511da177e4SLinus Torvalds 
6526ea183d6SMichal Hocko #ifdef CONFIG_SMP
6536ea183d6SMichal Hocko 
6546ea183d6SMichal Hocko static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
6556ea183d6SMichal Hocko 
656c4028958SDavid Howells static void lru_add_drain_per_cpu(struct work_struct *dummy)
657053837fcSNick Piggin {
658053837fcSNick Piggin 	lru_add_drain();
659053837fcSNick Piggin }
660053837fcSNick Piggin 
6619852a721SMichal Hocko /*
6629852a721SMichal Hocko  * Doesn't need any cpu hotplug locking because we do rely on per-cpu
6639852a721SMichal Hocko  * kworkers being shut down before our page_alloc_cpu_dead callback is
6649852a721SMichal Hocko  * executed on the offlined cpu.
6659852a721SMichal Hocko  * Calling this function with cpu hotplug locks held can actually lead
6669852a721SMichal Hocko  * to obscure indirect dependencies via WQ context.
6679852a721SMichal Hocko  */
6689852a721SMichal Hocko void lru_add_drain_all(void)
669053837fcSNick Piggin {
6705fbc4616SChris Metcalf 	static DEFINE_MUTEX(lock);
6715fbc4616SChris Metcalf 	static struct cpumask has_work;
6725fbc4616SChris Metcalf 	int cpu;
6735fbc4616SChris Metcalf 
674ce612879SMichal Hocko 	/*
675ce612879SMichal Hocko 	 * Make sure nobody triggers this path before mm_percpu_wq is fully
676ce612879SMichal Hocko 	 * initialized.
677ce612879SMichal Hocko 	 */
678ce612879SMichal Hocko 	if (WARN_ON(!mm_percpu_wq))
679ce612879SMichal Hocko 		return;
680ce612879SMichal Hocko 
6815fbc4616SChris Metcalf 	mutex_lock(&lock);
6825fbc4616SChris Metcalf 	cpumask_clear(&has_work);
6835fbc4616SChris Metcalf 
6845fbc4616SChris Metcalf 	for_each_online_cpu(cpu) {
6855fbc4616SChris Metcalf 		struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
6865fbc4616SChris Metcalf 
6875fbc4616SChris Metcalf 		if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
6885fbc4616SChris Metcalf 		    pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
689cc5993bdSMinchan Kim 		    pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
690f7ad2a6cSShaohua Li 		    pagevec_count(&per_cpu(lru_lazyfree_pvecs, cpu)) ||
6915fbc4616SChris Metcalf 		    need_activate_page_drain(cpu)) {
6925fbc4616SChris Metcalf 			INIT_WORK(work, lru_add_drain_per_cpu);
693ce612879SMichal Hocko 			queue_work_on(cpu, mm_percpu_wq, work);
6945fbc4616SChris Metcalf 			cpumask_set_cpu(cpu, &has_work);
6955fbc4616SChris Metcalf 		}
6965fbc4616SChris Metcalf 	}
6975fbc4616SChris Metcalf 
6985fbc4616SChris Metcalf 	for_each_cpu(cpu, &has_work)
6995fbc4616SChris Metcalf 		flush_work(&per_cpu(lru_add_drain_work, cpu));
7005fbc4616SChris Metcalf 
7015fbc4616SChris Metcalf 	mutex_unlock(&lock);
702053837fcSNick Piggin }
7036ea183d6SMichal Hocko #else
7046ea183d6SMichal Hocko void lru_add_drain_all(void)
7056ea183d6SMichal Hocko {
7066ea183d6SMichal Hocko 	lru_add_drain();
7076ea183d6SMichal Hocko }
7086ea183d6SMichal Hocko #endif
709053837fcSNick Piggin 
710aabfb572SMichal Hocko /**
711ea1754a0SKirill A. Shutemov  * release_pages - batched put_page()
712aabfb572SMichal Hocko  * @pages: array of pages to release
713aabfb572SMichal Hocko  * @nr: number of pages
7141da177e4SLinus Torvalds  *
715aabfb572SMichal Hocko  * Decrement the reference count on all the pages in @pages.  If it
716aabfb572SMichal Hocko  * fell to zero, remove the page from the LRU and free it.
7171da177e4SLinus Torvalds  */
718c6f92f9fSMel Gorman void release_pages(struct page **pages, int nr)
7191da177e4SLinus Torvalds {
7201da177e4SLinus Torvalds 	int i;
721cc59850eSKonstantin Khlebnikov 	LIST_HEAD(pages_to_free);
722599d0c95SMel Gorman 	struct pglist_data *locked_pgdat = NULL;
723fa9add64SHugh Dickins 	struct lruvec *lruvec;
724902aaed0SHisashi Hifumi 	unsigned long uninitialized_var(flags);
725aabfb572SMichal Hocko 	unsigned int uninitialized_var(lock_batch);
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	for (i = 0; i < nr; i++) {
7281da177e4SLinus Torvalds 		struct page *page = pages[i];
7291da177e4SLinus Torvalds 
730aabfb572SMichal Hocko 		/*
731aabfb572SMichal Hocko 		 * Make sure the IRQ-safe lock-holding time does not get
732aabfb572SMichal Hocko 		 * excessive with a continuous string of pages from the
733599d0c95SMel Gorman 		 * same pgdat. The lock is held only if pgdat != NULL.
734aabfb572SMichal Hocko 		 */
735599d0c95SMel Gorman 		if (locked_pgdat && ++lock_batch == SWAP_CLUSTER_MAX) {
736599d0c95SMel Gorman 			spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
737599d0c95SMel Gorman 			locked_pgdat = NULL;
738aabfb572SMichal Hocko 		}
739aabfb572SMichal Hocko 
7406fcb52a5SAaron Lu 		if (is_huge_zero_page(page))
741aa88b68cSKirill A. Shutemov 			continue;
742aa88b68cSKirill A. Shutemov 
743c5d6c45eSIra Weiny 		if (is_zone_device_page(page)) {
744df6ad698SJérôme Glisse 			if (locked_pgdat) {
745df6ad698SJérôme Glisse 				spin_unlock_irqrestore(&locked_pgdat->lru_lock,
746df6ad698SJérôme Glisse 						       flags);
747df6ad698SJérôme Glisse 				locked_pgdat = NULL;
748df6ad698SJérôme Glisse 			}
749c5d6c45eSIra Weiny 			/*
750c5d6c45eSIra Weiny 			 * ZONE_DEVICE pages that return 'false' from
751c5d6c45eSIra Weiny 			 * put_devmap_managed_page() do not require special
752c5d6c45eSIra Weiny 			 * processing, and instead, expect a call to
753c5d6c45eSIra Weiny 			 * put_page_testzero().
754c5d6c45eSIra Weiny 			 */
755c5d6c45eSIra Weiny 			if (put_devmap_managed_page(page))
756df6ad698SJérôme Glisse 				continue;
757df6ad698SJérôme Glisse 		}
758df6ad698SJérôme Glisse 
759ddc58f27SKirill A. Shutemov 		page = compound_head(page);
760b5810039SNick Piggin 		if (!put_page_testzero(page))
7611da177e4SLinus Torvalds 			continue;
7621da177e4SLinus Torvalds 
763ddc58f27SKirill A. Shutemov 		if (PageCompound(page)) {
764599d0c95SMel Gorman 			if (locked_pgdat) {
765599d0c95SMel Gorman 				spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
766599d0c95SMel Gorman 				locked_pgdat = NULL;
767ddc58f27SKirill A. Shutemov 			}
768ddc58f27SKirill A. Shutemov 			__put_compound_page(page);
769ddc58f27SKirill A. Shutemov 			continue;
770ddc58f27SKirill A. Shutemov 		}
771ddc58f27SKirill A. Shutemov 
77246453a6eSNick Piggin 		if (PageLRU(page)) {
773599d0c95SMel Gorman 			struct pglist_data *pgdat = page_pgdat(page);
774894bc310SLee Schermerhorn 
775599d0c95SMel Gorman 			if (pgdat != locked_pgdat) {
776599d0c95SMel Gorman 				if (locked_pgdat)
777599d0c95SMel Gorman 					spin_unlock_irqrestore(&locked_pgdat->lru_lock,
778902aaed0SHisashi Hifumi 									flags);
779aabfb572SMichal Hocko 				lock_batch = 0;
780599d0c95SMel Gorman 				locked_pgdat = pgdat;
781599d0c95SMel Gorman 				spin_lock_irqsave(&locked_pgdat->lru_lock, flags);
7821da177e4SLinus Torvalds 			}
783fa9add64SHugh Dickins 
784599d0c95SMel Gorman 			lruvec = mem_cgroup_page_lruvec(page, locked_pgdat);
785309381feSSasha Levin 			VM_BUG_ON_PAGE(!PageLRU(page), page);
78667453911SNick Piggin 			__ClearPageLRU(page);
787fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, page_off_lru(page));
78846453a6eSNick Piggin 		}
78946453a6eSNick Piggin 
790c53954a0SMel Gorman 		/* Clear Active bit in case of parallel mark_page_accessed */
791e3741b50SMel Gorman 		__ClearPageActive(page);
79262906027SNicholas Piggin 		__ClearPageWaiters(page);
793c53954a0SMel Gorman 
794cc59850eSKonstantin Khlebnikov 		list_add(&page->lru, &pages_to_free);
7951da177e4SLinus Torvalds 	}
796599d0c95SMel Gorman 	if (locked_pgdat)
797599d0c95SMel Gorman 		spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
7981da177e4SLinus Torvalds 
799747db954SJohannes Weiner 	mem_cgroup_uncharge_list(&pages_to_free);
8002d4894b5SMel Gorman 	free_unref_page_list(&pages_to_free);
8011da177e4SLinus Torvalds }
8020be8557bSMiklos Szeredi EXPORT_SYMBOL(release_pages);
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds /*
8051da177e4SLinus Torvalds  * The pages which we're about to release may be in the deferred lru-addition
8061da177e4SLinus Torvalds  * queues.  That would prevent them from really being freed right now.  That's
8071da177e4SLinus Torvalds  * OK from a correctness point of view but is inefficient - those pages may be
8081da177e4SLinus Torvalds  * cache-warm and we want to give them back to the page allocator ASAP.
8091da177e4SLinus Torvalds  *
8101da177e4SLinus Torvalds  * So __pagevec_release() will drain those queues here.  __pagevec_lru_add()
8111da177e4SLinus Torvalds  * and __pagevec_lru_add_active() call release_pages() directly to avoid
8121da177e4SLinus Torvalds  * mutual recursion.
8131da177e4SLinus Torvalds  */
8141da177e4SLinus Torvalds void __pagevec_release(struct pagevec *pvec)
8151da177e4SLinus Torvalds {
8167f0b5fb9SMel Gorman 	if (!pvec->percpu_pvec_drained) {
8171da177e4SLinus Torvalds 		lru_add_drain();
8187f0b5fb9SMel Gorman 		pvec->percpu_pvec_drained = true;
819d9ed0d08SMel Gorman 	}
820c6f92f9fSMel Gorman 	release_pages(pvec->pages, pagevec_count(pvec));
8211da177e4SLinus Torvalds 	pagevec_reinit(pvec);
8221da177e4SLinus Torvalds }
8237f285701SSteve French EXPORT_SYMBOL(__pagevec_release);
8247f285701SSteve French 
82512d27107SHugh Dickins #ifdef CONFIG_TRANSPARENT_HUGEPAGE
82671e3aac0SAndrea Arcangeli /* used by __split_huge_page_refcount() */
827fa9add64SHugh Dickins void lru_add_page_tail(struct page *page, struct page *page_tail,
8285bc7b8acSShaohua Li 		       struct lruvec *lruvec, struct list_head *list)
82971e3aac0SAndrea Arcangeli {
83071e3aac0SAndrea Arcangeli 	const int file = 0;
83171e3aac0SAndrea Arcangeli 
832309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageHead(page), page);
833309381feSSasha Levin 	VM_BUG_ON_PAGE(PageCompound(page_tail), page);
834309381feSSasha Levin 	VM_BUG_ON_PAGE(PageLRU(page_tail), page);
83535f3aa39SLance Roy 	lockdep_assert_held(&lruvec_pgdat(lruvec)->lru_lock);
83671e3aac0SAndrea Arcangeli 
8375bc7b8acSShaohua Li 	if (!list)
83871e3aac0SAndrea Arcangeli 		SetPageLRU(page_tail);
83971e3aac0SAndrea Arcangeli 
84012d27107SHugh Dickins 	if (likely(PageLRU(page)))
84112d27107SHugh Dickins 		list_add_tail(&page_tail->lru, &page->lru);
8425bc7b8acSShaohua Li 	else if (list) {
8435bc7b8acSShaohua Li 		/* page reclaim is reclaiming a huge page */
8445bc7b8acSShaohua Li 		get_page(page_tail);
8455bc7b8acSShaohua Li 		list_add_tail(&page_tail->lru, list);
8465bc7b8acSShaohua Li 	} else {
84712d27107SHugh Dickins 		struct list_head *list_head;
84812d27107SHugh Dickins 		/*
84912d27107SHugh Dickins 		 * Head page has not yet been counted, as an hpage,
85012d27107SHugh Dickins 		 * so we must account for each subpage individually.
85112d27107SHugh Dickins 		 *
85212d27107SHugh Dickins 		 * Use the standard add function to put page_tail on the list,
85312d27107SHugh Dickins 		 * but then correct its position so they all end up in order.
85412d27107SHugh Dickins 		 */
855e180cf80SKirill A. Shutemov 		add_page_to_lru_list(page_tail, lruvec, page_lru(page_tail));
85612d27107SHugh Dickins 		list_head = page_tail->lru.prev;
85712d27107SHugh Dickins 		list_move_tail(&page_tail->lru, list_head);
85871e3aac0SAndrea Arcangeli 	}
8597512102cSHugh Dickins 
8607512102cSHugh Dickins 	if (!PageUnevictable(page))
861e180cf80SKirill A. Shutemov 		update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
86271e3aac0SAndrea Arcangeli }
86312d27107SHugh Dickins #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
86471e3aac0SAndrea Arcangeli 
865fa9add64SHugh Dickins static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
866fa9add64SHugh Dickins 				 void *arg)
8673dd7ae8eSShaohua Li {
8689c4e6b1aSShakeel Butt 	enum lru_list lru;
8699c4e6b1aSShakeel Butt 	int was_unevictable = TestClearPageUnevictable(page);
8703dd7ae8eSShaohua Li 
871309381feSSasha Levin 	VM_BUG_ON_PAGE(PageLRU(page), page);
8723dd7ae8eSShaohua Li 
8733dd7ae8eSShaohua Li 	SetPageLRU(page);
8749c4e6b1aSShakeel Butt 	/*
8759c4e6b1aSShakeel Butt 	 * Page becomes evictable in two ways:
876dae966dcSPeng Fan 	 * 1) Within LRU lock [munlock_vma_page() and __munlock_pagevec()].
8779c4e6b1aSShakeel Butt 	 * 2) Before acquiring LRU lock to put the page to correct LRU and then
8789c4e6b1aSShakeel Butt 	 *   a) do PageLRU check with lock [check_move_unevictable_pages]
8799c4e6b1aSShakeel Butt 	 *   b) do PageLRU check before lock [clear_page_mlock]
8809c4e6b1aSShakeel Butt 	 *
8819c4e6b1aSShakeel Butt 	 * (1) & (2a) are ok as LRU lock will serialize them. For (2b), we need
8829c4e6b1aSShakeel Butt 	 * following strict ordering:
8839c4e6b1aSShakeel Butt 	 *
8849c4e6b1aSShakeel Butt 	 * #0: __pagevec_lru_add_fn		#1: clear_page_mlock
8859c4e6b1aSShakeel Butt 	 *
8869c4e6b1aSShakeel Butt 	 * SetPageLRU()				TestClearPageMlocked()
8879c4e6b1aSShakeel Butt 	 * smp_mb() // explicit ordering	// above provides strict
8889c4e6b1aSShakeel Butt 	 *					// ordering
8899c4e6b1aSShakeel Butt 	 * PageMlocked()			PageLRU()
8909c4e6b1aSShakeel Butt 	 *
8919c4e6b1aSShakeel Butt 	 *
8929c4e6b1aSShakeel Butt 	 * if '#1' does not observe setting of PG_lru by '#0' and fails
8939c4e6b1aSShakeel Butt 	 * isolation, the explicit barrier will make sure that page_evictable
8949c4e6b1aSShakeel Butt 	 * check will put the page in correct LRU. Without smp_mb(), SetPageLRU
8959c4e6b1aSShakeel Butt 	 * can be reordered after PageMlocked check and can make '#1' to fail
8969c4e6b1aSShakeel Butt 	 * the isolation of the page whose Mlocked bit is cleared (#0 is also
8979c4e6b1aSShakeel Butt 	 * looking at the same page) and the evictable page will be stranded
8989c4e6b1aSShakeel Butt 	 * in an unevictable LRU.
8999c4e6b1aSShakeel Butt 	 */
9009c4e6b1aSShakeel Butt 	smp_mb();
9019c4e6b1aSShakeel Butt 
9029c4e6b1aSShakeel Butt 	if (page_evictable(page)) {
9039c4e6b1aSShakeel Butt 		lru = page_lru(page);
9049c4e6b1aSShakeel Butt 		update_page_reclaim_stat(lruvec, page_is_file_cache(page),
9059c4e6b1aSShakeel Butt 					 PageActive(page));
9069c4e6b1aSShakeel Butt 		if (was_unevictable)
9079c4e6b1aSShakeel Butt 			count_vm_event(UNEVICTABLE_PGRESCUED);
9089c4e6b1aSShakeel Butt 	} else {
9099c4e6b1aSShakeel Butt 		lru = LRU_UNEVICTABLE;
9109c4e6b1aSShakeel Butt 		ClearPageActive(page);
9119c4e6b1aSShakeel Butt 		SetPageUnevictable(page);
9129c4e6b1aSShakeel Butt 		if (!was_unevictable)
9139c4e6b1aSShakeel Butt 			count_vm_event(UNEVICTABLE_PGCULLED);
9149c4e6b1aSShakeel Butt 	}
9159c4e6b1aSShakeel Butt 
916fa9add64SHugh Dickins 	add_page_to_lru_list(page, lruvec, lru);
91724b7e581SMel Gorman 	trace_mm_lru_insertion(page, lru);
9183dd7ae8eSShaohua Li }
9193dd7ae8eSShaohua Li 
9201da177e4SLinus Torvalds /*
9211da177e4SLinus Torvalds  * Add the passed pages to the LRU, then drop the caller's refcount
9221da177e4SLinus Torvalds  * on them.  Reinitialises the caller's pagevec.
9231da177e4SLinus Torvalds  */
924a0b8cab3SMel Gorman void __pagevec_lru_add(struct pagevec *pvec)
9251da177e4SLinus Torvalds {
926a0b8cab3SMel Gorman 	pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
9271da177e4SLinus Torvalds }
9285095ae83SHugh Dickins EXPORT_SYMBOL(__pagevec_lru_add);
929f04e9ebbSKOSAKI Motohiro 
9301da177e4SLinus Torvalds /**
9310cd6144aSJohannes Weiner  * pagevec_lookup_entries - gang pagecache lookup
9320cd6144aSJohannes Weiner  * @pvec:	Where the resulting entries are placed
9330cd6144aSJohannes Weiner  * @mapping:	The address_space to search
9340cd6144aSJohannes Weiner  * @start:	The starting entry index
935cb6f0f34SMike Rapoport  * @nr_entries:	The maximum number of pages
9360cd6144aSJohannes Weiner  * @indices:	The cache indices corresponding to the entries in @pvec
9370cd6144aSJohannes Weiner  *
9380cd6144aSJohannes Weiner  * pagevec_lookup_entries() will search for and return a group of up
939f144c390SMike Rapoport  * to @nr_pages pages and shadow entries in the mapping.  All
9400cd6144aSJohannes Weiner  * entries are placed in @pvec.  pagevec_lookup_entries() takes a
9410cd6144aSJohannes Weiner  * reference against actual pages in @pvec.
9420cd6144aSJohannes Weiner  *
9430cd6144aSJohannes Weiner  * The search returns a group of mapping-contiguous entries with
9440cd6144aSJohannes Weiner  * ascending indexes.  There may be holes in the indices due to
9450cd6144aSJohannes Weiner  * not-present entries.
9460cd6144aSJohannes Weiner  *
9470cd6144aSJohannes Weiner  * pagevec_lookup_entries() returns the number of entries which were
9480cd6144aSJohannes Weiner  * found.
9490cd6144aSJohannes Weiner  */
9500cd6144aSJohannes Weiner unsigned pagevec_lookup_entries(struct pagevec *pvec,
9510cd6144aSJohannes Weiner 				struct address_space *mapping,
952e02a9f04SRandy Dunlap 				pgoff_t start, unsigned nr_entries,
9530cd6144aSJohannes Weiner 				pgoff_t *indices)
9540cd6144aSJohannes Weiner {
955e02a9f04SRandy Dunlap 	pvec->nr = find_get_entries(mapping, start, nr_entries,
9560cd6144aSJohannes Weiner 				    pvec->pages, indices);
9570cd6144aSJohannes Weiner 	return pagevec_count(pvec);
9580cd6144aSJohannes Weiner }
9590cd6144aSJohannes Weiner 
9600cd6144aSJohannes Weiner /**
9610cd6144aSJohannes Weiner  * pagevec_remove_exceptionals - pagevec exceptionals pruning
9620cd6144aSJohannes Weiner  * @pvec:	The pagevec to prune
9630cd6144aSJohannes Weiner  *
9640cd6144aSJohannes Weiner  * pagevec_lookup_entries() fills both pages and exceptional radix
9650cd6144aSJohannes Weiner  * tree entries into the pagevec.  This function prunes all
9660cd6144aSJohannes Weiner  * exceptionals from @pvec without leaving holes, so that it can be
9670cd6144aSJohannes Weiner  * passed on to page-only pagevec operations.
9680cd6144aSJohannes Weiner  */
9690cd6144aSJohannes Weiner void pagevec_remove_exceptionals(struct pagevec *pvec)
9700cd6144aSJohannes Weiner {
9710cd6144aSJohannes Weiner 	int i, j;
9720cd6144aSJohannes Weiner 
9730cd6144aSJohannes Weiner 	for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
9740cd6144aSJohannes Weiner 		struct page *page = pvec->pages[i];
9753159f943SMatthew Wilcox 		if (!xa_is_value(page))
9760cd6144aSJohannes Weiner 			pvec->pages[j++] = page;
9770cd6144aSJohannes Weiner 	}
9780cd6144aSJohannes Weiner 	pvec->nr = j;
9790cd6144aSJohannes Weiner }
9800cd6144aSJohannes Weiner 
9810cd6144aSJohannes Weiner /**
982b947cee4SJan Kara  * pagevec_lookup_range - gang pagecache lookup
9831da177e4SLinus Torvalds  * @pvec:	Where the resulting pages are placed
9841da177e4SLinus Torvalds  * @mapping:	The address_space to search
9851da177e4SLinus Torvalds  * @start:	The starting page index
986b947cee4SJan Kara  * @end:	The final page index
9871da177e4SLinus Torvalds  *
988e02a9f04SRandy Dunlap  * pagevec_lookup_range() will search for & return a group of up to PAGEVEC_SIZE
989b947cee4SJan Kara  * pages in the mapping starting from index @start and upto index @end
990b947cee4SJan Kara  * (inclusive).  The pages are placed in @pvec.  pagevec_lookup() takes a
9911da177e4SLinus Torvalds  * reference against the pages in @pvec.
9921da177e4SLinus Torvalds  *
9931da177e4SLinus Torvalds  * The search returns a group of mapping-contiguous pages with ascending
994d72dc8a2SJan Kara  * indexes.  There may be holes in the indices due to not-present pages. We
995d72dc8a2SJan Kara  * also update @start to index the next page for the traversal.
9961da177e4SLinus Torvalds  *
997b947cee4SJan Kara  * pagevec_lookup_range() returns the number of pages which were found. If this
998e02a9f04SRandy Dunlap  * number is smaller than PAGEVEC_SIZE, the end of specified range has been
999b947cee4SJan Kara  * reached.
10001da177e4SLinus Torvalds  */
1001b947cee4SJan Kara unsigned pagevec_lookup_range(struct pagevec *pvec,
1002397162ffSJan Kara 		struct address_space *mapping, pgoff_t *start, pgoff_t end)
10031da177e4SLinus Torvalds {
1004397162ffSJan Kara 	pvec->nr = find_get_pages_range(mapping, start, end, PAGEVEC_SIZE,
1005b947cee4SJan Kara 					pvec->pages);
10061da177e4SLinus Torvalds 	return pagevec_count(pvec);
10071da177e4SLinus Torvalds }
1008b947cee4SJan Kara EXPORT_SYMBOL(pagevec_lookup_range);
100978539fdfSChristoph Hellwig 
101072b045aeSJan Kara unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
101172b045aeSJan Kara 		struct address_space *mapping, pgoff_t *index, pgoff_t end,
101210bbd235SMatthew Wilcox 		xa_mark_t tag)
10131da177e4SLinus Torvalds {
101472b045aeSJan Kara 	pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
101567fd707fSJan Kara 					PAGEVEC_SIZE, pvec->pages);
10161da177e4SLinus Torvalds 	return pagevec_count(pvec);
10171da177e4SLinus Torvalds }
101872b045aeSJan Kara EXPORT_SYMBOL(pagevec_lookup_range_tag);
10191da177e4SLinus Torvalds 
102093d3b714SJan Kara unsigned pagevec_lookup_range_nr_tag(struct pagevec *pvec,
102193d3b714SJan Kara 		struct address_space *mapping, pgoff_t *index, pgoff_t end,
102210bbd235SMatthew Wilcox 		xa_mark_t tag, unsigned max_pages)
102393d3b714SJan Kara {
102493d3b714SJan Kara 	pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
102593d3b714SJan Kara 		min_t(unsigned int, max_pages, PAGEVEC_SIZE), pvec->pages);
102693d3b714SJan Kara 	return pagevec_count(pvec);
102793d3b714SJan Kara }
102893d3b714SJan Kara EXPORT_SYMBOL(pagevec_lookup_range_nr_tag);
10291da177e4SLinus Torvalds /*
10301da177e4SLinus Torvalds  * Perform any setup for the swap system
10311da177e4SLinus Torvalds  */
10321da177e4SLinus Torvalds void __init swap_setup(void)
10331da177e4SLinus Torvalds {
1034ca79b0c2SArun KS 	unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
1035e0bf68ddSPeter Zijlstra 
10361da177e4SLinus Torvalds 	/* Use a smaller cluster for small-memory machines */
10371da177e4SLinus Torvalds 	if (megs < 16)
10381da177e4SLinus Torvalds 		page_cluster = 2;
10391da177e4SLinus Torvalds 	else
10401da177e4SLinus Torvalds 		page_cluster = 3;
10411da177e4SLinus Torvalds 	/*
10421da177e4SLinus Torvalds 	 * Right now other parts of the system means that we
10431da177e4SLinus Torvalds 	 * _really_ don't want to cluster much more
10441da177e4SLinus Torvalds 	 */
10451da177e4SLinus Torvalds }
1046