Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Matt Turner" <mattst88@gmail.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 5/7] parisc: perf: add a perf events PMU for PA-8800/PA-8900
Date: Fri, 27 Nov 2026 05:27:29 +0000	[thread overview]
Message-ID: <sashiko-outbox-236642@kernel.org> (raw)
In-Reply-To: <20261127-parisc-perf-pa8800-v1-5-4f3f077010ce@gmail.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] parisc: perf: NULL ctx access in mako_pmu_event_init() on
  standalone event
- [Medium] parisc: perf: unconditional counter disable and re-enable in
  mako_pmu_disable()
- [Medium] parisc: perf: missing perf_event_update_userpage() in
  mako_pmu_add()
- [Medium] parisc: perf: redundant RDR chain-1 counter reads in
  mako_read_all() callers
--

commit b4421aebf6e5977bf8d6dd6b5012fa15f8624837
Author: Matt Turner <mattst88@gmail.com>

parisc: perf: add a perf events PMU for PA-8800/PA-8900

This commit exposes the PA-8800/PA-8900 performance counters to perf events
as the "cpu" PMU, using a per-CPU hrtimer to poll the 31-bit hardware
counters and coordinating ownership with /dev/perf.

> diff --git a/arch/parisc/kernel/perf_event_mako.c b/arch/parisc/kernel/perf_event_mako.c
> new file mode 100644
> index 0000000000000..f43c8a6cf13d2
> --- /dev/null
> +++ b/arch/parisc/kernel/perf_event_mako.c
[ ... ]
> +/*
> + * Read the counters, and if they are running for perf, update every
> + * started event. The counters have to be stopped to be read. They may
> + * have been left running by /dev/perf, so this always reads them. Called
> + * with interrupts off.
> + */
> +static void mako_read_all(struct mako_pmu_cpu *pc)
> +{
> +	int i;
> +
> +	perf_mako_disable();
> +	if (perf_mako_read(pc->ctrs) == 0 && pc->running) {
> +		for (i = 0; i < MAKO_NUM_CTRS; i++) {
> +			struct perf_event *event = pc->events[i];
> +
> +			if (event && !(event->hw.state & PERF_HES_STOPPED))
> +				mako_event_update(event, pc->ctrs[i]);
> +		}
> +	}
> +	if (pc->running)
> +		perf_mako_enable();
> +}

[Severity: Medium]
Does mako_read_all() need to call perf_mako_read(pc->ctrs) when pc->running
is false?

The perf core calls mako_pmu_disable() before scheduling events out via
mako_pmu_del() -> mako_pmu_stop() and before scheduling events in via
mako_pmu_add() -> mako_pmu_start().  Because mako_pmu_stop() and
mako_pmu_start() each call mako_read_all(pc) per event while pc->running is
already false, every context switch or reschedule with N active events
performs up to 2N + 1 redundant perf_mako_read() calls on already-stopped
counters, each acquiring the cross-core RDR chain 1 lock and shifting all
256 bits of shared RDR15_1 out and back in with interrupts disabled.

> +
> +static enum hrtimer_restart mako_poll(struct hrtimer *timer)
> +{
> +	struct mako_pmu_cpu *pc = container_of(timer, struct mako_pmu_cpu, timer);
> +
> +	mako_read_all(pc);
> +	hrtimer_forward_now(timer, ns_to_ktime(MAKO_POLL_NS));
> +
> +	return HRTIMER_RESTART;
> +}
> +
> +static void mako_pmu_disable(struct pmu *pmu)
> +{
> +	struct mako_pmu_cpu *pc = this_cpu_ptr(&mako_pmu_cpu);
> +
> +	mako_read_all(pc);
> +	perf_mako_disable();
> +	pc->running = false;
> +}

[Severity: Medium]
Should mako_pmu_disable() return early when !pc->running, and clear
pc->running before calling mako_read_all(pc) (or after updating the counts
without re-enabling the counters)?

When pc->running is true, calling mako_read_all(pc) first causes
mako_read_all() to transiently re-enable the hardware counters right before
mako_pmu_disable() disables them again.

When pc->running is false, mako_pmu_disable() still stops the hardware
counters.  Once a per-CPU event has been created on mako_pmu and closed,
mako_pmu's perf_event_pmu_context remains linked on
cpuctx->ctx.pmu_ctx_list; if /dev/perf is then opened and starts the
counters, any perf_ctx_disable(&cpuctx->ctx, 0) on that CPU (such as a task
with a pinned software event scheduling in) calls mako_pmu_disable() and
stops /dev/perf's active counters, while the paired mako_pmu_enable() sees
no mako_pmu events and leaves them stopped.

