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