MIPS: config: Remove left-over BACKLIGHT_LCD_SUPPORT
[linux/fpc-iii.git] / mm / zpool.c
bloba2dd9107857d44bc2573bc6976df5f5afa0306ca
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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.
9 */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/list.h>
14 #include <linux/types.h>
15 #include <linux/mm.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/module.h>
19 #include <linux/zpool.h>
21 struct zpool {
22 struct zpool_driver *driver;
23 void *pool;
24 const struct zpool_ops *ops;
25 bool evictable;
27 struct list_head list;
30 static LIST_HEAD(drivers_head);
31 static DEFINE_SPINLOCK(drivers_lock);
33 static LIST_HEAD(pools_head);
34 static DEFINE_SPINLOCK(pools_lock);
36 /**
37 * zpool_register_driver() - register a zpool implementation.
38 * @driver: driver to register
40 void zpool_register_driver(struct zpool_driver *driver)
42 spin_lock(&drivers_lock);
43 atomic_set(&driver->refcount, 0);
44 list_add(&driver->list, &drivers_head);
45 spin_unlock(&drivers_lock);
47 EXPORT_SYMBOL(zpool_register_driver);
49 /**
50 * zpool_unregister_driver() - unregister a zpool implementation.
51 * @driver: driver to unregister.
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.
59 int zpool_unregister_driver(struct zpool_driver *driver)
61 int ret = 0, refcount;
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);
72 return ret;
74 EXPORT_SYMBOL(zpool_unregister_driver);
76 /* this assumes @type is null-terminated. */
77 static struct zpool_driver *zpool_get_driver(const char *type)
79 struct zpool_driver *driver;
81 spin_lock(&drivers_lock);
82 list_for_each_entry(driver, &drivers_head, list) {
83 if (!strcmp(driver->type, type)) {
84 bool got = try_module_get(driver->owner);
86 if (got)
87 atomic_inc(&driver->refcount);
88 spin_unlock(&drivers_lock);
89 return got ? driver : NULL;
93 spin_unlock(&drivers_lock);
94 return NULL;
97 static void zpool_put_driver(struct zpool_driver *driver)
99 atomic_dec(&driver->refcount);
100 module_put(driver->owner);
104 * zpool_has_pool() - Check if the pool driver is available
105 * @type: The type of the zpool to check (e.g. zbud, zsmalloc)
107 * This checks if the @type pool driver is available. This will try to load
108 * the requested module, if needed, but there is no guarantee the module will
109 * still be loaded and available immediately after calling. If this returns
110 * true, the caller should assume the pool is available, but must be prepared
111 * to handle the @zpool_create_pool() returning failure. However if this
112 * returns false, the caller should assume the requested pool type is not
113 * available; either the requested pool type module does not exist, or could
114 * not be loaded, and calling @zpool_create_pool() with the pool type will
115 * fail.
117 * The @type string must be null-terminated.
119 * Returns: true if @type pool is available, false if not
121 bool zpool_has_pool(char *type)
123 struct zpool_driver *driver = zpool_get_driver(type);
125 if (!driver) {
126 request_module("zpool-%s", type);
127 driver = zpool_get_driver(type);
130 if (!driver)
131 return false;
133 zpool_put_driver(driver);
134 return true;
136 EXPORT_SYMBOL(zpool_has_pool);
139 * zpool_create_pool() - Create a new zpool
140 * @type: The type of the zpool to create (e.g. zbud, zsmalloc)
141 * @name: The name of the zpool (e.g. zram0, zswap)
142 * @gfp: The GFP flags to use when allocating the pool.
143 * @ops: The optional ops callback.
145 * This creates a new zpool of the specified type. The gfp flags will be
146 * used when allocating memory, if the implementation supports it. If the
147 * ops param is NULL, then the created zpool will not be evictable.
149 * Implementations must guarantee this to be thread-safe.
151 * The @type and @name strings must be null-terminated.
153 * Returns: New zpool on success, NULL on failure.
155 struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp,
156 const struct zpool_ops *ops)
158 struct zpool_driver *driver;
159 struct zpool *zpool;
161 pr_debug("creating pool type %s\n", type);
163 driver = zpool_get_driver(type);
165 if (!driver) {
166 request_module("zpool-%s", type);
167 driver = zpool_get_driver(type);
170 if (!driver) {
171 pr_err("no driver for type %s\n", type);
172 return NULL;
175 zpool = kmalloc(sizeof(*zpool), gfp);
176 if (!zpool) {
177 pr_err("couldn't create zpool - out of memory\n");
178 zpool_put_driver(driver);
179 return NULL;
182 zpool->driver = driver;
183 zpool->pool = driver->create(name, gfp, ops, zpool);
184 zpool->ops = ops;
185 zpool->evictable = driver->shrink && ops && ops->evict;
187 if (!zpool->pool) {
188 pr_err("couldn't create %s pool\n", type);
189 zpool_put_driver(driver);
190 kfree(zpool);
191 return NULL;
194 pr_debug("created pool type %s\n", type);
196 spin_lock(&pools_lock);
197 list_add(&zpool->list, &pools_head);
198 spin_unlock(&pools_lock);
200 return zpool;
204 * zpool_destroy_pool() - Destroy a zpool
205 * @zpool: The zpool to destroy.
207 * Implementations must guarantee this to be thread-safe,
208 * however only when destroying different pools. The same
209 * pool should only be destroyed once, and should not be used
210 * after it is destroyed.
212 * This destroys an existing zpool. The zpool should not be in use.
214 void zpool_destroy_pool(struct zpool *zpool)
216 pr_debug("destroying pool type %s\n", zpool->driver->type);
218 spin_lock(&pools_lock);
219 list_del(&zpool->list);
220 spin_unlock(&pools_lock);
221 zpool->driver->destroy(zpool->pool);
222 zpool_put_driver(zpool->driver);
223 kfree(zpool);
227 * zpool_get_type() - Get the type of the zpool
228 * @zpool: The zpool to check
230 * This returns the type of the pool.
232 * Implementations must guarantee this to be thread-safe.
234 * Returns: The type of zpool.
236 const char *zpool_get_type(struct zpool *zpool)
238 return zpool->driver->type;
242 * zpool_malloc() - Allocate memory
243 * @zpool: The zpool to allocate from.
244 * @size: The amount of memory to allocate.
245 * @gfp: The GFP flags to use when allocating memory.
246 * @handle: Pointer to the handle to set
248 * This allocates the requested amount of memory from the pool.
249 * The gfp flags will be used when allocating memory, if the
250 * implementation supports it. The provided @handle will be
251 * set to the allocated object handle.
253 * Implementations must guarantee this to be thread-safe.
255 * Returns: 0 on success, negative value on error.
257 int zpool_malloc(struct zpool *zpool, size_t size, gfp_t gfp,
258 unsigned long *handle)
260 return zpool->driver->malloc(zpool->pool, size, gfp, handle);
264 * zpool_free() - Free previously allocated memory
265 * @zpool: The zpool that allocated the memory.
266 * @handle: The handle to the memory to free.
268 * This frees previously allocated memory. This does not guarantee
269 * that the pool will actually free memory, only that the memory
270 * in the pool will become available for use by the pool.
272 * Implementations must guarantee this to be thread-safe,
273 * however only when freeing different handles. The same
274 * handle should only be freed once, and should not be used
275 * after freeing.
277 void zpool_free(struct zpool *zpool, unsigned long handle)
279 zpool->driver->free(zpool->pool, handle);
283 * zpool_shrink() - Shrink the pool size
284 * @zpool: The zpool to shrink.
285 * @pages: The number of pages to shrink the pool.
286 * @reclaimed: The number of pages successfully evicted.
288 * This attempts to shrink the actual memory size of the pool
289 * by evicting currently used handle(s). If the pool was
290 * created with no zpool_ops, or the evict call fails for any
291 * of the handles, this will fail. If non-NULL, the @reclaimed
292 * parameter will be set to the number of pages reclaimed,
293 * which may be more than the number of pages requested.
295 * Implementations must guarantee this to be thread-safe.
297 * Returns: 0 on success, negative value on error/failure.
299 int zpool_shrink(struct zpool *zpool, unsigned int pages,
300 unsigned int *reclaimed)
302 return zpool->driver->shrink ?
303 zpool->driver->shrink(zpool->pool, pages, reclaimed) : -EINVAL;
307 * zpool_map_handle() - Map a previously allocated handle into memory
308 * @zpool: The zpool that the handle was allocated from
309 * @handle: The handle to map
310 * @mapmode: How the memory should be mapped
312 * This maps a previously allocated handle into memory. The @mapmode
313 * param indicates to the implementation how the memory will be
314 * used, i.e. read-only, write-only, read-write. If the
315 * implementation does not support it, the memory will be treated
316 * as read-write.
318 * This may hold locks, disable interrupts, and/or preemption,
319 * and the zpool_unmap_handle() must be called to undo those
320 * actions. The code that uses the mapped handle should complete
321 * its operatons on the mapped handle memory quickly and unmap
322 * as soon as possible. As the implementation may use per-cpu
323 * data, multiple handles should not be mapped concurrently on
324 * any cpu.
326 * Returns: A pointer to the handle's mapped memory area.
328 void *zpool_map_handle(struct zpool *zpool, unsigned long handle,
329 enum zpool_mapmode mapmode)
331 return zpool->driver->map(zpool->pool, handle, mapmode);
335 * zpool_unmap_handle() - Unmap a previously mapped handle
336 * @zpool: The zpool that the handle was allocated from
337 * @handle: The handle to unmap
339 * This unmaps a previously mapped handle. Any locks or other
340 * actions that the implementation took in zpool_map_handle()
341 * will be undone here. The memory area returned from
342 * zpool_map_handle() should no longer be used after this.
344 void zpool_unmap_handle(struct zpool *zpool, unsigned long handle)
346 zpool->driver->unmap(zpool->pool, handle);
350 * zpool_get_total_size() - The total size of the pool
351 * @zpool: The zpool to check
353 * This returns the total size in bytes of the pool.
355 * Returns: Total size of the zpool in bytes.
357 u64 zpool_get_total_size(struct zpool *zpool)
359 return zpool->driver->total_size(zpool->pool);
363 * zpool_evictable() - Test if zpool is potentially evictable
364 * @zpool: The zpool to test
366 * Zpool is only potentially evictable when it's created with struct
367 * zpool_ops.evict and its driver implements struct zpool_driver.shrink.
369 * However, it doesn't necessarily mean driver will use zpool_ops.evict
370 * in its implementation of zpool_driver.shrink. It could do internal
371 * defragmentation instead.
373 * Returns: true if potentially evictable; false otherwise.
375 bool zpool_evictable(struct zpool *zpool)
377 return zpool->evictable;
380 MODULE_LICENSE("GPL");
381 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
382 MODULE_DESCRIPTION("Common API for compressed memory storage");