All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: jim.cromie@gmail.com
Cc: Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	Josh Poimboeuf <jpoimboe@kernel.org>,
	Jason Baron <jbaron@akamai.com>,
	Alice Ryhl <aliceryhl@google.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ard Biesheuvel <ardb@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 4/7] jump_label: Add batching API for queued static key updates
Date: Mon, 30 Nov 2026 12:55:09 +0100	[thread overview]
Message-ID: <20261130115509.GL2668804@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20261129-jump-batch-261128-v2-4-b1829d2429f0@gmail.com>

On Sun, Nov 29, 2026 at 12:44:17AM -0700, Jim Cromie via B4 Relay wrote:
> From: Jim Cromie <jim.cromie@gmail.com>
> 
> Built-in subsystems such as dynamic-debug may toggle thousands of
> boolean static keys in one operation, incurring O(N) cross-CPU
> text-poke IPIs when calling static_branch_enable() or
> static_branch_disable() one key at a time.
> 
> Add a core-only batching API that amortizes cross-CPU text-poke
> synchronization across a batch of static keys (up to
> TEXT_POKE_ARRAY_MAX = 256 pokes per flush on x86) while keeping
> key->enabled consistent with live kernel text:
> 
> 0. Add static_key_batch_begin() and static_key_batch_end() (wrapped by
>    DEFINE_LOCK_GUARD_0(static_key_batch, ...) and static_branch_batch_*
>    helpers) to hold cpus_read_lock() and jump_label_lock() across a
>    batch and flush pending updates on exit.
> 1. Add static_key_enable_queued() and static_key_disable_queued()
>    (plus static_branch_*_queued() wrappers and !CONFIG_JUMP_LABEL
>    stubs), unified in __static_key_set_queued(key, enable) and
>    unexported so only built-in core code may batch-flip keys.
> 2. Defer publishing final key->enabled values (1 or 0) until after IPI
>    completion using negative transitional markers:
>    STATIC_KEY_TRANS_ENABLE (-1: 0 -> -1 -> 1, counted as 1) and
>    STATIC_KEY_TRANS_DISABLE (-2: 1 -> -2 -> 0, counted as 0). Record
>    in-flight keys in jump_label_queued_keys[MAX_QUEUED_KEYS] and
>    release-store their stable state in jump_label_finalize_queued_keys()
>    after arch_jump_label_transform_apply().
> 3. Flush the active batch via __static_key_flush_queued() before
>    queueing when jump_label_queued_keys[] is full, when the arch
>    transform queue fills mid-key, or when a batch reverses a key already
>    in-flight in the opposite direction (val == opp_trans) so live text
>    and key->enabled settle before the reverse patch is generated.
> 4. Fall back to immediate jump_label_update() on architectures without
>    HAVE_JUMP_LABEL_BATCH, and extend jump_label_test() to exercise
>    scoped_guard(static_key_batch) and same-batch reversals.


So this is just an iteration of what has been done; which I'm sure your
agent or something wanted, but it tells me nothing of *WHY*. That is,
for me the human, this all sucks balls.

For instance, why do we need STATIC_KEY_TRANS_DISABLE ? We don't have
this state currently. We set key->enable to 0 before we start patching,
and this is fine. The above only states we add this gunk, but no
explanation for why you think this is needed.

Why do I have to guess at this?

Also, I would much prefer not having static_key_{en,dis}able_queued() at
all. It would be ever so much better if you could just use the existing
static_key_{en,dis}able() inside of the batch guard, no? Smaller API
more better.

What's more; I think your previous patch is completely unneeded
complexity, and I hate your endless #ifdef sprinkling.

Does something like so work?

---
--- a/include/linux/jump_label.h
+++ b/include/linux/jump_label.h
@@ -222,15 +222,17 @@ extern int jump_label_text_reserved(void
 extern bool static_key_slow_inc(struct static_key *key);
 extern bool static_key_fast_inc_not_disabled(struct static_key *key);
 extern void static_key_slow_dec(struct static_key *key);
-extern bool static_key_slow_inc_cpuslocked(struct static_key *key);
-extern void static_key_slow_dec_cpuslocked(struct static_key *key);
 extern int static_key_count(struct static_key *key);
 extern void static_key_enable(struct static_key *key);
 extern void static_key_disable(struct static_key *key);
-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);
 
