dm-bio-prison-v2.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012-2017 Red Hat, Inc.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm.h"
  8. #include "dm-bio-prison-v2.h"
  9. #include <linux/spinlock.h>
  10. #include <linux/mempool.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/rwsem.h>
  14. /*----------------------------------------------------------------*/
  15. #define MIN_CELLS 1024
  16. struct dm_bio_prison_v2 {
  17. struct workqueue_struct *wq;
  18. spinlock_t lock;
  19. struct rb_root cells;
  20. mempool_t cell_pool;
  21. };
  22. static struct kmem_cache *_cell_cache;
  23. /*----------------------------------------------------------------*/
  24. /*
  25. * @nr_cells should be the number of cells you want in use _concurrently_.
  26. * Don't confuse it with the number of distinct keys.
  27. */
  28. struct dm_bio_prison_v2 *dm_bio_prison_create_v2(struct workqueue_struct *wq)
  29. {
  30. struct dm_bio_prison_v2 *prison = kzalloc_obj(*prison);
  31. int ret;
  32. if (!prison)
  33. return NULL;
  34. prison->wq = wq;
  35. spin_lock_init(&prison->lock);
  36. ret = mempool_init_slab_pool(&prison->cell_pool, MIN_CELLS, _cell_cache);
  37. if (ret) {
  38. kfree(prison);
  39. return NULL;
  40. }
  41. prison->cells = RB_ROOT;
  42. return prison;
  43. }
  44. EXPORT_SYMBOL_GPL(dm_bio_prison_create_v2);
  45. void dm_bio_prison_destroy_v2(struct dm_bio_prison_v2 *prison)
  46. {
  47. mempool_exit(&prison->cell_pool);
  48. kfree(prison);
  49. }
  50. EXPORT_SYMBOL_GPL(dm_bio_prison_destroy_v2);
  51. struct dm_bio_prison_cell_v2 *dm_bio_prison_alloc_cell_v2(struct dm_bio_prison_v2 *prison, gfp_t gfp)
  52. {
  53. return mempool_alloc(&prison->cell_pool, gfp);
  54. }
  55. EXPORT_SYMBOL_GPL(dm_bio_prison_alloc_cell_v2);
  56. void dm_bio_prison_free_cell_v2(struct dm_bio_prison_v2 *prison,
  57. struct dm_bio_prison_cell_v2 *cell)
  58. {
  59. mempool_free(cell, &prison->cell_pool);
  60. }
  61. EXPORT_SYMBOL_GPL(dm_bio_prison_free_cell_v2);
  62. static void __setup_new_cell(struct dm_cell_key_v2 *key,
  63. struct dm_bio_prison_cell_v2 *cell)
  64. {
  65. memset(cell, 0, sizeof(*cell));
  66. memcpy(&cell->key, key, sizeof(cell->key));
  67. bio_list_init(&cell->bios);
  68. }
  69. static int cmp_keys(struct dm_cell_key_v2 *lhs,
  70. struct dm_cell_key_v2 *rhs)
  71. {
  72. if (lhs->virtual < rhs->virtual)
  73. return -1;
  74. if (lhs->virtual > rhs->virtual)
  75. return 1;
  76. if (lhs->dev < rhs->dev)
  77. return -1;
  78. if (lhs->dev > rhs->dev)
  79. return 1;
  80. if (lhs->block_end <= rhs->block_begin)
  81. return -1;
  82. if (lhs->block_begin >= rhs->block_end)
  83. return 1;
  84. return 0;
  85. }
  86. /*
  87. * Returns true if node found, otherwise it inserts a new one.
  88. */
  89. static bool __find_or_insert(struct dm_bio_prison_v2 *prison,
  90. struct dm_cell_key_v2 *key,
  91. struct dm_bio_prison_cell_v2 *cell_prealloc,
  92. struct dm_bio_prison_cell_v2 **result)
  93. {
  94. int r;
  95. struct rb_node **new = &prison->cells.rb_node, *parent = NULL;
  96. while (*new) {
  97. struct dm_bio_prison_cell_v2 *cell =
  98. rb_entry(*new, struct dm_bio_prison_cell_v2, node);
  99. r = cmp_keys(key, &cell->key);
  100. parent = *new;
  101. if (r < 0)
  102. new = &((*new)->rb_left);
  103. else if (r > 0)
  104. new = &((*new)->rb_right);
  105. else {
  106. *result = cell;
  107. return true;
  108. }
  109. }
  110. __setup_new_cell(key, cell_prealloc);
  111. *result = cell_prealloc;
  112. rb_link_node(&cell_prealloc->node, parent, new);
  113. rb_insert_color(&cell_prealloc->node, &prison->cells);
  114. return false;
  115. }
  116. static bool __get(struct dm_bio_prison_v2 *prison,
  117. struct dm_cell_key_v2 *key,
  118. unsigned int lock_level,
  119. struct bio *inmate,
  120. struct dm_bio_prison_cell_v2 *cell_prealloc,
  121. struct dm_bio_prison_cell_v2 **cell)
  122. {
  123. if (__find_or_insert(prison, key, cell_prealloc, cell)) {
  124. if ((*cell)->exclusive_lock) {
  125. if (lock_level <= (*cell)->exclusive_level) {
  126. bio_list_add(&(*cell)->bios, inmate);
  127. return false;
  128. }
  129. }
  130. (*cell)->shared_count++;
  131. } else
  132. (*cell)->shared_count = 1;
  133. return true;
  134. }
  135. bool dm_cell_get_v2(struct dm_bio_prison_v2 *prison,
  136. struct dm_cell_key_v2 *key,
  137. unsigned int lock_level,
  138. struct bio *inmate,
  139. struct dm_bio_prison_cell_v2 *cell_prealloc,
  140. struct dm_bio_prison_cell_v2 **cell_result)
  141. {
  142. int r;
  143. spin_lock_irq(&prison->lock);
  144. r = __get(prison, key, lock_level, inmate, cell_prealloc, cell_result);
  145. spin_unlock_irq(&prison->lock);
  146. return r;
  147. }
  148. EXPORT_SYMBOL_GPL(dm_cell_get_v2);
  149. static bool __put(struct dm_bio_prison_v2 *prison,
  150. struct dm_bio_prison_cell_v2 *cell)
  151. {
  152. BUG_ON(!cell->shared_count);
  153. cell->shared_count--;
  154. // FIXME: shared locks granted above the lock level could starve this
  155. if (!cell->shared_count) {
  156. if (cell->exclusive_lock) {
  157. if (cell->quiesce_continuation) {
  158. queue_work(prison->wq, cell->quiesce_continuation);
  159. cell->quiesce_continuation = NULL;
  160. }
  161. } else {
  162. rb_erase(&cell->node, &prison->cells);
  163. return true;
  164. }
  165. }
  166. return false;
  167. }
  168. bool dm_cell_put_v2(struct dm_bio_prison_v2 *prison,
  169. struct dm_bio_prison_cell_v2 *cell)
  170. {
  171. bool r;
  172. unsigned long flags;
  173. spin_lock_irqsave(&prison->lock, flags);
  174. r = __put(prison, cell);
  175. spin_unlock_irqrestore(&prison->lock, flags);
  176. return r;
  177. }
  178. EXPORT_SYMBOL_GPL(dm_cell_put_v2);
  179. static int __lock(struct dm_bio_prison_v2 *prison,
  180. struct dm_cell_key_v2 *key,
  181. unsigned int lock_level,
  182. struct dm_bio_prison_cell_v2 *cell_prealloc,
  183. struct dm_bio_prison_cell_v2 **cell_result)
  184. {
  185. struct dm_bio_prison_cell_v2 *cell;
  186. if (__find_or_insert(prison, key, cell_prealloc, &cell)) {
  187. if (cell->exclusive_lock)
  188. return -EBUSY;
  189. cell->exclusive_lock = true;
  190. cell->exclusive_level = lock_level;
  191. *cell_result = cell;
  192. // FIXME: we don't yet know what level these shared locks
  193. // were taken at, so have to quiesce them all.
  194. return cell->shared_count > 0;
  195. } else {
  196. cell = cell_prealloc;
  197. cell->shared_count = 0;
  198. cell->exclusive_lock = true;
  199. cell->exclusive_level = lock_level;
  200. *cell_result = cell;
  201. }
  202. return 0;
  203. }
  204. int dm_cell_lock_v2(struct dm_bio_prison_v2 *prison,
  205. struct dm_cell_key_v2 *key,
  206. unsigned int lock_level,
  207. struct dm_bio_prison_cell_v2 *cell_prealloc,
  208. struct dm_bio_prison_cell_v2 **cell_result)
  209. {
  210. int r;
  211. spin_lock_irq(&prison->lock);
  212. r = __lock(prison, key, lock_level, cell_prealloc, cell_result);
  213. spin_unlock_irq(&prison->lock);
  214. return r;
  215. }
  216. EXPORT_SYMBOL_GPL(dm_cell_lock_v2);
  217. static void __quiesce(struct dm_bio_prison_v2 *prison,
  218. struct dm_bio_prison_cell_v2 *cell,
  219. struct work_struct *continuation)
  220. {
  221. if (!cell->shared_count)
  222. queue_work(prison->wq, continuation);
  223. else
  224. cell->quiesce_continuation = continuation;
  225. }
  226. void dm_cell_quiesce_v2(struct dm_bio_prison_v2 *prison,
  227. struct dm_bio_prison_cell_v2 *cell,
  228. struct work_struct *continuation)
  229. {
  230. spin_lock_irq(&prison->lock);
  231. __quiesce(prison, cell, continuation);
  232. spin_unlock_irq(&prison->lock);
  233. }
  234. EXPORT_SYMBOL_GPL(dm_cell_quiesce_v2);
  235. static int __promote(struct dm_bio_prison_v2 *prison,
  236. struct dm_bio_prison_cell_v2 *cell,
  237. unsigned int new_lock_level)
  238. {
  239. if (!cell->exclusive_lock)
  240. return -EINVAL;
  241. cell->exclusive_level = new_lock_level;
  242. return cell->shared_count > 0;
  243. }
  244. int dm_cell_lock_promote_v2(struct dm_bio_prison_v2 *prison,
  245. struct dm_bio_prison_cell_v2 *cell,
  246. unsigned int new_lock_level)
  247. {
  248. int r;
  249. spin_lock_irq(&prison->lock);
  250. r = __promote(prison, cell, new_lock_level);
  251. spin_unlock_irq(&prison->lock);
  252. return r;
  253. }
  254. EXPORT_SYMBOL_GPL(dm_cell_lock_promote_v2);
  255. static bool __unlock(struct dm_bio_prison_v2 *prison,
  256. struct dm_bio_prison_cell_v2 *cell,
  257. struct bio_list *bios)
  258. {
  259. BUG_ON(!cell->exclusive_lock);
  260. bio_list_merge_init(bios, &cell->bios);
  261. if (cell->shared_count) {
  262. cell->exclusive_lock = false;
  263. return false;
  264. }
  265. rb_erase(&cell->node, &prison->cells);
  266. return true;
  267. }
  268. bool dm_cell_unlock_v2(struct dm_bio_prison_v2 *prison,
  269. struct dm_bio_prison_cell_v2 *cell,
  270. struct bio_list *bios)
  271. {
  272. bool r;
  273. spin_lock_irq(&prison->lock);
  274. r = __unlock(prison, cell, bios);
  275. spin_unlock_irq(&prison->lock);
  276. return r;
  277. }
  278. EXPORT_SYMBOL_GPL(dm_cell_unlock_v2);
  279. /*----------------------------------------------------------------*/
  280. int __init dm_bio_prison_init_v2(void)
  281. {
  282. _cell_cache = KMEM_CACHE(dm_bio_prison_cell_v2, 0);
  283. if (!_cell_cache)
  284. return -ENOMEM;
  285. return 0;
  286. }
  287. void dm_bio_prison_exit_v2(void)
  288. {
  289. kmem_cache_destroy(_cell_cache);
  290. _cell_cache = NULL;
  291. }