blob: 850314c2c3abb2ad0ee40c8d3becb5ddb755247f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/mm/swap_state.c
4 *
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
6 * Swap reorganised 29.12.95, Stephen Tweedie
7 *
8 * Rewritten to use page cache, (C) 1998 Stephen Tweedie
9 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel_stat.h>
13#include <linux/swap.h>
Hugh Dickins46017e92008-02-04 22:28:41 -080014#include <linux/swapops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/backing-dev.h>
Christian Ehrhardt3fb5c292012-07-31 16:41:44 -070018#include <linux/blkdev.h>
Hugh Dickinsc484d412006-01-06 00:10:55 -080019#include <linux/pagevec.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080020#include <linux/migrate.h>
Huang, Ying4b3ef9d2017-02-22 15:45:26 -080021#include <linux/vmalloc.h>
Tim Chen67afa382017-02-22 15:45:39 -080022#include <linux/swap_slots.h>
Huang Ying38d8b4e2017-07-06 15:37:18 -070023#include <linux/huge_mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/pgtable.h>
26
27/*
28 * swapper_space is a fiction, retained to simplify the path through
Jens Axboe7eaceac2011-03-10 08:52:07 +010029 * vmscan's shrink_page_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070031static const struct address_space_operations swap_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 .writepage = swap_writepage,
Mel Gorman62c230b2012-07-31 16:44:55 -070033 .set_page_dirty = swap_set_page_dirty,
Andrew Morton1c939232014-10-09 15:27:59 -070034#ifdef CONFIG_MIGRATION
Christoph Lametere965f962006-02-01 03:05:41 -080035 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -070036#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
Changbin Du783cb682017-11-15 17:36:06 -080039struct address_space *swapper_spaces[MAX_SWAPFILES] __read_mostly;
40static unsigned int nr_swapper_spaces[MAX_SWAPFILES] __read_mostly;
Colin Ian Kingf5c754d2018-04-05 16:25:05 -070041static bool enable_vma_readahead __read_mostly = true;
Huang Yingec560172017-09-06 16:24:36 -070042
Huang Yingec560172017-09-06 16:24:36 -070043#define SWAP_RA_WIN_SHIFT (PAGE_SHIFT / 2)
44#define SWAP_RA_HITS_MASK ((1UL << SWAP_RA_WIN_SHIFT) - 1)
45#define SWAP_RA_HITS_MAX SWAP_RA_HITS_MASK
46#define SWAP_RA_WIN_MASK (~PAGE_MASK & ~SWAP_RA_HITS_MASK)
47
48#define SWAP_RA_HITS(v) ((v) & SWAP_RA_HITS_MASK)
49#define SWAP_RA_WIN(v) (((v) & SWAP_RA_WIN_MASK) >> SWAP_RA_WIN_SHIFT)
50#define SWAP_RA_ADDR(v) ((v) & PAGE_MASK)
51
52#define SWAP_RA_VAL(addr, win, hits) \
53 (((addr) & PAGE_MASK) | \
54 (((win) << SWAP_RA_WIN_SHIFT) & SWAP_RA_WIN_MASK) | \
55 ((hits) & SWAP_RA_HITS_MASK))
56
57/* Initial readahead hits is 4 to start up with a small window */
58#define GET_SWAP_RA_VAL(vma) \
59 (atomic_long_read(&(vma)->swap_readahead_info) ? : 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#define INC_CACHE_INFO(x) do { swap_cache_info.x++; } while (0)
Huang Ying38d8b4e2017-07-06 15:37:18 -070062#define ADD_CACHE_INFO(x, nr) do { swap_cache_info.x += (nr); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64static struct {
65 unsigned long add_total;
66 unsigned long del_total;
67 unsigned long find_success;
68 unsigned long find_total;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069} swap_cache_info;
70
Shaohua Li33806f02013-02-22 16:34:37 -080071unsigned long total_swapcache_pages(void)
72{
Huang, Ying4b3ef9d2017-02-22 15:45:26 -080073 unsigned int i, j, nr;
Shaohua Li33806f02013-02-22 16:34:37 -080074 unsigned long ret = 0;
Huang, Ying4b3ef9d2017-02-22 15:45:26 -080075 struct address_space *spaces;
Shaohua Li33806f02013-02-22 16:34:37 -080076
Huang, Ying4b3ef9d2017-02-22 15:45:26 -080077 rcu_read_lock();
78 for (i = 0; i < MAX_SWAPFILES; i++) {
79 /*
80 * The corresponding entries in nr_swapper_spaces and
81 * swapper_spaces will be reused only after at least
82 * one grace period. So it is impossible for them
83 * belongs to different usage.
84 */
85 nr = nr_swapper_spaces[i];
86 spaces = rcu_dereference(swapper_spaces[i]);
87 if (!nr || !spaces)
88 continue;
89 for (j = 0; j < nr; j++)
90 ret += spaces[j].nrpages;
91 }
92 rcu_read_unlock();
Shaohua Li33806f02013-02-22 16:34:37 -080093 return ret;
94}
95
Shaohua Li579f8292014-02-06 12:04:21 -080096static atomic_t swapin_readahead_hits = ATOMIC_INIT(4);
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098void show_swap_cache_info(void)
99{
Shaohua Li33806f02013-02-22 16:34:37 -0800100 printk("%lu pages in swap cache\n", total_swapcache_pages());
Johannes Weiner2c97b7f2008-07-25 19:46:01 -0700101 printk("Swap cache stats: add %lu, delete %lu, find %lu/%lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 swap_cache_info.add_total, swap_cache_info.del_total,
Hugh Dickinsbb63be02008-02-04 22:28:49 -0800103 swap_cache_info.find_success, swap_cache_info.find_total);
Shaohua Liec8acf22013-02-22 16:34:38 -0800104 printk("Free swap = %ldkB\n",
105 get_nr_swap_pages() << (PAGE_SHIFT - 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10));
107}
108
109/*
Matthew Wilcox8d93b412017-11-27 15:46:54 -0500110 * add_to_swap_cache resembles add_to_page_cache_locked on swapper_space,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * but sets SwapCache flag and private instead of mapping and index.
112 */
Matthew Wilcox8d93b412017-11-27 15:46:54 -0500113int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Matthew Wilcox8d93b412017-11-27 15:46:54 -0500115 struct address_space *address_space = swap_address_space(entry);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700116 pgoff_t idx = swp_offset(entry);
Matthew Wilcox8d93b412017-11-27 15:46:54 -0500117 XA_STATE_ORDER(xas, &address_space->i_pages, idx, compound_order(page));
118 unsigned long i, nr = 1UL << compound_order(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Sasha Levin309381fea2014-01-23 15:52:54 -0800120 VM_BUG_ON_PAGE(!PageLocked(page), page);
121 VM_BUG_ON_PAGE(PageSwapCache(page), page);
122 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Hugh Dickins51726b12009-01-06 14:39:25 -0800123
Huang Ying38d8b4e2017-07-06 15:37:18 -0700124 page_ref_add(page, nr);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700125 SetPageSwapCache(page);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700126
Matthew Wilcox8d93b412017-11-27 15:46:54 -0500127 do {
128 xas_lock_irq(&xas);
129 xas_create_range(&xas);
130 if (xas_error(&xas))
131 goto unlock;
132 for (i = 0; i < nr; i++) {
133 VM_BUG_ON_PAGE(xas.xa_index != idx + i, page);
134 set_page_private(page + i, entry.val + i);
135 xas_store(&xas, page + i);
136 xas_next(&xas);
137 }
Huang Ying38d8b4e2017-07-06 15:37:18 -0700138 address_space->nrpages += nr;
139 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
140 ADD_CACHE_INFO(add_total, nr);
Matthew Wilcox8d93b412017-11-27 15:46:54 -0500141unlock:
142 xas_unlock_irq(&xas);
143 } while (xas_nomem(&xas, gfp));
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700144
Matthew Wilcox8d93b412017-11-27 15:46:54 -0500145 if (!xas_error(&xas))
146 return 0;
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700147
Matthew Wilcox8d93b412017-11-27 15:46:54 -0500148 ClearPageSwapCache(page);
149 page_ref_sub(page, nr);
150 return xas_error(&xas);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
154 * This must be called only on pages that have
155 * been verified to be in the swap cache.
156 */
157void __delete_from_swap_cache(struct page *page)
158{
Shaohua Li33806f02013-02-22 16:34:37 -0800159 struct address_space *address_space;
Huang Ying38d8b4e2017-07-06 15:37:18 -0700160 int i, nr = hpage_nr_pages(page);
161 swp_entry_t entry;
162 pgoff_t idx;
Shaohua Li33806f02013-02-22 16:34:37 -0800163
Sasha Levin309381fea2014-01-23 15:52:54 -0800164 VM_BUG_ON_PAGE(!PageLocked(page), page);
165 VM_BUG_ON_PAGE(!PageSwapCache(page), page);
166 VM_BUG_ON_PAGE(PageWriteback(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Shaohua Li33806f02013-02-22 16:34:37 -0800168 entry.val = page_private(page);
169 address_space = swap_address_space(entry);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700170 idx = swp_offset(entry);
171 for (i = 0; i < nr; i++) {
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700172 radix_tree_delete(&address_space->i_pages, idx + i);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700173 set_page_private(page + i, 0);
174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 ClearPageSwapCache(page);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700176 address_space->nrpages -= nr;
177 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
178 ADD_CACHE_INFO(del_total, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/**
182 * add_to_swap - allocate swap space for a page
183 * @page: page we want to move to swap
184 *
185 * Allocate swap space for the page and add the page to the
186 * swap cache. Caller needs to hold the page lock.
187 */
Minchan Kim0f074652017-07-06 15:37:24 -0700188int add_to_swap(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 swp_entry_t entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int err;
192
Sasha Levin309381fea2014-01-23 15:52:54 -0800193 VM_BUG_ON_PAGE(!PageLocked(page), page);
194 VM_BUG_ON_PAGE(!PageUptodate(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Huang Ying38d8b4e2017-07-06 15:37:18 -0700196 entry = get_swap_page(page);
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700197 if (!entry.val)
Minchan Kim0f074652017-07-06 15:37:24 -0700198 return 0;
199
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700200 /*
Matthew Wilcox8d93b412017-11-27 15:46:54 -0500201 * XArray node allocations from PF_MEMALLOC contexts could
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700202 * completely exhaust the page allocator. __GFP_NOMEMALLOC
203 * stops emergency reserves from being allocated.
204 *
205 * TODO: this could cause a theoretical memory reclaim
206 * deadlock in the swap out path.
207 */
208 /*
Minchan Kim854e9ed2016-01-15 16:54:53 -0800209 * Add it to the swap cache.
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700210 */
211 err = add_to_swap_cache(page, entry,
212 __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700213 if (err)
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700214 /*
215 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
216 * clear SWAP_HAS_CACHE flag.
217 */
Minchan Kim0f074652017-07-06 15:37:24 -0700218 goto fail;
Shaohua Li96254562017-10-03 16:15:32 -0700219 /*
220 * Normally the page will be dirtied in unmap because its pte should be
221 * dirty. A special case is MADV_FREE page. The page'e pte could have
222 * dirty bit cleared but the page's SwapBacked bit is still set because
223 * clearing the dirty bit and SwapBacked bit has no lock protected. For
224 * such page, unmap will not set dirty bit for it, so page reclaim will
225 * not write the page out. This can cause data corruption when the page
226 * is swap in later. Always setting the dirty bit for the page solves
227 * the problem.
228 */
229 set_page_dirty(page);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700230
231 return 1;
232
Huang Ying38d8b4e2017-07-06 15:37:18 -0700233fail:
Minchan Kim0f074652017-07-06 15:37:24 -0700234 put_swap_page(page, entry);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/*
239 * This must be called only on pages that have
240 * been verified to be in the swap cache and locked.
241 * It will never put the page into the free list,
242 * the caller has a reference on the page.
243 */
244void delete_from_swap_cache(struct page *page)
245{
246 swp_entry_t entry;
Shaohua Li33806f02013-02-22 16:34:37 -0800247 struct address_space *address_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700249 entry.val = page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Shaohua Li33806f02013-02-22 16:34:37 -0800251 address_space = swap_address_space(entry);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700252 xa_lock_irq(&address_space->i_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 __delete_from_swap_cache(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700254 xa_unlock_irq(&address_space->i_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Minchan Kim75f6d6d2017-07-06 15:37:21 -0700256 put_swap_page(page, entry);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700257 page_ref_sub(page, hpage_nr_pages(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260/*
261 * If we are the only user, then try to free up the swap cache.
262 *
263 * Its ok to check for PageSwapCache without the page lock
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800264 * here because we are going to recheck again inside
265 * try_to_free_swap() _with_ the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 * - Marcelo
267 */
268static inline void free_swap_cache(struct page *page)
269{
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800270 if (PageSwapCache(page) && !page_mapped(page) && trylock_page(page)) {
271 try_to_free_swap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 unlock_page(page);
273 }
274}
275
276/*
277 * Perform a free_page(), also freeing any swap cache associated with
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700278 * this page if it is the last user of the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
280void free_page_and_swap_cache(struct page *page)
281{
282 free_swap_cache(page);
Aaron Lu6fcb52a2016-10-07 17:00:08 -0700283 if (!is_huge_zero_page(page))
Gerald Schaefer770a5372016-06-08 15:33:50 -0700284 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287/*
288 * Passed an array of pages, drop them all from swapcache and then release
289 * them. They are removed from the LRU and freed if this is their last use.
290 */
291void free_pages_and_swap_cache(struct page **pages, int nr)
292{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 struct page **pagep = pages;
Michal Hockoaabfb572014-10-09 15:28:52 -0700294 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 lru_add_drain();
Michal Hockoaabfb572014-10-09 15:28:52 -0700297 for (i = 0; i < nr; i++)
298 free_swap_cache(pagep[i]);
Mel Gormanc6f92f92017-11-15 17:37:55 -0800299 release_pages(pagep, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Minchan Kime9e9b7e2018-04-05 16:23:42 -0700302static inline bool swap_use_vma_readahead(void)
303{
304 return READ_ONCE(enable_vma_readahead) && !atomic_read(&nr_rotate_swap);
305}
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307/*
308 * Lookup a swap entry in the swap cache. A found page will be returned
309 * unlocked and with its refcount incremented - we rely on the kernel
310 * lock getting page table operations atomic even if we drop the page
311 * lock before returning.
312 */
Huang Yingec560172017-09-06 16:24:36 -0700313struct page *lookup_swap_cache(swp_entry_t entry, struct vm_area_struct *vma,
314 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct page *page;
317
Huang Yingf6ab1f72016-10-07 17:00:21 -0700318 page = find_get_page(swap_address_space(entry), swp_offset(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Huang Yingec560172017-09-06 16:24:36 -0700320 INC_CACHE_INFO(find_total);
321 if (page) {
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700322 bool vma_ra = swap_use_vma_readahead();
323 bool readahead;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 INC_CACHE_INFO(find_success);
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700326 /*
327 * At the moment, we don't support PG_readahead for anon THP
328 * so let's bail out rather than confusing the readahead stat.
329 */
Huang Yingec560172017-09-06 16:24:36 -0700330 if (unlikely(PageTransCompound(page)))
331 return page;
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700332
Huang Yingec560172017-09-06 16:24:36 -0700333 readahead = TestClearPageReadahead(page);
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700334 if (vma && vma_ra) {
335 unsigned long ra_val;
336 int win, hits;
337
338 ra_val = GET_SWAP_RA_VAL(vma);
339 win = SWAP_RA_WIN(ra_val);
340 hits = SWAP_RA_HITS(ra_val);
Huang Yingec560172017-09-06 16:24:36 -0700341 if (readahead)
342 hits = min_t(int, hits + 1, SWAP_RA_HITS_MAX);
343 atomic_long_set(&vma->swap_readahead_info,
344 SWAP_RA_VAL(addr, win, hits));
345 }
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700346
Huang Yingec560172017-09-06 16:24:36 -0700347 if (readahead) {
Huang Yingcbc65df2017-09-06 16:24:29 -0700348 count_vm_event(SWAP_RA_HIT);
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700349 if (!vma || !vma_ra)
Huang Yingec560172017-09-06 16:24:36 -0700350 atomic_inc(&swapin_readahead_hits);
Huang Yingcbc65df2017-09-06 16:24:29 -0700351 }
Shaohua Li579f8292014-02-06 12:04:21 -0800352 }
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return page;
355}
356
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700357struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
358 struct vm_area_struct *vma, unsigned long addr,
359 bool *new_page_allocated)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 struct page *found_page, *new_page = NULL;
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700362 struct address_space *swapper_space = swap_address_space(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 int err;
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700364 *new_page_allocated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 do {
367 /*
368 * First check the swap cache. Since this is normally
369 * called after lookup_swap_cache() failed, re-calling
370 * that would confuse statistics.
371 */
Huang Yingf6ab1f72016-10-07 17:00:21 -0700372 found_page = find_get_page(swapper_space, swp_offset(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (found_page)
374 break;
375
Huang Yingba81f832017-02-22 15:45:46 -0800376 /*
377 * Just skip read ahead for unused swap slot.
378 * During swap_off when swap_slot_cache is disabled,
379 * we have to handle the race between putting
380 * swap entry in swap cache and marking swap slot
381 * as SWAP_HAS_CACHE. That's done in later part of code or
382 * else swap_off will be aborted if we return NULL.
383 */
384 if (!__swp_swapcount(entry) && swap_slot_cache_enabled)
385 break;
Tim Chene8c26ab2017-02-22 15:45:29 -0800386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /*
388 * Get a new page to read into from swap.
389 */
390 if (!new_page) {
Hugh Dickins02098fe2008-02-04 22:28:42 -0800391 new_page = alloc_page_vma(gfp_mask, vma, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (!new_page)
393 break; /* Out of memory */
394 }
395
396 /*
Hugh Dickinsf0009442008-02-04 22:28:49 -0800397 * Swap entry may have been freed since our caller observed it.
398 */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700399 err = swapcache_prepare(entry);
Rafael Aquinicbab0e42013-06-12 14:04:49 -0700400 if (err == -EEXIST) {
Rafael Aquinicbab0e42013-06-12 14:04:49 -0700401 /*
402 * We might race against get_swap_page() and stumble
403 * across a SWAP_HAS_CACHE swap_map entry whose page
Huang Ying9c1cc2e2017-05-03 14:54:33 -0700404 * has not been brought into the swapcache yet.
Rafael Aquinicbab0e42013-06-12 14:04:49 -0700405 */
406 cond_resched();
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700407 continue;
Matthew Wilcox8d93b412017-11-27 15:46:54 -0500408 } else if (err) /* swp entry is obsolete ? */
Hugh Dickinsf0009442008-02-04 22:28:49 -0800409 break;
410
Matthew Wilcox8d93b412017-11-27 15:46:54 -0500411 /* May fail (-ENOMEM) if XArray node allocation failed. */
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800412 __SetPageLocked(new_page);
Hugh Dickinsfa9949d2016-05-19 17:12:41 -0700413 __SetPageSwapBacked(new_page);
Matthew Wilcox8d93b412017-11-27 15:46:54 -0500414 err = add_to_swap_cache(new_page, entry, gfp_mask & GFP_KERNEL);
Nick Piggin529ae9a2008-08-02 12:01:03 +0200415 if (likely(!err)) {
Matthew Wilcox8d93b412017-11-27 15:46:54 -0500416 /* Initiate read into locked page */
Rik van Rielc5fdae42008-10-18 20:26:36 -0700417 lru_cache_add_anon(new_page);
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700418 *new_page_allocated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return new_page;
420 }
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800421 __ClearPageLocked(new_page);
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700422 /*
423 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
424 * clear SWAP_HAS_CACHE flag.
425 */
Minchan Kim75f6d6d2017-07-06 15:37:21 -0700426 put_swap_page(new_page, entry);
Hugh Dickinsf0009442008-02-04 22:28:49 -0800427 } while (err != -ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 if (new_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300430 put_page(new_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return found_page;
432}
Hugh Dickins46017e92008-02-04 22:28:41 -0800433
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700434/*
435 * Locate a page of swap in physical memory, reserving swap cache space
436 * and reading the disk if it is not already cached.
437 * A failure return means that either the page allocation failed or that
438 * the swap entry is no longer in use.
439 */
440struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
Shaohua Li23955622017-07-10 15:47:11 -0700441 struct vm_area_struct *vma, unsigned long addr, bool do_poll)
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700442{
443 bool page_was_allocated;
444 struct page *retpage = __read_swap_cache_async(entry, gfp_mask,
445 vma, addr, &page_was_allocated);
446
447 if (page_was_allocated)
Shaohua Li23955622017-07-10 15:47:11 -0700448 swap_readpage(retpage, do_poll);
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700449
450 return retpage;
451}
452
Huang Yingec560172017-09-06 16:24:36 -0700453static unsigned int __swapin_nr_pages(unsigned long prev_offset,
454 unsigned long offset,
455 int hits,
456 int max_pages,
457 int prev_win)
Shaohua Li579f8292014-02-06 12:04:21 -0800458{
Huang Yingec560172017-09-06 16:24:36 -0700459 unsigned int pages, last_ra;
Shaohua Li579f8292014-02-06 12:04:21 -0800460
461 /*
462 * This heuristic has been found to work well on both sequential and
463 * random loads, swapping to hard disk or to SSD: please don't ask
464 * what the "+ 2" means, it just happens to work well, that's all.
465 */
Huang Yingec560172017-09-06 16:24:36 -0700466 pages = hits + 2;
Shaohua Li579f8292014-02-06 12:04:21 -0800467 if (pages == 2) {
468 /*
469 * We can have no readahead hits to judge by: but must not get
470 * stuck here forever, so check for an adjacent offset instead
471 * (and don't even bother to check whether swap type is same).
472 */
473 if (offset != prev_offset + 1 && offset != prev_offset - 1)
474 pages = 1;
Shaohua Li579f8292014-02-06 12:04:21 -0800475 } else {
476 unsigned int roundup = 4;
477 while (roundup < pages)
478 roundup <<= 1;
479 pages = roundup;
480 }
481
482 if (pages > max_pages)
483 pages = max_pages;
484
485 /* Don't shrink readahead too fast */
Huang Yingec560172017-09-06 16:24:36 -0700486 last_ra = prev_win / 2;
Shaohua Li579f8292014-02-06 12:04:21 -0800487 if (pages < last_ra)
488 pages = last_ra;
Huang Yingec560172017-09-06 16:24:36 -0700489
490 return pages;
491}
492
493static unsigned long swapin_nr_pages(unsigned long offset)
494{
495 static unsigned long prev_offset;
496 unsigned int hits, pages, max_pages;
497 static atomic_t last_readahead_pages;
498
499 max_pages = 1 << READ_ONCE(page_cluster);
500 if (max_pages <= 1)
501 return 1;
502
503 hits = atomic_xchg(&swapin_readahead_hits, 0);
504 pages = __swapin_nr_pages(prev_offset, offset, hits, max_pages,
505 atomic_read(&last_readahead_pages));
506 if (!hits)
507 prev_offset = offset;
Shaohua Li579f8292014-02-06 12:04:21 -0800508 atomic_set(&last_readahead_pages, pages);
509
510 return pages;
511}
512
Hugh Dickins46017e92008-02-04 22:28:41 -0800513/**
Minchan Kime9e9b7e2018-04-05 16:23:42 -0700514 * swap_cluster_readahead - swap in pages in hope we need them soon
Hugh Dickins46017e92008-02-04 22:28:41 -0800515 * @entry: swap entry of this memory
Randy Dunlap76824862008-03-19 17:00:40 -0700516 * @gfp_mask: memory allocation flags
Minchan Kime9e9b7e2018-04-05 16:23:42 -0700517 * @vmf: fault information
Hugh Dickins46017e92008-02-04 22:28:41 -0800518 *
519 * Returns the struct page for entry and addr, after queueing swapin.
520 *
521 * Primitive swap readahead code. We simply read an aligned block of
522 * (1 << page_cluster) entries in the swap area. This method is chosen
523 * because it doesn't cost us any seek time. We also make sure to queue
524 * the 'original' request together with the readahead ones...
525 *
526 * This has been extended to use the NUMA policies from the mm triggering
527 * the readahead.
528 *
Minchan Kime9e9b7e2018-04-05 16:23:42 -0700529 * Caller must hold down_read on the vma->vm_mm if vmf->vma is not NULL.
Hugh Dickins46017e92008-02-04 22:28:41 -0800530 */
Minchan Kime9e9b7e2018-04-05 16:23:42 -0700531struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
532 struct vm_fault *vmf)
Hugh Dickins46017e92008-02-04 22:28:41 -0800533{
Hugh Dickins46017e92008-02-04 22:28:41 -0800534 struct page *page;
Shaohua Li579f8292014-02-06 12:04:21 -0800535 unsigned long entry_offset = swp_offset(entry);
536 unsigned long offset = entry_offset;
Rik van Riel67f96aa2012-03-21 16:33:50 -0700537 unsigned long start_offset, end_offset;
Shaohua Li579f8292014-02-06 12:04:21 -0800538 unsigned long mask;
Huang Yinge9a6eff2017-11-15 17:33:15 -0800539 struct swap_info_struct *si = swp_swap_info(entry);
Christian Ehrhardt3fb5c292012-07-31 16:41:44 -0700540 struct blk_plug plug;
Huang Yingc4fa6302017-09-06 16:24:33 -0700541 bool do_poll = true, page_allocated;
Minchan Kime9e9b7e2018-04-05 16:23:42 -0700542 struct vm_area_struct *vma = vmf->vma;
543 unsigned long addr = vmf->address;
Hugh Dickins46017e92008-02-04 22:28:41 -0800544
Shaohua Li579f8292014-02-06 12:04:21 -0800545 mask = swapin_nr_pages(offset) - 1;
546 if (!mask)
547 goto skip;
548
Shaohua Li23955622017-07-10 15:47:11 -0700549 do_poll = false;
Rik van Riel67f96aa2012-03-21 16:33:50 -0700550 /* Read a page_cluster sized and aligned cluster around offset. */
551 start_offset = offset & ~mask;
552 end_offset = offset | mask;
553 if (!start_offset) /* First page is swap header. */
554 start_offset++;
Huang Yinge9a6eff2017-11-15 17:33:15 -0800555 if (end_offset >= si->max)
556 end_offset = si->max - 1;
Rik van Riel67f96aa2012-03-21 16:33:50 -0700557
Christian Ehrhardt3fb5c292012-07-31 16:41:44 -0700558 blk_start_plug(&plug);
Rik van Riel67f96aa2012-03-21 16:33:50 -0700559 for (offset = start_offset; offset <= end_offset ; offset++) {
Hugh Dickins46017e92008-02-04 22:28:41 -0800560 /* Ok, do the async read-ahead now */
Huang Yingc4fa6302017-09-06 16:24:33 -0700561 page = __read_swap_cache_async(
562 swp_entry(swp_type(entry), offset),
563 gfp_mask, vma, addr, &page_allocated);
Hugh Dickins46017e92008-02-04 22:28:41 -0800564 if (!page)
Rik van Riel67f96aa2012-03-21 16:33:50 -0700565 continue;
Huang Yingc4fa6302017-09-06 16:24:33 -0700566 if (page_allocated) {
567 swap_readpage(page, false);
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700568 if (offset != entry_offset) {
Huang Yingc4fa6302017-09-06 16:24:33 -0700569 SetPageReadahead(page);
570 count_vm_event(SWAP_RA);
571 }
Huang Yingcbc65df2017-09-06 16:24:29 -0700572 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300573 put_page(page);
Hugh Dickins46017e92008-02-04 22:28:41 -0800574 }
Christian Ehrhardt3fb5c292012-07-31 16:41:44 -0700575 blk_finish_plug(&plug);
576
Hugh Dickins46017e92008-02-04 22:28:41 -0800577 lru_add_drain(); /* Push any new pages onto the LRU now */
Shaohua Li579f8292014-02-06 12:04:21 -0800578skip:
Shaohua Li23955622017-07-10 15:47:11 -0700579 return read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll);
Hugh Dickins46017e92008-02-04 22:28:41 -0800580}
Huang, Ying4b3ef9d2017-02-22 15:45:26 -0800581
582int init_swap_address_space(unsigned int type, unsigned long nr_pages)
583{
584 struct address_space *spaces, *space;
585 unsigned int i, nr;
586
587 nr = DIV_ROUND_UP(nr_pages, SWAP_ADDRESS_SPACE_PAGES);
Kees Cook778e1cd2018-06-12 14:04:48 -0700588 spaces = kvcalloc(nr, sizeof(struct address_space), GFP_KERNEL);
Huang, Ying4b3ef9d2017-02-22 15:45:26 -0800589 if (!spaces)
590 return -ENOMEM;
591 for (i = 0; i < nr; i++) {
592 space = spaces + i;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700593 INIT_RADIX_TREE(&space->i_pages, GFP_ATOMIC|__GFP_NOWARN);
Huang, Ying4b3ef9d2017-02-22 15:45:26 -0800594 atomic_set(&space->i_mmap_writable, 0);
595 space->a_ops = &swap_aops;
596 /* swap cache doesn't use writeback related tags */
597 mapping_set_no_writeback_tags(space);
Huang, Ying4b3ef9d2017-02-22 15:45:26 -0800598 }
599 nr_swapper_spaces[type] = nr;
600 rcu_assign_pointer(swapper_spaces[type], spaces);
601
602 return 0;
603}
604
605void exit_swap_address_space(unsigned int type)
606{
607 struct address_space *spaces;
608
609 spaces = swapper_spaces[type];
610 nr_swapper_spaces[type] = 0;
611 rcu_assign_pointer(swapper_spaces[type], NULL);
612 synchronize_rcu();
613 kvfree(spaces);
614}
Huang Yingec560172017-09-06 16:24:36 -0700615
616static inline void swap_ra_clamp_pfn(struct vm_area_struct *vma,
617 unsigned long faddr,
618 unsigned long lpfn,
619 unsigned long rpfn,
620 unsigned long *start,
621 unsigned long *end)
622{
623 *start = max3(lpfn, PFN_DOWN(vma->vm_start),
624 PFN_DOWN(faddr & PMD_MASK));
625 *end = min3(rpfn, PFN_DOWN(vma->vm_end),
626 PFN_DOWN((faddr & PMD_MASK) + PMD_SIZE));
627}
628
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700629static void swap_ra_info(struct vm_fault *vmf,
630 struct vma_swap_readahead *ra_info)
Huang Yingec560172017-09-06 16:24:36 -0700631{
632 struct vm_area_struct *vma = vmf->vma;
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700633 unsigned long ra_val;
Huang Yingec560172017-09-06 16:24:36 -0700634 swp_entry_t entry;
635 unsigned long faddr, pfn, fpfn;
636 unsigned long start, end;
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700637 pte_t *pte, *orig_pte;
Huang Yingec560172017-09-06 16:24:36 -0700638 unsigned int max_win, hits, prev_win, win, left;
639#ifndef CONFIG_64BIT
640 pte_t *tpte;
641#endif
642
Huang Ying61b63972017-10-13 15:58:29 -0700643 max_win = 1 << min_t(unsigned int, READ_ONCE(page_cluster),
644 SWAP_RA_ORDER_CEILING);
645 if (max_win == 1) {
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700646 ra_info->win = 1;
647 return;
Huang Ying61b63972017-10-13 15:58:29 -0700648 }
649
Huang Yingec560172017-09-06 16:24:36 -0700650 faddr = vmf->address;
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700651 orig_pte = pte = pte_offset_map(vmf->pmd, faddr);
652 entry = pte_to_swp_entry(*pte);
653 if ((unlikely(non_swap_entry(entry)))) {
654 pte_unmap(orig_pte);
655 return;
656 }
Huang Yingec560172017-09-06 16:24:36 -0700657
Huang Yingec560172017-09-06 16:24:36 -0700658 fpfn = PFN_DOWN(faddr);
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700659 ra_val = GET_SWAP_RA_VAL(vma);
660 pfn = PFN_DOWN(SWAP_RA_ADDR(ra_val));
661 prev_win = SWAP_RA_WIN(ra_val);
662 hits = SWAP_RA_HITS(ra_val);
663 ra_info->win = win = __swapin_nr_pages(pfn, fpfn, hits,
Huang Yingec560172017-09-06 16:24:36 -0700664 max_win, prev_win);
665 atomic_long_set(&vma->swap_readahead_info,
666 SWAP_RA_VAL(faddr, win, 0));
667
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700668 if (win == 1) {
669 pte_unmap(orig_pte);
670 return;
671 }
Huang Yingec560172017-09-06 16:24:36 -0700672
673 /* Copy the PTEs because the page table may be unmapped */
674 if (fpfn == pfn + 1)
675 swap_ra_clamp_pfn(vma, faddr, fpfn, fpfn + win, &start, &end);
676 else if (pfn == fpfn + 1)
677 swap_ra_clamp_pfn(vma, faddr, fpfn - win + 1, fpfn + 1,
678 &start, &end);
679 else {
680 left = (win - 1) / 2;
681 swap_ra_clamp_pfn(vma, faddr, fpfn - left, fpfn + win - left,
682 &start, &end);
683 }
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700684 ra_info->nr_pte = end - start;
685 ra_info->offset = fpfn - start;
686 pte -= ra_info->offset;
Huang Yingec560172017-09-06 16:24:36 -0700687#ifdef CONFIG_64BIT
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700688 ra_info->ptes = pte;
Huang Yingec560172017-09-06 16:24:36 -0700689#else
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700690 tpte = ra_info->ptes;
Huang Yingec560172017-09-06 16:24:36 -0700691 for (pfn = start; pfn != end; pfn++)
692 *tpte++ = *pte++;
693#endif
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700694 pte_unmap(orig_pte);
Huang Yingec560172017-09-06 16:24:36 -0700695}
696
Colin Ian Kingf5c754d2018-04-05 16:25:05 -0700697static struct page *swap_vma_readahead(swp_entry_t fentry, gfp_t gfp_mask,
698 struct vm_fault *vmf)
Huang Yingec560172017-09-06 16:24:36 -0700699{
700 struct blk_plug plug;
701 struct vm_area_struct *vma = vmf->vma;
702 struct page *page;
703 pte_t *pte, pentry;
704 swp_entry_t entry;
705 unsigned int i;
706 bool page_allocated;
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700707 struct vma_swap_readahead ra_info = {0,};
Huang Yingec560172017-09-06 16:24:36 -0700708
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700709 swap_ra_info(vmf, &ra_info);
710 if (ra_info.win == 1)
Huang Yingec560172017-09-06 16:24:36 -0700711 goto skip;
712
713 blk_start_plug(&plug);
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700714 for (i = 0, pte = ra_info.ptes; i < ra_info.nr_pte;
Huang Yingec560172017-09-06 16:24:36 -0700715 i++, pte++) {
716 pentry = *pte;
717 if (pte_none(pentry))
718 continue;
719 if (pte_present(pentry))
720 continue;
721 entry = pte_to_swp_entry(pentry);
722 if (unlikely(non_swap_entry(entry)))
723 continue;
724 page = __read_swap_cache_async(entry, gfp_mask, vma,
725 vmf->address, &page_allocated);
726 if (!page)
727 continue;
728 if (page_allocated) {
729 swap_readpage(page, false);
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700730 if (i != ra_info.offset) {
Huang Yingec560172017-09-06 16:24:36 -0700731 SetPageReadahead(page);
732 count_vm_event(SWAP_RA);
733 }
734 }
735 put_page(page);
736 }
737 blk_finish_plug(&plug);
738 lru_add_drain();
739skip:
740 return read_swap_cache_async(fentry, gfp_mask, vma, vmf->address,
Minchan Kimeaf649eb2018-04-05 16:23:39 -0700741 ra_info.win == 1);
Huang Yingec560172017-09-06 16:24:36 -0700742}
Huang Yingd9bfcfd2017-09-06 16:24:40 -0700743
Minchan Kime9e9b7e2018-04-05 16:23:42 -0700744/**
745 * swapin_readahead - swap in pages in hope we need them soon
746 * @entry: swap entry of this memory
747 * @gfp_mask: memory allocation flags
748 * @vmf: fault information
749 *
750 * Returns the struct page for entry and addr, after queueing swapin.
751 *
752 * It's a main entry function for swap readahead. By the configuration,
753 * it will read ahead blocks by cluster-based(ie, physical disk based)
754 * or vma-based(ie, virtual address based on faulty address) readahead.
755 */
756struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
757 struct vm_fault *vmf)
758{
759 return swap_use_vma_readahead() ?
760 swap_vma_readahead(entry, gfp_mask, vmf) :
761 swap_cluster_readahead(entry, gfp_mask, vmf);
762}
763
Huang Yingd9bfcfd2017-09-06 16:24:40 -0700764#ifdef CONFIG_SYSFS
765static ssize_t vma_ra_enabled_show(struct kobject *kobj,
766 struct kobj_attribute *attr, char *buf)
767{
Minchan Kime9e9b7e2018-04-05 16:23:42 -0700768 return sprintf(buf, "%s\n", enable_vma_readahead ? "true" : "false");
Huang Yingd9bfcfd2017-09-06 16:24:40 -0700769}
770static ssize_t vma_ra_enabled_store(struct kobject *kobj,
771 struct kobj_attribute *attr,
772 const char *buf, size_t count)
773{
774 if (!strncmp(buf, "true", 4) || !strncmp(buf, "1", 1))
Minchan Kime9e9b7e2018-04-05 16:23:42 -0700775 enable_vma_readahead = true;
Huang Yingd9bfcfd2017-09-06 16:24:40 -0700776 else if (!strncmp(buf, "false", 5) || !strncmp(buf, "0", 1))
Minchan Kime9e9b7e2018-04-05 16:23:42 -0700777 enable_vma_readahead = false;
Huang Yingd9bfcfd2017-09-06 16:24:40 -0700778 else
779 return -EINVAL;
780
781 return count;
782}
783static struct kobj_attribute vma_ra_enabled_attr =
784 __ATTR(vma_ra_enabled, 0644, vma_ra_enabled_show,
785 vma_ra_enabled_store);
786
Huang Yingd9bfcfd2017-09-06 16:24:40 -0700787static struct attribute *swap_attrs[] = {
788 &vma_ra_enabled_attr.attr,
Huang Yingd9bfcfd2017-09-06 16:24:40 -0700789 NULL,
790};
791
792static struct attribute_group swap_attr_group = {
793 .attrs = swap_attrs,
794};
795
796static int __init swap_init_sysfs(void)
797{
798 int err;
799 struct kobject *swap_kobj;
800
801 swap_kobj = kobject_create_and_add("swap", mm_kobj);
802 if (!swap_kobj) {
803 pr_err("failed to create swap kobject\n");
804 return -ENOMEM;
805 }
806 err = sysfs_create_group(swap_kobj, &swap_attr_group);
807 if (err) {
808 pr_err("failed to register swap group\n");
809 goto delete_obj;
810 }
811 return 0;
812
813delete_obj:
814 kobject_put(swap_kobj);
815 return err;
816}
817subsys_initcall(swap_init_sysfs);
818#endif