Skip to contentSkip to conversation

blk-mq: Return invalid cookie if bio was split

PATCH In review Keith Busch 2 participants 8 messages Sep 26, 2016 – Oct 6, 2016

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

PATCH 1/1 blk-mq: Return invalid cookie if bio was split

The only user of polling requires its original request be completed in its entirety before continuing execution. If the bio needs to be split and chained for any reason, the direct IO path would have waited for just that split portion to complete, leading to potential data corruption if the remaining transfer has not yet completed.

This patch has blk-mq return an invalid cookie if a bio requires splitting so that polling does not occur.

---
 block/blk-mq.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
block/blk-mq.c +4 -2
index c207fa9..6385985 100644
@@ -1311,6 +1311,7 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
 	unsigned int request_count = 0;
 	struct blk_plug *plug;
 	struct request *same_queue_rq = NULL;
+	struct bio *orig = bio;
 	blk_qc_t cookie;
 
 	blk_queue_bounce(q, &bio);
@@ -1389,7 +1390,7 @@ run_queue:
 	}
 	blk_mq_put_ctx(data.ctx);
 done:
-	return cookie;
+	return bio == orig ? cookie : BLK_QC_T_NONE;
 }
 
 /*
@@ -1404,6 +1405,7 @@ static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
 	unsigned int request_count = 0;
 	struct blk_map_ctx data;
 	struct request *rq;
+	struct bio *orig = bio;
 	blk_qc_t cookie;
 
 	blk_queue_bounce(q, &bio);
@@ -1467,7 +1469,7 @@ run_queue:
 	}
 
 	blk_mq_put_ctx(data.ctx);
-	return cookie;
+	return bio == orig ? cookie : BLK_QC_T_NONE;
 }
 
 /*
Signature · 1 line
2.7.2
#1 lore raw
Quoted 5 lines · On Mon, 26 Sep 2016 19:00:30 -0400 Keith Busch <[email protected]> wrote:

The only user of polling requires its original request be completed in its entirety before continuing execution. If the bio needs to be split and chained for any reason, the direct IO path would have waited for just that split portion to complete, leading to potential data corruption if the remaining transfer has not yet completed.

The issue looks a bit tricky because there is no per-bio place for holding the cookie, and generic_make_request() only returns the cookie for the last bio in the current bio list, so maybe we need the following patch too.

Also seems merge case need to take care of too.

---

block/blk-core.c +5 -1
index 14d7c0740dc0..f1ab547173f8 100644
@@ -1996,6 +1996,8 @@ blk_qc_t generic_make_request(struct bio *bio)
 {
 	struct bio_list bio_list_on_stack;
 	blk_qc_t ret = BLK_QC_T_NONE;
+	blk_qc_t lret;
+	const struct bio *orig = bio;
 
 	if (!generic_make_request_checks(bio))
 		goto out;
@@ -2036,7 +2038,9 @@ blk_qc_t generic_make_request(struct bio *bio)
 		struct request_queue *q = bdev_get_queue(bio->bi_bdev);
 
 		if (likely(blk_queue_enter(q, false) == 0)) {
-			ret = q->make_request_fn(q, bio);
+			lret = q->make_request_fn(q, bio);
+			if (bio == orig)
+				ret = lret;
 
 			blk_queue_exit(q);
 
Quoted 47 lines

This patch has blk-mq return an invalid cookie if a bio requires splitting so that polling does not occur.

---
 block/blk-mq.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
block/blk-mq.c +4 -2
index c207fa9..6385985 100644
@@ -1311,6 +1311,7 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
 	unsigned int request_count = 0;
 	struct blk_plug *plug;
 	struct request *same_queue_rq = NULL;
+	struct bio *orig = bio;
 	blk_qc_t cookie;
 
 	blk_queue_bounce(q, &bio);
@@ -1389,7 +1390,7 @@ run_queue:
 	}
 	blk_mq_put_ctx(data.ctx);
 done:
-	return cookie;
+	return bio == orig ? cookie : BLK_QC_T_NONE;
 }
 
 /*
@@ -1404,6 +1405,7 @@ static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
 	unsigned int request_count = 0;
 	struct blk_map_ctx data;
 	struct request *rq;
+	struct bio *orig = bio;
 	blk_qc_t cookie;
 
 	blk_queue_bounce(q, &bio);
@@ -1467,7 +1469,7 @@ run_queue:
 	}
 
 	blk_mq_put_ctx(data.ctx);
-	return cookie;
+	return bio == orig ? cookie : BLK_QC_T_NONE;
 }
 
 /*
#2 lore raw
Quoted 9 lines · Ming Lei
Quoted 5 lines · On Mon, 26 Sep 2016 19:00:30 -0400 Keith Busch <[email protected]> wrote:

The only user of polling requires its original request be completed in its entirety before continuing execution. If the bio needs to be split and chained for any reason, the direct IO path would have waited for just that split portion to complete, leading to potential data corruption if the remaining transfer has not yet completed.

Quoted 5 lines

The issue looks a bit tricky because there is no per-bio place for holding the cookie, and generic_make_request() only returns the cookie for the last bio in the current bio list, so maybe we need the following patch too.

Also seems merge case need to take care of too.

Yah, I think you're right. I'll try to work out a test case that more readily exposes the problem so I can better verify the fix.

#3 lore raw
In reply to Ming Lei
Quoted 9 lines · Ming Lei
Quoted 5 lines · On Mon, 26 Sep 2016 19:00:30 -0400 Keith Busch <[email protected]> wrote:

The only user of polling requires its original request be completed in its entirety before continuing execution. If the bio needs to be split and chained for any reason, the direct IO path would have waited for just that split portion to complete, leading to potential data corruption if the remaining transfer has not yet completed.

Quoted 3 lines

The issue looks a bit tricky because there is no per-bio place for holding the cookie, and generic_make_request() only returns the cookie for the last bio in the current bio list, so maybe we need the following patch too.

I'm looking more into this, and I can't see why we're returning a cookie to poll on in the first place. blk_poll is only invoked when we could have called io_schedule, so we expect the task state gets set to TASK_RUNNING when all the work completes. So why do we need to poll for a specific tag instead of polling until task state is set back to running?

I've tried this out and it seems to work fine, and should fix any issues from split IO requests. It also helps direct IO polling, since it can have a list of bios, but can only save one cookie.

#4 lore raw
Quoted 14 lines · Keith Busch
Quoted 9 lines · Ming Lei
Quoted 5 lines · On Mon, 26 Sep 2016 19:00:30 -0400 Keith Busch <[email protected]> wrote:

The only user of polling requires its original request be completed in its entirety before continuing execution. If the bio needs to be split and chained for any reason, the direct IO path would have waited for just that split portion to complete, leading to potential data corruption if the remaining transfer has not yet completed.

Quoted 3 lines

The issue looks a bit tricky because there is no per-bio place for holding the cookie, and generic_make_request() only returns the cookie for the last bio in the current bio list, so maybe we need the following patch too.

Quoted 5 lines

I'm looking more into this, and I can't see why we're returning a cookie to poll on in the first place. blk_poll is only invoked when we could have called io_schedule, so we expect the task state gets set to TASK_RUNNING when all the work completes. So why do we need to poll for a specific tag instead of polling until task state is set back to running?

But .poll() need to check if the specific request is completed or not, then blk_poll() can set 'current' as RUNNING if it is completed.

blk_poll():

                ...
                ret = q->mq_ops->poll(hctx, blk_qc_t_to_tag(cookie));
                if (ret > 0) {
                        hctx->poll_success++;
                        set_current_state(TASK_RUNNING);
                        return true;
                }
                ...
Quoted 4 lines

I've tried this out and it seems to work fine, and should fix any issues from split IO requests. It also helps direct IO polling, since it can have a list of bios, but can only save one cookie.

I am glad to take a look the patch if you post it out.

Thanks,
Ming Lei

#5 lore raw
Quoted 11 lines · Ming Lei

But .poll() need to check if the specific request is completed or not, then blk_poll() can set 'current' as RUNNING if it is completed.

blk_poll():

                ...
                ret = q->mq_ops->poll(hctx, blk_qc_t_to_tag(cookie));
                if (ret > 0) {
                        hctx->poll_success++;
                        set_current_state(TASK_RUNNING);
                        return true;
                }

Right, but the task could be waiting on a whole lot more than just that one tag, so setting the task to running before knowing those all complete doesn't sound right.

Quoted 1 line

I am glad to take a look the patch if you post it out.

Here's what I got for block + nvme. It relies on all the requests to complete to set the task back to running.

---

block/blk-core.c +5 -15
index b993f88..3c1cfbf 100644
@@ -3342,6 +3342,8 @@ EXPORT_SYMBOL(blk_finish_plug);
 bool blk_poll(struct request_queue *q, blk_qc_t cookie)
 {
 	struct blk_plug *plug;
+	struct blk_mq_hw_ctx *hctx;
+	unsigned int queue_num;
 	long state;
 
 	if (!q->mq_ops || !q->mq_ops->poll || !blk_qc_t_valid(cookie) ||
@@ -3353,27 +3355,15 @@ bool blk_poll(struct request_queue *q, blk_qc_t cookie)
 		blk_flush_plug_list(plug, false);
 
 	state = current->state;
+	queue_num = blk_qc_t_to_queue_num(cookie);
+	hctx = q->queue_hw_ctx[queue_num];
 	while (!need_resched()) {
-		unsigned int queue_num = blk_qc_t_to_queue_num(cookie);
-		struct blk_mq_hw_ctx *hctx = q->queue_hw_ctx[queue_num];
-		int ret;
-
-		hctx->poll_invoked++;
-
-		ret = q->mq_ops->poll(hctx, blk_qc_t_to_tag(cookie));
-		if (ret > 0) {
-			hctx->poll_success++;
-			set_current_state(TASK_RUNNING);
-			return true;
-		}
+		q->mq_ops->poll(hctx);
 
 		if (signal_pending_state(state, current))
 			set_current_state(TASK_RUNNING);
-
 		if (current->state == TASK_RUNNING)
 			return true;
-		if (ret < 0)
-			break;
 		cpu_relax();
 	}
 
drivers/nvme/host/pci.c +3 -16
index befac5b..2e359e0 100644
@@ -649,7 +651,7 @@ static inline bool nvme_cqe_valid(struct nvme_queue *nvmeq, u16 head,
 	return (le16_to_cpu(nvmeq->cqes[head].status) & 1) == phase;
 }
 
-static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
+static void nvme_process_cq(struct nvme_queue *nvmeq)
 {
 	u16 head, phase;
 
@@ -665,9 +667,6 @@ static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
 			phase = !phase;
 		}
 
-		if (tag && *tag == cqe.command_id)
-			*tag = -1;
-
 		if (unlikely(cqe.command_id >= nvmeq->q_depth)) {
 			dev_warn(nvmeq->dev->ctrl.device,
 				"invalid id %d completed on queue %d\n",
@@ -711,11 +710,6 @@ static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
 	nvmeq->cqe_seen = 1;
 }
 
-static void nvme_process_cq(struct nvme_queue *nvmeq)
-{
-	__nvme_process_cq(nvmeq, NULL);
-}
-
 static irqreturn_t nvme_irq(int irq, void *data)
 {
 	irqreturn_t result;
@@ -736,20 +730,15 @@ static irqreturn_t nvme_irq_check(int irq, void *data)
 	return IRQ_NONE;
 }
 
-static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
+static void nvme_poll(struct blk_mq_hw_ctx *hctx)
 {
 	struct nvme_queue *nvmeq = hctx->driver_data;
 
 	if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase)) {
 		spin_lock_irq(&nvmeq->q_lock);
-		__nvme_process_cq(nvmeq, &tag);
+		nvme_process_cq(nvmeq);
 		spin_unlock_irq(&nvmeq->q_lock);
-
-		if (tag == -1)
-			return 1;
 	}
-
-	return 0;
 }
 
 static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl, int aer_idx)
include/linux/blk-mq.h +1 -1
index 2498fdf..a723af9 100644
@@ -100,7 +100,7 @@ typedef void (exit_request_fn)(void *, struct request *, unsigned int,
 typedef void (busy_iter_fn)(struct blk_mq_hw_ctx *, struct request *, void *,
 		bool);
 typedef void (busy_tag_iter_fn)(struct request *, void *, bool);
-typedef int (poll_fn)(struct blk_mq_hw_ctx *, unsigned int);
+typedef void (poll_fn)(struct blk_mq_hw_ctx *);
 
 
 struct blk_mq_ops {

--

#6 lore raw

Hi Keith,

Quoted 14 lines · Keith Busch
Quoted 11 lines · Ming Lei

But .poll() need to check if the specific request is completed or not, then blk_poll() can set 'current' as RUNNING if it is completed.

blk_poll():

                ...
                ret = q->mq_ops->poll(hctx, blk_qc_t_to_tag(cookie));
                if (ret > 0) {
                        hctx->poll_success++;
                        set_current_state(TASK_RUNNING);
                        return true;
                }
Quoted 8 lines

Right, but the task could be waiting on a whole lot more than just that one tag, so setting the task to running before knowing those all complete doesn't sound right.

Quoted 1 line

I am glad to take a look the patch if you post it out.

Here's what I got for block + nvme. It relies on all the requests to complete to set the task back to running.

Yeah, but your patch doesn't add that change, and looks 'task_struct *' in submission path need to be stored in request or somewhere else.

There are some issues with current polling approach:
- in dio, one dio may include lots of bios, but only the last submitted bio is polled
- one bio can be splitted into several bios, but submit_bio() only returns one cookie for polling

Looks your approach via polling current state can fix this issue.

Quoted 35 lines

---

block/blk-core.c +5 -12
index b993f88..3c1cfbf 100644
@@ -3342,6 +3342,8 @@ EXPORT_SYMBOL(blk_finish_plug);
 bool blk_poll(struct request_queue *q, blk_qc_t cookie)
 {
        struct blk_plug *plug;
+       struct blk_mq_hw_ctx *hctx;
+       unsigned int queue_num;
        long state;
 
        if (!q->mq_ops || !q->mq_ops->poll || !blk_qc_t_valid(cookie) ||
@@ -3353,27 +3355,15 @@ bool blk_poll(struct request_queue *q, blk_qc_t cookie)
                blk_flush_plug_list(plug, false);
 
        state = current->state;
+       queue_num = blk_qc_t_to_queue_num(cookie);
+       hctx = q->queue_hw_ctx[queue_num];
        while (!need_resched()) {
-               unsigned int queue_num = blk_qc_t_to_queue_num(cookie);
-               struct blk_mq_hw_ctx *hctx = q->queue_hw_ctx[queue_num];
-               int ret;
-
-               hctx->poll_invoked++;
-
-               ret = q->mq_ops->poll(hctx, blk_qc_t_to_tag(cookie));
-               if (ret > 0) {
-                       hctx->poll_success++;
-                       set_current_state(TASK_RUNNING);
-                       return true;
-               }
+               q->mq_ops->poll(hctx);

block/blk-core.c bool blk_poll(struct request_queue *q, blk_qc_t cookie) · line 3368

Quoted 10 lines
block/blk-core.c +0 -3
 
                if (signal_pending_state(state, current))
                        set_current_state(TASK_RUNNING);
-
                if (current->state == TASK_RUNNING)
                        return true;
-               if (ret < 0)
-                       break;
                cpu_relax();
        }

Then looks the whole hw queue is polled and only the queue num in cookie matters.

In theory, there might be one race:

- one dio need to submit several bios(suppose there are two bios: A and B)
- A is submitted to hardware queue M
- B is submitted to hardware queue N because the current task is migrated to another CPU
- then only hardware queue N is polled

Quoted 73 lines
drivers/nvme/host/pci.c +3 -16
index befac5b..2e359e0 100644
@@ -649,7 +651,7 @@ static inline bool nvme_cqe_valid(struct nvme_queue *nvmeq, u16 head,
        return (le16_to_cpu(nvmeq->cqes[head].status) & 1) == phase;
 }
 
-static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
+static void nvme_process_cq(struct nvme_queue *nvmeq)
 {
        u16 head, phase;
 
@@ -665,9 +667,6 @@ static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
                        phase = !phase;
                }
 
-               if (tag && *tag == cqe.command_id)
-                       *tag = -1;
-
                if (unlikely(cqe.command_id >= nvmeq->q_depth)) {
                        dev_warn(nvmeq->dev->ctrl.device,
                                "invalid id %d completed on queue %d\n",
@@ -711,11 +710,6 @@ static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
        nvmeq->cqe_seen = 1;
 }
 
-static void nvme_process_cq(struct nvme_queue *nvmeq)
-{
-       __nvme_process_cq(nvmeq, NULL);
-}
-
 static irqreturn_t nvme_irq(int irq, void *data)
 {
        irqreturn_t result;
@@ -736,20 +730,15 @@ static irqreturn_t nvme_irq_check(int irq, void *data)
        return IRQ_NONE;
 }
 
-static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
+static void nvme_poll(struct blk_mq_hw_ctx *hctx)
 {
        struct nvme_queue *nvmeq = hctx->driver_data;
 
        if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase)) {
                spin_lock_irq(&nvmeq->q_lock);
-               __nvme_process_cq(nvmeq, &tag);
+               nvme_process_cq(nvmeq);
                spin_unlock_irq(&nvmeq->q_lock);
-
-               if (tag == -1)
-                       return 1;
        }
-
-       return 0;
 }
 
 static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl, int aer_idx)
include/linux/blk-mq.h +1 -2
index 2498fdf..a723af9 100644
@@ -100,7 +100,7 @@ typedef void (exit_request_fn)(void *, struct request *, unsigned int,
 typedef void (busy_iter_fn)(struct blk_mq_hw_ctx *, struct request *, void *,
                bool);
 typedef void (busy_tag_iter_fn)(struct request *, void *, bool);
-typedef int (poll_fn)(struct blk_mq_hw_ctx *, unsigned int);
+typedef void (poll_fn)(struct blk_mq_hw_ctx *);
 
 
 struct blk_mq_ops {
--
Signature · 1 line
Ming Lei
#7 lore raw
Quoted 21 lines · Ming Lei

Hi Keith,

Quoted 18 lines · Keith Busch
Quoted 11 lines · Ming Lei

But .poll() need to check if the specific request is completed or not, then blk_poll() can set 'current' as RUNNING if it is completed.

blk_poll():

                ...
                ret = q->mq_ops->poll(hctx, blk_qc_t_to_tag(cookie));
                if (ret > 0) {
                        hctx->poll_success++;
                        set_current_state(TASK_RUNNING);
                        return true;
                }

Right, but the task could be waiting on a whole lot more than just that one tag, so setting the task to running before knowing those all complete doesn't sound right.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I am glad to take a look the patch if you post it out.

Here's what I got for block + nvme. It relies on all the requests to complete to set the task back to running.

Yeah, but your patch doesn't add that change, and looks 'task_struct *' in submission path need to be stored in request or somewhere else.

The polling function shouldn't have to set the task to running. The task_struct is the dio's "waiter", and dio_bio_end_io sets its state to noromal running when every bio submitted and split chained bios complete. Hopefully those all complete through the ->poll(), and blk_poll will automatically observe the state changed.

Quoted 10 lines

Then looks the whole hw queue is polled and only the queue num in cookie matters.

In theory, there might be one race:

- one dio need to submit several bios(suppose there are two bios: A and B)
- A is submitted to hardware queue M
- B is submitted to hardware queue N because the current task is migrated to another CPU
- then only hardware queue N is polled

Yeah, in that case we'd rely on the queue M's interrupt handler to do the completion.

Avoiding the context switch was the biggest win for polling, as I understand it. If the task is being migrated to other CPUs, I think we've already lost the latency benefit we'd have got.