mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Marc Zyngier <marc.zyngier@arm.com>
To: linux-kernel@vger.kernel.org
Cc: Jason Baron <jbaron@akamai.com>, Jonathan Corbet <corbet@lwn.net>,
	Peter Zijlstra <peterz@infradead.org>,
	Christoffer Dall <christoffer.dall@linaro.org>,
	Vladimir Murzin <vladimir.murzin@arm.com>,
	Catalin Marinas <catalin.marinas@arm.com>
Subject: [PATCH 1/2] jump_labels: Add API to deal with keys embedded in structures
Date: Mon, 19 Sep 2016 18:21:27 +0100	[thread overview]
Message-ID: <1474305688-22553-2-git-send-email-marc.zyngier@arm.com> (raw)
In-Reply-To: <1474305688-22553-1-git-send-email-marc.zyngier@arm.com>

It is desirable to allow static keys to be integrated in structures,
as it can lead do slightly more readable code. But the current API
only provides DEFINE_STATIC_KEY_TRUE/FALSE, which is not exactly
nice and leads to the following idiom:

	static struct {
		int			foo;
		struct static_key_false	key;
	} bar = {
		.key	= STATIC_KEY_FALSE_INIT,
	};

	[...]

	if (static_branch_unlikely(&bar.key))
		foo = -1;

which doesn't follow the recommended API, and uses the internals
of the static key implementation.

This patch introduces DECLARE_STATIC_KEY_TRUE/FALSE, as well as
INIT_STATIC_KEY_TRUE/FALSE, which abstract such construct and
allow the internals to evolve without having to fix everything else:

	static struct {
		int			 foo;
		DECLARE_STATIC_KEY_FALSE(key);
	} bar = {
		INIT_STATIC_KEY_FALSE(.key),
	};

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
---
 Documentation/static-keys.txt | 19 +++++++++++++++++++
 include/linux/jump_label.h    | 21 ++++++++++++++++++---
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/Documentation/static-keys.txt b/Documentation/static-keys.txt
index ea8d7b4..a2fedb2 100644
--- a/Documentation/static-keys.txt
+++ b/Documentation/static-keys.txt
@@ -15,6 +15,10 @@ The updated API replacements are:
 
 DEFINE_STATIC_KEY_TRUE(key);
 DEFINE_STATIC_KEY_FALSE(key);
+DECLARE_STATIC_KEY_TRUE(key);
+DECLARE_STATIC_KEY_FALSE(key);
+INIT_STATIC_KEY_TRUE(key);
+INIT_STATIC_KEY_FALSE(key);
 DEFINE_STATIC_KEY_ARRAY_TRUE(keys, count);
 DEFINE_STATIC_KEY_ARRAY_FALSE(keys, count);
 static_branch_likely()
@@ -142,6 +146,21 @@ static_branch_inc(), will change the branch back to true. Likewise, if the
 key is initialized false, a 'static_branch_inc()', will change the branch to
 true. And then a 'static_branch_dec()', will again make the branch false.
 
+Should the key be declared in a structure and required to be
+initialized when such structure is defined, the following construct
+can be used:
+
+    	struct foo {
+    		DECLARE_STATIC_KEY_FALSE(key);
+    	};
+
+	static struct foo bar = {
+    		INIT_STATIC_KEY_FALSE(.key),
+    	};
+
+(respectively DECLARE_STATIC_KEY_TRUE/INIT_STATIC_KEY_TRUE for the
+opposite case).
+
 Where an array of keys is required, it can be defined as:
 
 	DEFINE_STATIC_KEY_ARRAY_TRUE(keys, count);
diff --git a/include/linux/jump_label.h b/include/linux/jump_label.h
index a534c7f..10ee414 100644
--- a/include/linux/jump_label.h
+++ b/include/linux/jump_label.h
@@ -21,6 +21,10 @@
  *
  * DEFINE_STATIC_KEY_TRUE(key);
  * DEFINE_STATIC_KEY_FALSE(key);
+ * DECLARE_STATIC_KEY_TRUE(key);
+ * DECLARE_STATIC_KEY_FALSE(key);
+ * INIT_STATIC_KEY_TRUE(key);
+ * INIT_STATIC_KEY_FALSE(key);
  * DEFINE_STATIC_KEY_ARRAY_TRUE(keys, count);
  * DEFINE_STATIC_KEY_ARRAY_FALSE(keys, count);
  * static_branch_likely()
@@ -36,7 +40,10 @@
  * "if (static_branch_unlikely(&key))", in which case we will generate an
  * unconditional branch to the out-of-line true branch. Keys that are
  * initially true or false can be using in both static_branch_unlikely()
- * and static_branch_likely() statements.
+ * and static_branch_likely() statements. DECLARE_STATIC_KEY_TRUE/FALSE
+ * can be used to declare a key that will be defined somewhere else.
+ * INIT_STATIC_KEY_TRUE/FALSE initialize a static key that can be declared
+ * in another structure.
  *
  * At runtime we can change the branch target by setting the key
  * to true via a call to static_branch_enable(), or false using
@@ -266,11 +273,19 @@ struct static_key_false {
 #define STATIC_KEY_TRUE_INIT  (struct static_key_true) { .key = STATIC_KEY_INIT_TRUE,  }
 #define STATIC_KEY_FALSE_INIT (struct static_key_false){ .key = STATIC_KEY_INIT_FALSE, }
 
+#define DECLARE_STATIC_KEY_TRUE(name)	struct static_key_true name
+
+#define DECLARE_STATIC_KEY_FALSE(name)	struct static_key_false name
+
+#define INIT_STATIC_KEY_TRUE(name)	name = STATIC_KEY_TRUE_INIT
+
+#define INIT_STATIC_KEY_FALSE(name)	name = STATIC_KEY_FALSE_INIT
+
 #define DEFINE_STATIC_KEY_TRUE(name)	\
-	struct static_key_true name = STATIC_KEY_TRUE_INIT
+	DECLARE_STATIC_KEY_TRUE(name) = STATIC_KEY_TRUE_INIT
 
 #define DEFINE_STATIC_KEY_FALSE(name)	\
-	struct static_key_false name = STATIC_KEY_FALSE_INIT
+	DECLARE_STATIC_KEY_FALSE(name) = STATIC_KEY_FALSE_INIT
 
 #define DEFINE_STATIC_KEY_ARRAY_TRUE(name, count)		\
 	struct static_key_true name[count] = {			\
-- 
2.1.4

  reply	other threads:[~2016-09-19 17:21 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-19 17:21 [PATCH 0/2] jump_labels: Embedding static keys inside structures Marc Zyngier
2016-09-19 17:21 ` Marc Zyngier [this message]
2016-09-20  9:42   ` [PATCH 1/2] jump_labels: Add API to deal with keys embedded in structures Peter Zijlstra
2016-09-20  9:46     ` Marc Zyngier
2016-09-20 12:25     ` Christoffer Dall
2016-09-20 12:42       ` Peter Zijlstra
2016-09-20 12:54         ` Christoffer Dall
2016-09-22  7:47           ` Peter Zijlstra
2016-09-19 17:21 ` [PATCH 2/2] dynamic_debug: Use updated jump label API Marc Zyngier

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=1474305688-22553-2-git-send-email-marc.zyngier@arm.com \
    --to=marc.zyngier@arm.com \
    --cc=catalin.marinas@arm.com \
    --cc=christoffer.dall@linaro.org \
    --cc=corbet@lwn.net \
    --cc=jbaron@akamai.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=vladimir.murzin@arm.com \
    /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®