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>
This commit is contained in:
		
					parent
					
						
							
								e90abc8ec3
							
						
					
				
			
			
				commit
				
					
						196d38bccf
					
				
			
		
					 9 changed files with 90 additions and 11 deletions
				
			
		| 
						 | 
					@ -133,7 +133,7 @@ static void bch_bio_submit_split_done(struct closure *cl)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	s->bio->bi_end_io = s->bi_end_io;
 | 
						s->bio->bi_end_io = s->bi_end_io;
 | 
				
			||||||
	s->bio->bi_private = s->bi_private;
 | 
						s->bio->bi_private = s->bi_private;
 | 
				
			||||||
	bio_endio(s->bio, 0);
 | 
						bio_endio_nodec(s->bio, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	closure_debug_destroy(&s->cl);
 | 
						closure_debug_destroy(&s->cl);
 | 
				
			||||||
	mempool_free(s, s->p->bio_split_hook);
 | 
						mempool_free(s, s->p->bio_split_hook);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -765,6 +765,12 @@ static void writethrough_endio(struct bio *bio, int err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	dm_unhook_bio(&pb->hook_info, bio);
 | 
						dm_unhook_bio(&pb->hook_info, bio);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Must bump bi_remaining to allow bio to complete with
 | 
				
			||||||
 | 
						 * restored bi_end_io.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						atomic_inc(&bio->bi_remaining);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (err) {
 | 
						if (err) {
 | 
				
			||||||
		bio_endio(bio, err);
 | 
							bio_endio(bio, err);
 | 
				
			||||||
		return;
 | 
							return;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1415,6 +1415,7 @@ out:
 | 
				
			||||||
	if (full_bio) {
 | 
						if (full_bio) {
 | 
				
			||||||
		full_bio->bi_end_io = pe->full_bio_end_io;
 | 
							full_bio->bi_end_io = pe->full_bio_end_io;
 | 
				
			||||||
		full_bio->bi_private = pe->full_bio_private;
 | 
							full_bio->bi_private = pe->full_bio_private;
 | 
				
			||||||
 | 
							atomic_inc(&full_bio->bi_remaining);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	free_pending_exception(pe);
 | 
						free_pending_exception(pe);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -611,8 +611,10 @@ static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *c
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
 | 
					static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (m->bio)
 | 
						if (m->bio) {
 | 
				
			||||||
		m->bio->bi_end_io = m->saved_bi_end_io;
 | 
							m->bio->bi_end_io = m->saved_bi_end_io;
 | 
				
			||||||
 | 
							atomic_inc(&m->bio->bi_remaining);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	cell_error(m->tc->pool, m->cell);
 | 
						cell_error(m->tc->pool, m->cell);
 | 
				
			||||||
	list_del(&m->list);
 | 
						list_del(&m->list);
 | 
				
			||||||
	mempool_free(m, m->tc->pool->mapping_pool);
 | 
						mempool_free(m, m->tc->pool->mapping_pool);
 | 
				
			||||||
| 
						 | 
					@ -626,8 +628,10 @@ static void process_prepared_mapping(struct dm_thin_new_mapping *m)
 | 
				
			||||||
	int r;
 | 
						int r;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bio = m->bio;
 | 
						bio = m->bio;
 | 
				
			||||||
	if (bio)
 | 
						if (bio) {
 | 
				
			||||||
		bio->bi_end_io = m->saved_bi_end_io;
 | 
							bio->bi_end_io = m->saved_bi_end_io;
 | 
				
			||||||
 | 
							atomic_inc(&bio->bi_remaining);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (m->err) {
 | 
						if (m->err) {
 | 
				
			||||||
		cell_error(pool, m->cell);
 | 
							cell_error(pool, m->cell);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -385,7 +385,7 @@ static void verity_finish_io(struct dm_verity_io *io, int error)
 | 
				
			||||||
	bio->bi_end_io = io->orig_bi_end_io;
 | 
						bio->bi_end_io = io->orig_bi_end_io;
 | 
				
			||||||
	bio->bi_private = io->orig_bi_private;
 | 
						bio->bi_private = io->orig_bi_private;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bio_endio(bio, error);
 | 
						bio_endio_nodec(bio, error);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void verity_work(struct work_struct *w)
 | 
					static void verity_work(struct work_struct *w)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -502,7 +502,7 @@ static void bio_integrity_verify_fn(struct work_struct *work)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Restore original bio completion handler */
 | 
						/* Restore original bio completion handler */
 | 
				
			||||||
	bio->bi_end_io = bip->bip_end_io;
 | 
						bio->bi_end_io = bip->bip_end_io;
 | 
				
			||||||
	bio_endio(bio, error);
 | 
						bio_endio_nodec(bio, error);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										64
									
								
								fs/bio.c
									
										
									
									
									
								
							
							
						
						
									
										64
									
								
								fs/bio.c
									
										
									
									
									
								
							| 
						 | 
					@ -273,6 +273,7 @@ void bio_init(struct bio *bio)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	memset(bio, 0, sizeof(*bio));
 | 
						memset(bio, 0, sizeof(*bio));
 | 
				
			||||||
	bio->bi_flags = 1 << BIO_UPTODATE;
 | 
						bio->bi_flags = 1 << BIO_UPTODATE;
 | 
				
			||||||
 | 
						atomic_set(&bio->bi_remaining, 1);
 | 
				
			||||||
	atomic_set(&bio->bi_cnt, 1);
 | 
						atomic_set(&bio->bi_cnt, 1);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
EXPORT_SYMBOL(bio_init);
 | 
					EXPORT_SYMBOL(bio_init);
 | 
				
			||||||
| 
						 | 
					@ -295,9 +296,35 @@ void bio_reset(struct bio *bio)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	memset(bio, 0, BIO_RESET_BYTES);
 | 
						memset(bio, 0, BIO_RESET_BYTES);
 | 
				
			||||||
	bio->bi_flags = flags|(1 << BIO_UPTODATE);
 | 
						bio->bi_flags = flags|(1 << BIO_UPTODATE);
 | 
				
			||||||
 | 
						atomic_set(&bio->bi_remaining, 1);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
EXPORT_SYMBOL(bio_reset);
 | 
					EXPORT_SYMBOL(bio_reset);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void bio_chain_endio(struct bio *bio, int error)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						bio_endio(bio->bi_private, error);
 | 
				
			||||||
 | 
						bio_put(bio);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * bio_chain - chain bio completions
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * The caller won't have a bi_end_io called when @bio completes - instead,
 | 
				
			||||||
 | 
					 * @parent's bi_end_io won't be called until both @parent and @bio have
 | 
				
			||||||
 | 
					 * completed; the chained bio will also be freed when it completes.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * The caller must not set bi_private or bi_end_io in @bio.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					void bio_chain(struct bio *bio, struct bio *parent)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						BUG_ON(bio->bi_private || bio->bi_end_io);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						bio->bi_private = parent;
 | 
				
			||||||
 | 
						bio->bi_end_io	= bio_chain_endio;
 | 
				
			||||||
 | 
						atomic_inc(&parent->bi_remaining);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					EXPORT_SYMBOL(bio_chain);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void bio_alloc_rescue(struct work_struct *work)
 | 
					static void bio_alloc_rescue(struct work_struct *work)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
 | 
						struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
 | 
				
			||||||
| 
						 | 
					@ -1719,16 +1746,53 @@ EXPORT_SYMBOL(bio_flush_dcache_pages);
 | 
				
			||||||
 **/
 | 
					 **/
 | 
				
			||||||
void bio_endio(struct bio *bio, int error)
 | 
					void bio_endio(struct bio *bio, int error)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
						while (bio) {
 | 
				
			||||||
 | 
							BUG_ON(atomic_read(&bio->bi_remaining) <= 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (error)
 | 
							if (error)
 | 
				
			||||||
			clear_bit(BIO_UPTODATE, &bio->bi_flags);
 | 
								clear_bit(BIO_UPTODATE, &bio->bi_flags);
 | 
				
			||||||
		else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
 | 
							else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
 | 
				
			||||||
			error = -EIO;
 | 
								error = -EIO;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (!atomic_dec_and_test(&bio->bi_remaining))
 | 
				
			||||||
 | 
								return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							/*
 | 
				
			||||||
 | 
							 * Need to have a real endio function for chained bios,
 | 
				
			||||||
 | 
							 * otherwise various corner cases will break (like stacking
 | 
				
			||||||
 | 
							 * block devices that save/restore bi_end_io) - however, we want
 | 
				
			||||||
 | 
							 * to avoid unbounded recursion and blowing the stack. Tail call
 | 
				
			||||||
 | 
							 * optimization would handle this, but compiling with frame
 | 
				
			||||||
 | 
							 * pointers also disables gcc's sibling call optimization.
 | 
				
			||||||
 | 
							 */
 | 
				
			||||||
 | 
							if (bio->bi_end_io == bio_chain_endio) {
 | 
				
			||||||
 | 
								struct bio *parent = bio->bi_private;
 | 
				
			||||||
 | 
								bio_put(bio);
 | 
				
			||||||
 | 
								bio = parent;
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
			if (bio->bi_end_io)
 | 
								if (bio->bi_end_io)
 | 
				
			||||||
				bio->bi_end_io(bio, error);
 | 
									bio->bi_end_io(bio, error);
 | 
				
			||||||
 | 
								bio = NULL;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
EXPORT_SYMBOL(bio_endio);
 | 
					EXPORT_SYMBOL(bio_endio);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * bio_endio_nodec - end I/O on a bio, without decrementing bi_remaining
 | 
				
			||||||
 | 
					 * @bio:	bio
 | 
				
			||||||
 | 
					 * @error:	error, if any
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * For code that has saved and restored bi_end_io; thing hard before using this
 | 
				
			||||||
 | 
					 * function, probably you should've cloned the entire bio.
 | 
				
			||||||
 | 
					 **/
 | 
				
			||||||
 | 
					void bio_endio_nodec(struct bio *bio, int error)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						atomic_inc(&bio->bi_remaining);
 | 
				
			||||||
 | 
						bio_endio(bio, error);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					EXPORT_SYMBOL(bio_endio_nodec);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void bio_pair_release(struct bio_pair *bp)
 | 
					void bio_pair_release(struct bio_pair *bp)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (atomic_dec_and_test(&bp->cnt)) {
 | 
						if (atomic_dec_and_test(&bp->cnt)) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -356,6 +356,7 @@ static inline struct bio *bio_clone_kmalloc(struct bio *bio, gfp_t gfp_mask)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern void bio_endio(struct bio *, int);
 | 
					extern void bio_endio(struct bio *, int);
 | 
				
			||||||
 | 
					extern void bio_endio_nodec(struct bio *, int);
 | 
				
			||||||
struct request_queue;
 | 
					struct request_queue;
 | 
				
			||||||
extern int bio_phys_segments(struct request_queue *, struct bio *);
 | 
					extern int bio_phys_segments(struct request_queue *, struct bio *);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -364,6 +365,7 @@ extern void bio_advance(struct bio *, unsigned);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern void bio_init(struct bio *);
 | 
					extern void bio_init(struct bio *);
 | 
				
			||||||
extern void bio_reset(struct bio *);
 | 
					extern void bio_reset(struct bio *);
 | 
				
			||||||
 | 
					void bio_chain(struct bio *, struct bio *);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
 | 
					extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
 | 
				
			||||||
extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
 | 
					extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -65,6 +65,8 @@ struct bio {
 | 
				
			||||||
	unsigned int		bi_seg_front_size;
 | 
						unsigned int		bi_seg_front_size;
 | 
				
			||||||
	unsigned int		bi_seg_back_size;
 | 
						unsigned int		bi_seg_back_size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						atomic_t		bi_remaining;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bio_end_io_t		*bi_end_io;
 | 
						bio_end_io_t		*bi_end_io;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void			*bi_private;
 | 
						void			*bi_private;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue