* [PATCH] blk-mq: fix kernel panic during iterating over flush request
@ 2021-08-11 14:26 Ming Lei
2021-08-12 8:57 ` Christoph Hellwig
` (4 more replies)
0 siblings, 5 replies; 8+ messages in thread
From: Ming Lei @ 2021-08-11 14:26 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, Bart Van Assche, Christoph Hellwig, John Garry,
Ming Lei, Blank-Burian, Markus, Dr.
For fixing use-after-free during iterating over requests, we grabbed
request's refcount before calling ->fn in commit 2e315dc07df0 ("blk-mq:
grab rq->refcount before calling ->fn in blk_mq_tagset_busy_iter").
Turns out this way may cause kernel panic when iterating over one flush
request:
1) old flush request's tag is just released, and this tag is reused by
one new request, but ->rqs[] isn't updated yet
2) the flush request can be re-used for submitting one new flush command,
so blk_rq_init() is called at the same time
3) meantime blk_mq_queue_tag_busy_iter() is called, and old flush request
is retrieved from ->rqs[tag]; when blk_mq_put_rq_ref() is called,
flush_rq->end_io may not be updated yet, so NULL pointer dereference
is triggered in blk_mq_put_rq_ref().
Fix the issue by calling refcount_set(&flush_rq->ref, 1) after
flush_rq->end_io is set. So far the only other caller of blk_rq_init() is
scsi_ioctl_reset() in which the request doesn't enter block IO stack and
the request reference count isn't used, so the change is safe.
Fixes: 2e315dc07df0 ("blk-mq: grab rq->refcount before calling ->fn in
blk_mq_tagset_busy_iter")
Reported-by: "Blank-Burian, Markus, Dr." <blankburian@uni-muenster.de>
Tested-by: "Blank-Burian, Markus, Dr." <blankburian@uni-muenster.de>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
block/blk-core.c | 1 -
block/blk-flush.c | 8 ++++++++
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/block/blk-core.c b/block/blk-core.c
index 0874bc2fcdb4..b5098739f72a 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -121,7 +121,6 @@ void blk_rq_init(struct request_queue *q, struct request *rq)
rq->internal_tag = BLK_MQ_NO_TAG;
rq->start_time_ns = ktime_get_ns();
rq->part = NULL;
- refcount_set(&rq->ref, 1);
blk_crypto_rq_set_defaults(rq);
}
EXPORT_SYMBOL(blk_rq_init);
diff --git a/block/blk-flush.c b/block/blk-flush.c
index 1002f6c58181..4912c8dbb1d8 100644
--- a/block/blk-flush.c
+++ b/block/blk-flush.c
@@ -329,6 +329,14 @@ static void blk_kick_flush(struct request_queue *q, struct blk_flush_queue *fq,
flush_rq->rq_flags |= RQF_FLUSH_SEQ;
flush_rq->rq_disk = first_rq->rq_disk;
flush_rq->end_io = flush_end_io;
+ /*
+ * Order WRITE ->end_io and WRITE rq->ref, and its pair is the one
+ * implied in refcount_inc_not_zero() called from
+ * blk_mq_find_and_get_req(), which orders WRITE/READ flush_rq->ref
+ * and READ flush_rq->end_io
+ */
+ smp_wmb();
+ refcount_set(&flush_rq->ref, 1);
blk_flush_queue_rq(flush_rq, false);
}
--
2.31.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH] blk-mq: fix kernel panic during iterating over flush request
2021-08-11 14:26 [PATCH] blk-mq: fix kernel panic during iterating over flush request Ming Lei
@ 2021-08-12 8:57 ` Christoph Hellwig
2021-08-12 10:20 ` John Garry
` (3 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Christoph Hellwig @ 2021-08-12 8:57 UTC (permalink / raw)
To: Ming Lei
Cc: Jens Axboe, linux-block, Bart Van Assche, Christoph Hellwig,
John Garry, Blank-Burian, Markus, Dr.
Looks good,
Reviewed-by: Christoph Hellwig <hch@lst.de>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] blk-mq: fix kernel panic during iterating over flush request
2021-08-11 14:26 [PATCH] blk-mq: fix kernel panic during iterating over flush request Ming Lei
2021-08-12 8:57 ` Christoph Hellwig
@ 2021-08-12 10:20 ` John Garry
2021-08-12 11:47 ` Xiaoguang Wang
` (2 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: John Garry @ 2021-08-12 10:20 UTC (permalink / raw)
To: Ming Lei, Jens Axboe
Cc: linux-block, Bart Van Assche, Christoph Hellwig, Blank-Burian,
Markus, Dr.
On 11/08/2021 15:26, Ming Lei wrote:
> For fixing use-after-free during iterating over requests, we grabbed
> request's refcount before calling ->fn in commit 2e315dc07df0 ("blk-mq:
> grab rq->refcount before calling ->fn in blk_mq_tagset_busy_iter").
> Turns out this way may cause kernel panic when iterating over one flush
> request:
>
> 1) old flush request's tag is just released, and this tag is reused by
> one new request, but ->rqs[] isn't updated yet
>
> 2) the flush request can be re-used for submitting one new flush command,
> so blk_rq_init() is called at the same time
>
> 3) meantime blk_mq_queue_tag_busy_iter() is called, and old flush request
> is retrieved from ->rqs[tag]; when blk_mq_put_rq_ref() is called,
> flush_rq->end_io may not be updated yet, so NULL pointer dereference
> is triggered in blk_mq_put_rq_ref().
>
> Fix the issue by calling refcount_set(&flush_rq->ref, 1) after
> flush_rq->end_io is set. So far the only other caller of blk_rq_init() is
> scsi_ioctl_reset() in which the request doesn't enter block IO stack and
> the request reference count isn't used, so the change is safe.
>
> Fixes: 2e315dc07df0 ("blk-mq: grab rq->refcount before calling ->fn in
> blk_mq_tagset_busy_iter")
> Reported-by: "Blank-Burian, Markus, Dr." <blankburian@uni-muenster.de>
> Tested-by: "Blank-Burian, Markus, Dr." <blankburian@uni-muenster.de>
> Signed-off-by: Ming Lei <ming.lei@redhat.com>
it looks ok:
Reviewed-by: John Garry <john.garry@huawei.com>
> ---
> block/blk-core.c | 1 -
> block/blk-flush.c | 8 ++++++++
> 2 files changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/block/blk-core.c b/block/blk-core.c
> index 0874bc2fcdb4..b5098739f72a 100644
> --- a/block/blk-core.c
> +++ b/block/blk-core.c
> @@ -121,7 +121,6 @@ void blk_rq_init(struct request_queue *q, struct request *rq)
> rq->internal_tag = BLK_MQ_NO_TAG;
> rq->start_time_ns = ktime_get_ns();
> rq->part = NULL;
> - refcount_set(&rq->ref, 1);
> blk_crypto_rq_set_defaults(rq);
> }
> EXPORT_SYMBOL(blk_rq_init);
> diff --git a/block/blk-flush.c b/block/blk-flush.c
> index 1002f6c58181..4912c8dbb1d8 100644
> --- a/block/blk-flush.c
> +++ b/block/blk-flush.c
> @@ -329,6 +329,14 @@ static void blk_kick_flush(struct request_queue *q, struct blk_flush_queue *fq,
> flush_rq->rq_flags |= RQF_FLUSH_SEQ;
> flush_rq->rq_disk = first_rq->rq_disk;
> flush_rq->end_io = flush_end_io;
> + /*
> + * Order WRITE ->end_io and WRITE rq->ref, and its pair is the one
> + * implied in refcount_inc_not_zero() called from
> + * blk_mq_find_and_get_req(), which orders WRITE/READ flush_rq->ref
> + * and READ flush_rq->end_io
> + */
> + smp_wmb();
> + refcount_set(&flush_rq->ref, 1);
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] blk-mq: fix kernel panic during iterating over flush request
2021-08-11 14:26 [PATCH] blk-mq: fix kernel panic during iterating over flush request Ming Lei
2021-08-12 8:57 ` Christoph Hellwig
2021-08-12 10:20 ` John Garry
@ 2021-08-12 11:47 ` Xiaoguang Wang
2021-08-12 13:04 ` Ming Lei
2021-08-17 0:58 ` Ming Lei
2021-08-17 14:33 ` Jens Axboe
4 siblings, 1 reply; 8+ messages in thread
From: Xiaoguang Wang @ 2021-08-12 11:47 UTC (permalink / raw)
To: Ming Lei, Jens Axboe
Cc: linux-block, Bart Van Assche, Christoph Hellwig, John Garry,
Blank-Burian, Markus, Dr.
hi,
> For fixing use-after-free during iterating over requests, we grabbed
> request's refcount before calling ->fn in commit 2e315dc07df0 ("blk-mq:
> grab rq->refcount before calling ->fn in blk_mq_tagset_busy_iter").
> Turns out this way may cause kernel panic when iterating over one flush
> request:
>
> 1) old flush request's tag is just released, and this tag is reused by
> one new request, but ->rqs[] isn't updated yet
>
> 2) the flush request can be re-used for submitting one new flush command,
> so blk_rq_init() is called at the same time
>
> 3) meantime blk_mq_queue_tag_busy_iter() is called, and old flush request
> is retrieved from ->rqs[tag]; when blk_mq_put_rq_ref() is called,
> flush_rq->end_io may not be updated yet, so NULL pointer dereference
> is triggered in blk_mq_put_rq_ref().
>
> Fix the issue by calling refcount_set(&flush_rq->ref, 1) after
> flush_rq->end_io is set. So far the only other caller of blk_rq_init() is
> scsi_ioctl_reset() in which the request doesn't enter block IO stack and
> the request reference count isn't used, so the change is safe.
>
> Fixes: 2e315dc07df0 ("blk-mq: grab rq->refcount before calling ->fn in
> blk_mq_tagset_busy_iter")
> Reported-by: "Blank-Burian, Markus, Dr." <blankburian@uni-muenster.de>
> Tested-by: "Blank-Burian, Markus, Dr." <blankburian@uni-muenster.de>
> Signed-off-by: Ming Lei <ming.lei@redhat.com>
> ---
> block/blk-core.c | 1 -
> block/blk-flush.c | 8 ++++++++
> 2 files changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/block/blk-core.c b/block/blk-core.c
> index 0874bc2fcdb4..b5098739f72a 100644
> --- a/block/blk-core.c
> +++ b/block/blk-core.c
> @@ -121,7 +121,6 @@ void blk_rq_init(struct request_queue *q, struct request *rq)
> rq->internal_tag = BLK_MQ_NO_TAG;
> rq->start_time_ns = ktime_get_ns();
> rq->part = NULL;
> - refcount_set(&rq->ref, 1);
> blk_crypto_rq_set_defaults(rq);
> }
> EXPORT_SYMBOL(blk_rq_init);
> diff --git a/block/blk-flush.c b/block/blk-flush.c
> index 1002f6c58181..4912c8dbb1d8 100644
> --- a/block/blk-flush.c
> +++ b/block/blk-flush.c
> @@ -329,6 +329,14 @@ static void blk_kick_flush(struct request_queue *q, struct blk_flush_queue *fq,
> flush_rq->rq_flags |= RQF_FLUSH_SEQ;
> flush_rq->rq_disk = first_rq->rq_disk;
> flush_rq->end_io = flush_end_io;
> + /*
> + * Order WRITE ->end_io and WRITE rq->ref, and its pair is the one
> + * implied in refcount_inc_not_zero() called from
> + * blk_mq_find_and_get_req(), which orders WRITE/READ flush_rq->ref
> + * and READ flush_rq->end_io
Recently we run into similar panic which is a NULL dereference on
rq->mq_hctx in is_flush_rq(), we also
guess there is race bug just what you have fixed.
But I have one question here, for a blk-mq device, before issuing the
first flush req, flush_rq->end_io
is NULL, and for following flush reqs on this blk-mq device,
flush_rq->end_io won't be NULL. I searched
the codes and don't find any place that sets flush_rq->end_io to be NULL
once flush_rq has been completed.
Regards,
Xiaoguang Wang
> + */
> + smp_wmb();
> + refcount_set(&flush_rq->ref, 1);
>
> blk_flush_queue_rq(flush_rq, false);
> }
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] blk-mq: fix kernel panic during iterating over flush request
2021-08-12 11:47 ` Xiaoguang Wang
@ 2021-08-12 13:04 ` Ming Lei
2021-08-12 13:19 ` Xiaoguang Wang
0 siblings, 1 reply; 8+ messages in thread
From: Ming Lei @ 2021-08-12 13:04 UTC (permalink / raw)
To: Xiaoguang Wang
Cc: Jens Axboe, linux-block, Bart Van Assche, Christoph Hellwig,
John Garry, Blank-Burian, Markus, Dr.
On Thu, Aug 12, 2021 at 07:47:55PM +0800, Xiaoguang Wang wrote:
> hi,
>
>
> > For fixing use-after-free during iterating over requests, we grabbed
> > request's refcount before calling ->fn in commit 2e315dc07df0 ("blk-mq:
> > grab rq->refcount before calling ->fn in blk_mq_tagset_busy_iter").
> > Turns out this way may cause kernel panic when iterating over one flush
> > request:
> >
> > 1) old flush request's tag is just released, and this tag is reused by
> > one new request, but ->rqs[] isn't updated yet
> >
> > 2) the flush request can be re-used for submitting one new flush command,
> > so blk_rq_init() is called at the same time
> >
> > 3) meantime blk_mq_queue_tag_busy_iter() is called, and old flush request
> > is retrieved from ->rqs[tag]; when blk_mq_put_rq_ref() is called,
> > flush_rq->end_io may not be updated yet, so NULL pointer dereference
> > is triggered in blk_mq_put_rq_ref().
> >
> > Fix the issue by calling refcount_set(&flush_rq->ref, 1) after
> > flush_rq->end_io is set. So far the only other caller of blk_rq_init() is
> > scsi_ioctl_reset() in which the request doesn't enter block IO stack and
> > the request reference count isn't used, so the change is safe.
> >
> > Fixes: 2e315dc07df0 ("blk-mq: grab rq->refcount before calling ->fn in
> > blk_mq_tagset_busy_iter")
> > Reported-by: "Blank-Burian, Markus, Dr." <blankburian@uni-muenster.de>
> > Tested-by: "Blank-Burian, Markus, Dr." <blankburian@uni-muenster.de>
> > Signed-off-by: Ming Lei <ming.lei@redhat.com>
> > ---
> > block/blk-core.c | 1 -
> > block/blk-flush.c | 8 ++++++++
> > 2 files changed, 8 insertions(+), 1 deletion(-)
> >
> > diff --git a/block/blk-core.c b/block/blk-core.c
> > index 0874bc2fcdb4..b5098739f72a 100644
> > --- a/block/blk-core.c
> > +++ b/block/blk-core.c
> > @@ -121,7 +121,6 @@ void blk_rq_init(struct request_queue *q, struct request *rq)
> > rq->internal_tag = BLK_MQ_NO_TAG;
> > rq->start_time_ns = ktime_get_ns();
> > rq->part = NULL;
> > - refcount_set(&rq->ref, 1);
> > blk_crypto_rq_set_defaults(rq);
> > }
> > EXPORT_SYMBOL(blk_rq_init);
> > diff --git a/block/blk-flush.c b/block/blk-flush.c
> > index 1002f6c58181..4912c8dbb1d8 100644
> > --- a/block/blk-flush.c
> > +++ b/block/blk-flush.c
> > @@ -329,6 +329,14 @@ static void blk_kick_flush(struct request_queue *q, struct blk_flush_queue *fq,
> > flush_rq->rq_flags |= RQF_FLUSH_SEQ;
> > flush_rq->rq_disk = first_rq->rq_disk;
> > flush_rq->end_io = flush_end_io;
> > + /*
> > + * Order WRITE ->end_io and WRITE rq->ref, and its pair is the one
> > + * implied in refcount_inc_not_zero() called from
> > + * blk_mq_find_and_get_req(), which orders WRITE/READ flush_rq->ref
> > + * and READ flush_rq->end_io
>
> Recently we run into similar panic which is a NULL dereference on
> rq->mq_hctx in is_flush_rq(), we also
>
> guess there is race bug just what you have fixed.
>
> But I have one question here, for a blk-mq device, before issuing the first
> flush req, flush_rq->end_io
>
> is NULL, and for following flush reqs on this blk-mq device,
> flush_rq->end_io won't be NULL. I searched
>
> the codes and don't find any place that sets flush_rq->end_io to be NULL
> once flush_rq has been completed.
blk_kick_flush():
blk_rq_init
memset(rq, 0, sizeof(*rq));
Thanks,
Ming
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] blk-mq: fix kernel panic during iterating over flush request
2021-08-12 13:04 ` Ming Lei
@ 2021-08-12 13:19 ` Xiaoguang Wang
0 siblings, 0 replies; 8+ messages in thread
From: Xiaoguang Wang @ 2021-08-12 13:19 UTC (permalink / raw)
To: Ming Lei
Cc: Jens Axboe, linux-block, Bart Van Assche, Christoph Hellwig,
John Garry, Blank-Burian, Markus, Dr.
hi,
> On Thu, Aug 12, 2021 at 07:47:55PM +0800, Xiaoguang Wang wrote:
>> hi,
>>
>>
>>> For fixing use-after-free during iterating over requests, we grabbed
>>> request's refcount before calling ->fn in commit 2e315dc07df0 ("blk-mq:
>>> grab rq->refcount before calling ->fn in blk_mq_tagset_busy_iter").
>>> Turns out this way may cause kernel panic when iterating over one flush
>>> request:
>>>
>>> 1) old flush request's tag is just released, and this tag is reused by
>>> one new request, but ->rqs[] isn't updated yet
>>>
>>> 2) the flush request can be re-used for submitting one new flush command,
>>> so blk_rq_init() is called at the same time
>>>
>>> 3) meantime blk_mq_queue_tag_busy_iter() is called, and old flush request
>>> is retrieved from ->rqs[tag]; when blk_mq_put_rq_ref() is called,
>>> flush_rq->end_io may not be updated yet, so NULL pointer dereference
>>> is triggered in blk_mq_put_rq_ref().
>>>
>>> Fix the issue by calling refcount_set(&flush_rq->ref, 1) after
>>> flush_rq->end_io is set. So far the only other caller of blk_rq_init() is
>>> scsi_ioctl_reset() in which the request doesn't enter block IO stack and
>>> the request reference count isn't used, so the change is safe.
>>>
>>> Fixes: 2e315dc07df0 ("blk-mq: grab rq->refcount before calling ->fn in
>>> blk_mq_tagset_busy_iter")
>>> Reported-by: "Blank-Burian, Markus, Dr." <blankburian@uni-muenster.de>
>>> Tested-by: "Blank-Burian, Markus, Dr." <blankburian@uni-muenster.de>
>>> Signed-off-by: Ming Lei <ming.lei@redhat.com>
>>> ---
>>> block/blk-core.c | 1 -
>>> block/blk-flush.c | 8 ++++++++
>>> 2 files changed, 8 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/block/blk-core.c b/block/blk-core.c
>>> index 0874bc2fcdb4..b5098739f72a 100644
>>> --- a/block/blk-core.c
>>> +++ b/block/blk-core.c
>>> @@ -121,7 +121,6 @@ void blk_rq_init(struct request_queue *q, struct request *rq)
>>> rq->internal_tag = BLK_MQ_NO_TAG;
>>> rq->start_time_ns = ktime_get_ns();
>>> rq->part = NULL;
>>> - refcount_set(&rq->ref, 1);
>>> blk_crypto_rq_set_defaults(rq);
>>> }
>>> EXPORT_SYMBOL(blk_rq_init);
>>> diff --git a/block/blk-flush.c b/block/blk-flush.c
>>> index 1002f6c58181..4912c8dbb1d8 100644
>>> --- a/block/blk-flush.c
>>> +++ b/block/blk-flush.c
>>> @@ -329,6 +329,14 @@ static void blk_kick_flush(struct request_queue *q, struct blk_flush_queue *fq,
>>> flush_rq->rq_flags |= RQF_FLUSH_SEQ;
>>> flush_rq->rq_disk = first_rq->rq_disk;
>>> flush_rq->end_io = flush_end_io;
>>> + /*
>>> + * Order WRITE ->end_io and WRITE rq->ref, and its pair is the one
>>> + * implied in refcount_inc_not_zero() called from
>>> + * blk_mq_find_and_get_req(), which orders WRITE/READ flush_rq->ref
>>> + * and READ flush_rq->end_io
>> Recently we run into similar panic which is a NULL dereference on
>> rq->mq_hctx in is_flush_rq(), we also
>>
>> guess there is race bug just what you have fixed.
>>
>> But I have one question here, for a blk-mq device, before issuing the first
>> flush req, flush_rq->end_io
>>
>> is NULL, and for following flush reqs on this blk-mq device,
>> flush_rq->end_io won't be NULL. I searched
>>
>> the codes and don't find any place that sets flush_rq->end_io to be NULL
>> once flush_rq has been completed.
> blk_kick_flush():
> blk_rq_init
> memset(rq, 0, sizeof(*rq));
Oh, thanks.
I searched codes for quite a while...
Regards,
Xiaoguang Wang
>
>
> Thanks,
> Ming
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] blk-mq: fix kernel panic during iterating over flush request
2021-08-11 14:26 [PATCH] blk-mq: fix kernel panic during iterating over flush request Ming Lei
` (2 preceding siblings ...)
2021-08-12 11:47 ` Xiaoguang Wang
@ 2021-08-17 0:58 ` Ming Lei
2021-08-17 14:33 ` Jens Axboe
4 siblings, 0 replies; 8+ messages in thread
From: Ming Lei @ 2021-08-17 0:58 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, Bart Van Assche, Christoph Hellwig, John Garry,
Blank-Burian, Markus, Dr.
On Wed, Aug 11, 2021 at 10:26:24PM +0800, Ming Lei wrote:
> For fixing use-after-free during iterating over requests, we grabbed
> request's refcount before calling ->fn in commit 2e315dc07df0 ("blk-mq:
> grab rq->refcount before calling ->fn in blk_mq_tagset_busy_iter").
> Turns out this way may cause kernel panic when iterating over one flush
> request:
>
> 1) old flush request's tag is just released, and this tag is reused by
> one new request, but ->rqs[] isn't updated yet
>
> 2) the flush request can be re-used for submitting one new flush command,
> so blk_rq_init() is called at the same time
>
> 3) meantime blk_mq_queue_tag_busy_iter() is called, and old flush request
> is retrieved from ->rqs[tag]; when blk_mq_put_rq_ref() is called,
> flush_rq->end_io may not be updated yet, so NULL pointer dereference
> is triggered in blk_mq_put_rq_ref().
>
> Fix the issue by calling refcount_set(&flush_rq->ref, 1) after
> flush_rq->end_io is set. So far the only other caller of blk_rq_init() is
> scsi_ioctl_reset() in which the request doesn't enter block IO stack and
> the request reference count isn't used, so the change is safe.
>
> Fixes: 2e315dc07df0 ("blk-mq: grab rq->refcount before calling ->fn in
> blk_mq_tagset_busy_iter")
> Reported-by: "Blank-Burian, Markus, Dr." <blankburian@uni-muenster.de>
> Tested-by: "Blank-Burian, Markus, Dr." <blankburian@uni-muenster.de>
> Signed-off-by: Ming Lei <ming.lei@redhat.com>
Hello Jens,
Ping...
Thanks,
Ming
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] blk-mq: fix kernel panic during iterating over flush request
2021-08-11 14:26 [PATCH] blk-mq: fix kernel panic during iterating over flush request Ming Lei
` (3 preceding siblings ...)
2021-08-17 0:58 ` Ming Lei
@ 2021-08-17 14:33 ` Jens Axboe
4 siblings, 0 replies; 8+ messages in thread
From: Jens Axboe @ 2021-08-17 14:33 UTC (permalink / raw)
To: Ming Lei
Cc: linux-block, Bart Van Assche, Christoph Hellwig, John Garry,
Blank-Burian, Markus, Dr.
On 8/11/21 8:26 AM, Ming Lei wrote:
> For fixing use-after-free during iterating over requests, we grabbed
> request's refcount before calling ->fn in commit 2e315dc07df0 ("blk-mq:
> grab rq->refcount before calling ->fn in blk_mq_tagset_busy_iter").
> Turns out this way may cause kernel panic when iterating over one flush
> request:
>
> 1) old flush request's tag is just released, and this tag is reused by
> one new request, but ->rqs[] isn't updated yet
>
> 2) the flush request can be re-used for submitting one new flush command,
> so blk_rq_init() is called at the same time
>
> 3) meantime blk_mq_queue_tag_busy_iter() is called, and old flush request
> is retrieved from ->rqs[tag]; when blk_mq_put_rq_ref() is called,
> flush_rq->end_io may not be updated yet, so NULL pointer dereference
> is triggered in blk_mq_put_rq_ref().
>
> Fix the issue by calling refcount_set(&flush_rq->ref, 1) after
> flush_rq->end_io is set. So far the only other caller of blk_rq_init() is
> scsi_ioctl_reset() in which the request doesn't enter block IO stack and
> the request reference count isn't used, so the change is safe.
Applied, thanks.
--
Jens Axboe
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2021-08-17 14:35 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-11 14:26 [PATCH] blk-mq: fix kernel panic during iterating over flush request Ming Lei
2021-08-12 8:57 ` Christoph Hellwig
2021-08-12 10:20 ` John Garry
2021-08-12 11:47 ` Xiaoguang Wang
2021-08-12 13:04 ` Ming Lei
2021-08-12 13:19 ` Xiaoguang Wang
2021-08-17 0:58 ` Ming Lei
2021-08-17 14:33 ` Jens Axboe
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.