From nobody Sat Oct 4 16:15:04 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