blob: a50e2986cd2fab3491e38f9ba56958ac0c35f84c [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Seth Jennings2b281112013-07-10 16:05:03 -07002/*
3 * zswap.c - zswap driver file
4 *
Johannes Weiner42c06a02023-07-17 12:02:27 -04005 * zswap is a cache that takes pages that are in the process
Seth Jennings2b281112013-07-10 16:05:03 -07006 * of being swapped out and attempts to compress and store them in a
7 * RAM-based memory pool. This can result in a significant I/O reduction on
8 * the swap device and, in the case where decompressing from RAM is faster
9 * than reading from the swap device, can also improve workload performance.
10 *
11 * Copyright (C) 2012 Seth Jennings <sjenning@linux.vnet.ibm.com>
Seth Jennings2b281112013-07-10 16:05:03 -070012*/
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/module.h>
17#include <linux/cpu.h>
18#include <linux/highmem.h>
19#include <linux/slab.h>
20#include <linux/spinlock.h>
21#include <linux/types.h>
22#include <linux/atomic.h>
Seth Jennings2b281112013-07-10 16:05:03 -070023#include <linux/swap.h>
24#include <linux/crypto.h>
Barry Song1ec3b5f2020-12-14 19:14:18 -080025#include <linux/scatterlist.h>
Hugh Dickinsddc1a5c2023-10-19 13:39:08 -070026#include <linux/mempolicy.h>
Seth Jennings2b281112013-07-10 16:05:03 -070027#include <linux/mempool.h>
Dan Streetman12d79d62014-08-06 16:08:40 -070028#include <linux/zpool.h>
Barry Song1ec3b5f2020-12-14 19:14:18 -080029#include <crypto/acompress.h>
Johannes Weiner42c06a02023-07-17 12:02:27 -040030#include <linux/zswap.h>
Seth Jennings2b281112013-07-10 16:05:03 -070031#include <linux/mm_types.h>
32#include <linux/page-flags.h>
33#include <linux/swapops.h>
34#include <linux/writeback.h>
35#include <linux/pagemap.h>
Vitaly Wool45190f02020-01-30 22:15:04 -080036#include <linux/workqueue.h>
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -080037#include <linux/list_lru.h>
Seth Jennings2b281112013-07-10 16:05:03 -070038
NeilBrown014bb1d2022-05-09 18:20:47 -070039#include "swap.h"
Domenico Cerasuoloe0228d52023-05-26 20:32:27 +020040#include "internal.h"
NeilBrown014bb1d2022-05-09 18:20:47 -070041
Seth Jennings2b281112013-07-10 16:05:03 -070042/*********************************
43* statistics
44**********************************/
Seth Jennings2b281112013-07-10 16:05:03 -070045/* The number of compressed pages currently stored in zswap */
Johannes Weinerf6498b72022-05-19 14:08:53 -070046atomic_t zswap_stored_pages = ATOMIC_INIT(0);
Srividya Desireddya85f8782018-01-31 16:15:59 -080047/* The number of same-value filled pages currently stored in zswap */
48static atomic_t zswap_same_filled_pages = ATOMIC_INIT(0);
Seth Jennings2b281112013-07-10 16:05:03 -070049
50/*
51 * The statistics below are not protected from concurrent access for
52 * performance reasons so they may not be a 100% accurate. However,
53 * they do provide useful information on roughly how many times a
54 * certain event is occurring.
55*/
56
57/* Pool limit was hit (see zswap_max_pool_percent) */
58static u64 zswap_pool_limit_hit;
59/* Pages written back when pool limit was reached */
60static u64 zswap_written_back_pages;
61/* Store failed due to a reclaim failure after pool limit was reached */
62static u64 zswap_reject_reclaim_fail;
Nhat Phamcb61dad2023-10-24 16:45:09 -070063/* Store failed due to compression algorithm failure */
64static u64 zswap_reject_compress_fail;
Seth Jennings2b281112013-07-10 16:05:03 -070065/* Compressed page was too big for the allocator to (optimally) store */
66static u64 zswap_reject_compress_poor;
67/* Store failed because underlying allocator could not get memory */
68static u64 zswap_reject_alloc_fail;
69/* Store failed because the entry metadata could not be allocated (rare) */
70static u64 zswap_reject_kmemcache_fail;
Seth Jennings2b281112013-07-10 16:05:03 -070071
Vitaly Wool45190f02020-01-30 22:15:04 -080072/* Shrinker work queue */
73static struct workqueue_struct *shrink_wq;
74/* Pool limit was hit, we need to calm down */
75static bool zswap_pool_reached_full;
76
Seth Jennings2b281112013-07-10 16:05:03 -070077/*********************************
78* tunables
79**********************************/
Dan Streetmanc00ed162015-06-25 15:00:35 -070080
Dan Streetmanbae21db2017-02-27 14:26:50 -080081#define ZSWAP_PARAM_UNSET ""
82
Liu Shixin141fdee2023-04-03 20:13:18 +080083static int zswap_setup(void);
84
Maciej S. Szmigierobb8b93b2020-04-06 20:08:03 -070085/* Enable/disable zswap */
86static bool zswap_enabled = IS_ENABLED(CONFIG_ZSWAP_DEFAULT_ON);
Dan Streetmand7b028f2017-02-03 13:13:09 -080087static int zswap_enabled_param_set(const char *,
88 const struct kernel_param *);
Joe Perches83aed6c2020-12-14 19:14:11 -080089static const struct kernel_param_ops zswap_enabled_param_ops = {
Dan Streetmand7b028f2017-02-03 13:13:09 -080090 .set = zswap_enabled_param_set,
91 .get = param_get_bool,
92};
93module_param_cb(enabled, &zswap_enabled_param_ops, &zswap_enabled, 0644);
Seth Jennings2b281112013-07-10 16:05:03 -070094
Dan Streetman90b0fc22015-09-09 15:35:21 -070095/* Crypto compressor to use */
Maciej S. Szmigierobb8b93b2020-04-06 20:08:03 -070096static char *zswap_compressor = CONFIG_ZSWAP_COMPRESSOR_DEFAULT;
Dan Streetman90b0fc22015-09-09 15:35:21 -070097static int zswap_compressor_param_set(const char *,
98 const struct kernel_param *);
Joe Perches83aed6c2020-12-14 19:14:11 -080099static const struct kernel_param_ops zswap_compressor_param_ops = {
Dan Streetman90b0fc22015-09-09 15:35:21 -0700100 .set = zswap_compressor_param_set,
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800101 .get = param_get_charp,
102 .free = param_free_charp,
Dan Streetman90b0fc22015-09-09 15:35:21 -0700103};
104module_param_cb(compressor, &zswap_compressor_param_ops,
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800105 &zswap_compressor, 0644);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700106
107/* Compressed storage zpool to use */
Maciej S. Szmigierobb8b93b2020-04-06 20:08:03 -0700108static char *zswap_zpool_type = CONFIG_ZSWAP_ZPOOL_DEFAULT;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700109static int zswap_zpool_param_set(const char *, const struct kernel_param *);
Joe Perches83aed6c2020-12-14 19:14:11 -0800110static const struct kernel_param_ops zswap_zpool_param_ops = {
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800111 .set = zswap_zpool_param_set,
112 .get = param_get_charp,
113 .free = param_free_charp,
Dan Streetman90b0fc22015-09-09 15:35:21 -0700114};
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800115module_param_cb(zpool, &zswap_zpool_param_ops, &zswap_zpool_type, 0644);
Seth Jennings2b281112013-07-10 16:05:03 -0700116
117/* The maximum percentage of memory that the compressed pool can occupy */
118static unsigned int zswap_max_pool_percent = 20;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700119module_param_named(max_pool_percent, zswap_max_pool_percent, uint, 0644);
Minchan Kim60105e12014-04-07 15:38:27 -0700120
Vitaly Wool45190f02020-01-30 22:15:04 -0800121/* The threshold for accepting new pages after the max_pool_percent was hit */
122static unsigned int zswap_accept_thr_percent = 90; /* of max pool size */
123module_param_named(accept_threshold_percent, zswap_accept_thr_percent,
124 uint, 0644);
125
Yosry Ahmedb8cf32d2023-06-20 19:46:44 +0000126/* Number of zpools in zswap_pool (empirically determined for scalability) */
127#define ZSWAP_NR_ZPOOLS 32
128
Nhat Phamb5ba4742023-11-30 11:40:23 -0800129/* Enable/disable memory pressure-based shrinker. */
130static bool zswap_shrinker_enabled = IS_ENABLED(
131 CONFIG_ZSWAP_SHRINKER_DEFAULT_ON);
132module_param_named(shrinker_enabled, zswap_shrinker_enabled, bool, 0644);
133
Nhat Pham501a06f2023-12-07 11:24:06 -0800134bool is_zswap_enabled(void)
135{
136 return zswap_enabled;
137}
138
Seth Jennings2b281112013-07-10 16:05:03 -0700139/*********************************
Seth Jennings2b281112013-07-10 16:05:03 -0700140* data structures
141**********************************/
Dan Streetmanf1c54842015-09-09 15:35:19 -0700142
Barry Song1ec3b5f2020-12-14 19:14:18 -0800143struct crypto_acomp_ctx {
144 struct crypto_acomp *acomp;
145 struct acomp_req *req;
146 struct crypto_wait wait;
Chengming Zhou8ba2f842023-12-28 09:45:46 +0000147 u8 *buffer;
148 struct mutex mutex;
Barry Song270700d2024-02-22 21:11:35 +1300149 bool is_sleepable;
Barry Song1ec3b5f2020-12-14 19:14:18 -0800150};
151
Domenico Cerasuolof999f382023-06-12 11:38:09 +0200152/*
153 * The lock ordering is zswap_tree.lock -> zswap_pool.lru_lock.
154 * The only case where lru_lock is not acquired while holding tree.lock is
155 * when a zswap_entry is taken off the lru for writeback, in that case it
156 * needs to be verified that it's still valid in the tree.
157 */
Dan Streetmanf1c54842015-09-09 15:35:19 -0700158struct zswap_pool {
Yosry Ahmedb8cf32d2023-06-20 19:46:44 +0000159 struct zpool *zpools[ZSWAP_NR_ZPOOLS];
Barry Song1ec3b5f2020-12-14 19:14:18 -0800160 struct crypto_acomp_ctx __percpu *acomp_ctx;
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000161 struct percpu_ref ref;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700162 struct list_head list;
Vitaly Wool45190f02020-01-30 22:15:04 -0800163 struct work_struct release_work;
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100164 struct hlist_node node;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700165 char tfm_name[CRYPTO_MAX_ALG_NAME];
166};
167
Chengming Zhoue35606e2024-03-05 07:53:45 +0000168/* Global LRU lists shared by all zswap pools. */
169static struct list_lru zswap_list_lru;
Chengming Zhoue35606e2024-03-05 07:53:45 +0000170
171/* The lock protects zswap_next_shrink updates. */
172static DEFINE_SPINLOCK(zswap_shrink_lock);
173static struct mem_cgroup *zswap_next_shrink;
174static struct work_struct zswap_shrink_work;
175static struct shrinker *zswap_shrinker;
Chengming Zhoubf9b7df2024-02-16 08:55:04 +0000176
Seth Jennings2b281112013-07-10 16:05:03 -0700177/*
178 * struct zswap_entry
179 *
180 * This structure contains the metadata for tracking a single compressed
181 * page within zswap.
182 *
Xiu Jianfeng97157d82023-08-08 06:20:56 +0000183 * swpentry - associated swap entry, the offset indexes into the red-black tree
Seth Jennings2b281112013-07-10 16:05:03 -0700184 * length - the length in bytes of the compressed page data. Needed during
Domenico Cerasuolof999f382023-06-12 11:38:09 +0200185 * decompression. For a same value filled page length is 0, and both
186 * pool and lru are invalid and must be ignored.
Dan Streetmanf1c54842015-09-09 15:35:19 -0700187 * pool - the zswap_pool the entry's data is in
188 * handle - zpool allocation handle that stores the compressed page data
Srividya Desireddya85f8782018-01-31 16:15:59 -0800189 * value - value of the same-value filled pages which have same content
Xiu Jianfeng97157d82023-08-08 06:20:56 +0000190 * objcg - the obj_cgroup that the compressed memory is charged to
Domenico Cerasuolof999f382023-06-12 11:38:09 +0200191 * lru - handle to the pool's lru used to evict pages.
Seth Jennings2b281112013-07-10 16:05:03 -0700192 */
193struct zswap_entry {
Domenico Cerasuolo0bb48842023-06-12 11:38:15 +0200194 swp_entry_t swpentry;
Seth Jennings2b281112013-07-10 16:05:03 -0700195 unsigned int length;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700196 struct zswap_pool *pool;
Srividya Desireddya85f8782018-01-31 16:15:59 -0800197 union {
198 unsigned long handle;
199 unsigned long value;
200 };
Johannes Weinerf4840cc2022-05-19 14:08:53 -0700201 struct obj_cgroup *objcg;
Domenico Cerasuolof999f382023-06-12 11:38:09 +0200202 struct list_head lru;
Seth Jennings2b281112013-07-10 16:05:03 -0700203};
204
Chris Li796c2c22024-03-26 11:35:43 -0700205static struct xarray *zswap_trees[MAX_SWAPFILES];
Chengming Zhou44c7c732024-01-19 11:22:23 +0000206static unsigned int nr_zswap_trees[MAX_SWAPFILES];
Seth Jennings2b281112013-07-10 16:05:03 -0700207
Dan Streetmanf1c54842015-09-09 15:35:19 -0700208/* RCU-protected iteration */
209static LIST_HEAD(zswap_pools);
210/* protects zswap_pools list modification */
211static DEFINE_SPINLOCK(zswap_pools_lock);
Dan Streetman32a4e1692016-05-05 16:22:23 -0700212/* pool counter to provide unique names to zpool */
213static atomic_t zswap_pools_count = ATOMIC_INIT(0);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700214
Liu Shixin9021cce2023-04-03 20:13:17 +0800215enum zswap_init_type {
216 ZSWAP_UNINIT,
217 ZSWAP_INIT_SUCCEED,
218 ZSWAP_INIT_FAILED
219};
Dan Streetman90b0fc22015-09-09 15:35:21 -0700220
Liu Shixin9021cce2023-04-03 20:13:17 +0800221static enum zswap_init_type zswap_init_state;
Dan Streetmand7b028f2017-02-03 13:13:09 -0800222
Liu Shixin141fdee2023-04-03 20:13:18 +0800223/* used to ensure the integrity of initialization */
224static DEFINE_MUTEX(zswap_init_lock);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700225
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800226/* init completed, but couldn't create the initial pool */
227static bool zswap_has_pool;
228
Dan Streetmanf1c54842015-09-09 15:35:19 -0700229/*********************************
230* helpers and fwd declarations
231**********************************/
232
Chris Li796c2c22024-03-26 11:35:43 -0700233static inline struct xarray *swap_zswap_tree(swp_entry_t swp)
Chengming Zhou44c7c732024-01-19 11:22:23 +0000234{
235 return &zswap_trees[swp_type(swp)][swp_offset(swp)
236 >> SWAP_ADDRESS_SPACE_SHIFT];
237}
238
Dan Streetmanf1c54842015-09-09 15:35:19 -0700239#define zswap_pool_debug(msg, p) \
240 pr_debug("%s pool %s/%s\n", msg, (p)->tfm_name, \
Yosry Ahmedb8cf32d2023-06-20 19:46:44 +0000241 zpool_get_type((p)->zpools[0]))
Dan Streetmanf1c54842015-09-09 15:35:19 -0700242
Johannes Weinera9846492024-01-29 20:36:46 -0500243/*********************************
244* pool functions
245**********************************/
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000246static void __zswap_pool_empty(struct percpu_ref *ref);
Johannes Weinera9846492024-01-29 20:36:46 -0500247
Johannes Weinera9846492024-01-29 20:36:46 -0500248static struct zswap_pool *zswap_pool_create(char *type, char *compressor)
249{
250 int i;
251 struct zswap_pool *pool;
252 char name[38]; /* 'zswap' + 32 char (max) num + \0 */
253 gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;
254 int ret;
255
256 if (!zswap_has_pool) {
257 /* if either are unset, pool initialization failed, and we
258 * need both params to be set correctly before trying to
259 * create a pool.
260 */
261 if (!strcmp(type, ZSWAP_PARAM_UNSET))
262 return NULL;
263 if (!strcmp(compressor, ZSWAP_PARAM_UNSET))
264 return NULL;
265 }
266
267 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
268 if (!pool)
269 return NULL;
270
271 for (i = 0; i < ZSWAP_NR_ZPOOLS; i++) {
272 /* unique name for each pool specifically required by zsmalloc */
273 snprintf(name, 38, "zswap%x",
274 atomic_inc_return(&zswap_pools_count));
275
276 pool->zpools[i] = zpool_create_pool(type, name, gfp);
277 if (!pool->zpools[i]) {
278 pr_err("%s zpool not available\n", type);
279 goto error;
280 }
281 }
282 pr_debug("using %s zpool\n", zpool_get_type(pool->zpools[0]));
283
284 strscpy(pool->tfm_name, compressor, sizeof(pool->tfm_name));
285
286 pool->acomp_ctx = alloc_percpu(*pool->acomp_ctx);
287 if (!pool->acomp_ctx) {
288 pr_err("percpu alloc failed\n");
289 goto error;
290 }
291
292 ret = cpuhp_state_add_instance(CPUHP_MM_ZSWP_POOL_PREPARE,
293 &pool->node);
294 if (ret)
295 goto error;
296
Johannes Weinera9846492024-01-29 20:36:46 -0500297 /* being the current pool takes 1 ref; this func expects the
298 * caller to always add the new pool as the current pool
299 */
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000300 ret = percpu_ref_init(&pool->ref, __zswap_pool_empty,
301 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL);
302 if (ret)
303 goto ref_fail;
Johannes Weinera9846492024-01-29 20:36:46 -0500304 INIT_LIST_HEAD(&pool->list);
Johannes Weinera9846492024-01-29 20:36:46 -0500305
306 zswap_pool_debug("created", pool);
307
308 return pool;
309
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000310ref_fail:
311 cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node);
Johannes Weinera9846492024-01-29 20:36:46 -0500312error:
313 if (pool->acomp_ctx)
314 free_percpu(pool->acomp_ctx);
315 while (i--)
316 zpool_destroy_pool(pool->zpools[i]);
317 kfree(pool);
318 return NULL;
319}
320
321static struct zswap_pool *__zswap_pool_create_fallback(void)
322{
323 bool has_comp, has_zpool;
324
325 has_comp = crypto_has_acomp(zswap_compressor, 0, 0);
326 if (!has_comp && strcmp(zswap_compressor,
327 CONFIG_ZSWAP_COMPRESSOR_DEFAULT)) {
328 pr_err("compressor %s not available, using default %s\n",
329 zswap_compressor, CONFIG_ZSWAP_COMPRESSOR_DEFAULT);
330 param_free_charp(&zswap_compressor);
331 zswap_compressor = CONFIG_ZSWAP_COMPRESSOR_DEFAULT;
332 has_comp = crypto_has_acomp(zswap_compressor, 0, 0);
333 }
334 if (!has_comp) {
335 pr_err("default compressor %s not available\n",
336 zswap_compressor);
337 param_free_charp(&zswap_compressor);
338 zswap_compressor = ZSWAP_PARAM_UNSET;
339 }
340
341 has_zpool = zpool_has_pool(zswap_zpool_type);
342 if (!has_zpool && strcmp(zswap_zpool_type,
343 CONFIG_ZSWAP_ZPOOL_DEFAULT)) {
344 pr_err("zpool %s not available, using default %s\n",
345 zswap_zpool_type, CONFIG_ZSWAP_ZPOOL_DEFAULT);
346 param_free_charp(&zswap_zpool_type);
347 zswap_zpool_type = CONFIG_ZSWAP_ZPOOL_DEFAULT;
348 has_zpool = zpool_has_pool(zswap_zpool_type);
349 }
350 if (!has_zpool) {
351 pr_err("default zpool %s not available\n",
352 zswap_zpool_type);
353 param_free_charp(&zswap_zpool_type);
354 zswap_zpool_type = ZSWAP_PARAM_UNSET;
355 }
356
357 if (!has_comp || !has_zpool)
358 return NULL;
359
360 return zswap_pool_create(zswap_zpool_type, zswap_compressor);
361}
362
363static void zswap_pool_destroy(struct zswap_pool *pool)
364{
365 int i;
366
367 zswap_pool_debug("destroying", pool);
368
Johannes Weinera9846492024-01-29 20:36:46 -0500369 cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node);
370 free_percpu(pool->acomp_ctx);
Johannes Weinera9846492024-01-29 20:36:46 -0500371
372 for (i = 0; i < ZSWAP_NR_ZPOOLS; i++)
373 zpool_destroy_pool(pool->zpools[i]);
374 kfree(pool);
375}
376
Johannes Weiner39f3ec82024-01-29 20:36:47 -0500377static void __zswap_pool_release(struct work_struct *work)
378{
379 struct zswap_pool *pool = container_of(work, typeof(*pool),
380 release_work);
381
382 synchronize_rcu();
383
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000384 /* nobody should have been able to get a ref... */
385 WARN_ON(!percpu_ref_is_zero(&pool->ref));
386 percpu_ref_exit(&pool->ref);
Johannes Weiner39f3ec82024-01-29 20:36:47 -0500387
388 /* pool is now off zswap_pools list and has no references. */
389 zswap_pool_destroy(pool);
390}
391
392static struct zswap_pool *zswap_pool_current(void);
393
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000394static void __zswap_pool_empty(struct percpu_ref *ref)
Johannes Weiner39f3ec82024-01-29 20:36:47 -0500395{
396 struct zswap_pool *pool;
397
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000398 pool = container_of(ref, typeof(*pool), ref);
Johannes Weiner39f3ec82024-01-29 20:36:47 -0500399
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000400 spin_lock_bh(&zswap_pools_lock);
Johannes Weiner39f3ec82024-01-29 20:36:47 -0500401
402 WARN_ON(pool == zswap_pool_current());
403
404 list_del_rcu(&pool->list);
405
406 INIT_WORK(&pool->release_work, __zswap_pool_release);
407 schedule_work(&pool->release_work);
408
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000409 spin_unlock_bh(&zswap_pools_lock);
Johannes Weiner39f3ec82024-01-29 20:36:47 -0500410}
411
412static int __must_check zswap_pool_get(struct zswap_pool *pool)
413{
414 if (!pool)
415 return 0;
416
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000417 return percpu_ref_tryget(&pool->ref);
Johannes Weiner39f3ec82024-01-29 20:36:47 -0500418}
419
420static void zswap_pool_put(struct zswap_pool *pool)
421{
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000422 percpu_ref_put(&pool->ref);
Johannes Weiner39f3ec82024-01-29 20:36:47 -0500423}
424
Johannes Weinerc1a0ecb2024-01-29 20:36:48 -0500425static struct zswap_pool *__zswap_pool_current(void)
426{
427 struct zswap_pool *pool;
428
429 pool = list_first_or_null_rcu(&zswap_pools, typeof(*pool), list);
430 WARN_ONCE(!pool && zswap_has_pool,
431 "%s: no page storage pool!\n", __func__);
432
433 return pool;
434}
435
436static struct zswap_pool *zswap_pool_current(void)
437{
438 assert_spin_locked(&zswap_pools_lock);
439
440 return __zswap_pool_current();
441}
442
443static struct zswap_pool *zswap_pool_current_get(void)
444{
445 struct zswap_pool *pool;
446
447 rcu_read_lock();
448
449 pool = __zswap_pool_current();
450 if (!zswap_pool_get(pool))
451 pool = NULL;
452
453 rcu_read_unlock();
454
455 return pool;
456}
457
Johannes Weinerc1a0ecb2024-01-29 20:36:48 -0500458/* type and compressor must be null-terminated */
459static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor)
460{
461 struct zswap_pool *pool;
462
463 assert_spin_locked(&zswap_pools_lock);
464
465 list_for_each_entry_rcu(pool, &zswap_pools, list) {
466 if (strcmp(pool->tfm_name, compressor))
467 continue;
468 /* all zpools share the same type */
469 if (strcmp(zpool_get_type(pool->zpools[0]), type))
470 continue;
471 /* if we can't get it, it's about to be destroyed */
472 if (!zswap_pool_get(pool))
473 continue;
474 return pool;
475 }
476
477 return NULL;
478}
479
Johannes Weiner91cdcd82024-03-12 11:34:11 -0400480static unsigned long zswap_max_pages(void)
481{
482 return totalram_pages() * zswap_max_pool_percent / 100;
483}
484
485static unsigned long zswap_accept_thr_pages(void)
486{
487 return zswap_max_pages() * zswap_accept_thr_percent / 100;
488}
489
490unsigned long zswap_total_pages(void)
491{
492 struct zswap_pool *pool;
Johannes Weiner4196b482024-03-12 11:34:12 -0400493 unsigned long total = 0;
Johannes Weiner91cdcd82024-03-12 11:34:11 -0400494
495 rcu_read_lock();
496 list_for_each_entry_rcu(pool, &zswap_pools, list) {
497 int i;
498
499 for (i = 0; i < ZSWAP_NR_ZPOOLS; i++)
Johannes Weiner4196b482024-03-12 11:34:12 -0400500 total += zpool_get_total_pages(pool->zpools[i]);
Johannes Weiner91cdcd82024-03-12 11:34:11 -0400501 }
502 rcu_read_unlock();
503
Johannes Weiner4196b482024-03-12 11:34:12 -0400504 return total;
Johannes Weiner91cdcd82024-03-12 11:34:11 -0400505}
506
Yosry Ahmed82e0f8e2024-04-13 02:24:05 +0000507static bool zswap_check_limits(void)
508{
509 unsigned long cur_pages = zswap_total_pages();
510 unsigned long max_pages = zswap_max_pages();
511
512 if (cur_pages >= max_pages) {
513 zswap_pool_limit_hit++;
514 zswap_pool_reached_full = true;
515 } else if (zswap_pool_reached_full &&
516 cur_pages <= zswap_accept_thr_pages()) {
517 zswap_pool_reached_full = false;
518 }
519 return zswap_pool_reached_full;
520}
521
Johannes Weinerabca07c2024-01-29 20:36:49 -0500522/*********************************
523* param callbacks
524**********************************/
525
526static bool zswap_pool_changed(const char *s, const struct kernel_param *kp)
527{
528 /* no change required */
529 if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool)
530 return false;
531 return true;
532}
533
534/* val must be a null-terminated string */
535static int __zswap_param_set(const char *val, const struct kernel_param *kp,
536 char *type, char *compressor)
537{
538 struct zswap_pool *pool, *put_pool = NULL;
539 char *s = strstrip((char *)val);
540 int ret = 0;
541 bool new_pool = false;
542
543 mutex_lock(&zswap_init_lock);
544 switch (zswap_init_state) {
545 case ZSWAP_UNINIT:
546 /* if this is load-time (pre-init) param setting,
547 * don't create a pool; that's done during init.
548 */
549 ret = param_set_charp(s, kp);
550 break;
551 case ZSWAP_INIT_SUCCEED:
552 new_pool = zswap_pool_changed(s, kp);
553 break;
554 case ZSWAP_INIT_FAILED:
555 pr_err("can't set param, initialization failed\n");
556 ret = -ENODEV;
557 }
558 mutex_unlock(&zswap_init_lock);
559
560 /* no need to create a new pool, return directly */
561 if (!new_pool)
562 return ret;
563
564 if (!type) {
565 if (!zpool_has_pool(s)) {
566 pr_err("zpool %s not available\n", s);
567 return -ENOENT;
568 }
569 type = s;
570 } else if (!compressor) {
571 if (!crypto_has_acomp(s, 0, 0)) {
572 pr_err("compressor %s not available\n", s);
573 return -ENOENT;
574 }
575 compressor = s;
576 } else {
577 WARN_ON(1);
578 return -EINVAL;
579 }
580
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000581 spin_lock_bh(&zswap_pools_lock);
Johannes Weinerabca07c2024-01-29 20:36:49 -0500582
583 pool = zswap_pool_find_get(type, compressor);
584 if (pool) {
585 zswap_pool_debug("using existing", pool);
586 WARN_ON(pool == zswap_pool_current());
587 list_del_rcu(&pool->list);
588 }
589
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000590 spin_unlock_bh(&zswap_pools_lock);
Johannes Weinerabca07c2024-01-29 20:36:49 -0500591
592 if (!pool)
593 pool = zswap_pool_create(type, compressor);
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000594 else {
595 /*
596 * Restore the initial ref dropped by percpu_ref_kill()
597 * when the pool was decommissioned and switch it again
598 * to percpu mode.
599 */
600 percpu_ref_resurrect(&pool->ref);
601
602 /* Drop the ref from zswap_pool_find_get(). */
603 zswap_pool_put(pool);
604 }
Johannes Weinerabca07c2024-01-29 20:36:49 -0500605
606 if (pool)
607 ret = param_set_charp(s, kp);
608 else
609 ret = -EINVAL;
610
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000611 spin_lock_bh(&zswap_pools_lock);
Johannes Weinerabca07c2024-01-29 20:36:49 -0500612
613 if (!ret) {
614 put_pool = zswap_pool_current();
615 list_add_rcu(&pool->list, &zswap_pools);
616 zswap_has_pool = true;
617 } else if (pool) {
618 /* add the possibly pre-existing pool to the end of the pools
619 * list; if it's new (and empty) then it'll be removed and
620 * destroyed by the put after we drop the lock
621 */
622 list_add_tail_rcu(&pool->list, &zswap_pools);
623 put_pool = pool;
624 }
625
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000626 spin_unlock_bh(&zswap_pools_lock);
Johannes Weinerabca07c2024-01-29 20:36:49 -0500627
628 if (!zswap_has_pool && !pool) {
629 /* if initial pool creation failed, and this pool creation also
630 * failed, maybe both compressor and zpool params were bad.
631 * Allow changing this param, so pool creation will succeed
632 * when the other param is changed. We already verified this
633 * param is ok in the zpool_has_pool() or crypto_has_acomp()
634 * checks above.
635 */
636 ret = param_set_charp(s, kp);
637 }
638
639 /* drop the ref from either the old current pool,
640 * or the new pool we failed to add
641 */
642 if (put_pool)
Chengming Zhou94ace3f2024-02-16 08:55:05 +0000643 percpu_ref_kill(&put_pool->ref);
Johannes Weinerabca07c2024-01-29 20:36:49 -0500644
645 return ret;
646}
647
648static int zswap_compressor_param_set(const char *val,
649 const struct kernel_param *kp)
650{
651 return __zswap_param_set(val, kp, zswap_zpool_type, NULL);
652}
653
654static int zswap_zpool_param_set(const char *val,
655 const struct kernel_param *kp)
656{
657 return __zswap_param_set(val, kp, NULL, zswap_compressor);
658}
659
660static int zswap_enabled_param_set(const char *val,
661 const struct kernel_param *kp)
662{
663 int ret = -ENODEV;
664
665 /* if this is load-time (pre-init) param setting, only set param. */
666 if (system_state != SYSTEM_RUNNING)
667 return param_set_bool(val, kp);
668
669 mutex_lock(&zswap_init_lock);
670 switch (zswap_init_state) {
671 case ZSWAP_UNINIT:
672 if (zswap_setup())
673 break;
674 fallthrough;
675 case ZSWAP_INIT_SUCCEED:
676 if (!zswap_has_pool)
677 pr_err("can't enable, no pool configured\n");
678 else
679 ret = param_set_bool(val, kp);
680 break;
681 case ZSWAP_INIT_FAILED:
682 pr_err("can't enable, initialization failed\n");
683 }
684 mutex_unlock(&zswap_init_lock);
685
686 return ret;
687}
688
Johannes Weiner506a86c2024-01-29 20:36:50 -0500689/*********************************
690* lru functions
691**********************************/
692
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -0800693/* should be called under RCU */
694#ifdef CONFIG_MEMCG
695static inline struct mem_cgroup *mem_cgroup_from_entry(struct zswap_entry *entry)
696{
697 return entry->objcg ? obj_cgroup_memcg(entry->objcg) : NULL;
698}
699#else
700static inline struct mem_cgroup *mem_cgroup_from_entry(struct zswap_entry *entry)
701{
702 return NULL;
703}
704#endif
705
706static inline int entry_to_nid(struct zswap_entry *entry)
707{
708 return page_to_nid(virt_to_page(entry));
709}
710
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -0800711static void zswap_lru_add(struct list_lru *list_lru, struct zswap_entry *entry)
712{
Nhat Phamb5ba4742023-11-30 11:40:23 -0800713 atomic_long_t *nr_zswap_protected;
714 unsigned long lru_size, old, new;
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -0800715 int nid = entry_to_nid(entry);
716 struct mem_cgroup *memcg;
Nhat Phamb5ba4742023-11-30 11:40:23 -0800717 struct lruvec *lruvec;
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -0800718
719 /*
720 * Note that it is safe to use rcu_read_lock() here, even in the face of
721 * concurrent memcg offlining. Thanks to the memcg->kmemcg_id indirection
722 * used in list_lru lookup, only two scenarios are possible:
723 *
724 * 1. list_lru_add() is called before memcg->kmemcg_id is updated. The
725 * new entry will be reparented to memcg's parent's list_lru.
726 * 2. list_lru_add() is called after memcg->kmemcg_id is updated. The
727 * new entry will be added directly to memcg's parent's list_lru.
728 *
Chengming Zhou3f798aa2024-01-28 13:28:51 +0000729 * Similar reasoning holds for list_lru_del().
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -0800730 */
731 rcu_read_lock();
732 memcg = mem_cgroup_from_entry(entry);
733 /* will always succeed */
734 list_lru_add(list_lru, &entry->lru, nid, memcg);
Nhat Phamb5ba4742023-11-30 11:40:23 -0800735
736 /* Update the protection area */
737 lru_size = list_lru_count_one(list_lru, nid, memcg);
738 lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid));
739 nr_zswap_protected = &lruvec->zswap_lruvec_state.nr_zswap_protected;
740 old = atomic_long_inc_return(nr_zswap_protected);
741 /*
742 * Decay to avoid overflow and adapt to changing workloads.
743 * This is based on LRU reclaim cost decaying heuristics.
744 */
745 do {
746 new = old > lru_size / 4 ? old / 2 : old;
747 } while (!atomic_long_try_cmpxchg(nr_zswap_protected, &old, new));
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -0800748 rcu_read_unlock();
749}
750
751static void zswap_lru_del(struct list_lru *list_lru, struct zswap_entry *entry)
752{
753 int nid = entry_to_nid(entry);
754 struct mem_cgroup *memcg;
755
756 rcu_read_lock();
757 memcg = mem_cgroup_from_entry(entry);
758 /* will always succeed */
759 list_lru_del(list_lru, &entry->lru, nid, memcg);
760 rcu_read_unlock();
761}
762
Johannes Weiner5182661a2024-01-29 20:36:51 -0500763void zswap_lruvec_state_init(struct lruvec *lruvec)
764{
765 atomic_long_set(&lruvec->zswap_lruvec_state.nr_zswap_protected, 0);
766}
767
768void zswap_folio_swapin(struct folio *folio)
769{
770 struct lruvec *lruvec;
771
772 if (folio) {
773 lruvec = folio_lruvec(folio);
774 atomic_long_inc(&lruvec->zswap_lruvec_state.nr_zswap_protected);
775 }
776}
777
778void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg)
779{
Chengming Zhoubf9b7df2024-02-16 08:55:04 +0000780 /* lock out zswap shrinker walking memcg tree */
Chengming Zhoue35606e2024-03-05 07:53:45 +0000781 spin_lock(&zswap_shrink_lock);
782 if (zswap_next_shrink == memcg)
783 zswap_next_shrink = mem_cgroup_iter(NULL, zswap_next_shrink, NULL);
784 spin_unlock(&zswap_shrink_lock);
Johannes Weiner5182661a2024-01-29 20:36:51 -0500785}
786
787/*********************************
Johannes Weiner36034bf2024-01-29 20:36:52 -0500788* zswap entry functions
789**********************************/
790static struct kmem_cache *zswap_entry_cache;
791
792static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp, int nid)
793{
794 struct zswap_entry *entry;
795 entry = kmem_cache_alloc_node(zswap_entry_cache, gfp, nid);
796 if (!entry)
797 return NULL;
Johannes Weiner36034bf2024-01-29 20:36:52 -0500798 return entry;
799}
800
801static void zswap_entry_cache_free(struct zswap_entry *entry)
802{
803 kmem_cache_free(zswap_entry_cache, entry);
804}
805
Yosry Ahmedb8cf32d2023-06-20 19:46:44 +0000806static struct zpool *zswap_find_zpool(struct zswap_entry *entry)
807{
Yosry Ahmedfea68a72024-03-11 23:52:10 +0000808 return entry->pool->zpools[hash_ptr(entry, ilog2(ZSWAP_NR_ZPOOLS))];
Yosry Ahmedb8cf32d2023-06-20 19:46:44 +0000809}
810
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800811/*
Dan Streetman12d79d62014-08-06 16:08:40 -0700812 * Carries out the common pattern of freeing and entry's zpool allocation,
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800813 * freeing the entry itself, and decrementing the number of stored pages.
814 */
Johannes Weiner42398be2024-01-29 20:36:37 -0500815static void zswap_entry_free(struct zswap_entry *entry)
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800816{
Srividya Desireddya85f8782018-01-31 16:15:59 -0800817 if (!entry->length)
818 atomic_dec(&zswap_same_filled_pages);
819 else {
Chengming Zhoue35606e2024-03-05 07:53:45 +0000820 zswap_lru_del(&zswap_list_lru, entry);
Yosry Ahmedb8cf32d2023-06-20 19:46:44 +0000821 zpool_free(zswap_find_zpool(entry), entry->handle);
Srividya Desireddya85f8782018-01-31 16:15:59 -0800822 zswap_pool_put(entry->pool);
823 }
Johannes Weiner2e601e12024-01-29 20:34:38 -0500824 if (entry->objcg) {
825 obj_cgroup_uncharge_zswap(entry->objcg, entry->length);
826 obj_cgroup_put(entry->objcg);
827 }
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800828 zswap_entry_cache_free(entry);
829 atomic_dec(&zswap_stored_pages);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800830}
831
Seth Jennings2b281112013-07-10 16:05:03 -0700832/*********************************
Johannes Weinerf91e81d2024-01-29 20:36:53 -0500833* compressed storage functions
834**********************************/
Johannes Weiner64f200b2024-01-29 20:36:54 -0500835static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
836{
837 struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
838 struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu);
839 struct crypto_acomp *acomp;
840 struct acomp_req *req;
841 int ret;
842
843 mutex_init(&acomp_ctx->mutex);
844
845 acomp_ctx->buffer = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
846 if (!acomp_ctx->buffer)
847 return -ENOMEM;
848
849 acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, cpu_to_node(cpu));
850 if (IS_ERR(acomp)) {
851 pr_err("could not alloc crypto acomp %s : %ld\n",
852 pool->tfm_name, PTR_ERR(acomp));
853 ret = PTR_ERR(acomp);
854 goto acomp_fail;
855 }
856 acomp_ctx->acomp = acomp;
Barry Song270700d2024-02-22 21:11:35 +1300857 acomp_ctx->is_sleepable = acomp_is_async(acomp);
Johannes Weiner64f200b2024-01-29 20:36:54 -0500858
859 req = acomp_request_alloc(acomp_ctx->acomp);
860 if (!req) {
861 pr_err("could not alloc crypto acomp_request %s\n",
862 pool->tfm_name);
863 ret = -ENOMEM;
864 goto req_fail;
865 }
866 acomp_ctx->req = req;
867
868 crypto_init_wait(&acomp_ctx->wait);
869 /*
870 * if the backend of acomp is async zip, crypto_req_done() will wakeup
871 * crypto_wait_req(); if the backend of acomp is scomp, the callback
872 * won't be called, crypto_wait_req() will return without blocking.
873 */
874 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
875 crypto_req_done, &acomp_ctx->wait);
876
877 return 0;
878
879req_fail:
880 crypto_free_acomp(acomp_ctx->acomp);
881acomp_fail:
882 kfree(acomp_ctx->buffer);
883 return ret;
884}
885
886static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)
887{
888 struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
889 struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu);
890
891 if (!IS_ERR_OR_NULL(acomp_ctx)) {
892 if (!IS_ERR_OR_NULL(acomp_ctx->req))
893 acomp_request_free(acomp_ctx->req);
894 if (!IS_ERR_OR_NULL(acomp_ctx->acomp))
895 crypto_free_acomp(acomp_ctx->acomp);
896 kfree(acomp_ctx->buffer);
897 }
898
899 return 0;
900}
901
Johannes Weinerf91e81d2024-01-29 20:36:53 -0500902static bool zswap_compress(struct folio *folio, struct zswap_entry *entry)
903{
904 struct crypto_acomp_ctx *acomp_ctx;
905 struct scatterlist input, output;
Barry Song55e78c92024-02-20 10:19:35 +1300906 int comp_ret = 0, alloc_ret = 0;
Johannes Weinerf91e81d2024-01-29 20:36:53 -0500907 unsigned int dlen = PAGE_SIZE;
908 unsigned long handle;
909 struct zpool *zpool;
910 char *buf;
911 gfp_t gfp;
Johannes Weinerf91e81d2024-01-29 20:36:53 -0500912 u8 *dst;
913
914 acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
915
916 mutex_lock(&acomp_ctx->mutex);
917
918 dst = acomp_ctx->buffer;
919 sg_init_table(&input, 1);
920 sg_set_page(&input, &folio->page, PAGE_SIZE, 0);
921
922 /*
923 * We need PAGE_SIZE * 2 here since there maybe over-compression case,
924 * and hardware-accelerators may won't check the dst buffer size, so
925 * giving the dst buffer with enough length to avoid buffer overflow.
926 */
927 sg_init_one(&output, dst, PAGE_SIZE * 2);
928 acomp_request_set_params(acomp_ctx->req, &input, &output, PAGE_SIZE, dlen);
929
930 /*
931 * it maybe looks a little bit silly that we send an asynchronous request,
932 * then wait for its completion synchronously. This makes the process look
933 * synchronous in fact.
934 * Theoretically, acomp supports users send multiple acomp requests in one
935 * acomp instance, then get those requests done simultaneously. but in this
936 * case, zswap actually does store and load page by page, there is no
937 * existing method to send the second page before the first page is done
938 * in one thread doing zwap.
939 * but in different threads running on different cpu, we have different
940 * acomp instance, so multiple threads can do (de)compression in parallel.
941 */
Barry Song55e78c92024-02-20 10:19:35 +1300942 comp_ret = crypto_wait_req(crypto_acomp_compress(acomp_ctx->req), &acomp_ctx->wait);
Johannes Weinerf91e81d2024-01-29 20:36:53 -0500943 dlen = acomp_ctx->req->dlen;
Barry Song55e78c92024-02-20 10:19:35 +1300944 if (comp_ret)
Johannes Weinerf91e81d2024-01-29 20:36:53 -0500945 goto unlock;
Johannes Weinerf91e81d2024-01-29 20:36:53 -0500946
947 zpool = zswap_find_zpool(entry);
948 gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;
949 if (zpool_malloc_support_movable(zpool))
950 gfp |= __GFP_HIGHMEM | __GFP_MOVABLE;
Barry Song55e78c92024-02-20 10:19:35 +1300951 alloc_ret = zpool_malloc(zpool, dlen, gfp, &handle);
952 if (alloc_ret)
Johannes Weinerf91e81d2024-01-29 20:36:53 -0500953 goto unlock;
Johannes Weinerf91e81d2024-01-29 20:36:53 -0500954
955 buf = zpool_map_handle(zpool, handle, ZPOOL_MM_WO);
956 memcpy(buf, dst, dlen);
957 zpool_unmap_handle(zpool, handle);
958
959 entry->handle = handle;
960 entry->length = dlen;
961
962unlock:
Barry Song55e78c92024-02-20 10:19:35 +1300963 if (comp_ret == -ENOSPC || alloc_ret == -ENOSPC)
964 zswap_reject_compress_poor++;
965 else if (comp_ret)
966 zswap_reject_compress_fail++;
967 else if (alloc_ret)
968 zswap_reject_alloc_fail++;
969
Johannes Weinerf91e81d2024-01-29 20:36:53 -0500970 mutex_unlock(&acomp_ctx->mutex);
Barry Song55e78c92024-02-20 10:19:35 +1300971 return comp_ret == 0 && alloc_ret == 0;
Johannes Weinerf91e81d2024-01-29 20:36:53 -0500972}
973
974static void zswap_decompress(struct zswap_entry *entry, struct page *page)
975{
976 struct zpool *zpool = zswap_find_zpool(entry);
977 struct scatterlist input, output;
978 struct crypto_acomp_ctx *acomp_ctx;
979 u8 *src;
980
981 acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
982 mutex_lock(&acomp_ctx->mutex);
983
984 src = zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO);
Barry Song9c500832024-03-19 12:47:06 +1300985 /*
986 * If zpool_map_handle is atomic, we cannot reliably utilize its mapped buffer
987 * to do crypto_acomp_decompress() which might sleep. In such cases, we must
988 * resort to copying the buffer to a temporary one.
989 * Meanwhile, zpool_map_handle() might return a non-linearly mapped buffer,
990 * such as a kmap address of high memory or even ever a vmap address.
991 * However, sg_init_one is only equipped to handle linearly mapped low memory.
992 * In such cases, we also must copy the buffer to a temporary and lowmem one.
993 */
994 if ((acomp_ctx->is_sleepable && !zpool_can_sleep_mapped(zpool)) ||
995 !virt_addr_valid(src)) {
Johannes Weinerf91e81d2024-01-29 20:36:53 -0500996 memcpy(acomp_ctx->buffer, src, entry->length);
997 src = acomp_ctx->buffer;
998 zpool_unmap_handle(zpool, entry->handle);
999 }
1000
1001 sg_init_one(&input, src, entry->length);
1002 sg_init_table(&output, 1);
1003 sg_set_page(&output, page, PAGE_SIZE, 0);
1004 acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, PAGE_SIZE);
1005 BUG_ON(crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait));
1006 BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE);
1007 mutex_unlock(&acomp_ctx->mutex);
1008
Barry Song9c500832024-03-19 12:47:06 +13001009 if (src != acomp_ctx->buffer)
Johannes Weinerf91e81d2024-01-29 20:36:53 -05001010 zpool_unmap_handle(zpool, entry->handle);
1011}
1012
1013/*********************************
Johannes Weiner9986d352024-01-29 20:36:55 -05001014* writeback code
1015**********************************/
1016/*
1017 * Attempts to free an entry by adding a folio to the swap cache,
1018 * decompressing the entry data into the folio, and issuing a
1019 * bio write to write the folio back to the swap device.
1020 *
1021 * This can be thought of as a "resumed writeback" of the folio
1022 * to the swap device. We are basically resuming the same swap
1023 * writeback path that was intercepted with the zswap_store()
1024 * in the first place. After the folio has been decompressed into
1025 * the swap cache, the compressed version stored by zswap can be
1026 * freed.
1027 */
1028static int zswap_writeback_entry(struct zswap_entry *entry,
1029 swp_entry_t swpentry)
1030{
Chris Li796c2c22024-03-26 11:35:43 -07001031 struct xarray *tree;
1032 pgoff_t offset = swp_offset(swpentry);
Johannes Weiner9986d352024-01-29 20:36:55 -05001033 struct folio *folio;
1034 struct mempolicy *mpol;
1035 bool folio_was_allocated;
1036 struct writeback_control wbc = {
1037 .sync_mode = WB_SYNC_NONE,
1038 };
1039
1040 /* try to allocate swap cache folio */
1041 mpol = get_task_policy(current);
1042 folio = __read_swap_cache_async(swpentry, GFP_KERNEL, mpol,
1043 NO_INTERLEAVE_INDEX, &folio_was_allocated, true);
1044 if (!folio)
1045 return -ENOMEM;
1046
1047 /*
1048 * Found an existing folio, we raced with swapin or concurrent
1049 * shrinker. We generally writeback cold folios from zswap, and
1050 * swapin means the folio just became hot, so skip this folio.
1051 * For unlikely concurrent shrinker case, it will be unlinked
1052 * and freed when invalidated by the concurrent shrinker anyway.
1053 */
1054 if (!folio_was_allocated) {
1055 folio_put(folio);
1056 return -EEXIST;
1057 }
1058
1059 /*
1060 * folio is locked, and the swapcache is now secured against
Chengming Zhouf9c0f1c2024-02-04 03:05:59 +00001061 * concurrent swapping to and from the slot, and concurrent
1062 * swapoff so we can safely dereference the zswap tree here.
1063 * Verify that the swap entry hasn't been invalidated and recycled
1064 * behind our backs, to avoid overwriting a new swap folio with
1065 * old compressed data. Only when this is successful can the entry
1066 * be dereferenced.
Johannes Weiner9986d352024-01-29 20:36:55 -05001067 */
1068 tree = swap_zswap_tree(swpentry);
Chris Li796c2c22024-03-26 11:35:43 -07001069 if (entry != xa_cmpxchg(tree, offset, entry, NULL, GFP_KERNEL)) {
Johannes Weiner9986d352024-01-29 20:36:55 -05001070 delete_from_swap_cache(folio);
1071 folio_unlock(folio);
1072 folio_put(folio);
1073 return -ENOMEM;
1074 }
1075
Johannes Weiner9986d352024-01-29 20:36:55 -05001076 zswap_decompress(entry, &folio->page);
1077
1078 count_vm_event(ZSWPWB);
1079 if (entry->objcg)
1080 count_objcg_event(entry->objcg, ZSWPWB);
1081
Chengming Zhoua230c202024-02-04 03:06:04 +00001082 zswap_entry_free(entry);
Johannes Weiner9986d352024-01-29 20:36:55 -05001083
1084 /* folio is up to date */
1085 folio_mark_uptodate(folio);
1086
1087 /* move it to the tail of the inactive list after end_writeback */
1088 folio_set_reclaim(folio);
1089
1090 /* start writeback */
1091 __swap_writepage(folio, &wbc);
1092 folio_put(folio);
1093
1094 return 0;
1095}
1096
1097/*********************************
Nhat Phamb5ba4742023-11-30 11:40:23 -08001098* shrinker functions
1099**********************************/
1100static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_one *l,
Johannes Weinereb23ee42024-01-29 20:36:56 -05001101 spinlock_t *lock, void *arg)
1102{
1103 struct zswap_entry *entry = container_of(item, struct zswap_entry, lru);
1104 bool *encountered_page_in_swapcache = (bool *)arg;
1105 swp_entry_t swpentry;
1106 enum lru_status ret = LRU_REMOVED_RETRY;
1107 int writeback_result;
1108
1109 /*
Chengming Zhouf9c0f1c2024-02-04 03:05:59 +00001110 * As soon as we drop the LRU lock, the entry can be freed by
1111 * a concurrent invalidation. This means the following:
Johannes Weinereb23ee42024-01-29 20:36:56 -05001112 *
Chengming Zhouf9c0f1c2024-02-04 03:05:59 +00001113 * 1. We extract the swp_entry_t to the stack, allowing
1114 * zswap_writeback_entry() to pin the swap entry and
1115 * then validate the zwap entry against that swap entry's
1116 * tree using pointer value comparison. Only when that
1117 * is successful can the entry be dereferenced.
Johannes Weinereb23ee42024-01-29 20:36:56 -05001118 *
Chengming Zhouf9c0f1c2024-02-04 03:05:59 +00001119 * 2. Usually, objects are taken off the LRU for reclaim. In
1120 * this case this isn't possible, because if reclaim fails
1121 * for whatever reason, we have no means of knowing if the
1122 * entry is alive to put it back on the LRU.
Johannes Weinereb23ee42024-01-29 20:36:56 -05001123 *
Chengming Zhouf9c0f1c2024-02-04 03:05:59 +00001124 * So rotate it before dropping the lock. If the entry is
1125 * written back or invalidated, the free path will unlink
1126 * it. For failures, rotation is the right thing as well.
1127 *
1128 * Temporary failures, where the same entry should be tried
1129 * again immediately, almost never happen for this shrinker.
1130 * We don't do any trylocking; -ENOMEM comes closest,
1131 * but that's extremely rare and doesn't happen spuriously
1132 * either. Don't bother distinguishing this case.
Johannes Weinereb23ee42024-01-29 20:36:56 -05001133 */
1134 list_move_tail(item, &l->list);
1135
1136 /*
1137 * Once the lru lock is dropped, the entry might get freed. The
1138 * swpentry is copied to the stack, and entry isn't deref'd again
1139 * until the entry is verified to still be alive in the tree.
1140 */
1141 swpentry = entry->swpentry;
1142
1143 /*
1144 * It's safe to drop the lock here because we return either
1145 * LRU_REMOVED_RETRY or LRU_RETRY.
1146 */
1147 spin_unlock(lock);
1148
1149 writeback_result = zswap_writeback_entry(entry, swpentry);
1150
1151 if (writeback_result) {
1152 zswap_reject_reclaim_fail++;
1153 ret = LRU_RETRY;
1154
1155 /*
1156 * Encountering a page already in swap cache is a sign that we are shrinking
1157 * into the warmer region. We should terminate shrinking (if we're in the dynamic
1158 * shrinker context).
1159 */
Chengming Zhoub49547ad2024-02-04 03:06:01 +00001160 if (writeback_result == -EEXIST && encountered_page_in_swapcache) {
1161 ret = LRU_STOP;
Johannes Weinereb23ee42024-01-29 20:36:56 -05001162 *encountered_page_in_swapcache = true;
Chengming Zhoub49547ad2024-02-04 03:06:01 +00001163 }
Johannes Weinereb23ee42024-01-29 20:36:56 -05001164 } else {
1165 zswap_written_back_pages++;
1166 }
1167
1168 spin_lock(lock);
1169 return ret;
1170}
Nhat Phamb5ba4742023-11-30 11:40:23 -08001171
1172static unsigned long zswap_shrinker_scan(struct shrinker *shrinker,
1173 struct shrink_control *sc)
1174{
1175 struct lruvec *lruvec = mem_cgroup_lruvec(sc->memcg, NODE_DATA(sc->nid));
1176 unsigned long shrink_ret, nr_protected, lru_size;
Nhat Phamb5ba4742023-11-30 11:40:23 -08001177 bool encountered_page_in_swapcache = false;
1178
Nhat Pham501a06f2023-12-07 11:24:06 -08001179 if (!zswap_shrinker_enabled ||
1180 !mem_cgroup_zswap_writeback_enabled(sc->memcg)) {
Nhat Phamb5ba4742023-11-30 11:40:23 -08001181 sc->nr_scanned = 0;
1182 return SHRINK_STOP;
1183 }
1184
1185 nr_protected =
1186 atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected);
Chengming Zhoue35606e2024-03-05 07:53:45 +00001187 lru_size = list_lru_shrink_count(&zswap_list_lru, sc);
Nhat Phamb5ba4742023-11-30 11:40:23 -08001188
1189 /*
1190 * Abort if we are shrinking into the protected region.
1191 *
1192 * This short-circuiting is necessary because if we have too many multiple
1193 * concurrent reclaimers getting the freeable zswap object counts at the
1194 * same time (before any of them made reasonable progress), the total
1195 * number of reclaimed objects might be more than the number of unprotected
1196 * objects (i.e the reclaimers will reclaim into the protected area of the
1197 * zswap LRU).
1198 */
1199 if (nr_protected >= lru_size - sc->nr_to_scan) {
1200 sc->nr_scanned = 0;
1201 return SHRINK_STOP;
1202 }
1203
Chengming Zhoue35606e2024-03-05 07:53:45 +00001204 shrink_ret = list_lru_shrink_walk(&zswap_list_lru, sc, &shrink_memcg_cb,
Nhat Phamb5ba4742023-11-30 11:40:23 -08001205 &encountered_page_in_swapcache);
1206
1207 if (encountered_page_in_swapcache)
1208 return SHRINK_STOP;
1209
1210 return shrink_ret ? shrink_ret : SHRINK_STOP;
1211}
1212
1213static unsigned long zswap_shrinker_count(struct shrinker *shrinker,
1214 struct shrink_control *sc)
1215{
Nhat Phamb5ba4742023-11-30 11:40:23 -08001216 struct mem_cgroup *memcg = sc->memcg;
1217 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(sc->nid));
1218 unsigned long nr_backing, nr_stored, nr_freeable, nr_protected;
1219
Nhat Pham501a06f2023-12-07 11:24:06 -08001220 if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(memcg))
Nhat Phamb5ba4742023-11-30 11:40:23 -08001221 return 0;
1222
Johannes Weiner30fb6a82024-03-21 14:25:32 -04001223 /*
1224 * The shrinker resumes swap writeback, which will enter block
1225 * and may enter fs. XXX: Harmonize with vmscan.c __GFP_FS
1226 * rules (may_enter_fs()), which apply on a per-folio basis.
1227 */
1228 if (!gfp_has_io_fs(sc->gfp_mask))
1229 return 0;
1230
Johannes Weiner682886e2024-04-18 08:26:28 -04001231 /*
1232 * For memcg, use the cgroup-wide ZSWAP stats since we don't
1233 * have them per-node and thus per-lruvec. Careful if memcg is
1234 * runtime-disabled: we can get sc->memcg == NULL, which is ok
1235 * for the lruvec, but not for memcg_page_state().
1236 *
1237 * Without memcg, use the zswap pool-wide metrics.
1238 */
1239 if (!mem_cgroup_disabled()) {
1240 mem_cgroup_flush_stats(memcg);
1241 nr_backing = memcg_page_state(memcg, MEMCG_ZSWAP_B) >> PAGE_SHIFT;
1242 nr_stored = memcg_page_state(memcg, MEMCG_ZSWAPPED);
1243 } else {
Johannes Weiner91cdcd82024-03-12 11:34:11 -04001244 nr_backing = zswap_total_pages();
Yosry Ahmedcc9bc362024-03-22 00:10:01 +00001245 nr_stored = atomic_read(&zswap_stored_pages);
Johannes Weiner682886e2024-04-18 08:26:28 -04001246 }
Nhat Phamb5ba4742023-11-30 11:40:23 -08001247
1248 if (!nr_stored)
1249 return 0;
1250
1251 nr_protected =
1252 atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected);
Chengming Zhoue35606e2024-03-05 07:53:45 +00001253 nr_freeable = list_lru_shrink_count(&zswap_list_lru, sc);
Nhat Phamb5ba4742023-11-30 11:40:23 -08001254 /*
1255 * Subtract the lru size by an estimate of the number of pages
1256 * that should be protected.
1257 */
1258 nr_freeable = nr_freeable > nr_protected ? nr_freeable - nr_protected : 0;
1259
1260 /*
1261 * Scale the number of freeable pages by the memory saving factor.
1262 * This ensures that the better zswap compresses memory, the fewer
1263 * pages we will evict to swap (as it will otherwise incur IO for
1264 * relatively small memory saving).
Yosry Ahmedcc9bc362024-03-22 00:10:01 +00001265 *
1266 * The memory saving factor calculated here takes same-filled pages into
1267 * account, but those are not freeable since they almost occupy no
1268 * space. Hence, we may scale nr_freeable down a little bit more than we
1269 * should if we have a lot of same-filled pages.
Nhat Phamb5ba4742023-11-30 11:40:23 -08001270 */
1271 return mult_frac(nr_freeable, nr_backing, nr_stored);
1272}
1273
Chengming Zhoubf9b7df2024-02-16 08:55:04 +00001274static struct shrinker *zswap_alloc_shrinker(void)
Nhat Phamb5ba4742023-11-30 11:40:23 -08001275{
Chengming Zhoubf9b7df2024-02-16 08:55:04 +00001276 struct shrinker *shrinker;
Nhat Phamb5ba4742023-11-30 11:40:23 -08001277
Chengming Zhoubf9b7df2024-02-16 08:55:04 +00001278 shrinker =
1279 shrinker_alloc(SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE, "mm-zswap");
1280 if (!shrinker)
1281 return NULL;
1282
1283 shrinker->scan_objects = zswap_shrinker_scan;
1284 shrinker->count_objects = zswap_shrinker_count;
1285 shrinker->batch = 0;
1286 shrinker->seeks = DEFAULT_SEEKS;
1287 return shrinker;
Nhat Phamb5ba4742023-11-30 11:40:23 -08001288}
1289
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001290static int shrink_memcg(struct mem_cgroup *memcg)
1291{
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001292 int nid, shrunk = 0;
1293
Nhat Pham501a06f2023-12-07 11:24:06 -08001294 if (!mem_cgroup_zswap_writeback_enabled(memcg))
1295 return -EINVAL;
1296
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001297 /*
1298 * Skip zombies because their LRUs are reparented and we would be
1299 * reclaiming from the parent instead of the dead memcg.
1300 */
1301 if (memcg && !mem_cgroup_online(memcg))
1302 return -ENOENT;
1303
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001304 for_each_node_state(nid, N_NORMAL_MEMORY) {
1305 unsigned long nr_to_walk = 1;
1306
Chengming Zhoue35606e2024-03-05 07:53:45 +00001307 shrunk += list_lru_walk_one(&zswap_list_lru, nid, memcg,
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001308 &shrink_memcg_cb, NULL, &nr_to_walk);
1309 }
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001310 return shrunk ? 0 : -EAGAIN;
Domenico Cerasuolof999f382023-06-12 11:38:09 +02001311}
1312
Vitaly Wool45190f02020-01-30 22:15:04 -08001313static void shrink_worker(struct work_struct *w)
1314{
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001315 struct mem_cgroup *memcg;
Domenico Cerasuoloe0228d52023-05-26 20:32:27 +02001316 int ret, failures = 0;
Johannes Weiner91cdcd82024-03-12 11:34:11 -04001317 unsigned long thr;
1318
1319 /* Reclaim down to the accept threshold */
1320 thr = zswap_accept_thr_pages();
Vitaly Wool45190f02020-01-30 22:15:04 -08001321
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001322 /* global reclaim will select cgroup in a round-robin fashion. */
Domenico Cerasuoloe0228d52023-05-26 20:32:27 +02001323 do {
Chengming Zhoue35606e2024-03-05 07:53:45 +00001324 spin_lock(&zswap_shrink_lock);
1325 zswap_next_shrink = mem_cgroup_iter(NULL, zswap_next_shrink, NULL);
1326 memcg = zswap_next_shrink;
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001327
1328 /*
1329 * We need to retry if we have gone through a full round trip, or if we
1330 * got an offline memcg (or else we risk undoing the effect of the
1331 * zswap memcg offlining cleanup callback). This is not catastrophic
1332 * per se, but it will keep the now offlined memcg hostage for a while.
1333 *
1334 * Note that if we got an online memcg, we will keep the extra
1335 * reference in case the original reference obtained by mem_cgroup_iter
1336 * is dropped by the zswap memcg offlining callback, ensuring that the
1337 * memcg is not killed when we are reclaiming.
1338 */
1339 if (!memcg) {
Chengming Zhoue35606e2024-03-05 07:53:45 +00001340 spin_unlock(&zswap_shrink_lock);
Domenico Cerasuoloe0228d52023-05-26 20:32:27 +02001341 if (++failures == MAX_RECLAIM_RETRIES)
1342 break;
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001343
1344 goto resched;
Domenico Cerasuoloe0228d52023-05-26 20:32:27 +02001345 }
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001346
1347 if (!mem_cgroup_tryget_online(memcg)) {
1348 /* drop the reference from mem_cgroup_iter() */
1349 mem_cgroup_iter_break(NULL, memcg);
Chengming Zhoue35606e2024-03-05 07:53:45 +00001350 zswap_next_shrink = NULL;
1351 spin_unlock(&zswap_shrink_lock);
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001352
1353 if (++failures == MAX_RECLAIM_RETRIES)
1354 break;
1355
1356 goto resched;
1357 }
Chengming Zhoue35606e2024-03-05 07:53:45 +00001358 spin_unlock(&zswap_shrink_lock);
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001359
1360 ret = shrink_memcg(memcg);
1361 /* drop the extra reference */
1362 mem_cgroup_put(memcg);
1363
1364 if (ret == -EINVAL)
1365 break;
1366 if (ret && ++failures == MAX_RECLAIM_RETRIES)
1367 break;
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001368resched:
Domenico Cerasuoloe0228d52023-05-26 20:32:27 +02001369 cond_resched();
Johannes Weiner91cdcd82024-03-12 11:34:11 -04001370 } while (zswap_total_pages() > thr);
Vitaly Wool45190f02020-01-30 22:15:04 -08001371}
1372
Yosry Ahmede87b8812024-04-13 02:24:06 +00001373/*********************************
1374* same-filled functions
1375**********************************/
1376static bool zswap_is_folio_same_filled(struct folio *folio, unsigned long *value)
Srividya Desireddya85f8782018-01-31 16:15:59 -08001377{
Srividya Desireddya85f8782018-01-31 16:15:59 -08001378 unsigned long *page;
Taejoon Song62bf1252023-02-06 04:00:36 +09001379 unsigned long val;
1380 unsigned int pos, last_pos = PAGE_SIZE / sizeof(*page) - 1;
Yosry Ahmede87b8812024-04-13 02:24:06 +00001381 bool ret = false;
Srividya Desireddya85f8782018-01-31 16:15:59 -08001382
Yosry Ahmede87b8812024-04-13 02:24:06 +00001383 page = kmap_local_folio(folio, 0);
Taejoon Song62bf1252023-02-06 04:00:36 +09001384 val = page[0];
1385
1386 if (val != page[last_pos])
Yosry Ahmede87b8812024-04-13 02:24:06 +00001387 goto out;
Taejoon Song62bf1252023-02-06 04:00:36 +09001388
1389 for (pos = 1; pos < last_pos; pos++) {
1390 if (val != page[pos])
Yosry Ahmede87b8812024-04-13 02:24:06 +00001391 goto out;
Srividya Desireddya85f8782018-01-31 16:15:59 -08001392 }
Taejoon Song62bf1252023-02-06 04:00:36 +09001393
1394 *value = val;
Yosry Ahmede87b8812024-04-13 02:24:06 +00001395 ret = true;
1396out:
1397 kunmap_local(page);
1398 return ret;
Srividya Desireddya85f8782018-01-31 16:15:59 -08001399}
1400
1401static void zswap_fill_page(void *ptr, unsigned long value)
1402{
1403 unsigned long *page;
1404
1405 page = (unsigned long *)ptr;
1406 memset_l(page, value, PAGE_SIZE / sizeof(unsigned long));
1407}
1408
Yosry Ahmede87b8812024-04-13 02:24:06 +00001409/*********************************
1410* main API
1411**********************************/
Matthew Wilcox (Oracle)34f4c192023-07-15 05:23:40 +01001412bool zswap_store(struct folio *folio)
Seth Jennings2b281112013-07-10 16:05:03 -07001413{
David Hildenbrand3d2c9082023-08-21 18:08:48 +02001414 swp_entry_t swp = folio->swap;
Johannes Weiner42c06a02023-07-17 12:02:27 -04001415 pgoff_t offset = swp_offset(swp);
Chris Li796c2c22024-03-26 11:35:43 -07001416 struct xarray *tree = swap_zswap_tree(swp);
1417 struct zswap_entry *entry, *old;
Johannes Weinerf4840cc2022-05-19 14:08:53 -07001418 struct obj_cgroup *objcg = NULL;
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001419 struct mem_cgroup *memcg = NULL;
Yosry Ahmede87b8812024-04-13 02:24:06 +00001420 unsigned long value;
Johannes Weiner42c06a02023-07-17 12:02:27 -04001421
Matthew Wilcox (Oracle)34f4c192023-07-15 05:23:40 +01001422 VM_WARN_ON_ONCE(!folio_test_locked(folio));
1423 VM_WARN_ON_ONCE(!folio_test_swapcache(folio));
Seth Jennings2b281112013-07-10 16:05:03 -07001424
Matthew Wilcox (Oracle)34f4c192023-07-15 05:23:40 +01001425 /* Large folios aren't supported */
1426 if (folio_test_large(folio))
Johannes Weiner42c06a02023-07-17 12:02:27 -04001427 return false;
Huang Ying7ba71662018-02-21 14:45:39 -08001428
Chengming Zhou678e54d2024-02-08 02:32:54 +00001429 if (!zswap_enabled)
Chengming Zhouf576a1e2024-02-09 04:41:12 +00001430 goto check_old;
Chengming Zhou678e54d2024-02-08 02:32:54 +00001431
Johannes Weiner91cdcd82024-03-12 11:34:11 -04001432 /* Check cgroup limits */
Matthew Wilcox (Oracle)074e3e22023-07-15 05:23:41 +01001433 objcg = get_obj_cgroup_from_folio(folio);
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001434 if (objcg && !obj_cgroup_may_zswap(objcg)) {
1435 memcg = get_mem_cgroup_from_objcg(objcg);
1436 if (shrink_memcg(memcg)) {
1437 mem_cgroup_put(memcg);
1438 goto reject;
1439 }
1440 mem_cgroup_put(memcg);
1441 }
Johannes Weinerf4840cc2022-05-19 14:08:53 -07001442
Yosry Ahmed82e0f8e2024-04-13 02:24:05 +00001443 if (zswap_check_limits())
Yosry Ahmed4ea3fa92024-04-13 02:24:04 +00001444 goto reject;
Seth Jennings2b281112013-07-10 16:05:03 -07001445
1446 /* allocate entry */
Johannes Weinerbe7fc972024-01-29 20:36:44 -05001447 entry = zswap_entry_cache_alloc(GFP_KERNEL, folio_nid(folio));
Seth Jennings2b281112013-07-10 16:05:03 -07001448 if (!entry) {
1449 zswap_reject_kmemcache_fail++;
Seth Jennings2b281112013-07-10 16:05:03 -07001450 goto reject;
1451 }
1452
Yosry Ahmede87b8812024-04-13 02:24:06 +00001453 if (zswap_is_folio_same_filled(folio, &value)) {
1454 entry->length = 0;
1455 entry->value = value;
1456 atomic_inc(&zswap_same_filled_pages);
1457 goto store_entry;
Srividya Desireddya85f8782018-01-31 16:15:59 -08001458 }
1459
Dan Streetmanf1c54842015-09-09 15:35:19 -07001460 /* if entry is successfully added, it keeps the reference */
1461 entry->pool = zswap_pool_current_get();
Johannes Weiner42c06a02023-07-17 12:02:27 -04001462 if (!entry->pool)
Seth Jennings2b281112013-07-10 16:05:03 -07001463 goto freepage;
Seth Jennings2b281112013-07-10 16:05:03 -07001464
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001465 if (objcg) {
1466 memcg = get_mem_cgroup_from_objcg(objcg);
Chengming Zhoue35606e2024-03-05 07:53:45 +00001467 if (memcg_list_lru_alloc(memcg, &zswap_list_lru, GFP_KERNEL)) {
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001468 mem_cgroup_put(memcg);
1469 goto put_pool;
1470 }
1471 mem_cgroup_put(memcg);
1472 }
1473
Johannes Weinerfa9ad6e2024-01-29 20:36:43 -05001474 if (!zswap_compress(folio, entry))
1475 goto put_pool;
Barry Song1ec3b5f2020-12-14 19:14:18 -08001476
Yosry Ahmede87b8812024-04-13 02:24:06 +00001477store_entry:
Johannes Weinerbe7fc972024-01-29 20:36:44 -05001478 entry->swpentry = swp;
Johannes Weinerf4840cc2022-05-19 14:08:53 -07001479 entry->objcg = objcg;
Chris Li796c2c22024-03-26 11:35:43 -07001480
1481 old = xa_store(tree, offset, entry, GFP_KERNEL);
1482 if (xa_is_err(old)) {
1483 int err = xa_err(old);
1484
1485 WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err);
1486 zswap_reject_alloc_fail++;
1487 goto store_failed;
1488 }
1489
1490 /*
1491 * We may have had an existing entry that became stale when
1492 * the folio was redirtied and now the new version is being
1493 * swapped out. Get rid of the old.
1494 */
1495 if (old)
1496 zswap_entry_free(old);
1497
Johannes Weinerf4840cc2022-05-19 14:08:53 -07001498 if (objcg) {
1499 obj_cgroup_charge_zswap(objcg, entry->length);
Johannes Weinerf4840cc2022-05-19 14:08:53 -07001500 count_objcg_event(objcg, ZSWPOUT);
1501 }
1502
Domenico Cerasuoloca564892023-09-22 19:22:11 +02001503 /*
Chris Li796c2c22024-03-26 11:35:43 -07001504 * We finish initializing the entry while it's already in xarray.
1505 * This is safe because:
1506 *
1507 * 1. Concurrent stores and invalidations are excluded by folio lock.
1508 *
1509 * 2. Writeback is excluded by the entry not being on the LRU yet.
1510 * The publishing order matters to prevent writeback from seeing
1511 * an incoherent entry.
Domenico Cerasuoloca564892023-09-22 19:22:11 +02001512 */
Domenico Cerasuolo35499e22023-06-12 11:38:13 +02001513 if (entry->length) {
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001514 INIT_LIST_HEAD(&entry->lru);
Chengming Zhoue35606e2024-03-05 07:53:45 +00001515 zswap_lru_add(&zswap_list_lru, entry);
Domenico Cerasuolof999f382023-06-12 11:38:09 +02001516 }
Seth Jennings2b281112013-07-10 16:05:03 -07001517
1518 /* update stats */
1519 atomic_inc(&zswap_stored_pages);
Johannes Weinerf6498b72022-05-19 14:08:53 -07001520 count_vm_event(ZSWPOUT);
Seth Jennings2b281112013-07-10 16:05:03 -07001521
Johannes Weiner42c06a02023-07-17 12:02:27 -04001522 return true;
Seth Jennings2b281112013-07-10 16:05:03 -07001523
Chris Li796c2c22024-03-26 11:35:43 -07001524store_failed:
1525 if (!entry->length)
1526 atomic_dec(&zswap_same_filled_pages);
1527 else {
1528 zpool_free(zswap_find_zpool(entry), entry->handle);
Domenico Cerasuoloa65b0e72023-11-30 11:40:20 -08001529put_pool:
Chris Li796c2c22024-03-26 11:35:43 -07001530 zswap_pool_put(entry->pool);
1531 }
Dan Streetmanf1c54842015-09-09 15:35:19 -07001532freepage:
Seth Jennings2b281112013-07-10 16:05:03 -07001533 zswap_entry_cache_free(entry);
1534reject:
Yosry Ahmed91b71e782024-03-16 01:58:03 +00001535 obj_cgroup_put(objcg);
Yosry Ahmed4ea3fa92024-04-13 02:24:04 +00001536 if (zswap_pool_reached_full)
1537 queue_work(shrink_wq, &zswap_shrink_work);
Chengming Zhouf576a1e2024-02-09 04:41:12 +00001538check_old:
1539 /*
1540 * If the zswap store fails or zswap is disabled, we must invalidate the
1541 * possibly stale entry which was previously stored at this offset.
1542 * Otherwise, writeback could overwrite the new data in the swapfile.
1543 */
Chris Li796c2c22024-03-26 11:35:43 -07001544 entry = xa_erase(tree, offset);
Chengming Zhouf576a1e2024-02-09 04:41:12 +00001545 if (entry)
Chris Li796c2c22024-03-26 11:35:43 -07001546 zswap_entry_free(entry);
Johannes Weiner42c06a02023-07-17 12:02:27 -04001547 return false;
Seth Jennings2b281112013-07-10 16:05:03 -07001548}
1549
Matthew Wilcox (Oracle)ca54f6d2023-07-15 05:23:43 +01001550bool zswap_load(struct folio *folio)
Seth Jennings2b281112013-07-10 16:05:03 -07001551{
David Hildenbrand3d2c9082023-08-21 18:08:48 +02001552 swp_entry_t swp = folio->swap;
Johannes Weiner42c06a02023-07-17 12:02:27 -04001553 pgoff_t offset = swp_offset(swp);
Matthew Wilcox (Oracle)ca54f6d2023-07-15 05:23:43 +01001554 struct page *page = &folio->page;
Johannes Weiner25cd2412024-03-24 17:04:47 -04001555 bool swapcache = folio_test_swapcache(folio);
Chris Li796c2c22024-03-26 11:35:43 -07001556 struct xarray *tree = swap_zswap_tree(swp);
Seth Jennings2b281112013-07-10 16:05:03 -07001557 struct zswap_entry *entry;
Chengming Zhou32acba4c02023-12-28 09:45:43 +00001558 u8 *dst;
Johannes Weiner42c06a02023-07-17 12:02:27 -04001559
Matthew Wilcox (Oracle)ca54f6d2023-07-15 05:23:43 +01001560 VM_WARN_ON_ONCE(!folio_test_locked(folio));
Seth Jennings2b281112013-07-10 16:05:03 -07001561
Johannes Weiner25cd2412024-03-24 17:04:47 -04001562 /*
1563 * When reading into the swapcache, invalidate our entry. The
1564 * swapcache can be the authoritative owner of the page and
1565 * its mappings, and the pressure that results from having two
1566 * in-memory copies outweighs any benefits of caching the
1567 * compression work.
1568 *
1569 * (Most swapins go through the swapcache. The notable
1570 * exception is the singleton fault on SWP_SYNCHRONOUS_IO
1571 * files, which reads into a private page and may free it if
1572 * the fault fails. We remain the primary owner of the entry.)
1573 */
1574 if (swapcache)
Chris Li796c2c22024-03-26 11:35:43 -07001575 entry = xa_erase(tree, offset);
1576 else
1577 entry = xa_load(tree, offset);
1578
1579 if (!entry)
1580 return false;
Seth Jennings2b281112013-07-10 16:05:03 -07001581
Chengming Zhou66447fd2023-12-28 09:45:44 +00001582 if (entry->length)
Johannes Weinerff2972a2024-01-29 20:36:42 -05001583 zswap_decompress(entry, page);
Chengming Zhou66447fd2023-12-28 09:45:44 +00001584 else {
Fabio M. De Francesco003ae2f2023-11-27 16:55:21 +01001585 dst = kmap_local_page(page);
Srividya Desireddya85f8782018-01-31 16:15:59 -08001586 zswap_fill_page(dst, entry->value);
Fabio M. De Francesco003ae2f2023-11-27 16:55:21 +01001587 kunmap_local(dst);
Srividya Desireddya85f8782018-01-31 16:15:59 -08001588 }
1589
Johannes Weinerf6498b72022-05-19 14:08:53 -07001590 count_vm_event(ZSWPIN);
Johannes Weinerf4840cc2022-05-19 14:08:53 -07001591 if (entry->objcg)
1592 count_objcg_event(entry->objcg, ZSWPIN);
Chengming Zhouc75f5c12023-12-28 09:45:42 +00001593
Johannes Weiner25cd2412024-03-24 17:04:47 -04001594 if (swapcache) {
1595 zswap_entry_free(entry);
1596 folio_mark_dirty(folio);
1597 }
Chengming Zhouc2e2ba72024-02-04 03:06:03 +00001598
Chengming Zhou66447fd2023-12-28 09:45:44 +00001599 return true;
Seth Jennings2b281112013-07-10 16:05:03 -07001600}
1601
Chengming Zhou0827a1f2024-02-04 03:06:00 +00001602void zswap_invalidate(swp_entry_t swp)
Seth Jennings2b281112013-07-10 16:05:03 -07001603{
Chengming Zhou0827a1f2024-02-04 03:06:00 +00001604 pgoff_t offset = swp_offset(swp);
Chris Li796c2c22024-03-26 11:35:43 -07001605 struct xarray *tree = swap_zswap_tree(swp);
Seth Jennings2b281112013-07-10 16:05:03 -07001606 struct zswap_entry *entry;
Seth Jennings2b281112013-07-10 16:05:03 -07001607
Chris Li796c2c22024-03-26 11:35:43 -07001608 entry = xa_erase(tree, offset);
Johannes Weiner06ed2282024-01-29 20:36:45 -05001609 if (entry)
Chris Li796c2c22024-03-26 11:35:43 -07001610 zswap_entry_free(entry);
Seth Jennings2b281112013-07-10 16:05:03 -07001611}
1612
Chengming Zhou44c7c732024-01-19 11:22:23 +00001613int zswap_swapon(int type, unsigned long nr_pages)
Johannes Weiner42c06a02023-07-17 12:02:27 -04001614{
Chris Li796c2c22024-03-26 11:35:43 -07001615 struct xarray *trees, *tree;
Chengming Zhou44c7c732024-01-19 11:22:23 +00001616 unsigned int nr, i;
Johannes Weiner42c06a02023-07-17 12:02:27 -04001617
Chengming Zhou44c7c732024-01-19 11:22:23 +00001618 nr = DIV_ROUND_UP(nr_pages, SWAP_ADDRESS_SPACE_PAGES);
1619 trees = kvcalloc(nr, sizeof(*tree), GFP_KERNEL);
1620 if (!trees) {
Johannes Weiner42c06a02023-07-17 12:02:27 -04001621 pr_err("alloc failed, zswap disabled for swap type %d\n", type);
Chengming Zhoubb29fd72024-01-19 11:22:22 +00001622 return -ENOMEM;
Johannes Weiner42c06a02023-07-17 12:02:27 -04001623 }
1624
Chris Li796c2c22024-03-26 11:35:43 -07001625 for (i = 0; i < nr; i++)
1626 xa_init(trees + i);
Chengming Zhou44c7c732024-01-19 11:22:23 +00001627
1628 nr_zswap_trees[type] = nr;
1629 zswap_trees[type] = trees;
Chengming Zhoubb29fd72024-01-19 11:22:22 +00001630 return 0;
Johannes Weiner42c06a02023-07-17 12:02:27 -04001631}
1632
1633void zswap_swapoff(int type)
Seth Jennings2b281112013-07-10 16:05:03 -07001634{
Chris Li796c2c22024-03-26 11:35:43 -07001635 struct xarray *trees = zswap_trees[type];
Chengming Zhou44c7c732024-01-19 11:22:23 +00001636 unsigned int i;
Seth Jennings2b281112013-07-10 16:05:03 -07001637
Chengming Zhou44c7c732024-01-19 11:22:23 +00001638 if (!trees)
Seth Jennings2b281112013-07-10 16:05:03 -07001639 return;
1640
Yosry Ahmed83e68f22024-01-24 04:51:12 +00001641 /* try_to_unuse() invalidated all the entries already */
1642 for (i = 0; i < nr_zswap_trees[type]; i++)
Chris Li796c2c22024-03-26 11:35:43 -07001643 WARN_ON_ONCE(!xa_empty(trees + i));
Chengming Zhou44c7c732024-01-19 11:22:23 +00001644
1645 kvfree(trees);
1646 nr_zswap_trees[type] = 0;
Weijie Yangaa9bca02013-10-16 13:46:54 -07001647 zswap_trees[type] = NULL;
Seth Jennings2b281112013-07-10 16:05:03 -07001648}
1649
Seth Jennings2b281112013-07-10 16:05:03 -07001650/*********************************
1651* debugfs functions
1652**********************************/
1653#ifdef CONFIG_DEBUG_FS
1654#include <linux/debugfs.h>
1655
1656static struct dentry *zswap_debugfs_root;
1657
Johannes Weiner91cdcd82024-03-12 11:34:11 -04001658static int debugfs_get_total_size(void *data, u64 *val)
1659{
1660 *val = zswap_total_pages() * PAGE_SIZE;
1661 return 0;
1662}
1663DEFINE_DEBUGFS_ATTRIBUTE(total_size_fops, debugfs_get_total_size, NULL, "%llu\n");
1664
Liu Shixin141fdee2023-04-03 20:13:18 +08001665static int zswap_debugfs_init(void)
Seth Jennings2b281112013-07-10 16:05:03 -07001666{
1667 if (!debugfs_initialized())
1668 return -ENODEV;
1669
1670 zswap_debugfs_root = debugfs_create_dir("zswap", NULL);
Seth Jennings2b281112013-07-10 16:05:03 -07001671
Joe Perches0825a6f2018-06-14 15:27:58 -07001672 debugfs_create_u64("pool_limit_hit", 0444,
1673 zswap_debugfs_root, &zswap_pool_limit_hit);
1674 debugfs_create_u64("reject_reclaim_fail", 0444,
1675 zswap_debugfs_root, &zswap_reject_reclaim_fail);
1676 debugfs_create_u64("reject_alloc_fail", 0444,
1677 zswap_debugfs_root, &zswap_reject_alloc_fail);
1678 debugfs_create_u64("reject_kmemcache_fail", 0444,
1679 zswap_debugfs_root, &zswap_reject_kmemcache_fail);
Nhat Phamcb61dad2023-10-24 16:45:09 -07001680 debugfs_create_u64("reject_compress_fail", 0444,
1681 zswap_debugfs_root, &zswap_reject_compress_fail);
Joe Perches0825a6f2018-06-14 15:27:58 -07001682 debugfs_create_u64("reject_compress_poor", 0444,
1683 zswap_debugfs_root, &zswap_reject_compress_poor);
1684 debugfs_create_u64("written_back_pages", 0444,
1685 zswap_debugfs_root, &zswap_written_back_pages);
Johannes Weiner91cdcd82024-03-12 11:34:11 -04001686 debugfs_create_file("pool_total_size", 0444,
1687 zswap_debugfs_root, NULL, &total_size_fops);
Joe Perches0825a6f2018-06-14 15:27:58 -07001688 debugfs_create_atomic_t("stored_pages", 0444,
1689 zswap_debugfs_root, &zswap_stored_pages);
Srividya Desireddya85f8782018-01-31 16:15:59 -08001690 debugfs_create_atomic_t("same_filled_pages", 0444,
Joe Perches0825a6f2018-06-14 15:27:58 -07001691 zswap_debugfs_root, &zswap_same_filled_pages);
Seth Jennings2b281112013-07-10 16:05:03 -07001692
1693 return 0;
1694}
Seth Jennings2b281112013-07-10 16:05:03 -07001695#else
Liu Shixin141fdee2023-04-03 20:13:18 +08001696static int zswap_debugfs_init(void)
Seth Jennings2b281112013-07-10 16:05:03 -07001697{
1698 return 0;
1699}
Seth Jennings2b281112013-07-10 16:05:03 -07001700#endif
1701
1702/*********************************
1703* module init and exit
1704**********************************/
Liu Shixin141fdee2023-04-03 20:13:18 +08001705static int zswap_setup(void)
Seth Jennings2b281112013-07-10 16:05:03 -07001706{
Dan Streetmanf1c54842015-09-09 15:35:19 -07001707 struct zswap_pool *pool;
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +01001708 int ret;
Minchan Kim60105e12014-04-07 15:38:27 -07001709
Liu Shixinb7919122023-04-03 20:13:16 +08001710 zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
1711 if (!zswap_entry_cache) {
Seth Jennings2b281112013-07-10 16:05:03 -07001712 pr_err("entry cache creation failed\n");
Dan Streetmanf1c54842015-09-09 15:35:19 -07001713 goto cache_fail;
Seth Jennings2b281112013-07-10 16:05:03 -07001714 }
Dan Streetmanf1c54842015-09-09 15:35:19 -07001715
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +01001716 ret = cpuhp_setup_state_multi(CPUHP_MM_ZSWP_POOL_PREPARE,
1717 "mm/zswap_pool:prepare",
1718 zswap_cpu_comp_prepare,
1719 zswap_cpu_comp_dead);
1720 if (ret)
1721 goto hp_fail;
1722
Chengming Zhoubf9b7df2024-02-16 08:55:04 +00001723 shrink_wq = alloc_workqueue("zswap-shrink",
1724 WQ_UNBOUND|WQ_MEM_RECLAIM, 1);
1725 if (!shrink_wq)
1726 goto shrink_wq_fail;
1727
Chengming Zhoue35606e2024-03-05 07:53:45 +00001728 zswap_shrinker = zswap_alloc_shrinker();
1729 if (!zswap_shrinker)
Chengming Zhoubf9b7df2024-02-16 08:55:04 +00001730 goto shrinker_fail;
Chengming Zhoue35606e2024-03-05 07:53:45 +00001731 if (list_lru_init_memcg(&zswap_list_lru, zswap_shrinker))
Chengming Zhoubf9b7df2024-02-16 08:55:04 +00001732 goto lru_fail;
Chengming Zhoue35606e2024-03-05 07:53:45 +00001733 shrinker_register(zswap_shrinker);
Chengming Zhoubf9b7df2024-02-16 08:55:04 +00001734
Chengming Zhoue35606e2024-03-05 07:53:45 +00001735 INIT_WORK(&zswap_shrink_work, shrink_worker);
Chengming Zhoubf9b7df2024-02-16 08:55:04 +00001736
Dan Streetmanf1c54842015-09-09 15:35:19 -07001737 pool = __zswap_pool_create_fallback();
Dan Streetmanae3d89a2017-02-27 14:26:47 -08001738 if (pool) {
1739 pr_info("loaded using pool %s/%s\n", pool->tfm_name,
Yosry Ahmedb8cf32d2023-06-20 19:46:44 +00001740 zpool_get_type(pool->zpools[0]));
Dan Streetmanae3d89a2017-02-27 14:26:47 -08001741 list_add(&pool->list, &zswap_pools);
1742 zswap_has_pool = true;
1743 } else {
Dan Streetmanf1c54842015-09-09 15:35:19 -07001744 pr_err("pool creation failed\n");
Dan Streetmanae3d89a2017-02-27 14:26:47 -08001745 zswap_enabled = false;
Seth Jennings2b281112013-07-10 16:05:03 -07001746 }
Minchan Kim60105e12014-04-07 15:38:27 -07001747
Seth Jennings2b281112013-07-10 16:05:03 -07001748 if (zswap_debugfs_init())
1749 pr_warn("debugfs initialization failed\n");
Liu Shixin9021cce2023-04-03 20:13:17 +08001750 zswap_init_state = ZSWAP_INIT_SUCCEED;
Seth Jennings2b281112013-07-10 16:05:03 -07001751 return 0;
Dan Streetmanf1c54842015-09-09 15:35:19 -07001752
Chengming Zhoubf9b7df2024-02-16 08:55:04 +00001753lru_fail:
Chengming Zhoue35606e2024-03-05 07:53:45 +00001754 shrinker_free(zswap_shrinker);
Chengming Zhoubf9b7df2024-02-16 08:55:04 +00001755shrinker_fail:
1756 destroy_workqueue(shrink_wq);
1757shrink_wq_fail:
1758 cpuhp_remove_multi_state(CPUHP_MM_ZSWP_POOL_PREPARE);
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +01001759hp_fail:
Liu Shixinb7919122023-04-03 20:13:16 +08001760 kmem_cache_destroy(zswap_entry_cache);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001761cache_fail:
Dan Streetmand7b028f2017-02-03 13:13:09 -08001762 /* if built-in, we aren't unloaded on failure; don't allow use */
Liu Shixin9021cce2023-04-03 20:13:17 +08001763 zswap_init_state = ZSWAP_INIT_FAILED;
Dan Streetmand7b028f2017-02-03 13:13:09 -08001764 zswap_enabled = false;
Seth Jennings2b281112013-07-10 16:05:03 -07001765 return -ENOMEM;
1766}
Liu Shixin141fdee2023-04-03 20:13:18 +08001767
1768static int __init zswap_init(void)
1769{
1770 if (!zswap_enabled)
1771 return 0;
1772 return zswap_setup();
1773}
Seth Jennings2b281112013-07-10 16:05:03 -07001774/* must be late so crypto has time to come up */
Liu Shixin141fdee2023-04-03 20:13:18 +08001775late_initcall(zswap_init);
Seth Jennings2b281112013-07-10 16:05:03 -07001776
Seth Jennings68386da2014-11-12 21:08:46 -06001777MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
Seth Jennings2b281112013-07-10 16:05:03 -07001778MODULE_DESCRIPTION("Compressed cache for swap pages");