perf: Avoid -Wflex-array-member-not-at-end warnings
Subsystems (2)
PATCH 1/1 perf: Avoid -Wflex-array-member-not-at-end warnings
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are getting ready to enable it, globally.
So, in order to avoid ending up with a flexible-array member in the middle of multiple other structs, we use the struct_group_tagged() helper to create a new tagged struct perf_branch_stack_hdr. This structure groups together all the members of the flexible struct perf_branch_stack except the flexible array.
As a result, the array is effectively separated from the rest of the members without modifying the memory layout of the flexible structure. We then change the type of the middle struct members currently causing trouble from struct perf_branch_stack to struct perf_branch_stack_hdr.
We also want to ensure that when new members need to be added to the flexible structure, they are always included within the newly created tagged struct. For this, we use static_assert(). This ensures that the memory layout for both the flexible structure and the new tagged struct is the same after any changes.
This approach avoids having to implement struct perf_branch_stack_hdr as a completely separate structure, thus preventing having to maintain two independent but basically identical structures, closing the door to potential bugs in the future.
We also use container_of() whenever we need to retrieve a pointer to the flexible structure, through which the flexible-array member can be accessed, if necessary.
So, with these changes, fix the following warnings:arch/x86/events/amd/../perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] arch/x86/events/intel/../perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] arch/x86/events/perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] arch/x86/events/zhaoxin/../perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] ./arch/x86/include/generated/../../events/perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] arch/x86/xen/../events/perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
--- arch/x86/events/amd/core.c | 4 +++- arch/x86/events/core.c | 4 +++- arch/x86/events/intel/ds.c | 4 +++- arch/x86/events/intel/lbr.c | 8 ++++++-- arch/x86/events/perf_event.h | 2 +- include/linux/perf_event.h | 9 +++++++-- 6 files changed, 23 insertions(+), 8 deletions(-)
arch/x86/events/amd/core.c +3 -1
@@ -1001,7 +1001,9 @@ static int amd_pmu_v2_handle_irq(struct pt_regs *regs)continue ;if (has_branch_stack (event)) -perf_sample_save_brstack (&data, event, &cpuc->lbr_stack,NULL ); +perf_sample_save_brstack (&data, event, + container_of(&cpuc->lbr_stack, struct perf_branch_stack, hdr), +NULL );if (perf_event_overflow (event, &data, regs))x86_pmu_stop (event,0 );
arch/x86/events/core.c +3 -1
@@ -1704,7 +1704,9 @@ int x86_pmu_handle_irq(struct pt_regs *regs)perf_sample_data_init (&data,0 , event->hw.last_period);if (has_branch_stack (event)) -perf_sample_save_brstack (&data, event, &cpuc->lbr_stack,NULL ); +perf_sample_save_brstack (&data, event, + container_of(&cpuc->lbr_stack, struct perf_branch_stack, hdr), +NULL );if (perf_event_overflow (event, &data, regs))x86_pmu_stop (event,0 );
arch/x86/events/intel/ds.c +3 -1
@@ -1869,7 +1869,9 @@ static void setup_pebs_fixed_sample_data(struct perf_event *event,setup_pebs_time (event, data, pebs->tsc);if (has_branch_stack (event)) -perf_sample_save_brstack (data, event, &cpuc->lbr_stack,NULL ); +perf_sample_save_brstack (data, event, + container_of(&cpuc->lbr_stack, struct perf_branch_stack, hdr), +NULL ); } static void adaptive_pebs_save_regs(struct pt_regs *regs,
arch/x86/events/intel/lbr.c +6 -2
@@ -974,11 +974,15 @@ void intel_pmu_lbr_save_brstack(struct perf_sample_data *data, {if (is_branch_counters_group (event)) {intel_pmu_lbr_counters_reorder (cpuc, event); -perf_sample_save_brstack (data, event, &cpuc->lbr_stack, cpuc->lbr_counters); +perf_sample_save_brstack (data, event, + container_of(&cpuc->lbr_stack, struct perf_branch_stack, hdr), + cpuc->lbr_counters);return ; } -perf_sample_save_brstack (data, event, &cpuc->lbr_stack,NULL ); +perf_sample_save_brstack (data, event, + container_of(&cpuc->lbr_stack, struct perf_branch_stack, hdr), +NULL ); }static void intel_pmu_arch_lbr_read (struct cpu_hw_events *cpuc)
arch/x86/events/perf_event.h +1 -1
@@ -286,7 +286,7 @@ struct cpu_hw_events {*/ int lbr_users;int lbr_pebs_users; -struct perf_branch_stack lbr_stack; +struct perf_branch_stack_hdr lbr_stack;struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES ];u64 lbr_counters[MAX_LBR_ENTRIES ];/* branch stack extra */ union {
include/linux/perf_event.h +7 -2
@@ -121,10 +121,15 @@ static __always_inline bool perf_raw_frag_last(const struct perf_raw_frag *frag)* already stored in age order, the hw_idx should be 0. */ struct perf_branch_stack { -__u64 nr; -__u64 hw_idx; +/* New members MUST be added within the struct_group() macro below. */ + struct_group_tagged(perf_branch_stack_hdr, hdr, + __u64 nr; +__u64 hw_idx; + );struct perf_branch_entry entries[]; }; +static_assert (offsetof(struct perf_branch_stack , entries) ==sizeof (struct perf_branch_stack_hdr ), +"struct member likely outside of struct_group_tagged()" );struct task_struct ;
Signature · 1 line
2.34.1
Peter Zijlstra
maintainerQuoted 29 lines · Gustavo A. R. Silva
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are getting ready to enable it, globally.
So, in order to avoid ending up with a flexible-array member in the middle of multiple other structs, we use the
struct_group_tagged()helper to create a new taggedstruct perf_branch_stack_hdr. This structure groups together all the members of the flexiblestruct perf_branch_stackexcept the flexible array.As a result, the array is effectively separated from the rest of the members without modifying the memory layout of the flexible structure. We then change the type of the middle struct members currently causing trouble from
struct perf_branch_stacktostruct perf_branch_stack_hdr.We also want to ensure that when new members need to be added to the flexible structure, they are always included within the newly created tagged struct. For this, we use
static_assert(). This ensures that the memory layout for both the flexible structure and the new tagged struct is the same after any changes.This approach avoids having to implement
struct perf_branch_stack_hdras a completely separate structure, thus preventing having to maintain two independent but basically identical structures, closing the door to potential bugs in the future.We also use
container_of()whenever we need to retrieve a pointer to the flexible structure, through which the flexible-array member can be accessed, if necessary.
Quoted 7 lines
So, with these changes, fix the following warnings:
arch/x86/events/amd/../perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]arch/x86/events/intel/../perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]arch/x86/events/perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]arch/x86/events/zhaoxin/../perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]./arch/x86/include/generated/../../events/perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]arch/x86/xen/../events/perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
Urgh, this fugly.. And you're breaking coding style :-(
Let me see if this can't be done sanely...
Quoted 115 lines
Signed-off-by Gustavo A. R. Silva <[email protected]>--- arch/x86/events/amd/core.c | 4 +++- arch/x86/events/core.c | 4 +++- arch/x86/events/intel/ds.c | 4 +++- arch/x86/events/intel/lbr.c | 8 ++++++-- arch/x86/events/perf_event.h | 2 +- include/linux/perf_event.h | 9 +++++++-- 6 files changed, 23 insertions(+), 8 deletions(-)arch/x86/events/amd/core.c +3
-1@@ -1001,7 +1001,9 @@ static int amd_pmu_v2_handle_irq(struct pt_regs *regs)continue ;if (has_branch_stack (event)) -perf_sample_save_brstack (&data, event, &cpuc->lbr_stack,NULL ); +perf_sample_save_brstack (&data, event, + container_of(&cpuc->lbr_stack, struct perf_branch_stack, hdr), +NULL );if (perf_event_overflow (event, &data, regs))x86_pmu_stop (event,0 );arch/x86/events/core.c +3
-1@@ -1704,7 +1704,9 @@ int x86_pmu_handle_irq(struct pt_regs *regs)perf_sample_data_init (&data,0 , event->hw.last_period);if (has_branch_stack (event)) -perf_sample_save_brstack (&data, event, &cpuc->lbr_stack,NULL ); +perf_sample_save_brstack (&data, event, + container_of(&cpuc->lbr_stack, struct perf_branch_stack, hdr), +NULL );if (perf_event_overflow (event, &data, regs))x86_pmu_stop (event,0 );arch/x86/events/intel/ds.c +3
-1@@ -1869,7 +1869,9 @@ static void setup_pebs_fixed_sample_data(struct perf_event *event,setup_pebs_time (event, data, pebs->tsc);if (has_branch_stack (event)) -perf_sample_save_brstack (data, event, &cpuc->lbr_stack,NULL ); +perf_sample_save_brstack (data, event, + container_of(&cpuc->lbr_stack, struct perf_branch_stack, hdr), +NULL ); } static void adaptive_pebs_save_regs(struct pt_regs *regs,arch/x86/events/intel/lbr.c +6
-2@@ -974,11 +974,15 @@ void intel_pmu_lbr_save_brstack(struct perf_sample_data *data, {if (is_branch_counters_group (event)) {intel_pmu_lbr_counters_reorder (cpuc, event); -perf_sample_save_brstack (data, event, &cpuc->lbr_stack, cpuc->lbr_counters); +perf_sample_save_brstack (data, event, + container_of(&cpuc->lbr_stack, struct perf_branch_stack, hdr), + cpuc->lbr_counters);return ; } -perf_sample_save_brstack (data, event, &cpuc->lbr_stack,NULL ); +perf_sample_save_brstack (data, event, + container_of(&cpuc->lbr_stack, struct perf_branch_stack, hdr), +NULL ); }static void intel_pmu_arch_lbr_read (struct cpu_hw_events *cpuc)arch/x86/events/perf_event.h +1
-1@@ -286,7 +286,7 @@ struct cpu_hw_events {*/ int lbr_users;int lbr_pebs_users; -struct perf_branch_stack lbr_stack; +struct perf_branch_stack_hdr lbr_stack;struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES ];u64 lbr_counters[MAX_LBR_ENTRIES ];/* branch stack extra */ union {include/linux/perf_event.h +7
-2@@ -121,10 +121,15 @@ static __always_inline bool perf_raw_frag_last(const struct perf_raw_frag *frag)* already stored in age order, the hw_idx should be 0. */ struct perf_branch_stack { -__u64 nr; -__u64 hw_idx; +/* New members MUST be added within the struct_group() macro below. */ + struct_group_tagged(perf_branch_stack_hdr, hdr, + __u64 nr; +__u64 hw_idx; + );struct perf_branch_entry entries[]; }; +static_assert (offsetof(struct perf_branch_stack , entries) ==sizeof (struct perf_branch_stack_hdr ), +"struct member likely outside of struct_group_tagged()" );struct task_struct ;Signature · 1 line
2.34.1
Peter Zijlstra
maintainerQuoted 38 lines · Peter Zijlstra
Quoted 29 lines · Gustavo A. R. Silva
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are getting ready to enable it, globally.
So, in order to avoid ending up with a flexible-array member in the middle of multiple other structs, we use the
struct_group_tagged()helper to create a new taggedstruct perf_branch_stack_hdr. This structure groups together all the members of the flexiblestruct perf_branch_stackexcept the flexible array.As a result, the array is effectively separated from the rest of the members without modifying the memory layout of the flexible structure. We then change the type of the middle struct members currently causing trouble from
struct perf_branch_stacktostruct perf_branch_stack_hdr.We also want to ensure that when new members need to be added to the flexible structure, they are always included within the newly created tagged struct. For this, we use
static_assert(). This ensures that the memory layout for both the flexible structure and the new tagged struct is the same after any changes.This approach avoids having to implement
struct perf_branch_stack_hdras a completely separate structure, thus preventing having to maintain two independent but basically identical structures, closing the door to potential bugs in the future.We also use
container_of()whenever we need to retrieve a pointer to the flexible structure, through which the flexible-array member can be accessed, if necessary.Quoted 7 lines
So, with these changes, fix the following warnings:
arch/x86/events/amd/../perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]arch/x86/events/intel/../perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]arch/x86/events/perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]arch/x86/events/zhaoxin/../perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]./arch/x86/include/generated/../../events/perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]arch/x86/xen/../events/perf_event.h:289:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
Quoted 3 lines
Urgh, this fugly.. And you're breaking coding style :-(
Let me see if this can't be done sanely...
This seems to build...
arch/x86/events/amd/core.c +1 -1
@@ -1001,7 +1001,7 @@ static int amd_pmu_v2_handle_irq(struct pt_regs *regs)continue ;if (has_branch_stack (event)) -perf_sample_save_brstack (&data, event, &cpuc->lbr_stack,NULL ); +perf_sample_save_brstack (&data, event,cpu_lbr_stack (cpuc),NULL );if (perf_event_overflow (event, &data, regs))x86_pmu_stop (event,0 );
arch/x86/events/amd/lbr.c +6 -6
@@ -107,7 +107,7 @@ static void amd_pmu_lbr_filter(void) ((br_sel & X86_BR_TYPE_SAVE) != X86_BR_TYPE_SAVE)) fused_only = true; -for (i =0 ; i < cpuc->lbr_stack.nr; i++) { +for (i =0 ; i <cpu_lbr_stack (cpuc)->nr; i++) { from = cpuc->lbr_entries[i].from; to = cpuc->lbr_entries[i].to; type =branch_type_fused (from, to,0 , &offset);
@@ -137,12 +137,12 @@ static void amd_pmu_lbr_filter(void)return ;/* Remove all invalid entries */ -for (i =0 ; i < cpuc->lbr_stack.nr; ) { +for (i =0 ; i <cpu_lbr_stack (cpuc)->nr; ) {if (!cpuc->lbr_entries[i].from) { j = i; -while (++j < cpuc->lbr_stack.nr) +while (++j <cpu_lbr_stack (cpuc)->nr) cpuc->lbr_entries[j -1 ] = cpuc->lbr_entries[j]; - cpuc->lbr_stack.nr--; +cpu_lbr_stack (cpuc)->nr--;if (!cpuc->lbr_entries[i].from)continue ; }
@@ -208,13 +208,13 @@ void amd_pmu_lbr_read(void) out++; } - cpuc->lbr_stack.nr = out; +cpu_lbr_stack (cpuc)->nr = out;/* * Internal register renaming always ensures that LBR From[0] and * LBR To[0] always represent the TOS */ - cpuc->lbr_stack.hw_idx =0 ; +cpu_lbr_stack (cpuc)->hw_idx =0 ;/* Perform further software filtering */ amd_pmu_lbr_filter ();
arch/x86/events/core.c +1 -1
@@ -1706,7 +1706,7 @@ int x86_pmu_handle_irq(struct pt_regs *regs)perf_sample_data_init (&data,0 , event->hw.last_period);if (has_branch_stack (event)) -perf_sample_save_brstack (&data, event, &cpuc->lbr_stack,NULL ); +perf_sample_save_brstack (&data, event,cpu_lbr_stack (cpuc),NULL );if (perf_event_overflow (event, &data, regs))x86_pmu_stop (event,0 );
arch/x86/events/intel/ds.c +2 -2
@@ -1573,7 +1573,7 @@ static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)/* * No LBR entry, no basic block, no rewinding */ -if (!cpuc->lbr_stack.nr || !from || !to) +if (!cpu_lbr_stack (cpuc)->nr || !from || !to)return 0 ;/*
@@ -1869,7 +1869,7 @@ static void setup_pebs_fixed_sample_data(struct perf_event *event,setup_pebs_time (event, data, pebs->tsc);if (has_branch_stack (event)) -perf_sample_save_brstack (data, event, &cpuc->lbr_stack,NULL ); +perf_sample_save_brstack (data, event,cpu_lbr_stack (cpuc),NULL ); } static void adaptive_pebs_save_regs(struct pt_regs *regs,
arch/x86/events/intel/lbr.c +14 -14
@@ -751,8 +751,8 @@ void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc) br->to = msr_lastbranch.to; br++; } - cpuc->lbr_stack.nr = i; - cpuc->lbr_stack.hw_idx = tos; +cpu_lbr_stack (cpuc)->nr = i; +cpu_lbr_stack (cpuc)->hw_idx = tos; }/*
@@ -846,8 +846,8 @@ void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc) br[out].cycles = cycles; out++; } - cpuc->lbr_stack.nr = out; - cpuc->lbr_stack.hw_idx = tos; +cpu_lbr_stack (cpuc)->nr = out; +cpu_lbr_stack (cpuc)->hw_idx = tos; }static DEFINE_STATIC_KEY_FALSE (x86_lbr_mispred);
@@ -930,7 +930,7 @@ static void intel_pmu_store_lbr(struct cpu_hw_events *cpuc, e->reserved = (info >>LBR_INFO_BR_CNTR_OFFSET ) &LBR_INFO_BR_CNTR_FULL_MASK ; } - cpuc->lbr_stack.nr = i; +cpu_lbr_stack (cpuc)->nr = i; }/*
@@ -956,7 +956,7 @@ static void intel_pmu_lbr_counters_reorder(struct cpu_hw_events *cpuc,WARN_ON_ONCE (!pos); -for (i =0 ; i < cpuc->lbr_stack.nr; i++) { +for (i =0 ; i <cpu_lbr_stack (cpuc)->nr; i++) { src = cpuc->lbr_entries[i].reserved; dst =0 ;for (j =0 ; j < pos; j++) {
@@ -974,11 +974,11 @@ void intel_pmu_lbr_save_brstack(struct perf_sample_data *data, {if (is_branch_counters_group (event)) {intel_pmu_lbr_counters_reorder (cpuc, event); -perf_sample_save_brstack (data, event, &cpuc->lbr_stack, cpuc->lbr_counters); +perf_sample_save_brstack (data, event,cpu_lbr_stack (cpuc), cpuc->lbr_counters);return ; } -perf_sample_save_brstack (data, event, &cpuc->lbr_stack,NULL ); +perf_sample_save_brstack (data, event,cpu_lbr_stack (cpuc),NULL ); }static void intel_pmu_arch_lbr_read (struct cpu_hw_events *cpuc)
@@ -1210,7 +1210,7 @@ intel_pmu_lbr_filter(struct cpu_hw_events *cpuc) ((br_sel & X86_BR_TYPE_SAVE) != X86_BR_TYPE_SAVE)) return; -for (i =0 ; i < cpuc->lbr_stack.nr; i++) { +for (i =0 ; i <cpu_lbr_stack (cpuc)->nr; i++) { from = cpuc->lbr_entries[i].from; to = cpuc->lbr_entries[i].to;
@@ -1248,14 +1248,14 @@ intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)return ;/* remove all entries with from=0 */ -for (i =0 ; i < cpuc->lbr_stack.nr; ) { +for (i =0 ; i <cpu_lbr_stack (cpuc)->nr; ) {if (!cpuc->lbr_entries[i].from) { j = i; -while (++j < cpuc->lbr_stack.nr) { +while (++j <cpu_lbr_stack (cpuc)->nr) { cpuc->lbr_entries[j-1 ] = cpuc->lbr_entries[j]; cpuc->lbr_counters[j-1 ] = cpuc->lbr_counters[j]; } - cpuc->lbr_stack.nr--; +cpu_lbr_stack (cpuc)->nr--;if (!cpuc->lbr_entries[i].from)continue ; }
@@ -1270,9 +1270,9 @@ void intel_pmu_store_pebs_lbrs(struct lbr_entry *lbr)/* Cannot get TOS for large PEBS and Arch LBR */ if (static_cpu_has (X86_FEATURE_ARCH_LBR ) || (cpuc->n_pebs == cpuc->n_large_pebs)) - cpuc->lbr_stack.hw_idx =-1ULL ; +cpu_lbr_stack (cpuc)->hw_idx =-1ULL ;else - cpuc->lbr_stack.hw_idx =intel_pmu_lbr_tos (); +cpu_lbr_stack (cpuc)->hw_idx =intel_pmu_lbr_tos ();intel_pmu_store_lbr (cpuc, lbr);intel_pmu_lbr_filter (cpuc);
arch/x86/events/perf_event.h +6 -1
@@ -286,7 +286,7 @@ struct cpu_hw_events {*/ int lbr_users;int lbr_pebs_users; -struct perf_branch_stack lbr_stack; +u64 lbr_hdr[sizeof (struct perf_branch_stack )/sizeof (u64)];struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES ];u64 lbr_counters[MAX_LBR_ENTRIES ];/* branch stack extra */ union {
@@ -349,6 +349,11 @@ struct cpu_hw_events {struct pmu *pmu; }; +static __always_inline struct perf_branch_stack *cpu_lbr_stack(struct cpu_hw_events *cpuc) +{ +return (void *)&cpuc->lbr_hdr; +} +#define __EVENT_CONSTRAINT_RANGE (c, e, n, m, w, o, f) { \ { .idxmsk64 = (n) }, \ .code = (c), \