Linux Kernel Transitions to kmalloc_obj for Improved Memory Allocation
Tech
⚠ Single-source
2h ago

Linux Kernel Transitions to kmalloc_obj for Improved Memory Allocation

AI-synthesized · Bias removed · Facts only

Linus Torvalds has merged a significant code change into the Linux 7.3-rc2 kernel, converting more `kmalloc()` function calls to the newer `kmalloc_obj()` family of functions. This tree-wide effort, spearheaded by Linux developer Kees Cook, aims to improve memory allocation within the kernel.

The conversion, accomplished using a Coccinelle-powered script, impacts hundreds of files and over 600 lines of code. It addresses previous incomplete transitions to `kmalloc_obj()` and expands its use since its introduction in Linux 7.0. The `kmalloc_obj()` functions offer several advantages over `kmalloc()`, including eliminating the need for `sizeof()` and the type name, enabling internal introspection of allocated types, and paving the way for future enhancements like hardening and alignment-aware choices.

According to a commit introducing the `kmalloc_obj()` functions earlier this year, the change replaces common allocation idioms. For example, `ptr = kmalloc(sizeof(*ptr), gfp);` becomes `ptr = kmalloc_obj(*ptr, gfp);`. The primary benefit is the removal of the need for `sizeof()` and the type name, while also enforcing assignment types—the functions return a pointer to the type of the first argument. The type name can still be used for indirect assignments. Additionally, the new functions allow variables to be declared using `__auto_type`, such as `__auto_type ptr = kmalloc_obj(struct foo, gfp);`.

Was this useful?

Read the original coverage

💬 Comments

📜 Comment Policy