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