Re: [Bug] WARNING in static_key_disable_cpuslocked

From: Steven Rostedt
Date: Wed Mar 06 2024 - 21:33:09 EST


On Wed, 6 Mar 2024 17:30:09 -0800
Josh Poimboeuf <jpoimboe@xxxxxxxxxx> wrote:

> So, I think we can simplify this nicely by getting rid of the whole -1
> thing altogether:
>
> diff --git a/kernel/jump_label.c b/kernel/jump_label.c
> index d9c822bbffb8..ef7eda7685b2 100644
> --- a/kernel/jump_label.c
> +++ b/kernel/jump_label.c
> @@ -194,20 +194,15 @@ void static_key_enable_cpuslocked(struct static_key *key)
> STATIC_KEY_CHECK_USE(key);
> lockdep_assert_cpus_held();
>
> - if (atomic_read(&key->enabled) > 0) {
> - WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
> + if (atomic_read(&key->enabled) == 1)
> return;
> - }
> -
> jump_label_lock();
> - if (atomic_read(&key->enabled) == 0) {
> - atomic_set(&key->enabled, -1);
> +
> + if (atomic_cmpxchg(&key->enabled, 0, 1) == 0)
> jump_label_update(key);
> - /*
> - * See static_key_slow_inc().
> - */
> - atomic_set_release(&key->enabled, 1);
> - }
> + else
> + WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
> +
> jump_label_unlock();

You may be able to clean it up more with:

int tmp;

tmp = atomic_read(&key->enabled);
if (tmp == 1)
return;

jump_label_lock();

if (!tmp && atomic_try_cmpxchg(&key->enabled, &tmp, 1))
jump_label_update(key);
else
WARN_ON_ONCE(tmp != 1);

jump_label_unlock();

;-)

-- Steve


> }
> EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked);
> @@ -225,14 +220,16 @@ void static_key_disable_cpuslocked(struct static_key *key)
> STATIC_KEY_CHECK_USE(key);
> lockdep_assert_cpus_held();
>
> - if (atomic_read(&key->enabled) != 1) {
> - WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
> + if (atomic_read(&key->enabled) == 0)
> return;
> - }
>
> jump_label_lock();
> - if (atomic_cmpxchg(&key->enabled, 1, 0))
> +
> + if (atomic_cmpxchg(&key->enabled, 1, 0) == 1)
> jump_label_update(key);
> + else
> + WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
> +
> jump_label_unlock();
> }
> EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked);