Message ID | 20240205142613.23914-2-fabio.maria.de.francesco@linux.intel.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-52816-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp958448dyb; Mon, 5 Feb 2024 07:44:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IE0rXfk9ANHmMPdr0HcoFWATGzsIuJAhuv6KCBpsaN2qkCzD0wY98wPvPoNnzElbUX+tKGu X-Received: by 2002:a17:902:cec6:b0:1d9:4ebd:b94d with SMTP id d6-20020a170902cec600b001d94ebdb94dmr17741911plg.55.1707147871695; Mon, 05 Feb 2024 07:44:31 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707147871; cv=pass; d=google.com; s=arc-20160816; b=l8i6o1aHQ7ODQToQ7I+xqIuSX6JpyuX2eWF3rb5Hol5biHgDFELFiHUO15qQPWRUiP ciTWp9F1kZA/zAKgxAfrPyNZlXIJeF8cCLGZP9wEDdTQ2tK+LlXLwpHsZtRH0k3Gzq0+ fKHCCypHOhNNGD0nPAkQyANDR3hJsfw0rrmOtU36vE1lop13HI2e0RIZWqIQc2IbSJd0 ygcqB0cBgx4kwLrJ+wyDNGjtZW9k8TVaaGCACvbEuGXl28GxWum8m3Vsg1XfCPenok6z wJrEcsgVcYmRwRrEXs5d6Exwcuvz8WbsWKNGMQ3v539fyHzP6eycbt4UPWYSedFUr4/y 5FHA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=HMm6cNKmYJ3nxz1tt+YeLlW7gliMMBQ8LXu0R9D84gU=; fh=0+vYw1hOqOWtxkUPPOUTEHfYIuzgTI5Uln5PbWGmW2w=; b=Cay95otsTfSNgYVzic/LXDJnpPP1WDGcZMdvK/u24ApbswT4iQlXUDN2qhNg/Io61L kzZ2/hbk7hcl3MCMzmihwaeSitEh5/NE/+UbXQ7m7dYy8GeQZzIhrI01t0mJY6xIPZPq mGlvfG2Icb3jhL8gA9sdktjUhygSnmSJQgY0FfBAILxed42tKa0RUNDZNj5yu0xkahxa TvCcRRpSSGXvPcC1zuK1nsvfRAJ/fYm8qVKvCp28Q+yCYplcpzwgicQQRMmwEA/EgyXa F5QTKIhH+HpM2WSFKpAwusry2/fUuwZJ7KcKBmefyuIIqfDuyBLPf4ZQtrIhL5wv4qGB Zskw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=beHC2N0g; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-52816-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52816-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=1; AJvYcCU6wHNY2sxJK81ZdAtZeEJUJDuON6iCcQj6KVAn1kyRfGBiNjcqFZuQ8TZxu4EZEvghM6sBuxpl3a4mQq4+Jy06zd6L/w== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id s18-20020a170902c65200b001d9374b44f4si3094pls.653.2024.02.05.07.44.31 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 07:44:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-52816-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=beHC2N0g; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-52816-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52816-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id DB9362849AE for <ouuuleilei@gmail.com>; Mon, 5 Feb 2024 14:27:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 526F72D042; Mon, 5 Feb 2024 14:26:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="beHC2N0g" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) (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 0DF172C1A8; Mon, 5 Feb 2024 14:26:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707143188; cv=none; b=R46QM5RqzFif66z227iOX4mIyKOCQBe/pmTvMYFbw55YBA1LBbyGg1sLM9bQCMg7VyxE5sgB3MCrhDeQstAzEAEKW4bNH//UGHRrfL6UTOzxIXQdq/roMwoQSyXpX0UbHOu6m39+T5XoRE1AJ1iIVKUPiJW+H/e0Mf4UfvQ+z+c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707143188; c=relaxed/simple; bh=um9Vx8F751cSpatd8MK+ZXL+bVntzFGdFOdjUXLJBqg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZGVIfXEliShsjCw682ahaXt0WPepX2DibaCoV0pLvpkrgt1WeOGOXTPqg9y9aXSvPRKpLVcHc1CjdZiKoVmx0AUnSuP3VIjj0E+pvL3J8G0jPgL8tJ/pcbvaaODdjo4JE8hQVf7GiaJcDn+XAXMr00+TLyx6vQs3i8+hsD8/RgE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=beHC2N0g; arc=none smtp.client-ip=192.198.163.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1707143187; x=1738679187; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=um9Vx8F751cSpatd8MK+ZXL+bVntzFGdFOdjUXLJBqg=; b=beHC2N0gV7fYZXUuf54+uLUjNrekzE6YuC+rTX+9vMDO7vN6SkkxfGS3 BlkIv0dZJWUdOGnaQuxhE1L/0IyHz+87icibP0nwKYxAG4+2usOAkc7Zt bm9hDPJvzS7p62kcU4kTRG2WjkBBQHd/Tv3JzHjMn7k+f4CHSm7DQsq1W iPYXMbTCaHRbVx+m58WtgzZrTXZDYwWyqxgakZv62YnCX05tiIkNUK7XD W8uRkydQroJ8KxKultIo2PZiehbbCXDdd3XtmDnP+U+BVU2HS2ev+8vvd ELnDiJsQbOj4du3J48YxsUPC4YO7R+sGVp1gufaSryVVO4jj+KBW0j3QS w==; X-IronPort-AV: E=McAfee;i="6600,9927,10974"; a="787668" X-IronPort-AV: E=Sophos;i="6.05,245,1701158400"; d="scan'208";a="787668" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Feb 2024 06:26:26 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,245,1701158400"; d="scan'208";a="5473011" Received: from fdefranc-mobl3.ger.corp.intel.com (HELO fdefranc-mobl3.intel.com) ([10.213.21.219]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Feb 2024 06:26:23 -0800 From: "Fabio M. De Francesco" <fabio.maria.de.francesco@linux.intel.com> To: Peter Zijlstra <peterz@infradead.org>, Dan Williams <dan.j.williams@intel.com>, linux-kernel@vger.kernel.org Cc: linux-cxl@vger.kernel.org, Ingo Molnar <mingo@kernel.org>, Jonathan Cameron <Jonathan.Cameron@huawei.com>, "Fabio M. De Francesco" <fabio.maria.de.francesco@linux.intel.com>, Ira Weiny <ira.weiny@intel.com> Subject: [PATCH 1/2 v2] cleanup: Add cond_guard() to conditional guards Date: Mon, 5 Feb 2024 15:26:12 +0100 Message-ID: <20240205142613.23914-2-fabio.maria.de.francesco@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240205142613.23914-1-fabio.maria.de.francesco@linux.intel.com> References: <20240205142613.23914-1-fabio.maria.de.francesco@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790069556526111785 X-GMAIL-MSGID: 1790074286579394433 |
Series |
Add cond_guard() to conditional guards
|
|
Commit Message
Fabio M. De Francesco
Feb. 5, 2024, 2:26 p.m. UTC
Add cond_guard() macro to conditional guards.
cond_guard() is a guard to be used with the conditional variants of locks,
like down_read_trylock() or mutex_lock_interruptible().
It takes a statement (or more statements in a block) that is passed to its
second argument. That statement (or block) is executed if waiting for a
lock is interrupted or if a _trylock() fails in case of contention.
Usage example:
cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore);
Consistently with the other guards, locks are unlocked at the exit of the
scope where cond_guard() is called.
Cc: Peter Zijlstra <peterz@infradead.org>
Suggested-by: Dan Williams <dan.j.williams@intel.com>
Suggested-by: Ira Weiny <ira.weiny@intel.com>
Signed-off-by: Fabio M. De Francesco <fabio.maria.de.francesco@linux.intel.com>
---
include/linux/cleanup.h | 14 ++++++++++++++
1 file changed, 14 insertions(+)
Comments
On 2/5/24 7:26 AM, Fabio M. De Francesco wrote: > Add cond_guard() macro to conditional guards. > > cond_guard() is a guard to be used with the conditional variants of locks, > like down_read_trylock() or mutex_lock_interruptible(). > > It takes a statement (or more statements in a block) that is passed to its > second argument. That statement (or block) is executed if waiting for a > lock is interrupted or if a _trylock() fails in case of contention. > > Usage example: > > cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); > > Consistently with the other guards, locks are unlocked at the exit of the > scope where cond_guard() is called. > > Cc: Peter Zijlstra <peterz@infradead.org> > Suggested-by: Dan Williams <dan.j.williams@intel.com> > Suggested-by: Ira Weiny <ira.weiny@intel.com> > Signed-off-by: Fabio M. De Francesco <fabio.maria.de.francesco@linux.intel.com> Reviewed-by: Dave Jiang <dave.jiang@intel.com> > --- > include/linux/cleanup.h | 14 ++++++++++++++ > 1 file changed, 14 insertions(+) > > diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h > index c2d09bc4f976..88af56600325 100644 > --- a/include/linux/cleanup.h > +++ b/include/linux/cleanup.h > @@ -134,6 +134,16 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > * an anonymous instance of the (guard) class, not recommended for > * conditional locks. > * > + * cond_guard(name, fail, args...): > + * a guard to be used with the conditional variants of locks, like > + * down_read_trylock() or mutex_lock_interruptible. 'fail' are one or more > + * statements that are executed when waiting for a lock is interrupted or > + * when a _trylock() fails in case of contention. > + * > + * Example: > + * > + * cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); > + * > * scoped_guard (name, args...) { }: > * similar to CLASS(name, scope)(args), except the variable (with the > * explicit name 'scope') is declard in a for-loop such that its scope is > @@ -165,6 +175,10 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > > #define __guard_ptr(_name) class_##_name##_lock_ptr > > +#define cond_guard(_name, _fail, args...) \ > + CLASS(_name, scope)(args); \ > + if (!__guard_ptr(_name)(&scope)) _fail > + > #define scoped_guard(_name, args...) \ > for (CLASS(_name, scope)(args), \ > *done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1)
Fabio M. De Francesco wrote: > Add cond_guard() macro to conditional guards. > > cond_guard() is a guard to be used with the conditional variants of locks, > like down_read_trylock() or mutex_lock_interruptible(). > > It takes a statement (or more statements in a block) that is passed to its > second argument. That statement (or block) is executed if waiting for a > lock is interrupted or if a _trylock() fails in case of contention. > > Usage example: > > cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); > > Consistently with the other guards, locks are unlocked at the exit of the > scope where cond_guard() is called. > > Cc: Peter Zijlstra <peterz@infradead.org> > Suggested-by: Dan Williams <dan.j.williams@intel.com> > Suggested-by: Ira Weiny <ira.weiny@intel.com> > Signed-off-by: Fabio M. De Francesco <fabio.maria.de.francesco@linux.intel.com> > --- > include/linux/cleanup.h | 14 ++++++++++++++ > 1 file changed, 14 insertions(+) > > diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h > index c2d09bc4f976..88af56600325 100644 > --- a/include/linux/cleanup.h > +++ b/include/linux/cleanup.h > @@ -134,6 +134,16 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > * an anonymous instance of the (guard) class, not recommended for > * conditional locks. > * > + * cond_guard(name, fail, args...): > + * a guard to be used with the conditional variants of locks, like > + * down_read_trylock() or mutex_lock_interruptible. 'fail' are one or more > + * statements that are executed when waiting for a lock is interrupted or > + * when a _trylock() fails in case of contention. > + * > + * Example: > + * > + * cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); That _fail argument likely needs to be a statement expression for the multi-statement case. > + * > * scoped_guard (name, args...) { }: > * similar to CLASS(name, scope)(args), except the variable (with the > * explicit name 'scope') is declard in a for-loop such that its scope is > @@ -165,6 +175,10 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > > #define __guard_ptr(_name) class_##_name##_lock_ptr > > +#define cond_guard(_name, _fail, args...) \ > + CLASS(_name, scope)(args); \ > + if (!__guard_ptr(_name)(&scope)) _fail No, as I stated before this is broken for usages of: if () cond_guard() else if () The 'else' in the definition is critical, this builds for me (untested): diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index 88af56600325..665407498781 100644 --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -142,7 +142,7 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ * * Example: * - * cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); + * cond_guard(rwsem_read_try, ({ printk(...); return 0; }), &semaphore); * * scoped_guard (name, args...) { }: * similar to CLASS(name, scope)(args), except the variable (with the @@ -177,7 +177,8 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ #define cond_guard(_name, _fail, args...) \ CLASS(_name, scope)(args); \ - if (!__guard_ptr(_name)(&scope)) _fail + if (!__guard_ptr(_name)(&scope)) _fail; \ + else #define scoped_guard(_name, args...) \ for (CLASS(_name, scope)(args), \
Dan Williams wrote: > Fabio M. De Francesco wrote: > > Add cond_guard() macro to conditional guards. > > > > cond_guard() is a guard to be used with the conditional variants of locks, > > like down_read_trylock() or mutex_lock_interruptible(). > > > > It takes a statement (or more statements in a block) that is passed to its > > second argument. That statement (or block) is executed if waiting for a > > lock is interrupted or if a _trylock() fails in case of contention. > > > > Usage example: > > > > cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); > > > > Consistently with the other guards, locks are unlocked at the exit of the > > scope where cond_guard() is called. > > > > Cc: Peter Zijlstra <peterz@infradead.org> > > Suggested-by: Dan Williams <dan.j.williams@intel.com> > > Suggested-by: Ira Weiny <ira.weiny@intel.com> > > Signed-off-by: Fabio M. De Francesco <fabio.maria.de.francesco@linux.intel.com> > > --- > > include/linux/cleanup.h | 14 ++++++++++++++ > > 1 file changed, 14 insertions(+) > > > > diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h > > index c2d09bc4f976..88af56600325 100644 > > --- a/include/linux/cleanup.h > > +++ b/include/linux/cleanup.h > > @@ -134,6 +134,16 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > > * an anonymous instance of the (guard) class, not recommended for > > * conditional locks. > > * > > + * cond_guard(name, fail, args...): > > + * a guard to be used with the conditional variants of locks, like > > + * down_read_trylock() or mutex_lock_interruptible. 'fail' are one or more > > + * statements that are executed when waiting for a lock is interrupted or > > + * when a _trylock() fails in case of contention. > > + * > > + * Example: > > + * > > + * cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); > > That _fail argument likely needs to be a statement expression for the > multi-statement case. You mean ({ ... }) as discussed here? https://lore.kernel.org/all/65c1578c76def_37447929456@iweiny-mobl.notmuch/ > > > + * > > * scoped_guard (name, args...) { }: > > * similar to CLASS(name, scope)(args), except the variable (with the > > * explicit name 'scope') is declard in a for-loop such that its scope is > > @@ -165,6 +175,10 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > > > > #define __guard_ptr(_name) class_##_name##_lock_ptr > > > > +#define cond_guard(_name, _fail, args...) \ > > + CLASS(_name, scope)(args); \ > > + if (!__guard_ptr(_name)(&scope)) _fail > > No, as I stated before this is broken for usages of: > > if () cond_guard() else if () > > The 'else' in the definition is critical, this builds for me (untested): I did not test Fabios work directly but I don't understand this example. It seems like your suggestion does nothing useful. The cond_guard() becomes a single statement like... if () cond_guard(); else ... .. And can't protect anything. NOTE From my understanding of cond_guard() as defined, the ';' must be used as part of cond_guard() and should complete the internal macro 'if' statement. I think this would work: if () { cond_guard(); ... do locked stuff ... } else ... > > diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h > index 88af56600325..665407498781 100644 > --- a/include/linux/cleanup.h > +++ b/include/linux/cleanup.h > @@ -142,7 +142,7 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > * > * Example: > * > - * cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); > + * cond_guard(rwsem_read_try, ({ printk(...); return 0; }), &semaphore); > * > * scoped_guard (name, args...) { }: > * similar to CLASS(name, scope)(args), except the variable (with the > @@ -177,7 +177,8 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > > #define cond_guard(_name, _fail, args...) \ > CLASS(_name, scope)(args); \ > - if (!__guard_ptr(_name)(&scope)) _fail > + if (!__guard_ptr(_name)(&scope)) _fail; \ Building on what I found for scoped_cond_guard() this should be > + if (!__guard_ptr(_name)(&scope)) { _fail; } And drop the else. The else needs to clearly be part of an outside if in your example. Ira > + else > > #define scoped_guard(_name, args...) \ > for (CLASS(_name, scope)(args), \
Ira Weiny wrote: > Dan Williams wrote: > > Fabio M. De Francesco wrote: > > > Add cond_guard() macro to conditional guards. > > > > > > cond_guard() is a guard to be used with the conditional variants of locks, > > > like down_read_trylock() or mutex_lock_interruptible(). > > > > > > It takes a statement (or more statements in a block) that is passed to its > > > second argument. That statement (or block) is executed if waiting for a > > > lock is interrupted or if a _trylock() fails in case of contention. > > > > > > Usage example: > > > > > > cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); > > > > > > Consistently with the other guards, locks are unlocked at the exit of the > > > scope where cond_guard() is called. > > > > > > Cc: Peter Zijlstra <peterz@infradead.org> > > > Suggested-by: Dan Williams <dan.j.williams@intel.com> > > > Suggested-by: Ira Weiny <ira.weiny@intel.com> > > > Signed-off-by: Fabio M. De Francesco <fabio.maria.de.francesco@linux.intel.com> > > > --- > > > include/linux/cleanup.h | 14 ++++++++++++++ > > > 1 file changed, 14 insertions(+) > > > > > > diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h > > > index c2d09bc4f976..88af56600325 100644 > > > --- a/include/linux/cleanup.h > > > +++ b/include/linux/cleanup.h > > > @@ -134,6 +134,16 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > > > * an anonymous instance of the (guard) class, not recommended for > > > * conditional locks. > > > * > > > + * cond_guard(name, fail, args...): > > > + * a guard to be used with the conditional variants of locks, like > > > + * down_read_trylock() or mutex_lock_interruptible. 'fail' are one or more > > > + * statements that are executed when waiting for a lock is interrupted or > > > + * when a _trylock() fails in case of contention. > > > + * > > > + * Example: > > > + * > > > + * cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); > > > > That _fail argument likely needs to be a statement expression for the > > multi-statement case. > > You mean ({ ... }) as discussed here? > > https://lore.kernel.org/all/65c1578c76def_37447929456@iweiny-mobl.notmuch/ Yes. > > > + * > > > * scoped_guard (name, args...) { }: > > > * similar to CLASS(name, scope)(args), except the variable (with the > > > * explicit name 'scope') is declard in a for-loop such that its scope is > > > @@ -165,6 +175,10 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > > > > > > #define __guard_ptr(_name) class_##_name##_lock_ptr > > > > > > +#define cond_guard(_name, _fail, args...) \ > > > + CLASS(_name, scope)(args); \ > > > + if (!__guard_ptr(_name)(&scope)) _fail > > > > No, as I stated before this is broken for usages of: > > > > if () cond_guard() else if () > > > > The 'else' in the definition is critical, this builds for me (untested): > > I did not test Fabios work directly but I don't understand this example. > It seems like your suggestion does nothing useful. The cond_guard() > becomes a single statement like... > > if () > cond_guard(); > else ... > > ... And can't protect anything. A sequence to acquire and drop a lock is sometimes a barrier semantic. Is it typical, no, is it possible, yes. I otherwise do not understand the need to include the subtle side effect. > cond_guard() as defined, the ';' must be used as part of cond_guard() and > should complete the internal macro 'if' statement. > > I think this would work: > > if () { > cond_guard(); > ... do locked stuff ... > } else ... > > > > > diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h > > index 88af56600325..665407498781 100644 > > --- a/include/linux/cleanup.h > > +++ b/include/linux/cleanup.h > > @@ -142,7 +142,7 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > > * > > * Example: > > * > > - * cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); > > + * cond_guard(rwsem_read_try, ({ printk(...); return 0; }), &semaphore); > > * > > * scoped_guard (name, args...) { }: > > * similar to CLASS(name, scope)(args), except the variable (with the > > @@ -177,7 +177,8 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > > > > #define cond_guard(_name, _fail, args...) \ > > CLASS(_name, scope)(args); \ > > - if (!__guard_ptr(_name)(&scope)) _fail > > + if (!__guard_ptr(_name)(&scope)) _fail; \ > > Building on what I found for scoped_cond_guard() this should be > > > + if (!__guard_ptr(_name)(&scope)) { _fail; } That's still a dangling if () statement. > > And drop the else. The else needs to clearly be part of an outside if in > your example. Please just rely on a statement-expression for the odd multi-statement _fail use case and include the else in the definition to remove any room for confusion.
Dan Williams wrote: > Ira Weiny wrote: > > Dan Williams wrote: > > > Fabio M. De Francesco wrote: > > > > Add cond_guard() macro to conditional guards. > > > > > > > > cond_guard() is a guard to be used with the conditional variants of locks, > > > > like down_read_trylock() or mutex_lock_interruptible(). > > > > > > > > It takes a statement (or more statements in a block) that is passed to its > > > > second argument. That statement (or block) is executed if waiting for a > > > > lock is interrupted or if a _trylock() fails in case of contention. > > > > > > > > Usage example: > > > > > > > > cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); > > > > > > > > Consistently with the other guards, locks are unlocked at the exit of the > > > > scope where cond_guard() is called. > > > > > > > > Cc: Peter Zijlstra <peterz@infradead.org> > > > > Suggested-by: Dan Williams <dan.j.williams@intel.com> > > > > Suggested-by: Ira Weiny <ira.weiny@intel.com> > > > > Signed-off-by: Fabio M. De Francesco <fabio.maria.de.francesco@linux.intel.com> > > > > --- > > > > include/linux/cleanup.h | 14 ++++++++++++++ > > > > 1 file changed, 14 insertions(+) > > > > > > > > diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h > > > > index c2d09bc4f976..88af56600325 100644 > > > > --- a/include/linux/cleanup.h > > > > +++ b/include/linux/cleanup.h > > > > @@ -134,6 +134,16 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > > > > * an anonymous instance of the (guard) class, not recommended for > > > > * conditional locks. > > > > * > > > > + * cond_guard(name, fail, args...): > > > > + * a guard to be used with the conditional variants of locks, like > > > > + * down_read_trylock() or mutex_lock_interruptible. 'fail' are one or more > > > > + * statements that are executed when waiting for a lock is interrupted or > > > > + * when a _trylock() fails in case of contention. > > > > + * > > > > + * Example: > > > > + * > > > > + * cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); > > > > > > That _fail argument likely needs to be a statement expression for the > > > multi-statement case. > > > > You mean ({ ... }) as discussed here? > > > > https://lore.kernel.org/all/65c1578c76def_37447929456@iweiny-mobl.notmuch/ > > Yes. > > > > > + * > > > > * scoped_guard (name, args...) { }: > > > > * similar to CLASS(name, scope)(args), except the variable (with the > > > > * explicit name 'scope') is declard in a for-loop such that its scope is > > > > @@ -165,6 +175,10 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > > > > > > > > #define __guard_ptr(_name) class_##_name##_lock_ptr > > > > > > > > +#define cond_guard(_name, _fail, args...) \ > > > > + CLASS(_name, scope)(args); \ > > > > + if (!__guard_ptr(_name)(&scope)) _fail > > > > > > No, as I stated before this is broken for usages of: > > > > > > if () cond_guard() else if () > > > > > > The 'else' in the definition is critical, this builds for me (untested): > > > > I did not test Fabios work directly but I don't understand this example. > > It seems like your suggestion does nothing useful. The cond_guard() > > becomes a single statement like... > > > > if () > > cond_guard(); > > else ... > > > > ... And can't protect anything. > > A sequence to acquire and drop a lock is sometimes a barrier semantic. > Is it typical, no, is it possible, yes. I otherwise do not understand > the need to include the subtle side effect. I was not trying to include a subtle side effect. I was thinking that the else block would be the only block covered by the lock. I've looked at the preprocessor output again and I now see what you are saying. Also I see I was thinking incorrectly. The else will be an empty statement and the rest of the outer block will be covered by the lock... Sorry for not seeing this before. > > cond_guard() as defined, the ';' must be used as part of cond_guard() and > > should complete the internal macro 'if' statement. > > > > I think this would work: > > > > if () { > > cond_guard(); > > ... do locked stuff ... > > } else ... > > > > > > > > diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h > > > index 88af56600325..665407498781 100644 > > > --- a/include/linux/cleanup.h > > > +++ b/include/linux/cleanup.h > > > @@ -142,7 +142,7 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > > > * > > > * Example: > > > * > > > - * cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); > > > + * cond_guard(rwsem_read_try, ({ printk(...); return 0; }), &semaphore); > > > * > > > * scoped_guard (name, args...) { }: > > > * similar to CLASS(name, scope)(args), except the variable (with the > > > @@ -177,7 +177,8 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ > > > > > > #define cond_guard(_name, _fail, args...) \ > > > CLASS(_name, scope)(args); \ > > > - if (!__guard_ptr(_name)(&scope)) _fail > > > + if (!__guard_ptr(_name)(&scope)) _fail; \ > > > > Building on what I found for scoped_cond_guard() this should be > > > > > + if (!__guard_ptr(_name)(&scope)) { _fail; } > > That's still a dangling if () statement. > > > > > And drop the else. The else needs to clearly be part of an outside if in > > your example. > > Please just rely on a statement-expression for the odd multi-statement _fail > use case and include the else in the definition to remove any room for > confusion. Yea ok I see it now, Ira
diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index c2d09bc4f976..88af56600325 100644 --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -134,6 +134,16 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ * an anonymous instance of the (guard) class, not recommended for * conditional locks. * + * cond_guard(name, fail, args...): + * a guard to be used with the conditional variants of locks, like + * down_read_trylock() or mutex_lock_interruptible. 'fail' are one or more + * statements that are executed when waiting for a lock is interrupted or + * when a _trylock() fails in case of contention. + * + * Example: + * + * cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore); + * * scoped_guard (name, args...) { }: * similar to CLASS(name, scope)(args), except the variable (with the * explicit name 'scope') is declard in a for-loop such that its scope is @@ -165,6 +175,10 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ #define __guard_ptr(_name) class_##_name##_lock_ptr +#define cond_guard(_name, _fail, args...) \ + CLASS(_name, scope)(args); \ + if (!__guard_ptr(_name)(&scope)) _fail + #define scoped_guard(_name, args...) \ for (CLASS(_name, scope)(args), \ *done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1)