From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796408905; b=AMeA9qVBfO4HFsIjn4r5yEKUg9UX9mhhVx8iHQbvzVx2nDV0hkNIohTGVFleCVBsjSaO FQLIjaTFvekKsefW+lVciTG4o/0uNN9UcdFykr3PX6apnWBxl3XjuDDJ6D5gV/2shM55e WGpZTr0yzuesxrX1RM3JZsUJ9Yw3Qov3KzVsz8vqPgU8HkcnakFSd0iE6mIcFp49lRzuV +Wq1nDTxXB0G7HychtnqxjiRAQ84/nLDzp1WsMmzEOy97vynl1CPe/cFWdjptx71VtR0r 3ZuhGPprzbmT3VSu2Dol8evwAQsVcvltzV+cm6nbXusiGi4PwEY1cGTZp5lmaocSAQg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796408905; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=NSr7+v+Rs2RSA9hliroTEUrSfNFKSO8v0Mb/OMN8S4U=; b=jxg7aAiXpMTsA9j9aOqGa6mMg9xfarvqiBeSJMQZl28+4YO8xDvQXWOmlk0fPLpjiTOk 6URcWS83YdB4Uo6G3sCRKMRVdg99ZtpQbAJyOQKCecx7bpTSe03N0TAQegNTEqX0KGDDP 7X43pdeazbDGl/0bV3SzqI+DHa+PQUUTQISIUasSKmwsZfteTez7az01L1rwIIwe5JJUF 3nQKX0uBsl3DZypOn3yUrRHIilqQ3qfvPzWEPi5S/OPTu13K8DK9H0yRIWMvktqgqVd8w FMlJtLCTgSZyyPrJM+MQ10gCbEEuVhy5J9uLOWuYCA1uyS/1H59WH6uFhZcdHi3juOQ== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=knxBOvGQ; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 247F8606A697 for ; Fri, 4 Dec 2026 18:28:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=knxBOvGQ Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796408905; b=r/MrRzCf0ciZlIJsRL5sKEnqshyw7e4Lu37lqUi+b0NOGTHgvUoImY7UlYWpsbkZgqyh tSbv5E2ngKhHPs8baiFMMsGXh7Fpj/PIi9tGmB+sFzE1F1G/RTerELyZFcXAnjmAILtzq 92C5Zlc1DKYAf8tSugtyBHiX09P2b5jbLFPKcN06JWVKQTrVDeujVK6nsZ+IuaMKKlS5A jf59aH9bbOJY21LpfrXWvSC/PJlnCXjJk4L1kVnG7N/Q2ZokyA2QPR+AHdQ1gCRMqPEOK f71S6M9mo3naOXhsATEf0YGshL1niw+1JHkr0nO6cfzHESMzvuv4z/Y+rc39gkdSPDg== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796408905; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=NSr7+v+Rs2RSA9hliroTEUrSfNFKSO8v0Mb/OMN8S4U=; b=1YQp9qvJgteohL1joEBmfz8pyN60KLtdB/sNzEzZ8a/zHAnicjfURKkigmfRBhMtNDsu sfhdOhNgzecQelRsZFrR8dLqc6Khd+AS98c8zEkWyoc7SXtqK9Wqh6L/7Y95G7LO5dDN/ NMKtI1/GSQfcj9BpEkjMpyhURDTkdCsRHASOQhhloLKrBwgIak6AgbINuTi05dvGsxzLG 4idPXe3mBvHBn/H9kXQxwn2Ko+lmh5EcnwpTKEyGub5zq4WYCEMsUFhqkgj8a7VeLDBVr y4EIx8kRXAYA+iADAh8Bp7ayauVrYtENQYY4PRsfK76JnbKr1J2MrjzudEqQlSkiGrA== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 2C93F1F00A07; Fri, 4 Dec 2026 18:28:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796408905; bh=NSr7+v+Rs2RSA9hliroTEUrSfNFKSO8v0Mb/OMN8S4U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=knxBOvGQmFf1rgiUKZ24gvlKdY9JDvkPcR3LAlgFFRxnBJ1zmVy138xi69Knx2cW7 49jeZj9m+uDCOAKgfvkqXK/lt9gP7uSgPu8TVzk3UVC5vI/bW4o/zi1gjXGN1vy4IA qapEMJcTTj1ZKIUWQs8oepS7w+c/M6tzu7SOcN7BtVeO78RSwcssWaow7iO1HWG9Wo VNQpeUUcWV46FJlSE06p7NSWt6l6vGn6gbEZAGw9WotsNa3HbifiNi2KOqjZfKYFjs DKk1s6aPrbBBmqP3UTS3vL6L2CNkcmOfIEEnlzvuW0nQI4DaV1VEw3uDVNubm/ul3t yVIfk6CKOeW8g== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id AD4094DC1A; Fri, 4 Dec 2026 18:28:22 +0000 (UTC) From: Jim Cromie via B4 Relay Date: Fri, 04 Dec 2026 11:27:54 -0700 Subject: [PATCH v3 3/5] jump_label: Add static_key_batch API X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261204-jump-batch-261128-v3-3-2adda55a5d71@gmail.com> References: <20261204-jump-batch-261128-v3-0-2adda55a5d71@gmail.com> In-Reply-To: <20261204-jump-batch-261128-v3-0-2adda55a5d71@gmail.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Alice Ryhl , Steven Rostedt , Ard Biesheuvel , Andrew Morton Cc: linux-kernel@vger.kernel.org, Jim Cromie X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796408901; l=20695; i=jim.cromie@gmail.com; s=20260203; h=from:subject:message-id; bh=rnjLli0PJ/IUDI8hYB3uqeRRfANIdrZXPry+/kPE3bQ=; b=52jXWXsm9xh0U+OBCgBiTBvyCJh5B37gFI7zCZPWXaRJY5uRmlIDlVD2oxwdpnqYqKwwaU8X5 RpY8Dvd1ytYCqkas07wKQ/P5glQRi7YOJ1E8NOFjpbDAF1inLdv+vwa X-Developer-Key: i=jim.cromie@gmail.com; a=ed25519; pk=C6E5ODlPQo7ZBynATXH9wg7K6HxP0pIXyf4s38Qw0XE= X-Endpoint-Received: by B4 Relay for jim.cromie@gmail.com/20260203 with auth_id=958 X-Original-From: Jim Cromie Reply-To: jim.cromie@gmail.com Content-Transfer-Encoding: 7bit From: Jim Cromie Toggling many static keys in a loop -- such as dynamic-debug enabling thousands of pr_debug() sites -- currently runs arch_jump_label_transform_apply() and cross-CPU sync IPIs once per key. Instead of adding a parallel queued static-key API, introduce static_key_batch and static_key_batch_cpuslocked guards that hold cpus_read_lock() and jump_label_mutex across a block, record batch_owner = current, and defer __jump_label_finish() until the outermost guard exits. Routing static_key_{enable,disable,slow_inc, slow_dec}() through batch-aware lock guards lets callers coalesce pokes using the existing API, and collapses the duplicated _cpuslocked() implementations into inline static_key_batch_cpuslocked wrappers. Existing key->enabled protocol (unchanged for single-key callers): Lockless paths assume key->enabled >= 1 means text is already patched: - static_key_fast_inc_not_disabled(): v >= 1 -> CAS v + 1, return - static_key_dec_not_one(): v > 1 -> CAS v - 1, return - static_key_enable() entry: v > 0 -> early return - static_key_disable() entry: v == 0 -> early return - static_key_count() / _enabled(): treats -1 as 1 (also read by jump_label_type() during patching to choose JMP vs NOP) Under jump_label_mutex, transitions set direction before patching: - Enable (0 -> -1 -> [patch] -> 1): -1 selects JMP for patching while keeping concurrent lockless callers on jump_label_mutex until patching finishes and releases 1. - Disable (1 -> 0 -> [patch]): writes 0 up front because 0 already selects NOP and keeps concurrent inc/enable callers on the mutex. Changes needed to extend that protocol across a multi-key batch: 1. Deferred completion and -1 handling: Queue each updated key in jump_label_queue[256] (via jump_label_queue_key(), including 0-site keys) and defer arch_jump_label_transform_apply() plus the -1 -> 1 release to __jump_label_finish(). Because enabled keys stay -1 until flush, static_key_disable() must fall through on -1 onto jump_label_mutex instead of warning on != 1 and returning early, and static_key_dec_not_one() suppresses its -1 warning for batch_owner. 2. Same-batch key revisits: Revisiting an already-queued key (e.g. "+p;-p", or inc followed by dec/inc) must segment the batch in jump_label.c before arch queueing, because x86 validates against live text and -1 keys are not 1 yet. jump_label_flush_key() flushes first whenever the target key is already in jump_label_queue[]. 3. Nesting and _cpuslocked() unification: Track batch_depth so only the outermost guard acquires and releases cpus_read_lock(), jump_label_mutex, and batch_owner and runs __jump_label_finish(). Checking batch_owner == current before cpus_read_lock() avoids recursive cpu_hotplug_lock lockdep warnings and self-deadlock when _cpuslocked() wrappers or batch guards nest. 4. Module loading and empty-batch elision: Wrap jump_label_module_notify() in static_key_batch so module-load pokes flush and coalesce, while jump_label_pokes_queued skips text_mutex and sync IPIs on MODULE_STATE_GOING, 0-site keys, or empty batches. Extend jump_label_test() to cover batching, reversals, and nesting. Suggested-by: Peter Zijlstra Signed-off-by: Jim Cromie --- include/linux/jump_label.h | 48 +++++-- kernel/jump_label.c | 316 ++++++++++++++++++++++++++++++++------------- 2 files changed, 267 insertions(+), 97 deletions(-) diff --git a/include/linux/jump_label.h b/include/linux/jump_label.h index c95350a10987..e7a8a7d4450c 100644 --- a/include/linux/jump_label.h +++ b/include/linux/jump_label.h @@ -222,15 +222,17 @@ extern int jump_label_text_reserved(void *start, void *end); 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(struct static_key *key) 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(struct static_key *key) 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,38 @@ static inline void static_key_disable(struct static_key *key) #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 bool static_key_slow_inc_cpuslocked(struct static_key *key) +{ + guard(static_key_batch_cpuslocked)(); + return 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); +} + /* -------------------------------------------------------------------------- */ /* diff --git a/kernel/jump_label.c b/kernel/jump_label.c index ab6b774bcfd7..fe70b251ba77 100644 --- a/kernel/jump_label.c +++ b/kernel/jump_label.c @@ -113,6 +113,111 @@ int static_key_count(struct static_key *key) } EXPORT_SYMBOL_GPL(static_key_count); +static void __jump_label_finish(void); +static void jump_label_flush_key(struct static_key *key); +static void jump_label_queue_key(struct static_key *key); + +static struct task_struct *batch_owner; +static unsigned int batch_depth; + +void static_key_batch_begin_cpuslocked(void) +{ + lockdep_assert_cpus_held(); + if (READ_ONCE(batch_owner) == current) { + lockdep_assert_held(&jump_label_mutex); + batch_depth++; + return; + } + + jump_label_lock(); + batch_depth = 1; + WRITE_ONCE(batch_owner, current); +} + +void static_key_batch_begin(void) +{ + if (READ_ONCE(batch_owner) == current) { + lockdep_assert_cpus_held(); + lockdep_assert_held(&jump_label_mutex); + batch_depth++; + return; + } + + cpus_read_lock(); + static_key_batch_begin_cpuslocked(); +} + +static bool __static_key_batch_end_cpuslocked(void) +{ + lockdep_assert_cpus_held(); + lockdep_assert_held(&jump_label_mutex); + if (WARN_ON_ONCE(READ_ONCE(batch_owner) != current)) + return false; + if (--batch_depth) + return false; + + __jump_label_finish(); + WRITE_ONCE(batch_owner, NULL); + jump_label_unlock(); + return true; +} + +void static_key_batch_end_cpuslocked(void) +{ + __static_key_batch_end_cpuslocked(); +} + +void static_key_batch_end(void) +{ + if (__static_key_batch_end_cpuslocked()) + cpus_read_unlock(); +} + +static void jump_label_batch_lock(void) +{ + if (READ_ONCE(batch_owner) == current) { + lockdep_assert_held(&jump_label_mutex); + return; + } + + jump_label_lock(); +} + +static void jump_label_batch_unlock(void) +{ + if (READ_ONCE(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 (READ_ONCE(batch_owner) == current) { + lockdep_assert_cpus_held(); + return; + } + + cpus_read_lock(); +} + +static void cpus_read_batch_unlock(void) +{ + if (READ_ONCE(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 +241,7 @@ bool static_key_fast_inc_not_disabled(struct static_key *key) * * 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 +253,8 @@ bool static_key_fast_inc_not_disabled(struct static_key *key) } 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,16 +265,12 @@ bool static_key_slow_inc_cpuslocked(struct static_key *key) 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)(); + jump_label_flush_key(key); /* Try to mark it as 'enabling in progress. */ if (!atomic_cmpxchg(&key->enabled, 0, -1)) { jump_label_update(key); - /* - * Ensure that when static_key_fast_inc_not_disabled() or - * static_key_dec_not_one() observe the positive value, - * they must also observe all the text changes. - */ - atomic_set_release(&key->enabled, 1); } else { /* * While holding the mutex this should never observe @@ -182,71 +281,45 @@ bool static_key_slow_inc_cpuslocked(struct static_key *key) } 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) { + int val; + STATIC_KEY_CHECK_USE(key); - lockdep_assert_cpus_held(); - if (atomic_read(&key->enabled) > 0) { - WARN_ON_ONCE(atomic_read(&key->enabled) != 1); + val = atomic_read(&key->enabled); + if (val > 0) { + WARN_ON_ONCE(val != 1); return; } - jump_label_lock(); + guard(cpus_read_batch_lock)(); + guard(jump_label_batch_lock)(); if (atomic_read(&key->enabled) == 0) { + jump_label_flush_key(key); atomic_set(&key->enabled, -1); jump_label_update(key); - /* - * See static_key_slow_inc(). - */ - 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) { + int val; + STATIC_KEY_CHECK_USE(key); - lockdep_assert_cpus_held(); - if (atomic_read(&key->enabled) != 1) { - WARN_ON_ONCE(atomic_read(&key->enabled) != 0); + val = atomic_read(&key->enabled); + if (val == 0 || WARN_ON_ONCE(val > 1)) return; - } - jump_label_lock(); + guard(cpus_read_batch_lock)(); + guard(jump_label_batch_lock)(); + jump_label_flush_key(key); 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 +333,7 @@ static bool static_key_dec_not_one(struct static_key *key) * 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. @@ -273,7 +346,7 @@ static bool static_key_dec_not_one(struct static_key *key) * people are trying to disable something that wasn't yet fully * enabled. This suggests an ordering problem on the user side. */ - WARN_ON_ONCE(v < 0); + WARN_ON_ONCE(v < 0 && READ_ONCE(batch_owner) != current); /* * Warn about underflow, and lie about success in an attempt to @@ -289,19 +362,22 @@ static bool static_key_dec_not_one(struct static_key *key) 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(mutex)(&jump_label_mutex); + guard(cpus_read_batch_lock)(); + guard(jump_label_batch_lock)(); + jump_label_flush_key(key); 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 +390,16 @@ static void __static_key_slow_dec_cpuslocked(struct static_key *key) 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 +546,15 @@ static bool jump_label_can_update(struct jump_entry *entry, bool init) } #ifndef HAVE_JUMP_LABEL_BATCH +static void __jump_label_finish(void) { } +static void jump_label_flush_key(struct static_key *key) { } + +static void jump_label_queue_key(struct static_key *key) +{ + if (atomic_read(&key->enabled) == -1) + atomic_set_release(&key->enabled, 1); +} + static void __jump_label_update(struct static_key *key, struct jump_entry *entry, struct jump_entry *stop, @@ -500,6 +566,47 @@ static void __jump_label_update(struct static_key *key, } } #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 bool jump_label_pokes_queued; + +static void __jump_label_finish(void) +{ + if (jump_label_pokes_queued) { + arch_jump_label_transform_apply(); + jump_label_pokes_queued = false; + } + 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_flush_key(struct static_key *key) +{ + for (unsigned int i = 0; i < jump_label_nr_queued; i++) { + if (jump_label_queue[i] == key) { + __jump_label_finish(); + return; + } + } +} + +static void jump_label_queue_key(struct static_key *key) +{ + if (jump_label_nr_queued >= JUMP_LABEL_BATCH_SIZE) + __jump_label_finish(); + jump_label_queue[jump_label_nr_queued++] = key; +} + static void __jump_label_update(struct static_key *key, struct jump_entry *entry, struct jump_entry *stop, @@ -514,11 +621,12 @@ static void __jump_label_update(struct static_key *key, /* * Queue is full: Apply the current queue and try again. */ - arch_jump_label_transform_apply(); + jump_label_pokes_queued = true; + __jump_label_finish(); BUG_ON(!arch_jump_label_transform_queue(entry, jump_label_type(entry))); } + jump_label_pokes_queued = true; } - arch_jump_label_transform_apply(); } #endif @@ -819,8 +927,7 @@ jump_label_module_notify(struct notifier_block *self, unsigned long val, struct module *mod = data; int ret = 0; - cpus_read_lock(); - jump_label_lock(); + guard(static_key_batch)(); switch (val) { case MODULE_STATE_COMING: @@ -835,9 +942,6 @@ jump_label_module_notify(struct notifier_block *self, unsigned long val, break; } - jump_label_unlock(); - cpus_read_unlock(); - return notifier_from_errno(ret); } @@ -892,6 +996,7 @@ static void jump_label_update(struct static_key *key) if (static_key_linked(key)) { __jump_label_mod_update(key); + jump_label_queue_key(key); return; } @@ -907,6 +1012,7 @@ static void jump_label_update(struct static_key *key) /* if there are no users, entry can be NULL */ if (entry) __jump_label_update(key, entry, stop, init); + jump_label_queue_key(key); } #ifdef CONFIG_STATIC_KEYS_SELFTEST @@ -939,6 +1045,40 @@ static __init int jump_label_test(void) static_branch_enable(&sk_true); static_branch_disable(&sk_false); + + scoped_guard(static_key_batch) { + static_branch_disable(&sk_true); + static_branch_enable(&sk_false); + } + + WARN_ON(static_key_enabled(&sk_true.key) == true); + WARN_ON(static_key_enabled(&sk_false.key) == false); + WARN_ON(static_branch_likely(&sk_true)); + WARN_ON(static_branch_unlikely(&sk_true)); + WARN_ON(!static_branch_likely(&sk_false)); + WARN_ON(!static_branch_unlikely(&sk_false)); + + /* Same-batch reversals, nesting, and inc/dec */ + scoped_guard(static_key_batch) { + static_branch_enable_cpuslocked(&sk_true); + scoped_guard(static_key_batch) { + static_branch_disable(&sk_true); + static_branch_enable(&sk_true); + } + + static_branch_disable(&sk_false); + static_branch_inc(&sk_false); + static_branch_inc(&sk_false); + static_branch_dec(&sk_false); + static_branch_dec(&sk_false); + } + + WARN_ON(static_key_enabled(&sk_true.key) != true); + WARN_ON(static_key_enabled(&sk_false.key) != false); + WARN_ON(!static_branch_likely(&sk_true)); + WARN_ON(!static_branch_unlikely(&sk_true)); + WARN_ON(static_branch_likely(&sk_false)); + WARN_ON(static_branch_unlikely(&sk_false)); } return 0; -- 2.56.1