Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/kernel_stat.h> |
| 13 | #include <linux/swap.h> |
Hugh Dickins | 46017e9 | 2008-02-04 22:28:41 -0800 | [diff] [blame] | 14 | #include <linux/swapops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/pagemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/backing-dev.h> |
Christian Ehrhardt | 3fb5c29 | 2012-07-31 16:41:44 -0700 | [diff] [blame] | 18 | #include <linux/blkdev.h> |
Hugh Dickins | c484d41 | 2006-01-06 00:10:55 -0800 | [diff] [blame] | 19 | #include <linux/pagevec.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 20 | #include <linux/migrate.h> |
Huang, Ying | 4b3ef9d | 2017-02-22 15:45:26 -0800 | [diff] [blame] | 21 | #include <linux/vmalloc.h> |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 22 | #include <linux/swap_slots.h> |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 23 | #include <linux/huge_mm.h> |
Matthew Wilcox (Oracle) | 61ef186 | 2020-10-13 16:51:17 -0700 | [diff] [blame] | 24 | #include <linux/shmem_fs.h> |
Hugh Dickins | 243bce0 | 2020-06-25 20:29:59 -0700 | [diff] [blame] | 25 | #include "internal.h" |
NeilBrown | 014bb1d | 2022-05-09 18:20:47 -0700 | [diff] [blame] | 26 | #include "swap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * swapper_space is a fiction, retained to simplify the path through |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 30 | * vmscan's shrink_page_list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | */ |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 32 | static const struct address_space_operations swap_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | .writepage = swap_writepage, |
NeilBrown | 4c4a763 | 2022-05-09 18:20:47 -0700 | [diff] [blame] | 34 | .dirty_folio = noop_dirty_folio, |
Andrew Morton | 1c93923 | 2014-10-09 15:27:59 -0700 | [diff] [blame] | 35 | #ifdef CONFIG_MIGRATION |
Matthew Wilcox (Oracle) | 5418465 | 2022-06-06 10:27:41 -0400 | [diff] [blame] | 36 | .migrate_folio = migrate_folio, |
Andrew Morton | 1c93923 | 2014-10-09 15:27:59 -0700 | [diff] [blame] | 37 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
Changbin Du | 783cb68 | 2017-11-15 17:36:06 -0800 | [diff] [blame] | 40 | struct address_space *swapper_spaces[MAX_SWAPFILES] __read_mostly; |
| 41 | static unsigned int nr_swapper_spaces[MAX_SWAPFILES] __read_mostly; |
Colin Ian King | f5c754d | 2018-04-05 16:25:05 -0700 | [diff] [blame] | 42 | static bool enable_vma_readahead __read_mostly = true; |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 43 | |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Shaohua Li | 579f829 | 2014-02-06 12:04:21 -0800 | [diff] [blame] | 62 | static atomic_t swapin_readahead_hits = ATOMIC_INIT(4); |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | void show_swap_cache_info(void) |
| 65 | { |
Shaohua Li | 33806f0 | 2013-02-22 16:34:37 -0800 | [diff] [blame] | 66 | printk("%lu pages in swap cache\n", total_swapcache_pages()); |
Shaohua Li | ec8acf2 | 2013-02-22 16:34:38 -0800 | [diff] [blame] | 67 | printk("Free swap = %ldkB\n", |
| 68 | get_nr_swap_pages() << (PAGE_SHIFT - 10)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10)); |
| 70 | } |
| 71 | |
Joonsoo Kim | aae466b | 2020-08-11 18:30:50 -0700 | [diff] [blame] | 72 | void *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 | |
Matthew Wilcox (Oracle) | 8c647dd | 2021-02-25 17:15:33 -0800 | [diff] [blame] | 78 | page = xa_load(&address_space->i_pages, idx); |
Joonsoo Kim | aae466b | 2020-08-11 18:30:50 -0700 | [diff] [blame] | 79 | if (xa_is_value(page)) |
| 80 | return page; |
Joonsoo Kim | aae466b | 2020-08-11 18:30:50 -0700 | [diff] [blame] | 81 | return NULL; |
| 82 | } |
| 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | /* |
Matthew Wilcox (Oracle) | 2bb876b | 2022-06-01 15:13:59 -0400 | [diff] [blame] | 85 | * add_to_swap_cache resembles filemap_add_folio on swapper_space, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | * but sets SwapCache flag and private instead of mapping and index. |
| 87 | */ |
Matthew Wilcox (Oracle) | a4c366f | 2022-09-02 20:46:08 +0100 | [diff] [blame] | 88 | int add_to_swap_cache(struct folio *folio, swp_entry_t entry, |
Joonsoo Kim | 3852f67 | 2020-08-11 18:30:47 -0700 | [diff] [blame] | 89 | gfp_t gfp, void **shadowp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
Matthew Wilcox | 8d93b41 | 2017-11-27 15:46:54 -0500 | [diff] [blame] | 91 | struct address_space *address_space = swap_address_space(entry); |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 92 | pgoff_t idx = swp_offset(entry); |
Matthew Wilcox (Oracle) | a4c366f | 2022-09-02 20:46:08 +0100 | [diff] [blame] | 93 | XA_STATE_ORDER(xas, &address_space->i_pages, idx, folio_order(folio)); |
| 94 | unsigned long i, nr = folio_nr_pages(folio); |
Joonsoo Kim | 3852f67 | 2020-08-11 18:30:47 -0700 | [diff] [blame] | 95 | void *old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Yang Yang | 5649d11 | 2023-01-18 20:13:03 +0800 | [diff] [blame] | 97 | xas_set_update(&xas, workingset_update_node); |
| 98 | |
Matthew Wilcox (Oracle) | a4c366f | 2022-09-02 20:46:08 +0100 | [diff] [blame] | 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); |
Hugh Dickins | 51726b1 | 2009-01-06 14:39:25 -0800 | [diff] [blame] | 102 | |
Matthew Wilcox (Oracle) | a4c366f | 2022-09-02 20:46:08 +0100 | [diff] [blame] | 103 | folio_ref_add(folio, nr); |
| 104 | folio_set_swapcache(folio); |
Daisuke Nishimura | 31a5639 | 2009-09-21 17:02:50 -0700 | [diff] [blame] | 105 | |
Matthew Wilcox | 8d93b41 | 2017-11-27 15:46:54 -0500 | [diff] [blame] | 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++) { |
Matthew Wilcox (Oracle) | a4c366f | 2022-09-02 20:46:08 +0100 | [diff] [blame] | 112 | VM_BUG_ON_FOLIO(xas.xa_index != idx + i, folio); |
Joonsoo Kim | 3852f67 | 2020-08-11 18:30:47 -0700 | [diff] [blame] | 113 | old = xas_load(&xas); |
| 114 | if (xa_is_value(old)) { |
Joonsoo Kim | 3852f67 | 2020-08-11 18:30:47 -0700 | [diff] [blame] | 115 | if (shadowp) |
| 116 | *shadowp = old; |
| 117 | } |
Matthew Wilcox (Oracle) | a4c366f | 2022-09-02 20:46:08 +0100 | [diff] [blame] | 118 | set_page_private(folio_page(folio, i), entry.val + i); |
| 119 | xas_store(&xas, folio); |
Matthew Wilcox | 8d93b41 | 2017-11-27 15:46:54 -0500 | [diff] [blame] | 120 | xas_next(&xas); |
| 121 | } |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 122 | address_space->nrpages += nr; |
Matthew Wilcox (Oracle) | a4c366f | 2022-09-02 20:46:08 +0100 | [diff] [blame] | 123 | __node_stat_mod_folio(folio, NR_FILE_PAGES, nr); |
| 124 | __lruvec_stat_mod_folio(folio, NR_SWAPCACHE, nr); |
Matthew Wilcox | 8d93b41 | 2017-11-27 15:46:54 -0500 | [diff] [blame] | 125 | unlock: |
| 126 | xas_unlock_irq(&xas); |
| 127 | } while (xas_nomem(&xas, gfp)); |
Daisuke Nishimura | 31a5639 | 2009-09-21 17:02:50 -0700 | [diff] [blame] | 128 | |
Matthew Wilcox | 8d93b41 | 2017-11-27 15:46:54 -0500 | [diff] [blame] | 129 | if (!xas_error(&xas)) |
| 130 | return 0; |
Daisuke Nishimura | 31a5639 | 2009-09-21 17:02:50 -0700 | [diff] [blame] | 131 | |
Matthew Wilcox (Oracle) | a4c366f | 2022-09-02 20:46:08 +0100 | [diff] [blame] | 132 | folio_clear_swapcache(folio); |
| 133 | folio_ref_sub(folio, nr); |
Matthew Wilcox | 8d93b41 | 2017-11-27 15:46:54 -0500 | [diff] [blame] | 134 | return xas_error(&xas); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | /* |
Matthew Wilcox (Oracle) | ceff9d3 | 2022-06-17 18:50:20 +0100 | [diff] [blame] | 138 | * This must be called only on folios that have |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | * been verified to be in the swap cache. |
| 140 | */ |
Matthew Wilcox (Oracle) | ceff9d3 | 2022-06-17 18:50:20 +0100 | [diff] [blame] | 141 | void __delete_from_swap_cache(struct folio *folio, |
Joonsoo Kim | 3852f67 | 2020-08-11 18:30:47 -0700 | [diff] [blame] | 142 | swp_entry_t entry, void *shadow) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Matthew Wilcox | 4e17ec2 | 2017-11-29 08:32:39 -0500 | [diff] [blame] | 144 | struct address_space *address_space = swap_address_space(entry); |
Matthew Wilcox (Oracle) | ceff9d3 | 2022-06-17 18:50:20 +0100 | [diff] [blame] | 145 | int i; |
| 146 | long nr = folio_nr_pages(folio); |
Matthew Wilcox | 4e17ec2 | 2017-11-29 08:32:39 -0500 | [diff] [blame] | 147 | pgoff_t idx = swp_offset(entry); |
| 148 | XA_STATE(xas, &address_space->i_pages, idx); |
Shaohua Li | 33806f0 | 2013-02-22 16:34:37 -0800 | [diff] [blame] | 149 | |
Yang Yang | 5649d11 | 2023-01-18 20:13:03 +0800 | [diff] [blame] | 150 | xas_set_update(&xas, workingset_update_node); |
| 151 | |
Matthew Wilcox (Oracle) | ceff9d3 | 2022-06-17 18:50:20 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 156 | for (i = 0; i < nr; i++) { |
Joonsoo Kim | 3852f67 | 2020-08-11 18:30:47 -0700 | [diff] [blame] | 157 | void *entry = xas_store(&xas, shadow); |
Matthew Wilcox (Oracle) | b9eb777 | 2022-09-02 20:26:38 +0100 | [diff] [blame] | 158 | VM_BUG_ON_PAGE(entry != folio, entry); |
Matthew Wilcox (Oracle) | ceff9d3 | 2022-06-17 18:50:20 +0100 | [diff] [blame] | 159 | set_page_private(folio_page(folio, i), 0); |
Matthew Wilcox | 4e17ec2 | 2017-11-29 08:32:39 -0500 | [diff] [blame] | 160 | xas_next(&xas); |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 161 | } |
Matthew Wilcox (Oracle) | ceff9d3 | 2022-06-17 18:50:20 +0100 | [diff] [blame] | 162 | folio_clear_swapcache(folio); |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 163 | address_space->nrpages -= nr; |
Matthew Wilcox (Oracle) | ceff9d3 | 2022-06-17 18:50:20 +0100 | [diff] [blame] | 164 | __node_stat_mod_folio(folio, NR_FILE_PAGES, -nr); |
| 165 | __lruvec_stat_mod_folio(folio, NR_SWAPCACHE, -nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
Matthew Wilcox (Oracle) | 09c02e5 | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 169 | * add_to_swap - allocate swap space for a folio |
| 170 | * @folio: folio we want to move to swap |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | * |
Matthew Wilcox (Oracle) | 09c02e5 | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | */ |
Matthew Wilcox (Oracle) | 09c02e5 | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 178 | bool add_to_swap(struct folio *folio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { |
| 180 | swp_entry_t entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | int err; |
| 182 | |
Matthew Wilcox (Oracle) | 09c02e5 | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 183 | VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); |
| 184 | VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Matthew Wilcox (Oracle) | e2e3fdc | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 186 | entry = folio_alloc_swap(folio); |
Daisuke Nishimura | 2ca4532 | 2009-09-21 17:02:52 -0700 | [diff] [blame] | 187 | if (!entry.val) |
Matthew Wilcox (Oracle) | 09c02e5 | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 188 | return false; |
Minchan Kim | 0f07465 | 2017-07-06 15:37:24 -0700 | [diff] [blame] | 189 | |
Daisuke Nishimura | 2ca4532 | 2009-09-21 17:02:52 -0700 | [diff] [blame] | 190 | /* |
Matthew Wilcox | 8d93b41 | 2017-11-27 15:46:54 -0500 | [diff] [blame] | 191 | * XArray node allocations from PF_MEMALLOC contexts could |
Daisuke Nishimura | 2ca4532 | 2009-09-21 17:02:52 -0700 | [diff] [blame] | 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 | /* |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 199 | * Add it to the swap cache. |
Daisuke Nishimura | 2ca4532 | 2009-09-21 17:02:52 -0700 | [diff] [blame] | 200 | */ |
Matthew Wilcox (Oracle) | a4c366f | 2022-09-02 20:46:08 +0100 | [diff] [blame] | 201 | err = add_to_swap_cache(folio, entry, |
Joonsoo Kim | 3852f67 | 2020-08-11 18:30:47 -0700 | [diff] [blame] | 202 | __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN, NULL); |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 203 | if (err) |
Daisuke Nishimura | 2ca4532 | 2009-09-21 17:02:52 -0700 | [diff] [blame] | 204 | /* |
| 205 | * add_to_swap_cache() doesn't return -EEXIST, so we can safely |
| 206 | * clear SWAP_HAS_CACHE flag. |
| 207 | */ |
Minchan Kim | 0f07465 | 2017-07-06 15:37:24 -0700 | [diff] [blame] | 208 | goto fail; |
Shaohua Li | 9625456 | 2017-10-03 16:15:32 -0700 | [diff] [blame] | 209 | /* |
Matthew Wilcox (Oracle) | 09c02e5 | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 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. |
Shaohua Li | 9625456 | 2017-10-03 16:15:32 -0700 | [diff] [blame] | 219 | */ |
Matthew Wilcox (Oracle) | 09c02e5 | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 220 | folio_mark_dirty(folio); |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 221 | |
Matthew Wilcox (Oracle) | 09c02e5 | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 222 | return true; |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 223 | |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 224 | fail: |
Matthew Wilcox (Oracle) | 4081f74 | 2022-09-02 20:46:09 +0100 | [diff] [blame] | 225 | put_swap_folio(folio, entry); |
Matthew Wilcox (Oracle) | 09c02e5 | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 226 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* |
Matthew Wilcox (Oracle) | 75fa68a | 2022-06-17 18:50:19 +0100 | [diff] [blame] | 230 | * This must be called only on folios that have |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | * been verified to be in the swap cache and locked. |
Matthew Wilcox (Oracle) | 75fa68a | 2022-06-17 18:50:19 +0100 | [diff] [blame] | 232 | * It will never put the folio into the free list, |
| 233 | * the caller has a reference on the folio. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | */ |
Matthew Wilcox (Oracle) | 75fa68a | 2022-06-17 18:50:19 +0100 | [diff] [blame] | 235 | void delete_from_swap_cache(struct folio *folio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
Matthew Wilcox (Oracle) | 75fa68a | 2022-06-17 18:50:19 +0100 | [diff] [blame] | 237 | swp_entry_t entry = folio_swap_entry(folio); |
Matthew Wilcox | 4e17ec2 | 2017-11-29 08:32:39 -0500 | [diff] [blame] | 238 | struct address_space *address_space = swap_address_space(entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 240 | xa_lock_irq(&address_space->i_pages); |
Matthew Wilcox (Oracle) | ceff9d3 | 2022-06-17 18:50:20 +0100 | [diff] [blame] | 241 | __delete_from_swap_cache(folio, entry, NULL); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 242 | xa_unlock_irq(&address_space->i_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Matthew Wilcox (Oracle) | 4081f74 | 2022-09-02 20:46:09 +0100 | [diff] [blame] | 244 | put_swap_folio(folio, entry); |
Matthew Wilcox (Oracle) | 75fa68a | 2022-06-17 18:50:19 +0100 | [diff] [blame] | 245 | folio_ref_sub(folio, folio_nr_pages(folio)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Joonsoo Kim | 3852f67 | 2020-08-11 18:30:47 -0700 | [diff] [blame] | 248 | void 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 (;;) { |
Joonsoo Kim | 3852f67 | 2020-08-11 18:30:47 -0700 | [diff] [blame] | 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 | |
Yang Yang | 5649d11 | 2023-01-18 20:13:03 +0800 | [diff] [blame] | 259 | xas_set_update(&xas, workingset_update_node); |
| 260 | |
Joonsoo Kim | 3852f67 | 2020-08-11 18:30:47 -0700 | [diff] [blame] | 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); |
Joonsoo Kim | 3852f67 | 2020-08-11 18:30:47 -0700 | [diff] [blame] | 266 | } |
Joonsoo Kim | 3852f67 | 2020-08-11 18:30:47 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | /* |
| 279 | * If we are the only user, then try to free up the swap cache. |
| 280 | * |
Matthew Wilcox (Oracle) | aedd74d | 2022-09-02 20:46:35 +0100 | [diff] [blame] | 281 | * Its ok to check the swapcache flag without the folio lock |
Hugh Dickins | a2c43ee | 2009-01-06 14:39:36 -0800 | [diff] [blame] | 282 | * here because we are going to recheck again inside |
Matthew Wilcox (Oracle) | aedd74d | 2022-09-02 20:46:35 +0100 | [diff] [blame] | 283 | * folio_free_swap() _with_ the lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | * - Marcelo |
| 285 | */ |
Huang Ying | f4c4a3f | 2021-06-28 19:37:12 -0700 | [diff] [blame] | 286 | void free_swap_cache(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { |
Matthew Wilcox (Oracle) | aedd74d | 2022-09-02 20:46:35 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Perform a free_page(), also freeing any swap cache associated with |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 299 | * this page if it is the last user of the page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | */ |
| 301 | void free_page_and_swap_cache(struct page *page) |
| 302 | { |
| 303 | free_swap_cache(page); |
Aaron Lu | 6fcb52a | 2016-10-07 17:00:08 -0700 | [diff] [blame] | 304 | if (!is_huge_zero_page(page)) |
Gerald Schaefer | 770a537 | 2016-06-08 15:33:50 -0700 | [diff] [blame] | 305 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Linus Torvalds | 7cc8f9c | 2022-11-09 12:30:50 -0800 | [diff] [blame] | 312 | void free_pages_and_swap_cache(struct encoded_page **pages, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | lru_add_drain(); |
Linus Torvalds | 7cc8f9c | 2022-11-09 12:30:50 -0800 | [diff] [blame] | 315 | for (int i = 0; i < nr; i++) |
| 316 | free_swap_cache(encoded_page_ptr(pages[i])); |
| 317 | release_pages(pages, nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Minchan Kim | e9e9b7e | 2018-04-05 16:23:42 -0700 | [diff] [blame] | 320 | static inline bool swap_use_vma_readahead(void) |
| 321 | { |
| 322 | return READ_ONCE(enable_vma_readahead) && !atomic_read(&nr_rotate_swap); |
| 323 | } |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | /* |
Matthew Wilcox (Oracle) | c9edc24 | 2022-09-02 20:46:15 +0100 | [diff] [blame] | 326 | * Lookup a swap entry in the swap cache. A found folio will be returned |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | * unlocked and with its refcount incremented - we rely on the kernel |
Matthew Wilcox (Oracle) | c9edc24 | 2022-09-02 20:46:15 +0100 | [diff] [blame] | 328 | * lock getting page table operations atomic even if we drop the folio |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | * lock before returning. |
Kairui Song | cbc2bd9 | 2022-12-20 02:58:40 +0800 | [diff] [blame] | 330 | * |
| 331 | * Caller must lock the swap device or hold a reference to keep it valid. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | */ |
Matthew Wilcox (Oracle) | c9edc24 | 2022-09-02 20:46:15 +0100 | [diff] [blame] | 333 | struct folio *swap_cache_get_folio(swp_entry_t entry, |
| 334 | struct vm_area_struct *vma, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | { |
Matthew Wilcox (Oracle) | c9edc24 | 2022-09-02 20:46:15 +0100 | [diff] [blame] | 336 | struct folio *folio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Matthew Wilcox (Oracle) | c9edc24 | 2022-09-02 20:46:15 +0100 | [diff] [blame] | 338 | folio = filemap_get_folio(swap_address_space(entry), swp_offset(entry)); |
Matthew Wilcox (Oracle) | c9edc24 | 2022-09-02 20:46:15 +0100 | [diff] [blame] | 339 | if (folio) { |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 340 | bool vma_ra = swap_use_vma_readahead(); |
| 341 | bool readahead; |
| 342 | |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 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 | */ |
Matthew Wilcox (Oracle) | c9edc24 | 2022-09-02 20:46:15 +0100 | [diff] [blame] | 347 | if (unlikely(folio_test_large(folio))) |
| 348 | return folio; |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 349 | |
Matthew Wilcox (Oracle) | c9edc24 | 2022-09-02 20:46:15 +0100 | [diff] [blame] | 350 | readahead = folio_test_clear_readahead(folio); |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 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); |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 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 | } |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 363 | |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 364 | if (readahead) { |
Huang Ying | cbc65df | 2017-09-06 16:24:29 -0700 | [diff] [blame] | 365 | count_vm_event(SWAP_RA_HIT); |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 366 | if (!vma || !vma_ra) |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 367 | atomic_inc(&swapin_readahead_hits); |
Huang Ying | cbc65df | 2017-09-06 16:24:29 -0700 | [diff] [blame] | 368 | } |
Shaohua Li | 579f829 | 2014-02-06 12:04:21 -0800 | [diff] [blame] | 369 | } |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 370 | |
Matthew Wilcox (Oracle) | c9edc24 | 2022-09-02 20:46:15 +0100 | [diff] [blame] | 371 | return folio; |
| 372 | } |
| 373 | |
Matthew Wilcox (Oracle) | 61ef186 | 2020-10-13 16:51:17 -0700 | [diff] [blame] | 374 | /** |
Matthew Wilcox (Oracle) | 524984f | 2022-10-19 19:33:31 +0100 | [diff] [blame] | 375 | * filemap_get_incore_folio - Find and get a folio from the page or swap caches. |
Matthew Wilcox (Oracle) | 61ef186 | 2020-10-13 16:51:17 -0700 | [diff] [blame] | 376 | * @mapping: The address_space to search. |
| 377 | * @index: The page cache index. |
| 378 | * |
Matthew Wilcox (Oracle) | 524984f | 2022-10-19 19:33:31 +0100 | [diff] [blame] | 379 | * This differs from filemap_get_folio() in that it will also look for the |
| 380 | * folio in the swap cache. |
Matthew Wilcox (Oracle) | 61ef186 | 2020-10-13 16:51:17 -0700 | [diff] [blame] | 381 | * |
Matthew Wilcox (Oracle) | 524984f | 2022-10-19 19:33:31 +0100 | [diff] [blame] | 382 | * Return: The found folio or %NULL. |
Matthew Wilcox (Oracle) | 61ef186 | 2020-10-13 16:51:17 -0700 | [diff] [blame] | 383 | */ |
Matthew Wilcox (Oracle) | 524984f | 2022-10-19 19:33:31 +0100 | [diff] [blame] | 384 | struct folio *filemap_get_incore_folio(struct address_space *mapping, |
| 385 | pgoff_t index) |
Matthew Wilcox (Oracle) | 61ef186 | 2020-10-13 16:51:17 -0700 | [diff] [blame] | 386 | { |
| 387 | swp_entry_t swp; |
| 388 | struct swap_info_struct *si; |
Matthew Wilcox (Oracle) | dd8095b | 2022-10-19 19:33:30 +0100 | [diff] [blame] | 389 | struct folio *folio = __filemap_get_folio(mapping, index, FGP_ENTRY, 0); |
Matthew Wilcox (Oracle) | 61ef186 | 2020-10-13 16:51:17 -0700 | [diff] [blame] | 390 | |
Matthew Wilcox (Oracle) | dd8095b | 2022-10-19 19:33:30 +0100 | [diff] [blame] | 391 | if (!xa_is_value(folio)) |
| 392 | goto out; |
Matthew Wilcox (Oracle) | 61ef186 | 2020-10-13 16:51:17 -0700 | [diff] [blame] | 393 | if (!shmem_mapping(mapping)) |
| 394 | return NULL; |
| 395 | |
Matthew Wilcox (Oracle) | dd8095b | 2022-10-19 19:33:30 +0100 | [diff] [blame] | 396 | swp = radix_to_swp_entry(folio); |
Miaohe Lin | ba6851b | 2022-05-19 20:50:30 +0800 | [diff] [blame] | 397 | /* There might be swapin error entries in shmem mapping. */ |
| 398 | if (non_swap_entry(swp)) |
| 399 | return NULL; |
Matthew Wilcox (Oracle) | 61ef186 | 2020-10-13 16:51:17 -0700 | [diff] [blame] | 400 | /* Prevent swapoff from happening to us */ |
| 401 | si = get_swap_device(swp); |
| 402 | if (!si) |
| 403 | return NULL; |
Matthew Wilcox (Oracle) | dd8095b | 2022-10-19 19:33:30 +0100 | [diff] [blame] | 404 | index = swp_offset(swp); |
| 405 | folio = filemap_get_folio(swap_address_space(swp), index); |
Matthew Wilcox (Oracle) | 61ef186 | 2020-10-13 16:51:17 -0700 | [diff] [blame] | 406 | put_swap_device(si); |
Matthew Wilcox (Oracle) | dd8095b | 2022-10-19 19:33:30 +0100 | [diff] [blame] | 407 | out: |
Matthew Wilcox (Oracle) | 524984f | 2022-10-19 19:33:31 +0100 | [diff] [blame] | 408 | return folio; |
Matthew Wilcox (Oracle) | 61ef186 | 2020-10-13 16:51:17 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Dmitry Safonov | 5b999aa | 2015-09-08 15:05:00 -0700 | [diff] [blame] | 411 | struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, |
| 412 | struct vm_area_struct *vma, unsigned long addr, |
| 413 | bool *new_page_allocated) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
Huang Ying | eb08557 | 2019-07-11 20:55:33 -0700 | [diff] [blame] | 415 | struct swap_info_struct *si; |
Matthew Wilcox (Oracle) | a0d3374 | 2022-09-02 20:46:07 +0100 | [diff] [blame] | 416 | struct folio *folio; |
Joonsoo Kim | aae466b | 2020-08-11 18:30:50 -0700 | [diff] [blame] | 417 | void *shadow = NULL; |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 418 | |
Dmitry Safonov | 5b999aa | 2015-09-08 15:05:00 -0700 | [diff] [blame] | 419 | *new_page_allocated = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 421 | for (;;) { |
| 422 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | /* |
| 424 | * First check the swap cache. Since this is normally |
Matthew Wilcox (Oracle) | cb691e2 | 2022-09-02 20:46:34 +0100 | [diff] [blame] | 425 | * called after swap_cache_get_folio() failed, re-calling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | * that would confuse statistics. |
| 427 | */ |
Huang Ying | eb08557 | 2019-07-11 20:55:33 -0700 | [diff] [blame] | 428 | si = get_swap_device(entry); |
| 429 | if (!si) |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 430 | return NULL; |
Matthew Wilcox (Oracle) | a0d3374 | 2022-09-02 20:46:07 +0100 | [diff] [blame] | 431 | folio = filemap_get_folio(swap_address_space(entry), |
| 432 | swp_offset(entry)); |
Huang Ying | eb08557 | 2019-07-11 20:55:33 -0700 | [diff] [blame] | 433 | put_swap_device(si); |
Matthew Wilcox (Oracle) | a0d3374 | 2022-09-02 20:46:07 +0100 | [diff] [blame] | 434 | if (folio) |
| 435 | return folio_file_page(folio, swp_offset(entry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Huang Ying | ba81f83 | 2017-02-22 15:45:46 -0800 | [diff] [blame] | 437 | /* |
| 438 | * Just skip read ahead for unused swap slot. |
| 439 | * During swap_off when swap_slot_cache is disabled, |
| 440 | * we have to handle the race between putting |
| 441 | * swap entry in swap cache and marking swap slot |
| 442 | * as SWAP_HAS_CACHE. That's done in later part of code or |
| 443 | * else swap_off will be aborted if we return NULL. |
| 444 | */ |
| 445 | if (!__swp_swapcount(entry) && swap_slot_cache_enabled) |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 446 | return NULL; |
Tim Chen | e8c26ab | 2017-02-22 15:45:29 -0800 | [diff] [blame] | 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | /* |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 449 | * Get a new page to read into from swap. Allocate it now, |
| 450 | * before marking swap_map SWAP_HAS_CACHE, when -EEXIST will |
| 451 | * cause any racers to loop around until we add it to cache. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | */ |
Matthew Wilcox (Oracle) | a0d3374 | 2022-09-02 20:46:07 +0100 | [diff] [blame] | 453 | folio = vma_alloc_folio(gfp_mask, 0, vma, addr, false); |
| 454 | if (!folio) |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 455 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
| 457 | /* |
Hugh Dickins | f000944 | 2008-02-04 22:28:49 -0800 | [diff] [blame] | 458 | * Swap entry may have been freed since our caller observed it. |
| 459 | */ |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 460 | err = swapcache_prepare(entry); |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 461 | if (!err) |
Hugh Dickins | f000944 | 2008-02-04 22:28:49 -0800 | [diff] [blame] | 462 | break; |
| 463 | |
Matthew Wilcox (Oracle) | a0d3374 | 2022-09-02 20:46:07 +0100 | [diff] [blame] | 464 | folio_put(folio); |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 465 | if (err != -EEXIST) |
| 466 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 468 | /* |
| 469 | * We might race against __delete_from_swap_cache(), and |
| 470 | * stumble across a swap_map entry whose SWAP_HAS_CACHE |
| 471 | * has not yet been cleared. Or race against another |
| 472 | * __read_swap_cache_async(), which has set SWAP_HAS_CACHE |
| 473 | * in swap_map, but not yet added its page to swap cache. |
| 474 | */ |
Guo Ziliang | 029c462 | 2022-03-16 16:15:03 -0700 | [diff] [blame] | 475 | schedule_timeout_uninterruptible(1); |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /* |
| 479 | * The swap entry is ours to swap in. Prepare the new page. |
| 480 | */ |
| 481 | |
Matthew Wilcox (Oracle) | a0d3374 | 2022-09-02 20:46:07 +0100 | [diff] [blame] | 482 | __folio_set_locked(folio); |
| 483 | __folio_set_swapbacked(folio); |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 484 | |
Matthew Wilcox (Oracle) | 6599591 | 2022-09-02 20:46:12 +0100 | [diff] [blame] | 485 | if (mem_cgroup_swapin_charge_folio(folio, NULL, gfp_mask, entry)) |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 486 | goto fail_unlock; |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 487 | |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 488 | /* May fail (-ENOMEM) if XArray node allocation failed. */ |
Matthew Wilcox (Oracle) | a4c366f | 2022-09-02 20:46:08 +0100 | [diff] [blame] | 489 | if (add_to_swap_cache(folio, entry, gfp_mask & GFP_RECLAIM_MASK, &shadow)) |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 490 | goto fail_unlock; |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 491 | |
| 492 | mem_cgroup_swapin_uncharge_swap(entry); |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 493 | |
Joonsoo Kim | aae466b | 2020-08-11 18:30:50 -0700 | [diff] [blame] | 494 | if (shadow) |
Matthew Wilcox (Oracle) | a0d3374 | 2022-09-02 20:46:07 +0100 | [diff] [blame] | 495 | workingset_refault(folio, shadow); |
Johannes Weiner | 314b57f | 2020-06-03 16:03:03 -0700 | [diff] [blame] | 496 | |
Matthew Wilcox (Oracle) | a0d3374 | 2022-09-02 20:46:07 +0100 | [diff] [blame] | 497 | /* Caller will initiate read into locked folio */ |
| 498 | folio_add_lru(folio); |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 499 | *new_page_allocated = true; |
Matthew Wilcox (Oracle) | a0d3374 | 2022-09-02 20:46:07 +0100 | [diff] [blame] | 500 | return &folio->page; |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 501 | |
| 502 | fail_unlock: |
Matthew Wilcox (Oracle) | 4081f74 | 2022-09-02 20:46:09 +0100 | [diff] [blame] | 503 | put_swap_folio(folio, entry); |
Matthew Wilcox (Oracle) | a0d3374 | 2022-09-02 20:46:07 +0100 | [diff] [blame] | 504 | folio_unlock(folio); |
| 505 | folio_put(folio); |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 506 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | } |
Hugh Dickins | 46017e9 | 2008-02-04 22:28:41 -0800 | [diff] [blame] | 508 | |
Dmitry Safonov | 5b999aa | 2015-09-08 15:05:00 -0700 | [diff] [blame] | 509 | /* |
| 510 | * Locate a page of swap in physical memory, reserving swap cache space |
| 511 | * and reading the disk if it is not already cached. |
| 512 | * A failure return means that either the page allocation failed or that |
| 513 | * the swap entry is no longer in use. |
| 514 | */ |
| 515 | struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, |
NeilBrown | 5169b84 | 2022-05-09 18:20:49 -0700 | [diff] [blame] | 516 | struct vm_area_struct *vma, |
| 517 | unsigned long addr, bool do_poll, |
| 518 | struct swap_iocb **plug) |
Dmitry Safonov | 5b999aa | 2015-09-08 15:05:00 -0700 | [diff] [blame] | 519 | { |
| 520 | bool page_was_allocated; |
| 521 | struct page *retpage = __read_swap_cache_async(entry, gfp_mask, |
| 522 | vma, addr, &page_was_allocated); |
| 523 | |
| 524 | if (page_was_allocated) |
NeilBrown | 5169b84 | 2022-05-09 18:20:49 -0700 | [diff] [blame] | 525 | swap_readpage(retpage, do_poll, plug); |
Dmitry Safonov | 5b999aa | 2015-09-08 15:05:00 -0700 | [diff] [blame] | 526 | |
| 527 | return retpage; |
| 528 | } |
| 529 | |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 530 | static unsigned int __swapin_nr_pages(unsigned long prev_offset, |
| 531 | unsigned long offset, |
| 532 | int hits, |
| 533 | int max_pages, |
| 534 | int prev_win) |
Shaohua Li | 579f829 | 2014-02-06 12:04:21 -0800 | [diff] [blame] | 535 | { |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 536 | unsigned int pages, last_ra; |
Shaohua Li | 579f829 | 2014-02-06 12:04:21 -0800 | [diff] [blame] | 537 | |
| 538 | /* |
| 539 | * This heuristic has been found to work well on both sequential and |
| 540 | * random loads, swapping to hard disk or to SSD: please don't ask |
| 541 | * what the "+ 2" means, it just happens to work well, that's all. |
| 542 | */ |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 543 | pages = hits + 2; |
Shaohua Li | 579f829 | 2014-02-06 12:04:21 -0800 | [diff] [blame] | 544 | if (pages == 2) { |
| 545 | /* |
| 546 | * We can have no readahead hits to judge by: but must not get |
| 547 | * stuck here forever, so check for an adjacent offset instead |
| 548 | * (and don't even bother to check whether swap type is same). |
| 549 | */ |
| 550 | if (offset != prev_offset + 1 && offset != prev_offset - 1) |
| 551 | pages = 1; |
Shaohua Li | 579f829 | 2014-02-06 12:04:21 -0800 | [diff] [blame] | 552 | } else { |
| 553 | unsigned int roundup = 4; |
| 554 | while (roundup < pages) |
| 555 | roundup <<= 1; |
| 556 | pages = roundup; |
| 557 | } |
| 558 | |
| 559 | if (pages > max_pages) |
| 560 | pages = max_pages; |
| 561 | |
| 562 | /* Don't shrink readahead too fast */ |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 563 | last_ra = prev_win / 2; |
Shaohua Li | 579f829 | 2014-02-06 12:04:21 -0800 | [diff] [blame] | 564 | if (pages < last_ra) |
| 565 | pages = last_ra; |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 566 | |
| 567 | return pages; |
| 568 | } |
| 569 | |
| 570 | static unsigned long swapin_nr_pages(unsigned long offset) |
| 571 | { |
| 572 | static unsigned long prev_offset; |
| 573 | unsigned int hits, pages, max_pages; |
| 574 | static atomic_t last_readahead_pages; |
| 575 | |
| 576 | max_pages = 1 << READ_ONCE(page_cluster); |
| 577 | if (max_pages <= 1) |
| 578 | return 1; |
| 579 | |
| 580 | hits = atomic_xchg(&swapin_readahead_hits, 0); |
Qian Cai | d6c1f09 | 2020-06-01 21:48:40 -0700 | [diff] [blame] | 581 | pages = __swapin_nr_pages(READ_ONCE(prev_offset), offset, hits, |
| 582 | max_pages, |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 583 | atomic_read(&last_readahead_pages)); |
| 584 | if (!hits) |
Qian Cai | d6c1f09 | 2020-06-01 21:48:40 -0700 | [diff] [blame] | 585 | WRITE_ONCE(prev_offset, offset); |
Shaohua Li | 579f829 | 2014-02-06 12:04:21 -0800 | [diff] [blame] | 586 | atomic_set(&last_readahead_pages, pages); |
| 587 | |
| 588 | return pages; |
| 589 | } |
| 590 | |
Hugh Dickins | 46017e9 | 2008-02-04 22:28:41 -0800 | [diff] [blame] | 591 | /** |
Minchan Kim | e9e9b7e | 2018-04-05 16:23:42 -0700 | [diff] [blame] | 592 | * swap_cluster_readahead - swap in pages in hope we need them soon |
Hugh Dickins | 46017e9 | 2008-02-04 22:28:41 -0800 | [diff] [blame] | 593 | * @entry: swap entry of this memory |
Randy Dunlap | 7682486 | 2008-03-19 17:00:40 -0700 | [diff] [blame] | 594 | * @gfp_mask: memory allocation flags |
Minchan Kim | e9e9b7e | 2018-04-05 16:23:42 -0700 | [diff] [blame] | 595 | * @vmf: fault information |
Hugh Dickins | 46017e9 | 2008-02-04 22:28:41 -0800 | [diff] [blame] | 596 | * |
| 597 | * Returns the struct page for entry and addr, after queueing swapin. |
| 598 | * |
| 599 | * Primitive swap readahead code. We simply read an aligned block of |
| 600 | * (1 << page_cluster) entries in the swap area. This method is chosen |
| 601 | * because it doesn't cost us any seek time. We also make sure to queue |
| 602 | * the 'original' request together with the readahead ones... |
| 603 | * |
| 604 | * This has been extended to use the NUMA policies from the mm triggering |
| 605 | * the readahead. |
| 606 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 607 | * Caller must hold read mmap_lock if vmf->vma is not NULL. |
Hugh Dickins | 46017e9 | 2008-02-04 22:28:41 -0800 | [diff] [blame] | 608 | */ |
Minchan Kim | e9e9b7e | 2018-04-05 16:23:42 -0700 | [diff] [blame] | 609 | struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, |
| 610 | struct vm_fault *vmf) |
Hugh Dickins | 46017e9 | 2008-02-04 22:28:41 -0800 | [diff] [blame] | 611 | { |
Hugh Dickins | 46017e9 | 2008-02-04 22:28:41 -0800 | [diff] [blame] | 612 | struct page *page; |
Shaohua Li | 579f829 | 2014-02-06 12:04:21 -0800 | [diff] [blame] | 613 | unsigned long entry_offset = swp_offset(entry); |
| 614 | unsigned long offset = entry_offset; |
Rik van Riel | 67f96aa | 2012-03-21 16:33:50 -0700 | [diff] [blame] | 615 | unsigned long start_offset, end_offset; |
Shaohua Li | 579f829 | 2014-02-06 12:04:21 -0800 | [diff] [blame] | 616 | unsigned long mask; |
Huang Ying | e9a6eff | 2017-11-15 17:33:15 -0800 | [diff] [blame] | 617 | struct swap_info_struct *si = swp_swap_info(entry); |
Christian Ehrhardt | 3fb5c29 | 2012-07-31 16:41:44 -0700 | [diff] [blame] | 618 | struct blk_plug plug; |
NeilBrown | 5169b84 | 2022-05-09 18:20:49 -0700 | [diff] [blame] | 619 | struct swap_iocb *splug = NULL; |
Huang Ying | c4fa630 | 2017-09-06 16:24:33 -0700 | [diff] [blame] | 620 | bool do_poll = true, page_allocated; |
Minchan Kim | e9e9b7e | 2018-04-05 16:23:42 -0700 | [diff] [blame] | 621 | struct vm_area_struct *vma = vmf->vma; |
| 622 | unsigned long addr = vmf->address; |
Hugh Dickins | 46017e9 | 2008-02-04 22:28:41 -0800 | [diff] [blame] | 623 | |
Shaohua Li | 579f829 | 2014-02-06 12:04:21 -0800 | [diff] [blame] | 624 | mask = swapin_nr_pages(offset) - 1; |
| 625 | if (!mask) |
| 626 | goto skip; |
| 627 | |
Shaohua Li | 2395562 | 2017-07-10 15:47:11 -0700 | [diff] [blame] | 628 | do_poll = false; |
Rik van Riel | 67f96aa | 2012-03-21 16:33:50 -0700 | [diff] [blame] | 629 | /* Read a page_cluster sized and aligned cluster around offset. */ |
| 630 | start_offset = offset & ~mask; |
| 631 | end_offset = offset | mask; |
| 632 | if (!start_offset) /* First page is swap header. */ |
| 633 | start_offset++; |
Huang Ying | e9a6eff | 2017-11-15 17:33:15 -0800 | [diff] [blame] | 634 | if (end_offset >= si->max) |
| 635 | end_offset = si->max - 1; |
Rik van Riel | 67f96aa | 2012-03-21 16:33:50 -0700 | [diff] [blame] | 636 | |
Christian Ehrhardt | 3fb5c29 | 2012-07-31 16:41:44 -0700 | [diff] [blame] | 637 | blk_start_plug(&plug); |
Rik van Riel | 67f96aa | 2012-03-21 16:33:50 -0700 | [diff] [blame] | 638 | for (offset = start_offset; offset <= end_offset ; offset++) { |
Hugh Dickins | 46017e9 | 2008-02-04 22:28:41 -0800 | [diff] [blame] | 639 | /* Ok, do the async read-ahead now */ |
Huang Ying | c4fa630 | 2017-09-06 16:24:33 -0700 | [diff] [blame] | 640 | page = __read_swap_cache_async( |
| 641 | swp_entry(swp_type(entry), offset), |
| 642 | gfp_mask, vma, addr, &page_allocated); |
Hugh Dickins | 46017e9 | 2008-02-04 22:28:41 -0800 | [diff] [blame] | 643 | if (!page) |
Rik van Riel | 67f96aa | 2012-03-21 16:33:50 -0700 | [diff] [blame] | 644 | continue; |
Huang Ying | c4fa630 | 2017-09-06 16:24:33 -0700 | [diff] [blame] | 645 | if (page_allocated) { |
NeilBrown | 5169b84 | 2022-05-09 18:20:49 -0700 | [diff] [blame] | 646 | swap_readpage(page, false, &splug); |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 647 | if (offset != entry_offset) { |
Huang Ying | c4fa630 | 2017-09-06 16:24:33 -0700 | [diff] [blame] | 648 | SetPageReadahead(page); |
| 649 | count_vm_event(SWAP_RA); |
| 650 | } |
Huang Ying | cbc65df | 2017-09-06 16:24:29 -0700 | [diff] [blame] | 651 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 652 | put_page(page); |
Hugh Dickins | 46017e9 | 2008-02-04 22:28:41 -0800 | [diff] [blame] | 653 | } |
Christian Ehrhardt | 3fb5c29 | 2012-07-31 16:41:44 -0700 | [diff] [blame] | 654 | blk_finish_plug(&plug); |
NeilBrown | 5169b84 | 2022-05-09 18:20:49 -0700 | [diff] [blame] | 655 | swap_read_unplug(splug); |
Christian Ehrhardt | 3fb5c29 | 2012-07-31 16:41:44 -0700 | [diff] [blame] | 656 | |
Hugh Dickins | 46017e9 | 2008-02-04 22:28:41 -0800 | [diff] [blame] | 657 | lru_add_drain(); /* Push any new pages onto the LRU now */ |
Shaohua Li | 579f829 | 2014-02-06 12:04:21 -0800 | [diff] [blame] | 658 | skip: |
NeilBrown | 5169b84 | 2022-05-09 18:20:49 -0700 | [diff] [blame] | 659 | /* The page was likely read above, so no need for plugging here */ |
| 660 | return read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll, NULL); |
Hugh Dickins | 46017e9 | 2008-02-04 22:28:41 -0800 | [diff] [blame] | 661 | } |
Huang, Ying | 4b3ef9d | 2017-02-22 15:45:26 -0800 | [diff] [blame] | 662 | |
| 663 | int init_swap_address_space(unsigned int type, unsigned long nr_pages) |
| 664 | { |
| 665 | struct address_space *spaces, *space; |
| 666 | unsigned int i, nr; |
| 667 | |
| 668 | nr = DIV_ROUND_UP(nr_pages, SWAP_ADDRESS_SPACE_PAGES); |
Kees Cook | 778e1cd | 2018-06-12 14:04:48 -0700 | [diff] [blame] | 669 | spaces = kvcalloc(nr, sizeof(struct address_space), GFP_KERNEL); |
Huang, Ying | 4b3ef9d | 2017-02-22 15:45:26 -0800 | [diff] [blame] | 670 | if (!spaces) |
| 671 | return -ENOMEM; |
| 672 | for (i = 0; i < nr; i++) { |
| 673 | space = spaces + i; |
Matthew Wilcox | a283348 | 2017-12-05 19:04:20 -0500 | [diff] [blame] | 674 | xa_init_flags(&space->i_pages, XA_FLAGS_LOCK_IRQ); |
Huang, Ying | 4b3ef9d | 2017-02-22 15:45:26 -0800 | [diff] [blame] | 675 | atomic_set(&space->i_mmap_writable, 0); |
| 676 | space->a_ops = &swap_aops; |
| 677 | /* swap cache doesn't use writeback related tags */ |
| 678 | mapping_set_no_writeback_tags(space); |
Huang, Ying | 4b3ef9d | 2017-02-22 15:45:26 -0800 | [diff] [blame] | 679 | } |
| 680 | nr_swapper_spaces[type] = nr; |
Huang Ying | 054f1d1 | 2019-07-11 20:55:37 -0700 | [diff] [blame] | 681 | swapper_spaces[type] = spaces; |
Huang, Ying | 4b3ef9d | 2017-02-22 15:45:26 -0800 | [diff] [blame] | 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | void exit_swap_address_space(unsigned int type) |
| 687 | { |
Huang Ying | eea4a50 | 2021-06-28 19:37:16 -0700 | [diff] [blame] | 688 | int i; |
| 689 | struct address_space *spaces = swapper_spaces[type]; |
| 690 | |
| 691 | for (i = 0; i < nr_swapper_spaces[type]; i++) |
| 692 | VM_WARN_ON_ONCE(!mapping_empty(&spaces[i])); |
| 693 | kvfree(spaces); |
Huang, Ying | 4b3ef9d | 2017-02-22 15:45:26 -0800 | [diff] [blame] | 694 | nr_swapper_spaces[type] = 0; |
Huang Ying | 054f1d1 | 2019-07-11 20:55:37 -0700 | [diff] [blame] | 695 | swapper_spaces[type] = NULL; |
Huang, Ying | 4b3ef9d | 2017-02-22 15:45:26 -0800 | [diff] [blame] | 696 | } |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 697 | |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 698 | static void swap_ra_info(struct vm_fault *vmf, |
Kairui Song | 16ba391 | 2022-12-20 02:58:39 +0800 | [diff] [blame] | 699 | struct vma_swap_readahead *ra_info) |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 700 | { |
| 701 | struct vm_area_struct *vma = vmf->vma; |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 702 | unsigned long ra_val; |
Kairui Song | 16ba391 | 2022-12-20 02:58:39 +0800 | [diff] [blame] | 703 | unsigned long faddr, pfn, fpfn, lpfn, rpfn; |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 704 | unsigned long start, end; |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 705 | pte_t *pte, *orig_pte; |
Kairui Song | 16ba391 | 2022-12-20 02:58:39 +0800 | [diff] [blame] | 706 | unsigned int max_win, hits, prev_win, win; |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 707 | #ifndef CONFIG_64BIT |
| 708 | pte_t *tpte; |
| 709 | #endif |
| 710 | |
Huang Ying | 61b6397 | 2017-10-13 15:58:29 -0700 | [diff] [blame] | 711 | max_win = 1 << min_t(unsigned int, READ_ONCE(page_cluster), |
| 712 | SWAP_RA_ORDER_CEILING); |
| 713 | if (max_win == 1) { |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 714 | ra_info->win = 1; |
| 715 | return; |
Huang Ying | 61b6397 | 2017-10-13 15:58:29 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 718 | faddr = vmf->address; |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 719 | fpfn = PFN_DOWN(faddr); |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 720 | ra_val = GET_SWAP_RA_VAL(vma); |
| 721 | pfn = PFN_DOWN(SWAP_RA_ADDR(ra_val)); |
| 722 | prev_win = SWAP_RA_WIN(ra_val); |
| 723 | hits = SWAP_RA_HITS(ra_val); |
| 724 | ra_info->win = win = __swapin_nr_pages(pfn, fpfn, hits, |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 725 | max_win, prev_win); |
| 726 | atomic_long_set(&vma->swap_readahead_info, |
| 727 | SWAP_RA_VAL(faddr, win, 0)); |
| 728 | |
Kairui Song | 18ad72f | 2022-12-20 02:58:38 +0800 | [diff] [blame] | 729 | if (win == 1) |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 730 | return; |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 731 | |
| 732 | /* Copy the PTEs because the page table may be unmapped */ |
Kairui Song | 18ad72f | 2022-12-20 02:58:38 +0800 | [diff] [blame] | 733 | orig_pte = pte = pte_offset_map(vmf->pmd, faddr); |
Kairui Song | 16ba391 | 2022-12-20 02:58:39 +0800 | [diff] [blame] | 734 | if (fpfn == pfn + 1) { |
| 735 | lpfn = fpfn; |
| 736 | rpfn = fpfn + win; |
| 737 | } else if (pfn == fpfn + 1) { |
| 738 | lpfn = fpfn - win + 1; |
| 739 | rpfn = fpfn + 1; |
| 740 | } else { |
| 741 | unsigned int left = (win - 1) / 2; |
| 742 | |
| 743 | lpfn = fpfn - left; |
| 744 | rpfn = fpfn + win - left; |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 745 | } |
Kairui Song | 16ba391 | 2022-12-20 02:58:39 +0800 | [diff] [blame] | 746 | start = max3(lpfn, PFN_DOWN(vma->vm_start), |
| 747 | PFN_DOWN(faddr & PMD_MASK)); |
| 748 | end = min3(rpfn, PFN_DOWN(vma->vm_end), |
| 749 | PFN_DOWN((faddr & PMD_MASK) + PMD_SIZE)); |
| 750 | |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 751 | ra_info->nr_pte = end - start; |
| 752 | ra_info->offset = fpfn - start; |
| 753 | pte -= ra_info->offset; |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 754 | #ifdef CONFIG_64BIT |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 755 | ra_info->ptes = pte; |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 756 | #else |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 757 | tpte = ra_info->ptes; |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 758 | for (pfn = start; pfn != end; pfn++) |
| 759 | *tpte++ = *pte++; |
| 760 | #endif |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 761 | pte_unmap(orig_pte); |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Yang Shi | e9f5987 | 2019-03-05 15:44:15 -0800 | [diff] [blame] | 764 | /** |
| 765 | * swap_vma_readahead - swap in pages in hope we need them soon |
Krzysztof Kozlowski | 27ec487 | 2020-08-06 23:20:14 -0700 | [diff] [blame] | 766 | * @fentry: swap entry of this memory |
Yang Shi | e9f5987 | 2019-03-05 15:44:15 -0800 | [diff] [blame] | 767 | * @gfp_mask: memory allocation flags |
| 768 | * @vmf: fault information |
| 769 | * |
| 770 | * Returns the struct page for entry and addr, after queueing swapin. |
| 771 | * |
Shijie Luo | cb152a1 | 2021-05-06 18:05:51 -0700 | [diff] [blame] | 772 | * Primitive swap readahead code. We simply read in a few pages whose |
Yang Shi | e9f5987 | 2019-03-05 15:44:15 -0800 | [diff] [blame] | 773 | * virtual addresses are around the fault address in the same vma. |
| 774 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 775 | * Caller must hold read mmap_lock if vmf->vma is not NULL. |
Yang Shi | e9f5987 | 2019-03-05 15:44:15 -0800 | [diff] [blame] | 776 | * |
| 777 | */ |
Colin Ian King | f5c754d | 2018-04-05 16:25:05 -0700 | [diff] [blame] | 778 | static struct page *swap_vma_readahead(swp_entry_t fentry, gfp_t gfp_mask, |
| 779 | struct vm_fault *vmf) |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 780 | { |
| 781 | struct blk_plug plug; |
NeilBrown | 5169b84 | 2022-05-09 18:20:49 -0700 | [diff] [blame] | 782 | struct swap_iocb *splug = NULL; |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 783 | struct vm_area_struct *vma = vmf->vma; |
| 784 | struct page *page; |
| 785 | pte_t *pte, pentry; |
| 786 | swp_entry_t entry; |
| 787 | unsigned int i; |
| 788 | bool page_allocated; |
Miaohe Lin | e97af69 | 2020-12-14 19:06:01 -0800 | [diff] [blame] | 789 | struct vma_swap_readahead ra_info = { |
| 790 | .win = 1, |
| 791 | }; |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 792 | |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 793 | swap_ra_info(vmf, &ra_info); |
| 794 | if (ra_info.win == 1) |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 795 | goto skip; |
| 796 | |
| 797 | blk_start_plug(&plug); |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 798 | for (i = 0, pte = ra_info.ptes; i < ra_info.nr_pte; |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 799 | i++, pte++) { |
| 800 | pentry = *pte; |
Miaohe Lin | 92bafb2 | 2022-05-19 14:08:50 -0700 | [diff] [blame] | 801 | if (!is_swap_pte(pentry)) |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 802 | continue; |
| 803 | entry = pte_to_swp_entry(pentry); |
| 804 | if (unlikely(non_swap_entry(entry))) |
| 805 | continue; |
| 806 | page = __read_swap_cache_async(entry, gfp_mask, vma, |
| 807 | vmf->address, &page_allocated); |
| 808 | if (!page) |
| 809 | continue; |
| 810 | if (page_allocated) { |
NeilBrown | 5169b84 | 2022-05-09 18:20:49 -0700 | [diff] [blame] | 811 | swap_readpage(page, false, &splug); |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 812 | if (i != ra_info.offset) { |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 813 | SetPageReadahead(page); |
| 814 | count_vm_event(SWAP_RA); |
| 815 | } |
| 816 | } |
| 817 | put_page(page); |
| 818 | } |
| 819 | blk_finish_plug(&plug); |
NeilBrown | 5169b84 | 2022-05-09 18:20:49 -0700 | [diff] [blame] | 820 | swap_read_unplug(splug); |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 821 | lru_add_drain(); |
| 822 | skip: |
NeilBrown | 5169b84 | 2022-05-09 18:20:49 -0700 | [diff] [blame] | 823 | /* The page was likely read above, so no need for plugging here */ |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 824 | return read_swap_cache_async(fentry, gfp_mask, vma, vmf->address, |
NeilBrown | 5169b84 | 2022-05-09 18:20:49 -0700 | [diff] [blame] | 825 | ra_info.win == 1, NULL); |
Huang Ying | ec56017 | 2017-09-06 16:24:36 -0700 | [diff] [blame] | 826 | } |
Huang Ying | d9bfcfd | 2017-09-06 16:24:40 -0700 | [diff] [blame] | 827 | |
Minchan Kim | e9e9b7e | 2018-04-05 16:23:42 -0700 | [diff] [blame] | 828 | /** |
| 829 | * swapin_readahead - swap in pages in hope we need them soon |
| 830 | * @entry: swap entry of this memory |
| 831 | * @gfp_mask: memory allocation flags |
| 832 | * @vmf: fault information |
| 833 | * |
| 834 | * Returns the struct page for entry and addr, after queueing swapin. |
| 835 | * |
| 836 | * It's a main entry function for swap readahead. By the configuration, |
| 837 | * it will read ahead blocks by cluster-based(ie, physical disk based) |
| 838 | * or vma-based(ie, virtual address based on faulty address) readahead. |
| 839 | */ |
| 840 | struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, |
| 841 | struct vm_fault *vmf) |
| 842 | { |
| 843 | return swap_use_vma_readahead() ? |
| 844 | swap_vma_readahead(entry, gfp_mask, vmf) : |
| 845 | swap_cluster_readahead(entry, gfp_mask, vmf); |
| 846 | } |
| 847 | |
Huang Ying | d9bfcfd | 2017-09-06 16:24:40 -0700 | [diff] [blame] | 848 | #ifdef CONFIG_SYSFS |
| 849 | static ssize_t vma_ra_enabled_show(struct kobject *kobj, |
| 850 | struct kobj_attribute *attr, char *buf) |
| 851 | { |
Joe Perches | ae7a927 | 2020-12-14 19:14:42 -0800 | [diff] [blame] | 852 | return sysfs_emit(buf, "%s\n", |
| 853 | enable_vma_readahead ? "true" : "false"); |
Huang Ying | d9bfcfd | 2017-09-06 16:24:40 -0700 | [diff] [blame] | 854 | } |
| 855 | static ssize_t vma_ra_enabled_store(struct kobject *kobj, |
| 856 | struct kobj_attribute *attr, |
| 857 | const char *buf, size_t count) |
| 858 | { |
Jagdish Gediya | 717aeab | 2022-05-12 20:22:59 -0700 | [diff] [blame] | 859 | ssize_t ret; |
| 860 | |
| 861 | ret = kstrtobool(buf, &enable_vma_readahead); |
| 862 | if (ret) |
| 863 | return ret; |
Huang Ying | d9bfcfd | 2017-09-06 16:24:40 -0700 | [diff] [blame] | 864 | |
| 865 | return count; |
| 866 | } |
Miaohe Lin | 6106b93 | 2022-05-19 14:08:50 -0700 | [diff] [blame] | 867 | static struct kobj_attribute vma_ra_enabled_attr = __ATTR_RW(vma_ra_enabled); |
Huang Ying | d9bfcfd | 2017-09-06 16:24:40 -0700 | [diff] [blame] | 868 | |
Huang Ying | d9bfcfd | 2017-09-06 16:24:40 -0700 | [diff] [blame] | 869 | static struct attribute *swap_attrs[] = { |
| 870 | &vma_ra_enabled_attr.attr, |
Huang Ying | d9bfcfd | 2017-09-06 16:24:40 -0700 | [diff] [blame] | 871 | NULL, |
| 872 | }; |
| 873 | |
Rikard Falkeborn | e48333b | 2021-02-24 12:03:05 -0800 | [diff] [blame] | 874 | static const struct attribute_group swap_attr_group = { |
Huang Ying | d9bfcfd | 2017-09-06 16:24:40 -0700 | [diff] [blame] | 875 | .attrs = swap_attrs, |
| 876 | }; |
| 877 | |
| 878 | static int __init swap_init_sysfs(void) |
| 879 | { |
| 880 | int err; |
| 881 | struct kobject *swap_kobj; |
| 882 | |
| 883 | swap_kobj = kobject_create_and_add("swap", mm_kobj); |
| 884 | if (!swap_kobj) { |
| 885 | pr_err("failed to create swap kobject\n"); |
| 886 | return -ENOMEM; |
| 887 | } |
| 888 | err = sysfs_create_group(swap_kobj, &swap_attr_group); |
| 889 | if (err) { |
| 890 | pr_err("failed to register swap group\n"); |
| 891 | goto delete_obj; |
| 892 | } |
| 893 | return 0; |
| 894 | |
| 895 | delete_obj: |
| 896 | kobject_put(swap_kobj); |
| 897 | return err; |
| 898 | } |
| 899 | subsys_initcall(swap_init_sysfs); |
| 900 | #endif |