zswap.c 24 KB

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