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