diff mbox

usb: gadget: udc: atmel: used managed kasprintf

Message ID 20161201102656.29041-1-alexandre.belloni@free-electrons.com (mailing list archive)
State New, archived
Headers show

Commit Message

Alexandre Belloni Dec. 1, 2016, 10:26 a.m. UTC
Use devm_kasprintf instead of simple kasprintf to free the allocated memory
when needed.

Suggested-by: Peter Rosin <peda@axentia.se>
Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
---
 drivers/usb/gadget/udc/atmel_usba_udc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Nicolas Ferre Dec. 1, 2016, 11:20 a.m. UTC | #1
Le 01/12/2016 à 11:26, Alexandre Belloni a écrit :
> Use devm_kasprintf instead of simple kasprintf to free the allocated memory
> when needed.
> 
> Suggested-by: Peter Rosin <peda@axentia.se>
> Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>

Acked-by: Nicolas Ferre <nicolas.ferre@atmel.com>

> ---
>  drivers/usb/gadget/udc/atmel_usba_udc.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
> index 45bc997d0711..aec72fe8273c 100644
> --- a/drivers/usb/gadget/udc/atmel_usba_udc.c
> +++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
> @@ -1978,7 +1978,8 @@ static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev,
>  			dev_err(&pdev->dev, "of_probe: name error(%d)\n", ret);
>  			goto err;
>  		}
> -		ep->ep.name = kasprintf(GFP_KERNEL, "ep%d", ep->index);
> +		ep->ep.name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "ep%d",
> +					     ep->index);
>  
>  		ep->ep_regs = udc->regs + USBA_EPT_BASE(i);
>  		ep->dma_regs = udc->regs + USBA_DMA_BASE(i);
>
David Laight Dec. 2, 2016, 3:59 p.m. UTC | #2
From: Alexandre Belloni
> Sent: 01 December 2016 10:27
> Use devm_kasprintf instead of simple kasprintf to free the allocated memory
> when needed.

s/when needed/when the device is freed/

> Suggested-by: Peter Rosin <peda@axentia.se>
> Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
> ---
>  drivers/usb/gadget/udc/atmel_usba_udc.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
> index 45bc997d0711..aec72fe8273c 100644
> --- a/drivers/usb/gadget/udc/atmel_usba_udc.c
> +++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
> @@ -1978,7 +1978,8 @@ static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev,
>  			dev_err(&pdev->dev, "of_probe: name error(%d)\n", ret);
>  			goto err;
>  		}
> -		ep->ep.name = kasprintf(GFP_KERNEL, "ep%d", ep->index);
> +		ep->ep.name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "ep%d",
> +					     ep->index);

Acually why bother mallocing such a small string at all.
The maximum length is 12 bytes even if 'index' are unrestricted.
 
	David
Alexandre Belloni Dec. 2, 2016, 4:19 p.m. UTC | #3
On 02/12/2016 at 15:59:57 +0000, David Laight wrote :
> From: Alexandre Belloni
> > Sent: 01 December 2016 10:27
> > Use devm_kasprintf instead of simple kasprintf to free the allocated memory
> > when needed.
> 
> s/when needed/when the device is freed/
> 
> > Suggested-by: Peter Rosin <peda@axentia.se>
> > Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
> > ---
> >  drivers/usb/gadget/udc/atmel_usba_udc.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
> > index 45bc997d0711..aec72fe8273c 100644
> > --- a/drivers/usb/gadget/udc/atmel_usba_udc.c
> > +++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
> > @@ -1978,7 +1978,8 @@ static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev,
> >  			dev_err(&pdev->dev, "of_probe: name error(%d)\n", ret);
> >  			goto err;
> >  		}
> > -		ep->ep.name = kasprintf(GFP_KERNEL, "ep%d", ep->index);
> > +		ep->ep.name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "ep%d",
> > +					     ep->index);
> 
> Acually why bother mallocing such a small string at all.
> The maximum length is 12 bytes even if 'index' are unrestricted.
>  

