Add a capability to let userspace discover whether guest_memfd supports
removing its folios from the direct map. Support depends on guest_memfd
itself being supported, but also on whether KVM can manipulate the
direct map at page granularity at all (possible most of the time, just
arm64 is a notable outlier where its impossible if the direct map has
been setup using hugepages, as arm64 cannot break these apart due to
break-before-make semantics).

Signed-off-by: Patrick Roy <roy...@amazon.co.uk>
---
 include/uapi/linux/kvm.h | 1 +
 virt/kvm/kvm_main.c      | 5 +++++
 2 files changed, 6 insertions(+)

diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 4654c01a0a01..fb02a93546d8 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -930,6 +930,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237
 #define KVM_CAP_X86_GUEST_MODE 238
 #define KVM_CAP_GMEM_SHARED_MEM 239
+#define KVM_CAP_GMEM_NO_DIRECT_MAP 240
 
 struct kvm_irq_routing_irqchip {
        __u32 irqchip;
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 3e40acb9f5c0..32ca1c921ab0 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -65,6 +65,7 @@
 #include <trace/events/kvm.h>
 
 #include <linux/kvm_dirty_ring.h>
+#include <linux/set_memory.h>
 
 
 /* Worst case buffer size needed for holding an integer. */
@@ -4823,6 +4824,10 @@ static int kvm_vm_ioctl_check_extension_generic(struct 
kvm *kvm, long arg)
                return kvm_supported_mem_attributes(kvm);
 #endif
 #ifdef CONFIG_KVM_PRIVATE_MEM
+       case KVM_CAP_GMEM_NO_DIRECT_MAP:
+               if (!can_set_direct_map())
+                       return false;
+               fallthrough;
        case KVM_CAP_GUEST_MEMFD:
                return !kvm || kvm_arch_has_private_mem(kvm);
 #endif
-- 
2.48.1


Reply via email to