Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH] samples/ftrace: reject zero ftrace-ops call count
@ 2026-06-09  0:44 Samuel Moelius
  2026-06-09  1:03 ` Steven Rostedt
  0 siblings, 1 reply; 4+ messages in thread
From: Samuel Moelius @ 2026-06-09  0:44 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Samuel Moelius, Masami Hiramatsu, Mark Rutland,
	open list:FUNCTION HOOKS (FTRACE),
	open list:FUNCTION HOOKS (FTRACE)

The ftrace-ops sample exposes nr_function_calls as a module parameter
and uses it as the divisor when printing the measured time per call.
Loading the module with nr_function_calls=0 skips the benchmark loop and
then divides the elapsed time by zero, crashing the kernel during sample
module initialization.

Reject a zero call count before registering any ftrace ops.

Assisted-by: Codex:gpt-5.5-cyber-preview
Signed-off-by: Samuel Moelius <sam.moelius@trailofbits.com>
---
 samples/ftrace/ftrace-ops.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/samples/ftrace/ftrace-ops.c b/samples/ftrace/ftrace-ops.c
index 68d6685c80bd..d5adaa61484f 100644
--- a/samples/ftrace/ftrace-ops.c
+++ b/samples/ftrace/ftrace-ops.c
@@ -190,6 +190,11 @@ static int __init ftrace_ops_sample_init(void)
 		tracer_irrelevant = ops_func_count;
 	}
 
+	if (!nr_function_calls) {
+		pr_err("nr_function_calls must be non-zero\n");
+		return -EINVAL;
+	}
+
 	pr_info("registering:\n"
 		"  relevant ops: %u\n"
 		"    tracee: %ps\n"
-- 
2.43.0


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

* Re: [PATCH] samples/ftrace: reject zero ftrace-ops call count
  2026-06-09  0:44 [PATCH] samples/ftrace: reject zero ftrace-ops call count Samuel Moelius
@ 2026-06-09  1:03 ` Steven Rostedt
  2026-06-09 11:26   ` Samuel Moelius
  0 siblings, 1 reply; 4+ messages in thread
From: Steven Rostedt @ 2026-06-09  1:03 UTC (permalink / raw)
  To: Samuel Moelius
  Cc: Masami Hiramatsu, Mark Rutland, open list:FUNCTION HOOKS (FTRACE),
	open list:FUNCTION HOOKS (FTRACE)

On Tue,  9 Jun 2026 00:44:23 +0000
Samuel Moelius <sam.moelius@trailofbits.com> wrote:

> The ftrace-ops sample exposes nr_function_calls as a module parameter
> and uses it as the divisor when printing the measured time per call.
> Loading the module with nr_function_calls=0 skips the benchmark loop and
> then divides the elapsed time by zero, crashing the kernel during sample
> module initialization.

This change is rather pointless, but whatever.

> 
> Reject a zero call count before registering any ftrace ops.
> 
> Assisted-by: Codex:gpt-5.5-cyber-preview
> Signed-off-by: Samuel Moelius <sam.moelius@trailofbits.com>
> ---
>  samples/ftrace/ftrace-ops.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/samples/ftrace/ftrace-ops.c b/samples/ftrace/ftrace-ops.c
> index 68d6685c80bd..d5adaa61484f 100644
> --- a/samples/ftrace/ftrace-ops.c
> +++ b/samples/ftrace/ftrace-ops.c
> @@ -190,6 +190,11 @@ static int __init ftrace_ops_sample_init(void)
>  		tracer_irrelevant = ops_func_count;
>  	}
>  
> +	if (!nr_function_calls) {
> +		pr_err("nr_function_calls must be non-zero\n");
> +		return -EINVAL;

No need to print that the admin did something stupid.

> +	}
> +
>  	pr_info("registering:\n"
>  		"  relevant ops: %u\n"
>  		"    tracee: %ps\n"

In fact, I would just change the output to be:

        pr_info("Attempted %u calls to %ps in %lluns (%lluns / call)\n",
                nr_function_calls, tracee_relevant,
                period, nr_function_calls ? div_u64(period, nr_function_calls) : -1LL);

and have garbage in, garbage out.

-- Steve




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

* Re: [PATCH] samples/ftrace: reject zero ftrace-ops call count
  2026-06-09  1:03 ` Steven Rostedt
@ 2026-06-09 11:26   ` Samuel Moelius
  2026-06-11  0:03     ` Steven Rostedt
  0 siblings, 1 reply; 4+ messages in thread
From: Samuel Moelius @ 2026-06-09 11:26 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Masami Hiramatsu, Mark Rutland, open list:FUNCTION HOOKS (FTRACE),
	open list:FUNCTION HOOKS (FTRACE)

On Mon, Jun 8, 2026 at 9:03 PM Steven Rostedt <rostedt@goodmis.org> wrote:
>
> On Tue,  9 Jun 2026 00:44:23 +0000
> Samuel Moelius <sam.moelius@trailofbits.com> wrote:
>
> > The ftrace-ops sample exposes nr_function_calls as a module parameter
> > and uses it as the divisor when printing the measured time per call.
> > Loading the module with nr_function_calls=0 skips the benchmark loop and
> > then divides the elapsed time by zero, crashing the kernel during sample
> > module initialization.
>
> This change is rather pointless, but whatever.
>
> >
> > Reject a zero call count before registering any ftrace ops.
> >
> > Assisted-by: Codex:gpt-5.5-cyber-preview
> > Signed-off-by: Samuel Moelius <sam.moelius@trailofbits.com>
> > ---
> >  samples/ftrace/ftrace-ops.c | 5 +++++
> >  1 file changed, 5 insertions(+)
> >
> > diff --git a/samples/ftrace/ftrace-ops.c b/samples/ftrace/ftrace-ops.c
> > index 68d6685c80bd..d5adaa61484f 100644
> > --- a/samples/ftrace/ftrace-ops.c
> > +++ b/samples/ftrace/ftrace-ops.c
> > @@ -190,6 +190,11 @@ static int __init ftrace_ops_sample_init(void)
> >               tracer_irrelevant = ops_func_count;
> >       }
> >
> > +     if (!nr_function_calls) {
> > +             pr_err("nr_function_calls must be non-zero\n");
> > +             return -EINVAL;
>
> No need to print that the admin did something stupid.
>
> > +     }
> > +
> >       pr_info("registering:\n"
> >               "  relevant ops: %u\n"
> >               "    tracee: %ps\n"
>
> In fact, I would just change the output to be:
>
>         pr_info("Attempted %u calls to %ps in %lluns (%lluns / call)\n",
>                 nr_function_calls, tracee_relevant,
>                 period, nr_function_calls ? div_u64(period, nr_function_calls) : -1LL);
>
> and have garbage in, garbage out.

Is it okay to keep the same subject line or should I change it?

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

* Re: [PATCH] samples/ftrace: reject zero ftrace-ops call count
  2026-06-09 11:26   ` Samuel Moelius
@ 2026-06-11  0:03     ` Steven Rostedt
  0 siblings, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2026-06-11  0:03 UTC (permalink / raw)
  To: Samuel Moelius
  Cc: Masami Hiramatsu, Mark Rutland, open list:FUNCTION HOOKS (FTRACE),
	open list:FUNCTION HOOKS (FTRACE)

On Tue, 9 Jun 2026 07:26:27 -0400
Samuel Moelius <sam.moelius@trailofbits.com> wrote:

> Is it okay to keep the same subject line or should I change it?

Yeah, and also note that the tracing subsystem uses capital letters:

  samples/ftrace: Reject zero ftrace-ops call count

But you can change it to:

  samples/ftrace: Prevent division by zero when nr_function_calls is zero

-- Steve

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

end of thread, other threads:[~2026-06-11  0:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-09  0:44 [PATCH] samples/ftrace: reject zero ftrace-ops call count Samuel Moelius
2026-06-09  1:03 ` Steven Rostedt
2026-06-09 11:26   ` Samuel Moelius
2026-06-11  0:03     ` Steven Rostedt

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