[PATCH 05/14] futex: Annotate __attach_to_pi_owner() and fixup_pi_*owner()

From: Bart Van Assche

Date: Mon Nov 02 2026 - 16:35:56 EST


Annotate __attach_to_pi_owner() with __must_hold(&p->pi_lock) and
annotate fixup_pi_state_owner() and fixup_pi_owner() with
__must_hold(q->lock_ptr).

__attach_to_pi_owner() modifies p->futex.pi_state_list and attaches a
new pi_state to owner task p, which requires holding p->pi_lock.
Enforce this requirement at compile time with __must_hold(&p->pi_lock).

__fixup_pi_state_owner() requires holding q->lock_ptr. Its caller
fixup_pi_state_owner() and outer caller fixup_pi_owner() must both be
called with the hash bucket lock held. Currently,
lockdep_assert_held(q->lock_ptr) in fixup_pi_state_owner() implicitly
assumes lock ownership for Clang context analysis. Prepare for removing
the context assumption from lockdep_assert_held() by annotating both
fixup_pi_state_owner() and fixup_pi_owner() with
__must_hold(q->lock_ptr).

Signed-off-by: Bart Van Assche <bvanassche@xxxxxxx>
---
kernel/futex/futex.h | 3 ++-
kernel/futex/pi.c | 3 +++
2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/kernel/futex/futex.h b/kernel/futex/futex.h
index f00f0863ed44..3e162837e9d2 100644
--- a/kernel/futex/futex.h
+++ b/kernel/futex/futex.h
@@ -394,7 +394,8 @@ extern int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
extern int refill_pi_state_cache(void);
extern void get_pi_state(struct futex_pi_state *pi_state);
extern void put_pi_state(struct futex_pi_state *pi_state);
-extern int fixup_pi_owner(u32 __user *uaddr, struct futex_q *q, int locked);
+extern int fixup_pi_owner(u32 __user *uaddr, struct futex_q *q, int locked)
+ __must_hold(q->lock_ptr);

/*
* Express the locking dependencies for lockdep:
diff --git a/kernel/futex/pi.c b/kernel/futex/pi.c
index 98f1b962e59a..e0310776b484 100644
--- a/kernel/futex/pi.c
+++ b/kernel/futex/pi.c
@@ -420,6 +420,7 @@ static int handle_exit_race(u32 __user *uaddr, u32 uval)

static void __attach_to_pi_owner(struct task_struct *p, union futex_key *key,
struct futex_pi_state **ps)
+ __must_hold(&p->pi_lock)
{
/*
* No existing pi state. First waiter. [2]
@@ -912,6 +913,7 @@ static int __fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,

static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
struct task_struct *argowner)
+ __must_hold(q->lock_ptr)
{
struct futex_pi_state *pi_state = q->pi_state;
int ret;
@@ -940,6 +942,7 @@ static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
* - <0 - on error (-EFAULT)
*/
int fixup_pi_owner(u32 __user *uaddr, struct futex_q *q, int locked)
+ __must_hold(q->lock_ptr)
{
if (locked) {
/*