swap_state.c 13 KB

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