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