Skip to contentSkip to conversation

sparc: Replace one-element array with flexible array member

PATCH Superseded Thorsten Blum 2 participants 4 messages Nov 11, 2024 SPARC + UltraSPARC (sparc/sparc64)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sparc: Replace one-element array with flexible array member

Replace the deprecated one-element array with a modern flexible array member in the struct hvtramp_descr.

Additionally, 15 unnecessary bytes are allocated for hdesc, but instead of fixing the parentheses in the open-coded version, use struct_size() to calculate the correct number of bytes.

---
 arch/sparc/include/asm/hvtramp.h | 2 +-
 arch/sparc/kernel/smp_64.c       | 4 +---
 2 files changed, 2 insertions(+), 4 deletions(-)
arch/sparc/include/asm/hvtramp.h +1 -1
index 688ea43af0f5..ce2453ea4f2b 100644
@@ -17,7 +17,7 @@ struct hvtramp_descr {
 	__u64			fault_info_va;
 	__u64			fault_info_pa;
 	__u64			thread_reg;
-	struct hvtramp_mapping	maps[1];
+	struct hvtramp_mapping	maps[];
 };
 
 void hv_cpu_startup(unsigned long hvdescr_pa);
arch/sparc/kernel/smp_64.c +1 -3
index e40c395db202..24d980220bf1 100644
@@ -297,9 +297,7 @@ static void ldom_startcpu_cpuid(unsigned int cpu, unsigned long thread_reg,
 	unsigned long hv_err;
 	int i;
 
-	hdesc = kzalloc(sizeof(*hdesc) +
-			(sizeof(struct hvtramp_mapping) *
-			 num_kernel_image_mappings - 1),
+	hdesc = kzalloc(struct_size(hdesc, maps, num_kernel_image_mappings - 1),
 			GFP_KERNEL);
 	if (!hdesc) {
 		printk(KERN_ERR "ldom_startcpu_cpuid: Cannot allocate "
Signature · 1 line
2.47.0
#1 lore raw
Quoted 4 lines · Thorsten Blum

Replace the deprecated one-element array with a modern flexible array member in the struct hvtramp_descr.

Additionally, 15 unnecessary bytes are allocated for hdesc, but instead

15? unnecessary?

Quoted 15 lines

of fixing the parentheses in the open-coded version, use struct_size() to calculate the correct number of bytes.

---
  arch/sparc/include/asm/hvtramp.h | 2 +-
  arch/sparc/kernel/smp_64.c       | 4 +---
  2 files changed, 2 insertions(+), 4 deletions(-)
arch/sparc/include/asm/hvtramp.h +0 -0
index 688ea43af0f5..ce2453ea4f2b 100644

arch/sparc/include/asm/hvtramp.h struct hvtramp_descr { · line 21

Quoted 6 lines
arch/sparc/include/asm/hvtramp.h +1 -1
@@ -17,7 +17,7 @@ struct hvtramp_descr {
  	__u64			fault_info_va;
  	__u64			fault_info_pa;
  	__u64			thread_reg;
-	struct hvtramp_mapping	maps[1];
+	struct hvtramp_mapping	maps[];
  };

It seems this struct is a candidate for __counted_by()

Quoted 7 lines
arch/sparc/include/asm/hvtramp.h +0 -0
  void hv_cpu_startup(unsigned long hvdescr_pa);
arch/sparc/kernel/smp_64.c +0 -0
index e40c395db202..24d980220bf1 100644

arch/sparc/kernel/smp_64.c static void ldom_startcpu_cpuid(unsigned int cpu, unsigned long thread_reg, · line 301

Quoted 8 lines
arch/sparc/kernel/smp_64.c +1 -3
@@ -297,9 +297,7 @@ static void ldom_startcpu_cpuid(unsigned int cpu, unsigned long thread_reg,
  	unsigned long hv_err;
  	int i;
 
-	hdesc = kzalloc(sizeof(*hdesc) +
-			(sizeof(struct hvtramp_mapping) *
-			 num_kernel_image_mappings - 1),
+	hdesc = kzalloc(struct_size(hdesc, maps, num_kernel_image_mappings - 1),
  			GFP_KERNEL);

Now the code is broken because it's allocating num_kernel_image_mappings - 1 elements instead of num_kernel_image_mappings.

--
Gustavo

#2 lore raw
Quoted 6 lines · Gustavo A. R. Silva
Quoted 3 lines · Thorsten Blum

Replace the deprecated one-element array with a modern flexible array member in the struct hvtramp_descr.
Additionally, 15 unnecessary bytes are allocated for hdesc, but instead

15? unnecessary?

hvtramp_mapping is 16 bytes and the size is calculated as follows:

(16 * num_kernel_image_mappings - 1)

which is 15 bytes too many for any number of mappings because hdesc includes the first map. It probably should have been:

16 * (num_kernel_image_mappings - 1)

unless I'm missing something.

Quoted 1 line

It seems this struct is a candidate for __counted_by()

Yes, but sparc doesn't seem to support it?

Quoted 2 lines

Now the code is broken because it's allocating num_kernel_image_mappings - 1 elements instead of num_kernel_image_mappings.

Ah sorry, missed that and will fix in v2 shortly.

Thanks,
Thorsten

#3 lore raw
Quoted 8 lines · Thorsten Blum
Quoted 6 lines · Gustavo A. R. Silva
Quoted 3 lines · Thorsten Blum

Replace the deprecated one-element array with a modern flexible array member in the struct hvtramp_descr.
Additionally, 15 unnecessary bytes are allocated for hdesc, but instead

15? unnecessary?

Quoted 8 lines

hvtramp_mapping is 16 bytes and the size is calculated as follows:

   (16 * num_kernel_image_mappings - 1)

which is 15 bytes too many for any number of mappings because hdesc includes the first map. It probably should have been:

   16 * (num_kernel_image_mappings - 1)

Ah yes, that opening parenthesis before sizeof(struct hvtramp_mapping) was misplaced.

Quoted 11 lines

unless I'm missing something.

Quoted 1 line

It seems this struct is a candidate for __counted_by()

Yes, but sparc doesn't seem to support it?

Quoted 2 lines

Now the code is broken because it's allocating num_kernel_image_mappings - 1 elements instead of num_kernel_image_mappings.

Ah sorry, missed that and will fix in v2 shortly.

Thanks
--
Gustavo