xref: /openbmc/linux/mm/swap_state.c (revision 3d2c9087)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/mm/swap_state.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
61da177e4SLinus Torvalds  *  Swap reorganised 29.12.95, Stephen Tweedie
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  Rewritten to use page cache, (C) 1998 Stephen Tweedie
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds #include <linux/mm.h>
115a0e3ad6STejun Heo #include <linux/gfp.h>
121da177e4SLinus Torvalds #include <linux/kernel_stat.h>
131da177e4SLinus Torvalds #include <linux/swap.h>
1446017e95SHugh Dickins #include <linux/swapops.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/pagemap.h>
171da177e4SLinus Torvalds #include <linux/backing-dev.h>
183fb5c298SChristian Ehrhardt #include <linux/blkdev.h>
19b20a3503SChristoph Lameter #include <linux/migrate.h>
204b3ef9daSHuang, Ying #include <linux/vmalloc.h>
2167afa38eSTim Chen #include <linux/swap_slots.h>
2238d8b4e6SHuang Ying #include <linux/huge_mm.h>
2361ef1865SMatthew Wilcox (Oracle) #include <linux/shmem_fs.h>
24243bce09SHugh Dickins #include "internal.h"
25014bb1deSNeilBrown #include "swap.h"
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds /*
281da177e4SLinus Torvalds  * swapper_space is a fiction, retained to simplify the path through
297eaceaccSJens Axboe  * vmscan's shrink_page_list.
301da177e4SLinus Torvalds  */
31f5e54d6eSChristoph Hellwig static const struct address_space_operations swap_aops = {
321da177e4SLinus Torvalds 	.writepage	= swap_writepage,
334c4a7634SNeilBrown 	.dirty_folio	= noop_dirty_folio,
341c93923cSAndrew Morton #ifdef CONFIG_MIGRATION
3554184650SMatthew Wilcox (Oracle) 	.migrate_folio	= migrate_folio,
361c93923cSAndrew Morton #endif
371da177e4SLinus Torvalds };
381da177e4SLinus Torvalds 
39783cb68eSChangbin Du struct address_space *swapper_spaces[MAX_SWAPFILES] __read_mostly;
40783cb68eSChangbin Du static unsigned int nr_swapper_spaces[MAX_SWAPFILES] __read_mostly;
41f5c754d6SColin Ian King static bool enable_vma_readahead __read_mostly = true;
42ec560175SHuang Ying 
43ec560175SHuang Ying #define SWAP_RA_WIN_SHIFT	(PAGE_SHIFT / 2)
44ec560175SHuang Ying #define SWAP_RA_HITS_MASK	((1UL << SWAP_RA_WIN_SHIFT) - 1)
45ec560175SHuang Ying #define SWAP_RA_HITS_MAX	SWAP_RA_HITS_MASK
46ec560175SHuang Ying #define SWAP_RA_WIN_MASK	(~PAGE_MASK & ~SWAP_RA_HITS_MASK)
47ec560175SHuang Ying 
48ec560175SHuang Ying #define SWAP_RA_HITS(v)		((v) & SWAP_RA_HITS_MASK)
49ec560175SHuang Ying #define SWAP_RA_WIN(v)		(((v) & SWAP_RA_WIN_MASK) >> SWAP_RA_WIN_SHIFT)
50ec560175SHuang Ying #define SWAP_RA_ADDR(v)		((v) & PAGE_MASK)
51ec560175SHuang Ying 
52ec560175SHuang Ying #define SWAP_RA_VAL(addr, win, hits)				\
53ec560175SHuang Ying 	(((addr) & PAGE_MASK) |					\
54ec560175SHuang Ying 	 (((win) << SWAP_RA_WIN_SHIFT) & SWAP_RA_WIN_MASK) |	\
55ec560175SHuang Ying 	 ((hits) & SWAP_RA_HITS_MASK))
56ec560175SHuang Ying 
57ec560175SHuang Ying /* Initial readahead hits is 4 to start up with a small window */
58ec560175SHuang Ying #define GET_SWAP_RA_VAL(vma)					\
59ec560175SHuang Ying 	(atomic_long_read(&(vma)->swap_readahead_info) ? : 4)
601da177e4SLinus Torvalds 
61579f8290SShaohua Li static atomic_t swapin_readahead_hits = ATOMIC_INIT(4);
62579f8290SShaohua Li 
show_swap_cache_info(void)631da177e4SLinus Torvalds void show_swap_cache_info(void)
641da177e4SLinus Torvalds {
6533806f06SShaohua Li 	printk("%lu pages in swap cache\n", total_swapcache_pages());
663cb8eaa4SZhangPeng 	printk("Free swap  = %ldkB\n", K(get_nr_swap_pages()));
673cb8eaa4SZhangPeng 	printk("Total swap = %lukB\n", K(total_swap_pages));
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
get_shadow_from_swap_cache(swp_entry_t entry)70aae466b0SJoonsoo Kim void *get_shadow_from_swap_cache(swp_entry_t entry)
71aae466b0SJoonsoo Kim {
72aae466b0SJoonsoo Kim 	struct address_space *address_space = swap_address_space(entry);
73aae466b0SJoonsoo Kim 	pgoff_t idx = swp_offset(entry);
74aae466b0SJoonsoo Kim 	struct page *page;
75aae466b0SJoonsoo Kim 
768c647dd1SMatthew Wilcox (Oracle) 	page = xa_load(&address_space->i_pages, idx);
77aae466b0SJoonsoo Kim 	if (xa_is_value(page))
78aae466b0SJoonsoo Kim 		return page;
79aae466b0SJoonsoo Kim 	return NULL;
80aae466b0SJoonsoo Kim }
81aae466b0SJoonsoo Kim 
821da177e4SLinus Torvalds /*
832bb876b5SMatthew Wilcox (Oracle)  * add_to_swap_cache resembles filemap_add_folio on swapper_space,
841da177e4SLinus Torvalds  * but sets SwapCache flag and private instead of mapping and index.
851da177e4SLinus Torvalds  */
add_to_swap_cache(struct folio * folio,swp_entry_t entry,gfp_t gfp,void ** shadowp)86a4c366f0SMatthew Wilcox (Oracle) int add_to_swap_cache(struct folio *folio, swp_entry_t entry,
873852f676SJoonsoo Kim 			gfp_t gfp, void **shadowp)
881da177e4SLinus Torvalds {
898d93b41cSMatthew Wilcox 	struct address_space *address_space = swap_address_space(entry);
9038d8b4e6SHuang Ying 	pgoff_t idx = swp_offset(entry);
91a4c366f0SMatthew Wilcox (Oracle) 	XA_STATE_ORDER(xas, &address_space->i_pages, idx, folio_order(folio));
92a4c366f0SMatthew Wilcox (Oracle) 	unsigned long i, nr = folio_nr_pages(folio);
933852f676SJoonsoo Kim 	void *old;
941da177e4SLinus Torvalds 
955649d113SYang Yang 	xas_set_update(&xas, workingset_update_node);
965649d113SYang Yang 
97a4c366f0SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
98a4c366f0SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_swapcache(folio), folio);
99a4c366f0SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(!folio_test_swapbacked(folio), folio);
10051726b12SHugh Dickins 
101a4c366f0SMatthew Wilcox (Oracle) 	folio_ref_add(folio, nr);
102a4c366f0SMatthew Wilcox (Oracle) 	folio_set_swapcache(folio);
103*3d2c9087SDavid Hildenbrand 	folio->swap = entry;
104e286781dSNick Piggin 
1058d93b41cSMatthew Wilcox 	do {
1068d93b41cSMatthew Wilcox 		xas_lock_irq(&xas);
1078d93b41cSMatthew Wilcox 		xas_create_range(&xas);
1088d93b41cSMatthew Wilcox 		if (xas_error(&xas))
1098d93b41cSMatthew Wilcox 			goto unlock;
11038d8b4e6SHuang Ying 		for (i = 0; i < nr; i++) {
111a4c366f0SMatthew Wilcox (Oracle) 			VM_BUG_ON_FOLIO(xas.xa_index != idx + i, folio);
1123852f676SJoonsoo Kim 			old = xas_load(&xas);
1133852f676SJoonsoo Kim 			if (xa_is_value(old)) {
1143852f676SJoonsoo Kim 				if (shadowp)
1153852f676SJoonsoo Kim 					*shadowp = old;
1163852f676SJoonsoo Kim 			}
117a4c366f0SMatthew Wilcox (Oracle) 			xas_store(&xas, folio);
1188d93b41cSMatthew Wilcox 			xas_next(&xas);
1191da177e4SLinus Torvalds 		}
12038d8b4e6SHuang Ying 		address_space->nrpages += nr;
121a4c366f0SMatthew Wilcox (Oracle) 		__node_stat_mod_folio(folio, NR_FILE_PAGES, nr);
122a4c366f0SMatthew Wilcox (Oracle) 		__lruvec_stat_mod_folio(folio, NR_SWAPCACHE, nr);
1238d93b41cSMatthew Wilcox unlock:
1248d93b41cSMatthew Wilcox 		xas_unlock_irq(&xas);
1258d93b41cSMatthew Wilcox 	} while (xas_nomem(&xas, gfp));
1268d93b41cSMatthew Wilcox 
1278d93b41cSMatthew Wilcox 	if (!xas_error(&xas))
1288d93b41cSMatthew Wilcox 		return 0;
1298d93b41cSMatthew Wilcox 
130a4c366f0SMatthew Wilcox (Oracle) 	folio_clear_swapcache(folio);
131a4c366f0SMatthew Wilcox (Oracle) 	folio_ref_sub(folio, nr);
1328d93b41cSMatthew Wilcox 	return xas_error(&xas);
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds /*
136ceff9d33SMatthew Wilcox (Oracle)  * This must be called only on folios that have
1371da177e4SLinus Torvalds  * been verified to be in the swap cache.
1381da177e4SLinus Torvalds  */
__delete_from_swap_cache(struct folio * folio,swp_entry_t entry,void * shadow)139ceff9d33SMatthew Wilcox (Oracle) void __delete_from_swap_cache(struct folio *folio,
1403852f676SJoonsoo Kim 			swp_entry_t entry, void *shadow)
1411da177e4SLinus Torvalds {
1424e17ec25SMatthew Wilcox 	struct address_space *address_space = swap_address_space(entry);
143ceff9d33SMatthew Wilcox (Oracle) 	int i;
144ceff9d33SMatthew Wilcox (Oracle) 	long nr = folio_nr_pages(folio);
1454e17ec25SMatthew Wilcox 	pgoff_t idx = swp_offset(entry);
1464e17ec25SMatthew Wilcox 	XA_STATE(xas, &address_space->i_pages, idx);
14733806f06SShaohua Li 
1485649d113SYang Yang 	xas_set_update(&xas, workingset_update_node);
1495649d113SYang Yang 
150ceff9d33SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
151ceff9d33SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
152ceff9d33SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_writeback(folio), folio);
1531da177e4SLinus Torvalds 
15438d8b4e6SHuang Ying 	for (i = 0; i < nr; i++) {
1553852f676SJoonsoo Kim 		void *entry = xas_store(&xas, shadow);
156b9eb7776SMatthew Wilcox (Oracle) 		VM_BUG_ON_PAGE(entry != folio, entry);
1574e17ec25SMatthew Wilcox 		xas_next(&xas);
15838d8b4e6SHuang Ying 	}
159*3d2c9087SDavid Hildenbrand 	folio->swap.val = 0;
160ceff9d33SMatthew Wilcox (Oracle) 	folio_clear_swapcache(folio);
16138d8b4e6SHuang Ying 	address_space->nrpages -= nr;
162ceff9d33SMatthew Wilcox (Oracle) 	__node_stat_mod_folio(folio, NR_FILE_PAGES, -nr);
163ceff9d33SMatthew Wilcox (Oracle) 	__lruvec_stat_mod_folio(folio, NR_SWAPCACHE, -nr);
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds /**
16709c02e56SMatthew Wilcox (Oracle)  * add_to_swap - allocate swap space for a folio
16809c02e56SMatthew Wilcox (Oracle)  * @folio: folio we want to move to swap
1691da177e4SLinus Torvalds  *
17009c02e56SMatthew Wilcox (Oracle)  * Allocate swap space for the folio and add the folio to the
17109c02e56SMatthew Wilcox (Oracle)  * swap cache.
17209c02e56SMatthew Wilcox (Oracle)  *
17309c02e56SMatthew Wilcox (Oracle)  * Context: Caller needs to hold the folio lock.
17409c02e56SMatthew Wilcox (Oracle)  * Return: Whether the folio was added to the swap cache.
1751da177e4SLinus Torvalds  */
add_to_swap(struct folio * folio)17609c02e56SMatthew Wilcox (Oracle) bool add_to_swap(struct folio *folio)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	swp_entry_t entry;
1791da177e4SLinus Torvalds 	int err;
1801da177e4SLinus Torvalds 
18109c02e56SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
18209c02e56SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio);
1831da177e4SLinus Torvalds 
184e2e3fdc7SMatthew Wilcox (Oracle) 	entry = folio_alloc_swap(folio);
1851da177e4SLinus Torvalds 	if (!entry.val)
18609c02e56SMatthew Wilcox (Oracle) 		return false;
1870f074658SMinchan Kim 
188bd53b714SNick Piggin 	/*
1898d93b41cSMatthew Wilcox 	 * XArray node allocations from PF_MEMALLOC contexts could
190bd53b714SNick Piggin 	 * completely exhaust the page allocator. __GFP_NOMEMALLOC
191bd53b714SNick Piggin 	 * stops emergency reserves from being allocated.
1921da177e4SLinus Torvalds 	 *
193bd53b714SNick Piggin 	 * TODO: this could cause a theoretical memory reclaim
194bd53b714SNick Piggin 	 * deadlock in the swap out path.
1951da177e4SLinus Torvalds 	 */
1961da177e4SLinus Torvalds 	/*
197854e9ed0SMinchan Kim 	 * Add it to the swap cache.
1981da177e4SLinus Torvalds 	 */
199a4c366f0SMatthew Wilcox (Oracle) 	err = add_to_swap_cache(folio, entry,
2003852f676SJoonsoo Kim 			__GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN, NULL);
20138d8b4e6SHuang Ying 	if (err)
2022ca4532aSDaisuke Nishimura 		/*
2032ca4532aSDaisuke Nishimura 		 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
2042ca4532aSDaisuke Nishimura 		 * clear SWAP_HAS_CACHE flag.
2052ca4532aSDaisuke Nishimura 		 */
2060f074658SMinchan Kim 		goto fail;
2079625456cSShaohua Li 	/*
20809c02e56SMatthew Wilcox (Oracle) 	 * Normally the folio will be dirtied in unmap because its
20909c02e56SMatthew Wilcox (Oracle) 	 * pte should be dirty. A special case is MADV_FREE page. The
21009c02e56SMatthew Wilcox (Oracle) 	 * page's pte could have dirty bit cleared but the folio's
21109c02e56SMatthew Wilcox (Oracle) 	 * SwapBacked flag is still set because clearing the dirty bit
21209c02e56SMatthew Wilcox (Oracle) 	 * and SwapBacked flag has no lock protected. For such folio,
21309c02e56SMatthew Wilcox (Oracle) 	 * unmap will not set dirty bit for it, so folio reclaim will
21409c02e56SMatthew Wilcox (Oracle) 	 * not write the folio out. This can cause data corruption when
21509c02e56SMatthew Wilcox (Oracle) 	 * the folio is swapped in later. Always setting the dirty flag
21609c02e56SMatthew Wilcox (Oracle) 	 * for the folio solves the problem.
2179625456cSShaohua Li 	 */
21809c02e56SMatthew Wilcox (Oracle) 	folio_mark_dirty(folio);
2191da177e4SLinus Torvalds 
22009c02e56SMatthew Wilcox (Oracle) 	return true;
22138d8b4e6SHuang Ying 
22238d8b4e6SHuang Ying fail:
2234081f744SMatthew Wilcox (Oracle) 	put_swap_folio(folio, entry);
22409c02e56SMatthew Wilcox (Oracle) 	return false;
22538d8b4e6SHuang Ying }
22638d8b4e6SHuang Ying 
2271da177e4SLinus Torvalds /*
22875fa68a5SMatthew Wilcox (Oracle)  * This must be called only on folios that have
2291da177e4SLinus Torvalds  * been verified to be in the swap cache and locked.
23075fa68a5SMatthew Wilcox (Oracle)  * It will never put the folio into the free list,
23175fa68a5SMatthew Wilcox (Oracle)  * the caller has a reference on the folio.
2321da177e4SLinus Torvalds  */
delete_from_swap_cache(struct folio * folio)23375fa68a5SMatthew Wilcox (Oracle) void delete_from_swap_cache(struct folio *folio)
2341da177e4SLinus Torvalds {
235*3d2c9087SDavid Hildenbrand 	swp_entry_t entry = folio->swap;
2364e17ec25SMatthew Wilcox 	struct address_space *address_space = swap_address_space(entry);
2371da177e4SLinus Torvalds 
238b93b0163SMatthew Wilcox 	xa_lock_irq(&address_space->i_pages);
239ceff9d33SMatthew Wilcox (Oracle) 	__delete_from_swap_cache(folio, entry, NULL);
240b93b0163SMatthew Wilcox 	xa_unlock_irq(&address_space->i_pages);
2411da177e4SLinus Torvalds 
2424081f744SMatthew Wilcox (Oracle) 	put_swap_folio(folio, entry);
24375fa68a5SMatthew Wilcox (Oracle) 	folio_ref_sub(folio, folio_nr_pages(folio));
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds 
clear_shadow_from_swap_cache(int type,unsigned long begin,unsigned long end)2463852f676SJoonsoo Kim void clear_shadow_from_swap_cache(int type, unsigned long begin,
2473852f676SJoonsoo Kim 				unsigned long end)
2483852f676SJoonsoo Kim {
2493852f676SJoonsoo Kim 	unsigned long curr = begin;
2503852f676SJoonsoo Kim 	void *old;
2513852f676SJoonsoo Kim 
2523852f676SJoonsoo Kim 	for (;;) {
2533852f676SJoonsoo Kim 		swp_entry_t entry = swp_entry(type, curr);
2543852f676SJoonsoo Kim 		struct address_space *address_space = swap_address_space(entry);
2553852f676SJoonsoo Kim 		XA_STATE(xas, &address_space->i_pages, curr);
2563852f676SJoonsoo Kim 
2575649d113SYang Yang 		xas_set_update(&xas, workingset_update_node);
2585649d113SYang Yang 
2593852f676SJoonsoo Kim 		xa_lock_irq(&address_space->i_pages);
2603852f676SJoonsoo Kim 		xas_for_each(&xas, old, end) {
2613852f676SJoonsoo Kim 			if (!xa_is_value(old))
2623852f676SJoonsoo Kim 				continue;
2633852f676SJoonsoo Kim 			xas_store(&xas, NULL);
2643852f676SJoonsoo Kim 		}
2653852f676SJoonsoo Kim 		xa_unlock_irq(&address_space->i_pages);
2663852f676SJoonsoo Kim 
2673852f676SJoonsoo Kim 		/* search the next swapcache until we meet end */
2683852f676SJoonsoo Kim 		curr >>= SWAP_ADDRESS_SPACE_SHIFT;
2693852f676SJoonsoo Kim 		curr++;
2703852f676SJoonsoo Kim 		curr <<= SWAP_ADDRESS_SPACE_SHIFT;
2713852f676SJoonsoo Kim 		if (curr > end)
2723852f676SJoonsoo Kim 			break;
2733852f676SJoonsoo Kim 	}
2743852f676SJoonsoo Kim }
2753852f676SJoonsoo Kim 
2761da177e4SLinus Torvalds /*
2771da177e4SLinus Torvalds  * If we are the only user, then try to free up the swap cache.
2781da177e4SLinus Torvalds  *
279aedd74d4SMatthew Wilcox (Oracle)  * Its ok to check the swapcache flag without the folio lock
2801da177e4SLinus Torvalds  * here because we are going to recheck again inside
281aedd74d4SMatthew Wilcox (Oracle)  * folio_free_swap() _with_ the lock.
2821da177e4SLinus Torvalds  * 					- Marcelo
2831da177e4SLinus Torvalds  */
free_swap_cache(struct page * page)284f4c4a3f4SHuang Ying void free_swap_cache(struct page *page)
2851da177e4SLinus Torvalds {
286aedd74d4SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
287aedd74d4SMatthew Wilcox (Oracle) 
288aedd74d4SMatthew Wilcox (Oracle) 	if (folio_test_swapcache(folio) && !folio_mapped(folio) &&
289aedd74d4SMatthew Wilcox (Oracle) 	    folio_trylock(folio)) {
290aedd74d4SMatthew Wilcox (Oracle) 		folio_free_swap(folio);
291aedd74d4SMatthew Wilcox (Oracle) 		folio_unlock(folio);
2921da177e4SLinus Torvalds 	}
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds /*
2961da177e4SLinus Torvalds  * Perform a free_page(), also freeing any swap cache associated with
297b8072f09SHugh Dickins  * this page if it is the last user of the page.
2981da177e4SLinus Torvalds  */
free_page_and_swap_cache(struct page * page)2991da177e4SLinus Torvalds void free_page_and_swap_cache(struct page *page)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	free_swap_cache(page);
3026fcb52a5SAaron Lu 	if (!is_huge_zero_page(page))
30309cbfeafSKirill A. Shutemov 		put_page(page);
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds /*
3071da177e4SLinus Torvalds  * Passed an array of pages, drop them all from swapcache and then release
3081da177e4SLinus Torvalds  * them.  They are removed from the LRU and freed if this is their last use.
3091da177e4SLinus Torvalds  */
free_pages_and_swap_cache(struct encoded_page ** pages,int nr)3107cc8f9c7SLinus Torvalds void free_pages_and_swap_cache(struct encoded_page **pages, int nr)
3111da177e4SLinus Torvalds {
312aabfb572SMichal Hocko 	lru_add_drain();
3137cc8f9c7SLinus Torvalds 	for (int i = 0; i < nr; i++)
3147cc8f9c7SLinus Torvalds 		free_swap_cache(encoded_page_ptr(pages[i]));
3157cc8f9c7SLinus Torvalds 	release_pages(pages, nr);
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
swap_use_vma_readahead(void)318e9e9b7ecSMinchan Kim static inline bool swap_use_vma_readahead(void)
319e9e9b7ecSMinchan Kim {
320e9e9b7ecSMinchan Kim 	return READ_ONCE(enable_vma_readahead) && !atomic_read(&nr_rotate_swap);
321e9e9b7ecSMinchan Kim }
322e9e9b7ecSMinchan Kim 
3231da177e4SLinus Torvalds /*
324c9edc242SMatthew Wilcox (Oracle)  * Lookup a swap entry in the swap cache. A found folio will be returned
3251da177e4SLinus Torvalds  * unlocked and with its refcount incremented - we rely on the kernel
326c9edc242SMatthew Wilcox (Oracle)  * lock getting page table operations atomic even if we drop the folio
3271da177e4SLinus Torvalds  * lock before returning.
328cbc2bd98SKairui Song  *
329cbc2bd98SKairui Song  * Caller must lock the swap device or hold a reference to keep it valid.
3301da177e4SLinus Torvalds  */
swap_cache_get_folio(swp_entry_t entry,struct vm_area_struct * vma,unsigned long addr)331c9edc242SMatthew Wilcox (Oracle) struct folio *swap_cache_get_folio(swp_entry_t entry,
332c9edc242SMatthew Wilcox (Oracle) 		struct vm_area_struct *vma, unsigned long addr)
3331da177e4SLinus Torvalds {
334c9edc242SMatthew Wilcox (Oracle) 	struct folio *folio;
3351da177e4SLinus Torvalds 
336c9edc242SMatthew Wilcox (Oracle) 	folio = filemap_get_folio(swap_address_space(entry), swp_offset(entry));
33766dabbb6SChristoph Hellwig 	if (!IS_ERR(folio)) {
338eaf649ebSMinchan Kim 		bool vma_ra = swap_use_vma_readahead();
339eaf649ebSMinchan Kim 		bool readahead;
340eaf649ebSMinchan Kim 
341eaf649ebSMinchan Kim 		/*
342eaf649ebSMinchan Kim 		 * At the moment, we don't support PG_readahead for anon THP
343eaf649ebSMinchan Kim 		 * so let's bail out rather than confusing the readahead stat.
344eaf649ebSMinchan Kim 		 */
345c9edc242SMatthew Wilcox (Oracle) 		if (unlikely(folio_test_large(folio)))
346c9edc242SMatthew Wilcox (Oracle) 			return folio;
347eaf649ebSMinchan Kim 
348c9edc242SMatthew Wilcox (Oracle) 		readahead = folio_test_clear_readahead(folio);
349eaf649ebSMinchan Kim 		if (vma && vma_ra) {
350eaf649ebSMinchan Kim 			unsigned long ra_val;
351eaf649ebSMinchan Kim 			int win, hits;
352eaf649ebSMinchan Kim 
353eaf649ebSMinchan Kim 			ra_val = GET_SWAP_RA_VAL(vma);
354eaf649ebSMinchan Kim 			win = SWAP_RA_WIN(ra_val);
355eaf649ebSMinchan Kim 			hits = SWAP_RA_HITS(ra_val);
356ec560175SHuang Ying 			if (readahead)
357ec560175SHuang Ying 				hits = min_t(int, hits + 1, SWAP_RA_HITS_MAX);
358ec560175SHuang Ying 			atomic_long_set(&vma->swap_readahead_info,
359ec560175SHuang Ying 					SWAP_RA_VAL(addr, win, hits));
360ec560175SHuang Ying 		}
361eaf649ebSMinchan Kim 
362ec560175SHuang Ying 		if (readahead) {
363ec560175SHuang Ying 			count_vm_event(SWAP_RA_HIT);
364eaf649ebSMinchan Kim 			if (!vma || !vma_ra)
365ec560175SHuang Ying 				atomic_inc(&swapin_readahead_hits);
366ec560175SHuang Ying 		}
36766dabbb6SChristoph Hellwig 	} else {
36866dabbb6SChristoph Hellwig 		folio = NULL;
369ec560175SHuang Ying 	}
370eaf649ebSMinchan Kim 
371c9edc242SMatthew Wilcox (Oracle) 	return folio;
372c9edc242SMatthew Wilcox (Oracle) }
373c9edc242SMatthew Wilcox (Oracle) 
37461ef1865SMatthew Wilcox (Oracle) /**
375524984ffSMatthew Wilcox (Oracle)  * filemap_get_incore_folio - Find and get a folio from the page or swap caches.
37661ef1865SMatthew Wilcox (Oracle)  * @mapping: The address_space to search.
37761ef1865SMatthew Wilcox (Oracle)  * @index: The page cache index.
37861ef1865SMatthew Wilcox (Oracle)  *
379524984ffSMatthew Wilcox (Oracle)  * This differs from filemap_get_folio() in that it will also look for the
380524984ffSMatthew Wilcox (Oracle)  * folio in the swap cache.
38161ef1865SMatthew Wilcox (Oracle)  *
382524984ffSMatthew Wilcox (Oracle)  * Return: The found folio or %NULL.
38361ef1865SMatthew Wilcox (Oracle)  */
filemap_get_incore_folio(struct address_space * mapping,pgoff_t index)384524984ffSMatthew Wilcox (Oracle) struct folio *filemap_get_incore_folio(struct address_space *mapping,
385524984ffSMatthew Wilcox (Oracle) 		pgoff_t index)
38661ef1865SMatthew Wilcox (Oracle) {
38761ef1865SMatthew Wilcox (Oracle) 	swp_entry_t swp;
38861ef1865SMatthew Wilcox (Oracle) 	struct swap_info_struct *si;
389097b3e59SChristoph Hellwig 	struct folio *folio = filemap_get_entry(mapping, index);
39061ef1865SMatthew Wilcox (Oracle) 
39166dabbb6SChristoph Hellwig 	if (!folio)
39266dabbb6SChristoph Hellwig 		return ERR_PTR(-ENOENT);
393dd8095b1SMatthew Wilcox (Oracle) 	if (!xa_is_value(folio))
39466dabbb6SChristoph Hellwig 		return folio;
39561ef1865SMatthew Wilcox (Oracle) 	if (!shmem_mapping(mapping))
39666dabbb6SChristoph Hellwig 		return ERR_PTR(-ENOENT);
39761ef1865SMatthew Wilcox (Oracle) 
398dd8095b1SMatthew Wilcox (Oracle) 	swp = radix_to_swp_entry(folio);
399ba6851b4SMiaohe Lin 	/* There might be swapin error entries in shmem mapping. */
400ba6851b4SMiaohe Lin 	if (non_swap_entry(swp))
40166dabbb6SChristoph Hellwig 		return ERR_PTR(-ENOENT);
40261ef1865SMatthew Wilcox (Oracle) 	/* Prevent swapoff from happening to us */
40361ef1865SMatthew Wilcox (Oracle) 	si = get_swap_device(swp);
40461ef1865SMatthew Wilcox (Oracle) 	if (!si)
40566dabbb6SChristoph Hellwig 		return ERR_PTR(-ENOENT);
406dd8095b1SMatthew Wilcox (Oracle) 	index = swp_offset(swp);
407dd8095b1SMatthew Wilcox (Oracle) 	folio = filemap_get_folio(swap_address_space(swp), index);
40861ef1865SMatthew Wilcox (Oracle) 	put_swap_device(si);
409524984ffSMatthew Wilcox (Oracle) 	return folio;
41061ef1865SMatthew Wilcox (Oracle) }
41161ef1865SMatthew Wilcox (Oracle) 
__read_swap_cache_async(swp_entry_t entry,gfp_t gfp_mask,struct vm_area_struct * vma,unsigned long addr,bool * new_page_allocated)4125b999aadSDmitry Safonov struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
4135b999aadSDmitry Safonov 			struct vm_area_struct *vma, unsigned long addr,
4145b999aadSDmitry Safonov 			bool *new_page_allocated)
4151da177e4SLinus Torvalds {
416eb085574SHuang Ying 	struct swap_info_struct *si;
417a0d3374bSMatthew Wilcox (Oracle) 	struct folio *folio;
41846a774d3SHuang Ying 	struct page *page;
419aae466b0SJoonsoo Kim 	void *shadow = NULL;
4204c6355b2SJohannes Weiner 
4215b999aadSDmitry Safonov 	*new_page_allocated = false;
42246a774d3SHuang Ying 	si = get_swap_device(entry);
42346a774d3SHuang Ying 	if (!si)
42446a774d3SHuang Ying 		return NULL;
4251da177e4SLinus Torvalds 
4264c6355b2SJohannes Weiner 	for (;;) {
4274c6355b2SJohannes Weiner 		int err;
4281da177e4SLinus Torvalds 		/*
4291da177e4SLinus Torvalds 		 * First check the swap cache.  Since this is normally
430cb691e2fSMatthew Wilcox (Oracle) 		 * called after swap_cache_get_folio() failed, re-calling
4311da177e4SLinus Torvalds 		 * that would confuse statistics.
4321da177e4SLinus Torvalds 		 */
433a0d3374bSMatthew Wilcox (Oracle) 		folio = filemap_get_folio(swap_address_space(entry),
434eb085574SHuang Ying 						swp_offset(entry));
43546a774d3SHuang Ying 		if (!IS_ERR(folio)) {
43646a774d3SHuang Ying 			page = folio_file_page(folio, swp_offset(entry));
43746a774d3SHuang Ying 			goto got_page;
43846a774d3SHuang Ying 		}
4391da177e4SLinus Torvalds 
440ba81f838SHuang Ying 		/*
441ba81f838SHuang Ying 		 * Just skip read ahead for unused swap slot.
442ba81f838SHuang Ying 		 * During swap_off when swap_slot_cache is disabled,
443ba81f838SHuang Ying 		 * we have to handle the race between putting
444ba81f838SHuang Ying 		 * swap entry in swap cache and marking swap slot
445ba81f838SHuang Ying 		 * as SWAP_HAS_CACHE.  That's done in later part of code or
446ba81f838SHuang Ying 		 * else swap_off will be aborted if we return NULL.
447ba81f838SHuang Ying 		 */
4483ecdeb0fSHuang Ying 		if (!swap_swapcount(si, entry) && swap_slot_cache_enabled)
44946a774d3SHuang Ying 			goto fail_put_swap;
450e8c26ab6STim Chen 
4511da177e4SLinus Torvalds 		/*
4524c6355b2SJohannes Weiner 		 * Get a new page to read into from swap.  Allocate it now,
4534c6355b2SJohannes Weiner 		 * before marking swap_map SWAP_HAS_CACHE, when -EEXIST will
4544c6355b2SJohannes Weiner 		 * cause any racers to loop around until we add it to cache.
4551da177e4SLinus Torvalds 		 */
456a0d3374bSMatthew Wilcox (Oracle) 		folio = vma_alloc_folio(gfp_mask, 0, vma, addr, false);
457a0d3374bSMatthew Wilcox (Oracle) 		if (!folio)
45846a774d3SHuang Ying                         goto fail_put_swap;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 		/*
461f000944dSHugh Dickins 		 * Swap entry may have been freed since our caller observed it.
462f000944dSHugh Dickins 		 */
463355cfa73SKAMEZAWA Hiroyuki 		err = swapcache_prepare(entry);
4644c6355b2SJohannes Weiner 		if (!err)
465f000944dSHugh Dickins 			break;
466f000944dSHugh Dickins 
467a0d3374bSMatthew Wilcox (Oracle) 		folio_put(folio);
4684c6355b2SJohannes Weiner 		if (err != -EEXIST)
46946a774d3SHuang Ying 			goto fail_put_swap;
4701da177e4SLinus Torvalds 
4714c6355b2SJohannes Weiner 		/*
4724c6355b2SJohannes Weiner 		 * We might race against __delete_from_swap_cache(), and
4734c6355b2SJohannes Weiner 		 * stumble across a swap_map entry whose SWAP_HAS_CACHE
4744c6355b2SJohannes Weiner 		 * has not yet been cleared.  Or race against another
4754c6355b2SJohannes Weiner 		 * __read_swap_cache_async(), which has set SWAP_HAS_CACHE
4764c6355b2SJohannes Weiner 		 * in swap_map, but not yet added its page to swap cache.
4774c6355b2SJohannes Weiner 		 */
478029c4628SGuo Ziliang 		schedule_timeout_uninterruptible(1);
4794c6355b2SJohannes Weiner 	}
4804c6355b2SJohannes Weiner 
4814c6355b2SJohannes Weiner 	/*
4824c6355b2SJohannes Weiner 	 * The swap entry is ours to swap in. Prepare the new page.
4834c6355b2SJohannes Weiner 	 */
4844c6355b2SJohannes Weiner 
485a0d3374bSMatthew Wilcox (Oracle) 	__folio_set_locked(folio);
486a0d3374bSMatthew Wilcox (Oracle) 	__folio_set_swapbacked(folio);
4874c6355b2SJohannes Weiner 
48865995918SMatthew Wilcox (Oracle) 	if (mem_cgroup_swapin_charge_folio(folio, NULL, gfp_mask, entry))
4894c6355b2SJohannes Weiner 		goto fail_unlock;
4904c6355b2SJohannes Weiner 
4910add0c77SShakeel Butt 	/* May fail (-ENOMEM) if XArray node allocation failed. */
492a4c366f0SMatthew Wilcox (Oracle) 	if (add_to_swap_cache(folio, entry, gfp_mask & GFP_RECLAIM_MASK, &shadow))
4934c6355b2SJohannes Weiner 		goto fail_unlock;
4940add0c77SShakeel Butt 
4950add0c77SShakeel Butt 	mem_cgroup_swapin_uncharge_swap(entry);
4964c6355b2SJohannes Weiner 
497aae466b0SJoonsoo Kim 	if (shadow)
498a0d3374bSMatthew Wilcox (Oracle) 		workingset_refault(folio, shadow);
499314b57fbSJohannes Weiner 
500a0d3374bSMatthew Wilcox (Oracle) 	/* Caller will initiate read into locked folio */
501a0d3374bSMatthew Wilcox (Oracle) 	folio_add_lru(folio);
5024c6355b2SJohannes Weiner 	*new_page_allocated = true;
50346a774d3SHuang Ying 	page = &folio->page;
50446a774d3SHuang Ying got_page:
50546a774d3SHuang Ying 	put_swap_device(si);
50646a774d3SHuang Ying 	return page;
5074c6355b2SJohannes Weiner 
5084c6355b2SJohannes Weiner fail_unlock:
5094081f744SMatthew Wilcox (Oracle) 	put_swap_folio(folio, entry);
510a0d3374bSMatthew Wilcox (Oracle) 	folio_unlock(folio);
511a0d3374bSMatthew Wilcox (Oracle) 	folio_put(folio);
51246a774d3SHuang Ying fail_put_swap:
51346a774d3SHuang Ying 	put_swap_device(si);
5144c6355b2SJohannes Weiner 	return NULL;
5151da177e4SLinus Torvalds }
51646017e95SHugh Dickins 
5175b999aadSDmitry Safonov /*
5185b999aadSDmitry Safonov  * Locate a page of swap in physical memory, reserving swap cache space
5195b999aadSDmitry Safonov  * and reading the disk if it is not already cached.
5205b999aadSDmitry Safonov  * A failure return means that either the page allocation failed or that
5215b999aadSDmitry Safonov  * the swap entry is no longer in use.
52246a774d3SHuang Ying  *
52346a774d3SHuang Ying  * get/put_swap_device() aren't needed to call this function, because
52446a774d3SHuang Ying  * __read_swap_cache_async() call them and swap_readpage() holds the
52546a774d3SHuang Ying  * swap cache folio lock.
5265b999aadSDmitry Safonov  */
read_swap_cache_async(swp_entry_t entry,gfp_t gfp_mask,struct vm_area_struct * vma,unsigned long addr,struct swap_iocb ** plug)5275b999aadSDmitry Safonov struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
5285169b844SNeilBrown 				   struct vm_area_struct *vma,
529b243dcbfSSuren Baghdasaryan 				   unsigned long addr, struct swap_iocb **plug)
5305b999aadSDmitry Safonov {
5315b999aadSDmitry Safonov 	bool page_was_allocated;
5325b999aadSDmitry Safonov 	struct page *retpage = __read_swap_cache_async(entry, gfp_mask,
5335b999aadSDmitry Safonov 			vma, addr, &page_was_allocated);
5345b999aadSDmitry Safonov 
5355b999aadSDmitry Safonov 	if (page_was_allocated)
536b243dcbfSSuren Baghdasaryan 		swap_readpage(retpage, false, plug);
5375b999aadSDmitry Safonov 
5385b999aadSDmitry Safonov 	return retpage;
5395b999aadSDmitry Safonov }
5405b999aadSDmitry Safonov 
__swapin_nr_pages(unsigned long prev_offset,unsigned long offset,int hits,int max_pages,int prev_win)541ec560175SHuang Ying static unsigned int __swapin_nr_pages(unsigned long prev_offset,
542ec560175SHuang Ying 				      unsigned long offset,
543ec560175SHuang Ying 				      int hits,
544ec560175SHuang Ying 				      int max_pages,
545ec560175SHuang Ying 				      int prev_win)
546579f8290SShaohua Li {
547ec560175SHuang Ying 	unsigned int pages, last_ra;
548579f8290SShaohua Li 
549579f8290SShaohua Li 	/*
550579f8290SShaohua Li 	 * This heuristic has been found to work well on both sequential and
551579f8290SShaohua Li 	 * random loads, swapping to hard disk or to SSD: please don't ask
552579f8290SShaohua Li 	 * what the "+ 2" means, it just happens to work well, that's all.
553579f8290SShaohua Li 	 */
554ec560175SHuang Ying 	pages = hits + 2;
555579f8290SShaohua Li 	if (pages == 2) {
556579f8290SShaohua Li 		/*
557579f8290SShaohua Li 		 * We can have no readahead hits to judge by: but must not get
558579f8290SShaohua Li 		 * stuck here forever, so check for an adjacent offset instead
559579f8290SShaohua Li 		 * (and don't even bother to check whether swap type is same).
560579f8290SShaohua Li 		 */
561579f8290SShaohua Li 		if (offset != prev_offset + 1 && offset != prev_offset - 1)
562579f8290SShaohua Li 			pages = 1;
563579f8290SShaohua Li 	} else {
564579f8290SShaohua Li 		unsigned int roundup = 4;
565579f8290SShaohua Li 		while (roundup < pages)
566579f8290SShaohua Li 			roundup <<= 1;
567579f8290SShaohua Li 		pages = roundup;
568579f8290SShaohua Li 	}
569579f8290SShaohua Li 
570579f8290SShaohua Li 	if (pages > max_pages)
571579f8290SShaohua Li 		pages = max_pages;
572579f8290SShaohua Li 
573579f8290SShaohua Li 	/* Don't shrink readahead too fast */
574ec560175SHuang Ying 	last_ra = prev_win / 2;
575579f8290SShaohua Li 	if (pages < last_ra)
576579f8290SShaohua Li 		pages = last_ra;
577ec560175SHuang Ying 
578ec560175SHuang Ying 	return pages;
579ec560175SHuang Ying }
580ec560175SHuang Ying 
swapin_nr_pages(unsigned long offset)581ec560175SHuang Ying static unsigned long swapin_nr_pages(unsigned long offset)
582ec560175SHuang Ying {
583ec560175SHuang Ying 	static unsigned long prev_offset;
584ec560175SHuang Ying 	unsigned int hits, pages, max_pages;
585ec560175SHuang Ying 	static atomic_t last_readahead_pages;
586ec560175SHuang Ying 
587ec560175SHuang Ying 	max_pages = 1 << READ_ONCE(page_cluster);
588ec560175SHuang Ying 	if (max_pages <= 1)
589ec560175SHuang Ying 		return 1;
590ec560175SHuang Ying 
591ec560175SHuang Ying 	hits = atomic_xchg(&swapin_readahead_hits, 0);
592d6c1f098SQian Cai 	pages = __swapin_nr_pages(READ_ONCE(prev_offset), offset, hits,
593d6c1f098SQian Cai 				  max_pages,
594ec560175SHuang Ying 				  atomic_read(&last_readahead_pages));
595ec560175SHuang Ying 	if (!hits)
596d6c1f098SQian Cai 		WRITE_ONCE(prev_offset, offset);
597579f8290SShaohua Li 	atomic_set(&last_readahead_pages, pages);
598579f8290SShaohua Li 
599579f8290SShaohua Li 	return pages;
600579f8290SShaohua Li }
601579f8290SShaohua Li 
60246017e95SHugh Dickins /**
603e9e9b7ecSMinchan Kim  * swap_cluster_readahead - swap in pages in hope we need them soon
60446017e95SHugh Dickins  * @entry: swap entry of this memory
6057682486bSRandy Dunlap  * @gfp_mask: memory allocation flags
606e9e9b7ecSMinchan Kim  * @vmf: fault information
60746017e95SHugh Dickins  *
60846017e95SHugh Dickins  * Returns the struct page for entry and addr, after queueing swapin.
60946017e95SHugh Dickins  *
61046017e95SHugh Dickins  * Primitive swap readahead code. We simply read an aligned block of
61146017e95SHugh Dickins  * (1 << page_cluster) entries in the swap area. This method is chosen
61246017e95SHugh Dickins  * because it doesn't cost us any seek time.  We also make sure to queue
61346017e95SHugh Dickins  * the 'original' request together with the readahead ones...
61446017e95SHugh Dickins  *
61546017e95SHugh Dickins  * This has been extended to use the NUMA policies from the mm triggering
61646017e95SHugh Dickins  * the readahead.
61746017e95SHugh Dickins  *
618c1e8d7c6SMichel Lespinasse  * Caller must hold read mmap_lock if vmf->vma is not NULL.
61946017e95SHugh Dickins  */
swap_cluster_readahead(swp_entry_t entry,gfp_t gfp_mask,struct vm_fault * vmf)620e9e9b7ecSMinchan Kim struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
621e9e9b7ecSMinchan Kim 				struct vm_fault *vmf)
62246017e95SHugh Dickins {
62346017e95SHugh Dickins 	struct page *page;
624579f8290SShaohua Li 	unsigned long entry_offset = swp_offset(entry);
625579f8290SShaohua Li 	unsigned long offset = entry_offset;
62667f96aa2SRik van Riel 	unsigned long start_offset, end_offset;
627579f8290SShaohua Li 	unsigned long mask;
628e9a6effaSHuang Ying 	struct swap_info_struct *si = swp_swap_info(entry);
6293fb5c298SChristian Ehrhardt 	struct blk_plug plug;
6305169b844SNeilBrown 	struct swap_iocb *splug = NULL;
631b243dcbfSSuren Baghdasaryan 	bool page_allocated;
632e9e9b7ecSMinchan Kim 	struct vm_area_struct *vma = vmf->vma;
633e9e9b7ecSMinchan Kim 	unsigned long addr = vmf->address;
63446017e95SHugh Dickins 
635579f8290SShaohua Li 	mask = swapin_nr_pages(offset) - 1;
636579f8290SShaohua Li 	if (!mask)
637579f8290SShaohua Li 		goto skip;
638579f8290SShaohua Li 
63967f96aa2SRik van Riel 	/* Read a page_cluster sized and aligned cluster around offset. */
64067f96aa2SRik van Riel 	start_offset = offset & ~mask;
64167f96aa2SRik van Riel 	end_offset = offset | mask;
64267f96aa2SRik van Riel 	if (!start_offset)	/* First page is swap header. */
64367f96aa2SRik van Riel 		start_offset++;
644e9a6effaSHuang Ying 	if (end_offset >= si->max)
645e9a6effaSHuang Ying 		end_offset = si->max - 1;
64667f96aa2SRik van Riel 
6473fb5c298SChristian Ehrhardt 	blk_start_plug(&plug);
64867f96aa2SRik van Riel 	for (offset = start_offset; offset <= end_offset ; offset++) {
64946017e95SHugh Dickins 		/* Ok, do the async read-ahead now */
650c4fa6309SHuang Ying 		page = __read_swap_cache_async(
651c4fa6309SHuang Ying 			swp_entry(swp_type(entry), offset),
652c4fa6309SHuang Ying 			gfp_mask, vma, addr, &page_allocated);
65346017e95SHugh Dickins 		if (!page)
65467f96aa2SRik van Riel 			continue;
655c4fa6309SHuang Ying 		if (page_allocated) {
6565169b844SNeilBrown 			swap_readpage(page, false, &splug);
657eaf649ebSMinchan Kim 			if (offset != entry_offset) {
658579f8290SShaohua Li 				SetPageReadahead(page);
659cbc65df2SHuang Ying 				count_vm_event(SWAP_RA);
660cbc65df2SHuang Ying 			}
661c4fa6309SHuang Ying 		}
66209cbfeafSKirill A. Shutemov 		put_page(page);
66346017e95SHugh Dickins 	}
6643fb5c298SChristian Ehrhardt 	blk_finish_plug(&plug);
6655169b844SNeilBrown 	swap_read_unplug(splug);
6663fb5c298SChristian Ehrhardt 
66746017e95SHugh Dickins 	lru_add_drain();	/* Push any new pages onto the LRU now */
668579f8290SShaohua Li skip:
6695169b844SNeilBrown 	/* The page was likely read above, so no need for plugging here */
670b243dcbfSSuren Baghdasaryan 	return read_swap_cache_async(entry, gfp_mask, vma, addr, NULL);
67146017e95SHugh Dickins }
6724b3ef9daSHuang, Ying 
init_swap_address_space(unsigned int type,unsigned long nr_pages)6734b3ef9daSHuang, Ying int init_swap_address_space(unsigned int type, unsigned long nr_pages)
6744b3ef9daSHuang, Ying {
6754b3ef9daSHuang, Ying 	struct address_space *spaces, *space;
6764b3ef9daSHuang, Ying 	unsigned int i, nr;
6774b3ef9daSHuang, Ying 
6784b3ef9daSHuang, Ying 	nr = DIV_ROUND_UP(nr_pages, SWAP_ADDRESS_SPACE_PAGES);
679778e1cddSKees Cook 	spaces = kvcalloc(nr, sizeof(struct address_space), GFP_KERNEL);
6804b3ef9daSHuang, Ying 	if (!spaces)
6814b3ef9daSHuang, Ying 		return -ENOMEM;
6824b3ef9daSHuang, Ying 	for (i = 0; i < nr; i++) {
6834b3ef9daSHuang, Ying 		space = spaces + i;
684a2833486SMatthew Wilcox 		xa_init_flags(&space->i_pages, XA_FLAGS_LOCK_IRQ);
6854b3ef9daSHuang, Ying 		atomic_set(&space->i_mmap_writable, 0);
6864b3ef9daSHuang, Ying 		space->a_ops = &swap_aops;
6874b3ef9daSHuang, Ying 		/* swap cache doesn't use writeback related tags */
6884b3ef9daSHuang, Ying 		mapping_set_no_writeback_tags(space);
6894b3ef9daSHuang, Ying 	}
6904b3ef9daSHuang, Ying 	nr_swapper_spaces[type] = nr;
691054f1d1fSHuang Ying 	swapper_spaces[type] = spaces;
6924b3ef9daSHuang, Ying 
6934b3ef9daSHuang, Ying 	return 0;
6944b3ef9daSHuang, Ying }
6954b3ef9daSHuang, Ying 
exit_swap_address_space(unsigned int type)6964b3ef9daSHuang, Ying void exit_swap_address_space(unsigned int type)
6974b3ef9daSHuang, Ying {
698eea4a501SHuang Ying 	int i;
699eea4a501SHuang Ying 	struct address_space *spaces = swapper_spaces[type];
700eea4a501SHuang Ying 
701eea4a501SHuang Ying 	for (i = 0; i < nr_swapper_spaces[type]; i++)
702eea4a501SHuang Ying 		VM_WARN_ON_ONCE(!mapping_empty(&spaces[i]));
703eea4a501SHuang Ying 	kvfree(spaces);
7044b3ef9daSHuang, Ying 	nr_swapper_spaces[type] = 0;
705054f1d1fSHuang Ying 	swapper_spaces[type] = NULL;
7064b3ef9daSHuang, Ying }
707ec560175SHuang Ying 
7084f8fcf4cSHugh Dickins #define SWAP_RA_ORDER_CEILING	5
7094f8fcf4cSHugh Dickins 
7104f8fcf4cSHugh Dickins struct vma_swap_readahead {
7114f8fcf4cSHugh Dickins 	unsigned short win;
7124f8fcf4cSHugh Dickins 	unsigned short offset;
7134f8fcf4cSHugh Dickins 	unsigned short nr_pte;
7144f8fcf4cSHugh Dickins };
7154f8fcf4cSHugh Dickins 
swap_ra_info(struct vm_fault * vmf,struct vma_swap_readahead * ra_info)716eaf649ebSMinchan Kim static void swap_ra_info(struct vm_fault *vmf,
717eaf649ebSMinchan Kim 			 struct vma_swap_readahead *ra_info)
718ec560175SHuang Ying {
719ec560175SHuang Ying 	struct vm_area_struct *vma = vmf->vma;
720eaf649ebSMinchan Kim 	unsigned long ra_val;
72116ba391eSKairui Song 	unsigned long faddr, pfn, fpfn, lpfn, rpfn;
722ec560175SHuang Ying 	unsigned long start, end;
72316ba391eSKairui Song 	unsigned int max_win, hits, prev_win, win;
724ec560175SHuang Ying 
72561b63972SHuang Ying 	max_win = 1 << min_t(unsigned int, READ_ONCE(page_cluster),
72661b63972SHuang Ying 			     SWAP_RA_ORDER_CEILING);
72761b63972SHuang Ying 	if (max_win == 1) {
728eaf649ebSMinchan Kim 		ra_info->win = 1;
729eaf649ebSMinchan Kim 		return;
73061b63972SHuang Ying 	}
73161b63972SHuang Ying 
732ec560175SHuang Ying 	faddr = vmf->address;
733ec560175SHuang Ying 	fpfn = PFN_DOWN(faddr);
734eaf649ebSMinchan Kim 	ra_val = GET_SWAP_RA_VAL(vma);
735eaf649ebSMinchan Kim 	pfn = PFN_DOWN(SWAP_RA_ADDR(ra_val));
736eaf649ebSMinchan Kim 	prev_win = SWAP_RA_WIN(ra_val);
737eaf649ebSMinchan Kim 	hits = SWAP_RA_HITS(ra_val);
738eaf649ebSMinchan Kim 	ra_info->win = win = __swapin_nr_pages(pfn, fpfn, hits,
739ec560175SHuang Ying 					       max_win, prev_win);
740ec560175SHuang Ying 	atomic_long_set(&vma->swap_readahead_info,
741ec560175SHuang Ying 			SWAP_RA_VAL(faddr, win, 0));
74218ad72f5SKairui Song 	if (win == 1)
743eaf649ebSMinchan Kim 		return;
744ec560175SHuang Ying 
74516ba391eSKairui Song 	if (fpfn == pfn + 1) {
74616ba391eSKairui Song 		lpfn = fpfn;
74716ba391eSKairui Song 		rpfn = fpfn + win;
74816ba391eSKairui Song 	} else if (pfn == fpfn + 1) {
74916ba391eSKairui Song 		lpfn = fpfn - win + 1;
75016ba391eSKairui Song 		rpfn = fpfn + 1;
75116ba391eSKairui Song 	} else {
75216ba391eSKairui Song 		unsigned int left = (win - 1) / 2;
75316ba391eSKairui Song 
75416ba391eSKairui Song 		lpfn = fpfn - left;
75516ba391eSKairui Song 		rpfn = fpfn + win - left;
756ec560175SHuang Ying 	}
75716ba391eSKairui Song 	start = max3(lpfn, PFN_DOWN(vma->vm_start),
75816ba391eSKairui Song 		     PFN_DOWN(faddr & PMD_MASK));
75916ba391eSKairui Song 	end = min3(rpfn, PFN_DOWN(vma->vm_end),
76016ba391eSKairui Song 		   PFN_DOWN((faddr & PMD_MASK) + PMD_SIZE));
76116ba391eSKairui Song 
762eaf649ebSMinchan Kim 	ra_info->nr_pte = end - start;
763eaf649ebSMinchan Kim 	ra_info->offset = fpfn - start;
764ec560175SHuang Ying }
765ec560175SHuang Ying 
766e9f59873SYang Shi /**
767e9f59873SYang Shi  * swap_vma_readahead - swap in pages in hope we need them soon
76827ec4878SKrzysztof Kozlowski  * @fentry: swap entry of this memory
769e9f59873SYang Shi  * @gfp_mask: memory allocation flags
770e9f59873SYang Shi  * @vmf: fault information
771e9f59873SYang Shi  *
772e9f59873SYang Shi  * Returns the struct page for entry and addr, after queueing swapin.
773e9f59873SYang Shi  *
774cb152a1aSShijie Luo  * Primitive swap readahead code. We simply read in a few pages whose
775e9f59873SYang Shi  * virtual addresses are around the fault address in the same vma.
776e9f59873SYang Shi  *
777c1e8d7c6SMichel Lespinasse  * Caller must hold read mmap_lock if vmf->vma is not NULL.
778e9f59873SYang Shi  *
779e9f59873SYang Shi  */
swap_vma_readahead(swp_entry_t fentry,gfp_t gfp_mask,struct vm_fault * vmf)780f5c754d6SColin Ian King static struct page *swap_vma_readahead(swp_entry_t fentry, gfp_t gfp_mask,
781eaf649ebSMinchan Kim 				       struct vm_fault *vmf)
782ec560175SHuang Ying {
783ec560175SHuang Ying 	struct blk_plug plug;
7845169b844SNeilBrown 	struct swap_iocb *splug = NULL;
785ec560175SHuang Ying 	struct vm_area_struct *vma = vmf->vma;
786ec560175SHuang Ying 	struct page *page;
7874f8fcf4cSHugh Dickins 	pte_t *pte = NULL, pentry;
7884f8fcf4cSHugh Dickins 	unsigned long addr;
789ec560175SHuang Ying 	swp_entry_t entry;
790ec560175SHuang Ying 	unsigned int i;
791ec560175SHuang Ying 	bool page_allocated;
792e97af699SMiaohe Lin 	struct vma_swap_readahead ra_info = {
793e97af699SMiaohe Lin 		.win = 1,
794e97af699SMiaohe Lin 	};
795ec560175SHuang Ying 
796eaf649ebSMinchan Kim 	swap_ra_info(vmf, &ra_info);
797eaf649ebSMinchan Kim 	if (ra_info.win == 1)
798ec560175SHuang Ying 		goto skip;
799ec560175SHuang Ying 
8004f8fcf4cSHugh Dickins 	addr = vmf->address - (ra_info.offset * PAGE_SIZE);
8014f8fcf4cSHugh Dickins 
802ec560175SHuang Ying 	blk_start_plug(&plug);
8034f8fcf4cSHugh Dickins 	for (i = 0; i < ra_info.nr_pte; i++, addr += PAGE_SIZE) {
8044f8fcf4cSHugh Dickins 		if (!pte++) {
8054f8fcf4cSHugh Dickins 			pte = pte_offset_map(vmf->pmd, addr);
8064f8fcf4cSHugh Dickins 			if (!pte)
8074f8fcf4cSHugh Dickins 				break;
8084f8fcf4cSHugh Dickins 		}
8094f8fcf4cSHugh Dickins 		pentry = ptep_get_lockless(pte);
81092bafb20SMiaohe Lin 		if (!is_swap_pte(pentry))
811ec560175SHuang Ying 			continue;
812ec560175SHuang Ying 		entry = pte_to_swp_entry(pentry);
813ec560175SHuang Ying 		if (unlikely(non_swap_entry(entry)))
814ec560175SHuang Ying 			continue;
8154f8fcf4cSHugh Dickins 		pte_unmap(pte);
8164f8fcf4cSHugh Dickins 		pte = NULL;
817ec560175SHuang Ying 		page = __read_swap_cache_async(entry, gfp_mask, vma,
8184f8fcf4cSHugh Dickins 					       addr, &page_allocated);
819ec560175SHuang Ying 		if (!page)
820ec560175SHuang Ying 			continue;
821ec560175SHuang Ying 		if (page_allocated) {
8225169b844SNeilBrown 			swap_readpage(page, false, &splug);
823eaf649ebSMinchan Kim 			if (i != ra_info.offset) {
824ec560175SHuang Ying 				SetPageReadahead(page);
825ec560175SHuang Ying 				count_vm_event(SWAP_RA);
826ec560175SHuang Ying 			}
827ec560175SHuang Ying 		}
828ec560175SHuang Ying 		put_page(page);
829ec560175SHuang Ying 	}
8304f8fcf4cSHugh Dickins 	if (pte)
8314f8fcf4cSHugh Dickins 		pte_unmap(pte);
832ec560175SHuang Ying 	blk_finish_plug(&plug);
8335169b844SNeilBrown 	swap_read_unplug(splug);
834ec560175SHuang Ying 	lru_add_drain();
835ec560175SHuang Ying skip:
8365169b844SNeilBrown 	/* The page was likely read above, so no need for plugging here */
837ec560175SHuang Ying 	return read_swap_cache_async(fentry, gfp_mask, vma, vmf->address,
838b243dcbfSSuren Baghdasaryan 				     NULL);
839ec560175SHuang Ying }
840d9bfcfdcSHuang Ying 
841e9e9b7ecSMinchan Kim /**
842e9e9b7ecSMinchan Kim  * swapin_readahead - swap in pages in hope we need them soon
843e9e9b7ecSMinchan Kim  * @entry: swap entry of this memory
844e9e9b7ecSMinchan Kim  * @gfp_mask: memory allocation flags
845e9e9b7ecSMinchan Kim  * @vmf: fault information
846e9e9b7ecSMinchan Kim  *
847e9e9b7ecSMinchan Kim  * Returns the struct page for entry and addr, after queueing swapin.
848e9e9b7ecSMinchan Kim  *
849e9e9b7ecSMinchan Kim  * It's a main entry function for swap readahead. By the configuration,
850e9e9b7ecSMinchan Kim  * it will read ahead blocks by cluster-based(ie, physical disk based)
851e9e9b7ecSMinchan Kim  * or vma-based(ie, virtual address based on faulty address) readahead.
852e9e9b7ecSMinchan Kim  */
swapin_readahead(swp_entry_t entry,gfp_t gfp_mask,struct vm_fault * vmf)853e9e9b7ecSMinchan Kim struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
854e9e9b7ecSMinchan Kim 				struct vm_fault *vmf)
855e9e9b7ecSMinchan Kim {
856e9e9b7ecSMinchan Kim 	return swap_use_vma_readahead() ?
857e9e9b7ecSMinchan Kim 			swap_vma_readahead(entry, gfp_mask, vmf) :
858e9e9b7ecSMinchan Kim 			swap_cluster_readahead(entry, gfp_mask, vmf);
859e9e9b7ecSMinchan Kim }
860e9e9b7ecSMinchan Kim 
861d9bfcfdcSHuang Ying #ifdef CONFIG_SYSFS
vma_ra_enabled_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)862d9bfcfdcSHuang Ying static ssize_t vma_ra_enabled_show(struct kobject *kobj,
863d9bfcfdcSHuang Ying 				     struct kobj_attribute *attr, char *buf)
864d9bfcfdcSHuang Ying {
865ae7a927dSJoe Perches 	return sysfs_emit(buf, "%s\n",
866ae7a927dSJoe Perches 			  enable_vma_readahead ? "true" : "false");
867d9bfcfdcSHuang Ying }
vma_ra_enabled_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)868d9bfcfdcSHuang Ying static ssize_t vma_ra_enabled_store(struct kobject *kobj,
869d9bfcfdcSHuang Ying 				      struct kobj_attribute *attr,
870d9bfcfdcSHuang Ying 				      const char *buf, size_t count)
871d9bfcfdcSHuang Ying {
872717aeab4SJagdish Gediya 	ssize_t ret;
873717aeab4SJagdish Gediya 
874717aeab4SJagdish Gediya 	ret = kstrtobool(buf, &enable_vma_readahead);
875717aeab4SJagdish Gediya 	if (ret)
876717aeab4SJagdish Gediya 		return ret;
877d9bfcfdcSHuang Ying 
878d9bfcfdcSHuang Ying 	return count;
879d9bfcfdcSHuang Ying }
8806106b93eSMiaohe Lin static struct kobj_attribute vma_ra_enabled_attr = __ATTR_RW(vma_ra_enabled);
881d9bfcfdcSHuang Ying 
882d9bfcfdcSHuang Ying static struct attribute *swap_attrs[] = {
883d9bfcfdcSHuang Ying 	&vma_ra_enabled_attr.attr,
884d9bfcfdcSHuang Ying 	NULL,
885d9bfcfdcSHuang Ying };
886d9bfcfdcSHuang Ying 
887e48333b6SRikard Falkeborn static const struct attribute_group swap_attr_group = {
888d9bfcfdcSHuang Ying 	.attrs = swap_attrs,
889d9bfcfdcSHuang Ying };
890d9bfcfdcSHuang Ying 
swap_init_sysfs(void)891d9bfcfdcSHuang Ying static int __init swap_init_sysfs(void)
892d9bfcfdcSHuang Ying {
893d9bfcfdcSHuang Ying 	int err;
894d9bfcfdcSHuang Ying 	struct kobject *swap_kobj;
895d9bfcfdcSHuang Ying 
896d9bfcfdcSHuang Ying 	swap_kobj = kobject_create_and_add("swap", mm_kobj);
897d9bfcfdcSHuang Ying 	if (!swap_kobj) {
898d9bfcfdcSHuang Ying 		pr_err("failed to create swap kobject\n");
899d9bfcfdcSHuang Ying 		return -ENOMEM;
900d9bfcfdcSHuang Ying 	}
901d9bfcfdcSHuang Ying 	err = sysfs_create_group(swap_kobj, &swap_attr_group);
902d9bfcfdcSHuang Ying 	if (err) {
903d9bfcfdcSHuang Ying 		pr_err("failed to register swap group\n");
904d9bfcfdcSHuang Ying 		goto delete_obj;
905d9bfcfdcSHuang Ying 	}
906d9bfcfdcSHuang Ying 	return 0;
907d9bfcfdcSHuang Ying 
908d9bfcfdcSHuang Ying delete_obj:
909d9bfcfdcSHuang Ying 	kobject_put(swap_kobj);
910d9bfcfdcSHuang Ying 	return err;
911d9bfcfdcSHuang Ying }
912d9bfcfdcSHuang Ying subsys_initcall(swap_init_sysfs);
913d9bfcfdcSHuang Ying #endif
914