summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Christopherson <seanjc@google.com>2026-09-10 16:55:35 -0700
committerSean Christopherson <seanjc@google.com>2026-09-22 07:28:25 -0700
commitee1168fb73136dc1bd6b6f26142fdaa2399a2d60 (patch)
treef5a50123eadeec3e795b5a8e36ec2ed95955d259
parent8ab5719fbcdc96f5e636b67f14204ef7670abde1 (diff)
KVM: guest_memfd: Stub in ability to enable in-place shared<=>private conversion
Stub in global variable to enable in-place guest_memfd private<=>shared memory conversion, which will eventually be exposed to userspace via a module param, and wire up the __kvm_is_private_gfn() static call to the guest_memfd version when in-place conversion is enabled, i.e. when gmem is the sole authority on private vs. shared memory. Co-developed-by: Ackerley Tng <ackerleytng@google.com> Signed-off-by: Ackerley Tng <ackerleytng@google.com> Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev> Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com> Reviewed-by: David Hildenbrand (Arm) <david@kernel.org> Cc: Fuad Tabba <tabba@google.com> Cc: Xiaoyao Li <xiaoyao.li@intel.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Link: https://patch.msgid.link/20260910-gmem-inplace-conversion-v13-9-dd6fbf94f4e1@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
-rw-r--r--Documentation/virt/kvm/api.rst17
-rw-r--r--include/linux/kvm_host.h6
-rw-r--r--virt/kvm/guest_memfd.c26
-rw-r--r--virt/kvm/kvm_main.c12
4 files changed, 55 insertions, 6 deletions
diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index e0430cc750c9..09967e030803 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -6381,11 +6381,16 @@ mapping for userspace_addr is not required to be valid/populated at the time of
KVM_SET_USER_MEMORY_REGION2, e.g. shared memory can be lazily mapped/allocated
on-demand.
-When mapping a gfn into the guest, KVM selects shared vs. private, i.e consumes
-userspace_addr vs. guest_memfd, based on the gfn's KVM_MEMORY_ATTRIBUTE_PRIVATE
-state. At VM creation time, all memory is shared, i.e. the PRIVATE attribute
-is '0' for all gfns. Userspace can control whether memory is shared/private by
-toggling KVM_MEMORY_ATTRIBUTE_PRIVATE via KVM_SET_MEMORY_ATTRIBUTES as needed.
+When mapping a gfn into the guest, KVM selects shared vs. private, i.e. consumes
+userspace_addr vs. guest_memfd, based on the state in guest_memfd, which is the
+sole authority on private vs. shared memory. See :ref:`KVM_CREATE_GUEST_MEMFD`
+to find out more about the creation-time shared/private status.
+
+If in-place conversion is disabled, KVM selects shared vs. private based on the
+gfn's KVM_MEMORY_ATTRIBUTE_PRIVATE state. At VM creation time, all memory is
+shared, i.e. the PRIVATE attribute is '0' for all gfns. Userspace can control
+whether memory is shared/private by toggling KVM_MEMORY_ATTRIBUTE_PRIVATE via
+KVM_SET_MEMORY_ATTRIBUTES as needed.
S390:
^^^^^
@@ -6429,6 +6434,8 @@ the state of a gfn/page as needed.
The "flags" field is reserved for future extensions and must be '0'.
+.. _KVM_CREATE_GUEST_MEMFD:
+
4.142 KVM_CREATE_GUEST_MEMFD
----------------------------
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 3579feb63177..b0110143d2ff 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -2580,6 +2580,8 @@ static inline bool kvm_vm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
#endif /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */
#ifdef kvm_arch_has_private_mem
+extern bool gmem_in_place_conversion;
+
typedef bool (kvm_is_private_gfn_t)(struct kvm *kvm, gfn_t gfn);
DECLARE_STATIC_CALL(__kvm_is_private_gfn, kvm_is_private_gfn_t);
@@ -2588,6 +2590,8 @@ static inline bool kvm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
return static_call(__kvm_is_private_gfn)(kvm, gfn);
}
#else
+#define gmem_in_place_conversion false
+
static inline bool kvm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
{
return false;
@@ -2595,6 +2599,8 @@ static inline bool kvm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
#endif /* kvm_arch_has_private_mem */
#ifdef CONFIG_KVM_GUEST_MEMFD
+bool kvm_gmem_is_private_gfn(struct kvm *kvm, gfn_t gfn);
+
int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
gfn_t gfn, kvm_pfn_t *pfn, int *max_order);
#else
diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index 3b05c6443f0f..134fed0e24fa 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -517,6 +517,32 @@ static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)
return 0;
}
+bool kvm_gmem_is_private_gfn(struct kvm *kvm, gfn_t gfn)
+{
+ struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
+ struct inode *inode;
+
+ if (!slot)
+ return 0;
+
+ CLASS(gmem_get_file, file)(slot);
+ if (!file)
+ return 0;
+
+ inode = file_inode(file);
+
+ /*
+ * Rely on the maple tree's internal RCU lock to ensure a stable result.
+ * This result can become stale as soon as the lock is dropped, so the
+ * caller _must_ protect consumption of private vs. shared either by
+ * holding guest_memfd's invalidate lock for the entire duration, or by
+ * checking mmu_invalidate_retry_gfn() under mmu_lock to serialize
+ * against concurrent attribute updates.
+ */
+ return kvm_gmem_is_private_mem(inode, kvm_gmem_get_index(slot, gfn));
+}
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_gmem_is_private_gfn);
+
static struct file_operations kvm_gmem_fops = {
.mmap = kvm_gmem_mmap,
.open = generic_file_open,
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 19098a51c35d..2532d1252a10 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -102,6 +102,10 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(halt_poll_ns_shrink);
static bool __ro_after_init allow_unsafe_mappings;
module_param(allow_unsafe_mappings, bool, 0444);
+#ifdef kvm_arch_has_private_mem
+bool __ro_after_init gmem_in_place_conversion = false;
+#endif
+
/*
* Ordering of locks:
*
@@ -2442,6 +2446,9 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm,
static u64 kvm_supported_vm_mem_attributes(struct kvm *kvm)
{
#ifdef kvm_arch_has_private_mem
+ if (gmem_in_place_conversion)
+ return 0;
+
if (!kvm || kvm_arch_has_private_mem(kvm))
return KVM_MEMORY_ATTRIBUTE_PRIVATE;
#endif
@@ -2653,8 +2660,11 @@ EXPORT_STATIC_CALL_GPL(__kvm_is_private_gfn);
static void kvm_init_memory_attributes(void)
{
+ if (gmem_in_place_conversion)
+ static_call_update(__kvm_is_private_gfn, kvm_gmem_is_private_gfn);
#ifdef CONFIG_KVM_VM_MEMORY_ATTRIBUTES
- static_call_update(__kvm_is_private_gfn, kvm_vm_is_private_gfn);
+ else
+ static_call_update(__kvm_is_private_gfn, kvm_vm_is_private_gfn);
#endif
}
#else