dm-bio-prison-v2.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. struct rb_root cells;
  19. mempool_t cell_pool;
  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 = kzalloc(sizeof(*prison), GFP_KERNEL);
  30. int ret;
  31. if (!prison)
  32. return NULL;
  33. prison->wq = wq;
  34. spin_lock_init(&prison->lock);
  35. ret = mempool_init_slab_pool(&prison->cell_pool, MIN_CELLS, _cell_cache);
  36. if (ret) {
  37. kfree(prison);
  38. return NULL;
  39. }
  40. prison->cells = RB_ROOT;
  41. return prison;
  42. }
  43. EXPORT_SYMBOL_GPL(dm_bio_prison_create_v2);
  44. void dm_bio_prison_destroy_v2(struct dm_bio_prison_v2 *prison)
  45. {
  46. mempool_exit(&prison->cell_pool);
  47. kfree(prison);
  48. }
  49. EXPORT_SYMBOL_GPL(dm_bio_prison_destroy_v2);
  50. struct dm_bio_prison_cell_v2 *dm_bio_prison_alloc_cell_v2(struct dm_bio_prison_v2 *prison, gfp_t gfp)
  51. {
  52. return mempool_alloc(&prison->cell_pool, gfp);
  53. }
  54. EXPORT_SYMBOL_GPL(dm_bio_prison_alloc_cell_v2);
  55. void dm_bio_prison_free_cell_v2(struct dm_bio_prison_v2 *prison,
  56. struct dm_bio_prison_cell_v2 *cell)
  57. {
  58. mempool_free(cell, &prison->cell_pool);
  59. }
  60. EXPORT_SYMBOL_GPL(dm_bio_prison_free_cell_v2);
  61. static void __setup_new_cell(struct dm_cell_key_v2 *key,
  62. struct dm_bio_prison_cell_v2 *cell)
  63. {
  64. memset(cell, 0, sizeof(*cell));
  65. memcpy(&cell->key, key, sizeof(cell->key));
  66. bio_list_init(&cell->bios);
  67. }
  68. static int cmp_keys(struct dm_cell_key_v2 *lhs,
  69. struct dm_cell_key_v2 *rhs)
  70. {
  71. if (lhs->virtual < rhs->virtual)
  72. return -1;
  73. if (lhs->virtual > rhs->virtual)
  74. return 1;
  75. if (lhs->dev < rhs->dev)
  76. return -1;
  77. if (lhs->dev > rhs->dev)
  78. return 1;
  79. if (lhs->block_end <= rhs->block_begin)
  80. return -1;
  81. if (lhs->block_begin >= rhs->block_end)
  82. return 1;
  83. return 0;
  84. }
  85. /*
  86. * Returns true if node found, otherwise it inserts a new one.
  87. */
  88. static bool __find_or_insert(struct dm_bio_prison_v2 *prison,
  89. struct dm_cell_key_v2 *key,
  90. struct dm_bio_prison_cell_v2 *cell_prealloc,
  91. struct dm_bio_prison_cell_v2 **result)
  92. {
  93. int r;
  94. struct rb_node **new = &prison->cells.rb_node, *parent = NULL;
  95. while (*new) {
  96. struct dm_bio_prison_cell_v2 *cell =
  97. rb_entry(*new, struct dm_bio_prison_cell_v2, node);
  98. r = cmp_keys(key, &cell->key);
  99. parent = *new;
  100. if (r < 0)
  101. new = &((*new)->rb_left);
  102. else if (r > 0)
  103. new = &((*new)->rb_right);
  104. else {
  105. *result = cell;
  106. return true;
  107. }
  108. }
  109. __setup_new_cell(key, cell_prealloc);
  110. *result = cell_prealloc;
  111. rb_link_node(&cell_prealloc->node, parent, new);
  112. rb_insert_color(&cell_prealloc->node, &prison->cells);
  113. return false;
  114. }
  115. static bool __get(struct dm_bio_prison_v2 *prison,
  116. struct dm_cell_key_v2 *key,
  117. unsigned lock_level,
  118. struct bio *inmate,
  119. struct dm_bio_prison_cell_v2 *cell_prealloc,
  120. struct dm_bio_prison_cell_v2 **cell)
  121. {
  122. if (__find_or_insert(prison, key, cell_prealloc, cell)) {
  123. if ((*cell)->exclusive_lock) {
  124. if (lock_level <= (*cell)->exclusive_level) {
  125. bio_list_add(&(*cell)->bios, inmate);
  126. return false;
  127. }
  128. }
  129. (*cell)->shared_count++;
  130. } else
  131. (*cell)->shared_count = 1;
  132. return true;
  133. }
  134. bool dm_cell_get_v2(struct dm_bio_prison_v2 *prison,
  135. struct dm_cell_key_v2 *key,
  136. unsigned lock_level,
  137. struct bio *inmate,
  138. struct dm_bio_prison_cell_v2 *cell_prealloc,
  139. struct dm_bio_prison_cell_v2 **cell_result)
  140. {
  141. int r;
  142. unsigned long flags;
  143. spin_lock_irqsave(&prison->lock, flags);
  144. r = __get(prison, key, lock_level, inmate, cell_prealloc, cell_result);
  145. spin_unlock_irqrestore(&prison->lock, flags);
  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 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 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. unsigned long flags;
  212. spin_lock_irqsave(&prison->lock, flags);
  213. r = __lock(prison, key, lock_level, cell_prealloc, cell_result);
  214. spin_unlock_irqrestore(&prison->lock, flags);
  215. return r;
  216. }
  217. EXPORT_SYMBOL_GPL(dm_cell_lock_v2);
  218. static void __quiesce(struct dm_bio_prison_v2 *prison,
  219. struct dm_bio_prison_cell_v2 *cell,
  220. struct work_struct *continuation)
  221. {
  222. if (!cell->shared_count)
  223. queue_work(prison->wq, continuation);
  224. else
  225. cell->quiesce_continuation = continuation;
  226. }
  227. void dm_cell_quiesce_v2(struct dm_bio_prison_v2 *prison,
  228. struct dm_bio_prison_cell_v2 *cell,
  229. struct work_struct *continuation)
  230. {
  231. unsigned long flags;
  232. spin_lock_irqsave(&prison->lock, flags);
  233. __quiesce(prison, cell, continuation);
  234. spin_unlock_irqrestore(&prison->lock, flags);
  235. }
  236. EXPORT_SYMBOL_GPL(dm_cell_quiesce_v2);
  237. static int __promote(struct dm_bio_prison_v2 *prison,
  238. struct dm_bio_prison_cell_v2 *cell,
  239. unsigned new_lock_level)
  240. {
  241. if (!cell->exclusive_lock)
  242. return -EINVAL;
  243. cell->exclusive_level = new_lock_level;
  244. return cell->shared_count > 0;
  245. }
  246. int dm_cell_lock_promote_v2(struct dm_bio_prison_v2 *prison,
  247. struct dm_bio_prison_cell_v2 *cell,
  248. unsigned new_lock_level)
  249. {
  250. int r;
  251. unsigned long flags;
  252. spin_lock_irqsave(&prison->lock, flags);
  253. r = __promote(prison, cell, new_lock_level);
  254. spin_unlock_irqrestore(&prison->lock, flags);
  255. return r;
  256. }
  257. EXPORT_SYMBOL_GPL(dm_cell_lock_promote_v2);
  258. static bool __unlock(struct dm_bio_prison_v2 *prison,
  259. struct dm_bio_prison_cell_v2 *cell,
  260. struct bio_list *bios)
  261. {
  262. BUG_ON(!cell->exclusive_lock);
  263. bio_list_merge(bios, &cell->bios);
  264. bio_list_init(&cell->bios);
  265. if (cell->shared_count) {
  266. cell->exclusive_lock = 0;
  267. return false;
  268. }
  269. rb_erase(&cell->node, &prison->cells);
  270. return true;
  271. }
  272. bool dm_cell_unlock_v2(struct dm_bio_prison_v2 *prison,
  273. struct dm_bio_prison_cell_v2 *cell,
  274. struct bio_list *bios)
  275. {
  276. bool r;
  277. unsigned long flags;
  278. spin_lock_irqsave(&prison->lock, flags);
  279. r = __unlock(prison, cell, bios);
  280. spin_unlock_irqrestore(&prison->lock, flags);
  281. return r;
  282. }
  283. EXPORT_SYMBOL_GPL(dm_cell_unlock_v2);
  284. /*----------------------------------------------------------------*/
  285. int __init dm_bio_prison_init_v2(void)
  286. {
  287. _cell_cache = KMEM_CACHE(dm_bio_prison_cell_v2, 0);
  288. if (!_cell_cache)
  289. return -ENOMEM;
  290. return 0;
  291. }
  292. void dm_bio_prison_exit_v2(void)
  293. {
  294. kmem_cache_destroy(_cell_cache);
  295. _cell_cache = NULL;
  296. }