Skip to contentSkip to conversation

next round of blk-mq updates

PATCH In review Christoph Hellwig 2 participants 15 messages Apr 16, 2014

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

Hi Jens,

these are the final blk-mq changes for a fully working SCSI midlayer using blk-mq.

Summary of the changes:

- a new split I/O completion handler that allow the driver to free ressources when it knows a request will be fully completed, but before it has been freed
- support for bidirectional requests, which is very trivial when used with the above split I/O completion handler.
- support to requeue a request that already entered the driver, which is needed by the SCSI midlayer to support partial completions as well as various error conditions.
- a couple of new ways to poke a queue:
- an equivalent to blk_delay_queue to wake a stopped queue after a delay
- a new function to kick a queue that might be stopped or not
- a parameter to blk_mq_start_stopped_hw_queues so that it can be called from (soft)irq context

#2 lore raw

PATCH 2/8 blk-mq: bidi support

Add two unlinkely branches to make sure the resid is initialized correctly for bidi request pairs, and the second request gets properly freed.

---
 block/blk-mq.c |    9 +++++++--
 block/bsg.c    |    2 +-
 2 files changed, 8 insertions(+), 3 deletions(-)
block/blk-mq.c +7 -2
index e921085..afdab13 100644
@@ -298,10 +298,13 @@ inline void __blk_mq_end_io(struct request *rq, int error)
 {
 	blk_account_io_done(rq);
 
-	if (rq->end_io)
+	if (rq->end_io) {
 		rq->end_io(rq, error);
-	else
+	} else {
+		if (unlikely(blk_bidi_rq(rq)))
+			blk_mq_free_request(rq->next_rq);
 		blk_mq_free_request(rq);
+	}
 }
 EXPORT_SYMBOL(__blk_mq_end_io);
 
@@ -366,6 +369,8 @@ static void blk_mq_start_request(struct request *rq, bool last)
 	trace_block_rq_issue(q, rq);
 
 	rq->resid_len = blk_rq_bytes(rq);
+	if (unlikely(blk_bidi_rq(rq)))
+		rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
 
 	/*
 	 * Just mark start time and set the started bit. Due to memory
block/bsg.c +1 -1
index 420a5a9..2956086 100644
@@ -1008,7 +1008,7 @@ int bsg_register_queue(struct request_queue *q, struct device *parent,
 	/*
 	 * we need a proper transport to send commands, not a stacked device
 	 */
-	if (!q->request_fn)
+	if (!q->request_fn && !q->mq_ops)
 		return 0;
 
 	bcd = &q->bsg_dev;
Signature · 1 line
1.7.10.4
#11 lore raw

PATCH 5/8 blk-mq: add blk_mq_start_hw_queues

In reply to Christoph Hellwig

Add a helper to unconditionally kick contexts of a queue. This will be needed by the SCSI layer to provide fair queueing between multiple devices on a single host.

---
 block/blk-mq.c         |   11 +++++++++++
 include/linux/blk-mq.h |    1 +
 2 files changed, 12 insertions(+)
block/blk-mq.c +11 -0
index 128b5e5..7946f0b 100644
@@ -699,6 +699,17 @@ void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
 }
 EXPORT_SYMBOL(blk_mq_start_hw_queue);
 
+void blk_mq_start_hw_queues(struct request_queue *q)
+{
+	struct blk_mq_hw_ctx *hctx;
+	int i;
+
+	queue_for_each_hw_ctx(q, hctx, i)
+		blk_mq_start_hw_queue(hctx);
+}
+EXPORT_SYMBOL(blk_mq_start_hw_queues);
+
+
 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
 {
 	struct blk_mq_hw_ctx *hctx;
include/linux/blk-mq.h +1 -0
index ae868e7..391377e 100644
@@ -158,6 +158,7 @@ void blk_mq_complete_request(struct request *rq);
 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx);
 void blk_mq_stop_hw_queues(struct request_queue *q);
