malloc memory to be 32 byte aligned in kernel
From: m silverstri <hidden>
Date: 2014-01-29 01:00:13
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, [off-list ref] wrote:
On Tue, 28 Jan 2014 15:32:37 -0800, anish singh said:quoted
On Tue, Jan 28, 2014 at 11:20 AM, m silverstri [off-list ref] wrote:quoted
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 AFAIKYes, 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))'