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 | **********************************/ |
Dan Streetman | c00ed16 | 2015-06-25 15:00:35 -0700 | [diff] [blame] | 78 | |
| 79 | /* Enable/disable zswap (disabled by default) */ |
| 80 | static bool zswap_enabled; |
| 81 | module_param_named(enabled, zswap_enabled, bool, 0644); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 82 | |
| 83 | /* Compressor to be used by zswap (fixed at boot for now) */ |
| 84 | #define ZSWAP_COMPRESSOR_DEFAULT "lzo" |
| 85 | static char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT; |
Dan Streetman | 12ab028 | 2014-01-23 15:52:48 -0800 | [diff] [blame] | 86 | module_param_named(compressor, zswap_compressor, charp, 0444); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 87 | |
| 88 | /* The maximum percentage of memory that the compressed pool can occupy */ |
| 89 | static unsigned int zswap_max_pool_percent = 20; |
| 90 | module_param_named(max_pool_percent, |
| 91 | zswap_max_pool_percent, uint, 0644); |
| 92 | |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 93 | /* Compressed storage to use */ |
| 94 | #define ZSWAP_ZPOOL_DEFAULT "zbud" |
| 95 | static char *zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT; |
| 96 | module_param_named(zpool, zswap_zpool_type, charp, 0444); |
| 97 | |
| 98 | /* zpool is shared by all of zswap backend */ |
| 99 | static struct zpool *zswap_pool; |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 100 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 101 | /********************************* |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 102 | * data structures |
| 103 | **********************************/ |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 104 | |
| 105 | struct zswap_pool { |
| 106 | struct zpool *zpool; |
| 107 | struct crypto_comp * __percpu *tfm; |
| 108 | struct kref kref; |
| 109 | struct list_head list; |
| 110 | struct rcu_head rcu_head; |
| 111 | struct notifier_block notifier; |
| 112 | char tfm_name[CRYPTO_MAX_ALG_NAME]; |
| 113 | }; |
| 114 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 115 | /* |
| 116 | * struct zswap_entry |
| 117 | * |
| 118 | * This structure contains the metadata for tracking a single compressed |
| 119 | * page within zswap. |
| 120 | * |
| 121 | * rbnode - links the entry into red-black tree for the appropriate swap type |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 122 | * offset - the swap offset for the entry. Index into the red-black tree. |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 123 | * refcount - the number of outstanding reference to the entry. This is needed |
| 124 | * to protect against premature freeing of the entry by code |
SeongJae Park | 6b45251 | 2014-04-07 15:38:25 -0700 | [diff] [blame] | 125 | * concurrent calls to load, invalidate, and writeback. The lock |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 126 | * for the zswap_tree structure that contains the entry must |
| 127 | * be held while changing the refcount. Since the lock must |
| 128 | * be held, there is no reason to also make refcount atomic. |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 129 | * length - the length in bytes of the compressed page data. Needed during |
SeongJae Park | 6b45251 | 2014-04-07 15:38:25 -0700 | [diff] [blame] | 130 | * decompression |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 131 | * pool - the zswap_pool the entry's data is in |
| 132 | * handle - zpool allocation handle that stores the compressed page data |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 133 | */ |
| 134 | struct zswap_entry { |
| 135 | struct rb_node rbnode; |
| 136 | pgoff_t offset; |
| 137 | int refcount; |
| 138 | unsigned int length; |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 139 | struct zswap_pool *pool; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 140 | unsigned long handle; |
| 141 | }; |
| 142 | |
| 143 | struct zswap_header { |
| 144 | swp_entry_t swpentry; |
| 145 | }; |
| 146 | |
| 147 | /* |
| 148 | * The tree lock in the zswap_tree struct protects a few things: |
| 149 | * - the rbtree |
| 150 | * - the refcount field of each entry in the tree |
| 151 | */ |
| 152 | struct zswap_tree { |
| 153 | struct rb_root rbroot; |
| 154 | spinlock_t lock; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | static struct zswap_tree *zswap_trees[MAX_SWAPFILES]; |
| 158 | |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 159 | /* RCU-protected iteration */ |
| 160 | static LIST_HEAD(zswap_pools); |
| 161 | /* protects zswap_pools list modification */ |
| 162 | static DEFINE_SPINLOCK(zswap_pools_lock); |
| 163 | |
| 164 | /********************************* |
| 165 | * helpers and fwd declarations |
| 166 | **********************************/ |
| 167 | |
| 168 | #define zswap_pool_debug(msg, p) \ |
| 169 | pr_debug("%s pool %s/%s\n", msg, (p)->tfm_name, \ |
| 170 | zpool_get_type((p)->zpool)) |
| 171 | |
| 172 | static int zswap_writeback_entry(struct zpool *pool, unsigned long handle); |
| 173 | static int zswap_pool_get(struct zswap_pool *pool); |
| 174 | static void zswap_pool_put(struct zswap_pool *pool); |
| 175 | |
| 176 | static const struct zpool_ops zswap_zpool_ops = { |
| 177 | .evict = zswap_writeback_entry |
| 178 | }; |
| 179 | |
| 180 | static bool zswap_is_full(void) |
| 181 | { |
| 182 | return totalram_pages * zswap_max_pool_percent / 100 < |
| 183 | DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE); |
| 184 | } |
| 185 | |
| 186 | static void zswap_update_total_size(void) |
| 187 | { |
| 188 | struct zswap_pool *pool; |
| 189 | u64 total = 0; |
| 190 | |
| 191 | rcu_read_lock(); |
| 192 | |
| 193 | list_for_each_entry_rcu(pool, &zswap_pools, list) |
| 194 | total += zpool_get_total_size(pool->zpool); |
| 195 | |
| 196 | rcu_read_unlock(); |
| 197 | |
| 198 | zswap_pool_total_size = total; |
| 199 | } |
| 200 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 201 | /********************************* |
| 202 | * zswap entry functions |
| 203 | **********************************/ |
| 204 | static struct kmem_cache *zswap_entry_cache; |
| 205 | |
Mahendran Ganesh | dd01d7d | 2014-12-12 16:57:15 -0800 | [diff] [blame] | 206 | static int __init zswap_entry_cache_create(void) |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 207 | { |
| 208 | zswap_entry_cache = KMEM_CACHE(zswap_entry, 0); |
SeongJae Park | 5d2d42d | 2014-04-07 15:38:28 -0700 | [diff] [blame] | 209 | return zswap_entry_cache == NULL; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Fabian Frederick | c119239 | 2014-08-08 14:19:35 -0700 | [diff] [blame] | 212 | static void __init zswap_entry_cache_destroy(void) |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 213 | { |
| 214 | kmem_cache_destroy(zswap_entry_cache); |
| 215 | } |
| 216 | |
| 217 | static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp) |
| 218 | { |
| 219 | struct zswap_entry *entry; |
| 220 | entry = kmem_cache_alloc(zswap_entry_cache, gfp); |
| 221 | if (!entry) |
| 222 | return NULL; |
| 223 | entry->refcount = 1; |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 224 | RB_CLEAR_NODE(&entry->rbnode); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 225 | return entry; |
| 226 | } |
| 227 | |
| 228 | static void zswap_entry_cache_free(struct zswap_entry *entry) |
| 229 | { |
| 230 | kmem_cache_free(zswap_entry_cache, entry); |
| 231 | } |
| 232 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 233 | /********************************* |
| 234 | * rbtree functions |
| 235 | **********************************/ |
| 236 | static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset) |
| 237 | { |
| 238 | struct rb_node *node = root->rb_node; |
| 239 | struct zswap_entry *entry; |
| 240 | |
| 241 | while (node) { |
| 242 | entry = rb_entry(node, struct zswap_entry, rbnode); |
| 243 | if (entry->offset > offset) |
| 244 | node = node->rb_left; |
| 245 | else if (entry->offset < offset) |
| 246 | node = node->rb_right; |
| 247 | else |
| 248 | return entry; |
| 249 | } |
| 250 | return NULL; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * In the case that a entry with the same offset is found, a pointer to |
| 255 | * the existing entry is stored in dupentry and the function returns -EEXIST |
| 256 | */ |
| 257 | static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry, |
| 258 | struct zswap_entry **dupentry) |
| 259 | { |
| 260 | struct rb_node **link = &root->rb_node, *parent = NULL; |
| 261 | struct zswap_entry *myentry; |
| 262 | |
| 263 | while (*link) { |
| 264 | parent = *link; |
| 265 | myentry = rb_entry(parent, struct zswap_entry, rbnode); |
| 266 | if (myentry->offset > entry->offset) |
| 267 | link = &(*link)->rb_left; |
| 268 | else if (myentry->offset < entry->offset) |
| 269 | link = &(*link)->rb_right; |
| 270 | else { |
| 271 | *dupentry = myentry; |
| 272 | return -EEXIST; |
| 273 | } |
| 274 | } |
| 275 | rb_link_node(&entry->rbnode, parent, link); |
| 276 | rb_insert_color(&entry->rbnode, root); |
| 277 | return 0; |
| 278 | } |
| 279 | |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 280 | static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry) |
| 281 | { |
| 282 | if (!RB_EMPTY_NODE(&entry->rbnode)) { |
| 283 | rb_erase(&entry->rbnode, root); |
| 284 | RB_CLEAR_NODE(&entry->rbnode); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /* |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 289 | * Carries out the common pattern of freeing and entry's zpool allocation, |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 290 | * freeing the entry itself, and decrementing the number of stored pages. |
| 291 | */ |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 292 | static void zswap_free_entry(struct zswap_entry *entry) |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 293 | { |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 294 | zpool_free(entry->pool->zpool, entry->handle); |
| 295 | zswap_pool_put(entry->pool); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 296 | zswap_entry_cache_free(entry); |
| 297 | atomic_dec(&zswap_stored_pages); |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 298 | zswap_update_total_size(); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* caller must hold the tree lock */ |
| 302 | static void zswap_entry_get(struct zswap_entry *entry) |
| 303 | { |
| 304 | entry->refcount++; |
| 305 | } |
| 306 | |
| 307 | /* caller must hold the tree lock |
| 308 | * remove from the tree and free it, if nobody reference the entry |
| 309 | */ |
| 310 | static void zswap_entry_put(struct zswap_tree *tree, |
| 311 | struct zswap_entry *entry) |
| 312 | { |
| 313 | int refcount = --entry->refcount; |
| 314 | |
| 315 | BUG_ON(refcount < 0); |
| 316 | if (refcount == 0) { |
| 317 | zswap_rb_erase(&tree->rbroot, entry); |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 318 | zswap_free_entry(entry); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | /* caller must hold the tree lock */ |
| 323 | static struct zswap_entry *zswap_entry_find_get(struct rb_root *root, |
| 324 | pgoff_t offset) |
| 325 | { |
| 326 | struct zswap_entry *entry = NULL; |
| 327 | |
| 328 | entry = zswap_rb_search(root, offset); |
| 329 | if (entry) |
| 330 | zswap_entry_get(entry); |
| 331 | |
| 332 | return entry; |
| 333 | } |
| 334 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 335 | /********************************* |
| 336 | * per-cpu code |
| 337 | **********************************/ |
| 338 | static DEFINE_PER_CPU(u8 *, zswap_dstmem); |
| 339 | |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 340 | static int __zswap_cpu_dstmem_notifier(unsigned long action, unsigned long cpu) |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 341 | { |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 342 | u8 *dst; |
| 343 | |
| 344 | switch (action) { |
| 345 | case CPU_UP_PREPARE: |
Eric Dumazet | 72d0963 | 2014-06-04 16:11:11 -0700 | [diff] [blame] | 346 | dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu)); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 347 | if (!dst) { |
| 348 | pr_err("can't allocate compressor buffer\n"); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 349 | return NOTIFY_BAD; |
| 350 | } |
| 351 | per_cpu(zswap_dstmem, cpu) = dst; |
| 352 | break; |
| 353 | case CPU_DEAD: |
| 354 | case CPU_UP_CANCELED: |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 355 | dst = per_cpu(zswap_dstmem, cpu); |
| 356 | kfree(dst); |
| 357 | per_cpu(zswap_dstmem, cpu) = NULL; |
| 358 | break; |
| 359 | default: |
| 360 | break; |
| 361 | } |
| 362 | return NOTIFY_OK; |
| 363 | } |
| 364 | |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 365 | static int zswap_cpu_dstmem_notifier(struct notifier_block *nb, |
| 366 | unsigned long action, void *pcpu) |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 367 | { |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 368 | return __zswap_cpu_dstmem_notifier(action, (unsigned long)pcpu); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 371 | static struct notifier_block zswap_dstmem_notifier = { |
| 372 | .notifier_call = zswap_cpu_dstmem_notifier, |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 373 | }; |
| 374 | |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 375 | static int __init zswap_cpu_dstmem_init(void) |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 376 | { |
| 377 | unsigned long cpu; |
| 378 | |
Srivatsa S. Bhat | 5763782 | 2014-03-11 02:12:40 +0530 | [diff] [blame] | 379 | cpu_notifier_register_begin(); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 380 | for_each_online_cpu(cpu) |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 381 | if (__zswap_cpu_dstmem_notifier(CPU_UP_PREPARE, cpu) == |
| 382 | NOTIFY_BAD) |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 383 | goto cleanup; |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 384 | __register_cpu_notifier(&zswap_dstmem_notifier); |
Srivatsa S. Bhat | 5763782 | 2014-03-11 02:12:40 +0530 | [diff] [blame] | 385 | cpu_notifier_register_done(); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 386 | return 0; |
| 387 | |
| 388 | cleanup: |
| 389 | for_each_online_cpu(cpu) |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 390 | __zswap_cpu_dstmem_notifier(CPU_UP_CANCELED, cpu); |
Srivatsa S. Bhat | 5763782 | 2014-03-11 02:12:40 +0530 | [diff] [blame] | 391 | cpu_notifier_register_done(); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 392 | return -ENOMEM; |
| 393 | } |
| 394 | |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 395 | static void zswap_cpu_dstmem_destroy(void) |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 396 | { |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 397 | unsigned long cpu; |
| 398 | |
| 399 | cpu_notifier_register_begin(); |
| 400 | for_each_online_cpu(cpu) |
| 401 | __zswap_cpu_dstmem_notifier(CPU_UP_CANCELED, cpu); |
| 402 | __unregister_cpu_notifier(&zswap_dstmem_notifier); |
| 403 | cpu_notifier_register_done(); |
| 404 | } |
| 405 | |
| 406 | static int __zswap_cpu_comp_notifier(struct zswap_pool *pool, |
| 407 | unsigned long action, unsigned long cpu) |
| 408 | { |
| 409 | struct crypto_comp *tfm; |
| 410 | |
| 411 | switch (action) { |
| 412 | case CPU_UP_PREPARE: |
| 413 | if (WARN_ON(*per_cpu_ptr(pool->tfm, cpu))) |
| 414 | break; |
| 415 | tfm = crypto_alloc_comp(pool->tfm_name, 0, 0); |
| 416 | if (IS_ERR_OR_NULL(tfm)) { |
| 417 | pr_err("could not alloc crypto comp %s : %ld\n", |
| 418 | pool->tfm_name, PTR_ERR(tfm)); |
| 419 | return NOTIFY_BAD; |
| 420 | } |
| 421 | *per_cpu_ptr(pool->tfm, cpu) = tfm; |
| 422 | break; |
| 423 | case CPU_DEAD: |
| 424 | case CPU_UP_CANCELED: |
| 425 | tfm = *per_cpu_ptr(pool->tfm, cpu); |
| 426 | if (!IS_ERR_OR_NULL(tfm)) |
| 427 | crypto_free_comp(tfm); |
| 428 | *per_cpu_ptr(pool->tfm, cpu) = NULL; |
| 429 | break; |
| 430 | default: |
| 431 | break; |
| 432 | } |
| 433 | return NOTIFY_OK; |
| 434 | } |
| 435 | |
| 436 | static int zswap_cpu_comp_notifier(struct notifier_block *nb, |
| 437 | unsigned long action, void *pcpu) |
| 438 | { |
| 439 | unsigned long cpu = (unsigned long)pcpu; |
| 440 | struct zswap_pool *pool = container_of(nb, typeof(*pool), notifier); |
| 441 | |
| 442 | return __zswap_cpu_comp_notifier(pool, action, cpu); |
| 443 | } |
| 444 | |
| 445 | static int zswap_cpu_comp_init(struct zswap_pool *pool) |
| 446 | { |
| 447 | unsigned long cpu; |
| 448 | |
| 449 | memset(&pool->notifier, 0, sizeof(pool->notifier)); |
| 450 | pool->notifier.notifier_call = zswap_cpu_comp_notifier; |
| 451 | |
| 452 | cpu_notifier_register_begin(); |
| 453 | for_each_online_cpu(cpu) |
| 454 | if (__zswap_cpu_comp_notifier(pool, CPU_UP_PREPARE, cpu) == |
| 455 | NOTIFY_BAD) |
| 456 | goto cleanup; |
| 457 | __register_cpu_notifier(&pool->notifier); |
| 458 | cpu_notifier_register_done(); |
| 459 | return 0; |
| 460 | |
| 461 | cleanup: |
| 462 | for_each_online_cpu(cpu) |
| 463 | __zswap_cpu_comp_notifier(pool, CPU_UP_CANCELED, cpu); |
| 464 | cpu_notifier_register_done(); |
| 465 | return -ENOMEM; |
| 466 | } |
| 467 | |
| 468 | static void zswap_cpu_comp_destroy(struct zswap_pool *pool) |
| 469 | { |
| 470 | unsigned long cpu; |
| 471 | |
| 472 | cpu_notifier_register_begin(); |
| 473 | for_each_online_cpu(cpu) |
| 474 | __zswap_cpu_comp_notifier(pool, CPU_UP_CANCELED, cpu); |
| 475 | __unregister_cpu_notifier(&pool->notifier); |
| 476 | cpu_notifier_register_done(); |
| 477 | } |
| 478 | |
| 479 | /********************************* |
| 480 | * pool functions |
| 481 | **********************************/ |
| 482 | |
| 483 | static struct zswap_pool *__zswap_pool_current(void) |
| 484 | { |
| 485 | struct zswap_pool *pool; |
| 486 | |
| 487 | pool = list_first_or_null_rcu(&zswap_pools, typeof(*pool), list); |
| 488 | WARN_ON(!pool); |
| 489 | |
| 490 | return pool; |
| 491 | } |
| 492 | |
| 493 | static struct zswap_pool *zswap_pool_current(void) |
| 494 | { |
| 495 | assert_spin_locked(&zswap_pools_lock); |
| 496 | |
| 497 | return __zswap_pool_current(); |
| 498 | } |
| 499 | |
| 500 | static struct zswap_pool *zswap_pool_current_get(void) |
| 501 | { |
| 502 | struct zswap_pool *pool; |
| 503 | |
| 504 | rcu_read_lock(); |
| 505 | |
| 506 | pool = __zswap_pool_current(); |
| 507 | if (!pool || !zswap_pool_get(pool)) |
| 508 | pool = NULL; |
| 509 | |
| 510 | rcu_read_unlock(); |
| 511 | |
| 512 | return pool; |
| 513 | } |
| 514 | |
| 515 | static struct zswap_pool *zswap_pool_last_get(void) |
| 516 | { |
| 517 | struct zswap_pool *pool, *last = NULL; |
| 518 | |
| 519 | rcu_read_lock(); |
| 520 | |
| 521 | list_for_each_entry_rcu(pool, &zswap_pools, list) |
| 522 | last = pool; |
| 523 | if (!WARN_ON(!last) && !zswap_pool_get(last)) |
| 524 | last = NULL; |
| 525 | |
| 526 | rcu_read_unlock(); |
| 527 | |
| 528 | return last; |
| 529 | } |
| 530 | |
| 531 | static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor) |
| 532 | { |
| 533 | struct zswap_pool *pool; |
| 534 | |
| 535 | assert_spin_locked(&zswap_pools_lock); |
| 536 | |
| 537 | list_for_each_entry_rcu(pool, &zswap_pools, list) { |
| 538 | if (strncmp(pool->tfm_name, compressor, sizeof(pool->tfm_name))) |
| 539 | continue; |
| 540 | if (strncmp(zpool_get_type(pool->zpool), type, |
| 541 | sizeof(zswap_zpool_type))) |
| 542 | continue; |
| 543 | /* if we can't get it, it's about to be destroyed */ |
| 544 | if (!zswap_pool_get(pool)) |
| 545 | continue; |
| 546 | return pool; |
| 547 | } |
| 548 | |
| 549 | return NULL; |
| 550 | } |
| 551 | |
| 552 | static struct zswap_pool *zswap_pool_create(char *type, char *compressor) |
| 553 | { |
| 554 | struct zswap_pool *pool; |
| 555 | gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN; |
| 556 | |
| 557 | pool = kzalloc(sizeof(*pool), GFP_KERNEL); |
| 558 | if (!pool) { |
| 559 | pr_err("pool alloc failed\n"); |
| 560 | return NULL; |
| 561 | } |
| 562 | |
| 563 | pool->zpool = zpool_create_pool(type, "zswap", gfp, &zswap_zpool_ops); |
| 564 | if (!pool->zpool) { |
| 565 | pr_err("%s zpool not available\n", type); |
| 566 | goto error; |
| 567 | } |
| 568 | pr_debug("using %s zpool\n", zpool_get_type(pool->zpool)); |
| 569 | |
| 570 | strlcpy(pool->tfm_name, compressor, sizeof(pool->tfm_name)); |
| 571 | pool->tfm = alloc_percpu(struct crypto_comp *); |
| 572 | if (!pool->tfm) { |
| 573 | pr_err("percpu alloc failed\n"); |
| 574 | goto error; |
| 575 | } |
| 576 | |
| 577 | if (zswap_cpu_comp_init(pool)) |
| 578 | goto error; |
| 579 | pr_debug("using %s compressor\n", pool->tfm_name); |
| 580 | |
| 581 | /* being the current pool takes 1 ref; this func expects the |
| 582 | * caller to always add the new pool as the current pool |
| 583 | */ |
| 584 | kref_init(&pool->kref); |
| 585 | INIT_LIST_HEAD(&pool->list); |
| 586 | |
| 587 | zswap_pool_debug("created", pool); |
| 588 | |
| 589 | return pool; |
| 590 | |
| 591 | error: |
| 592 | free_percpu(pool->tfm); |
| 593 | if (pool->zpool) |
| 594 | zpool_destroy_pool(pool->zpool); |
| 595 | kfree(pool); |
| 596 | return NULL; |
| 597 | } |
| 598 | |
| 599 | static struct zswap_pool *__zswap_pool_create_fallback(void) |
| 600 | { |
| 601 | if (!crypto_has_comp(zswap_compressor, 0, 0)) { |
| 602 | pr_err("compressor %s not available, using default %s\n", |
| 603 | zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT); |
| 604 | strncpy(zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT, |
| 605 | sizeof(zswap_compressor)); |
| 606 | } |
| 607 | if (!zpool_has_pool(zswap_zpool_type)) { |
| 608 | pr_err("zpool %s not available, using default %s\n", |
| 609 | zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT); |
| 610 | strncpy(zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT, |
| 611 | sizeof(zswap_zpool_type)); |
| 612 | } |
| 613 | |
| 614 | return zswap_pool_create(zswap_zpool_type, zswap_compressor); |
| 615 | } |
| 616 | |
| 617 | static void zswap_pool_destroy(struct zswap_pool *pool) |
| 618 | { |
| 619 | zswap_pool_debug("destroying", pool); |
| 620 | |
| 621 | zswap_cpu_comp_destroy(pool); |
| 622 | free_percpu(pool->tfm); |
| 623 | zpool_destroy_pool(pool->zpool); |
| 624 | kfree(pool); |
| 625 | } |
| 626 | |
| 627 | static int __must_check zswap_pool_get(struct zswap_pool *pool) |
| 628 | { |
| 629 | return kref_get_unless_zero(&pool->kref); |
| 630 | } |
| 631 | |
| 632 | static void __zswap_pool_release(struct rcu_head *head) |
| 633 | { |
| 634 | struct zswap_pool *pool = container_of(head, typeof(*pool), rcu_head); |
| 635 | |
| 636 | /* nobody should have been able to get a kref... */ |
| 637 | WARN_ON(kref_get_unless_zero(&pool->kref)); |
| 638 | |
| 639 | /* pool is now off zswap_pools list and has no references. */ |
| 640 | zswap_pool_destroy(pool); |
| 641 | } |
| 642 | |
| 643 | static void __zswap_pool_empty(struct kref *kref) |
| 644 | { |
| 645 | struct zswap_pool *pool; |
| 646 | |
| 647 | pool = container_of(kref, typeof(*pool), kref); |
| 648 | |
| 649 | spin_lock(&zswap_pools_lock); |
| 650 | |
| 651 | WARN_ON(pool == zswap_pool_current()); |
| 652 | |
| 653 | list_del_rcu(&pool->list); |
| 654 | call_rcu(&pool->rcu_head, __zswap_pool_release); |
| 655 | |
| 656 | spin_unlock(&zswap_pools_lock); |
| 657 | } |
| 658 | |
| 659 | static void zswap_pool_put(struct zswap_pool *pool) |
| 660 | { |
| 661 | kref_put(&pool->kref, __zswap_pool_empty); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 664 | /********************************* |
| 665 | * writeback code |
| 666 | **********************************/ |
| 667 | /* return enum for zswap_get_swap_cache_page */ |
| 668 | enum zswap_get_swap_ret { |
| 669 | ZSWAP_SWAPCACHE_NEW, |
| 670 | ZSWAP_SWAPCACHE_EXIST, |
Weijie Yang | 67d13fe | 2013-11-12 15:08:26 -0800 | [diff] [blame] | 671 | ZSWAP_SWAPCACHE_FAIL, |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 672 | }; |
| 673 | |
| 674 | /* |
| 675 | * zswap_get_swap_cache_page |
| 676 | * |
| 677 | * This is an adaption of read_swap_cache_async() |
| 678 | * |
| 679 | * This function tries to find a page with the given swap entry |
| 680 | * in the swapper_space address space (the swap cache). If the page |
| 681 | * is found, it is returned in retpage. Otherwise, a page is allocated, |
| 682 | * added to the swap cache, and returned in retpage. |
| 683 | * |
| 684 | * If success, the swap cache page is returned in retpage |
Weijie Yang | 67d13fe | 2013-11-12 15:08:26 -0800 | [diff] [blame] | 685 | * Returns ZSWAP_SWAPCACHE_EXIST if page was already in the swap cache |
| 686 | * Returns ZSWAP_SWAPCACHE_NEW if the new page needs to be populated, |
| 687 | * the new page is added to swapcache and locked |
| 688 | * Returns ZSWAP_SWAPCACHE_FAIL on error |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 689 | */ |
| 690 | static int zswap_get_swap_cache_page(swp_entry_t entry, |
| 691 | struct page **retpage) |
| 692 | { |
Dmitry Safonov | 5b999aa | 2015-09-08 15:05:00 -0700 | [diff] [blame] | 693 | bool page_was_allocated; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 694 | |
Dmitry Safonov | 5b999aa | 2015-09-08 15:05:00 -0700 | [diff] [blame] | 695 | *retpage = __read_swap_cache_async(entry, GFP_KERNEL, |
| 696 | NULL, 0, &page_was_allocated); |
| 697 | if (page_was_allocated) |
| 698 | return ZSWAP_SWAPCACHE_NEW; |
| 699 | if (!*retpage) |
Weijie Yang | 67d13fe | 2013-11-12 15:08:26 -0800 | [diff] [blame] | 700 | return ZSWAP_SWAPCACHE_FAIL; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 701 | return ZSWAP_SWAPCACHE_EXIST; |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Attempts to free an entry by adding a page to the swap cache, |
| 706 | * decompressing the entry data into the page, and issuing a |
| 707 | * bio write to write the page back to the swap device. |
| 708 | * |
| 709 | * This can be thought of as a "resumed writeback" of the page |
| 710 | * to the swap device. We are basically resuming the same swap |
| 711 | * writeback path that was intercepted with the frontswap_store() |
| 712 | * in the first place. After the page has been decompressed into |
| 713 | * the swap cache, the compressed version stored by zswap can be |
| 714 | * freed. |
| 715 | */ |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 716 | static int zswap_writeback_entry(struct zpool *pool, unsigned long handle) |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 717 | { |
| 718 | struct zswap_header *zhdr; |
| 719 | swp_entry_t swpentry; |
| 720 | struct zswap_tree *tree; |
| 721 | pgoff_t offset; |
| 722 | struct zswap_entry *entry; |
| 723 | struct page *page; |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 724 | struct crypto_comp *tfm; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 725 | u8 *src, *dst; |
| 726 | unsigned int dlen; |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 727 | int ret; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 728 | struct writeback_control wbc = { |
| 729 | .sync_mode = WB_SYNC_NONE, |
| 730 | }; |
| 731 | |
| 732 | /* extract swpentry from data */ |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 733 | zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 734 | swpentry = zhdr->swpentry; /* here */ |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 735 | zpool_unmap_handle(pool, handle); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 736 | tree = zswap_trees[swp_type(swpentry)]; |
| 737 | offset = swp_offset(swpentry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 738 | |
| 739 | /* find and ref zswap entry */ |
| 740 | spin_lock(&tree->lock); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 741 | entry = zswap_entry_find_get(&tree->rbroot, offset); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 742 | if (!entry) { |
| 743 | /* entry was invalidated */ |
| 744 | spin_unlock(&tree->lock); |
| 745 | return 0; |
| 746 | } |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 747 | spin_unlock(&tree->lock); |
| 748 | BUG_ON(offset != entry->offset); |
| 749 | |
| 750 | /* try to allocate swap cache page */ |
| 751 | switch (zswap_get_swap_cache_page(swpentry, &page)) { |
Weijie Yang | 67d13fe | 2013-11-12 15:08:26 -0800 | [diff] [blame] | 752 | case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */ |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 753 | ret = -ENOMEM; |
| 754 | goto fail; |
| 755 | |
Weijie Yang | 67d13fe | 2013-11-12 15:08:26 -0800 | [diff] [blame] | 756 | case ZSWAP_SWAPCACHE_EXIST: |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 757 | /* page is already in the swap cache, ignore for now */ |
| 758 | page_cache_release(page); |
| 759 | ret = -EEXIST; |
| 760 | goto fail; |
| 761 | |
| 762 | case ZSWAP_SWAPCACHE_NEW: /* page is locked */ |
| 763 | /* decompress */ |
| 764 | dlen = PAGE_SIZE; |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 765 | src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle, |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 766 | ZPOOL_MM_RO) + sizeof(struct zswap_header); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 767 | dst = kmap_atomic(page); |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 768 | tfm = *get_cpu_ptr(entry->pool->tfm); |
| 769 | ret = crypto_comp_decompress(tfm, src, entry->length, |
| 770 | dst, &dlen); |
| 771 | put_cpu_ptr(entry->pool->tfm); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 772 | kunmap_atomic(dst); |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 773 | zpool_unmap_handle(entry->pool->zpool, entry->handle); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 774 | BUG_ON(ret); |
| 775 | BUG_ON(dlen != PAGE_SIZE); |
| 776 | |
| 777 | /* page is up to date */ |
| 778 | SetPageUptodate(page); |
| 779 | } |
| 780 | |
Weijie Yang | b349acc | 2013-11-12 15:07:52 -0800 | [diff] [blame] | 781 | /* move it to the tail of the inactive list after end_writeback */ |
| 782 | SetPageReclaim(page); |
| 783 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 784 | /* start writeback */ |
| 785 | __swap_writepage(page, &wbc, end_swap_bio_write); |
| 786 | page_cache_release(page); |
| 787 | zswap_written_back_pages++; |
| 788 | |
| 789 | spin_lock(&tree->lock); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 790 | /* drop local reference */ |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 791 | zswap_entry_put(tree, entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 792 | |
| 793 | /* |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 794 | * There are two possible situations for entry here: |
| 795 | * (1) refcount is 1(normal case), entry is valid and on the tree |
| 796 | * (2) refcount is 0, entry is freed and not on the tree |
| 797 | * because invalidate happened during writeback |
| 798 | * search the tree and free the entry if find entry |
| 799 | */ |
| 800 | if (entry == zswap_rb_search(&tree->rbroot, offset)) |
| 801 | zswap_entry_put(tree, entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 802 | spin_unlock(&tree->lock); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 803 | |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 804 | goto end; |
| 805 | |
| 806 | /* |
| 807 | * if we get here due to ZSWAP_SWAPCACHE_EXIST |
| 808 | * a load may happening concurrently |
| 809 | * it is safe and okay to not free the entry |
| 810 | * if we free the entry in the following put |
| 811 | * it it either okay to return !0 |
| 812 | */ |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 813 | fail: |
| 814 | spin_lock(&tree->lock); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 815 | zswap_entry_put(tree, entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 816 | spin_unlock(&tree->lock); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 817 | |
| 818 | end: |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 819 | return ret; |
| 820 | } |
| 821 | |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 822 | static int zswap_shrink(void) |
| 823 | { |
| 824 | struct zswap_pool *pool; |
| 825 | int ret; |
| 826 | |
| 827 | pool = zswap_pool_last_get(); |
| 828 | if (!pool) |
| 829 | return -ENOENT; |
| 830 | |
| 831 | ret = zpool_shrink(pool->zpool, 1, NULL); |
| 832 | |
| 833 | zswap_pool_put(pool); |
| 834 | |
| 835 | return ret; |
| 836 | } |
| 837 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 838 | /********************************* |
| 839 | * frontswap hooks |
| 840 | **********************************/ |
| 841 | /* attempts to compress and store an single page */ |
| 842 | static int zswap_frontswap_store(unsigned type, pgoff_t offset, |
| 843 | struct page *page) |
| 844 | { |
| 845 | struct zswap_tree *tree = zswap_trees[type]; |
| 846 | struct zswap_entry *entry, *dupentry; |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 847 | struct crypto_comp *tfm; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 848 | int ret; |
| 849 | unsigned int dlen = PAGE_SIZE, len; |
| 850 | unsigned long handle; |
| 851 | char *buf; |
| 852 | u8 *src, *dst; |
| 853 | struct zswap_header *zhdr; |
| 854 | |
Dan Streetman | c00ed16 | 2015-06-25 15:00:35 -0700 | [diff] [blame] | 855 | if (!zswap_enabled || !tree) { |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 856 | ret = -ENODEV; |
| 857 | goto reject; |
| 858 | } |
| 859 | |
| 860 | /* reclaim space if needed */ |
| 861 | if (zswap_is_full()) { |
| 862 | zswap_pool_limit_hit++; |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 863 | if (zswap_shrink()) { |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 864 | zswap_reject_reclaim_fail++; |
| 865 | ret = -ENOMEM; |
| 866 | goto reject; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | /* allocate entry */ |
| 871 | entry = zswap_entry_cache_alloc(GFP_KERNEL); |
| 872 | if (!entry) { |
| 873 | zswap_reject_kmemcache_fail++; |
| 874 | ret = -ENOMEM; |
| 875 | goto reject; |
| 876 | } |
| 877 | |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 878 | /* if entry is successfully added, it keeps the reference */ |
| 879 | entry->pool = zswap_pool_current_get(); |
| 880 | if (!entry->pool) { |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 881 | ret = -EINVAL; |
| 882 | goto freepage; |
| 883 | } |
| 884 | |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 885 | /* compress */ |
| 886 | dst = get_cpu_var(zswap_dstmem); |
| 887 | tfm = *get_cpu_ptr(entry->pool->tfm); |
| 888 | src = kmap_atomic(page); |
| 889 | ret = crypto_comp_compress(tfm, src, PAGE_SIZE, dst, &dlen); |
| 890 | kunmap_atomic(src); |
| 891 | put_cpu_ptr(entry->pool->tfm); |
| 892 | if (ret) { |
| 893 | ret = -EINVAL; |
| 894 | goto put_dstmem; |
| 895 | } |
| 896 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 897 | /* store */ |
| 898 | len = dlen + sizeof(struct zswap_header); |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 899 | ret = zpool_malloc(entry->pool->zpool, len, |
| 900 | __GFP_NORETRY | __GFP_NOWARN, &handle); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 901 | if (ret == -ENOSPC) { |
| 902 | zswap_reject_compress_poor++; |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 903 | goto put_dstmem; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 904 | } |
| 905 | if (ret) { |
| 906 | zswap_reject_alloc_fail++; |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 907 | goto put_dstmem; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 908 | } |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 909 | zhdr = zpool_map_handle(entry->pool->zpool, handle, ZPOOL_MM_RW); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 910 | zhdr->swpentry = swp_entry(type, offset); |
| 911 | buf = (u8 *)(zhdr + 1); |
| 912 | memcpy(buf, dst, dlen); |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 913 | zpool_unmap_handle(entry->pool->zpool, handle); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 914 | put_cpu_var(zswap_dstmem); |
| 915 | |
| 916 | /* populate entry */ |
| 917 | entry->offset = offset; |
| 918 | entry->handle = handle; |
| 919 | entry->length = dlen; |
| 920 | |
| 921 | /* map */ |
| 922 | spin_lock(&tree->lock); |
| 923 | do { |
| 924 | ret = zswap_rb_insert(&tree->rbroot, entry, &dupentry); |
| 925 | if (ret == -EEXIST) { |
| 926 | zswap_duplicate_entry++; |
| 927 | /* remove from rbtree */ |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 928 | zswap_rb_erase(&tree->rbroot, dupentry); |
| 929 | zswap_entry_put(tree, dupentry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 930 | } |
| 931 | } while (ret == -EEXIST); |
| 932 | spin_unlock(&tree->lock); |
| 933 | |
| 934 | /* update stats */ |
| 935 | atomic_inc(&zswap_stored_pages); |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 936 | zswap_update_total_size(); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 937 | |
| 938 | return 0; |
| 939 | |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 940 | put_dstmem: |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 941 | put_cpu_var(zswap_dstmem); |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 942 | zswap_pool_put(entry->pool); |
| 943 | freepage: |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 944 | zswap_entry_cache_free(entry); |
| 945 | reject: |
| 946 | return ret; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * returns 0 if the page was successfully decompressed |
| 951 | * return -1 on entry not found or error |
| 952 | */ |
| 953 | static int zswap_frontswap_load(unsigned type, pgoff_t offset, |
| 954 | struct page *page) |
| 955 | { |
| 956 | struct zswap_tree *tree = zswap_trees[type]; |
| 957 | struct zswap_entry *entry; |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 958 | struct crypto_comp *tfm; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 959 | u8 *src, *dst; |
| 960 | unsigned int dlen; |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 961 | int ret; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 962 | |
| 963 | /* find */ |
| 964 | spin_lock(&tree->lock); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 965 | entry = zswap_entry_find_get(&tree->rbroot, offset); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 966 | if (!entry) { |
| 967 | /* entry was written back */ |
| 968 | spin_unlock(&tree->lock); |
| 969 | return -1; |
| 970 | } |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 971 | spin_unlock(&tree->lock); |
| 972 | |
| 973 | /* decompress */ |
| 974 | dlen = PAGE_SIZE; |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 975 | src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle, |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 976 | ZPOOL_MM_RO) + sizeof(struct zswap_header); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 977 | dst = kmap_atomic(page); |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 978 | tfm = *get_cpu_ptr(entry->pool->tfm); |
| 979 | ret = crypto_comp_decompress(tfm, src, entry->length, dst, &dlen); |
| 980 | put_cpu_ptr(entry->pool->tfm); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 981 | kunmap_atomic(dst); |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 982 | zpool_unmap_handle(entry->pool->zpool, entry->handle); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 983 | BUG_ON(ret); |
| 984 | |
| 985 | spin_lock(&tree->lock); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 986 | zswap_entry_put(tree, entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 987 | spin_unlock(&tree->lock); |
| 988 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | /* frees an entry in zswap */ |
| 993 | static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset) |
| 994 | { |
| 995 | struct zswap_tree *tree = zswap_trees[type]; |
| 996 | struct zswap_entry *entry; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 997 | |
| 998 | /* find */ |
| 999 | spin_lock(&tree->lock); |
| 1000 | entry = zswap_rb_search(&tree->rbroot, offset); |
| 1001 | if (!entry) { |
| 1002 | /* entry was written back */ |
| 1003 | spin_unlock(&tree->lock); |
| 1004 | return; |
| 1005 | } |
| 1006 | |
| 1007 | /* remove from rbtree */ |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 1008 | zswap_rb_erase(&tree->rbroot, entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1009 | |
| 1010 | /* drop the initial reference from entry creation */ |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 1011 | zswap_entry_put(tree, entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1012 | |
| 1013 | spin_unlock(&tree->lock); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | /* frees all zswap entries for the given swap type */ |
| 1017 | static void zswap_frontswap_invalidate_area(unsigned type) |
| 1018 | { |
| 1019 | struct zswap_tree *tree = zswap_trees[type]; |
Cody P Schafer | 0bd4213 | 2013-09-11 14:25:33 -0700 | [diff] [blame] | 1020 | struct zswap_entry *entry, *n; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1021 | |
| 1022 | if (!tree) |
| 1023 | return; |
| 1024 | |
| 1025 | /* walk the tree and free everything */ |
| 1026 | spin_lock(&tree->lock); |
Weijie Yang | 0ab0abc | 2013-11-12 15:08:27 -0800 | [diff] [blame] | 1027 | rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode) |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 1028 | zswap_free_entry(entry); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1029 | tree->rbroot = RB_ROOT; |
| 1030 | spin_unlock(&tree->lock); |
Weijie Yang | aa9bca0 | 2013-10-16 13:46:54 -0700 | [diff] [blame] | 1031 | kfree(tree); |
| 1032 | zswap_trees[type] = NULL; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1035 | static void zswap_frontswap_init(unsigned type) |
| 1036 | { |
| 1037 | struct zswap_tree *tree; |
| 1038 | |
| 1039 | tree = kzalloc(sizeof(struct zswap_tree), GFP_KERNEL); |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 1040 | if (!tree) { |
| 1041 | pr_err("alloc failed, zswap disabled for swap type %d\n", type); |
| 1042 | return; |
| 1043 | } |
| 1044 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1045 | tree->rbroot = RB_ROOT; |
| 1046 | spin_lock_init(&tree->lock); |
| 1047 | zswap_trees[type] = tree; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | static struct frontswap_ops zswap_frontswap_ops = { |
| 1051 | .store = zswap_frontswap_store, |
| 1052 | .load = zswap_frontswap_load, |
| 1053 | .invalidate_page = zswap_frontswap_invalidate_page, |
| 1054 | .invalidate_area = zswap_frontswap_invalidate_area, |
| 1055 | .init = zswap_frontswap_init |
| 1056 | }; |
| 1057 | |
| 1058 | /********************************* |
| 1059 | * debugfs functions |
| 1060 | **********************************/ |
| 1061 | #ifdef CONFIG_DEBUG_FS |
| 1062 | #include <linux/debugfs.h> |
| 1063 | |
| 1064 | static struct dentry *zswap_debugfs_root; |
| 1065 | |
| 1066 | static int __init zswap_debugfs_init(void) |
| 1067 | { |
| 1068 | if (!debugfs_initialized()) |
| 1069 | return -ENODEV; |
| 1070 | |
| 1071 | zswap_debugfs_root = debugfs_create_dir("zswap", NULL); |
| 1072 | if (!zswap_debugfs_root) |
| 1073 | return -ENOMEM; |
| 1074 | |
| 1075 | debugfs_create_u64("pool_limit_hit", S_IRUGO, |
| 1076 | zswap_debugfs_root, &zswap_pool_limit_hit); |
| 1077 | debugfs_create_u64("reject_reclaim_fail", S_IRUGO, |
| 1078 | zswap_debugfs_root, &zswap_reject_reclaim_fail); |
| 1079 | debugfs_create_u64("reject_alloc_fail", S_IRUGO, |
| 1080 | zswap_debugfs_root, &zswap_reject_alloc_fail); |
| 1081 | debugfs_create_u64("reject_kmemcache_fail", S_IRUGO, |
| 1082 | zswap_debugfs_root, &zswap_reject_kmemcache_fail); |
| 1083 | debugfs_create_u64("reject_compress_poor", S_IRUGO, |
| 1084 | zswap_debugfs_root, &zswap_reject_compress_poor); |
| 1085 | debugfs_create_u64("written_back_pages", S_IRUGO, |
| 1086 | zswap_debugfs_root, &zswap_written_back_pages); |
| 1087 | debugfs_create_u64("duplicate_entry", S_IRUGO, |
| 1088 | zswap_debugfs_root, &zswap_duplicate_entry); |
Dan Streetman | 12d79d6 | 2014-08-06 16:08:40 -0700 | [diff] [blame] | 1089 | debugfs_create_u64("pool_total_size", S_IRUGO, |
| 1090 | zswap_debugfs_root, &zswap_pool_total_size); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1091 | debugfs_create_atomic_t("stored_pages", S_IRUGO, |
| 1092 | zswap_debugfs_root, &zswap_stored_pages); |
| 1093 | |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | static void __exit zswap_debugfs_exit(void) |
| 1098 | { |
| 1099 | debugfs_remove_recursive(zswap_debugfs_root); |
| 1100 | } |
| 1101 | #else |
| 1102 | static int __init zswap_debugfs_init(void) |
| 1103 | { |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | static void __exit zswap_debugfs_exit(void) { } |
| 1108 | #endif |
| 1109 | |
| 1110 | /********************************* |
| 1111 | * module init and exit |
| 1112 | **********************************/ |
| 1113 | static int __init init_zswap(void) |
| 1114 | { |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 1115 | struct zswap_pool *pool; |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 1116 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1117 | if (zswap_entry_cache_create()) { |
| 1118 | pr_err("entry cache creation failed\n"); |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 1119 | goto cache_fail; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1120 | } |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 1121 | |
| 1122 | if (zswap_cpu_dstmem_init()) { |
| 1123 | pr_err("dstmem alloc failed\n"); |
| 1124 | goto dstmem_fail; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1125 | } |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 1126 | |
| 1127 | pool = __zswap_pool_create_fallback(); |
| 1128 | if (!pool) { |
| 1129 | pr_err("pool creation failed\n"); |
| 1130 | goto pool_fail; |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1131 | } |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 1132 | pr_info("loaded using pool %s/%s\n", pool->tfm_name, |
| 1133 | zpool_get_type(pool->zpool)); |
| 1134 | |
| 1135 | list_add(&pool->list, &zswap_pools); |
Minchan Kim | 60105e1 | 2014-04-07 15:38:27 -0700 | [diff] [blame] | 1136 | |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1137 | frontswap_register_ops(&zswap_frontswap_ops); |
| 1138 | if (zswap_debugfs_init()) |
| 1139 | pr_warn("debugfs initialization failed\n"); |
| 1140 | return 0; |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 1141 | |
| 1142 | pool_fail: |
| 1143 | zswap_cpu_dstmem_destroy(); |
| 1144 | dstmem_fail: |
Fabian Frederick | c119239 | 2014-08-08 14:19:35 -0700 | [diff] [blame] | 1145 | zswap_entry_cache_destroy(); |
Dan Streetman | f1c5484 | 2015-09-09 15:35:19 -0700 | [diff] [blame^] | 1146 | cache_fail: |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1147 | return -ENOMEM; |
| 1148 | } |
| 1149 | /* must be late so crypto has time to come up */ |
| 1150 | late_initcall(init_zswap); |
| 1151 | |
| 1152 | MODULE_LICENSE("GPL"); |
Seth Jennings | 68386da | 2014-11-12 21:08:46 -0600 | [diff] [blame] | 1153 | MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>"); |
Seth Jennings | 2b28111 | 2013-07-10 16:05:03 -0700 | [diff] [blame] | 1154 | MODULE_DESCRIPTION("Compressed cache for swap pages"); |