blk-mq: Fix two racy hctx->tags->rqs[] assignments
PATCH 1/1 blk-mq: Fix two racy hctx->tags->rqs[] assignments
hctx->tags->rqs[] must be cleared before releasing a request tag because otherwise clearing that pointer races with the following assignment in blk_mq_get_driver_tag():
rcu_assign_pointer(hctx->tags->rqs[rq->tag], rq);
--- block/blk-mq.c | 2 +- block/blk-mq.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
block/blk-mq.c +1 -1
@@ -501,8 +501,8 @@ static void __blk_mq_free_request(struct request *rq)blk_pm_mark_last_busy (rq); rq->mq_hctx =NULL ;if (rq->tag !=BLK_MQ_NO_TAG ) { -blk_mq_put_tag (hctx->tags, ctx, rq->tag);rcu_assign_pointer (hctx->tags->rqs[rq->tag],NULL ); +blk_mq_put_tag (hctx->tags, ctx, rq->tag); }if (sched_tag !=BLK_MQ_NO_TAG )blk_mq_put_tag (hctx->sched_tags, ctx, sched_tag);
block/blk-mq.h +1 -1
@@ -225,8 +225,8 @@ static inline int __blk_mq_active_requests(struct blk_mq_hw_ctx *hctx)static inline void __blk_mq_put_driver_tag (struct blk_mq_hw_ctx *hctx,struct request *rq) { -blk_mq_put_tag (hctx->tags, rq->mq_ctx, rq->tag);rcu_assign_pointer (hctx->tags->rqs[rq->tag],NULL ); +blk_mq_put_tag (hctx->tags, rq->mq_ctx, rq->tag); rq->tag =BLK_MQ_NO_TAG ;if (rq->rq_flags &RQF_MQ_INFLIGHT ) {
Jens Axboe
maintainerQuoted 5 lines · Bart Van Assche
hctx->tags->rqs[] must be cleared before releasing a request tag because otherwise clearing that pointer races with the following assignment in
blk_mq_get_driver_tag():rcu_assign_pointer(hctx->tags->rqs[rq->tag], rq);
Looks good to me. Applied, and added:
which was missing.
Signature · 1 line
Jens Axboe
Quoted 37 lines · Bart Van Assche
hctx->tags->rqs[] must be cleared before releasing a request tag because otherwise clearing that pointer races with the following assignment in
blk_mq_get_driver_tag():rcu_assign_pointer(hctx->tags->rqs[rq->tag], rq);Reported-by Ming Lei <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Ming Lei <[email protected]>Cc Hannes Reinecke <[email protected]>Cc John Garry <[email protected]>Signed-off-by Bart Van Assche <[email protected]>--- block/blk-mq.c | 2 +- block/blk-mq.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)block/blk-mq.c +1
-1@@ -501,8 +501,8 @@ static void __blk_mq_free_request(struct request *rq)blk_pm_mark_last_busy (rq); rq->mq_hctx =NULL ;if (rq->tag !=BLK_MQ_NO_TAG ) { -blk_mq_put_tag (hctx->tags, ctx, rq->tag);rcu_assign_pointer (hctx->tags->rqs[rq->tag],NULL ); +blk_mq_put_tag (hctx->tags, ctx, rq->tag); }if (sched_tag !=BLK_MQ_NO_TAG )blk_mq_put_tag (hctx->sched_tags, ctx, sched_tag);block/blk-mq.h +0
-0
block/blk-mq.h static inline int __blk_mq_active_requests(struct blk_mq_hw_ctx *hctx) · line 232
Quoted 9 lines
block/blk-mq.h +1
-1@@ -225,8 +225,8 @@ static inline int __blk_mq_active_requests(struct blk_mq_hw_ctx *hctx)static inline void __blk_mq_put_driver_tag (struct blk_mq_hw_ctx *hctx,struct request *rq) { -blk_mq_put_tag (hctx->tags, rq->mq_ctx, rq->tag);rcu_assign_pointer (hctx->tags->rqs[rq->tag],NULL ); +blk_mq_put_tag (hctx->tags, rq->mq_ctx, rq->tag); rq->tag =BLK_MQ_NO_TAG ;if (rq->rq_flags &RQF_MQ_INFLIGHT ) {
I'd suggest to document the memory order which is key to the usage's correctness, especially both memory barriers are implied in allocating & releasing bit tag.
Thanks,
Ming