[PATCH v2] tracing/osnoise: validate and safely print sample runtime

From: Kyuwon Cho

Date: Thu Dec 03 2026 - 14:51:57 EST


The osnoise runtime parameter accepts zero, allowing
trace_osnoise_print() to divide by a zero sample runtime. A
sub-microsecond sample produced while stopping the tracer can also be
truncated to zero.

The period is unbounded as well, so converting it to nanoseconds can
overflow. In addition, do_div() truncates its divisor to 32 bits, making
some nonzero runtimes act as zero while printing a sample.

Require a nonzero runtime, bound the period before its nanosecond
conversion, and discard zero-duration samples. Use
mul_u64_u64_div_u64() for the availability ratio and keep the trace
printer guarded against a zero record.

Fixes: bce29ac9ce0b ("trace: Add osnoise tracer")
Signed-off-by: Kyuwon Cho <kyuwon.cho@xxxxxxxx>
---
Changes in v2:
- Bound the period before its nanosecond conversion and replace the
truncated do_div() divisor, as suggested by the Sashiko review.
- Drop zero-duration samples produced while stopping a tracer and guard
trace output against an existing zero record.

v1: https://lore.kernel.org/all/179620722295.2187910.14090340832228644346@xxxxxxxx/

The issue was found by an LLM-guided test generator that we are
developing as a research prototype.

A reproducer is available on request.

kernel/trace/trace_osnoise.c | 10 ++++++++--
kernel/trace/trace_output.c | 13 +++++++++----
2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/kernel/trace/trace_osnoise.c b/kernel/trace/trace_osnoise.c
index fbba42286747..7f8cbca7b9a7 100644
--- a/kernel/trace/trace_osnoise.c
+++ b/kernel/trace/trace_osnoise.c
@@ -17,6 +17,7 @@
*/

#include <linux/kthread.h>
+#include <linux/limits.h>
#include <linux/tracefs.h>
#include <linux/uaccess.h>
#include <linux/cpumask.h>
@@ -1608,6 +1609,8 @@ static int run_osnoise(void)
s.runtime = time_to_us(total);
s.max_sample = time_to_us(max_noise);
s.hw_count = hw_count;
+ if (!s.runtime)
+ return 0;

/* Save interference stats info */
diff_osn_sample_stats(osn_var, &s);
@@ -2648,11 +2651,14 @@ static int timerlat_fd_release(struct inode *inode, struct file *file)
/*
* osnoise/runtime_us: cannot be greater than the period.
*/
+static u64 osnoise_min_runtime = 1;
+static u64 osnoise_max_period = S64_MAX / NSEC_PER_USEC;
+
static struct trace_min_max_param osnoise_runtime = {
.lock = &interface_lock,
.val = &osnoise_data.sample_runtime,
.max = &osnoise_data.sample_period,
- .min = NULL,
+ .min = &osnoise_min_runtime,
};

/*
@@ -2661,7 +2667,7 @@ static struct trace_min_max_param osnoise_runtime = {
static struct trace_min_max_param osnoise_period = {
.lock = &interface_lock,
.val = &osnoise_data.sample_period,
- .max = NULL,
+ .max = &osnoise_max_period,
.min = &osnoise_data.sample_runtime,
};

diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c
index 0850b3ad5707..afc89f235a30 100644
--- a/kernel/trace/trace_output.c
+++ b/kernel/trace/trace_output.c
@@ -10,6 +10,7 @@
#include <linux/mutex.h>
#include <linux/ftrace.h>
#include <linux/kprobes.h>
+#include <linux/math64.h>
#include <linux/sched/clock.h>
#include <linux/sched/mm.h>
#include <linux/idr.h>
@@ -1642,10 +1643,14 @@ trace_osnoise_print(struct trace_iterator *iter, int flags,
/*
* compute the available % of cpu time.
*/
- net_runtime = field->runtime - field->noise;
- ratio = net_runtime * 10000000;
- do_div(ratio, field->runtime);
- ratio_dec = do_div(ratio, 100000);
+ ratio = 0;
+ ratio_dec = 0;
+ if (field->runtime) {
+ net_runtime = field->runtime - field->noise;
+ ratio = mul_u64_u64_div_u64(net_runtime, 10000000,
+ field->runtime);
+ ratio_dec = do_div(ratio, 100000);
+ }

trace_seq_printf(s, "%llu %10llu %3llu.%05llu %7llu",
field->runtime,
--
2.43.0