* [PATCH] perf/x86/amd/uncore: turn amd_uncore_ctx events into a flexible array
@ 2026-05-23 1:19 Rosen Penev
2026-09-15 20:58 ` Ian Rogers
0 siblings, 1 reply; 3+ messages in thread
From: Rosen Penev @ 2026-05-23 1:19 UTC (permalink / raw)
To: linux-perf-users
Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Thomas Gleixner,
Borislav Petkov, Dave Hansen,
maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT),
H. Peter Anvin, open list:PERFORMANCE EVENTS SUBSYSTEM
The events pointer was kzalloc_node()'d immediately after the parent
struct allocation, with the count (pmu->num_counters) trivially
available beforehand. Move events to the struct tail as a flexible
array member and fold the two allocations into a single kzalloc_node()
with struct_size(), dropping the separate kfree(ctx->events) in the
free path.
Assisted-by: Claude:Opus-4.7
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
arch/x86/events/amd/uncore.c | 15 +++------------
1 file changed, 3 insertions(+), 12 deletions(-)
diff --git a/arch/x86/events/amd/uncore.c b/arch/x86/events/amd/uncore.c
index 05cff39968ec..0e265fc1fef8 100644
--- a/arch/x86/events/amd/uncore.c
+++ b/arch/x86/events/amd/uncore.c
@@ -39,11 +39,11 @@ static int pmu_version;
struct amd_uncore_ctx {
int refcnt;
int cpu;
- struct perf_event **events;
unsigned long active_mask[BITS_TO_LONGS(NUM_COUNTERS_MAX)];
int nr_active;
struct hrtimer hrtimer;
u64 hrtimer_duration;
+ struct perf_event *events[];
};
struct amd_uncore_pmu {
@@ -488,10 +488,8 @@ static void amd_uncore_ctx_free(struct amd_uncore *uncore, unsigned int cpu)
if (cpu == ctx->cpu)
cpumask_clear_cpu(cpu, &pmu->active_mask);
- if (!--ctx->refcnt) {
- kfree(ctx->events);
+ if (!--ctx->refcnt)
kfree(ctx);
- }
*per_cpu_ptr(pmu->ctx, cpu) = NULL;
}
@@ -536,18 +534,11 @@ static int amd_uncore_ctx_init(struct amd_uncore *uncore, unsigned int cpu)
/* Allocate context if sibling does not exist */
if (!curr) {
node = cpu_to_node(cpu);
- curr = kzalloc_node(sizeof(*curr), GFP_KERNEL, node);
+ curr = kzalloc_node(struct_size(curr, events, pmu->num_counters), GFP_KERNEL, node);
if (!curr)
goto fail;
curr->cpu = cpu;
- curr->events = kzalloc_node(sizeof(*curr->events) *
- pmu->num_counters,
- GFP_KERNEL, node);
- if (!curr->events) {
- kfree(curr);
- goto fail;
- }
amd_uncore_init_hrtimer(curr);
curr->hrtimer_duration = (u64)update_interval * NSEC_PER_MSEC;
--
2.54.0
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] perf/x86/amd/uncore: turn amd_uncore_ctx events into a flexible array
2026-05-23 1:19 [PATCH] perf/x86/amd/uncore: turn amd_uncore_ctx events into a flexible array Rosen Penev
@ 2026-09-15 20:58 ` Ian Rogers
2026-09-16 0:14 ` Mi, Dapeng
0 siblings, 1 reply; 3+ messages in thread
From: Ian Rogers @ 2026-09-15 20:58 UTC (permalink / raw)
To: Rosen Penev, Dapeng Mi
Cc: linux-perf-users, Peter Zijlstra, Ingo Molnar,
Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Adrian Hunter, James Clark,
Thomas Gleixner, Borislav Petkov, Dave Hansen,
maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT),
H. Peter Anvin, open list:PERFORMANCE EVENTS SUBSYSTEM
On Fri, May 22, 2026 at 6:19 PM Rosen Penev <rosenp@gmail.com> wrote:
>
> The events pointer was kzalloc_node()'d immediately after the parent
> struct allocation, with the count (pmu->num_counters) trivially
> available beforehand. Move events to the struct tail as a flexible
> array member and fold the two allocations into a single kzalloc_node()
> with struct_size(), dropping the separate kfree(ctx->events) in the
> free path.
>
> Assisted-by: Claude:Opus-4.7
> Signed-off-by: Rosen Penev <rosenp@gmail.com>
> ---
> arch/x86/events/amd/uncore.c | 15 +++------------
> 1 file changed, 3 insertions(+), 12 deletions(-)
>
> diff --git a/arch/x86/events/amd/uncore.c b/arch/x86/events/amd/uncore.c
> index 05cff39968ec..0e265fc1fef8 100644
> --- a/arch/x86/events/amd/uncore.c
> +++ b/arch/x86/events/amd/uncore.c
> @@ -39,11 +39,11 @@ static int pmu_version;
> struct amd_uncore_ctx {
> int refcnt;
> int cpu;
> - struct perf_event **events;
> unsigned long active_mask[BITS_TO_LONGS(NUM_COUNTERS_MAX)];
> int nr_active;
> struct hrtimer hrtimer;
> u64 hrtimer_duration;
> + struct perf_event *events[];
As the number of counters is fixed and events doesn't grow, this makes
sense to me. Dapeng, what do you think?
Thanks,
Ian
> };
>
> struct amd_uncore_pmu {
> @@ -488,10 +488,8 @@ static void amd_uncore_ctx_free(struct amd_uncore *uncore, unsigned int cpu)
> if (cpu == ctx->cpu)
> cpumask_clear_cpu(cpu, &pmu->active_mask);
>
> - if (!--ctx->refcnt) {
> - kfree(ctx->events);
> + if (!--ctx->refcnt)
> kfree(ctx);
> - }
>
> *per_cpu_ptr(pmu->ctx, cpu) = NULL;
> }
> @@ -536,18 +534,11 @@ static int amd_uncore_ctx_init(struct amd_uncore *uncore, unsigned int cpu)
> /* Allocate context if sibling does not exist */
> if (!curr) {
> node = cpu_to_node(cpu);
> - curr = kzalloc_node(sizeof(*curr), GFP_KERNEL, node);
> + curr = kzalloc_node(struct_size(curr, events, pmu->num_counters), GFP_KERNEL, node);
> if (!curr)
> goto fail;
>
> curr->cpu = cpu;
> - curr->events = kzalloc_node(sizeof(*curr->events) *
> - pmu->num_counters,
> - GFP_KERNEL, node);
> - if (!curr->events) {
> - kfree(curr);
> - goto fail;
> - }
>
> amd_uncore_init_hrtimer(curr);
> curr->hrtimer_duration = (u64)update_interval * NSEC_PER_MSEC;
> --
> 2.54.0
>
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] perf/x86/amd/uncore: turn amd_uncore_ctx events into a flexible array
2026-09-15 20:58 ` Ian Rogers
@ 2026-09-16 0:14 ` Mi, Dapeng
0 siblings, 0 replies; 3+ messages in thread
From: Mi, Dapeng @ 2026-09-16 0:14 UTC (permalink / raw)
To: Ian Rogers, Rosen Penev
Cc: linux-perf-users, Peter Zijlstra, Ingo Molnar,
Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Adrian Hunter, James Clark,
Thomas Gleixner, Borislav Petkov, Dave Hansen,
maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT),
H. Peter Anvin, open list:PERFORMANCE EVENTS SUBSYSTEM
On 9/16/2026 4:58 AM, Ian Rogers wrote:
> On Fri, May 22, 2026 at 6:19 PM Rosen Penev <rosenp@gmail.com> wrote:
>> The events pointer was kzalloc_node()'d immediately after the parent
>> struct allocation, with the count (pmu->num_counters) trivially
>> available beforehand. Move events to the struct tail as a flexible
>> array member and fold the two allocations into a single kzalloc_node()
>> with struct_size(), dropping the separate kfree(ctx->events) in the
>> free path.
>>
>> Assisted-by: Claude:Opus-4.7
>> Signed-off-by: Rosen Penev <rosenp@gmail.com>
>> ---
>> arch/x86/events/amd/uncore.c | 15 +++------------
>> 1 file changed, 3 insertions(+), 12 deletions(-)
>>
>> diff --git a/arch/x86/events/amd/uncore.c b/arch/x86/events/amd/uncore.c
>> index 05cff39968ec..0e265fc1fef8 100644
>> --- a/arch/x86/events/amd/uncore.c
>> +++ b/arch/x86/events/amd/uncore.c
>> @@ -39,11 +39,11 @@ static int pmu_version;
>> struct amd_uncore_ctx {
>> int refcnt;
>> int cpu;
>> - struct perf_event **events;
>> unsigned long active_mask[BITS_TO_LONGS(NUM_COUNTERS_MAX)];
>> int nr_active;
>> struct hrtimer hrtimer;
>> u64 hrtimer_duration;
>> + struct perf_event *events[];
> As the number of counters is fixed and events doesn't grow, this makes
> sense to me. Dapeng, what do you think?
It looks good to me. But it seems there is no Sashikio review, so better
resend to trigger Sashiko review for double check. Thanks.
>
> Thanks,
> Ian
>
>> };
>>
>> struct amd_uncore_pmu {
>> @@ -488,10 +488,8 @@ static void amd_uncore_ctx_free(struct amd_uncore *uncore, unsigned int cpu)
>> if (cpu == ctx->cpu)
>> cpumask_clear_cpu(cpu, &pmu->active_mask);
>>
>> - if (!--ctx->refcnt) {
>> - kfree(ctx->events);
>> + if (!--ctx->refcnt)
>> kfree(ctx);
>> - }
>>
>> *per_cpu_ptr(pmu->ctx, cpu) = NULL;
>> }
>> @@ -536,18 +534,11 @@ static int amd_uncore_ctx_init(struct amd_uncore *uncore, unsigned int cpu)
>> /* Allocate context if sibling does not exist */
>> if (!curr) {
>> node = cpu_to_node(cpu);
>> - curr = kzalloc_node(sizeof(*curr), GFP_KERNEL, node);
>> + curr = kzalloc_node(struct_size(curr, events, pmu->num_counters), GFP_KERNEL, node);
>> if (!curr)
>> goto fail;
>>
>> curr->cpu = cpu;
>> - curr->events = kzalloc_node(sizeof(*curr->events) *
>> - pmu->num_counters,
>> - GFP_KERNEL, node);
>> - if (!curr->events) {
>> - kfree(curr);
>> - goto fail;
>> - }
>>
>> amd_uncore_init_hrtimer(curr);
>> curr->hrtimer_duration = (u64)update_interval * NSEC_PER_MSEC;
>> --
>> 2.54.0
>>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-09-16 0:15 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-23 1:19 [PATCH] perf/x86/amd/uncore: turn amd_uncore_ctx events into a flexible array Rosen Penev
2026-09-15 20:58 ` Ian Rogers
2026-09-16 0:14 ` Mi, Dapeng
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®