frontswap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. }
  230. if (frontswap_writethrough_enabled)
  231. /* report failure so swap also writes to swap device */
  232. ret = -1;
  233. return ret;
  234. }
  235. EXPORT_SYMBOL(__frontswap_store);
  236. /*
  237. * "Get" data from frontswap associated with swaptype and offset that were
  238. * specified when the data was put to frontswap and use it to fill the
  239. * specified page with data. Page must be locked and in the swap cache.
  240. */
  241. int __frontswap_load(struct page *page)
  242. {
  243. int ret = -1;
  244. swp_entry_t entry = { .val = page_private(page), };
  245. int type = swp_type(entry);
  246. struct swap_info_struct *sis = swap_info[type];
  247. pgoff_t offset = swp_offset(entry);
  248. BUG_ON(!PageLocked(page));
  249. BUG_ON(sis == NULL);
  250. /*
  251. * __frontswap_test() will check whether there is backend registered
  252. */
  253. if (__frontswap_test(sis, offset))
  254. ret = frontswap_ops->load(type, offset, page);
  255. if (ret == 0) {
  256. inc_frontswap_loads();
  257. if (frontswap_tmem_exclusive_gets_enabled) {
  258. SetPageDirty(page);
  259. __frontswap_clear(sis, offset);
  260. }
  261. }
  262. return ret;
  263. }
  264. EXPORT_SYMBOL(__frontswap_load);
  265. /*
  266. * Invalidate any data from frontswap associated with the specified swaptype
  267. * and offset so that a subsequent "get" will fail.
  268. */
  269. void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
  270. {
  271. struct swap_info_struct *sis = swap_info[type];
  272. BUG_ON(sis == NULL);
  273. /*
  274. * __frontswap_test() will check whether there is backend registered
  275. */
  276. if (__frontswap_test(sis, offset)) {
  277. frontswap_ops->invalidate_page(type, offset);
  278. __frontswap_clear(sis, offset);
  279. inc_frontswap_invalidates();
  280. }
  281. }
  282. EXPORT_SYMBOL(__frontswap_invalidate_page);
  283. /*
  284. * Invalidate all data from frontswap associated with all offsets for the
  285. * specified swaptype.
  286. */
  287. void __frontswap_invalidate_area(unsigned type)
  288. {
  289. struct swap_info_struct *sis = swap_info[type];
  290. if (frontswap_ops) {
  291. BUG_ON(sis == NULL);
  292. if (sis->frontswap_map == NULL)
  293. return;
  294. frontswap_ops->invalidate_area(type);
  295. atomic_set(&sis->frontswap_pages, 0);
  296. memset(sis->frontswap_map, 0, sis->max / sizeof(long));
  297. }
  298. clear_bit(type, need_init);
  299. }
  300. EXPORT_SYMBOL(__frontswap_invalidate_area);
  301. static unsigned long __frontswap_curr_pages(void)
  302. {
  303. int type;
  304. unsigned long totalpages = 0;
  305. struct swap_info_struct *si = NULL;
  306. assert_spin_locked(&swap_lock);
  307. for (type = swap_list.head; type >= 0; type = si->next) {
  308. si = swap_info[type];
  309. totalpages += atomic_read(&si->frontswap_pages);
  310. }
  311. return totalpages;
  312. }
  313. static int __frontswap_unuse_pages(unsigned long total, unsigned long *unused,
  314. int *swapid)
  315. {
  316. int ret = -EINVAL;
  317. struct swap_info_struct *si = NULL;
  318. int si_frontswap_pages;
  319. unsigned long total_pages_to_unuse = total;
  320. unsigned long pages = 0, pages_to_unuse = 0;
  321. int type;
  322. assert_spin_locked(&swap_lock);
  323. for (type = swap_list.head; type >= 0; type = si->next) {
  324. si = swap_info[type];
  325. si_frontswap_pages = atomic_read(&si->frontswap_pages);
  326. if (total_pages_to_unuse < si_frontswap_pages) {
  327. pages = pages_to_unuse = total_pages_to_unuse;
  328. } else {
  329. pages = si_frontswap_pages;
  330. pages_to_unuse = 0; /* unuse all */
  331. }
  332. /* ensure there is enough RAM to fetch pages from frontswap */
  333. if (security_vm_enough_memory_mm(current->mm, pages)) {
  334. ret = -ENOMEM;
  335. continue;
  336. }
  337. vm_unacct_memory(pages);
  338. *unused = pages_to_unuse;
  339. *swapid = type;
  340. ret = 0;
  341. break;
  342. }
  343. return ret;
  344. }
  345. /*
  346. * Used to check if it's necessory and feasible to unuse pages.
  347. * Return 1 when nothing to do, 0 when need to shink pages,
  348. * error code when there is an error.
  349. */
  350. static int __frontswap_shrink(unsigned long target_pages,
  351. unsigned long *pages_to_unuse,
  352. int *type)
  353. {
  354. unsigned long total_pages = 0, total_pages_to_unuse;
  355. assert_spin_locked(&swap_lock);
  356. total_pages = __frontswap_curr_pages();
  357. if (total_pages <= target_pages) {
  358. /* Nothing to do */
  359. *pages_to_unuse = 0;
  360. return 1;
  361. }
  362. total_pages_to_unuse = total_pages - target_pages;
  363. return __frontswap_unuse_pages(total_pages_to_unuse, pages_to_unuse, type);
  364. }
  365. /*
  366. * Frontswap, like a true swap device, may unnecessarily retain pages
  367. * under certain circumstances; "shrink" frontswap is essentially a
  368. * "partial swapoff" and works by calling try_to_unuse to attempt to
  369. * unuse enough frontswap pages to attempt to -- subject to memory
  370. * constraints -- reduce the number of pages in frontswap to the
  371. * number given in the parameter target_pages.
  372. */
  373. void frontswap_shrink(unsigned long target_pages)
  374. {
  375. unsigned long pages_to_unuse = 0;
  376. int uninitialized_var(type), ret;
  377. /*
  378. * we don't want to hold swap_lock while doing a very
  379. * lengthy try_to_unuse, but swap_list may change
  380. * so restart scan from swap_list.head each time
  381. */
  382. spin_lock(&swap_lock);
  383. ret = __frontswap_shrink(target_pages, &pages_to_unuse, &type);
  384. spin_unlock(&swap_lock);
  385. if (ret == 0)
  386. try_to_unuse(type, true, pages_to_unuse);
  387. return;
  388. }
  389. EXPORT_SYMBOL(frontswap_shrink);
  390. /*
  391. * Count and return the number of frontswap pages across all
  392. * swap devices. This is exported so that backend drivers can
  393. * determine current usage without reading debugfs.
  394. */
  395. unsigned long frontswap_curr_pages(void)
  396. {
  397. unsigned long totalpages = 0;
  398. spin_lock(&swap_lock);
  399. totalpages = __frontswap_curr_pages();
  400. spin_unlock(&swap_lock);
  401. return totalpages;
  402. }
  403. EXPORT_SYMBOL(frontswap_curr_pages);
  404. static int __init init_frontswap(void)
  405. {
  406. #ifdef CONFIG_DEBUG_FS
  407. struct dentry *root = debugfs_create_dir("frontswap", NULL);
  408. if (root == NULL)
  409. return -ENXIO;
  410. debugfs_create_u64("loads", S_IRUGO, root, &frontswap_loads);
  411. debugfs_create_u64("succ_stores", S_IRUGO, root, &frontswap_succ_stores);
  412. debugfs_create_u64("failed_stores", S_IRUGO, root,
  413. &frontswap_failed_stores);
  414. debugfs_create_u64("invalidates", S_IRUGO,
  415. root, &frontswap_invalidates);
  416. #endif
  417. return 0;
  418. }
  419. module_init(init_frontswap);