qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Zheng Huang <hz1624917200@gmail.com>
To: "Philippe Mathieu-Daudé" <philmd@linaro.org>
Cc: qemu-devel@nongnu.org
Subject: Re: [PATCH] virtio-pci: fix memory leak from device realization failure
Date: Mon, 3 Mar 2025 15:41:02 +0800	[thread overview]
Message-ID: <ea7e1e88-d627-4230-8a6c-5aefbbb3bbef@gmail.com> (raw)
In-Reply-To: <c6bf97bf-df26-495d-9886-dfda55cc99db@linaro.org>


Hi Philippe,

On 2025/2/28 17:24, Philippe Mathieu-Daudé wrote:
> Hi Zheng,
> 
> On 28/2/25 06:03, Zheng Huang wrote:
>> This commit adds failback routine for `virtio_pci_realize` to
>> fix the memory leak of an address space and the virtio-net device object.
>> If the realization of the device failed, the address space should be
>> destroyed too.
>>
>> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2845
>>
>> Signed-off-by: Zheng Huang <hz1624917200@outlook.com>
>>
>> ---
>>   hw/virtio/virtio-pci.c | 3 +++
>>   1 file changed, 3 insertions(+)
>>
>> diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
>> index c773a9130c..4b0d8cd90a 100644
>> --- a/hw/virtio/virtio-pci.c
>> +++ b/hw/virtio/virtio-pci.c
>> @@ -2266,6 +2266,9 @@ static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
>>       virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
>>       if (k->realize) {
>>           k->realize(proxy, errp);
>> +        if (*errp) {
>> +            address_space_destroy(&proxy->modern_cfg_mem_as);
>> +        }
>>       }
>>   }
>>   
> 
> I think instead we want to add an instance_init in virtio_pci_class_init
> and move the address_space_init call from virtio_pci_realize there.
> 
> Regards,
> 
> Phil.

I have reviewed the relevant code again and found that if address_space_init
is moved into instance_init, it will not be able to take follow-up actions
such as free the AS if device realization failed, thus failing to address the
issue. Additionally, I referred to the code for AS initialization and
destruction in other devices and found that they are managed in device
realize and unrealize handlers. Therefore, I still believe the previous
approach is a better choice.

If there are other potential solutions or considerations that I might have
missed, please let me know. I'm looking forward to hearing your thoughts!

Best regards,

Zheng.


  reply	other threads:[~2025-03-03 13:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <7702b335-6e92-47c7-baf9-a384f75a0db3@gmail.com>
2025-02-28  5:03 ` [PATCH] virtio-pci: fix memory leak from device realization failure Zheng Huang
2025-02-28  9:24   ` Philippe Mathieu-Daudé
2025-03-03  7:41     ` Zheng Huang [this message]
2025-03-10  9:04     ` Zheng Huang

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=ea7e1e88-d627-4230-8a6c-5aefbbb3bbef@gmail.com \
    --to=hz1624917200@gmail.com \
    --cc=philmd@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).