perf/ftrace: Fix WARNING in __unregister_ftrace_function
PATCH v2 1/1 perf/ftrace: Fix WARNING in __unregister_ftrace_function
perf_ftrace_function_unregister() unconditionally calls unregister_ftrace_function() without checking whether the ftrace_ops was ever successfully registered. This triggers a WARN_ON in __unregister_ftrace_function() when the ops doesn't have FTRACE_OPS_FL_ENABLED set.
This can happen during perf_event_alloc() error cleanup when perf_trace_destroy() is called via __free_event() on an event whose ftrace_ops registration failed or was already torn down by perf_try_init_event()'s err_destroy path.
The call path is:
perf_event_alloc() error cleanup
-> __free_event()
-> event->destroy() [tp_perf_event_destroy]
-> perf_trace_destroy()
-> perf_trace_event_close()
-> TRACE_REG_PERF_CLOSE
-> perf_ftrace_function_unregister()
-> unregister_ftrace_function()
-> __unregister_ftrace_function()
-> WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED))Fix this by checking FTRACE_OPS_FL_ENABLED before attempting to unregister. If the ops is not enabled, just free the filter and return success.
--- kernel/trace/trace_event_perf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/trace/trace_event_perf.c +5 -1
@@ -497,7 +497,11 @@ static int perf_ftrace_function_register(struct perf_event *event)static int perf_ftrace_function_unregister (struct perf_event *event) {struct ftrace_ops *ops = &event->ftrace_ops; -int ret =unregister_ftrace_function (ops); +int ret =0 ; + +if (ops->flags &FTRACE_OPS_FL_ENABLED ) + ret =unregister_ftrace_function (ops); +ftrace_free_filter (ops);return ret; }
Signature · 1 line
2.52.0
Quoted 23 lines · On Wed, 13 May 2026 16:19:16 -0400 Rik van Riel <[email protected]> wrote:
perf_ftrace_function_unregister()unconditionally callsunregister_ftrace_function()without checking whether theftrace_opswas ever successfully registered. This triggers aWARN_ONin__unregister_ftrace_function()when the ops doesn't haveFTRACE_OPS_FL_ENABLEDset.This can happen during
perf_event_alloc()error cleanup whenperf_trace_destroy()is called via__free_event()on an event whoseftrace_opsregistration failed or was already torn down by perf_try_init_event()'serr_destroypath.The call path is:
perf_event_alloc()error cleanup-> __free_event() -> event->destroy() [tp_perf_event_destroy] -> perf_trace_destroy() -> perf_trace_event_close() -> TRACE_REG_PERF_CLOSE -> perf_ftrace_function_unregister() -> unregister_ftrace_function() -> __unregister_ftrace_function() -> WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED))
Quoted 6 lines
Fix this by checking
FTRACE_OPS_FL_ENABLEDbefore attempting to unregister. If the ops is not enabled, just free the filter and return success.Assisted-by Claude:claude-opus-4.7 syzkallerSigned-off-by Rik van Riel <[email protected]>
Looks good to me.
Thanks,
Quoted 25 lines
--- kernel/trace/trace_event_perf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)kernel/trace/trace_event_perf.c +5
-1@@ -497,7 +497,11 @@ static int perf_ftrace_function_register(struct perf_event *event)static int perf_ftrace_function_unregister (struct perf_event *event) {struct ftrace_ops *ops = &event->ftrace_ops; -int ret =unregister_ftrace_function (ops); +int ret =0 ; + +if (ops->flags &FTRACE_OPS_FL_ENABLED ) + ret =unregister_ftrace_function (ops); +ftrace_free_filter (ops);return ret; }Signature · 1 line
2.52.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Steven Rostedt
maintainerRik,
Can you reply to Sashiko here?
The below comes from Sashiko: https://sashiko.dev/?list=org.kernel.vger.linux-trace-kernel#/patchset/20260513161916.04151502%40fangorn
Thanks,
-- Steve
Quoted 50 lines
commit
f90ac36f39b79a19c015522bb1caf7ef903caf02
Author: Rik van Riel <[email protected]>
perf/ftrace: Fix WARNING in__unregister_ftrace_function
This commit attempts to fix aWARN_ONtriggered duringperf_event_alloc()error cleanup by checkingFTRACE_OPS_FL_ENABLEDbefore unregistering the ftrace function. The warning occurs because theftrace_opsregistration failed or was torn down byperf_try_init_event()earlier, but theevent->destroycallback is executed again during the error path.Quoted 5 lines
kernel/trace/trace_event_perf.c +0
-0kernel/trace/trace_event_perf.c static int perf_ftrace_function_register(struct perf_event *event) · line 503
Quoted 10 lines
kernel/trace/trace_event_perf.c +5
-1@@ -495,7 +495,11 @@ static int perf_ftrace_function_register(struct perf_event *event)static int perf_ftrace_function_unregister (struct perf_event *event) {struct ftrace_ops *ops = &event->ftrace_ops; -int ret =unregister_ftrace_function (ops); +int ret =0 ; + +if (ops->flags &FTRACE_OPS_FL_ENABLED ) + ret =unregister_ftrace_function (ops); +ftrace_free_filter (ops);Does calling
ftrace_free_filter()unconditionally here mask a double-teardown regression while leaving the underlying double-free active?
Inperf_try_init_event(), if a PMUevent_init()succeeds but a subsequent capability check fails, it explicitly callsevent->destroy(event)to roll back:kernel/events/core.c:perf_try_init_event() { ... if (ret && event->destroy) event->destroy(event); ... }However, it does not set
event->destroyto NULL.
When the event is later freed in error paths, the staleevent->destroycallback executes a second time.
Ifftrace_free_filter()is called twice, won't it result in a direct double-free ofops->func_hash->filter_hash, sinceftrace_free_filter()frees the hash but does not nullify the pointers?
Additionally, for regular tracepoint events, wouldn't the second teardown executeperf_trace_event_unreg()again?
This could double-decrementtotal_ref_count, potentially freeing the sharedperf_trace_bufwhile other events are active, and trigger an unbalancedmodule_put()leading to module refcount underflows.
Should the root cause be addressed by settingevent->destroy= NULL inperf_try_init_event()immediately after invoking it?Quoted 2 lines
kernel/trace/trace_event_perf.c +0
-0return ret; }
Quoted 47 lines · Steven Rostedt
Rik,
Can you reply to Sashiko here?
The below comes from Sashiko:
https://sashiko.dev/?list=org.kernel.vger.linux-trace-kernel#/patchset/20260513161916.04151502%40fangornThanks,
-- Steve
Quoted 34 lines
commit
f90ac36f39b79a19c015522bb1caf7ef903caf02
Author: Rik van Riel <[email protected]>
perf/ftrace: Fix WARNING in__unregister_ftrace_functionThis commit attempts to fix aWARN_ONtriggered duringperf_event_alloc()error
cleanup by checkingFTRACE_OPS_FL_ENABLEDbefore unregistering the ftrace
function. The warning occurs because theftrace_opsregistration failed or was
torn down byperf_try_init_event()earlier, but theevent->destroycallback is
executed again during the error path.Quoted 17 lines
kernel/trace/trace_event_perf.c +0
-0@@ -495,7 +495,11 @@ static int perf_ftrace_function_register(struct perf_event *event)static int
perf_ftrace_function_unregister(struct perf_event *event)
{
structftrace_ops*ops =&event->ftrace_ops;kernel/trace/trace_event_perf.c +5
-1-int ret =unregister_ftrace_function (ops); +int ret =0 ; + +if (ops->flags &FTRACE_OPS_FL_ENABLED ) + ret =unregister_ftrace_function (ops); +Quoted 1 line
ftrace_free_filter(ops);Does calling
ftrace_free_filter()unconditionally here mask a double-teardown
regression while leaving the underlying double-free active?
I don't see how calling ftrace_free_filter() twice would call issues, given that it sets the ->*_hash values to EMPTY_HASH:
void ftrace_free_filter(struct ftrace_ops *ops)
{
ftrace_ops_init(ops);
if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
return;
free_ftrace_hash(ops->func_hash->filter_hash);
free_ftrace_hash(ops->func_hash->notrace_hash);
ops->func_hash->filter_hash = EMPTY_HASH;
ops->func_hash->notrace_hash = EMPTY_HASH;
}void free_ftrace_hash(struct ftrace_hash *hash)
{
if (!hash || hash == EMPTY_HASH)
return;..
Quoted 10 lines
Quoted 10 lines
In
perf_try_init_event(), if a PMUevent_init()succeeds but a subsequent
capability check fails, it explicitly callsevent->destroy(event)to roll back:
kernel/events/core.c:perf_try_init_event() {
...if (ret && event->destroy) event->destroy(event);...
}
The error handling there all seems to "goto err_destroy"
err_destroy:
if (event->destroy) {
event->destroy(event);
event->destroy = NULL;
}Quoted 1 line
Quoted 1 line
However, it does not set
event->destroyto NULL.
... but it does?
I am not sure what code Sashiko is looking at,
but it does not look like the code I just pulled.
Is there a different tree I should be looking at
than upstream Linus?
Signature · 1 line
All Rights Reversed.
Masami Hiramatsu (Google)
maintainerQuoted 64 lines · On Fri, 22 May 2026 16:39:41 -0400 Rik van Riel <[email protected]> wrote:
Quoted 47 lines · Steven Rostedt
Rik,
Can you reply to Sashiko here?
The below comes from Sashiko:
https://sashiko.dev/?list=org.kernel.vger.linux-trace-kernel#/patchset/20260513161916.04151502%40fangornThanks,
-- Steve
Quoted 34 lines
commit
f90ac36f39b79a19c015522bb1caf7ef903caf02
Author: Rik van Riel <[email protected]>
perf/ftrace: Fix WARNING in__unregister_ftrace_functionThis commit attempts to fix aWARN_ONtriggered duringperf_event_alloc()error
cleanup by checkingFTRACE_OPS_FL_ENABLEDbefore unregistering the ftrace
function. The warning occurs because theftrace_opsregistration failed or was
torn down byperf_try_init_event()earlier, but theevent->destroycallback is
executed again during the error path.Quoted 17 lines
kernel/trace/trace_event_perf.c +0
-0@@ -495,7 +495,11 @@ static int perf_ftrace_function_register(struct perf_event *event)static int
perf_ftrace_function_unregister(struct perf_event *event)
{
structftrace_ops*ops =&event->ftrace_ops;kernel/trace/trace_event_perf.c +5
-1-int ret =unregister_ftrace_function (ops); +int ret =0 ; + +if (ops->flags &FTRACE_OPS_FL_ENABLED ) + ret =unregister_ftrace_function (ops); +Quoted 1 line
ftrace_free_filter(ops);Does calling
ftrace_free_filter()unconditionally here mask a double-teardown
regression while leaving the underlying double-free active?I don't see how calling
ftrace_free_filter()twice would call issues, given that it sets the ->*_hash values toEMPTY_HASH:void
ftrace_free_filter(struct ftrace_ops *ops){ ftrace_ops_init(ops); if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED)) return; free_ftrace_hash(ops->func_hash->filter_hash); free_ftrace_hash(ops->func_hash->notrace_hash); ops->func_hash->filter_hash = EMPTY_HASH; ops->func_hash->notrace_hash = EMPTY_HASH; }
Quoted 6 lines
void
free_ftrace_hash(struct ftrace_hash *hash){ if (!hash || hash == EMPTY_HASH) return;..
Yeah, confirmed.
Quoted 21 lines
Quoted 11 lines
Quoted 11 lines
In
perf_try_init_event(), if a PMUevent_init()succeeds but a subsequent
capability check fails, it explicitly callsevent->destroy(event)to roll back:
kernel/events/core.c:perf_try_init_event() {
...if (ret && event->destroy) event->destroy(event);...
}The error handling there all seems to "goto
err_destroy"
err_destroy:if (event->destroy) { event->destroy(event); event->destroy = NULL; }
Quoted 6 lines
Quoted 1 line
Quoted 1 line
However, it does not set
event->destroyto NULL.... but it does?
I am not sure what code Sashiko is looking at,
but it does not look like the code I just pulled.
Indeed.
Quoted 3 lines
Is there a different tree I should be looking at
than upstream Linus?
You can see the baseline info if you expand the collapsed triangle. Anyway, it said:
linux-trace/HEAD (70575e77839f4c5337ce2653b39b86bb365a870e)
So that is linux-trace/master.
commit 70575e77839f4c5337ce2653b39b86bb365a870e (linux-trace/master)
Merge: 7bc6e90d7aa4 a43ae8057cc1
Author: Linus Torvalds <[email protected]>
Date: Fri Sep 30 09:41:34 2022 -0700
Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
Hmm, this is too old. And linux-trace/master is not used anymore.
Reported to Sashiko.
https://github.com/sashiko-dev/sashiko/issues/218
Thank you,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Steven Rostedt
maintainerQuoted 34 lines · On Mon, 25 May 2026 14:39:47 +0900 Masami Hiramatsu (Google)…
Quoted 9 lines
The error handling there all seems to "goto
err_destroy"
err_destroy:if (event->destroy) { event->destroy(event); event->destroy = NULL; }Quoted 6 lines
Quoted 1 line
Quoted 1 line
However, it does not set
event->destroyto NULL.... but it does?
I am not sure what code Sashiko is looking at,
but it does not look like the code I just pulled.Indeed.
Quoted 3 lines
Is there a different tree I should be looking at
than upstream Linus?You can see the baseline info if you expand the collapsed triangle. Anyway, it said:
linux-trace/HEAD (
70575e77839f4c5337ce2653b39b86bb365a870e)So that is linux-trace/master.
commit
70575e77839f4c5337ce2653b39b86bb365a870e(linux-trace/master)
Merge:7bc6e90d7aa4a43ae8057cc1
Author: Linus Torvalds <[email protected]>
Date: Fri Sep 30 09:41:34 2022 -0700
Quoted 4 lines
Merge tag '
for_linus' ofgit://git.kernel.org/pub/scm/linux/kernel/git/mst/vhostHmm, this is too old. And linux-trace/master is not used anymore.
Hmm, I probably should delete that branch.
Thanks,
-- Steve
Steven Rostedt
maintainerkernel/trace/trace_event_perf.c static int perf_ftrace_function_register(struct perf_event *event) · line 501
Quoted 11 lines · On Wed, 13 May 2026 16:19:16 -0400 Rik van Riel <[email protected]> wrote:
kernel/trace/trace_event_perf.c +2
-1@@ -497,7 +497,11 @@ static int perf_ftrace_function_register(struct perf_event *event)static int perf_ftrace_function_unregister (struct perf_event *event) {struct ftrace_ops *ops = &event->ftrace_ops; -int ret =unregister_ftrace_function (ops); +int ret =0 ; +
Because this is different than unregister_ftrace_function() where it will not fail if the ops is not registered, it deserves a comment. Something like:
/*
* Perf will call this unconditionally even if the ops is not
* enabled. The unregister_ftrace_function() will warn if called
* when not enabled. Just bypass the unregistering if ops isn't
* enabled here.
*/
Thanks,
-- Steve
Quoted 6 lines
kernel/trace/trace_event_perf.c +3
-0+if (ops->flags &FTRACE_OPS_FL_ENABLED ) + ret =unregister_ftrace_function (ops); +ftrace_free_filter (ops);return ret; }