block: support to freeze bio based queue
Subsystems (5)
cover block: support to freeze bio based queue
Hello,
For bio based request queue, the queue usage refcnt is only grabbed during submission, which isn't consistent with request base queue.
Queue freezing has been used widely, and turns out it is very useful to quiesce queue activity.
So try to support to freeze bio based queue by ->q_usage_counter.
Any comment are welcome!
Ming Lei (2):
percpu_ref: add percpu_ref_tryget_many_live
block: support to freeze bio based request queue
block/bio.c | 12 ++++++++++-- block/blk-core.c | 23 +++++++++++++++++------ drivers/nvme/host/core.c | 16 ++++++++++++++++ drivers/nvme/host/multipath.c | 6 ++++++ include/linux/blk-mq.h | 2 ++ include/linux/blk_types.h | 1 + include/linux/blkdev.h | 7 ++++++- include/linux/percpu-refcount.h | 30 ++++++++++++++++++++++++++---- 8 files changed, 84 insertions(+), 13 deletions(-)
Signature · 1 line
2.29.2
RFC PATCH 1/2 percpu_ref: add percpu_ref_tryget_many_live
Prepare for support freezing bio based request queues.
--- include/linux/percpu-refcount.h | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-)
include/linux/percpu-refcount.h +26 -4
@@ -267,8 +267,9 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref) }/** -* percpu_ref_tryget_live - try to increment a live percpu refcount +* percpu_ref_tryget_many_live - try to increment a live percpu refcount * @ref: percpu_ref to try-get +* @nr: number of references to get * * Increment a percpu refcount unless it has already been killed. Returns * %true on success; %false on failure.
@@ -281,7 +282,8 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref)* * This function is safe to call as long as @ref is between init and exit. */ -static inline bool percpu_ref_tryget_live (struct percpu_ref *ref) +static inline bool percpu_ref_tryget_many_live (struct percpu_ref *ref, +unsigned long nr) {unsigned long __percpu *percpu_count;bool ret = false;
@@ -289,10 +291,10 @@ static inline bool percpu_ref_tryget_live(struct percpu_ref *ref)rcu_read_lock ();if (__ref_is_percpu (ref, &percpu_count)) { -this_cpu_inc (*percpu_count); +this_cpu_add (*percpu_count, nr); ret = true; }else if (!(ref->percpu_count_ptr & __PERCPU_REF_DEAD)) { - ret =atomic_long_inc_not_zero (&ref->data->count); + ret =atomic_long_add_unless (&ref->data->count, nr,0 ); }rcu_read_unlock ();
@@ -300,6 +302,26 @@ static inline bool percpu_ref_tryget_live(struct percpu_ref *ref)return ret; } +/** +* percpu_ref_tryget_live - try to increment a live percpu refcount +* @ref: percpu_ref to try-get +* +* Increment a percpu refcount unless it has already been killed. Returns +* %true on success; %false on failure. +* +* Completion of percpu_ref_kill() in itself doesn't guarantee that this +* function will fail. For such guarantee, percpu_ref_kill_and_confirm() +* should be used. After the confirm_kill callback is invoked, it's +* guaranteed that no new reference will be given out by +* percpu_ref_tryget_live(). +* +* This function is safe to call as long as @ref is between init and exit. +*/ +static inline bool percpu_ref_tryget_live (struct percpu_ref *ref) +{ +return percpu_ref_tryget_many_live (ref,1 ); +} +/** * percpu_ref_put_many - decrement a percpu refcount * @ref: percpu_ref to put
Signature · 1 line
2.29.2
Re: [RFC PATCH 1/2] percpu_ref: add percpu_ref_tryget_many_live
Quoted 5 lines · Ming Lei
/**
- *percpu_ref_tryget_live- try to increment a live percpu refcount
+ *percpu_ref_tryget_many_live- try to increment a live percpu refcount
* @ref:percpu_refto try-get
+ * @nr: number of references to get
You may want to change "increment" into "increase" to make it more clear that this function may increase the percpu refcount by more than one.
Anyway:
RFC PATCH 2/2 block: support to freeze bio based request queue
In reply to Ming LeiFor bio based request queue, the queue usage refcnt is only grabbed during submission, which isn't consistent with request base queue.
Queue freezing has been used widely, and turns out it is very useful to quiesce queue activity.
Support to freeze bio based request queue by the following approach:
1) grab two queue usage refcount for blk-mq before submitting blk-mq bio, one is for bio, anther is for request;
2) add bio flag of BIO_QUEUE_REFFED for making sure that only one refcnt is grabbed for each bio, so we can put the refcnt when the bio is going away
3) nvme mpath is a bit special, because same bio is used for both mpath queue and underlying nvme queue. So we put the mpath queue's usage refcnt before completing the nvme request.
--- block/bio.c | 12 ++++++++++-- block/blk-core.c | 23 +++++++++++++++++------ drivers/nvme/host/core.c | 16 ++++++++++++++++ drivers/nvme/host/multipath.c | 6 ++++++ include/linux/blk-mq.h | 2 ++ include/linux/blk_types.h | 1 + include/linux/blkdev.h | 7 ++++++- 7 files changed, 58 insertions(+), 9 deletions(-)
block/bio.c +10 -2
@@ -1365,14 +1365,18 @@ static inline bool bio_remaining_done(struct bio *bio)**/ void bio_endio (struct bio *bio) { +struct block_device *bdev; +bool put_queue; again: + bdev = bio->bi_bdev; + put_queue =bio_flagged (bio,BIO_QUEUE_REFFED );if (!bio_remaining_done (bio))return ;if (!bio_integrity_endio (bio))return ; -if (bio->bi_bdev) -rq_qos_done_bio (bio->bi_bdev->bd_disk->queue, bio); +if (bdev) +rq_qos_done_bio (bdev->bd_disk->queue, bio);/* * Need to have a real endio function for chained bios, otherwise
@@ -1384,6 +1388,8 @@ void bio_endio(struct bio *bio)*/ if (bio->bi_end_io == bio_chain_endio) { bio =__bio_chain_endio (bio); +if (bdev && put_queue) +blk_queue_exit (bdev->bd_disk->queue); goto again; }
@@ -1397,6 +1403,8 @@ void bio_endio(struct bio *bio)bio_uninit (bio);if (bio->bi_end_io) bio->bi_end_io (bio); +if (bdev && put_queue) +blk_queue_exit (bdev->bd_disk->queue); }EXPORT_SYMBOL (bio_endio);
block/blk-core.c +17 -6
@@ -431,12 +431,13 @@ EXPORT_SYMBOL(blk_cleanup_queue);int blk_queue_enter (struct request_queue *q,blk_mq_req_flags_t flags) {const bool pm = flags &BLK_MQ_REQ_PM ; +const unsigned int nr = (flags &BLK_MQ_REQ_DOUBLE_REF ) ?2 :1 ;while (true) {bool success = false;rcu_read_lock (); -if (percpu_ref_tryget_live (&q->q_usage_counter)) { +if (percpu_ref_tryget_many_live (&q->q_usage_counter, nr)) {/* * The code that increments the pm_only counter is * responsible for ensuring that that counter is
@@ -446,7 +447,7 @@ int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags) !blk_queue_pm_only(q)) { success = true; }else { -percpu_ref_put (&q->q_usage_counter); +percpu_ref_put_many (&q->q_usage_counter, nr); } }rcu_read_unlock ();
@@ -480,8 +481,18 @@ static inline int bio_queue_enter(struct bio *bio)struct request_queue *q = bio->bi_bdev->bd_disk->queue;bool nowait = bio->bi_opf &REQ_NOWAIT ;int ret; +blk_mq_req_flags_t flags = nowait ?BLK_MQ_REQ_NOWAIT :0 ; +bool reffed =bio_flagged (bio,BIO_QUEUE_REFFED ); - ret =blk_queue_enter (q, nowait ?BLK_MQ_REQ_NOWAIT :0 ); +if (!reffed) +bio_set_flag (bio,BIO_QUEUE_REFFED ); + +/* +* Grab two queue references for blk-mq, one is for bio, and +* another is for blk-mq request. +*/ + ret =blk_queue_enter (q, q->mq_ops && !reffed ? + (flags |BLK_MQ_REQ_DOUBLE_REF ) : flags);if (unlikely (ret)) {if (nowait && !blk_queue_dying (q))bio_wouldblock_error (bio);
@@ -492,10 +503,11 @@ static inline int bio_queue_enter(struct bio *bio)return ret; } -void blk_queue_exit (struct request_queue *q) +void __blk_queue_exit (struct request_queue *q,unsigned int nr) { -percpu_ref_put (&q->q_usage_counter); +percpu_ref_put_many (&q->q_usage_counter, nr); } +EXPORT_SYMBOL_GPL (__blk_queue_exit);static void blk_queue_usage_counter_release (struct percpu_ref *ref) {
@@ -920,7 +932,6 @@ static blk_qc_t __submit_bio(struct bio *bio)return blk_mq_submit_bio (bio); ret = disk->fops->submit_bio (bio); } -blk_queue_exit (disk->queue);return ret; }
drivers/nvme/host/core.c +16 -0
@@ -323,14 +323,30 @@ static inline enum nvme_disposition nvme_decide_disposition(struct request *req)static inline void nvme_end_req (struct request *req) {blk_status_t status =nvme_error_status (nvme_req (req)->status); +const bool mpath = req->cmd_flags &REQ_NVME_MPATH ; +unsigned int nr =0 ; +struct bio *bio; +struct nvme_ns *ns;if (IS_ENABLED (CONFIG_BLK_DEV_ZONED ) &&req_op (req) ==REQ_OP_ZONE_APPEND ) req->__sector =nvme_lba_to_sect (req->q->queuedata,le64_to_cpu (nvme_req (req)->result.u64)); +if (mpath) { + ns = req->q->queuedata; +__rq_for_each_bio (bio, req) + nr++; + }nvme_trace_bio_complete (req);blk_mq_end_request (req, status); + +/* +* We changed multipath bio->bi_bdev, so have to drop the queue +* reference manually +*/ +if (mpath && nr) +__blk_queue_exit (ns->head->disk->queue, nr); }void nvme_complete_rq (struct request *req)
drivers/nvme/host/multipath.c +6 -0
@@ -312,6 +312,12 @@ blk_qc_t nvme_ns_head_submit_bio(struct bio *bio) srcu_idx =srcu_read_lock (&head->srcu); ns =nvme_find_path (head);if (likely (ns)) { +/* +* this bio's ownership is transferred to underlying queue, so +* clear the queue reffed flag and let underlying queue to put +* the multipath queue for us. +*/ +bio_clear_flag (bio,BIO_QUEUE_REFFED );bio_set_dev (bio, ns->disk->part0); bio->bi_opf |=REQ_NVME_MPATH ; trace_block_bio_remap(bio, disk_devt(ns->head->disk),
include/linux/blk-mq.h +2 -0
@@ -445,6 +445,8 @@ enum {BLK_MQ_REQ_RESERVED = (__force blk_mq_req_flags_t)(1 <<1 ),/* set RQF_PM */ BLK_MQ_REQ_PM = (__force blk_mq_req_flags_t)(1 <<2 ), +/* double queue reference */ + BLK_MQ_REQ_DOUBLE_REF = (__force blk_mq_req_flags_t)(1 << 3), }; struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
include/linux/blk_types.h +1 -0
@@ -305,6 +305,7 @@ enum {BIO_CGROUP_ACCT ,/* has been accounted to a cgroup */ BIO_TRACKED ,/* set if bio goes through the rq_qos path */ BIO_REMAPPED , +BIO_QUEUE_REFFED ,/* need to put queue refcnt */ BIO_FLAG_LAST };
include/linux/blkdev.h +6 -1
@@ -925,7 +925,7 @@ extern int get_sg_io_hdr(struct sg_io_hdr *hdr, const void __user *argp);extern int put_sg_io_hdr (const struct sg_io_hdr *hdr,void __user *argp);extern int blk_queue_enter (struct request_queue *q,blk_mq_req_flags_t flags); -extern void blk_queue_exit (struct request_queue *q); +extern void __blk_queue_exit (struct request_queue *q,unsigned int nr);extern void blk_sync_queue (struct request_queue *q); extern int blk_rq_map_user(struct request_queue *, struct request *, struct rq_map_data *, void __user *, unsigned long,
@@ -947,6 +947,11 @@ blk_status_t errno_to_blk_status(int errno);int blk_poll (struct request_queue *q,blk_qc_t cookie,bool spin); +static inline void blk_queue_exit (struct request_queue *q) +{ +__blk_queue_exit (q,1 ); +} +static inline struct request_queue *bdev_get_queue (struct block_device *bdev) {return bdev->bd_disk->queue;/* this is never NULL */
Signature · 1 line
2.29.2
kernel test robot
build botRe: [RFC PATCH 2/2] block: support to freeze bio based request queue
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Ming,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on block/for-next]
[also build test ERROR on dennis-percpu/for-next linus/master v5.12-rc7 next-20210415] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Ming-Lei/block-support-to-freeze-bio-based-queue/20210415-183554 base: https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-next config: arm-randconfig-r034-20210415 (attached as .config) compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 6a18cc23efad410db48a3ccfc233d215de7d4cb9)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# https://github.com/0day-ci/linux/commit/efe3badef858ce63a834964692be0d07d82c155e
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Ming-Lei/block-support-to-freeze-bio-based-queue/20210415-183554 git checkout efe3badef858ce63a834964692be0d07d82c155e
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 ARCH=arm
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
>> drivers/nvme/host/core.c:349:30: error: no member named 'disk' in 'struct nvme_ns_head'
__blk_queue_exit(ns->head->disk->queue, nr);
~~~~~~~~ ^1 error generated.
vim +349 drivers/nvme/host/core.c
322
323 static inline void nvme_end_req(struct request *req)
324 {
325 blk_status_t status = nvme_error_status(nvme_req(req)->status);
326 const bool mpath = req->cmd_flags & REQ_NVME_MPATH;
327 unsigned int nr = 0;
328 struct bio *bio;
329 struct nvme_ns *ns;
330
331 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) &&
332 req_op(req) == REQ_OP_ZONE_APPEND)
333 req->__sector = nvme_lba_to_sect(req->q->queuedata,
334 le64_to_cpu(nvme_req(req)->result.u64));
335
336 if (mpath) {
337 ns = req->q->queuedata;
338 __rq_for_each_bio(bio, req)
339 nr++;
340 }
341 nvme_trace_bio_complete(req);
342 blk_mq_end_request(req, status);
343
344 /*
345 * We changed multipath bio->bi_bdev, so have to drop the queue
346 * reference manually
347 */
348 if (mpath && nr)
> 349 __blk_queue_exit(ns->head->disk->queue, nr);
350 }
351
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Re: [RFC PATCH 2/2] block: support to freeze bio based request queue
In reply to Ming LeiQuoted 11 lines · Ming Lei
For bio based request queue, the queue usage refcnt is only grabbed during submission, which isn't consistent with request base queue.
Queue freezing has been used widely, and turns out it is very useful to quiesce queue activity.
Support to freeze bio based request queue by the following approach:
1) grab two queue usage refcount for blk-mq before submitting blk-mq bio, one is for bio, anther is for request;
Quoted 7 lines
2) add bio flag of
BIO_QUEUE_REFFEDfor making sure that only one refcnt is grabbed for each bio, so we can put the refcnt when the bio is going away3) nvme mpath is a bit special, because same bio is used for both mpath queue and underlying nvme queue. So we put the mpath queue's usage refcnt before completing the nvme request.
RAID needs similar handling too, but it is easy to do, see md_end_io().
Signature · 1 line
Ming
kernel test robot
build botRe: [RFC PATCH 2/2] block: support to freeze bio based request queue
In reply to Ming Lei[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Ming,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on block/for-next]
[also build test ERROR on dennis-percpu/for-next linus/master v5.12-rc7 next-20210415] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Ming-Lei/block-support-to-freeze-bio-based-queue/20210415-183554 base: https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-next config: parisc-randconfig-r023-20210415 (attached as .config) compiler: hppa-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/efe3badef858ce63a834964692be0d07d82c155e
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Ming-Lei/block-support-to-freeze-bio-based-queue/20210415-183554 git checkout efe3badef858ce63a834964692be0d07d82c155e
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross W=1 ARCH=parisc
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
drivers/nvme/host/core.c: In function 'nvme_end_req':
>> drivers/nvme/host/core.c:349:28: error: 'struct nvme_ns_head' has no member named 'disk'
349 | __blk_queue_exit(ns->head->disk->queue, nr);
| ^~vim +349 drivers/nvme/host/core.c
322
323 static inline void nvme_end_req(struct request *req)
324 {
325 blk_status_t status = nvme_error_status(nvme_req(req)->status);
326 const bool mpath = req->cmd_flags & REQ_NVME_MPATH;
327 unsigned int nr = 0;
328 struct bio *bio;
329 struct nvme_ns *ns;
330
331 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) &&
332 req_op(req) == REQ_OP_ZONE_APPEND)
333 req->__sector = nvme_lba_to_sect(req->q->queuedata,
334 le64_to_cpu(nvme_req(req)->result.u64));
335
336 if (mpath) {
337 ns = req->q->queuedata;
338 __rq_for_each_bio(bio, req)
339 nr++;
340 }
341 nvme_trace_bio_complete(req);
342 blk_mq_end_request(req, status);
343
344 /*
345 * We changed multipath bio->bi_bdev, so have to drop the queue
346 * reference manually
347 */
348 if (mpath && nr)
> 349 __blk_queue_exit(ns->head->disk->queue, nr);
350 }
351
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Re: [dm-devel] [RFC PATCH 2/2] block: support to freeze bio based request queue
In reply to Ming LeiQuoted 2 lines · Ming Lei
1) grab two queue usage refcount for blk-mq before submitting blk-mq bio, one is for bio, anther is for request;
^^^^^^
another?block/blk-core.c EXPORT_SYMBOL(blk_cleanup_queue); · line 434
Quoted 9 lines
block/blk-core.c +1
-0@@ -431,12 +431,13 @@ EXPORT_SYMBOL(blk_cleanup_queue);int blk_queue_enter (struct request_queue *q,blk_mq_req_flags_t flags) {const bool pm = flags &BLK_MQ_REQ_PM ; +const unsigned int nr = (flags &BLK_MQ_REQ_DOUBLE_REF ) ?2 :1 ;
Please leave out the parentheses from around the condition in the above and in other ternary expressions. The ternary operator has a very low precedence so adding parentheses around the condition in a ternary operator is almost never necessary.
Quoted 7 lines
block/blk-core.c +2
-0@@ -480,8 +481,18 @@ static inline int bio_queue_enter(struct bio *bio)struct request_queue *q = bio->bi_bdev->bd_disk->queue;bool nowait = bio->bi_opf &REQ_NOWAIT ;int ret; +blk_mq_req_flags_t flags = nowait ?BLK_MQ_REQ_NOWAIT :0 ; +bool reffed =bio_flagged (bio,BIO_QUEUE_REFFED );
block/blk-core.c static inline int bio_queue_enter(struct bio *bio) · line 495
Quoted 10 lines
block/blk-core.c +9
-1- ret =blk_queue_enter (q, nowait ?BLK_MQ_REQ_NOWAIT :0 ); +if (!reffed) +bio_set_flag (bio,BIO_QUEUE_REFFED ); + +/* +* Grab two queue references for blk-mq, one is for bio, and +* another is for blk-mq request. +*/ + ret =blk_queue_enter (q, q->mq_ops && !reffed ? + (flags |BLK_MQ_REQ_DOUBLE_REF ) : flags);
Consider rewriting the above code as follows to make it easier to read:
if (q->mq_ops && !reffed) flags |= BLK_MQ_REQ_DOUBLE_REF; ret = blk_queue_enter(q, flags);
Please also expand the comment above this code. The comment only explains the reffed == false case but not the reffed == true case. I assume that the reffed == true case applies to stacked bio-based drivers?
Thanks,
Bart.
Re: [dm-devel] [RFC PATCH 2/2] block: support to freeze bio based request queue
Quoted 41 lines · Bart Van Assche
Quoted 2 lines · Ming Lei
1) grab two queue usage refcount for blk-mq before submitting blk-mq bio, one is for bio, anther is for request;
^^^^^^ another?block/blk-core.c EXPORT_SYMBOL(blk_cleanup_queue); · line 434
Quoted 9 lines
block/blk-core.c +1
-0@@ -431,12 +431,13 @@ EXPORT_SYMBOL(blk_cleanup_queue);int blk_queue_enter (struct request_queue *q,blk_mq_req_flags_t flags) {const bool pm = flags &BLK_MQ_REQ_PM ; +const unsigned int nr = (flags &BLK_MQ_REQ_DOUBLE_REF ) ?2 :1 ;Please leave out the parentheses from around the condition in the above and in other ternary expressions. The ternary operator has a very low precedence so adding parentheses around the condition in a ternary operator is almost never necessary.
Quoted 7 lines
block/blk-core.c +2
-0@@ -480,8 +481,18 @@ static inline int bio_queue_enter(struct bio *bio)struct request_queue *q = bio->bi_bdev->bd_disk->queue;bool nowait = bio->bi_opf &REQ_NOWAIT ;int ret; +blk_mq_req_flags_t flags = nowait ?BLK_MQ_REQ_NOWAIT :0 ; +bool reffed =bio_flagged (bio,BIO_QUEUE_REFFED );block/blk-core.c static inline int bio_queue_enter(struct bio *bio) · line 495
Quoted 10 lines
block/blk-core.c +9
-1- ret =blk_queue_enter (q, nowait ?BLK_MQ_REQ_NOWAIT :0 ); +if (!reffed) +bio_set_flag (bio,BIO_QUEUE_REFFED ); + +/* +* Grab two queue references for blk-mq, one is for bio, and +* another is for blk-mq request. +*/ + ret =blk_queue_enter (q, q->mq_ops && !reffed ? + (flags |BLK_MQ_REQ_DOUBLE_REF ) : flags);Consider rewriting the above code as follows to make it easier to read:
Quoted 7 lines
if (q->mq_ops && !reffed) flags |= BLK_MQ_REQ_DOUBLE_REF; ret = blk_queue_enter(q, flags);Please also expand the comment above this code. The comment only explains the reffed == false case but not the reffed == true case. I assume that the reffed == true case applies to stacked bio-based drivers?
'reffed == true' means we have got one queue usage count already for this bio, so only need to grab one usage count for blk-mq request.
Thanks,
Ming
Re: [dm-devel] [RFC PATCH 2/2] block: support to freeze bio based request queue
In reply to Ming LeiQuoted 10 lines · Ming Lei
For bio based request queue, the queue usage refcnt is only grabbed during submission, which isn't consistent with request base queue.
Queue freezing has been used widely, and turns out it is very useful to quiesce queue activity.
Support to freeze bio based request queue by the following approach:
1) grab two queue usage refcount for blk-mq before submitting blk-mq bio, one is for bio, anther is for request;
Hi, I can't understand the sense of grabbing two refcounts on the @q_usage_count of the underlying blk-mq device, while @q_usage_count of the MD/DM device is kept untouched.
In the following calling stack
``` queue_poll_store blk_mq_freeze_queue(q) ```
Is the input @q still the request queue of MD/DM device?
Quoted 238 lines
2) add bio flag of
BIO_QUEUE_REFFEDfor making sure that only one refcnt is grabbed for each bio, so we can put the refcnt when the bio is going away3) nvme mpath is a bit special, because same bio is used for both mpath queue and underlying nvme queue. So we put the mpath queue's usage refcnt before completing the nvme request.
Cc Christoph Hellwig <[email protected]>Cc Bart Van Assche <[email protected]>Signed-off-by Ming Lei <[email protected]>--- block/bio.c | 12 ++++++++++-- block/blk-core.c | 23 +++++++++++++++++------ drivers/nvme/host/core.c | 16 ++++++++++++++++ drivers/nvme/host/multipath.c | 6 ++++++ include/linux/blk-mq.h | 2 ++ include/linux/blk_types.h | 1 + include/linux/blkdev.h | 7 ++++++- 7 files changed, 58 insertions(+), 9 deletions(-)block/bio.c +10
-2@@ -1365,14 +1365,18 @@ static inline bool bio_remaining_done(struct bio *bio)**/ void bio_endio (struct bio *bio) { +struct block_device *bdev; +bool put_queue; again: + bdev = bio->bi_bdev; + put_queue =bio_flagged (bio,BIO_QUEUE_REFFED );if (!bio_remaining_done (bio))return ;if (!bio_integrity_endio (bio))return ; -if (bio->bi_bdev) -rq_qos_done_bio (bio->bi_bdev->bd_disk->queue, bio); +if (bdev) +rq_qos_done_bio (bdev->bd_disk->queue, bio);/* * Need to have a real endio function for chained bios, otherwise @@ -1384,6 +1388,8 @@ void bio_endio(struct bio *bio)*/ if (bio->bi_end_io == bio_chain_endio) { bio =__bio_chain_endio (bio); +if (bdev && put_queue) +blk_queue_exit (bdev->bd_disk->queue); goto again; }@@ -1397,6 +1403,8 @@ void bio_endio(struct bio *bio)bio_uninit (bio);if (bio->bi_end_io) bio->bi_end_io (bio); +if (bdev && put_queue) +blk_queue_exit (bdev->bd_disk->queue); }EXPORT_SYMBOL (bio_endio);block/blk-core.c +17
-6@@ -431,12 +431,13 @@ EXPORT_SYMBOL(blk_cleanup_queue);int blk_queue_enter (struct request_queue *q,blk_mq_req_flags_t flags) {const bool pm = flags &BLK_MQ_REQ_PM ; +const unsigned int nr = (flags &BLK_MQ_REQ_DOUBLE_REF ) ?2 :1 ;while (true) {bool success = false;rcu_read_lock (); -if (percpu_ref_tryget_live (&q->q_usage_counter)) { +if (percpu_ref_tryget_many_live (&q->q_usage_counter, nr)) {/* * The code that increments the pm_only counter is * responsible for ensuring that that counter is @@ -446,7 +447,7 @@ int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags) !blk_queue_pm_only(q)) { success = true; }else { -percpu_ref_put (&q->q_usage_counter); +percpu_ref_put_many (&q->q_usage_counter, nr); } }rcu_read_unlock ();@@ -480,8 +481,18 @@ static inline int bio_queue_enter(struct bio *bio)struct request_queue *q = bio->bi_bdev->bd_disk->queue;bool nowait = bio->bi_opf &REQ_NOWAIT ;int ret; +blk_mq_req_flags_t flags = nowait ?BLK_MQ_REQ_NOWAIT :0 ; +bool reffed =bio_flagged (bio,BIO_QUEUE_REFFED ); - ret =blk_queue_enter (q, nowait ?BLK_MQ_REQ_NOWAIT :0 ); +if (!reffed) +bio_set_flag (bio,BIO_QUEUE_REFFED ); + +/* +* Grab two queue references for blk-mq, one is for bio, and +* another is for blk-mq request. +*/ + ret =blk_queue_enter (q, q->mq_ops && !reffed ? + (flags |BLK_MQ_REQ_DOUBLE_REF ) : flags);if (unlikely (ret)) {if (nowait && !blk_queue_dying (q))bio_wouldblock_error (bio);@@ -492,10 +503,11 @@ static inline int bio_queue_enter(struct bio *bio)return ret; } -void blk_queue_exit (struct request_queue *q) +void __blk_queue_exit (struct request_queue *q,unsigned int nr) { -percpu_ref_put (&q->q_usage_counter); +percpu_ref_put_many (&q->q_usage_counter, nr); } +EXPORT_SYMBOL_GPL (__blk_queue_exit);static void blk_queue_usage_counter_release (struct percpu_ref *ref) {@@ -920,7 +932,6 @@ static blk_qc_t __submit_bio(struct bio *bio)return blk_mq_submit_bio (bio); ret = disk->fops->submit_bio (bio); } -blk_queue_exit (disk->queue);return ret; }drivers/nvme/host/core.c +16
-0@@ -323,14 +323,30 @@ static inline enum nvme_disposition nvme_decide_disposition(struct request *req)static inline void nvme_end_req (struct request *req) {blk_status_t status =nvme_error_status (nvme_req (req)->status); +const bool mpath = req->cmd_flags &REQ_NVME_MPATH ; +unsigned int nr =0 ; +struct bio *bio; +struct nvme_ns *ns;if (IS_ENABLED (CONFIG_BLK_DEV_ZONED ) &&req_op (req) ==REQ_OP_ZONE_APPEND ) req->__sector =nvme_lba_to_sect (req->q->queuedata,le64_to_cpu (nvme_req (req)->result.u64)); +if (mpath) { + ns = req->q->queuedata; +__rq_for_each_bio (bio, req) + nr++; + }nvme_trace_bio_complete (req);blk_mq_end_request (req, status); + +/* +* We changed multipath bio->bi_bdev, so have to drop the queue +* reference manually +*/ +if (mpath && nr) +__blk_queue_exit (ns->head->disk->queue, nr); }void nvme_complete_rq (struct request *req)drivers/nvme/host/multipath.c +6
-0@@ -312,6 +312,12 @@ blk_qc_t nvme_ns_head_submit_bio(struct bio *bio) srcu_idx =srcu_read_lock (&head->srcu); ns =nvme_find_path (head);if (likely (ns)) { +/* +* this bio's ownership is transferred to underlying queue, so +* clear the queue reffed flag and let underlying queue to put +* the multipath queue for us. +*/ +bio_clear_flag (bio,BIO_QUEUE_REFFED );bio_set_dev (bio, ns->disk->part0); bio->bi_opf |=REQ_NVME_MPATH ; trace_block_bio_remap(bio, disk_devt(ns->head->disk),include/linux/blk-mq.h +2
-0@@ -445,6 +445,8 @@ enum {BLK_MQ_REQ_RESERVED = (__force blk_mq_req_flags_t)(1 <<1 ),/* set RQF_PM */ BLK_MQ_REQ_PM = (__force blk_mq_req_flags_t)(1 <<2 ), +/* double queue reference */ + BLK_MQ_REQ_DOUBLE_REF = (__force blk_mq_req_flags_t)(1 << 3), }; struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,include/linux/blk_types.h +1
-0@@ -305,6 +305,7 @@ enum {BIO_CGROUP_ACCT ,/* has been accounted to a cgroup */ BIO_TRACKED ,/* set if bio goes through the rq_qos path */ BIO_REMAPPED , +BIO_QUEUE_REFFED ,/* need to put queue refcnt */ BIO_FLAG_LAST };include/linux/blkdev.h +6
-1@@ -925,7 +925,7 @@ extern int get_sg_io_hdr(struct sg_io_hdr *hdr, const void __user *argp);extern int put_sg_io_hdr (const struct sg_io_hdr *hdr,void __user *argp);extern int blk_queue_enter (struct request_queue *q,blk_mq_req_flags_t flags); -extern void blk_queue_exit (struct request_queue *q); +extern void __blk_queue_exit (struct request_queue *q,unsigned int nr);extern void blk_sync_queue (struct request_queue *q); extern int blk_rq_map_user(struct request_queue *, struct request *, struct rq_map_data *, void __user *, unsigned long,@@ -947,6 +947,11 @@ blk_status_t errno_to_blk_status(int errno);int blk_poll (struct request_queue *q,blk_qc_t cookie,bool spin); +static inline void blk_queue_exit (struct request_queue *q) +{ +__blk_queue_exit (q,1 ); +} +static inline struct request_queue *bdev_get_queue (struct block_device *bdev) {return bdev->bd_disk->queue;/* this is never NULL */
Signature · 2 lines
Thanks, Jeffle
Re: [dm-devel] [RFC PATCH 2/2] block: support to freeze bio based request queue
Quoted 11 lines · JeffleXu
Quoted 8 lines · Ming Lei
For bio based request queue, the queue usage refcnt is only grabbed during submission, which isn't consistent with request base queue.
Queue freezing has been used widely, and turns out it is very useful to quiesce queue activity.
Support to freeze bio based request queue by the following approach:
Quoted 7 lines
Quoted 2 lines
1) grab two queue usage refcount for blk-mq before submitting blk-mq bio, one is for bio, anther is for request;
Hi, I can't understand the sense of grabbing two refcounts on the @q_usage_count of the underlying blk-mq device, while @q_usage_count of the MD/DM device is kept untouched.
Follows the point:
1) for blk-mq, we hold one refcount for bio and another for request, and release one after ending bio or completing request.
2) for bio based queue, just holding one refcount for bio, and release it after the bio is ended.
As I mentioned to you, the current in-tree code only grabs the refcount during submitting bio for bio base queue, and the refcount is released after returning from submission, see __submit_bio().
Quoted 9 lines
In the following calling stack
``` queue_poll_store blk_mq_freeze_queue(q) ```Is the input @q still the request queue of MD/DM device?
It can be either one after bio based io polling is supported, queue/io_poll is exposed for both blk-mq and bio based queue.
However, I guess bio based polling doesn't need such strict bio queue freezing, cause QUEUE_FLAG_POLL is only read in submission path, so looks current freezing just during submission is enough.
Thanks,
Ming
Re: [dm-devel] [RFC PATCH 2/2] block: support to freeze bio based request queue
Quoted 16 lines · Ming Lei
Quoted 15 lines · JeffleXu
Quoted 10 lines · Ming Lei
For bio based request queue, the queue usage refcnt is only grabbed during submission, which isn't consistent with request base queue.
Queue freezing has been used widely, and turns out it is very useful to quiesce queue activity.
Support to freeze bio based request queue by the following approach:
1) grab two queue usage refcount for blk-mq before submitting blk-mq bio, one is for bio, anther is for request;
Quoted 8 lines
Quoted 3 lines
Hi, I can't understand the sense of grabbing two refcounts on the @q_usage_count of the underlying blk-mq device, while @q_usage_count of the MD/DM device is kept untouched.
Follows the point:
1) for blk-mq, we hold one refcount for bio and another for request, and release one after ending bio or completing request.
Blk-mq has already implemented queue freezing semantics, even without this 'grabbing two refcount'. So is this just for the code consisdency with the bio-based queue?
Quoted 3 lines
2) for bio based queue, just holding one refcount for bio, and release it after the bio is ended.
OK.
Quoted 4 lines
As I mentioned to you, the current in-tree code only grabs the refcount during submitting bio for bio base queue, and the refcount is released after returning from submission, see
__submit_bio().
Yes. I ignored that the refcount grabbed in the entry of bio submission has been returned back when the submission completes for bio-based queue.
Quoted 9 lines
Quoted 9 lines
In the following calling stack
``` queue_poll_store blk_mq_freeze_queue(q) ```
Quoted 8 lines
Quoted 1 line
Is the input @q still the request queue of MD/DM device?
It can be either one after bio based io polling is supported, queue/io_poll is exposed for both blk-mq and bio based queue.
However, I guess bio based polling doesn't need such strict bio queue freezing, cause
QUEUE_FLAG_POLLis only read in submission path, so looks current freezing just during submission is enough.
Not actually.
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) || !blk_queue_poll(q))
+ if (!blk_queue_poll(q) || (queue_is_mq(q) && !blk_qc_t_valid(cookie)))
Here QUEUE_FLAG_POLL is still checked in blk_poll() for bio-based queue, at least in your latest patch for bio-based polling.
Signature · 2 lines
Thanks, Jeffle
Re: [dm-devel] [RFC PATCH 2/2] block: support to freeze bio based request queue
Quoted 23 lines · JeffleXu
Quoted 20 lines · Ming Lei
Quoted 18 lines · JeffleXu
Quoted 10 lines · Ming Lei
For bio based request queue, the queue usage refcnt is only grabbed during submission, which isn't consistent with request base queue.
Queue freezing has been used widely, and turns out it is very useful to quiesce queue activity.
Support to freeze bio based request queue by the following approach:
1) grab two queue usage refcount for blk-mq before submitting blk-mq bio, one is for bio, anther is for request;
Hi, I can't understand the sense of grabbing two refcounts on the @q_usage_count of the underlying blk-mq device, while @q_usage_count of the MD/DM device is kept untouched.
Quoted 8 lines
Quoted 4 lines
Follows the point:
1) for blk-mq, we hold one refcount for bio and another for request, and release one after ending bio or completing request.
Blk-mq has already implemented queue freezing semantics, even without this 'grabbing two refcount'. So is this just for the code consisdency with the bio-based queue?
Right.
Quoted 43 lines
Quoted 5 lines
2) for bio based queue, just holding one refcount for bio, and release it after the bio is ended.
OK.
Quoted 4 lines
As I mentioned to you, the current in-tree code only grabs the refcount during submitting bio for bio base queue, and the refcount is released after returning from submission, see
__submit_bio().Yes. I ignored that the refcount grabbed in the entry of bio submission has been returned back when the submission completes for bio-based queue.
Quoted 9 lines
Quoted 9 lines
In the following calling stack
``` queue_poll_store blk_mq_freeze_queue(q) ```Quoted 8 lines
Quoted 1 line
Is the input @q still the request queue of MD/DM device?
It can be either one after bio based io polling is supported, queue/io_poll is exposed for both blk-mq and bio based queue.
However, I guess bio based polling doesn't need such strict bio queue freezing, cause
QUEUE_FLAG_POLLis only read in submission path, so looks current freezing just during submission is enough.Not actually.
blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin)(quoted diff) +1
-1struct blk_mq_hw_ctx *hctx; long state; - if (!blk_qc_t_valid(cookie) || !blk_queue_poll(q)) + if (!blk_queue_poll(q) || (queue_is_mq(q) && !blk_qc_t_valid(cookie)))
Quoted 2 lines
Here
QUEUE_FLAG_POLLis still checked inblk_poll()for bio-based queue, at least in your latest patch for bio-based polling.
OK, we can simply drop it.
Thanks,
Ming