Linux Perf Users
 help / color / mirror / Atom feed
From: "Mi, Dapeng" <dapeng1.mi@linux.intel.com>
To: Yosry Ahmed <yosry@kernel.org>
Cc: Sean Christopherson <seanjc@google.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	James Clark <james.clark@linaro.org>,
	Paolo Bonzini <pbonzini@redhat.com>,
	x86@kernel.org, Mingwei Zhang <mizhang@google.com>,
	linux-perf-users@vger.kernel.org, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
Date: Thu, 3 Dec 2026 09:09:44 +0800	[thread overview]
Message-ID: <57f648a3-3b15-4c6f-bb7f-9a219c41ad9a@linux.intel.com> (raw)
In-Reply-To: <CAO9r8zMnKaxY3LMfC6J13HBRFOvyf6p8t-yuC6j6K=sV+vc9tw@mail.gmail.com>


On 12/3/2026 5:04 AM, Yosry Ahmed wrote:
>>>> IMO, making active_events really track active events could be the simplest
>>>> and straightforward way to fix this issue.
>>> Taking a closer look, I don't think this is viable:
>>> - active_events is global, not per-CPU.
>> Yes, but we can extend the active_events to per-cpu, PMI is per-cpu, right?
> Maybe? I didn't take a closer look to check how straightforward it might be.
>
>>> - If active_events tracks currently scheduled events (which is what we
>>> need here AFAICT), bailing in the NMI handler can miss a PMI if a PMI
>>> was in-flight or delayed when the event is scheduled out.
>> Generally the GLOBAL_CTRL is cleared in the entire schedule_in or
>> schedule_out process until the scheduling finishes, this would prevent the
>> PMI is triggered again in the event scheduling in theory.
> Hmm yeah if we always disable the PMU first (and account for NMI delays).
>
>>> - active_events is not necessarily 0 while the guest mediated PMU
>>> context is loaded. Other PMUs like Intel PT/BTS could have active
>>> events AFAICT.
>> Yes, Intel PT is an issue. The simplest way to fix the issue is to disallow
>> PT and mediated vPMU to work in coexistence.
> That was also a sticking point in the dynamic PMU context switching
> RFC, Intel PT and BTS use the same PMI vector but are not mediated (so
> are not scheduled out).

Yes, that's what I'm concerning. PT and BTS shares same PMI with PMU, while
currently we only disable the PMU instead of all including PT and BTS. If
PT and BTS are not disabled, is it safe enough to switch the PMI vector?


>
> I think changing the semantics of active_events or adding mutual
> exclusion between Intel PT/BTS and the mediated vPMU is a larger lift
> that can be explored separately.

I agree. The fix for active_events can be done as a separate patchset, it's
more complicated than I initially thought.


>
>> An alternative way is to move perf_load_guest_lvtpc() into
>> perf_load_guest_context(), call perf_load_guest_context() between the
>> disabling perf contexts and enabling perf contexts. Actually it goes back
>> our previous implementation.
> I think this works as an alternative to this patch, although it still
> doesn't guarantee PERF_GLOBAL_CTRL being 0 when LVTPC is switched
> AFAICT. I think perf_ctx_disable() may not clear PERF_GLOBAL_CTRL if
> there are no core PMU events being scheduled out. Also,
> perf_ctx_enable() will re-enable PERF_GLOBAL_CTRL before returning
> anyway, so KVM still needs to clear it afterwards. So it doesn't buy
> us much compared to this patch, does it?

Did I miss something? perf_ctx_disable() would always call
x86_pmu_disable() to disable the whole PMU if it's not skipped, it doesn't
check if there are active events. 

From my point of view,  the inside of perf_load_guest_context() may be the
best place to call perf_load_guest_lvtpc() to switch the PMI vector,
especially considering if we also need to disable PT and BTS PMU before
switching the PMI vector.

But as you mentioned, Peter didn't like it. perf_load_guest_context() is
perf/core scope function which shared by all architectures, while
perf_load_guest_lvtpc() is a x86 specific function. It makes sense that
Peter doesn't want they are mixed together. 


>
> Additionally, we'd need to restore the arch hook which I think Peter
> didn't like in previous implementations:
> https://lore.kernel.org/all/20250815113951.GC4067720@noisy.programming.kicks-ass.net/
>
>> I'm fine with either way.
> I think out of all the options I prefer this patch because it's a much
> smaller change and is more stable-friendly. I am also fine with moving
> perf_load_guest_lvtpc() into perf_load_guest_context() if others are
> okay with it.

The reason that I don't like current implementation is that the PMU is not
fully disabled before switching the PMI vector. Although all host events
are scheduled out, it doesn't mean all PMU functionalities are disabled,
like the PERF_METRICS. PERF_METRICS has no an extra independent MSR to
manage the enabling, which is different with other counters. If
PERF_METRICS is not disabled in the GLOBAL_CTRL, a PERF_METRICS overflow
PMI could be triggered during the PMI vector switching, it could be unsafe.

As the change log says, we could move the clearing of GLOBAL_CTRL into
perf_load_guest_context() and put it before the vector switching, which
looks better than current implementation.

If we want to ensure the 100% safe, we may also need to disable PT and BTS
along with the PMU before the vector switching.

Thanks.



  reply	other threads:[~2026-12-03  1:09 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 17:14 [PATCH 0/2] perf/x86: Fix races when loading guest mediated PMU context Yosry Ahmed
2026-12-01 17:14 ` [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC Yosry Ahmed
2026-12-01 23:56   ` Sean Christopherson
2026-12-02  0:39     ` Yosry Ahmed
2026-12-02  2:44   ` Mi, Dapeng
2026-12-02  4:18     ` Yosry Ahmed
2026-12-01 17:14 ` [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC Yosry Ahmed
2026-12-01 23:50   ` Sean Christopherson
2026-12-02  0:46     ` Yosry Ahmed
2026-12-02  1:14       ` Sean Christopherson
2026-12-02  3:39         ` Mi, Dapeng
2026-12-02  4:32           ` Yosry Ahmed
2026-12-02  5:26             ` Mi, Dapeng
2026-12-02  5:48               ` Yosry Ahmed
2026-12-02  7:18                 ` Mi, Dapeng
2026-12-02 21:04                   ` Yosry Ahmed
2026-12-03  1:09                     ` Mi, Dapeng [this message]
2026-12-03  1:44                       ` Yosry Ahmed
2026-12-03  6:10                         ` Mi, Dapeng
2026-12-01 17:20 ` [PATCH 0/2] perf/x86: Fix races when loading guest mediated PMU context sashiko-bot

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=57f648a3-3b15-4c6f-bb7f-9a219c41ad9a@linux.intel.com \
    --to=dapeng1.mi@linux.intel.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=mizhang@google.com \
    --cc=namhyung@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=seanjc@google.com \
    --cc=stable@vger.kernel.org \
    --cc=x86@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