News: 0001656773

  ARM Give a man a fire and he's warm for a day, but set fire to him and he's warm for the rest of his life (Terry Pratchett, Jingo)

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

([Linux Kernel] 6 Hours Ago kmalloc_obj Conversion)


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.

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 [1]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."

[2]This merge takes care of more of the conversion for Linux 7.3-rc2.



[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=2932ba8d9c99875b98c951d9d3fd6d651d35df3a

[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1fc5a74b108fc90951890ec513ac81869f5eaff1



<Knghtbrd> 2fort5 sucks enough to have its own gravity ...