Skip to contentSkip to conversation

staging: wilc1000: Return correct error codes

PATCH Superseded Amitoj Kaur Chawla 3 participants 8 messages Feb 17, 2016 Staging

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 staging: wilc1000: Return correct error codes

This change has been made with the goal that kernel functions should return something more descriptive than -1 on failure.

The return value of alloc_etherdev on failure should return a -ENOMEM and not a -1. This was found using Coccinelle. A simplified version of the semantic patch used is:

//<smpl>
@@
expression *e;
identifier l1;
@@

e = alloc_etherdev(...);
if (e == NULL) {
...
return -1
+ -ENOMEM
;
}
//</smpl

Furthermore, return -ENODEV on failure of register_netdev() instead of -1.

The two call sites store the return value in a variable which only checks that the value is non-zero, hence no change is required at the call site.

---
Changes in v3:
-Further checked call sites to understand how the value is
being analyzed
Changes in v2:
-Corrected semantic patch

 drivers/staging/wilc1000/linux_wlan.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/wilc1000/linux_wlan.c +2 -2
index a731b46..4f8b21d 100644
@@ -1333,7 +1333,7 @@ int wilc_netdev_init(struct wilc **wilc, struct device *dev, int io_type,
 	for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
 		ndev = alloc_etherdev(sizeof(struct wilc_vif));
 		if (!ndev)
-			return -1;
+			return -ENOMEM;
 
 		vif = netdev_priv(ndev);
 		memset(vif, 0, sizeof(struct wilc_vif));
@@ -1373,7 +1373,7 @@ int wilc_netdev_init(struct wilc **wilc, struct device *dev, int io_type,
 		}
 
 		if (register_netdev(ndev))
-			return -1;
+			return -ENODEV;
 
 		vif->iftype = STATION_MODE;
 		vif->mac_opened = 0;
Signature · 1 line
1.9.1
#1 lore raw
Quoted 22 lines · Amitoj Kaur Chawla

This change has been made with the goal that kernel functions should return something more descriptive than -1 on failure.

The return value of alloc_etherdev on failure should return a -ENOMEM and not a -1. This was found using Coccinelle. A simplified version of the semantic patch used is:

//<smpl>
@@
expression *e;
identifier l1;
@@

e = alloc_etherdev(...);
if (e == NULL) {
...
return -1
+ -ENOMEM
;
}
//</smpl

Quoted 2 lines

Furthermore, return -ENODEV on failure of register_netdev() instead of -1.

There is yet another failure that returns -1 in this function, on wilc_create_wiphy. That one should be fixed too.

Also, the call to the latter function is in a nested { }. I guess this is to be able to declare the variable wdev, but I don't see why this variable can't be declared at the top of the function, like all the others.

Quoted 3 lines

The two call sites store the return value in a variable which only checks that the value is non-zero, hence no change is required at the call site.

This is fine now.

julia

Quoted 43 lines

---
Changes in v3:
-Further checked call sites to understand how the value is
being analyzed
Changes in v2:
-Corrected semantic patch

 drivers/staging/wilc1000/linux_wlan.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/wilc1000/linux_wlan.c +2 -3
index a731b46..4f8b21d 100644
@@ -1333,7 +1333,7 @@ int wilc_netdev_init(struct wilc **wilc, struct device *dev, int io_type,
 	for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
 		ndev = alloc_etherdev(sizeof(struct wilc_vif));
 		if (!ndev)
-			return -1;
+			return -ENOMEM;
 
 		vif = netdev_priv(ndev);
 		memset(vif, 0, sizeof(struct wilc_vif));
@@ -1373,7 +1373,7 @@ int wilc_netdev_init(struct wilc **wilc, struct device *dev, int io_type,
 		}
 
 		if (register_netdev(ndev))
-			return -1;
+			return -ENODEV;
 
 		vif->iftype = STATION_MODE;
 		vif->mac_opened = 0;
--

1.9.1

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20160217124204.GA30394%40amitoj-Inspiron-3542.
For more options, visit https://groups.google.com/d/optout.

#2 lore raw
In reply to Amitoj Kaur Chawla

drivers/staging/wilc1000/linux_wlan.c int wilc_netdev_init(struct wilc **wilc, struct device *dev, int io_type, · line 1379

Quoted 11 lines · On Wednesday 17 February 2016 18:12:05 Amitoj Kaur Chawla wrote:
drivers/staging/wilc1000/linux_wlan.c +1 -1
@@ -1373,7 +1373,7 @@ int wilc_netdev_init(struct wilc **wilc, struct device *dev, int io_type,
                }
 
                if (register_netdev(ndev))
-                       return -1;
+                       return -ENODEV;
 
                vif->iftype = STATION_MODE;
                vif->mac_opened = 0;

I would use whatever return code that register_netdev() returns here.

Arnd

#3 lore raw
Quoted 4 lines · Arnd Bergmann

I would use whatever return code that register_netdev() returns here.

Arnd

I can change it to that although I wrote the following script after your comment to find the general pattern in the kernel:
@@
@@

if (register_netdev(...)) {
...
* return ...;
}

and most of the cases, there are quite some results in drivers/net/ return -ENODEV in the same scenario.

Would you still prefer to return the code that register_netdev() returns?

Amitoj

#4 lore raw
Quoted 16 lines · On Wednesday 17 February 2016 18:43:01 Amitoj Kaur Chawla wrote:
Quoted 4 lines · Arnd Bergmann

I would use whatever return code that register_netdev() returns here.

Arnd

I can change it to that although I wrote the following script after your comment to find the general pattern in the kernel:
@@
@@

if (register_netdev(...)) {
...
* return ...;
}

Quoted 6 lines

and most of the cases, there are quite some results in drivers/net/ return -ENODEV in the same scenario.

Would you still prefer to return the code that register_netdev() returns?

Yes, I think it's better: The reason for register_netdev failing is almost certainly not that the device is not there, so -ENODEV is a bit misleading.

Arnd

#5 lore raw
Quoted 5 lines · Arnd Bergmann

Yes, I think it's better: The reason for register_netdev failing is almost certainly not that the device is not there, so -ENODEV is a bit misleading.

Arnd

Okay. Will redo and send v4.

Thanks for the feedback,

Amitoj

#6 lore raw
In reply to Arnd Bergmann
Quoted 12 lines · Arnd Bergmann
Quoted 11 lines · On Wednesday 17 February 2016 18:43:01 Amitoj Kaur Chawla wrote:
Quoted 4 lines · Arnd Bergmann

I would use whatever return code that register_netdev() returns here.

Arnd

I can change it to that although I wrote the following script after your comment to find the general pattern in the kernel:
@@
@@

Quoted 4 lines
Quoted 4 lines

if (register_netdev(...)) {
...
* return ...;
}

Amitoj, you can also write another script to see how many calls propagate the error. You will miss them with the above, because they don't have the register_netdev call directly in the if test. Maybe there are far more that propagate than that follow the above pattern.

julia

Quoted 21 lines
Quoted 7 lines

and most of the cases, there are quite some results in drivers/net/ return -ENODEV in the same scenario.

Would you still prefer to return the code that register_netdev() returns?

Yes, I think it's better: The reason for register_netdev failing is almost certainly not that the device is not there, so -ENODEV is a bit misleading.

Arnd

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/3731725.Uhb0JIDMJg%40wuerfel.
For more options, visit https://groups.google.com/d/optout.

#7 lore raw
Quoted 7 lines · Julia Lawall

Amitoj, you can also write another script to see how many calls propagate the error. You will miss them with the above, because they don't have the register_netdev call directly in the if test. Maybe there are far more that propagate than that follow the above pattern.

julia

Oh okay will do. I just wrote the above one quickly to find cases. I can write a better one to check for all possible cases of register_netdev and what is returned on failure.

Amitoj