swap_state.c 13 KB

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