UM: Fine-tuning for some function implementations
Subsystems (2)
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
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
@@ -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
PATCH 2/5 um: port: Delete three error messages for a failed memory allocation
In reply to SF Markus ElfringFrom: 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
@@ -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
PATCH 3/5 um: port: Improve size determinations in port_data()
In reply to SF Markus ElfringFrom: 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
@@ -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
PATCH 4/5 um: ubd: Move two assignments for the variable "err" in ubd_remove()
In reply to SF Markus ElfringFrom: 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
@@ -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
PATCH 5/5 um: ubd: Improve size determinations in do_ubd_request()
In reply to SF Markus ElfringFrom: 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
@@ -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
Re: [PATCH 1/5] um: port: Move an assignment for the variable "fd" in port_wait()
In reply to SF Markus ElfringQuoted 21 lines · SF Markus Elfring
arch/um/drivers/port_kern.c +3
-3@@ -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)
Re: [PATCH 2/5] um: port: Delete three error messages for a failed memory allocation
In reply to SF Markus ElfringQuoted 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
-4conn =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)
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 +0100A 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" inport_wait()
port: Delete three error messages for a failed memory allocation
port: Improve size determinations inport_data()
ubd: Move two assignments for the variable "err" inubd_remove()
ubd: Improve size determinations indo_ubd_request()
please don't send drive-by patches.
Please see:
marc.info/?i 170111225640.14e5d962@bbrezillon
Thanks,
//richard
Re: [PATCH 2/5] um: port: Delete three error messages for a failed memory allocation
In reply to Jeff DikeQuoted 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
-4conn =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
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
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
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)