Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1701152 > unrolled thread

[PATCH 1/3] jump_labels: fix concurrent static_key_enable/disable()

Started byPaolo Bonzini <pbonzini@redhat.com>
First post2017-08-01 17:30 +0200
Last post2017-08-10 14:20 +0200
Articles 4 — 3 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH 1/3] jump_labels: fix concurrent static_key_enable/disable() Paolo Bonzini <pbonzini@redhat.com> - 2017-08-01 17:30 +0200
    Re: [PATCH 1/3] jump_labels: fix concurrent  static_key_enable/disable() Peter Zijlstra <peterz@infradead.org> - 2017-08-01 18:50 +0200
      Re: [PATCH 1/3] jump_labels: fix concurrent  static_key_enable/disable() Paolo Bonzini <pbonzini@redhat.com> - 2017-08-01 18:50 +0200
    [tip:locking/core] jump_label: Fix concurrent  static_key_enable/disable() tip-bot for Paolo Bonzini <tipbot@zytor.com> - 2017-08-10 14:20 +0200

#1701152 — [PATCH 1/3] jump_labels: fix concurrent static_key_enable/disable()

FromPaolo Bonzini <pbonzini@redhat.com>
Date2017-08-01 17:30 +0200
Subject[PATCH 1/3] jump_labels: fix concurrent static_key_enable/disable()
Message-ID<u9Hho-5Gd-29@gated-at.bofh.it>
static_key_enable/disable are trying to cap the static key count to
0/1.  However, their use of key->enabled is outside jump_label_lock
so they do not really ensure that.

Rewrite them to do a quick check for an already enabled (respectively,
already disabled), and then recheck under the jump label lock.  Unlike
static_key_slow_inc/dec, a failed check under the jump label lock does
not modify key->enabled.

Cc: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 include/linux/jump_label.h | 22 +++++++++--------
 kernel/jump_label.c        | 59 +++++++++++++++++++++++++++++-----------------
 2 files changed, 49 insertions(+), 32 deletions(-)

diff --git a/include/linux/jump_label.h b/include/linux/jump_label.h
index 2afd74b9d844..740a42ea7f7f 100644
--- a/include/linux/jump_label.h
+++ b/include/linux/jump_label.h
@@ -234,22 +234,24 @@ static inline int jump_label_apply_nops(struct module *mod)
 
 static inline void static_key_enable(struct static_key *key)
 {
-	int count = static_key_count(key);
-
-	WARN_ON_ONCE(count < 0 || count > 1);
+	STATIC_KEY_CHECK_USE();
 
-	if (!count)
-		static_key_slow_inc(key);
+	if (atomic_read(&key->enabled) != 0) {
+		WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
+		return;
+	}
+	atomic_set(&key->enabled, 1);
 }
 
 static inline void static_key_disable(struct static_key *key)
 {
-	int count = static_key_count(key);
-
-	WARN_ON_ONCE(count < 0 || count > 1);
+	STATIC_KEY_CHECK_USE();
 
-	if (count)
-		static_key_slow_dec(key);
+	if (atomic_read(&key->enabled) != 1) {
+		WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
+		return;
+	}
+	atomic_set(&key->enabled, 0);
 }
 
 #define STATIC_KEY_INIT_TRUE	{ .enabled = ATOMIC_INIT(1) }
diff --git a/kernel/jump_label.c b/kernel/jump_label.c
index 6c9cb208ac48..04a574fd5844 100644
--- a/kernel/jump_label.c
+++ b/kernel/jump_label.c
@@ -78,28 +78,6 @@ int static_key_count(struct static_key *key)
 }
 EXPORT_SYMBOL_GPL(static_key_count);
 
-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);
-}
-EXPORT_SYMBOL_GPL(static_key_enable);
-
-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);
-}
-EXPORT_SYMBOL_GPL(static_key_disable);
-
 void static_key_slow_inc(struct static_key *key)
 {
 	int v, v1;
@@ -136,6 +114,43 @@ void static_key_slow_inc(struct static_key *key)
 }
 EXPORT_SYMBOL_GPL(static_key_slow_inc);
 
