blob: 91dad80d068b75de629105ec0e688ce16335e16f [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;
81module_param_named(enabled, zswap_enabled, bool, 0644);
Seth Jennings2b281112013-07-10 16:05:03 -070082
Dan Streetman90b0fc22015-09-09 15:35:21 -070083/* Crypto compressor to use */
Seth Jennings2b281112013-07-10 16:05:03 -070084#define ZSWAP_COMPRESSOR_DEFAULT "lzo"
Dan Streetmanc99b42c2015-11-06 16:29:15 -080085static char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
Dan Streetman90b0fc22015-09-09 15:35:21 -070086static int zswap_compressor_param_set(const char *,
87 const struct kernel_param *);
88static struct kernel_param_ops zswap_compressor_param_ops = {
89 .set = zswap_compressor_param_set,
Dan Streetmanc99b42c2015-11-06 16:29:15 -080090 .get = param_get_charp,
91 .free = param_free_charp,
Dan Streetman90b0fc22015-09-09 15:35:21 -070092};
93module_param_cb(compressor, &zswap_compressor_param_ops,
Dan Streetmanc99b42c2015-11-06 16:29:15 -080094 &zswap_compressor, 0644);
Dan Streetman90b0fc22015-09-09 15:35:21 -070095
96/* Compressed storage zpool to use */
97#define ZSWAP_ZPOOL_DEFAULT "zbud"
Dan Streetmanc99b42c2015-11-06 16:29:15 -080098static char *zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
Dan Streetman90b0fc22015-09-09 15:35:21 -070099static int zswap_zpool_param_set(const char *, const struct kernel_param *);
100static struct kernel_param_ops zswap_zpool_param_ops = {
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800101 .set = zswap_zpool_param_set,
102 .get = param_get_charp,
103 .free = param_free_charp,
Dan Streetman90b0fc22015-09-09 15:35:21 -0700104};
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800105module_param_cb(zpool, &zswap_zpool_param_ops, &zswap_zpool_type, 0644);
Seth Jennings2b281112013-07-10 16:05:03 -0700106
107/* The maximum percentage of memory that the compressed pool can occupy */
108static unsigned int zswap_max_pool_percent = 20;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700109module_param_named(max_pool_percent, zswap_max_pool_percent, uint, 0644);
Minchan Kim60105e12014-04-07 15:38:27 -0700110
Seth Jennings2b281112013-07-10 16:05:03 -0700111/*********************************
Seth Jennings2b281112013-07-10 16:05:03 -0700112* data structures
113**********************************/
Dan Streetmanf1c54842015-09-09 15:35:19 -0700114
115struct zswap_pool {
116 struct zpool *zpool;
117 struct crypto_comp * __percpu *tfm;
118 struct kref kref;
119 struct list_head list;
120 struct rcu_head rcu_head;
121 struct notifier_block notifier;
122 char tfm_name[CRYPTO_MAX_ALG_NAME];
123};
124
Seth Jennings2b281112013-07-10 16:05:03 -0700125/*
126 * struct zswap_entry
127 *
128 * This structure contains the metadata for tracking a single compressed
129 * page within zswap.
130 *
131 * rbnode - links the entry into red-black tree for the appropriate swap type
Dan Streetmanf1c54842015-09-09 15:35:19 -0700132 * offset - the swap offset for the entry. Index into the red-black tree.
Seth Jennings2b281112013-07-10 16:05:03 -0700133 * refcount - the number of outstanding reference to the entry. This is needed
134 * to protect against premature freeing of the entry by code
SeongJae Park6b452512014-04-07 15:38:25 -0700135 * concurrent calls to load, invalidate, and writeback. The lock
Seth Jennings2b281112013-07-10 16:05:03 -0700136 * for the zswap_tree structure that contains the entry must
137 * be held while changing the refcount. Since the lock must
138 * be held, there is no reason to also make refcount atomic.
Seth Jennings2b281112013-07-10 16:05:03 -0700139 * length - the length in bytes of the compressed page data. Needed during
SeongJae Park6b452512014-04-07 15:38:25 -0700140 * decompression
Dan Streetmanf1c54842015-09-09 15:35:19 -0700141 * pool - the zswap_pool the entry's data is in
142 * handle - zpool allocation handle that stores the compressed page data
Seth Jennings2b281112013-07-10 16:05:03 -0700143 */
144struct zswap_entry {
145 struct rb_node rbnode;
146 pgoff_t offset;
147 int refcount;
148 unsigned int length;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700149 struct zswap_pool *pool;
Seth Jennings2b281112013-07-10 16:05:03 -0700150 unsigned long handle;
151};
152
153struct zswap_header {
154 swp_entry_t swpentry;
155};
156
157/*
158 * The tree lock in the zswap_tree struct protects a few things:
159 * - the rbtree
160 * - the refcount field of each entry in the tree
161 */
162struct zswap_tree {
163 struct rb_root rbroot;
164 spinlock_t lock;
Seth Jennings2b281112013-07-10 16:05:03 -0700165};
166
167static struct zswap_tree *zswap_trees[MAX_SWAPFILES];
168
Dan Streetmanf1c54842015-09-09 15:35:19 -0700169/* RCU-protected iteration */
170static LIST_HEAD(zswap_pools);
171/* protects zswap_pools list modification */
172static DEFINE_SPINLOCK(zswap_pools_lock);
173
Dan Streetman90b0fc22015-09-09 15:35:21 -0700174/* used by param callback function */
175static bool zswap_init_started;
176
Dan Streetmanf1c54842015-09-09 15:35:19 -0700177/*********************************
178* helpers and fwd declarations
179**********************************/
180
181#define zswap_pool_debug(msg, p) \
182 pr_debug("%s pool %s/%s\n", msg, (p)->tfm_name, \
183 zpool_get_type((p)->zpool))
184
185static int zswap_writeback_entry(struct zpool *pool, unsigned long handle);
186static int zswap_pool_get(struct zswap_pool *pool);
187static void zswap_pool_put(struct zswap_pool *pool);
188
189static const struct zpool_ops zswap_zpool_ops = {
190 .evict = zswap_writeback_entry
191};
192
193static bool zswap_is_full(void)
194{
195 return totalram_pages * zswap_max_pool_percent / 100 <
196 DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
197}
198
199static void zswap_update_total_size(void)
200{
201 struct zswap_pool *pool;
202 u64 total = 0;
203
204 rcu_read_lock();
205
206 list_for_each_entry_rcu(pool, &zswap_pools, list)
207 total += zpool_get_total_size(pool->zpool);
208
209 rcu_read_unlock();
210
211 zswap_pool_total_size = total;
212}
213
Seth Jennings2b281112013-07-10 16:05:03 -0700214/*********************************
215* zswap entry functions
216**********************************/
217static struct kmem_cache *zswap_entry_cache;
218
Mahendran Ganeshdd01d7d2014-12-12 16:57:15 -0800219static int __init zswap_entry_cache_create(void)
Seth Jennings2b281112013-07-10 16:05:03 -0700220{
221 zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
SeongJae Park5d2d42d2014-04-07 15:38:28 -0700222 return zswap_entry_cache == NULL;
Seth Jennings2b281112013-07-10 16:05:03 -0700223}
224
Fabian Frederickc1192392014-08-08 14:19:35 -0700225static void __init zswap_entry_cache_destroy(void)
Seth Jennings2b281112013-07-10 16:05:03 -0700226{
227 kmem_cache_destroy(zswap_entry_cache);
228}
229
230static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp)
231{
232 struct zswap_entry *entry;
233 entry = kmem_cache_alloc(zswap_entry_cache, gfp);
234 if (!entry)
235 return NULL;
236 entry->refcount = 1;
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800237 RB_CLEAR_NODE(&entry->rbnode);
Seth Jennings2b281112013-07-10 16:05:03 -0700238 return entry;
239}
240
241static void zswap_entry_cache_free(struct zswap_entry *entry)
242{
243 kmem_cache_free(zswap_entry_cache, entry);
244}
245
Seth Jennings2b281112013-07-10 16:05:03 -0700246/*********************************
247* rbtree functions
248**********************************/
249static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset)
250{
251 struct rb_node *node = root->rb_node;
252 struct zswap_entry *entry;
253
254 while (node) {
255 entry = rb_entry(node, struct zswap_entry, rbnode);
256 if (entry->offset > offset)
257 node = node->rb_left;
258 else if (entry->offset < offset)
259 node = node->rb_right;
260 else
261 return entry;
262 }
263 return NULL;
264}
265
266/*
267 * In the case that a entry with the same offset is found, a pointer to
268 * the existing entry is stored in dupentry and the function returns -EEXIST
269 */
270static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry,
271 struct zswap_entry **dupentry)
272{
273 struct rb_node **link = &root->rb_node, *parent = NULL;
274 struct zswap_entry *myentry;
275
276 while (*link) {
277 parent = *link;
278 myentry = rb_entry(parent, struct zswap_entry, rbnode);
279 if (myentry->offset > entry->offset)
280 link = &(*link)->rb_left;
281 else if (myentry->offset < entry->offset)
282 link = &(*link)->rb_right;
283 else {
284 *dupentry = myentry;
285 return -EEXIST;
286 }
287 }
288 rb_link_node(&entry->rbnode, parent, link);
289 rb_insert_color(&entry->rbnode, root);
290 return 0;
291}
292
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800293static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry)
294{
295 if (!RB_EMPTY_NODE(&entry->rbnode)) {
296 rb_erase(&entry->rbnode, root);
297 RB_CLEAR_NODE(&entry->rbnode);
298 }
299}
300
301/*
Dan Streetman12d79d62014-08-06 16:08:40 -0700302 * Carries out the common pattern of freeing and entry's zpool allocation,
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800303 * freeing the entry itself, and decrementing the number of stored pages.
304 */
Minchan Kim60105e12014-04-07 15:38:27 -0700305static void zswap_free_entry(struct zswap_entry *entry)
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800306{
Dan Streetmanf1c54842015-09-09 15:35:19 -0700307 zpool_free(entry->pool->zpool, entry->handle);
308 zswap_pool_put(entry->pool);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800309 zswap_entry_cache_free(entry);
310 atomic_dec(&zswap_stored_pages);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700311 zswap_update_total_size();
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800312}
313
314/* caller must hold the tree lock */
315static void zswap_entry_get(struct zswap_entry *entry)
316{
317 entry->refcount++;
318}
319
320/* caller must hold the tree lock
321* remove from the tree and free it, if nobody reference the entry
322*/
323static void zswap_entry_put(struct zswap_tree *tree,
324 struct zswap_entry *entry)
325{
326 int refcount = --entry->refcount;
327
328 BUG_ON(refcount < 0);
329 if (refcount == 0) {
330 zswap_rb_erase(&tree->rbroot, entry);
Minchan Kim60105e12014-04-07 15:38:27 -0700331 zswap_free_entry(entry);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800332 }
333}
334
335/* caller must hold the tree lock */
336static struct zswap_entry *zswap_entry_find_get(struct rb_root *root,
337 pgoff_t offset)
338{
Alexey Klimovb0c98652015-11-06 16:29:09 -0800339 struct zswap_entry *entry;
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800340
341 entry = zswap_rb_search(root, offset);
342 if (entry)
343 zswap_entry_get(entry);
344
345 return entry;
346}
347
Seth Jennings2b281112013-07-10 16:05:03 -0700348/*********************************
349* per-cpu code
350**********************************/
351static DEFINE_PER_CPU(u8 *, zswap_dstmem);
352
Dan Streetmanf1c54842015-09-09 15:35:19 -0700353static int __zswap_cpu_dstmem_notifier(unsigned long action, unsigned long cpu)
Seth Jennings2b281112013-07-10 16:05:03 -0700354{
Seth Jennings2b281112013-07-10 16:05:03 -0700355 u8 *dst;
356
357 switch (action) {
358 case CPU_UP_PREPARE:
Eric Dumazet72d09632014-06-04 16:11:11 -0700359 dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
Seth Jennings2b281112013-07-10 16:05:03 -0700360 if (!dst) {
361 pr_err("can't allocate compressor buffer\n");
Seth Jennings2b281112013-07-10 16:05:03 -0700362 return NOTIFY_BAD;
363 }
364 per_cpu(zswap_dstmem, cpu) = dst;
365 break;
366 case CPU_DEAD:
367 case CPU_UP_CANCELED:
Seth Jennings2b281112013-07-10 16:05:03 -0700368 dst = per_cpu(zswap_dstmem, cpu);
369 kfree(dst);
370 per_cpu(zswap_dstmem, cpu) = NULL;
371 break;
372 default:
373 break;
374 }
375 return NOTIFY_OK;
376}
377
Dan Streetmanf1c54842015-09-09 15:35:19 -0700378static int zswap_cpu_dstmem_notifier(struct notifier_block *nb,
379 unsigned long action, void *pcpu)
Seth Jennings2b281112013-07-10 16:05:03 -0700380{
Dan Streetmanf1c54842015-09-09 15:35:19 -0700381 return __zswap_cpu_dstmem_notifier(action, (unsigned long)pcpu);
Seth Jennings2b281112013-07-10 16:05:03 -0700382}
383
Dan Streetmanf1c54842015-09-09 15:35:19 -0700384static struct notifier_block zswap_dstmem_notifier = {
385 .notifier_call = zswap_cpu_dstmem_notifier,
Seth Jennings2b281112013-07-10 16:05:03 -0700386};
387
Dan Streetmanf1c54842015-09-09 15:35:19 -0700388static int __init zswap_cpu_dstmem_init(void)
Seth Jennings2b281112013-07-10 16:05:03 -0700389{
390 unsigned long cpu;
391
Srivatsa S. Bhat57637822014-03-11 02:12:40 +0530392 cpu_notifier_register_begin();
Seth Jennings2b281112013-07-10 16:05:03 -0700393 for_each_online_cpu(cpu)
Dan Streetmanf1c54842015-09-09 15:35:19 -0700394 if (__zswap_cpu_dstmem_notifier(CPU_UP_PREPARE, cpu) ==
395 NOTIFY_BAD)
Seth Jennings2b281112013-07-10 16:05:03 -0700396 goto cleanup;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700397 __register_cpu_notifier(&zswap_dstmem_notifier);
Srivatsa S. Bhat57637822014-03-11 02:12:40 +0530398 cpu_notifier_register_done();
Seth Jennings2b281112013-07-10 16:05:03 -0700399 return 0;
400
401cleanup:
402 for_each_online_cpu(cpu)
Dan Streetmanf1c54842015-09-09 15:35:19 -0700403 __zswap_cpu_dstmem_notifier(CPU_UP_CANCELED, cpu);
Srivatsa S. Bhat57637822014-03-11 02:12:40 +0530404 cpu_notifier_register_done();
Seth Jennings2b281112013-07-10 16:05:03 -0700405 return -ENOMEM;
406}
407
Dan Streetmanf1c54842015-09-09 15:35:19 -0700408static void zswap_cpu_dstmem_destroy(void)
Seth Jennings2b281112013-07-10 16:05:03 -0700409{
Dan Streetmanf1c54842015-09-09 15:35:19 -0700410 unsigned long cpu;
411
412 cpu_notifier_register_begin();
413 for_each_online_cpu(cpu)
414 __zswap_cpu_dstmem_notifier(CPU_UP_CANCELED, cpu);
415 __unregister_cpu_notifier(&zswap_dstmem_notifier);
416 cpu_notifier_register_done();
417}
418
419static int __zswap_cpu_comp_notifier(struct zswap_pool *pool,
420 unsigned long action, unsigned long cpu)
421{
422 struct crypto_comp *tfm;
423
424 switch (action) {
425 case CPU_UP_PREPARE:
426 if (WARN_ON(*per_cpu_ptr(pool->tfm, cpu)))
427 break;
428 tfm = crypto_alloc_comp(pool->tfm_name, 0, 0);
429 if (IS_ERR_OR_NULL(tfm)) {
430 pr_err("could not alloc crypto comp %s : %ld\n",
431 pool->tfm_name, PTR_ERR(tfm));
432 return NOTIFY_BAD;
433 }
434 *per_cpu_ptr(pool->tfm, cpu) = tfm;
435 break;
436 case CPU_DEAD:
437 case CPU_UP_CANCELED:
438 tfm = *per_cpu_ptr(pool->tfm, cpu);
439 if (!IS_ERR_OR_NULL(tfm))
440 crypto_free_comp(tfm);
441 *per_cpu_ptr(pool->tfm, cpu) = NULL;
442 break;
443 default:
444 break;
445 }
446 return NOTIFY_OK;
447}
448
449static int zswap_cpu_comp_notifier(struct notifier_block *nb,
450 unsigned long action, void *pcpu)
451{
452 unsigned long cpu = (unsigned long)pcpu;
453 struct zswap_pool *pool = container_of(nb, typeof(*pool), notifier);
454
455 return __zswap_cpu_comp_notifier(pool, action, cpu);
456}
457
458static int zswap_cpu_comp_init(struct zswap_pool *pool)
459{
460 unsigned long cpu;
461
462 memset(&pool->notifier, 0, sizeof(pool->notifier));
463 pool->notifier.notifier_call = zswap_cpu_comp_notifier;
464
465 cpu_notifier_register_begin();
466 for_each_online_cpu(cpu)
467 if (__zswap_cpu_comp_notifier(pool, CPU_UP_PREPARE, cpu) ==
468 NOTIFY_BAD)
469 goto cleanup;
470 __register_cpu_notifier(&pool->notifier);
471 cpu_notifier_register_done();
472 return 0;
473
474cleanup:
475 for_each_online_cpu(cpu)
476 __zswap_cpu_comp_notifier(pool, CPU_UP_CANCELED, cpu);
477 cpu_notifier_register_done();
478 return -ENOMEM;
479}
480
481static void zswap_cpu_comp_destroy(struct zswap_pool *pool)
482{
483 unsigned long cpu;
484
485 cpu_notifier_register_begin();
486 for_each_online_cpu(cpu)
487 __zswap_cpu_comp_notifier(pool, CPU_UP_CANCELED, cpu);
488 __unregister_cpu_notifier(&pool->notifier);
489 cpu_notifier_register_done();
490}
491
492/*********************************
493* pool functions
494**********************************/
495
496static struct zswap_pool *__zswap_pool_current(void)
497{
498 struct zswap_pool *pool;
499
500 pool = list_first_or_null_rcu(&zswap_pools, typeof(*pool), list);
501 WARN_ON(!pool);
502
503 return pool;
504}
505
506static struct zswap_pool *zswap_pool_current(void)
507{
508 assert_spin_locked(&zswap_pools_lock);
509
510 return __zswap_pool_current();
511}
512
513static struct zswap_pool *zswap_pool_current_get(void)
514{
515 struct zswap_pool *pool;
516
517 rcu_read_lock();
518
519 pool = __zswap_pool_current();
520 if (!pool || !zswap_pool_get(pool))
521 pool = NULL;
522
523 rcu_read_unlock();
524
525 return pool;
526}
527
528static struct zswap_pool *zswap_pool_last_get(void)
529{
530 struct zswap_pool *pool, *last = NULL;
531
532 rcu_read_lock();
533
534 list_for_each_entry_rcu(pool, &zswap_pools, list)
535 last = pool;
536 if (!WARN_ON(!last) && !zswap_pool_get(last))
537 last = NULL;
538
539 rcu_read_unlock();
540
541 return last;
542}
543
Dan Streetman8bc8b222015-12-18 14:22:04 -0800544/* type and compressor must be null-terminated */
Dan Streetmanf1c54842015-09-09 15:35:19 -0700545static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor)
546{
547 struct zswap_pool *pool;
548
549 assert_spin_locked(&zswap_pools_lock);
550
551 list_for_each_entry_rcu(pool, &zswap_pools, list) {
Dan Streetman8bc8b222015-12-18 14:22:04 -0800552 if (strcmp(pool->tfm_name, compressor))
Dan Streetmanf1c54842015-09-09 15:35:19 -0700553 continue;
Dan Streetman8bc8b222015-12-18 14:22:04 -0800554 if (strcmp(zpool_get_type(pool->zpool), type))
Dan Streetmanf1c54842015-09-09 15:35:19 -0700555 continue;
556 /* if we can't get it, it's about to be destroyed */
557 if (!zswap_pool_get(pool))
558 continue;
559 return pool;
560 }
561
562 return NULL;
563}
564
565static struct zswap_pool *zswap_pool_create(char *type, char *compressor)
566{
567 struct zswap_pool *pool;
Mel Gormand0164ad2015-11-06 16:28:21 -0800568 gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700569
570 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
571 if (!pool) {
572 pr_err("pool alloc failed\n");
573 return NULL;
574 }
575
576 pool->zpool = zpool_create_pool(type, "zswap", gfp, &zswap_zpool_ops);
577 if (!pool->zpool) {
578 pr_err("%s zpool not available\n", type);
579 goto error;
580 }
581 pr_debug("using %s zpool\n", zpool_get_type(pool->zpool));
582
583 strlcpy(pool->tfm_name, compressor, sizeof(pool->tfm_name));
584 pool->tfm = alloc_percpu(struct crypto_comp *);
585 if (!pool->tfm) {
586 pr_err("percpu alloc failed\n");
587 goto error;
588 }
589
590 if (zswap_cpu_comp_init(pool))
591 goto error;
592 pr_debug("using %s compressor\n", pool->tfm_name);
593
594 /* being the current pool takes 1 ref; this func expects the
595 * caller to always add the new pool as the current pool
596 */
597 kref_init(&pool->kref);
598 INIT_LIST_HEAD(&pool->list);
599
600 zswap_pool_debug("created", pool);
601
602 return pool;
603
604error:
605 free_percpu(pool->tfm);
606 if (pool->zpool)
607 zpool_destroy_pool(pool->zpool);
608 kfree(pool);
609 return NULL;
610}
611
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800612static __init struct zswap_pool *__zswap_pool_create_fallback(void)
Dan Streetmanf1c54842015-09-09 15:35:19 -0700613{
614 if (!crypto_has_comp(zswap_compressor, 0, 0)) {
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800615 if (!strcmp(zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT)) {
616 pr_err("default compressor %s not available\n",
617 zswap_compressor);
618 return NULL;
619 }
Dan Streetmanf1c54842015-09-09 15:35:19 -0700620 pr_err("compressor %s not available, using default %s\n",
621 zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT);
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800622 param_free_charp(&zswap_compressor);
623 zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700624 }
625 if (!zpool_has_pool(zswap_zpool_type)) {
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800626 if (!strcmp(zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT)) {
627 pr_err("default zpool %s not available\n",
628 zswap_zpool_type);
629 return NULL;
630 }
Dan Streetmanf1c54842015-09-09 15:35:19 -0700631 pr_err("zpool %s not available, using default %s\n",
632 zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT);
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800633 param_free_charp(&zswap_zpool_type);
634 zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700635 }
636
637 return zswap_pool_create(zswap_zpool_type, zswap_compressor);
638}
639
640static void zswap_pool_destroy(struct zswap_pool *pool)
641{
642 zswap_pool_debug("destroying", pool);
643
644 zswap_cpu_comp_destroy(pool);
645 free_percpu(pool->tfm);
646 zpool_destroy_pool(pool->zpool);
647 kfree(pool);
648}
649
650static int __must_check zswap_pool_get(struct zswap_pool *pool)
651{
652 return kref_get_unless_zero(&pool->kref);
653}
654
655static void __zswap_pool_release(struct rcu_head *head)
656{
657 struct zswap_pool *pool = container_of(head, typeof(*pool), rcu_head);
658
659 /* nobody should have been able to get a kref... */
660 WARN_ON(kref_get_unless_zero(&pool->kref));
661
662 /* pool is now off zswap_pools list and has no references. */
663 zswap_pool_destroy(pool);
664}
665
666static void __zswap_pool_empty(struct kref *kref)
667{
668 struct zswap_pool *pool;
669
670 pool = container_of(kref, typeof(*pool), kref);
671
672 spin_lock(&zswap_pools_lock);
673
674 WARN_ON(pool == zswap_pool_current());
675
676 list_del_rcu(&pool->list);
677 call_rcu(&pool->rcu_head, __zswap_pool_release);
678
679 spin_unlock(&zswap_pools_lock);
680}
681
682static void zswap_pool_put(struct zswap_pool *pool)
683{
684 kref_put(&pool->kref, __zswap_pool_empty);
Seth Jennings2b281112013-07-10 16:05:03 -0700685}
686
Seth Jennings2b281112013-07-10 16:05:03 -0700687/*********************************
Dan Streetman90b0fc22015-09-09 15:35:21 -0700688* param callbacks
689**********************************/
690
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800691/* val must be a null-terminated string */
Dan Streetman90b0fc22015-09-09 15:35:21 -0700692static int __zswap_param_set(const char *val, const struct kernel_param *kp,
693 char *type, char *compressor)
694{
695 struct zswap_pool *pool, *put_pool = NULL;
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800696 char *s = strstrip((char *)val);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700697 int ret;
698
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800699 /* no change required */
700 if (!strcmp(s, *(char **)kp->arg))
701 return 0;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700702
703 /* if this is load-time (pre-init) param setting,
704 * don't create a pool; that's done during init.
705 */
706 if (!zswap_init_started)
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800707 return param_set_charp(s, kp);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700708
709 if (!type) {
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800710 if (!zpool_has_pool(s)) {
711 pr_err("zpool %s not available\n", s);
712 return -ENOENT;
713 }
Dan Streetman90b0fc22015-09-09 15:35:21 -0700714 type = s;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700715 } else if (!compressor) {
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800716 if (!crypto_has_comp(s, 0, 0)) {
717 pr_err("compressor %s not available\n", s);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700718 return -ENOENT;
719 }
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800720 compressor = s;
721 } else {
722 WARN_ON(1);
723 return -EINVAL;
Dan Streetman90b0fc22015-09-09 15:35:21 -0700724 }
725
726 spin_lock(&zswap_pools_lock);
727
728 pool = zswap_pool_find_get(type, compressor);
729 if (pool) {
730 zswap_pool_debug("using existing", pool);
731 list_del_rcu(&pool->list);
732 } else {
733 spin_unlock(&zswap_pools_lock);
734 pool = zswap_pool_create(type, compressor);
735 spin_lock(&zswap_pools_lock);
736 }
737
738 if (pool)
Dan Streetmanc99b42c2015-11-06 16:29:15 -0800739 ret = param_set_charp(s, kp);
Dan Streetman90b0fc22015-09-09 15:35:21 -0700740 else
741 ret = -EINVAL;
742
743 if (!ret) {
744 put_pool = zswap_pool_current();
745 list_add_rcu(&pool->list, &zswap_pools);
746 } else if (pool) {
747 /* add the possibly pre-existing pool to the end of the pools
748 * list; if it's new (and empty) then it'll be removed and
749 * destroyed by the put after we drop the lock
750 */
751 list_add_tail_rcu(&pool->list, &zswap_pools);
752 put_pool = pool;
753 }
754
755 spin_unlock(&zswap_pools_lock);
756
757 /* drop the ref from either the old current pool,
758 * or the new pool we failed to add
759 */
760 if (put_pool)
761 zswap_pool_put(put_pool);
762
763 return ret;
764}
765
766static int zswap_compressor_param_set(const char *val,
767 const struct kernel_param *kp)
768{
769 return __zswap_param_set(val, kp, zswap_zpool_type, NULL);
770}
771
772static int zswap_zpool_param_set(const char *val,
773 const struct kernel_param *kp)
774{
775 return __zswap_param_set(val, kp, NULL, zswap_compressor);
776}
777
778/*********************************
Seth Jennings2b281112013-07-10 16:05:03 -0700779* writeback code
780**********************************/
781/* return enum for zswap_get_swap_cache_page */
782enum zswap_get_swap_ret {
783 ZSWAP_SWAPCACHE_NEW,
784 ZSWAP_SWAPCACHE_EXIST,
Weijie Yang67d13fe2013-11-12 15:08:26 -0800785 ZSWAP_SWAPCACHE_FAIL,
Seth Jennings2b281112013-07-10 16:05:03 -0700786};
787
788/*
789 * zswap_get_swap_cache_page
790 *
791 * This is an adaption of read_swap_cache_async()
792 *
793 * This function tries to find a page with the given swap entry
794 * in the swapper_space address space (the swap cache). If the page
795 * is found, it is returned in retpage. Otherwise, a page is allocated,
796 * added to the swap cache, and returned in retpage.
797 *
798 * If success, the swap cache page is returned in retpage
Weijie Yang67d13fe2013-11-12 15:08:26 -0800799 * Returns ZSWAP_SWAPCACHE_EXIST if page was already in the swap cache
800 * Returns ZSWAP_SWAPCACHE_NEW if the new page needs to be populated,
801 * the new page is added to swapcache and locked
802 * Returns ZSWAP_SWAPCACHE_FAIL on error
Seth Jennings2b281112013-07-10 16:05:03 -0700803 */
804static int zswap_get_swap_cache_page(swp_entry_t entry,
805 struct page **retpage)
806{
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700807 bool page_was_allocated;
Seth Jennings2b281112013-07-10 16:05:03 -0700808
Dmitry Safonov5b999aa2015-09-08 15:05:00 -0700809 *retpage = __read_swap_cache_async(entry, GFP_KERNEL,
810 NULL, 0, &page_was_allocated);
811 if (page_was_allocated)
812 return ZSWAP_SWAPCACHE_NEW;
813 if (!*retpage)
Weijie Yang67d13fe2013-11-12 15:08:26 -0800814 return ZSWAP_SWAPCACHE_FAIL;
Seth Jennings2b281112013-07-10 16:05:03 -0700815 return ZSWAP_SWAPCACHE_EXIST;
816}
817
818/*
819 * Attempts to free an entry by adding a page to the swap cache,
820 * decompressing the entry data into the page, and issuing a
821 * bio write to write the page back to the swap device.
822 *
823 * This can be thought of as a "resumed writeback" of the page
824 * to the swap device. We are basically resuming the same swap
825 * writeback path that was intercepted with the frontswap_store()
826 * in the first place. After the page has been decompressed into
827 * the swap cache, the compressed version stored by zswap can be
828 * freed.
829 */
Dan Streetman12d79d62014-08-06 16:08:40 -0700830static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
Seth Jennings2b281112013-07-10 16:05:03 -0700831{
832 struct zswap_header *zhdr;
833 swp_entry_t swpentry;
834 struct zswap_tree *tree;
835 pgoff_t offset;
836 struct zswap_entry *entry;
837 struct page *page;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700838 struct crypto_comp *tfm;
Seth Jennings2b281112013-07-10 16:05:03 -0700839 u8 *src, *dst;
840 unsigned int dlen;
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800841 int ret;
Seth Jennings2b281112013-07-10 16:05:03 -0700842 struct writeback_control wbc = {
843 .sync_mode = WB_SYNC_NONE,
844 };
845
846 /* extract swpentry from data */
Dan Streetman12d79d62014-08-06 16:08:40 -0700847 zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO);
Seth Jennings2b281112013-07-10 16:05:03 -0700848 swpentry = zhdr->swpentry; /* here */
Dan Streetman12d79d62014-08-06 16:08:40 -0700849 zpool_unmap_handle(pool, handle);
Seth Jennings2b281112013-07-10 16:05:03 -0700850 tree = zswap_trees[swp_type(swpentry)];
851 offset = swp_offset(swpentry);
Seth Jennings2b281112013-07-10 16:05:03 -0700852
853 /* find and ref zswap entry */
854 spin_lock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800855 entry = zswap_entry_find_get(&tree->rbroot, offset);
Seth Jennings2b281112013-07-10 16:05:03 -0700856 if (!entry) {
857 /* entry was invalidated */
858 spin_unlock(&tree->lock);
859 return 0;
860 }
Seth Jennings2b281112013-07-10 16:05:03 -0700861 spin_unlock(&tree->lock);
862 BUG_ON(offset != entry->offset);
863
864 /* try to allocate swap cache page */
865 switch (zswap_get_swap_cache_page(swpentry, &page)) {
Weijie Yang67d13fe2013-11-12 15:08:26 -0800866 case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */
Seth Jennings2b281112013-07-10 16:05:03 -0700867 ret = -ENOMEM;
868 goto fail;
869
Weijie Yang67d13fe2013-11-12 15:08:26 -0800870 case ZSWAP_SWAPCACHE_EXIST:
Seth Jennings2b281112013-07-10 16:05:03 -0700871 /* page is already in the swap cache, ignore for now */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300872 put_page(page);
Seth Jennings2b281112013-07-10 16:05:03 -0700873 ret = -EEXIST;
874 goto fail;
875
876 case ZSWAP_SWAPCACHE_NEW: /* page is locked */
877 /* decompress */
878 dlen = PAGE_SIZE;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700879 src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle,
Dan Streetman12d79d62014-08-06 16:08:40 -0700880 ZPOOL_MM_RO) + sizeof(struct zswap_header);
Seth Jennings2b281112013-07-10 16:05:03 -0700881 dst = kmap_atomic(page);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700882 tfm = *get_cpu_ptr(entry->pool->tfm);
883 ret = crypto_comp_decompress(tfm, src, entry->length,
884 dst, &dlen);
885 put_cpu_ptr(entry->pool->tfm);
Seth Jennings2b281112013-07-10 16:05:03 -0700886 kunmap_atomic(dst);
Dan Streetmanf1c54842015-09-09 15:35:19 -0700887 zpool_unmap_handle(entry->pool->zpool, entry->handle);
Seth Jennings2b281112013-07-10 16:05:03 -0700888 BUG_ON(ret);
889 BUG_ON(dlen != PAGE_SIZE);
890
891 /* page is up to date */
892 SetPageUptodate(page);
893 }
894
Weijie Yangb349acc2013-11-12 15:07:52 -0800895 /* move it to the tail of the inactive list after end_writeback */
896 SetPageReclaim(page);
897
Seth Jennings2b281112013-07-10 16:05:03 -0700898 /* start writeback */
899 __swap_writepage(page, &wbc, end_swap_bio_write);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300900 put_page(page);
Seth Jennings2b281112013-07-10 16:05:03 -0700901 zswap_written_back_pages++;
902
903 spin_lock(&tree->lock);
Seth Jennings2b281112013-07-10 16:05:03 -0700904 /* drop local reference */
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800905 zswap_entry_put(tree, entry);
Seth Jennings2b281112013-07-10 16:05:03 -0700906
907 /*
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800908 * There are two possible situations for entry here:
909 * (1) refcount is 1(normal case), entry is valid and on the tree
910 * (2) refcount is 0, entry is freed and not on the tree
911 * because invalidate happened during writeback
912 * search the tree and free the entry if find entry
913 */
914 if (entry == zswap_rb_search(&tree->rbroot, offset))
915 zswap_entry_put(tree, entry);
Seth Jennings2b281112013-07-10 16:05:03 -0700916 spin_unlock(&tree->lock);
Seth Jennings2b281112013-07-10 16:05:03 -0700917
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800918 goto end;
919
920 /*
921 * if we get here due to ZSWAP_SWAPCACHE_EXIST
922 * a load may happening concurrently
923 * it is safe and okay to not free the entry
924 * if we free the entry in the following put
925 * it it either okay to return !0
926 */
Seth Jennings2b281112013-07-10 16:05:03 -0700927fail:
928 spin_lock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800929 zswap_entry_put(tree, entry);
Seth Jennings2b281112013-07-10 16:05:03 -0700930 spin_unlock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -0800931
932end:
Seth Jennings2b281112013-07-10 16:05:03 -0700933 return ret;
934}
935
Dan Streetmanf1c54842015-09-09 15:35:19 -0700936static int zswap_shrink(void)
937{
938 struct zswap_pool *pool;
939 int ret;
940
941 pool = zswap_pool_last_get();
942 if (!pool)
943 return -ENOENT;
944
945 ret = zpool_shrink(pool->zpool, 1, NULL);
946
947 zswap_pool_put(pool);
948
949 return ret;
950}
951
Seth Jennings2b281112013-07-10 16:05:03 -0700952/*********************************
953* frontswap hooks
954**********************************/
955/* attempts to compress and store an single page */
956static int zswap_frontswap_store(unsigned type, pgoff_t offset,
957 struct page *page)
958{
959 struct zswap_tree *tree = zswap_trees[type];
960 struct zswap_entry *entry, *dupentry;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700961 struct crypto_comp *tfm;
Seth Jennings2b281112013-07-10 16:05:03 -0700962 int ret;
963 unsigned int dlen = PAGE_SIZE, len;
964 unsigned long handle;
965 char *buf;
966 u8 *src, *dst;
967 struct zswap_header *zhdr;
968
Dan Streetmanc00ed162015-06-25 15:00:35 -0700969 if (!zswap_enabled || !tree) {
Seth Jennings2b281112013-07-10 16:05:03 -0700970 ret = -ENODEV;
971 goto reject;
972 }
973
974 /* reclaim space if needed */
975 if (zswap_is_full()) {
976 zswap_pool_limit_hit++;
Dan Streetmanf1c54842015-09-09 15:35:19 -0700977 if (zswap_shrink()) {
Seth Jennings2b281112013-07-10 16:05:03 -0700978 zswap_reject_reclaim_fail++;
979 ret = -ENOMEM;
980 goto reject;
981 }
982 }
983
984 /* allocate entry */
985 entry = zswap_entry_cache_alloc(GFP_KERNEL);
986 if (!entry) {
987 zswap_reject_kmemcache_fail++;
988 ret = -ENOMEM;
989 goto reject;
990 }
991
Dan Streetmanf1c54842015-09-09 15:35:19 -0700992 /* if entry is successfully added, it keeps the reference */
993 entry->pool = zswap_pool_current_get();
994 if (!entry->pool) {
Seth Jennings2b281112013-07-10 16:05:03 -0700995 ret = -EINVAL;
996 goto freepage;
997 }
998
Dan Streetmanf1c54842015-09-09 15:35:19 -0700999 /* compress */
1000 dst = get_cpu_var(zswap_dstmem);
1001 tfm = *get_cpu_ptr(entry->pool->tfm);
1002 src = kmap_atomic(page);
1003 ret = crypto_comp_compress(tfm, src, PAGE_SIZE, dst, &dlen);
1004 kunmap_atomic(src);
1005 put_cpu_ptr(entry->pool->tfm);
1006 if (ret) {
1007 ret = -EINVAL;
1008 goto put_dstmem;
1009 }
1010
Seth Jennings2b281112013-07-10 16:05:03 -07001011 /* store */
1012 len = dlen + sizeof(struct zswap_header);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001013 ret = zpool_malloc(entry->pool->zpool, len,
Mel Gormand0164ad2015-11-06 16:28:21 -08001014 __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM,
1015 &handle);
Seth Jennings2b281112013-07-10 16:05:03 -07001016 if (ret == -ENOSPC) {
1017 zswap_reject_compress_poor++;
Dan Streetmanf1c54842015-09-09 15:35:19 -07001018 goto put_dstmem;
Seth Jennings2b281112013-07-10 16:05:03 -07001019 }
1020 if (ret) {
1021 zswap_reject_alloc_fail++;
Dan Streetmanf1c54842015-09-09 15:35:19 -07001022 goto put_dstmem;
Seth Jennings2b281112013-07-10 16:05:03 -07001023 }
Dan Streetmanf1c54842015-09-09 15:35:19 -07001024 zhdr = zpool_map_handle(entry->pool->zpool, handle, ZPOOL_MM_RW);
Seth Jennings2b281112013-07-10 16:05:03 -07001025 zhdr->swpentry = swp_entry(type, offset);
1026 buf = (u8 *)(zhdr + 1);
1027 memcpy(buf, dst, dlen);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001028 zpool_unmap_handle(entry->pool->zpool, handle);
Seth Jennings2b281112013-07-10 16:05:03 -07001029 put_cpu_var(zswap_dstmem);
1030
1031 /* populate entry */
1032 entry->offset = offset;
1033 entry->handle = handle;
1034 entry->length = dlen;
1035
1036 /* map */
1037 spin_lock(&tree->lock);
1038 do {
1039 ret = zswap_rb_insert(&tree->rbroot, entry, &dupentry);
1040 if (ret == -EEXIST) {
1041 zswap_duplicate_entry++;
1042 /* remove from rbtree */
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001043 zswap_rb_erase(&tree->rbroot, dupentry);
1044 zswap_entry_put(tree, dupentry);
Seth Jennings2b281112013-07-10 16:05:03 -07001045 }
1046 } while (ret == -EEXIST);
1047 spin_unlock(&tree->lock);
1048
1049 /* update stats */
1050 atomic_inc(&zswap_stored_pages);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001051 zswap_update_total_size();
Seth Jennings2b281112013-07-10 16:05:03 -07001052
1053 return 0;
1054
Dan Streetmanf1c54842015-09-09 15:35:19 -07001055put_dstmem:
Seth Jennings2b281112013-07-10 16:05:03 -07001056 put_cpu_var(zswap_dstmem);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001057 zswap_pool_put(entry->pool);
1058freepage:
Seth Jennings2b281112013-07-10 16:05:03 -07001059 zswap_entry_cache_free(entry);
1060reject:
1061 return ret;
1062}
1063
1064/*
1065 * returns 0 if the page was successfully decompressed
1066 * return -1 on entry not found or error
1067*/
1068static int zswap_frontswap_load(unsigned type, pgoff_t offset,
1069 struct page *page)
1070{
1071 struct zswap_tree *tree = zswap_trees[type];
1072 struct zswap_entry *entry;
Dan Streetmanf1c54842015-09-09 15:35:19 -07001073 struct crypto_comp *tfm;
Seth Jennings2b281112013-07-10 16:05:03 -07001074 u8 *src, *dst;
1075 unsigned int dlen;
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001076 int ret;
Seth Jennings2b281112013-07-10 16:05:03 -07001077
1078 /* find */
1079 spin_lock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001080 entry = zswap_entry_find_get(&tree->rbroot, offset);
Seth Jennings2b281112013-07-10 16:05:03 -07001081 if (!entry) {
1082 /* entry was written back */
1083 spin_unlock(&tree->lock);
1084 return -1;
1085 }
Seth Jennings2b281112013-07-10 16:05:03 -07001086 spin_unlock(&tree->lock);
1087
1088 /* decompress */
1089 dlen = PAGE_SIZE;
Dan Streetmanf1c54842015-09-09 15:35:19 -07001090 src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle,
Dan Streetman12d79d62014-08-06 16:08:40 -07001091 ZPOOL_MM_RO) + sizeof(struct zswap_header);
Seth Jennings2b281112013-07-10 16:05:03 -07001092 dst = kmap_atomic(page);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001093 tfm = *get_cpu_ptr(entry->pool->tfm);
1094 ret = crypto_comp_decompress(tfm, src, entry->length, dst, &dlen);
1095 put_cpu_ptr(entry->pool->tfm);
Seth Jennings2b281112013-07-10 16:05:03 -07001096 kunmap_atomic(dst);
Dan Streetmanf1c54842015-09-09 15:35:19 -07001097 zpool_unmap_handle(entry->pool->zpool, entry->handle);
Seth Jennings2b281112013-07-10 16:05:03 -07001098 BUG_ON(ret);
1099
1100 spin_lock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001101 zswap_entry_put(tree, entry);
Seth Jennings2b281112013-07-10 16:05:03 -07001102 spin_unlock(&tree->lock);
1103
Seth Jennings2b281112013-07-10 16:05:03 -07001104 return 0;
1105}
1106
1107/* frees an entry in zswap */
1108static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset)
1109{
1110 struct zswap_tree *tree = zswap_trees[type];
1111 struct zswap_entry *entry;
Seth Jennings2b281112013-07-10 16:05:03 -07001112
1113 /* find */
1114 spin_lock(&tree->lock);
1115 entry = zswap_rb_search(&tree->rbroot, offset);
1116 if (!entry) {
1117 /* entry was written back */
1118 spin_unlock(&tree->lock);
1119 return;
1120 }
1121
1122 /* remove from rbtree */
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001123 zswap_rb_erase(&tree->rbroot, entry);
Seth Jennings2b281112013-07-10 16:05:03 -07001124
1125 /* drop the initial reference from entry creation */
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001126 zswap_entry_put(tree, entry);
Seth Jennings2b281112013-07-10 16:05:03 -07001127
1128 spin_unlock(&tree->lock);
Seth Jennings2b281112013-07-10 16:05:03 -07001129}
1130
1131/* frees all zswap entries for the given swap type */
1132static void zswap_frontswap_invalidate_area(unsigned type)
1133{
1134 struct zswap_tree *tree = zswap_trees[type];
Cody P Schafer0bd42132013-09-11 14:25:33 -07001135 struct zswap_entry *entry, *n;
Seth Jennings2b281112013-07-10 16:05:03 -07001136
1137 if (!tree)
1138 return;
1139
1140 /* walk the tree and free everything */
1141 spin_lock(&tree->lock);
Weijie Yang0ab0abc2013-11-12 15:08:27 -08001142 rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode)
Minchan Kim60105e12014-04-07 15:38:27 -07001143 zswap_free_entry(entry);
Seth Jennings2b281112013-07-10 16:05:03 -07001144 tree->rbroot = RB_ROOT;
1145 spin_unlock(&tree->lock);
Weijie Yangaa9bca02013-10-16 13:46:54 -07001146 kfree(tree);
1147 zswap_trees[type] = NULL;
Seth Jennings2b281112013-07-10 16:05:03 -07001148}
1149
Seth Jennings2b281112013-07-10 16:05:03 -07001150static void zswap_frontswap_init(unsigned type)
1151{
1152 struct zswap_tree *tree;
1153
1154 tree = kzalloc(sizeof(struct zswap_tree), GFP_KERNEL);
Minchan Kim60105e12014-04-07 15:38:27 -07001155 if (!tree) {
1156 pr_err("alloc failed, zswap disabled for swap type %d\n", type);
1157 return;
1158 }
1159
Seth Jennings2b281112013-07-10 16:05:03 -07001160 tree->rbroot = RB_ROOT;
1161 spin_lock_init(&tree->lock);
1162 zswap_trees[type] = tree;
Seth Jennings2b281112013-07-10 16:05:03 -07001163}
1164
1165static struct frontswap_ops zswap_frontswap_ops = {
1166 .store = zswap_frontswap_store,
1167 .load = zswap_frontswap_load,
1168 .invalidate_page = zswap_frontswap_invalidate_page,
1169 .invalidate_area = zswap_frontswap_invalidate_area,
1170 .init = zswap_frontswap_init
1171};
1172
1173/*********************************
1174* debugfs functions
1175**********************************/
1176#ifdef CONFIG_DEBUG_FS
1177#include <linux/debugfs.h>
1178
1179static struct dentry *zswap_debugfs_root;
1180
1181static int __init zswap_debugfs_init(void)
1182{
1183 if (!debugfs_initialized())
1184 return -ENODEV;
1185
1186 zswap_debugfs_root = debugfs_create_dir("zswap", NULL);
1187 if (!zswap_debugfs_root)
1188 return -ENOMEM;
1189
1190 debugfs_create_u64("pool_limit_hit", S_IRUGO,
1191 zswap_debugfs_root, &zswap_pool_limit_hit);
1192 debugfs_create_u64("reject_reclaim_fail", S_IRUGO,
1193 zswap_debugfs_root, &zswap_reject_reclaim_fail);
1194 debugfs_create_u64("reject_alloc_fail", S_IRUGO,
1195 zswap_debugfs_root, &zswap_reject_alloc_fail);
1196 debugfs_create_u64("reject_kmemcache_fail", S_IRUGO,
1197 zswap_debugfs_root, &zswap_reject_kmemcache_fail);
1198 debugfs_create_u64("reject_compress_poor", S_IRUGO,
1199 zswap_debugfs_root, &zswap_reject_compress_poor);
1200 debugfs_create_u64("written_back_pages", S_IRUGO,
1201 zswap_debugfs_root, &zswap_written_back_pages);
1202 debugfs_create_u64("duplicate_entry", S_IRUGO,
1203 zswap_debugfs_root, &zswap_duplicate_entry);
Dan Streetman12d79d62014-08-06 16:08:40 -07001204 debugfs_create_u64("pool_total_size", S_IRUGO,
1205 zswap_debugfs_root, &zswap_pool_total_size);
Seth Jennings2b281112013-07-10 16:05:03 -07001206 debugfs_create_atomic_t("stored_pages", S_IRUGO,
1207 zswap_debugfs_root, &zswap_stored_pages);
1208
1209 return 0;
1210}
1211
1212static void __exit zswap_debugfs_exit(void)
1213{
1214 debugfs_remove_recursive(zswap_debugfs_root);
1215}
1216#else
1217static int __init zswap_debugfs_init(void)
1218{
1219 return 0;
1220}
1221
1222static void __exit zswap_debugfs_exit(void) { }
1223#endif
1224
1225/*********************************
1226* module init and exit
1227**********************************/
1228static int __init init_zswap(void)
1229{
Dan Streetmanf1c54842015-09-09 15:35:19 -07001230 struct zswap_pool *pool;
Minchan Kim60105e12014-04-07 15:38:27 -07001231
Dan Streetman90b0fc22015-09-09 15:35:21 -07001232 zswap_init_started = true;
1233
Seth Jennings2b281112013-07-10 16:05:03 -07001234 if (zswap_entry_cache_create()) {
1235 pr_err("entry cache creation failed\n");
Dan Streetmanf1c54842015-09-09 15:35:19 -07001236 goto cache_fail;
Seth Jennings2b281112013-07-10 16:05:03 -07001237 }
Dan Streetmanf1c54842015-09-09 15:35:19 -07001238
1239 if (zswap_cpu_dstmem_init()) {
1240 pr_err("dstmem alloc failed\n");
1241 goto dstmem_fail;
Seth Jennings2b281112013-07-10 16:05:03 -07001242 }
Dan Streetmanf1c54842015-09-09 15:35:19 -07001243
1244 pool = __zswap_pool_create_fallback();
1245 if (!pool) {
1246 pr_err("pool creation failed\n");
1247 goto pool_fail;
Seth Jennings2b281112013-07-10 16:05:03 -07001248 }
Dan Streetmanf1c54842015-09-09 15:35:19 -07001249 pr_info("loaded using pool %s/%s\n", pool->tfm_name,
1250 zpool_get_type(pool->zpool));
1251
1252 list_add(&pool->list, &zswap_pools);
Minchan Kim60105e12014-04-07 15:38:27 -07001253
Seth Jennings2b281112013-07-10 16:05:03 -07001254 frontswap_register_ops(&zswap_frontswap_ops);
1255 if (zswap_debugfs_init())
1256 pr_warn("debugfs initialization failed\n");
1257 return 0;
Dan Streetmanf1c54842015-09-09 15:35:19 -07001258
1259pool_fail:
1260 zswap_cpu_dstmem_destroy();
1261dstmem_fail:
Fabian Frederickc1192392014-08-08 14:19:35 -07001262 zswap_entry_cache_destroy();
Dan Streetmanf1c54842015-09-09 15:35:19 -07001263cache_fail:
Seth Jennings2b281112013-07-10 16:05:03 -07001264 return -ENOMEM;
1265}
1266/* must be late so crypto has time to come up */
1267late_initcall(init_zswap);
1268
1269MODULE_LICENSE("GPL");
Seth Jennings68386da2014-11-12 21:08:46 -06001270MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
Seth Jennings2b281112013-07-10 16:05:03 -07001271MODULE_DESCRIPTION("Compressed cache for swap pages");