REDUCTING SIZE OF TAGS ON LINUX KERNEL
In the Linux Kernel development, even small changes can make difference. Recently, I contributed with a patch that is release with linux kernel 6.16, improving how tag sizes are defined in KASAN (Kernel Addres Sanitizer), one of the main tools for detection memory erros.
What is the KASAN?
KASAN helps to detect the memory bugs in the kernel, such as use-after-frer and overflows. It works by assigning tags to memory allocations, so that invalid access can be intercepted.
KASAN have two modes of operations:
* SW_TAGS (Software Tags): Tags are handled purely in software.
* HW_TAGS (Hardware Tags): Tags are suported directly by the hardware (via ARM MTE - Memory Tagging Extension).
The problem
Before my patch, the tag size (KASAN_TAG_WIDTH) was always degined as 8 bits, regardless of whether KASAN was running in SW_TAGS or HE_TAGS.
However, there was a important detail:
* In HW_TAGS mode, ARM MTE only requires 4 bits to store tags.
* This meant the kernel was allocating more bits than necessary, wasting space in the page flags (critical fields used to store metadata about memory pages).
This, reduced the flexibilit of the kernel to use those bits for another functionalities.
The solution
My patch changes the definition of KASAN_TAG_WIDTH so that it better reflects the requirements of each mode:
#if defined(CONFIG_KASAN_SW_TAGS)
#define KASAN_TAG_WIDTH 8
#elif defined(CONFIG_KASAN_HW_TAGS)
#define KASAN_TAG_WIDTH 4
#else
#define KASAN_TAG_WIDTH 0
#endif
* With SW_TAGS -> 8 bits
* With HW_TAGS -> 4 bits
* With KASAN disabled -> 0
Advantages
This change may look small, but it has pratical benefits:
* Avoids wasting bits in `page flags`
* Free up space for future kernel features that may need those bits.
* Aligns the kernel more closely with the real design of ARM MTE.
Contribution
This improvement was suggested by for Andrey Konovalov, who has a extensive expirence n the memory management and sanitizationi n the Linux Kernel. Receivinv his feedback and applying it helped me refine the patch and learn from someone who has contributed to Linux for a long time.
Links
LKML