On 12/13/2025 1:41 AM, Peter Xu wrote:
please add the following check as I commented in v1:
if (!kvm_enabled()) {
error_setg(errp, "in-place guest-memfd requires KVM");
return false;
}
IMHO it's redundant to set here, when kvm not enabled,
kvm_create_guest_memfd() should be a stub.
No. The KVM stub is for the case where KVM is disable at compile time.
The kvm_enabled() check here is for the case where users use different
accelerators other than KVM, e.g., -accel tcg.
I thought the kvm-compiled case is already covered, at least the flags will
be 0 here:
if (!kvm_guest_memfd_supported) {
error_setg(errp, "KVM does not support guest_memfd");
return -1;
}
So I can change below stub patch into this one, would it look better (so
that we'll provide explicit errors for all cases)?
It looks good to me.
===8<===
commit 70012ceb70d3ffe624db33a8aeaaec581c7b4ccd
Author: Peter Xu<[email protected]>
Date: Thu Dec 11 11:19:44 2025 -0500
kvm: Provide explicit error for kvm_create_guest_memfd()
So that there will be a verbal string returned when kvm not enabled, or kvm
not compiled.
Signed-off-by: Peter Xu<[email protected]>
---
accel/kvm/kvm-all.c | 5 +++++
accel/stubs/kvm-stub.c | 1 +
2 files changed, 6 insertions(+)
diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
index 68d57c1af0..c32fbcf9cc 100644
--- a/accel/kvm/kvm-all.c
+++ b/accel/kvm/kvm-all.c
@@ -4492,6 +4492,11 @@ int kvm_create_guest_memfd(uint64_t size, uint64_t
flags, Error **errp)
.flags = flags,
};
+ if (!kvm_enabled()) {
+ error_setg(errp, "guest-memfd requires KVM accelerator");
+ return -1;
+ }
+
if (!kvm_guest_memfd_supported) {
error_setg(errp, "KVM does not support guest_memfd");
return -1;
diff --git a/accel/stubs/kvm-stub.c b/accel/stubs/kvm-stub.c
index 73f04eb589..01b1d6285e 100644
--- a/accel/stubs/kvm-stub.c
+++ b/accel/stubs/kvm-stub.c
@@ -123,6 +123,7 @@ bool kvm_hwpoisoned_mem(void)
int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp)
{
+ error_setg(errp, "KVM is not enabled");
return -ENOSYS;
}
===8<===
Thanks,