+void blk_mq_start_hw_queues(struct request_queue *q);
 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async);
 void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs);
 
Signature · 1 line
1.7.10.4
#9 lore raw

PATCH 4/8 blk-mq: add blk_mq_delay_queue

In reply to Christoph Hellwig

Add a blk-mq equivalent to blk_delay_queue so that the scsi layer can ask to be kicked again after a delay.

---
 block/blk-core.c       |    6 ++++--
 block/blk-mq.c         |   47 +++++++++++++++++++++++++++++++++++++++++------
 include/linux/blk-mq.h |    4 +++-
 3 files changed, 48 insertions(+), 9 deletions(-)
block/blk-core.c +4 -2
index ae6227f..90b6e63 100644
@@ -251,8 +251,10 @@ void blk_sync_queue(struct request_queue *q)
 		struct blk_mq_hw_ctx *hctx;
 		int i;
 
-		queue_for_each_hw_ctx(q, hctx, i)
-			cancel_delayed_work_sync(&hctx->delayed_work);
+		queue_for_each_hw_ctx(q, hctx, i) {
+			cancel_delayed_work_sync(&hctx->run_work);
+			cancel_delayed_work_sync(&hctx->delay_work);
+		}
 	} else {
 		cancel_delayed_work_sync(&q->delay_work);
 	}
block/blk-mq.c +41 -6

86 lines not on this page · raw message

include/linux/blk-mq.h +3 -1
index 9ecfab9..ae868e7 100644
@@ -18,7 +18,8 @@ struct blk_mq_hw_ctx {
 	} ____cacheline_aligned_in_smp;
 
 	unsigned long		state;		/* BLK_MQ_S_* flags */
-	struct delayed_work	delayed_work;
+	struct delayed_work	run_work;
+	struct delayed_work	delay_work;
 	cpumask_var_t		cpumask;
 
 	unsigned long		flags;		/* BLK_MQ_F_* flags */
@@ -158,6 +159,7 @@ void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx);
 void blk_mq_stop_hw_queues(struct request_queue *q);
 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async);
+void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs);
 
 /*
  * Driver command data is immediately after the request. So subtract request
Signature · 1 line
1.7.10.4
#14 lore raw

PATCH 8/8 block: export blk_finish_request

In reply to Christoph Hellwig

This allows to mirror the blk-mq code flow for more a more readable I/O completion handler in SCSI.

---
 block/blk-core.c       |    3 ++-
 include/linux/blkdev.h |    1 +
 2 files changed, 3 insertions(+), 1 deletion(-)
block/blk-core.c +2 -1
index 90b6e63..c426970 100644
@@ -2497,7 +2497,7 @@ EXPORT_SYMBOL_GPL(blk_unprep_request);
 /*
  * queue lock must be held
  */
-static void blk_finish_request(struct request *req, int error)
+void blk_finish_request(struct request *req, int error)
 {
 	if (blk_rq_tagged(req))
 		blk_queue_end_tag(req->q, req);
@@ -2523,6 +2523,7 @@ static void blk_finish_request(struct request *req, int error)
 		__blk_put_request(req->q, req);
 	}
 }
+EXPORT_SYMBOL(blk_finish_request);
 
 /**
  * blk_end_bidi_request - Complete a bidi request
include/linux/blkdev.h +1 -0
index 7128808..20b26d4 100644
@@ -936,6 +936,7 @@ extern struct request *blk_fetch_request(struct request_queue *q);
  */
 extern bool blk_update_request(struct request *rq, int error,
 			       unsigned int nr_bytes);
+extern void blk_finish_request(struct request *rq, int error);
 extern bool blk_end_request(struct request *rq, int error,
 			    unsigned int nr_bytes);
 extern void blk_end_request_all(struct request *rq, int error);
Signature · 1 line
1.7.10.4
#13 lore raw

PATCH 7/8 blk-mq: rename mq_flush_work struct request member

In reply to Christoph Hellwig

