* [PATCH v5 0/4] cleanup: Remove NULL check from unconditional guards
@ 2026-06-02 7:12 Dmitry Ilvokhin
2026-06-02 7:12 ` [PATCH v5 1/4] nvdimm: Convert nvdimm_bus guard to class Dmitry Ilvokhin
` (3 more replies)
0 siblings, 4 replies; 14+ messages in thread
From: Dmitry Ilvokhin @ 2026-06-02 7:12 UTC (permalink / raw)
To: Peter Zijlstra, Dan Williams, Vishal Verma, Dave Jiang, Ira Weiny,
Miguel Ojeda, Thomas Gleixner, Christian Brauner, Marco Elver,
H. Peter Anvin, Andrew Morton
Cc: nvdimm, linux-kernel, linux-mm, kernel-team, Dmitry Ilvokhin
Unconditional guard destructors have dead NULL checks. The lock operation in
the constructor would crash before the destructor ever runs with NULL.
- Patches 1-2 prepare guards that legitimately handle NULL.
- Patch 3 adds __nonnull_args() to guard constructors for compile-time
enforcement.
- Patch 4 removes the dead checks.
As compiled by GCC-11 with defconfig on top of the locking/core:
Total: Before=23889980, After=23834334, chg -0.23%
Changes in v5:
- Renamed __nonnull() attribute to __nonnull_args() to fix User Mode Linux
build failures (kernel test robot).
- Dropped Acked-by tag from Miguel, since I renamed the attribute.
Changes in v4:
- Re-worded commit message paragraph about nonnull "compiler-enforced
verification" (Miguel Ojeda).
- Fixed GCC documentation link for nonnull() attribute (Miguel Ojeda).
- Placed nonnull() before nonstring() in
include/linux/compiler_attributes.h (Miguel Ojeda).
- Picked up tags, where appropriate.
Changes in v3:
- Audited usages of DEFINE_GUARD(), __DEFINE_UNLOCK_GUARD() and
DEFINE_LOCK_GUARD_1() to make sure NULL check removal will work correctly
(Peter Zijlstra).
- Moved NULL check into irqdesc_lock unlock expression (Peter Zijlstra).
- Added compiler-enforced nonnull() check for guard constructors.
- Converted nvdimm_bus guard to class.
Changes in v2:
- Expand commit message with detailed reasoning, why the proposed
change is correct.
- Rebase on top of locking/core.
v4: https://lore.kernel.org/all/cover.1779286416.git.d@ilvokhin.com/
v3: https://lore.kernel.org/all/cover.1779116497.git.d@ilvokhin.com/
v2: https://lore.kernel.org/all/20260512071510.92451-1-d@ilvokhin.com/
v1: https://lore.kernel.org/all/20260427165037.205337-1-d@ilvokhin.com/
See also [1] for relevant discussion.
[1]: https://lore.kernel.org/all/afCS4d4YccQFtvpi@shell.ilvokhin.com/
Dmitry Ilvokhin (4):
nvdimm: Convert nvdimm_bus guard to class
genirq: Move NULL check into irqdesc_lock guard unlock expression
cleanup: Annotate guard constructors with nonnull
cleanup: Remove NULL check from unconditional guards
drivers/nvdimm/nd.h | 7 +++++--
include/linux/cleanup.h | 8 +++++---
include/linux/compiler_attributes.h | 9 +++++++++
kernel/irq/internals.h | 2 +-
4 files changed, 20 insertions(+), 6 deletions(-)
--
2.53.0-Meta
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v5 1/4] nvdimm: Convert nvdimm_bus guard to class
2026-06-02 7:12 [PATCH v5 0/4] cleanup: Remove NULL check from unconditional guards Dmitry Ilvokhin
@ 2026-06-02 7:12 ` 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
` (2 subsequent siblings)
3 siblings, 1 reply; 14+ messages in thread
From: Dmitry Ilvokhin @ 2026-06-02 7:12 UTC (permalink / raw)
To: Peter Zijlstra, Dan Williams, Vishal Verma, Dave Jiang, Ira Weiny,
Miguel Ojeda, Thomas Gleixner, Christian Brauner, Marco Elver,
H. Peter Anvin, Andrew Morton
Cc: nvdimm, linux-kernel, linux-mm, kernel-team, Dmitry Ilvokhin
The nvdimm_bus guard accepts NULL and skips locking when NULL is passed.
Convert from DEFINE_GUARD() to DEFINE_CLASS() + DEFINE_CLASS_IS_GUARD().
This is a preparatory change for making DEFINE_GUARD() constructors
__nonnull_args(). nvdimm_bus legitimately passes NULL, so it must be
adjusted to avoid a compile error.
No functional change.
Signed-off-by: Dmitry Ilvokhin <d@ilvokhin.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
---
drivers/nvdimm/nd.h | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/drivers/nvdimm/nd.h b/drivers/nvdimm/nd.h
index b199eea3260e..18b64559664b 100644
--- a/drivers/nvdimm/nd.h
+++ b/drivers/nvdimm/nd.h
@@ -632,8 +632,11 @@ u64 nd_region_interleave_set_cookie(struct nd_region *nd_region,
u64 nd_region_interleave_set_altcookie(struct nd_region *nd_region);
void nvdimm_bus_lock(struct device *dev);
void nvdimm_bus_unlock(struct device *dev);
-DEFINE_GUARD(nvdimm_bus, struct device *,
- if (_T) nvdimm_bus_lock(_T), if (_T) nvdimm_bus_unlock(_T));
+DEFINE_CLASS(nvdimm_bus, struct device *,
+ if (_T) nvdimm_bus_unlock(_T),
+ ({ if (_T) nvdimm_bus_lock(_T); _T; }),
+ struct device *_T);
+DEFINE_CLASS_IS_GUARD(nvdimm_bus);
bool is_nvdimm_bus_locked(struct device *dev);
void nvdimm_check_and_set_ro(struct gendisk *disk);
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v5 2/4] genirq: Move NULL check into irqdesc_lock guard unlock expression
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-02 7:12 ` 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 ` [PATCH v5 3/4] cleanup: Annotate guard constructors with nonnull Dmitry Ilvokhin
2026-06-02 7:12 ` [PATCH v5 4/4] cleanup: Remove NULL check from unconditional guards Dmitry Ilvokhin
3 siblings, 2 replies; 14+ messages in thread
From: Dmitry Ilvokhin @ 2026-06-02 7:12 UTC (permalink / raw)
To: Peter Zijlstra, Dan Williams, Vishal Verma, Dave Jiang, Ira Weiny,
Miguel Ojeda, Thomas Gleixner, Christian Brauner, Marco Elver,
H. Peter Anvin, Andrew Morton
Cc: nvdimm, linux-kernel, linux-mm, kernel-team, Dmitry Ilvokhin
irqdesc_lock uses __DEFINE_UNLOCK_GUARD() directly with a custom
constructor that can set .lock to NULL.
In preparation for removing the NULL check from __DEFINE_UNLOCK_GUARD(),
move the NULL check into the irqdesc_lock unlock expression, making the
NULL handling explicit at the call site.
No functional change.
Signed-off-by: Dmitry Ilvokhin <d@ilvokhin.com>
---
kernel/irq/internals.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/kernel/irq/internals.h b/kernel/irq/internals.h
index 9412e57056f5..347cb333b9fe 100644
--- a/kernel/irq/internals.h
+++ b/kernel/irq/internals.h
@@ -171,7 +171,7 @@ void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
__DEFINE_CLASS_IS_CONDITIONAL(irqdesc_lock, true);
__DEFINE_UNLOCK_GUARD(irqdesc_lock, struct irq_desc,
- __irq_put_desc_unlock(_T->lock, _T->flags, _T->bus),
+ if (_T->lock) __irq_put_desc_unlock(_T->lock, _T->flags, _T->bus),
unsigned long flags; bool bus);
static inline class_irqdesc_lock_t class_irqdesc_lock_constructor(unsigned int irq, bool bus,
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v5 3/4] cleanup: Annotate guard constructors with nonnull
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-02 7:12 ` [PATCH v5 2/4] genirq: Move NULL check into irqdesc_lock guard unlock expression Dmitry Ilvokhin
@ 2026-06-02 7:12 ` Dmitry Ilvokhin
[not found] ` <CANiq72=vaiGXPwmdOuTHDp30Nm62UgjtL1STJx6aj=dDPWTQYA@mail.gmail.com>
` (2 more replies)
2026-06-02 7:12 ` [PATCH v5 4/4] cleanup: Remove NULL check from unconditional guards Dmitry Ilvokhin
3 siblings, 3 replies; 14+ messages in thread
From: Dmitry Ilvokhin @ 2026-06-02 7:12 UTC (permalink / raw)
To: Peter Zijlstra, Dan Williams, Vishal Verma, Dave Jiang, Ira Weiny,
Miguel Ojeda, Thomas Gleixner, Christian Brauner, Marco Elver,
H. Peter Anvin, Andrew Morton
Cc: nvdimm, linux-kernel, linux-mm, kernel-team, Dmitry Ilvokhin
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
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v5 4/4] cleanup: Remove NULL check from unconditional guards
2026-06-02 7:12 [PATCH v5 0/4] cleanup: Remove NULL check from unconditional guards Dmitry Ilvokhin
` (2 preceding siblings ...)
2026-06-02 7:12 ` [PATCH v5 3/4] cleanup: Annotate guard constructors with nonnull Dmitry Ilvokhin
@ 2026-06-02 7:12 ` Dmitry Ilvokhin
2026-06-03 14:25 ` [tip: locking/core] " tip-bot2 for Dmitry Ilvokhin
3 siblings, 1 reply; 14+ messages in thread
From: Dmitry Ilvokhin @ 2026-06-02 7:12 UTC (permalink / raw)
To: Peter Zijlstra, Dan Williams, Vishal Verma, Dave Jiang, Ira Weiny,
Miguel Ojeda, Thomas Gleixner, Christian Brauner, Marco Elver,
H. Peter Anvin, Andrew Morton
Cc: nvdimm, linux-kernel, linux-mm, kernel-team, Dmitry Ilvokhin
The unconditional guard destructors check whether the lock pointer is
NULL before unlocking. This check is dead code because unconditional
guards guarantee a non-NULL lock pointer at destructor time.
DEFINE_GUARD() runs the lock operation unconditionally in the
constructor. If the pointer were NULL, the lock operation (e.g.
mutex_lock(NULL)) would crash before the constructor returns. The
destructor never runs with a NULL pointer. All DEFINE_GUARD() users
dereference the pointer in their lock. Verified by auditing every
instance found by: git grep -n -A 1 'DEFINE_GUARD('. The only exception
is xe_pm_runtime_release_only, whose constructor is a noop, but it has
no callers.
__DEFINE_UNLOCK_GUARD() has only a few usages outside of
include/linux/cleanup.h: tty_port_tty (NULL-checks in its tty_kref_put()
call), irqdesc_lock (fixed earlier) and two guards in
kernel/sched/sched.h (dereference the pointer unconditionally in their
lock constructors).
DEFINE_LOCK_GUARD_1() sets .lock from its argument and runs the lock
operation in the constructor. Same reasoning applies. All
DEFINE_LOCK_GUARD_1() users dereference the pointer in their lock. Also,
verified by auditing every match of: git grep -n 'DEFINE_LOCK_GUARD_1('.
DEFINE_LOCK_GUARD_0() hardcodes .lock = (void *)1 in the constructor,
so it is never NULL by construction.
Conditional (_try) variants: DEFINE_GUARD_COND() and
DEFINE_LOCK_GUARD_1_COND() use EXTEND_CLASS_COND(), whose wrapper
destructor returns early when the lock was not acquired, before reaching
the base destructor since commit 2deccd5c862a ("cleanup: Optimize
guards"):
if (_cond) return; class_##_name##_destructor(_T);
As compiled by GCC-11 with defconfig on top of the locking/core:
Total: Before=23889980, After=23834334, chg -0.23%
Signed-off-by: Dmitry Ilvokhin <d@ilvokhin.com>
---
include/linux/cleanup.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index 4e60d519713c..65416938e318 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -398,7 +398,7 @@ static __maybe_unused const bool class_##_name##_is_conditional = _is_cond
#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(_name, _type, _unlock, ({ _lock; _T; }), _type _T); \
DEFINE_CLASS_IS_GUARD(_name)
#define DEFINE_GUARD_COND_4(_name, _ext, _lock, _cond) \
@@ -492,7 +492,7 @@ typedef struct { \
static __always_inline void class_##_name##_destructor(class_##_name##_t *_T) \
__no_context_analysis \
{ \
- if (_T->lock) { _unlock; } \
+ _unlock; \
} \
\
__DEFINE_GUARD_LOCK_PTR(_name, &_T->lock)
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 14+ messages in thread
* Re: [PATCH v5 3/4] cleanup: Annotate guard constructors with nonnull
[not found] ` <CANiq72=vaiGXPwmdOuTHDp30Nm62UgjtL1STJx6aj=dDPWTQYA@mail.gmail.com>
@ 2026-06-02 9:19 ` Dmitry Ilvokhin
0 siblings, 0 replies; 14+ messages in thread
From: Dmitry Ilvokhin @ 2026-06-02 9:19 UTC (permalink / raw)
To: Miguel Ojeda
Cc: Peter Zijlstra, Dan Williams, Vishal Verma, Dave Jiang, Ira Weiny,
Miguel Ojeda, Thomas Gleixner, Christian Brauner, Marco Elver,
H. Peter Anvin, Andrew Morton, nvdimm, linux-kernel, linux-mm,
kernel-team
On Tue, Jun 02, 2026 at 09:32:12AM +0200, Miguel Ojeda wrote:
> On Tue, Jun 2, 2026 at 9:13 AM Dmitry Ilvokhin <d@ilvokhin.com> wrote:
> >
> > 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.
>
> I am fine with either, but thanks for the caution! :)
Thanks for confirmation, Miguel.
>
> I assume `_args()` is meant as "the `nonnull` for the arguments, not
> the return"?
Yes, that is exactly what I had in mind.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v5 2/4] genirq: Move NULL check into irqdesc_lock guard unlock expression
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
1 sibling, 0 replies; 14+ messages in thread
From: Thomas Gleixner @ 2026-06-02 20:37 UTC (permalink / raw)
To: Dmitry Ilvokhin, Peter Zijlstra, Dan Williams, Vishal Verma,
Dave Jiang, Ira Weiny, Miguel Ojeda, Christian Brauner,
Marco Elver, H. Peter Anvin, Andrew Morton
Cc: nvdimm, linux-kernel, linux-mm, kernel-team, Dmitry Ilvokhin
On Tue, Jun 02 2026 at 07:12, Dmitry Ilvokhin wrote:
> irqdesc_lock uses __DEFINE_UNLOCK_GUARD() directly with a custom
> constructor that can set .lock to NULL.
>
> In preparation for removing the NULL check from __DEFINE_UNLOCK_GUARD(),
> move the NULL check into the irqdesc_lock unlock expression, making the
> NULL handling explicit at the call site.
>
> No functional change.
>
> Signed-off-by: Dmitry Ilvokhin <d@ilvokhin.com>
Acked-by: Thomas Gleixner <tglx@kernel.org>
^ permalink raw reply [flat|nested] 14+ messages in thread
* [tip: locking/core] cleanup: Remove NULL check from unconditional guards
2026-06-02 7:12 ` [PATCH v5 4/4] cleanup: Remove NULL check from unconditional guards Dmitry Ilvokhin
@ 2026-06-03 14:25 ` tip-bot2 for Dmitry Ilvokhin
0 siblings, 0 replies; 14+ messages in thread
From: tip-bot2 for Dmitry Ilvokhin @ 2026-06-03 14:25 UTC (permalink / raw)
To: linux-tip-commits
Cc: Dmitry Ilvokhin, Peter Zijlstra (Intel), x86, linux-kernel
The following commit has been merged into the locking/core branch of tip:
Commit-ID: a13ab9dd6eb2a89f14b466c3884730ea7969253f
Gitweb: https://git.kernel.org/tip/a13ab9dd6eb2a89f14b466c3884730ea7969253f
Author: Dmitry Ilvokhin <d@ilvokhin.com>
AuthorDate: Tue, 02 Jun 2026 07:12:53
Committer: Peter Zijlstra <peterz@infradead.org>
CommitterDate: Wed, 03 Jun 2026 11:38:48 +02:00
cleanup: Remove NULL check from unconditional guards
The unconditional guard destructors check whether the lock pointer is
NULL before unlocking. This check is dead code because unconditional
guards guarantee a non-NULL lock pointer at destructor time.
DEFINE_GUARD() runs the lock operation unconditionally in the
constructor. If the pointer were NULL, the lock operation (e.g.
mutex_lock(NULL)) would crash before the constructor returns. The
destructor never runs with a NULL pointer. All DEFINE_GUARD() users
dereference the pointer in their lock. Verified by auditing every
instance found by: git grep -n -A 1 'DEFINE_GUARD('. The only exception
is xe_pm_runtime_release_only, whose constructor is a noop, but it has
no callers.
__DEFINE_UNLOCK_GUARD() has only a few usages outside of
include/linux/cleanup.h: tty_port_tty (NULL-checks in its tty_kref_put()
call), irqdesc_lock (fixed earlier) and two guards in
kernel/sched/sched.h (dereference the pointer unconditionally in their
lock constructors).
DEFINE_LOCK_GUARD_1() sets .lock from its argument and runs the lock
operation in the constructor. Same reasoning applies. All
DEFINE_LOCK_GUARD_1() users dereference the pointer in their lock. Also,
verified by auditing every match of: git grep -n 'DEFINE_LOCK_GUARD_1('.
DEFINE_LOCK_GUARD_0() hardcodes .lock = (void *)1 in the constructor,
so it is never NULL by construction.
Conditional (_try) variants: DEFINE_GUARD_COND() and
DEFINE_LOCK_GUARD_1_COND() use EXTEND_CLASS_COND(), whose wrapper
destructor returns early when the lock was not acquired, before reaching
the base destructor since commit 2deccd5c862a ("cleanup: Optimize
guards"):
if (_cond) return; class_##_name##_destructor(_T);
As compiled by GCC-11 with defconfig on top of the locking/core:
Total: Before=23889980, After=23834334, chg -0.23%
Signed-off-by: Dmitry Ilvokhin <d@ilvokhin.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/0503a089389b2270c478a873e095cf0a4ff26d24.1780064327.git.d@ilvokhin.com
---
include/linux/cleanup.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index 4e60d51..6541693 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -398,7 +398,7 @@ static __maybe_unused const bool class_##_name##_is_conditional = _is_cond
#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(_name, _type, _unlock, ({ _lock; _T; }), _type _T); \
DEFINE_CLASS_IS_GUARD(_name)
#define DEFINE_GUARD_COND_4(_name, _ext, _lock, _cond) \
@@ -492,7 +492,7 @@ typedef struct { \
static __always_inline void class_##_name##_destructor(class_##_name##_t *_T) \
__no_context_analysis \
{ \
- if (_T->lock) { _unlock; } \
+ _unlock; \
} \
\
__DEFINE_GUARD_LOCK_PTR(_name, &_T->lock)
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [tip: locking/core] cleanup: Annotate guard constructors with nonnull
2026-06-02 7:12 ` [PATCH v5 3/4] cleanup: Annotate guard constructors with nonnull Dmitry Ilvokhin
[not found] ` <CANiq72=vaiGXPwmdOuTHDp30Nm62UgjtL1STJx6aj=dDPWTQYA@mail.gmail.com>
@ 2026-06-03 14:25 ` tip-bot2 for Dmitry Ilvokhin
2026-06-05 5:46 ` [PATCH v5 3/4] " Dan Carpenter
2 siblings, 0 replies; 14+ messages in thread
From: tip-bot2 for Dmitry Ilvokhin @ 2026-06-03 14:25 UTC (permalink / raw)
To: linux-tip-commits
Cc: Dmitry Ilvokhin, Peter Zijlstra (Intel), x86, linux-kernel
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 22302af28d3f7c3ca38536978c80db51d2a9e283
Gitweb: https://git.kernel.org/tip/22302af28d3f7c3ca38536978c80db51d2a9e283
Author: Dmitry Ilvokhin <d@ilvokhin.com>
AuthorDate: Tue, 02 Jun 2026 07:12:52
Committer: Peter Zijlstra <peterz@infradead.org>
CommitterDate: Wed, 03 Jun 2026 11:38:48 +02:00
cleanup: Annotate guard constructors with nonnull
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>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/85fee12eec20abfcf711443518e8f0caec982a86.1780064327.git.d@ilvokhin.com
---
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 ea95ca4..4e60d51 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 c16d419..cffe093 100644
--- a/include/linux/compiler_attributes.h
+++ b/include/linux/compiler_attributes.h
@@ -231,6 +231,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
*
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [tip: locking/core] genirq: Move NULL check into irqdesc_lock guard unlock expression
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-bot2 for Dmitry Ilvokhin
1 sibling, 0 replies; 14+ messages in thread
From: tip-bot2 for Dmitry Ilvokhin @ 2026-06-03 14:25 UTC (permalink / raw)
To: linux-tip-commits
Cc: Dmitry Ilvokhin, Peter Zijlstra (Intel), Thomas Gleixner, x86,
linux-kernel
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 08d4a7837f008ea6031c1292aa839ad881d7b3f1
Gitweb: https://git.kernel.org/tip/08d4a7837f008ea6031c1292aa839ad881d7b3f1
Author: Dmitry Ilvokhin <d@ilvokhin.com>
AuthorDate: Tue, 02 Jun 2026 07:12:51
Committer: Peter Zijlstra <peterz@infradead.org>
CommitterDate: Wed, 03 Jun 2026 11:38:47 +02:00
genirq: Move NULL check into irqdesc_lock guard unlock expression
irqdesc_lock uses __DEFINE_UNLOCK_GUARD() directly with a custom
constructor that can set .lock to NULL.
In preparation for removing the NULL check from __DEFINE_UNLOCK_GUARD(),
move the NULL check into the irqdesc_lock unlock expression, making the
NULL handling explicit at the call site.
No functional change.
Signed-off-by: Dmitry Ilvokhin <d@ilvokhin.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Thomas Gleixner <tglx@kernel.org>
Link: https://patch.msgid.link/ab457810653e4356e29b2d74ba616478bd9328ad.1780064327.git.d@ilvokhin.com
---
kernel/irq/internals.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/kernel/irq/internals.h b/kernel/irq/internals.h
index 9412e57..347cb33 100644
--- a/kernel/irq/internals.h
+++ b/kernel/irq/internals.h
@@ -171,7 +171,7 @@ void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
__DEFINE_CLASS_IS_CONDITIONAL(irqdesc_lock, true);
__DEFINE_UNLOCK_GUARD(irqdesc_lock, struct irq_desc,
- __irq_put_desc_unlock(_T->lock, _T->flags, _T->bus),
+ if (_T->lock) __irq_put_desc_unlock(_T->lock, _T->flags, _T->bus),
unsigned long flags; bool bus);
static inline class_irqdesc_lock_t class_irqdesc_lock_constructor(unsigned int irq, bool bus,
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [tip: locking/core] nvdimm: Convert nvdimm_bus guard to class
2026-06-02 7:12 ` [PATCH v5 1/4] nvdimm: Convert nvdimm_bus guard to class Dmitry Ilvokhin
@ 2026-06-03 14:25 ` tip-bot2 for Dmitry Ilvokhin
0 siblings, 0 replies; 14+ messages in thread
From: tip-bot2 for Dmitry Ilvokhin @ 2026-06-03 14:25 UTC (permalink / raw)
To: linux-tip-commits
Cc: Dmitry Ilvokhin, Peter Zijlstra (Intel), Dave Jiang, x86,
linux-kernel
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 813e5598e5b551a1fb82b516428ce2f135921122
Gitweb: https://git.kernel.org/tip/813e5598e5b551a1fb82b516428ce2f135921122
Author: Dmitry Ilvokhin <d@ilvokhin.com>
AuthorDate: Tue, 02 Jun 2026 07:12:50
Committer: Peter Zijlstra <peterz@infradead.org>
CommitterDate: Wed, 03 Jun 2026 11:38:47 +02:00
nvdimm: Convert nvdimm_bus guard to class
The nvdimm_bus guard accepts NULL and skips locking when NULL is passed.
Convert from DEFINE_GUARD() to DEFINE_CLASS() + DEFINE_CLASS_IS_GUARD().
This is a preparatory change for making DEFINE_GUARD() constructors
__nonnull_args(). nvdimm_bus legitimately passes NULL, so it must be
adjusted to avoid a compile error.
No functional change.
Signed-off-by: Dmitry Ilvokhin <d@ilvokhin.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Link: https://patch.msgid.link/8c0417904d280896ecf2e9923ffa9f20076f59b8.1780064327.git.d@ilvokhin.com
---
drivers/nvdimm/nd.h | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/drivers/nvdimm/nd.h b/drivers/nvdimm/nd.h
index b199eea..18b6455 100644
--- a/drivers/nvdimm/nd.h
+++ b/drivers/nvdimm/nd.h
@@ -632,8 +632,11 @@ u64 nd_region_interleave_set_cookie(struct nd_region *nd_region,
u64 nd_region_interleave_set_altcookie(struct nd_region *nd_region);
void nvdimm_bus_lock(struct device *dev);
void nvdimm_bus_unlock(struct device *dev);
-DEFINE_GUARD(nvdimm_bus, struct device *,
- if (_T) nvdimm_bus_lock(_T), if (_T) nvdimm_bus_unlock(_T));
+DEFINE_CLASS(nvdimm_bus, struct device *,
+ if (_T) nvdimm_bus_unlock(_T),
+ ({ if (_T) nvdimm_bus_lock(_T); _T; }),
+ struct device *_T);
+DEFINE_CLASS_IS_GUARD(nvdimm_bus);
bool is_nvdimm_bus_locked(struct device *dev);
void nvdimm_check_and_set_ro(struct gendisk *disk);
^ permalink raw reply related [flat|nested] 14+ messages in thread
* Re: [PATCH v5 3/4] cleanup: Annotate guard constructors with nonnull
2026-06-02 7:12 ` [PATCH v5 3/4] cleanup: Annotate guard constructors with nonnull Dmitry Ilvokhin
[not found] ` <CANiq72=vaiGXPwmdOuTHDp30Nm62UgjtL1STJx6aj=dDPWTQYA@mail.gmail.com>
2026-06-03 14:25 ` [tip: locking/core] " tip-bot2 for Dmitry Ilvokhin
@ 2026-06-05 5:46 ` Dan Carpenter
2026-06-05 10:46 ` Dmitry Ilvokhin
2 siblings, 1 reply; 14+ messages in thread
From: Dan Carpenter @ 2026-06-05 5:46 UTC (permalink / raw)
To: Dmitry Ilvokhin
Cc: Peter Zijlstra, Dan Williams, Vishal Verma, Dave Jiang, Ira Weiny,
Miguel Ojeda, Thomas Gleixner, Christian Brauner, Marco Elver,
H. Peter Anvin, Andrew Morton, nvdimm, linux-kernel, linux-mm,
kernel-team
On Tue, Jun 02, 2026 at 07:12:52AM +0000, Dmitry Ilvokhin wrote:
> 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.
>
Sparse doesn't like an empty __nonnull_args() at all.
./include/linux/spinlock.h:608:1: error: an expression is expected before ')'
./include/linux/spinlock.h:619:1: error: an expression is expected before ')'
./include/linux/spinlock.h:631:1: error: an expression is expected before ')'
Shouldn't we specify the arguments which are non-NULL?
__nonnull_args(1)?
regards,
dan carpenter
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v5 3/4] cleanup: Annotate guard constructors with nonnull
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
0 siblings, 1 reply; 14+ messages in thread
From: Dmitry Ilvokhin @ 2026-06-05 10:46 UTC (permalink / raw)
To: Dan Carpenter
Cc: Peter Zijlstra, Dan Williams, Vishal Verma, Dave Jiang, Ira Weiny,
Miguel Ojeda, Thomas Gleixner, Christian Brauner, Marco Elver,
H. Peter Anvin, Andrew Morton, nvdimm, linux-kernel, linux-mm,
kernel-team
On Fri, Jun 05, 2026 at 08:46:57AM +0300, Dan Carpenter wrote:
> On Tue, Jun 02, 2026 at 07:12:52AM +0000, Dmitry Ilvokhin wrote:
> > 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.
> >
>
> Sparse doesn't like an empty __nonnull_args() at all.
>
> ./include/linux/spinlock.h:608:1: error: an expression is expected before ')'
> ./include/linux/spinlock.h:619:1: error: an expression is expected before ')'
> ./include/linux/spinlock.h:631:1: error: an expression is expected before ')'
>
> Shouldn't we specify the arguments which are non-NULL?
> __nonnull_args(1)?
Thanks for the report, Dan.
That is interesting, it seems sparse trips over the empty-parens form
the guard macro expands to, nonnull(), but is fine with
__attribute__((nonnull)) without explicit arguments, that a few places
in-tree already use. GCC and clang accept both, so this probably
deserves a fix in sparse too.
Meanwhile, spelling the argument out explicitly is a simple and
reasonable fix (and arguably clearer). Patch below.
From 50f7e7eaad1d33773cee9a278625fdb4b451f53b Mon Sep 17 00:00:00 2001
From: Dmitry Ilvokhin <d@ilvokhin.com>
Date: Fri, 5 Jun 2026 03:06:22 -0700
Subject: [PATCH] cleanup: Specify nonnull argument index
The guard constructors were annotated with an empty __nonnull_args(),
relying on __nonnull__() marking every pointer parameter as non-NULL.
Sparse cannot parse the empty argument list.
Both constructors take the lock pointer as their first parameter, so
specify the index explicitly: __nonnull_args(1).
Reported-by: Dan Carpenter <error27@gmail.com>
Closes: https://lore.kernel.org/all/aiJi0WcYE8FZt-jO@stanley.mountain/
Signed-off-by: Dmitry Ilvokhin <d@ilvokhin.com>
---
include/linux/cleanup.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index 65416938e318..b1b5698cbf1b 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -397,7 +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); \
+ static __always_inline __nonnull_args(1) _type class_##_name##_constructor(_type _T); \
DEFINE_CLASS(_name, _type, _unlock, ({ _lock; _T; }), _type _T); \
DEFINE_CLASS_IS_GUARD(_name)
@@ -498,7 +498,7 @@ 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 __nonnull_args() \
+static __always_inline __nonnull_args(1) \
class_##_name##_t class_##_name##_constructor(_type *l) \
__no_context_analysis \
{ \
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [tip: locking/core] cleanup: Specify nonnull argument index
2026-06-05 10:46 ` Dmitry Ilvokhin
@ 2026-06-05 13:08 ` tip-bot2 for Dmitry Ilvokhin
0 siblings, 0 replies; 14+ messages in thread
From: tip-bot2 for Dmitry Ilvokhin @ 2026-06-05 13:08 UTC (permalink / raw)
To: linux-tip-commits
Cc: Dan Carpenter, Dmitry Ilvokhin, Peter Zijlstra (Intel), x86,
linux-kernel
The following commit has been merged into the locking/core branch of tip:
Commit-ID: a40e0f8eadd44d7b0f856b54c876aea1b93415f4
Gitweb: https://git.kernel.org/tip/a40e0f8eadd44d7b0f856b54c876aea1b93415f4
Author: Dmitry Ilvokhin <d@ilvokhin.com>
AuthorDate: Fri, 05 Jun 2026 03:06:22 -07:00
Committer: Peter Zijlstra <peterz@infradead.org>
CommitterDate: Fri, 05 Jun 2026 14:46:51 +02:00
cleanup: Specify nonnull argument index
The guard constructors were annotated with an empty __nonnull_args(),
relying on __nonnull__() marking every pointer parameter as non-NULL.
Sparse cannot parse the empty argument list.
Both constructors take the lock pointer as their first parameter, so
specify the index explicitly: __nonnull_args(1).
Reported-by: Dan Carpenter <error27@gmail.com>
Closes: https://lore.kernel.org/all/aiJi0WcYE8FZt-jO@stanley.mountain/
Signed-off-by: Dmitry Ilvokhin <d@ilvokhin.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/aiKpH3cLBEj3TF2Q@shell.ilvokhin.com
---
include/linux/cleanup.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index 6541693..b1b5698 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -397,7 +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); \
+ static __always_inline __nonnull_args(1) _type class_##_name##_constructor(_type _T); \
DEFINE_CLASS(_name, _type, _unlock, ({ _lock; _T; }), _type _T); \
DEFINE_CLASS_IS_GUARD(_name)
@@ -498,7 +498,7 @@ 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 __nonnull_args() \
+static __always_inline __nonnull_args(1) \
class_##_name##_t class_##_name##_constructor(_type *l) \
__no_context_analysis \
{ \
^ permalink raw reply related [flat|nested] 14+ messages in thread
end of thread, other threads:[~2026-06-05 13:08 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH v5 3/4] cleanup: Annotate guard constructors with nonnull Dmitry Ilvokhin
[not found] ` <CANiq72=vaiGXPwmdOuTHDp30Nm62UgjtL1STJx6aj=dDPWTQYA@mail.gmail.com>
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox