xref: /linux/mm/swap.c (revision 6ea183d60c469560e7b08a83c9804299e84ec9eb)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/mm/swap.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
8183ff22bSSimon Arlott  * This file contains the default values for the operation of the
91da177e4SLinus Torvalds  * Linux VM subsystem. Fine-tuning documentation can be found in
101da177e4SLinus Torvalds  * Documentation/sysctl/vm.txt.
111da177e4SLinus Torvalds  * Started 18.12.91
121da177e4SLinus Torvalds  * Swap aging added 23.2.95, Stephen Tweedie.
131da177e4SLinus Torvalds  * Buffermem limits added 12.3.98, Rik van Riel.
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/mm.h>
171da177e4SLinus Torvalds #include <linux/sched.h>
181da177e4SLinus Torvalds #include <linux/kernel_stat.h>
191da177e4SLinus Torvalds #include <linux/swap.h>
201da177e4SLinus Torvalds #include <linux/mman.h>
211da177e4SLinus Torvalds #include <linux/pagemap.h>
221da177e4SLinus Torvalds #include <linux/pagevec.h>
231da177e4SLinus Torvalds #include <linux/init.h>
24b95f1b31SPaul Gortmaker #include <linux/export.h>
251da177e4SLinus Torvalds #include <linux/mm_inline.h>
261da177e4SLinus Torvalds #include <linux/percpu_counter.h>
273565fce3SDan Williams #include <linux/memremap.h>
281da177e4SLinus Torvalds #include <linux/percpu.h>
291da177e4SLinus Torvalds #include <linux/cpu.h>
301da177e4SLinus Torvalds #include <linux/notifier.h>
31e0bf68ddSPeter Zijlstra #include <linux/backing-dev.h>
3266e1707bSBalbir Singh #include <linux/memcontrol.h>
335a0e3ad6STejun Heo #include <linux/gfp.h>
34a27bb332SKent Overstreet #include <linux/uio.h>
35822fc613SNaoya Horiguchi #include <linux/hugetlb.h>
3633c3fc71SVladimir Davydov #include <linux/page_idle.h>
371da177e4SLinus Torvalds 
3864d6519dSLee Schermerhorn #include "internal.h"
3964d6519dSLee Schermerhorn 
40c6286c98SMel Gorman #define CREATE_TRACE_POINTS
41c6286c98SMel Gorman #include <trace/events/pagemap.h>
42c6286c98SMel Gorman 
431da177e4SLinus Torvalds /* How many pages do we try to swap or page in/out together? */
441da177e4SLinus Torvalds int page_cluster;
451da177e4SLinus Torvalds 
4613f7f789SMel Gorman static DEFINE_PER_CPU(struct pagevec, lru_add_pvec);
47f84f9504SVegard Nossum static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
48cc5993bdSMinchan Kim static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
49f7ad2a6cSShaohua Li static DEFINE_PER_CPU(struct pagevec, lru_lazyfree_pvecs);
50a4a921aaSMing Li #ifdef CONFIG_SMP
51a4a921aaSMing Li static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
52a4a921aaSMing Li #endif
53902aaed0SHisashi Hifumi 
54b221385bSAdrian Bunk /*
55b221385bSAdrian Bunk  * This path almost never happens for VM activity - pages are normally
56b221385bSAdrian Bunk  * freed via pagevecs.  But it gets used by networking.
57b221385bSAdrian Bunk  */
58920c7a5dSHarvey Harrison static void __page_cache_release(struct page *page)
59b221385bSAdrian Bunk {
60b221385bSAdrian Bunk 	if (PageLRU(page)) {
61b221385bSAdrian Bunk 		struct zone *zone = page_zone(page);
62fa9add64SHugh Dickins 		struct lruvec *lruvec;
63fa9add64SHugh Dickins 		unsigned long flags;
64b221385bSAdrian Bunk 
65a52633d8SMel Gorman 		spin_lock_irqsave(zone_lru_lock(zone), flags);
66599d0c95SMel Gorman 		lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
67309381feSSasha Levin 		VM_BUG_ON_PAGE(!PageLRU(page), page);
68b221385bSAdrian Bunk 		__ClearPageLRU(page);
69fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, page_off_lru(page));
70a52633d8SMel Gorman 		spin_unlock_irqrestore(zone_lru_lock(zone), flags);
71b221385bSAdrian Bunk 	}
7262906027SNicholas Piggin 	__ClearPageWaiters(page);
730a31bc97SJohannes Weiner 	mem_cgroup_uncharge(page);
7491807063SAndrea Arcangeli }
7591807063SAndrea Arcangeli 
7691807063SAndrea Arcangeli static void __put_single_page(struct page *page)
7791807063SAndrea Arcangeli {
7891807063SAndrea Arcangeli 	__page_cache_release(page);
792d4894b5SMel Gorman 	free_unref_page(page);
80b221385bSAdrian Bunk }
81b221385bSAdrian Bunk 
8291807063SAndrea Arcangeli static void __put_compound_page(struct page *page)
8391807063SAndrea Arcangeli {
8491807063SAndrea Arcangeli 	compound_page_dtor *dtor;
8591807063SAndrea Arcangeli 
86822fc613SNaoya Horiguchi 	/*
87822fc613SNaoya Horiguchi 	 * __page_cache_release() is supposed to be called for thp, not for
88822fc613SNaoya Horiguchi 	 * hugetlb. This is because hugetlb page does never have PageLRU set
89822fc613SNaoya Horiguchi 	 * (it's never listed to any LRU lists) and no memcg routines should
90822fc613SNaoya Horiguchi 	 * be called for hugetlb (it has a separate hugetlb_cgroup.)
91822fc613SNaoya Horiguchi 	 */
92822fc613SNaoya Horiguchi 	if (!PageHuge(page))
9391807063SAndrea Arcangeli 		__page_cache_release(page);
9491807063SAndrea Arcangeli 	dtor = get_compound_page_dtor(page);
9591807063SAndrea Arcangeli 	(*dtor)(page);
9691807063SAndrea Arcangeli }
9791807063SAndrea Arcangeli 
98ddc58f27SKirill A. Shutemov void __put_page(struct page *page)
99c747ce79SJianyu Zhan {
10071389703SDan Williams 	if (is_zone_device_page(page)) {
10171389703SDan Williams 		put_dev_pagemap(page->pgmap);
10271389703SDan Williams 
10371389703SDan Williams 		/*
10471389703SDan Williams 		 * The page belongs to the device that created pgmap. Do
10571389703SDan Williams 		 * not return it to page allocator.
10671389703SDan Williams 		 */
10771389703SDan Williams 		return;
10871389703SDan Williams 	}
10971389703SDan Williams 
110ddc58f27SKirill A. Shutemov 	if (unlikely(PageCompound(page)))
11126296ad2SAndrew Morton 		__put_compound_page(page);
11226296ad2SAndrew Morton 	else
11326296ad2SAndrew Morton 		__put_single_page(page);
11426296ad2SAndrew Morton }
115ddc58f27SKirill A. Shutemov EXPORT_SYMBOL(__put_page);
11670b50f94SAndrea Arcangeli 
1171d7ea732SAlexander Zarochentsev /**
1187682486bSRandy Dunlap  * put_pages_list() - release a list of pages
1197682486bSRandy Dunlap  * @pages: list of pages threaded on page->lru
1201d7ea732SAlexander Zarochentsev  *
1211d7ea732SAlexander Zarochentsev  * Release a list of pages which are strung together on page.lru.  Currently
1221d7ea732SAlexander Zarochentsev  * used by read_cache_pages() and related error recovery code.
1231d7ea732SAlexander Zarochentsev  */
1241d7ea732SAlexander Zarochentsev void put_pages_list(struct list_head *pages)
1251d7ea732SAlexander Zarochentsev {
1261d7ea732SAlexander Zarochentsev 	while (!list_empty(pages)) {
1271d7ea732SAlexander Zarochentsev 		struct page *victim;
1281d7ea732SAlexander Zarochentsev 
129f86196eaSNikolay Borisov 		victim = lru_to_page(pages);
1301d7ea732SAlexander Zarochentsev 		list_del(&victim->lru);
13109cbfeafSKirill A. Shutemov 		put_page(victim);
1321d7ea732SAlexander Zarochentsev 	}
1331d7ea732SAlexander Zarochentsev }
1341d7ea732SAlexander Zarochentsev EXPORT_SYMBOL(put_pages_list);
1351d7ea732SAlexander Zarochentsev 
13618022c5dSMel Gorman /*
13718022c5dSMel Gorman  * get_kernel_pages() - pin kernel pages in memory
13818022c5dSMel Gorman  * @kiov:	An array of struct kvec structures
13918022c5dSMel Gorman  * @nr_segs:	number of segments to pin
14018022c5dSMel Gorman  * @write:	pinning for read/write, currently ignored
14118022c5dSMel Gorman  * @pages:	array that receives pointers to the pages pinned.
14218022c5dSMel Gorman  *		Should be at least nr_segs long.
14318022c5dSMel Gorman  *
14418022c5dSMel Gorman  * Returns number of pages pinned. This may be fewer than the number
14518022c5dSMel Gorman  * requested. If nr_pages is 0 or negative, returns 0. If no pages
14618022c5dSMel Gorman  * were pinned, returns -errno. Each page returned must be released
14718022c5dSMel Gorman  * with a put_page() call when it is finished with.
14818022c5dSMel Gorman  */
14918022c5dSMel Gorman int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
15018022c5dSMel Gorman 		struct page **pages)
15118022c5dSMel Gorman {
15218022c5dSMel Gorman 	int seg;
15318022c5dSMel Gorman 
15418022c5dSMel Gorman 	for (seg = 0; seg < nr_segs; seg++) {
15518022c5dSMel Gorman 		if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
15618022c5dSMel Gorman 			return seg;
15718022c5dSMel Gorman 
1585a178119SMel Gorman 		pages[seg] = kmap_to_page(kiov[seg].iov_base);
15909cbfeafSKirill A. Shutemov 		get_page(pages[seg]);
16018022c5dSMel Gorman 	}
16118022c5dSMel Gorman 
16218022c5dSMel Gorman 	return seg;
16318022c5dSMel Gorman }
16418022c5dSMel Gorman EXPORT_SYMBOL_GPL(get_kernel_pages);
16518022c5dSMel Gorman 
16618022c5dSMel Gorman /*
16718022c5dSMel Gorman  * get_kernel_page() - pin a kernel page in memory
16818022c5dSMel Gorman  * @start:	starting kernel address
16918022c5dSMel Gorman  * @write:	pinning for read/write, currently ignored
17018022c5dSMel Gorman  * @pages:	array that receives pointer to the page pinned.
17118022c5dSMel Gorman  *		Must be at least nr_segs long.
17218022c5dSMel Gorman  *
17318022c5dSMel Gorman  * Returns 1 if page is pinned. If the page was not pinned, returns
17418022c5dSMel Gorman  * -errno. The page returned must be released with a put_page() call
17518022c5dSMel Gorman  * when it is finished with.
17618022c5dSMel Gorman  */
17718022c5dSMel Gorman int get_kernel_page(unsigned long start, int write, struct page **pages)
17818022c5dSMel Gorman {
17918022c5dSMel Gorman 	const struct kvec kiov = {
18018022c5dSMel Gorman 		.iov_base = (void *)start,
18118022c5dSMel Gorman 		.iov_len = PAGE_SIZE
18218022c5dSMel Gorman 	};
18318022c5dSMel Gorman 
18418022c5dSMel Gorman 	return get_kernel_pages(&kiov, 1, write, pages);
18518022c5dSMel Gorman }
18618022c5dSMel Gorman EXPORT_SYMBOL_GPL(get_kernel_page);
18718022c5dSMel Gorman 
1883dd7ae8eSShaohua Li static void pagevec_lru_move_fn(struct pagevec *pvec,
189fa9add64SHugh Dickins 	void (*move_fn)(struct page *page, struct lruvec *lruvec, void *arg),
1903dd7ae8eSShaohua Li 	void *arg)
191902aaed0SHisashi Hifumi {
192902aaed0SHisashi Hifumi 	int i;
19368eb0731SMel Gorman 	struct pglist_data *pgdat = NULL;
194fa9add64SHugh Dickins 	struct lruvec *lruvec;
1953dd7ae8eSShaohua Li 	unsigned long flags = 0;
196902aaed0SHisashi Hifumi 
197902aaed0SHisashi Hifumi 	for (i = 0; i < pagevec_count(pvec); i++) {
198902aaed0SHisashi Hifumi 		struct page *page = pvec->pages[i];
19968eb0731SMel Gorman 		struct pglist_data *pagepgdat = page_pgdat(page);
200902aaed0SHisashi Hifumi 
20168eb0731SMel Gorman 		if (pagepgdat != pgdat) {
20268eb0731SMel Gorman 			if (pgdat)
20368eb0731SMel Gorman 				spin_unlock_irqrestore(&pgdat->lru_lock, flags);
20468eb0731SMel Gorman 			pgdat = pagepgdat;
20568eb0731SMel Gorman 			spin_lock_irqsave(&pgdat->lru_lock, flags);
206902aaed0SHisashi Hifumi 		}
2073dd7ae8eSShaohua Li 
20868eb0731SMel Gorman 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
209fa9add64SHugh Dickins 		(*move_fn)(page, lruvec, arg);
2103dd7ae8eSShaohua Li 	}
21168eb0731SMel Gorman 	if (pgdat)
21268eb0731SMel Gorman 		spin_unlock_irqrestore(&pgdat->lru_lock, flags);
213c6f92f9fSMel Gorman 	release_pages(pvec->pages, pvec->nr);
2143dd7ae8eSShaohua Li 	pagevec_reinit(pvec);
2153dd7ae8eSShaohua Li }
2163dd7ae8eSShaohua Li 
217fa9add64SHugh Dickins static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec,
218fa9add64SHugh Dickins 				 void *arg)
2193dd7ae8eSShaohua Li {
2203dd7ae8eSShaohua Li 	int *pgmoved = arg;
2213dd7ae8eSShaohua Li 
222c55e8d03SJohannes Weiner 	if (PageLRU(page) && !PageUnevictable(page)) {
223c55e8d03SJohannes Weiner 		del_page_from_lru_list(page, lruvec, page_lru(page));
224c55e8d03SJohannes Weiner 		ClearPageActive(page);
225c55e8d03SJohannes Weiner 		add_page_to_lru_list_tail(page, lruvec, page_lru(page));
2263dd7ae8eSShaohua Li 		(*pgmoved)++;
227902aaed0SHisashi Hifumi 	}
228902aaed0SHisashi Hifumi }
2293dd7ae8eSShaohua Li 
2303dd7ae8eSShaohua Li /*
2313dd7ae8eSShaohua Li  * pagevec_move_tail() must be called with IRQ disabled.
2323dd7ae8eSShaohua Li  * Otherwise this may cause nasty races.
2333dd7ae8eSShaohua Li  */
2343dd7ae8eSShaohua Li static void pagevec_move_tail(struct pagevec *pvec)
2353dd7ae8eSShaohua Li {
2363dd7ae8eSShaohua Li 	int pgmoved = 0;
2373dd7ae8eSShaohua Li 
2383dd7ae8eSShaohua Li 	pagevec_lru_move_fn(pvec, pagevec_move_tail_fn, &pgmoved);
239902aaed0SHisashi Hifumi 	__count_vm_events(PGROTATED, pgmoved);
240902aaed0SHisashi Hifumi }
241902aaed0SHisashi Hifumi 
242902aaed0SHisashi Hifumi /*
2431da177e4SLinus Torvalds  * Writeback is about to end against a page which has been marked for immediate
2441da177e4SLinus Torvalds  * reclaim.  If it still appears to be reclaimable, move it to the tail of the
245902aaed0SHisashi Hifumi  * inactive list.
2461da177e4SLinus Torvalds  */
247ac6aadb2SMiklos Szeredi void rotate_reclaimable_page(struct page *page)
2481da177e4SLinus Torvalds {
249c55e8d03SJohannes Weiner 	if (!PageLocked(page) && !PageDirty(page) &&
250894bc310SLee Schermerhorn 	    !PageUnevictable(page) && PageLRU(page)) {
251902aaed0SHisashi Hifumi 		struct pagevec *pvec;
2521da177e4SLinus Torvalds 		unsigned long flags;
2531da177e4SLinus Torvalds 
25409cbfeafSKirill A. Shutemov 		get_page(page);
255902aaed0SHisashi Hifumi 		local_irq_save(flags);
2567c8e0181SChristoph Lameter 		pvec = this_cpu_ptr(&lru_rotate_pvecs);
2578f182270SLukasz Odzioba 		if (!pagevec_add(pvec, page) || PageCompound(page))
258902aaed0SHisashi Hifumi 			pagevec_move_tail(pvec);
259902aaed0SHisashi Hifumi 		local_irq_restore(flags);
260ac6aadb2SMiklos Szeredi 	}
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds 
263fa9add64SHugh Dickins static void update_page_reclaim_stat(struct lruvec *lruvec,
2643e2f41f1SKOSAKI Motohiro 				     int file, int rotated)
2653e2f41f1SKOSAKI Motohiro {
266fa9add64SHugh Dickins 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
2673e2f41f1SKOSAKI Motohiro 
2683e2f41f1SKOSAKI Motohiro 	reclaim_stat->recent_scanned[file]++;
2693e2f41f1SKOSAKI Motohiro 	if (rotated)
2703e2f41f1SKOSAKI Motohiro 		reclaim_stat->recent_rotated[file]++;
2713e2f41f1SKOSAKI Motohiro }
2723e2f41f1SKOSAKI Motohiro 
273fa9add64SHugh Dickins static void __activate_page(struct page *page, struct lruvec *lruvec,
274fa9add64SHugh Dickins 			    void *arg)
275744ed144SShaohua Li {
2767a608572SLinus Torvalds 	if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
277744ed144SShaohua Li 		int file = page_is_file_cache(page);
278744ed144SShaohua Li 		int lru = page_lru_base_type(page);
279744ed144SShaohua Li 
280fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, lru);
281744ed144SShaohua Li 		SetPageActive(page);
282744ed144SShaohua Li 		lru += LRU_ACTIVE;
283fa9add64SHugh Dickins 		add_page_to_lru_list(page, lruvec, lru);
28424b7e581SMel Gorman 		trace_mm_lru_activate(page);
2857a608572SLinus Torvalds 
286fa9add64SHugh Dickins 		__count_vm_event(PGACTIVATE);
287fa9add64SHugh Dickins 		update_page_reclaim_stat(lruvec, file, 1);
288744ed144SShaohua Li 	}
289eb709b0dSShaohua Li }
290eb709b0dSShaohua Li 
291eb709b0dSShaohua Li #ifdef CONFIG_SMP
292eb709b0dSShaohua Li static void activate_page_drain(int cpu)
293eb709b0dSShaohua Li {
294eb709b0dSShaohua Li 	struct pagevec *pvec = &per_cpu(activate_page_pvecs, cpu);
295eb709b0dSShaohua Li 
296eb709b0dSShaohua Li 	if (pagevec_count(pvec))
297eb709b0dSShaohua Li 		pagevec_lru_move_fn(pvec, __activate_page, NULL);
298eb709b0dSShaohua Li }
299eb709b0dSShaohua Li 
3005fbc4616SChris Metcalf static bool need_activate_page_drain(int cpu)
3015fbc4616SChris Metcalf {
3025fbc4616SChris Metcalf 	return pagevec_count(&per_cpu(activate_page_pvecs, cpu)) != 0;
3035fbc4616SChris Metcalf }
3045fbc4616SChris Metcalf 
305eb709b0dSShaohua Li void activate_page(struct page *page)
306eb709b0dSShaohua Li {
307800d8c63SKirill A. Shutemov 	page = compound_head(page);
308eb709b0dSShaohua Li 	if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
309eb709b0dSShaohua Li 		struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
310eb709b0dSShaohua Li 
31109cbfeafSKirill A. Shutemov 		get_page(page);
3128f182270SLukasz Odzioba 		if (!pagevec_add(pvec, page) || PageCompound(page))
313eb709b0dSShaohua Li 			pagevec_lru_move_fn(pvec, __activate_page, NULL);
314eb709b0dSShaohua Li 		put_cpu_var(activate_page_pvecs);
315eb709b0dSShaohua Li 	}
316eb709b0dSShaohua Li }
317eb709b0dSShaohua Li 
318eb709b0dSShaohua Li #else
319eb709b0dSShaohua Li static inline void activate_page_drain(int cpu)
320eb709b0dSShaohua Li {
321eb709b0dSShaohua Li }
322eb709b0dSShaohua Li 
323eb709b0dSShaohua Li void activate_page(struct page *page)
324eb709b0dSShaohua Li {
325eb709b0dSShaohua Li 	struct zone *zone = page_zone(page);
326eb709b0dSShaohua Li 
327800d8c63SKirill A. Shutemov 	page = compound_head(page);
328a52633d8SMel Gorman 	spin_lock_irq(zone_lru_lock(zone));
329599d0c95SMel Gorman 	__activate_page(page, mem_cgroup_page_lruvec(page, zone->zone_pgdat), NULL);
330a52633d8SMel Gorman 	spin_unlock_irq(zone_lru_lock(zone));
3311da177e4SLinus Torvalds }
332eb709b0dSShaohua Li #endif
3331da177e4SLinus Torvalds 
334059285a2SMel Gorman static void __lru_cache_activate_page(struct page *page)
335059285a2SMel Gorman {
336059285a2SMel Gorman 	struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
337059285a2SMel Gorman 	int i;
338059285a2SMel Gorman 
339059285a2SMel Gorman 	/*
340059285a2SMel Gorman 	 * Search backwards on the optimistic assumption that the page being
341059285a2SMel Gorman 	 * activated has just been added to this pagevec. Note that only
342059285a2SMel Gorman 	 * the local pagevec is examined as a !PageLRU page could be in the
343059285a2SMel Gorman 	 * process of being released, reclaimed, migrated or on a remote
344059285a2SMel Gorman 	 * pagevec that is currently being drained. Furthermore, marking
345059285a2SMel Gorman 	 * a remote pagevec's page PageActive potentially hits a race where
346059285a2SMel Gorman 	 * a page is marked PageActive just after it is added to the inactive
347059285a2SMel Gorman 	 * list causing accounting errors and BUG_ON checks to trigger.
348059285a2SMel Gorman 	 */
349059285a2SMel Gorman 	for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
350059285a2SMel Gorman 		struct page *pagevec_page = pvec->pages[i];
351059285a2SMel Gorman 
352059285a2SMel Gorman 		if (pagevec_page == page) {
353059285a2SMel Gorman 			SetPageActive(page);
354059285a2SMel Gorman 			break;
355059285a2SMel Gorman 		}
356059285a2SMel Gorman 	}
357059285a2SMel Gorman 
358059285a2SMel Gorman 	put_cpu_var(lru_add_pvec);
359059285a2SMel Gorman }
360059285a2SMel Gorman 
3611da177e4SLinus Torvalds /*
3621da177e4SLinus Torvalds  * Mark a page as having seen activity.
3631da177e4SLinus Torvalds  *
3641da177e4SLinus Torvalds  * inactive,unreferenced	->	inactive,referenced
3651da177e4SLinus Torvalds  * inactive,referenced		->	active,unreferenced
3661da177e4SLinus Torvalds  * active,unreferenced		->	active,referenced
367eb39d618SHugh Dickins  *
368eb39d618SHugh Dickins  * When a newly allocated page is not yet visible, so safe for non-atomic ops,
369eb39d618SHugh Dickins  * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
3701da177e4SLinus Torvalds  */
371920c7a5dSHarvey Harrison void mark_page_accessed(struct page *page)
3721da177e4SLinus Torvalds {
373e90309c9SKirill A. Shutemov 	page = compound_head(page);
374894bc310SLee Schermerhorn 	if (!PageActive(page) && !PageUnevictable(page) &&
375059285a2SMel Gorman 			PageReferenced(page)) {
376059285a2SMel Gorman 
377059285a2SMel Gorman 		/*
378059285a2SMel Gorman 		 * If the page is on the LRU, queue it for activation via
379059285a2SMel Gorman 		 * activate_page_pvecs. Otherwise, assume the page is on a
380059285a2SMel Gorman 		 * pagevec, mark it active and it'll be moved to the active
381059285a2SMel Gorman 		 * LRU on the next drain.
382059285a2SMel Gorman 		 */
383059285a2SMel Gorman 		if (PageLRU(page))
3841da177e4SLinus Torvalds 			activate_page(page);
385059285a2SMel Gorman 		else
386059285a2SMel Gorman 			__lru_cache_activate_page(page);
3871da177e4SLinus Torvalds 		ClearPageReferenced(page);
388a528910eSJohannes Weiner 		if (page_is_file_cache(page))
389a528910eSJohannes Weiner 			workingset_activation(page);
3901da177e4SLinus Torvalds 	} else if (!PageReferenced(page)) {
3911da177e4SLinus Torvalds 		SetPageReferenced(page);
3921da177e4SLinus Torvalds 	}
39333c3fc71SVladimir Davydov 	if (page_is_idle(page))
39433c3fc71SVladimir Davydov 		clear_page_idle(page);
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds EXPORT_SYMBOL(mark_page_accessed);
3971da177e4SLinus Torvalds 
3982329d375SJianyu Zhan static void __lru_cache_add(struct page *page)
3991da177e4SLinus Torvalds {
40013f7f789SMel Gorman 	struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
40113f7f789SMel Gorman 
40209cbfeafSKirill A. Shutemov 	get_page(page);
4038f182270SLukasz Odzioba 	if (!pagevec_add(pvec, page) || PageCompound(page))
404a0b8cab3SMel Gorman 		__pagevec_lru_add(pvec);
40513f7f789SMel Gorman 	put_cpu_var(lru_add_pvec);
4061da177e4SLinus Torvalds }
4072329d375SJianyu Zhan 
4082329d375SJianyu Zhan /**
409e02a9f04SRandy Dunlap  * lru_cache_add_anon - add a page to the page lists
4102329d375SJianyu Zhan  * @page: the page to add
4112329d375SJianyu Zhan  */
4122329d375SJianyu Zhan void lru_cache_add_anon(struct page *page)
4132329d375SJianyu Zhan {
4146fb81a17SMel Gorman 	if (PageActive(page))
4152329d375SJianyu Zhan 		ClearPageActive(page);
4162329d375SJianyu Zhan 	__lru_cache_add(page);
4172329d375SJianyu Zhan }
4182329d375SJianyu Zhan 
4192329d375SJianyu Zhan void lru_cache_add_file(struct page *page)
4202329d375SJianyu Zhan {
4216fb81a17SMel Gorman 	if (PageActive(page))
4222329d375SJianyu Zhan 		ClearPageActive(page);
4232329d375SJianyu Zhan 	__lru_cache_add(page);
4242329d375SJianyu Zhan }
4252329d375SJianyu Zhan EXPORT_SYMBOL(lru_cache_add_file);
4261da177e4SLinus Torvalds 
427f04e9ebbSKOSAKI Motohiro /**
428c53954a0SMel Gorman  * lru_cache_add - add a page to a page list
429f04e9ebbSKOSAKI Motohiro  * @page: the page to be added to the LRU.
4302329d375SJianyu Zhan  *
4312329d375SJianyu Zhan  * Queue the page for addition to the LRU via pagevec. The decision on whether
4322329d375SJianyu Zhan  * to add the page to the [in]active [file|anon] list is deferred until the
4332329d375SJianyu Zhan  * pagevec is drained. This gives a chance for the caller of lru_cache_add()
4342329d375SJianyu Zhan  * have the page added to the active list using mark_page_accessed().
435f04e9ebbSKOSAKI Motohiro  */
436c53954a0SMel Gorman void lru_cache_add(struct page *page)
4371da177e4SLinus Torvalds {
438309381feSSasha Levin 	VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
439309381feSSasha Levin 	VM_BUG_ON_PAGE(PageLRU(page), page);
440c53954a0SMel Gorman 	__lru_cache_add(page);
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
443894bc310SLee Schermerhorn /**
44400501b53SJohannes Weiner  * lru_cache_add_active_or_unevictable
44500501b53SJohannes Weiner  * @page:  the page to be added to LRU
44600501b53SJohannes Weiner  * @vma:   vma in which page is mapped for determining reclaimability
44700501b53SJohannes Weiner  *
44800501b53SJohannes Weiner  * Place @page on the active or unevictable LRU list, depending on its
44900501b53SJohannes Weiner  * evictability.  Note that if the page is not evictable, it goes
45000501b53SJohannes Weiner  * directly back onto it's zone's unevictable list, it does NOT use a
45100501b53SJohannes Weiner  * per cpu pagevec.
45200501b53SJohannes Weiner  */
45300501b53SJohannes Weiner void lru_cache_add_active_or_unevictable(struct page *page,
45400501b53SJohannes Weiner 					 struct vm_area_struct *vma)
45500501b53SJohannes Weiner {
45600501b53SJohannes Weiner 	VM_BUG_ON_PAGE(PageLRU(page), page);
45700501b53SJohannes Weiner 
4589c4e6b1aSShakeel Butt 	if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED))
45900501b53SJohannes Weiner 		SetPageActive(page);
4609c4e6b1aSShakeel Butt 	else if (!TestSetPageMlocked(page)) {
46100501b53SJohannes Weiner 		/*
46200501b53SJohannes Weiner 		 * We use the irq-unsafe __mod_zone_page_stat because this
46300501b53SJohannes Weiner 		 * counter is not modified from interrupt context, and the pte
46400501b53SJohannes Weiner 		 * lock is held(spinlock), which implies preemption disabled.
46500501b53SJohannes Weiner 		 */
46600501b53SJohannes Weiner 		__mod_zone_page_state(page_zone(page), NR_MLOCK,
46700501b53SJohannes Weiner 				    hpage_nr_pages(page));
46800501b53SJohannes Weiner 		count_vm_event(UNEVICTABLE_PGMLOCKED);
46900501b53SJohannes Weiner 	}
4709c4e6b1aSShakeel Butt 	lru_cache_add(page);
47100501b53SJohannes Weiner }
47200501b53SJohannes Weiner 
473902aaed0SHisashi Hifumi /*
47431560180SMinchan Kim  * If the page can not be invalidated, it is moved to the
47531560180SMinchan Kim  * inactive list to speed up its reclaim.  It is moved to the
47631560180SMinchan Kim  * head of the list, rather than the tail, to give the flusher
47731560180SMinchan Kim  * threads some time to write it out, as this is much more
47831560180SMinchan Kim  * effective than the single-page writeout from reclaim.
479278df9f4SMinchan Kim  *
480278df9f4SMinchan Kim  * If the page isn't page_mapped and dirty/writeback, the page
481278df9f4SMinchan Kim  * could reclaim asap using PG_reclaim.
482278df9f4SMinchan Kim  *
483278df9f4SMinchan Kim  * 1. active, mapped page -> none
484278df9f4SMinchan Kim  * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
485278df9f4SMinchan Kim  * 3. inactive, mapped page -> none
486278df9f4SMinchan Kim  * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
487278df9f4SMinchan Kim  * 5. inactive, clean -> inactive, tail
488278df9f4SMinchan Kim  * 6. Others -> none
489278df9f4SMinchan Kim  *
490278df9f4SMinchan Kim  * In 4, why it moves inactive's head, the VM expects the page would
491278df9f4SMinchan Kim  * be write it out by flusher threads as this is much more effective
492278df9f4SMinchan Kim  * than the single-page writeout from reclaim.
49331560180SMinchan Kim  */
494cc5993bdSMinchan Kim static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
495fa9add64SHugh Dickins 			      void *arg)
49631560180SMinchan Kim {
49731560180SMinchan Kim 	int lru, file;
498278df9f4SMinchan Kim 	bool active;
49931560180SMinchan Kim 
500278df9f4SMinchan Kim 	if (!PageLRU(page))
50131560180SMinchan Kim 		return;
50231560180SMinchan Kim 
503bad49d9cSMinchan Kim 	if (PageUnevictable(page))
504bad49d9cSMinchan Kim 		return;
505bad49d9cSMinchan Kim 
50631560180SMinchan Kim 	/* Some processes are using the page */
50731560180SMinchan Kim 	if (page_mapped(page))
50831560180SMinchan Kim 		return;
50931560180SMinchan Kim 
510278df9f4SMinchan Kim 	active = PageActive(page);
51131560180SMinchan Kim 	file = page_is_file_cache(page);
51231560180SMinchan Kim 	lru = page_lru_base_type(page);
513fa9add64SHugh Dickins 
514fa9add64SHugh Dickins 	del_page_from_lru_list(page, lruvec, lru + active);
51531560180SMinchan Kim 	ClearPageActive(page);
51631560180SMinchan Kim 	ClearPageReferenced(page);
517fa9add64SHugh Dickins 	add_page_to_lru_list(page, lruvec, lru);
51831560180SMinchan Kim 
519278df9f4SMinchan Kim 	if (PageWriteback(page) || PageDirty(page)) {
520278df9f4SMinchan Kim 		/*
521278df9f4SMinchan Kim 		 * PG_reclaim could be raced with end_page_writeback
522278df9f4SMinchan Kim 		 * It can make readahead confusing.  But race window
523278df9f4SMinchan Kim 		 * is _really_ small and  it's non-critical problem.
524278df9f4SMinchan Kim 		 */
525278df9f4SMinchan Kim 		SetPageReclaim(page);
526278df9f4SMinchan Kim 	} else {
527278df9f4SMinchan Kim 		/*
528278df9f4SMinchan Kim 		 * The page's writeback ends up during pagevec
529278df9f4SMinchan Kim 		 * We moves tha page into tail of inactive.
530278df9f4SMinchan Kim 		 */
531925b7673SJohannes Weiner 		list_move_tail(&page->lru, &lruvec->lists[lru]);
532278df9f4SMinchan Kim 		__count_vm_event(PGROTATED);
533278df9f4SMinchan Kim 	}
534278df9f4SMinchan Kim 
535278df9f4SMinchan Kim 	if (active)
536278df9f4SMinchan Kim 		__count_vm_event(PGDEACTIVATE);
537fa9add64SHugh Dickins 	update_page_reclaim_stat(lruvec, file, 0);
53831560180SMinchan Kim }
53931560180SMinchan Kim 
54010853a03SMinchan Kim 
541f7ad2a6cSShaohua Li static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec,
54210853a03SMinchan Kim 			    void *arg)
54310853a03SMinchan Kim {
544f7ad2a6cSShaohua Li 	if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
54524c92eb7SShaohua Li 	    !PageSwapCache(page) && !PageUnevictable(page)) {
546f7ad2a6cSShaohua Li 		bool active = PageActive(page);
54710853a03SMinchan Kim 
548f7ad2a6cSShaohua Li 		del_page_from_lru_list(page, lruvec,
549f7ad2a6cSShaohua Li 				       LRU_INACTIVE_ANON + active);
55010853a03SMinchan Kim 		ClearPageActive(page);
55110853a03SMinchan Kim 		ClearPageReferenced(page);
552f7ad2a6cSShaohua Li 		/*
553f7ad2a6cSShaohua Li 		 * lazyfree pages are clean anonymous pages. They have
554f7ad2a6cSShaohua Li 		 * SwapBacked flag cleared to distinguish normal anonymous
555f7ad2a6cSShaohua Li 		 * pages
556f7ad2a6cSShaohua Li 		 */
557f7ad2a6cSShaohua Li 		ClearPageSwapBacked(page);
558f7ad2a6cSShaohua Li 		add_page_to_lru_list(page, lruvec, LRU_INACTIVE_FILE);
55910853a03SMinchan Kim 
560f7ad2a6cSShaohua Li 		__count_vm_events(PGLAZYFREE, hpage_nr_pages(page));
5612262185cSRoman Gushchin 		count_memcg_page_event(page, PGLAZYFREE);
562f7ad2a6cSShaohua Li 		update_page_reclaim_stat(lruvec, 1, 0);
56310853a03SMinchan Kim 	}
56410853a03SMinchan Kim }
56510853a03SMinchan Kim 
56631560180SMinchan Kim /*
567902aaed0SHisashi Hifumi  * Drain pages out of the cpu's pagevecs.
568902aaed0SHisashi Hifumi  * Either "cpu" is the current CPU, and preemption has already been
569902aaed0SHisashi Hifumi  * disabled; or "cpu" is being hot-unplugged, and is already dead.
570902aaed0SHisashi Hifumi  */
571f0cb3c76SKonstantin Khlebnikov void lru_add_drain_cpu(int cpu)
5721da177e4SLinus Torvalds {
57313f7f789SMel Gorman 	struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	if (pagevec_count(pvec))
576a0b8cab3SMel Gorman 		__pagevec_lru_add(pvec);
577902aaed0SHisashi Hifumi 
578902aaed0SHisashi Hifumi 	pvec = &per_cpu(lru_rotate_pvecs, cpu);
579902aaed0SHisashi Hifumi 	if (pagevec_count(pvec)) {
580902aaed0SHisashi Hifumi 		unsigned long flags;
581902aaed0SHisashi Hifumi 
582902aaed0SHisashi Hifumi 		/* No harm done if a racing interrupt already did this */
583902aaed0SHisashi Hifumi 		local_irq_save(flags);
584902aaed0SHisashi Hifumi 		pagevec_move_tail(pvec);
585902aaed0SHisashi Hifumi 		local_irq_restore(flags);
586902aaed0SHisashi Hifumi 	}
58731560180SMinchan Kim 
588cc5993bdSMinchan Kim 	pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
58931560180SMinchan Kim 	if (pagevec_count(pvec))
590cc5993bdSMinchan Kim 		pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
591eb709b0dSShaohua Li 
592f7ad2a6cSShaohua Li 	pvec = &per_cpu(lru_lazyfree_pvecs, cpu);
59310853a03SMinchan Kim 	if (pagevec_count(pvec))
594f7ad2a6cSShaohua Li 		pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
59510853a03SMinchan Kim 
596eb709b0dSShaohua Li 	activate_page_drain(cpu);
59731560180SMinchan Kim }
59831560180SMinchan Kim 
59931560180SMinchan Kim /**
600cc5993bdSMinchan Kim  * deactivate_file_page - forcefully deactivate a file page
60131560180SMinchan Kim  * @page: page to deactivate
60231560180SMinchan Kim  *
60331560180SMinchan Kim  * This function hints the VM that @page is a good reclaim candidate,
60431560180SMinchan Kim  * for example if its invalidation fails due to the page being dirty
60531560180SMinchan Kim  * or under writeback.
60631560180SMinchan Kim  */
607cc5993bdSMinchan Kim void deactivate_file_page(struct page *page)
60831560180SMinchan Kim {
609821ed6bbSMinchan Kim 	/*
610cc5993bdSMinchan Kim 	 * In a workload with many unevictable page such as mprotect,
611cc5993bdSMinchan Kim 	 * unevictable page deactivation for accelerating reclaim is pointless.
612821ed6bbSMinchan Kim 	 */
613821ed6bbSMinchan Kim 	if (PageUnevictable(page))
614821ed6bbSMinchan Kim 		return;
615821ed6bbSMinchan Kim 
61631560180SMinchan Kim 	if (likely(get_page_unless_zero(page))) {
617cc5993bdSMinchan Kim 		struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
61831560180SMinchan Kim 
6198f182270SLukasz Odzioba 		if (!pagevec_add(pvec, page) || PageCompound(page))
620cc5993bdSMinchan Kim 			pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
621cc5993bdSMinchan Kim 		put_cpu_var(lru_deactivate_file_pvecs);
62231560180SMinchan Kim 	}
62380bfed90SAndrew Morton }
62480bfed90SAndrew Morton 
62510853a03SMinchan Kim /**
626f7ad2a6cSShaohua Li  * mark_page_lazyfree - make an anon page lazyfree
62710853a03SMinchan Kim  * @page: page to deactivate
62810853a03SMinchan Kim  *
629f7ad2a6cSShaohua Li  * mark_page_lazyfree() moves @page to the inactive file list.
630f7ad2a6cSShaohua Li  * This is done to accelerate the reclaim of @page.
63110853a03SMinchan Kim  */
632f7ad2a6cSShaohua Li void mark_page_lazyfree(struct page *page)
63310853a03SMinchan Kim {
634f7ad2a6cSShaohua Li 	if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
63524c92eb7SShaohua Li 	    !PageSwapCache(page) && !PageUnevictable(page)) {
636f7ad2a6cSShaohua Li 		struct pagevec *pvec = &get_cpu_var(lru_lazyfree_pvecs);
63710853a03SMinchan Kim 
63809cbfeafSKirill A. Shutemov 		get_page(page);
6398f182270SLukasz Odzioba 		if (!pagevec_add(pvec, page) || PageCompound(page))
640f7ad2a6cSShaohua Li 			pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
641f7ad2a6cSShaohua Li 		put_cpu_var(lru_lazyfree_pvecs);
64210853a03SMinchan Kim 	}
64310853a03SMinchan Kim }
64410853a03SMinchan Kim 
64580bfed90SAndrew Morton void lru_add_drain(void)
64680bfed90SAndrew Morton {
647f0cb3c76SKonstantin Khlebnikov 	lru_add_drain_cpu(get_cpu());
64880bfed90SAndrew Morton 	put_cpu();
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
651*6ea183d6SMichal Hocko #ifdef CONFIG_SMP
652*6ea183d6SMichal Hocko 
653*6ea183d6SMichal Hocko static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
654*6ea183d6SMichal Hocko 
655c4028958SDavid Howells static void lru_add_drain_per_cpu(struct work_struct *dummy)
656053837fcSNick Piggin {
657053837fcSNick Piggin 	lru_add_drain();
658053837fcSNick Piggin }
659053837fcSNick Piggin 
6609852a721SMichal Hocko /*
6619852a721SMichal Hocko  * Doesn't need any cpu hotplug locking because we do rely on per-cpu
6629852a721SMichal Hocko  * kworkers being shut down before our page_alloc_cpu_dead callback is
6639852a721SMichal Hocko  * executed on the offlined cpu.
6649852a721SMichal Hocko  * Calling this function with cpu hotplug locks held can actually lead
6659852a721SMichal Hocko  * to obscure indirect dependencies via WQ context.
6669852a721SMichal Hocko  */
6679852a721SMichal Hocko void lru_add_drain_all(void)
668053837fcSNick Piggin {
6695fbc4616SChris Metcalf 	static DEFINE_MUTEX(lock);
6705fbc4616SChris Metcalf 	static struct cpumask has_work;
6715fbc4616SChris Metcalf 	int cpu;
6725fbc4616SChris Metcalf 
673ce612879SMichal Hocko 	/*
674ce612879SMichal Hocko 	 * Make sure nobody triggers this path before mm_percpu_wq is fully
675ce612879SMichal Hocko 	 * initialized.
676ce612879SMichal Hocko 	 */
677ce612879SMichal Hocko 	if (WARN_ON(!mm_percpu_wq))
678ce612879SMichal Hocko 		return;
679ce612879SMichal Hocko 
6805fbc4616SChris Metcalf 	mutex_lock(&lock);
6815fbc4616SChris Metcalf 	cpumask_clear(&has_work);
6825fbc4616SChris Metcalf 
6835fbc4616SChris Metcalf 	for_each_online_cpu(cpu) {
6845fbc4616SChris Metcalf 		struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
6855fbc4616SChris Metcalf 
6865fbc4616SChris Metcalf 		if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
6875fbc4616SChris Metcalf 		    pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
688cc5993bdSMinchan Kim 		    pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
689f7ad2a6cSShaohua Li 		    pagevec_count(&per_cpu(lru_lazyfree_pvecs, cpu)) ||
6905fbc4616SChris Metcalf 		    need_activate_page_drain(cpu)) {
6915fbc4616SChris Metcalf 			INIT_WORK(work, lru_add_drain_per_cpu);
692ce612879SMichal Hocko 			queue_work_on(cpu, mm_percpu_wq, work);
6935fbc4616SChris Metcalf 			cpumask_set_cpu(cpu, &has_work);
6945fbc4616SChris Metcalf 		}
6955fbc4616SChris Metcalf 	}
6965fbc4616SChris Metcalf 
6975fbc4616SChris Metcalf 	for_each_cpu(cpu, &has_work)
6985fbc4616SChris Metcalf 		flush_work(&per_cpu(lru_add_drain_work, cpu));
6995fbc4616SChris Metcalf 
7005fbc4616SChris Metcalf 	mutex_unlock(&lock);
701053837fcSNick Piggin }
702*6ea183d6SMichal Hocko #else
703*6ea183d6SMichal Hocko void lru_add_drain_all(void)
704*6ea183d6SMichal Hocko {
705*6ea183d6SMichal Hocko 	lru_add_drain();
706*6ea183d6SMichal Hocko }
707*6ea183d6SMichal Hocko #endif
708053837fcSNick Piggin 
709aabfb572SMichal Hocko /**
710ea1754a0SKirill A. Shutemov  * release_pages - batched put_page()
711aabfb572SMichal Hocko  * @pages: array of pages to release
712aabfb572SMichal Hocko  * @nr: number of pages
7131da177e4SLinus Torvalds  *
714aabfb572SMichal Hocko  * Decrement the reference count on all the pages in @pages.  If it
715aabfb572SMichal Hocko  * fell to zero, remove the page from the LRU and free it.
7161da177e4SLinus Torvalds  */
717c6f92f9fSMel Gorman void release_pages(struct page **pages, int nr)
7181da177e4SLinus Torvalds {
7191da177e4SLinus Torvalds 	int i;
720cc59850eSKonstantin Khlebnikov 	LIST_HEAD(pages_to_free);
721599d0c95SMel Gorman 	struct pglist_data *locked_pgdat = NULL;
722fa9add64SHugh Dickins 	struct lruvec *lruvec;
723902aaed0SHisashi Hifumi 	unsigned long uninitialized_var(flags);
724aabfb572SMichal Hocko 	unsigned int uninitialized_var(lock_batch);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	for (i = 0; i < nr; i++) {
7271da177e4SLinus Torvalds 		struct page *page = pages[i];
7281da177e4SLinus Torvalds 
729aabfb572SMichal Hocko 		/*
730aabfb572SMichal Hocko 		 * Make sure the IRQ-safe lock-holding time does not get
731aabfb572SMichal Hocko 		 * excessive with a continuous string of pages from the
732599d0c95SMel Gorman 		 * same pgdat. The lock is held only if pgdat != NULL.
733aabfb572SMichal Hocko 		 */
734599d0c95SMel Gorman 		if (locked_pgdat && ++lock_batch == SWAP_CLUSTER_MAX) {
735599d0c95SMel Gorman 			spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
736599d0c95SMel Gorman 			locked_pgdat = NULL;
737aabfb572SMichal Hocko 		}
738aabfb572SMichal Hocko 
7396fcb52a5SAaron Lu 		if (is_huge_zero_page(page))
740aa88b68cSKirill A. Shutemov 			continue;
741aa88b68cSKirill A. Shutemov 
742df6ad698SJérôme Glisse 		/* Device public page can not be huge page */
743df6ad698SJérôme Glisse 		if (is_device_public_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 			}
749e7638488SDan Williams 			put_devmap_managed_page(page);
750df6ad698SJérôme Glisse 			continue;
751df6ad698SJérôme Glisse 		}
752df6ad698SJérôme Glisse 
753ddc58f27SKirill A. Shutemov 		page = compound_head(page);
754b5810039SNick Piggin 		if (!put_page_testzero(page))
7551da177e4SLinus Torvalds 			continue;
7561da177e4SLinus Torvalds 
757ddc58f27SKirill A. Shutemov 		if (PageCompound(page)) {
758599d0c95SMel Gorman 			if (locked_pgdat) {
759599d0c95SMel Gorman 				spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
760599d0c95SMel Gorman 				locked_pgdat = NULL;
761ddc58f27SKirill A. Shutemov 			}
762ddc58f27SKirill A. Shutemov 			__put_compound_page(page);
763ddc58f27SKirill A. Shutemov 			continue;
764ddc58f27SKirill A. Shutemov 		}
765ddc58f27SKirill A. Shutemov 
76646453a6eSNick Piggin 		if (PageLRU(page)) {
767599d0c95SMel Gorman 			struct pglist_data *pgdat = page_pgdat(page);
768894bc310SLee Schermerhorn 
769599d0c95SMel Gorman 			if (pgdat != locked_pgdat) {
770599d0c95SMel Gorman 				if (locked_pgdat)
771599d0c95SMel Gorman 					spin_unlock_irqrestore(&locked_pgdat->lru_lock,
772902aaed0SHisashi Hifumi 									flags);
773aabfb572SMichal Hocko 				lock_batch = 0;
774599d0c95SMel Gorman 				locked_pgdat = pgdat;
775599d0c95SMel Gorman 				spin_lock_irqsave(&locked_pgdat->lru_lock, flags);
7761da177e4SLinus Torvalds 			}
777fa9add64SHugh Dickins 
778599d0c95SMel Gorman 			lruvec = mem_cgroup_page_lruvec(page, locked_pgdat);
779309381feSSasha Levin 			VM_BUG_ON_PAGE(!PageLRU(page), page);
78067453911SNick Piggin 			__ClearPageLRU(page);
781fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, page_off_lru(page));
78246453a6eSNick Piggin 		}
78346453a6eSNick Piggin 
784c53954a0SMel Gorman 		/* Clear Active bit in case of parallel mark_page_accessed */
785e3741b50SMel Gorman 		__ClearPageActive(page);
78662906027SNicholas Piggin 		__ClearPageWaiters(page);
787c53954a0SMel Gorman 
788cc59850eSKonstantin Khlebnikov 		list_add(&page->lru, &pages_to_free);
7891da177e4SLinus Torvalds 	}
790599d0c95SMel Gorman 	if (locked_pgdat)
791599d0c95SMel Gorman 		spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
7921da177e4SLinus Torvalds 
793747db954SJohannes Weiner 	mem_cgroup_uncharge_list(&pages_to_free);
7942d4894b5SMel Gorman 	free_unref_page_list(&pages_to_free);
7951da177e4SLinus Torvalds }
7960be8557bSMiklos Szeredi EXPORT_SYMBOL(release_pages);
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds /*
7991da177e4SLinus Torvalds  * The pages which we're about to release may be in the deferred lru-addition
8001da177e4SLinus Torvalds  * queues.  That would prevent them from really being freed right now.  That's
8011da177e4SLinus Torvalds  * OK from a correctness point of view but is inefficient - those pages may be
8021da177e4SLinus Torvalds  * cache-warm and we want to give them back to the page allocator ASAP.
8031da177e4SLinus Torvalds  *
8041da177e4SLinus Torvalds  * So __pagevec_release() will drain those queues here.  __pagevec_lru_add()
8051da177e4SLinus Torvalds  * and __pagevec_lru_add_active() call release_pages() directly to avoid
8061da177e4SLinus Torvalds  * mutual recursion.
8071da177e4SLinus Torvalds  */
8081da177e4SLinus Torvalds void __pagevec_release(struct pagevec *pvec)
8091da177e4SLinus Torvalds {
8107f0b5fb9SMel Gorman 	if (!pvec->percpu_pvec_drained) {
8111da177e4SLinus Torvalds 		lru_add_drain();
8127f0b5fb9SMel Gorman 		pvec->percpu_pvec_drained = true;
813d9ed0d08SMel Gorman 	}
814c6f92f9fSMel Gorman 	release_pages(pvec->pages, pagevec_count(pvec));
8151da177e4SLinus Torvalds 	pagevec_reinit(pvec);
8161da177e4SLinus Torvalds }
8177f285701SSteve French EXPORT_SYMBOL(__pagevec_release);
8187f285701SSteve French 
81912d27107SHugh Dickins #ifdef CONFIG_TRANSPARENT_HUGEPAGE
82071e3aac0SAndrea Arcangeli /* used by __split_huge_page_refcount() */
821fa9add64SHugh Dickins void lru_add_page_tail(struct page *page, struct page *page_tail,
8225bc7b8acSShaohua Li 		       struct lruvec *lruvec, struct list_head *list)
82371e3aac0SAndrea Arcangeli {
82471e3aac0SAndrea Arcangeli 	const int file = 0;
82571e3aac0SAndrea Arcangeli 
826309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageHead(page), page);
827309381feSSasha Levin 	VM_BUG_ON_PAGE(PageCompound(page_tail), page);
828309381feSSasha Levin 	VM_BUG_ON_PAGE(PageLRU(page_tail), page);
82935f3aa39SLance Roy 	lockdep_assert_held(&lruvec_pgdat(lruvec)->lru_lock);
83071e3aac0SAndrea Arcangeli 
8315bc7b8acSShaohua Li 	if (!list)
83271e3aac0SAndrea Arcangeli 		SetPageLRU(page_tail);
83371e3aac0SAndrea Arcangeli 
83412d27107SHugh Dickins 	if (likely(PageLRU(page)))
83512d27107SHugh Dickins 		list_add_tail(&page_tail->lru, &page->lru);
8365bc7b8acSShaohua Li 	else if (list) {
8375bc7b8acSShaohua Li 		/* page reclaim is reclaiming a huge page */
8385bc7b8acSShaohua Li 		get_page(page_tail);
8395bc7b8acSShaohua Li 		list_add_tail(&page_tail->lru, list);
8405bc7b8acSShaohua Li 	} else {
84112d27107SHugh Dickins 		struct list_head *list_head;
84212d27107SHugh Dickins 		/*
84312d27107SHugh Dickins 		 * Head page has not yet been counted, as an hpage,
84412d27107SHugh Dickins 		 * so we must account for each subpage individually.
84512d27107SHugh Dickins 		 *
84612d27107SHugh Dickins 		 * Use the standard add function to put page_tail on the list,
84712d27107SHugh Dickins 		 * but then correct its position so they all end up in order.
84812d27107SHugh Dickins 		 */
849e180cf80SKirill A. Shutemov 		add_page_to_lru_list(page_tail, lruvec, page_lru(page_tail));
85012d27107SHugh Dickins 		list_head = page_tail->lru.prev;
85112d27107SHugh Dickins 		list_move_tail(&page_tail->lru, list_head);
85271e3aac0SAndrea Arcangeli 	}
8537512102cSHugh Dickins 
8547512102cSHugh Dickins 	if (!PageUnevictable(page))
855e180cf80SKirill A. Shutemov 		update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
85671e3aac0SAndrea Arcangeli }
85712d27107SHugh Dickins #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
85871e3aac0SAndrea Arcangeli 
859fa9add64SHugh Dickins static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
860fa9add64SHugh Dickins 				 void *arg)
8613dd7ae8eSShaohua Li {
8629c4e6b1aSShakeel Butt 	enum lru_list lru;
8639c4e6b1aSShakeel Butt 	int was_unevictable = TestClearPageUnevictable(page);
8643dd7ae8eSShaohua Li 
865309381feSSasha Levin 	VM_BUG_ON_PAGE(PageLRU(page), page);
8663dd7ae8eSShaohua Li 
8673dd7ae8eSShaohua Li 	SetPageLRU(page);
8689c4e6b1aSShakeel Butt 	/*
8699c4e6b1aSShakeel Butt 	 * Page becomes evictable in two ways:
8709c4e6b1aSShakeel Butt 	 * 1) Within LRU lock [munlock_vma_pages() and __munlock_pagevec()].
8719c4e6b1aSShakeel Butt 	 * 2) Before acquiring LRU lock to put the page to correct LRU and then
8729c4e6b1aSShakeel Butt 	 *   a) do PageLRU check with lock [check_move_unevictable_pages]
8739c4e6b1aSShakeel Butt 	 *   b) do PageLRU check before lock [clear_page_mlock]
8749c4e6b1aSShakeel Butt 	 *
8759c4e6b1aSShakeel Butt 	 * (1) & (2a) are ok as LRU lock will serialize them. For (2b), we need
8769c4e6b1aSShakeel Butt 	 * following strict ordering:
8779c4e6b1aSShakeel Butt 	 *
8789c4e6b1aSShakeel Butt 	 * #0: __pagevec_lru_add_fn		#1: clear_page_mlock
8799c4e6b1aSShakeel Butt 	 *
8809c4e6b1aSShakeel Butt 	 * SetPageLRU()				TestClearPageMlocked()
8819c4e6b1aSShakeel Butt 	 * smp_mb() // explicit ordering	// above provides strict
8829c4e6b1aSShakeel Butt 	 *					// ordering
8839c4e6b1aSShakeel Butt 	 * PageMlocked()			PageLRU()
8849c4e6b1aSShakeel Butt 	 *
8859c4e6b1aSShakeel Butt 	 *
8869c4e6b1aSShakeel Butt 	 * if '#1' does not observe setting of PG_lru by '#0' and fails
8879c4e6b1aSShakeel Butt 	 * isolation, the explicit barrier will make sure that page_evictable
8889c4e6b1aSShakeel Butt 	 * check will put the page in correct LRU. Without smp_mb(), SetPageLRU
8899c4e6b1aSShakeel Butt 	 * can be reordered after PageMlocked check and can make '#1' to fail
8909c4e6b1aSShakeel Butt 	 * the isolation of the page whose Mlocked bit is cleared (#0 is also
8919c4e6b1aSShakeel Butt 	 * looking at the same page) and the evictable page will be stranded
8929c4e6b1aSShakeel Butt 	 * in an unevictable LRU.
8939c4e6b1aSShakeel Butt 	 */
8949c4e6b1aSShakeel Butt 	smp_mb();
8959c4e6b1aSShakeel Butt 
8969c4e6b1aSShakeel Butt 	if (page_evictable(page)) {
8979c4e6b1aSShakeel Butt 		lru = page_lru(page);
8989c4e6b1aSShakeel Butt 		update_page_reclaim_stat(lruvec, page_is_file_cache(page),
8999c4e6b1aSShakeel Butt 					 PageActive(page));
9009c4e6b1aSShakeel Butt 		if (was_unevictable)
9019c4e6b1aSShakeel Butt 			count_vm_event(UNEVICTABLE_PGRESCUED);
9029c4e6b1aSShakeel Butt 	} else {
9039c4e6b1aSShakeel Butt 		lru = LRU_UNEVICTABLE;
9049c4e6b1aSShakeel Butt 		ClearPageActive(page);
9059c4e6b1aSShakeel Butt 		SetPageUnevictable(page);
9069c4e6b1aSShakeel Butt 		if (!was_unevictable)
9079c4e6b1aSShakeel Butt 			count_vm_event(UNEVICTABLE_PGCULLED);
9089c4e6b1aSShakeel Butt 	}
9099c4e6b1aSShakeel Butt 
910fa9add64SHugh Dickins 	add_page_to_lru_list(page, lruvec, lru);
91124b7e581SMel Gorman 	trace_mm_lru_insertion(page, lru);
9123dd7ae8eSShaohua Li }
9133dd7ae8eSShaohua Li 
9141da177e4SLinus Torvalds /*
9151da177e4SLinus Torvalds  * Add the passed pages to the LRU, then drop the caller's refcount
9161da177e4SLinus Torvalds  * on them.  Reinitialises the caller's pagevec.
9171da177e4SLinus Torvalds  */
918a0b8cab3SMel Gorman void __pagevec_lru_add(struct pagevec *pvec)
9191da177e4SLinus Torvalds {
920a0b8cab3SMel Gorman 	pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
9211da177e4SLinus Torvalds }
9225095ae83SHugh Dickins EXPORT_SYMBOL(__pagevec_lru_add);
923f04e9ebbSKOSAKI Motohiro 
9241da177e4SLinus Torvalds /**
9250cd6144aSJohannes Weiner  * pagevec_lookup_entries - gang pagecache lookup
9260cd6144aSJohannes Weiner  * @pvec:	Where the resulting entries are placed
9270cd6144aSJohannes Weiner  * @mapping:	The address_space to search
9280cd6144aSJohannes Weiner  * @start:	The starting entry index
929cb6f0f34SMike Rapoport  * @nr_entries:	The maximum number of pages
9300cd6144aSJohannes Weiner  * @indices:	The cache indices corresponding to the entries in @pvec
9310cd6144aSJohannes Weiner  *
9320cd6144aSJohannes Weiner  * pagevec_lookup_entries() will search for and return a group of up
933f144c390SMike Rapoport  * to @nr_pages pages and shadow entries in the mapping.  All
9340cd6144aSJohannes Weiner  * entries are placed in @pvec.  pagevec_lookup_entries() takes a
9350cd6144aSJohannes Weiner  * reference against actual pages in @pvec.
9360cd6144aSJohannes Weiner  *
9370cd6144aSJohannes Weiner  * The search returns a group of mapping-contiguous entries with
9380cd6144aSJohannes Weiner  * ascending indexes.  There may be holes in the indices due to
9390cd6144aSJohannes Weiner  * not-present entries.
9400cd6144aSJohannes Weiner  *
9410cd6144aSJohannes Weiner  * pagevec_lookup_entries() returns the number of entries which were
9420cd6144aSJohannes Weiner  * found.
9430cd6144aSJohannes Weiner  */
9440cd6144aSJohannes Weiner unsigned pagevec_lookup_entries(struct pagevec *pvec,
9450cd6144aSJohannes Weiner 				struct address_space *mapping,
946e02a9f04SRandy Dunlap 				pgoff_t start, unsigned nr_entries,
9470cd6144aSJohannes Weiner 				pgoff_t *indices)
9480cd6144aSJohannes Weiner {
949e02a9f04SRandy Dunlap 	pvec->nr = find_get_entries(mapping, start, nr_entries,
9500cd6144aSJohannes Weiner 				    pvec->pages, indices);
9510cd6144aSJohannes Weiner 	return pagevec_count(pvec);
9520cd6144aSJohannes Weiner }
9530cd6144aSJohannes Weiner 
9540cd6144aSJohannes Weiner /**
9550cd6144aSJohannes Weiner  * pagevec_remove_exceptionals - pagevec exceptionals pruning
9560cd6144aSJohannes Weiner  * @pvec:	The pagevec to prune
9570cd6144aSJohannes Weiner  *
9580cd6144aSJohannes Weiner  * pagevec_lookup_entries() fills both pages and exceptional radix
9590cd6144aSJohannes Weiner  * tree entries into the pagevec.  This function prunes all
9600cd6144aSJohannes Weiner  * exceptionals from @pvec without leaving holes, so that it can be
9610cd6144aSJohannes Weiner  * passed on to page-only pagevec operations.
9620cd6144aSJohannes Weiner  */
9630cd6144aSJohannes Weiner void pagevec_remove_exceptionals(struct pagevec *pvec)
9640cd6144aSJohannes Weiner {
9650cd6144aSJohannes Weiner 	int i, j;
9660cd6144aSJohannes Weiner 
9670cd6144aSJohannes Weiner 	for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
9680cd6144aSJohannes Weiner 		struct page *page = pvec->pages[i];
9693159f943SMatthew Wilcox 		if (!xa_is_value(page))
9700cd6144aSJohannes Weiner 			pvec->pages[j++] = page;
9710cd6144aSJohannes Weiner 	}
9720cd6144aSJohannes Weiner 	pvec->nr = j;
9730cd6144aSJohannes Weiner }
9740cd6144aSJohannes Weiner 
9750cd6144aSJohannes Weiner /**
976b947cee4SJan Kara  * pagevec_lookup_range - gang pagecache lookup
9771da177e4SLinus Torvalds  * @pvec:	Where the resulting pages are placed
9781da177e4SLinus Torvalds  * @mapping:	The address_space to search
9791da177e4SLinus Torvalds  * @start:	The starting page index
980b947cee4SJan Kara  * @end:	The final page index
9811da177e4SLinus Torvalds  *
982e02a9f04SRandy Dunlap  * pagevec_lookup_range() will search for & return a group of up to PAGEVEC_SIZE
983b947cee4SJan Kara  * pages in the mapping starting from index @start and upto index @end
984b947cee4SJan Kara  * (inclusive).  The pages are placed in @pvec.  pagevec_lookup() takes a
9851da177e4SLinus Torvalds  * reference against the pages in @pvec.
9861da177e4SLinus Torvalds  *
9871da177e4SLinus Torvalds  * The search returns a group of mapping-contiguous pages with ascending
988d72dc8a2SJan Kara  * indexes.  There may be holes in the indices due to not-present pages. We
989d72dc8a2SJan Kara  * also update @start to index the next page for the traversal.
9901da177e4SLinus Torvalds  *
991b947cee4SJan Kara  * pagevec_lookup_range() returns the number of pages which were found. If this
992e02a9f04SRandy Dunlap  * number is smaller than PAGEVEC_SIZE, the end of specified range has been
993b947cee4SJan Kara  * reached.
9941da177e4SLinus Torvalds  */
995b947cee4SJan Kara unsigned pagevec_lookup_range(struct pagevec *pvec,
996397162ffSJan Kara 		struct address_space *mapping, pgoff_t *start, pgoff_t end)
9971da177e4SLinus Torvalds {
998397162ffSJan Kara 	pvec->nr = find_get_pages_range(mapping, start, end, PAGEVEC_SIZE,
999b947cee4SJan Kara 					pvec->pages);
10001da177e4SLinus Torvalds 	return pagevec_count(pvec);
10011da177e4SLinus Torvalds }
1002b947cee4SJan Kara EXPORT_SYMBOL(pagevec_lookup_range);
100378539fdfSChristoph Hellwig 
100472b045aeSJan Kara unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
100572b045aeSJan Kara 		struct address_space *mapping, pgoff_t *index, pgoff_t end,
100610bbd235SMatthew Wilcox 		xa_mark_t tag)
10071da177e4SLinus Torvalds {
100872b045aeSJan Kara 	pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
100967fd707fSJan Kara 					PAGEVEC_SIZE, pvec->pages);
10101da177e4SLinus Torvalds 	return pagevec_count(pvec);
10111da177e4SLinus Torvalds }
101272b045aeSJan Kara EXPORT_SYMBOL(pagevec_lookup_range_tag);
10131da177e4SLinus Torvalds 
101493d3b714SJan Kara unsigned pagevec_lookup_range_nr_tag(struct pagevec *pvec,
101593d3b714SJan Kara 		struct address_space *mapping, pgoff_t *index, pgoff_t end,
101610bbd235SMatthew Wilcox 		xa_mark_t tag, unsigned max_pages)
101793d3b714SJan Kara {
101893d3b714SJan Kara 	pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
101993d3b714SJan Kara 		min_t(unsigned int, max_pages, PAGEVEC_SIZE), pvec->pages);
102093d3b714SJan Kara 	return pagevec_count(pvec);
102193d3b714SJan Kara }
102293d3b714SJan Kara EXPORT_SYMBOL(pagevec_lookup_range_nr_tag);
10231da177e4SLinus Torvalds /*
10241da177e4SLinus Torvalds  * Perform any setup for the swap system
10251da177e4SLinus Torvalds  */
10261da177e4SLinus Torvalds void __init swap_setup(void)
10271da177e4SLinus Torvalds {
1028ca79b0c2SArun KS 	unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
1029e0bf68ddSPeter Zijlstra 
10301da177e4SLinus Torvalds 	/* Use a smaller cluster for small-memory machines */
10311da177e4SLinus Torvalds 	if (megs < 16)
10321da177e4SLinus Torvalds 		page_cluster = 2;
10331da177e4SLinus Torvalds 	else
10341da177e4SLinus Torvalds 		page_cluster = 3;
10351da177e4SLinus Torvalds 	/*
10361da177e4SLinus Torvalds 	 * Right now other parts of the system means that we
10371da177e4SLinus Torvalds 	 * _really_ don't want to cluster much more
10381da177e4SLinus Torvalds 	 */
10391da177e4SLinus Torvalds }
1040