zpool.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/list.h>
  12. #include <linux/types.h>
  13. #include <linux/mm.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/module.h>
  17. #include <linux/zpool.h>
  18. struct zpool {
  19. struct zpool_driver *driver;
  20. void *pool;
  21. const struct zpool_ops *ops;
  22. bool evictable;
  23. struct list_head list;
  24. };
  25. static LIST_HEAD(drivers_head);
  26. static DEFINE_SPINLOCK(drivers_lock);
  27. static LIST_HEAD(pools_head);
  28. static DEFINE_SPINLOCK(pools_lock);
  29. /**
  30. * zpool_register_driver() - register a zpool implementation.
  31. * @driver: driver to register
  32. */
  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. * zpool_unregister_driver() - unregister a zpool implementation.
  43. * @driver: driver to unregister.
  44. *
  45. * Module usage counting is used to prevent using a driver
  46. * while/after unloading, so if this is called from module
  47. * exit function, this should never fail; if called from
  48. * other than the module exit function, and this returns
  49. * failure, the driver is in use and must remain available.
  50. */
  51. int zpool_unregister_driver(struct zpool_driver *driver)
  52. {
  53. int ret = 0, refcount;
  54. spin_lock(&drivers_lock);
  55. refcount = atomic_read(&driver->refcount);
  56. WARN_ON(refcount < 0);
  57. if (refcount > 0)
  58. ret = -EBUSY;
  59. else
  60. list_del(&driver->list);
  61. spin_unlock(&drivers_lock);
  62. return ret;
  63. }
  64. EXPORT_SYMBOL(zpool_unregister_driver);
  65. /* this assumes @type is null-terminated. */
  66. static struct zpool_driver *zpool_get_driver(const char *type)
  67. {
  68. struct zpool_driver *driver;
  69. spin_lock(&drivers_lock);
  70. list_for_each_entry(driver, &drivers_head, list) {
  71. if (!strcmp(driver->type, type)) {
  72. bool got = try_module_get(driver->owner);
  73. if (got)
  74. atomic_inc(&driver->refcount);
  75. spin_unlock(&drivers_lock);
  76. return got ? driver : NULL;
  77. }
  78. }
  79. spin_unlock(&drivers_lock);
  80. return NULL;
  81. }
  82. static void zpool_put_driver(struct zpool_driver *driver)
  83. {
  84. atomic_dec(&driver->refcount);
  85. module_put(driver->owner);
  86. }
  87. /**
  88. * zpool_has_pool() - Check if the pool driver is available
  89. * @type: The type of the zpool to check (e.g. zbud, zsmalloc)
  90. *
  91. * This checks if the @type pool driver is available. This will try to load
  92. * the requested module, if needed, but there is no guarantee the module will
  93. * still be loaded and available immediately after calling. If this returns
  94. * true, the caller should assume the pool is available, but must be prepared
  95. * to handle the @zpool_create_pool() returning failure. However if this
  96. * returns false, the caller should assume the requested pool type is not
  97. * available; either the requested pool type module does not exist, or could
  98. * not be loaded, and calling @zpool_create_pool() with the pool type will
  99. * fail.
  100. *
  101. * The @type string must be null-terminated.
  102. *
  103. * Returns: true if @type pool is available, false if not
  104. */
  105. bool zpool_has_pool(char *type)
  106. {
  107. struct zpool_driver *driver = zpool_get_driver(type);
  108. if (!driver) {
  109. request_module("zpool-%s", type);
  110. driver = zpool_get_driver(type);
  111. }
  112. if (!driver)
  113. return false;
  114. zpool_put_driver(driver);
  115. return true;
  116. }
  117. EXPORT_SYMBOL(zpool_has_pool);
  118. /**
  119. * zpool_create_pool() - Create a new zpool
  120. * @type: The type of the zpool to create (e.g. zbud, zsmalloc)
  121. * @name: The name of the zpool (e.g. zram0, zswap)
  122. * @gfp: The GFP flags to use when allocating the pool.
  123. * @ops: The optional ops callback.
  124. *
  125. * This creates a new zpool of the specified type. The gfp flags will be
  126. * used when allocating memory, if the implementation supports it. If the
  127. * ops param is NULL, then the created zpool will not be evictable.
  128. *
  129. * Implementations must guarantee this to be thread-safe.
  130. *
  131. * The @type and @name strings must be null-terminated.
  132. *
  133. * Returns: New zpool on success, NULL on failure.
  134. */
  135. struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp,
  136. const struct zpool_ops *ops)
  137. {
  138. struct zpool_driver *driver;
  139. struct zpool *zpool;
  140. pr_debug("creating pool type %s\n", type);
  141. driver = zpool_get_driver(type);
  142. if (!driver) {
  143. request_module("zpool-%s", type);
  144. driver = zpool_get_driver(type);
  145. }
  146. if (!driver) {
  147. pr_err("no driver for type %s\n", type);
  148. return NULL;
  149. }
  150. zpool = kmalloc(sizeof(*zpool), gfp);
  151. if (!zpool) {
  152. pr_err("couldn't create zpool - out of memory\n");
  153. zpool_put_driver(driver);
  154. return NULL;
  155. }
  156. zpool->driver = driver;
  157. zpool->pool = driver->create(name, gfp, ops, zpool);
  158. zpool->ops = ops;
  159. zpool->evictable = driver->shrink && ops && ops->evict;
  160. if (!zpool->pool) {
  161. pr_err("couldn't create %s pool\n", type);
  162. zpool_put_driver(driver);
  163. kfree(zpool);
  164. return NULL;
  165. }
  166. pr_debug("created pool type %s\n", type);
  167. spin_lock(&pools_lock);
  168. list_add(&zpool->list, &pools_head);
  169. spin_unlock(&pools_lock);
  170. return zpool;
  171. }
  172. /**
  173. * zpool_destroy_pool() - Destroy a zpool
  174. * @zpool: The zpool to destroy.
  175. *
  176. * Implementations must guarantee this to be thread-safe,
  177. * however only when destroying different pools. The same
  178. * pool should only be destroyed once, and should not be used
  179. * after it is destroyed.
  180. *
  181. * This destroys an existing zpool. The zpool should not be in use.
  182. */
  183. void zpool_destroy_pool(struct zpool *zpool)
  184. {
  185. pr_debug("destroying pool type %s\n", zpool->driver->type);
  186. spin_lock(&pools_lock);
  187. list_del(&zpool->list);
  188. spin_unlock(&pools_lock);
  189. zpool->driver->destroy(zpool->pool);
  190. zpool_put_driver(zpool->driver);
  191. kfree(zpool);
  192. }
  193. /**
  194. * zpool_get_type() - Get the type of the zpool
  195. * @zpool: The zpool to check
  196. *
  197. * This returns the type of the pool.
  198. *
  199. * Implementations must guarantee this to be thread-safe.
  200. *
  201. * Returns: The type of zpool.
  202. */
  203. const char *zpool_get_type(struct zpool *zpool)
  204. {
  205. return zpool->driver->type;
  206. }
  207. /**
  208. * zpool_malloc_support_movable() - Check if the zpool supports
  209. * allocating movable memory
  210. * @zpool: The zpool to check
  211. *
  212. * This returns if the zpool supports allocating movable memory.
  213. *
  214. * Implementations must guarantee this to be thread-safe.
  215. *
  216. * Returns: true if the zpool supports allocating movable memory, false if not
  217. */
  218. bool zpool_malloc_support_movable(struct zpool *zpool)
  219. {
  220. return zpool->driver->malloc_support_movable;
  221. }
  222. /**
  223. * zpool_malloc() - Allocate memory
  224. * @zpool: The zpool to allocate from.
  225. * @size: The amount of memory to allocate.
  226. * @gfp: The GFP flags to use when allocating memory.
  227. * @handle: Pointer to the handle to set
  228. *
  229. * This allocates the requested amount of memory from the pool.
  230. * The gfp flags will be used when allocating memory, if the
  231. * implementation supports it. The provided @handle will be
  232. * set to the allocated object handle.
  233. *
  234. * Implementations must guarantee this to be thread-safe.
  235. *
  236. * Returns: 0 on success, negative value on error.
  237. */
  238. int zpool_malloc(struct zpool *zpool, size_t size, gfp_t gfp,
  239. unsigned long *handle)
  240. {
  241. return zpool->driver->malloc(zpool->pool, size, gfp, handle);
  242. }
  243. /**
  244. * zpool_free() - Free previously allocated memory
  245. * @zpool: The zpool that allocated the memory.
  246. * @handle: The handle to the memory to free.
  247. *
  248. * This frees previously allocated memory. This does not guarantee
  249. * that the pool will actually free memory, only that the memory
  250. * in the pool will become available for use by the pool.
  251. *
  252. * Implementations must guarantee this to be thread-safe,
  253. * however only when freeing different handles. The same
  254. * handle should only be freed once, and should not be used
  255. * after freeing.
  256. */
  257. void zpool_free(struct zpool *zpool, unsigned long handle)
  258. {
  259. zpool->driver->free(zpool->pool, handle);
  260. }
  261. /**
  262. * zpool_shrink() - Shrink the pool size
  263. * @zpool: The zpool to shrink.
  264. * @pages: The number of pages to shrink the pool.
  265. * @reclaimed: The number of pages successfully evicted.
  266. *
  267. * This attempts to shrink the actual memory size of the pool
  268. * by evicting currently used handle(s). If the pool was
  269. * created with no zpool_ops, or the evict call fails for any
  270. * of the handles, this will fail. If non-NULL, the @reclaimed
  271. * parameter will be set to the number of pages reclaimed,
  272. * which may be more than the number of pages requested.
  273. *
  274. * Implementations must guarantee this to be thread-safe.
  275. *
  276. * Returns: 0 on success, negative value on error/failure.
  277. */
  278. int zpool_shrink(struct zpool *zpool, unsigned int pages,
  279. unsigned int *reclaimed)
  280. {
  281. return zpool->driver->shrink ?
  282. zpool->driver->shrink(zpool->pool, pages, reclaimed) : -EINVAL;
  283. }
  284. /**
  285. * zpool_map_handle() - Map a previously allocated handle into memory
  286. * @zpool: The zpool that the handle was allocated from
  287. * @handle: The handle to map
  288. * @mapmode: How the memory should be mapped
  289. *
  290. * This maps a previously allocated handle into memory. The @mapmode
  291. * param indicates to the implementation how the memory will be
  292. * used, i.e. read-only, write-only, read-write. If the
  293. * implementation does not support it, the memory will be treated
  294. * as read-write.
  295. *
  296. * This may hold locks, disable interrupts, and/or preemption,
  297. * and the zpool_unmap_handle() must be called to undo those
  298. * actions. The code that uses the mapped handle should complete
  299. * its operatons on the mapped handle memory quickly and unmap
  300. * as soon as possible. As the implementation may use per-cpu
  301. * data, multiple handles should not be mapped concurrently on
  302. * any cpu.
  303. *
  304. * Returns: A pointer to the handle's mapped memory area.
  305. */
  306. void *zpool_map_handle(struct zpool *zpool, unsigned long handle,
  307. enum zpool_mapmode mapmode)
  308. {
  309. return zpool->driver->map(zpool->pool, handle, mapmode);
  310. }
  311. /**
  312. * zpool_unmap_handle() - Unmap a previously mapped handle
  313. * @zpool: The zpool that the handle was allocated from
  314. * @handle: The handle to unmap
  315. *
  316. * This unmaps a previously mapped handle. Any locks or other
  317. * actions that the implementation took in zpool_map_handle()
  318. * will be undone here. The memory area returned from
  319. * zpool_map_handle() should no longer be used after this.
  320. */
  321. void zpool_unmap_handle(struct zpool *zpool, unsigned long handle)
  322. {
  323. zpool->driver->unmap(zpool->pool, handle);
  324. }
  325. /**
  326. * zpool_get_total_size() - The total size of the pool
  327. * @zpool: The zpool to check
  328. *
  329. * This returns the total size in bytes of the pool.
  330. *
  331. * Returns: Total size of the zpool in bytes.
  332. */
  333. u64 zpool_get_total_size(struct zpool *zpool)
  334. {
  335. return zpool->driver->total_size(zpool->pool);
  336. }
  337. /**
  338. * zpool_evictable() - Test if zpool is potentially evictable
  339. * @zpool: The zpool to test
  340. *
  341. * Zpool is only potentially evictable when it's created with struct
  342. * zpool_ops.evict and its driver implements struct zpool_driver.shrink.
  343. *
  344. * However, it doesn't necessarily mean driver will use zpool_ops.evict
  345. * in its implementation of zpool_driver.shrink. It could do internal
  346. * defragmentation instead.
  347. *
  348. * Returns: true if potentially evictable; false otherwise.
  349. */
  350. bool zpool_evictable(struct zpool *zpool)
  351. {
  352. return zpool->evictable;
  353. }
  354. MODULE_LICENSE("GPL");
  355. MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
  356. MODULE_DESCRIPTION("Common API for compressed memory storage");