Locking changes for v6.17
Subsystems (6)
Hi Ingo & Peter,
Please pull the lockdep changes for v6.17 into tip. One thing to notice is that I would like to add Breno Leitao's workaround in lockdep_unregister_key(), since the alternative requires hazptr and I'm not near to get it done. I added comments explaining this and left a TODO there, and I will replace it once the shazptr patch is in a good shape. Peter, if you have any concern about it, either we can drop the patch #9 or I can create another tag. Thanks!
The following changes since commit 7ff495e26a39f3e7a3d4058df59b5b6d6f943cab:
local_lock: Move this_cpu_ptr() notation from internal to main header (2025-06-30 17:45:35 +0200)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/boqun/linux.git tags/lockdep-for-tip.2025.07.16
for you to fetch changes up to 7a3cedafccf8e7d038ad4cfec5b38052647ceac5:
lockdep: Speed up lockdep_unregister_key() with expedited RCU synchronization (2025-07-14 21:57:29 -0700)
----------------------------------------------------------------
Locking changes for v6.17:
- General
- Mark devm_mutex_init() as __must_check
- Add #[must_use] to Lock::try_lock()
- Remove OWNER_SPINNABLE in rwsem
- Remove redundant #ifdefs in mutex
- Lockdep
- Avoid returning struct in lock_stats()
- Change static const into enum for LOCKF_*_IRQ_*
- Temporarily use synchronize_rcu_expedited() in lockdep_unregister_key() to speed things up.
----------------------------------------------------------------
Regards,
Boqun
Arnd Bergmann (2):
locking/lockdep: Avoid struct return in lock_stats() locking/lockdep: Change 'static const' variables to enum values
Breno Leitao (1):
lockdep: Speed up lockdep_unregister_key() with expedited RCU synchronization
Jason Devers (1):
rust: sync: Add #[must_use] to Lock::try_lock()
Jinliang Zheng (1):
locking/rwsem: Use OWNER_NONSPINNABLE directly instead of OWNER_SPINNABLE
Ran Xiaokai (1):
locking/mutex: Remove redundant #ifdefs
Thomas Weißschuh (3):
spi: spi-nxp-fspi: Check return value of devm_mutex_init()
leds: lp8860: Check return value of devm_mutex_init() locking/mutex: Mark devm_mutex_init() as __must_check
drivers/leds/leds-lp8860.c | 4 +++- drivers/spi/spi-nxp-fspi.c | 4 +++- include/linux/lockdep_types.h | 2 +- include/linux/mutex.h | 11 +++++++---- kernel/locking/lockdep.c | 39 +++++++++++++++++++++----------------- kernel/locking/lockdep_internals.h | 18 ++++++++++-------- kernel/locking/lockdep_proc.c | 2 +- kernel/locking/mutex.c | 4 ---- kernel/locking/rwsem.c | 4 +--- rust/kernel/sync/lock.rs | 2 ++ 10 files changed, 50 insertions(+), 40 deletions(-)
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH 1/9 spi: spi-nxp-fspi: Check return value of devm_mutex_init()
From: Thomas Weißschuh <[email protected]>
devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.
Add the missed check.
--- drivers/spi/spi-nxp-fspi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/spi/spi-nxp-fspi.c +3 -1
@@ -1273,7 +1273,9 @@ static int nxp_fspi_probe(struct platform_device *pdev)if (ret)return dev_err_probe (dev, ret,"Failed to request irq\n" ); -devm_mutex_init (dev, &f->lock); + ret =devm_mutex_init (dev, &f->lock); +if (ret) +return dev_err_probe (dev, ret,"Failed to initialize lock\n" ); ctlr->bus_num =-1 ; ctlr->num_chipselect =NXP_FSPI_MAX_CHIPSELECT ;
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH 2/9 leds: lp8860: Check return value of devm_mutex_init()
In reply to Boqun FengFrom: Thomas Weißschuh <[email protected]>
devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.
Add the missed check.
--- drivers/leds/leds-lp8860.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/leds/leds-lp8860.c +3 -1
@@ -307,7 +307,9 @@ static int lp8860_probe(struct i2c_client *client) led->client = client; led->led_dev.brightness_set_blocking = lp8860_brightness_set; -devm_mutex_init (&client->dev, &led->lock); + ret =devm_mutex_init (&client->dev, &led->lock); +if (ret) +return dev_err_probe (&client->dev, ret,"Failed to initialize lock\n" ); led->regmap =devm_regmap_init_i2c (client, &lp8860_regmap_config);if (IS_ERR (led->regmap)) {
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH 3/9 locking/mutex: Mark devm_mutex_init() as __must_check
In reply to Boqun FengFrom: Thomas Weißschuh <[email protected]>
devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.
Enforce all callers check the return value through the compiler.
As devm_mutex_init() itself is a macro, it can not be annotated directly. Annotate __devm_mutex_init() instead. Unfortunately __must_check/warn_unused_result don't propagate through statement expression. So move the statement expression into the argument list of the call to __devm_mutex_init() through a helper macro.
--- include/linux/mutex.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
include/linux/mutex.h +7 -4
@@ -126,11 +126,11 @@ do { \#ifdef CONFIG_DEBUG_MUTEXES -int __devm_mutex_init (struct device *dev,struct mutex *lock); +int __must_check __devm_mutex_init (struct device *dev,struct mutex *lock);#else -static inline int __devm_mutex_init (struct device *dev,struct mutex *lock) +static inline int __must_check __devm_mutex_init (struct device *dev,struct mutex *lock) {/* * When CONFIG_DEBUG_MUTEXES is off mutex_destroy() is just a nop so
@@ -141,14 +141,17 @@ static inline int __devm_mutex_init(struct device *dev, struct mutex *lock)#endif -#define devm_mutex_init (dev, mutex) \ +#define __mutex_init_ret (mutex) \ ({ \ typeof(mutex) mutex_ = (mutex); \ \ mutex_init(mutex_); \ - __devm_mutex_init(dev, mutex_); \ + mutex_; \ }) +#define devm_mutex_init (dev, mutex) \ + __devm_mutex_init(dev, __mutex_init_ret(mutex)) +/* * See kernel/locking/mutex.c for detailed documentation of these APIs. * Also see Documentation/locking/mutex-design.rst.
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH 4/9 rust: sync: Add #[must_use] to Lock::try_lock()
In reply to Boqun FengFrom: Jason Devers <[email protected]>
The Lock::try_lock() function returns an Option<Guard<...>>, but it currently does not issue a warning if the return value is unused. To avoid potential bugs, the #[must_use] annotation is added to ensure proper usage.
Note that T is #[must_use] but Option<T> is not.
For more context, see: https://github.com/rust-lang/rust/issues/71368.
--- rust/kernel/sync/lock.rs | 2 ++ 1 file changed, 2 insertions(+)
rust/kernel/sync/lock.rs +2 -0
@@ -175,6 +175,8 @@ pub fn lock(&self) -> Guard<'_, T, B> {/// Tries to acquire the lock. /// /// Returns a guard that can be used to access the data protected by the lock if successful. +// `Option<T>` is not `#[must_use]` even if `T` is, thus the attribute is needed here. +#[must_use = "if unused, the lock will be immediately unlocked" ] pub fn try_lock (&self) ->Option <Guard <'_,T ,B >> {// SAFETY: The constructor of the type calls `init`, so the existence of the object proves // that `init` was called.
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH 5/9 locking/rwsem: Use OWNER_NONSPINNABLE directly instead of OWNER_SPINNABLE
In reply to Boqun FengFrom: Jinliang Zheng <[email protected]>
After commit 7d43f1ce9dd0 ("locking/rwsem: Enable time-based spinning on reader-owned rwsem"), OWNER_SPINNABLE contains all possible values except OWNER_NONSPINNABLE, namely OWNER_NULL | OWNER_WRITER | OWNER_READER.
Therefore, it is better to use OWNER_NONSPINNABLE directly to determine whether to exit optimistic spin.
And, remove useless OWNER_SPINNABLE to simplify the code.
--- kernel/locking/rwsem.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/locking/rwsem.c +1 -3
@@ -727,8 +727,6 @@ static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem)return ret; } -#define OWNER_SPINNABLE (OWNER_NULL | OWNER_WRITER | OWNER_READER) -static inline enum owner_state rwsem_owner_state (struct task_struct *owner,unsigned long flags) {
@@ -835,7 +833,7 @@ static bool rwsem_optimistic_spin(struct rw_semaphore *sem)enum owner_state owner_state; owner_state =rwsem_spin_on_owner (sem); -if (!(owner_state &OWNER_SPINNABLE )) +if (owner_state ==OWNER_NONSPINNABLE )break ;/*
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH 6/9 locking/lockdep: Avoid struct return in lock_stats()
In reply to Boqun FengFrom: Arnd Bergmann <[email protected]>
Returning a large structure from the lock_stats() function causes clang to have multiple copies of it on the stack and copy between them, which can end up exceeding the frame size warning limit:
kernel/locking/lockdep.c:300:25: error: stack frame size (1464) exceeds limit (1280) in 'lock_stats' [-Werror,-Wframe-larger-than]
300 | struct lock_class_stats lock_stats(struct lock_class *class)
Change the calling conventions to directly operate on the caller's copy, which apparently is what gcc does already.
--- include/linux/lockdep_types.h | 2 +- kernel/locking/lockdep.c | 27 ++++++++++++--------------- kernel/locking/lockdep_proc.c | 2 +- 3 files changed, 14 insertions(+), 17 deletions(-)
include/linux/lockdep_types.h +1 -1
@@ -175,7 +175,7 @@ struct lock_class_stats { unsigned long bounces[nr_bounce_types]; }; -struct lock_class_stats lock_stats (struct lock_class *class); +void lock_stats (struct lock_class *class,struct lock_class_stats *stats);void clear_lock_stats (struct lock_class *class);#endif
kernel/locking/lockdep.c +12 -15
@@ -297,33 +297,30 @@ static inline void lock_time_add(struct lock_time *src, struct lock_time *dst) dst->nr += src->nr; } -struct lock_class_stats lock_stats (struct lock_class *class) +void lock_stats (struct lock_class *class,struct lock_class_stats *stats) { -struct lock_class_stats stats;int cpu, i; -memset (&stats,0 ,sizeof (struct lock_class_stats )); +memset (stats,0 ,sizeof (struct lock_class_stats ));for_each_possible_cpu (cpu) {struct lock_class_stats *pcs = &per_cpu (cpu_lock_stats, cpu)[class - lock_classes]; -for (i =0 ; i <ARRAY_SIZE (stats.contention_point); i++) - stats.contention_point[i] += pcs->contention_point[i]; +for (i =0 ; i <ARRAY_SIZE (stats->contention_point); i++) + stats->contention_point[i] += pcs->contention_point[i]; -for (i =0 ; i <ARRAY_SIZE (stats.contending_point); i++) - stats.contending_point[i] += pcs->contending_point[i]; +for (i =0 ; i <ARRAY_SIZE (stats->contending_point); i++) + stats->contending_point[i] += pcs->contending_point[i]; -lock_time_add (&pcs->read_waittime, &stats.read_waittime); -lock_time_add (&pcs->write_waittime, &stats.write_waittime); +lock_time_add (&pcs->read_waittime, &stats->read_waittime); +lock_time_add (&pcs->write_waittime, &stats->write_waittime); -lock_time_add (&pcs->read_holdtime, &stats.read_holdtime); -lock_time_add (&pcs->write_holdtime, &stats.write_holdtime); +lock_time_add (&pcs->read_holdtime, &stats->read_holdtime); +lock_time_add (&pcs->write_holdtime, &stats->write_holdtime); -for (i =0 ; i <ARRAY_SIZE (stats.bounces); i++) - stats.bounces[i] += pcs->bounces[i]; +for (i =0 ; i <ARRAY_SIZE (stats->bounces); i++) + stats->bounces[i] += pcs->bounces[i]; } - -return stats; }void clear_lock_stats (struct lock_class *class)
kernel/locking/lockdep_proc.c +1 -1
@@ -657,7 +657,7 @@ static int lock_stat_open(struct inode *inode, struct file *file)if (!test_bit (idx, lock_classes_in_use))continue ; iter->class = class; - iter->stats =lock_stats (class); +lock_stats (class, &iter->stats); iter++; }
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH 7/9 locking/lockdep: Change 'static const' variables to enum values
In reply to Boqun FengFrom: Arnd Bergmann <[email protected]>
gcc warns about 'static const' variables even in headers when building with -Wunused-const-variables enabled:
In file included from kernel/locking/lockdep_proc.c:25:kernel/locking/lockdep_internals.h:69:28: error: 'LOCKF_USED_IN_IRQ_READ' defined but not used [-Werror=unused-const-variable=]
69 | static const unsigned long LOCKF_USED_IN_IRQ_READ =
| ^~~~~~~~~~~~~~~~~~~~~~
kernel/locking/lockdep_internals.h:63:28: error: 'LOCKF_ENABLED_IRQ_READ' defined but not used [-Werror=unused-const-variable=]
63 | static const unsigned long LOCKF_ENABLED_IRQ_READ =
| ^~~~~~~~~~~~~~~~~~~~~~
kernel/locking/lockdep_internals.h:57:28: error: 'LOCKF_USED_IN_IRQ' defined but not used [-Werror=unused-const-variable=]
57 | static const unsigned long LOCKF_USED_IN_IRQ =
| ^~~~~~~~~~~~~~~~~
kernel/locking/lockdep_internals.h:51:28: error: 'LOCKF_ENABLED_IRQ' defined but not used [-Werror=unused-const-variable=]
51 | static const unsigned long LOCKF_ENABLED_IRQ =
| ^~~~~~~~~~~~~~~~~This one is easy to avoid by changing the generated constant definition into an equivalent enum.
--- kernel/locking/lockdep_internals.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
kernel/locking/lockdep_internals.h +10 -8
@@ -47,29 +47,31 @@ enum {__LOCKF (USED_READ) }; +enum {#define LOCKDEP_STATE(__STATE) LOCKF_ENABLED_##__STATE | -static const unsigned long LOCKF_ENABLED_IRQ = + LOCKF_ENABLED_IRQ = #include "lockdep_states.h" -0 ; +0 , #undef LOCKDEP_STATE #define LOCKDEP_STATE(__STATE) LOCKF_USED_IN_##__STATE | -static const unsigned long LOCKF_USED_IN_IRQ = +LOCKF_USED_IN_IRQ = #include "lockdep_states.h" -0 ; +0 , #undef LOCKDEP_STATE #define LOCKDEP_STATE(__STATE) LOCKF_ENABLED_##__STATE##_READ | -static const unsigned long LOCKF_ENABLED_IRQ_READ = +LOCKF_ENABLED_IRQ_READ = #include "lockdep_states.h" -0 ; +0 , #undef LOCKDEP_STATE #define LOCKDEP_STATE(__STATE) LOCKF_USED_IN_##__STATE##_READ | -static const unsigned long LOCKF_USED_IN_IRQ_READ = +LOCKF_USED_IN_IRQ_READ = #include "lockdep_states.h" -0 ; +0 ,#undef LOCKDEP_STATE +};#define LOCKF_ENABLED_IRQ_ALL (LOCKF_ENABLED_IRQ | LOCKF_ENABLED_IRQ_READ)#define LOCKF_USED_IN_IRQ_ALL (LOCKF_USED_IN_IRQ | LOCKF_USED_IN_IRQ_READ)
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH 8/9 locking/mutex: Remove redundant #ifdefs
In reply to Boqun FengFrom: Ran Xiaokai <[email protected]>
hung_task_{set,clear}_blocker() is already guarded by CONFIG_DETECT_HUNG_TASK_BLOCKER in hung_task.h, So remove the redudant check of #ifdef.
--- kernel/locking/mutex.c | 4 ---- 1 file changed, 4 deletions(-)
kernel/locking/mutex.c +0 -4
@@ -191,9 +191,7 @@ static void __mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter, struct list_head *list) { -#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKERhung_task_set_blocker (lock, BLOCKER_TYPE_MUTEX); -#endif debug_mutex_add_waiter (lock, waiter, current);list_add_tail (&waiter->list, list);
@@ -209,9 +207,7 @@ __mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter)__mutex_clear_flag (lock,MUTEX_FLAGS );debug_mutex_remove_waiter (lock, waiter, current); -#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER hung_task_clear_blocker (); -#endif }/*
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH 9/9 lockdep: Speed up lockdep_unregister_key() with expedited RCU synchronization
In reply to Boqun FengFrom: Breno Leitao <[email protected]>
lockdep_unregister_key() is called from critical code paths, including sections where rtnl_lock() is held. For example, when replacing a qdisc in a network device, network egress traffic is disabled while __qdisc_destroy() is called for every network queue.
If lockdep is enabled, __qdisc_destroy() calls lockdep_unregister_key(), which gets blocked waiting for synchronize_rcu() to complete.
For example, a simple tc command to replace a qdisc could take 13 seconds:
# time /usr/sbin/tc qdisc replace dev eth0 root handle 0x1: mq
real 0m13.195s
user 0m0.001s
sys 0m2.746sDuring this time, network egress is completely frozen while waiting for RCU synchronization.
Use synchronize_rcu_expedited() instead to minimize the impact on critical operations like network connectivity changes.
This improves 10x the function call to tc, when replacing the qdisc for a network card.
# time /usr/sbin/tc qdisc replace dev eth0 root handle 0x1: mq
real 0m1.789s
user 0m0.000s
sys 0m1.613s[boqun: Fixed the comment and add more information for the temporary workaround, and add TODO information for hazptr]
--- kernel/locking/lockdep.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
kernel/locking/lockdep.c +10 -2
@@ -6616,8 +6616,16 @@ void lockdep_unregister_key(struct lock_class_key *key)if (need_callback)call_rcu (&delayed_free.rcu_head, free_zapped_rcu); -/* Wait until is_dynamic_key() has finished accessing k->hash_entry. */ -synchronize_rcu (); +/* +* Wait until is_dynamic_key() has finished accessing k->hash_entry. +* +* Some operations like __qdisc_destroy() will call this in a debug +* kernel, and the network traffic is disabled while waiting, hence +* the delay of the wait matters in debugging cases. Currently use a +* synchronize_rcu_expedited() to speed up the wait at the cost of +* system IPIs. TODO: Replace RCU with hazptr for this. +*/ +synchronize_rcu_expedited (); }EXPORT_SYMBOL_GPL (lockdep_unregister_key);
Signature · 1 line
2.39.5 (Apple Git-154)