malloc memory to be 32 byte aligned in kernel
Hi, I am writing a kernel driver, can you please tell me how can I allocate a buffer which is 32 byte aligned? Thank you.
On Tue, Jan 28, 2014 at 11:20 AM, m silverstri <michael.j.silverstri@gmail.com> wrote:
Hi,
I am writing a kernel driver, can you please tell me how can I allocate a buffer which is 32 byte aligned? malloc already aligns memory for basic data types AFAIK
Thank you.
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
On Tue, 28 Jan 2014 15:32:37 -0800, anish singh said:
On Tue, Jan 28, 2014 at 11:20 AM, m silverstri <michael.j.silverstri@gmail.com> wrote:
Hi,
I am writing a kernel driver, can you please tell me how can I allocate a buffer which is 32 byte aligned? malloc already aligns memory for basic data types AFAIK
Yes, but even if it allocates on a long-long boundary, he can still get hosed if it ends up on a 8-byte boundary that's *not* a 32-byte aligned. Your best approach is probably to use the KMEM_CACHE() macro to create a slab cache, and then allocate from that slab. See include/linux/slab.h /* * Please use this macro to create slab caches. Simply specify the * name of the structure and maybe some flags that are listed above. * * The alignment of the struct determines object alignment. If you * f.e. add ____cacheline_aligned_in_smp to the struct declaration * then the objects will be properly aligned in SMP configurations. */ #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct, sizeof(struct __struct), __alignof__(struct __struct), (__flags), NULL) Oh, and you'll need to declare a 'struct foo {...} __attribute__(aligned(32))'
Thanks. How big should be my slab cache and how to allocate from that? And to declare a struct ' __attribute__(aligned(32))'', does that mean I do that for every file in my struct? On Tue, Jan 28, 2014 at 4:15 PM, <Valdis.Kletnieks@vt.edu> wrote:
On Tue, 28 Jan 2014 15:32:37 -0800, anish singh said:
On Tue, Jan 28, 2014 at 11:20 AM, m silverstri <michael.j.silverstri@gmail.com> wrote:
Hi,
I am writing a kernel driver, can you please tell me how can I allocate a buffer which is 32 byte aligned? malloc already aligns memory for basic data types AFAIK
Yes, but even if it allocates on a long-long boundary, he can still get hosed if it ends up on a 8-byte boundary that's *not* a 32-byte aligned.
Your best approach is probably to use the KMEM_CACHE() macro to create a slab cache, and then allocate from that slab. See include/linux/slab.h
/* * Please use this macro to create slab caches. Simply specify the * name of the structure and maybe some flags that are listed above. * * The alignment of the struct determines object alignment. If you * f.e. add ____cacheline_aligned_in_smp to the struct declaration * then the objects will be properly aligned in SMP configurations. */ #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct, sizeof(struct __struct), __alignof__(struct __struct), (__flags), NULL)
Oh, and you'll need to declare a 'struct foo {...} __attribute__(aligned(32))'
On Tue, 28 Jan 2014 17:00:13 -0800, m silverstri said:
Thanks. How big should be my slab cache and how to allocate from that?
kmem_cache_alloc() and kmem_cache_free() are your friends.
And to declare a struct ' __attribute__(aligned(32))'', does that mean I do that for every file in my struct?
I'm guessing you mean "for every field in my struct". And the answer is no, unless you actually need the alignment/padding for every field. You declare the struct wrapping all the stuff you need and give that the aligned().
participants (3)
-
anish singh -
m silverstri -
Valdis.Kletnieks@vt.edu