From nobody Mon Feb 9 01:45:45 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 B75772DB79E for ; Fri, 9 Jan 2026 03:45:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767930340; cv=none; b=UUHLBh1W5w/tc4Yv083y5x94LWG/sXmmGAYIJxwgYOnr3W3hdU9c927pysSKi0nIozRMvWfgMW5Zua5HrgCaPUKQfPKIyxO5N+6+vdRmItasKvXGRVLPbGHdmESPJ4PFMbT1aosXWB+mTHjyILbwzmZF+5zY4z1L1RLTuVFtfO4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767930340; c=relaxed/simple; bh=orWaPkiAuW5dWdX6xCjlTadYJ3ABx54C6lKaFm93YOI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ACbZC3FuK+a73zEOzrkZlL7TXqXVnzN4fopU/7AfknPGNnhuUE5+ByVc3QxxrqzOvcawmrsziE7d5nkw4iMiBERivcVPtJyBIIZjwgBvFf9R/P6INTSvTauWLbTVodj0xfu5jvxjRSCRccFblP1i7LtCTQ6RAJc50LTH0glKJSA= 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=azFhIlUD; arc=none smtp.client-ip=209.85.216.73 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="azFhIlUD" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-34e70e2e363so5213004a91.1 for ; Thu, 08 Jan 2026 19:45:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1767930338; x=1768535138; 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=NikTKJr2vdZDsmzh4h4fhl6av/cM1UMaxZuL4gTL4pg=; b=azFhIlUDLrRcRPimszwVs5/AmDCOfJOitIbXwrHhXzfsdRfnBQfdOUKcOkBjeYz9Wc VTQLNPdHCjt9G43PBVe4GV4GYzcEwp0/IxkzfLwoHYE8wFIYROp3DqXYYqChU27QC1sj xw100e3d/Fncgs7k68Is1XFrvkeqkj4GR1xauYeyR41HsGZFJkN9UoalpoJoh8LBkkWG vDkkpWmF71dR4oDr1m4leSaaPif0UEkkouose7Ue/UHxdraGhjTRS+Lkq7DRa9aU0psA HwZ3HZ3fp4YvaDh3Mfy1/yv/nlwcc5qyPbVM4iC9G62OWvApVqZcuG2VEo/emF/kDhL6 O3vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767930338; x=1768535138; 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=NikTKJr2vdZDsmzh4h4fhl6av/cM1UMaxZuL4gTL4pg=; b=UChx7i/69HKA4SuDidypziRZjeAEHNkFw+Dy5/7rucocP36r4YtY3QHMU9HmAVJint rtXlrCXDOXZfVAUQjtN1JgEi2gjg+RdtSNuAF3sAd85URQ4IIb+O72fB/QWKZlrnzjyk FGk8yhXpWyYB1aP7SNg2AM63txWPR/nSmocRBHWXwwtYCEah4eAnK5yqa4bCYA4OPJPk +0nsARZBG2jygrOfdKmCl9RN7Ol29+OlfNv7APpur/4Q0V3/agRxY9pSMwhkq4DvE87H Gf46TSZk33oxLMouDZCUFXmISpRIfyv+zxbkthTYX2CPakZoin15+gwvt2JiRGPfW9Yw v9BA== X-Forwarded-Encrypted: i=1; AJvYcCWo1HojRhikxTVXjVUJk3UkyyZuTWU8dZ+s0N23ZxTHYHseRT2fW30VfEFmG7gzEQUP+jsTSVz0X8XGqLU=@vger.kernel.org X-Gm-Message-State: AOJu0Ywn9n0Pa/YUuOasii+tiXsmJKbPrlvrLHB7yV77lL4imTjENsYb 1UXC11O0kpXx1wYfdNRBiB1/GoT2NMy21W1Go1aNJ/Vfc+14DAPx3T3DTupCNXqe6luE9OSGpq5 FiEcTjA== X-Google-Smtp-Source: AGHT+IFQrJmdgyIMjO3aabN+GefhkhIHSPxGlLvPD9SKtJfdIwMArFhB//EsefNEDdNzyQmaMbWvHQOv21c= X-Received: from pjbch23.prod.google.com ([2002:a17:90a:f417:b0:34c:d9a0:3bf6]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6300:218c:b0:364:1318:caaf with SMTP id adf61e73a8af0-3898fa38c61mr7540200637.58.1767930337960; Thu, 08 Jan 2026 19:45:37 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 8 Jan 2026 19:45:26 -0800 In-Reply-To: <20260109034532.1012993-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: <20260109034532.1012993-1-seanjc@google.com> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog Message-ID: <20260109034532.1012993-3-seanjc@google.com> Subject: [PATCH v4 2/8] 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, 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. Reviewed-by: Chao Gao 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.457.g6b5491de43-goog