Skip to contentSkip to conversation

sparc: Fine-tuning for three function implementations

PATCH In review SF Markus Elfring 3 participants 7 messages Aug 25, 2016 – Aug 29, 2016

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

cover sparc: Fine-tuning for three function implementations

From: Markus Elfring <[email protected]>
Date: Thu, 25 Aug 2016 10:05:45 +0200

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

Markus Elfring (2):
Use kmalloc_array()
Delete an unnecessary initialisation in led_proc_write()

 arch/sparc/kernel/led.c          | 4 ++--
 arch/sparc/kernel/nmi.c          | 4 +++-
 arch/sparc/kernel/sys_sparc_64.c | 6 ++++--
 3 files changed, 9 insertions(+), 5 deletions(-)
Signature · 1 line
2.9.3
#1 lore raw

PATCH 1/2 sparc: Use kmalloc_array() in three functions

From: Markus Elfring <[email protected]>
Date: Thu, 25 Aug 2016 09:52:44 +0200

* Multiplications for the size determination of memory allocations indicated that array data structures should be processed. Thus reuse the corresponding function "kmalloc_array".

This issue was detected by using the Coccinelle software.

* Replace the specifications of data types by pointer dereferences to make the corresponding size determination a bit safer according to the Linux coding style convention.

---
 arch/sparc/kernel/led.c          | 2 +-
 arch/sparc/kernel/nmi.c          | 4 +++-
 arch/sparc/kernel/sys_sparc_64.c | 6 ++++--
 3 files changed, 8 insertions(+), 4 deletions(-)
