zswap.c 24 KB

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