Skip to contentSkip to conversation

block: support bio based io polling

PATCH In review Ming Lei 3 participants 26 messages Apr 22, 2021 – May 17, 2021

Subsystems (3)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

v6 cover block: support bio based io polling

Hi Jens,

Add per-task io poll context for holding HIPRI blk-mq/underlying bios queued from bio based driver's io submission context, and reuse one bio padding field for storing 'cookie' returned from submit_bio() for these bios. Also explicitly end these bios in poll context by adding two new bio flags.

In this way, we needn't to poll all underlying hw queues any more, which is implemented in Jeffle's patches. And we can just poll hw queues in which there is HIPRI IO queued.

Usually io submission and io poll share same context, so the added io poll context data is just like one stack variable, and the cost for saving bios is cheap.

V6:
- move poll code into block/blk-poll.c, as suggested by Christoph
- define bvec_iter as __packed, and add one new field to bio, as suggested by Christoph
- re-organize patch order, as suggested by Christoph
- add one flag for checking if the disk is capable of bio polling and remove .poll_capable(), as suggested by Christoph
- fix type of .bi_poll

V5:
- fix one use-after-free issue in case that polling is from another
context: adds one new cookie of BLK_QC_T_NOT_READY for preventing this issue in patch 8/12
- add reviewed-by & tested-by tag

V4:
- cover one more test_bit(QUEUE_FLAG_POLL, ...) suggested by Jeffle(01/12)
- drop patch of 'block: add helper of blk_create_io_context'
- add new helper of blk_create_io_poll_context() (03/12)
- drain submission queues in exit_io_context(), suggested by Jeffle(08/13)
- considering shared io context case for blk_bio_poll_io_drain() (08/13)
- fix one issue in blk_bio_poll_pack_groups() as suggested by Jeffle(08/13)
- add reviewed-by tag
V3:
- fix cookie returned for bio based driver, as suggested by Jeffle Xu
- draining pending bios when submission context is exiting
- patch style and comment fix, as suggested by Mike
- allow poll context data to be NULL by always polling on submission queue
- remove RFC, and reviewed-by

V2:
- address queue depth scalability issue reported by Jeffle via bio group list. Reuse .bi_end_io for linking bios which share same .bi_end_io, and support 32 such groups in submit queue. With this way, the scalability issue caused by kfifio is solved. Before really ending bio, .bi_end_io is recovered from the group head.

Jeffle Xu (2):
block: extract one helper function polling hw queue
dm: support IO polling for bio-based dm device

Ming Lei (10):
block: add helper of blk_queue_poll
block: define 'struct bvec_iter' as packed
block: add one helper to free io_context
block: move block polling code into one dedicated source file
block: prepare for supporting bio_list via other link
block: create io poll context for submission and poll task
block: add req flag of REQ_POLL_CTX
block: use per-task poll context to implement bio based io polling
block: limit hw queues to be polled in each blk_poll()
block: allow to control FLAG_POLL via sysfs for bio poll capable queue

 block/Makefile                |   3 +-
 block/bio.c                   |   5 +
 block/blk-core.c              |  68 +++-
 block/blk-ioc.c               |  15 +-
 block/blk-mq.c                | 231 -------------
 block/blk-mq.h                |  40 +++
 block/blk-poll.c              | 632 ++++++++++++++++++++++++++++++++++
 block/blk-sysfs.c             |  16 +-
 block/blk.h                   | 112 ++++++
 drivers/md/dm-table.c         |  24 ++
 drivers/md/dm.c               |   2 +
 drivers/nvme/host/core.c      |   2 +-
 include/linux/bio.h           | 132 +++----
 include/linux/blk_types.h     |  31 +-
 include/linux/blkdev.h        |   1 +
 include/linux/bvec.h          |   2 +-
 include/linux/device-mapper.h |   1 +
 include/linux/genhd.h         |   2 +
 include/linux/iocontext.h     |   2 +
 19 files changed, 1003 insertions(+), 318 deletions(-)
 create mode 100644 block/blk-poll.c
Signature · 6 lines
2.29.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author
#1 lore raw

PATCH v6 1/12 block: add helper of blk_queue_poll

There has been 3 users, and will be more, so add one such helper.

---
 block/blk-core.c         | 2 +-
 block/blk-mq.c           | 3 +--
 block/blk-sysfs.c        | 2 +-
 drivers/nvme/host/core.c | 2 +-
 include/linux/blkdev.h   | 1 +
 5 files changed, 5 insertions(+), 5 deletions(-)
block/blk-core.c +1 -1
index ca7f833e25a8..d44a8b934608 100644
@@ -868,7 +868,7 @@ static noinline_for_stack bool submit_bio_checks(struct bio *bio)
 		}
 	}
 
-	if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
+	if (!blk_queue_poll(q))
 		bio->bi_opf &= ~REQ_HIPRI;
 
 	switch (bio_op(bio)) {
block/blk-mq.c +1 -2
index 7d2ea6357c7d..47e650bb836b 100644
@@ -3887,8 +3887,7 @@ int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin)
 	struct blk_mq_hw_ctx *hctx;
 	long state;
 
