1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Red Hat, Inc.
5 * This file is released under the GPL.
9 #include "dm-bio-prison-v1.h"
10 #include "dm-bio-prison-v2.h"
12 #include <linux/spinlock.h>
13 #include <linux/mempool.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
17 /*----------------------------------------------------------------*/
19 #define MIN_CELLS 1024
21 struct prison_region {
24 } ____cacheline_aligned_in_smp;
26 struct dm_bio_prison {
28 unsigned int num_locks;
29 struct prison_region regions[];
32 static struct kmem_cache *_cell_cache;
34 /*----------------------------------------------------------------*/
37 * @nr_cells should be the number of cells you want in use _concurrently_.
38 * Don't confuse it with the number of distinct keys.
40 struct dm_bio_prison *dm_bio_prison_create(void)
43 unsigned int i, num_locks;
44 struct dm_bio_prison *prison;
46 num_locks = dm_num_hash_locks();
47 prison = kzalloc(struct_size(prison, regions, num_locks), GFP_KERNEL);
50 prison->num_locks = num_locks;
52 for (i = 0; i < prison->num_locks; i++) {
53 spin_lock_init(&prison->regions[i].lock);
54 prison->regions[i].cell = RB_ROOT;
57 ret = mempool_init_slab_pool(&prison->cell_pool, MIN_CELLS, _cell_cache);
65 EXPORT_SYMBOL_GPL(dm_bio_prison_create);
67 void dm_bio_prison_destroy(struct dm_bio_prison *prison)
69 mempool_exit(&prison->cell_pool);
72 EXPORT_SYMBOL_GPL(dm_bio_prison_destroy);
74 struct dm_bio_prison_cell *dm_bio_prison_alloc_cell(struct dm_bio_prison *prison, gfp_t gfp)
76 return mempool_alloc(&prison->cell_pool, gfp);
78 EXPORT_SYMBOL_GPL(dm_bio_prison_alloc_cell);
80 void dm_bio_prison_free_cell(struct dm_bio_prison *prison,
81 struct dm_bio_prison_cell *cell)
83 mempool_free(cell, &prison->cell_pool);
85 EXPORT_SYMBOL_GPL(dm_bio_prison_free_cell);
87 static void __setup_new_cell(struct dm_cell_key *key,
89 struct dm_bio_prison_cell *cell)
91 memcpy(&cell->key, key, sizeof(cell->key));
92 cell->holder = holder;
93 bio_list_init(&cell->bios);
96 static int cmp_keys(struct dm_cell_key *lhs,
97 struct dm_cell_key *rhs)
99 if (lhs->virtual < rhs->virtual)
102 if (lhs->virtual > rhs->virtual)
105 if (lhs->dev < rhs->dev)
108 if (lhs->dev > rhs->dev)
111 if (lhs->block_end <= rhs->block_begin)
114 if (lhs->block_begin >= rhs->block_end)
120 static inline unsigned int lock_nr(struct dm_cell_key *key, unsigned int num_locks)
122 return dm_hash_locks_index((key->block_begin >> BIO_PRISON_MAX_RANGE_SHIFT),
126 bool dm_cell_key_has_valid_range(struct dm_cell_key *key)
128 if (WARN_ON_ONCE(key->block_end - key->block_begin > BIO_PRISON_MAX_RANGE))
130 if (WARN_ON_ONCE((key->block_begin >> BIO_PRISON_MAX_RANGE_SHIFT) !=
131 (key->block_end - 1) >> BIO_PRISON_MAX_RANGE_SHIFT))
136 EXPORT_SYMBOL(dm_cell_key_has_valid_range);
138 static int __bio_detain(struct rb_root *root,
139 struct dm_cell_key *key,
141 struct dm_bio_prison_cell *cell_prealloc,
142 struct dm_bio_prison_cell **cell_result)
145 struct rb_node **new = &root->rb_node, *parent = NULL;
148 struct dm_bio_prison_cell *cell =
149 rb_entry(*new, struct dm_bio_prison_cell, node);
151 r = cmp_keys(key, &cell->key);
155 new = &((*new)->rb_left);
157 new = &((*new)->rb_right);
160 bio_list_add(&cell->bios, inmate);
166 __setup_new_cell(key, inmate, cell_prealloc);
167 *cell_result = cell_prealloc;
169 rb_link_node(&cell_prealloc->node, parent, new);
170 rb_insert_color(&cell_prealloc->node, root);
175 static int bio_detain(struct dm_bio_prison *prison,
176 struct dm_cell_key *key,
178 struct dm_bio_prison_cell *cell_prealloc,
179 struct dm_bio_prison_cell **cell_result)
182 unsigned l = lock_nr(key, prison->num_locks);
184 spin_lock_irq(&prison->regions[l].lock);
185 r = __bio_detain(&prison->regions[l].cell, key, inmate, cell_prealloc, cell_result);
186 spin_unlock_irq(&prison->regions[l].lock);
191 int dm_bio_detain(struct dm_bio_prison *prison,
192 struct dm_cell_key *key,
194 struct dm_bio_prison_cell *cell_prealloc,
195 struct dm_bio_prison_cell **cell_result)
197 return bio_detain(prison, key, inmate, cell_prealloc, cell_result);
199 EXPORT_SYMBOL_GPL(dm_bio_detain);
201 int dm_get_cell(struct dm_bio_prison *prison,
202 struct dm_cell_key *key,
203 struct dm_bio_prison_cell *cell_prealloc,
204 struct dm_bio_prison_cell **cell_result)
206 return bio_detain(prison, key, NULL, cell_prealloc, cell_result);
208 EXPORT_SYMBOL_GPL(dm_get_cell);
211 * @inmates must have been initialised prior to this call
213 static void __cell_release(struct rb_root *root,
214 struct dm_bio_prison_cell *cell,
215 struct bio_list *inmates)
217 rb_erase(&cell->node, root);
221 bio_list_add(inmates, cell->holder);
222 bio_list_merge(inmates, &cell->bios);
226 void dm_cell_release(struct dm_bio_prison *prison,
227 struct dm_bio_prison_cell *cell,
228 struct bio_list *bios)
230 unsigned l = lock_nr(&cell->key, prison->num_locks);
232 spin_lock_irq(&prison->regions[l].lock);
233 __cell_release(&prison->regions[l].cell, cell, bios);
234 spin_unlock_irq(&prison->regions[l].lock);
236 EXPORT_SYMBOL_GPL(dm_cell_release);
239 * Sometimes we don't want the holder, just the additional bios.
241 static void __cell_release_no_holder(struct rb_root *root,
242 struct dm_bio_prison_cell *cell,
243 struct bio_list *inmates)
245 rb_erase(&cell->node, root);
246 bio_list_merge(inmates, &cell->bios);
249 void dm_cell_release_no_holder(struct dm_bio_prison *prison,
250 struct dm_bio_prison_cell *cell,
251 struct bio_list *inmates)
253 unsigned l = lock_nr(&cell->key, prison->num_locks);
256 spin_lock_irqsave(&prison->regions[l].lock, flags);
257 __cell_release_no_holder(&prison->regions[l].cell, cell, inmates);
258 spin_unlock_irqrestore(&prison->regions[l].lock, flags);
260 EXPORT_SYMBOL_GPL(dm_cell_release_no_holder);
262 void dm_cell_error(struct dm_bio_prison *prison,
263 struct dm_bio_prison_cell *cell, blk_status_t error)
265 struct bio_list bios;
268 bio_list_init(&bios);
269 dm_cell_release(prison, cell, &bios);
271 while ((bio = bio_list_pop(&bios))) {
272 bio->bi_status = error;
276 EXPORT_SYMBOL_GPL(dm_cell_error);
278 void dm_cell_visit_release(struct dm_bio_prison *prison,
279 void (*visit_fn)(void *, struct dm_bio_prison_cell *),
281 struct dm_bio_prison_cell *cell)
283 unsigned l = lock_nr(&cell->key, prison->num_locks);
284 spin_lock_irq(&prison->regions[l].lock);
285 visit_fn(context, cell);
286 rb_erase(&cell->node, &prison->regions[l].cell);
287 spin_unlock_irq(&prison->regions[l].lock);
289 EXPORT_SYMBOL_GPL(dm_cell_visit_release);
291 static int __promote_or_release(struct rb_root *root,
292 struct dm_bio_prison_cell *cell)
294 if (bio_list_empty(&cell->bios)) {
295 rb_erase(&cell->node, root);
299 cell->holder = bio_list_pop(&cell->bios);
303 int dm_cell_promote_or_release(struct dm_bio_prison *prison,
304 struct dm_bio_prison_cell *cell)
307 unsigned l = lock_nr(&cell->key, prison->num_locks);
309 spin_lock_irq(&prison->regions[l].lock);
310 r = __promote_or_release(&prison->regions[l].cell, cell);
311 spin_unlock_irq(&prison->regions[l].lock);
315 EXPORT_SYMBOL_GPL(dm_cell_promote_or_release);
317 /*----------------------------------------------------------------*/
319 #define DEFERRED_SET_SIZE 64
321 struct dm_deferred_entry {
322 struct dm_deferred_set *ds;
324 struct list_head work_items;
327 struct dm_deferred_set {
329 unsigned int current_entry;
330 unsigned int sweeper;
331 struct dm_deferred_entry entries[DEFERRED_SET_SIZE];
334 struct dm_deferred_set *dm_deferred_set_create(void)
337 struct dm_deferred_set *ds;
339 ds = kmalloc(sizeof(*ds), GFP_KERNEL);
343 spin_lock_init(&ds->lock);
344 ds->current_entry = 0;
346 for (i = 0; i < DEFERRED_SET_SIZE; i++) {
347 ds->entries[i].ds = ds;
348 ds->entries[i].count = 0;
349 INIT_LIST_HEAD(&ds->entries[i].work_items);
354 EXPORT_SYMBOL_GPL(dm_deferred_set_create);
356 void dm_deferred_set_destroy(struct dm_deferred_set *ds)
360 EXPORT_SYMBOL_GPL(dm_deferred_set_destroy);
362 struct dm_deferred_entry *dm_deferred_entry_inc(struct dm_deferred_set *ds)
365 struct dm_deferred_entry *entry;
367 spin_lock_irqsave(&ds->lock, flags);
368 entry = ds->entries + ds->current_entry;
370 spin_unlock_irqrestore(&ds->lock, flags);
374 EXPORT_SYMBOL_GPL(dm_deferred_entry_inc);
376 static unsigned int ds_next(unsigned int index)
378 return (index + 1) % DEFERRED_SET_SIZE;
381 static void __sweep(struct dm_deferred_set *ds, struct list_head *head)
383 while ((ds->sweeper != ds->current_entry) &&
384 !ds->entries[ds->sweeper].count) {
385 list_splice_init(&ds->entries[ds->sweeper].work_items, head);
386 ds->sweeper = ds_next(ds->sweeper);
389 if ((ds->sweeper == ds->current_entry) && !ds->entries[ds->sweeper].count)
390 list_splice_init(&ds->entries[ds->sweeper].work_items, head);
393 void dm_deferred_entry_dec(struct dm_deferred_entry *entry, struct list_head *head)
397 spin_lock_irqsave(&entry->ds->lock, flags);
398 BUG_ON(!entry->count);
400 __sweep(entry->ds, head);
401 spin_unlock_irqrestore(&entry->ds->lock, flags);
403 EXPORT_SYMBOL_GPL(dm_deferred_entry_dec);
406 * Returns 1 if deferred or 0 if no pending items to delay job.
408 int dm_deferred_set_add_work(struct dm_deferred_set *ds, struct list_head *work)
411 unsigned int next_entry;
413 spin_lock_irq(&ds->lock);
414 if ((ds->sweeper == ds->current_entry) &&
415 !ds->entries[ds->current_entry].count)
418 list_add(work, &ds->entries[ds->current_entry].work_items);
419 next_entry = ds_next(ds->current_entry);
420 if (!ds->entries[next_entry].count)
421 ds->current_entry = next_entry;
423 spin_unlock_irq(&ds->lock);
427 EXPORT_SYMBOL_GPL(dm_deferred_set_add_work);
429 /*----------------------------------------------------------------*/
431 static int __init dm_bio_prison_init_v1(void)
433 _cell_cache = KMEM_CACHE(dm_bio_prison_cell, 0);
440 static void dm_bio_prison_exit_v1(void)
442 kmem_cache_destroy(_cell_cache);
446 static int (*_inits[])(void) __initdata = {
447 dm_bio_prison_init_v1,
448 dm_bio_prison_init_v2,
451 static void (*_exits[])(void) = {
452 dm_bio_prison_exit_v1,
453 dm_bio_prison_exit_v2,
456 static int __init dm_bio_prison_init(void)
458 const int count = ARRAY_SIZE(_inits);
462 for (i = 0; i < count; i++) {
477 static void __exit dm_bio_prison_exit(void)
479 int i = ARRAY_SIZE(_exits);
488 module_init(dm_bio_prison_init);
489 module_exit(dm_bio_prison_exit);
491 MODULE_DESCRIPTION(DM_NAME " bio prison");
492 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
493 MODULE_LICENSE("GPL");