blob: d4f3b2fec061a0831f08e57d1875e008529ac539 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/swap_state.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 * Swap reorganised 29.12.95, Stephen Tweedie
6 *
7 * Rewritten to use page cache, (C) 1998 Stephen Tweedie
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel_stat.h>
12#include <linux/swap.h>
Hugh Dickins46017e92008-02-04 22:28:41 -080013#include <linux/swapops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/backing-dev.h>
Christian Ehrhardt3fb5c292012-07-31 16:41:44 -070017#include <linux/blkdev.h>
Hugh Dickinsc484d412006-01-06 00:10:55 -080018#include <linux/pagevec.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080019#include <linux/migrate.h>
Zhenhua Huang05b7b7c2018-10-12 17:51:32 +080020#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/pgtable.h>
23
24/*
25 * swapper_space is a fiction, retained to simplify the path through
Jens Axboe7eaceac2011-03-10 08:52:07 +010026 * vmscan's shrink_page_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070028static const struct address_space_operations swap_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 .writepage = swap_writepage,
Mel Gorman62c230b2012-07-31 16:44:55 -070030 .set_page_dirty = swap_set_page_dirty,
Andrew Morton1c939232014-10-09 15:27:59 -070031#ifdef CONFIG_MIGRATION
Christoph Lametere965f962006-02-01 03:05:41 -080032 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -070033#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
Shaohua Li33806f02013-02-22 16:34:37 -080036struct address_space swapper_spaces[MAX_SWAPFILES] = {
37 [0 ... MAX_SWAPFILES - 1] = {
38 .page_tree = RADIX_TREE_INIT(GFP_ATOMIC|__GFP_NOWARN),
David Herrmann4bb5f5d2014-08-08 14:25:25 -070039 .i_mmap_writable = ATOMIC_INIT(0),
Shaohua Li33806f02013-02-22 16:34:37 -080040 .a_ops = &swap_aops,
Huang Ying371a0962016-10-07 16:59:30 -070041 /* swap cache doesn't use writeback related tags */
42 .flags = 1 << AS_NO_WRITEBACK_TAGS,
Shaohua Li33806f02013-02-22 16:34:37 -080043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define INC_CACHE_INFO(x) do { swap_cache_info.x++; } while (0)
47
48static struct {
49 unsigned long add_total;
50 unsigned long del_total;
51 unsigned long find_success;
52 unsigned long find_total;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053} swap_cache_info;
54
Shaohua Li33806f02013-02-22 16:34:37 -080055unsigned long total_swapcache_pages(void)
56{
57 int i;
58 unsigned long ret = 0;
59
60 for (i = 0; i < MAX_SWAPFILES; i++)
61 ret += swapper_spaces[i].nrpages;
62 return ret;
63}
64
Shaohua Li579f8292014-02-06 12:04:21 -080065static atomic_t swapin_readahead_hits = ATOMIC_INIT(4);
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067void show_swap_cache_info(void)
68{
Shaohua Li33806f02013-02-22 16:34:37 -080069 printk("%lu pages in swap cache\n", total_swapcache_pages());
Johannes Weiner2c97b7f2008-07-25 19:46:01 -070070 printk("Swap cache stats: add %lu, delete %lu, find %lu/%lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 swap_cache_info.add_total, swap_cache_info.del_total,
Hugh Dickinsbb63be02008-02-04 22:28:49 -080072 swap_cache_info.find_success, swap_cache_info.find_total);
Shaohua Liec8acf22013-02-22 16:34:38 -080073 printk("Free swap = %ldkB\n",
74 get_nr_swap_pages() << (PAGE_SHIFT - 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10));
76}
77
78/*
Daisuke Nishimura31a56392009-09-21 17:02:50 -070079 * __add_to_swap_cache resembles add_to_page_cache_locked on swapper_space,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * but sets SwapCache flag and private instead of mapping and index.
81 */
Seth Jennings2f772e62013-04-29 15:08:34 -070082int __add_to_swap_cache(struct page *page, swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 int error;
Shaohua Li33806f02013-02-22 16:34:37 -080085 struct address_space *address_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Sasha Levin309381fea2014-01-23 15:52:54 -080087 VM_BUG_ON_PAGE(!PageLocked(page), page);
88 VM_BUG_ON_PAGE(PageSwapCache(page), page);
89 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Hugh Dickins51726b12009-01-06 14:39:25 -080090
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030091 get_page(page);
Daisuke Nishimura31a56392009-09-21 17:02:50 -070092 SetPageSwapCache(page);
93 set_page_private(page, entry.val);
94
Shaohua Li33806f02013-02-22 16:34:37 -080095 address_space = swap_address_space(entry);
96 spin_lock_irq(&address_space->tree_lock);
97 error = radix_tree_insert(&address_space->page_tree,
Huang Yingf6ab1f72016-10-07 17:00:21 -070098 swp_offset(entry), page);
Daisuke Nishimura31a56392009-09-21 17:02:50 -070099 if (likely(!error)) {
Shaohua Li33806f02013-02-22 16:34:37 -0800100 address_space->nrpages++;
Mel Gorman11fb9982016-07-28 15:46:20 -0700101 __inc_node_page_state(page, NR_FILE_PAGES);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700102 INC_CACHE_INFO(add_total);
103 }
Shaohua Li33806f02013-02-22 16:34:37 -0800104 spin_unlock_irq(&address_space->tree_lock);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700105
106 if (unlikely(error)) {
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700107 /*
108 * Only the context which have set SWAP_HAS_CACHE flag
109 * would call add_to_swap_cache().
110 * So add_to_swap_cache() doesn't returns -EEXIST.
111 */
112 VM_BUG_ON(error == -EEXIST);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700113 set_page_private(page, 0UL);
114 ClearPageSwapCache(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300115 put_page(page);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700116 }
117
118 return error;
119}
120
121
122int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp_mask)
123{
124 int error;
125
Jan Kara5e4c0d972013-09-11 14:26:05 -0700126 error = radix_tree_maybe_preload(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (!error) {
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700128 error = __add_to_swap_cache(page, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 radix_tree_preload_end();
Hugh Dickinsfa1de902008-02-07 00:14:13 -0800130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return error;
132}
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/*
135 * This must be called only on pages that have
136 * been verified to be in the swap cache.
137 */
138void __delete_from_swap_cache(struct page *page)
139{
Shaohua Li33806f02013-02-22 16:34:37 -0800140 swp_entry_t entry;
141 struct address_space *address_space;
142
Sasha Levin309381fea2014-01-23 15:52:54 -0800143 VM_BUG_ON_PAGE(!PageLocked(page), page);
144 VM_BUG_ON_PAGE(!PageSwapCache(page), page);
145 VM_BUG_ON_PAGE(PageWriteback(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Shaohua Li33806f02013-02-22 16:34:37 -0800147 entry.val = page_private(page);
148 address_space = swap_address_space(entry);
Huang Yingf6ab1f72016-10-07 17:00:21 -0700149 radix_tree_delete(&address_space->page_tree, swp_offset(entry));
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700150 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 ClearPageSwapCache(page);
Shaohua Li33806f02013-02-22 16:34:37 -0800152 address_space->nrpages--;
Mel Gorman11fb9982016-07-28 15:46:20 -0700153 __dec_node_page_state(page, NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 INC_CACHE_INFO(del_total);
155}
156
157/**
158 * add_to_swap - allocate swap space for a page
159 * @page: page we want to move to swap
160 *
161 * Allocate swap space for the page and add the page to the
162 * swap cache. Caller needs to hold the page lock.
163 */
Shaohua Li5bc7b8a2013-04-29 15:08:36 -0700164int add_to_swap(struct page *page, struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 swp_entry_t entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 int err;
168
Sasha Levin309381fea2014-01-23 15:52:54 -0800169 VM_BUG_ON_PAGE(!PageLocked(page), page);
170 VM_BUG_ON_PAGE(!PageUptodate(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700172 entry = get_swap_page();
173 if (!entry.val)
174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Vladimir Davydov37e84352016-01-20 15:02:56 -0800176 if (mem_cgroup_try_charge_swap(page, entry)) {
177 swapcache_free(entry);
178 return 0;
179 }
180
Andrea Arcangeli3f04f622011-01-13 15:46:47 -0800181 if (unlikely(PageTransHuge(page)))
Shaohua Li5bc7b8a2013-04-29 15:08:36 -0700182 if (unlikely(split_huge_page_to_list(page, list))) {
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700183 swapcache_free(entry);
Andrea Arcangeli3f04f622011-01-13 15:46:47 -0800184 return 0;
185 }
186
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700187 /*
188 * Radix-tree node allocations from PF_MEMALLOC contexts could
189 * completely exhaust the page allocator. __GFP_NOMEMALLOC
190 * stops emergency reserves from being allocated.
191 *
192 * TODO: this could cause a theoretical memory reclaim
193 * deadlock in the swap out path.
194 */
195 /*
Minchan Kim854e9ed2016-01-15 16:54:53 -0800196 * Add it to the swap cache.
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700197 */
198 err = add_to_swap_cache(page, entry,
199 __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Minchan Kim854e9ed2016-01-15 16:54:53 -0800201 if (!err) {
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700202 return 1;
203 } else { /* -ENOMEM radix-tree allocation failure */
204 /*
205 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
206 * clear SWAP_HAS_CACHE flag.
207 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700208 swapcache_free(entry);
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211}
212
213/*
214 * This must be called only on pages that have
215 * been verified to be in the swap cache and locked.
216 * It will never put the page into the free list,
217 * the caller has a reference on the page.
218 */
219void delete_from_swap_cache(struct page *page)
220{
221 swp_entry_t entry;
Shaohua Li33806f02013-02-22 16:34:37 -0800222 struct address_space *address_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700224 entry.val = page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Shaohua Li33806f02013-02-22 16:34:37 -0800226 address_space = swap_address_space(entry);
227 spin_lock_irq(&address_space->tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 __delete_from_swap_cache(page);
Shaohua Li33806f02013-02-22 16:34:37 -0800229 spin_unlock_irq(&address_space->tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700231 swapcache_free(entry);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300232 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/*
236 * If we are the only user, then try to free up the swap cache.
237 *
238 * Its ok to check for PageSwapCache without the page lock
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800239 * here because we are going to recheck again inside
240 * try_to_free_swap() _with_ the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * - Marcelo
242 */
243static inline void free_swap_cache(struct page *page)
244{
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800245 if (PageSwapCache(page) && !page_mapped(page) && trylock_page(page)) {
246 try_to_free_swap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 unlock_page(page);
248 }
249}
250
251/*
252 * Perform a free_page(), also freeing any swap cache associated with
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700253 * this page if it is the last user of the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
255void free_page_and_swap_cache(struct page *page)
256{
257 free_swap_cache(page);
Aaron Lu6fcb52a2016-10-07 17:00:08 -0700258 if (!is_huge_zero_page(page))
Gerald Schaefer770a5372016-06-08 15:33:50 -0700259 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262/*
263 * Passed an array of pages, drop them all from swapcache and then release
264 * them. They are removed from the LRU and freed if this is their last use.
265 */
266void free_pages_and_swap_cache(struct page **pages, int nr)
267{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct page **pagep = pages;
Michal Hockoaabfb572014-10-09 15:28:52 -0700269 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 lru_add_drain();
Michal Hockoaabfb572014-10-09 15:28:52 -0700272 for (i = 0; i < nr; i++)
273 free_swap_cache(pagep[i]);
274 release_pages(pagep, nr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277/*
278 * Lookup a swap entry in the swap cache. A found page will be returned
279 * unlocked and with its refcount incremented - we rely on the kernel
280 * lock getting page table operations atomic even if we drop the page
281 * lock before returning.
282 */
283struct page * lookup_swap_cache(swp_entry_t entry)
284{
285 struct page *page;
286
Huang Yingf6ab1f72016-10-07 17:00:21 -0700287 page = find_get_page(swap_address_space(entry), swp_offset(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Shaohua Li579f8292014-02-06 12:04:21 -0800289 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 INC_CACHE_INFO(find_success);
Shaohua Li579f8292014-02-06 12:04:21 -0800291 if (TestClearPageReadahead(page))
292 atomic_inc(&swapin_readahead_hits);
293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 INC_CACHE_INFO(find_total);
296 return page;
297}
298
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700299struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
300 struct vm_area_struct *vma, unsigned long addr,
301 bool *new_page_allocated)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct page *found_page, *new_page = NULL;
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700304 struct address_space *swapper_space = swap_address_space(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 int err;
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700306 *new_page_allocated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 do {
309 /*
310 * First check the swap cache. Since this is normally
311 * called after lookup_swap_cache() failed, re-calling
312 * that would confuse statistics.
313 */
Huang Yingf6ab1f72016-10-07 17:00:21 -0700314 found_page = find_get_page(swapper_space, swp_offset(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (found_page)
316 break;
317
318 /*
319 * Get a new page to read into from swap.
320 */
321 if (!new_page) {
Hugh Dickins02098fe2008-02-04 22:28:42 -0800322 new_page = alloc_page_vma(gfp_mask, vma, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (!new_page)
324 break; /* Out of memory */
325 }
326
327 /*
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700328 * call radix_tree_preload() while we can wait.
329 */
Jan Kara5e4c0d972013-09-11 14:26:05 -0700330 err = radix_tree_maybe_preload(gfp_mask & GFP_KERNEL);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700331 if (err)
332 break;
333
334 /*
Hugh Dickinsf0009442008-02-04 22:28:49 -0800335 * Swap entry may have been freed since our caller observed it.
336 */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700337 err = swapcache_prepare(entry);
Rafael Aquinicbab0e42013-06-12 14:04:49 -0700338 if (err == -EEXIST) {
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700339 radix_tree_preload_end();
Rafael Aquinicbab0e42013-06-12 14:04:49 -0700340 /*
341 * We might race against get_swap_page() and stumble
342 * across a SWAP_HAS_CACHE swap_map entry whose page
343 * has not been brought into the swapcache yet, while
344 * the other end is scheduled away waiting on discard
345 * I/O completion at scan_swap_map().
346 *
347 * In order to avoid turning this transitory state
348 * into a permanent loop around this -EEXIST case
349 * if !CONFIG_PREEMPT and the I/O completion happens
350 * to be waiting on the CPU waitqueue where we are now
351 * busy looping, we just conditionally invoke the
352 * scheduler here, if there are some more important
353 * tasks to run.
Zhenhua Huang05b7b7c2018-10-12 17:51:32 +0800354 *
355 * cond_resched() may not work if the process is RT.
356 * We need a usleep_range() give up CPU to another task.
Rafael Aquinicbab0e42013-06-12 14:04:49 -0700357 */
Zhenhua Huang05b7b7c2018-10-12 17:51:32 +0800358 usleep_range(500, 1000);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700359 continue;
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700360 }
361 if (err) { /* swp entry is obsolete ? */
362 radix_tree_preload_end();
Hugh Dickinsf0009442008-02-04 22:28:49 -0800363 break;
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700364 }
Hugh Dickinsf0009442008-02-04 22:28:49 -0800365
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700366 /* May fail (-ENOMEM) if radix-tree node allocation failed. */
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800367 __SetPageLocked(new_page);
Hugh Dickinsfa9949d2016-05-19 17:12:41 -0700368 __SetPageSwapBacked(new_page);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700369 err = __add_to_swap_cache(new_page, entry);
Nick Piggin529ae9a2008-08-02 12:01:03 +0200370 if (likely(!err)) {
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700371 radix_tree_preload_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /*
373 * Initiate read into locked page and return.
374 */
Johannes Weinera2383322018-10-26 15:06:04 -0700375 SetPageWorkingset(new_page);
Rik van Rielc5fdae42008-10-18 20:26:36 -0700376 lru_cache_add_anon(new_page);
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700377 *new_page_allocated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return new_page;
379 }
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700380 radix_tree_preload_end();
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800381 __ClearPageLocked(new_page);
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700382 /*
383 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
384 * clear SWAP_HAS_CACHE flag.
385 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700386 swapcache_free(entry);
Hugh Dickinsf0009442008-02-04 22:28:49 -0800387 } while (err != -ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if (new_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300390 put_page(new_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return found_page;
392}
Hugh Dickins46017e92008-02-04 22:28:41 -0800393
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700394/*
395 * Locate a page of swap in physical memory, reserving swap cache space
396 * and reading the disk if it is not already cached.
397 * A failure return means that either the page allocation failed or that
398 * the swap entry is no longer in use.
399 */
400struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
401 struct vm_area_struct *vma, unsigned long addr)
402{
403 bool page_was_allocated;
404 struct page *retpage = __read_swap_cache_async(entry, gfp_mask,
405 vma, addr, &page_was_allocated);
406
407 if (page_was_allocated)
408 swap_readpage(retpage);
409
410 return retpage;
411}
412
Shaohua Li579f8292014-02-06 12:04:21 -0800413static unsigned long swapin_nr_pages(unsigned long offset)
414{
415 static unsigned long prev_offset;
416 unsigned int pages, max_pages, last_ra;
417 static atomic_t last_readahead_pages;
418
Jason Low4db0c3c2015-04-15 16:14:08 -0700419 max_pages = 1 << READ_ONCE(page_cluster);
Shaohua Li579f8292014-02-06 12:04:21 -0800420 if (max_pages <= 1)
421 return 1;
422
423 /*
424 * This heuristic has been found to work well on both sequential and
425 * random loads, swapping to hard disk or to SSD: please don't ask
426 * what the "+ 2" means, it just happens to work well, that's all.
427 */
428 pages = atomic_xchg(&swapin_readahead_hits, 0) + 2;
429 if (pages == 2) {
430 /*
431 * We can have no readahead hits to judge by: but must not get
432 * stuck here forever, so check for an adjacent offset instead
433 * (and don't even bother to check whether swap type is same).
434 */
435 if (offset != prev_offset + 1 && offset != prev_offset - 1)
436 pages = 1;
437 prev_offset = offset;
438 } else {
439 unsigned int roundup = 4;
440 while (roundup < pages)
441 roundup <<= 1;
442 pages = roundup;
443 }
444
445 if (pages > max_pages)
446 pages = max_pages;
447
448 /* Don't shrink readahead too fast */
449 last_ra = atomic_read(&last_readahead_pages) / 2;
450 if (pages < last_ra)
451 pages = last_ra;
452 atomic_set(&last_readahead_pages, pages);
453
454 return pages;
455}
456
Hugh Dickins46017e92008-02-04 22:28:41 -0800457/**
458 * swapin_readahead - swap in pages in hope we need them soon
459 * @entry: swap entry of this memory
Randy Dunlap76824862008-03-19 17:00:40 -0700460 * @gfp_mask: memory allocation flags
Hugh Dickins46017e92008-02-04 22:28:41 -0800461 * @vma: user vma this address belongs to
462 * @addr: target address for mempolicy
463 *
464 * Returns the struct page for entry and addr, after queueing swapin.
465 *
466 * Primitive swap readahead code. We simply read an aligned block of
467 * (1 << page_cluster) entries in the swap area. This method is chosen
468 * because it doesn't cost us any seek time. We also make sure to queue
469 * the 'original' request together with the readahead ones...
470 *
471 * This has been extended to use the NUMA policies from the mm triggering
472 * the readahead.
473 *
474 * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
Laurent Dufourdd2b4652018-04-17 16:33:15 +0200475 * This is needed to ensure the VMA will not be freed in our back. In the case
476 * of the speculative page fault handler, this cannot happen, even if we don't
477 * hold the mmap_sem. Callees are assumed to take care of reading VMA's fields
478 * using READ_ONCE() to read consistent values.
Hugh Dickins46017e92008-02-04 22:28:41 -0800479 */
Hugh Dickins02098fe2008-02-04 22:28:42 -0800480struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
Hugh Dickins46017e92008-02-04 22:28:41 -0800481 struct vm_area_struct *vma, unsigned long addr)
482{
Hugh Dickins46017e92008-02-04 22:28:41 -0800483 struct page *page;
Shaohua Li579f8292014-02-06 12:04:21 -0800484 unsigned long entry_offset = swp_offset(entry);
485 unsigned long offset = entry_offset;
Rik van Riel67f96aa2012-03-21 16:33:50 -0700486 unsigned long start_offset, end_offset;
Vinayak Menonee081c232015-11-20 13:37:08 +0530487 unsigned long mask;
Christian Ehrhardt3fb5c292012-07-31 16:41:44 -0700488 struct blk_plug plug;
Hugh Dickins46017e92008-02-04 22:28:41 -0800489
Vinayak Menonee081c232015-11-20 13:37:08 +0530490 mask = is_swap_fast(entry) ? 0 : swapin_nr_pages(offset) - 1;
Shaohua Li579f8292014-02-06 12:04:21 -0800491 if (!mask)
492 goto skip;
493
Rik van Riel67f96aa2012-03-21 16:33:50 -0700494 /* Read a page_cluster sized and aligned cluster around offset. */
495 start_offset = offset & ~mask;
496 end_offset = offset | mask;
497 if (!start_offset) /* First page is swap header. */
498 start_offset++;
499
Christian Ehrhardt3fb5c292012-07-31 16:41:44 -0700500 blk_start_plug(&plug);
Rik van Riel67f96aa2012-03-21 16:33:50 -0700501 for (offset = start_offset; offset <= end_offset ; offset++) {
Hugh Dickins46017e92008-02-04 22:28:41 -0800502 /* Ok, do the async read-ahead now */
503 page = read_swap_cache_async(swp_entry(swp_type(entry), offset),
Hugh Dickins02098fe2008-02-04 22:28:42 -0800504 gfp_mask, vma, addr);
Hugh Dickins46017e92008-02-04 22:28:41 -0800505 if (!page)
Rik van Riel67f96aa2012-03-21 16:33:50 -0700506 continue;
Shaohua Li579f8292014-02-06 12:04:21 -0800507 if (offset != entry_offset)
508 SetPageReadahead(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300509 put_page(page);
Hugh Dickins46017e92008-02-04 22:28:41 -0800510 }
Christian Ehrhardt3fb5c292012-07-31 16:41:44 -0700511 blk_finish_plug(&plug);
512
Hugh Dickins46017e92008-02-04 22:28:41 -0800513 lru_add_drain(); /* Push any new pages onto the LRU now */
Shaohua Li579f8292014-02-06 12:04:21 -0800514skip:
Hugh Dickins02098fe2008-02-04 22:28:42 -0800515 return read_swap_cache_async(entry, gfp_mask, vma, addr);
Hugh Dickins46017e92008-02-04 22:28:41 -0800516}