swap_state.c 24 KB

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