Skip to contentSkip to conversation

libtracecmd: fix memory leak on partial reverse iteration

PATCH In review Felix Moessbauer 2 participants 6 messages Nov 21, 2025 – Nov 25, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libtracecmd: fix memory leak on partial reverse iteration

When calling tracecmd_iterate_events_reverse with a callback that does not always return 0, the trace is only partially iterated. By that, the non-iterated records are leaked, resulting in the error:

1 pages still allocated on cpu <cpu>

We fix this by always iterating the remaining events on all selected CPUs. In the full iteration case, this stops on the first record as this is already zero. In the partial iteration case, all remaining records are freed, which is - by construction of the records list - at max a page size.

---
Note, that this bug has been reported in [1].

[1] https://lore.kernel.org/linux-trace-devel/[email protected]/

Best regards,
Felix Moessbauer
Siemens AG

 lib/trace-cmd/trace-input.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
lib/trace-cmd/trace-input.c +20 -0
index f2471c92..afdbd2aa 100644
@@ -2995,6 +2995,25 @@ static struct tep_record *next_last_event(struct tracecmd_input *handle,
 	return record;
 }
 
+static void free_last_events(struct tracecmd_input *handle,
+			     struct tep_record **last_records,
+			     cpu_set_t *cpu_set, int cpu_size,
+			     int cpus)
+{
+	struct tep_record *record;
+	int cpu;
+
+	for (cpu = 0; cpu < cpus; cpu++) {
+		if (cpus && !CPU_ISSET_S(cpu, cpu_size, cpu_set))
+			continue;
+
+		do {
+			record = next_last_event(handle, last_records, cpu);
+			tracecmd_free_record(record);
+		} while (record);
+	}
+}
+
 /**
  * tracecmd_iterate_events_reverse - iterate events over a given handle backwards
  * @handle: The handle to iterate over
@@ -3057,6 +3076,7 @@ int tracecmd_iterate_events_reverse(struct tracecmd_input *handle,
 		}
 	} while (next_cpu >= 0 && ret == 0);
 
+	free_last_events(handle, records, cpus, cpu_size, max_cpus);
 	free(records);
 
 	return ret;
Signature · 1 line
2.51.0
#1 lore raw
Quoted 12 lines · On Fri, 21 Nov 2025 14:47:49 +0100 Felix Moessbauer…

When calling tracecmd_iterate_events_reverse with a callback that does not always return 0, the trace is only partially iterated. By that, the non-iterated records are leaked, resulting in the error:

1 pages still allocated on cpu <cpu>

We fix this by always iterating the remaining events on all selected CPUs. In the full iteration case, this stops on the first record as this is already zero. In the partial iteration case, all remaining records are freed, which is - by construction of the records list - at max a page size.

Quoted 6 lines

---
Note, that this bug has been reported in [1].

[1] https://lore.kernel.org/linux-trace-devel/[email protected]/

Thanks for the report. I actually found two bugs here.

Quoted 21 lines

Best regards,
Felix Moessbauer
Siemens AG

 lib/trace-cmd/trace-input.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
lib/trace-cmd/trace-input.c +6 -0
index f2471c92..afdbd2aa 100644
@@ -2995,6 +2995,25 @@ static struct tep_record *next_last_event(struct tracecmd_input *handle,
 	return record;
 }
 
+static void free_last_events(struct tracecmd_input *handle,
+			     struct tep_record **last_records,
+			     cpu_set_t *cpu_set, int cpu_size,
+			     int cpus)
+{
+	struct tep_record *record;

lib/trace-cmd/trace-input.c static struct tep_record *next_last_event(struct tracecmd_input *handle, · line 3013

Quoted 10 lines
lib/trace-cmd/trace-input.c +10 -0
+	int cpu;
+
+	for (cpu = 0; cpu < cpus; cpu++) {
+		if (cpus && !CPU_ISSET_S(cpu, cpu_size, cpu_set))
+			continue;
+
+		do {
+			record = next_last_event(handle, last_records, cpu);
+			tracecmd_free_record(record);
+		} while (record);

I just fixed it slightly different than what you proposed here. But I think we can take your patch instead.

Quoted 7 lines
lib/trace-cmd/trace-input.c +3 -0
+	}
+}
+
 /**
  * tracecmd_iterate_events_reverse - iterate events over a given handle backwards
  * @handle: The handle to iterate over

lib/trace-cmd/trace-input.c int tracecmd_iterate_events_reverse(struct tracecmd_input *handle, · line 3082

Quoted 7 lines
lib/trace-cmd/trace-input.c +1 -0
@@ -3057,6 +3076,7 @@ int tracecmd_iterate_events_reverse(struct tracecmd_input *handle,
 		}
 	} while (next_cpu >= 0 && ret == 0);
 
+	free_last_events(handle, records, cpus, cpu_size, max_cpus);
 	free(records);
 
 	return ret;

The second bug I found was that calling tracecmd_iterate_events_reverse() again after a callback returned a ret value, and this time with cont = true, it can skip a lot of records because the cursor of the last record read is not saved. This now makes calling this function again with cont start with the last record that was read (the one that the caller exited from).

The below patch fixes that too:

-- Steve

lib/trace-cmd/trace-input.c +14 -0
index afdbd2aa98b6..8dfeff458485 100644
@@ -3036,6 +3036,7 @@ int tracecmd_iterate_events_reverse(struct tracecmd_input *handle,
 				    void *callback_data, bool cont)
 {
 	unsigned long long last_timestamp = 0;
+	unsigned long long page_offset = 0;
 	struct tep_record **records;
 	struct tep_record *record;
 	int next_cpu;
@@ -3072,6 +3073,8 @@ int tracecmd_iterate_events_reverse(struct tracecmd_input *handle,
 			record = next_last_event(handle, records, next_cpu);;
 			ret = call_callbacks(handle, record, next_cpu,
 					     callback, callback_data);
+			if (ret)
+				page_offset = record->offset;
 			tracecmd_free_record(record);
 		}
 	} while (next_cpu >= 0 && ret == 0);
@@ -3079,6 +3082,17 @@ int tracecmd_iterate_events_reverse(struct tracecmd_input *handle,
 	free_last_events(handle, records, cpus, cpu_size, max_cpus);
 	free(records);
 
+	/*
+	 * If the callback exited out early, then set the cursor back
+	 * to the location of that record so that if this gets called
+	 * again with cont = true, it will continue where it left off.
+	 */
+	if (page_offset) {
+		/* Set the record to the previous record that was read */
+		record = tracecmd_read_at(handle, page_offset - 4, NULL);
+		tracecmd_free_record(record);
+	}
+
 	return ret;
 }
 
