qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@linaro.org>
To: Peter Maydell <peter.maydell@linaro.org>, qemu-devel@nongnu.org
Cc: "Michael S. Tsirkin" <mst@redhat.com>,
	Marcel Apfelbaum <marcel.apfelbaum@gmail.com>
Subject: Re: [PATCH] hw/i386/amd_iommu: Don't leak memory in amdvi_update_iotlb()
Date: Mon, 17 Feb 2025 08:26:53 +0100	[thread overview]
Message-ID: <5a220564-313a-44e9-8ae9-a2faec14ebb5@linaro.org> (raw)
In-Reply-To: <20240731170019.3590563-1-peter.maydell@linaro.org>

On 31/7/24 19:00, Peter Maydell wrote:
> In amdvi_update_iotlb() we will only put a new entry in the hash
> table if to_cache.perm is not IOMMU_NONE.  However we allocate the
> memory for the new AMDVIIOTLBEntry and for the hash table key
> regardless.  This means that in the IOMMU_NONE case we will leak the
> memory we alloacted.
> 
> Move the allocations into the if() to the point where we know we're
> going to add the item to the hash table.
> 
> Cc: qemu-stable@nongnu.org
> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2452
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> Tested with 'make check' and 'make check-avocado' only, but the
> bug and fix seem straightforward...

Oh now I remembered:
https://lore.kernel.org/qemu-devel/CACGkMEtjmpX8G9HYZ0r3n5ErhAENKhQ81f4ocfCYrh=XoF=5hw@mail.gmail.com/

> ---
>   hw/i386/amd_iommu.c | 8 ++++----
>   1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c
> index 6d4fde72f9b..87643d28917 100644
> --- a/hw/i386/amd_iommu.c
> +++ b/hw/i386/amd_iommu.c
> @@ -357,12 +357,12 @@ static void amdvi_update_iotlb(AMDVIState *s, uint16_t devid,
>                                  uint64_t gpa, IOMMUTLBEntry to_cache,
>                                  uint16_t domid)
>   {
> -    AMDVIIOTLBEntry *entry = g_new(AMDVIIOTLBEntry, 1);
> -    uint64_t *key = g_new(uint64_t, 1);
> -    uint64_t gfn = gpa >> AMDVI_PAGE_SHIFT_4K;
> -
>       /* don't cache erroneous translations */
>       if (to_cache.perm != IOMMU_NONE) {
> +        AMDVIIOTLBEntry *entry = g_new(AMDVIIOTLBEntry, 1);
> +        uint64_t *key = g_new(uint64_t, 1);
> +        uint64_t gfn = gpa >> AMDVI_PAGE_SHIFT_4K;
> +
>           trace_amdvi_cache_update(domid, PCI_BUS_NUM(devid), PCI_SLOT(devid),
>                   PCI_FUNC(devid), gpa, to_cache.translated_addr);
>   



  parent reply	other threads:[~2025-02-17  7:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-31 17:00 [PATCH] hw/i386/amd_iommu: Don't leak memory in amdvi_update_iotlb() Peter Maydell
2024-07-31 21:07 ` Philippe Mathieu-Daudé
2025-02-17  7:26 ` Philippe Mathieu-Daudé [this message]
2025-02-20 15:55   ` Alejandro Jimenez

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=5a220564-313a-44e9-8ae9-a2faec14ebb5@linaro.org \
    --to=philmd@linaro.org \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mst@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.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;
as well as URLs for NNTP newsgroup(s).