[PATCH v4 1/9] Compiler Attributes: Add __must_init macro
- From
- Ian Rogers <irogers@google.com>
- Date
- 2026-11-19 19:25:35
- Also in
- dri-devel, driver-core, gfs2, linux-acpi, linux-doc, linux-fsdevel, linux-hardening, linux-pm, linux-s390, linux-sh, lkml, llvm, xen-devel
- Reply to
- Ian Rogers, 2026-11-19
Clang supports the 'require_explicit_initialization' attribute on struct fields to indicate that a field must be initialized explicitly by the user when the object is constructed. When an aggregate initializer or uninitialized variable omits an annotated field, Clang emits a -Wuninitialized-explicit-init warning (enabled by default). Add the __must_init macro to include/linux/compiler_attributes.h (which is included by include/linux/compiler_types.h and include/linux/compiler.h) so kernel structs with error-prone initialization (such as mandatory callbacks or cleanup handlers) can enforce explicit initialization at compile time, as discussed at the Linux Plumbers Conference. The shorter __must_init spelling, rather than the attribute's full name, keeps annotated struct members readable and was suggested by Greg Kroah-Hartman. Note that Clang versions prior to the fix for https://github.com/llvm/llvm-project/issues/232483 do not diagnose file-scope objects declared without any initializer (initializers at file scope that omit the field are diagnosed). Also teach Sphinx (Documentation/conf.py), kernel-doc (tools/lib/python/kdoc/xforms_lists.py), and checkpatch.pl about the new attribute macro. Link: https://lpc.events/event/20/contributions/2392/ Link: https://lore.kernel.org/lkml/2026101532-fantastic-deafness-e04c@gregkh/ (local) Acked-by: Miguel Ojeda <ojeda@kernel.org> Reviewed-by: Nick Desaulniers <ndesaulniers@google.com> Reviewed-by: Nathan Chancellor <nathan@kernel.org> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Kees Cook <kees@kernel.org> Signed-off-by: Ian Rogers <irogers@google.com> --- Documentation/conf.py | 1 + include/linux/compiler_attributes.h | 12 ++++++++++++ scripts/checkpatch.pl | 1 + tools/lib/python/kdoc/xforms_lists.py | 1 + 4 files changed, 15 insertions(+)
diff --git a/Documentation/conf.py b/Documentation/conf.py
index 5ab26ce89daf..baaa4035258c 100644
--- a/Documentation/conf.py
+++ b/Documentation/conf.py@@ -203,6 +203,7 @@ c_id_attributes = [ "__noreturn", "__packed", "__pure", + "__must_init", "__section", "__always_unused", "__maybe_unused",
diff --git a/include/linux/compiler_attributes.h b/include/linux/compiler_attributes.h
index 476c4c560d17..b9c0b56dd22e 100644
--- a/include/linux/compiler_attributes.h
+++ b/include/linux/compiler_attributes.h@@ -322,6 +322,18 @@ */ #define __pure __attribute__((__pure__)) +/* + * Optional: not supported by gcc + * Optional: only supported since clang >= 20.0 + * + * clang: https://clang.llvm.org/docs/AttributeReference.html#require-explicit-initialization + */ +#if __has_attribute(__require_explicit_initialization__) +# define __must_init __attribute__((__require_explicit_initialization__)) +#else +# define __must_init +#endif + /* * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-section-function-attribute * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-section-variable-attribute
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index b8b64f720191..6a091c45b1a1 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl@@ -6888,6 +6888,7 @@ sub process { "noreturn" => "__noreturn", "packed" => "__packed", "pure" => "__pure", + "require_explicit_initialization" => "__must_init", "section" => "__section", "used" => "__used", "weak" => "__weak"
diff --git a/tools/lib/python/kdoc/xforms_lists.py b/tools/lib/python/kdoc/xforms_lists.py
index db23fb650ea8..42956b98aae3 100644
--- a/tools/lib/python/kdoc/xforms_lists.py
+++ b/tools/lib/python/kdoc/xforms_lists.py@@ -36,6 +36,7 @@ class CTransforms: (CMatch("CRYPTO_MINALIGN_ATTR"), ""), (CMatch("__private"), ""), (CMatch("__rcu"), ""), + (CMatch("__must_init"), ""), (CMatch("____cacheline_aligned_in_smp"), ""), (CMatch("____cacheline_aligned"), ""), (CMatch("__cacheline_group_(?:begin|end)"), ""),
--
2.56.1.708.g77570a6a1d-goog