frontswap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Frontswap frontend
  3. *
  4. * This code provides the generic "frontend" layer to call a matching
  5. * "backend" driver implementation of frontswap. See
  6. * Documentation/vm/frontswap.txt for more information.
  7. *
  8. * Copyright (C) 2009-2012 Oracle Corp. All rights reserved.
  9. * Author: Dan Magenheimer
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2.
  12. */
  13. #include <linux/mman.h>
  14. #include <linux/swap.h>
  15. #include <linux/swapops.h>
  16. #include <linux/security.h>
  17. #include <linux/module.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/frontswap.h>
  20. #include <linux/swapfile.h>
  21. DEFINE_STATIC_KEY_FALSE(frontswap_enabled_key);
  22. /*
  23. * frontswap_ops are added by frontswap_register_ops, and provide the
  24. * frontswap "backend" implementation functions. Multiple implementations
  25. * may be registered, but implementations can never deregister. This
  26. * is a simple singly-linked list of all registered implementations.
  27. */
  28. static struct frontswap_ops *frontswap_ops __read_mostly;
  29. #define for_each_frontswap_ops(ops) \
  30. for ((ops) = frontswap_ops; (ops); (ops) = (ops)->next)
  31. /*
  32. * If enabled, frontswap_store will return failure even on success. As
  33. * a result, the swap subsystem will always write the page to swap, in
  34. * effect converting frontswap into a writethrough cache. In this mode,
  35. * there is no direct reduction in swap writes, but a frontswap backend
  36. * can unilaterally "reclaim" any pages in use with no data loss, thus
  37. * providing increases control over maximum memory usage due to frontswap.
  38. */
  39. static bool frontswap_writethrough_enabled __read_mostly;
  40. /*
  41. * If enabled, the underlying tmem implementation is capable of doing
  42. * exclusive gets, so frontswap_load, on a successful tmem_get must
  43. * mark the page as no longer in frontswap AND mark it dirty.
  44. */
  45. static bool frontswap_tmem_exclusive_gets_enabled __read_mostly;
  46. #ifdef CONFIG_DEBUG_FS
  47. /*
  48. * Counters available via /sys/kernel/debug/frontswap (if debugfs is
  49. * properly configured). These are for information only so are not protected
  50. * against increment races.
  51. */
  52. static u64 frontswap_loads;
  53. static u64 frontswap_succ_stores;
  54. static u64 frontswap_failed_stores;
  55. static u64 frontswap_invalidates;
  56. static inline void inc_frontswap_loads(void) {
  57. frontswap_loads++;
  58. }
  59. static inline void inc_frontswap_succ_stores(void) {
  60. frontswap_succ_stores++;
  61. }
  62. static inline void inc_frontswap_failed_stores(void) {
  63. frontswap_failed_stores++;
  64. }
  65. static inline void inc_frontswap_invalidates(void) {
  66. frontswap_invalidates++;
  67. }
  68. #else
  69. static inline void inc_frontswap_loads(void) { }
  70. static inline void inc_frontswap_succ_stores(void) { }
  71. static inline void inc_frontswap_failed_stores(void) { }
  72. static inline void inc_frontswap_invalidates(void) { }
  73. #endif
  74. /*
  75. * Due to the asynchronous nature of the backends loading potentially
  76. * _after_ the swap system has been activated, we have chokepoints
  77. * on all frontswap functions to not call the backend until the backend
  78. * has registered.
  79. *
  80. * This would not guards us against the user deciding to call swapoff right as
  81. * we are calling the backend to initialize (so swapon is in action).
  82. * Fortunatly for us, the swapon_mutex has been taked by the callee so we are
  83. * OK. The other scenario where calls to frontswap_store (called via
  84. * swap_writepage) is racing with frontswap_invalidate_area (called via
  85. * swapoff) is again guarded by the swap subsystem.
  86. *
  87. * While no backend is registered all calls to frontswap_[store|load|
  88. * invalidate_area|invalidate_page] are ignored or fail.
  89. *
  90. * The time between the backend being registered and the swap file system
  91. * calling the backend (via the frontswap_* functions) is indeterminate as
  92. * frontswap_ops is not atomic_t (or a value guarded by a spinlock).
  93. * That is OK as we are comfortable missing some of these calls to the newly
  94. * registered backend.
  95. *
  96. * Obviously the opposite (unloading the backend) must be done after all
  97. * the frontswap_[store|load|invalidate_area|invalidate_page] start
  98. * ignoring or failing the requests. However, there is currently no way
  99. * to unload a backend once it is registered.
  100. */
  101. /*
  102. * Register operations for frontswap
  103. */
  104. void frontswap_register_ops(struct frontswap_ops *ops)
  105. {
  106. DECLARE_BITMAP(a, MAX_SWAPFILES);
  107. DECLARE_BITMAP(b, MAX_SWAPFILES);
  108. struct swap_info_struct *si;
  109. unsigned int i;
  110. bitmap_zero(a, MAX_SWAPFILES);
  111. bitmap_zero(b, MAX_SWAPFILES);
  112. spin_lock(&swap_lock);
  113. plist_for_each_entry(si, &swap_active_head, list) {
  114. if (!WARN_ON(!si->frontswap_map))
  115. set_bit(si->type, a);
  116. }
  117. spin_unlock(&swap_lock);
  118. /* the new ops needs to know the currently active swap devices */
  119. for_each_set_bit(i, a, MAX_SWAPFILES)
  120. ops->init(i);
  121. /*
  122. * Setting frontswap_ops must happen after the ops->init() calls
  123. * above; cmpxchg implies smp_mb() which will ensure the init is
  124. * complete at this point.
  125. */
  126. do {
  127. ops->next = frontswap_ops;
  128. } while (cmpxchg(&frontswap_ops, ops->next, ops) != ops->next);
  129. static_branch_inc(&frontswap_enabled_key);
  130. spin_lock(&swap_lock);
  131. plist_for_each_entry(si, &swap_active_head, list) {
  132. if (si->frontswap_map)
  133. set_bit(si->type, b);
  134. }
  135. spin_unlock(&swap_lock);
  136. /*
  137. * On the very unlikely chance that a swap device was added or
  138. * removed between setting the "a" list bits and the ops init
  139. * calls, we re-check and do init or invalidate for any changed
  140. * bits.
  141. */
  142. if (unlikely(!bitmap_equal(a, b, MAX_SWAPFILES))) {
  143. for (i = 0; i < MAX_SWAPFILES; i++) {
  144. if (!test_bit(i, a) && test_bit(i, b))
  145. ops->init(i);
  146. else if (test_bit(i, a) && !test_bit(i, b))
  147. ops->invalidate_area(i);
  148. }
  149. }
  150. }
  151. EXPORT_SYMBOL(frontswap_register_ops);
  152. /*
  153. * Enable/disable frontswap writethrough (see above).
  154. */
  155. void frontswap_writethrough(bool enable)
  156. {
  157. frontswap_writethrough_enabled = enable;
  158. }
  159. EXPORT_SYMBOL(frontswap_writethrough);
  160. /*
  161. * Enable/disable frontswap exclusive gets (see above).
  162. */
  163. void frontswap_tmem_exclusive_gets(bool enable)
  164. {
  165. frontswap_tmem_exclusive_gets_enabled = enable;
  166. }
  167. EXPORT_SYMBOL(frontswap_tmem_exclusive_gets);
  168. /*
  169. * Called when a swap device is swapon'd.
  170. */
  171. void __frontswap_init(unsigned type, unsigned long *map)
  172. {
  173. struct swap_info_struct *sis = swap_info[type];
  174. struct frontswap_ops *ops;
  175. VM_BUG_ON(sis == NULL);
  176. /*
  177. * p->frontswap is a bitmap that we MUST have to figure out which page
  178. * has gone in frontswap. Without it there is no point of continuing.
  179. */
  180. if (WARN_ON(!map))
  181. return;
  182. /*
  183. * Irregardless of whether the frontswap backend has been loaded
  184. * before this function or it will be later, we _MUST_ have the
  185. * p->frontswap set to something valid to work properly.
  186. */
  187. frontswap_map_set(sis, map);
  188. for_each_frontswap_ops(ops)
  189. ops->init(type);
  190. }
  191. EXPORT_SYMBOL(__frontswap_init);
  192. bool __frontswap_test(struct swap_info_struct *sis,
  193. pgoff_t offset)
  194. {
  195. if (sis->frontswap_map)
  196. return test_bit(offset, sis->frontswap_map);
  197. return false;
  198. }
  199. EXPORT_SYMBOL(__frontswap_test);
  200. static inline void __frontswap_set(struct swap_info_struct *sis,
  201. pgoff_t offset)
  202. {
  203. set_bit(offset, sis->frontswap_map);
  204. atomic_inc(&sis->frontswap_pages);
  205. }
  206. static inline void __frontswap_clear(struct swap_info_struct *sis,
  207. pgoff_t offset)
  208. {
  209. clear_bit(offset, sis->frontswap_map);
  210. atomic_dec(&sis->frontswap_pages);
  211. }
  212. /*
  213. * "Store" data from a page to frontswap and associate it with the page's
  214. * swaptype and offset. Page must be locked and in the swap cache.
  215. * If frontswap already contains a page with matching swaptype and
  216. * offset, the frontswap implementation may either overwrite the data and
  217. * return success or invalidate the page from frontswap and return failure.
  218. */
  219. int __frontswap_store(struct page *page)
  220. {
  221. int ret = -1;
  222. swp_entry_t entry = { .val = page_private(page), };
  223. int type = swp_type(entry);
  224. struct swap_info_struct *sis = swap_info[type];
  225. pgoff_t offset = swp_offset(entry);
  226. struct frontswap_ops *ops;
  227. VM_BUG_ON(!frontswap_ops);
  228. VM_BUG_ON(!PageLocked(page));
  229. VM_BUG_ON(sis == NULL);
  230. /*
  231. * If a dup, we must remove the old page first; we can't leave the
  232. * old page no matter if the store of the new page succeeds or fails,
  233. * and we can't rely on the new page replacing the old page as we may
  234. * not store to the same implementation that contains the old page.
  235. */
  236. if (__frontswap_test(sis, offset)) {
  237. __frontswap_clear(sis, offset);
  238. for_each_frontswap_ops(ops)
  239. ops->invalidate_page(type, offset);
  240. }
  241. /* Try to store in each implementation, until one succeeds. */
  242. for_each_frontswap_ops(ops) {
  243. ret = ops->store(type, offset, page);
  244. if (!ret) /* successful store */
  245. break;
  246. }
  247. if (ret == 0) {
  248. __frontswap_set(sis, offset);
  249. inc_frontswap_succ_stores();
  250. } else {
  251. inc_frontswap_failed_stores();
  252. }
  253. if (frontswap_writethrough_enabled)
  254. /* report failure so swap also writes to swap device */
  255. ret = -1;
  256. return ret;
  257. }
  258. EXPORT_SYMBOL(__frontswap_store);
  259. /*
  260. * "Get" data from frontswap associated with swaptype and offset that were
  261. * specified when the data was put to frontswap and use it to fill the
  262. * specified page with data. Page must be locked and in the swap cache.
  263. */
  264. int __frontswap_load(struct page *page)
  265. {
  266. int ret = -1;
  267. swp_entry_t entry = { .val = page_private(page), };
  268. int type = swp_type(entry);
  269. struct swap_info_struct *sis = swap_info[type];
  270. pgoff_t offset = swp_offset(entry);
  271. struct frontswap_ops *ops;
  272. VM_BUG_ON(!frontswap_ops);
  273. VM_BUG_ON(!PageLocked(page));
  274. VM_BUG_ON(sis == NULL);
  275. if (!__frontswap_test(sis, offset))
  276. return -1;
  277. /* Try loading from each implementation, until one succeeds. */
  278. for_each_frontswap_ops(ops) {
  279. ret = ops->load(type, offset, page);
  280. if (!ret) /* successful load */
  281. break;
  282. }
  283. if (ret == 0) {
  284. inc_frontswap_loads();
  285. if (frontswap_tmem_exclusive_gets_enabled) {
  286. SetPageDirty(page);
  287. __frontswap_clear(sis, offset);
  288. }
  289. }
  290. return ret;
  291. }
  292. EXPORT_SYMBOL(__frontswap_load);
  293. /*
  294. * Invalidate any data from frontswap associated with the specified swaptype
  295. * and offset so that a subsequent "get" will fail.
  296. */
  297. void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
  298. {
  299. struct swap_info_struct *sis = swap_info[type];
  300. struct frontswap_ops *ops;
  301. VM_BUG_ON(!frontswap_ops);
  302. VM_BUG_ON(sis == NULL);
  303. if (!__frontswap_test(sis, offset))
  304. return;
  305. for_each_frontswap_ops(ops)
  306. ops->invalidate_page(type, offset);
  307. __frontswap_clear(sis, offset);
  308. inc_frontswap_invalidates();
  309. }
  310. EXPORT_SYMBOL(__frontswap_invalidate_page);
  311. /*
  312. * Invalidate all data from frontswap associated with all offsets for the
  313. * specified swaptype.
  314. */
  315. void __frontswap_invalidate_area(unsigned type)
  316. {
  317. struct swap_info_struct *sis = swap_info[type];
  318. struct frontswap_ops *ops;
  319. VM_BUG_ON(!frontswap_ops);
  320. VM_BUG_ON(sis == NULL);
  321. if (sis->frontswap_map == NULL)
  322. return;
  323. for_each_frontswap_ops(ops)
  324. ops->invalidate_area(type);
  325. atomic_set(&sis->frontswap_pages, 0);
  326. bitmap_zero(sis->frontswap_map, sis->max);
  327. }
  328. EXPORT_SYMBOL(__frontswap_invalidate_area);
  329. static unsigned long __frontswap_curr_pages(void)
  330. {
  331. unsigned long totalpages = 0;
  332. struct swap_info_struct *si = NULL;
  333. assert_spin_locked(&swap_lock);
  334. plist_for_each_entry(si, &swap_active_head, list)
  335. totalpages += atomic_read(&si->frontswap_pages);
  336. return totalpages;
  337. }
  338. static int __frontswap_unuse_pages(unsigned long total, unsigned long *unused,
  339. int *swapid)
  340. {
  341. int ret = -EINVAL;
  342. struct swap_info_struct *si = NULL;
  343. int si_frontswap_pages;
  344. unsigned long total_pages_to_unuse = total;
  345. unsigned long pages = 0, pages_to_unuse = 0;
  346. assert_spin_locked(&swap_lock);
  347. plist_for_each_entry(si, &swap_active_head, list) {
  348. si_frontswap_pages = atomic_read(&si->frontswap_pages);
  349. if (total_pages_to_unuse < si_frontswap_pages) {
  350. pages = pages_to_unuse = total_pages_to_unuse;
  351. } else {
  352. pages = si_frontswap_pages;
  353. pages_to_unuse = 0; /* unuse all */
  354. }
  355. /* ensure there is enough RAM to fetch pages from frontswap */
  356. if (security_vm_enough_memory_mm(current->mm, pages)) {
  357. ret = -ENOMEM;
  358. continue;
  359. }
  360. vm_unacct_memory(pages);
  361. *unused = pages_to_unuse;
  362. *swapid = si->type;
  363. ret = 0;
  364. break;
  365. }
  366. return ret;
  367. }
  368. /*
  369. * Used to check if it's necessory and feasible to unuse pages.
  370. * Return 1 when nothing to do, 0 when need to shink pages,
  371. * error code when there is an error.
  372. */
  373. static int __frontswap_shrink(unsigned long target_pages,
  374. unsigned long *pages_to_unuse,
  375. int *type)
  376. {
  377. unsigned long total_pages = 0, total_pages_to_unuse;
  378. assert_spin_locked(&swap_lock);
  379. total_pages = __frontswap_curr_pages();
  380. if (total_pages <= target_pages) {
  381. /* Nothing to do */
  382. *pages_to_unuse = 0;
  383. return 1;
  384. }
  385. total_pages_to_unuse = total_pages - target_pages;
  386. return __frontswap_unuse_pages(total_pages_to_unuse, pages_to_unuse, type);
  387. }
  388. /*
  389. * Frontswap, like a true swap device, may unnecessarily retain pages
  390. * under certain circumstances; "shrink" frontswap is essentially a
  391. * "partial swapoff" and works by calling try_to_unuse to attempt to
  392. * unuse enough frontswap pages to attempt to -- subject to memory
  393. * constraints -- reduce the number of pages in frontswap to the
  394. * number given in the parameter target_pages.
  395. */
  396. void frontswap_shrink(unsigned long target_pages)
  397. {
  398. unsigned long pages_to_unuse = 0;
  399. int uninitialized_var(type), ret;
  400. /*
  401. * we don't want to hold swap_lock while doing a very
  402. * lengthy try_to_unuse, but swap_list may change
  403. * so restart scan from swap_active_head each time
  404. */
  405. spin_lock(&swap_lock);
  406. ret = __frontswap_shrink(target_pages, &pages_to_unuse, &type);
  407. spin_unlock(&swap_lock);
  408. if (ret == 0)
  409. try_to_unuse(type, true, pages_to_unuse);
  410. return;
  411. }
  412. EXPORT_SYMBOL(frontswap_shrink);
  413. /*
  414. * Count and return the number of frontswap pages across all
  415. * swap devices. This is exported so that backend drivers can
  416. * determine current usage without reading debugfs.
  417. */
  418. unsigned long frontswap_curr_pages(void)
  419. {
  420. unsigned long totalpages = 0;
  421. spin_lock(&swap_lock);
  422. totalpages = __frontswap_curr_pages();
  423. spin_unlock(&swap_lock);
  424. return totalpages;
  425. }
  426. EXPORT_SYMBOL(frontswap_curr_pages);
  427. static int __init init_frontswap(void)
  428. {
  429. #ifdef CONFIG_DEBUG_FS
  430. struct dentry *root = debugfs_create_dir("frontswap", NULL);
  431. if (root == NULL)
  432. return -ENXIO;
  433. debugfs_create_u64("loads", S_IRUGO, root, &frontswap_loads);
  434. debugfs_create_u64("succ_stores", S_IRUGO, root, &frontswap_succ_stores);
  435. debugfs_create_u64("failed_stores", S_IRUGO, root,
  436. &frontswap_failed_stores);
  437. debugfs_create_u64("invalidates", S_IRUGO,
  438. root, &frontswap_invalidates);
  439. #endif
  440. return 0;
  441. }
  442. module_init(init_frontswap);