[PATCH v6 3/3] perf/core: Clear the whole branch entry in perf_clear_branch_entry()

Puranjay Mohan puranjay12 at gmail.com
Fri Aug 7 07:32:53 PDT 2026


On Fri, Aug 7, 2026 at 11:29 AM Peter Zijlstra <peterz at infradead.org> wrote:
>
> On Thu, Aug 06, 2026 at 06:52:23AM -0700, Puranjay Mohan wrote:
> > perf_clear_branch_entry_bitfields() clears the bitfields of struct
> > perf_branch_entry one by one and leaves from/to alone, since callers
> > overwrite those straight away. The list has to be kept in sync with the
> > struct by hand and has already fallen behind: new_type and priv were
> > added to perf_branch_entry and never added here.
> >
> > Only BRBE writes those two, and neither is written for every record.
> > brbe_set_perf_entry_type() leaves new_type alone for a branch type it
> > does not recognise, and priv is not set for source-only records.
> > arm_pmuv3.c allocates the per-CPU branch stack with kmalloc(), so such a
> > record carries whatever the slot held: uninitialised kmalloc() data on
> > the first pass over the buffer, the previous record's values after that.
> > Both reach userspace through the branch stack. Nothing under
> > arch/x86/events/ writes either field, so x86 is unaffected.
> >
> > Clear the entry with a single struct assignment instead:
> >
> >       *br = (struct perf_branch_entry){ };
> >
> > The bitfields add up to exactly 64 bits, so there is no padding, and
> > every caller assigns from/to immediately afterwards, so zeroing those as
> > well changes nothing. PERF_BR_SPEC_NA is 0, so dropping the explicit
> > spec assignment leaves the behaviour unchanged. Nothing needs keeping in
> > sync when a field is added.
> >
> > The helper no longer touches only bitfields, so rename it to
> > perf_clear_branch_entry().
>
> Fair enough I suppose, but then why not write it like so?

Yes, that is better. I will use your version in v7.

Thanks,
Puranjay



More information about the linux-arm-kernel mailing list