From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796371125; b=ZwbhbYf9U3SQjjeqv+tbCNOoXOqqyZwprys6fD9I6foaWMGfRAtn+lgGvfF8PGQbgI5F limhLU095wHyLi8KYtaaaEjENDqTb7hwUni0F8wFQ7LQ3glZajmEb2vgDamAOZF8gYSTe H9Zde8f4PB7EVn+LvZBOEXY8yewJy4XojrHd7HybViMt6MZPYKtWQjJV4Ytg0Uv82TPAS y0QPDPp9XwK4jzjgC0NcKwVBwP4caJfCAFRLNJlrkfdQ+sjzyV3Oz0TKJpV9lWI5Uu5KR ToNVzEEEBxFN0PnLHvW4klT9HtjhtL9In5K+FvfZ+jjcEdkAGAn9SgHBoxVzS2/BEmQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796371125; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=MDJV8PV5S5cVLVIKqT+zMVcjsX0fKDTYGkw5QyysMMk=; b=Z16OrwZFK0VudN6xqu3KfUWr34rGVR/7j5vuAx8Fnzhno2i6jQSQcrSYmlmSWZi44A3n yelo8IB95DgT6xEdCAn4DkuIpjVJvWS4HPpUR1CGYAMoeUL/hpCC+8uvFp3djeinncSQ7 uqJeGIQe1AgwtOSUOcXZLptsDtDY6ZIkq3mZXYJICgYlAf6Gm7C1qLIIMpDlQWx0F9XuE GaqJlHdfxGsGay/36/mdmPI+DBC0RpDI1330u9JRt+ASolQk7vVnMdFPtuTZ4dhfZZTLu e/YQmInWVCTbGmdjrrXitvsiv4mDIJXeVfNg2DxnlmamtubKRjTt1WbCh5Uy9OAs2bg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=S1V8e7Lh; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E5D2B606A322 for ; Fri, 4 Dec 2026 07:58:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=S1V8e7Lh Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7622D1F000CE; Fri, 4 Dec 2026 07:58:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796371124; bh=MDJV8PV5S5cVLVIKqT+zMVcjsX0fKDTYGkw5QyysMMk=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=S1V8e7LhDZooSlTNzGjgQVWfOr4rY0NSYj4DrdAiyU6n9PMMoq41gnjUG4jvgsvo9 Wr86TB19HaQ+0eIkiGL39gJcyNBb8ha0Wxr0CTmjc2ruXfh46JZ2XBTGWSfIej66pH INiM6qrSKZ8m09x7kBkE5iiAuGTKsJu895PRoQmshaDLmavtHUqSDD0PJYkHhF96TO Vck1ubmEq3zE1liFGFPlnmsZ5eS1gBUUcUK5uMMgde27F9FBnjlegB09OVLLEhJRS6 5VeKJ/sCYbdvSTh4rgOuhQEZG80Fum0ukE6qBIPlDLcJOak3jVXIOkwBBH7oVX/L28 Pi4mqV3MWACQw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] perf/core: Subtract accumulated guest time after guest exit Reply-To: sashiko-reviews@lists.linux.dev To: "Qing Wang" Cc: linux-perf-users@vger.kernel.org In-Reply-To: <20261204073206.3395313-1-wangqing7171@gmail.com> References: <20261204073206.3395313-1-wangqing7171@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 07:58:44 +0000 X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 pe= rf_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) :=3D > @@ -895,7 +897,11 @@ static inline u64 __perf_event_time_ctx_now(struct p= erf_event *event, > return READ_ONCE(times[T_TOTAL].offset) - READ_ONCE(times[T_GUEST].off= set); > } > =20 > - return now + READ_ONCE(times[T_TOTAL].offset); > + time =3D now + READ_ONCE(times[T_TOTAL].offset); > + if (event->attr.exclude_guest) > + time -=3D 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 =3D perf_clock() perf_put_guest_context() ... update_perf_time_ctx() time->time +=3D ... __perf_event_time_ctx_now() READ_ONCE(times[T_GUEST].time) <-- data race / newer guest time __perf_update_times() delta =3D 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). --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204073206.3395= 313-1-wangqing7171@gmail.com?part=3D1