Skip to contentSkip to conversation

block: fix two regressiones on bounce

PATCH In review Ming Lei 7 participants 19 messages Dec 18, 2017 – Feb 28, 2018 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

cover block: fix two regressiones on bounce

Hi Jens,

The 1st patch makes sure that passthrough IO won't enter .make_request_fn.

The 2nd one fixes blk_rq_append_bio(), which is from your post with small change on failure handling.

Jens Axboe (1):
block: fix blk_rq_append_bio

Ming Lei (1):
block: don't let passthrough IO go into .make_request_fn()

 block/blk-map.c                    | 38 ++++++++++++++++++++++----------------
 block/bounce.c                     |  6 ++++--
 drivers/scsi/osd/osd_initiator.c   |  4 +++-
 drivers/target/target_core_pscsi.c |  4 ++--
 include/linux/blkdev.h             | 23 ++++++++++++++++++++---
 5 files changed, 51 insertions(+), 24 deletions(-)
Signature · 1 line
2.9.5
author
#1 lore raw

PATCH 1/2 block: don't let passthrough IO go into .make_request_fn()

Commit a8821f3f3("block: Improvements to bounce-buffer handling") tries to make sure that the bio to .make_request_fn won't exceed BIO_MAX_PAGES, but ignores that passthrough I/O can use blk_queue_bounce() too. Especially, passthrough IO may not be sector-aligned, and the check of 'sectors < bio_sectors(*bio_orig)' inside __blk_queue_bounce() may become true even though the max bvec number doesn't exceed BIO_MAX_PAGES, then cause the bio splitted, and the original passthrough bio is submited to generic_make_request().

This patch fixes this issue by checking if the bio is passthrough IO, and use bio_kmalloc() to allocate the cloned passthrough bio.

Cc NeilBrown <[email protected]>
Fixes a8821f3f3("block: Improvements to bounce-buffer handling")
---
 block/bounce.c         |  6 ++++--
 include/linux/blkdev.h | 21 +++++++++++++++++++--
 2 files changed, 23 insertions(+), 4 deletions(-)