#2 lore raw
Quoted 15 lines · Steven Rostedt
Quoted 12 lines · On Fri, 21 Nov 2025 14:47:49 +0100 Felix Moessbauer…

When calling tracecmd_iterate_events_reverse with a callback that does not always return 0, the trace is only partially iterated. By that, the non-iterated records are leaked, resulting in the error:

1 pages still allocated on cpu <cpu>

We fix this by always iterating the remaining events on all selected CPUs. In the full iteration case, this stops on the first record as this is already zero. In the partial iteration case, all remaining records are freed, which is - by construction of the records list - at max a page size.

Quoted 8 lines
Quoted 6 lines

---
Note, that this bug has been reported in [1].

[1] https://lore.kernel.org/linux-trace-devel/[email protected]/

Thanks for the report. I actually found two bugs here.

Hi,

while thinking about it again, I noticed that the callback based iteration interface relies on some assumptions which are not documented. While probably obvious, it should be documented what is NOT allowed to be done inside the callback (i.e. calling any tracecmd_iterate_* function, but also calling tracecmd_read_* and alike?).

Best regards,
Felix

Signature · 4 lines
Siemens AG
Linux Expert Center
Friedrich-Ludwig-Bauer-Str. 3
85748 Garching, Germany
#3 lore raw
Quoted 8 lines · Felix

Hi,

while thinking about it again, I noticed that the callback based iteration interface relies on some assumptions which are not documented. While probably obvious, it should be documented what is NOT allowed to be done inside the callback (i.e. calling any tracecmd_iterate_* function, but also calling tracecmd_read_* and alike?).

Agreed that this should be documented. I wouldn't say it is "NOT" allowed, but instead specify what the side effects are.

I think stating how the iterator is somewhat implemented is good, as it should allow the user to assume its behavior.

Basically saying that the internal cursor of the iterator is:

  do {
	record = tracecmd_read_next_data(handle, &cpu);
	if (!record)
		break;
	ret = callback(handle, record, cpu, data);

} while (!ret);

And state that any reads in the callback can change the iterator, and cause it to behave differently, just like it would in the above code.

Hmm, my fix of the reverse code had the last record get read again. That's different than the iterator itself. I should get rid of the "- 4" and make the continue read the next record after the one last read too.

Thanks,

-- Steve

#4 lore raw
Quoted 12 lines · Steven Rostedt
Quoted 8 lines · Felix

Hi,

while thinking about it again, I noticed that the callback based iteration interface relies on some assumptions which are not documented. While probably obvious, it should be documented what is NOT allowed to be done inside the callback (i.e. calling any tracecmd_iterate_* function, but also calling tracecmd_read_* and alike?).

Quoted 2 lines

Agreed that this should be documented. I wouldn't say it is "NOT" allowed, but instead specify what the side effects are.

Agree, makes sense.

Quoted 11 lines

I think stating how the iterator is somewhat implemented is good, as it should allow the user to assume its behavior.

Basically saying that the internal cursor of the iterator is:

  do {
	record = tracecmd_read_next_data(handle, &cpu);
	if (!record)
		break;
	ret = callback(handle, record, cpu, data);
Quoted 4 lines

} while (!ret);

And state that any reads in the callback can change the iterator, and cause it to behave differently, just like it would in the above code.

I'm just wondering if all possible / reasonable user interactions lead to a state that does not leak memory. Anyways, just documenting it is IMHO sufficient.

Quoted 4 lines

Hmm, my fix of the reverse code had the last record get read again. That's different than the iterator itself. I should get rid of the "- 4" and make the continue read the next record after the one last read too.

Yep!

Felix

Quoted 4 lines

Thanks,

-- Steve

Signature · 4 lines
Siemens AG
Linux Expert Center
Friedrich-Ludwig-Bauer-Str. 3
85748 Garching, Germany
#5 lore raw
In reply to Felix Moessbauer

Nit, when sending patches, please capitalize the first word in the subject:

[PATCH] libtracecmd: Fix memory leak on partial reverse iteration

(I updated this when pulling in your patch).

And you don't need the 1/1 for a single patch.

Thanks!

-- Steve