+extern void static_key_batch_begin(void);
+extern void static_key_batch_end(void);
+
+extern void static_key_batch_begin_cpuslocked(void);
+extern void static_key_batch_end_cpuslocked(void);
+
 #define STATIC_KEY_INIT_TRUE					\
 	{ .enabled = ATOMIC_INIT(1),				\
 	  .type = JUMP_TYPE_TRUE }
@@ -293,9 +295,6 @@ static inline void static_key_slow_dec(s
 	atomic_dec(&key->enabled);
 }
 
-#define static_key_slow_inc_cpuslocked(key) static_key_slow_inc(key)
-#define static_key_slow_dec_cpuslocked(key) static_key_slow_dec(key)
-
 static inline int jump_label_text_reserved(void *start, void *end)
 {
 	return 0;
@@ -326,8 +325,11 @@ static inline void static_key_disable(st
 	atomic_set(&key->enabled, 0);
 }
 
-#define static_key_enable_cpuslocked(k)		static_key_enable((k))
-#define static_key_disable_cpuslocked(k)	static_key_disable((k))
+static inline void static_key_batch_begin(void) { }
+static inline void static_key_batch_end(void) { }
+
+static inline void static_key_batch_begin_cpuslocked(void) { }
+static inline void static_key_batch_end_cpuslocked(void) { }
 
 #define STATIC_KEY_INIT_TRUE	{ .enabled = ATOMIC_INIT(1) }
 #define STATIC_KEY_INIT_FALSE	{ .enabled = ATOMIC_INIT(0) }
@@ -335,10 +337,37 @@ static inline void static_key_disable(st
 #endif	/* CONFIG_JUMP_LABEL */
 
 DEFINE_LOCK_GUARD_0(jump_label_lock, jump_label_lock(), jump_label_unlock())
+DEFINE_LOCK_GUARD_0(static_key_batch, static_key_batch_begin(), static_key_batch_end())
+DEFINE_LOCK_GUARD_0(static_key_batch_cpuslocked,
+		    static_key_batch_begin_cpuslocked(),
+		    static_key_batch_end_cpuslocked())
 
 #define STATIC_KEY_INIT STATIC_KEY_INIT_FALSE
 #define jump_label_enabled static_key_enabled
 
+static inline void static_key_slow_inc_cpuslocked(struct static_key *key)
+{
+	guard(static_key_batch_cpuslocked)();
+	static_key_slow_inc(key);
+}
+
+static inline void static_key_slow_dec_cpuslocked(struct static_key *key)
+{
+	guard(static_key_batch_cpuslocked)();
+	static_key_slow_dec(key);
+}
+
+static inline void static_key_enable_cpuslocked(struct static_key *key)
+{
+	guard(static_key_batch_cpuslocked)();
+	static_key_enable(key);
+}
+
+static inline void static_key_disable_cpuslocked(struct static_key *key)
+{
+	guard(static_key_batch_cpuslocked)();
+	static_key_disable(key);
+}
 /* -------------------------------------------------------------------------- */
 
 /*
@@ -512,8 +541,6 @@ extern bool ____wrong_branch_error(void)
 
 #define static_branch_inc(x)		static_key_slow_inc(&(x)->key)
 #define static_branch_dec(x)		static_key_slow_dec(&(x)->key)
-#define static_branch_inc_cpuslocked(x)	static_key_slow_inc_cpuslocked(&(x)->key)
-#define static_branch_dec_cpuslocked(x)	static_key_slow_dec_cpuslocked(&(x)->key)
 
 /*
  * Normal usage; boolean enable/disable.
@@ -521,8 +548,6 @@ extern bool ____wrong_branch_error(void)
 
 #define static_branch_enable(x)			static_key_enable(&(x)->key)
 #define static_branch_disable(x)		static_key_disable(&(x)->key)
-#define static_branch_enable_cpuslocked(x)	static_key_enable_cpuslocked(&(x)->key)
-#define static_branch_disable_cpuslocked(x)	static_key_disable_cpuslocked(&(x)->key)
 
 #endif /* __ASSEMBLER__ */
 
--- a/kernel/jump_label.c
+++ b/kernel/jump_label.c
@@ -113,6 +113,85 @@ int static_key_count(struct static_key *
 }
 EXPORT_SYMBOL_GPL(static_key_count);
 
+static void __jump_label_finish(void);
+
+static struct task_struct *batch_owner;
+
+void static_key_batch_begin_cpuslocked(void)
+{
+	lockdep_assert_cpus_held();
+	jump_label_lock();
+	batch_owner = current;
+	barrier();
+}
+
+void static_key_batch_begin(void)
+{
+	cpus_read_lock();
+	static_key_batch_begin_cpuslocked();
+}
+
+void static_key_batch_end_cpuslocked(void)
+{
+	lockdep_assert(batch_owner == current);
+
+	__jump_label_finish();
+	barrier();
+	batch_owner = NULL;
+	jump_label_unlock();
+}
+
+void static_key_batch_end(void)
+{
+	static_key_batch_begin_cpuslocked();
+	cpus_read_unlock();
+}
+
+static void jump_label_batch_lock(void)
+{
+	if (batch_owner == current) {
+		lockdep_assert_held(&jump_label_mutex);
+		return;
+	}
+
+	jump_label_lock();
+}
+
+static void jump_label_batch_unlock(void)
+{
+	if (batch_owner == current) {
+		lockdep_assert_held(&jump_label_mutex);
+		return;
+	}
+
+	__jump_label_finish();
+	jump_label_unlock();
+}
+
+DEFINE_LOCK_GUARD_0(jump_label_batch_lock, jump_label_batch_lock(), jump_label_batch_unlock())
+
+static void cpus_read_batch_lock(void)
+{
+	if (batch_owner == current) {
+		lockdep_assert_cpus_held();
+		return;
+	}
+
+	cpus_read_lock();
+}
+
+static void cpus_read_batch_unlock(void)
+{
+	if (batch_owner == current) {
+		lockdep_assert_cpus_held();
+		return;
+	}
+
+	cpus_read_unlock();
+}
+
+DEFINE_LOCK_GUARD_0(cpus_read_batch_lock, cpus_read_batch_lock(), cpus_read_batch_unlock())
+
 /*
  * static_key_fast_inc_not_disabled - adds a user for a static key
  * @key: static key that must be already enabled
@@ -136,7 +215,7 @@ bool static_key_fast_inc_not_disabled(st
 	 *
 	 * The INT_MAX overflow condition is either used by the networking
 	 * code to reset or detected in the slow path of
-	 * static_key_slow_inc_cpuslocked().
+	 * static_key_slow_inc().
 	 */
 	v = atomic_read(&key->enabled);
 	do {
@@ -148,10 +227,8 @@ bool static_key_fast_inc_not_disabled(st
 }
 EXPORT_SYMBOL_GPL(static_key_fast_inc_not_disabled);
 
-bool static_key_slow_inc_cpuslocked(struct static_key *key)
+bool static_key_slow_inc(struct static_key *key)
 {
-	lockdep_assert_cpus_held();
-
 	/*
 	 * Careful if we get concurrent static_key_slow_inc/dec() calls;
 	 * later calls must wait for the first one to _finish_ the
@@ -162,7 +239,8 @@ bool static_key_slow_inc_cpuslocked(stru
 	if (static_key_fast_inc_not_disabled(key))
 		return true;
 
-	guard(mutex)(&jump_label_mutex);
+	guard(cpus_read_batch_lock)();
+	guard(jump_label_batch_lock)();
 	/* Try to mark it as 'enabling in progress. */
 	if (!atomic_cmpxchg(&key->enabled, 0, -1)) {
 		jump_label_update(key);
@@ -182,29 +260,19 @@ bool static_key_slow_inc_cpuslocked(stru
 	}
 	return true;
 }
-
-bool static_key_slow_inc(struct static_key *key)
-{
-	bool ret;
-
-	cpus_read_lock();
-	ret = static_key_slow_inc_cpuslocked(key);
-	cpus_read_unlock();
-	return ret;
-}
 EXPORT_SYMBOL_GPL(static_key_slow_inc);
 
-void static_key_enable_cpuslocked(struct static_key *key)
+void static_key_enable(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);
 		return;
 	}
 
-	jump_label_lock();
+	guard(cpus_read_batch_lock)();
+	guard(jump_label_batch_lock)();
 	if (atomic_read(&key->enabled) == 0) {
 		atomic_set(&key->enabled, -1);
 		jump_label_update(key);
@@ -213,40 +281,22 @@ void static_key_enable_cpuslocked(struct
 		 */
 		atomic_set_release(&key->enabled, 1);
 	}
-	jump_label_unlock();
-}
-EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked);
-
-void static_key_enable(struct static_key *key)
-{
-	cpus_read_lock();
-	static_key_enable_cpuslocked(key);
-	cpus_read_unlock();
 }
 EXPORT_SYMBOL_GPL(static_key_enable);
 
-void static_key_disable_cpuslocked(struct static_key *key)
+void static_key_disable(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);
 		return;
 	}
 
-	jump_label_lock();
+	guard(cpus_read_batch_lock)();
+	guard(jump_label_batch_lock)();
 	if (atomic_cmpxchg(&key->enabled, 1, 0) == 1)
 		jump_label_update(key);
-	jump_label_unlock();
-}
-EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked);
-
-void static_key_disable(struct static_key *key)
-{
-	cpus_read_lock();
-	static_key_disable_cpuslocked(key);
-	cpus_read_unlock();
 }
 EXPORT_SYMBOL_GPL(static_key_disable);
 