-	if (!blk_qc_t_valid(cookie) ||
-	    !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
+	if (!blk_qc_t_valid(cookie) || !blk_queue_poll(q))
 		return 0;
 
 	if (current->plug)
block/blk-sysfs.c +1 -1
index e03bedf180ab..fed4981b1f7a 100644
@@ -422,7 +422,7 @@ static ssize_t queue_poll_delay_store(struct request_queue *q, const char *page,
 
 static ssize_t queue_poll_show(struct request_queue *q, char *page)
 {
-	return queue_var_show(test_bit(QUEUE_FLAG_POLL, &q->queue_flags), page);
+	return queue_var_show(blk_queue_poll(q), page);
 }
 
 static ssize_t queue_poll_store(struct request_queue *q, const char *page,
drivers/nvme/host/core.c +1 -1
index 0896e21642be..34b8c78f88e0 100644
@@ -956,7 +956,7 @@ static void nvme_execute_rq_polled(struct request_queue *q,
 {
 	DECLARE_COMPLETION_ONSTACK(wait);
 
-	WARN_ON_ONCE(!test_bit(QUEUE_FLAG_POLL, &q->queue_flags));
+	WARN_ON_ONCE(!blk_queue_poll(q));
 
 	rq->cmd_flags |= REQ_HIPRI;
 	rq->end_io_data = &wait;
include/linux/blkdev.h +1 -0
index f2e77ba97550..668223168412 100644
@@ -675,6 +675,7 @@ bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q);
 #define blk_queue_fua(q)	test_bit(QUEUE_FLAG_FUA, &(q)->queue_flags)
 #define blk_queue_registered(q)	test_bit(QUEUE_FLAG_REGISTERED, &(q)->queue_flags)
 #define blk_queue_nowait(q)	test_bit(QUEUE_FLAG_NOWAIT, &(q)->queue_flags)
+#define blk_queue_poll(q)	test_bit(QUEUE_FLAG_POLL, &(q)->queue_flags)
 
 extern void blk_set_pm_only(struct request_queue *q);
 extern void blk_clear_pm_only(struct request_queue *q);
Signature · 1 line
2.29.2
author
#2 lore raw

PATCH v6 2/12 block: define 'struct bvec_iter' as packed

In reply to Ming Lei

'struct bvec_iter' is embedded into 'struct bio', define it as packed so that we can get one extra 4bytes for other uses without expanding bio.

'struct bvec_iter' is often allocated on stack, so making it packed doesn't affect performance. Also I have run io_uring on both nvme/null_blk, and not observe performance effect in this way.

---
 include/linux/bvec.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bvec.h +1 -1
index ff832e698efb..a0c4f41dfc83 100644
@@ -43,7 +43,7 @@ struct bvec_iter {
 
 	unsigned int            bi_bvec_done;	/* number of bytes completed in
 						   current bvec */
-};
+} __packed;
 
 struct bvec_iter_all {
 	struct bio_vec	bv;
Signature · 6 lines
2.29.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Reviewed-by on v6 2/12
#3 lore raw

Re: [PATCH V6 02/12] block: define 'struct bvec_iter' as packed

Quoted 20 lines · Ming Lei

'struct bvec_iter' is embedded into 'struct bio', define it as packed so that we can get one extra 4bytes for other uses without expanding bio.

'struct bvec_iter' is often allocated on stack, so making it packed doesn't affect performance. Also I have run io_uring on both nvme/null_blk, and not observe performance effect in this way.

---
  include/linux/bvec.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bvec.h +0 -0
index ff832e698efb..a0c4f41dfc83 100644

include/linux/bvec.h struct bvec_iter { · line 49

Quoted 8 lines
include/linux/bvec.h +1 -1
@@ -43,7 +43,7 @@ struct bvec_iter {
 
  	unsigned int            bi_bvec_done;	/* number of bytes completed in
  						   current bvec */
-};
+} __packed;
 
  struct bvec_iter_all {
  	struct bio_vec	bv;

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
author
#4 lore raw

PATCH v6 3/12 block: add one helper to free io_context

In reply to Ming Lei

Prepare for putting bio poll queue into io_context, so add one helper for free io_context.

---
 block/blk-ioc.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
block/blk-ioc.c +8 -3
index 57299f860d41..b0cde18c4b8c 100644
@@ -17,6 +17,11 @@
  */
 static struct kmem_cache *iocontext_cachep;
 
+static inline void free_io_context(struct io_context *ioc)
+{
+	kmem_cache_free(iocontext_cachep, ioc);
+}
+
 /**
  * get_io_context - increment reference count to io_context
  * @ioc: io_context to get
@@ -129,7 +134,7 @@ static void ioc_release_fn(struct work_struct *work)
 
 	spin_unlock_irq(&ioc->lock);
 
-	kmem_cache_free(iocontext_cachep, ioc);
+	free_io_context(ioc);
 }
 
 /**
@@ -164,7 +169,7 @@ void put_io_context(struct io_context *ioc)
 	}
 
 	if (free_ioc)
-		kmem_cache_free(iocontext_cachep, ioc);
+		free_io_context(ioc);
 }
 
 /**
@@ -278,7 +283,7 @@ int create_task_io_context(struct task_struct *task, gfp_t gfp_flags, int node)
 	    (task == current || !(task->flags & PF_EXITING)))
 		task->io_context = ioc;
 	else
-		kmem_cache_free(iocontext_cachep, ioc);
+		free_io_context(ioc);
 
 	ret = task->io_context ? 0 : -EBUSY;
 
Signature · 1 line
2.29.2
author
#5 lore raw

PATCH v6 4/12 block: move block polling code into one dedicated source file

In reply to Ming Lei

Prepare for supporting bio based io polling, and move blk polling code into one dedicated source file. And three shared functions are put into private header of blk-mq.h

---
 block/Makefile   |   3 +-
 block/blk-mq.c   | 230 -----------------------------------------------
 block/blk-mq.h   |  40 +++++++++
 block/blk-poll.c | 196 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 238 insertions(+), 231 deletions(-)
 create mode 100644 block/blk-poll.c
block/Makefile +2 -1
index 8d841f5f986f..d7abe2333407 100644
@@ -8,7 +8,8 @@ obj-$(CONFIG_BLOCK) := bio.o elevator.o blk-core.o blk-sysfs.o \
 			blk-exec.o blk-merge.o blk-timeout.o \
 			blk-lib.o blk-mq.o blk-mq-tag.o blk-stat.o \
 			blk-mq-sysfs.o blk-mq-cpumap.o blk-mq-sched.o ioctl.o \
-			genhd.o ioprio.o badblocks.o partitions/ blk-rq-qos.o
+			genhd.o ioprio.o badblocks.o partitions/ blk-rq-qos.o \
+			blk-poll.o
 
 obj-$(CONFIG_BOUNCE)		+= bounce.o
 obj-$(CONFIG_BLK_SCSI_REQUEST)	+= scsi_ioctl.o
block/blk-mq.c +0 -230

242 lines not on this page · raw message

block/blk-mq.h +40 -0
index 9ccb1818303b..2eea38cd8048 100644
@@ -324,5 +324,45 @@ static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx,
 	return __blk_mq_active_requests(hctx) < depth;
 }
 
+static inline int blk_mq_poll_stats_bkt(const struct request *rq)
+{
+	int ddir, sectors, bucket;
+
+	ddir = rq_data_dir(rq);
+	sectors = blk_rq_stats_sectors(rq);
+
+	bucket = ddir + 2 * ilog2(sectors);
+
+	if (bucket < 0)
+		return -1;
+	else if (bucket >= BLK_MQ_POLL_STATS_BKTS)
+		return ddir + BLK_MQ_POLL_STATS_BKTS - 2;
+
+	return bucket;
+}
+
+static inline void blk_mq_poll_stats_start(struct request_queue *q)
+{
+	/*
+	 * We don't arm the callback if polling stats are not enabled or the
+	 * callback is already active.
+	 */
+	if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
+	    blk_stat_is_active(q->poll_cb))
+		return;
+
+	blk_stat_activate_msecs(q->poll_cb, 100);
+}
+
+static inline void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
+{
+	struct request_queue *q = cb->data;
+	int bucket;
+
+	for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
+		if (cb->stat[bucket].nr_samples)
+			q->poll_stat[bucket] = cb->stat[bucket];
+	}
+}
 
 #endif
block/blk-poll.c new file +196 -0

196 lines not on this page · raw message

Signature · 6 lines
2.29.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Reviewed-by on v6 4/12
#6 lore raw

Re: [PATCH V6 04/12] block: move block polling code into one dedicated source file

Quoted 6 lines · Ming Lei

Prepare for supporting bio based io polling, and move blk polling code into one dedicated source file. And three shared functions are put into private header of blk-mq.h

Quoted 8 lines
---
  block/Makefile   |   3 +-
  block/blk-mq.c   | 230 -----------------------------------------------
  block/blk-mq.h   |  40 +++++++++
  block/blk-poll.c | 196 ++++++++++++++++++++++++++++++++++++++++
  4 files changed, 238 insertions(+), 231 deletions(-)

create mode 100644 block/blk-poll.c

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Reviewed-by on v6 4/12
#7 lore raw

Re: [PATCH V6 04/12] block: move block polling code into one dedicated source file

In reply to Ming Lei
Quoted 6 lines · Ming Lei

Prepare for supporting bio based io polling, and move blk polling code into one dedicated source file. And three shared functions are put into private header of blk-mq.h

Quoted 8 lines
---
 block/Makefile   |   3 +-
 block/blk-mq.c   | 230 -----------------------------------------------
 block/blk-mq.h   |  40 +++++++++
 block/blk-poll.c | 196 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 238 insertions(+), 231 deletions(-)
 create mode 100644 block/blk-poll.c

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		        Kernel Storage Architect
[email protected]			               +49 911 74053 688
SUSE Software Solutions Germany GmbH, 90409 Nürnberg
GF: F. Imendörffer, HRB 36809 (AG Nürnberg)
author
#8 lore raw

PATCH v6 5/12 block: extract one helper function polling hw queue

In reply to Ming Lei

From: Jeffle Xu <[email protected]>

Extract the logic of polling a hw queue and related statistics handling out as the helper function.

---
 block/blk-poll.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
block/blk-poll.c +14 -4
index daa307f84792..0a38c25bcee5 100644
@@ -129,6 +129,19 @@ static bool blk_mq_poll_hybrid(struct request_queue *q,
 	return blk_mq_poll_hybrid_sleep(q, rq);
 }
 
+static inline int blk_mq_poll_hctx(struct request_queue *q,
+				   struct blk_mq_hw_ctx *hctx)
+{
+	int ret;
+
+	hctx->poll_invoked++;
+	ret = q->mq_ops->poll(hctx);
+	if (ret > 0)
+		hctx->poll_success++;
+
+	return ret;
+}
+
 /**
  * blk_poll - poll for IO completions
  * @q:  the queue
@@ -171,11 +184,8 @@ int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin)
 	do {
 		int ret;
 
-		hctx->poll_invoked++;
-
-		ret = q->mq_ops->poll(hctx);
+		ret = blk_mq_poll_hctx(q, hctx);
 		if (ret > 0) {
-			hctx->poll_success++;
 			__set_current_state(TASK_RUNNING);
 			return ret;
 		}
Signature · 1 line
2.29.2
author
#9 lore raw

PATCH v6 6/12 block: prepare for supporting bio_list via other link

In reply to Ming Lei

So far bio list helpers always use .bi_next to traverse the list, we will support to link bios by other bio field.

Prepare for such support by adding a macro so that users can define another helpers for linking bios by other bio field.

---
 include/linux/bio.h | 132 +++++++++++++++++++++++---------------------
 1 file changed, 68 insertions(+), 64 deletions(-)
include/linux/bio.h +68 -64
index a0b4cfdf62a4..c95f0e4fe530 100644
@@ -602,75 +602,11 @@ static inline unsigned bio_list_size(const struct bio_list *bl)
 	return sz;
 }
 
-static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
-{
-	bio->bi_next = NULL;
-
-	if (bl->tail)
-		bl->tail->bi_next = bio;
-	else
-		bl->head = bio;
-
-	bl->tail = bio;
-}
-
-static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
-{
-	bio->bi_next = bl->head;
-
-	bl->head = bio;
-
-	if (!bl->tail)
-		bl->tail = bio;
-}
-
-static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
-{
-	if (!bl2->head)
-		return;
-
-	if (bl->tail)
-		bl->tail->bi_next = bl2->head;
-	else
-		bl->head = bl2->head;
-
-	bl->tail = bl2->tail;
-}
-
-static inline void bio_list_merge_head(struct bio_list *bl,
-				       struct bio_list *bl2)
-{
-	if (!bl2->head)
-		return;
-
-	if (bl->head)
-		bl2->tail->bi_next = bl->head;
-	else
-		bl->tail = bl2->tail;
-
-	bl->head = bl2->head;
-}
-
 static inline struct bio *bio_list_peek(struct bio_list *bl)
 {
 	return bl->head;
 }
 
-static inline struct bio *bio_list_pop(struct bio_list *bl)
-{
-	struct bio *bio = bl->head;
-
-	if (bio) {
-		bl->head = bl->head->bi_next;
-		if (!bl->head)
-			bl->tail = NULL;
-
-		bio->bi_next = NULL;
-	}
-
-	return bio;
-}
-
 static inline struct bio *bio_list_get(struct bio_list *bl)
 {
 	struct bio *bio = bl->head;
@@ -680,6 +616,74 @@ static inline struct bio *bio_list_get(struct bio_list *bl)
 	return bio;
 }
 
+#define BIO_LIST_HELPERS(_pre, link)					\
+									\
+static inline void _pre##_add(struct bio_list *bl, struct bio *bio)	\
+{									\
+	bio->bi_##link = NULL;						\
+									\
+	if (bl->tail)							\
+		bl->tail->bi_##link = bio;				\
+	else								\
+		bl->head = bio;						\
+									\
+	bl->tail = bio;							\
+}									\
+									\
+static inline void _pre##_add_head(struct bio_list *bl, struct bio *bio) \
+{									\
+	bio->bi_##link = bl->head;					\
+									\
+	bl->head = bio;							\
+									\
+	if (!bl->tail)							\
+		bl->tail = bio;						\
+}									\
+									\
+static inline void _pre##_merge(struct bio_list *bl, struct bio_list *bl2) \
+{									\
+	if (!bl2->head)							\
+		return;							\
+									\
+	if (bl->tail)							\
+		bl->tail->bi_##link = bl2->head;			\
+	else								\
+		bl->head = bl2->head;					\
+									\
+	bl->tail = bl2->tail;						\
+}									\
+									\
+static inline void _pre##_merge_head(struct bio_list *bl,		\
+				       struct bio_list *bl2)		\
+{									\
+	if (!bl2->head)							\
+		return;							\
+									\
+	if (bl->head)							\
+		bl2->tail->bi_##link = bl->head;			\
+	else								\
+		bl->tail = bl2->tail;					\
+									\
+	bl->head = bl2->head;						\
+}									\
+									\
+static inline struct bio *_pre##_pop(struct bio_list *bl)		\
+{									\
+	struct bio *bio = bl->head;					\
+									\
+	if (bio) {							\
+		bl->head = bl->head->bi_##link;				\
+		if (!bl->head)						\
+			bl->tail = NULL;				\
+									\
+		bio->bi_##link = NULL;					\
+	}								\
+									\
+	return bio;							\
+}									\
+
+BIO_LIST_HELPERS(bio_list, next);
+
 /*
  * Increment chain count for the bio. Make sure the CHAIN flag update
  * is visible before the raised count.
Signature · 6 lines
2.29.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author
#10 lore raw

PATCH v6 7/12 block: create io poll context for submission and poll task

In reply to Ming Lei

Create per-task io poll context for both IO submission and poll task if the queue is bio based and supports polling.

This io polling context includes two queues:

1) submission queue(sq) for storing HIPRI bio, written by submission task and read by poll task.
2) polling queue(pq) for holding data moved from sq, only used in poll context for running bio polling.

Following patches will support bio based io polling.

---
 block/blk-core.c          | 24 +++++++++-------
 block/blk-ioc.c           |  1 +
 block/blk-poll.c          | 51 +++++++++++++++++++++++++++++++++
 block/blk.h               | 60 +++++++++++++++++++++++++++++++++++++++
 include/linux/iocontext.h |  2 ++
 5 files changed, 127 insertions(+), 11 deletions(-)
block/blk-core.c +13 -11
index d44a8b934608..5830ef4d733e 100644
@@ -868,8 +868,19 @@ static noinline_for_stack bool submit_bio_checks(struct bio *bio)
 		}
 	}
 
-	if (!blk_queue_poll(q))
-		bio->bi_opf &= ~REQ_HIPRI;
+	/*
+	 * Various block parts want %current->io_context, so allocate it up
+	 * front rather than dealing with lots of pain to allocate it only
+	 * where needed. This may fail and the block layer knows how to live
+	 * with it.
+	 */
+	if (unlikely(!current->io_context))
+		create_task_io_context(current, GFP_ATOMIC, q->node);
+
+	if ((bio->bi_opf & REQ_HIPRI) && blk_queue_support_bio_poll(q))
+		blk_create_io_poll_context(q);
+
+	blk_poll_prepare(q, bio);
 
 	switch (bio_op(bio)) {
 	case REQ_OP_DISCARD:
@@ -908,15 +919,6 @@ static noinline_for_stack bool submit_bio_checks(struct bio *bio)
 		break;
 	}
 
-	/*
-	 * Various block parts want %current->io_context, so allocate it up
-	 * front rather than dealing with lots of pain to allocate it only
-	 * where needed. This may fail and the block layer knows how to live
-	 * with it.
-	 */
-	if (unlikely(!current->io_context))
-		create_task_io_context(current, GFP_ATOMIC, q->node);
-
 	if (blk_throtl_bio(bio)) {
 		blkcg_bio_issue_init(bio);
 		return false;
block/blk-ioc.c +1 -0
index b0cde18c4b8c..5574c398eff6 100644
@@ -19,6 +19,7 @@ static struct kmem_cache *iocontext_cachep;
 
 static inline void free_io_context(struct io_context *ioc)
 {
+	kfree(ioc->data);
 	kmem_cache_free(iocontext_cachep, ioc);
 }
 
block/blk-poll.c +51 -0

71 lines not on this page · raw message

block/blk.h +60 -0

64 lines not on this page · raw message

include/linux/iocontext.h +2 -0
index 0a9dc40b7be8..f9a467571356 100644
@@ -110,6 +110,8 @@ struct io_context {
 	struct io_cq __rcu	*icq_hint;
 	struct hlist_head	icq_list;
 
+	void			*data;
+
 	struct work_struct release_work;
 };
 
Signature · 1 line
2.29.2
author
#11 lore raw

PATCH v6 8/12 block: add req flag of REQ_POLL_CTX

In reply to Ming Lei

Add one req flag REQ_POLL_CTX which will be used in the following patch for supporting bio based IO polling.

Exactly this flag can help us to do:

1) request flag is cloned in bio_fast_clone(), so if we mark one FS bio as REQ_POLL_CTX, all bios cloned from this FS bio will be marked as REQ_POLL_CTX too.

2) create per-task io polling context if the bio based queue supports polling and the submitted bio is HIPRI. Per-task io poll context will be created during submit_bio() before marking this HIPRI bio as REQ_POLL_CTX. Then we can avoid to create such io polling context if one cloned bio with REQ_POLL_CTX is submitted from another kernel context.

3) for supporting bio based io polling, we need to poll IOs from all underlying queues of the bio device, this way help us to recognize which IO needs to polled in bio based style, which will be applied in following patch.

