swap_state.c 22 KB

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