swap_state.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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/module.h>
  10. #include <linux/mm.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/buffer_head.h>
  17. #include <linux/backing-dev.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, to make sync_page look nicer, and to allow
  25. * future use of radix_tree tags in the swap cache.
  26. */
  27. static const struct address_space_operations swap_aops = {
  28. .writepage = swap_writepage,
  29. .sync_page = block_sync_page,
  30. .set_page_dirty = __set_page_dirty_nobuffers,
  31. .migratepage = migrate_page,
  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. .unplug_io_fn = swap_unplug_io_fn,
  37. };
  38. struct address_space swapper_space = {
  39. .page_tree = RADIX_TREE_INIT(GFP_ATOMIC|__GFP_NOWARN),
  40. .tree_lock = __SPIN_LOCK_UNLOCKED(swapper_space.tree_lock),
  41. .a_ops = &swap_aops,
  42. .i_mmap_nonlinear = LIST_HEAD_INIT(swapper_space.i_mmap_nonlinear),
  43. .backing_dev_info = &swap_backing_dev_info,
  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. void show_swap_cache_info(void)
  53. {
  54. printk("%lu pages in swap cache\n", total_swapcache_pages);
  55. printk("Swap cache stats: add %lu, delete %lu, find %lu/%lu\n",
  56. swap_cache_info.add_total, swap_cache_info.del_total,
  57. swap_cache_info.find_success, swap_cache_info.find_total);
  58. printk("Free swap = %ldkB\n", nr_swap_pages << (PAGE_SHIFT - 10));
  59. printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10));
  60. }
  61. /*
  62. * __add_to_swap_cache resembles add_to_page_cache_locked on swapper_space,
  63. * but sets SwapCache flag and private instead of mapping and index.
  64. */
  65. static int __add_to_swap_cache(struct page *page, swp_entry_t entry)
  66. {
  67. int error;
  68. VM_BUG_ON(!PageLocked(page));
  69. VM_BUG_ON(PageSwapCache(page));
  70. VM_BUG_ON(!PageSwapBacked(page));
  71. page_cache_get(page);
  72. SetPageSwapCache(page);
  73. set_page_private(page, entry.val);
  74. spin_lock_irq(&swapper_space.tree_lock);
  75. error = radix_tree_insert(&swapper_space.page_tree, entry.val, page);
  76. if (likely(!error)) {
  77. total_swapcache_pages++;
  78. __inc_zone_page_state(page, NR_FILE_PAGES);
  79. INC_CACHE_INFO(add_total);
  80. }
  81. spin_unlock_irq(&swapper_space.tree_lock);
  82. if (unlikely(error)) {
  83. set_page_private(page, 0UL);
  84. ClearPageSwapCache(page);
  85. page_cache_release(page);
  86. }
  87. return error;
  88. }
  89. int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp_mask)
  90. {
  91. int error;
  92. error = radix_tree_preload(gfp_mask);
  93. if (!error) {
  94. error = __add_to_swap_cache(page, entry);
  95. radix_tree_preload_end();
  96. }
  97. return error;
  98. }
  99. /*
  100. * This must be called only on pages that have
  101. * been verified to be in the swap cache.
  102. */
  103. void __delete_from_swap_cache(struct page *page)
  104. {
  105. VM_BUG_ON(!PageLocked(page));
  106. VM_BUG_ON(!PageSwapCache(page));
  107. VM_BUG_ON(PageWriteback(page));
  108. radix_tree_delete(&swapper_space.page_tree, page_private(page));
  109. set_page_private(page, 0);
  110. ClearPageSwapCache(page);
  111. total_swapcache_pages--;
  112. __dec_zone_page_state(page, NR_FILE_PAGES);
  113. INC_CACHE_INFO(del_total);
  114. }
  115. /**
  116. * add_to_swap - allocate swap space for a page
  117. * @page: page we want to move to swap
  118. *
  119. * Allocate swap space for the page and add the page to the
  120. * swap cache. Caller needs to hold the page lock.
  121. */
  122. int add_to_swap(struct page *page)
  123. {
  124. swp_entry_t entry;
  125. int err;
  126. VM_BUG_ON(!PageLocked(page));
  127. VM_BUG_ON(!PageUptodate(page));
  128. for (;;) {
  129. entry = get_swap_page();
  130. if (!entry.val)
  131. return 0;
  132. /*
  133. * Radix-tree node allocations from PF_MEMALLOC contexts could
  134. * completely exhaust the page allocator. __GFP_NOMEMALLOC
  135. * stops emergency reserves from being allocated.
  136. *
  137. * TODO: this could cause a theoretical memory reclaim
  138. * deadlock in the swap out path.
  139. */
  140. /*
  141. * Add it to the swap cache and mark it dirty
  142. */
  143. err = add_to_swap_cache(page, entry,
  144. __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN);
  145. switch (err) {
  146. case 0: /* Success */
  147. SetPageDirty(page);
  148. return 1;
  149. case -EEXIST:
  150. /* Raced with "speculative" read_swap_cache_async */
  151. swapcache_free(entry, NULL);
  152. continue;
  153. default:
  154. /* -ENOMEM radix-tree allocation failure */
  155. swapcache_free(entry, NULL);
  156. return 0;
  157. }
  158. }
  159. }
  160. /*
  161. * This must be called only on pages that have
  162. * been verified to be in the swap cache and locked.
  163. * It will never put the page into the free list,
  164. * the caller has a reference on the page.
  165. */
  166. void delete_from_swap_cache(struct page *page)
  167. {
  168. swp_entry_t entry;
  169. entry.val = page_private(page);
  170. spin_lock_irq(&swapper_space.tree_lock);
  171. __delete_from_swap_cache(page);
  172. spin_unlock_irq(&swapper_space.tree_lock);
  173. swapcache_free(entry, page);
  174. page_cache_release(page);
  175. }
  176. /*
  177. * If we are the only user, then try to free up the swap cache.
  178. *
  179. * Its ok to check for PageSwapCache without the page lock
  180. * here because we are going to recheck again inside
  181. * try_to_free_swap() _with_ the lock.
  182. * - Marcelo
  183. */
  184. static inline void free_swap_cache(struct page *page)
  185. {
  186. if (PageSwapCache(page) && !page_mapped(page) && trylock_page(page)) {
  187. try_to_free_swap(page);
  188. unlock_page(page);
  189. }
  190. }
  191. /*
  192. * Perform a free_page(), also freeing any swap cache associated with
  193. * this page if it is the last user of the page.
  194. */
  195. void free_page_and_swap_cache(struct page *page)
  196. {
  197. free_swap_cache(page);
  198. page_cache_release(page);
  199. }
  200. /*
  201. * Passed an array of pages, drop them all from swapcache and then release
  202. * them. They are removed from the LRU and freed if this is their last use.
  203. */
  204. void free_pages_and_swap_cache(struct page **pages, int nr)
  205. {
  206. struct page **pagep = pages;
  207. lru_add_drain();
  208. while (nr) {
  209. int todo = min(nr, PAGEVEC_SIZE);
  210. int i;
  211. for (i = 0; i < todo; i++)
  212. free_swap_cache(pagep[i]);
  213. release_pages(pagep, todo, 0);
  214. pagep += todo;
  215. nr -= todo;
  216. }
  217. }
  218. /*
  219. * Lookup a swap entry in the swap cache. A found page will be returned
  220. * unlocked and with its refcount incremented - we rely on the kernel
  221. * lock getting page table operations atomic even if we drop the page
  222. * lock before returning.
  223. */
  224. struct page * lookup_swap_cache(swp_entry_t entry)
  225. {
  226. struct page *page;
  227. page = find_get_page(&swapper_space, entry.val);
  228. if (page)
  229. INC_CACHE_INFO(find_success);
  230. INC_CACHE_INFO(find_total);
  231. return page;
  232. }
  233. /*
  234. * Locate a page of swap in physical memory, reserving swap cache space
  235. * and reading the disk if it is not already cached.
  236. * A failure return means that either the page allocation failed or that
  237. * the swap entry is no longer in use.
  238. */
  239. struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
  240. struct vm_area_struct *vma, unsigned long addr)
  241. {
  242. struct page *found_page, *new_page = NULL;
  243. int err;
  244. do {
  245. /*
  246. * First check the swap cache. Since this is normally
  247. * called after lookup_swap_cache() failed, re-calling
  248. * that would confuse statistics.
  249. */
  250. found_page = find_get_page(&swapper_space, entry.val);
  251. if (found_page)
  252. break;
  253. /*
  254. * Get a new page to read into from swap.
  255. */
  256. if (!new_page) {
  257. new_page = alloc_page_vma(gfp_mask, vma, addr);
  258. if (!new_page)
  259. break; /* Out of memory */
  260. }
  261. /*
  262. * call radix_tree_preload() while we can wait.
  263. */
  264. err = radix_tree_preload(gfp_mask & GFP_KERNEL);
  265. if (err)
  266. break;
  267. /*
  268. * Swap entry may have been freed since our caller observed it.
  269. */
  270. err = swapcache_prepare(entry);
  271. if (err == -EEXIST) { /* seems racy */
  272. radix_tree_preload_end();
  273. continue;
  274. }
  275. if (err) { /* swp entry is obsolete ? */
  276. radix_tree_preload_end();
  277. break;
  278. }
  279. /*
  280. * Associate the page with swap entry in the swap cache.
  281. * May fail (-EEXIST) if there is already a page associated
  282. * with this entry in the swap cache: added by a racing
  283. * read_swap_cache_async, or add_to_swap or shmem_writepage
  284. * re-using the just freed swap entry for an existing page.
  285. * May fail (-ENOMEM) if radix-tree node allocation failed.
  286. */
  287. __set_page_locked(new_page);
  288. SetPageSwapBacked(new_page);
  289. err = __add_to_swap_cache(new_page, entry);
  290. if (likely(!err)) {
  291. radix_tree_preload_end();
  292. /*
  293. * Initiate read into locked page and return.
  294. */
  295. lru_cache_add_anon(new_page);
  296. swap_readpage(new_page);
  297. return new_page;
  298. }
  299. radix_tree_preload_end();
  300. ClearPageSwapBacked(new_page);
  301. __clear_page_locked(new_page);
  302. swapcache_free(entry, NULL);
  303. } while (err != -ENOMEM);
  304. if (new_page)
  305. page_cache_release(new_page);
  306. return found_page;
  307. }
  308. /**
  309. * swapin_readahead - swap in pages in hope we need them soon
  310. * @entry: swap entry of this memory
  311. * @gfp_mask: memory allocation flags
  312. * @vma: user vma this address belongs to
  313. * @addr: target address for mempolicy
  314. *
  315. * Returns the struct page for entry and addr, after queueing swapin.
  316. *
  317. * Primitive swap readahead code. We simply read an aligned block of
  318. * (1 << page_cluster) entries in the swap area. This method is chosen
  319. * because it doesn't cost us any seek time. We also make sure to queue
  320. * the 'original' request together with the readahead ones...
  321. *
  322. * This has been extended to use the NUMA policies from the mm triggering
  323. * the readahead.
  324. *
  325. * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
  326. */
  327. struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
  328. struct vm_area_struct *vma, unsigned long addr)
  329. {
  330. int nr_pages;
  331. struct page *page;
  332. unsigned long offset;
  333. unsigned long end_offset;
  334. /*
  335. * Get starting offset for readaround, and number of pages to read.
  336. * Adjust starting address by readbehind (for NUMA interleave case)?
  337. * No, it's very unlikely that swap layout would follow vma layout,
  338. * more likely that neighbouring swap pages came from the same node:
  339. * so use the same "addr" to choose the same node for each swap read.
  340. */
  341. nr_pages = valid_swaphandles(entry, &offset);
  342. for (end_offset = offset + nr_pages; offset < end_offset; offset++) {
  343. /* Ok, do the async read-ahead now */
  344. page = read_swap_cache_async(swp_entry(swp_type(entry), offset),
  345. gfp_mask, vma, addr);
  346. if (!page)
  347. break;
  348. page_cache_release(page);
  349. }
  350. lru_add_drain(); /* Push any new pages onto the LRU now */
  351. return read_swap_cache_async(entry, gfp_mask, vma, addr);
  352. }