---
 block/blk-core.c          |  7 ++++++-
 block/blk.h               | 21 ++++++++++++++++++++-
 include/linux/blk_types.h |  4 ++++
 3 files changed, 30 insertions(+), 2 deletions(-)
block/blk-core.c +6 -1
index 5830ef4d733e..ad57e04d5297 100644
@@ -877,7 +877,12 @@ static noinline_for_stack bool submit_bio_checks(struct bio *bio)
 	if (unlikely(!current->io_context))
 		create_task_io_context(current, GFP_ATOMIC, q->node);
 
-	if ((bio->bi_opf & REQ_HIPRI) && blk_queue_support_bio_poll(q))
+	/*
+	 * If REQ_POLL_CTX isn't set for this HIPRI bio, we think it
+	 * originated from FS and allocate io polling context.
+	 */
+	if ((bio->bi_opf & REQ_HIPRI) && !(bio->bi_opf & REQ_POLL_CTX) &&
+			blk_queue_support_bio_poll(q))
 		blk_create_io_poll_context(q);
 
 	blk_poll_prepare(q, bio);
block/blk.h +20 -1
index bc6d63ae36b7..47f60612957a 100644
@@ -393,11 +393,30 @@ static inline struct blk_bio_poll_ctx *blk_get_bio_poll_ctx(void)
 static inline void blk_poll_prepare(struct request_queue *q,
 		struct bio *bio)
 {
+	bool mq;
+
 	if (!(bio->bi_opf & REQ_HIPRI))
 		return;
 
-	if (!blk_queue_poll(q) || (!queue_is_mq(q) && !blk_get_bio_poll_ctx()))
+	/*
+	 * Can't support bio based IO polling without per-task poll ctx
+	 *
+	 * We have created per-task io poll context, and mark this
+	 * bio as REQ_POLL_CTX, so: 1) if any cloned bio from this bio is
+	 * submitted from another kernel context, we won't create bio
+	 * poll context for it, and that bio can be completed by IRQ;
+	 * 2) If such bio is submitted from current context, we will
+	 * complete it via blk_poll(); 3) If driver knows that one
+	 * underlying bio allocated from driver is for FS bio, meantime
+	 * it is submitted in current context, driver can mark such bio
+	 * as REQ_HIPRI & REQ_POLL_CTX manually, so the bio can be completed
+	 * via blk_poll too.
+	 */
+	mq = queue_is_mq(q);
+	if (!blk_queue_poll(q) || (!mq && !blk_get_bio_poll_ctx()))
 		bio->bi_opf &= ~REQ_HIPRI;
+	else if (!mq)
+		bio->bi_opf |= REQ_POLL_CTX;
 }
 
 static inline void blk_create_io_poll_context(struct request_queue *q)
