Skip to contentSkip to conversation

x86/smpboot: Fine-tuning for smp_init_package_map()

PATCH In review SF Markus Elfring 3 participants 8 messages Sep 5, 2016

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

cover x86/smpboot: Fine-tuning for smp_init_package_map()

From: Markus Elfring <[email protected]>
Date: Mon, 5 Sep 2016 09:50:05 +0200

A few update suggestions were taken into account
from static source code analysis.

Markus Elfring (4):
Use kmalloc_array()
Return directly after a failed kmalloc_array()
Replace one kzalloc() call by kcalloc()
Return directly after a failed kcalloc()

 arch/x86/kernel/smpboot.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
Signature · 1 line
2.9.3
#1 lore raw

PATCH 1/4 x86/smpboot: Use kmalloc_array() in smp_init_package_map()

From: Markus Elfring <[email protected]>
Date: Mon, 5 Sep 2016 08:30:20 +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.

* Replace the specification of a data type by a pointer dereference to make the corresponding size determination a bit safer according to the Linux coding style convention.

* Move a calculation for one function call.

---
 arch/x86/kernel/smpboot.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/kernel/smpboot.c +6 -3
index 3878725..36cf27e 100644
@@ -349,9 +349,12 @@ static void __init smp_init_package_map(void)
 	 * package can be smaller than the actual used apic ids.
 	 */
 	max_physical_pkg_id = DIV_ROUND_UP(MAX_LOCAL_APIC, ncpus);
-	size = max_physical_pkg_id * sizeof(unsigned int);
-	physical_to_logical_pkg = kmalloc(size, GFP_KERNEL);
-	memset(physical_to_logical_pkg, 0xff, size);
+	physical_to_logical_pkg = kmalloc_array(max_physical_pkg_id,
+						sizeof(*physical_to_logical_pkg),
+						GFP_KERNEL);
+	memset(physical_to_logical_pkg,
+	       0xff,
+	       sizeof(*physical_to_logical_pkg) * max_physical_pkg_id);
 	size = BITS_TO_LONGS(max_physical_pkg_id) * sizeof(unsigned long);
 	physical_package_map = kzalloc(size, GFP_KERNEL);
 
Signature · 1 line
2.9.3
#3 lore raw

PATCH 2/4 x86/smpboot: Return directly after a failed kmalloc_array()

In reply to SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Mon, 5 Sep 2016 08:48:32 +0200

Return directly after a memory allocation failed at the beginning.

---
 arch/x86/kernel/smpboot.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kernel/smpboot.c +2 -0
index 36cf27e..2ce06ef 100644
@@ -352,6 +352,8 @@ static void __init smp_init_package_map(void)
 	physical_to_logical_pkg = kmalloc_array(max_physical_pkg_id,
 						sizeof(*physical_to_logical_pkg),
 						GFP_KERNEL);
+	if (!physical_to_logical_pkg)
+		return;
 	memset(physical_to_logical_pkg,
 	       0xff,
 	       sizeof(*physical_to_logical_pkg) * max_physical_pkg_id);
Signature · 1 line
2.9.3
#4 lore raw

PATCH 3/4 x86/smpboot: Replace one kzalloc() call by kcalloc()

In reply to SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Mon, 5 Sep 2016 09:29:40 +0200

* The script "checkpatch.pl" can point information out like the following.

WARNING: Prefer kcalloc over kzalloc with multiply

Thus fix the affected source code place.

* Replace the specification of a data type by a pointer dereference to make the corresponding size determination a bit safer according to the Linux coding style convention.

* Delete the local variable "size" which became unnecessary with this refactoring.

---
 arch/x86/kernel/smpboot.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/smpboot.c +3 -3
