Linux 7.3-rc2 Transitions More kmalloc Calls To The Superior kmalloc_obj

Written by Michael Larabel in Linux Kernel on 6 September 2026 at 06:28 AM EDT. 8 Comments
LINUX KERNEL
Ahead of the Linux 7.3-rc2 kernel release due out later today, a merge causing a lot of code churn was pulled in by Linus Torvalds. This tree-wide effort converts more of the kmalloc() function calls over to the newer and superior kmalloc_obj() family of functions.

Linux developer Kees Cook ran the tree-wide conversion of converting more kmalloc() allocations over to using kmalloc_obj() instead with the Coccinelle-powered script. This addresses some kmalloc usage that previously wasn't transitioned over as well as for expanded use since the kmalloc_obj() code was introduced in Linux 7.0. It touches hundreds of files and changing over 600 lines of code of kmalloc() allocations.

kmalloc to kmalloc_obj


The kmalloc_obj() functions are cleaner with not needing sizeof() nnor the type name, internal introspection of the allocated type is possible, and allowing for future work like additional hardening and alignment-aware choices. The commit earlier this year introducing kmalloc_obj() functions explained:
"Introduce type-aware kmalloc-family helpers to replace the common idioms for single object and arrays of objects allocation:

ptr = kmalloc(sizeof(*ptr), gfp);
ptr = kmalloc(sizeof(struct some_obj_name), gfp);
ptr = kzalloc(sizeof(*ptr), gfp);
ptr = kmalloc_array(count, sizeof(*ptr), gfp);
ptr = kcalloc(count, sizeof(*ptr), gfp);

These become, respectively:

ptr = kmalloc_obj(*ptr, gfp);
ptr = kmalloc_obj(*ptr, gfp);
ptr = kzalloc_obj(*ptr, gfp);
ptr = kmalloc_objs(*ptr, count, gfp);
ptr = kzalloc_objs(*ptr, count, gfp);

Beyond the other benefits outlined below, the primary ergonomic benefit is the elimination of needing "sizeof" nor the type name, and the enforcement of assignment types (they do not return "void *", but rather a pointer to the type of the first argument). The type name _can_ be used, though, in the case where an assignment is indirect (e.g. via "return"). This additionally allows[1] variables to be declared via __auto_type:

__auto_type ptr = kmalloc_obj(struct foo, gfp);

Internal introspection of the allocated type now becomes possible, allowing for future alignment-aware choices to be made by the allocator and future hardening work that can be type sensitive. For example, adding __alignof(*ptr) as an argument to the internal allocators so that appropriate/efficient alignment choices can be made, or being able to correctly choose per-allocation offset randomization within a bucket that does not break alignment requirements."

This merge takes care of more of the conversion for Linux 7.3-rc2.
Related News
About The Author

Michael Larabel is the principal author of Phoronix.com and founded the site in 2004 with a focus on enriching the Linux hardware experience. Michael has written more than 20,000 articles covering the state of Linux hardware support, Linux performance, graphics drivers, and other topics. Michael is also the lead developer of the Phoronix Test Suite, Phoromatic, and OpenBenchmarking.org automated benchmarking software. He can be followed via Twitter, LinkedIn, or contacted via MichaelLarabel.com.

Popular News This Week