xref: /openbmc/linux/mm/zswap.c (revision 2fe60ec9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * zswap.c - zswap driver file
4  *
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.
10  *
11  * Copyright (C) 2012  Seth Jennings <sjenning@linux.vnet.ibm.com>
12 */
13 
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 
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>
31 
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>
38 
39 #include "swap.h"
40 
41 /*********************************
42 * statistics
43 **********************************/
44 /* Total bytes used by the compressed storage */
45 static u64 zswap_pool_total_size;
46 /* The number of compressed pages currently stored in zswap */
47 static atomic_t zswap_stored_pages = ATOMIC_INIT(0);
48 /* The number of same-value filled pages currently stored in zswap */
49 static atomic_t zswap_same_filled_pages = ATOMIC_INIT(0);
50 
51 /*
52  * The statistics below are not protected from concurrent access for
53  * performance reasons so they may not be a 100% accurate.  However,
54  * they do provide useful information on roughly how many times a
55  * certain event is occurring.
56 */
57 
58 /* Pool limit was hit (see zswap_max_pool_percent) */
59 static u64 zswap_pool_limit_hit;
60 /* Pages written back when pool limit was reached */
61 static u64 zswap_written_back_pages;
62 /* Store failed due to a reclaim failure after pool limit was reached */
63 static u64 zswap_reject_reclaim_fail;
64 /* Compressed page was too big for the allocator to (optimally) store */
65 static u64 zswap_reject_compress_poor;
66 /* Store failed because underlying allocator could not get memory */
67 static u64 zswap_reject_alloc_fail;
68 /* Store failed because the entry metadata could not be allocated (rare) */
69 static u64 zswap_reject_kmemcache_fail;
70 /* Duplicate store was encountered (rare) */
71 static u64 zswap_duplicate_entry;
72 
73 /* Shrinker work queue */
74 static struct workqueue_struct *shrink_wq;
75 /* Pool limit was hit, we need to calm down */
76 static bool zswap_pool_reached_full;
77 
78 /*********************************
79 * tunables
80 **********************************/
81 
82 #define ZSWAP_PARAM_UNSET ""
83 
84 /* Enable/disable zswap */
85 static bool zswap_enabled = IS_ENABLED(CONFIG_ZSWAP_DEFAULT_ON);
86 static int zswap_enabled_param_set(const char *,
87 				   const struct kernel_param *);
88 static const struct kernel_param_ops zswap_enabled_param_ops = {
89 	.set =		zswap_enabled_param_set,
90 	.get =		param_get_bool,
91 };
92 module_param_cb(enabled, &zswap_enabled_param_ops, &zswap_enabled, 0644);
93 
94 /* Crypto compressor to use */
95 static char *zswap_compressor = CONFIG_ZSWAP_COMPRESSOR_DEFAULT;
96 static int zswap_compressor_param_set(const char *,
97 				      const struct kernel_param *);
98 static const struct kernel_param_ops zswap_compressor_param_ops = {
99 	.set =		zswap_compressor_param_set,
100 	.get =		param_get_charp,
101 	.free =		param_free_charp,
102 };
103 module_param_cb(compressor, &zswap_compressor_param_ops,
104 		&zswap_compressor, 0644);
105 
106 /* Compressed storage zpool to use */
107 static char *zswap_zpool_type = CONFIG_ZSWAP_ZPOOL_DEFAULT;
108 static int zswap_zpool_param_set(const char *, const struct kernel_param *);
109 static const struct kernel_param_ops zswap_zpool_param_ops = {
110 	.set =		zswap_zpool_param_set,
111 	.get =		param_get_charp,
112 	.free =		param_free_charp,
113 };
114 module_param_cb(zpool, &zswap_zpool_param_ops, &zswap_zpool_type, 0644);
115 
116 /* The maximum percentage of memory that the compressed pool can occupy */
117 static unsigned int zswap_max_pool_percent = 20;
118 module_param_named(max_pool_percent, zswap_max_pool_percent, uint, 0644);
119 
120 /* The threshold for accepting new pages after the max_pool_percent was hit */
121 static unsigned int zswap_accept_thr_percent = 90; /* of max pool size */
122 module_param_named(accept_threshold_percent, zswap_accept_thr_percent,
123 		   uint, 0644);
124 
125 /*
126  * Enable/disable handling same-value filled pages (enabled by default).
127  * If disabled every page is considered non-same-value filled.
128  */
129 static bool zswap_same_filled_pages_enabled = true;
130 module_param_named(same_filled_pages_enabled, zswap_same_filled_pages_enabled,
131 		   bool, 0644);
132 
133 /* Enable/disable handling non-same-value filled pages (enabled by default) */
134 static bool zswap_non_same_filled_pages_enabled = true;
135 module_param_named(non_same_filled_pages_enabled, zswap_non_same_filled_pages_enabled,
136 		   bool, 0644);
137 
138 /*********************************
139 * data structures
140 **********************************/
141 
142 struct crypto_acomp_ctx {
143 	struct crypto_acomp *acomp;
144 	struct acomp_req *req;
145 	struct crypto_wait wait;
146 	u8 *dstmem;
147 	struct mutex *mutex;
148 };
149 
150 struct zswap_pool {
151 	struct zpool *zpool;
152 	struct crypto_acomp_ctx __percpu *acomp_ctx;
153 	struct kref kref;
154 	struct list_head list;
155 	struct work_struct release_work;
156 	struct work_struct shrink_work;
157 	struct hlist_node node;
158 	char tfm_name[CRYPTO_MAX_ALG_NAME];
159 };
160 
161 /*
162  * struct zswap_entry
163  *
164  * This structure contains the metadata for tracking a single compressed
165  * page within zswap.
166  *
167  * rbnode - links the entry into red-black tree for the appropriate swap type
168  * offset - the swap offset for the entry.  Index into the red-black tree.
169  * refcount - the number of outstanding reference to the entry. This is needed
170  *            to protect against premature freeing of the entry by code
171  *            concurrent calls to load, invalidate, and writeback.  The lock
172  *            for the zswap_tree structure that contains the entry must
173  *            be held while changing the refcount.  Since the lock must
174  *            be held, there is no reason to also make refcount atomic.
175  * length - the length in bytes of the compressed page data.  Needed during
176  *          decompression. For a same value filled page length is 0.
177  * pool - the zswap_pool the entry's data is in
178  * handle - zpool allocation handle that stores the compressed page data
179  * value - value of the same-value filled pages which have same content
180  */
181 struct zswap_entry {
182 	struct rb_node rbnode;
183 	pgoff_t offset;
184 	int refcount;
185 	unsigned int length;
186 	struct zswap_pool *pool;
187 	union {
188 		unsigned long handle;
189 		unsigned long value;
190 	};
191 };
192 
193 struct zswap_header {
194 	swp_entry_t swpentry;
195 };
196 
197 /*
198  * The tree lock in the zswap_tree struct protects a few things:
199  * - the rbtree
200  * - the refcount field of each entry in the tree
201  */
202 struct zswap_tree {
203 	struct rb_root rbroot;
204 	spinlock_t lock;
205 };
206 
207 static struct zswap_tree *zswap_trees[MAX_SWAPFILES];
208 
209 /* RCU-protected iteration */
210 static LIST_HEAD(zswap_pools);
211 /* protects zswap_pools list modification */
212 static DEFINE_SPINLOCK(zswap_pools_lock);
213 /* pool counter to provide unique names to zpool */
214 static atomic_t zswap_pools_count = ATOMIC_INIT(0);
215 
216 /* used by param callback function */
217 static bool zswap_init_started;
218 
219 /* fatal error during init */
220 static bool zswap_init_failed;
221 
222 /* init completed, but couldn't create the initial pool */
223 static bool zswap_has_pool;
224 
225 /*********************************
226 * helpers and fwd declarations
227 **********************************/
228 
229 #define zswap_pool_debug(msg, p)				\
230 	pr_debug("%s pool %s/%s\n", msg, (p)->tfm_name,		\
231 		 zpool_get_type((p)->zpool))
232 
233 static int zswap_writeback_entry(struct zpool *pool, unsigned long handle);
234 static int zswap_pool_get(struct zswap_pool *pool);
235 static void zswap_pool_put(struct zswap_pool *pool);
236 
237 static const struct zpool_ops zswap_zpool_ops = {
238 	.evict = zswap_writeback_entry
239 };
240 
241 static bool zswap_is_full(void)
242 {
243 	return totalram_pages() * zswap_max_pool_percent / 100 <
244 			DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
245 }
246 
247 static bool zswap_can_accept(void)
248 {
249 	return totalram_pages() * zswap_accept_thr_percent / 100 *
250 				zswap_max_pool_percent / 100 >
251 			DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
252 }
253 
254 static void zswap_update_total_size(void)
255 {
256 	struct zswap_pool *pool;
257 	u64 total = 0;
258 
259 	rcu_read_lock();
260 
261 	list_for_each_entry_rcu(pool, &zswap_pools, list)
262 		total += zpool_get_total_size(pool->zpool);
263 
264 	rcu_read_unlock();
265 
266 	zswap_pool_total_size = total;
267 }
268 
269 /*********************************
270 * zswap entry functions
271 **********************************/
272 static struct kmem_cache *zswap_entry_cache;
273 
274 static int __init zswap_entry_cache_create(void)
275 {
276 	zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
277 	return zswap_entry_cache == NULL;
278 }
279 
280 static void __init zswap_entry_cache_destroy(void)
281 {
282 	kmem_cache_destroy(zswap_entry_cache);
283 }
284 
285 static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp)
286 {
287 	struct zswap_entry *entry;
288 	entry = kmem_cache_alloc(zswap_entry_cache, gfp);
289 	if (!entry)
290 		return NULL;
291 	entry->refcount = 1;
292 	RB_CLEAR_NODE(&entry->rbnode);
293 	return entry;
294 }
295 
296 static void zswap_entry_cache_free(struct zswap_entry *entry)
297 {
298 	kmem_cache_free(zswap_entry_cache, entry);
299 }
300 
301 /*********************************
302 * rbtree functions
303 **********************************/
304 static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset)
305 {
306 	struct rb_node *node = root->rb_node;
307 	struct zswap_entry *entry;
308 
309 	while (node) {
310 		entry = rb_entry(node, struct zswap_entry, rbnode);
311 		if (entry->offset > offset)
312 			node = node->rb_left;
313 		else if (entry->offset < offset)
314 			node = node->rb_right;
315 		else
316 			return entry;
317 	}
318 	return NULL;
319 }
320 
321 /*
322  * In the case that a entry with the same offset is found, a pointer to
323  * the existing entry is stored in dupentry and the function returns -EEXIST
324  */
325 static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry,
326 			struct zswap_entry **dupentry)
327 {
328 	struct rb_node **link = &root->rb_node, *parent = NULL;
329 	struct zswap_entry *myentry;
330 
331 	while (*link) {
332 		parent = *link;
333 		myentry = rb_entry(parent, struct zswap_entry, rbnode);
334 		if (myentry->offset > entry->offset)
335 			link = &(*link)->rb_left;
336 		else if (myentry->offset < entry->offset)
337 			link = &(*link)->rb_right;
338 		else {
339 			*dupentry = myentry;
340 			return -EEXIST;
341 		}
342 	}
343 	rb_link_node(&entry->rbnode, parent, link);
344 	rb_insert_color(&entry->rbnode, root);
345 	return 0;
346 }
347 
348 static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry)
349 {
350 	if (!RB_EMPTY_NODE(&entry->rbnode)) {
351 		rb_erase(&entry->rbnode, root);
352 		RB_CLEAR_NODE(&entry->rbnode);
353 	}
354 }
355 
356 /*
357  * Carries out the common pattern of freeing and entry's zpool allocation,
358  * freeing the entry itself, and decrementing the number of stored pages.
359  */
360 static void zswap_free_entry(struct zswap_entry *entry)
361 {
362 	if (!entry->length)
363 		atomic_dec(&zswap_same_filled_pages);
364 	else {
365 		zpool_free(entry->pool->zpool, entry->handle);
366 		zswap_pool_put(entry->pool);
367 	}
368 	zswap_entry_cache_free(entry);
369 	atomic_dec(&zswap_stored_pages);
370 	zswap_update_total_size();
371 }
372 
373 /* caller must hold the tree lock */
374 static void zswap_entry_get(struct zswap_entry *entry)
375 {
376 	entry->refcount++;
377 }
378 
379 /* caller must hold the tree lock
380 * remove from the tree and free it, if nobody reference the entry
381 */
382 static void zswap_entry_put(struct zswap_tree *tree,
383 			struct zswap_entry *entry)
384 {
385 	int refcount = --entry->refcount;
386 
387 	BUG_ON(refcount < 0);
388 	if (refcount == 0) {
389 		zswap_rb_erase(&tree->rbroot, entry);
390 		zswap_free_entry(entry);
391 	}
392 }
393 
394 /* caller must hold the tree lock */
395 static struct zswap_entry *zswap_entry_find_get(struct rb_root *root,
396 				pgoff_t offset)
397 {
398 	struct zswap_entry *entry;
399 
400 	entry = zswap_rb_search(root, offset);
401 	if (entry)
402 		zswap_entry_get(entry);
403 
404 	return entry;
405 }
406 
407 /*********************************
408 * per-cpu code
409 **********************************/
410 static DEFINE_PER_CPU(u8 *, zswap_dstmem);
411 /*
412  * If users dynamically change the zpool type and compressor at runtime, i.e.
413  * zswap is running, zswap can have more than one zpool on one cpu, but they
414  * are sharing dtsmem. So we need this mutex to be per-cpu.
415  */
416 static DEFINE_PER_CPU(struct mutex *, zswap_mutex);
417 
418 static int zswap_dstmem_prepare(unsigned int cpu)
419 {
420 	struct mutex *mutex;
421 	u8 *dst;
422 
423 	dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
424 	if (!dst)
425 		return -ENOMEM;
426 
427 	mutex = kmalloc_node(sizeof(*mutex), GFP_KERNEL, cpu_to_node(cpu));
428 	if (!mutex) {
429 		kfree(dst);
430 		return -ENOMEM;
431 	}
432 
433 	mutex_init(mutex);
434 	per_cpu(zswap_dstmem, cpu) = dst;
435 	per_cpu(zswap_mutex, cpu) = mutex;
436 	return 0;
437 }
438 
439 static int zswap_dstmem_dead(unsigned int cpu)
440 {
441 	struct mutex *mutex;
442 	u8 *dst;
443 
444 	mutex = per_cpu(zswap_mutex, cpu);
445 	kfree(mutex);
446 	per_cpu(zswap_mutex, cpu) = NULL;
447 
448 	dst = per_cpu(zswap_dstmem, cpu);
449 	kfree(dst);
450 	per_cpu(zswap_dstmem, cpu) = NULL;
451 
452 	return 0;
453 }
454 
455 static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
456 {
457 	struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
458 	struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu);
459 	struct crypto_acomp *acomp;
460 	struct acomp_req *req;
461 
462 	acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, cpu_to_node(cpu));
463 	if (IS_ERR(acomp)) {
464 		pr_err("could not alloc crypto acomp %s : %ld\n",
465 				pool->tfm_name, PTR_ERR(acomp));
466 		return PTR_ERR(acomp);
467 	}
468 	acomp_ctx->acomp = acomp;
469 
470 	req = acomp_request_alloc(acomp_ctx->acomp);
471 	if (!req) {
472 		pr_err("could not alloc crypto acomp_request %s\n",
473 		       pool->tfm_name);
474 		crypto_free_acomp(acomp_ctx->acomp);
475 		return -ENOMEM;
476 	}
477 	acomp_ctx->req = req;
478 
479 	crypto_init_wait(&acomp_ctx->wait);
480 	/*
481 	 * if the backend of acomp is async zip, crypto_req_done() will wakeup
482 	 * crypto_wait_req(); if the backend of acomp is scomp, the callback
483 	 * won't be called, crypto_wait_req() will return without blocking.
484 	 */
485 	acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
486 				   crypto_req_done, &acomp_ctx->wait);
487 
488 	acomp_ctx->mutex = per_cpu(zswap_mutex, cpu);
489 	acomp_ctx->dstmem = per_cpu(zswap_dstmem, cpu);
490 
491 	return 0;
492 }
493 
494 static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)
495 {
496 	struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
497 	struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu);
498 
499 	if (!IS_ERR_OR_NULL(acomp_ctx)) {
500 		if (!IS_ERR_OR_NULL(acomp_ctx->req))
501 			acomp_request_free(acomp_ctx->req);
502 		if (!IS_ERR_OR_NULL(acomp_ctx->acomp))
503 			crypto_free_acomp(acomp_ctx->acomp);
504 	}
505 
506 	return 0;
507 }
508 
509 /*********************************
510 * pool functions
511 **********************************/
512 
513 static struct zswap_pool *__zswap_pool_current(void)
514 {
515 	struct zswap_pool *pool;
516 
517 	pool = list_first_or_null_rcu(&zswap_pools, typeof(*pool), list);
518 	WARN_ONCE(!pool && zswap_has_pool,
519 		  "%s: no page storage pool!\n", __func__);
520 
521 	return pool;
522 }
523 
524 static struct zswap_pool *zswap_pool_current(void)
525 {
526 	assert_spin_locked(&zswap_pools_lock);
527 
528 	return __zswap_pool_current();
529 }
530 
531 static struct zswap_pool *zswap_pool_current_get(void)
532 {
533 	struct zswap_pool *pool;
534 
535 	rcu_read_lock();
536 
537 	pool = __zswap_pool_current();
538 	if (!zswap_pool_get(pool))
539 		pool = NULL;
540 
541 	rcu_read_unlock();
542 
543 	return pool;
544 }
545 
546 static struct zswap_pool *zswap_pool_last_get(void)
547 {
548 	struct zswap_pool *pool, *last = NULL;
549 
550 	rcu_read_lock();
551 
552 	list_for_each_entry_rcu(pool, &zswap_pools, list)
553 		last = pool;
554 	WARN_ONCE(!last && zswap_has_pool,
555 		  "%s: no page storage pool!\n", __func__);
556 	if (!zswap_pool_get(last))
557 		last = NULL;
558 
559 	rcu_read_unlock();
560 
561 	return last;
562 }
563 
564 /* type and compressor must be null-terminated */
565 static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor)
566 {
567 	struct zswap_pool *pool;
568 
569 	assert_spin_locked(&zswap_pools_lock);
570 
571 	list_for_each_entry_rcu(pool, &zswap_pools, list) {
572 		if (strcmp(pool->tfm_name, compressor))
573 			continue;
574 		if (strcmp(zpool_get_type(pool->zpool), type))
575 			continue;
576 		/* if we can't get it, it's about to be destroyed */
577 		if (!zswap_pool_get(pool))
578 			continue;
579 		return pool;
580 	}
581 
582 	return NULL;
583 }
584 
585 static void shrink_worker(struct work_struct *w)
586 {
587 	struct zswap_pool *pool = container_of(w, typeof(*pool),
588 						shrink_work);
589 
590 	if (zpool_shrink(pool->zpool, 1, NULL))
591 		zswap_reject_reclaim_fail++;
592 	zswap_pool_put(pool);
593 }
594 
595 static struct zswap_pool *zswap_pool_create(char *type, char *compressor)
596 {
597 	struct zswap_pool *pool;
598 	char name[38]; /* 'zswap' + 32 char (max) num + \0 */
599 	gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;
600 	int ret;
601 
602 	if (!zswap_has_pool) {
603 		/* if either are unset, pool initialization failed, and we
604 		 * need both params to be set correctly before trying to
605 		 * create a pool.
606 		 */
607 		if (!strcmp(type, ZSWAP_PARAM_UNSET))
608 			return NULL;
609 		if (!strcmp(compressor, ZSWAP_PARAM_UNSET))
610 			return NULL;
611 	}
612 
613 	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
614 	if (!pool)
615 		return NULL;
616 
617 	/* unique name for each pool specifically required by zsmalloc */
618 	snprintf(name, 38, "zswap%x", atomic_inc_return(&zswap_pools_count));
619 
620 	pool->zpool = zpool_create_pool(type, name, gfp, &zswap_zpool_ops);
621 	if (!pool->zpool) {
622 		pr_err("%s zpool not available\n", type);
623 		goto error;
624 	}
625 	pr_debug("using %s zpool\n", zpool_get_type(pool->zpool));
626 
627 	strscpy(pool->tfm_name, compressor, sizeof(pool->tfm_name));
628 
629 	pool->acomp_ctx = alloc_percpu(*pool->acomp_ctx);
630 	if (!pool->acomp_ctx) {
631 		pr_err("percpu alloc failed\n");
632 		goto error;
633 	}
634 
635 	ret = cpuhp_state_add_instance(CPUHP_MM_ZSWP_POOL_PREPARE,
636 				       &pool->node);
637 	if (ret)
638 		goto error;
639 	pr_debug("using %s compressor\n", pool->tfm_name);
640 
641 	/* being the current pool takes 1 ref; this func expects the
642 	 * caller to always add the new pool as the current pool
643 	 */
644 	kref_init(&pool->kref);
645 	INIT_LIST_HEAD(&pool->list);
646 	INIT_WORK(&pool->shrink_work, shrink_worker);
647 
648 	zswap_pool_debug("created", pool);
649 
650 	return pool;
651 
652 error:
653 	if (pool->acomp_ctx)
654 		free_percpu(pool->acomp_ctx);
655 	if (pool->zpool)
656 		zpool_destroy_pool(pool->zpool);
657 	kfree(pool);
658 	return NULL;
659 }
660 
661 static __init struct zswap_pool *__zswap_pool_create_fallback(void)
662 {
663 	bool has_comp, has_zpool;
664 
665 	has_comp = crypto_has_acomp(zswap_compressor, 0, 0);
666 	if (!has_comp && strcmp(zswap_compressor,
667 				CONFIG_ZSWAP_COMPRESSOR_DEFAULT)) {
668 		pr_err("compressor %s not available, using default %s\n",
669 		       zswap_compressor, CONFIG_ZSWAP_COMPRESSOR_DEFAULT);
670 		param_free_charp(&zswap_compressor);
671 		zswap_compressor = CONFIG_ZSWAP_COMPRESSOR_DEFAULT;
672 		has_comp = crypto_has_acomp(zswap_compressor, 0, 0);
673 	}
674 	if (!has_comp) {
675 		pr_err("default compressor %s not available\n",
676 		       zswap_compressor);
677 		param_free_charp(&zswap_compressor);
678 		zswap_compressor = ZSWAP_PARAM_UNSET;
679 	}
680 
681 	has_zpool = zpool_has_pool(zswap_zpool_type);
682 	if (!has_zpool && strcmp(zswap_zpool_type,
683 				 CONFIG_ZSWAP_ZPOOL_DEFAULT)) {
684 		pr_err("zpool %s not available, using default %s\n",
685 		       zswap_zpool_type, CONFIG_ZSWAP_ZPOOL_DEFAULT);
686 		param_free_charp(&zswap_zpool_type);
687 		zswap_zpool_type = CONFIG_ZSWAP_ZPOOL_DEFAULT;
688 		has_zpool = zpool_has_pool(zswap_zpool_type);
689 	}
690 	if (!has_zpool) {
691 		pr_err("default zpool %s not available\n",
692 		       zswap_zpool_type);
693 		param_free_charp(&zswap_zpool_type);
694 		zswap_zpool_type = ZSWAP_PARAM_UNSET;
695 	}
696 
697 	if (!has_comp || !has_zpool)
698 		return NULL;
699 
700 	return zswap_pool_create(zswap_zpool_type, zswap_compressor);
701 }
702 
703 static void zswap_pool_destroy(struct zswap_pool *pool)
704 {
705 	zswap_pool_debug("destroying", pool);
706 
707 	cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node);
708 	free_percpu(pool->acomp_ctx);
709 	zpool_destroy_pool(pool->zpool);
710 	kfree(pool);
711 }
712 
713 static int __must_check zswap_pool_get(struct zswap_pool *pool)
714 {
715 	if (!pool)
716 		return 0;
717 
718 	return kref_get_unless_zero(&pool->kref);
719 }
720 
721 static void __zswap_pool_release(struct work_struct *work)
722 {
723 	struct zswap_pool *pool = container_of(work, typeof(*pool),
724 						release_work);
725 
726 	synchronize_rcu();
727 
728 	/* nobody should have been able to get a kref... */
729 	WARN_ON(kref_get_unless_zero(&pool->kref));
730 
731 	/* pool is now off zswap_pools list and has no references. */
732 	zswap_pool_destroy(pool);
733 }
734 
735 static void __zswap_pool_empty(struct kref *kref)
736 {
737 	struct zswap_pool *pool;
738 
739 	pool = container_of(kref, typeof(*pool), kref);
740 
741 	spin_lock(&zswap_pools_lock);
742 
743 	WARN_ON(pool == zswap_pool_current());
744 
745 	list_del_rcu(&pool->list);
746 
747 	INIT_WORK(&pool->release_work, __zswap_pool_release);
748 	schedule_work(&pool->release_work);
749 
750 	spin_unlock(&zswap_pools_lock);
751 }
752 
753 static void zswap_pool_put(struct zswap_pool *pool)
754 {
755 	kref_put(&pool->kref, __zswap_pool_empty);
756 }
757 
758 /*********************************
759 * param callbacks
760 **********************************/
761 
762 /* val must be a null-terminated string */
763 static int __zswap_param_set(const char *val, const struct kernel_param *kp,
764 			     char *type, char *compressor)
765 {
766 	struct zswap_pool *pool, *put_pool = NULL;
767 	char *s = strstrip((char *)val);
768 	int ret;
769 
770 	if (zswap_init_failed) {
771 		pr_err("can't set param, initialization failed\n");
772 		return -ENODEV;
773 	}
774 
775 	/* no change required */
776 	if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool)
777 		return 0;
778 
779 	/* if this is load-time (pre-init) param setting,
780 	 * don't create a pool; that's done during init.
781 	 */
782 	if (!zswap_init_started)
783 		return param_set_charp(s, kp);
784 
785 	if (!type) {
786 		if (!zpool_has_pool(s)) {
787 			pr_err("zpool %s not available\n", s);
788 			return -ENOENT;
789 		}
790 		type = s;
791 	} else if (!compressor) {
792 		if (!crypto_has_acomp(s, 0, 0)) {
793 			pr_err("compressor %s not available\n", s);
794 			return -ENOENT;
795 		}
796 		compressor = s;
797 	} else {
798 		WARN_ON(1);
799 		return -EINVAL;
800 	}
801 
802 	spin_lock(&zswap_pools_lock);
803 
804 	pool = zswap_pool_find_get(type, compressor);
805 	if (pool) {
806 		zswap_pool_debug("using existing", pool);
807 		WARN_ON(pool == zswap_pool_current());
808 		list_del_rcu(&pool->list);
809 	}
810 
811 	spin_unlock(&zswap_pools_lock);
812 
813 	if (!pool)
814 		pool = zswap_pool_create(type, compressor);
815 
816 	if (pool)
817 		ret = param_set_charp(s, kp);
818 	else
819 		ret = -EINVAL;
820 
821 	spin_lock(&zswap_pools_lock);
822 
823 	if (!ret) {
824 		put_pool = zswap_pool_current();
825 		list_add_rcu(&pool->list, &zswap_pools);
826 		zswap_has_pool = true;
827 	} else if (pool) {
828 		/* add the possibly pre-existing pool to the end of the pools
829 		 * list; if it's new (and empty) then it'll be removed and
830 		 * destroyed by the put after we drop the lock
831 		 */
832 		list_add_tail_rcu(&pool->list, &zswap_pools);
833 		put_pool = pool;
834 	}
835 
836 	spin_unlock(&zswap_pools_lock);
837 
838 	if (!zswap_has_pool && !pool) {
839 		/* if initial pool creation failed, and this pool creation also
840 		 * failed, maybe both compressor and zpool params were bad.
841 		 * Allow changing this param, so pool creation will succeed
842 		 * when the other param is changed. We already verified this
843 		 * param is ok in the zpool_has_pool() or crypto_has_acomp()
844 		 * checks above.
845 		 */
846 		ret = param_set_charp(s, kp);
847 	}
848 
849 	/* drop the ref from either the old current pool,
850 	 * or the new pool we failed to add
851 	 */
852 	if (put_pool)
853 		zswap_pool_put(put_pool);
854 
855 	return ret;
856 }
857 
858 static int zswap_compressor_param_set(const char *val,
859 				      const struct kernel_param *kp)
860 {
861 	return __zswap_param_set(val, kp, zswap_zpool_type, NULL);
862 }
863 
864 static int zswap_zpool_param_set(const char *val,
865 				 const struct kernel_param *kp)
866 {
867 	return __zswap_param_set(val, kp, NULL, zswap_compressor);
868 }
869 
870 static int zswap_enabled_param_set(const char *val,
871 				   const struct kernel_param *kp)
872 {
873 	if (zswap_init_failed) {
874 		pr_err("can't enable, initialization failed\n");
875 		return -ENODEV;
876 	}
877 	if (!zswap_has_pool && zswap_init_started) {
878 		pr_err("can't enable, no pool configured\n");
879 		return -ENODEV;
880 	}
881 
882 	return param_set_bool(val, kp);
883 }
884 
885 /*********************************
886 * writeback code
887 **********************************/
888 /* return enum for zswap_get_swap_cache_page */
889 enum zswap_get_swap_ret {
890 	ZSWAP_SWAPCACHE_NEW,
891 	ZSWAP_SWAPCACHE_EXIST,
892 	ZSWAP_SWAPCACHE_FAIL,
893 };
894 
895 /*
896  * zswap_get_swap_cache_page
897  *
898  * This is an adaption of read_swap_cache_async()
899  *
900  * This function tries to find a page with the given swap entry
901  * in the swapper_space address space (the swap cache).  If the page
902  * is found, it is returned in retpage.  Otherwise, a page is allocated,
903  * added to the swap cache, and returned in retpage.
904  *
905  * If success, the swap cache page is returned in retpage
906  * Returns ZSWAP_SWAPCACHE_EXIST if page was already in the swap cache
907  * Returns ZSWAP_SWAPCACHE_NEW if the new page needs to be populated,
908  *     the new page is added to swapcache and locked
909  * Returns ZSWAP_SWAPCACHE_FAIL on error
910  */
911 static int zswap_get_swap_cache_page(swp_entry_t entry,
912 				struct page **retpage)
913 {
914 	bool page_was_allocated;
915 
916 	*retpage = __read_swap_cache_async(entry, GFP_KERNEL,
917 			NULL, 0, &page_was_allocated);
918 	if (page_was_allocated)
919 		return ZSWAP_SWAPCACHE_NEW;
920 	if (!*retpage)
921 		return ZSWAP_SWAPCACHE_FAIL;
922 	return ZSWAP_SWAPCACHE_EXIST;
923 }
924 
925 /*
926  * Attempts to free an entry by adding a page to the swap cache,
927  * decompressing the entry data into the page, and issuing a
928  * bio write to write the page back to the swap device.
929  *
930  * This can be thought of as a "resumed writeback" of the page
931  * to the swap device.  We are basically resuming the same swap
932  * writeback path that was intercepted with the frontswap_store()
933  * in the first place.  After the page has been decompressed into
934  * the swap cache, the compressed version stored by zswap can be
935  * freed.
936  */
937 static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
938 {
939 	struct zswap_header *zhdr;
940 	swp_entry_t swpentry;
941 	struct zswap_tree *tree;
942 	pgoff_t offset;
943 	struct zswap_entry *entry;
944 	struct page *page;
945 	struct scatterlist input, output;
946 	struct crypto_acomp_ctx *acomp_ctx;
947 
948 	u8 *src, *tmp = NULL;
949 	unsigned int dlen;
950 	int ret;
951 	struct writeback_control wbc = {
952 		.sync_mode = WB_SYNC_NONE,
953 	};
954 
955 	if (!zpool_can_sleep_mapped(pool)) {
956 		tmp = kmalloc(PAGE_SIZE, GFP_ATOMIC);
957 		if (!tmp)
958 			return -ENOMEM;
959 	}
960 
961 	/* extract swpentry from data */
962 	zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO);
963 	swpentry = zhdr->swpentry; /* here */
964 	tree = zswap_trees[swp_type(swpentry)];
965 	offset = swp_offset(swpentry);
966 
967 	/* find and ref zswap entry */
968 	spin_lock(&tree->lock);
969 	entry = zswap_entry_find_get(&tree->rbroot, offset);
970 	if (!entry) {
971 		/* entry was invalidated */
972 		spin_unlock(&tree->lock);
973 		zpool_unmap_handle(pool, handle);
974 		kfree(tmp);
975 		return 0;
976 	}
977 	spin_unlock(&tree->lock);
978 	BUG_ON(offset != entry->offset);
979 
980 	src = (u8 *)zhdr + sizeof(struct zswap_header);
981 	if (!zpool_can_sleep_mapped(pool)) {
982 		memcpy(tmp, src, entry->length);
983 		src = tmp;
984 		zpool_unmap_handle(pool, handle);
985 	}
986 
987 	/* try to allocate swap cache page */
988 	switch (zswap_get_swap_cache_page(swpentry, &page)) {
989 	case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */
990 		ret = -ENOMEM;
991 		goto fail;
992 
993 	case ZSWAP_SWAPCACHE_EXIST:
994 		/* page is already in the swap cache, ignore for now */
995 		put_page(page);
996 		ret = -EEXIST;
997 		goto fail;
998 
999 	case ZSWAP_SWAPCACHE_NEW: /* page is locked */
1000 		/* decompress */
1001 		acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
1002 		dlen = PAGE_SIZE;
1003 
1004 		mutex_lock(acomp_ctx->mutex);
1005 		sg_init_one(&input, src, entry->length);
1006 		sg_init_table(&output, 1);
1007 		sg_set_page(&output, page, PAGE_SIZE, 0);
1008 		acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, dlen);
1009 		ret = crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait);
1010 		dlen = acomp_ctx->req->dlen;
1011 		mutex_unlock(acomp_ctx->mutex);
1012 
1013 		BUG_ON(ret);
1014 		BUG_ON(dlen != PAGE_SIZE);
1015 
1016 		/* page is up to date */
1017 		SetPageUptodate(page);
1018 	}
1019 
1020 	/* move it to the tail of the inactive list after end_writeback */
1021 	SetPageReclaim(page);
1022 
1023 	/* start writeback */
1024 	__swap_writepage(page, &wbc, end_swap_bio_write);
1025 	put_page(page);
1026 	zswap_written_back_pages++;
1027 
1028 	spin_lock(&tree->lock);
1029 	/* drop local reference */
1030 	zswap_entry_put(tree, entry);
1031 
1032 	/*
1033 	* There are two possible situations for entry here:
1034 	* (1) refcount is 1(normal case),  entry is valid and on the tree
1035 	* (2) refcount is 0, entry is freed and not on the tree
1036 	*     because invalidate happened during writeback
1037 	*  search the tree and free the entry if find entry
1038 	*/
1039 	if (entry == zswap_rb_search(&tree->rbroot, offset))
1040 		zswap_entry_put(tree, entry);
1041 	spin_unlock(&tree->lock);
1042 
1043 	goto end;
1044 
1045 	/*
1046 	* if we get here due to ZSWAP_SWAPCACHE_EXIST
1047 	* a load may be happening concurrently.
1048 	* it is safe and okay to not free the entry.
1049 	* if we free the entry in the following put
1050 	* it is also okay to return !0
1051 	*/
1052 fail:
1053 	spin_lock(&tree->lock);
1054 	zswap_entry_put(tree, entry);
1055 	spin_unlock(&tree->lock);
1056 
1057 end:
1058 	if (zpool_can_sleep_mapped(pool))
1059 		zpool_unmap_handle(pool, handle);
1060 	else
1061 		kfree(tmp);
1062 
1063 	return ret;
1064 }
1065 
1066 static int zswap_is_page_same_filled(void *ptr, unsigned long *value)
1067 {
1068 	unsigned int pos;
1069 	unsigned long *page;
1070 
1071 	page = (unsigned long *)ptr;
1072 	for (pos = 1; pos < PAGE_SIZE / sizeof(*page); pos++) {
1073 		if (page[pos] != page[0])
1074 			return 0;
1075 	}
1076 	*value = page[0];
1077 	return 1;
1078 }
1079 
1080 static void zswap_fill_page(void *ptr, unsigned long value)
1081 {
1082 	unsigned long *page;
1083 
1084 	page = (unsigned long *)ptr;
1085 	memset_l(page, value, PAGE_SIZE / sizeof(unsigned long));
1086 }
1087 
1088 /*********************************
1089 * frontswap hooks
1090 **********************************/
1091 /* attempts to compress and store an single page */
1092 static int zswap_frontswap_store(unsigned type, pgoff_t offset,
1093 				struct page *page)
1094 {
1095 	struct zswap_tree *tree = zswap_trees[type];
1096 	struct zswap_entry *entry, *dupentry;
1097 	struct scatterlist input, output;
1098 	struct crypto_acomp_ctx *acomp_ctx;
1099 	int ret;
1100 	unsigned int hlen, dlen = PAGE_SIZE;
1101 	unsigned long handle, value;
1102 	char *buf;
1103 	u8 *src, *dst;
1104 	struct zswap_header zhdr = { .swpentry = swp_entry(type, offset) };
1105 	gfp_t gfp;
1106 
1107 	/* THP isn't supported */
1108 	if (PageTransHuge(page)) {
1109 		ret = -EINVAL;
1110 		goto reject;
1111 	}
1112 
1113 	if (!zswap_enabled || !tree) {
1114 		ret = -ENODEV;
1115 		goto reject;
1116 	}
1117 
1118 	/* reclaim space if needed */
1119 	if (zswap_is_full()) {
1120 		struct zswap_pool *pool;
1121 
1122 		zswap_pool_limit_hit++;
1123 		zswap_pool_reached_full = true;
1124 		pool = zswap_pool_last_get();
1125 		if (pool)
1126 			queue_work(shrink_wq, &pool->shrink_work);
1127 		ret = -ENOMEM;
1128 		goto reject;
1129 	}
1130 
1131 	if (zswap_pool_reached_full) {
1132 	       if (!zswap_can_accept()) {
1133 			ret = -ENOMEM;
1134 			goto reject;
1135 		} else
1136 			zswap_pool_reached_full = false;
1137 	}
1138 
1139 	/* allocate entry */
1140 	entry = zswap_entry_cache_alloc(GFP_KERNEL);
1141 	if (!entry) {
1142 		zswap_reject_kmemcache_fail++;
1143 		ret = -ENOMEM;
1144 		goto reject;
1145 	}
1146 
1147 	if (zswap_same_filled_pages_enabled) {
1148 		src = kmap_atomic(page);
1149 		if (zswap_is_page_same_filled(src, &value)) {
1150 			kunmap_atomic(src);
1151 			entry->offset = offset;
1152 			entry->length = 0;
1153 			entry->value = value;
1154 			atomic_inc(&zswap_same_filled_pages);
1155 			goto insert_entry;
1156 		}
1157 		kunmap_atomic(src);
1158 	}
1159 
1160 	if (!zswap_non_same_filled_pages_enabled) {
1161 		ret = -EINVAL;
1162 		goto freepage;
1163 	}
1164 
1165 	/* if entry is successfully added, it keeps the reference */
1166 	entry->pool = zswap_pool_current_get();
1167 	if (!entry->pool) {
1168 		ret = -EINVAL;
1169 		goto freepage;
1170 	}
1171 
1172 	/* compress */
1173 	acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
1174 
1175 	mutex_lock(acomp_ctx->mutex);
1176 
1177 	dst = acomp_ctx->dstmem;
1178 	sg_init_table(&input, 1);
1179 	sg_set_page(&input, page, PAGE_SIZE, 0);
1180 
1181 	/* zswap_dstmem is of size (PAGE_SIZE * 2). Reflect same in sg_list */
1182 	sg_init_one(&output, dst, PAGE_SIZE * 2);
1183 	acomp_request_set_params(acomp_ctx->req, &input, &output, PAGE_SIZE, dlen);
1184 	/*
1185 	 * it maybe looks a little bit silly that we send an asynchronous request,
1186 	 * then wait for its completion synchronously. This makes the process look
1187 	 * synchronous in fact.
1188 	 * Theoretically, acomp supports users send multiple acomp requests in one
1189 	 * acomp instance, then get those requests done simultaneously. but in this
1190 	 * case, frontswap actually does store and load page by page, there is no
1191 	 * existing method to send the second page before the first page is done
1192 	 * in one thread doing frontswap.
1193 	 * but in different threads running on different cpu, we have different
1194 	 * acomp instance, so multiple threads can do (de)compression in parallel.
1195 	 */
1196 	ret = crypto_wait_req(crypto_acomp_compress(acomp_ctx->req), &acomp_ctx->wait);
1197 	dlen = acomp_ctx->req->dlen;
1198 
1199 	if (ret) {
1200 		ret = -EINVAL;
1201 		goto put_dstmem;
1202 	}
1203 
1204 	/* store */
1205 	hlen = zpool_evictable(entry->pool->zpool) ? sizeof(zhdr) : 0;
1206 	gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;
1207 	if (zpool_malloc_support_movable(entry->pool->zpool))
1208 		gfp |= __GFP_HIGHMEM | __GFP_MOVABLE;
1209 	ret = zpool_malloc(entry->pool->zpool, hlen + dlen, gfp, &handle);
1210 	if (ret == -ENOSPC) {
1211 		zswap_reject_compress_poor++;
1212 		goto put_dstmem;
1213 	}
1214 	if (ret) {
1215 		zswap_reject_alloc_fail++;
1216 		goto put_dstmem;
1217 	}
1218 	buf = zpool_map_handle(entry->pool->zpool, handle, ZPOOL_MM_WO);
1219 	memcpy(buf, &zhdr, hlen);
1220 	memcpy(buf + hlen, dst, dlen);
1221 	zpool_unmap_handle(entry->pool->zpool, handle);
1222 	mutex_unlock(acomp_ctx->mutex);
1223 
1224 	/* populate entry */
1225 	entry->offset = offset;
1226 	entry->handle = handle;
1227 	entry->length = dlen;
1228 
1229 insert_entry:
1230 	/* map */
1231 	spin_lock(&tree->lock);
1232 	do {
1233 		ret = zswap_rb_insert(&tree->rbroot, entry, &dupentry);
1234 		if (ret == -EEXIST) {
1235 			zswap_duplicate_entry++;
1236 			/* remove from rbtree */
1237 			zswap_rb_erase(&tree->rbroot, dupentry);
1238 			zswap_entry_put(tree, dupentry);
1239 		}
1240 	} while (ret == -EEXIST);
1241 	spin_unlock(&tree->lock);
1242 
1243 	/* update stats */
1244 	atomic_inc(&zswap_stored_pages);
1245 	zswap_update_total_size();
1246 
1247 	return 0;
1248 
1249 put_dstmem:
1250 	mutex_unlock(acomp_ctx->mutex);
1251 	zswap_pool_put(entry->pool);
1252 freepage:
1253 	zswap_entry_cache_free(entry);
1254 reject:
1255 	return ret;
1256 }
1257 
1258 /*
1259  * returns 0 if the page was successfully decompressed
1260  * return -1 on entry not found or error
1261 */
1262 static int zswap_frontswap_load(unsigned type, pgoff_t offset,
1263 				struct page *page)
1264 {
1265 	struct zswap_tree *tree = zswap_trees[type];
1266 	struct zswap_entry *entry;
1267 	struct scatterlist input, output;
1268 	struct crypto_acomp_ctx *acomp_ctx;
1269 	u8 *src, *dst, *tmp;
1270 	unsigned int dlen;
1271 	int ret;
1272 
1273 	/* find */
1274 	spin_lock(&tree->lock);
1275 	entry = zswap_entry_find_get(&tree->rbroot, offset);
1276 	if (!entry) {
1277 		/* entry was written back */
1278 		spin_unlock(&tree->lock);
1279 		return -1;
1280 	}
1281 	spin_unlock(&tree->lock);
1282 
1283 	if (!entry->length) {
1284 		dst = kmap_atomic(page);
1285 		zswap_fill_page(dst, entry->value);
1286 		kunmap_atomic(dst);
1287 		ret = 0;
1288 		goto freeentry;
1289 	}
1290 
1291 	if (!zpool_can_sleep_mapped(entry->pool->zpool)) {
1292 
1293 		tmp = kmalloc(entry->length, GFP_ATOMIC);
1294 		if (!tmp) {
1295 			ret = -ENOMEM;
1296 			goto freeentry;
1297 		}
1298 	}
1299 
1300 	/* decompress */
1301 	dlen = PAGE_SIZE;
1302 	src = zpool_map_handle(entry->pool->zpool, entry->handle, ZPOOL_MM_RO);
1303 	if (zpool_evictable(entry->pool->zpool))
1304 		src += sizeof(struct zswap_header);
1305 
1306 	if (!zpool_can_sleep_mapped(entry->pool->zpool)) {
1307 
1308 		memcpy(tmp, src, entry->length);
1309 		src = tmp;
1310 
1311 		zpool_unmap_handle(entry->pool->zpool, entry->handle);
1312 	}
1313 
1314 	acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
1315 	mutex_lock(acomp_ctx->mutex);
1316 	sg_init_one(&input, src, entry->length);
1317 	sg_init_table(&output, 1);
1318 	sg_set_page(&output, page, PAGE_SIZE, 0);
1319 	acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, dlen);
1320 	ret = crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait);
1321 	mutex_unlock(acomp_ctx->mutex);
1322 
1323 	if (zpool_can_sleep_mapped(entry->pool->zpool))
1324 		zpool_unmap_handle(entry->pool->zpool, entry->handle);
1325 	else
1326 		kfree(tmp);
1327 
1328 	BUG_ON(ret);
1329 
1330 freeentry:
1331 	spin_lock(&tree->lock);
1332 	zswap_entry_put(tree, entry);
1333 	spin_unlock(&tree->lock);
1334 
1335 	return ret;
1336 }
1337 
1338 /* frees an entry in zswap */
1339 static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset)
1340 {
1341 	struct zswap_tree *tree = zswap_trees[type];
1342 	struct zswap_entry *entry;
1343 
1344 	/* find */
1345 	spin_lock(&tree->lock);
1346 	entry = zswap_rb_search(&tree->rbroot, offset);
1347 	if (!entry) {
1348 		/* entry was written back */
1349 		spin_unlock(&tree->lock);
1350 		return;
1351 	}
1352 
1353 	/* remove from rbtree */
1354 	zswap_rb_erase(&tree->rbroot, entry);
1355 
1356 	/* drop the initial reference from entry creation */
1357 	zswap_entry_put(tree, entry);
1358 
1359 	spin_unlock(&tree->lock);
1360 }
1361 
1362 /* frees all zswap entries for the given swap type */
1363 static void zswap_frontswap_invalidate_area(unsigned type)
1364 {
1365 	struct zswap_tree *tree = zswap_trees[type];
1366 	struct zswap_entry *entry, *n;
1367 
1368 	if (!tree)
1369 		return;
1370 
1371 	/* walk the tree and free everything */
1372 	spin_lock(&tree->lock);
1373 	rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode)
1374 		zswap_free_entry(entry);
1375 	tree->rbroot = RB_ROOT;
1376 	spin_unlock(&tree->lock);
1377 	kfree(tree);
1378 	zswap_trees[type] = NULL;
1379 }
1380 
1381 static void zswap_frontswap_init(unsigned type)
1382 {
1383 	struct zswap_tree *tree;
1384 
1385 	tree = kzalloc(sizeof(*tree), GFP_KERNEL);
1386 	if (!tree) {
1387 		pr_err("alloc failed, zswap disabled for swap type %d\n", type);
1388 		return;
1389 	}
1390 
1391 	tree->rbroot = RB_ROOT;
1392 	spin_lock_init(&tree->lock);
1393 	zswap_trees[type] = tree;
1394 }
1395 
1396 static const struct frontswap_ops zswap_frontswap_ops = {
1397 	.store = zswap_frontswap_store,
1398 	.load = zswap_frontswap_load,
1399 	.invalidate_page = zswap_frontswap_invalidate_page,
1400 	.invalidate_area = zswap_frontswap_invalidate_area,
1401 	.init = zswap_frontswap_init
1402 };
1403 
1404 /*********************************
1405 * debugfs functions
1406 **********************************/
1407 #ifdef CONFIG_DEBUG_FS
1408 #include <linux/debugfs.h>
1409 
1410 static struct dentry *zswap_debugfs_root;
1411 
1412 static int __init zswap_debugfs_init(void)
1413 {
1414 	if (!debugfs_initialized())
1415 		return -ENODEV;
1416 
1417 	zswap_debugfs_root = debugfs_create_dir("zswap", NULL);
1418 
1419 	debugfs_create_u64("pool_limit_hit", 0444,
1420 			   zswap_debugfs_root, &zswap_pool_limit_hit);
1421 	debugfs_create_u64("reject_reclaim_fail", 0444,
1422 			   zswap_debugfs_root, &zswap_reject_reclaim_fail);
1423 	debugfs_create_u64("reject_alloc_fail", 0444,
1424 			   zswap_debugfs_root, &zswap_reject_alloc_fail);
1425 	debugfs_create_u64("reject_kmemcache_fail", 0444,
1426 			   zswap_debugfs_root, &zswap_reject_kmemcache_fail);
1427 	debugfs_create_u64("reject_compress_poor", 0444,
1428 			   zswap_debugfs_root, &zswap_reject_compress_poor);
1429 	debugfs_create_u64("written_back_pages", 0444,
1430 			   zswap_debugfs_root, &zswap_written_back_pages);
1431 	debugfs_create_u64("duplicate_entry", 0444,
1432 			   zswap_debugfs_root, &zswap_duplicate_entry);
1433 	debugfs_create_u64("pool_total_size", 0444,
1434 			   zswap_debugfs_root, &zswap_pool_total_size);
1435 	debugfs_create_atomic_t("stored_pages", 0444,
1436 				zswap_debugfs_root, &zswap_stored_pages);
1437 	debugfs_create_atomic_t("same_filled_pages", 0444,
1438 				zswap_debugfs_root, &zswap_same_filled_pages);
1439 
1440 	return 0;
1441 }
1442 #else
1443 static int __init zswap_debugfs_init(void)
1444 {
1445 	return 0;
1446 }
1447 #endif
1448 
1449 /*********************************
1450 * module init and exit
1451 **********************************/
1452 static int __init init_zswap(void)
1453 {
1454 	struct zswap_pool *pool;
1455 	int ret;
1456 
1457 	zswap_init_started = true;
1458 
1459 	if (zswap_entry_cache_create()) {
1460 		pr_err("entry cache creation failed\n");
1461 		goto cache_fail;
1462 	}
1463 
1464 	ret = cpuhp_setup_state(CPUHP_MM_ZSWP_MEM_PREPARE, "mm/zswap:prepare",
1465 				zswap_dstmem_prepare, zswap_dstmem_dead);
1466 	if (ret) {
1467 		pr_err("dstmem alloc failed\n");
1468 		goto dstmem_fail;
1469 	}
1470 
1471 	ret = cpuhp_setup_state_multi(CPUHP_MM_ZSWP_POOL_PREPARE,
1472 				      "mm/zswap_pool:prepare",
1473 				      zswap_cpu_comp_prepare,
1474 				      zswap_cpu_comp_dead);
1475 	if (ret)
1476 		goto hp_fail;
1477 
1478 	pool = __zswap_pool_create_fallback();
1479 	if (pool) {
1480 		pr_info("loaded using pool %s/%s\n", pool->tfm_name,
1481 			zpool_get_type(pool->zpool));
1482 		list_add(&pool->list, &zswap_pools);
1483 		zswap_has_pool = true;
1484 	} else {
1485 		pr_err("pool creation failed\n");
1486 		zswap_enabled = false;
1487 	}
1488 
1489 	shrink_wq = create_workqueue("zswap-shrink");
1490 	if (!shrink_wq)
1491 		goto fallback_fail;
1492 
1493 	ret = frontswap_register_ops(&zswap_frontswap_ops);
1494 	if (ret)
1495 		goto destroy_wq;
1496 	if (zswap_debugfs_init())
1497 		pr_warn("debugfs initialization failed\n");
1498 	return 0;
1499 
1500 destroy_wq:
1501 	destroy_workqueue(shrink_wq);
1502 fallback_fail:
1503 	if (pool)
1504 		zswap_pool_destroy(pool);
1505 hp_fail:
1506 	cpuhp_remove_state(CPUHP_MM_ZSWP_MEM_PREPARE);
1507 dstmem_fail:
1508 	zswap_entry_cache_destroy();
1509 cache_fail:
1510 	/* if built-in, we aren't unloaded on failure; don't allow use */
1511 	zswap_init_failed = true;
1512 	zswap_enabled = false;
1513 	return -ENOMEM;
1514 }
1515 /* must be late so crypto has time to come up */
1516 late_initcall(init_zswap);
1517 
1518 MODULE_LICENSE("GPL");
1519 MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
1520 MODULE_DESCRIPTION("Compressed cache for swap pages");
1521