2007-11-26 09:03:56

by Joonwoo Park

[permalink] [raw]
Subject: [PATCH 4/4] atm/ambassador: kmalloc + memset conversion to kzalloc

atm/ambassador: kmalloc + memset conversion to kzalloc

Signed-off-by: Joonwoo Park <[email protected]>

Thanks.
Joonwoo

---
diff --git a/drivers/atm/ambassador.c b/drivers/atm/ambassador.c
index b34b382..4f99ba3 100644
--- a/drivers/atm/ambassador.c
+++ b/drivers/atm/ambassador.c
@@ -2163,7 +2163,6 @@ static int __devinit amb_init (amb_dev * dev)
static void setup_dev(amb_dev *dev, struct pci_dev *pci_dev)
{
unsigned char pool;
- memset (dev, 0, sizeof(amb_dev));

// set up known dev items straight away
dev->pci_dev = pci_dev;
@@ -2253,7 +2252,7 @@ static int __devinit amb_probe(struct pci_dev *pci_dev, const struct pci_device_
goto out_disable;
}

- dev = kmalloc (sizeof(amb_dev), GFP_KERNEL);
+ dev = kzalloc(sizeof(amb_dev), GFP_KERNEL);
if (!dev) {
PRINTK (KERN_ERR, "out of memory!");
err = -ENOMEM;
---


2007-11-26 09:14:06

by Robert P. J. Day

[permalink] [raw]
Subject: Re: [PATCH 4/4] atm/ambassador: kmalloc + memset conversion to kzalloc

On Mon, 26 Nov 2007, Joonwoo Park wrote:

> atm/ambassador: kmalloc + memset conversion to kzalloc
>
> Signed-off-by: Joonwoo Park <[email protected]>
>
> Thanks.
> Joonwoo
>
> ---
> diff --git a/drivers/atm/ambassador.c b/drivers/atm/ambassador.c
> index b34b382..4f99ba3 100644
> --- a/drivers/atm/ambassador.c
> +++ b/drivers/atm/ambassador.c
> @@ -2163,7 +2163,6 @@ static int __devinit amb_init (amb_dev * dev)
> static void setup_dev(amb_dev *dev, struct pci_dev *pci_dev)
> {
> unsigned char pool;
> - memset (dev, 0, sizeof(amb_dev));
>
> // set up known dev items straight away
> dev->pci_dev = pci_dev;
> @@ -2253,7 +2252,7 @@ static int __devinit amb_probe(struct pci_dev *pci_dev, const struct pci_device_
> goto out_disable;
> }
>
> - dev = kmalloc (sizeof(amb_dev), GFP_KERNEL);
> + dev = kzalloc(sizeof(amb_dev), GFP_KERNEL);
> if (!dev) {
> PRINTK (KERN_ERR, "out of memory!");
> err = -ENOMEM;
> ---

i'm not sure the above is a safe thing to do, as you're zeroing that
area, then making a function call and assuming, upon entry to the
function call, that the caller has done the right thing. i don't see
how you can count on that, depending on who else might want to call
that routine and whether they get sloppy about it. unless you're
prepared to guarantee that there will never be another call to
setup_dev() from elsewhere.


rday


========================================================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry
Waterloo, Ontario, CANADA

http://crashcourse.ca
========================================================================

2007-11-26 10:24:15

by Joonwoo Park

[permalink] [raw]
Subject: Re: [PATCH 4/4] atm/ambassador: kmalloc + memset conversion to kzalloc

2007/11/26, Robert P. J. Day <[email protected]>:
> i'm not sure the above is a safe thing to do, as you're zeroing that
> area, then making a function call and assuming, upon entry to the
> function call, that the caller has done the right thing. i don't see
> how you can count on that, depending on who else might want to call
> that routine and whether they get sloppy about it. unless you're
> prepared to guarantee that there will never be another call to
> setup_dev() from elsewhere.
>

Thanks for your response.
But setup_dev is static function and only amb_init calls it.
IMO it's safe.

Thanks.
Joonwoo

2007-11-26 10:31:44

by Robert P. J. Day

[permalink] [raw]
Subject: Re: [PATCH 4/4] atm/ambassador: kmalloc + memset conversion to kzalloc

On Mon, 26 Nov 2007, Joonwoo Park wrote:

> 2007/11/26, Robert P. J. Day <[email protected]>:
> > i'm not sure the above is a safe thing to do, as you're zeroing that
> > area, then making a function call and assuming, upon entry to the
> > function call, that the caller has done the right thing. i don't see
> > how you can count on that, depending on who else might want to call
> > that routine and whether they get sloppy about it. unless you're
> > prepared to guarantee that there will never be another call to
> > setup_dev() from elsewhere.
>
> Thanks for your response. But setup_dev is static function and only
> amb_init calls it.

i realized that. but all you can say is that only amb_init() calls
setup_dev() *currently*. when you're not looking, someone else might
(for whatever reason) call setup_dev() from elsewhere, and *that* call
might not zero that memory area.

IMHO, the only safe transforms of kmalloc+memset -> kzalloc are those
in which the flow of control is unmistakable and invariant. splitting
that across a function call seems like a dangerous thing to do.
(except, of course, in the case, where the kzalloc() is added inside
the function -- then all callers are entitled to simplify *their*
code. but that's different.)

in any event, i just thought i'd point it out. if you're absolutely
sure there will never be another call to setup_dev() from somewhere
else, then, yes, it's safe.

rday

========================================================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry
Waterloo, Ontario, CANADA

http://crashcourse.ca
========================================================================

2007-11-26 10:53:32

by Joonwoo Park

[permalink] [raw]
Subject: Re: [PATCH 4/4] atm/ambassador: kmalloc + memset conversion to kzalloc

2007/11/26, Robert P. J. Day <[email protected]>:
> i realized that. but all you can say is that only amb_init() calls
> setup_dev() *currently*. when you're not looking, someone else might
> (for whatever reason) call setup_dev() from elsewhere, and *that* call
> might not zero that memory area.
>
> IMHO, the only safe transforms of kmalloc+memset -> kzalloc are those
> in which the flow of control is unmistakable and invariant. splitting
> that across a function call seems like a dangerous thing to do.
> (except, of course, in the case, where the kzalloc() is added inside
> the function -- then all callers are entitled to simplify *their*
> code. but that's different.)
>
> in any event, i just thought i'd point it out. if you're absolutely
> sure there will never be another call to setup_dev() from somewhere
> else, then, yes, it's safe.
>

I understood your opinions. and partially agree with you.
But isn't it a unfounded fear?

Thanks
Joonwoo

2007-11-26 11:22:43

by Robert P. J. Day

[permalink] [raw]
Subject: Re: [PATCH 4/4] atm/ambassador: kmalloc + memset conversion to kzalloc

On Mon, 26 Nov 2007, Joonwoo Park wrote:

> 2007/11/26, Robert P. J. Day <[email protected]>:
> > i realized that. but all you can say is that only amb_init() calls
> > setup_dev() *currently*. when you're not looking, someone else might
> > (for whatever reason) call setup_dev() from elsewhere, and *that* call
> > might not zero that memory area.
> >
> > IMHO, the only safe transforms of kmalloc+memset -> kzalloc are those
> > in which the flow of control is unmistakable and invariant. splitting
> > that across a function call seems like a dangerous thing to do.
> > (except, of course, in the case, where the kzalloc() is added inside
> > the function -- then all callers are entitled to simplify *their*
> > code. but that's different.)
> >
> > in any event, i just thought i'd point it out. if you're absolutely
> > sure there will never be another call to setup_dev() from somewhere
> > else, then, yes, it's safe.
> >
>
> I understood your opinions. and partially agree with you.
> But isn't it a unfounded fear?

i don't know, i just thought i'd mention it. if no one thinks it's an
issue, it's certainly fine with me.

rday

========================================================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry
Waterloo, Ontario, CANADA

http://crashcourse.ca
========================================================================

Subject: Re: [PATCH 4/4] atm/ambassador: kmalloc + memset conversion to kzalloc

In message <[email protected]>,"Rober
t P. J. Day" writes:
>> > in any event, i just thought i'd point it out. if you're absolutely
>> > sure there will never be another call to setup_dev() from somewhere
>> > else, then, yes, it's safe.
>>
>> I understood your opinions. and partially agree with you.
>> But isn't it a unfounded fear?
>
>i don't know, i just thought i'd mention it. if no one thinks it's an
>issue, it's certainly fine with me.

its very unlikely that setup_dev() is likely to be called from another
code path. this patch looks fine to me. i will take it and get it
submitted on the next merge.