Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1 | /* |
| 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 Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 37 | #include <linux/zpool.h> |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 38 | |
| 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 Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 48 | /* Total bytes used by the compressed storage */ |
| 49 | static u64 zswap_pool_total_size; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 50 | /* The number of compressed pages currently stored in zswap */ |
| 51 | static 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) */ |
| 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; |
| 74 | |
| 75 | /********************************* |
| 76 | * tunables |
| 77 | **********************************/ |
| 78 | /* Enable/disable zswap (disabled by default, fixed at boot for now) */ |
| 79 | static bool zswap_enabled __read_mostly; |
Dan Streetman | 12ab028 | 2014-01-23 15:52:48 -0800 | [diff] [blame] | 80 | module_param_named(enabled, zswap_enabled, bool, 0444); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 81 | |
| 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; |
Dan Streetman | 12ab028 | 2014-01-23 15:52:48 -0800 | [diff] [blame] | 85 | module_param_named(compressor, zswap_compressor, charp, 0444); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 86 | |
| 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); |
| 91 | |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 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); |
| 96 | |
| 97 | /* zpool is shared by all of zswap backend */ |
| 98 | static struct zpool *zswap_pool; |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 99 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 100 | /********************************* |
| 101 | * compression functions |
| 102 | **********************************/ |
| 103 | /* per-cpu compression transforms */ |
| 104 | static struct crypto_comp * __percpu *zswap_comp_pcpu_tfms; |
| 105 | |
| 106 | enum comp_op { |
| 107 | ZSWAP_COMPOP_COMPRESS, |
| 108 | ZSWAP_COMPOP_DECOMPRESS |
| 109 | }; |
| 110 | |
| 111 | static int zswap_comp_op(enum comp_op op, const u8 *src, unsigned int slen, |
| 112 | u8 *dst, unsigned int *dlen) |
| 113 | { |
| 114 | struct crypto_comp *tfm; |
| 115 | int ret; |
| 116 | |
| 117 | tfm = *per_cpu_ptr(zswap_comp_pcpu_tfms, get_cpu()); |
| 118 | switch (op) { |
| 119 | case ZSWAP_COMPOP_COMPRESS: |
| 120 | ret = crypto_comp_compress(tfm, src, slen, dst, dlen); |
| 121 | break; |
| 122 | case ZSWAP_COMPOP_DECOMPRESS: |
| 123 | ret = crypto_comp_decompress(tfm, src, slen, dst, dlen); |
| 124 | break; |
| 125 | default: |
| 126 | ret = -EINVAL; |
| 127 | } |
| 128 | |
| 129 | put_cpu(); |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | static int __init zswap_comp_init(void) |
| 134 | { |
| 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 */ |
| 141 | return -ENODEV; |
| 142 | } |
| 143 | pr_info("using %s compressor\n", zswap_compressor); |
| 144 | |
| 145 | /* alloc percpu transforms */ |
| 146 | zswap_comp_pcpu_tfms = alloc_percpu(struct crypto_comp *); |
| 147 | if (!zswap_comp_pcpu_tfms) |
| 148 | return -ENOMEM; |
| 149 | return 0; |
| 150 | } |
| 151 | |
Mahendran Ganesh | dd01d7d | 2014-12-12 16:57:15 -0800 | [diff] [blame] | 152 | static void __init zswap_comp_exit(void) |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 153 | { |
| 154 | /* free percpu transforms */ |
Markus Elfring | 442cc43 | 2014-12-12 16:57:18 -0800 | [diff] [blame^] | 155 | free_percpu(zswap_comp_pcpu_tfms); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /********************************* |
| 159 | * data structures |
| 160 | **********************************/ |
| 161 | /* |
| 162 | * struct zswap_entry |
| 163 | * |
| 164 | * This structure contains the metadata for tracking a single compressed |
| 165 | * page within zswap. |
| 166 | * |
| 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 |
SeongJae Park | 6b45251 | 2014-04-07 15:38:25 -0700 | [diff] [blame] | 170 | * concurrent calls to load, invalidate, and writeback. The lock |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 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. |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 175 | * handle - zpool allocation handle that stores the compressed page data |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 176 | * length - the length in bytes of the compressed page data. Needed during |
SeongJae Park | 6b45251 | 2014-04-07 15:38:25 -0700 | [diff] [blame] | 177 | * decompression |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 178 | */ |
| 179 | struct zswap_entry { |
| 180 | struct rb_node rbnode; |
| 181 | pgoff_t offset; |
| 182 | int refcount; |
| 183 | unsigned int length; |
| 184 | unsigned long handle; |
| 185 | }; |
| 186 | |
| 187 | struct zswap_header { |
| 188 | swp_entry_t swpentry; |
| 189 | }; |
| 190 | |
| 191 | /* |
| 192 | * The tree lock in the zswap_tree struct protects a few things: |
| 193 | * - the rbtree |
| 194 | * - the refcount field of each entry in the tree |
| 195 | */ |
| 196 | struct zswap_tree { |
| 197 | struct rb_root rbroot; |
| 198 | spinlock_t lock; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | static struct zswap_tree *zswap_trees[MAX_SWAPFILES]; |
| 202 | |
| 203 | /********************************* |
| 204 | * zswap entry functions |
| 205 | **********************************/ |
| 206 | static struct kmem_cache *zswap_entry_cache; |
| 207 | |
Mahendran Ganesh | dd01d7d | 2014-12-12 16:57:15 -0800 | [diff] [blame] | 208 | static int __init zswap_entry_cache_create(void) |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 209 | { |
| 210 | zswap_entry_cache = KMEM_CACHE(zswap_entry, 0); |
SeongJae Park | 5d2d42d | 2014-04-07 15:38:28 -0700 | [diff] [blame] | 211 | return zswap_entry_cache == NULL; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Fabian Frederick | c119239 | 2014-08-08 14:19:35 -0700 | [diff] [blame] | 214 | static void __init zswap_entry_cache_destroy(void) |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 215 | { |
| 216 | kmem_cache_destroy(zswap_entry_cache); |
| 217 | } |
| 218 | |
| 219 | static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp) |
| 220 | { |
| 221 | struct zswap_entry *entry; |
| 222 | entry = kmem_cache_alloc(zswap_entry_cache, gfp); |
| 223 | if (!entry) |
| 224 | return NULL; |
| 225 | entry->refcount = 1; |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 226 | RB_CLEAR_NODE(&entry->rbnode); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 227 | return entry; |
| 228 | } |
| 229 | |
| 230 | static void zswap_entry_cache_free(struct zswap_entry *entry) |
| 231 | { |
| 232 | kmem_cache_free(zswap_entry_cache, entry); |
| 233 | } |
| 234 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 235 | /********************************* |
| 236 | * rbtree functions |
| 237 | **********************************/ |
| 238 | static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset) |
| 239 | { |
| 240 | struct rb_node *node = root->rb_node; |
| 241 | struct zswap_entry *entry; |
| 242 | |
| 243 | while (node) { |
| 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; |
| 249 | else |
| 250 | return entry; |
| 251 | } |
| 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | /* |
| 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 |
| 258 | */ |
| 259 | static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry, |
| 260 | struct zswap_entry **dupentry) |
| 261 | { |
| 262 | struct rb_node **link = &root->rb_node, *parent = NULL; |
| 263 | struct zswap_entry *myentry; |
| 264 | |
| 265 | while (*link) { |
| 266 | parent = *link; |
| 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; |
| 272 | else { |
| 273 | *dupentry = myentry; |
| 274 | return -EEXIST; |
| 275 | } |
| 276 | } |
| 277 | rb_link_node(&entry->rbnode, parent, link); |
| 278 | rb_insert_color(&entry->rbnode, root); |
| 279 | return 0; |
| 280 | } |
| 281 | |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 282 | static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry) |
| 283 | { |
| 284 | if (!RB_EMPTY_NODE(&entry->rbnode)) { |
| 285 | rb_erase(&entry->rbnode, root); |
| 286 | RB_CLEAR_NODE(&entry->rbnode); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /* |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 291 | * Carries out the common pattern of freeing and entry's zpool allocation, |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 292 | * freeing the entry itself, and decrementing the number of stored pages. |
| 293 | */ |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 294 | static void zswap_free_entry(struct zswap_entry *entry) |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 295 | { |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 296 | zpool_free(zswap_pool, entry->handle); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 297 | zswap_entry_cache_free(entry); |
| 298 | atomic_dec(&zswap_stored_pages); |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 299 | zswap_pool_total_size = zpool_get_total_size(zswap_pool); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* caller must hold the tree lock */ |
| 303 | static void zswap_entry_get(struct zswap_entry *entry) |
| 304 | { |
| 305 | entry->refcount++; |
| 306 | } |
| 307 | |
| 308 | /* caller must hold the tree lock |
| 309 | * remove from the tree and free it, if nobody reference the entry |
| 310 | */ |
| 311 | static void zswap_entry_put(struct zswap_tree *tree, |
| 312 | struct zswap_entry *entry) |
| 313 | { |
| 314 | int refcount = --entry->refcount; |
| 315 | |
| 316 | BUG_ON(refcount < 0); |
| 317 | if (refcount == 0) { |
| 318 | zswap_rb_erase(&tree->rbroot, entry); |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 319 | zswap_free_entry(entry); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | /* caller must hold the tree lock */ |
| 324 | static struct zswap_entry *zswap_entry_find_get(struct rb_root *root, |
| 325 | pgoff_t offset) |
| 326 | { |
| 327 | struct zswap_entry *entry = NULL; |
| 328 | |
| 329 | entry = zswap_rb_search(root, offset); |
| 330 | if (entry) |
| 331 | zswap_entry_get(entry); |
| 332 | |
| 333 | return entry; |
| 334 | } |
| 335 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 336 | /********************************* |
| 337 | * per-cpu code |
| 338 | **********************************/ |
| 339 | static DEFINE_PER_CPU(u8 *, zswap_dstmem); |
| 340 | |
| 341 | static int __zswap_cpu_notifier(unsigned long action, unsigned long cpu) |
| 342 | { |
| 343 | struct crypto_comp *tfm; |
| 344 | u8 *dst; |
| 345 | |
| 346 | switch (action) { |
| 347 | case CPU_UP_PREPARE: |
| 348 | tfm = crypto_alloc_comp(zswap_compressor, 0, 0); |
| 349 | if (IS_ERR(tfm)) { |
| 350 | pr_err("can't allocate compressor transform\n"); |
| 351 | return NOTIFY_BAD; |
| 352 | } |
| 353 | *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = tfm; |
Eric Dumazet | 72d0963 | 2014-06-04 16:11:11 -0700 | [diff] [blame] | 354 | dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu)); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 355 | if (!dst) { |
| 356 | pr_err("can't allocate compressor buffer\n"); |
| 357 | crypto_free_comp(tfm); |
| 358 | *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = NULL; |
| 359 | return NOTIFY_BAD; |
| 360 | } |
| 361 | per_cpu(zswap_dstmem, cpu) = dst; |
| 362 | break; |
| 363 | case CPU_DEAD: |
| 364 | case CPU_UP_CANCELED: |
| 365 | tfm = *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu); |
| 366 | if (tfm) { |
| 367 | crypto_free_comp(tfm); |
| 368 | *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = NULL; |
| 369 | } |
| 370 | dst = per_cpu(zswap_dstmem, cpu); |
| 371 | kfree(dst); |
| 372 | per_cpu(zswap_dstmem, cpu) = NULL; |
| 373 | break; |
| 374 | default: |
| 375 | break; |
| 376 | } |
| 377 | return NOTIFY_OK; |
| 378 | } |
| 379 | |
| 380 | static int zswap_cpu_notifier(struct notifier_block *nb, |
| 381 | unsigned long action, void *pcpu) |
| 382 | { |
| 383 | unsigned long cpu = (unsigned long)pcpu; |
| 384 | return __zswap_cpu_notifier(action, cpu); |
| 385 | } |
| 386 | |
| 387 | static struct notifier_block zswap_cpu_notifier_block = { |
| 388 | .notifier_call = zswap_cpu_notifier |
| 389 | }; |
| 390 | |
Mahendran Ganesh | dd01d7d | 2014-12-12 16:57:15 -0800 | [diff] [blame] | 391 | static int __init zswap_cpu_init(void) |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 392 | { |
| 393 | unsigned long cpu; |
| 394 | |
Srivatsa S. Bhat | 5763782 | 2014-03-11 02:12:40 +0530 | [diff] [blame] | 395 | cpu_notifier_register_begin(); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 396 | for_each_online_cpu(cpu) |
| 397 | if (__zswap_cpu_notifier(CPU_UP_PREPARE, cpu) != NOTIFY_OK) |
| 398 | goto cleanup; |
Srivatsa S. Bhat | 5763782 | 2014-03-11 02:12:40 +0530 | [diff] [blame] | 399 | __register_cpu_notifier(&zswap_cpu_notifier_block); |
| 400 | cpu_notifier_register_done(); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 401 | return 0; |
| 402 | |
| 403 | cleanup: |
| 404 | for_each_online_cpu(cpu) |
| 405 | __zswap_cpu_notifier(CPU_UP_CANCELED, cpu); |
Srivatsa S. Bhat | 5763782 | 2014-03-11 02:12:40 +0530 | [diff] [blame] | 406 | cpu_notifier_register_done(); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 407 | return -ENOMEM; |
| 408 | } |
| 409 | |
| 410 | /********************************* |
| 411 | * helpers |
| 412 | **********************************/ |
| 413 | static bool zswap_is_full(void) |
| 414 | { |
SeongJae Park | 5d2d42d | 2014-04-07 15:38:28 -0700 | [diff] [blame] | 415 | return totalram_pages * zswap_max_pool_percent / 100 < |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 416 | DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 419 | /********************************* |
| 420 | * writeback code |
| 421 | **********************************/ |
| 422 | /* return enum for zswap_get_swap_cache_page */ |
| 423 | enum zswap_get_swap_ret { |
| 424 | ZSWAP_SWAPCACHE_NEW, |
| 425 | ZSWAP_SWAPCACHE_EXIST, |
Weijie Yang | 67d13fe | 2013-11-12 15:08:26 -0800 | [diff] [blame] | 426 | ZSWAP_SWAPCACHE_FAIL, |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | /* |
| 430 | * zswap_get_swap_cache_page |
| 431 | * |
| 432 | * This is an adaption of read_swap_cache_async() |
| 433 | * |
| 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. |
| 438 | * |
| 439 | * If success, the swap cache page is returned in retpage |
Weijie Yang | 67d13fe | 2013-11-12 15:08:26 -0800 | [diff] [blame] | 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 |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 444 | */ |
| 445 | static int zswap_get_swap_cache_page(swp_entry_t entry, |
| 446 | struct page **retpage) |
| 447 | { |
| 448 | struct page *found_page, *new_page = NULL; |
Sunghan Suh | 822518d | 2013-09-11 14:20:22 -0700 | [diff] [blame] | 449 | struct address_space *swapper_space = swap_address_space(entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 450 | int err; |
| 451 | |
| 452 | *retpage = NULL; |
| 453 | do { |
| 454 | /* |
| 455 | * First check the swap cache. Since this is normally |
| 456 | * called after lookup_swap_cache() failed, re-calling |
| 457 | * that would confuse statistics. |
| 458 | */ |
| 459 | found_page = find_get_page(swapper_space, entry.val); |
| 460 | if (found_page) |
| 461 | break; |
| 462 | |
| 463 | /* |
| 464 | * Get a new page to read into from swap. |
| 465 | */ |
| 466 | if (!new_page) { |
| 467 | new_page = alloc_page(GFP_KERNEL); |
| 468 | if (!new_page) |
| 469 | break; /* Out of memory */ |
| 470 | } |
| 471 | |
| 472 | /* |
| 473 | * call radix_tree_preload() while we can wait. |
| 474 | */ |
| 475 | err = radix_tree_preload(GFP_KERNEL); |
| 476 | if (err) |
| 477 | break; |
| 478 | |
| 479 | /* |
| 480 | * Swap entry may have been freed since our caller observed it. |
| 481 | */ |
| 482 | err = swapcache_prepare(entry); |
| 483 | if (err == -EEXIST) { /* seems racy */ |
| 484 | radix_tree_preload_end(); |
| 485 | continue; |
| 486 | } |
| 487 | if (err) { /* swp entry is obsolete ? */ |
| 488 | radix_tree_preload_end(); |
| 489 | break; |
| 490 | } |
| 491 | |
| 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); |
| 496 | if (likely(!err)) { |
| 497 | radix_tree_preload_end(); |
| 498 | lru_cache_add_anon(new_page); |
| 499 | *retpage = new_page; |
| 500 | return ZSWAP_SWAPCACHE_NEW; |
| 501 | } |
| 502 | radix_tree_preload_end(); |
| 503 | ClearPageSwapBacked(new_page); |
| 504 | __clear_page_locked(new_page); |
| 505 | /* |
| 506 | * add_to_swap_cache() doesn't return -EEXIST, so we can safely |
| 507 | * clear SWAP_HAS_CACHE flag. |
| 508 | */ |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 509 | swapcache_free(entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 510 | } while (err != -ENOMEM); |
| 511 | |
| 512 | if (new_page) |
| 513 | page_cache_release(new_page); |
| 514 | if (!found_page) |
Weijie Yang | 67d13fe | 2013-11-12 15:08:26 -0800 | [diff] [blame] | 515 | return ZSWAP_SWAPCACHE_FAIL; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 516 | *retpage = found_page; |
| 517 | return ZSWAP_SWAPCACHE_EXIST; |
| 518 | } |
| 519 | |
| 520 | /* |
| 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. |
| 524 | * |
| 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 |
| 530 | * freed. |
| 531 | */ |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 532 | static int zswap_writeback_entry(struct zpool *pool, unsigned long handle) |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 533 | { |
| 534 | struct zswap_header *zhdr; |
| 535 | swp_entry_t swpentry; |
| 536 | struct zswap_tree *tree; |
| 537 | pgoff_t offset; |
| 538 | struct zswap_entry *entry; |
| 539 | struct page *page; |
| 540 | u8 *src, *dst; |
| 541 | unsigned int dlen; |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 542 | int ret; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 543 | struct writeback_control wbc = { |
| 544 | .sync_mode = WB_SYNC_NONE, |
| 545 | }; |
| 546 | |
| 547 | /* extract swpentry from data */ |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 548 | zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 549 | swpentry = zhdr->swpentry; /* here */ |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 550 | zpool_unmap_handle(pool, handle); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 551 | tree = zswap_trees[swp_type(swpentry)]; |
| 552 | offset = swp_offset(swpentry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 553 | |
| 554 | /* find and ref zswap entry */ |
| 555 | spin_lock(&tree->lock); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 556 | entry = zswap_entry_find_get(&tree->rbroot, offset); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 557 | if (!entry) { |
| 558 | /* entry was invalidated */ |
| 559 | spin_unlock(&tree->lock); |
| 560 | return 0; |
| 561 | } |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 562 | spin_unlock(&tree->lock); |
| 563 | BUG_ON(offset != entry->offset); |
| 564 | |
| 565 | /* try to allocate swap cache page */ |
| 566 | switch (zswap_get_swap_cache_page(swpentry, &page)) { |
Weijie Yang | 67d13fe | 2013-11-12 15:08:26 -0800 | [diff] [blame] | 567 | case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */ |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 568 | ret = -ENOMEM; |
| 569 | goto fail; |
| 570 | |
Weijie Yang | 67d13fe | 2013-11-12 15:08:26 -0800 | [diff] [blame] | 571 | case ZSWAP_SWAPCACHE_EXIST: |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 572 | /* page is already in the swap cache, ignore for now */ |
| 573 | page_cache_release(page); |
| 574 | ret = -EEXIST; |
| 575 | goto fail; |
| 576 | |
| 577 | case ZSWAP_SWAPCACHE_NEW: /* page is locked */ |
| 578 | /* decompress */ |
| 579 | dlen = PAGE_SIZE; |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 580 | src = (u8 *)zpool_map_handle(zswap_pool, entry->handle, |
| 581 | ZPOOL_MM_RO) + sizeof(struct zswap_header); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 582 | dst = kmap_atomic(page); |
| 583 | ret = zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS, src, |
| 584 | entry->length, dst, &dlen); |
| 585 | kunmap_atomic(dst); |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 586 | zpool_unmap_handle(zswap_pool, entry->handle); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 587 | BUG_ON(ret); |
| 588 | BUG_ON(dlen != PAGE_SIZE); |
| 589 | |
| 590 | /* page is up to date */ |
| 591 | SetPageUptodate(page); |
| 592 | } |
| 593 | |
Weijie Yang | b349acc | 2013-11-12 15:07:52 -0800 | [diff] [blame] | 594 | /* move it to the tail of the inactive list after end_writeback */ |
| 595 | SetPageReclaim(page); |
| 596 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 597 | /* start writeback */ |
| 598 | __swap_writepage(page, &wbc, end_swap_bio_write); |
| 599 | page_cache_release(page); |
| 600 | zswap_written_back_pages++; |
| 601 | |
| 602 | spin_lock(&tree->lock); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 603 | /* drop local reference */ |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 604 | zswap_entry_put(tree, entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 605 | |
| 606 | /* |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 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 |
| 612 | */ |
| 613 | if (entry == zswap_rb_search(&tree->rbroot, offset)) |
| 614 | zswap_entry_put(tree, entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 615 | spin_unlock(&tree->lock); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 616 | |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 617 | goto end; |
| 618 | |
| 619 | /* |
| 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 |
| 625 | */ |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 626 | fail: |
| 627 | spin_lock(&tree->lock); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 628 | zswap_entry_put(tree, entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 629 | spin_unlock(&tree->lock); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 630 | |
| 631 | end: |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 632 | return ret; |
| 633 | } |
| 634 | |
| 635 | /********************************* |
| 636 | * frontswap hooks |
| 637 | **********************************/ |
| 638 | /* attempts to compress and store an single page */ |
| 639 | static int zswap_frontswap_store(unsigned type, pgoff_t offset, |
| 640 | struct page *page) |
| 641 | { |
| 642 | struct zswap_tree *tree = zswap_trees[type]; |
| 643 | struct zswap_entry *entry, *dupentry; |
| 644 | int ret; |
| 645 | unsigned int dlen = PAGE_SIZE, len; |
| 646 | unsigned long handle; |
| 647 | char *buf; |
| 648 | u8 *src, *dst; |
| 649 | struct zswap_header *zhdr; |
| 650 | |
| 651 | if (!tree) { |
| 652 | ret = -ENODEV; |
| 653 | goto reject; |
| 654 | } |
| 655 | |
| 656 | /* reclaim space if needed */ |
| 657 | if (zswap_is_full()) { |
| 658 | zswap_pool_limit_hit++; |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 659 | if (zpool_shrink(zswap_pool, 1, NULL)) { |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 660 | zswap_reject_reclaim_fail++; |
| 661 | ret = -ENOMEM; |
| 662 | goto reject; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | /* allocate entry */ |
| 667 | entry = zswap_entry_cache_alloc(GFP_KERNEL); |
| 668 | if (!entry) { |
| 669 | zswap_reject_kmemcache_fail++; |
| 670 | ret = -ENOMEM; |
| 671 | goto reject; |
| 672 | } |
| 673 | |
| 674 | /* compress */ |
| 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); |
| 678 | kunmap_atomic(src); |
| 679 | if (ret) { |
| 680 | ret = -EINVAL; |
| 681 | goto freepage; |
| 682 | } |
| 683 | |
| 684 | /* store */ |
| 685 | len = dlen + sizeof(struct zswap_header); |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 686 | ret = zpool_malloc(zswap_pool, len, __GFP_NORETRY | __GFP_NOWARN, |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 687 | &handle); |
| 688 | if (ret == -ENOSPC) { |
| 689 | zswap_reject_compress_poor++; |
| 690 | goto freepage; |
| 691 | } |
| 692 | if (ret) { |
| 693 | zswap_reject_alloc_fail++; |
| 694 | goto freepage; |
| 695 | } |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 696 | zhdr = zpool_map_handle(zswap_pool, handle, ZPOOL_MM_RW); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 697 | zhdr->swpentry = swp_entry(type, offset); |
| 698 | buf = (u8 *)(zhdr + 1); |
| 699 | memcpy(buf, dst, dlen); |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 700 | zpool_unmap_handle(zswap_pool, handle); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 701 | put_cpu_var(zswap_dstmem); |
| 702 | |
| 703 | /* populate entry */ |
| 704 | entry->offset = offset; |
| 705 | entry->handle = handle; |
| 706 | entry->length = dlen; |
| 707 | |
| 708 | /* map */ |
| 709 | spin_lock(&tree->lock); |
| 710 | do { |
| 711 | ret = zswap_rb_insert(&tree->rbroot, entry, &dupentry); |
| 712 | if (ret == -EEXIST) { |
| 713 | zswap_duplicate_entry++; |
| 714 | /* remove from rbtree */ |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 715 | zswap_rb_erase(&tree->rbroot, dupentry); |
| 716 | zswap_entry_put(tree, dupentry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 717 | } |
| 718 | } while (ret == -EEXIST); |
| 719 | spin_unlock(&tree->lock); |
| 720 | |
| 721 | /* update stats */ |
| 722 | atomic_inc(&zswap_stored_pages); |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 723 | zswap_pool_total_size = zpool_get_total_size(zswap_pool); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 724 | |
| 725 | return 0; |
| 726 | |
| 727 | freepage: |
| 728 | put_cpu_var(zswap_dstmem); |
| 729 | zswap_entry_cache_free(entry); |
| 730 | reject: |
| 731 | return ret; |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | * returns 0 if the page was successfully decompressed |
| 736 | * return -1 on entry not found or error |
| 737 | */ |
| 738 | static int zswap_frontswap_load(unsigned type, pgoff_t offset, |
| 739 | struct page *page) |
| 740 | { |
| 741 | struct zswap_tree *tree = zswap_trees[type]; |
| 742 | struct zswap_entry *entry; |
| 743 | u8 *src, *dst; |
| 744 | unsigned int dlen; |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 745 | int ret; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 746 | |
| 747 | /* find */ |
| 748 | spin_lock(&tree->lock); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 749 | entry = zswap_entry_find_get(&tree->rbroot, offset); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 750 | if (!entry) { |
| 751 | /* entry was written back */ |
| 752 | spin_unlock(&tree->lock); |
| 753 | return -1; |
| 754 | } |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 755 | spin_unlock(&tree->lock); |
| 756 | |
| 757 | /* decompress */ |
| 758 | dlen = PAGE_SIZE; |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 759 | src = (u8 *)zpool_map_handle(zswap_pool, entry->handle, |
| 760 | ZPOOL_MM_RO) + sizeof(struct zswap_header); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 761 | dst = kmap_atomic(page); |
| 762 | ret = zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS, src, entry->length, |
| 763 | dst, &dlen); |
| 764 | kunmap_atomic(dst); |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 765 | zpool_unmap_handle(zswap_pool, entry->handle); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 766 | BUG_ON(ret); |
| 767 | |
| 768 | spin_lock(&tree->lock); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 769 | zswap_entry_put(tree, entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 770 | spin_unlock(&tree->lock); |
| 771 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | /* frees an entry in zswap */ |
| 776 | static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset) |
| 777 | { |
| 778 | struct zswap_tree *tree = zswap_trees[type]; |
| 779 | struct zswap_entry *entry; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 780 | |
| 781 | /* find */ |
| 782 | spin_lock(&tree->lock); |
| 783 | entry = zswap_rb_search(&tree->rbroot, offset); |
| 784 | if (!entry) { |
| 785 | /* entry was written back */ |
| 786 | spin_unlock(&tree->lock); |
| 787 | return; |
| 788 | } |
| 789 | |
| 790 | /* remove from rbtree */ |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 791 | zswap_rb_erase(&tree->rbroot, entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 792 | |
| 793 | /* drop the initial reference from entry creation */ |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 794 | zswap_entry_put(tree, entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 795 | |
| 796 | spin_unlock(&tree->lock); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /* frees all zswap entries for the given swap type */ |
| 800 | static void zswap_frontswap_invalidate_area(unsigned type) |
| 801 | { |
| 802 | struct zswap_tree *tree = zswap_trees[type]; |
Cody P Schafer | 0bd4213 | 2013-09-11 14:25:33 -0700 | [diff] [blame] | 803 | struct zswap_entry *entry, *n; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 804 | |
| 805 | if (!tree) |
| 806 | return; |
| 807 | |
| 808 | /* walk the tree and free everything */ |
| 809 | spin_lock(&tree->lock); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 810 | rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode) |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 811 | zswap_free_entry(entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 812 | tree->rbroot = RB_ROOT; |
| 813 | spin_unlock(&tree->lock); |
Weijie Yang | aa9bca0 | 2013-10-16 13:46:54 -0700 | [diff] [blame] | 814 | kfree(tree); |
| 815 | zswap_trees[type] = NULL; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 818 | static struct zpool_ops zswap_zpool_ops = { |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 819 | .evict = zswap_writeback_entry |
| 820 | }; |
| 821 | |
| 822 | static void zswap_frontswap_init(unsigned type) |
| 823 | { |
| 824 | struct zswap_tree *tree; |
| 825 | |
| 826 | tree = kzalloc(sizeof(struct zswap_tree), GFP_KERNEL); |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 827 | if (!tree) { |
| 828 | pr_err("alloc failed, zswap disabled for swap type %d\n", type); |
| 829 | return; |
| 830 | } |
| 831 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 832 | tree->rbroot = RB_ROOT; |
| 833 | spin_lock_init(&tree->lock); |
| 834 | zswap_trees[type] = tree; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 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 |
| 843 | }; |
| 844 | |
| 845 | /********************************* |
| 846 | * debugfs functions |
| 847 | **********************************/ |
| 848 | #ifdef CONFIG_DEBUG_FS |
| 849 | #include <linux/debugfs.h> |
| 850 | |
| 851 | static struct dentry *zswap_debugfs_root; |
| 852 | |
| 853 | static int __init zswap_debugfs_init(void) |
| 854 | { |
| 855 | if (!debugfs_initialized()) |
| 856 | return -ENODEV; |
| 857 | |
| 858 | zswap_debugfs_root = debugfs_create_dir("zswap", NULL); |
| 859 | if (!zswap_debugfs_root) |
| 860 | return -ENOMEM; |
| 861 | |
| 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); |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 876 | debugfs_create_u64("pool_total_size", S_IRUGO, |
| 877 | zswap_debugfs_root, &zswap_pool_total_size); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 878 | debugfs_create_atomic_t("stored_pages", S_IRUGO, |
| 879 | zswap_debugfs_root, &zswap_stored_pages); |
| 880 | |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | static void __exit zswap_debugfs_exit(void) |
| 885 | { |
| 886 | debugfs_remove_recursive(zswap_debugfs_root); |
| 887 | } |
| 888 | #else |
| 889 | static int __init zswap_debugfs_init(void) |
| 890 | { |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | static void __exit zswap_debugfs_exit(void) { } |
| 895 | #endif |
| 896 | |
| 897 | /********************************* |
| 898 | * module init and exit |
| 899 | **********************************/ |
| 900 | static int __init init_zswap(void) |
| 901 | { |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 902 | gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN; |
| 903 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 904 | if (!zswap_enabled) |
| 905 | return 0; |
| 906 | |
| 907 | pr_info("loading zswap\n"); |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 908 | |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 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, |
| 914 | &zswap_zpool_ops); |
| 915 | } |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 916 | if (!zswap_pool) { |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 917 | pr_err("%s zpool not available\n", zswap_zpool_type); |
| 918 | pr_err("zpool creation failed\n"); |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 919 | goto error; |
| 920 | } |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 921 | pr_info("using %s pool\n", zswap_zpool_type); |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 922 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 923 | if (zswap_entry_cache_create()) { |
| 924 | pr_err("entry cache creation failed\n"); |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 925 | goto cachefail; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 926 | } |
| 927 | if (zswap_comp_init()) { |
| 928 | pr_err("compressor initialization failed\n"); |
| 929 | goto compfail; |
| 930 | } |
| 931 | if (zswap_cpu_init()) { |
| 932 | pr_err("per-cpu initialization failed\n"); |
| 933 | goto pcpufail; |
| 934 | } |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 935 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 936 | frontswap_register_ops(&zswap_frontswap_ops); |
| 937 | if (zswap_debugfs_init()) |
| 938 | pr_warn("debugfs initialization failed\n"); |
| 939 | return 0; |
| 940 | pcpufail: |
| 941 | zswap_comp_exit(); |
| 942 | compfail: |
Fabian Frederick | c119239 | 2014-08-08 14:19:35 -0700 | [diff] [blame] | 943 | zswap_entry_cache_destroy(); |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 944 | cachefail: |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 945 | zpool_destroy_pool(zswap_pool); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 946 | error: |
| 947 | return -ENOMEM; |
| 948 | } |
| 949 | /* must be late so crypto has time to come up */ |
| 950 | late_initcall(init_zswap); |
| 951 | |
| 952 | MODULE_LICENSE("GPL"); |
Seth Jennings | 68386da | 2014-11-12 21:08:46 -0600 | [diff] [blame] | 953 | MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>"); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 954 | MODULE_DESCRIPTION("Compressed cache for swap pages"); |