include/linux/blk_types.h +4 -0
index db026b6ec15a..99160d588c2d 100644
@@ -394,6 +394,9 @@ enum req_flag_bits {
 
 	__REQ_HIPRI,
 
+	/* for marking IOs originated from same FS bio in same context */
+	__REQ_POLL_CTX,
+
 	/* for driver use */
 	__REQ_DRV,
 	__REQ_SWAP,		/* swapping request. */
@@ -418,6 +421,7 @@ enum req_flag_bits {
 
 #define REQ_NOUNMAP		(1ULL << __REQ_NOUNMAP)
 #define REQ_HIPRI		(1ULL << __REQ_HIPRI)
+#define REQ_POLL_CTX			(1ULL << __REQ_POLL_CTX)
 
 #define REQ_DRV			(1ULL << __REQ_DRV)
 #define REQ_SWAP		(1ULL << __REQ_SWAP)
Signature · 6 lines
2.29.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author
#12 lore raw

PATCH v6 9/12 block: use per-task poll context to implement bio based io polling

In reply to Ming Lei

Currently bio based IO polling needs to poll all hw queue blindly, this way is very inefficient, and one big reason is that we can't pass any bio submission result to blk_poll().

In IO submission context, track associated underlying bios by per-task submission queue and store returned 'cookie' in bio->bi_poll_data which is added by filling a hole of .bi_iter, and return current->pid to caller of submit_bio() for any bio based driver's IO, which is submitted from FS.

In IO poll context, the passed cookie tells us the PID of submission context, then we can find bios from the per-task io pull context of submission context. Moving bios from submission queue to poll queue of the poll context, and keep polling until these bios are ended. Remove bio from poll queue if the bio is ended. Add bio flags of BIO_DONE and BIO_END_BY_POLL for such purpose.

In was found in Jeffle Xu's test that kfifo doesn't scale well for a submission queue as queue depth is increased, so a new mechanism for tracking bios is needed. So far bio's size is close to 2 cacheline size, and it may not be accepted to add new field into bio for solving the scalability issue by tracking bios via linked list, switch to bio group list for tracking bio, the idea is to reuse .bi_end_io for linking bios into a linked list for all sharing same .bi_end_io(call it bio group), which is recovered before ending bio really, since BIO_END_BY_POLL is added for enhancing this point. Usually .bi_end_bio is same for all bios in same layer, so it is enough to provide very limited groups, such as 16 or less for fixing the scalability issue.

Usually submission shares context with io poll. The per-task poll context is just like stack variable, and it is cheap to move data between the two per-task queues.

Also when the submission task is exiting, drain pending IOs in the context until all are done.

---
 block/bio.c               |   5 +
 block/blk-core.c          |  39 ++++-
 block/blk-ioc.c           |   3 +
 block/blk-poll.c          | 345 +++++++++++++++++++++++++++++++++++++-
 block/blk.h               |  33 ++++
 include/linux/blk_types.h |  27 ++-
 6 files changed, 448 insertions(+), 4 deletions(-)
block/bio.c +5 -0
index 303298996afe..3cf9cf4479db 100644
@@ -1365,6 +1365,11 @@ static inline bool bio_remaining_done(struct bio *bio)
  **/
 void bio_endio(struct bio *bio)
 {
+	/* BIO_END_BY_POLL has to be set before calling submit_bio */
+	if (bio_flagged(bio, BIO_END_BY_POLL)) {
+		bio_set_flag(bio, BIO_DONE);
+		return;
+	}
 again:
 	if (!bio_remaining_done(bio))
 		return;
block/blk-core.c +37 -2
index ad57e04d5297..06acb233e606 100644
@@ -982,7 +982,7 @@ static blk_qc_t __submit_bio(struct bio *bio)
  * bio_list_on_stack[1] contains bios that were submitted before the current
  *	->submit_bio_bio, but that haven't been processed yet.
  */
-static blk_qc_t __submit_bio_noacct(struct bio *bio)
+static blk_qc_t __submit_bio_noacct_ctx(struct bio *bio, struct io_context *ioc)
 {
 	struct bio_list bio_list_on_stack[2];
 	blk_qc_t ret = BLK_QC_T_NONE;
@@ -1005,7 +1005,15 @@ static blk_qc_t __submit_bio_noacct(struct bio *bio)
 		bio_list_on_stack[1] = bio_list_on_stack[0];
 		bio_list_init(&bio_list_on_stack[0]);
 
-		ret = __submit_bio(bio);
+		if (ioc && queue_is_mq(q) && (bio->bi_opf & REQ_HIPRI)) {
+			bool queued = blk_bio_poll_prep_submit(ioc, bio);
+
+			ret = __submit_bio(bio);
+			if (queued)
+				bio_set_poll_data(bio, ret);
+		} else {
+			ret = __submit_bio(bio);
+		}
 
 		/*
 		 * Sort new bios into those for a lower level and those for the
@@ -1031,6 +1039,33 @@ static blk_qc_t __submit_bio_noacct(struct bio *bio)
 	return ret;
 }
 
+static inline blk_qc_t __submit_bio_noacct_poll(struct bio *bio,
+		struct io_context *ioc)
+{
+	struct blk_bio_poll_ctx *pc = ioc->data;
+
+	__submit_bio_noacct_ctx(bio, ioc);
+
+	/* bio submissions queued to per-task poll context */
+	if (READ_ONCE(pc->sq->nr_grps))
+		return current->pid;
+
+	/* swapper's pid is 0, but it can't submit poll IO for us */
+	return BLK_QC_T_BIO_NONE;
+}
+
+static inline blk_qc_t __submit_bio_noacct(struct bio *bio)
+{
+	struct io_context *ioc = current->io_context;
+
+	if (ioc && ioc->data && (bio->bi_opf & REQ_HIPRI))
+		return __submit_bio_noacct_poll(bio, ioc);
+
+	__submit_bio_noacct_ctx(bio, NULL);
+
+	return BLK_QC_T_BIO_NONE;
+}
+
 static blk_qc_t __submit_bio_noacct_mq(struct bio *bio)
 {
 	struct bio_list bio_list[2] = { };
block/blk-ioc.c +3 -0
index 5574c398eff6..c1fd7c593a54 100644
@@ -211,6 +211,9 @@ void exit_io_context(struct task_struct *task)
 	task->io_context = NULL;
 	task_unlock(task);
 
+	/* drain io poll submissions */
+	blk_bio_poll_io_drain(ioc);
+
 	atomic_dec(&ioc->nr_tasks);
 	put_io_context_active(ioc);
 }
block/blk-poll.c +344 -1

368 lines not on this page · raw message

block/blk.h +33 -0
index 47f60612957a..4590da07f8f6 100644
@@ -376,6 +376,8 @@ struct blk_bio_poll_ctx {
 #define BLK_BIO_POLL_SQ_SZ		16U
 #define BLK_BIO_POLL_PQ_SZ		(BLK_BIO_POLL_SQ_SZ * 2)
 
+bool blk_bio_poll_prep_submit(struct io_context *ioc, struct bio *bio);
+void blk_bio_poll_io_drain(struct io_context *submit_ioc);
 void bio_poll_ctx_alloc(struct io_context *ioc);
 
 static inline bool blk_queue_support_bio_poll(struct request_queue *q)
@@ -431,4 +433,35 @@ static inline void blk_create_io_poll_context(struct request_queue *q)
 		bio_poll_ctx_alloc(ioc);
 }
 
+BIO_LIST_HELPERS(__bio_grp_list, poll);
+
+static inline bool bio_grp_list_grp_empty(struct bio_grp_list_data *grp)
+{
+	return bio_list_empty(&grp->list);
+}
+
+static inline void blk_bio_poll_mark_queued(struct bio *bio, bool queued)
+{
+	/*
+	 * The bio has been added to per-task poll queue, mark it as
+	 * END_BY_POLL, so that this bio is always completed from
+	 * blk_poll() which is provided with cookied from this bio's
+	 * submission.
+	 */
+	if (!queued)
+		bio->bi_opf &= ~(REQ_HIPRI | REQ_POLL_CTX);
+	else
+		bio_set_flag(bio, BIO_END_BY_POLL);
+}
+
+static inline unsigned int bio_get_poll_data(struct bio *bio)
+{
+	return bio->bi_poll_data;
+}
+
+static inline void bio_set_poll_data(struct bio *bio, unsigned int data)
+{
+	bio->bi_poll_data = data;
+}
+
 #endif /* BLK_INTERNAL_H */
include/linux/blk_types.h +26 -1
index 99160d588c2d..3c276d163480 100644
@@ -234,8 +234,20 @@ struct bio {
 	atomic_t		__bi_remaining;
 
 	struct bvec_iter	bi_iter;
+	unsigned int		bi_poll_data;	/* fill hole of bi_iter */
 
-	bio_end_io_t		*bi_end_io;
+	union {
+		bio_end_io_t		*bi_end_io;
+		/*
+		 * bio based io polling needs to track bio via bio group
+		 * list which links bios by their .bi_end_io, and original
+		 * .bi_end_io is saved into the group head. Will recover
+		 * .bi_end_io before really ending bio. BIO_END_BY_POLL
+		 * will make sure that this bio won't be ended before
+		 * recovering .bi_end_io.
+		 */
+		void			*bi_poll;
+	};
 
 	void			*bi_private;
 #ifdef CONFIG_BLK_CGROUP
@@ -304,6 +316,9 @@ enum {
 	BIO_CGROUP_ACCT,	/* has been accounted to a cgroup */
 	BIO_TRACKED,		/* set if bio goes through the rq_qos path */
 	BIO_REMAPPED,
+	BIO_END_BY_POLL,	/* end by blk_bio_poll() explicitly */
+	/* set when bio can be ended, used for bio with BIO_END_BY_POLL */
+	BIO_DONE,
 	BIO_FLAG_LAST
 };
 
@@ -513,6 +528,16 @@ typedef unsigned int blk_qc_t;
 #define BLK_QC_T_NONE		-1U
 #define BLK_QC_T_SHIFT		16
 #define BLK_QC_T_INTERNAL	(1U << 31)
+/* only used for bio based submission, has to be defined as 0 */
+#define BLK_QC_T_BIO_NONE	0
+/* only used for bio based polling, not ready for polling */
+#define BLK_QC_T_NOT_READY	-2U
+
+/* not ready for bio based polling since this bio isn't submitted really */
+static inline bool blk_qc_t_ready(blk_qc_t cookie)
+{
+	return cookie != BLK_QC_T_NOT_READY;
+}
 
 static inline bool blk_qc_t_valid(blk_qc_t cookie)
 {
Signature · 1 line
2.29.2
Reviewed-by on v6 9/12
#13 lore raw

Re: [PATCH V6 09/12] block: use per-task poll context to implement bio based io polling

Quoted 39 lines · Ming Lei

Currently bio based IO polling needs to poll all hw queue blindly, this way is very inefficient, and one big reason is that we can't pass any bio submission result to blk_poll().

In IO submission context, track associated underlying bios by per-task submission queue and store returned 'cookie' in bio->bi_poll_data which is added by filling a hole of .bi_iter, and return current->pid to caller of submit_bio() for any bio based driver's IO, which is submitted from FS.

In IO poll context, the passed cookie tells us the PID of submission context, then we can find bios from the per-task io pull context of submission context. Moving bios from submission queue to poll queue of the poll context, and keep polling until these bios are ended. Remove bio from poll queue if the bio is ended. Add bio flags of BIO_DONE and BIO_END_BY_POLL for such purpose.

In was found in Jeffle Xu's test that kfifo doesn't scale well for a submission queue as queue depth is increased, so a new mechanism for tracking bios is needed. So far bio's size is close to 2 cacheline size, and it may not be accepted to add new field into bio for solving the scalability issue by tracking bios via linked list, switch to bio group list for tracking bio, the idea is to reuse .bi_end_io for linking bios into a linked list for all sharing same .bi_end_io(call it bio group), which is recovered before ending bio really, since BIO_END_BY_POLL is added for enhancing this point. Usually .bi_end_bio is same for all bios in same layer, so it is enough to provide very limited groups, such as 16 or less for fixing the scalability issue.

Usually submission shares context with io poll. The per-task poll context is just like stack variable, and it is cheap to move data between the two per-task queues.

Also when the submission task is exiting, drain pending IOs in the context until all are done.

Quoted 9 lines
---
 block/bio.c               |   5 +
 block/blk-core.c          |  39 ++++-
 block/blk-ioc.c           |   3 +
 block/blk-poll.c          | 345 +++++++++++++++++++++++++++++++++++++-
 block/blk.h               |  33 ++++
 include/linux/blk_types.h |  27 ++-
 6 files changed, 448 insertions(+), 4 deletions(-)

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		        Kernel Storage Architect
[email protected]			               +49 911 74053 688
SUSE Software Solutions Germany GmbH, 90409 Nürnberg
GF: F. Imendörffer, HRB 36809 (AG Nürnberg)
author
#14 lore raw

PATCH v6 10/12 block: limit hw queues to be polled in each blk_poll()

In reply to Ming Lei

Limit at most 8 queues are polled in each blk_pull(), avoid to add extra latency when queue depth is high.

---
 block/blk-poll.c | 78 ++++++++++++++++++++++++++++++++++--------------
 1 file changed, 55 insertions(+), 23 deletions(-)
block/blk-poll.c +55 -23
index 249d73ff6f81..20e7c47cc984 100644
@@ -288,36 +288,32 @@ static void bio_grp_list_move(struct bio_grp_list *dst,
 	src->nr_grps -= cnt;
 }
 
-static int blk_mq_poll_io(struct bio *bio)
+#define POLL_HCTX_MAX_CNT 8
+
+static bool blk_add_unique_hctx(struct blk_mq_hw_ctx **data, int *cnt,
+		struct blk_mq_hw_ctx *hctx)
 {
-	struct request_queue *q = bio->bi_bdev->bd_disk->queue;
-	blk_qc_t cookie = bio_get_poll_data(bio);
-	int ret = 0;
+	int i;
 
-	/* wait until the bio is submitted really */
-	if (!blk_qc_t_ready(cookie))
-		return 0;
 
-	if (!bio_flagged(bio, BIO_DONE) && blk_qc_t_valid(cookie)) {
-		struct blk_mq_hw_ctx *hctx =
-			q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
+	for (i = 0; i < *cnt; i++) {
+		if (data[i] == hctx)
+			goto exit;
+	}
 
-		ret += blk_mq_poll_hctx(q, hctx);
+	if (i < POLL_HCTX_MAX_CNT) {
+		data[i] = hctx;
+		(*cnt)++;
 	}
-	return ret;
+ exit:
+	return *cnt == POLL_HCTX_MAX_CNT;
 }
 
-static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
+static void blk_build_poll_queues(struct bio_grp_list *grps,
+		struct blk_mq_hw_ctx **data, int *cnt)
 {
-	int ret = 0;
 	int i;
 
-	/*
-	 * Poll hw queue first.
-	 *
-	 * TODO: limit max poll times and make sure to not poll same
-	 * hw queue one more time.
-	 */
 	for (i = 0; i < grps->nr_grps; i++) {
 		struct bio_grp_list_data *grp = &grps->head[i];
 		struct bio *bio;
@@ -325,11 +321,31 @@ static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
 		if (bio_grp_list_grp_empty(grp))
 			continue;
 
-		for (bio = grp->list.head; bio; bio = bio->bi_poll)
-			ret += blk_mq_poll_io(bio);
+		for (bio = grp->list.head; bio; bio = bio->bi_poll) {
+			blk_qc_t  cookie;
+			struct blk_mq_hw_ctx *hctx;
+			struct request_queue *q;
+
+			if (bio_flagged(bio, BIO_DONE))
+				continue;
+
+			/* wait until the bio is submitted really */
+			cookie = bio_get_poll_data(bio);
+			if (!blk_qc_t_ready(cookie) || !blk_qc_t_valid(cookie))
+				continue;
+
+			q = bio->bi_bdev->bd_disk->queue;
+			hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
+			if (blk_add_unique_hctx(data, cnt, hctx))
+				return;
+		}
 	}
+}
+
+static void blk_bio_poll_reap_ios(struct bio_grp_list *grps)
+{
+	int i;
 
-	/* reap bios */
 	for (i = 0; i < grps->nr_grps; i++) {
 		struct bio_grp_list_data *grp = &grps->head[i];
 		struct bio *bio;
@@ -354,6 +370,22 @@ static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
 		}
 		__bio_grp_list_merge(&grp->list, &bl);
 	}
+}
+
+static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
+{
+	int ret = 0;
+	int i;
+	struct blk_mq_hw_ctx *hctx[POLL_HCTX_MAX_CNT];
+	int cnt = 0;
+
+	blk_build_poll_queues(grps, hctx, &cnt);
+
+	for (i = 0; i < cnt; i++)
+		ret += blk_mq_poll_hctx(hctx[i]->queue, hctx[i]);
+
+	blk_bio_poll_reap_ios(grps);
+
 	return ret;
 }
 
Signature · 1 line
2.29.2
#15 lore raw

Re: [PATCH V6 10/12] block: limit hw queues to be polled in each blk_poll()

Quoted 115 lines · Ming Lei

Limit at most 8 queues are polled in each blk_pull(), avoid to add extra latency when queue depth is high.

---
 block/blk-poll.c | 78 ++++++++++++++++++++++++++++++++++--------------
 1 file changed, 55 insertions(+), 23 deletions(-)
block/blk-poll.c +48 -23
index 249d73ff6f81..20e7c47cc984 100644
@@ -288,36 +288,32 @@ static void bio_grp_list_move(struct bio_grp_list *dst,
 	src->nr_grps -= cnt;
 }
 
-static int blk_mq_poll_io(struct bio *bio)
+#define POLL_HCTX_MAX_CNT 8
+
+static bool blk_add_unique_hctx(struct blk_mq_hw_ctx **data, int *cnt,
+		struct blk_mq_hw_ctx *hctx)
 {
-	struct request_queue *q = bio->bi_bdev->bd_disk->queue;
-	blk_qc_t cookie = bio_get_poll_data(bio);
-	int ret = 0;
+	int i;
 
-	/* wait until the bio is submitted really */
-	if (!blk_qc_t_ready(cookie))
-		return 0;
 
-	if (!bio_flagged(bio, BIO_DONE) && blk_qc_t_valid(cookie)) {
-		struct blk_mq_hw_ctx *hctx =
-			q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
+	for (i = 0; i < *cnt; i++) {
+		if (data[i] == hctx)
+			goto exit;
+	}
 
-		ret += blk_mq_poll_hctx(q, hctx);
+	if (i < POLL_HCTX_MAX_CNT) {
+		data[i] = hctx;
+		(*cnt)++;
 	}
-	return ret;
+ exit:
+	return *cnt == POLL_HCTX_MAX_CNT;
 }
 
-static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
+static void blk_build_poll_queues(struct bio_grp_list *grps,
+		struct blk_mq_hw_ctx **data, int *cnt)
 {
-	int ret = 0;
 	int i;
 
-	/*
-	 * Poll hw queue first.
-	 *
-	 * TODO: limit max poll times and make sure to not poll same
-	 * hw queue one more time.
-	 */
 	for (i = 0; i < grps->nr_grps; i++) {
 		struct bio_grp_list_data *grp = &grps->head[i];
 		struct bio *bio;
@@ -325,11 +321,31 @@ static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
 		if (bio_grp_list_grp_empty(grp))
 			continue;
 
-		for (bio = grp->list.head; bio; bio = bio->bi_poll)
-			ret += blk_mq_poll_io(bio);
+		for (bio = grp->list.head; bio; bio = bio->bi_poll) {
+			blk_qc_t  cookie;
+			struct blk_mq_hw_ctx *hctx;
+			struct request_queue *q;
+
+			if (bio_flagged(bio, BIO_DONE))
+				continue;
+
+			/* wait until the bio is submitted really */
+			cookie = bio_get_poll_data(bio);
+			if (!blk_qc_t_ready(cookie) || !blk_qc_t_valid(cookie))
+				continue;
+
+			q = bio->bi_bdev->bd_disk->queue;
+			hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
+			if (blk_add_unique_hctx(data, cnt, hctx))
+				return;
+		}
 	}
+}
+
+static void blk_bio_poll_reap_ios(struct bio_grp_list *grps)
+{
+	int i;
 
-	/* reap bios */
 	for (i = 0; i < grps->nr_grps; i++) {
 		struct bio_grp_list_data *grp = &grps->head[i];
 		struct bio *bio;
@@ -354,6 +370,22 @@ static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
 		}
 		__bio_grp_list_merge(&grp->list, &bl);
 	}
+}
+
+static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
+{
+	int ret = 0;
+	int i;
+	struct blk_mq_hw_ctx *hctx[POLL_HCTX_MAX_CNT];
+	int cnt = 0;
+

block/blk-poll.c static int blk_bio_poll_and_end_io(struct bio_grp_list *grps) · line 391

Quoted 10 lines
block/blk-poll.c +7 -0
+	blk_build_poll_queues(grps, hctx, &cnt);
+
+	for (i = 0; i < cnt; i++)
+		ret += blk_mq_poll_hctx(hctx[i]->queue, hctx[i]);
+
+	blk_bio_poll_reap_ios(grps);
+
 	return ret;
 }
 

Can't we make it a sysfs attribute instead of hard-coding it? '8' seems a bit arbitrary to me, I'd rather have the ability to modify it...

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		        Kernel Storage Architect
[email protected]			               +49 911 74053 688
SUSE Software Solutions Germany GmbH, 90409 Nürnberg
GF: F. Imendörffer, HRB 36809 (AG Nürnberg)
author
#16 lore raw

Re: [PATCH V6 10/12] block: limit hw queues to be polled in each blk_poll()

Quoted 120 lines · Hannes Reinecke
Quoted 119 lines · Ming Lei

Limit at most 8 queues are polled in each blk_pull(), avoid to add extra latency when queue depth is high.

---
 block/blk-poll.c | 78 ++++++++++++++++++++++++++++++++++--------------
 1 file changed, 55 insertions(+), 23 deletions(-)
block/blk-poll.c +53 -23
index 249d73ff6f81..20e7c47cc984 100644
@@ -288,36 +288,32 @@ static void bio_grp_list_move(struct bio_grp_list *dst,
 	src->nr_grps -= cnt;
 }
 
-static int blk_mq_poll_io(struct bio *bio)
+#define POLL_HCTX_MAX_CNT 8
+
+static bool blk_add_unique_hctx(struct blk_mq_hw_ctx **data, int *cnt,
+		struct blk_mq_hw_ctx *hctx)
 {
-	struct request_queue *q = bio->bi_bdev->bd_disk->queue;
-	blk_qc_t cookie = bio_get_poll_data(bio);
-	int ret = 0;
+	int i;
 
-	/* wait until the bio is submitted really */
-	if (!blk_qc_t_ready(cookie))
-		return 0;
 
-	if (!bio_flagged(bio, BIO_DONE) && blk_qc_t_valid(cookie)) {
-		struct blk_mq_hw_ctx *hctx =
-			q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
+	for (i = 0; i < *cnt; i++) {
+		if (data[i] == hctx)
+			goto exit;
+	}
 
-		ret += blk_mq_poll_hctx(q, hctx);
+	if (i < POLL_HCTX_MAX_CNT) {
+		data[i] = hctx;
+		(*cnt)++;
 	}
-	return ret;
+ exit:
+	return *cnt == POLL_HCTX_MAX_CNT;
 }
 
-static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
+static void blk_build_poll_queues(struct bio_grp_list *grps,
+		struct blk_mq_hw_ctx **data, int *cnt)
 {
-	int ret = 0;
 	int i;
 
-	/*
-	 * Poll hw queue first.
-	 *
-	 * TODO: limit max poll times and make sure to not poll same
-	 * hw queue one more time.
-	 */
 	for (i = 0; i < grps->nr_grps; i++) {
 		struct bio_grp_list_data *grp = &grps->head[i];
 		struct bio *bio;
@@ -325,11 +321,31 @@ static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
 		if (bio_grp_list_grp_empty(grp))
 			continue;
 
-		for (bio = grp->list.head; bio; bio = bio->bi_poll)
-			ret += blk_mq_poll_io(bio);
+		for (bio = grp->list.head; bio; bio = bio->bi_poll) {
+			blk_qc_t  cookie;
+			struct blk_mq_hw_ctx *hctx;
+			struct request_queue *q;
+
+			if (bio_flagged(bio, BIO_DONE))
+				continue;
+
+			/* wait until the bio is submitted really */
+			cookie = bio_get_poll_data(bio);
+			if (!blk_qc_t_ready(cookie) || !blk_qc_t_valid(cookie))
+				continue;
+
+			q = bio->bi_bdev->bd_disk->queue;
+			hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
+			if (blk_add_unique_hctx(data, cnt, hctx))
+				return;
+		}
 	}
+}
+
+static void blk_bio_poll_reap_ios(struct bio_grp_list *grps)
+{
+	int i;
 
-	/* reap bios */
 	for (i = 0; i < grps->nr_grps; i++) {
 		struct bio_grp_list_data *grp = &grps->head[i];
 		struct bio *bio;
@@ -354,6 +370,22 @@ static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
 		}
 		__bio_grp_list_merge(&grp->list, &bl);
 	}
+}
+
+static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
+{
+	int ret = 0;
+	int i;
+	struct blk_mq_hw_ctx *hctx[POLL_HCTX_MAX_CNT];
+	int cnt = 0;
+
+	blk_build_poll_queues(grps, hctx, &cnt);
+
+	for (i = 0; i < cnt; i++)
+		ret += blk_mq_poll_hctx(hctx[i]->queue, hctx[i]);
+
Quoted 8 lines

block/blk-poll.c static int blk_bio_poll_and_end_io(struct bio_grp_list *grps) · line 391

Quoted 6 lines
block/blk-poll.c +2 -0
+	blk_bio_poll_reap_ios(grps);
+
 	return ret;
 }
 

Can't we make it a sysfs attribute instead of hard-coding it? '8' seems a bit arbitrary to me, I'd rather have the ability to modify it...

I'd rather not add such code in the feature 'enablement' stage since I doesn't observe the number plays a big role yet. It is added for holding hw queues to be polled on stack variables, also avoid to add too much latency if there is too many bios from too many hw queues to be reaped.

Also the actual polled hw queues can be observed easily via bpftrace, so debug purpose from sysfs isn't necessary too.

Thanks,
Ming

Reviewed-by
#17 lore raw

Re: [PATCH V6 10/12] block: limit hw queues to be polled in each blk_poll()

Quoted 130 lines · Ming Lei
Quoted 120 lines · Hannes Reinecke
Quoted 119 lines · Ming Lei

Limit at most 8 queues are polled in each blk_pull(), avoid to add extra latency when queue depth is high.

---
 block/blk-poll.c | 78 ++++++++++++++++++++++++++++++++++--------------
 1 file changed, 55 insertions(+), 23 deletions(-)
block/blk-poll.c +53 -23
index 249d73ff6f81..20e7c47cc984 100644
@@ -288,36 +288,32 @@ static void bio_grp_list_move(struct bio_grp_list *dst,
 	src->nr_grps -= cnt;
 }
 
-static int blk_mq_poll_io(struct bio *bio)
+#define POLL_HCTX_MAX_CNT 8
+
+static bool blk_add_unique_hctx(struct blk_mq_hw_ctx **data, int *cnt,
+		struct blk_mq_hw_ctx *hctx)
 {
-	struct request_queue *q = bio->bi_bdev->bd_disk->queue;
-	blk_qc_t cookie = bio_get_poll_data(bio);
-	int ret = 0;
+	int i;
 
-	/* wait until the bio is submitted really */
-	if (!blk_qc_t_ready(cookie))
-		return 0;
 
-	if (!bio_flagged(bio, BIO_DONE) && blk_qc_t_valid(cookie)) {
-		struct blk_mq_hw_ctx *hctx =
-			q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
+	for (i = 0; i < *cnt; i++) {
+		if (data[i] == hctx)
+			goto exit;
+	}
 
-		ret += blk_mq_poll_hctx(q, hctx);
+	if (i < POLL_HCTX_MAX_CNT) {
+		data[i] = hctx;
+		(*cnt)++;
 	}
-	return ret;
+ exit:
+	return *cnt == POLL_HCTX_MAX_CNT;
 }
 
-static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
+static void blk_build_poll_queues(struct bio_grp_list *grps,
+		struct blk_mq_hw_ctx **data, int *cnt)
 {
-	int ret = 0;
 	int i;
 
-	/*
-	 * Poll hw queue first.
-	 *
-	 * TODO: limit max poll times and make sure to not poll same
-	 * hw queue one more time.
-	 */
 	for (i = 0; i < grps->nr_grps; i++) {
 		struct bio_grp_list_data *grp = &grps->head[i];
 		struct bio *bio;
@@ -325,11 +321,31 @@ static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
 		if (bio_grp_list_grp_empty(grp))
 			continue;
 
-		for (bio = grp->list.head; bio; bio = bio->bi_poll)
-			ret += blk_mq_poll_io(bio);
+		for (bio = grp->list.head; bio; bio = bio->bi_poll) {
+			blk_qc_t  cookie;
+			struct blk_mq_hw_ctx *hctx;
+			struct request_queue *q;
+
+			if (bio_flagged(bio, BIO_DONE))
+				continue;
+
+			/* wait until the bio is submitted really */
+			cookie = bio_get_poll_data(bio);
+			if (!blk_qc_t_ready(cookie) || !blk_qc_t_valid(cookie))
+				continue;
+
+			q = bio->bi_bdev->bd_disk->queue;
+			hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
+			if (blk_add_unique_hctx(data, cnt, hctx))
+				return;
+		}
 	}
+}
+
+static void blk_bio_poll_reap_ios(struct bio_grp_list *grps)
+{
+	int i;
 
-	/* reap bios */
 	for (i = 0; i < grps->nr_grps; i++) {
 		struct bio_grp_list_data *grp = &grps->head[i];
 		struct bio *bio;
@@ -354,6 +370,22 @@ static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
 		}
 		__bio_grp_list_merge(&grp->list, &bl);
 	}
+}
+
+static int blk_bio_poll_and_end_io(struct bio_grp_list *grps)
+{
+	int ret = 0;
+	int i;
+	struct blk_mq_hw_ctx *hctx[POLL_HCTX_MAX_CNT];
+	int cnt = 0;
+
+	blk_build_poll_queues(grps, hctx, &cnt);
+
+	for (i = 0; i < cnt; i++)
+		ret += blk_mq_poll_hctx(hctx[i]->queue, hctx[i]);
+
Quoted 8 lines

block/blk-poll.c static int blk_bio_poll_and_end_io(struct bio_grp_list *grps) · line 391

Quoted 6 lines
block/blk-poll.c +2 -0
+	blk_bio_poll_reap_ios(grps);
+
 	return ret;
 }
 

Can't we make it a sysfs attribute instead of hard-coding it? '8' seems a bit arbitrary to me, I'd rather have the ability to modify it...

Quoted 8 lines

I'd rather not add such code in the feature 'enablement' stage since I doesn't observe the number plays a big role yet. It is added for holding hw queues to be polled on stack variables, also avoid to add too much latency if there is too many bios from too many hw queues to be reaped.

Also the actual polled hw queues can be observed easily via bpftrace, so debug purpose from sysfs isn't necessary too.

Okay. You can add my

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		        Kernel Storage Architect
[email protected]			               +49 911 74053 688
SUSE Software Solutions Germany GmbH, 90409 Nürnberg
GF: F. Imendörffer, HRB 36809 (AG Nürnberg)
author
#18 lore raw

PATCH v6 11/12 block: allow to control FLAG_POLL via sysfs for bio poll capable queue

In reply to Ming Lei

Prepare for supporting bio based io polling. If one disk is capable of bio polling, we allow user to control FLAG_POLL via sysfs.

---
 block/blk-sysfs.c     | 14 ++++++++++++--
 include/linux/genhd.h |  2 ++
 2 files changed, 14 insertions(+), 2 deletions(-)
block/blk-sysfs.c +12 -2
index fed4981b1f7a..3620db390658 100644
@@ -430,9 +430,14 @@ static ssize_t queue_poll_store(struct request_queue *q, const char *page,
 {
 	unsigned long poll_on;
 	ssize_t ret;
+	struct gendisk *disk = queue_to_disk(q);
 
-	if (!q->tag_set || q->tag_set->nr_maps <= HCTX_TYPE_POLL ||
-	    !q->tag_set->map[HCTX_TYPE_POLL].nr_queues)
+	if (!queue_is_mq(q) && !(disk->flags & GENHD_FL_CAP_BIO_POLL))
+		return -EINVAL;
+
+	if (queue_is_mq(q) && (!q->tag_set ||
+	    q->tag_set->nr_maps <= HCTX_TYPE_POLL ||
+	    !q->tag_set->map[HCTX_TYPE_POLL].nr_queues))
 		return -EINVAL;
 
 	ret = queue_var_store(&poll_on, page, count);
@@ -442,6 +447,11 @@ static ssize_t queue_poll_store(struct request_queue *q, const char *page,
 	if (poll_on) {
 		blk_queue_flag_set(QUEUE_FLAG_POLL, q);
 	} else {
+		/*
+		 * For bio queue, it is safe to just freeze bio submission
+		 * activity because we don't read FLAG_POLL after bio is
+		 * submitted.
+		 */
 		blk_mq_freeze_queue(q);
 		blk_queue_flag_clear(QUEUE_FLAG_POLL, q);
 		blk_mq_unfreeze_queue(q);
include/linux/genhd.h +2 -0
index 7e9660ea967d..e5ae77cba853 100644
@@ -104,6 +104,8 @@ struct partition_meta_info {
 #define GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE	0x0100
 #define GENHD_FL_NO_PART_SCAN			0x0200
 #define GENHD_FL_HIDDEN				0x0400
+/* only valid for bio based disk */
+#define GENHD_FL_CAP_BIO_POLL			0x0800
 
 enum {
 	DISK_EVENT_MEDIA_CHANGE			= 1 << 0, /* media changed */
Signature · 1 line
2.29.2
Reviewed-by on v6 11/12
#19 lore raw

Re: [PATCH V6 11/12] block: allow to control FLAG_POLL via sysfs for bio poll capable queue

Quoted 10 lines · Ming Lei

Prepare for supporting bio based io polling. If one disk is capable of bio polling, we allow user to control FLAG_POLL via sysfs.

---
 block/blk-sysfs.c     | 14 ++++++++++++--
 include/linux/genhd.h |  2 ++
 2 files changed, 14 insertions(+), 2 deletions(-)

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		        Kernel Storage Architect
[email protected]			               +49 911 74053 688
SUSE Software Solutions Germany GmbH, 90409 Nürnberg
GF: F. Imendörffer, HRB 36809 (AG Nürnberg)
author
#20 lore raw

PATCH v6 12/12 dm: support IO polling for bio-based dm device

In reply to Ming Lei

From: Jeffle Xu <[email protected]>

IO polling is enabled when all underlying target devices are capable of IO polling. The sanity check supports the stacked device model, in which one dm device may be build upon another dm device. In this case, the mapped device will check if the underlying dm target device supports IO polling.

---
 drivers/md/dm-table.c         | 24 ++++++++++++++++++++++++
 drivers/md/dm.c               |  2 ++
 include/linux/device-mapper.h |  1 +
 3 files changed, 27 insertions(+)
drivers/md/dm-table.c +24 -0
index 95391f78b8d5..a8f3575fb118 100644
@@ -1509,6 +1509,12 @@ struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
 	return &t->targets[(KEYS_PER_NODE * n) + k];
 }
 
+static int device_not_poll_capable(struct dm_target *ti, struct dm_dev *dev,
+				   sector_t start, sector_t len, void *data)
+{
+	return !blk_queue_poll(bdev_get_queue(dev->bdev));
+}
+
 /*
  * type->iterate_devices() should be called when the sanity check needs to
  * iterate and check all underlying data devices. iterate_devices() will
@@ -1559,6 +1565,11 @@ static int count_device(struct dm_target *ti, struct dm_dev *dev,
 	return 0;
 }
 
+int dm_table_supports_poll(struct dm_table *t)
+{
+	return !dm_table_any_dev_attr(t, device_not_poll_capable, NULL);
+}
+
 /*
  * Check whether a table has no data devices attached using each
  * target's iterate_devices method.
@@ -2079,6 +2090,19 @@ void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
 
 	dm_update_keyslot_manager(q, t);
 	blk_queue_update_readahead(q);
+
+	/*
+	 * Check for request-based device is remained to
+	 * dm_mq_init_request_queue()->blk_mq_init_allocated_queue().
+	 * For bio-based device, only set QUEUE_FLAG_POLL when all underlying
+	 * devices supporting polling.
+	 */
+	if (__table_type_bio_based(t->type)) {
+		if (dm_table_supports_poll(t))
+			blk_queue_flag_set(QUEUE_FLAG_POLL, q);
+		else
+			blk_queue_flag_clear(QUEUE_FLAG_POLL, q);
+	}
 }
 
 unsigned int dm_table_get_num_targets(struct dm_table *t)
drivers/md/dm.c +2 -0
index 50b693d776d6..1b160e4e6446 100644
@@ -2175,6 +2175,8 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
 		}
 		break;
 	case DM_TYPE_BIO_BASED:
+		/* tell block layer we are capable of bio polling */
+		md->disk->flags |= GENHD_FL_CAP_BIO_POLL;
 	case DM_TYPE_DAX_BIO_BASED:
 		break;
 	case DM_TYPE_NONE:
include/linux/device-mapper.h +1 -0
index 7f4ac87c0b32..31bfd6f70013 100644
@@ -538,6 +538,7 @@ unsigned int dm_table_get_num_targets(struct dm_table *t);
 fmode_t dm_table_get_mode(struct dm_table *t);
 struct mapped_device *dm_table_get_md(struct dm_table *t);
 const char *dm_table_device_name(struct dm_table *t);
+int dm_table_supports_poll(struct dm_table *t);
 
 /*
  * Trigger an event.
Signature · 1 line
2.29.2
#21 lore raw

Re: [PATCH V6 12/12] dm: support IO polling for bio-based dm device

Quoted 36 lines · Ming Lei

From: Jeffle Xu <[email protected]>

IO polling is enabled when all underlying target devices are capable of IO polling. The sanity check supports the stacked device model, in which one dm device may be build upon another dm device. In this case, the mapped device will check if the underlying dm target device supports IO polling.

---
 drivers/md/dm-table.c         | 24 ++++++++++++++++++++++++
 drivers/md/dm.c               |  2 ++
 include/linux/device-mapper.h |  1 +
 3 files changed, 27 insertions(+)
drivers/md/dm-table.c +6 -0
index 95391f78b8d5..a8f3575fb118 100644
@@ -1509,6 +1509,12 @@ struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
 	return &t->targets[(KEYS_PER_NODE * n) + k];
 }
 
+static int device_not_poll_capable(struct dm_target *ti, struct dm_dev *dev,
+				   sector_t start, sector_t len, void *data)
+{
+	return !blk_queue_poll(bdev_get_queue(dev->bdev));
+}
+
 /*
  * type->iterate_devices() should be called when the sanity check needs to
  * iterate and check all underlying data devices. iterate_devices() will

drivers/md/dm-table.c static int count_device(struct dm_target *ti, struct dm_dev *dev, · line 1572

Quoted 8 lines
drivers/md/dm-table.c +5 -0
@@ -1559,6 +1565,11 @@ static int count_device(struct dm_target *ti, struct dm_dev *dev,
 	return 0;
 }
 
+int dm_table_supports_poll(struct dm_table *t)
+{
+	return !dm_table_any_dev_attr(t, device_not_poll_capable, NULL);
+}
+

Since .poll_capable() has been dropped, dm_table_supports_poll() can be declared as 'static' here.

Quoted 43 lines
drivers/md/dm-table.c +13 -0
 /*
  * Check whether a table has no data devices attached using each
  * target's iterate_devices method.
@@ -2079,6 +2090,19 @@ void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
 
 	dm_update_keyslot_manager(q, t);
 	blk_queue_update_readahead(q);
+
+	/*
+	 * Check for request-based device is remained to
+	 * dm_mq_init_request_queue()->blk_mq_init_allocated_queue().
+	 * For bio-based device, only set QUEUE_FLAG_POLL when all underlying
+	 * devices supporting polling.
+	 */
+	if (__table_type_bio_based(t->type)) {
+		if (dm_table_supports_poll(t))
+			blk_queue_flag_set(QUEUE_FLAG_POLL, q);
+		else
+			blk_queue_flag_clear(QUEUE_FLAG_POLL, q);
+	}
 }
 
 unsigned int dm_table_get_num_targets(struct dm_table *t)
drivers/md/dm.c +2 -0
index 50b693d776d6..1b160e4e6446 100644
@@ -2175,6 +2175,8 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
 		}
 		break;
 	case DM_TYPE_BIO_BASED:
+		/* tell block layer we are capable of bio polling */
+		md->disk->flags |= GENHD_FL_CAP_BIO_POLL;
 	case DM_TYPE_DAX_BIO_BASED:
 		break;
 	case DM_TYPE_NONE:
include/linux/device-mapper.h +0 -0
index 7f4ac87c0b32..31bfd6f70013 100644

include/linux/device-mapper.h unsigned int dm_table_get_num_targets(struct dm_table *t); · line 541

Quoted 4 lines
include/linux/device-mapper.h +1 -0
@@ -538,6 +538,7 @@ unsigned int dm_table_get_num_targets(struct dm_table *t);
 fmode_t dm_table_get_mode(struct dm_table *t);
 struct mapped_device *dm_table_get_md(struct dm_table *t);
 const char *dm_table_device_name(struct dm_table *t);
+int dm_table_supports_poll(struct dm_table *t);

Similarly, dm_table_supports_poll() doesn't need to be exported.

Signature · 2 lines
Thanks,
Jeffle
author
#22 lore raw

Re: [PATCH V6 12/12] dm: support IO polling for bio-based dm device

Quoted 89 lines · JeffleXu
Quoted 36 lines · Ming Lei

From: Jeffle Xu <[email protected]>

IO polling is enabled when all underlying target devices are capable of IO polling. The sanity check supports the stacked device model, in which one dm device may be build upon another dm device. In this case, the mapped device will check if the underlying dm target device supports IO polling.

---
 drivers/md/dm-table.c         | 24 ++++++++++++++++++++++++
 drivers/md/dm.c               |  2 ++
 include/linux/device-mapper.h |  1 +
 3 files changed, 27 insertions(+)
drivers/md/dm-table.c +6 -0
index 95391f78b8d5..a8f3575fb118 100644
@@ -1509,6 +1509,12 @@ struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
 	return &t->targets[(KEYS_PER_NODE * n) + k];
 }
 
+static int device_not_poll_capable(struct dm_target *ti, struct dm_dev *dev,
+				   sector_t start, sector_t len, void *data)
+{
+	return !blk_queue_poll(bdev_get_queue(dev->bdev));
+}
+
 /*
  * type->iterate_devices() should be called when the sanity check needs to
  * iterate and check all underlying data devices. iterate_devices() will

drivers/md/dm-table.c static int count_device(struct dm_target *ti, struct dm_dev *dev, · line 1572

Quoted 8 lines
drivers/md/dm-table.c +5 -0
@@ -1559,6 +1565,11 @@ static int count_device(struct dm_target *ti, struct dm_dev *dev,
 	return 0;
 }
 
+int dm_table_supports_poll(struct dm_table *t)
+{
+	return !dm_table_any_dev_attr(t, device_not_poll_capable, NULL);
+}
+

Since .poll_capable() has been dropped, dm_table_supports_poll() can be declared as 'static' here.

Quoted 36 lines
drivers/md/dm-table.c +13 -0
 /*
  * Check whether a table has no data devices attached using each
  * target's iterate_devices method.
@@ -2079,6 +2090,19 @@ void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
 
 	dm_update_keyslot_manager(q, t);
 	blk_queue_update_readahead(q);
+
+	/*
+	 * Check for request-based device is remained to
+	 * dm_mq_init_request_queue()->blk_mq_init_allocated_queue().
+	 * For bio-based device, only set QUEUE_FLAG_POLL when all underlying
+	 * devices supporting polling.
+	 */
+	if (__table_type_bio_based(t->type)) {
+		if (dm_table_supports_poll(t))
+			blk_queue_flag_set(QUEUE_FLAG_POLL, q);
+		else
+			blk_queue_flag_clear(QUEUE_FLAG_POLL, q);
+	}
 }
 
 unsigned int dm_table_get_num_targets(struct dm_table *t)
drivers/md/dm.c +2 -0
index 50b693d776d6..1b160e4e6446 100644
@@ -2175,6 +2175,8 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
 		}
 		break;
 	case DM_TYPE_BIO_BASED:
+		/* tell block layer we are capable of bio polling */
+		md->disk->flags |= GENHD_FL_CAP_BIO_POLL;
 	case DM_TYPE_DAX_BIO_BASED:
 		break;
 	case DM_TYPE_NONE:
Quoted 11 lines

include/linux/device-mapper.h unsigned int dm_table_get_num_targets(struct dm_table *t); · line 541

Quoted 9 lines
include/linux/device-mapper.h +1 -0
index 7f4ac87c0b32..31bfd6f70013 100644
@@ -538,6 +538,7 @@ unsigned int dm_table_get_num_targets(struct dm_table *t);
 fmode_t dm_table_get_mode(struct dm_table *t);
 struct mapped_device *dm_table_get_md(struct dm_table *t);
 const char *dm_table_device_name(struct dm_table *t);
+int dm_table_supports_poll(struct dm_table *t);

Similarly, dm_table_supports_poll() doesn't need to be exported.

Yeah, has fixed it in V7.

Thanks,
Ming

author
#23 lore raw

PATCH v7 12/12 dm: support IO polling for bio-based dm device

In reply to Ming Lei

From 98a73d99c3c663a3cfaadfec2825d6d88289a102 Mon Sep 17 00:00:00 2001
From: Jeffle Xu <[email protected]>
Date: Mon, 8 Feb 2021 16:52:41 +0800
Subject: [PATCH V7 12/12] dm: support IO polling for bio-based dm device

IO polling is enabled when all underlying target devices are capable of IO polling. The sanity check supports the stacked device model, in which one dm device may be build upon another dm device. In this case, the mapped device will check if the underlying dm target device supports IO polling.

---
V7:
- don't export dm_table_supports_poll, as suggested by Jeffle

 drivers/md/dm-table.c | 24 ++++++++++++++++++++++++
 drivers/md/dm.c       |  2 ++
 2 files changed, 26 insertions(+)
drivers/md/dm-table.c +24 -0
index 95391f78b8d5..0b3e34cbe241 100644
@@ -1509,6 +1509,12 @@ struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
 	return &t->targets[(KEYS_PER_NODE * n) + k];
 }
 
+static int device_not_poll_capable(struct dm_target *ti, struct dm_dev *dev,
+				   sector_t start, sector_t len, void *data)
+{
+	return !blk_queue_poll(bdev_get_queue(dev->bdev));
+}
+
 /*
  * type->iterate_devices() should be called when the sanity check needs to
  * iterate and check all underlying data devices. iterate_devices() will
@@ -1559,6 +1565,11 @@ static int count_device(struct dm_target *ti, struct dm_dev *dev,
 	return 0;
 }
 
+static int dm_table_supports_poll(struct dm_table *t)
+{
+	return !dm_table_any_dev_attr(t, device_not_poll_capable, NULL);
+}
+
 /*
  * Check whether a table has no data devices attached using each
  * target's iterate_devices method.
@@ -2079,6 +2090,19 @@ void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
 
 	dm_update_keyslot_manager(q, t);
 	blk_queue_update_readahead(q);
+
+	/*
+	 * Check for request-based device is remained to
+	 * dm_mq_init_request_queue()->blk_mq_init_allocated_queue().
+	 * For bio-based device, only set QUEUE_FLAG_POLL when all underlying
+	 * devices supporting polling.
+	 */
+	if (__table_type_bio_based(t->type)) {
+		if (dm_table_supports_poll(t))
+			blk_queue_flag_set(QUEUE_FLAG_POLL, q);
+		else
+			blk_queue_flag_clear(QUEUE_FLAG_POLL, q);
+	}
 }
 
 unsigned int dm_table_get_num_targets(struct dm_table *t)
drivers/md/dm.c +2 -0
index 50b693d776d6..1b160e4e6446 100644
@@ -2175,6 +2175,8 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
 		}
 		break;
 	case DM_TYPE_BIO_BASED:
