Skip to contentSkip to conversation

perf/x86/amd/uncore: turn amd_uncore_ctx events into a flexible array

PATCH RESEND Applied Rosen Penev 4 participants 4 messages Sep 16 – Sep 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 perf/x86/amd/uncore: turn amd_uncore_ctx events into a flexible array

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
---
 arch/x86/events/amd/uncore.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
arch/x86/events/amd/uncore.c +3 -12
index 7181973b5b12..74b9c92d517a 100644
@@ -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 {
@@ -519,10 +519,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;
 	}
@@ -567,18 +565,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;
Signature · 1 line
2.55.0
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

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

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Rosen Penev
Quoted 52 lines · Rosen Penev

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
---
 arch/x86/events/amd/uncore.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
arch/x86/events/amd/uncore.c +3 -5
index 7181973b5b12..74b9c92d517a 100644
@@ -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 {
@@ -519,10 +519,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;
 	}
@@ -567,18 +565,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;

arch/x86/events/amd/uncore.c static int amd_uncore_ctx_init(struct amd_uncore *uncore, unsigned int cpu) · line 575

Quoted 10 lines
arch/x86/events/amd/uncore.c +0 -7
-			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;
merge bot Applied
#3 lore raw

[tip: perf/core] perf/x86/amd/uncore: Turn amd_uncore_ctx events into a flexible array

In reply to Rosen Penev
The following commit has been merged into the perf/core branch of tip:

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     df935e26ca9a8fd4ac7431b73348938250f80c6e
Gitweb:        https://git.kernel.org/tip/df935e26ca9a8fd4ac7431b73348938250f80c6e
Author:        Rosen Penev <[email protected]>
AuthorDate:    Tue, 15 Sep 2026 17:45:16 -07:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Fri, 25 Sep 2026 12:45:52 +02:00

perf/x86/amd/uncore: Turn amd_uncore_ctx events into a flexible array

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.

---
 arch/x86/events/amd/uncore.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
arch/x86/events/amd/uncore.c +3 -12
index 7181973..74b9c92 100644
@@ -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 {
@@ -519,10 +519,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;
 	}
@@ -567,18 +565,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;