Skip to content

Commit

Permalink
tracing: Add free_trace_iter_content() helper function
Browse files Browse the repository at this point in the history
As the trace iterator is created and used by various interfaces, the clean
up of it needs to be consistent. Create a free_trace_iter_content() helper
function that frees the content of the iterator and use that to clean it
up in all places that it is used.

Link: https://lkml.kernel.org/r/20230715141348.341887497@goodmis.org

Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
  • Loading branch information
Steven Rostedt (Google) committed Jul 30, 2023
1 parent 9182b51 commit 6bba928
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions kernel/trace/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -4799,6 +4799,25 @@ static const struct seq_operations tracer_seq_ops = {
.show = s_show,
};

/*
* Note, as iter itself can be allocated and freed in different
* ways, this function is only used to free its content, and not
* the iterator itself. The only requirement to all the allocations
* is that it must zero all fields (kzalloc), as freeing works with
* ethier allocated content or NULL.
*/
static void free_trace_iter_content(struct trace_iterator *iter)
{
/* The fmt is either NULL, allocated or points to static_fmt_buf */
if (iter->fmt != static_fmt_buf)
kfree(iter->fmt);

kfree(iter->temp);
kfree(iter->buffer_iter);
mutex_destroy(&iter->mutex);
free_cpumask_var(iter->started);
}

static struct trace_iterator *
__tracing_open(struct inode *inode, struct file *file, bool snapshot)
{
Expand Down Expand Up @@ -4906,8 +4925,7 @@ __tracing_open(struct inode *inode, struct file *file, bool snapshot)

fail:
mutex_unlock(&trace_types_lock);
kfree(iter->temp);
kfree(iter->buffer_iter);
free_trace_iter_content(iter);
release:
seq_release_private(inode, file);
return ERR_PTR(-ENOMEM);
Expand Down Expand Up @@ -4986,11 +5004,7 @@ static int tracing_release(struct inode *inode, struct file *file)

mutex_unlock(&trace_types_lock);

mutex_destroy(&iter->mutex);
free_cpumask_var(iter->started);
kfree(iter->fmt);
kfree(iter->temp);
kfree(iter->buffer_iter);
free_trace_iter_content(iter);
seq_release_private(inode, file);

return 0;
Expand Down Expand Up @@ -6747,10 +6761,7 @@ static int tracing_release_pipe(struct inode *inode, struct file *file)

mutex_unlock(&trace_types_lock);

free_cpumask_var(iter->started);
kfree(iter->fmt);
kfree(iter->temp);
mutex_destroy(&iter->mutex);
free_trace_iter_content(iter);
kfree(iter);

trace_array_put(tr);
Expand Down

0 comments on commit 6bba928

Please sign in to comment.