KVM Archive on lore.kernel.org
 help / color / Atom feed
From: Sean Christopherson <sean.j.christopherson@intel.com>
To: "Paolo Bonzini" <pbonzini@redhat.com>,
	"Radim Krčmář" <rkrcmar@redhat.com>
Cc: Sean Christopherson <sean.j.christopherson@intel.com>,
	Vitaly Kuznetsov <vkuznets@redhat.com>,
	Wanpeng Li <wanpengli@tencent.com>,
	Jim Mattson <jmattson@google.com>, Joerg Roedel <joro@8bytes.org>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 00/16] KVM: x86: MMU page fault clean-up
Date: Fri,  6 Dec 2019 15:57:13 -0800
Message-ID: <20191206235729.29263-1-sean.j.christopherson@intel.com> (raw)

The original purpose of this series was to call thp_adjust() from
__direct_map() and FNAME(fetch) to eliminate a page refcounting quirk[*].
Before doing that, I wanted to clean up the large page handling so that
the map/fetch funtions weren't being passed multiple booleans that tracked
the same basic info.  While trying to decipher all the the interactions,
I stumbled across a handful of fun things:

  - 32-bit KVM w/ TDP is completely broken with respect to 64-bit GPAs due
    to the page fault handlers and all related flows dropping bits 63:32
    of the GPA.  As a result, KVM inserts the wrong GPA and the guest hangs
    because it generates EPT/NPT faults until it's killed.

  - The TDP and non-paging page fault flows are identical except for
    one-off constraints on guest page size.

  - The !VALID_PAGE(root_hpa) checks in the page fault flows are bogus.
    They were added a few years ago to "fix" a nVMX bug and are no longer
    needed now that nVMX is in much better shape.

Patch 1 fixes the 32-bit KVM w/ TDP issue.  More details below.

Patches 2-12 are 99% refactoring to merge TDP and non-paging page fault
handling, and to do the thp_adjust() move.  These are basically nops from
a functional perspective.  There are technically functional changes in a
few patches, but they are very superficial and in theory won't be
observable in normal usage.

Patches 13-16 add WARNs on the !VALID_PAGE(root_hpa) checks to make it
clear that root_hpa is expected to be valid when handling page faults,
e.g. for the longest time I thought KVM relied on the checks in map/fetch
to correctly handle kvm_mmu_zap_all().


32-bit KVM w/ TDP:

I marked this patch for stable because it's obviously a bug fix, but I'm
entirely not sure we want to backport the fix.  Obviously no userspace VMM
is actually exposing 64-bit GPAs to its guests, i.e. odds are this won't
actually fix any real world use cases.  And, the scope of the changes are
likely going to make backporting a pain.  But, on the other hand, if it's
not backported then future bug fixes in related code are likely to
conflict, and it does fix the case where a buggy guest kernel accesses a
non-existent 64-bit GPA (crashes instead of hanging indefinitely).

I'm also not confident I found all the cases where KVM is truncating the
GPA.  AFAIK, 32-bit Qemu simply doesn't support 64-bit GPAs.  To confirm
the bug and verify the fix, I hacked KVM and the guest kernel to generate
64-bit GPAs when remapping MMIO, which covers a tiny fragment of KVM.

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index fa46fbed60013..49a59bcb32117 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -5737,7 +5737,7 @@ static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
        vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
        vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
        vcpu->run->exit_reason = KVM_EXIT_MMIO;
-       vcpu->run->mmio.phys_addr = gpa;
+       vcpu->run->mmio.phys_addr = gpa & 0xffffffffull;
 
        return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
 }


diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c
index b9c78f3bcd673..e22f254987bea 100644
--- a/arch/x86/mm/ioremap.c
+++ b/arch/x86/mm/ioremap.c
@@ -184,7 +184,10 @@ static void __iomem *__ioremap_caller(resource_size_t phys_addr,
        if (kernel_map_sync_memtype(phys_addr, size, pcm))
                goto err_free_area;
 
-       if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot))
+       BUG_ON(!boot_cpu_data.x86_phys_bits);
+
+       if (ioremap_page_range(vaddr, vaddr + size,
+                              phys_addr | BIT_ULL(boot_cpu_data.x86_phys_bits - 1), prot))
                goto err_free_area;
 
        ret_addr = (void __iomem *) (vaddr + offset);

[*] https://lkml.kernel.org/r/20191126174603.GB22233@linux.intel.com

