swap_state.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. // SPDX-License-Identifier: GPL-2.0
  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. */
  10. #include <linux/mm.h>
  11. #include <linux/gfp.h>
  12. #include <linux/kernel_stat.h>
  13. #include <linux/swap.h>
  14. #include <linux/swapops.h>
  15. #include <linux/init.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/backing-dev.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/pagevec.h>
  20. #include <linux/migrate.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/swap_slots.h>
  23. #include <linux/huge_mm.h>
  24. #include <asm/pgtable.h>
  25. #include "internal.h"
  26. /*
  27. * swapper_space is a fiction, retained to simplify the path through
  28. * vmscan's shrink_page_list.
  29. */
  30. static const struct address_space_operations swap_aops = {
  31. .writepage = swap_writepage,
  32. .set_page_dirty = swap_set_page_dirty,
  33. #ifdef CONFIG_MIGRATION
  34. .migratepage = migrate_page,
  35. #endif
  36. };
  37. struct address_space *swapper_spaces[MAX_SWAPFILES] __read_mostly;
  38. static unsigned int nr_swapper_spaces[MAX_SWAPFILES] __read_mostly;
  39. static bool enable_vma_readahead __read_mostly = true;
  40. #define SWAP_RA_WIN_SHIFT (PAGE_SHIFT / 2)
  41. #define SWAP_RA_HITS_MASK ((1UL << SWAP_RA_WIN_SHIFT) - 1)
  42. #define SWAP_RA_HITS_MAX SWAP_RA_HITS_MASK
  43. #define SWAP_RA_WIN_MASK (~PAGE_MASK & ~SWAP_RA_HITS_MASK)
  44. #define SWAP_RA_HITS(v) ((v) & SWAP_RA_HITS_MASK)
  45. #define SWAP_RA_WIN(v) (((v) & SWAP_RA_WIN_MASK) >> SWAP_RA_WIN_SHIFT)
  46. #define SWAP_RA_ADDR(v) ((v) & PAGE_MASK)
  47. #define SWAP_RA_VAL(addr, win, hits) \
  48. (((addr) & PAGE_MASK) | \
  49. (((win) << SWAP_RA_WIN_SHIFT) & SWAP_RA_WIN_MASK) | \
  50. ((hits) & SWAP_RA_HITS_MASK))
  51. /* Initial readahead hits is 4 to start up with a small window */
  52. #define GET_SWAP_RA_VAL(vma) \
  53. (atomic_long_read(&(vma)->swap_readahead_info) ? : 4)
  54. #define INC_CACHE_INFO(x) do { swap_cache_info.x++; } while (0)
  55. #define ADD_CACHE_INFO(x, nr) do { swap_cache_info.x += (nr); } while (0)
  56. static struct {
  57. unsigned long add_total;
  58. unsigned long del_total;
  59. unsigned long find_success;
  60. unsigned long find_total;
  61. } swap_cache_info;
  62. unsigned long total_swapcache_pages(void)
  63. {
  64. unsigned int i, j, nr;
  65. unsigned long ret = 0;
  66. struct address_space *spaces;
  67. rcu_read_lock();
  68. for (i = 0; i < MAX_SWAPFILES; i++) {
  69. /*
  70. * The corresponding entries in nr_swapper_spaces and
  71. * swapper_spaces will be reused only after at least
  72. * one grace period. So it is impossible for them
  73. * belongs to different usage.
  74. */
  75. nr = nr_swapper_spaces[i];
  76. spaces = rcu_dereference(swapper_spaces[i]);
  77. if (!nr || !spaces)
  78. continue;
  79. for (j = 0; j < nr; j++)
  80. ret += spaces[j].nrpages;
  81. }
  82. rcu_read_unlock();
  83. return ret;
  84. }
  85. static atomic_t swapin_readahead_hits = ATOMIC_INIT(4);
  86. void show_swap_cache_info(void)
  87. {
  88. printk("%lu pages in swap cache\n", total_swapcache_pages());
  89. printk("Swap cache stats: add %lu, delete %lu, find %lu/%lu\n",
  90. swap_cache_info.add_total, swap_cache_info.del_total,
  91. swap_cache_info.find_success, swap_cache_info.find_total);
  92. printk("Free swap = %ldkB\n",
  93. get_nr_swap_pages() << (PAGE_SHIFT - 10));
  94. printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10));
  95. }
  96. /*
  97. * __add_to_swap_cache resembles add_to_page_cache_locked on swapper_space,
  98. * but sets SwapCache flag and private instead of mapping and index.
  99. */
  100. int __add_to_swap_cache(struct page *page, swp_entry_t entry)
  101. {
  102. int error, i, nr = hpage_nr_pages(page);
  103. struct address_space *address_space;
  104. pgoff_t idx = swp_offset(entry);
  105. VM_BUG_ON_PAGE(!PageLocked(page), page);
  106. VM_BUG_ON_PAGE(PageSwapCache(page), page);
  107. VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
  108. page_ref_add(page, nr);
  109. SetPageSwapCache(page);
  110. address_space = swap_address_space(entry);
  111. xa_lock_irq(&address_space->i_pages);
  112. for (i = 0; i < nr; i++) {
  113. set_page_private(page + i, entry.val + i);
  114. error = radix_tree_insert(&address_space->i_pages,
  115. idx + i, page + i);
  116. if (unlikely(error))
  117. break;
  118. }
  119. if (likely(!error)) {
  120. address_space->nrpages += nr;
  121. __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
  122. ADD_CACHE_INFO(add_total, nr);
  123. } else {
  124. /*
  125. * Only the context which have set SWAP_HAS_CACHE flag
  126. * would call add_to_swap_cache().
  127. * So add_to_swap_cache() doesn't returns -EEXIST.
  128. */
  129. VM_BUG_ON(error == -EEXIST);
  130. set_page_private(page + i, 0UL);
  131. while (i--) {
  132. radix_tree_delete(&address_space->i_pages, idx + i);
  133. set_page_private(page + i, 0UL);
  134. }
  135. ClearPageSwapCache(page);
  136. page_ref_sub(page, nr);
  137. }
  138. xa_unlock_irq(&address_space->i_pages);
  139. return error;
  140. }
  141. int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp_mask)
  142. {
  143. int error;
  144. error = radix_tree_maybe_preload_order(gfp_mask, compound_order(page));
  145. if (!error) {
  146. error = __add_to_swap_cache(page, entry);
  147. radix_tree_preload_end();
  148. }
  149. return error;
  150. }
  151. /*
  152. * This must be called only on pages that have
  153. * been verified to be in the swap cache.
  154. */
  155. void __delete_from_swap_cache(struct page *page)
  156. {
  157. struct address_space *address_space;
  158. int i, nr = hpage_nr_pages(page);
  159. swp_entry_t entry;
  160. pgoff_t idx;
  161. VM_BUG_ON_PAGE(!PageLocked(page), page);
  162. VM_BUG_ON_PAGE(!PageSwapCache(page), page);
  163. VM_BUG_ON_PAGE(PageWriteback(page), page);
  164. entry.val = page_private(page);
  165. address_space = swap_address_space(entry);
  166. idx = swp_offset(entry);
  167. for (i = 0; i < nr; i++) {
  168. radix_tree_delete(&address_space->i_pages, idx + i);
  169. set_page_private(page + i, 0);
  170. }
  171. ClearPageSwapCache(page);
  172. address_space->nrpages -= nr;
  173. __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
  174. ADD_CACHE_INFO(del_total, nr);
  175. }
  176. /**
  177. * add_to_swap - allocate swap space for a page
  178. * @page: page we want to move to swap
  179. *
  180. * Allocate swap space for the page and add the page to the
  181. * swap cache. Caller needs to hold the page lock.
  182. */
  183. int add_to_swap(struct page *page)
  184. {
  185. swp_entry_t entry;
  186. int err;
  187. VM_BUG_ON_PAGE(!PageLocked(page), page);
  188. VM_BUG_ON_PAGE(!PageUptodate(page), page);
  189. entry = get_swap_page(page);
  190. if (!entry.val)
  191. return 0;
  192. /*
  193. * Radix-tree node allocations from PF_MEMALLOC contexts could
  194. * completely exhaust the page allocator. __GFP_NOMEMALLOC
  195. * stops emergency reserves from being allocated.
  196. *
  197. * TODO: this could cause a theoretical memory reclaim
  198. * deadlock in the swap out path.
  199. */
  200. /*
  201. * Add it to the swap cache.
  202. */
  203. err = add_to_swap_cache(page, entry,
  204. __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN);
  205. /* -ENOMEM radix-tree allocation failure */
  206. if (err)
  207. /*
  208. * add_to_swap_cache() doesn't return -EEXIST, so we can safely
  209. * clear SWAP_HAS_CACHE flag.
  210. */
  211. goto fail;
  212. /*
  213. * Normally the page will be dirtied in unmap because its pte should be
  214. * dirty. A special case is MADV_FREE page. The page'e pte could have
  215. * dirty bit cleared but the page's SwapBacked bit is still set because
  216. * clearing the dirty bit and SwapBacked bit has no lock protected. For
  217. * such page, unmap will not set dirty bit for it, so page reclaim will
  218. * not write the page out. This can cause data corruption when the page
  219. * is swap in later. Always setting the dirty bit for the page solves
  220. * the problem.
  221. */
  222. set_page_dirty(page);
  223. return 1;
  224. fail:
  225. put_swap_page(page, entry);
  226. return 0;
  227. }
  228. /*
  229. * This must be called only on pages that have
  230. * been verified to be in the swap cache and locked.
  231. * It will never put the page into the free list,
  232. * the caller has a reference on the page.
  233. */
  234. void delete_from_swap_cache(struct page *page)
  235. {
  236. swp_entry_t entry;
  237. struct address_space *address_space;
  238. entry.val = page_private(page);
  239. address_space = swap_address_space(entry);
  240. xa_lock_irq(&address_space->i_pages);
  241. __delete_from_swap_cache(page);
  242. xa_unlock_irq(&address_space->i_pages);
  243. put_swap_page(page, entry);
  244. page_ref_sub(page, hpage_nr_pages(page));
  245. }
  246. /*
  247. * If we are the only user, then try to free up the swap cache.
  248. *
  249. * Its ok to check for PageSwapCache without the page lock
  250. * here because we are going to recheck again inside
  251. * try_to_free_swap() _with_ the lock.
  252. * - Marcelo
  253. */
  254. static inline void free_swap_cache(struct page *page)
  255. {
  256. if (PageSwapCache(page) && !page_mapped(page) && trylock_page(page)) {
  257. try_to_free_swap(page);
  258. unlock_page(page);
  259. }
  260. }
  261. /*
  262. * Perform a free_page(), also freeing any swap cache associated with
  263. * this page if it is the last user of the page.
  264. */
  265. void free_page_and_swap_cache(struct page *page)
  266. {
  267. free_swap_cache(page);
  268. if (!is_huge_zero_page(page))
  269. put_page(page);
  270. }
  271. /*
  272. * Passed an array of pages, drop them all from swapcache and then release
  273. * them. They are removed from the LRU and freed if this is their last use.
  274. */
  275. void free_pages_and_swap_cache(struct page **pages, int nr)
  276. {
  277. struct page **pagep = pages;
  278. int i;
  279. lru_add_drain();
  280. for (i = 0; i < nr; i++)
  281. free_swap_cache(pagep[i]);
  282. release_pages(pagep, nr);
  283. }
  284. static inline bool swap_use_vma_readahead(void)
  285. {
  286. return READ_ONCE(enable_vma_readahead) && !atomic_read(&nr_rotate_swap);
  287. }
  288. /*
  289. * Lookup a swap entry in the swap cache. A found page will be returned
  290. * unlocked and with its refcount incremented - we rely on the kernel
  291. * lock getting page table operations atomic even if we drop the page
  292. * lock before returning.
  293. */
  294. struct page *lookup_swap_cache(swp_entry_t entry, struct vm_area_struct *vma,
  295. unsigned long addr)
  296. {
  297. struct page *page;
  298. page = find_get_page(swap_address_space(entry), swp_offset(entry));
  299. INC_CACHE_INFO(find_total);
  300. if (page) {
  301. bool vma_ra = swap_use_vma_readahead();
  302. bool readahead;
  303. INC_CACHE_INFO(find_success);
  304. /*
  305. * At the moment, we don't support PG_readahead for anon THP
  306. * so let's bail out rather than confusing the readahead stat.
  307. */
  308. if (unlikely(PageTransCompound(page)))
  309. return page;
  310. readahead = TestClearPageReadahead(page);
  311. if (vma && vma_ra) {
  312. unsigned long ra_val;
  313. int win, hits;
  314. ra_val = GET_SWAP_RA_VAL(vma);
  315. win = SWAP_RA_WIN(ra_val);
  316. hits = SWAP_RA_HITS(ra_val);
  317. if (readahead)
  318. hits = min_t(int, hits + 1, SWAP_RA_HITS_MAX);
  319. atomic_long_set(&vma->swap_readahead_info,
  320. SWAP_RA_VAL(addr, win, hits));
  321. }
  322. if (readahead) {
  323. count_vm_event(SWAP_RA_HIT);
  324. if (!vma || !vma_ra)
  325. atomic_inc(&swapin_readahead_hits);
  326. }
  327. }
  328. return page;
  329. }
  330. struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
  331. struct vm_area_struct *vma, unsigned long addr,
  332. bool *new_page_allocated)
  333. {
  334. struct page *found_page, *new_page = NULL;
  335. struct address_space *swapper_space = swap_address_space(entry);
  336. int err;
  337. *new_page_allocated = false;
  338. do {
  339. /*
  340. * First check the swap cache. Since this is normally
  341. * called after lookup_swap_cache() failed, re-calling
  342. * that would confuse statistics.
  343. */
  344. found_page = find_get_page(swapper_space, swp_offset(entry));
  345. if (found_page)
  346. break;
  347. /*
  348. * Just skip read ahead for unused swap slot.
  349. * During swap_off when swap_slot_cache is disabled,
  350. * we have to handle the race between putting
  351. * swap entry in swap cache and marking swap slot
  352. * as SWAP_HAS_CACHE. That's done in later part of code or
  353. * else swap_off will be aborted if we return NULL.
  354. */
  355. if (!__swp_swapcount(entry) && swap_slot_cache_enabled)
  356. break;
  357. /*
  358. * Get a new page to read into from swap.
  359. */
  360. if (!new_page) {
  361. new_page = alloc_page_vma(gfp_mask, vma, addr);
  362. if (!new_page)
  363. break; /* Out of memory */
  364. }
  365. /*
  366. * call radix_tree_preload() while we can wait.
  367. */
  368. err = radix_tree_maybe_preload(gfp_mask & GFP_RECLAIM_MASK);
  369. if (err)
  370. break;
  371. /*
  372. * Swap entry may have been freed since our caller observed it.
  373. */
  374. err = swapcache_prepare(entry);
  375. if (err == -EEXIST) {
  376. radix_tree_preload_end();
  377. /*
  378. * We might race against get_swap_page() and stumble
  379. * across a SWAP_HAS_CACHE swap_map entry whose page
  380. * has not been brought into the swapcache yet.
  381. */
  382. cond_resched();
  383. continue;
  384. }
  385. if (err) { /* swp entry is obsolete ? */
  386. radix_tree_preload_end();
  387. break;
  388. }
  389. /* May fail (-ENOMEM) if radix-tree node allocation failed. */
  390. __SetPageLocked(new_page);
  391. __SetPageSwapBacked(new_page);
  392. err = __add_to_swap_cache(new_page, entry);
  393. if (likely(!err)) {
  394. radix_tree_preload_end();
  395. /*
  396. * Initiate read into locked page and return.
  397. */
  398. lru_cache_add_anon(new_page);
  399. *new_page_allocated = true;
  400. return new_page;
  401. }
  402. radix_tree_preload_end();
  403. __ClearPageLocked(new_page);
  404. /*
  405. * add_to_swap_cache() doesn't return -EEXIST, so we can safely
  406. * clear SWAP_HAS_CACHE flag.
  407. */
  408. put_swap_page(new_page, entry);
  409. } while (err != -ENOMEM);
  410. if (new_page)
  411. put_page(new_page);
  412. return found_page;
  413. }
  414. /*
  415. * Locate a page of swap in physical memory, reserving swap cache space
  416. * and reading the disk if it is not already cached.
  417. * A failure return means that either the page allocation failed or that
  418. * the swap entry is no longer in use.
  419. */
  420. struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
  421. struct vm_area_struct *vma, unsigned long addr, bool do_poll)
  422. {
  423. bool page_was_allocated;
  424. struct page *retpage = __read_swap_cache_async(entry, gfp_mask,
  425. vma, addr, &page_was_allocated);
  426. if (page_was_allocated)
  427. swap_readpage(retpage, do_poll);
  428. return retpage;
  429. }
  430. static unsigned int __swapin_nr_pages(unsigned long prev_offset,
  431. unsigned long offset,
  432. int hits,
  433. int max_pages,
  434. int prev_win)
  435. {
  436. unsigned int pages, last_ra;
  437. /*
  438. * This heuristic has been found to work well on both sequential and
  439. * random loads, swapping to hard disk or to SSD: please don't ask
  440. * what the "+ 2" means, it just happens to work well, that's all.
  441. */
  442. pages = hits + 2;
  443. if (pages == 2) {
  444. /*
  445. * We can have no readahead hits to judge by: but must not get
  446. * stuck here forever, so check for an adjacent offset instead
  447. * (and don't even bother to check whether swap type is same).
  448. */
  449. if (offset != prev_offset + 1 && offset != prev_offset - 1)
  450. pages = 1;
  451. } else {
  452. unsigned int roundup = 4;
  453. while (roundup < pages)
  454. roundup <<= 1;
  455. pages = roundup;
  456. }
  457. if (pages > max_pages)
  458. pages = max_pages;
  459. /* Don't shrink readahead too fast */
  460. last_ra = prev_win / 2;
  461. if (pages < last_ra)
  462. pages = last_ra;
  463. return pages;
  464. }
  465. static unsigned long swapin_nr_pages(unsigned long offset)
  466. {
  467. static unsigned long prev_offset;
  468. unsigned int hits, pages, max_pages;
  469. static atomic_t last_readahead_pages;
  470. max_pages = 1 << READ_ONCE(page_cluster);
  471. if (max_pages <= 1)
  472. return 1;
  473. hits = atomic_xchg(&swapin_readahead_hits, 0);
  474. pages = __swapin_nr_pages(READ_ONCE(prev_offset), offset, hits,
  475. max_pages,
  476. atomic_read(&last_readahead_pages));
  477. if (!hits)
  478. WRITE_ONCE(prev_offset, offset);
  479. atomic_set(&last_readahead_pages, pages);
  480. return pages;
  481. }
  482. /**
  483. * swap_cluster_readahead - swap in pages in hope we need them soon
  484. * @entry: swap entry of this memory
  485. * @gfp_mask: memory allocation flags
  486. * @vmf: fault information
  487. *
  488. * Returns the struct page for entry and addr, after queueing swapin.
  489. *
  490. * Primitive swap readahead code. We simply read an aligned block of
  491. * (1 << page_cluster) entries in the swap area. This method is chosen
  492. * because it doesn't cost us any seek time. We also make sure to queue
  493. * the 'original' request together with the readahead ones...
  494. *
  495. * This has been extended to use the NUMA policies from the mm triggering
  496. * the readahead.
  497. *
  498. * Caller must hold down_read on the vma->vm_mm if vmf->vma is not NULL.
  499. */
  500. struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
  501. struct vm_fault *vmf)
  502. {
  503. struct page *page;
  504. unsigned long entry_offset = swp_offset(entry);
  505. unsigned long offset = entry_offset;
  506. unsigned long start_offset, end_offset;
  507. unsigned long mask;
  508. struct swap_info_struct *si = swp_swap_info(entry);
  509. struct blk_plug plug;
  510. bool do_poll = true, page_allocated;
  511. struct vm_area_struct *vma = vmf->vma;
  512. unsigned long addr = vmf->address;
  513. mask = swapin_nr_pages(offset) - 1;
  514. if (!mask)
  515. goto skip;
  516. do_poll = false;
  517. /* Read a page_cluster sized and aligned cluster around offset. */
  518. start_offset = offset & ~mask;
  519. end_offset = offset | mask;
  520. if (!start_offset) /* First page is swap header. */
  521. start_offset++;
  522. if (end_offset >= si->max)
  523. end_offset = si->max - 1;
  524. blk_start_plug(&plug);
  525. for (offset = start_offset; offset <= end_offset ; offset++) {
  526. /* Ok, do the async read-ahead now */
  527. page = __read_swap_cache_async(
  528. swp_entry(swp_type(entry), offset),
  529. gfp_mask, vma, addr, &page_allocated);
  530. if (!page)
  531. continue;
  532. if (page_allocated) {
  533. swap_readpage(page, false);
  534. if (offset != entry_offset) {
  535. SetPageReadahead(page);
  536. count_vm_event(SWAP_RA);
  537. }
  538. }
  539. put_page(page);
  540. }
  541. blk_finish_plug(&plug);
  542. lru_add_drain(); /* Push any new pages onto the LRU now */
  543. skip:
  544. return read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll);
  545. }
  546. int init_swap_address_space(unsigned int type, unsigned long nr_pages)
  547. {
  548. struct address_space *spaces, *space;
  549. unsigned int i, nr;
  550. nr = DIV_ROUND_UP(nr_pages, SWAP_ADDRESS_SPACE_PAGES);
  551. spaces = kvcalloc(nr, sizeof(struct address_space), GFP_KERNEL);
  552. if (!spaces)
  553. return -ENOMEM;
  554. for (i = 0; i < nr; i++) {
  555. space = spaces + i;
  556. INIT_RADIX_TREE(&space->i_pages, GFP_ATOMIC|__GFP_NOWARN);
  557. atomic_set(&space->i_mmap_writable, 0);
  558. space->a_ops = &swap_aops;
  559. /* swap cache doesn't use writeback related tags */
  560. mapping_set_no_writeback_tags(space);
  561. }
  562. nr_swapper_spaces[type] = nr;
  563. rcu_assign_pointer(swapper_spaces[type], spaces);
  564. return 0;
  565. }
  566. void exit_swap_address_space(unsigned int type)
  567. {
  568. struct address_space *spaces;
  569. spaces = swapper_spaces[type];
  570. nr_swapper_spaces[type] = 0;
  571. rcu_assign_pointer(swapper_spaces[type], NULL);
  572. synchronize_rcu();
  573. kvfree(spaces);
  574. }
  575. static inline void swap_ra_clamp_pfn(struct vm_area_struct *vma,
  576. unsigned long faddr,
  577. unsigned long lpfn,
  578. unsigned long rpfn,
  579. unsigned long *start,
  580. unsigned long *end)
  581. {
  582. *start = max3(lpfn, PFN_DOWN(vma->vm_start),
  583. PFN_DOWN(faddr & PMD_MASK));
  584. *end = min3(rpfn, PFN_DOWN(vma->vm_end),
  585. PFN_DOWN((faddr & PMD_MASK) + PMD_SIZE));
  586. }
  587. static void swap_ra_info(struct vm_fault *vmf,
  588. struct vma_swap_readahead *ra_info)
  589. {
  590. struct vm_area_struct *vma = vmf->vma;
  591. unsigned long ra_val;
  592. swp_entry_t entry;
  593. unsigned long faddr, pfn, fpfn;
  594. unsigned long start, end;
  595. pte_t *pte, *orig_pte;
  596. unsigned int max_win, hits, prev_win, win, left;
  597. #ifndef CONFIG_64BIT
  598. pte_t *tpte;
  599. #endif
  600. max_win = 1 << min_t(unsigned int, READ_ONCE(page_cluster),
  601. SWAP_RA_ORDER_CEILING);
  602. if (max_win == 1) {
  603. ra_info->win = 1;
  604. return;
  605. }
  606. faddr = vmf->address;
  607. orig_pte = pte = pte_offset_map(vmf->pmd, faddr);
  608. entry = pte_to_swp_entry(*pte);
  609. if ((unlikely(non_swap_entry(entry)))) {
  610. pte_unmap(orig_pte);
  611. return;
  612. }
  613. fpfn = PFN_DOWN(faddr);
  614. ra_val = GET_SWAP_RA_VAL(vma);
  615. pfn = PFN_DOWN(SWAP_RA_ADDR(ra_val));
  616. prev_win = SWAP_RA_WIN(ra_val);
  617. hits = SWAP_RA_HITS(ra_val);
  618. ra_info->win = win = __swapin_nr_pages(pfn, fpfn, hits,
  619. max_win, prev_win);
  620. atomic_long_set(&vma->swap_readahead_info,
  621. SWAP_RA_VAL(faddr, win, 0));
  622. if (win == 1) {
  623. pte_unmap(orig_pte);
  624. return;
  625. }
  626. /* Copy the PTEs because the page table may be unmapped */
  627. if (fpfn == pfn + 1)
  628. swap_ra_clamp_pfn(vma, faddr, fpfn, fpfn + win, &start, &end);
  629. else if (pfn == fpfn + 1)
  630. swap_ra_clamp_pfn(vma, faddr, fpfn - win + 1, fpfn + 1,
  631. &start, &end);
  632. else {
  633. left = (win - 1) / 2;
  634. swap_ra_clamp_pfn(vma, faddr, fpfn - left, fpfn + win - left,
  635. &start, &end);
  636. }
  637. ra_info->nr_pte = end - start;
  638. ra_info->offset = fpfn - start;
  639. pte -= ra_info->offset;
  640. #ifdef CONFIG_64BIT
  641. ra_info->ptes = pte;
  642. #else
  643. tpte = ra_info->ptes;
  644. for (pfn = start; pfn != end; pfn++)
  645. *tpte++ = *pte++;
  646. #endif
  647. pte_unmap(orig_pte);
  648. }
  649. static struct page *swap_vma_readahead(swp_entry_t fentry, gfp_t gfp_mask,
  650. struct vm_fault *vmf)
  651. {
  652. struct blk_plug plug;
  653. struct vm_area_struct *vma = vmf->vma;
  654. struct page *page;
  655. pte_t *pte, pentry;
  656. swp_entry_t entry;
  657. unsigned int i;
  658. bool page_allocated;
  659. struct vma_swap_readahead ra_info = {0,};
  660. swap_ra_info(vmf, &ra_info);
  661. if (ra_info.win == 1)
  662. goto skip;
  663. blk_start_plug(&plug);
  664. for (i = 0, pte = ra_info.ptes; i < ra_info.nr_pte;
  665. i++, pte++) {
  666. pentry = *pte;
  667. if (pte_none(pentry))
  668. continue;
  669. if (pte_present(pentry))
  670. continue;
  671. entry = pte_to_swp_entry(pentry);
  672. if (unlikely(non_swap_entry(entry)))
  673. continue;
  674. page = __read_swap_cache_async(entry, gfp_mask, vma,
  675. vmf->address, &page_allocated);
  676. if (!page)
  677. continue;
  678. if (page_allocated) {
  679. swap_readpage(page, false);
  680. if (i != ra_info.offset) {
  681. SetPageReadahead(page);
  682. count_vm_event(SWAP_RA);
  683. }
  684. }
  685. put_page(page);
  686. }
  687. blk_finish_plug(&plug);
  688. lru_add_drain();
  689. skip:
  690. return read_swap_cache_async(fentry, gfp_mask, vma, vmf->address,
  691. ra_info.win == 1);
  692. }
  693. /**
  694. * swapin_readahead - swap in pages in hope we need them soon
  695. * @entry: swap entry of this memory
  696. * @gfp_mask: memory allocation flags
  697. * @vmf: fault information
  698. *
  699. * Returns the struct page for entry and addr, after queueing swapin.
  700. *
  701. * It's a main entry function for swap readahead. By the configuration,
  702. * it will read ahead blocks by cluster-based(ie, physical disk based)
  703. * or vma-based(ie, virtual address based on faulty address) readahead.
  704. */
  705. struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
  706. struct vm_fault *vmf)
  707. {
  708. return swap_use_vma_readahead() ?
  709. swap_vma_readahead(entry, gfp_mask, vmf) :
  710. swap_cluster_readahead(entry, gfp_mask, vmf);
  711. }
  712. #ifdef CONFIG_SYSFS
  713. static ssize_t vma_ra_enabled_show(struct kobject *kobj,
  714. struct kobj_attribute *attr, char *buf)
  715. {
  716. return sprintf(buf, "%s\n", enable_vma_readahead ? "true" : "false");
  717. }
  718. static ssize_t vma_ra_enabled_store(struct kobject *kobj,
  719. struct kobj_attribute *attr,
  720. const char *buf, size_t count)
  721. {
  722. if (!strncmp(buf, "true", 4) || !strncmp(buf, "1", 1))
  723. enable_vma_readahead = true;
  724. else if (!strncmp(buf, "false", 5) || !strncmp(buf, "0", 1))
  725. enable_vma_readahead = false;
  726. else
  727. return -EINVAL;
  728. return count;
  729. }
  730. static struct kobj_attribute vma_ra_enabled_attr =
  731. __ATTR(vma_ra_enabled, 0644, vma_ra_enabled_show,
  732. vma_ra_enabled_store);
  733. static struct attribute *swap_attrs[] = {
  734. &vma_ra_enabled_attr.attr,
  735. NULL,
  736. };
  737. static struct attribute_group swap_attr_group = {
  738. .attrs = swap_attrs,
  739. };
  740. static int __init swap_init_sysfs(void)
  741. {
  742. int err;
  743. struct kobject *swap_kobj;
  744. swap_kobj = kobject_create_and_add("swap", mm_kobj);
  745. if (!swap_kobj) {
  746. pr_err("failed to create swap kobject\n");
  747. return -ENOMEM;
  748. }
  749. err = sysfs_create_group(swap_kobj, &swap_attr_group);
  750. if (err) {
  751. pr_err("failed to register swap group\n");
  752. goto delete_obj;
  753. }
  754. return 0;
  755. delete_obj:
  756. kobject_put(swap_kobj);
  757. return err;
  758. }
  759. subsys_initcall(swap_init_sysfs);
  760. #endif