mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Jakub Kicinski <jakub.kicinski@netronome.com>
Cc: pbonzini@redhat.com, ard.biesheuvel@linaro.org,
	tglx@linutronix.de, mingo@kernel.org,
	linux-kernel@vger.kernel.org, oss-drivers@netronome.com
Subject: Re: [PATCH] locking/static_key: Fix false positive warnings on concurrent dec/inc
Date: Wed, 20 Mar 2019 17:14:51 +0100	[thread overview]
Message-ID: <20190320161451.GJ6058@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <20190319104657.6fa99d82@cakuba.netronome.com>

On Tue, Mar 19, 2019 at 10:46:57AM -0700, Jakub Kicinski wrote:

> That indeed looks far cleanest, thanks!
> 
> Tested-by: Jakub Kicinski <jakub.kicinski@netronome.com>

Thanks, I've made it into the below patch.

---

Subject: locking/static_key: Fix false positive warnings on concurrent dec/inc
From: Peter Zijlstra <peterz@infradead.org>
Date: Tue, 19 Mar 2019 13:18:56 +0100

Even though the atomic_dec_and_mutex_lock() in
__static_key_slow_dec_cpuslocked() can never see a negative value in
key->enabled the subsequent sanity check is re-reading key->enabled, which may
have been set to -1 in the meantime by static_key_slow_inc_cpuslocked().


                CPU  A                               CPU B

 __static_key_slow_dec_cpuslocked():          static_key_slow_inc_cpuslocked():
                               # enabled = 1
   atomic_dec_and_mutex_lock()
                               # enabled = 0
                                              atomic_read() == 0
                                              atomic_set(-1)
                               # enabled = -1
   val = atomic_read()
   # Oops - val == -1!


The test case is TCP's clean_acked_data_enable() / clean_acked_data_disable()
as tickled by ktls (net/ktls).

Cc: mingo@kernel.org
Cc: oss-drivers@netronome.com
Cc: ard.biesheuvel@linaro.org
Cc: tglx@linutronix.de
Cc: pbonzini@redhat.com
Reported-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Suggested-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Tested-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 kernel/jump_label.c |   21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

--- a/kernel/jump_label.c
+++ b/kernel/jump_label.c
@@ -206,6 +206,8 @@ static void __static_key_slow_dec_cpuslo
 					   unsigned long rate_limit,
 					   struct delayed_work *work)
 {
+	int val;
+
 	lockdep_assert_cpus_held();
 
 	/*
@@ -215,17 +217,20 @@ static void __static_key_slow_dec_cpuslo
 	 * returns is unbalanced, because all other static_key_slow_inc()
 	 * instances block while the update is in progress.
 	 */
-	if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) {
-		WARN(atomic_read(&key->enabled) < 0,
-		     "jump label: negative count!\n");
+	val = atomic_fetch_add_unless(&key->enabled, -1, 1);
+	if (val != 1) {
+		WARN(val < 0, "jump label: negative count!\n");
 		return;
 	}
 
-	if (rate_limit) {
-		atomic_inc(&key->enabled);
-		schedule_delayed_work(work, rate_limit);
-	} else {
-		jump_label_update(key);
+	jump_label_lock();
+	if (atomic_dec_and_test(&key->enabled)) {
+		if (rate_limit) {
+			atomic_inc(&key->enabled);
+			schedule_delayed_work(work, rate_limit);
+		} else {
+			jump_label_update(key);
+		}
 	}
 	jump_label_unlock();
 }

      reply	other threads:[~2019-03-20 16:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-18 21:58 Jakub Kicinski
2019-03-19 12:18 ` Peter Zijlstra
2019-03-19 17:46   ` Jakub Kicinski
2019-03-20 16:14     ` Peter Zijlstra [this message]

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=20190320161451.GJ6058@hirez.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=ard.biesheuvel@linaro.org \
    --cc=jakub.kicinski@netronome.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=oss-drivers@netronome.com \
    --cc=pbonzini@redhat.com \
    --cc=tglx@linutronix.de \
    /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®