Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Yosry Ahmed <yosry@kernel.org>
To: Sean Christopherson <seanjc@google.com>,
	Peter Zijlstra <peterz@infradead.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	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>,
	Kan Liang <kan.liang@linux.intel.com>,
	Mingwei Zhang <mizhang@google.com>,
	Jim Mattson <jmattson@google.com>,
	Zide Chen <zide.chen@intel.com>,
	Dapeng Mi <dapeng1.mi@linux.intel.com>,
	linux-perf-users@vger.kernel.org, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, Yosry Ahmed <yosry@kernel.org>,
	stable@vger.kernel.org
Subject: [RFC PATCH 01/26] perf/core: Fix lockless time read for exclude_guest events
Date: Tue, 24 Nov 2026 00:22:38 +0000	[thread overview]
Message-ID: <20261124002303.1785363-2-yosry@kernel.org> (raw)
In-Reply-To: <20261124002303.1785363-1-yosry@kernel.org>

When a host perf event has exclude_guest=1, its elapsed time should
exclude guest time.  However, lockless time reads through
__perf_event_time_ctx_now() only subtracts guest time while a guest
context is loaded, unlike __perf_event_time_ctx(). As a result,
time_enabled and time_running read locklessly include past guest time
after the guest context is put.

Subtract guest time regardless of whether a guest context is loaded. Guest
time does not advance while no guest context is loaded, so use the
accumulated guest time in that case.

Undo a minor optimization that avoids adding and subtracting 'now' while a
guest context is loaded, to make it clearer that guest time is always
subtracted, and only how it's calculated depends on whether a guest
context is loaded.

Fixes: 4593b4b6e218 ("perf: Add a EVENT_GUEST flag")
Cc: stable@vger.kernel.org
Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
 kernel/events/core.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index f5f2261f1fe6f..6f076e5483cb7 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -850,7 +850,7 @@ static void ctx_sched_in(struct perf_event_context *ctx, struct pmu *pmu, enum e
 static inline void update_perf_time_ctx(struct perf_time_ctx *time, u64 now, bool adv)
 {
 	if (adv)
-		time->time += now - time->stamp;
+		WRITE_ONCE(time->time, time->time + now - time->stamp);
 	time->stamp = now;
 
 	/*
@@ -887,15 +887,19 @@ static inline u64 __perf_event_time_ctx_now(struct perf_event *event,
 					    struct perf_time_ctx *times,
 					    u64 now)
 {
-	if (is_guest_mediated_pmu_loaded() && event->attr.exclude_guest) {
-		/*
-		 * (now + times[total].offset) - (now + times[guest].offset) :=
-		 * times[total].offset - times[guest].offset
-		 */
-		return READ_ONCE(times[T_TOTAL].offset) - READ_ONCE(times[T_GUEST].offset);
+	u64 time = now + READ_ONCE(times[T_TOTAL].offset);
+	u64 guest_time;
+
+	if (event->attr.exclude_guest) {
+		if (is_guest_mediated_pmu_loaded())
+			guest_time = now + READ_ONCE(times[T_GUEST].offset);
+		else
+			guest_time = READ_ONCE(times[T_GUEST].time);
+
+		time -= guest_time;
 	}
 
-	return now + READ_ONCE(times[T_TOTAL].offset);
+	return time;
 }
 
 #ifdef CONFIG_CGROUP_PERF
-- 
2.56.1.809.ga0b61035b5-goog


           reply	other threads:[~2026-11-24  0:23 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <20261124002303.1785363-1-yosry@kernel.org>]

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=20261124002303.1785363-2-yosry@kernel.org \
    --to=yosry@kernel.org \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jmattson@google.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --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=zide.chen@intel.com \
    /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