Skip to contentSkip to conversation

UM: Fine-tuning for some function implementations

PATCH NAK SF Markus Elfring 4 participants 13 messages Jan 18, 2017 – Jan 20, 2017

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

cover UM: Fine-tuning for some function implementations

From: Markus Elfring <[email protected]>
Date: Wed, 18 Jan 2017 22:48:02 +0100

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

Markus Elfring (5):
port: Move an assignment for the variable "fd" in port_wait()
port: Delete three error messages for a failed memory allocation
port: Improve size determinations in port_data()
ubd: Move two assignments for the variable "err" in ubd_remove()
ubd: Improve size determinations in do_ubd_request()

 arch/um/drivers/port_kern.c | 23 ++++++++---------------
 arch/um/drivers/ubd_kern.c  | 18 +++++++++---------
 2 files changed, 17 insertions(+), 24 deletions(-)
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/5 um: port: Move an assignment for the variable "fd" in port_wait()

From: Markus Elfring <[email protected]>
Date: Wed, 18 Jan 2017 21:40:29 +0100

A local variable was set to an error code in one case before a concrete error situation was detected. Thus move the corresponding assignment into an if branch to indicate a software failure there.

This issue was detected by using the Coccinelle software.

---
 arch/um/drivers/port_kern.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/um/drivers/port_kern.c +3 -3
