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=1796196806; b=WYVy1w84+vw3Cni34GK2Gl9Hg9CJvSoQU1IF+Vsaqpl6wDB6gb3IIPMclY5qb1k2V0yN /JafoWiw2SmWDD0mbb0F1kmVB23v9VNWs1GTnJZvVD2hEklagipx9q3QBDxcwyqXaXGbr OAiSQvUSb8gXQH+sgsJEe231hO9UHZw9B0C3btOfmCN1LmRqJwb4XFzSwlbd78GpPV3Pu ykXIG+vAFbLACqkuh5u4jAGCEbb/aej1xHtZV5MAy83uOXyTitXhhxgOJXE3imbXW/nm4 sLGK5dhj1yHUG21ulfSvl0277WyzvkgpZb0pY/kIfaSw9Ti6nRWbBPBQ3L8P68drZ/g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796196806; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=Ze+mQocfFzXVwh+109jpzS7fukHmgXu+JOIbjBkMKwM=; b=AVLJKb3/avMlZaS+zE/Cp2gRCwW4mVpiYWGn59rtJEqZrA/sjPVgmHZmIHcAaR+NlIxw LbfdfAb8m0SFj77vdsDQ/lIYlcc5t8f7FqK8DHKmVGTyz9h9bPgUBvNWnkS2vLKNQlk2C g8N50MXsodCuKl4BrOpnRxjSP9XV7SvhhT102dAQPs8M6K/WgVgJzXiCwdATGAWz7hna3 C6FWZl38rsartgNWhmEZOl5z1iWfzho+awXf2b60dofdqckKr7kHoi8SknWr1cbGqBUnx ytl+VpEXc6hVfBMUwOvKMxhdI/3HRExbLezEqK1byBtlFmwysnNvR+5qN4daBwW+F3A== 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=cdk9tjyF; 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 D2DEF606A322 for ; Wed, 2 Dec 2026 07:33:26 +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=cdk9tjyF Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4495F1F000C1; Wed, 2 Dec 2026 07:33:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796196806; bh=Ze+mQocfFzXVwh+109jpzS7fukHmgXu+JOIbjBkMKwM=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=cdk9tjyFLP7p1qVxFS3ApbqIuB+z+LgcPp1K+UVKGd4sFuoA7LngW5SIYokfsbwSa IY0SLbjffTyjKMPI0dD3lEizh/36usMUKezi+szm9UmfbGNE6rhxVRofE6+mCONZQ6 WWSQDrtHpBsQhKFEYAY0zRLyLQvh/1Cz5BTWjuIr0GLUMyUBzTp3P6mjxgxLrNcLt3 e8MhDCn33M7CPAScHdhxGT2bXw8eqLSJc7TlYIwbudK9RbAU4FYCQeb+us/pHLCszE nksWnn1pXJgOK+I/HwCIIJRT9brAtHhlf3APzphw70TKOXsnSzqoYu5WmK554LleZr WtvXFMXI7OzYw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v14 07/10] perf: RISC-V: add support for SSE event Reply-To: sashiko-reviews@lists.linux.dev To: "Zhanpeng Zhang" Cc: linux-perf-users@vger.kernel.org In-Reply-To: <20261202070003.42491-19-zhangzhanpeng.jasper@bytedance.com> References: <20261202070003.42491-12-zhangzhanpeng.jasper@bytedance.com> <20261202070003.42491-19-zhangzhanpeng.jasper@bytedance.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 07:33:25 +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 10 potential issues to consider: - [High] perf: riscv: unstopped counters in pmu_sbi_ovf_sse_handler() on NULL regs - [High] perf: riscv: unhandled PERF_HES_STOPPED counters in overflow and stop paths - [Medium] perf: riscv: missing sse_failed check in riscv_pm_pmu_notify() - [Medium] perf: riscv: missing sse_event_cleanup() when pmu_sbi_setup_sse() fails - [Medium] perf: riscv: batch counter stop in pmu_sbi_cleanup_sse() skips counters - [Medium] perf: riscv: premature hw_evt->state reset in pmu_sbi_ovf_handler() loop - [Medium] perf: riscv: pmu_sbi_sse_enable() latches sse_failed on hotplug and reboot - [Medium] perf: riscv: missing perf_event_update_userpage() in pmu_sbi_fail_sse() - [Medium] perf: riscv: lost PMU SSE resume state on CPU_PM_ENTER after unmask failure - [Low] perf: riscv: unused pmu_sbi_stop_all_cpu() when CONFIG_RISCV_PMU_SBI_SSE=3Dn -- commit 1709e80c1999104fc7fd5406b9dc4110661b778c Author: Cl=C3=A9ment L=C3=A9ger perf: RISC-V: add support for SSE event This commit adds support for delivering RISC-V SBI PMU counter overflow events through Supervisor Software Events (SSE), including probe-time delivery selection, overflow handling, error latching, and CPU power management integration. Link: https://lore.kernel.org/r/20261002-reach-cloning-f1594729dbe3@wendy/ > diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c > index 21a2078eab98a..a984ac57a7b6e 100644 > --- a/drivers/perf/riscv_pmu_sbi.c > +++ b/drivers/perf/riscv_pmu_sbi.c [ ... ] > @@ -936,14 +938,42 @@ static inline void pmu_sbi_stop_all(struct riscv_pm= u *pmu) > * which may include counters that are not enabled yet. > */ > for (i =3D 0; i < BITS_TO_LONGS(RISCV_MAX_COUNTERS); i++) { > - if (!pmu->cmask[i]) > + if (!ctr_mask[i]) > continue; > sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, > - i * BITS_PER_LONG, pmu->cmask[i], > + i * BITS_PER_LONG, ctr_mask[i], > SBI_PMU_STOP_FLAG_RESET); > } > } > =20 > +static void pmu_sbi_sse_stop_all(const unsigned long *ctr_mask) > +{ > + struct sbiret ret; > + int i; > + > + /* > + * Stop each counter separately: an already-stopped counter must not > + * prevent firmware from stopping a later, overflowing counter. > + */ > + for_each_set_bit(i, ctr_mask, RISCV_MAX_COUNTERS) { > + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, > + i, 1, SBI_PMU_STOP_FLAG_RESET); > + if (ret.error && ret.error !=3D SBI_ERR_ALREADY_STOPPED) > + pr_warn_ratelimited("failed to stop counter %d: %ld\n", > + i, ret.error); > + } > +} > + > +static inline void pmu_sbi_stop_all(struct riscv_pmu *pmu) > +{ > + pmu_sbi_stop_all_mask(pmu->cmask); > +} > + > +static void pmu_sbi_stop_all_cpu(void *info) > +{ > + pmu_sbi_stop_all(info); > +} [Severity: Low] Does pmu_sbi_stop_all_cpu() trigger a -Wunused-function warning when CONFIG_RISCV_PMU_SBI_SSE is disabled, since its only caller (pmu_sbi_cleanup_sse()) is guarded by #ifdef CONFIG_RISCV_PMU_SBI_SSE? [ ... ] > @@ -1054,32 +1098,111 @@ static inline void pmu_sbi_start_ovf_ctrs_snapsh= ot(struct cpu_hw_events *cpu_hw_ [ ... ] > +#ifdef CONFIG_RISCV_PMU_SBI_SSE > +/* > + * A local SSE delivery failure makes the current PMU state unsafe to re= sume. > + * Latch the failure before stopping mapped events so the SSE transition= and > + * overflow restart paths cannot undo the fail-safe while they are quies= ced. > + */ > +static void pmu_sbi_fail_sse(struct riscv_pmu *pmu, const char *op, int = ret) > +{ > + struct cpu_hw_events *cpu_hw_evt =3D this_cpu_ptr(pmu->hw_events); > + struct perf_event *event; > + int idx; > + > + if (READ_ONCE(cpu_hw_evt->sse_failed)) > + return; > + > + WRITE_ONCE(cpu_hw_evt->sse_failed, true); > + pr_err_ratelimited("failed to %s local PMU SSE event: %d; stopping coun= ters\n", > + op, ret); > + > + for (idx =3D 0; idx < RISCV_MAX_COUNTERS; idx++) { > + event =3D cpu_hw_evt->events[idx]; > + if (event) > + riscv_pmu_stop(event, PERF_EF_UPDATE); > + } > +} [Severity: Medium] Should pmu_sbi_fail_sse() also call perf_event_update_userpage(event) after riscv_pmu_stop()? riscv_pmu_stop() clears the counter bit in CSR_SCOUNTEREN via pmu_sbi_ctr_stop() -> pmu_sbi_reset_scounteren() and sets PERF_HES_STOPPED, but does not update the perf mmap userpage. When pmu_sbi_fail_sse() runs after riscv_pmu_start() has already published a non-zero userpg->index (such as in pmu_sbi_sse_enable() at the end of event_sched_in() or on CPU_PM_EXIT), userspace self-monitoring tasks still see userpg->index !=3D 0 and will fault with SIGILL when executing csrr on the disabled counter CSR. > + > +static void pmu_sbi_sse_disable(struct pmu *pmu) > +{ > + struct riscv_pmu *rvpmu =3D to_riscv_pmu(pmu); > + struct cpu_hw_events *cpu_hw_evt =3D this_cpu_ptr(rvpmu->hw_events); > + int ret; > + > + if (!READ_ONCE(rvpmu->sse_active) || > + READ_ONCE(cpu_hw_evt->sse_failed)) > + return; > + > + ret =3D sse_event_disable_local(rvpmu->sse_evt); > + if (unlikely(ret)) > + pmu_sbi_fail_sse(rvpmu, "disable", ret); > +} > + > +static void pmu_sbi_sse_enable(struct pmu *pmu) > +{ > + struct riscv_pmu *rvpmu =3D to_riscv_pmu(pmu); > + struct cpu_hw_events *cpu_hw_evt =3D this_cpu_ptr(rvpmu->hw_events); > + int ret; > + > + if (!READ_ONCE(rvpmu->sse_active) || > + READ_ONCE(cpu_hw_evt->sse_failed)) > + return; > + > + ret =3D sse_event_enable_local(rvpmu->sse_evt); > + if (unlikely(ret)) > + pmu_sbi_fail_sse(rvpmu, "enable", ret); > +} > +#endif [Severity: Medium] Can pmu_sbi_sse_enable() permanently latch cpu_hw_evt->sse_failed during CPU hotplug or system reboot? CPUHP_AP_RISCV_SSE_ONLINE is in the threaded ONLINE section after CPUHP_AP_ONLINE_IDLE, CPUHP_AP_SCHED_WAIT_EMPTY, and CPUHP_AP_SMPBOOT_THREADS. On cpu_down, sse_cpu_teardown() marks the local SSE event unregistered (reg_evt->is_registered =3D false) before per-CPU kthreads are parked and remaining tasks are evacuated; on cpu_up, tasks can schedule before sse_cpu_online() registers the event. If a task with a hardware perf event context-switches on that CPU while reg_evt->is_registered is false, perf_pmu_enable() -> pmu_sbi_sse_enable() -> sse_event_enable_local() returns -EINVAL: drivers/firmware/riscv/riscv_sbi_sse.c:sse_event_enable_local() { ... if (READ_ONCE(sse_shutting_down)) { ret =3D -ESHUTDOWN; goto out; } if (!reg_evt->is_registered) { ret =3D -EINVAL; goto out; } ... } pmu_sbi_sse_enable() treats that -EINVAL as a fatal failure and calls pmu_sbi_fail_sse(), latching cpu_hw_evt->sse_failed =3D true (which is never cleared on subsequent cpu_up) and permanently disabling counters on that CPU. Similarly, on reboot or kexec, sse_reboot_nb (priority 0) sets sse_shutting_down =3D true before perf_reboot_notifier (priority INT_MIN) cleans up perf contexts, causing sse_event_enable_local() to return -ESHUTDOWN and trigger pmu_sbi_fail_sse(). [ ... ] > @@ -1165,6 +1306,14 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, vo= id *dev) > hw_evt->state |=3D PERF_HES_UPTODATE; > perf_sample_data_init(&data, 0, hw_evt->last_period); > if (riscv_pmu_event_set_period(event)) { > + int overflow_ret; > + > + /* Guest attribution and guest stack sampling are not supported yet. = */ > + if (from_guest) { > + hw_evt->state =3D 0; > + continue; > + } > + > /* > * Unlike other ISAs, RISC-V don't have to disable interrupts > * to avoid throttling here. As per the specification, the > @@ -1173,128 +1322,291 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq,= void *dev) > * TODO: We will need to stop the guest counters once > * virtualization support is added. > */ > - perf_event_overflow(event, &data, regs); > + overflow_ret =3D perf_event_overflow(event, &data, regs); > + if (!overflow_ret) > + hw_evt->state =3D 0; > + } else { > + hw_evt->state =3D 0; > } > - /* Reset the state as we are going to start the counter after the loop= */ > - hw_evt->state =3D 0; > } [Severity: High] When a counter is throttled and left with PERF_HES_STOPPED set, what happens on the next overflow of another counter on the same CPU? First, pmu_sbi_stop_hw_ctrs() passes the unfiltered cpu_hw_evt->used_hw_ctrs[i] mask (which still includes the stopped counter) to a batch SBI_EXT_PMU_COUNTER_STOP call. If firmware returns SBI_ERR_ALREADY_STOPPED for that batch, it can abort before stopping higher-indexed overflowing counters, and when SBI PMU snapshot is used, if (!ret.error && sbi_pmu_snapshot_available()) skips saving sdata->ctr_values[] into snapshot_cval_shcopy[] and overwrites sdata->ctr_values[] with zeroes. Second, because the throttled counter was never restarted via SBI_EXT_PMU_COUNTER_START, its overflow bit can remain set in CSR_SCOUNTOVF while its hw_evt->period_left was already reloaded (> 0) by riscv_pmu_event_set_period() before perf_event_overflow() throttled it. On the next overflow of another counter, the loop in pmu_sbi_ovf_handler() does not skip events with PERF_HES_STOPPED set: riscv_pmu_event_update() is a no-op because PERF_HES_UPTODATE is set, riscv_pmu_event_set_period() returns 0 because period_left > 0, and the else branch (or from_guest branch) resets hw_evt->state =3D 0, causing pmu_sbi_start_overflow_mask() to restart the throttled counter. [Severity: Medium] Can resetting hw_evt->state =3D 0 inside the loop before pmu_sbi_start_overflow_mask() corrupt group sibling counts or restart throttled frequency-mode events? If two events in the same perf group overflow together and the first does not throttle (so its hw_evt->state is cleared to 0 after riscv_pmu_event_set_period() updates prev_count) while the second hits max_samples_per_tick, perf_event_throttle_group() calls riscv_pmu_stop() on the first event before pmu_sbi_start_overflow_mask() has reprogrammed its hardware counter. Because hw_evt->state is already 0, riscv_pmu_stop() calls riscv_pmu_event_update() a second time against the new prev_count, adding a bogus delta to event->count. Also, for frequency-mode events (event->attr.freq =3D=3D 1), __perf_event_account_interrupt() calls perf_adjust_period() even after perf_event_throttle_group() sets the return value to 1: kernel/events/core.c:__perf_event_account_interrupt() { ... if (event->hw.interrupts > max_samples_per_tick) { __this_cpu_inc(perf_throttled_count); tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); event->hw.interrupts =3D MAX_INTERRUPTS; perf_log_throttle(event, 0); perf_event_throttle_group(event); ret =3D 1; } if (event->attr.freq && event->hw.sample_period) { u64 now =3D perf_clock(); s64 delta =3D now - event->hw.freq_time_stamp; event->hw.freq_time_stamp =3D now; if (delta > 0 && delta < 2*TICK_NSEC) perf_adjust_period(event, delta, hwc->last_period, true); } return ret; } When period_left > 8 * sample_period, perf_adjust_period() calls event->pmu->start(event, PERF_EF_RELOAD) -> riscv_pmu_start(), which clears hw_evt->state =3D 0 and starts the counter despite perf_event_overflow() returning 1. > =20 > pmu_sbi_start_overflow_mask(pmu, overflowed_ctrs); > + > perf_sample_event_took(sched_clock() - start_clock); > =20 > return IRQ_HANDLED; > } > =20 > -static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *nod= e) > +static irqreturn_t pmu_sbi_ovf_irq_handler(int irq, void *dev) > { > - struct riscv_pmu *pmu =3D hlist_entry_safe(node, struct riscv_pmu, node= ); > - struct cpu_hw_events *cpu_hw_evt =3D this_cpu_ptr(pmu->hw_events); > - > - /* > - * We keep enabling userspace access to CYCLE, TIME and INSTRET via the > - * legacy option but that will be removed in the future. > - */ > - if (sysctl_perf_user_access =3D=3D SYSCTL_LEGACY) > - csr_write(CSR_SCOUNTEREN, 0x7); > - else > - csr_write(CSR_SCOUNTEREN, 0x2); > + return pmu_sbi_ovf_handler(dev, get_irq_regs(), false, false); > +} > =20 > - /* Stop all the counters so that they can be enabled from perf */ > - pmu_sbi_stop_all(pmu); > +#ifdef CONFIG_RISCV_PMU_SBI_SSE > +static int pmu_sbi_ovf_sse_handler(u32 evt, void *arg, struct pt_regs *r= egs) > +{ > + const struct riscv_sse_interrupted_context *context; > + struct riscv_pmu *pmu =3D arg; > + struct cpu_hw_events *hw_event =3D raw_cpu_ptr(pmu->hw_events); > + bool from_guest; > + > + if (unlikely(!READ_ONCE(pmu->sse_active))) { > + pmu_sbi_sse_stop_all(pmu->cmask); > + return -EIO; > + } > =20 > - if (riscv_pmu_use_irq) { > - cpu_hw_evt->irq =3D riscv_pmu_irq; > - ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask); > - enable_percpu_irq(riscv_pmu_irq, IRQ_TYPE_NONE); > + if (unlikely(!regs)) { > + pmu_sbi_fail_sse(pmu, "read interrupted context for", -EIO); > + return -EIO; > } [Severity: High] Does the !regs path in pmu_sbi_ovf_sse_handler() leave the overflowing counter running when no perf event is mapped or when cpu_hw_evt->sse_failed is already true? When SBI_SSE_EVENT_ATTR_READ fails in do_sse(), it calls the handler with regs =3D=3D NULL so the handler can quiesce its event source before SBI_SSE_EVENT_COMPLETE. However, pmu_sbi_fail_sse() only stops non-NULL events in cpu_hw_evt->events[] and returns immediately if cpu_hw_evt->sse_failed is already true. If an overflow arrives with regs =3D=3D NULL before any perf event is installed (or from an unmapped counter after pmu_sbi_setup_sse() sets pmu->sse_active =3D true during probe), or after a failed sse_event_disable_local() has already set cpu_hw_evt->sse_failed =3D true while leaving the SSE event enabled in firmware, pmu_sbi_fail_sse() stops no counters at all. Completing the SSE event with the counter still overflowing will immediately re-deliver the NMI-like SSE in an infinite loop; calling pmu_sbi_sse_stop_all(pmu->cmask) on this error path would quiesce all counter sources. > =20 > - if (sbi_pmu_snapshot_available()) > - return pmu_sbi_snapshot_setup(pmu, cpu); > + context =3D riscv_sse_get_interrupted_context(); > + from_guest =3D context && context->regs =3D=3D regs && > + (context->hstatus & HSTATUS_SPV); > + pmu_sbi_ovf_handler(hw_event, regs, true, from_guest); > =20 > return 0; > } > =20 > -static int pmu_sbi_dying_cpu(unsigned int cpu, struct hlist_node *node) > +static int pmu_sbi_setup_sse(struct riscv_pmu *pmu) > { > - if (riscv_pmu_use_irq) { > - disable_percpu_irq(riscv_pmu_irq); > - } > + int ret; > + struct sse_event *evt; > =20 > - /* Disable all counters access for user mode now */ > - csr_write(CSR_SCOUNTEREN, 0x0); > + evt =3D sse_event_register(SBI_SSE_EVENT_LOCAL_PMU_OVERFLOW, 0, > + pmu_sbi_ovf_sse_handler, pmu); > + if (IS_ERR(evt)) > + return PTR_ERR(evt); > + pmu->sse_evt =3D evt; > =20 > - if (sbi_pmu_snapshot_available()) > - return pmu_sbi_snapshot_disable(); > + ret =3D sse_event_enable(evt); > + if (ret) { > + int cleanup_ret; > =20 > - return 0; > + cleanup_ret =3D sse_event_disable(evt); > + if (cleanup_ret) { > + pr_warn("failed to disable SSE event after setup error: %d\n", > + cleanup_ret); > + return cleanup_ret; > + } > + > + cleanup_ret =3D sse_event_unregister(evt); > + > + if (cleanup_ret) { > + pr_warn("failed to unregister SSE event: %d\n", > + cleanup_ret); > + } else { > + pmu->sse_evt =3D NULL; > + } > + return cleanup_ret ?: ret; > + } [Severity: Medium] If sse_event_enable() fails here and sse_event_disable() or sse_event_unregister() also fails, should the retained evt be handed to sse_event_cleanup()? When pmu_sbi_setup_irqs() returns -EUCLEAN to pmu_sbi_device_probe(), probe continues in counting-only mode and returns 0 without calling riscv_pmu_destroy() -> pmu_sbi_cleanup_sse(). Because sse_event_cleanup(evt) is never called, evt->cleanup_pending remains false, so sse_cpu_online() and sse_restore_local_events() treat evt as an active registration and register it on newly onlined CPUs instead of tearing it down. In addition, if the event remained enabled on any CPU and a counting counter wraps (programmed at half-width by riscv_pmu_event_init()), pmu_sbi_ovf_sse_handler() sees !pmu->sse_active and calls pmu_sbi_sse_stop_all(pmu->cmask) with SBI_PMU_STOP_FLAG_RESET, resetting active counting counters behind perf. > + > + WRITE_ONCE(pmu->sse_active, true); > + pr_info("using SSE for PMU event delivery\n"); > + > + return ret; > } > =20 > -static int pmu_sbi_setup_irqs(struct riscv_pmu *pmu, struct platform_dev= ice *pdev) > +static void pmu_sbi_cleanup_sse(struct riscv_pmu *pmu) > { > + struct sse_event *sse_evt; > int ret; > - struct cpu_hw_events __percpu *hw_events =3D pmu->hw_events; > - struct irq_domain *domain =3D NULL; > =20 > + sse_evt =3D pmu->sse_evt; > + if (!sse_evt) > + return; > + /* > + * Close callback admission before draining each CPU. PMU callbacks run > + * with local interrupts disabled, so the synchronous IPI cannot comple= te > + * until a callback that observed the old state has returned. > + */ > + WRITE_ONCE(pmu->sse_active, false); > + on_each_cpu(pmu_sbi_stop_all_cpu, pmu, 1); [Severity: Medium] Should pmu_sbi_cleanup_sse() stop counters on each CPU via pmu_sbi_sse_stop_all(pmu->cmask) instead of pmu_sbi_stop_all_cpu()? pmu_sbi_stop_all_cpu() calls pmu_sbi_stop_all_mask(pmu->cmask), which issues a batch SBI_EXT_PMU_COUNTER_STOP across all counters in pmu->cmask. As noted for pmu_sbi_sse_stop_all(), a batch stop can abort with SBI_ERR_ALREADY_STOPPED on an already-stopped lower-indexed counter and leave higher-indexed counters running when sse_event_cleanup() replaces the SSE handler with a no-op at retain:. [ ... ] > @@ -1325,9 +1641,59 @@ static int riscv_pm_pmu_notify(struct notifier_blo= ck *b, unsigned long cmd, > return NOTIFY_OK; > } > =20 > +static int riscv_pm_pmu_notify(struct notifier_block *b, > + unsigned long cmd, void *v) > +{ > + struct riscv_pmu *rvpmu =3D container_of(b, struct riscv_pmu, > + riscv_pm_nb); > + > +#ifdef CONFIG_RISCV_PMU_SBI_SSE > + struct cpu_hw_events *cpuc =3D this_cpu_ptr(rvpmu->hw_events); > + int ret; > + > + if (!riscv_pmu_use_irq && READ_ONCE(rvpmu->sse_active)) { > + switch (cmd) { > + case CPU_PM_ENTER: > + cpuc->pm_resume_sse =3D > + sse_event_is_enabled_local(rvpmu->sse_evt); > + if (cpuc->pm_resume_sse) { > + ret =3D sse_event_disable_local(rvpmu->sse_evt); > + if (ret) { > + cpuc->pm_resume_sse =3D false; > + pmu_sbi_fail_sse(rvpmu, "disable for CPU PM", > + ret); > + return notifier_from_errno(ret); > + } > + } > + break; > + case CPU_PM_EXIT: > + case CPU_PM_ENTER_FAILED: > + ret =3D riscv_pm_pmu_update(rvpmu, cmd); > + if (!cpuc->pm_resume_sse) > + return ret; > + > + cpuc->pm_resume_sse =3D false; > + ret =3D sse_event_enable_local(rvpmu->sse_evt); > + if (ret) { > + pmu_sbi_fail_sse(rvpmu, "enable after CPU PM", ret); > + return notifier_from_errno(ret); > + } > + return NOTIFY_OK; > + default: > + break; > + } > + } > +#endif [Severity: Medium] Should riscv_pm_pmu_notify() also check !READ_ONCE(cpuc->sse_failed) before running local SSE transitions? When pmu_sbi_fail_sse() latches cpuc->sse_failed while the local SSE event is enabled (for example, when pmu_sbi_ovf_sse_handler() sees !regs, or when pmu_sbi_sse_disable() fails), reg_evt->is_enabled stays true and pmu_sbi_sse_disable() / pmu_sbi_sse_enable() become no-ops. Without a cpuc->sse_failed check here, the next CPU_PM_ENTER sees sse_event_is_enabled_local() =3D=3D true and calls sse_event_disable_local(): if that succeeds, CPU_PM_EXIT re-enables the SSE event on a CPU that latched sse_failed, and if it fails, every subseque= nt CPU_PM_ENTER returns notifier_from_errno(ret) and aborts idle entry. [Severity: Medium] What happens on the next CPU_PM_ENTER if sse_events_unmask() fails in sse_pm_notifier() during CPU_PM_EXIT? On CPU_PM_EXIT, sse_pm_notifier() (priority 0) runs before riscv_pm_pmu_notify() (priority -1). If sse_events_unmask() fails, sse_pm_notifier() returns notifier_from_errno(ret) with NOTIFY_STOP_MASK, stopping the notifier chain before riscv_pm_pmu_notify() runs, so cpuc->sse_failed is not latched while the SSE event remains disabled and the counters remain stopped with PERF_HES_STOPPED. On the next CPU_PM_ENTER, riscv_pm_pmu_notify() overwrites cpuc->pm_resume_sse with sse_event_is_enabled_local(rvpmu->sse_evt) (false), and riscv_pm_pmu_update() zeroes cpuc->pm_resume_hw_ctrs and skips all counters with PERF_HES_STOPPED set, permanently losing the saved resume state for both the counters and the SSE event. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202070003.4249= 1-12-zhangzhanpeng.jasper@bytedance.com?part=3D7