1 // SPDX-License-Identifier: GPL-2.0
3 * linux/mm/swap_state.c
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
6 * Swap reorganised 29.12.95, Stephen Tweedie
8 * Rewritten to use page cache, (C) 1998 Stephen Tweedie
11 #include <linux/gfp.h>
12 #include <linux/kernel_stat.h>
13 #include <linux/mempolicy.h>
14 #include <linux/swap.h>
15 #include <linux/swapops.h>
16 #include <linux/init.h>
17 #include <linux/pagemap.h>
18 #include <linux/pagevec.h>
19 #include <linux/backing-dev.h>
20 #include <linux/blkdev.h>
21 #include <linux/migrate.h>
22 #include <linux/vmalloc.h>
23 #include <linux/swap_slots.h>
24 #include <linux/huge_mm.h>
25 #include <linux/shmem_fs.h>
30 * swapper_space is a fiction, retained to simplify the path through
31 * vmscan's shrink_page_list.
33 static const struct address_space_operations swap_aops
= {
34 .writepage
= swap_writepage
,
35 .dirty_folio
= noop_dirty_folio
,
36 #ifdef CONFIG_MIGRATION
37 .migrate_folio
= migrate_folio
,
41 struct address_space
*swapper_spaces
[MAX_SWAPFILES
] __read_mostly
;
42 static unsigned int nr_swapper_spaces
[MAX_SWAPFILES
] __read_mostly
;
43 static bool enable_vma_readahead __read_mostly
= true;
45 #define SWAP_RA_WIN_SHIFT (PAGE_SHIFT / 2)
46 #define SWAP_RA_HITS_MASK ((1UL << SWAP_RA_WIN_SHIFT) - 1)
47 #define SWAP_RA_HITS_MAX SWAP_RA_HITS_MASK
48 #define SWAP_RA_WIN_MASK (~PAGE_MASK & ~SWAP_RA_HITS_MASK)
50 #define SWAP_RA_HITS(v) ((v) & SWAP_RA_HITS_MASK)
51 #define SWAP_RA_WIN(v) (((v) & SWAP_RA_WIN_MASK) >> SWAP_RA_WIN_SHIFT)
52 #define SWAP_RA_ADDR(v) ((v) & PAGE_MASK)
54 #define SWAP_RA_VAL(addr, win, hits) \
55 (((addr) & PAGE_MASK) | \
56 (((win) << SWAP_RA_WIN_SHIFT) & SWAP_RA_WIN_MASK) | \
57 ((hits) & SWAP_RA_HITS_MASK))
59 /* Initial readahead hits is 4 to start up with a small window */
60 #define GET_SWAP_RA_VAL(vma) \
61 (atomic_long_read(&(vma)->swap_readahead_info) ? : 4)
63 static atomic_t swapin_readahead_hits
= ATOMIC_INIT(4);
65 void show_swap_cache_info(void)
67 printk("%lu pages in swap cache\n", total_swapcache_pages());
68 printk("Free swap = %ldkB\n", K(get_nr_swap_pages()));
69 printk("Total swap = %lukB\n", K(total_swap_pages
));
72 void *get_shadow_from_swap_cache(swp_entry_t entry
)
74 struct address_space
*address_space
= swap_address_space(entry
);
75 pgoff_t idx
= swp_offset(entry
);
78 page
= xa_load(&address_space
->i_pages
, idx
);
79 if (xa_is_value(page
))
85 * add_to_swap_cache resembles filemap_add_folio on swapper_space,
86 * but sets SwapCache flag and private instead of mapping and index.
88 int add_to_swap_cache(struct folio
*folio
, swp_entry_t entry
,
89 gfp_t gfp
, void **shadowp
)
91 struct address_space
*address_space
= swap_address_space(entry
);
92 pgoff_t idx
= swp_offset(entry
);
93 XA_STATE_ORDER(xas
, &address_space
->i_pages
, idx
, folio_order(folio
));
94 unsigned long i
, nr
= folio_nr_pages(folio
);
97 xas_set_update(&xas
, workingset_update_node
);
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
);
103 folio_ref_add(folio
, nr
);
104 folio_set_swapcache(folio
);
109 xas_create_range(&xas
);
112 for (i
= 0; i
< nr
; i
++) {
113 VM_BUG_ON_FOLIO(xas
.xa_index
!= idx
+ i
, folio
);
115 old
= xas_load(&xas
);
116 if (xa_is_value(old
))
119 xas_store(&xas
, folio
);
122 address_space
->nrpages
+= nr
;
123 __node_stat_mod_folio(folio
, NR_FILE_PAGES
, nr
);
124 __lruvec_stat_mod_folio(folio
, NR_SWAPCACHE
, nr
);
126 xas_unlock_irq(&xas
);
127 } while (xas_nomem(&xas
, gfp
));
129 if (!xas_error(&xas
))
132 folio_clear_swapcache(folio
);
133 folio_ref_sub(folio
, nr
);
134 return xas_error(&xas
);
138 * This must be called only on folios that have
139 * been verified to be in the swap cache.
141 void __delete_from_swap_cache(struct folio
*folio
,
142 swp_entry_t entry
, void *shadow
)
144 struct address_space
*address_space
= swap_address_space(entry
);
146 long nr
= folio_nr_pages(folio
);
147 pgoff_t idx
= swp_offset(entry
);
148 XA_STATE(xas
, &address_space
->i_pages
, idx
);
150 xas_set_update(&xas
, workingset_update_node
);
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
);
156 for (i
= 0; i
< nr
; i
++) {
157 void *entry
= xas_store(&xas
, shadow
);
158 VM_BUG_ON_PAGE(entry
!= folio
, entry
);
162 folio_clear_swapcache(folio
);
163 address_space
->nrpages
-= nr
;
164 __node_stat_mod_folio(folio
, NR_FILE_PAGES
, -nr
);
165 __lruvec_stat_mod_folio(folio
, NR_SWAPCACHE
, -nr
);
169 * add_to_swap - allocate swap space for a folio
170 * @folio: folio we want to move to swap
172 * Allocate swap space for the folio and add the folio to the
175 * Context: Caller needs to hold the folio lock.
176 * Return: Whether the folio was added to the swap cache.
178 bool add_to_swap(struct folio
*folio
)
183 VM_BUG_ON_FOLIO(!folio_test_locked(folio
), folio
);
184 VM_BUG_ON_FOLIO(!folio_test_uptodate(folio
), folio
);
186 entry
= folio_alloc_swap(folio
);
191 * XArray node allocations from PF_MEMALLOC contexts could
192 * completely exhaust the page allocator. __GFP_NOMEMALLOC
193 * stops emergency reserves from being allocated.
195 * TODO: this could cause a theoretical memory reclaim
196 * deadlock in the swap out path.
199 * Add it to the swap cache.
201 err
= add_to_swap_cache(folio
, entry
,
202 __GFP_HIGH
|__GFP_NOMEMALLOC
|__GFP_NOWARN
, NULL
);
205 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
206 * clear SWAP_HAS_CACHE flag.
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.
220 folio_mark_dirty(folio
);
225 put_swap_folio(folio
, entry
);
230 * This must be called only on folios that have
231 * been verified to be in the swap cache and locked.
232 * It will never put the folio into the free list,
233 * the caller has a reference on the folio.
235 void delete_from_swap_cache(struct folio
*folio
)
237 swp_entry_t entry
= folio
->swap
;
238 struct address_space
*address_space
= swap_address_space(entry
);
240 xa_lock_irq(&address_space
->i_pages
);
241 __delete_from_swap_cache(folio
, entry
, NULL
);
242 xa_unlock_irq(&address_space
->i_pages
);
244 put_swap_folio(folio
, entry
);
245 folio_ref_sub(folio
, folio_nr_pages(folio
));
248 void clear_shadow_from_swap_cache(int type
, unsigned long begin
,
251 unsigned long curr
= begin
;
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
);
259 xas_set_update(&xas
, workingset_update_node
);
261 xa_lock_irq(&address_space
->i_pages
);
262 xas_for_each(&xas
, old
, end
) {
263 if (!xa_is_value(old
))
265 xas_store(&xas
, NULL
);
267 xa_unlock_irq(&address_space
->i_pages
);
269 /* search the next swapcache until we meet end */
270 curr
>>= SWAP_ADDRESS_SPACE_SHIFT
;
272 curr
<<= SWAP_ADDRESS_SPACE_SHIFT
;
279 * If we are the only user, then try to free up the swap cache.
281 * Its ok to check the swapcache flag without the folio lock
282 * here because we are going to recheck again inside
283 * folio_free_swap() _with_ the lock.
286 void free_swap_cache(struct folio
*folio
)
288 if (folio_test_swapcache(folio
) && !folio_mapped(folio
) &&
289 folio_trylock(folio
)) {
290 folio_free_swap(folio
);
296 * Perform a free_page(), also freeing any swap cache associated with
297 * this page if it is the last user of the page.
299 void free_page_and_swap_cache(struct page
*page
)
301 struct folio
*folio
= page_folio(page
);
303 free_swap_cache(folio
);
304 if (!is_huge_zero_page(page
))
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.
312 void free_pages_and_swap_cache(struct encoded_page
**pages
, int nr
)
314 struct folio_batch folios
;
315 unsigned int refs
[PAGEVEC_SIZE
];
318 folio_batch_init(&folios
);
319 for (int i
= 0; i
< nr
; i
++) {
320 struct folio
*folio
= page_folio(encoded_page_ptr(pages
[i
]));
322 free_swap_cache(folio
);
324 if (unlikely(encoded_page_flags(pages
[i
]) &
325 ENCODED_PAGE_BIT_NR_PAGES_NEXT
))
326 refs
[folios
.nr
] = encoded_nr_pages(pages
[++i
]);
328 if (folio_batch_add(&folios
, folio
) == 0)
329 folios_put_refs(&folios
, refs
);
332 folios_put_refs(&folios
, refs
);
335 static inline bool swap_use_vma_readahead(void)
337 return READ_ONCE(enable_vma_readahead
) && !atomic_read(&nr_rotate_swap
);
341 * Lookup a swap entry in the swap cache. A found folio will be returned
342 * unlocked and with its refcount incremented - we rely on the kernel
343 * lock getting page table operations atomic even if we drop the folio
344 * lock before returning.
346 * Caller must lock the swap device or hold a reference to keep it valid.
348 struct folio
*swap_cache_get_folio(swp_entry_t entry
,
349 struct vm_area_struct
*vma
, unsigned long addr
)
353 folio
= filemap_get_folio(swap_address_space(entry
), swp_offset(entry
));
354 if (!IS_ERR(folio
)) {
355 bool vma_ra
= swap_use_vma_readahead();
359 * At the moment, we don't support PG_readahead for anon THP
360 * so let's bail out rather than confusing the readahead stat.
362 if (unlikely(folio_test_large(folio
)))
365 readahead
= folio_test_clear_readahead(folio
);
367 unsigned long ra_val
;
370 ra_val
= GET_SWAP_RA_VAL(vma
);
371 win
= SWAP_RA_WIN(ra_val
);
372 hits
= SWAP_RA_HITS(ra_val
);
374 hits
= min_t(int, hits
+ 1, SWAP_RA_HITS_MAX
);
375 atomic_long_set(&vma
->swap_readahead_info
,
376 SWAP_RA_VAL(addr
, win
, hits
));
380 count_vm_event(SWAP_RA_HIT
);
382 atomic_inc(&swapin_readahead_hits
);
392 * filemap_get_incore_folio - Find and get a folio from the page or swap caches.
393 * @mapping: The address_space to search.
394 * @index: The page cache index.
396 * This differs from filemap_get_folio() in that it will also look for the
397 * folio in the swap cache.
399 * Return: The found folio or %NULL.
401 struct folio
*filemap_get_incore_folio(struct address_space
*mapping
,
405 struct swap_info_struct
*si
;
406 struct folio
*folio
= filemap_get_entry(mapping
, index
);
409 return ERR_PTR(-ENOENT
);
410 if (!xa_is_value(folio
))
412 if (!shmem_mapping(mapping
))
413 return ERR_PTR(-ENOENT
);
415 swp
= radix_to_swp_entry(folio
);
416 /* There might be swapin error entries in shmem mapping. */
417 if (non_swap_entry(swp
))
418 return ERR_PTR(-ENOENT
);
419 /* Prevent swapoff from happening to us */
420 si
= get_swap_device(swp
);
422 return ERR_PTR(-ENOENT
);
423 index
= swp_offset(swp
);
424 folio
= filemap_get_folio(swap_address_space(swp
), index
);
429 struct folio
*__read_swap_cache_async(swp_entry_t entry
, gfp_t gfp_mask
,
430 struct mempolicy
*mpol
, pgoff_t ilx
, bool *new_page_allocated
,
433 struct swap_info_struct
*si
;
437 *new_page_allocated
= false;
438 si
= get_swap_device(entry
);
445 * First check the swap cache. Since this is normally
446 * called after swap_cache_get_folio() failed, re-calling
447 * that would confuse statistics.
449 folio
= filemap_get_folio(swap_address_space(entry
),
455 * Just skip read ahead for unused swap slot.
456 * During swap_off when swap_slot_cache is disabled,
457 * we have to handle the race between putting
458 * swap entry in swap cache and marking swap slot
459 * as SWAP_HAS_CACHE. That's done in later part of code or
460 * else swap_off will be aborted if we return NULL.
462 if (!swap_swapcount(si
, entry
) && swap_slot_cache_enabled
)
466 * Get a new folio to read into from swap. Allocate it now,
467 * before marking swap_map SWAP_HAS_CACHE, when -EEXIST will
468 * cause any racers to loop around until we add it to cache.
470 folio
= (struct folio
*)alloc_pages_mpol(gfp_mask
, 0,
471 mpol
, ilx
, numa_node_id());
476 * Swap entry may have been freed since our caller observed it.
478 err
= swapcache_prepare(entry
);
487 * Protect against a recursive call to __read_swap_cache_async()
488 * on the same entry waiting forever here because SWAP_HAS_CACHE
489 * is set but the folio is not the swap cache yet. This can
490 * happen today if mem_cgroup_swapin_charge_folio() below
491 * triggers reclaim through zswap, which may call
492 * __read_swap_cache_async() in the writeback path.
498 * We might race against __delete_from_swap_cache(), and
499 * stumble across a swap_map entry whose SWAP_HAS_CACHE
500 * has not yet been cleared. Or race against another
501 * __read_swap_cache_async(), which has set SWAP_HAS_CACHE
502 * in swap_map, but not yet added its folio to swap cache.
504 schedule_timeout_uninterruptible(1);
508 * The swap entry is ours to swap in. Prepare the new folio.
511 __folio_set_locked(folio
);
512 __folio_set_swapbacked(folio
);
514 if (mem_cgroup_swapin_charge_folio(folio
, NULL
, gfp_mask
, entry
))
517 /* May fail (-ENOMEM) if XArray node allocation failed. */
518 if (add_to_swap_cache(folio
, entry
, gfp_mask
& GFP_RECLAIM_MASK
, &shadow
))
521 mem_cgroup_swapin_uncharge_swap(entry
);
524 workingset_refault(folio
, shadow
);
526 /* Caller will initiate read into locked folio */
527 folio_add_lru(folio
);
528 *new_page_allocated
= true;
534 put_swap_folio(folio
, entry
);
543 * Locate a page of swap in physical memory, reserving swap cache space
544 * and reading the disk if it is not already cached.
545 * A failure return means that either the page allocation failed or that
546 * the swap entry is no longer in use.
548 * get/put_swap_device() aren't needed to call this function, because
549 * __read_swap_cache_async() call them and swap_read_folio() holds the
550 * swap cache folio lock.
552 struct folio
*read_swap_cache_async(swp_entry_t entry
, gfp_t gfp_mask
,
553 struct vm_area_struct
*vma
, unsigned long addr
,
554 struct swap_iocb
**plug
)
557 struct mempolicy
*mpol
;
561 mpol
= get_vma_policy(vma
, addr
, 0, &ilx
);
562 folio
= __read_swap_cache_async(entry
, gfp_mask
, mpol
, ilx
,
563 &page_allocated
, false);
567 swap_read_folio(folio
, false, plug
);
571 static unsigned int __swapin_nr_pages(unsigned long prev_offset
,
572 unsigned long offset
,
577 unsigned int pages
, last_ra
;
580 * This heuristic has been found to work well on both sequential and
581 * random loads, swapping to hard disk or to SSD: please don't ask
582 * what the "+ 2" means, it just happens to work well, that's all.
587 * We can have no readahead hits to judge by: but must not get
588 * stuck here forever, so check for an adjacent offset instead
589 * (and don't even bother to check whether swap type is same).
591 if (offset
!= prev_offset
+ 1 && offset
!= prev_offset
- 1)
594 unsigned int roundup
= 4;
595 while (roundup
< pages
)
600 if (pages
> max_pages
)
603 /* Don't shrink readahead too fast */
604 last_ra
= prev_win
/ 2;
611 static unsigned long swapin_nr_pages(unsigned long offset
)
613 static unsigned long prev_offset
;
614 unsigned int hits
, pages
, max_pages
;
615 static atomic_t last_readahead_pages
;
617 max_pages
= 1 << READ_ONCE(page_cluster
);
621 hits
= atomic_xchg(&swapin_readahead_hits
, 0);
622 pages
= __swapin_nr_pages(READ_ONCE(prev_offset
), offset
, hits
,
624 atomic_read(&last_readahead_pages
));
626 WRITE_ONCE(prev_offset
, offset
);
627 atomic_set(&last_readahead_pages
, pages
);
633 * swap_cluster_readahead - swap in pages in hope we need them soon
634 * @entry: swap entry of this memory
635 * @gfp_mask: memory allocation flags
636 * @mpol: NUMA memory allocation policy to be applied
637 * @ilx: NUMA interleave index, for use only when MPOL_INTERLEAVE
639 * Returns the struct folio for entry and addr, after queueing swapin.
641 * Primitive swap readahead code. We simply read an aligned block of
642 * (1 << page_cluster) entries in the swap area. This method is chosen
643 * because it doesn't cost us any seek time. We also make sure to queue
644 * the 'original' request together with the readahead ones...
646 * Note: it is intentional that the same NUMA policy and interleave index
647 * are used for every page of the readahead: neighbouring pages on swap
648 * are fairly likely to have been swapped out from the same node.
650 struct folio
*swap_cluster_readahead(swp_entry_t entry
, gfp_t gfp_mask
,
651 struct mempolicy
*mpol
, pgoff_t ilx
)
654 unsigned long entry_offset
= swp_offset(entry
);
655 unsigned long offset
= entry_offset
;
656 unsigned long start_offset
, end_offset
;
658 struct swap_info_struct
*si
= swp_swap_info(entry
);
659 struct blk_plug plug
;
660 struct swap_iocb
*splug
= NULL
;
663 mask
= swapin_nr_pages(offset
) - 1;
667 /* Read a page_cluster sized and aligned cluster around offset. */
668 start_offset
= offset
& ~mask
;
669 end_offset
= offset
| mask
;
670 if (!start_offset
) /* First page is swap header. */
672 if (end_offset
>= si
->max
)
673 end_offset
= si
->max
- 1;
675 blk_start_plug(&plug
);
676 for (offset
= start_offset
; offset
<= end_offset
; offset
++) {
677 /* Ok, do the async read-ahead now */
678 folio
= __read_swap_cache_async(
679 swp_entry(swp_type(entry
), offset
),
680 gfp_mask
, mpol
, ilx
, &page_allocated
, false);
683 if (page_allocated
) {
684 swap_read_folio(folio
, false, &splug
);
685 if (offset
!= entry_offset
) {
686 folio_set_readahead(folio
);
687 count_vm_event(SWAP_RA
);
692 blk_finish_plug(&plug
);
693 swap_read_unplug(splug
);
694 lru_add_drain(); /* Push any new pages onto the LRU now */
696 /* The page was likely read above, so no need for plugging here */
697 folio
= __read_swap_cache_async(entry
, gfp_mask
, mpol
, ilx
,
698 &page_allocated
, false);
699 if (unlikely(page_allocated
)) {
700 zswap_folio_swapin(folio
);
701 swap_read_folio(folio
, false, NULL
);
706 int init_swap_address_space(unsigned int type
, unsigned long nr_pages
)
708 struct address_space
*spaces
, *space
;
711 nr
= DIV_ROUND_UP(nr_pages
, SWAP_ADDRESS_SPACE_PAGES
);
712 spaces
= kvcalloc(nr
, sizeof(struct address_space
), GFP_KERNEL
);
715 for (i
= 0; i
< nr
; i
++) {
717 xa_init_flags(&space
->i_pages
, XA_FLAGS_LOCK_IRQ
);
718 atomic_set(&space
->i_mmap_writable
, 0);
719 space
->a_ops
= &swap_aops
;
720 /* swap cache doesn't use writeback related tags */
721 mapping_set_no_writeback_tags(space
);
723 nr_swapper_spaces
[type
] = nr
;
724 swapper_spaces
[type
] = spaces
;
729 void exit_swap_address_space(unsigned int type
)
732 struct address_space
*spaces
= swapper_spaces
[type
];
734 for (i
= 0; i
< nr_swapper_spaces
[type
]; i
++)
735 VM_WARN_ON_ONCE(!mapping_empty(&spaces
[i
]));
737 nr_swapper_spaces
[type
] = 0;
738 swapper_spaces
[type
] = NULL
;
741 #define SWAP_RA_ORDER_CEILING 5
743 struct vma_swap_readahead
{
745 unsigned short offset
;
746 unsigned short nr_pte
;
749 static void swap_ra_info(struct vm_fault
*vmf
,
750 struct vma_swap_readahead
*ra_info
)
752 struct vm_area_struct
*vma
= vmf
->vma
;
753 unsigned long ra_val
;
754 unsigned long faddr
, pfn
, fpfn
, lpfn
, rpfn
;
755 unsigned long start
, end
;
756 unsigned int max_win
, hits
, prev_win
, win
;
758 max_win
= 1 << min_t(unsigned int, READ_ONCE(page_cluster
),
759 SWAP_RA_ORDER_CEILING
);
765 faddr
= vmf
->address
;
766 fpfn
= PFN_DOWN(faddr
);
767 ra_val
= GET_SWAP_RA_VAL(vma
);
768 pfn
= PFN_DOWN(SWAP_RA_ADDR(ra_val
));
769 prev_win
= SWAP_RA_WIN(ra_val
);
770 hits
= SWAP_RA_HITS(ra_val
);
771 ra_info
->win
= win
= __swapin_nr_pages(pfn
, fpfn
, hits
,
773 atomic_long_set(&vma
->swap_readahead_info
,
774 SWAP_RA_VAL(faddr
, win
, 0));
778 if (fpfn
== pfn
+ 1) {
781 } else if (pfn
== fpfn
+ 1) {
782 lpfn
= fpfn
- win
+ 1;
785 unsigned int left
= (win
- 1) / 2;
788 rpfn
= fpfn
+ win
- left
;
790 start
= max3(lpfn
, PFN_DOWN(vma
->vm_start
),
791 PFN_DOWN(faddr
& PMD_MASK
));
792 end
= min3(rpfn
, PFN_DOWN(vma
->vm_end
),
793 PFN_DOWN((faddr
& PMD_MASK
) + PMD_SIZE
));
795 ra_info
->nr_pte
= end
- start
;
796 ra_info
->offset
= fpfn
- start
;
800 * swap_vma_readahead - swap in pages in hope we need them soon
801 * @targ_entry: swap entry of the targeted memory
802 * @gfp_mask: memory allocation flags
803 * @mpol: NUMA memory allocation policy to be applied
804 * @targ_ilx: NUMA interleave index, for use only when MPOL_INTERLEAVE
805 * @vmf: fault information
807 * Returns the struct folio for entry and addr, after queueing swapin.
809 * Primitive swap readahead code. We simply read in a few pages whose
810 * virtual addresses are around the fault address in the same vma.
812 * Caller must hold read mmap_lock if vmf->vma is not NULL.
815 static struct folio
*swap_vma_readahead(swp_entry_t targ_entry
, gfp_t gfp_mask
,
816 struct mempolicy
*mpol
, pgoff_t targ_ilx
, struct vm_fault
*vmf
)
818 struct blk_plug plug
;
819 struct swap_iocb
*splug
= NULL
;
821 pte_t
*pte
= NULL
, pentry
;
827 struct vma_swap_readahead ra_info
= {
831 swap_ra_info(vmf
, &ra_info
);
832 if (ra_info
.win
== 1)
835 addr
= vmf
->address
- (ra_info
.offset
* PAGE_SIZE
);
836 ilx
= targ_ilx
- ra_info
.offset
;
838 blk_start_plug(&plug
);
839 for (i
= 0; i
< ra_info
.nr_pte
; i
++, ilx
++, addr
+= PAGE_SIZE
) {
841 pte
= pte_offset_map(vmf
->pmd
, addr
);
845 pentry
= ptep_get_lockless(pte
);
846 if (!is_swap_pte(pentry
))
848 entry
= pte_to_swp_entry(pentry
);
849 if (unlikely(non_swap_entry(entry
)))
853 folio
= __read_swap_cache_async(entry
, gfp_mask
, mpol
, ilx
,
854 &page_allocated
, false);
857 if (page_allocated
) {
858 swap_read_folio(folio
, false, &splug
);
859 if (i
!= ra_info
.offset
) {
860 folio_set_readahead(folio
);
861 count_vm_event(SWAP_RA
);
868 blk_finish_plug(&plug
);
869 swap_read_unplug(splug
);
872 /* The folio was likely read above, so no need for plugging here */
873 folio
= __read_swap_cache_async(targ_entry
, gfp_mask
, mpol
, targ_ilx
,
874 &page_allocated
, false);
875 if (unlikely(page_allocated
)) {
876 zswap_folio_swapin(folio
);
877 swap_read_folio(folio
, false, NULL
);
883 * swapin_readahead - swap in pages in hope we need them soon
884 * @entry: swap entry of this memory
885 * @gfp_mask: memory allocation flags
886 * @vmf: fault information
888 * Returns the struct page for entry and addr, after queueing swapin.
890 * It's a main entry function for swap readahead. By the configuration,
891 * it will read ahead blocks by cluster-based(ie, physical disk based)
892 * or vma-based(ie, virtual address based on faulty address) readahead.
894 struct page
*swapin_readahead(swp_entry_t entry
, gfp_t gfp_mask
,
895 struct vm_fault
*vmf
)
897 struct mempolicy
*mpol
;
901 mpol
= get_vma_policy(vmf
->vma
, vmf
->address
, 0, &ilx
);
902 folio
= swap_use_vma_readahead() ?
903 swap_vma_readahead(entry
, gfp_mask
, mpol
, ilx
, vmf
) :
904 swap_cluster_readahead(entry
, gfp_mask
, mpol
, ilx
);
909 return folio_file_page(folio
, swp_offset(entry
));
913 static ssize_t
vma_ra_enabled_show(struct kobject
*kobj
,
914 struct kobj_attribute
*attr
, char *buf
)
916 return sysfs_emit(buf
, "%s\n",
917 enable_vma_readahead
? "true" : "false");
919 static ssize_t
vma_ra_enabled_store(struct kobject
*kobj
,
920 struct kobj_attribute
*attr
,
921 const char *buf
, size_t count
)
925 ret
= kstrtobool(buf
, &enable_vma_readahead
);
931 static struct kobj_attribute vma_ra_enabled_attr
= __ATTR_RW(vma_ra_enabled
);
933 static struct attribute
*swap_attrs
[] = {
934 &vma_ra_enabled_attr
.attr
,
938 static const struct attribute_group swap_attr_group
= {
942 static int __init
swap_init_sysfs(void)
945 struct kobject
*swap_kobj
;
947 swap_kobj
= kobject_create_and_add("swap", mm_kobj
);
949 pr_err("failed to create swap kobject\n");
952 err
= sysfs_create_group(swap_kobj
, &swap_attr_group
);
954 pr_err("failed to register swap group\n");
960 kobject_put(swap_kobj
);
963 subsys_initcall(swap_init_sysfs
);