dm-bio-prison-v2.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 int 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 int 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. spin_lock_irq(&prison->lock);
  143. r = __get(prison, key, lock_level, inmate, cell_prealloc, cell_result);
  144. spin_unlock_irq(&prison->lock);
  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 int 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 int 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. spin_lock_irq(&prison->lock);
  211. r = __lock(prison, key, lock_level, cell_prealloc, cell_result);
  212. spin_unlock_irq(&prison->lock);
  213. return r;
  214. }
  215. EXPORT_SYMBOL_GPL(dm_cell_lock_v2);
  216. static void __quiesce(struct dm_bio_prison_v2 *prison,
  217. struct dm_bio_prison_cell_v2 *cell,
  218. struct work_struct *continuation)
  219. {
  220. if (!cell->shared_count)
  221. queue_work(prison->wq, continuation);
  222. else
  223. cell->quiesce_continuation = continuation;
  224. }
  225. void dm_cell_quiesce_v2(struct dm_bio_prison_v2 *prison,
  226. struct dm_bio_prison_cell_v2 *cell,
  227. struct work_struct *continuation)
  228. {
  229. spin_lock_irq(&prison->lock);
  230. __quiesce(prison, cell, continuation);
  231. spin_unlock_irq(&prison->lock);
  232. }
  233. EXPORT_SYMBOL_GPL(dm_cell_quiesce_v2);
  234. static int __promote(struct dm_bio_prison_v2 *prison,
  235. struct dm_bio_prison_cell_v2 *cell,
  236. unsigned int new_lock_level)
  237. {
  238. if (!cell->exclusive_lock)
  239. return -EINVAL;
  240. cell->exclusive_level = new_lock_level;
  241. return cell->shared_count > 0;
  242. }
  243. int dm_cell_lock_promote_v2(struct dm_bio_prison_v2 *prison,
  244. struct dm_bio_prison_cell_v2 *cell,
  245. unsigned int new_lock_level)
  246. {
  247. int r;
  248. spin_lock_irq(&prison->lock);
  249. r = __promote(prison, cell, new_lock_level);
  250. spin_unlock_irq(&prison->lock);
  251. return r;
  252. }
  253. EXPORT_SYMBOL_GPL(dm_cell_lock_promote_v2);
  254. static bool __unlock(struct dm_bio_prison_v2 *prison,
  255. struct dm_bio_prison_cell_v2 *cell,
  256. struct bio_list *bios)
  257. {
  258. BUG_ON(!cell->exclusive_lock);
  259. bio_list_merge(bios, &cell->bios);
  260. bio_list_init(&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. }