dm-bio-prison-v2.c 8.2 KB

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