Skip to contentSkip to conversation

blk-mq: add tracepoint block_rq_tag_wait

PATCH Applied Aaron Tomlin 2 participants 2 messages May 25 – May 26

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v9 1/1 blk-mq: add tracepoint block_rq_tag_wait

In high-performance storage environments, particularly when utilising RAID controllers with shared tag sets (BLK_MQ_F_TAG_HCTX_SHARED), severe latency spikes can occur when fast devices (SSDs) are starved of hardware tags when sharing the same blk_mq_tag_set.

Currently, diagnosing this specific hardware queue contention is difficult. When a CPU thread exhausts the tag pool, blk_mq_get_tag() forces the current thread to block uninterruptible via io_schedule(). While this can be inferred via sched:sched_switch or dynamically traced by attaching a kprobe to blk_mq_mark_tag_wait(), there is no dedicated, out-of-the-box observability for this event.

This patch introduces the block_rq_tag_wait tracepoint in the tag allocation slow-path. It triggers immediately before the task state is altered to TASK_UNINTERRUPTIBLE (ensuring safety for PREEMPT_RT locks). It exposes the exact hardware context (hctx) that is starved, the specific pool experiencing starvation (driver, software scheduler, or reserved), and the exact pool depth.

This provides storage engineers with a zero-configuration, low-overhead mechanism to definitively identify shared-tag bottlenecks. For example, userspace can trivially replicate tag starvation counters using bpftrace:

    # bpftrace -e 'tracepoint:block:block_rq_tag_wait { @tag_waits[cpu] = count(); }'
    Attaching 1 probe...
    ^C
    @tag_waits[4]: 12
    @tag_waits[12]: 87

---
Hi Johannes, Damien, Chaitanya, Laurence,

I have dropped the earlier "Reviewed-by:" and "Tested-by:" tags because of functional logic changes in the tracepoint assignment block. A fresh review would be highly appreciated. Thank you.

Changes since v8 [1]:
- Fixed the standard pool depth calculation in TP_fast_assign to accurately report the unreserved capacity by mathematically subtracting nr_reserved_tags from nr_tags

- Removed "Reviewed-by:" and "Tested-by:" tags due to the functional logic updates in the tracepoint assignment block

Changes since v7 [2]:
- Added an is_reserved boolean to the trace record to explicitly expose reserved pool starvation to userspace

- Fixed TP_fast_assign to report the correct nr_reserved_tags depth when I/O schedulers utilise the reserved pool

Changes since v6 [3]:
- Dropped Patch 2. Observability is now driven entirely by the tracepoint, with the commit message updated to demonstrate how userspace (e.g., bpftrace) can safely replicate counting out-of-band (Jens Axboe)

- Moved tracepoint call above sbitmap_prepare_to_wait(). This prevents inadvertently resetting the task state under PREEMPT_RT locks

- Updated the tracepoint signature and TP_fast_assign block to evaluate the allocation flags. If the submitting context is starved of a reserved tag (BLK_MQ_REQ_RESERVED), the tracepoint now accurately reports the severely constrained nr_reserved_tags depth instead of the total nr_tags depth.

Changes since v5 [4]:
- Replaced this_cpu_inc() with raw_cpu_inc() within blk_mq_debugfs_inc_wait_tags(). This resolves a preemption warning triggered under CONFIG_DEBUG_PREEMPT=y, as the routine is invoked from a preemptible context immediately prior to io_schedule(). This adjustment deliberately prioritises the reduction of execution overhead over absolute statistical precision for this diagnostic interface.

Changes since v4 [5]:
- Prevented a NULL pointer dereference in the tracepoint fast-assign for disk-less request queues by safely checking q->disk before resolving the dev_t

- Fixed a Use-After-Free (UAF) and permanent memory leak by decoupling the per-CPU counter allocation from the volatile debugfs lifecycle and tying it directly to the core hctx lifecycle (i.e., blk_mq_init_hctx() and blk_mq_exit_hctx())

- Fixed a potential compiler double-fetch bug by wrapping the per-CPU pointer evaluations with READ_ONCE() in blk_mq_debugfs_inc_wait_tags()

- Passed the appropriate gfp_t flags down to the allocation routines to maintain the strict GFP_NOIO context

- Updated kernel-doc descriptions to clarify that the NULL pointer checks guard against memory allocation failures under pressure, rather than initialisation race conditions

Changes since v3 [6]:
- Transitioned tracking architecture from shared atomic_t variables to dynamically allocated per-CPU counters to resolve cache line bouncing (Bart Van Assche)

Changes since v2 [7]:
- Added "Reviewed-by:" and "Tested-by:" tags for patch 1

- Evaluate is_sched_tag directly within TP_fast_assign (Steven Rostedt)

- Introduced atomic counters via debugfs

Changes since v1 [8]:
- Improved the description of the trace point (Damien Le Moal)

- Removed the redundant "active requests" (Laurence Oberman)

- Introduced pool-specific starvation tracking

[1]: https://lore.kernel.org/lkml/[email protected]/
[2]: https://lore.kernel.org/lkml/[email protected]/
[3]: https://lore.kernel.org/lkml/[email protected]/
[4]: https://lore.kernel.org/lkml/[email protected]/
[5]: https://lore.kernel.org/lkml/[email protected]/
[6]: https://lore.kernel.org/lkml/[email protected]/
[7]: https://lore.kernel.org/lkml/[email protected]/
[8]: https://lore.kernel.org/lkml/[email protected]/

---
 block/blk-mq-tag.c           |  6 ++++
 include/trace/events/block.h | 59 ++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)
block/blk-mq-tag.c +6 -0
index 33946cdb5716..35deee5bbc73 100644
@@ -13,6 +13,7 @@
 #include <linux/kmemleak.h>
 
 #include <linux/delay.h>
+#include <trace/events/block.h>
 #include "blk.h"
 #include "blk-mq.h"
 #include "blk-mq-sched.h"
@@ -181,6 +182,11 @@ unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
 		if (tag != BLK_MQ_NO_TAG)
 			break;
 
+		/* Log the starvation event before altering task state */
+		trace_block_rq_tag_wait(data->q, data->hctx,
+					data->rq_flags & RQF_SCHED_TAGS,
+					data->flags);
+
 		sbitmap_prepare_to_wait(bt, ws, &wait, TASK_UNINTERRUPTIBLE);
 
 		tag = __blk_mq_get_tag(data, bt);
include/trace/events/block.h +59 -0

65 lines not on this page · raw message

base-commit: 6779b50faa562e6cca1aa6a4649a4d764c6c7e28

Signature · 1 line
2.51.0
maintainer Applied
#1 lore raw
Quoted 5 lines · Aaron Tomlin

In high-performance storage environments, particularly when utilising RAID controllers with shared tag sets (BLK_MQ_F_TAG_HCTX_SHARED), severe latency spikes can occur when fast devices (SSDs) are starved of hardware tags when sharing the same blk_mq_tag_set.

Quoted 8 lines

Currently, diagnosing this specific hardware queue contention is difficult. When a CPU thread exhausts the tag pool, blk_mq_get_tag() forces the current thread to block uninterruptible via io_schedule(). While this can be inferred via sched:sched_switch or dynamically traced by attaching a kprobe to blk_mq_mark_tag_wait(), there is no dedicated, out-of-the-box observability for this event.

[...]

Applied, thanks!

[1/1] blk-mq: add tracepoint block_rq_tag_wait

      commit: 9ece10778f8931630f86e802f94dc71115de0c8c

Best regards,

Signature · 1 line
Jens Axboe