From nobody Tue Dec 16 14:52:18 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7CF59199931 for ; Fri, 5 Dec 2025 23:19:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764976769; cv=none; b=toA7wyKLo9u5VyuFCDan1WO5iueVzWqCgp87fGfg8z7r1SknK1yN1r/SjTPl1ectkgwcJrzJDWGB9lhkGqYB+SBX/Pz1IPtBY70fuPe9R/VxAuW0Tix/3hQ1/EhfpWGO5JN+OnVbH3P4+lCQwHcvPrdumN1wfkt6I7N3M/ux3P0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764976769; c=relaxed/simple; bh=JZFCAVE8KHQyo10EbULFfjgFlLhIrbp0ClGa9OqgEO0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BN+53nSfFIYzUd7QBp2h9gNmXdG6LZ/djVfGzOrMN5UhVP0XEgiVdDcuTFp73yYMTqUlTXeaKjnTN7PPKZIvNh/6bPHXpfqctfPR2E07s4XDAlurgk7kF18MV78YNTjXrQqqugQFXPTk4KB5qKkYAOi+GHpwPxLRKhrzZ/znRHE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=4Z8DcuV7; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4Z8DcuV7" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-298535ef0ccso33741415ad.3 for ; Fri, 05 Dec 2025 15:19:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764976764; x=1765581564; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Mvq9EwNuVt/uRDW4qk+DItZf7McZ3rUgRJ5xdyR4olA=; b=4Z8DcuV7M/pXTI9xnqEcXVojGpPfDfXdlQ0LJl7YgmqodRB5vv1/MDnUOwAfGqu/VP Su7wRrvEJBAP9xXT5iK9ChmfUMkD5bXh22e8D1JeQpgBrpqO4f2DtHg+vsxZNI9BCb5n w9cHUZZPKHr6FIGVOgPn4bhEizRBjkwcOwntY59WWNB1nnVfiMvw2l3GbGMcgUWEBaAf c3c+ocPl701ieur1Sjksn9a56Qdhw96u0qUs4OYnjctOJ/gVd4UyTJdWEOVEesVAG3Vj I8lvaRnqxyaVwe1ZxVCzQpJ6xDHSE51tgRO6u111ktnaQARRCLkx+nYXhwJgePqSEBEd YvVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764976764; x=1765581564; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Mvq9EwNuVt/uRDW4qk+DItZf7McZ3rUgRJ5xdyR4olA=; b=vJPbt0VF9++38V4XNvNoS26NYMOwDNE38r1dbw4NSFmCwtsCrpfQ+yDiX9fug+QNIw kxl92O4WjE51stVuc+LMU2zUucTDX/8517DCthXntdIJMCKXdj2Rc93tuCbO145Q3ffI zJxqgEiIvY5ksghq6QmpVD2JPVHdWFYS39aU9gpwtGKpvIWtpzHpOjUS0XNkirdtAFfj dnhksktk5dH8f94YoT/6Wxe/OtlfcCxe5Cv1wjdAlYzxbWrmz+U91pQgwr+wsmqfQ7xi e63TjNtRS2PAWCXvgmlvBkBuDqkFL05K8d9e4c4pQqjDUOlw6w2E23FOea/4r6hvSnv8 9TPw== X-Forwarded-Encrypted: i=1; AJvYcCV+dfQuKj++0Jme2cq3DSy/gNcyVAZEWy5wLFt9D0MD0EC2A+PW3uVQwQTRcd37DQZ76SP7oLl0ZLfdTxw=@vger.kernel.org X-Gm-Message-State: AOJu0YympQeXr3JOOhe0wOxUWPLyuf9juHVB5sdmD71lEDKKB64cBfQj 6NnMF4htocavXWQv9yB22UYHs+TMhDJk54Aw9ZqVMrlOztbMLB8lMh4xXSWIbpGSenepWrxanlM gSI5vLg== X-Google-Smtp-Source: AGHT+IGaqtsQIlRIt8dhC6XhjoLt1eLpPH33jgp2ob6v9qBUcmBPpf0biCLwuxLzldqH2TDHNchN+IL7vp8= X-Received: from pgbee7.prod.google.com ([2002:a05:6a02:4587:b0:bc0:d9a9:8a8e]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:1584:b0:361:2c56:fca8 with SMTP id adf61e73a8af0-36618164900mr850783637.50.1764976764391; Fri, 05 Dec 2025 15:19:24 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 5 Dec 2025 15:19:07 -0800 In-Reply-To: <20251205231913.441872-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251205231913.441872-1-seanjc@google.com> X-Mailer: git-send-email 2.52.0.223.gf5cc29aaa4-goog Message-ID: <20251205231913.441872-5-seanjc@google.com> Subject: [PATCH v3 04/10] KVM: nVMX: Switch to vmcs01 to update PML controls on-demand if L2 is active From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Dongli Zhang , Chao Gao Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If KVM toggles "CPU dirty logging", a.k.a. Page-Modification Logging (PML), while L2 is active, temporarily load vmcs01 and immediately update the relevant controls instead of deferring the update until the next nested VM-Exit. For PML, deferring the update is relatively straightforward, but for several APICv related updates, deferring updates creates ordering and state consistency problems, e.g. KVM at-large thinks APICv is enabled, but vmcs01 is still running with stale (and effectively unknown) state. Convert PML first precisely because it's the simplest case to handle: if something is broken with the vmcs01 <=3D> vmcs02 dance, then hopefully bugs will bisect here. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 5 ----- arch/x86/kvm/vmx/vmx.c | 40 +++++++++++++++++++++++++++++++++++---- arch/x86/kvm/vmx/vmx.h | 1 - 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 6137e5307d0f..920a925bb46f 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -5152,11 +5152,6 @@ void __nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 = vm_exit_reason, vmx_set_virtual_apic_mode(vcpu); } =20 - if (vmx->nested.update_vmcs01_cpu_dirty_logging) { - vmx->nested.update_vmcs01_cpu_dirty_logging =3D false; - vmx_update_cpu_dirty_logging(vcpu); - } - nested_put_vmcs12_pages(vcpu); =20 if (vmx->nested.reload_vmcs01_apic_access_page) { diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 6b96f7aea20b..1420665fbb66 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1594,6 +1594,41 @@ void vmx_vcpu_put(struct kvm_vcpu *vcpu) vmx_prepare_switch_to_host(to_vmx(vcpu)); } =20 +static void vmx_switch_loaded_vmcs(struct kvm_vcpu *vcpu, + struct loaded_vmcs *vmcs) +{ + struct vcpu_vmx *vmx =3D to_vmx(vcpu); + int cpu; + + cpu =3D get_cpu(); + vmx->loaded_vmcs =3D vmcs; + vmx_vcpu_load_vmcs(vcpu, cpu); + put_cpu(); +} + +static void vmx_load_vmcs01(struct kvm_vcpu *vcpu) +{ + struct vcpu_vmx *vmx =3D to_vmx(vcpu); + + if (!is_guest_mode(vcpu)) { + WARN_ON_ONCE(vmx->loaded_vmcs !=3D &vmx->vmcs01); + return; + } + + WARN_ON_ONCE(vmx->loaded_vmcs !=3D &vmx->nested.vmcs02); + vmx_switch_loaded_vmcs(vcpu, &vmx->vmcs01); +} + +static void vmx_put_vmcs01(struct kvm_vcpu *vcpu) +{ + if (!is_guest_mode(vcpu)) + return; + + vmx_switch_loaded_vmcs(vcpu, &to_vmx(vcpu)->nested.vmcs02); +} +DEFINE_GUARD(vmx_vmcs01, struct kvm_vcpu *, + vmx_load_vmcs01(_T), vmx_put_vmcs01(_T)) + bool vmx_emulation_required(struct kvm_vcpu *vcpu) { return emulate_invalid_guest_state && !vmx_guest_state_valid(vcpu); @@ -8267,10 +8302,7 @@ void vmx_update_cpu_dirty_logging(struct kvm_vcpu *v= cpu) if (WARN_ON_ONCE(!enable_pml)) return; =20 - if (is_guest_mode(vcpu)) { - vmx->nested.update_vmcs01_cpu_dirty_logging =3D true; - return; - } + guard(vmx_vmcs01)(vcpu); =20 /* * Note, nr_memslots_dirty_logging can be changed concurrent with this diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index bc3ed3145d7e..b44eda6225f4 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -133,7 +133,6 @@ struct nested_vmx { =20 bool change_vmcs01_virtual_apic_mode; bool reload_vmcs01_apic_access_page; - bool update_vmcs01_cpu_dirty_logging; bool update_vmcs01_apicv_status; bool update_vmcs01_hwapic_isr; =20 --=20 2.52.0.223.gf5cc29aaa4-goog