xref: /linux/mm/zpool.c (revision 00c010e130e58301db2ea0cec1eadc931e1cb8cf)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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 
21 struct zpool {
22 	struct zpool_driver *driver;
23 	void *pool;
24 };
25 
26 static LIST_HEAD(drivers_head);
27 static DEFINE_SPINLOCK(drivers_lock);
28 
29 /**
30  * zpool_register_driver() - register a zpool implementation.
31  * @driver:	driver to register
32  */
zpool_register_driver(struct zpool_driver * driver)33 void zpool_register_driver(struct zpool_driver *driver)
34 {
35 	spin_lock(&drivers_lock);
36 	atomic_set(&driver->refcount, 0);
37 	list_add(&driver->list, &drivers_head);
38 	spin_unlock(&drivers_lock);
39 }
40 EXPORT_SYMBOL(zpool_register_driver);
41 
42 /**
43  * zpool_unregister_driver() - unregister a zpool implementation.
44  * @driver:	driver to unregister.
45  *
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.
51  */
zpool_unregister_driver(struct zpool_driver * driver)52 int zpool_unregister_driver(struct zpool_driver *driver)
53 {
54 	int ret = 0, refcount;
55 
56 	spin_lock(&drivers_lock);
57 	refcount = atomic_read(&driver->refcount);
58 	WARN_ON(refcount < 0);
59 	if (refcount > 0)
60 		ret = -EBUSY;
61 	else
62 		list_del(&driver->list);
63 	spin_unlock(&drivers_lock);
64 
65 	return ret;
66 }
67 EXPORT_SYMBOL(zpool_unregister_driver);
68 
69 /* this assumes @type is null-terminated. */
zpool_get_driver(const char * type)70 static struct zpool_driver *zpool_get_driver(const char *type)
71 {
72 	struct zpool_driver *driver;
73 
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);
78 
79 			if (got)
80 				atomic_inc(&driver->refcount);
81 			spin_unlock(&drivers_lock);
82 			return got ? driver : NULL;
83 		}
84 	}
85 
86 	spin_unlock(&drivers_lock);
87 	return NULL;
88 }
89 
zpool_put_driver(struct zpool_driver * driver)90 static void zpool_put_driver(struct zpool_driver *driver)
91 {
92 	atomic_dec(&driver->refcount);
93 	module_put(driver->owner);
94 }
95 
96 /**
97  * zpool_has_pool() - Check if the pool driver is available
98  * @type:	The type of the zpool to check (e.g. zsmalloc)
99  *
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
108  * fail.
109  *
110  * The @type string must be null-terminated.
111  *
112  * Returns: true if @type pool is available, false if not
113  */
zpool_has_pool(char * type)114 bool zpool_has_pool(char *type)
115 {
116 	struct zpool_driver *driver = zpool_get_driver(type);
117 
118 	if (!driver) {
119 		request_module("zpool-%s", type);
120 		driver = zpool_get_driver(type);
121 	}
122 
123 	if (!driver)
124 		return false;
125 
126 	zpool_put_driver(driver);
127 	return true;
128 }
129 EXPORT_SYMBOL(zpool_has_pool);
130 
131 /**
132  * zpool_create_pool() - Create a new zpool
133  * @type:	The type of the zpool to create (e.g. zsmalloc)
134  * @name:	The name of the zpool (e.g. zram0, zswap)
135  * @gfp:	The GFP flags to use when allocating the pool.
136  *
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.
140  *
141  * Implementations must guarantee this to be thread-safe.
142  *
143  * The @type and @name strings must be null-terminated.
144  *
145  * Returns: New zpool on success, NULL on failure.
146  */
zpool_create_pool(const char * type,const char * name,gfp_t gfp)147 struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp)
148 {
149 	struct zpool_driver *driver;
150 	struct zpool *zpool;
151 
152 	pr_debug("creating pool type %s\n", type);
153 
154 	driver = zpool_get_driver(type);
155 
156 	if (!driver) {
157 		request_module("zpool-%s", type);
158 		driver = zpool_get_driver(type);
159 	}
160 
161 	if (!driver) {
162 		pr_err("no driver for type %s\n", type);
163 		return NULL;
164 	}
165 
166 	zpool = kmalloc(sizeof(*zpool), gfp);
167 	if (!zpool) {
168 		pr_err("couldn't create zpool - out of memory\n");
169 		zpool_put_driver(driver);
170 		return NULL;
171 	}
172 
173 	zpool->driver = driver;
174 	zpool->pool = driver->create(name, gfp);
175 
176 	if (!zpool->pool) {
177 		pr_err("couldn't create %s pool\n", type);
178 		zpool_put_driver(driver);
179 		kfree(zpool);
180 		return NULL;
181 	}
182 
183 	pr_debug("created pool type %s\n", type);
184 
185 	return zpool;
186 }
187 
188 /**
189  * zpool_destroy_pool() - Destroy a zpool
190  * @zpool:	The zpool to destroy.
191  *
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.
196  *
197  * This destroys an existing zpool.  The zpool should not be in use.
198  */
zpool_destroy_pool(struct zpool * zpool)199 void zpool_destroy_pool(struct zpool *zpool)
200 {
201 	pr_debug("destroying pool type %s\n", zpool->driver->type);
202 
203 	zpool->driver->destroy(zpool->pool);
204 	zpool_put_driver(zpool->driver);
205 	kfree(zpool);
206 }
207 
208 /**
209  * zpool_get_type() - Get the type of the zpool
210  * @zpool:	The zpool to check
211  *
212  * This returns the type of the pool.
213  *
214  * Implementations must guarantee this to be thread-safe.
215  *
216  * Returns: The type of zpool.
217  */
zpool_get_type(struct zpool * zpool)218 const char *zpool_get_type(struct zpool *zpool)
219 {
220 	return zpool->driver->type;
221 }
222 
223 /**
224  * zpool_malloc() - Allocate memory
225  * @zpool:	The zpool to allocate from.
226  * @size:	The amount of memory to allocate.
227  * @gfp:	The GFP flags to use when allocating memory.
228  * @handle:	Pointer to the handle to set
229  * @nid:	The preferred node id.
230  *
231  * This allocates the requested amount of memory from the pool.
232  * The gfp flags will be used when allocating memory, if the
233  * implementation supports it.  The provided @handle will be
234  * set to the allocated object handle. The allocation will
235  * prefer the NUMA node specified by @nid.
236  *
237  * Implementations must guarantee this to be thread-safe.
238  *
239  * Returns: 0 on success, negative value on error.
240  */
zpool_malloc(struct zpool * zpool,size_t size,gfp_t gfp,unsigned long * handle,const int nid)241 int zpool_malloc(struct zpool *zpool, size_t size, gfp_t gfp,
242 		 unsigned long *handle, const int nid)
243 {
244 	return zpool->driver->malloc(zpool->pool, size, gfp, handle, nid);
245 }
246 
247 /**
248  * zpool_free() - Free previously allocated memory
249  * @zpool:	The zpool that allocated the memory.
250  * @handle:	The handle to the memory to free.
251  *
252  * This frees previously allocated memory.  This does not guarantee
253  * that the pool will actually free memory, only that the memory
254  * in the pool will become available for use by the pool.
255  *
256  * Implementations must guarantee this to be thread-safe,
257  * however only when freeing different handles.  The same
258  * handle should only be freed once, and should not be used
259  * after freeing.
260  */
zpool_free(struct zpool * zpool,unsigned long handle)261 void zpool_free(struct zpool *zpool, unsigned long handle)
262 {
263 	zpool->driver->free(zpool->pool, handle);
264 }
265 
266 /**
267  * zpool_obj_read_begin() - Start reading from a previously allocated handle.
268  * @zpool:	The zpool that the handle was allocated from
269  * @handle:	The handle to read from
270  * @local_copy:	A local buffer to use if needed.
271  *
272  * This starts a read operation of a previously allocated handle. The passed
273  * @local_copy buffer may be used if needed by copying the memory into.
274  * zpool_obj_read_end() MUST be called after the read is completed to undo any
275  * actions taken (e.g. release locks).
276  *
277  * Returns: A pointer to the handle memory to be read, if @local_copy is used,
278  * the returned pointer is @local_copy.
279  */
zpool_obj_read_begin(struct zpool * zpool,unsigned long handle,void * local_copy)280 void *zpool_obj_read_begin(struct zpool *zpool, unsigned long handle,
281 			   void *local_copy)
282 {
283 	return zpool->driver->obj_read_begin(zpool->pool, handle, local_copy);
284 }
285 
286 /**
287  * zpool_obj_read_end() - Finish reading from a previously allocated handle.
288  * @zpool:	The zpool that the handle was allocated from
289  * @handle:	The handle to read from
290  * @handle_mem:	The pointer returned by zpool_obj_read_begin()
291  *
292  * Finishes a read operation previously started by zpool_obj_read_begin().
293  */
zpool_obj_read_end(struct zpool * zpool,unsigned long handle,void * handle_mem)294 void zpool_obj_read_end(struct zpool *zpool, unsigned long handle,
295 			void *handle_mem)
296 {
297 	zpool->driver->obj_read_end(zpool->pool, handle, handle_mem);
298 }
299 
300 /**
301  * zpool_obj_write() - Write to a previously allocated handle.
302  * @zpool:	The zpool that the handle was allocated from
303  * @handle:	The handle to read from
304  * @handle_mem:	The memory to copy from into the handle.
305  * @mem_len:	The length of memory to be written.
306  *
307  */
zpool_obj_write(struct zpool * zpool,unsigned long handle,void * handle_mem,size_t mem_len)308 void zpool_obj_write(struct zpool *zpool, unsigned long handle,
309 		     void *handle_mem, size_t mem_len)
310 {
311 	zpool->driver->obj_write(zpool->pool, handle, handle_mem, mem_len);
312 }
313 
314 /**
315  * zpool_get_total_pages() - The total size of the pool
316  * @zpool:	The zpool to check
317  *
318  * This returns the total size in pages of the pool.
319  *
320  * Returns: Total size of the zpool in pages.
321  */
zpool_get_total_pages(struct zpool * zpool)322 u64 zpool_get_total_pages(struct zpool *zpool)
323 {
324 	return zpool->driver->total_pages(zpool->pool);
325 }
326 
327 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
328 MODULE_DESCRIPTION("Common API for compressed memory storage");
329