swap_state.c 10 KB

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