Skip to content

Commit 1ed0f11

Browse files
committed
KVM: nVMX: Explicitly invalidate posted_intr_nv if PI is disabled at VM-Enter
Explicitly invalidate posted_intr_nv when emulating nested VM-Enter and posted interrupts are disabled to make it clear that posted_intr_nv is valid if and only if nested posted interrupts are enabled, and as a cheap way to harden against KVM bugs. KVM initializes posted_intr_nv to -1 at vCPU creation and resets it to -1 when unloading vmcs12 and/or leaving nested mode, i.e. this is not a bug fix (or at least, it's not intended to be a bug fix). Note, tracking nested.posted_intr_nv as a u16 subtly adds a measure of safety, as it prevents unintentionally matching KVM's informal "no IRQ" vector of -1, stored as a signed int. Because a u16 can be always be represented as a signed int, the effective "invalid" value of posted_intr_nv, 65535, will be preserved as-is when comparing against an int, i.e. will be zero-extended, not sign-extended, and thus won't get a false positive if KVM is buggy and compares posted_intr_nv against -1. Opportunistically add a comment in vmx_deliver_nested_posted_interrupt() to call out that it must check vmx->nested.posted_intr_nv, not the vector in vmcs12, which is presumably the _entire_ reason nested.posted_intr_nv exists. E.g. vmcs12 is a KVM-controlled snapshot, so there are no TOCTOU races to worry about, the only potential badness is if the vCPU leaves nested and frees vmcs12 between the sender checking is_guest_mode() and dereferencing the vmcs12 pointer. Link: https://lore.kernel.org/r/20240906043413.1049633-7-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent aa94779 commit 1ed0f11

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

arch/x86/kvm/vmx/nested.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,10 +2317,12 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0
23172317

23182318
/* Posted interrupts setting is only taken from vmcs12. */
23192319
vmx->nested.pi_pending = false;
2320-
if (nested_cpu_has_posted_intr(vmcs12))
2320+
if (nested_cpu_has_posted_intr(vmcs12)) {
23212321
vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
2322-
else
2322+
} else {
2323+
vmx->nested.posted_intr_nv = -1;
23232324
exec_control &= ~PIN_BASED_POSTED_INTR;
2325+
}
23242326
pin_controls_set(vmx, exec_control);
23252327

23262328
/*

arch/x86/kvm/vmx/vmx.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4215,6 +4215,13 @@ static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
42154215
{
42164216
struct vcpu_vmx *vmx = to_vmx(vcpu);
42174217

4218+
/*
4219+
* DO NOT query the vCPU's vmcs12, as vmcs12 is dynamically allocated
4220+
* and freed, and must not be accessed outside of vcpu->mutex. The
4221+
* vCPU's cached PI NV is valid if and only if posted interrupts
4222+
* enabled in its vmcs12, i.e. checking the vector also checks that
4223+
* L1 has enabled posted interrupts for L2.
4224+
*/
42184225
if (is_guest_mode(vcpu) &&
42194226
vector == vmx->nested.posted_intr_nv) {
42204227
/*

0 commit comments

Comments
 (0)