diff mbox

[U-Boot,v2,1/6] usb:gadget:ums: Replace malloc calls with memalign to fix cache buffer alignment

Message ID 1391591446-17843-2-git-send-email-l.majewski@samsung.com
State Accepted
Delegated to: Marek Vasut
Headers show

Commit Message

Łukasz Majewski Feb. 5, 2014, 9:10 a.m. UTC
Calls to malloc() have been replaced by memalign. It now provides proper
buffer alignment.

Signed-off-by: Lukasz Majewski <l.majewski@samsung.com>
Cc: Marek Vasut <marex@denx.de>

---
Changes for v2:
- Remove Change-Id.
---
 drivers/usb/gadget/f_mass_storage.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Marek Vasut Feb. 6, 2014, 1:20 a.m. UTC | #1
On Wednesday, February 05, 2014 at 10:10:41 AM, Lukasz Majewski wrote:
> Calls to malloc() have been replaced by memalign. It now provides proper
> buffer alignment.
> 
> Signed-off-by: Lukasz Majewski <l.majewski@samsung.com>
> Cc: Marek Vasut <marex@denx.de>
> 
> ---
> Changes for v2:
> - Remove Change-Id.
> ---
>  drivers/usb/gadget/f_mass_storage.c |    4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/usb/gadget/f_mass_storage.c
> b/drivers/usb/gadget/f_mass_storage.c index b1fe8bd..f896169 100644
> --- a/drivers/usb/gadget/f_mass_storage.c
> +++ b/drivers/usb/gadget/f_mass_storage.c
> @@ -2515,7 +2515,7 @@ static struct fsg_common *fsg_common_init(struct
> fsg_common *common, buffhds_first_it:
>  		bh->inreq_busy = 0;
>  		bh->outreq_busy = 0;
> -		bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
> +		bh->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, FSG_BUFLEN);
>  		if (unlikely(!bh->buf)) {
>  			rc = -ENOMEM;
>  			goto error_release;
> @@ -2622,7 +2622,7 @@ usb_copy_descriptors(struct usb_descriptor_header
> **src) bytes += (*tmp)->bLength;
>  	bytes += (n_desc + 1) * sizeof(*tmp);
> 
> -	mem = kmalloc(bytes, GFP_KERNEL);
> +	mem = memalign(CONFIG_SYS_CACHELINE_SIZE, bytes);

I wonder, does this align the begining of the buffer as well or only the size? 
Can we rely on the fact the begining is also cacheline-aligned ?

>  	if (!mem)
>  		return NULL;

Best regards,
Marek Vasut
Łukasz Majewski Feb. 6, 2014, 6:33 a.m. UTC | #2
Hi Marek,

> On Wednesday, February 05, 2014 at 10:10:41 AM, Lukasz Majewski wrote:
> > Calls to malloc() have been replaced by memalign. It now provides
> > proper buffer alignment.
> > 
> > Signed-off-by: Lukasz Majewski <l.majewski@samsung.com>
> > Cc: Marek Vasut <marex@denx.de>
> > 
> > ---
> > Changes for v2:
> > - Remove Change-Id.
> > ---
> >  drivers/usb/gadget/f_mass_storage.c |    4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/usb/gadget/f_mass_storage.c
> > b/drivers/usb/gadget/f_mass_storage.c index b1fe8bd..f896169 100644
> > --- a/drivers/usb/gadget/f_mass_storage.c
> > +++ b/drivers/usb/gadget/f_mass_storage.c
> > @@ -2515,7 +2515,7 @@ static struct fsg_common
> > *fsg_common_init(struct fsg_common *common, buffhds_first_it:
> >  		bh->inreq_busy = 0;
> >  		bh->outreq_busy = 0;
> > -		bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
> > +		bh->buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
> > FSG_BUFLEN); if (unlikely(!bh->buf)) {
> >  			rc = -ENOMEM;
> >  			goto error_release;
> > @@ -2622,7 +2622,7 @@ usb_copy_descriptors(struct
> > usb_descriptor_header **src) bytes += (*tmp)->bLength;
> >  	bytes += (n_desc + 1) * sizeof(*tmp);
> > 
> > -	mem = kmalloc(bytes, GFP_KERNEL);
> > +	mem = memalign(CONFIG_SYS_CACHELINE_SIZE, bytes);
> 
> I wonder, does this align the begining of the buffer as well or only
> the size? Can we rely on the fact the begining is also
> cacheline-aligned ?

In this case, the memalign assures, that beginning of the buffer is
aligned to a cache line.

It is the programmer's responsibility to either have "bytes" aligned to
cache line size or use a wrapper like DEFINE_CACHE_ALIGN_BUFFER().

> 
> >  	if (!mem)
> >  		return NULL;
> 
> Best regards,
> Marek Vasut
Marek Vasut Feb. 6, 2014, 6:41 a.m. UTC | #3
On Thursday, February 06, 2014 at 07:33:07 AM, Lukasz Majewski wrote:
> Hi Marek,
> 
> > On Wednesday, February 05, 2014 at 10:10:41 AM, Lukasz Majewski wrote:
> > > Calls to malloc() have been replaced by memalign. It now provides
> > > proper buffer alignment.
> > > 
> > > Signed-off-by: Lukasz Majewski <l.majewski@samsung.com>
> > > Cc: Marek Vasut <marex@denx.de>
> > > 
> > > ---
> > > Changes for v2:
> > > - Remove Change-Id.
> > > ---
> > > 
> > >  drivers/usb/gadget/f_mass_storage.c |    4 ++--
> > >  1 file changed, 2 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/drivers/usb/gadget/f_mass_storage.c
> > > b/drivers/usb/gadget/f_mass_storage.c index b1fe8bd..f896169 100644
> > > --- a/drivers/usb/gadget/f_mass_storage.c
> > > +++ b/drivers/usb/gadget/f_mass_storage.c
> > > @@ -2515,7 +2515,7 @@ static struct fsg_common
> > > 
> > > *fsg_common_init(struct fsg_common *common, buffhds_first_it:
> > >  		bh->inreq_busy = 0;
> > >  		bh->outreq_busy = 0;
> > > 
> > > -		bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
> > > +		bh->buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
> > > FSG_BUFLEN); if (unlikely(!bh->buf)) {
> > > 
> > >  			rc = -ENOMEM;
> > >  			goto error_release;
> > > 
> > > @@ -2622,7 +2622,7 @@ usb_copy_descriptors(struct
> > > usb_descriptor_header **src) bytes += (*tmp)->bLength;
> > > 
> > >  	bytes += (n_desc + 1) * sizeof(*tmp);
> > > 
> > > -	mem = kmalloc(bytes, GFP_KERNEL);
> > > +	mem = memalign(CONFIG_SYS_CACHELINE_SIZE, bytes);
> > 
> > I wonder, does this align the begining of the buffer as well or only
> > the size? Can we rely on the fact the begining is also
> > cacheline-aligned ?
> 
> In this case, the memalign assures, that beginning of the buffer is
> aligned to a cache line.

How so ? :)
[...]

Best regards,
Marek Vasut
Łukasz Majewski Feb. 6, 2014, 8:16 a.m. UTC | #4
Hi Marek,

> On Thursday, February 06, 2014 at 07:33:07 AM, Lukasz Majewski wrote:
> > Hi Marek,
> > 
> > > On Wednesday, February 05, 2014 at 10:10:41 AM, Lukasz Majewski
> > > wrote:
> > > > Calls to malloc() have been replaced by memalign. It now
> > > > provides proper buffer alignment.
> > > > 
> > > > Signed-off-by: Lukasz Majewski <l.majewski@samsung.com>
> > > > Cc: Marek Vasut <marex@denx.de>
> > > > 
> > > > ---
> > > > Changes for v2:
> > > > - Remove Change-Id.
> > > > ---
> > > > 
> > > >  drivers/usb/gadget/f_mass_storage.c |    4 ++--
> > > >  1 file changed, 2 insertions(+), 2 deletions(-)
> > > > 
> > > > diff --git a/drivers/usb/gadget/f_mass_storage.c
> > > > b/drivers/usb/gadget/f_mass_storage.c index b1fe8bd..f896169
> > > > 100644 --- a/drivers/usb/gadget/f_mass_storage.c
> > > > +++ b/drivers/usb/gadget/f_mass_storage.c
> > > > @@ -2515,7 +2515,7 @@ static struct fsg_common
> > > > 
> > > > *fsg_common_init(struct fsg_common *common, buffhds_first_it:
> > > >  		bh->inreq_busy = 0;
> > > >  		bh->outreq_busy = 0;
> > > > 
> > > > -		bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
> > > > +		bh->buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
> > > > FSG_BUFLEN); if (unlikely(!bh->buf)) {
> > > > 
> > > >  			rc = -ENOMEM;
> > > >  			goto error_release;
> > > > 
> > > > @@ -2622,7 +2622,7 @@ usb_copy_descriptors(struct
> > > > usb_descriptor_header **src) bytes += (*tmp)->bLength;
> > > > 
> > > >  	bytes += (n_desc + 1) * sizeof(*tmp);
> > > > 
> > > > -	mem = kmalloc(bytes, GFP_KERNEL);
> > > > +	mem = memalign(CONFIG_SYS_CACHELINE_SIZE, bytes);
> > > 
> > > I wonder, does this align the begining of the buffer as well or
> > > only the size? Can we rely on the fact the begining is also
> > > cacheline-aligned ?
> > 
> > In this case, the memalign assures, that beginning of the buffer is
> > aligned to a cache line.
> 
> How so ? :)

I'm just following what is written at ./include/malloc.h  :-)

memalign(size_t alignment, size_t n);
     Return a pointer to a newly allocated chunk of n bytes, aligned
     in accord with the alignment argument, which must be a power of
     two.

Usage of memalign seemed to fix the wrong cache alignment buffer
allocation issue in a code which I know.

> [...]
> 
> Best regards,
> Marek Vasut
diff mbox

Patch

diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
index b1fe8bd..f896169 100644
--- a/drivers/usb/gadget/f_mass_storage.c
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -2515,7 +2515,7 @@  static struct fsg_common *fsg_common_init(struct fsg_common *common,
 buffhds_first_it:
 		bh->inreq_busy = 0;
 		bh->outreq_busy = 0;
-		bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
+		bh->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, FSG_BUFLEN);
 		if (unlikely(!bh->buf)) {
 			rc = -ENOMEM;
 			goto error_release;
@@ -2622,7 +2622,7 @@  usb_copy_descriptors(struct usb_descriptor_header **src)
 		bytes += (*tmp)->bLength;
 	bytes += (n_desc + 1) * sizeof(*tmp);
 
-	mem = kmalloc(bytes, GFP_KERNEL);
+	mem = memalign(CONFIG_SYS_CACHELINE_SIZE, bytes);
 	if (!mem)
 		return NULL;