use kzalloc instead of malloc+memset
Subsystems (3)
PATCH 1/1 use kzalloc instead of malloc+memset
This patch against 2.6.13-mm3 replaces malloc and memset with kzalloc in drivers/base/class.c . Furthermore this patch fixes actually two bugs:
The memset arguments were occasionally swaped and therefore wrong.
Usage of kzalloc makes this code shorter and more bugfree.
Please apply.
drivers/base/class.c +6 -8
@@ -190,12 +190,11 @@ struct class *class_create(struct modulestruct class *cls;int retval; - cls =kmalloc (sizeof (struct class ),GFP_KERNEL ); + cls =kzalloc (sizeof (struct class ),GFP_KERNEL );if (!cls) { retval = -ENOMEM ; goto error; } -memset (cls,0x00 ,sizeof (struct class )); cls->name = name; cls->owner = owner;
@@ -519,13 +518,13 @@ int class_device_add(struct class_device/* add the needed attributes to this device */ if (MAJOR (class_dev->devt)) {struct class_device_attribute *attr; - attr =kmalloc (sizeof (*attr),GFP_KERNEL ); + attr =kzalloc (sizeof (*attr),GFP_KERNEL );if (!attr) { error = -ENOMEM ;kobject_del (&class_dev->kobj); goto register_done; } -memset (attr,sizeof (*attr),0x00 ); + attr->attr.name ="dev" ; attr->attr.mode =S_IRUGO ; attr->attr.owner = parent->owner;
@@ -534,13 +533,13 @@ int class_device_add(struct class_deviceclass_device_create_file (class_dev, attr); class_dev->devt_attr = attr; - attr =kmalloc (sizeof (*attr),GFP_KERNEL ); + attr =kzalloc (sizeof (*attr),GFP_KERNEL );if (!attr) { error = -ENOMEM ;kobject_del (&class_dev->kobj); goto register_done; } -memset (attr,sizeof (*attr),0x00 ); + attr->attr.name ="sample.sh" ; attr->attr.mode =S_IRUSR |S_IXUSR |S_IRUGO ; attr->attr.owner = parent->owner;
@@ -611,12 +610,11 @@ struct class_device *class_device_createif (cls ==NULL ||IS_ERR (cls)) goto error; - class_dev =kmalloc (sizeof (struct class_device ),GFP_KERNEL ); + class_dev =kzalloc (sizeof (struct class_device ),GFP_KERNEL );if (!class_dev) { retval = -ENOMEM ; goto error; } -memset (class_dev,0x00 ,sizeof (struct class_device )); class_dev->devt = devt; class_dev->dev = device;
Quoted 2 lines · Lion Vollnhals
This patch against 2.6.13-mm3 replaces malloc and memset with kzalloc in
drivers/base/class.c.
The following patch converts further kmalllocs and memsets in drivers/base/* to kzallocs.
Please apply.
drivers/base/attribute_container.c +3 -2
@@ -152,12 +152,13 @@ attribute_container_add_device(struct deif (!cont->match (cont, dev))continue ; - ic =kmalloc (sizeof (struct internal_container ),GFP_KERNEL ); + + ic =kzalloc (sizeof (struct internal_container ),GFP_KERNEL );if (!ic) {dev_printk (KERN_ERR , dev,"failed to allocate class container\n" );continue ; } -memset (ic,0 ,sizeof (struct internal_container )); + ic->cont = cont;class_device_initialize (&ic->classdev); ic->classdev.dev =get_device (dev);
drivers/base/firmware_class.c +3 -6
@@ -301,9 +301,9 @@ fw_register_class_device(struct class_de const char *fw_name, struct device *device) { int retval; -struct firmware_priv *fw_priv =kmalloc (sizeof (struct firmware_priv ), +struct firmware_priv *fw_priv =kzalloc (sizeof (struct firmware_priv ),GFP_KERNEL ); -struct class_device *class_dev =kmalloc (sizeof (struct class_device ), +struct class_device *class_dev =kzalloc (sizeof (struct class_device ),GFP_KERNEL ); *class_dev_p =NULL ;
@@ -313,8 +313,6 @@ fw_register_class_device(struct class_de retval = -ENOMEM ; goto error_kfree; } -memset (fw_priv,0 ,sizeof (*fw_priv)); -memset (class_dev,0 ,sizeof (*class_dev));init_completion (&fw_priv->completion); fw_priv->attr_data = firmware_attr_data_tmpl;
@@ -402,14 +400,13 @@ _request_firmware(const struct firmwareif (!firmware_p)return -EINVAL ; - *firmware_p = firmware =kmalloc (sizeof (struct firmware ),GFP_KERNEL ); + *firmware_p = firmware =kzalloc (sizeof (struct firmware ),GFP_KERNEL );if (!firmware) {printk (KERN_ERR "%s: kmalloc(struct firmware) failed\n" , __FUNCTION__); retval = -ENOMEM ; goto out; } -memset (firmware,0 ,sizeof (*firmware)); retval =fw_setup_class_device (firmware, &class_dev, name, device, hotplug);
drivers/base/map.c +1 -2
@@ -135,7 +135,7 @@ retry:struct kobj_map *kobj_map_init (kobj_probe_t *base_probe,struct semaphore *sem) {struct kobj_map *p =kmalloc (sizeof (struct kobj_map ),GFP_KERNEL ); -struct probe *base =kmalloc (sizeof (struct probe ),GFP_KERNEL ); +struct probe *base =kzalloc (sizeof (struct probe ),GFP_KERNEL );int i;if ((p ==NULL ) || (base ==NULL )) {
@@ -144,7 +144,6 @@ struct kobj_map *kobj_map_init(kobj_probreturn NULL ; } -memset (base,0 ,sizeof (struct probe )); base->dev =1 ; base->range = ~0 ; base->get = base_probe;
drivers/base/memory.c +1 -3
@@ -341,14 +341,12 @@ int add_memory_block(unsigned long node_ unsigned long state, int phys_device) { size_t size = sizeof(struct memory_block); -struct memory_block *mem =kmalloc (size,GFP_KERNEL ); +struct memory_block *mem =kzalloc (size,GFP_KERNEL );int ret =0 ;if (!mem)return -ENOMEM ; -memset (mem,0 , size); - mem->phys_index =__section_nr (section); mem->state = state;init_MUTEX (&mem->state_sem);
drivers/base/platform.c +1 -2
@@ -225,13 +225,12 @@ struct platform_device *platform_device_struct platform_object *pobj;int retval; - pobj =kmalloc (sizeof (struct platform_object ) +sizeof (struct resource ) * num,GFP_KERNEL ); + pobj =kzalloc (sizeof (struct platform_object ) +sizeof (struct resource ) * num,GFP_KERNEL );if (!pobj) { retval = -ENOMEM ; goto error; } -memset (pobj,0 ,sizeof (*pobj)); pobj->pdev.name = name; pobj->pdev.id = id; pobj->pdev.dev.release = platform_device_release_simple;
Quoted 15 lines · Lion Vollnhals
Quoted 2 lines · Lion Vollnhals
This patch against 2.6.13-mm3 replaces malloc and memset with kzalloc in
drivers/base/class.c.The following patch converts further kmalllocs and memsets in
drivers/base/*to kzallocs.Please apply.
Signed-off-by Lion Vollnhals <[email protected]>diff -Nurp
2.6.13-mm3/drivers/base/platform.c2.6.13-mm3-changed/drivers/base/platform.cdrivers/base/platform.c +0
-0@@ -225,13 +225,12 @@ struct platform_device *platform_device_struct platform_object *pobj;
drivers/base/platform.c struct platform_device *platform_device_ · line 234
Quoted 10 lines
drivers/base/platform.c +1
-2int retval; - pobj =kmalloc (sizeof (struct platform_object ) +sizeof (struct resource ) * num,GFP_KERNEL ); + pobj =kzalloc (sizeof (struct platform_object ) +sizeof (struct resource ) * num,GFP_KERNEL );if (!pobj) { retval = -ENOMEM ; goto error; } -memset (pobj,0 ,sizeof (*pobj));
Was this a bug or did you just introduced one?
--
vda
Denis Vlasenko napsal(a):
Quoted 34 lines · Lion Vollnhals
Quoted 33 lines · Lion Vollnhals
Quoted 6 lines
This patch against 2.6.13-mm3 replaces malloc and memset with kzalloc in
drivers/base/class.c.The following patch converts further kmalllocs and memsets in
drivers/base/*to kzallocs.Please apply.
Signed-off-by Lion Vollnhals <[email protected]>diff -Nurp
2.6.13-mm3/drivers/base/platform.c2.6.13-mm3-changed/drivers/base/platform.cdrivers/base/platform.c +1
-1@@ -225,13 +225,12 @@ struct platform_device *platform_device_struct platform_object *pobj;int retval; - pobj =kmalloc (sizeof (struct platform_object ) +sizeof (struct resource ) * num,GFP_KERNEL ); + pobj =kzalloc (sizeof (struct platform_object ) +sizeof (struct resource ) * num,GFP_KERNEL );if (!pobj) { retval = -ENOMEM ; goto error; }
Quoted 7 lines
drivers/base/platform.c struct platform_device *platform_device_ · line 235
Quoted 3 lines
drivers/base/platform.c +0
-1-memset (pobj,0 ,sizeof (*pobj));Was this a bug or did you just introduced one?
Yes, this was a bug.
Signature · 3 lines
Jiri Slaby www.fi.muni.cz/~xslaby ~\-/~ [email protected] ~\-/~ 241B347EC88228DE51EE A49C4A73A25004CB2A10
Lion Vollnhals napsal(a):
Quoted 13 lines
This patch against 2.6.13-mm3 replaces malloc and memset with kzalloc in
drivers/base/class.c. Furthermore this patch fixes actually two bugs:
The memset arguments were occasionally swaped and therefore wrong.Usage of kzalloc makes this code shorter and more bugfree.
Please apply.
Signed-off-by Lion Vollnhals <[email protected]>
drivers/base/class.c struct class *class_create(struct module · line 195
Quoted 7 lines
drivers/base/class.c +1
-1@@ -190,12 +190,11 @@ struct class *class_create(struct modulestruct class *cls;int retval; - cls =kmalloc (sizeof (struct class ),GFP_KERNEL ); + cls =kzalloc (sizeof (struct class ),GFP_KERNEL );
maybe, the better way is to write *cls' instead of struct class', better for further changes
Quoted 10 lines
drivers/base/class.c +0
-1if (!cls) { retval = -ENOMEM ; goto error; } -memset (cls,0x00 ,sizeof (struct class ));cls->name = name; cls->owner = owner;
[snip]
drivers/base/class.c struct class_device *class_device_create · line 615
Quoted 8 lines
drivers/base/class.c +1
-1@@ -611,12 +610,11 @@ struct class_device *class_device_createif (cls ==NULL ||IS_ERR (cls)) goto error; - class_dev =kmalloc (sizeof (struct class_device ),GFP_KERNEL ); + class_dev =kzalloc (sizeof (struct class_device ),GFP_KERNEL );
*class_dev' instead of struct class_device'
Quoted 10 lines
drivers/base/class.c +0
-1if (!class_dev) { retval = -ENOMEM ; goto error; } -memset (class_dev,0x00 ,sizeof (struct class_device ));class_dev->devt = devt; class_dev->dev = device;
thanks,
Signature · 3 lines
Jiri Slaby www.fi.muni.cz/~xslaby ~\-/~ [email protected] ~\-/~ 241B347EC88228DE51EE A49C4A73A25004CB2A10
Quoted 6 lines · Jiri Slaby
drivers/base/class.c struct class *class_create(struct module · line 193
Quoted 4 lines
drivers/base/class.c +1
-1- cls =kmalloc (sizeof (struct class ),GFP_KERNEL ); + cls =kzalloc (sizeof (struct class ),GFP_KERNEL );maybe, the better way is to write
*cls' instead ofstruct class', better for further changes
Please note that some maintainers don't like it. I at least could not sneak in patches like these to drivers/usb/ because I had changed sizeof.
Pekka
Quoted 5 lines · Pekka Enberg
Quoted 4 lines · Jiri Slaby
Quoted 4 lines
drivers/base/class.c +1
-1- cls =kmalloc (sizeof (struct class ),GFP_KERNEL ); + cls =kzalloc (sizeof (struct class ),GFP_KERNEL );
Quoted 7 lines
Quoted 2 lines
maybe, the better way is to write
*cls' instead ofstruct class', better for further changesPlease note that some maintainers don't like it. I at least could not sneak in patches like these to
drivers/usb/because I had changed sizeof.
And given the fact that Greg maintains driver core it probably won't be accepted here either :)
FWIW I also prefer spelling out the structure I am allocating.
Signature · 1 line
Dmitry
Quoted 15 lines · Dmitry Torokhov
Quoted 5 lines · Pekka Enberg
Quoted 4 lines · Jiri Slaby
Quoted 4 lines
drivers/base/class.c +1
-1- cls =kmalloc (sizeof (struct class ),GFP_KERNEL ); + cls =kzalloc (sizeof (struct class ),GFP_KERNEL );Quoted 7 lines
Quoted 2 lines
maybe, the better way is to write
*cls' instead ofstruct class', better for further changesPlease note that some maintainers don't like it. I at least could not sneak in patches like these to
drivers/usb/because I had changed sizeof.
Quoted 5 lines
And given the fact that Greg maintains driver core it probably won't be accepted here either :)
FWIW I also prefer spelling out the structure I am allocating.
It hurts readability. Quick question: is this code correct?
dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);
you don't know. You have to go hunting down the declaration of `dev' to find out.
Quoted 1 line · Dmitry Torokhov
Quoted 1 line
FWIW I also prefer spelling out the structure I am allocating.
Quoted 6 lines · Andrew Morton
It hurts readability. Quick question: is this code correct?
dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);you don't know. You have to go hunting down the declaration of `dev' to find out.
Andrew, how about something like this?
Pekka
[PATCH] CodingStyle: memory allocation
This patch adds a new chapter on memory allocation to Documentation/CodingStyle.
---
CodingStyle | 21 ++++++++++++++++++++- 1 files changed, 20 insertions(+), 1 deletion(-)
Documentation/CodingStyle +20 -1
@@ -410,7 +410,26 @@ Kernel messages do not have to be termin Printing numbers in parentheses (%d) adds no value and should be avoided. - Chapter 13: References + Chapter 13: Allocating memory + +The kernel provides the following general purpose memory allocators: +kmalloc(), kzalloc(), kcalloc(), and vmalloc(). Please refer to the API +documentation for further information about them. + +The preferred form for passing a size of a struct is the following: + + p = kmalloc(sizeof(*p), ...); + +The alternative form where struct name is spelled out hurts readability and +introduces an opportunity for a bug when the pointer variable type is changed +but the corresponding sizeof that is passed to a memory allocator is not. + +Casting the return value which is a void pointer is redundant. The conversion +from void pointer to any other pointer type is guaranteed by the C programming +language. + + + Chapter 14: References The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie.
Pekka J Enberg writes:
[...]
Documentation/CodingStyle Kernel messages do not have to be termin · line 421
Quoted 8 lines
Documentation/CodingStyle +8
-0+ +The kernel provides the following general purpose memory allocators: +kmalloc(), kzalloc(), kcalloc(), and vmalloc(). Please refer to the API +documentation for further information about them. + +The preferred form for passing a size of a struct is the following: + + p = kmalloc(sizeof(*p), ...);
Parentheses around *p are superfluous. See
Quoted 2 lines
The C Programming Language, Second Edition
by Brian W. Kernighan and Dennis M. Ritchie.
Nikita.
Quoted 7 lines · Pekka J Enberg
Quoted 7 lines
Documentation/CodingStyle +7
-0+The kernel provides the following general purpose memory allocators: +kmalloc(), kzalloc(), kcalloc(), and vmalloc(). Please refer to the API +documentation for further information about them. + +The preferred form for passing a size of a struct is the following: + + p = kmalloc(sizeof(*p), ...);
Quoted 1 line · Nikita Danilov
Parentheses around *p are superfluous. See
Sure but is it the preferred form for the kernel?
Pekka
Quoted 4 lines · Nikita Danilov
Pekka J Enberg writes:
[...]
Quoted 13 lines
Documentation/CodingStyle Kernel messages do not have to be termin · line 421
Quoted 8 lines
Documentation/CodingStyle +8
-0+ +The kernel provides the following general purpose memory allocators: +kmalloc(), kzalloc(), kcalloc(), and vmalloc(). Please refer to the API +documentation for further information about them. + +The preferred form for passing a size of a struct is the following: + + p = kmalloc(sizeof(*p), ...);Parentheses around *p are superfluous. See
Quoted 2 lines
The C Programming Language, Second Edition
by Brian W. Kernighan and Dennis M. Ritchie.
I remember that sizeof has two forms: sizeof(type) and sizeof(expr), and in one of them ()'s are optional. But I fail to remember in which one. I use ()'s always.
Thanks for refreshing my memory but I'm sure I'll forget again ;)
--
vda
Denis Vlasenko writes:
[...]
Quoted 4 lines
I remember that sizeof has two forms: sizeof(type) and sizeof(expr), and in one of them ()'s are optional. But I fail to remember in which one. I use ()'s always.
Formally speaking, sizeof have forms
sizeof(type), and
sizeof expr
it is just that expression can usually be wrapped into parentheses, like (((((0))))).
Quoted 3 lines
Thanks for refreshing my memory but I'm sure I'll forget again ;)
That's why we need more instances of sizeof expr in the kernel code, to keep your knowledge of C afresh all the time. :-)
Note that Linux doesn't follow a custom of... some other kernels to parenthesize everything to the heretical extent of writing
if ((a == 0) && (b == 1))
or
return (foo);
Quoted 2 lines
--
vda
Nikita.
Quoted 3 lines · Andrew
It hurts readability. Quick question: is this code correct?
dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);
And it hurts maintainability. If someone changes 'dev' so that it is no longer of type 'struct net_device', then they risk missing this allocation, and introducing what could be a nasty memory corruption kernel bug.
Signature · 3 lines
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <[email protected]> 1.925.600.0401Quoted 7 lines · Paul Jackson
Quoted 3 lines · Andrew
It hurts readability. Quick question: is this code correct?
dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);And it hurts maintainability. If someone changes 'dev' so that it is no longer of type 'struct
net_device', then they
'struct net_device *'
Quoted 2 lines
risk missing this allocation, and introducing what could be a nasty memory corruption kernel bug.
SCNR,
Bernd
Signature · 3 lines
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and Services