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

Michael Tokarev posted 1 patch 3 days, 17 hours ago
There is a newer version of this series
backends/hostmem-memfd.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
[PATCH for-11.2] hostmem: allow building without KVM support
Posted by Michael Tokarev 3 days, 17 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

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..3164dd6198c 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");
+        fd = -ENOSYS;
+#endif
     } else {
         fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
                                m->hugetlb, m->hugetlbsize, m->seal ?
-- 
2.47.3
Re: [PATCH for-11.2] hostmem: allow building without KVM support
Posted by Paolo Bonzini 3 days, 15 hours ago
Queued, thanks.

Paolo
Re: [PATCH for-11.2] hostmem: allow building without KVM support
Posted by Michael Tokarev 3 days, 14 hours ago
On 11/19/26 16:15, Paolo Bonzini wrote:
> Queued, thanks.

Please queue the v2 which is with a complete commit message :)

Thanks!

/mjt
Re: [PATCH for-11.2] hostmem: allow building without KVM support
Posted by Xiaoyao Li 3 days, 16 hours ago
On 11/19/2026 7:10 PM, 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

It reads the sentence doesn't finish...

> 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..3164dd6198c 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");
> +        fd = -ENOSYS;
> +#endif

There is stub implemented in kvm-stub.c. Doesn't it work?

>      } else {
>          fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
>                                 m->hugetlb, m->hugetlbsize, m->seal ?
Re: [PATCH for-11.2] hostmem: allow building without KVM support
Posted by Michael Tokarev 3 days, 16 hours ago
On 11/19/26 14:26, Xiaoyao Li wrote:
> On 11/19/2026 7:10 PM, 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
> 
> It reads the sentence doesn't finish...

Yeah, one line slipped out, which explains..

>> 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..3164dd6198c 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");
>> +        fd = -ENOSYS;
>> +#endif
> 
> There is stub implemented in kvm-stub.c. Doesn't it work?

..which explains why the stub doesn't work: we still need to provide
defines for GUEST_MEMFD_FLAG_* which are normally declared in <linux/kvm.h>.
So we still need the ifdef'fery one way or another, and in my view it's better
to #ifdef the function call to provide a more detailed error message.

Thanks,

/mjt

>>       } else {
>>           fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
>>                                  m->hugetlb, m->hugetlbsize, m->seal ?
>
Re: [PATCH for-11.2] hostmem: allow building without KVM support
Posted by Michael Tokarev 3 days, 17 hours ago
On 11/19/26 14:10, 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
> 
> 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..3164dd6198c 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");
> +        fd = -ENOSYS;

this better should be 'return false;' like above.  But it doesn't really
matter since the next code block checks fd for being <0 and returns false.

> +#endif
>       } else {
>           fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
>                                  m->hugetlb, m->hugetlbsize, m->seal ?