swap_state.c 14 KB

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