blob: 57c35b5e75fcba9bd9f2c3ff107995a0c8685585 [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>
Hugh Dickinsb6478892020-06-25 20:29:59 -070023#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25/*
26 * swapper_space is a fiction, retained to simplify the path through
Jens Axboe7eaceac2011-03-10 08:52:07 +010027 * vmscan's shrink_page_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 */
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070029static const struct address_space_operations swap_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 .writepage = swap_writepage,
Mel Gorman62c230b2012-07-31 16:44:55 -070031 .set_page_dirty = swap_set_page_dirty,
Andrew Morton1c939232014-10-09 15:27:59 -070032#ifdef CONFIG_MIGRATION
Christoph Lametere965f962006-02-01 03:05:41 -080033 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -070034#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
Shaohua Li33806f02013-02-22 16:34:37 -080037struct address_space swapper_spaces[MAX_SWAPFILES] = {
38 [0 ... MAX_SWAPFILES - 1] = {
39 .page_tree = RADIX_TREE_INIT(GFP_ATOMIC|__GFP_NOWARN),
David Herrmann4bb5f5d2014-08-08 14:25:25 -070040 .i_mmap_writable = ATOMIC_INIT(0),
Shaohua Li33806f02013-02-22 16:34:37 -080041 .a_ops = &swap_aops,
Huang Ying371a0962016-10-07 16:59:30 -070042 /* swap cache doesn't use writeback related tags */
43 .flags = 1 << AS_NO_WRITEBACK_TAGS,
Shaohua Li33806f02013-02-22 16:34:37 -080044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define INC_CACHE_INFO(x) do { swap_cache_info.x++; } while (0)
48
49static struct {
50 unsigned long add_total;
51 unsigned long del_total;
52 unsigned long find_success;
53 unsigned long find_total;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054} swap_cache_info;
55
Shaohua Li33806f02013-02-22 16:34:37 -080056unsigned long total_swapcache_pages(void)
57{
58 int i;
59 unsigned long ret = 0;
60
61 for (i = 0; i < MAX_SWAPFILES; i++)
62 ret += swapper_spaces[i].nrpages;
63 return ret;
64}
65
Shaohua Li579f8292014-02-06 12:04:21 -080066static atomic_t swapin_readahead_hits = ATOMIC_INIT(4);
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068void show_swap_cache_info(void)
69{
Shaohua Li33806f02013-02-22 16:34:37 -080070 printk("%lu pages in swap cache\n", total_swapcache_pages());
Johannes Weiner2c97b7f2008-07-25 19:46:01 -070071 printk("Swap cache stats: add %lu, delete %lu, find %lu/%lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 swap_cache_info.add_total, swap_cache_info.del_total,
Hugh Dickinsbb63be02008-02-04 22:28:49 -080073 swap_cache_info.find_success, swap_cache_info.find_total);
Shaohua Liec8acf22013-02-22 16:34:38 -080074 printk("Free swap = %ldkB\n",
75 get_nr_swap_pages() << (PAGE_SHIFT - 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10));
77}
78
79/*
Daisuke Nishimura31a56392009-09-21 17:02:50 -070080 * __add_to_swap_cache resembles add_to_page_cache_locked on swapper_space,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * but sets SwapCache flag and private instead of mapping and index.
82 */
Seth Jennings2f772e62013-04-29 15:08:34 -070083int __add_to_swap_cache(struct page *page, swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 int error;
Shaohua Li33806f02013-02-22 16:34:37 -080086 struct address_space *address_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Sasha Levin309381fea2014-01-23 15:52:54 -080088 VM_BUG_ON_PAGE(!PageLocked(page), page);
89 VM_BUG_ON_PAGE(PageSwapCache(page), page);
90 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Hugh Dickins51726b12009-01-06 14:39:25 -080091
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030092 get_page(page);
Daisuke Nishimura31a56392009-09-21 17:02:50 -070093 SetPageSwapCache(page);
94 set_page_private(page, entry.val);
95
Shaohua Li33806f02013-02-22 16:34:37 -080096 address_space = swap_address_space(entry);
97 spin_lock_irq(&address_space->tree_lock);
98 error = radix_tree_insert(&address_space->page_tree,
Huang Yingf6ab1f72016-10-07 17:00:21 -070099 swp_offset(entry), page);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700100 if (likely(!error)) {
Shaohua Li33806f02013-02-22 16:34:37 -0800101 address_space->nrpages++;
Mel Gorman11fb9982016-07-28 15:46:20 -0700102 __inc_node_page_state(page, NR_FILE_PAGES);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700103 INC_CACHE_INFO(add_total);
104 }
Shaohua Li33806f02013-02-22 16:34:37 -0800105 spin_unlock_irq(&address_space->tree_lock);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700106
107 if (unlikely(error)) {
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700108 /*
109 * Only the context which have set SWAP_HAS_CACHE flag
110 * would call add_to_swap_cache().
111 * So add_to_swap_cache() doesn't returns -EEXIST.
112 */
113 VM_BUG_ON(error == -EEXIST);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700114 set_page_private(page, 0UL);
115 ClearPageSwapCache(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300116 put_page(page);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700117 }
118
119 return error;
120}
121
122
123int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp_mask)
124{
125 int error;
126
Jan Kara5e4c0d972013-09-11 14:26:05 -0700127 error = radix_tree_maybe_preload(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (!error) {
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700129 error = __add_to_swap_cache(page, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 radix_tree_preload_end();
Hugh Dickinsfa1de902008-02-07 00:14:13 -0800131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return error;
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
136 * This must be called only on pages that have
137 * been verified to be in the swap cache.
138 */
139void __delete_from_swap_cache(struct page *page)
140{
Shaohua Li33806f02013-02-22 16:34:37 -0800141 swp_entry_t entry;
142 struct address_space *address_space;
143
Sasha Levin309381fea2014-01-23 15:52:54 -0800144 VM_BUG_ON_PAGE(!PageLocked(page), page);
145 VM_BUG_ON_PAGE(!PageSwapCache(page), page);
146 VM_BUG_ON_PAGE(PageWriteback(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Shaohua Li33806f02013-02-22 16:34:37 -0800148 entry.val = page_private(page);
149 address_space = swap_address_space(entry);
Huang Yingf6ab1f72016-10-07 17:00:21 -0700150 radix_tree_delete(&address_space->page_tree, swp_offset(entry));
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700151 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 ClearPageSwapCache(page);
Shaohua Li33806f02013-02-22 16:34:37 -0800153 address_space->nrpages--;
Mel Gorman11fb9982016-07-28 15:46:20 -0700154 __dec_node_page_state(page, NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 INC_CACHE_INFO(del_total);
156}
157
158/**
159 * add_to_swap - allocate swap space for a page
160 * @page: page we want to move to swap
161 *
162 * Allocate swap space for the page and add the page to the
163 * swap cache. Caller needs to hold the page lock.
164 */
Shaohua Li5bc7b8a2013-04-29 15:08:36 -0700165int add_to_swap(struct page *page, struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 swp_entry_t entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 int err;
169
Sasha Levin309381fea2014-01-23 15:52:54 -0800170 VM_BUG_ON_PAGE(!PageLocked(page), page);
171 VM_BUG_ON_PAGE(!PageUptodate(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700173 entry = get_swap_page();
174 if (!entry.val)
175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Vladimir Davydov37e84352016-01-20 15:02:56 -0800177 if (mem_cgroup_try_charge_swap(page, entry)) {
178 swapcache_free(entry);
179 return 0;
180 }
181
Andrea Arcangeli3f04f622011-01-13 15:46:47 -0800182 if (unlikely(PageTransHuge(page)))
Shaohua Li5bc7b8a2013-04-29 15:08:36 -0700183 if (unlikely(split_huge_page_to_list(page, list))) {
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700184 swapcache_free(entry);
Andrea Arcangeli3f04f622011-01-13 15:46:47 -0800185 return 0;
186 }
187
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700188 /*
189 * Radix-tree node allocations from PF_MEMALLOC contexts could
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 /*
Minchan Kim854e9ed2016-01-15 16:54:53 -0800197 * Add it to the swap cache.
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700198 */
199 err = add_to_swap_cache(page, entry,
200 __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Minchan Kim854e9ed2016-01-15 16:54:53 -0800202 if (!err) {
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700203 return 1;
204 } else { /* -ENOMEM radix-tree allocation failure */
205 /*
206 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
207 * clear SWAP_HAS_CACHE flag.
208 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700209 swapcache_free(entry);
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212}
213
214/*
215 * This must be called only on pages that have
216 * been verified to be in the swap cache and locked.
217 * It will never put the page into the free list,
218 * the caller has a reference on the page.
219 */
220void delete_from_swap_cache(struct page *page)
221{
222 swp_entry_t entry;
Shaohua Li33806f02013-02-22 16:34:37 -0800223 struct address_space *address_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700225 entry.val = page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Shaohua Li33806f02013-02-22 16:34:37 -0800227 address_space = swap_address_space(entry);
228 spin_lock_irq(&address_space->tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 __delete_from_swap_cache(page);
Shaohua Li33806f02013-02-22 16:34:37 -0800230 spin_unlock_irq(&address_space->tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700232 swapcache_free(entry);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300233 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/*
237 * If we are the only user, then try to free up the swap cache.
238 *
239 * Its ok to check for PageSwapCache without the page lock
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800240 * here because we are going to recheck again inside
241 * try_to_free_swap() _with_ the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * - Marcelo
243 */
244static inline void free_swap_cache(struct page *page)
245{
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800246 if (PageSwapCache(page) && !page_mapped(page) && trylock_page(page)) {
247 try_to_free_swap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 unlock_page(page);
249 }
250}
251
252/*
253 * Perform a free_page(), also freeing any swap cache associated with
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700254 * this page if it is the last user of the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
256void free_page_and_swap_cache(struct page *page)
257{
258 free_swap_cache(page);
Aaron Lu6fcb52a2016-10-07 17:00:08 -0700259 if (!is_huge_zero_page(page))
Gerald Schaefer770a5372016-06-08 15:33:50 -0700260 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263/*
264 * Passed an array of pages, drop them all from swapcache and then release
265 * them. They are removed from the LRU and freed if this is their last use.
266 */
267void free_pages_and_swap_cache(struct page **pages, int nr)
268{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct page **pagep = pages;
Michal Hockoaabfb572014-10-09 15:28:52 -0700270 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 lru_add_drain();
Michal Hockoaabfb572014-10-09 15:28:52 -0700273 for (i = 0; i < nr; i++)
274 free_swap_cache(pagep[i]);
275 release_pages(pagep, nr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/*
279 * Lookup a swap entry in the swap cache. A found page will be returned
280 * unlocked and with its refcount incremented - we rely on the kernel
281 * lock getting page table operations atomic even if we drop the page
282 * lock before returning.
283 */
284struct page * lookup_swap_cache(swp_entry_t entry)
285{
286 struct page *page;
287
Huang Yingf6ab1f72016-10-07 17:00:21 -0700288 page = find_get_page(swap_address_space(entry), swp_offset(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Shaohua Li579f8292014-02-06 12:04:21 -0800290 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 INC_CACHE_INFO(find_success);
Shaohua Li579f8292014-02-06 12:04:21 -0800292 if (TestClearPageReadahead(page))
293 atomic_inc(&swapin_readahead_hits);
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 INC_CACHE_INFO(find_total);
297 return page;
298}
299
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700300struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
301 struct vm_area_struct *vma, unsigned long addr,
302 bool *new_page_allocated)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 struct page *found_page, *new_page = NULL;
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700305 struct address_space *swapper_space = swap_address_space(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int err;
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700307 *new_page_allocated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 do {
310 /*
311 * First check the swap cache. Since this is normally
312 * called after lookup_swap_cache() failed, re-calling
313 * that would confuse statistics.
314 */
Huang Yingf6ab1f72016-10-07 17:00:21 -0700315 found_page = find_get_page(swapper_space, swp_offset(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (found_page)
317 break;
318
319 /*
320 * Get a new page to read into from swap.
321 */
322 if (!new_page) {
Hugh Dickins02098fe2008-02-04 22:28:42 -0800323 new_page = alloc_page_vma(gfp_mask, vma, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (!new_page)
325 break; /* Out of memory */
326 }
327
328 /*
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700329 * call radix_tree_preload() while we can wait.
330 */
Hugh Dickinsb6478892020-06-25 20:29:59 -0700331 err = radix_tree_maybe_preload(gfp_mask & GFP_RECLAIM_MASK);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700332 if (err)
333 break;
334
335 /*
Hugh Dickinsf0009442008-02-04 22:28:49 -0800336 * Swap entry may have been freed since our caller observed it.
337 */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700338 err = swapcache_prepare(entry);
Rafael Aquinicbab0e42013-06-12 14:04:49 -0700339 if (err == -EEXIST) {
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700340 radix_tree_preload_end();
Rafael Aquinicbab0e42013-06-12 14:04:49 -0700341 /*
342 * We might race against get_swap_page() and stumble
343 * across a SWAP_HAS_CACHE swap_map entry whose page
344 * has not been brought into the swapcache yet, while
345 * the other end is scheduled away waiting on discard
346 * I/O completion at scan_swap_map().
347 *
348 * In order to avoid turning this transitory state
349 * into a permanent loop around this -EEXIST case
350 * if !CONFIG_PREEMPT and the I/O completion happens
351 * to be waiting on the CPU waitqueue where we are now
352 * busy looping, we just conditionally invoke the
353 * scheduler here, if there are some more important
354 * tasks to run.
Zhenhua Huang05b7b7c2018-10-12 17:51:32 +0800355 *
356 * cond_resched() may not work if the process is RT.
357 * We need a usleep_range() give up CPU to another task.
Rafael Aquinicbab0e42013-06-12 14:04:49 -0700358 */
Zhenhua Huang05b7b7c2018-10-12 17:51:32 +0800359 usleep_range(500, 1000);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700360 continue;
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700361 }
362 if (err) { /* swp entry is obsolete ? */
363 radix_tree_preload_end();
Hugh Dickinsf0009442008-02-04 22:28:49 -0800364 break;
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700365 }
Hugh Dickinsf0009442008-02-04 22:28:49 -0800366
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700367 /* May fail (-ENOMEM) if radix-tree node allocation failed. */
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800368 __SetPageLocked(new_page);
Hugh Dickinsfa9949d2016-05-19 17:12:41 -0700369 __SetPageSwapBacked(new_page);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700370 err = __add_to_swap_cache(new_page, entry);
Nick Piggin529ae9a2008-08-02 12:01:03 +0200371 if (likely(!err)) {
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700372 radix_tree_preload_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /*
374 * Initiate read into locked page and return.
375 */
Johannes Weinera2383322018-10-26 15:06:04 -0700376 SetPageWorkingset(new_page);
Rik van Rielc5fdae42008-10-18 20:26:36 -0700377 lru_cache_add_anon(new_page);
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700378 *new_page_allocated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return new_page;
380 }
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700381 radix_tree_preload_end();
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800382 __ClearPageLocked(new_page);
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700383 /*
384 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
385 * clear SWAP_HAS_CACHE flag.
386 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700387 swapcache_free(entry);
Hugh Dickinsf0009442008-02-04 22:28:49 -0800388 } while (err != -ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 if (new_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300391 put_page(new_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return found_page;
393}
Hugh Dickins46017e92008-02-04 22:28:41 -0800394
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700395/*
396 * Locate a page of swap in physical memory, reserving swap cache space
397 * and reading the disk if it is not already cached.
398 * A failure return means that either the page allocation failed or that
399 * the swap entry is no longer in use.
400 */
401struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
402 struct vm_area_struct *vma, unsigned long addr)
403{
404 bool page_was_allocated;
405 struct page *retpage = __read_swap_cache_async(entry, gfp_mask,
406 vma, addr, &page_was_allocated);
407
408 if (page_was_allocated)
409 swap_readpage(retpage);
410
411 return retpage;
412}
413
Shaohua Li579f8292014-02-06 12:04:21 -0800414static unsigned long swapin_nr_pages(unsigned long offset)
415{
416 static unsigned long prev_offset;
417 unsigned int pages, max_pages, last_ra;
418 static atomic_t last_readahead_pages;
419
Jason Low4db0c3c2015-04-15 16:14:08 -0700420 max_pages = 1 << READ_ONCE(page_cluster);
Shaohua Li579f8292014-02-06 12:04:21 -0800421 if (max_pages <= 1)
422 return 1;
423
424 /*
425 * This heuristic has been found to work well on both sequential and
426 * random loads, swapping to hard disk or to SSD: please don't ask
427 * what the "+ 2" means, it just happens to work well, that's all.
428 */
429 pages = atomic_xchg(&swapin_readahead_hits, 0) + 2;
430 if (pages == 2) {
431 /*
432 * We can have no readahead hits to judge by: but must not get
433 * stuck here forever, so check for an adjacent offset instead
434 * (and don't even bother to check whether swap type is same).
435 */
436 if (offset != prev_offset + 1 && offset != prev_offset - 1)
437 pages = 1;
438 prev_offset = offset;
439 } else {
440 unsigned int roundup = 4;
441 while (roundup < pages)
442 roundup <<= 1;
443 pages = roundup;
444 }
445
446 if (pages > max_pages)
447 pages = max_pages;
448
449 /* Don't shrink readahead too fast */
450 last_ra = atomic_read(&last_readahead_pages) / 2;
451 if (pages < last_ra)
452 pages = last_ra;
453 atomic_set(&last_readahead_pages, pages);
454
455 return pages;
456}
457
Hugh Dickins46017e92008-02-04 22:28:41 -0800458/**
459 * swapin_readahead - swap in pages in hope we need them soon
460 * @entry: swap entry of this memory
Randy Dunlap76824862008-03-19 17:00:40 -0700461 * @gfp_mask: memory allocation flags
Hugh Dickins46017e92008-02-04 22:28:41 -0800462 * @vma: user vma this address belongs to
463 * @addr: target address for mempolicy
464 *
465 * Returns the struct page for entry and addr, after queueing swapin.
466 *
467 * Primitive swap readahead code. We simply read an aligned block of
468 * (1 << page_cluster) entries in the swap area. This method is chosen
469 * because it doesn't cost us any seek time. We also make sure to queue
470 * the 'original' request together with the readahead ones...
471 *
472 * This has been extended to use the NUMA policies from the mm triggering
473 * the readahead.
474 *
475 * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
Laurent Dufourdd2b4652018-04-17 16:33:15 +0200476 * This is needed to ensure the VMA will not be freed in our back. In the case
477 * of the speculative page fault handler, this cannot happen, even if we don't
478 * hold the mmap_sem. Callees are assumed to take care of reading VMA's fields
479 * using READ_ONCE() to read consistent values.
Hugh Dickins46017e92008-02-04 22:28:41 -0800480 */
Hugh Dickins02098fe2008-02-04 22:28:42 -0800481struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
Hugh Dickins46017e92008-02-04 22:28:41 -0800482 struct vm_area_struct *vma, unsigned long addr)
483{
Hugh Dickins46017e92008-02-04 22:28:41 -0800484 struct page *page;
Shaohua Li579f8292014-02-06 12:04:21 -0800485 unsigned long entry_offset = swp_offset(entry);
486 unsigned long offset = entry_offset;
Rik van Riel67f96aa2012-03-21 16:33:50 -0700487 unsigned long start_offset, end_offset;
Vinayak Menonee081c232015-11-20 13:37:08 +0530488 unsigned long mask;
Christian Ehrhardt3fb5c292012-07-31 16:41:44 -0700489 struct blk_plug plug;
Hugh Dickins46017e92008-02-04 22:28:41 -0800490
Vinayak Menonee081c232015-11-20 13:37:08 +0530491 mask = is_swap_fast(entry) ? 0 : swapin_nr_pages(offset) - 1;
Shaohua Li579f8292014-02-06 12:04:21 -0800492 if (!mask)
493 goto skip;
494
Rik van Riel67f96aa2012-03-21 16:33:50 -0700495 /* Read a page_cluster sized and aligned cluster around offset. */
496 start_offset = offset & ~mask;
497 end_offset = offset | mask;
498 if (!start_offset) /* First page is swap header. */
499 start_offset++;
500
Christian Ehrhardt3fb5c292012-07-31 16:41:44 -0700501 blk_start_plug(&plug);
Rik van Riel67f96aa2012-03-21 16:33:50 -0700502 for (offset = start_offset; offset <= end_offset ; offset++) {
Hugh Dickins46017e92008-02-04 22:28:41 -0800503 /* Ok, do the async read-ahead now */
504 page = read_swap_cache_async(swp_entry(swp_type(entry), offset),
Hugh Dickins02098fe2008-02-04 22:28:42 -0800505 gfp_mask, vma, addr);
Hugh Dickins46017e92008-02-04 22:28:41 -0800506 if (!page)
Rik van Riel67f96aa2012-03-21 16:33:50 -0700507 continue;
Shaohua Li579f8292014-02-06 12:04:21 -0800508 if (offset != entry_offset)
509 SetPageReadahead(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300510 put_page(page);
Hugh Dickins46017e92008-02-04 22:28:41 -0800511 }
Christian Ehrhardt3fb5c292012-07-31 16:41:44 -0700512 blk_finish_plug(&plug);
513
Hugh Dickins46017e92008-02-04 22:28:41 -0800514 lru_add_drain(); /* Push any new pages onto the LRU now */
Shaohua Li579f8292014-02-06 12:04:21 -0800515skip:
Hugh Dickins02098fe2008-02-04 22:28:42 -0800516 return read_swap_cache_async(entry, gfp_mask, vma, addr);
Hugh Dickins46017e92008-02-04 22:28:41 -0800517}