mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "tip-bot2 for Dmitry Ilvokhin" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: Dmitry Ilvokhin <d@ilvokhin.com>,
	"Peter Zijlstra (Intel)" <peterz@infradead.org>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: locking/core] cleanup: Remove NULL check from unconditional guards
Date: Wed, 03 Jun 2026 14:25:07 -0000	[thread overview]
Message-ID: <178049670778.710.17702813837987687303.tip-bot2@tip-bot2> (raw)
In-Reply-To: <0503a089389b2270c478a873e095cf0a4ff26d24.1780064327.git.d@ilvokhin.com>

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)

      reply	other threads:[~2026-06-03 14:25 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-02  7:12 [PATCH v5 0/4] " 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
2026-06-02  7:32   ` 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-bot2 for Dmitry Ilvokhin [this message]

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=178049670778.710.17702813837987687303.tip-bot2@tip-bot2 \
    --to=tip-bot2@linutronix.de \
    --cc=d@ilvokhin.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=x86@kernel.org \
    /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®