Skip to content

Commit

Permalink
blk-mq: avoid double ->queue_rq() because of early timeout
Browse files Browse the repository at this point in the history
[ Upstream commit 82c2294 ]

David Jeffery found one double ->queue_rq() issue, so far it can
be triggered in VM use case because of long vmexit latency or preempt
latency of vCPU pthread or long page fault in vCPU pthread, then block
IO req could be timed out before queuing the request to hardware but after
calling blk_mq_start_request() during ->queue_rq(), then timeout handler
may handle it by requeue, then double ->queue_rq() is caused, and kernel
panic.

So far, it is driver's responsibility to cover the race between timeout
and completion, so it seems supposed to be solved in driver in theory,
given driver has enough knowledge.

But it is really one common problem, lots of driver could have similar
issue, and could be hard to fix all affected drivers, even it isn't easy
for driver to handle the race. So David suggests this patch by draining
in-progress ->queue_rq() for solving this issue.

Cc: Stefan Hajnoczi <[email protected]>
Cc: Keith Busch <[email protected]>
Cc: [email protected]
Cc: Bart Van Assche <[email protected]>
Signed-off-by: David Jeffery <[email protected]>
Signed-off-by: Ming Lei <[email protected]>
Reviewed-by: Bart Van Assche <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Jens Axboe <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
  • Loading branch information
David Jeffery authored and gregkh committed Dec 28, 2022
1 parent 52931c3 commit 91d825b
Showing 1 changed file with 44 additions and 12 deletions.
56 changes: 44 additions & 12 deletions block/blk-mq.c
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,13 @@ static void blk_mq_rq_timed_out(struct request *req)
blk_add_timer(req);
}

static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
struct blk_expired_data {
bool has_timedout_rq;
unsigned long next;
unsigned long timeout_start;
};

static bool blk_mq_req_expired(struct request *rq, struct blk_expired_data *expired)
{
unsigned long deadline;

Expand All @@ -1539,13 +1545,13 @@ static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
return false;

deadline = READ_ONCE(rq->deadline);
if (time_after_eq(jiffies, deadline))
if (time_after_eq(expired->timeout_start, deadline))
return true;

if (*next == 0)
*next = deadline;
else if (time_after(*next, deadline))
*next = deadline;
if (expired->next == 0)
expired->next = deadline;
else if (time_after(expired->next, deadline))
expired->next = deadline;
return false;
}

Expand All @@ -1561,7 +1567,7 @@ void blk_mq_put_rq_ref(struct request *rq)

static bool blk_mq_check_expired(struct request *rq, void *priv)
{
unsigned long *next = priv;
struct blk_expired_data *expired = priv;

/*
* blk_mq_queue_tag_busy_iter() has locked the request, so it cannot
Expand All @@ -1570,7 +1576,18 @@ static bool blk_mq_check_expired(struct request *rq, void *priv)
* it was completed and reallocated as a new request after returning
* from blk_mq_check_expired().
*/
if (blk_mq_req_expired(rq, next))
if (blk_mq_req_expired(rq, expired)) {
expired->has_timedout_rq = true;
return false;
}
return true;
}

static bool blk_mq_handle_expired(struct request *rq, void *priv)
{
struct blk_expired_data *expired = priv;

if (blk_mq_req_expired(rq, expired))
blk_mq_rq_timed_out(rq);
return true;
}
Expand All @@ -1579,7 +1596,9 @@ static void blk_mq_timeout_work(struct work_struct *work)
{
struct request_queue *q =
container_of(work, struct request_queue, timeout_work);
unsigned long next = 0;
struct blk_expired_data expired = {
.timeout_start = jiffies,
};
struct blk_mq_hw_ctx *hctx;
unsigned long i;

Expand All @@ -1599,10 +1618,23 @@ static void blk_mq_timeout_work(struct work_struct *work)
if (!percpu_ref_tryget(&q->q_usage_counter))
return;

blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
/* check if there is any timed-out request */
blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &expired);
if (expired.has_timedout_rq) {
/*
* Before walking tags, we must ensure any submit started
* before the current time has finished. Since the submit
* uses srcu or rcu, wait for a synchronization point to
* ensure all running submits have finished
*/
blk_mq_wait_quiesce_done(q);

expired.next = 0;
blk_mq_queue_tag_busy_iter(q, blk_mq_handle_expired, &expired);
}

if (next != 0) {
mod_timer(&q->timeout, next);
if (expired.next != 0) {
mod_timer(&q->timeout, expired.next);
} else {
/*
* Request timeouts are handled as a forward rolling timer. If
Expand Down

0 comments on commit 91d825b

Please sign in to comment.