From: Dmitry Ilvokhin <d@ilvokhin.com>
To: Peter Zijlstra <peterz@infradead.org>,
Dan Williams <djbw@kernel.org>,
Vishal Verma <vishal.l.verma@intel.com>,
Dave Jiang <dave.jiang@intel.com>,
Ira Weiny <ira.weiny@intel.com>, Miguel Ojeda <ojeda@kernel.org>,
Thomas Gleixner <tglx@kernel.org>,
Christian Brauner <brauner@kernel.org>,
Marco Elver <elver@google.com>, "H. Peter Anvin" <hpa@zytor.com>,
Andrew Morton <akpm@linux-foundation.org>
Cc: nvdimm@lists.linux.dev, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, kernel-team@meta.com,
Dmitry Ilvokhin <d@ilvokhin.com>
Subject: [PATCH v5 3/4] cleanup: Annotate guard constructors with nonnull
Date: Tue, 2 Jun 2026 07:12:52 +0000 [thread overview]
Message-ID: <85fee12eec20abfcf711443518e8f0caec982a86.1780064327.git.d@ilvokhin.com> (raw)
In-Reply-To: <cover.1780064327.git.d@ilvokhin.com>
Add __nonnull_args() to unconditional guard constructors so the compiler
warns when NULL is statically known to be passed:
- DEFINE_GUARD(): re-declare the constructor with __nonnull_args().
- __DEFINE_LOCK_GUARD_1(): annotate the constructor directly.
DEFINE_LOCK_GUARD_0() needs no annotation: its constructor takes no
pointer arguments (.lock is hardcoded to (void *)1).
Define the __nonnull_args() macro in compiler_attributes.h, following
the existing convention for attribute wrappers. Deliberately not named
'__nonnull', to avoid clashing with glibc's __nonnull() when kernel and
userspace headers are combined (User Mode Linux for example).
Signed-off-by: Dmitry Ilvokhin <d@ilvokhin.com>
---
Miguel, I dropped your Acked-by due to the rename. Went with
__nonnull_args() (over __knonnull()). Happy to restore your tag if that
spelling works for you.
include/linux/cleanup.h | 4 +++-
include/linux/compiler_attributes.h | 9 +++++++++
2 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index ea95ca4bc11c..4e60d519713c 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -397,6 +397,7 @@ static __maybe_unused const bool class_##_name##_is_conditional = _is_cond
__DEFINE_GUARD_LOCK_PTR(_name, _T)
#define DEFINE_GUARD(_name, _type, _lock, _unlock) \
+ static __always_inline __nonnull_args() _type class_##_name##_constructor(_type _T); \
DEFINE_CLASS(_name, _type, if (_T) { _unlock; }, ({ _lock; _T; }), _type _T); \
DEFINE_CLASS_IS_GUARD(_name)
@@ -497,7 +498,8 @@ static __always_inline void class_##_name##_destructor(class_##_name##_t *_T) \
__DEFINE_GUARD_LOCK_PTR(_name, &_T->lock)
#define __DEFINE_LOCK_GUARD_1(_name, _type, ...) \
-static __always_inline class_##_name##_t class_##_name##_constructor(_type *l) \
+static __always_inline __nonnull_args() \
+class_##_name##_t class_##_name##_constructor(_type *l) \
__no_context_analysis \
{ \
class_##_name##_t _t = { .lock = l }, *_T = &_t; \
diff --git a/include/linux/compiler_attributes.h b/include/linux/compiler_attributes.h
index c16d4199bf92..cffe09387ea6 100644
--- a/include/linux/compiler_attributes.h
+++ b/include/linux/compiler_attributes.h
@@ -230,6 +230,15 @@
*/
#define noinline __attribute__((__noinline__))
+/*
+ * Note: deliberately not named '__nonnull', to avoid clashing with glibc's
+ * __nonnull() when kernel and userspace headers are combined.
+ *
+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-nonnull
+ * clang: https://clang.llvm.org/docs/AttributeReference.html#nonnull
+ */
+#define __nonnull_args(x...) __attribute__((__nonnull__(x)))
+
/*
* Optional: only supported since gcc >= 8
* Optional: not supported by clang
--
2.53.0-Meta
next prev parent reply other threads:[~2026-06-02 7:21 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-02 7:12 [PATCH v5 0/4] cleanup: Remove NULL check from unconditional guards Dmitry Ilvokhin
2026-06-02 7:12 ` [PATCH v5 1/4] nvdimm: Convert nvdimm_bus guard to class Dmitry Ilvokhin
2026-06-03 14:25 ` [tip: locking/core] " tip-bot2 for Dmitry Ilvokhin
2026-06-02 7:12 ` [PATCH v5 2/4] genirq: Move NULL check into irqdesc_lock guard unlock expression Dmitry Ilvokhin
2026-06-02 20:37 ` Thomas Gleixner
2026-06-03 14:25 ` [tip: locking/core] " tip-bot2 for Dmitry Ilvokhin
2026-06-02 7:12 ` Dmitry Ilvokhin [this message]
2026-06-02 7:32 ` [PATCH v5 3/4] cleanup: Annotate guard constructors with nonnull Miguel Ojeda
2026-06-02 9:19 ` Dmitry Ilvokhin
2026-06-03 14:25 ` [tip: locking/core] " tip-bot2 for Dmitry Ilvokhin
2026-06-05 5:46 ` [PATCH v5 3/4] " Dan Carpenter
2026-06-05 10:46 ` Dmitry Ilvokhin
2026-06-05 13:08 ` [tip: locking/core] cleanup: Specify nonnull argument index tip-bot2 for Dmitry Ilvokhin
2026-06-02 7:12 ` [PATCH v5 4/4] cleanup: Remove NULL check from unconditional guards Dmitry Ilvokhin
2026-06-03 14:25 ` [tip: locking/core] " tip-bot2 for Dmitry Ilvokhin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=85fee12eec20abfcf711443518e8f0caec982a86.1780064327.git.d@ilvokhin.com \
--to=d@ilvokhin.com \
--cc=akpm@linux-foundation.org \
--cc=brauner@kernel.org \
--cc=dave.jiang@intel.com \
--cc=djbw@kernel.org \
--cc=elver@google.com \
--cc=hpa@zytor.com \
--cc=ira.weiny@intel.com \
--cc=kernel-team@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nvdimm@lists.linux.dev \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=tglx@kernel.org \
--cc=vishal.l.verma@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®