* [PATCH] KVM: SEV: never retry after #NPF on paging structures
@ 2026-11-17 18:59 Paolo Bonzini
2026-11-17 19:28 ` sashiko-bot
2026-11-17 20:07 ` Sasha Levin
0 siblings, 2 replies; 3+ messages in thread
From: Paolo Bonzini @ 2026-11-17 18:59 UTC (permalink / raw)
To: linux-kernel, kvm; +Cc: seanjc, Sasha Levin
KVM cannot walk the encrypted page tables of SEV guests, so there is
no point in emulating the instruction if the #NPF fault was caused by
either data or code. In the latter case the instruction buffer is going
to be empty, so emulation was avoided by resuming the guest, but in the
data case KVM attempted emulation which is pointless.
Apply the existing check for PFERR_GUEST_PAGE_MASK even if the instruction
buffer was filled.
Reported-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
arch/x86/kvm/svm/svm.c | 30 ++++++++++++++++++------------
1 file changed, 18 insertions(+), 12 deletions(-)
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index af0deb198a84..4f42da3642b6 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(struct kvm_vcpu *vcpu, int emul_type,
}
/*
- * 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)
+ goto resume_guest;
+
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.
*
- * Errata:
+ * A data fault instead could be Errata 1096 Fam_17h_00_0Fh, so only
+ * retry for a fetch fault and otherwise try to workaround it.
+ */
+ if (error_code & PFERR_FETCH_MASK)
+ goto resume_guest;
+
+ /*
* 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(struct kvm_vcpu *vcpu, int emul_type,
* 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;
--
2.56.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] KVM: SEV: never retry after #NPF on paging structures
2026-11-17 18:59 [PATCH] KVM: SEV: never retry after #NPF on paging structures Paolo Bonzini
@ 2026-11-17 19:28 ` sashiko-bot
2026-11-17 20:07 ` Sasha Levin
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-17 19:28 UTC (permalink / raw)
To: Paolo Bonzini, kvm
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] KVM: SVM: unchecked exit_info_1 read in
svm_check_emulate_instruction()
- [Low] KVM: SEV: commit subject says never retry instead of never
emulate
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Medium] KVM: SVM: wrong exit_int_info mask in
svm_check_emulate_instruction() [1]
--
commit 79cd3998685bf7eaec1673e862ee32458c3ae3ee
Author: Paolo Bonzini <pbonzini@redhat.com>
KVM: SEV: never retry after #NPF on paging structures
This commit moves the PFERR_GUEST_PAGE_MASK check in
svm_check_emulate_instruction() ahead of the insn_len check so that KVM
resumes the SEV guest instead of attempting instruction emulation after a
#NPF on guest page tables.
> KVM: SEV: never retry after #NPF on paging structures
[Severity: Low]
Should the commit subject say "never emulate" (or "always retry") instead
of "never retry"? Jumping to resume_guest returns X86EMUL_RETRY_INSTR to
resume the guest and retry the instruction rather than emulating it.
> diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
> index fe0cba4731b2e..65b15999cf9d8 100644
> --- a/arch/x86/kvm/svm/svm.c
> +++ b/arch/x86/kvm/svm/svm.c
> @@ -5192,19 +5192,29 @@ static int svm_check_emulate_instruction(struct kvm_vcpu *vcpu, int emul_type,
> }
>
> /*
> - * 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)
> + goto resume_guest;
> +
> if (likely(insn_len))
> return X86EMUL_CONTINUE;
[Severity: Medium]
Can moving the exit_info_1 check ahead of the insn_len check without
checking emul_type & EMULTYPE_PF cause non-page-fault emulations to consume
a stale exit_info_1 value?
When an SEV guest executes RSM, rsm_interception() calls
kvm_emulate_instruction_from_buffer(vcpu, rsm_ins_bytes, 2) with
emul_type == 0 and insn_len == 2:
arch/x86/kvm/svm/svm.c:rsm_interception() {
return kvm_emulate_instruction_from_buffer(vcpu, rsm_ins_bytes, 2);
}
Previously, that path returned X86EMUL_CONTINUE at if (likely(insn_len))
before reading exit_info_1. Because SVM_EXIT_RSM does not define
EXITINFO1, a stale PFERR_GUEST_PAGE_MASK bit left in
vmcb->control.exit_info_1 from an earlier #NPF causes
svm_check_emulate_instruction() to return X86EMUL_RETRY_INSTR and loop on
RSM instead of emulating it or injecting #UD.
> /*
> - * 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.
> *
> - * Errata:
> + * A data fault instead could be Errata 1096 Fam_17h_00_0Fh, so only
> + * retry for a fetch fault and otherwise try to workaround it.
> + */
> + if (error_code & PFERR_FETCH_MASK)
> + goto resume_guest;
> +
> + /*
> * 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
> @@ -5231,12 +5241,8 @@ static int svm_check_emulate_instruction(struct kvm_vcpu *vcpu, int emul_type,
> * 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;
[Severity: Medium]
This problem wasn't introduced by this patch, but at the start of
svm_check_emulate_instruction() the event-vectoring check [1] masks
exit_int_info with SVM_EXITINTINFO_TYPE_MASK instead of
SVM_EXITINTINFO_VALID:
arch/x86/kvm/svm/svm.c:svm_check_emulate_instruction() {
...
/* Check that emulation is possible during event vectoring */
if ((svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK) &&
!kvm_can_emulate_event_vectoring(emul_type))
return X86EMUL_UNHANDLEABLE_VECTORING;
...
}
Because SVM_EXITINTINFO_TYPE_INTR is 0, this test misses exits that occur
while vectoring a hardware interrupt, allowing the vCPU to emulate or skip
the interrupted instruction instead of returning
X86EMUL_UNHANDLEABLE_VECTORING.
[1] https://sashiko.dev/#/bug/linux-7ce5444c-332c-4505-82c7-e1440dea4b14
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117185922.466255-1-pbonzini@redhat.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] KVM: SEV: never retry after #NPF on paging structures
2026-11-17 18:59 [PATCH] KVM: SEV: never retry after #NPF on paging structures Paolo Bonzini
2026-11-17 19:28 ` sashiko-bot
@ 2026-11-17 20:07 ` Sasha Levin
1 sibling, 0 replies; 3+ messages in thread
From: Sasha Levin @ 2026-11-17 20:07 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: linux-kernel, kvm, Sean Christopherson
On Tue, Nov 17, 2026 at 07:59:22PM +0100, Paolo Bonzini wrote:
> + error_code = svm->vmcb->control.exit_info_1;
> + if (error_code & PFERR_GUEST_PAGE_MASK)
> + goto resume_guest;
The subject says "never retry", but resume_guest returns
X86EMUL_RETRY_INSTR, so this does retry: the guest is resumed and the
instruction runs again.
In your reply to my patch you had X86EMUL_UNHANDLEABLE here instead:
https://lore.kernel.org/r/7b4beea2-2bc9-44d8-be6a-ecfe678091dc@redhat.com
Which one did you mean? If it's the retry, the subject probably wants to
say "never emulate".
--
Thanks,
Sasha
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-17 20:07 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 18:59 [PATCH] KVM: SEV: never retry after #NPF on paging structures Paolo Bonzini
2026-11-17 19:28 ` sashiko-bot
2026-11-17 20:07 ` Sasha Levin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox