@@ -933,6 +933,7 @@ struct kvm_enable_cap {
#define KVM_CAP_PRE_FAULT_MEMORY 236
#define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237
#define KVM_CAP_X86_GUEST_MODE 238
+#define KVM_CAP_GMEM_SHARED_MEM 239
struct kvm_irq_routing_irqchip {
__u32 irqchip;
@@ -308,6 +308,13 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn)
}
#ifdef CONFIG_KVM_GMEM_SHARED_MEM
+static bool kvm_gmem_is_shared(struct file *file, pgoff_t pgoff)
+{
+ struct kvm_gmem *gmem = file->private_data;
+
+ return kvm_arch_gmem_supports_shared_mem(gmem->kvm);
+}
+
static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf)
{
struct inode *inode = file_inode(vmf->vma->vm_file);
@@ -327,6 +334,12 @@ static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf)
goto out_folio;
}
+ /* Must be called with folio lock held, i.e., after kvm_gmem_get_folio() */
+ if (!kvm_gmem_is_shared(vmf->vma->vm_file, vmf->pgoff)) {
+ ret = VM_FAULT_SIGBUS;
+ goto out_folio;
+ }
+
if (WARN_ON_ONCE(folio_test_guestmem(folio))) {
ret = VM_FAULT_SIGBUS;
goto out_folio;
@@ -4792,6 +4792,10 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
#ifdef CONFIG_KVM_PRIVATE_MEM
case KVM_CAP_GUEST_MEMFD:
return !kvm || kvm_arch_has_private_mem(kvm);
+#endif
+#ifdef CONFIG_KVM_GMEM_SHARED_MEM
+ case KVM_CAP_GMEM_SHARED_MEM:
+ return !kvm || kvm_arch_gmem_supports_shared_mem(kvm);
#endif
default:
break;
Add the KVM capability KVM_CAP_GMEM_SHARED_MEM, which indicates that the VM supports shared memory in guest_memfd, or that the host can create VMs that support shared memory. Supporting shared memory implies that memory can be mapped when shared with the host. For now, this checks only whether the VM type supports sharing guest_memfd backed memory. In the future, it will be expanded to check whether the specific memory address is shared with the host. Signed-off-by: Fuad Tabba <tabba@google.com> --- include/uapi/linux/kvm.h | 1 + virt/kvm/guest_memfd.c | 13 +++++++++++++ virt/kvm/kvm_main.c | 4 ++++ 3 files changed, 18 insertions(+)