Linux block layer
 help / color / mirror / Atom feed
* [PATCH] blktrace: propagate tracepoint registration errors instead of warning
@ 2026-12-03 19:40 Björn Bärtschi
  0 siblings, 0 replies; only message in thread
From: Björn Bärtschi @ 2026-12-03 19:40 UTC (permalink / raw)
  To: Jens Axboe, linux-block
  Cc: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
	Arnaldo Carvalho de Melo, Chaitanya Kulkarni, linux-trace-kernel,
	linux-kernel, syzbot+c54ded83396afee31eb1,
	syzbot+8672dcb9d10011c0a160

blk_register_tracepoints() registers 18 tracepoint probes and only does
WARN_ON(ret) when a registration fails. The tracepoint core already warns
about every failure other than -ENOMEM, so the WARN_ON() in blktrace can
only trigger on an allocation failure, which is a legitimate runtime
error rather than a kernel bug. syzbot hits it with fault injection:

  WARNING: kernel/trace/blktrace.c:1330 at blk_register_tracepoints+0x33c/0x440
  Call Trace:
   get_probe_ref kernel/trace/blktrace.c:493 [inline]
   blk_trace_setup_queue+0x269/0x300 kernel/trace/blktrace.c:1897
   sysfs_blk_trace_attr_store+0x6e2/0xa20 kernel/trace/blktrace.c:2092

Besides the warning, the failure is ignored: the setup reports success
while some of the probes are missing, and when tracing is disabled again,
blk_unregister_tracepoints() also tries to remove the probes that were
never added, which triggers a second warning in the tracepoint core:

  WARNING: kernel/tracepoint.c:369 at tracepoint_probe_unregister+0x85f/0xcd0

Unwind the already registered probes when a registration fails and return
the error. Make get_probe_ref() propagate it and drop the reference it
took, and take the probe reference before the trace is published:
in blk_trace_setup_prepare(), whose error path already frees the
blk_trace, and in blk_trace_setup_queue(). The blktrace setup then fails
with -ENOMEM instead of silently running with missing tracepoints.

Tested in QEMU on a loop device with fault injection (fail-nth with
failslab/fail_page_alloc ignore-gfp-wait=N) on the sysfs, BLKTRACESETUP
and BLKTRACESETUP2 paths: before the change both warnings trigger and
the setup succeeds with missing probes; after it, the setup fails with
-ENOMEM, no warnings are printed, and the blk tracer still records
events afterwards. With two loop devices, failed setups on the second
one leave tracing on the first intact, and the probe reference count
returns to zero afterwards. syzbot's own C reproducers (/dev/sg,
BLKTRACESETUP2) were not run.

Triage, reproduction, the patch and this changelog were done with an AI
coding assistant (Claude Opus 5.5). I reviewed the change and re-ran the
tests above.

Fixes: 5f3ea37c7716 ("blktrace: port to tracepoints")
Reported-by: syzbot+c54ded83396afee31eb1@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=c54ded83396afee31eb1
Reported-by: syzbot+8672dcb9d10011c0a160@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=8672dcb9d10011c0a160
Signed-off-by: Björn Bärtschi <linuxsatellite@gmail.com>
---
A similar change was proposed in 2023 [1] and stalled on the concern that
it adds a lot of code. Since then it turned out that ignoring the error
is not only a spurious warning: the setup succeeds with probes missing,
and disabling the trace afterwards trips a second warning in the
tracepoint core. The unwind follows tracing_sched_register() in
kernel/trace/trace_sched_switch.c; taking the probe reference in
blk_trace_setup_prepare() keeps the three ioctl callers unchanged.

This touches a context line next to Johannes' ioprio series [2]
(bt->flags in blk_trace_setup_finalize()); trivial to rebase either way.

[1] https://lore.kernel.org/all/20230328094605.480557-1-zhengyejian1@huawei.com/
[2] https://lore.kernel.org/all/20261203092748.1960101-1-johannes.thumshirn@wdc.com/

 kernel/trace/blktrace.c | 119 +++++++++++++++++++++++++++++++---------
 1 file changed, 94 insertions(+), 25 deletions(-)

diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c
index 8cd2520b4c99..76af7fd67074 100644
--- a/kernel/trace/blktrace.c
+++ b/kernel/trace/blktrace.c
@@ -60,7 +60,7 @@ static struct tracer_flags blk_tracer_flags = {
 static DEFINE_MUTEX(blk_probe_mutex);
 static int blk_probes_ref;
 
-static void blk_register_tracepoints(void);
+static int blk_register_tracepoints(void);
 static void blk_unregister_tracepoints(void);
 
 static void record_blktrace_event(struct blk_io_trace *t, pid_t pid, int cpu,
@@ -485,12 +485,18 @@ static void blk_trace_free(struct request_queue *q, struct blk_trace *bt)
 	kfree(bt);
 }
 
-static void get_probe_ref(void)
+static int get_probe_ref(void)
 {
+	int ret = 0;
+
 	mutex_lock(&blk_probe_mutex);
-	if (++blk_probes_ref == 1)
-		blk_register_tracepoints();
+	if (++blk_probes_ref == 1) {
+		ret = blk_register_tracepoints();
+		if (ret)
+			blk_probes_ref--;
+	}
 	mutex_unlock(&blk_probe_mutex);
+	return ret;
 }
 
 static void put_probe_ref(void)
@@ -719,6 +725,10 @@ static struct blk_trace *blk_trace_setup_prepare(struct request_queue *q,
 
 	blk_trace_setup_lba(bt, bdev);
 
+	ret = get_probe_ref();
+	if (ret)
+		goto err;
+
 	return bt;
 
 err:
@@ -756,7 +766,6 @@ static void blk_trace_setup_finalize(struct request_queue *q,
 	bt->trace_state = Blktrace_setup;
 
 	rcu_assign_pointer(q->blk_trace, bt);
-	get_probe_ref();
 }
 
 int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
@@ -1324,49 +1333,106 @@ void blk_add_driver_data(struct request *rq, void *data, size_t len)
 }
 EXPORT_SYMBOL_GPL(blk_add_driver_data);
 
-static void blk_register_tracepoints(void)
+static int blk_register_tracepoints(void)
 {
 	int ret;
 
 	ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
-	WARN_ON(ret);
+	if (ret)
+		return ret;
 	ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_block_rq_insert;
 	ret = register_trace_block_rq_merge(blk_add_trace_rq_merge, NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_block_rq_issue;
 	ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_block_rq_merge;
 	ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_block_rq_requeue;
 	ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_block_rq_complete;
 	ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_block_bio_complete;
 	ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_block_bio_backmerge;
 	ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_block_bio_frontmerge;
 	ret = register_trace_block_getrq(blk_add_trace_getrq, NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_block_bio_queue;
 	ret = register_trace_blk_zone_append_update_request_bio(
 		blk_add_trace_zone_update_request, NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_block_getrq;
 	ret = register_trace_disk_zone_wplug_add_bio(blk_add_trace_zone_plug,
 						     NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_blk_zone_append_update_request_bio;
 	ret = register_trace_blk_zone_wplug_bio(blk_add_trace_zone_unplug,
 						NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_disk_zone_wplug_add_bio;
 	ret = register_trace_block_plug(blk_add_trace_plug, NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_blk_zone_wplug_bio;
 	ret = register_trace_block_unplug(blk_add_trace_unplug, NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_block_plug;
 	ret = register_trace_block_split(blk_add_trace_split, NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_block_unplug;
 	ret = register_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_block_split;
 	ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
-	WARN_ON(ret);
+	if (ret)
+		goto unreg_block_bio_remap;
+	return 0;
+
+unreg_block_bio_remap:
+	unregister_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
+unreg_block_split:
+	unregister_trace_block_split(blk_add_trace_split, NULL);
+unreg_block_unplug:
+	unregister_trace_block_unplug(blk_add_trace_unplug, NULL);
+unreg_block_plug:
+	unregister_trace_block_plug(blk_add_trace_plug, NULL);
+unreg_blk_zone_wplug_bio:
+	unregister_trace_blk_zone_wplug_bio(blk_add_trace_zone_unplug, NULL);
+unreg_disk_zone_wplug_add_bio:
+	unregister_trace_disk_zone_wplug_add_bio(blk_add_trace_zone_plug, NULL);
+unreg_blk_zone_append_update_request_bio:
+	unregister_trace_blk_zone_append_update_request_bio(
+		blk_add_trace_zone_update_request, NULL);
+unreg_block_getrq:
+	unregister_trace_block_getrq(blk_add_trace_getrq, NULL);
+unreg_block_bio_queue:
+	unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
+unreg_block_bio_frontmerge:
+	unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
+unreg_block_bio_backmerge:
+	unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
+unreg_block_bio_complete:
+	unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
+unreg_block_rq_complete:
+	unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
+unreg_block_rq_requeue:
+	unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
+unreg_block_rq_merge:
+	unregister_trace_block_rq_merge(blk_add_trace_rq_merge, NULL);
+unreg_block_rq_issue:
+	unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
+unreg_block_rq_insert:
+	unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
+	tracepoint_synchronize_unregister();
+	return ret;
 }
 
 static void blk_unregister_tracepoints(void)
@@ -1916,8 +1982,11 @@ static int blk_trace_setup_queue(struct request_queue *q,
 
 	blk_trace_setup_lba(bt, bdev);
 
+	ret = get_probe_ref();
+	if (ret)
+		goto free_bt;
+
 	rcu_assign_pointer(q->blk_trace, bt);
-	get_probe_ref();
 	return 0;
 
 free_bt:
-- 
2.43.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-03 19:40 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 19:40 [PATCH] blktrace: propagate tracepoint registration errors instead of warning Björn Bärtschi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox