swap_state.c 10 KB

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