swap_state.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * linux/mm/swap_state.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. * Swap reorganised 29.12.95, Stephen Tweedie
  6. *
  7. * Rewritten to use page cache, (C) 1998 Stephen Tweedie
  8. */
  9. #include <linux/mm.h>
  10. #include <linux/gfp.h>
  11. #include <linux/kernel_stat.h>
  12. #include <linux/swap.h>
  13. #include <linux/swapops.h>
  14. #include <linux/init.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/backing-dev.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/pagevec.h>
  19. #include <linux/migrate.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/swap_slots.h>
  22. #include <linux/huge_mm.h>
  23. #include <asm/pgtable.h>
  24. /*
  25. * swapper_space is a fiction, retained to simplify the path through
  26. * vmscan's shrink_page_list.
  27. */
  28. static const struct address_space_operations swap_aops = {
  29. .writepage = swap_writepage,
  30. .set_page_dirty = swap_set_page_dirty,
  31. #ifdef CONFIG_MIGRATION
  32. .migratepage = migrate_page,
  33. #endif
  34. };
  35. struct address_space *swapper_spaces[MAX_SWAPFILES];
  36. static unsigned int nr_swapper_spaces[MAX_SWAPFILES];
  37. #define INC_CACHE_INFO(x) do { swap_cache_info.x++; } while (0)
  38. #define ADD_CACHE_INFO(x, nr) do { swap_cache_info.x += (nr); } while (0)
  39. static struct {
  40. unsigned long add_total;
  41. unsigned long del_total;
  42. unsigned long find_success;
  43. unsigned long find_total;
  44. } swap_cache_info;
  45. unsigned long total_swapcache_pages(void)
  46. {
  47. unsigned int i, j, nr;
  48. unsigned long ret = 0;
  49. struct address_space *spaces;
  50. rcu_read_lock();
  51. for (i = 0; i < MAX_SWAPFILES; i++) {
  52. /*
  53. * The corresponding entries in nr_swapper_spaces and
  54. * swapper_spaces will be reused only after at least
  55. * one grace period. So it is impossible for them
  56. * belongs to different usage.
  57. */
  58. nr = nr_swapper_spaces[i];
  59. spaces = rcu_dereference(swapper_spaces[i]);
  60. if (!nr || !spaces)
  61. continue;
  62. for (j = 0; j < nr; j++)
  63. ret += spaces[j].nrpages;
  64. }
  65. rcu_read_unlock();
  66. return ret;
  67. }
  68. static atomic_t swapin_readahead_hits = ATOMIC_INIT(4);
  69. void show_swap_cache_info(void)
  70. {
  71. printk("%lu pages in swap cache\n", total_swapcache_pages());
  72. printk("Swap cache stats: add %lu, delete %lu, find %lu/%lu\n",
  73. swap_cache_info.add_total, swap_cache_info.del_total,
  74. swap_cache_info.find_success, swap_cache_info.find_total);
  75. printk("Free swap = %ldkB\n",
  76. get_nr_swap_pages() << (PAGE_SHIFT - 10));
  77. printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10));
  78. }
  79. /*
  80. * __add_to_swap_cache resembles add_to_page_cache_locked on swapper_space,
  81. * but sets SwapCache flag and private instead of mapping and index.
  82. */
  83. int __add_to_swap_cache(struct page *page, swp_entry_t entry)
  84. {
  85. int error, i, nr = hpage_nr_pages(page);
  86. struct address_space *address_space;
  87. pgoff_t idx = swp_offset(entry);
  88. VM_BUG_ON_PAGE(!PageLocked(page), page);
  89. VM_BUG_ON_PAGE(PageSwapCache(page), page);
  90. VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
  91. page_ref_add(page, nr);
  92. SetPageSwapCache(page);
  93. address_space = swap_address_space(entry);
  94. spin_lock_irq(&address_space->tree_lock);
  95. for (i = 0; i < nr; i++) {
  96. set_page_private(page + i, entry.val + i);
  97. error = radix_tree_insert(&address_space->page_tree,
  98. idx + i, page + i);
  99. if (unlikely(error))
  100. break;
  101. }
  102. if (likely(!error)) {
  103. address_space->nrpages += nr;
  104. __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
  105. ADD_CACHE_INFO(add_total, nr);
  106. } else {
  107. /*
  108. * Only the context which have set SWAP_HAS_CACHE flag
  109. * would call add_to_swap_cache().
  110. * So add_to_swap_cache() doesn't returns -EEXIST.
  111. */
  112. VM_BUG_ON(error == -EEXIST);
  113. set_page_private(page + i, 0UL);
  114. while (i--) {
  115. radix_tree_delete(&address_space->page_tree, idx + i);
  116. set_page_private(page + i, 0UL);
  117. }
  118. ClearPageSwapCache(page);
  119. page_ref_sub(page, nr);
  120. }
  121. spin_unlock_irq(&address_space->tree_lock);
  122. return error;
  123. }
  124. int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp_mask)
  125. {
  126. int error;
  127. error = radix_tree_maybe_preload_order(gfp_mask, compound_order(page));
  128. if (!error) {
  129. error = __add_to_swap_cache(page, entry);
  130. radix_tree_preload_end();
  131. }
  132. return error;
  133. }
  134. /*
  135. * This must be called only on pages that have
  136. * been verified to be in the swap cache.
  137. */
  138. void __delete_from_swap_cache(struct page *page)
  139. {
  140. struct address_space *address_space;
  141. int i, nr = hpage_nr_pages(page);
  142. swp_entry_t entry;
  143. pgoff_t idx;
  144. VM_BUG_ON_PAGE(!PageLocked(page), page);
  145. VM_BUG_ON_PAGE(!PageSwapCache(page), page);
  146. VM_BUG_ON_PAGE(PageWriteback(page), page);
  147. entry.val = page_private(page);
  148. address_space = swap_address_space(entry);
  149. idx = swp_offset(entry);
  150. for (i = 0; i < nr; i++) {
  151. radix_tree_delete(&address_space->page_tree, idx + i);
  152. set_page_private(page + i, 0);
  153. }
  154. ClearPageSwapCache(page);
  155. address_space->nrpages -= nr;
  156. __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
  157. ADD_CACHE_INFO(del_total, nr);
  158. }
  159. /**
  160. * add_to_swap - allocate swap space for a page
  161. * @page: page we want to move to swap
  162. *
  163. * Allocate swap space for the page and add the page to the
  164. * swap cache. Caller needs to hold the page lock.
  165. */
  166. int add_to_swap(struct page *page)
  167. {
  168. swp_entry_t entry;
  169. int err;
  170. VM_BUG_ON_PAGE(!PageLocked(page), page);
  171. VM_BUG_ON_PAGE(!PageUptodate(page), page);
  172. entry = get_swap_page(page);
  173. if (!entry.val)
  174. return 0;
  175. if (mem_cgroup_try_charge_swap(page, entry))
  176. goto fail;
  177. /*
  178. * Radix-tree node allocations from PF_MEMALLOC contexts could
  179. * completely exhaust the page allocator. __GFP_NOMEMALLOC
  180. * stops emergency reserves from being allocated.
  181. *
  182. * TODO: this could cause a theoretical memory reclaim
  183. * deadlock in the swap out path.
  184. */
  185. /*
  186. * Add it to the swap cache.
  187. */
  188. err = add_to_swap_cache(page, entry,
  189. __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN);
  190. /* -ENOMEM radix-tree allocation failure */
  191. if (err)
  192. /*
  193. * add_to_swap_cache() doesn't return -EEXIST, so we can safely
  194. * clear SWAP_HAS_CACHE flag.
  195. */
  196. goto fail;
  197. return 1;
  198. fail:
  199. put_swap_page(page, entry);
  200. return 0;
  201. }
  202. /*
  203. * This must be called only on pages that have
  204. * been verified to be in the swap cache and locked.
  205. * It will never put the page into the free list,
  206. * the caller has a reference on the page.
  207. */
  208. void delete_from_swap_cache(struct page *page)
  209. {
  210. swp_entry_t entry;
  211. struct address_space *address_space;
  212. entry.val = page_private(page);
  213. address_space = swap_address_space(entry);
  214. spin_lock_irq(&address_space->tree_lock);
  215. __delete_from_swap_cache(page);
  216. spin_unlock_irq(&address_space->tree_lock);
  217. put_swap_page(page, entry);
  218. page_ref_sub(page, hpage_nr_pages(page));
  219. }
  220. /*
  221. * If we are the only user, then try to free up the swap cache.
  222. *
  223. * Its ok to check for PageSwapCache without the page lock
  224. * here because we are going to recheck again inside
  225. * try_to_free_swap() _with_ the lock.
  226. * - Marcelo
  227. */
  228. static inline void free_swap_cache(struct page *page)
  229. {
  230. if (PageSwapCache(page) && !page_mapped(page) && trylock_page(page)) {
  231. try_to_free_swap(page);
  232. unlock_page(page);
  233. }
  234. }
  235. /*
  236. * Perform a free_page(), also freeing any swap cache associated with
  237. * this page if it is the last user of the page.
  238. */
  239. void free_page_and_swap_cache(struct page *page)
  240. {
  241. free_swap_cache(page);
  242. if (!is_huge_zero_page(page))
  243. put_page(page);
  244. }
  245. /*
  246. * Passed an array of pages, drop them all from swapcache and then release
  247. * them. They are removed from the LRU and freed if this is their last use.
  248. */
  249. void free_pages_and_swap_cache(struct page **pages, int nr)
  250. {
  251. struct page **pagep = pages;
  252. int i;
  253. lru_add_drain();
  254. for (i = 0; i < nr; i++)
  255. free_swap_cache(pagep[i]);
  256. release_pages(pagep, nr, false);
  257. }
  258. /*
  259. * Lookup a swap entry in the swap cache. A found page will be returned
  260. * unlocked and with its refcount incremented - we rely on the kernel
  261. * lock getting page table operations atomic even if we drop the page
  262. * lock before returning.
  263. */
  264. struct page * lookup_swap_cache(swp_entry_t entry)
  265. {
  266. struct page *page;
  267. page = find_get_page(swap_address_space(entry), swp_offset(entry));
  268. if (page && likely(!PageTransCompound(page))) {
  269. INC_CACHE_INFO(find_success);
  270. if (TestClearPageReadahead(page)) {
  271. atomic_inc(&swapin_readahead_hits);
  272. count_vm_event(SWAP_RA_HIT);
  273. }
  274. }
  275. INC_CACHE_INFO(find_total);
  276. return page;
  277. }
  278. struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
  279. struct vm_area_struct *vma, unsigned long addr,
  280. bool *new_page_allocated)
  281. {
  282. struct page *found_page, *new_page = NULL;
  283. struct address_space *swapper_space = swap_address_space(entry);
  284. int err;
  285. *new_page_allocated = false;
  286. do {
  287. /*
  288. * First check the swap cache. Since this is normally
  289. * called after lookup_swap_cache() failed, re-calling
  290. * that would confuse statistics.
  291. */
  292. found_page = find_get_page(swapper_space, swp_offset(entry));
  293. if (found_page)
  294. break;
  295. /*
  296. * Just skip read ahead for unused swap slot.
  297. * During swap_off when swap_slot_cache is disabled,
  298. * we have to handle the race between putting
  299. * swap entry in swap cache and marking swap slot
  300. * as SWAP_HAS_CACHE. That's done in later part of code or
  301. * else swap_off will be aborted if we return NULL.
  302. */
  303. if (!__swp_swapcount(entry) && swap_slot_cache_enabled)
  304. break;
  305. /*
  306. * Get a new page to read into from swap.
  307. */
  308. if (!new_page) {
  309. new_page = alloc_page_vma(gfp_mask, vma, addr);
  310. if (!new_page)
  311. break; /* Out of memory */
  312. }
  313. /*
  314. * call radix_tree_preload() while we can wait.
  315. */
  316. err = radix_tree_maybe_preload(gfp_mask & GFP_KERNEL);
  317. if (err)
  318. break;
  319. /*
  320. * Swap entry may have been freed since our caller observed it.
  321. */
  322. err = swapcache_prepare(entry);
  323. if (err == -EEXIST) {
  324. radix_tree_preload_end();
  325. /*
  326. * We might race against get_swap_page() and stumble
  327. * across a SWAP_HAS_CACHE swap_map entry whose page
  328. * has not been brought into the swapcache yet.
  329. */
  330. cond_resched();
  331. continue;
  332. }
  333. if (err) { /* swp entry is obsolete ? */
  334. radix_tree_preload_end();
  335. break;
  336. }
  337. /* May fail (-ENOMEM) if radix-tree node allocation failed. */
  338. __SetPageLocked(new_page);
  339. __SetPageSwapBacked(new_page);
  340. err = __add_to_swap_cache(new_page, entry);
  341. if (likely(!err)) {
  342. radix_tree_preload_end();
  343. /*
  344. * Initiate read into locked page and return.
  345. */
  346. lru_cache_add_anon(new_page);
  347. *new_page_allocated = true;
  348. return new_page;
  349. }
  350. radix_tree_preload_end();
  351. __ClearPageLocked(new_page);
  352. /*
  353. * add_to_swap_cache() doesn't return -EEXIST, so we can safely
  354. * clear SWAP_HAS_CACHE flag.
  355. */
  356. put_swap_page(new_page, entry);
  357. } while (err != -ENOMEM);
  358. if (new_page)
  359. put_page(new_page);
  360. return found_page;
  361. }
  362. /*
  363. * Locate a page of swap in physical memory, reserving swap cache space
  364. * and reading the disk if it is not already cached.
  365. * A failure return means that either the page allocation failed or that
  366. * the swap entry is no longer in use.
  367. */
  368. struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
  369. struct vm_area_struct *vma, unsigned long addr, bool do_poll)
  370. {
  371. bool page_was_allocated;
  372. struct page *retpage = __read_swap_cache_async(entry, gfp_mask,
  373. vma, addr, &page_was_allocated);
  374. if (page_was_allocated)
  375. swap_readpage(retpage, do_poll);
  376. return retpage;
  377. }
  378. static unsigned long swapin_nr_pages(unsigned long offset)
  379. {
  380. static unsigned long prev_offset;
  381. unsigned int pages, max_pages, last_ra;
  382. static atomic_t last_readahead_pages;
  383. max_pages = 1 << READ_ONCE(page_cluster);
  384. if (max_pages <= 1)
  385. return 1;
  386. /*
  387. * This heuristic has been found to work well on both sequential and
  388. * random loads, swapping to hard disk or to SSD: please don't ask
  389. * what the "+ 2" means, it just happens to work well, that's all.
  390. */
  391. pages = atomic_xchg(&swapin_readahead_hits, 0) + 2;
  392. if (pages == 2) {
  393. /*
  394. * We can have no readahead hits to judge by: but must not get
  395. * stuck here forever, so check for an adjacent offset instead
  396. * (and don't even bother to check whether swap type is same).
  397. */
  398. if (offset != prev_offset + 1 && offset != prev_offset - 1)
  399. pages = 1;
  400. prev_offset = offset;
  401. } else {
  402. unsigned int roundup = 4;
  403. while (roundup < pages)
  404. roundup <<= 1;
  405. pages = roundup;
  406. }
  407. if (pages > max_pages)
  408. pages = max_pages;
  409. /* Don't shrink readahead too fast */
  410. last_ra = atomic_read(&last_readahead_pages) / 2;
  411. if (pages < last_ra)
  412. pages = last_ra;
  413. atomic_set(&last_readahead_pages, pages);
  414. return pages;
  415. }
  416. /**
  417. * swapin_readahead - swap in pages in hope we need them soon
  418. * @entry: swap entry of this memory
  419. * @gfp_mask: memory allocation flags
  420. * @vma: user vma this address belongs to
  421. * @addr: target address for mempolicy
  422. *
  423. * Returns the struct page for entry and addr, after queueing swapin.
  424. *
  425. * Primitive swap readahead code. We simply read an aligned block of
  426. * (1 << page_cluster) entries in the swap area. This method is chosen
  427. * because it doesn't cost us any seek time. We also make sure to queue
  428. * the 'original' request together with the readahead ones...
  429. *
  430. * This has been extended to use the NUMA policies from the mm triggering
  431. * the readahead.
  432. *
  433. * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
  434. */
  435. struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
  436. struct vm_area_struct *vma, unsigned long addr)
  437. {
  438. struct page *page;
  439. unsigned long entry_offset = swp_offset(entry);
  440. unsigned long offset = entry_offset;
  441. unsigned long start_offset, end_offset;
  442. unsigned long mask;
  443. struct blk_plug plug;
  444. bool do_poll = true;
  445. mask = swapin_nr_pages(offset) - 1;
  446. if (!mask)
  447. goto skip;
  448. do_poll = false;
  449. /* Read a page_cluster sized and aligned cluster around offset. */
  450. start_offset = offset & ~mask;
  451. end_offset = offset | mask;
  452. if (!start_offset) /* First page is swap header. */
  453. start_offset++;
  454. blk_start_plug(&plug);
  455. for (offset = start_offset; offset <= end_offset ; offset++) {
  456. /* Ok, do the async read-ahead now */
  457. page = read_swap_cache_async(swp_entry(swp_type(entry), offset),
  458. gfp_mask, vma, addr, false);
  459. if (!page)
  460. continue;
  461. if (offset != entry_offset &&
  462. likely(!PageTransCompound(page))) {
  463. SetPageReadahead(page);
  464. count_vm_event(SWAP_RA);
  465. }
  466. put_page(page);
  467. }
  468. blk_finish_plug(&plug);
  469. lru_add_drain(); /* Push any new pages onto the LRU now */
  470. skip:
  471. return read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll);
  472. }
  473. int init_swap_address_space(unsigned int type, unsigned long nr_pages)
  474. {
  475. struct address_space *spaces, *space;
  476. unsigned int i, nr;
  477. nr = DIV_ROUND_UP(nr_pages, SWAP_ADDRESS_SPACE_PAGES);
  478. spaces = kvzalloc(sizeof(struct address_space) * nr, GFP_KERNEL);
  479. if (!spaces)
  480. return -ENOMEM;
  481. for (i = 0; i < nr; i++) {
  482. space = spaces + i;
  483. INIT_RADIX_TREE(&space->page_tree, GFP_ATOMIC|__GFP_NOWARN);
  484. atomic_set(&space->i_mmap_writable, 0);
  485. space->a_ops = &swap_aops;
  486. /* swap cache doesn't use writeback related tags */
  487. mapping_set_no_writeback_tags(space);
  488. spin_lock_init(&space->tree_lock);
  489. }
  490. nr_swapper_spaces[type] = nr;
  491. rcu_assign_pointer(swapper_spaces[type], spaces);
  492. return 0;
  493. }
  494. void exit_swap_address_space(unsigned int type)
  495. {
  496. struct address_space *spaces;
  497. spaces = swapper_spaces[type];
  498. nr_swapper_spaces[type] = 0;
  499. rcu_assign_pointer(swapper_spaces[type], NULL);
  500. synchronize_rcu();
  501. kvfree(spaces);
  502. }