malloc memory to be 32 byte aligned in kernel
m silverstri
michael.j.silverstri at gmail.com
Tue Jan 28 20:00:13 EST 2014
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 at 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 at 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))'
More information about the Kernelnewbies
mailing list