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