zpool.c 11 KB

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