Skip to contentSkip to conversation

tracing: Free tracer resources on instance creation failure

PATCH Superseded Baek Kyu, Kim 3 participants 4 messages Oct 25 – Oct 29 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing: Free tracer resources on instance creation failure

Creating a tracefs instance can allocate private tracer nodes, copied flags and option arrays before trace_array_create_dir() returns. If a later allocation fails, trace_array_create_systems() jumps to out_free_tr and frees the trace_array without releasing those private allocations. The failed instance is never registered, so normal removal cannot reclaim them.

Free the tracer nodes and option arrays when trace_array_create_dir() fails, then continue through the existing trace_array cleanup. Earlier failures still use out_free_tr because the tracer list is not yet initialized there.

---
Tested under ARM64 QEMU/KVM through tracefs instance mkdir with failslab targeting tracer flags, a later tracer node and option-array growth. The baseline retained one or four private allocations after the failed mkdir; the patched kernel retained none. Normal instance option read/write and removal also passed.

 kernel/trace/trace.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/trace/trace.c +6 -1
index 97a058623f94..ad68a39a1161 100644
@@ -8751,7 +8751,7 @@ trace_array_create_systems(const char *name, const char *systems,
 	if (trace_instance_dir) {
 		ret = trace_array_create_dir(tr);
 		if (ret)
-			goto out_free_tr;
+			goto out_free_tracers;
 	} else
 		__trace_early_add_events(tr);
 
@@ -8761,6 +8761,11 @@ trace_array_create_systems(const char *name, const char *systems,
 
 	return tr;
 
+ out_free_tracers:
+	free_tracers(tr);
+	for (int i = 0; i < tr->nr_topts; i++)
+		kfree(tr->topts[i].topts);
+	kfree(tr->topts);
  out_free_tr:
 	ftrace_free_ftrace_ops(tr);
 	free_trace_buffers(tr);
#1 lore raw

Hi,

Quoted 7 lines · Kim

Creating a tracefs instance can allocate private tracer nodes, copied flags and option arrays before trace_array_create_dir() returns. If a later allocation fails, trace_array_create_systems() jumps to out_free_tr and frees the trace_array without releasing those private allocations. The failed instance is never registered, so normal removal cannot reclaim them.

Quoted 6 lines

Free the tracer nodes and option arrays when trace_array_create_dir() fails, then continue through the existing trace_array cleanup. Earlier failures still use out_free_tr because the tracer list is not yet initialized there.

Please add the appropriate Fixes tag before the Signed-off-by:

Fixes 428add559b69 ("tracing: Have tracer option be instance specific")

Commit 428add559b69 introduced per-instance private tracer nodes (tr->tracers), flags, and option arrays, and called __update_tracer() from trace_array_create_dir(). When trace_array_create_dir() fails, these allocated resources were leaked because out_free_tr did not release them.

Quoted 13 lines

---
Tested under ARM64 QEMU/KVM through tracefs instance mkdir with failslab targeting tracer flags, a later tracer node and option-array growth. The baseline retained one or four private allocations after the failed mkdir; the patched kernel retained none. Normal instance option read/write and removal also passed.

 kernel/trace/trace.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/trace/trace.c +0 -0

[...]

kernel/trace/trace.c trace_array_create_systems(const char *name, const char *systems, · line 8767

Quoted 8 lines
kernel/trace/trace.c +5 -0
@@ -8761,6 +8761,11 @@ trace_array_create_systems(const char *name, const char *systems,
 	return tr;
 
+ out_free_tracers:
+	free_tracers(tr);
+	for (int i = 0; i < tr->nr_topts; i++)
+		kfree(tr->topts[i].topts);
+	kfree(tr->topts);

Two minor nits:

- Please declare int i; at the beginning of trace_array_create_systems() rather than inlining it inside for (int i = 0; ...), matching the prevailing style in trace.c (e.g. in __remove_instance()).

- The cleanup loop freeing tr->topts[i].topts and tr->topts exactly mirrors the logic in __remove_instance(). Consider refactoring this into a small helper function, e.g. free_tracer_options(struct trace_array *tr), shared by both trace_array_create_systems() and __remove_instance().

Thanks,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
maintainer
#2 lore raw
Quoted 11 lines · On Mon, 26 Oct 2026 10:53:40 +0900 Masami Hiramatsu (Google)…

kernel/trace/trace.c trace_array_create_systems(const char *name, const char *systems, · line 8768

Quoted 5 lines

+ out_free_tracers:

kernel/trace/trace.c +4 -0
+	free_tracers(tr);
+	for (int i = 0; i < tr->nr_topts; i++)
+		kfree(tr->topts[i].topts);
+	kfree(tr->topts);  

Two minor nits:

- Please declare int i; at the beginning of trace_array_create_systems() rather than inlining it inside for (int i = 0; ...), matching the prevailing style in trace.c (e.g. in __remove_instance()).

Actually, it's now OK to do this. It used to not be (which is why the old code doesn't do it), but I've been slowing moving to this style. And new code may do so too.

-- Steve

#3 lore raw

PATCH v2 1/1 tracing: Free tracer resources on instance creation failure

In reply to Baek Kyu, Kim

Creating a tracefs instance can allocate private tracer nodes, copied flags and option arrays before trace_array_create_dir() returns. If a later allocation fails, trace_array_create_systems() frees the trace_array without releasing those private allocations. The failed instance is never registered, so normal removal cannot reclaim them.

Free the tracer nodes and option arrays when trace_array_create_dir() fails, then continue through the existing trace_array cleanup. Share the option array cleanup with normal instance removal. Earlier failures still use out_free_tr because the tracer list is not yet initialized there.

Fixes 428add559b69 ("tracing: Have tracer option be instance specific")

---
Changes in v2:
- Add the Fixes tag requested by Masami Hiramatsu.
- Share option array cleanup between failed creation and normal removal.

 kernel/trace/trace.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
kernel/trace/trace.c +12 -5
index 97a058623f94..faa90323809c 100644
@@ -1456,6 +1456,13 @@ static void free_tracers(struct trace_array *tr)
 	}
 }
 
+static void free_tracer_options(struct trace_array *tr)
+{
+	for (int i = 0; i < tr->nr_topts; i++)
+		kfree(tr->topts[i].topts);
+	kfree(tr->topts);
+}
+
 /**
  * register_tracer - register a tracer with the ftrace system.
  * @type: the plugin for the tracer
@@ -8751,7 +8758,7 @@ trace_array_create_systems(const char *name, const char *systems,
 	if (trace_instance_dir) {
 		ret = trace_array_create_dir(tr);
 		if (ret)
-			goto out_free_tr;
+			goto out_free_tracers;
 	} else
 		__trace_early_add_events(tr);
 
@@ -8761,6 +8768,9 @@ trace_array_create_systems(const char *name, const char *systems,
 
 	return tr;
 
+ out_free_tracers:
+	free_tracers(tr);
+	free_tracer_options(tr);
  out_free_tr:
 	ftrace_free_ftrace_ops(tr);
 	free_trace_buffers(tr);
@@ -8914,10 +8924,7 @@ static int __remove_instance(struct trace_array *tr)
 	if (tr->flags & TRACE_ARRAY_FL_VMALLOC)
 		vfree((void *)tr->range_addr_start);
 
-	for (i = 0; i < tr->nr_topts; i++) {
-		kfree(tr->topts[i].topts);
-	}
-	kfree(tr->topts);
+	free_tracer_options(tr);
 
 	free_cpumask_var(tr->pipe_cpumask);
 	free_cpumask_var(tr->tracing_cpumask);