Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Zhanpeng Zhang" <zhangzhanpeng.jasper@bytedance.com>,
	linux-perf-users@vger.kernel.org
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [RFC PATCH v1 5/5] perf: riscv: Deliver PDIS samples through the SBI PMU interrupt
Date: Wed, 18 Nov 2026 08:47:09 +0000	[thread overview]
Message-ID: <sashiko-outbox-220493@kernel.org> (raw)
In-Reply-To: <20261118081327.10684-12-zhangzhanpeng.jasper@bytedance.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] perf: riscv: transient pdis_cpus clear in
  pdis_cpu_pm_notify() on idle exit
- [Medium] perf: riscv: pmu_sbi_ovf_handler() restarts stopped counters
  on PDIS IRQ
- [Medium] perf: riscv: pcpu->ready left enabled on failure in
  riscv_pdis_register()
--

commit cd24c079596cb117f90d17389f647a0a1991c1b3
Author: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>

perf: riscv: Deliver PDIS samples through the SBI PMU interrupt

This commit wires up PDIS overflow handling into the shared SBI PMU
interrupt handler, registers a CPU PM notifier to preserve PDIS state
across idle states, and registers the PDIS test PMU from the SBI PMU
probe path.

> diff --git a/drivers/perf/riscv_pdis.c b/drivers/perf/riscv_pdis.c
> index 9a8fe5e4ab828..60c9ece22dfce 100644
> --- a/drivers/perf/riscv_pdis.c
> +++ b/drivers/perf/riscv_pdis.c

[ ... ]

> @@ -413,6 +480,32 @@ void riscv_pdis_cpu_dying(void)
>  	cpumask_clear_cpu(smp_processor_id(), &pdis_cpus);
>  }
>  
> +static int pdis_cpu_pm_notify(struct notifier_block *nb, unsigned long cmd, void *v)
> +{
> +	struct pdis_cpu *pcpu = this_cpu_ptr(&pdis_cpu);
> +	struct perf_event *event = pcpu->event;
> +
> +	if (!pcpu->ready)
> +		return NOTIFY_DONE;
> +	if (cmd == CPU_PM_ENTER) {
> +		pcpu->pm_resume = event && !(event->hw.state & PERF_HES_STOPPED);
> +		if (event)
> +			pdis_stop(event, PERF_EF_UPDATE);
> +		else
> +			pdis_quiesce();
> +	} else if (cmd == CPU_PM_EXIT || cmd == CPU_PM_ENTER_FAILED) {
> +		riscv_pdis_cpu_starting();
> +		if (pcpu->pm_resume && pcpu->ready && event)
> +			pdis_start(event, PERF_EF_RELOAD);
> +		pcpu->pm_resume = false;
> +	}
> +	return NOTIFY_OK;
> +}

[Severity: Medium]
Can calling riscv_pdis_cpu_starting() on CPU_PM_EXIT or
CPU_PM_ENTER_FAILED cause concurrent perf_event_open() calls targeting the
waking CPU to fail spuriously with -ENODEV?

riscv_pdis_cpu_starting() clears the CPU from pdis_cpus before re-probing
device tree properties and CSRs:

drivers/perf/riscv_pdis.c:riscv_pdis_cpu_starting() {
	pcpu->ready = false;
	pcpu->counter_valid = false;
	cpumask_clear_cpu(cpu, &pdis_cpus);
	...
	pcpu->ready = true;
	cpumask_set_cpu(cpu, &pdis_cpus);
}

Because CPU_PM_EXIT runs on every deep cpuidle wakeup without serializing
against pdis_event_init() or cpumask_show() on other CPUs, a concurrent
reader can observe the online CPU missing from pdis_cpus:

CPU0                              CPU1
pdis_cpu_pm_notify(CPU_PM_EXIT)
  riscv_pdis_cpu_starting()
    cpumask_clear_cpu(0, &pdis_cpus)
    pdis_test_model_matches(0)
                                  perf_event_open(..., cpu=0)
                                    pdis_event_init()
                                      !cpumask_test_cpu(0, &pdis_cpus)
                                        --> returns -ENODEV
    cpumask_set_cpu(0, &pdis_cpus)

[ ... ]

> @@ -444,3 +537,22 @@ static struct pmu pdis_pmu = {
>  	.check_period = pdis_check_period,
>  	.sched_task = pdis_sched_task,
>  };
> +
> +int riscv_pdis_register(struct device *parent)
> +{
> +	int ret;
> +
> +	if (cpumask_empty(&pdis_cpus)) {
> +		if (pdis_test)
> +			dev_info(parent, "PDIS test PMU unavailable on all online CPUs\n");
> +		return -ENODEV;
> +	}
> +	ret = cpu_pm_register_notifier(&pdis_cpu_pm_nb);
> +	if (ret)
> +		return ret;
> +	pdis_pmu.parent = parent;
> +	ret = perf_pmu_register(&pdis_pmu, "riscv_pdis_test", -1);
> +	if (ret)
> +		cpu_pm_unregister_notifier(&pdis_cpu_pm_nb);
> +	return ret;
> +}

