All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Qing Wang" <wangqing7171@gmail.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH] perf/core: Subtract accumulated guest time after guest exit
Date: Fri, 04 Dec 2026 07:58:44 +0000	[thread overview]
Message-ID: <sashiko-outbox-249352@kernel.org> (raw)
In-Reply-To: <20261204073206.3395313-1-wangqing7171@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] perf/core: data race and u64 underflow in
  __perf_event_time_ctx_now()
--

commit d1ce4efe3703c30bcd75dbb7b1c31f8b0e5ad234
Author: wangqing <wangqing7171@gmail.com>

perf/core: Subtract accumulated guest time after guest exit

This commit updates __perf_event_time_ctx_now() to subtract
times[T_GUEST].time for exclude_guest events when
is_guest_mediated_pmu_loaded() is false.

> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 602b56b884e71..ba35ffa62f346 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -887,6 +887,8 @@ static inline u64 __perf_event_time_ctx_now(struct perf_event *event,
>  					    struct perf_time_ctx *times,
>  					    u64 now)
>  {
> +	u64 time;
> +
>  	if (is_guest_mediated_pmu_loaded() && event->attr.exclude_guest) {
>  		/*
>  		 * (now + times[total].offset) - (now + times[guest].offset) :=
> @@ -895,7 +897,11 @@ static inline u64 __perf_event_time_ctx_now(struct perf_event *event,
>  		return READ_ONCE(times[T_TOTAL].offset) - READ_ONCE(times[T_GUEST].offset);
>  	}
>  
> -	return now + READ_ONCE(times[T_TOTAL].offset);
> +	time = now + READ_ONCE(times[T_TOTAL].offset);
> +	if (event->attr.exclude_guest)
> +		time -= READ_ONCE(times[T_GUEST].time);
> +
> +	return time;
>  }

[Severity: Medium]
Can this cross-CPU read of times[T_GUEST].time race with
update_perf_time_ctx() and underflow event time accounting?

is_guest_mediated_pmu_loaded() only checks the local CPU's
guest_ctx_loaded flag, so another CPU can reach
__perf_event_time_ctx_now() without ctx->lock (for example via
perf_mmap_rb() -> perf_event_update_userpage()) while a guest runs:

CPU0                                    CPU1 (in guest)
perf_mmap()
  perf_mmap_rb()
    perf_event_update_userpage()
      calc_timer_values()
        now = perf_clock()
                                        perf_put_guest_context()
                                          ...
                                          update_perf_time_ctx()
                                            time->time += ...
        __perf_event_time_ctx_now()
          READ_ONCE(times[T_GUEST].time)  <-- data race / newer guest time
        __perf_update_times()
          delta = now - event->tstamp     <-- u64 underflow

Because CPU1 exits the guest and advances times[T_GUEST].time after CPU0
sampled perf_clock(), CPU0 subtracts guest time past its own now value.
__perf_event_time_ctx_now() then returns a time earlier than
event->tstamp, underflowing delta and corrupting userpage times.

In addition, update_perf_time_ctx() updates time->time with a plain
read-modify-write rather than WRITE_ONCE(), so the lockless
READ_ONCE(times[T_GUEST].time) also causes a data race (and potential
64-bit tearing on 32-bit x86).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204073206.3395313-1-wangqing7171@gmail.com?part=1

      reply	other threads:[~2026-12-04  7:58 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  7:32 [PATCH] perf/core: Subtract accumulated guest time after guest exit Qing Wang
2026-12-04  7:58 ` sashiko-bot [this message]

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=sashiko-outbox-249352@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=wangqing7171@gmail.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 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.