Linux Trace Kernel
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Kyuwon Cho" <kyuwon.cho@skku.edu>
Cc: linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH v2] tracing/osnoise: validate and safely print sample runtime
Date: Thu, 03 Dec 2026 20:12:32 +0000	[thread overview]
Message-ID: <sashiko-outbox-248602@kernel.org> (raw)
In-Reply-To: <179632406628.3855633.16468487680690267787@skku.edu>

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

      reply	other threads:[~2026-12-03 20:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-248602@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kyuwon.cho@skku.edu \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox