From: Joao Martins <joao.m.martins@oracle.com>
To: "Duan, Zhenzhong" <zhenzhong.duan@intel.com>
Cc: "alex.williamson@redhat.com" <alex.williamson@redhat.com>,
"clg@redhat.com" <clg@redhat.com>,
"qemu-devel@nongnu.org" <qemu-devel@nongnu.org>,
"avihaih@nvidia.com" <avihaih@nvidia.com>,
"Peng, Chao P" <chao.p.peng@intel.com>
Subject: Re: [PATCH v3 1/3] vfio/pci: Fix resource leak in vfio_realize
Date: Mon, 26 Jun 2023 11:07:36 +0100 [thread overview]
Message-ID: <7e1567f1-aa79-cbeb-3c1a-20594f1942cd@oracle.com> (raw)
In-Reply-To: <SJ0PR11MB6744837FBCEAB1F9060BBAFD9226A@SJ0PR11MB6744.namprd11.prod.outlook.com>
On 26/06/2023 08:02, Duan, Zhenzhong wrote:
>> -----Original Message-----
>> From: Duan, Zhenzhong
>> Sent: Sunday, June 25, 2023 2:01 PM
>> To: Joao Martins <joao.m.martins@oracle.com>
>> Cc: alex.williamson@redhat.com; clg@redhat.com; qemu-devel@nongnu.org;
>> avihaih@nvidia.com; Peng, Chao P <chao.p.peng@intel.com>
>> Subject: RE: [PATCH v3 1/3] vfio/pci: Fix resource leak in vfio_realize
>>
>>> -----Original Message-----
>>> From: Joao Martins <joao.m.martins@oracle.com>
>>> Sent: Wednesday, June 21, 2023 7:08 PM
>>> To: Duan, Zhenzhong <zhenzhong.duan@intel.com>
>>> Cc: alex.williamson@redhat.com; clg@redhat.com; qemu-
>> devel@nongnu.org;
>>> avihaih@nvidia.com; Peng, Chao P <chao.p.peng@intel.com>
>>> Subject: Re: [PATCH v3 1/3] vfio/pci: Fix resource leak in vfio_realize
>>>
>>>
>>>
>>> On 21/06/2023 09:02, Zhenzhong Duan wrote:
>>>> When adding migration blocker failed in vfio_migration_realize(),
>>>> hotplug will fail and we see below:
>>>>
>>>> (qemu) device_add
>>>> vfio-pci,host=81:11.1,id=vfio1,bus=root1,x-enable-migration=true
>>>> 0000:81:11.1: VFIO migration is not supported in kernel
>>>> Error: disallowing migration blocker (--only-migratable) for: VFIO
>>>> device doesn't support migration
>>>>
>>>> If we hotplug again we should see same log as above, but we see:
>>>> (qemu) device_add
>>>> vfio-pci,host=81:11.0,id=vfio0,bus=root0,x-enable-migration=true
>>>> Error: vfio 0000:81:11.0: device is already attached
>>>>
>>>> That's because some references to VFIO device isn't released, we
>>>> should check return value of vfio_migration_realize() and release the
>>>> references, then VFIO device will be truely released when hotplug
>>>> failed.
>>>>
>>>> Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
>>>> ---
>>>> hw/vfio/pci.c | 1 +
>>>> 1 file changed, 1 insertion(+)
>>>>
>>>> diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index
>>>> 73874a94de12..c71b0955d81c 100644
>>>> --- a/hw/vfio/pci.c
>>>> +++ b/hw/vfio/pci.c
>>>> @@ -3210,6 +3210,7 @@ static void vfio_realize(PCIDevice *pdev, Error
>>> **errp)
>>>> ret = vfio_migration_realize(vbasedev, errp);
>>>> if (ret) {
>>>> error_report("%s: Migration disabled", vbasedev->name);
>>>> + goto out_deregister;
>>>> }
>>>> }
>>>>
>>> This doesn't look right. This means that failure to support migration
>>> will deregister the device.
>>
>> In my understanding, failure to support migration but success to add
>> migration blocker will not deregister device. vfio_migration_realize() is
>> successful in this case.
>> But failure to add migration blocker should deregister device, because
>> vfio_exitfn() is never called in this case(errp set), jumping to out_deregister is
>> the best choice. Then vfio_migration_realize() should return failure in this
>> case.
>
I was checking other devices in the tree, and I think you are right. Failure to
add the migration blocker results in a failure of device realize. Which IIUC
only happens in 'only-migratable' setups and during snapshots.
Maybe also including a sentence explainer in the commit message is useful too.
> I just realized the error path in vfio_realize() isn't adequate. We need to add more deregister code just as vfio_exitfn(), see below. I'll re-post after we are consensus on this and get some comments of PATCH3.
>
> --- a/hw/vfio/pci.c
> +++ b/hw/vfio/pci.c
> @@ -3210,7 +3210,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
> ret = vfio_migration_realize(vbasedev, errp);
> if (ret) {
> error_report("%s: Migration disabled", vbasedev->name);
> - goto out_deregister;
> + goto out_vfio_migration;
> }
> }
>
> @@ -3220,11 +3220,17 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
>
> return;
>
> +out_vfio_migration:
> + vfio_migration_exit(vbasedev);
This belongs in this patch from the looks
> out_deregister:
> pci_device_set_intx_routing_notifier(&vdev->pdev, NULL);
> if (vdev->irqchip_change_notifier.notify) {
> kvm_irqchip_remove_change_notifier(&vdev->irqchip_change_notifier);
> }
> + vfio_disable_interrupts(vdev);
> + if (vdev->intx.mmap_timer) {
> + timer_free(vdev->intx.mmap_timer);
> + }
But this one suggests another one as it looks a pre-existing issue?
> out_teardown:
> vfio_teardown_msi(vdev);
> vfio_bars_exit(vdev);
>
> Thanks
> Zhenzhong
next prev parent reply other threads:[~2023-06-26 10:08 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-21 8:02 [PATCH v3 0/3] VFIO migration related refactor and bug fix Zhenzhong Duan
2023-06-21 8:02 ` [PATCH v3 1/3] vfio/pci: Fix resource leak in vfio_realize Zhenzhong Duan
2023-06-21 11:08 ` Joao Martins
2023-06-25 6:00 ` Duan, Zhenzhong
2023-06-26 7:02 ` Duan, Zhenzhong
2023-06-26 10:07 ` Joao Martins [this message]
2023-06-27 2:38 ` Duan, Zhenzhong
2023-06-27 10:21 ` Joao Martins
2023-06-27 10:28 ` Duan, Zhenzhong
2023-06-21 8:02 ` [PATCH v3 2/3] vfio/pci: Fix a segfault " Zhenzhong Duan
2023-06-21 11:08 ` Joao Martins
2023-06-25 6:01 ` Duan, Zhenzhong
2023-06-26 9:58 ` Joao Martins
2023-06-21 8:02 ` [PATCH v3 3/3] vfio/migration: vfio/migration: Refactor and fix print of "Migration disabled" Zhenzhong Duan
2023-06-26 9:34 ` Avihai Horon
2023-06-26 10:18 ` Joao Martins
2023-06-27 2:55 ` Duan, Zhenzhong
2023-06-27 10:56 ` Joao Martins
2023-06-28 2:26 ` Duan, Zhenzhong
2023-06-27 2:46 ` Duan, Zhenzhong
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=7e1567f1-aa79-cbeb-3c1a-20594f1942cd@oracle.com \
--to=joao.m.martins@oracle.com \
--cc=alex.williamson@redhat.com \
--cc=avihaih@nvidia.com \
--cc=chao.p.peng@intel.com \
--cc=clg@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=zhenzhong.duan@intel.com \
/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).