Commit | Line | Data |
---|---|---|
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 | 31 | static 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 |
39 | struct address_space *swapper_spaces[MAX_SWAPFILES] __read_mostly; |
40 | static unsigned int nr_swapper_spaces[MAX_SWAPFILES] __read_mostly; | |
f5c754d6 | 41 | static 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 |
61 | static atomic_t swapin_readahead_hits = ATOMIC_INIT(4); |
62 | ||
1da177e4 LT |
63 | void 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 |
70 | void *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 | 86 | int 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); | |
3d2c9087 | 103 | folio->swap = entry; |
31a56396 | 104 | |
8d93b41c MW |
105 | do { |
106 | xas_lock_irq(&xas); | |
107 | xas_create_range(&xas); | |
108 | if (xas_error(&xas)) | |
109 | goto unlock; | |
110 | for (i = 0; i < nr; i++) { | |
a4c366f0 | 111 | VM_BUG_ON_FOLIO(xas.xa_index != idx + i, folio); |
3852f676 JK |
112 | old = xas_load(&xas); |
113 | if (xa_is_value(old)) { | |
3852f676 JK |
114 | if (shadowp) |
115 | *shadowp = old; | |
116 | } | |
a4c366f0 | 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 |
123 | unlock: |
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 | 139 | void __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); |
4e17ec25 | 157 | xas_next(&xas); |
38d8b4e6 | 158 | } |
3d2c9087 | 159 | folio->swap.val = 0; |
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 | 176 | bool 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 | 222 | fail: |
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 | 233 | void delete_from_swap_cache(struct folio *folio) |
1da177e4 | 234 | { |
3d2c9087 | 235 | swp_entry_t entry = folio->swap; |
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 |
246 | void 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 | 284 | void 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 | */ |
299 | void 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 | 310 | void 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 |
318 | static 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 |
331 | struct 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 |
384 | struct 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 |
412 | struct 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; |
504 | got_page: | |
505 | put_swap_device(si); | |
506 | return page; | |
1da177e4 | 507 | |
4c6355b2 | 508 | fail_unlock: |
4081f744 | 509 | put_swap_folio(folio, entry); |
a0d3374b MWO |
510 | folio_unlock(folio); |
511 | folio_put(folio); | |
46a774d3 HY |
512 | fail_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 | */ |
527 | struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, | |
5169b844 | 528 | struct vm_area_struct *vma, |
b243dcbf | 529 | unsigned long addr, struct swap_iocb **plug) |
5b999aad DS |
530 | { |
531 | bool page_was_allocated; | |
532 | struct page *retpage = __read_swap_cache_async(entry, gfp_mask, | |
533 | vma, addr, &page_was_allocated); | |
534 | ||
535 | if (page_was_allocated) | |
b243dcbf | 536 | swap_readpage(retpage, false, plug); |
5b999aad DS |
537 | |
538 | return retpage; | |
539 | } | |
540 | ||
ec560175 HY |
541 | static unsigned int __swapin_nr_pages(unsigned long prev_offset, |
542 | unsigned long offset, | |
543 | int hits, | |
544 | int max_pages, | |
545 | int prev_win) | |
579f8290 | 546 | { |
ec560175 | 547 | unsigned int pages, last_ra; |
579f8290 SL |
548 | |
549 | /* | |
550 | * This heuristic has been found to work well on both sequential and | |
551 | * random loads, swapping to hard disk or to SSD: please don't ask | |
552 | * what the "+ 2" means, it just happens to work well, that's all. | |
553 | */ | |
ec560175 | 554 | pages = hits + 2; |
579f8290 SL |
555 | if (pages == 2) { |
556 | /* | |
557 | * We can have no readahead hits to judge by: but must not get | |
558 | * stuck here forever, so check for an adjacent offset instead | |
559 | * (and don't even bother to check whether swap type is same). | |
560 | */ | |
561 | if (offset != prev_offset + 1 && offset != prev_offset - 1) | |
562 | pages = 1; | |
579f8290 SL |
563 | } else { |
564 | unsigned int roundup = 4; | |
565 | while (roundup < pages) | |
566 | roundup <<= 1; | |
567 | pages = roundup; | |
568 | } | |
569 | ||
570 | if (pages > max_pages) | |
571 | pages = max_pages; | |
572 | ||
573 | /* Don't shrink readahead too fast */ | |
ec560175 | 574 | last_ra = prev_win / 2; |
579f8290 SL |
575 | if (pages < last_ra) |
576 | pages = last_ra; | |
ec560175 HY |
577 | |
578 | return pages; | |
579 | } | |
580 | ||
581 | static unsigned long swapin_nr_pages(unsigned long offset) | |
582 | { | |
583 | static unsigned long prev_offset; | |
584 | unsigned int hits, pages, max_pages; | |
585 | static atomic_t last_readahead_pages; | |
586 | ||
587 | max_pages = 1 << READ_ONCE(page_cluster); | |
588 | if (max_pages <= 1) | |
589 | return 1; | |
590 | ||
591 | hits = atomic_xchg(&swapin_readahead_hits, 0); | |
d6c1f098 QC |
592 | pages = __swapin_nr_pages(READ_ONCE(prev_offset), offset, hits, |
593 | max_pages, | |
ec560175 HY |
594 | atomic_read(&last_readahead_pages)); |
595 | if (!hits) | |
d6c1f098 | 596 | WRITE_ONCE(prev_offset, offset); |
579f8290 SL |
597 | atomic_set(&last_readahead_pages, pages); |
598 | ||
599 | return pages; | |
600 | } | |
601 | ||
46017e95 | 602 | /** |
e9e9b7ec | 603 | * swap_cluster_readahead - swap in pages in hope we need them soon |
46017e95 | 604 | * @entry: swap entry of this memory |
7682486b | 605 | * @gfp_mask: memory allocation flags |
e9e9b7ec | 606 | * @vmf: fault information |
46017e95 HD |
607 | * |
608 | * Returns the struct page for entry and addr, after queueing swapin. | |
609 | * | |
610 | * Primitive swap readahead code. We simply read an aligned block of | |
611 | * (1 << page_cluster) entries in the swap area. This method is chosen | |
612 | * because it doesn't cost us any seek time. We also make sure to queue | |
613 | * the 'original' request together with the readahead ones... | |
614 | * | |
615 | * This has been extended to use the NUMA policies from the mm triggering | |
616 | * the readahead. | |
617 | * | |
c1e8d7c6 | 618 | * Caller must hold read mmap_lock if vmf->vma is not NULL. |
46017e95 | 619 | */ |
e9e9b7ec MK |
620 | struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, |
621 | struct vm_fault *vmf) | |
46017e95 | 622 | { |
46017e95 | 623 | struct page *page; |
579f8290 SL |
624 | unsigned long entry_offset = swp_offset(entry); |
625 | unsigned long offset = entry_offset; | |
67f96aa2 | 626 | unsigned long start_offset, end_offset; |
579f8290 | 627 | unsigned long mask; |
e9a6effa | 628 | struct swap_info_struct *si = swp_swap_info(entry); |
3fb5c298 | 629 | struct blk_plug plug; |
5169b844 | 630 | struct swap_iocb *splug = NULL; |
b243dcbf | 631 | bool page_allocated; |
e9e9b7ec MK |
632 | struct vm_area_struct *vma = vmf->vma; |
633 | unsigned long addr = vmf->address; | |
46017e95 | 634 | |
579f8290 SL |
635 | mask = swapin_nr_pages(offset) - 1; |
636 | if (!mask) | |
637 | goto skip; | |
638 | ||
67f96aa2 RR |
639 | /* Read a page_cluster sized and aligned cluster around offset. */ |
640 | start_offset = offset & ~mask; | |
641 | end_offset = offset | mask; | |
642 | if (!start_offset) /* First page is swap header. */ | |
643 | start_offset++; | |
e9a6effa HY |
644 | if (end_offset >= si->max) |
645 | end_offset = si->max - 1; | |
67f96aa2 | 646 | |
3fb5c298 | 647 | blk_start_plug(&plug); |
67f96aa2 | 648 | for (offset = start_offset; offset <= end_offset ; offset++) { |
46017e95 | 649 | /* Ok, do the async read-ahead now */ |
c4fa6309 HY |
650 | page = __read_swap_cache_async( |
651 | swp_entry(swp_type(entry), offset), | |
652 | gfp_mask, vma, addr, &page_allocated); | |
46017e95 | 653 | if (!page) |
67f96aa2 | 654 | continue; |
c4fa6309 | 655 | if (page_allocated) { |
5169b844 | 656 | swap_readpage(page, false, &splug); |
eaf649eb | 657 | if (offset != entry_offset) { |
c4fa6309 HY |
658 | SetPageReadahead(page); |
659 | count_vm_event(SWAP_RA); | |
660 | } | |
cbc65df2 | 661 | } |
09cbfeaf | 662 | put_page(page); |
46017e95 | 663 | } |
3fb5c298 | 664 | blk_finish_plug(&plug); |
5169b844 | 665 | swap_read_unplug(splug); |
3fb5c298 | 666 | |
46017e95 | 667 | lru_add_drain(); /* Push any new pages onto the LRU now */ |
579f8290 | 668 | skip: |
5169b844 | 669 | /* The page was likely read above, so no need for plugging here */ |
b243dcbf | 670 | return read_swap_cache_async(entry, gfp_mask, vma, addr, NULL); |
46017e95 | 671 | } |
4b3ef9da HY |
672 | |
673 | int init_swap_address_space(unsigned int type, unsigned long nr_pages) | |
674 | { | |
675 | struct address_space *spaces, *space; | |
676 | unsigned int i, nr; | |
677 | ||
678 | nr = DIV_ROUND_UP(nr_pages, SWAP_ADDRESS_SPACE_PAGES); | |
778e1cdd | 679 | spaces = kvcalloc(nr, sizeof(struct address_space), GFP_KERNEL); |
4b3ef9da HY |
680 | if (!spaces) |
681 | return -ENOMEM; | |
682 | for (i = 0; i < nr; i++) { | |
683 | space = spaces + i; | |
a2833486 | 684 | xa_init_flags(&space->i_pages, XA_FLAGS_LOCK_IRQ); |
4b3ef9da HY |
685 | atomic_set(&space->i_mmap_writable, 0); |
686 | space->a_ops = &swap_aops; | |
687 | /* swap cache doesn't use writeback related tags */ | |
688 | mapping_set_no_writeback_tags(space); | |
4b3ef9da HY |
689 | } |
690 | nr_swapper_spaces[type] = nr; | |
054f1d1f | 691 | swapper_spaces[type] = spaces; |
4b3ef9da HY |
692 | |
693 | return 0; | |
694 | } | |
695 | ||
696 | void exit_swap_address_space(unsigned int type) | |
697 | { | |
eea4a501 HY |
698 | int i; |
699 | struct address_space *spaces = swapper_spaces[type]; | |
700 | ||
701 | for (i = 0; i < nr_swapper_spaces[type]; i++) | |
702 | VM_WARN_ON_ONCE(!mapping_empty(&spaces[i])); | |
703 | kvfree(spaces); | |
4b3ef9da | 704 | nr_swapper_spaces[type] = 0; |
054f1d1f | 705 | swapper_spaces[type] = NULL; |
4b3ef9da | 706 | } |
ec560175 | 707 | |
4f8fcf4c HD |
708 | #define SWAP_RA_ORDER_CEILING 5 |
709 | ||
710 | struct vma_swap_readahead { | |
711 | unsigned short win; | |
712 | unsigned short offset; | |
713 | unsigned short nr_pte; | |
714 | }; | |
715 | ||
eaf649eb | 716 | static void swap_ra_info(struct vm_fault *vmf, |
16ba391e | 717 | struct vma_swap_readahead *ra_info) |
ec560175 HY |
718 | { |
719 | struct vm_area_struct *vma = vmf->vma; | |
eaf649eb | 720 | unsigned long ra_val; |
16ba391e | 721 | unsigned long faddr, pfn, fpfn, lpfn, rpfn; |
ec560175 | 722 | unsigned long start, end; |
16ba391e | 723 | unsigned int max_win, hits, prev_win, win; |
ec560175 | 724 | |
61b63972 HY |
725 | max_win = 1 << min_t(unsigned int, READ_ONCE(page_cluster), |
726 | SWAP_RA_ORDER_CEILING); | |
727 | if (max_win == 1) { | |
eaf649eb MK |
728 | ra_info->win = 1; |
729 | return; | |
61b63972 HY |
730 | } |
731 | ||
ec560175 | 732 | faddr = vmf->address; |
ec560175 | 733 | fpfn = PFN_DOWN(faddr); |
eaf649eb MK |
734 | ra_val = GET_SWAP_RA_VAL(vma); |
735 | pfn = PFN_DOWN(SWAP_RA_ADDR(ra_val)); | |
736 | prev_win = SWAP_RA_WIN(ra_val); | |
737 | hits = SWAP_RA_HITS(ra_val); | |
738 | ra_info->win = win = __swapin_nr_pages(pfn, fpfn, hits, | |
ec560175 HY |
739 | max_win, prev_win); |
740 | atomic_long_set(&vma->swap_readahead_info, | |
741 | SWAP_RA_VAL(faddr, win, 0)); | |
18ad72f5 | 742 | if (win == 1) |
eaf649eb | 743 | return; |
ec560175 | 744 | |
16ba391e KS |
745 | if (fpfn == pfn + 1) { |
746 | lpfn = fpfn; | |
747 | rpfn = fpfn + win; | |
748 | } else if (pfn == fpfn + 1) { | |
749 | lpfn = fpfn - win + 1; | |
750 | rpfn = fpfn + 1; | |
751 | } else { | |
752 | unsigned int left = (win - 1) / 2; | |
753 | ||
754 | lpfn = fpfn - left; | |
755 | rpfn = fpfn + win - left; | |
ec560175 | 756 | } |
16ba391e KS |
757 | start = max3(lpfn, PFN_DOWN(vma->vm_start), |
758 | PFN_DOWN(faddr & PMD_MASK)); | |
759 | end = min3(rpfn, PFN_DOWN(vma->vm_end), | |
760 | PFN_DOWN((faddr & PMD_MASK) + PMD_SIZE)); | |
761 | ||
eaf649eb MK |
762 | ra_info->nr_pte = end - start; |
763 | ra_info->offset = fpfn - start; | |
ec560175 HY |
764 | } |
765 | ||
e9f59873 YS |
766 | /** |
767 | * swap_vma_readahead - swap in pages in hope we need them soon | |
27ec4878 | 768 | * @fentry: swap entry of this memory |
e9f59873 YS |
769 | * @gfp_mask: memory allocation flags |
770 | * @vmf: fault information | |
771 | * | |
772 | * Returns the struct page for entry and addr, after queueing swapin. | |
773 | * | |
cb152a1a | 774 | * Primitive swap readahead code. We simply read in a few pages whose |
e9f59873 YS |
775 | * virtual addresses are around the fault address in the same vma. |
776 | * | |
c1e8d7c6 | 777 | * Caller must hold read mmap_lock if vmf->vma is not NULL. |
e9f59873 YS |
778 | * |
779 | */ | |
f5c754d6 CIK |
780 | static struct page *swap_vma_readahead(swp_entry_t fentry, gfp_t gfp_mask, |
781 | struct vm_fault *vmf) | |
ec560175 HY |
782 | { |
783 | struct blk_plug plug; | |
5169b844 | 784 | struct swap_iocb *splug = NULL; |
ec560175 HY |
785 | struct vm_area_struct *vma = vmf->vma; |
786 | struct page *page; | |
4f8fcf4c HD |
787 | pte_t *pte = NULL, pentry; |
788 | unsigned long addr; | |
ec560175 HY |
789 | swp_entry_t entry; |
790 | unsigned int i; | |
791 | bool page_allocated; | |
e97af699 ML |
792 | struct vma_swap_readahead ra_info = { |
793 | .win = 1, | |
794 | }; | |
ec560175 | 795 | |
eaf649eb MK |
796 | swap_ra_info(vmf, &ra_info); |
797 | if (ra_info.win == 1) | |
ec560175 HY |
798 | goto skip; |
799 | ||
4f8fcf4c HD |
800 | addr = vmf->address - (ra_info.offset * PAGE_SIZE); |
801 | ||
ec560175 | 802 | blk_start_plug(&plug); |
4f8fcf4c HD |
803 | for (i = 0; i < ra_info.nr_pte; i++, addr += PAGE_SIZE) { |
804 | if (!pte++) { | |
805 | pte = pte_offset_map(vmf->pmd, addr); | |
806 | if (!pte) | |
807 | break; | |
808 | } | |
809 | pentry = ptep_get_lockless(pte); | |
92bafb20 | 810 | if (!is_swap_pte(pentry)) |
ec560175 HY |
811 | continue; |
812 | entry = pte_to_swp_entry(pentry); | |
813 | if (unlikely(non_swap_entry(entry))) | |
814 | continue; | |
4f8fcf4c HD |
815 | pte_unmap(pte); |
816 | pte = NULL; | |
ec560175 | 817 | page = __read_swap_cache_async(entry, gfp_mask, vma, |
4f8fcf4c | 818 | addr, &page_allocated); |
ec560175 HY |
819 | if (!page) |
820 | continue; | |
821 | if (page_allocated) { | |
5169b844 | 822 | swap_readpage(page, false, &splug); |
eaf649eb | 823 | if (i != ra_info.offset) { |
ec560175 HY |
824 | SetPageReadahead(page); |
825 | count_vm_event(SWAP_RA); | |
826 | } | |
827 | } | |
828 | put_page(page); | |
829 | } | |
4f8fcf4c HD |
830 | if (pte) |
831 | pte_unmap(pte); | |
ec560175 | 832 | blk_finish_plug(&plug); |
5169b844 | 833 | swap_read_unplug(splug); |
ec560175 HY |
834 | lru_add_drain(); |
835 | skip: | |
5169b844 | 836 | /* The page was likely read above, so no need for plugging here */ |
ec560175 | 837 | return read_swap_cache_async(fentry, gfp_mask, vma, vmf->address, |
b243dcbf | 838 | NULL); |
ec560175 | 839 | } |
d9bfcfdc | 840 | |
e9e9b7ec MK |
841 | /** |
842 | * swapin_readahead - swap in pages in hope we need them soon | |
843 | * @entry: swap entry of this memory | |
844 | * @gfp_mask: memory allocation flags | |
845 | * @vmf: fault information | |
846 | * | |
847 | * Returns the struct page for entry and addr, after queueing swapin. | |
848 | * | |
849 | * It's a main entry function for swap readahead. By the configuration, | |
850 | * it will read ahead blocks by cluster-based(ie, physical disk based) | |
851 | * or vma-based(ie, virtual address based on faulty address) readahead. | |
852 | */ | |
853 | struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, | |
854 | struct vm_fault *vmf) | |
855 | { | |
856 | return swap_use_vma_readahead() ? | |
857 | swap_vma_readahead(entry, gfp_mask, vmf) : | |
858 | swap_cluster_readahead(entry, gfp_mask, vmf); | |
859 | } | |
860 | ||
d9bfcfdc HY |
861 | #ifdef CONFIG_SYSFS |
862 | static ssize_t vma_ra_enabled_show(struct kobject *kobj, | |
863 | struct kobj_attribute *attr, char *buf) | |
864 | { | |
ae7a927d JP |
865 | return sysfs_emit(buf, "%s\n", |
866 | enable_vma_readahead ? "true" : "false"); | |
d9bfcfdc HY |
867 | } |
868 | static ssize_t vma_ra_enabled_store(struct kobject *kobj, | |
869 | struct kobj_attribute *attr, | |
870 | const char *buf, size_t count) | |
871 | { | |
717aeab4 JG |
872 | ssize_t ret; |
873 | ||
874 | ret = kstrtobool(buf, &enable_vma_readahead); | |
875 | if (ret) | |
876 | return ret; | |
d9bfcfdc HY |
877 | |
878 | return count; | |
879 | } | |
6106b93e | 880 | static struct kobj_attribute vma_ra_enabled_attr = __ATTR_RW(vma_ra_enabled); |
d9bfcfdc | 881 | |
d9bfcfdc HY |
882 | static struct attribute *swap_attrs[] = { |
883 | &vma_ra_enabled_attr.attr, | |
d9bfcfdc HY |
884 | NULL, |
885 | }; | |
886 | ||
e48333b6 | 887 | static const struct attribute_group swap_attr_group = { |
d9bfcfdc HY |
888 | .attrs = swap_attrs, |
889 | }; | |
890 | ||
891 | static int __init swap_init_sysfs(void) | |
892 | { | |
893 | int err; | |
894 | struct kobject *swap_kobj; | |
895 | ||
896 | swap_kobj = kobject_create_and_add("swap", mm_kobj); | |
897 | if (!swap_kobj) { | |
898 | pr_err("failed to create swap kobject\n"); | |
899 | return -ENOMEM; | |
900 | } | |
901 | err = sysfs_create_group(swap_kobj, &swap_attr_group); | |
902 | if (err) { | |
903 | pr_err("failed to register swap group\n"); | |
904 | goto delete_obj; | |
905 | } | |
906 | return 0; | |
907 | ||
908 | delete_obj: | |
909 | kobject_put(swap_kobj); | |
910 | return err; | |
911 | } | |
912 | subsys_initcall(swap_init_sysfs); | |
913 | #endif |