diff options
author | Kent Overstreet <kmo@daterainc.com> | 2013-11-23 21:34:15 -0500 |
---|---|---|
committer | Kent Overstreet <kmo@daterainc.com> | 2013-11-24 01:33:56 -0500 |
commit | 196d38bccfcfa32faed8c561868336fdfa0fe8e4 (patch) | |
tree | 9dee2bc174436072c9e90ffebbe71f47fde63aaf | |
parent | e90abc8ec323c1fd2a25600097ef7ae1e91f39b0 (diff) |
block: Generic bio chaining
This adds a generic mechanism for chaining bio completions. This is
going to be used for a bio_split() replacement, and it turns out to be
very useful in a fair amount of driver code - a fair number of drivers
were implementing this in their own roundabout ways, often painfully.
Note that this means it's no longer to call bio_endio() more than once
on the same bio! This can cause problems for drivers that save/restore
bi_end_io. Arguably they shouldn't be saving/restoring bi_end_io at all
- in all but the simplest cases they'd be better off just cloning the
bio, and immutable biovecs is making bio cloning cheaper. But for now,
we add a bio_endio_nodec() for these cases.
Signed-off-by: Kent Overstreet <kmo@daterainc.com>
Cc: Jens Axboe <axboe@kernel.dk>
-rw-r--r-- | drivers/md/bcache/io.c | 2 | ||||
-rw-r--r-- | drivers/md/dm-cache-target.c | 6 | ||||
-rw-r--r-- | drivers/md/dm-snap.c | 1 | ||||
-rw-r--r-- | drivers/md/dm-thin.c | 8 | ||||
-rw-r--r-- | drivers/md/dm-verity.c | 2 | ||||
-rw-r--r-- | fs/bio-integrity.c | 2 | ||||
-rw-r--r-- | fs/bio.c | 76 | ||||
-rw-r--r-- | include/linux/bio.h | 2 | ||||
-rw-r--r-- | include/linux/blk_types.h | 2 |
9 files changed, 90 insertions, 11 deletions
diff --git a/drivers/md/bcache/io.c b/drivers/md/bcache/io.c index 0f0ab659914d..522f95778443 100644 --- a/drivers/md/bcache/io.c +++ b/drivers/md/bcache/io.c | |||
@@ -133,7 +133,7 @@ static void bch_bio_submit_split_done(struct closure *cl) | |||
133 | 133 | ||
134 | s->bio->bi_end_io = s->bi_end_io; | 134 | s->bio->bi_end_io = s->bi_end_io; |
135 | s->bio->bi_private = s->bi_private; | 135 | s->bio->bi_private = s->bi_private; |
136 | bio_endio(s->bio, 0); | 136 | bio_endio_nodec(s->bio, 0); |
137 | 137 | ||
138 | closure_debug_destroy(&s->cl); | 138 | closure_debug_destroy(&s->cl); |
139 | mempool_free(s, s->p->bio_split_hook); | 139 | mempool_free(s, s->p->bio_split_hook); |
diff --git a/drivers/md/dm-cache-target.c b/drivers/md/dm-cache-target.c index 86f9c83eb30c..bf3a206abd78 100644 --- a/drivers/md/dm-cache-target.c +++ b/drivers/md/dm-cache-target.c | |||
@@ -765,6 +765,12 @@ static void writethrough_endio(struct bio *bio, int err) | |||
765 | 765 | ||
766 | dm_unhook_bio(&pb->hook_info, bio); | 766 | dm_unhook_bio(&pb->hook_info, bio); |
767 | 767 | ||
768 | /* | ||
769 | * Must bump bi_remaining to allow bio to complete with | ||
770 | * restored bi_end_io. | ||
771 | */ | ||
772 | atomic_inc(&bio->bi_remaining); | ||
773 | |||
768 | if (err) { | 774 | if (err) { |
769 | bio_endio(bio, err); | 775 | bio_endio(bio, err); |
770 | return; | 776 | return; |
diff --git a/drivers/md/dm-snap.c b/drivers/md/dm-snap.c index 3ded8c729dfb..80b5cabbea29 100644 --- a/drivers/md/dm-snap.c +++ b/drivers/md/dm-snap.c | |||
@@ -1415,6 +1415,7 @@ out: | |||
1415 | if (full_bio) { | 1415 | if (full_bio) { |
1416 | full_bio->bi_end_io = pe->full_bio_end_io; | 1416 | full_bio->bi_end_io = pe->full_bio_end_io; |
1417 | full_bio->bi_private = pe->full_bio_private; | 1417 | full_bio->bi_private = pe->full_bio_private; |
1418 | atomic_inc(&full_bio->bi_remaining); | ||
1418 | } | 1419 | } |
1419 | free_pending_exception(pe); | 1420 | free_pending_exception(pe); |
1420 | 1421 | ||
diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c index a65402480c8c..1abb4a24c338 100644 --- a/drivers/md/dm-thin.c +++ b/drivers/md/dm-thin.c | |||
@@ -611,8 +611,10 @@ static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *c | |||
611 | 611 | ||
612 | static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m) | 612 | static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m) |
613 | { | 613 | { |
614 | if (m->bio) | 614 | if (m->bio) { |
615 | m->bio->bi_end_io = m->saved_bi_end_io; | 615 | m->bio->bi_end_io = m->saved_bi_end_io; |
616 | atomic_inc(&m->bio->bi_remaining); | ||
617 | } | ||
616 | cell_error(m->tc->pool, m->cell); | 618 | cell_error(m->tc->pool, m->cell); |
617 | list_del(&m->list); | 619 | list_del(&m->list); |
618 | mempool_free(m, m->tc->pool->mapping_pool); | 620 | mempool_free(m, m->tc->pool->mapping_pool); |
@@ -626,8 +628,10 @@ static void process_prepared_mapping(struct dm_thin_new_mapping *m) | |||
626 | int r; | 628 | int r; |
627 | 629 | ||
628 | bio = m->bio; | 630 | bio = m->bio; |
629 | if (bio) | 631 | if (bio) { |
630 | bio->bi_end_io = m->saved_bi_end_io; | 632 | bio->bi_end_io = m->saved_bi_end_io; |
633 | atomic_inc(&bio->bi_remaining); | ||
634 | } | ||
631 | 635 | ||
632 | if (m->err) { | 636 | if (m->err) { |
633 | cell_error(pool, m->cell); | 637 | cell_error(pool, m->cell); |
diff --git a/drivers/md/dm-verity.c b/drivers/md/dm-verity.c index ac35e959d49b..796007a5e0e1 100644 --- a/drivers/md/dm-verity.c +++ b/drivers/md/dm-verity.c | |||
@@ -385,7 +385,7 @@ static void verity_finish_io(struct dm_verity_io *io, int error) | |||
385 | bio->bi_end_io = io->orig_bi_end_io; | 385 | bio->bi_end_io = io->orig_bi_end_io; |
386 | bio->bi_private = io->orig_bi_private; | 386 | bio->bi_private = io->orig_bi_private; |
387 | 387 | ||
388 | bio_endio(bio, error); | 388 | bio_endio_nodec(bio, error); |
389 | } | 389 | } |
390 | 390 | ||
391 | static void verity_work(struct work_struct *w) | 391 | static void verity_work(struct work_struct *w) |
diff --git a/fs/bio-integrity.c b/fs/bio-integrity.c index fed744b8c9e5..9d547d2e357c 100644 --- a/fs/bio-integrity.c +++ b/fs/bio-integrity.c | |||
@@ -502,7 +502,7 @@ static void bio_integrity_verify_fn(struct work_struct *work) | |||
502 | 502 | ||
503 | /* Restore original bio completion handler */ | 503 | /* Restore original bio completion handler */ |
504 | bio->bi_end_io = bip->bip_end_io; | 504 | bio->bi_end_io = bip->bip_end_io; |
505 | bio_endio(bio, error); | 505 | bio_endio_nodec(bio, error); |
506 | } | 506 | } |
507 | 507 | ||
508 | /** | 508 | /** |
@@ -273,6 +273,7 @@ void bio_init(struct bio *bio) | |||
273 | { | 273 | { |
274 | memset(bio, 0, sizeof(*bio)); | 274 | memset(bio, 0, sizeof(*bio)); |
275 | bio->bi_flags = 1 << BIO_UPTODATE; | 275 | bio->bi_flags = 1 << BIO_UPTODATE; |
276 | atomic_set(&bio->bi_remaining, 1); | ||
276 | atomic_set(&bio->bi_cnt, 1); | 277 | atomic_set(&bio->bi_cnt, 1); |
277 | } | 278 | } |
278 | EXPORT_SYMBOL(bio_init); | 279 | EXPORT_SYMBOL(bio_init); |
@@ -295,9 +296,35 @@ void bio_reset(struct bio *bio) | |||
295 | 296 | ||
296 | memset(bio, 0, BIO_RESET_BYTES); | 297 | memset(bio, 0, BIO_RESET_BYTES); |
297 | bio->bi_flags = flags|(1 << BIO_UPTODATE); | 298 | bio->bi_flags = flags|(1 << BIO_UPTODATE); |
299 | atomic_set(&bio->bi_remaining, 1); | ||
298 | } | 300 | } |
299 | EXPORT_SYMBOL(bio_reset); | 301 | EXPORT_SYMBOL(bio_reset); |
300 | 302 | ||
303 | static void bio_chain_endio(struct bio *bio, int error) | ||
304 | { | ||
305 | bio_endio(bio->bi_private, error); | ||
306 | bio_put(bio); | ||
307 | } | ||
308 | |||
309 | /** | ||
310 | * bio_chain - chain bio completions | ||
311 | * | ||
312 | * The caller won't have a bi_end_io called when @bio completes - instead, | ||
313 | * @parent's bi_end_io won't be called until both @parent and @bio have | ||
314 | * completed; the chained bio will also be freed when it completes. | ||
315 | * | ||
316 | * The caller must not set bi_private or bi_end_io in @bio. | ||
317 | */ | ||
318 | void bio_chain(struct bio *bio, struct bio *parent) | ||
319 | { | ||
320 | BUG_ON(bio->bi_private || bio->bi_end_io); | ||
321 | |||
322 | bio->bi_private = parent; | ||
323 | bio->bi_end_io = bio_chain_endio; | ||
324 | atomic_inc(&parent->bi_remaining); | ||
325 | } | ||
326 | EXPORT_SYMBOL(bio_chain); | ||
327 | |||
301 | static void bio_alloc_rescue(struct work_struct *work) | 328 | static void bio_alloc_rescue(struct work_struct *work) |
302 | { | 329 | { |
303 | struct bio_set *bs = container_of(work, struct bio_set, rescue_work); | 330 | struct bio_set *bs = container_of(work, struct bio_set, rescue_work); |
@@ -1719,16 +1746,53 @@ EXPORT_SYMBOL(bio_flush_dcache_pages); | |||
1719 | **/ | 1746 | **/ |
1720 | void bio_endio(struct bio *bio, int error) | 1747 | void bio_endio(struct bio *bio, int error) |
1721 | { | 1748 | { |
1722 | if (error) | 1749 | while (bio) { |
1723 | clear_bit(BIO_UPTODATE, &bio->bi_flags); | 1750 | BUG_ON(atomic_read(&bio->bi_remaining) <= 0); |
1724 | else if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) | 1751 | |
1725 | error = -EIO; | 1752 | if (error) |
1753 | clear_bit(BIO_UPTODATE, &bio->bi_flags); | ||
1754 | else if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) | ||
1755 | error = -EIO; | ||
1756 | |||
1757 | if (!atomic_dec_and_test(&bio->bi_remaining)) | ||
1758 | return; | ||
1726 | 1759 | ||
1727 | if (bio->bi_end_io) | 1760 | /* |
1728 | bio->bi_end_io(bio, error); | 1761 | * Need to have a real endio function for chained bios, |
1762 | * otherwise various corner cases will break (like stacking | ||
1763 | * block devices that save/restore bi_end_io) - however, we want | ||
1764 | * to avoid unbounded recursion and blowing the stack. Tail call | ||
1765 | * optimization would handle this, but compiling with frame | ||
1766 | * pointers also disables gcc's sibling call optimization. | ||
1767 | */ | ||
1768 | if (bio->bi_end_io == bio_chain_endio) { | ||
1769 | struct bio *parent = bio->bi_private; | ||
1770 | bio_put(bio); | ||
1771 | bio = parent; | ||
1772 | } else { | ||
1773 | if (bio->bi_end_io) | ||
1774 | bio->bi_end_io(bio, error); | ||
1775 | bio = NULL; | ||
1776 | } | ||
1777 | } | ||
1729 | } | 1778 | } |
1730 | EXPORT_SYMBOL(bio_endio); | 1779 | EXPORT_SYMBOL(bio_endio); |
1731 | 1780 | ||
1781 | /** | ||
1782 | * bio_endio_nodec - end I/O on a bio, without decrementing bi_remaining | ||
1783 | * @bio: bio | ||
1784 | * @error: error, if any | ||
1785 | * | ||
1786 | * For code that has saved and restored bi_end_io; thing hard before using this | ||
1787 | * function, probably you should've cloned the entire bio. | ||
1788 | **/ | ||
1789 | void bio_endio_nodec(struct bio *bio, int error) | ||
1790 | { | ||
1791 | atomic_inc(&bio->bi_remaining); | ||
1792 | bio_endio(bio, error); | ||
1793 | } | ||
1794 | EXPORT_SYMBOL(bio_endio_nodec); | ||
1795 | |||
1732 | void bio_pair_release(struct bio_pair *bp) | 1796 | void bio_pair_release(struct bio_pair *bp) |
1733 | { | 1797 | { |
1734 | if (atomic_dec_and_test(&bp->cnt)) { | 1798 | if (atomic_dec_and_test(&bp->cnt)) { |
diff --git a/include/linux/bio.h b/include/linux/bio.h index 0c32a45a419c..64f5169c224b 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h | |||
@@ -356,6 +356,7 @@ static inline struct bio *bio_clone_kmalloc(struct bio *bio, gfp_t gfp_mask) | |||
356 | } | 356 | } |
357 | 357 | ||
358 | extern void bio_endio(struct bio *, int); | 358 | extern void bio_endio(struct bio *, int); |
359 | extern void bio_endio_nodec(struct bio *, int); | ||
359 | struct request_queue; | 360 | struct request_queue; |
360 | extern int bio_phys_segments(struct request_queue *, struct bio *); | 361 | extern int bio_phys_segments(struct request_queue *, struct bio *); |
361 | 362 | ||
@@ -364,6 +365,7 @@ extern void bio_advance(struct bio *, unsigned); | |||
364 | 365 | ||
365 | extern void bio_init(struct bio *); | 366 | extern void bio_init(struct bio *); |
366 | extern void bio_reset(struct bio *); | 367 | extern void bio_reset(struct bio *); |
368 | void bio_chain(struct bio *, struct bio *); | ||
367 | 369 | ||
368 | extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int); | 370 | extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int); |
369 | extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *, | 371 | extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *, |
diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h index d369f8f6af79..bbc3a6c88fce 100644 --- a/include/linux/blk_types.h +++ b/include/linux/blk_types.h | |||
@@ -65,6 +65,8 @@ struct bio { | |||
65 | unsigned int bi_seg_front_size; | 65 | unsigned int bi_seg_front_size; |
66 | unsigned int bi_seg_back_size; | 66 | unsigned int bi_seg_back_size; |
67 | 67 | ||
68 | atomic_t bi_remaining; | ||
69 | |||
68 | bio_end_io_t *bi_end_io; | 70 | bio_end_io_t *bi_end_io; |
69 | 71 | ||
70 | void *bi_private; | 72 | void *bi_private; |