From: Steven Rostedt <rostedt@goodmis.org>
To: Sam Sun <samsun1006219@gmail.com>
Cc: linux-kernel@vger.kernel.org, syzkaller@googlegroups.com,
xrivendell7@gmail.com, ardb@kernel.org, jbaron@akamai.com,
jpoimboe@kernel.org, peterz@infradead.org, linux-mm@kvack.org,
akpm@linux-foundation.org
Subject: Re: [Bug] WARNING in static_key_disable_cpuslocked
Date: Wed, 6 Mar 2024 10:54:20 -0500 [thread overview]
Message-ID: <20240306105420.6a6bea2c@gandalf.local.home> (raw)
In-Reply-To: <CAEkJfYNNZftjpYBpnH4tEnm82orKtQ6SQn9i3sg7YNO-Df3tSQ@mail.gmail.com>
On Tue, 5 Mar 2024 15:54:24 +0800
Sam Sun <samsun1006219@gmail.com> wrote:
> We analyzed the cause of this bug. It seems that in function
> static_key_disable_cpuslocked(), there is a small racing window
> between two atomic_read(&key->enabled) in line 228 & 229, triggering
> the WARN_ON_ONCE macro. This might cause function returned without
> actually disabling the static_key "key". I am not sure if there is any
> other potential threat here.
>
> I searched on web and found that there was a similar bug reported by
> syzbot several years ago
> (https://groups.google.com/g/syzkaller-bugs/c/_W3lgRCwlb4/m/TqzyQcPpAQAJ).
> At that time the fix was in the net instead of kernel/jump_label.c. So
> I checked the call stack and cc this email to memory management
> maintainers. Hope there is no confusion.
>
> If you have any questions, please contact us.
> Reported by: Yue Sun <samsun1006219@gmail.com>
> Reported by: xingwei lee <xrivendell7@gmail.com>
Thanks for the report. I wonder if it simply needs to add the tests in the
locking? Like the patch below.
Because I could see:
CPU 0 CPU 1
----- -----
key->enabled = 0
static_key_enable_cpus_locked()
jump_label_lock();
static_key_disable_cpus_locked()
if (key->enabled != 1) {
key->enabled = 1;
WARN_ON(key->enabled != 0)
Now I guess the question is, why is something trying to disable something
that is not enabled? Is the above scenario OK? Or should the users of
static_key also prevent this?
-- Steve
diff --git a/kernel/jump_label.c b/kernel/jump_label.c
index d9c822bbffb8..f154caf2a62b 100644
--- a/kernel/jump_label.c
+++ b/kernel/jump_label.c
@@ -194,12 +194,12 @@ void static_key_enable_cpuslocked(struct static_key *key)
STATIC_KEY_CHECK_USE(key);
lockdep_assert_cpus_held();
+ jump_label_lock();
if (atomic_read(&key->enabled) > 0) {
WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
- return;
+ goto unlock;
}
- jump_label_lock();
if (atomic_read(&key->enabled) == 0) {
atomic_set(&key->enabled, -1);
jump_label_update(key);
@@ -208,6 +208,7 @@ void static_key_enable_cpuslocked(struct static_key *key)
*/
atomic_set_release(&key->enabled, 1);
}
+unlock:
jump_label_unlock();
}
EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked);
@@ -225,14 +226,15 @@ void static_key_disable_cpuslocked(struct static_key *key)
STATIC_KEY_CHECK_USE(key);
lockdep_assert_cpus_held();
+ jump_label_lock();
if (atomic_read(&key->enabled) != 1) {
WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
- return;
+ goto unlock;
}
- jump_label_lock();
if (atomic_cmpxchg(&key->enabled, 1, 0))
jump_label_update(key);
+unlock:
jump_label_unlock();
}
EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked);
next prev parent reply other threads:[~2024-03-06 15:52 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-05 7:54 Sam Sun
2024-03-06 15:54 ` Steven Rostedt [this message]
2024-03-06 19:31 ` Josh Poimboeuf
2024-03-06 20:12 ` Jason Baron
2024-03-06 22:16 ` Josh Poimboeuf
2024-03-06 22:40 ` Jason Baron
2024-03-06 23:42 ` Josh Poimboeuf
2024-03-07 1:30 ` Josh Poimboeuf
2024-03-07 2:34 ` Steven Rostedt
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=20240306105420.6a6bea2c@gandalf.local.home \
--to=rostedt@goodmis.org \
--cc=akpm@linux-foundation.org \
--cc=ardb@kernel.org \
--cc=jbaron@akamai.com \
--cc=jpoimboe@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=peterz@infradead.org \
--cc=samsun1006219@gmail.com \
--cc=syzkaller@googlegroups.com \
--cc=xrivendell7@gmail.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