zswap.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*
  2. * zswap.c - zswap driver file
  3. *
  4. * zswap is a backend for frontswap that takes pages that are in the process
  5. * of being swapped out and attempts to compress and store them in a
  6. * RAM-based memory pool. This can result in a significant I/O reduction on
  7. * the swap device and, in the case where decompressing from RAM is faster
  8. * than reading from the swap device, can also improve workload performance.
  9. *
  10. * Copyright (C) 2012 Seth Jennings <sjenning@linux.vnet.ibm.com>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/module.h>
  24. #include <linux/cpu.h>
  25. #include <linux/highmem.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/types.h>
  29. #include <linux/atomic.h>
  30. #include <linux/frontswap.h>
  31. #include <linux/rbtree.h>
  32. #include <linux/swap.h>
  33. #include <linux/crypto.h>
  34. #include <linux/mempool.h>
  35. #include <linux/zpool.h>
  36. #include <linux/mm_types.h>
  37. #include <linux/page-flags.h>
  38. #include <linux/swapops.h>
  39. #include <linux/writeback.h>
  40. #include <linux/pagemap.h>
  41. /*********************************
  42. * statistics
  43. **********************************/
  44. /* Total bytes used by the compressed storage */
  45. static u64 zswap_pool_total_size;
  46. /* The number of compressed pages currently stored in zswap */
  47. static atomic_t zswap_stored_pages = ATOMIC_INIT(0);
  48. /*
  49. * The statistics below are not protected from concurrent access for
  50. * performance reasons so they may not be a 100% accurate. However,
  51. * they do provide useful information on roughly how many times a
  52. * certain event is occurring.
  53. */
  54. /* Pool limit was hit (see zswap_max_pool_percent) */
  55. static u64 zswap_pool_limit_hit;
  56. /* Pages written back when pool limit was reached */
  57. static u64 zswap_written_back_pages;
  58. /* Store failed due to a reclaim failure after pool limit was reached */
  59. static u64 zswap_reject_reclaim_fail;
  60. /* Compressed page was too big for the allocator to (optimally) store */
  61. static u64 zswap_reject_compress_poor;
  62. /* Store failed because underlying allocator could not get memory */
  63. static u64 zswap_reject_alloc_fail;
  64. /* Store failed because the entry metadata could not be allocated (rare) */
  65. static u64 zswap_reject_kmemcache_fail;
  66. /* Duplicate store was encountered (rare) */
  67. static u64 zswap_duplicate_entry;
  68. /*********************************
  69. * tunables
  70. **********************************/
  71. /* Enable/disable zswap (disabled by default, fixed at boot for now) */
  72. static bool zswap_enabled __read_mostly;
  73. module_param_named(enabled, zswap_enabled, bool, 0444);
  74. /* Compressor to be used by zswap (fixed at boot for now) */
  75. #define ZSWAP_COMPRESSOR_DEFAULT "lzo"
  76. static char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
  77. module_param_named(compressor, zswap_compressor, charp, 0444);
  78. /* The maximum percentage of memory that the compressed pool can occupy */
  79. static unsigned int zswap_max_pool_percent = 20;
  80. module_param_named(max_pool_percent,
  81. zswap_max_pool_percent, uint, 0644);
  82. /* Compressed storage to use */
  83. #define ZSWAP_ZPOOL_DEFAULT "zbud"
  84. static char *zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
  85. module_param_named(zpool, zswap_zpool_type, charp, 0444);
  86. /* zpool is shared by all of zswap backend */
  87. static struct zpool *zswap_pool;
  88. /*********************************
  89. * compression functions
  90. **********************************/
  91. /* per-cpu compression transforms */
  92. static struct crypto_comp * __percpu *zswap_comp_pcpu_tfms;
  93. enum comp_op {
  94. ZSWAP_COMPOP_COMPRESS,
  95. ZSWAP_COMPOP_DECOMPRESS
  96. };
  97. static int zswap_comp_op(enum comp_op op, const u8 *src, unsigned int slen,
  98. u8 *dst, unsigned int *dlen)
  99. {
  100. struct crypto_comp *tfm;
  101. int ret;
  102. tfm = *per_cpu_ptr(zswap_comp_pcpu_tfms, get_cpu());
  103. switch (op) {
  104. case ZSWAP_COMPOP_COMPRESS:
  105. ret = crypto_comp_compress(tfm, src, slen, dst, dlen);
  106. break;
  107. case ZSWAP_COMPOP_DECOMPRESS:
  108. ret = crypto_comp_decompress(tfm, src, slen, dst, dlen);
  109. break;
  110. default:
  111. ret = -EINVAL;
  112. }
  113. put_cpu();
  114. return ret;
  115. }
  116. static int __init zswap_comp_init(void)
  117. {
  118. if (!crypto_has_comp(zswap_compressor, 0, 0)) {
  119. pr_info("%s compressor not available\n", zswap_compressor);
  120. /* fall back to default compressor */
  121. zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
  122. if (!crypto_has_comp(zswap_compressor, 0, 0))
  123. /* can't even load the default compressor */
  124. return -ENODEV;
  125. }
  126. pr_info("using %s compressor\n", zswap_compressor);
  127. /* alloc percpu transforms */
  128. zswap_comp_pcpu_tfms = alloc_percpu(struct crypto_comp *);
  129. if (!zswap_comp_pcpu_tfms)
  130. return -ENOMEM;
  131. return 0;
  132. }
  133. static void __init zswap_comp_exit(void)
  134. {
  135. /* free percpu transforms */
  136. free_percpu(zswap_comp_pcpu_tfms);
  137. }
  138. /*********************************
  139. * data structures
  140. **********************************/
  141. /*
  142. * struct zswap_entry
  143. *
  144. * This structure contains the metadata for tracking a single compressed
  145. * page within zswap.
  146. *
  147. * rbnode - links the entry into red-black tree for the appropriate swap type
  148. * refcount - the number of outstanding reference to the entry. This is needed
  149. * to protect against premature freeing of the entry by code
  150. * concurrent calls to load, invalidate, and writeback. The lock
  151. * for the zswap_tree structure that contains the entry must
  152. * be held while changing the refcount. Since the lock must
  153. * be held, there is no reason to also make refcount atomic.
  154. * offset - the swap offset for the entry. Index into the red-black tree.
  155. * handle - zpool allocation handle that stores the compressed page data
  156. * length - the length in bytes of the compressed page data. Needed during
  157. * decompression
  158. */
  159. struct zswap_entry {
  160. struct rb_node rbnode;
  161. pgoff_t offset;
  162. int refcount;
  163. unsigned int length;
  164. unsigned long handle;
  165. };
  166. struct zswap_header {
  167. swp_entry_t swpentry;
  168. };
  169. /*
  170. * The tree lock in the zswap_tree struct protects a few things:
  171. * - the rbtree
  172. * - the refcount field of each entry in the tree
  173. */
  174. struct zswap_tree {
  175. struct rb_root rbroot;
  176. spinlock_t lock;
  177. };
  178. static struct zswap_tree *zswap_trees[MAX_SWAPFILES];
  179. /*********************************
  180. * zswap entry functions
  181. **********************************/
  182. static struct kmem_cache *zswap_entry_cache;
  183. static int __init zswap_entry_cache_create(void)
  184. {
  185. zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
  186. return zswap_entry_cache == NULL;
  187. }
  188. static void __init zswap_entry_cache_destroy(void)
  189. {
  190. kmem_cache_destroy(zswap_entry_cache);
  191. }
  192. static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp)
  193. {
  194. struct zswap_entry *entry;
  195. entry = kmem_cache_alloc(zswap_entry_cache, gfp);
  196. if (!entry)
  197. return NULL;
  198. entry->refcount = 1;
  199. RB_CLEAR_NODE(&entry->rbnode);
  200. return entry;
  201. }
  202. static void zswap_entry_cache_free(struct zswap_entry *entry)
  203. {
  204. kmem_cache_free(zswap_entry_cache, entry);
  205. }
  206. /*********************************
  207. * rbtree functions
  208. **********************************/
  209. static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset)
  210. {
  211. struct rb_node *node = root->rb_node;
  212. struct zswap_entry *entry;
  213. while (node) {
  214. entry = rb_entry(node, struct zswap_entry, rbnode);
  215. if (entry->offset > offset)
  216. node = node->rb_left;
  217. else if (entry->offset < offset)
  218. node = node->rb_right;
  219. else
  220. return entry;
  221. }
  222. return NULL;
  223. }
  224. /*
  225. * In the case that a entry with the same offset is found, a pointer to
  226. * the existing entry is stored in dupentry and the function returns -EEXIST
  227. */
  228. static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry,
  229. struct zswap_entry **dupentry)
  230. {
  231. struct rb_node **link = &root->rb_node, *parent = NULL;
  232. struct zswap_entry *myentry;
  233. while (*link) {
  234. parent = *link;
  235. myentry = rb_entry(parent, struct zswap_entry, rbnode);
  236. if (myentry->offset > entry->offset)
  237. link = &(*link)->rb_left;
  238. else if (myentry->offset < entry->offset)
  239. link = &(*link)->rb_right;
  240. else {
  241. *dupentry = myentry;
  242. return -EEXIST;
  243. }
  244. }
  245. rb_link_node(&entry->rbnode, parent, link);
  246. rb_insert_color(&entry->rbnode, root);
  247. return 0;
  248. }
  249. static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry)
  250. {
  251. if (!RB_EMPTY_NODE(&entry->rbnode)) {
  252. rb_erase(&entry->rbnode, root);
  253. RB_CLEAR_NODE(&entry->rbnode);
  254. }
  255. }
  256. /*
  257. * Carries out the common pattern of freeing and entry's zpool allocation,
  258. * freeing the entry itself, and decrementing the number of stored pages.
  259. */
  260. static void zswap_free_entry(struct zswap_entry *entry)
  261. {
  262. zpool_free(zswap_pool, entry->handle);
  263. zswap_entry_cache_free(entry);
  264. atomic_dec(&zswap_stored_pages);
  265. zswap_pool_total_size = zpool_get_total_size(zswap_pool);
  266. }
  267. /* caller must hold the tree lock */
  268. static void zswap_entry_get(struct zswap_entry *entry)
  269. {
  270. entry->refcount++;
  271. }
  272. /* caller must hold the tree lock
  273. * remove from the tree and free it, if nobody reference the entry
  274. */
  275. static void zswap_entry_put(struct zswap_tree *tree,
  276. struct zswap_entry *entry)
  277. {
  278. int refcount = --entry->refcount;
  279. BUG_ON(refcount < 0);
  280. if (refcount == 0) {
  281. zswap_rb_erase(&tree->rbroot, entry);
  282. zswap_free_entry(entry);
  283. }
  284. }
  285. /* caller must hold the tree lock */
  286. static struct zswap_entry *zswap_entry_find_get(struct rb_root *root,
  287. pgoff_t offset)
  288. {
  289. struct zswap_entry *entry = NULL;
  290. entry = zswap_rb_search(root, offset);
  291. if (entry)
  292. zswap_entry_get(entry);
  293. return entry;
  294. }
  295. /*********************************
  296. * per-cpu code
  297. **********************************/
  298. static DEFINE_PER_CPU(u8 *, zswap_dstmem);
  299. static int __zswap_cpu_notifier(unsigned long action, unsigned long cpu)
  300. {
  301. struct crypto_comp *tfm;
  302. u8 *dst;
  303. switch (action) {
  304. case CPU_UP_PREPARE:
  305. tfm = crypto_alloc_comp(zswap_compressor, 0, 0);
  306. if (IS_ERR(tfm)) {
  307. pr_err("can't allocate compressor transform\n");
  308. return NOTIFY_BAD;
  309. }
  310. *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = tfm;
  311. dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
  312. if (!dst) {
  313. pr_err("can't allocate compressor buffer\n");
  314. crypto_free_comp(tfm);
  315. *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = NULL;
  316. return NOTIFY_BAD;
  317. }
  318. per_cpu(zswap_dstmem, cpu) = dst;
  319. break;
  320. case CPU_DEAD:
  321. case CPU_UP_CANCELED:
  322. tfm = *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu);
  323. if (tfm) {
  324. crypto_free_comp(tfm);
  325. *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = NULL;
  326. }
  327. dst = per_cpu(zswap_dstmem, cpu);
  328. kfree(dst);
  329. per_cpu(zswap_dstmem, cpu) = NULL;
  330. break;
  331. default:
  332. break;
  333. }
  334. return NOTIFY_OK;
  335. }
  336. static int zswap_cpu_notifier(struct notifier_block *nb,
  337. unsigned long action, void *pcpu)
  338. {
  339. unsigned long cpu = (unsigned long)pcpu;
  340. return __zswap_cpu_notifier(action, cpu);
  341. }
  342. static struct notifier_block zswap_cpu_notifier_block = {
  343. .notifier_call = zswap_cpu_notifier
  344. };
  345. static int __init zswap_cpu_init(void)
  346. {
  347. unsigned long cpu;
  348. cpu_notifier_register_begin();
  349. for_each_online_cpu(cpu)
  350. if (__zswap_cpu_notifier(CPU_UP_PREPARE, cpu) != NOTIFY_OK)
  351. goto cleanup;
  352. __register_cpu_notifier(&zswap_cpu_notifier_block);
  353. cpu_notifier_register_done();
  354. return 0;
  355. cleanup:
  356. for_each_online_cpu(cpu)
  357. __zswap_cpu_notifier(CPU_UP_CANCELED, cpu);
  358. cpu_notifier_register_done();
  359. return -ENOMEM;
  360. }
  361. /*********************************
  362. * helpers
  363. **********************************/
  364. static bool zswap_is_full(void)
  365. {
  366. return totalram_pages * zswap_max_pool_percent / 100 <
  367. DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
  368. }
  369. /*********************************
  370. * writeback code
  371. **********************************/
  372. /* return enum for zswap_get_swap_cache_page */
  373. enum zswap_get_swap_ret {
  374. ZSWAP_SWAPCACHE_NEW,
  375. ZSWAP_SWAPCACHE_EXIST,
  376. ZSWAP_SWAPCACHE_FAIL,
  377. };
  378. /*
  379. * zswap_get_swap_cache_page
  380. *
  381. * This is an adaption of read_swap_cache_async()
  382. *
  383. * This function tries to find a page with the given swap entry
  384. * in the swapper_space address space (the swap cache). If the page
  385. * is found, it is returned in retpage. Otherwise, a page is allocated,
  386. * added to the swap cache, and returned in retpage.
  387. *
  388. * If success, the swap cache page is returned in retpage
  389. * Returns ZSWAP_SWAPCACHE_EXIST if page was already in the swap cache
  390. * Returns ZSWAP_SWAPCACHE_NEW if the new page needs to be populated,
  391. * the new page is added to swapcache and locked
  392. * Returns ZSWAP_SWAPCACHE_FAIL on error
  393. */
  394. static int zswap_get_swap_cache_page(swp_entry_t entry,
  395. struct page **retpage)
  396. {
  397. struct page *found_page, *new_page = NULL;
  398. struct address_space *swapper_space = swap_address_space(entry);
  399. int err;
  400. *retpage = NULL;
  401. do {
  402. /*
  403. * First check the swap cache. Since this is normally
  404. * called after lookup_swap_cache() failed, re-calling
  405. * that would confuse statistics.
  406. */
  407. found_page = find_get_page(swapper_space, entry.val);
  408. if (found_page)
  409. break;
  410. /*
  411. * Get a new page to read into from swap.
  412. */
  413. if (!new_page) {
  414. new_page = alloc_page(GFP_KERNEL);
  415. if (!new_page)
  416. break; /* Out of memory */
  417. }
  418. /*
  419. * call radix_tree_preload() while we can wait.
  420. */
  421. err = radix_tree_preload(GFP_KERNEL);
  422. if (err)
  423. break;
  424. /*
  425. * Swap entry may have been freed since our caller observed it.
  426. */
  427. err = swapcache_prepare(entry);
  428. if (err == -EEXIST) { /* seems racy */
  429. radix_tree_preload_end();
  430. continue;
  431. }
  432. if (err) { /* swp entry is obsolete ? */
  433. radix_tree_preload_end();
  434. break;
  435. }
  436. /* May fail (-ENOMEM) if radix-tree node allocation failed. */
  437. __set_page_locked(new_page);
  438. SetPageSwapBacked(new_page);
  439. err = __add_to_swap_cache(new_page, entry);
  440. if (likely(!err)) {
  441. radix_tree_preload_end();
  442. lru_cache_add_anon(new_page);
  443. *retpage = new_page;
  444. return ZSWAP_SWAPCACHE_NEW;
  445. }
  446. radix_tree_preload_end();
  447. ClearPageSwapBacked(new_page);
  448. __clear_page_locked(new_page);
  449. /*
  450. * add_to_swap_cache() doesn't return -EEXIST, so we can safely
  451. * clear SWAP_HAS_CACHE flag.
  452. */
  453. swapcache_free(entry);
  454. } while (err != -ENOMEM);
  455. if (new_page)
  456. page_cache_release(new_page);
  457. if (!found_page)
  458. return ZSWAP_SWAPCACHE_FAIL;
  459. *retpage = found_page;
  460. return ZSWAP_SWAPCACHE_EXIST;
  461. }
  462. /*
  463. * Attempts to free an entry by adding a page to the swap cache,
  464. * decompressing the entry data into the page, and issuing a
  465. * bio write to write the page back to the swap device.
  466. *
  467. * This can be thought of as a "resumed writeback" of the page
  468. * to the swap device. We are basically resuming the same swap
  469. * writeback path that was intercepted with the frontswap_store()
  470. * in the first place. After the page has been decompressed into
  471. * the swap cache, the compressed version stored by zswap can be
  472. * freed.
  473. */
  474. static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
  475. {
  476. struct zswap_header *zhdr;
  477. swp_entry_t swpentry;
  478. struct zswap_tree *tree;
  479. pgoff_t offset;
  480. struct zswap_entry *entry;
  481. struct page *page;
  482. u8 *src, *dst;
  483. unsigned int dlen;
  484. int ret;
  485. struct writeback_control wbc = {
  486. .sync_mode = WB_SYNC_NONE,
  487. };
  488. /* extract swpentry from data */
  489. zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO);
  490. swpentry = zhdr->swpentry; /* here */
  491. zpool_unmap_handle(pool, handle);
  492. tree = zswap_trees[swp_type(swpentry)];
  493. offset = swp_offset(swpentry);
  494. /* find and ref zswap entry */
  495. spin_lock(&tree->lock);
  496. entry = zswap_entry_find_get(&tree->rbroot, offset);
  497. if (!entry) {
  498. /* entry was invalidated */
  499. spin_unlock(&tree->lock);
  500. return 0;
  501. }
  502. spin_unlock(&tree->lock);
  503. BUG_ON(offset != entry->offset);
  504. /* try to allocate swap cache page */
  505. switch (zswap_get_swap_cache_page(swpentry, &page)) {
  506. case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */
  507. ret = -ENOMEM;
  508. goto fail;
  509. case ZSWAP_SWAPCACHE_EXIST:
  510. /* page is already in the swap cache, ignore for now */
  511. page_cache_release(page);
  512. ret = -EEXIST;
  513. goto fail;
  514. case ZSWAP_SWAPCACHE_NEW: /* page is locked */
  515. /* decompress */
  516. dlen = PAGE_SIZE;
  517. src = (u8 *)zpool_map_handle(zswap_pool, entry->handle,
  518. ZPOOL_MM_RO) + sizeof(struct zswap_header);
  519. dst = kmap_atomic(page);
  520. ret = zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS, src,
  521. entry->length, dst, &dlen);
  522. kunmap_atomic(dst);
  523. zpool_unmap_handle(zswap_pool, entry->handle);
  524. BUG_ON(ret);
  525. BUG_ON(dlen != PAGE_SIZE);
  526. /* page is up to date */
  527. SetPageUptodate(page);
  528. }
  529. /* move it to the tail of the inactive list after end_writeback */
  530. SetPageReclaim(page);
  531. /* start writeback */
  532. __swap_writepage(page, &wbc, end_swap_bio_write);
  533. page_cache_release(page);
  534. zswap_written_back_pages++;
  535. spin_lock(&tree->lock);
  536. /* drop local reference */
  537. zswap_entry_put(tree, entry);
  538. /*
  539. * There are two possible situations for entry here:
  540. * (1) refcount is 1(normal case), entry is valid and on the tree
  541. * (2) refcount is 0, entry is freed and not on the tree
  542. * because invalidate happened during writeback
  543. * search the tree and free the entry if find entry
  544. */
  545. if (entry == zswap_rb_search(&tree->rbroot, offset))
  546. zswap_entry_put(tree, entry);
  547. spin_unlock(&tree->lock);
  548. goto end;
  549. /*
  550. * if we get here due to ZSWAP_SWAPCACHE_EXIST
  551. * a load may happening concurrently
  552. * it is safe and okay to not free the entry
  553. * if we free the entry in the following put
  554. * it it either okay to return !0
  555. */
  556. fail:
  557. spin_lock(&tree->lock);
  558. zswap_entry_put(tree, entry);
  559. spin_unlock(&tree->lock);
  560. end:
  561. return ret;
  562. }
  563. /*********************************
  564. * frontswap hooks
  565. **********************************/
  566. /* attempts to compress and store an single page */
  567. static int zswap_frontswap_store(unsigned type, pgoff_t offset,
  568. struct page *page)
  569. {
  570. struct zswap_tree *tree = zswap_trees[type];
  571. struct zswap_entry *entry, *dupentry;
  572. int ret;
  573. unsigned int dlen = PAGE_SIZE, len;
  574. unsigned long handle;
  575. char *buf;
  576. u8 *src, *dst;
  577. struct zswap_header *zhdr;
  578. if (!tree) {
  579. ret = -ENODEV;
  580. goto reject;
  581. }
  582. /* reclaim space if needed */
  583. if (zswap_is_full()) {
  584. zswap_pool_limit_hit++;
  585. if (zpool_shrink(zswap_pool, 1, NULL)) {
  586. zswap_reject_reclaim_fail++;
  587. ret = -ENOMEM;
  588. goto reject;
  589. }
  590. }
  591. /* allocate entry */
  592. entry = zswap_entry_cache_alloc(GFP_KERNEL);
  593. if (!entry) {
  594. zswap_reject_kmemcache_fail++;
  595. ret = -ENOMEM;
  596. goto reject;
  597. }
  598. /* compress */
  599. dst = get_cpu_var(zswap_dstmem);
  600. src = kmap_atomic(page);
  601. ret = zswap_comp_op(ZSWAP_COMPOP_COMPRESS, src, PAGE_SIZE, dst, &dlen);
  602. kunmap_atomic(src);
  603. if (ret) {
  604. ret = -EINVAL;
  605. goto freepage;
  606. }
  607. /* store */
  608. len = dlen + sizeof(struct zswap_header);
  609. ret = zpool_malloc(zswap_pool, len, __GFP_NORETRY | __GFP_NOWARN,
  610. &handle);
  611. if (ret == -ENOSPC) {
  612. zswap_reject_compress_poor++;
  613. goto freepage;
  614. }
  615. if (ret) {
  616. zswap_reject_alloc_fail++;
  617. goto freepage;
  618. }
  619. zhdr = zpool_map_handle(zswap_pool, handle, ZPOOL_MM_RW);
  620. zhdr->swpentry = swp_entry(type, offset);
  621. buf = (u8 *)(zhdr + 1);
  622. memcpy(buf, dst, dlen);
  623. zpool_unmap_handle(zswap_pool, handle);
  624. put_cpu_var(zswap_dstmem);
  625. /* populate entry */
  626. entry->offset = offset;
  627. entry->handle = handle;
  628. entry->length = dlen;
  629. /* map */
  630. spin_lock(&tree->lock);
  631. do {
  632. ret = zswap_rb_insert(&tree->rbroot, entry, &dupentry);
  633. if (ret == -EEXIST) {
  634. zswap_duplicate_entry++;
  635. /* remove from rbtree */
  636. zswap_rb_erase(&tree->rbroot, dupentry);
  637. zswap_entry_put(tree, dupentry);
  638. }
  639. } while (ret == -EEXIST);
  640. spin_unlock(&tree->lock);
  641. /* update stats */
  642. atomic_inc(&zswap_stored_pages);
  643. zswap_pool_total_size = zpool_get_total_size(zswap_pool);
  644. return 0;
  645. freepage:
  646. put_cpu_var(zswap_dstmem);
  647. zswap_entry_cache_free(entry);
  648. reject:
  649. return ret;
  650. }
  651. /*
  652. * returns 0 if the page was successfully decompressed
  653. * return -1 on entry not found or error
  654. */
  655. static int zswap_frontswap_load(unsigned type, pgoff_t offset,
  656. struct page *page)
  657. {
  658. struct zswap_tree *tree = zswap_trees[type];
  659. struct zswap_entry *entry;
  660. u8 *src, *dst;
  661. unsigned int dlen;
  662. int ret;
  663. /* find */
  664. spin_lock(&tree->lock);
  665. entry = zswap_entry_find_get(&tree->rbroot, offset);
  666. if (!entry) {
  667. /* entry was written back */
  668. spin_unlock(&tree->lock);
  669. return -1;
  670. }
  671. spin_unlock(&tree->lock);
  672. /* decompress */
  673. dlen = PAGE_SIZE;
  674. src = (u8 *)zpool_map_handle(zswap_pool, entry->handle,
  675. ZPOOL_MM_RO) + sizeof(struct zswap_header);
  676. dst = kmap_atomic(page);
  677. ret = zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS, src, entry->length,
  678. dst, &dlen);
  679. kunmap_atomic(dst);
  680. zpool_unmap_handle(zswap_pool, entry->handle);
  681. BUG_ON(ret);
  682. spin_lock(&tree->lock);
  683. zswap_entry_put(tree, entry);
  684. spin_unlock(&tree->lock);
  685. return 0;
  686. }
  687. /* frees an entry in zswap */
  688. static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset)
  689. {
  690. struct zswap_tree *tree = zswap_trees[type];
  691. struct zswap_entry *entry;
  692. /* find */
  693. spin_lock(&tree->lock);
  694. entry = zswap_rb_search(&tree->rbroot, offset);
  695. if (!entry) {
  696. /* entry was written back */
  697. spin_unlock(&tree->lock);
  698. return;
  699. }
  700. /* remove from rbtree */
  701. zswap_rb_erase(&tree->rbroot, entry);
  702. /* drop the initial reference from entry creation */
  703. zswap_entry_put(tree, entry);
  704. spin_unlock(&tree->lock);
  705. }
  706. /* frees all zswap entries for the given swap type */
  707. static void zswap_frontswap_invalidate_area(unsigned type)
  708. {
  709. struct zswap_tree *tree = zswap_trees[type];
  710. struct zswap_entry *entry, *n;
  711. if (!tree)
  712. return;
  713. /* walk the tree and free everything */
  714. spin_lock(&tree->lock);
  715. rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode)
  716. zswap_free_entry(entry);
  717. tree->rbroot = RB_ROOT;
  718. spin_unlock(&tree->lock);
  719. kfree(tree);
  720. zswap_trees[type] = NULL;
  721. }
  722. static struct zpool_ops zswap_zpool_ops = {
  723. .evict = zswap_writeback_entry
  724. };
  725. static void zswap_frontswap_init(unsigned type)
  726. {
  727. struct zswap_tree *tree;
  728. tree = kzalloc(sizeof(struct zswap_tree), GFP_KERNEL);
  729. if (!tree) {
  730. pr_err("alloc failed, zswap disabled for swap type %d\n", type);
  731. return;
  732. }
  733. tree->rbroot = RB_ROOT;
  734. spin_lock_init(&tree->lock);
  735. zswap_trees[type] = tree;
  736. }
  737. static struct frontswap_ops zswap_frontswap_ops = {
  738. .store = zswap_frontswap_store,
  739. .load = zswap_frontswap_load,
  740. .invalidate_page = zswap_frontswap_invalidate_page,
  741. .invalidate_area = zswap_frontswap_invalidate_area,
  742. .init = zswap_frontswap_init
  743. };
  744. /*********************************
  745. * debugfs functions
  746. **********************************/
  747. #ifdef CONFIG_DEBUG_FS
  748. #include <linux/debugfs.h>
  749. static struct dentry *zswap_debugfs_root;
  750. static int __init zswap_debugfs_init(void)
  751. {
  752. if (!debugfs_initialized())
  753. return -ENODEV;
  754. zswap_debugfs_root = debugfs_create_dir("zswap", NULL);
  755. if (!zswap_debugfs_root)
  756. return -ENOMEM;
  757. debugfs_create_u64("pool_limit_hit", S_IRUGO,
  758. zswap_debugfs_root, &zswap_pool_limit_hit);
  759. debugfs_create_u64("reject_reclaim_fail", S_IRUGO,
  760. zswap_debugfs_root, &zswap_reject_reclaim_fail);
  761. debugfs_create_u64("reject_alloc_fail", S_IRUGO,
  762. zswap_debugfs_root, &zswap_reject_alloc_fail);
  763. debugfs_create_u64("reject_kmemcache_fail", S_IRUGO,
  764. zswap_debugfs_root, &zswap_reject_kmemcache_fail);
  765. debugfs_create_u64("reject_compress_poor", S_IRUGO,
  766. zswap_debugfs_root, &zswap_reject_compress_poor);
  767. debugfs_create_u64("written_back_pages", S_IRUGO,
  768. zswap_debugfs_root, &zswap_written_back_pages);
  769. debugfs_create_u64("duplicate_entry", S_IRUGO,
  770. zswap_debugfs_root, &zswap_duplicate_entry);
  771. debugfs_create_u64("pool_total_size", S_IRUGO,
  772. zswap_debugfs_root, &zswap_pool_total_size);
  773. debugfs_create_atomic_t("stored_pages", S_IRUGO,
  774. zswap_debugfs_root, &zswap_stored_pages);
  775. return 0;
  776. }
  777. static void __exit zswap_debugfs_exit(void)
  778. {
  779. debugfs_remove_recursive(zswap_debugfs_root);
  780. }
  781. #else
  782. static int __init zswap_debugfs_init(void)
  783. {
  784. return 0;
  785. }
  786. static void __exit zswap_debugfs_exit(void) { }
  787. #endif
  788. /*********************************
  789. * module init and exit
  790. **********************************/
  791. static int __init init_zswap(void)
  792. {
  793. gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN;
  794. if (!zswap_enabled)
  795. return 0;
  796. pr_info("loading zswap\n");
  797. zswap_pool = zpool_create_pool(zswap_zpool_type, "zswap", gfp,
  798. &zswap_zpool_ops);
  799. if (!zswap_pool && strcmp(zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT)) {
  800. pr_info("%s zpool not available\n", zswap_zpool_type);
  801. zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
  802. zswap_pool = zpool_create_pool(zswap_zpool_type, "zswap", gfp,
  803. &zswap_zpool_ops);
  804. }
  805. if (!zswap_pool) {
  806. pr_err("%s zpool not available\n", zswap_zpool_type);
  807. pr_err("zpool creation failed\n");
  808. goto error;
  809. }
  810. pr_info("using %s pool\n", zswap_zpool_type);
  811. if (zswap_entry_cache_create()) {
  812. pr_err("entry cache creation failed\n");
  813. goto cachefail;
  814. }
  815. if (zswap_comp_init()) {
  816. pr_err("compressor initialization failed\n");
  817. goto compfail;
  818. }
  819. if (zswap_cpu_init()) {
  820. pr_err("per-cpu initialization failed\n");
  821. goto pcpufail;
  822. }
  823. frontswap_register_ops(&zswap_frontswap_ops);
  824. if (zswap_debugfs_init())
  825. pr_warn("debugfs initialization failed\n");
  826. return 0;
  827. pcpufail:
  828. zswap_comp_exit();
  829. compfail:
  830. zswap_entry_cache_destroy();
  831. cachefail:
  832. zpool_destroy_pool(zswap_pool);
  833. error:
  834. return -ENOMEM;
  835. }
  836. /* must be late so crypto has time to come up */
  837. late_initcall(init_zswap);
  838. MODULE_LICENSE("GPL");
  839. MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
  840. MODULE_DESCRIPTION("Compressed cache for swap pages");