Skip to contentSkip to conversation

block: remove management of bi_remaining when restoring original bi_end_io

PATCH In review Mike Snitzer 3 participants 14 messages May 22, 2015 – Jun 3, 2015

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 block: remove management of bi_remaining when restoring original bi_end_io

Commit c4cf5261 ("bio: skip atomic inc/dec of ->bi_remaining for non-chains") regressed all existing callers that followed this pattern:
1) saving a bio's original bi_end_io
2) wiring up an intermediate bi_end_io
3) restoring the original bi_end_io from intermediate bi_end_io
4) calling bio_endio() to execute the restored original bi_end_io

The regression was due to BIO_CHAIN only ever getting set if bio_inc_remaining() is called. For the above pattern it isn't set until step 3 above (step 2 would've needed to establish BIO_CHAIN). As such the first bio_endio(), in step 2 above, never decremented __bi_remaining before calling the intermediate bi_end_io -- leaving __bi_remaining with the value 1 instead of 0. When bio_inc_remaining() occurred during step 3 it brought it to a value of 2. When the second bio_endio() was called, in step 4 above, it should've called the original bi_end_io but it didn't because there was an extra reference that wasn't dropped (due to atomic operations being optimized away since BIO_CHAIN wasn't set upfront).

Fix this issue by removing the __bi_remaining management complexity for all callers that use the above pattern -- bio_chain() is the only interface that _needs_ to be concerned with __bi_remaining. For the above pattern callers just expect the bi_end_io they set to get called! Remove bio_endio_nodec() and also remove all bio_inc_remaining() calls that aren't associated with the bio_chain() interface.

Also, the bio_inc_remaining() interface has been moved local to bio.c.

Fixes c4cf5261 ("bio: skip atomic inc/dec of ->bi_remaining for non-chains")
---
 block/bio-integrity.c        |  4 ++--
 block/bio.c                  | 35 ++++++++++++++---------------------
 drivers/md/bcache/io.c       |  2 +-
 drivers/md/dm-cache-target.c |  6 ------
 drivers/md/dm-raid1.c        |  2 --
 drivers/md/dm-snap.c         |  1 -
 drivers/md/dm-thin.c         |  9 +++------
 drivers/md/dm-verity.c       |  2 +-
 fs/btrfs/disk-io.c           |  2 +-
 fs/btrfs/volumes.c           | 16 +++++-----------
 fs/btrfs/volumes.h           |  2 --
 include/linux/bio.h          | 12 ------------
 12 files changed, 27 insertions(+), 66 deletions(-)
block/bio-integrity.c +2 -2
index 5cbd5d9..0436c21 100644
@@ -361,7 +361,7 @@ static void bio_integrity_verify_fn(struct work_struct *work)
 
 	/* Restore original bio completion handler */
 	bio->bi_end_io = bip->bip_end_io;
-	bio_endio_nodec(bio, error);
+	bio_endio(bio, error);
 }
 
 /**
@@ -388,7 +388,7 @@ void bio_integrity_endio(struct bio *bio, int error)
 	 */
 	if (error) {
 		bio->bi_end_io = bip->bip_end_io;
-		bio_endio_nodec(bio, error);
+		bio_endio(bio, error);
 
 		return;
 	}
block/bio.c +14 -21
index c2ff8a8..259197d 100644
@@ -303,6 +303,17 @@ static void bio_chain_endio(struct bio *bio, int error)
 	bio_put(bio);
 }
 
+/*
+ * Increment chain count for the bio. Make sure the CHAIN flag update
+ * is visible before the raised count.
+ */
+static inline void bio_inc_remaining(struct bio *bio)
+{
+	bio->bi_flags |= (1 << BIO_CHAIN);
+	smp_mb__before_atomic();
+	atomic_inc(&bio->__bi_remaining);
+}
+
 /**
  * bio_chain - chain bio completions
  * @bio: the target bio
@@ -1756,8 +1767,10 @@ static inline bool bio_remaining_done(struct bio *bio)
 
 	BUG_ON(atomic_read(&bio->__bi_remaining) <= 0);
 
-	if (atomic_dec_and_test(&bio->__bi_remaining))
+	if (atomic_dec_and_test(&bio->__bi_remaining)) {
+		clear_bit(BIO_CHAIN, &bio->bi_flags);
 		return true;
+	}
 
 	return false;
 }
@@ -1809,26 +1822,6 @@ void bio_endio(struct bio *bio, int error)
 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)
-{
-	/*
-	 * If it's not flagged as a chain, we are not going to dec the count
-	 */
-	if (bio_flagged(bio, BIO_CHAIN))
-		bio_inc_remaining(bio);
-
-	bio_endio(bio, error);
-}
-EXPORT_SYMBOL(bio_endio_nodec);
-
-/**
  * bio_split - split a bio
  * @bio:	bio to split
  * @sectors:	number of sectors to split from the front of @bio
drivers/md/bcache/io.c +1 -1
index fa028fa..cb64e64a 100644
@@ -55,7 +55,7 @@ static void bch_bio_submit_split_done(struct closure *cl)
 
 	s->bio->bi_end_io = s->bi_end_io;
 	s->bio->bi_private = s->bi_private;
-	bio_endio_nodec(s->bio, 0);
+	bio_endio(s->bio, 0);
 
 	closure_debug_destroy(&s->cl);
 	mempool_free(s, s->p->bio_split_hook);
drivers/md/dm-cache-target.c +0 -6
index 705eb7b..41b2594 100644
@@ -86,12 +86,6 @@ static void dm_unhook_bio(struct dm_hook_info *h, struct bio *bio)
 {
 	bio->bi_end_io = h->bi_end_io;
 	bio->bi_private = h->bi_private;
-
-	/*
-	 * Must bump bi_remaining to allow bio to complete with
-	 * restored bi_end_io.
-	 */
-	bio_inc_remaining(bio);
 }
 
 /*----------------------------------------------------------------*/
