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 zswap_comp_exit(void)
  134. {
  135. /* free percpu transforms */
  136. if (zswap_comp_pcpu_tfms)
  137. free_percpu(zswap_comp_pcpu_tfms);
  138. }
  139. /*********************************
  140. * data structures
  141. **********************************/
  142. /*
  143. * struct zswap_entry
  144. *
  145. * This structure contains the metadata for tracking a single compressed
  146. * page within zswap.
  147. *
  148. * rbnode - links the entry into red-black tree for the appropriate swap type
  149. * refcount - the number of outstanding reference to the entry. This is needed
  150. * to protect against premature freeing of the entry by code
  151. * concurrent calls to load, invalidate, and writeback. The lock
  152. * for the zswap_tree structure that contains the entry must
  153. * be held while changing the refcount. Since the lock must
  154. * be held, there is no reason to also make refcount atomic.
  155. * offset - the swap offset for the entry. Index into the red-black tree.
  156. * handle - zpool allocation handle that stores the compressed page data
  157. * length - the length in bytes of the compressed page data. Needed during
  158. * decompression
  159. */
  160. struct zswap_entry {
  161. struct rb_node rbnode;
  162. pgoff_t offset;
  163. int refcount;
  164. unsigned int length;
  165. unsigned long handle;
  166. };
  167. struct zswap_header {
  168. swp_entry_t swpentry;
  169. };
  170. /*
  171. * The tree lock in the zswap_tree struct protects a few things:
  172. * - the rbtree
  173. * - the refcount field of each entry in the tree
  174. */
  175. struct zswap_tree {
  176. struct rb_root rbroot;
  177. spinlock_t lock;
  178. };
  179. static struct zswap_tree *zswap_trees[MAX_SWAPFILES];
  180. /*********************************
  181. * zswap entry functions
  182. **********************************/
  183. static struct kmem_cache *zswap_entry_cache;
  184. static int zswap_entry_cache_create(void)
  185. {
  186. zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
  187. return zswap_entry_cache == NULL;
  188. }
  189. static void __init zswap_entry_cache_destroy(void)
  190. {
  191. kmem_cache_destroy(zswap_entry_cache);
  192. }
  193. static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp)
  194. {
  195. struct zswap_entry *entry;
  196. entry = kmem_cache_alloc(zswap_entry_cache, gfp);
  197. if (!entry)
  198. return NULL;
  199. entry->refcount = 1;
  200. RB_CLEAR_NODE(&entry->rbnode);
  201. return entry;
  202. }
  203. static void zswap_entry_cache_free(struct zswap_entry *entry)
  204. {
  205. kmem_cache_free(zswap_entry_cache, entry);
  206. }
  207. /*********************************
  208. * rbtree functions
  209. **********************************/
  210. static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset)
  211. {
  212. struct rb_node *node = root->rb_node;
  213. struct zswap_entry *entry;
  214. while (node) {
  215. entry = rb_entry(node, struct zswap_entry, rbnode);
  216. if (entry->offset > offset)
  217. node = node->rb_left;
  218. else if (entry->offset < offset)
  219. node = node->rb_right;
  220. else
  221. return entry;
  222. }
  223. return NULL;
  224. }
  225. /*
  226. * In the case that a entry with the same offset is found, a pointer to
  227. * the existing entry is stored in dupentry and the function returns -EEXIST
  228. */
  229. static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry,
  230. struct zswap_entry **dupentry)
  231. {
  232. struct rb_node **link = &root->rb_node, *parent = NULL;
  233. struct zswap_entry *myentry;
  234. while (*link) {
  235. parent = *link;
  236. myentry = rb_entry(parent, struct zswap_entry, rbnode);
  237. if (myentry->offset > entry->offset)
  238. link = &(*link)->rb_left;
  239. else if (myentry->offset < entry->offset)
  240. link = &(*link)->rb_right;
  241. else {
  242. *dupentry = myentry;
  243. return -EEXIST;
  244. }
  245. }
  246. rb_link_node(&entry->rbnode, parent, link);
  247. rb_insert_color(&entry->rbnode, root);
  248. return 0;
  249. }
  250. static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry)
  251. {
  252. if (!RB_EMPTY_NODE(&entry->rbnode)) {
  253. rb_erase(&entry->rbnode, root);
  254. RB_CLEAR_NODE(&entry->rbnode);
  255. }
  256. }
  257. /*
  258. * Carries out the common pattern of freeing and entry's zpool allocation,
  259. * freeing the entry itself, and decrementing the number of stored pages.
  260. */
  261. static void zswap_free_entry(struct zswap_entry *entry)
  262. {
  263. zpool_free(zswap_pool, entry->handle);
  264. zswap_entry_cache_free(entry);
  265. atomic_dec(&zswap_stored_pages);
  266. zswap_pool_total_size = zpool_get_total_size(zswap_pool);
  267. }
  268. /* caller must hold the tree lock */
  269. static void zswap_entry_get(struct zswap_entry *entry)
  270. {
  271. entry->refcount++;
  272. }
  273. /* caller must hold the tree lock
  274. * remove from the tree and free it, if nobody reference the entry
  275. */
  276. static void zswap_entry_put(struct zswap_tree *tree,
  277. struct zswap_entry *entry)
  278. {
  279. int refcount = --entry->refcount;
  280. BUG_ON(refcount < 0);
  281. if (refcount == 0) {
  282. zswap_rb_erase(&tree->rbroot, entry);
  283. zswap_free_entry(entry);
  284. }
  285. }
  286. /* caller must hold the tree lock */
  287. static struct zswap_entry *zswap_entry_find_get(struct rb_root *root,
  288. pgoff_t offset)
  289. {
  290. struct zswap_entry *entry = NULL;
  291. entry = zswap_rb_search(root, offset);
  292. if (entry)
  293. zswap_entry_get(entry);
  294. return entry;
  295. }
  296. /*********************************
  297. * per-cpu code
  298. **********************************/
  299. static DEFINE_PER_CPU(u8 *, zswap_dstmem);
  300. static int __zswap_cpu_notifier(unsigned long action, unsigned long cpu)
  301. {
  302. struct crypto_comp *tfm;
  303. u8 *dst;
  304. switch (action) {
  305. case CPU_UP_PREPARE:
  306. tfm = crypto_alloc_comp(zswap_compressor, 0, 0);
  307. if (IS_ERR(tfm)) {
  308. pr_err("can't allocate compressor transform\n");
  309. return NOTIFY_BAD;
  310. }
  311. *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = tfm;
  312. dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
  313. if (!dst) {
  314. pr_err("can't allocate compressor buffer\n");
  315. crypto_free_comp(tfm);
  316. *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = NULL;
  317. return NOTIFY_BAD;
  318. }
  319. per_cpu(zswap_dstmem, cpu) = dst;
  320. break;
  321. case CPU_DEAD:
  322. case CPU_UP_CANCELED:
  323. tfm = *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu);
  324. if (tfm) {
  325. crypto_free_comp(tfm);
  326. *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = NULL;
  327. }
  328. dst = per_cpu(zswap_dstmem, cpu);
  329. kfree(dst);
  330. per_cpu(zswap_dstmem, cpu) = NULL;
  331. break;
  332. default:
  333. break;
  334. }
  335. return NOTIFY_OK;
  336. }
  337. static int zswap_cpu_notifier(struct notifier_block *nb,
  338. unsigned long action, void *pcpu)
  339. {
  340. unsigned long cpu = (unsigned long)pcpu;
  341. return __zswap_cpu_notifier(action, cpu);
  342. }
  343. static struct notifier_block zswap_cpu_notifier_block = {
  344. .notifier_call = zswap_cpu_notifier
  345. };
  346. static int zswap_cpu_init(void)
  347. {
  348. unsigned long cpu;
  349. cpu_notifier_register_begin();
  350. for_each_online_cpu(cpu)
  351. if (__zswap_cpu_notifier(CPU_UP_PREPARE, cpu) != NOTIFY_OK)
  352. goto cleanup;
  353. __register_cpu_notifier(&zswap_cpu_notifier_block);
  354. cpu_notifier_register_done();
  355. return 0;
  356. cleanup:
  357. for_each_online_cpu(cpu)
  358. __zswap_cpu_notifier(CPU_UP_CANCELED, cpu);
  359. cpu_notifier_register_done();
  360. return -ENOMEM;
  361. }
  362. /*********************************
  363. * helpers
  364. **********************************/
  365. static bool zswap_is_full(void)
  366. {
  367. return totalram_pages * zswap_max_pool_percent / 100 <
  368. DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
  369. }
  370. /*********************************
  371. * writeback code
  372. **********************************/
  373. /* return enum for zswap_get_swap_cache_page */
  374. enum zswap_get_swap_ret {
  375. ZSWAP_SWAPCACHE_NEW,
  376. ZSWAP_SWAPCACHE_EXIST,
  377. ZSWAP_SWAPCACHE_FAIL,
  378. };
  379. /*
  380. * zswap_get_swap_cache_page
  381. *
  382. * This is an adaption of read_swap_cache_async()
  383. *
  384. * This function tries to find a page with the given swap entry
  385. * in the swapper_space address space (the swap cache). If the page
  386. * is found, it is returned in retpage. Otherwise, a page is allocated,
  387. * added to the swap cache, and returned in retpage.
  388. *
  389. * If success, the swap cache page is returned in retpage
  390. * Returns ZSWAP_SWAPCACHE_EXIST if page was already in the swap cache
  391. * Returns ZSWAP_SWAPCACHE_NEW if the new page needs to be populated,
  392. * the new page is added to swapcache and locked
  393. * Returns ZSWAP_SWAPCACHE_FAIL on error
  394. */
  395. static int zswap_get_swap_cache_page(swp_entry_t entry,
  396. struct page **retpage)
  397. {
  398. struct page *found_page, *new_page = NULL;
  399. struct address_space *swapper_space = swap_address_space(entry);
  400. int err;
  401. *retpage = NULL;
  402. do {
  403. /*
  404. * First check the swap cache. Since this is normally
  405. * called after lookup_swap_cache() failed, re-calling
  406. * that would confuse statistics.
  407. */
  408. found_page = find_get_page(swapper_space, entry.val);
  409. if (found_page)
  410. break;
  411. /*
  412. * Get a new page to read into from swap.
  413. */
  414. if (!new_page) {
  415. new_page = alloc_page(GFP_KERNEL);
  416. if (!new_page)
  417. break; /* Out of memory */
  418. }
  419. /*
  420. * call radix_tree_preload() while we can wait.
  421. */
  422. err = radix_tree_preload(GFP_KERNEL);
  423. if (err)
  424. break;
  425. /*
  426. * Swap entry may have been freed since our caller observed it.
  427. */
  428. err = swapcache_prepare(entry);
  429. if (err == -EEXIST) { /* seems racy */
  430. radix_tree_preload_end();
  431. continue;
  432. }
  433. if (err) { /* swp entry is obsolete ? */
  434. radix_tree_preload_end();
  435. break;
  436. }
  437. /* May fail (-ENOMEM) if radix-tree node allocation failed. */
  438. __set_page_locked(new_page);
  439. SetPageSwapBacked(new_page);
  440. err = __add_to_swap_cache(new_page, entry);
  441. if (likely(!err)) {
  442. radix_tree_preload_end();
  443. lru_cache_add_anon(new_page);
  444. *retpage = new_page;
  445. return ZSWAP_SWAPCACHE_NEW;
  446. }
  447. radix_tree_preload_end();
  448. ClearPageSwapBacked(new_page);
  449. __clear_page_locked(new_page);
  450. /*
  451. * add_to_swap_cache() doesn't return -EEXIST, so we can safely
  452. * clear SWAP_HAS_CACHE flag.
  453. */
  454. swapcache_free(entry);
  455. } while (err != -ENOMEM);
  456. if (new_page)
  457. page_cache_release(new_page);
  458. if (!found_page)
  459. return ZSWAP_SWAPCACHE_FAIL;
  460. *retpage = found_page;
  461. return ZSWAP_SWAPCACHE_EXIST;
  462. }
  463. /*
  464. * Attempts to free an entry by adding a page to the swap cache,
  465. * decompressing the entry data into the page, and issuing a
  466. * bio write to write the page back to the swap device.
  467. *
  468. * This can be thought of as a "resumed writeback" of the page
  469. * to the swap device. We are basically resuming the same swap
  470. * writeback path that was intercepted with the frontswap_store()
  471. * in the first place. After the page has been decompressed into
  472. * the swap cache, the compressed version stored by zswap can be
  473. * freed.
  474. */
  475. static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
  476. {
  477. struct zswap_header *zhdr;
  478. swp_entry_t swpentry;
  479. struct zswap_tree *tree;
  480. pgoff_t offset;
  481. struct zswap_entry *entry;
  482. struct page *page;
  483. u8 *src, *dst;
  484. unsigned int dlen;
  485. int ret;
  486. struct writeback_control wbc = {
  487. .sync_mode = WB_SYNC_NONE,
  488. };
  489. /* extract swpentry from data */
  490. zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO);
  491. swpentry = zhdr->swpentry; /* here */
  492. zpool_unmap_handle(pool, handle);
  493. tree = zswap_trees[swp_type(swpentry)];
  494. offset = swp_offset(swpentry);
  495. /* find and ref zswap entry */
  496. spin_lock(&tree->lock);
  497. entry = zswap_entry_find_get(&tree->rbroot, offset);
  498. if (!entry) {
  499. /* entry was invalidated */
  500. spin_unlock(&tree->lock);
  501. return 0;
  502. }
  503. spin_unlock(&tree->lock);
  504. BUG_ON(offset != entry->offset);
  505. /* try to allocate swap cache page */
  506. switch (zswap_get_swap_cache_page(swpentry, &page)) {
  507. case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */
  508. ret = -ENOMEM;
  509. goto fail;
  510. case ZSWAP_SWAPCACHE_EXIST:
  511. /* page is already in the swap cache, ignore for now */
  512. page_cache_release(page);
  513. ret = -EEXIST;
  514. goto fail;
  515. case ZSWAP_SWAPCACHE_NEW: /* page is locked */
  516. /* decompress */
  517. dlen = PAGE_SIZE;
  518. src = (u8 *)zpool_map_handle(zswap_pool, entry->handle,
  519. ZPOOL_MM_RO) + sizeof(struct zswap_header);
  520. dst = kmap_atomic(page);
  521. ret = zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS, src,
  522. entry->length, dst, &dlen);
  523. kunmap_atomic(dst);
  524. zpool_unmap_handle(zswap_pool, entry->handle);
  525. BUG_ON(ret);
  526. BUG_ON(dlen != PAGE_SIZE);
  527. /* page is up to date */
  528. SetPageUptodate(page);
  529. }
  530. /* move it to the tail of the inactive list after end_writeback */
  531. SetPageReclaim(page);
  532. /* start writeback */
  533. __swap_writepage(page, &wbc, end_swap_bio_write);
  534. page_cache_release(page);
  535. zswap_written_back_pages++;
  536. spin_lock(&tree->lock);
  537. /* drop local reference */
  538. zswap_entry_put(tree, entry);
  539. /*
  540. * There are two possible situations for entry here:
  541. * (1) refcount is 1(normal case), entry is valid and on the tree
  542. * (2) refcount is 0, entry is freed and not on the tree
  543. * because invalidate happened during writeback
  544. * search the tree and free the entry if find entry
  545. */
  546. if (entry == zswap_rb_search(&tree->rbroot, offset))
  547. zswap_entry_put(tree, entry);
  548. spin_unlock(&tree->lock);
  549. goto end;
  550. /*
  551. * if we get here due to ZSWAP_SWAPCACHE_EXIST
  552. * a load may happening concurrently
  553. * it is safe and okay to not free the entry
  554. * if we free the entry in the following put
  555. * it it either okay to return !0
  556. */
  557. fail:
  558. spin_lock(&tree->lock);
  559. zswap_entry_put(tree, entry);
  560. spin_unlock(&tree->lock);
  561. end:
  562. return ret;
  563. }
  564. /*********************************
  565. * frontswap hooks
  566. **********************************/
  567. /* attempts to compress and store an single page */
  568. static int zswap_frontswap_store(unsigned type, pgoff_t offset,
  569. struct page *page)
  570. {
  571. struct zswap_tree *tree = zswap_trees[type];
  572. struct zswap_entry *entry, *dupentry;
  573. int ret;
  574. unsigned int dlen = PAGE_SIZE, len;
  575. unsigned long handle;
  576. char *buf;
  577. u8 *src, *dst;
  578. struct zswap_header *zhdr;
  579. if (!tree) {
  580. ret = -ENODEV;
  581. goto reject;
  582. }
  583. /* reclaim space if needed */
  584. if (zswap_is_full()) {
  585. zswap_pool_limit_hit++;
  586. if (zpool_shrink(zswap_pool, 1, NULL)) {
  587. zswap_reject_reclaim_fail++;
  588. ret = -ENOMEM;
  589. goto reject;
  590. }
  591. }
  592. /* allocate entry */
  593. entry = zswap_entry_cache_alloc(GFP_KERNEL);
  594. if (!entry) {
  595. zswap_reject_kmemcache_fail++;
  596. ret = -ENOMEM;
  597. goto reject;
  598. }
  599. /* compress */
  600. dst = get_cpu_var(zswap_dstmem);
  601. src = kmap_atomic(page);
  602. ret = zswap_comp_op(ZSWAP_COMPOP_COMPRESS, src, PAGE_SIZE, dst, &dlen);
  603. kunmap_atomic(src);
  604. if (ret) {
  605. ret = -EINVAL;
  606. goto freepage;
  607. }
  608. /* store */
  609. len = dlen + sizeof(struct zswap_header);
  610. ret = zpool_malloc(zswap_pool, len, __GFP_NORETRY | __GFP_NOWARN,
  611. &handle);
  612. if (ret == -ENOSPC) {
  613. zswap_reject_compress_poor++;
  614. goto freepage;
  615. }
  616. if (ret) {
  617. zswap_reject_alloc_fail++;
  618. goto freepage;
  619. }
  620. zhdr = zpool_map_handle(zswap_pool, handle, ZPOOL_MM_RW);
  621. zhdr->swpentry = swp_entry(type, offset);
  622. buf = (u8 *)(zhdr + 1);
  623. memcpy(buf, dst, dlen);
  624. zpool_unmap_handle(zswap_pool, handle);
  625. put_cpu_var(zswap_dstmem);
  626. /* populate entry */
  627. entry->offset = offset;
  628. entry->handle = handle;
  629. entry->length = dlen;
  630. /* map */
  631. spin_lock(&tree->lock);
  632. do {
  633. ret = zswap_rb_insert(&tree->rbroot, entry, &dupentry);
  634. if (ret == -EEXIST) {
  635. zswap_duplicate_entry++;
  636. /* remove from rbtree */
  637. zswap_rb_erase(&tree->rbroot, dupentry);
  638. zswap_entry_put(tree, dupentry);
  639. }
  640. } while (ret == -EEXIST);
  641. spin_unlock(&tree->lock);
  642. /* update stats */
  643. atomic_inc(&zswap_stored_pages);
  644. zswap_pool_total_size = zpool_get_total_size(zswap_pool);
  645. return 0;
  646. freepage:
  647. put_cpu_var(zswap_dstmem);
  648. zswap_entry_cache_free(entry);
  649. reject:
  650. return ret;
  651. }
  652. /*
  653. * returns 0 if the page was successfully decompressed
  654. * return -1 on entry not found or error
  655. */
  656. static int zswap_frontswap_load(unsigned type, pgoff_t offset,
  657. struct page *page)
  658. {
  659. struct zswap_tree *tree = zswap_trees[type];
  660. struct zswap_entry *entry;
  661. u8 *src, *dst;
  662. unsigned int dlen;
  663. int ret;
  664. /* find */
  665. spin_lock(&tree->lock);
  666. entry = zswap_entry_find_get(&tree->rbroot, offset);
  667. if (!entry) {
  668. /* entry was written back */
  669. spin_unlock(&tree->lock);
  670. return -1;
  671. }
  672. spin_unlock(&tree->lock);
  673. /* decompress */
  674. dlen = PAGE_SIZE;
  675. src = (u8 *)zpool_map_handle(zswap_pool, entry->handle,
  676. ZPOOL_MM_RO) + sizeof(struct zswap_header);
  677. dst = kmap_atomic(page);
  678. ret = zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS, src, entry->length,
  679. dst, &dlen);
  680. kunmap_atomic(dst);
  681. zpool_unmap_handle(zswap_pool, entry->handle);
  682. BUG_ON(ret);
  683. spin_lock(&tree->lock);
  684. zswap_entry_put(tree, entry);
  685. spin_unlock(&tree->lock);
  686. return 0;
  687. }
  688. /* frees an entry in zswap */
  689. static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset)
  690. {
  691. struct zswap_tree *tree = zswap_trees[type];
  692. struct zswap_entry *entry;
  693. /* find */
  694. spin_lock(&tree->lock);
  695. entry = zswap_rb_search(&tree->rbroot, offset);
  696. if (!entry) {
  697. /* entry was written back */
  698. spin_unlock(&tree->lock);
  699. return;
  700. }
  701. /* remove from rbtree */
  702. zswap_rb_erase(&tree->rbroot, entry);
  703. /* drop the initial reference from entry creation */
  704. zswap_entry_put(tree, entry);
  705. spin_unlock(&tree->lock);
  706. }
  707. /* frees all zswap entries for the given swap type */
  708. static void zswap_frontswap_invalidate_area(unsigned type)
  709. {
  710. struct zswap_tree *tree = zswap_trees[type];
  711. struct zswap_entry *entry, *n;
  712. if (!tree)
  713. return;
  714. /* walk the tree and free everything */
  715. spin_lock(&tree->lock);
  716. rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode)
  717. zswap_free_entry(entry);
  718. tree->rbroot = RB_ROOT;
  719. spin_unlock(&tree->lock);
  720. kfree(tree);
  721. zswap_trees[type] = NULL;
  722. }
  723. static struct zpool_ops zswap_zpool_ops = {
  724. .evict = zswap_writeback_entry
  725. };
  726. static void zswap_frontswap_init(unsigned type)
  727. {
  728. struct zswap_tree *tree;
  729. tree = kzalloc(sizeof(struct zswap_tree), GFP_KERNEL);
  730. if (!tree) {
  731. pr_err("alloc failed, zswap disabled for swap type %d\n", type);
  732. return;
  733. }
  734. tree->rbroot = RB_ROOT;
  735. spin_lock_init(&tree->lock);
  736. zswap_trees[type] = tree;
  737. }
  738. static struct frontswap_ops zswap_frontswap_ops = {
  739. .store = zswap_frontswap_store,
  740. .load = zswap_frontswap_load,
  741. .invalidate_page = zswap_frontswap_invalidate_page,
  742. .invalidate_area = zswap_frontswap_invalidate_area,
  743. .init = zswap_frontswap_init
  744. };
  745. /*********************************
  746. * debugfs functions
  747. **********************************/
  748. #ifdef CONFIG_DEBUG_FS
  749. #include <linux/debugfs.h>
  750. static struct dentry *zswap_debugfs_root;
  751. static int __init zswap_debugfs_init(void)
  752. {
  753. if (!debugfs_initialized())
  754. return -ENODEV;
  755. zswap_debugfs_root = debugfs_create_dir("zswap", NULL);
  756. if (!zswap_debugfs_root)
  757. return -ENOMEM;
  758. debugfs_create_u64("pool_limit_hit", S_IRUGO,
  759. zswap_debugfs_root, &zswap_pool_limit_hit);
  760. debugfs_create_u64("reject_reclaim_fail", S_IRUGO,
  761. zswap_debugfs_root, &zswap_reject_reclaim_fail);
  762. debugfs_create_u64("reject_alloc_fail", S_IRUGO,
  763. zswap_debugfs_root, &zswap_reject_alloc_fail);
  764. debugfs_create_u64("reject_kmemcache_fail", S_IRUGO,
  765. zswap_debugfs_root, &zswap_reject_kmemcache_fail);
  766. debugfs_create_u64("reject_compress_poor", S_IRUGO,
  767. zswap_debugfs_root, &zswap_reject_compress_poor);
  768. debugfs_create_u64("written_back_pages", S_IRUGO,
  769. zswap_debugfs_root, &zswap_written_back_pages);
  770. debugfs_create_u64("duplicate_entry", S_IRUGO,
  771. zswap_debugfs_root, &zswap_duplicate_entry);
  772. debugfs_create_u64("pool_total_size", S_IRUGO,
  773. zswap_debugfs_root, &zswap_pool_total_size);
  774. debugfs_create_atomic_t("stored_pages", S_IRUGO,
  775. zswap_debugfs_root, &zswap_stored_pages);
  776. return 0;
  777. }
  778. static void __exit zswap_debugfs_exit(void)
  779. {
  780. debugfs_remove_recursive(zswap_debugfs_root);
  781. }
  782. #else
  783. static int __init zswap_debugfs_init(void)
  784. {
  785. return 0;
  786. }
  787. static void __exit zswap_debugfs_exit(void) { }
  788. #endif
  789. /*********************************
  790. * module init and exit
  791. **********************************/
  792. static int __init init_zswap(void)
  793. {
  794. gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN;
  795. if (!zswap_enabled)
  796. return 0;
  797. pr_info("loading zswap\n");
  798. zswap_pool = zpool_create_pool(zswap_zpool_type, gfp, &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, 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 <sjenning@linux.vnet.ibm.com>");
  840. MODULE_DESCRIPTION("Compressed cache for swap pages");