Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1340113 > unrolled thread

Re: [PATCH 4/4] staging: rtl8192u: r8192U_core: use kmalloc_array instead of kmalloc

Started byGeliang Tang <geliangtang@163.com>
First post2016-02-23 02:00 +0100
Last post2016-02-23 02:20 +0100
Articles 2 — 2 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: [PATCH 4/4] staging: rtl8192u: r8192U_core: use kmalloc_array  instead of kmalloc Geliang Tang <geliangtang@163.com> - 2016-02-23 02:00 +0100
    Re: [PATCH 4/4] staging: rtl8192u: r8192U_core: use kmalloc_array  instead of kmalloc Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2016-02-23 02:20 +0100

#1340113 — Re: [PATCH 4/4] staging: rtl8192u: r8192U_core: use kmalloc_array instead of kmalloc

FromGeliang Tang <geliangtang@163.com>
Date2016-02-23 02:00 +0100
SubjectRe: [PATCH 4/4] staging: rtl8192u: r8192U_core: use kmalloc_array instead of kmalloc
Message-ID<r59uy-5YU-7@gated-at.bofh.it>
On Sun, Feb 07, 2016 at 07:55:09PM -0800, Greg Kroah-Hartman wrote:
> On Sun, Nov 08, 2015 at 10:17:54PM +0800, Geliang Tang wrote:
> > Use kmalloc_array instead of kmalloc to allocate memory for an array.
> > 
> > Signed-off-by: Geliang Tang <geliangtang@163.com>
> > ---
> >  drivers/staging/rtl8192u/r8192U_core.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/staging/rtl8192u/r8192U_core.c b/drivers/staging/rtl8192u/r8192U_core.c
> > index e06864f..07a1447 100644
> > --- a/drivers/staging/rtl8192u/r8192U_core.c
> > +++ b/drivers/staging/rtl8192u/r8192U_core.c
> > @@ -1725,8 +1725,8 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
> >  {
> >  	struct r8192_priv *priv = ieee80211_priv(dev);
> >  
> > -	priv->rx_urb = kmalloc(sizeof(struct urb *) * (MAX_RX_URB + 1),
> > -			       GFP_KERNEL);
> > +	priv->rx_urb = kmalloc_array(MAX_RX_URB + 1, sizeof(struct urb *),
> > +				     GFP_KERNEL);
> 
> I don't see the benefit here with this change, do you?

It is highly likely that there is a checkpatch warning "kmalloc with multiplies"
here. But the checkpatch script does not report this warning, I think it is
because MAX_RX_URB is a macro, and there is a '+1' after it. Therefore although
it passes,I think it still should be deemed as a warning. So to use kmalloc_array
here seems better.

Thanks.

- Geliang

[toc] | [next] | [standalone]


#1340121

FromGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Date2016-02-23 02:20 +0100
Message-ID<r59NU-6nf-15@gated-at.bofh.it>
In reply to#1340113
On Tue, Feb 23, 2016 at 08:55:04AM +0800, Geliang Tang wrote:
> On Sun, Feb 07, 2016 at 07:55:09PM -0800, Greg Kroah-Hartman wrote:
> > On Sun, Nov 08, 2015 at 10:17:54PM +0800, Geliang Tang wrote:
> > > Use kmalloc_array instead of kmalloc to allocate memory for an array.
> > > 
> > > Signed-off-by: Geliang Tang <geliangtang@163.com>
> > > ---
> > >  drivers/staging/rtl8192u/r8192U_core.c | 4 ++--
> > >  1 file changed, 2 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/drivers/staging/rtl8192u/r8192U_core.c b/drivers/staging/rtl8192u/r8192U_core.c
> > > index e06864f..07a1447 100644
> > > --- a/drivers/staging/rtl8192u/r8192U_core.c
> > > +++ b/drivers/staging/rtl8192u/r8192U_core.c
> > > @@ -1725,8 +1725,8 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
> > >  {
> > >  	struct r8192_priv *priv = ieee80211_priv(dev);
> > >  
> > > -	priv->rx_urb = kmalloc(sizeof(struct urb *) * (MAX_RX_URB + 1),
> > > -			       GFP_KERNEL);
> > > +	priv->rx_urb = kmalloc_array(MAX_RX_URB + 1, sizeof(struct urb *),
> > > +				     GFP_KERNEL);
> > 
> > I don't see the benefit here with this change, do you?
> 
> It is highly likely that there is a checkpatch warning "kmalloc with multiplies"
> here. But the checkpatch script does not report this warning, I think it is
> because MAX_RX_URB is a macro, and there is a '+1' after it. Therefore although
> it passes,I think it still should be deemed as a warning. So to use kmalloc_array
> here seems better.

No, not for a statically defined size, it doesn't make any sense at all.
kmalloc_array() is to ensure that if you need to allocate a huge size of
memory, that you don't accidentally overflow the memory size with the
calculation you have made, so it does that calculation in a "safe" way.

For a static number, like you have here, it doesn't make any difference.

checkpatch.pl is a _HINT_ not a hard-and-fast-rule.

sorry,

greg k-h

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web