Skip to contentSkip to conversation

percpu: convert flexible array initializers to use struct_size()

PATCH In review Dennis Zhou 2 participants 2 messages Oct 30, 2020

Subsystems (2)
lore
Share & tools
lore

Dennis Zhou

author maintainer
#0 lore raw

PATCH 1/1 percpu: convert flexible array initializers to use struct_size()

Use the safer macro as sparked by the long discussion in [1].

[1] https://lore.kernel.org/lkml/[email protected]/

---
I'll apply it to for-5.10-fixes.

 mm/percpu.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/percpu.c +4 -4
index 66a93f096394..ad7a37ee74ef 100644
@@ -1315,8 +1315,8 @@ static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr,
 	region_size = ALIGN(start_offset + map_size, lcm_align);
 
 	/* allocate chunk */
-	alloc_size = sizeof(struct pcpu_chunk) +
-		BITS_TO_LONGS(region_size >> PAGE_SHIFT) * sizeof(unsigned long);
+	alloc_size = struct_size(chunk, populated,
+				 BITS_TO_LONGS(region_size >> PAGE_SHIFT));
 	chunk = memblock_alloc(alloc_size, SMP_CACHE_BYTES);
 	if (!chunk)
 		panic("%s: Failed to allocate %zu bytes\n", __func__,
@@ -2521,8 +2521,8 @@ void __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
 	pcpu_unit_pages = ai->unit_size >> PAGE_SHIFT;
 	pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT;
 	pcpu_atom_size = ai->atom_size;
-	pcpu_chunk_struct_size = sizeof(struct pcpu_chunk) +
-		BITS_TO_LONGS(pcpu_unit_pages) * sizeof(unsigned long);
+	pcpu_chunk_struct_size = struct_size(chunk, populated,
+					     BITS_TO_LONGS(pcpu_unit_pages));
 
 	pcpu_stats_save_ai(ai);
 
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Dennis Zhou

Use the safer macro as sparked by the long discussion in [1].

[1] https://lore.kernel.org/lkml/[email protected]/

Thanks
--
Gustavo

Quoted 33 lines

---
I'll apply it to for-5.10-fixes.

 mm/percpu.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/percpu.c +4 -4
index 66a93f096394..ad7a37ee74ef 100644
@@ -1315,8 +1315,8 @@ static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr,
 	region_size = ALIGN(start_offset + map_size, lcm_align);
 
 	/* allocate chunk */
-	alloc_size = sizeof(struct pcpu_chunk) +
-		BITS_TO_LONGS(region_size >> PAGE_SHIFT) * sizeof(unsigned long);
+	alloc_size = struct_size(chunk, populated,
+				 BITS_TO_LONGS(region_size >> PAGE_SHIFT));
 	chunk = memblock_alloc(alloc_size, SMP_CACHE_BYTES);
 	if (!chunk)
 		panic("%s: Failed to allocate %zu bytes\n", __func__,
@@ -2521,8 +2521,8 @@ void __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
 	pcpu_unit_pages = ai->unit_size >> PAGE_SHIFT;
 	pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT;
 	pcpu_atom_size = ai->atom_size;
-	pcpu_chunk_struct_size = sizeof(struct pcpu_chunk) +
-		BITS_TO_LONGS(pcpu_unit_pages) * sizeof(unsigned long);
+	pcpu_chunk_struct_size = struct_size(chunk, populated,
+					     BITS_TO_LONGS(pcpu_unit_pages));
 
 	pcpu_stats_save_ai(ai);