From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796145824; b=NkMh6nsqe4EvIB7Bu+0KMlU9MsDT3ZBKFpP6r6zh16RT+EFFV7bQnZp2KrFIbshPsA9v Iy1qFxCkwlN1RY0vyOzh/t8oUezIs6YlK4ugumG/HBMyfRPa4yPnHLkiab+ydKoS4ycoV JwdS+8DJu56v4Y7aKcs/8cXyA3L+UPLlkhid/RYQZBhx6QZrT/OX8zAoLIHZT5RWZomxN ohz2Hw+bZLXnUBldLkxb62zfTFUkYZoP179kVdjZ/8zv9RWzJ18nX/BlucMX3XVCIYt/J WdksMGySp3GA+y7qQbOCpboViqDke0aqjZhwDyBSPCTWCPFG1ADtb8vmvU3EOH/lPTQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796145824; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=AyUG9gu+hmnFVbMMWfb+vwn/Mn7sdWo5x2neojrw8NI=; b=CFzCaA08U28UvBiD1Nobi7coNY6XDnBg9Xd3269I3zoTAaN9R43spPrz1CVqvnyMOs5n LSXvlxP8WbKGoVi6XjyompkyeHyNwQD3tUiYg7n6PknbsImggvvo2sFxIClKwZT4/qTrR j/bjRN8c117qPnQAEaV/gzr68g938Yv17RNF9R+4YErDKQm7Xju9d+ouqI8RAEcItWcrm vpceqn7QlU20Ph/nhjkB2IyVyIHuUEERE67vQbfzRMbO5Pi6o16YpO9Hu0UEukIY18CeU thTYlZ0bzzoO2KbY4czJqd4VMkGYaL2Zmdtc1xyi+M17A+L/OGsUBozomCEmHElhZ2Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=oQNVav2j; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C0285606A325; Tue, 1 Dec 2026 17:23:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=oQNVav2j Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24DAA1F008A0; Tue, 1 Dec 2026 17:23:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796145824; bh=AyUG9gu+hmnFVbMMWfb+vwn/Mn7sdWo5x2neojrw8NI=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=oQNVav2jkSJ8oHPqnRoIV3cnP7vyDQxYydLW2QbJaBRQDgmmhHSMmyPBNknI7I0QW XsdDcKI6GZTfOqy0P2zy2GGwziyp3oBQZVOPXaieJspwaaTKfSVLF39Zkdjtd0I/hH JboWsQ3O9sBc65gJS8QonRxBfWzhrjUruHQMd7LICqgdi5o4BhuOpwkjcU6BeW2LAh mIfnMaKRjWs8PmnAqx2YvesCw7kSle6gdy3S9CivhEjV2SWX0IbtrPpnt7YuETO+6g RGwpgpiJKmvD0aDfhpwSybwRvHKDuM5QUNdHQeI5/AlkdN8GKH4cjQnhFy0p1mHLbt UeO8qD02jzvuw== Date: Tue, 1 Dec 2026 17:23:42 +0000 From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH 2/3] KVM: x86/pmu: Always defer PMU reprogramming on SVM nested transitions Message-ID: References: <20261125182703.1694998-1-yosry@kernel.org> <20261125182703.1694998-3-yosry@kernel.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Tue, Dec 01, 2026 at 09:14:16AM -0800, Sean Christopherson wrote: > On Wed, Nov 25, 2026, Yosry Ahmed wrote: > > Always defer PMU counter reprogramming via KVM_REQ_PMU on SVM nested > > transitions and EFER.SVME toggles instead of invoking > > kvm_pmu_handle_event() synchronously. The motivation for synchronous > > handling was to ensure to-be-emulated PMCs are updated before > > instruction emulation completes, such that instructions count in the > > mode they retire in (as per the hardware behavior). > > > > However, since kvm_pmu_request_counters_reprogram() now recalculates > > to-be-emulated PMCs synchronously, and kvm_pmu_recalc_pmc_emulation() -> > > pmc_is_locally_enabled() already accounts for Host-Only and Guest-Only > > bits on AMD, VMRUN or WRMSR(MSR_EFER) would correctly be counted in the > > mode they retire in. > > > > In addition to being a nice cleanup, this fixes an unprotected SRCU > > access (and potential UAF) when userspace updates EFER.SVME via > > KVM_SET_SREGS[2], as svm_set_efer() ends up calling > > kvm_mediated_pmu_refresh_event_filter() -> pmc_is_event_allowed(), which > > dereferences kvm->arch.pmu_event_filter via srcu_dereference(). This > > results in the following splat (with CONFIG_PROVE_RCU): > > > > WARNING: suspicious RCU usage > > arch/x86/kvm/pmu.c:539 suspicious rcu_dereference_check() usage! > > ... > > pmc_is_event_allowed+0x27c/0x2b0 [kvm] > > kvm_pmu_handle_event+0x365/0x880 [kvm] > > svm_set_efer+0x16b/0x1a0 [kvm_amd] > > __set_sregs_common+0x2fd/0x6a0 [kvm] > > __set_sregs+0x37/0xd0 [kvm] > > kvm_arch_vcpu_ioctl_set_sregs+0x3e/0x50 [kvm] > > kvm_vcpu_ioctl+0x53a/0x6b0 [kvm] > > > > Fixes: a02a25a65246 ("KVM: x86/pmu: Reprogram Host/Guest-Only counters on nested transitions") > > I strongly prefer we fix the immediate SRCU issue and then do the more agressive > cleanup. I don't want to get into a game of whack-a-mole with stable@ backports, > e.g. if it turns out there's a flaw with always deferring updates. Yeah I figured you might prefer this (and called it out in the cover letter). > > IIUC, it would be this? Actually what I had was more localized, in kvm_pmu_handle_event(). It's consistent with kvm_pmu_trigger_event(), and avoids hiding other bugs. I think we generally don't want to hold the SRCU read lock more than needed? I can add this as patch #1 in the series for a stable fix, not sure if you'd prefer that we keep it anyway or revert it later in the series: diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index ae781bce7d322..e740da2f2a92e 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -662,7 +662,7 @@ void kvm_pmu_handle_event(struct kvm_vcpu *vcpu) DECLARE_BITMAP(bitmap, X86_PMC_IDX_MAX); struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); struct kvm_pmc *pmc; - int bit, r; + int bit, r, idx; bitmap_copy(bitmap, pmu->reprogram_pmi, X86_PMC_IDX_MAX); @@ -674,6 +674,7 @@ void kvm_pmu_handle_event(struct kvm_vcpu *vcpu) BUILD_BUG_ON(sizeof(bitmap) != sizeof(atomic64_t)); atomic64_andnot(*(s64 *)bitmap, &pmu->__reprogram_pmi); + idx = srcu_read_lock(&vcpu->kvm->srcu); kvm_for_each_pmc(pmu, pmc, bit, bitmap) { /* * If reprogramming fails on a transient condition, e.g. due to @@ -686,6 +687,7 @@ void kvm_pmu_handle_event(struct kvm_vcpu *vcpu) if (r == -EBUSY || r == -ENOMEM) set_bit(pmc->idx, pmu->reprogram_pmi); } + srcu_read_unlock(&vcpu->kvm->srcu, idx); /* * Release unused perf_events if the corresponding guest MSRs weren't > > diff --git a/arch/x86/kvm/regs.c b/arch/x86/kvm/regs.c > index 3621f1a47d37..36e518df017b 100644 > --- a/arch/x86/kvm/regs.c > +++ b/arch/x86/kvm/regs.c > @@ -577,7 +577,6 @@ static bool kvm_is_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) > static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs, > int *mmu_reset_needed, bool update_pdptrs) > { > - int idx; > struct desc_ptr dt; > > if (!kvm_is_valid_sregs(vcpu, sregs)) > @@ -589,6 +588,8 @@ static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs, > if (vcpu->arch.guest_state_protected) > return 0; > > + guard(srcu)(&vcpu->kvm->srcu); > + > dt.size = sregs->idt.limit; > dt.address = sregs->idt.base; > kvm_x86_call(set_idt)(vcpu, &dt); > @@ -611,13 +612,9 @@ static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs, > *mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; > kvm_x86_call(set_cr4)(vcpu, sregs->cr4); > > - if (update_pdptrs) { > - idx = srcu_read_lock(&vcpu->kvm->srcu); > - if (is_pae_paging(vcpu)) { > - load_pdptrs(vcpu, kvm_read_cr3(vcpu)); > - *mmu_reset_needed = 1; > - } > - srcu_read_unlock(&vcpu->kvm->srcu, idx); > + if (update_pdptrs && is_pae_paging(vcpu)) { > + load_pdptrs(vcpu, kvm_read_cr3(vcpu)); > + *mmu_reset_needed = 1; > } > > kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); >