From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (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 617AD3911B1 for ; Tue, 12 May 2026 12:46:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778589971; cv=none; b=iWoZgM8V277NOUDROIzGJKwmT8XTyppo1rYhHrE7KrJgZSdg7WUIU463TfDaqVF4qlujdfYF0beel1G6BfedKh6gr6gsU8cYwwA4z8QDepXzaOvyPNkavkF8menCFAZuUKw6ZVfXyJi0dNUgQ0mhrizcRXo83o/Zm4zxnLvQ2SQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778589971; c=relaxed/simple; bh=42MmlD1ueEAx4Zj/A3jmAoaCuHkzcgMFBzCkIYA1nF0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=tV/mEoRRPKcM8qK+CKXLlHiBkxyqDkIueuqBOCmNwRvadQjk5rOWcj59VvDZQ9VrFbKoD1xc8FuZHZJcIY0waH2D1z0gDS24zopv4cmQrszmuVZMCOyyAotT9L+F0sicEyuU/1PeY9S+VNZrMNQToXv3h2E4xZGvbh3lMrIQzgk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=cnPiN3oT; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="cnPiN3oT" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=jxNlaTzC5bFl5oe0izqxrB7/VUScvvXxN9sPBAhg49w=; b=cnPiN3oT4SiReKTwxUbP2nS9sJ wEfrtZz2+QgfmVLNt+XIdUGz2WIIVxqy4fj1C/9XSsEu3IbnLRth2BC19QJmkPZkN3PdwYix+HtPD bIE9mTH4q6C/8nXSjZ3VwIBfjSHG7ai6F+z215UZlFaAwG/wxcYSxII+FPMpPffAFV42nDbLDHN63 HzCal3iQTqamwzJzS6SwbBZ5X3QSft9NIZ7+aXo6l/BfilepdllJ68FRKhtTN3R6N+JZPLiNJMmyH iVe+JMhoRZkg12IUEab+irI18eysRjIIDLQb2mAOMXXsDN+wYiVOsycE1vGlVIfIqDx8iDj3aagRq dYT5HATQ==; Received: from 2001-1c00-8d85-4b00-266e-96ff-fe07-7dcc.cable.dynamic.v6.ziggo.nl ([2001:1c00:8d85:4b00:266e:96ff:fe07:7dcc] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.99.1 #2 (Red Hat Linux)) id 1wMmUo-00000009kMX-1yVD; Tue, 12 May 2026 12:45:59 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id 0448C30119C; Tue, 12 May 2026 14:45:58 +0200 (CEST) Date: Tue, 12 May 2026 14:45:57 +0200 From: Peter Zijlstra To: Dmitry Ilvokhin Cc: Christian Brauner , Dan Williams , Dave Jiang , Marco Elver , "H. Peter Anvin" , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: Re: [PATCH v2] cleanup: Remove NULL check from unconditional guards Message-ID: <20260512124557.GD1889694@noisy.programming.kicks-ass.net> References: <20260512071510.92451-1-d@ilvokhin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260512071510.92451-1-d@ilvokhin.com> On Tue, May 12, 2026 at 07:15:10AM +0000, Dmitry Ilvokhin wrote: > 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) What about class_irqdesc_lock_constructor() ? AFAICT __irq_get_desc_lock() can return NULL.