blob: 77cb847938e1c30391b9ddb69f1be5721478f37b [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
79/* Enable/disable zswap (disabled by default) */
80static bool zswap_enabled;
Dan Streetmand7b028f2017-02-03 13:13:09 -080081static int zswap_enabled_param_set(const char *,
82 const struct kernel_param *);
83static struct kernel_param_ops zswap_enabled_param_ops = {
84 .set = zswap_enabled_param_set,
85 .get = param_get_bool,
86};
87module_param_cb(enabled, &zswap_enabled_param_ops, &zswap_enabled, 0644);
Seth Jennings2b281112013-07-10 16:05:03 -070088
Dan Streetman90b0fc22015-09-09 15:35:21 -070089/* Crypto compressor to use */
Seth Jennings2b281112013-07-10 16:05:03 -070090#define ZSWAP_COMPRESSOR_DEFAULT "lzo"
Dan Streetmanc99b42c2015-11-06 16:29:15 -080091static char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
Dan Streetman90b0fc22015-09-09 15:35:21 -070092static int zswap_compressor_param_set(const char *,
93 const struct kernel_param *);
94static struct kernel_param_ops zswap_compressor_param_ops = {
95 .set = zswap_compressor_param_set,
Dan Streetmanc99b42c2015-11-06 16:29:15 -080096 .get = param_get_charp,
97 .free = param_free_charp,
Dan Streetman90b0fc22015-09-09 15:35:21 -070098};
99module_param_cb(compressor, &zswap_compressor_param_ops,
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800100 &zswap_compressor, 0644);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700101
102/* Compressed storage zpool to use */
103#define ZSWAP_ZPOOL_DEFAULT "zbud"
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800104static char *zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700105static int zswap_zpool_param_set(const char *, const struct kernel_param *);
106static struct kernel_param_ops zswap_zpool_param_ops = {
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800107 .set = zswap_zpool_param_set,
108 .get = param_get_charp,
109 .free = param_free_charp,
Dan Streetman90b0fc22015-09-09 15:35:21 -0700110};
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800111module_param_cb(zpool, &zswap_zpool_param_ops, &zswap_zpool_type, 0644);
Seth Jennings2b281112013-07-10 16:05:03 -0700112
113/* The maximum percentage of memory that the compressed pool can occupy */
114static unsigned int zswap_max_pool_percent = 20;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700115module_param_named(max_pool_percent, zswap_max_pool_percent, uint, 0644);
Minchan Kim60105e12014-04-07 15:38:27 -0700116
Seth Jennings2b281112013-07-10 16:05:03 -0700117/*********************************
Seth Jennings2b281112013-07-10 16:05:03 -0700118* data structures
119**********************************/
Dan Streetmanf1c54842015-09-09 15:35:19 -0700120
121struct zswap_pool {
122 struct zpool *zpool;
123 struct crypto_comp * __percpu *tfm;
124 struct kref kref;
125 struct list_head list;
Dan Streetman200867a2016-05-20 16:59:54 -0700126 struct work_struct work;
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100127 struct hlist_node node;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700128 char tfm_name[CRYPTO_MAX_ALG_NAME];
129};
130
Seth Jennings2b281112013-07-10 16:05:03 -0700131/*
132 * struct zswap_entry
133 *
134 * This structure contains the metadata for tracking a single compressed
135 * page within zswap.
136 *
137 * rbnode - links the entry into red-black tree for the appropriate swap type
Dan Streetmanf1c54842015-09-09 15:35:19 -0700138 * offset - the swap offset for the entry. Index into the red-black tree.
Seth Jennings2b281112013-07-10 16:05:03 -0700139 * refcount - the number of outstanding reference to the entry. This is needed
140 * to protect against premature freeing of the entry by code
SeongJae Park6b452512014-04-07 15:38:25 -0700141 * concurrent calls to load, invalidate, and writeback. The lock
Seth Jennings2b281112013-07-10 16:05:03 -0700142 * for the zswap_tree structure that contains the entry must
143 * be held while changing the refcount. Since the lock must
144 * be held, there is no reason to also make refcount atomic.
Seth Jennings2b281112013-07-10 16:05:03 -0700145 * length - the length in bytes of the compressed page data. Needed during
SeongJae Park6b452512014-04-07 15:38:25 -0700146 * decompression
Dan Streetmanf1c54842015-09-09 15:35:19 -0700147 * pool - the zswap_pool the entry's data is in
148 * handle - zpool allocation handle that stores the compressed page data
Seth Jennings2b281112013-07-10 16:05:03 -0700149 */
150struct zswap_entry {
151 struct rb_node rbnode;
152 pgoff_t offset;
153 int refcount;
154 unsigned int length;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700155 struct zswap_pool *pool;
Seth Jennings2b281112013-07-10 16:05:03 -0700156 unsigned long handle;
157};
158
159struct zswap_header {
160 swp_entry_t swpentry;
161};
162
163/*
164 * The tree lock in the zswap_tree struct protects a few things:
165 * - the rbtree
166 * - the refcount field of each entry in the tree
167 */
168struct zswap_tree {
169 struct rb_root rbroot;
170 spinlock_t lock;
Seth Jennings2b281112013-07-10 16:05:03 -0700171};
172
173static struct zswap_tree *zswap_trees[MAX_SWAPFILES];
174
Dan Streetmanf1c54842015-09-09 15:35:19 -0700175/* RCU-protected iteration */
176static LIST_HEAD(zswap_pools);
177/* protects zswap_pools list modification */
178static DEFINE_SPINLOCK(zswap_pools_lock);
Dan Streetman32a4e1692016-05-05 16:22:23 -0700179/* pool counter to provide unique names to zpool */
180static atomic_t zswap_pools_count = ATOMIC_INIT(0);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700181
Dan Streetman90b0fc22015-09-09 15:35:21 -0700182/* used by param callback function */
183static bool zswap_init_started;
184
Dan Streetmand7b028f2017-02-03 13:13:09 -0800185/* fatal error during init */
186static bool zswap_init_failed;
187
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800188/* init completed, but couldn't create the initial pool */
189static bool zswap_has_pool;
190
Dan Streetmanf1c54842015-09-09 15:35:19 -0700191/*********************************
192* helpers and fwd declarations
193**********************************/
194
195#define zswap_pool_debug(msg, p) \
196 pr_debug("%s pool %s/%s\n", msg, (p)->tfm_name, \
197 zpool_get_type((p)->zpool))
198
199static int zswap_writeback_entry(struct zpool *pool, unsigned long handle);
200static int zswap_pool_get(struct zswap_pool *pool);
201static void zswap_pool_put(struct zswap_pool *pool);
202
203static const struct zpool_ops zswap_zpool_ops = {
204 .evict = zswap_writeback_entry
205};
206
207static bool zswap_is_full(void)
208{
209 return totalram_pages * zswap_max_pool_percent / 100 <
210 DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
211}
212
213static void zswap_update_total_size(void)
214{
215 struct zswap_pool *pool;
216 u64 total = 0;
217
218 rcu_read_lock();
219
220 list_for_each_entry_rcu(pool, &zswap_pools, list)
221 total += zpool_get_total_size(pool->zpool);
222
223 rcu_read_unlock();
224
225 zswap_pool_total_size = total;
226}
227
Seth Jennings2b281112013-07-10 16:05:03 -0700228/*********************************
229* zswap entry functions
230**********************************/
231static struct kmem_cache *zswap_entry_cache;
232
Mahendran Ganeshdd01d7d2014-12-12 16:57:15 -0800233static int __init zswap_entry_cache_create(void)
Seth Jennings2b281112013-07-10 16:05:03 -0700234{
235 zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
SeongJae Park5d2d42d2014-04-07 15:38:28 -0700236 return zswap_entry_cache == NULL;
Seth Jennings2b281112013-07-10 16:05:03 -0700237}
238
Fabian Frederickc1192392014-08-08 14:19:35 -0700239static void __init zswap_entry_cache_destroy(void)
Seth Jennings2b281112013-07-10 16:05:03 -0700240{
241 kmem_cache_destroy(zswap_entry_cache);
242}
243
244static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp)
245{
246 struct zswap_entry *entry;
247 entry = kmem_cache_alloc(zswap_entry_cache, gfp);
248 if (!entry)
249 return NULL;
250 entry->refcount = 1;
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800251 RB_CLEAR_NODE(&entry->rbnode);
Seth Jennings2b281112013-07-10 16:05:03 -0700252 return entry;
253}
254
255static void zswap_entry_cache_free(struct zswap_entry *entry)
256{
257 kmem_cache_free(zswap_entry_cache, entry);
258}
259
Seth Jennings2b281112013-07-10 16:05:03 -0700260/*********************************
261* rbtree functions
262**********************************/
263static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset)
264{
265 struct rb_node *node = root->rb_node;
266 struct zswap_entry *entry;
267
268 while (node) {
269 entry = rb_entry(node, struct zswap_entry, rbnode);
270 if (entry->offset > offset)
271 node = node->rb_left;
272 else if (entry->offset < offset)
273 node = node->rb_right;
274 else
275 return entry;
276 }
277 return NULL;
278}
279
280/*
281 * In the case that a entry with the same offset is found, a pointer to
282 * the existing entry is stored in dupentry and the function returns -EEXIST
283 */
284static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry,
285 struct zswap_entry **dupentry)
286{
287 struct rb_node **link = &root->rb_node, *parent = NULL;
288 struct zswap_entry *myentry;
289
290 while (*link) {
291 parent = *link;
292 myentry = rb_entry(parent, struct zswap_entry, rbnode);
293 if (myentry->offset > entry->offset)
294 link = &(*link)->rb_left;
295 else if (myentry->offset < entry->offset)
296 link = &(*link)->rb_right;
297 else {
298 *dupentry = myentry;
299 return -EEXIST;
300 }
301 }
302 rb_link_node(&entry->rbnode, parent, link);
303 rb_insert_color(&entry->rbnode, root);
304 return 0;
305}
306
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800307static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry)
308{
309 if (!RB_EMPTY_NODE(&entry->rbnode)) {
310 rb_erase(&entry->rbnode, root);
311 RB_CLEAR_NODE(&entry->rbnode);
312 }
313}
314
315/*
Dan Streetman12d79d62014-08-06 16:08:40 -0700316 * Carries out the common pattern of freeing and entry's zpool allocation,
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800317 * freeing the entry itself, and decrementing the number of stored pages.
318 */
Minchan Kim60105e12014-04-07 15:38:27 -0700319static void zswap_free_entry(struct zswap_entry *entry)
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800320{
Dan Streetmanf1c54842015-09-09 15:35:19 -0700321 zpool_free(entry->pool->zpool, entry->handle);
322 zswap_pool_put(entry->pool);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800323 zswap_entry_cache_free(entry);
324 atomic_dec(&zswap_stored_pages);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700325 zswap_update_total_size();
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800326}
327
328/* caller must hold the tree lock */
329static void zswap_entry_get(struct zswap_entry *entry)
330{
331 entry->refcount++;
332}
333
334/* caller must hold the tree lock
335* remove from the tree and free it, if nobody reference the entry
336*/
337static void zswap_entry_put(struct zswap_tree *tree,
338 struct zswap_entry *entry)
339{
340 int refcount = --entry->refcount;
341
342 BUG_ON(refcount < 0);
343 if (refcount == 0) {
344 zswap_rb_erase(&tree->rbroot, entry);
Minchan Kim60105e12014-04-07 15:38:27 -0700345 zswap_free_entry(entry);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800346 }
347}
348
349/* caller must hold the tree lock */
350static struct zswap_entry *zswap_entry_find_get(struct rb_root *root,
351 pgoff_t offset)
352{
Alexey Klimovb0c98652015-11-06 16:29:09 -0800353 struct zswap_entry *entry;
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800354
355 entry = zswap_rb_search(root, offset);
356 if (entry)
357 zswap_entry_get(entry);
358
359 return entry;
360}
361
Seth Jennings2b281112013-07-10 16:05:03 -0700362/*********************************
363* per-cpu code
364**********************************/
365static DEFINE_PER_CPU(u8 *, zswap_dstmem);
366
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +0100367static int zswap_dstmem_prepare(unsigned int cpu)
Seth Jennings2b281112013-07-10 16:05:03 -0700368{
Seth Jennings2b281112013-07-10 16:05:03 -0700369 u8 *dst;
370
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +0100371 dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
372 if (!dst) {
373 pr_err("can't allocate compressor buffer\n");
374 return -ENOMEM;
Seth Jennings2b281112013-07-10 16:05:03 -0700375 }
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +0100376 per_cpu(zswap_dstmem, cpu) = dst;
Seth Jennings2b281112013-07-10 16:05:03 -0700377 return 0;
Seth Jennings2b281112013-07-10 16:05:03 -0700378}
379
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +0100380static int zswap_dstmem_dead(unsigned int cpu)
Seth Jennings2b281112013-07-10 16:05:03 -0700381{
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +0100382 u8 *dst;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700383
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +0100384 dst = per_cpu(zswap_dstmem, cpu);
385 kfree(dst);
386 per_cpu(zswap_dstmem, cpu) = NULL;
387
388 return 0;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700389}
390
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100391static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
Dan Streetmanf1c54842015-09-09 15:35:19 -0700392{
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100393 struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700394 struct crypto_comp *tfm;
395
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100396 if (WARN_ON(*per_cpu_ptr(pool->tfm, cpu)))
397 return 0;
398
399 tfm = crypto_alloc_comp(pool->tfm_name, 0, 0);
400 if (IS_ERR_OR_NULL(tfm)) {
401 pr_err("could not alloc crypto comp %s : %ld\n",
402 pool->tfm_name, PTR_ERR(tfm));
403 return -ENOMEM;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700404 }
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100405 *per_cpu_ptr(pool->tfm, cpu) = tfm;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700406 return 0;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700407}
408
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100409static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)
Dan Streetmanf1c54842015-09-09 15:35:19 -0700410{
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100411 struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
412 struct crypto_comp *tfm;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700413
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100414 tfm = *per_cpu_ptr(pool->tfm, cpu);
415 if (!IS_ERR_OR_NULL(tfm))
416 crypto_free_comp(tfm);
417 *per_cpu_ptr(pool->tfm, cpu) = NULL;
418 return 0;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700419}
420
421/*********************************
422* pool functions
423**********************************/
424
425static struct zswap_pool *__zswap_pool_current(void)
426{
427 struct zswap_pool *pool;
428
429 pool = list_first_or_null_rcu(&zswap_pools, typeof(*pool), list);
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800430 WARN_ONCE(!pool && zswap_has_pool,
431 "%s: no page storage pool!\n", __func__);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700432
433 return pool;
434}
435
436static struct zswap_pool *zswap_pool_current(void)
437{
438 assert_spin_locked(&zswap_pools_lock);
439
440 return __zswap_pool_current();
441}
442
443static struct zswap_pool *zswap_pool_current_get(void)
444{
445 struct zswap_pool *pool;
446
447 rcu_read_lock();
448
449 pool = __zswap_pool_current();
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800450 if (!zswap_pool_get(pool))
Dan Streetmanf1c54842015-09-09 15:35:19 -0700451 pool = NULL;
452
453 rcu_read_unlock();
454
455 return pool;
456}
457
458static struct zswap_pool *zswap_pool_last_get(void)
459{
460 struct zswap_pool *pool, *last = NULL;
461
462 rcu_read_lock();
463
464 list_for_each_entry_rcu(pool, &zswap_pools, list)
465 last = pool;
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800466 WARN_ONCE(!last && zswap_has_pool,
467 "%s: no page storage pool!\n", __func__);
468 if (!zswap_pool_get(last))
Dan Streetmanf1c54842015-09-09 15:35:19 -0700469 last = NULL;
470
471 rcu_read_unlock();
472
473 return last;
474}
475
Dan Streetman8bc8b222015-12-18 14:22:04 -0800476/* type and compressor must be null-terminated */
Dan Streetmanf1c54842015-09-09 15:35:19 -0700477static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor)
478{
479 struct zswap_pool *pool;
480
481 assert_spin_locked(&zswap_pools_lock);
482
483 list_for_each_entry_rcu(pool, &zswap_pools, list) {
Dan Streetman8bc8b222015-12-18 14:22:04 -0800484 if (strcmp(pool->tfm_name, compressor))
Dan Streetmanf1c54842015-09-09 15:35:19 -0700485 continue;
Dan Streetman8bc8b222015-12-18 14:22:04 -0800486 if (strcmp(zpool_get_type(pool->zpool), type))
Dan Streetmanf1c54842015-09-09 15:35:19 -0700487 continue;
488 /* if we can't get it, it's about to be destroyed */
489 if (!zswap_pool_get(pool))
490 continue;
491 return pool;
492 }
493
494 return NULL;
495}
496
497static struct zswap_pool *zswap_pool_create(char *type, char *compressor)
498{
499 struct zswap_pool *pool;
Dan Streetman32a4e1692016-05-05 16:22:23 -0700500 char name[38]; /* 'zswap' + 32 char (max) num + \0 */
Mel Gormand0164ad2015-11-06 16:28:21 -0800501 gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100502 int ret;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700503
504 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
505 if (!pool) {
506 pr_err("pool alloc failed\n");
507 return NULL;
508 }
509
Dan Streetman32a4e1692016-05-05 16:22:23 -0700510 /* unique name for each pool specifically required by zsmalloc */
511 snprintf(name, 38, "zswap%x", atomic_inc_return(&zswap_pools_count));
512
513 pool->zpool = zpool_create_pool(type, name, gfp, &zswap_zpool_ops);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700514 if (!pool->zpool) {
515 pr_err("%s zpool not available\n", type);
516 goto error;
517 }
518 pr_debug("using %s zpool\n", zpool_get_type(pool->zpool));
519
520 strlcpy(pool->tfm_name, compressor, sizeof(pool->tfm_name));
521 pool->tfm = alloc_percpu(struct crypto_comp *);
522 if (!pool->tfm) {
523 pr_err("percpu alloc failed\n");
524 goto error;
525 }
526
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100527 ret = cpuhp_state_add_instance(CPUHP_MM_ZSWP_POOL_PREPARE,
528 &pool->node);
529 if (ret)
Dan Streetmanf1c54842015-09-09 15:35:19 -0700530 goto error;
531 pr_debug("using %s compressor\n", pool->tfm_name);
532
533 /* being the current pool takes 1 ref; this func expects the
534 * caller to always add the new pool as the current pool
535 */
536 kref_init(&pool->kref);
537 INIT_LIST_HEAD(&pool->list);
538
539 zswap_pool_debug("created", pool);
540
541 return pool;
542
543error:
544 free_percpu(pool->tfm);
545 if (pool->zpool)
546 zpool_destroy_pool(pool->zpool);
547 kfree(pool);
548 return NULL;
549}
550
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800551static __init struct zswap_pool *__zswap_pool_create_fallback(void)
Dan Streetmanf1c54842015-09-09 15:35:19 -0700552{
553 if (!crypto_has_comp(zswap_compressor, 0, 0)) {
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800554 if (!strcmp(zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT)) {
555 pr_err("default compressor %s not available\n",
556 zswap_compressor);
557 return NULL;
558 }
Dan Streetmanf1c54842015-09-09 15:35:19 -0700559 pr_err("compressor %s not available, using default %s\n",
560 zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT);
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800561 param_free_charp(&zswap_compressor);
562 zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700563 }
564 if (!zpool_has_pool(zswap_zpool_type)) {
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800565 if (!strcmp(zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT)) {
566 pr_err("default zpool %s not available\n",
567 zswap_zpool_type);
568 return NULL;
569 }
Dan Streetmanf1c54842015-09-09 15:35:19 -0700570 pr_err("zpool %s not available, using default %s\n",
571 zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT);
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800572 param_free_charp(&zswap_zpool_type);
573 zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700574 }
575
576 return zswap_pool_create(zswap_zpool_type, zswap_compressor);
577}
578
579static void zswap_pool_destroy(struct zswap_pool *pool)
580{
581 zswap_pool_debug("destroying", pool);
582
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +0100583 cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700584 free_percpu(pool->tfm);
585 zpool_destroy_pool(pool->zpool);
586 kfree(pool);
587}
588
589static int __must_check zswap_pool_get(struct zswap_pool *pool)
590{
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800591 if (!pool)
592 return 0;
593
Dan Streetmanf1c54842015-09-09 15:35:19 -0700594 return kref_get_unless_zero(&pool->kref);
595}
596
Dan Streetman200867a2016-05-20 16:59:54 -0700597static void __zswap_pool_release(struct work_struct *work)
Dan Streetmanf1c54842015-09-09 15:35:19 -0700598{
Dan Streetman200867a2016-05-20 16:59:54 -0700599 struct zswap_pool *pool = container_of(work, typeof(*pool), work);
600
601 synchronize_rcu();
Dan Streetmanf1c54842015-09-09 15:35:19 -0700602
603 /* nobody should have been able to get a kref... */
604 WARN_ON(kref_get_unless_zero(&pool->kref));
605
606 /* pool is now off zswap_pools list and has no references. */
607 zswap_pool_destroy(pool);
608}
609
610static void __zswap_pool_empty(struct kref *kref)
611{
612 struct zswap_pool *pool;
613
614 pool = container_of(kref, typeof(*pool), kref);
615
616 spin_lock(&zswap_pools_lock);
617
618 WARN_ON(pool == zswap_pool_current());
619
620 list_del_rcu(&pool->list);
Dan Streetman200867a2016-05-20 16:59:54 -0700621
622 INIT_WORK(&pool->work, __zswap_pool_release);
623 schedule_work(&pool->work);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700624
625 spin_unlock(&zswap_pools_lock);
626}
627
628static void zswap_pool_put(struct zswap_pool *pool)
629{
630 kref_put(&pool->kref, __zswap_pool_empty);
Seth Jennings2b281112013-07-10 16:05:03 -0700631}
632
Seth Jennings2b281112013-07-10 16:05:03 -0700633/*********************************
Dan Streetman90b0fc22015-09-09 15:35:21 -0700634* param callbacks
635**********************************/
636
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800637/* val must be a null-terminated string */
Dan Streetman90b0fc22015-09-09 15:35:21 -0700638static int __zswap_param_set(const char *val, const struct kernel_param *kp,
639 char *type, char *compressor)
640{
641 struct zswap_pool *pool, *put_pool = NULL;
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800642 char *s = strstrip((char *)val);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700643 int ret;
644
Dan Streetmand7b028f2017-02-03 13:13:09 -0800645 if (zswap_init_failed) {
646 pr_err("can't set param, initialization failed\n");
647 return -ENODEV;
648 }
649
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800650 /* no change required */
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800651 if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool)
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800652 return 0;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700653
654 /* if this is load-time (pre-init) param setting,
655 * don't create a pool; that's done during init.
656 */
657 if (!zswap_init_started)
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800658 return param_set_charp(s, kp);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700659
660 if (!type) {
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800661 if (!zpool_has_pool(s)) {
662 pr_err("zpool %s not available\n", s);
663 return -ENOENT;
664 }
Dan Streetman90b0fc22015-09-09 15:35:21 -0700665 type = s;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700666 } else if (!compressor) {
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800667 if (!crypto_has_comp(s, 0, 0)) {
668 pr_err("compressor %s not available\n", s);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700669 return -ENOENT;
670 }
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800671 compressor = s;
672 } else {
673 WARN_ON(1);
674 return -EINVAL;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700675 }
676
677 spin_lock(&zswap_pools_lock);
678
679 pool = zswap_pool_find_get(type, compressor);
680 if (pool) {
681 zswap_pool_debug("using existing", pool);
682 list_del_rcu(&pool->list);
683 } else {
684 spin_unlock(&zswap_pools_lock);
685 pool = zswap_pool_create(type, compressor);
686 spin_lock(&zswap_pools_lock);
687 }
688
689 if (pool)
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800690 ret = param_set_charp(s, kp);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700691 else
692 ret = -EINVAL;
693
694 if (!ret) {
695 put_pool = zswap_pool_current();
696 list_add_rcu(&pool->list, &zswap_pools);
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800697 zswap_has_pool = true;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700698 } else if (pool) {
699 /* add the possibly pre-existing pool to the end of the pools
700 * list; if it's new (and empty) then it'll be removed and
701 * destroyed by the put after we drop the lock
702 */
703 list_add_tail_rcu(&pool->list, &zswap_pools);
704 put_pool = pool;
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800705 } else if (!zswap_has_pool) {
706 /* if initial pool creation failed, and this pool creation also
707 * failed, maybe both compressor and zpool params were bad.
708 * Allow changing this param, so pool creation will succeed
709 * when the other param is changed. We already verified this
710 * param is ok in the zpool_has_pool() or crypto_has_comp()
711 * checks above.
712 */
713 ret = param_set_charp(s, kp);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700714 }
715
716 spin_unlock(&zswap_pools_lock);
717
718 /* drop the ref from either the old current pool,
719 * or the new pool we failed to add
720 */
721 if (put_pool)
722 zswap_pool_put(put_pool);
723
724 return ret;
725}
726
727static int zswap_compressor_param_set(const char *val,
728 const struct kernel_param *kp)
729{
730 return __zswap_param_set(val, kp, zswap_zpool_type, NULL);
731}
732
733static int zswap_zpool_param_set(const char *val,
734 const struct kernel_param *kp)
735{
736 return __zswap_param_set(val, kp, NULL, zswap_compressor);
737}
738
Dan Streetmand7b028f2017-02-03 13:13:09 -0800739static int zswap_enabled_param_set(const char *val,
740 const struct kernel_param *kp)
741{
742 if (zswap_init_failed) {
743 pr_err("can't enable, initialization failed\n");
744 return -ENODEV;
745 }
Dan Streetmanae3d89a2017-02-27 14:26:47 -0800746 if (!zswap_has_pool && zswap_init_started) {
747 pr_err("can't enable, no pool configured\n");
748 return -ENODEV;
749 }
Dan Streetmand7b028f2017-02-03 13:13:09 -0800750
751 return param_set_bool(val, kp);
752}
753
Dan Streetman90b0fc22015-09-09 15:35:21 -0700754/*********************************
Seth Jennings2b281112013-07-10 16:05:03 -0700755* writeback code
756**********************************/
757/* return enum for zswap_get_swap_cache_page */
758enum zswap_get_swap_ret {
759 ZSWAP_SWAPCACHE_NEW,
760 ZSWAP_SWAPCACHE_EXIST,
Weijie Yang67d13fe2013-11-12 15:08:26 -0800761 ZSWAP_SWAPCACHE_FAIL,
Seth Jennings2b281112013-07-10 16:05:03 -0700762};
763
764/*
765 * zswap_get_swap_cache_page
766 *
767 * This is an adaption of read_swap_cache_async()
768 *
769 * This function tries to find a page with the given swap entry
770 * in the swapper_space address space (the swap cache). If the page
771 * is found, it is returned in retpage. Otherwise, a page is allocated,
772 * added to the swap cache, and returned in retpage.
773 *
774 * If success, the swap cache page is returned in retpage
Weijie Yang67d13fe2013-11-12 15:08:26 -0800775 * Returns ZSWAP_SWAPCACHE_EXIST if page was already in the swap cache
776 * Returns ZSWAP_SWAPCACHE_NEW if the new page needs to be populated,
777 * the new page is added to swapcache and locked
778 * Returns ZSWAP_SWAPCACHE_FAIL on error
Seth Jennings2b281112013-07-10 16:05:03 -0700779 */
780static int zswap_get_swap_cache_page(swp_entry_t entry,
781 struct page **retpage)
782{
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700783 bool page_was_allocated;
Seth Jennings2b281112013-07-10 16:05:03 -0700784
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700785 *retpage = __read_swap_cache_async(entry, GFP_KERNEL,
786 NULL, 0, &page_was_allocated);
787 if (page_was_allocated)
788 return ZSWAP_SWAPCACHE_NEW;
789 if (!*retpage)
Weijie Yang67d13fe2013-11-12 15:08:26 -0800790 return ZSWAP_SWAPCACHE_FAIL;
Seth Jennings2b281112013-07-10 16:05:03 -0700791 return ZSWAP_SWAPCACHE_EXIST;
792}
793
794/*
795 * Attempts to free an entry by adding a page to the swap cache,
796 * decompressing the entry data into the page, and issuing a
797 * bio write to write the page back to the swap device.
798 *
799 * This can be thought of as a "resumed writeback" of the page
800 * to the swap device. We are basically resuming the same swap
801 * writeback path that was intercepted with the frontswap_store()
802 * in the first place. After the page has been decompressed into
803 * the swap cache, the compressed version stored by zswap can be
804 * freed.
805 */
Dan Streetman12d79d62014-08-06 16:08:40 -0700806static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
Seth Jennings2b281112013-07-10 16:05:03 -0700807{
808 struct zswap_header *zhdr;
809 swp_entry_t swpentry;
810 struct zswap_tree *tree;
811 pgoff_t offset;
812 struct zswap_entry *entry;
813 struct page *page;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700814 struct crypto_comp *tfm;
Seth Jennings2b281112013-07-10 16:05:03 -0700815 u8 *src, *dst;
816 unsigned int dlen;
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800817 int ret;
Seth Jennings2b281112013-07-10 16:05:03 -0700818 struct writeback_control wbc = {
819 .sync_mode = WB_SYNC_NONE,
820 };
821
822 /* extract swpentry from data */
Dan Streetman12d79d62014-08-06 16:08:40 -0700823 zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO);
Seth Jennings2b281112013-07-10 16:05:03 -0700824 swpentry = zhdr->swpentry; /* here */
Dan Streetman12d79d62014-08-06 16:08:40 -0700825 zpool_unmap_handle(pool, handle);
Seth Jennings2b281112013-07-10 16:05:03 -0700826 tree = zswap_trees[swp_type(swpentry)];
827 offset = swp_offset(swpentry);
Seth Jennings2b281112013-07-10 16:05:03 -0700828
829 /* find and ref zswap entry */
830 spin_lock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800831 entry = zswap_entry_find_get(&tree->rbroot, offset);
Seth Jennings2b281112013-07-10 16:05:03 -0700832 if (!entry) {
833 /* entry was invalidated */
834 spin_unlock(&tree->lock);
835 return 0;
836 }
Seth Jennings2b281112013-07-10 16:05:03 -0700837 spin_unlock(&tree->lock);
838 BUG_ON(offset != entry->offset);
839
840 /* try to allocate swap cache page */
841 switch (zswap_get_swap_cache_page(swpentry, &page)) {
Weijie Yang67d13fe2013-11-12 15:08:26 -0800842 case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */
Seth Jennings2b281112013-07-10 16:05:03 -0700843 ret = -ENOMEM;
844 goto fail;
845
Weijie Yang67d13fe2013-11-12 15:08:26 -0800846 case ZSWAP_SWAPCACHE_EXIST:
Seth Jennings2b281112013-07-10 16:05:03 -0700847 /* page is already in the swap cache, ignore for now */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300848 put_page(page);
Seth Jennings2b281112013-07-10 16:05:03 -0700849 ret = -EEXIST;
850 goto fail;
851
852 case ZSWAP_SWAPCACHE_NEW: /* page is locked */
853 /* decompress */
854 dlen = PAGE_SIZE;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700855 src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle,
Dan Streetman12d79d62014-08-06 16:08:40 -0700856 ZPOOL_MM_RO) + sizeof(struct zswap_header);
Seth Jennings2b281112013-07-10 16:05:03 -0700857 dst = kmap_atomic(page);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700858 tfm = *get_cpu_ptr(entry->pool->tfm);
859 ret = crypto_comp_decompress(tfm, src, entry->length,
860 dst, &dlen);
861 put_cpu_ptr(entry->pool->tfm);
Seth Jennings2b281112013-07-10 16:05:03 -0700862 kunmap_atomic(dst);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700863 zpool_unmap_handle(entry->pool->zpool, entry->handle);
Seth Jennings2b281112013-07-10 16:05:03 -0700864 BUG_ON(ret);
865 BUG_ON(dlen != PAGE_SIZE);
866
867 /* page is up to date */
868 SetPageUptodate(page);
869 }
870
Weijie Yangb349acc2013-11-12 15:07:52 -0800871 /* move it to the tail of the inactive list after end_writeback */
872 SetPageReclaim(page);
873
Seth Jennings2b281112013-07-10 16:05:03 -0700874 /* start writeback */
875 __swap_writepage(page, &wbc, end_swap_bio_write);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300876 put_page(page);
Seth Jennings2b281112013-07-10 16:05:03 -0700877 zswap_written_back_pages++;
878
879 spin_lock(&tree->lock);
Seth Jennings2b281112013-07-10 16:05:03 -0700880 /* drop local reference */
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800881 zswap_entry_put(tree, entry);
Seth Jennings2b281112013-07-10 16:05:03 -0700882
883 /*
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800884 * There are two possible situations for entry here:
885 * (1) refcount is 1(normal case), entry is valid and on the tree
886 * (2) refcount is 0, entry is freed and not on the tree
887 * because invalidate happened during writeback
888 * search the tree and free the entry if find entry
889 */
890 if (entry == zswap_rb_search(&tree->rbroot, offset))
891 zswap_entry_put(tree, entry);
Seth Jennings2b281112013-07-10 16:05:03 -0700892 spin_unlock(&tree->lock);
Seth Jennings2b281112013-07-10 16:05:03 -0700893
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800894 goto end;
895
896 /*
897 * if we get here due to ZSWAP_SWAPCACHE_EXIST
898 * a load may happening concurrently
899 * it is safe and okay to not free the entry
900 * if we free the entry in the following put
901 * it it either okay to return !0
902 */
Seth Jennings2b281112013-07-10 16:05:03 -0700903fail:
904 spin_lock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800905 zswap_entry_put(tree, entry);
Seth Jennings2b281112013-07-10 16:05:03 -0700906 spin_unlock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800907
908end:
Seth Jennings2b281112013-07-10 16:05:03 -0700909 return ret;
910}
911
Dan Streetmanf1c54842015-09-09 15:35:19 -0700912static int zswap_shrink(void)
913{
914 struct zswap_pool *pool;
915 int ret;
916
917 pool = zswap_pool_last_get();
918 if (!pool)
919 return -ENOENT;
920
921 ret = zpool_shrink(pool->zpool, 1, NULL);
922
923 zswap_pool_put(pool);
924
925 return ret;
926}
927
Seth Jennings2b281112013-07-10 16:05:03 -0700928/*********************************
929* frontswap hooks
930**********************************/
931/* attempts to compress and store an single page */
932static int zswap_frontswap_store(unsigned type, pgoff_t offset,
933 struct page *page)
934{
935 struct zswap_tree *tree = zswap_trees[type];
936 struct zswap_entry *entry, *dupentry;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700937 struct crypto_comp *tfm;
Seth Jennings2b281112013-07-10 16:05:03 -0700938 int ret;
939 unsigned int dlen = PAGE_SIZE, len;
940 unsigned long handle;
941 char *buf;
942 u8 *src, *dst;
943 struct zswap_header *zhdr;
944
Dan Streetmanc00ed162015-06-25 15:00:35 -0700945 if (!zswap_enabled || !tree) {
Seth Jennings2b281112013-07-10 16:05:03 -0700946 ret = -ENODEV;
947 goto reject;
948 }
949
950 /* reclaim space if needed */
951 if (zswap_is_full()) {
952 zswap_pool_limit_hit++;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700953 if (zswap_shrink()) {
Seth Jennings2b281112013-07-10 16:05:03 -0700954 zswap_reject_reclaim_fail++;
955 ret = -ENOMEM;
956 goto reject;
957 }
958 }
959
960 /* allocate entry */
961 entry = zswap_entry_cache_alloc(GFP_KERNEL);
962 if (!entry) {
963 zswap_reject_kmemcache_fail++;
964 ret = -ENOMEM;
965 goto reject;
966 }
967
Dan Streetmanf1c54842015-09-09 15:35:19 -0700968 /* if entry is successfully added, it keeps the reference */
969 entry->pool = zswap_pool_current_get();
970 if (!entry->pool) {
Seth Jennings2b281112013-07-10 16:05:03 -0700971 ret = -EINVAL;
972 goto freepage;
973 }
974
Dan Streetmanf1c54842015-09-09 15:35:19 -0700975 /* compress */
976 dst = get_cpu_var(zswap_dstmem);
977 tfm = *get_cpu_ptr(entry->pool->tfm);
978 src = kmap_atomic(page);
979 ret = crypto_comp_compress(tfm, src, PAGE_SIZE, dst, &dlen);
980 kunmap_atomic(src);
981 put_cpu_ptr(entry->pool->tfm);
982 if (ret) {
983 ret = -EINVAL;
984 goto put_dstmem;
985 }
986
Seth Jennings2b281112013-07-10 16:05:03 -0700987 /* store */
988 len = dlen + sizeof(struct zswap_header);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700989 ret = zpool_malloc(entry->pool->zpool, len,
Mel Gormand0164ad2015-11-06 16:28:21 -0800990 __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM,
991 &handle);
Seth Jennings2b281112013-07-10 16:05:03 -0700992 if (ret == -ENOSPC) {
993 zswap_reject_compress_poor++;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700994 goto put_dstmem;
Seth Jennings2b281112013-07-10 16:05:03 -0700995 }
996 if (ret) {
997 zswap_reject_alloc_fail++;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700998 goto put_dstmem;
Seth Jennings2b281112013-07-10 16:05:03 -0700999 }
Dan Streetmanf1c54842015-09-09 15:35:19 -07001000 zhdr = zpool_map_handle(entry->pool->zpool, handle, ZPOOL_MM_RW);
Seth Jennings2b281112013-07-10 16:05:03 -07001001 zhdr->swpentry = swp_entry(type, offset);
1002 buf = (u8 *)(zhdr + 1);
1003 memcpy(buf, dst, dlen);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001004 zpool_unmap_handle(entry->pool->zpool, handle);
Seth Jennings2b281112013-07-10 16:05:03 -07001005 put_cpu_var(zswap_dstmem);
1006
1007 /* populate entry */
1008 entry->offset = offset;
1009 entry->handle = handle;
1010 entry->length = dlen;
1011
1012 /* map */
1013 spin_lock(&tree->lock);
1014 do {
1015 ret = zswap_rb_insert(&tree->rbroot, entry, &dupentry);
1016 if (ret == -EEXIST) {
1017 zswap_duplicate_entry++;
1018 /* remove from rbtree */
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001019 zswap_rb_erase(&tree->rbroot, dupentry);
1020 zswap_entry_put(tree, dupentry);
Seth Jennings2b281112013-07-10 16:05:03 -07001021 }
1022 } while (ret == -EEXIST);
1023 spin_unlock(&tree->lock);
1024
1025 /* update stats */
1026 atomic_inc(&zswap_stored_pages);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001027 zswap_update_total_size();
Seth Jennings2b281112013-07-10 16:05:03 -07001028
1029 return 0;
1030
Dan Streetmanf1c54842015-09-09 15:35:19 -07001031put_dstmem:
Seth Jennings2b281112013-07-10 16:05:03 -07001032 put_cpu_var(zswap_dstmem);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001033 zswap_pool_put(entry->pool);
1034freepage:
Seth Jennings2b281112013-07-10 16:05:03 -07001035 zswap_entry_cache_free(entry);
1036reject:
1037 return ret;
1038}
1039
1040/*
1041 * returns 0 if the page was successfully decompressed
1042 * return -1 on entry not found or error
1043*/
1044static int zswap_frontswap_load(unsigned type, pgoff_t offset,
1045 struct page *page)
1046{
1047 struct zswap_tree *tree = zswap_trees[type];
1048 struct zswap_entry *entry;
Dan Streetmanf1c54842015-09-09 15:35:19 -07001049 struct crypto_comp *tfm;
Seth Jennings2b281112013-07-10 16:05:03 -07001050 u8 *src, *dst;
1051 unsigned int dlen;
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001052 int ret;
Seth Jennings2b281112013-07-10 16:05:03 -07001053
1054 /* find */
1055 spin_lock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001056 entry = zswap_entry_find_get(&tree->rbroot, offset);
Seth Jennings2b281112013-07-10 16:05:03 -07001057 if (!entry) {
1058 /* entry was written back */
1059 spin_unlock(&tree->lock);
1060 return -1;
1061 }
Seth Jennings2b281112013-07-10 16:05:03 -07001062 spin_unlock(&tree->lock);
1063
1064 /* decompress */
1065 dlen = PAGE_SIZE;
Dan Streetmanf1c54842015-09-09 15:35:19 -07001066 src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle,
Dan Streetman12d79d62014-08-06 16:08:40 -07001067 ZPOOL_MM_RO) + sizeof(struct zswap_header);
Seth Jennings2b281112013-07-10 16:05:03 -07001068 dst = kmap_atomic(page);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001069 tfm = *get_cpu_ptr(entry->pool->tfm);
1070 ret = crypto_comp_decompress(tfm, src, entry->length, dst, &dlen);
1071 put_cpu_ptr(entry->pool->tfm);
Seth Jennings2b281112013-07-10 16:05:03 -07001072 kunmap_atomic(dst);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001073 zpool_unmap_handle(entry->pool->zpool, entry->handle);
Seth Jennings2b281112013-07-10 16:05:03 -07001074 BUG_ON(ret);
1075
1076 spin_lock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001077 zswap_entry_put(tree, entry);
Seth Jennings2b281112013-07-10 16:05:03 -07001078 spin_unlock(&tree->lock);
1079
Seth Jennings2b281112013-07-10 16:05:03 -07001080 return 0;
1081}
1082
1083/* frees an entry in zswap */
1084static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset)
1085{
1086 struct zswap_tree *tree = zswap_trees[type];
1087 struct zswap_entry *entry;
Seth Jennings2b281112013-07-10 16:05:03 -07001088
1089 /* find */
1090 spin_lock(&tree->lock);
1091 entry = zswap_rb_search(&tree->rbroot, offset);
1092 if (!entry) {
1093 /* entry was written back */
1094 spin_unlock(&tree->lock);
1095 return;
1096 }
1097
1098 /* remove from rbtree */
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001099 zswap_rb_erase(&tree->rbroot, entry);
Seth Jennings2b281112013-07-10 16:05:03 -07001100
1101 /* drop the initial reference from entry creation */
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001102 zswap_entry_put(tree, entry);
Seth Jennings2b281112013-07-10 16:05:03 -07001103
1104 spin_unlock(&tree->lock);
Seth Jennings2b281112013-07-10 16:05:03 -07001105}
1106
1107/* frees all zswap entries for the given swap type */
1108static void zswap_frontswap_invalidate_area(unsigned type)
1109{
1110 struct zswap_tree *tree = zswap_trees[type];
Cody P Schafer0bd42132013-09-11 14:25:33 -07001111 struct zswap_entry *entry, *n;
Seth Jennings2b281112013-07-10 16:05:03 -07001112
1113 if (!tree)
1114 return;
1115
1116 /* walk the tree and free everything */
1117 spin_lock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001118 rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode)
Minchan Kim60105e12014-04-07 15:38:27 -07001119 zswap_free_entry(entry);
Seth Jennings2b281112013-07-10 16:05:03 -07001120 tree->rbroot = RB_ROOT;
1121 spin_unlock(&tree->lock);
Weijie Yangaa9bca02013-10-16 13:46:54 -07001122 kfree(tree);
1123 zswap_trees[type] = NULL;
Seth Jennings2b281112013-07-10 16:05:03 -07001124}
1125
Seth Jennings2b281112013-07-10 16:05:03 -07001126static void zswap_frontswap_init(unsigned type)
1127{
1128 struct zswap_tree *tree;
1129
1130 tree = kzalloc(sizeof(struct zswap_tree), GFP_KERNEL);
Minchan Kim60105e12014-04-07 15:38:27 -07001131 if (!tree) {
1132 pr_err("alloc failed, zswap disabled for swap type %d\n", type);
1133 return;
1134 }
1135
Seth Jennings2b281112013-07-10 16:05:03 -07001136 tree->rbroot = RB_ROOT;
1137 spin_lock_init(&tree->lock);
1138 zswap_trees[type] = tree;
Seth Jennings2b281112013-07-10 16:05:03 -07001139}
1140
1141static struct frontswap_ops zswap_frontswap_ops = {
1142 .store = zswap_frontswap_store,
1143 .load = zswap_frontswap_load,
1144 .invalidate_page = zswap_frontswap_invalidate_page,
1145 .invalidate_area = zswap_frontswap_invalidate_area,
1146 .init = zswap_frontswap_init
1147};
1148
1149/*********************************
1150* debugfs functions
1151**********************************/
1152#ifdef CONFIG_DEBUG_FS
1153#include <linux/debugfs.h>
1154
1155static struct dentry *zswap_debugfs_root;
1156
1157static int __init zswap_debugfs_init(void)
1158{
1159 if (!debugfs_initialized())
1160 return -ENODEV;
1161
1162 zswap_debugfs_root = debugfs_create_dir("zswap", NULL);
1163 if (!zswap_debugfs_root)
1164 return -ENOMEM;
1165
1166 debugfs_create_u64("pool_limit_hit", S_IRUGO,
1167 zswap_debugfs_root, &zswap_pool_limit_hit);
1168 debugfs_create_u64("reject_reclaim_fail", S_IRUGO,
1169 zswap_debugfs_root, &zswap_reject_reclaim_fail);
1170 debugfs_create_u64("reject_alloc_fail", S_IRUGO,
1171 zswap_debugfs_root, &zswap_reject_alloc_fail);
1172 debugfs_create_u64("reject_kmemcache_fail", S_IRUGO,
1173 zswap_debugfs_root, &zswap_reject_kmemcache_fail);
1174 debugfs_create_u64("reject_compress_poor", S_IRUGO,
1175 zswap_debugfs_root, &zswap_reject_compress_poor);
1176 debugfs_create_u64("written_back_pages", S_IRUGO,
1177 zswap_debugfs_root, &zswap_written_back_pages);
1178 debugfs_create_u64("duplicate_entry", S_IRUGO,
1179 zswap_debugfs_root, &zswap_duplicate_entry);
Dan Streetman12d79d62014-08-06 16:08:40 -07001180 debugfs_create_u64("pool_total_size", S_IRUGO,
1181 zswap_debugfs_root, &zswap_pool_total_size);
Seth Jennings2b281112013-07-10 16:05:03 -07001182 debugfs_create_atomic_t("stored_pages", S_IRUGO,
1183 zswap_debugfs_root, &zswap_stored_pages);
1184
1185 return 0;
1186}
1187
1188static void __exit zswap_debugfs_exit(void)
1189{
1190 debugfs_remove_recursive(zswap_debugfs_root);
1191}
1192#else
1193static int __init zswap_debugfs_init(void)
1194{
1195 return 0;
1196}
1197
1198static void __exit zswap_debugfs_exit(void) { }
1199#endif
1200
1201/*********************************
1202* module init and exit
1203**********************************/
1204static int __init init_zswap(void)
1205{
Dan Streetmanf1c54842015-09-09 15:35:19 -07001206 struct zswap_pool *pool;
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +01001207 int ret;
Minchan Kim60105e12014-04-07 15:38:27 -07001208
Dan Streetman90b0fc22015-09-09 15:35:21 -07001209 zswap_init_started = true;
1210
Seth Jennings2b281112013-07-10 16:05:03 -07001211 if (zswap_entry_cache_create()) {
1212 pr_err("entry cache creation failed\n");
Dan Streetmanf1c54842015-09-09 15:35:19 -07001213 goto cache_fail;
Seth Jennings2b281112013-07-10 16:05:03 -07001214 }
Dan Streetmanf1c54842015-09-09 15:35:19 -07001215
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +01001216 ret = cpuhp_setup_state(CPUHP_MM_ZSWP_MEM_PREPARE, "mm/zswap:prepare",
1217 zswap_dstmem_prepare, zswap_dstmem_dead);
1218 if (ret) {
Dan Streetmanf1c54842015-09-09 15:35:19 -07001219 pr_err("dstmem alloc failed\n");
1220 goto dstmem_fail;
Seth Jennings2b281112013-07-10 16:05:03 -07001221 }
Dan Streetmanf1c54842015-09-09 15:35:19 -07001222
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +01001223 ret = cpuhp_setup_state_multi(CPUHP_MM_ZSWP_POOL_PREPARE,
1224 "mm/zswap_pool:prepare",
1225 zswap_cpu_comp_prepare,
1226 zswap_cpu_comp_dead);
1227 if (ret)
1228 goto hp_fail;
1229
Dan Streetmanf1c54842015-09-09 15:35:19 -07001230 pool = __zswap_pool_create_fallback();
Dan Streetmanae3d89a2017-02-27 14:26:47 -08001231 if (pool) {
1232 pr_info("loaded using pool %s/%s\n", pool->tfm_name,
1233 zpool_get_type(pool->zpool));
1234 list_add(&pool->list, &zswap_pools);
1235 zswap_has_pool = true;
1236 } else {
Dan Streetmanf1c54842015-09-09 15:35:19 -07001237 pr_err("pool creation failed\n");
Dan Streetmanae3d89a2017-02-27 14:26:47 -08001238 zswap_enabled = false;
Seth Jennings2b281112013-07-10 16:05:03 -07001239 }
Minchan Kim60105e12014-04-07 15:38:27 -07001240
Seth Jennings2b281112013-07-10 16:05:03 -07001241 frontswap_register_ops(&zswap_frontswap_ops);
1242 if (zswap_debugfs_init())
1243 pr_warn("debugfs initialization failed\n");
1244 return 0;
Dan Streetmanf1c54842015-09-09 15:35:19 -07001245
Sebastian Andrzej Siewiorcab7a7e2016-11-27 00:13:40 +01001246hp_fail:
Sebastian Andrzej Siewiorad7ed772016-11-27 00:13:39 +01001247 cpuhp_remove_state(CPUHP_MM_ZSWP_MEM_PREPARE);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001248dstmem_fail:
Fabian Frederickc1192392014-08-08 14:19:35 -07001249 zswap_entry_cache_destroy();
Dan Streetmanf1c54842015-09-09 15:35:19 -07001250cache_fail:
Dan Streetmand7b028f2017-02-03 13:13:09 -08001251 /* if built-in, we aren't unloaded on failure; don't allow use */
1252 zswap_init_failed = true;
1253 zswap_enabled = false;
Seth Jennings2b281112013-07-10 16:05:03 -07001254 return -ENOMEM;
1255}
1256/* must be late so crypto has time to come up */
1257late_initcall(init_zswap);
1258
1259MODULE_LICENSE("GPL");
Seth Jennings68386da2014-11-12 21:08:46 -06001260MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
Seth Jennings2b281112013-07-10 16:05:03 -07001261MODULE_DESCRIPTION("Compressed cache for swap pages");