* [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events @ 2023-11-07 18:36 Sean Christopherson 2023-11-07 19:38 ` Mingwei Zhang 2023-11-17 10:32 ` Peter Zijlstra 0 siblings, 2 replies; 8+ messages in thread From: Sean Christopherson @ 2023-11-07 18:36 UTC (permalink / raw) To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Thomas Gleixner, Borislav Petkov, Dave Hansen, x86 Cc: linux-perf-users, linux-kernel, Like Xu, Jim Mattson, Mingwei Zhang, Sean Christopherson Don't apply minimum period workarounds/requirements to events that are being created by KVM to virtualize PMCs for guests, i.e. skip limit enforcement for events that exclude the host. Perf's somewhat arbitrary limits prevents KVM from correctly virtualizing counter overflow, e.g. if the guest sets a counter to have an effective period of '1', forcing a minimum period of '2' results in overflow occurring at the incorrect time. Whether or not a "real" profiling use case is affected is debatable, but the incorrect behavior is trivially easy to observe and reproduce, and is deterministic enough to make the PMU appear to be broken from the guest's perspective. Furthermore, the "period" set by KVM isn't actually a period, as KVM won't automatically reprogram the event with the same period on overflow. KVM will synthesize a PMI into the guest when appropriate, but what the guest does in response to the PMI is purely a guest decision. In other words, KVM effectively operates in a one-shot mode, not a periodic mode. Letting KVM and/or the guest program "too small" periods is safe for the host, as events that exclude the host are atomically disabled with respect to VM-Exit, i.e. are guaranteed to stop counting upon transitioning to the host. And whether or not *explicitly* programming a short period is safe is somewhat of a moot point, as transitions to/from the guest effectively yield the same effect, e.g. an unrelated VM-Exit => VM-Enter transition will re-enable guest PMCs with whatever count happened to be in the PMC at the time of VM-Exit. Cc: Like Xu <likexu@tencent.com> Cc: Jim Mattson <jmattson@google.com> Cc: Mingwei Zhang <mizhang@google.com> Signed-off-by: Sean Christopherson <seanjc@google.com> --- Disclaimer: I've only tested this from KVM's side of things. arch/x86/events/core.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 40ad1425ffa2..f8a8a4ea4d47 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -1388,16 +1388,25 @@ int x86_perf_event_set_period(struct perf_event *event) hwc->last_period = period; ret = 1; } - /* - * Quirk: certain CPUs dont like it if just 1 hw_event is left: - */ - if (unlikely(left < 2)) - left = 2; if (left > x86_pmu.max_period) left = x86_pmu.max_period; - static_call_cond(x86_pmu_limit_period)(event, &left); + /* + * Exempt KVM guest events from the minimum period requirements. It's + * the guest's responsibility to ensure it can make forward progress, + * and it's KVM's responsibility to configure an appropriate "period" + * to correctly virtualize overflow for the guest's PMCs. + */ + if (!event->attr.exclude_host) { + /* + * Quirk: certain CPUs dont like it if just 1 event is left: + */ + if (unlikely(left < 2)) + left = 2; + + static_call_cond(x86_pmu_limit_period)(event, &left); + } this_cpu_write(pmc_prev_left[idx], left); base-commit: 744940f1921c8feb90e3c4bcc1e153fdd6e10fe2 -- 2.42.0.869.gea05f2083d-goog ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events 2023-11-07 18:36 [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events Sean Christopherson @ 2023-11-07 19:38 ` Mingwei Zhang 2023-11-07 23:02 ` Sean Christopherson 2023-11-17 10:32 ` Peter Zijlstra 1 sibling, 1 reply; 8+ messages in thread From: Mingwei Zhang @ 2023-11-07 19:38 UTC (permalink / raw) To: Sean Christopherson Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Thomas Gleixner, Borislav Petkov, Dave Hansen, x86, linux-perf-users, linux-kernel, Like Xu, Jim Mattson On Tue, Nov 07, 2023, Sean Christopherson wrote: > Don't apply minimum period workarounds/requirements to events that are > being created by KVM to virtualize PMCs for guests, i.e. skip limit > enforcement for events that exclude the host. Perf's somewhat arbitrary > limits prevents KVM from correctly virtualizing counter overflow, e.g. if > the guest sets a counter to have an effective period of '1', forcing a > minimum period of '2' results in overflow occurring at the incorrect time. > > Whether or not a "real" profiling use case is affected is debatable, but > the incorrect behavior is trivially easy to observe and reproduce, and is > deterministic enough to make the PMU appear to be broken from the guest's > perspective. > > Furthermore, the "period" set by KVM isn't actually a period, as KVM won't > automatically reprogram the event with the same period on overflow. KVM > will synthesize a PMI into the guest when appropriate, but what the guest > does in response to the PMI is purely a guest decision. In other words, > KVM effectively operates in a one-shot mode, not a periodic mode. > > Letting KVM and/or the guest program "too small" periods is safe for the > host, as events that exclude the host are atomically disabled with respect > to VM-Exit, i.e. are guaranteed to stop counting upon transitioning to the > host. And whether or not *explicitly* programming a short period is safe > is somewhat of a moot point, as transitions to/from the guest effectively > yield the same effect, e.g. an unrelated VM-Exit => VM-Enter transition > will re-enable guest PMCs with whatever count happened to be in the PMC at > the time of VM-Exit. > > Cc: Like Xu <likexu@tencent.com> > Cc: Jim Mattson <jmattson@google.com> > Cc: Mingwei Zhang <mizhang@google.com> > Signed-off-by: Sean Christopherson <seanjc@google.com> > --- > > Disclaimer: I've only tested this from KVM's side of things. > > arch/x86/events/core.c | 21 +++++++++++++++------ > 1 file changed, 15 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c > index 40ad1425ffa2..f8a8a4ea4d47 100644 > --- a/arch/x86/events/core.c > +++ b/arch/x86/events/core.c > @@ -1388,16 +1388,25 @@ int x86_perf_event_set_period(struct perf_event *event) > hwc->last_period = period; > ret = 1; > } > - /* > - * Quirk: certain CPUs dont like it if just 1 hw_event is left: > - */ > - if (unlikely(left < 2)) > - left = 2; > > if (left > x86_pmu.max_period) > left = x86_pmu.max_period; > > - static_call_cond(x86_pmu_limit_period)(event, &left); > + /* > + * Exempt KVM guest events from the minimum period requirements. It's > + * the guest's responsibility to ensure it can make forward progress, > + * and it's KVM's responsibility to configure an appropriate "period" > + * to correctly virtualize overflow for the guest's PMCs. > + */ > + if (!event->attr.exclude_host) { > + /* > + * Quirk: certain CPUs dont like it if just 1 event is left: > + */ > + if (unlikely(left < 2)) > + left = 2; > + > + static_call_cond(x86_pmu_limit_period)(event, &left); > + } > > this_cpu_write(pmc_prev_left[idx], left); > Nice one. I am curious how you tested this one? I would like to reproduce that one on my side. > > base-commit: 744940f1921c8feb90e3c4bcc1e153fdd6e10fe2 > -- > 2.42.0.869.gea05f2083d-goog > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events 2023-11-07 19:38 ` Mingwei Zhang @ 2023-11-07 23:02 ` Sean Christopherson 2023-11-07 23:47 ` Mingwei Zhang 0 siblings, 1 reply; 8+ messages in thread From: Sean Christopherson @ 2023-11-07 23:02 UTC (permalink / raw) To: Mingwei Zhang Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Thomas Gleixner, Borislav Petkov, Dave Hansen, x86, linux-perf-users, linux-kernel, Like Xu, Jim Mattson On Tue, Nov 07, 2023, Mingwei Zhang wrote: > On Tue, Nov 07, 2023, Sean Christopherson wrote: > > arch/x86/events/core.c | 21 +++++++++++++++------ > > 1 file changed, 15 insertions(+), 6 deletions(-) > > > > diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c > > index 40ad1425ffa2..f8a8a4ea4d47 100644 > > --- a/arch/x86/events/core.c > > +++ b/arch/x86/events/core.c > > @@ -1388,16 +1388,25 @@ int x86_perf_event_set_period(struct perf_event *event) > > hwc->last_period = period; > > ret = 1; > > } > > - /* > > - * Quirk: certain CPUs dont like it if just 1 hw_event is left: > > - */ > > - if (unlikely(left < 2)) > > - left = 2; > > > > if (left > x86_pmu.max_period) > > left = x86_pmu.max_period; > > > > - static_call_cond(x86_pmu_limit_period)(event, &left); > > + /* > > + * Exempt KVM guest events from the minimum period requirements. It's > > + * the guest's responsibility to ensure it can make forward progress, > > + * and it's KVM's responsibility to configure an appropriate "period" > > + * to correctly virtualize overflow for the guest's PMCs. > > + */ > > + if (!event->attr.exclude_host) { > > + /* > > + * Quirk: certain CPUs dont like it if just 1 event is left: > > + */ > > + if (unlikely(left < 2)) > > + left = 2; > > + > > + static_call_cond(x86_pmu_limit_period)(event, &left); > > + } > > > > this_cpu_write(pmc_prev_left[idx], left); > > > > Nice one. I am curious how you tested this one? I would like to > reproduce that one on my side. The check_emulated_instr() sub-test in KVM-Unit-Tests's x86/pmu.c fails when run with "my" (which is really yours) fix for the KVM's handling of emulated PMC events[*]. If KVM synthesizes an "instructions retired" event that bumps the PMC to all ones, i.e. -1 for all intents and purposes, the test fails because KVM creates a sample_period of '1', but perf programs a period of '2'. I suspect a very simple test of writing -1 to a PMC from the guest would exhibit the same behavior. [*] https://lkml.kernel.org/r/ZUWAg3WP2XESCAR4%40google.com ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events 2023-11-07 23:02 ` Sean Christopherson @ 2023-11-07 23:47 ` Mingwei Zhang 0 siblings, 0 replies; 8+ messages in thread From: Mingwei Zhang @ 2023-11-07 23:47 UTC (permalink / raw) To: Sean Christopherson Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Thomas Gleixner, Borislav Petkov, Dave Hansen, x86, linux-perf-users, linux-kernel, Like Xu, Jim Mattson On Tue, Nov 7, 2023 at 3:02 PM Sean Christopherson <seanjc@google.com> wrote: > > On Tue, Nov 07, 2023, Mingwei Zhang wrote: > > On Tue, Nov 07, 2023, Sean Christopherson wrote: > > > arch/x86/events/core.c | 21 +++++++++++++++------ > > > 1 file changed, 15 insertions(+), 6 deletions(-) > > > > > > diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c > > > index 40ad1425ffa2..f8a8a4ea4d47 100644 > > > --- a/arch/x86/events/core.c > > > +++ b/arch/x86/events/core.c > > > @@ -1388,16 +1388,25 @@ int x86_perf_event_set_period(struct perf_event *event) > > > hwc->last_period = period; > > > ret = 1; > > > } > > > - /* > > > - * Quirk: certain CPUs dont like it if just 1 hw_event is left: > > > - */ > > > - if (unlikely(left < 2)) > > > - left = 2; > > > > > > if (left > x86_pmu.max_period) > > > left = x86_pmu.max_period; > > > > > > - static_call_cond(x86_pmu_limit_period)(event, &left); > > > + /* > > > + * Exempt KVM guest events from the minimum period requirements. It's > > > + * the guest's responsibility to ensure it can make forward progress, > > > + * and it's KVM's responsibility to configure an appropriate "period" > > > + * to correctly virtualize overflow for the guest's PMCs. > > > + */ > > > + if (!event->attr.exclude_host) { > > > + /* > > > + * Quirk: certain CPUs dont like it if just 1 event is left: > > > + */ > > > + if (unlikely(left < 2)) > > > + left = 2; > > > + > > > + static_call_cond(x86_pmu_limit_period)(event, &left); > > > + } > > > > > > this_cpu_write(pmc_prev_left[idx], left); > > > > > > > Nice one. I am curious how you tested this one? I would like to > > reproduce that one on my side. > > The check_emulated_instr() sub-test in KVM-Unit-Tests's x86/pmu.c fails when run > with "my" (which is really yours) fix for the KVM's handling of emulated PMC > events[*]. If KVM synthesizes an "instructions retired" event that bumps the > PMC to all ones, i.e. -1 for all intents and purposes, the test fails because > KVM creates a sample_period of '1', but perf programs a period of '2'. > > I suspect a very simple test of writing -1 to a PMC from the guest would exhibit > the same behavior. > > [*] https://lkml.kernel.org/r/ZUWAg3WP2XESCAR4%40google.com Nice, I will try that and see if I can reproduce. Will give Reviewed-by after testing it on my side. Thanks. -Mingwei ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events 2023-11-07 18:36 [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events Sean Christopherson 2023-11-07 19:38 ` Mingwei Zhang @ 2023-11-17 10:32 ` Peter Zijlstra 2023-11-29 1:33 ` Sean Christopherson 1 sibling, 1 reply; 8+ messages in thread From: Peter Zijlstra @ 2023-11-17 10:32 UTC (permalink / raw) To: Sean Christopherson Cc: Ingo Molnar, Arnaldo Carvalho de Melo, Thomas Gleixner, Borislav Petkov, Dave Hansen, x86, linux-perf-users, linux-kernel, Like Xu, Jim Mattson, Mingwei Zhang On Tue, Nov 07, 2023 at 10:36:05AM -0800, Sean Christopherson wrote: > Don't apply minimum period workarounds/requirements to events that are > being created by KVM to virtualize PMCs for guests, i.e. skip limit > enforcement for events that exclude the host. Perf's somewhat arbitrary > limits prevents KVM from correctly virtualizing counter overflow, e.g. if > the guest sets a counter to have an effective period of '1', forcing a > minimum period of '2' results in overflow occurring at the incorrect time. > > Whether or not a "real" profiling use case is affected is debatable, but > the incorrect behavior is trivially easy to observe and reproduce, and is > deterministic enough to make the PMU appear to be broken from the guest's > perspective. > > Furthermore, the "period" set by KVM isn't actually a period, as KVM won't > automatically reprogram the event with the same period on overflow. KVM > will synthesize a PMI into the guest when appropriate, but what the guest > does in response to the PMI is purely a guest decision. In other words, > KVM effectively operates in a one-shot mode, not a periodic mode. > > Letting KVM and/or the guest program "too small" periods is safe for the > host, as events that exclude the host are atomically disabled with respect > to VM-Exit, i.e. are guaranteed to stop counting upon transitioning to the > host. And whether or not *explicitly* programming a short period is safe > is somewhat of a moot point, as transitions to/from the guest effectively > yield the same effect, e.g. an unrelated VM-Exit => VM-Enter transition > will re-enable guest PMCs with whatever count happened to be in the PMC at > the time of VM-Exit. > > Cc: Like Xu <likexu@tencent.com> > Cc: Jim Mattson <jmattson@google.com> > Cc: Mingwei Zhang <mizhang@google.com> > Signed-off-by: Sean Christopherson <seanjc@google.com> > --- > > Disclaimer: I've only tested this from KVM's side of things. > > arch/x86/events/core.c | 21 +++++++++++++++------ > 1 file changed, 15 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c > index 40ad1425ffa2..f8a8a4ea4d47 100644 > --- a/arch/x86/events/core.c > +++ b/arch/x86/events/core.c > @@ -1388,16 +1388,25 @@ int x86_perf_event_set_period(struct perf_event *event) > hwc->last_period = period; > ret = 1; > } > - /* > - * Quirk: certain CPUs dont like it if just 1 hw_event is left: > - */ > - if (unlikely(left < 2)) > - left = 2; > > if (left > x86_pmu.max_period) > left = x86_pmu.max_period; > > - static_call_cond(x86_pmu_limit_period)(event, &left); > + /* > + * Exempt KVM guest events from the minimum period requirements. It's > + * the guest's responsibility to ensure it can make forward progress, > + * and it's KVM's responsibility to configure an appropriate "period" > + * to correctly virtualize overflow for the guest's PMCs. > + */ > + if (!event->attr.exclude_host) { > + /* > + * Quirk: certain CPUs dont like it if just 1 event is left: > + */ > + if (unlikely(left < 2)) > + left = 2; > + > + static_call_cond(x86_pmu_limit_period)(event, &left); > + } Hmm, IIRC we can disable that left < 2 thing for anything that doesn't have x86_pmu.pebs_no_isolation IIRC. I'm not sure about taking out the limit_period call, why does it make sense to allow the guest to program obviously invalid settings? That is, would something like the below work for you? --- diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 40ad1425ffa2..5543a0bab1f8 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -152,6 +152,14 @@ u64 x86_perf_event_update(struct perf_event *event) return new_raw_count; } +static void x86_perf_limit_period(struct perf_event *event, s64 *left) +{ + /* + * Quirk: certain CPUs dont like it if just 1 hw_event is left: + */ + *left = max(*left, 2); +} + /* * Find and validate any extra registers to set up. */ @@ -1388,11 +1396,6 @@ int x86_perf_event_set_period(struct perf_event *event) hwc->last_period = period; ret = 1; } - /* - * Quirk: certain CPUs dont like it if just 1 hw_event is left: - */ - if (unlikely(left < 2)) - left = 2; if (left > x86_pmu.max_period) left = x86_pmu.max_period; @@ -2130,6 +2133,10 @@ static int __init init_hw_perf_events(void) if (!x86_pmu.update) x86_pmu.update = x86_perf_event_update; + // XXX check non-Intel + if (!x86_pmu.limit_period && x86_pmu.pebs_no_isolation) + x86_pmu.limit_update = x86_perf_limit_period; + x86_pmu_static_call_update(); /* diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index a08f794a0e79..9fe0f241779e 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -4471,7 +4471,10 @@ static void bdw_limit_period(struct perf_event *event, s64 *left) if (*left < 128) *left = 128; *left &= ~0x3fULL; + return; } + if (unlikely(x86_pmu.pebs_no_isolation)) + *left = max(*left, 2); } static void nhm_limit_period(struct perf_event *event, s64 *left) ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events 2023-11-17 10:32 ` Peter Zijlstra @ 2023-11-29 1:33 ` Sean Christopherson 2023-11-29 11:20 ` Peter Zijlstra 0 siblings, 1 reply; 8+ messages in thread From: Sean Christopherson @ 2023-11-29 1:33 UTC (permalink / raw) To: Peter Zijlstra Cc: Ingo Molnar, Arnaldo Carvalho de Melo, Thomas Gleixner, Borislav Petkov, Dave Hansen, x86, linux-perf-users, linux-kernel, Like Xu, Jim Mattson, Mingwei Zhang On Fri, Nov 17, 2023, Peter Zijlstra wrote: > On Tue, Nov 07, 2023 at 10:36:05AM -0800, Sean Christopherson wrote: > > diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c > > index 40ad1425ffa2..f8a8a4ea4d47 100644 > > --- a/arch/x86/events/core.c > > +++ b/arch/x86/events/core.c > > @@ -1388,16 +1388,25 @@ int x86_perf_event_set_period(struct perf_event *event) > > hwc->last_period = period; > > ret = 1; > > } > > - /* > > - * Quirk: certain CPUs dont like it if just 1 hw_event is left: > > - */ > > - if (unlikely(left < 2)) > > - left = 2; > > > > if (left > x86_pmu.max_period) > > left = x86_pmu.max_period; > > > > - static_call_cond(x86_pmu_limit_period)(event, &left); > > + /* > > + * Exempt KVM guest events from the minimum period requirements. It's > > + * the guest's responsibility to ensure it can make forward progress, > > + * and it's KVM's responsibility to configure an appropriate "period" > > + * to correctly virtualize overflow for the guest's PMCs. > > + */ > > + if (!event->attr.exclude_host) { > > + /* > > + * Quirk: certain CPUs dont like it if just 1 event is left: > > + */ > > + if (unlikely(left < 2)) > > + left = 2; > > + > > + static_call_cond(x86_pmu_limit_period)(event, &left); > > + } > > Hmm, IIRC we can disable that left < 2 thing for anything that doesn't > have x86_pmu.pebs_no_isolation IIRC. > > I'm not sure about taking out the limit_period call, why does it make > sense to allow the guest to program obviously invalid settings? I don't see how the guest behavior is obviously invalid. Architecturally, writing -1 to a counter should result in overflow after a single event. Underlying uarch goofiness shouldn't enter into that equation. Honoring the guest's programming *might* cause oddness for the guest, whereas not honoring the architecture is guaranteed to cause visible issues. If programming a "period" of 1 puts the host at risk in some way, then I agree that this is unsafe and we need a different solution. But if the worst case scenario is non-determinstic or odd behavior from the guest's perspective, then that's the guest's problem (with the caveat that the guest might not have accurate Family/Model/Stepping data to make informed decisions). > That is, would something like the below work for you? No, because the fix ideally wouldn't require fancy hardware, i.e. would work for all CPUs for which KVM supports a virtual PMU. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events 2023-11-29 1:33 ` Sean Christopherson @ 2023-11-29 11:20 ` Peter Zijlstra 2026-11-25 23:23 ` Yosry Ahmed 0 siblings, 1 reply; 8+ messages in thread From: Peter Zijlstra @ 2023-11-29 11:20 UTC (permalink / raw) To: Sean Christopherson Cc: Ingo Molnar, Arnaldo Carvalho de Melo, Thomas Gleixner, Borislav Petkov, Dave Hansen, x86, linux-perf-users, linux-kernel, Like Xu, Jim Mattson, Mingwei Zhang On Tue, Nov 28, 2023 at 05:33:16PM -0800, Sean Christopherson wrote: > If programming a "period" of 1 puts the host at risk in some way, then I agree > that this is unsafe and we need a different solution. IIRC if you put in -1 on a Nehalem, you end up with an NMI-storm which wasn't trivial to recover from if at all (it's too long ago and I don't have ancient hardware like that anymore :/) > But if the worst case > scenario is non-determinstic or odd behavior from the guest's perspective, then > that's the guest's problem (with the caveat that the guest might not have accurate > Family/Model/Stepping data to make informed decisions). Things like bdm_limit_period() will cause odd behaviour IIRC, it does daft things like generate extra PEBS records on overflow and gives otherwise daft results for PDIR. glc_limit_period() lacks a useful comment :/ ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events 2023-11-29 11:20 ` Peter Zijlstra @ 2026-11-25 23:23 ` Yosry Ahmed 0 siblings, 0 replies; 8+ messages in thread From: Yosry Ahmed @ 2026-11-25 23:23 UTC (permalink / raw) To: Peter Zijlstra Cc: Sean Christopherson, Ingo Molnar, Arnaldo Carvalho de Melo, Thomas Gleixner, Borislav Petkov, Dave Hansen, x86, linux-perf-users, linux-kernel, Like Xu, Jim Mattson, Mingwei Zhang On Wed, Nov 29, 2023 at 12:20:19PM +0100, Peter Zijlstra wrote: > On Tue, Nov 28, 2023 at 05:33:16PM -0800, Sean Christopherson wrote: > > > If programming a "period" of 1 puts the host at risk in some way, then I agree > > that this is unsafe and we need a different solution. > > IIRC if you put in -1 on a Nehalem, you end up with an NMI-storm which > wasn't trivial to recover from if at all (it's too long ago and I don't > have ancient hardware like that anymore :/) > > > But if the worst case > > scenario is non-determinstic or odd behavior from the guest's perspective, then > > that's the guest's problem (with the caveat that the guest might not have accurate > > Family/Model/Stepping data to make informed decisions). > > Things like bdm_limit_period() will cause odd behaviour IIRC, it does > daft things like generate extra PEBS records on overflow and gives > otherwise daft results for PDIR. > > glc_limit_period() lacks a useful comment :/ Sorry for thread necromancy, but it seems like the discussion stopped here and I'd like to get this fixed as I am observing the same failures that Sean did. Basically, if KVM needs to program period=1 (guest counter is -1), perf overwrites it to 2. After 1 event, the guest counter should overflow, but the hardware counter doesn't, so there is no PMI. On the other hand, KVM relies on the hardware PMIs to signal overflows while perf events are counting. The overflow is then lost: when KVM pauses the event (e.g. to account for an emulated instruction), it folds the perf count into its software counter and restarts the event with a new period, so the late hardware overflow never comes, and KVM only checks for wraps on its own emulated counts. Sean's fix makes the problem go away on all CPU platforms, and Peter's makes it go away on Skylake+ on Intel (with updated microcode) and all AMD platforms. So I am mostly happy with both. It's worth noting that the blast radius is smaller with Sean's fix as it only affects guest events. A middle ground would be skipping the left < 2 clamp on exclude_host events, but not the x86_pmu_limit_period() callback, which makes the problem go away on slightly more CPUs than Peter's patch (I think Sandy/Ivy Bridge, and Skylake on older microcode versions). This keeps the *_limit_period() subtleties in place. All three versions seem to make the problem go away on AMD Turin. WDYT? An alternative to all of this is a "fix" on the KVM side where KVM is always responsible for signaling all PMC overflows, instead of signaling some of them from the PMI code path. KVM would keep track of the guest counter value as of the last pause or write, and on PMI it would just pause the event, manually check for the counter wrapping, and deliver the PMI to the guest. This would obviously be a more complex change. ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-25 23:23 UTC | newest] Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2023-11-07 18:36 [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events Sean Christopherson 2023-11-07 19:38 ` Mingwei Zhang 2023-11-07 23:02 ` Sean Christopherson 2023-11-07 23:47 ` Mingwei Zhang 2023-11-17 10:32 ` Peter Zijlstra 2023-11-29 1:33 ` Sean Christopherson 2023-11-29 11:20 ` Peter Zijlstra 2026-11-25 23:23 ` Yosry Ahmed
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®