blob: 0ad214d7a7ad22c1ee871759f07d70e6f74be0c8 [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>
Huang, Ying4b3ef9d2017-02-22 15:45:26 -080020#include <linux/vmalloc.h>
Tim Chen67afa382017-02-22 15:45:39 -080021#include <linux/swap_slots.h>
Huang Ying38d8b4e2017-07-06 15:37:18 -070022#include <linux/huge_mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/pgtable.h>
25
26/*
27 * swapper_space is a fiction, retained to simplify the path through
Jens Axboe7eaceac2011-03-10 08:52:07 +010028 * vmscan's shrink_page_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070030static const struct address_space_operations swap_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 .writepage = swap_writepage,
Mel Gorman62c230b2012-07-31 16:44:55 -070032 .set_page_dirty = swap_set_page_dirty,
Andrew Morton1c939232014-10-09 15:27:59 -070033#ifdef CONFIG_MIGRATION
Christoph Lametere965f962006-02-01 03:05:41 -080034 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -070035#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070036};
37
Huang, Ying4b3ef9d2017-02-22 15:45:26 -080038struct address_space *swapper_spaces[MAX_SWAPFILES];
39static unsigned int nr_swapper_spaces[MAX_SWAPFILES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define INC_CACHE_INFO(x) do { swap_cache_info.x++; } while (0)
Huang Ying38d8b4e2017-07-06 15:37:18 -070042#define ADD_CACHE_INFO(x, nr) do { swap_cache_info.x += (nr); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44static struct {
45 unsigned long add_total;
46 unsigned long del_total;
47 unsigned long find_success;
48 unsigned long find_total;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049} swap_cache_info;
50
Shaohua Li33806f02013-02-22 16:34:37 -080051unsigned long total_swapcache_pages(void)
52{
Huang, Ying4b3ef9d2017-02-22 15:45:26 -080053 unsigned int i, j, nr;
Shaohua Li33806f02013-02-22 16:34:37 -080054 unsigned long ret = 0;
Huang, Ying4b3ef9d2017-02-22 15:45:26 -080055 struct address_space *spaces;
Shaohua Li33806f02013-02-22 16:34:37 -080056
Huang, Ying4b3ef9d2017-02-22 15:45:26 -080057 rcu_read_lock();
58 for (i = 0; i < MAX_SWAPFILES; i++) {
59 /*
60 * The corresponding entries in nr_swapper_spaces and
61 * swapper_spaces will be reused only after at least
62 * one grace period. So it is impossible for them
63 * belongs to different usage.
64 */
65 nr = nr_swapper_spaces[i];
66 spaces = rcu_dereference(swapper_spaces[i]);
67 if (!nr || !spaces)
68 continue;
69 for (j = 0; j < nr; j++)
70 ret += spaces[j].nrpages;
71 }
72 rcu_read_unlock();
Shaohua Li33806f02013-02-22 16:34:37 -080073 return ret;
74}
75
Shaohua Li579f8292014-02-06 12:04:21 -080076static atomic_t swapin_readahead_hits = ATOMIC_INIT(4);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078void show_swap_cache_info(void)
79{
Shaohua Li33806f02013-02-22 16:34:37 -080080 printk("%lu pages in swap cache\n", total_swapcache_pages());
Johannes Weiner2c97b7f2008-07-25 19:46:01 -070081 printk("Swap cache stats: add %lu, delete %lu, find %lu/%lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 swap_cache_info.add_total, swap_cache_info.del_total,
Hugh Dickinsbb63be02008-02-04 22:28:49 -080083 swap_cache_info.find_success, swap_cache_info.find_total);
Shaohua Liec8acf22013-02-22 16:34:38 -080084 printk("Free swap = %ldkB\n",
85 get_nr_swap_pages() << (PAGE_SHIFT - 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10));
87}
88
89/*
Daisuke Nishimura31a56392009-09-21 17:02:50 -070090 * __add_to_swap_cache resembles add_to_page_cache_locked on swapper_space,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * but sets SwapCache flag and private instead of mapping and index.
92 */
Seth Jennings2f772e62013-04-29 15:08:34 -070093int __add_to_swap_cache(struct page *page, swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Huang Ying38d8b4e2017-07-06 15:37:18 -070095 int error, i, nr = hpage_nr_pages(page);
Shaohua Li33806f02013-02-22 16:34:37 -080096 struct address_space *address_space;
Huang Ying38d8b4e2017-07-06 15:37:18 -070097 pgoff_t idx = swp_offset(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Sasha Levin309381fea2014-01-23 15:52:54 -080099 VM_BUG_ON_PAGE(!PageLocked(page), page);
100 VM_BUG_ON_PAGE(PageSwapCache(page), page);
101 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Hugh Dickins51726b12009-01-06 14:39:25 -0800102
Huang Ying38d8b4e2017-07-06 15:37:18 -0700103 page_ref_add(page, nr);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700104 SetPageSwapCache(page);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700105
Shaohua Li33806f02013-02-22 16:34:37 -0800106 address_space = swap_address_space(entry);
107 spin_lock_irq(&address_space->tree_lock);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700108 for (i = 0; i < nr; i++) {
109 set_page_private(page + i, entry.val + i);
110 error = radix_tree_insert(&address_space->page_tree,
111 idx + i, page + i);
112 if (unlikely(error))
113 break;
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700114 }
Huang Ying38d8b4e2017-07-06 15:37:18 -0700115 if (likely(!error)) {
116 address_space->nrpages += nr;
117 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
118 ADD_CACHE_INFO(add_total, nr);
119 } else {
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700120 /*
121 * Only the context which have set SWAP_HAS_CACHE flag
122 * would call add_to_swap_cache().
123 * So add_to_swap_cache() doesn't returns -EEXIST.
124 */
125 VM_BUG_ON(error == -EEXIST);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700126 set_page_private(page + i, 0UL);
127 while (i--) {
128 radix_tree_delete(&address_space->page_tree, idx + i);
129 set_page_private(page + i, 0UL);
130 }
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700131 ClearPageSwapCache(page);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700132 page_ref_sub(page, nr);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700133 }
Huang Ying38d8b4e2017-07-06 15:37:18 -0700134 spin_unlock_irq(&address_space->tree_lock);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700135
136 return error;
137}
138
139
140int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp_mask)
141{
142 int error;
143
Huang Ying38d8b4e2017-07-06 15:37:18 -0700144 error = radix_tree_maybe_preload_order(gfp_mask, compound_order(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (!error) {
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700146 error = __add_to_swap_cache(page, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 radix_tree_preload_end();
Hugh Dickinsfa1de902008-02-07 00:14:13 -0800148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return error;
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
153 * This must be called only on pages that have
154 * been verified to be in the swap cache.
155 */
156void __delete_from_swap_cache(struct page *page)
157{
Shaohua Li33806f02013-02-22 16:34:37 -0800158 struct address_space *address_space;
Huang Ying38d8b4e2017-07-06 15:37:18 -0700159 int i, nr = hpage_nr_pages(page);
160 swp_entry_t entry;
161 pgoff_t idx;
Shaohua Li33806f02013-02-22 16:34:37 -0800162
Sasha Levin309381fea2014-01-23 15:52:54 -0800163 VM_BUG_ON_PAGE(!PageLocked(page), page);
164 VM_BUG_ON_PAGE(!PageSwapCache(page), page);
165 VM_BUG_ON_PAGE(PageWriteback(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Shaohua Li33806f02013-02-22 16:34:37 -0800167 entry.val = page_private(page);
168 address_space = swap_address_space(entry);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700169 idx = swp_offset(entry);
170 for (i = 0; i < nr; i++) {
171 radix_tree_delete(&address_space->page_tree, idx + i);
172 set_page_private(page + i, 0);
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 ClearPageSwapCache(page);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700175 address_space->nrpages -= nr;
176 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
177 ADD_CACHE_INFO(del_total, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180/**
181 * add_to_swap - allocate swap space for a page
182 * @page: page we want to move to swap
183 *
184 * Allocate swap space for the page and add the page to the
185 * swap cache. Caller needs to hold the page lock.
186 */
Shaohua Li5bc7b8a2013-04-29 15:08:36 -0700187int add_to_swap(struct page *page, struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 swp_entry_t entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int err;
191
Sasha Levin309381fea2014-01-23 15:52:54 -0800192 VM_BUG_ON_PAGE(!PageLocked(page), page);
193 VM_BUG_ON_PAGE(!PageUptodate(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Huang Ying38d8b4e2017-07-06 15:37:18 -0700195retry:
196 entry = get_swap_page(page);
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700197 if (!entry.val)
Huang Ying38d8b4e2017-07-06 15:37:18 -0700198 goto fail;
199 if (mem_cgroup_try_charge_swap(page, entry))
200 goto fail_free;
Andrea Arcangeli3f04f622011-01-13 15:46:47 -0800201
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700202 /*
203 * Radix-tree node allocations from PF_MEMALLOC contexts could
204 * completely exhaust the page allocator. __GFP_NOMEMALLOC
205 * stops emergency reserves from being allocated.
206 *
207 * TODO: this could cause a theoretical memory reclaim
208 * deadlock in the swap out path.
209 */
210 /*
Minchan Kim854e9ed2016-01-15 16:54:53 -0800211 * Add it to the swap cache.
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700212 */
213 err = add_to_swap_cache(page, entry,
214 __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700215 /* -ENOMEM radix-tree allocation failure */
216 if (err)
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700217 /*
218 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
219 * clear SWAP_HAS_CACHE flag.
220 */
Huang Ying38d8b4e2017-07-06 15:37:18 -0700221 goto fail_free;
222
223 if (PageTransHuge(page)) {
224 err = split_huge_page_to_list(page, list);
225 if (err) {
226 delete_from_swap_cache(page);
227 return 0;
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Huang Ying38d8b4e2017-07-06 15:37:18 -0700230
231 return 1;
232
233fail_free:
Minchan Kim75f6d6d2017-07-06 15:37:21 -0700234 put_swap_page(page, entry);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700235fail:
236 if (PageTransHuge(page) && !split_huge_page_to_list(page, list))
237 goto retry;
238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241/*
242 * This must be called only on pages that have
243 * been verified to be in the swap cache and locked.
244 * It will never put the page into the free list,
245 * the caller has a reference on the page.
246 */
247void delete_from_swap_cache(struct page *page)
248{
249 swp_entry_t entry;
Shaohua Li33806f02013-02-22 16:34:37 -0800250 struct address_space *address_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700252 entry.val = page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Shaohua Li33806f02013-02-22 16:34:37 -0800254 address_space = swap_address_space(entry);
255 spin_lock_irq(&address_space->tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 __delete_from_swap_cache(page);
Shaohua Li33806f02013-02-22 16:34:37 -0800257 spin_unlock_irq(&address_space->tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Minchan Kim75f6d6d2017-07-06 15:37:21 -0700259 put_swap_page(page, entry);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700260 page_ref_sub(page, hpage_nr_pages(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263/*
264 * If we are the only user, then try to free up the swap cache.
265 *
266 * Its ok to check for PageSwapCache without the page lock
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800267 * here because we are going to recheck again inside
268 * try_to_free_swap() _with_ the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * - Marcelo
270 */
271static inline void free_swap_cache(struct page *page)
272{
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800273 if (PageSwapCache(page) && !page_mapped(page) && trylock_page(page)) {
274 try_to_free_swap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 unlock_page(page);
276 }
277}
278
279/*
280 * Perform a free_page(), also freeing any swap cache associated with
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700281 * this page if it is the last user of the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
283void free_page_and_swap_cache(struct page *page)
284{
285 free_swap_cache(page);
Aaron Lu6fcb52a2016-10-07 17:00:08 -0700286 if (!is_huge_zero_page(page))
Gerald Schaefer770a5372016-06-08 15:33:50 -0700287 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290/*
291 * Passed an array of pages, drop them all from swapcache and then release
292 * them. They are removed from the LRU and freed if this is their last use.
293 */
294void free_pages_and_swap_cache(struct page **pages, int nr)
295{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct page **pagep = pages;
Michal Hockoaabfb572014-10-09 15:28:52 -0700297 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 lru_add_drain();
Michal Hockoaabfb572014-10-09 15:28:52 -0700300 for (i = 0; i < nr; i++)
301 free_swap_cache(pagep[i]);
302 release_pages(pagep, nr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305/*
306 * Lookup a swap entry in the swap cache. A found page will be returned
307 * unlocked and with its refcount incremented - we rely on the kernel
308 * lock getting page table operations atomic even if we drop the page
309 * lock before returning.
310 */
311struct page * lookup_swap_cache(swp_entry_t entry)
312{
313 struct page *page;
314
Huang Yingf6ab1f72016-10-07 17:00:21 -0700315 page = find_get_page(swap_address_space(entry), swp_offset(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Huang Ying38d8b4e2017-07-06 15:37:18 -0700317 if (page && likely(!PageTransCompound(page))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 INC_CACHE_INFO(find_success);
Shaohua Li579f8292014-02-06 12:04:21 -0800319 if (TestClearPageReadahead(page))
320 atomic_inc(&swapin_readahead_hits);
321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 INC_CACHE_INFO(find_total);
324 return page;
325}
326
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700327struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
328 struct vm_area_struct *vma, unsigned long addr,
329 bool *new_page_allocated)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 struct page *found_page, *new_page = NULL;
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700332 struct address_space *swapper_space = swap_address_space(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int err;
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700334 *new_page_allocated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 do {
337 /*
338 * First check the swap cache. Since this is normally
339 * called after lookup_swap_cache() failed, re-calling
340 * that would confuse statistics.
341 */
Huang Yingf6ab1f72016-10-07 17:00:21 -0700342 found_page = find_get_page(swapper_space, swp_offset(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (found_page)
344 break;
345
Huang Yingba81f832017-02-22 15:45:46 -0800346 /*
347 * Just skip read ahead for unused swap slot.
348 * During swap_off when swap_slot_cache is disabled,
349 * we have to handle the race between putting
350 * swap entry in swap cache and marking swap slot
351 * as SWAP_HAS_CACHE. That's done in later part of code or
352 * else swap_off will be aborted if we return NULL.
353 */
354 if (!__swp_swapcount(entry) && swap_slot_cache_enabled)
355 break;
Tim Chene8c26ab2017-02-22 15:45:29 -0800356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 /*
358 * Get a new page to read into from swap.
359 */
360 if (!new_page) {
Hugh Dickins02098fe2008-02-04 22:28:42 -0800361 new_page = alloc_page_vma(gfp_mask, vma, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (!new_page)
363 break; /* Out of memory */
364 }
365
366 /*
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700367 * call radix_tree_preload() while we can wait.
368 */
Jan Kara5e4c0d972013-09-11 14:26:05 -0700369 err = radix_tree_maybe_preload(gfp_mask & GFP_KERNEL);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700370 if (err)
371 break;
372
373 /*
Hugh Dickinsf0009442008-02-04 22:28:49 -0800374 * Swap entry may have been freed since our caller observed it.
375 */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700376 err = swapcache_prepare(entry);
Rafael Aquinicbab0e42013-06-12 14:04:49 -0700377 if (err == -EEXIST) {
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700378 radix_tree_preload_end();
Rafael Aquinicbab0e42013-06-12 14:04:49 -0700379 /*
380 * We might race against get_swap_page() and stumble
381 * across a SWAP_HAS_CACHE swap_map entry whose page
Huang Ying9c1cc2e2017-05-03 14:54:33 -0700382 * has not been brought into the swapcache yet.
Rafael Aquinicbab0e42013-06-12 14:04:49 -0700383 */
384 cond_resched();
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700385 continue;
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700386 }
387 if (err) { /* swp entry is obsolete ? */
388 radix_tree_preload_end();
Hugh Dickinsf0009442008-02-04 22:28:49 -0800389 break;
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700390 }
Hugh Dickinsf0009442008-02-04 22:28:49 -0800391
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700392 /* May fail (-ENOMEM) if radix-tree node allocation failed. */
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800393 __SetPageLocked(new_page);
Hugh Dickinsfa9949d2016-05-19 17:12:41 -0700394 __SetPageSwapBacked(new_page);
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700395 err = __add_to_swap_cache(new_page, entry);
Nick Piggin529ae9a2008-08-02 12:01:03 +0200396 if (likely(!err)) {
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700397 radix_tree_preload_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /*
399 * Initiate read into locked page and return.
400 */
Rik van Rielc5fdae42008-10-18 20:26:36 -0700401 lru_cache_add_anon(new_page);
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700402 *new_page_allocated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return new_page;
404 }
Daisuke Nishimura31a56392009-09-21 17:02:50 -0700405 radix_tree_preload_end();
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800406 __ClearPageLocked(new_page);
Daisuke Nishimura2ca45322009-09-21 17:02:52 -0700407 /*
408 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
409 * clear SWAP_HAS_CACHE flag.
410 */
Minchan Kim75f6d6d2017-07-06 15:37:21 -0700411 put_swap_page(new_page, entry);
Hugh Dickinsf0009442008-02-04 22:28:49 -0800412 } while (err != -ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 if (new_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300415 put_page(new_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return found_page;
417}
Hugh Dickins46017e92008-02-04 22:28:41 -0800418
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700419/*
420 * Locate a page of swap in physical memory, reserving swap cache space
421 * and reading the disk if it is not already cached.
422 * A failure return means that either the page allocation failed or that
423 * the swap entry is no longer in use.
424 */
425struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
426 struct vm_area_struct *vma, unsigned long addr)
427{
428 bool page_was_allocated;
429 struct page *retpage = __read_swap_cache_async(entry, gfp_mask,
430 vma, addr, &page_was_allocated);
431
432 if (page_was_allocated)
433 swap_readpage(retpage);
434
435 return retpage;
436}
437
Shaohua Li579f8292014-02-06 12:04:21 -0800438static unsigned long swapin_nr_pages(unsigned long offset)
439{
440 static unsigned long prev_offset;
441 unsigned int pages, max_pages, last_ra;
442 static atomic_t last_readahead_pages;
443
Jason Low4db0c3c2015-04-15 16:14:08 -0700444 max_pages = 1 << READ_ONCE(page_cluster);
Shaohua Li579f8292014-02-06 12:04:21 -0800445 if (max_pages <= 1)
446 return 1;
447
448 /*
449 * This heuristic has been found to work well on both sequential and
450 * random loads, swapping to hard disk or to SSD: please don't ask
451 * what the "+ 2" means, it just happens to work well, that's all.
452 */
453 pages = atomic_xchg(&swapin_readahead_hits, 0) + 2;
454 if (pages == 2) {
455 /*
456 * We can have no readahead hits to judge by: but must not get
457 * stuck here forever, so check for an adjacent offset instead
458 * (and don't even bother to check whether swap type is same).
459 */
460 if (offset != prev_offset + 1 && offset != prev_offset - 1)
461 pages = 1;
462 prev_offset = offset;
463 } else {
464 unsigned int roundup = 4;
465 while (roundup < pages)
466 roundup <<= 1;
467 pages = roundup;
468 }
469
470 if (pages > max_pages)
471 pages = max_pages;
472
473 /* Don't shrink readahead too fast */
474 last_ra = atomic_read(&last_readahead_pages) / 2;
475 if (pages < last_ra)
476 pages = last_ra;
477 atomic_set(&last_readahead_pages, pages);
478
479 return pages;
480}
481
Hugh Dickins46017e92008-02-04 22:28:41 -0800482/**
483 * swapin_readahead - swap in pages in hope we need them soon
484 * @entry: swap entry of this memory
Randy Dunlap76824862008-03-19 17:00:40 -0700485 * @gfp_mask: memory allocation flags
Hugh Dickins46017e92008-02-04 22:28:41 -0800486 * @vma: user vma this address belongs to
487 * @addr: target address for mempolicy
488 *
489 * Returns the struct page for entry and addr, after queueing swapin.
490 *
491 * Primitive swap readahead code. We simply read an aligned block of
492 * (1 << page_cluster) entries in the swap area. This method is chosen
493 * because it doesn't cost us any seek time. We also make sure to queue
494 * the 'original' request together with the readahead ones...
495 *
496 * This has been extended to use the NUMA policies from the mm triggering
497 * the readahead.
498 *
499 * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
500 */
Hugh Dickins02098fe2008-02-04 22:28:42 -0800501struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
Hugh Dickins46017e92008-02-04 22:28:41 -0800502 struct vm_area_struct *vma, unsigned long addr)
503{
Hugh Dickins46017e92008-02-04 22:28:41 -0800504 struct page *page;
Shaohua Li579f8292014-02-06 12:04:21 -0800505 unsigned long entry_offset = swp_offset(entry);
506 unsigned long offset = entry_offset;
Rik van Riel67f96aa2012-03-21 16:33:50 -0700507 unsigned long start_offset, end_offset;
Shaohua Li579f8292014-02-06 12:04:21 -0800508 unsigned long mask;
Christian Ehrhardt3fb5c292012-07-31 16:41:44 -0700509 struct blk_plug plug;
Hugh Dickins46017e92008-02-04 22:28:41 -0800510
Shaohua Li579f8292014-02-06 12:04:21 -0800511 mask = swapin_nr_pages(offset) - 1;
512 if (!mask)
513 goto skip;
514
Rik van Riel67f96aa2012-03-21 16:33:50 -0700515 /* Read a page_cluster sized and aligned cluster around offset. */
516 start_offset = offset & ~mask;
517 end_offset = offset | mask;
518 if (!start_offset) /* First page is swap header. */
519 start_offset++;
520
Christian Ehrhardt3fb5c292012-07-31 16:41:44 -0700521 blk_start_plug(&plug);
Rik van Riel67f96aa2012-03-21 16:33:50 -0700522 for (offset = start_offset; offset <= end_offset ; offset++) {
Hugh Dickins46017e92008-02-04 22:28:41 -0800523 /* Ok, do the async read-ahead now */
524 page = read_swap_cache_async(swp_entry(swp_type(entry), offset),
Hugh Dickins02098fe2008-02-04 22:28:42 -0800525 gfp_mask, vma, addr);
Hugh Dickins46017e92008-02-04 22:28:41 -0800526 if (!page)
Rik van Riel67f96aa2012-03-21 16:33:50 -0700527 continue;
Huang Ying38d8b4e2017-07-06 15:37:18 -0700528 if (offset != entry_offset && likely(!PageTransCompound(page)))
Shaohua Li579f8292014-02-06 12:04:21 -0800529 SetPageReadahead(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300530 put_page(page);
Hugh Dickins46017e92008-02-04 22:28:41 -0800531 }
Christian Ehrhardt3fb5c292012-07-31 16:41:44 -0700532 blk_finish_plug(&plug);
533
Hugh Dickins46017e92008-02-04 22:28:41 -0800534 lru_add_drain(); /* Push any new pages onto the LRU now */
Shaohua Li579f8292014-02-06 12:04:21 -0800535skip:
Hugh Dickins02098fe2008-02-04 22:28:42 -0800536 return read_swap_cache_async(entry, gfp_mask, vma, addr);
Hugh Dickins46017e92008-02-04 22:28:41 -0800537}
Huang, Ying4b3ef9d2017-02-22 15:45:26 -0800538
539int init_swap_address_space(unsigned int type, unsigned long nr_pages)
540{
541 struct address_space *spaces, *space;
542 unsigned int i, nr;
543
544 nr = DIV_ROUND_UP(nr_pages, SWAP_ADDRESS_SPACE_PAGES);
Huang Ying54f180d2017-05-08 15:57:40 -0700545 spaces = kvzalloc(sizeof(struct address_space) * nr, GFP_KERNEL);
Huang, Ying4b3ef9d2017-02-22 15:45:26 -0800546 if (!spaces)
547 return -ENOMEM;
548 for (i = 0; i < nr; i++) {
549 space = spaces + i;
550 INIT_RADIX_TREE(&space->page_tree, GFP_ATOMIC|__GFP_NOWARN);
551 atomic_set(&space->i_mmap_writable, 0);
552 space->a_ops = &swap_aops;
553 /* swap cache doesn't use writeback related tags */
554 mapping_set_no_writeback_tags(space);
555 spin_lock_init(&space->tree_lock);
556 }
557 nr_swapper_spaces[type] = nr;
558 rcu_assign_pointer(swapper_spaces[type], spaces);
559
560 return 0;
561}
562
563void exit_swap_address_space(unsigned int type)
564{
565 struct address_space *spaces;
566
567 spaces = swapper_spaces[type];
568 nr_swapper_spaces[type] = 0;
569 rcu_assign_pointer(swapper_spaces[type], NULL);
570 synchronize_rcu();
571 kvfree(spaces);
572}