swap_state.c 13 KB

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