mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "tip-bot2 for David Lechner" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: David Lechner <dlechner@baylibre.com>,
	"Fabio M. De Francesco" <fabio.m.de.francesco@linux.intel.com>,
	"Peter Zijlstra (Intel)" <peterz@infradead.org>,
	Dan Williams <dan.j.williams@intel.com>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: locking/core] cleanup: Add conditional guard helper
Date: Sat, 26 Oct 2024 07:35:26 -0000	[thread overview]
Message-ID: <172992812675.1442.16190431929459104566.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20241001-cleanup-if_not_cond_guard-v1-1-7753810b0f7a@baylibre.com>

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     36c2cf88808d47e926d11b98734f154fe4a9f50f
Gitweb:        https://git.kernel.org/tip/36c2cf88808d47e926d11b98734f154fe4a9f50f
Author:        David Lechner <dlechner@baylibre.com>
AuthorDate:    Tue, 01 Oct 2024 17:30:18 -05:00
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Fri, 25 Oct 2024 10:01:51 +02:00

cleanup: Add conditional guard helper

Add a new if_not_guard() macro to cleanup.h for handling
conditional guards such as mutext_trylock().

This is more ergonomic than scoped_guard() for most use cases.
Instead of hiding the error handling statement in the macro args, it
works like a normal if statement and allow the error path to be indented
while the normal code flow path is not indented. And it avoid unwanted
side-effect from hidden for loop in scoped_guard().

Signed-off-by: David Lechner <dlechner@baylibre.com>
Co-developed-by: Fabio M. De Francesco <fabio.m.de.francesco@linux.intel.com>
Signed-off-by: Fabio M. De Francesco <fabio.m.de.francesco@linux.intel.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
Link: https://lkml.kernel.org/r/20241001-cleanup-if_not_cond_guard-v1-1-7753810b0f7a@baylibre.com
---
 include/linux/cleanup.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index 0cc66f8..e859f79 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -273,6 +273,12 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
  *	an anonymous instance of the (guard) class, not recommended for
  *	conditional locks.
  *
+ * if_not_guard(name, args...) { <error handling> }:
+ *	convenience macro for conditional guards that calls the statement that
+ *	follows only if the lock was not acquired (typically an error return).
+ *
+ *	Only for conditional locks.
+ *
  * 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
@@ -343,6 +349,15 @@ _label:									\
 
 #define scoped_cond_guard(_name, _fail, args...)	\
 	__scoped_cond_guard(_name, _fail, __UNIQUE_ID(label), args)
+
+#define __if_not_guard(_name, _id, args...)		\
+	BUILD_BUG_ON(!__is_cond_ptr(_name));		\
+	CLASS(_name, _id)(args);			\
+	if (!__guard_ptr(_name)(&_id))
+
+#define if_not_guard(_name, args...) \
+	__if_not_guard(_name, __UNIQUE_ID(guard), args)
+
 /*
  * Additional helper macros for generating lock guards with types, either for
  * locks that don't have a native type (eg. RCU, preempt) or those that need a

  parent reply	other threads:[~2024-10-26  7:35 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-01 22:30 [PATCH 0/3] cleanup: add if_not_cond_guard macro David Lechner
2024-10-01 22:30 ` [PATCH 1/3] cleanup: add conditional guard helper David Lechner
2024-10-04 17:34   ` Dan Williams
2024-10-04 20:27     ` David Lechner
2024-10-18 11:15   ` Peter Zijlstra
2024-10-18 12:31     ` Przemek Kitszel
2024-10-18 16:29       ` Jonathan Cameron
2024-10-18 19:29     ` Dan Williams
2024-10-23 10:57       ` Peter Zijlstra
2024-10-26  7:35   ` tip-bot2 for David Lechner [this message]
2024-10-01 22:30 ` [PATCH 2/3] iio: adc: ad7380: use if_not_cond_guard for claim direct David Lechner
2024-10-03  4:23   ` kernel test robot
2024-10-03  5:35   ` kernel test robot
2024-10-03 14:20     ` David Lechner
2024-10-01 22:30 ` [PATCH 3/3] cxl/region: Use cond_guard() in show_targetN() David Lechner
2024-10-02  2:13 ` [PATCH 0/3] cleanup: add if_not_cond_guard macro Dan Williams
2024-10-06 11:35   ` Jonathan Cameron

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=172992812675.1442.16190431929459104566.tip-bot2@tip-bot2 \
    --to=tip-bot2@linutronix.de \
    --cc=dan.j.williams@intel.com \
    --cc=dlechner@baylibre.com \
    --cc=fabio.m.de.francesco@linux.intel.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®