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=1796299309; b=sjt/2e48INVTMihth8eukqYUPuCF3/OGpEEwzDTdUHpBEeIOk5mTQOQz+aahktXxYhJh PsR3GRdCcXRjgTr+BeAEcBTVbUocTS4T+FJynkM07m9yDlbLDqlu1IqTaa+L/NI9qSSt9 7YlJqFeZp+oEajata2oFXL9+wD4+5ZHZRuFtVYAJziQlg0cmGX6ydTnl21vylTlGiumMU VO3fluTUYGCuoQ4uMk+Gbe231VK+fXHh4VRk1YmJLStC3teNQSS5dh/U5ME8wcgfTvn7b KZ1hveYV87M8+iZMuWat6Sd0BIx6ETMPha631bZNQnOGTb2DBs+Ff24aava7WlEp9yg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796299309; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=rzdOTriQCru9J9K67pGopGGNYBjZWgAGG1+itedP26w=; b=b3CYoBIjThi8TIjuLvlQCeh0p2jX2ruZ/mNjx/fUnwJjKpi/WnQ8fa9ZAtABYP+9LhKg 7HBp8czhofRqvLz31qBl0SgWU39Q9gt26DTM0VQCQO5m15XC8allHNTK5OjpQzoLYCadJ 7h8GSPnJ/co48zn5XLxzSWjjAyb2JVMOhfdxi/t0gDoLEA3ukW2m5hQEkOSvrOsPgG/pL BdYRQrHYGSZsoftEH14fEl9/IpZna+GUp8AeMYHNucfAZiQjMqGdYbvLI1wSnLlwNxqP+ 869CLfkMQPCJPDn0bhCzR5fXvFZaCujvH7vUiSb6VYJZteFEYAdPhVvDsmN47TtKV+A== 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=AUkgXepf; 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 9839D606A322 for ; Thu, 3 Dec 2026 12:01:49 +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=AUkgXepf Received: by smtp.kernel.org (Postfix) with ESMTPSA id 253FB1F000C1; Thu, 3 Dec 2026 12:01:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796299309; bh=rzdOTriQCru9J9K67pGopGGNYBjZWgAGG1+itedP26w=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=AUkgXepfjDWlH84evlmuLapKc9oP2mDljFhP8q4pUj1kG2lGDwRBV8KRXRHiTCirj 6vR4zb89SGslut8BoJv/BEXpQTlJSwRkWM9Y0xV2L+28uDcp0rXJs9HqEcX9UMm/Hi Ni5LfzhKiSpPriD5OitKMM4pH+jEs6tJu8gLG8bR0GFiyc3QOQlkfJsrZpRxv+Ze7d xPIsixu4qN/VXM21cQ/VL3tGr7MV/jOtwUhy+SmvLz1vi2IG81Ez8v8/fS4u3Vj7/y +WFIoSi6xVB3MQ2EjYX4sR8fowauGf670wzaWJsIrPPjDA/G1dhyeTQ0B9jW67x99Q sRmRYvCmnzMaQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v4 1/4] perf/core: Do not account overflows of a throttled event Reply-To: sashiko-reviews@lists.linux.dev To: "Oancea Catalin" Cc: linux-perf-users@vger.kernel.org In-Reply-To: <20261203113922.77972-2-john3@mailbox.org> References: <20261203113922.77972-1-john3@mailbox.org> <20261203113922.77972-2-john3@mailbox.org> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 12:01:48 +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 2 potential issues to consider: - [Medium] perf/core: NMI TOCTOU in __perf_event_account_interrupt() clobbers throttle - [Medium] perf/core: 32-bit torn u64 hwc->interrupts store in perf_event_throttle() -- commit 6a3f8cdc41d352bcd4c6bff16e1484d89e2f36cd Author: Oancea Catalin perf/core: Do not account overflows of a throttled event This commit returns 1 early from __perf_event_account_interrupt() when hwc->interrupts is already MAX_INTERRUPTS, so overflows on a throttled event do not overwrite the throttle mark. > diff --git a/kernel/events/core.c b/kernel/events/core.c > index 4ab3919ac78e9..4b47883e82af7 100644 > --- a/kernel/events/core.c > +++ b/kernel/events/core.c > @@ -10773,6 +10773,15 @@ __perf_event_account_interrupt(struct perf_event= *event, int throttle, > int ret =3D 0; > u64 seq; > =20 > + /* > + * A throttled event can still report an overflow: from an NMI before > + * perf_event_throttle() has stopped it, or one that was pending when > + * it was stopped. Do not account it, that would overwrite the mark > + * which the tick needs on the group leader to unthrottle the group. > + */ > + if (unlikely(hwc->interrupts =3D=3D MAX_INTERRUPTS)) > + return 1; > + > seq =3D __this_cpu_read(perf_throttled_seq); > if (seq !=3D hwc->interrupts_seq) { > hwc->interrupts_seq =3D seq; [Severity: Medium] Can an NMI still clobber MAX_INTERRUPTS between this check and the subsequent write to hwc->interrupts? In a mixed group with a software event as group_leader and a hardware sibling (supported by __do_sys_perf_event_open()), the software leader runs __perf_event_account_interrupt() with NMIs enabled: __perf_event_account_interrupt(sw_leader) if (hwc->interrupts =3D=3D MAX_INTERRUPTS) /* false */ perf_event_throttle_group(hw_sibling) perf_event_throttle(sw_leader) sw_leader->hw.interrupts =3D MAX_INTERRUPTS sw_leader->pmu->stop(sw_leader, 0) hwc->interrupts++ <-- clobbers MAX_INTERRUPTS On NMI return, hwc->interrupts is overwritten (with 1 if seq changed, or wrapped on increment), so perf_adjust_freq_unthr_events() never sees MAX_INTERRUPTS on the leader and the stopped group stays silent. Updating hwc->interrupts with local_t or local64_t try_cmpxchg() would abort the update and return 1 if an NMI sets MAX_INTERRUPTS in between. [Severity: Medium] On 32-bit architectures, will a torn 64-bit store of MAX_INTERRUPTS in perf_event_throttle() bypass this check? hwc->interrupts is a plain u64 and MAX_INTERRUPTS is ~0ULL, so the store in perf_event_throttle() compiles to two 32-bit writes on 32-bit kernels such as x86-32. If the hardware leader's NMI arrives between them: perf_event_throttle(hw_leader) write low 32 bits of hw.interrupts =3D 0xffffffff __perf_event_account_interrupt(hw_leader) hwc->interrupts =3D=3D MAX_INTERRUPTS /* false, torn value */ hwc->interrupts =3D 1 write high 32 bits of hw.interrupts =3D 0xffffffff <-- 0xffffffff00000001 hw_leader->pmu->stop(hw_leader, 0) The NMI misses the MAX_INTERRUPTS check and resets hwc->interrupts, and the second store leaves leader->hw.interrupts corrupted so the tick never unthrottles the group. If seq matches, the torn count exceeds max_samples_per_tick and re-enters perf_event_throttle_group() from NMI. Since max_samples_per_tick is an int, using a word-sized local_t (or local64_t) for hwc->interrupts would prevent 32-bit tearing across NMIs. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203113922.7797= 2-1-john3@mailbox.org?part=3D1