mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Yosry Ahmed <yosry@kernel.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Jim Mattson <jmattson@google.com>,
	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
Date: Tue, 1 Dec 2026 15:47:13 -0800	[thread overview]
Message-ID: <aw9cgai0_KE3pV55@seanjc-mac.roam.internal> (raw)
In-Reply-To: <aw8B4sRhtLpqM9mF@google.com>

On Tue, Dec 01, 2026, Yosry Ahmed wrote:
> 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.

Well, kvm_pmu_trigger_event() is very different, as it's used from the fastpath
run loop and we explicitly want to support that.  For kvm_pmu_handle_event(),
its caller(s) really should acquire SCRU.

I'm not opposed to a more precise fix, though if we go that route, I'd vote to
make it *extremely* precise, e.g. put it in svm_leave_nested() or something.

> I think we generally don't want to hold the SRCU read lock more than
> needed?

Eh, only if we care about letting flows run in parallel.  I suppose it's possible
userspace would configure memslots and/or I/O buses concurrently with restoring
vCPU state, but I doubt it's a high priority.

> 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:

If we go with a precise fix, revert it.  If we smack all of KVM_SET_SREGS, leave
it there.

  reply	other threads:[~2026-12-01 23:47 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 18:27 [PATCH 0/3] KVM: x86/pmu: Always defer PMU reprogramming on " Yosry Ahmed
2026-11-25 18:27 ` [PATCH 1/3] KVM: x86/pmu: Recalc PMC emulation when deferring counter reprogramming Yosry Ahmed
2026-11-25 18:27 ` [PATCH 2/3] KVM: x86/pmu: Always defer PMU reprogramming on SVM nested transitions Yosry Ahmed
2026-12-01 17:14   ` Sean Christopherson
2026-12-01 17:23     ` Yosry Ahmed
2026-12-01 23:47       ` Sean Christopherson [this message]
2026-11-25 18:27 ` [PATCH 3/3] KVM: selftests: Set EFER.SVME via KVM_SET_SREGS in svm_pmu_host_guest_test Yosry Ahmed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aw9cgai0_KE3pV55@seanjc-mac.roam.internal \
    --to=seanjc@google.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=yosry@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®