index 40ca5cc275e9..b2bbda21c5f3 100644
@@ -230,10 +230,10 @@ int port_wait(void *data)
 
 	atomic_inc(&port->wait_count);
 	while (1) {
-		fd = -ERESTARTSYS;
-		if (wait_for_completion_interruptible(&port->done))
+		if (wait_for_completion_interruptible(&port->done)) {
+			fd = -ERESTARTSYS;
 			goto out;
-
+		}
 		spin_lock(&port->lock);
 
 		conn = list_entry(port->connections.next, struct connection,
Signature · 1 line
2.11.0
#3 lore raw

PATCH 2/5 um: port: Delete three error messages for a failed memory allocation

In reply to SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Wed, 18 Jan 2017 22:00:14 +0100

The script "checkpatch.pl" pointed information out like the following.

WARNING: Possible unnecessary 'out of memory' message

Thus fix the affected source code places.

---
 arch/um/drivers/port_kern.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
arch/um/drivers/port_kern.c +3 -10
index b2bbda21c5f3..c96741e920f5 100644
@@ -87,11 +87,8 @@ static int port_accept(struct port_list *port)
 	}
 
 	conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
-	if (conn = NULL) {
-		printk(KERN_ERR "port_accept : failed to allocate "
-		       "connection\n");
+	if (!conn)
 		goto out_close;
-	}
 	*conn = ((struct connection)
 		{ .list 	= LIST_HEAD_INIT(conn->list),
 		  .fd 		= fd,
@@ -170,10 +167,8 @@ void *port_data(int port_num)
 			goto found;
 	}
 	port = kmalloc(sizeof(struct port_list), GFP_KERNEL);
-	if (port = NULL) {
-		printk(KERN_ERR "Allocation of port list failed\n");
+	if (!port)
 		goto out;
-	}
 
 	fd = port_listen_fd(port_num);
 	if (fd < 0) {
@@ -202,10 +197,8 @@ void *port_data(int port_num)
 
  found:
 	dev = kmalloc(sizeof(struct port_dev), GFP_KERNEL);
-	if (dev = NULL) {
-		printk(KERN_ERR "Allocation of port device entry failed\n");
+	if (!dev)
 		goto out;
-	}
 
 	*dev = ((struct port_dev) { .port  		= port,
 				    .helper_pid  	= -1,
Signature · 1 line
2.11.0
#6 lore raw

PATCH 3/5 um: port: Improve size determinations in port_data()

In reply to SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Wed, 18 Jan 2017 22:20:14 +0100

Replace the specification of data structures by pointer dereferences as the parameter for the operator "sizeof" to make the corresponding size determination a bit safer.

---
 arch/um/drivers/port_kern.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/um/drivers/port_kern.c +2 -2
index c96741e920f5..60c1b43a8381 100644
@@ -166,7 +166,7 @@ void *port_data(int port_num)
 		if (port->port = port_num)
 			goto found;
 	}
-	port = kmalloc(sizeof(struct port_list), GFP_KERNEL);
+	port = kmalloc(sizeof(*port), GFP_KERNEL);
 	if (!port)
 		goto out;
 
@@ -196,7 +196,7 @@ void *port_data(int port_num)
 	list_add(&port->list, &ports);
 
  found:
-	dev = kmalloc(sizeof(struct port_dev), GFP_KERNEL);
+	dev = kmalloc(sizeof(*dev), GFP_KERNEL);
 	if (!dev)
 		goto out;
 
Signature · 1 line
2.11.0
#7 lore raw

PATCH 4/5 um: ubd: Move two assignments for the variable "err" in ubd_remove()

In reply to SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Wed, 18 Jan 2017 22:23:18 +0100

A local variable was set to an error code in two cases before a concrete error situation was detected. Thus move the corresponding assignments into if branches to indicate a software failure there.

This issue was detected by using the Coccinelle software.

---
 arch/um/drivers/ubd_kern.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
arch/um/drivers/ubd_kern.c +7 -5
index 85410279beab..6d686f735538 100644
@@ -1049,19 +1049,21 @@ static int ubd_remove(int n, char **error_out)
 {
 	struct gendisk *disk = ubd_gendisk[n];
 	struct ubd *ubd_dev;
-	int err = -ENODEV;
+	int err;
 
 	mutex_lock(&ubd_lock);
 
 	ubd_dev = &ubd_devs[n];
-
-	if(ubd_dev->file = NULL)
+	if (!ubd_dev->file) {
+		err = -ENODEV;
 		goto out;
+	}
 
 	/* you cannot remove a open disk */
-	err = -EBUSY;
-	if(ubd_dev->count > 0)
+	if (ubd_dev->count > 0) {
+		err = -EBUSY;
 		goto out;
+	}
 
 	ubd_gendisk[n] = NULL;
 	if(disk != NULL){
Signature · 1 line
2.11.0
#8 lore raw

PATCH 5/5 um: ubd: Improve size determinations in do_ubd_request()

In reply to SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Wed, 18 Jan 2017 22:38:04 +0100

Replace the specification of data structures by pointer dereferences as the parameter for the operator "sizeof" to make the corresponding size determination a bit safer.

---
 arch/um/drivers/ubd_kern.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/um/drivers/ubd_kern.c +2 -4
index 6d686f735538..50327d5a9a01 100644
@@ -1386,8 +1386,7 @@ static void do_ubd_request(struct request_queue *q)
 		req = dev->request;
 
 		if (req_op(req) = REQ_OP_FLUSH) {
-			io_req = kmalloc(sizeof(struct io_thread_req),
-					 GFP_ATOMIC);
+			io_req = kmalloc(sizeof(*io_req), GFP_ATOMIC);
 			if (io_req = NULL) {
 				if (list_empty(&dev->restart))
 					list_add(&dev->restart, &restart);
@@ -1401,8 +1400,7 @@ static void do_ubd_request(struct request_queue *q)
 		while(dev->start_sg < dev->end_sg){
 			struct scatterlist *sg = &dev->sg[dev->start_sg];
 
-			io_req = kmalloc(sizeof(struct io_thread_req),
-					 GFP_ATOMIC);
+			io_req = kmalloc(sizeof(*io_req), GFP_ATOMIC);
 			if(io_req = NULL){
 				if(list_empty(&dev->restart))
 					list_add(&dev->restart, &restart);
Signature · 1 line
2.11.0
#2 lore raw

Re: [PATCH 1/5] um: port: Move an assignment for the variable "fd" in port_wait()

In reply to SF Markus Elfring
Quoted 21 lines · SF Markus Elfring
arch/um/drivers/port_kern.c +3 -3
index 40ca5cc275e9..b2bbda21c5f3 100644
@@ -230,10 +230,10 @@ int port_wait(void *data)
 
 	atomic_inc(&port->wait_count);
 	while (1) {
-		fd = -ERESTARTSYS;
-		if (wait_for_completion_interruptible(&port->done))
+		if (wait_for_completion_interruptible(&port->done)) {
+			fd = -ERESTARTSYS;
 			goto out;
-
+		}
 		spin_lock(&port->lock);
 
 		conn = list_entry(port->connections.next, struct connection,
Signature · 1 line
2.11.0

The current code is pretty standard kernel coding style. The inline

    fd = -ERESTARTSYS;

is likely free, or close to it, and I believe that having anything besides a goto inside the if could make it significantly more expensive.

Jeff

Signature · 4 lines
Jeff Dike
AddToIt
978-254-0789 (o)
978-394-8986 (c)
#4 lore raw

Re: [PATCH 2/5] um: port: Delete three error messages for a failed memory allocation

In reply to SF Markus Elfring
Quoted 5 lines
arch/um/drivers/port_kern.c +0 -0
@@ -87,11 +87,8 @@ static int port_accept(struct port_list *port)
 	}
 

arch/um/drivers/port_kern.c static int port_accept(struct port_list *port) · line 94

Quoted 10 lines
arch/um/drivers/port_kern.c +1 -4
 	conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
-	if (conn = NULL) {
-		printk(KERN_ERR "port_accept : failed to allocate "
-		       "connection\n");
+	if (!conn)
 		goto out_close;
-	}
 	*conn = ((struct connection)
 		{ .list 	= LIST_HEAD_INIT(conn->list),
 		  .fd 		= fd,

I don't see how this eliminates a possible error. It should behave exactly the same. To me, this is an expressiveness issue.

!x is something you use with something that is conceptually a Boolean.

x = NULL is a question about a pointer, which is the case here.

Jeff

Signature · 4 lines
Jeff Dike
AddToIt
978-254-0789 (o)
978-394-8986 (c)
maintainer Nacked-by on the v1 series NAK
#9 lore raw
In reply to SF Markus Elfring

Markus,

Am 18.01.2017 um 22:55 schrieb SF Markus Elfring:

Quoted 6 lines

From: Markus Elfring <[email protected]>
Date: Wed, 18 Jan 2017 22:48:02 +0100

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

Quoted 6 lines

Markus Elfring (5):
port: Move an assignment for the variable "fd" in port_wait()
port: Delete three error messages for a failed memory allocation
port: Improve size determinations in port_data()
ubd: Move two assignments for the variable "err" in ubd_remove()
ubd: Improve size determinations in do_ubd_request()

please don't send drive-by patches.
Please see:
marc.info/?i 170111225640.14e5d962@bbrezillon

Thanks,
//richard

#5 lore raw

Re: [PATCH 2/5] um: port: Delete three error messages for a failed memory allocation

In reply to Jeff Dike
Quoted 4 lines
Quoted 4 lines

+++ b/arch/um/drivers/port_kern.c

(quoted diff) +0 -0
@@ -87,11 +87,8 @@ static int port_accept(struct port_list *port)
 	}
 
Quoted 12 lines

arch/um/drivers/port_kern.c static int port_accept(struct port_list *port) · line 94

Quoted 10 lines
arch/um/drivers/port_kern.c +1 -4
 	conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
-	if (conn = NULL) {
-		printk(KERN_ERR "port_accept : failed to allocate "
-		       "connection\n");
+	if (!conn)
 		goto out_close;
-	}
 	*conn = ((struct connection)
 		{ .list 	= LIST_HEAD_INIT(conn->list),
 		  .fd 		= fd,

I don't see how this eliminates a possible error.

The suggested change affects three coding style issues at this place.

* Repetition of an out-of-memory message

See also:
http://events.linuxfoundation.org/sites/events/files/slides/LCJ16-Refactor_Strings-WSang_0.pdf

* Unwanted splitting of a message string

* Usage of a specific preprocessor symbol

Quoted 1 line

!x is something you use with something that is conceptually a Boolean.

Pointers can be also treated in this way, can't they?

Regards,
Markus

#11 lore raw

How about tackling some real problems and performance issues instead?

There are a few of those in the network, interrupt and memory subsystems. Take your pick.

A.

Quoted 7 lines · SF Markus Elfring
Quoted 1 line

please don't send drive-by patches.

Would you dare to take another look at the published update steps in any other software combination?

Regards,
Markus

#12 lore raw
In reply to Richard Weinberger

The kmalloc(sizeof(struct foo), ...) => kmalloc(sizeof(*foo), ...) ones are OK.

The rest is cargo-cult programming.

Jeff

Signature · 4 lines
Jeff Dike
AddToIt
978-254-0789 (o)
978-394-8986 (c)