swap_state.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 <asm/pgtable.h>
  21. /*
  22. * swapper_space is a fiction, retained to simplify the path through
  23. * vmscan's shrink_page_list, to make sync_page look nicer, and to allow
  24. * future use of radix_tree tags in the swap cache.
  25. */
  26. static const struct address_space_operations swap_aops = {
  27. .writepage = swap_writepage,
  28. .sync_page = block_sync_page,
  29. .set_page_dirty = __set_page_dirty_nobuffers,
  30. .migratepage = migrate_page,
  31. };
  32. static struct backing_dev_info swap_backing_dev_info = {
  33. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  34. .unplug_io_fn = swap_unplug_io_fn,
  35. };
  36. struct address_space swapper_space = {
  37. .page_tree = RADIX_TREE_INIT(GFP_ATOMIC|__GFP_NOWARN),
  38. .tree_lock = __RW_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("Swap cache: 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 = %lukB\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 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. gfp_t gfp_mask)
  64. {
  65. int error;
  66. BUG_ON(!PageLocked(page));
  67. BUG_ON(PageSwapCache(page));
  68. BUG_ON(PagePrivate(page));
  69. error = radix_tree_preload(gfp_mask);
  70. if (!error) {
  71. write_lock_irq(&swapper_space.tree_lock);
  72. error = radix_tree_insert(&swapper_space.page_tree,
  73. entry.val, page);
  74. if (!error) {
  75. page_cache_get(page);
  76. SetPageSwapCache(page);
  77. set_page_private(page, entry.val);
  78. total_swapcache_pages++;
  79. __inc_zone_page_state(page, NR_FILE_PAGES);
  80. INC_CACHE_INFO(add_total);
  81. }
  82. write_unlock_irq(&swapper_space.tree_lock);
  83. radix_tree_preload_end();
  84. }
  85. return error;
  86. }
  87. static int add_to_swap_cache(struct page *page, swp_entry_t entry,
  88. gfp_t gfp_mask)
  89. {
  90. int error;
  91. BUG_ON(PageLocked(page));
  92. if (!swap_duplicate(entry))
  93. return -ENOENT;
  94. SetPageLocked(page);
  95. error = __add_to_swap_cache(page, entry, gfp_mask & GFP_KERNEL);
  96. /*
  97. * Anon pages are already on the LRU, we don't run lru_cache_add here.
  98. */
  99. if (error) {
  100. ClearPageLocked(page);
  101. swap_free(entry);
  102. return error;
  103. }
  104. return 0;
  105. }
  106. /*
  107. * This must be called only on pages that have
  108. * been verified to be in the swap cache.
  109. */
  110. void __delete_from_swap_cache(struct page *page)
  111. {
  112. BUG_ON(!PageLocked(page));
  113. BUG_ON(!PageSwapCache(page));
  114. BUG_ON(PageWriteback(page));
  115. BUG_ON(PagePrivate(page));
  116. radix_tree_delete(&swapper_space.page_tree, page_private(page));
  117. set_page_private(page, 0);
  118. ClearPageSwapCache(page);
  119. total_swapcache_pages--;
  120. __dec_zone_page_state(page, NR_FILE_PAGES);
  121. INC_CACHE_INFO(del_total);
  122. }
  123. /**
  124. * add_to_swap - allocate swap space for a page
  125. * @page: page we want to move to swap
  126. *
  127. * Allocate swap space for the page and add the page to the
  128. * swap cache. Caller needs to hold the page lock.
  129. */
  130. int add_to_swap(struct page * page, gfp_t gfp_mask)
  131. {
  132. swp_entry_t entry;
  133. int err;
  134. BUG_ON(!PageLocked(page));
  135. for (;;) {
  136. entry = get_swap_page();
  137. if (!entry.val)
  138. return 0;
  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_mask|__GFP_NOMEMALLOC|__GFP_NOWARN);
  152. switch (err) {
  153. case 0: /* Success */
  154. SetPageUptodate(page);
  155. SetPageDirty(page);
  156. return 1;
  157. case -EEXIST:
  158. /* Raced with "speculative" read_swap_cache_async */
  159. swap_free(entry);
  160. continue;
  161. default:
  162. /* -ENOMEM radix-tree allocation failure */
  163. swap_free(entry);
  164. return 0;
  165. }
  166. }
  167. }
  168. /*
  169. * This must be called only on pages that have
  170. * been verified to be in the swap cache and locked.
  171. * It will never put the page into the free list,
  172. * the caller has a reference on the page.
  173. */
  174. void delete_from_swap_cache(struct page *page)
  175. {
  176. swp_entry_t entry;
  177. entry.val = page_private(page);
  178. write_lock_irq(&swapper_space.tree_lock);
  179. __delete_from_swap_cache(page);
  180. write_unlock_irq(&swapper_space.tree_lock);
  181. swap_free(entry);
  182. page_cache_release(page);
  183. }
  184. /*
  185. * Strange swizzling function only for use by shmem_writepage
  186. */
  187. int move_to_swap_cache(struct page *page, swp_entry_t entry)
  188. {
  189. int err = __add_to_swap_cache(page, entry, GFP_ATOMIC);
  190. if (!err) {
  191. remove_from_page_cache(page);
  192. page_cache_release(page); /* pagecache ref */
  193. if (!swap_duplicate(entry))
  194. BUG();
  195. SetPageDirty(page);
  196. }
  197. return err;
  198. }
  199. /*
  200. * Strange swizzling function for shmem_getpage (and shmem_unuse)
  201. */
  202. int move_from_swap_cache(struct page *page, unsigned long index,
  203. struct address_space *mapping)
  204. {
  205. int err = add_to_page_cache(page, mapping, index, GFP_ATOMIC);
  206. if (!err) {
  207. delete_from_swap_cache(page);
  208. /* shift page from clean_pages to dirty_pages list */
  209. ClearPageDirty(page);
  210. set_page_dirty(page);
  211. }
  212. return err;
  213. }
  214. /*
  215. * If we are the only user, then try to free up the swap cache.
  216. *
  217. * Its ok to check for PageSwapCache without the page lock
  218. * here because we are going to recheck again inside
  219. * exclusive_swap_page() _with_ the lock.
  220. * - Marcelo
  221. */
  222. static inline void free_swap_cache(struct page *page)
  223. {
  224. if (PageSwapCache(page) && !TestSetPageLocked(page)) {
  225. remove_exclusive_swap_page(page);
  226. unlock_page(page);
  227. }
  228. }
  229. /*
  230. * Perform a free_page(), also freeing any swap cache associated with
  231. * this page if it is the last user of the page.
  232. */
  233. void free_page_and_swap_cache(struct page *page)
  234. {
  235. free_swap_cache(page);
  236. page_cache_release(page);
  237. }
  238. /*
  239. * Passed an array of pages, drop them all from swapcache and then release
  240. * them. They are removed from the LRU and freed if this is their last use.
  241. */
  242. void free_pages_and_swap_cache(struct page **pages, int nr)
  243. {
  244. struct page **pagep = pages;
  245. lru_add_drain();
  246. while (nr) {
  247. int todo = min(nr, PAGEVEC_SIZE);
  248. int i;
  249. for (i = 0; i < todo; i++)
  250. free_swap_cache(pagep[i]);
  251. release_pages(pagep, todo, 0);
  252. pagep += todo;
  253. nr -= todo;
  254. }
  255. }
  256. /*
  257. * Lookup a swap entry in the swap cache. A found page will be returned
  258. * unlocked and with its refcount incremented - we rely on the kernel
  259. * lock getting page table operations atomic even if we drop the page
  260. * lock before returning.
  261. */
  262. struct page * lookup_swap_cache(swp_entry_t entry)
  263. {
  264. struct page *page;
  265. page = find_get_page(&swapper_space, entry.val);
  266. if (page)
  267. INC_CACHE_INFO(find_success);
  268. INC_CACHE_INFO(find_total);
  269. return page;
  270. }
  271. /*
  272. * Locate a page of swap in physical memory, reserving swap cache space
  273. * and reading the disk if it is not already cached.
  274. * A failure return means that either the page allocation failed or that
  275. * the swap entry is no longer in use.
  276. */
  277. struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
  278. struct vm_area_struct *vma, unsigned long addr)
  279. {
  280. struct page *found_page, *new_page = NULL;
  281. int err;
  282. do {
  283. /*
  284. * First check the swap cache. Since this is normally
  285. * called after lookup_swap_cache() failed, re-calling
  286. * that would confuse statistics.
  287. */
  288. found_page = find_get_page(&swapper_space, entry.val);
  289. if (found_page)
  290. break;
  291. /*
  292. * Get a new page to read into from swap.
  293. */
  294. if (!new_page) {
  295. new_page = alloc_page_vma(gfp_mask, vma, addr);
  296. if (!new_page)
  297. break; /* Out of memory */
  298. }
  299. /*
  300. * Associate the page with swap entry in the swap cache.
  301. * May fail (-ENOENT) if swap entry has been freed since
  302. * our caller observed it. May fail (-EEXIST) if there
  303. * is already a page associated with this entry in the
  304. * swap cache: added by a racing read_swap_cache_async,
  305. * or by try_to_swap_out (or shmem_writepage) re-using
  306. * the just freed swap entry for an existing page.
  307. * May fail (-ENOMEM) if radix-tree node allocation failed.
  308. */
  309. err = add_to_swap_cache(new_page, entry, gfp_mask);
  310. if (!err) {
  311. /*
  312. * Initiate read into locked page and return.
  313. */
  314. lru_cache_add_active(new_page);
  315. swap_readpage(NULL, new_page);
  316. return new_page;
  317. }
  318. } while (err != -ENOENT && err != -ENOMEM);
  319. if (new_page)
  320. page_cache_release(new_page);
  321. return found_page;
  322. }
  323. /**
  324. * swapin_readahead - swap in pages in hope we need them soon
  325. * @entry: swap entry of this memory
  326. * @vma: user vma this address belongs to
  327. * @addr: target address for mempolicy
  328. *
  329. * Returns the struct page for entry and addr, after queueing swapin.
  330. *
  331. * Primitive swap readahead code. We simply read an aligned block of
  332. * (1 << page_cluster) entries in the swap area. This method is chosen
  333. * because it doesn't cost us any seek time. We also make sure to queue
  334. * the 'original' request together with the readahead ones...
  335. *
  336. * This has been extended to use the NUMA policies from the mm triggering
  337. * the readahead.
  338. *
  339. * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
  340. */
  341. struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
  342. struct vm_area_struct *vma, unsigned long addr)
  343. {
  344. int nr_pages;
  345. struct page *page;
  346. unsigned long offset;
  347. unsigned long end_offset;
  348. /*
  349. * Get starting offset for readaround, and number of pages to read.
  350. * Adjust starting address by readbehind (for NUMA interleave case)?
  351. * No, it's very unlikely that swap layout would follow vma layout,
  352. * more likely that neighbouring swap pages came from the same node:
  353. * so use the same "addr" to choose the same node for each swap read.
  354. */
  355. nr_pages = valid_swaphandles(entry, &offset);
  356. for (end_offset = offset + nr_pages; offset < end_offset; offset++) {
  357. /* Ok, do the async read-ahead now */
  358. page = read_swap_cache_async(swp_entry(swp_type(entry), offset),
  359. gfp_mask, vma, addr);
  360. if (!page)
  361. break;
  362. page_cache_release(page);
  363. }
  364. lru_add_drain(); /* Push any new pages onto the LRU now */
  365. return read_swap_cache_async(entry, gfp_mask, vma, addr);
  366. }