[Severity: Medium]
If riscv_pdis_register() fails, does it leave pcpu->ready and pdis_test
enabled while the SBI PMU continues running without pdis_cpu_pm_nb
registered?

pmu_sbi_device_probe() calls cpuhp_state_add_instance() before
riscv_pdis_register(), which sets pcpu->ready = true on online CPUs via
pmu_sbi_starting_cpu() -> riscv_pdis_cpu_starting(), and ignores
registration errors from riscv_pdis_register().  If
cpu_pm_register_notifier() or perf_pmu_register() fails (or if
riscv_pdis_register() returns -ENODEV and a PDIS-capable CPU is brought
online later), riscv_pdis_irq_prepare() still runs on every SBI PMU
interrupt and executes an unguarded csr_clear(RISCV_PDIS_CSR_CTL,
RISCV_PDIS_CTL_OF) without pdis_cpu_pm_notify() registered to quiesce or
re-probe PDIS CSRs across non-retentive CPU idle transitions.

> diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
> index 545b8becf90ae..af86a30aae835 100644
> --- a/drivers/perf/riscv_pmu_sbi.c
> +++ b/drivers/perf/riscv_pmu_sbi.c
> @@ -1092,24 +1092,23 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)

[ ... ]

> @@ -1128,7 +1127,7 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
>  
>  	/* No overflow bit is set */
>  	if (!overflow)
> -		return IRQ_NONE;
> +		goto restart;
>  
>  	regs = get_irq_regs();
>  
> @@ -1181,10 +1180,20 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
>  		hw_evt->state = 0;
>  	}
>  
> -	pmu_sbi_start_overflow_mask(pmu, overflowed_ctrs);
> -	perf_sample_event_took(sched_clock() - start_clock);
> +	handled = true;
>  
> -	return IRQ_HANDLED;
> +restart:
> +	/* A PDIS-only interrupt must also resume the ordinary HPM counters. */
> +	pmu_sbi_start_overflow_mask(pmu, overflowed_ctrs);

[Severity: Medium]
Does jumping to restart on a PDIS-only interrupt start SBI hardware
counters that perf has stopped or throttled?

When an SBI PMU event is stopped via riscv_pmu_stop() (for example by
PERF_EVENT_IOC_DISABLE or perf_event_throttle()), event->hw.state has
PERF_HES_STOPPED set, but its bit in cpu_hw_evt->used_hw_ctrs remains set
until riscv_pmu_del().  pmu_sbi_start_overflow_mask() starts every counter
in used_hw_ctrs & ~ctr_ovf_mask without checking PERF_HES_STOPPED:

drivers/perf/riscv_pmu_sbi.c:pmu_sbi_start_ovf_ctrs_sbi() {
	for (i = 0; i < BITS_TO_LONGS(RISCV_MAX_COUNTERS); i++) {
		ctr_start_mask = cpu_hw_evt->used_hw_ctrs[i] & ~ctr_ovf_mask;
		/* Start all the counters that did not overflow in a single shot */
		if (ctr_start_mask) {
			sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, i * BITS_PER_LONG,
				  ctr_start_mask, 0);
		}
	}
}

When a PDIS-only interrupt fires on a CPU with a stopped SBI HPM event,
overflow is 0 and overflowed_ctrs is 0, so pmu_sbi_start_overflow_mask()
restarts the stopped hardware counter behind perf's back.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118081327.10684-7-zhangzhanpeng.jasper@bytedance.com?part=5

      reply	other threads:[~2026-11-18  8:47 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  8:13 [RFC PATCH v1 0/5] RISC-V: Add perf support for PDIS Zhanpeng Zhang
2026-11-18  8:13 ` [RFC PATCH v1 1/5] dt-bindings: riscv: Add Smpdis and Sspdis ISA extensions Zhanpeng Zhang
2026-11-18  8:23   ` sashiko-bot
2026-11-18  8:13 ` [RFC PATCH v1 2/5] riscv: Detect " Zhanpeng Zhang
2026-11-18  8:13 ` [RFC PATCH v1 3/5] perf: riscv: Probe the experimental PDIS CSR layout on matching CPUs Zhanpeng Zhang
2026-11-18  8:39   ` sashiko-bot
2026-11-18  8:13 ` [RFC PATCH v1 4/5] perf: riscv: Add PDIS event and counter callbacks Zhanpeng Zhang
2026-11-18  8:49   ` sashiko-bot
2026-11-18  8:13 ` [RFC PATCH v1 5/5] perf: riscv: Deliver PDIS samples through the SBI PMU interrupt Zhanpeng Zhang
2026-11-18  8:47   ` 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-220493@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=zhangzhanpeng.jasper@bytedance.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox