public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: kan.liang@linux.intel.com
To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org,
	namhyung@kernel.org, irogers@google.com,
	linux-kernel@vger.kernel.org
Cc: Kan Liang <kan.liang@linux.intel.com>
Subject: [PATCH V2 5/5] perf/x86/intel/ds: Fix counter backwards of non-precise events counters-snapshotting
Date: Thu, 24 Apr 2025 06:47:18 -0700	[thread overview]
Message-ID: <20250424134718.311934-6-kan.liang@linux.intel.com> (raw)
In-Reply-To: <20250424134718.311934-1-kan.liang@linux.intel.com>

From: Kan Liang <kan.liang@linux.intel.com>

The counter backwards may be observed in the PMI handler when
counters-snapshotting some non-precise events in the freq mode.

For the non-precise events, it's possible the counters-snapshotting
records a positive value for an overflowed PEBS event. Then the HW
auto-reload mechanism reset the counter to 0 immediately. Because the
pebs_event_reset is cleared in the freq mode, which doesn't set the
PERF_X86_EVENT_AUTO_RELOAD.
In the PMI handler, 0 will be read rather than the positive value
recorded in the counters-snapshotting record.

The counters-snapshotting case has to be specially handled. Since the
event value has been updated when processing the counters-snapshotting
record, only needs to set the new period for the counter via
x86_pmu_set_period().

Fixes: e02e9b0374c3 ("perf/x86/intel: Support PEBS counters snapshotting")
Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
---
 arch/x86/events/intel/ds.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c
index 486881fe162e..83ffbfdf4982 100644
--- a/arch/x86/events/intel/ds.c
+++ b/arch/x86/events/intel/ds.c
@@ -2376,8 +2376,25 @@ __intel_pmu_pebs_last_event(struct perf_event *event,
 			 */
 			intel_pmu_save_and_restart_reload(event, count);
 		}
-	} else
-		intel_pmu_save_and_restart(event);
+	} else {
+		/*
+		 * For a non-precise event, it's possible the
+		 * counters-snapshotting records a positive value for the
+		 * overflowed event. Then the HW auto-reload mechanism
+		 * reset the counter to 0 immediately, because the
+		 * pebs_event_reset is cleared if the PERF_X86_EVENT_AUTO_RELOAD
+		 * is not set. The counter backwards may be observed in a
+		 * PMI handler.
+		 *
+		 * Since the event value has been updated when processing the
+		 * counters-snapshotting record, only needs to set the new
+		 * period for the counter.
+		 */
+		if (is_pebs_counter_event_group(event))
+			static_call(x86_pmu_set_period)(event);
+		else
+			intel_pmu_save_and_restart(event);
+	}
 }
 
 static __always_inline void
-- 
2.38.1


  parent reply	other threads:[~2025-04-24 13:47 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-24 13:47 [PATCH V2 0/5] Several fixes for group flag and counters-snapshotting kan.liang
2025-04-24 13:47 ` [PATCH V2 1/5] perf/x86/intel: Only check the group flag for X86 leader kan.liang
2025-04-30 11:58   ` [tip: perf/core] " tip-bot2 for Kan Liang
2025-04-24 13:47 ` [PATCH V2 2/5] perf/x86/intel: Check the X86 leader for pebs_counter_event_group kan.liang
2025-04-30 11:58   ` [tip: perf/core] " tip-bot2 for Kan Liang
2025-04-24 13:47 ` [PATCH V2 3/5] perf/x86/intel: Check the X86 leader for ACR group kan.liang
2025-04-30 11:58   ` [tip: perf/core] " tip-bot2 for Kan Liang
2025-04-24 13:47 ` [PATCH V2 4/5] perf/x86: Optimize the is_x86_event kan.liang
2025-04-30 11:58   ` [tip: perf/core] " tip-bot2 for Kan Liang
2025-04-24 13:47 ` kan.liang [this message]
2025-04-30 11:58   ` [tip: perf/core] perf/x86/intel/ds: Fix counter backwards of non-precise events counters-snapshotting tip-bot2 for Kan Liang
2025-04-24 14:25 ` [PATCH V2 0/5] Several fixes for group flag and counters-snapshotting Peter Zijlstra
2025-04-24 16:00   ` Liang, Kan
2025-04-25 12:55     ` Peter Zijlstra
2025-04-25 13:54       ` Liang, Kan

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=20250424134718.311934-6-kan.liang@linux.intel.com \
    --to=kan.liang@linux.intel.com \
    --cc=acme@kernel.org \
    --cc=irogers@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.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