+		/* tell block layer we are capable of bio polling */
+		md->disk->flags |= GENHD_FL_CAP_BIO_POLL;
 	case DM_TYPE_DAX_BIO_BASED:
 		break;
 	case DM_TYPE_NONE:
Signature · 1 line
2.29.2
#24 lore raw
In reply to Ming Lei

Hi all,

What's the latest progress of this bio-based polling feature?

I've noticed that hch has also sent a patch set on this [1]. But as far as I know, hch's patch set only refactors the interface of polling in the block layer. It indeed helps bio-based polling for some kind of bio-based driver, but for DM/MD where one bio could be mapped to several split bios, more work is obviously needed, just like Lei Ming's io_context related code in this patch set.

hch may have better idea, after all [1] is just a preparation patch set.

[1]
https://lore.kernel.org/linux-block/[email protected]/T/

Signature · 2 lines
Thanks,
Jeffle
Quoted 97 lines · Ming Lei

Hi Jens,

Add per-task io poll context for holding HIPRI blk-mq/underlying bios queued from bio based driver's io submission context, and reuse one bio padding field for storing 'cookie' returned from submit_bio() for these bios. Also explicitly end these bios in poll context by adding two new bio flags.

In this way, we needn't to poll all underlying hw queues any more, which is implemented in Jeffle's patches. And we can just poll hw queues in which there is HIPRI IO queued.

