Skip to content

Commit e563592

Browse files
committed
KVM: Make KVM_MEM_GUEST_MEMFD mutually exclusive with KVM_MEM_READONLY
Disallow creating read-only memslots that support GUEST_MEMFD, as GUEST_MEMFD is fundamentally incompatible with KVM's semantics for read-only memslots. Read-only memslots allow the userspace VMM to emulate option ROMs by filling the backing memory with readable, executable code and data, while triggering emulated MMIO on writes. GUEST_MEMFD doesn't currently support writes from userspace and KVM doesn't support emulated MMIO on private accesses, i.e. the guest can only ever read zeros, and writes will always be treated as errors. Cc: Fuad Tabba <tabba@google.com> Cc: Michael Roth <michael.roth@amd.com> Cc: Isaku Yamahata <isaku.yamahata@gmail.com> Cc: Yu Zhang <yu.c.zhang@linux.intel.com> Cc: Chao Peng <chao.p.peng@linux.intel.com> Fixes: a7800aa ("KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory") Link: https://lore.kernel.org/r/20240222190612.2942589-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent c48617f commit e563592

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

virt/kvm/kvm_main.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,13 @@ static int check_memory_region_flags(struct kvm *kvm,
16151615
valid_flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
16161616

16171617
#ifdef __KVM_HAVE_READONLY_MEM
1618-
valid_flags |= KVM_MEM_READONLY;
1618+
/*
1619+
* GUEST_MEMFD is incompatible with read-only memslots, as writes to
1620+
* read-only memslots have emulated MMIO, not page fault, semantics,
1621+
* and KVM doesn't allow emulated MMIO for private memory.
1622+
*/
1623+
if (!(mem->flags & KVM_MEM_GUEST_MEMFD))
1624+
valid_flags |= KVM_MEM_READONLY;
16191625
#endif
16201626

16211627
if (mem->flags & ~valid_flags)

0 commit comments

Comments
 (0)