xref: /linux/kernel/locking/mutex.h (revision a514e6f8f5caa24413731bed54b322bd34d918dd)
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 
10 /*
11  * This is the control structure for tasks blocked on mutex, which resides
12  * on the blocked task's kernel stack:
13  */
14 struct mutex_waiter {
15 	struct list_head	list;
16 	struct task_struct	*task;
17 	struct ww_acquire_ctx	*ww_ctx;
18 #ifdef CONFIG_DEBUG_MUTEXES
19 	void			*magic;
20 #endif
21 };
22 
23 /*
24  * @owner: contains: 'struct task_struct *' to the current lock owner,
25  * NULL means not owned. Since task_struct pointers are aligned at
26  * at least L1_CACHE_BYTES, we have low bits to store extra state.
27  *
28  * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup.
29  * Bit1 indicates unlock needs to hand the lock to the top-waiter
30  * Bit2 indicates handoff has been done and we're waiting for pickup.
31  */
32 #define MUTEX_FLAG_WAITERS	0x01
33 #define MUTEX_FLAG_HANDOFF	0x02
34 #define MUTEX_FLAG_PICKUP	0x04
35 
36 #define MUTEX_FLAGS		0x07
37 
38 /*
39  * Internal helper function; C doesn't allow us to hide it :/
40  *
41  * DO NOT USE (outside of mutex & scheduler code).
42  */
43 static inline struct task_struct *__mutex_owner(struct mutex *lock)
44 {
45 	if (!lock)
46 		return NULL;
47 	return (struct task_struct *)(atomic_long_read(&lock->owner) & ~MUTEX_FLAGS);
48 }
49 
50 #ifdef CONFIG_DEBUG_MUTEXES
51 extern void debug_mutex_lock_common(struct mutex *lock,
52 				    struct mutex_waiter *waiter);
53 extern void debug_mutex_wake_waiter(struct mutex *lock,
54 				    struct mutex_waiter *waiter);
55 extern void debug_mutex_free_waiter(struct mutex_waiter *waiter);
56 extern void debug_mutex_add_waiter(struct mutex *lock,
57 				   struct mutex_waiter *waiter,
58 				   struct task_struct *task);
59 extern void debug_mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter,
60 				      struct task_struct *task);
61 extern void debug_mutex_unlock(struct mutex *lock);
62 extern void debug_mutex_init(struct mutex *lock, const char *name,
63 			     struct lock_class_key *key);
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, name, key)		do { } while (0)
72 #endif /* !CONFIG_DEBUG_MUTEXES */
73