Back to home page

LXR

 
 

    


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