Skip to contentSkip to conversation

perf/x86/intel/uncore: Use struct_size() in kzalloc_node()

PATCH New Gustavo A. R. Silva 1 participant 1 message Jun 20, 2020

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

PATCH 1/1 perf/x86/intel/uncore: Use struct_size() in kzalloc_node()

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes. Also, remove unnecessary variable _size_.

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

---
 arch/x86/events/intel/uncore.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/x86/events/intel/uncore.c +2 -4
index cf76d6631afa..958c6baaf200 100644
@@ -313,12 +313,10 @@ 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.27.0