mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Andy Lutomirski <luto@amacapital.net>
Cc: Jason Baron <jasonbaron0@gmail.com>,
	Mikulas Patocka <mpatocka@redhat.com>,
	Paul Mackerras <paulus@samba.org>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Kees Cook <keescook@chromium.org>,
	Andrea Arcangeli <aarcange@redhat.com>,
	Vince Weaver <vince@deater.net>,
	"hillf.zj" <hillf.zj@alibaba-inc.com>,
	Valdis Kletnieks <Valdis.Kletnieks@vt.edu>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: Kernel broken on processors without performance counters
Date: Fri, 10 Jul 2015 16:13:59 +0200	[thread overview]
Message-ID: <20150710141359.GJ19282@twins.programming.kicks-ass.net> (raw)
In-Reply-To: <CALCETrUb8tjDLQHF3q=_F7JRXhC8iEHW9o2xFsUbqsmyEOAiCQ@mail.gmail.com>

On Wed, Jul 08, 2015 at 05:36:43PM -0700, Andy Lutomirski wrote:
> >> In what universe is "static_key_false" a reasonable name for a
> >> function that returns true if a static key is true?

> I think the current naming is almost maximally bad.  The naming would
> be less critical if it were typesafe, though.

How about something like so on top? It will allow us to slowly migrate
existing and new users over to the hopefully saner interface?

---
 include/linux/jump_label.h | 67 +++++++++++++++++++++++++++++++++++++++++++++-
 kernel/sched/core.c        | 18 ++++++-------
 2 files changed, 74 insertions(+), 11 deletions(-)

diff --git a/include/linux/jump_label.h b/include/linux/jump_label.h
index f4de473f226b..98ed3c2ec78d 100644
--- a/include/linux/jump_label.h
+++ b/include/linux/jump_label.h
@@ -213,6 +213,71 @@ static inline bool static_key_enabled(struct static_key *key)
 	return static_key_count(key) > 0;
 }
 
-#endif	/* _LINUX_JUMP_LABEL_H */
+static inline void static_key_enable(struct static_key *key)
+{
+	int count = static_key_count(key);
+
+	WARN_ON_ONCE(count < 0 || count > 1);
+
+	if (!count)
+		static_key_slow_inc(key);
+}
+
+static inline void static_key_disable(struct static_key *key)
+{
+	int count = static_key_count(key);
+
+	WARN_ON_ONCE(count < 0 || count > 1);
+
+	if (count)
+		static_key_slow_dec(key);
+}
+
+/* -------------------------------------------------------------------------- */
+
+/*
+ * likely -- default enabled, puts the branch body in-line
+ */
+
+struct static_likely_key {
+	struct static_key key;
+};
+
+#define STATIC_LIKELY_KEY_INIT	(struct static_likely_key){ .key = STATIC_KEY_INIT_TRUE, }
+
+static inline bool static_likely_branch(struct static_likely_key *key)
+{
+	return static_key_true(&key->key);
+}
+
+/*
+ * unlikely -- default disabled, puts the branch body out-of-line
+ */
+
+struct static_unlikely_key {
+	struct static_key key;
+};
+
+#define STATIC_UNLIKELY_KEY_INIT (struct static_unlikely_key){ .key = STATIC_KEY_INIT_FALSE, }
+
+static inline bool static_unlikely_branch(struct static_unlikely_key *key)
+{
+	return static_key_false(&key->key);
+}
+
+/*
+ * Advanced usage; refcount, branch is enabled when: count != 0
+ */
+
+#define static_branch_inc(_k)	static_key_slow_inc(&(_k)->key)
+#define static_branch_dec(_k)	static_key_slow_dec(&(_k)->key)
+
+/*
+ * Normal usage; boolean enable/disable.
+ */
+
+#define static_branch_enable(_k)	static_key_enable(&(_k)->key)
+#define static_branch_disable(_k)	static_key_disable(&(_k)->key)
 
 #endif /* __ASSEMBLY__ */
