All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vincent Donnefort <vdonnefort@google.com>
To: sashiko-reviews@lists.linux.dev
Cc: kvmarm@lists.linux.dev, Oliver Upton <oupton@kernel.org>,
	Marc Zyngier <maz@kernel.org>
Subject: Re: [PATCH] KVM: arm64: Fix potential leak in hyp_trace_buffer_alloc_bpages_backing
Date: Wed, 8 Jul 2026 16:19:43 +0100	[thread overview]
Message-ID: <ak5qjzhwSooBcjsZ@google.com> (raw)
In-Reply-To: <20260707170245.3967E1F000E9@smtp.kernel.org>

On Tue, Jul 07, 2026 at 05:02:44PM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> 
> Pre-existing issues:
> - [High] trace_remote_alloc_buffer leaks page allocations on the error path if a per-CPU ring buffer allocation fails.
> --
> 
> --- Patch [1]: [PATCH] KVM: arm64: Fix potential leak in hyp_trace_buffer_alloc_bpages_backing ---
> commit 21f10293a05c7439f7a1d1be691e92ef2d36331a
> Author: Vincent Donnefort <vdonnefort@google.com>
> 
> KVM: arm64: Fix potential leak in hyp_trace_buffer_alloc_bpages_backing
> 
> In the very unlikely event of a failure in __map_hyp, the allocated
> backing pages are leaked in hyp_trace_buffer_alloc_bpages_backing(). Fix
> this by freeing the pages on error.
> 
> Fixes: 3aed038aac8d ("KVM: arm64: Add trace remote for the nVHE/pKVM hyp")
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
> 
> > diff --git a/arch/arm64/kvm/hyp_trace.c b/arch/arm64/kvm/hyp_trace.c
> > index 2411b4c32932c..5fbbfd40376f7 100644
> > --- a/arch/arm64/kvm/hyp_trace.c
> > +++ b/arch/arm64/kvm/hyp_trace.c
> > @@ -160,6 +160,7 @@ static int hyp_trace_buffer_alloc_bpages_backing(struct hyp_trace_buffer *trace_
> >  	int nr_bpages = (PAGE_ALIGN(size) / PAGE_SIZE) + 1;
> >  	size_t backing_size;
> >  	void *start;
> > +	int ret;
> >  
> >  	backing_size = PAGE_ALIGN(sizeof(struct simple_buffer_page) * nr_bpages *
> >  				  num_possible_cpus());
> > @@ -171,7 +172,11 @@ static int hyp_trace_buffer_alloc_bpages_backing(struct hyp_trace_buffer *trace_
> >  	trace_buffer->desc->bpages_backing_start = (unsigned long)start;
> >  	trace_buffer->desc->bpages_backing_size = backing_size;
> >  
> > -	return __map_hyp(start, backing_size);
> > +	ret = __map_hyp(start, backing_size);
> > +	if (ret)
> > +		free_pages_exact(start, backing_size);
> > +
> > +	return ret;
> >  }
> 
> [Severity: High]
> This is a pre-existing issue, but since this patch fixes a memory leak in the 
> trace buffer allocation, I noticed a potential leak in a related error path.
> 
> In kernel/trace/trace_remote.c:trace_remote_alloc_buffer(), if an allocation
> fails, the code jumps to the err label:
> 
> 	rb_desc->meta_va = (unsigned long)__get_free_page(GFP_KERNEL);
> 	if (!rb_desc->meta_va)
> 		goto err;
> 
> 	for (id = 0; id < nr_pages; id++) {
> 		rb_desc->page_va[id] = (unsigned long)__get_free_page(GFP_KERNEL);
> 		if (!rb_desc->page_va[id])
> 			goto err;
> 
> 		rb_desc->nr_page_va++;
> 	}
> 	desc->nr_cpus++;
> 
> Because desc->nr_cpus is only incremented after all pages for the current CPU
> are allocated, any successfully allocated pages for the failing CPU are left
> out of the count.
> 
> When the error path calls trace_remote_free_buffer(), it iterates based on
> desc->nr_cpus:
> 
> 	for_each_ring_buffer_desc(rb_desc, cpu, desc) {
> 		unsigned int id;
> 
> 		free_page(rb_desc->meta_va);
> 
> 		for (id = 0; id < rb_desc->nr_page_va; id++)
> 			free_page(rb_desc->page_va[id]);
> 	}
> 
> Does this skip the partially initialized ring buffer for the failing CPU and 
> leak its allocated pages?

https://lore.kernel.org/all/20260708133201.295072-1-vdonnefort@google.com/

> 
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/20260707165029.3410701-1-vdonnefort@google.com?part=1
> 

  reply	other threads:[~2026-07-08 15:19 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-07 16:50 [PATCH] KVM: arm64: Fix potential leak in hyp_trace_buffer_alloc_bpages_backing Vincent Donnefort
2026-07-07 17:02 ` sashiko-bot
2026-07-08 15:19   ` Vincent Donnefort [this message]
2026-07-07 18:32 ` Fuad Tabba
2026-07-08  7:43   ` Vincent Donnefort

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=ak5qjzhwSooBcjsZ@google.com \
    --to=vdonnefort@google.com \
    --cc=kvmarm@lists.linux.dev \
    --cc=maz@kernel.org \
    --cc=oupton@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.