swap_state.c 22 KB

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