1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
22b281117SSeth Jennings /*
32b281117SSeth Jennings * zswap.c - zswap driver file
42b281117SSeth Jennings *
542c06a0eSJohannes Weiner * zswap is a cache that takes pages that are in the process
62b281117SSeth Jennings * of being swapped out and attempts to compress and store them in a
72b281117SSeth Jennings * RAM-based memory pool. This can result in a significant I/O reduction on
82b281117SSeth Jennings * the swap device and, in the case where decompressing from RAM is faster
92b281117SSeth Jennings * than reading from the swap device, can also improve workload performance.
102b281117SSeth Jennings *
112b281117SSeth Jennings * Copyright (C) 2012 Seth Jennings <sjenning@linux.vnet.ibm.com>
122b281117SSeth Jennings */
132b281117SSeth Jennings
142b281117SSeth Jennings #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
152b281117SSeth Jennings
162b281117SSeth Jennings #include <linux/module.h>
172b281117SSeth Jennings #include <linux/cpu.h>
182b281117SSeth Jennings #include <linux/highmem.h>
192b281117SSeth Jennings #include <linux/slab.h>
202b281117SSeth Jennings #include <linux/spinlock.h>
212b281117SSeth Jennings #include <linux/types.h>
222b281117SSeth Jennings #include <linux/atomic.h>
232b281117SSeth Jennings #include <linux/swap.h>
242b281117SSeth Jennings #include <linux/crypto.h>
251ec3b5feSBarry Song #include <linux/scatterlist.h>
26ddc1a5cbSHugh Dickins #include <linux/mempolicy.h>
272b281117SSeth Jennings #include <linux/mempool.h>
281ec3b5feSBarry Song #include <crypto/acompress.h>
29e2c3b6b2SYosry Ahmed #include <crypto/scatterwalk.h>
3042c06a0eSJohannes Weiner #include <linux/zswap.h>
312b281117SSeth Jennings #include <linux/mm_types.h>
322b281117SSeth Jennings #include <linux/page-flags.h>
332b281117SSeth Jennings #include <linux/swapops.h>
342b281117SSeth Jennings #include <linux/writeback.h>
352b281117SSeth Jennings #include <linux/pagemap.h>
3645190f01SVitaly Wool #include <linux/workqueue.h>
37a65b0e76SDomenico Cerasuolo #include <linux/list_lru.h>
385c3f8be0SJohannes Weiner #include <linux/zsmalloc.h>
392b281117SSeth Jennings
40014bb1deSNeilBrown #include "swap.h"
41e0228d59SDomenico Cerasuolo #include "internal.h"
42014bb1deSNeilBrown
432b281117SSeth Jennings /*********************************
442b281117SSeth Jennings * statistics
452b281117SSeth Jennings **********************************/
46dca4437aSSeongJae Park /* The number of pages currently stored in zswap */
47ea6de4f8SSun YangKai atomic_long_t zswap_stored_pages = ATOMIC_LONG_INIT(0);
48dca4437aSSeongJae Park /* The number of incompressible pages currently stored in zswap */
49dca4437aSSeongJae Park static atomic_long_t zswap_stored_incompressible_pages = ATOMIC_LONG_INIT(0);
502b281117SSeth Jennings
512b281117SSeth Jennings /*
522b281117SSeth Jennings * The statistics below are not protected from concurrent access for
532b281117SSeth Jennings * performance reasons so they may not be a 100% accurate. However,
542b281117SSeth Jennings * they do provide useful information on roughly how many times a
552b281117SSeth Jennings * certain event is occurring.
562b281117SSeth Jennings */
572b281117SSeth Jennings
582b281117SSeth Jennings /* Pool limit was hit (see zswap_max_pool_percent) */
592b281117SSeth Jennings static u64 zswap_pool_limit_hit;
602b281117SSeth Jennings /* Pages written back when pool limit was reached */
612b281117SSeth Jennings static u64 zswap_written_back_pages;
622b281117SSeth Jennings /* Store failed due to a reclaim failure after pool limit was reached */
632b281117SSeth Jennings static u64 zswap_reject_reclaim_fail;
64cb61dad8SNhat Pham /* Store failed due to compression algorithm failure */
65cb61dad8SNhat Pham static u64 zswap_reject_compress_fail;
662b281117SSeth Jennings /* Compressed page was too big for the allocator to (optimally) store */
672b281117SSeth Jennings static u64 zswap_reject_compress_poor;
68ff22f929SNhat Pham /* Load or writeback failed due to decompression failure */
69ff22f929SNhat Pham static u64 zswap_decompress_fail;
702b281117SSeth Jennings /* Store failed because underlying allocator could not get memory */
712b281117SSeth Jennings static u64 zswap_reject_alloc_fail;
722b281117SSeth Jennings /* Store failed because the entry metadata could not be allocated (rare) */
732b281117SSeth Jennings static u64 zswap_reject_kmemcache_fail;
742b281117SSeth Jennings
7545190f01SVitaly Wool /* Shrinker work queue */
7645190f01SVitaly Wool static struct workqueue_struct *shrink_wq;
7745190f01SVitaly Wool /* Pool limit was hit, we need to calm down */
7845190f01SVitaly Wool static bool zswap_pool_reached_full;
7945190f01SVitaly Wool
802b281117SSeth Jennings /*********************************
812b281117SSeth Jennings * tunables
822b281117SSeth Jennings **********************************/
83c00ed16aSDan Streetman
84bae21db8SDan Streetman #define ZSWAP_PARAM_UNSET ""
85bae21db8SDan Streetman
86141fdeecSLiu Shixin static int zswap_setup(void);
87141fdeecSLiu Shixin
88bb8b93b5SMaciej S. Szmigiero /* Enable/disable zswap */
892d4d2b1cSYosry Ahmed static DEFINE_STATIC_KEY_MAYBE(CONFIG_ZSWAP_DEFAULT_ON, zswap_ever_enabled);
90bb8b93b5SMaciej S. Szmigiero static bool zswap_enabled = IS_ENABLED(CONFIG_ZSWAP_DEFAULT_ON);
91d7b028f5SDan Streetman static int zswap_enabled_param_set(const char *,
92d7b028f5SDan Streetman const struct kernel_param *);
9383aed6cdSJoe Perches static const struct kernel_param_ops zswap_enabled_param_ops = {
94d7b028f5SDan Streetman .set = zswap_enabled_param_set,
95d7b028f5SDan Streetman .get = param_get_bool,
96d7b028f5SDan Streetman };
97d7b028f5SDan Streetman module_param_cb(enabled, &zswap_enabled_param_ops, &zswap_enabled, 0644);
982b281117SSeth Jennings
9990b0fc26SDan Streetman /* Crypto compressor to use */
100bb8b93b5SMaciej S. Szmigiero static char *zswap_compressor = CONFIG_ZSWAP_COMPRESSOR_DEFAULT;
10190b0fc26SDan Streetman static int zswap_compressor_param_set(const char *,
10290b0fc26SDan Streetman const struct kernel_param *);
10383aed6cdSJoe Perches static const struct kernel_param_ops zswap_compressor_param_ops = {
10490b0fc26SDan Streetman .set = zswap_compressor_param_set,
105c99b42c3SDan Streetman .get = param_get_charp,
106c99b42c3SDan Streetman .free = param_free_charp,
10790b0fc26SDan Streetman };
10890b0fc26SDan Streetman module_param_cb(compressor, &zswap_compressor_param_ops,
109c99b42c3SDan Streetman &zswap_compressor, 0644);
11090b0fc26SDan Streetman
1112b281117SSeth Jennings /* The maximum percentage of memory that the compressed pool can occupy */
1122b281117SSeth Jennings static unsigned int zswap_max_pool_percent = 20;
11390b0fc26SDan Streetman module_param_named(max_pool_percent, zswap_max_pool_percent, uint, 0644);
11460105e12SMinchan Kim
11545190f01SVitaly Wool /* The threshold for accepting new pages after the max_pool_percent was hit */
11645190f01SVitaly Wool static unsigned int zswap_accept_thr_percent = 90; /* of max pool size */
11745190f01SVitaly Wool module_param_named(accept_threshold_percent, zswap_accept_thr_percent,
11845190f01SVitaly Wool uint, 0644);
11945190f01SVitaly Wool
120b5ba474fSNhat Pham /* Enable/disable memory pressure-based shrinker. */
121b5ba474fSNhat Pham static bool zswap_shrinker_enabled = IS_ENABLED(
122b5ba474fSNhat Pham CONFIG_ZSWAP_SHRINKER_DEFAULT_ON);
123b5ba474fSNhat Pham module_param_named(shrinker_enabled, zswap_shrinker_enabled, bool, 0644);
124b5ba474fSNhat Pham
zswap_is_enabled(void)1252b33a97cSYosry Ahmed bool zswap_is_enabled(void)
126501a06feSNhat Pham {
127501a06feSNhat Pham return zswap_enabled;
128501a06feSNhat Pham }
129501a06feSNhat Pham
zswap_never_enabled(void)1302d4d2b1cSYosry Ahmed bool zswap_never_enabled(void)
1312d4d2b1cSYosry Ahmed {
1322d4d2b1cSYosry Ahmed return !static_branch_maybe(CONFIG_ZSWAP_DEFAULT_ON, &zswap_ever_enabled);
1332d4d2b1cSYosry Ahmed }
1342d4d2b1cSYosry Ahmed
1352b281117SSeth Jennings /*********************************
1362b281117SSeth Jennings * data structures
1372b281117SSeth Jennings **********************************/
138f1c54846SDan Streetman
1391ec3b5feSBarry Song struct crypto_acomp_ctx {
1401ec3b5feSBarry Song struct crypto_acomp *acomp;
1411ec3b5feSBarry Song struct acomp_req *req;
1421ec3b5feSBarry Song struct crypto_wait wait;
1438ba2f844SChengming Zhou u8 *buffer;
1448ba2f844SChengming Zhou struct mutex mutex;
1451ec3b5feSBarry Song };
1461ec3b5feSBarry Song
147f999f38bSDomenico Cerasuolo /*
148f999f38bSDomenico Cerasuolo * The lock ordering is zswap_tree.lock -> zswap_pool.lru_lock.
149f999f38bSDomenico Cerasuolo * The only case where lru_lock is not acquired while holding tree.lock is
150f999f38bSDomenico Cerasuolo * when a zswap_entry is taken off the lru for writeback, in that case it
151f999f38bSDomenico Cerasuolo * needs to be verified that it's still valid in the tree.
152f999f38bSDomenico Cerasuolo */
153f1c54846SDan Streetman struct zswap_pool {
1545c3f8be0SJohannes Weiner struct zs_pool *zs_pool;
1551ec3b5feSBarry Song struct crypto_acomp_ctx __percpu *acomp_ctx;
15694ace3feSChengming Zhou struct percpu_ref ref;
157f1c54846SDan Streetman struct list_head list;
15845190f01SVitaly Wool struct work_struct release_work;
159cab7a7e5SSebastian Andrzej Siewior struct hlist_node node;
160f1c54846SDan Streetman char tfm_name[CRYPTO_MAX_ALG_NAME];
161f1c54846SDan Streetman };
162f1c54846SDan Streetman
163e35606e4SChengming Zhou /* Global LRU lists shared by all zswap pools. */
164e35606e4SChengming Zhou static struct list_lru zswap_list_lru;
165e35606e4SChengming Zhou
166e35606e4SChengming Zhou /* The lock protects zswap_next_shrink updates. */
167e35606e4SChengming Zhou static DEFINE_SPINLOCK(zswap_shrink_lock);
168e35606e4SChengming Zhou static struct mem_cgroup *zswap_next_shrink;
169e35606e4SChengming Zhou static struct work_struct zswap_shrink_work;
170e35606e4SChengming Zhou static struct shrinker *zswap_shrinker;
171bf9b7df2SChengming Zhou
1722b281117SSeth Jennings /*
1732b281117SSeth Jennings * struct zswap_entry
1742b281117SSeth Jennings *
1752b281117SSeth Jennings * This structure contains the metadata for tracking a single compressed
1762b281117SSeth Jennings * page within zswap.
1772b281117SSeth Jennings *
1781f52f3deSSeongJae Park * swpentry - associated swap entry, the offset indexes into the xarray
1792b281117SSeth Jennings * length - the length in bytes of the compressed page data. Needed during
18020a5532fSUsama Arif * decompression.
181e31c38e0SNhat Pham * referenced - true if the entry recently entered the zswap pool. Unset by the
182e31c38e0SNhat Pham * writeback logic. The entry is only reclaimed by the writeback
183e31c38e0SNhat Pham * logic if referenced is unset. See comments in the shrinker
184e31c38e0SNhat Pham * section for context.
185f1c54846SDan Streetman * pool - the zswap_pool the entry's data is in
1865c3f8be0SJohannes Weiner * handle - zsmalloc allocation handle that stores the compressed page data
18797157d89SXiu Jianfeng * objcg - the obj_cgroup that the compressed memory is charged to
188f999f38bSDomenico Cerasuolo * lru - handle to the pool's lru used to evict pages.
1892b281117SSeth Jennings */
1902b281117SSeth Jennings struct zswap_entry {
1910bb48849SDomenico Cerasuolo swp_entry_t swpentry;
1922b281117SSeth Jennings unsigned int length;
193e31c38e0SNhat Pham bool referenced;
194f1c54846SDan Streetman struct zswap_pool *pool;
1952b281117SSeth Jennings unsigned long handle;
196f4840ccfSJohannes Weiner struct obj_cgroup *objcg;
197f999f38bSDomenico Cerasuolo struct list_head lru;
1982b281117SSeth Jennings };
1992b281117SSeth Jennings
200796c2c23SChris Li static struct xarray *zswap_trees[MAX_SWAPFILES];
20144c7c734SChengming Zhou static unsigned int nr_zswap_trees[MAX_SWAPFILES];
2022b281117SSeth Jennings
203f1c54846SDan Streetman /* RCU-protected iteration */
204f1c54846SDan Streetman static LIST_HEAD(zswap_pools);
205f1c54846SDan Streetman /* protects zswap_pools list modification */
206f1c54846SDan Streetman static DEFINE_SPINLOCK(zswap_pools_lock);
2075c3f8be0SJohannes Weiner /* pool counter to provide unique names to zsmalloc */
20832a4e169SDan Streetman static atomic_t zswap_pools_count = ATOMIC_INIT(0);
209f1c54846SDan Streetman
2109021ccecSLiu Shixin enum zswap_init_type {
2119021ccecSLiu Shixin ZSWAP_UNINIT,
2129021ccecSLiu Shixin ZSWAP_INIT_SUCCEED,
2139021ccecSLiu Shixin ZSWAP_INIT_FAILED
2149021ccecSLiu Shixin };
21590b0fc26SDan Streetman
2169021ccecSLiu Shixin static enum zswap_init_type zswap_init_state;
217d7b028f5SDan Streetman
218141fdeecSLiu Shixin /* used to ensure the integrity of initialization */
219141fdeecSLiu Shixin static DEFINE_MUTEX(zswap_init_lock);
220f1c54846SDan Streetman
221ae3d89a7SDan Streetman /* init completed, but couldn't create the initial pool */
222ae3d89a7SDan Streetman static bool zswap_has_pool;
223ae3d89a7SDan Streetman
224f1c54846SDan Streetman /*********************************
225f1c54846SDan Streetman * helpers and fwd declarations
226f1c54846SDan Streetman **********************************/
227f1c54846SDan Streetman
228685a17fbSKairui Song /* One swap address space for each 64M swap space */
229685a17fbSKairui Song #define ZSWAP_ADDRESS_SPACE_SHIFT 14
230685a17fbSKairui Song #define ZSWAP_ADDRESS_SPACE_PAGES (1 << ZSWAP_ADDRESS_SPACE_SHIFT)
swap_zswap_tree(swp_entry_t swp)231796c2c23SChris Li static inline struct xarray *swap_zswap_tree(swp_entry_t swp)
23244c7c734SChengming Zhou {
23344c7c734SChengming Zhou return &zswap_trees[swp_type(swp)][swp_offset(swp)
234685a17fbSKairui Song >> ZSWAP_ADDRESS_SPACE_SHIFT];
23544c7c734SChengming Zhou }
23644c7c734SChengming Zhou
237f1c54846SDan Streetman #define zswap_pool_debug(msg, p) \
2385c3f8be0SJohannes Weiner pr_debug("%s pool %s\n", msg, (p)->tfm_name)
239f1c54846SDan Streetman
240a984649bSJohannes Weiner /*********************************
241a984649bSJohannes Weiner * pool functions
242a984649bSJohannes Weiner **********************************/
24394ace3feSChengming Zhou static void __zswap_pool_empty(struct percpu_ref *ref);
244a984649bSJohannes Weiner
acomp_ctx_free(struct crypto_acomp_ctx * acomp_ctx)245*ef3c0f6cSKanchana P. Sridhar static void acomp_ctx_free(struct crypto_acomp_ctx *acomp_ctx)
246*ef3c0f6cSKanchana P. Sridhar {
247*ef3c0f6cSKanchana P. Sridhar if (!acomp_ctx)
248*ef3c0f6cSKanchana P. Sridhar return;
249*ef3c0f6cSKanchana P. Sridhar
250*ef3c0f6cSKanchana P. Sridhar /*
251*ef3c0f6cSKanchana P. Sridhar * If there was an error in allocating @acomp_ctx->req, it
252*ef3c0f6cSKanchana P. Sridhar * would be set to NULL.
253*ef3c0f6cSKanchana P. Sridhar */
254*ef3c0f6cSKanchana P. Sridhar if (acomp_ctx->req)
255*ef3c0f6cSKanchana P. Sridhar acomp_request_free(acomp_ctx->req);
256*ef3c0f6cSKanchana P. Sridhar
257*ef3c0f6cSKanchana P. Sridhar acomp_ctx->req = NULL;
258*ef3c0f6cSKanchana P. Sridhar
259*ef3c0f6cSKanchana P. Sridhar /*
260*ef3c0f6cSKanchana P. Sridhar * We have to handle both cases here: an error pointer return from
261*ef3c0f6cSKanchana P. Sridhar * crypto_alloc_acomp_node(); and a) NULL initialization by zswap, or
262*ef3c0f6cSKanchana P. Sridhar * b) NULL assignment done in a previous call to acomp_ctx_free().
263*ef3c0f6cSKanchana P. Sridhar */
264*ef3c0f6cSKanchana P. Sridhar if (!IS_ERR_OR_NULL(acomp_ctx->acomp))
265*ef3c0f6cSKanchana P. Sridhar crypto_free_acomp(acomp_ctx->acomp);
266*ef3c0f6cSKanchana P. Sridhar
267*ef3c0f6cSKanchana P. Sridhar acomp_ctx->acomp = NULL;
268*ef3c0f6cSKanchana P. Sridhar
269*ef3c0f6cSKanchana P. Sridhar kfree(acomp_ctx->buffer);
270*ef3c0f6cSKanchana P. Sridhar acomp_ctx->buffer = NULL;
271*ef3c0f6cSKanchana P. Sridhar }
272*ef3c0f6cSKanchana P. Sridhar
zswap_pool_create(char * compressor)2735c3f8be0SJohannes Weiner static struct zswap_pool *zswap_pool_create(char *compressor)
274a984649bSJohannes Weiner {
275a984649bSJohannes Weiner struct zswap_pool *pool;
276a984649bSJohannes Weiner char name[38]; /* 'zswap' + 32 char (max) num + \0 */
27712dcb0efSYosry Ahmed int ret, cpu;
278a984649bSJohannes Weiner
2795c3f8be0SJohannes Weiner if (!zswap_has_pool && !strcmp(compressor, ZSWAP_PARAM_UNSET))
280a984649bSJohannes Weiner return NULL;
281a984649bSJohannes Weiner
282bf4afc53SLinus Torvalds pool = kzalloc_obj(*pool);
283a984649bSJohannes Weiner if (!pool)
284a984649bSJohannes Weiner return NULL;
285a984649bSJohannes Weiner
286a984649bSJohannes Weiner /* unique name for each pool specifically required by zsmalloc */
2878edc9c4eSChengming Zhou snprintf(name, 38, "zswap%x", atomic_inc_return(&zswap_pools_count));
2885c3f8be0SJohannes Weiner pool->zs_pool = zs_create_pool(name);
2895c3f8be0SJohannes Weiner if (!pool->zs_pool)
290a984649bSJohannes Weiner goto error;
291a984649bSJohannes Weiner
292a984649bSJohannes Weiner strscpy(pool->tfm_name, compressor, sizeof(pool->tfm_name));
293a984649bSJohannes Weiner
294*ef3c0f6cSKanchana P. Sridhar /* Many things rely on the zero-initialization. */
295*ef3c0f6cSKanchana P. Sridhar pool->acomp_ctx = alloc_percpu_gfp(*pool->acomp_ctx,
296*ef3c0f6cSKanchana P. Sridhar GFP_KERNEL | __GFP_ZERO);
297a984649bSJohannes Weiner if (!pool->acomp_ctx) {
298a984649bSJohannes Weiner pr_err("percpu alloc failed\n");
299a984649bSJohannes Weiner goto error;
300a984649bSJohannes Weiner }
301a984649bSJohannes Weiner
302*ef3c0f6cSKanchana P. Sridhar /*
303*ef3c0f6cSKanchana P. Sridhar * This is serialized against CPU hotplug operations. Hence, cores
304*ef3c0f6cSKanchana P. Sridhar * cannot be offlined until this finishes.
305*ef3c0f6cSKanchana P. Sridhar */
306a984649bSJohannes Weiner ret = cpuhp_state_add_instance(CPUHP_MM_ZSWP_POOL_PREPARE,
307a984649bSJohannes Weiner &pool->node);
308*ef3c0f6cSKanchana P. Sridhar
309*ef3c0f6cSKanchana P. Sridhar /*
310*ef3c0f6cSKanchana P. Sridhar * cpuhp_state_add_instance() will not cleanup on failure since
311*ef3c0f6cSKanchana P. Sridhar * we don't register a hotunplug callback.
312*ef3c0f6cSKanchana P. Sridhar */
313a984649bSJohannes Weiner if (ret)
314*ef3c0f6cSKanchana P. Sridhar goto cpuhp_add_fail;
315a984649bSJohannes Weiner
316a984649bSJohannes Weiner /* being the current pool takes 1 ref; this func expects the
317a984649bSJohannes Weiner * caller to always add the new pool as the current pool
318a984649bSJohannes Weiner */
31994ace3feSChengming Zhou ret = percpu_ref_init(&pool->ref, __zswap_pool_empty,
32094ace3feSChengming Zhou PERCPU_REF_ALLOW_REINIT, GFP_KERNEL);
32194ace3feSChengming Zhou if (ret)
32294ace3feSChengming Zhou goto ref_fail;
323a984649bSJohannes Weiner INIT_LIST_HEAD(&pool->list);
324a984649bSJohannes Weiner
325a984649bSJohannes Weiner zswap_pool_debug("created", pool);
326a984649bSJohannes Weiner
327a984649bSJohannes Weiner return pool;
328a984649bSJohannes Weiner
32994ace3feSChengming Zhou ref_fail:
33094ace3feSChengming Zhou cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node);
331*ef3c0f6cSKanchana P. Sridhar
332*ef3c0f6cSKanchana P. Sridhar cpuhp_add_fail:
333*ef3c0f6cSKanchana P. Sridhar for_each_possible_cpu(cpu)
334*ef3c0f6cSKanchana P. Sridhar acomp_ctx_free(per_cpu_ptr(pool->acomp_ctx, cpu));
335a984649bSJohannes Weiner error:
336a984649bSJohannes Weiner if (pool->acomp_ctx)
337a984649bSJohannes Weiner free_percpu(pool->acomp_ctx);
3385c3f8be0SJohannes Weiner if (pool->zs_pool)
3395c3f8be0SJohannes Weiner zs_destroy_pool(pool->zs_pool);
340a984649bSJohannes Weiner kfree(pool);
341a984649bSJohannes Weiner return NULL;
342a984649bSJohannes Weiner }
343a984649bSJohannes Weiner
__zswap_pool_create_fallback(void)344a984649bSJohannes Weiner static struct zswap_pool *__zswap_pool_create_fallback(void)
345a984649bSJohannes Weiner {
3465c3f8be0SJohannes Weiner if (!crypto_has_acomp(zswap_compressor, 0, 0) &&
3475c3f8be0SJohannes Weiner strcmp(zswap_compressor, CONFIG_ZSWAP_COMPRESSOR_DEFAULT)) {
348a984649bSJohannes Weiner pr_err("compressor %s not available, using default %s\n",
349a984649bSJohannes Weiner zswap_compressor, CONFIG_ZSWAP_COMPRESSOR_DEFAULT);
350a984649bSJohannes Weiner param_free_charp(&zswap_compressor);
351a984649bSJohannes Weiner zswap_compressor = CONFIG_ZSWAP_COMPRESSOR_DEFAULT;
352a984649bSJohannes Weiner }
3535c3f8be0SJohannes Weiner
3545c3f8be0SJohannes Weiner /* Default compressor should be available. Kconfig bug? */
3555c3f8be0SJohannes Weiner if (WARN_ON_ONCE(!crypto_has_acomp(zswap_compressor, 0, 0))) {
356a984649bSJohannes Weiner zswap_compressor = ZSWAP_PARAM_UNSET;
357a984649bSJohannes Weiner return NULL;
3585c3f8be0SJohannes Weiner }
359a984649bSJohannes Weiner
3605c3f8be0SJohannes Weiner return zswap_pool_create(zswap_compressor);
361a984649bSJohannes Weiner }
362a984649bSJohannes Weiner
zswap_pool_destroy(struct zswap_pool * pool)363a984649bSJohannes Weiner static void zswap_pool_destroy(struct zswap_pool *pool)
364a984649bSJohannes Weiner {
365*ef3c0f6cSKanchana P. Sridhar int cpu;
366*ef3c0f6cSKanchana P. Sridhar
367a984649bSJohannes Weiner zswap_pool_debug("destroying", pool);
368a984649bSJohannes Weiner
369a984649bSJohannes Weiner cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node);
370*ef3c0f6cSKanchana P. Sridhar
371*ef3c0f6cSKanchana P. Sridhar for_each_possible_cpu(cpu)
372*ef3c0f6cSKanchana P. Sridhar acomp_ctx_free(per_cpu_ptr(pool->acomp_ctx, cpu));
373*ef3c0f6cSKanchana P. Sridhar
374a984649bSJohannes Weiner free_percpu(pool->acomp_ctx);
375a984649bSJohannes Weiner
3765c3f8be0SJohannes Weiner zs_destroy_pool(pool->zs_pool);
377a984649bSJohannes Weiner kfree(pool);
378a984649bSJohannes Weiner }
379a984649bSJohannes Weiner
__zswap_pool_release(struct work_struct * work)38039f3ec8eSJohannes Weiner static void __zswap_pool_release(struct work_struct *work)
38139f3ec8eSJohannes Weiner {
38239f3ec8eSJohannes Weiner struct zswap_pool *pool = container_of(work, typeof(*pool),
38339f3ec8eSJohannes Weiner release_work);
38439f3ec8eSJohannes Weiner
38539f3ec8eSJohannes Weiner synchronize_rcu();
38639f3ec8eSJohannes Weiner
38794ace3feSChengming Zhou /* nobody should have been able to get a ref... */
38894ace3feSChengming Zhou WARN_ON(!percpu_ref_is_zero(&pool->ref));
38994ace3feSChengming Zhou percpu_ref_exit(&pool->ref);
39039f3ec8eSJohannes Weiner
39139f3ec8eSJohannes Weiner /* pool is now off zswap_pools list and has no references. */
39239f3ec8eSJohannes Weiner zswap_pool_destroy(pool);
39339f3ec8eSJohannes Weiner }
39439f3ec8eSJohannes Weiner
39539f3ec8eSJohannes Weiner static struct zswap_pool *zswap_pool_current(void);
39639f3ec8eSJohannes Weiner
__zswap_pool_empty(struct percpu_ref * ref)39794ace3feSChengming Zhou static void __zswap_pool_empty(struct percpu_ref *ref)
39839f3ec8eSJohannes Weiner {
39939f3ec8eSJohannes Weiner struct zswap_pool *pool;
40039f3ec8eSJohannes Weiner
40194ace3feSChengming Zhou pool = container_of(ref, typeof(*pool), ref);
40239f3ec8eSJohannes Weiner
40394ace3feSChengming Zhou spin_lock_bh(&zswap_pools_lock);
40439f3ec8eSJohannes Weiner
40539f3ec8eSJohannes Weiner WARN_ON(pool == zswap_pool_current());
40639f3ec8eSJohannes Weiner
40739f3ec8eSJohannes Weiner list_del_rcu(&pool->list);
40839f3ec8eSJohannes Weiner
40939f3ec8eSJohannes Weiner INIT_WORK(&pool->release_work, __zswap_pool_release);
41039f3ec8eSJohannes Weiner schedule_work(&pool->release_work);
41139f3ec8eSJohannes Weiner
41294ace3feSChengming Zhou spin_unlock_bh(&zswap_pools_lock);
41339f3ec8eSJohannes Weiner }
41439f3ec8eSJohannes Weiner
zswap_pool_tryget(struct zswap_pool * pool)4150201c054SKanchana P Sridhar static int __must_check zswap_pool_tryget(struct zswap_pool *pool)
41639f3ec8eSJohannes Weiner {
41739f3ec8eSJohannes Weiner if (!pool)
41839f3ec8eSJohannes Weiner return 0;
41939f3ec8eSJohannes Weiner
42094ace3feSChengming Zhou return percpu_ref_tryget(&pool->ref);
42139f3ec8eSJohannes Weiner }
42239f3ec8eSJohannes Weiner
423b7c0ccdfSKanchana P Sridhar /* The caller must already have a reference. */
zswap_pool_get(struct zswap_pool * pool)424b7c0ccdfSKanchana P Sridhar static void zswap_pool_get(struct zswap_pool *pool)
425b7c0ccdfSKanchana P Sridhar {
426b7c0ccdfSKanchana P Sridhar percpu_ref_get(&pool->ref);
427b7c0ccdfSKanchana P Sridhar }
428b7c0ccdfSKanchana P Sridhar
zswap_pool_put(struct zswap_pool * pool)42939f3ec8eSJohannes Weiner static void zswap_pool_put(struct zswap_pool *pool)
43039f3ec8eSJohannes Weiner {
43194ace3feSChengming Zhou percpu_ref_put(&pool->ref);
43239f3ec8eSJohannes Weiner }
43339f3ec8eSJohannes Weiner
__zswap_pool_current(void)434c1a0ecb8SJohannes Weiner static struct zswap_pool *__zswap_pool_current(void)
435c1a0ecb8SJohannes Weiner {
436c1a0ecb8SJohannes Weiner struct zswap_pool *pool;
437c1a0ecb8SJohannes Weiner
438c1a0ecb8SJohannes Weiner pool = list_first_or_null_rcu(&zswap_pools, typeof(*pool), list);
439c1a0ecb8SJohannes Weiner WARN_ONCE(!pool && zswap_has_pool,
440c1a0ecb8SJohannes Weiner "%s: no page storage pool!\n", __func__);
441c1a0ecb8SJohannes Weiner
442c1a0ecb8SJohannes Weiner return pool;
443c1a0ecb8SJohannes Weiner }
444c1a0ecb8SJohannes Weiner
zswap_pool_current(void)445c1a0ecb8SJohannes Weiner static struct zswap_pool *zswap_pool_current(void)
446c1a0ecb8SJohannes Weiner {
447c1a0ecb8SJohannes Weiner assert_spin_locked(&zswap_pools_lock);
448c1a0ecb8SJohannes Weiner
449c1a0ecb8SJohannes Weiner return __zswap_pool_current();
450c1a0ecb8SJohannes Weiner }
451c1a0ecb8SJohannes Weiner
zswap_pool_current_get(void)452c1a0ecb8SJohannes Weiner static struct zswap_pool *zswap_pool_current_get(void)
453c1a0ecb8SJohannes Weiner {
454c1a0ecb8SJohannes Weiner struct zswap_pool *pool;
455c1a0ecb8SJohannes Weiner
456c1a0ecb8SJohannes Weiner rcu_read_lock();
457c1a0ecb8SJohannes Weiner
458c1a0ecb8SJohannes Weiner pool = __zswap_pool_current();
4590201c054SKanchana P Sridhar if (!zswap_pool_tryget(pool))
460c1a0ecb8SJohannes Weiner pool = NULL;
461c1a0ecb8SJohannes Weiner
462c1a0ecb8SJohannes Weiner rcu_read_unlock();
463c1a0ecb8SJohannes Weiner
464c1a0ecb8SJohannes Weiner return pool;
465c1a0ecb8SJohannes Weiner }
466c1a0ecb8SJohannes Weiner
467c1a0ecb8SJohannes Weiner /* type and compressor must be null-terminated */
zswap_pool_find_get(char * compressor)4685c3f8be0SJohannes Weiner static struct zswap_pool *zswap_pool_find_get(char *compressor)
469c1a0ecb8SJohannes Weiner {
470c1a0ecb8SJohannes Weiner struct zswap_pool *pool;
471c1a0ecb8SJohannes Weiner
472c1a0ecb8SJohannes Weiner assert_spin_locked(&zswap_pools_lock);
473c1a0ecb8SJohannes Weiner
474c1a0ecb8SJohannes Weiner list_for_each_entry_rcu(pool, &zswap_pools, list) {
475c1a0ecb8SJohannes Weiner if (strcmp(pool->tfm_name, compressor))
476c1a0ecb8SJohannes Weiner continue;
477c1a0ecb8SJohannes Weiner /* if we can't get it, it's about to be destroyed */
4780201c054SKanchana P Sridhar if (!zswap_pool_tryget(pool))
479c1a0ecb8SJohannes Weiner continue;
480c1a0ecb8SJohannes Weiner return pool;
481c1a0ecb8SJohannes Weiner }
482c1a0ecb8SJohannes Weiner
483c1a0ecb8SJohannes Weiner return NULL;
484c1a0ecb8SJohannes Weiner }
485c1a0ecb8SJohannes Weiner
zswap_max_pages(void)48691cdcd8dSJohannes Weiner static unsigned long zswap_max_pages(void)
48791cdcd8dSJohannes Weiner {
48891cdcd8dSJohannes Weiner return totalram_pages() * zswap_max_pool_percent / 100;
48991cdcd8dSJohannes Weiner }
49091cdcd8dSJohannes Weiner
zswap_accept_thr_pages(void)49191cdcd8dSJohannes Weiner static unsigned long zswap_accept_thr_pages(void)
49291cdcd8dSJohannes Weiner {
49391cdcd8dSJohannes Weiner return zswap_max_pages() * zswap_accept_thr_percent / 100;
49491cdcd8dSJohannes Weiner }
49591cdcd8dSJohannes Weiner
zswap_total_pages(void)49691cdcd8dSJohannes Weiner unsigned long zswap_total_pages(void)
49791cdcd8dSJohannes Weiner {
49891cdcd8dSJohannes Weiner struct zswap_pool *pool;
4994196b48dSJohannes Weiner unsigned long total = 0;
50091cdcd8dSJohannes Weiner
50191cdcd8dSJohannes Weiner rcu_read_lock();
5028edc9c4eSChengming Zhou list_for_each_entry_rcu(pool, &zswap_pools, list)
5035c3f8be0SJohannes Weiner total += zs_get_total_pages(pool->zs_pool);
50491cdcd8dSJohannes Weiner rcu_read_unlock();
50591cdcd8dSJohannes Weiner
5064196b48dSJohannes Weiner return total;
50791cdcd8dSJohannes Weiner }
50891cdcd8dSJohannes Weiner
zswap_check_limits(void)50982e0f8e4SYosry Ahmed static bool zswap_check_limits(void)
51082e0f8e4SYosry Ahmed {
51182e0f8e4SYosry Ahmed unsigned long cur_pages = zswap_total_pages();
51282e0f8e4SYosry Ahmed unsigned long max_pages = zswap_max_pages();
51382e0f8e4SYosry Ahmed
51482e0f8e4SYosry Ahmed if (cur_pages >= max_pages) {
51582e0f8e4SYosry Ahmed zswap_pool_limit_hit++;
51682e0f8e4SYosry Ahmed zswap_pool_reached_full = true;
51782e0f8e4SYosry Ahmed } else if (zswap_pool_reached_full &&
51882e0f8e4SYosry Ahmed cur_pages <= zswap_accept_thr_pages()) {
51982e0f8e4SYosry Ahmed zswap_pool_reached_full = false;
52082e0f8e4SYosry Ahmed }
52182e0f8e4SYosry Ahmed return zswap_pool_reached_full;
52282e0f8e4SYosry Ahmed }
52382e0f8e4SYosry Ahmed
524abca07c0SJohannes Weiner /*********************************
525abca07c0SJohannes Weiner * param callbacks
526abca07c0SJohannes Weiner **********************************/
527abca07c0SJohannes Weiner
zswap_compressor_param_set(const char * val,const struct kernel_param * kp)5285c3f8be0SJohannes Weiner static int zswap_compressor_param_set(const char *val, const struct kernel_param *kp)
529abca07c0SJohannes Weiner {
530abca07c0SJohannes Weiner struct zswap_pool *pool, *put_pool = NULL;
531abca07c0SJohannes Weiner char *s = strstrip((char *)val);
5325c3f8be0SJohannes Weiner bool create_pool = false;
533abca07c0SJohannes Weiner int ret = 0;
534abca07c0SJohannes Weiner
535abca07c0SJohannes Weiner mutex_lock(&zswap_init_lock);
536abca07c0SJohannes Weiner switch (zswap_init_state) {
537abca07c0SJohannes Weiner case ZSWAP_UNINIT:
5385c3f8be0SJohannes Weiner /* Handled in zswap_setup() */
539abca07c0SJohannes Weiner ret = param_set_charp(s, kp);
540abca07c0SJohannes Weiner break;
541abca07c0SJohannes Weiner case ZSWAP_INIT_SUCCEED:
5425c3f8be0SJohannes Weiner if (!zswap_has_pool || strcmp(s, *(char **)kp->arg))
5435c3f8be0SJohannes Weiner create_pool = true;
544abca07c0SJohannes Weiner break;
545abca07c0SJohannes Weiner case ZSWAP_INIT_FAILED:
546abca07c0SJohannes Weiner pr_err("can't set param, initialization failed\n");
547abca07c0SJohannes Weiner ret = -ENODEV;
548abca07c0SJohannes Weiner }
549abca07c0SJohannes Weiner mutex_unlock(&zswap_init_lock);
550abca07c0SJohannes Weiner
5515c3f8be0SJohannes Weiner if (!create_pool)
552abca07c0SJohannes Weiner return ret;
553abca07c0SJohannes Weiner
554abca07c0SJohannes Weiner if (!crypto_has_acomp(s, 0, 0)) {
555abca07c0SJohannes Weiner pr_err("compressor %s not available\n", s);
556abca07c0SJohannes Weiner return -ENOENT;
557abca07c0SJohannes Weiner }
558abca07c0SJohannes Weiner
55994ace3feSChengming Zhou spin_lock_bh(&zswap_pools_lock);
560abca07c0SJohannes Weiner
5615c3f8be0SJohannes Weiner pool = zswap_pool_find_get(s);
562abca07c0SJohannes Weiner if (pool) {
563abca07c0SJohannes Weiner zswap_pool_debug("using existing", pool);
564abca07c0SJohannes Weiner WARN_ON(pool == zswap_pool_current());
565abca07c0SJohannes Weiner list_del_rcu(&pool->list);
566abca07c0SJohannes Weiner }
567abca07c0SJohannes Weiner
56894ace3feSChengming Zhou spin_unlock_bh(&zswap_pools_lock);
569abca07c0SJohannes Weiner
570abca07c0SJohannes Weiner if (!pool)
5715c3f8be0SJohannes Weiner pool = zswap_pool_create(s);
57294ace3feSChengming Zhou else {
57394ace3feSChengming Zhou /*
57494ace3feSChengming Zhou * Restore the initial ref dropped by percpu_ref_kill()
57594ace3feSChengming Zhou * when the pool was decommissioned and switch it again
57694ace3feSChengming Zhou * to percpu mode.
57794ace3feSChengming Zhou */
57894ace3feSChengming Zhou percpu_ref_resurrect(&pool->ref);
57994ace3feSChengming Zhou
58094ace3feSChengming Zhou /* Drop the ref from zswap_pool_find_get(). */
58194ace3feSChengming Zhou zswap_pool_put(pool);
58294ace3feSChengming Zhou }
583abca07c0SJohannes Weiner
584abca07c0SJohannes Weiner if (pool)
585abca07c0SJohannes Weiner ret = param_set_charp(s, kp);
586abca07c0SJohannes Weiner else
587abca07c0SJohannes Weiner ret = -EINVAL;
588abca07c0SJohannes Weiner
58994ace3feSChengming Zhou spin_lock_bh(&zswap_pools_lock);
590abca07c0SJohannes Weiner
591abca07c0SJohannes Weiner if (!ret) {
592abca07c0SJohannes Weiner put_pool = zswap_pool_current();
593abca07c0SJohannes Weiner list_add_rcu(&pool->list, &zswap_pools);
594abca07c0SJohannes Weiner zswap_has_pool = true;
595abca07c0SJohannes Weiner } else if (pool) {
5965c3f8be0SJohannes Weiner /*
5975c3f8be0SJohannes Weiner * Add the possibly pre-existing pool to the end of the pools
598abca07c0SJohannes Weiner * list; if it's new (and empty) then it'll be removed and
599abca07c0SJohannes Weiner * destroyed by the put after we drop the lock
600abca07c0SJohannes Weiner */
601abca07c0SJohannes Weiner list_add_tail_rcu(&pool->list, &zswap_pools);
602abca07c0SJohannes Weiner put_pool = pool;
603abca07c0SJohannes Weiner }
604abca07c0SJohannes Weiner
60594ace3feSChengming Zhou spin_unlock_bh(&zswap_pools_lock);
606abca07c0SJohannes Weiner
6075c3f8be0SJohannes Weiner /*
6085c3f8be0SJohannes Weiner * Drop the ref from either the old current pool,
609abca07c0SJohannes Weiner * or the new pool we failed to add
610abca07c0SJohannes Weiner */
611abca07c0SJohannes Weiner if (put_pool)
61294ace3feSChengming Zhou percpu_ref_kill(&put_pool->ref);
613abca07c0SJohannes Weiner
614abca07c0SJohannes Weiner return ret;
615abca07c0SJohannes Weiner }
616abca07c0SJohannes Weiner
zswap_enabled_param_set(const char * val,const struct kernel_param * kp)617abca07c0SJohannes Weiner static int zswap_enabled_param_set(const char *val,
618abca07c0SJohannes Weiner const struct kernel_param *kp)
619abca07c0SJohannes Weiner {
620abca07c0SJohannes Weiner int ret = -ENODEV;
621abca07c0SJohannes Weiner
622abca07c0SJohannes Weiner /* if this is load-time (pre-init) param setting, only set param. */
623abca07c0SJohannes Weiner if (system_state != SYSTEM_RUNNING)
624abca07c0SJohannes Weiner return param_set_bool(val, kp);
625abca07c0SJohannes Weiner
626abca07c0SJohannes Weiner mutex_lock(&zswap_init_lock);
627abca07c0SJohannes Weiner switch (zswap_init_state) {
628abca07c0SJohannes Weiner case ZSWAP_UNINIT:
629abca07c0SJohannes Weiner if (zswap_setup())
630abca07c0SJohannes Weiner break;
631abca07c0SJohannes Weiner fallthrough;
632abca07c0SJohannes Weiner case ZSWAP_INIT_SUCCEED:
633abca07c0SJohannes Weiner if (!zswap_has_pool)
634abca07c0SJohannes Weiner pr_err("can't enable, no pool configured\n");
635abca07c0SJohannes Weiner else
636abca07c0SJohannes Weiner ret = param_set_bool(val, kp);
637abca07c0SJohannes Weiner break;
638abca07c0SJohannes Weiner case ZSWAP_INIT_FAILED:
639abca07c0SJohannes Weiner pr_err("can't enable, initialization failed\n");
640abca07c0SJohannes Weiner }
641abca07c0SJohannes Weiner mutex_unlock(&zswap_init_lock);
642abca07c0SJohannes Weiner
643abca07c0SJohannes Weiner return ret;
644abca07c0SJohannes Weiner }
645abca07c0SJohannes Weiner
646506a86c5SJohannes Weiner /*********************************
647506a86c5SJohannes Weiner * lru functions
648506a86c5SJohannes Weiner **********************************/
649506a86c5SJohannes Weiner
650a65b0e76SDomenico Cerasuolo /* should be called under RCU */
651a65b0e76SDomenico Cerasuolo #ifdef CONFIG_MEMCG
mem_cgroup_from_entry(struct zswap_entry * entry)652a65b0e76SDomenico Cerasuolo static inline struct mem_cgroup *mem_cgroup_from_entry(struct zswap_entry *entry)
653a65b0e76SDomenico Cerasuolo {
654a65b0e76SDomenico Cerasuolo return entry->objcg ? obj_cgroup_memcg(entry->objcg) : NULL;
655a65b0e76SDomenico Cerasuolo }
656a65b0e76SDomenico Cerasuolo #else
mem_cgroup_from_entry(struct zswap_entry * entry)657a65b0e76SDomenico Cerasuolo static inline struct mem_cgroup *mem_cgroup_from_entry(struct zswap_entry *entry)
658a65b0e76SDomenico Cerasuolo {
659a65b0e76SDomenico Cerasuolo return NULL;
660a65b0e76SDomenico Cerasuolo }
661a65b0e76SDomenico Cerasuolo #endif
662a65b0e76SDomenico Cerasuolo
entry_to_nid(struct zswap_entry * entry)663a65b0e76SDomenico Cerasuolo static inline int entry_to_nid(struct zswap_entry *entry)
664a65b0e76SDomenico Cerasuolo {
665a65b0e76SDomenico Cerasuolo return page_to_nid(virt_to_page(entry));
666a65b0e76SDomenico Cerasuolo }
667a65b0e76SDomenico Cerasuolo
zswap_lru_add(struct list_lru * list_lru,struct zswap_entry * entry)668a65b0e76SDomenico Cerasuolo static void zswap_lru_add(struct list_lru *list_lru, struct zswap_entry *entry)
669a65b0e76SDomenico Cerasuolo {
670a65b0e76SDomenico Cerasuolo int nid = entry_to_nid(entry);
671a65b0e76SDomenico Cerasuolo struct mem_cgroup *memcg;
672a65b0e76SDomenico Cerasuolo
673a65b0e76SDomenico Cerasuolo /*
674a65b0e76SDomenico Cerasuolo * Note that it is safe to use rcu_read_lock() here, even in the face of
67528e98022SKairui Song * concurrent memcg offlining:
676a65b0e76SDomenico Cerasuolo *
677fb56fdf8SKairui Song * 1. list_lru_add() is called before list_lru_one is dead. The
678a65b0e76SDomenico Cerasuolo * new entry will be reparented to memcg's parent's list_lru.
679fb56fdf8SKairui Song * 2. list_lru_add() is called after list_lru_one is dead. The
680a65b0e76SDomenico Cerasuolo * new entry will be added directly to memcg's parent's list_lru.
681a65b0e76SDomenico Cerasuolo *
6823f798aa6SChengming Zhou * Similar reasoning holds for list_lru_del().
683a65b0e76SDomenico Cerasuolo */
684a65b0e76SDomenico Cerasuolo rcu_read_lock();
685a65b0e76SDomenico Cerasuolo memcg = mem_cgroup_from_entry(entry);
686a65b0e76SDomenico Cerasuolo /* will always succeed */
687a65b0e76SDomenico Cerasuolo list_lru_add(list_lru, &entry->lru, nid, memcg);
688a65b0e76SDomenico Cerasuolo rcu_read_unlock();
689a65b0e76SDomenico Cerasuolo }
690a65b0e76SDomenico Cerasuolo
zswap_lru_del(struct list_lru * list_lru,struct zswap_entry * entry)691a65b0e76SDomenico Cerasuolo static void zswap_lru_del(struct list_lru *list_lru, struct zswap_entry *entry)
692a65b0e76SDomenico Cerasuolo {
693a65b0e76SDomenico Cerasuolo int nid = entry_to_nid(entry);
694a65b0e76SDomenico Cerasuolo struct mem_cgroup *memcg;
695a65b0e76SDomenico Cerasuolo
696a65b0e76SDomenico Cerasuolo rcu_read_lock();
697a65b0e76SDomenico Cerasuolo memcg = mem_cgroup_from_entry(entry);
698a65b0e76SDomenico Cerasuolo /* will always succeed */
699a65b0e76SDomenico Cerasuolo list_lru_del(list_lru, &entry->lru, nid, memcg);
700a65b0e76SDomenico Cerasuolo rcu_read_unlock();
701a65b0e76SDomenico Cerasuolo }
702a65b0e76SDomenico Cerasuolo
zswap_lruvec_state_init(struct lruvec * lruvec)7035182661aSJohannes Weiner void zswap_lruvec_state_init(struct lruvec *lruvec)
7045182661aSJohannes Weiner {
705e31c38e0SNhat Pham atomic_long_set(&lruvec->zswap_lruvec_state.nr_disk_swapins, 0);
7065182661aSJohannes Weiner }
7075182661aSJohannes Weiner
zswap_folio_swapin(struct folio * folio)7085182661aSJohannes Weiner void zswap_folio_swapin(struct folio *folio)
7095182661aSJohannes Weiner {
7105182661aSJohannes Weiner struct lruvec *lruvec;
7115182661aSJohannes Weiner
7125182661aSJohannes Weiner if (folio) {
713d5ddaf43SMuchun Song rcu_read_lock();
7145182661aSJohannes Weiner lruvec = folio_lruvec(folio);
715e31c38e0SNhat Pham atomic_long_inc(&lruvec->zswap_lruvec_state.nr_disk_swapins);
716d5ddaf43SMuchun Song rcu_read_unlock();
7175182661aSJohannes Weiner }
7185182661aSJohannes Weiner }
7195182661aSJohannes Weiner
720c5519e0aSTakero Funaki /*
721c5519e0aSTakero Funaki * This function should be called when a memcg is being offlined.
722c5519e0aSTakero Funaki *
723c5519e0aSTakero Funaki * Since the global shrinker shrink_worker() may hold a reference
724c5519e0aSTakero Funaki * of the memcg, we must check and release the reference in
725c5519e0aSTakero Funaki * zswap_next_shrink.
726c5519e0aSTakero Funaki *
727c5519e0aSTakero Funaki * shrink_worker() must handle the case where this function releases
728c5519e0aSTakero Funaki * the reference of memcg being shrunk.
729c5519e0aSTakero Funaki */
zswap_memcg_offline_cleanup(struct mem_cgroup * memcg)7305182661aSJohannes Weiner void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg)
7315182661aSJohannes Weiner {
732bf9b7df2SChengming Zhou /* lock out zswap shrinker walking memcg tree */
733e35606e4SChengming Zhou spin_lock(&zswap_shrink_lock);
734c5519e0aSTakero Funaki if (zswap_next_shrink == memcg) {
735c5519e0aSTakero Funaki do {
736e35606e4SChengming Zhou zswap_next_shrink = mem_cgroup_iter(NULL, zswap_next_shrink, NULL);
737c5519e0aSTakero Funaki } while (zswap_next_shrink && !mem_cgroup_online(zswap_next_shrink));
738c5519e0aSTakero Funaki }
739e35606e4SChengming Zhou spin_unlock(&zswap_shrink_lock);
7405182661aSJohannes Weiner }
7415182661aSJohannes Weiner
7425182661aSJohannes Weiner /*********************************
74336034bf6SJohannes Weiner * zswap entry functions
74436034bf6SJohannes Weiner **********************************/
74536034bf6SJohannes Weiner static struct kmem_cache *zswap_entry_cache;
74636034bf6SJohannes Weiner
zswap_entry_cache_alloc(gfp_t gfp,int nid)74736034bf6SJohannes Weiner static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp, int nid)
74836034bf6SJohannes Weiner {
74936034bf6SJohannes Weiner struct zswap_entry *entry;
75036034bf6SJohannes Weiner entry = kmem_cache_alloc_node(zswap_entry_cache, gfp, nid);
75136034bf6SJohannes Weiner if (!entry)
75236034bf6SJohannes Weiner return NULL;
75336034bf6SJohannes Weiner return entry;
75436034bf6SJohannes Weiner }
75536034bf6SJohannes Weiner
zswap_entry_cache_free(struct zswap_entry * entry)75636034bf6SJohannes Weiner static void zswap_entry_cache_free(struct zswap_entry *entry)
75736034bf6SJohannes Weiner {
75836034bf6SJohannes Weiner kmem_cache_free(zswap_entry_cache, entry);
75936034bf6SJohannes Weiner }
76036034bf6SJohannes Weiner
7610ab0abcfSWeijie Yang /*
7625c3f8be0SJohannes Weiner * Carries out the common pattern of freeing an entry's zsmalloc allocation,
7630ab0abcfSWeijie Yang * freeing the entry itself, and decrementing the number of stored pages.
7640ab0abcfSWeijie Yang */
zswap_entry_free(struct zswap_entry * entry)76542398be2SJohannes Weiner static void zswap_entry_free(struct zswap_entry *entry)
7660ab0abcfSWeijie Yang {
767e35606e4SChengming Zhou zswap_lru_del(&zswap_list_lru, entry);
7685c3f8be0SJohannes Weiner zs_free(entry->pool->zs_pool, entry->handle);
769f1c54846SDan Streetman zswap_pool_put(entry->pool);
7702e601e1eSJohannes Weiner if (entry->objcg) {
7712e601e1eSJohannes Weiner obj_cgroup_uncharge_zswap(entry->objcg, entry->length);
7722e601e1eSJohannes Weiner obj_cgroup_put(entry->objcg);
7732e601e1eSJohannes Weiner }
774dca4437aSSeongJae Park if (entry->length == PAGE_SIZE)
775dca4437aSSeongJae Park atomic_long_dec(&zswap_stored_incompressible_pages);
7760ab0abcfSWeijie Yang zswap_entry_cache_free(entry);
7776e1fa555SKanchana P Sridhar atomic_long_dec(&zswap_stored_pages);
7780ab0abcfSWeijie Yang }
7790ab0abcfSWeijie Yang
7802b281117SSeth Jennings /*********************************
781f91e81d3SJohannes Weiner * compressed storage functions
782f91e81d3SJohannes Weiner **********************************/
zswap_cpu_comp_prepare(unsigned int cpu,struct hlist_node * node)78364f200b8SJohannes Weiner static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
78464f200b8SJohannes Weiner {
78564f200b8SJohannes Weiner struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
78664f200b8SJohannes Weiner struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu);
787*ef3c0f6cSKanchana P. Sridhar int ret = -ENOMEM;
78864f200b8SJohannes Weiner
789*ef3c0f6cSKanchana P. Sridhar /*
790*ef3c0f6cSKanchana P. Sridhar * To handle cases where the CPU goes through online-offline-online
791*ef3c0f6cSKanchana P. Sridhar * transitions, we return if the acomp_ctx has already been initialized.
792*ef3c0f6cSKanchana P. Sridhar */
793*ef3c0f6cSKanchana P. Sridhar if (acomp_ctx->acomp) {
794*ef3c0f6cSKanchana P. Sridhar WARN_ON_ONCE(IS_ERR(acomp_ctx->acomp));
795*ef3c0f6cSKanchana P. Sridhar return 0;
79612dcb0efSYosry Ahmed }
79764f200b8SJohannes Weiner
798*ef3c0f6cSKanchana P. Sridhar acomp_ctx->buffer = kmalloc_node(PAGE_SIZE, GFP_KERNEL, cpu_to_node(cpu));
799*ef3c0f6cSKanchana P. Sridhar if (!acomp_ctx->buffer)
800*ef3c0f6cSKanchana P. Sridhar return ret;
801*ef3c0f6cSKanchana P. Sridhar
8021556478eSKanchana P. Sridhar /*
8031556478eSKanchana P. Sridhar * In case of an error, crypto_alloc_acomp_node() returns an
8041556478eSKanchana P. Sridhar * error pointer, never NULL.
8051556478eSKanchana P. Sridhar */
806*ef3c0f6cSKanchana P. Sridhar acomp_ctx->acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, cpu_to_node(cpu));
807*ef3c0f6cSKanchana P. Sridhar if (IS_ERR(acomp_ctx->acomp)) {
808c69ca4e9SSahil Chandna pr_err("could not alloc crypto acomp %s : %pe\n",
809*ef3c0f6cSKanchana P. Sridhar pool->tfm_name, acomp_ctx->acomp);
810*ef3c0f6cSKanchana P. Sridhar ret = PTR_ERR(acomp_ctx->acomp);
811779b9955SYosry Ahmed goto fail;
81264f200b8SJohannes Weiner }
81364f200b8SJohannes Weiner
8141556478eSKanchana P. Sridhar /* acomp_request_alloc() returns NULL in case of an error. */
815*ef3c0f6cSKanchana P. Sridhar acomp_ctx->req = acomp_request_alloc(acomp_ctx->acomp);
816*ef3c0f6cSKanchana P. Sridhar if (!acomp_ctx->req) {
81764f200b8SJohannes Weiner pr_err("could not alloc crypto acomp_request %s\n",
81864f200b8SJohannes Weiner pool->tfm_name);
819779b9955SYosry Ahmed goto fail;
82064f200b8SJohannes Weiner }
82164f200b8SJohannes Weiner
82264f200b8SJohannes Weiner crypto_init_wait(&acomp_ctx->wait);
823779b9955SYosry Ahmed
82464f200b8SJohannes Weiner /*
82564f200b8SJohannes Weiner * if the backend of acomp is async zip, crypto_req_done() will wakeup
82664f200b8SJohannes Weiner * crypto_wait_req(); if the backend of acomp is scomp, the callback
82764f200b8SJohannes Weiner * won't be called, crypto_wait_req() will return without blocking.
82864f200b8SJohannes Weiner */
829*ef3c0f6cSKanchana P. Sridhar acomp_request_set_callback(acomp_ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
83064f200b8SJohannes Weiner crypto_req_done, &acomp_ctx->wait);
83164f200b8SJohannes Weiner
832*ef3c0f6cSKanchana P. Sridhar mutex_init(&acomp_ctx->mutex);
83364f200b8SJohannes Weiner return 0;
83464f200b8SJohannes Weiner
835779b9955SYosry Ahmed fail:
836*ef3c0f6cSKanchana P. Sridhar acomp_ctx_free(acomp_ctx);
83764f200b8SJohannes Weiner return ret;
83864f200b8SJohannes Weiner }
83964f200b8SJohannes Weiner
zswap_compress(struct page * page,struct zswap_entry * entry,struct zswap_pool * pool)840ed882addSKanchana P Sridhar static bool zswap_compress(struct page *page, struct zswap_entry *entry,
841ed882addSKanchana P Sridhar struct zswap_pool *pool)
842f91e81d3SJohannes Weiner {
843f91e81d3SJohannes Weiner struct crypto_acomp_ctx *acomp_ctx;
844f91e81d3SJohannes Weiner struct scatterlist input, output;
84555e78c93SBarry Song int comp_ret = 0, alloc_ret = 0;
846f91e81d3SJohannes Weiner unsigned int dlen = PAGE_SIZE;
847f91e81d3SJohannes Weiner unsigned long handle;
848f91e81d3SJohannes Weiner gfp_t gfp;
849f91e81d3SJohannes Weiner u8 *dst;
850dca4437aSSeongJae Park bool mapped = false;
851f91e81d3SJohannes Weiner
852*ef3c0f6cSKanchana P. Sridhar acomp_ctx = raw_cpu_ptr(pool->acomp_ctx);
853*ef3c0f6cSKanchana P. Sridhar mutex_lock(&acomp_ctx->mutex);
854*ef3c0f6cSKanchana P. Sridhar
855f91e81d3SJohannes Weiner dst = acomp_ctx->buffer;
856f91e81d3SJohannes Weiner sg_init_table(&input, 1);
8573d0f560aSKanchana P Sridhar sg_set_page(&input, page, PAGE_SIZE, 0);
858f91e81d3SJohannes Weiner
8590b1bf60cSNhat Pham sg_init_one(&output, dst, PAGE_SIZE);
860f91e81d3SJohannes Weiner acomp_request_set_params(acomp_ctx->req, &input, &output, PAGE_SIZE, dlen);
861f91e81d3SJohannes Weiner
862f91e81d3SJohannes Weiner /*
863f91e81d3SJohannes Weiner * it maybe looks a little bit silly that we send an asynchronous request,
864f91e81d3SJohannes Weiner * then wait for its completion synchronously. This makes the process look
865f91e81d3SJohannes Weiner * synchronous in fact.
866f91e81d3SJohannes Weiner * Theoretically, acomp supports users send multiple acomp requests in one
867f91e81d3SJohannes Weiner * acomp instance, then get those requests done simultaneously. but in this
868f91e81d3SJohannes Weiner * case, zswap actually does store and load page by page, there is no
869f91e81d3SJohannes Weiner * existing method to send the second page before the first page is done
870f7ed6bf2SSeongJae Park * in one thread doing zswap.
871f91e81d3SJohannes Weiner * but in different threads running on different cpu, we have different
872f91e81d3SJohannes Weiner * acomp instance, so multiple threads can do (de)compression in parallel.
873f91e81d3SJohannes Weiner */
87455e78c93SBarry Song comp_ret = crypto_wait_req(crypto_acomp_compress(acomp_ctx->req), &acomp_ctx->wait);
875f91e81d3SJohannes Weiner dlen = acomp_ctx->req->dlen;
876dca4437aSSeongJae Park
877dca4437aSSeongJae Park /*
878dca4437aSSeongJae Park * If a page cannot be compressed into a size smaller than PAGE_SIZE,
879dca4437aSSeongJae Park * save the content as is without a compression, to keep the LRU order
880dca4437aSSeongJae Park * of writebacks. If writeback is disabled, reject the page since it
881dca4437aSSeongJae Park * only adds metadata overhead. swap_writeout() will put the page back
882dca4437aSSeongJae Park * to the active LRU list in the case.
883dca4437aSSeongJae Park */
884dca4437aSSeongJae Park if (comp_ret || !dlen || dlen >= PAGE_SIZE) {
885cf4d6ad5SQi Zheng rcu_read_lock();
886dca4437aSSeongJae Park if (!mem_cgroup_zswap_writeback_enabled(
887dca4437aSSeongJae Park folio_memcg(page_folio(page)))) {
888cf4d6ad5SQi Zheng rcu_read_unlock();
889dca4437aSSeongJae Park comp_ret = comp_ret ? comp_ret : -EINVAL;
890f91e81d3SJohannes Weiner goto unlock;
891dca4437aSSeongJae Park }
892cf4d6ad5SQi Zheng rcu_read_unlock();
893dca4437aSSeongJae Park comp_ret = 0;
894dca4437aSSeongJae Park dlen = PAGE_SIZE;
895dca4437aSSeongJae Park dst = kmap_local_page(page);
896dca4437aSSeongJae Park mapped = true;
897dca4437aSSeongJae Park }
898f91e81d3SJohannes Weiner
8997b600411SYosry Ahmed gfp = GFP_NOWAIT | __GFP_NORETRY | __GFP_HIGHMEM | __GFP_MOVABLE;
9005c3f8be0SJohannes Weiner handle = zs_malloc(pool->zs_pool, dlen, gfp, page_to_nid(page));
9015c3f8be0SJohannes Weiner if (IS_ERR_VALUE(handle)) {
9025c3f8be0SJohannes Weiner alloc_ret = PTR_ERR((void *)handle);
903f91e81d3SJohannes Weiner goto unlock;
9045c3f8be0SJohannes Weiner }
905f91e81d3SJohannes Weiner
9065c3f8be0SJohannes Weiner zs_obj_write(pool->zs_pool, handle, dst, dlen);
907f91e81d3SJohannes Weiner entry->handle = handle;
908f91e81d3SJohannes Weiner entry->length = dlen;
909f91e81d3SJohannes Weiner
910f91e81d3SJohannes Weiner unlock:
911dca4437aSSeongJae Park if (mapped)
912dca4437aSSeongJae Park kunmap_local(dst);
91355e78c93SBarry Song if (comp_ret == -ENOSPC || alloc_ret == -ENOSPC)
91455e78c93SBarry Song zswap_reject_compress_poor++;
91555e78c93SBarry Song else if (comp_ret)
91655e78c93SBarry Song zswap_reject_compress_fail++;
91755e78c93SBarry Song else if (alloc_ret)
91855e78c93SBarry Song zswap_reject_alloc_fail++;
91955e78c93SBarry Song
920*ef3c0f6cSKanchana P. Sridhar mutex_unlock(&acomp_ctx->mutex);
92155e78c93SBarry Song return comp_ret == 0 && alloc_ret == 0;
922f91e81d3SJohannes Weiner }
923f91e81d3SJohannes Weiner
zswap_decompress(struct zswap_entry * entry,struct folio * folio)924ff22f929SNhat Pham static bool zswap_decompress(struct zswap_entry *entry, struct folio *folio)
925f91e81d3SJohannes Weiner {
9265c3f8be0SJohannes Weiner struct zswap_pool *pool = entry->pool;
927e2c3b6b2SYosry Ahmed struct scatterlist input[2]; /* zsmalloc returns an SG list 1-2 entries */
928e2c3b6b2SYosry Ahmed struct scatterlist output;
929f91e81d3SJohannes Weiner struct crypto_acomp_ctx *acomp_ctx;
930e2c3b6b2SYosry Ahmed int ret = 0, dlen;
931f91e81d3SJohannes Weiner
932*ef3c0f6cSKanchana P. Sridhar acomp_ctx = raw_cpu_ptr(pool->acomp_ctx);
933*ef3c0f6cSKanchana P. Sridhar mutex_lock(&acomp_ctx->mutex);
934e2c3b6b2SYosry Ahmed zs_obj_read_sg_begin(pool->zs_pool, entry->handle, input, entry->length);
9357d4c9629SYosry Ahmed
936dca4437aSSeongJae Park /* zswap entries of length PAGE_SIZE are not compressed. */
937dca4437aSSeongJae Park if (entry->length == PAGE_SIZE) {
938631c1111SLorenzo Stoakes (Oracle) void *dst;
939631c1111SLorenzo Stoakes (Oracle)
940e2c3b6b2SYosry Ahmed WARN_ON_ONCE(input->length != PAGE_SIZE);
941631c1111SLorenzo Stoakes (Oracle)
942631c1111SLorenzo Stoakes (Oracle) dst = kmap_local_folio(folio, 0);
943631c1111SLorenzo Stoakes (Oracle) memcpy_from_sglist(dst, input, 0, PAGE_SIZE);
944e2c3b6b2SYosry Ahmed dlen = PAGE_SIZE;
945631c1111SLorenzo Stoakes (Oracle) kunmap_local(dst);
946631c1111SLorenzo Stoakes (Oracle) flush_dcache_folio(folio);
9477d4c9629SYosry Ahmed } else {
948f91e81d3SJohannes Weiner sg_init_table(&output, 1);
9495d19f5deSYosry Ahmed sg_set_folio(&output, folio, PAGE_SIZE, 0);
950e2c3b6b2SYosry Ahmed acomp_request_set_params(acomp_ctx->req, input, &output,
951e2c3b6b2SYosry Ahmed entry->length, PAGE_SIZE);
952e2c3b6b2SYosry Ahmed ret = crypto_acomp_decompress(acomp_ctx->req);
953e2c3b6b2SYosry Ahmed ret = crypto_wait_req(ret, &acomp_ctx->wait);
954ff22f929SNhat Pham dlen = acomp_ctx->req->dlen;
955e2c3b6b2SYosry Ahmed }
956f91e81d3SJohannes Weiner
957e2c3b6b2SYosry Ahmed zs_obj_read_sg_end(pool->zs_pool, entry->handle);
958*ef3c0f6cSKanchana P. Sridhar mutex_unlock(&acomp_ctx->mutex);
959ff22f929SNhat Pham
960e2c3b6b2SYosry Ahmed if (!ret && dlen == PAGE_SIZE)
961ff22f929SNhat Pham return true;
962ff22f929SNhat Pham
963ff22f929SNhat Pham zswap_decompress_fail++;
964ff22f929SNhat Pham pr_alert_ratelimited("Decompression error from zswap (%d:%lu %s %u->%d)\n",
965ff22f929SNhat Pham swp_type(entry->swpentry),
966ff22f929SNhat Pham swp_offset(entry->swpentry),
967e2c3b6b2SYosry Ahmed entry->pool->tfm_name,
968e2c3b6b2SYosry Ahmed entry->length, dlen);
969ff22f929SNhat Pham return false;
970f91e81d3SJohannes Weiner }
971f91e81d3SJohannes Weiner
972f91e81d3SJohannes Weiner /*********************************
9739986d35dSJohannes Weiner * writeback code
9749986d35dSJohannes Weiner **********************************/
9759986d35dSJohannes Weiner /*
9769986d35dSJohannes Weiner * Attempts to free an entry by adding a folio to the swap cache,
9779986d35dSJohannes Weiner * decompressing the entry data into the folio, and issuing a
9789986d35dSJohannes Weiner * bio write to write the folio back to the swap device.
9799986d35dSJohannes Weiner *
9809986d35dSJohannes Weiner * This can be thought of as a "resumed writeback" of the folio
9819986d35dSJohannes Weiner * to the swap device. We are basically resuming the same swap
9829986d35dSJohannes Weiner * writeback path that was intercepted with the zswap_store()
9839986d35dSJohannes Weiner * in the first place. After the folio has been decompressed into
9849986d35dSJohannes Weiner * the swap cache, the compressed version stored by zswap can be
9859986d35dSJohannes Weiner * freed.
9869986d35dSJohannes Weiner */
zswap_writeback_entry(struct zswap_entry * entry,swp_entry_t swpentry)9879986d35dSJohannes Weiner static int zswap_writeback_entry(struct zswap_entry *entry,
9889986d35dSJohannes Weiner swp_entry_t swpentry)
9899986d35dSJohannes Weiner {
990796c2c23SChris Li struct xarray *tree;
991796c2c23SChris Li pgoff_t offset = swp_offset(swpentry);
9929986d35dSJohannes Weiner struct folio *folio;
9939986d35dSJohannes Weiner struct mempolicy *mpol;
9949986d35dSJohannes Weiner bool folio_was_allocated;
99578524b05SKairui Song struct swap_info_struct *si;
996ff22f929SNhat Pham int ret = 0;
9979986d35dSJohannes Weiner
9989986d35dSJohannes Weiner /* try to allocate swap cache folio */
99978524b05SKairui Song si = get_swap_device(swpentry);
100078524b05SKairui Song if (!si)
100178524b05SKairui Song return -EEXIST;
100278524b05SKairui Song
10039986d35dSJohannes Weiner mpol = get_task_policy(current);
1004d7cf0d54SKairui Song folio = swap_cache_alloc_folio(swpentry, GFP_KERNEL, mpol,
1005de85024bSKairui Song NO_INTERLEAVE_INDEX, &folio_was_allocated);
100678524b05SKairui Song put_swap_device(si);
10079986d35dSJohannes Weiner if (!folio)
10089986d35dSJohannes Weiner return -ENOMEM;
10099986d35dSJohannes Weiner
10109986d35dSJohannes Weiner /*
10119986d35dSJohannes Weiner * Found an existing folio, we raced with swapin or concurrent
10129986d35dSJohannes Weiner * shrinker. We generally writeback cold folios from zswap, and
10139986d35dSJohannes Weiner * swapin means the folio just became hot, so skip this folio.
10149986d35dSJohannes Weiner * For unlikely concurrent shrinker case, it will be unlinked
10159986d35dSJohannes Weiner * and freed when invalidated by the concurrent shrinker anyway.
10169986d35dSJohannes Weiner */
10179986d35dSJohannes Weiner if (!folio_was_allocated) {
1018ff22f929SNhat Pham ret = -EEXIST;
1019ff22f929SNhat Pham goto out;
10209986d35dSJohannes Weiner }
10219986d35dSJohannes Weiner
10229986d35dSJohannes Weiner /*
10239986d35dSJohannes Weiner * folio is locked, and the swapcache is now secured against
1024f9c0f1c3SChengming Zhou * concurrent swapping to and from the slot, and concurrent
1025f9c0f1c3SChengming Zhou * swapoff so we can safely dereference the zswap tree here.
1026f9c0f1c3SChengming Zhou * Verify that the swap entry hasn't been invalidated and recycled
1027f9c0f1c3SChengming Zhou * behind our backs, to avoid overwriting a new swap folio with
1028f9c0f1c3SChengming Zhou * old compressed data. Only when this is successful can the entry
1029f9c0f1c3SChengming Zhou * be dereferenced.
10309986d35dSJohannes Weiner */
10319986d35dSJohannes Weiner tree = swap_zswap_tree(swpentry);
1032ff22f929SNhat Pham if (entry != xa_load(tree, offset)) {
1033ff22f929SNhat Pham ret = -ENOMEM;
1034ff22f929SNhat Pham goto out;
10359986d35dSJohannes Weiner }
10369986d35dSJohannes Weiner
1037ff22f929SNhat Pham if (!zswap_decompress(entry, folio)) {
1038ff22f929SNhat Pham ret = -EIO;
1039ff22f929SNhat Pham goto out;
1040ff22f929SNhat Pham }
1041ff22f929SNhat Pham
1042ff22f929SNhat Pham xa_erase(tree, offset);
10439986d35dSJohannes Weiner
10449986d35dSJohannes Weiner count_vm_event(ZSWPWB);
10459986d35dSJohannes Weiner if (entry->objcg)
1046e7ac4daeSBarry Song count_objcg_events(entry->objcg, ZSWPWB, 1);
10479986d35dSJohannes Weiner
1048a230c20eSChengming Zhou zswap_entry_free(entry);
10499986d35dSJohannes Weiner
10509986d35dSJohannes Weiner /* folio is up to date */
10519986d35dSJohannes Weiner folio_mark_uptodate(folio);
10529986d35dSJohannes Weiner
10539986d35dSJohannes Weiner /* move it to the tail of the inactive list after end_writeback */
10549986d35dSJohannes Weiner folio_set_reclaim(folio);
10559986d35dSJohannes Weiner
10569986d35dSJohannes Weiner /* start writeback */
10572ba8ffceSChristoph Hellwig __swap_writepage(folio, NULL);
10589986d35dSJohannes Weiner
1059ff22f929SNhat Pham out:
1060ff22f929SNhat Pham if (ret && ret != -EEXIST) {
1061fd8d4f86SKairui Song swap_cache_del_folio(folio);
1062ff22f929SNhat Pham folio_unlock(folio);
1063ff22f929SNhat Pham }
1064ff22f929SNhat Pham folio_put(folio);
1065ff22f929SNhat Pham return ret;
10669986d35dSJohannes Weiner }
10679986d35dSJohannes Weiner
10689986d35dSJohannes Weiner /*********************************
1069b5ba474fSNhat Pham * shrinker functions
1070b5ba474fSNhat Pham **********************************/
1071e31c38e0SNhat Pham /*
1072e31c38e0SNhat Pham * The dynamic shrinker is modulated by the following factors:
1073e31c38e0SNhat Pham *
1074e31c38e0SNhat Pham * 1. Each zswap entry has a referenced bit, which the shrinker unsets (giving
1075e31c38e0SNhat Pham * the entry a second chance) before rotating it in the LRU list. If the
1076e31c38e0SNhat Pham * entry is considered again by the shrinker, with its referenced bit unset,
1077e31c38e0SNhat Pham * it is written back. The writeback rate as a result is dynamically
1078e31c38e0SNhat Pham * adjusted by the pool activities - if the pool is dominated by new entries
1079e31c38e0SNhat Pham * (i.e lots of recent zswapouts), these entries will be protected and
1080e31c38e0SNhat Pham * the writeback rate will slow down. On the other hand, if the pool has a
1081e31c38e0SNhat Pham * lot of stagnant entries, these entries will be reclaimed immediately,
1082e31c38e0SNhat Pham * effectively increasing the writeback rate.
1083e31c38e0SNhat Pham *
1084e31c38e0SNhat Pham * 2. Swapins counter: If we observe swapins, it is a sign that we are
1085e31c38e0SNhat Pham * overshrinking and should slow down. We maintain a swapins counter, which
1086e31c38e0SNhat Pham * is consumed and subtract from the number of eligible objects on the LRU
1087e31c38e0SNhat Pham * in zswap_shrinker_count().
1088e31c38e0SNhat Pham *
1089e31c38e0SNhat Pham * 3. Compression ratio. The better the workload compresses, the less gains we
1090e31c38e0SNhat Pham * can expect from writeback. We scale down the number of objects available
1091e31c38e0SNhat Pham * for reclaim by this ratio.
1092e31c38e0SNhat Pham */
shrink_memcg_cb(struct list_head * item,struct list_lru_one * l,void * arg)1093b5ba474fSNhat Pham static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_one *l,
1094da0c0251SKairui Song void *arg)
1095eb23ee4fSJohannes Weiner {
1096eb23ee4fSJohannes Weiner struct zswap_entry *entry = container_of(item, struct zswap_entry, lru);
1097eb23ee4fSJohannes Weiner bool *encountered_page_in_swapcache = (bool *)arg;
1098eb23ee4fSJohannes Weiner swp_entry_t swpentry;
1099eb23ee4fSJohannes Weiner enum lru_status ret = LRU_REMOVED_RETRY;
1100eb23ee4fSJohannes Weiner int writeback_result;
1101eb23ee4fSJohannes Weiner
1102eb23ee4fSJohannes Weiner /*
1103e31c38e0SNhat Pham * Second chance algorithm: if the entry has its referenced bit set, give it
1104e31c38e0SNhat Pham * a second chance. Only clear the referenced bit and rotate it in the
1105e31c38e0SNhat Pham * zswap's LRU list.
1106e31c38e0SNhat Pham */
1107e31c38e0SNhat Pham if (entry->referenced) {
1108e31c38e0SNhat Pham entry->referenced = false;
1109e31c38e0SNhat Pham return LRU_ROTATE;
1110e31c38e0SNhat Pham }
1111e31c38e0SNhat Pham
1112e31c38e0SNhat Pham /*
1113f9c0f1c3SChengming Zhou * As soon as we drop the LRU lock, the entry can be freed by
1114f9c0f1c3SChengming Zhou * a concurrent invalidation. This means the following:
1115eb23ee4fSJohannes Weiner *
1116f9c0f1c3SChengming Zhou * 1. We extract the swp_entry_t to the stack, allowing
1117f9c0f1c3SChengming Zhou * zswap_writeback_entry() to pin the swap entry and
1118f7ed6bf2SSeongJae Park * then validate the zswap entry against that swap entry's
1119f9c0f1c3SChengming Zhou * tree using pointer value comparison. Only when that
1120f9c0f1c3SChengming Zhou * is successful can the entry be dereferenced.
1121f9c0f1c3SChengming Zhou *
1122f9c0f1c3SChengming Zhou * 2. Usually, objects are taken off the LRU for reclaim. In
1123f9c0f1c3SChengming Zhou * this case this isn't possible, because if reclaim fails
1124f9c0f1c3SChengming Zhou * for whatever reason, we have no means of knowing if the
1125f9c0f1c3SChengming Zhou * entry is alive to put it back on the LRU.
1126f9c0f1c3SChengming Zhou *
1127f9c0f1c3SChengming Zhou * So rotate it before dropping the lock. If the entry is
1128f9c0f1c3SChengming Zhou * written back or invalidated, the free path will unlink
1129f9c0f1c3SChengming Zhou * it. For failures, rotation is the right thing as well.
1130eb23ee4fSJohannes Weiner *
1131eb23ee4fSJohannes Weiner * Temporary failures, where the same entry should be tried
1132eb23ee4fSJohannes Weiner * again immediately, almost never happen for this shrinker.
1133eb23ee4fSJohannes Weiner * We don't do any trylocking; -ENOMEM comes closest,
1134eb23ee4fSJohannes Weiner * but that's extremely rare and doesn't happen spuriously
1135eb23ee4fSJohannes Weiner * either. Don't bother distinguishing this case.
1136eb23ee4fSJohannes Weiner */
1137eb23ee4fSJohannes Weiner list_move_tail(item, &l->list);
1138eb23ee4fSJohannes Weiner
1139eb23ee4fSJohannes Weiner /*
1140eb23ee4fSJohannes Weiner * Once the lru lock is dropped, the entry might get freed. The
1141eb23ee4fSJohannes Weiner * swpentry is copied to the stack, and entry isn't deref'd again
1142eb23ee4fSJohannes Weiner * until the entry is verified to still be alive in the tree.
1143eb23ee4fSJohannes Weiner */
1144eb23ee4fSJohannes Weiner swpentry = entry->swpentry;
1145eb23ee4fSJohannes Weiner
1146eb23ee4fSJohannes Weiner /*
1147eb23ee4fSJohannes Weiner * It's safe to drop the lock here because we return either
1148a882dd92SAlice Ryhl * LRU_REMOVED_RETRY, LRU_RETRY or LRU_STOP.
1149eb23ee4fSJohannes Weiner */
1150da0c0251SKairui Song spin_unlock(&l->lock);
1151eb23ee4fSJohannes Weiner
1152eb23ee4fSJohannes Weiner writeback_result = zswap_writeback_entry(entry, swpentry);
1153eb23ee4fSJohannes Weiner
1154eb23ee4fSJohannes Weiner if (writeback_result) {
1155eb23ee4fSJohannes Weiner zswap_reject_reclaim_fail++;
1156eb23ee4fSJohannes Weiner ret = LRU_RETRY;
1157eb23ee4fSJohannes Weiner
1158eb23ee4fSJohannes Weiner /*
1159eb23ee4fSJohannes Weiner * Encountering a page already in swap cache is a sign that we are shrinking
1160eb23ee4fSJohannes Weiner * into the warmer region. We should terminate shrinking (if we're in the dynamic
1161eb23ee4fSJohannes Weiner * shrinker context).
1162eb23ee4fSJohannes Weiner */
1163b49547adSChengming Zhou if (writeback_result == -EEXIST && encountered_page_in_swapcache) {
1164b49547adSChengming Zhou ret = LRU_STOP;
1165eb23ee4fSJohannes Weiner *encountered_page_in_swapcache = true;
1166b49547adSChengming Zhou }
1167eb23ee4fSJohannes Weiner } else {
1168eb23ee4fSJohannes Weiner zswap_written_back_pages++;
1169eb23ee4fSJohannes Weiner }
1170eb23ee4fSJohannes Weiner
1171eb23ee4fSJohannes Weiner return ret;
1172eb23ee4fSJohannes Weiner }
1173b5ba474fSNhat Pham
zswap_shrinker_scan(struct shrinker * shrinker,struct shrink_control * sc)1174b5ba474fSNhat Pham static unsigned long zswap_shrinker_scan(struct shrinker *shrinker,
1175b5ba474fSNhat Pham struct shrink_control *sc)
1176b5ba474fSNhat Pham {
1177e31c38e0SNhat Pham unsigned long shrink_ret;
1178b5ba474fSNhat Pham bool encountered_page_in_swapcache = false;
1179b5ba474fSNhat Pham
1180501a06feSNhat Pham if (!zswap_shrinker_enabled ||
1181501a06feSNhat Pham !mem_cgroup_zswap_writeback_enabled(sc->memcg)) {
1182b5ba474fSNhat Pham sc->nr_scanned = 0;
1183b5ba474fSNhat Pham return SHRINK_STOP;
1184b5ba474fSNhat Pham }
1185b5ba474fSNhat Pham
1186e35606e4SChengming Zhou shrink_ret = list_lru_shrink_walk(&zswap_list_lru, sc, &shrink_memcg_cb,
1187b5ba474fSNhat Pham &encountered_page_in_swapcache);
1188b5ba474fSNhat Pham
1189b5ba474fSNhat Pham if (encountered_page_in_swapcache)
1190b5ba474fSNhat Pham return SHRINK_STOP;
1191b5ba474fSNhat Pham
1192b5ba474fSNhat Pham return shrink_ret ? shrink_ret : SHRINK_STOP;
1193b5ba474fSNhat Pham }
1194b5ba474fSNhat Pham
zswap_shrinker_count(struct shrinker * shrinker,struct shrink_control * sc)1195b5ba474fSNhat Pham static unsigned long zswap_shrinker_count(struct shrinker *shrinker,
1196b5ba474fSNhat Pham struct shrink_control *sc)
1197b5ba474fSNhat Pham {
1198b5ba474fSNhat Pham struct mem_cgroup *memcg = sc->memcg;
1199b5ba474fSNhat Pham struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(sc->nid));
1200e31c38e0SNhat Pham atomic_long_t *nr_disk_swapins =
1201e31c38e0SNhat Pham &lruvec->zswap_lruvec_state.nr_disk_swapins;
1202e31c38e0SNhat Pham unsigned long nr_backing, nr_stored, nr_freeable, nr_disk_swapins_cur,
1203e31c38e0SNhat Pham nr_remain;
1204b5ba474fSNhat Pham
1205501a06feSNhat Pham if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(memcg))
1206b5ba474fSNhat Pham return 0;
1207b5ba474fSNhat Pham
120830fb6a8dSJohannes Weiner /*
120930fb6a8dSJohannes Weiner * The shrinker resumes swap writeback, which will enter block
121030fb6a8dSJohannes Weiner * and may enter fs. XXX: Harmonize with vmscan.c __GFP_FS
121130fb6a8dSJohannes Weiner * rules (may_enter_fs()), which apply on a per-folio basis.
121230fb6a8dSJohannes Weiner */
121330fb6a8dSJohannes Weiner if (!gfp_has_io_fs(sc->gfp_mask))
121430fb6a8dSJohannes Weiner return 0;
121530fb6a8dSJohannes Weiner
1216682886ecSJohannes Weiner /*
1217682886ecSJohannes Weiner * For memcg, use the cgroup-wide ZSWAP stats since we don't
1218682886ecSJohannes Weiner * have them per-node and thus per-lruvec. Careful if memcg is
1219682886ecSJohannes Weiner * runtime-disabled: we can get sc->memcg == NULL, which is ok
1220682886ecSJohannes Weiner * for the lruvec, but not for memcg_page_state().
1221682886ecSJohannes Weiner *
1222682886ecSJohannes Weiner * Without memcg, use the zswap pool-wide metrics.
1223682886ecSJohannes Weiner */
1224682886ecSJohannes Weiner if (!mem_cgroup_disabled()) {
12257d7ef0a4SYosry Ahmed mem_cgroup_flush_stats(memcg);
1226b5ba474fSNhat Pham nr_backing = memcg_page_state(memcg, MEMCG_ZSWAP_B) >> PAGE_SHIFT;
1227b5ba474fSNhat Pham nr_stored = memcg_page_state(memcg, MEMCG_ZSWAPPED);
1228682886ecSJohannes Weiner } else {
122991cdcd8dSJohannes Weiner nr_backing = zswap_total_pages();
12306e1fa555SKanchana P Sridhar nr_stored = atomic_long_read(&zswap_stored_pages);
1231682886ecSJohannes Weiner }
1232b5ba474fSNhat Pham
1233b5ba474fSNhat Pham if (!nr_stored)
1234b5ba474fSNhat Pham return 0;
1235b5ba474fSNhat Pham
1236e35606e4SChengming Zhou nr_freeable = list_lru_shrink_count(&zswap_list_lru, sc);
1237e31c38e0SNhat Pham if (!nr_freeable)
1238e31c38e0SNhat Pham return 0;
1239e31c38e0SNhat Pham
1240b5ba474fSNhat Pham /*
1241e31c38e0SNhat Pham * Subtract from the lru size the number of pages that are recently swapped
1242e31c38e0SNhat Pham * in from disk. The idea is that had we protect the zswap's LRU by this
1243e31c38e0SNhat Pham * amount of pages, these disk swapins would not have happened.
1244b5ba474fSNhat Pham */
1245e31c38e0SNhat Pham nr_disk_swapins_cur = atomic_long_read(nr_disk_swapins);
1246e31c38e0SNhat Pham do {
1247e31c38e0SNhat Pham if (nr_freeable >= nr_disk_swapins_cur)
1248e31c38e0SNhat Pham nr_remain = 0;
1249e31c38e0SNhat Pham else
1250e31c38e0SNhat Pham nr_remain = nr_disk_swapins_cur - nr_freeable;
1251e31c38e0SNhat Pham } while (!atomic_long_try_cmpxchg(
1252e31c38e0SNhat Pham nr_disk_swapins, &nr_disk_swapins_cur, nr_remain));
1253e31c38e0SNhat Pham
1254e31c38e0SNhat Pham nr_freeable -= nr_disk_swapins_cur - nr_remain;
1255e31c38e0SNhat Pham if (!nr_freeable)
1256e31c38e0SNhat Pham return 0;
1257b5ba474fSNhat Pham
1258b5ba474fSNhat Pham /*
1259b5ba474fSNhat Pham * Scale the number of freeable pages by the memory saving factor.
1260b5ba474fSNhat Pham * This ensures that the better zswap compresses memory, the fewer
1261b5ba474fSNhat Pham * pages we will evict to swap (as it will otherwise incur IO for
1262b5ba474fSNhat Pham * relatively small memory saving).
1263b5ba474fSNhat Pham */
1264b5ba474fSNhat Pham return mult_frac(nr_freeable, nr_backing, nr_stored);
1265b5ba474fSNhat Pham }
1266b5ba474fSNhat Pham
zswap_alloc_shrinker(void)1267bf9b7df2SChengming Zhou static struct shrinker *zswap_alloc_shrinker(void)
1268b5ba474fSNhat Pham {
1269bf9b7df2SChengming Zhou struct shrinker *shrinker;
1270b5ba474fSNhat Pham
1271bf9b7df2SChengming Zhou shrinker =
1272bf9b7df2SChengming Zhou shrinker_alloc(SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE, "mm-zswap");
1273bf9b7df2SChengming Zhou if (!shrinker)
1274bf9b7df2SChengming Zhou return NULL;
1275bf9b7df2SChengming Zhou
1276bf9b7df2SChengming Zhou shrinker->scan_objects = zswap_shrinker_scan;
1277bf9b7df2SChengming Zhou shrinker->count_objects = zswap_shrinker_count;
1278bf9b7df2SChengming Zhou shrinker->batch = 0;
1279bf9b7df2SChengming Zhou shrinker->seeks = DEFAULT_SEEKS;
1280bf9b7df2SChengming Zhou return shrinker;
1281b5ba474fSNhat Pham }
1282b5ba474fSNhat Pham
shrink_memcg(struct mem_cgroup * memcg)1283a65b0e76SDomenico Cerasuolo static int shrink_memcg(struct mem_cgroup *memcg)
1284a65b0e76SDomenico Cerasuolo {
128581920438STakero Funaki int nid, shrunk = 0, scanned = 0;
1286a65b0e76SDomenico Cerasuolo
1287501a06feSNhat Pham if (!mem_cgroup_zswap_writeback_enabled(memcg))
128881920438STakero Funaki return -ENOENT;
1289501a06feSNhat Pham
1290a65b0e76SDomenico Cerasuolo /*
1291a65b0e76SDomenico Cerasuolo * Skip zombies because their LRUs are reparented and we would be
1292a65b0e76SDomenico Cerasuolo * reclaiming from the parent instead of the dead memcg.
1293a65b0e76SDomenico Cerasuolo */
1294a65b0e76SDomenico Cerasuolo if (memcg && !mem_cgroup_online(memcg))
1295a65b0e76SDomenico Cerasuolo return -ENOENT;
1296a65b0e76SDomenico Cerasuolo
1297a65b0e76SDomenico Cerasuolo for_each_node_state(nid, N_NORMAL_MEMORY) {
1298a65b0e76SDomenico Cerasuolo unsigned long nr_to_walk = 1;
1299a65b0e76SDomenico Cerasuolo
1300e35606e4SChengming Zhou shrunk += list_lru_walk_one(&zswap_list_lru, nid, memcg,
1301a65b0e76SDomenico Cerasuolo &shrink_memcg_cb, NULL, &nr_to_walk);
130281920438STakero Funaki scanned += 1 - nr_to_walk;
1303a65b0e76SDomenico Cerasuolo }
130481920438STakero Funaki
130581920438STakero Funaki if (!scanned)
130681920438STakero Funaki return -ENOENT;
130781920438STakero Funaki
1308a65b0e76SDomenico Cerasuolo return shrunk ? 0 : -EAGAIN;
1309f999f38bSDomenico Cerasuolo }
1310f999f38bSDomenico Cerasuolo
shrink_worker(struct work_struct * w)131145190f01SVitaly Wool static void shrink_worker(struct work_struct *w)
131245190f01SVitaly Wool {
1313a65b0e76SDomenico Cerasuolo struct mem_cgroup *memcg;
131481920438STakero Funaki int ret, failures = 0, attempts = 0;
131591cdcd8dSJohannes Weiner unsigned long thr;
131691cdcd8dSJohannes Weiner
131791cdcd8dSJohannes Weiner /* Reclaim down to the accept threshold */
131891cdcd8dSJohannes Weiner thr = zswap_accept_thr_pages();
131945190f01SVitaly Wool
1320a65b0e76SDomenico Cerasuolo /*
132181920438STakero Funaki * Global reclaim will select cgroup in a round-robin fashion from all
132281920438STakero Funaki * online memcgs, but memcgs that have no pages in zswap and
132381920438STakero Funaki * writeback-disabled memcgs (memory.zswap.writeback=0) are not
132481920438STakero Funaki * candidates for shrinking.
132581920438STakero Funaki *
132681920438STakero Funaki * Shrinking will be aborted if we encounter the following
132781920438STakero Funaki * MAX_RECLAIM_RETRIES times:
132881920438STakero Funaki * - No writeback-candidate memcgs found in a memcg tree walk.
132981920438STakero Funaki * - Shrinking a writeback-candidate memcg failed.
1330a65b0e76SDomenico Cerasuolo *
1331c5519e0aSTakero Funaki * We save iteration cursor memcg into zswap_next_shrink,
1332c5519e0aSTakero Funaki * which can be modified by the offline memcg cleaner
1333c5519e0aSTakero Funaki * zswap_memcg_offline_cleanup().
1334c5519e0aSTakero Funaki *
1335c5519e0aSTakero Funaki * Since the offline cleaner is called only once, we cannot leave an
1336c5519e0aSTakero Funaki * offline memcg reference in zswap_next_shrink.
1337c5519e0aSTakero Funaki * We can rely on the cleaner only if we get online memcg under lock.
1338c5519e0aSTakero Funaki *
1339c5519e0aSTakero Funaki * If we get an offline memcg, we cannot determine if the cleaner has
1340c5519e0aSTakero Funaki * already been called or will be called later. We must put back the
1341c5519e0aSTakero Funaki * reference before returning from this function. Otherwise, the
1342c5519e0aSTakero Funaki * offline memcg left in zswap_next_shrink will hold the reference
1343c5519e0aSTakero Funaki * until the next run of shrink_worker().
1344a65b0e76SDomenico Cerasuolo */
1345c5519e0aSTakero Funaki do {
1346c5519e0aSTakero Funaki /*
1347c5519e0aSTakero Funaki * Start shrinking from the next memcg after zswap_next_shrink.
1348c5519e0aSTakero Funaki * When the offline cleaner has already advanced the cursor,
1349c5519e0aSTakero Funaki * advancing the cursor here overlooks one memcg, but this
1350c5519e0aSTakero Funaki * should be negligibly rare.
1351c5519e0aSTakero Funaki *
1352c5519e0aSTakero Funaki * If we get an online memcg, keep the extra reference in case
1353c5519e0aSTakero Funaki * the original one obtained by mem_cgroup_iter() is dropped by
1354c5519e0aSTakero Funaki * zswap_memcg_offline_cleanup() while we are shrinking the
1355c5519e0aSTakero Funaki * memcg.
1356c5519e0aSTakero Funaki */
1357c5519e0aSTakero Funaki spin_lock(&zswap_shrink_lock);
1358c5519e0aSTakero Funaki do {
1359c5519e0aSTakero Funaki memcg = mem_cgroup_iter(NULL, zswap_next_shrink, NULL);
1360c5519e0aSTakero Funaki zswap_next_shrink = memcg;
1361c5519e0aSTakero Funaki } while (memcg && !mem_cgroup_tryget_online(memcg));
1362c5519e0aSTakero Funaki spin_unlock(&zswap_shrink_lock);
1363c5519e0aSTakero Funaki
1364a65b0e76SDomenico Cerasuolo if (!memcg) {
136581920438STakero Funaki /*
136681920438STakero Funaki * Continue shrinking without incrementing failures if
136781920438STakero Funaki * we found candidate memcgs in the last tree walk.
136881920438STakero Funaki */
136981920438STakero Funaki if (!attempts && ++failures == MAX_RECLAIM_RETRIES)
1370e0228d59SDomenico Cerasuolo break;
1371a65b0e76SDomenico Cerasuolo
137281920438STakero Funaki attempts = 0;
1373a65b0e76SDomenico Cerasuolo goto resched;
1374e0228d59SDomenico Cerasuolo }
1375a65b0e76SDomenico Cerasuolo
1376a65b0e76SDomenico Cerasuolo ret = shrink_memcg(memcg);
1377a65b0e76SDomenico Cerasuolo /* drop the extra reference */
1378a65b0e76SDomenico Cerasuolo mem_cgroup_put(memcg);
1379a65b0e76SDomenico Cerasuolo
138081920438STakero Funaki /*
138181920438STakero Funaki * There are no writeback-candidate pages in the memcg.
138281920438STakero Funaki * This is not an issue as long as we can find another memcg
138381920438STakero Funaki * with pages in zswap. Skip this without incrementing attempts
138481920438STakero Funaki * and failures.
138581920438STakero Funaki */
138681920438STakero Funaki if (ret == -ENOENT)
138781920438STakero Funaki continue;
138881920438STakero Funaki ++attempts;
138981920438STakero Funaki
1390a65b0e76SDomenico Cerasuolo if (ret && ++failures == MAX_RECLAIM_RETRIES)
1391a65b0e76SDomenico Cerasuolo break;
1392a65b0e76SDomenico Cerasuolo resched:
1393e0228d59SDomenico Cerasuolo cond_resched();
139491cdcd8dSJohannes Weiner } while (zswap_total_pages() > thr);
139545190f01SVitaly Wool }
139645190f01SVitaly Wool
1397e87b8814SYosry Ahmed /*********************************
1398e87b8814SYosry Ahmed * main API
1399e87b8814SYosry Ahmed **********************************/
1400b7c0ccdfSKanchana P Sridhar
zswap_store_page(struct page * page,struct obj_cgroup * objcg,struct zswap_pool * pool)140163895d20SHyeonggon Yoo static bool zswap_store_page(struct page *page,
1402b7c0ccdfSKanchana P Sridhar struct obj_cgroup *objcg,
1403b7c0ccdfSKanchana P Sridhar struct zswap_pool *pool)
14042b281117SSeth Jennings {
1405ed882addSKanchana P Sridhar swp_entry_t page_swpentry = page_swap_entry(page);
1406796c2c23SChris Li struct zswap_entry *entry, *old;
14072b281117SSeth Jennings
14082b281117SSeth Jennings /* allocate entry */
1409b7c0ccdfSKanchana P Sridhar entry = zswap_entry_cache_alloc(GFP_KERNEL, page_to_nid(page));
14102b281117SSeth Jennings if (!entry) {
14112b281117SSeth Jennings zswap_reject_kmemcache_fail++;
141263895d20SHyeonggon Yoo return false;
14132b281117SSeth Jennings }
14142b281117SSeth Jennings
1415ed882addSKanchana P Sridhar if (!zswap_compress(page, entry, pool))
1416ed882addSKanchana P Sridhar goto compress_failed;
1417b7c0ccdfSKanchana P Sridhar
1418ed882addSKanchana P Sridhar old = xa_store(swap_zswap_tree(page_swpentry),
1419ed882addSKanchana P Sridhar swp_offset(page_swpentry),
1420b7c0ccdfSKanchana P Sridhar entry, GFP_KERNEL);
1421796c2c23SChris Li if (xa_is_err(old)) {
1422796c2c23SChris Li int err = xa_err(old);
1423796c2c23SChris Li
1424796c2c23SChris Li WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err);
1425796c2c23SChris Li zswap_reject_alloc_fail++;
1426796c2c23SChris Li goto store_failed;
1427796c2c23SChris Li }
1428796c2c23SChris Li
1429796c2c23SChris Li /*
1430796c2c23SChris Li * We may have had an existing entry that became stale when
1431796c2c23SChris Li * the folio was redirtied and now the new version is being
1432796c2c23SChris Li * swapped out. Get rid of the old.
1433796c2c23SChris Li */
1434796c2c23SChris Li if (old)
1435796c2c23SChris Li zswap_entry_free(old);
1436796c2c23SChris Li
1437ca56489cSDomenico Cerasuolo /*
1438ed882addSKanchana P Sridhar * The entry is successfully compressed and stored in the tree, there is
143963895d20SHyeonggon Yoo * no further possibility of failure. Grab refs to the pool and objcg,
144063895d20SHyeonggon Yoo * charge zswap memory, and increment zswap_stored_pages.
144163895d20SHyeonggon Yoo * The opposite actions will be performed by zswap_entry_free()
144263895d20SHyeonggon Yoo * when the entry is removed from the tree.
1443ed882addSKanchana P Sridhar */
1444ed882addSKanchana P Sridhar zswap_pool_get(pool);
144563895d20SHyeonggon Yoo if (objcg) {
1446ed882addSKanchana P Sridhar obj_cgroup_get(objcg);
144763895d20SHyeonggon Yoo obj_cgroup_charge_zswap(objcg, entry->length);
144863895d20SHyeonggon Yoo }
144963895d20SHyeonggon Yoo atomic_long_inc(&zswap_stored_pages);
1450dca4437aSSeongJae Park if (entry->length == PAGE_SIZE)
1451dca4437aSSeongJae Park atomic_long_inc(&zswap_stored_incompressible_pages);
1452ed882addSKanchana P Sridhar
1453ed882addSKanchana P Sridhar /*
1454796c2c23SChris Li * We finish initializing the entry while it's already in xarray.
1455796c2c23SChris Li * This is safe because:
1456796c2c23SChris Li *
1457796c2c23SChris Li * 1. Concurrent stores and invalidations are excluded by folio lock.
1458796c2c23SChris Li *
1459796c2c23SChris Li * 2. Writeback is excluded by the entry not being on the LRU yet.
1460796c2c23SChris Li * The publishing order matters to prevent writeback from seeing
1461796c2c23SChris Li * an incoherent entry.
1462ca56489cSDomenico Cerasuolo */
1463ed882addSKanchana P Sridhar entry->pool = pool;
1464ed882addSKanchana P Sridhar entry->swpentry = page_swpentry;
1465ed882addSKanchana P Sridhar entry->objcg = objcg;
1466ed882addSKanchana P Sridhar entry->referenced = true;
146735499e2bSDomenico Cerasuolo if (entry->length) {
1468a65b0e76SDomenico Cerasuolo INIT_LIST_HEAD(&entry->lru);
1469e35606e4SChengming Zhou zswap_lru_add(&zswap_list_lru, entry);
1470f999f38bSDomenico Cerasuolo }
14712b281117SSeth Jennings
147263895d20SHyeonggon Yoo return true;
14732b281117SSeth Jennings
1474796c2c23SChris Li store_failed:
14755c3f8be0SJohannes Weiner zs_free(pool->zs_pool, entry->handle);
1476ed882addSKanchana P Sridhar compress_failed:
14772b281117SSeth Jennings zswap_entry_cache_free(entry);
147863895d20SHyeonggon Yoo return false;
1479b7c0ccdfSKanchana P Sridhar }
1480b7c0ccdfSKanchana P Sridhar
zswap_store(struct folio * folio)1481b7c0ccdfSKanchana P Sridhar bool zswap_store(struct folio *folio)
1482b7c0ccdfSKanchana P Sridhar {
1483b7c0ccdfSKanchana P Sridhar long nr_pages = folio_nr_pages(folio);
1484b7c0ccdfSKanchana P Sridhar swp_entry_t swp = folio->swap;
1485b7c0ccdfSKanchana P Sridhar struct obj_cgroup *objcg = NULL;
1486b7c0ccdfSKanchana P Sridhar struct mem_cgroup *memcg = NULL;
1487b7c0ccdfSKanchana P Sridhar struct zswap_pool *pool;
1488b7c0ccdfSKanchana P Sridhar bool ret = false;
1489b7c0ccdfSKanchana P Sridhar long index;
1490b7c0ccdfSKanchana P Sridhar
1491b7c0ccdfSKanchana P Sridhar VM_WARN_ON_ONCE(!folio_test_locked(folio));
1492b7c0ccdfSKanchana P Sridhar VM_WARN_ON_ONCE(!folio_test_swapcache(folio));
1493b7c0ccdfSKanchana P Sridhar
1494b7c0ccdfSKanchana P Sridhar if (!zswap_enabled)
1495b7c0ccdfSKanchana P Sridhar goto check_old;
1496b7c0ccdfSKanchana P Sridhar
1497b7c0ccdfSKanchana P Sridhar objcg = get_obj_cgroup_from_folio(folio);
1498b7c0ccdfSKanchana P Sridhar if (objcg && !obj_cgroup_may_zswap(objcg)) {
1499b7c0ccdfSKanchana P Sridhar memcg = get_mem_cgroup_from_objcg(objcg);
1500b7c0ccdfSKanchana P Sridhar if (shrink_memcg(memcg)) {
1501b7c0ccdfSKanchana P Sridhar mem_cgroup_put(memcg);
1502b7c0ccdfSKanchana P Sridhar goto put_objcg;
1503b7c0ccdfSKanchana P Sridhar }
1504b7c0ccdfSKanchana P Sridhar mem_cgroup_put(memcg);
1505b7c0ccdfSKanchana P Sridhar }
1506b7c0ccdfSKanchana P Sridhar
1507b7c0ccdfSKanchana P Sridhar if (zswap_check_limits())
1508b7c0ccdfSKanchana P Sridhar goto put_objcg;
1509b7c0ccdfSKanchana P Sridhar
1510b7c0ccdfSKanchana P Sridhar pool = zswap_pool_current_get();
1511b7c0ccdfSKanchana P Sridhar if (!pool)
1512b7c0ccdfSKanchana P Sridhar goto put_objcg;
1513b7c0ccdfSKanchana P Sridhar
1514b7c0ccdfSKanchana P Sridhar if (objcg) {
1515b7c0ccdfSKanchana P Sridhar memcg = get_mem_cgroup_from_objcg(objcg);
1516b7c0ccdfSKanchana P Sridhar if (memcg_list_lru_alloc(memcg, &zswap_list_lru, GFP_KERNEL)) {
1517b7c0ccdfSKanchana P Sridhar mem_cgroup_put(memcg);
1518b7c0ccdfSKanchana P Sridhar goto put_pool;
1519b7c0ccdfSKanchana P Sridhar }
1520b7c0ccdfSKanchana P Sridhar mem_cgroup_put(memcg);
1521b7c0ccdfSKanchana P Sridhar }
1522b7c0ccdfSKanchana P Sridhar
1523b7c0ccdfSKanchana P Sridhar for (index = 0; index < nr_pages; ++index) {
1524b7c0ccdfSKanchana P Sridhar struct page *page = folio_page(folio, index);
1525b7c0ccdfSKanchana P Sridhar
152663895d20SHyeonggon Yoo if (!zswap_store_page(page, objcg, pool))
1527b7c0ccdfSKanchana P Sridhar goto put_pool;
1528b7c0ccdfSKanchana P Sridhar }
1529b7c0ccdfSKanchana P Sridhar
153063895d20SHyeonggon Yoo if (objcg)
1531b7c0ccdfSKanchana P Sridhar count_objcg_events(objcg, ZSWPOUT, nr_pages);
1532b7c0ccdfSKanchana P Sridhar
1533b7c0ccdfSKanchana P Sridhar count_vm_events(ZSWPOUT, nr_pages);
1534b7c0ccdfSKanchana P Sridhar
1535b7c0ccdfSKanchana P Sridhar ret = true;
1536b7c0ccdfSKanchana P Sridhar
1537b7c0ccdfSKanchana P Sridhar put_pool:
1538b7c0ccdfSKanchana P Sridhar zswap_pool_put(pool);
1539b7c0ccdfSKanchana P Sridhar put_objcg:
1540f4840ccfSJohannes Weiner obj_cgroup_put(objcg);
1541b7c0ccdfSKanchana P Sridhar if (!ret && zswap_pool_reached_full)
15424ea3fa9dSYosry Ahmed queue_work(shrink_wq, &zswap_shrink_work);
1543f576a1e8SChengming Zhou check_old:
1544f576a1e8SChengming Zhou /*
1545b7c0ccdfSKanchana P Sridhar * If the zswap store fails or zswap is disabled, we must invalidate
1546b7c0ccdfSKanchana P Sridhar * the possibly stale entries which were previously stored at the
1547b7c0ccdfSKanchana P Sridhar * offsets corresponding to each page of the folio. Otherwise,
1548b7c0ccdfSKanchana P Sridhar * writeback could overwrite the new data in the swapfile.
1549f576a1e8SChengming Zhou */
1550b7c0ccdfSKanchana P Sridhar if (!ret) {
1551b7c0ccdfSKanchana P Sridhar unsigned type = swp_type(swp);
1552b7c0ccdfSKanchana P Sridhar pgoff_t offset = swp_offset(swp);
1553b7c0ccdfSKanchana P Sridhar struct zswap_entry *entry;
1554b7c0ccdfSKanchana P Sridhar struct xarray *tree;
1555b7c0ccdfSKanchana P Sridhar
1556b7c0ccdfSKanchana P Sridhar for (index = 0; index < nr_pages; ++index) {
1557b7c0ccdfSKanchana P Sridhar tree = swap_zswap_tree(swp_entry(type, offset + index));
1558b7c0ccdfSKanchana P Sridhar entry = xa_erase(tree, offset + index);
1559f576a1e8SChengming Zhou if (entry)
1560796c2c23SChris Li zswap_entry_free(entry);
1561b7c0ccdfSKanchana P Sridhar }
1562b7c0ccdfSKanchana P Sridhar }
1563b7c0ccdfSKanchana P Sridhar
1564b7c0ccdfSKanchana P Sridhar return ret;
15652b281117SSeth Jennings }
15662b281117SSeth Jennings
1567ff22f929SNhat Pham /**
1568ff22f929SNhat Pham * zswap_load() - load a folio from zswap
1569ff22f929SNhat Pham * @folio: folio to load
1570ff22f929SNhat Pham *
1571ff22f929SNhat Pham * Return: 0 on success, with the folio unlocked and marked up-to-date, or one
1572ff22f929SNhat Pham * of the following error codes:
1573ff22f929SNhat Pham *
1574ff22f929SNhat Pham * -EIO: if the swapped out content was in zswap, but could not be loaded
1575ff22f929SNhat Pham * into the page due to a decompression failure. The folio is unlocked, but
1576ff22f929SNhat Pham * NOT marked up-to-date, so that an IO error is emitted (e.g. do_swap_page()
1577ff22f929SNhat Pham * will SIGBUS).
1578ff22f929SNhat Pham *
1579ff22f929SNhat Pham * -EINVAL: if the swapped out content was in zswap, but the page belongs
1580ff22f929SNhat Pham * to a large folio, which is not supported by zswap. The folio is unlocked,
1581ff22f929SNhat Pham * but NOT marked up-to-date, so that an IO error is emitted (e.g.
1582ff22f929SNhat Pham * do_swap_page() will SIGBUS).
1583ff22f929SNhat Pham *
1584ff22f929SNhat Pham * -ENOENT: if the swapped out content was not in zswap. The folio remains
1585ff22f929SNhat Pham * locked on return.
1586ff22f929SNhat Pham */
zswap_load(struct folio * folio)1587ff22f929SNhat Pham int zswap_load(struct folio *folio)
15882b281117SSeth Jennings {
15893d2c9087SDavid Hildenbrand swp_entry_t swp = folio->swap;
159042c06a0eSJohannes Weiner pgoff_t offset = swp_offset(swp);
1591796c2c23SChris Li struct xarray *tree = swap_zswap_tree(swp);
15922b281117SSeth Jennings struct zswap_entry *entry;
159342c06a0eSJohannes Weiner
1594ca54f6d8SMatthew Wilcox (Oracle) VM_WARN_ON_ONCE(!folio_test_locked(folio));
1595ae1a645dSKairui Song VM_WARN_ON_ONCE(!folio_test_swapcache(folio));
15962b281117SSeth Jennings
15972d4d2b1cSYosry Ahmed if (zswap_never_enabled())
1598ff22f929SNhat Pham return -ENOENT;
15992d4d2b1cSYosry Ahmed
160025cd2414SJohannes Weiner /*
1601c63f210dSYosry Ahmed * Large folios should not be swapped in while zswap is being used, as
1602c63f210dSYosry Ahmed * they are not properly handled. Zswap does not properly load large
1603c63f210dSYosry Ahmed * folios, and a large folio may only be partially in zswap.
1604c63f210dSYosry Ahmed */
1605ff22f929SNhat Pham if (WARN_ON_ONCE(folio_test_large(folio))) {
1606ff22f929SNhat Pham folio_unlock(folio);
1607ff22f929SNhat Pham return -EINVAL;
1608ff22f929SNhat Pham }
1609ff22f929SNhat Pham
1610ff22f929SNhat Pham entry = xa_load(tree, offset);
1611ff22f929SNhat Pham if (!entry)
1612ff22f929SNhat Pham return -ENOENT;
1613ff22f929SNhat Pham
1614ff22f929SNhat Pham if (!zswap_decompress(entry, folio)) {
1615ff22f929SNhat Pham folio_unlock(folio);
1616ff22f929SNhat Pham return -EIO;
1617ff22f929SNhat Pham }
1618ff22f929SNhat Pham
1619ff22f929SNhat Pham folio_mark_uptodate(folio);
1620ff22f929SNhat Pham
1621ff22f929SNhat Pham count_vm_event(ZSWPIN);
1622ff22f929SNhat Pham if (entry->objcg)
1623ff22f929SNhat Pham count_objcg_events(entry->objcg, ZSWPIN, 1);
1624c63f210dSYosry Ahmed
1625c63f210dSYosry Ahmed /*
1626ae1a645dSKairui Song * We are reading into the swapcache, invalidate zswap entry.
1627ae1a645dSKairui Song * The swapcache is the authoritative owner of the page and
162825cd2414SJohannes Weiner * its mappings, and the pressure that results from having two
162925cd2414SJohannes Weiner * in-memory copies outweighs any benefits of caching the
163025cd2414SJohannes Weiner * compression work.
163125cd2414SJohannes Weiner */
1632c2e2ba77SChengming Zhou folio_mark_dirty(folio);
1633ff22f929SNhat Pham xa_erase(tree, offset);
1634ff22f929SNhat Pham zswap_entry_free(entry);
1635c2e2ba77SChengming Zhou
1636ff22f929SNhat Pham folio_unlock(folio);
1637ff22f929SNhat Pham return 0;
16382b281117SSeth Jennings }
16392b281117SSeth Jennings
zswap_invalidate(swp_entry_t swp)16400827a1fbSChengming Zhou void zswap_invalidate(swp_entry_t swp)
16412b281117SSeth Jennings {
16420827a1fbSChengming Zhou pgoff_t offset = swp_offset(swp);
1643796c2c23SChris Li struct xarray *tree = swap_zswap_tree(swp);
16442b281117SSeth Jennings struct zswap_entry *entry;
16452b281117SSeth Jennings
1646773ee2cdSKairui Song if (xa_empty(tree))
1647773ee2cdSKairui Song return;
1648773ee2cdSKairui Song
1649796c2c23SChris Li entry = xa_erase(tree, offset);
165006ed2289SJohannes Weiner if (entry)
1651796c2c23SChris Li zswap_entry_free(entry);
16522b281117SSeth Jennings }
16532b281117SSeth Jennings
zswap_swapon(int type,unsigned long nr_pages)165444c7c734SChengming Zhou int zswap_swapon(int type, unsigned long nr_pages)
165542c06a0eSJohannes Weiner {
1656796c2c23SChris Li struct xarray *trees, *tree;
165744c7c734SChengming Zhou unsigned int nr, i;
165842c06a0eSJohannes Weiner
1659685a17fbSKairui Song nr = DIV_ROUND_UP(nr_pages, ZSWAP_ADDRESS_SPACE_PAGES);
1660bf4afc53SLinus Torvalds trees = kvzalloc_objs(*tree, nr);
166144c7c734SChengming Zhou if (!trees) {
166242c06a0eSJohannes Weiner pr_err("alloc failed, zswap disabled for swap type %d\n", type);
1663bb29fd77SChengming Zhou return -ENOMEM;
166442c06a0eSJohannes Weiner }
166542c06a0eSJohannes Weiner
1666796c2c23SChris Li for (i = 0; i < nr; i++)
1667796c2c23SChris Li xa_init(trees + i);
166844c7c734SChengming Zhou
166944c7c734SChengming Zhou nr_zswap_trees[type] = nr;
167044c7c734SChengming Zhou zswap_trees[type] = trees;
1671bb29fd77SChengming Zhou return 0;
167242c06a0eSJohannes Weiner }
167342c06a0eSJohannes Weiner
zswap_swapoff(int type)167442c06a0eSJohannes Weiner void zswap_swapoff(int type)
16752b281117SSeth Jennings {
1676796c2c23SChris Li struct xarray *trees = zswap_trees[type];
167744c7c734SChengming Zhou unsigned int i;
16782b281117SSeth Jennings
167944c7c734SChengming Zhou if (!trees)
16802b281117SSeth Jennings return;
16812b281117SSeth Jennings
168283e68f25SYosry Ahmed /* try_to_unuse() invalidated all the entries already */
168383e68f25SYosry Ahmed for (i = 0; i < nr_zswap_trees[type]; i++)
1684796c2c23SChris Li WARN_ON_ONCE(!xa_empty(trees + i));
168544c7c734SChengming Zhou
168644c7c734SChengming Zhou kvfree(trees);
168744c7c734SChengming Zhou nr_zswap_trees[type] = 0;
1688aa9bca05SWeijie Yang zswap_trees[type] = NULL;
16892b281117SSeth Jennings }
16902b281117SSeth Jennings
16912b281117SSeth Jennings /*********************************
16922b281117SSeth Jennings * debugfs functions
16932b281117SSeth Jennings **********************************/
16942b281117SSeth Jennings #ifdef CONFIG_DEBUG_FS
16952b281117SSeth Jennings #include <linux/debugfs.h>
16962b281117SSeth Jennings
16972b281117SSeth Jennings static struct dentry *zswap_debugfs_root;
16982b281117SSeth Jennings
debugfs_get_total_size(void * data,u64 * val)169991cdcd8dSJohannes Weiner static int debugfs_get_total_size(void *data, u64 *val)
170091cdcd8dSJohannes Weiner {
170191cdcd8dSJohannes Weiner *val = zswap_total_pages() * PAGE_SIZE;
170291cdcd8dSJohannes Weiner return 0;
170391cdcd8dSJohannes Weiner }
170491cdcd8dSJohannes Weiner DEFINE_DEBUGFS_ATTRIBUTE(total_size_fops, debugfs_get_total_size, NULL, "%llu\n");
170591cdcd8dSJohannes Weiner
debugfs_get_stored_pages(void * data,u64 * val)17066e1fa555SKanchana P Sridhar static int debugfs_get_stored_pages(void *data, u64 *val)
17076e1fa555SKanchana P Sridhar {
17086e1fa555SKanchana P Sridhar *val = atomic_long_read(&zswap_stored_pages);
17096e1fa555SKanchana P Sridhar return 0;
17106e1fa555SKanchana P Sridhar }
17116e1fa555SKanchana P Sridhar DEFINE_DEBUGFS_ATTRIBUTE(stored_pages_fops, debugfs_get_stored_pages, NULL, "%llu\n");
17126e1fa555SKanchana P Sridhar
debugfs_get_stored_incompressible_pages(void * data,u64 * val)1713dca4437aSSeongJae Park static int debugfs_get_stored_incompressible_pages(void *data, u64 *val)
1714dca4437aSSeongJae Park {
1715dca4437aSSeongJae Park *val = atomic_long_read(&zswap_stored_incompressible_pages);
1716dca4437aSSeongJae Park return 0;
1717dca4437aSSeongJae Park }
1718dca4437aSSeongJae Park DEFINE_DEBUGFS_ATTRIBUTE(stored_incompressible_pages_fops,
1719dca4437aSSeongJae Park debugfs_get_stored_incompressible_pages, NULL, "%llu\n");
1720dca4437aSSeongJae Park
zswap_debugfs_init(void)1721141fdeecSLiu Shixin static int zswap_debugfs_init(void)
17222b281117SSeth Jennings {
17232b281117SSeth Jennings if (!debugfs_initialized())
17242b281117SSeth Jennings return -ENODEV;
17252b281117SSeth Jennings
17262b281117SSeth Jennings zswap_debugfs_root = debugfs_create_dir("zswap", NULL);
17272b281117SSeth Jennings
17280825a6f9SJoe Perches debugfs_create_u64("pool_limit_hit", 0444,
17292b281117SSeth Jennings zswap_debugfs_root, &zswap_pool_limit_hit);
17300825a6f9SJoe Perches debugfs_create_u64("reject_reclaim_fail", 0444,
17312b281117SSeth Jennings zswap_debugfs_root, &zswap_reject_reclaim_fail);
17320825a6f9SJoe Perches debugfs_create_u64("reject_alloc_fail", 0444,
17332b281117SSeth Jennings zswap_debugfs_root, &zswap_reject_alloc_fail);
17340825a6f9SJoe Perches debugfs_create_u64("reject_kmemcache_fail", 0444,
17352b281117SSeth Jennings zswap_debugfs_root, &zswap_reject_kmemcache_fail);
1736cb61dad8SNhat Pham debugfs_create_u64("reject_compress_fail", 0444,
1737cb61dad8SNhat Pham zswap_debugfs_root, &zswap_reject_compress_fail);
17380825a6f9SJoe Perches debugfs_create_u64("reject_compress_poor", 0444,
17392b281117SSeth Jennings zswap_debugfs_root, &zswap_reject_compress_poor);
1740ff22f929SNhat Pham debugfs_create_u64("decompress_fail", 0444,
1741ff22f929SNhat Pham zswap_debugfs_root, &zswap_decompress_fail);
17420825a6f9SJoe Perches debugfs_create_u64("written_back_pages", 0444,
17432b281117SSeth Jennings zswap_debugfs_root, &zswap_written_back_pages);
174491cdcd8dSJohannes Weiner debugfs_create_file("pool_total_size", 0444,
174591cdcd8dSJohannes Weiner zswap_debugfs_root, NULL, &total_size_fops);
17466e1fa555SKanchana P Sridhar debugfs_create_file("stored_pages", 0444,
17476e1fa555SKanchana P Sridhar zswap_debugfs_root, NULL, &stored_pages_fops);
1748dca4437aSSeongJae Park debugfs_create_file("stored_incompressible_pages", 0444,
1749dca4437aSSeongJae Park zswap_debugfs_root, NULL,
1750dca4437aSSeongJae Park &stored_incompressible_pages_fops);
17512b281117SSeth Jennings
17522b281117SSeth Jennings return 0;
17532b281117SSeth Jennings }
17542b281117SSeth Jennings #else
zswap_debugfs_init(void)1755141fdeecSLiu Shixin static int zswap_debugfs_init(void)
17562b281117SSeth Jennings {
17572b281117SSeth Jennings return 0;
17582b281117SSeth Jennings }
17592b281117SSeth Jennings #endif
17602b281117SSeth Jennings
17612b281117SSeth Jennings /*********************************
17622b281117SSeth Jennings * module init and exit
17632b281117SSeth Jennings **********************************/
zswap_setup(void)1764141fdeecSLiu Shixin static int zswap_setup(void)
17652b281117SSeth Jennings {
1766f1c54846SDan Streetman struct zswap_pool *pool;
1767ad7ed770SSebastian Andrzej Siewior int ret;
176860105e12SMinchan Kim
1769b7919122SLiu Shixin zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
1770b7919122SLiu Shixin if (!zswap_entry_cache) {
17712b281117SSeth Jennings pr_err("entry cache creation failed\n");
1772f1c54846SDan Streetman goto cache_fail;
17732b281117SSeth Jennings }
1774f1c54846SDan Streetman
1775cab7a7e5SSebastian Andrzej Siewior ret = cpuhp_setup_state_multi(CPUHP_MM_ZSWP_POOL_PREPARE,
1776cab7a7e5SSebastian Andrzej Siewior "mm/zswap_pool:prepare",
1777cab7a7e5SSebastian Andrzej Siewior zswap_cpu_comp_prepare,
1778*ef3c0f6cSKanchana P. Sridhar NULL);
1779cab7a7e5SSebastian Andrzej Siewior if (ret)
1780cab7a7e5SSebastian Andrzej Siewior goto hp_fail;
1781cab7a7e5SSebastian Andrzej Siewior
1782bf9b7df2SChengming Zhou shrink_wq = alloc_workqueue("zswap-shrink",
1783bf9b7df2SChengming Zhou WQ_UNBOUND|WQ_MEM_RECLAIM, 1);
1784bf9b7df2SChengming Zhou if (!shrink_wq)
1785bf9b7df2SChengming Zhou goto shrink_wq_fail;
1786bf9b7df2SChengming Zhou
1787e35606e4SChengming Zhou zswap_shrinker = zswap_alloc_shrinker();
1788e35606e4SChengming Zhou if (!zswap_shrinker)
1789bf9b7df2SChengming Zhou goto shrinker_fail;
1790e35606e4SChengming Zhou if (list_lru_init_memcg(&zswap_list_lru, zswap_shrinker))
1791bf9b7df2SChengming Zhou goto lru_fail;
1792e35606e4SChengming Zhou shrinker_register(zswap_shrinker);
1793bf9b7df2SChengming Zhou
1794e35606e4SChengming Zhou INIT_WORK(&zswap_shrink_work, shrink_worker);
1795bf9b7df2SChengming Zhou
1796f1c54846SDan Streetman pool = __zswap_pool_create_fallback();
1797ae3d89a7SDan Streetman if (pool) {
17985c3f8be0SJohannes Weiner pr_info("loaded using pool %s\n", pool->tfm_name);
1799f1c54846SDan Streetman list_add(&pool->list, &zswap_pools);
1800ae3d89a7SDan Streetman zswap_has_pool = true;
18012d4d2b1cSYosry Ahmed static_branch_enable(&zswap_ever_enabled);
1802ae3d89a7SDan Streetman } else {
1803ae3d89a7SDan Streetman pr_err("pool creation failed\n");
1804ae3d89a7SDan Streetman zswap_enabled = false;
1805ae3d89a7SDan Streetman }
180660105e12SMinchan Kim
18072b281117SSeth Jennings if (zswap_debugfs_init())
18082b281117SSeth Jennings pr_warn("debugfs initialization failed\n");
18099021ccecSLiu Shixin zswap_init_state = ZSWAP_INIT_SUCCEED;
18102b281117SSeth Jennings return 0;
1811f1c54846SDan Streetman
1812bf9b7df2SChengming Zhou lru_fail:
1813e35606e4SChengming Zhou shrinker_free(zswap_shrinker);
1814bf9b7df2SChengming Zhou shrinker_fail:
1815bf9b7df2SChengming Zhou destroy_workqueue(shrink_wq);
1816bf9b7df2SChengming Zhou shrink_wq_fail:
1817bf9b7df2SChengming Zhou cpuhp_remove_multi_state(CPUHP_MM_ZSWP_POOL_PREPARE);
1818cab7a7e5SSebastian Andrzej Siewior hp_fail:
1819b7919122SLiu Shixin kmem_cache_destroy(zswap_entry_cache);
1820f1c54846SDan Streetman cache_fail:
1821d7b028f5SDan Streetman /* if built-in, we aren't unloaded on failure; don't allow use */
18229021ccecSLiu Shixin zswap_init_state = ZSWAP_INIT_FAILED;
1823d7b028f5SDan Streetman zswap_enabled = false;
18242b281117SSeth Jennings return -ENOMEM;
18252b281117SSeth Jennings }
1826141fdeecSLiu Shixin
zswap_init(void)1827141fdeecSLiu Shixin static int __init zswap_init(void)
1828141fdeecSLiu Shixin {
1829141fdeecSLiu Shixin if (!zswap_enabled)
1830141fdeecSLiu Shixin return 0;
1831141fdeecSLiu Shixin return zswap_setup();
1832141fdeecSLiu Shixin }
18332b281117SSeth Jennings /* must be late so crypto has time to come up */
1834141fdeecSLiu Shixin late_initcall(zswap_init);
18352b281117SSeth Jennings
183668386da8SSeth Jennings MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
18372b281117SSeth Jennings MODULE_DESCRIPTION("Compressed cache for swap pages");
1838