On Wed, Aug 07, 2024 at 04:03:12PM +0200, Thomas Gleixner wrote: > > + if (static_key_dec(key, true)) // dec-not-one > > Eeew. :-) I knew you'd hate on that > Something like the below? > > Thanks, > > tglx > --- > @@ -250,49 +250,71 @@ void static_key_disable(struct static_ke > } > EXPORT_SYMBOL_GPL(static_key_disable); > > -static bool static_key_slow_try_dec(struct static_key *key) > +static bool static_key_dec(struct static_key *key, bool dec_not_one) > { > + int v = atomic_read(&key->enabled); > > do { > /* > + * Warn about the '-1' case; since that means a decrement is > + * concurrent with a first (0->1) increment. IOW people are > + * trying to disable something that wasn't yet fully enabled. > + * This suggests an ordering problem on the user side. > + * > + * Warn about the '0' case; simple underflow. > */ > + if (WARN_ON_ONCE(v <= 0)) > + return v; > + > + if (dec_not_one && v == 1) > + return v; > + > } while (!likely(atomic_try_cmpxchg(&key->enabled, &v, v - 1))); > > + return v; > +} > + > +/* > + * Fastpath: Decrement if the reference count is greater than one > + * > + * Returns false, if the reference count is 1 or -1 to force the caller > + * into the slowpath. > + * > + * The -1 case is to handle a decrement during a concurrent first enable, > + * which sets the count to -1 in static_key_slow_inc_cpuslocked(). As the > + * slow path is serialized the caller will observe 1 once it acquired the > + * jump_label_mutex, so the slow path can succeed. > + */ > +static bool static_key_dec_not_one(struct static_key *key) > +{ > + int v = static_key_dec(key, true); > + > + return v != 1 && v != -1; if (v < 0) return false; /* * Notably, 0 (underflow) returns true such that it bails out * without doing anything. */ return v != 1; Perhaps? > +} > + > +/* > + * Slowpath: Decrement and test whether the refcount hit 0. > + * > + * Returns true if the refcount hit zero, i.e. the previous value was one. > + */ > +static bool static_key_dec_and_test(struct static_key *key) > +{ > + int v = static_key_dec(key, false); > + > + lockdep_assert_held(&jump_label_mutex); > + return v == 1; > } But yeah, this is nicer!