drivers/md/dm-raid1.c +0 -2
index d6a1c09..743fa9b 100644
@@ -1254,8 +1254,6 @@ static int mirror_end_io(struct dm_target *ti, struct bio *bio, int error)
 			dm_bio_restore(bd, bio);
 			bio_record->details.bi_bdev = NULL;
 
-			bio_inc_remaining(bio);
-
 			queue_bio(ms, bio, rw);
 			return DM_ENDIO_INCOMPLETE;
 		}
drivers/md/dm-snap.c +0 -1
index 8bfeae2..7c82d3c 100644
@@ -1478,7 +1478,6 @@ out:
 	if (full_bio) {
 		full_bio->bi_end_io = pe->full_bio_end_io;
 		full_bio->bi_private = pe->full_bio_private;
-		bio_inc_remaining(full_bio);
 	}
 	increment_pending_exceptions_done_count();
 
drivers/md/dm-thin.c +3 -6
index 342dbda..e852602c 100644
@@ -793,10 +793,9 @@ static void inc_remap_and_issue_cell(struct thin_c *tc,
 
 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;
-		bio_inc_remaining(m->bio);
-	}
+
 	cell_error(m->tc->pool, m->cell);
 	list_del(&m->list);
 	mempool_free(m, m->tc->pool->mapping_pool);
@@ -810,10 +809,8 @@ static void process_prepared_mapping(struct dm_thin_new_mapping *m)
 	int r;
 
 	bio = m->bio;
-	if (bio) {
+	if (bio)
 		bio->bi_end_io = m->saved_bi_end_io;
-		bio_inc_remaining(bio);
-	}
 
 	if (m->err) {
 		cell_error(pool, m->cell);
drivers/md/dm-verity.c +1 -1
index 66616db..bb9c6a0 100644
@@ -459,7 +459,7 @@ static void verity_finish_io(struct dm_verity_io *io, int error)
 	bio->bi_end_io = io->orig_bi_end_io;
 	bio->bi_private = io->orig_bi_private;
 
-	bio_endio_nodec(bio, error);
+	bio_endio(bio, error);
 }
 
 static void verity_work(struct work_struct *w)
fs/btrfs/disk-io.c +1 -1
index e08a926..0bccf18 100644
@@ -1745,7 +1745,7 @@ static void end_workqueue_fn(struct btrfs_work *work)
 	bio->bi_private = end_io_wq->private;
 	bio->bi_end_io = end_io_wq->end_io;
 	kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq);
-	bio_endio_nodec(bio, error);
+	bio_endio(bio, error);
 }
 
 static int cleaner_kthread(void *arg)
fs/btrfs/volumes.c +5 -11
index 8e8d1d1..dac77d4 100644
@@ -5585,10 +5585,10 @@ int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
 
 static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
 {
-	if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
-		bio_endio_nodec(bio, err);
-	else
-		bio_endio(bio, err);
+	bio->bi_private = bbio->private;
+	bio->bi_end_io = bbio->end_io;
+	bio_endio(bio, err);
+
 	btrfs_put_bbio(bbio);
 }
 
@@ -5632,8 +5632,6 @@ static void btrfs_end_bio(struct bio *bio, int err)
 			bio = bbio->orig_bio;
 		}
 
-		bio->bi_private = bbio->private;
-		bio->bi_end_io = bbio->end_io;
 		btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
 		/* only send an error to the higher layers if it is
 		 * beyond the tolerance of the btrfs bio
@@ -5815,8 +5813,6 @@ static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
 		/* Shoud be the original bio. */
 		WARN_ON(bio != bbio->orig_bio);
 
