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

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@skku.edu>
---
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@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 | 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


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

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

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] tracing/osnoise: osnoise_max_period still overflows ktime_t
  in osnoise_sleep()
- [Medium] tracing/osnoise: unchecked underflow in trace_osnoise_print()
  can cause #DE
--

commit 18cb5b7e9e9bcf7ac48000897ed2f20aad86f24c
Author: Kyuwon Cho <kyuwon.cho@skku.edu>

tracing/osnoise: validate and safely print sample runtime

This commit adds minimum and maximum bounds for the osnoise runtime and
period parameters, drops zero-duration samples in run_osnoise(), and
replaces the availability ratio division in trace_osnoise_print() with
mul_u64_u64_div_u64().

> diff --git a/kernel/trace/trace_osnoise.c b/kernel/trace/trace_osnoise.c
> index fbba422867474..7f8cbca7b9a7c 100644
> --- a/kernel/trace/trace_osnoise.c
> +++ b/kernel/trace/trace_osnoise.c
[ ... ]
> @@ -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,
>  };

[Severity: Medium]
Can osnoise_max_period still overflow ktime_t in osnoise_sleep()?

S64_MAX / NSEC_PER_USEC converts to S64_MAX - 807 ns.  In osnoise_sleep(),
ktime_add_us(ktime_get(), interval) performs an unsaturating signed addition,
so any period near osnoise_max_period wraps wake_time to a negative ktime_t.

schedule_hrtimeout(&wake_time, HRTIMER_MODE_ABS) treats a negative deadline
as already expired and returns immediately without sleeping.  When tracing
is off, osnoise_main() loops on osnoise_sleep(true) with no cond_resched(),
spinning the per-CPU osnoise kthreads and contending on interface_lock.

Using ktime_add_safe() in osnoise_sleep(), or passing a relative timeout with
HRTIMER_MODE_REL, would prevent the wrap.

> 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 an underflow in net_runtime trigger a #DE trap in mul_u64_u64_div_u64()?

On x86_64, mul_u64_add_u64_div_u64() uses raw mulq and divq with no exception
table entry.  If field->noise > field->runtime, net_runtime underflows near
U64_MAX and raises an unhandled #DE for any runtime up to 9999999 us
(including the default 1000000 us).

run_osnoise() reads start and last_sample in separate time_get() calls
without checking last_sample >= start.  If sched_clock() steps backwards
between them (for example in tsc_refine_calibration_work() ->
set_cyc2ns_scale()), noise exceeds total and records noise > runtime > 0.

Guarding the calculation with if (field->runtime > field->noise) avoids both
divide-by-zero and the #DE trap.

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

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

end of thread, other threads:[~2026-12-03 20:12 UTC | newest]

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

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