blob: 9e8565d9c66beebc14c93dcf8e03a7f76b73f334 [file] [log] [blame]
Seth Jennings2b281112013-07-10 16:05:03 -07001/*
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>
Dan Streetman12d79d62014-08-06 16:08:40 -070037#include <linux/zpool.h>
Seth Jennings2b281112013-07-10 16:05:03 -070038
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**********************************/
Dan Streetman12d79d62014-08-06 16:08:40 -070048/* Total bytes used by the compressed storage */
49static u64 zswap_pool_total_size;
Seth Jennings2b281112013-07-10 16:05:03 -070050/* The number of compressed pages currently stored in zswap */
51static 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) */
61static u64 zswap_pool_limit_hit;
62/* Pages written back when pool limit was reached */
63static u64 zswap_written_back_pages;
64/* Store failed due to a reclaim failure after pool limit was reached */
65static u64 zswap_reject_reclaim_fail;
66/* Compressed page was too big for the allocator to (optimally) store */
67static u64 zswap_reject_compress_poor;
68/* Store failed because underlying allocator could not get memory */
69static u64 zswap_reject_alloc_fail;
70/* Store failed because the entry metadata could not be allocated (rare) */
71static u64 zswap_reject_kmemcache_fail;
72/* Duplicate store was encountered (rare) */
73static u64 zswap_duplicate_entry;
74
75/*********************************
76* tunables
77**********************************/
Dan Streetmanc00ed162015-06-25 15:00:35 -070078
Dan Streetmanbae21db2017-02-27 14:26:50 -080079#define ZSWAP_PARAM_UNSET ""
80
Dan Streetmanc00ed162015-06-25 15:00:35 -070081/* Enable/disable zswap (disabled by default) */
82static bool zswap_enabled;
Dan Streetmand7b028f2017-02-03 13:13:09 -080083static int zswap_enabled_param_set(const char *,
84 const struct kernel_param *);
85static struct kernel_param_ops zswap_enabled_param_ops = {
86 .set = zswap_enabled_param_set,
87 .get = param_get_bool,
88};
89module_param_cb(enabled, &zswap_enabled_param_ops, &zswap_enabled, 0644);
Seth Jennings2b281112013-07-10 16:05:03 -070090
Dan Streetman90b0fc22015-09-09 15:35:21 -070091/* Crypto compressor to use */
Seth Jennings2b281112013-07-10 16:05:03 -070092#define ZSWAP_COMPRESSOR_DEFAULT "lzo"
Dan Streetmanc99b42c2015-11-06 16:29:15 -080093static char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
Dan Streetman90b0fc22015-09-09 15:35:21 -070094static int zswap_compressor_param_set(const char *,
95 const struct kernel_param *);
96static struct kernel_param_ops zswap_compressor_param_ops = {
97 .set = zswap_compressor_param_set,
Dan Streetmanc99b42c2015-11-06 16:29:15 -080098 .get = param_get_charp,
99 .free = param_free_charp,
Dan Streetman90b0fc22015-09-09 15:35:21 -0700100};
101module_param_cb(compressor, &zswap_compressor_param_ops,
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800102 &zswap_compressor, 0644);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700103
104/* Compressed storage zpool to use */
105#define ZSWAP_ZPOOL_DEFAULT "zbud"
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800106static char *zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700107static int zswap_zpool_param_set(const char *, const struct kernel_param *);
108static struct kernel_param_ops zswap_zpool_param_ops = {
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800109 .set = zswap_zpool_param_set,
110 .get = param_get_charp,
111 .free = param_free_charp,
Dan Streetman90b0fc22015-09-09 15:35:21 -0700112};
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800113module_param_cb(zpool, &zswap_zpool_param_ops, &zswap_zpool_type, 0644);
Seth Jennings2b281112013-07-10 16:05:03 -0700114
115/* The maximum percentage of memory that the compressed pool can occupy */
116static unsigned int zswap_max_pool_percent = 20;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700117module_param_named(max_pool_percent, zswap_max_pool_percent, uint, 0644);
Minchan Kim60105e12014-04-07 15:38:27 -0700118
Seth Jennings2b281112013-07-10 16:05:03 -0700119/*********************************
Seth Jennings2b281112013-07-10 16:05:03 -0700120* data structures
121**********************************/
Dan Streetmanf1c54842015-09-09 15:35:19 -0700122
123struct zswap_pool {
124 struct zpool *zpool;
125 struct crypto_comp * __percpu *tfm;
126 struct kref kref;
127 struct list_head list;
Dan Streetman200867a2016-05-20 16:59:54 -0700128 struct work_struct work;
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100129 struct hlist_node node;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700130 char tfm_name[CRYPTO_MAX_ALG_NAME];
131};
132
Seth Jennings2b281112013-07-10 16:05:03 -0700133/*
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
Dan Streetmanf1c54842015-09-09 15:35:19 -0700140 * offset - the swap offset for the entry. Index into the red-black tree.
Seth Jennings2b281112013-07-10 16:05:03 -0700141 * refcount - the number of outstanding reference to the entry. This is needed
142 * to protect against premature freeing of the entry by code
SeongJae Park6b452512014-04-07 15:38:25 -0700143 * concurrent calls to load, invalidate, and writeback. The lock
Seth Jennings2b281112013-07-10 16:05:03 -0700144 * 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.
Seth Jennings2b281112013-07-10 16:05:03 -0700147 * length - the length in bytes of the compressed page data. Needed during
SeongJae Park6b452512014-04-07 15:38:25 -0700148 * decompression
Dan Streetmanf1c54842015-09-09 15:35:19 -0700149 * pool - the zswap_pool the entry's data is in
150 * handle - zpool allocation handle that stores the compressed page data
Seth Jennings2b281112013-07-10 16:05:03 -0700151 */
152struct zswap_entry {
153 struct rb_node rbnode;
154 pgoff_t offset;
155 int refcount;
156 unsigned int length;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700157 struct zswap_pool *pool;
Seth Jennings2b281112013-07-10 16:05:03 -0700158 unsigned long handle;
159};
160
161struct 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 */
170struct zswap_tree {
171 struct rb_root rbroot;
172 spinlock_t lock;
Seth Jennings2b281112013-07-10 16:05:03 -0700173};
174
175static struct zswap_tree *zswap_trees[MAX_SWAPFILES];
176
Dan Streetmanf1c54842015-09-09 15:35:19 -0700177/* RCU-protected iteration */
178static LIST_HEAD(zswap_pools);
179/* protects zswap_pools list modification */
180static DEFINE_SPINLOCK(zswap_pools_lock);
Dan Streetman32a4e1692016-05-05 16:22:23 -0700181/* pool counter to provide unique names to zpool */
182static atomic_t zswap_pools_count = ATOMIC_INIT(0);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700183
Dan Streetman90b0fc22015-09-09 15:35:21 -0700184/* used by param callback function */
185static bool zswap_init_started;
186
Dan Streetmand7b028f2017-02-03 13:13:09 -0800187/* fatal error during init */
188static bool zswap_init_failed;
189
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800190/* init completed, but couldn't create the initial pool */
191static bool zswap_has_pool;
192
Dan Streetmanf1c54842015-09-09 15:35:19 -0700193/*********************************
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
201static int zswap_writeback_entry(struct zpool *pool, unsigned long handle);
202static int zswap_pool_get(struct zswap_pool *pool);
203static void zswap_pool_put(struct zswap_pool *pool);
204
205static const struct zpool_ops zswap_zpool_ops = {
206 .evict = zswap_writeback_entry
207};
208
209static 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
215static 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
Seth Jennings2b281112013-07-10 16:05:03 -0700230/*********************************
231* zswap entry functions
232**********************************/
233static struct kmem_cache *zswap_entry_cache;
234
Mahendran Ganeshdd01d7d2014-12-12 16:57:15 -0800235static int __init zswap_entry_cache_create(void)
Seth Jennings2b281112013-07-10 16:05:03 -0700236{
237 zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
SeongJae Park5d2d42d2014-04-07 15:38:28 -0700238 return zswap_entry_cache == NULL;
Seth Jennings2b281112013-07-10 16:05:03 -0700239}
240
Fabian Frederickc1192392014-08-08 14:19:35 -0700241static void __init zswap_entry_cache_destroy(void)
Seth Jennings2b281112013-07-10 16:05:03 -0700242{
243 kmem_cache_destroy(zswap_entry_cache);
244}
245
246static 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;
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800253 RB_CLEAR_NODE(&entry->rbnode);
Seth Jennings2b281112013-07-10 16:05:03 -0700254 return entry;
255}
256
257static void zswap_entry_cache_free(struct zswap_entry *entry)
258{
259 kmem_cache_free(zswap_entry_cache, entry);
260}
261
Seth Jennings2b281112013-07-10 16:05:03 -0700262/*********************************
263* rbtree functions
264**********************************/
265static 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 */
286static 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
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800309static 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/*
Dan Streetman12d79d62014-08-06 16:08:40 -0700318 * Carries out the common pattern of freeing and entry's zpool allocation,
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800319 * freeing the entry itself, and decrementing the number of stored pages.
320 */
Minchan Kim60105e12014-04-07 15:38:27 -0700321static void zswap_free_entry(struct zswap_entry *entry)
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800322{
Dan Streetmanf1c54842015-09-09 15:35:19 -0700323 zpool_free(entry->pool->zpool, entry->handle);
324 zswap_pool_put(entry->pool);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800325 zswap_entry_cache_free(entry);
326 atomic_dec(&zswap_stored_pages);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700327 zswap_update_total_size();
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800328}
329
330/* caller must hold the tree lock */
331static 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*/
339static 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);
Minchan Kim60105e12014-04-07 15:38:27 -0700347 zswap_free_entry(entry);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800348 }
349}
350
351/* caller must hold the tree lock */
352static struct zswap_entry *zswap_entry_find_get(struct rb_root *root,
353 pgoff_t offset)
354{
Alexey Klimovb0c98652015-11-06 16:29:09 -0800355 struct zswap_entry *entry;
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800356
357 entry = zswap_rb_search(root, offset);
358 if (entry)
359 zswap_entry_get(entry);
360
361 return entry;
362}
363
Seth Jennings2b281112013-07-10 16:05:03 -0700364/*********************************
365* per-cpu code
366**********************************/
367static DEFINE_PER_CPU(u8 *, zswap_dstmem);
368
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +0100369static int zswap_dstmem_prepare(unsigned int cpu)
Seth Jennings2b281112013-07-10 16:05:03 -0700370{
Seth Jennings2b281112013-07-10 16:05:03 -0700371 u8 *dst;
372
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +0100373 dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
374 if (!dst) {
375 pr_err("can't allocate compressor buffer\n");
376 return -ENOMEM;
Seth Jennings2b281112013-07-10 16:05:03 -0700377 }
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +0100378 per_cpu(zswap_dstmem, cpu) = dst;
Seth Jennings2b281112013-07-10 16:05:03 -0700379 return 0;
Seth Jennings2b281112013-07-10 16:05:03 -0700380}
381
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +0100382static int zswap_dstmem_dead(unsigned int cpu)
Seth Jennings2b281112013-07-10 16:05:03 -0700383{
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +0100384 u8 *dst;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700385
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +0100386 dst = per_cpu(zswap_dstmem, cpu);
387 kfree(dst);
388 per_cpu(zswap_dstmem, cpu) = NULL;
389
390 return 0;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700391}
392
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100393static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
Dan Streetmanf1c54842015-09-09 15:35:19 -0700394{
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100395 struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700396 struct crypto_comp *tfm;
397
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100398 if (WARN_ON(*per_cpu_ptr(pool->tfm, cpu)))
399 return 0;
400
401 tfm = crypto_alloc_comp(pool->tfm_name, 0, 0);
402 if (IS_ERR_OR_NULL(tfm)) {
403 pr_err("could not alloc crypto comp %s : %ld\n",
404 pool->tfm_name, PTR_ERR(tfm));
405 return -ENOMEM;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700406 }
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100407 *per_cpu_ptr(pool->tfm, cpu) = tfm;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700408 return 0;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700409}
410
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100411static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)
Dan Streetmanf1c54842015-09-09 15:35:19 -0700412{
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100413 struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
414 struct crypto_comp *tfm;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700415
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100416 tfm = *per_cpu_ptr(pool->tfm, cpu);
417 if (!IS_ERR_OR_NULL(tfm))
418 crypto_free_comp(tfm);
419 *per_cpu_ptr(pool->tfm, cpu) = NULL;
420 return 0;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700421}
422
423/*********************************
424* pool functions
425**********************************/
426
427static struct zswap_pool *__zswap_pool_current(void)
428{
429 struct zswap_pool *pool;
430
431 pool = list_first_or_null_rcu(&zswap_pools, typeof(*pool), list);
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800432 WARN_ONCE(!pool && zswap_has_pool,
433 "%s: no page storage pool!\n", __func__);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700434
435 return pool;
436}
437
438static struct zswap_pool *zswap_pool_current(void)
439{
440 assert_spin_locked(&zswap_pools_lock);
441
442 return __zswap_pool_current();
443}
444
445static struct zswap_pool *zswap_pool_current_get(void)
446{
447 struct zswap_pool *pool;
448
449 rcu_read_lock();
450
451 pool = __zswap_pool_current();
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800452 if (!zswap_pool_get(pool))
Dan Streetmanf1c54842015-09-09 15:35:19 -0700453 pool = NULL;
454
455 rcu_read_unlock();
456
457 return pool;
458}
459
460static struct zswap_pool *zswap_pool_last_get(void)
461{
462 struct zswap_pool *pool, *last = NULL;
463
464 rcu_read_lock();
465
466 list_for_each_entry_rcu(pool, &zswap_pools, list)
467 last = pool;
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800468 WARN_ONCE(!last && zswap_has_pool,
469 "%s: no page storage pool!\n", __func__);
470 if (!zswap_pool_get(last))
Dan Streetmanf1c54842015-09-09 15:35:19 -0700471 last = NULL;
472
473 rcu_read_unlock();
474
475 return last;
476}
477
Dan Streetman8bc8b222015-12-18 14:22:04 -0800478/* type and compressor must be null-terminated */
Dan Streetmanf1c54842015-09-09 15:35:19 -0700479static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor)
480{
481 struct zswap_pool *pool;
482
483 assert_spin_locked(&zswap_pools_lock);
484
485 list_for_each_entry_rcu(pool, &zswap_pools, list) {
Dan Streetman8bc8b222015-12-18 14:22:04 -0800486 if (strcmp(pool->tfm_name, compressor))
Dan Streetmanf1c54842015-09-09 15:35:19 -0700487 continue;
Dan Streetman8bc8b222015-12-18 14:22:04 -0800488 if (strcmp(zpool_get_type(pool->zpool), type))
Dan Streetmanf1c54842015-09-09 15:35:19 -0700489 continue;
490 /* if we can't get it, it's about to be destroyed */
491 if (!zswap_pool_get(pool))
492 continue;
493 return pool;
494 }
495
496 return NULL;
497}
498
499static struct zswap_pool *zswap_pool_create(char *type, char *compressor)
500{
501 struct zswap_pool *pool;
Dan Streetman32a4e1692016-05-05 16:22:23 -0700502 char name[38]; /* 'zswap' + 32 char (max) num + \0 */
Mel Gormand0164ad2015-11-06 16:28:21 -0800503 gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100504 int ret;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700505
Dan Streetmanbae21db2017-02-27 14:26:50 -0800506 if (!zswap_has_pool) {
507 /* if either are unset, pool initialization failed, and we
508 * need both params to be set correctly before trying to
509 * create a pool.
510 */
511 if (!strcmp(type, ZSWAP_PARAM_UNSET))
512 return NULL;
513 if (!strcmp(compressor, ZSWAP_PARAM_UNSET))
514 return NULL;
515 }
516
Dan Streetmanf1c54842015-09-09 15:35:19 -0700517 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
518 if (!pool) {
519 pr_err("pool alloc failed\n");
520 return NULL;
521 }
522
Dan Streetman32a4e1692016-05-05 16:22:23 -0700523 /* unique name for each pool specifically required by zsmalloc */
524 snprintf(name, 38, "zswap%x", atomic_inc_return(&zswap_pools_count));
525
526 pool->zpool = zpool_create_pool(type, name, gfp, &zswap_zpool_ops);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700527 if (!pool->zpool) {
528 pr_err("%s zpool not available\n", type);
529 goto error;
530 }
531 pr_debug("using %s zpool\n", zpool_get_type(pool->zpool));
532
533 strlcpy(pool->tfm_name, compressor, sizeof(pool->tfm_name));
534 pool->tfm = alloc_percpu(struct crypto_comp *);
535 if (!pool->tfm) {
536 pr_err("percpu alloc failed\n");
537 goto error;
538 }
539
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100540 ret = cpuhp_state_add_instance(CPUHP_MM_ZSWP_POOL_PREPARE,
541 &pool->node);
542 if (ret)
Dan Streetmanf1c54842015-09-09 15:35:19 -0700543 goto error;
544 pr_debug("using %s compressor\n", pool->tfm_name);
545
546 /* being the current pool takes 1 ref; this func expects the
547 * caller to always add the new pool as the current pool
548 */
549 kref_init(&pool->kref);
550 INIT_LIST_HEAD(&pool->list);
551
552 zswap_pool_debug("created", pool);
553
554 return pool;
555
556error:
557 free_percpu(pool->tfm);
558 if (pool->zpool)
559 zpool_destroy_pool(pool->zpool);
560 kfree(pool);
561 return NULL;
562}
563
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800564static __init struct zswap_pool *__zswap_pool_create_fallback(void)
Dan Streetmanf1c54842015-09-09 15:35:19 -0700565{
Dan Streetmanbae21db2017-02-27 14:26:50 -0800566 bool has_comp, has_zpool;
567
568 has_comp = crypto_has_comp(zswap_compressor, 0, 0);
569 if (!has_comp && strcmp(zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT)) {
Dan Streetmanf1c54842015-09-09 15:35:19 -0700570 pr_err("compressor %s not available, using default %s\n",
571 zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT);
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800572 param_free_charp(&zswap_compressor);
573 zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
Dan Streetmanbae21db2017-02-27 14:26:50 -0800574 has_comp = crypto_has_comp(zswap_compressor, 0, 0);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700575 }
Dan Streetmanbae21db2017-02-27 14:26:50 -0800576 if (!has_comp) {
577 pr_err("default compressor %s not available\n",
578 zswap_compressor);
579 param_free_charp(&zswap_compressor);
580 zswap_compressor = ZSWAP_PARAM_UNSET;
581 }
582
583 has_zpool = zpool_has_pool(zswap_zpool_type);
584 if (!has_zpool && strcmp(zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT)) {
Dan Streetmanf1c54842015-09-09 15:35:19 -0700585 pr_err("zpool %s not available, using default %s\n",
586 zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT);
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800587 param_free_charp(&zswap_zpool_type);
588 zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
Dan Streetmanbae21db2017-02-27 14:26:50 -0800589 has_zpool = zpool_has_pool(zswap_zpool_type);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700590 }
Dan Streetmanbae21db2017-02-27 14:26:50 -0800591 if (!has_zpool) {
592 pr_err("default zpool %s not available\n",
593 zswap_zpool_type);
594 param_free_charp(&zswap_zpool_type);
595 zswap_zpool_type = ZSWAP_PARAM_UNSET;
596 }
597
598 if (!has_comp || !has_zpool)
599 return NULL;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700600
601 return zswap_pool_create(zswap_zpool_type, zswap_compressor);
602}
603
604static void zswap_pool_destroy(struct zswap_pool *pool)
605{
606 zswap_pool_debug("destroying", pool);
607
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100608 cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700609 free_percpu(pool->tfm);
610 zpool_destroy_pool(pool->zpool);
611 kfree(pool);
612}
613
614static int __must_check zswap_pool_get(struct zswap_pool *pool)
615{
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800616 if (!pool)
617 return 0;
618
Dan Streetmanf1c54842015-09-09 15:35:19 -0700619 return kref_get_unless_zero(&pool->kref);
620}
621
Dan Streetman200867a2016-05-20 16:59:54 -0700622static void __zswap_pool_release(struct work_struct *work)
Dan Streetmanf1c54842015-09-09 15:35:19 -0700623{
Dan Streetman200867a2016-05-20 16:59:54 -0700624 struct zswap_pool *pool = container_of(work, typeof(*pool), work);
625
626 synchronize_rcu();
Dan Streetmanf1c54842015-09-09 15:35:19 -0700627
628 /* nobody should have been able to get a kref... */
629 WARN_ON(kref_get_unless_zero(&pool->kref));
630
631 /* pool is now off zswap_pools list and has no references. */
632 zswap_pool_destroy(pool);
633}
634
635static void __zswap_pool_empty(struct kref *kref)
636{
637 struct zswap_pool *pool;
638
639 pool = container_of(kref, typeof(*pool), kref);
640
641 spin_lock(&zswap_pools_lock);
642
643 WARN_ON(pool == zswap_pool_current());
644
645 list_del_rcu(&pool->list);
Dan Streetman200867a2016-05-20 16:59:54 -0700646
647 INIT_WORK(&pool->work, __zswap_pool_release);
648 schedule_work(&pool->work);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700649
650 spin_unlock(&zswap_pools_lock);
651}
652
653static void zswap_pool_put(struct zswap_pool *pool)
654{
655 kref_put(&pool->kref, __zswap_pool_empty);
Seth Jennings2b281112013-07-10 16:05:03 -0700656}
657
Seth Jennings2b281112013-07-10 16:05:03 -0700658/*********************************
Dan Streetman90b0fc22015-09-09 15:35:21 -0700659* param callbacks
660**********************************/
661
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800662/* val must be a null-terminated string */
Dan Streetman90b0fc22015-09-09 15:35:21 -0700663static int __zswap_param_set(const char *val, const struct kernel_param *kp,
664 char *type, char *compressor)
665{
666 struct zswap_pool *pool, *put_pool = NULL;
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800667 char *s = strstrip((char *)val);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700668 int ret;
669
Dan Streetmand7b028f2017-02-03 13:13:09 -0800670 if (zswap_init_failed) {
671 pr_err("can't set param, initialization failed\n");
672 return -ENODEV;
673 }
674
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800675 /* no change required */
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800676 if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool)
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800677 return 0;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700678
679 /* if this is load-time (pre-init) param setting,
680 * don't create a pool; that's done during init.
681 */
682 if (!zswap_init_started)
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800683 return param_set_charp(s, kp);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700684
685 if (!type) {
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800686 if (!zpool_has_pool(s)) {
687 pr_err("zpool %s not available\n", s);
688 return -ENOENT;
689 }
Dan Streetman90b0fc22015-09-09 15:35:21 -0700690 type = s;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700691 } else if (!compressor) {
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800692 if (!crypto_has_comp(s, 0, 0)) {
693 pr_err("compressor %s not available\n", s);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700694 return -ENOENT;
695 }
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800696 compressor = s;
697 } else {
698 WARN_ON(1);
699 return -EINVAL;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700700 }
701
702 spin_lock(&zswap_pools_lock);
703
704 pool = zswap_pool_find_get(type, compressor);
705 if (pool) {
706 zswap_pool_debug("using existing", pool);
707 list_del_rcu(&pool->list);
708 } else {
709 spin_unlock(&zswap_pools_lock);
710 pool = zswap_pool_create(type, compressor);
711 spin_lock(&zswap_pools_lock);
712 }
713
714 if (pool)
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800715 ret = param_set_charp(s, kp);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700716 else
717 ret = -EINVAL;
718
719 if (!ret) {
720 put_pool = zswap_pool_current();
721 list_add_rcu(&pool->list, &zswap_pools);
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800722 zswap_has_pool = true;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700723 } else if (pool) {
724 /* add the possibly pre-existing pool to the end of the pools
725 * list; if it's new (and empty) then it'll be removed and
726 * destroyed by the put after we drop the lock
727 */
728 list_add_tail_rcu(&pool->list, &zswap_pools);
729 put_pool = pool;
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800730 } else if (!zswap_has_pool) {
731 /* if initial pool creation failed, and this pool creation also
732 * failed, maybe both compressor and zpool params were bad.
733 * Allow changing this param, so pool creation will succeed
734 * when the other param is changed. We already verified this
735 * param is ok in the zpool_has_pool() or crypto_has_comp()
736 * checks above.
737 */
738 ret = param_set_charp(s, kp);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700739 }
740
741 spin_unlock(&zswap_pools_lock);
742
743 /* drop the ref from either the old current pool,
744 * or the new pool we failed to add
745 */
746 if (put_pool)
747 zswap_pool_put(put_pool);
748
749 return ret;
750}
751
752static int zswap_compressor_param_set(const char *val,
753 const struct kernel_param *kp)
754{
755 return __zswap_param_set(val, kp, zswap_zpool_type, NULL);
756}
757
758static int zswap_zpool_param_set(const char *val,
759 const struct kernel_param *kp)
760{
761 return __zswap_param_set(val, kp, NULL, zswap_compressor);
762}
763
Dan Streetmand7b028f2017-02-03 13:13:09 -0800764static int zswap_enabled_param_set(const char *val,
765 const struct kernel_param *kp)
766{
767 if (zswap_init_failed) {
768 pr_err("can't enable, initialization failed\n");
769 return -ENODEV;
770 }
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800771 if (!zswap_has_pool && zswap_init_started) {
772 pr_err("can't enable, no pool configured\n");
773 return -ENODEV;
774 }
Dan Streetmand7b028f2017-02-03 13:13:09 -0800775
776 return param_set_bool(val, kp);
777}
778
Dan Streetman90b0fc22015-09-09 15:35:21 -0700779/*********************************
Seth Jennings2b281112013-07-10 16:05:03 -0700780* writeback code
781**********************************/
782/* return enum for zswap_get_swap_cache_page */
783enum zswap_get_swap_ret {
784 ZSWAP_SWAPCACHE_NEW,
785 ZSWAP_SWAPCACHE_EXIST,
Weijie Yang67d13fe2013-11-12 15:08:26 -0800786 ZSWAP_SWAPCACHE_FAIL,
Seth Jennings2b281112013-07-10 16:05:03 -0700787};
788
789/*
790 * zswap_get_swap_cache_page
791 *
792 * This is an adaption of read_swap_cache_async()
793 *
794 * This function tries to find a page with the given swap entry
795 * in the swapper_space address space (the swap cache). If the page
796 * is found, it is returned in retpage. Otherwise, a page is allocated,
797 * added to the swap cache, and returned in retpage.
798 *
799 * If success, the swap cache page is returned in retpage
Weijie Yang67d13fe2013-11-12 15:08:26 -0800800 * Returns ZSWAP_SWAPCACHE_EXIST if page was already in the swap cache
801 * Returns ZSWAP_SWAPCACHE_NEW if the new page needs to be populated,
802 * the new page is added to swapcache and locked
803 * Returns ZSWAP_SWAPCACHE_FAIL on error
Seth Jennings2b281112013-07-10 16:05:03 -0700804 */
805static int zswap_get_swap_cache_page(swp_entry_t entry,
806 struct page **retpage)
807{
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700808 bool page_was_allocated;
Seth Jennings2b281112013-07-10 16:05:03 -0700809
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700810 *retpage = __read_swap_cache_async(entry, GFP_KERNEL,
811 NULL, 0, &page_was_allocated);
812 if (page_was_allocated)
813 return ZSWAP_SWAPCACHE_NEW;
814 if (!*retpage)
Weijie Yang67d13fe2013-11-12 15:08:26 -0800815 return ZSWAP_SWAPCACHE_FAIL;
Seth Jennings2b281112013-07-10 16:05:03 -0700816 return ZSWAP_SWAPCACHE_EXIST;
817}
818
819/*
820 * Attempts to free an entry by adding a page to the swap cache,
821 * decompressing the entry data into the page, and issuing a
822 * bio write to write the page back to the swap device.
823 *
824 * This can be thought of as a "resumed writeback" of the page
825 * to the swap device. We are basically resuming the same swap
826 * writeback path that was intercepted with the frontswap_store()
827 * in the first place. After the page has been decompressed into
828 * the swap cache, the compressed version stored by zswap can be
829 * freed.
830 */
Dan Streetman12d79d62014-08-06 16:08:40 -0700831static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
Seth Jennings2b281112013-07-10 16:05:03 -0700832{
833 struct zswap_header *zhdr;
834 swp_entry_t swpentry;
835 struct zswap_tree *tree;
836 pgoff_t offset;
837 struct zswap_entry *entry;
838 struct page *page;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700839 struct crypto_comp *tfm;
Seth Jennings2b281112013-07-10 16:05:03 -0700840 u8 *src, *dst;
841 unsigned int dlen;
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800842 int ret;
Seth Jennings2b281112013-07-10 16:05:03 -0700843 struct writeback_control wbc = {
844 .sync_mode = WB_SYNC_NONE,
845 };
846
847 /* extract swpentry from data */
Dan Streetman12d79d62014-08-06 16:08:40 -0700848 zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO);
Seth Jennings2b281112013-07-10 16:05:03 -0700849 swpentry = zhdr->swpentry; /* here */
Dan Streetman12d79d62014-08-06 16:08:40 -0700850 zpool_unmap_handle(pool, handle);
Seth Jennings2b281112013-07-10 16:05:03 -0700851 tree = zswap_trees[swp_type(swpentry)];
852 offset = swp_offset(swpentry);
Seth Jennings2b281112013-07-10 16:05:03 -0700853
854 /* find and ref zswap entry */
855 spin_lock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800856 entry = zswap_entry_find_get(&tree->rbroot, offset);
Seth Jennings2b281112013-07-10 16:05:03 -0700857 if (!entry) {
858 /* entry was invalidated */
859 spin_unlock(&tree->lock);
860 return 0;
861 }
Seth Jennings2b281112013-07-10 16:05:03 -0700862 spin_unlock(&tree->lock);
863 BUG_ON(offset != entry->offset);
864
865 /* try to allocate swap cache page */
866 switch (zswap_get_swap_cache_page(swpentry, &page)) {
Weijie Yang67d13fe2013-11-12 15:08:26 -0800867 case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */
Seth Jennings2b281112013-07-10 16:05:03 -0700868 ret = -ENOMEM;
869 goto fail;
870
Weijie Yang67d13fe2013-11-12 15:08:26 -0800871 case ZSWAP_SWAPCACHE_EXIST:
Seth Jennings2b281112013-07-10 16:05:03 -0700872 /* page is already in the swap cache, ignore for now */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300873 put_page(page);
Seth Jennings2b281112013-07-10 16:05:03 -0700874 ret = -EEXIST;
875 goto fail;
876
877 case ZSWAP_SWAPCACHE_NEW: /* page is locked */
878 /* decompress */
879 dlen = PAGE_SIZE;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700880 src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle,
Dan Streetman12d79d62014-08-06 16:08:40 -0700881 ZPOOL_MM_RO) + sizeof(struct zswap_header);
Seth Jennings2b281112013-07-10 16:05:03 -0700882 dst = kmap_atomic(page);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700883 tfm = *get_cpu_ptr(entry->pool->tfm);
884 ret = crypto_comp_decompress(tfm, src, entry->length,
885 dst, &dlen);
886 put_cpu_ptr(entry->pool->tfm);
Seth Jennings2b281112013-07-10 16:05:03 -0700887 kunmap_atomic(dst);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700888 zpool_unmap_handle(entry->pool->zpool, entry->handle);
Seth Jennings2b281112013-07-10 16:05:03 -0700889 BUG_ON(ret);
890 BUG_ON(dlen != PAGE_SIZE);
891
892 /* page is up to date */
893 SetPageUptodate(page);
894 }
895
Weijie Yangb349acc2013-11-12 15:07:52 -0800896 /* move it to the tail of the inactive list after end_writeback */
897 SetPageReclaim(page);
898
Seth Jennings2b281112013-07-10 16:05:03 -0700899 /* start writeback */
900 __swap_writepage(page, &wbc, end_swap_bio_write);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300901 put_page(page);
Seth Jennings2b281112013-07-10 16:05:03 -0700902 zswap_written_back_pages++;
903
904 spin_lock(&tree->lock);
Seth Jennings2b281112013-07-10 16:05:03 -0700905 /* drop local reference */
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800906 zswap_entry_put(tree, entry);
Seth Jennings2b281112013-07-10 16:05:03 -0700907
908 /*
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800909 * There are two possible situations for entry here:
910 * (1) refcount is 1(normal case), entry is valid and on the tree
911 * (2) refcount is 0, entry is freed and not on the tree
912 * because invalidate happened during writeback
913 * search the tree and free the entry if find entry
914 */
915 if (entry == zswap_rb_search(&tree->rbroot, offset))
916 zswap_entry_put(tree, entry);
Seth Jennings2b281112013-07-10 16:05:03 -0700917 spin_unlock(&tree->lock);
Seth Jennings2b281112013-07-10 16:05:03 -0700918
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800919 goto end;
920
921 /*
922 * if we get here due to ZSWAP_SWAPCACHE_EXIST
923 * a load may happening concurrently
924 * it is safe and okay to not free the entry
925 * if we free the entry in the following put
926 * it it either okay to return !0
927 */
Seth Jennings2b281112013-07-10 16:05:03 -0700928fail:
929 spin_lock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800930 zswap_entry_put(tree, entry);
Seth Jennings2b281112013-07-10 16:05:03 -0700931 spin_unlock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800932
933end:
Seth Jennings2b281112013-07-10 16:05:03 -0700934 return ret;
935}
936
Dan Streetmanf1c54842015-09-09 15:35:19 -0700937static int zswap_shrink(void)
938{
939 struct zswap_pool *pool;
940 int ret;
941
942 pool = zswap_pool_last_get();
943 if (!pool)
944 return -ENOENT;
945
946 ret = zpool_shrink(pool->zpool, 1, NULL);
947
948 zswap_pool_put(pool);
949
950 return ret;
951}
952
Seth Jennings2b281112013-07-10 16:05:03 -0700953/*********************************
954* frontswap hooks
955**********************************/
956/* attempts to compress and store an single page */
957static int zswap_frontswap_store(unsigned type, pgoff_t offset,
958 struct page *page)
959{
960 struct zswap_tree *tree = zswap_trees[type];
961 struct zswap_entry *entry, *dupentry;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700962 struct crypto_comp *tfm;
Seth Jennings2b281112013-07-10 16:05:03 -0700963 int ret;
964 unsigned int dlen = PAGE_SIZE, len;
965 unsigned long handle;
966 char *buf;
967 u8 *src, *dst;
968 struct zswap_header *zhdr;
969
Dan Streetmanc00ed162015-06-25 15:00:35 -0700970 if (!zswap_enabled || !tree) {
Seth Jennings2b281112013-07-10 16:05:03 -0700971 ret = -ENODEV;
972 goto reject;
973 }
974
975 /* reclaim space if needed */
976 if (zswap_is_full()) {
977 zswap_pool_limit_hit++;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700978 if (zswap_shrink()) {
Seth Jennings2b281112013-07-10 16:05:03 -0700979 zswap_reject_reclaim_fail++;
980 ret = -ENOMEM;
981 goto reject;
982 }
983 }
984
985 /* allocate entry */
986 entry = zswap_entry_cache_alloc(GFP_KERNEL);
987 if (!entry) {
988 zswap_reject_kmemcache_fail++;
989 ret = -ENOMEM;
990 goto reject;
991 }
992
Dan Streetmanf1c54842015-09-09 15:35:19 -0700993 /* if entry is successfully added, it keeps the reference */
994 entry->pool = zswap_pool_current_get();
995 if (!entry->pool) {
Seth Jennings2b281112013-07-10 16:05:03 -0700996 ret = -EINVAL;
997 goto freepage;
998 }
999
Dan Streetmanf1c54842015-09-09 15:35:19 -07001000 /* compress */
1001 dst = get_cpu_var(zswap_dstmem);
1002 tfm = *get_cpu_ptr(entry->pool->tfm);
1003 src = kmap_atomic(page);
1004 ret = crypto_comp_compress(tfm, src, PAGE_SIZE, dst, &dlen);
1005 kunmap_atomic(src);
1006 put_cpu_ptr(entry->pool->tfm);
1007 if (ret) {
1008 ret = -EINVAL;
1009 goto put_dstmem;
1010 }
1011
Seth Jennings2b281112013-07-10 16:05:03 -07001012 /* store */
1013 len = dlen + sizeof(struct zswap_header);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001014 ret = zpool_malloc(entry->pool->zpool, len,
Mel Gormand0164ad2015-11-06 16:28:21 -08001015 __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM,
1016 &handle);
Seth Jennings2b281112013-07-10 16:05:03 -07001017 if (ret == -ENOSPC) {
1018 zswap_reject_compress_poor++;
Dan Streetmanf1c54842015-09-09 15:35:19 -07001019 goto put_dstmem;
Seth Jennings2b281112013-07-10 16:05:03 -07001020 }
1021 if (ret) {
1022 zswap_reject_alloc_fail++;
Dan Streetmanf1c54842015-09-09 15:35:19 -07001023 goto put_dstmem;
Seth Jennings2b281112013-07-10 16:05:03 -07001024 }
Dan Streetmanf1c54842015-09-09 15:35:19 -07001025 zhdr = zpool_map_handle(entry->pool->zpool, handle, ZPOOL_MM_RW);
Seth Jennings2b281112013-07-10 16:05:03 -07001026 zhdr->swpentry = swp_entry(type, offset);
1027 buf = (u8 *)(zhdr + 1);
1028 memcpy(buf, dst, dlen);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001029 zpool_unmap_handle(entry->pool->zpool, handle);
Seth Jennings2b281112013-07-10 16:05:03 -07001030 put_cpu_var(zswap_dstmem);
1031
1032 /* populate entry */
1033 entry->offset = offset;
1034 entry->handle = handle;
1035 entry->length = dlen;
1036
1037 /* map */
1038 spin_lock(&tree->lock);
1039 do {
1040 ret = zswap_rb_insert(&tree->rbroot, entry, &dupentry);
1041 if (ret == -EEXIST) {
1042 zswap_duplicate_entry++;
1043 /* remove from rbtree */
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001044 zswap_rb_erase(&tree->rbroot, dupentry);
1045 zswap_entry_put(tree, dupentry);
Seth Jennings2b281112013-07-10 16:05:03 -07001046 }
1047 } while (ret == -EEXIST);
1048 spin_unlock(&tree->lock);
1049
1050 /* update stats */
1051 atomic_inc(&zswap_stored_pages);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001052 zswap_update_total_size();
Seth Jennings2b281112013-07-10 16:05:03 -07001053
1054 return 0;
1055
Dan Streetmanf1c54842015-09-09 15:35:19 -07001056put_dstmem:
Seth Jennings2b281112013-07-10 16:05:03 -07001057 put_cpu_var(zswap_dstmem);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001058 zswap_pool_put(entry->pool);
1059freepage:
Seth Jennings2b281112013-07-10 16:05:03 -07001060 zswap_entry_cache_free(entry);
1061reject:
1062 return ret;
1063}
1064
1065/*
1066 * returns 0 if the page was successfully decompressed
1067 * return -1 on entry not found or error
1068*/
1069static int zswap_frontswap_load(unsigned type, pgoff_t offset,
1070 struct page *page)
1071{
1072 struct zswap_tree *tree = zswap_trees[type];
1073 struct zswap_entry *entry;
Dan Streetmanf1c54842015-09-09 15:35:19 -07001074 struct crypto_comp *tfm;
Seth Jennings2b281112013-07-10 16:05:03 -07001075 u8 *src, *dst;
1076 unsigned int dlen;
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001077 int ret;
Seth Jennings2b281112013-07-10 16:05:03 -07001078
1079 /* find */
1080 spin_lock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001081 entry = zswap_entry_find_get(&tree->rbroot, offset);
Seth Jennings2b281112013-07-10 16:05:03 -07001082 if (!entry) {
1083 /* entry was written back */
1084 spin_unlock(&tree->lock);
1085 return -1;
1086 }
Seth Jennings2b281112013-07-10 16:05:03 -07001087 spin_unlock(&tree->lock);
1088
1089 /* decompress */
1090 dlen = PAGE_SIZE;
Dan Streetmanf1c54842015-09-09 15:35:19 -07001091 src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle,
Dan Streetman12d79d62014-08-06 16:08:40 -07001092 ZPOOL_MM_RO) + sizeof(struct zswap_header);
Seth Jennings2b281112013-07-10 16:05:03 -07001093 dst = kmap_atomic(page);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001094 tfm = *get_cpu_ptr(entry->pool->tfm);
1095 ret = crypto_comp_decompress(tfm, src, entry->length, dst, &dlen);
1096 put_cpu_ptr(entry->pool->tfm);
Seth Jennings2b281112013-07-10 16:05:03 -07001097 kunmap_atomic(dst);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001098 zpool_unmap_handle(entry->pool->zpool, entry->handle);
Seth Jennings2b281112013-07-10 16:05:03 -07001099 BUG_ON(ret);
1100
1101 spin_lock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001102 zswap_entry_put(tree, entry);
Seth Jennings2b281112013-07-10 16:05:03 -07001103 spin_unlock(&tree->lock);
1104
Seth Jennings2b281112013-07-10 16:05:03 -07001105 return 0;
1106}
1107
1108/* frees an entry in zswap */
1109static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset)
1110{
1111 struct zswap_tree *tree = zswap_trees[type];
1112 struct zswap_entry *entry;
Seth Jennings2b281112013-07-10 16:05:03 -07001113
1114 /* find */
1115 spin_lock(&tree->lock);
1116 entry = zswap_rb_search(&tree->rbroot, offset);
1117 if (!entry) {
1118 /* entry was written back */
1119 spin_unlock(&tree->lock);
1120 return;
1121 }
1122
1123 /* remove from rbtree */
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001124 zswap_rb_erase(&tree->rbroot, entry);
Seth Jennings2b281112013-07-10 16:05:03 -07001125
1126 /* drop the initial reference from entry creation */
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001127 zswap_entry_put(tree, entry);
Seth Jennings2b281112013-07-10 16:05:03 -07001128
1129 spin_unlock(&tree->lock);
Seth Jennings2b281112013-07-10 16:05:03 -07001130}
1131
1132/* frees all zswap entries for the given swap type */
1133static void zswap_frontswap_invalidate_area(unsigned type)
1134{
1135 struct zswap_tree *tree = zswap_trees[type];
Cody P Schafer0bd42132013-09-11 14:25:33 -07001136 struct zswap_entry *entry, *n;
Seth Jennings2b281112013-07-10 16:05:03 -07001137
1138 if (!tree)
1139 return;
1140
1141 /* walk the tree and free everything */
1142 spin_lock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001143 rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode)
Minchan Kim60105e12014-04-07 15:38:27 -07001144 zswap_free_entry(entry);
Seth Jennings2b281112013-07-10 16:05:03 -07001145 tree->rbroot = RB_ROOT;
1146 spin_unlock(&tree->lock);
Weijie Yangaa9bca02013-10-16 13:46:54 -07001147 kfree(tree);
1148 zswap_trees[type] = NULL;
Seth Jennings2b281112013-07-10 16:05:03 -07001149}
1150
Seth Jennings2b281112013-07-10 16:05:03 -07001151static void zswap_frontswap_init(unsigned type)
1152{
1153 struct zswap_tree *tree;
1154
1155 tree = kzalloc(sizeof(struct zswap_tree), GFP_KERNEL);
Minchan Kim60105e12014-04-07 15:38:27 -07001156 if (!tree) {
1157 pr_err("alloc failed, zswap disabled for swap type %d\n", type);
1158 return;
1159 }
1160
Seth Jennings2b281112013-07-10 16:05:03 -07001161 tree->rbroot = RB_ROOT;
1162 spin_lock_init(&tree->lock);
1163 zswap_trees[type] = tree;
Seth Jennings2b281112013-07-10 16:05:03 -07001164}
1165
1166static struct frontswap_ops zswap_frontswap_ops = {
1167 .store = zswap_frontswap_store,
1168 .load = zswap_frontswap_load,
1169 .invalidate_page = zswap_frontswap_invalidate_page,
1170 .invalidate_area = zswap_frontswap_invalidate_area,
1171 .init = zswap_frontswap_init
1172};
1173
1174/*********************************
1175* debugfs functions
1176**********************************/
1177#ifdef CONFIG_DEBUG_FS
1178#include <linux/debugfs.h>
1179
1180static struct dentry *zswap_debugfs_root;
1181
1182static int __init zswap_debugfs_init(void)
1183{
1184 if (!debugfs_initialized())
1185 return -ENODEV;
1186
1187 zswap_debugfs_root = debugfs_create_dir("zswap", NULL);
1188 if (!zswap_debugfs_root)
1189 return -ENOMEM;
1190
1191 debugfs_create_u64("pool_limit_hit", S_IRUGO,
1192 zswap_debugfs_root, &zswap_pool_limit_hit);
1193 debugfs_create_u64("reject_reclaim_fail", S_IRUGO,
1194 zswap_debugfs_root, &zswap_reject_reclaim_fail);
1195 debugfs_create_u64("reject_alloc_fail", S_IRUGO,
1196 zswap_debugfs_root, &zswap_reject_alloc_fail);
1197 debugfs_create_u64("reject_kmemcache_fail", S_IRUGO,
1198 zswap_debugfs_root, &zswap_reject_kmemcache_fail);
1199 debugfs_create_u64("reject_compress_poor", S_IRUGO,
1200 zswap_debugfs_root, &zswap_reject_compress_poor);
1201 debugfs_create_u64("written_back_pages", S_IRUGO,
1202 zswap_debugfs_root, &zswap_written_back_pages);
1203 debugfs_create_u64("duplicate_entry", S_IRUGO,
1204 zswap_debugfs_root, &zswap_duplicate_entry);
Dan Streetman12d79d62014-08-06 16:08:40 -07001205 debugfs_create_u64("pool_total_size", S_IRUGO,
1206 zswap_debugfs_root, &zswap_pool_total_size);
Seth Jennings2b281112013-07-10 16:05:03 -07001207 debugfs_create_atomic_t("stored_pages", S_IRUGO,
1208 zswap_debugfs_root, &zswap_stored_pages);
1209
1210 return 0;
1211}
1212
1213static void __exit zswap_debugfs_exit(void)
1214{
1215 debugfs_remove_recursive(zswap_debugfs_root);
1216}
1217#else
1218static int __init zswap_debugfs_init(void)
1219{
1220 return 0;
1221}
1222
1223static void __exit zswap_debugfs_exit(void) { }
1224#endif
1225
1226/*********************************
1227* module init and exit
1228**********************************/
1229static int __init init_zswap(void)
1230{
Dan Streetmanf1c54842015-09-09 15:35:19 -07001231 struct zswap_pool *pool;
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +01001232 int ret;
Minchan Kim60105e12014-04-07 15:38:27 -07001233
Dan Streetman90b0fc22015-09-09 15:35:21 -07001234 zswap_init_started = true;
1235
Seth Jennings2b281112013-07-10 16:05:03 -07001236 if (zswap_entry_cache_create()) {
1237 pr_err("entry cache creation failed\n");
Dan Streetmanf1c54842015-09-09 15:35:19 -07001238 goto cache_fail;
Seth Jennings2b281112013-07-10 16:05:03 -07001239 }
Dan Streetmanf1c54842015-09-09 15:35:19 -07001240
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +01001241 ret = cpuhp_setup_state(CPUHP_MM_ZSWP_MEM_PREPARE, "mm/zswap:prepare",
1242 zswap_dstmem_prepare, zswap_dstmem_dead);
1243 if (ret) {
Dan Streetmanf1c54842015-09-09 15:35:19 -07001244 pr_err("dstmem alloc failed\n");
1245 goto dstmem_fail;
Seth Jennings2b281112013-07-10 16:05:03 -07001246 }
Dan Streetmanf1c54842015-09-09 15:35:19 -07001247
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +01001248 ret = cpuhp_setup_state_multi(CPUHP_MM_ZSWP_POOL_PREPARE,
1249 "mm/zswap_pool:prepare",
1250 zswap_cpu_comp_prepare,
1251 zswap_cpu_comp_dead);
1252 if (ret)
1253 goto hp_fail;
1254
Dan Streetmanf1c54842015-09-09 15:35:19 -07001255 pool = __zswap_pool_create_fallback();
Dan Streetmanae3d89a2017-02-27 14:26:47 -08001256 if (pool) {
1257 pr_info("loaded using pool %s/%s\n", pool->tfm_name,
1258 zpool_get_type(pool->zpool));
1259 list_add(&pool->list, &zswap_pools);
1260 zswap_has_pool = true;
1261 } else {
Dan Streetmanf1c54842015-09-09 15:35:19 -07001262 pr_err("pool creation failed\n");
Dan Streetmanae3d89a2017-02-27 14:26:47 -08001263 zswap_enabled = false;
Seth Jennings2b281112013-07-10 16:05:03 -07001264 }
Minchan Kim60105e12014-04-07 15:38:27 -07001265
Seth Jennings2b281112013-07-10 16:05:03 -07001266 frontswap_register_ops(&zswap_frontswap_ops);
1267 if (zswap_debugfs_init())
1268 pr_warn("debugfs initialization failed\n");
1269 return 0;
Dan Streetmanf1c54842015-09-09 15:35:19 -07001270
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +01001271hp_fail:
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +01001272 cpuhp_remove_state(CPUHP_MM_ZSWP_MEM_PREPARE);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001273dstmem_fail:
Fabian Frederickc1192392014-08-08 14:19:35 -07001274 zswap_entry_cache_destroy();
Dan Streetmanf1c54842015-09-09 15:35:19 -07001275cache_fail:
Dan Streetmand7b028f2017-02-03 13:13:09 -08001276 /* if built-in, we aren't unloaded on failure; don't allow use */
1277 zswap_init_failed = true;
1278 zswap_enabled = false;
Seth Jennings2b281112013-07-10 16:05:03 -07001279 return -ENOMEM;
1280}
1281/* must be late so crypto has time to come up */
1282late_initcall(init_zswap);
1283
1284MODULE_LICENSE("GPL");
Seth Jennings68386da2014-11-12 21:08:46 -06001285MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
Seth Jennings2b281112013-07-10 16:05:03 -07001286MODULE_DESCRIPTION("Compressed cache for swap pages");