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>
45 /*********************************
47 **********************************/
48 /* Total bytes used by the compressed storage */
49 static u64 zswap_pool_total_size
;
50 /* The number of compressed pages currently stored in zswap */
51 static atomic_t zswap_stored_pages
= ATOMIC_INIT(0);
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.
60 /* Pool limit was hit (see zswap_max_pool_percent) */
61 static u64 zswap_pool_limit_hit
;
62 /* Pages written back when pool limit was reached */
63 static u64 zswap_written_back_pages
;
64 /* Store failed due to a reclaim failure after pool limit was reached */
65 static u64 zswap_reject_reclaim_fail
;
66 /* Compressed page was too big for the allocator to (optimally) store */
67 static u64 zswap_reject_compress_poor
;
68 /* Store failed because underlying allocator could not get memory */
69 static u64 zswap_reject_alloc_fail
;
70 /* Store failed because the entry metadata could not be allocated (rare) */
71 static u64 zswap_reject_kmemcache_fail
;
72 /* Duplicate store was encountered (rare) */
73 static u64 zswap_duplicate_entry
;
75 /*********************************
77 **********************************/
78 /* Enable/disable zswap (disabled by default, fixed at boot for now) */
79 static bool zswap_enabled __read_mostly
;
80 module_param_named(enabled
, zswap_enabled
, bool, 0444);
82 /* Compressor to be used by zswap (fixed at boot for now) */
83 #define ZSWAP_COMPRESSOR_DEFAULT "lzo"
84 static char *zswap_compressor
= ZSWAP_COMPRESSOR_DEFAULT
;
85 module_param_named(compressor
, zswap_compressor
, charp
, 0444);
87 /* The maximum percentage of memory that the compressed pool can occupy */
88 static unsigned int zswap_max_pool_percent
= 20;
89 module_param_named(max_pool_percent
,
90 zswap_max_pool_percent
, uint
, 0644);
92 /* Compressed storage to use */
93 #define ZSWAP_ZPOOL_DEFAULT "zbud"
94 static char *zswap_zpool_type
= ZSWAP_ZPOOL_DEFAULT
;
95 module_param_named(zpool
, zswap_zpool_type
, charp
, 0444);
97 /* zpool is shared by all of zswap backend */
98 static struct zpool
*zswap_pool
;
100 /*********************************
101 * compression functions
102 **********************************/
103 /* per-cpu compression transforms */
104 static struct crypto_comp
* __percpu
*zswap_comp_pcpu_tfms
;
107 ZSWAP_COMPOP_COMPRESS
,
108 ZSWAP_COMPOP_DECOMPRESS
111 static int zswap_comp_op(enum comp_op op
, const u8
*src
, unsigned int slen
,
112 u8
*dst
, unsigned int *dlen
)
114 struct crypto_comp
*tfm
;
117 tfm
= *per_cpu_ptr(zswap_comp_pcpu_tfms
, get_cpu());
119 case ZSWAP_COMPOP_COMPRESS
:
120 ret
= crypto_comp_compress(tfm
, src
, slen
, dst
, dlen
);
122 case ZSWAP_COMPOP_DECOMPRESS
:
123 ret
= crypto_comp_decompress(tfm
, src
, slen
, dst
, dlen
);
133 static int __init
zswap_comp_init(void)
135 if (!crypto_has_comp(zswap_compressor
, 0, 0)) {
136 pr_info("%s compressor not available\n", zswap_compressor
);
137 /* fall back to default compressor */
138 zswap_compressor
= ZSWAP_COMPRESSOR_DEFAULT
;
139 if (!crypto_has_comp(zswap_compressor
, 0, 0))
140 /* can't even load the default compressor */
143 pr_info("using %s compressor\n", zswap_compressor
);
145 /* alloc percpu transforms */
146 zswap_comp_pcpu_tfms
= alloc_percpu(struct crypto_comp
*);
147 if (!zswap_comp_pcpu_tfms
)
152 static void __init
zswap_comp_exit(void)
154 /* free percpu transforms */
155 free_percpu(zswap_comp_pcpu_tfms
);
158 /*********************************
160 **********************************/
164 * This structure contains the metadata for tracking a single compressed
167 * rbnode - links the entry into red-black tree for the appropriate swap type
168 * refcount - the number of outstanding reference to the entry. This is needed
169 * to protect against premature freeing of the entry by code
170 * concurrent calls to load, invalidate, and writeback. The lock
171 * for the zswap_tree structure that contains the entry must
172 * be held while changing the refcount. Since the lock must
173 * be held, there is no reason to also make refcount atomic.
174 * offset - the swap offset for the entry. Index into the red-black tree.
175 * handle - zpool allocation handle that stores the compressed page data
176 * length - the length in bytes of the compressed page data. Needed during
180 struct rb_node rbnode
;
184 unsigned long handle
;
187 struct zswap_header
{
188 swp_entry_t swpentry
;
192 * The tree lock in the zswap_tree struct protects a few things:
194 * - the refcount field of each entry in the tree
197 struct rb_root rbroot
;
201 static struct zswap_tree
*zswap_trees
[MAX_SWAPFILES
];
203 /*********************************
204 * zswap entry functions
205 **********************************/
206 static struct kmem_cache
*zswap_entry_cache
;
208 static int __init
zswap_entry_cache_create(void)
210 zswap_entry_cache
= KMEM_CACHE(zswap_entry
, 0);
211 return zswap_entry_cache
== NULL
;
214 static void __init
zswap_entry_cache_destroy(void)
216 kmem_cache_destroy(zswap_entry_cache
);
219 static struct zswap_entry
*zswap_entry_cache_alloc(gfp_t gfp
)
221 struct zswap_entry
*entry
;
222 entry
= kmem_cache_alloc(zswap_entry_cache
, gfp
);
226 RB_CLEAR_NODE(&entry
->rbnode
);
230 static void zswap_entry_cache_free(struct zswap_entry
*entry
)
232 kmem_cache_free(zswap_entry_cache
, entry
);
235 /*********************************
237 **********************************/
238 static struct zswap_entry
*zswap_rb_search(struct rb_root
*root
, pgoff_t offset
)
240 struct rb_node
*node
= root
->rb_node
;
241 struct zswap_entry
*entry
;
244 entry
= rb_entry(node
, struct zswap_entry
, rbnode
);
245 if (entry
->offset
> offset
)
246 node
= node
->rb_left
;
247 else if (entry
->offset
< offset
)
248 node
= node
->rb_right
;
256 * In the case that a entry with the same offset is found, a pointer to
257 * the existing entry is stored in dupentry and the function returns -EEXIST
259 static int zswap_rb_insert(struct rb_root
*root
, struct zswap_entry
*entry
,
260 struct zswap_entry
**dupentry
)
262 struct rb_node
**link
= &root
->rb_node
, *parent
= NULL
;
263 struct zswap_entry
*myentry
;
267 myentry
= rb_entry(parent
, struct zswap_entry
, rbnode
);
268 if (myentry
->offset
> entry
->offset
)
269 link
= &(*link
)->rb_left
;
270 else if (myentry
->offset
< entry
->offset
)
271 link
= &(*link
)->rb_right
;
277 rb_link_node(&entry
->rbnode
, parent
, link
);
278 rb_insert_color(&entry
->rbnode
, root
);
282 static void zswap_rb_erase(struct rb_root
*root
, struct zswap_entry
*entry
)
284 if (!RB_EMPTY_NODE(&entry
->rbnode
)) {
285 rb_erase(&entry
->rbnode
, root
);
286 RB_CLEAR_NODE(&entry
->rbnode
);
291 * Carries out the common pattern of freeing and entry's zpool allocation,
292 * freeing the entry itself, and decrementing the number of stored pages.
294 static void zswap_free_entry(struct zswap_entry
*entry
)
296 zpool_free(zswap_pool
, entry
->handle
);
297 zswap_entry_cache_free(entry
);
298 atomic_dec(&zswap_stored_pages
);
299 zswap_pool_total_size
= zpool_get_total_size(zswap_pool
);
302 /* caller must hold the tree lock */
303 static void zswap_entry_get(struct zswap_entry
*entry
)
308 /* caller must hold the tree lock
309 * remove from the tree and free it, if nobody reference the entry
311 static void zswap_entry_put(struct zswap_tree
*tree
,
312 struct zswap_entry
*entry
)
314 int refcount
= --entry
->refcount
;
316 BUG_ON(refcount
< 0);
318 zswap_rb_erase(&tree
->rbroot
, entry
);
319 zswap_free_entry(entry
);
323 /* caller must hold the tree lock */
324 static struct zswap_entry
*zswap_entry_find_get(struct rb_root
*root
,
327 struct zswap_entry
*entry
= NULL
;
329 entry
= zswap_rb_search(root
, offset
);
331 zswap_entry_get(entry
);
336 /*********************************
338 **********************************/
339 static DEFINE_PER_CPU(u8
*, zswap_dstmem
);
341 static int __zswap_cpu_notifier(unsigned long action
, unsigned long cpu
)
343 struct crypto_comp
*tfm
;
348 tfm
= crypto_alloc_comp(zswap_compressor
, 0, 0);
350 pr_err("can't allocate compressor transform\n");
353 *per_cpu_ptr(zswap_comp_pcpu_tfms
, cpu
) = tfm
;
354 dst
= kmalloc_node(PAGE_SIZE
* 2, GFP_KERNEL
, cpu_to_node(cpu
));
356 pr_err("can't allocate compressor buffer\n");
357 crypto_free_comp(tfm
);
358 *per_cpu_ptr(zswap_comp_pcpu_tfms
, cpu
) = NULL
;
361 per_cpu(zswap_dstmem
, cpu
) = dst
;
364 case CPU_UP_CANCELED
:
365 tfm
= *per_cpu_ptr(zswap_comp_pcpu_tfms
, cpu
);
367 crypto_free_comp(tfm
);
368 *per_cpu_ptr(zswap_comp_pcpu_tfms
, cpu
) = NULL
;
370 dst
= per_cpu(zswap_dstmem
, cpu
);
372 per_cpu(zswap_dstmem
, cpu
) = NULL
;
380 static int zswap_cpu_notifier(struct notifier_block
*nb
,
381 unsigned long action
, void *pcpu
)
383 unsigned long cpu
= (unsigned long)pcpu
;
384 return __zswap_cpu_notifier(action
, cpu
);
387 static struct notifier_block zswap_cpu_notifier_block
= {
388 .notifier_call
= zswap_cpu_notifier
391 static int __init
zswap_cpu_init(void)
395 cpu_notifier_register_begin();
396 for_each_online_cpu(cpu
)
397 if (__zswap_cpu_notifier(CPU_UP_PREPARE
, cpu
) != NOTIFY_OK
)
399 __register_cpu_notifier(&zswap_cpu_notifier_block
);
400 cpu_notifier_register_done();
404 for_each_online_cpu(cpu
)
405 __zswap_cpu_notifier(CPU_UP_CANCELED
, cpu
);
406 cpu_notifier_register_done();
410 /*********************************
412 **********************************/
413 static bool zswap_is_full(void)
415 return totalram_pages
* zswap_max_pool_percent
/ 100 <
416 DIV_ROUND_UP(zswap_pool_total_size
, PAGE_SIZE
);
419 /*********************************
421 **********************************/
422 /* return enum for zswap_get_swap_cache_page */
423 enum zswap_get_swap_ret
{
425 ZSWAP_SWAPCACHE_EXIST
,
426 ZSWAP_SWAPCACHE_FAIL
,
430 * zswap_get_swap_cache_page
432 * This is an adaption of read_swap_cache_async()
434 * This function tries to find a page with the given swap entry
435 * in the swapper_space address space (the swap cache). If the page
436 * is found, it is returned in retpage. Otherwise, a page is allocated,
437 * added to the swap cache, and returned in retpage.
439 * If success, the swap cache page is returned in retpage
440 * Returns ZSWAP_SWAPCACHE_EXIST if page was already in the swap cache
441 * Returns ZSWAP_SWAPCACHE_NEW if the new page needs to be populated,
442 * the new page is added to swapcache and locked
443 * Returns ZSWAP_SWAPCACHE_FAIL on error
445 static int zswap_get_swap_cache_page(swp_entry_t entry
,
446 struct page
**retpage
)
448 struct page
*found_page
, *new_page
= NULL
;
449 struct address_space
*swapper_space
= swap_address_space(entry
);
455 * First check the swap cache. Since this is normally
456 * called after lookup_swap_cache() failed, re-calling
457 * that would confuse statistics.
459 found_page
= find_get_page(swapper_space
, entry
.val
);
464 * Get a new page to read into from swap.
467 new_page
= alloc_page(GFP_KERNEL
);
469 break; /* Out of memory */
473 * call radix_tree_preload() while we can wait.
475 err
= radix_tree_preload(GFP_KERNEL
);
480 * Swap entry may have been freed since our caller observed it.
482 err
= swapcache_prepare(entry
);
483 if (err
== -EEXIST
) { /* seems racy */
484 radix_tree_preload_end();
487 if (err
) { /* swp entry is obsolete ? */
488 radix_tree_preload_end();
492 /* May fail (-ENOMEM) if radix-tree node allocation failed. */
493 __set_page_locked(new_page
);
494 SetPageSwapBacked(new_page
);
495 err
= __add_to_swap_cache(new_page
, entry
);
497 radix_tree_preload_end();
498 lru_cache_add_anon(new_page
);
500 return ZSWAP_SWAPCACHE_NEW
;
502 radix_tree_preload_end();
503 ClearPageSwapBacked(new_page
);
504 __clear_page_locked(new_page
);
506 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
507 * clear SWAP_HAS_CACHE flag.
509 swapcache_free(entry
);
510 } while (err
!= -ENOMEM
);
513 page_cache_release(new_page
);
515 return ZSWAP_SWAPCACHE_FAIL
;
516 *retpage
= found_page
;
517 return ZSWAP_SWAPCACHE_EXIST
;
521 * Attempts to free an entry by adding a page to the swap cache,
522 * decompressing the entry data into the page, and issuing a
523 * bio write to write the page back to the swap device.
525 * This can be thought of as a "resumed writeback" of the page
526 * to the swap device. We are basically resuming the same swap
527 * writeback path that was intercepted with the frontswap_store()
528 * in the first place. After the page has been decompressed into
529 * the swap cache, the compressed version stored by zswap can be
532 static int zswap_writeback_entry(struct zpool
*pool
, unsigned long handle
)
534 struct zswap_header
*zhdr
;
535 swp_entry_t swpentry
;
536 struct zswap_tree
*tree
;
538 struct zswap_entry
*entry
;
543 struct writeback_control wbc
= {
544 .sync_mode
= WB_SYNC_NONE
,
547 /* extract swpentry from data */
548 zhdr
= zpool_map_handle(pool
, handle
, ZPOOL_MM_RO
);
549 swpentry
= zhdr
->swpentry
; /* here */
550 zpool_unmap_handle(pool
, handle
);
551 tree
= zswap_trees
[swp_type(swpentry
)];
552 offset
= swp_offset(swpentry
);
554 /* find and ref zswap entry */
555 spin_lock(&tree
->lock
);
556 entry
= zswap_entry_find_get(&tree
->rbroot
, offset
);
558 /* entry was invalidated */
559 spin_unlock(&tree
->lock
);
562 spin_unlock(&tree
->lock
);
563 BUG_ON(offset
!= entry
->offset
);
565 /* try to allocate swap cache page */
566 switch (zswap_get_swap_cache_page(swpentry
, &page
)) {
567 case ZSWAP_SWAPCACHE_FAIL
: /* no memory or invalidate happened */
571 case ZSWAP_SWAPCACHE_EXIST
:
572 /* page is already in the swap cache, ignore for now */
573 page_cache_release(page
);
577 case ZSWAP_SWAPCACHE_NEW
: /* page is locked */
580 src
= (u8
*)zpool_map_handle(zswap_pool
, entry
->handle
,
581 ZPOOL_MM_RO
) + sizeof(struct zswap_header
);
582 dst
= kmap_atomic(page
);
583 ret
= zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS
, src
,
584 entry
->length
, dst
, &dlen
);
586 zpool_unmap_handle(zswap_pool
, entry
->handle
);
588 BUG_ON(dlen
!= PAGE_SIZE
);
590 /* page is up to date */
591 SetPageUptodate(page
);
594 /* move it to the tail of the inactive list after end_writeback */
595 SetPageReclaim(page
);
597 /* start writeback */
598 __swap_writepage(page
, &wbc
, end_swap_bio_write
);
599 page_cache_release(page
);
600 zswap_written_back_pages
++;
602 spin_lock(&tree
->lock
);
603 /* drop local reference */
604 zswap_entry_put(tree
, entry
);
607 * There are two possible situations for entry here:
608 * (1) refcount is 1(normal case), entry is valid and on the tree
609 * (2) refcount is 0, entry is freed and not on the tree
610 * because invalidate happened during writeback
611 * search the tree and free the entry if find entry
613 if (entry
== zswap_rb_search(&tree
->rbroot
, offset
))
614 zswap_entry_put(tree
, entry
);
615 spin_unlock(&tree
->lock
);
620 * if we get here due to ZSWAP_SWAPCACHE_EXIST
621 * a load may happening concurrently
622 * it is safe and okay to not free the entry
623 * if we free the entry in the following put
624 * it it either okay to return !0
627 spin_lock(&tree
->lock
);
628 zswap_entry_put(tree
, entry
);
629 spin_unlock(&tree
->lock
);
635 /*********************************
637 **********************************/
638 /* attempts to compress and store an single page */
639 static int zswap_frontswap_store(unsigned type
, pgoff_t offset
,
642 struct zswap_tree
*tree
= zswap_trees
[type
];
643 struct zswap_entry
*entry
, *dupentry
;
645 unsigned int dlen
= PAGE_SIZE
, len
;
646 unsigned long handle
;
649 struct zswap_header
*zhdr
;
656 /* reclaim space if needed */
657 if (zswap_is_full()) {
658 zswap_pool_limit_hit
++;
659 if (zpool_shrink(zswap_pool
, 1, NULL
)) {
660 zswap_reject_reclaim_fail
++;
667 entry
= zswap_entry_cache_alloc(GFP_KERNEL
);
669 zswap_reject_kmemcache_fail
++;
675 dst
= get_cpu_var(zswap_dstmem
);
676 src
= kmap_atomic(page
);
677 ret
= zswap_comp_op(ZSWAP_COMPOP_COMPRESS
, src
, PAGE_SIZE
, dst
, &dlen
);
685 len
= dlen
+ sizeof(struct zswap_header
);
686 ret
= zpool_malloc(zswap_pool
, len
, __GFP_NORETRY
| __GFP_NOWARN
,
688 if (ret
== -ENOSPC
) {
689 zswap_reject_compress_poor
++;
693 zswap_reject_alloc_fail
++;
696 zhdr
= zpool_map_handle(zswap_pool
, handle
, ZPOOL_MM_RW
);
697 zhdr
->swpentry
= swp_entry(type
, offset
);
698 buf
= (u8
*)(zhdr
+ 1);
699 memcpy(buf
, dst
, dlen
);
700 zpool_unmap_handle(zswap_pool
, handle
);
701 put_cpu_var(zswap_dstmem
);
704 entry
->offset
= offset
;
705 entry
->handle
= handle
;
706 entry
->length
= dlen
;
709 spin_lock(&tree
->lock
);
711 ret
= zswap_rb_insert(&tree
->rbroot
, entry
, &dupentry
);
712 if (ret
== -EEXIST
) {
713 zswap_duplicate_entry
++;
714 /* remove from rbtree */
715 zswap_rb_erase(&tree
->rbroot
, dupentry
);
716 zswap_entry_put(tree
, dupentry
);
718 } while (ret
== -EEXIST
);
719 spin_unlock(&tree
->lock
);
722 atomic_inc(&zswap_stored_pages
);
723 zswap_pool_total_size
= zpool_get_total_size(zswap_pool
);
728 put_cpu_var(zswap_dstmem
);
729 zswap_entry_cache_free(entry
);
735 * returns 0 if the page was successfully decompressed
736 * return -1 on entry not found or error
738 static int zswap_frontswap_load(unsigned type
, pgoff_t offset
,
741 struct zswap_tree
*tree
= zswap_trees
[type
];
742 struct zswap_entry
*entry
;
748 spin_lock(&tree
->lock
);
749 entry
= zswap_entry_find_get(&tree
->rbroot
, offset
);
751 /* entry was written back */
752 spin_unlock(&tree
->lock
);
755 spin_unlock(&tree
->lock
);
759 src
= (u8
*)zpool_map_handle(zswap_pool
, entry
->handle
,
760 ZPOOL_MM_RO
) + sizeof(struct zswap_header
);
761 dst
= kmap_atomic(page
);
762 ret
= zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS
, src
, entry
->length
,
765 zpool_unmap_handle(zswap_pool
, entry
->handle
);
768 spin_lock(&tree
->lock
);
769 zswap_entry_put(tree
, entry
);
770 spin_unlock(&tree
->lock
);
775 /* frees an entry in zswap */
776 static void zswap_frontswap_invalidate_page(unsigned type
, pgoff_t offset
)
778 struct zswap_tree
*tree
= zswap_trees
[type
];
779 struct zswap_entry
*entry
;
782 spin_lock(&tree
->lock
);
783 entry
= zswap_rb_search(&tree
->rbroot
, offset
);
785 /* entry was written back */
786 spin_unlock(&tree
->lock
);
790 /* remove from rbtree */
791 zswap_rb_erase(&tree
->rbroot
, entry
);
793 /* drop the initial reference from entry creation */
794 zswap_entry_put(tree
, entry
);
796 spin_unlock(&tree
->lock
);
799 /* frees all zswap entries for the given swap type */
800 static void zswap_frontswap_invalidate_area(unsigned type
)
802 struct zswap_tree
*tree
= zswap_trees
[type
];
803 struct zswap_entry
*entry
, *n
;
808 /* walk the tree and free everything */
809 spin_lock(&tree
->lock
);
810 rbtree_postorder_for_each_entry_safe(entry
, n
, &tree
->rbroot
, rbnode
)
811 zswap_free_entry(entry
);
812 tree
->rbroot
= RB_ROOT
;
813 spin_unlock(&tree
->lock
);
815 zswap_trees
[type
] = NULL
;
818 static struct zpool_ops zswap_zpool_ops
= {
819 .evict
= zswap_writeback_entry
822 static void zswap_frontswap_init(unsigned type
)
824 struct zswap_tree
*tree
;
826 tree
= kzalloc(sizeof(struct zswap_tree
), GFP_KERNEL
);
828 pr_err("alloc failed, zswap disabled for swap type %d\n", type
);
832 tree
->rbroot
= RB_ROOT
;
833 spin_lock_init(&tree
->lock
);
834 zswap_trees
[type
] = tree
;
837 static struct frontswap_ops zswap_frontswap_ops
= {
838 .store
= zswap_frontswap_store
,
839 .load
= zswap_frontswap_load
,
840 .invalidate_page
= zswap_frontswap_invalidate_page
,
841 .invalidate_area
= zswap_frontswap_invalidate_area
,
842 .init
= zswap_frontswap_init
845 /*********************************
847 **********************************/
848 #ifdef CONFIG_DEBUG_FS
849 #include <linux/debugfs.h>
851 static struct dentry
*zswap_debugfs_root
;
853 static int __init
zswap_debugfs_init(void)
855 if (!debugfs_initialized())
858 zswap_debugfs_root
= debugfs_create_dir("zswap", NULL
);
859 if (!zswap_debugfs_root
)
862 debugfs_create_u64("pool_limit_hit", S_IRUGO
,
863 zswap_debugfs_root
, &zswap_pool_limit_hit
);
864 debugfs_create_u64("reject_reclaim_fail", S_IRUGO
,
865 zswap_debugfs_root
, &zswap_reject_reclaim_fail
);
866 debugfs_create_u64("reject_alloc_fail", S_IRUGO
,
867 zswap_debugfs_root
, &zswap_reject_alloc_fail
);
868 debugfs_create_u64("reject_kmemcache_fail", S_IRUGO
,
869 zswap_debugfs_root
, &zswap_reject_kmemcache_fail
);
870 debugfs_create_u64("reject_compress_poor", S_IRUGO
,
871 zswap_debugfs_root
, &zswap_reject_compress_poor
);
872 debugfs_create_u64("written_back_pages", S_IRUGO
,
873 zswap_debugfs_root
, &zswap_written_back_pages
);
874 debugfs_create_u64("duplicate_entry", S_IRUGO
,
875 zswap_debugfs_root
, &zswap_duplicate_entry
);
876 debugfs_create_u64("pool_total_size", S_IRUGO
,
877 zswap_debugfs_root
, &zswap_pool_total_size
);
878 debugfs_create_atomic_t("stored_pages", S_IRUGO
,
879 zswap_debugfs_root
, &zswap_stored_pages
);
884 static void __exit
zswap_debugfs_exit(void)
886 debugfs_remove_recursive(zswap_debugfs_root
);
889 static int __init
zswap_debugfs_init(void)
894 static void __exit
zswap_debugfs_exit(void) { }
897 /*********************************
898 * module init and exit
899 **********************************/
900 static int __init
init_zswap(void)
902 gfp_t gfp
= __GFP_NORETRY
| __GFP_NOWARN
;
907 pr_info("loading zswap\n");
909 zswap_pool
= zpool_create_pool(zswap_zpool_type
, gfp
, &zswap_zpool_ops
);
910 if (!zswap_pool
&& strcmp(zswap_zpool_type
, ZSWAP_ZPOOL_DEFAULT
)) {
911 pr_info("%s zpool not available\n", zswap_zpool_type
);
912 zswap_zpool_type
= ZSWAP_ZPOOL_DEFAULT
;
913 zswap_pool
= zpool_create_pool(zswap_zpool_type
, gfp
,
917 pr_err("%s zpool not available\n", zswap_zpool_type
);
918 pr_err("zpool creation failed\n");
921 pr_info("using %s pool\n", zswap_zpool_type
);
923 if (zswap_entry_cache_create()) {
924 pr_err("entry cache creation failed\n");
927 if (zswap_comp_init()) {
928 pr_err("compressor initialization failed\n");
931 if (zswap_cpu_init()) {
932 pr_err("per-cpu initialization failed\n");
936 frontswap_register_ops(&zswap_frontswap_ops
);
937 if (zswap_debugfs_init())
938 pr_warn("debugfs initialization failed\n");
943 zswap_entry_cache_destroy();
945 zpool_destroy_pool(zswap_pool
);
949 /* must be late so crypto has time to come up */
950 late_initcall(init_zswap
);
952 MODULE_LICENSE("GPL");
953 MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
954 MODULE_DESCRIPTION("Compressed cache for swap pages");