index 2ce06ef..e3fdc44 100644
@@ -311,7 +311,6 @@ EXPORT_SYMBOL(topology_phys_to_logical_pkg);
 static void __init smp_init_package_map(void)
 {
 	unsigned int ncpus, cpu;
-	size_t size;
 
 	/*
 	 * Today neither Intel nor AMD support heterogenous systems. That
@@ -357,8 +356,9 @@ static void __init smp_init_package_map(void)
 	memset(physical_to_logical_pkg,
 	       0xff,
 	       sizeof(*physical_to_logical_pkg) * max_physical_pkg_id);
-	size = BITS_TO_LONGS(max_physical_pkg_id) * sizeof(unsigned long);
-	physical_package_map = kzalloc(size, GFP_KERNEL);
+	physical_package_map = kcalloc(BITS_TO_LONGS(max_physical_pkg_id),
+				       sizeof(*physical_package_map),
+				       GFP_KERNEL);
 
 	for_each_present_cpu(cpu) {
 		unsigned int apicid = apic->cpu_present_to_apicid(cpu);
Signature · 1 line
2.9.3
#7 lore raw

PATCH 4/4 x86/smpboot: Return directly after a failed kcalloc()

In reply to SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Mon, 5 Sep 2016 09:39:05 +0200

Return directly after a memory allocation failed in this function.

---
 arch/x86/kernel/smpboot.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/kernel/smpboot.c +4 -0
index e3fdc44..a30f625 100644
@@ -359,6 +359,10 @@ static void __init smp_init_package_map(void)
 	physical_package_map = kcalloc(BITS_TO_LONGS(max_physical_pkg_id),
 				       sizeof(*physical_package_map),
 				       GFP_KERNEL);
+	if (!physical_package_map) {
+		kfree(physical_to_logical_pkg);
+		return;
+	}
 
 	for_each_present_cpu(cpu) {
 		unsigned int apicid = apic->cpu_present_to_apicid(cpu);
Signature · 1 line
2.9.3
#2 lore raw

Re: [PATCH 1/4] x86/smpboot: Use kmalloc_array() in smp_init_package_map()

In reply to SF Markus Elfring
Quoted 27 lines · SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Mon, 5 Sep 2016 08:30:20 +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.

* Replace the specification of a data type by a pointer dereference to make the corresponding size determination a bit safer according to the Linux coding style convention.

* Move a calculation for one function call.

---
 arch/x86/kernel/smpboot.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/kernel/smpboot.c +0 -0
index 3878725..36cf27e 100644
@@ -349,9 +349,12 @@ static void __init smp_init_package_map(void)
 	 * package can be smaller than the actual used apic ids.
 	 */

arch/x86/kernel/smpboot.c static void __init smp_init_package_map(void) · line 357

Quoted 10 lines
arch/x86/kernel/smpboot.c +6 -3
 	max_physical_pkg_id = DIV_ROUND_UP(MAX_LOCAL_APIC, ncpus);
-	size = max_physical_pkg_id * sizeof(unsigned int);
-	physical_to_logical_pkg = kmalloc(size, GFP_KERNEL);
-	memset(physical_to_logical_pkg, 0xff, size);
+	physical_to_logical_pkg = kmalloc_array(max_physical_pkg_id,
+						sizeof(*physical_to_logical_pkg),
+						GFP_KERNEL);
+	memset(physical_to_logical_pkg,
+	       0xff,
+	       sizeof(*physical_to_logical_pkg) * max_physical_pkg_id);

Why are you replacing well readable code by this line breaked crap?

Stop acting like a shell script or we might replace you by one.

Thanks,

tglx

#5 lore raw

Re: [PATCH 3/4] x86/smpboot: Replace one kzalloc() call by kcalloc()

In reply to SF Markus Elfring
Quoted 34 lines · SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Mon, 5 Sep 2016 09:29:40 +0200

* The script "checkpatch.pl" can point information out like the following.

WARNING: Prefer kcalloc over kzalloc with multiply

Thus fix the affected source code place.

* Replace the specification of a data type by a pointer dereference to make the corresponding size determination a bit safer according to the Linux coding style convention.

* Delete the local variable "size" which became unnecessary with this refactoring.

---
 arch/x86/kernel/smpboot.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/smpboot.c +0 -1
index 2ce06ef..e3fdc44 100644
@@ -311,7 +311,6 @@ EXPORT_SYMBOL(topology_phys_to_logical_pkg);
 static void __init smp_init_package_map(void)
 {
 	unsigned int ncpus, cpu;
-	size_t size;
 
 	/*
 	 * Today neither Intel nor AMD support heterogenous systems. That

arch/x86/kernel/smpboot.c static void __init smp_init_package_map(void) · line 361

Quoted 8 lines
arch/x86/kernel/smpboot.c +3 -2
@@ -357,8 +356,9 @@ static void __init smp_init_package_map(void)
 	memset(physical_to_logical_pkg,
 	       0xff,
 	       sizeof(*physical_to_logical_pkg) * max_physical_pkg_id);
-	size = BITS_TO_LONGS(max_physical_pkg_id) * sizeof(unsigned long);
-	physical_package_map = kzalloc(size, GFP_KERNEL);
+	physical_package_map = kcalloc(BITS_TO_LONGS(max_physical_pkg_id),
+				       sizeof(*physical_package_map),
+				       GFP_KERNEL);

This is bitmap_alloc.

Paolo

Quoted 3 lines
arch/x86/kernel/smpboot.c +0 -0
 	for_each_present_cpu(cpu) {
 		unsigned int apicid = apic->cpu_present_to_apicid(cpu);
 
#6 lore raw

Re: [PATCH 3/4] x86/smpboot: Replace one kzalloc() call by kcalloc()

Quoted 34 lines · Paolo Bonzini
Quoted 31 lines · SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Mon, 5 Sep 2016 09:29:40 +0200

* The script "checkpatch.pl" can point information out like the following.

WARNING: Prefer kcalloc over kzalloc with multiply

Thus fix the affected source code place.

* Replace the specification of a data type by a pointer dereference to make the corresponding size determination a bit safer according to the Linux coding style convention.

* Delete the local variable "size" which became unnecessary with this refactoring.

---
 arch/x86/kernel/smpboot.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/smpboot.c +0 -1
index 2ce06ef..e3fdc44 100644
@@ -311,7 +311,6 @@ EXPORT_SYMBOL(topology_phys_to_logical_pkg);
 static void __init smp_init_package_map(void)
 {
 	unsigned int ncpus, cpu;
-	size_t size;
 
Quoted 13 lines

arch/x86/kernel/smpboot.c static void __init smp_init_package_map(void) · line 361

Quoted 11 lines
arch/x86/kernel/smpboot.c +3 -2
 	/*
 	 * Today neither Intel nor AMD support heterogenous systems. That
@@ -357,8 +356,9 @@ static void __init smp_init_package_map(void)
 	memset(physical_to_logical_pkg,
 	       0xff,
 	       sizeof(*physical_to_logical_pkg) * max_physical_pkg_id);
-	size = BITS_TO_LONGS(max_physical_pkg_id) * sizeof(unsigned long);
-	physical_package_map = kzalloc(size, GFP_KERNEL);
+	physical_package_map = kcalloc(BITS_TO_LONGS(max_physical_pkg_id),
+				       sizeof(*physical_package_map),
+				       GFP_KERNEL);

This is bitmap_alloc.

Doh, bitmap_alloc is only in tools/include/linux/bitmap.h, but perhaps it makes sense to add it to include/linux/bitmap.h or include/linux/slab.h too...

Paolo