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