Skip to contentSkip to conversation

perf/x86/amd/uncore: Add flex array to struct amd_uncore_ctx

PATCH New Erick Archer 1 participant 3 messages Jun 1, 2024 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v4 1/3 perf/x86/amd/uncore: Add flex array to struct amd_uncore_ctx

This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2].

The "struct amd_uncore_ctx" can be refactored to use a flex array for the "events" member. This way, the allocation/freeing of the memory can be simplified.

Specifically, as the "curr" variable is a pointer to the amd_uncore_ctx structure and it now ends up in a flexible array:

struct amd_uncore_ctx {
        [...]
        struct perf_event *events[];
};

the two-step allocation can be simplifief by using just one kzalloc_node function and the struct_size() helper to do the arithmetic calculation for the memory to be allocated.

This way, the code is more readable and safer.

---
 arch/x86/events/amd/uncore.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)
arch/x86/events/amd/uncore.c +5 -13
index 0fafe233bba4..9d43048bfdab 100644
@@ -37,8 +37,8 @@ static int pmu_version;
 struct amd_uncore_ctx {
 	int refcnt;
 	int cpu;
-	struct perf_event **events;
 	struct hlist_node node;
+	struct perf_event *events[];
 };
 
 struct amd_uncore_pmu {
@@ -430,10 +430,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;
 	}
@@ -478,19 +476,13 @@ 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;
-			}
-
 			cpumask_set_cpu(cpu, &pmu->active_mask);
 		}
 
Signature · 1 line
2.25.1
#2 lore raw

PATCH v4 2/3 perf/x86/intel/uncore: Prefer struct_size over open coded arithmetic

In reply to a message not in the archive

This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2].

As the "box" variable is a pointer to "struct intel_uncore_box" and this structure ends in a flexible array:

struct intel_uncore_box {
	[...]
	struct intel_uncore_extra_reg shared_regs[];
};

the preferred way in the kernel is to use the struct_size() helper to do the arithmetic instead of the calculation "size + count * size" in the kzalloc_node() function.

This way, the code is more readable and safer.

This code was detected with the help of Coccinelle, and audited and modified manually.

---
 arch/x86/events/intel/uncore.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
arch/x86/events/intel/uncore.c +3 -4
index 419c517b8594..b0d518d752aa 100644
@@ -350,12 +350,11 @@ static void uncore_pmu_init_hrtimer(struct intel_uncore_box *box)
 static struct intel_uncore_box *uncore_alloc_box(struct intel_uncore_type *type,
 						 int node)
 {
-	int i, size, numshared = type->num_shared_regs ;
+	int i, numshared = type->num_shared_regs;
 	struct intel_uncore_box *box;
 
-	size = sizeof(*box) + numshared * sizeof(struct intel_uncore_extra_reg);
-
-	box = kzalloc_node(size, GFP_KERNEL, node);
+	box = kzalloc_node(struct_size(box, shared_regs, numshared), GFP_KERNEL,
+			   node);
 	if (!box)
 		return NULL;
 
Signature · 1 line
2.25.1
#3 lore raw

PATCH v4 3/3 perf/ring_buffer: Prefer struct_size over open coded arithmetic

In reply to a message not in the archive

This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2].

As the "rb" variable is a pointer to "struct perf_buffer" and this structure ends in a flexible array:

struct perf_buffer {
	[...]
	void	*data_pages[];
};

the preferred way in the kernel is to use the struct_size() helper to do the arithmetic instead of the calculation "size + count * size" in the kzalloc_node() functions.

In the "rb_alloc" function defined in the else branch of the macro

#ifndef CONFIG_PERF_USE_VMALLOC

the count in the struct_size helper is the literal "1" since only one pointer to void is allocated. Also, remove the "size" variable as it is no longer needed.

At the same time, prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions). In this case, it is important to note that the logic needs a little refactoring to ensure that the "nr_pages" member is initialized before the first access to the flex array.

In one case, it is only necessary to move the "nr_pages" assignment before the array-writing loop while in the other case the access to the flex array needs to be moved inside the if branch and after the "nr_pages" assignment.

This way, the code is more safer.

This code was detected with the help of Coccinelle, and audited and modified manually.

---
 kernel/events/internal.h    |  2 +-
 kernel/events/ring_buffer.c | 15 ++++-----------
 2 files changed, 5 insertions(+), 12 deletions(-)
kernel/events/internal.h +1 -1
index 5150d5f84c03..dc8d39b01adb 100644
@@ -55,7 +55,7 @@ struct perf_buffer {
 	void				*aux_priv;
 
 	struct perf_event_mmap_page	*user_page;
-	void				*data_pages[];
+	void				*data_pages[] __counted_by(nr_pages);
 };
 
 extern void rb_free(struct perf_buffer *rb);
kernel/events/ring_buffer.c +4 -11
index 4013408ce012..d123fa2096cf 100644
@@ -822,9 +822,7 @@ struct perf_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
 	unsigned long size;
 	int i, node;
 
-	size = sizeof(struct perf_buffer);
-	size += nr_pages * sizeof(void *);
-
+	size = struct_size(rb, data_pages, nr_pages);
 	if (order_base_2(size) > PAGE_SHIFT+MAX_PAGE_ORDER)
 		goto fail;
 
@@ -833,6 +831,7 @@ struct perf_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
 	if (!rb)
 		goto fail;
 
+	rb->nr_pages = nr_pages;
 	rb->user_page = perf_mmap_alloc_page(cpu);
 	if (!rb->user_page)
 		goto fail_user_page;
@@ -843,8 +842,6 @@ struct perf_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
 			goto fail_data_pages;
 	}
 
-	rb->nr_pages = nr_pages;
-
 	ring_buffer_init(rb, watermark, flags);
 
 	return rb;
@@ -916,15 +913,11 @@ void rb_free(struct perf_buffer *rb)
 struct perf_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
 {
 	struct perf_buffer *rb;
-	unsigned long size;
 	void *all_buf;
 	int node;
 
-	size = sizeof(struct perf_buffer);
-	size += sizeof(void *);
-
 	node = (cpu == -1) ? cpu : cpu_to_node(cpu);
-	rb = kzalloc_node(size, GFP_KERNEL, node);
+	rb = kzalloc_node(struct_size(rb, data_pages, 1), GFP_KERNEL, node);
 	if (!rb)
 		goto fail;
 
@@ -935,9 +928,9 @@ struct perf_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
 		goto fail_all_buf;
 
 	rb->user_page = all_buf;
-	rb->data_pages[0] = all_buf + PAGE_SIZE;
 	if (nr_pages) {
 		rb->nr_pages = 1;
+		rb->data_pages[0] = all_buf + PAGE_SIZE;
 		rb->page_order = ilog2(nr_pages);
 	}
 
Signature · 1 line
2.25.1