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