Sean Christopherson (16):
  KVM: x86: Use gpa_t for cr2/gpa to fix TDP support on 32-bit KVM
  KVM: x86/mmu: Move definition of make_mmu_pages_available() up
  KVM: x86/mmu: Fold nonpaging_map() into nonpaging_page_fault()
  KVM: x86/mmu: Move nonpaging_page_fault() below try_async_pf()
  KVM: x86/mmu: Refactor handling of cache consistency with TDP
  KVM: x86/mmu: Refactor the per-slot level calculation in
    mapping_level()
  KVM: x86/mmu: Refactor handling of forced 4k pages in page faults
  KVM: x86/mmu: Incorporate guest's page level into max level for shadow
    MMU
  KVM: x86/mmu: Persist gfn_lpage_is_disallowed() to max_level
  KVM: x86/mmu: Rename lpage_disallowed to account_disallowed_nx_lpage
  KVM: x86/mmu: Consolidate tdp_page_fault() and nonpaging_page_fault()
  KVM: x86/mmu: Move transparent_hugepage_adjust() above __direct_map()
  KVM: x86/mmu: Move calls to thp_adjust() down a level
  KVM: x86/mmu: Move root_hpa validity checks to top of page fault
    handler
  KVM: x86/mmu: WARN on an invalid root_hpa
  KVM: x86/mmu: WARN if root_hpa is invalid when handling a page fault

 arch/x86/include/asm/kvm_host.h |   8 +-
 arch/x86/kvm/mmu/mmu.c          | 438 ++++++++++++++------------------
 arch/x86/kvm/mmu/paging_tmpl.h  |  58 +++--
 arch/x86/kvm/mmutrace.h         |  12 +-
 arch/x86/kvm/x86.c              |  40 ++-
 arch/x86/kvm/x86.h              |   2 +-
 include/linux/kvm_host.h        |   6 +-
 virt/kvm/async_pf.c             |  10 +-
 8 files changed, 259 insertions(+), 315 deletions(-)

-- 
2.24.0


             reply index

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-06 23:57 Sean Christopherson [this message]
2019-12-06 23:57 ` [PATCH 01/16] KVM: x86: Use gpa_t for cr2/gpa to fix TDP support on 32-bit KVM Sean Christopherson
2019-12-06 23:57 ` [PATCH 02/16] KVM: x86/mmu: Move definition of make_mmu_pages_available() up Sean Christopherson
2019-12-06 23:57 ` [PATCH 03/16] KVM: x86/mmu: Fold nonpaging_map() into nonpaging_page_fault() Sean Christopherson
2019-12-06 23:57 ` [PATCH 04/16] KVM: x86/mmu: Move nonpaging_page_fault() below try_async_pf() Sean Christopherson
2019-12-06 23:57 ` [PATCH 05/16] KVM: x86/mmu: Refactor handling of cache consistency with TDP Sean Christopherson
2019-12-06 23:57 ` [PATCH 06/16] KVM: x86/mmu: Refactor the per-slot level calculation in mapping_level() Sean Christopherson
2019-12-06 23:57 ` [PATCH 07/16] KVM: x86/mmu: Refactor handling of forced 4k pages in page faults Sean Christopherson
2019-12-06 23:57 ` [PATCH 08/16] KVM: x86/mmu: Incorporate guest's page level into max level for shadow MMU Sean Christopherson
2019-12-06 23:57 ` [PATCH 09/16] KVM: x86/mmu: Persist gfn_lpage_is_disallowed() to max_level Sean Christopherson
2019-12-06 23:57 ` [PATCH 10/16] KVM: x86/mmu: Rename lpage_disallowed to account_disallowed_nx_lpage Sean Christopherson
2019-12-06 23:57 ` [PATCH 11/16] KVM: x86/mmu: Consolidate tdp_page_fault() and nonpaging_page_fault() Sean Christopherson
2019-12-06 23:57 ` [PATCH 12/16] KVM: x86/mmu: Move transparent_hugepage_adjust() above __direct_map() Sean Christopherson
2019-12-06 23:57 ` [PATCH 13/16] KVM: x86/mmu: Move calls to thp_adjust() down a level Sean Christopherson
2019-12-06 23:57 ` [PATCH 14/16] KVM: x86/mmu: Move root_hpa validity checks to top of page fault handler Sean Christopherson
2019-12-06 23:57 ` [PATCH 15/16] KVM: x86/mmu: WARN on an invalid root_hpa Sean Christopherson
2019-12-06 23:57 ` [PATCH 16/16] KVM: x86/mmu: WARN if root_hpa is invalid when handling a page fault Sean Christopherson
2019-12-09 15:31 ` [PATCH 00/16] KVM: x86: MMU page fault clean-up Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191206235729.29263-1-sean.j.christopherson@intel.com \
    --to=sean.j.christopherson@intel.com \
    --cc=jmattson@google.com \
    --cc=joro@8bytes.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=rkrcmar@redhat.com \
    --cc=vkuznets@redhat.com \
    --cc=wanpengli@tencent.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

KVM Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/kvm/0 kvm/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kvm kvm/ https://lore.kernel.org/kvm \
		kvm@vger.kernel.org
	public-inbox-index kvm

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.kvm


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git