Skip to contentSkip to conversation

x86/amd_nb: Use kmalloc_array() in amd_cache_gart()

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

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

PATCH 1/1 x86/amd_nb: Use kmalloc_array() in amd_cache_gart()

From: Markus Elfring <[email protected]>
Date: Sun, 4 Sep 2016 21:01:47 +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.

---
 arch/x86/kernel/amd_nb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/amd_nb.c +3 -1
index 4fdf623..e2e3e8a 100644
@@ -226,7 +226,9 @@ static void amd_cache_gart(void)
 	if (!amd_nb_has_feature(AMD_NB_GART))
 		return;
 
-	flush_words = kmalloc(amd_nb_num() * sizeof(u32), GFP_KERNEL);
+	flush_words = kmalloc_array(amd_nb_num(),
+				    sizeof(*flush_words),
+				    GFP_KERNEL);
 	if (!flush_words) {
 		amd_northbridges.flags &= ~AMD_NB_GART;
 		pr_notice("Cannot initialize GART flush words, GART support disabled\n");
Signature · 1 line
2.9.3
#1 lore raw
Quoted 9 lines · SF Markus Elfring

* 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.

[]

arch/x86/kernel/amd_nb.c

Quoted 1 line
arch/x86/kernel/amd_nb.c +0 -0

[]

arch/x86/kernel/amd_nb.c static void amd_cache_gart(void) · line 231

Quoted 8 lines
arch/x86/kernel/amd_nb.c +0 -0
@@ -226,7 +226,9 @@ static void amd_cache_gart(void)
  	if (!amd_nb_has_feature(AMD_NB_GART))
  		return;

arch/x86/kernel/amd_nb.c +3 -1
-	flush_words = kmalloc(amd_nb_num() * sizeof(u32), GFP_KERNEL);
+	flush_words = kmalloc_array(amd_nb_num(),
+				    sizeof(*flush_words),
+				    GFP_KERNEL);

Markus, this sort of change really isn't very useful.

amd_nb_num is a u16 and is really just a count of
northbridges in the system.

It'll never be a particular high number that could possibly overflow a size_t of num * sizeof(u32).

Detecting an inconsistency is different than an issue that has any real significance.

Please stop blindly converting these from one form to another.  Please inspect these potential changes first and find instances where this conversion could actually matter before sending auto-generated patches.

--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html