]> git.ipfire.org Git - thirdparty/linux.git/blame - mm/swap_state.c
mm: move ptep_get() and pmdp_get() helpers
[thirdparty/linux.git] / mm / swap_state.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
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 */
1da177e4 10#include <linux/mm.h>
5a0e3ad6 11#include <linux/gfp.h>
1da177e4
LT
12#include <linux/kernel_stat.h>
13#include <linux/swap.h>
46017e95 14#include <linux/swapops.h>
1da177e4
LT
15#include <linux/init.h>
16#include <linux/pagemap.h>
1da177e4 17#include <linux/backing-dev.h>
3fb5c298 18#include <linux/blkdev.h>
c484d410 19#include <linux/pagevec.h>
b20a3503 20#include <linux/migrate.h>
4b3ef9da 21#include <linux/vmalloc.h>
67afa38e 22#include <linux/swap_slots.h>
38d8b4e6 23#include <linux/huge_mm.h>
61ef1865 24#include <linux/shmem_fs.h>
243bce09 25#include "internal.h"
014bb1de 26#include "swap.h"
1da177e4
LT
27
28/*
29 * swapper_space is a fiction, retained to simplify the path through
7eaceacc 30 * vmscan's shrink_page_list.
1da177e4 31 */
f5e54d6e 32static const struct address_space_operations swap_aops = {
1da177e4 33 .writepage = swap_writepage,
4c4a7634 34 .dirty_folio = noop_dirty_folio,
1c93923c 35#ifdef CONFIG_MIGRATION
54184650 36 .migrate_folio = migrate_folio,
1c93923c 37#endif
1da177e4
LT
38};
39
783cb68e
CD
40struct address_space *swapper_spaces[MAX_SWAPFILES] __read_mostly;
41static unsigned int nr_swapper_spaces[MAX_SWAPFILES] __read_mostly;
f5c754d6 42static bool enable_vma_readahead __read_mostly = true;
ec560175 43
ec560175
HY
44#define SWAP_RA_WIN_SHIFT (PAGE_SHIFT / 2)
45#define SWAP_RA_HITS_MASK ((1UL << SWAP_RA_WIN_SHIFT) - 1)
46#define SWAP_RA_HITS_MAX SWAP_RA_HITS_MASK
47#define SWAP_RA_WIN_MASK (~PAGE_MASK & ~SWAP_RA_HITS_MASK)
48
49#define SWAP_RA_HITS(v) ((v) & SWAP_RA_HITS_MASK)
50#define SWAP_RA_WIN(v) (((v) & SWAP_RA_WIN_MASK) >> SWAP_RA_WIN_SHIFT)
51#define SWAP_RA_ADDR(v) ((v) & PAGE_MASK)
52
53#define SWAP_RA_VAL(addr, win, hits) \
54 (((addr) & PAGE_MASK) | \
55 (((win) << SWAP_RA_WIN_SHIFT) & SWAP_RA_WIN_MASK) | \
56 ((hits) & SWAP_RA_HITS_MASK))
57
58/* Initial readahead hits is 4 to start up with a small window */
59#define GET_SWAP_RA_VAL(vma) \
60 (atomic_long_read(&(vma)->swap_readahead_info) ? : 4)
1da177e4 61
579f8290
SL
62static atomic_t swapin_readahead_hits = ATOMIC_INIT(4);
63
1da177e4
LT
64void show_swap_cache_info(void)
65{
33806f06 66 printk("%lu pages in swap cache\n", total_swapcache_pages());
ec8acf20
SL
67 printk("Free swap = %ldkB\n",
68 get_nr_swap_pages() << (PAGE_SHIFT - 10));
1da177e4
LT
69 printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10));
70}
71
aae466b0
JK
72void *get_shadow_from_swap_cache(swp_entry_t entry)
73{
74 struct address_space *address_space = swap_address_space(entry);
75 pgoff_t idx = swp_offset(entry);
76 struct page *page;
77
8c647dd1 78 page = xa_load(&address_space->i_pages, idx);
aae466b0
JK
79 if (xa_is_value(page))
80 return page;
aae466b0
JK
81 return NULL;
82}
83
1da177e4 84/*
2bb876b5 85 * add_to_swap_cache resembles filemap_add_folio on swapper_space,
1da177e4
LT
86 * but sets SwapCache flag and private instead of mapping and index.
87 */
a4c366f0 88int add_to_swap_cache(struct folio *folio, swp_entry_t entry,
3852f676 89 gfp_t gfp, void **shadowp)
1da177e4 90{
8d93b41c 91 struct address_space *address_space = swap_address_space(entry);
38d8b4e6 92 pgoff_t idx = swp_offset(entry);
a4c366f0
MWO
93 XA_STATE_ORDER(xas, &address_space->i_pages, idx, folio_order(folio));
94 unsigned long i, nr = folio_nr_pages(folio);
3852f676 95 void *old;
1da177e4 96
5649d113
YY
97 xas_set_update(&xas, workingset_update_node);
98
a4c366f0
MWO
99 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
100 VM_BUG_ON_FOLIO(folio_test_swapcache(folio), folio);
101 VM_BUG_ON_FOLIO(!folio_test_swapbacked(folio), folio);
51726b12 102
a4c366f0
MWO
103 folio_ref_add(folio, nr);
104 folio_set_swapcache(folio);
31a56396 105
8d93b41c
MW
106 do {
107 xas_lock_irq(&xas);
108 xas_create_range(&xas);
109 if (xas_error(&xas))
110 goto unlock;
111 for (i = 0; i < nr; i++) {
a4c366f0 112 VM_BUG_ON_FOLIO(xas.xa_index != idx + i, folio);
3852f676
JK
113 old = xas_load(&xas);
114 if (xa_is_value(old)) {
3852f676
JK
115 if (shadowp)
116 *shadowp = old;
117 }
a4c366f0
MWO
118 set_page_private(folio_page(folio, i), entry.val + i);
119 xas_store(&xas, folio);
8d93b41c
MW
120 xas_next(&xas);
121 }
38d8b4e6 122 address_space->nrpages += nr;
a4c366f0
MWO
123 __node_stat_mod_folio(folio, NR_FILE_PAGES, nr);
124 __lruvec_stat_mod_folio(folio, NR_SWAPCACHE, nr);
8d93b41c
MW
125unlock:
126 xas_unlock_irq(&xas);
127 } while (xas_nomem(&xas, gfp));
31a56396 128
8d93b41c
MW
129 if (!xas_error(&xas))
130 return 0;
31a56396 131
a4c366f0
MWO
132 folio_clear_swapcache(folio);
133 folio_ref_sub(folio, nr);
8d93b41c 134 return xas_error(&xas);
1da177e4
LT
135}
136
1da177e4 137/*
ceff9d33 138 * This must be called only on folios that have
1da177e4
LT
139 * been verified to be in the swap cache.
140 */
ceff9d33 141void __delete_from_swap_cache(struct folio *folio,
3852f676 142 swp_entry_t entry, void *shadow)
1da177e4 143{
4e17ec25 144 struct address_space *address_space = swap_address_space(entry);
ceff9d33
MWO
145 int i;
146 long nr = folio_nr_pages(folio);
4e17ec25
MW
147 pgoff_t idx = swp_offset(entry);
148 XA_STATE(xas, &address_space->i_pages, idx);
33806f06 149
5649d113
YY
150 xas_set_update(&xas, workingset_update_node);
151
ceff9d33
MWO
152 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
153 VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
154 VM_BUG_ON_FOLIO(folio_test_writeback(folio), folio);
1da177e4 155
38d8b4e6 156 for (i = 0; i < nr; i++) {
3852f676 157 void *entry = xas_store(&xas, shadow);
b9eb7776 158 VM_BUG_ON_PAGE(entry != folio, entry);
ceff9d33 159 set_page_private(folio_page(folio, i), 0);
4e17ec25 160 xas_next(&xas);
38d8b4e6 161 }
ceff9d33 162 folio_clear_swapcache(folio);
38d8b4e6 163 address_space->nrpages -= nr;
ceff9d33
MWO
164 __node_stat_mod_folio(folio, NR_FILE_PAGES, -nr);
165 __lruvec_stat_mod_folio(folio, NR_SWAPCACHE, -nr);
1da177e4
LT
166}
167
168/**
09c02e56
MWO
169 * add_to_swap - allocate swap space for a folio
170 * @folio: folio we want to move to swap
1da177e4 171 *
09c02e56
MWO
172 * Allocate swap space for the folio and add the folio to the
173 * swap cache.
174 *
175 * Context: Caller needs to hold the folio lock.
176 * Return: Whether the folio was added to the swap cache.
1da177e4 177 */
09c02e56 178bool add_to_swap(struct folio *folio)
1da177e4
LT
179{
180 swp_entry_t entry;
1da177e4
LT
181 int err;
182
09c02e56
MWO
183 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
184 VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio);
1da177e4 185
e2e3fdc7 186 entry = folio_alloc_swap(folio);
2ca4532a 187 if (!entry.val)
09c02e56 188 return false;
0f074658 189
2ca4532a 190 /*
8d93b41c 191 * XArray node allocations from PF_MEMALLOC contexts could
2ca4532a
DN
192 * completely exhaust the page allocator. __GFP_NOMEMALLOC
193 * stops emergency reserves from being allocated.
194 *
195 * TODO: this could cause a theoretical memory reclaim
196 * deadlock in the swap out path.
197 */
198 /*
854e9ed0 199 * Add it to the swap cache.
2ca4532a 200 */
a4c366f0 201 err = add_to_swap_cache(folio, entry,
3852f676 202 __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN, NULL);
38d8b4e6 203 if (err)
bd53b714 204 /*
2ca4532a
DN
205 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
206 * clear SWAP_HAS_CACHE flag.
1da177e4 207 */
0f074658 208 goto fail;
9625456c 209 /*
09c02e56
MWO
210 * Normally the folio will be dirtied in unmap because its
211 * pte should be dirty. A special case is MADV_FREE page. The
212 * page's pte could have dirty bit cleared but the folio's
213 * SwapBacked flag is still set because clearing the dirty bit
214 * and SwapBacked flag has no lock protected. For such folio,
215 * unmap will not set dirty bit for it, so folio reclaim will
216 * not write the folio out. This can cause data corruption when
217 * the folio is swapped in later. Always setting the dirty flag
218 * for the folio solves the problem.
9625456c 219 */
09c02e56 220 folio_mark_dirty(folio);
38d8b4e6 221
09c02e56 222 return true;
38d8b4e6 223
38d8b4e6 224fail:
4081f744 225 put_swap_folio(folio, entry);
09c02e56 226 return false;
1da177e4
LT
227}
228
229/*
75fa68a5 230 * This must be called only on folios that have
1da177e4 231 * been verified to be in the swap cache and locked.
75fa68a5
MWO
232 * It will never put the folio into the free list,
233 * the caller has a reference on the folio.
1da177e4 234 */
75fa68a5 235void delete_from_swap_cache(struct folio *folio)
1da177e4 236{
75fa68a5 237 swp_entry_t entry = folio_swap_entry(folio);
4e17ec25 238 struct address_space *address_space = swap_address_space(entry);
1da177e4 239
b93b0163 240 xa_lock_irq(&address_space->i_pages);
ceff9d33 241 __delete_from_swap_cache(folio, entry, NULL);
b93b0163 242 xa_unlock_irq(&address_space->i_pages);
1da177e4 243
4081f744 244 put_swap_folio(folio, entry);
75fa68a5 245 folio_ref_sub(folio, folio_nr_pages(folio));
1da177e4
LT
246}
247
3852f676
JK
248void clear_shadow_from_swap_cache(int type, unsigned long begin,
249 unsigned long end)
250{
251 unsigned long curr = begin;
252 void *old;
253
254 for (;;) {
3852f676
JK
255 swp_entry_t entry = swp_entry(type, curr);
256 struct address_space *address_space = swap_address_space(entry);
257 XA_STATE(xas, &address_space->i_pages, curr);
258
5649d113
YY
259 xas_set_update(&xas, workingset_update_node);
260
3852f676
JK
261 xa_lock_irq(&address_space->i_pages);
262 xas_for_each(&xas, old, end) {
263 if (!xa_is_value(old))
264 continue;
265 xas_store(&xas, NULL);
3852f676 266 }
3852f676
JK
267 xa_unlock_irq(&address_space->i_pages);
268
269 /* search the next swapcache until we meet end */
270 curr >>= SWAP_ADDRESS_SPACE_SHIFT;
271 curr++;
272 curr <<= SWAP_ADDRESS_SPACE_SHIFT;
273 if (curr > end)
274 break;
275 }
276}
277
1da177e4
LT
278/*
279 * If we are the only user, then try to free up the swap cache.
280 *
aedd74d4 281 * Its ok to check the swapcache flag without the folio lock
a2c43eed 282 * here because we are going to recheck again inside
aedd74d4 283 * folio_free_swap() _with_ the lock.
1da177e4
LT
284 * - Marcelo
285 */
f4c4a3f4 286void free_swap_cache(struct page *page)
1da177e4 287{
aedd74d4
MWO
288 struct folio *folio = page_folio(page);
289
290 if (folio_test_swapcache(folio) && !folio_mapped(folio) &&
291 folio_trylock(folio)) {
292 folio_free_swap(folio);
293 folio_unlock(folio);
1da177e4
LT
294 }
295}
296
297/*
298 * Perform a free_page(), also freeing any swap cache associated with
b8072f09 299 * this page if it is the last user of the page.
1da177e4
LT
300 */
301void free_page_and_swap_cache(struct page *page)
302{
303 free_swap_cache(page);
6fcb52a5 304 if (!is_huge_zero_page(page))
770a5370 305 put_page(page);
1da177e4
LT
306}
307
308/*
309 * Passed an array of pages, drop them all from swapcache and then release
310 * them. They are removed from the LRU and freed if this is their last use.
311 */
7cc8f9c7 312void free_pages_and_swap_cache(struct encoded_page **pages, int nr)
1da177e4 313{
1da177e4 314 lru_add_drain();
7cc8f9c7
LT
315 for (int i = 0; i < nr; i++)
316 free_swap_cache(encoded_page_ptr(pages[i]));
317 release_pages(pages, nr);
1da177e4
LT
318}
319
e9e9b7ec
MK
320static inline bool swap_use_vma_readahead(void)
321{
322 return READ_ONCE(enable_vma_readahead) && !atomic_read(&nr_rotate_swap);
323}
324
1da177e4 325/*
c9edc242 326 * Lookup a swap entry in the swap cache. A found folio will be returned
1da177e4 327 * unlocked and with its refcount incremented - we rely on the kernel
c9edc242 328 * lock getting page table operations atomic even if we drop the folio
1da177e4 329 * lock before returning.
cbc2bd98
KS
330 *
331 * Caller must lock the swap device or hold a reference to keep it valid.
1da177e4 332 */
c9edc242
MWO
333struct folio *swap_cache_get_folio(swp_entry_t entry,
334 struct vm_area_struct *vma, unsigned long addr)
1da177e4 335{
c9edc242 336 struct folio *folio;
1da177e4 337
c9edc242 338 folio = filemap_get_folio(swap_address_space(entry), swp_offset(entry));
66dabbb6 339 if (!IS_ERR(folio)) {
eaf649eb
MK
340 bool vma_ra = swap_use_vma_readahead();
341 bool readahead;
342
eaf649eb
MK
343 /*
344 * At the moment, we don't support PG_readahead for anon THP
345 * so let's bail out rather than confusing the readahead stat.
346 */
c9edc242
MWO
347 if (unlikely(folio_test_large(folio)))
348 return folio;
eaf649eb 349
c9edc242 350 readahead = folio_test_clear_readahead(folio);
eaf649eb
MK
351 if (vma && vma_ra) {
352 unsigned long ra_val;
353 int win, hits;
354
355 ra_val = GET_SWAP_RA_VAL(vma);
356 win = SWAP_RA_WIN(ra_val);
357 hits = SWAP_RA_HITS(ra_val);
ec560175
HY
358 if (readahead)
359 hits = min_t(int, hits + 1, SWAP_RA_HITS_MAX);
360 atomic_long_set(&vma->swap_readahead_info,
361 SWAP_RA_VAL(addr, win, hits));
362 }
eaf649eb 363
ec560175 364 if (readahead) {
cbc65df2 365 count_vm_event(SWAP_RA_HIT);
eaf649eb 366 if (!vma || !vma_ra)
ec560175 367 atomic_inc(&swapin_readahead_hits);
cbc65df2 368 }
66dabbb6
CH
369 } else {
370 folio = NULL;
579f8290 371 }
eaf649eb 372
c9edc242
MWO
373 return folio;
374}
375
61ef1865 376/**
524984ff 377 * filemap_get_incore_folio - Find and get a folio from the page or swap caches.
61ef1865
MWO
378 * @mapping: The address_space to search.
379 * @index: The page cache index.
380 *
524984ff
MWO
381 * This differs from filemap_get_folio() in that it will also look for the
382 * folio in the swap cache.
61ef1865 383 *
524984ff 384 * Return: The found folio or %NULL.
61ef1865 385 */
524984ff
MWO
386struct folio *filemap_get_incore_folio(struct address_space *mapping,
387 pgoff_t index)
61ef1865
MWO
388{
389 swp_entry_t swp;
390 struct swap_info_struct *si;
097b3e59 391 struct folio *folio = filemap_get_entry(mapping, index);
61ef1865 392
66dabbb6
CH
393 if (!folio)
394 return ERR_PTR(-ENOENT);
dd8095b1 395 if (!xa_is_value(folio))
66dabbb6 396 return folio;
61ef1865 397 if (!shmem_mapping(mapping))
66dabbb6 398 return ERR_PTR(-ENOENT);
61ef1865 399
dd8095b1 400 swp = radix_to_swp_entry(folio);
ba6851b4
ML
401 /* There might be swapin error entries in shmem mapping. */
402 if (non_swap_entry(swp))
66dabbb6 403 return ERR_PTR(-ENOENT);
61ef1865
MWO
404 /* Prevent swapoff from happening to us */
405 si = get_swap_device(swp);
406 if (!si)
66dabbb6 407 return ERR_PTR(-ENOENT);
dd8095b1
MWO
408 index = swp_offset(swp);
409 folio = filemap_get_folio(swap_address_space(swp), index);
61ef1865 410 put_swap_device(si);
524984ff 411 return folio;
61ef1865
MWO
412}
413
5b999aad
DS
414struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
415 struct vm_area_struct *vma, unsigned long addr,
416 bool *new_page_allocated)
1da177e4 417{
eb085574 418 struct swap_info_struct *si;
a0d3374b 419 struct folio *folio;
46a774d3 420 struct page *page;
aae466b0 421 void *shadow = NULL;
4c6355b2 422
5b999aad 423 *new_page_allocated = false;
46a774d3
HY
424 si = get_swap_device(entry);
425 if (!si)
426 return NULL;
1da177e4 427
4c6355b2
JW
428 for (;;) {
429 int err;
1da177e4
LT
430 /*
431 * First check the swap cache. Since this is normally
cb691e2f 432 * called after swap_cache_get_folio() failed, re-calling
1da177e4
LT
433 * that would confuse statistics.
434 */
a0d3374b
MWO
435 folio = filemap_get_folio(swap_address_space(entry),
436 swp_offset(entry));
46a774d3
HY
437 if (!IS_ERR(folio)) {
438 page = folio_file_page(folio, swp_offset(entry));
439 goto got_page;
440 }
1da177e4 441
ba81f838
HY
442 /*
443 * Just skip read ahead for unused swap slot.
444 * During swap_off when swap_slot_cache is disabled,
445 * we have to handle the race between putting
446 * swap entry in swap cache and marking swap slot
447 * as SWAP_HAS_CACHE. That's done in later part of code or
448 * else swap_off will be aborted if we return NULL.
449 */
3ecdeb0f 450 if (!swap_swapcount(si, entry) && swap_slot_cache_enabled)
46a774d3 451 goto fail_put_swap;
e8c26ab6 452
1da177e4 453 /*
4c6355b2
JW
454 * Get a new page to read into from swap. Allocate it now,
455 * before marking swap_map SWAP_HAS_CACHE, when -EEXIST will
456 * cause any racers to loop around until we add it to cache.
1da177e4 457 */
a0d3374b
MWO
458 folio = vma_alloc_folio(gfp_mask, 0, vma, addr, false);
459 if (!folio)
46a774d3 460 goto fail_put_swap;
1da177e4 461
f000944d
HD
462 /*
463 * Swap entry may have been freed since our caller observed it.
464 */
355cfa73 465 err = swapcache_prepare(entry);
4c6355b2 466 if (!err)
f000944d
HD
467 break;
468
a0d3374b 469 folio_put(folio);
4c6355b2 470 if (err != -EEXIST)
46a774d3 471 goto fail_put_swap;
4c6355b2 472
2ca4532a 473 /*
4c6355b2
JW
474 * We might race against __delete_from_swap_cache(), and
475 * stumble across a swap_map entry whose SWAP_HAS_CACHE
476 * has not yet been cleared. Or race against another
477 * __read_swap_cache_async(), which has set SWAP_HAS_CACHE
478 * in swap_map, but not yet added its page to swap cache.
2ca4532a 479 */
029c4628 480 schedule_timeout_uninterruptible(1);
4c6355b2
JW
481 }
482
483 /*
484 * The swap entry is ours to swap in. Prepare the new page.
485 */
486
a0d3374b
MWO
487 __folio_set_locked(folio);
488 __folio_set_swapbacked(folio);
4c6355b2 489
65995918 490 if (mem_cgroup_swapin_charge_folio(folio, NULL, gfp_mask, entry))
4c6355b2 491 goto fail_unlock;
4c6355b2 492
0add0c77 493 /* May fail (-ENOMEM) if XArray node allocation failed. */
a4c366f0 494 if (add_to_swap_cache(folio, entry, gfp_mask & GFP_RECLAIM_MASK, &shadow))
4c6355b2 495 goto fail_unlock;
0add0c77
SB
496
497 mem_cgroup_swapin_uncharge_swap(entry);
4c6355b2 498
aae466b0 499 if (shadow)
a0d3374b 500 workingset_refault(folio, shadow);
314b57fb 501
a0d3374b
MWO
502 /* Caller will initiate read into locked folio */
503 folio_add_lru(folio);
4c6355b2 504 *new_page_allocated = true;
46a774d3
HY
505 page = &folio->page;
506got_page:
507 put_swap_device(si);
508 return page;
1da177e4 509
4c6355b2 510fail_unlock:
4081f744 511 put_swap_folio(folio, entry);
a0d3374b
MWO
512 folio_unlock(folio);
513 folio_put(folio);
46a774d3
HY
514fail_put_swap:
515 put_swap_device(si);
4c6355b2 516 return NULL;
1da177e4 517}
46017e95 518
5b999aad
DS
519/*
520 * Locate a page of swap in physical memory, reserving swap cache space
521 * and reading the disk if it is not already cached.
522 * A failure return means that either the page allocation failed or that
523 * the swap entry is no longer in use.
46a774d3
HY
524 *
525 * get/put_swap_device() aren't needed to call this function, because
526 * __read_swap_cache_async() call them and swap_readpage() holds the
527 * swap cache folio lock.
5b999aad
DS
528 */
529struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
5169b844
N
530 struct vm_area_struct *vma,
531 unsigned long addr, bool do_poll,
532 struct swap_iocb **plug)
5b999aad
DS
533{
534 bool page_was_allocated;
535 struct page *retpage = __read_swap_cache_async(entry, gfp_mask,
536 vma, addr, &page_was_allocated);
537
538 if (page_was_allocated)
5169b844 539 swap_readpage(retpage, do_poll, plug);
5b999aad
DS
540
541 return retpage;
542}
543
ec560175
HY
544static unsigned int __swapin_nr_pages(unsigned long prev_offset,
545 unsigned long offset,
546 int hits,
547 int max_pages,
548 int prev_win)
579f8290 549{
ec560175 550 unsigned int pages, last_ra;
579f8290
SL
551
552 /*
553 * This heuristic has been found to work well on both sequential and
554 * random loads, swapping to hard disk or to SSD: please don't ask
555 * what the "+ 2" means, it just happens to work well, that's all.
556 */
ec560175 557 pages = hits + 2;
579f8290
SL
558 if (pages == 2) {
559 /*
560 * We can have no readahead hits to judge by: but must not get
561 * stuck here forever, so check for an adjacent offset instead
562 * (and don't even bother to check whether swap type is same).
563 */
564 if (offset != prev_offset + 1 && offset != prev_offset - 1)
565 pages = 1;
579f8290
SL
566 } else {
567 unsigned int roundup = 4;
568 while (roundup < pages)
569 roundup <<= 1;
570 pages = roundup;
571 }
572
573 if (pages > max_pages)
574 pages = max_pages;
575
576 /* Don't shrink readahead too fast */
ec560175 577 last_ra = prev_win / 2;
579f8290
SL
578 if (pages < last_ra)
579 pages = last_ra;
ec560175
HY
580
581 return pages;
582}
583
584static unsigned long swapin_nr_pages(unsigned long offset)
585{
586 static unsigned long prev_offset;
587 unsigned int hits, pages, max_pages;
588 static atomic_t last_readahead_pages;
589
590 max_pages = 1 << READ_ONCE(page_cluster);
591 if (max_pages <= 1)
592 return 1;
593
594 hits = atomic_xchg(&swapin_readahead_hits, 0);
d6c1f098
QC
595 pages = __swapin_nr_pages(READ_ONCE(prev_offset), offset, hits,
596 max_pages,
ec560175
HY
597 atomic_read(&last_readahead_pages));
598 if (!hits)
d6c1f098 599 WRITE_ONCE(prev_offset, offset);
579f8290
SL
600 atomic_set(&last_readahead_pages, pages);
601
602 return pages;
603}
604
46017e95 605/**
e9e9b7ec 606 * swap_cluster_readahead - swap in pages in hope we need them soon
46017e95 607 * @entry: swap entry of this memory
7682486b 608 * @gfp_mask: memory allocation flags
e9e9b7ec 609 * @vmf: fault information
46017e95
HD
610 *
611 * Returns the struct page for entry and addr, after queueing swapin.
612 *
613 * Primitive swap readahead code. We simply read an aligned block of
614 * (1 << page_cluster) entries in the swap area. This method is chosen
615 * because it doesn't cost us any seek time. We also make sure to queue
616 * the 'original' request together with the readahead ones...
617 *
618 * This has been extended to use the NUMA policies from the mm triggering
619 * the readahead.
620 *
c1e8d7c6 621 * Caller must hold read mmap_lock if vmf->vma is not NULL.
46017e95 622 */
e9e9b7ec
MK
623struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
624 struct vm_fault *vmf)
46017e95 625{
46017e95 626 struct page *page;
579f8290
SL
627 unsigned long entry_offset = swp_offset(entry);
628 unsigned long offset = entry_offset;
67f96aa2 629 unsigned long start_offset, end_offset;
579f8290 630 unsigned long mask;
e9a6effa 631 struct swap_info_struct *si = swp_swap_info(entry);
3fb5c298 632 struct blk_plug plug;
5169b844 633 struct swap_iocb *splug = NULL;
c4fa6309 634 bool do_poll = true, page_allocated;
e9e9b7ec
MK
635 struct vm_area_struct *vma = vmf->vma;
636 unsigned long addr = vmf->address;
46017e95 637
579f8290
SL
638 mask = swapin_nr_pages(offset) - 1;
639 if (!mask)
640 goto skip;
641
23955622 642 do_poll = false;
67f96aa2
RR
643 /* Read a page_cluster sized and aligned cluster around offset. */
644 start_offset = offset & ~mask;
645 end_offset = offset | mask;
646 if (!start_offset) /* First page is swap header. */
647 start_offset++;
e9a6effa
HY
648 if (end_offset >= si->max)
649 end_offset = si->max - 1;
67f96aa2 650
3fb5c298 651 blk_start_plug(&plug);
67f96aa2 652 for (offset = start_offset; offset <= end_offset ; offset++) {
46017e95 653 /* Ok, do the async read-ahead now */
c4fa6309
HY
654 page = __read_swap_cache_async(
655 swp_entry(swp_type(entry), offset),
656 gfp_mask, vma, addr, &page_allocated);
46017e95 657 if (!page)
67f96aa2 658 continue;
c4fa6309 659 if (page_allocated) {
5169b844 660 swap_readpage(page, false, &splug);
eaf649eb 661 if (offset != entry_offset) {
c4fa6309
HY
662 SetPageReadahead(page);
663 count_vm_event(SWAP_RA);
664 }
cbc65df2 665 }
09cbfeaf 666 put_page(page);
46017e95 667 }
3fb5c298 668 blk_finish_plug(&plug);
5169b844 669 swap_read_unplug(splug);
3fb5c298 670
46017e95 671 lru_add_drain(); /* Push any new pages onto the LRU now */
579f8290 672skip:
5169b844
N
673 /* The page was likely read above, so no need for plugging here */
674 return read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll, NULL);
46017e95 675}
4b3ef9da
HY
676
677int init_swap_address_space(unsigned int type, unsigned long nr_pages)
678{
679 struct address_space *spaces, *space;
680 unsigned int i, nr;
681
682 nr = DIV_ROUND_UP(nr_pages, SWAP_ADDRESS_SPACE_PAGES);
778e1cdd 683 spaces = kvcalloc(nr, sizeof(struct address_space), GFP_KERNEL);
4b3ef9da
HY
684 if (!spaces)
685 return -ENOMEM;
686 for (i = 0; i < nr; i++) {
687 space = spaces + i;
a2833486 688 xa_init_flags(&space->i_pages, XA_FLAGS_LOCK_IRQ);
4b3ef9da
HY
689 atomic_set(&space->i_mmap_writable, 0);
690 space->a_ops = &swap_aops;
691 /* swap cache doesn't use writeback related tags */
692 mapping_set_no_writeback_tags(space);
4b3ef9da
HY
693 }
694 nr_swapper_spaces[type] = nr;
054f1d1f 695 swapper_spaces[type] = spaces;
4b3ef9da
HY
696
697 return 0;
698}
699
700void exit_swap_address_space(unsigned int type)
701{
eea4a501
HY
702 int i;
703 struct address_space *spaces = swapper_spaces[type];
704
705 for (i = 0; i < nr_swapper_spaces[type]; i++)
706 VM_WARN_ON_ONCE(!mapping_empty(&spaces[i]));
707 kvfree(spaces);
4b3ef9da 708 nr_swapper_spaces[type] = 0;
054f1d1f 709 swapper_spaces[type] = NULL;
4b3ef9da 710}
ec560175 711
4f8fcf4c
HD
712#define SWAP_RA_ORDER_CEILING 5
713
714struct vma_swap_readahead {
715 unsigned short win;
716 unsigned short offset;
717 unsigned short nr_pte;
718};
719
eaf649eb 720static void swap_ra_info(struct vm_fault *vmf,
16ba391e 721 struct vma_swap_readahead *ra_info)
ec560175
HY
722{
723 struct vm_area_struct *vma = vmf->vma;
eaf649eb 724 unsigned long ra_val;
16ba391e 725 unsigned long faddr, pfn, fpfn, lpfn, rpfn;
ec560175 726 unsigned long start, end;
16ba391e 727 unsigned int max_win, hits, prev_win, win;
ec560175 728
61b63972
HY
729 max_win = 1 << min_t(unsigned int, READ_ONCE(page_cluster),
730 SWAP_RA_ORDER_CEILING);
731 if (max_win == 1) {
eaf649eb
MK
732 ra_info->win = 1;
733 return;
61b63972
HY
734 }
735
ec560175 736 faddr = vmf->address;
ec560175 737 fpfn = PFN_DOWN(faddr);
eaf649eb
MK
738 ra_val = GET_SWAP_RA_VAL(vma);
739 pfn = PFN_DOWN(SWAP_RA_ADDR(ra_val));
740 prev_win = SWAP_RA_WIN(ra_val);
741 hits = SWAP_RA_HITS(ra_val);
742 ra_info->win = win = __swapin_nr_pages(pfn, fpfn, hits,
ec560175
HY
743 max_win, prev_win);
744 atomic_long_set(&vma->swap_readahead_info,
745 SWAP_RA_VAL(faddr, win, 0));
18ad72f5 746 if (win == 1)
eaf649eb 747 return;
ec560175 748
16ba391e
KS
749 if (fpfn == pfn + 1) {
750 lpfn = fpfn;
751 rpfn = fpfn + win;
752 } else if (pfn == fpfn + 1) {
753 lpfn = fpfn - win + 1;
754 rpfn = fpfn + 1;
755 } else {
756 unsigned int left = (win - 1) / 2;
757
758 lpfn = fpfn - left;
759 rpfn = fpfn + win - left;
ec560175 760 }
16ba391e
KS
761 start = max3(lpfn, PFN_DOWN(vma->vm_start),
762 PFN_DOWN(faddr & PMD_MASK));
763 end = min3(rpfn, PFN_DOWN(vma->vm_end),
764 PFN_DOWN((faddr & PMD_MASK) + PMD_SIZE));
765
eaf649eb
MK
766 ra_info->nr_pte = end - start;
767 ra_info->offset = fpfn - start;
ec560175
HY
768}
769
e9f59873
YS
770/**
771 * swap_vma_readahead - swap in pages in hope we need them soon
27ec4878 772 * @fentry: swap entry of this memory
e9f59873
YS
773 * @gfp_mask: memory allocation flags
774 * @vmf: fault information
775 *
776 * Returns the struct page for entry and addr, after queueing swapin.
777 *
cb152a1a 778 * Primitive swap readahead code. We simply read in a few pages whose
e9f59873
YS
779 * virtual addresses are around the fault address in the same vma.
780 *
c1e8d7c6 781 * Caller must hold read mmap_lock if vmf->vma is not NULL.
e9f59873
YS
782 *
783 */
f5c754d6
CIK
784static struct page *swap_vma_readahead(swp_entry_t fentry, gfp_t gfp_mask,
785 struct vm_fault *vmf)
ec560175
HY
786{
787 struct blk_plug plug;
5169b844 788 struct swap_iocb *splug = NULL;
ec560175
HY
789 struct vm_area_struct *vma = vmf->vma;
790 struct page *page;
4f8fcf4c
HD
791 pte_t *pte = NULL, pentry;
792 unsigned long addr;
ec560175
HY
793 swp_entry_t entry;
794 unsigned int i;
795 bool page_allocated;
e97af699
ML
796 struct vma_swap_readahead ra_info = {
797 .win = 1,
798 };
ec560175 799
eaf649eb
MK
800 swap_ra_info(vmf, &ra_info);
801 if (ra_info.win == 1)
ec560175
HY
802 goto skip;
803
4f8fcf4c
HD
804 addr = vmf->address - (ra_info.offset * PAGE_SIZE);
805
ec560175 806 blk_start_plug(&plug);
4f8fcf4c
HD
807 for (i = 0; i < ra_info.nr_pte; i++, addr += PAGE_SIZE) {
808 if (!pte++) {
809 pte = pte_offset_map(vmf->pmd, addr);
810 if (!pte)
811 break;
812 }
813 pentry = ptep_get_lockless(pte);
92bafb20 814 if (!is_swap_pte(pentry))
ec560175
HY
815 continue;
816 entry = pte_to_swp_entry(pentry);
817 if (unlikely(non_swap_entry(entry)))
818 continue;
4f8fcf4c
HD
819 pte_unmap(pte);
820 pte = NULL;
ec560175 821 page = __read_swap_cache_async(entry, gfp_mask, vma,
4f8fcf4c 822 addr, &page_allocated);
ec560175
HY
823 if (!page)
824 continue;
825 if (page_allocated) {
5169b844 826 swap_readpage(page, false, &splug);
eaf649eb 827 if (i != ra_info.offset) {
ec560175
HY
828 SetPageReadahead(page);
829 count_vm_event(SWAP_RA);
830 }
831 }
832 put_page(page);
833 }
4f8fcf4c
HD
834 if (pte)
835 pte_unmap(pte);
ec560175 836 blk_finish_plug(&plug);
5169b844 837 swap_read_unplug(splug);
ec560175
HY
838 lru_add_drain();
839skip:
5169b844 840 /* The page was likely read above, so no need for plugging here */
ec560175 841 return read_swap_cache_async(fentry, gfp_mask, vma, vmf->address,
5169b844 842 ra_info.win == 1, NULL);
ec560175 843}
d9bfcfdc 844
e9e9b7ec
MK
845/**
846 * swapin_readahead - swap in pages in hope we need them soon
847 * @entry: swap entry of this memory
848 * @gfp_mask: memory allocation flags
849 * @vmf: fault information
850 *
851 * Returns the struct page for entry and addr, after queueing swapin.
852 *
853 * It's a main entry function for swap readahead. By the configuration,
854 * it will read ahead blocks by cluster-based(ie, physical disk based)
855 * or vma-based(ie, virtual address based on faulty address) readahead.
856 */
857struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
858 struct vm_fault *vmf)
859{
860 return swap_use_vma_readahead() ?
861 swap_vma_readahead(entry, gfp_mask, vmf) :
862 swap_cluster_readahead(entry, gfp_mask, vmf);
863}
864
d9bfcfdc
HY
865#ifdef CONFIG_SYSFS
866static ssize_t vma_ra_enabled_show(struct kobject *kobj,
867 struct kobj_attribute *attr, char *buf)
868{
ae7a927d
JP
869 return sysfs_emit(buf, "%s\n",
870 enable_vma_readahead ? "true" : "false");
d9bfcfdc
HY
871}
872static ssize_t vma_ra_enabled_store(struct kobject *kobj,
873 struct kobj_attribute *attr,
874 const char *buf, size_t count)
875{
717aeab4
JG
876 ssize_t ret;
877
878 ret = kstrtobool(buf, &enable_vma_readahead);
879 if (ret)
880 return ret;
d9bfcfdc
HY
881
882 return count;
883}
6106b93e 884static struct kobj_attribute vma_ra_enabled_attr = __ATTR_RW(vma_ra_enabled);
d9bfcfdc 885
d9bfcfdc
HY
886static struct attribute *swap_attrs[] = {
887 &vma_ra_enabled_attr.attr,
d9bfcfdc
HY
888 NULL,
889};
890
e48333b6 891static const struct attribute_group swap_attr_group = {
d9bfcfdc
HY
892 .attrs = swap_attrs,
893};
894
895static int __init swap_init_sysfs(void)
896{
897 int err;
898 struct kobject *swap_kobj;
899
900 swap_kobj = kobject_create_and_add("swap", mm_kobj);
901 if (!swap_kobj) {
902 pr_err("failed to create swap kobject\n");
903 return -ENOMEM;
904 }
905 err = sysfs_create_group(swap_kobj, &swap_attr_group);
906 if (err) {
907 pr_err("failed to register swap group\n");
908 goto delete_obj;
909 }
910 return 0;
911
912delete_obj:
913 kobject_put(swap_kobj);
914 return err;
915}
916subsys_initcall(swap_init_sysfs);
917#endif