mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Sasha Levin <sashal@kernel.org>, Sean Christopherson <seanjc@google.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>,
	Kyle Zeng <kylebot@openai.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] KVM: x86: Don't reuse the GPA of a fault on the guest's page tables
Date: Tue, 17 Nov 2026 19:50:13 +0100	[thread overview]
Message-ID: <7b4beea2-2bc9-44d8-be6a-ecfe678091dc@redhat.com> (raw)
In-Reply-To: <20261027191914.2341911-1-sashal@kernel.org>

On 10/27/26 20:19, Sasha Levin wrote:
> DecodeAssist provides the instruction bytes, the read of 0xc0000000 has
> the same page offset as 0x180000, and the emulator turns it into an MMIO
> read of 0x180000:
> 
>    ==== Test Assertion Failure ====
>      set_memory_region_test.c:83: run->mmio.phys_addr == MEM_REGION_GPA
>      ...
>      Unexpected exit mmio address = 0x180000
> 
> If the vCPU is at the next instruction instead, the #NPF happens on the
> code fetch, DecodeAssist provides no bytes, KVM fails to fetch the
> instruction itself and injects a #PF, and the guest shuts down as the
> test expects.  KernelCI sees the failure in about half of the runs on a
> DL345 Gen11 (EPYC).
> 
> The CPU walks the guest's page tables with write accesses, so such faults
> also set EMULTYPE_PF_WRITE, and a write would be redirected the same way.
> 
> Tell the emulator when the fault occurred on the guest's page tables and
> don't use the GPA for the access then, so that the access is translated
> through the guest's page tables.  KVM can't walk the encrypted page tables
                                     ^^^^^^^^^

This is a separate bug; something like "Also, KVM can't walk..." would be
better - but actually a separate patch would be even better.

The non-SEV part looks good.  I'll wait for Sean to shout that it isn't,
or otherwise apply it and send out the SEV patch below.
> +	/*
> +	 * Don't emulate if the #NPF occurred while walking the guest's page
> +	 * tables.  The GPA is that of a guest paging structure, not of the
> +	 * access, and KVM can't walk the guest's encrypted page tables to
> +	 * translate the access itself.  Resume the guest, see below.
> +	 */
> +	if (emul_type & EMULTYPE_PF_GUEST_PAGE)
> +		goto resume_guest;

Here I would return X86EMUL_UNHANDLEABLE, which causes an internal-error
exit or #UD.  Also, even though it's mostly aesthetic, SEV only gets
emulation for #PF and the code is already consulting exit_info_1.  In
fact it is already checking PFERR_GUEST_PAGE_MASK but in the wrong order.
Something like this will do:

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index af0deb198a84..4e0f5a8c8275 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -5513,19 +5513,29 @@ static int svm_check_emulate_instruction(
  	}
  
  	/*
-	 * Emulate for SEV guests if the insn buffer is not empty.  The buffer
-	 * will be empty if the DecodeAssist microcode cannot fetch bytes for
-	 * the faulting instruction because the code fetch itself faulted, e.g.
-	 * the guest attempted to fetch from emulated MMIO or a guest page
-	 * table used to translate CS:RIP resides in emulated MMIO.
+	 * Under SEV, page tables are encrypted and cannot be read by the
+	 * hypervisor, so there is no way to resolve a fault on page tables.
  	 */
+	error_code = svm->vmcb->control.exit_info_1;
+	if (error_code & PFERR_GUEST_PAGE_MASK)
+		return X86EMUL_UNHANDLEABLE;
+
  	if (likely(insn_len))
  		return X86EMUL_CONTINUE;
  
  	/*
-	 * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
+	 * The insn buffer was empty; the DecodeAssist microcode could not fetch
+	 * bytes for the faulting instruction because the code fetch itself faulted,
+	 * e.g. the guest attempted to fetch from emulated MMIO or a guest page
+	 * table used to translate CS:RIP resides in emulated MMIO.  Just retry.
+	 */
+	if (error_code & PFERR_FETCH_MASK)
+		goto resume_guest;
+
+	/*
+	 * A data fault could actually be Errata 1096 Fam_17h_00_0Fh.
+	 * Detect it to apply a workaround.
  	 *
-	 * Errata:
  	 * When CPU raises #NPF on guest data access and vCPU CR4.SMAP=1, it is
  	 * possible that CPU microcode implementing DecodeAssist will fail to
  	 * read guest memory at CS:RIP and vmcb.GuestIntrBytes will incorrectly
@@ -5552,12 +5562,8 @@ static int svm_check_emulate_instruction(
  	 *       retrieve the instruction bytes is legitimate (see abvoe).
  	 *
  	 * In addition, don't apply the erratum workaround if the #NPF occurred
-	 * while translating guest page tables (see below).
+	 * while translating guest page tables (see above).
  	 */
-	error_code = svm->vmcb->control.exit_info_1;
-	if (error_code & (PFERR_GUEST_PAGE_MASK | PFERR_FETCH_MASK))
-		goto resume_guest;
-
  	smep = kvm_is_cr4_bit_set(vcpu, X86_CR4_SMEP);
  	smap = kvm_is_cr4_bit_set(vcpu, X86_CR4_SMAP);
  	is_user = svm_get_cpl(vcpu) == 3;


Paolo


      parent reply	other threads:[~2026-11-17 18:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27 19:19 Sasha Levin
2026-11-17 13:50 ` Sasha Levin
2026-11-17 16:41   ` Paolo Bonzini
2026-11-17 18:50 ` Paolo Bonzini [this message]

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=7b4beea2-2bc9-44d8-be6a-ecfe678091dc@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=kylebot@openai.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sashal@kernel.org \
    --cc=seanjc@google.com \
    --cc=thomas.lendacky@amd.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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®