IIRC, using statically allocated string is failing somewhere is the USB
core but I don't remember all the details.
David Laight Dec. 5, 2016, 5:17 p.m. UTC | #4
From: Alexandre Belloni
> Sent: 02 December 2016 16:19
> On 02/12/2016 at 15:59:57 +0000, David Laight wrote :
> > From: Alexandre Belloni
> > > Sent: 01 December 2016 10:27
> > > Use devm_kasprintf instead of simple kasprintf to free the allocated memory
> > > when needed.
> >
> > s/when needed/when the device is freed/
> >
> > > Suggested-by: Peter Rosin <peda@axentia.se>
> > > Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
> > > ---
> > >  drivers/usb/gadget/udc/atmel_usba_udc.c | 3 ++-
> > >  1 file changed, 2 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
> > > index 45bc997d0711..aec72fe8273c 100644
> > > --- a/drivers/usb/gadget/udc/atmel_usba_udc.c
> > > +++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
> > > @@ -1978,7 +1978,8 @@ static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev,
> > >  			dev_err(&pdev->dev, "of_probe: name error(%d)\n", ret);
> > >  			goto err;
> > >  		}
> > > -		ep->ep.name = kasprintf(GFP_KERNEL, "ep%d", ep->index);
> > > +		ep->ep.name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "ep%d",
> > > +					     ep->index);
> >
> > Acually why bother mallocing such a small string at all.
> > The maximum length is 12 bytes even if 'index' are unrestricted.
> >
> 
> IIRC, using statically allocated string is failing somewhere is the USB
> core but I don't remember all the details.

I can't imagine that changing ep->ep.name from 'char *' to 'char [12]' would
make any difference.

	David
Felipe Balbi Jan. 16, 2017, 10:27 a.m. UTC | #5
Hi,

David Laight <David.Laight@ACULAB.COM> writes:
> From: Alexandre Belloni
>> Sent: 02 December 2016 16:19
>> On 02/12/2016 at 15:59:57 +0000, David Laight wrote :
>> > From: Alexandre Belloni
>> > > Sent: 01 December 2016 10:27
>> > > Use devm_kasprintf instead of simple kasprintf to free the allocated memory
>> > > when needed.
>> >
>> > s/when needed/when the device is freed/
>> >
>> > > Suggested-by: Peter Rosin <peda@axentia.se>
>> > > Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
>> > > ---
>> > >  drivers/usb/gadget/udc/atmel_usba_udc.c | 3 ++-
>> > >  1 file changed, 2 insertions(+), 1 deletion(-)
>> > >
>> > > diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
>> > > index 45bc997d0711..aec72fe8273c 100644
>> > > --- a/drivers/usb/gadget/udc/atmel_usba_udc.c
>> > > +++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
>> > > @@ -1978,7 +1978,8 @@ static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev,
>> > >  			dev_err(&pdev->dev, "of_probe: name error(%d)\n", ret);
>> > >  			goto err;
>> > >  		}
>> > > -		ep->ep.name = kasprintf(GFP_KERNEL, "ep%d", ep->index);
>> > > +		ep->ep.name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "ep%d",
>> > > +					     ep->index);
>> >
>> > Acually why bother mallocing such a small string at all.
>> > The maximum length is 12 bytes even if 'index' are unrestricted.
>> >
>> 
>> IIRC, using statically allocated string is failing somewhere is the USB
>> core but I don't remember all the details.
>
> I can't imagine that changing ep->ep.name from 'char *' to 'char [12]' would
> make any difference.

the actual name is managed by the UDC. Meaning, ep->ep.name should be a
pointer, but it could very well just point to ep->name which would be
char [12].
Alexandre Belloni Jan. 16, 2017, 11:11 a.m. UTC | #6
On 16/01/2017 at 12:27:04 +0200, Felipe Balbi wrote :
> 
> Hi,
> 
> David Laight <David.Laight@ACULAB.COM> writes:
> > From: Alexandre Belloni
> >> Sent: 02 December 2016 16:19
> >> On 02/12/2016 at 15:59:57 +0000, David Laight wrote :
> >> > From: Alexandre Belloni
> >> > > Sent: 01 December 2016 10:27
> >> > > Use devm_kasprintf instead of simple kasprintf to free the allocated memory
> >> > > when needed.
> >> >
> >> > s/when needed/when the device is freed/
> >> >
> >> > > Suggested-by: Peter Rosin <peda@axentia.se>
> >> > > Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
> >> > > ---
> >> > >  drivers/usb/gadget/udc/atmel_usba_udc.c | 3 ++-
> >> > >  1 file changed, 2 insertions(+), 1 deletion(-)
> >> > >
> >> > > diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
> >> > > index 45bc997d0711..aec72fe8273c 100644
> >> > > --- a/drivers/usb/gadget/udc/atmel_usba_udc.c
> >> > > +++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
> >> > > @@ -1978,7 +1978,8 @@ static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev,
> >> > >  			dev_err(&pdev->dev, "of_probe: name error(%d)\n", ret);
> >> > >  			goto err;
> >> > >  		}
> >> > > -		ep->ep.name = kasprintf(GFP_KERNEL, "ep%d", ep->index);
> >> > > +		ep->ep.name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "ep%d",
> >> > > +					     ep->index);
> >> >
> >> > Acually why bother mallocing such a small string at all.
> >> > The maximum length is 12 bytes even if 'index' are unrestricted.
> >> >
> >> 
> >> IIRC, using statically allocated string is failing somewhere is the USB
> >> core but I don't remember all the details.
> >
> > I can't imagine that changing ep->ep.name from 'char *' to 'char [12]' would
> > make any difference.
> 
> the actual name is managed by the UDC. Meaning, ep->ep.name should be a
> pointer, but it could very well just point to ep->name which would be
> char [12].
> 