block/bounce.c +4 -2
index fceb1a96480b..1d05c422c932 100644
@@ -200,6 +200,7 @@ static void __blk_queue_bounce(struct request_queue *q, struct bio **bio_orig,
 	unsigned i = 0;
 	bool bounce = false;
 	int sectors = 0;
+	bool passthrough = bio_is_passthrough(*bio_orig);
 
 	bio_for_each_segment(from, *bio_orig, iter) {
 		if (i++ < BIO_MAX_PAGES)
@@ -210,13 +211,14 @@ static void __blk_queue_bounce(struct request_queue *q, struct bio **bio_orig,
 	if (!bounce)
 		return;
 
-	if (sectors < bio_sectors(*bio_orig)) {
+	if (!passthrough && sectors < bio_sectors(*bio_orig)) {
 		bio = bio_split(*bio_orig, sectors, GFP_NOIO, bounce_bio_split);
 		bio_chain(bio, *bio_orig);
 		generic_make_request(*bio_orig);
 		*bio_orig = bio;
 	}
-	bio = bio_clone_bioset(*bio_orig, GFP_NOIO, bounce_bio_set);
+	bio = bio_clone_bioset(*bio_orig, GFP_NOIO, passthrough ? NULL :
+			bounce_bio_set);
 
 	bio_for_each_segment_all(to, bio, i) {
 		struct page *page = to->bv_page;
include/linux/blkdev.h +19 -2
index 8089ca17db9a..abd06f540863 100644
@@ -241,14 +241,24 @@ struct request {
 	struct request *next_rq;
 };
 
+static inline bool blk_op_is_scsi(unsigned int op)
+{
+	return op == REQ_OP_SCSI_IN || op == REQ_OP_SCSI_OUT;
+}
+
+static inline bool blk_op_is_private(unsigned int op)
+{
+	return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
+}
+
 static inline bool blk_rq_is_scsi(struct request *rq)
 {
-	return req_op(rq) == REQ_OP_SCSI_IN || req_op(rq) == REQ_OP_SCSI_OUT;
+	return blk_op_is_scsi(req_op(rq));
 }
 
 static inline bool blk_rq_is_private(struct request *rq)
 {
-	return req_op(rq) == REQ_OP_DRV_IN || req_op(rq) == REQ_OP_DRV_OUT;
+	return blk_op_is_private(req_op(rq));
 }
 
 static inline bool blk_rq_is_passthrough(struct request *rq)
@@ -256,6 +266,13 @@ static inline bool blk_rq_is_passthrough(struct request *rq)
 	return blk_rq_is_scsi(rq) || blk_rq_is_private(rq);
 }
 
+static inline bool bio_is_passthrough(struct bio *bio)
+{
+	unsigned op = bio_op(bio);
+
+	return blk_op_is_scsi(op) || blk_op_is_private(op);
+}
+
 static inline unsigned short req_get_ioprio(struct request *req)
 {
 	return req->ioprio;
Signature · 1 line
2.9.5
author
#2 lore raw

PATCH 2/2 block: fix blk_rq_append_bio

In reply to Ming Lei

From: Jens Axboe <[email protected]>

Commit caa4b02476e3(blk-map: call blk_queue_bounce from blk_rq_append_bio) moves blk_queue_bounce() into blk_rq_append_bio(), but don't consider that the bounced bio becomes invisiable to caller since the parameter type is 'struct bio *', which should have been 'struct bio **'.

This patch fixes this issue by passing 'struct bio **' to blk_rq_append_bio(), then the bounced bio can be returned to caller.

Also failure handling is considered too.

Fixes caa4b02476e3 ("blk-map: call blk_queue_bounce from blk_rq_append_bio")
Cc Christoph Hellwig <[email protected]>

(handling failure of blk_rq_append_bio(), only call bio_get() after blk_rq_append_bio() returns OK)

---
 block/blk-map.c                    | 38 ++++++++++++++++++++++----------------
 drivers/scsi/osd/osd_initiator.c   |  4 +++-
 drivers/target/target_core_pscsi.c |  4 ++--
 include/linux/blkdev.h             |  2 +-
 4 files changed, 28 insertions(+), 20 deletions(-)
block/blk-map.c +22 -16
index b21f8e86f120..d3a94719f03f 100644
@@ -12,22 +12,29 @@
 #include "blk.h"
 
 /*
- * Append a bio to a passthrough request.  Only works can be merged into
- * the request based on the driver constraints.
+ * Append a bio to a passthrough request.  Only works if the bio can be merged
+ * into the request based on the driver constraints.
  */
-int blk_rq_append_bio(struct request *rq, struct bio *bio)
+int blk_rq_append_bio(struct request *rq, struct bio **bio)
 {
-	blk_queue_bounce(rq->q, &bio);
+	struct bio *orig_bio = *bio;
+
+	blk_queue_bounce(rq->q, bio);
 
 	if (!rq->bio) {
-		blk_rq_bio_prep(rq->q, rq, bio);
+		blk_rq_bio_prep(rq->q, rq, *bio);
 	} else {
-		if (!ll_back_merge_fn(rq->q, rq, bio))
+		if (!ll_back_merge_fn(rq->q, rq, *bio)) {
+			if (orig_bio != *bio) {
+				bio_put(*bio);
+				*bio = orig_bio;
+			}
 			return -EINVAL;
+		}
 
-		rq->biotail->bi_next = bio;
-		rq->biotail = bio;
-		rq->__data_len += bio->bi_iter.bi_size;
+		rq->biotail->bi_next = *bio;
+		rq->biotail = *bio;
+		rq->__data_len += (*bio)->bi_iter.bi_size;
 	}
 
 	return 0;
@@ -73,14 +80,12 @@ static int __blk_rq_map_user_iov(struct request *rq,
 	 * We link the bounce buffer in and could have to traverse it
 	 * later so we have to get a ref to prevent it from being freed
 	 */
-	ret = blk_rq_append_bio(rq, bio);
-	bio_get(bio);
+	ret = blk_rq_append_bio(rq, &bio);
 	if (ret) {
-		bio_endio(bio);
 		__blk_rq_unmap_user(orig_bio);
-		bio_put(bio);
 		return ret;
 	}
+	bio_get(bio);
 
 	return 0;
 }
@@ -213,7 +218,7 @@ int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
 	int reading = rq_data_dir(rq) == READ;
 	unsigned long addr = (unsigned long) kbuf;
 	int do_copy = 0;
-	struct bio *bio;
+	struct bio *bio, *orig_bio;
 	int ret;
 
 	if (len > (queue_max_hw_sectors(q) << 9))
@@ -236,10 +241,11 @@ int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
 	if (do_copy)
 		rq->rq_flags |= RQF_COPY_USER;
 
-	ret = blk_rq_append_bio(rq, bio);
+	orig_bio = bio;
+	ret = blk_rq_append_bio(rq, &bio);
 	if (unlikely(ret)) {
 		/* request is too big */
-		bio_put(bio);
+		bio_put(orig_bio);
 		return ret;
 	}
 
drivers/scsi/osd/osd_initiator.c +3 -1
index a4f28b7e4c65..e18877177f1b 100644
@@ -1576,7 +1576,9 @@ static struct request *_make_request(struct request_queue *q, bool has_write,
 		return req;
 
 	for_each_bio(bio) {
-		ret = blk_rq_append_bio(req, bio);
+		struct bio *bounce_bio = bio;
+
+		ret = blk_rq_append_bio(req, &bounce_bio);
 		if (ret)
 			return ERR_PTR(ret);
 	}
drivers/target/target_core_pscsi.c +2 -2
index 7c69b4a9694d..0d99b242e82e 100644
@@ -920,7 +920,7 @@ pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
 					" %d i: %d bio: %p, allocating another"
 					" bio\n", bio->bi_vcnt, i, bio);
 
-				rc = blk_rq_append_bio(req, bio);
+				rc = blk_rq_append_bio(req, &bio);
 				if (rc) {
 					pr_err("pSCSI: failed to append bio\n");
 					goto fail;
@@ -938,7 +938,7 @@ pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
 	}
 
 	if (bio) {
-		rc = blk_rq_append_bio(req, bio);
+		rc = blk_rq_append_bio(req, &bio);
 		if (rc) {
 			pr_err("pSCSI: failed to append bio\n");
 			goto fail;
include/linux/blkdev.h +1 -1
index abd06f540863..100d0df38026 100644
@@ -965,7 +965,7 @@ extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
 extern void blk_rq_unprep_clone(struct request *rq);
 extern blk_status_t blk_insert_cloned_request(struct request_queue *q,
 				     struct request *rq);
-extern int blk_rq_append_bio(struct request *rq, struct bio *bio);
+extern int blk_rq_append_bio(struct request *rq, struct bio **bio);
 extern void blk_delay_queue(struct request_queue *, unsigned long);
 extern void blk_queue_split(struct request_queue *, struct bio **);
 extern void blk_recount_segments(struct request_queue *, struct bio *);
Signature · 1 line
2.9.5
#3 lore raw

Re: [2/2] block: fix blk_rq_append_bio

Hello,

I see a problem with this patch:

Quoted 45 lines · Ming Lei

From: Jens Axboe <[email protected]>

Commit caa4b02476e3(blk-map: call blk_queue_bounce from blk_rq_append_bio) moves blk_queue_bounce() into blk_rq_append_bio(), but don't consider that the bounced bio becomes invisiable to caller since the parameter type is 'struct bio *', which should have been 'struct bio **'.

This patch fixes this issue by passing 'struct bio **' to blk_rq_append_bio(), then the bounced bio can be returned to caller.

Also failure handling is considered too.

Fixes caa4b02476e3 ("blk-map: call blk_queue_bounce from blk_rq_append_bio")
Cc Christoph Hellwig <[email protected]>

(handling failure of blk_rq_append_bio(), only call bio_get() after blk_rq_append_bio() returns OK)

---
 block/blk-map.c                    | 38 ++++++++++++++++++++++----------------
 drivers/scsi/osd/osd_initiator.c   |  4 +++-
 drivers/target/target_core_pscsi.c |  4 ++--
 include/linux/blkdev.h             |  2 +-
 4 files changed, 28 insertions(+), 20 deletions(-)
block/blk-map.c +6 -4
index b21f8e86f120..d3a94719f03f 100644
@@ -12,22 +12,29 @@
 #include "blk.h"
 
 /*
- * Append a bio to a passthrough request.  Only works can be merged into
- * the request based on the driver constraints.
+ * Append a bio to a passthrough request.  Only works if the bio can be merged
+ * into the request based on the driver constraints.
  */
-int blk_rq_append_bio(struct request *rq, struct bio *bio)
+int blk_rq_append_bio(struct request *rq, struct bio **bio)
 {
-	blk_queue_bounce(rq->q, &bio);
+	struct bio *orig_bio = *bio;
+
+	blk_queue_bounce(rq->q, bio);

block/blk-map.c line 30

Quoted 10 lines
block/blk-map.c +5 -2
 
 	if (!rq->bio) {
-		blk_rq_bio_prep(rq->q, rq, bio);
+		blk_rq_bio_prep(rq->q, rq, *bio);
 	} else {
-		if (!ll_back_merge_fn(rq->q, rq, bio))
+		if (!ll_back_merge_fn(rq->q, rq, *bio)) {
+			if (orig_bio != *bio) {
+				bio_put(*bio);
+				*bio = orig_bio;

1) Suppose the bio *does* bounce, which allocates some pages from the DMA pool, and subsequently this test fails. bio_put won't deallocate those pages. Prior to caa4b02476e3, it would be done by bio_endio, which is IMHO the correct way to do it.

2) What if the bio bounces and goes into the splitting path of __blk_queue_bounce. That would submit the request, which would be a problem should this test or some other later fail, cancelling the whole request.

For 1, I will send a patch. As for 2, it would be a problem for a long time so I have a hunch it doesn't actually happen, however, I'd like to know if it is somehow guaranteed.

Regards
Jiri Palecek

author
#4 lore raw

Re: [2/2] block: fix blk_rq_append_bio

Quoted 52 lines · Jiri Palecek

Hello,

I see a problem with this patch:

Quoted 46 lines · Ming Lei

From: Jens Axboe <[email protected]>

Commit caa4b02476e3(blk-map: call blk_queue_bounce from blk_rq_append_bio) moves blk_queue_bounce() into blk_rq_append_bio(), but don't consider that the bounced bio becomes invisiable to caller since the parameter type is 'struct bio *', which should have been 'struct bio **'.

This patch fixes this issue by passing 'struct bio **' to blk_rq_append_bio(), then the bounced bio can be returned to caller.

Also failure handling is considered too.

Fixes caa4b02476e3 ("blk-map: call blk_queue_bounce from blk_rq_append_bio")
Cc Christoph Hellwig <[email protected]>

(handling failure of blk_rq_append_bio(), only call bio_get() after blk_rq_append_bio() returns OK)

---
 block/blk-map.c                    | 38 ++++++++++++++++++++++----------------
 drivers/scsi/osd/osd_initiator.c   |  4 +++-
 drivers/target/target_core_pscsi.c |  4 ++--
 include/linux/blkdev.h             |  2 +-
 4 files changed, 28 insertions(+), 20 deletions(-)
block/blk-map.c +6 -4
index b21f8e86f120..d3a94719f03f 100644
@@ -12,22 +12,29 @@
 #include "blk.h"
 
 /*
- * Append a bio to a passthrough request.  Only works can be merged into
- * the request based on the driver constraints.
+ * Append a bio to a passthrough request.  Only works if the bio can be merged
+ * into the request based on the driver constraints.
  */
-int blk_rq_append_bio(struct request *rq, struct bio *bio)
+int blk_rq_append_bio(struct request *rq, struct bio **bio)
 {
-	blk_queue_bounce(rq->q, &bio);
+	struct bio *orig_bio = *bio;
+
+	blk_queue_bounce(rq->q, bio);
 
Quoted 14 lines

block/blk-map.c line 30

Quoted 9 lines
block/blk-map.c +5 -2
 	if (!rq->bio) {
-		blk_rq_bio_prep(rq->q, rq, bio);
+		blk_rq_bio_prep(rq->q, rq, *bio);
 	} else {
-		if (!ll_back_merge_fn(rq->q, rq, bio))
+		if (!ll_back_merge_fn(rq->q, rq, *bio)) {
+			if (orig_bio != *bio) {
+				bio_put(*bio);
+				*bio = orig_bio;

1) Suppose the bio *does* bounce, which allocates some pages from the DMA pool, and subsequently this test fails. bio_put won't deallocate those pages. Prior to caa4b02476e3, it would be done by bio_endio, which is IMHO the correct way to do it.

You are right, that is my fault, and will review your patch later.

Quoted 5 lines

2) What if the bio bounces and goes into the splitting path of __blk_queue_bounce. That would submit the request, which would be a problem should this test or some other later fail, cancelling the whole request.

When bio comes here, bio_is_passthrough() is true, it won't go into that path, so needn't to worry.

Thanks,
Ming

#5 lore raw

PATCH 1/1 Use bio_endio instead of bio_put in error path of blk_rq_append_bio

In reply to Ming Lei

Avoids page leak from bounced requests

---
 block/blk-map.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block/blk-map.c +2 -1
index d3a94719f03f..702d68166689 100644
@@ -26,7 +26,8 @@ int blk_rq_append_bio(struct request *rq, struct bio **bio)
 	} else {
 		if (!ll_back_merge_fn(rq->q, rq, *bio)) {
 			if (orig_bio != *bio) {
-				bio_put(*bio);
+				bio_inc_remaining(orig_bio);
+				bio_endio(*bio);
 				*bio = orig_bio;
 			}
 			return -EINVAL;
Signature · 1 line
2.15.1
#6 lore raw

Re: [PATCH] Use bio_endio instead of bio_put in error path of blk_rq_append_bio

block/blk-map.c int blk_rq_append_bio(struct request *rq, struct bio **=

Quoted 10 lines · On Thu, Jan 25, 2018 at 9:58 PM, Ji=C5=99=C3=AD Pale=C4=8Dek <jpalecek@web.=…

Avoids page leak from bounced requests

---
 block/blk-map.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block/blk-map.c +0 -0
index d3a94719f03f..702d68166689 100644
@@ -26,7 +26,8 @@ int blk_rq_append_bio(struct request *rq, struct bio **=

bio)

block/blk-map.c int blk_rq_append_bio(struct request *rq, struct bio **bio) · line 30

Quoted 6 lines
block/blk-map.c +2 -1
        } else {
                if (!ll_back_merge_fn(rq->q, rq, *bio)) {
                        if (orig_bio !=3D *bio) {
-                               bio_put(*bio);
+                               bio_inc_remaining(orig_bio);
+                               bio_endio(*bio);

'bio_inc_remaining(orig_bio);' shouldn't be needed since we don't chain bou= nced
bio, otherwise this patch is fine.

Thanks,
Ming Lei

#7 lore raw

Re: [PATCH] Use bio_endio instead of bio_put in error path of blk_rq_append_bio

Quoted 6 lines · Ming Lei
Quoted 5 lines · Jiří Paleček

Avoids page leak from bounced requests

---
  block/blk-map.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 13 lines

block/blk-map.c int blk_rq_append_bio(struct request *rq, struct bio **bio) · line 30

Quoted 11 lines
block/blk-map.c +2 -1
index d3a94719f03f..702d68166689 100644
@@ -26,7 +26,8 @@ int blk_rq_append_bio(struct request *rq, struct bio **bio)
         } else {
                 if (!ll_back_merge_fn(rq->q, rq, *bio)) {
                         if (orig_bio != *bio) {
-                               bio_put(*bio);
+                               bio_inc_remaining(orig_bio);
+                               bio_endio(*bio);

'bio_inc_remaining(orig_bio);' shouldn't be needed since we don't chain bounced bio, otherwise this patch is fine.

I believe it is needed or at least desirable. The situation when the request bounced is like this

bio (bounced) . bi_private ---> orig_bio

and at the end of bounce_end_io, bio_endio(bio->bi_private) [which is bio_endio(orig_bio) in our case] is called. That doesn't have any effect on __blk_rq_map_user_iov; its bios have .bi_end_io==0, therefore one call more or less doesn't matter. However, for other callers, like osd_initiator.c, this is not the case. They pass bios which have bi_end_io, and might be surprised if this was called unexpectedly.

Before  caa4b02476e3, blk_rq_append_request wouldn't touch/delete the passed bio at all under any circumstances. I believe it should stay that way and incrementing the remaining counter, which effectively nullifies the extra bio_endio call, does that pretty straightforwardly.

Regards

    Jiri Palecek

author
#8 lore raw

Re: [PATCH] Use bio_endio instead of bio_put in error path of blk_rq_append_bio

Quoted 25 lines · Jiri Palecek
Quoted 6 lines · Ming Lei
Quoted 5 lines · Jiří Paleček

Avoids page leak from bounced requests

---
  block/blk-map.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 13 lines

block/blk-map.c int blk_rq_append_bio(struct request *rq, struct bio **bio) · line 30

Quoted 11 lines
block/blk-map.c +2 -1
index d3a94719f03f..702d68166689 100644
@@ -26,7 +26,8 @@ int blk_rq_append_bio(struct request *rq, struct bio **bio)
         } else {
                 if (!ll_back_merge_fn(rq->q, rq, *bio)) {
                         if (orig_bio != *bio) {
-                               bio_put(*bio);
+                               bio_inc_remaining(orig_bio);
+                               bio_endio(*bio);

'bio_inc_remaining(orig_bio);' shouldn't be needed since we don't chain bounced bio, otherwise this patch is fine.

I believe it is needed or at least desirable. The situation when the request bounced is like this

Quoted 8 lines

bio (bounced) . bi_private ---> orig_bio

and at the end of bounce_end_io, bio_endio(bio->bi_private) [which is bio_endio(orig_bio) in our case] is called. That doesn't have any effect on __blk_rq_map_user_iov; its bios have .bi_end_io==0, therefore one call more or less doesn't matter. However, for other callers, like osd_initiator.c, this is not the case. They pass bios which have bi_end_io, and might be surprised if this was called unexpectedly.

OK, I think it is good to follow the rule of not calling .bi_end_io() in the failure path, just like __blk_rq_map_user_iov()/blk_rq_map_kern().

But seems pscsi_map_sg() depends on bio_endio(orig_bio) to free the 'orig_bio', could you fix it in this patch too?

Quoted 5 lines

Before  caa4b02476e3, blk_rq_append_request wouldn't touch/delete the passed bio at all under any circumstances. I believe it should stay that way and incrementing the remaining counter, which effectively nullifies the extra bio_endio call, does that pretty straightforwardly.

Seems too tricky to use bio_inc_remaining() for avoiding bio_endio(orig_bio), if we have to do that, I suggest to add comment on that.

Thanks,
Ming

#9 lore raw

Re: [PATCH] Use bio_endio instead of bio_put in error path of blk_rq_append_bio

Quoted 32 lines · Ming Lei
Quoted 23 lines · Jiri Palecek
Quoted 6 lines · Ming Lei
Quoted 5 lines · Jiří Paleček

Avoids page leak from bounced requests

---
   block/blk-map.c | 3 ++-
   1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 13 lines

block/blk-map.c int blk_rq_append_bio(struct request *rq, struct bio **bio) · line 30

Quoted 11 lines
block/blk-map.c +2 -1
index d3a94719f03f..702d68166689 100644
@@ -26,7 +26,8 @@ int blk_rq_append_bio(struct request *rq, struct bio **bio)
          } else {
                  if (!ll_back_merge_fn(rq->q, rq, *bio)) {
                          if (orig_bio != *bio) {
-                               bio_put(*bio);
+                               bio_inc_remaining(orig_bio);
+                               bio_endio(*bio);

'bio_inc_remaining(orig_bio);' shouldn't be needed since we don't chain bounced bio, otherwise this patch is fine.

I believe it is needed or at least desirable. The situation when the request bounced is like this

Quoted 8 lines

bio (bounced) . bi_private ---> orig_bio

and at the end of bounce_end_io, bio_endio(bio->bi_private) [which is bio_endio(orig_bio) in our case] is called. That doesn't have any effect on __blk_rq_map_user_iov; its bios have .bi_end_io==0, therefore one call more or less doesn't matter. However, for other callers, like osd_initiator.c, this is not the case. They pass bios which have bi_end_io, and might be surprised if this was called unexpectedly.

Quoted 5 lines

OK, I think it is good to follow the rule of not calling .bi_end_io() in the failure path, just like __blk_rq_map_user_iov()/blk_rq_map_kern().

But seems pscsi_map_sg() depends on bio_endio(orig_bio) to free the 'orig_bio', could you fix it in this patch too?

Yes, there seems to be leak in the error path of that code. However, that was there for more than a year so I didn't think that was urgent. I'll have a look at it, but I would prefer if someone familiar with pscsi had their say as well.

Quoted 7 lines
Quoted 5 lines

Before  caa4b02476e3, blk_rq_append_request wouldn't touch/delete the passed bio at all under any circumstances. I believe it should stay that way and incrementing the remaining counter, which effectively nullifies the extra bio_endio call, does that pretty straightforwardly.

Seems too tricky to use bio_inc_remaining() for avoiding bio_endio(orig_bio), if we have to do that, I suggest to add comment on that.

Okay. I thought that it didn't really reach the level of sophistication otherwise seen in block layer code :)

Regards

    Jiri Palecek

#10 lore raw

Re: [PATCH] Use bio_endio instead of bio_put in error path of blk_rq_append_bio

In reply to Ming Lei

Hello,

I had a look at the callers of blk_rq_append_bio and checked the callers. Some changes may need to be done there and I'd like the input of their maintainers as well before finalising the patch.

Quoted 35 lines · Ming Lei
Quoted 25 lines · Jiri Palecek
Quoted 6 lines · Ming Lei
Quoted 5 lines · Jiří Paleček

Avoids page leak from bounced requests

---
  block/blk-map.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 13 lines

block/blk-map.c int blk_rq_append_bio(struct request *rq, struct bio **bio) · line 30

Quoted 11 lines
block/blk-map.c +2 -1
index d3a94719f03f..702d68166689 100644
@@ -26,7 +26,8 @@ int blk_rq_append_bio(struct request *rq, struct bio **bio)
         } else {
                 if (!ll_back_merge_fn(rq->q, rq, *bio)) {
                         if (orig_bio != *bio) {
-                               bio_put(*bio);
+                               bio_inc_remaining(orig_bio);
+                               bio_endio(*bio);

'bio_inc_remaining(orig_bio);' shouldn't be needed since we don't chain bounced bio, otherwise this patch is fine.

I believe it is needed or at least desirable. The situation when the request bounced is like this

Quoted 8 lines

bio (bounced) . bi_private ---> orig_bio

and at the end of bounce_end_io, bio_endio(bio->bi_private) [which is bio_endio(orig_bio) in our case] is called. That doesn't have any effect on __blk_rq_map_user_iov; its bios have .bi_end_io==0, therefore one call more or less doesn't matter. However, for other callers, like osd_initiator.c, this is not the case. They pass bios which have bi_end_io, and might be surprised if this was called unexpectedly.

Quoted 5 lines

OK, I think it is good to follow the rule of not calling .bi_end_io() in the failure path, just like __blk_rq_map_user_iov()/blk_rq_map_kern().

But seems pscsi_map_sg() depends on bio_endio(orig_bio) to free the 'orig_bio', could you fix it in this patch too?

I've come up with the following patch. Some notes:

1) First of all, I think your suggestion to call bio_endio of the original bio in blk_rq_append_bio is right. It would make things a bit easier for the callers and those who I suspected need to hold on the bio are actually just ignorant about errors. However, it does change the api. So if you agree and the other parts are OK too, I'd make a patch without the bio_inc_remaining.

2) The osd_initiator.c seems to be a bit oblivious about errors, leaking not only the bios, but the request as well. I added some functions for proper cleanup there, with considerations:

- I think it's better to unhook the .bi_next link of the bio before adding it to the request, because blk_rq_append_bio uses that for its own purposes.

- Once the bios from osd_request have been spent (added to a blk_request), they can be NULL-ified.

I'd like hear if these are OK.

3) PSCSI needs to free the bios in pscsi_map_sg, but also needs to clear the bios from the request in pscsi_execute_cmd in case of partial errors (ie. first sg segment makes it to the request, second fails). To my understanding, blk_put_request is insufficient in that case.

Regards
Jiri Palecek

Quoted 1 line

From fca495c6bf41775be152e7f7f00be6f0dc746ac3 Mon Sep 17 00:00:00 2001

From: =?iso8859-2?q?Ji=F8=ED=20Pale=E8ek?= <[email protected]>
Date: Sun, 4 Feb 2018 21:55:56 +0100
Subject: [PATCH 2/2] Some error paths

---
 block/blk-map.c                    |  4 +++-
 drivers/scsi/osd/osd_initiator.c   | 29 +++++++++++++++++++++++++----
 drivers/target/target_core_pscsi.c |  4 +++-
 3 files changed, 31 insertions(+), 6 deletions(-)
block/blk-map.c +1 -1
index 702d68166689..8378f4ddd419 100644
@@ -246,7 +246,7 @@ int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
 	ret = blk_rq_append_bio(rq, &bio);
 	if (unlikely(ret)) {
 		/* request is too big */
-		bio_put(orig_bio);
+		bio_endio(orig_bio);
 		return ret;
 	}
 
drivers/scsi/osd/osd_initiator.c +25 -4
index e18877177f1b..f352fdda52b9 100644
@@ -437,6 +437,16 @@ static void _osd_free_seg(struct osd_request *or __unused,
 	seg->alloc_size = 0;
 }
 
+static void _osd_end_bios(struct bio *bio)
+{
+	struct bio *nxt = NULL;
+	while (bio) {
+		nxt = bio->bi_next;
+		bio_endio(bio);
+		bio = nxt;
+	}
+}
+
 static void _put_request(struct request *rq)
 {
 	/*
@@ -469,6 +479,10 @@ void osd_end_request(struct osd_request *or)
 	_osd_free_seg(or, &or->set_attr);
 	_osd_free_seg(or, &or->cdb_cont);
 
+	/* Only in case of errors should these be non-NULL */
+	_osd_end_bios(or->in.bio);
+	_osd_end_bios(or->out.bio);
+
 	_osd_request_free(or);
 }
 EXPORT_SYMBOL(osd_end_request);
@@ -1575,13 +1589,20 @@ static struct request *_make_request(struct request_queue *q, bool has_write,
 	if (IS_ERR(req))
 		return req;
 
-	for_each_bio(bio) {
-		struct bio *bounce_bio = bio;
+	while(bio) {
+		struct bio *nxt = bio->bi_next;
+		bio->bi_next = NULL;
 
-		ret = blk_rq_append_bio(req, &bounce_bio);
-		if (ret)
+		ret = blk_rq_append_bio(req, &bio);
+		if (ret) {
+			_put_request(req);
+			bio_endio(bio);
+			oii->bio = nxt;
 			return ERR_PTR(ret);
+		}
+		bio = nxt;
 	}
+	oii->bio = NULL;
 
 	return req;
 }
drivers/target/target_core_pscsi.c +3 -1
index 0d99b242e82e..42c24356d683 100644
@@ -922,6 +922,7 @@ pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
 
 				rc = blk_rq_append_bio(req, &bio);
 				if (rc) {
+					bio_put(bio);
 					pr_err("pSCSI: failed to append bio\n");
 					goto fail;
 				}
@@ -940,6 +941,7 @@ pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
 	if (bio) {
 		rc = blk_rq_append_bio(req, &bio);
 		if (rc) {
+			bio_put(bio);
 			pr_err("pSCSI: failed to append bio\n");
 			goto fail;
 		}
@@ -1016,7 +1018,7 @@ pscsi_execute_cmd(struct se_cmd *cmd)
 	return 0;
 
 fail_put_request:
-	blk_put_request(req);
+	blk_end_request_all(req, BLK_STS_IOERR);
 fail:
 	kfree(pt);
 	return ret;
Signature · 1 line
2.15.1
#11 lore raw

Re: [PATCH] Use bio_endio instead of bio_put in error path of blk_rq_append_bio

Quoted 62 lines · Jiri Palecek

Hello,

I had a look at the callers of blk_rq_append_bio and checked the callers. Some changes may need to be done there and I'd like the input of their maintainers as well before finalising the patch.

Quoted 35 lines · Ming Lei
Quoted 25 lines · Jiri Palecek
Quoted 6 lines · Ming Lei
Quoted 5 lines · Jiří Paleček

Avoids page leak from bounced requests

---
  block/blk-map.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 13 lines

block/blk-map.c int blk_rq_append_bio(struct request *rq, struct bio **bio) · line 30

Quoted 11 lines
block/blk-map.c +2 -1
index d3a94719f03f..702d68166689 100644
@@ -26,7 +26,8 @@ int blk_rq_append_bio(struct request *rq, struct bio **bio)
         } else {
                 if (!ll_back_merge_fn(rq->q, rq, *bio)) {
                         if (orig_bio != *bio) {
-                               bio_put(*bio);
+                               bio_inc_remaining(orig_bio);
+                               bio_endio(*bio);

'bio_inc_remaining(orig_bio);' shouldn't be needed since we don't chain bounced bio, otherwise this patch is fine.

I believe it is needed or at least desirable. The situation when the request bounced is like this

Quoted 8 lines

bio (bounced) . bi_private ---> orig_bio

and at the end of bounce_end_io, bio_endio(bio->bi_private) [which is bio_endio(orig_bio) in our case] is called. That doesn't have any effect on __blk_rq_map_user_iov; its bios have .bi_end_io==0, therefore one call more or less doesn't matter. However, for other callers, like osd_initiator.c, this is not the case. They pass bios which have bi_end_io, and might be surprised if this was called unexpectedly.

Quoted 5 lines

OK, I think it is good to follow the rule of not calling .bi_end_io() in the failure path, just like __blk_rq_map_user_iov()/blk_rq_map_kern().

But seems pscsi_map_sg() depends on bio_endio(orig_bio) to free the 'orig_bio', could you fix it in this patch too?

I've come up with the following patch. Some notes:

1) First of all, I think your suggestion to call bio_endio of the original bio in blk_rq_append_bio is right. It would make things a bit easier for the callers and those who I suspected need to hold on the bio are actually just ignorant about errors. However, it does change the api. So if you agree and the other parts are OK too, I'd make a patch without the bio_inc_remaining.

2) The osd_initiator.c seems to be a bit oblivious about errors, leaking not only the bios, but the request as well. I added some functions for proper cleanup there, with considerations:

Quoted 7 lines

- I think it's better to unhook the .bi_next link of the bio before adding it to the request, because blk_rq_append_bio uses that for its own purposes.

- Once the bios from osd_request have been spent (added to a blk_request), they can be NULL-ified.

I have not followed closely the issue but ...

No! the osd_initiator.c is completely out of scope. And does not leak any bios and need not to be fixed. let me explain.

These are BIDI commands that travel as a couple of chained requests. They are sent as BLOCK_PC command and complete or fail as one hole unit. The system is not allowed (And does not know how) to split them or complete them partially. This is not an FS read or write request of data. But a unique OSD request that the system knows nothing about. The all scsi-command is specially crafted by the caller. It is all properly destroyed at osd_request end of life (sync or async) (NOTE there is no bio-end function only req-end)

again not followed closely but if it is about the free of the bounce buffers then I would just disable bouncing for osd all together. There are only a very few (2) drivers that support bidi for osd. iscsi and iser so we know those are totally cool and this is all not an existing problem.

Thanks
Boaz

Quoted 11 lines

I'd like hear if these are OK.

3) PSCSI needs to free the bios in pscsi_map_sg, but also needs to clear the bios from the request in pscsi_execute_cmd in case of partial errors (ie. first sg segment makes it to the request, second fails). To my understanding, blk_put_request is insufficient in that case.

Regards
Jiri Palecek

#12 lore raw

Re: [PATCH] Use bio_endio instead of bio_put in error path of blk_rq_append_bio

On 21/02/18 18:20, Boaz Harrosh wrote:
<>

Quoted 5 lines

again not followed closely but if it is about the free of the bounce buffers then I would just disable bouncing for osd all together. There are only a very few (2) drivers that support bidi for osd. iscsi and iser so we know those are totally cool and this is all not an existing problem.

And also it is possible that I have at all not understood the issue please bring me up to speed. I would like to help any way I can.

Quoted 3 lines

Thanks
Boaz

A message that is not in the archive (sent off-list or not yet received): [email protected]

#14 lore raw

Re: [PATCH] Use bio_endio instead of bio_put in error path of blk_rq_append_bio

On 27/02/18 16:44, Jiri Palecek wrote:
<>

Quoted 11 lines
Quoted 7 lines

These are BIDI commands that travel as a couple of chained requests. They are sent as BLOCK_PC command and complete or fail as one hole unit. The system is not allowed (And does not know how) to split them or complete them partially. This is not an FS read or write request of data. But a unique OSD request that the system knows nothing about. The all scsi-command is specially crafted by the caller. It is all properly destroyed at osd_request end of life (sync or async) (NOTE there is no bio-end function only req-end)

That's correct, however, the assumed leak would happen when the preparation of the request fails.

Quoted 7 lines

The whole issue started with possible bounce buffer leaks in blk_rq_append_request, which didn't happen before it started to handle bouncing. However, Ming Lei noticed it would be simpler and easier to also free the original bio, as opposed to just the bounce buffer, on error. Because that wasn't so previously, the question now stands, what do the callers likely expect?

Sigh! That bouncing thing. It is so not relevant for osd. I wish we could just drop the bouncing and return an error, if it was ever needed (because it never is for osd)

Quoted 7 lines

This leads us to osd_initiator.c, which IMHO doesn't expect anything and simply lacks any sane handling of such possibility. If you say there can't be any leaks, please explain to me this riddle. Given the code:

static struct request *_make_request(struct request_queue *q, bool has_write,
struct _osd_io_info *oii, gfp_t flags)

Quoted 14 lines
{
    struct request *req;
    struct bio *bio = oii->bio;
    int ret;

    req = blk_get_request(q, has_write ? REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN,
            flags); /** 1 **/
    if (IS_ERR(req))
        return req;

    for_each_bio(bio) {
        struct bio *bounce_bio = bio;

        ret = blk_rq_append_bio(req, &bounce_bio);

[Sorry still did not see the all code]
What is the meaning of the pointer-to-pointer-to-bio here? it used to be relevant to blk_queue_bounce, that I guess you internalized into blk_rq_append_bio() but why do we need it outside the call now?

Quoted 9 lines
        if (ret)
            return ERR_PTR(ret); /** 2 **/
    }

    return req;
}

1. If this function exits by line labeled "2", where is struct request *req allocated on line 1 freed?

You are probably right, if you want to fix it? This used to never fail because blk_queue_bounce() retuning void. And blk_rq_append_bio could only fail if ll_back_merge_fn would fail, but for OSD supporting drivers this would never fail. And also those checks were already preformed before (when more segments were added).

So in practice this could never fail and never showed in testing. But I agree this is a layering violation and the code is wrong.

Quoted 5 lines

2. If this function exits by line 2, where are the bio(s) in oii->bio freed? What if it fails on the second bio?

I assume osd_end_request would be called afterwards (as in exofs_read_kern).

This code always scared me to bits because if you look closely it does nothing It does not actually builds the chain it relays on bio->bi_next not being modified and so we loop on doing nothing (resting the bio->bi_next to the same thing it was before)

So yes oii->bio(s) are freed in osd_end_request and/or by callers because the bios might have one more ref on them, because they might be needed by caller to finish up.

Quoted 3 lines

Regards
Jiri Palecek

Thank you for looking into this, sorry for the headache this code gives you. If there was a way to just fail if bounce is needed that could be just fine for osd. But else I guess that error exit needs fixing.

Based on v4.15 code feel free to add to your patchset where needed

----
Don't leak the request if blk_rq_append_bio fails

----

drivers/scsi/osd/osd_initiator.c +3 -1
index 2f2a991..3c97e0e 100644
@@ -1572,8 +1572,10 @@ static struct request *_make_request(struct request_queue *q, bool has_write,
 
 		blk_queue_bounce(req->q, &bounce_bio);
 		ret = blk_rq_append_bio(req, bounce_bio);
-		if (ret)
+		if (ret) {
+			_put_request(req);
 			return ERR_PTR(ret);
+		}
 	}
 
 	return req;
#15 lore raw

Re: [PATCH] Use bio_endio instead of bio_put in error path of blk_rq_append_bio

On 28/02/18 19:38, Boaz Harrosh wrote:
<>

Quoted 3 lines

Based on v4.15 code feel free to add to your patchset where needed

Sorry is what happens when you work on so many Linux versions at the same time. This one is based on v4.15

Quoted 5 lines

----
Don't leak the request if blk_rq_append_bio fails

----

drivers/scsi/osd/osd_initiator.c +3 -1
index e188771..58782dc 100644
@@ -1579,8 +1579,10 @@ static struct request *_make_request(struct request_queue *q, bool has_write,
 		struct bio *bounce_bio = bio;
 
 		ret = blk_rq_append_bio(req, &bounce_bio);
-		if (ret)
+		if (ret) {
+			_put_request(req);
 			return ERR_PTR(ret);
+		}
 	}
 
 	return req;
maintainer
#16 lore raw
In reply to Ming Lei
Quoted 6 lines · Ming Lei

Hi Jens,

The 1st patch makes sure that passthrough IO won't enter .make_request_fn.

The 2nd one fixes blk_rq_append_bio(), which is from your post with small change on failure handling.

Thanks Ming, queued up.

Signature · 1 line
Jens Axboe
#17 lore raw

Just got a confirmation that this patch set, when applied to the 4.14 branch, fixes the issue.

Quoted 13 lines · Jens Axboe
Quoted 6 lines · Ming Lei

Hi Jens,

The 1st patch makes sure that passthrough IO won't enter .make_request_fn.

The 2nd one fixes blk_rq_append_bio(), which is from your post with small change on failure handling.

Thanks Ming, queued up.

--
Jens Axboe

#18 lore raw
In reply to Jens Axboe

Will this set make it to the 4.14.x stable branch?

vb

Quoted 13 lines · Jens Axboe
Quoted 6 lines · Ming Lei

Hi Jens,

The 1st patch makes sure that passthrough IO won't enter .make_request_fn.

The 2nd one fixes blk_rq_append_bio(), which is from your post with small change on failure handling.

Thanks Ming, queued up.

--
Jens Axboe

author
#19 lore raw

Hi Vincent,

Quoted 1 line · Vincent Batts

Will this set make it to the 4.14.x stable branch?

Greg has posted out the two patches for 4.14-stable review:

	https://marc.info/?l=linux-kernel&m=151481772722222&w=2

So it will be in 4.14.x stable branch soon.

Thanks,
Ming

Quoted 17 lines

vb

Quoted 13 lines · Jens Axboe
Quoted 6 lines · Ming Lei

Hi Jens,

The 1st patch makes sure that passthrough IO won't enter .make_request_fn.

The 2nd one fixes blk_rq_append_bio(), which is from your post with small change on failure handling.

Thanks Ming, queued up.

--
Jens Axboe

Signature · 1 line
Ming