xref: /linux-6.15/include/linux/mutex_types.h (revision d84f3179)
1*d84f3179SKent Overstreet /* SPDX-License-Identifier: GPL-2.0 */
2*d84f3179SKent Overstreet #ifndef __LINUX_MUTEX_TYPES_H
3*d84f3179SKent Overstreet #define __LINUX_MUTEX_TYPES_H
4*d84f3179SKent Overstreet 
5*d84f3179SKent Overstreet #include <linux/atomic.h>
6*d84f3179SKent Overstreet #include <linux/lockdep_types.h>
7*d84f3179SKent Overstreet #include <linux/osq_lock.h>
8*d84f3179SKent Overstreet #include <linux/spinlock_types.h>
9*d84f3179SKent Overstreet #include <linux/types.h>
10*d84f3179SKent Overstreet 
11*d84f3179SKent Overstreet #ifndef CONFIG_PREEMPT_RT
12*d84f3179SKent Overstreet 
13*d84f3179SKent Overstreet /*
14*d84f3179SKent Overstreet  * Simple, straightforward mutexes with strict semantics:
15*d84f3179SKent Overstreet  *
16*d84f3179SKent Overstreet  * - only one task can hold the mutex at a time
17*d84f3179SKent Overstreet  * - only the owner can unlock the mutex
18*d84f3179SKent Overstreet  * - multiple unlocks are not permitted
19*d84f3179SKent Overstreet  * - recursive locking is not permitted
20*d84f3179SKent Overstreet  * - a mutex object must be initialized via the API
21*d84f3179SKent Overstreet  * - a mutex object must not be initialized via memset or copying
22*d84f3179SKent Overstreet  * - task may not exit with mutex held
23*d84f3179SKent Overstreet  * - memory areas where held locks reside must not be freed
24*d84f3179SKent Overstreet  * - held mutexes must not be reinitialized
25*d84f3179SKent Overstreet  * - mutexes may not be used in hardware or software interrupt
26*d84f3179SKent Overstreet  *   contexts such as tasklets and timers
27*d84f3179SKent Overstreet  *
28*d84f3179SKent Overstreet  * These semantics are fully enforced when DEBUG_MUTEXES is
29*d84f3179SKent Overstreet  * enabled. Furthermore, besides enforcing the above rules, the mutex
30*d84f3179SKent Overstreet  * debugging code also implements a number of additional features
31*d84f3179SKent Overstreet  * that make lock debugging easier and faster:
32*d84f3179SKent Overstreet  *
33*d84f3179SKent Overstreet  * - uses symbolic names of mutexes, whenever they are printed in debug output
34*d84f3179SKent Overstreet  * - point-of-acquire tracking, symbolic lookup of function names
35*d84f3179SKent Overstreet  * - list of all locks held in the system, printout of them
36*d84f3179SKent Overstreet  * - owner tracking
37*d84f3179SKent Overstreet  * - detects self-recursing locks and prints out all relevant info
38*d84f3179SKent Overstreet  * - detects multi-task circular deadlocks and prints out all affected
39*d84f3179SKent Overstreet  *   locks and tasks (and only those tasks)
40*d84f3179SKent Overstreet  */
41*d84f3179SKent Overstreet struct mutex {
42*d84f3179SKent Overstreet 	atomic_long_t		owner;
43*d84f3179SKent Overstreet 	raw_spinlock_t		wait_lock;
44*d84f3179SKent Overstreet #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
45*d84f3179SKent Overstreet 	struct optimistic_spin_queue osq; /* Spinner MCS lock */
46*d84f3179SKent Overstreet #endif
47*d84f3179SKent Overstreet 	struct list_head	wait_list;
48*d84f3179SKent Overstreet #ifdef CONFIG_DEBUG_MUTEXES
49*d84f3179SKent Overstreet 	void			*magic;
50*d84f3179SKent Overstreet #endif
51*d84f3179SKent Overstreet #ifdef CONFIG_DEBUG_LOCK_ALLOC
52*d84f3179SKent Overstreet 	struct lockdep_map	dep_map;
53*d84f3179SKent Overstreet #endif
54*d84f3179SKent Overstreet };
55*d84f3179SKent Overstreet 
56*d84f3179SKent Overstreet #else /* !CONFIG_PREEMPT_RT */
57*d84f3179SKent Overstreet /*
58*d84f3179SKent Overstreet  * Preempt-RT variant based on rtmutexes.
59*d84f3179SKent Overstreet  */
60*d84f3179SKent Overstreet #include <linux/rtmutex.h>
61*d84f3179SKent Overstreet 
62*d84f3179SKent Overstreet struct mutex {
63*d84f3179SKent Overstreet 	struct rt_mutex_base	rtmutex;
64*d84f3179SKent Overstreet #ifdef CONFIG_DEBUG_LOCK_ALLOC
65*d84f3179SKent Overstreet 	struct lockdep_map	dep_map;
66*d84f3179SKent Overstreet #endif
67*d84f3179SKent Overstreet };
68*d84f3179SKent Overstreet 
69*d84f3179SKent Overstreet #endif /* CONFIG_PREEMPT_RT */
70*d84f3179SKent Overstreet 
71*d84f3179SKent Overstreet #endif /* __LINUX_MUTEX_TYPES_H */
72