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