+void static_key_enable(struct static_key *key)
+{
+	STATIC_KEY_CHECK_USE();
+	if (atomic_read(&key->enabled) > 0) {
+		WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
+		return;
+	}
+
+	cpus_read_lock();
+	jump_label_lock();
+	if (atomic_read(&key->enabled) == 0) {
+		atomic_set(&key->enabled, -1);
+		jump_label_update(key);
+		atomic_set(&key->enabled, 1);
+	}
+	jump_label_unlock();
+	cpus_read_unlock();
+}
+EXPORT_SYMBOL_GPL(static_key_enable);
+
+void static_key_disable(struct static_key *key)
+{
+	STATIC_KEY_CHECK_USE();
+	if (atomic_read(&key->enabled) != 1) {
+		WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
+		return;
+	}
+
+	cpus_read_lock();
+	jump_label_lock();
+	if (atomic_cmpxchg(&key->enabled, 1, 0))
+		jump_label_update(key);
+	jump_label_unlock();
+	cpus_read_unlock();
+}
+EXPORT_SYMBOL_GPL(static_key_disable);
+
 static void __static_key_slow_dec(struct static_key *key,
 		unsigned long rate_limit, struct delayed_work *work)
 {
-- 
1.8.3.1

[toc] | [next] | [standalone]


#1701237 — Re: [PATCH 1/3] jump_labels: fix concurrent static_key_enable/disable()

FromPeter Zijlstra <peterz@infradead.org>
Date2017-08-01 18:50 +0200
SubjectRe: [PATCH 1/3] jump_labels: fix concurrent static_key_enable/disable()
Message-ID<u9IwP-6mc-19@gated-at.bofh.it>
In reply to#1701152
Thanks for doing these patches, I hadn't come around to them yet.

On Tue, Aug 01, 2017 at 05:24:04PM +0200, Paolo Bonzini wrote:
>  
> +void static_key_enable(struct static_key *key)
> +{
> +	STATIC_KEY_CHECK_USE();
> +	if (atomic_read(&key->enabled) > 0) {
> +		WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
> +		return;
> +	}
> +
> +	cpus_read_lock();
> +	jump_label_lock();
> +	if (atomic_read(&key->enabled) == 0) {
> +		atomic_set(&key->enabled, -1);
> +		jump_label_update(key);

As per the previous discussion, should I do a patch adding barriers here
(or using atomic_set_release()) such that we close the window where a
concurrent inc/enable sees 1 but not all text changes?

> +		atomic_set(&key->enabled, 1);
> +	}
> +	jump_label_unlock();
> +	cpus_read_unlock();
> +}
> +EXPORT_SYMBOL_GPL(static_key_enable);

[toc] | [prev] | [next] | [standalone]


#1701239 — Re: [PATCH 1/3] jump_labels: fix concurrent static_key_enable/disable()

FromPaolo Bonzini <pbonzini@redhat.com>
Date2017-08-01 18:50 +0200
SubjectRe: [PATCH 1/3] jump_labels: fix concurrent static_key_enable/disable()
Message-ID<u9IwQ-6mc-29@gated-at.bofh.it>
In reply to#1701237
On 01/08/2017 18:45, Peter Zijlstra wrote:
> 
> Thanks for doing these patches, I hadn't come around to them yet.
> 
> On Tue, Aug 01, 2017 at 05:24:04PM +0200, Paolo Bonzini wrote:
>>  
>> +void static_key_enable(struct static_key *key)
>> +{
>> +	STATIC_KEY_CHECK_USE();
>> +	if (atomic_read(&key->enabled) > 0) {
>> +		WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
>> +		return;
>> +	}
>> +
>> +	cpus_read_lock();
>> +	jump_label_lock();
>> +	if (atomic_read(&key->enabled) == 0) {
>> +		atomic_set(&key->enabled, -1);
>> +		jump_label_update(key);
> 
> As per the previous discussion, should I do a patch adding barriers here
> (or using atomic_set_release()) such that we close the window where a
> concurrent inc/enable sees 1 but not all text changes?

Sure, and that applies to static_key_slow_inc as well.

Paolo

>> +		atomic_set(&key->enabled, 1);
>> +	}
>> +	jump_label_unlock();
>> +	cpus_read_unlock();
>> +}
>> +EXPORT_SYMBOL_GPL(static_key_enable);

[toc] | [prev] | [next] | [standalone]


#1708535 — [tip:locking/core] jump_label: Fix concurrent static_key_enable/disable()

Fromtip-bot for Paolo Bonzini <tipbot@zytor.com>
Date2017-08-10 14:20 +0200
Subject[tip:locking/core] jump_label: Fix concurrent static_key_enable/disable()
Message-ID<ucUBu-5tj-65@gated-at.bofh.it>
In reply to#1701152
Commit-ID:  1dbb6704de91b169a58d0c8221624afd6a95cfc7
Gitweb:     http://git.kernel.org/tip/1dbb6704de91b169a58d0c8221624afd6a95cfc7
Author:     Paolo Bonzini <pbonzini@redhat.com>
AuthorDate: Tue, 1 Aug 2017 17:24:04 +0200
Committer:  Ingo Molnar <mingo@kernel.org>
CommitDate: Thu, 10 Aug 2017 12:28:56 +0200

jump_label: Fix concurrent static_key_enable/disable()

static_key_enable/disable are trying to cap the static key count to
0/1.  However, their use of key->enabled is outside jump_label_lock
so they do not really ensure that.

Rewrite them to do a quick check for an already enabled (respectively,
already disabled), and then recheck under the jump label lock.  Unlike
static_key_slow_inc/dec, a failed check under the jump label lock does
not modify key->enabled.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: Jason Baron <jbaron@akamai.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/1501601046-35683-2-git-send-email-pbonzini@redhat.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
---
 include/linux/jump_label.h | 22 +++++++++--------
 kernel/jump_label.c        | 59 +++++++++++++++++++++++++++++-----------------
 2 files changed, 49 insertions(+), 32 deletions(-)

diff --git a/include/linux/jump_label.h b/include/linux/jump_label.h
index 2afd74b..740a42e 100644
--- a/include/linux/jump_label.h
+++ b/include/linux/jump_label.h
@@ -234,22 +234,24 @@ static inline int jump_label_apply_nops(struct module *mod)
 
 static inline void static_key_enable(struct static_key *key)
 {
-	int count = static_key_count(key);
-
-	WARN_ON_ONCE(count < 0 || count > 1);
+	STATIC_KEY_CHECK_USE();
 
-	if (!count)
-		static_key_slow_inc(key);
+	if (atomic_read(&key->enabled) != 0) {
+		WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
+		return;
+	}
+	atomic_set(&key->enabled, 1);
 }
 
 static inline void static_key_disable(struct static_key *key)
 {
-	int count = static_key_count(key);
-
-	WARN_ON_ONCE(count < 0 || count > 1);
+	STATIC_KEY_CHECK_USE();
 
-	if (count)
-		static_key_slow_dec(key);
+	if (atomic_read(&key->enabled) != 1) {
+		WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
+		return;
+	}
+	atomic_set(&key->enabled, 0);
 }
 
 #define STATIC_KEY_INIT_TRUE	{ .enabled = ATOMIC_INIT(1) }
diff --git a/kernel/jump_label.c b/kernel/jump_label.c
index d11c506..833eeca 100644
--- a/kernel/jump_label.c
+++ b/kernel/jump_label.c
@@ -79,28 +79,6 @@ int static_key_count(struct static_key *key)
 }
 EXPORT_SYMBOL_GPL(static_key_count);
 
