Skip to content

Commit 8d69966

Browse files
Yufen Yuaxboe
authored andcommitted
block: fix null pointer dereference in blk_mq_rq_timed_out()
We got a null pointer deference BUG_ON in blk_mq_rq_timed_out() as following: [ 108.825472] BUG: kernel NULL pointer dereference, address: 0000000000000040 [ 108.827059] PGD 0 P4D 0 [ 108.827313] Oops: 0000 [#1] SMP PTI [ 108.827657] CPU: 6 PID: 198 Comm: kworker/6:1H Not tainted 5.3.0-rc8+ #431 [ 108.829503] Workqueue: kblockd blk_mq_timeout_work [ 108.829913] RIP: 0010:blk_mq_check_expired+0x258/0x330 [ 108.838191] Call Trace: [ 108.838406] bt_iter+0x74/0x80 [ 108.838665] blk_mq_queue_tag_busy_iter+0x204/0x450 [ 108.839074] ? __switch_to_asm+0x34/0x70 [ 108.839405] ? blk_mq_stop_hw_queue+0x40/0x40 [ 108.839823] ? blk_mq_stop_hw_queue+0x40/0x40 [ 108.840273] ? syscall_return_via_sysret+0xf/0x7f [ 108.840732] blk_mq_timeout_work+0x74/0x200 [ 108.841151] process_one_work+0x297/0x680 [ 108.841550] worker_thread+0x29c/0x6f0 [ 108.841926] ? rescuer_thread+0x580/0x580 [ 108.842344] kthread+0x16a/0x1a0 [ 108.842666] ? kthread_flush_work+0x170/0x170 [ 108.843100] ret_from_fork+0x35/0x40 The bug is caused by the race between timeout handle and completion for flush request. When timeout handle function blk_mq_rq_timed_out() try to read 'req->q->mq_ops', the 'req' have completed and reinitiated by next flush request, which would call blk_rq_init() to clear 'req' as 0. After commit 12f5b93 ("blk-mq: Remove generation seqeunce"), normal requests lifetime are protected by refcount. Until 'rq->ref' drop to zero, the request can really be free. Thus, these requests cannot been reused before timeout handle finish. However, flush request has defined .end_io and rq->end_io() is still called even if 'rq->ref' doesn't drop to zero. After that, the 'flush_rq' can be reused by the next flush request handle, resulting in null pointer deference BUG ON. We fix this problem by covering flush request with 'rq->ref'. If the refcount is not zero, flush_end_io() return and wait the last holder recall it. To record the request status, we add a new entry 'rq_status', which will be used in flush_end_io(). Cc: Christoph Hellwig <[email protected]> Cc: Keith Busch <[email protected]> Cc: Bart Van Assche <[email protected]> Cc: [email protected] # v4.18+ Reviewed-by: Ming Lei <[email protected]> Reviewed-by: Bob Liu <[email protected]> Signed-off-by: Yufen Yu <[email protected]> ------- v2: - move rq_status from struct request to struct blk_flush_queue v3: - remove unnecessary '{}' pair. v4: - let spinlock to protect 'fq->rq_status' v5: - move rq_status after flush_running_idx member of struct blk_flush_queue Signed-off-by: Jens Axboe <[email protected]>
1 parent 2af2783 commit 8d69966

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

block/blk-flush.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,16 @@ static void flush_end_io(struct request *flush_rq, blk_status_t error)
214214

215215
/* release the tag's ownership to the req cloned from */
216216
spin_lock_irqsave(&fq->mq_flush_lock, flags);
217+
218+
if (!refcount_dec_and_test(&flush_rq->ref)) {
219+
fq->rq_status = error;
220+
spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
221+
return;
222+
}
223+
224+
if (fq->rq_status != BLK_STS_OK)
225+
error = fq->rq_status;
226+
217227
hctx = flush_rq->mq_hctx;
218228
if (!q->elevator) {
219229
blk_mq_tag_set_rq(hctx, flush_rq->tag, fq->orig_rq);

block/blk-mq.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,10 @@ static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
918918
*/
919919
if (blk_mq_req_expired(rq, next))
920920
blk_mq_rq_timed_out(rq, reserved);
921-
if (refcount_dec_and_test(&rq->ref))
921+
922+
if (is_flush_rq(rq, hctx))
923+
rq->end_io(rq, 0);
924+
else if (refcount_dec_and_test(&rq->ref))
922925
__blk_mq_free_request(rq);
923926

924927
return true;

block/blk.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct blk_flush_queue {
1919
unsigned int flush_queue_delayed:1;
2020
unsigned int flush_pending_idx:1;
2121
unsigned int flush_running_idx:1;
22+
blk_status_t rq_status;
2223
unsigned long flush_pending_since;
2324
struct list_head flush_queue[2];
2425
struct list_head flush_data_in_flight;
@@ -47,6 +48,12 @@ static inline void __blk_get_queue(struct request_queue *q)
4748
kobject_get(&q->kobj);
4849
}
4950

51+
static inline bool
52+
is_flush_rq(struct request *req, struct blk_mq_hw_ctx *hctx)
53+
{
54+
return hctx->fq->flush_rq == req;
55+
}
56+
5057
struct blk_flush_queue *blk_alloc_flush_queue(struct request_queue *q,
5158
int node, int cmd_size, gfp_t flags);
5259
void blk_free_flush_queue(struct blk_flush_queue *q);

0 commit comments

Comments
 (0)