Re: [PATCH] locking/rtmutex: Annotate API and implementation
From: Peter Zijlstra
Date: Mon May 04 2026 - 05:40:00 EST
On Fri, May 01, 2026 at 06:53:49PM -0700, Bart Van Assche wrote:
> Add lock context annotations to the rtmutex API and implementation and
> enable lock context analysis.
>
> Signed-off-by: Bart Van Assche <bvanassche@xxxxxxx>
This does not build, I need at least the below, but then PREEMPT_RT is
still not happy.
---
--- a/include/linux/rtmutex.h
+++ b/include/linux/rtmutex.h
@@ -20,7 +20,7 @@
extern int max_lock_depth;
-struct rt_mutex_base {
+context_lock_struct(rt_mutex_base) {
raw_spinlock_t wait_lock;
struct rb_root_cached waiters __guarded_by(&wait_lock);
struct task_struct *owner __guarded_by(&wait_lock);
--- a/kernel/locking/rtmutex.c
+++ b/kernel/locking/rtmutex.c
@@ -314,6 +314,7 @@ static __always_inline bool rt_mutex_cmp
static int __sched rt_mutex_slowtrylock(struct rt_mutex_base *lock);
static __always_inline bool rt_mutex_try_acquire(struct rt_mutex_base *lock)
+ __cond_acquires(true, lock)
{
/*
* With debug enabled rt_mutex_cmpxchg trylock() will always fail.
@@ -1095,6 +1096,7 @@ static int __sched
try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task,
struct rt_mutex_waiter *waiter)
__must_hold(&lock->wait_lock)
+ __cond_acquires(nonzero, lock)
{
lockdep_assert_held(&lock->wait_lock);
@@ -1625,6 +1627,7 @@ static int __sched rt_mutex_slowlock_blo
struct rt_mutex_waiter *waiter,
struct wake_q_head *wake_q)
__releases(&lock->wait_lock) __acquires(&lock->wait_lock)
+ __cond_acquires(0, lock)
{
struct rt_mutex *rtm = container_of(lock, struct rt_mutex, rtmutex);
struct task_struct *owner;
@@ -1715,6 +1718,7 @@ static int __sched __rt_mutex_slowlock(s
struct rt_mutex_waiter *waiter,
struct wake_q_head *wake_q)
__must_hold(&lock->wait_lock)
+ __cond_acquires(0, lock)
{
struct rt_mutex *rtm = container_of(lock, struct rt_mutex, rtmutex);
struct ww_mutex *ww = ww_container_of(rtm);
@@ -1773,6 +1777,7 @@ static inline int __rt_mutex_slowlock_lo
unsigned int state,
struct wake_q_head *wake_q)
__must_hold(&lock->wait_lock)
+ __cond_acquires(0, lock)
{
struct rt_mutex_waiter waiter;
int ret;
@@ -1797,6 +1802,7 @@ static inline int __rt_mutex_slowlock_lo
static int __sched rt_mutex_slowlock(struct rt_mutex_base *lock,
struct ww_acquire_ctx *ww_ctx,
unsigned int state)
+ __cond_acquires(0, lock)
{
DEFINE_WAKE_Q(wake_q);
unsigned long flags;
@@ -1829,6 +1835,7 @@ static int __sched rt_mutex_slowlock(str
static __always_inline int __rt_mutex_lock(struct rt_mutex_base *lock,
unsigned int state)
+ __cond_acquires(0, lock)
{
lockdep_assert(!current->pi_blocked_on);
--- a/kernel/locking/rtmutex_api.c
+++ b/kernel/locking/rtmutex_api.c
@@ -88,6 +88,7 @@ EXPORT_SYMBOL_GPL(_rt_mutex_lock_nest_lo
* @lock: the rt_mutex to be locked
*/
void __sched rt_mutex_lock(struct rt_mutex *lock)
+ __no_context_analysis /* ignoring the return value below is fine in this case */
{
__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, 0);
}
@@ -532,7 +533,7 @@ static __always_inline int __mutex_lock_
unsigned int subclass,
struct lockdep_map *nest_lock,
unsigned long ip)
- __acquires(lock)
+ __cond_acquires(0, lock)
{
int ret;