-		bio->bi_private = bbio->private;
-		bio->bi_end_io = bbio->end_io;
 		btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
 		bio->bi_iter.bi_sector = logical >> 9;
 
@@ -5897,10 +5893,8 @@ int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
 		if (dev_nr < total_devs - 1) {
 			bio = btrfs_bio_clone(first_bio, GFP_NOFS);
 			BUG_ON(!bio); /* -ENOMEM */
-		} else {
+		} else
 			bio = first_bio;
-			bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
-		}
 
 		submit_stripe_bio(root, bbio, bio,
 				  bbio->stripes[dev_nr].physical, dev_nr, rw,
fs/btrfs/volumes.h +0 -2
index ebc3133..cedae03 100644
@@ -292,8 +292,6 @@ struct btrfs_bio_stripe {
 struct btrfs_bio;
 typedef void (btrfs_bio_end_io_t) (struct btrfs_bio *bio, int err);
 
-#define BTRFS_BIO_ORIG_BIO_SUBMITTED	(1 << 0)
-
 struct btrfs_bio {
 	atomic_t refs;
 	atomic_t stripes_pending;
include/linux/bio.h +0 -12
index 7486ea1..f0291cf 100644
@@ -427,7 +427,6 @@ 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_nodec(struct bio *, int);
 struct request_queue;
 extern int bio_phys_segments(struct request_queue *, struct bio *);
 
@@ -659,17 +658,6 @@ static inline struct bio *bio_list_get(struct bio_list *bl)
 }
 
 /*
- * Increment chain count for the bio. Make sure the CHAIN flag update
- * is visible before the raised count.
- */
-static inline void bio_inc_remaining(struct bio *bio)
-{
-	bio->bi_flags |= (1 << BIO_CHAIN);
-	smp_mb__before_atomic();
-	atomic_inc(&bio->__bi_remaining);
-}
-
-/*
  * bio_set is used to allow other portions of the IO system to
  * allocate their own private memory pools for bio and iovec structures.
  * These memory pools in turn all allocate from the bio_slab
Signature · 1 line
2.3.2 (Apple Git-55)
#1 lore raw

PATCH 2/3 block, dm: don't copy bios for request clones

From: Christoph Hellwig <[email protected]>

Currently dm-multipath has to clone the bios for every request sent to the lower devices, which wastes cpu cycles and ties down memory.

This patch instead adds a new REQ_CLONE flag that instructs req_bio_endio to not complete bios attached to a request, which we set on clone requests similar to bios in a flush sequence. With this change I/O errors on a path failure only get propagated to dm-multipath, which can then either resubmit the I/O or complete the bios on the original request.

I've done some basic testing of this on a Linux target with ALUA support, and it survives path failures during I/O nicely.

---
 block/blk-core.c          |  94 +++----------------------
 drivers/md/dm-table.c     |  25 ++++---
 drivers/md/dm.c           | 171 +++++++++++-----------------------------------
 drivers/md/dm.h           |   5 +-
 include/linux/blk_types.h |   2 +
 include/linux/blkdev.h    |   6 +-
 6 files changed, 73 insertions(+), 230 deletions(-)
block/blk-core.c +11 -83
index 7a71a1d..986b256 100644
@@ -117,7 +117,7 @@ EXPORT_SYMBOL(blk_rq_init);
 static void req_bio_endio(struct request *rq, struct bio *bio,
 			  unsigned int nbytes, int error)
 {
-	if (error)
+	if (error && !(rq->cmd_flags & REQ_CLONE))
 		clear_bit(BIO_UPTODATE, &bio->bi_flags);
 	else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
 		error = -EIO;
@@ -128,7 +128,8 @@ static void req_bio_endio(struct request *rq, struct bio *bio,
 	bio_advance(bio, nbytes);
 
 	/* don't actually finish bio if it's part of flush sequence */
-	if (bio->bi_iter.bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
+	if (bio->bi_iter.bi_size == 0 &&
+	    !(rq->cmd_flags & (REQ_FLUSH_SEQ|REQ_CLONE)))
 		bio_endio(bio, error);
 }
 
@@ -2911,95 +2912,22 @@ int blk_lld_busy(struct request_queue *q)
 }
 EXPORT_SYMBOL_GPL(blk_lld_busy);
 
-/**
- * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
- * @rq: the clone request to be cleaned up
- *
- * Description:
- *     Free all bios in @rq for a cloned request.
- */
-void blk_rq_unprep_clone(struct request *rq)
-{
-	struct bio *bio;
-
-	while ((bio = rq->bio) != NULL) {
-		rq->bio = bio->bi_next;
-
-		bio_put(bio);
-	}
-}
-EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
-
-/*
- * Copy attributes of the original request to the clone request.
- * The actual data parts (e.g. ->cmd, ->sense) are not copied.
- */
-static void __blk_rq_prep_clone(struct request *dst, struct request *src)
+void blk_rq_prep_clone(struct request *dst, struct request *src)
 {
 	dst->cpu = src->cpu;
-	dst->cmd_flags |= (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
+	dst->cmd_flags |= (src->cmd_flags & REQ_CLONE_MASK);
+	dst->cmd_flags |= REQ_NOMERGE | REQ_CLONE;
 	dst->cmd_type = src->cmd_type;
 	dst->__sector = blk_rq_pos(src);
 	dst->__data_len = blk_rq_bytes(src);
 	dst->nr_phys_segments = src->nr_phys_segments;
 	dst->ioprio = src->ioprio;
 	dst->extra_len = src->extra_len;
-}
-
-/**
- * blk_rq_prep_clone - Helper function to setup clone request
- * @rq: the request to be setup
- * @rq_src: original request to be cloned
- * @bs: bio_set that bios for clone are allocated from
- * @gfp_mask: memory allocation mask for bio
- * @bio_ctr: setup function to be called for each clone bio.
- *           Returns %0 for success, non %0 for failure.
- * @data: private data to be passed to @bio_ctr
- *
- * Description:
- *     Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
- *     The actual data parts of @rq_src (e.g. ->cmd, ->sense)
- *     are not copied, and copying such parts is the caller's responsibility.
- *     Also, pages which the original bios are pointing to are not copied
- *     and the cloned bios just point same pages.
- *     So cloned bios must be completed before original bios, which means
- *     the caller must complete @rq before @rq_src.
- */
-int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
-		      struct bio_set *bs, gfp_t gfp_mask,
-		      int (*bio_ctr)(struct bio *, struct bio *, void *),
-		      void *data)
-{
-	struct bio *bio, *bio_src;
-
-	if (!bs)
-		bs = fs_bio_set;
-
-	__rq_for_each_bio(bio_src, rq_src) {
-		bio = bio_clone_fast(bio_src, gfp_mask, bs);
-		if (!bio)
-			goto free_and_out;
-
-		if (bio_ctr && bio_ctr(bio, bio_src, data))
-			goto free_and_out;
-
-		if (rq->bio) {
-			rq->biotail->bi_next = bio;
-			rq->biotail = bio;
-		} else
-			rq->bio = rq->biotail = bio;
-	}
-
-	__blk_rq_prep_clone(rq, rq_src);
-
-	return 0;
-
-free_and_out:
-	if (bio)
-		bio_put(bio);
-	blk_rq_unprep_clone(rq);
-
-	return -ENOMEM;
+	dst->bio = src->bio;
+	dst->biotail = src->biotail;
+	dst->cmd = src->cmd;
+	dst->cmd_len = src->cmd_len;
+	dst->sense = src->sense;
 }
 EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
 
drivers/md/dm-table.c +16 -9
index d9b00b8..3662b2e 100644
@@ -940,21 +940,28 @@ static int dm_table_alloc_md_mempools(struct dm_table *t, struct mapped_device *
 {
 	unsigned type = dm_table_get_type(t);
 	unsigned per_bio_data_size = 0;
-	struct dm_target *tgt;
 	unsigned i;
 
-	if (unlikely(type == DM_TYPE_NONE)) {
+	switch (type) {
+	case DM_TYPE_BIO_BASED:
+		for (i = 0; i < t->num_targets; i++) {
+			struct dm_target *tgt = t->targets + i;
+
+			per_bio_data_size = max(per_bio_data_size,
+						tgt->per_bio_data_size);
+		}
+		t->mempools = dm_alloc_bio_mempools(t->integrity_supported,
+						    per_bio_data_size);
+		break;
+	case DM_TYPE_REQUEST_BASED:
+	case DM_TYPE_MQ_REQUEST_BASED:
+		t->mempools = dm_alloc_rq_mempools(md, type);
+		break;
+	default:
 		DMWARN("no table type is set, can't allocate mempools");
 		return -EINVAL;
 	}
 
-	if (type == DM_TYPE_BIO_BASED)
-		for (i = 0; i < t->num_targets; i++) {
-			tgt = t->targets + i;
-			per_bio_data_size = max(per_bio_data_size, tgt->per_bio_data_size);
-		}
-
-	t->mempools = dm_alloc_md_mempools(md, type, t->integrity_supported, per_bio_data_size);
 	if (!t->mempools)
 		return -ENOMEM;
 
drivers/md/dm.c +40 -131

233 lines not on this page · raw message

drivers/md/dm.h +3 -2
index 6123c2b..e6e66d0 100644
@@ -222,8 +222,9 @@ void dm_kcopyd_exit(void);
 /*
  * Mempool operations
  */
-struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
-					    unsigned integrity, unsigned per_bio_data_size);
+struct dm_md_mempools *dm_alloc_bio_mempools(unsigned integrity,
+					     unsigned per_bio_data_size);
+struct dm_md_mempools *dm_alloc_rq_mempools(struct mapped_device *md, unsigned type);
 void dm_free_md_mempools(struct dm_md_mempools *pools);
 
 /*
include/linux/blk_types.h +2 -0
index 7303b34..6ab9d12 100644
@@ -192,6 +192,7 @@ enum rq_flag_bits {
 	__REQ_HASHED,		/* on IO scheduler merge hash */
 	__REQ_MQ_INFLIGHT,	/* track inflight for MQ */
 	__REQ_NO_TIMEOUT,	/* requests may never expire */
+	__REQ_CLONE,		/* cloned bios */
 	__REQ_NR_BITS,		/* stops here */
 };
 
@@ -246,5 +247,6 @@ enum rq_flag_bits {
 #define REQ_HASHED		(1ULL << __REQ_HASHED)
 #define REQ_MQ_INFLIGHT		(1ULL << __REQ_MQ_INFLIGHT)
 #define REQ_NO_TIMEOUT		(1ULL << __REQ_NO_TIMEOUT)
+#define REQ_CLONE		(1ULL << __REQ_CLONE)
 
 #endif /* __LINUX_BLK_TYPES_H */
include/linux/blkdev.h +1 -5
index bc91795..9ded80d 100644
@@ -775,11 +775,7 @@ extern void blk_add_request_payload(struct request *rq, struct page *page,
 		unsigned int len);
 extern int blk_rq_check_limits(struct request_queue *q, struct request *rq);
 extern int blk_lld_busy(struct request_queue *q);
-extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
-			     struct bio_set *bs, gfp_t gfp_mask,
-			     int (*bio_ctr)(struct bio *, struct bio *, void *),
-			     void *data);
-extern void blk_rq_unprep_clone(struct request *rq);
+extern void blk_rq_prep_clone(struct request *rq, struct request *rq_src);
 extern int blk_insert_cloned_request(struct request_queue *q,
 				     struct request *rq);
 extern void blk_delay_queue(struct request_queue *, unsigned long);
Signature · 1 line
2.3.2 (Apple Git-55)
#2 lore raw

Re: [PATCH for-4.2 2/3] block, dm: don't copy bios for request clones

Quoted 11 lines · Mike Snitzer

From: Christoph Hellwig <[email protected]>

Currently dm-multipath has to clone the bios for every request sent to the lower devices, which wastes cpu cycles and ties down memory.

This patch instead adds a new REQ_CLONE flag that instructs req_bio_endio to not complete bios attached to a request, which we set on clone requests similar to bios in a flush sequence. With this change I/O errors on a path failure only get propagated to dm-multipath, which can then either resubmit the I/O or complete the bios on the original request.

..

block/blk-core.c static void req_bio_endio(struct request *rq, struct bio *bio, · line 134

Quoted 9 lines
block/blk-core.c +2 -1
@@ -128,7 +128,8 @@ static void req_bio_endio(struct request *rq, struct bio *bio,
 	bio_advance(bio, nbytes);
 
 	/* don't actually finish bio if it's part of flush sequence */
-	if (bio->bi_iter.bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
+	if (bio->bi_iter.bi_size == 0 &&
+	    !(rq->cmd_flags & (REQ_FLUSH_SEQ|REQ_CLONE)))
 		bio_endio(bio, error);
 }

Not completing bios is not sufficient.
If you advance the bi_iter to the end, you need to somehow rewind it or the re-submission will be incomplete, that would end up as a data corruption...

Signature · 1 line
Jun'ichi Nomura, NEC Corporation
#3 lore raw

Re: [PATCH for-4.2 2/3] block, dm: don't copy bios for request clones

Quoted 4 lines · Junichi Nomura

Not completing bios is not sufficient.
If you advance the bi_iter to the end, you need to somehow rewind it or the re-submission will be incomplete, that would end up as a data corruption...

Can you explain which particular case you're worried about?

#4 lore raw

Re: [PATCH for-4.2 2/3] block, dm: don't copy bios for request clones

Quoted 7 lines · Christoph Hellwig
Quoted 4 lines · Junichi Nomura

Not completing bios is not sufficient.
If you advance the bi_iter to the end, you need to somehow rewind it or the re-submission will be incomplete, that would end up as a data corruption...

Can you explain which particular case you're worried about?

General path failure case.

On retrying, another clone is created but bios it points to are already advanced to the end with your patch. So they look like bios with no remaining segments. Lower driver may successfully completes such a resubmitted clone *without doing actual I/O*.
Then written data will be lost / read data will be bogus.

Can you test this scenario with your patch?
1. Set up a multipath device with fail-over mode
2. Write something to the multipath device. After the clone request is sent to the primary path and before the data goes to the disk,
down the primary path
(e.g. echo offline > /sys/block/sdXX/device/state)
3. (dm-mpath will retry from the secondary path and the write will eventually succeed)
4. Verify if the written data is really on the disk

Signature · 1 line
Jun'ichi Nomura, NEC Corporation
#5 lore raw

Re: [PATCH for-4.2 2/3] block, dm: don't copy bios for request clones

Quoted 6 lines · Junichi Nomura
Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Junichi Nomura

Not completing bios is not sufficient.
If you advance the bi_iter to the end, you need to somehow rewind it or the re-submission will be incomplete, that would end up as a data corruption...

Less critical than the data corruption issue,
I'm also worried about partial completion case. For successful partial completion, current code completes bio before fully completing the request.
Your patch changes bios not completed until the request is fully completed.

Other related concern is partial failure. In the case of bad sector, for example, current code fails I/O for the particular sector but other sectors in the request succeeds.
If you make the request completion as all-or-nothing model, that will be a degrade for such a case.

I'm not very sure how much impact does the removal of partial completion have in the real world.
If partial completion is so negligible, I think it should be handled in such a way all the cases, instead of special casing REQ_CLONE.

Quoted 11 lines
Quoted 1 line

Can you explain which particular case you're worried about?

General path failure case.

On retrying, another clone is created but bios it points to are already advanced to the end with your patch. So they look like bios with no remaining segments. Lower driver may successfully completes such a resubmitted clone *without doing actual I/O*.
Then written data will be lost / read data will be bogus.

Quoted 10 lines

Can you test this scenario with your patch?
1. Set up a multipath device with fail-over mode
2. Write something to the multipath device. After the clone request is sent to the primary path and before the data goes to the disk,
down the primary path
(e.g. echo offline > /sys/block/sdXX/device/state)
3. (dm-mpath will retry from the secondary path and the write will eventually succeed)
4. Verify if the written data is really on the disk

I made a small script so that people can play with.
The script sets up tcm_loop multipath device and fio verification test while repeating paths up and down quickly.

When your patch is applied, fio reports verification failure within a minute like this:

# ./stress-mp.sh
..
test1: (g=0): rw=randwrite, bs=512K-512K/512K-512K/512K-512K, ioengine=libaio, iodepth=2
fio-2.2.8-16-g68d9
Starting 1 process
meta: verify failed at file /dev/mapper/mp offset 477626368, length 524288
       received data dumped as mp.477626368.received
       expected data dumped as mp.477626368.expected
fio: pid=13560, err=84/file:io_u.c:1866, func=io_u_queued_complete, error=Invalid or incomplete multibyte or wide character

test1: (groupid=0, jobs=1): err=84 (file:io_u.c:1866, func=io_u_queued_complete, error=Invalid or incomplete multibyte or wide character): pid=13560: Thu May 28 01:54:56 2015

Signature · 1 line
Jun'ichi Nomura, NEC Corporation
#6 lore raw

Re: [PATCH for-4.2 2/3] block, dm: don't copy bios for request clones

Quoted 5 lines · Junichi Nomura

I'm also worried about partial completion case. For successful partial completion, current code completes bio before fully completing the request.
Your patch changes bios not completed until the request is fully completed.

Yes. Why are you worried about this case?

Quoted 5 lines

I'm not very sure how much impact does the removal of partial completion have in the real world.
If partial completion is so negligible, I think it should be handled in such a way all the cases, instead of special casing REQ_CLONE.

It isn't negligible - under load it actually does matter that we handle partial completions as arrays decide to just complete a request partially, so if we don't handle them we might end up in a loop not making progress. But if we only do that once on path fail over it's not an actual issue.

#7 lore raw

Re: [PATCH for-4.2 2/3] block, dm: don't copy bios for request clones

Quoted 8 lines · Christoph Hellwig
Quoted 5 lines · Junichi Nomura

I'm also worried about partial completion case. For successful partial completion, current code completes bio before fully completing the request.
Your patch changes bios not completed until the request is fully completed.

Yes. Why are you worried about this case?

See below.

Quoted 10 lines
Quoted 5 lines

I'm not very sure how much impact does the removal of partial completion have in the real world.
If partial completion is so negligible, I think it should be handled in such a way all the cases, instead of special casing REQ_CLONE.

It isn't negligible - under load it actually does matter that we handle partial completions as arrays decide to just complete a request partially, so if we don't handle them we might end up in a loop not making progress.

OK.

Quoted 2 lines

But if we only do that once on
path fail over it's not an actual issue.

But with your patch, the fail over happens after full completion of the request. Is it ok?

Signature · 1 line
Jun'ichi Nomura, NEC Corporation
#8 lore raw

Re: [PATCH for-4.2 2/3] block, dm: don't copy bios for request clones

Quoted 5 lines · Junichi Nomura
Quoted 2 lines

But if we only do that once on
path fail over it's not an actual issue.

But with your patch, the fail over happens after full completion of the request. Is it ok?

At least for SCSI it is, although the reason why is very subtile:

From scsi_io_completion:

	/*
	 * If we finished all bytes in the request we are done now.
	 */
	if (!scsi_end_request(req, error, good_bytes, 0))
		return;

	/*
	 * Kill remainder if no retrys.
	 */
	if (error && scsi_noretry_cmd(cmd)) {
		if (scsi_end_request(req, error, blk_rq_bytes(req), 0))
			BUG();
		return;
	}

So for a noretry command send from dm-mpath we will never leave a command that had an error completion around, even if it was a partial completion.

Relying on the LLDDs to get this right seems rather dangerous, though, so it might make sense to lift the above sequence to core code after a careful audit of other drivers to see if and how they handle partial completions.

#9 lore raw

Re: [PATCH for-4.2 2/3] block, dm: don't copy bios for request clones

Quoted 17 lines · Christoph Hellwig

From scsi_io_completion:

	/*
	 * If we finished all bytes in the request we are done now.
	 */
	if (!scsi_end_request(req, error, good_bytes, 0))
		return;

	/*
	 * Kill remainder if no retrys.
	 */
	if (error && scsi_noretry_cmd(cmd)) {
		if (scsi_end_request(req, error, blk_rq_bytes(req), 0))
			BUG();
		return;
	}
Quoted 2 lines

So for a noretry command send from dm-mpath we will never leave a command that had an error completion around, even if it was a partial completion.

scsi_noretry_cmd() is not always true even for dm-mpath.

Following code in the later part of the function tries to complete some bytes with error and requeue the remainder.
However it depends on blk_rq_err_bytes() whether partial error completion actually happens...

        case ACTION_FAIL:
                ...
                if (!scsi_end_request(req, error, blk_rq_err_bytes(req), 0))
                        return;
                /*FALLTHRU*/
        case ACTION_REPREP:
        requeue:
                /* Unprep the request and put it back at the head of the queue.
                 * A new command will be prepared and issued.
                 */
                if (q->mq_ops) {
                        cmd->request->cmd_flags &= ~REQ_DONTPREP;
                        scsi_mq_uninit_cmd(cmd);
                        scsi_mq_requeue_cmd(cmd);
                } else {
                        scsi_release_buffers(cmd);
                        scsi_requeue_command(q, cmd);
                }
Quoted 3 lines

Relying on the LLDDs to get this right seems rather dangerous, though, so it might make sense to lift the above sequence to core code after a careful audit of other drivers to see if and how they handle partial completions.

Right, that is the point.

Signature · 1 line
Jun'ichi Nomura, NEC Corporation
#10 lore raw

Re: [PATCH for-4.2 2/3] block, dm: don't copy bios for request clones

In reply to Junichi Nomura
Quoted 10 lines · Junichi Nomura

Can you test this scenario with your patch?
1. Set up a multipath device with fail-over mode
2. Write something to the multipath device. After the clone request is sent to the primary path and before the data goes to the disk,
down the primary path
(e.g. echo offline > /sys/block/sdXX/device/state)
3. (dm-mpath will retry from the secondary path and the write will eventually succeed)
4. Verify if the written data is really on the disk

Verified as not working correctly. The patch below fixes it, but it needs more testing and some comments:

block/blk-core.c +8 -3
index aa819a5..54feaae 100644
@@ -117,7 +117,13 @@ EXPORT_SYMBOL(blk_rq_init);
 static void req_bio_endio(struct request *rq, struct bio *bio,
 			  unsigned int nbytes, int error)
 {
-	if (error && !(rq->cmd_flags & REQ_CLONE))
+	if (rq->cmd_flags & REQ_CLONE) {
+		if (!error && test_bit(BIO_UPTODATE, &bio->bi_flags))
+			bio_advance(bio, nbytes);
+		return;
+	}
+
+	if (error)
 		clear_bit(BIO_UPTODATE, &bio->bi_flags);
 	else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
 		error = -EIO;
@@ -128,8 +134,7 @@ static void req_bio_endio(struct request *rq, struct bio *bio,
 	bio_advance(bio, nbytes);
 
 	/* don't actually finish bio if it's part of flush sequence */
-	if (bio->bi_iter.bi_size == 0 &&
-	    !(rq->cmd_flags & (REQ_FLUSH_SEQ|REQ_CLONE)))
+	if (bio->bi_iter.bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
 		bio_endio(bio, error);
 }
 
#11 lore raw

Re: [PATCH for-4.2 2/3] block, dm: don't copy bios for request clones

Quoted 12 lines · Christoph Hellwig
Quoted 10 lines · Junichi Nomura

Can you test this scenario with your patch?
1. Set up a multipath device with fail-over mode
2. Write something to the multipath device. After the clone request is sent to the primary path and before the data goes to the disk,
down the primary path
(e.g. echo offline > /sys/block/sdXX/device/state)
3. (dm-mpath will retry from the secondary path and the write will eventually succeed)
4. Verify if the written data is really on the disk

Quoted 2 lines

Verified as not working correctly. The patch below fixes it, but it needs more testing and some comments:

Thanks. But just skipping bio_advance() is not good.

For example, blk_update_request() does this:

Quoted 6 lines
        while (req->bio) {
                struct bio *bio = req->bio;
                unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);

                if (bio_bytes == bio->bi_iter.bi_size)
                        req->bio = bio->bi_next;

if bi_iter.bi_size is not correct, we possibly fail to move the req->bio pointer.

So other test case could be:

1. Create a bio with 1MB size
2. Submit a request with the bio
3. From lower driver,
firstly partial complete 512KB with error,
secondly partial complete 512KB without error
4. The I/O should complete successfully.

And yet another test:

1. Create 4 READ bios with each 4KB
2. Submit a request with the 4 bios
3. From lower driver,
partial complete 8KB without error,
partial complete 4KB with error (emulate medium error), partial complete 4KB without error
4. Verify the read data.
Only the 3rd 4KB should be failed.
Others should have correct contents.

Signature · 1 line
Jun'ichi Nomura, NEC Corporation
#12 lore raw

Re: [PATCH for-4.2 2/3] block, dm: don't copy bios for request clones

Quoted 12 lines · Junichi Nomura

For example, blk_update_request() does this:

Quoted 6 lines
        while (req->bio) {
                struct bio *bio = req->bio;
                unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);

                if (bio_bytes == bio->bi_iter.bi_size)
                        req->bio = bio->bi_next;

if bi_iter.bi_size is not correct, we possibly fail to move the req->bio pointer.

Quoted 8 lines

So other test case could be:

1. Create a bio with 1MB size
2. Submit a request with the bio
3. From lower driver,
firstly partial complete 512KB with error,
secondly partial complete 512KB without error
4. The I/O should complete successfully.

It will be returned to the upper layer after the first completion as mentioned in my previous mail. Same for the other test.

Think of partial completion as a way for SCSI targets to chunk transfers, higher layers will only see full success/failure cases.

For that reason we also removed the bytes completed argument to ->bi_end_io because the callers should not care about these details.

#13 lore raw

PATCH 3/3 block: remove export for blk_queue_bio

In reply to Mike Snitzer

With commit ff36ab345 ("dm: remove request-based logic from make_request_fn wrapper") DM no longer calls blk_queue_bio() directly, so remove its export. Doing so required a forward declaration in blk-core.c.

---
 block/blk-core.c       | 5 +++--
 include/linux/blkdev.h | 2 --
 2 files changed, 3 insertions(+), 4 deletions(-)
block/blk-core.c +3 -2
index 986b256..f6ab750 100644
@@ -736,6 +736,8 @@ blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
 }
 EXPORT_SYMBOL(blk_init_queue_node);
 
+static void blk_queue_bio(struct request_queue *q, struct bio *bio);
+
 struct request_queue *
 blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
 			 spinlock_t *lock)
@@ -1589,7 +1591,7 @@ void init_request_from_bio(struct request *req, struct bio *bio)
 	blk_rq_bio_prep(req->q, req, bio);
 }
 
-void blk_queue_bio(struct request_queue *q, struct bio *bio)
+static void blk_queue_bio(struct request_queue *q, struct bio *bio)
 {
 	const bool sync = !!(bio->bi_rw & REQ_SYNC);
 	struct blk_plug *plug;
@@ -1697,7 +1699,6 @@ out_unlock:
 		spin_unlock_irq(q->queue_lock);
 	}
 }
-EXPORT_SYMBOL_GPL(blk_queue_bio);	/* for device mapper only */
 
 /*
  * If bio->bi_dev is a partition, remap the location
include/linux/blkdev.h +0 -2
index 9ded80d..776d2ee 100644
@@ -788,8 +788,6 @@ extern int scsi_cmd_ioctl(struct request_queue *, struct gendisk *, fmode_t,
 extern int sg_scsi_ioctl(struct request_queue *, struct gendisk *, fmode_t,
 			 struct scsi_ioctl_command __user *);
 
-extern void blk_queue_bio(struct request_queue *q, struct bio *bio);
-
 /*
  * A queue has just exitted congestion.  Note this in the global counter of
  * congested queues, and wake up anyone who was waiting for requests to be
Signature · 1 line
2.3.2 (Apple Git-55)