All of lore.kernel.org
 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: Wed, 2 Dec 2026 13:26:29 +0800	[thread overview]
Message-ID: <0f15903d-8354-496e-8d03-3ea3e15e38df@linux.intel.com> (raw)
In-Reply-To: <CAO9r8zOTTTYBLWsn_GzQNd0DtZRMm-EiZ4FTMoossuzce7Oe8A@mail.gmail.com>


On 12/2/2026 12:32 PM, Yosry Ahmed wrote:
> On Tue, Dec 1, 2026 at 7:39 PM Mi, Dapeng <dapeng1.mi@linux.intel.com> wrote:
>>
>> On 12/2/2026 9:14 AM, Sean Christopherson wrote:
>>> On Tue, Dec 01, 2026, Yosry Ahmed wrote:
>>>> On Tue, Dec 1, 2026 at 3:50 PM Sean Christopherson <seanjc@google.com> wrote:
>>>>> On Tue, Dec 01, 2026, Yosry Ahmed wrote:
>>>>>> When loading the guest PMU context, clear PERF_GLOBAL_CTRL after
>>>>>> switching to guest LVTPC. Otherwise, the host perf NMI handler may set
>>>>>> bits in PERF_GLOBAL_CTRL after KVM clears it (specifically on Intel,
>>>>>> through __intel_pmu_enable_all()).
>>>>> But disabling PERF_GLOBAL_CTRL should prevent taking any perf NMIs, no?
>>>> If I am reading the code correctly, perf_event_nmi_handler() ->
>>>> intel_pmu_handle_irq() -> __intel_pmu_enable_all() will be executed on
>>>> an NMI even if it wasn't a perf NMI (__intel_pmu_enable_all() is
>>>> called in the done label even if status is clear), but I might be
>>>> mistaken.
>>> That feels like a perf bug?  I would expect perf_load_guest_context() to prevent
>>> trying to enable perf events.
> IIUC, perf not clearing PERF_GLOBAL_CTRL after
> perf_load_guest_context() is the reason KVM clears PERF_GLOBAL_CTRL
> here to begin with:
> https://lore.kernel.org/all/aR43LoV1ti5-2WRD@google.com/.
>
>> perf_load_guest_context() schedule out all host events, and
>> perf_event_nmi_handler() should early return and don't touch any PMU MSRs
>> if active_events is 0. While the issue is that active_events currently
>> doesn't really track the active events as what it intends. Instead, it
>> seems to track the created events regardless of it's active. This is a perf
>> bug.
>>
>> It's intended to clear GLOBAL_CTRL MSR before calling
>> perf_load_guest_lvtpc(), it prevents triggering PMI from hit into
>> perf_load_guest_lvtpc().
>>
>> It looks the better way to fix this issue is to make active_events really
>> track the active events (increase/decrease active_events in
>> x86_pmu_add()/x86_pmu_del()).
> If active_events is always 0 while the guest LVTPC is loaded, then we
> can probably drop guest_lvtpc_loaded, right?

I think so.


>
> Having perf clear GLOBAL_CTRL when no events are active (e.g. in
> perf_load_guest_context() and in the NMI handler) would also let KVM
> drop the MSR write completely. It seems like that's what Peter and
> Sean initially expected based on the discussion linked above

Currently GLOBAL_CTRL keeps enabled for all counters by default even though
there is no active event. Whether a single event is enabled depends on its
EVENTSEL "enable" field. GLOBAL_CTRL is usually used to ensure all counters
are enabled or disabled simultaneously. 

In theory, it can work by making perf_load_guest_context() clear
GLOBAL_CTRL, but it won't be a simple change and could break current x86
perf convention. It may cause some corner issues.

IMO, making active_events really track active events could be the simplest
and straightforward way to fix this issue.

Thanks.


>
> Anyway, either of the above seems like it may be a more intrusive fix
> for stable, but if Sean/Peter think it's fine then it works for me
> too.

  reply	other threads:[~2026-12-02  5:26 UTC|newest]

Thread overview: 16+ 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 [this message]
2026-12-02  5:48               ` Yosry Ahmed
2026-12-02  7:18                 ` 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=0f15903d-8354-496e-8d03-3ea3e15e38df@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.