swap_state.c 11 KB

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