Skip to contentSkip to conversation

staging: wlags49_h2: Replace kmalloc+memset by kzalloc and add error handling.

PATCH Superseded Dan Carpenter 4 participants 8 messages Mar 23, 2012 – Mar 27, 2012

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

Review

Quoted 5 lines · santosh nayak
(quoted diff) +3 -2
-        dev_wds = kmalloc( sizeof( struct net_device ), GFP_KERNEL );
-        memset( dev_wds, 0, sizeof( struct net_device ));
+	dev_wds = kzalloc(sizeof(struct net_device), GFP_KERNEL);
+	if (unlikely(!dev_wds)) {
+		printk(KERN_ERR "%s: failed to alloc memory\n", __func__);

Don't resend, but kmalloc() already prints out way more extensive and useful error messages than this. No need to print an error message yourself normally.

regards,
dan carpenter

#1 lore raw

PATCH 1/1 staging: wlags49_h2: Replace kmalloc+memset by kzalloc and add error handling.

From: Santosh Nayak <[email protected]>

Replace kmalloc+memset pair by kzalloc() in 'wl_wds_device_alloc()'. Add error handling to avoid null derefernce.

---
 drivers/staging/wlags49_h2/wl_netdev.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
drivers/staging/wlags49_h2/wl_netdev.c +6 -2
index 9c16f54..426ba04 100644
@@ -1511,8 +1511,12 @@ void wl_wds_device_alloc( struct wl_private *lp )
     for( count = 0; count < NUM_WDS_PORTS; count++ ) {
         struct net_device *dev_wds = NULL;
 
-        dev_wds = kmalloc( sizeof( struct net_device ), GFP_KERNEL );
-        memset( dev_wds, 0, sizeof( struct net_device ));
+	dev_wds = kzalloc(sizeof(struct net_device), GFP_KERNEL);
+	if (unlikely(!dev_wds)) {
+		printk(KERN_ERR "%s: failed to alloc memory\n", __func__);
+		DBG_LEAVE(DbgInfo);
+		return;
+	}
 
         ether_setup( dev_wds );
 
Signature · 1 line
1.7.4.4
#2 lore raw
In reply to Dan Carpenter
Quoted 11 lines · Dan Carpenter
Quoted 5 lines · santosh nayak

-        dev_wds = kmalloc( sizeof( struct net_device ), GFP_KERNEL );
-        memset( dev_wds, 0, sizeof( struct net_device ));
+     dev_wds = kzalloc(sizeof(struct net_device), GFP_KERNEL);
+     if (unlikely(!dev_wds)) {
+             printk(KERN_ERR "%s: failed to alloc memory\n", __func__);

Don't resend, but kmalloc() already prints out way more extensive and useful error messages than this.  No need to print an error message yourself normally.

Do you mean in the following format ?

+ if (unlikely(!dev_wds)) {
+ DBG_LEAVE(DbgInfo);
+ return;
+ }

regards
Santosh

Quoted 4 lines

regards,
dan carpenter

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

#3 lore raw
In reply to Dan Carpenter
Quoted 15 lines · santosh prasad nayak
Quoted 11 lines · Dan Carpenter
Quoted 5 lines · santosh nayak

-        dev_wds = kmalloc( sizeof( struct net_device ), GFP_KERNEL );
-        memset( dev_wds, 0, sizeof( struct net_device ));
+     dev_wds = kzalloc(sizeof(struct net_device), GFP_KERNEL);
+     if (unlikely(!dev_wds)) {
+             printk(KERN_ERR "%s: failed to alloc memory\n", __func__);

Don't resend, but kmalloc() already prints out way more extensive and useful error messages than this.  No need to print an error message yourself normally.

Do you mean in the following format ?

Review

Quoted 4 lines
(quoted diff) +4 -0
+       if (unlikely(!dev_wds)) {
+               DBG_LEAVE(DbgInfo);
+               return;
+       }

No need to resend. It's just for next time something to consider.

regards,
dan carpenter

maintainer
#4 lore raw
In reply to Dan Carpenter
Quoted 15 lines · santosh nayak

From: Santosh Nayak <[email protected]>

Replace kmalloc+memset pair by kzalloc() in 'wl_wds_device_alloc()'. Add error handling to avoid null derefernce.

---
 drivers/staging/wlags49_h2/wl_netdev.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
drivers/staging/wlags49_h2/wl_netdev.c +0 -0
index 9c16f54..426ba04 100644

drivers/staging/wlags49_h2/wl_netdev.c void wl_wds_device_alloc( struct wl_private *lp ) · line 1515

Quoted 7 lines
drivers/staging/wlags49_h2/wl_netdev.c +2 -2
@@ -1511,8 +1511,12 @@ void wl_wds_device_alloc( struct wl_private *lp )
     for( count = 0; count < NUM_WDS_PORTS; count++ ) {
         struct net_device *dev_wds = NULL;
 
-        dev_wds = kmalloc( sizeof( struct net_device ), GFP_KERNEL );
-        memset( dev_wds, 0, sizeof( struct net_device ));
+	dev_wds = kzalloc(sizeof(struct net_device), GFP_KERNEL);
+	if (unlikely(!dev_wds)) {

NEVER use unlikely unless you are working on scheduler code, or something else equally time critical. So, take this into consideration, and Dan's comments, and please redo this, I can't take it as-is.

thanks,

greg k-h

maintainer
#5 lore raw
In reply to Dan Carpenter
Quoted 2 lines · santosh prasad nayak

Can you please explain whats the disadvantage of having 'unlikely()' here ? I have seen it at several places in driver side.

And I bet they are used wrong there. Again, unless you are in some very performance critical section of code, don't use unlikely().

The big thing is that it just is not needed at all. Don't try to be smarter than the compiler if you don't need it, _AND_ you haven't actually measured it.

greg k-h

#6 lore raw
In reply to Dan Carpenter
Quoted 19 lines · Greg KH
Quoted 18 lines · santosh nayak

From: Santosh Nayak <[email protected]>

Replace kmalloc+memset pair by kzalloc() in 'wl_wds_device_alloc()'. Add error handling to avoid null derefernce.

---
 drivers/staging/wlags49_h2/wl_netdev.c |    8 ++++++--

 1 files changed, 6 insertions(+), 2 deletions(-)
drivers/staging/wlags49_h2/wl_netdev.c +0 -0
index 9c16f54..426ba04 100644
@@ -1511,8 +1511,12 @@ void wl_wds_device_alloc( struct wl_private *lp )
      for( count = 0; count < NUM_WDS_PORTS; count++ ) {
          struct net_device *dev_wds = NULL;
 
Quoted 9 lines

drivers/staging/wlags49_h2/wl_netdev.c void wl_wds_device_alloc( struct wl_private *lp ) · line 1515

Quoted 4 lines
drivers/staging/wlags49_h2/wl_netdev.c +2 -2
-        dev_wds = kmalloc( sizeof( struct net_device ), GFP_KERNEL );
-        memset( dev_wds, 0, sizeof( struct net_device ));
+     dev_wds = kzalloc(sizeof(struct net_device), GFP_KERNEL);
+     if (unlikely(!dev_wds)) {

NEVER use unlikely unless you are working on scheduler code, or something else equally time critical.  So, take this into consideration, and Dan's comments, and please redo this, I can't take it as-is.

Greg,

I will include your suggestion and resend it.

Can you please explain whats the disadvantage of having 'unlikely()' here ? I have seen it at several places in driver side.

regards
Santosh

Quoted 3 lines

thanks,

greg k-h

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

#7 lore raw
In reply to Dan Carpenter

Hi,

Quoted 7 lines · Greg KH

And I bet they are used wrong there.  Again, unless you are in some very performance critical section of code, don't use unlikely().

The big thing is that it just is not needed at all.  Don't try to be smarter than the compiler if you don't need it, _AND_ you haven't actually measured it.

Nice answer. I did a quick grep and I found this scary result:

~/devel/v4l-dvb $ grep -r -n "unlikely(" drivers/ | wc -l 5018

I wonder perhaps we could add a check for unlikely in checkpatch script? Something like: "are you sure about this"?

Also, perhaps this quote from [1] should be somewhere in Documentation:

"How should I use it ?
You should use it only in cases when the likeliest branch is very very very likely, or when the unlikeliest branch is very very very unlikely."

[1] http://kernelnewbies.org/FAQ/LikelyUnlikely
--
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