Skip to content

Commit

Permalink
tracing: Freeable reserved ring buffer
Browse files Browse the repository at this point in the history
Make the ring buffer on reserved memory to be freeable. This allows us
to free the trace instance on the reserved memory without changing
cmdline and rebooting. Even if we can not change the kernel cmdline
for security reason, we can release the reserved memory for the ring
buffer as free (available) memory.

For example, boot kernel with reserved memory;
"reserve_mem=20M:2M:trace trace_instance=boot_mapped^traceoff@trace"

~ # free
              total        used        free      shared  buff/cache   available
Mem:        1995548       50544     1927568       14964       17436     1911480
Swap:             0           0           0
~ # rmdir /sys/kernel/tracing/instances/boot_mapped/
[   23.704023] Freeing reserve_mem:trace memory: 20476K
~ # free
              total        used        free      shared  buff/cache   available
Mem:        2016024       41844     1956740       14968       17440     1940572
Swap:             0           0           0

Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Mike Rapoport <rppt@kernel.org>
Link: https://lore.kernel.org/173989134814.230693.18199312930337815629.stgit@devnote2
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
  • Loading branch information
Masami Hiramatsu (Google) authored and Steven Rostedt (Google) committed Mar 28, 2025
1 parent 74e2498 commit fb6d032
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
13 changes: 12 additions & 1 deletion kernel/trace/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -9479,6 +9479,9 @@ static void free_trace_buffers(struct trace_array *tr)
#ifdef CONFIG_TRACER_MAX_TRACE
free_trace_buffer(&tr->max_buffer);
#endif

if (tr->range_addr_start)
vunmap((void *)tr->range_addr_start);
}

static void init_trace_flags_index(struct trace_array *tr)
Expand Down Expand Up @@ -9640,6 +9643,7 @@ trace_array_create_systems(const char *name, const char *systems,
free_cpumask_var(tr->pipe_cpumask);
free_cpumask_var(tr->tracing_cpumask);
kfree_const(tr->system_names);
kfree(tr->range_name);
kfree(tr->name);
kfree(tr);

Expand Down Expand Up @@ -9766,6 +9770,11 @@ static int __remove_instance(struct trace_array *tr)
free_trace_buffers(tr);
clear_tracing_err_log(tr);

if (tr->range_name) {
reserve_mem_release_by_name(tr->range_name);
kfree(tr->range_name);
}

for (i = 0; i < tr->nr_topts; i++) {
kfree(tr->topts[i].topts);
}
Expand Down Expand Up @@ -10590,6 +10599,7 @@ __init static void enable_instances(void)
bool traceoff = false;
char *flag_delim;
char *addr_delim;
char *rname __free(kfree) = NULL;

tok = strsep(&curr_str, ",");

Expand Down Expand Up @@ -10646,6 +10656,7 @@ __init static void enable_instances(void)
pr_warn("Failed to map boot instance %s to %s\n", name, tok);
continue;
}
rname = kstrdup(tok, GFP_KERNEL);
}

if (start) {
Expand Down Expand Up @@ -10682,7 +10693,7 @@ __init static void enable_instances(void)
*/
if (start) {
tr->flags |= TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT;
tr->ref++;
tr->range_name = no_free_ptr(rname);
}

while ((tok = strsep(&curr_str, ","))) {
Expand Down
1 change: 1 addition & 0 deletions kernel/trace/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ struct trace_array {
unsigned int mapped;
unsigned long range_addr_start;
unsigned long range_addr_size;
char *range_name;
long text_delta;
void *scratch; /* pointer in persistent memory */
int scratch_size;
Expand Down

0 comments on commit fb6d032

Please sign in to comment.