diff mbox series

[v2,2/2] tracing: Add free_trace_iter_content() helper function

Message ID 20230715141348.341887497@goodmis.org (mailing list archive)
State Accepted
Commit 6bba92881de9dde4c07cbb24b5691eb6eb9495f2
Headers show
Series tracing: Clean up how iter is freed | expand

Commit Message

Steven Rostedt July 15, 2023, 2:12 p.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

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.

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 kernel/trace/trace.c | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)

Comments

Masami Hiramatsu (Google) July 26, 2023, 1:42 p.m. UTC | #1
On Sat, 15 Jul 2023 10:12:15 -0400
Steven Rostedt <rostedt@goodmis.org> wrote:

> From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
> 
> 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.
> 
> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

Looks good to me.

Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

BTW, this adds iter->fmt != static_fmt_buf check. Is it a kind of fix?

Thank you,

> ---
>  kernel/trace/trace.c | 33 ++++++++++++++++++++++-----------
>  1 file changed, 22 insertions(+), 11 deletions(-)
> 
> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> index 1c370ffbe062..8775930aa545 100644
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
> @@ -4815,6 +4815,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)
>  {
> @@ -4922,8 +4941,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);
> @@ -5002,11 +5020,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;
> @@ -6763,10 +6777,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);
> -- 
> 2.40.1
Steven Rostedt July 28, 2023, 11:08 p.m. UTC | #2
On Wed, 26 Jul 2023 22:42:13 +0900
Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote:

> On Sat, 15 Jul 2023 10:12:15 -0400
> Steven Rostedt <rostedt@goodmis.org> wrote:
> 
> > From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
> > 
> > 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.
> > 
> > Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>  
> 
> Looks good to me.
> 
> Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> 
> BTW, this adds iter->fmt != static_fmt_buf check. Is it a kind of fix?

No, because all of the callers shouldn't actually set it to that. I
added the if statement in case one of the places that do set it does
call this.

In other words, I added the if statement to make it more robust and
prevent a bug in the future ;-)

-- Steve


> 
> Thank you,
> 
> > ---
> >  kernel/trace/trace.c | 33 ++++++++++++++++++++++-----------
> >  1 file changed, 22 insertions(+), 11 deletions(-)
> > 
> > diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> > index 1c370ffbe062..8775930aa545 100644
> > --- a/kernel/trace/trace.c
> > +++ b/kernel/trace/trace.c
> > @@ -4815,6 +4815,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)
> >  {
> > @@ -4922,8 +4941,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);
> > @@ -5002,11 +5020,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;
> > @@ -6763,10 +6777,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);
> > -- 
> > 2.40.1  
> 
>
diff mbox series

Patch

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 1c370ffbe062..8775930aa545 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -4815,6 +4815,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)
 {
@@ -4922,8 +4941,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);
@@ -5002,11 +5020,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;
@@ -6763,10 +6777,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);