@@ -260,7 +310,7 @@ static bool static_key_dec_not_one(struc
 	 * is an imbalance, which is handled at the call site.
 	 *
 	 * That includes the special case of '-1' which is set in
-	 * static_key_slow_inc_cpuslocked(), but that's harmless as it is
+	 * static_key_slow_inc(), but that's harmless as it is
 	 * fully serialized in the slow path below. By the time this task
 	 * acquires the jump label lock the value is back to one and the
 	 * retry under the lock must succeed.
@@ -289,19 +339,21 @@ static bool static_key_dec_not_one(struc
 	return true;
 }
 
-static void __static_key_slow_dec_cpuslocked(struct static_key *key)
+void static_key_slow_dec(struct static_key *key)
 {
-	lockdep_assert_cpus_held();
 	int val;
 
+	STATIC_KEY_CHECK_USE(key);
+
 	if (static_key_dec_not_one(key))
 		return;
 
+	guard(cpus_read_batch_lock)();
 	guard(mutex)(&jump_label_mutex);
 	val = atomic_read(&key->enabled);
 	/*
 	 * It should be impossible to observe -1 with jump_label_mutex held,
-	 * see static_key_slow_inc_cpuslocked().
+	 * see static_key_slow_inc().
 	 */
 	if (WARN_ON_ONCE(val == -1))
 		return;
@@ -314,35 +366,16 @@ static void __static_key_slow_dec_cpuslo
 	if (atomic_dec_and_test(&key->enabled))
 		jump_label_update(key);
 }
-
-static void __static_key_slow_dec(struct static_key *key)
-{
-	cpus_read_lock();
-	__static_key_slow_dec_cpuslocked(key);
-	cpus_read_unlock();
-}
+EXPORT_SYMBOL_GPL(static_key_slow_dec);
 
 void jump_label_update_timeout(struct work_struct *work)
 {
 	struct static_key_deferred *key =
 		container_of(work, struct static_key_deferred, work.work);
-	__static_key_slow_dec(&key->key);
+	static_key_slow_dec(&key->key);
 }
 EXPORT_SYMBOL_GPL(jump_label_update_timeout);
 
-void static_key_slow_dec(struct static_key *key)
-{
-	STATIC_KEY_CHECK_USE(key);
-	__static_key_slow_dec(key);
-}
-EXPORT_SYMBOL_GPL(static_key_slow_dec);
-
-void static_key_slow_dec_cpuslocked(struct static_key *key)
-{
-	STATIC_KEY_CHECK_USE(key);
-	__static_key_slow_dec_cpuslocked(key);
-}
-
 void __static_key_slow_dec_deferred(struct static_key *key,
 				    struct delayed_work *work,
 				    unsigned long timeout)
@@ -489,6 +522,10 @@ static bool jump_label_can_update(struct
 }
 
 #ifndef HAVE_JUMP_LABEL_BATCH
+static void __jump_label_finish(void)
+{
+}
+
 static void __jump_label_update(struct static_key *key,
 				struct jump_entry *entry,
 				struct jump_entry *stop,
@@ -499,7 +536,28 @@ static void __jump_label_update(struct s
 			arch_jump_label_transform(entry, jump_label_type(entry));
 	}
 }
+
 #else
+
+#ifndef JUMP_LABEL_BATCH_SIZE
+#define JUMP_LABEL_BATCH_SIZE 256
+#endif
+
+static struct static_key *jump_label_queue[JUMP_LABEL_BATCH_SIZE];
+static unsigned int jump_label_nr_queued;
+
+static void __jump_label_finish(void)
+{
+	arch_jump_label_transform_apply();
+	for (unsigned int i = 0; i < jump_label_nr_queued; i++) {
+		struct static_key *key = jump_label_queue[i];
+
+		if (atomic_read(&key->enabled) == -1)
+			atomic_set_release(&key->enabled, 1);
+	}
+	jump_label_nr_queued = 0;
+}
+
 static void __jump_label_update(struct static_key *key,
 				struct jump_entry *entry,
 				struct jump_entry *stop,
@@ -510,15 +568,16 @@ static void __jump_label_update(struct s
 		if (!jump_label_can_update(entry, init))
 			continue;
 
-		if (!arch_jump_label_transform_queue(entry, jump_label_type(entry))) {
+		if (jump_label_nr_queued >= JUMP_LABEL_BATCH_SIZE ||
+		    !arch_jump_label_transform_queue(entry, jump_label_type(entry))) {
 			/*
 			 * Queue is full: Apply the current queue and try again.
 			 */
-			arch_jump_label_transform_apply();
+			__jump_label_finish();
 			BUG_ON(!arch_jump_label_transform_queue(entry, jump_label_type(entry)));
 		}
+		jump_label_queue[jump_label_nr_queued++] = key;
 	}
-	arch_jump_label_transform_apply();
 }
 #endif
 

  reply	other threads:[~2026-11-30 11:55 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29  7:44 [PATCH v2 0/7] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
2026-11-29  7:44 ` Jim Cromie
2026-11-29  7:44 ` [PATCH v2 1/7] x86/alternatives: Keep text-poke batch sorted on insert Jim Cromie via B4 Relay
2026-11-29  7:44   ` Jim Cromie
2026-11-30 16:13   ` Steven Rostedt
2026-11-29  7:44 ` [PATCH v2 2/7] x86/jump_label: Sync arch_jump_label_transform_queue() with batch limit Jim Cromie via B4 Relay
2026-11-29  7:44   ` Jim Cromie
2026-11-29  7:44 ` [PATCH v2 3/7] jump_label: Plumb bool apply through jump_label_update_key() Jim Cromie via B4 Relay
2026-11-29  7:44   ` Jim Cromie
2026-11-29  7:44 ` [PATCH v2 4/7] jump_label: Add batching API for queued static key updates Jim Cromie via B4 Relay
2026-11-29  7:44   ` Jim Cromie
2026-11-30 11:55   ` Peter Zijlstra [this message]
2026-11-30 13:51     ` Peter Zijlstra
2026-11-29  7:44 ` [PATCH v2 5/7] dyndbg: Use queued static-key batching in ddebug_change() Jim Cromie via B4 Relay
2026-11-29  7:44   ` Jim Cromie
2026-11-29  7:44 ` [PATCH v2 6/7] dyndbg: Batch classmap param bit-changes into multi-query Jim Cromie via B4 Relay
2026-11-29  7:44   ` Jim Cromie
2026-11-29  7:44 ` [PATCH v2 7/7] dyndbg: Hoist lock and static_key_batch across queries Jim Cromie via B4 Relay
2026-11-29  7:44   ` Jim Cromie

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=20261130115509.GL2668804@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=akpm@linux-foundation.org \
    --cc=aliceryhl@google.com \
    --cc=ardb@kernel.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=jbaron@akamai.com \
    --cc=jim.cromie@gmail.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=rostedt@goodmis.org \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.