Skip to contentSkip to conversation

blk-mq: use BLK_POLL_ONESHOT for synchronous poll completion

PATCH Applied Ming Lei 3 participants 3 messages Jan 22 Block layer

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 blk-mq: use BLK_POLL_ONESHOT for synchronous poll completion

blk_execute_rq() with polling is used in kernel code paths such as NVMe controller connect. The aggressive spinning in blk_hctx_poll() can prevent the completion task from getting a chance to run, causing a lockup.

The spinning with cpu_relax() doesn't yield CPU, so need_resched() only becomes true on timer tick. This causes unnecessary spinning while the completion task is already waiting to run.

Before commit f22ecf9c14c1, the loop would exit early because task_is_running() was always true. After that commit removed the check, the loop now spins until need_resched().

Fix this by using BLK_POLL_ONESHOT in blk_rq_poll_completion(). This causes blk_hctx_poll() to poll once and return immediately, letting the outer loop's cond_resched() yield CPU so the completion task can run.

Fixes f22ecf9c14c1 ("blk-mq: delete task running check in blk_hctx_poll()")
Cc Diangang Li <[email protected]>
Cc Fengnan Chang <[email protected]>
---
 block/blk-mq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-mq.c +1 -1
index a29d8ac9d3e3..968699277c3d 100644
@@ -1480,7 +1480,7 @@ EXPORT_SYMBOL_GPL(blk_rq_is_poll);
 static void blk_rq_poll_completion(struct request *rq, struct completion *wait)
 {
 	do {
-		blk_hctx_poll(rq->q, rq->mq_hctx, NULL, 0);
+		blk_hctx_poll(rq->q, rq->mq_hctx, NULL, BLK_POLL_ONESHOT);
 		cond_resched();
 	} while (!completion_done(wait));
 }
Signature · 1 line
2.47.1
Tested-by on v1 1/1
#1 lore raw
Quoted 19 lines · Ming Lei

blk_execute_rq() with polling is used in kernel code paths such as NVMe controller connect. The aggressive spinning in blk_hctx_poll() can prevent the completion task from getting a chance to run, causing a lockup.

The spinning with cpu_relax() doesn't yield CPU, so need_resched() only becomes true on timer tick. This causes unnecessary spinning while the completion task is already waiting to run.

Before commit f22ecf9c14c1, the loop would exit early because task_is_running() was always true. After that commit removed the check, the loop now spins until need_resched().

Fix this by using BLK_POLL_ONESHOT in blk_rq_poll_completion(). This causes blk_hctx_poll() to poll once and return immediately, letting the outer loop's cond_resched() yield CPU so the completion task can run.

Quoted 9 lines
Fixes f22ecf9c14c1 ("blk-mq: delete task running check in blk_hctx_poll()")
Cc Diangang Li <[email protected]>
Cc Fengnan Chang <[email protected]>
---
 block/blk-mq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Verified the issue was fixed by this patch:

Quoted 16 lines
block/blk-mq.c +1 -2
index a29d8ac9d3e3..968699277c3d 100644
@@ -1480,7 +1480,7 @@ EXPORT_SYMBOL_GPL(blk_rq_is_poll);
 static void blk_rq_poll_completion(struct request *rq, struct completion *wait)
 {
        do {
-               blk_hctx_poll(rq->q, rq->mq_hctx, NULL, 0);
+               blk_hctx_poll(rq->q, rq->mq_hctx, NULL, BLK_POLL_ONESHOT);
                cond_resched();
        } while (!completion_done(wait));
 }
--

2.47.1

Signature · 2 lines
Best Regards,
  Yi Zhang
maintainer Applied
#2 lore raw
In reply to Ming Lei
Quoted 10 lines · Ming Lei

blk_execute_rq() with polling is used in kernel code paths such as NVMe controller connect. The aggressive spinning in blk_hctx_poll() can prevent the completion task from getting a chance to run, causing a lockup.

The spinning with cpu_relax() doesn't yield CPU, so need_resched() only becomes true on timer tick. This causes unnecessary spinning while the completion task is already waiting to run.

[...]

Applied, thanks!

[1/1] blk-mq: use BLK_POLL_ONESHOT for synchronous poll completion

      commit: 046be7e5967ef80547f7fd8a399e932f5338d5d4

Best regards,

Signature · 1 line
Jens Axboe