xref: /linux-6.15/kernel/locking/mutex.h (revision 3a9320ec)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
201768b42SPeter Zijlstra /*
301768b42SPeter Zijlstra  * Mutexes: blocking mutual exclusion locks
401768b42SPeter Zijlstra  *
501768b42SPeter Zijlstra  * started by Ingo Molnar:
601768b42SPeter Zijlstra  *
701768b42SPeter Zijlstra  *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <[email protected]>
801768b42SPeter Zijlstra  */
901768b42SPeter Zijlstra 
1043d2d52dSThomas Gleixner /*
1143d2d52dSThomas Gleixner  * This is the control structure for tasks blocked on mutex, which resides
1243d2d52dSThomas Gleixner  * on the blocked task's kernel stack:
1343d2d52dSThomas Gleixner  */
1443d2d52dSThomas Gleixner struct mutex_waiter {
1543d2d52dSThomas Gleixner 	struct list_head	list;
1643d2d52dSThomas Gleixner 	struct task_struct	*task;
1743d2d52dSThomas Gleixner 	struct ww_acquire_ctx	*ww_ctx;
1843d2d52dSThomas Gleixner #ifdef CONFIG_DEBUG_MUTEXES
1943d2d52dSThomas Gleixner 	void			*magic;
2043d2d52dSThomas Gleixner #endif
2143d2d52dSThomas Gleixner };
2243d2d52dSThomas Gleixner 
23*3a9320ecSJuri Lelli /*
24*3a9320ecSJuri Lelli  * @owner: contains: 'struct task_struct *' to the current lock owner,
25*3a9320ecSJuri Lelli  * NULL means not owned. Since task_struct pointers are aligned at
26*3a9320ecSJuri Lelli  * at least L1_CACHE_BYTES, we have low bits to store extra state.
27*3a9320ecSJuri Lelli  *
28*3a9320ecSJuri Lelli  * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup.
29*3a9320ecSJuri Lelli  * Bit1 indicates unlock needs to hand the lock to the top-waiter
30*3a9320ecSJuri Lelli  * Bit2 indicates handoff has been done and we're waiting for pickup.
31*3a9320ecSJuri Lelli  */
32*3a9320ecSJuri Lelli #define MUTEX_FLAG_WAITERS	0x01
33*3a9320ecSJuri Lelli #define MUTEX_FLAG_HANDOFF	0x02
34*3a9320ecSJuri Lelli #define MUTEX_FLAG_PICKUP	0x04
35*3a9320ecSJuri Lelli 
36*3a9320ecSJuri Lelli #define MUTEX_FLAGS		0x07
37*3a9320ecSJuri Lelli 
38*3a9320ecSJuri Lelli /*
39*3a9320ecSJuri Lelli  * Internal helper function; C doesn't allow us to hide it :/
40*3a9320ecSJuri Lelli  *
41*3a9320ecSJuri Lelli  * DO NOT USE (outside of mutex & scheduler code).
42*3a9320ecSJuri Lelli  */
__mutex_owner(struct mutex * lock)43*3a9320ecSJuri Lelli static inline struct task_struct *__mutex_owner(struct mutex *lock)
44*3a9320ecSJuri Lelli {
45*3a9320ecSJuri Lelli 	if (!lock)
46*3a9320ecSJuri Lelli 		return NULL;
47*3a9320ecSJuri Lelli 	return (struct task_struct *)(atomic_long_read(&lock->owner) & ~MUTEX_FLAGS);
48*3a9320ecSJuri Lelli }
49*3a9320ecSJuri Lelli 
50a321fb90SThomas Gleixner #ifdef CONFIG_DEBUG_MUTEXES
51a321fb90SThomas Gleixner extern void debug_mutex_lock_common(struct mutex *lock,
52a321fb90SThomas Gleixner 				    struct mutex_waiter *waiter);
53a321fb90SThomas Gleixner extern void debug_mutex_wake_waiter(struct mutex *lock,
54a321fb90SThomas Gleixner 				    struct mutex_waiter *waiter);
55a321fb90SThomas Gleixner extern void debug_mutex_free_waiter(struct mutex_waiter *waiter);
56a321fb90SThomas Gleixner extern void debug_mutex_add_waiter(struct mutex *lock,
57a321fb90SThomas Gleixner 				   struct mutex_waiter *waiter,
58a321fb90SThomas Gleixner 				   struct task_struct *task);
59a321fb90SThomas Gleixner extern void debug_mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter,
60a321fb90SThomas Gleixner 				      struct task_struct *task);
61a321fb90SThomas Gleixner extern void debug_mutex_unlock(struct mutex *lock);
62a321fb90SThomas Gleixner extern void debug_mutex_init(struct mutex *lock, const char *name,
63a321fb90SThomas Gleixner 			     struct lock_class_key *key);
64a321fb90SThomas Gleixner #else /* CONFIG_DEBUG_MUTEXES */
65a321fb90SThomas Gleixner # define debug_mutex_lock_common(lock, waiter)		do { } while (0)
6601768b42SPeter Zijlstra # define debug_mutex_wake_waiter(lock, waiter)		do { } while (0)
6701768b42SPeter Zijlstra # define debug_mutex_free_waiter(waiter)		do { } while (0)
6801768b42SPeter Zijlstra # define debug_mutex_add_waiter(lock, waiter, ti)	do { } while (0)
693a010c49SZqiang # define debug_mutex_remove_waiter(lock, waiter, ti)	do { } while (0)
7001768b42SPeter Zijlstra # define debug_mutex_unlock(lock)			do { } while (0)
7101768b42SPeter Zijlstra # define debug_mutex_init(lock, name, key)		do { } while (0)
72a321fb90SThomas Gleixner #endif /* !CONFIG_DEBUG_MUTEXES */
73