[ ... ]
> +static int mako_pmu_add(struct perf_event *event, int flags)
> +{
> +	struct mako_pmu_cpu *pc = this_cpu_ptr(&mako_pmu_cpu);
> +	struct perf_event *events[MAKO_NUM_CTRS + 1];
> +	unsigned int code = mako_event_code(event);
> +	struct perf_mako_config cfg;
> +	int idx[MAKO_NUM_CTRS + 1];
> +	unsigned long free = 0;
> +	int i, n;
> +
> +	n = mako_collect(pc, event, events);
> +	if (n > MAKO_NUM_CTRS || !mako_schedule(events, n, idx, &cfg))
> +		return -EAGAIN;
> +
> +	/*
> +	 * Keep the events already here on their counters. Each event can
> +	 * use either only one counter or only ctr1 and ctr3, so if the
> +	 * events fit together, one of this event's counters is free.
> +	 */
> +	for (i = 0; i < MAKO_NUM_CTRS; i++)
> +		if (!pc->events[i] && (mako_ctrs[code] & BIT(i)))
> +			free |= BIT(i);
> +
> +	event->hw.idx = __ffs(free);
> +	pc->events[event->hw.idx] = event;
> +
> +	event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
> +	if (flags & PERF_EF_START)
> +		mako_pmu_start(event, PERF_EF_RELOAD);
> +
> +	return 0;
> +}

[Severity: Medium]
Is perf_event_update_userpage(event) missing in mako_pmu_add()?

mako_pmu_del() calls perf_event_update_userpage(event) on schedule-out, but
neither mako_pmu_add() nor mako_pmu_start() calls it on schedule-in.  Since
merge_sched_in() only updates the userpage when scheduling in fails,
userspace mappings of perf_event_mmap_page see stale index, offset,
time_enabled, and time_running fields while the event is active.

[ ... ]
> +static int mako_pmu_event_init(struct perf_event *event)
> +{
> +	struct perf_event *leader = event->group_leader, *sibling;
> +	struct perf_event *events[MAKO_NUM_CTRS + 1];
> +	struct perf_mako_config cfg;
> +	int idx[MAKO_NUM_CTRS + 1];
> +	int code, n = 0;
> +
> +	switch (event->attr.type) {
> +	case PERF_TYPE_HARDWARE:
> +		code = mako_map_hw(event->attr.config);
> +		break;
> +	case PERF_TYPE_RAW:
> +		if (event->attr.config & ~MAKO_CONFIG_EVENT)
> +			return -EINVAL;
> +		code = event->attr.config;
> +		break;
> +	default:
> +		return -ENOENT;
> +	}
> +	if (code < 0)
> +		return code;
> +	if (code >= MAKO_EV_MAX || !mako_ctrs[code])
> +		return -EINVAL;
> +
> +	event->hw.config = code;
> +
> +	/* The whole group has to fit on one core. */
> +	if (leader != event && leader->pmu == event->pmu)
> +		events[n++] = leader;
> +	for_each_sibling_event(sibling, leader) {

[Severity: Medium]
Can for_each_sibling_event(sibling, leader) dereference a NULL leader->ctx
when event is a standalone event or a new group leader (leader == event)?

During perf_event_alloc() -> perf_init_event() -> perf_try_init_event(),
event->ctx is still NULL and perf_try_init_event() only locks
group_leader->ctx->mutex when event->group_leader != event.  Calling
for_each_sibling_event(sibling, leader) when leader == event evaluates
lockdep_assert_event_ctx(leader):

include/linux/perf_event.h:for_each_sibling_event() {
	lockdep_assert_event_ctx(event);			\
	if ((event)->group_leader == (event))			\
		list_for_each_entry((sibling), &(event)->sibling_list, sibling_list)
}

Under CONFIG_PROVE_LOCKING=y, lockdep_is_held(&(leader)->ctx->mutex)
triggers a WARN_ON_ONCE() splat on every standalone perf_event_open() and
dereferences a near-NULL pointer in match_held_lock() when an event is
inherited across fork().  Guarding the sibling loop with if (leader !=
event) avoids touching leader->ctx before the event is installed in a
context.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127-parisc-perf-pa8800-v1-0-4f3f077010ce@gmail.com?part=5

  reply	other threads:[~2026-11-27  5:27 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  5:02 [PATCH 0/7] parisc: perf: fix the RDR tables and add PA-8800/PA-8900 counters Matt Turner
2026-11-27  5:02 ` [PATCH 1/7] parisc: perf: swap the PCX-U and PCX-W RDR tables Matt Turner
2026-11-27  5:02 ` [PATCH 2/7] parisc: perf: add an RDR table for PCX-W2 (PA-8700) Matt Turner
2026-11-27  5:02 ` [PATCH 3/7] parisc: perf: add PA-8800/PA-8900 RDR and DR2 access Matt Turner
2026-11-27  5:02 ` [PATCH 4/7] parisc: perf: program the PA-8800/PA-8900 counters Matt Turner
2026-11-27  5:02 ` [PATCH 5/7] parisc: perf: add a perf events PMU for PA-8800/PA-8900 Matt Turner
2026-11-27  5:27   ` sashiko-bot [this message]
2026-11-27  5:02 ` [PATCH 6/7] parisc: perf: add PA-8800/PA-8900 event sets and an instruction filter Matt Turner
2026-11-27  5:03 ` [PATCH 7/7] parisc: perf: add PA-8800/PA-8900 memory and branch events Matt Turner
2026-11-27  5:27 ` [PATCH 0/7] parisc: perf: fix the RDR tables and add PA-8800/PA-8900 counters sashiko-bot

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-236642@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mattst88@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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