[PATCH v2 for-11.2] hostmem: allow building without KVM support

Michael Tokarev posted 1 patch 3 days, 16 hours ago
backends/hostmem-memfd.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
[PATCH v2 for-11.2] hostmem: allow building without KVM support
Posted by Michael Tokarev 3 days, 16 hours ago
On architectures where KVM is not available and so including <linux/kvm.h>
does not work, buildnig backends/hostmem-memfd.c fails since 7ca43d7ad02f:

 In file included from backends/hostmem-memfd.c:23:
 linux-headers/linux/kvm.h:16:10: fatal error: asm/kvm.h: No such file or directory
   16 | #include <asm/kvm.h>

Do not include <linux/kvm.h> because "system/kvm.h" already includes it for us
when KVM is enabled, and guard the actual code using memfd definitions with
<linux/kvm.h> too.  When using #ifdef like this, we can also provide a more
clear error message.

Fixes: 7ca43d7ad02f ("hostmem: Support fully shared guest memfd to back a VM")
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
---
 backends/hostmem-memfd.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c
index 46489212a23..698ec930a5c 100644
--- a/backends/hostmem-memfd.c
+++ b/backends/hostmem-memfd.c
@@ -20,7 +20,6 @@
 #include "qom/object.h"
 #include "migration/cpr.h"
 #include "system/kvm.h"
-#include <linux/kvm.h>
 #include "qapi/qapi-type-infos-common.h"
 
 OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendMemfd, MEMORY_BACKEND_MEMFD)
@@ -71,10 +70,15 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
             return false;
         }
 
+#ifdef CONFIG_KVM
         fd = kvm_create_guest_memfd(backend->size,
                                     GUEST_MEMFD_FLAG_MMAP |
                                     GUEST_MEMFD_FLAG_INIT_SHARED,
                                     errp);
+#else
+        error_setg(errp, "guest-memfd=on requires KVM which is not enabled");
+        return false;
+#endif
     } else {
         fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
                                m->hugetlb, m->hugetlbsize, m->seal ?
-- 
2.47.3
Re: [PATCH v2 for-11.2] hostmem: allow building without KVM support
Posted by BALATON Zoltan 3 days, 11 hours ago
On Thu, 19 Nov 2026, Michael Tokarev wrote:
> On architectures where KVM is not available and so including <linux/kvm.h>
> does not work, buildnig backends/hostmem-memfd.c fails since 7ca43d7ad02f:

Typo: buildnig

Regards,
BALATON Zoltan
Re: [PATCH v2 for-11.2] hostmem: allow building without KVM support
Posted by Philippe Mathieu-Daudé 3 days, 16 hours ago
On 2026-11-19 12:33, Michael Tokarev wrote:
> On architectures where KVM is not available and so including <linux/kvm.h>
> does not work, buildnig backends/hostmem-memfd.c fails since 7ca43d7ad02f:
> 
>   In file included from backends/hostmem-memfd.c:23:
>   linux-headers/linux/kvm.h:16:10: fatal error: asm/kvm.h: No such file or directory
>     16 | #include <asm/kvm.h>
> 
> Do not include <linux/kvm.h> because "system/kvm.h" already includes it for us
> when KVM is enabled, and guard the actual code using memfd definitions with
> <linux/kvm.h> too.  When using #ifdef like this, we can also provide a more
> clear error message.
> 
> Fixes: 7ca43d7ad02f ("hostmem: Support fully shared guest memfd to back a VM")
> Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
> ---
>   backends/hostmem-memfd.c | 6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c
> index 46489212a23..698ec930a5c 100644
> --- a/backends/hostmem-memfd.c
> +++ b/backends/hostmem-memfd.c
> @@ -20,7 +20,6 @@
>   #include "qom/object.h"
>   #include "migration/cpr.h"
>   #include "system/kvm.h"
> -#include <linux/kvm.h>

OK.

>   #include "qapi/qapi-type-infos-common.h"
>   
>   OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendMemfd, MEMORY_BACKEND_MEMFD)
> @@ -71,10 +70,15 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
>               return false;
>           }
>   
> +#ifdef CONFIG_KVM

We prefer using runtime check (probably providing a stub):

-- >8 --
diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c
index 666593ff6c..d39bc71d25 100644
--- a/backends/hostmem-memfd.c
+++ b/backends/hostmem-memfd.c
@@ -62,7 +62,10 @@ memfd_backend_memory_alloc(HostMemoryBackend 
*backend, Error **errp)
           * NOTE: guest-memfd ignores seal=on/off because it always
           * implicitly seals the FD by definition.
           */
-        if (!backend->share) {
+        if (!kvm_enabled()) {
+            error_setg(errp, "guest-memfd=on requires KVM which is not 
enabled");
+            return false;
+        } else if (!backend->share) {
              error_setg(errp, "guest-memfd=on must be used with share=on");
              return false;
          } else if (m->hugetlb) {
---

>           fd = kvm_create_guest_memfd(backend->size,
>                                       GUEST_MEMFD_FLAG_MMAP |
>                                       GUEST_MEMFD_FLAG_INIT_SHARED,
>                                       errp);
> +#else
> +        error_setg(errp, "guest-memfd=on requires KVM which is not enabled");
> +        return false;
> +#endif
>       } else {
>           fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
>                                  m->hugetlb, m->hugetlbsize, m->seal ?
Re: [PATCH v2 for-11.2] hostmem: allow building without KVM support
Posted by Michael Tokarev 3 days, 16 hours ago
On 11/19/26 14:42, Philippe Mathieu-Daudé wrote:
> On 2026-11-19 12:33, Michael Tokarev wrote:
...
> We prefer using runtime check (probably providing a stub):
> 
> -- >8 --
> diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c

> +        if (!kvm_enabled()) {
> +            error_setg(errp, "guest-memfd=on requires KVM which is not enabled");
> +            return false;
> +        } else if (!backend->share) {

>>           fd = kvm_create_guest_memfd(backend->size,
>>                                       GUEST_MEMFD_FLAG_MMAP |
>>                                       GUEST_MEMFD_FLAG_INIT_SHARED,
>>                                       errp);

This will fail the same way because GUEST_MEMFD_FLAG_MMAP is still not defined.
This is why I especially made this #ifdef the way I did.

Sure I can add

#ifndef GUEST_MEMFD_FLAG_MMAP  (if you prefer not to use #ifndef CONFIG_KVM)
# define GUEST_MEMFD_FLAG_MMAP 0
# define GUEST_MEMFD_FLAG_INIT_SHARED 0
#endif

but it much uglier and less understandable than #ifndef CONFIG_KVM.

Thanks,

/mjt

Re: [PATCH v2 for-11.2] hostmem: allow building without KVM support
Posted by Philippe Mathieu-Daudé 3 days, 16 hours ago
On 2026-11-19 12:46, Michael Tokarev wrote:
> On 11/19/26 14:42, Philippe Mathieu-Daudé wrote:
>> On 2026-11-19 12:33, Michael Tokarev wrote:
> ...
>> We prefer using runtime check (probably providing a stub):
>>
>> -- >8 --
>> diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c
> 
>> +        if (!kvm_enabled()) {
>> +            error_setg(errp, "guest-memfd=on requires KVM which is 
>> not enabled");
>> +            return false;
>> +        } else if (!backend->share) {
> 
>>>           fd = kvm_create_guest_memfd(backend->size,
>>>                                       GUEST_MEMFD_FLAG_MMAP |
>>>                                       GUEST_MEMFD_FLAG_INIT_SHARED,
>>>                                       errp);
> 
> This will fail the same way because GUEST_MEMFD_FLAG_MMAP is still not 
> defined.
> This is why I especially made this #ifdef the way I did.
> 
> Sure I can add
> 
> #ifndef GUEST_MEMFD_FLAG_MMAP  (if you prefer not to use #ifndef 
> CONFIG_KVM)
> # define GUEST_MEMFD_FLAG_MMAP 0
> # define GUEST_MEMFD_FLAG_INIT_SHARED 0
> #endif
> 
> but it much uglier and less understandable than #ifndef CONFIG_KVM.

As you prefer. That would be the first accelerator check in backends/,
I'm not sure we want it (then they are hard to remove):

$ git grep CONFIG_KVM backends/
$

> 
> Thanks,
> 
> /mjt


Re: [PATCH v2 for-11.2] hostmem: allow building without KVM support
Posted by Michael Tokarev 3 days, 14 hours ago
On 11/19/26 14:54, Philippe Mathieu-Daudé wrote:
> On 2026-11-19 12:46, Michael Tokarev wrote:

>> Sure I can add
>>
>> #ifndef GUEST_MEMFD_FLAG_MMAP  (if you prefer not to use #ifndef CONFIG_KVM)
>> # define GUEST_MEMFD_FLAG_MMAP 0
>> # define GUEST_MEMFD_FLAG_INIT_SHARED 0
>> #endif
>>
>> but it much uglier and less understandable than #ifndef CONFIG_KVM.
> 
> As you prefer. That would be the first accelerator check in backends/,
> I'm not sure we want it (then they are hard to remove):
Well, sure, -- what would be the better way here?

I don't have particular preferences, I just want this bug to be fixed
(without sacrificing the future development ofc).

Thanks,

/mjt