Kernel series forces kobject release callbacks, fixing latent use-after-frees
A Clang attribute now requires every kobj_type to name a release method, exposing leaks and use-after-frees in DLM, UBIFS, AMDGPU, Xen, and others.
Ian Rogers has posted a nine-patch series that makes the kernel compiler-check a rule the core documentation has long called non-negotiable: every kobject type must name a release method, and the object must stay alive until that method runs. The series both adds the check and repairs several subsystems that had been violating it, including cases that become use-after-frees when delayed kobject cleanup is enabled.
The mechanism is Clang's require_explicit_initialization attribute, exposed in the kernel as __must_init after a shorter spelling suggested by Greg Kroah-Hartman. Applied to the release field of a kobject type, an omitted initializer is a warning. The idea was discussed at the Linux Plumbers Conference; Kees Cook had carried an earlier version that missed the 7.4 merge window and asked Rogers to land the renamed attribute with this series. Danilo Krummrich offered to take the set through the driver-core tree once subsystem maintainers signed off.
The audit found real lifetime bugs, not just missing annotations. The distributed lock manager freed a lockspace while a delayed kobject release could still touch it, and unloading the module in that window could fault on an unmapped instruction page. UBIFS kept a static kset with no release callback, so unloading the filesystem while delayed cleanup was pending used memory after free. AMD's KFD queue code freed a queue immediately after dropping the kobject, the same class of bug under the debug delay. Elsewhere the failure was a leak: PowerPC's secure-variable sysfs objects and SuperH store-queue objects were allocated dynamically and then put without a callback that would free them. Xen's hypervisor sysfs code overwrote a dynamically created kobject's type with one that had no release method at all.
Rogers kept the new free helpers local rather than exporting a shared "just kfree the kobject" function, which Juergen Gross had suggested. A new driver-core export would complicate backports of the Fixes-tagged patches, and most kobjects are embedded in a larger structure rather than allocated on their own.
Sites whose kobjects are static, per-CPU, permanent firmware objects, or owned by a parent or devm allocation are left with an explicit null release and a comment. Rogers noted that many of those are still unsafe if the backing memory disappears while delayed cleanup is pending, and left those lifetime fixes for later. PowerPC and SuperH maintainers have since acknowledged the patches that were still waiting on them.