blob: 68facc193496523c6a51394483c9f0b70cc839aa [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Dan Streetmanaf8d4172014-08-06 16:08:36 -07002/*
3 * zpool memory storage api
4 *
5 * Copyright (C) 2014 Dan Streetman
6 *
7 * This is a common frontend for memory storage pool implementations.
8 * Typically, this is used to store compressed memory.
9 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
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>
20
21struct zpool {
Dan Streetmanaf8d4172014-08-06 16:08:36 -070022 struct zpool_driver *driver;
23 void *pool;
Krzysztof Kozlowski78672772015-09-08 15:05:03 -070024 const struct zpool_ops *ops;
Yu Zhao9c3760e2018-01-31 16:19:59 -080025 bool evictable;
Tian Taofc6697a2021-02-25 17:18:17 -080026 bool can_sleep_mapped;
Dan Streetmanaf8d4172014-08-06 16:08:36 -070027};
28
29static LIST_HEAD(drivers_head);
30static DEFINE_SPINLOCK(drivers_lock);
31
Dan Streetmanaf8d4172014-08-06 16:08:36 -070032/**
33 * zpool_register_driver() - register a zpool implementation.
34 * @driver: driver to register
35 */
36void zpool_register_driver(struct zpool_driver *driver)
37{
38 spin_lock(&drivers_lock);
39 atomic_set(&driver->refcount, 0);
40 list_add(&driver->list, &drivers_head);
41 spin_unlock(&drivers_lock);
42}
43EXPORT_SYMBOL(zpool_register_driver);
44
45/**
46 * zpool_unregister_driver() - unregister a zpool implementation.
47 * @driver: driver to unregister.
48 *
49 * Module usage counting is used to prevent using a driver
50 * while/after unloading, so if this is called from module
51 * exit function, this should never fail; if called from
52 * other than the module exit function, and this returns
53 * failure, the driver is in use and must remain available.
54 */
55int zpool_unregister_driver(struct zpool_driver *driver)
56{
57 int ret = 0, refcount;
58
59 spin_lock(&drivers_lock);
60 refcount = atomic_read(&driver->refcount);
61 WARN_ON(refcount < 0);
62 if (refcount > 0)
63 ret = -EBUSY;
64 else
65 list_del(&driver->list);
66 spin_unlock(&drivers_lock);
67
68 return ret;
69}
70EXPORT_SYMBOL(zpool_unregister_driver);
71
Dan Streetman69e18f42015-11-06 16:29:18 -080072/* this assumes @type is null-terminated. */
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -080073static struct zpool_driver *zpool_get_driver(const char *type)
Dan Streetmanaf8d4172014-08-06 16:08:36 -070074{
75 struct zpool_driver *driver;
76
77 spin_lock(&drivers_lock);
78 list_for_each_entry(driver, &drivers_head, list) {
79 if (!strcmp(driver->type, type)) {
80 bool got = try_module_get(driver->owner);
81
82 if (got)
83 atomic_inc(&driver->refcount);
84 spin_unlock(&drivers_lock);
85 return got ? driver : NULL;
86 }
87 }
88
89 spin_unlock(&drivers_lock);
90 return NULL;
91}
92
93static void zpool_put_driver(struct zpool_driver *driver)
94{
95 atomic_dec(&driver->refcount);
96 module_put(driver->owner);
97}
98
99/**
Dan Streetman3f0e1312015-09-09 15:35:16 -0700100 * zpool_has_pool() - Check if the pool driver is available
Mike Rapoportb7701a52018-02-06 15:42:13 -0800101 * @type: The type of the zpool to check (e.g. zbud, zsmalloc)
Dan Streetman3f0e1312015-09-09 15:35:16 -0700102 *
103 * This checks if the @type pool driver is available. This will try to load
104 * the requested module, if needed, but there is no guarantee the module will
105 * still be loaded and available immediately after calling. If this returns
106 * true, the caller should assume the pool is available, but must be prepared
107 * to handle the @zpool_create_pool() returning failure. However if this
108 * returns false, the caller should assume the requested pool type is not
109 * available; either the requested pool type module does not exist, or could
110 * not be loaded, and calling @zpool_create_pool() with the pool type will
111 * fail.
112 *
Dan Streetman69e18f42015-11-06 16:29:18 -0800113 * The @type string must be null-terminated.
114 *
Dan Streetman3f0e1312015-09-09 15:35:16 -0700115 * Returns: true if @type pool is available, false if not
116 */
117bool zpool_has_pool(char *type)
118{
119 struct zpool_driver *driver = zpool_get_driver(type);
120
121 if (!driver) {
122 request_module("zpool-%s", type);
123 driver = zpool_get_driver(type);
124 }
125
126 if (!driver)
127 return false;
128
129 zpool_put_driver(driver);
130 return true;
131}
132EXPORT_SYMBOL(zpool_has_pool);
133
134/**
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700135 * zpool_create_pool() - Create a new zpool
Mike Rapoportb7701a52018-02-06 15:42:13 -0800136 * @type: The type of the zpool to create (e.g. zbud, zsmalloc)
137 * @name: The name of the zpool (e.g. zram0, zswap)
138 * @gfp: The GFP flags to use when allocating the pool.
139 * @ops: The optional ops callback.
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700140 *
141 * This creates a new zpool of the specified type. The gfp flags will be
142 * used when allocating memory, if the implementation supports it. If the
Yu Zhao9c3760e2018-01-31 16:19:59 -0800143 * ops param is NULL, then the created zpool will not be evictable.
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700144 *
145 * Implementations must guarantee this to be thread-safe.
146 *
Dan Streetman69e18f42015-11-06 16:29:18 -0800147 * The @type and @name strings must be null-terminated.
148 *
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700149 * Returns: New zpool on success, NULL on failure.
150 */
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800151struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp,
Krzysztof Kozlowski78672772015-09-08 15:05:03 -0700152 const struct zpool_ops *ops)
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700153{
154 struct zpool_driver *driver;
155 struct zpool *zpool;
156
Dan Streetmancf41f5f2015-06-25 15:00:37 -0700157 pr_debug("creating pool type %s\n", type);
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700158
159 driver = zpool_get_driver(type);
160
161 if (!driver) {
Kees Cook137f8cf2014-08-29 15:18:40 -0700162 request_module("zpool-%s", type);
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700163 driver = zpool_get_driver(type);
164 }
165
166 if (!driver) {
167 pr_err("no driver for type %s\n", type);
168 return NULL;
169 }
170
171 zpool = kmalloc(sizeof(*zpool), gfp);
172 if (!zpool) {
173 pr_err("couldn't create zpool - out of memory\n");
174 zpool_put_driver(driver);
175 return NULL;
176 }
177
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700178 zpool->driver = driver;
Dan Streetman479305f2015-06-25 15:00:40 -0700179 zpool->pool = driver->create(name, gfp, ops, zpool);
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700180 zpool->ops = ops;
Yu Zhao9c3760e2018-01-31 16:19:59 -0800181 zpool->evictable = driver->shrink && ops && ops->evict;
Tian Taofc6697a2021-02-25 17:18:17 -0800182 zpool->can_sleep_mapped = driver->sleep_mapped;
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700183
184 if (!zpool->pool) {
185 pr_err("couldn't create %s pool\n", type);
186 zpool_put_driver(driver);
187 kfree(zpool);
188 return NULL;
189 }
190
Dan Streetmancf41f5f2015-06-25 15:00:37 -0700191 pr_debug("created pool type %s\n", type);
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700192
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700193 return zpool;
194}
195
196/**
197 * zpool_destroy_pool() - Destroy a zpool
Mike Rapoportf144c392018-02-06 15:42:16 -0800198 * @zpool: The zpool to destroy.
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700199 *
200 * Implementations must guarantee this to be thread-safe,
201 * however only when destroying different pools. The same
202 * pool should only be destroyed once, and should not be used
203 * after it is destroyed.
204 *
205 * This destroys an existing zpool. The zpool should not be in use.
206 */
207void zpool_destroy_pool(struct zpool *zpool)
208{
Dan Streetman69e18f42015-11-06 16:29:18 -0800209 pr_debug("destroying pool type %s\n", zpool->driver->type);
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700210
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700211 zpool->driver->destroy(zpool->pool);
212 zpool_put_driver(zpool->driver);
213 kfree(zpool);
214}
215
216/**
217 * zpool_get_type() - Get the type of the zpool
Mike Rapoportf144c392018-02-06 15:42:16 -0800218 * @zpool: The zpool to check
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700219 *
220 * This returns the type of the pool.
221 *
222 * Implementations must guarantee this to be thread-safe.
223 *
224 * Returns: The type of zpool.
225 */
Dan Streetman69e18f42015-11-06 16:29:18 -0800226const char *zpool_get_type(struct zpool *zpool)
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700227{
Dan Streetman69e18f42015-11-06 16:29:18 -0800228 return zpool->driver->type;
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700229}
230
231/**
Randy Dunlapb6aa2c82020-08-11 18:33:28 -0700232 * zpool_malloc_support_movable() - Check if the zpool supports
233 * allocating movable memory
Hui Zhuc165f252019-09-23 15:39:37 -0700234 * @zpool: The zpool to check
235 *
Randy Dunlapb6aa2c82020-08-11 18:33:28 -0700236 * This returns if the zpool supports allocating movable memory.
Hui Zhuc165f252019-09-23 15:39:37 -0700237 *
238 * Implementations must guarantee this to be thread-safe.
239 *
Randy Dunlapb6aa2c82020-08-11 18:33:28 -0700240 * Returns: true if the zpool supports allocating movable memory, false if not
Hui Zhuc165f252019-09-23 15:39:37 -0700241 */
242bool zpool_malloc_support_movable(struct zpool *zpool)
243{
244 return zpool->driver->malloc_support_movable;
245}
246
247/**
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700248 * zpool_malloc() - Allocate memory
Mike Rapoportf144c392018-02-06 15:42:16 -0800249 * @zpool: The zpool to allocate from.
Mike Rapoportb7701a52018-02-06 15:42:13 -0800250 * @size: The amount of memory to allocate.
251 * @gfp: The GFP flags to use when allocating memory.
252 * @handle: Pointer to the handle to set
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700253 *
254 * This allocates the requested amount of memory from the pool.
255 * The gfp flags will be used when allocating memory, if the
256 * implementation supports it. The provided @handle will be
257 * set to the allocated object handle.
258 *
259 * Implementations must guarantee this to be thread-safe.
260 *
261 * Returns: 0 on success, negative value on error.
262 */
263int zpool_malloc(struct zpool *zpool, size_t size, gfp_t gfp,
264 unsigned long *handle)
265{
266 return zpool->driver->malloc(zpool->pool, size, gfp, handle);
267}
268
269/**
270 * zpool_free() - Free previously allocated memory
Mike Rapoportf144c392018-02-06 15:42:16 -0800271 * @zpool: The zpool that allocated the memory.
Mike Rapoportb7701a52018-02-06 15:42:13 -0800272 * @handle: The handle to the memory to free.
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700273 *
274 * This frees previously allocated memory. This does not guarantee
275 * that the pool will actually free memory, only that the memory
276 * in the pool will become available for use by the pool.
277 *
278 * Implementations must guarantee this to be thread-safe,
279 * however only when freeing different handles. The same
280 * handle should only be freed once, and should not be used
281 * after freeing.
282 */
283void zpool_free(struct zpool *zpool, unsigned long handle)
284{
285 zpool->driver->free(zpool->pool, handle);
286}
287
288/**
289 * zpool_shrink() - Shrink the pool size
Mike Rapoportf144c392018-02-06 15:42:16 -0800290 * @zpool: The zpool to shrink.
Mike Rapoportb7701a52018-02-06 15:42:13 -0800291 * @pages: The number of pages to shrink the pool.
292 * @reclaimed: The number of pages successfully evicted.
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700293 *
294 * This attempts to shrink the actual memory size of the pool
295 * by evicting currently used handle(s). If the pool was
296 * created with no zpool_ops, or the evict call fails for any
297 * of the handles, this will fail. If non-NULL, the @reclaimed
298 * parameter will be set to the number of pages reclaimed,
299 * which may be more than the number of pages requested.
300 *
301 * Implementations must guarantee this to be thread-safe.
302 *
303 * Returns: 0 on success, negative value on error/failure.
304 */
305int zpool_shrink(struct zpool *zpool, unsigned int pages,
306 unsigned int *reclaimed)
307{
Yu Zhao9c3760e2018-01-31 16:19:59 -0800308 return zpool->driver->shrink ?
309 zpool->driver->shrink(zpool->pool, pages, reclaimed) : -EINVAL;
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700310}
311
312/**
313 * zpool_map_handle() - Map a previously allocated handle into memory
Mike Rapoportf144c392018-02-06 15:42:16 -0800314 * @zpool: The zpool that the handle was allocated from
Mike Rapoportb7701a52018-02-06 15:42:13 -0800315 * @handle: The handle to map
Mike Rapoportf144c392018-02-06 15:42:16 -0800316 * @mapmode: How the memory should be mapped
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700317 *
Mike Rapoportf144c392018-02-06 15:42:16 -0800318 * This maps a previously allocated handle into memory. The @mapmode
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700319 * param indicates to the implementation how the memory will be
320 * used, i.e. read-only, write-only, read-write. If the
321 * implementation does not support it, the memory will be treated
322 * as read-write.
323 *
324 * This may hold locks, disable interrupts, and/or preemption,
325 * and the zpool_unmap_handle() must be called to undo those
326 * actions. The code that uses the mapped handle should complete
Ingo Molnarf0953a12021-05-06 18:06:47 -0700327 * its operations on the mapped handle memory quickly and unmap
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700328 * as soon as possible. As the implementation may use per-cpu
329 * data, multiple handles should not be mapped concurrently on
330 * any cpu.
331 *
332 * Returns: A pointer to the handle's mapped memory area.
333 */
334void *zpool_map_handle(struct zpool *zpool, unsigned long handle,
335 enum zpool_mapmode mapmode)
336{
337 return zpool->driver->map(zpool->pool, handle, mapmode);
338}
339
340/**
341 * zpool_unmap_handle() - Unmap a previously mapped handle
Mike Rapoportf144c392018-02-06 15:42:16 -0800342 * @zpool: The zpool that the handle was allocated from
Mike Rapoportb7701a52018-02-06 15:42:13 -0800343 * @handle: The handle to unmap
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700344 *
345 * This unmaps a previously mapped handle. Any locks or other
346 * actions that the implementation took in zpool_map_handle()
347 * will be undone here. The memory area returned from
348 * zpool_map_handle() should no longer be used after this.
349 */
350void zpool_unmap_handle(struct zpool *zpool, unsigned long handle)
351{
352 zpool->driver->unmap(zpool->pool, handle);
353}
354
355/**
356 * zpool_get_total_size() - The total size of the pool
Mike Rapoportf144c392018-02-06 15:42:16 -0800357 * @zpool: The zpool to check
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700358 *
359 * This returns the total size in bytes of the pool.
360 *
361 * Returns: Total size of the zpool in bytes.
362 */
363u64 zpool_get_total_size(struct zpool *zpool)
364{
365 return zpool->driver->total_size(zpool->pool);
366}
367
Yu Zhao9c3760e2018-01-31 16:19:59 -0800368/**
369 * zpool_evictable() - Test if zpool is potentially evictable
Mike Rapoport14fec9e2018-02-21 14:45:46 -0800370 * @zpool: The zpool to test
Yu Zhao9c3760e2018-01-31 16:19:59 -0800371 *
372 * Zpool is only potentially evictable when it's created with struct
373 * zpool_ops.evict and its driver implements struct zpool_driver.shrink.
374 *
375 * However, it doesn't necessarily mean driver will use zpool_ops.evict
376 * in its implementation of zpool_driver.shrink. It could do internal
377 * defragmentation instead.
378 *
379 * Returns: true if potentially evictable; false otherwise.
380 */
381bool zpool_evictable(struct zpool *zpool)
382{
383 return zpool->evictable;
384}
385
Tian Taofc6697a2021-02-25 17:18:17 -0800386/**
387 * zpool_can_sleep_mapped - Test if zpool can sleep when do mapped.
388 * @zpool: The zpool to test
389 *
390 * Returns: true if zpool can sleep; false otherwise.
391 */
392bool zpool_can_sleep_mapped(struct zpool *zpool)
393{
394 return zpool->can_sleep_mapped;
395}
396
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700397MODULE_LICENSE("GPL");
398MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
399MODULE_DESCRIPTION("Common API for compressed memory storage");