Yeah, I sent a patch that did just that.
http://lists.infradead.org/pipermail/linux-arm-kernel/2017-January/478602.html
Felipe Balbi Jan. 16, 2017, 11:19 a.m. UTC | #7
Hi,

Alexandre Belloni <alexandre.belloni@free-electrons.com> writes:
>> David Laight <David.Laight@ACULAB.COM> writes:
>> > From: Alexandre Belloni
>> >> Sent: 02 December 2016 16:19
>> >> On 02/12/2016 at 15:59:57 +0000, David Laight wrote :
>> >> > From: Alexandre Belloni
>> >> > > Sent: 01 December 2016 10:27
>> >> > > Use devm_kasprintf instead of simple kasprintf to free the allocated memory
>> >> > > when needed.
>> >> >
>> >> > s/when needed/when the device is freed/
>> >> >
>> >> > > Suggested-by: Peter Rosin <peda@axentia.se>
>> >> > > Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
>> >> > > ---
>> >> > >  drivers/usb/gadget/udc/atmel_usba_udc.c | 3 ++-
>> >> > >  1 file changed, 2 insertions(+), 1 deletion(-)
>> >> > >
>> >> > > diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
>> >> > > index 45bc997d0711..aec72fe8273c 100644
>> >> > > --- a/drivers/usb/gadget/udc/atmel_usba_udc.c
>> >> > > +++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
>> >> > > @@ -1978,7 +1978,8 @@ static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev,
>> >> > >  			dev_err(&pdev->dev, "of_probe: name error(%d)\n", ret);
>> >> > >  			goto err;
>> >> > >  		}
>> >> > > -		ep->ep.name = kasprintf(GFP_KERNEL, "ep%d", ep->index);
>> >> > > +		ep->ep.name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "ep%d",
>> >> > > +					     ep->index);
>> >> >
>> >> > Acually why bother mallocing such a small string at all.
>> >> > The maximum length is 12 bytes even if 'index' are unrestricted.
>> >> >
>> >> 
>> >> IIRC, using statically allocated string is failing somewhere is the USB
>> >> core but I don't remember all the details.
>> >
>> > I can't imagine that changing ep->ep.name from 'char *' to 'char [12]' would
>> > make any difference.
>> 
>> the actual name is managed by the UDC. Meaning, ep->ep.name should be a
>> pointer, but it could very well just point to ep->name which would be
>> char [12].
>> 
>
> Yeah, I sent a patch that did just that.
> http://lists.infradead.org/pipermail/linux-arm-kernel/2017-January/478602.html

it's in my fixes now :-)
diff mbox

Patch

diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
index 45bc997d0711..aec72fe8273c 100644
--- a/drivers/usb/gadget/udc/atmel_usba_udc.c
+++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
@@ -1978,7 +1978,8 @@  static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev,
 			dev_err(&pdev->dev, "of_probe: name error(%d)\n", ret);
 			goto err;
 		}
-		ep->ep.name = kasprintf(GFP_KERNEL, "ep%d", ep->index);
+		ep->ep.name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "ep%d",
+					     ep->index);
 
 		ep->ep_regs = udc->regs + USBA_EPT_BASE(i);
 		ep->dma_regs = udc->regs + USBA_DMA_BASE(i);