Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH v3] tracing/osnoise: validate and safely print sample runtime
@ 2026-12-04  5:04 Kyuwon Cho
  2026-12-04  5:23 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Kyuwon Cho @ 2026-12-04  5:04 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Masami Hiramatsu, Mathieu Desnoyers, Daniel Bristot de Oliveira,
	sashiko-reviews, linux-kernel, linux-trace-kernel

osnoise accepts a zero sample runtime.  run_osnoise() then records a
zero-duration sample, and trace_osnoise_print() divides by that runtime
while formatting the trace.

Reject a zero runtime and cap the period so its microsecond-to-nanosecond
conversion fits in ktime_t.  Use ktime_add_safe() when forming the
absolute sleep deadline because adding ktime_get() can still overflow at
the conversion limit.

Also drop any zero-duration sample that races with configuration and use
mul_u64_u64_div_u64() to scale the availability ratio without overflowing
its multiplication.

Fixes: bce29ac9ce0b ("trace: Add osnoise tracer")
Signed-off-by: Kyuwon Cho <kyuwon.cho@skku.edu>
---
Changes in v3:
- Form the absolute deadline with ktime_add_safe(), as identified by the
  Sashiko review, because ktime_get() plus the maximum period can overflow.
- Keep the availability subtraction unchanged: run_osnoise() rejects a
  negative interval and accumulates disjoint noise intervals within the
  measured runtime.
- Rebase onto trace/for-next.

v2: https://lore.kernel.org/all/179632406628.3855633.16468487680690267787@skku.edu/
v1: https://lore.kernel.org/all/179620722295.2187910.14090340832228644346@skku.edu/

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 | 13 ++++++++++---
 kernel/trace/trace_output.c  | 13 +++++++++----
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/kernel/trace/trace_osnoise.c b/kernel/trace/trace_osnoise.c
index fbba42286747..6eef4dc431e7 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);
@@ -1654,7 +1657,8 @@ static void osnoise_sleep(bool skip_period)
 		return;
 	}
 
-	wake_time = ktime_add_us(ktime_get(), interval);
+	wake_time = ktime_add_safe(ktime_get(),
+				   ns_to_ktime(interval * NSEC_PER_USEC));
 	__set_current_state(TASK_INTERRUPTIBLE);
 
 	while (schedule_hrtimeout(&wake_time, HRTIMER_MODE_ABS)) {
@@ -2648,11 +2652,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 +2668,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..adf03e8222a1 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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v3] tracing/osnoise: validate and safely print sample runtime
  2026-12-04  5:04 [PATCH v3] tracing/osnoise: validate and safely print sample runtime Kyuwon Cho
@ 2026-12-04  5:23 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04  5:23 UTC (permalink / raw)
  To: Kyuwon Cho; +Cc: linux-trace-kernel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] tracing/osnoise: unchecked underflow in trace_osnoise_print()
--

commit 06b53da4388551e81287d6f227d53cd537058525
Author: Kyuwon Cho <kyuwon.cho@skku.edu>

tracing/osnoise: validate and safely print sample runtime

This commit rejects zero osnoise sample runtimes, caps the osnoise period and
sleep deadline to avoid ktime_t overflow, drops zero-duration samples in
run_osnoise(), and updates trace_osnoise_print() to guard against zero
runtime and use mul_u64_u64_div_u64().

> diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c
> index 0850b3ad57075..adf03e8222a1b 100644
> --- a/kernel/trace/trace_output.c
> +++ b/kernel/trace/trace_output.c

[ ... ]

> @@ -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);
> +	}

[Severity: Medium]
Can net_runtime underflow here and cause a divide-overflow exception in
mul_u64_u64_div_u64()?

On x86_64, mul_u64_u64_div_u64() uses raw mulq and divq instructions with no
exception table entry (arch/x86/include/asm/div64.h).  When field->noise
exceeds field->runtime, net_runtime wraps to a large unsigned value and the
high 64 bits of net_runtime * 10000000 become 9999999.

For any field->runtime up to 9999999 us, including the default 1000000 us,
divq then traps with an unhandled divide-overflow exception and oopses the
kernel.  Previously, the 64-bit multiplication wrapped and do_div() could not
trap on quotient overflow, so this is a regression.

Although run_osnoise() checks for negative noise inside its loop, it captures
start and last_sample in two separate time_get() calls without checking
last_sample >= start:

kernel/trace/trace_osnoise.c:run_osnoise() {
    ...
	start = time_get();

	/*
	 * "previous" loop.
	 */
	last_int_count = set_int_safe_time(osn_var, &last_sample);
    ...
}

If trace_clock_local() steps backward between those calls (for example, when
tsc_refine_calibration_work() updates cyc2ns), last_sample < start.  The
first iteration then computes noise > total while passing the noise < 0
check, and can record a sample with field->noise > field->runtime.

Checking if (field->runtime > field->noise) instead of if (field->runtime)
would prevent both divide-by-zero and unsigned underflow.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179633331752.64245.15271121236897350387@skku.edu?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-04  5:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  5:04 [PATCH v3] tracing/osnoise: validate and safely print sample runtime Kyuwon Cho
2026-12-04  5:23 ` sashiko-bot

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