frontswap.c 13 KB

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