From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ilvokhin.com (mail.ilvokhin.com [178.62.254.231]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 261BA384CFA for ; Tue, 12 May 2026 07:15:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.62.254.231 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778570142; cv=none; b=J3qoUBStqS3KChaFepoGGQzq2Vt4l4zN0BwphGeXPZa8mQjjvkyu8Bx09+OsrEiG0C4EqqZHibHVKvZvmUFxrMxyvvQ+dEHfcMi4T3unSocpUL/o9Ih7ngiSO6r/lDVOqg/42SUzTFlBhceVGpMkc1jWgBDs5gUaDgl0GM4zdpM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778570142; c=relaxed/simple; bh=nIyLZ9c2UEcQH7nHkP4dXz1wJJpK1owoWr1+QajafEo=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=pHcTruqiVkRPKB9A3XRKfeuXTZtkWyE7HYXlf7k3O/AyjhjQVa983IO+YKXnx8UHS7hhPZ2DlMtH1vkXJpg1utP5jdpQsx7m/z/Fx1eaZeui0MGIlkCKy4ticDFruV6rqEcI3FplhbrQ3F0i0Ys8Dj9uaKkf9/uOv8O6y77n18Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=ilvokhin.com; spf=pass smtp.mailfrom=ilvokhin.com; dkim=pass (1024-bit key) header.d=ilvokhin.com header.i=@ilvokhin.com header.b=BZY3XNC+; arc=none smtp.client-ip=178.62.254.231 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=ilvokhin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ilvokhin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ilvokhin.com header.i=@ilvokhin.com header.b="BZY3XNC+" Received: from localhost.localdomain (shell.ilvokhin.com [138.68.190.75]) (Authenticated sender: d@ilvokhin.com) by mail.ilvokhin.com (Postfix) with ESMTPSA id 9E93DD03F1; Tue, 12 May 2026 07:15:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ilvokhin.com; s=mail; t=1778570130; bh=BFor0KrBkxvS9w0H6iEW/kE3ciN1+oSXsC1h7srZquA=; h=From:To:Cc:Subject:Date; b=BZY3XNC+rtFbvH+ITtrgxA6DrZeIf4ChmwIoKFc78kqEg7hSrMs25mrS9VOyhTP/N YVgDeEkO0Rib71w4X0DLdGHCTE0POQWEYFHb3/ZiXRfmlAgMt3W8gR2MqTM6wjaa6o D55j8H1OwaL7YGNsr7uxzZ57bS6Wl1ShaMQ5OHc4= From: Dmitry Ilvokhin To: Peter Zijlstra , Christian Brauner , Dan Williams , Dave Jiang , Marco Elver , "H. Peter Anvin" , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, Dmitry Ilvokhin Subject: [PATCH v2] cleanup: Remove NULL check from unconditional guards Date: Tue, 12 May 2026 07:15:10 +0000 Message-ID: <20260512071510.92451-1-d@ilvokhin.com> X-Mailer: git-send-email 2.54.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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() and DEFINE_LOCK_GUARD_1() both run the lock operation in the constructor before returning. 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. 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 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=23833993, chg -0.23% Signed-off-by: Dmitry Ilvokhin --- Changes in v2: - Expand commit message with detailed reasoning, why the proposed change is correct. - Rebase on top of locking/core. 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/ 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 ea95ca4bc11c..1410effa8780 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) \ - 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) \ @@ -491,7 +491,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