swap_state.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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/init.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/buffer_head.h>
  16. #include <linux/backing-dev.h>
  17. #include <asm/pgtable.h>
  18. /*
  19. * swapper_space is a fiction, retained to simplify the path through
  20. * vmscan's shrink_list, to make sync_page look nicer, and to allow
  21. * future use of radix_tree tags in the swap cache.
  22. */
  23. static struct address_space_operations swap_aops = {
  24. .writepage = swap_writepage,
  25. .sync_page = block_sync_page,
  26. .set_page_dirty = __set_page_dirty_nobuffers,
  27. };
  28. static struct backing_dev_info swap_backing_dev_info = {
  29. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  30. .unplug_io_fn = swap_unplug_io_fn,
  31. };
  32. struct address_space swapper_space = {
  33. .page_tree = RADIX_TREE_INIT(GFP_ATOMIC|__GFP_NOWARN),
  34. .tree_lock = RW_LOCK_UNLOCKED,
  35. .a_ops = &swap_aops,
  36. .i_mmap_nonlinear = LIST_HEAD_INIT(swapper_space.i_mmap_nonlinear),
  37. .backing_dev_info = &swap_backing_dev_info,
  38. };
  39. EXPORT_SYMBOL(swapper_space);
  40. #define INC_CACHE_INFO(x) do { swap_cache_info.x++; } while (0)
  41. static struct {
  42. unsigned long add_total;
  43. unsigned long del_total;
  44. unsigned long find_success;
  45. unsigned long find_total;
  46. unsigned long noent_race;
  47. unsigned long exist_race;
  48. } swap_cache_info;
  49. void show_swap_cache_info(void)
  50. {
  51. printk("Swap cache: add %lu, delete %lu, find %lu/%lu, race %lu+%lu\n",
  52. swap_cache_info.add_total, swap_cache_info.del_total,
  53. swap_cache_info.find_success, swap_cache_info.find_total,
  54. swap_cache_info.noent_race, swap_cache_info.exist_race);
  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,
  63. swp_entry_t entry, int gfp_mask)
  64. {
  65. int error;
  66. BUG_ON(PageSwapCache(page));
  67. BUG_ON(PagePrivate(page));
  68. error = radix_tree_preload(gfp_mask);
  69. if (!error) {
  70. write_lock_irq(&swapper_space.tree_lock);
  71. error = radix_tree_insert(&swapper_space.page_tree,
  72. entry.val, page);
  73. if (!error) {
  74. page_cache_get(page);
  75. SetPageLocked(page);
  76. SetPageSwapCache(page);
  77. page->private = entry.val;
  78. total_swapcache_pages++;
  79. pagecache_acct(1);
  80. }
  81. write_unlock_irq(&swapper_space.tree_lock);
  82. radix_tree_preload_end();
  83. }
  84. return error;
  85. }
  86. static int add_to_swap_cache(struct page *page, swp_entry_t entry)
  87. {
  88. int error;
  89. if (!swap_duplicate(entry)) {
  90. INC_CACHE_INFO(noent_race);
  91. return -ENOENT;
  92. }
  93. error = __add_to_swap_cache(page, entry, GFP_KERNEL);
  94. /*
  95. * Anon pages are already on the LRU, we don't run lru_cache_add here.
  96. */
  97. if (error) {
  98. swap_free(entry);
  99. if (error == -EEXIST)
  100. INC_CACHE_INFO(exist_race);
  101. return error;
  102. }
  103. INC_CACHE_INFO(add_total);
  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. radix_tree_delete(&swapper_space.page_tree, page->private);
  116. page->private = 0;
  117. ClearPageSwapCache(page);
  118. total_swapcache_pages--;
  119. pagecache_acct(-1);
  120. INC_CACHE_INFO(del_total);
  121. }
  122. /**
  123. * add_to_swap - allocate swap space for a page
  124. * @page: page we want to move to swap
  125. *
  126. * Allocate swap space for the page and add the page to the
  127. * swap cache. Caller needs to hold the page lock.
  128. */
  129. int add_to_swap(struct page * page)
  130. {
  131. swp_entry_t entry;
  132. int pf_flags;
  133. int err;
  134. if (!PageLocked(page))
  135. BUG();
  136. for (;;) {
  137. entry = get_swap_page();
  138. if (!entry.val)
  139. return 0;
  140. /* Radix-tree node allocations are performing
  141. * GFP_ATOMIC allocations under PF_MEMALLOC.
  142. * They can completely exhaust the page allocator.
  143. *
  144. * So PF_MEMALLOC is dropped here. This causes the slab
  145. * allocations to fail earlier, so radix-tree nodes will
  146. * then be allocated from the mempool reserves.
  147. *
  148. * We're still using __GFP_HIGH for radix-tree node
  149. * allocations, so some of the emergency pools are available,
  150. * just not all of them.
  151. */
  152. pf_flags = current->flags;
  153. current->flags &= ~PF_MEMALLOC;
  154. /*
  155. * Add it to the swap cache and mark it dirty
  156. */
  157. err = __add_to_swap_cache(page, entry, GFP_ATOMIC|__GFP_NOWARN);
  158. if (pf_flags & PF_MEMALLOC)
  159. current->flags |= PF_MEMALLOC;
  160. switch (err) {
  161. case 0: /* Success */
  162. SetPageUptodate(page);
  163. SetPageDirty(page);
  164. INC_CACHE_INFO(add_total);
  165. return 1;
  166. case -EEXIST:
  167. /* Raced with "speculative" read_swap_cache_async */
  168. INC_CACHE_INFO(exist_race);
  169. swap_free(entry);
  170. continue;
  171. default:
  172. /* -ENOMEM radix-tree allocation failure */
  173. swap_free(entry);
  174. return 0;
  175. }
  176. }
  177. }
  178. /*
  179. * This must be called only on pages that have
  180. * been verified to be in the swap cache and locked.
  181. * It will never put the page into the free list,
  182. * the caller has a reference on the page.
  183. */
  184. void delete_from_swap_cache(struct page *page)
  185. {
  186. swp_entry_t entry;
  187. BUG_ON(!PageSwapCache(page));
  188. BUG_ON(!PageLocked(page));
  189. BUG_ON(PageWriteback(page));
  190. BUG_ON(PagePrivate(page));
  191. entry.val = page->private;
  192. write_lock_irq(&swapper_space.tree_lock);
  193. __delete_from_swap_cache(page);
  194. write_unlock_irq(&swapper_space.tree_lock);
  195. swap_free(entry);
  196. page_cache_release(page);
  197. }
  198. /*
  199. * Strange swizzling function only for use by shmem_writepage
  200. */
  201. int move_to_swap_cache(struct page *page, swp_entry_t entry)
  202. {
  203. int err = __add_to_swap_cache(page, entry, GFP_ATOMIC);
  204. if (!err) {
  205. remove_from_page_cache(page);
  206. page_cache_release(page); /* pagecache ref */
  207. if (!swap_duplicate(entry))
  208. BUG();
  209. SetPageDirty(page);
  210. INC_CACHE_INFO(add_total);
  211. } else if (err == -EEXIST)
  212. INC_CACHE_INFO(exist_race);
  213. return err;
  214. }
  215. /*
  216. * Strange swizzling function for shmem_getpage (and shmem_unuse)
  217. */
  218. int move_from_swap_cache(struct page *page, unsigned long index,
  219. struct address_space *mapping)
  220. {
  221. int err = add_to_page_cache(page, mapping, index, GFP_ATOMIC);
  222. if (!err) {
  223. delete_from_swap_cache(page);
  224. /* shift page from clean_pages to dirty_pages list */
  225. ClearPageDirty(page);
  226. set_page_dirty(page);
  227. }
  228. return err;
  229. }
  230. /*
  231. * If we are the only user, then try to free up the swap cache.
  232. *
  233. * Its ok to check for PageSwapCache without the page lock
  234. * here because we are going to recheck again inside
  235. * exclusive_swap_page() _with_ the lock.
  236. * - Marcelo
  237. */
  238. static inline void free_swap_cache(struct page *page)
  239. {
  240. if (PageSwapCache(page) && !TestSetPageLocked(page)) {
  241. remove_exclusive_swap_page(page);
  242. unlock_page(page);
  243. }
  244. }
  245. /*
  246. * Perform a free_page(), also freeing any swap cache associated with
  247. * this page if it is the last user of the page. Can not do a lock_page,
  248. * as we are holding the page_table_lock spinlock.
  249. */
  250. void free_page_and_swap_cache(struct page *page)
  251. {
  252. free_swap_cache(page);
  253. page_cache_release(page);
  254. }
  255. /*
  256. * Passed an array of pages, drop them all from swapcache and then release
  257. * them. They are removed from the LRU and freed if this is their last use.
  258. */
  259. void free_pages_and_swap_cache(struct page **pages, int nr)
  260. {
  261. int chunk = 16;
  262. struct page **pagep = pages;
  263. lru_add_drain();
  264. while (nr) {
  265. int todo = min(chunk, nr);
  266. int i;
  267. for (i = 0; i < todo; i++)
  268. free_swap_cache(pagep[i]);
  269. release_pages(pagep, todo, 0);
  270. pagep += todo;
  271. nr -= todo;
  272. }
  273. }
  274. /*
  275. * Lookup a swap entry in the swap cache. A found page will be returned
  276. * unlocked and with its refcount incremented - we rely on the kernel
  277. * lock getting page table operations atomic even if we drop the page
  278. * lock before returning.
  279. */
  280. struct page * lookup_swap_cache(swp_entry_t entry)
  281. {
  282. struct page *page;
  283. page = find_get_page(&swapper_space, entry.val);
  284. if (page)
  285. INC_CACHE_INFO(find_success);
  286. INC_CACHE_INFO(find_total);
  287. return page;
  288. }
  289. /*
  290. * Locate a page of swap in physical memory, reserving swap cache space
  291. * and reading the disk if it is not already cached.
  292. * A failure return means that either the page allocation failed or that
  293. * the swap entry is no longer in use.
  294. */
  295. struct page *read_swap_cache_async(swp_entry_t entry,
  296. struct vm_area_struct *vma, unsigned long addr)
  297. {
  298. struct page *found_page, *new_page = NULL;
  299. int err;
  300. do {
  301. /*
  302. * First check the swap cache. Since this is normally
  303. * called after lookup_swap_cache() failed, re-calling
  304. * that would confuse statistics.
  305. */
  306. found_page = find_get_page(&swapper_space, entry.val);
  307. if (found_page)
  308. break;
  309. /*
  310. * Get a new page to read into from swap.
  311. */
  312. if (!new_page) {
  313. new_page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
  314. if (!new_page)
  315. break; /* Out of memory */
  316. }
  317. /*
  318. * Associate the page with swap entry in the swap cache.
  319. * May fail (-ENOENT) if swap entry has been freed since
  320. * our caller observed it. May fail (-EEXIST) if there
  321. * is already a page associated with this entry in the
  322. * swap cache: added by a racing read_swap_cache_async,
  323. * or by try_to_swap_out (or shmem_writepage) re-using
  324. * the just freed swap entry for an existing page.
  325. * May fail (-ENOMEM) if radix-tree node allocation failed.
  326. */
  327. err = add_to_swap_cache(new_page, entry);
  328. if (!err) {
  329. /*
  330. * Initiate read into locked page and return.
  331. */
  332. lru_cache_add_active(new_page);
  333. swap_readpage(NULL, new_page);
  334. return new_page;
  335. }
  336. } while (err != -ENOENT && err != -ENOMEM);
  337. if (new_page)
  338. page_cache_release(new_page);
  339. return found_page;
  340. }