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 static inline struct mutex *get_task_blocked_on(struct task_struct *p) 52 { 53 guard(raw_spinlock_irqsave)(&p->blocked_lock); 54 return __get_task_blocked_on(p); 55 } 56 57 #ifdef CONFIG_DEBUG_MUTEXES 58 extern void debug_mutex_lock_common(struct mutex *lock, 59 struct mutex_waiter *waiter); 60 extern void debug_mutex_wake_waiter(struct mutex *lock, 61 struct mutex_waiter *waiter); 62 extern void debug_mutex_free_waiter(struct mutex_waiter *waiter); 63 extern void debug_mutex_add_waiter(struct mutex *lock, 64 struct mutex_waiter *waiter, 65 struct task_struct *task); 66 extern void debug_mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter, 67 struct task_struct *task); 68 extern void debug_mutex_unlock(struct mutex *lock); 69 extern void debug_mutex_init(struct mutex *lock); 70 #else /* CONFIG_DEBUG_MUTEXES */ 71 # define debug_mutex_lock_common(lock, waiter) do { } while (0) 72 # define debug_mutex_wake_waiter(lock, waiter) do { } while (0) 73 # define debug_mutex_free_waiter(waiter) do { } while (0) 74 # define debug_mutex_add_waiter(lock, waiter, ti) do { } while (0) 75 # define debug_mutex_remove_waiter(lock, waiter, ti) do { } while (0) 76 # define debug_mutex_unlock(lock) do { } while (0) 77 # define debug_mutex_init(lock) do { } while (0) 78 #endif /* !CONFIG_DEBUG_MUTEXES */ 79 #endif /* CONFIG_PREEMPT_RT */ 80