Usually io submission and io poll share same context, so the added io poll context data is just like one stack variable, and the cost for saving bios is cheap.

V6:
- move poll code into block/blk-poll.c, as suggested by Christoph
- define bvec_iter as __packed, and add one new field to bio, as suggested by Christoph
- re-organize patch order, as suggested by Christoph
- add one flag for checking if the disk is capable of bio polling and remove .poll_capable(), as suggested by Christoph
- fix type of .bi_poll

V5:
- fix one use-after-free issue in case that polling is from another
context: adds one new cookie of BLK_QC_T_NOT_READY for preventing this issue in patch 8/12
- add reviewed-by & tested-by tag

V4:
- cover one more test_bit(QUEUE_FLAG_POLL, ...) suggested by Jeffle(01/12)
- drop patch of 'block: add helper of blk_create_io_context'
- add new helper of blk_create_io_poll_context() (03/12)
- drain submission queues in exit_io_context(), suggested by Jeffle(08/13)
- considering shared io context case for blk_bio_poll_io_drain() (08/13)
- fix one issue in blk_bio_poll_pack_groups() as suggested by Jeffle(08/13)
- add reviewed-by tag
V3:
- fix cookie returned for bio based driver, as suggested by Jeffle Xu
- draining pending bios when submission context is exiting
- patch style and comment fix, as suggested by Mike
- allow poll context data to be NULL by always polling on submission queue
- remove RFC, and reviewed-by

