Commit | Line | Data |
---|---|---|
2b281117 SJ |
1 | /* |
2 | * zswap.c - zswap driver file | |
3 | * | |
4 | * zswap is a backend for frontswap that takes pages that are in the process | |
5 | * of being swapped out and attempts to compress and store them in a | |
6 | * RAM-based memory pool. This can result in a significant I/O reduction on | |
7 | * the swap device and, in the case where decompressing from RAM is faster | |
8 | * than reading from the swap device, can also improve workload performance. | |
9 | * | |
10 | * Copyright (C) 2012 Seth Jennings <sjenning@linux.vnet.ibm.com> | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU General Public License | |
14 | * as published by the Free Software Foundation; either version 2 | |
15 | * of the License, or (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
21 | */ | |
22 | ||
23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
24 | ||
25 | #include <linux/module.h> | |
26 | #include <linux/cpu.h> | |
27 | #include <linux/highmem.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/spinlock.h> | |
30 | #include <linux/types.h> | |
31 | #include <linux/atomic.h> | |
32 | #include <linux/frontswap.h> | |
33 | #include <linux/rbtree.h> | |
34 | #include <linux/swap.h> | |
35 | #include <linux/crypto.h> | |
36 | #include <linux/mempool.h> | |
12d79d64 | 37 | #include <linux/zpool.h> |
2b281117 SJ |
38 | |
39 | #include <linux/mm_types.h> | |
40 | #include <linux/page-flags.h> | |
41 | #include <linux/swapops.h> | |
42 | #include <linux/writeback.h> | |
43 | #include <linux/pagemap.h> | |
44 | ||
45 | /********************************* | |
46 | * statistics | |
47 | **********************************/ | |
12d79d64 DS |
48 | /* Total bytes used by the compressed storage */ |
49 | static u64 zswap_pool_total_size; | |
2b281117 SJ |
50 | /* The number of compressed pages currently stored in zswap */ |
51 | static atomic_t zswap_stored_pages = ATOMIC_INIT(0); | |
52 | ||
53 | /* | |
54 | * The statistics below are not protected from concurrent access for | |
55 | * performance reasons so they may not be a 100% accurate. However, | |
56 | * they do provide useful information on roughly how many times a | |
57 | * certain event is occurring. | |
58 | */ | |
59 | ||
60 | /* Pool limit was hit (see zswap_max_pool_percent) */ | |
61 | static u64 zswap_pool_limit_hit; | |
62 | /* Pages written back when pool limit was reached */ | |
63 | static u64 zswap_written_back_pages; | |
64 | /* Store failed due to a reclaim failure after pool limit was reached */ | |
65 | static u64 zswap_reject_reclaim_fail; | |
66 | /* Compressed page was too big for the allocator to (optimally) store */ | |
67 | static u64 zswap_reject_compress_poor; | |
68 | /* Store failed because underlying allocator could not get memory */ | |
69 | static u64 zswap_reject_alloc_fail; | |
70 | /* Store failed because the entry metadata could not be allocated (rare) */ | |
71 | static u64 zswap_reject_kmemcache_fail; | |
72 | /* Duplicate store was encountered (rare) */ | |
73 | static u64 zswap_duplicate_entry; | |
74 | ||
75 | /********************************* | |
76 | * tunables | |
77 | **********************************/ | |
c00ed16a DS |
78 | |
79 | /* Enable/disable zswap (disabled by default) */ | |
80 | static bool zswap_enabled; | |
d7b028f5 DS |
81 | static int zswap_enabled_param_set(const char *, |
82 | const struct kernel_param *); | |
83 | static struct kernel_param_ops zswap_enabled_param_ops = { | |
84 | .set = zswap_enabled_param_set, | |
85 | .get = param_get_bool, | |
86 | }; | |
87 | module_param_cb(enabled, &zswap_enabled_param_ops, &zswap_enabled, 0644); | |
2b281117 | 88 | |
90b0fc26 | 89 | /* Crypto compressor to use */ |
2b281117 | 90 | #define ZSWAP_COMPRESSOR_DEFAULT "lzo" |
c99b42c3 | 91 | static char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT; |
90b0fc26 DS |
92 | static int zswap_compressor_param_set(const char *, |
93 | const struct kernel_param *); | |
94 | static struct kernel_param_ops zswap_compressor_param_ops = { | |
95 | .set = zswap_compressor_param_set, | |
c99b42c3 DS |
96 | .get = param_get_charp, |
97 | .free = param_free_charp, | |
90b0fc26 DS |
98 | }; |
99 | module_param_cb(compressor, &zswap_compressor_param_ops, | |
c99b42c3 | 100 | &zswap_compressor, 0644); |
2b281117 | 101 | |
90b0fc26 | 102 | /* Compressed storage zpool to use */ |
12d79d64 | 103 | #define ZSWAP_ZPOOL_DEFAULT "zbud" |
c99b42c3 | 104 | static char *zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT; |
90b0fc26 DS |
105 | static int zswap_zpool_param_set(const char *, const struct kernel_param *); |
106 | static struct kernel_param_ops zswap_zpool_param_ops = { | |
c99b42c3 DS |
107 | .set = zswap_zpool_param_set, |
108 | .get = param_get_charp, | |
109 | .free = param_free_charp, | |
90b0fc26 | 110 | }; |
c99b42c3 | 111 | module_param_cb(zpool, &zswap_zpool_param_ops, &zswap_zpool_type, 0644); |
12d79d64 | 112 | |
90b0fc26 DS |
113 | /* The maximum percentage of memory that the compressed pool can occupy */ |
114 | static unsigned int zswap_max_pool_percent = 20; | |
115 | module_param_named(max_pool_percent, zswap_max_pool_percent, uint, 0644); | |
60105e12 | 116 | |
2b281117 | 117 | /********************************* |
f1c54846 | 118 | * data structures |
2b281117 | 119 | **********************************/ |
2b281117 | 120 | |
f1c54846 DS |
121 | struct zswap_pool { |
122 | struct zpool *zpool; | |
123 | struct crypto_comp * __percpu *tfm; | |
124 | struct kref kref; | |
125 | struct list_head list; | |
200867af | 126 | struct work_struct work; |
cab7a7e5 | 127 | struct hlist_node node; |
f1c54846 | 128 | char tfm_name[CRYPTO_MAX_ALG_NAME]; |
2b281117 SJ |
129 | }; |
130 | ||
2b281117 SJ |
131 | /* |
132 | * struct zswap_entry | |
133 | * | |
134 | * This structure contains the metadata for tracking a single compressed | |
135 | * page within zswap. | |
136 | * | |
137 | * rbnode - links the entry into red-black tree for the appropriate swap type | |
f1c54846 | 138 | * offset - the swap offset for the entry. Index into the red-black tree. |
2b281117 SJ |
139 | * refcount - the number of outstanding reference to the entry. This is needed |
140 | * to protect against premature freeing of the entry by code | |
6b452516 | 141 | * concurrent calls to load, invalidate, and writeback. The lock |
2b281117 SJ |
142 | * for the zswap_tree structure that contains the entry must |
143 | * be held while changing the refcount. Since the lock must | |
144 | * be held, there is no reason to also make refcount atomic. | |
2b281117 | 145 | * length - the length in bytes of the compressed page data. Needed during |
6b452516 | 146 | * decompression |
f1c54846 DS |
147 | * pool - the zswap_pool the entry's data is in |
148 | * handle - zpool allocation handle that stores the compressed page data | |
2b281117 SJ |
149 | */ |
150 | struct zswap_entry { | |
151 | struct rb_node rbnode; | |
152 | pgoff_t offset; | |
153 | int refcount; | |
154 | unsigned int length; | |
f1c54846 | 155 | struct zswap_pool *pool; |
2b281117 SJ |
156 | unsigned long handle; |
157 | }; | |
158 | ||
159 | struct zswap_header { | |
160 | swp_entry_t swpentry; | |
161 | }; | |
162 | ||
163 | /* | |
164 | * The tree lock in the zswap_tree struct protects a few things: | |
165 | * - the rbtree | |
166 | * - the refcount field of each entry in the tree | |
167 | */ | |
168 | struct zswap_tree { | |
169 | struct rb_root rbroot; | |
170 | spinlock_t lock; | |
2b281117 SJ |
171 | }; |
172 | ||
173 | static struct zswap_tree *zswap_trees[MAX_SWAPFILES]; | |
174 | ||
f1c54846 DS |
175 | /* RCU-protected iteration */ |
176 | static LIST_HEAD(zswap_pools); | |
177 | /* protects zswap_pools list modification */ | |
178 | static DEFINE_SPINLOCK(zswap_pools_lock); | |
32a4e169 DS |
179 | /* pool counter to provide unique names to zpool */ |
180 | static atomic_t zswap_pools_count = ATOMIC_INIT(0); | |
f1c54846 | 181 | |
90b0fc26 DS |
182 | /* used by param callback function */ |
183 | static bool zswap_init_started; | |
184 | ||
d7b028f5 DS |
185 | /* fatal error during init */ |
186 | static bool zswap_init_failed; | |
187 | ||
ae3d89a7 DS |
188 | /* init completed, but couldn't create the initial pool */ |
189 | static bool zswap_has_pool; | |
190 | ||
f1c54846 DS |
191 | /********************************* |
192 | * helpers and fwd declarations | |
193 | **********************************/ | |
194 | ||
195 | #define zswap_pool_debug(msg, p) \ | |
196 | pr_debug("%s pool %s/%s\n", msg, (p)->tfm_name, \ | |
197 | zpool_get_type((p)->zpool)) | |
198 | ||
199 | static int zswap_writeback_entry(struct zpool *pool, unsigned long handle); | |
200 | static int zswap_pool_get(struct zswap_pool *pool); | |
201 | static void zswap_pool_put(struct zswap_pool *pool); | |
202 | ||
203 | static const struct zpool_ops zswap_zpool_ops = { | |
204 | .evict = zswap_writeback_entry | |
205 | }; | |
206 | ||
207 | static bool zswap_is_full(void) | |
208 | { | |
209 | return totalram_pages * zswap_max_pool_percent / 100 < | |
210 | DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE); | |
211 | } | |
212 | ||
213 | static void zswap_update_total_size(void) | |
214 | { | |
215 | struct zswap_pool *pool; | |
216 | u64 total = 0; | |
217 | ||
218 | rcu_read_lock(); | |
219 | ||
220 | list_for_each_entry_rcu(pool, &zswap_pools, list) | |
221 | total += zpool_get_total_size(pool->zpool); | |
222 | ||
223 | rcu_read_unlock(); | |
224 | ||
225 | zswap_pool_total_size = total; | |
226 | } | |
227 | ||
2b281117 SJ |
228 | /********************************* |
229 | * zswap entry functions | |
230 | **********************************/ | |
231 | static struct kmem_cache *zswap_entry_cache; | |
232 | ||
dd01d7d8 | 233 | static int __init zswap_entry_cache_create(void) |
2b281117 SJ |
234 | { |
235 | zswap_entry_cache = KMEM_CACHE(zswap_entry, 0); | |
5d2d42de | 236 | return zswap_entry_cache == NULL; |
2b281117 SJ |
237 | } |
238 | ||
c119239b | 239 | static void __init zswap_entry_cache_destroy(void) |
2b281117 SJ |
240 | { |
241 | kmem_cache_destroy(zswap_entry_cache); | |
242 | } | |
243 | ||
244 | static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp) | |
245 | { | |
246 | struct zswap_entry *entry; | |
247 | entry = kmem_cache_alloc(zswap_entry_cache, gfp); | |
248 | if (!entry) | |
249 | return NULL; | |
250 | entry->refcount = 1; | |
0ab0abcf | 251 | RB_CLEAR_NODE(&entry->rbnode); |
2b281117 SJ |
252 | return entry; |
253 | } | |
254 | ||
255 | static void zswap_entry_cache_free(struct zswap_entry *entry) | |
256 | { | |
257 | kmem_cache_free(zswap_entry_cache, entry); | |
258 | } | |
259 | ||
2b281117 SJ |
260 | /********************************* |
261 | * rbtree functions | |
262 | **********************************/ | |
263 | static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset) | |
264 | { | |
265 | struct rb_node *node = root->rb_node; | |
266 | struct zswap_entry *entry; | |
267 | ||
268 | while (node) { | |
269 | entry = rb_entry(node, struct zswap_entry, rbnode); | |
270 | if (entry->offset > offset) | |
271 | node = node->rb_left; | |
272 | else if (entry->offset < offset) | |
273 | node = node->rb_right; | |
274 | else | |
275 | return entry; | |
276 | } | |
277 | return NULL; | |
278 | } | |
279 | ||
280 | /* | |
281 | * In the case that a entry with the same offset is found, a pointer to | |
282 | * the existing entry is stored in dupentry and the function returns -EEXIST | |
283 | */ | |
284 | static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry, | |
285 | struct zswap_entry **dupentry) | |
286 | { | |
287 | struct rb_node **link = &root->rb_node, *parent = NULL; | |
288 | struct zswap_entry *myentry; | |
289 | ||
290 | while (*link) { | |
291 | parent = *link; | |
292 | myentry = rb_entry(parent, struct zswap_entry, rbnode); | |
293 | if (myentry->offset > entry->offset) | |
294 | link = &(*link)->rb_left; | |
295 | else if (myentry->offset < entry->offset) | |
296 | link = &(*link)->rb_right; | |
297 | else { | |
298 | *dupentry = myentry; | |
299 | return -EEXIST; | |
300 | } | |
301 | } | |
302 | rb_link_node(&entry->rbnode, parent, link); | |
303 | rb_insert_color(&entry->rbnode, root); | |
304 | return 0; | |
305 | } | |
306 | ||
0ab0abcf WY |
307 | static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry) |
308 | { | |
309 | if (!RB_EMPTY_NODE(&entry->rbnode)) { | |
310 | rb_erase(&entry->rbnode, root); | |
311 | RB_CLEAR_NODE(&entry->rbnode); | |
312 | } | |
313 | } | |
314 | ||
315 | /* | |
12d79d64 | 316 | * Carries out the common pattern of freeing and entry's zpool allocation, |
0ab0abcf WY |
317 | * freeing the entry itself, and decrementing the number of stored pages. |
318 | */ | |
60105e12 | 319 | static void zswap_free_entry(struct zswap_entry *entry) |
0ab0abcf | 320 | { |
f1c54846 DS |
321 | zpool_free(entry->pool->zpool, entry->handle); |
322 | zswap_pool_put(entry->pool); | |
0ab0abcf WY |
323 | zswap_entry_cache_free(entry); |
324 | atomic_dec(&zswap_stored_pages); | |
f1c54846 | 325 | zswap_update_total_size(); |
0ab0abcf WY |
326 | } |
327 | ||
328 | /* caller must hold the tree lock */ | |
329 | static void zswap_entry_get(struct zswap_entry *entry) | |
330 | { | |
331 | entry->refcount++; | |
332 | } | |
333 | ||
334 | /* caller must hold the tree lock | |
335 | * remove from the tree and free it, if nobody reference the entry | |
336 | */ | |
337 | static void zswap_entry_put(struct zswap_tree *tree, | |
338 | struct zswap_entry *entry) | |
339 | { | |
340 | int refcount = --entry->refcount; | |
341 | ||
342 | BUG_ON(refcount < 0); | |
343 | if (refcount == 0) { | |
344 | zswap_rb_erase(&tree->rbroot, entry); | |
60105e12 | 345 | zswap_free_entry(entry); |
0ab0abcf WY |
346 | } |
347 | } | |
348 | ||
349 | /* caller must hold the tree lock */ | |
350 | static struct zswap_entry *zswap_entry_find_get(struct rb_root *root, | |
351 | pgoff_t offset) | |
352 | { | |
b0c9865f | 353 | struct zswap_entry *entry; |
0ab0abcf WY |
354 | |
355 | entry = zswap_rb_search(root, offset); | |
356 | if (entry) | |
357 | zswap_entry_get(entry); | |
358 | ||
359 | return entry; | |
360 | } | |
361 | ||
2b281117 SJ |
362 | /********************************* |
363 | * per-cpu code | |
364 | **********************************/ | |
365 | static DEFINE_PER_CPU(u8 *, zswap_dstmem); | |
366 | ||
ad7ed770 | 367 | static int zswap_dstmem_prepare(unsigned int cpu) |
2b281117 | 368 | { |
2b281117 SJ |
369 | u8 *dst; |
370 | ||
ad7ed770 SAS |
371 | dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu)); |
372 | if (!dst) { | |
373 | pr_err("can't allocate compressor buffer\n"); | |
374 | return -ENOMEM; | |
2b281117 | 375 | } |
ad7ed770 SAS |
376 | per_cpu(zswap_dstmem, cpu) = dst; |
377 | return 0; | |
2b281117 SJ |
378 | } |
379 | ||
ad7ed770 | 380 | static int zswap_dstmem_dead(unsigned int cpu) |
2b281117 | 381 | { |
ad7ed770 | 382 | u8 *dst; |
2b281117 | 383 | |
ad7ed770 SAS |
384 | dst = per_cpu(zswap_dstmem, cpu); |
385 | kfree(dst); | |
386 | per_cpu(zswap_dstmem, cpu) = NULL; | |
f1c54846 | 387 | |
f1c54846 | 388 | return 0; |
f1c54846 DS |
389 | } |
390 | ||
cab7a7e5 | 391 | static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) |
f1c54846 | 392 | { |
cab7a7e5 | 393 | struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node); |
f1c54846 DS |
394 | struct crypto_comp *tfm; |
395 | ||
cab7a7e5 SAS |
396 | if (WARN_ON(*per_cpu_ptr(pool->tfm, cpu))) |
397 | return 0; | |
f1c54846 | 398 | |
cab7a7e5 SAS |
399 | tfm = crypto_alloc_comp(pool->tfm_name, 0, 0); |
400 | if (IS_ERR_OR_NULL(tfm)) { | |
401 | pr_err("could not alloc crypto comp %s : %ld\n", | |
402 | pool->tfm_name, PTR_ERR(tfm)); | |
403 | return -ENOMEM; | |
404 | } | |
405 | *per_cpu_ptr(pool->tfm, cpu) = tfm; | |
2b281117 | 406 | return 0; |
2b281117 SJ |
407 | } |
408 | ||
cab7a7e5 | 409 | static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) |
f1c54846 | 410 | { |
cab7a7e5 SAS |
411 | struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node); |
412 | struct crypto_comp *tfm; | |
f1c54846 | 413 | |
cab7a7e5 SAS |
414 | tfm = *per_cpu_ptr(pool->tfm, cpu); |
415 | if (!IS_ERR_OR_NULL(tfm)) | |
416 | crypto_free_comp(tfm); | |
417 | *per_cpu_ptr(pool->tfm, cpu) = NULL; | |
418 | return 0; | |
f1c54846 DS |
419 | } |
420 | ||
2b281117 | 421 | /********************************* |
f1c54846 | 422 | * pool functions |
2b281117 | 423 | **********************************/ |
f1c54846 DS |
424 | |
425 | static struct zswap_pool *__zswap_pool_current(void) | |
2b281117 | 426 | { |
f1c54846 DS |
427 | struct zswap_pool *pool; |
428 | ||
429 | pool = list_first_or_null_rcu(&zswap_pools, typeof(*pool), list); | |
ae3d89a7 DS |
430 | WARN_ONCE(!pool && zswap_has_pool, |
431 | "%s: no page storage pool!\n", __func__); | |
f1c54846 DS |
432 | |
433 | return pool; | |
434 | } | |
435 | ||
436 | static struct zswap_pool *zswap_pool_current(void) | |
437 | { | |
438 | assert_spin_locked(&zswap_pools_lock); | |
439 | ||
440 | return __zswap_pool_current(); | |
441 | } | |
442 | ||
443 | static struct zswap_pool *zswap_pool_current_get(void) | |
444 | { | |
445 | struct zswap_pool *pool; | |
446 | ||
447 | rcu_read_lock(); | |
448 | ||
449 | pool = __zswap_pool_current(); | |
ae3d89a7 | 450 | if (!zswap_pool_get(pool)) |
f1c54846 DS |
451 | pool = NULL; |
452 | ||
453 | rcu_read_unlock(); | |
454 | ||
455 | return pool; | |
456 | } | |
457 | ||
458 | static struct zswap_pool *zswap_pool_last_get(void) | |
459 | { | |
460 | struct zswap_pool *pool, *last = NULL; | |
461 | ||
462 | rcu_read_lock(); | |
463 | ||
464 | list_for_each_entry_rcu(pool, &zswap_pools, list) | |
465 | last = pool; | |
ae3d89a7 DS |
466 | WARN_ONCE(!last && zswap_has_pool, |
467 | "%s: no page storage pool!\n", __func__); | |
468 | if (!zswap_pool_get(last)) | |
f1c54846 DS |
469 | last = NULL; |
470 | ||
471 | rcu_read_unlock(); | |
472 | ||
473 | return last; | |
474 | } | |
475 | ||
8bc8b228 | 476 | /* type and compressor must be null-terminated */ |
f1c54846 DS |
477 | static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor) |
478 | { | |
479 | struct zswap_pool *pool; | |
480 | ||
481 | assert_spin_locked(&zswap_pools_lock); | |
482 | ||
483 | list_for_each_entry_rcu(pool, &zswap_pools, list) { | |
8bc8b228 | 484 | if (strcmp(pool->tfm_name, compressor)) |
f1c54846 | 485 | continue; |
8bc8b228 | 486 | if (strcmp(zpool_get_type(pool->zpool), type)) |
f1c54846 DS |
487 | continue; |
488 | /* if we can't get it, it's about to be destroyed */ | |
489 | if (!zswap_pool_get(pool)) | |
490 | continue; | |
491 | return pool; | |
492 | } | |
493 | ||
494 | return NULL; | |
495 | } | |
496 | ||
497 | static struct zswap_pool *zswap_pool_create(char *type, char *compressor) | |
498 | { | |
499 | struct zswap_pool *pool; | |
32a4e169 | 500 | char name[38]; /* 'zswap' + 32 char (max) num + \0 */ |
d0164adc | 501 | gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM; |
cab7a7e5 | 502 | int ret; |
f1c54846 DS |
503 | |
504 | pool = kzalloc(sizeof(*pool), GFP_KERNEL); | |
505 | if (!pool) { | |
506 | pr_err("pool alloc failed\n"); | |
507 | return NULL; | |
508 | } | |
509 | ||
32a4e169 DS |
510 | /* unique name for each pool specifically required by zsmalloc */ |
511 | snprintf(name, 38, "zswap%x", atomic_inc_return(&zswap_pools_count)); | |
512 | ||
513 | pool->zpool = zpool_create_pool(type, name, gfp, &zswap_zpool_ops); | |
f1c54846 DS |
514 | if (!pool->zpool) { |
515 | pr_err("%s zpool not available\n", type); | |
516 | goto error; | |
517 | } | |
518 | pr_debug("using %s zpool\n", zpool_get_type(pool->zpool)); | |
519 | ||
520 | strlcpy(pool->tfm_name, compressor, sizeof(pool->tfm_name)); | |
521 | pool->tfm = alloc_percpu(struct crypto_comp *); | |
522 | if (!pool->tfm) { | |
523 | pr_err("percpu alloc failed\n"); | |
524 | goto error; | |
525 | } | |
526 | ||
cab7a7e5 SAS |
527 | ret = cpuhp_state_add_instance(CPUHP_MM_ZSWP_POOL_PREPARE, |
528 | &pool->node); | |
529 | if (ret) | |
f1c54846 DS |
530 | goto error; |
531 | pr_debug("using %s compressor\n", pool->tfm_name); | |
532 | ||
533 | /* being the current pool takes 1 ref; this func expects the | |
534 | * caller to always add the new pool as the current pool | |
535 | */ | |
536 | kref_init(&pool->kref); | |
537 | INIT_LIST_HEAD(&pool->list); | |
538 | ||
539 | zswap_pool_debug("created", pool); | |
540 | ||
541 | return pool; | |
542 | ||
543 | error: | |
544 | free_percpu(pool->tfm); | |
545 | if (pool->zpool) | |
546 | zpool_destroy_pool(pool->zpool); | |
547 | kfree(pool); | |
548 | return NULL; | |
549 | } | |
550 | ||
c99b42c3 | 551 | static __init struct zswap_pool *__zswap_pool_create_fallback(void) |
f1c54846 DS |
552 | { |
553 | if (!crypto_has_comp(zswap_compressor, 0, 0)) { | |
c99b42c3 DS |
554 | if (!strcmp(zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT)) { |
555 | pr_err("default compressor %s not available\n", | |
556 | zswap_compressor); | |
557 | return NULL; | |
558 | } | |
f1c54846 DS |
559 | pr_err("compressor %s not available, using default %s\n", |
560 | zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT); | |
c99b42c3 DS |
561 | param_free_charp(&zswap_compressor); |
562 | zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT; | |
f1c54846 DS |
563 | } |
564 | if (!zpool_has_pool(zswap_zpool_type)) { | |
c99b42c3 DS |
565 | if (!strcmp(zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT)) { |
566 | pr_err("default zpool %s not available\n", | |
567 | zswap_zpool_type); | |
568 | return NULL; | |
569 | } | |
f1c54846 DS |
570 | pr_err("zpool %s not available, using default %s\n", |
571 | zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT); | |
c99b42c3 DS |
572 | param_free_charp(&zswap_zpool_type); |
573 | zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT; | |
f1c54846 DS |
574 | } |
575 | ||
576 | return zswap_pool_create(zswap_zpool_type, zswap_compressor); | |
577 | } | |
578 | ||
579 | static void zswap_pool_destroy(struct zswap_pool *pool) | |
580 | { | |
581 | zswap_pool_debug("destroying", pool); | |
582 | ||
cab7a7e5 | 583 | cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node); |
f1c54846 DS |
584 | free_percpu(pool->tfm); |
585 | zpool_destroy_pool(pool->zpool); | |
586 | kfree(pool); | |
587 | } | |
588 | ||
589 | static int __must_check zswap_pool_get(struct zswap_pool *pool) | |
590 | { | |
ae3d89a7 DS |
591 | if (!pool) |
592 | return 0; | |
593 | ||
f1c54846 DS |
594 | return kref_get_unless_zero(&pool->kref); |
595 | } | |
596 | ||
200867af | 597 | static void __zswap_pool_release(struct work_struct *work) |
f1c54846 | 598 | { |
200867af DS |
599 | struct zswap_pool *pool = container_of(work, typeof(*pool), work); |
600 | ||
601 | synchronize_rcu(); | |
f1c54846 DS |
602 | |
603 | /* nobody should have been able to get a kref... */ | |
604 | WARN_ON(kref_get_unless_zero(&pool->kref)); | |
605 | ||
606 | /* pool is now off zswap_pools list and has no references. */ | |
607 | zswap_pool_destroy(pool); | |
608 | } | |
609 | ||
610 | static void __zswap_pool_empty(struct kref *kref) | |
611 | { | |
612 | struct zswap_pool *pool; | |
613 | ||
614 | pool = container_of(kref, typeof(*pool), kref); | |
615 | ||
616 | spin_lock(&zswap_pools_lock); | |
617 | ||
618 | WARN_ON(pool == zswap_pool_current()); | |
619 | ||
620 | list_del_rcu(&pool->list); | |
200867af DS |
621 | |
622 | INIT_WORK(&pool->work, __zswap_pool_release); | |
623 | schedule_work(&pool->work); | |
f1c54846 DS |
624 | |
625 | spin_unlock(&zswap_pools_lock); | |
626 | } | |
627 | ||
628 | static void zswap_pool_put(struct zswap_pool *pool) | |
629 | { | |
630 | kref_put(&pool->kref, __zswap_pool_empty); | |
2b281117 SJ |
631 | } |
632 | ||
90b0fc26 DS |
633 | /********************************* |
634 | * param callbacks | |
635 | **********************************/ | |
636 | ||
c99b42c3 | 637 | /* val must be a null-terminated string */ |
90b0fc26 DS |
638 | static int __zswap_param_set(const char *val, const struct kernel_param *kp, |
639 | char *type, char *compressor) | |
640 | { | |
641 | struct zswap_pool *pool, *put_pool = NULL; | |
c99b42c3 | 642 | char *s = strstrip((char *)val); |
90b0fc26 DS |
643 | int ret; |
644 | ||
d7b028f5 DS |
645 | if (zswap_init_failed) { |
646 | pr_err("can't set param, initialization failed\n"); | |
647 | return -ENODEV; | |
648 | } | |
649 | ||
c99b42c3 | 650 | /* no change required */ |
ae3d89a7 | 651 | if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool) |
c99b42c3 | 652 | return 0; |
90b0fc26 DS |
653 | |
654 | /* if this is load-time (pre-init) param setting, | |
655 | * don't create a pool; that's done during init. | |
656 | */ | |
657 | if (!zswap_init_started) | |
c99b42c3 | 658 | return param_set_charp(s, kp); |
90b0fc26 DS |
659 | |
660 | if (!type) { | |
c99b42c3 DS |
661 | if (!zpool_has_pool(s)) { |
662 | pr_err("zpool %s not available\n", s); | |
90b0fc26 DS |
663 | return -ENOENT; |
664 | } | |
c99b42c3 | 665 | type = s; |
90b0fc26 | 666 | } else if (!compressor) { |
c99b42c3 DS |
667 | if (!crypto_has_comp(s, 0, 0)) { |
668 | pr_err("compressor %s not available\n", s); | |
90b0fc26 DS |
669 | return -ENOENT; |
670 | } | |
c99b42c3 DS |
671 | compressor = s; |
672 | } else { | |
673 | WARN_ON(1); | |
674 | return -EINVAL; | |
90b0fc26 DS |
675 | } |
676 | ||
677 | spin_lock(&zswap_pools_lock); | |
678 | ||
679 | pool = zswap_pool_find_get(type, compressor); | |
680 | if (pool) { | |
681 | zswap_pool_debug("using existing", pool); | |
682 | list_del_rcu(&pool->list); | |
683 | } else { | |
684 | spin_unlock(&zswap_pools_lock); | |
685 | pool = zswap_pool_create(type, compressor); | |
686 | spin_lock(&zswap_pools_lock); | |
687 | } | |
688 | ||
689 | if (pool) | |
c99b42c3 | 690 | ret = param_set_charp(s, kp); |
90b0fc26 DS |
691 | else |
692 | ret = -EINVAL; | |
693 | ||
694 | if (!ret) { | |
695 | put_pool = zswap_pool_current(); | |
696 | list_add_rcu(&pool->list, &zswap_pools); | |
ae3d89a7 | 697 | zswap_has_pool = true; |
90b0fc26 DS |
698 | } else if (pool) { |
699 | /* add the possibly pre-existing pool to the end of the pools | |
700 | * list; if it's new (and empty) then it'll be removed and | |
701 | * destroyed by the put after we drop the lock | |
702 | */ | |
703 | list_add_tail_rcu(&pool->list, &zswap_pools); | |
704 | put_pool = pool; | |
ae3d89a7 DS |
705 | } else if (!zswap_has_pool) { |
706 | /* if initial pool creation failed, and this pool creation also | |
707 | * failed, maybe both compressor and zpool params were bad. | |
708 | * Allow changing this param, so pool creation will succeed | |
709 | * when the other param is changed. We already verified this | |
710 | * param is ok in the zpool_has_pool() or crypto_has_comp() | |
711 | * checks above. | |
712 | */ | |
713 | ret = param_set_charp(s, kp); | |
90b0fc26 DS |
714 | } |
715 | ||
716 | spin_unlock(&zswap_pools_lock); | |
717 | ||
718 | /* drop the ref from either the old current pool, | |
719 | * or the new pool we failed to add | |
720 | */ | |
721 | if (put_pool) | |
722 | zswap_pool_put(put_pool); | |
723 | ||
724 | return ret; | |
725 | } | |
726 | ||
727 | static int zswap_compressor_param_set(const char *val, | |
728 | const struct kernel_param *kp) | |
729 | { | |
730 | return __zswap_param_set(val, kp, zswap_zpool_type, NULL); | |
731 | } | |
732 | ||
733 | static int zswap_zpool_param_set(const char *val, | |
734 | const struct kernel_param *kp) | |
735 | { | |
736 | return __zswap_param_set(val, kp, NULL, zswap_compressor); | |
737 | } | |
738 | ||
d7b028f5 DS |
739 | static int zswap_enabled_param_set(const char *val, |
740 | const struct kernel_param *kp) | |
741 | { | |
742 | if (zswap_init_failed) { | |
743 | pr_err("can't enable, initialization failed\n"); | |
744 | return -ENODEV; | |
745 | } | |
ae3d89a7 DS |
746 | if (!zswap_has_pool && zswap_init_started) { |
747 | pr_err("can't enable, no pool configured\n"); | |
748 | return -ENODEV; | |
749 | } | |
d7b028f5 DS |
750 | |
751 | return param_set_bool(val, kp); | |
752 | } | |
753 | ||
2b281117 SJ |
754 | /********************************* |
755 | * writeback code | |
756 | **********************************/ | |
757 | /* return enum for zswap_get_swap_cache_page */ | |
758 | enum zswap_get_swap_ret { | |
759 | ZSWAP_SWAPCACHE_NEW, | |
760 | ZSWAP_SWAPCACHE_EXIST, | |
67d13fe8 | 761 | ZSWAP_SWAPCACHE_FAIL, |
2b281117 SJ |
762 | }; |
763 | ||
764 | /* | |
765 | * zswap_get_swap_cache_page | |
766 | * | |
767 | * This is an adaption of read_swap_cache_async() | |
768 | * | |
769 | * This function tries to find a page with the given swap entry | |
770 | * in the swapper_space address space (the swap cache). If the page | |
771 | * is found, it is returned in retpage. Otherwise, a page is allocated, | |
772 | * added to the swap cache, and returned in retpage. | |
773 | * | |
774 | * If success, the swap cache page is returned in retpage | |
67d13fe8 WY |
775 | * Returns ZSWAP_SWAPCACHE_EXIST if page was already in the swap cache |
776 | * Returns ZSWAP_SWAPCACHE_NEW if the new page needs to be populated, | |
777 | * the new page is added to swapcache and locked | |
778 | * Returns ZSWAP_SWAPCACHE_FAIL on error | |
2b281117 SJ |
779 | */ |
780 | static int zswap_get_swap_cache_page(swp_entry_t entry, | |
781 | struct page **retpage) | |
782 | { | |
5b999aad | 783 | bool page_was_allocated; |
2b281117 | 784 | |
5b999aad DS |
785 | *retpage = __read_swap_cache_async(entry, GFP_KERNEL, |
786 | NULL, 0, &page_was_allocated); | |
787 | if (page_was_allocated) | |
788 | return ZSWAP_SWAPCACHE_NEW; | |
789 | if (!*retpage) | |
67d13fe8 | 790 | return ZSWAP_SWAPCACHE_FAIL; |
2b281117 SJ |
791 | return ZSWAP_SWAPCACHE_EXIST; |
792 | } | |
793 | ||
794 | /* | |
795 | * Attempts to free an entry by adding a page to the swap cache, | |
796 | * decompressing the entry data into the page, and issuing a | |
797 | * bio write to write the page back to the swap device. | |
798 | * | |
799 | * This can be thought of as a "resumed writeback" of the page | |
800 | * to the swap device. We are basically resuming the same swap | |
801 | * writeback path that was intercepted with the frontswap_store() | |
802 | * in the first place. After the page has been decompressed into | |
803 | * the swap cache, the compressed version stored by zswap can be | |
804 | * freed. | |
805 | */ | |
12d79d64 | 806 | static int zswap_writeback_entry(struct zpool *pool, unsigned long handle) |
2b281117 SJ |
807 | { |
808 | struct zswap_header *zhdr; | |
809 | swp_entry_t swpentry; | |
810 | struct zswap_tree *tree; | |
811 | pgoff_t offset; | |
812 | struct zswap_entry *entry; | |
813 | struct page *page; | |
f1c54846 | 814 | struct crypto_comp *tfm; |
2b281117 SJ |
815 | u8 *src, *dst; |
816 | unsigned int dlen; | |
0ab0abcf | 817 | int ret; |
2b281117 SJ |
818 | struct writeback_control wbc = { |
819 | .sync_mode = WB_SYNC_NONE, | |
820 | }; | |
821 | ||
822 | /* extract swpentry from data */ | |
12d79d64 | 823 | zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO); |
2b281117 | 824 | swpentry = zhdr->swpentry; /* here */ |
12d79d64 | 825 | zpool_unmap_handle(pool, handle); |
2b281117 SJ |
826 | tree = zswap_trees[swp_type(swpentry)]; |
827 | offset = swp_offset(swpentry); | |
2b281117 SJ |
828 | |
829 | /* find and ref zswap entry */ | |
830 | spin_lock(&tree->lock); | |
0ab0abcf | 831 | entry = zswap_entry_find_get(&tree->rbroot, offset); |
2b281117 SJ |
832 | if (!entry) { |
833 | /* entry was invalidated */ | |
834 | spin_unlock(&tree->lock); | |
835 | return 0; | |
836 | } | |
2b281117 SJ |
837 | spin_unlock(&tree->lock); |
838 | BUG_ON(offset != entry->offset); | |
839 | ||
840 | /* try to allocate swap cache page */ | |
841 | switch (zswap_get_swap_cache_page(swpentry, &page)) { | |
67d13fe8 | 842 | case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */ |
2b281117 SJ |
843 | ret = -ENOMEM; |
844 | goto fail; | |
845 | ||
67d13fe8 | 846 | case ZSWAP_SWAPCACHE_EXIST: |
2b281117 | 847 | /* page is already in the swap cache, ignore for now */ |
09cbfeaf | 848 | put_page(page); |
2b281117 SJ |
849 | ret = -EEXIST; |
850 | goto fail; | |
851 | ||
852 | case ZSWAP_SWAPCACHE_NEW: /* page is locked */ | |
853 | /* decompress */ | |
854 | dlen = PAGE_SIZE; | |
f1c54846 | 855 | src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle, |
12d79d64 | 856 | ZPOOL_MM_RO) + sizeof(struct zswap_header); |
2b281117 | 857 | dst = kmap_atomic(page); |
f1c54846 DS |
858 | tfm = *get_cpu_ptr(entry->pool->tfm); |
859 | ret = crypto_comp_decompress(tfm, src, entry->length, | |
860 | dst, &dlen); | |
861 | put_cpu_ptr(entry->pool->tfm); | |
2b281117 | 862 | kunmap_atomic(dst); |
f1c54846 | 863 | zpool_unmap_handle(entry->pool->zpool, entry->handle); |
2b281117 SJ |
864 | BUG_ON(ret); |
865 | BUG_ON(dlen != PAGE_SIZE); | |
866 | ||
867 | /* page is up to date */ | |
868 | SetPageUptodate(page); | |
869 | } | |
870 | ||
b349acc7 WY |
871 | /* move it to the tail of the inactive list after end_writeback */ |
872 | SetPageReclaim(page); | |
873 | ||
2b281117 SJ |
874 | /* start writeback */ |
875 | __swap_writepage(page, &wbc, end_swap_bio_write); | |
09cbfeaf | 876 | put_page(page); |
2b281117 SJ |
877 | zswap_written_back_pages++; |
878 | ||
879 | spin_lock(&tree->lock); | |
2b281117 | 880 | /* drop local reference */ |
0ab0abcf | 881 | zswap_entry_put(tree, entry); |
2b281117 SJ |
882 | |
883 | /* | |
0ab0abcf WY |
884 | * There are two possible situations for entry here: |
885 | * (1) refcount is 1(normal case), entry is valid and on the tree | |
886 | * (2) refcount is 0, entry is freed and not on the tree | |
887 | * because invalidate happened during writeback | |
888 | * search the tree and free the entry if find entry | |
889 | */ | |
890 | if (entry == zswap_rb_search(&tree->rbroot, offset)) | |
891 | zswap_entry_put(tree, entry); | |
2b281117 | 892 | spin_unlock(&tree->lock); |
2b281117 | 893 | |
0ab0abcf WY |
894 | goto end; |
895 | ||
896 | /* | |
897 | * if we get here due to ZSWAP_SWAPCACHE_EXIST | |
898 | * a load may happening concurrently | |
899 | * it is safe and okay to not free the entry | |
900 | * if we free the entry in the following put | |
901 | * it it either okay to return !0 | |
902 | */ | |
2b281117 SJ |
903 | fail: |
904 | spin_lock(&tree->lock); | |
0ab0abcf | 905 | zswap_entry_put(tree, entry); |
2b281117 | 906 | spin_unlock(&tree->lock); |
0ab0abcf WY |
907 | |
908 | end: | |
2b281117 SJ |
909 | return ret; |
910 | } | |
911 | ||
f1c54846 DS |
912 | static int zswap_shrink(void) |
913 | { | |
914 | struct zswap_pool *pool; | |
915 | int ret; | |
916 | ||
917 | pool = zswap_pool_last_get(); | |
918 | if (!pool) | |
919 | return -ENOENT; | |
920 | ||
921 | ret = zpool_shrink(pool->zpool, 1, NULL); | |
922 | ||
923 | zswap_pool_put(pool); | |
924 | ||
925 | return ret; | |
926 | } | |
927 | ||
2b281117 SJ |
928 | /********************************* |
929 | * frontswap hooks | |
930 | **********************************/ | |
931 | /* attempts to compress and store an single page */ | |
932 | static int zswap_frontswap_store(unsigned type, pgoff_t offset, | |
933 | struct page *page) | |
934 | { | |
935 | struct zswap_tree *tree = zswap_trees[type]; | |
936 | struct zswap_entry *entry, *dupentry; | |
f1c54846 | 937 | struct crypto_comp *tfm; |
2b281117 SJ |
938 | int ret; |
939 | unsigned int dlen = PAGE_SIZE, len; | |
940 | unsigned long handle; | |
941 | char *buf; | |
942 | u8 *src, *dst; | |
943 | struct zswap_header *zhdr; | |
944 | ||
c00ed16a | 945 | if (!zswap_enabled || !tree) { |
2b281117 SJ |
946 | ret = -ENODEV; |
947 | goto reject; | |
948 | } | |
949 | ||
950 | /* reclaim space if needed */ | |
951 | if (zswap_is_full()) { | |
952 | zswap_pool_limit_hit++; | |
f1c54846 | 953 | if (zswap_shrink()) { |
2b281117 SJ |
954 | zswap_reject_reclaim_fail++; |
955 | ret = -ENOMEM; | |
956 | goto reject; | |
957 | } | |
958 | } | |
959 | ||
960 | /* allocate entry */ | |
961 | entry = zswap_entry_cache_alloc(GFP_KERNEL); | |
962 | if (!entry) { | |
963 | zswap_reject_kmemcache_fail++; | |
964 | ret = -ENOMEM; | |
965 | goto reject; | |
966 | } | |
967 | ||
f1c54846 DS |
968 | /* if entry is successfully added, it keeps the reference */ |
969 | entry->pool = zswap_pool_current_get(); | |
970 | if (!entry->pool) { | |
971 | ret = -EINVAL; | |
972 | goto freepage; | |
973 | } | |
974 | ||
2b281117 SJ |
975 | /* compress */ |
976 | dst = get_cpu_var(zswap_dstmem); | |
f1c54846 | 977 | tfm = *get_cpu_ptr(entry->pool->tfm); |
2b281117 | 978 | src = kmap_atomic(page); |
f1c54846 | 979 | ret = crypto_comp_compress(tfm, src, PAGE_SIZE, dst, &dlen); |
2b281117 | 980 | kunmap_atomic(src); |
f1c54846 | 981 | put_cpu_ptr(entry->pool->tfm); |
2b281117 SJ |
982 | if (ret) { |
983 | ret = -EINVAL; | |
f1c54846 | 984 | goto put_dstmem; |
2b281117 SJ |
985 | } |
986 | ||
987 | /* store */ | |
988 | len = dlen + sizeof(struct zswap_header); | |
f1c54846 | 989 | ret = zpool_malloc(entry->pool->zpool, len, |
d0164adc MG |
990 | __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM, |
991 | &handle); | |
2b281117 SJ |
992 | if (ret == -ENOSPC) { |
993 | zswap_reject_compress_poor++; | |
f1c54846 | 994 | goto put_dstmem; |
2b281117 SJ |
995 | } |
996 | if (ret) { | |
997 | zswap_reject_alloc_fail++; | |
f1c54846 | 998 | goto put_dstmem; |
2b281117 | 999 | } |
f1c54846 | 1000 | zhdr = zpool_map_handle(entry->pool->zpool, handle, ZPOOL_MM_RW); |
2b281117 SJ |
1001 | zhdr->swpentry = swp_entry(type, offset); |
1002 | buf = (u8 *)(zhdr + 1); | |
1003 | memcpy(buf, dst, dlen); | |
f1c54846 | 1004 | zpool_unmap_handle(entry->pool->zpool, handle); |
2b281117 SJ |
1005 | put_cpu_var(zswap_dstmem); |
1006 | ||
1007 | /* populate entry */ | |
1008 | entry->offset = offset; | |
1009 | entry->handle = handle; | |
1010 | entry->length = dlen; | |
1011 | ||
1012 | /* map */ | |
1013 | spin_lock(&tree->lock); | |
1014 | do { | |
1015 | ret = zswap_rb_insert(&tree->rbroot, entry, &dupentry); | |
1016 | if (ret == -EEXIST) { | |
1017 | zswap_duplicate_entry++; | |
1018 | /* remove from rbtree */ | |
0ab0abcf WY |
1019 | zswap_rb_erase(&tree->rbroot, dupentry); |
1020 | zswap_entry_put(tree, dupentry); | |
2b281117 SJ |
1021 | } |
1022 | } while (ret == -EEXIST); | |
1023 | spin_unlock(&tree->lock); | |
1024 | ||
1025 | /* update stats */ | |
1026 | atomic_inc(&zswap_stored_pages); | |
f1c54846 | 1027 | zswap_update_total_size(); |
2b281117 SJ |
1028 | |
1029 | return 0; | |
1030 | ||
f1c54846 | 1031 | put_dstmem: |
2b281117 | 1032 | put_cpu_var(zswap_dstmem); |
f1c54846 DS |
1033 | zswap_pool_put(entry->pool); |
1034 | freepage: | |
2b281117 SJ |
1035 | zswap_entry_cache_free(entry); |
1036 | reject: | |
1037 | return ret; | |
1038 | } | |
1039 | ||
1040 | /* | |
1041 | * returns 0 if the page was successfully decompressed | |
1042 | * return -1 on entry not found or error | |
1043 | */ | |
1044 | static int zswap_frontswap_load(unsigned type, pgoff_t offset, | |
1045 | struct page *page) | |
1046 | { | |
1047 | struct zswap_tree *tree = zswap_trees[type]; | |
1048 | struct zswap_entry *entry; | |
f1c54846 | 1049 | struct crypto_comp *tfm; |
2b281117 SJ |
1050 | u8 *src, *dst; |
1051 | unsigned int dlen; | |
0ab0abcf | 1052 | int ret; |
2b281117 SJ |
1053 | |
1054 | /* find */ | |
1055 | spin_lock(&tree->lock); | |
0ab0abcf | 1056 | entry = zswap_entry_find_get(&tree->rbroot, offset); |
2b281117 SJ |
1057 | if (!entry) { |
1058 | /* entry was written back */ | |
1059 | spin_unlock(&tree->lock); | |
1060 | return -1; | |
1061 | } | |
2b281117 SJ |
1062 | spin_unlock(&tree->lock); |
1063 | ||
1064 | /* decompress */ | |
1065 | dlen = PAGE_SIZE; | |
f1c54846 | 1066 | src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle, |
12d79d64 | 1067 | ZPOOL_MM_RO) + sizeof(struct zswap_header); |
2b281117 | 1068 | dst = kmap_atomic(page); |
f1c54846 DS |
1069 | tfm = *get_cpu_ptr(entry->pool->tfm); |
1070 | ret = crypto_comp_decompress(tfm, src, entry->length, dst, &dlen); | |
1071 | put_cpu_ptr(entry->pool->tfm); | |
2b281117 | 1072 | kunmap_atomic(dst); |
f1c54846 | 1073 | zpool_unmap_handle(entry->pool->zpool, entry->handle); |
2b281117 SJ |
1074 | BUG_ON(ret); |
1075 | ||
1076 | spin_lock(&tree->lock); | |
0ab0abcf | 1077 | zswap_entry_put(tree, entry); |
2b281117 SJ |
1078 | spin_unlock(&tree->lock); |
1079 | ||
2b281117 SJ |
1080 | return 0; |
1081 | } | |
1082 | ||
1083 | /* frees an entry in zswap */ | |
1084 | static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset) | |
1085 | { | |
1086 | struct zswap_tree *tree = zswap_trees[type]; | |
1087 | struct zswap_entry *entry; | |
2b281117 SJ |
1088 | |
1089 | /* find */ | |
1090 | spin_lock(&tree->lock); | |
1091 | entry = zswap_rb_search(&tree->rbroot, offset); | |
1092 | if (!entry) { | |
1093 | /* entry was written back */ | |
1094 | spin_unlock(&tree->lock); | |
1095 | return; | |
1096 | } | |
1097 | ||
1098 | /* remove from rbtree */ | |
0ab0abcf | 1099 | zswap_rb_erase(&tree->rbroot, entry); |
2b281117 SJ |
1100 | |
1101 | /* drop the initial reference from entry creation */ | |
0ab0abcf | 1102 | zswap_entry_put(tree, entry); |
2b281117 SJ |
1103 | |
1104 | spin_unlock(&tree->lock); | |
2b281117 SJ |
1105 | } |
1106 | ||
1107 | /* frees all zswap entries for the given swap type */ | |
1108 | static void zswap_frontswap_invalidate_area(unsigned type) | |
1109 | { | |
1110 | struct zswap_tree *tree = zswap_trees[type]; | |
0bd42136 | 1111 | struct zswap_entry *entry, *n; |
2b281117 SJ |
1112 | |
1113 | if (!tree) | |
1114 | return; | |
1115 | ||
1116 | /* walk the tree and free everything */ | |
1117 | spin_lock(&tree->lock); | |
0ab0abcf | 1118 | rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode) |
60105e12 | 1119 | zswap_free_entry(entry); |
2b281117 SJ |
1120 | tree->rbroot = RB_ROOT; |
1121 | spin_unlock(&tree->lock); | |
aa9bca05 WY |
1122 | kfree(tree); |
1123 | zswap_trees[type] = NULL; | |
2b281117 SJ |
1124 | } |
1125 | ||
2b281117 SJ |
1126 | static void zswap_frontswap_init(unsigned type) |
1127 | { | |
1128 | struct zswap_tree *tree; | |
1129 | ||
1130 | tree = kzalloc(sizeof(struct zswap_tree), GFP_KERNEL); | |
60105e12 MK |
1131 | if (!tree) { |
1132 | pr_err("alloc failed, zswap disabled for swap type %d\n", type); | |
1133 | return; | |
1134 | } | |
1135 | ||
2b281117 SJ |
1136 | tree->rbroot = RB_ROOT; |
1137 | spin_lock_init(&tree->lock); | |
1138 | zswap_trees[type] = tree; | |
2b281117 SJ |
1139 | } |
1140 | ||
1141 | static struct frontswap_ops zswap_frontswap_ops = { | |
1142 | .store = zswap_frontswap_store, | |
1143 | .load = zswap_frontswap_load, | |
1144 | .invalidate_page = zswap_frontswap_invalidate_page, | |
1145 | .invalidate_area = zswap_frontswap_invalidate_area, | |
1146 | .init = zswap_frontswap_init | |
1147 | }; | |
1148 | ||
1149 | /********************************* | |
1150 | * debugfs functions | |
1151 | **********************************/ | |
1152 | #ifdef CONFIG_DEBUG_FS | |
1153 | #include <linux/debugfs.h> | |
1154 | ||
1155 | static struct dentry *zswap_debugfs_root; | |
1156 | ||
1157 | static int __init zswap_debugfs_init(void) | |
1158 | { | |
1159 | if (!debugfs_initialized()) | |
1160 | return -ENODEV; | |
1161 | ||
1162 | zswap_debugfs_root = debugfs_create_dir("zswap", NULL); | |
1163 | if (!zswap_debugfs_root) | |
1164 | return -ENOMEM; | |
1165 | ||
1166 | debugfs_create_u64("pool_limit_hit", S_IRUGO, | |
1167 | zswap_debugfs_root, &zswap_pool_limit_hit); | |
1168 | debugfs_create_u64("reject_reclaim_fail", S_IRUGO, | |
1169 | zswap_debugfs_root, &zswap_reject_reclaim_fail); | |
1170 | debugfs_create_u64("reject_alloc_fail", S_IRUGO, | |
1171 | zswap_debugfs_root, &zswap_reject_alloc_fail); | |
1172 | debugfs_create_u64("reject_kmemcache_fail", S_IRUGO, | |
1173 | zswap_debugfs_root, &zswap_reject_kmemcache_fail); | |
1174 | debugfs_create_u64("reject_compress_poor", S_IRUGO, | |
1175 | zswap_debugfs_root, &zswap_reject_compress_poor); | |
1176 | debugfs_create_u64("written_back_pages", S_IRUGO, | |
1177 | zswap_debugfs_root, &zswap_written_back_pages); | |
1178 | debugfs_create_u64("duplicate_entry", S_IRUGO, | |
1179 | zswap_debugfs_root, &zswap_duplicate_entry); | |
12d79d64 DS |
1180 | debugfs_create_u64("pool_total_size", S_IRUGO, |
1181 | zswap_debugfs_root, &zswap_pool_total_size); | |
2b281117 SJ |
1182 | debugfs_create_atomic_t("stored_pages", S_IRUGO, |
1183 | zswap_debugfs_root, &zswap_stored_pages); | |
1184 | ||
1185 | return 0; | |
1186 | } | |
1187 | ||
1188 | static void __exit zswap_debugfs_exit(void) | |
1189 | { | |
1190 | debugfs_remove_recursive(zswap_debugfs_root); | |
1191 | } | |
1192 | #else | |
1193 | static int __init zswap_debugfs_init(void) | |
1194 | { | |
1195 | return 0; | |
1196 | } | |
1197 | ||
1198 | static void __exit zswap_debugfs_exit(void) { } | |
1199 | #endif | |
1200 | ||
1201 | /********************************* | |
1202 | * module init and exit | |
1203 | **********************************/ | |
1204 | static int __init init_zswap(void) | |
1205 | { | |
f1c54846 | 1206 | struct zswap_pool *pool; |
ad7ed770 | 1207 | int ret; |
60105e12 | 1208 | |
90b0fc26 DS |
1209 | zswap_init_started = true; |
1210 | ||
2b281117 SJ |
1211 | if (zswap_entry_cache_create()) { |
1212 | pr_err("entry cache creation failed\n"); | |
f1c54846 | 1213 | goto cache_fail; |
2b281117 | 1214 | } |
f1c54846 | 1215 | |
ad7ed770 SAS |
1216 | ret = cpuhp_setup_state(CPUHP_MM_ZSWP_MEM_PREPARE, "mm/zswap:prepare", |
1217 | zswap_dstmem_prepare, zswap_dstmem_dead); | |
1218 | if (ret) { | |
f1c54846 DS |
1219 | pr_err("dstmem alloc failed\n"); |
1220 | goto dstmem_fail; | |
2b281117 | 1221 | } |
f1c54846 | 1222 | |
cab7a7e5 SAS |
1223 | ret = cpuhp_setup_state_multi(CPUHP_MM_ZSWP_POOL_PREPARE, |
1224 | "mm/zswap_pool:prepare", | |
1225 | zswap_cpu_comp_prepare, | |
1226 | zswap_cpu_comp_dead); | |
1227 | if (ret) | |
1228 | goto hp_fail; | |
1229 | ||
f1c54846 | 1230 | pool = __zswap_pool_create_fallback(); |
ae3d89a7 DS |
1231 | if (pool) { |
1232 | pr_info("loaded using pool %s/%s\n", pool->tfm_name, | |
1233 | zpool_get_type(pool->zpool)); | |
1234 | list_add(&pool->list, &zswap_pools); | |
1235 | zswap_has_pool = true; | |
1236 | } else { | |
f1c54846 | 1237 | pr_err("pool creation failed\n"); |
ae3d89a7 | 1238 | zswap_enabled = false; |
2b281117 | 1239 | } |
60105e12 | 1240 | |
2b281117 SJ |
1241 | frontswap_register_ops(&zswap_frontswap_ops); |
1242 | if (zswap_debugfs_init()) | |
1243 | pr_warn("debugfs initialization failed\n"); | |
1244 | return 0; | |
f1c54846 | 1245 | |
cab7a7e5 | 1246 | hp_fail: |
ad7ed770 | 1247 | cpuhp_remove_state(CPUHP_MM_ZSWP_MEM_PREPARE); |
f1c54846 | 1248 | dstmem_fail: |
c119239b | 1249 | zswap_entry_cache_destroy(); |
f1c54846 | 1250 | cache_fail: |
d7b028f5 DS |
1251 | /* if built-in, we aren't unloaded on failure; don't allow use */ |
1252 | zswap_init_failed = true; | |
1253 | zswap_enabled = false; | |
2b281117 SJ |
1254 | return -ENOMEM; |
1255 | } | |
1256 | /* must be late so crypto has time to come up */ | |
1257 | late_initcall(init_zswap); | |
1258 | ||
1259 | MODULE_LICENSE("GPL"); | |
68386da8 | 1260 | MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>"); |
2b281117 | 1261 | MODULE_DESCRIPTION("Compressed cache for swap pages"); |