arch/sparc/kernel/led.c +1 -1
index 3ae36f3..7444e4a 100644
@@ -69,7 +69,7 @@ static ssize_t led_proc_write(struct file *file, const char __user *buffer,
 	if (count > LED_MAX_LENGTH)
 		count = LED_MAX_LENGTH;
 
-	buf = kmalloc(sizeof(char) * (count + 1), GFP_KERNEL);
+	buf = kmalloc_array(count + 1, sizeof(*buf), GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 
arch/sparc/kernel/nmi.c +3 -1
index a9973bb..b88e97b 100644
@@ -166,7 +166,9 @@ static int __init check_nmi_watchdog(void)
 	if (!atomic_read(&nmi_active))
 		return 0;
 
-	prev_nmi_count = kmalloc(nr_cpu_ids * sizeof(unsigned int), GFP_KERNEL);
+	prev_nmi_count = kmalloc_array(nr_cpu_ids,
+				       sizeof(*prev_nmi_count),
+				       GFP_KERNEL);
 	if (!prev_nmi_count) {
 		err = -ENOMEM;
 		goto error;
arch/sparc/kernel/sys_sparc_64.c +4 -1
index fe8b8ee..25b7a14 100644
@@ -580,8 +580,10 @@ SYSCALL_DEFINE5(utrap_install, utrap_entry_t, type,
 			unsigned long *p = current_thread_info()->utraps;
 
 			current_thread_info()->utraps -				kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long),
-					GFP_KERNEL);
+				kmalloc_array(UT_TRAP_INSTRUCTION_31 + 1,
+					      sizeof(*current_thread_info()
+						     ->utraps),
+					      GFP_KERNEL);
 			if (!current_thread_info()->utraps) {
 				current_thread_info()->utraps = p;
 				return -ENOMEM;
Signature · 1 line
2.9.3
#6 lore raw

PATCH 2/2 sparc: Delete an unnecessary initialisation in led_proc_write()

In reply to SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 25 Aug 2016 09:55:22 +0200

The local variable "buf" will be set to an appropriate value a bit later. Thus omit the explicit initialisation at the beginning.

---
 arch/sparc/kernel/led.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/sparc/kernel/led.c +1 -1
index 7444e4a..d6671af 100644
@@ -64,7 +64,7 @@ static int led_proc_open(struct inode *inode, struct file *file)
 static ssize_t led_proc_write(struct file *file, const char __user *buffer,
 			      size_t count, loff_t *ppos)
 {
-	char *buf = NULL;
+	char *buf;
 
 	if (count > LED_MAX_LENGTH)
 		count = LED_MAX_LENGTH;
Signature · 1 line
2.9.3
#2 lore raw

Re: [PATCH 1/2] sparc: Use kmalloc_array() in three functions

In reply to SF Markus Elfring

arch/sparc/kernel/led.c static ssize_t led_proc_write(struct file *file, const char __user *buffer, · line 75

Quoted 11 lines · SF Markus Elfring
arch/sparc/kernel/led.c +1 -1
@@ -69,7 +69,7 @@ static ssize_t led_proc_write(struct file *file, const char __user *buffer,
 	if (count > LED_MAX_LENGTH)
 		count = LED_MAX_LENGTH;
 
-	buf = kmalloc(sizeof(char) * (count + 1), GFP_KERNEL);
+	buf = kmalloc_array(count + 1, sizeof(*buf), GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 

Here it's probably best to just remove sizeof(char) completely, as it's 1 by definition.

Paolo

#3 lore raw

Re: [PATCH 1/2] sparc: Use kmalloc_array() in three functions

Quoted 4 lines
Quoted 4 lines
arch/sparc/kernel/led.c +0 -0
@@ -69,7 +69,7 @@ static ssize_t led_proc_write(struct file *file, const char __user *buffer,
 	if (count > LED_MAX_LENGTH)
 		count = LED_MAX_LENGTH;
 
Quoted 8 lines

arch/sparc/kernel/led.c static ssize_t led_proc_write(struct file *file, const char __user *buffer, · line 75

Quoted 5 lines
arch/sparc/kernel/led.c +1 -1
-	buf = kmalloc(sizeof(char) * (count + 1), GFP_KERNEL);
+	buf = kmalloc_array(count + 1, sizeof(*buf), GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 

Here it's probably best to just remove sizeof(char) completely, as it's 1 by definition.

Would you like to be prepared anyhow that the shown buffer variable could eventually store other data than ordinary single byte characters?

Regards,
Markus

#4 lore raw

Re: [PATCH 1/2] sparc: Use kmalloc_array() in three functions

In reply to Paolo Bonzini

Am 29.08.2016 11:47, schrieb Paolo Bonzini:

Quoted 18 lines · SF Markus Elfring

arch/sparc/kernel/led.c static ssize_t led_proc_write(struct file *file, const char __user *buffer, · line 75

Quoted 11 lines
arch/sparc/kernel/led.c +1 -1
@@ -69,7 +69,7 @@ static ssize_t led_proc_write(struct file *file, const char __user *buffer,
 	if (count > LED_MAX_LENGTH)
 		count = LED_MAX_LENGTH;
 
-	buf = kmalloc(sizeof(char) * (count + 1), GFP_KERNEL);
+	buf = kmalloc_array(count + 1, sizeof(*buf), GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 

Here it's probably best to just remove sizeof(char) completely, as it's 1 by definition.

Quoted 1 line

Paolo

I would not do so, if you ever change buf to something else it would break, having a sizeof(*buf) here is harmless and a known pattern.

re,
wh

#5 lore raw

Re: [PATCH 1/2] sparc: Use kmalloc_array() in three functions

Quoted 11 lines · walter harms
Quoted 8 lines

arch/sparc/kernel/led.c static ssize_t led_proc_write(struct file *file, const char __user *buffer, · line 74

Quoted 5 lines
arch/sparc/kernel/led.c +1 -1
-	buf = kmalloc(sizeof(char) * (count + 1), GFP_KERNEL);
+	buf = kmalloc_array(count + 1, sizeof(*buf), GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;

Here it's probably best to just remove sizeof(char) completely, as it's 1 by definition.

I would not do so, if you ever change buf to something else it would break, having a sizeof(*buf) here is harmless and a known pattern.

Fair enough, it's not code I maintain.

Paolo