frontswap.c 14 KB

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