[PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is not available

Pierrick Bouvier posted 2 patches 2 weeks, 5 days ago
Maintainers: Yi Liu <yi.l.liu@intel.com>, Eric Auger <eric.auger@redhat.com>, Zhenzhong Duan <zhenzhong.duan@intel.com>, Alex Williamson <alex@shazbot.org>, "Cédric Le Goater" <clg@redhat.com>
There is a newer version of this series
[PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is not available
Posted by Pierrick Bouvier 2 weeks, 5 days ago
In case current kernel does not support /dev/iommu, qemu will probably
fail first because /sys/bus/pci/devices/*/vfio-dev/ is not present,
since QEMU opens it before /dev/iommu.

Instead, report an error directly when initializing iommufd object, to
inform user that kernel does not support it, with a hint about missing
CONFIG_IOMMUFD.

Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
---
 backends/iommufd.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/backends/iommufd.c b/backends/iommufd.c
index acfab907c03..c4eb3de1307 100644
--- a/backends/iommufd.c
+++ b/backends/iommufd.c
@@ -32,6 +32,11 @@ static void iommufd_backend_init(Object *obj)
 {
     IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
 
+    if (!g_file_test("/dev/iommu", G_FILE_TEST_EXISTS)) {
+        error_setg(&error_fatal, "/dev/iommu does not exist"
+                                 " (is your kernel config missing CONFIG_IOMMUFD?)");
+    }
+
     be->fd = -1;
     be->users = 0;
     be->owned = true;
-- 
2.47.3
RE: [PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is not available
Posted by Duan, Zhenzhong 2 weeks, 5 days ago

>-----Original Message-----
>From: Pierrick Bouvier <pierrick.bouvier@linaro.org>
>Subject: [PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is not
>available
>
>In case current kernel does not support /dev/iommu, qemu will probably
>fail first because /sys/bus/pci/devices/*/vfio-dev/ is not present,
>since QEMU opens it before /dev/iommu.
>
>Instead, report an error directly when initializing iommufd object, to
>inform user that kernel does not support it, with a hint about missing
>CONFIG_IOMMUFD.
>
>Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
>---
> backends/iommufd.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
>diff --git a/backends/iommufd.c b/backends/iommufd.c
>index acfab907c03..c4eb3de1307 100644
>--- a/backends/iommufd.c
>+++ b/backends/iommufd.c
>@@ -32,6 +32,11 @@ static void iommufd_backend_init(Object *obj)
> {
>     IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
>
>+    if (!g_file_test("/dev/iommu", G_FILE_TEST_EXISTS)) {
>+        error_setg(&error_fatal, "/dev/iommu does not exist"
>+                                 " (is your kernel config missing CONFIG_IOMMUFD?)");

If hot add an iommufd object with object_add QMP, may this kill QEMU instance?

Thanks
Zhenzhong

>+    }
>+
>     be->fd = -1;
>     be->users = 0;
>     be->owned = true;
>--
>2.47.3
Re: [PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is not available
Posted by Cédric Le Goater 2 weeks, 5 days ago
On 3/18/26 08:36, Duan, Zhenzhong wrote:
> 
> 
>> -----Original Message-----
>> From: Pierrick Bouvier <pierrick.bouvier@linaro.org>
>> Subject: [PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is not
>> available
>>
>> In case current kernel does not support /dev/iommu, qemu will probably
>> fail first because /sys/bus/pci/devices/*/vfio-dev/ is not present,
>> since QEMU opens it before /dev/iommu.
>>
>> Instead, report an error directly when initializing iommufd object, to
>> inform user that kernel does not support it, with a hint about missing
>> CONFIG_IOMMUFD.
>>
>> Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
>> ---
>> backends/iommufd.c | 5 +++++
>> 1 file changed, 5 insertions(+)
>>
>> diff --git a/backends/iommufd.c b/backends/iommufd.c
>> index acfab907c03..c4eb3de1307 100644
>> --- a/backends/iommufd.c
>> +++ b/backends/iommufd.c
>> @@ -32,6 +32,11 @@ static void iommufd_backend_init(Object *obj)
>> {
>>      IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
>>
>> +    if (!g_file_test("/dev/iommu", G_FILE_TEST_EXISTS)) {
>> +        error_setg(&error_fatal, "/dev/iommu does not exist"
>> +                                 " (is your kernel config missing CONFIG_IOMMUFD?)");
> 
> If hot add an iommufd object with object_add QMP, may this kill QEMU instance?

yes.


Unfortunately, backends are Objects. So we don't have a realize
handler and a 'Error **' parameter :/

Thanks,

C.



> 
> Thanks
> Zhenzhong
> 
>> +    }
>> +
>>      be->fd = -1;
>>      be->users = 0;
>>      be->owned = true;
>> --
>> 2.47.3
>
RE: [PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is not available
Posted by Duan, Zhenzhong 2 weeks, 5 days ago

>-----Original Message-----
>From: Cédric Le Goater <clg@redhat.com>
>Subject: Re: [PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is
>not available
>
>On 3/18/26 08:36, Duan, Zhenzhong wrote:
>>
>>
>>> -----Original Message-----
>>> From: Pierrick Bouvier <pierrick.bouvier@linaro.org>
>>> Subject: [PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is
>not
>>> available
>>>
>>> In case current kernel does not support /dev/iommu, qemu will probably
>>> fail first because /sys/bus/pci/devices/*/vfio-dev/ is not present,
>>> since QEMU opens it before /dev/iommu.
>>>
>>> Instead, report an error directly when initializing iommufd object, to
>>> inform user that kernel does not support it, with a hint about missing
>>> CONFIG_IOMMUFD.
>>>
>>> Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
>>> ---
>>> backends/iommufd.c | 5 +++++
>>> 1 file changed, 5 insertions(+)
>>>
>>> diff --git a/backends/iommufd.c b/backends/iommufd.c
>>> index acfab907c03..c4eb3de1307 100644
>>> --- a/backends/iommufd.c
>>> +++ b/backends/iommufd.c
>>> @@ -32,6 +32,11 @@ static void iommufd_backend_init(Object *obj)
>>> {
>>>      IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
>>>
>>> +    if (!g_file_test("/dev/iommu", G_FILE_TEST_EXISTS)) {
>>> +        error_setg(&error_fatal, "/dev/iommu does not exist"
>>> +                                 " (is your kernel config missing CONFIG_IOMMUFD?)");
>>
>> If hot add an iommufd object with object_add QMP, may this kill QEMU
>instance?
>
>yes.
>
>
>Unfortunately, backends are Objects. So we don't have a realize
>handler and a 'Error **' parameter :/

Maybe do it in iommufd_backend_complete()?

Thanks
Zhenzhong
Re: [PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is not available
Posted by Cédric Le Goater 2 weeks, 5 days ago
On 3/18/26 09:37, Duan, Zhenzhong wrote:
> 
> 
>> -----Original Message-----
>> From: Cédric Le Goater <clg@redhat.com>
>> Subject: Re: [PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is
>> not available
>>
>> On 3/18/26 08:36, Duan, Zhenzhong wrote:
>>>
>>>
>>>> -----Original Message-----
>>>> From: Pierrick Bouvier <pierrick.bouvier@linaro.org>
>>>> Subject: [PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is
>> not
>>>> available
>>>>
>>>> In case current kernel does not support /dev/iommu, qemu will probably
>>>> fail first because /sys/bus/pci/devices/*/vfio-dev/ is not present,
>>>> since QEMU opens it before /dev/iommu.
>>>>
>>>> Instead, report an error directly when initializing iommufd object, to
>>>> inform user that kernel does not support it, with a hint about missing
>>>> CONFIG_IOMMUFD.
>>>>
>>>> Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
>>>> ---
>>>> backends/iommufd.c | 5 +++++
>>>> 1 file changed, 5 insertions(+)
>>>>
>>>> diff --git a/backends/iommufd.c b/backends/iommufd.c
>>>> index acfab907c03..c4eb3de1307 100644
>>>> --- a/backends/iommufd.c
>>>> +++ b/backends/iommufd.c
>>>> @@ -32,6 +32,11 @@ static void iommufd_backend_init(Object *obj)
>>>> {
>>>>       IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
>>>>
>>>> +    if (!g_file_test("/dev/iommu", G_FILE_TEST_EXISTS)) {
>>>> +        error_setg(&error_fatal, "/dev/iommu does not exist"
>>>> +                                 " (is your kernel config missing CONFIG_IOMMUFD?)");
>>>
>>> If hot add an iommufd object with object_add QMP, may this kill QEMU
>> instance?
>>
>> yes.
>>
>>
>> Unfortunately, backends are Objects. So we don't have a realize
>> handler and a 'Error **' parameter :/
> 
> Maybe do it in iommufd_backend_complete()?
Yep. Perfect.

Pierrick,

Can you please move the test under the complete handler ?
May be add a  #define for "/dev/iommu" while at it.

Thanks,

C.




Re: [PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is not available
Posted by Pierrick Bouvier 2 weeks, 5 days ago
On 3/18/26 5:18 AM, Cédric Le Goater wrote:
> On 3/18/26 09:37, Duan, Zhenzhong wrote:
>>
>>
>>> -----Original Message-----
>>> From: Cédric Le Goater <clg@redhat.com>
>>> Subject: Re: [PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is
>>> not available
>>>
>>> On 3/18/26 08:36, Duan, Zhenzhong wrote:
>>>>
>>>>
>>>>> -----Original Message-----
>>>>> From: Pierrick Bouvier <pierrick.bouvier@linaro.org>
>>>>> Subject: [PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is
>>> not
>>>>> available
>>>>>
>>>>> In case current kernel does not support /dev/iommu, qemu will probably
>>>>> fail first because /sys/bus/pci/devices/*/vfio-dev/ is not present,
>>>>> since QEMU opens it before /dev/iommu.
>>>>>
>>>>> Instead, report an error directly when initializing iommufd object, to
>>>>> inform user that kernel does not support it, with a hint about missing
>>>>> CONFIG_IOMMUFD.
>>>>>
>>>>> Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
>>>>> ---
>>>>> backends/iommufd.c | 5 +++++
>>>>> 1 file changed, 5 insertions(+)
>>>>>
>>>>> diff --git a/backends/iommufd.c b/backends/iommufd.c
>>>>> index acfab907c03..c4eb3de1307 100644
>>>>> --- a/backends/iommufd.c
>>>>> +++ b/backends/iommufd.c
>>>>> @@ -32,6 +32,11 @@ static void iommufd_backend_init(Object *obj)
>>>>> {
>>>>>        IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
>>>>>
>>>>> +    if (!g_file_test("/dev/iommu", G_FILE_TEST_EXISTS)) {
>>>>> +        error_setg(&error_fatal, "/dev/iommu does not exist"
>>>>> +                                 " (is your kernel config missing CONFIG_IOMMUFD?)");
>>>>
>>>> If hot add an iommufd object with object_add QMP, may this kill QEMU
>>> instance?
>>>
>>> yes.
>>>

Good point, I didn't think about it.

>>>
>>> Unfortunately, backends are Objects. So we don't have a realize
>>> handler and a 'Error **' parameter :/
>>
>> Maybe do it in iommufd_backend_complete()?
> Yep. Perfect.
> 
> Pierrick,
> 
> Can you please move the test under the complete handler ?
> May be add a  #define for "/dev/iommu" while at it.
> 

Yes, it works as expected.
I was not sure when the "complete" phase would be called, but it's still 
triggered before trying to open vfio-dev/vfio*/dev, so all good for me!

> Thanks,
> 
> C.
> 
> 
> 

Regards,
Pierrick

Re: [PATCH 1/2] backends/iommufd.c: report error when /dev/iommu is not available
Posted by Philippe Mathieu-Daudé 2 weeks, 5 days ago
On 17/3/26 23:57, Pierrick Bouvier wrote:
> In case current kernel does not support /dev/iommu, qemu will probably
> fail first because /sys/bus/pci/devices/*/vfio-dev/ is not present,
> since QEMU opens it before /dev/iommu.
> 
> Instead, report an error directly when initializing iommufd object, to
> inform user that kernel does not support it, with a hint about missing
> CONFIG_IOMMUFD.
> 
> Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
> ---
>   backends/iommufd.c | 5 +++++
>   1 file changed, 5 insertions(+)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>