Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

fgraph: Keep track of when fgraph_ops are registered or not

Add a warning if unregister_ftrace_graph() is called without ever
registering it, or if register_ftrace_graph() is called twice. This can
detect errors when they happen and not later when there's a side effect:

Link: https://lore.kernel.org/all/20250617120830.24fbdd62@gandalf.local.home/

Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Link: https://lore.kernel.org/20250701194451.22e34724@gandalf.local.home
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

+13 -3
+13 -3
kernel/trace/fgraph.c
··· 1325 1325 int ret = 0; 1326 1326 int i = -1; 1327 1327 1328 + if (WARN_ONCE(gops->ops.flags & FTRACE_OPS_FL_GRAPH, 1329 + "function graph ops registered again")) 1330 + return -EBUSY; 1331 + 1328 1332 guard(mutex)(&ftrace_lock); 1329 1333 1330 1334 if (!fgraph_stack_cachep) { ··· 1405 1401 { 1406 1402 int command = 0; 1407 1403 1404 + if (WARN_ONCE(!(gops->ops.flags & FTRACE_OPS_FL_GRAPH), 1405 + "function graph ops unregistered without registering")) 1406 + return; 1407 + 1408 1408 guard(mutex)(&ftrace_lock); 1409 1409 1410 1410 if (unlikely(!ftrace_graph_active)) 1411 - return; 1411 + goto out; 1412 1412 1413 1413 if (unlikely(gops->idx < 0 || gops->idx >= FGRAPH_ARRAY_SIZE || 1414 1414 fgraph_array[gops->idx] != gops)) 1415 - return; 1415 + goto out; 1416 1416 1417 1417 if (fgraph_lru_release_index(gops->idx) < 0) 1418 - return; 1418 + goto out; 1419 1419 1420 1420 fgraph_array[gops->idx] = &fgraph_stub; 1421 1421 ··· 1442 1434 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); 1443 1435 } 1444 1436 gops->saved_func = NULL; 1437 + out: 1438 + gops->ops.flags &= ~FTRACE_OPS_FL_GRAPH; 1445 1439 }