1 // SPDX-License-Identifier: GPL-2.0-only
3 * zpool memory storage api
5 * Copyright (C) 2014 Dan Streetman
7 * This is a common frontend for memory storage pool implementations.
8 * Typically, this is used to store compressed memory.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/list.h>
14 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/module.h>
19 #include <linux/zpool.h>
22 struct zpool_driver *driver;
26 static LIST_HEAD(drivers_head);
27 static DEFINE_SPINLOCK(drivers_lock);
30 * zpool_register_driver() - register a zpool implementation.
31 * @driver: driver to register
33 void zpool_register_driver(struct zpool_driver *driver)
35 spin_lock(&drivers_lock);
36 atomic_set(&driver->refcount, 0);
37 list_add(&driver->list, &drivers_head);
38 spin_unlock(&drivers_lock);
40 EXPORT_SYMBOL(zpool_register_driver);
43 * zpool_unregister_driver() - unregister a zpool implementation.
44 * @driver: driver to unregister.
46 * Module usage counting is used to prevent using a driver
47 * while/after unloading, so if this is called from module
48 * exit function, this should never fail; if called from
49 * other than the module exit function, and this returns
50 * failure, the driver is in use and must remain available.
52 int zpool_unregister_driver(struct zpool_driver *driver)
54 int ret = 0, refcount;
56 spin_lock(&drivers_lock);
57 refcount = atomic_read(&driver->refcount);
58 WARN_ON(refcount < 0);
62 list_del(&driver->list);
63 spin_unlock(&drivers_lock);
67 EXPORT_SYMBOL(zpool_unregister_driver);
69 /* this assumes @type is null-terminated. */
70 static struct zpool_driver *zpool_get_driver(const char *type)
72 struct zpool_driver *driver;
74 spin_lock(&drivers_lock);
75 list_for_each_entry(driver, &drivers_head, list) {
76 if (!strcmp(driver->type, type)) {
77 bool got = try_module_get(driver->owner);
80 atomic_inc(&driver->refcount);
81 spin_unlock(&drivers_lock);
82 return got ? driver : NULL;
86 spin_unlock(&drivers_lock);
90 static void zpool_put_driver(struct zpool_driver *driver)
92 atomic_dec(&driver->refcount);
93 module_put(driver->owner);
97 * zpool_has_pool() - Check if the pool driver is available
98 * @type: The type of the zpool to check (e.g. zbud, zsmalloc)
100 * This checks if the @type pool driver is available. This will try to load
101 * the requested module, if needed, but there is no guarantee the module will
102 * still be loaded and available immediately after calling. If this returns
103 * true, the caller should assume the pool is available, but must be prepared
104 * to handle the @zpool_create_pool() returning failure. However if this
105 * returns false, the caller should assume the requested pool type is not
106 * available; either the requested pool type module does not exist, or could
107 * not be loaded, and calling @zpool_create_pool() with the pool type will
110 * The @type string must be null-terminated.
112 * Returns: true if @type pool is available, false if not
114 bool zpool_has_pool(char *type)
116 struct zpool_driver *driver = zpool_get_driver(type);
119 request_module("zpool-%s", type);
120 driver = zpool_get_driver(type);
126 zpool_put_driver(driver);
129 EXPORT_SYMBOL(zpool_has_pool);
132 * zpool_create_pool() - Create a new zpool
133 * @type: The type of the zpool to create (e.g. zbud, zsmalloc)
134 * @name: The name of the zpool (e.g. zram0, zswap)
135 * @gfp: The GFP flags to use when allocating the pool.
137 * This creates a new zpool of the specified type. The gfp flags will be
138 * used when allocating memory, if the implementation supports it. If the
139 * ops param is NULL, then the created zpool will not be evictable.
141 * Implementations must guarantee this to be thread-safe.
143 * The @type and @name strings must be null-terminated.
145 * Returns: New zpool on success, NULL on failure.
147 struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp)
149 struct zpool_driver *driver;
152 pr_debug("creating pool type %s\n", type);
154 driver = zpool_get_driver(type);
157 request_module("zpool-%s", type);
158 driver = zpool_get_driver(type);
162 pr_err("no driver for type %s\n", type);
166 zpool = kmalloc(sizeof(*zpool), gfp);
168 pr_err("couldn't create zpool - out of memory\n");
169 zpool_put_driver(driver);
173 zpool->driver = driver;
174 zpool->pool = driver->create(name, gfp);
177 pr_err("couldn't create %s pool\n", type);
178 zpool_put_driver(driver);
183 pr_debug("created pool type %s\n", type);
189 * zpool_destroy_pool() - Destroy a zpool
190 * @zpool: The zpool to destroy.
192 * Implementations must guarantee this to be thread-safe,
193 * however only when destroying different pools. The same
194 * pool should only be destroyed once, and should not be used
195 * after it is destroyed.
197 * This destroys an existing zpool. The zpool should not be in use.
199 void zpool_destroy_pool(struct zpool *zpool)
201 pr_debug("destroying pool type %s\n", zpool->driver->type);
203 zpool->driver->destroy(zpool->pool);
204 zpool_put_driver(zpool->driver);
209 * zpool_get_type() - Get the type of the zpool
210 * @zpool: The zpool to check
212 * This returns the type of the pool.
214 * Implementations must guarantee this to be thread-safe.
216 * Returns: The type of zpool.
218 const char *zpool_get_type(struct zpool *zpool)
220 return zpool->driver->type;
224 * zpool_malloc_support_movable() - Check if the zpool supports
225 * allocating movable memory
226 * @zpool: The zpool to check
228 * This returns if the zpool supports allocating movable memory.
230 * Implementations must guarantee this to be thread-safe.
232 * Returns: true if the zpool supports allocating movable memory, false if not
234 bool zpool_malloc_support_movable(struct zpool *zpool)
236 return zpool->driver->malloc_support_movable;
240 * zpool_malloc() - Allocate memory
241 * @zpool: The zpool to allocate from.
242 * @size: The amount of memory to allocate.
243 * @gfp: The GFP flags to use when allocating memory.
244 * @handle: Pointer to the handle to set
246 * This allocates the requested amount of memory from the pool.
247 * The gfp flags will be used when allocating memory, if the
248 * implementation supports it. The provided @handle will be
249 * set to the allocated object handle.
251 * Implementations must guarantee this to be thread-safe.
253 * Returns: 0 on success, negative value on error.
255 int zpool_malloc(struct zpool *zpool, size_t size, gfp_t gfp,
256 unsigned long *handle)
258 return zpool->driver->malloc(zpool->pool, size, gfp, handle);
262 * zpool_free() - Free previously allocated memory
263 * @zpool: The zpool that allocated the memory.
264 * @handle: The handle to the memory to free.
266 * This frees previously allocated memory. This does not guarantee
267 * that the pool will actually free memory, only that the memory
268 * in the pool will become available for use by the pool.
270 * Implementations must guarantee this to be thread-safe,
271 * however only when freeing different handles. The same
272 * handle should only be freed once, and should not be used
275 void zpool_free(struct zpool *zpool, unsigned long handle)
277 zpool->driver->free(zpool->pool, handle);
281 * zpool_map_handle() - Map a previously allocated handle into memory
282 * @zpool: The zpool that the handle was allocated from
283 * @handle: The handle to map
284 * @mapmode: How the memory should be mapped
286 * This maps a previously allocated handle into memory. The @mapmode
287 * param indicates to the implementation how the memory will be
288 * used, i.e. read-only, write-only, read-write. If the
289 * implementation does not support it, the memory will be treated
292 * This may hold locks, disable interrupts, and/or preemption,
293 * and the zpool_unmap_handle() must be called to undo those
294 * actions. The code that uses the mapped handle should complete
295 * its operations on the mapped handle memory quickly and unmap
296 * as soon as possible. As the implementation may use per-cpu
297 * data, multiple handles should not be mapped concurrently on
300 * Returns: A pointer to the handle's mapped memory area.
302 void *zpool_map_handle(struct zpool *zpool, unsigned long handle,
303 enum zpool_mapmode mapmode)
305 return zpool->driver->map(zpool->pool, handle, mapmode);
309 * zpool_unmap_handle() - Unmap a previously mapped handle
310 * @zpool: The zpool that the handle was allocated from
311 * @handle: The handle to unmap
313 * This unmaps a previously mapped handle. Any locks or other
314 * actions that the implementation took in zpool_map_handle()
315 * will be undone here. The memory area returned from
316 * zpool_map_handle() should no longer be used after this.
318 void zpool_unmap_handle(struct zpool *zpool, unsigned long handle)
320 zpool->driver->unmap(zpool->pool, handle);
324 * zpool_get_total_size() - The total size of the pool
325 * @zpool: The zpool to check
327 * This returns the total size in bytes of the pool.
329 * Returns: Total size of the zpool in bytes.
331 u64 zpool_get_total_size(struct zpool *zpool)
333 return zpool->driver->total_size(zpool->pool);
337 * zpool_can_sleep_mapped - Test if zpool can sleep when do mapped.
338 * @zpool: The zpool to test
340 * Some allocators enter non-preemptible context in ->map() callback (e.g.
341 * disable pagefaults) and exit that context in ->unmap(), which limits what
342 * we can do with the mapped object. For instance, we cannot wait for
343 * asynchronous crypto API to decompress such an object or take mutexes
344 * since those will call into the scheduler. This function tells us whether
345 * we use such an allocator.
347 * Returns: true if zpool can sleep; false otherwise.
349 bool zpool_can_sleep_mapped(struct zpool *zpool)
351 return zpool->driver->sleep_mapped;
354 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
355 MODULE_DESCRIPTION("Common API for compressed memory storage");