We will use this work_struct to requeue scsi commands from the complention handler as well, so give it a more generic name.

---
 block/blk-flush.c      |    6 +++---
 include/linux/blkdev.h |    2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
block/blk-flush.c +3 -3
index c41fc19..ec7a224 100644
@@ -134,7 +134,7 @@ static void mq_flush_run(struct work_struct *work)
 {
 	struct request *rq;
 
-	rq = container_of(work, struct request, mq_flush_work);
+	rq = container_of(work, struct request, requeue_work);
 
 	memset(&rq->csd, 0, sizeof(rq->csd));
 	blk_mq_insert_request(rq, false, true, false);
@@ -143,8 +143,8 @@ static void mq_flush_run(struct work_struct *work)
 static bool blk_flush_queue_rq(struct request *rq, bool add_front)
 {
 	if (rq->q->mq_ops) {
-		INIT_WORK(&rq->mq_flush_work, mq_flush_run);
-		kblockd_schedule_work(&rq->mq_flush_work);
+		INIT_WORK(&rq->requeue_work, mq_flush_run);
+		kblockd_schedule_work(&rq->requeue_work);
 		return false;
 	} else {
 		if (add_front)
include/linux/blkdev.h +1 -1
index 95bb551..7128808 100644
@@ -98,7 +98,7 @@ struct request {
 	struct list_head queuelist;
 	union {
 		struct call_single_data csd;
-		struct work_struct mq_flush_work;
+		struct work_struct requeue_work;
 		unsigned long fifo_time;
 	};
 
Signature · 1 line
1.7.10.4
#12 lore raw

PATCH 6/8 blk-mq: add blk_mq_requeue_request

In reply to Christoph Hellwig

This allows to requeue a request that has been accepted by ->queue_rq earlier. This is needed by the SCSI layer in various error conditions.

The existing internal blk_mq_requeue_request is renamed to __blk_mq_requeue_request as it is a lower level building block for this funtionality.

---
 block/blk-mq.c         |   18 ++++++++++++++++--
 include/linux/blk-mq.h |    2 ++
 2 files changed, 18 insertions(+), 2 deletions(-)
block/blk-mq.c +16 -2
index 7946f0b..99d3795 100644
@@ -400,7 +400,7 @@ static void blk_mq_start_request(struct request *rq, bool last)
 		rq->cmd_flags |= REQ_END;
 }
 
-static void blk_mq_requeue_request(struct request *rq)
+static void __blk_mq_requeue_request(struct request *rq)
 {
 	struct request_queue *q = rq->q;
 
@@ -413,6 +413,20 @@ static void blk_mq_requeue_request(struct request *rq)
 		rq->nr_phys_segments--;
 }
 
+void blk_mq_requeue_request(struct request *rq)
+{
+	struct request_queue *q = rq->q;
+
+	__blk_mq_requeue_request(rq);
+	blk_clear_rq_complete(rq);
+
+	trace_block_rq_requeue(q, rq);
+
+	BUG_ON(blk_queued_rq(rq));
+	blk_mq_insert_request(rq, true, true, false);
+}
+EXPORT_SYMBOL(blk_mq_requeue_request);
+
 struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
 {
 	return tags->rqs[tag];
@@ -600,7 +614,7 @@ static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
 			 * time
 			 */
 			list_add(&rq->queuelist, &rq_list);
-			blk_mq_requeue_request(rq);
+			__blk_mq_requeue_request(rq);
 			break;
 		default:
 			pr_err("blk-mq: bad return on queue: %d\n", ret);
include/linux/blk-mq.h +2 -0
index 391377e..ab469d5 100644
@@ -153,6 +153,8 @@ void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *, unsigned int);
 void blk_mq_end_io(struct request *rq, int error);
 void __blk_mq_end_io(struct request *rq, int error);
 
+void blk_mq_requeue_request(struct request *rq);
+
 void blk_mq_complete_request(struct request *rq);
 
 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH 1/8 blk-mq: allow drivers to hook into I/O completion

In reply to Christoph Hellwig

Split out the bottom half of blk_mq_end_io so that drivers can perform work when they know a request has been completed, but before it has been freed. This also obsoletes blk_mq_end_io_partial as drivers can now pass any value to blk_update_request directly.

---
 block/blk-mq.c         |   16 ++++++++++------
 include/linux/blk-mq.h |    9 ++-------
 2 files changed, 12 insertions(+), 13 deletions(-)
block/blk-mq.c +10 -6
index 360a1d9..e921085 100644
@@ -294,20 +294,24 @@ void blk_mq_clone_flush_request(struct request *flush_rq,
 		hctx->cmd_size);
 }
 
-bool blk_mq_end_io_partial(struct request *rq, int error, unsigned int nr_bytes)
+inline void __blk_mq_end_io(struct request *rq, int error)
 {
-	if (blk_update_request(rq, error, blk_rq_bytes(rq)))
-		return true;
-
 	blk_account_io_done(rq);
 
 	if (rq->end_io)
 		rq->end_io(rq, error);
 	else
 		blk_mq_free_request(rq);
-	return false;
 }
-EXPORT_SYMBOL(blk_mq_end_io_partial);
+EXPORT_SYMBOL(__blk_mq_end_io);
+
+void blk_mq_end_io(struct request *rq, int error)
+{
+	if (blk_update_request(rq, error, blk_rq_bytes(rq)))
+		BUG();
+	__blk_mq_end_io(rq, error);
+}
+EXPORT_SYMBOL(blk_mq_end_io);
 
 static void __blk_mq_complete_request_remote(void *data)
 {
include/linux/blk-mq.h +2 -7
index a4ea0ce..a81b474 100644
@@ -149,13 +149,8 @@ struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_ind
 struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *, unsigned int);
 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *, unsigned int);
 
-bool blk_mq_end_io_partial(struct request *rq, int error,
-		unsigned int nr_bytes);
-static inline void blk_mq_end_io(struct request *rq, int error)
-{
-	bool done = !blk_mq_end_io_partial(rq, error, blk_rq_bytes(rq));
-	BUG_ON(!done);
-}
+void blk_mq_end_io(struct request *rq, int error);
+void __blk_mq_end_io(struct request *rq, int error);
 
 void blk_mq_complete_request(struct request *rq);
 
Signature · 1 line
1.7.10.4
#8 lore raw

PATCH 3/8 blk-mq: add async paramter to blk_mq_start_stopped_hw_queues

In reply to Christoph Hellwig
---
 block/blk-mq.c             |    4 ++--
 drivers/block/virtio_blk.c |    4 ++--
 include/linux/blk-mq.h     |    2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)
