xref: /linux/kernel/locking/mutex.h (revision 4b2bdc22210e39a02b3dc984cb8eb6b3293a56a7)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Mutexes: blocking mutual exclusion locks
4  *
5  * started by Ingo Molnar:
6  *
7  *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8  */
9 #ifndef CONFIG_PREEMPT_RT
10 #include <linux/mutex.h>
11 /*
12  * This is the control structure for tasks blocked on mutex, which resides
13  * on the blocked task's kernel stack:
14  */
15 struct mutex_waiter {
16 	struct list_head	list;
17 	struct task_struct	*task;
18 	struct ww_acquire_ctx	*ww_ctx;
19 #ifdef CONFIG_DEBUG_MUTEXES
20 	void			*magic;
21 #endif
22 };
23 
24 /*
25  * @owner: contains: 'struct task_struct *' to the current lock owner,
26  * NULL means not owned. Since task_struct pointers are aligned at
27  * at least L1_CACHE_BYTES, we have low bits to store extra state.
28  *
29  * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup.
30  * Bit1 indicates unlock needs to hand the lock to the top-waiter
31  * Bit2 indicates handoff has been done and we're waiting for pickup.
32  */
33 #define MUTEX_FLAG_WAITERS	0x01
34 #define MUTEX_FLAG_HANDOFF	0x02
35 #define MUTEX_FLAG_PICKUP	0x04
36 
37 #define MUTEX_FLAGS		0x07
38 
39 /*
40  * Internal helper function; C doesn't allow us to hide it :/
41  *
42  * DO NOT USE (outside of mutex & scheduler code).
43  */
44 static inline struct task_struct *__mutex_owner(struct mutex *lock)
45 {
46 	if (!lock)
47 		return NULL;
48 	return (struct task_struct *)(atomic_long_read(&lock->owner) & ~MUTEX_FLAGS);
49 }
50 
51 #ifdef CONFIG_DEBUG_MUTEXES
52 extern void debug_mutex_lock_common(struct mutex *lock,
53 				    struct mutex_waiter *waiter);
54 extern void debug_mutex_wake_waiter(struct mutex *lock,
55 				    struct mutex_waiter *waiter);
56 extern void debug_mutex_free_waiter(struct mutex_waiter *waiter);
57 extern void debug_mutex_add_waiter(struct mutex *lock,
58 				   struct mutex_waiter *waiter,
59 				   struct task_struct *task);
60 extern void debug_mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter,
61 				      struct task_struct *task);
62 extern void debug_mutex_unlock(struct mutex *lock);
63 extern void debug_mutex_init(struct mutex *lock);
64 #else /* CONFIG_DEBUG_MUTEXES */
65 # define debug_mutex_lock_common(lock, waiter)		do { } while (0)
66 # define debug_mutex_wake_waiter(lock, waiter)		do { } while (0)
67 # define debug_mutex_free_waiter(waiter)		do { } while (0)
68 # define debug_mutex_add_waiter(lock, waiter, ti)	do { } while (0)
69 # define debug_mutex_remove_waiter(lock, waiter, ti)	do { } while (0)
70 # define debug_mutex_unlock(lock)			do { } while (0)
71 # define debug_mutex_init(lock)				do { } while (0)
72 #endif /* !CONFIG_DEBUG_MUTEXES */
73 #endif /* CONFIG_PREEMPT_RT */
74