+#endif	/* _LINUX_JUMP_LABEL_H */
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 78b4bad10081..22ba92297375 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -164,14 +164,12 @@ struct static_key sched_feat_keys[__SCHED_FEAT_NR] = {
 
 static void sched_feat_disable(int i)
 {
-	if (static_key_enabled(&sched_feat_keys[i]))
-		static_key_slow_dec(&sched_feat_keys[i]);
+	static_key_enable(&sched_feat_keys[i]);
 }
 
 static void sched_feat_enable(int i)
 {
-	if (!static_key_enabled(&sched_feat_keys[i]))
-		static_key_slow_inc(&sched_feat_keys[i]);
+	static_key_disable(&sched_feat_keys[i]);
 }
 #else
 static void sched_feat_disable(int i) { };
@@ -2318,17 +2316,17 @@ void wake_up_new_task(struct task_struct *p)
 
 #ifdef CONFIG_PREEMPT_NOTIFIERS
 
-static struct static_key preempt_notifier_key = STATIC_KEY_INIT_FALSE;
+static struct static_unlikely_key preempt_notifier_key = STATIC_UNLIKELY_KEY_INIT;
 
 void preempt_notifier_inc(void)
 {
-	static_key_slow_inc(&preempt_notifier_key);
+	static_branch_inc(&preempt_notifier_key);
 }
 EXPORT_SYMBOL_GPL(preempt_notifier_inc);
 
 void preempt_notifier_dec(void)
 {
-	static_key_slow_dec(&preempt_notifier_key);
+	static_branch_dec(&preempt_notifier_key);
 }
 EXPORT_SYMBOL_GPL(preempt_notifier_dec);
 
@@ -2338,7 +2336,7 @@ EXPORT_SYMBOL_GPL(preempt_notifier_dec);
  */
 void preempt_notifier_register(struct preempt_notifier *notifier)
 {
-	if (!static_key_false(&preempt_notifier_key))
+	if (!static_unlikely_branch(&preempt_notifier_key))
 		WARN(1, "registering preempt_notifier while notifiers disabled\n");
 
 	hlist_add_head(&notifier->link, &current->preempt_notifiers);
@@ -2367,7 +2365,7 @@ static void __fire_sched_in_preempt_notifiers(struct task_struct *curr)
 
 static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
 {
-	if (static_key_false(&preempt_notifier_key))
+	if (static_unlikely_branch(&preempt_notifier_key))
 		__fire_sched_in_preempt_notifiers(curr);
 }
 
@@ -2385,7 +2383,7 @@ static __always_inline void
 fire_sched_out_preempt_notifiers(struct task_struct *curr,
 				 struct task_struct *next)
 {
-	if (static_key_false(&preempt_notifier_key))
+	if (static_unlikely_branch(&preempt_notifier_key))
 		__fire_sched_out_preempt_notifiers(curr, next);
 }
 

  reply	other threads:[~2015-07-10 14:14 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-08 15:17 Mikulas Patocka
2015-07-08 16:07 ` Peter Zijlstra
2015-07-08 16:54   ` Mikulas Patocka
2015-07-09 17:23     ` [PATCH] x86: Fix static_key in load_mm_cr4() Peter Zijlstra
2015-07-09 19:11       ` Mikulas Patocka
2015-07-10  8:27       ` [tip:perf/urgent] x86, perf: Fix static_key bug " tip-bot for Peter Zijlstra
2015-07-08 17:37   ` Kernel broken on processors without performance counters Andy Lutomirski
2015-07-08 20:04     ` Jason Baron
2015-07-09  0:36       ` Andy Lutomirski
2015-07-10 14:13         ` Peter Zijlstra [this message]
2015-07-10 15:29           ` Jason Baron
2015-07-21  8:21           ` Peter Zijlstra
2015-07-21 15:43             ` Thomas Gleixner
2015-07-21 15:49               ` Peter Zijlstra
2015-07-21 15:51                 ` Andy Lutomirski
2015-07-21 16:12                   ` Peter Zijlstra
2015-07-21 16:57                     ` Jason Baron
2015-07-23 14:54                       ` Steven Rostedt
2015-07-21 18:15                     ` Borislav Petkov
2015-07-21 18:50                       ` Jason Baron
2015-07-21 18:54                         ` Andy Lutomirski
2015-07-21 19:00                           ` Valdis.Kletnieks
2015-07-21 19:29                             ` Andy Lutomirski
2015-07-21 23:49                               ` Valdis.Kletnieks
2015-07-22  4:24                         ` Borislav Petkov
2015-07-22 17:06                           ` Jason Baron
2015-07-23 10:42                             ` Peter Zijlstra
2015-07-23 10:53                               ` Borislav Petkov
2015-07-23 14:19                               ` Jason Baron
2015-07-23 14:33                                 ` Peter Zijlstra
2015-07-23 14:49                                   ` Peter Zijlstra
2015-07-23 19:14                                     ` Jason Baron
2015-07-24 10:56                                       ` Peter Zijlstra
2015-07-24 12:36                                         ` Peter Zijlstra
2015-07-24 14:15                                           ` Jason Baron
2015-07-23 14:58                                 ` Peter Zijlstra
2015-07-23 15:34                               ` Steven Rostedt
2015-07-23 17:08                                 ` Peter Zijlstra
2015-07-23 17:18                                   ` Steven Rostedt
2015-07-23 17:33                                   ` Jason Baron
2015-07-23 18:12                                     ` Steven Rostedt
2015-07-23 19:02                                     ` Peter Zijlstra
2015-07-23 17:35                                   ` Andy Lutomirski
2015-07-23 17:54                                   ` Borislav Petkov
2015-07-23 19:02                                     ` Peter Zijlstra
2015-07-24  5:29                                       ` Borislav Petkov
2015-07-24 10:36                                         ` Peter Zijlstra
2015-07-22 20:43                           ` Mikulas Patocka
2015-07-21 15:53                 ` Thomas Gleixner
2015-07-21 15:54                 ` Peter Zijlstra
2015-07-09 17:11       ` Peter Zijlstra
2015-07-09 19:15         ` Jason Baron
2015-07-14  9:35 ` Borislav Petkov
2015-07-14 12:43   ` Mikulas Patocka

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=20150710141359.GJ19282@twins.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=Valdis.Kletnieks@vt.edu \
    --cc=aarcange@redhat.com \
    --cc=acme@kernel.org \
    --cc=hillf.zj@alibaba-inc.com \
    --cc=jasonbaron0@gmail.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=mpatocka@redhat.com \
    --cc=paulus@samba.org \
    --cc=vince@deater.net \
    /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

Powered by JetHome