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
[] __counted_by(num_locks
);
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
);
202 * @inmates must have been initialised prior to this call
204 static void __cell_release(struct rb_root
*root
,
205 struct dm_bio_prison_cell
*cell
,
206 struct bio_list
*inmates
)
208 rb_erase(&cell
->node
, root
);
212 bio_list_add(inmates
, cell
->holder
);
213 bio_list_merge(inmates
, &cell
->bios
);
217 void dm_cell_release(struct dm_bio_prison
*prison
,
218 struct dm_bio_prison_cell
*cell
,
219 struct bio_list
*bios
)
221 unsigned l
= lock_nr(&cell
->key
, prison
->num_locks
);
223 spin_lock_irq(&prison
->regions
[l
].lock
);
224 __cell_release(&prison
->regions
[l
].cell
, cell
, bios
);
225 spin_unlock_irq(&prison
->regions
[l
].lock
);
227 EXPORT_SYMBOL_GPL(dm_cell_release
);
230 * Sometimes we don't want the holder, just the additional bios.
232 static void __cell_release_no_holder(struct rb_root
*root
,
233 struct dm_bio_prison_cell
*cell
,
234 struct bio_list
*inmates
)
236 rb_erase(&cell
->node
, root
);
237 bio_list_merge(inmates
, &cell
->bios
);
240 void dm_cell_release_no_holder(struct dm_bio_prison
*prison
,
241 struct dm_bio_prison_cell
*cell
,
242 struct bio_list
*inmates
)
244 unsigned l
= lock_nr(&cell
->key
, prison
->num_locks
);
247 spin_lock_irqsave(&prison
->regions
[l
].lock
, flags
);
248 __cell_release_no_holder(&prison
->regions
[l
].cell
, cell
, inmates
);
249 spin_unlock_irqrestore(&prison
->regions
[l
].lock
, flags
);
251 EXPORT_SYMBOL_GPL(dm_cell_release_no_holder
);
253 void dm_cell_error(struct dm_bio_prison
*prison
,
254 struct dm_bio_prison_cell
*cell
, blk_status_t error
)
256 struct bio_list bios
;
259 bio_list_init(&bios
);
260 dm_cell_release(prison
, cell
, &bios
);
262 while ((bio
= bio_list_pop(&bios
))) {
263 bio
->bi_status
= error
;
267 EXPORT_SYMBOL_GPL(dm_cell_error
);
269 void dm_cell_visit_release(struct dm_bio_prison
*prison
,
270 void (*visit_fn
)(void *, struct dm_bio_prison_cell
*),
272 struct dm_bio_prison_cell
*cell
)
274 unsigned l
= lock_nr(&cell
->key
, prison
->num_locks
);
275 spin_lock_irq(&prison
->regions
[l
].lock
);
276 visit_fn(context
, cell
);
277 rb_erase(&cell
->node
, &prison
->regions
[l
].cell
);
278 spin_unlock_irq(&prison
->regions
[l
].lock
);
280 EXPORT_SYMBOL_GPL(dm_cell_visit_release
);
282 /*----------------------------------------------------------------*/
284 #define DEFERRED_SET_SIZE 64
286 struct dm_deferred_entry
{
287 struct dm_deferred_set
*ds
;
289 struct list_head work_items
;
292 struct dm_deferred_set
{
294 unsigned int current_entry
;
295 unsigned int sweeper
;
296 struct dm_deferred_entry entries
[DEFERRED_SET_SIZE
];
299 struct dm_deferred_set
*dm_deferred_set_create(void)
302 struct dm_deferred_set
*ds
;
304 ds
= kmalloc(sizeof(*ds
), GFP_KERNEL
);
308 spin_lock_init(&ds
->lock
);
309 ds
->current_entry
= 0;
311 for (i
= 0; i
< DEFERRED_SET_SIZE
; i
++) {
312 ds
->entries
[i
].ds
= ds
;
313 ds
->entries
[i
].count
= 0;
314 INIT_LIST_HEAD(&ds
->entries
[i
].work_items
);
319 EXPORT_SYMBOL_GPL(dm_deferred_set_create
);
321 void dm_deferred_set_destroy(struct dm_deferred_set
*ds
)
325 EXPORT_SYMBOL_GPL(dm_deferred_set_destroy
);
327 struct dm_deferred_entry
*dm_deferred_entry_inc(struct dm_deferred_set
*ds
)
330 struct dm_deferred_entry
*entry
;
332 spin_lock_irqsave(&ds
->lock
, flags
);
333 entry
= ds
->entries
+ ds
->current_entry
;
335 spin_unlock_irqrestore(&ds
->lock
, flags
);
339 EXPORT_SYMBOL_GPL(dm_deferred_entry_inc
);
341 static unsigned int ds_next(unsigned int index
)
343 return (index
+ 1) % DEFERRED_SET_SIZE
;
346 static void __sweep(struct dm_deferred_set
*ds
, struct list_head
*head
)
348 while ((ds
->sweeper
!= ds
->current_entry
) &&
349 !ds
->entries
[ds
->sweeper
].count
) {
350 list_splice_init(&ds
->entries
[ds
->sweeper
].work_items
, head
);
351 ds
->sweeper
= ds_next(ds
->sweeper
);
354 if ((ds
->sweeper
== ds
->current_entry
) && !ds
->entries
[ds
->sweeper
].count
)
355 list_splice_init(&ds
->entries
[ds
->sweeper
].work_items
, head
);
358 void dm_deferred_entry_dec(struct dm_deferred_entry
*entry
, struct list_head
*head
)
362 spin_lock_irqsave(&entry
->ds
->lock
, flags
);
363 BUG_ON(!entry
->count
);
365 __sweep(entry
->ds
, head
);
366 spin_unlock_irqrestore(&entry
->ds
->lock
, flags
);
368 EXPORT_SYMBOL_GPL(dm_deferred_entry_dec
);
371 * Returns 1 if deferred or 0 if no pending items to delay job.
373 int dm_deferred_set_add_work(struct dm_deferred_set
*ds
, struct list_head
*work
)
376 unsigned int next_entry
;
378 spin_lock_irq(&ds
->lock
);
379 if ((ds
->sweeper
== ds
->current_entry
) &&
380 !ds
->entries
[ds
->current_entry
].count
)
383 list_add(work
, &ds
->entries
[ds
->current_entry
].work_items
);
384 next_entry
= ds_next(ds
->current_entry
);
385 if (!ds
->entries
[next_entry
].count
)
386 ds
->current_entry
= next_entry
;
388 spin_unlock_irq(&ds
->lock
);
392 EXPORT_SYMBOL_GPL(dm_deferred_set_add_work
);
394 /*----------------------------------------------------------------*/
396 static int __init
dm_bio_prison_init_v1(void)
398 _cell_cache
= KMEM_CACHE(dm_bio_prison_cell
, 0);
405 static void dm_bio_prison_exit_v1(void)
407 kmem_cache_destroy(_cell_cache
);
411 static int (*_inits
[])(void) __initdata
= {
412 dm_bio_prison_init_v1
,
413 dm_bio_prison_init_v2
,
416 static void (*_exits
[])(void) = {
417 dm_bio_prison_exit_v1
,
418 dm_bio_prison_exit_v2
,
421 static int __init
dm_bio_prison_init(void)
423 const int count
= ARRAY_SIZE(_inits
);
427 for (i
= 0; i
< count
; i
++) {
442 static void __exit
dm_bio_prison_exit(void)
444 int i
= ARRAY_SIZE(_exits
);
453 module_init(dm_bio_prison_init
);
454 module_exit(dm_bio_prison_exit
);
456 MODULE_DESCRIPTION(DM_NAME
" bio prison");
457 MODULE_AUTHOR("Joe Thornber <dm-devel@lists.linux.dev>");
458 MODULE_LICENSE("GPL");