[PATCH v2 1/3] jump_label: Add static_key_fast_inc()

From: Dmitry Safonov
Date: Thu Nov 03 2022 - 17:25:45 EST


A helper to add another user for an existing enabled static key.

Cc: Ard Biesheuvel <ardb@xxxxxxxxxx>
Cc: Jason Baron <jbaron@xxxxxxxxxx>
Cc: Josh Poimboeuf <jpoimboe@xxxxxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Steven Rostedt <rostedt@xxxxxxxxxxx>
Suggested-by: Eric Dumazet <edumazet@xxxxxxxxxx>
Signed-off-by: Dmitry Safonov <dima@xxxxxxxxxx>
---
include/linux/jump_label.h | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/include/linux/jump_label.h b/include/linux/jump_label.h
index 570831ca9951..cb6c722c4816 100644
--- a/include/linux/jump_label.h
+++ b/include/linux/jump_label.h
@@ -75,6 +75,8 @@

#include <linux/types.h>
#include <linux/compiler.h>
+#include <linux/atomic.h>
+#include <linux/bug.h>

extern bool static_key_initialized;

@@ -235,6 +237,21 @@ extern void static_key_enable_cpuslocked(struct static_key *key);
extern void static_key_disable_cpuslocked(struct static_key *key);
extern enum jump_label_type jump_label_init_type(struct jump_entry *entry);

+/***
+ * static_key_fast_inc - adds a user for a static key
+ * @key: static key that must be already enabled
+ *
+ * The caller must make sure that the static key can't get disabled while
+ * in this function. It doesn't patch jump labels, only adds a user to
+ * an already enabled static key.
+ */
+static inline void static_key_fast_inc(struct static_key *key)
+{
+ STATIC_KEY_CHECK_USE(key);
+ WARN_ON_ONCE(atomic_read(&key->enabled) < 1);
+ atomic_inc(&key->enabled);
+}
+
/*
* We should be using ATOMIC_INIT() for initializing .enabled, but
* the inclusion of atomic.h is problematic for inclusion of jump_label.h
@@ -251,9 +268,6 @@ extern enum jump_label_type jump_label_init_type(struct jump_entry *entry);

#else /* !CONFIG_JUMP_LABEL */

-#include <linux/atomic.h>
-#include <linux/bug.h>
-
static __always_inline int static_key_count(struct static_key *key)
{
return arch_atomic_read(&key->enabled);
@@ -290,6 +304,7 @@ static inline void static_key_slow_dec(struct static_key *key)
atomic_dec(&key->enabled);
}

+#define static_key_fast_inc(key) static_key_slow_inc(key)
#define static_key_slow_inc_cpuslocked(key) static_key_slow_inc(key)
#define static_key_slow_dec_cpuslocked(key) static_key_slow_dec(key)

--
2.38.1