[PATCH v2 2/4] perf: Fix uninitialized bitfields in perf_clear_branch_entry_bitfields()

Leo Yan leo.yan at arm.com
Thu Apr 9 07:28:03 PDT 2026


On Wed, Mar 18, 2026 at 10:16:56AM -0700, Puranjay Mohan wrote:
> perf_clear_branch_entry_bitfields() zeroes individual bitfields of struct
> perf_branch_entry but misses the new_type (4 bits) and priv (3 bits)
> fields. This means any code path that relies on this function to produce
> a clean entry may expose stale or uninitialised data in these fields to
> userspace.
> 
> The function was introduced by commit bfe4daf850f4 ("perf/core: Add
> perf_clear_branch_entry_bitfields() helper") specifically to "centralize
> the initialization to avoid missing a field in case more are added."
> Unfortunately, the commits that later added new_type and priv to struct
> perf_branch_entry only updated the UAPI header and did not update this
> clearing function.
> 
> Zero new_type and priv alongside the other bitfields.
> 
> Fixes: b190bc4ac9e6 ("perf: Extend branch type classification")
> Fixes: 5402d25aa571 ("perf: Capture branch privilege information")
> Signed-off-by: Puranjay Mohan <puranjay at kernel.org>
> ---
>  include/linux/perf_event.h | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
> index 48d851fbd8ea..d7f39b7e9cda 100644
> --- a/include/linux/perf_event.h
> +++ b/include/linux/perf_event.h
> @@ -1481,6 +1481,8 @@ static inline void perf_clear_branch_entry_bitfields(struct perf_branch_entry *b
>  	br->cycles	= 0;
>  	br->type	= 0;
>  	br->spec	= PERF_BR_SPEC_NA;
> +	br->new_type	= 0;
> +	br->priv	= 0;
>  	br->reserved	= 0;
>  }

We already know this does not work well. Instead, we can define a union
for bitfield and use memset to clear it, later we will not bother for
this kind of issue anymore.

  struct perf_branch_entry {
          ...
  
          union {
                  struct {
                          __u64   mispred   :  1, /* target mispredicted */
                                  predicted :  1, /* target predicted */
                                  ...
                                  reserved  : 31;
                  };
                  __u64 bitfields;
          };
  };

static inline void perf_clear_branch_entry_bitfields(struct perf_branch_entry *br)
{
        memset(&br->bitfields, 0, sizeof(br->bitfields));
}

Thanks,
Leo



More information about the linux-arm-kernel mailing list