block/blk-mq.c +2 -2
index afdab13..8a080c2 100644
@@ -698,7 +698,7 @@ void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
 }
 EXPORT_SYMBOL(blk_mq_start_hw_queue);
 
-void blk_mq_start_stopped_hw_queues(struct request_queue *q)
+void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
 {
 	struct blk_mq_hw_ctx *hctx;
 	int i;
@@ -709,7 +709,7 @@ void blk_mq_start_stopped_hw_queues(struct request_queue *q)
 
 		clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
 		preempt_disable();
-		blk_mq_run_hw_queue(hctx, true);
+		blk_mq_run_hw_queue(hctx, async);
 		preempt_enable();
 	}
 }
drivers/block/virtio_blk.c +2 -2
index f909a88..7a51f06 100644
@@ -151,7 +151,7 @@ static void virtblk_done(struct virtqueue *vq)
 
 	/* In case queue is stopped waiting for more buffers. */
 	if (req_done)
-		blk_mq_start_stopped_hw_queues(vblk->disk->queue);
+		blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
 }
 
 static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req)
@@ -762,7 +762,7 @@ static int virtblk_restore(struct virtio_device *vdev)
 	vblk->config_enable = true;
 	ret = init_vq(vdev->priv);
 	if (!ret)
-		blk_mq_start_stopped_hw_queues(vblk->disk->queue);
+		blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
 
 	return ret;
 }
include/linux/blk-mq.h +1 -1
index a81b474..9ecfab9 100644
@@ -157,7 +157,7 @@ void blk_mq_complete_request(struct request *rq);
 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx);
 void blk_mq_stop_hw_queues(struct request_queue *q);
-void blk_mq_start_stopped_hw_queues(struct request_queue *q);
+void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async);
 
 /*
  * Driver command data is immediately after the request. So subtract request
Signature · 1 line
1.7.10.4
maintainer
#3 lore raw

Re: [PATCH 2/8] blk-mq: bidi support

In reply to Christoph Hellwig

block/bsg.c int bsg_register_queue(struct request_queue *q, struct device *parent, · line 1014

Quoted 13 lines · Christoph Hellwig
block/bsg.c +1 -1
index 420a5a9..2956086 100644
@@ -1008,7 +1008,7 @@ int bsg_register_queue(struct request_queue *q, struct device *parent,
 	/*
 	 * we need a proper transport to send commands, not a stacked device
 	 */
-	if (!q->request_fn)
+	if (!q->request_fn && !q->mq_ops)
 		return 0;
 
 	bcd = &q->bsg_dev;

This looks misplaced. But I dropped the one I generated last week, I'll queue it up separately in the drivers branch.

Signature · 1 line
Jens Axboe
#4 lore raw

Re: [PATCH 2/8] blk-mq: bidi support

Quoted 8 lines · Jens Axboe

block/bsg.c int bsg_register_queue(struct request_queue *q, struct device *parent, · line 1014

Quoted 5 lines
block/bsg.c +1 -1
-	if (!q->request_fn)
+	if (!q->request_fn && !q->mq_ops)
 		return 0;
 
 	bcd = &q->bsg_dev;

This looks misplaced. But I dropped the one I generated last week, I'll queue it up separately in the drivers branch.

Could be argued that it should be a separate patch, but the check should work fine.

maintainer
#5 lore raw

Re: [PATCH 2/8] blk-mq: bidi support

Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Jens Axboe
Quoted 4 lines
block/bsg.c +1 -1
-	if (!q->request_fn)
+	if (!q->request_fn && !q->mq_ops)
 		return 0;
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

bcd = &q->bsg_dev;

This looks misplaced. But I dropped the one I generated last week, I'll queue it up separately in the drivers branch.

Could be argued that it should be a separate patch, but the check should work fine.

There's nothing wrong with the check, it's identical to one I sent out last week. But it's not part of the bidi enable patch, it's a separate bug fix for bsg.

Signature · 1 line
Jens Axboe
#6 lore raw

Re: [PATCH 2/8] blk-mq: bidi support

Quoted 3 lines · Jens Axboe

There's nothing wrong with the check, it's identical to one I sent out last week. But it's not part of the bidi enable patch, it's a separate bug fix for bsg.

Do you want to split it up while applying or should I resend two separate patches?

maintainer
#10 lore raw

Re: [PATCH 4/8] blk-mq: add blk_mq_delay_queue

In reply to Christoph Hellwig
Quoted 2 lines · Christoph Hellwig

Add a blk-mq equivalent to blk_delay_queue so that the scsi layer can ask to be kicked again after a delay.

Applied with the preempts bits from the delayed work handler removed.

Signature · 1 line
Jens Axboe
maintainer
#7 lore raw

Re: [PATCH 2/8] blk-mq: bidi support

In reply to Christoph Hellwig
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Jens Axboe

There's nothing wrong with the check, it's identical to one I sent out last week. But it's not part of the bidi enable patch, it's a separate bug fix for bsg.

Do you want to split it up while applying or should I resend two separate patches?

I cut that part and applied, whole series is applied now.

Signature · 1 line
Jens Axboe