xref: /openbmc/linux/mm/zswap.c (revision d63670d2)
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 /*********************************
40 * statistics
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);
48 
49 /*
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.
54 */
55 
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;
70 
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;
75 
76 /*********************************
77 * tunables
78 **********************************/
79 
80 #define ZSWAP_PARAM_UNSET ""
81 
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,
89 };
90 module_param_cb(enabled, &zswap_enabled_param_ops, &zswap_enabled, 0644);
91 
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,
100 };
101 module_param_cb(compressor, &zswap_compressor_param_ops,
102 		&zswap_compressor, 0644);
103 
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,
111 };
112 module_param_cb(zpool, &zswap_zpool_param_ops, &zswap_zpool_type, 0644);
113 
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);
117 
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,
121 		   uint, 0644);
122 
123 /*
124  * Enable/disable handling same-value filled pages (enabled by default).
125  * If disabled every page is considered non-same-value filled.
126  */
127 static bool zswap_same_filled_pages_enabled = true;
128 module_param_named(same_filled_pages_enabled, zswap_same_filled_pages_enabled,
129 		   bool, 0644);
130 
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,
134 		   bool, 0644);
135 
136 /*********************************
137 * data structures
138 **********************************/
139 
140 struct crypto_acomp_ctx {
141 	struct crypto_acomp *acomp;
142 	struct acomp_req *req;
143 	struct crypto_wait wait;
144 	u8 *dstmem;
145 	struct mutex *mutex;
146 };
147 
148 struct zswap_pool {
149 	struct zpool *zpool;
150 	struct crypto_acomp_ctx __percpu *acomp_ctx;
151 	struct kref kref;
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];
157 };
158 
159 /*
160  * struct zswap_entry
161  *
162  * This structure contains the metadata for tracking a single compressed
163  * page within zswap.
164  *
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
178  */
179 struct zswap_entry {
180 	struct rb_node rbnode;
181 	pgoff_t offset;
182 	int refcount;
183 	unsigned int length;
184 	struct zswap_pool *pool;
185 	union {
186 		unsigned long handle;
187 		unsigned long value;
188 	};
189 };
190 
191 struct zswap_header {
192 	swp_entry_t swpentry;
193 };
194 
195 /*
196  * The tree lock in the zswap_tree struct protects a few things:
197  * - the rbtree
198  * - the refcount field of each entry in the tree
199  */
200 struct zswap_tree {
201 	struct rb_root rbroot;
202 	spinlock_t lock;
203 };
204 
205 static struct zswap_tree *zswap_trees[MAX_SWAPFILES];
206 
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);
213 
214 /* used by param callback function */
215 static bool zswap_init_started;
216 
217 /* fatal error during init */
218 static bool zswap_init_failed;
219 
220 /* init completed, but couldn't create the initial pool */
221 static bool zswap_has_pool;
222 
223 /*********************************
224 * helpers and fwd declarations
225 **********************************/
226 
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))
230 
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);
234 
235 static const struct zpool_ops zswap_zpool_ops = {
236 	.evict = zswap_writeback_entry
237 };
238 
239 static bool zswap_is_full(void)
240 {
241 	return totalram_pages() * zswap_max_pool_percent / 100 <
242 			DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
243 }
244 
245 static bool zswap_can_accept(void)
246 {
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);
250 }
251 
252 static void zswap_update_total_size(void)
253 {
254 	struct zswap_pool *pool;
255 	u64 total = 0;
256 
257 	rcu_read_lock();
258 
259 	list_for_each_entry_rcu(pool, &zswap_pools, list)
260 		total += zpool_get_total_size(pool->zpool);
261 
262 	rcu_read_unlock();
263 
264 	zswap_pool_total_size = total;
265 }
266 
267 /*********************************
268 * zswap entry functions
269 **********************************/
270 static struct kmem_cache *zswap_entry_cache;
271 
272 static int __init zswap_entry_cache_create(void)
273 {
274 	zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
275 	return zswap_entry_cache == NULL;
276 }
277 
278 static void __init zswap_entry_cache_destroy(void)
279 {
280 	kmem_cache_destroy(zswap_entry_cache);
281 }
282 
283 static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp)
284 {
285 	struct zswap_entry *entry;
286 	entry = kmem_cache_alloc(zswap_entry_cache, gfp);
287 	if (!entry)
288 		return NULL;
289 	entry->refcount = 1;
290 	RB_CLEAR_NODE(&entry->rbnode);
291 	return entry;
292 }
293 
294 static void zswap_entry_cache_free(struct zswap_entry *entry)
295 {
296 	kmem_cache_free(zswap_entry_cache, entry);
297 }
298 
299 /*********************************
300 * rbtree functions
301 **********************************/
302 static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset)
303 {
304 	struct rb_node *node = root->rb_node;
305 	struct zswap_entry *entry;
306 
307 	while (node) {
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;
313 		else
314 			return entry;
315 	}
316 	return NULL;
317 }
318 
319 /*
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
322  */
323 static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry,
324 			struct zswap_entry **dupentry)
325 {
326 	struct rb_node **link = &root->rb_node, *parent = NULL;
327 	struct zswap_entry *myentry;
328 
329 	while (*link) {
330 		parent = *link;
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;
336 		else {
337 			*dupentry = myentry;
338 			return -EEXIST;
339 		}
340 	}
341 	rb_link_node(&entry->rbnode, parent, link);
342 	rb_insert_color(&entry->rbnode, root);
343 	return 0;
344 }
345 
346 static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry)
347 {
348 	if (!RB_EMPTY_NODE(&entry->rbnode)) {
349 		rb_erase(&entry->rbnode, root);
350 		RB_CLEAR_NODE(&entry->rbnode);
351 	}
352 }
353 
354 /*
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.
357  */
358 static void zswap_free_entry(struct zswap_entry *entry)
359 {
360 	if (!entry->length)
361 		atomic_dec(&zswap_same_filled_pages);
362 	else {
363 		zpool_free(entry->pool->zpool, entry->handle);
364 		zswap_pool_put(entry->pool);
365 	}
366 	zswap_entry_cache_free(entry);
367 	atomic_dec(&zswap_stored_pages);
368 	zswap_update_total_size();
369 }
370 
371 /* caller must hold the tree lock */
372 static void zswap_entry_get(struct zswap_entry *entry)
373 {
374 	entry->refcount++;
375 }
376 
377 /* caller must hold the tree lock
378 * remove from the tree and free it, if nobody reference the entry
379 */
380 static void zswap_entry_put(struct zswap_tree *tree,
381 			struct zswap_entry *entry)
382 {
383 	int refcount = --entry->refcount;
384 
385 	BUG_ON(refcount < 0);
386 	if (refcount == 0) {
387 		zswap_rb_erase(&tree->rbroot, entry);
388 		zswap_free_entry(entry);
389 	}
390 }
391 
392 /* caller must hold the tree lock */
393 static struct zswap_entry *zswap_entry_find_get(struct rb_root *root,
394 				pgoff_t offset)
395 {
396 	struct zswap_entry *entry;
397 
398 	entry = zswap_rb_search(root, offset);
399 	if (entry)
400 		zswap_entry_get(entry);
401 
402 	return entry;
403 }
404 
405 /*********************************
406 * per-cpu code
407 **********************************/
408 static DEFINE_PER_CPU(u8 *, zswap_dstmem);
409 /*
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.
413  */
414 static DEFINE_PER_CPU(struct mutex *, zswap_mutex);
415 
416 static int zswap_dstmem_prepare(unsigned int cpu)
417 {
418 	struct mutex *mutex;
419 	u8 *dst;
420 
421 	dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
422 	if (!dst)
423 		return -ENOMEM;
424 
425 	mutex = kmalloc_node(sizeof(*mutex), GFP_KERNEL, cpu_to_node(cpu));
426 	if (!mutex) {
427 		kfree(dst);
428 		return -ENOMEM;
429 	}
430 
431 	mutex_init(mutex);
432 	per_cpu(zswap_dstmem, cpu) = dst;
433 	per_cpu(zswap_mutex, cpu) = mutex;
434 	return 0;
435 }
436 
437 static int zswap_dstmem_dead(unsigned int cpu)
438 {
439 	struct mutex *mutex;
440 	u8 *dst;
441 
442 	mutex = per_cpu(zswap_mutex, cpu);
443 	kfree(mutex);
444 	per_cpu(zswap_mutex, cpu) = NULL;
445 
446 	dst = per_cpu(zswap_dstmem, cpu);
447 	kfree(dst);
448 	per_cpu(zswap_dstmem, cpu) = NULL;
449 
450 	return 0;
451 }
452 
453 static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
454 {
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;
459 
460 	acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, cpu_to_node(cpu));
461 	if (IS_ERR(acomp)) {
462 		pr_err("could not alloc crypto acomp %s : %ld\n",
463 				pool->tfm_name, PTR_ERR(acomp));
464 		return PTR_ERR(acomp);
465 	}
466 	acomp_ctx->acomp = acomp;
467 
468 	req = acomp_request_alloc(acomp_ctx->acomp);
469 	if (!req) {
470 		pr_err("could not alloc crypto acomp_request %s\n",
471 		       pool->tfm_name);
472 		crypto_free_acomp(acomp_ctx->acomp);
473 		return -ENOMEM;
474 	}
475 	acomp_ctx->req = req;
476 
477 	crypto_init_wait(&acomp_ctx->wait);
478 	/*
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.
482 	 */
483 	acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
484 				   crypto_req_done, &acomp_ctx->wait);
485 
486 	acomp_ctx->mutex = per_cpu(zswap_mutex, cpu);
487 	acomp_ctx->dstmem = per_cpu(zswap_dstmem, cpu);
488 
489 	return 0;
490 }
491 
492 static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)
493 {
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 
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);
502 	}
503 
504 	return 0;
505 }
506 
507 /*********************************
508 * pool functions
509 **********************************/
510 
511 static struct zswap_pool *__zswap_pool_current(void)
512 {
513 	struct zswap_pool *pool;
514 
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__);
518 
519 	return pool;
520 }
521 
522 static struct zswap_pool *zswap_pool_current(void)
523 {
524 	assert_spin_locked(&zswap_pools_lock);
525 
526 	return __zswap_pool_current();
527 }
528 
529 static struct zswap_pool *zswap_pool_current_get(void)
530 {
531 	struct zswap_pool *pool;
532 
533 	rcu_read_lock();
534 
535 	pool = __zswap_pool_current();
536 	if (!zswap_pool_get(pool))
537 		pool = NULL;
538 
539 	rcu_read_unlock();
540 
541 	return pool;
542 }
543 
544 static struct zswap_pool *zswap_pool_last_get(void)
545 {
546 	struct zswap_pool *pool, *last = NULL;
547 
548 	rcu_read_lock();
549 
550 	list_for_each_entry_rcu(pool, &zswap_pools, list)
551 		last = pool;
552 	WARN_ONCE(!last && zswap_has_pool,
553 		  "%s: no page storage pool!\n", __func__);
554 	if (!zswap_pool_get(last))
555 		last = NULL;
556 
557 	rcu_read_unlock();
558 
559 	return last;
560 }
561 
562 /* type and compressor must be null-terminated */
563 static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor)
564 {
565 	struct zswap_pool *pool;
566 
567 	assert_spin_locked(&zswap_pools_lock);
568 
569 	list_for_each_entry_rcu(pool, &zswap_pools, list) {
570 		if (strcmp(pool->tfm_name, compressor))
571 			continue;
572 		if (strcmp(zpool_get_type(pool->zpool), type))
573 			continue;
574 		/* if we can't get it, it's about to be destroyed */
575 		if (!zswap_pool_get(pool))
576 			continue;
577 		return pool;
578 	}
579 
580 	return NULL;
581 }
582 
583 static void shrink_worker(struct work_struct *w)
584 {
585 	struct zswap_pool *pool = container_of(w, typeof(*pool),
586 						shrink_work);
587 
588 	if (zpool_shrink(pool->zpool, 1, NULL))
589 		zswap_reject_reclaim_fail++;
590 	zswap_pool_put(pool);
591 }
592 
593 static struct zswap_pool *zswap_pool_create(char *type, char *compressor)
594 {
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;
598 	int ret;
599 
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
603 		 * create a pool.
604 		 */
605 		if (!strcmp(type, ZSWAP_PARAM_UNSET))
606 			return NULL;
607 		if (!strcmp(compressor, ZSWAP_PARAM_UNSET))
608 			return NULL;
609 	}
610 
611 	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
612 	if (!pool)
613 		return NULL;
614 
615 	/* unique name for each pool specifically required by zsmalloc */
616 	snprintf(name, 38, "zswap%x", atomic_inc_return(&zswap_pools_count));
617 
618 	pool->zpool = zpool_create_pool(type, name, gfp, &zswap_zpool_ops);
619 	if (!pool->zpool) {
620 		pr_err("%s zpool not available\n", type);
621 		goto error;
622 	}
623 	pr_debug("using %s zpool\n", zpool_get_type(pool->zpool));
624 
625 	strscpy(pool->tfm_name, compressor, sizeof(pool->tfm_name));
626 
627 	pool->acomp_ctx = alloc_percpu(*pool->acomp_ctx);
628 	if (!pool->acomp_ctx) {
629 		pr_err("percpu alloc failed\n");
630 		goto error;
631 	}
632 
633 	ret = cpuhp_state_add_instance(CPUHP_MM_ZSWP_POOL_PREPARE,
634 				       &pool->node);
635 	if (ret)
636 		goto error;
637 	pr_debug("using %s compressor\n", pool->tfm_name);
638 
639 	/* being the current pool takes 1 ref; this func expects the
640 	 * caller to always add the new pool as the current pool
641 	 */
642 	kref_init(&pool->kref);
643 	INIT_LIST_HEAD(&pool->list);
644 	INIT_WORK(&pool->shrink_work, shrink_worker);
645 
646 	zswap_pool_debug("created", pool);
647 
648 	return pool;
649 
650 error:
651 	if (pool->acomp_ctx)
652 		free_percpu(pool->acomp_ctx);
653 	if (pool->zpool)
654 		zpool_destroy_pool(pool->zpool);
655 	kfree(pool);
656 	return NULL;
657 }
658 
659 static __init struct zswap_pool *__zswap_pool_create_fallback(void)
660 {
661 	bool has_comp, has_zpool;
662 
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);
671 	}
672 	if (!has_comp) {
673 		pr_err("default compressor %s not available\n",
674 		       zswap_compressor);
675 		param_free_charp(&zswap_compressor);
676 		zswap_compressor = ZSWAP_PARAM_UNSET;
677 	}
678 
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);
687 	}
688 	if (!has_zpool) {
689 		pr_err("default zpool %s not available\n",
690 		       zswap_zpool_type);
691 		param_free_charp(&zswap_zpool_type);
692 		zswap_zpool_type = ZSWAP_PARAM_UNSET;
693 	}
694 
695 	if (!has_comp || !has_zpool)
696 		return NULL;
697 
698 	return zswap_pool_create(zswap_zpool_type, zswap_compressor);
699 }
700 
701 static void zswap_pool_destroy(struct zswap_pool *pool)
702 {
703 	zswap_pool_debug("destroying", pool);
704 
705 	cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node);
706 	free_percpu(pool->acomp_ctx);
707 	zpool_destroy_pool(pool->zpool);
708 	kfree(pool);
709 }
710 
711 static int __must_check zswap_pool_get(struct zswap_pool *pool)
712 {
713 	if (!pool)
714 		return 0;
715 
716 	return kref_get_unless_zero(&pool->kref);
717 }
718 
719 static void __zswap_pool_release(struct work_struct *work)
720 {
721 	struct zswap_pool *pool = container_of(work, typeof(*pool),
722 						release_work);
723 
724 	synchronize_rcu();
725 
726 	/* nobody should have been able to get a kref... */
727 	WARN_ON(kref_get_unless_zero(&pool->kref));
728 
729 	/* pool is now off zswap_pools list and has no references. */
730 	zswap_pool_destroy(pool);
731 }
732 
733 static void __zswap_pool_empty(struct kref *kref)
734 {
735 	struct zswap_pool *pool;
736 
737 	pool = container_of(kref, typeof(*pool), kref);
738 
739 	spin_lock(&zswap_pools_lock);
740 
741 	WARN_ON(pool == zswap_pool_current());
742 
743 	list_del_rcu(&pool->list);
744 
745 	INIT_WORK(&pool->release_work, __zswap_pool_release);
746 	schedule_work(&pool->release_work);
747 
748 	spin_unlock(&zswap_pools_lock);
749 }
750 
751 static void zswap_pool_put(struct zswap_pool *pool)
752 {
753 	kref_put(&pool->kref, __zswap_pool_empty);
754 }
755 
756 /*********************************
757 * param callbacks
758 **********************************/
759 
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)
763 {
764 	struct zswap_pool *pool, *put_pool = NULL;
765 	char *s = strstrip((char *)val);
766 	int ret;
767 
768 	if (zswap_init_failed) {
769 		pr_err("can't set param, initialization failed\n");
770 		return -ENODEV;
771 	}
772 
773 	/* no change required */
774 	if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool)
775 		return 0;
776 
777 	/* if this is load-time (pre-init) param setting,
778 	 * don't create a pool; that's done during init.
779 	 */
780 	if (!zswap_init_started)
781 		return param_set_charp(s, kp);
782 
783 	if (!type) {
784 		if (!zpool_has_pool(s)) {
785 			pr_err("zpool %s not available\n", s);
786 			return -ENOENT;
787 		}
788 		type = s;
789 	} else if (!compressor) {
790 		if (!crypto_has_acomp(s, 0, 0)) {
791 			pr_err("compressor %s not available\n", s);
792 			return -ENOENT;
793 		}
794 		compressor = s;
795 	} else {
796 		WARN_ON(1);
797 		return -EINVAL;
798 	}
799 
800 	spin_lock(&zswap_pools_lock);
801 
802 	pool = zswap_pool_find_get(type, compressor);
803 	if (pool) {
804 		zswap_pool_debug("using existing", pool);
805 		WARN_ON(pool == zswap_pool_current());
806 		list_del_rcu(&pool->list);
807 	}
808 
809 	spin_unlock(&zswap_pools_lock);
810 
811 	if (!pool)
812 		pool = zswap_pool_create(type, compressor);
813 
814 	if (pool)
815 		ret = param_set_charp(s, kp);
816 	else
817 		ret = -EINVAL;
818 
819 	spin_lock(&zswap_pools_lock);
820 
821 	if (!ret) {
822 		put_pool = zswap_pool_current();
823 		list_add_rcu(&pool->list, &zswap_pools);
824 		zswap_has_pool = true;
825 	} else if (pool) {
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
829 		 */
830 		list_add_tail_rcu(&pool->list, &zswap_pools);
831 		put_pool = pool;
832 	}
833 
834 	spin_unlock(&zswap_pools_lock);
835 
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()
842 		 * checks above.
843 		 */
844 		ret = param_set_charp(s, kp);
845 	}
846 
847 	/* drop the ref from either the old current pool,
848 	 * or the new pool we failed to add
849 	 */
850 	if (put_pool)
851 		zswap_pool_put(put_pool);
852 
853 	return ret;
854 }
855 
856 static int zswap_compressor_param_set(const char *val,
857 				      const struct kernel_param *kp)
858 {
859 	return __zswap_param_set(val, kp, zswap_zpool_type, NULL);
860 }
861 
862 static int zswap_zpool_param_set(const char *val,
863 				 const struct kernel_param *kp)
864 {
865 	return __zswap_param_set(val, kp, NULL, zswap_compressor);
866 }
867 
868 static int zswap_enabled_param_set(const char *val,
869 				   const struct kernel_param *kp)
870 {
871 	if (zswap_init_failed) {
872 		pr_err("can't enable, initialization failed\n");
873 		return -ENODEV;
874 	}
875 	if (!zswap_has_pool && zswap_init_started) {
876 		pr_err("can't enable, no pool configured\n");
877 		return -ENODEV;
878 	}
879 
880 	return param_set_bool(val, kp);
881 }
882 
883 /*********************************
884 * writeback code
885 **********************************/
886 /* return enum for zswap_get_swap_cache_page */
887 enum zswap_get_swap_ret {
888 	ZSWAP_SWAPCACHE_NEW,
889 	ZSWAP_SWAPCACHE_EXIST,
890 	ZSWAP_SWAPCACHE_FAIL,
891 };
892 
893 /*
894  * zswap_get_swap_cache_page
895  *
896  * This is an adaption of read_swap_cache_async()
897  *
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.
902  *
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
908  */
909 static int zswap_get_swap_cache_page(swp_entry_t entry,
910 				struct page **retpage)
911 {
912 	bool page_was_allocated;
913 
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;
918 	if (!*retpage)
919 		return ZSWAP_SWAPCACHE_FAIL;
920 	return ZSWAP_SWAPCACHE_EXIST;
921 }
922 
923 /*
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.
927  *
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
933  * freed.
934  */
935 static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
936 {
937 	struct zswap_header *zhdr;
938 	swp_entry_t swpentry;
939 	struct zswap_tree *tree;
940 	pgoff_t offset;
941 	struct zswap_entry *entry;
942 	struct page *page;
943 	struct scatterlist input, output;
944 	struct crypto_acomp_ctx *acomp_ctx;
945 
946 	u8 *src, *tmp = NULL;
947 	unsigned int dlen;
948 	int ret;
949 	struct writeback_control wbc = {
950 		.sync_mode = WB_SYNC_NONE,
951 	};
952 
953 	if (!zpool_can_sleep_mapped(pool)) {
954 		tmp = kmalloc(PAGE_SIZE, GFP_ATOMIC);
955 		if (!tmp)
956 			return -ENOMEM;
957 	}
958 
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);
964 
965 	/* find and ref zswap entry */
966 	spin_lock(&tree->lock);
967 	entry = zswap_entry_find_get(&tree->rbroot, offset);
968 	if (!entry) {
969 		/* entry was invalidated */
970 		spin_unlock(&tree->lock);
971 		zpool_unmap_handle(pool, handle);
972 		kfree(tmp);
973 		return 0;
974 	}
975 	spin_unlock(&tree->lock);
976 	BUG_ON(offset != entry->offset);
977 
978 	src = (u8 *)zhdr + sizeof(struct zswap_header);
979 	if (!zpool_can_sleep_mapped(pool)) {
980 		memcpy(tmp, src, entry->length);
981 		src = tmp;
982 		zpool_unmap_handle(pool, handle);
983 	}
984 
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 */
988 		ret = -ENOMEM;
989 		goto fail;
990 
991 	case ZSWAP_SWAPCACHE_EXIST:
992 		/* page is already in the swap cache, ignore for now */
993 		put_page(page);
994 		ret = -EEXIST;
995 		goto fail;
996 
997 	case ZSWAP_SWAPCACHE_NEW: /* page is locked */
998 		/* decompress */
999 		acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
1000 		dlen = PAGE_SIZE;
1001 
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);
1010 
1011 		BUG_ON(ret);
1012 		BUG_ON(dlen != PAGE_SIZE);
1013 
1014 		/* page is up to date */
1015 		SetPageUptodate(page);
1016 	}
1017 
1018 	/* move it to the tail of the inactive list after end_writeback */
1019 	SetPageReclaim(page);
1020 
1021 	/* start writeback */
1022 	__swap_writepage(page, &wbc, end_swap_bio_write);
1023 	put_page(page);
1024 	zswap_written_back_pages++;
1025 
1026 	spin_lock(&tree->lock);
1027 	/* drop local reference */
1028 	zswap_entry_put(tree, entry);
1029 
1030 	/*
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
1036 	*/
1037 	if (entry == zswap_rb_search(&tree->rbroot, offset))
1038 		zswap_entry_put(tree, entry);
1039 	spin_unlock(&tree->lock);
1040 
1041 	goto end;
1042 
1043 	/*
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
1049 	*/
1050 fail:
1051 	spin_lock(&tree->lock);
1052 	zswap_entry_put(tree, entry);
1053 	spin_unlock(&tree->lock);
1054 
1055 end:
1056 	if (zpool_can_sleep_mapped(pool))
1057 		zpool_unmap_handle(pool, handle);
1058 	else
1059 		kfree(tmp);
1060 
1061 	return ret;
1062 }
1063 
1064 static int zswap_is_page_same_filled(void *ptr, unsigned long *value)
1065 {
1066 	unsigned int pos;
1067 	unsigned long *page;
1068 
1069 	page = (unsigned long *)ptr;
1070 	for (pos = 1; pos < PAGE_SIZE / sizeof(*page); pos++) {
1071 		if (page[pos] != page[0])
1072 			return 0;
1073 	}
1074 	*value = page[0];
1075 	return 1;
1076 }
1077 
1078 static void zswap_fill_page(void *ptr, unsigned long value)
1079 {
1080 	unsigned long *page;
1081 
1082 	page = (unsigned long *)ptr;
1083 	memset_l(page, value, PAGE_SIZE / sizeof(unsigned long));
1084 }
1085 
1086 /*********************************
1087 * frontswap hooks
1088 **********************************/
1089 /* attempts to compress and store an single page */
1090 static int zswap_frontswap_store(unsigned type, pgoff_t offset,
1091 				struct page *page)
1092 {
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;
1097 	int ret;
1098 	unsigned int hlen, dlen = PAGE_SIZE;
1099 	unsigned long handle, value;
1100 	char *buf;
1101 	u8 *src, *dst;
1102 	struct zswap_header zhdr = { .swpentry = swp_entry(type, offset) };
1103 	gfp_t gfp;
1104 
1105 	/* THP isn't supported */
1106 	if (PageTransHuge(page)) {
1107 		ret = -EINVAL;
1108 		goto reject;
1109 	}
1110 
1111 	if (!zswap_enabled || !tree) {
1112 		ret = -ENODEV;
1113 		goto reject;
1114 	}
1115 
1116 	/* reclaim space if needed */
1117 	if (zswap_is_full()) {
1118 		struct zswap_pool *pool;
1119 
1120 		zswap_pool_limit_hit++;
1121 		zswap_pool_reached_full = true;
1122 		pool = zswap_pool_last_get();
1123 		if (pool)
1124 			queue_work(shrink_wq, &pool->shrink_work);
1125 		ret = -ENOMEM;
1126 		goto reject;
1127 	}
1128 
1129 	if (zswap_pool_reached_full) {
1130 	       if (!zswap_can_accept()) {
1131 			ret = -ENOMEM;
1132 			goto reject;
1133 		} else
1134 			zswap_pool_reached_full = false;
1135 	}
1136 
1137 	/* allocate entry */
1138 	entry = zswap_entry_cache_alloc(GFP_KERNEL);
1139 	if (!entry) {
1140 		zswap_reject_kmemcache_fail++;
1141 		ret = -ENOMEM;
1142 		goto reject;
1143 	}
1144 
1145 	if (zswap_same_filled_pages_enabled) {
1146 		src = kmap_atomic(page);
1147 		if (zswap_is_page_same_filled(src, &value)) {
1148 			kunmap_atomic(src);
1149 			entry->offset = offset;
1150 			entry->length = 0;
1151 			entry->value = value;
1152 			atomic_inc(&zswap_same_filled_pages);
1153 			goto insert_entry;
1154 		}
1155 		kunmap_atomic(src);
1156 	}
1157 
1158 	if (!zswap_non_same_filled_pages_enabled) {
1159 		ret = -EINVAL;
1160 		goto freepage;
1161 	}
1162 
1163 	/* if entry is successfully added, it keeps the reference */
1164 	entry->pool = zswap_pool_current_get();
1165 	if (!entry->pool) {
1166 		ret = -EINVAL;
1167 		goto freepage;
1168 	}
1169 
1170 	/* compress */
1171 	acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
1172 
1173 	mutex_lock(acomp_ctx->mutex);
1174 
1175 	dst = acomp_ctx->dstmem;
1176 	sg_init_table(&input, 1);
1177 	sg_set_page(&input, page, PAGE_SIZE, 0);
1178 
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);
1182 	/*
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.
1193 	 */
1194 	ret = crypto_wait_req(crypto_acomp_compress(acomp_ctx->req), &acomp_ctx->wait);
1195 	dlen = acomp_ctx->req->dlen;
1196 
1197 	if (ret) {
1198 		ret = -EINVAL;
1199 		goto put_dstmem;
1200 	}
1201 
1202 	/* store */
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++;
1210 		goto put_dstmem;
1211 	}
1212 	if (ret) {
1213 		zswap_reject_alloc_fail++;
1214 		goto put_dstmem;
1215 	}
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);
1221 
1222 	/* populate entry */
1223 	entry->offset = offset;
1224 	entry->handle = handle;
1225 	entry->length = dlen;
1226 
1227 insert_entry:
1228 	/* map */
1229 	spin_lock(&tree->lock);
1230 	do {
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);
1237 		}
1238 	} while (ret == -EEXIST);
1239 	spin_unlock(&tree->lock);
1240 
1241 	/* update stats */
1242 	atomic_inc(&zswap_stored_pages);
1243 	zswap_update_total_size();
1244 
1245 	return 0;
1246 
1247 put_dstmem:
1248 	mutex_unlock(acomp_ctx->mutex);
1249 	zswap_pool_put(entry->pool);
1250 freepage:
1251 	zswap_entry_cache_free(entry);
1252 reject:
1253 	return ret;
1254 }
1255 
1256 /*
1257  * returns 0 if the page was successfully decompressed
1258  * return -1 on entry not found or error
1259 */
1260 static int zswap_frontswap_load(unsigned type, pgoff_t offset,
1261 				struct page *page)
1262 {
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;
1268 	unsigned int dlen;
1269 	int ret;
1270 
1271 	/* find */
1272 	spin_lock(&tree->lock);
1273 	entry = zswap_entry_find_get(&tree->rbroot, offset);
1274 	if (!entry) {
1275 		/* entry was written back */
1276 		spin_unlock(&tree->lock);
1277 		return -1;
1278 	}
1279 	spin_unlock(&tree->lock);
1280 
1281 	if (!entry->length) {
1282 		dst = kmap_atomic(page);
1283 		zswap_fill_page(dst, entry->value);
1284 		kunmap_atomic(dst);
1285 		ret = 0;
1286 		goto freeentry;
1287 	}
1288 
1289 	if (!zpool_can_sleep_mapped(entry->pool->zpool)) {
1290 
1291 		tmp = kmalloc(entry->length, GFP_ATOMIC);
1292 		if (!tmp) {
1293 			ret = -ENOMEM;
1294 			goto freeentry;
1295 		}
1296 	}
1297 
1298 	/* decompress */
1299 	dlen = PAGE_SIZE;
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);
1303 
1304 	if (!zpool_can_sleep_mapped(entry->pool->zpool)) {
1305 
1306 		memcpy(tmp, src, entry->length);
1307 		src = tmp;
1308 
1309 		zpool_unmap_handle(entry->pool->zpool, entry->handle);
1310 	}
1311 
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);
1320 
1321 	if (zpool_can_sleep_mapped(entry->pool->zpool))
1322 		zpool_unmap_handle(entry->pool->zpool, entry->handle);
1323 	else
1324 		kfree(tmp);
1325 
1326 	BUG_ON(ret);
1327 
1328 freeentry:
1329 	spin_lock(&tree->lock);
1330 	zswap_entry_put(tree, entry);
1331 	spin_unlock(&tree->lock);
1332 
1333 	return ret;
1334 }
1335 
1336 /* frees an entry in zswap */
1337 static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset)
1338 {
1339 	struct zswap_tree *tree = zswap_trees[type];
1340 	struct zswap_entry *entry;
1341 
1342 	/* find */
1343 	spin_lock(&tree->lock);
1344 	entry = zswap_rb_search(&tree->rbroot, offset);
1345 	if (!entry) {
1346 		/* entry was written back */
1347 		spin_unlock(&tree->lock);
1348 		return;
1349 	}
1350 
1351 	/* remove from rbtree */
1352 	zswap_rb_erase(&tree->rbroot, entry);
1353 
1354 	/* drop the initial reference from entry creation */
1355 	zswap_entry_put(tree, entry);
1356 
1357 	spin_unlock(&tree->lock);
1358 }
1359 
1360 /* frees all zswap entries for the given swap type */
1361 static void zswap_frontswap_invalidate_area(unsigned type)
1362 {
1363 	struct zswap_tree *tree = zswap_trees[type];
1364 	struct zswap_entry *entry, *n;
1365 
1366 	if (!tree)
1367 		return;
1368 
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);
1375 	kfree(tree);
1376 	zswap_trees[type] = NULL;
1377 }
1378 
1379 static void zswap_frontswap_init(unsigned type)
1380 {
1381 	struct zswap_tree *tree;
1382 
1383 	tree = kzalloc(sizeof(*tree), GFP_KERNEL);
1384 	if (!tree) {
1385 		pr_err("alloc failed, zswap disabled for swap type %d\n", type);
1386 		return;
1387 	}
1388 
1389 	tree->rbroot = RB_ROOT;
1390 	spin_lock_init(&tree->lock);
1391 	zswap_trees[type] = tree;
1392 }
1393 
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
1400 };
1401 
1402 /*********************************
1403 * debugfs functions
1404 **********************************/
1405 #ifdef CONFIG_DEBUG_FS
1406 #include <linux/debugfs.h>
1407 
1408 static struct dentry *zswap_debugfs_root;
1409 
1410 static int __init zswap_debugfs_init(void)
1411 {
1412 	if (!debugfs_initialized())
1413 		return -ENODEV;
1414 
1415 	zswap_debugfs_root = debugfs_create_dir("zswap", NULL);
1416 
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);
1437 
1438 	return 0;
1439 }
1440 #else
1441 static int __init zswap_debugfs_init(void)
1442 {
1443 	return 0;
1444 }
1445 #endif
1446 
1447 /*********************************
1448 * module init and exit
1449 **********************************/
1450 static int __init init_zswap(void)
1451 {
1452 	struct zswap_pool *pool;
1453 	int ret;
1454 
1455 	zswap_init_started = true;
1456 
1457 	if (zswap_entry_cache_create()) {
1458 		pr_err("entry cache creation failed\n");
1459 		goto cache_fail;
1460 	}
1461 
1462 	ret = cpuhp_setup_state(CPUHP_MM_ZSWP_MEM_PREPARE, "mm/zswap:prepare",
1463 				zswap_dstmem_prepare, zswap_dstmem_dead);
1464 	if (ret) {
1465 		pr_err("dstmem alloc failed\n");
1466 		goto dstmem_fail;
1467 	}
1468 
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);
1473 	if (ret)
1474 		goto hp_fail;
1475 
1476 	pool = __zswap_pool_create_fallback();
1477 	if (pool) {
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;
1482 	} else {
1483 		pr_err("pool creation failed\n");
1484 		zswap_enabled = false;
1485 	}
1486 
1487 	shrink_wq = create_workqueue("zswap-shrink");
1488 	if (!shrink_wq)
1489 		goto fallback_fail;
1490 
1491 	ret = frontswap_register_ops(&zswap_frontswap_ops);
1492 	if (ret)
1493 		goto destroy_wq;
1494 	if (zswap_debugfs_init())
1495 		pr_warn("debugfs initialization failed\n");
1496 	return 0;
1497 
1498 destroy_wq:
1499 	destroy_workqueue(shrink_wq);
1500 fallback_fail:
1501 	if (pool)
1502 		zswap_pool_destroy(pool);
1503 hp_fail:
1504 	cpuhp_remove_state(CPUHP_MM_ZSWP_MEM_PREPARE);
1505 dstmem_fail:
1506 	zswap_entry_cache_destroy();
1507 cache_fail:
1508 	/* if built-in, we aren't unloaded on failure; don't allow use */
1509 	zswap_init_failed = true;
1510 	zswap_enabled = false;
1511 	return -ENOMEM;
1512 }
1513 /* must be late so crypto has time to come up */
1514 late_initcall(init_zswap);
1515 
1516 MODULE_LICENSE("GPL");
1517 MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
1518 MODULE_DESCRIPTION("Compressed cache for swap pages");
1519