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=1796326135; b=yybdaElu5qHD+2AGbz9OIBalz3mKCPK6BfG0VFOzJnIaUqzTb4EfEzYRtGgkDWMaecEG Xkmyw8bvTQm/9dJFlJqkDngGp1Bz/sWL+ULNZxNGeMcnbAVImss516YFwKPsszRv4uAP7 7HO2L7L06HQveN9bnLIKweMUSggvAXpx74iDfujxdFOyW+ZDw2RR4+yQm8SFAscX0sldi VSb4R27uaJFVQiTw9SVPvnR23ULEmVbC6VmAdoOKEjImURprArQxPoi8K/UR8NYrr3NDh rvXpOmReTFNiNJD7+cJbKbi2zXRGynkb6lnk/uX6Gb57dusHLTcvjhSGC1bwUnu7yzA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796326135; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=DMyfa641t/NjnuNO4fyiNLRl/u93IMZ8O5hTAD+78A8=; b=Y82Gwi36eX6ruLx1FVw0BNaDhRD8o2PyT8NNyZxkqMwarYOmwAj9BFjCVrYd0fv+5VXV xY2iNqpmFFGh7Echq0l9hRB6JvZ5Vl18Su6Vgwwfy/JnFukUClPjDqtGPmBn4Vo+KED0l sitgF0+izF3WTr2e8CsxR688Q8rW8eA6WEU5nuF2cMI6hujvRGDGtphN8g8tMQOn+kVbD TXp9nV8CtbxDLaW/U+qDVLEHhrM1uA5URgy/nlwqH5YsEjOHUuF3a57SE9MVbrUevr5Qv X0Uaz/Tw2VieN25o4D5BRuOFZXAhgjhSS1CqDt8iyQN/ILXvY2pGKsv5qgdHwL6wRaA== 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=Z6gNXsia; 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 10B13606A682; Thu, 3 Dec 2026 19:28:55 +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=Z6gNXsia Received: by smtp.kernel.org (Postfix) with ESMTPSA id CB7AD1F000CE; Thu, 3 Dec 2026 19:28:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796326134; bh=DMyfa641t/NjnuNO4fyiNLRl/u93IMZ8O5hTAD+78A8=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=Z6gNXsialYol0aBFrn9OUambj5AI+qME431E46yiMvdOdtXOalReTVeI6BzLwzve6 xeNZuZqUFdCkB8zZV9W+Ja4mzQr6iV7exX88fZVmHSCmVY+0jQdITv1r2ppGFiS3gz R9bBHBA7eMbd/+Z/fM8aGYo7nqtOMbZBQyiqlLF4LRyaU5A7s98DXMN0iNyfRSGt3Y KPgshNU+2qAwfDRTNUlu/jvpHV5VWmIYBpvujwQAntV2IIWmjfBbG8cLWFPlJCtZL6 kE7SEsooK+/9810rhits9klaNhkxBJU1UQWBj0M2dP+9cp6qiXhbzDm6CCaS72MEfJ k/oBRknRt944w== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v5 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time Reply-To: sashiko-reviews@lists.linux.dev To: "Ian Rogers" Cc: linux-perf-users@vger.kernel.org, bpf@vger.kernel.org In-Reply-To: <6e0c4ab555fda856605bd818854a6ef8ced4fcbd.1796323635.git.irogers@google.com> References: <6e0c4ab555fda856605bd818854a6ef8ced4fcbd.1796323635.git.irogers@google.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 19:28:54 +0000 X-Mailing-List: bpf@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 3 potential issues to consider: - [Medium] perf/core: O(N) INACTIVE scan in perf_adjust_freq_unthr_events() - [Medium] perf/core: unguarded perf_event_update_userpage() in event_sched_out() - [Medium] perf/core: NMI-unsafe perf_event_update_userpage() in __perf_event_enable() -- commit dd98e51c34a35ea87d43dcd67b2004fe170ac7ce Author: Ian Rogers perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time This commit introduces PERF_EVENT_STATE_ON for enabled events that are filtered out during sched_in, reserving PERF_EVENT_STATE_INACTIVE for unscheduled flexible groups that could not fit on the PMU so enabled time only accrues in INACTIVE and ACTIVE states. > diff --git a/kernel/events/core.c b/kernel/events/core.c > index 614f9f0e65005..ba04f8d878267 100644 > --- a/kernel/events/core.c > +++ b/kernel/events/core.c [ ... ] > @@ -2505,16 +2509,23 @@ event_sched_out(struct perf_event *event, struct = perf_event_context *ctx) > =20 > perf_event_set_state(event, state); > =20 > - if (!is_software_event(event)) > - cpc->active_oncpu--; > - if (is_event_in_freq_mode(event)) { > - ctx->nr_freq--; > - epc->nr_freq--; > - } > - if (event->attr.exclusive || !cpc->active_oncpu) > - cpc->exclusive =3D 0; > + if (active) { > + if (!is_software_event(event)) > + cpc->active_oncpu--; > + if (is_event_in_freq_mode(event)) { > + ctx->nr_freq--; > + epc->nr_freq--; > + } > + if (event->attr.exclusive || !cpc->active_oncpu) > + cpc->exclusive =3D 0; > =20 > - perf_pmu_enable(event->pmu); > + perf_pmu_enable(event->pmu); > + } else { > + /* > + * active events get this through ->del(). > + */ > + perf_event_update_userpage(event); > + } > } [Severity: Medium] Should this perf_event_update_userpage() call be guarded by refcount_read(&event->mmap_count) like event_set_inactive()? perf_event_update_userpage() only checks event->rb, not mmap_count. For multiplexed events redirected via PERF_EVENT_IOC_SET_OUTPUT (such as in perf record), mmap_count is 0 while event->rb is non-NULL, so every INACTIVE leader and enabled sibling runs perf_clock(), arch_perf_update_userpage(), and seqlock writes under ctx->lock on every rotation or context switch, overwriting the ring buffer owner's rb->user_page. Unmmapped events also pay an out-of-line RCU call and event->rb load per inactive event, and directly mmapped events update the userpage twice in perf_rotate_context() (here on INACTIVE -> ON, then again in group_set_inactive()). [ ... ] > @@ -3280,10 +3290,17 @@ static void __perf_event_enable(struct perf_event= *event, > =20 > /* > * If the event is in a group and isn't the group leader, > - * then don't put it on unless the group is on. > - */ > - if (leader !=3D event && leader->state !=3D PERF_EVENT_STATE_ACTIVE) > + * then don't put it on unless the group is on. If the leader > + * is INACTIVE (waiting for counters), match its state so the > + * sibling accrues enabled time with the group. > + */ > + if (leader !=3D event && leader->state !=3D PERF_EVENT_STATE_ACTIVE) { > + if (leader->state =3D=3D PERF_EVENT_STATE_INACTIVE) { > + perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); > + perf_event_update_userpage(event); > + } > return; > + } [Severity: Medium] Can perf_event_update_userpage() here nest with an NMI on a shared rb->user_page? event_function() holds ctx->lock with local IRQs disabled, but leaves the PMU enabled. If an active sampling event on the same CPU shares event->rb via PERF_EVENT_IOC_SET_OUTPUT and overflows during this call, the nested perf_event_update_userpage() breaks the non-nestable userpg->lock seqlock: __perf_event_enable(sibling) perf_event_update_userpage(sibling) ++userpg->lock (odd) x86_pmu_handle_irq() x86_perf_event_set_period(active_ev) perf_event_update_userpage(active_ev) ++userpg->lock <-- even while writing ... ++userpg->lock (odd) write time_enabled/time_running <-- mixes sibling & active_ev ++userpg->lock (even, torn fields published) Also, without the mmap_count check from event_set_inactive(), a redirected sibling unconditionally overwrites the active event's user_page. Wrapping this in perf_pmu_disable()/perf_pmu_enable() and calling event_set_inactive(event) avoids both. [ ... ] > @@ -4156,29 +4174,38 @@ static int merge_sched_in(struct perf_event *even= t, void *data) [ ... ] > + } else { > + struct perf_cpu_pmu_context *cpc =3D this_cpc(event->pmu_ctx->pmu); > + > + event->pmu_ctx->rotate_necessary =3D 1; > + perf_mux_hrtimer_restart(cpc); > + > + group_set_inactive(event); > + list_add_tail(&event->active_list, > + &event->pmu_ctx->flexible_active); > } [Severity: Medium] Does appending INACTIVE groups to flexible_active turn perf_adjust_freq_unthr_events() into an O(N) scan over all multiplexed groups on every timer tick? perf_event_task_tick() -> perf_adjust_freq_unthr_context() calls perf_adjust_freq_unthr_events(&pmu_ctx->flexible_active) in hardirq context under ctx->lock with the PMU disabled, and its loop continues past non-ACTIVE entries instead of stopping at the INACTIVE tail: kernel/events/core.c:perf_adjust_freq_unthr_events() { ... list_for_each_entry(event, event_list, active_list) { if (event->state !=3D PERF_EVENT_STATE_ACTIVE) continue; ... } In addition, perf_pmu_ctx_is_active() checks !list_empty(&epc->flexible_active), so a pmu_ctx with only INACTIVE groups and no ACTIVE events now invokes perf_pmu_disable()/perf_pmu_enable() and scans the INACTIVE list on unthrottle ticks. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/6e0c4ab555fda856605= bd818854a6ef8ced4fcbd.1796323635.git.irogers@google.com?part=3D1