1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * zswap.c - zswap driver file
5 * zswap is a backend for frontswap that takes pages that are in the process
6 * of being swapped out and attempts to compress and store them in a
7 * RAM-based memory pool. This can result in a significant I/O reduction on
8 * the swap device and, in the case where decompressing from RAM is faster
9 * than reading from the swap device, can also improve workload performance.
11 * Copyright (C) 2012 Seth Jennings <sjenning@linux.vnet.ibm.com>
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/module.h>
17 #include <linux/cpu.h>
18 #include <linux/highmem.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/types.h>
22 #include <linux/atomic.h>
23 #include <linux/frontswap.h>
24 #include <linux/rbtree.h>
25 #include <linux/swap.h>
26 #include <linux/crypto.h>
27 #include <linux/scatterlist.h>
28 #include <linux/mempool.h>
29 #include <linux/zpool.h>
30 #include <crypto/acompress.h>
32 #include <linux/mm_types.h>
33 #include <linux/page-flags.h>
34 #include <linux/swapops.h>
35 #include <linux/writeback.h>
36 #include <linux/pagemap.h>
37 #include <linux/workqueue.h>
39 /*********************************
41 **********************************/
42 /* Total bytes used by the compressed storage */
43 static u64 zswap_pool_total_size;
44 /* The number of compressed pages currently stored in zswap */
45 static atomic_t zswap_stored_pages = ATOMIC_INIT(0);
46 /* The number of same-value filled pages currently stored in zswap */
47 static atomic_t zswap_same_filled_pages = ATOMIC_INIT(0);
50 * The statistics below are not protected from concurrent access for
51 * performance reasons so they may not be a 100% accurate. However,
52 * they do provide useful information on roughly how many times a
53 * certain event is occurring.
56 /* Pool limit was hit (see zswap_max_pool_percent) */
57 static u64 zswap_pool_limit_hit;
58 /* Pages written back when pool limit was reached */
59 static u64 zswap_written_back_pages;
60 /* Store failed due to a reclaim failure after pool limit was reached */
61 static u64 zswap_reject_reclaim_fail;
62 /* Compressed page was too big for the allocator to (optimally) store */
63 static u64 zswap_reject_compress_poor;
64 /* Store failed because underlying allocator could not get memory */
65 static u64 zswap_reject_alloc_fail;
66 /* Store failed because the entry metadata could not be allocated (rare) */
67 static u64 zswap_reject_kmemcache_fail;
68 /* Duplicate store was encountered (rare) */
69 static u64 zswap_duplicate_entry;
71 /* Shrinker work queue */
72 static struct workqueue_struct *shrink_wq;
73 /* Pool limit was hit, we need to calm down */
74 static bool zswap_pool_reached_full;
76 /*********************************
78 **********************************/
80 #define ZSWAP_PARAM_UNSET ""
82 /* Enable/disable zswap */
83 static bool zswap_enabled = IS_ENABLED(CONFIG_ZSWAP_DEFAULT_ON);
84 static int zswap_enabled_param_set(const char *,
85 const struct kernel_param *);
86 static const struct kernel_param_ops zswap_enabled_param_ops = {
87 .set = zswap_enabled_param_set,
88 .get = param_get_bool,
90 module_param_cb(enabled, &zswap_enabled_param_ops, &zswap_enabled, 0644);
92 /* Crypto compressor to use */
93 static char *zswap_compressor = CONFIG_ZSWAP_COMPRESSOR_DEFAULT;
94 static int zswap_compressor_param_set(const char *,
95 const struct kernel_param *);
96 static const struct kernel_param_ops zswap_compressor_param_ops = {
97 .set = zswap_compressor_param_set,
98 .get = param_get_charp,
99 .free = param_free_charp,
101 module_param_cb(compressor, &zswap_compressor_param_ops,
102 &zswap_compressor, 0644);
104 /* Compressed storage zpool to use */
105 static char *zswap_zpool_type = CONFIG_ZSWAP_ZPOOL_DEFAULT;
106 static int zswap_zpool_param_set(const char *, const struct kernel_param *);
107 static const struct kernel_param_ops zswap_zpool_param_ops = {
108 .set = zswap_zpool_param_set,
109 .get = param_get_charp,
110 .free = param_free_charp,
112 module_param_cb(zpool, &zswap_zpool_param_ops, &zswap_zpool_type, 0644);
114 /* The maximum percentage of memory that the compressed pool can occupy */
115 static unsigned int zswap_max_pool_percent = 20;
116 module_param_named(max_pool_percent, zswap_max_pool_percent, uint, 0644);
118 /* The threshold for accepting new pages after the max_pool_percent was hit */
119 static unsigned int zswap_accept_thr_percent = 90; /* of max pool size */
120 module_param_named(accept_threshold_percent, zswap_accept_thr_percent,
124 * Enable/disable handling same-value filled pages (enabled by default).
125 * If disabled every page is considered non-same-value filled.
127 static bool zswap_same_filled_pages_enabled = true;
128 module_param_named(same_filled_pages_enabled, zswap_same_filled_pages_enabled,
131 /* Enable/disable handling non-same-value filled pages (enabled by default) */
132 static bool zswap_non_same_filled_pages_enabled = true;
133 module_param_named(non_same_filled_pages_enabled, zswap_non_same_filled_pages_enabled,
136 /*********************************
138 **********************************/
140 struct crypto_acomp_ctx {
141 struct crypto_acomp *acomp;
142 struct acomp_req *req;
143 struct crypto_wait wait;
150 struct crypto_acomp_ctx __percpu *acomp_ctx;
152 struct list_head list;
153 struct work_struct release_work;
154 struct work_struct shrink_work;
155 struct hlist_node node;
156 char tfm_name[CRYPTO_MAX_ALG_NAME];
162 * This structure contains the metadata for tracking a single compressed
165 * rbnode - links the entry into red-black tree for the appropriate swap type
166 * offset - the swap offset for the entry. Index into the red-black tree.
167 * refcount - the number of outstanding reference to the entry. This is needed
168 * to protect against premature freeing of the entry by code
169 * concurrent calls to load, invalidate, and writeback. The lock
170 * for the zswap_tree structure that contains the entry must
171 * be held while changing the refcount. Since the lock must
172 * be held, there is no reason to also make refcount atomic.
173 * length - the length in bytes of the compressed page data. Needed during
174 * decompression. For a same value filled page length is 0.
175 * pool - the zswap_pool the entry's data is in
176 * handle - zpool allocation handle that stores the compressed page data
177 * value - value of the same-value filled pages which have same content
180 struct rb_node rbnode;
184 struct zswap_pool *pool;
186 unsigned long handle;
191 struct zswap_header {
192 swp_entry_t swpentry;
196 * The tree lock in the zswap_tree struct protects a few things:
198 * - the refcount field of each entry in the tree
201 struct rb_root rbroot;
205 static struct zswap_tree *zswap_trees[MAX_SWAPFILES];
207 /* RCU-protected iteration */
208 static LIST_HEAD(zswap_pools);
209 /* protects zswap_pools list modification */
210 static DEFINE_SPINLOCK(zswap_pools_lock);
211 /* pool counter to provide unique names to zpool */
212 static atomic_t zswap_pools_count = ATOMIC_INIT(0);
214 /* used by param callback function */
215 static bool zswap_init_started;
217 /* fatal error during init */
218 static bool zswap_init_failed;
220 /* init completed, but couldn't create the initial pool */
221 static bool zswap_has_pool;
223 /*********************************
224 * helpers and fwd declarations
225 **********************************/
227 #define zswap_pool_debug(msg, p) \
228 pr_debug("%s pool %s/%s\n", msg, (p)->tfm_name, \
229 zpool_get_type((p)->zpool))
231 static int zswap_writeback_entry(struct zpool *pool, unsigned long handle);
232 static int zswap_pool_get(struct zswap_pool *pool);
233 static void zswap_pool_put(struct zswap_pool *pool);
235 static const struct zpool_ops zswap_zpool_ops = {
236 .evict = zswap_writeback_entry
239 static bool zswap_is_full(void)
241 return totalram_pages() * zswap_max_pool_percent / 100 <
242 DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
245 static bool zswap_can_accept(void)
247 return totalram_pages() * zswap_accept_thr_percent / 100 *
248 zswap_max_pool_percent / 100 >
249 DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
252 static void zswap_update_total_size(void)
254 struct zswap_pool *pool;
259 list_for_each_entry_rcu(pool, &zswap_pools, list)
260 total += zpool_get_total_size(pool->zpool);
264 zswap_pool_total_size = total;
267 /*********************************
268 * zswap entry functions
269 **********************************/
270 static struct kmem_cache *zswap_entry_cache;
272 static int __init zswap_entry_cache_create(void)
274 zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
275 return zswap_entry_cache == NULL;
278 static void __init zswap_entry_cache_destroy(void)
280 kmem_cache_destroy(zswap_entry_cache);
283 static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp)
285 struct zswap_entry *entry;
286 entry = kmem_cache_alloc(zswap_entry_cache, gfp);
290 RB_CLEAR_NODE(&entry->rbnode);
294 static void zswap_entry_cache_free(struct zswap_entry *entry)
296 kmem_cache_free(zswap_entry_cache, entry);
299 /*********************************
301 **********************************/
302 static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset)
304 struct rb_node *node = root->rb_node;
305 struct zswap_entry *entry;
308 entry = rb_entry(node, struct zswap_entry, rbnode);
309 if (entry->offset > offset)
310 node = node->rb_left;
311 else if (entry->offset < offset)
312 node = node->rb_right;
320 * In the case that a entry with the same offset is found, a pointer to
321 * the existing entry is stored in dupentry and the function returns -EEXIST
323 static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry,
324 struct zswap_entry **dupentry)
326 struct rb_node **link = &root->rb_node, *parent = NULL;
327 struct zswap_entry *myentry;
331 myentry = rb_entry(parent, struct zswap_entry, rbnode);
332 if (myentry->offset > entry->offset)
333 link = &(*link)->rb_left;
334 else if (myentry->offset < entry->offset)
335 link = &(*link)->rb_right;
341 rb_link_node(&entry->rbnode, parent, link);
342 rb_insert_color(&entry->rbnode, root);
346 static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry)
348 if (!RB_EMPTY_NODE(&entry->rbnode)) {
349 rb_erase(&entry->rbnode, root);
350 RB_CLEAR_NODE(&entry->rbnode);
355 * Carries out the common pattern of freeing and entry's zpool allocation,
356 * freeing the entry itself, and decrementing the number of stored pages.
358 static void zswap_free_entry(struct zswap_entry *entry)
361 atomic_dec(&zswap_same_filled_pages);
363 zpool_free(entry->pool->zpool, entry->handle);
364 zswap_pool_put(entry->pool);
366 zswap_entry_cache_free(entry);
367 atomic_dec(&zswap_stored_pages);
368 zswap_update_total_size();
371 /* caller must hold the tree lock */
372 static void zswap_entry_get(struct zswap_entry *entry)
377 /* caller must hold the tree lock
378 * remove from the tree and free it, if nobody reference the entry
380 static void zswap_entry_put(struct zswap_tree *tree,
381 struct zswap_entry *entry)
383 int refcount = --entry->refcount;
385 BUG_ON(refcount < 0);
387 zswap_rb_erase(&tree->rbroot, entry);
388 zswap_free_entry(entry);
392 /* caller must hold the tree lock */
393 static struct zswap_entry *zswap_entry_find_get(struct rb_root *root,
396 struct zswap_entry *entry;
398 entry = zswap_rb_search(root, offset);
400 zswap_entry_get(entry);
405 /*********************************
407 **********************************/
408 static DEFINE_PER_CPU(u8 *, zswap_dstmem);
410 * If users dynamically change the zpool type and compressor at runtime, i.e.
411 * zswap is running, zswap can have more than one zpool on one cpu, but they
412 * are sharing dtsmem. So we need this mutex to be per-cpu.
414 static DEFINE_PER_CPU(struct mutex *, zswap_mutex);
416 static int zswap_dstmem_prepare(unsigned int cpu)
421 dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
425 mutex = kmalloc_node(sizeof(*mutex), GFP_KERNEL, cpu_to_node(cpu));
432 per_cpu(zswap_dstmem, cpu) = dst;
433 per_cpu(zswap_mutex, cpu) = mutex;
437 static int zswap_dstmem_dead(unsigned int cpu)
442 mutex = per_cpu(zswap_mutex, cpu);
444 per_cpu(zswap_mutex, cpu) = NULL;
446 dst = per_cpu(zswap_dstmem, cpu);
448 per_cpu(zswap_dstmem, cpu) = NULL;
453 static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
455 struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
456 struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu);
457 struct crypto_acomp *acomp;
458 struct acomp_req *req;
460 acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, cpu_to_node(cpu));
462 pr_err("could not alloc crypto acomp %s : %ld\n",
463 pool->tfm_name, PTR_ERR(acomp));
464 return PTR_ERR(acomp);
466 acomp_ctx->acomp = acomp;
468 req = acomp_request_alloc(acomp_ctx->acomp);
470 pr_err("could not alloc crypto acomp_request %s\n",
472 crypto_free_acomp(acomp_ctx->acomp);
475 acomp_ctx->req = req;
477 crypto_init_wait(&acomp_ctx->wait);
479 * if the backend of acomp is async zip, crypto_req_done() will wakeup
480 * crypto_wait_req(); if the backend of acomp is scomp, the callback
481 * won't be called, crypto_wait_req() will return without blocking.
483 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
484 crypto_req_done, &acomp_ctx->wait);
486 acomp_ctx->mutex = per_cpu(zswap_mutex, cpu);
487 acomp_ctx->dstmem = per_cpu(zswap_dstmem, cpu);
492 static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)
494 struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
495 struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu);
497 if (!IS_ERR_OR_NULL(acomp_ctx)) {
498 if (!IS_ERR_OR_NULL(acomp_ctx->req))
499 acomp_request_free(acomp_ctx->req);
500 if (!IS_ERR_OR_NULL(acomp_ctx->acomp))
501 crypto_free_acomp(acomp_ctx->acomp);
507 /*********************************
509 **********************************/
511 static struct zswap_pool *__zswap_pool_current(void)
513 struct zswap_pool *pool;
515 pool = list_first_or_null_rcu(&zswap_pools, typeof(*pool), list);
516 WARN_ONCE(!pool && zswap_has_pool,
517 "%s: no page storage pool!\n", __func__);
522 static struct zswap_pool *zswap_pool_current(void)
524 assert_spin_locked(&zswap_pools_lock);
526 return __zswap_pool_current();
529 static struct zswap_pool *zswap_pool_current_get(void)
531 struct zswap_pool *pool;
535 pool = __zswap_pool_current();
536 if (!zswap_pool_get(pool))
544 static struct zswap_pool *zswap_pool_last_get(void)
546 struct zswap_pool *pool, *last = NULL;
550 list_for_each_entry_rcu(pool, &zswap_pools, list)
552 WARN_ONCE(!last && zswap_has_pool,
553 "%s: no page storage pool!\n", __func__);
554 if (!zswap_pool_get(last))
562 /* type and compressor must be null-terminated */
563 static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor)
565 struct zswap_pool *pool;
567 assert_spin_locked(&zswap_pools_lock);
569 list_for_each_entry_rcu(pool, &zswap_pools, list) {
570 if (strcmp(pool->tfm_name, compressor))
572 if (strcmp(zpool_get_type(pool->zpool), type))
574 /* if we can't get it, it's about to be destroyed */
575 if (!zswap_pool_get(pool))
583 static void shrink_worker(struct work_struct *w)
585 struct zswap_pool *pool = container_of(w, typeof(*pool),
588 if (zpool_shrink(pool->zpool, 1, NULL))
589 zswap_reject_reclaim_fail++;
590 zswap_pool_put(pool);
593 static struct zswap_pool *zswap_pool_create(char *type, char *compressor)
595 struct zswap_pool *pool;
596 char name[38]; /* 'zswap' + 32 char (max) num + \0 */
597 gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;
600 if (!zswap_has_pool) {
601 /* if either are unset, pool initialization failed, and we
602 * need both params to be set correctly before trying to
605 if (!strcmp(type, ZSWAP_PARAM_UNSET))
607 if (!strcmp(compressor, ZSWAP_PARAM_UNSET))
611 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
615 /* unique name for each pool specifically required by zsmalloc */
616 snprintf(name, 38, "zswap%x", atomic_inc_return(&zswap_pools_count));
618 pool->zpool = zpool_create_pool(type, name, gfp, &zswap_zpool_ops);
620 pr_err("%s zpool not available\n", type);
623 pr_debug("using %s zpool\n", zpool_get_type(pool->zpool));
625 strscpy(pool->tfm_name, compressor, sizeof(pool->tfm_name));
627 pool->acomp_ctx = alloc_percpu(*pool->acomp_ctx);
628 if (!pool->acomp_ctx) {
629 pr_err("percpu alloc failed\n");
633 ret = cpuhp_state_add_instance(CPUHP_MM_ZSWP_POOL_PREPARE,
637 pr_debug("using %s compressor\n", pool->tfm_name);
639 /* being the current pool takes 1 ref; this func expects the
640 * caller to always add the new pool as the current pool
642 kref_init(&pool->kref);
643 INIT_LIST_HEAD(&pool->list);
644 INIT_WORK(&pool->shrink_work, shrink_worker);
646 zswap_pool_debug("created", pool);
652 free_percpu(pool->acomp_ctx);
654 zpool_destroy_pool(pool->zpool);
659 static __init struct zswap_pool *__zswap_pool_create_fallback(void)
661 bool has_comp, has_zpool;
663 has_comp = crypto_has_acomp(zswap_compressor, 0, 0);
664 if (!has_comp && strcmp(zswap_compressor,
665 CONFIG_ZSWAP_COMPRESSOR_DEFAULT)) {
666 pr_err("compressor %s not available, using default %s\n",
667 zswap_compressor, CONFIG_ZSWAP_COMPRESSOR_DEFAULT);
668 param_free_charp(&zswap_compressor);
669 zswap_compressor = CONFIG_ZSWAP_COMPRESSOR_DEFAULT;
670 has_comp = crypto_has_acomp(zswap_compressor, 0, 0);
673 pr_err("default compressor %s not available\n",
675 param_free_charp(&zswap_compressor);
676 zswap_compressor = ZSWAP_PARAM_UNSET;
679 has_zpool = zpool_has_pool(zswap_zpool_type);
680 if (!has_zpool && strcmp(zswap_zpool_type,
681 CONFIG_ZSWAP_ZPOOL_DEFAULT)) {
682 pr_err("zpool %s not available, using default %s\n",
683 zswap_zpool_type, CONFIG_ZSWAP_ZPOOL_DEFAULT);
684 param_free_charp(&zswap_zpool_type);
685 zswap_zpool_type = CONFIG_ZSWAP_ZPOOL_DEFAULT;
686 has_zpool = zpool_has_pool(zswap_zpool_type);
689 pr_err("default zpool %s not available\n",
691 param_free_charp(&zswap_zpool_type);
692 zswap_zpool_type = ZSWAP_PARAM_UNSET;
695 if (!has_comp || !has_zpool)
698 return zswap_pool_create(zswap_zpool_type, zswap_compressor);
701 static void zswap_pool_destroy(struct zswap_pool *pool)
703 zswap_pool_debug("destroying", pool);
705 cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node);
706 free_percpu(pool->acomp_ctx);
707 zpool_destroy_pool(pool->zpool);
711 static int __must_check zswap_pool_get(struct zswap_pool *pool)
716 return kref_get_unless_zero(&pool->kref);
719 static void __zswap_pool_release(struct work_struct *work)
721 struct zswap_pool *pool = container_of(work, typeof(*pool),
726 /* nobody should have been able to get a kref... */
727 WARN_ON(kref_get_unless_zero(&pool->kref));
729 /* pool is now off zswap_pools list and has no references. */
730 zswap_pool_destroy(pool);
733 static void __zswap_pool_empty(struct kref *kref)
735 struct zswap_pool *pool;
737 pool = container_of(kref, typeof(*pool), kref);
739 spin_lock(&zswap_pools_lock);
741 WARN_ON(pool == zswap_pool_current());
743 list_del_rcu(&pool->list);
745 INIT_WORK(&pool->release_work, __zswap_pool_release);
746 schedule_work(&pool->release_work);
748 spin_unlock(&zswap_pools_lock);
751 static void zswap_pool_put(struct zswap_pool *pool)
753 kref_put(&pool->kref, __zswap_pool_empty);
756 /*********************************
758 **********************************/
760 /* val must be a null-terminated string */
761 static int __zswap_param_set(const char *val, const struct kernel_param *kp,
762 char *type, char *compressor)
764 struct zswap_pool *pool, *put_pool = NULL;
765 char *s = strstrip((char *)val);
768 if (zswap_init_failed) {
769 pr_err("can't set param, initialization failed\n");
773 /* no change required */
774 if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool)
777 /* if this is load-time (pre-init) param setting,
778 * don't create a pool; that's done during init.
780 if (!zswap_init_started)
781 return param_set_charp(s, kp);
784 if (!zpool_has_pool(s)) {
785 pr_err("zpool %s not available\n", s);
789 } else if (!compressor) {
790 if (!crypto_has_acomp(s, 0, 0)) {
791 pr_err("compressor %s not available\n", s);
800 spin_lock(&zswap_pools_lock);
802 pool = zswap_pool_find_get(type, compressor);
804 zswap_pool_debug("using existing", pool);
805 WARN_ON(pool == zswap_pool_current());
806 list_del_rcu(&pool->list);
809 spin_unlock(&zswap_pools_lock);
812 pool = zswap_pool_create(type, compressor);
815 ret = param_set_charp(s, kp);
819 spin_lock(&zswap_pools_lock);
822 put_pool = zswap_pool_current();
823 list_add_rcu(&pool->list, &zswap_pools);
824 zswap_has_pool = true;
826 /* add the possibly pre-existing pool to the end of the pools
827 * list; if it's new (and empty) then it'll be removed and
828 * destroyed by the put after we drop the lock
830 list_add_tail_rcu(&pool->list, &zswap_pools);
834 spin_unlock(&zswap_pools_lock);
836 if (!zswap_has_pool && !pool) {
837 /* if initial pool creation failed, and this pool creation also
838 * failed, maybe both compressor and zpool params were bad.
839 * Allow changing this param, so pool creation will succeed
840 * when the other param is changed. We already verified this
841 * param is ok in the zpool_has_pool() or crypto_has_acomp()
844 ret = param_set_charp(s, kp);
847 /* drop the ref from either the old current pool,
848 * or the new pool we failed to add
851 zswap_pool_put(put_pool);
856 static int zswap_compressor_param_set(const char *val,
857 const struct kernel_param *kp)
859 return __zswap_param_set(val, kp, zswap_zpool_type, NULL);
862 static int zswap_zpool_param_set(const char *val,
863 const struct kernel_param *kp)
865 return __zswap_param_set(val, kp, NULL, zswap_compressor);
868 static int zswap_enabled_param_set(const char *val,
869 const struct kernel_param *kp)
871 if (zswap_init_failed) {
872 pr_err("can't enable, initialization failed\n");
875 if (!zswap_has_pool && zswap_init_started) {
876 pr_err("can't enable, no pool configured\n");
880 return param_set_bool(val, kp);
883 /*********************************
885 **********************************/
886 /* return enum for zswap_get_swap_cache_page */
887 enum zswap_get_swap_ret {
889 ZSWAP_SWAPCACHE_EXIST,
890 ZSWAP_SWAPCACHE_FAIL,
894 * zswap_get_swap_cache_page
896 * This is an adaption of read_swap_cache_async()
898 * This function tries to find a page with the given swap entry
899 * in the swapper_space address space (the swap cache). If the page
900 * is found, it is returned in retpage. Otherwise, a page is allocated,
901 * added to the swap cache, and returned in retpage.
903 * If success, the swap cache page is returned in retpage
904 * Returns ZSWAP_SWAPCACHE_EXIST if page was already in the swap cache
905 * Returns ZSWAP_SWAPCACHE_NEW if the new page needs to be populated,
906 * the new page is added to swapcache and locked
907 * Returns ZSWAP_SWAPCACHE_FAIL on error
909 static int zswap_get_swap_cache_page(swp_entry_t entry,
910 struct page **retpage)
912 bool page_was_allocated;
914 *retpage = __read_swap_cache_async(entry, GFP_KERNEL,
915 NULL, 0, &page_was_allocated);
916 if (page_was_allocated)
917 return ZSWAP_SWAPCACHE_NEW;
919 return ZSWAP_SWAPCACHE_FAIL;
920 return ZSWAP_SWAPCACHE_EXIST;
924 * Attempts to free an entry by adding a page to the swap cache,
925 * decompressing the entry data into the page, and issuing a
926 * bio write to write the page back to the swap device.
928 * This can be thought of as a "resumed writeback" of the page
929 * to the swap device. We are basically resuming the same swap
930 * writeback path that was intercepted with the frontswap_store()
931 * in the first place. After the page has been decompressed into
932 * the swap cache, the compressed version stored by zswap can be
935 static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
937 struct zswap_header *zhdr;
938 swp_entry_t swpentry;
939 struct zswap_tree *tree;
941 struct zswap_entry *entry;
943 struct scatterlist input, output;
944 struct crypto_acomp_ctx *acomp_ctx;
946 u8 *src, *tmp = NULL;
949 struct writeback_control wbc = {
950 .sync_mode = WB_SYNC_NONE,
953 if (!zpool_can_sleep_mapped(pool)) {
954 tmp = kmalloc(PAGE_SIZE, GFP_ATOMIC);
959 /* extract swpentry from data */
960 zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO);
961 swpentry = zhdr->swpentry; /* here */
962 tree = zswap_trees[swp_type(swpentry)];
963 offset = swp_offset(swpentry);
965 /* find and ref zswap entry */
966 spin_lock(&tree->lock);
967 entry = zswap_entry_find_get(&tree->rbroot, offset);
969 /* entry was invalidated */
970 spin_unlock(&tree->lock);
971 zpool_unmap_handle(pool, handle);
975 spin_unlock(&tree->lock);
976 BUG_ON(offset != entry->offset);
978 src = (u8 *)zhdr + sizeof(struct zswap_header);
979 if (!zpool_can_sleep_mapped(pool)) {
980 memcpy(tmp, src, entry->length);
982 zpool_unmap_handle(pool, handle);
985 /* try to allocate swap cache page */
986 switch (zswap_get_swap_cache_page(swpentry, &page)) {
987 case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */
991 case ZSWAP_SWAPCACHE_EXIST:
992 /* page is already in the swap cache, ignore for now */
997 case ZSWAP_SWAPCACHE_NEW: /* page is locked */
999 acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
1002 mutex_lock(acomp_ctx->mutex);
1003 sg_init_one(&input, src, entry->length);
1004 sg_init_table(&output, 1);
1005 sg_set_page(&output, page, PAGE_SIZE, 0);
1006 acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, dlen);
1007 ret = crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait);
1008 dlen = acomp_ctx->req->dlen;
1009 mutex_unlock(acomp_ctx->mutex);
1012 BUG_ON(dlen != PAGE_SIZE);
1014 /* page is up to date */
1015 SetPageUptodate(page);
1018 /* move it to the tail of the inactive list after end_writeback */
1019 SetPageReclaim(page);
1021 /* start writeback */
1022 __swap_writepage(page, &wbc, end_swap_bio_write);
1024 zswap_written_back_pages++;
1026 spin_lock(&tree->lock);
1027 /* drop local reference */
1028 zswap_entry_put(tree, entry);
1031 * There are two possible situations for entry here:
1032 * (1) refcount is 1(normal case), entry is valid and on the tree
1033 * (2) refcount is 0, entry is freed and not on the tree
1034 * because invalidate happened during writeback
1035 * search the tree and free the entry if find entry
1037 if (entry == zswap_rb_search(&tree->rbroot, offset))
1038 zswap_entry_put(tree, entry);
1039 spin_unlock(&tree->lock);
1044 * if we get here due to ZSWAP_SWAPCACHE_EXIST
1045 * a load may be happening concurrently.
1046 * it is safe and okay to not free the entry.
1047 * if we free the entry in the following put
1048 * it is also okay to return !0
1051 spin_lock(&tree->lock);
1052 zswap_entry_put(tree, entry);
1053 spin_unlock(&tree->lock);
1056 if (zpool_can_sleep_mapped(pool))
1057 zpool_unmap_handle(pool, handle);
1064 static int zswap_is_page_same_filled(void *ptr, unsigned long *value)
1067 unsigned long *page;
1069 page = (unsigned long *)ptr;
1070 for (pos = 1; pos < PAGE_SIZE / sizeof(*page); pos++) {
1071 if (page[pos] != page[0])
1078 static void zswap_fill_page(void *ptr, unsigned long value)
1080 unsigned long *page;
1082 page = (unsigned long *)ptr;
1083 memset_l(page, value, PAGE_SIZE / sizeof(unsigned long));
1086 /*********************************
1088 **********************************/
1089 /* attempts to compress and store an single page */
1090 static int zswap_frontswap_store(unsigned type, pgoff_t offset,
1093 struct zswap_tree *tree = zswap_trees[type];
1094 struct zswap_entry *entry, *dupentry;
1095 struct scatterlist input, output;
1096 struct crypto_acomp_ctx *acomp_ctx;
1098 unsigned int hlen, dlen = PAGE_SIZE;
1099 unsigned long handle, value;
1102 struct zswap_header zhdr = { .swpentry = swp_entry(type, offset) };
1105 /* THP isn't supported */
1106 if (PageTransHuge(page)) {
1111 if (!zswap_enabled || !tree) {
1116 /* reclaim space if needed */
1117 if (zswap_is_full()) {
1118 struct zswap_pool *pool;
1120 zswap_pool_limit_hit++;
1121 zswap_pool_reached_full = true;
1122 pool = zswap_pool_last_get();
1124 queue_work(shrink_wq, &pool->shrink_work);
1129 if (zswap_pool_reached_full) {
1130 if (!zswap_can_accept()) {
1134 zswap_pool_reached_full = false;
1137 /* allocate entry */
1138 entry = zswap_entry_cache_alloc(GFP_KERNEL);
1140 zswap_reject_kmemcache_fail++;
1145 if (zswap_same_filled_pages_enabled) {
1146 src = kmap_atomic(page);
1147 if (zswap_is_page_same_filled(src, &value)) {
1149 entry->offset = offset;
1151 entry->value = value;
1152 atomic_inc(&zswap_same_filled_pages);
1158 if (!zswap_non_same_filled_pages_enabled) {
1163 /* if entry is successfully added, it keeps the reference */
1164 entry->pool = zswap_pool_current_get();
1171 acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
1173 mutex_lock(acomp_ctx->mutex);
1175 dst = acomp_ctx->dstmem;
1176 sg_init_table(&input, 1);
1177 sg_set_page(&input, page, PAGE_SIZE, 0);
1179 /* zswap_dstmem is of size (PAGE_SIZE * 2). Reflect same in sg_list */
1180 sg_init_one(&output, dst, PAGE_SIZE * 2);
1181 acomp_request_set_params(acomp_ctx->req, &input, &output, PAGE_SIZE, dlen);
1183 * it maybe looks a little bit silly that we send an asynchronous request,
1184 * then wait for its completion synchronously. This makes the process look
1185 * synchronous in fact.
1186 * Theoretically, acomp supports users send multiple acomp requests in one
1187 * acomp instance, then get those requests done simultaneously. but in this
1188 * case, frontswap actually does store and load page by page, there is no
1189 * existing method to send the second page before the first page is done
1190 * in one thread doing frontswap.
1191 * but in different threads running on different cpu, we have different
1192 * acomp instance, so multiple threads can do (de)compression in parallel.
1194 ret = crypto_wait_req(crypto_acomp_compress(acomp_ctx->req), &acomp_ctx->wait);
1195 dlen = acomp_ctx->req->dlen;
1203 hlen = zpool_evictable(entry->pool->zpool) ? sizeof(zhdr) : 0;
1204 gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;
1205 if (zpool_malloc_support_movable(entry->pool->zpool))
1206 gfp |= __GFP_HIGHMEM | __GFP_MOVABLE;
1207 ret = zpool_malloc(entry->pool->zpool, hlen + dlen, gfp, &handle);
1208 if (ret == -ENOSPC) {
1209 zswap_reject_compress_poor++;
1213 zswap_reject_alloc_fail++;
1216 buf = zpool_map_handle(entry->pool->zpool, handle, ZPOOL_MM_WO);
1217 memcpy(buf, &zhdr, hlen);
1218 memcpy(buf + hlen, dst, dlen);
1219 zpool_unmap_handle(entry->pool->zpool, handle);
1220 mutex_unlock(acomp_ctx->mutex);
1222 /* populate entry */
1223 entry->offset = offset;
1224 entry->handle = handle;
1225 entry->length = dlen;
1229 spin_lock(&tree->lock);
1231 ret = zswap_rb_insert(&tree->rbroot, entry, &dupentry);
1232 if (ret == -EEXIST) {
1233 zswap_duplicate_entry++;
1234 /* remove from rbtree */
1235 zswap_rb_erase(&tree->rbroot, dupentry);
1236 zswap_entry_put(tree, dupentry);
1238 } while (ret == -EEXIST);
1239 spin_unlock(&tree->lock);
1242 atomic_inc(&zswap_stored_pages);
1243 zswap_update_total_size();
1248 mutex_unlock(acomp_ctx->mutex);
1249 zswap_pool_put(entry->pool);
1251 zswap_entry_cache_free(entry);
1257 * returns 0 if the page was successfully decompressed
1258 * return -1 on entry not found or error
1260 static int zswap_frontswap_load(unsigned type, pgoff_t offset,
1263 struct zswap_tree *tree = zswap_trees[type];
1264 struct zswap_entry *entry;
1265 struct scatterlist input, output;
1266 struct crypto_acomp_ctx *acomp_ctx;
1267 u8 *src, *dst, *tmp;
1272 spin_lock(&tree->lock);
1273 entry = zswap_entry_find_get(&tree->rbroot, offset);
1275 /* entry was written back */
1276 spin_unlock(&tree->lock);
1279 spin_unlock(&tree->lock);
1281 if (!entry->length) {
1282 dst = kmap_atomic(page);
1283 zswap_fill_page(dst, entry->value);
1289 if (!zpool_can_sleep_mapped(entry->pool->zpool)) {
1291 tmp = kmalloc(entry->length, GFP_ATOMIC);
1300 src = zpool_map_handle(entry->pool->zpool, entry->handle, ZPOOL_MM_RO);
1301 if (zpool_evictable(entry->pool->zpool))
1302 src += sizeof(struct zswap_header);
1304 if (!zpool_can_sleep_mapped(entry->pool->zpool)) {
1306 memcpy(tmp, src, entry->length);
1309 zpool_unmap_handle(entry->pool->zpool, entry->handle);
1312 acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
1313 mutex_lock(acomp_ctx->mutex);
1314 sg_init_one(&input, src, entry->length);
1315 sg_init_table(&output, 1);
1316 sg_set_page(&output, page, PAGE_SIZE, 0);
1317 acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, dlen);
1318 ret = crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait);
1319 mutex_unlock(acomp_ctx->mutex);
1321 if (zpool_can_sleep_mapped(entry->pool->zpool))
1322 zpool_unmap_handle(entry->pool->zpool, entry->handle);
1329 spin_lock(&tree->lock);
1330 zswap_entry_put(tree, entry);
1331 spin_unlock(&tree->lock);
1336 /* frees an entry in zswap */
1337 static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset)
1339 struct zswap_tree *tree = zswap_trees[type];
1340 struct zswap_entry *entry;
1343 spin_lock(&tree->lock);
1344 entry = zswap_rb_search(&tree->rbroot, offset);
1346 /* entry was written back */
1347 spin_unlock(&tree->lock);
1351 /* remove from rbtree */
1352 zswap_rb_erase(&tree->rbroot, entry);
1354 /* drop the initial reference from entry creation */
1355 zswap_entry_put(tree, entry);
1357 spin_unlock(&tree->lock);
1360 /* frees all zswap entries for the given swap type */
1361 static void zswap_frontswap_invalidate_area(unsigned type)
1363 struct zswap_tree *tree = zswap_trees[type];
1364 struct zswap_entry *entry, *n;
1369 /* walk the tree and free everything */
1370 spin_lock(&tree->lock);
1371 rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode)
1372 zswap_free_entry(entry);
1373 tree->rbroot = RB_ROOT;
1374 spin_unlock(&tree->lock);
1376 zswap_trees[type] = NULL;
1379 static void zswap_frontswap_init(unsigned type)
1381 struct zswap_tree *tree;
1383 tree = kzalloc(sizeof(*tree), GFP_KERNEL);
1385 pr_err("alloc failed, zswap disabled for swap type %d\n", type);
1389 tree->rbroot = RB_ROOT;
1390 spin_lock_init(&tree->lock);
1391 zswap_trees[type] = tree;
1394 static const struct frontswap_ops zswap_frontswap_ops = {
1395 .store = zswap_frontswap_store,
1396 .load = zswap_frontswap_load,
1397 .invalidate_page = zswap_frontswap_invalidate_page,
1398 .invalidate_area = zswap_frontswap_invalidate_area,
1399 .init = zswap_frontswap_init
1402 /*********************************
1404 **********************************/
1405 #ifdef CONFIG_DEBUG_FS
1406 #include <linux/debugfs.h>
1408 static struct dentry *zswap_debugfs_root;
1410 static int __init zswap_debugfs_init(void)
1412 if (!debugfs_initialized())
1415 zswap_debugfs_root = debugfs_create_dir("zswap", NULL);
1417 debugfs_create_u64("pool_limit_hit", 0444,
1418 zswap_debugfs_root, &zswap_pool_limit_hit);
1419 debugfs_create_u64("reject_reclaim_fail", 0444,
1420 zswap_debugfs_root, &zswap_reject_reclaim_fail);
1421 debugfs_create_u64("reject_alloc_fail", 0444,
1422 zswap_debugfs_root, &zswap_reject_alloc_fail);
1423 debugfs_create_u64("reject_kmemcache_fail", 0444,
1424 zswap_debugfs_root, &zswap_reject_kmemcache_fail);
1425 debugfs_create_u64("reject_compress_poor", 0444,
1426 zswap_debugfs_root, &zswap_reject_compress_poor);
1427 debugfs_create_u64("written_back_pages", 0444,
1428 zswap_debugfs_root, &zswap_written_back_pages);
1429 debugfs_create_u64("duplicate_entry", 0444,
1430 zswap_debugfs_root, &zswap_duplicate_entry);
1431 debugfs_create_u64("pool_total_size", 0444,
1432 zswap_debugfs_root, &zswap_pool_total_size);
1433 debugfs_create_atomic_t("stored_pages", 0444,
1434 zswap_debugfs_root, &zswap_stored_pages);
1435 debugfs_create_atomic_t("same_filled_pages", 0444,
1436 zswap_debugfs_root, &zswap_same_filled_pages);
1441 static int __init zswap_debugfs_init(void)
1447 /*********************************
1448 * module init and exit
1449 **********************************/
1450 static int __init init_zswap(void)
1452 struct zswap_pool *pool;
1455 zswap_init_started = true;
1457 if (zswap_entry_cache_create()) {
1458 pr_err("entry cache creation failed\n");
1462 ret = cpuhp_setup_state(CPUHP_MM_ZSWP_MEM_PREPARE, "mm/zswap:prepare",
1463 zswap_dstmem_prepare, zswap_dstmem_dead);
1465 pr_err("dstmem alloc failed\n");
1469 ret = cpuhp_setup_state_multi(CPUHP_MM_ZSWP_POOL_PREPARE,
1470 "mm/zswap_pool:prepare",
1471 zswap_cpu_comp_prepare,
1472 zswap_cpu_comp_dead);
1476 pool = __zswap_pool_create_fallback();
1478 pr_info("loaded using pool %s/%s\n", pool->tfm_name,
1479 zpool_get_type(pool->zpool));
1480 list_add(&pool->list, &zswap_pools);
1481 zswap_has_pool = true;
1483 pr_err("pool creation failed\n");
1484 zswap_enabled = false;
1487 shrink_wq = create_workqueue("zswap-shrink");
1491 ret = frontswap_register_ops(&zswap_frontswap_ops);
1494 if (zswap_debugfs_init())
1495 pr_warn("debugfs initialization failed\n");
1499 destroy_workqueue(shrink_wq);
1502 zswap_pool_destroy(pool);
1504 cpuhp_remove_state(CPUHP_MM_ZSWP_MEM_PREPARE);
1506 zswap_entry_cache_destroy();
1508 /* if built-in, we aren't unloaded on failure; don't allow use */
1509 zswap_init_failed = true;
1510 zswap_enabled = false;
1513 /* must be late so crypto has time to come up */
1514 late_initcall(init_zswap);
1516 MODULE_LICENSE("GPL");
1517 MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
1518 MODULE_DESCRIPTION("Compressed cache for swap pages");