* [PATCH 0/4] Fix sparse warnings in the block layer
@ 2026-12-04 22:10 Bart Van Assche
2026-12-04 22:10 ` [PATCH 1/4] block: Fix sparse warnings in blk_op_str() Bart Van Assche
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Bart Van Assche @ 2026-12-04 22:10 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, Christoph Hellwig, Damien Le Moal, Bart Van Assche
Hi Jens,
This patch series fixes sparse warnings in the block layer core, in
blk-mq and in the block tracepoints. These warnings are caused by the
__bitwise annotations of blk_opf_t and blk_mq_req_flags_t and by the
__rcu annotation of struct request_queue::queue_hw_ctx. The warnings
fixed by patch 1/4 are only reported by sparse after a pending sparse
change has been merged (I plan to merge that sparse change soon). These
patches do not modify the code generated for x86-64 systems. Please
consider this patch series for the next merge window.
Thanks,
Bart.
Bart Van Assche (4):
block: Fix sparse warnings in blk_op_str()
block: Fix a sparse warning in str_to_blk_op()
blk-mq: Fix sparse warnings in block_rq_tag_wait
blk-mq: Use RCU helpers when accessing q->queue_hw_ctx
block/blk-core.c | 7 ++++---
block/blk-mq.c | 22 ++++++++++++++++++----
include/trace/events/block.h | 2 +-
3 files changed, 23 insertions(+), 8 deletions(-)
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/4] block: Fix sparse warnings in blk_op_str()
2026-12-04 22:10 [PATCH 0/4] Fix sparse warnings in the block layer Bart Van Assche
@ 2026-12-04 22:10 ` Bart Van Assche
2026-12-04 22:11 ` [PATCH 2/4] block: Fix a sparse warning in str_to_blk_op() Bart Van Assche
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Bart Van Assche @ 2026-12-04 22:10 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, Christoph Hellwig, Damien Le Moal, Bart Van Assche
Convert the op argument of blk_op_str() to a local unsigned int variable
using (__force unsigned int) before comparing it against
ARRAY_SIZE(blk_op_name) and indexing blk_op_name[].
Because the enumerators of enum req_op are defined with (__force blk_opf_t)
and blk_opf_t is a __bitwise type, sparse treats enum req_op as a
restricted bitwise type. Hence, an enum req_op value must neither be
compared directly against ARRAY_SIZE(blk_op_name) nor be used directly as
an array subscript.
Sparse does not yet report this because blk_op_str() is an inline function
that is exported but that is not called from block/blk-core.c, and because
sparse skips the body of inline functions that have no caller. A sparse
change is pending that makes sparse check the body of exported inline
functions that have no caller in their translation unit. With that change
applied, sparse reports the following warnings:
block/blk-core.c:130:13: warning: restricted blk_opf_t degrades to integer
block/blk-core.c:130:57: warning: restricted blk_opf_t degrades to integer
block/blk-core.c:131:38: warning: restricted blk_opf_t degrades to integer
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
block/blk-core.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/block/blk-core.c b/block/blk-core.c
index 2ee079c1ce15..6802f4d88ec8 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -126,9 +126,10 @@ static const char *const blk_op_name[] = {
inline const char *blk_op_str(enum req_op op)
{
const char *op_str = "UNKNOWN";
+ unsigned int uint_op = (__force unsigned int)op;
- if (op < ARRAY_SIZE(blk_op_name) && blk_op_name[op])
- op_str = blk_op_name[op];
+ if (uint_op < ARRAY_SIZE(blk_op_name) && blk_op_name[uint_op])
+ op_str = blk_op_name[uint_op];
return op_str;
}
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/4] block: Fix a sparse warning in str_to_blk_op()
2026-12-04 22:10 [PATCH 0/4] Fix sparse warnings in the block layer Bart Van Assche
2026-12-04 22:10 ` [PATCH 1/4] block: Fix sparse warnings in blk_op_str() Bart Van Assche
@ 2026-12-04 22:11 ` Bart Van Assche
2026-12-04 22:11 ` [PATCH 3/4] blk-mq: Fix sparse warnings in block_rq_tag_wait Bart Van Assche
2026-12-04 22:11 ` [PATCH 4/4] blk-mq: Use RCU helpers when accessing q->queue_hw_ctx Bart Van Assche
3 siblings, 0 replies; 5+ messages in thread
From: Bart Van Assche @ 2026-12-04 22:11 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, Christoph Hellwig, Damien Le Moal, Bart Van Assche
Add the __force qualifier to the (enum req_op) cast in str_to_blk_op()
when returning the matching index into blk_op_name[].
Because the enumerators of enum req_op are defined with (__force blk_opf_t)
and blk_opf_t is a __bitwise type, sparse treats enum req_op as a
restricted bitwise type and warns when casting a plain int to enum req_op
without __force. Match the (__force blk_status_t) casts used in
errno_to_blk_status() and tag_to_blk_status().
This fixes the following sparse warning:
block/blk-core.c:144:33: warning: cast to restricted blk_opf_t
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
block/blk-core.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/block/blk-core.c b/block/blk-core.c
index 6802f4d88ec8..7f74d76ce0c7 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -141,7 +141,7 @@ enum req_op str_to_blk_op(const char *op)
for (i = 0; i < ARRAY_SIZE(blk_op_name); i++)
if (blk_op_name[i] && !strcmp(blk_op_name[i], op))
- return (enum req_op)i;
+ return (__force enum req_op)i;
return REQ_OP_LAST;
}
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 3/4] blk-mq: Fix sparse warnings in block_rq_tag_wait
2026-12-04 22:10 [PATCH 0/4] Fix sparse warnings in the block layer Bart Van Assche
2026-12-04 22:10 ` [PATCH 1/4] block: Fix sparse warnings in blk_op_str() Bart Van Assche
2026-12-04 22:11 ` [PATCH 2/4] block: Fix a sparse warning in str_to_blk_op() Bart Van Assche
@ 2026-12-04 22:11 ` Bart Van Assche
2026-12-04 22:11 ` [PATCH 4/4] blk-mq: Use RCU helpers when accessing q->queue_hw_ctx Bart Van Assche
3 siblings, 0 replies; 5+ messages in thread
From: Bart Van Assche @ 2026-12-04 22:11 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, Christoph Hellwig, Damien Le Moal, Bart Van Assche,
Steven Rostedt, Masami Hiramatsu, Aaron Tomlin
Change the type of the alloc_flags parameter of the block_rq_tag_wait
tracepoint from unsigned int to blk_mq_req_flags_t.
blk_mq_get_tag() passes data->flags, which has the __bitwise type
blk_mq_req_flags_t, as the alloc_flags argument, and TP_fast_assign masks
alloc_flags with BLK_MQ_REQ_RESERVED, which also has type
blk_mq_req_flags_t. Declaring alloc_flags as unsigned int causes sparse
type-mismatch warnings at the call site and in the bitwise AND expression.
This fixes the following sparse warnings:
./include/trace/events/block.h:244:1: warning: restricted blk_mq_req_flags_t degrades to integer
block/blk-mq-tag.c:195:45: warning: incorrect type in argument 4 (different base types)
block/blk-mq-tag.c:195:45: expected unsigned int alloc_flags
block/blk-mq-tag.c:195:45: got restricted blk_mq_req_flags_t [usertype] flags
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
include/trace/events/block.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/trace/events/block.h b/include/trace/events/block.h
index 9c97a16850b9..32563fac73c0 100644
--- a/include/trace/events/block.h
+++ b/include/trace/events/block.h
@@ -244,7 +244,7 @@ DECLARE_EVENT_CLASS(block_rq,
TRACE_EVENT(block_rq_tag_wait,
TP_PROTO(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
- bool is_sched_tag, unsigned int alloc_flags),
+ bool is_sched_tag, blk_mq_req_flags_t alloc_flags),
TP_ARGS(q, hctx, is_sched_tag, alloc_flags),
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 4/4] blk-mq: Use RCU helpers when accessing q->queue_hw_ctx
2026-12-04 22:10 [PATCH 0/4] Fix sparse warnings in the block layer Bart Van Assche
` (2 preceding siblings ...)
2026-12-04 22:11 ` [PATCH 3/4] blk-mq: Fix sparse warnings in block_rq_tag_wait Bart Van Assche
@ 2026-12-04 22:11 ` Bart Van Assche
3 siblings, 0 replies; 5+ messages in thread
From: Bart Van Assche @ 2026-12-04 22:11 UTC (permalink / raw)
To: Jens Axboe
Cc: linux-block, Christoph Hellwig, Damien Le Moal, Bart Van Assche
Use RCU helpers when accessing q->queue_hw_ctx in block/blk-mq.c:
- Introduce queue_hctx_entered() and use it in
blk_mq_alloc_request_hctx() and blk_mq_poll(). Both functions access
q->queue_hw_ctx while holding a q_usage_counter reference.
q->queue_hw_ctx is allocated when the request queue is initialized and
is only reallocated while the request queue is frozen. Hence use
rcu_dereference_protected() instead of queue_hctx() to avoid adding an
RCU read-side critical section in these two functions.
- Use rcu_access_pointer() when freeing q->queue_hw_ctx in
blk_mq_release().
- Use rcu_dereference_protected() with set->tag_list_lock held in
__blk_mq_realloc_hw_ctxs().
Commit 89e1fb7ceffd ("blk-mq: fix potential uaf for 'queue_hw_ctx'")
annotated struct request_queue::queue_hw_ctx with __rcu, leaving these
four direct accesses triggering the following sparse warnings:
block/blk-mq.c:785:36: warning: dereference of noderef expression
block/blk-mq.c:4482:16: warning: incorrect type in argument 1 (different address spaces)
block/blk-mq.c:4482:16: expected void const *objp
block/blk-mq.c:4482:16: got struct blk_mq_hw_ctx *[noderef] __rcu *queue_hw_ctx
block/blk-mq.c:4627:41: warning: incorrect type in initializer (different address spaces)
block/blk-mq.c:4627:41: expected struct blk_mq_hw_ctx **hctxs
block/blk-mq.c:4627:41: got struct blk_mq_hw_ctx *[noderef] __rcu *queue_hw_ctx
block/blk-mq.c:5358:31: warning: dereference of noderef expression
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
block/blk-mq.c | 22 ++++++++++++++++++----
1 file changed, 18 insertions(+), 4 deletions(-)
diff --git a/block/blk-mq.c b/block/blk-mq.c
index d3fa7abcffb5..bc10eee503ca 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -736,6 +736,18 @@ static unsigned int blk_mq_housekeeping_cpu(void)
return cpu;
}
+/*
+ * Return a hardware queue pointer. The caller must hold a q_usage_counter
+ * reference. q->queue_hw_ctx is allocated when the request queue is
+ * initialized and is only reallocated while the request queue is frozen.
+ */
+static inline struct blk_mq_hw_ctx *queue_hctx_entered(struct request_queue *q,
+ unsigned int id)
+{
+ return rcu_dereference_protected(q->queue_hw_ctx,
+ !percpu_ref_is_zero(&q->q_usage_counter))[id];
+}
+
struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
blk_opf_t opf, blk_mq_req_flags_t flags, unsigned int hctx_idx)
{
@@ -782,7 +794,7 @@ struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
* If not tell the caller that it should skip this queue.
*/
ret = -EXDEV;
- data.hctx = q->queue_hw_ctx[hctx_idx];
+ data.hctx = queue_hctx_entered(q, hctx_idx);
if (!blk_mq_hw_queue_mapped(data.hctx))
goto out_queue_exit;
cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask);
@@ -4479,7 +4491,7 @@ void blk_mq_release(struct request_queue *q)
kobject_put(&hctx->kobj);
}
- kfree(q->queue_hw_ctx);
+ kfree(rcu_access_pointer(q->queue_hw_ctx));
/*
* release .mq_kobj and sw queue's kobject now because
@@ -4624,7 +4636,9 @@ static void __blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
struct request_queue *q)
{
int i, j, end;
- struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
+ struct blk_mq_hw_ctx **hctxs =
+ rcu_dereference_protected(q->queue_hw_ctx,
+ lockdep_is_held(&set->tag_list_lock));
if (q->nr_hw_queues < set->nr_hw_queues) {
struct blk_mq_hw_ctx **new_hctxs;
@@ -5355,7 +5369,7 @@ int blk_mq_poll(struct request_queue *q, blk_qc_t cookie,
*/
if (cookie >= q->nr_hw_queues)
return 0;
- hctx = q->queue_hw_ctx[cookie];
+ hctx = queue_hctx_entered(q, cookie);
if (hctx->type != HCTX_TYPE_POLL)
return 0;
return blk_hctx_poll(q, hctx, iob, flags);
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-04 22:11 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 22:10 [PATCH 0/4] Fix sparse warnings in the block layer Bart Van Assche
2026-12-04 22:10 ` [PATCH 1/4] block: Fix sparse warnings in blk_op_str() Bart Van Assche
2026-12-04 22:11 ` [PATCH 2/4] block: Fix a sparse warning in str_to_blk_op() Bart Van Assche
2026-12-04 22:11 ` [PATCH 3/4] blk-mq: Fix sparse warnings in block_rq_tag_wait Bart Van Assche
2026-12-04 22:11 ` [PATCH 4/4] blk-mq: Use RCU helpers when accessing q->queue_hw_ctx Bart Van Assche
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox