zpool.c 11 KB

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