Skip to contentSkip to conversation

s390/hypfs: Use kmalloc_array() in diag0c_store()

PATCH In review SF Markus Elfring 4 participants 4 messages Sep 1, 2016

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

PATCH 1/1 s390/hypfs: Use kmalloc_array() in diag0c_store()

From: Markus Elfring <[email protected]>
Date: Thu, 1 Sep 2016 11:30:58 +0200

A multiplication for the size determination of a memory allocation indicated that an array data structure should be processed. Thus use the corresponding function "kmalloc_array".

This issue was detected by using the Coccinelle software.

---
 arch/s390/hypfs/hypfs_diag0c.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/s390/hypfs/hypfs_diag0c.c +3 -1
index 0f1927c..61418a8 100644
@@ -48,7 +48,9 @@ static void *diag0c_store(unsigned int *count)
 
 	get_online_cpus();
 	cpu_count = num_online_cpus();
-	cpu_vec = kmalloc(sizeof(*cpu_vec) * num_possible_cpus(), GFP_KERNEL);
+	cpu_vec = kmalloc_array(num_possible_cpus(),
+				sizeof(*cpu_vec),
+				GFP_KERNEL);
 	if (!cpu_vec)
 		goto fail_put_online_cpus;
 	/* Note: Diag 0c needs 8 byte alignment and real storage */
Signature · 1 line
2.9.3
#1 lore raw
Quoted 19 lines · SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 1 Sep 2016 11:30:58 +0200

A multiplication for the size determination of a memory allocation indicated that an array data structure should be processed. Thus use the corresponding function "kmalloc_array".

This issue was detected by using the Coccinelle software.

---
 arch/s390/hypfs/hypfs_diag0c.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/s390/hypfs/hypfs_diag0c.c +0 -0
index 0f1927c..61418a8 100644

arch/s390/hypfs/hypfs_diag0c.c static void *diag0c_store(unsigned int *count) · line 53

Quoted 7 lines
arch/s390/hypfs/hypfs_diag0c.c +3 -1
@@ -48,7 +48,9 @@ static void *diag0c_store(unsigned int *count)
 
 	get_online_cpus();
 	cpu_count = num_online_cpus();
-	cpu_vec = kmalloc(sizeof(*cpu_vec) * num_possible_cpus(), GFP_KERNEL);
+	cpu_vec = kmalloc_array(num_possible_cpus(),
+				sizeof(*cpu_vec),
+				GFP_KERNEL);

How does this improve the situation? For any real life scenario this can't overflow, but it does add an extra (pointless) runtime check, since num_possible_cpus() is not a compile time constant.

So, why is this an "issue"?

#2 lore raw
Quoted 32 lines · Heiko Carstens
Quoted 19 lines · SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 1 Sep 2016 11:30:58 +0200

A multiplication for the size determination of a memory allocation indicated that an array data structure should be processed. Thus use the corresponding function "kmalloc_array".

This issue was detected by using the Coccinelle software.

---
 arch/s390/hypfs/hypfs_diag0c.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/s390/hypfs/hypfs_diag0c.c +0 -0
index 0f1927c..61418a8 100644

arch/s390/hypfs/hypfs_diag0c.c static void *diag0c_store(unsigned int *count) · line 53

Quoted 7 lines
arch/s390/hypfs/hypfs_diag0c.c +3 -1
@@ -48,7 +48,9 @@ static void *diag0c_store(unsigned int *count)
 
 	get_online_cpus();
 	cpu_count = num_online_cpus();
-	cpu_vec = kmalloc(sizeof(*cpu_vec) * num_possible_cpus(), GFP_KERNEL);
+	cpu_vec = kmalloc_array(num_possible_cpus(),
+				sizeof(*cpu_vec),
+				GFP_KERNEL);

How does this improve the situation? For any real life scenario this can't overflow, but it does add an extra (pointless) runtime check, since num_possible_cpus() is not a compile time constant.

Quoted 1 line

So, why is this an "issue"?

It's not an issue but I for one still prefer consistent use of kmalloc_array and kcalloc.

Paolo

#3 lore raw

Am Thu, 1 Sep 2016 17:39:02 +0200
schrieb Paolo Bonzini <[email protected]>:

Quoted 36 lines · Heiko Carstens
Quoted 33 lines · SF Markus Elfring
Quoted 19 lines

From: Markus Elfring <[email protected]>
Date: Thu, 1 Sep 2016 11:30:58 +0200

A multiplication for the size determination of a memory allocation indicated that an array data structure should be processed. Thus use the corresponding function "kmalloc_array".

This issue was detected by using the Coccinelle software.

---
 arch/s390/hypfs/hypfs_diag0c.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/arch/s390/hypfs/hypfs_diag0c.c index 0f1927c..61418a8 100644

arch/s390/hypfs/hypfs_diag0c.c static void *diag0c_store(unsigned int *count) · line 54

Quoted 8 lines
arch/s390/hypfs/hypfs_diag0c.c +3 -1
@@ -48,7 +48,9 @@ static void *diag0c_store(unsigned int *count)
 
 	get_online_cpus();
 	cpu_count = num_online_cpus();
-	cpu_vec = kmalloc(sizeof(*cpu_vec) * num_possible_cpus(),
 GFP_KERNEL);
+	cpu_vec = kmalloc_array(num_possible_cpus(),
+				sizeof(*cpu_vec),
+				GFP_KERNEL);

How does this improve the situation? For any real life scenario this can't overflow, but it does add an extra (pointless) runtime check, since num_possible_cpus() is not a compile time constant.

Quoted 4 lines
Quoted 1 line

So, why is this an "issue"?

It's not an issue but I for one still prefer consistent use of kmalloc_array and kcalloc.

Hello Paolo,

I will keep this in mind for future code, but would prefer not changing this now.

Michael