2 * zswap.c - zswap driver file
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.
10 * Copyright (C) 2012 Seth Jennings <sjenning@linux.vnet.ibm.com>
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.
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.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
37 #include <linux/zpool.h>
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 #include <linux/sec_debug.h>
46 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
47 #include <linux/jhash.h>
50 /*********************************
52 **********************************/
53 /* Total bytes used by the compressed storage */
54 static u64 zswap_pool_total_size
;
55 /* Number of memory pages used by the compressed pool */
57 /* The number of compressed pages currently stored in zswap */
58 atomic_t zswap_stored_pages
= ATOMIC_INIT(0);
60 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
61 /* The number of swapped out pages which are identified as duplicate
62 to the existing zswap pages. Compression and storing of these
64 static atomic_t zswap_duplicate_pages
= ATOMIC_INIT(0);
68 * The statistics below are not protected from concurrent access for
69 * performance reasons so they may not be a 100% accurate. However,
70 * they do provide useful information on roughly how many times a
71 * certain event is occurring.
74 /* Pool limit was hit (see zswap_max_pool_percent) */
75 static u64 zswap_pool_limit_hit
;
76 /* Pages written back when pool limit was reached */
77 static u64 zswap_written_back_pages
;
78 /* Store failed due to a reclaim failure after pool limit was reached */
79 static u64 zswap_reject_reclaim_fail
;
80 /* Compressed page was too big for the allocator to (optimally) store */
81 static u64 zswap_reject_compress_poor
;
82 /* Store failed because underlying allocator could not get memory */
83 static u64 zswap_reject_alloc_fail
;
84 /* Store failed because the entry metadata could not be allocated (rare) */
85 static u64 zswap_reject_kmemcache_fail
;
86 /* Duplicate store was encountered (rare) */
87 static u64 zswap_duplicate_entry
;
89 /* The number of zero pages currently stored in zswap */
90 static atomic_t zswap_zero_pages
= ATOMIC_INIT(0);
92 /*********************************
94 **********************************/
96 /* Enable/disable zswap (disabled by default) */
97 static bool zswap_enabled
= 1;
98 static int zswap_enabled_param_set(const char *,
99 const struct kernel_param
*);
100 static struct kernel_param_ops zswap_enabled_param_ops
= {
101 .set
= zswap_enabled_param_set
,
102 .get
= param_get_bool
,
104 module_param_cb(enabled
, &zswap_enabled_param_ops
, &zswap_enabled
, 0644);
106 /* Crypto compressor to use */
107 #define ZSWAP_COMPRESSOR_DEFAULT "lzo"
108 #define ZSWAP_COMPRESSOR "lz4"
109 static char *zswap_compressor
= ZSWAP_COMPRESSOR
;
110 static int zswap_compressor_param_set(const char *,
111 const struct kernel_param
*);
112 static struct kernel_param_ops zswap_compressor_param_ops
= {
113 .set
= zswap_compressor_param_set
,
114 .get
= param_get_charp
,
115 .free
= param_free_charp
,
117 module_param_cb(compressor
, &zswap_compressor_param_ops
,
118 &zswap_compressor
, 0644);
120 /* Compressed storage zpool to use */
121 #define ZSWAP_ZPOOL_DEFAULT "zsmalloc"
122 static char *zswap_zpool_type
= ZSWAP_ZPOOL_DEFAULT
;
123 static int zswap_zpool_param_set(const char *, const struct kernel_param
*);
124 static struct kernel_param_ops zswap_zpool_param_ops
= {
125 .set
= zswap_zpool_param_set
,
126 .get
= param_get_charp
,
127 .free
= param_free_charp
,
129 module_param_cb(zpool
, &zswap_zpool_param_ops
, &zswap_zpool_type
, 0644);
131 /* The maximum percentage of memory that the compressed pool can occupy */
132 static unsigned int zswap_max_pool_percent
= 50;
133 module_param_named(max_pool_percent
, zswap_max_pool_percent
, uint
, 0644);
135 /*********************************
137 **********************************/
138 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
140 * struct zswap_handle
141 * This structure contains the metadata for tracking single zpool
144 * rbnode - links the handle into red-black tree
145 * checksum - 32-bit checksum value of the page swapped to zswap
146 * ref_count - number of pages sharing this handle
147 * length - the length in bytes of the compressed page data.
148 * Needed during decompression.
149 * handle - zpool allocation handle that stores the compressed page data
151 struct zswap_handle
{
152 struct rb_node rbnode
;
156 unsigned long handle
;
162 struct crypto_comp
* __percpu
*tfm
;
164 struct list_head list
;
165 struct rcu_head rcu_head
;
166 struct notifier_block notifier
;
167 char tfm_name
[CRYPTO_MAX_ALG_NAME
];
173 * This structure contains the metadata for tracking a single compressed
176 * #ifndef CONFIG_ZSWAP_SAME_PAGE_SHARING
177 * rbnode - links the entry into red-black tree for the appropriate swap type
178 * offset - the swap offset for the entry. Index into the red-black tree.
179 * refcount - the number of outstanding reference to the entry. This is needed
180 * to protect against premature freeing of the entry by code
181 * concurrent calls to load, invalidate, and writeback. The lock
182 * for the zswap_tree structure that contains the entry must
183 * be held while changing the refcount. Since the lock must
184 * be held, there is no reason to also make refcount atomic.
185 * length - the length in bytes of the compressed page data. Needed during
187 * pool - the zswap_pool the entry's data is in
188 * handle - zpool allocation handle that stores the compressed page data
189 * zero_flag - the flag indicating the page for the zswap_entry is a zero page.
190 * zswap does not store the page during compression.
191 * It memsets the page with 0 during decompression.
193 * zhandle - pointer to struct zswap_handle where length and handle are moved into.
196 #ifndef CONFIG_ZSWAP_SAME_PAGE_SHARING
198 struct rb_node rbnode
;
202 struct zswap_pool
*pool
;
203 unsigned long handle
;
204 unsigned char zero_flag
;
208 struct rb_node rbnode
;
211 struct zswap_pool
*pool
;
212 struct zswap_handle
*zhandle
;
213 unsigned char zero_flag
;
217 #ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
218 struct zswap_header
{
219 swp_entry_t swpentry
;
224 * The tree lock in the zswap_tree struct protects a few things:
226 * - the refcount field of each entry in the tree
229 struct rb_root rbroot
;
230 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
231 struct rb_root zhandleroot
;
237 static struct zswap_tree
*zswap_trees
[MAX_SWAPFILES
];
239 /* RCU-protected iteration */
240 static LIST_HEAD(zswap_pools
);
241 /* protects zswap_pools list modification */
242 static DEFINE_SPINLOCK(zswap_pools_lock
);
243 /* pool counter to provide unique names to zpool */
244 static atomic_t zswap_pools_count
= ATOMIC_INIT(0);
246 /* used by param callback function */
247 static bool zswap_init_started
;
249 /* fatal error during init */
250 static bool zswap_init_failed
;
252 /*********************************
253 * helpers and fwd declarations
254 **********************************/
256 #define zswap_pool_debug(msg, p) \
257 pr_debug("%s pool %s/%s\n", msg, (p)->tfm_name, \
258 zpool_get_type((p)->zpool))
260 static int zswap_writeback_entry(struct zpool
*pool
, unsigned long handle
);
261 static int zswap_pool_get(struct zswap_pool
*pool
);
262 static void zswap_pool_put(struct zswap_pool
*pool
);
264 static const struct zpool_ops zswap_zpool_ops
= {
265 .evict
= zswap_writeback_entry
268 static bool zswap_is_full(void)
270 return totalram_pages
* zswap_max_pool_percent
/ 100 <
271 DIV_ROUND_UP(zswap_pool_total_size
, PAGE_SIZE
);
274 static void zswap_update_total_size(void)
276 struct zswap_pool
*pool
;
281 list_for_each_entry_rcu(pool
, &zswap_pools
, list
)
282 total
+= zpool_get_total_size(pool
->zpool
);
286 zswap_pool_total_size
= total
;
287 zswap_pool_pages
= zswap_pool_total_size
>> PAGE_SHIFT
;
290 /*********************************
291 * zswap entry functions
292 **********************************/
293 static struct kmem_cache
*zswap_entry_cache
;
295 static int __init
zswap_entry_cache_create(void)
297 zswap_entry_cache
= KMEM_CACHE(zswap_entry
, 0);
298 return zswap_entry_cache
== NULL
;
301 static void __init
zswap_entry_cache_destroy(void)
303 kmem_cache_destroy(zswap_entry_cache
);
306 static struct zswap_entry
*zswap_entry_cache_alloc(gfp_t gfp
)
308 struct zswap_entry
*entry
;
309 entry
= kmem_cache_alloc(zswap_entry_cache
, gfp
);
313 entry
->zero_flag
= 0;
314 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
315 entry
->zhandle
= NULL
;
317 RB_CLEAR_NODE(&entry
->rbnode
);
321 static void zswap_entry_cache_free(struct zswap_entry
*entry
)
323 kmem_cache_free(zswap_entry_cache
, entry
);
326 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
327 /*********************************
328 * zswap handle functions
329 **********************************/
330 static struct kmem_cache
*zswap_handle_cache
;
332 static int zswap_handle_cache_create(void)
334 zswap_handle_cache
= KMEM_CACHE(zswap_handle
, 0);
335 return zswap_handle_cache
== NULL
;
338 static void __init
zswap_handle_cache_destroy(void)
340 kmem_cache_destroy(zswap_handle_cache
);
343 static struct zswap_handle
*zswap_handle_cache_alloc(gfp_t gfp
)
345 struct zswap_handle
*zhandle
;
346 zhandle
= kmem_cache_alloc(zswap_handle_cache
, gfp
);
349 zhandle
->ref_count
= 1;
350 RB_CLEAR_NODE(&zhandle
->rbnode
);
354 static void zswap_handle_cache_free(struct zswap_handle
*zhandle
)
356 kmem_cache_free(zswap_handle_cache
, zhandle
);
360 /*********************************
362 **********************************/
363 static struct zswap_entry
*zswap_rb_search(struct rb_root
*root
, pgoff_t offset
)
365 struct rb_node
*node
= root
->rb_node
;
366 struct zswap_entry
*entry
;
369 entry
= rb_entry(node
, struct zswap_entry
, rbnode
);
370 if (entry
->offset
> offset
)
371 node
= node
->rb_left
;
372 else if (entry
->offset
< offset
)
373 node
= node
->rb_right
;
381 * In the case that a entry with the same offset is found, a pointer to
382 * the existing entry is stored in dupentry and the function returns -EEXIST
384 static int zswap_rb_insert(struct rb_root
*root
, struct zswap_entry
*entry
,
385 struct zswap_entry
**dupentry
)
387 struct rb_node
**link
= &root
->rb_node
, *parent
= NULL
;
388 struct zswap_entry
*myentry
;
392 myentry
= rb_entry(parent
, struct zswap_entry
, rbnode
);
393 if (myentry
->offset
> entry
->offset
)
394 link
= &(*link
)->rb_left
;
395 else if (myentry
->offset
< entry
->offset
)
396 link
= &(*link
)->rb_right
;
402 rb_link_node(&entry
->rbnode
, parent
, link
);
403 rb_insert_color(&entry
->rbnode
, root
);
407 static void zswap_rb_erase(struct rb_root
*root
, struct zswap_entry
*entry
)
409 if (!RB_EMPTY_NODE(&entry
->rbnode
)) {
410 rb_erase(&entry
->rbnode
, root
);
411 RB_CLEAR_NODE(&entry
->rbnode
);
415 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
416 static struct zswap_handle
*zswap_handle_rb_search(struct rb_root
*root
,
419 struct rb_node
*node
= root
->rb_node
;
420 struct zswap_handle
*zhandle
;
423 zhandle
= rb_entry(node
, struct zswap_handle
, rbnode
);
424 if (zhandle
->checksum
> checksum
)
425 node
= node
->rb_left
;
426 else if (zhandle
->checksum
< checksum
)
427 node
= node
->rb_right
;
435 * In the case that zhandle with the same checksum is found, a pointer to
436 * the existing zhandle is stored in duphandle and the function returns -EEXIST
438 static int zswap_handle_rb_insert(struct rb_root
*root
,
439 struct zswap_handle
*zhandle
,
440 struct zswap_handle
**duphandle
)
442 struct rb_node
**link
= &root
->rb_node
, *parent
= NULL
;
443 struct zswap_handle
*myhandle
;
447 myhandle
= rb_entry(parent
, struct zswap_handle
, rbnode
);
448 if (myhandle
->checksum
> zhandle
->checksum
)
449 link
= &parent
->rb_left
;
450 else if (myhandle
->checksum
< zhandle
->checksum
)
451 link
= &parent
->rb_right
;
453 *duphandle
= myhandle
;
457 rb_link_node(&zhandle
->rbnode
, parent
, link
);
458 rb_insert_color(&zhandle
->rbnode
, root
);
462 static void zswap_handle_erase(struct rb_root
*root
,
463 struct zswap_handle
*zhandle
)
465 if (!RB_EMPTY_NODE(&zhandle
->rbnode
)) {
466 rb_erase(&zhandle
->rbnode
, root
);
467 RB_CLEAR_NODE(&zhandle
->rbnode
);
471 static void zswap_free_handle(struct zswap_pool
*pool
, struct zswap_handle
*zhandle
)
473 zpool_free(pool
->zpool
, zhandle
->handle
);
474 zswap_handle_cache_free(zhandle
);
477 /* This function searches for the same page in the zhandle RB-Tree based on the
478 * checksum value of the new page. If the same page is found the zhandle of that
481 static struct zswap_handle
*zswap_same_page_search(struct zswap_pool
*pool
, struct zswap_tree
*tree
,
482 u8
*uncmem
, u32 checksum
)
485 unsigned int dlen
= PAGE_SIZE
;
486 u8
*src
= NULL
, *dst
= NULL
;
487 struct zswap_handle
*myhandle
= NULL
;
488 struct crypto_comp
*tfm
;
490 myhandle
= zswap_handle_rb_search(&tree
->zhandleroot
, checksum
);
492 /* Compare memory contents */
493 dst
= (u8
*)tree
->buffer
;
494 src
= (u8
*)zpool_map_handle(pool
->zpool
,
495 myhandle
->handle
, ZPOOL_MM_RO_NOWAIT
);
499 if (myhandle
->length
== PAGE_SIZE
)
502 tfm
= *get_cpu_ptr(pool
->tfm
);
503 ret
= crypto_comp_decompress(tfm
, src
, myhandle
->length
, dst
, &dlen
);
504 put_cpu_ptr(pool
->tfm
);
507 zpool_unmap_handle(pool
->zpool
, myhandle
->handle
);
510 ret
= memcmp(dst
, uncmem
, PAGE_SIZE
);
519 * Carries out the common pattern of freeing and entry's zpool allocation,
520 * freeing the entry itself, and decrementing the number of stored pages.
522 static void zswap_free_entry(struct zswap_entry
*entry
)
524 if (entry
->zero_flag
== 1) {
525 atomic_dec(&zswap_zero_pages
);
528 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
529 entry
->zhandle
->ref_count
--;
530 if (!entry
->zhandle
->ref_count
)
531 zswap_free_handle(entry
->pool
, entry
->zhandle
);
533 atomic_dec(&zswap_duplicate_pages
);
535 zpool_free(entry
->pool
->zpool
, entry
->handle
);
538 zswap_pool_put(entry
->pool
);
539 zswap_entry_cache_free(entry
);
540 atomic_dec(&zswap_stored_pages
);
541 zswap_update_total_size();
544 /* caller must hold the tree lock */
545 static void zswap_entry_get(struct zswap_entry
*entry
)
550 /* caller must hold the tree lock
551 * remove from the tree and free it, if nobody reference the entry
553 static void zswap_entry_put(struct zswap_tree
*tree
,
554 struct zswap_entry
*entry
)
556 int refcount
= --entry
->refcount
;
558 BUG_ON(refcount
< 0);
560 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
561 if (entry
->zhandle
&& entry
->zhandle
->ref_count
== 1)
562 zswap_handle_erase(&tree
->zhandleroot
, entry
->zhandle
);
564 zswap_rb_erase(&tree
->rbroot
, entry
);
565 zswap_free_entry(entry
);
569 /* caller must hold the tree lock */
570 static struct zswap_entry
*zswap_entry_find_get(struct rb_root
*root
,
573 struct zswap_entry
*entry
;
575 entry
= zswap_rb_search(root
, offset
);
577 zswap_entry_get(entry
);
582 /*********************************
584 **********************************/
585 static DEFINE_PER_CPU(u8
*, zswap_dstmem
);
587 static int __zswap_cpu_dstmem_notifier(unsigned long action
, unsigned long cpu
)
593 dst
= kmalloc_node(PAGE_SIZE
* 2, GFP_KERNEL
, cpu_to_node(cpu
));
595 pr_err("can't allocate compressor buffer\n");
598 per_cpu(zswap_dstmem
, cpu
) = dst
;
601 case CPU_UP_CANCELED
:
602 dst
= per_cpu(zswap_dstmem
, cpu
);
604 per_cpu(zswap_dstmem
, cpu
) = NULL
;
612 static int zswap_cpu_dstmem_notifier(struct notifier_block
*nb
,
613 unsigned long action
, void *pcpu
)
615 return __zswap_cpu_dstmem_notifier(action
, (unsigned long)pcpu
);
618 static struct notifier_block zswap_dstmem_notifier
= {
619 .notifier_call
= zswap_cpu_dstmem_notifier
,
622 static int __init
zswap_cpu_dstmem_init(void)
626 cpu_notifier_register_begin();
627 for_each_online_cpu(cpu
)
628 if (__zswap_cpu_dstmem_notifier(CPU_UP_PREPARE
, cpu
) ==
631 __register_cpu_notifier(&zswap_dstmem_notifier
);
632 cpu_notifier_register_done();
636 for_each_online_cpu(cpu
)
637 __zswap_cpu_dstmem_notifier(CPU_UP_CANCELED
, cpu
);
638 cpu_notifier_register_done();
642 static void zswap_cpu_dstmem_destroy(void)
646 cpu_notifier_register_begin();
647 for_each_online_cpu(cpu
)
648 __zswap_cpu_dstmem_notifier(CPU_UP_CANCELED
, cpu
);
649 __unregister_cpu_notifier(&zswap_dstmem_notifier
);
650 cpu_notifier_register_done();
653 static int __zswap_cpu_comp_notifier(struct zswap_pool
*pool
,
654 unsigned long action
, unsigned long cpu
)
656 struct crypto_comp
*tfm
;
660 if (WARN_ON(*per_cpu_ptr(pool
->tfm
, cpu
)))
662 tfm
= crypto_alloc_comp(pool
->tfm_name
, 0, 0);
663 if (IS_ERR_OR_NULL(tfm
)) {
664 pr_err("could not alloc crypto comp %s : %ld\n",
665 pool
->tfm_name
, PTR_ERR(tfm
));
668 *per_cpu_ptr(pool
->tfm
, cpu
) = tfm
;
671 case CPU_UP_CANCELED
:
672 tfm
= *per_cpu_ptr(pool
->tfm
, cpu
);
673 if (!IS_ERR_OR_NULL(tfm
))
674 crypto_free_comp(tfm
);
675 *per_cpu_ptr(pool
->tfm
, cpu
) = NULL
;
683 static int zswap_cpu_comp_notifier(struct notifier_block
*nb
,
684 unsigned long action
, void *pcpu
)
686 unsigned long cpu
= (unsigned long)pcpu
;
687 struct zswap_pool
*pool
= container_of(nb
, typeof(*pool
), notifier
);
689 return __zswap_cpu_comp_notifier(pool
, action
, cpu
);
692 static int zswap_cpu_comp_init(struct zswap_pool
*pool
)
696 memset(&pool
->notifier
, 0, sizeof(pool
->notifier
));
697 pool
->notifier
.notifier_call
= zswap_cpu_comp_notifier
;
699 cpu_notifier_register_begin();
700 for_each_online_cpu(cpu
)
701 if (__zswap_cpu_comp_notifier(pool
, CPU_UP_PREPARE
, cpu
) ==
704 __register_cpu_notifier(&pool
->notifier
);
705 cpu_notifier_register_done();
709 for_each_online_cpu(cpu
)
710 __zswap_cpu_comp_notifier(pool
, CPU_UP_CANCELED
, cpu
);
711 cpu_notifier_register_done();
715 static void zswap_cpu_comp_destroy(struct zswap_pool
*pool
)
719 cpu_notifier_register_begin();
720 for_each_online_cpu(cpu
)
721 __zswap_cpu_comp_notifier(pool
, CPU_UP_CANCELED
, cpu
);
722 __unregister_cpu_notifier(&pool
->notifier
);
723 cpu_notifier_register_done();
726 /*********************************
728 **********************************/
730 static struct zswap_pool
*__zswap_pool_current(void)
732 struct zswap_pool
*pool
;
734 pool
= list_first_or_null_rcu(&zswap_pools
, typeof(*pool
), list
);
740 static struct zswap_pool
*zswap_pool_current(void)
742 assert_spin_locked(&zswap_pools_lock
);
744 return __zswap_pool_current();
747 static struct zswap_pool
*zswap_pool_current_get(void)
749 struct zswap_pool
*pool
;
753 pool
= __zswap_pool_current();
754 if (!pool
|| !zswap_pool_get(pool
))
762 #ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
763 static struct zswap_pool
*zswap_pool_last_get(void)
765 struct zswap_pool
*pool
, *last
= NULL
;
769 list_for_each_entry_rcu(pool
, &zswap_pools
, list
)
771 if (!WARN_ON(!last
) && !zswap_pool_get(last
))
780 /* type and compressor must be null-terminated */
781 static struct zswap_pool
*zswap_pool_find_get(char *type
, char *compressor
)
783 struct zswap_pool
*pool
;
785 assert_spin_locked(&zswap_pools_lock
);
787 list_for_each_entry_rcu(pool
, &zswap_pools
, list
) {
788 if (strcmp(pool
->tfm_name
, compressor
))
790 if (strcmp(zpool_get_type(pool
->zpool
), type
))
792 /* if we can't get it, it's about to be destroyed */
793 if (!zswap_pool_get(pool
))
801 static struct zswap_pool
*zswap_pool_create(char *type
, char *compressor
)
803 struct zswap_pool
*pool
;
804 char name
[38]; /* 'zswap' + 32 char (max) num + \0 */
805 #ifdef CONFIG_ZSWAP_MIGRATION_SUPPORT
806 gfp_t gfp
= __GFP_NORETRY
| __GFP_NOWARN
| __GFP_KSWAPD_RECLAIM
| __GFP_HIGHMEM
| __GFP_MOVABLE
;
808 gfp_t gfp
= __GFP_NORETRY
| __GFP_NOWARN
| __GFP_KSWAPD_RECLAIM
| __GFP_HIGHMEM
;
811 pool
= kzalloc(sizeof(*pool
), GFP_KERNEL
);
813 pr_err("pool alloc failed\n");
817 /* unique name for each pool specifically required by zsmalloc */
818 snprintf(name
, 38, "zswap%x", atomic_inc_return(&zswap_pools_count
));
820 pool
->zpool
= zpool_create_pool(type
, name
, gfp
, &zswap_zpool_ops
);
822 pr_err("%s zpool not available\n", type
);
825 pr_debug("using %s zpool\n", zpool_get_type(pool
->zpool
));
827 strlcpy(pool
->tfm_name
, compressor
, sizeof(pool
->tfm_name
));
828 pool
->tfm
= alloc_percpu(struct crypto_comp
*);
830 pr_err("percpu alloc failed\n");
834 if (zswap_cpu_comp_init(pool
))
836 pr_debug("using %s compressor\n", pool
->tfm_name
);
838 /* being the current pool takes 1 ref; this func expects the
839 * caller to always add the new pool as the current pool
841 kref_init(&pool
->kref
);
842 INIT_LIST_HEAD(&pool
->list
);
844 zswap_pool_debug("created", pool
);
849 free_percpu(pool
->tfm
);
851 zpool_destroy_pool(pool
->zpool
);
856 static __init
struct zswap_pool
*__zswap_pool_create_fallback(void)
858 if (!crypto_has_comp(zswap_compressor
, 0, 0)) {
859 if (!strcmp(zswap_compressor
, ZSWAP_COMPRESSOR_DEFAULT
)) {
860 pr_err("default compressor %s not available\n",
864 pr_err("compressor %s not available, using default %s\n",
865 zswap_compressor
, ZSWAP_COMPRESSOR_DEFAULT
);
866 param_free_charp(&zswap_compressor
);
867 zswap_compressor
= ZSWAP_COMPRESSOR_DEFAULT
;
869 if (!zpool_has_pool(zswap_zpool_type
)) {
870 if (!strcmp(zswap_zpool_type
, ZSWAP_ZPOOL_DEFAULT
)) {
871 pr_err("default zpool %s not available\n",
875 pr_err("zpool %s not available, using default %s\n",
876 zswap_zpool_type
, ZSWAP_ZPOOL_DEFAULT
);
877 param_free_charp(&zswap_zpool_type
);
878 zswap_zpool_type
= ZSWAP_ZPOOL_DEFAULT
;
881 return zswap_pool_create(zswap_zpool_type
, zswap_compressor
);
884 static void zswap_pool_destroy(struct zswap_pool
*pool
)
886 zswap_pool_debug("destroying", pool
);
888 zswap_cpu_comp_destroy(pool
);
889 free_percpu(pool
->tfm
);
890 zpool_destroy_pool(pool
->zpool
);
894 static int __must_check
zswap_pool_get(struct zswap_pool
*pool
)
896 return kref_get_unless_zero(&pool
->kref
);
899 static void __zswap_pool_release(struct rcu_head
*head
)
901 struct zswap_pool
*pool
= container_of(head
, typeof(*pool
), rcu_head
);
903 /* nobody should have been able to get a kref... */
904 WARN_ON(kref_get_unless_zero(&pool
->kref
));
906 /* pool is now off zswap_pools list and has no references. */
907 zswap_pool_destroy(pool
);
910 static void __zswap_pool_empty(struct kref
*kref
)
912 struct zswap_pool
*pool
;
914 pool
= container_of(kref
, typeof(*pool
), kref
);
916 spin_lock(&zswap_pools_lock
);
918 WARN_ON(pool
== zswap_pool_current());
920 list_del_rcu(&pool
->list
);
921 call_rcu(&pool
->rcu_head
, __zswap_pool_release
);
923 spin_unlock(&zswap_pools_lock
);
926 static void zswap_pool_put(struct zswap_pool
*pool
)
928 kref_put(&pool
->kref
, __zswap_pool_empty
);
931 /*********************************
933 **********************************/
935 /* val must be a null-terminated string */
936 static int __zswap_param_set(const char *val
, const struct kernel_param
*kp
,
937 char *type
, char *compressor
)
939 struct zswap_pool
*pool
, *put_pool
= NULL
;
940 char *s
= strstrip((char *)val
);
943 if (zswap_init_failed
) {
944 pr_err("can't set param, initialization failed\n");
948 /* no change required */
949 if (!strcmp(s
, *(char **)kp
->arg
))
952 /* if this is load-time (pre-init) param setting,
953 * don't create a pool; that's done during init.
955 if (!zswap_init_started
)
956 return param_set_charp(s
, kp
);
959 if (!zpool_has_pool(s
)) {
960 pr_err("zpool %s not available\n", s
);
964 } else if (!compressor
) {
965 if (!crypto_has_comp(s
, 0, 0)) {
966 pr_err("compressor %s not available\n", s
);
975 spin_lock(&zswap_pools_lock
);
977 pool
= zswap_pool_find_get(type
, compressor
);
979 zswap_pool_debug("using existing", pool
);
980 list_del_rcu(&pool
->list
);
982 spin_unlock(&zswap_pools_lock
);
983 pool
= zswap_pool_create(type
, compressor
);
984 spin_lock(&zswap_pools_lock
);
988 ret
= param_set_charp(s
, kp
);
993 put_pool
= zswap_pool_current();
994 list_add_rcu(&pool
->list
, &zswap_pools
);
996 /* add the possibly pre-existing pool to the end of the pools
997 * list; if it's new (and empty) then it'll be removed and
998 * destroyed by the put after we drop the lock
1000 list_add_tail_rcu(&pool
->list
, &zswap_pools
);
1004 spin_unlock(&zswap_pools_lock
);
1006 /* drop the ref from either the old current pool,
1007 * or the new pool we failed to add
1010 zswap_pool_put(put_pool
);
1015 static int zswap_compressor_param_set(const char *val
,
1016 const struct kernel_param
*kp
)
1018 return __zswap_param_set(val
, kp
, zswap_zpool_type
, NULL
);
1021 static int zswap_zpool_param_set(const char *val
,
1022 const struct kernel_param
*kp
)
1024 return __zswap_param_set(val
, kp
, NULL
, zswap_compressor
);
1027 static int zswap_enabled_param_set(const char *val
,
1028 const struct kernel_param
*kp
)
1030 if (zswap_init_failed
) {
1031 pr_err("can't enable, initialization failed\n");
1035 return param_set_bool(val
, kp
);
1038 /*********************************
1040 **********************************/
1041 /* return enum for zswap_get_swap_cache_page */
1042 enum zswap_get_swap_ret
{
1043 ZSWAP_SWAPCACHE_NEW
,
1044 ZSWAP_SWAPCACHE_EXIST
,
1045 ZSWAP_SWAPCACHE_FAIL
,
1048 #ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
1050 * zswap_get_swap_cache_page
1052 * This is an adaption of read_swap_cache_async()
1054 * This function tries to find a page with the given swap entry
1055 * in the swapper_space address space (the swap cache). If the page
1056 * is found, it is returned in retpage. Otherwise, a page is allocated,
1057 * added to the swap cache, and returned in retpage.
1059 * If success, the swap cache page is returned in retpage
1060 * Returns ZSWAP_SWAPCACHE_EXIST if page was already in the swap cache
1061 * Returns ZSWAP_SWAPCACHE_NEW if the new page needs to be populated,
1062 * the new page is added to swapcache and locked
1063 * Returns ZSWAP_SWAPCACHE_FAIL on error
1065 static int zswap_get_swap_cache_page(swp_entry_t entry
,
1066 struct page
**retpage
)
1068 bool page_was_allocated
;
1070 *retpage
= __read_swap_cache_async(entry
, GFP_KERNEL
,
1071 NULL
, 0, &page_was_allocated
);
1072 if (page_was_allocated
)
1073 return ZSWAP_SWAPCACHE_NEW
;
1075 return ZSWAP_SWAPCACHE_FAIL
;
1076 return ZSWAP_SWAPCACHE_EXIST
;
1080 * Attempts to free an entry by adding a page to the swap cache,
1081 * decompressing the entry data into the page, and issuing a
1082 * bio write to write the page back to the swap device.
1084 * This can be thought of as a "resumed writeback" of the page
1085 * to the swap device. We are basically resuming the same swap
1086 * writeback path that was intercepted with the frontswap_store()
1087 * in the first place. After the page has been decompressed into
1088 * the swap cache, the compressed version stored by zswap can be
1091 static int zswap_writeback_entry(struct zpool
*pool
, unsigned long handle
)
1093 struct zswap_header
*zhdr
;
1094 swp_entry_t swpentry
;
1095 struct zswap_tree
*tree
;
1097 struct zswap_entry
*entry
;
1099 struct crypto_comp
*tfm
;
1103 struct writeback_control wbc
= {
1104 .sync_mode
= WB_SYNC_NONE
,
1107 /* extract swpentry from data */
1108 zhdr
= zpool_map_handle(pool
, handle
, ZPOOL_MM_RO
);
1109 swpentry
= zhdr
->swpentry
; /* here */
1110 zpool_unmap_handle(pool
, handle
);
1111 tree
= zswap_trees
[swp_type(swpentry
)];
1112 offset
= swp_offset(swpentry
);
1114 /* find and ref zswap entry */
1115 spin_lock(&tree
->lock
);
1116 entry
= zswap_entry_find_get(&tree
->rbroot
, offset
);
1118 /* entry was invalidated */
1119 spin_unlock(&tree
->lock
);
1122 spin_unlock(&tree
->lock
);
1123 BUG_ON(offset
!= entry
->offset
);
1125 /* try to allocate swap cache page */
1126 switch (zswap_get_swap_cache_page(swpentry
, &page
)) {
1127 case ZSWAP_SWAPCACHE_FAIL
: /* no memory or invalidate happened */
1131 case ZSWAP_SWAPCACHE_EXIST
:
1132 /* page is already in the swap cache, ignore for now */
1133 page_cache_release(page
);
1137 case ZSWAP_SWAPCACHE_NEW
: /* page is locked */
1140 src
= (u8
*)zpool_map_handle(entry
->pool
->zpool
, entry
->handle
,
1141 ZPOOL_MM_RO
) + sizeof(struct zswap_header
);
1142 dst
= kmap_atomic(page
);
1143 tfm
= *get_cpu_ptr(entry
->pool
->tfm
);
1144 ret
= crypto_comp_decompress(tfm
, src
, entry
->length
,
1146 put_cpu_ptr(entry
->pool
->tfm
);
1148 zpool_unmap_handle(entry
->pool
->zpool
, entry
->handle
);
1150 BUG_ON(dlen
!= PAGE_SIZE
);
1152 /* page is up to date */
1153 SetPageUptodate(page
);
1156 /* move it to the tail of the inactive list after end_writeback */
1157 SetPageReclaim(page
);
1159 /* start writeback */
1160 __swap_writepage(page
, &wbc
, end_swap_bio_write
);
1161 page_cache_release(page
);
1162 zswap_written_back_pages
++;
1164 spin_lock(&tree
->lock
);
1165 /* drop local reference */
1166 zswap_entry_put(tree
, entry
);
1169 * There are two possible situations for entry here:
1170 * (1) refcount is 1(normal case), entry is valid and on the tree
1171 * (2) refcount is 0, entry is freed and not on the tree
1172 * because invalidate happened during writeback
1173 * search the tree and free the entry if find entry
1175 if (entry
== zswap_rb_search(&tree
->rbroot
, offset
))
1176 zswap_entry_put(tree
, entry
);
1177 spin_unlock(&tree
->lock
);
1182 * if we get here due to ZSWAP_SWAPCACHE_EXIST
1183 * a load may happening concurrently
1184 * it is safe and okay to not free the entry
1185 * if we free the entry in the following put
1186 * it it either okay to return !0
1189 spin_lock(&tree
->lock
);
1190 zswap_entry_put(tree
, entry
);
1191 spin_unlock(&tree
->lock
);
1197 static int zswap_shrink(void)
1199 struct zswap_pool
*pool
;
1202 pool
= zswap_pool_last_get();
1206 ret
= zpool_shrink(pool
->zpool
, 1, NULL
);
1208 zswap_pool_put(pool
);
1213 static int zswap_writeback_entry(struct zpool
*pool
, unsigned long handle
)
1218 static int zswap_shrink(void)
1222 #endif /* CONFIG_ZSWAP_ENABLE_WRITEBACK */
1224 static int page_zero_filled(void *ptr
)
1227 unsigned long *page
;
1229 page
= (unsigned long *)ptr
;
1231 for (pos
= 0; pos
!= PAGE_SIZE
/ sizeof(*page
); pos
++) {
1239 /*********************************
1241 **********************************/
1242 /* attempts to compress and store an single page */
1243 static int zswap_frontswap_store(unsigned type
, pgoff_t offset
,
1246 struct zswap_tree
*tree
= zswap_trees
[type
];
1247 struct zswap_entry
*entry
, *dupentry
;
1248 struct crypto_comp
*tfm
;
1250 unsigned int dlen
= PAGE_SIZE
, len
;
1251 unsigned long handle
;
1254 #ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
1255 struct zswap_header
*zhdr
;
1257 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1258 struct zswap_handle
*zhandle
= NULL
, *duphandle
= NULL
;
1262 if (!zswap_enabled
|| !tree
) {
1267 /* if this page got EIO on pageout before, give up immediately */
1268 if (PageError(page
)) {
1273 /* reclaim space if needed */
1274 if (zswap_is_full()) {
1275 zswap_pool_limit_hit
++;
1276 if (zswap_shrink()) {
1277 zswap_reject_reclaim_fail
++;
1283 /* allocate entry */
1284 entry
= zswap_entry_cache_alloc(GFP_KERNEL
);
1286 zswap_reject_kmemcache_fail
++;
1291 /* if entry is successfully added, it keeps the reference */
1292 entry
->pool
= zswap_pool_current_get();
1299 src
= kmap_atomic(page
);
1300 if (page_zero_filled(src
)) {
1301 atomic_inc(&zswap_zero_pages
);
1302 entry
->zero_flag
= 1;
1305 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1306 entry
->offset
= offset
;
1314 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1315 checksum
= jhash2((const u32
*)src
, PAGE_SIZE
/ 4, 17);
1316 spin_lock(&tree
->lock
);
1317 zhandle
= zswap_same_page_search(entry
->pool
, tree
, src
, checksum
);
1319 entry
->offset
= offset
;
1320 entry
->zhandle
= zhandle
;
1321 entry
->zhandle
->ref_count
++;
1322 spin_unlock(&tree
->lock
);
1324 atomic_inc(&zswap_duplicate_pages
);
1327 spin_unlock(&tree
->lock
);
1331 dst
= get_cpu_var(zswap_dstmem
);
1332 tfm
= *get_cpu_ptr(entry
->pool
->tfm
);
1333 ret
= crypto_comp_compress(tfm
, src
, PAGE_SIZE
, dst
, &dlen
);
1335 put_cpu_ptr(entry
->pool
->tfm
);
1342 if (dlen
> PAGE_SIZE
)
1345 #ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
1346 len
+= sizeof(struct zswap_header
);
1348 ret
= zpool_malloc(entry
->pool
->zpool
, len
,
1349 __GFP_NORETRY
| __GFP_NOWARN
| __GFP_KSWAPD_RECLAIM
,
1351 if (ret
== -ENOSPC
) {
1352 zswap_reject_compress_poor
++;
1356 zswap_reject_alloc_fail
++;
1359 #ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
1360 zhdr
= zpool_map_handle(entry
->pool
->zpool
, handle
, ZPOOL_MM_RW
);
1361 zhdr
->swpentry
= swp_entry(type
, offset
);
1362 buf
= (u8
*)(zhdr
+ 1);
1363 memcpy(buf
, dst
, dlen
);
1365 buf
= (u8
*)zpool_map_handle(entry
->pool
->zpool
, handle
, ZPOOL_MM_RW
);
1366 if (dlen
== PAGE_SIZE
) {
1367 src
= kmap_atomic(page
);
1368 copy_page(buf
, src
);
1371 memcpy(buf
, dst
, dlen
);
1373 zpool_unmap_handle(entry
->pool
->zpool
, handle
);
1374 put_cpu_var(zswap_dstmem
);
1376 #ifndef CONFIG_ZSWAP_SAME_PAGE_SHARING
1379 /* populate entry */
1380 entry
->offset
= offset
;
1381 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1382 zhandle
= zswap_handle_cache_alloc(GFP_KERNEL
);
1387 entry
->zhandle
= zhandle
;
1388 entry
->zhandle
->handle
= handle
;
1389 entry
->zhandle
->length
= dlen
;
1390 entry
->zhandle
->checksum
= checksum
;
1392 entry
->handle
= handle
;
1393 entry
->length
= dlen
;
1396 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1397 spin_lock(&tree
->lock
);
1398 ret
= zswap_handle_rb_insert(&tree
->zhandleroot
,
1399 entry
->zhandle
, &duphandle
);
1400 spin_unlock(&tree
->lock
);
1405 spin_lock(&tree
->lock
);
1407 ret
= zswap_rb_insert(&tree
->rbroot
, entry
, &dupentry
);
1408 if (ret
== -EEXIST
) {
1409 zswap_duplicate_entry
++;
1410 /* remove from rbtree */
1411 zswap_rb_erase(&tree
->rbroot
, dupentry
);
1412 zswap_entry_put(tree
, dupentry
);
1414 } while (ret
== -EEXIST
);
1415 spin_unlock(&tree
->lock
);
1418 atomic_inc(&zswap_stored_pages
);
1419 zswap_update_total_size();
1424 put_cpu_var(zswap_dstmem
);
1425 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1428 zswap_pool_put(entry
->pool
);
1430 zswap_entry_cache_free(entry
);
1435 static void hexdump(char *title
, u8
*data
, int len
)
1439 printk("%s: length = %d @ %p\n", title
, len
, data
);
1440 for (i
= 0; i
< len
; i
++) {
1441 printk("%02x ", data
[i
]);
1442 if ((i
& 0xf) == 0xf)
1448 static void check_single_bitflip(struct zswap_entry
*entry
,
1449 unsigned char *src
, size_t src_size
, unsigned char *dst
)
1451 struct crypto_comp
*tfm
;
1453 int byte_offset
, bit_offset
, type
;
1454 int i
, j
, ret
, success
= 0;
1457 for (i
= 0; i
< src_size
; i
++) {
1458 for (j
= 0; j
< 8; j
++) {
1459 unsigned char org_byte
= src
[i
];
1460 unsigned char tmp_byte
= src
[i
];
1462 if (((tmp_byte
>> j
) & 0x1) == 0x1)
1463 tmp_byte
&= ~(1 << j
);
1465 tmp_byte
|= (1 << j
);
1469 tfm
= *get_cpu_ptr(entry
->pool
->tfm
);
1470 ret
= crypto_comp_decompress(tfm
, src
, src_size
, dst
, &dlen
);
1471 put_cpu_ptr(entry
->pool
->tfm
);
1475 type
= ((org_byte
>> j
) & 0x1);
1476 byte_offset
= (i
/ 4) * 4;
1477 bit_offset
= ((i
% 4) * 8) + j
;
1487 sprintf(debug_msg
, "%02d/0x%16p/%02d/%d", success
, src
+ byte_offset
, bit_offset
, type
);
1489 sprintf(debug_msg
, "%02d/0x%16p", success
, src
);
1491 sec_debug_set_extra_info_zswap(debug_msg
);
1497 * returns 0 if the page was successfully decompressed
1498 * return -1 on entry not found or error
1500 static int zswap_frontswap_load(unsigned type
, pgoff_t offset
,
1503 struct zswap_tree
*tree
= zswap_trees
[type
];
1504 struct zswap_entry
*entry
;
1505 struct crypto_comp
*tfm
;
1511 spin_lock(&tree
->lock
);
1512 entry
= zswap_entry_find_get(&tree
->rbroot
, offset
);
1514 /* entry was written back */
1515 spin_unlock(&tree
->lock
);
1518 spin_unlock(&tree
->lock
);
1520 if (entry
->zero_flag
== 1) {
1521 dst
= kmap_atomic(page
);
1522 memset(dst
, 0, PAGE_SIZE
);
1529 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1530 src
= (u8
*)zpool_map_handle(entry
->pool
->zpool
, entry
->zhandle
->handle
,
1533 src
= (u8
*)zpool_map_handle(entry
->pool
->zpool
, entry
->handle
,
1536 dst
= kmap_atomic(page
);
1537 #ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
1538 src
+= sizeof(struct zswap_header
);
1539 tfm
= *get_cpu_ptr(entry
->pool
->tfm
);
1540 ret
= crypto_comp_decompress(tfm
, src
, entry
->length
, dst
, &dlen
);
1541 put_cpu_ptr(entry
->pool
->tfm
);
1543 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1544 if (entry
->zhandle
->length
== PAGE_SIZE
)
1545 copy_page(dst
, src
);
1547 tfm
= *get_cpu_ptr(entry
->pool
->tfm
);
1548 ret
= crypto_comp_decompress(tfm
, src
, entry
->zhandle
->length
, dst
, &dlen
);
1549 put_cpu_ptr(entry
->pool
->tfm
);
1552 if (entry
->length
== PAGE_SIZE
)
1553 copy_page(dst
, src
);
1555 tfm
= *get_cpu_ptr(entry
->pool
->tfm
);
1556 ret
= crypto_comp_decompress(tfm
, src
, entry
->length
, dst
, &dlen
);
1557 put_cpu_ptr(entry
->pool
->tfm
);
1562 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1563 hexdump("src buffer", src
, entry
->zhandle
->length
);
1565 hexdump("src buffer", src
, entry
->length
);
1568 hexdump("dest buffer", dst
, dlen
);
1569 printk("zswap_comp_op returned %d\n", ret
);
1570 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1571 check_single_bitflip(entry
, src
, entry
->zhandle
->length
, dst
);
1573 check_single_bitflip(entry
, src
, entry
->length
, dst
);
1578 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1579 zpool_unmap_handle(entry
->pool
->zpool
, entry
->zhandle
->handle
);
1581 zpool_unmap_handle(entry
->pool
->zpool
, entry
->handle
);
1586 spin_lock(&tree
->lock
);
1587 zswap_entry_put(tree
, entry
);
1588 spin_unlock(&tree
->lock
);
1593 /* frees an entry in zswap */
1594 static void zswap_frontswap_invalidate_page(unsigned type
, pgoff_t offset
)
1596 struct zswap_tree
*tree
= zswap_trees
[type
];
1597 struct zswap_entry
*entry
;
1600 spin_lock(&tree
->lock
);
1601 entry
= zswap_rb_search(&tree
->rbroot
, offset
);
1603 /* entry was written back */
1604 spin_unlock(&tree
->lock
);
1608 /* remove from rbtree */
1609 zswap_rb_erase(&tree
->rbroot
, entry
);
1611 /* drop the initial reference from entry creation */
1612 zswap_entry_put(tree
, entry
);
1614 spin_unlock(&tree
->lock
);
1617 /* frees all zswap entries for the given swap type */
1618 static void zswap_frontswap_invalidate_area(unsigned type
)
1620 struct zswap_tree
*tree
= zswap_trees
[type
];
1621 struct zswap_entry
*entry
, *n
;
1626 /* walk the tree and free everything */
1627 spin_lock(&tree
->lock
);
1628 rbtree_postorder_for_each_entry_safe(entry
, n
, &tree
->rbroot
, rbnode
)
1629 zswap_free_entry(entry
);
1630 tree
->rbroot
= RB_ROOT
;
1631 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1632 tree
->zhandleroot
= RB_ROOT
;
1634 spin_unlock(&tree
->lock
);
1635 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1636 free_page((unsigned long)tree
->buffer
);
1639 zswap_trees
[type
] = NULL
;
1642 static void zswap_frontswap_init(unsigned type
)
1644 struct zswap_tree
*tree
;
1646 tree
= kzalloc(sizeof(struct zswap_tree
), GFP_KERNEL
);
1648 pr_err("alloc failed, zswap disabled for swap type %d\n", type
);
1651 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1652 tree
->buffer
= (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
1653 if (!tree
->buffer
) {
1654 pr_err("zswap: Error allocating compressor buffer\n");
1658 tree
->zhandleroot
= RB_ROOT
;
1661 tree
->rbroot
= RB_ROOT
;
1662 spin_lock_init(&tree
->lock
);
1663 zswap_trees
[type
] = tree
;
1666 static struct frontswap_ops zswap_frontswap_ops
= {
1667 .store
= zswap_frontswap_store
,
1668 .load
= zswap_frontswap_load
,
1669 .invalidate_page
= zswap_frontswap_invalidate_page
,
1670 .invalidate_area
= zswap_frontswap_invalidate_area
,
1671 .init
= zswap_frontswap_init
1674 /*********************************
1676 **********************************/
1677 #ifdef CONFIG_DEBUG_FS
1678 #include <linux/debugfs.h>
1680 static struct dentry
*zswap_debugfs_root
;
1682 static int __init
zswap_debugfs_init(void)
1684 if (!debugfs_initialized())
1687 zswap_debugfs_root
= debugfs_create_dir("zswap", NULL
);
1688 if (!zswap_debugfs_root
)
1691 debugfs_create_u64("pool_limit_hit", S_IRUGO
,
1692 zswap_debugfs_root
, &zswap_pool_limit_hit
);
1693 debugfs_create_u64("reject_reclaim_fail", S_IRUGO
,
1694 zswap_debugfs_root
, &zswap_reject_reclaim_fail
);
1695 debugfs_create_u64("reject_alloc_fail", S_IRUGO
,
1696 zswap_debugfs_root
, &zswap_reject_alloc_fail
);
1697 debugfs_create_u64("reject_kmemcache_fail", S_IRUGO
,
1698 zswap_debugfs_root
, &zswap_reject_kmemcache_fail
);
1699 debugfs_create_u64("reject_compress_poor", S_IRUGO
,
1700 zswap_debugfs_root
, &zswap_reject_compress_poor
);
1701 debugfs_create_u64("written_back_pages", S_IRUGO
,
1702 zswap_debugfs_root
, &zswap_written_back_pages
);
1703 debugfs_create_u64("duplicate_entry", S_IRUGO
,
1704 zswap_debugfs_root
, &zswap_duplicate_entry
);
1705 debugfs_create_u64("pool_total_size", S_IRUGO
,
1706 zswap_debugfs_root
, &zswap_pool_total_size
);
1707 debugfs_create_u64("pool_pages", S_IRUGO
,
1708 zswap_debugfs_root
, &zswap_pool_pages
);
1709 debugfs_create_atomic_t("stored_pages", S_IRUGO
,
1710 zswap_debugfs_root
, &zswap_stored_pages
);
1711 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1712 debugfs_create_atomic_t("duplicate_pages", S_IRUGO
,
1713 zswap_debugfs_root
, &zswap_duplicate_pages
);
1715 debugfs_create_atomic_t("zero_pages", S_IRUGO
,
1716 zswap_debugfs_root
, &zswap_zero_pages
);
1721 static void __exit
zswap_debugfs_exit(void)
1723 debugfs_remove_recursive(zswap_debugfs_root
);
1726 static int __init
zswap_debugfs_init(void)
1731 static void __exit
zswap_debugfs_exit(void) { }
1734 /*********************************
1735 * module init and exit
1736 **********************************/
1737 static int __init
init_zswap(void)
1739 struct zswap_pool
*pool
;
1741 zswap_init_started
= true;
1743 if (zswap_entry_cache_create()) {
1744 pr_err("entry cache creation failed\n");
1748 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1749 if (zswap_handle_cache_create()) {
1750 pr_err("handle cache creation failed\n");
1751 goto handlecachefail
;
1754 if (zswap_cpu_dstmem_init()) {
1755 pr_err("dstmem alloc failed\n");
1759 pool
= __zswap_pool_create_fallback();
1761 pr_err("pool creation failed\n");
1764 pr_info("loaded using pool %s/%s\n", pool
->tfm_name
,
1765 zpool_get_type(pool
->zpool
));
1767 list_add(&pool
->list
, &zswap_pools
);
1769 frontswap_register_ops(&zswap_frontswap_ops
);
1770 if (zswap_debugfs_init())
1771 pr_warn("debugfs initialization failed\n");
1775 zswap_cpu_dstmem_destroy();
1777 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1778 zswap_handle_cache_destroy();
1781 zswap_entry_cache_destroy();
1783 /* if built-in, we aren't unloaded on failure; don't allow use */
1784 zswap_init_failed
= true;
1785 zswap_enabled
= false;
1788 /* must be late so crypto has time to come up */
1789 late_initcall(init_zswap
);
1791 MODULE_LICENSE("GPL");
1792 MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
1793 MODULE_DESCRIPTION("Compressed cache for swap pages");