Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * zpool memory storage api |
| 3 | * |
| 4 | * Copyright (C) 2014 Dan Streetman |
| 5 | * |
| 6 | * This is a common frontend for memory storage pool implementations. |
| 7 | * Typically, this is used to store compressed memory. |
| 8 | */ |
| 9 | |
| 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 | |
| 12 | #include <linux/list.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/mm.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/zpool.h> |
| 19 | |
| 20 | struct zpool { |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 21 | struct zpool_driver *driver; |
| 22 | void *pool; |
Krzysztof Kozlowski | 7867277 | 2015-09-08 15:05:03 -0700 | [diff] [blame] | 23 | const struct zpool_ops *ops; |
Yu Zhao | 9c3760e | 2018-01-31 16:19:59 -0800 | [diff] [blame] | 24 | bool evictable; |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 25 | |
| 26 | struct list_head list; |
| 27 | }; |
| 28 | |
| 29 | static LIST_HEAD(drivers_head); |
| 30 | static DEFINE_SPINLOCK(drivers_lock); |
| 31 | |
| 32 | static LIST_HEAD(pools_head); |
| 33 | static DEFINE_SPINLOCK(pools_lock); |
| 34 | |
| 35 | /** |
| 36 | * zpool_register_driver() - register a zpool implementation. |
| 37 | * @driver: driver to register |
| 38 | */ |
| 39 | void zpool_register_driver(struct zpool_driver *driver) |
| 40 | { |
| 41 | spin_lock(&drivers_lock); |
| 42 | atomic_set(&driver->refcount, 0); |
| 43 | list_add(&driver->list, &drivers_head); |
| 44 | spin_unlock(&drivers_lock); |
| 45 | } |
| 46 | EXPORT_SYMBOL(zpool_register_driver); |
| 47 | |
| 48 | /** |
| 49 | * zpool_unregister_driver() - unregister a zpool implementation. |
| 50 | * @driver: driver to unregister. |
| 51 | * |
| 52 | * Module usage counting is used to prevent using a driver |
| 53 | * while/after unloading, so if this is called from module |
| 54 | * exit function, this should never fail; if called from |
| 55 | * other than the module exit function, and this returns |
| 56 | * failure, the driver is in use and must remain available. |
| 57 | */ |
| 58 | int zpool_unregister_driver(struct zpool_driver *driver) |
| 59 | { |
| 60 | int ret = 0, refcount; |
| 61 | |
| 62 | spin_lock(&drivers_lock); |
| 63 | refcount = atomic_read(&driver->refcount); |
| 64 | WARN_ON(refcount < 0); |
| 65 | if (refcount > 0) |
| 66 | ret = -EBUSY; |
| 67 | else |
| 68 | list_del(&driver->list); |
| 69 | spin_unlock(&drivers_lock); |
| 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | EXPORT_SYMBOL(zpool_unregister_driver); |
| 74 | |
Dan Streetman | 69e18f4 | 2015-11-06 16:29:18 -0800 | [diff] [blame] | 75 | /* this assumes @type is null-terminated. */ |
Sergey SENOZHATSKY | 6f3526d | 2015-11-06 16:29:21 -0800 | [diff] [blame] | 76 | static struct zpool_driver *zpool_get_driver(const char *type) |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 77 | { |
| 78 | struct zpool_driver *driver; |
| 79 | |
| 80 | spin_lock(&drivers_lock); |
| 81 | list_for_each_entry(driver, &drivers_head, list) { |
| 82 | if (!strcmp(driver->type, type)) { |
| 83 | bool got = try_module_get(driver->owner); |
| 84 | |
| 85 | if (got) |
| 86 | atomic_inc(&driver->refcount); |
| 87 | spin_unlock(&drivers_lock); |
| 88 | return got ? driver : NULL; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | spin_unlock(&drivers_lock); |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | static void zpool_put_driver(struct zpool_driver *driver) |
| 97 | { |
| 98 | atomic_dec(&driver->refcount); |
| 99 | module_put(driver->owner); |
| 100 | } |
| 101 | |
| 102 | /** |
Dan Streetman | 3f0e131 | 2015-09-09 15:35:16 -0700 | [diff] [blame] | 103 | * zpool_has_pool() - Check if the pool driver is available |
Mike Rapoport | b7701a5 | 2018-02-06 15:42:13 -0800 | [diff] [blame] | 104 | * @type: The type of the zpool to check (e.g. zbud, zsmalloc) |
Dan Streetman | 3f0e131 | 2015-09-09 15:35:16 -0700 | [diff] [blame] | 105 | * |
| 106 | * This checks if the @type pool driver is available. This will try to load |
| 107 | * the requested module, if needed, but there is no guarantee the module will |
| 108 | * still be loaded and available immediately after calling. If this returns |
| 109 | * true, the caller should assume the pool is available, but must be prepared |
| 110 | * to handle the @zpool_create_pool() returning failure. However if this |
| 111 | * returns false, the caller should assume the requested pool type is not |
| 112 | * available; either the requested pool type module does not exist, or could |
| 113 | * not be loaded, and calling @zpool_create_pool() with the pool type will |
| 114 | * fail. |
| 115 | * |
Dan Streetman | 69e18f4 | 2015-11-06 16:29:18 -0800 | [diff] [blame] | 116 | * The @type string must be null-terminated. |
| 117 | * |
Dan Streetman | 3f0e131 | 2015-09-09 15:35:16 -0700 | [diff] [blame] | 118 | * Returns: true if @type pool is available, false if not |
| 119 | */ |
| 120 | bool zpool_has_pool(char *type) |
| 121 | { |
| 122 | struct zpool_driver *driver = zpool_get_driver(type); |
| 123 | |
| 124 | if (!driver) { |
| 125 | request_module("zpool-%s", type); |
| 126 | driver = zpool_get_driver(type); |
| 127 | } |
| 128 | |
| 129 | if (!driver) |
| 130 | return false; |
| 131 | |
| 132 | zpool_put_driver(driver); |
| 133 | return true; |
| 134 | } |
| 135 | EXPORT_SYMBOL(zpool_has_pool); |
| 136 | |
| 137 | /** |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 138 | * zpool_create_pool() - Create a new zpool |
Mike Rapoport | b7701a5 | 2018-02-06 15:42:13 -0800 | [diff] [blame] | 139 | * @type: The type of the zpool to create (e.g. zbud, zsmalloc) |
| 140 | * @name: The name of the zpool (e.g. zram0, zswap) |
| 141 | * @gfp: The GFP flags to use when allocating the pool. |
| 142 | * @ops: The optional ops callback. |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 143 | * |
| 144 | * This creates a new zpool of the specified type. The gfp flags will be |
| 145 | * used when allocating memory, if the implementation supports it. If the |
Yu Zhao | 9c3760e | 2018-01-31 16:19:59 -0800 | [diff] [blame] | 146 | * ops param is NULL, then the created zpool will not be evictable. |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 147 | * |
| 148 | * Implementations must guarantee this to be thread-safe. |
| 149 | * |
Dan Streetman | 69e18f4 | 2015-11-06 16:29:18 -0800 | [diff] [blame] | 150 | * The @type and @name strings must be null-terminated. |
| 151 | * |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 152 | * Returns: New zpool on success, NULL on failure. |
| 153 | */ |
Sergey SENOZHATSKY | 6f3526d | 2015-11-06 16:29:21 -0800 | [diff] [blame] | 154 | struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp, |
Krzysztof Kozlowski | 7867277 | 2015-09-08 15:05:03 -0700 | [diff] [blame] | 155 | const struct zpool_ops *ops) |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 156 | { |
| 157 | struct zpool_driver *driver; |
| 158 | struct zpool *zpool; |
| 159 | |
Dan Streetman | cf41f5f | 2015-06-25 15:00:37 -0700 | [diff] [blame] | 160 | pr_debug("creating pool type %s\n", type); |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 161 | |
| 162 | driver = zpool_get_driver(type); |
| 163 | |
| 164 | if (!driver) { |
Kees Cook | 137f8cf | 2014-08-29 15:18:40 -0700 | [diff] [blame] | 165 | request_module("zpool-%s", type); |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 166 | driver = zpool_get_driver(type); |
| 167 | } |
| 168 | |
| 169 | if (!driver) { |
| 170 | pr_err("no driver for type %s\n", type); |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | zpool = kmalloc(sizeof(*zpool), gfp); |
| 175 | if (!zpool) { |
| 176 | pr_err("couldn't create zpool - out of memory\n"); |
| 177 | zpool_put_driver(driver); |
| 178 | return NULL; |
| 179 | } |
| 180 | |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 181 | zpool->driver = driver; |
Dan Streetman | 479305f | 2015-06-25 15:00:40 -0700 | [diff] [blame] | 182 | zpool->pool = driver->create(name, gfp, ops, zpool); |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 183 | zpool->ops = ops; |
Yu Zhao | 9c3760e | 2018-01-31 16:19:59 -0800 | [diff] [blame] | 184 | zpool->evictable = driver->shrink && ops && ops->evict; |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 185 | |
| 186 | if (!zpool->pool) { |
| 187 | pr_err("couldn't create %s pool\n", type); |
| 188 | zpool_put_driver(driver); |
| 189 | kfree(zpool); |
| 190 | return NULL; |
| 191 | } |
| 192 | |
Dan Streetman | cf41f5f | 2015-06-25 15:00:37 -0700 | [diff] [blame] | 193 | pr_debug("created pool type %s\n", type); |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 194 | |
| 195 | spin_lock(&pools_lock); |
| 196 | list_add(&zpool->list, &pools_head); |
| 197 | spin_unlock(&pools_lock); |
| 198 | |
| 199 | return zpool; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * zpool_destroy_pool() - Destroy a zpool |
Mike Rapoport | f144c39 | 2018-02-06 15:42:16 -0800 | [diff] [blame] | 204 | * @zpool: The zpool to destroy. |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 205 | * |
| 206 | * Implementations must guarantee this to be thread-safe, |
| 207 | * however only when destroying different pools. The same |
| 208 | * pool should only be destroyed once, and should not be used |
| 209 | * after it is destroyed. |
| 210 | * |
| 211 | * This destroys an existing zpool. The zpool should not be in use. |
| 212 | */ |
| 213 | void zpool_destroy_pool(struct zpool *zpool) |
| 214 | { |
Dan Streetman | 69e18f4 | 2015-11-06 16:29:18 -0800 | [diff] [blame] | 215 | pr_debug("destroying pool type %s\n", zpool->driver->type); |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 216 | |
| 217 | spin_lock(&pools_lock); |
| 218 | list_del(&zpool->list); |
| 219 | spin_unlock(&pools_lock); |
| 220 | zpool->driver->destroy(zpool->pool); |
| 221 | zpool_put_driver(zpool->driver); |
| 222 | kfree(zpool); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * zpool_get_type() - Get the type of the zpool |
Mike Rapoport | f144c39 | 2018-02-06 15:42:16 -0800 | [diff] [blame] | 227 | * @zpool: The zpool to check |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 228 | * |
| 229 | * This returns the type of the pool. |
| 230 | * |
| 231 | * Implementations must guarantee this to be thread-safe. |
| 232 | * |
| 233 | * Returns: The type of zpool. |
| 234 | */ |
Dan Streetman | 69e18f4 | 2015-11-06 16:29:18 -0800 | [diff] [blame] | 235 | const char *zpool_get_type(struct zpool *zpool) |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 236 | { |
Dan Streetman | 69e18f4 | 2015-11-06 16:29:18 -0800 | [diff] [blame] | 237 | return zpool->driver->type; |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
| 241 | * zpool_malloc() - Allocate memory |
Mike Rapoport | f144c39 | 2018-02-06 15:42:16 -0800 | [diff] [blame] | 242 | * @zpool: The zpool to allocate from. |
Mike Rapoport | b7701a5 | 2018-02-06 15:42:13 -0800 | [diff] [blame] | 243 | * @size: The amount of memory to allocate. |
| 244 | * @gfp: The GFP flags to use when allocating memory. |
| 245 | * @handle: Pointer to the handle to set |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 246 | * |
| 247 | * This allocates the requested amount of memory from the pool. |
| 248 | * The gfp flags will be used when allocating memory, if the |
| 249 | * implementation supports it. The provided @handle will be |
| 250 | * set to the allocated object handle. |
| 251 | * |
| 252 | * Implementations must guarantee this to be thread-safe. |
| 253 | * |
| 254 | * Returns: 0 on success, negative value on error. |
| 255 | */ |
| 256 | int zpool_malloc(struct zpool *zpool, size_t size, gfp_t gfp, |
| 257 | unsigned long *handle) |
| 258 | { |
| 259 | return zpool->driver->malloc(zpool->pool, size, gfp, handle); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * zpool_free() - Free previously allocated memory |
Mike Rapoport | f144c39 | 2018-02-06 15:42:16 -0800 | [diff] [blame] | 264 | * @zpool: The zpool that allocated the memory. |
Mike Rapoport | b7701a5 | 2018-02-06 15:42:13 -0800 | [diff] [blame] | 265 | * @handle: The handle to the memory to free. |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 266 | * |
| 267 | * This frees previously allocated memory. This does not guarantee |
| 268 | * that the pool will actually free memory, only that the memory |
| 269 | * in the pool will become available for use by the pool. |
| 270 | * |
| 271 | * Implementations must guarantee this to be thread-safe, |
| 272 | * however only when freeing different handles. The same |
| 273 | * handle should only be freed once, and should not be used |
| 274 | * after freeing. |
| 275 | */ |
| 276 | void zpool_free(struct zpool *zpool, unsigned long handle) |
| 277 | { |
| 278 | zpool->driver->free(zpool->pool, handle); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * zpool_shrink() - Shrink the pool size |
Mike Rapoport | f144c39 | 2018-02-06 15:42:16 -0800 | [diff] [blame] | 283 | * @zpool: The zpool to shrink. |
Mike Rapoport | b7701a5 | 2018-02-06 15:42:13 -0800 | [diff] [blame] | 284 | * @pages: The number of pages to shrink the pool. |
| 285 | * @reclaimed: The number of pages successfully evicted. |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 286 | * |
| 287 | * This attempts to shrink the actual memory size of the pool |
| 288 | * by evicting currently used handle(s). If the pool was |
| 289 | * created with no zpool_ops, or the evict call fails for any |
| 290 | * of the handles, this will fail. If non-NULL, the @reclaimed |
| 291 | * parameter will be set to the number of pages reclaimed, |
| 292 | * which may be more than the number of pages requested. |
| 293 | * |
| 294 | * Implementations must guarantee this to be thread-safe. |
| 295 | * |
| 296 | * Returns: 0 on success, negative value on error/failure. |
| 297 | */ |
| 298 | int zpool_shrink(struct zpool *zpool, unsigned int pages, |
| 299 | unsigned int *reclaimed) |
| 300 | { |
Yu Zhao | 9c3760e | 2018-01-31 16:19:59 -0800 | [diff] [blame] | 301 | return zpool->driver->shrink ? |
| 302 | zpool->driver->shrink(zpool->pool, pages, reclaimed) : -EINVAL; |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /** |
| 306 | * zpool_map_handle() - Map a previously allocated handle into memory |
Mike Rapoport | f144c39 | 2018-02-06 15:42:16 -0800 | [diff] [blame] | 307 | * @zpool: The zpool that the handle was allocated from |
Mike Rapoport | b7701a5 | 2018-02-06 15:42:13 -0800 | [diff] [blame] | 308 | * @handle: The handle to map |
Mike Rapoport | f144c39 | 2018-02-06 15:42:16 -0800 | [diff] [blame] | 309 | * @mapmode: How the memory should be mapped |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 310 | * |
Mike Rapoport | f144c39 | 2018-02-06 15:42:16 -0800 | [diff] [blame] | 311 | * This maps a previously allocated handle into memory. The @mapmode |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 312 | * param indicates to the implementation how the memory will be |
| 313 | * used, i.e. read-only, write-only, read-write. If the |
| 314 | * implementation does not support it, the memory will be treated |
| 315 | * as read-write. |
| 316 | * |
| 317 | * This may hold locks, disable interrupts, and/or preemption, |
| 318 | * and the zpool_unmap_handle() must be called to undo those |
| 319 | * actions. The code that uses the mapped handle should complete |
| 320 | * its operatons on the mapped handle memory quickly and unmap |
| 321 | * as soon as possible. As the implementation may use per-cpu |
| 322 | * data, multiple handles should not be mapped concurrently on |
| 323 | * any cpu. |
| 324 | * |
| 325 | * Returns: A pointer to the handle's mapped memory area. |
| 326 | */ |
| 327 | void *zpool_map_handle(struct zpool *zpool, unsigned long handle, |
| 328 | enum zpool_mapmode mapmode) |
| 329 | { |
| 330 | return zpool->driver->map(zpool->pool, handle, mapmode); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * zpool_unmap_handle() - Unmap a previously mapped handle |
Mike Rapoport | f144c39 | 2018-02-06 15:42:16 -0800 | [diff] [blame] | 335 | * @zpool: The zpool that the handle was allocated from |
Mike Rapoport | b7701a5 | 2018-02-06 15:42:13 -0800 | [diff] [blame] | 336 | * @handle: The handle to unmap |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 337 | * |
| 338 | * This unmaps a previously mapped handle. Any locks or other |
| 339 | * actions that the implementation took in zpool_map_handle() |
| 340 | * will be undone here. The memory area returned from |
| 341 | * zpool_map_handle() should no longer be used after this. |
| 342 | */ |
| 343 | void zpool_unmap_handle(struct zpool *zpool, unsigned long handle) |
| 344 | { |
| 345 | zpool->driver->unmap(zpool->pool, handle); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * zpool_get_total_size() - The total size of the pool |
Mike Rapoport | f144c39 | 2018-02-06 15:42:16 -0800 | [diff] [blame] | 350 | * @zpool: The zpool to check |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 351 | * |
| 352 | * This returns the total size in bytes of the pool. |
| 353 | * |
| 354 | * Returns: Total size of the zpool in bytes. |
| 355 | */ |
| 356 | u64 zpool_get_total_size(struct zpool *zpool) |
| 357 | { |
| 358 | return zpool->driver->total_size(zpool->pool); |
| 359 | } |
| 360 | |
Yu Zhao | 9c3760e | 2018-01-31 16:19:59 -0800 | [diff] [blame] | 361 | /** |
| 362 | * zpool_evictable() - Test if zpool is potentially evictable |
Mike Rapoport | 14fec9e | 2018-02-21 14:45:46 -0800 | [diff] [blame] | 363 | * @zpool: The zpool to test |
Yu Zhao | 9c3760e | 2018-01-31 16:19:59 -0800 | [diff] [blame] | 364 | * |
| 365 | * Zpool is only potentially evictable when it's created with struct |
| 366 | * zpool_ops.evict and its driver implements struct zpool_driver.shrink. |
| 367 | * |
| 368 | * However, it doesn't necessarily mean driver will use zpool_ops.evict |
| 369 | * in its implementation of zpool_driver.shrink. It could do internal |
| 370 | * defragmentation instead. |
| 371 | * |
| 372 | * Returns: true if potentially evictable; false otherwise. |
| 373 | */ |
| 374 | bool zpool_evictable(struct zpool *zpool) |
| 375 | { |
| 376 | return zpool->evictable; |
| 377 | } |
| 378 | |
Dan Streetman | af8d417 | 2014-08-06 16:08:36 -0700 | [diff] [blame] | 379 | MODULE_LICENSE("GPL"); |
| 380 | MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>"); |
| 381 | MODULE_DESCRIPTION("Common API for compressed memory storage"); |