![]() |
|
|||
0001 // SPDX-License-Identifier: GPL-2.0-only 0002 /* 0003 * zpool memory storage api 0004 * 0005 * Copyright (C) 2014 Dan Streetman 0006 * 0007 * This is a common frontend for memory storage pool implementations. 0008 * Typically, this is used to store compressed memory. 0009 */ 0010 0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 0012 0013 #include <linux/list.h> 0014 #include <linux/types.h> 0015 #include <linux/mm.h> 0016 #include <linux/slab.h> 0017 #include <linux/spinlock.h> 0018 #include <linux/module.h> 0019 #include <linux/zpool.h> 0020 0021 struct zpool { 0022 struct zpool_driver *driver; 0023 void *pool; 0024 const struct zpool_ops *ops; 0025 bool evictable; 0026 bool can_sleep_mapped; 0027 }; 0028 0029 static LIST_HEAD(drivers_head); 0030 static DEFINE_SPINLOCK(drivers_lock); 0031 0032 /** 0033 * zpool_register_driver() - register a zpool implementation. 0034 * @driver: driver to register 0035 */ 0036 void zpool_register_driver(struct zpool_driver *driver) 0037 { 0038 spin_lock(&drivers_lock); 0039 atomic_set(&driver->refcount, 0); 0040 list_add(&driver->list, &drivers_head); 0041 spin_unlock(&drivers_lock); 0042 } 0043 EXPORT_SYMBOL(zpool_register_driver); 0044 0045 /** 0046 * zpool_unregister_driver() - unregister a zpool implementation. 0047 * @driver: driver to unregister. 0048 * 0049 * Module usage counting is used to prevent using a driver 0050 * while/after unloading, so if this is called from module 0051 * exit function, this should never fail; if called from 0052 * other than the module exit function, and this returns 0053 * failure, the driver is in use and must remain available. 0054 */ 0055 int zpool_unregister_driver(struct zpool_driver *driver) 0056 { 0057 int ret = 0, refcount; 0058 0059 spin_lock(&drivers_lock); 0060 refcount = atomic_read(&driver->refcount); 0061 WARN_ON(refcount < 0); 0062 if (refcount > 0) 0063 ret = -EBUSY; 0064 else 0065 list_del(&driver->list); 0066 spin_unlock(&drivers_lock); 0067 0068 return ret; 0069 } 0070 EXPORT_SYMBOL(zpool_unregister_driver); 0071 0072 /* this assumes @type is null-terminated. */ 0073 static struct zpool_driver *zpool_get_driver(const char *type) 0074 { 0075 struct zpool_driver *driver; 0076 0077 spin_lock(&drivers_lock); 0078 list_for_each_entry(driver, &drivers_head, list) { 0079 if (!strcmp(driver->type, type)) { 0080 bool got = try_module_get(driver->owner); 0081 0082 if (got) 0083 atomic_inc(&driver->refcount); 0084 spin_unlock(&drivers_lock); 0085 return got ? driver : NULL; 0086 } 0087 } 0088 0089 spin_unlock(&drivers_lock); 0090 return NULL; 0091 } 0092 0093 static void zpool_put_driver(struct zpool_driver *driver) 0094 { 0095 atomic_dec(&driver->refcount); 0096 module_put(driver->owner); 0097 } 0098 0099 /** 0100 * zpool_has_pool() - Check if the pool driver is available 0101 * @type: The type of the zpool to check (e.g. zbud, zsmalloc) 0102 * 0103 * This checks if the @type pool driver is available. This will try to load 0104 * the requested module, if needed, but there is no guarantee the module will 0105 * still be loaded and available immediately after calling. If this returns 0106 * true, the caller should assume the pool is available, but must be prepared 0107 * to handle the @zpool_create_pool() returning failure. However if this 0108 * returns false, the caller should assume the requested pool type is not 0109 * available; either the requested pool type module does not exist, or could 0110 * not be loaded, and calling @zpool_create_pool() with the pool type will 0111 * fail. 0112 * 0113 * The @type string must be null-terminated. 0114 * 0115 * Returns: true if @type pool is available, false if not 0116 */ 0117 bool zpool_has_pool(char *type) 0118 { 0119 struct zpool_driver *driver = zpool_get_driver(type); 0120 0121 if (!driver) { 0122 request_module("zpool-%s", type); 0123 driver = zpool_get_driver(type); 0124 } 0125 0126 if (!driver) 0127 return false; 0128 0129 zpool_put_driver(driver); 0130 return true; 0131 } 0132 EXPORT_SYMBOL(zpool_has_pool); 0133 0134 /** 0135 * zpool_create_pool() - Create a new zpool 0136 * @type: The type of the zpool to create (e.g. zbud, zsmalloc) 0137 * @name: The name of the zpool (e.g. zram0, zswap) 0138 * @gfp: The GFP flags to use when allocating the pool. 0139 * @ops: The optional ops callback. 0140 * 0141 * This creates a new zpool of the specified type. The gfp flags will be 0142 * used when allocating memory, if the implementation supports it. If the 0143 * ops param is NULL, then the created zpool will not be evictable. 0144 * 0145 * Implementations must guarantee this to be thread-safe. 0146 * 0147 * The @type and @name strings must be null-terminated. 0148 * 0149 * Returns: New zpool on success, NULL on failure. 0150 */ 0151 struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp, 0152 const struct zpool_ops *ops) 0153 { 0154 struct zpool_driver *driver; 0155 struct zpool *zpool; 0156 0157 pr_debug("creating pool type %s\n", type); 0158 0159 driver = zpool_get_driver(type); 0160 0161 if (!driver) { 0162 request_module("zpool-%s", type); 0163 driver = zpool_get_driver(type); 0164 } 0165 0166 if (!driver) { 0167 pr_err("no driver for type %s\n", type); 0168 return NULL; 0169 } 0170 0171 zpool = kmalloc(sizeof(*zpool), gfp); 0172 if (!zpool) { 0173 pr_err("couldn't create zpool - out of memory\n"); 0174 zpool_put_driver(driver); 0175 return NULL; 0176 } 0177 0178 zpool->driver = driver; 0179 zpool->pool = driver->create(name, gfp, ops, zpool); 0180 zpool->ops = ops; 0181 zpool->evictable = driver->shrink && ops && ops->evict; 0182 zpool->can_sleep_mapped = driver->sleep_mapped; 0183 0184 if (!zpool->pool) { 0185 pr_err("couldn't create %s pool\n", type); 0186 zpool_put_driver(driver); 0187 kfree(zpool); 0188 return NULL; 0189 } 0190 0191 pr_debug("created pool type %s\n", type); 0192 0193 return zpool; 0194 } 0195 0196 /** 0197 * zpool_destroy_pool() - Destroy a zpool 0198 * @zpool: The zpool to destroy. 0199 * 0200 * Implementations must guarantee this to be thread-safe, 0201 * however only when destroying different pools. The same 0202 * pool should only be destroyed once, and should not be used 0203 * after it is destroyed. 0204 * 0205 * This destroys an existing zpool. The zpool should not be in use. 0206 */ 0207 void zpool_destroy_pool(struct zpool *zpool) 0208 { 0209 pr_debug("destroying pool type %s\n", zpool->driver->type); 0210 0211 zpool->driver->destroy(zpool->pool); 0212 zpool_put_driver(zpool->driver); 0213 kfree(zpool); 0214 } 0215 0216 /** 0217 * zpool_get_type() - Get the type of the zpool 0218 * @zpool: The zpool to check 0219 * 0220 * This returns the type of the pool. 0221 * 0222 * Implementations must guarantee this to be thread-safe. 0223 * 0224 * Returns: The type of zpool. 0225 */ 0226 const char *zpool_get_type(struct zpool *zpool) 0227 { 0228 return zpool->driver->type; 0229 } 0230 0231 /** 0232 * zpool_malloc_support_movable() - Check if the zpool supports 0233 * allocating movable memory 0234 * @zpool: The zpool to check 0235 * 0236 * This returns if the zpool supports allocating movable memory. 0237 * 0238 * Implementations must guarantee this to be thread-safe. 0239 * 0240 * Returns: true if the zpool supports allocating movable memory, false if not 0241 */ 0242 bool zpool_malloc_support_movable(struct zpool *zpool) 0243 { 0244 return zpool->driver->malloc_support_movable; 0245 } 0246 0247 /** 0248 * zpool_malloc() - Allocate memory 0249 * @zpool: The zpool to allocate from. 0250 * @size: The amount of memory to allocate. 0251 * @gfp: The GFP flags to use when allocating memory. 0252 * @handle: Pointer to the handle to set 0253 * 0254 * This allocates the requested amount of memory from the pool. 0255 * The gfp flags will be used when allocating memory, if the 0256 * implementation supports it. The provided @handle will be 0257 * set to the allocated object handle. 0258 * 0259 * Implementations must guarantee this to be thread-safe. 0260 * 0261 * Returns: 0 on success, negative value on error. 0262 */ 0263 int zpool_malloc(struct zpool *zpool, size_t size, gfp_t gfp, 0264 unsigned long *handle) 0265 { 0266 return zpool->driver->malloc(zpool->pool, size, gfp, handle); 0267 } 0268 0269 /** 0270 * zpool_free() - Free previously allocated memory 0271 * @zpool: The zpool that allocated the memory. 0272 * @handle: The handle to the memory to free. 0273 * 0274 * This frees previously allocated memory. This does not guarantee 0275 * that the pool will actually free memory, only that the memory 0276 * in the pool will become available for use by the pool. 0277 * 0278 * Implementations must guarantee this to be thread-safe, 0279 * however only when freeing different handles. The same 0280 * handle should only be freed once, and should not be used 0281 * after freeing. 0282 */ 0283 void zpool_free(struct zpool *zpool, unsigned long handle) 0284 { 0285 zpool->driver->free(zpool->pool, handle); 0286 } 0287 0288 /** 0289 * zpool_shrink() - Shrink the pool size 0290 * @zpool: The zpool to shrink. 0291 * @pages: The number of pages to shrink the pool. 0292 * @reclaimed: The number of pages successfully evicted. 0293 * 0294 * This attempts to shrink the actual memory size of the pool 0295 * by evicting currently used handle(s). If the pool was 0296 * created with no zpool_ops, or the evict call fails for any 0297 * of the handles, this will fail. If non-NULL, the @reclaimed 0298 * parameter will be set to the number of pages reclaimed, 0299 * which may be more than the number of pages requested. 0300 * 0301 * Implementations must guarantee this to be thread-safe. 0302 * 0303 * Returns: 0 on success, negative value on error/failure. 0304 */ 0305 int zpool_shrink(struct zpool *zpool, unsigned int pages, 0306 unsigned int *reclaimed) 0307 { 0308 return zpool->driver->shrink ? 0309 zpool->driver->shrink(zpool->pool, pages, reclaimed) : -EINVAL; 0310 } 0311 0312 /** 0313 * zpool_map_handle() - Map a previously allocated handle into memory 0314 * @zpool: The zpool that the handle was allocated from 0315 * @handle: The handle to map 0316 * @mapmode: How the memory should be mapped 0317 * 0318 * This maps a previously allocated handle into memory. The @mapmode 0319 * param indicates to the implementation how the memory will be 0320 * used, i.e. read-only, write-only, read-write. If the 0321 * implementation does not support it, the memory will be treated 0322 * as read-write. 0323 * 0324 * This may hold locks, disable interrupts, and/or preemption, 0325 * and the zpool_unmap_handle() must be called to undo those 0326 * actions. The code that uses the mapped handle should complete 0327 * its operations on the mapped handle memory quickly and unmap 0328 * as soon as possible. As the implementation may use per-cpu 0329 * data, multiple handles should not be mapped concurrently on 0330 * any cpu. 0331 * 0332 * Returns: A pointer to the handle's mapped memory area. 0333 */ 0334 void *zpool_map_handle(struct zpool *zpool, unsigned long handle, 0335 enum zpool_mapmode mapmode) 0336 { 0337 return zpool->driver->map(zpool->pool, handle, mapmode); 0338 } 0339 0340 /** 0341 * zpool_unmap_handle() - Unmap a previously mapped handle 0342 * @zpool: The zpool that the handle was allocated from 0343 * @handle: The handle to unmap 0344 * 0345 * This unmaps a previously mapped handle. Any locks or other 0346 * actions that the implementation took in zpool_map_handle() 0347 * will be undone here. The memory area returned from 0348 * zpool_map_handle() should no longer be used after this. 0349 */ 0350 void zpool_unmap_handle(struct zpool *zpool, unsigned long handle) 0351 { 0352 zpool->driver->unmap(zpool->pool, handle); 0353 } 0354 0355 /** 0356 * zpool_get_total_size() - The total size of the pool 0357 * @zpool: The zpool to check 0358 * 0359 * This returns the total size in bytes of the pool. 0360 * 0361 * Returns: Total size of the zpool in bytes. 0362 */ 0363 u64 zpool_get_total_size(struct zpool *zpool) 0364 { 0365 return zpool->driver->total_size(zpool->pool); 0366 } 0367 0368 /** 0369 * zpool_evictable() - Test if zpool is potentially evictable 0370 * @zpool: The zpool to test 0371 * 0372 * Zpool is only potentially evictable when it's created with struct 0373 * zpool_ops.evict and its driver implements struct zpool_driver.shrink. 0374 * 0375 * However, it doesn't necessarily mean driver will use zpool_ops.evict 0376 * in its implementation of zpool_driver.shrink. It could do internal 0377 * defragmentation instead. 0378 * 0379 * Returns: true if potentially evictable; false otherwise. 0380 */ 0381 bool zpool_evictable(struct zpool *zpool) 0382 { 0383 return zpool->evictable; 0384 } 0385 0386 /** 0387 * zpool_can_sleep_mapped - Test if zpool can sleep when do mapped. 0388 * @zpool: The zpool to test 0389 * 0390 * Returns: true if zpool can sleep; false otherwise. 0391 */ 0392 bool zpool_can_sleep_mapped(struct zpool *zpool) 0393 { 0394 return zpool->can_sleep_mapped; 0395 } 0396 0397 MODULE_LICENSE("GPL"); 0398 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>"); 0399 MODULE_DESCRIPTION("Common API for compressed memory storage");
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |