Skip to content

Commit 26cf445

Browse files
committed
KVM: x86/mmu: Track PRIVATE impact on hugepage mappings for all memslots
Track the effects of private attributes on potential hugepage mappings if the VM supports private memory, i.e. even if the target memslot can only ever be mapped shared. If userspace configures a chunk of memory as private, KVM must not allow that memory to be mapped shared regardless of whether or not the *current* memslot can be mapped private. E.g. if the guest accesses a private range using a shared memslot, then KVM must exit to userspace. Fixes: 5bb0b4e ("KVM: x86: Disallow hugepages when memory attributes are mixed") Link: https://lore.kernel.org/r/20230921203331.3746712-8-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 1c297b8 commit 26cf445

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

arch/x86/kvm/mmu/mmu.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7314,10 +7314,12 @@ bool kvm_arch_post_set_memory_attributes(struct kvm *kvm,
73147314
lockdep_assert_held(&kvm->slots_lock);
73157315

73167316
/*
7317-
* KVM x86 currently only supports KVM_MEMORY_ATTRIBUTE_PRIVATE, skip
7318-
* the slot if the slot will never consume the PRIVATE attribute.
7317+
* Calculate which ranges can be mapped with hugepages even if the slot
7318+
* can't map memory PRIVATE. KVM mustn't create a SHARED hugepage over
7319+
* a range that has PRIVATE GFNs, and conversely converting a range to
7320+
* SHARED may now allow hugepages.
73197321
*/
7320-
if (!kvm_slot_can_be_private(slot))
7322+
if (WARN_ON_ONCE(!kvm_arch_has_private_mem(kvm)))
73217323
return false;
73227324

73237325
/*
@@ -7372,7 +7374,7 @@ void kvm_mmu_init_memslot_memory_attributes(struct kvm *kvm,
73727374
{
73737375
int level;
73747376

7375-
if (!kvm_slot_can_be_private(slot))
7377+
if (!kvm_arch_has_private_mem(kvm))
73767378
return;
73777379

73787380
for (level = PG_LEVEL_2M; level <= KVM_MAX_HUGEPAGE_LEVEL; level++) {

0 commit comments

Comments
 (0)