KVM: x86: clamp host mapping level to max_level in kvm_mmu_max_mapping_level
authorPaolo Bonzini <pbonzini@redhat.com>
Fri, 6 Aug 2021 11:05:58 +0000 (07:05 -0400)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 20 Aug 2021 20:06:41 +0000 (16:06 -0400)
commitec607a564f70519b340f7eb4cfc0f4a6b55285ac
treeae9d397e6f5841ac05b194d6aa005c9627776f63
parent85cc207b8e07df8ee05ee83115c9086aef20a1f5
KVM: x86: clamp host mapping level to max_level in kvm_mmu_max_mapping_level

This change started as a way to make kvm_mmu_hugepage_adjust a bit simpler,
but it does fix two bugs as well.

One bug is in zapping collapsible PTEs.  If a large page size is
disallowed but not all of them, kvm_mmu_max_mapping_level will return the
host mapping level and the small PTEs will be zapped up to that level.
However, if e.g. 1GB are prohibited, we can still zap 4KB mapping and
preserve the 2MB ones. This can happen for example when NX huge pages
are in use.

The second would happen when userspace backs guest memory
with a 1gb hugepage but only assign a subset of the page to
the guest.  1gb pages would be disallowed by the memslot, but
not 2mb.  kvm_mmu_max_mapping_level() would fall through to the
host_pfn_mapping_level() logic, see the 1gb hugepage, and map the whole
thing into the guest.

Fixes: 2f57b7051fe8 ("KVM: x86/mmu: Persist gfn_lpage_is_disallowed() to max_level")
Cc: stable@vger.kernel.org
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/mmu/mmu.c