]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - mm/zpool.c
mm: swap: zswap: maybe_preload & refactoring
[mirror_ubuntu-bionic-kernel.git] / mm / zpool.c
CommitLineData
af8d417a
DS
1/*
2 * zpool memory storage api
3 *
4 * Copyright (C) 2014 Dan Streetman
5 *
6 * This is a common frontend for memory storage pool implementations.
7 * Typically, this is used to store compressed memory.
8 */
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/list.h>
13#include <linux/types.h>
14#include <linux/mm.h>
15#include <linux/slab.h>
16#include <linux/spinlock.h>
17#include <linux/module.h>
18#include <linux/zpool.h>
19
20struct zpool {
21 char *type;
22
23 struct zpool_driver *driver;
24 void *pool;
25 struct zpool_ops *ops;
26
27 struct list_head list;
28};
29
30static LIST_HEAD(drivers_head);
31static DEFINE_SPINLOCK(drivers_lock);
32
33static LIST_HEAD(pools_head);
34static DEFINE_SPINLOCK(pools_lock);
35
36/**
37 * zpool_register_driver() - register a zpool implementation.
38 * @driver: driver to register
39 */
40void zpool_register_driver(struct zpool_driver *driver)
41{
42 spin_lock(&drivers_lock);
43 atomic_set(&driver->refcount, 0);
44 list_add(&driver->list, &drivers_head);
45 spin_unlock(&drivers_lock);
46}
47EXPORT_SYMBOL(zpool_register_driver);
48
49/**
50 * zpool_unregister_driver() - unregister a zpool implementation.
51 * @driver: driver to unregister.
52 *
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.
58 */
59int zpool_unregister_driver(struct zpool_driver *driver)
60{
61 int ret = 0, refcount;
62
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);
71
72 return ret;
73}
74EXPORT_SYMBOL(zpool_unregister_driver);
75
af8d417a
DS
76static struct zpool_driver *zpool_get_driver(char *type)
77{
78 struct zpool_driver *driver;
79
80 spin_lock(&drivers_lock);
81 list_for_each_entry(driver, &drivers_head, list) {
82 if (!strcmp(driver->type, type)) {
83 bool got = try_module_get(driver->owner);
84
85 if (got)
86 atomic_inc(&driver->refcount);
87 spin_unlock(&drivers_lock);
88 return got ? driver : NULL;
89 }
90 }
91
92 spin_unlock(&drivers_lock);
93 return NULL;
94}
95
96static void zpool_put_driver(struct zpool_driver *driver)
97{
98 atomic_dec(&driver->refcount);
99 module_put(driver->owner);
100}
101
102/**
103 * zpool_create_pool() - Create a new zpool
104 * @type The type of the zpool to create (e.g. zbud, zsmalloc)
3eba0c6a 105 * @name The name of the zpool (e.g. zram0, zswap)
af8d417a
DS
106 * @gfp The GFP flags to use when allocating the pool.
107 * @ops The optional ops callback.
108 *
109 * This creates a new zpool of the specified type. The gfp flags will be
110 * used when allocating memory, if the implementation supports it. If the
111 * ops param is NULL, then the created zpool will not be shrinkable.
112 *
113 * Implementations must guarantee this to be thread-safe.
114 *
115 * Returns: New zpool on success, NULL on failure.
116 */
3eba0c6a
GM
117struct zpool *zpool_create_pool(char *type, char *name, gfp_t gfp,
118 struct zpool_ops *ops)
af8d417a
DS
119{
120 struct zpool_driver *driver;
121 struct zpool *zpool;
122
cf41f5f4 123 pr_debug("creating pool type %s\n", type);
af8d417a
DS
124
125 driver = zpool_get_driver(type);
126
127 if (!driver) {
137f8cff 128 request_module("zpool-%s", type);
af8d417a
DS
129 driver = zpool_get_driver(type);
130 }
131
132 if (!driver) {
133 pr_err("no driver for type %s\n", type);
134 return NULL;
135 }
136
137 zpool = kmalloc(sizeof(*zpool), gfp);
138 if (!zpool) {
139 pr_err("couldn't create zpool - out of memory\n");
140 zpool_put_driver(driver);
141 return NULL;
142 }
143
144 zpool->type = driver->type;
145 zpool->driver = driver;
479305fd 146 zpool->pool = driver->create(name, gfp, ops, zpool);
af8d417a
DS
147 zpool->ops = ops;
148
149 if (!zpool->pool) {
150 pr_err("couldn't create %s pool\n", type);
151 zpool_put_driver(driver);
152 kfree(zpool);
153 return NULL;
154 }
155
cf41f5f4 156 pr_debug("created pool type %s\n", type);
af8d417a
DS
157
158 spin_lock(&pools_lock);
159 list_add(&zpool->list, &pools_head);
160 spin_unlock(&pools_lock);
161
162 return zpool;
163}
164
165/**
166 * zpool_destroy_pool() - Destroy a zpool
167 * @pool The zpool to destroy.
168 *
169 * Implementations must guarantee this to be thread-safe,
170 * however only when destroying different pools. The same
171 * pool should only be destroyed once, and should not be used
172 * after it is destroyed.
173 *
174 * This destroys an existing zpool. The zpool should not be in use.
175 */
176void zpool_destroy_pool(struct zpool *zpool)
177{
cf41f5f4 178 pr_debug("destroying pool type %s\n", zpool->type);
af8d417a
DS
179
180 spin_lock(&pools_lock);
181 list_del(&zpool->list);
182 spin_unlock(&pools_lock);
183 zpool->driver->destroy(zpool->pool);
184 zpool_put_driver(zpool->driver);
185 kfree(zpool);
186}
187
188/**
189 * zpool_get_type() - Get the type of the zpool
190 * @pool The zpool to check
191 *
192 * This returns the type of the pool.
193 *
194 * Implementations must guarantee this to be thread-safe.
195 *
196 * Returns: The type of zpool.
197 */
198char *zpool_get_type(struct zpool *zpool)
199{
200 return zpool->type;
201}
202
203/**
204 * zpool_malloc() - Allocate memory
205 * @pool The zpool to allocate from.
206 * @size The amount of memory to allocate.
207 * @gfp The GFP flags to use when allocating memory.
208 * @handle Pointer to the handle to set
209 *
210 * This allocates the requested amount of memory from the pool.
211 * The gfp flags will be used when allocating memory, if the
212 * implementation supports it. The provided @handle will be
213 * set to the allocated object handle.
214 *
215 * Implementations must guarantee this to be thread-safe.
216 *
217 * Returns: 0 on success, negative value on error.
218 */
219int zpool_malloc(struct zpool *zpool, size_t size, gfp_t gfp,
220 unsigned long *handle)
221{
222 return zpool->driver->malloc(zpool->pool, size, gfp, handle);
223}
224
225/**
226 * zpool_free() - Free previously allocated memory
227 * @pool The zpool that allocated the memory.
228 * @handle The handle to the memory to free.
229 *
230 * This frees previously allocated memory. This does not guarantee
231 * that the pool will actually free memory, only that the memory
232 * in the pool will become available for use by the pool.
233 *
234 * Implementations must guarantee this to be thread-safe,
235 * however only when freeing different handles. The same
236 * handle should only be freed once, and should not be used
237 * after freeing.
238 */
239void zpool_free(struct zpool *zpool, unsigned long handle)
240{
241 zpool->driver->free(zpool->pool, handle);
242}
243
244/**
245 * zpool_shrink() - Shrink the pool size
246 * @pool The zpool to shrink.
247 * @pages The number of pages to shrink the pool.
248 * @reclaimed The number of pages successfully evicted.
249 *
250 * This attempts to shrink the actual memory size of the pool
251 * by evicting currently used handle(s). If the pool was
252 * created with no zpool_ops, or the evict call fails for any
253 * of the handles, this will fail. If non-NULL, the @reclaimed
254 * parameter will be set to the number of pages reclaimed,
255 * which may be more than the number of pages requested.
256 *
257 * Implementations must guarantee this to be thread-safe.
258 *
259 * Returns: 0 on success, negative value on error/failure.
260 */
261int zpool_shrink(struct zpool *zpool, unsigned int pages,
262 unsigned int *reclaimed)
263{
264 return zpool->driver->shrink(zpool->pool, pages, reclaimed);
265}
266
267/**
268 * zpool_map_handle() - Map a previously allocated handle into memory
269 * @pool The zpool that the handle was allocated from
270 * @handle The handle to map
271 * @mm How the memory should be mapped
272 *
273 * This maps a previously allocated handle into memory. The @mm
274 * param indicates to the implementation how the memory will be
275 * used, i.e. read-only, write-only, read-write. If the
276 * implementation does not support it, the memory will be treated
277 * as read-write.
278 *
279 * This may hold locks, disable interrupts, and/or preemption,
280 * and the zpool_unmap_handle() must be called to undo those
281 * actions. The code that uses the mapped handle should complete
282 * its operatons on the mapped handle memory quickly and unmap
283 * as soon as possible. As the implementation may use per-cpu
284 * data, multiple handles should not be mapped concurrently on
285 * any cpu.
286 *
287 * Returns: A pointer to the handle's mapped memory area.
288 */
289void *zpool_map_handle(struct zpool *zpool, unsigned long handle,
290 enum zpool_mapmode mapmode)
291{
292 return zpool->driver->map(zpool->pool, handle, mapmode);
293}
294
295/**
296 * zpool_unmap_handle() - Unmap a previously mapped handle
297 * @pool The zpool that the handle was allocated from
298 * @handle The handle to unmap
299 *
300 * This unmaps a previously mapped handle. Any locks or other
301 * actions that the implementation took in zpool_map_handle()
302 * will be undone here. The memory area returned from
303 * zpool_map_handle() should no longer be used after this.
304 */
305void zpool_unmap_handle(struct zpool *zpool, unsigned long handle)
306{
307 zpool->driver->unmap(zpool->pool, handle);
308}
309
310/**
311 * zpool_get_total_size() - The total size of the pool
312 * @pool The zpool to check
313 *
314 * This returns the total size in bytes of the pool.
315 *
316 * Returns: Total size of the zpool in bytes.
317 */
318u64 zpool_get_total_size(struct zpool *zpool)
319{
320 return zpool->driver->total_size(zpool->pool);
321}
322
323static int __init init_zpool(void)
324{
325 pr_info("loaded\n");
326 return 0;
327}
328
329static void __exit exit_zpool(void)
330{
331 pr_info("unloaded\n");
332}
333
334module_init(init_zpool);
335module_exit(exit_zpool);
336
337MODULE_LICENSE("GPL");
338MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
339MODULE_DESCRIPTION("Common API for compressed memory storage");