BPF List
 help / color / mirror / Atom feed
From: Leo Yan <leo.yan@arm.com>
To: Puranjay Mohan <puranjay@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	bpf@vger.kernel.org,
	Anshuman Khandual <anshuman.khandual@arm.com>,
	Puranjay Mohan <puranjay12@gmail.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	John Fastabend <john.fastabend@gmail.com>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Rob Herring <robh@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	James Clark <james.clark@linaro.org>,
	Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Shuah Khan <shuah@kernel.org>, Breno Leitao <leitao@debian.org>,
	Ravi Bangoria <ravi.bangoria@amd.com>,
	Stephane Eranian <eranian@google.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Usama Arif <usama.arif@linux.dev>,
	linux-arm-kernel@lists.infradead.org,
	linux-perf-users@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
	kernel-team@meta.com
Subject: Re: [PATCH v6 2/3] perf/arm64: Add BRBE support for bpf_get_branch_snapshot()
Date: Thu, 26 Nov 2026 17:19:04 +0000	[thread overview]
Message-ID: <20261126171904.GA809232@e132581.arm.com> (raw)
In-Reply-To: <20260810164622.1829843-3-puranjay@kernel.org>

On Mon, Aug 10, 2026 at 09:46:19AM -0700, Puranjay Mohan wrote:

[...]

> +int brbe_snapshot_branch_stack(struct perf_branch_entry *entries, unsigned int cnt)
> +{
> +	u64 brbidr, brbfcr, brbcr, pmcr;
> +	int nr_hw, nr_copied = 0;
> +	bool paused_here = false;
> +	unsigned long flags;
> +
> +	/* Called with migration disabled, so this is the CPU read below. */
> +	if (!valid_brbe_version())
> +		return 0;
> +
> +	flags = raw_local_daif_save();
> +
> +	brbcr = read_sysreg_s(SYS_BRBCR_EL1);
> +	brbfcr = read_sysreg_s(SYS_BRBFCR_EL1);
> +
> +	/*
> +	 * PAUSED is only ever set behind our back, so finding it set is final.
> +	 * likely() keeps this inline; a taken branch costs a record.
> +	 */
> +	if (likely(brbcr && !(brbfcr & BRBFCR_EL1_PAUSED))) {
> +		/*
> +		 * Stop the counters so a freeze cannot set PAUSED behind the
> +		 * value read below; the D19.3 conditions need a PMOVSCLR_EL0
> +		 * bit, which a stopped counter cannot set.
> +		 */
> +		pmcr = read_pmcr();
> +		write_pmcr(pmcr & ~ARMV8_PMU_PMCR_E);
> +		isb();
> +
> +		brbfcr = read_sysreg_s(SYS_BRBFCR_EL1);
> +		paused_here = !(brbfcr & BRBFCR_EL1_PAUSED);
> +
> +		write_sysreg_s(brbfcr | BRBFCR_EL1_PAUSED, SYS_BRBFCR_EL1);
> +		isb();
> +	}

AI pointed out a edge case:

 1) The PMU is enabled (PMCR_EL0.E=1). The first BRBFCR_EL1 read
    observes PAUSED=0.
 2) A counter overflows and hardware freezes BRBE before the helper
    stops the counters. This can happen while interrupt is masked.
 3) The helper saves PMCR_EL0 and clears PMCR_E. Its second BRBFCR_EL1
    read observes PAUSED=1, so paused_here becomes false.
 4) The only write restoring the saved PMCR_EL0 is inside
    "if (paused_here)". The helper therefore returns with PMCR_EL0.E=0.

This can happen with a normal PMU IRQ because hardware can freeze BRBE
while local IRQs are masked.

> +	trace_hardirqs_off();

The irqsoff tracer misses the time between raw_local_daif_save() and
this call. Other callers could introduce similar gaps by using the raw
helper.

I'll reply separately on the pNMI / NMI cases with an approach that
may avoid adding the raw helpers.

Thanks,
Leo

  parent reply	other threads:[~2026-11-26 17:19 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-10 16:46 [PATCH v6 0/3] arm64: Add BRBE support for bpf_get_branch_snapshot() Puranjay Mohan
2026-08-10 16:46 ` [PATCH v6 1/3] arm64: Add raw variants of local_daif_save() and local_daif_restore() Puranjay Mohan
2026-08-10 16:46 ` [PATCH v6 2/3] perf/arm64: Add BRBE support for bpf_get_branch_snapshot() Puranjay Mohan
2026-08-10 16:56   ` sashiko-bot
2026-11-26 17:19   ` Leo Yan [this message]
2026-08-10 16:46 ` [PATCH v6 3/3] selftests/bpf: Adjust entry thresholds for ARM64 BRBE Puranjay Mohan
2026-09-01 13:45 ` [PATCH v6 0/3] arm64: Add BRBE support for bpf_get_branch_snapshot() Puranjay Mohan
2026-09-22 20:40   ` Andrii Nakryiko
2026-09-23  9:15     ` James Clark
2026-09-23 16:46       ` Andrii Nakryiko
2026-11-26 12:39         ` James Clark

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=20261126171904.GA809232@e132581.arm.com \
    --to=leo.yan@arm.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=andrii@kernel.org \
    --cc=anshuman.khandual@arm.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=eranian@google.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=john.fastabend@gmail.com \
    --cc=kernel-team@meta.com \
    --cc=leitao@debian.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=puranjay12@gmail.com \
    --cc=puranjay@kernel.org \
    --cc=ravi.bangoria@amd.com \
    --cc=robh@kernel.org \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=usama.arif@linux.dev \
    --cc=will@kernel.org \
    --cc=yonghong.song@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