Skip to content

Commit 370ac28

Browse files
shroffniaxboe
authored andcommitted
block: avoid cpu_hotplug_lock depedency on freeze_lock
A recent lockdep[1] splat observed while running blktest block/005 reveals a potential deadlock caused by the cpu_hotplug_lock dependency on ->freeze_lock. This dependency was introduced by commit 033b667 ("block: blk-rq-qos: guard rq-qos helpers by static key"). That change added a static key to avoid fetching q->rq_qos when neither blk-wbt nor blk-iolatency is configured. The static key dynamically patches kernel text to a NOP when disabled, eliminating overhead of fetching q->rq_qos in the I/O hot path. However, enabling a static key at runtime requires acquiring both cpu_hotplug_lock and jump_label_mutex. When this happens after the queue has already been frozen (i.e., while holding ->freeze_lock), it creates a locking dependency from cpu_hotplug_lock to ->freeze_lock, which leads to a potential deadlock reported by lockdep [1]. To resolve this, replace the static key mechanism with q->queue_flags: QUEUE_FLAG_QOS_ENABLED. This flag is evaluated in the fast path before accessing q->rq_qos. If the flag is set, we proceed to fetch q->rq_qos; otherwise, the access is skipped. Since q->queue_flags is commonly accessed in IO hotpath and resides in the first cacheline of struct request_queue, checking it imposes minimal overhead while eliminating the deadlock risk. This change avoids the lockdep splat without introducing performance regressions. [1] https://lore.kernel.org/linux-block/4fdm37so3o4xricdgfosgmohn63aa7wj3ua4e5vpihoamwg3ui@fq42f5q5t5ic/ Reported-by: Shinichiro Kawasaki <[email protected]> Closes: https://lore.kernel.org/linux-block/4fdm37so3o4xricdgfosgmohn63aa7wj3ua4e5vpihoamwg3ui@fq42f5q5t5ic/ Fixes: 033b667 ("block: blk-rq-qos: guard rq-qos helpers by static key") Tested-by: Shin'ichiro Kawasaki <[email protected]> Signed-off-by: Nilay Shroff <[email protected]> Reviewed-by: Ming Lei <[email protected]> Reviewed-by: Yu Kuai <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent ade1bee commit 370ac28

File tree

4 files changed

+37
-28
lines changed

4 files changed

+37
-28
lines changed

block/blk-mq-debugfs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ static const char *const blk_queue_flag_name[] = {
9595
QUEUE_FLAG_NAME(SQ_SCHED),
9696
QUEUE_FLAG_NAME(DISABLE_WBT_DEF),
9797
QUEUE_FLAG_NAME(NO_ELV_SWITCH),
98+
QUEUE_FLAG_NAME(QOS_ENABLED),
9899
};
99100
#undef QUEUE_FLAG_NAME
100101

block/blk-rq-qos.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
#include "blk-rq-qos.h"
44

5-
__read_mostly DEFINE_STATIC_KEY_FALSE(block_rq_qos);
6-
75
/*
86
* Increment 'v', if 'v' is below 'below'. Returns true if we succeeded,
97
* false if 'v' + 1 would be bigger than 'below'.
@@ -319,8 +317,8 @@ void rq_qos_exit(struct request_queue *q)
319317
struct rq_qos *rqos = q->rq_qos;
320318
q->rq_qos = rqos->next;
321319
rqos->ops->exit(rqos);
322-
static_branch_dec(&block_rq_qos);
323320
}
321+
blk_queue_flag_clear(QUEUE_FLAG_QOS_ENABLED, q);
324322
mutex_unlock(&q->rq_qos_mutex);
325323
}
326324

@@ -346,7 +344,7 @@ int rq_qos_add(struct rq_qos *rqos, struct gendisk *disk, enum rq_qos_id id,
346344
goto ebusy;
347345
rqos->next = q->rq_qos;
348346
q->rq_qos = rqos;
349-
static_branch_inc(&block_rq_qos);
347+
blk_queue_flag_set(QUEUE_FLAG_QOS_ENABLED, q);
350348

351349
blk_mq_unfreeze_queue(q, memflags);
352350

@@ -374,10 +372,11 @@ void rq_qos_del(struct rq_qos *rqos)
374372
for (cur = &q->rq_qos; *cur; cur = &(*cur)->next) {
375373
if (*cur == rqos) {
376374
*cur = rqos->next;
377-
static_branch_dec(&block_rq_qos);
378375
break;
379376
}
380377
}
378+
if (!q->rq_qos)
379+
blk_queue_flag_clear(QUEUE_FLAG_QOS_ENABLED, q);
381380
blk_mq_unfreeze_queue(q, memflags);
382381

383382
mutex_lock(&q->debugfs_mutex);

block/blk-rq-qos.h

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "blk-mq-debugfs.h"
1313

1414
struct blk_mq_debugfs_attr;
15-
extern struct static_key_false block_rq_qos;
1615

1716
enum rq_qos_id {
1817
RQ_QOS_WBT,
@@ -113,49 +112,55 @@ void __rq_qos_queue_depth_changed(struct rq_qos *rqos);
113112

114113
static inline void rq_qos_cleanup(struct request_queue *q, struct bio *bio)
115114
{
116-
if (static_branch_unlikely(&block_rq_qos) && q->rq_qos)
115+
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
116+
q->rq_qos)
117117
__rq_qos_cleanup(q->rq_qos, bio);
118118
}
119119

120120
static inline void rq_qos_done(struct request_queue *q, struct request *rq)
121121
{
122-
if (static_branch_unlikely(&block_rq_qos) && q->rq_qos &&
123-
!blk_rq_is_passthrough(rq))
122+
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
123+
q->rq_qos && !blk_rq_is_passthrough(rq))
124124
__rq_qos_done(q->rq_qos, rq);
125125
}
126126

127127
static inline void rq_qos_issue(struct request_queue *q, struct request *rq)
128128
{
129-
if (static_branch_unlikely(&block_rq_qos) && q->rq_qos)
129+
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
130+
q->rq_qos)
130131
__rq_qos_issue(q->rq_qos, rq);
131132
}
132133

133134
static inline void rq_qos_requeue(struct request_queue *q, struct request *rq)
134135
{
135-
if (static_branch_unlikely(&block_rq_qos) && q->rq_qos)
136+
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
137+
q->rq_qos)
136138
__rq_qos_requeue(q->rq_qos, rq);
137139
}
138140

139141
static inline void rq_qos_done_bio(struct bio *bio)
140142
{
141-
if (static_branch_unlikely(&block_rq_qos) &&
142-
bio->bi_bdev && (bio_flagged(bio, BIO_QOS_THROTTLED) ||
143-
bio_flagged(bio, BIO_QOS_MERGED))) {
144-
struct request_queue *q = bdev_get_queue(bio->bi_bdev);
145-
146-
/*
147-
* If a bio has BIO_QOS_xxx set, it implicitly implies that
148-
* q->rq_qos is present. So, we skip re-checking q->rq_qos
149-
* here as an extra optimization and directly call
150-
* __rq_qos_done_bio().
151-
*/
152-
__rq_qos_done_bio(q->rq_qos, bio);
153-
}
143+
struct request_queue *q;
144+
145+
if (!bio->bi_bdev || (!bio_flagged(bio, BIO_QOS_THROTTLED) &&
146+
!bio_flagged(bio, BIO_QOS_MERGED)))
147+
return;
148+
149+
q = bdev_get_queue(bio->bi_bdev);
150+
151+
/*
152+
* If a bio has BIO_QOS_xxx set, it implicitly implies that
153+
* q->rq_qos is present. So, we skip re-checking q->rq_qos
154+
* here as an extra optimization and directly call
155+
* __rq_qos_done_bio().
156+
*/
157+
__rq_qos_done_bio(q->rq_qos, bio);
154158
}
155159

156160
static inline void rq_qos_throttle(struct request_queue *q, struct bio *bio)
157161
{
158-
if (static_branch_unlikely(&block_rq_qos) && q->rq_qos) {
162+
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
163+
q->rq_qos) {
159164
bio_set_flag(bio, BIO_QOS_THROTTLED);
160165
__rq_qos_throttle(q->rq_qos, bio);
161166
}
@@ -164,22 +169,25 @@ static inline void rq_qos_throttle(struct request_queue *q, struct bio *bio)
164169
static inline void rq_qos_track(struct request_queue *q, struct request *rq,
165170
struct bio *bio)
166171
{
167-
if (static_branch_unlikely(&block_rq_qos) && q->rq_qos)
172+
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
173+
q->rq_qos)
168174
__rq_qos_track(q->rq_qos, rq, bio);
169175
}
170176

171177
static inline void rq_qos_merge(struct request_queue *q, struct request *rq,
172178
struct bio *bio)
173179
{
174-
if (static_branch_unlikely(&block_rq_qos) && q->rq_qos) {
180+
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
181+
q->rq_qos) {
175182
bio_set_flag(bio, BIO_QOS_MERGED);
176183
__rq_qos_merge(q->rq_qos, rq, bio);
177184
}
178185
}
179186

180187
static inline void rq_qos_queue_depth_changed(struct request_queue *q)
181188
{
182-
if (static_branch_unlikely(&block_rq_qos) && q->rq_qos)
189+
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
190+
q->rq_qos)
183191
__rq_qos_queue_depth_changed(q->rq_qos);
184192
}
185193

include/linux/blkdev.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ enum {
656656
QUEUE_FLAG_SQ_SCHED, /* single queue style io dispatch */
657657
QUEUE_FLAG_DISABLE_WBT_DEF, /* for sched to disable/enable wbt */
658658
QUEUE_FLAG_NO_ELV_SWITCH, /* can't switch elevator any more */
659+
QUEUE_FLAG_QOS_ENABLED, /* qos is enabled */
659660
QUEUE_FLAG_MAX
660661
};
661662

0 commit comments

Comments
 (0)