frontswap.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Frontswap frontend
  4. *
  5. * This code provides the generic "frontend" layer to call a matching
  6. * "backend" driver implementation of frontswap. See
  7. * Documentation/mm/frontswap.rst for more information.
  8. *
  9. * Copyright (C) 2009-2012 Oracle Corp. All rights reserved.
  10. * Author: Dan Magenheimer
  11. */
  12. #include <linux/mman.h>
  13. #include <linux/swap.h>
  14. #include <linux/swapops.h>
  15. #include <linux/security.h>
  16. #include <linux/module.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/frontswap.h>
  19. #include <linux/swapfile.h>
  20. DEFINE_STATIC_KEY_FALSE(frontswap_enabled_key);
  21. /*
  22. * frontswap_ops are added by frontswap_register_ops, and provide the
  23. * frontswap "backend" implementation functions. Multiple implementations
  24. * may be registered, but implementations can never deregister. This
  25. * is a simple singly-linked list of all registered implementations.
  26. */
  27. static const struct frontswap_ops *frontswap_ops __read_mostly;
  28. #ifdef CONFIG_DEBUG_FS
  29. /*
  30. * Counters available via /sys/kernel/debug/frontswap (if debugfs is
  31. * properly configured). These are for information only so are not protected
  32. * against increment races.
  33. */
  34. static u64 frontswap_loads;
  35. static u64 frontswap_succ_stores;
  36. static u64 frontswap_failed_stores;
  37. static u64 frontswap_invalidates;
  38. static inline void inc_frontswap_loads(void)
  39. {
  40. data_race(frontswap_loads++);
  41. }
  42. static inline void inc_frontswap_succ_stores(void)
  43. {
  44. data_race(frontswap_succ_stores++);
  45. }
  46. static inline void inc_frontswap_failed_stores(void)
  47. {
  48. data_race(frontswap_failed_stores++);
  49. }
  50. static inline void inc_frontswap_invalidates(void)
  51. {
  52. data_race(frontswap_invalidates++);
  53. }
  54. #else
  55. static inline void inc_frontswap_loads(void) { }
  56. static inline void inc_frontswap_succ_stores(void) { }
  57. static inline void inc_frontswap_failed_stores(void) { }
  58. static inline void inc_frontswap_invalidates(void) { }
  59. #endif
  60. /*
  61. * Due to the asynchronous nature of the backends loading potentially
  62. * _after_ the swap system has been activated, we have chokepoints
  63. * on all frontswap functions to not call the backend until the backend
  64. * has registered.
  65. *
  66. * This would not guards us against the user deciding to call swapoff right as
  67. * we are calling the backend to initialize (so swapon is in action).
  68. * Fortunately for us, the swapon_mutex has been taken by the callee so we are
  69. * OK. The other scenario where calls to frontswap_store (called via
  70. * swap_writepage) is racing with frontswap_invalidate_area (called via
  71. * swapoff) is again guarded by the swap subsystem.
  72. *
  73. * While no backend is registered all calls to frontswap_[store|load|
  74. * invalidate_area|invalidate_page] are ignored or fail.
  75. *
  76. * The time between the backend being registered and the swap file system
  77. * calling the backend (via the frontswap_* functions) is indeterminate as
  78. * frontswap_ops is not atomic_t (or a value guarded by a spinlock).
  79. * That is OK as we are comfortable missing some of these calls to the newly
  80. * registered backend.
  81. *
  82. * Obviously the opposite (unloading the backend) must be done after all
  83. * the frontswap_[store|load|invalidate_area|invalidate_page] start
  84. * ignoring or failing the requests. However, there is currently no way
  85. * to unload a backend once it is registered.
  86. */
  87. /*
  88. * Register operations for frontswap
  89. */
  90. int frontswap_register_ops(const struct frontswap_ops *ops)
  91. {
  92. if (frontswap_ops)
  93. return -EINVAL;
  94. frontswap_ops = ops;
  95. static_branch_inc(&frontswap_enabled_key);
  96. return 0;
  97. }
  98. /*
  99. * Called when a swap device is swapon'd.
  100. */
  101. void frontswap_init(unsigned type, unsigned long *map)
  102. {
  103. struct swap_info_struct *sis = swap_info[type];
  104. VM_BUG_ON(sis == NULL);
  105. /*
  106. * p->frontswap is a bitmap that we MUST have to figure out which page
  107. * has gone in frontswap. Without it there is no point of continuing.
  108. */
  109. if (WARN_ON(!map))
  110. return;
  111. /*
  112. * Irregardless of whether the frontswap backend has been loaded
  113. * before this function or it will be later, we _MUST_ have the
  114. * p->frontswap set to something valid to work properly.
  115. */
  116. frontswap_map_set(sis, map);
  117. if (!frontswap_enabled())
  118. return;
  119. frontswap_ops->init(type);
  120. }
  121. static bool __frontswap_test(struct swap_info_struct *sis,
  122. pgoff_t offset)
  123. {
  124. if (sis->frontswap_map)
  125. return test_bit(offset, sis->frontswap_map);
  126. return false;
  127. }
  128. static inline void __frontswap_set(struct swap_info_struct *sis,
  129. pgoff_t offset)
  130. {
  131. set_bit(offset, sis->frontswap_map);
  132. atomic_inc(&sis->frontswap_pages);
  133. }
  134. static inline void __frontswap_clear(struct swap_info_struct *sis,
  135. pgoff_t offset)
  136. {
  137. clear_bit(offset, sis->frontswap_map);
  138. atomic_dec(&sis->frontswap_pages);
  139. }
  140. /*
  141. * "Store" data from a page to frontswap and associate it with the page's
  142. * swaptype and offset. Page must be locked and in the swap cache.
  143. * If frontswap already contains a page with matching swaptype and
  144. * offset, the frontswap implementation may either overwrite the data and
  145. * return success or invalidate the page from frontswap and return failure.
  146. */
  147. int __frontswap_store(struct page *page)
  148. {
  149. int ret = -1;
  150. swp_entry_t entry = { .val = page_private(page), };
  151. int type = swp_type(entry);
  152. struct swap_info_struct *sis = swap_info[type];
  153. pgoff_t offset = swp_offset(entry);
  154. VM_BUG_ON(!frontswap_ops);
  155. VM_BUG_ON(!PageLocked(page));
  156. VM_BUG_ON(sis == NULL);
  157. /*
  158. * If a dup, we must remove the old page first; we can't leave the
  159. * old page no matter if the store of the new page succeeds or fails,
  160. * and we can't rely on the new page replacing the old page as we may
  161. * not store to the same implementation that contains the old page.
  162. */
  163. if (__frontswap_test(sis, offset)) {
  164. __frontswap_clear(sis, offset);
  165. frontswap_ops->invalidate_page(type, offset);
  166. }
  167. ret = frontswap_ops->store(type, offset, page);
  168. if (ret == 0) {
  169. __frontswap_set(sis, offset);
  170. inc_frontswap_succ_stores();
  171. } else {
  172. inc_frontswap_failed_stores();
  173. }
  174. return ret;
  175. }
  176. /*
  177. * "Get" data from frontswap associated with swaptype and offset that were
  178. * specified when the data was put to frontswap and use it to fill the
  179. * specified page with data. Page must be locked and in the swap cache.
  180. */
  181. int __frontswap_load(struct page *page)
  182. {
  183. int ret = -1;
  184. swp_entry_t entry = { .val = page_private(page), };
  185. int type = swp_type(entry);
  186. struct swap_info_struct *sis = swap_info[type];
  187. pgoff_t offset = swp_offset(entry);
  188. VM_BUG_ON(!frontswap_ops);
  189. VM_BUG_ON(!PageLocked(page));
  190. VM_BUG_ON(sis == NULL);
  191. if (!__frontswap_test(sis, offset))
  192. return -1;
  193. /* Try loading from each implementation, until one succeeds. */
  194. ret = frontswap_ops->load(type, offset, page);
  195. if (ret == 0)
  196. inc_frontswap_loads();
  197. return ret;
  198. }
  199. /*
  200. * Invalidate any data from frontswap associated with the specified swaptype
  201. * and offset so that a subsequent "get" will fail.
  202. */
  203. void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
  204. {
  205. struct swap_info_struct *sis = swap_info[type];
  206. VM_BUG_ON(!frontswap_ops);
  207. VM_BUG_ON(sis == NULL);
  208. if (!__frontswap_test(sis, offset))
  209. return;
  210. frontswap_ops->invalidate_page(type, offset);
  211. __frontswap_clear(sis, offset);
  212. inc_frontswap_invalidates();
  213. }
  214. /*
  215. * Invalidate all data from frontswap associated with all offsets for the
  216. * specified swaptype.
  217. */
  218. void __frontswap_invalidate_area(unsigned type)
  219. {
  220. struct swap_info_struct *sis = swap_info[type];
  221. VM_BUG_ON(!frontswap_ops);
  222. VM_BUG_ON(sis == NULL);
  223. if (sis->frontswap_map == NULL)
  224. return;
  225. frontswap_ops->invalidate_area(type);
  226. atomic_set(&sis->frontswap_pages, 0);
  227. bitmap_zero(sis->frontswap_map, sis->max);
  228. }
  229. static int __init init_frontswap(void)
  230. {
  231. #ifdef CONFIG_DEBUG_FS
  232. struct dentry *root = debugfs_create_dir("frontswap", NULL);
  233. if (root == NULL)
  234. return -ENXIO;
  235. debugfs_create_u64("loads", 0444, root, &frontswap_loads);
  236. debugfs_create_u64("succ_stores", 0444, root, &frontswap_succ_stores);
  237. debugfs_create_u64("failed_stores", 0444, root,
  238. &frontswap_failed_stores);
  239. debugfs_create_u64("invalidates", 0444, root, &frontswap_invalidates);
  240. #endif
  241. return 0;
  242. }
  243. module_init(init_frontswap);