From nobody Sat Oct 4 14:35: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 0E240BA3F for ; Fri, 15 Aug 2025 00:12:14 +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=1755216736; cv=none; b=mLioWx8zEmjcpbKgN0mnO2fUu3uHVkZGp9jNel/xZCIPczVKfdiW9SSiDhiQJPnYd6k25P6RiWg1HKiMUHKEBoSwQICX2VrScSQJto3FO9SpPDz333NF0rGaGzQI2IkdQgud3W5PwEq/UXJeriHKUp77oiebeF7gtSPsSLXndhQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216736; c=relaxed/simple; bh=QZ7hiEeEodzcf9N5xa+v0EKi7D4cdSZUxTI4oTTqBUM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DV07upgSTyr0LAKLAE8+D6ALKklCZqe7BSkEzGH147aW6jSjxIzeV3asSJUn0/RseyhhmUZHSSGVweV2OCnz/Wg0ugmDaQnZcSTnuUsrTqfqbEWvPvUX0IBFx6jrB6j3d+rmEOavEM/tiP80EoEyGDXT01WwG8G+QMfjIfduoY8= 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=QnJSMhGf; 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="QnJSMhGf" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-24458194d82so15458795ad.2 for ; Thu, 14 Aug 2025 17:12:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216734; x=1755821534; 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=+3Z7z+GhkYgH9ZaG5Bt9ilPatbNNXsIC5wDZoaGAfW0=; b=QnJSMhGf1xkhdlJm+hIeKc4J5S8Y/wvko75pb+7GHk9MjGCUkqE8y35/FGQTison19 BPSZ/vk+VMfwpX+TjRTm896g4xV9WJlR/Dwe+c/+F3LvN0IsKcAui23Bk3Sf1lHXDZhM V6LKbRSgs/8k+vXFDVnv7oZgLeIybS28wo+bEVDf8BlEq037qwmZkaVNampp76RD688a DaNlCMUrgjRXIlFrl1I3LB3/+PRyFEKVySy0nDQEeKsYDS62u5G2NAjBhBolOy0Qn7Ny b65u+8arDYyhaSRiRP/mxzTiM12WazTJiqujIe8kj394s3gKC+QvCLZQKAydQC8SXnGp AaBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216734; x=1755821534; 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=+3Z7z+GhkYgH9ZaG5Bt9ilPatbNNXsIC5wDZoaGAfW0=; b=OIb3j3ZLM7glIyX0eprzZI7R6hZxbbdpWRoumIrSgW8pXA3ncUjoUsAn1huYjzIobo jpYShEA2xapGSdbXDPwsOx2KtwHg4bFr6/zDXMnVEynaXkAGoCE38paK1lElXGujWlDf dc59YCzssQMU0FdLHoGBD4i7u7/LZLuC5sSA8qKaeT52z+eqPcb/aL4Dr8mO3jQlOWo6 PY4zOFTwVKsDuSczsizrFrrRjqXWXOT9JSFbpzxYMug9QeULpehueq8DztmHjqzeIyjK crYf/xvSTqocBhrUifp7d5cuWkwnTA+gom2UVQengojrWLq9mI29/6SWjo3jpzRgjPV5 xWzQ== X-Forwarded-Encrypted: i=1; AJvYcCXyO2uQKEnUFaKhIcxKjnBvGq2X45XIVoo+2RGmbrNspP1HzVUncFhgnKKpqLUH+gkbPR7u9vS6XZALv+M=@vger.kernel.org X-Gm-Message-State: AOJu0YyQo2W7QPp38uy1kaf7BqGI66y2fe5ekmc84dnU/NVUgJrLaYpP CdCAXiFfc/Qy9yoMCm+Gt+F+JNr181lcFopl9i1NXjNx6Mbn4e3iGEndc4EEwQcgWKla7hLpHeJ 9Lwo9Mw== X-Google-Smtp-Source: AGHT+IFeyVWtbJnA+0jK98/KDDcJOwZAstyIgyOg41fad5x07dGh22iBOzCqZIcKbU86Qr8Tb8SFx3dcllQ= X-Received: from pjbnd12.prod.google.com ([2002:a17:90b:4ccc:b0:31f:36c3:b18a]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:22c7:b0:242:8a45:a95e with SMTP id d9443c01a7336-2446d6e3df4mr1726155ad.15.1755216734230; Thu, 14 Aug 2025 17:12:14 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:11:45 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-2-seanjc@google.com> Subject: [PATCH 6.1.y 01/21] KVM: SVM: Set RFLAGS.IF=1 in C code, to get VMRUN out of the STI shadow From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit be45bc4eff33d9a7dae84a2150f242a91a617402 ] Enable/disable local IRQs, i.e. set/clear RFLAGS.IF, in the common svm_vcpu_enter_exit() just after/before guest_state_{enter,exit}_irqoff() so that VMRUN is not executed in an STI shadow. AMD CPUs have a quirk (some would say "bug"), where the STI shadow bleeds into the guest's intr_state field if a #VMEXIT occurs during injection of an event, i.e. if the VMRUN doesn't complete before the subsequent #VMEXIT. The spurious "interrupts masked" state is relatively benign, as it only occurs during event injection and is transient. Because KVM is already injecting an event, the guest can't be in HLT, and if KVM is querying IRQ blocking for injection, then KVM would need to force an immediate exit anyways since injecting multiple events is impossible. However, because KVM copies int_state verbatim from vmcb02 to vmcb12, the spurious STI shadow is visible to L1 when running a nested VM, which can trip sanity checks, e.g. in VMware's VMM. Hoist the STI+CLI all the way to C code, as the aforementioned calls to guest_state_{enter,exit}_irqoff() already inform lockdep that IRQs are enabled/disabled, and taking a fault on VMRUN with RFLAGS.IF=3D1 is already possible. I.e. if there's kernel code that is confused by running with RFLAGS.IF=3D1, then it's already a problem. In practice, since GIF=3D0 also blocks NMIs, the only change in exposure to non-KVM code (relative to surrounding VMRUN with STI+CLI) is exception handling code, and except for the kvm_rebooting=3D1 case, all exception in the core VM-Enter/VM-Exit path are fatal. Use the "raw" variants to enable/disable IRQs to avoid tracing in the "no instrumentation" code; the guest state helpers also take care of tracing IRQ state. Oppurtunstically document why KVM needs to do STI in the first place. Reported-by: Doug Covelli Closes: https://lore.kernel.org/all/CADH9ctBs1YPmE4aCfGPNBwA10cA8RuAk2gO754= 2DjMZgs4uzJQ@mail.gmail.com Fixes: f14eec0a3203 ("KVM: SVM: move more vmentry code to assembly") Cc: stable@vger.kernel.org Reviewed-by: Jim Mattson Link: https://lore.kernel.org/r/20250224165442.2338294-2-seanjc@google.com Signed-off-by: Sean Christopherson [sean: resolve minor syntatic conflict in __svm_sev_es_vcpu_run()] Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 14 ++++++++++++++ arch/x86/kvm/svm/vmenter.S | 9 +-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index b6bbd0dc4e65..c95a84afc35f 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3982,6 +3982,18 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_v= cpu *vcpu, bool spec_ctrl_in =20 guest_state_enter_irqoff(); =20 + /* + * Set RFLAGS.IF prior to VMRUN, as the host's RFLAGS.IF at the time of + * VMRUN controls whether or not physical IRQs are masked (KVM always + * runs with V_INTR_MASKING_MASK). Toggle RFLAGS.IF here to avoid the + * temptation to do STI+VMRUN+CLI, as AMD CPUs bleed the STI shadow + * into guest state if delivery of an event during VMRUN triggers a + * #VMEXIT, and the guest_state transitions already tell lockdep that + * IRQs are being enabled/disabled. Note! GIF=3D0 for the entirety of + * this path, so IRQs aren't actually unmasked while running host code. + */ + raw_local_irq_enable(); + amd_clear_divider(); =20 if (sev_es_guest(vcpu->kvm)) @@ -3989,6 +4001,8 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vc= pu *vcpu, bool spec_ctrl_in else __svm_vcpu_run(svm, spec_ctrl_intercepted); =20 + raw_local_irq_disable(); + guest_state_exit_irqoff(); } =20 diff --git a/arch/x86/kvm/svm/vmenter.S b/arch/x86/kvm/svm/vmenter.S index 42824f9b06a2..48b72625cc45 100644 --- a/arch/x86/kvm/svm/vmenter.S +++ b/arch/x86/kvm/svm/vmenter.S @@ -170,12 +170,8 @@ SYM_FUNC_START(__svm_vcpu_run) VM_CLEAR_CPU_BUFFERS =20 /* Enter guest mode */ - sti - 3: vmrun %_ASM_AX 4: - cli - /* Pop @svm to RAX while it's the only available register. */ pop %_ASM_AX =20 @@ -343,11 +339,8 @@ SYM_FUNC_START(__svm_sev_es_vcpu_run) VM_CLEAR_CPU_BUFFERS =20 /* Enter guest mode */ - sti - 1: vmrun %_ASM_AX - -2: cli +2: =20 /* Pop @svm to RDI, guest registers have been saved already. */ pop %_ASM_DI --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 78B481DA21 for ; Fri, 15 Aug 2025 00:12:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216738; cv=none; b=KwIRDcHBXA9ZN752mB1wTpg4R0+rQHX8AjRJmkP6Ygb63Nh4ppxwUfo7dku52BSc8zrhTJ61RyUtk5yEV+HJ7ZRj5/3R4kp/SxD/hnsmS21xWr1FoQ7zgq7djURbIuDTTE/T2oj1YbgtEbjtRJmwP6B0LRGQaIyZ2DV3wo++Uck= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216738; c=relaxed/simple; bh=/BkaJeh+u+w0PxculFeU+YAag3AmTz4Ma5Y3HwSoFSs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CWeW0pglCiRYdhwbVg5pG23ZOxFq9hH3LATYS2tJrGDB1xZautndTqpy5koyxGK8Czq9we5awTCWxPUAkQLgyQAF1bjV6ovqjLzeIGHOnasnZhWe8uAuS1ZbzTwrb0tiOdmKVRu7Q8OJq0I7iwin5nWlw296QAuPNzWPTNoVcA4= 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=fjxpROve; arc=none smtp.client-ip=209.85.210.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="fjxpROve" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-76e2ead3b51so2439206b3a.2 for ; Thu, 14 Aug 2025 17:12:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216736; x=1755821536; 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=14QM1z+dm5GkgFjlzKHbTF7lRU9gP89BnvFIFOimBio=; b=fjxpROveZrLbbe9N4INHzKkHMIVPiymU4b25zskqhOQ2r17Bqa5kno+lqqVbFrCgQV XfhEOZhuXieVIcCHJCtUw7Jv3UA+8bfVxfJPtMd0P/6KtU/wyvOqZ3dxRFK++J4Nq3LD c++H/cVEOVaF1yox2vc0tDFGGSIv5kE7nl/ImF/uWjy7kZ9UOpYgnwEUfdWKq79EZhXP VHb2bxk9oHdKXwdupzv/2X/dJDVCaxk50qxJWfIkaeyYO5BqeltBlMfSFs+gb89tPgsh NyeDSiN2gUP1N26lIgRCChMzRpYtsEMMiXVM4aqFv556A56CSq2dEI5vLHvzzbHAKEfE exFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216736; x=1755821536; 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=14QM1z+dm5GkgFjlzKHbTF7lRU9gP89BnvFIFOimBio=; b=c9grhCnEsWjkY91M1Qc8sBaJO5fD6azPKASmnWHXd7bt60nULSisPTlWy7JKeFFY3B 4KcHsFiJTYxVVY+qGmkNshNV7z1qpfy0zGl0Do7QSuVmXz/xfmP40idqWIElGhgSaQN5 rBfNGOGjv3+hy3WVsXbw9pCmJj2eQ/fdb3IyORlPusKuF4Nw2RXWFZxb+SnUKNQxhbeJ zJIhS9WLp28Yxak33LY6x2b6gOtZVOqIn0Hh/EUDJvVUXzDF9n1InwwrWnoM2NjKwBsG zBz1VtQnFXZ4sodx0WueQdciwg2kMqt1IIXA0O11o5qEZMnynH50FBNYOOGJRQiI/kb+ v07w== X-Forwarded-Encrypted: i=1; AJvYcCXOXWJTpP4fYy9sapC/1SRyG/qLkyhtHdii2YuV/VYTwbPMG/cq8kYQt+88fo42mUozT8it1ID9G9mZT+E=@vger.kernel.org X-Gm-Message-State: AOJu0YzO0XjHyIqhUX3IxKFxmTXTXvYnuhQob10S2u18nO8z1VaRQFda RIvfUrMxWD9fNAfu4oBBnTmQfUOlXwZi8Z6fu1uN2tryaCgEeqG0sXKQeJMABQ5rcSwPl2tLaYz hYl+UYQ== X-Google-Smtp-Source: AGHT+IFeo9E+l/DagxMz1Ec9Bybnt5sRn/yXPRsbfvMN+GXHL3ok4MUTGXxfotByzxbvgqgO+WCoCPEZr8U= X-Received: from pfmy19.prod.google.com ([2002:aa7:8053:0:b0:76b:cac2:6d23]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:32a8:b0:232:6630:bca4 with SMTP id adf61e73a8af0-240d2e5bad4mr311881637.15.1755216735769; Thu, 14 Aug 2025 17:12:15 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:11:46 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-3-seanjc@google.com> Subject: [PATCH 6.1.y 02/21] KVM: x86: Re-split x2APIC ICR into ICR+ICR2 for AMD (x2AVIC) From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit 73b42dc69be8564d4951a14d00f827929fe5ef79 ] Re-introduce the "split" x2APIC ICR storage that KVM used prior to Intel's IPI virtualization support, but only for AMD. While not stated anywhere in the APM, despite stating the ICR is a single 64-bit register, AMD CPUs store the 64-bit ICR as two separate 32-bit values in ICR and ICR2. When IPI virtualization (IPIv on Intel, all AVIC flavors on AMD) is enabled, KVM needs to match CPU behavior as some ICR ICR writes will be handled by the CPU, not by KVM. Add a kvm_x86_ops knob to control the underlying format used by the CPU to store the x2APIC ICR, and tune it to AMD vs. Intel regardless of whether or not x2AVIC is enabled. If KVM is handling all ICR writes, the storage format for x2APIC mode doesn't matter, and having the behavior follow AMD versus Intel will provide better test coverage and ease debugging. Fixes: 4d1d7942e36a ("KVM: SVM: Introduce logic to (de)activate x2AVIC mode= ") Cc: stable@vger.kernel.org Cc: Maxim Levitsky Cc: Suravee Suthikulpanit Link: https://lore.kernel.org/r/20240719235107.3023592-4-seanjc@google.com Signed-off-by: Sean Christopherson [sean: resolve minor syntatic conflicts] Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/lapic.c | 42 +++++++++++++++++++++++---------- arch/x86/kvm/svm/svm.c | 2 ++ arch/x86/kvm/vmx/vmx.c | 2 ++ 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index eb06c2f68314..17b4e61a52b9 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1547,6 +1547,8 @@ struct kvm_x86_ops { void (*enable_nmi_window)(struct kvm_vcpu *vcpu); void (*enable_irq_window)(struct kvm_vcpu *vcpu); void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr); + + const bool x2apic_icr_is_split; bool (*check_apicv_inhibit_reasons)(enum kvm_apicv_inhibit reason); void (*refresh_apicv_exec_ctrl)(struct kvm_vcpu *vcpu); void (*hwapic_irr_update)(struct kvm_vcpu *vcpu, int max_irr); diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 7f57dce5c828..42eec987ac3d 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2315,11 +2315,25 @@ int kvm_x2apic_icr_write(struct kvm_lapic *apic, u6= 4 data) data &=3D ~APIC_ICR_BUSY; =20 kvm_apic_send_ipi(apic, (u32)data, (u32)(data >> 32)); - kvm_lapic_set_reg64(apic, APIC_ICR, data); + if (kvm_x86_ops.x2apic_icr_is_split) { + kvm_lapic_set_reg(apic, APIC_ICR, data); + kvm_lapic_set_reg(apic, APIC_ICR2, data >> 32); + } else { + kvm_lapic_set_reg64(apic, APIC_ICR, data); + } trace_kvm_apic_write(APIC_ICR, data); return 0; } =20 +static u64 kvm_x2apic_icr_read(struct kvm_lapic *apic) +{ + if (kvm_x86_ops.x2apic_icr_is_split) + return (u64)kvm_lapic_get_reg(apic, APIC_ICR) | + (u64)kvm_lapic_get_reg(apic, APIC_ICR2) << 32; + + return kvm_lapic_get_reg64(apic, APIC_ICR); +} + /* emulate APIC access in a trap manner */ void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset) { @@ -2337,7 +2351,7 @@ void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u= 32 offset) * maybe-unecessary write, and both are in the noise anyways. */ if (apic_x2apic_mode(apic) && offset =3D=3D APIC_ICR) - WARN_ON_ONCE(kvm_x2apic_icr_write(apic, kvm_lapic_get_reg64(apic, APIC_I= CR))); + WARN_ON_ONCE(kvm_x2apic_icr_write(apic, kvm_x2apic_icr_read(apic))); else kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset)); } @@ -2760,18 +2774,22 @@ static int kvm_apic_state_fixup(struct kvm_vcpu *vc= pu, =20 /* * In x2APIC mode, the LDR is fixed and based on the id. And - * ICR is internally a single 64-bit register, but needs to be - * split to ICR+ICR2 in userspace for backwards compatibility. + * if the ICR is _not_ split, ICR is internally a single 64-bit + * register, but needs to be split to ICR+ICR2 in userspace for + * backwards compatibility. */ - if (set) { + if (set) *ldr =3D kvm_apic_calc_x2apic_ldr(*id); =20 - icr =3D __kvm_lapic_get_reg(s->regs, APIC_ICR) | - (u64)__kvm_lapic_get_reg(s->regs, APIC_ICR2) << 32; - __kvm_lapic_set_reg64(s->regs, APIC_ICR, icr); - } else { - icr =3D __kvm_lapic_get_reg64(s->regs, APIC_ICR); - __kvm_lapic_set_reg(s->regs, APIC_ICR2, icr >> 32); + if (!kvm_x86_ops.x2apic_icr_is_split) { + if (set) { + icr =3D __kvm_lapic_get_reg(s->regs, APIC_ICR) | + (u64)__kvm_lapic_get_reg(s->regs, APIC_ICR2) << 32; + __kvm_lapic_set_reg64(s->regs, APIC_ICR, icr); + } else { + icr =3D __kvm_lapic_get_reg64(s->regs, APIC_ICR); + __kvm_lapic_set_reg(s->regs, APIC_ICR2, icr >> 32); + } } } =20 @@ -2971,7 +2989,7 @@ static int kvm_lapic_msr_read(struct kvm_lapic *apic,= u32 reg, u64 *data) u32 low; =20 if (reg =3D=3D APIC_ICR) { - *data =3D kvm_lapic_get_reg64(apic, APIC_ICR); + *data =3D kvm_x2apic_icr_read(apic); return 0; } =20 diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index c95a84afc35f..b922f31d1415 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4851,6 +4851,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata =3D { .enable_nmi_window =3D svm_enable_nmi_window, .enable_irq_window =3D svm_enable_irq_window, .update_cr8_intercept =3D svm_update_cr8_intercept, + + .x2apic_icr_is_split =3D true, .set_virtual_apic_mode =3D avic_refresh_virtual_apic_mode, .refresh_apicv_exec_ctrl =3D avic_refresh_apicv_exec_ctrl, .check_apicv_inhibit_reasons =3D avic_check_apicv_inhibit_reasons, diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index fbe26b88f731..9a5cb896229f 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8202,6 +8202,8 @@ static struct kvm_x86_ops vmx_x86_ops __initdata =3D { .enable_nmi_window =3D vmx_enable_nmi_window, .enable_irq_window =3D vmx_enable_irq_window, .update_cr8_intercept =3D vmx_update_cr8_intercept, + + .x2apic_icr_is_split =3D false, .set_virtual_apic_mode =3D vmx_set_virtual_apic_mode, .set_apic_access_page_addr =3D vmx_set_apic_access_page_addr, .refresh_apicv_exec_ctrl =3D vmx_refresh_apicv_exec_ctrl, --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 77E5142056 for ; Fri, 15 Aug 2025 00:12:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216740; cv=none; b=hJUMD6ZbrkH7rQbZYT0rZ3iBXQ29U+SJFECwcppcHOGz59nnITJkm/gvQHH/1fNxQDBiRmWLwcN4SnK/XHHZapH/WsRAh0TMOLSmfYkmYNM7zb1M2eDVzaYHRmq9cDcRgWv7XtPMekxcgrqobYfas+Vu9wRoxL/FNwYy/NUxcG4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216740; c=relaxed/simple; bh=kvEVV1BoPOFs6DDYtZzil6bpoP269npdUmCmNBs7jHQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EWWN4/qYXmwRuuqS+aofDXk+ok5xBt/k6/MouG1JOQ5RhsEdOgPUUc9Ve86KH6tVnt0OJJmtZ1Gurn1ko3Xn3eOnpV2S3HnP22vROrzXt6ALEDRHbRAMNFhMkVWdyjXxqhRp92PLYVuZXFZYXADMQoLwMIIXW8PdaCbwHoV5rhE= 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=k2ZbMKnV; arc=none smtp.client-ip=209.85.216.74 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="k2ZbMKnV" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-323266d8396so1502918a91.0 for ; Thu, 14 Aug 2025 17:12:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216738; x=1755821538; 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=ijMOWxVCOFsVrE+awRlE3st0fZ/ms+lEIOtbXiEijA4=; b=k2ZbMKnV9PvYErS4J+9SX1977kmRRqAFShGwEQSHhL0Ya7TCl6JNAPTgosM0m7FSPk iFDT8iz8tgnAJhazOtoDQc9EL3MbRcG6sLGSMJ7aY+WutVn6O3VyHJAZBhLI1ijztKPV SsMdrXztAQFsFe11ykvj/Cm6OFsu7Dlh0gpPEAFW5v8fZlzk0nn9IdnXbCdDiolViYAd DV9a2V5UzYNA5l0EY5BziRokTzhdKIBLbpJhrOp66YhC9rRVCnwCKeAAIz04WME0uJNh imgpvdCakC1BZUuwqvgiNL/6Xa6FjWg4zKDrApSudFM0FCO6PL82CZ//l/Rbk/zW8Xfk L26g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216738; x=1755821538; 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=ijMOWxVCOFsVrE+awRlE3st0fZ/ms+lEIOtbXiEijA4=; b=bzubk3yqU3ISfY2veON+iz+NVU1pI4xGKqQwzbHCZKpEvh0HvxTvY9oCVBu2hDvoHo l91qtMYPeNXFM+Qdf8tuTeTuXRE3u8khQdNv9nkIJBNsEhA7SvMrnIgtWMAn/pjwpG7R sfT3Nc4vp6Sc0R4AvZAPIjzvmwAFVQemyiWVk2XkgCydegsnd8keVlD4GPTJGrh6iQSs TG9Yp3c+Z4/yvWynLTRhRY+P7H2pMBFbl4VQeJiOULM/ZhfMqtlPho3HPagcXskNMm6A C6bI2t4HELGJ/cS5XxlFblNz97keAdmNPo37Wg9aJfXTG2fEj3WtUaJNUk1vxSYmxW50 N6EQ== X-Forwarded-Encrypted: i=1; AJvYcCUR7fVkVSfkhDEaiD5WmgZhaI4EHuF+G/KGyIcQQ7/aukRTAeIUhgWQN952P/UkdYLRTQZwex5qQ/O/1wc=@vger.kernel.org X-Gm-Message-State: AOJu0YyvV9lrG+oFxvUmR+hXsl/ieWlI32zVHAEiJgu1ZH3uU0vV1fOA QNI2RQzpV8eOYoW9bRAV8Z8X4m8CaABg5dcB37w+DDabeCDFhRyIut261k2oGUICQjHqZRLrxcH CpwDCVA== X-Google-Smtp-Source: AGHT+IHpTDFEVltDF/eSPHVFejV9HOBd6wSUTgz0cpglTB51iNXwA6rUqHlyqBsTMJfnk58lhRvX00nXvgs= X-Received: from pjyp16.prod.google.com ([2002:a17:90a:e710:b0:31c:160d:e3be]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2882:b0:313:f6fa:5bca with SMTP id 98e67ed59e1d1-323421476f1mr263256a91.22.1755216737817; Thu, 14 Aug 2025 17:12:17 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:11:47 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-4-seanjc@google.com> Subject: [PATCH 6.1.y 03/21] KVM: x86: Plumb in the vCPU to kvm_x86_ops.hwapic_isr_update() From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit 76bce9f10162cd4b36ac0b7889649b22baf70ebd ] Pass the target vCPU to the hwapic_isr_update() vendor hook so that VMX can defer the update until after nested VM-Exit if an EOI for L1's vAPIC occurs while L2 is active. Note, commit d39850f57d21 ("KVM: x86: Drop @vcpu parameter from kvm_x86_ops.hwapic_isr_update()") removed the parameter with the justification that doing so "allows for a decent amount of (future) cleanup in the APIC code", but it's not at all clear what cleanup was intended, or if it was ever realized. No functional change intended. Cc: stable@vger.kernel.org Reviewed-by: Chao Gao Tested-by: Chao Gao Link: https://lore.kernel.org/r/20241128000010.4051275-2-seanjc@google.com Signed-off-by: Sean Christopherson [sean: account for lack of kvm_x86_call(), drop vmx/x86_ops.h change] Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/lapic.c | 8 ++++---- arch/x86/kvm/vmx/vmx.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 17b4e61a52b9..6db42ee82032 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1552,7 +1552,7 @@ struct kvm_x86_ops { bool (*check_apicv_inhibit_reasons)(enum kvm_apicv_inhibit reason); void (*refresh_apicv_exec_ctrl)(struct kvm_vcpu *vcpu); void (*hwapic_irr_update)(struct kvm_vcpu *vcpu, int max_irr); - void (*hwapic_isr_update)(int isr); + void (*hwapic_isr_update)(struct kvm_vcpu *vcpu, int isr); bool (*guest_apic_has_interrupt)(struct kvm_vcpu *vcpu); void (*load_eoi_exitmap)(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap); void (*set_virtual_apic_mode)(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 42eec987ac3d..3d65d6a023c9 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -587,7 +587,7 @@ static inline void apic_set_isr(int vec, struct kvm_lap= ic *apic) * just set SVI. */ if (unlikely(apic->apicv_active)) - static_call_cond(kvm_x86_hwapic_isr_update)(vec); + static_call_cond(kvm_x86_hwapic_isr_update)(apic->vcpu, vec); else { ++apic->isr_count; BUG_ON(apic->isr_count > MAX_APIC_VECTOR); @@ -632,7 +632,7 @@ static inline void apic_clear_isr(int vec, struct kvm_l= apic *apic) * and must be left alone. */ if (unlikely(apic->apicv_active)) - static_call_cond(kvm_x86_hwapic_isr_update)(apic_find_highest_isr(apic)); + static_call_cond(kvm_x86_hwapic_isr_update)(apic->vcpu, apic_find_highes= t_isr(apic)); else { --apic->isr_count; BUG_ON(apic->isr_count < 0); @@ -2554,7 +2554,7 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init= _event) if (apic->apicv_active) { static_call_cond(kvm_x86_apicv_post_state_restore)(vcpu); static_call_cond(kvm_x86_hwapic_irr_update)(vcpu, -1); - static_call_cond(kvm_x86_hwapic_isr_update)(-1); + static_call_cond(kvm_x86_hwapic_isr_update)(vcpu, -1); } =20 vcpu->arch.apic_arb_prio =3D 0; @@ -2847,7 +2847,7 @@ int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct = kvm_lapic_state *s) if (apic->apicv_active) { static_call_cond(kvm_x86_apicv_post_state_restore)(vcpu); static_call_cond(kvm_x86_hwapic_irr_update)(vcpu, apic_find_highest_irr(= apic)); - static_call_cond(kvm_x86_hwapic_isr_update)(apic_find_highest_isr(apic)); + static_call_cond(kvm_x86_hwapic_isr_update)(vcpu, apic_find_highest_isr(= apic)); } kvm_make_request(KVM_REQ_EVENT, vcpu); if (ioapic_in_kernel(vcpu->kvm)) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 9a5cb896229f..721ba6ddb121 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6708,7 +6708,7 @@ static void vmx_set_apic_access_page_addr(struct kvm_= vcpu *vcpu) put_page(page); } =20 -static void vmx_hwapic_isr_update(int max_isr) +static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr) { u16 status; u8 old; --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 E1BFE72614 for ; Fri, 15 Aug 2025 00:12:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216741; cv=none; b=gSMcx0aJQup8goeYuFw2YHFW6IRp6+7OlSq3errH6P3jkPCjtcBsaGShIpJCFv6j/RIIw+iR0+7beogeFMvejSqKgx167omusWD1tKNO9DRiN+E/ZniU2aSoLEYFzPaqgyfQoAtpOXz6YjLB1vgV3m8vB1/8HeBKSTjxShu99jc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216741; c=relaxed/simple; bh=wXaM5AxQLRyXz5bcyjoW0XMTnldHJbjQsY/Zd0euGkE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TbGpZpp5UjYZ8d7WJyFZn8TLNfyVg23xyrXIjcS8lo5cQhclevcOKhhyQrRIlC7qVERNw/SFs9oLIhJUc2xj2cxWhbJOlO2rFwhFHAUrvIlDJ3Xxt8Bb1k7bIJ6Vj5etN5eS12WU4xKcXbJVY/jOcHNFMEF4t9UgEBRvondHLDg= 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=cu8upKj5; arc=none smtp.client-ip=209.85.216.74 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="cu8upKj5" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-323266b700cso2874378a91.0 for ; Thu, 14 Aug 2025 17:12:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216739; x=1755821539; darn=vger.kernel.org; h=content-transfer-encoding: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=+Fqu7wrx+Ay2x6fvOSsf35l8fx+Qfz/0UwNU0RPDTSM=; b=cu8upKj5/ryc6b/BhlHtU3pp7awy1mYIjuHAMNp1DoiF82zbGej51LshRMaU3CQOFC qAAqixmYvX+cBkSuDi1165ZpcwV9Evva3zmTasW0K4nNF7XkMxxJjCeR8mUmcS3eAvMI KrgjmpW9pBaVI3TGfU8nU8/GNdD/OxonwLxqpErttQKCjFzNZlCj8OAFCRikEyNFynTM ebZG3rdhX7al798sQ85HQOy1kKtUa5MSWyCSlGucGmhLnwRejSSTLWr0FEnQKqWht/x3 L70O8c8+gzlVTI9IRh6tjv6LcYB77125aGfxFehR4AnhGjrUJ170o6Zha2H8Zaq3d27K keiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216739; x=1755821539; h=content-transfer-encoding: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=+Fqu7wrx+Ay2x6fvOSsf35l8fx+Qfz/0UwNU0RPDTSM=; b=vxChbCSYyy3MMVEKN/E3l0jMAsclgiMEDgS0qsxFutkw+T/3Jz077a6VUKny90lK1d OUkvZUKnv9zosxwKADH393NTezUxz2kld7lbimlUDx/5RoLwXvrqSVnzDvkDdIrsXjDD vKnFXdSMt6UKtXaVDQH+cKargAAc0YQ0J9aVTWBB0bHgbtq1X6kJFCL9NRsuJvxXHz1H mTlaOPH4XJBd28kND1VlVOw6q4lZQVl56OWN8z33WTFdnRsEQ6VqMSAqsuHGZ3PH17xP 6xeujjJefgQvcEyo0mqrPqSVfRVo/lqv0s2wQZI3bePS2iPZTjuYZ4VmXYhTeZBfzctl 3Q+w== X-Forwarded-Encrypted: i=1; AJvYcCXsHExKmSZJQ+ptL3chyh6IBF8a4i517Ym+9zkl4Tp1AwW+LBMwXq985rMHCUP0jsNWO3LoQPuv7qcS/i4=@vger.kernel.org X-Gm-Message-State: AOJu0YzPzAPdQ3Yza9VgFrQMPZmD5jTKRDZFwz2XzgE/OdHu+eskfNq/ cj4f2g0EdHPGswExpOXTCMlzXXvOyXgm005fSm7Eotp9CW7Fl/j52SFpvfbsy0cQc5qo84nNeAM oVxfY7g== X-Google-Smtp-Source: AGHT+IHdvPjO+PSktSEguZkgDKjA9pak/bEJv7YrtAW2Eb4NHFPHoTx4HeFa5dmZWDF+LEswbee2ZeWv+GA= X-Received: from pjff7.prod.google.com ([2002:a17:90b:5627:b0:321:abeb:1d8a]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3fcf:b0:31e:998f:7b75 with SMTP id 98e67ed59e1d1-32341e0d69cmr359045a91.9.1755216739296; Thu, 14 Aug 2025 17:12:19 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:11:48 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-5-seanjc@google.com> Subject: [PATCH 6.1.y 04/21] KVM: nVMX: Defer SVI update to vmcs01 on EOI when L2 is active w/o VID From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Chao Gao [ Upstream commit 04bc93cf49d16d01753b95ddb5d4f230b809a991 ] If KVM emulates an EOI for L1's virtual APIC while L2 is active, defer updating GUEST_INTERUPT_STATUS.SVI, i.e. the VMCS's cache of the highest in-service IRQ, until L1 is active, as vmcs01, not vmcs02, needs to track vISR. The missed SVI update for vmcs01 can result in L1 interrupts being incorrectly blocked, e.g. if there is a pending interrupt with lower priority than the interrupt that was EOI'd. This bug only affects use cases where L1's vAPIC is effectively passed through to L2, e.g. in a pKVM scenario where L2 is L1's depriveleged host, as KVM will only emulate an EOI for L1's vAPIC if Virtual Interrupt Delivery (VID) is disabled in vmc12, and L1 isn't intercepting L2 accesses to its (virtual) APIC page (or if x2APIC is enabled, the EOI MSR). WARN() if KVM updates L1's ISR while L2 is active with VID enabled, as an EOI from L2 is supposed to affect L2's vAPIC, but still defer the update, to try to keep L1 alive. Specifically, KVM forwards all APICv-related VM-Exits to L1 via nested_vmx_l1_wants_exit(): case EXIT_REASON_APIC_ACCESS: case EXIT_REASON_APIC_WRITE: case EXIT_REASON_EOI_INDUCED: /* * The controls for "virtualize APIC accesses," "APIC- * register virtualization," and "virtual-interrupt * delivery" only come from vmcs12. */ return true; Fixes: c7c9c56ca26f ("x86, apicv: add virtual interrupt delivery support") Cc: stable@vger.kernel.org Link: https://lore.kernel.org/kvm/20230312180048.1778187-1-jason.cj.chen@in= tel.com Reported-by: Markku Ahvenj=C3=A4rvi Closes: https://lore.kernel.org/all/20240920080012.74405-1-mankku@gmail.com Cc: Janne Karhunen Signed-off-by: Chao Gao [sean: drop request, handle in VMX, write changelog] Tested-by: Chao Gao Link: https://lore.kernel.org/r/20241128000010.4051275-3-seanjc@google.com Signed-off-by: Sean Christopherson [sean: resolve minor syntactic conflict in lapic.h, account for lack of kvm_x86_call(), drop sanity check due to lack of wants_to_run] Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 11 +++++++++++ arch/x86/kvm/lapic.h | 1 + arch/x86/kvm/vmx/nested.c | 5 +++++ arch/x86/kvm/vmx/vmx.c | 16 ++++++++++++++++ arch/x86/kvm/vmx/vmx.h | 1 + 5 files changed, 34 insertions(+) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 3d65d6a023c9..9aae76b74417 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -640,6 +640,17 @@ static inline void apic_clear_isr(int vec, struct kvm_= lapic *apic) } } =20 +void kvm_apic_update_hwapic_isr(struct kvm_vcpu *vcpu) +{ + struct kvm_lapic *apic =3D vcpu->arch.apic; + + if (WARN_ON_ONCE(!lapic_in_kernel(vcpu)) || !apic->apicv_active) + return; + + static_call(kvm_x86_hwapic_isr_update)(vcpu, apic_find_highest_isr(apic)); +} +EXPORT_SYMBOL_GPL(kvm_apic_update_hwapic_isr); + int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu) { /* This may race with setting of irr in __apic_accept_irq() and diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index a5ac4a5a5179..e5d2dc58fcf8 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -122,6 +122,7 @@ int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr= _data *msr_info); int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s); int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s); enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu); +void kvm_apic_update_hwapic_isr(struct kvm_vcpu *vcpu); int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu); =20 u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 8052f8b7d8e1..d55f7edc0860 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -4839,6 +4839,11 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm= _exit_reason, kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu); } =20 + if (vmx->nested.update_vmcs01_hwapic_isr) { + vmx->nested.update_vmcs01_hwapic_isr =3D false; + kvm_apic_update_hwapic_isr(vcpu); + } + if ((vm_exit_reason !=3D -1) && (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))) vmx->nested.need_vmcs12_to_shadow_sync =3D true; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 721ba6ddb121..7b87fbc69b21 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6713,6 +6713,22 @@ static void vmx_hwapic_isr_update(struct kvm_vcpu *v= cpu, int max_isr) u16 status; u8 old; =20 + /* + * If L2 is active, defer the SVI update until vmcs01 is loaded, as SVI + * is only relevant for if and only if Virtual Interrupt Delivery is + * enabled in vmcs12, and if VID is enabled then L2 EOIs affect L2's + * vAPIC, not L1's vAPIC. KVM must update vmcs01 on the next nested + * VM-Exit, otherwise L1 with run with a stale SVI. + */ + if (is_guest_mode(vcpu)) { + /* + * KVM is supposed to forward intercepted L2 EOIs to L1 if VID + * is enabled in vmcs12; as above, the EOIs affect L2's vAPIC. + */ + to_vmx(vcpu)->nested.update_vmcs01_hwapic_isr =3D true; + return; + } + if (max_isr =3D=3D -1) max_isr =3D 0; =20 diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 9e0bb98b116d..8b4b149bd9c1 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -189,6 +189,7 @@ struct nested_vmx { bool reload_vmcs01_apic_access_page; bool update_vmcs01_cpu_dirty_logging; bool update_vmcs01_apicv_status; + bool update_vmcs01_hwapic_isr; =20 /* * Enlightened VMCS has been enabled. It does not mean that L1 has to --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 85E581514F7 for ; Fri, 15 Aug 2025 00:12:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216743; cv=none; b=enxIAWgUEPjwBbKXc4w77qgL/W/rny+pTc1+LEsi7bxbAy92JtjeWu5HqUqQFz6E0rqweP4JGWQK+mjsR2Wwqcov5r5khY9aOpHN6ldRlGljGkseeLRZAljJObwzgAXDdKZOUPpWjzeCiX5kOKfQ8a8mJCD3ytuEt/5zfeDcdlo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216743; c=relaxed/simple; bh=VhV9EFrTudTyrNyi+hXz4xZPaoxl5bt2eNREG6X3+KU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hOgrm7oOHe3NxzL3T92pxKHQ8nlgKVkXyRLDW+qOq1amlBKUbKFeqR/Vx/1/GTPKWbk5GKNoVvbxZfBhgF+uKCu0NoPUHZ7BLo7ixUrQS/iK6h4kUnRwW8CKtS3SZ2NpMJK8D5D0b05l0JMV+PaqurvQamOG44oogVeWPFDukmY= 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=zP5yQsCc; arc=none smtp.client-ip=209.85.216.74 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="zP5yQsCc" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-3232677ad11so1441683a91.1 for ; Thu, 14 Aug 2025 17:12:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216741; x=1755821541; 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=IwosmCXhK1vj9BTxghsFWvBqb4znPc+loO6bF0vRg/w=; b=zP5yQsCcy5QpL2kFvKjhxF2peyA3+u4o0TlE6elSG9Ivsg3bBJdnvJ9Y6jBYpmSYgn mr/drTREDgzrzZ3k+moyZMe7c2lexPaQxVUSvXEbwJe9XBPa3D/PFDSlHpvRvhMvi+NJ 0oSws3wpOReY9JsC+BpAMDvbEeiHD21IPSAjhuPoH3nRBKvJi7jULHYsW3eHJHACdNd7 XyKki0La52diHF9vDvZBqWsCgSa70F7LBiMzei4bB9/4ygQXGWPJhlfkxjU8U2LwfxlM 7OGAYfBIPHkeG16T5e54deb6zbKWgakdMIcXHC2+2/05dCB1KfkXfVmZ1o0A4kO/9WSN wgsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216741; x=1755821541; 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=IwosmCXhK1vj9BTxghsFWvBqb4znPc+loO6bF0vRg/w=; b=r3qyLjmD6e3kukwW3oGl4CT4YrU+R5tsmcbX8fnbTKwTK7eijPwkUSucHQBSnZPrZ8 2ymHmt09D2q6li8dhGvIdH/KNhbpl3QO3EmIoQv2ZpY7IjfHgDbDMqqHRnFj7N4aqHFk bDaI04tvVt2yqR9HUNZYvMqjIrAB5d0lwXtDAyH4rqVEUJsHPHSswV4zfEzorhSwZAfy l2XWeC0yn2bIBiMjirTJJ8vWGCVok2CUbJCKEvuL9rDNSz2TTE+sLJBf3E0HayVtAlUT Subb92R9aqxSvd1aURWs3OmlTCKncnjkqhSLIMFKHm0DJynOZVm0fwQj9sxiqx9pH6YT rWNA== X-Forwarded-Encrypted: i=1; AJvYcCXLrNxGiV9UV7liSOCHmBVAnREl3asbOSnC1wCDT0/2XV3UYWoUPyXlCZhwAWE82qLe+tuaKfQz88lzsEs=@vger.kernel.org X-Gm-Message-State: AOJu0YyNVSOCtX/EZ9DjmcUfj6CkewUN0Y8/YbODyRcr2b6AcoF2H2KC DTcJNFyOTTcfE7katt5WBcVd+HWNKLr1osG3icXndaxXYOR3I6p1xUSYJ6XeXuGumXqO8cgEYwX R2KV2sw== X-Google-Smtp-Source: AGHT+IGO/jPS6jdJmEtvnGY3PxwPeRyl+ObkL45xgwHrNfQdLHZ7iDlH5jvrHyrHHvz2++/SRO0Y4P+AtJ8= X-Received: from pjbpq1.prod.google.com ([2002:a17:90b:3d81:b0:31f:1ed:c76e]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:dfcd:b0:312:1ae9:1525 with SMTP id 98e67ed59e1d1-32341e17c8cmr356019a91.8.1755216740828; Thu, 14 Aug 2025 17:12:20 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:11:49 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-6-seanjc@google.com> Subject: [PATCH 6.1.y 05/21] KVM: x86: Take irqfds.lock when adding/deleting IRQ bypass producer From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit f1fb088d9cecde5c3066d8ff8846789667519b7d ] Take irqfds.lock when adding/deleting an IRQ bypass producer to ensure irqfd->producer isn't modified while kvm_irq_routing_update() is running. The only lock held when a producer is added/removed is irqbypass's mutex. Fixes: 872768800652 ("KVM: x86: select IRQ_BYPASS_MANAGER") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Message-ID: <20250404193923.1413163-5-seanjc@google.com> Signed-off-by: Paolo Bonzini [sean: account for lack of kvm_x86_call()] Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a6dc8f662fa4..08c4ad276ccb 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13385,16 +13385,22 @@ int kvm_arch_irq_bypass_add_producer(struct irq_b= ypass_consumer *cons, { struct kvm_kernel_irqfd *irqfd =3D container_of(cons, struct kvm_kernel_irqfd, consumer); + struct kvm *kvm =3D irqfd->kvm; int ret; =20 - irqfd->producer =3D prod; kvm_arch_start_assignment(irqfd->kvm); + + spin_lock_irq(&kvm->irqfds.lock); + irqfd->producer =3D prod; + ret =3D static_call(kvm_x86_pi_update_irte)(irqfd->kvm, prod->irq, irqfd->gsi, 1); - if (ret) kvm_arch_end_assignment(irqfd->kvm); =20 + spin_unlock_irq(&kvm->irqfds.lock); + + return ret; } =20 @@ -13404,9 +13410,9 @@ void kvm_arch_irq_bypass_del_producer(struct irq_by= pass_consumer *cons, int ret; struct kvm_kernel_irqfd *irqfd =3D container_of(cons, struct kvm_kernel_irqfd, consumer); + struct kvm *kvm =3D irqfd->kvm; =20 WARN_ON(irqfd->producer !=3D prod); - irqfd->producer =3D NULL; =20 /* * When producer of consumer is unregistered, we change back to @@ -13414,11 +13420,18 @@ void kvm_arch_irq_bypass_del_producer(struct irq_= bypass_consumer *cons, * when the irq is masked/disabled or the consumer side (KVM * int this case doesn't want to receive the interrupts. */ + spin_lock_irq(&kvm->irqfds.lock); + irqfd->producer =3D NULL; + + ret =3D static_call(kvm_x86_pi_update_irte)(irqfd->kvm, prod->irq, irqfd-= >gsi, 0); if (ret) printk(KERN_INFO "irq bypass consumer (token %p) unregistration" " fails: %d\n", irqfd->consumer.token, ret); =20 + spin_unlock_irq(&kvm->irqfds.lock); + + kvm_arch_end_assignment(irqfd->kvm); } =20 --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35: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 3BD671922C4 for ; Fri, 15 Aug 2025 00:12:23 +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=1755216744; cv=none; b=e000lpyGS0vF2xR3xAVsufPNTA1zbPg54p6eCSWGi9DOTaPik++I8R8TZddP6tJfVnwzwmwtwv40Hje8bHd0B0J7e6DLHSuAjdnvJHb2ZR5cxNoRcpXTStP7KqMGUXCN1JDOfOmnwATp+/xIwygjrzioj6QOYe3gjHFmN/w2zuU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216744; c=relaxed/simple; bh=O2LmGYHZhZkLGwfBk2Yp1f3Rs3CptBnqxMLK4Ir6eIg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RdEsmJGIE8l7iS5Cm5CcuJVgdQ6aJJtiTg1vTzfLGO5dcJMEHOREpr6n1Jfr1Gx1yyH/Jj8xKk3Trjzaf7lPX6PvLj86WEA4tdFX6ffpzz/lf+kDyYN2Ur3HdeYz/270uj8TtsnSe9Fev0yxQS7uh/VsS4HLy3TaCoKHpHMmR84= 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=BUZT9xXj; 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="BUZT9xXj" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2445806b18aso14744195ad.1 for ; Thu, 14 Aug 2025 17:12:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216742; x=1755821542; 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=pXQWA5FLczzcFgAGgva/tQ0VYzpmUU3vq4Bm8GhyjFA=; b=BUZT9xXjxbtNoMsY1W1el/OZHt/eLhsCx+Bmkg9/s71XRz3jI0McbmKGSxgQevDhe7 v31d4LoDYWYiWdOvsO4QIuUN7fZXAmp9Sa80BOB1qUzzDca2w34MVVBFGd3LXDAVrqsY BHDg2hDAPQCjZ2+Eg0n98WpzcR7EBEkHLPYke8itjCU9g9RNtmcIy4gUdb3bWIHHL3xH jWflMO5QbkqFTvc00plobFmeJVENrVckBs2t8x4rKxy1jJhY7iUd0ovu8WSrPvdzy5ql tql2fDiz9CMgWyEHwYz+1Z1XntgqnvVDIefXqe8hqEsswy4Pt8Hs5rT2tCTzyH4CyLHf pOCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216742; x=1755821542; 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=pXQWA5FLczzcFgAGgva/tQ0VYzpmUU3vq4Bm8GhyjFA=; b=QwNZ5mAFMVj+pQDD+pXOCaNvnAyn0AlpvHJJbJeBbAhadZiQ0JorQSDi//xLr3e2AX aDItOW5WpELVrxNq8dQ3MPtw5pYP5g+yWoZFxGqb/I/l2ctbT0/hFXOKwIeizeUQp53p Z1dEBpeOp1YA64MuEXrQgclKjxvorrFGWDc2ssg01+AmmSIjQki/1PNeYbWTzo885aOG KFqPZ+rJfxGU10AuYfrSxd76yJApzuQNOv83asdReUwz66Y6T+PednMmeQ+LDM8ffo/d YJsp4xnaHJBzlA7bsSgoz0fqXP5DwmREJqt6dbBb6uzBFSY/sxK4HRfH90L7wfqX2yh6 2JBw== X-Forwarded-Encrypted: i=1; AJvYcCWYCKrdvjapcowZ0kEVP030oapBSQrscUlfTpP/ADyXlTWtM4dm/a71ppBIq7EePpQIYwSW2/arZQO+7Uk=@vger.kernel.org X-Gm-Message-State: AOJu0Yy1pCjwryg++40mgYJeYDhET8K34TTB0GJgoeEXWjjb049MAyPj 5dZMwJ2g2mfuSYSmhcVYjElXmc3HNpDlqP3yUcJk32tOyVK5te54HpZ+fsbATYYL8m48I4MDbpm tOnEw0A== X-Google-Smtp-Source: AGHT+IEpd6403VG8BvXCtL5Wm0bfYaHW/IiE3Ya7RRt9tZra7RgrQ/Ti92XyDRtdQNwdGxQoM3swrEunIu8= X-Received: from pjuj12.prod.google.com ([2002:a17:90a:d00c:b0:314:29b4:453]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:dac1:b0:240:3db8:9adc with SMTP id d9443c01a7336-2446d5ac887mr1825575ad.4.1755216742609; Thu, 14 Aug 2025 17:12:22 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:11:50 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-7-seanjc@google.com> Subject: [PATCH 6.1.y 06/21] KVM: x86: Snapshot the host's DEBUGCTL in common x86 From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit fb71c795935652fa20eaf9517ca9547f5af99a76 ] Move KVM's snapshot of DEBUGCTL to kvm_vcpu_arch and take the snapshot in common x86, so that SVM can also use the snapshot. Opportunistically change the field to a u64. While bits 63:32 are reserved on AMD, not mentioned at all in Intel's SDM, and managed as an "unsigned long" by the kernel, DEBUGCTL is an MSR and therefore a 64-bit value. Reviewed-by: Xiaoyao Li Cc: stable@vger.kernel.org Reviewed-and-tested-by: Ravi Bangoria Link: https://lore.kernel.org/r/20250227222411.3490595-4-seanjc@google.com Signed-off-by: Sean Christopherson [sean: resolve minor syntatic conflict in vmx_vcpu_load()] Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/vmx.c | 8 ++------ arch/x86/kvm/vmx/vmx.h | 2 -- arch/x86/kvm/x86.c | 1 + 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 6db42ee82032..555c7bf35e28 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -677,6 +677,7 @@ struct kvm_vcpu_arch { u32 pkru; u32 hflags; u64 efer; + u64 host_debugctl; u64 apic_base; struct kvm_lapic *apic; /* kernel irqchip context */ bool load_eoi_exitmap_pending; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 7b87fbc69b21..c24da2cff208 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1418,13 +1418,9 @@ void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int c= pu, */ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) { - struct vcpu_vmx *vmx =3D to_vmx(vcpu); - vmx_vcpu_load_vmcs(vcpu, cpu, NULL); =20 vmx_vcpu_pi_load(vcpu, cpu); - - vmx->host_debugctlmsr =3D get_debugctlmsr(); } =20 static void vmx_vcpu_put(struct kvm_vcpu *vcpu) @@ -7275,8 +7271,8 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu) } =20 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ - if (vmx->host_debugctlmsr) - update_debugctlmsr(vmx->host_debugctlmsr); + if (vcpu->arch.host_debugctl) + update_debugctlmsr(vcpu->arch.host_debugctl); =20 #ifndef CONFIG_X86_64 /* diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 8b4b149bd9c1..357819872d80 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -352,8 +352,6 @@ struct vcpu_vmx { /* apic deadline value in host tsc */ u64 hv_deadline_tsc; =20 - unsigned long host_debugctlmsr; - /* * Only bits masked by msr_ia32_feature_control_valid_bits can be set in * msr_ia32_feature_control. FEAT_CTL_LOCKED is always included diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 08c4ad276ccb..2178f6bb8e90 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4742,6 +4742,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cp= u) =20 /* Save host pkru register if supported */ vcpu->arch.host_pkru =3D read_pkru(); + vcpu->arch.host_debugctl =3D get_debugctlmsr(); =20 /* Apply any externally detected TSC adjustments (due to suspend) */ if (unlikely(vcpu->arch.tsc_offset_adjustment)) { --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 07CA21B040B for ; Fri, 15 Aug 2025 00:12:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216746; cv=none; b=WY6/c7FRYc7QjGMZOa/z3UP4a0P2QCPlMB4u0wT487/7uZXT2hqw2cpaJ/CPCdOIWvVxU/dSVbjIFTz5wK/Yz6KhLginRP9m99zQVu2J8Hbz7zCZquvJXNMpql7mvPfUhi3xI40GjyiE7Yqk1TMZQeHsscu4+vt1+2RFDwOcEZ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216746; c=relaxed/simple; bh=BtM8dFTmM/9dZyw4RXoSLIMVKXsY5bNsgrn1HhBtzJo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pyJEWl5aGdxevLGgmrBWVFawIYnpVc2c2PC57PfBJMos77/ufezL7HAGfM/1p6hjYVq8ufG4oIMzl2U+0lBiHWu8MtlzRhKDldDWeRXsEvn1N6qMxcuTPcmAOzNf6mk9AlhVeA3vHHb0UVwPEGSi8OdXikd/PckaHc4EZC8FBFA= 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=ZXQDQea9; arc=none smtp.client-ip=209.85.215.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="ZXQDQea9" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b47174c667aso1077212a12.2 for ; Thu, 14 Aug 2025 17:12:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216744; x=1755821544; 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=BXUEX1H6LyLUJSMPiSF5Cl3sy6zgRvsqwEwkPUOpCvs=; b=ZXQDQea9fO2ch2wBlP47qxH3lBvsXLDT8Q0Z8cHAYvXRkqN/An+nXX+R8jQoftaGCD 1PP6S5w56GQXE9L/UoZb3RtGQNDVrM9i/+RHFXs5E/u9xUkKFyi6m5smhkTOdnzKx3V4 s0baNyKWiiW11R2eK7yzs/XpwTNIbEWX3JwhVBlfBK1ku2uJvMaDIjvROfjAOhccYSo6 59smYawOQe6h9LNTe0aGqESVeGU5ulg00wkqAgma/IHV9a8LK+YvNzf1V61OxX5LfZK8 DI9Vu2xZ4+mZ2ZhTtJt6qw/udEgvq+de297huT0oKmdNq8aex24DfVsinnL/1dJ13+Hd yQkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216744; x=1755821544; 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=BXUEX1H6LyLUJSMPiSF5Cl3sy6zgRvsqwEwkPUOpCvs=; b=Q8mGpnVk0uVJ2g8b1uYmIBssObH5wh8oDm9bHozKfanIlSSw7qC/15iZmojoGOpOIG a5yY0c2XdMgMBdtAYpTOGwKrLEqBUPZ4LJc71CXNDUCrYkmEAvBGUBnkWahC9XfljtQa 3nXBFfrGDFtQzPoWs4I3dIeJBPqK+rODx/ijMMCZt8sfXm3/1UZePeyRZESvHtDIVAV8 lbCuz7tVrobBmBd/RqWkLSBaKlh2bRfR6i+soA1He4y21EHS7JukgvjleJ/S8S7OCfaL ryWDm2HHzGTwtcLhyeRNr4q35ouU2EVjdMnj3NDyl39ljcMdHjRxO245C9dAXqsQrhb+ 5S6A== X-Forwarded-Encrypted: i=1; AJvYcCU30vxRg0F0TbTlRe6VLM1TtQgcs0GU13mZF54uiOGsgKgFDE0aNl788iEYtRFq4RvVDmUmK15QCn/X6f0=@vger.kernel.org X-Gm-Message-State: AOJu0Yy7BSFiEpfE4hWnraF9eb7S5ltojOvJhNvx3W7JYNgLRm87JDCQ y70enzdafN/vR1b3QlbsVjY50JGrv43NKZh7FnvzNf33n1GgYjdFi0SRsaeGqSfv9syi3sN3pRr 0EOzLLA== X-Google-Smtp-Source: AGHT+IEL5helDYMbTg+EjsCxZbXQyOUBux72Tc/5TNXET/7xtgbFbL1AxnUElCcBkw/pIUt2U1qDn9Z+2G0= X-Received: from pgg2.prod.google.com ([2002:a05:6a02:4d82:b0:b47:16f7:2be5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:9392:b0:240:252a:158e with SMTP id adf61e73a8af0-240d2d913d0mr350688637.3.1755216744472; Thu, 14 Aug 2025 17:12:24 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:11:51 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-8-seanjc@google.com> Subject: [PATCH 6.1.y 07/21] KVM: x86: Snapshot the host's DEBUGCTL after disabling IRQs From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit 189ecdb3e112da703ac0699f4ec76aa78122f911 ] Snapshot the host's DEBUGCTL after disabling IRQs, as perf can toggle debugctl bits from IRQ context, e.g. when enabling/disabling events via smp_call_function_single(). Taking the snapshot (long) before IRQs are disabled could result in KVM effectively clobbering DEBUGCTL due to using a stale snapshot. Cc: stable@vger.kernel.org Reviewed-and-tested-by: Ravi Bangoria Link: https://lore.kernel.org/r/20250227222411.3490595-6-seanjc@google.com Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2178f6bb8e90..0c3908544205 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4742,7 +4742,6 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cp= u) =20 /* Save host pkru register if supported */ vcpu->arch.host_pkru =3D read_pkru(); - vcpu->arch.host_debugctl =3D get_debugctlmsr(); =20 /* Apply any externally detected TSC adjustments (due to suspend) */ if (unlikely(vcpu->arch.tsc_offset_adjustment)) { @@ -10851,6 +10850,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) set_debugreg(0, 7); } =20 + vcpu->arch.host_debugctl =3D get_debugctlmsr(); + guest_timing_enter_irqoff(); =20 for (;;) { --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 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 0FE171C8631 for ; Fri, 15 Aug 2025 00:12:26 +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=1755216748; cv=none; b=X7rF9f97WTZf+mA+I6gHw3z8FAI3pDq4IcJoBZNB1bZidwf63S7BTUEAg2TI5tveZmRVHufcmvLYuYmQRMGp4404o7gHcR6ZLwBvRYGuYYgsrm3i1U7BQ0ByiEQbgr+x/hJscyF0EQtduSV/mpjiYNwprra0gJeAVgG3CZ4M4vY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216748; c=relaxed/simple; bh=PVWSzQsCuzhIQraDNHBUSRWbizU7d5//fml+dTGSHko=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rzo3luo4kq2kaO+m69E5kFDCFD065ZVML9cvJHbVnuZYncnDvIajTDHdWRRJ/IlStrlgjrAtYO4vf5J2qcc0ugpPTO3ik7z+ge9AyKbB7yNCtjVjjD59NNydYa4u67TzjxbFiIm4aUcAikoArUk3eZ0qsVSQ6KdAVuSFHjfysk4= 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=I1mBRpXY; 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="I1mBRpXY" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-32326e6c74eso1623877a91.3 for ; Thu, 14 Aug 2025 17:12:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216746; x=1755821546; 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=pVdj1r2b9ebCmTm8plsT1ommEw0GDtpTKl01m2bkq3Q=; b=I1mBRpXYmzpTElUrzsKxeqOlFdytnv9oWYsRbUcgq6c2VG/Cbx8jGGY3TICHbQnEgz 4VdsRsxLt9lpUhzVUSOzvyXJFNJha/2DBzV191Y6uCFtprVX/+BAmVslOdQDpX3PqAVj 8dHs7wP4eeypyCzclJxsaJoKWRuGoBWaAkKsuShNSrACEWUZodcu0NpRDe2bQRZDwoc7 fcBCV2b6PjW7nnAb4K1ALv66PDBVwZ9ShRJIDgUYdZhZ8jpZqq2JqoRkL0V/eGFtMaJT BB9aKEMHGgdKiRjEf+FpL8ikGPiYfXFVip0avrvj0n3mRRTBDSLytQKzTzDfCo6On8HN zILQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216746; x=1755821546; 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=pVdj1r2b9ebCmTm8plsT1ommEw0GDtpTKl01m2bkq3Q=; b=dfF53/fxIDq4arhy7noT2rcMVZ4xGOwIv0Jbx670ZC0d8MsoS2GxgoXfagGPggzecl qQlKuWWgWXIGrff3Uxl1ko18RtcsVkTQfLpRByufqFn40ggx+HoHEiDUBGuS/FaVdPJl UYfQEJdVn7dHXQxTzxDWOj9FVii1bVH4A2frznHKOvUzWY51tz2ECs9wjEc3wD3UC181 maQlXQ/J4bShaxB1+TrBiTCZ0OM31n2O0mExWT8fXx3LqmYIsZdphNFhVdkg6d7+cdCW mEXoOc9rMKdaV9IqPfU8VHT4csshe01ffNpe6CzA40AqDpyUiwTl8mSz7k9yKXy4XTco v1ag== X-Forwarded-Encrypted: i=1; AJvYcCXZFpFKPIcEdI5tWRB0rzkyTbYlqLsS5keiGqoKl/y1vkJz+zaJORK+Cldkp1fUrpgVjox7/69rTGpLNG0=@vger.kernel.org X-Gm-Message-State: AOJu0Ywvt6SODYkes9NKtDqEWbKM+Q3+6ucljKNYwfYw312eIbVQ8qdd HlN8R09ydRJL9WfbzrMkqFo/YulXADZnhVKUf1etYUCogNeINtO+JtBvLSRf5352GfxVGL9kgvd XDo5yFg== X-Google-Smtp-Source: AGHT+IHCdltiMGoEmzO852wzwsXGl3QojD4i2zmqNtHwTNCqS6gRwntRjdN0Liw8X0G78BCwZg+gEODIk28= X-Received: from pjbsj7.prod.google.com ([2002:a17:90b:2d87:b0:312:15b:e5d1]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3f88:b0:31e:cdfb:5f1f with SMTP id 98e67ed59e1d1-32341e11c6amr338244a91.14.1755216746371; Thu, 14 Aug 2025 17:12:26 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:11:52 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-9-seanjc@google.com> Subject: [PATCH 6.1.y 08/21] KVM: x86/pmu: Gate all "unimplemented MSR" prints on report_ignored_msrs From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit e76ae52747a82a548742107b4100e90da41a624d ] Add helpers to print unimplemented MSR accesses and condition all such prints on report_ignored_msrs, i.e. honor userspace's request to not print unimplemented MSRs. Even though vcpu_unimpl() is ratelimited, printing can still be problematic, e.g. if a print gets stalled when host userspace is writing MSRs during live migration, an effective stall can result in very noticeable disruption in the guest. E.g. the profile below was taken while calling KVM_SET_MSRS on the PMU counters while the PMU was disabled in KVM. - 99.75% 0.00% [.] __ioctl - __ioctl - 99.74% entry_SYSCALL_64_after_hwframe do_syscall_64 sys_ioctl - do_vfs_ioctl - 92.48% kvm_vcpu_ioctl - kvm_arch_vcpu_ioctl - 85.12% kvm_set_msr_ignored_check svm_set_msr kvm_set_msr_common printk vprintk_func vprintk_default vprintk_emit console_unlock call_console_drivers univ8250_console_write serial8250_console_write uart_console_write Reported-by: Aaron Lewis Reviewed-by: Vitaly Kuznetsov Link: https://lore.kernel.org/r/20230124234905.3774678-3-seanjc@google.com Stable-dep-of: 7d0cce6cbe71 ("KVM: VMX: Wrap all accesses to IA32_DEBUGCTL = with getter/setter APIs") Signed-off-by: Sasha Levin Signed-off-by: Sean Christopherson --- arch/x86/kvm/hyperv.c | 10 ++++------ arch/x86/kvm/svm/svm.c | 5 ++--- arch/x86/kvm/vmx/vmx.c | 4 +--- arch/x86/kvm/x86.c | 18 +++++------------- arch/x86/kvm/x86.h | 12 ++++++++++++ 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 28555bbd52e8..cb0a531e13c5 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -1406,8 +1406,7 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u= 32 msr, u64 data, case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: return syndbg_set_msr(vcpu, msr, data, host); default: - vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n", - msr, data); + kvm_pr_unimpl_wrmsr(vcpu, msr, data); return 1; } return 0; @@ -1528,8 +1527,7 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 = msr, u64 data, bool host) return 1; break; default: - vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n", - msr, data); + kvm_pr_unimpl_wrmsr(vcpu, msr, data); return 1; } =20 @@ -1581,7 +1579,7 @@ static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u= 32 msr, u64 *pdata, case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: return syndbg_get_msr(vcpu, msr, pdata, host); default: - vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); + kvm_pr_unimpl_rdmsr(vcpu, msr); return 1; } =20 @@ -1646,7 +1644,7 @@ static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 = msr, u64 *pdata, data =3D APIC_BUS_FREQUENCY; break; default: - vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); + kvm_pr_unimpl_rdmsr(vcpu, msr); return 1; } *pdata =3D data; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index b922f31d1415..2c0f9c7d1242 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3035,8 +3035,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct = msr_data *msr) break; case MSR_IA32_DEBUGCTLMSR: if (!lbrv) { - vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n", - __func__, data); + kvm_pr_unimpl_wrmsr(vcpu, ecx, data); break; } =20 @@ -3077,7 +3076,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct = msr_data *msr) case MSR_VM_CR: return svm_set_vm_cr(vcpu, data); case MSR_VM_IGNNE: - vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data); + kvm_pr_unimpl_wrmsr(vcpu, ecx, data); break; case MSR_AMD64_DE_CFG: { struct kvm_msr_entry msr_entry; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index c24da2cff208..390af16d9a67 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2140,9 +2140,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct = msr_data *msr_info) =20 invalid =3D data & ~vmx_get_supported_debugctl(vcpu, msr_info->host_init= iated); if (invalid & (DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR)) { - if (report_ignored_msrs) - vcpu_unimpl(vcpu, "%s: BTF|LBR in IA32_DEBUGCTLMSR 0x%llx, nop\n", - __func__, data); + kvm_pr_unimpl_wrmsr(vcpu, msr_index, data); data &=3D ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR); invalid &=3D ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR); } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0c3908544205..e7c73360890d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3573,7 +3573,6 @@ static void record_steal_time(struct kvm_vcpu *vcpu) =20 int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) { - bool pr =3D false; u32 msr =3D msr_info->index; u64 data =3D msr_info->data; =20 @@ -3625,15 +3624,13 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struc= t msr_data *msr_info) if (data =3D=3D BIT_ULL(18)) { vcpu->arch.msr_hwcr =3D data; } else if (data !=3D 0) { - vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n", - data); + kvm_pr_unimpl_wrmsr(vcpu, msr, data); return 1; } break; case MSR_FAM10H_MMIO_CONF_BASE: if (data !=3D 0) { - vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: " - "0x%llx\n", data); + kvm_pr_unimpl_wrmsr(vcpu, msr, data); return 1; } break; @@ -3813,16 +3810,13 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struc= t msr_data *msr_info) =20 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: - pr =3D true; - fallthrough; case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1: if (kvm_pmu_is_valid_msr(vcpu, msr)) return kvm_pmu_set_msr(vcpu, msr_info); =20 - if (pr || data !=3D 0) - vcpu_unimpl(vcpu, "disabled perfctr wrmsr: " - "0x%x data 0x%llx\n", msr, data); + if (data) + kvm_pr_unimpl_wrmsr(vcpu, msr, data); break; case MSR_K7_CLK_CTL: /* @@ -3849,9 +3843,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct = msr_data *msr_info) /* Drop writes to this legacy MSR -- see rdmsr * counterpart for further detail. */ - if (report_ignored_msrs) - vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n", - msr, data); + kvm_pr_unimpl_wrmsr(vcpu, msr, data); break; case MSR_AMD64_OSVW_ID_LENGTH: if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 9de72586f406..f3554bf05201 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -331,6 +331,18 @@ extern bool report_ignored_msrs; =20 extern bool eager_page_split; =20 +static inline void kvm_pr_unimpl_wrmsr(struct kvm_vcpu *vcpu, u32 msr, u64= data) +{ + if (report_ignored_msrs) + vcpu_unimpl(vcpu, "Unhandled WRMSR(0x%x) =3D 0x%llx\n", msr, data); +} + +static inline void kvm_pr_unimpl_rdmsr(struct kvm_vcpu *vcpu, u32 msr) +{ + if (report_ignored_msrs) + vcpu_unimpl(vcpu, "Unhandled RDMSR(0x%x)\n", msr); +} + static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec) { return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult, --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 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 BCFBD1DE2B4 for ; Fri, 15 Aug 2025 00:12:28 +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=1755216750; cv=none; b=fS6kxBFEauIPNLa+/nR9fKD4beLlDiZV8vGP53zxXn1g9eBAq/NLYPhFkl0SIMqiGIccwzYFZMZKVe1pZ/U/aCT8HO0d0LyyuieQRRB7CHa+SNfh4kKKrtV2ToEgD0LKt6NbB8W3Hw3L09SSeaKzPwQPYljP2t6J8/AkziGrPW8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216750; c=relaxed/simple; bh=b0MSDJUvRaRrmaqNzr3tH1ktx4FdSQpCWc/AwKrDnco=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=k5Li5VHoQzsP3/k8vioJw4G0eUWHp64X6h3sCOH/o3W0ix6QkX5vboItONHTeW5ois5W/TYBZEw8V/MB/3p9W6BmcmNVHKBXPqrQRGHdiq888x77+xhK+HpyC21Tg56exq1i3/e3K65dtOiBzXJxdWLxRm9Y6/fq8UDo356t0Dw= 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=V+NBhyCc; 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="V+NBhyCc" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-32326e46a20so2897666a91.3 for ; Thu, 14 Aug 2025 17:12:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216748; x=1755821548; 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=1dCGbgwW0NS5O3KDS8Qj8WR3qsePAzSTDbd1f03DmFA=; b=V+NBhyCcWEjqK2mdAkIDmiZ4gU5P9Js20MvASYk8QrbhU9k6K9lcxw3ODfZiwiPO8I 7h5vaqIXyZdP0doLI+utR7qIlwWIZB4r/onlGLOxXK6+M1qnGy0Qy1AK9VQedwsa772H C4Li/5+77Ncfc2dTWUZd/TJber/CeNBBkfyHb2G8AIu5kdtWnhCILaTbQiTRlsHwRlCH av5hy+XGE1ptcmWp3WuQSqRa9FF9VPIPmt3543eKsJyPLLZJC/I0GAl9dpIKV1K4qWY2 WMJnLS8HrFlQpcS5WYRx0Bq5GnhJKZVF03qe4CR9i4uwUz3R6BVhSDTNcFVRhSm4inzB AbuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216748; x=1755821548; 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=1dCGbgwW0NS5O3KDS8Qj8WR3qsePAzSTDbd1f03DmFA=; b=eypVqXDuPSiuRGbKD9it7YIEybEybmpnkZayhRBDC2LaKLqZYQPqmt6RKGGinV3am5 2FuYhjG6yop89BMeh4EfDLOdlaS3dTdTNAIX8ILZO4wpdB85msY5H5JfHzDVjzE1Jz73 ElBsz/nEMnOr/Q67TlHgeji5Xpj601qsoVNfmatwUJCKoFddhWxg2h18O9xkr4/kRso4 fvi3Fng7eKjpnCoL24UKBc2+XZx/jG0WlnaeHGe4ZEsUyNPiRvqUArFrUW6nFIaK/1xs yf8fs789d9cx8B9ViJXJTbmrYYqGsFMQYKMVGE2sjLfh4fHZPQTJ9opLy+8C0EZW82xh +1tA== X-Forwarded-Encrypted: i=1; AJvYcCV9TMcduYfuGNiyOgUOzJRtAh2PidLNel7dYl+5EtS0edeoEYPYRM/Hz1fALHaBg8aZ1vT35XRJtNfBa2Q=@vger.kernel.org X-Gm-Message-State: AOJu0YyakPJsjWi8jrky0wwdRpEF60di9r5SYXFgHnUqUEB5mB1/Ed8J 96cKWULjNsAjGkD7BHET2X4LjGr6JRG6BUh4o/N6+7HgVXz2YD5EyJx7PZysDIqI+RKjy8S8c8s MRf+PPw== X-Google-Smtp-Source: AGHT+IHSbQwafud8Qz/m7ae8IRhczfBCvOeRvHnNzuxVSbiy+AufqS0UNTfG33jEJIXJxNQLdXS3xFn+KhM= X-Received: from pjbqx15.prod.google.com ([2002:a17:90b:3e4f:b0:31f:37f:d381]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:224f:b0:31a:9004:899d with SMTP id 98e67ed59e1d1-3234213b9bemr306527a91.18.1755216748115; Thu, 14 Aug 2025 17:12:28 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:11:53 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-10-seanjc@google.com> Subject: [PATCH 6.1.y 09/21] KVM: x86: Plumb "force_immediate_exit" into kvm_entry() tracepoint From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit 9c9025ea003a03f967affd690f39b4ef3452c0f5 ] Annotate the kvm_entry() tracepoint with "immediate exit" when KVM is forcing a VM-Exit immediately after VM-Enter, e.g. when KVM wants to inject an event but needs to first complete some other operation. Knowing that KVM is (or isn't) forcing an exit is useful information when debugging issues related to event injection. Suggested-by: Maxim Levitsky Link: https://lore.kernel.org/r/20240110012705.506918-2-seanjc@google.com Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 3 ++- arch/x86/kvm/svm/svm.c | 5 +++-- arch/x86/kvm/trace.h | 9 ++++++--- arch/x86/kvm/vmx/vmx.c | 4 ++-- arch/x86/kvm/x86.c | 2 +- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 555c7bf35e28..93f523762854 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1528,7 +1528,8 @@ struct kvm_x86_ops { void (*flush_tlb_guest)(struct kvm_vcpu *vcpu); =20 int (*vcpu_pre_run)(struct kvm_vcpu *vcpu); - enum exit_fastpath_completion (*vcpu_run)(struct kvm_vcpu *vcpu); + enum exit_fastpath_completion (*vcpu_run)(struct kvm_vcpu *vcpu, + bool force_immediate_exit); int (*handle_exit)(struct kvm_vcpu *vcpu, enum exit_fastpath_completion exit_fastpath); int (*skip_emulated_instruction)(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 2c0f9c7d1242..b4283c2358a6 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4005,12 +4005,13 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_= vcpu *vcpu, bool spec_ctrl_in guest_state_exit_irqoff(); } =20 -static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) +static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, + bool force_immediate_exit) { struct vcpu_svm *svm =3D to_svm(vcpu); bool spec_ctrl_intercepted =3D msr_write_intercepted(vcpu, MSR_IA32_SPEC_= CTRL); =20 - trace_kvm_entry(vcpu); + trace_kvm_entry(vcpu, force_immediate_exit); =20 svm->vmcb->save.rax =3D vcpu->arch.regs[VCPU_REGS_RAX]; svm->vmcb->save.rsp =3D vcpu->arch.regs[VCPU_REGS_RSP]; diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index 6c1dcf44c4fa..ab407bc00d84 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -15,20 +15,23 @@ * Tracepoint for guest mode entry. */ TRACE_EVENT(kvm_entry, - TP_PROTO(struct kvm_vcpu *vcpu), - TP_ARGS(vcpu), + TP_PROTO(struct kvm_vcpu *vcpu, bool force_immediate_exit), + TP_ARGS(vcpu, force_immediate_exit), =20 TP_STRUCT__entry( __field( unsigned int, vcpu_id ) __field( unsigned long, rip ) + __field( bool, immediate_exit ) ), =20 TP_fast_assign( __entry->vcpu_id =3D vcpu->vcpu_id; __entry->rip =3D kvm_rip_read(vcpu); + __entry->immediate_exit =3D force_immediate_exit; ), =20 - TP_printk("vcpu %u, rip 0x%lx", __entry->vcpu_id, __entry->rip) + TP_printk("vcpu %u, rip 0x%lx%s", __entry->vcpu_id, __entry->rip, + __entry->immediate_exit ? "[immediate exit]" : "") ); =20 /* diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 390af16d9a67..0b495979a02b 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7171,7 +7171,7 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vc= pu *vcpu, guest_state_exit_irqoff(); } =20 -static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu) +static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, bool force_immediate= _exit) { struct vcpu_vmx *vmx =3D to_vmx(vcpu); unsigned long cr3, cr4; @@ -7198,7 +7198,7 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu) return EXIT_FASTPATH_NONE; } =20 - trace_kvm_entry(vcpu); + trace_kvm_entry(vcpu, force_immediate_exit); =20 if (vmx->ple_window_dirty) { vmx->ple_window_dirty =3D false; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e7c73360890d..652f90ad7107 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10856,7 +10856,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) WARN_ON_ONCE((kvm_vcpu_apicv_activated(vcpu) !=3D kvm_vcpu_apicv_active(= vcpu)) && (kvm_get_apic_mode(vcpu) !=3D LAPIC_MODE_DISABLED)); =20 - exit_fastpath =3D static_call(kvm_x86_vcpu_run)(vcpu); + exit_fastpath =3D static_call(kvm_x86_vcpu_run)(vcpu, req_immediate_exit= ); if (likely(exit_fastpath !=3D EXIT_FASTPATH_REENTER_GUEST)) break; =20 --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35: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 8CF1F1E1A3B for ; Fri, 15 Aug 2025 00:12:30 +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=1755216751; cv=none; b=SCrjwkUDKS7K5qoq7lRF65SxqV1oRbhVbM7hSfQnxkBPFXarqRBTtSOFOTQFHVeUVmwl8G422Kytnn79VZdmE3LKQStD0vmxIKY4JOobP2URQ3WyKEWz+pJukUE0M+QdFLJJiYMOStMMr37E446pZUE/Tyu6qKOggVW2LSNZAvw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216751; c=relaxed/simple; bh=bvDp4MOWnvUg7G9Pwr/XZYkF2LA55VcOt3qLgd8IQvo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fpmRDrdszHd3G2OGVp1m5ZDSpjBS+cUryP1umIlAgNcczzdkdTgjoWkjwdP9q4F1V2n8xPFh5g5ahAFaeJ6c7WMusz/Qj6MBaXiciYPprqet/M7hOp00t/PSJ9lm9ZIdOkwfLNjV7l+K5JSS0FTkeLojq/xDiBIbMLutP5BcXaw= 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=ZgSVV3qi; 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="ZgSVV3qi" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-24458345f5dso14554725ad.3 for ; Thu, 14 Aug 2025 17:12:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216750; x=1755821550; 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=BqNFRdZp4dZOuhcg+tVI2WIxCzfFJg3qux8SRQQatZg=; b=ZgSVV3qiulTNeH5/itel50oP2lVOq3E3lm/pQ3Wgxy9TnNXMM22KFJXI4jjs7dYTuB puJYvLma2WWF+POCRCr8svbkF/l5F54wU9SOzRE+j0dRLHa3anjqztVkoMXX4dmaX6wh u+0e2X15Vb1cXIx3t42rvUBt1eDaeU4le4tn7hHMlD/OCibta3c+nx9eARZt9fWTB1vI q70MwMGl7BFWdTyMNe8cuiUWd/IlzhZkI/CqoRE3ns+MwKFniAgAa7P4z8c55o/HNBkm Ms/VSkCmffmJOD/z2VkRc2FJJBIkedzc7etA6FaPcCDwhZIoeT6gXAxjBB8vqKj7rckB QI8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216750; x=1755821550; 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=BqNFRdZp4dZOuhcg+tVI2WIxCzfFJg3qux8SRQQatZg=; b=Dd+OzsjaiqZAhxk3LrHgoKih+Re3sBoZXTIzaXP4WVJ5NwUXHT4HRhm+MOoiGJkEn4 mC46vPZt7x4LdmxePZyr4dR84GbDjmoGg3hcmSn7usJ7i3DQ9A07KbEzGRpNDgHXdwvX qY/VevblrKkEf9TpofoNI5W7NjisTnsE2S4u3nMeg328w8vRe/Adylm5HARRL9dlUnES lclFmCi5ovq/01n5Ys+g60E6eyB/s4ya59m9grOTriSMs1fr+xPCPOLr8J0gTokBJqP6 /SRRw/HTUhgQkIE88VVoG2v6xQaO2zK94F6xx3AHhejnW71FzyZBA22oi7gS2PHHZJSE /g9Q== X-Forwarded-Encrypted: i=1; AJvYcCVOMhK/BL4+JQ9TibUWF+HxSbZc3pPkKHLfr1zgX5IXcEatwHQBIWmNjs60Zefsx9Fkux49PUJc1T4CJ0s=@vger.kernel.org X-Gm-Message-State: AOJu0YxCsUAC/kjIDmEKNpXIZtHpHhe4jluiGXnYBPaDpM8V9ylL6j9n SDEhLmTnSi3GIZPxy+FzvNPNEUpnkUFcvs1nj1qjE5AZHHaD+q/l5rtKm/IpsMGFH8SEdUvPajG pj+tFew== X-Google-Smtp-Source: AGHT+IHsUD4IEXcOJDddmNyAVMtxzUdZNE4LwkwjB6Xni2icG5GH9wBulaAHQkFDZ2ytKoB6uP5ydyFou3Y= X-Received: from plbkc6.prod.google.com ([2002:a17:903:33c6:b0:240:7619:64aa]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:dac6:b0:243:ead:f694 with SMTP id d9443c01a7336-2446d6f6e46mr1645445ad.17.1755216749987; Thu, 14 Aug 2025 17:12:29 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:11:54 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-11-seanjc@google.com> Subject: [PATCH 6.1.y 10/21] KVM: VMX: Re-enter guest in fastpath for "spurious" preemption timer exits From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit e6b5d16bbd2d4c8259ad76aa33de80d561aba5f9 ] Re-enter the guest in the fast path if VMX preeemption timer VM-Exit was "spurious", i.e. if KVM "soft disabled" the timer by writing -1u and by some miracle the timer expired before any other VM-Exit occurred. This is just an intermediate step to cleaning up the preemption timer handling, optimizing these types of spurious VM-Exits is not interesting as they are extremely rare/infrequent. Link: https://lore.kernel.org/r/20240110012705.506918-3-seanjc@google.com Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 0b495979a02b..96bbccd9477c 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5933,8 +5933,15 @@ static fastpath_t handle_fastpath_preemption_timer(s= truct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx =3D to_vmx(vcpu); =20 - if (!vmx->req_immediate_exit && - !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) { + /* + * In the *extremely* unlikely scenario that this is a spurious VM-Exit + * due to the timer expiring while it was "soft" disabled, just eat the + * exit and re-enter the guest. + */ + if (unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) + return EXIT_FASTPATH_REENTER_GUEST; + + if (!vmx->req_immediate_exit) { kvm_lapic_expired_hv_timer(vcpu); return EXIT_FASTPATH_REENTER_GUEST; } --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (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 B903F1F0984 for ; Fri, 15 Aug 2025 00:12:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216754; cv=none; b=aelp8X5SCO9HcUZx38oj1qbzH3LhX06m4546fxA0+90q/092NFAz6MoysvdeNPu+Q+R957KgwqrjkZ7n4VWbnWqG2u2vu1XbO6z8ax5cgce1D1tcsDuMWF1IwlZ6bXqvrXk+ixWqnA+S5xy5nZoz+Jt7QQKk8vTEYlqCApf8Bvc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216754; c=relaxed/simple; bh=LCntfj91WO8jUz7s5IHbhKB5AC6FTcDEfOnPfQM51s0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DKW319pf1N4hCRDrMCV3wFdFtWlU5Wb+hTTsLCTItUtZuicAvgA1McFZbb/dUP81pdz+dat4QsdawC5XimjPbKX1M6UvmUDivc2JKb76J5YYwVyzrTZMsAJILfwcqHEbmXpi+m1IZqJs+ZetqH2zFAqrzWxHEPRYqP9xeUbnpsw= 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=RpVcUAZo; arc=none smtp.client-ip=209.85.215.201 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="RpVcUAZo" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b471757d82fso945789a12.3 for ; Thu, 14 Aug 2025 17:12:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216752; x=1755821552; 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=yKPV7LpuHxf54DscfDg8szYhzWL5y/uRD3gLziYibUE=; b=RpVcUAZooJRyNam/5TaanJYubJp2VutesV4Be8wudSHmmwDkQqmkZ89wrfsNzCOIVj tPDLUqrNyKHC9Ku0MP99T7mRp4/A6x40izHuSammaqNWrLpyeDcR78UVYJrbp/fDdgnB i7XiEFQcHxGInA37xpeWgYBp6skhNTHMyzV38vCiGbAXCaQRmpxBgtjxUTeaGFiRay+q IMhd2OwkBE2I9V6pZqsJdCo6WRhwAeHx3S8xB0QfOeIHDSUdp/ab1FW37hWN+qJZd8gw WfOLeYaRBgaRSC1JlbRcS2zHNBKMlvJMcgytRp54bpKE/j8pdA4Gzw3+XvyOeH46fw6e rQ/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216752; x=1755821552; 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=yKPV7LpuHxf54DscfDg8szYhzWL5y/uRD3gLziYibUE=; b=I/D0zwahKgUnvRckW1YF5Xwvn2y3Ns04UNF0UhMpoe7p7CliUZVdbgFDHjWtL2pVhp L0aCT1hggorG+5JEZFppkQ6Mw19X6XEvJi7ZubEzjnJCBikRmxdUXQ7O5Adk3PryHEfu zIra53rfYWTTysvPRQaqiM23Y9kfk1z6xWucHEyrHYszo3KEvFPErvVQdSQNgD6QUSs4 VmWlwIUl+vWvajczE86JEGqM7UzN+aLDzXo1km6lNBZSMvfzsrkKUhqcCrGUIkCmcj6U Dfrcb5/uCxy4iwFYiqkfjeCaryKAbL7eq+XczbaxrKmrhMZ4XAowjVv/PGY8E0xATrX8 EUYQ== X-Forwarded-Encrypted: i=1; AJvYcCWdFLpC6yLzGKyLNOzwQHKRG/Ozp5UZwEji6zFcclqTOtizyFb/XeaxD2ubzWfEoMISTHV+8fXqVZa67r0=@vger.kernel.org X-Gm-Message-State: AOJu0YymZYnkwBXm6rbWkrAcHE/VjWqeje5IC0Msr5N0RhyVs+q/SCDU 9F2UcQs0U/Ci1nOwY/c8+9zSGSxr4KdSPtCybTAQJGlqxvESQKbHgbZhGjj9sZ+20wfVIWgV+/9 hkatpBA== X-Google-Smtp-Source: AGHT+IHx6QGco7q/Svyh1zldivJ2YnlH9/HCz3WBRxb7BjTBtbIgCAz0ycOEnw++JSEa9trdl9BE0YwnK9E= X-Received: from pjbsl16.prod.google.com ([2002:a17:90b:2e10:b0:31f:b2f:aeed]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:394e:b0:31e:3bbc:e9e6 with SMTP id 98e67ed59e1d1-3234213f914mr241081a91.19.1755216751796; Thu, 14 Aug 2025 17:12:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:11:55 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-12-seanjc@google.com> Subject: [PATCH 6.1.y 11/21] KVM: VMX: Handle forced exit due to preemption timer in fastpath From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit 11776aa0cfa7d007ad1799b1553bdcbd830e5010 ] Handle VMX preemption timer VM-Exits due to KVM forcing an exit in the exit fastpath, i.e. avoid calling back into handle_preemption_timer() for the same exit. There is no work to be done for forced exits, as the name suggests the goal is purely to get control back in KVM. In addition to shaving a few cycles, this will allow cleanly separating handle_fastpath_preemption_timer() from handle_preemption_timer(), e.g. it's not immediately obvious why _apparently_ calling handle_fastpath_preemption_timer() twice on a "slow" exit is necessary: the "slow" call is necessary to handle exits from L2, which are excluded from the fastpath by vmx_vcpu_run(). Link: https://lore.kernel.org/r/20240110012705.506918-4-seanjc@google.com Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 96bbccd9477c..c804ad001a79 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5941,12 +5941,15 @@ static fastpath_t handle_fastpath_preemption_timer(= struct kvm_vcpu *vcpu) if (unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) return EXIT_FASTPATH_REENTER_GUEST; =20 - if (!vmx->req_immediate_exit) { - kvm_lapic_expired_hv_timer(vcpu); - return EXIT_FASTPATH_REENTER_GUEST; - } + /* + * If the timer expired because KVM used it to force an immediate exit, + * then mission accomplished. + */ + if (vmx->req_immediate_exit) + return EXIT_FASTPATH_EXIT_HANDLED; =20 - return EXIT_FASTPATH_NONE; + kvm_lapic_expired_hv_timer(vcpu); + return EXIT_FASTPATH_REENTER_GUEST; } =20 static int handle_preemption_timer(struct kvm_vcpu *vcpu) --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (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 40BF71F561D for ; Fri, 15 Aug 2025 00:12:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216755; cv=none; b=hScMG0kj0KibRi9KajNO2QtlippALKq3tZeue6YvdhQ35YhvTHwhESm8fV3mrwL7tKjS7wlnpCSALL1YUz+Rx4mxMR9H72LKzB8luR5rNvEmcHNWfjlfA/RFi+OBxfHMFIz2C+10wm8ehxYwF2CtBaMixZR5z1qd9FnKzm1+XO0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216755; c=relaxed/simple; bh=J5bjGgVJv9hmsbY0pSYL5E2uFR0Opy+IWAryQkIYkd0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qetrvaIrweOkacnLGEFt3Jx+a/wCtleefPbNMUGzwbPA++22etPT4ZXYgMcvORqt04epN73+Es41oalfVnfyduXVcpqdHbaEkWrPpZA4f3u620HyrRBCdeSlEik9sXoYAjzU567H5JJgJy5NDK56kYkw4OC4iMcJQtNOdBNBNEg= 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=K5iFbDKN; arc=none smtp.client-ip=209.85.215.201 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="K5iFbDKN" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b4716fb7f2aso983219a12.0 for ; Thu, 14 Aug 2025 17:12:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216753; x=1755821553; 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=2wMy9TJhpZES3QcM2QFYJyQalMsJ3q38AGM7XgsPeU0=; b=K5iFbDKNiuvqtn2yUuJONz8cPEPByRqTrosxRvmAVp/RUDlzbr7WWcVlQ76mL1wKPs N9JSPirjGDQC9tpGRlv0H2+gI4bZKN4F6nyFc2CtDLT0pXw+jCWRxwzMGjlKmsx5c1fh qqBtPezgy2ptM1KbEdhI5952rG0g3csv+o15i7ZiYTRKEYlzmX5rAsHCjb3drF/gxDlT ikFi1WfbDN75GKFmrxTu9oSxfq/kNPciBwUNcvMs+Y5VYj7sLOu6WPNEHhXtqQ08CwEW 3r9Xafi2LVQiMWZEudmujWpbvwCjeQGdaFM6WSZ49VTQ5dfq4wlzADV8EDqgKdeaKMYt IUkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216753; x=1755821553; 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=2wMy9TJhpZES3QcM2QFYJyQalMsJ3q38AGM7XgsPeU0=; b=Vqrb9PbVeCp30v0lrspDWBstPHvzQjpqItqDhC8/unF9ZioWkNL7Uzf2+sLEs3rX1K kAQiTKvbFn71GAkpiXe16ddoezViaV/KqiU7OEp/P3o0gPkR9C8QBk8/FJELwVOwErp7 VEDl4Vl/Bt1PjFWs8x+5YboUsNPZjwPmYl9C4fTt4n1+/jC25q6vQhtrH6+XU9qKfNSZ TT0EokvBDtTKdbTdyYthglfPKicUjfVWXBd6/4ltDFZOJs4MrKLItJ8TSSx3Igan3pHR dap/I8QmK/EUaeEIUqcPlK2CDavRq6mnSGebbnT3MV26IfACZ0DEGUsWN4jSdRL20xhH 8nyw== X-Forwarded-Encrypted: i=1; AJvYcCXlqmYyzdIpXD4Hmr27tz1QfrmR/QpGHZdOVk9g2FkCAzwSlhZ3aBHS/6jyat6QAd4rWrW+qp/VVlGs7Q8=@vger.kernel.org X-Gm-Message-State: AOJu0YyblWlyirbkLw2UInAopYhK3z6ZrhMLdvQr3SxIFB5uZ8MafLAo n5xSi9iMLX9j7c15RY05KaIf0AZloKI/8RPdvLi6JLya2eaB8Z6iXyIGOFPLiPajW7SnbaJgsQ0 e/FHH7g== X-Google-Smtp-Source: AGHT+IEgo7vJgzcLT1RHaYoSqGTHO6Al4/b0zR+cP3g/V7QzZ9vp0b/qWYdJWt1FD0Lbuih760/NY1kEXkU= X-Received: from pjbnc16.prod.google.com ([2002:a17:90b:37d0:b0:312:dbc:f731]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:3c6f:b0:23f:df56:c74c with SMTP id d9443c01a7336-2446d715b0bmr1607785ad.14.1755216753639; Thu, 14 Aug 2025 17:12:33 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:11:56 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-13-seanjc@google.com> Subject: [PATCH 6.1.y 12/21] KVM: x86: Move handling of is_guest_mode() into fastpath exit handlers From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit bf1a49436ea37b98dd2f37c57608951d0e28eecc ] Let the fastpath code decide which exits can/can't be handled in the fastpath when L2 is active, e.g. when KVM generates a VMX preemption timer exit to forcefully regain control, there is no "work" to be done and so such exits can be handled in the fastpath regardless of whether L1 or L2 is active. Moving the is_guest_mode() check into the fastpath code also makes it easier to see that L2 isn't allowed to use the fastpath in most cases, e.g. it's not immediately obvious why handle_fastpath_preemption_timer() is called from the fastpath and the normal path. Link: https://lore.kernel.org/r/20240110012705.506918-5-seanjc@google.com Signed-off-by: Sean Christopherson [sean: resolve syntactic conflict in svm_exit_handlers_fastpath()] Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 6 +++--- arch/x86/kvm/vmx/vmx.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index b4283c2358a6..337a304d211b 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3964,6 +3964,9 @@ static fastpath_t svm_exit_handlers_fastpath(struct k= vm_vcpu *vcpu) { struct vmcb_control_area *control =3D &to_svm(vcpu)->vmcb->control; =20 + if (is_guest_mode(vcpu)) + return EXIT_FASTPATH_NONE; + /* * Note, the next RIP must be provided as SRCU isn't held, i.e. KVM * can't read guest memory (dereference memslots) to decode the WRMSR. @@ -4127,9 +4130,6 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_= vcpu *vcpu, =20 svm_complete_interrupts(vcpu); =20 - if (is_guest_mode(vcpu)) - return EXIT_FASTPATH_NONE; - return svm_exit_handlers_fastpath(vcpu); } =20 diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index c804ad001a79..18ceed9046a9 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7138,6 +7138,9 @@ void noinstr vmx_spec_ctrl_restore_host(struct vcpu_v= mx *vmx, =20 static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu) { + if (is_guest_mode(vcpu)) + return EXIT_FASTPATH_NONE; + switch (to_vmx(vcpu)->exit_reason.basic) { case EXIT_REASON_MSR_WRITE: return handle_fastpath_set_msr_irqoff(vcpu); @@ -7337,9 +7340,6 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu,= bool force_immediate_exit) vmx_recover_nmi_blocking(vmx); vmx_complete_interrupts(vmx); =20 - if (is_guest_mode(vcpu)) - return EXIT_FASTPATH_NONE; - return vmx_exit_handlers_fastpath(vcpu); } =20 --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 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 E279B1FF1D1 for ; Fri, 15 Aug 2025 00:12:35 +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=1755216757; cv=none; b=OfkgNIRmxpryyUYqT2gETcjpuw4Cxjlyjv6Vj0OGsktYnuLm/bqsfOVONDeJQxj7Htb4Wrnn+cJ7U4xKo/ySij3T6qCjfc8F0vfwMxPzD5urJoglO7wtjalRWkUPYtq3tglHgId8Bn+uAuveKMMDfoqNH6yJFQh96jokKs7p0v8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216757; c=relaxed/simple; bh=K45y4oY618XxqPpW0fSd+M48YvCVJ5OhxcpoKJCPJLU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fIFyDK6Aju3Z65M/1PKkn55IFgVhKrLgHhhcOMuS/MwG5hCTHp9m4b4fZf66dckipXU6+MaKXMLEDs4dMmaTNzNnZspIJoANJ4FpMhORtd2cMye5sGy+E9TzEM58ZrWA2OQENyFG40+3bMAc+avxqi571Dd3pr/tRRL/uNZnV/E= 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=TvqmdVpv; 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="TvqmdVpv" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-323266d8396so1503018a91.0 for ; Thu, 14 Aug 2025 17:12:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216755; x=1755821555; 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=A081K7Vxl6mjJzXEGmaHtEMcM0O3QUXdDAu/aDF05mY=; b=TvqmdVpvY/zWAxyxp+LBS78F/dqrCLAIpPNVxEBDvgOtf27t6irErUMg0vqp7BHHym XQBtVNP3raT1m7x3iu7VHCA+kbchYgOkOLU6JTgaGKofTjcn8Ns5E46Edq0/SKd1C+oj WlURRKFt/OLJFmlWIekzk2svqgB+QACHkunewOINHnyWebx+MT9ngR15A97nzGzp6mkx cNjDnDOCBw54pSg4oSAqDSXapefr+jMb5BUjuXm+7Noe/SI0TGBVMwmEjU1IzGJNVEcG +VfCF3HRYZKP4icpnDbpo6mOFULGhqs3rGe3jP+gI2P4Z4AAvM+2606i/9Qe3+l6pPPy RClw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216755; x=1755821555; 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=A081K7Vxl6mjJzXEGmaHtEMcM0O3QUXdDAu/aDF05mY=; b=oazVNllgQisibkwn+HiqnT4qbcV254q/pSvznH1QEfdy04dpksQQfZmDjbHXdDArdS jveEtywm/Q8GOiezo1Io7Houj6Ho8n3drfArIlXpP+ncyQJ8wkv79lcq3dhIF3rErjv6 XdeXEs8wA5zBHkfpQeCqzyjaBKaLI1LXW4rqsRXbgTozLD6/Hv1po3F3/l02zy+Ti+cN n2FzJohVluhR5KtwRKkC86ZGMPZ3faE376aHdq/PBv37XWDCrYzrie5I8LPL6ndi1AYY nmsirTyz6MaeT4vxB85VX8HVhhvEr9eji+8zJETv/mOVx1pwX073MIUgFm28ULyoPA4U dDlA== X-Forwarded-Encrypted: i=1; AJvYcCUbfhIZ662GnU/aoYivBmZSe5TCGzoKWyidiIWo/Eb6FqkDLZi335Z2VtPmEmqZPpqKip2J2BZnlSJB3Uk=@vger.kernel.org X-Gm-Message-State: AOJu0YyA5SDdGdpQ5hN4WTKpEIkPFkl11N++oujtHSCjSk1Tleb/7rVZ 4yLhFFTMML8d86CzWTkBzHjG1dv5JdYxwOaiVmPSqjW8vfomE5U3xQYoaqPZ719Ar97r5IBC9+A i1bWBQQ== X-Google-Smtp-Source: AGHT+IHyQTBLqAZEkEEr74CWBCFhJKsIaas1PzPmgFEdWLst5iOyF6eTrA8Q7itvt8DfCaBEBiBohqtDZ/4= X-Received: from pjbst7.prod.google.com ([2002:a17:90b:1fc7:b0:321:6ddc:33a0]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:278a:b0:323:28ac:3c59 with SMTP id 98e67ed59e1d1-32341ec4ad6mr240224a91.13.1755216755117; Thu, 14 Aug 2025 17:12:35 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:11:57 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-14-seanjc@google.com> Subject: [PATCH 6.1.y 13/21] KVM: VMX: Handle KVM-induced preemption timer exits in fastpath for L2 From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit 7b3d1bbf8d68d76fb21210932a5e8ed8ea80dbcc ] Eat VMX treemption timer exits in the fastpath regardless of whether L1 or L2 is active. The VM-Exit is 100% KVM-induced, i.e. there is nothing directly related to the exit that KVM needs to do on behalf of the guest, thus there is no reason to wait until the slow path to do nothing. Opportunistically add comments explaining why preemption timer exits for emulating the guest's APIC timer need to go down the slow path. Link: https://lore.kernel.org/r/20240110012705.506918-6-seanjc@google.com Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 18ceed9046a9..4db9d41d988c 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5948,13 +5948,26 @@ static fastpath_t handle_fastpath_preemption_timer(= struct kvm_vcpu *vcpu) if (vmx->req_immediate_exit) return EXIT_FASTPATH_EXIT_HANDLED; =20 + /* + * If L2 is active, go down the slow path as emulating the guest timer + * expiration likely requires synthesizing a nested VM-Exit. + */ + if (is_guest_mode(vcpu)) + return EXIT_FASTPATH_NONE; + kvm_lapic_expired_hv_timer(vcpu); return EXIT_FASTPATH_REENTER_GUEST; } =20 static int handle_preemption_timer(struct kvm_vcpu *vcpu) { - handle_fastpath_preemption_timer(vcpu); + /* + * This non-fastpath handler is reached if and only if the preemption + * timer was being used to emulate a guest timer while L2 is active. + * All other scenarios are supposed to be handled in the fastpath. + */ + WARN_ON_ONCE(!is_guest_mode(vcpu)); + kvm_lapic_expired_hv_timer(vcpu); return 1; } =20 @@ -7138,7 +7151,12 @@ void noinstr vmx_spec_ctrl_restore_host(struct vcpu_= vmx *vmx, =20 static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu) { - if (is_guest_mode(vcpu)) + /* + * If L2 is active, some VMX preemption timer exits can be handled in + * the fastpath even, all other exits must use the slow path. + */ + if (is_guest_mode(vcpu) && + to_vmx(vcpu)->exit_reason.basic !=3D EXIT_REASON_PREEMPTION_TIMER) return EXIT_FASTPATH_NONE; =20 switch (to_vmx(vcpu)->exit_reason.basic) { --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 E4BAA1FF5EC for ; Fri, 15 Aug 2025 00:12:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216760; cv=none; b=tc+/gQEIF10L+z6zQGM9PjhZwhj20Jz2LL8mbll8c19Yb7ZepJOW3iP0bDZrF7qTe/osLR97sHt832NKHkptLzq13OBnWDwifRXUbgc2ZPVke/m+10/AoTt/0WxnZzNyHIXRYkEZt15YvAJMVFwQz1WD/5jwB/1pSBzBCo69B5E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216760; c=relaxed/simple; bh=yTUz+qG+cU6V8xwnekCabgy0vngZnFqvIXMxd1Gydqk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=g4HNATk8X16v6TiWdCxJdXNWMn3Sd6gtdpAxECQGyFpv41eCBd+1NI6hoJG3qlgXxdpEWOKbkDeDpJbTEXnRK0z4xHCwB+Mi5FLmf5rCOojcjGHDOGN6qRBtsddxJ0y6+N6K/pDr9cdbACRAy/Kv+0CVDtVgIUU0fniHRjlfLoo= 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=u7zuo4Ts; arc=none smtp.client-ip=209.85.210.201 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="u7zuo4Ts" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-76e2eb787f2so1437561b3a.3 for ; Thu, 14 Aug 2025 17:12:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216757; x=1755821557; 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=jPuQ+cNG6Ky3AiODPwKfwN4pIduvIz2auWyqIQ/IVDc=; b=u7zuo4Ts9CRAFLBGTYOHv+T9LI33EIUalKWvC7MFMTQtH76MLOegfvJItkV0z2qWzA DjFtunlcMmjZZm07/5wO6KnrrfmAkFAJ8KKhqE4x7mwLpBsJaZPnWW330bvfeb+WAAEf fLb+QlyEA4xFla/xz7yhVniEvSpO2vr5TCyhJud9taGAESPDKJG8MPGC6F16DCi6Fuba 9WSc8TWPkj+mEjom9Y0yogJgGC6Ndiw5iK9wp97iUVKs6Ipc2lAf2hltpi4pq9LQRAyB a5qNdB/GnCpEbveOdphpPr32IyF74aKr+U4HUIS/o+yNQugY5EzLPQdAfTjofPZkpQSE VQLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216757; x=1755821557; 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=jPuQ+cNG6Ky3AiODPwKfwN4pIduvIz2auWyqIQ/IVDc=; b=VRKH0ORkUcrAS8Ppofp3IFqrtZVChMoXRcX1EqYlaFGB/4NUZUnvK0zdOuJe5/Aqdw OB/uzxPX1Mba0+/UnPol9Nk112doovH6o0Tu2z3QCmzD9e4QX2m1sLmWAqRFGISBIqz/ xPmvZZPusflc0304iFgsX4ftOB+/Beh+XwKkKLiju2aAeAwo4c40uyt+s6Zfyt7/+EHK Tk0sw1WksAxNLuTuZIEp8L/EEhD1wYQiQr4dLf814NqkoygRCF2STtSrNNuTqzV96KrF DSdOApp0mcbxEOmP9SCRTif1XgWdqTnAD9OE/IsHLT9sWIP7Q5DZq+AxU+XJMLWwBAcM cfjA== X-Forwarded-Encrypted: i=1; AJvYcCWwtpI8FdzReIuyk5N9KRSZdMDY7CXdpRgJuKL2LInDAfVuP4wD+HWf/FEUD0nyBPigROd7hyJHDp09FAU=@vger.kernel.org X-Gm-Message-State: AOJu0YzPVUCygTu19Ig4VzRBvkNJzaJq/Op2dZebFFTnAAEtb6gqIMmh gsLnEd+JS1R1Ugr9uCufupWUUgCrGy+3iTOTWCVWKabfdRuBGuwByHJWAleILrRExxHj2S1ocjx JTCwsdw== X-Google-Smtp-Source: AGHT+IHJ7XeWcEMUSG1jDAsSbG5DOzoVCeXpHBp7b8baQtDYYKASKeMj0hM/4Tl94qbNbHjH0SF36wjcd4I= X-Received: from pfbik4.prod.google.com ([2002:a05:6a00:8d04:b0:76b:d1ee:d06c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1492:b0:74d:3a57:81d9 with SMTP id d2e1a72fcca58-76e446de7a4mr75086b3a.8.1755216756957; Thu, 14 Aug 2025 17:12:36 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:11:58 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-15-seanjc@google.com> Subject: [PATCH 6.1.y 14/21] KVM: x86: Fully defer to vendor code to decide how to force immediate exit From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit 0ec3d6d1f169baa7fc512ae4b78d17e7c94b7763 ] Now that vmx->req_immediate_exit is used only in the scope of vmx_vcpu_run(), use force_immediate_exit to detect that KVM should usurp the VMX preemption to force a VM-Exit and let vendor code fully handle forcing a VM-Exit. Opportunsitically drop __kvm_request_immediate_exit() and just have vendor code call smp_send_reschedule() directly. SVM already does this when injecting an event while also trying to single-step an IRET, i.e. it's not exactly secret knowledge that KVM uses a reschedule IPI to force an exit. Link: https://lore.kernel.org/r/20240110012705.506918-7-seanjc@google.com Signed-off-by: Sean Christopherson [sean: resolve absurd conflict due to funky kvm_x86_ops.sched_in prototype] Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm-x86-ops.h | 1 - arch/x86/include/asm/kvm_host.h | 3 --- arch/x86/kvm/svm/svm.c | 7 ++++--- arch/x86/kvm/vmx/vmx.c | 32 +++++++++++++----------------- arch/x86/kvm/vmx/vmx.h | 2 -- arch/x86/kvm/x86.c | 10 +--------- 6 files changed, 19 insertions(+), 36 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-= x86-ops.h index 29bef25ac77c..0e5ae3b0c867 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -100,7 +100,6 @@ KVM_X86_OP(write_tsc_multiplier) KVM_X86_OP(get_exit_info) KVM_X86_OP(check_intercept) KVM_X86_OP(handle_exit_irqoff) -KVM_X86_OP(request_immediate_exit) KVM_X86_OP(sched_in) KVM_X86_OP_OPTIONAL(update_cpu_dirty_logging) KVM_X86_OP_OPTIONAL(vcpu_blocking) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 93f523762854..86f3bd6601e7 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1590,8 +1590,6 @@ struct kvm_x86_ops { struct x86_exception *exception); void (*handle_exit_irqoff)(struct kvm_vcpu *vcpu); =20 - void (*request_immediate_exit)(struct kvm_vcpu *vcpu); - void (*sched_in)(struct kvm_vcpu *kvm, int cpu); =20 /* @@ -2059,7 +2057,6 @@ extern bool kvm_find_async_pf_gfn(struct kvm_vcpu *vc= pu, gfn_t gfn); =20 int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu); int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err); -void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu); =20 void __user *__x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 337a304d211b..12de50db401f 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4033,8 +4033,11 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm= _vcpu *vcpu, * is enough to force an immediate vmexit. */ disable_nmi_singlestep(svm); + force_immediate_exit =3D true; + } + + if (force_immediate_exit) smp_send_reschedule(vcpu->cpu); - } =20 pre_svm_run(vcpu); =20 @@ -4874,8 +4877,6 @@ static struct kvm_x86_ops svm_x86_ops __initdata =3D { .check_intercept =3D svm_check_intercept, .handle_exit_irqoff =3D svm_handle_exit_irqoff, =20 - .request_immediate_exit =3D __kvm_request_immediate_exit, - .sched_in =3D svm_sched_in, =20 .nested_ops =3D &svm_nested_ops, diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 4db9d41d988c..179747d04edc 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -49,6 +49,8 @@ #include #include =20 +#include + #include "capabilities.h" #include "cpuid.h" #include "evmcs.h" @@ -1223,8 +1225,6 @@ void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcp= u) u16 fs_sel, gs_sel; int i; =20 - vmx->req_immediate_exit =3D false; - /* * Note that guest MSRs to be saved/restored can also be changed * when guest state is loaded. This happens when guest transitions @@ -5929,7 +5929,8 @@ static int handle_pml_full(struct kvm_vcpu *vcpu) return 1; } =20 -static fastpath_t handle_fastpath_preemption_timer(struct kvm_vcpu *vcpu) +static fastpath_t handle_fastpath_preemption_timer(struct kvm_vcpu *vcpu, + bool force_immediate_exit) { struct vcpu_vmx *vmx =3D to_vmx(vcpu); =20 @@ -5945,7 +5946,7 @@ static fastpath_t handle_fastpath_preemption_timer(st= ruct kvm_vcpu *vcpu) * If the timer expired because KVM used it to force an immediate exit, * then mission accomplished. */ - if (vmx->req_immediate_exit) + if (force_immediate_exit) return EXIT_FASTPATH_EXIT_HANDLED; =20 /* @@ -7090,13 +7091,13 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx= *vmx) msrs[i].host, false); } =20 -static void vmx_update_hv_timer(struct kvm_vcpu *vcpu) +static void vmx_update_hv_timer(struct kvm_vcpu *vcpu, bool force_immediat= e_exit) { struct vcpu_vmx *vmx =3D to_vmx(vcpu); u64 tscl; u32 delta_tsc; =20 - if (vmx->req_immediate_exit) { + if (force_immediate_exit) { vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, 0); vmx->loaded_vmcs->hv_timer_soft_disabled =3D false; } else if (vmx->hv_deadline_tsc !=3D -1) { @@ -7149,7 +7150,8 @@ void noinstr vmx_spec_ctrl_restore_host(struct vcpu_v= mx *vmx, barrier_nospec(); } =20 -static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu) +static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu, + bool force_immediate_exit) { /* * If L2 is active, some VMX preemption timer exits can be handled in @@ -7163,7 +7165,7 @@ static fastpath_t vmx_exit_handlers_fastpath(struct k= vm_vcpu *vcpu) case EXIT_REASON_MSR_WRITE: return handle_fastpath_set_msr_irqoff(vcpu); case EXIT_REASON_PREEMPTION_TIMER: - return handle_fastpath_preemption_timer(vcpu); + return handle_fastpath_preemption_timer(vcpu, force_immediate_exit); default: return EXIT_FASTPATH_NONE; } @@ -7284,7 +7286,9 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu,= bool force_immediate_exit) vmx_passthrough_lbr_msrs(vcpu); =20 if (enable_preemption_timer) - vmx_update_hv_timer(vcpu); + vmx_update_hv_timer(vcpu, force_immediate_exit); + else if (force_immediate_exit) + smp_send_reschedule(vcpu->cpu); =20 kvm_wait_lapic_expire(vcpu); =20 @@ -7358,7 +7362,7 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu,= bool force_immediate_exit) vmx_recover_nmi_blocking(vmx); vmx_complete_interrupts(vmx); =20 - return vmx_exit_handlers_fastpath(vcpu); + return vmx_exit_handlers_fastpath(vcpu, force_immediate_exit); } =20 static void vmx_vcpu_free(struct kvm_vcpu *vcpu) @@ -7865,11 +7869,6 @@ static __init void vmx_set_cpu_caps(void) kvm_cpu_cap_check_and_set(X86_FEATURE_WAITPKG); } =20 -static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu) -{ - to_vmx(vcpu)->req_immediate_exit =3D true; -} - static int vmx_check_intercept_io(struct kvm_vcpu *vcpu, struct x86_instruction_info *info) { @@ -8275,8 +8274,6 @@ static struct kvm_x86_ops vmx_x86_ops __initdata =3D { .check_intercept =3D vmx_check_intercept, .handle_exit_irqoff =3D vmx_handle_exit_irqoff, =20 - .request_immediate_exit =3D vmx_request_immediate_exit, - .sched_in =3D vmx_sched_in, =20 .cpu_dirty_log_size =3D PML_ENTITY_NUM, @@ -8533,7 +8530,6 @@ static __init int hardware_setup(void) if (!enable_preemption_timer) { vmx_x86_ops.set_hv_timer =3D NULL; vmx_x86_ops.cancel_hv_timer =3D NULL; - vmx_x86_ops.request_immediate_exit =3D __kvm_request_immediate_exit; } =20 kvm_caps.supported_mce_cap |=3D MCG_LMCE_P; diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 357819872d80..ddbe73958d7f 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -343,8 +343,6 @@ struct vcpu_vmx { unsigned int ple_window; bool ple_window_dirty; =20 - bool req_immediate_exit; - /* Support for PML */ #define PML_ENTITY_NUM 512 struct page *pml_pg; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 652f90ad7107..bc586a6df6ab 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10578,12 +10578,6 @@ static void kvm_vcpu_reload_apic_access_page(struc= t kvm_vcpu *vcpu) static_call_cond(kvm_x86_set_apic_access_page_addr)(vcpu); } =20 -void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu) -{ - smp_send_reschedule(vcpu->cpu); -} -EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit); - /* * Called within kvm->srcu read side. * Returns 1 to let vcpu_run() continue the guest execution loop without @@ -10817,10 +10811,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) goto cancel_injection; } =20 - if (req_immediate_exit) { + if (req_immediate_exit) kvm_make_request(KVM_REQ_EVENT, vcpu); - static_call(kvm_x86_request_immediate_exit)(vcpu); - } =20 fpregs_assert_state_consistent(); if (test_thread_flag(TIF_NEED_FPU_LOAD)) --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 90DE4214807 for ; Fri, 15 Aug 2025 00:12:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216761; cv=none; b=T6G/0WSTq/RhvGgUJrRlp/D7MJyFdrn29Q4hvq6mwWaxgjZDChzia05iA5D/vn2bLovluZPMsMJa6jbQJu8yTlxWJPIkGGcqxjAFpExNVtoK2o8LvO6sPoTmW0uRuBJ3Fv36XiNRwsdAAi0JMgqXfkV2vbdiaemi2hZZ0mldQWo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216761; c=relaxed/simple; bh=RZ7Dz8xwBsHGy5mh7rqSjgoKEOyTvpiJll9GacNdIxQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RY3WSgxv+JQLN+cinYA1QRvP5b7w2uPBbX3GiVyPVlMFeN90bqD1mI8J4nBhc8+v3bnFsZXBsuWc32ZX8Xcu10nDecNU04g93TleMjxz50CBBMHXtfUw2WeHrk5XuOvMB+BVSCBt3erxkTJrAGh6onYMOTMaJIToE6GR2vnsK9E= 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=sYLQ5sqP; arc=none smtp.client-ip=209.85.215.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="sYLQ5sqP" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b4716fc56a9so1752720a12.0 for ; Thu, 14 Aug 2025 17:12:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216759; x=1755821559; 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=5WU9voBHmtHCID+62ekjip+WsDaLPoXQ1Ho4KAjz+Bk=; b=sYLQ5sqP+joFgShRs229ltnfmFKtmVeGfB3+jobeQCppC5tinDwcFsakJuE7PWJD3n RCeGR2mhTHOLudMle49N2/iSBI+zo1uDLnobkkri/XPCm9PbmoHcXCRqVjaXl5Qlvot4 1YFwDbnobQPgw2RBW7qVG/mTcMaHToJ17ByTiYtamMmmkAyYUXi6wganyRUvLdNpuqSR mynmI/SGoZEdteBvLpBWSrl7x9M39eUcdefHVyzrvCdj/J85uhNZlG9DChjhAKJJShFo bVUZth8UMurwozCNBm0TRJnOxNRkLBlqeXdZupDpawqnW0lXdZaNP0ilI9s6LNIlcqln TJag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216759; x=1755821559; 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=5WU9voBHmtHCID+62ekjip+WsDaLPoXQ1Ho4KAjz+Bk=; b=vqkbYDJQnvwaKMLdNoUUF56AecAyH/Xujm4duAVuzaPnSQT1AMDcyD2JfM7RIf6ZHQ qO0ZGZ6BUF7nF1I3wbiaHkAEm+8Qvka9TZWOe9AihtVk/bo0wTAdUpXBcdI2f3/owQDe v3n+AJUm8bb8/k+5httYnmIWfzCGN7spofe1zT6GojteTDRj5zQ7WskmdHV/mgbAIMU7 xGq7YZ40/2phj2hmTGYlr0331/59blZ7YbKbFRrkKc/lr8ZLb/XwDezMnzAzpB20tTkG pc7J9P/UwjZ2YwngBpZpKkq0Ie+oFINh8LeRlNEhmNf0pDW4/zr60ZGe0EBNzzSRdgvI KQbA== X-Forwarded-Encrypted: i=1; AJvYcCXeaBHl6q6aatKo9pfPtZgxPB25T66nyT1sfsuPeMRoBaoTUEJjX/E8qBoc2/3b/G2+nTspNmVmgijTp/g=@vger.kernel.org X-Gm-Message-State: AOJu0Yz1UCRB9nuSfHnkn3up9kBQZbUJuBJ8sCTfGDvQ7jmDtEGn6Ve4 uA3s1e6jitzd4lwlyuWra3EE5ULO6Jd4H0P+1ogI5qiZ3xBBIka6c9T3t0Dt6xxDMWhunngOx3R VHVrzKQ== X-Google-Smtp-Source: AGHT+IHLp96Otjk1T4s0hj1MiompkYvxUqUTRUDczNOqtCXM0tJwB6ezd2X3zMPFXAvmZWvj66nHpW6DhT8= X-Received: from pgc14.prod.google.com ([2002:a05:6a02:2f8e:b0:b42:1052:52f5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:430d:b0:238:351a:6437 with SMTP id adf61e73a8af0-240d2f3f71bmr291638637.43.1755216758682; Thu, 14 Aug 2025 17:12:38 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:11:59 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-16-seanjc@google.com> Subject: [PATCH 6.1.y 15/21] KVM: x86: Convert vcpu_run()'s immediate exit param into a generic bitmap From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit 2478b1b220c49d25cb1c3f061ec4f9b351d9a131 ] Convert kvm_x86_ops.vcpu_run()'s "force_immediate_exit" boolean parameter into an a generic bitmap so that similar "take action" information can be passed to vendor code without creating a pile of boolean parameters. This will allow dropping kvm_x86_ops.set_dr6() in favor of a new flag, and will also allow for adding similar functionality for re-loading debugctl in the active VMCS. Opportunistically massage the TDX WARN and comment to prepare for adding more run_flags, all of which are expected to be mutually exclusive with TDX, i.e. should be WARNed on. No functional change intended. Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20250610232010.162191-3-seanjc@google.com Signed-off-by: Sean Christopherson [sean: drop TDX crud, account for lack of kvm_x86_call()] Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 6 +++++- arch/x86/kvm/svm/svm.c | 4 ++-- arch/x86/kvm/vmx/vmx.c | 3 ++- arch/x86/kvm/x86.c | 10 ++++++++-- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 86f3bd6601e7..1383f5e5238a 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1456,6 +1456,10 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_= mode_logical) return dest_mode_logical ? APIC_DEST_LOGICAL : APIC_DEST_PHYSICAL; } =20 +enum kvm_x86_run_flags { + KVM_RUN_FORCE_IMMEDIATE_EXIT =3D BIT(0), +}; + struct kvm_x86_ops { const char *name; =20 @@ -1529,7 +1533,7 @@ struct kvm_x86_ops { =20 int (*vcpu_pre_run)(struct kvm_vcpu *vcpu); enum exit_fastpath_completion (*vcpu_run)(struct kvm_vcpu *vcpu, - bool force_immediate_exit); + u64 run_flags); int (*handle_exit)(struct kvm_vcpu *vcpu, enum exit_fastpath_completion exit_fastpath); int (*skip_emulated_instruction)(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 12de50db401f..dc8a1b72d8ec 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4008,9 +4008,9 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vc= pu *vcpu, bool spec_ctrl_in guest_state_exit_irqoff(); } =20 -static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, - bool force_immediate_exit) +static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_f= lags) { + bool force_immediate_exit =3D run_flags & KVM_RUN_FORCE_IMMEDIATE_EXIT; struct vcpu_svm *svm =3D to_svm(vcpu); bool spec_ctrl_intercepted =3D msr_write_intercepted(vcpu, MSR_IA32_SPEC_= CTRL); =20 diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 179747d04edc..382f42200688 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7204,8 +7204,9 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vc= pu *vcpu, guest_state_exit_irqoff(); } =20 -static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, bool force_immediate= _exit) +static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags) { + bool force_immediate_exit =3D run_flags & KVM_RUN_FORCE_IMMEDIATE_EXIT; struct vcpu_vmx *vmx =3D to_vmx(vcpu); unsigned long cr3, cr4; =20 diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index bc586a6df6ab..b9cc3c05590e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10591,6 +10591,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) dm_request_for_irq_injection(vcpu) && kvm_cpu_accept_dm_intr(vcpu); fastpath_t exit_fastpath; + u64 run_flags; =20 bool req_immediate_exit =3D false; =20 @@ -10811,8 +10812,11 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) goto cancel_injection; } =20 - if (req_immediate_exit) + run_flags =3D 0; + if (req_immediate_exit) { + run_flags |=3D KVM_RUN_FORCE_IMMEDIATE_EXIT; kvm_make_request(KVM_REQ_EVENT, vcpu); + } =20 fpregs_assert_state_consistent(); if (test_thread_flag(TIF_NEED_FPU_LOAD)) @@ -10848,7 +10852,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) WARN_ON_ONCE((kvm_vcpu_apicv_activated(vcpu) !=3D kvm_vcpu_apicv_active(= vcpu)) && (kvm_get_apic_mode(vcpu) !=3D LAPIC_MODE_DISABLED)); =20 - exit_fastpath =3D static_call(kvm_x86_vcpu_run)(vcpu, req_immediate_exit= ); + exit_fastpath =3D static_call(kvm_x86_vcpu_run)(vcpu, run_flags); if (likely(exit_fastpath !=3D EXIT_FASTPATH_REENTER_GUEST)) break; =20 @@ -10860,6 +10864,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) break; } =20 + run_flags =3D 0; + /* Note, VM-Exits that go down the "slow" path are accounted below. */ ++vcpu->stat.exits; } --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 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 6ED2A21859A for ; Fri, 15 Aug 2025 00:12:41 +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=1755216763; cv=none; b=RtEP6nDPRk5THg/9GeiqL0CyMdnGtIwo259I9yCWOc+oRfDa430zW6CzZZGgFYwBw9zIrPn4SPpVlkxmyWOn4jeLGafl9DujbVTb+MMGMMsd0sKu6OaRhFsB6el+z7E28T4kzFBmzIWzG8J5hqoncBT1kOMGdGI/ZfsngSQrEzI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216763; c=relaxed/simple; bh=IJjich3kREVhBiDmmkayA+cGB2r0yDEDCHV8gx8rv4o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Fk1XPYBzzkL87Iv6TRpFfChVAGXZpTaWisuwyhroJ8EVAuuYbuRKQAxQbfNbR/4FBBeJRVoIU/88kCCRKsPCmguMPVFjn/AAV1jzx3qM4jFTzT8ZhOxEPBdniejdqLiiCdS9VWlKQXa+R2ZkVNwNbXFa3k3zT8CN2LOr0R+lGf0= 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=J6pPKiOe; 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="J6pPKiOe" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-323267915ebso2865274a91.1 for ; Thu, 14 Aug 2025 17:12:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216761; x=1755821561; 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=5G4NXIFbMtOfdxRkfDv9xmi14IdY9gfOWQtzLsQpLqY=; b=J6pPKiOeggCL7VuY8yWlXa0mJudn+MlDY3pWiHDSgAKkthjmK35ejq96IKwBw5Lmbm d6J+7A/8NKLeXf1qdCTwbvD9q/xgJHqGGy3uNZLyYYoPUg3IeuPbThhm/Dt7lGElmM1s Q9iQA2lU8fLUv6JCZGBW8zcl0SzTa3bAEHT0jP9QMoViXCKj+dPp4XGn5vmd+FxbQLqr 3GBsnvw6IZrw1N0iHmvBPigWEn54pOKu2h2TBqbn8Rds1ZlEiIp7IA0dUAc4+q4/NqBI 1gNLyr/boHl9P1EUHD29f+oH9OiS09pLoD9o6BnfihSFV9+nj0noIFdI2lzqszwk0V1A iF2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216761; x=1755821561; 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=5G4NXIFbMtOfdxRkfDv9xmi14IdY9gfOWQtzLsQpLqY=; b=PORHK08trzxIg5+MKoX2Qwta2I8TV6YpySKBOwcZKrU8FH3SS8qTvud1tAMqNekKKO aZEO2NJ7CheOD3sKkS6WuHmplUawxZs/ZVZFwH+7j0UQip38wLhF/ZnNthHkkqm1+Y+K Q1aWqaKCLZKpD1unP1uxi9phsIvDrrb8ezpg0aKRX3iHuKVbmGwMkUZpQlwv6hdHHtl9 S1+ytPLv3DA6LkEE91zCrzAzlBtsPZTc16ksXP3l7X5u42NkzPjp3sshHbPYPAH5ugZA //THEatzXIrLUhOXANO7huCF8sUpKR9+68Dg3XcAqHg+RoFcPnlkTgWcCih43v5YPml9 UeiQ== X-Forwarded-Encrypted: i=1; AJvYcCWEay0kjBJ3CHLvEc67VVLqvkHv3aD/NLd+dw6HUW/T9ObHkh1wA9gIQaVIk8PKygZYsvTawsEl1xWejF0=@vger.kernel.org X-Gm-Message-State: AOJu0Yxi2hjcdvUBB47AFVRGlpeo1LorkUzODFXHrLsxHS/ucPZRALSF cgMNOedu5uoeQV89gu6vMw3OK5kdsgoLKkNNlrcfdeQQGXr14qiMEOJ2vqQpo7rLKXMIRn9YIYW v++atyw== X-Google-Smtp-Source: AGHT+IHFLzHsufIdYLhmtILOmyyKJEN6iKyKAqEtk3zibf6S7DBDVdQO5N3DQ51R7vHwonzWkjCop2nYinA= X-Received: from pjuj12.prod.google.com ([2002:a17:90a:d00c:b0:314:29b4:453]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1d05:b0:31e:f3b7:49d2 with SMTP id 98e67ed59e1d1-32340f9db40mr443366a91.0.1755216760574; Thu, 14 Aug 2025 17:12:40 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:12:00 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-17-seanjc@google.com> Subject: [PATCH 6.1.y 16/21] KVM: x86: Drop kvm_x86_ops.set_dr6() in favor of a new KVM_RUN flag From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit 80c64c7afea1da6a93ebe88d3d29d8a60377ef80 ] Instruct vendor code to load the guest's DR6 into hardware via a new KVM_RUN flag, and remove kvm_x86_ops.set_dr6(), whose sole purpose was to load vcpu->arch.dr6 into hardware when DR6 can be read/written directly by the guest. Note, TDX already WARNs on any run_flag being set, i.e. will yell if KVM thinks DR6 needs to be reloaded. TDX vCPUs force KVM_DEBUGREG_AUTO_SWITCH and never clear the flag, i.e. should never observe KVM_RUN_LOAD_GUEST_DR6. Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20250610232010.162191-4-seanjc@google.com Signed-off-by: Sean Christopherson [sean: account for lack of vmx/main.c] Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm-x86-ops.h | 1 - arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/svm/svm.c | 10 ++++++---- arch/x86/kvm/vmx/vmx.c | 10 +++------- arch/x86/kvm/x86.c | 2 +- 5 files changed, 11 insertions(+), 14 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-= x86-ops.h index 0e5ae3b0c867..c068565fe954 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -47,7 +47,6 @@ KVM_X86_OP(set_idt) KVM_X86_OP(get_gdt) KVM_X86_OP(set_gdt) KVM_X86_OP(sync_dirty_debug_regs) -KVM_X86_OP(set_dr6) KVM_X86_OP(set_dr7) KVM_X86_OP(cache_reg) KVM_X86_OP(get_rflags) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 1383f5e5238a..c8fc4f2acf69 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1458,6 +1458,7 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_m= ode_logical) =20 enum kvm_x86_run_flags { KVM_RUN_FORCE_IMMEDIATE_EXIT =3D BIT(0), + KVM_RUN_LOAD_GUEST_DR6 =3D BIT(1), }; =20 struct kvm_x86_ops { @@ -1504,7 +1505,6 @@ struct kvm_x86_ops { void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt); void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt); void (*sync_dirty_debug_regs)(struct kvm_vcpu *vcpu); - void (*set_dr6)(struct kvm_vcpu *vcpu, unsigned long value); void (*set_dr7)(struct kvm_vcpu *vcpu, unsigned long value); void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg); unsigned long (*get_rflags)(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index dc8a1b72d8ec..5a6bd9d5cceb 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4052,10 +4052,13 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kv= m_vcpu *vcpu, u64 run_flags) svm_hv_update_vp_id(svm->vmcb, vcpu); =20 /* - * Run with all-zero DR6 unless needed, so that we can get the exact cause - * of a #DB. + * Run with all-zero DR6 unless the guest can write DR6 freely, so that + * KVM can get the exact cause of a #DB. Note, loading guest DR6 from + * KVM's snapshot is only necessary when DR accesses won't exit. */ - if (likely(!(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))) + if (unlikely(run_flags & KVM_RUN_LOAD_GUEST_DR6)) + svm_set_dr6(vcpu, vcpu->arch.dr6); + else if (likely(!(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))) svm_set_dr6(vcpu, DR6_ACTIVE_LOW); =20 clgi(); @@ -4822,7 +4825,6 @@ static struct kvm_x86_ops svm_x86_ops __initdata =3D { .set_idt =3D svm_set_idt, .get_gdt =3D svm_get_gdt, .set_gdt =3D svm_set_gdt, - .set_dr6 =3D svm_set_dr6, .set_dr7 =3D svm_set_dr7, .sync_dirty_debug_regs =3D svm_sync_dirty_debug_regs, .cache_reg =3D svm_cache_reg, diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 382f42200688..60d1ff3fca45 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5530,12 +5530,6 @@ static void vmx_sync_dirty_debug_regs(struct kvm_vcp= u *vcpu) set_debugreg(DR6_RESERVED, 6); } =20 -static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) -{ - lockdep_assert_irqs_disabled(); - set_debugreg(vcpu->arch.dr6, 6); -} - static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) { vmcs_writel(GUEST_DR7, val); @@ -7251,6 +7245,9 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu,= u64 run_flags) vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); vcpu->arch.regs_dirty =3D 0; =20 + if (run_flags & KVM_RUN_LOAD_GUEST_DR6) + set_debugreg(vcpu->arch.dr6, 6); + /* * Refresh vmcs.HOST_CR3 if necessary. This must be done immediately * prior to VM-Enter, as the kernel may load a new ASID (PCID) any time @@ -8208,7 +8205,6 @@ static struct kvm_x86_ops vmx_x86_ops __initdata =3D { .set_idt =3D vmx_set_idt, .get_gdt =3D vmx_get_gdt, .set_gdt =3D vmx_set_gdt, - .set_dr6 =3D vmx_set_dr6, .set_dr7 =3D vmx_set_dr7, .sync_dirty_debug_regs =3D vmx_sync_dirty_debug_regs, .cache_reg =3D vmx_cache_reg, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b9cc3c05590e..27e7253972ea 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10833,7 +10833,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) set_debugreg(vcpu->arch.eff_db[3], 3); /* When KVM_DEBUGREG_WONT_EXIT, dr6 is accessible in guest. */ if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) - static_call(kvm_x86_set_dr6)(vcpu, vcpu->arch.dr6); + run_flags |=3D KVM_RUN_LOAD_GUEST_DR6; } else if (unlikely(hw_breakpoint_active())) { set_debugreg(0, 7); } --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 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 3B23721ABD5 for ; Fri, 15 Aug 2025 00:12:43 +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=1755216765; cv=none; b=jWqBd9VAj2xWtvPUfFNjmeSRsB80ue+TWhnc6T1Ti/vd9XqRfxhahevA1wVMJZ6kr/jxSsefI8FtkfdqaPM4C61PvEjLkeXT7H6Eti6EFweAH3Wr8d+dzRy3Hg9lodCXnPwoVAUUjJTte5TGl+tDdhUt+Yc4nP+NGt1MzvyKlKo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216765; c=relaxed/simple; bh=ZNUgVI46E0OBePkY8rZgCoXXPCeoOFlmMiH7qcC5XD0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XnGOjOVQayan+0N/UO+amurR5ZBTFcpkA2/pkWZ39+gJ+odzMQpEFCuhhb15iW3jWeiaGtz0nzN6QrXcUbEeIc32jJLqrnGNr6PlsSy7zqDpDBIjwSuNXoKbAk4mdUxuaGMGdnhXjQIwhELcfYrR/3ryQ1Zy1mEJxrgEqc6a+2o= 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=g1OB//JM; 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="g1OB//JM" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-321cf7549afso2074721a91.0 for ; Thu, 14 Aug 2025 17:12:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216762; x=1755821562; 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=R2pN41pL9/2mODdo8yvh5+ZzvaLNu8BCBG5lgJlQkBA=; b=g1OB//JMiHaGKTPF/ppT7jhcgprpcJoJmhppeO0G8IsgIGFbuunWILVAnhzjUViZHH kQVj0vDOjI9mT/ZCgWdY2qQqrylIuUdpd5Wfh7lQKwbg999IjXpv/irS5alEtutfTIC3 MhK2Ln597eRuN5MTvwayIXIZ4hB6sUmYE/ms8HTaPGhggxouJBhAjgyQSOE5wuogFFHY Em8/xdKNKk20eFgmnkvnmA/5JMD475Jb7Qpnw66s84IAjVCfn3ugrXexi0QJxybl+GcZ PpWYDPupUeFe4ObHQPSqQatFCkGKeqk9j9ij3ehG/tlZe6gW7KeTOOazz2EibcyL8TDI vLcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216762; x=1755821562; 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=R2pN41pL9/2mODdo8yvh5+ZzvaLNu8BCBG5lgJlQkBA=; b=b+sO3PoBLfyIhM6qWq6ACJyXrn6/P7N8zI/oABgkBwnUBbYM2nVQzjOge825hhtSxa slZJh31d1c31UuWVRUknGz6tY93GTUDbnhP8dSk/0m3nvQ1KKB3dsCMqOX8koPTh4YvF Zpg4GPdZFXuDClVYJDB0LUYYyntsSWsIBsWn2lqan4Zgzy1cEu8VmN0SurVuzYVEEUNQ 1Y4SAcRN8mEKunqWA0944U0KEUhuPI+SD4sohd7Nc4B0mqB87stdPlwCcaE9M2AyAaqw z9LmrsFz07vc4/2D/Z9AT+jJMCg+lmmMcdDVDIYzsAn16j03ViGsHJcsANrEkA/kWzs9 qDQw== X-Forwarded-Encrypted: i=1; AJvYcCXFvhsAcYtup1FEmgtNEwI55uwgBHnHWuH6N86xdyBPWpLRK4m1X5A2wBUyCfiuLyioQ4csMC/9VthEQVA=@vger.kernel.org X-Gm-Message-State: AOJu0YycBA02zDiEZKAdQJUXaGFQZDiaHbj+2+6f7tm1xMS/CW4KPpJa j4hFcUTO5ee+OnDRGenOQCRRfiEB6a3qgqcfA5e18rLjMFeitg6AXSVsf59nOrvZyT6KxAfQKR+ gM5fCrA== X-Google-Smtp-Source: AGHT+IED/dB3BvLeWzmXrmh6dopVdFx3W31DTAGoQRFRaiqxSY4V+FoKnuxqrBvFDmZBctuTt5QJjgSBCfE= X-Received: from pjbsd8.prod.google.com ([2002:a17:90b:5148:b0:321:c9cf:de39]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2681:b0:321:c9cf:deaa with SMTP id 98e67ed59e1d1-323297bdeb6mr7052059a91.17.1755216762510; Thu, 14 Aug 2025 17:12:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:12:01 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-18-seanjc@google.com> Subject: [PATCH 6.1.y 17/21] KVM: VMX: Allow guest to set DEBUGCTL.RTM_DEBUG if RTM is supported From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit 17ec2f965344ee3fd6620bef7ef68792f4ac3af0 ] Let the guest set DEBUGCTL.RTM_DEBUG if RTM is supported according to the guest CPUID model, as debug support is supposed to be available if RTM is supported, and there are no known downsides to letting the guest debug RTM aborts. Note, there are no known bug reports related to RTM_DEBUG, the primary motivation is to reduce the probability of breaking existing guests when a future change adds a missing consistency check on vmcs12.GUEST_DEBUGCTL (KVM currently lets L2 run with whatever hardware supports; whoops). Note #2, KVM already emulates DR6.RTM, and doesn't restrict access to DR7.RTM. Fixes: 83c529151ab0 ("KVM: x86: expose Intel cpu new features (HLE, RTM) to= guest") Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20250610232010.162191-5-seanjc@google.com Signed-off-by: Sasha Levin Signed-off-by: Sean Christopherson --- arch/x86/include/asm/msr-index.h | 1 + arch/x86/kvm/vmx/vmx.c | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-in= dex.h index 727947ed5e5e..afd65c815043 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -379,6 +379,7 @@ #define DEBUGCTLMSR_FREEZE_PERFMON_ON_PMI (1UL << 12) #define DEBUGCTLMSR_FREEZE_IN_SMM_BIT 14 #define DEBUGCTLMSR_FREEZE_IN_SMM (1UL << DEBUGCTLMSR_FREEZE_IN_SMM_BIT) +#define DEBUGCTLMSR_RTM_DEBUG BIT(15) =20 #define MSR_PEBS_FRONTEND 0x000003f7 =20 diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 60d1ff3fca45..9445def2b3d2 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2064,6 +2064,10 @@ static u64 vmx_get_supported_debugctl(struct kvm_vcp= u *vcpu, bool host_initiated (host_initiated || intel_pmu_lbr_is_enabled(vcpu))) debugctl |=3D DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI; =20 + if (boot_cpu_has(X86_FEATURE_RTM) && + (host_initiated || guest_cpuid_has(vcpu, X86_FEATURE_RTM))) + debugctl |=3D DEBUGCTLMSR_RTM_DEBUG; + return debugctl; } =20 --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 D4E2C21C161 for ; Fri, 15 Aug 2025 00:12:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216767; cv=none; b=lJCd14w7N3MJ8zVnpstjbDe94E0iC9TuyFqpf2j8WyRngAmJnDvAuPRbkoPaqKJIl9sKHM+jaBt0iSeSRPgRGnD86cUDOVHbaGoDg3dIP67Ro+wIFTfWDYzpI0bqOk6ZmTFqGzluczlbd+PTQn+Vg5v1iPWatJBeIVCogGm0+Dw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216767; c=relaxed/simple; bh=d4VZkPw4RAyHIWRZJDzABU/cEq7qkdsgmjzT5CLiyGI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kCunBlAP1YDj32mQ5b6BvQMFl7mhjg8alz6onqNw0ZeMsOXE8wXw199rvQU8ABqqsPzK+M06xsaTkanz24Az7H4z2NEIQw0Jt8k4ctP5uzpJyQbHnip71gbG/ZY37UoPCJhR7PXPCSLrloYqSL6C2/TJp82NLVle1T8bMgYwvz0= 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=V5GnQYN5; arc=none smtp.client-ip=209.85.215.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="V5GnQYN5" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b471758845dso1034603a12.3 for ; Thu, 14 Aug 2025 17:12:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216764; x=1755821564; 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=eQfmi+GnDIrdp8ejQz7KjH8qWzqOpQFlk46cwdVc498=; b=V5GnQYN53x+sZZPDcYocLWwx8Jaa9cxT8zEbXf+PiDqEAAJuHOetTvS5Yb3PO03l4+ gsULL0/Tv2uQ5eAYgDQ1e9oyWboJlb3kYM9dBBxiiDAcC1iPYp+Y4wJUkABpZLReCC2B pOxOPRm2Ue8+hUxs8i/stjZ90l+HAjZQWFGHch3fmUtO6DCQxzNdADkaS8MuS6ucV03k iRDwjby7Bmk1q+gROtO33562B/NUZoj5RTBdA+9RdzXndOwOSpvlD+esgwSa7bv3rPqr w+w0DXqHIYwHP57Q3n+S5rVqWOGAKz7DMNSW7czJclEm4nccENGApCryp5PPNgeF9R1C 8NKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216764; x=1755821564; 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=eQfmi+GnDIrdp8ejQz7KjH8qWzqOpQFlk46cwdVc498=; b=grLOqYRTu7v645e2iEwmiiLD9VjeXvlIhBcb8kIaoWpQWiD/QLWu26yw8bGpg5ka6X vylUAUhpOJE02I/zFwZM9eDl+fC1k0/7gug25b6fWaGmdG/bh0J5+e+O7slbuBdEgyOM EmL0f/dm69JbktyIMmrVwwQ3Ek0Q0ZlQ09ypxqCPpJx8J2HY4im64VdM/x8+cGc0AL7A A8iGyXyeetspfIzffrQb5Dt9kKiPL7A+7OC7JdgpyKhxZGx5t9LpgWtsqzEbRG/lKzF3 dhax1nligCz+xTI3xZsIzqpxYPoTHAwT3zqZuRYrcad5xrG96Bva7s9r1WWhENGotacf 5JvA== X-Forwarded-Encrypted: i=1; AJvYcCVuRgfGDV58fdz/CTVtEGuDyYp2aCJxefLyPOhGMucqUGeVYCDWgQ1PkfKreZxYkTW5Oz7qPGNsyelTvGs=@vger.kernel.org X-Gm-Message-State: AOJu0Yw/GEUiDUKqpK4JlMCF/WjgX4dsrdGYuAGbEk7HoSWe2HxD+0N7 bL/+E6elngGhINeNGairdgsJp+cb80Gg+JtEdTRWQQhI/0v1WR/TcG/72WjOlaX2FP27l9iPlm5 4/QHiDw== X-Google-Smtp-Source: AGHT+IFtuPHuUUf2dGJRUA8RXNywCif+lc9d/kdwZ3rsFPF10o+G2qnMZNWB7mcc8Bbok7Ll5v8EWWQ9YSk= X-Received: from pjbqn6.prod.google.com ([2002:a17:90b:3d46:b0:31f:1a3e:fe3b]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:cccb:b0:235:7c6:ebdb with SMTP id d9443c01a7336-2446d6dc1bemr1642285ad.10.1755216764117; Thu, 14 Aug 2025 17:12:44 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:12:02 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-19-seanjc@google.com> Subject: [PATCH 6.1.y 18/21] KVM: VMX: Extract checking of guest's DEBUGCTL into helper From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" [ Upstream commit 8a4351ac302cd8c19729ba2636acfd0467c22ae8 ] Move VMX's logic to check DEBUGCTL values into a standalone helper so that the code can be used by nested VM-Enter to apply the same logic to the value being loaded from vmcs12. KVM needs to explicitly check vmcs12->guest_ia32_debugctl on nested VM-Enter, as hardware may support features that KVM does not, i.e. relying on hardware to detect invalid guest state will result in false negatives. Unfortunately, that means applying KVM's funky suppression of BTF and LBR to vmcs12 so as not to break existing guests. No functional change intended. Reviewed-by: Dapeng Mi Link: https://lore.kernel.org/r/20250610232010.162191-6-seanjc@google.com Stable-dep-of: 7d0cce6cbe71 ("KVM: VMX: Wrap all accesses to IA32_DEBUGCTL = with getter/setter APIs") Signed-off-by: Sasha Levin Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 9445def2b3d2..6517b9d929bf 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2071,6 +2071,19 @@ static u64 vmx_get_supported_debugctl(struct kvm_vcp= u *vcpu, bool host_initiated return debugctl; } =20 +static bool vmx_is_valid_debugctl(struct kvm_vcpu *vcpu, u64 data, + bool host_initiated) +{ + u64 invalid; + + invalid =3D data & ~vmx_get_supported_debugctl(vcpu, host_initiated); + if (invalid & (DEBUGCTLMSR_BTF | DEBUGCTLMSR_LBR)) { + kvm_pr_unimpl_wrmsr(vcpu, MSR_IA32_DEBUGCTLMSR, data); + invalid &=3D ~(DEBUGCTLMSR_BTF | DEBUGCTLMSR_LBR); + } + return !invalid; +} + /* * Writes msr value into the appropriate "register". * Returns 0 on success, non-0 otherwise. @@ -2139,19 +2152,12 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struc= t msr_data *msr_info) } vmcs_writel(GUEST_SYSENTER_ESP, data); break; - case MSR_IA32_DEBUGCTLMSR: { - u64 invalid; - - invalid =3D data & ~vmx_get_supported_debugctl(vcpu, msr_info->host_init= iated); - if (invalid & (DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR)) { - kvm_pr_unimpl_wrmsr(vcpu, msr_index, data); - data &=3D ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR); - invalid &=3D ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR); - } - - if (invalid) + case MSR_IA32_DEBUGCTLMSR: + if (!vmx_is_valid_debugctl(vcpu, data, msr_info->host_initiated)) return 1; =20 + data &=3D vmx_get_supported_debugctl(vcpu, msr_info->host_initiated); + if (is_guest_mode(vcpu) && get_vmcs12(vcpu)->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) get_vmcs12(vcpu)->guest_ia32_debugctl =3D data; @@ -2161,7 +2167,6 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct = msr_data *msr_info) (data & DEBUGCTLMSR_LBR)) intel_pmu_create_guest_lbr_event(vcpu); return 0; - } case MSR_IA32_BNDCFGS: if (!kvm_mpx_supported() || (!msr_info->host_initiated && --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 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 4C62321ABDB for ; Fri, 15 Aug 2025 00:12:46 +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=1755216767; cv=none; b=k33t9HdwVPiszYk+T8/1+28/thsV5UeN2Tl9yeIGcSTHTXKirXUJwfV4gNmobgUHM/XiReCtfrJJwIqC/S82IlP3F60J68h2ANaFjKmJQLmXbNCvb61OWWOAN3aSqzXGwgvEO3cJDi9fkq4ohrQwpLJN3IxzZdo1l0vLMESRJc4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216767; c=relaxed/simple; bh=yiaSDx7e6sWQQKFcDrLgGmFh6BpT+OLRzKDneGi1IIc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WDDBgyX3SJxhLgr/wYP8j4oulHrik490XMH8FkIvmph7A+SKMhoJtx6UmNdOEcr7fLm9Qtj0KsSjrE4IYQhTLGGEamKEVADvXaTabmN4llQf/1XRam12BugjhEI/6WBhcLodQx+Bn07wwlxkmghFmk5r1LIZ7Q5LHDITIC4Fgsk= 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=j0LUzwdj; 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="j0LUzwdj" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-32326e21dd6so2806578a91.2 for ; Thu, 14 Aug 2025 17:12:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216766; x=1755821566; 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=sYGTBxrbvnlTTypYR8kPZ7oeuE+b5ffu+snS99kWz7E=; b=j0LUzwdjGRqhGdjMx4dIHckEjsOHjGMFdNbMCCajpbzzL/Z+jgeyiu6k4rlIb3XUnx y4fAUnUyXRC+kLhAUQ9nzhDaUaOlJ1XXHym5C05Nv/wJRUwTSuII3XK5oXVjf8x41t9Y LUtbvT65rCNu9+hzGCHZYMziwDaR+LP8d0lYBWK0hdvpw/Yr3PGP62OFdO7sxYFcJ9xQ Yo7oLx/TngSZ/9GhLLoOMKBOyke5XvE9fKUnbU65hg8RCctU/ByXNiePN4jqXfbhnQ+D BfHiapfYx1NFPAX7RqwvRd479SIvdMG6cFg87+gh0kVr8AjDA/huysuUS+ohdfrb+wCL y0RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216766; x=1755821566; 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=sYGTBxrbvnlTTypYR8kPZ7oeuE+b5ffu+snS99kWz7E=; b=W5X3b54XUIVPRZf8i1AIotSiVK2CAxINdwX8RVZv2gi2EU0utnBfENm/x/h3yzEZdA mN/t6LrtE43Gs0U0+pxRsXF+DomjaLoaWsnWySXKo2p9OpR43KSnrJPCHbLoplr9b3uv RhngHTwwsh6SD1j2UZGSgBNeg+iAeH0anUzhMDYuktk7du1i5LffBW+pzAh77U4qoM/f Crh7wHRJO6W8vnZvcWAPcKUNfRqkO+96r0M0Z/7H8mCyfhr55c3Rkae9ZpNziH/zuRFx zcxwTwLQCYM1FaxyINeEuxUCsuq8wn2A7KX5KK6xp4QytQeAmoEmIad3JtYQFkMDer5L yFMg== X-Forwarded-Encrypted: i=1; AJvYcCXNhA3eJ6hsUD4ffilOhVSHAI+C0P72ZdMH6B3e4AMtKiiFwAGkzC5948e93m3Yl2I/ehTu/fXJRk6Jz0k=@vger.kernel.org X-Gm-Message-State: AOJu0YzVRU/V/n6RpNxae1oP5gux9RUMbYMM/hTzl8cIurl2mdBXdwLK ptZCD806mE/ko/7D5JE01laoPhTbLeknrw8dMT+l3a2CN99rI2rMd+cqXb7BdoHKUWmxLF56VH/ LnzTsiQ== X-Google-Smtp-Source: AGHT+IEa2xwPDg4mAwZRm01WhH0BbYLoS+7JRVpUXoc2tdiWMXQvhEWV0oli5YcLQ+5ol+jWKpEVdPVyD9c= X-Received: from pjbov8.prod.google.com ([2002:a17:90b:2588:b0:31f:210e:e35d]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3bd0:b0:31e:d2a5:c08d with SMTP id 98e67ed59e1d1-32342186735mr317531a91.33.1755216765834; Thu, 14 Aug 2025 17:12:45 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:12:03 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-20-seanjc@google.com> Subject: [PATCH 6.1.y 19/21] KVM: nVMX: Check vmcs12->guest_ia32_debugctl on nested VM-Enter From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maxim Levitsky [ Upstream commit 095686e6fcb4150f0a55b1a25987fad3d8af58d6 ] Add a consistency check for L2's guest_ia32_debugctl, as KVM only supports a subset of hardware functionality, i.e. KVM can't rely on hardware to detect illegal/unsupported values. Failure to check the vmcs12 value would allow the guest to load any harware-supported value while running L2. Take care to exempt BTF and LBR from the validity check in order to match KVM's behavior for writes via WRMSR, but without clobbering vmcs12. Even if VM_EXIT_SAVE_DEBUG_CONTROLS is set in vmcs12, L1 can reasonably expect that vmcs12->guest_ia32_debugctl will not be modified if writes to the MSR are being intercepted. Arguably, KVM _should_ update vmcs12 if VM_EXIT_SAVE_DEBUG_CONTROLS is set *and* writes to MSR_IA32_DEBUGCTLMSR are not being intercepted by L1, but that would incur non-trivial complexity and wouldn't change the fact that KVM's handling of DEBUGCTL is blatantly broken. I.e. the extra complexity is not worth carrying. Cc: stable@vger.kernel.org Signed-off-by: Maxim Levitsky Co-developed-by: Sean Christopherson Link: https://lore.kernel.org/r/20250610232010.162191-7-seanjc@google.com Stable-dep-of: 7d0cce6cbe71 ("KVM: VMX: Wrap all accesses to IA32_DEBUGCTL = with getter/setter APIs") Signed-off-by: Sasha Levin Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 12 ++++++++++-- arch/x86/kvm/vmx/vmx.c | 5 ++--- arch/x86/kvm/vmx/vmx.h | 3 +++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index d55f7edc0860..da129e12cff9 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -2532,7 +2532,8 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, stru= ct vmcs12 *vmcs12, if (vmx->nested.nested_run_pending && (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); - vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); + vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl & + vmx_get_supported_debugctl(vcpu, false)); } else { kvm_set_dr(vcpu, 7, vcpu->arch.dr7); vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.pre_vmenter_debugctl); @@ -3022,7 +3023,8 @@ static int nested_vmx_check_guest_state(struct kvm_vc= pu *vcpu, return -EINVAL; =20 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) && - CC(!kvm_dr7_valid(vmcs12->guest_dr7))) + (CC(!kvm_dr7_valid(vmcs12->guest_dr7)) || + CC(!vmx_is_valid_debugctl(vcpu, vmcs12->guest_ia32_debugctl, false))= )) return -EINVAL; =20 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) && @@ -4374,6 +4376,12 @@ static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *v= cpu, struct vmcs12 *vmcs12) (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); =20 + /* + * Note! Save DR7, but intentionally don't grab DEBUGCTL from vmcs02. + * Writes to DEBUGCTL that aren't intercepted by L1 are immediately + * propagated to vmcs12 (see vmx_set_msr()), as the value loaded into + * vmcs02 doesn't strictly track vmcs12. + */ if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); =20 diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 6517b9d929bf..0b37e21d55b1 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2052,7 +2052,7 @@ static u64 nested_vmx_truncate_sysenter_addr(struct k= vm_vcpu *vcpu, return (unsigned long)data; } =20 -static u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcpu, bool host_ini= tiated) +u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcpu, bool host_initiated) { u64 debugctl =3D 0; =20 @@ -2071,8 +2071,7 @@ static u64 vmx_get_supported_debugctl(struct kvm_vcpu= *vcpu, bool host_initiated return debugctl; } =20 -static bool vmx_is_valid_debugctl(struct kvm_vcpu *vcpu, u64 data, - bool host_initiated) +bool vmx_is_valid_debugctl(struct kvm_vcpu *vcpu, u64 data, bool host_init= iated) { u64 invalid; =20 diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index ddbe73958d7f..99e3f46de2ec 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -442,6 +442,9 @@ static inline void vmx_set_intercept_for_msr(struct kvm= _vcpu *vcpu, u32 msr, =20 void vmx_update_cpu_dirty_logging(struct kvm_vcpu *vcpu); =20 +u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcpu, bool host_initiated); +bool vmx_is_valid_debugctl(struct kvm_vcpu *vcpu, u64 data, bool host_init= iated); + /* * Note, early Intel manuals have the write-low and read-high bitmap offse= ts * the wrong way round. The bitmaps control MSRs 0x00000000-0x00001fff and --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35:18 2025 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 781E322DFA3 for ; Fri, 15 Aug 2025 00:12:49 +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=1755216771; cv=none; b=iiHFKko9YoXTP8vywr4n14RZGU4sZjKdrU7UlNyqwzTql3wcFvrMhf4PWai3Yt9JbckkQ5A9lTAQaqTsaACsMuycGLw3TwJVePG1NVP/cy6qvgs0hYstDkMdTzgEeabPq9vlQ5Wt65V4Ocpiw4mapom1oDhkmIzRiNgcBffqZcc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216771; c=relaxed/simple; bh=YiHyQB/qYAohXmstBYtVCc+gNJrMGrDDL1Alwq4NWxY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IY5aERE31SI/OoI24F81ss7zqkBVu72D3+FuGYM4Qw1YDnh0Q1qPWf/B89iUYh1UIA5TSMroxj/ojVdvoB2n/DdFx8HO3CqPsmWIyrJueoC5QU1hwYSTmS9B/dIwBC+pgbn48Kcyjqi0VAhyKNqpPqpdGGA1eNC35I4behJc0oU= 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=yo4r9KVK; 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="yo4r9KVK" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-32326e6c74eso1624014a91.3 for ; Thu, 14 Aug 2025 17:12:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216769; x=1755821569; 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=och+fjSaGItFDqW/mIp4YckXfoDV7hYGcjSSMk0M92U=; b=yo4r9KVKOu6QLm/7pi2meb7qOdK3vroFFc08pyfM0SLQhrYavXbK/zlwnNvsbJzwMi I4U+3mdaozk8WE6c3+yktKEEC9Xkto2EeK2Xn/ZixjIcwRXObE5hbNF4bH5bBGt52sb8 0jxWeia7EKsG2yfGg5sdSV7teR5loas8YQWtERbPFjqpqPgGLA6UWVqmgCK1z42x1Dtm Ck7hHlpwcwbpkdEvA9QTburf+hSFbQhwYZAM8/fSFY4x8+5LIWEb6gtPg0CPkqEMg2un UkxYay1i19ODTMRUKE7GARhH00xfZundzmWUD5bQvyzicQsH+YuScNQM1x+6/I4eQL4I 2aZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216769; x=1755821569; 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=och+fjSaGItFDqW/mIp4YckXfoDV7hYGcjSSMk0M92U=; b=phlT8xYPbZFhQ+58NgKaAQbksMMQTt0pE45WOQXuoyFUrdICVWa+i3Fo0hiP4I5l9R k+w9fJKbDnEgfr7hJ4G1zwSbommjRRJSZK26OXKIZRZJnbVtxGtS9C5c21N6AYwHxat9 ZuREFqgnZgwdoWXdId6jpzb68BR5tilmpannu8vk3KrhG9AysNmXoYEA9PtqP4178Qrs ZPju7HR03PwG2vICnZSHQpk4ka5jd3ve59VEMCKPFlEGztBEDRAYPuM0+05J/Jze45nT g4XLxfQTLTeV1jyj1ZWl7vHOerfjEHt6vSIRuqFNobd9R0ksvWLmNtViCr9HJ2e7urVN jpzw== X-Forwarded-Encrypted: i=1; AJvYcCXayursslTK5OVRnbtUtN6SQCK4p0f8Z4KwaBJw6YDxzAxKTF30ORYvgZ05Nia4/wFjaUnfS2XlaUELFF4=@vger.kernel.org X-Gm-Message-State: AOJu0YzODqejiGDl1q7oLL7RfcU1KsiTAyiyDkmvCICAP6G6nbvBXuBk WC8MFycyJ2pB+4y1B61qybR6Mwh18y3UWgG5YJdt4/aUQtMa5DQn+CR/jPtjsiVb2S6ZcBO8JgH T9xpS3g== X-Google-Smtp-Source: AGHT+IHnBqQFlSoQZIiUvaYDbCSfuOs+wL7h0mHAfHpPwBt4Jj0voWey9YcQIwrrJgBEcsHkOP+C64kXqm8= X-Received: from pja3.prod.google.com ([2002:a17:90b:5483:b0:323:2b2a:1de5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4f8f:b0:31e:f397:b5b4 with SMTP id 98e67ed59e1d1-32341ec5658mr295776a91.22.1755216768856; Thu, 14 Aug 2025 17:12:48 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:12:04 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-21-seanjc@google.com> Subject: [PATCH 6.1.y 20/21] KVM: VMX: Wrap all accesses to IA32_DEBUGCTL with getter/setter APIs From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maxim Levitsky [ Upstream commit 7d0cce6cbe71af6e9c1831bff101a2b9c249c4a2 ] Introduce vmx_guest_debugctl_{read,write}() to handle all accesses to vmcs.GUEST_IA32_DEBUGCTL. This will allow stuffing FREEZE_IN_SMM into GUEST_IA32_DEBUGCTL based on the host setting without bleeding the state into the guest, and without needing to copy+paste the FREEZE_IN_SMM logic into every patch that accesses GUEST_IA32_DEBUGCTL. No functional change intended. Cc: stable@vger.kernel.org Signed-off-by: Maxim Levitsky [sean: massage changelog, make inline, use in all prepare_vmcs02() cases] Reviewed-by: Dapeng Mi Link: https://lore.kernel.org/r/20250610232010.162191-8-seanjc@google.com Signed-off-by: Sasha Levin Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 10 +++++----- arch/x86/kvm/vmx/pmu_intel.c | 8 ++++---- arch/x86/kvm/vmx/vmx.c | 8 +++++--- arch/x86/kvm/vmx/vmx.h | 10 ++++++++++ 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index da129e12cff9..a220770644e1 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -2532,11 +2532,11 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, st= ruct vmcs12 *vmcs12, if (vmx->nested.nested_run_pending && (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); - vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl & - vmx_get_supported_debugctl(vcpu, false)); + vmx_guest_debugctl_write(vcpu, vmcs12->guest_ia32_debugctl & + vmx_get_supported_debugctl(vcpu, false)); } else { kvm_set_dr(vcpu, 7, vcpu->arch.dr7); - vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.pre_vmenter_debugctl); + vmx_guest_debugctl_write(vcpu, vmx->nested.pre_vmenter_debugctl); } if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending || !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))) @@ -3404,7 +3404,7 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mo= de(struct kvm_vcpu *vcpu, =20 if (!vmx->nested.nested_run_pending || !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) - vmx->nested.pre_vmenter_debugctl =3D vmcs_read64(GUEST_IA32_DEBUGCTL); + vmx->nested.pre_vmenter_debugctl =3D vmx_guest_debugctl_read(); if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending || !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))) @@ -4572,7 +4572,7 @@ static void load_vmcs12_host_state(struct kvm_vcpu *v= cpu, __vmx_set_segment(vcpu, &seg, VCPU_SREG_LDTR); =20 kvm_set_dr(vcpu, 7, 0x400); - vmcs_write64(GUEST_IA32_DEBUGCTL, 0); + vmx_guest_debugctl_write(vcpu, 0); =20 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, vmcs12->vm_exit_msr_load_count)) diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 220cdbe1e286..76d3ed8abf6a 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -672,11 +672,11 @@ static void intel_pmu_reset(struct kvm_vcpu *vcpu) */ static void intel_pmu_legacy_freezing_lbrs_on_pmi(struct kvm_vcpu *vcpu) { - u64 data =3D vmcs_read64(GUEST_IA32_DEBUGCTL); + u64 data =3D vmx_guest_debugctl_read(); =20 if (data & DEBUGCTLMSR_FREEZE_LBRS_ON_PMI) { data &=3D ~DEBUGCTLMSR_LBR; - vmcs_write64(GUEST_IA32_DEBUGCTL, data); + vmx_guest_debugctl_write(vcpu, data); } } =20 @@ -746,7 +746,7 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu) =20 if (!lbr_desc->event) { vmx_disable_lbr_msrs_passthrough(vcpu); - if (vmcs_read64(GUEST_IA32_DEBUGCTL) & DEBUGCTLMSR_LBR) + if (vmx_guest_debugctl_read() & DEBUGCTLMSR_LBR) goto warn; if (test_bit(INTEL_PMC_IDX_FIXED_VLBR, pmu->pmc_in_use)) goto warn; @@ -769,7 +769,7 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu) =20 static void intel_pmu_cleanup(struct kvm_vcpu *vcpu) { - if (!(vmcs_read64(GUEST_IA32_DEBUGCTL) & DEBUGCTLMSR_LBR)) + if (!(vmx_guest_debugctl_read() & DEBUGCTLMSR_LBR)) intel_pmu_release_guest_lbr_event(vcpu); } =20 diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 0b37e21d55b1..e470a294b22d 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2027,7 +2027,7 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct = msr_data *msr_info) msr_info->data =3D vmx->pt_desc.guest.addr_a[index / 2]; break; case MSR_IA32_DEBUGCTLMSR: - msr_info->data =3D vmcs_read64(GUEST_IA32_DEBUGCTL); + msr_info->data =3D vmx_guest_debugctl_read(); break; default: find_uret_msr: @@ -2161,7 +2161,8 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct = msr_data *msr_info) VM_EXIT_SAVE_DEBUG_CONTROLS) get_vmcs12(vcpu)->guest_ia32_debugctl =3D data; =20 - vmcs_write64(GUEST_IA32_DEBUGCTL, data); + vmx_guest_debugctl_write(vcpu, data); + if (intel_pmu_lbr_is_enabled(vcpu) && !to_vmx(vcpu)->lbr_desc.event && (data & DEBUGCTLMSR_LBR)) intel_pmu_create_guest_lbr_event(vcpu); @@ -4751,7 +4752,8 @@ static void init_vmcs(struct vcpu_vmx *vmx) vmcs_write32(GUEST_SYSENTER_CS, 0); vmcs_writel(GUEST_SYSENTER_ESP, 0); vmcs_writel(GUEST_SYSENTER_EIP, 0); - vmcs_write64(GUEST_IA32_DEBUGCTL, 0); + + vmx_guest_debugctl_write(&vmx->vcpu, 0); =20 if (cpu_has_vmx_tpr_shadow()) { vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 99e3f46de2ec..b7ae263cde7b 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -445,6 +445,16 @@ void vmx_update_cpu_dirty_logging(struct kvm_vcpu *vcp= u); u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcpu, bool host_initiated); bool vmx_is_valid_debugctl(struct kvm_vcpu *vcpu, u64 data, bool host_init= iated); =20 +static inline void vmx_guest_debugctl_write(struct kvm_vcpu *vcpu, u64 val) +{ + vmcs_write64(GUEST_IA32_DEBUGCTL, val); +} + +static inline u64 vmx_guest_debugctl_read(void) +{ + return vmcs_read64(GUEST_IA32_DEBUGCTL); +} + /* * Note, early Intel manuals have the write-low and read-high bitmap offse= ts * the wrong way round. The bitmaps control MSRs 0x00000000-0x00001fff and --=20 2.51.0.rc1.163.g2494970778-goog From nobody Sat Oct 4 14:35: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 40E87233D85 for ; Fri, 15 Aug 2025 00:12:51 +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=1755216773; cv=none; b=n9s3nViRrkVe3omitPpWZ+QfMWZwmEQQaXXaczkbQ9OBLMnbCbE3W6deb3Ddj0RNVMvZ+EoySHQMmXunMWQYpJ8UcmTC6nHYkKNG6Lw/rFYHh4Uf905lULS/E1iPLuslcAFBP75GLEliYwId1mxw+EAjo7N42IyGWPI9MWA89Zw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755216773; c=relaxed/simple; bh=lOvxPSaxtzgfnLuwRx2Czq523miEVYErN+utCgJz7BI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UDFFITd0vQJ3P1jWZMPS0A5CL8TeNDZ+D0Za45SeLr/u9FuX9OjPVNh62JTkNSigpQwSY68FD73Gky/wuVYXXwNiKwht7uZGi68A21EHfWm7Pmze/MO4Cqt9yWRceD4/V3nWW9ZmTrFyUklDIVJuGKwtuOjCTkYM9mlW2uPZleI= 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=QwOlrUiV; 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="QwOlrUiV" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2445827ab71so12325025ad.3 for ; Thu, 14 Aug 2025 17:12:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755216771; x=1755821571; 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=c13NaN+YXr/BX/bGMCHRTcbQakErEGNzfF6KpN+Ic+4=; b=QwOlrUiVLfUaXvMjPoOdPjIu57klsuoccp+JjbjCBykkf9YoZLPb3BvZPNyFQEf6lb BF4SktIuxPxZtuZrYGfHuklkc1NDcTwT9PrZdWyvn+LnKI/UOAdnQU9L0Xw3U9elxuaN a/xcpgCn4wsjGffNfD/TaTGFoNDatoP2U26kIFOjVgG21K5+fyECezlMrgBWrrAr5zHu iRTIlubrNm7GTKw5CClp0wFJg7PCOBRIkVkWBQvlOOMqVYujhVlU0bRIKyxRFkYCKL15 XhzV7A+8O1IjUYd4EMnfJW/C0qt++iF8JuYQ9/wUaBIC4gBkq+ri+KDcMNInVdPJ6chL KnVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755216771; x=1755821571; 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=c13NaN+YXr/BX/bGMCHRTcbQakErEGNzfF6KpN+Ic+4=; b=dznL+jXvB99qATnVJ+sIRIZul2qg4FjUzQbI/3ilTRYvCNQTlKvRNAYLDXPp6ivh3h W7mw20UdY/+bpPAPKBdnLn2b3KBWfAbIGERMkc8iPXAsVx/tVrnaGHJ6mEa72QWiGzmR WwdGwz1I0CTycjms5aUSvyLCGvLaqxlF4D3JNTCyAkBeKUVCdBUvIP5/f63EiqlbMeXR daIobofPpKXPaJ8z0Y6dxC/ikK6sh7xYDM4+ybF7wXXkSsEA/7asCxVna/C654qFXxv/ 6vRl3Lry7Y0uI1PUY82HdpV7Ux/FlJ2TNNQ3f5W2KdIUYz0An1HawgOWxdUsH7ZNmZoD CH/A== X-Forwarded-Encrypted: i=1; AJvYcCVeXTvfGghX4AhX8ruv+DquYHLKSiQhwm394fdzNmFG8M9MtEe6i0MPAF87n6ft+S/+JQ1L1g6PMTZ+Yds=@vger.kernel.org X-Gm-Message-State: AOJu0YzbovxSu5N7CEI+tl9tuiFX27JE75Lt6hEL63PM874XzFFftSy3 9PKNZGCIHkJdlJ6ATYrY+8TbsJQF+4lSsqc0gctEraZDIPaqoewp5oqH5h6e2LxKhuX4Sgr0VLp mb5D6QQ== X-Google-Smtp-Source: AGHT+IHQ8AGbnn6QCZLFRxGDCTZzOhReMkq4QJBU0f701KawomGbTYhx5V2Jp8mxit55/TGINdvLjXyC+B8= X-Received: from pjbsb5.prod.google.com ([2002:a17:90b:50c5:b0:312:ea08:fa64]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:fab:b0:23f:f6ca:6a3 with SMTP id d9443c01a7336-2446d8f561dmr1329135ad.43.1755216770682; Thu, 14 Aug 2025 17:12:50 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Aug 2025 17:12:05 -0700 In-Reply-To: <20250815001205.2370711-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: <20250815001205.2370711-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog Message-ID: <20250815001205.2370711-22-seanjc@google.com> Subject: [PATCH 6.1.y 21/21] KVM: VMX: Preserve host's DEBUGCTLMSR_FREEZE_IN_SMM while running the guest From: Sean Christopherson To: stable@vger.kernel.org, Greg Kroah-Hartman , Sasha Levin Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maxim Levitsky [ Upstream commit 6b1dd26544d045f6a79e8c73572c0c0db3ef3c1a ] Set/clear DEBUGCTLMSR_FREEZE_IN_SMM in GUEST_IA32_DEBUGCTL based on the host's pre-VM-Enter value, i.e. preserve the host's FREEZE_IN_SMM setting while running the guest. When running with the "default treatment of SMIs" in effect (the only mode KVM supports), SMIs do not generate a VM-Exit that is visible to host (non-SMM) software, and instead transitions directly from VMX non-root to SMM. And critically, DEBUGCTL isn't context switched by hardware on SMI or RSM, i.e. SMM will run with whatever value was resident in hardware at the time of the SMI. Failure to preserve FREEZE_IN_SMM results in the PMU unexpectedly counting events while the CPU is executing in SMM, which can pollute profiling and potentially leak information into the guest. Check for changes in FREEZE_IN_SMM prior to every entry into KVM's inner run loop, as the bit can be toggled in IRQ context via IPI callback (SMP function call), by way of /sys/devices/cpu/freeze_on_smi. Add a field in kvm_x86_ops to communicate which DEBUGCTL bits need to be preserved, as FREEZE_IN_SMM is only supported and defined for Intel CPUs, i.e. explicitly checking FREEZE_IN_SMM in common x86 is at best weird, and at worst could lead to undesirable behavior in the future if AMD CPUs ever happened to pick up a collision with the bit. Exempt TDX vCPUs, i.e. protected guests, from the check, as the TDX Module owns and controls GUEST_IA32_DEBUGCTL. WARN in SVM if KVM_RUN_LOAD_DEBUGCTL is set, mostly to document that the lack of handling isn't a KVM bug (TDX already WARNs on any run_flag). Lastly, explicitly reload GUEST_IA32_DEBUGCTL on a VM-Fail that is missed by KVM but detected by hardware, i.e. in nested_vmx_restore_host_state(). Doing so avoids the need to track host_debugctl on a per-VMCS basis, as GUEST_IA32_DEBUGCTL is unconditionally written by prepare_vmcs02() and load_vmcs12_host_state(). For the VM-Fail case, even though KVM won't have actually entered the guest, vcpu_enter_guest() will have run with vmcs02 active and thus could result in vmcs01 being run with a stale value. Cc: stable@vger.kernel.org Signed-off-by: Maxim Levitsky Co-developed-by: Sean Christopherson Link: https://lore.kernel.org/r/20250610232010.162191-9-seanjc@google.com Signed-off-by: Sean Christopherson [sean: move vmx/main.c change to vmx/vmx.c] Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/vmx/nested.c | 3 +++ arch/x86/kvm/vmx/vmx.c | 5 +++++ arch/x86/kvm/vmx/vmx.h | 15 ++++++++++++++- arch/x86/kvm/x86.c | 14 ++++++++++++-- 5 files changed, 41 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index c8fc4f2acf69..d0229323ca63 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1459,6 +1459,7 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_m= ode_logical) enum kvm_x86_run_flags { KVM_RUN_FORCE_IMMEDIATE_EXIT =3D BIT(0), KVM_RUN_LOAD_GUEST_DR6 =3D BIT(1), + KVM_RUN_LOAD_DEBUGCTL =3D BIT(2), }; =20 struct kvm_x86_ops { @@ -1484,6 +1485,12 @@ struct kvm_x86_ops { void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu); void (*vcpu_put)(struct kvm_vcpu *vcpu); =20 + /* + * Mask of DEBUGCTL bits that are owned by the host, i.e. that need to + * match the host's value even while the guest is active. + */ + const u64 HOST_OWNED_DEBUGCTL; + void (*update_exception_bitmap)(struct kvm_vcpu *vcpu); int (*get_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr); int (*set_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr); diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index a220770644e1..2c3cf4351c4c 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -4627,6 +4627,9 @@ static void nested_vmx_restore_host_state(struct kvm_= vcpu *vcpu) WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7))); } =20 + /* Reload DEBUGCTL to ensure vmcs01 has a fresh FREEZE_IN_SMM value. */ + vmx_reload_guest_debugctl(vcpu); + /* * Note that calling vmx_set_{efer,cr0,cr4} is important as they * handle a variety of side effects to KVM's software model. diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index e470a294b22d..3fef4e14abc6 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7258,6 +7258,9 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu,= u64 run_flags) if (run_flags & KVM_RUN_LOAD_GUEST_DR6) set_debugreg(vcpu->arch.dr6, 6); =20 + if (run_flags & KVM_RUN_LOAD_DEBUGCTL) + vmx_reload_guest_debugctl(vcpu); + /* * Refresh vmcs.HOST_CR3 if necessary. This must be done immediately * prior to VM-Enter, as the kernel may load a new ASID (PCID) any time @@ -8197,6 +8200,8 @@ static struct kvm_x86_ops vmx_x86_ops __initdata =3D { .vcpu_load =3D vmx_vcpu_load, .vcpu_put =3D vmx_vcpu_put, =20 + .HOST_OWNED_DEBUGCTL =3D DEBUGCTLMSR_FREEZE_IN_SMM, + .update_exception_bitmap =3D vmx_update_exception_bitmap, .get_msr_feature =3D vmx_get_msr_feature, .get_msr =3D vmx_get_msr, diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index b7ae263cde7b..dc6f06326648 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -447,12 +447,25 @@ bool vmx_is_valid_debugctl(struct kvm_vcpu *vcpu, u64= data, bool host_initiated) =20 static inline void vmx_guest_debugctl_write(struct kvm_vcpu *vcpu, u64 val) { + WARN_ON_ONCE(val & DEBUGCTLMSR_FREEZE_IN_SMM); + + val |=3D vcpu->arch.host_debugctl & DEBUGCTLMSR_FREEZE_IN_SMM; vmcs_write64(GUEST_IA32_DEBUGCTL, val); } =20 static inline u64 vmx_guest_debugctl_read(void) { - return vmcs_read64(GUEST_IA32_DEBUGCTL); + return vmcs_read64(GUEST_IA32_DEBUGCTL) & ~DEBUGCTLMSR_FREEZE_IN_SMM; +} + +static inline void vmx_reload_guest_debugctl(struct kvm_vcpu *vcpu) +{ + u64 val =3D vmcs_read64(GUEST_IA32_DEBUGCTL); + + if (!((val ^ vcpu->arch.host_debugctl) & DEBUGCTLMSR_FREEZE_IN_SMM)) + return; + + vmx_guest_debugctl_write(vcpu, val & ~DEBUGCTLMSR_FREEZE_IN_SMM); } =20 /* diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 27e7253972ea..f542ab5e8698 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10591,7 +10591,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) dm_request_for_irq_injection(vcpu) && kvm_cpu_accept_dm_intr(vcpu); fastpath_t exit_fastpath; - u64 run_flags; + u64 run_flags, debug_ctl; =20 bool req_immediate_exit =3D false; =20 @@ -10838,7 +10838,17 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) set_debugreg(0, 7); } =20 - vcpu->arch.host_debugctl =3D get_debugctlmsr(); + /* + * Refresh the host DEBUGCTL snapshot after disabling IRQs, as DEBUGCTL + * can be modified in IRQ context, e.g. via SMP function calls. Inform + * vendor code if any host-owned bits were changed, e.g. so that the + * value loaded into hardware while running the guest can be updated. + */ + debug_ctl =3D get_debugctlmsr(); + if ((debug_ctl ^ vcpu->arch.host_debugctl) & kvm_x86_ops.HOST_OWNED_DEBUG= CTL && + !vcpu->arch.guest_state_protected) + run_flags |=3D KVM_RUN_LOAD_DEBUGCTL; + vcpu->arch.host_debugctl =3D debug_ctl; =20 guest_timing_enter_irqoff(); =20 --=20 2.51.0.rc1.163.g2494970778-goog