linuxlinux-kernelkmalloc

Kmalloc Alignment


Lets say I allocate with kmalloc an array of uint64_t (and lets assume the size of the array is 32kB). I have the following questions :

1) Is the array guaranteed to be page aligned ? 2) Is the array guaranteed to be cache / block aligned ? 3) Is there no guarantee at all ?

When I allocate the array , and i use virt_to_phys to get the physical address of the array i am gettign physical addresses like 00000040142d5c00 and virtual addresses like fffffe07df400000

Is there any chance that i will end up with alignment smaller than uint64_t , lets say 4 byte alignment or not ?

Thank you in advance


Solution

  • The alignment defined by preprocessor constant ARCH_KMALLOC_MINALIGN,

    it was calculeted like this:

    #if defined(ARCH_DMA_MINALIGN) && ARCH_DMA_MINALIGN > 8
    #define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN
    #define KMALLOC_MIN_SIZE ARCH_DMA_MINALIGN
    #define KMALLOC_SHIFT_LOW ilog2(ARCH_DMA_MINALIGN)
    #else
    #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
    #endif
    

    So in theory __alignof__(unsigned long long) may return some smaller then 8 on some exotic case, but in practice __alignof__(unsigned long long) >= 8, and so ARCH_KMALLOC_MINALIGN would be >= 8.