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

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