Linux Trace Kernel
 help / color / mirror / Atom feed
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: LKML <linux-kernel@vger.kernel.org>,
	Linux Trace Kernel <linux-trace-kernel@vger.kernel.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Subject: Re: [PATCH] ring-buffer: Simplify functions with __free(kfree) to free allocations
Date: Wed, 28 May 2025 09:28:54 +0900	[thread overview]
Message-ID: <20250528092854.2d94f047c5c947a7d1b1ea8b@kernel.org> (raw)
In-Reply-To: <20250527143144.6edc4625@gandalf.local.home>

On Tue, 27 May 2025 14:31:44 -0400
Steven Rostedt <rostedt@goodmis.org> wrote:

> From: Steven Rostedt <rostedt@goodmis.org>
> 
> The function rb_allocate_pages() allocates cpu_buffer and on error needs
> to free it. It has a single return. Use __free(kfree) and return directly
> on errors and have the return use return_ptr(cpu_buffer).
> 
> The function alloc_buffer() allocates buffer and on error needs to free
> it. It has a single return. Use __free(kfree) and return directly on
> errors and have the return use return_ptr(buffer).
> 
> The function __rb_map_vma() allocates a temporary array "pages". Have it
> use __free() and not worry about freeing it when returning.
> 

Looks good to me.

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

> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
> ---
>  kernel/trace/ring_buffer.c | 27 +++++++++------------------
>  1 file changed, 9 insertions(+), 18 deletions(-)
> 
> diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
> index f3aa18c89166..10a4b26929ae 100644
> --- a/kernel/trace/ring_buffer.c
> +++ b/kernel/trace/ring_buffer.c
> @@ -2226,7 +2226,7 @@ static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
>  static struct ring_buffer_per_cpu *
>  rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
>  {
> -	struct ring_buffer_per_cpu *cpu_buffer;
> +	struct ring_buffer_per_cpu *cpu_buffer __free(kfree) = NULL;
>  	struct ring_buffer_cpu_meta *meta;
>  	struct buffer_page *bpage;
>  	struct page *page;
> @@ -2252,7 +2252,7 @@ rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
>  	bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
>  			    GFP_KERNEL, cpu_to_node(cpu));
>  	if (!bpage)
> -		goto fail_free_buffer;
> +		return NULL;
>  
>  	rb_check_bpage(cpu_buffer, bpage);
>  
> @@ -2318,13 +2318,11 @@ rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
>  		rb_head_page_activate(cpu_buffer);
>  	}
>  
> -	return cpu_buffer;
> +	return_ptr(cpu_buffer);
>  
>   fail_free_reader:
>  	free_buffer_page(cpu_buffer->reader_page);
>  
> - fail_free_buffer:
> -	kfree(cpu_buffer);
>  	return NULL;
>  }
>  
> @@ -2359,7 +2357,7 @@ static struct trace_buffer *alloc_buffer(unsigned long size, unsigned flags,
>  					 unsigned long scratch_size,
>  					 struct lock_class_key *key)
>  {
> -	struct trace_buffer *buffer;
> +	struct trace_buffer *buffer __free(kfree) = NULL;
>  	long nr_pages;
>  	int subbuf_size;
>  	int bsize;
> @@ -2373,7 +2371,7 @@ static struct trace_buffer *alloc_buffer(unsigned long size, unsigned flags,
>  		return NULL;
>  
>  	if (!zalloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
> -		goto fail_free_buffer;
> +		return NULL;
>  
>  	buffer->subbuf_order = order;
>  	subbuf_size = (PAGE_SIZE << order);
> @@ -2472,7 +2470,7 @@ static struct trace_buffer *alloc_buffer(unsigned long size, unsigned flags,
>  
>  	mutex_init(&buffer->mutex);
>  
> -	return buffer;
> +	return_ptr(buffer);
>  
>   fail_free_buffers:
>  	for_each_buffer_cpu(buffer, cpu) {
> @@ -2484,8 +2482,6 @@ static struct trace_buffer *alloc_buffer(unsigned long size, unsigned flags,
>   fail_free_cpumask:
>  	free_cpumask_var(buffer->cpumask);
>  
> - fail_free_buffer:
> -	kfree(buffer);
>  	return NULL;
>  }
>  
> @@ -7076,7 +7072,7 @@ static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
>  {
>  	unsigned long nr_subbufs, nr_pages, nr_vma_pages, pgoff = vma->vm_pgoff;
>  	unsigned int subbuf_pages, subbuf_order;
> -	struct page **pages;
> +	struct page **pages __free(kfree) = NULL;
>  	int p = 0, s = 0;
>  	int err;
>  
> @@ -7144,10 +7140,8 @@ static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
>  		struct page *page;
>  		int off = 0;
>  
> -		if (WARN_ON_ONCE(s >= nr_subbufs)) {
> -			err = -EINVAL;
> -			goto out;
> -		}
> +		if (WARN_ON_ONCE(s >= nr_subbufs))
> +			return -EINVAL;
>  
>  		page = virt_to_page((void *)cpu_buffer->subbuf_ids[s]);
>  
> @@ -7162,9 +7156,6 @@ static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
>  
>  	err = vm_insert_pages(vma, vma->vm_start, pages, &nr_pages);
>  
> -out:
> -	kfree(pages);
> -
>  	return err;
>  }
>  #else
> -- 
> 2.47.2
> 


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

      reply	other threads:[~2025-05-28  0:28 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-27 18:31 [PATCH] ring-buffer: Simplify functions with __free(kfree) to free allocations Steven Rostedt
2025-05-28  0:28 ` Masami Hiramatsu [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250528092854.2d94f047c5c947a7d1b1ea8b@kernel.org \
    --to=mhiramat@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox