swap_state.c 22 KB

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