1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * zswap.c - zswap driver file
5 * zswap is a cache 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.
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/rbtree.h>
24 #include <linux/swap.h>
25 #include <linux/crypto.h>
26 #include <linux/scatterlist.h>
27 #include <linux/mempolicy.h>
28 #include <linux/mempool.h>
29 #include <linux/zpool.h>
30 #include <crypto/acompress.h>
31 #include <linux/zswap.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>
42 /*********************************
44 **********************************/
45 /* Total bytes used by the compressed storage */
46 u64 zswap_pool_total_size;
47 /* The number of compressed pages currently stored in zswap */
48 atomic_t zswap_stored_pages = ATOMIC_INIT(0);
49 /* The number of same-value filled pages currently stored in zswap */
50 static atomic_t zswap_same_filled_pages = ATOMIC_INIT(0);
53 * The statistics below are not protected from concurrent access for
54 * performance reasons so they may not be a 100% accurate. However,
55 * they do provide useful information on roughly how many times a
56 * certain event is occurring.
59 /* Pool limit was hit (see zswap_max_pool_percent) */
60 static u64 zswap_pool_limit_hit;
61 /* Pages written back when pool limit was reached */
62 static u64 zswap_written_back_pages;
63 /* Store failed due to a reclaim failure after pool limit was reached */
64 static u64 zswap_reject_reclaim_fail;
65 /* Store failed due to compression algorithm failure */
66 static u64 zswap_reject_compress_fail;
67 /* Compressed page was too big for the allocator to (optimally) store */
68 static u64 zswap_reject_compress_poor;
69 /* Store failed because underlying allocator could not get memory */
70 static u64 zswap_reject_alloc_fail;
71 /* Store failed because the entry metadata could not be allocated (rare) */
72 static u64 zswap_reject_kmemcache_fail;
73 /* Duplicate store was encountered (rare) */
74 static u64 zswap_duplicate_entry;
76 /* Shrinker work queue */
77 static struct workqueue_struct *shrink_wq;
78 /* Pool limit was hit, we need to calm down */
79 static bool zswap_pool_reached_full;
81 /*********************************
83 **********************************/
85 #define ZSWAP_PARAM_UNSET ""
87 static int zswap_setup(void);
89 /* Enable/disable zswap */
90 static bool zswap_enabled = IS_ENABLED(CONFIG_ZSWAP_DEFAULT_ON);
91 static int zswap_enabled_param_set(const char *,
92 const struct kernel_param *);
93 static const struct kernel_param_ops zswap_enabled_param_ops = {
94 .set = zswap_enabled_param_set,
95 .get = param_get_bool,
97 module_param_cb(enabled, &zswap_enabled_param_ops, &zswap_enabled, 0644);
99 /* Crypto compressor to use */
100 static char *zswap_compressor = CONFIG_ZSWAP_COMPRESSOR_DEFAULT;
101 static int zswap_compressor_param_set(const char *,
102 const struct kernel_param *);
103 static const struct kernel_param_ops zswap_compressor_param_ops = {
104 .set = zswap_compressor_param_set,
105 .get = param_get_charp,
106 .free = param_free_charp,
108 module_param_cb(compressor, &zswap_compressor_param_ops,
109 &zswap_compressor, 0644);
111 /* Compressed storage zpool to use */
112 static char *zswap_zpool_type = CONFIG_ZSWAP_ZPOOL_DEFAULT;
113 static int zswap_zpool_param_set(const char *, const struct kernel_param *);
114 static const struct kernel_param_ops zswap_zpool_param_ops = {
115 .set = zswap_zpool_param_set,
116 .get = param_get_charp,
117 .free = param_free_charp,
119 module_param_cb(zpool, &zswap_zpool_param_ops, &zswap_zpool_type, 0644);
121 /* The maximum percentage of memory that the compressed pool can occupy */
122 static unsigned int zswap_max_pool_percent = 20;
123 module_param_named(max_pool_percent, zswap_max_pool_percent, uint, 0644);
125 /* The threshold for accepting new pages after the max_pool_percent was hit */
126 static unsigned int zswap_accept_thr_percent = 90; /* of max pool size */
127 module_param_named(accept_threshold_percent, zswap_accept_thr_percent,
131 * Enable/disable handling same-value filled pages (enabled by default).
132 * If disabled every page is considered non-same-value filled.
134 static bool zswap_same_filled_pages_enabled = true;
135 module_param_named(same_filled_pages_enabled, zswap_same_filled_pages_enabled,
138 /* Enable/disable handling non-same-value filled pages (enabled by default) */
139 static bool zswap_non_same_filled_pages_enabled = true;
140 module_param_named(non_same_filled_pages_enabled, zswap_non_same_filled_pages_enabled,
143 static bool zswap_exclusive_loads_enabled = IS_ENABLED(
144 CONFIG_ZSWAP_EXCLUSIVE_LOADS_DEFAULT_ON);
145 module_param_named(exclusive_loads, zswap_exclusive_loads_enabled, bool, 0644);
147 /* Number of zpools in zswap_pool (empirically determined for scalability) */
148 #define ZSWAP_NR_ZPOOLS 32
150 /*********************************
152 **********************************/
154 struct crypto_acomp_ctx {
155 struct crypto_acomp *acomp;
156 struct acomp_req *req;
157 struct crypto_wait wait;
163 * The lock ordering is zswap_tree.lock -> zswap_pool.lru_lock.
164 * The only case where lru_lock is not acquired while holding tree.lock is
165 * when a zswap_entry is taken off the lru for writeback, in that case it
166 * needs to be verified that it's still valid in the tree.
169 struct zpool *zpools[ZSWAP_NR_ZPOOLS];
170 struct crypto_acomp_ctx __percpu *acomp_ctx;
172 struct list_head list;
173 struct work_struct release_work;
174 struct work_struct shrink_work;
175 struct hlist_node node;
176 char tfm_name[CRYPTO_MAX_ALG_NAME];
177 struct list_head lru;
184 * This structure contains the metadata for tracking a single compressed
187 * rbnode - links the entry into red-black tree for the appropriate swap type
188 * swpentry - associated swap entry, the offset indexes into the red-black tree
189 * refcount - the number of outstanding reference to the entry. This is needed
190 * to protect against premature freeing of the entry by code
191 * concurrent calls to load, invalidate, and writeback. The lock
192 * for the zswap_tree structure that contains the entry must
193 * be held while changing the refcount. Since the lock must
194 * be held, there is no reason to also make refcount atomic.
195 * length - the length in bytes of the compressed page data. Needed during
196 * decompression. For a same value filled page length is 0, and both
197 * pool and lru are invalid and must be ignored.
198 * pool - the zswap_pool the entry's data is in
199 * handle - zpool allocation handle that stores the compressed page data
200 * value - value of the same-value filled pages which have same content
201 * objcg - the obj_cgroup that the compressed memory is charged to
202 * lru - handle to the pool's lru used to evict pages.
205 struct rb_node rbnode;
206 swp_entry_t swpentry;
209 struct zswap_pool *pool;
211 unsigned long handle;
214 struct obj_cgroup *objcg;
215 struct list_head lru;
219 * The tree lock in the zswap_tree struct protects a few things:
221 * - the refcount field of each entry in the tree
224 struct rb_root rbroot;
228 static struct zswap_tree *zswap_trees[MAX_SWAPFILES];
230 /* RCU-protected iteration */
231 static LIST_HEAD(zswap_pools);
232 /* protects zswap_pools list modification */
233 static DEFINE_SPINLOCK(zswap_pools_lock);
234 /* pool counter to provide unique names to zpool */
235 static atomic_t zswap_pools_count = ATOMIC_INIT(0);
237 enum zswap_init_type {
243 static enum zswap_init_type zswap_init_state;
245 /* used to ensure the integrity of initialization */
246 static DEFINE_MUTEX(zswap_init_lock);
248 /* init completed, but couldn't create the initial pool */
249 static bool zswap_has_pool;
251 /*********************************
252 * helpers and fwd declarations
253 **********************************/
255 #define zswap_pool_debug(msg, p) \
256 pr_debug("%s pool %s/%s\n", msg, (p)->tfm_name, \
257 zpool_get_type((p)->zpools[0]))
259 static int zswap_writeback_entry(struct zswap_entry *entry,
260 struct zswap_tree *tree);
261 static int zswap_pool_get(struct zswap_pool *pool);
262 static void zswap_pool_put(struct zswap_pool *pool);
264 static bool zswap_is_full(void)
266 return totalram_pages() * zswap_max_pool_percent / 100 <
267 DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
270 static bool zswap_can_accept(void)
272 return totalram_pages() * zswap_accept_thr_percent / 100 *
273 zswap_max_pool_percent / 100 >
274 DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
277 static void zswap_update_total_size(void)
279 struct zswap_pool *pool;
285 list_for_each_entry_rcu(pool, &zswap_pools, list)
286 for (i = 0; i < ZSWAP_NR_ZPOOLS; i++)
287 total += zpool_get_total_size(pool->zpools[i]);
291 zswap_pool_total_size = total;
294 /*********************************
295 * zswap entry functions
296 **********************************/
297 static struct kmem_cache *zswap_entry_cache;
299 static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp)
301 struct zswap_entry *entry;
302 entry = kmem_cache_alloc(zswap_entry_cache, gfp);
306 RB_CLEAR_NODE(&entry->rbnode);
310 static void zswap_entry_cache_free(struct zswap_entry *entry)
312 kmem_cache_free(zswap_entry_cache, entry);
315 /*********************************
317 **********************************/
318 static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset)
320 struct rb_node *node = root->rb_node;
321 struct zswap_entry *entry;
322 pgoff_t entry_offset;
325 entry = rb_entry(node, struct zswap_entry, rbnode);
326 entry_offset = swp_offset(entry->swpentry);
327 if (entry_offset > offset)
328 node = node->rb_left;
329 else if (entry_offset < offset)
330 node = node->rb_right;
338 * In the case that a entry with the same offset is found, a pointer to
339 * the existing entry is stored in dupentry and the function returns -EEXIST
341 static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry,
342 struct zswap_entry **dupentry)
344 struct rb_node **link = &root->rb_node, *parent = NULL;
345 struct zswap_entry *myentry;
346 pgoff_t myentry_offset, entry_offset = swp_offset(entry->swpentry);
350 myentry = rb_entry(parent, struct zswap_entry, rbnode);
351 myentry_offset = swp_offset(myentry->swpentry);
352 if (myentry_offset > entry_offset)
353 link = &(*link)->rb_left;
354 else if (myentry_offset < entry_offset)
355 link = &(*link)->rb_right;
361 rb_link_node(&entry->rbnode, parent, link);
362 rb_insert_color(&entry->rbnode, root);
366 static bool zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry)
368 if (!RB_EMPTY_NODE(&entry->rbnode)) {
369 rb_erase(&entry->rbnode, root);
370 RB_CLEAR_NODE(&entry->rbnode);
376 static struct zpool *zswap_find_zpool(struct zswap_entry *entry)
380 if (ZSWAP_NR_ZPOOLS > 1)
381 i = hash_ptr(entry, ilog2(ZSWAP_NR_ZPOOLS));
383 return entry->pool->zpools[i];
387 * Carries out the common pattern of freeing and entry's zpool allocation,
388 * freeing the entry itself, and decrementing the number of stored pages.
390 static void zswap_free_entry(struct zswap_entry *entry)
393 obj_cgroup_uncharge_zswap(entry->objcg, entry->length);
394 obj_cgroup_put(entry->objcg);
397 atomic_dec(&zswap_same_filled_pages);
399 spin_lock(&entry->pool->lru_lock);
400 list_del(&entry->lru);
401 spin_unlock(&entry->pool->lru_lock);
402 zpool_free(zswap_find_zpool(entry), entry->handle);
403 zswap_pool_put(entry->pool);
405 zswap_entry_cache_free(entry);
406 atomic_dec(&zswap_stored_pages);
407 zswap_update_total_size();
410 /* caller must hold the tree lock */
411 static void zswap_entry_get(struct zswap_entry *entry)
416 /* caller must hold the tree lock
417 * remove from the tree and free it, if nobody reference the entry
419 static void zswap_entry_put(struct zswap_tree *tree,
420 struct zswap_entry *entry)
422 int refcount = --entry->refcount;
424 WARN_ON_ONCE(refcount < 0);
426 WARN_ON_ONCE(!RB_EMPTY_NODE(&entry->rbnode));
427 zswap_free_entry(entry);
431 /* caller must hold the tree lock */
432 static struct zswap_entry *zswap_entry_find_get(struct rb_root *root,
435 struct zswap_entry *entry;
437 entry = zswap_rb_search(root, offset);
439 zswap_entry_get(entry);
444 /*********************************
446 **********************************/
447 static DEFINE_PER_CPU(u8 *, zswap_dstmem);
449 * If users dynamically change the zpool type and compressor at runtime, i.e.
450 * zswap is running, zswap can have more than one zpool on one cpu, but they
451 * are sharing dtsmem. So we need this mutex to be per-cpu.
453 static DEFINE_PER_CPU(struct mutex *, zswap_mutex);
455 static int zswap_dstmem_prepare(unsigned int cpu)
460 dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
464 mutex = kmalloc_node(sizeof(*mutex), GFP_KERNEL, cpu_to_node(cpu));
471 per_cpu(zswap_dstmem, cpu) = dst;
472 per_cpu(zswap_mutex, cpu) = mutex;
476 static int zswap_dstmem_dead(unsigned int cpu)
481 mutex = per_cpu(zswap_mutex, cpu);
483 per_cpu(zswap_mutex, cpu) = NULL;
485 dst = per_cpu(zswap_dstmem, cpu);
487 per_cpu(zswap_dstmem, cpu) = NULL;
492 static int zswap_cpu_comp_prepare(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);
496 struct crypto_acomp *acomp;
497 struct acomp_req *req;
499 acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, cpu_to_node(cpu));
501 pr_err("could not alloc crypto acomp %s : %ld\n",
502 pool->tfm_name, PTR_ERR(acomp));
503 return PTR_ERR(acomp);
505 acomp_ctx->acomp = acomp;
507 req = acomp_request_alloc(acomp_ctx->acomp);
509 pr_err("could not alloc crypto acomp_request %s\n",
511 crypto_free_acomp(acomp_ctx->acomp);
514 acomp_ctx->req = req;
516 crypto_init_wait(&acomp_ctx->wait);
518 * if the backend of acomp is async zip, crypto_req_done() will wakeup
519 * crypto_wait_req(); if the backend of acomp is scomp, the callback
520 * won't be called, crypto_wait_req() will return without blocking.
522 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
523 crypto_req_done, &acomp_ctx->wait);
525 acomp_ctx->mutex = per_cpu(zswap_mutex, cpu);
526 acomp_ctx->dstmem = per_cpu(zswap_dstmem, cpu);
531 static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)
533 struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
534 struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu);
536 if (!IS_ERR_OR_NULL(acomp_ctx)) {
537 if (!IS_ERR_OR_NULL(acomp_ctx->req))
538 acomp_request_free(acomp_ctx->req);
539 if (!IS_ERR_OR_NULL(acomp_ctx->acomp))
540 crypto_free_acomp(acomp_ctx->acomp);
546 /*********************************
548 **********************************/
550 static struct zswap_pool *__zswap_pool_current(void)
552 struct zswap_pool *pool;
554 pool = list_first_or_null_rcu(&zswap_pools, typeof(*pool), list);
555 WARN_ONCE(!pool && zswap_has_pool,
556 "%s: no page storage pool!\n", __func__);
561 static struct zswap_pool *zswap_pool_current(void)
563 assert_spin_locked(&zswap_pools_lock);
565 return __zswap_pool_current();
568 static struct zswap_pool *zswap_pool_current_get(void)
570 struct zswap_pool *pool;
574 pool = __zswap_pool_current();
575 if (!zswap_pool_get(pool))
583 static struct zswap_pool *zswap_pool_last_get(void)
585 struct zswap_pool *pool, *last = NULL;
589 list_for_each_entry_rcu(pool, &zswap_pools, list)
591 WARN_ONCE(!last && zswap_has_pool,
592 "%s: no page storage pool!\n", __func__);
593 if (!zswap_pool_get(last))
601 /* type and compressor must be null-terminated */
602 static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor)
604 struct zswap_pool *pool;
606 assert_spin_locked(&zswap_pools_lock);
608 list_for_each_entry_rcu(pool, &zswap_pools, list) {
609 if (strcmp(pool->tfm_name, compressor))
611 /* all zpools share the same type */
612 if (strcmp(zpool_get_type(pool->zpools[0]), type))
614 /* if we can't get it, it's about to be destroyed */
615 if (!zswap_pool_get(pool))
624 * If the entry is still valid in the tree, drop the initial ref and remove it
625 * from the tree. This function must be called with an additional ref held,
626 * otherwise it may race with another invalidation freeing the entry.
628 static void zswap_invalidate_entry(struct zswap_tree *tree,
629 struct zswap_entry *entry)
631 if (zswap_rb_erase(&tree->rbroot, entry))
632 zswap_entry_put(tree, entry);
635 static int zswap_reclaim_entry(struct zswap_pool *pool)
637 struct zswap_entry *entry;
638 struct zswap_tree *tree;
642 /* Get an entry off the LRU */
643 spin_lock(&pool->lru_lock);
644 if (list_empty(&pool->lru)) {
645 spin_unlock(&pool->lru_lock);
648 entry = list_last_entry(&pool->lru, struct zswap_entry, lru);
649 list_del_init(&entry->lru);
651 * Once the lru lock is dropped, the entry might get freed. The
652 * swpoffset is copied to the stack, and entry isn't deref'd again
653 * until the entry is verified to still be alive in the tree.
655 swpoffset = swp_offset(entry->swpentry);
656 tree = zswap_trees[swp_type(entry->swpentry)];
657 spin_unlock(&pool->lru_lock);
659 /* Check for invalidate() race */
660 spin_lock(&tree->lock);
661 if (entry != zswap_rb_search(&tree->rbroot, swpoffset)) {
665 /* Hold a reference to prevent a free during writeback */
666 zswap_entry_get(entry);
667 spin_unlock(&tree->lock);
669 ret = zswap_writeback_entry(entry, tree);
671 spin_lock(&tree->lock);
673 /* Writeback failed, put entry back on LRU */
674 spin_lock(&pool->lru_lock);
675 list_move(&entry->lru, &pool->lru);
676 spin_unlock(&pool->lru_lock);
681 * Writeback started successfully, the page now belongs to the
682 * swapcache. Drop the entry from zswap - unless invalidate already
683 * took it out while we had the tree->lock released for IO.
685 zswap_invalidate_entry(tree, entry);
688 /* Drop local reference */
689 zswap_entry_put(tree, entry);
691 spin_unlock(&tree->lock);
692 return ret ? -EAGAIN : 0;
695 static void shrink_worker(struct work_struct *w)
697 struct zswap_pool *pool = container_of(w, typeof(*pool),
699 int ret, failures = 0;
702 ret = zswap_reclaim_entry(pool);
704 zswap_reject_reclaim_fail++;
707 if (++failures == MAX_RECLAIM_RETRIES)
711 } while (!zswap_can_accept());
712 zswap_pool_put(pool);
715 static struct zswap_pool *zswap_pool_create(char *type, char *compressor)
718 struct zswap_pool *pool;
719 char name[38]; /* 'zswap' + 32 char (max) num + \0 */
720 gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;
723 if (!zswap_has_pool) {
724 /* if either are unset, pool initialization failed, and we
725 * need both params to be set correctly before trying to
728 if (!strcmp(type, ZSWAP_PARAM_UNSET))
730 if (!strcmp(compressor, ZSWAP_PARAM_UNSET))
734 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
738 for (i = 0; i < ZSWAP_NR_ZPOOLS; i++) {
739 /* unique name for each pool specifically required by zsmalloc */
740 snprintf(name, 38, "zswap%x",
741 atomic_inc_return(&zswap_pools_count));
743 pool->zpools[i] = zpool_create_pool(type, name, gfp);
744 if (!pool->zpools[i]) {
745 pr_err("%s zpool not available\n", type);
749 pr_debug("using %s zpool\n", zpool_get_type(pool->zpools[0]));
751 strscpy(pool->tfm_name, compressor, sizeof(pool->tfm_name));
753 pool->acomp_ctx = alloc_percpu(*pool->acomp_ctx);
754 if (!pool->acomp_ctx) {
755 pr_err("percpu alloc failed\n");
759 ret = cpuhp_state_add_instance(CPUHP_MM_ZSWP_POOL_PREPARE,
763 pr_debug("using %s compressor\n", pool->tfm_name);
765 /* being the current pool takes 1 ref; this func expects the
766 * caller to always add the new pool as the current pool
768 kref_init(&pool->kref);
769 INIT_LIST_HEAD(&pool->list);
770 INIT_LIST_HEAD(&pool->lru);
771 spin_lock_init(&pool->lru_lock);
772 INIT_WORK(&pool->shrink_work, shrink_worker);
774 zswap_pool_debug("created", pool);
780 free_percpu(pool->acomp_ctx);
782 zpool_destroy_pool(pool->zpools[i]);
787 static struct zswap_pool *__zswap_pool_create_fallback(void)
789 bool has_comp, has_zpool;
791 has_comp = crypto_has_acomp(zswap_compressor, 0, 0);
792 if (!has_comp && strcmp(zswap_compressor,
793 CONFIG_ZSWAP_COMPRESSOR_DEFAULT)) {
794 pr_err("compressor %s not available, using default %s\n",
795 zswap_compressor, CONFIG_ZSWAP_COMPRESSOR_DEFAULT);
796 param_free_charp(&zswap_compressor);
797 zswap_compressor = CONFIG_ZSWAP_COMPRESSOR_DEFAULT;
798 has_comp = crypto_has_acomp(zswap_compressor, 0, 0);
801 pr_err("default compressor %s not available\n",
803 param_free_charp(&zswap_compressor);
804 zswap_compressor = ZSWAP_PARAM_UNSET;
807 has_zpool = zpool_has_pool(zswap_zpool_type);
808 if (!has_zpool && strcmp(zswap_zpool_type,
809 CONFIG_ZSWAP_ZPOOL_DEFAULT)) {
810 pr_err("zpool %s not available, using default %s\n",
811 zswap_zpool_type, CONFIG_ZSWAP_ZPOOL_DEFAULT);
812 param_free_charp(&zswap_zpool_type);
813 zswap_zpool_type = CONFIG_ZSWAP_ZPOOL_DEFAULT;
814 has_zpool = zpool_has_pool(zswap_zpool_type);
817 pr_err("default zpool %s not available\n",
819 param_free_charp(&zswap_zpool_type);
820 zswap_zpool_type = ZSWAP_PARAM_UNSET;
823 if (!has_comp || !has_zpool)
826 return zswap_pool_create(zswap_zpool_type, zswap_compressor);
829 static void zswap_pool_destroy(struct zswap_pool *pool)
833 zswap_pool_debug("destroying", pool);
835 cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node);
836 free_percpu(pool->acomp_ctx);
837 for (i = 0; i < ZSWAP_NR_ZPOOLS; i++)
838 zpool_destroy_pool(pool->zpools[i]);
842 static int __must_check zswap_pool_get(struct zswap_pool *pool)
847 return kref_get_unless_zero(&pool->kref);
850 static void __zswap_pool_release(struct work_struct *work)
852 struct zswap_pool *pool = container_of(work, typeof(*pool),
857 /* nobody should have been able to get a kref... */
858 WARN_ON(kref_get_unless_zero(&pool->kref));
860 /* pool is now off zswap_pools list and has no references. */
861 zswap_pool_destroy(pool);
864 static void __zswap_pool_empty(struct kref *kref)
866 struct zswap_pool *pool;
868 pool = container_of(kref, typeof(*pool), kref);
870 spin_lock(&zswap_pools_lock);
872 WARN_ON(pool == zswap_pool_current());
874 list_del_rcu(&pool->list);
876 INIT_WORK(&pool->release_work, __zswap_pool_release);
877 schedule_work(&pool->release_work);
879 spin_unlock(&zswap_pools_lock);
882 static void zswap_pool_put(struct zswap_pool *pool)
884 kref_put(&pool->kref, __zswap_pool_empty);
887 /*********************************
889 **********************************/
891 static bool zswap_pool_changed(const char *s, const struct kernel_param *kp)
893 /* no change required */
894 if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool)
899 /* val must be a null-terminated string */
900 static int __zswap_param_set(const char *val, const struct kernel_param *kp,
901 char *type, char *compressor)
903 struct zswap_pool *pool, *put_pool = NULL;
904 char *s = strstrip((char *)val);
906 bool new_pool = false;
908 mutex_lock(&zswap_init_lock);
909 switch (zswap_init_state) {
911 /* if this is load-time (pre-init) param setting,
912 * don't create a pool; that's done during init.
914 ret = param_set_charp(s, kp);
916 case ZSWAP_INIT_SUCCEED:
917 new_pool = zswap_pool_changed(s, kp);
919 case ZSWAP_INIT_FAILED:
920 pr_err("can't set param, initialization failed\n");
923 mutex_unlock(&zswap_init_lock);
925 /* no need to create a new pool, return directly */
930 if (!zpool_has_pool(s)) {
931 pr_err("zpool %s not available\n", s);
935 } else if (!compressor) {
936 if (!crypto_has_acomp(s, 0, 0)) {
937 pr_err("compressor %s not available\n", s);
946 spin_lock(&zswap_pools_lock);
948 pool = zswap_pool_find_get(type, compressor);
950 zswap_pool_debug("using existing", pool);
951 WARN_ON(pool == zswap_pool_current());
952 list_del_rcu(&pool->list);
955 spin_unlock(&zswap_pools_lock);
958 pool = zswap_pool_create(type, compressor);
961 ret = param_set_charp(s, kp);
965 spin_lock(&zswap_pools_lock);
968 put_pool = zswap_pool_current();
969 list_add_rcu(&pool->list, &zswap_pools);
970 zswap_has_pool = true;
972 /* add the possibly pre-existing pool to the end of the pools
973 * list; if it's new (and empty) then it'll be removed and
974 * destroyed by the put after we drop the lock
976 list_add_tail_rcu(&pool->list, &zswap_pools);
980 spin_unlock(&zswap_pools_lock);
982 if (!zswap_has_pool && !pool) {
983 /* if initial pool creation failed, and this pool creation also
984 * failed, maybe both compressor and zpool params were bad.
985 * Allow changing this param, so pool creation will succeed
986 * when the other param is changed. We already verified this
987 * param is ok in the zpool_has_pool() or crypto_has_acomp()
990 ret = param_set_charp(s, kp);
993 /* drop the ref from either the old current pool,
994 * or the new pool we failed to add
997 zswap_pool_put(put_pool);
1002 static int zswap_compressor_param_set(const char *val,
1003 const struct kernel_param *kp)
1005 return __zswap_param_set(val, kp, zswap_zpool_type, NULL);
1008 static int zswap_zpool_param_set(const char *val,
1009 const struct kernel_param *kp)
1011 return __zswap_param_set(val, kp, NULL, zswap_compressor);
1014 static int zswap_enabled_param_set(const char *val,
1015 const struct kernel_param *kp)
1019 /* if this is load-time (pre-init) param setting, only set param. */
1020 if (system_state != SYSTEM_RUNNING)
1021 return param_set_bool(val, kp);
1023 mutex_lock(&zswap_init_lock);
1024 switch (zswap_init_state) {
1029 case ZSWAP_INIT_SUCCEED:
1030 if (!zswap_has_pool)
1031 pr_err("can't enable, no pool configured\n");
1033 ret = param_set_bool(val, kp);
1035 case ZSWAP_INIT_FAILED:
1036 pr_err("can't enable, initialization failed\n");
1038 mutex_unlock(&zswap_init_lock);
1043 /*********************************
1045 **********************************/
1047 * Attempts to free an entry by adding a page to the swap cache,
1048 * decompressing the entry data into the page, and issuing a
1049 * bio write to write the page back to the swap device.
1051 * This can be thought of as a "resumed writeback" of the page
1052 * to the swap device. We are basically resuming the same swap
1053 * writeback path that was intercepted with the zswap_store()
1054 * in the first place. After the page has been decompressed into
1055 * the swap cache, the compressed version stored by zswap can be
1058 static int zswap_writeback_entry(struct zswap_entry *entry,
1059 struct zswap_tree *tree)
1061 swp_entry_t swpentry = entry->swpentry;
1063 struct mempolicy *mpol;
1064 struct scatterlist input, output;
1065 struct crypto_acomp_ctx *acomp_ctx;
1066 struct zpool *pool = zswap_find_zpool(entry);
1067 bool page_was_allocated;
1068 u8 *src, *tmp = NULL;
1071 struct writeback_control wbc = {
1072 .sync_mode = WB_SYNC_NONE,
1075 if (!zpool_can_sleep_mapped(pool)) {
1076 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
1081 /* try to allocate swap cache page */
1082 mpol = get_task_policy(current);
1083 page = __read_swap_cache_async(swpentry, GFP_KERNEL, mpol,
1084 NO_INTERLEAVE_INDEX, &page_was_allocated);
1090 /* Found an existing page, we raced with load/swapin */
1091 if (!page_was_allocated) {
1098 * Page is locked, and the swapcache is now secured against
1099 * concurrent swapping to and from the slot. Verify that the
1100 * swap entry hasn't been invalidated and recycled behind our
1101 * backs (our zswap_entry reference doesn't prevent that), to
1102 * avoid overwriting a new swap page with old compressed data.
1104 spin_lock(&tree->lock);
1105 if (zswap_rb_search(&tree->rbroot, swp_offset(entry->swpentry)) != entry) {
1106 spin_unlock(&tree->lock);
1107 delete_from_swap_cache(page_folio(page));
1111 spin_unlock(&tree->lock);
1114 acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
1117 src = zpool_map_handle(pool, entry->handle, ZPOOL_MM_RO);
1118 if (!zpool_can_sleep_mapped(pool)) {
1119 memcpy(tmp, src, entry->length);
1121 zpool_unmap_handle(pool, entry->handle);
1124 mutex_lock(acomp_ctx->mutex);
1125 sg_init_one(&input, src, entry->length);
1126 sg_init_table(&output, 1);
1127 sg_set_page(&output, page, PAGE_SIZE, 0);
1128 acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, dlen);
1129 ret = crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait);
1130 dlen = acomp_ctx->req->dlen;
1131 mutex_unlock(acomp_ctx->mutex);
1133 if (!zpool_can_sleep_mapped(pool))
1136 zpool_unmap_handle(pool, entry->handle);
1139 BUG_ON(dlen != PAGE_SIZE);
1141 /* page is up to date */
1142 SetPageUptodate(page);
1144 /* move it to the tail of the inactive list after end_writeback */
1145 SetPageReclaim(page);
1147 /* start writeback */
1148 __swap_writepage(page, &wbc);
1150 zswap_written_back_pages++;
1155 if (!zpool_can_sleep_mapped(pool))
1159 * If we get here because the page is already in swapcache, a
1160 * load may be happening concurrently. It is safe and okay to
1161 * not free the entry. It is also okay to return !0.
1166 static int zswap_is_page_same_filled(void *ptr, unsigned long *value)
1168 unsigned long *page;
1170 unsigned int pos, last_pos = PAGE_SIZE / sizeof(*page) - 1;
1172 page = (unsigned long *)ptr;
1175 if (val != page[last_pos])
1178 for (pos = 1; pos < last_pos; pos++) {
1179 if (val != page[pos])
1188 static void zswap_fill_page(void *ptr, unsigned long value)
1190 unsigned long *page;
1192 page = (unsigned long *)ptr;
1193 memset_l(page, value, PAGE_SIZE / sizeof(unsigned long));
1196 bool zswap_store(struct folio *folio)
1198 swp_entry_t swp = folio->swap;
1199 int type = swp_type(swp);
1200 pgoff_t offset = swp_offset(swp);
1201 struct page *page = &folio->page;
1202 struct zswap_tree *tree = zswap_trees[type];
1203 struct zswap_entry *entry, *dupentry;
1204 struct scatterlist input, output;
1205 struct crypto_acomp_ctx *acomp_ctx;
1206 struct obj_cgroup *objcg = NULL;
1207 struct zswap_pool *pool;
1208 struct zpool *zpool;
1209 unsigned int dlen = PAGE_SIZE;
1210 unsigned long handle, value;
1216 VM_WARN_ON_ONCE(!folio_test_locked(folio));
1217 VM_WARN_ON_ONCE(!folio_test_swapcache(folio));
1219 /* Large folios aren't supported */
1220 if (folio_test_large(folio))
1223 if (!zswap_enabled || !tree)
1227 * If this is a duplicate, it must be removed before attempting to store
1228 * it, otherwise, if the store fails the old page won't be removed from
1229 * the tree, and it might be written back overriding the new data.
1231 spin_lock(&tree->lock);
1232 dupentry = zswap_rb_search(&tree->rbroot, offset);
1234 zswap_duplicate_entry++;
1235 zswap_invalidate_entry(tree, dupentry);
1237 spin_unlock(&tree->lock);
1240 * XXX: zswap reclaim does not work with cgroups yet. Without a
1241 * cgroup-aware entry LRU, we will push out entries system-wide based on
1242 * local cgroup limits.
1244 objcg = get_obj_cgroup_from_folio(folio);
1245 if (objcg && !obj_cgroup_may_zswap(objcg))
1248 /* reclaim space if needed */
1249 if (zswap_is_full()) {
1250 zswap_pool_limit_hit++;
1251 zswap_pool_reached_full = true;
1255 if (zswap_pool_reached_full) {
1256 if (!zswap_can_accept())
1259 zswap_pool_reached_full = false;
1262 /* allocate entry */
1263 entry = zswap_entry_cache_alloc(GFP_KERNEL);
1265 zswap_reject_kmemcache_fail++;
1269 if (zswap_same_filled_pages_enabled) {
1270 src = kmap_atomic(page);
1271 if (zswap_is_page_same_filled(src, &value)) {
1273 entry->swpentry = swp_entry(type, offset);
1275 entry->value = value;
1276 atomic_inc(&zswap_same_filled_pages);
1282 if (!zswap_non_same_filled_pages_enabled)
1285 /* if entry is successfully added, it keeps the reference */
1286 entry->pool = zswap_pool_current_get();
1291 acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
1293 mutex_lock(acomp_ctx->mutex);
1295 dst = acomp_ctx->dstmem;
1296 sg_init_table(&input, 1);
1297 sg_set_page(&input, page, PAGE_SIZE, 0);
1299 /* zswap_dstmem is of size (PAGE_SIZE * 2). Reflect same in sg_list */
1300 sg_init_one(&output, dst, PAGE_SIZE * 2);
1301 acomp_request_set_params(acomp_ctx->req, &input, &output, PAGE_SIZE, dlen);
1303 * it maybe looks a little bit silly that we send an asynchronous request,
1304 * then wait for its completion synchronously. This makes the process look
1305 * synchronous in fact.
1306 * Theoretically, acomp supports users send multiple acomp requests in one
1307 * acomp instance, then get those requests done simultaneously. but in this
1308 * case, zswap actually does store and load page by page, there is no
1309 * existing method to send the second page before the first page is done
1310 * in one thread doing zwap.
1311 * but in different threads running on different cpu, we have different
1312 * acomp instance, so multiple threads can do (de)compression in parallel.
1314 ret = crypto_wait_req(crypto_acomp_compress(acomp_ctx->req), &acomp_ctx->wait);
1315 dlen = acomp_ctx->req->dlen;
1318 zswap_reject_compress_fail++;
1323 zpool = zswap_find_zpool(entry);
1324 gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;
1325 if (zpool_malloc_support_movable(zpool))
1326 gfp |= __GFP_HIGHMEM | __GFP_MOVABLE;
1327 ret = zpool_malloc(zpool, dlen, gfp, &handle);
1328 if (ret == -ENOSPC) {
1329 zswap_reject_compress_poor++;
1333 zswap_reject_alloc_fail++;
1336 buf = zpool_map_handle(zpool, handle, ZPOOL_MM_WO);
1337 memcpy(buf, dst, dlen);
1338 zpool_unmap_handle(zpool, handle);
1339 mutex_unlock(acomp_ctx->mutex);
1341 /* populate entry */
1342 entry->swpentry = swp_entry(type, offset);
1343 entry->handle = handle;
1344 entry->length = dlen;
1347 entry->objcg = objcg;
1349 obj_cgroup_charge_zswap(objcg, entry->length);
1350 /* Account before objcg ref is moved to tree */
1351 count_objcg_event(objcg, ZSWPOUT);
1355 spin_lock(&tree->lock);
1357 * A duplicate entry should have been removed at the beginning of this
1358 * function. Since the swap entry should be pinned, if a duplicate is
1359 * found again here it means that something went wrong in the swap
1362 while (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) {
1364 zswap_duplicate_entry++;
1365 zswap_invalidate_entry(tree, dupentry);
1367 if (entry->length) {
1368 spin_lock(&entry->pool->lru_lock);
1369 list_add(&entry->lru, &entry->pool->lru);
1370 spin_unlock(&entry->pool->lru_lock);
1372 spin_unlock(&tree->lock);
1375 atomic_inc(&zswap_stored_pages);
1376 zswap_update_total_size();
1377 count_vm_event(ZSWPOUT);
1382 mutex_unlock(acomp_ctx->mutex);
1383 zswap_pool_put(entry->pool);
1385 zswap_entry_cache_free(entry);
1388 obj_cgroup_put(objcg);
1392 pool = zswap_pool_last_get();
1393 if (pool && !queue_work(shrink_wq, &pool->shrink_work))
1394 zswap_pool_put(pool);
1398 bool zswap_load(struct folio *folio)
1400 swp_entry_t swp = folio->swap;
1401 int type = swp_type(swp);
1402 pgoff_t offset = swp_offset(swp);
1403 struct page *page = &folio->page;
1404 struct zswap_tree *tree = zswap_trees[type];
1405 struct zswap_entry *entry;
1406 struct scatterlist input, output;
1407 struct crypto_acomp_ctx *acomp_ctx;
1408 u8 *src, *dst, *tmp;
1409 struct zpool *zpool;
1413 VM_WARN_ON_ONCE(!folio_test_locked(folio));
1416 spin_lock(&tree->lock);
1417 entry = zswap_entry_find_get(&tree->rbroot, offset);
1419 spin_unlock(&tree->lock);
1422 spin_unlock(&tree->lock);
1424 if (!entry->length) {
1425 dst = kmap_atomic(page);
1426 zswap_fill_page(dst, entry->value);
1432 zpool = zswap_find_zpool(entry);
1433 if (!zpool_can_sleep_mapped(zpool)) {
1434 tmp = kmalloc(entry->length, GFP_KERNEL);
1443 src = zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO);
1445 if (!zpool_can_sleep_mapped(zpool)) {
1446 memcpy(tmp, src, entry->length);
1448 zpool_unmap_handle(zpool, entry->handle);
1451 acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
1452 mutex_lock(acomp_ctx->mutex);
1453 sg_init_one(&input, src, entry->length);
1454 sg_init_table(&output, 1);
1455 sg_set_page(&output, page, PAGE_SIZE, 0);
1456 acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, dlen);
1457 if (crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait))
1459 mutex_unlock(acomp_ctx->mutex);
1461 if (zpool_can_sleep_mapped(zpool))
1462 zpool_unmap_handle(zpool, entry->handle);
1468 count_vm_event(ZSWPIN);
1470 count_objcg_event(entry->objcg, ZSWPIN);
1472 spin_lock(&tree->lock);
1473 if (ret && zswap_exclusive_loads_enabled) {
1474 zswap_invalidate_entry(tree, entry);
1475 folio_mark_dirty(folio);
1476 } else if (entry->length) {
1477 spin_lock(&entry->pool->lru_lock);
1478 list_move(&entry->lru, &entry->pool->lru);
1479 spin_unlock(&entry->pool->lru_lock);
1481 zswap_entry_put(tree, entry);
1482 spin_unlock(&tree->lock);
1487 void zswap_invalidate(int type, pgoff_t offset)
1489 struct zswap_tree *tree = zswap_trees[type];
1490 struct zswap_entry *entry;
1493 spin_lock(&tree->lock);
1494 entry = zswap_rb_search(&tree->rbroot, offset);
1496 /* entry was written back */
1497 spin_unlock(&tree->lock);
1500 zswap_invalidate_entry(tree, entry);
1501 spin_unlock(&tree->lock);
1504 void zswap_swapon(int type)
1506 struct zswap_tree *tree;
1508 tree = kzalloc(sizeof(*tree), GFP_KERNEL);
1510 pr_err("alloc failed, zswap disabled for swap type %d\n", type);
1514 tree->rbroot = RB_ROOT;
1515 spin_lock_init(&tree->lock);
1516 zswap_trees[type] = tree;
1519 void zswap_swapoff(int type)
1521 struct zswap_tree *tree = zswap_trees[type];
1522 struct zswap_entry *entry, *n;
1527 /* walk the tree and free everything */
1528 spin_lock(&tree->lock);
1529 rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode)
1530 zswap_free_entry(entry);
1531 tree->rbroot = RB_ROOT;
1532 spin_unlock(&tree->lock);
1534 zswap_trees[type] = NULL;
1537 /*********************************
1539 **********************************/
1540 #ifdef CONFIG_DEBUG_FS
1541 #include <linux/debugfs.h>
1543 static struct dentry *zswap_debugfs_root;
1545 static int zswap_debugfs_init(void)
1547 if (!debugfs_initialized())
1550 zswap_debugfs_root = debugfs_create_dir("zswap", NULL);
1552 debugfs_create_u64("pool_limit_hit", 0444,
1553 zswap_debugfs_root, &zswap_pool_limit_hit);
1554 debugfs_create_u64("reject_reclaim_fail", 0444,
1555 zswap_debugfs_root, &zswap_reject_reclaim_fail);
1556 debugfs_create_u64("reject_alloc_fail", 0444,
1557 zswap_debugfs_root, &zswap_reject_alloc_fail);
1558 debugfs_create_u64("reject_kmemcache_fail", 0444,
1559 zswap_debugfs_root, &zswap_reject_kmemcache_fail);
1560 debugfs_create_u64("reject_compress_fail", 0444,
1561 zswap_debugfs_root, &zswap_reject_compress_fail);
1562 debugfs_create_u64("reject_compress_poor", 0444,
1563 zswap_debugfs_root, &zswap_reject_compress_poor);
1564 debugfs_create_u64("written_back_pages", 0444,
1565 zswap_debugfs_root, &zswap_written_back_pages);
1566 debugfs_create_u64("duplicate_entry", 0444,
1567 zswap_debugfs_root, &zswap_duplicate_entry);
1568 debugfs_create_u64("pool_total_size", 0444,
1569 zswap_debugfs_root, &zswap_pool_total_size);
1570 debugfs_create_atomic_t("stored_pages", 0444,
1571 zswap_debugfs_root, &zswap_stored_pages);
1572 debugfs_create_atomic_t("same_filled_pages", 0444,
1573 zswap_debugfs_root, &zswap_same_filled_pages);
1578 static int zswap_debugfs_init(void)
1584 /*********************************
1585 * module init and exit
1586 **********************************/
1587 static int zswap_setup(void)
1589 struct zswap_pool *pool;
1592 zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
1593 if (!zswap_entry_cache) {
1594 pr_err("entry cache creation failed\n");
1598 ret = cpuhp_setup_state(CPUHP_MM_ZSWP_MEM_PREPARE, "mm/zswap:prepare",
1599 zswap_dstmem_prepare, zswap_dstmem_dead);
1601 pr_err("dstmem alloc failed\n");
1605 ret = cpuhp_setup_state_multi(CPUHP_MM_ZSWP_POOL_PREPARE,
1606 "mm/zswap_pool:prepare",
1607 zswap_cpu_comp_prepare,
1608 zswap_cpu_comp_dead);
1612 pool = __zswap_pool_create_fallback();
1614 pr_info("loaded using pool %s/%s\n", pool->tfm_name,
1615 zpool_get_type(pool->zpools[0]));
1616 list_add(&pool->list, &zswap_pools);
1617 zswap_has_pool = true;
1619 pr_err("pool creation failed\n");
1620 zswap_enabled = false;
1623 shrink_wq = create_workqueue("zswap-shrink");
1627 if (zswap_debugfs_init())
1628 pr_warn("debugfs initialization failed\n");
1629 zswap_init_state = ZSWAP_INIT_SUCCEED;
1634 zswap_pool_destroy(pool);
1636 cpuhp_remove_state(CPUHP_MM_ZSWP_MEM_PREPARE);
1638 kmem_cache_destroy(zswap_entry_cache);
1640 /* if built-in, we aren't unloaded on failure; don't allow use */
1641 zswap_init_state = ZSWAP_INIT_FAILED;
1642 zswap_enabled = false;
1646 static int __init zswap_init(void)
1650 return zswap_setup();
1652 /* must be late so crypto has time to come up */
1653 late_initcall(zswap_init);
1656 MODULE_DESCRIPTION("Compressed cache for swap pages");