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 { 21 char *type; 22 23 struct zpool_driver *driver; 24 void *pool; 25 const struct zpool_ops *ops; 26 27 struct list_head list; 28 }; 29 30 static LIST_HEAD(drivers_head); 31 static DEFINE_SPINLOCK(drivers_lock); 32 33 static LIST_HEAD(pools_head); 34 static DEFINE_SPINLOCK(pools_lock); 35 36 /** 37 * zpool_register_driver() - register a zpool implementation. 38 * @driver: driver to register 39 */ 40 void zpool_register_driver(struct zpool_driver *driver) 41 { 42 spin_lock(&drivers_lock); 43 atomic_set(&driver->refcount, 0); 44 list_add(&driver->list, &drivers_head); 45 spin_unlock(&drivers_lock); 46 } 47 EXPORT_SYMBOL(zpool_register_driver); 48 49 /** 50 * zpool_unregister_driver() - unregister a zpool implementation. 51 * @driver: driver to unregister. 52 * 53 * Module usage counting is used to prevent using a driver 54 * while/after unloading, so if this is called from module 55 * exit function, this should never fail; if called from 56 * other than the module exit function, and this returns 57 * failure, the driver is in use and must remain available. 58 */ 59 int zpool_unregister_driver(struct zpool_driver *driver) 60 { 61 int ret = 0, refcount; 62 63 spin_lock(&drivers_lock); 64 refcount = atomic_read(&driver->refcount); 65 WARN_ON(refcount < 0); 66 if (refcount > 0) 67 ret = -EBUSY; 68 else 69 list_del(&driver->list); 70 spin_unlock(&drivers_lock); 71 72 return ret; 73 } 74 EXPORT_SYMBOL(zpool_unregister_driver); 75 76 static struct zpool_driver *zpool_get_driver(char *type) 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 /** 103 * zpool_has_pool() - Check if the pool driver is available 104 * @type The type of the zpool to check (e.g. zbud, zsmalloc) 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 * 116 * Returns: true if @type pool is available, false if not 117 */ 118 bool zpool_has_pool(char *type) 119 { 120 struct zpool_driver *driver = zpool_get_driver(type); 121 122 if (!driver) { 123 request_module("zpool-%s", type); 124 driver = zpool_get_driver(type); 125 } 126 127 if (!driver) 128 return false; 129 130 zpool_put_driver(driver); 131 return true; 132 } 133 EXPORT_SYMBOL(zpool_has_pool); 134 135 /** 136 * zpool_create_pool() - Create a new zpool 137 * @type The type of the zpool to create (e.g. zbud, zsmalloc) 138 * @name The name of the zpool (e.g. zram0, zswap) 139 * @gfp The GFP flags to use when allocating the pool. 140 * @ops The optional ops callback. 141 * 142 * This creates a new zpool of the specified type. The gfp flags will be 143 * used when allocating memory, if the implementation supports it. If the 144 * ops param is NULL, then the created zpool will not be shrinkable. 145 * 146 * Implementations must guarantee this to be thread-safe. 147 * 148 * Returns: New zpool on success, NULL on failure. 149 */ 150 struct zpool *zpool_create_pool(char *type, char *name, gfp_t gfp, 151 const struct zpool_ops *ops) 152 { 153 struct zpool_driver *driver; 154 struct zpool *zpool; 155 156 pr_debug("creating pool type %s\n", type); 157 158 driver = zpool_get_driver(type); 159 160 if (!driver) { 161 request_module("zpool-%s", type); 162 driver = zpool_get_driver(type); 163 } 164 165 if (!driver) { 166 pr_err("no driver for type %s\n", type); 167 return NULL; 168 } 169 170 zpool = kmalloc(sizeof(*zpool), gfp); 171 if (!zpool) { 172 pr_err("couldn't create zpool - out of memory\n"); 173 zpool_put_driver(driver); 174 return NULL; 175 } 176 177 zpool->type = driver->type; 178 zpool->driver = driver; 179 zpool->pool = driver->create(name, gfp, ops, zpool); 180 zpool->ops = ops; 181 182 if (!zpool->pool) { 183 pr_err("couldn't create %s pool\n", type); 184 zpool_put_driver(driver); 185 kfree(zpool); 186 return NULL; 187 } 188 189 pr_debug("created pool type %s\n", type); 190 191 spin_lock(&pools_lock); 192 list_add(&zpool->list, &pools_head); 193 spin_unlock(&pools_lock); 194 195 return zpool; 196 } 197 198 /** 199 * zpool_destroy_pool() - Destroy a zpool 200 * @pool The zpool to destroy. 201 * 202 * Implementations must guarantee this to be thread-safe, 203 * however only when destroying different pools. The same 204 * pool should only be destroyed once, and should not be used 205 * after it is destroyed. 206 * 207 * This destroys an existing zpool. The zpool should not be in use. 208 */ 209 void zpool_destroy_pool(struct zpool *zpool) 210 { 211 pr_debug("destroying pool type %s\n", zpool->type); 212 213 spin_lock(&pools_lock); 214 list_del(&zpool->list); 215 spin_unlock(&pools_lock); 216 zpool->driver->destroy(zpool->pool); 217 zpool_put_driver(zpool->driver); 218 kfree(zpool); 219 } 220 221 /** 222 * zpool_get_type() - Get the type of the zpool 223 * @pool The zpool to check 224 * 225 * This returns the type of the pool. 226 * 227 * Implementations must guarantee this to be thread-safe. 228 * 229 * Returns: The type of zpool. 230 */ 231 char *zpool_get_type(struct zpool *zpool) 232 { 233 return zpool->type; 234 } 235 236 /** 237 * zpool_malloc() - Allocate memory 238 * @pool The zpool to allocate from. 239 * @size The amount of memory to allocate. 240 * @gfp The GFP flags to use when allocating memory. 241 * @handle Pointer to the handle to set 242 * 243 * This allocates the requested amount of memory from the pool. 244 * The gfp flags will be used when allocating memory, if the 245 * implementation supports it. The provided @handle will be 246 * set to the allocated object handle. 247 * 248 * Implementations must guarantee this to be thread-safe. 249 * 250 * Returns: 0 on success, negative value on error. 251 */ 252 int zpool_malloc(struct zpool *zpool, size_t size, gfp_t gfp, 253 unsigned long *handle) 254 { 255 return zpool->driver->malloc(zpool->pool, size, gfp, handle); 256 } 257 258 /** 259 * zpool_free() - Free previously allocated memory 260 * @pool The zpool that allocated the memory. 261 * @handle The handle to the memory to free. 262 * 263 * This frees previously allocated memory. This does not guarantee 264 * that the pool will actually free memory, only that the memory 265 * in the pool will become available for use by the pool. 266 * 267 * Implementations must guarantee this to be thread-safe, 268 * however only when freeing different handles. The same 269 * handle should only be freed once, and should not be used 270 * after freeing. 271 */ 272 void zpool_free(struct zpool *zpool, unsigned long handle) 273 { 274 zpool->driver->free(zpool->pool, handle); 275 } 276 277 /** 278 * zpool_shrink() - Shrink the pool size 279 * @pool The zpool to shrink. 280 * @pages The number of pages to shrink the pool. 281 * @reclaimed The number of pages successfully evicted. 282 * 283 * This attempts to shrink the actual memory size of the pool 284 * by evicting currently used handle(s). If the pool was 285 * created with no zpool_ops, or the evict call fails for any 286 * of the handles, this will fail. If non-NULL, the @reclaimed 287 * parameter will be set to the number of pages reclaimed, 288 * which may be more than the number of pages requested. 289 * 290 * Implementations must guarantee this to be thread-safe. 291 * 292 * Returns: 0 on success, negative value on error/failure. 293 */ 294 int zpool_shrink(struct zpool *zpool, unsigned int pages, 295 unsigned int *reclaimed) 296 { 297 return zpool->driver->shrink(zpool->pool, pages, reclaimed); 298 } 299 300 /** 301 * zpool_map_handle() - Map a previously allocated handle into memory 302 * @pool The zpool that the handle was allocated from 303 * @handle The handle to map 304 * @mm How the memory should be mapped 305 * 306 * This maps a previously allocated handle into memory. The @mm 307 * param indicates to the implementation how the memory will be 308 * used, i.e. read-only, write-only, read-write. If the 309 * implementation does not support it, the memory will be treated 310 * as read-write. 311 * 312 * This may hold locks, disable interrupts, and/or preemption, 313 * and the zpool_unmap_handle() must be called to undo those 314 * actions. The code that uses the mapped handle should complete 315 * its operatons on the mapped handle memory quickly and unmap 316 * as soon as possible. As the implementation may use per-cpu 317 * data, multiple handles should not be mapped concurrently on 318 * any cpu. 319 * 320 * Returns: A pointer to the handle's mapped memory area. 321 */ 322 void *zpool_map_handle(struct zpool *zpool, unsigned long handle, 323 enum zpool_mapmode mapmode) 324 { 325 return zpool->driver->map(zpool->pool, handle, mapmode); 326 } 327 328 /** 329 * zpool_unmap_handle() - Unmap a previously mapped handle 330 * @pool The zpool that the handle was allocated from 331 * @handle The handle to unmap 332 * 333 * This unmaps a previously mapped handle. Any locks or other 334 * actions that the implementation took in zpool_map_handle() 335 * will be undone here. The memory area returned from 336 * zpool_map_handle() should no longer be used after this. 337 */ 338 void zpool_unmap_handle(struct zpool *zpool, unsigned long handle) 339 { 340 zpool->driver->unmap(zpool->pool, handle); 341 } 342 343 /** 344 * zpool_get_total_size() - The total size of the pool 345 * @pool The zpool to check 346 * 347 * This returns the total size in bytes of the pool. 348 * 349 * Returns: Total size of the zpool in bytes. 350 */ 351 u64 zpool_get_total_size(struct zpool *zpool) 352 { 353 return zpool->driver->total_size(zpool->pool); 354 } 355 356 MODULE_LICENSE("GPL"); 357 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>"); 358 MODULE_DESCRIPTION("Common API for compressed memory storage"); 359