1

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

XII
  • 420
  • 4
  • 16

1 Answers1

1

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.

fghj
  • 8,898
  • 4
  • 28
  • 56
  • Thank you very much for your answer sir !!! The fact that i am constantly getting physical addresses like 0x00000040142d5c00 you think is due to the fact that my system has lots of memory (8 gB) and it is after a reboot so there is little to none fragmentation ? – XII May 30 '17 at 08:26
  • @XII I suppose most memory allocation happens during boot, almost all other cases uses memory pools (already allocated memory/memory allocated by big chunks), plus many need virtual memory, not physical, so they use `vmalloc` so it is big probability to get the same address after `kmalloc`. – fghj May 30 '17 at 08:32
  • So, does this mean that kmalloc does not guarantee page/cacheline alignment? Is there another function that needs to be called before kmalloc to ensure cache/page alignment? – szs Oct 27 '21 at 11:11
  • 1
    @szs It was fixed in recent kernels, see https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?h=v5.15-rc7&id=59bb47985c1db229ccff8c5deebecd54fc77d2a9 – fghj Oct 27 '21 at 12:18