Skip to content

Commit 893d290

Browse files
rolanddaxboe
authored andcommitted
block: Don't access request after it might be freed
After we've done __elv_add_request() and __blk_run_queue() in blk_execute_rq_nowait(), the request might finish and be freed immediately. Therefore checking if the type is REQ_TYPE_PM_RESUME isn't safe afterwards, because if it isn't, rq might be gone. Instead, check beforehand and stash the result in a temporary. This fixes crashes in blk_execute_rq_nowait() I get occasionally when running with lots of memory debugging options enabled -- I think this race is usually harmless because the window for rq to be reallocated is so small. Signed-off-by: Roland Dreier <roland@purestorage.com> Cc: stable@kernel.org Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 836413e commit 893d290

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

‎block/blk-exec.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,17 @@ void blk_execute_rq_nowait(struct request_queue *q, struct gendisk *bd_disk,
5252
rq_end_io_fn *done)
5353
{
5454
int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
55+
bool is_pm_resume;
5556

5657
WARN_ON(irqs_disabled());
5758

5859
rq->rq_disk = bd_disk;
5960
rq->end_io = done;
61+
/*
62+
* need to check this before __blk_run_queue(), because rq can
63+
* be freed before that returns.
64+
*/
65+
is_pm_resume = rq->cmd_type == REQ_TYPE_PM_RESUME;
6066

6167
spin_lock_irq(q->queue_lock);
6268

@@ -71,7 +77,7 @@ void blk_execute_rq_nowait(struct request_queue *q, struct gendisk *bd_disk,
7177
__elv_add_request(q, rq, where);
7278
__blk_run_queue(q);
7379
/* the queue is stopped so it won't be run */
74-
if (rq->cmd_type == REQ_TYPE_PM_RESUME)
80+
if (is_pm_resume)
7581
q->request_fn(q);
7682
spin_unlock_irq(q->queue_lock);
7783
}

0 commit comments

Comments
 (0)