Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference of ALIGN() and round_up() macro in the Linux kernel

I'm recently exploring alignment mechanisms inside the Linux kernel, however, I'm confused by two macros, which are ALIGN() and round_up(), they have different implementation but same purpose (as my understanding), which is do the 2^n-bytes round up.

I'm curious about the reason they need two macros with different impl but same output.

Also, they are both inside the same header file, include/linux/kernel.h.

Impl of round_up():

#define __round_mask(x, y) ((__typeof__(x))((y)-1))

#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)

Impl of ALIGN():

#define __ALIGN_KERNEL(x, a)        __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
#define __ALIGN_KERNEL_MASK(x, mask)    (((x) + (mask)) & ~(mask))

#define ALIGN(x, a)     __ALIGN_KERNEL((x), (a))

Thanks!

like image 564
Feng. Ma Avatar asked Oct 15 '25 03:10

Feng. Ma


1 Answers

They are not anymore in the same header. __ALIGN_KERNEL*() now is a part of UAPI. But the main differences I guess are:

a) slightly different implementation and

b) the historical reason, i.e. the author of 9b3be9f99203 ("Move round_up/down to kernel.h") simple hadn't found the counterpart to ALIGN() which appears in the generic headers later, in ed067d4a859f ("linux/kernel.h: Add ALIGN_DOWN macro").

I would recommend to drop an email to LKML with authors of above mentioned commits, Kees Cook, perhaps some other people to ask them to take a look.

like image 176
0andriy Avatar answered Oct 18 '25 04:10

0andriy