-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);
-}
-EXPORT_SYMBOL_GPL(static_key_enable);
-
-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);
-}
-EXPORT_SYMBOL_GPL(static_key_disable);
-
 void static_key_slow_inc(struct static_key *key)
 {
 	int v, v1;
@@ -139,6 +117,43 @@ void static_key_slow_inc(struct static_key *key)
 }
 EXPORT_SYMBOL_GPL(static_key_slow_inc);
 
+void static_key_enable(struct static_key *key)
+{
+	STATIC_KEY_CHECK_USE();
+	if (atomic_read(&key->enabled) > 0) {
+		WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
+		return;
+	}
+
+	cpus_read_lock();
+	jump_label_lock();
+	if (atomic_read(&key->enabled) == 0) {
+		atomic_set(&key->enabled, -1);
+		jump_label_update(key);
+		atomic_set(&key->enabled, 1);
+	}
+	jump_label_unlock();
+	cpus_read_unlock();
+}
+EXPORT_SYMBOL_GPL(static_key_enable);
+
+void static_key_disable(struct static_key *key)
+{
+	STATIC_KEY_CHECK_USE();
+	if (atomic_read(&key->enabled) != 1) {
+		WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
+		return;
+	}
+
+	cpus_read_lock();
+	jump_label_lock();
+	if (atomic_cmpxchg(&key->enabled, 1, 0))
+		jump_label_update(key);
+	jump_label_unlock();
+	cpus_read_unlock();
+}
+EXPORT_SYMBOL_GPL(static_key_disable);
+
 static void __static_key_slow_dec(struct static_key *key,
 		unsigned long rate_limit, struct delayed_work *work)
 {

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web