swap_state.c 22 KB

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