V2:
- address queue depth scalability issue reported by Jeffle via bio group list. Reuse .bi_end_io for linking bios which share same .bi_end_io, and support 32 such groups in submit queue. With this way, the scalability issue caused by kfifio is solved. Before really ending bio, .bi_end_io is recovered from the group head.

Jeffle Xu (2):
block: extract one helper function polling hw queue
dm: support IO polling for bio-based dm device

Ming Lei (10):
block: add helper of blk_queue_poll
block: define 'struct bvec_iter' as packed
block: add one helper to free io_context
block: move block polling code into one dedicated source file
block: prepare for supporting bio_list via other link
block: create io poll context for submission and poll task
block: add req flag of REQ_POLL_CTX
block: use per-task poll context to implement bio based io polling
block: limit hw queues to be polled in each blk_poll()
block: allow to control FLAG_POLL via sysfs for bio poll capable queue

 block/Makefile                |   3 +-
 block/bio.c                   |   5 +
 block/blk-core.c              |  68 +++-
 block/blk-ioc.c               |  15 +-
 block/blk-mq.c                | 231 -------------
 block/blk-mq.h                |  40 +++
 block/blk-poll.c              | 632 ++++++++++++++++++++++++++++++++++
 block/blk-sysfs.c             |  16 +-
 block/blk.h                   | 112 ++++++
 drivers/md/dm-table.c         |  24 ++
 drivers/md/dm.c               |   2 +
 drivers/nvme/host/core.c      |   2 +-
 include/linux/bio.h           | 132 +++----
 include/linux/blk_types.h     |  31 +-
 include/linux/blkdev.h        |   1 +
 include/linux/bvec.h          |   2 +-
 include/linux/device-mapper.h |   1 +
 include/linux/genhd.h         |   2 +
 include/linux/iocontext.h     |   2 +
 19 files changed, 1003 insertions(+), 318 deletions(-)
 create mode 100644 block/blk-poll.c
author
#25 lore raw

Hi JeffleXu,

Quoted 4 lines · JeffleXu

Hi all,

What's the latest progress of this bio-based polling feature?

Quoted 8 lines

I've noticed that hch has also sent a patch set on this [1]. But as far as I know, hch's patch set only refactors the interface of polling in the block layer. It indeed helps bio-based polling for some kind of bio-based driver, but for DM/MD where one bio could be mapped to several split bios, more work is obviously needed, just like Lei Ming's io_context related code in this patch set.

hch may have better idea, after all [1] is just a preparation patch set.

Yeah, we have to rebase V6 against Christoph's patchset anyway.

Looks there is at least two approaches left for us:

1) keep the generic approach in V6, just rebase after Christoph's patch is finalized

2) support io polling simply in bio driver, since bio->bi_cookie is assigned for underlying bio, and it shouldn't be very difficult to support that in DM/MD. I have been thinking of it a while, but not coding it yet. BTW, all underlying bios can be linked to DM bio->bi_next, and we can add one new callback of .io_poll for polling DM/MD's bio.

Thanks,
Ming

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel