Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 2 | /* |
| 3 | * RT Mutexes: blocking mutual exclusion locks with PI support |
| 4 | * |
| 5 | * started by Ingo Molnar and Thomas Gleixner: |
| 6 | * |
| 7 | * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> |
| 8 | * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com> |
| 9 | * |
| 10 | * This file contains the private data structure and API definitions. |
| 11 | */ |
| 12 | |
| 13 | #ifndef __KERNEL_RTMUTEX_COMMON_H |
| 14 | #define __KERNEL_RTMUTEX_COMMON_H |
| 15 | |
| 16 | #include <linux/rtmutex.h> |
Ingo Molnar | 84f001e | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 17 | #include <linux/sched/wake_q.h> |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 18 | |
| 19 | /* |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 20 | * This is the control structure for tasks blocked on a rt_mutex, |
| 21 | * which is allocated on the kernel stack on of the blocked task. |
| 22 | * |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 23 | * @tree_entry: pi node to enqueue into the mutex waiters tree |
| 24 | * @pi_tree_entry: pi node to enqueue into the mutex owner waiters tree |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 25 | * @task: task reference to the blocked task |
| 26 | */ |
| 27 | struct rt_mutex_waiter { |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 28 | struct rb_node tree_entry; |
| 29 | struct rb_node pi_tree_entry; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 30 | struct task_struct *task; |
| 31 | struct rt_mutex *lock; |
| 32 | #ifdef CONFIG_DEBUG_RT_MUTEXES |
| 33 | unsigned long ip; |
Pavel Emelyanov | 48d13e4 | 2008-02-08 04:21:53 -0800 | [diff] [blame] | 34 | struct pid *deadlock_task_pid; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 35 | struct rt_mutex *deadlock_lock; |
| 36 | #endif |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 37 | int prio; |
Peter Zijlstra | e0aad5b | 2017-03-23 15:56:13 +0100 | [diff] [blame] | 38 | u64 deadline; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | /* |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 42 | * Various helpers to access the waiters-tree: |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 43 | */ |
Nicolas Pitre | bc2eecd | 2017-08-01 00:31:32 -0400 | [diff] [blame] | 44 | |
| 45 | #ifdef CONFIG_RT_MUTEXES |
| 46 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 47 | static inline int rt_mutex_has_waiters(struct rt_mutex *lock) |
| 48 | { |
Davidlohr Bueso | a23ba90 | 2017-09-08 16:15:01 -0700 | [diff] [blame] | 49 | return !RB_EMPTY_ROOT(&lock->waiters.rb_root); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static inline struct rt_mutex_waiter * |
| 53 | rt_mutex_top_waiter(struct rt_mutex *lock) |
| 54 | { |
| 55 | struct rt_mutex_waiter *w; |
| 56 | |
Davidlohr Bueso | a23ba90 | 2017-09-08 16:15:01 -0700 | [diff] [blame] | 57 | w = rb_entry(lock->waiters.rb_leftmost, |
| 58 | struct rt_mutex_waiter, tree_entry); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 59 | BUG_ON(w->lock != lock); |
| 60 | |
| 61 | return w; |
| 62 | } |
| 63 | |
| 64 | static inline int task_has_pi_waiters(struct task_struct *p) |
| 65 | { |
Davidlohr Bueso | a23ba90 | 2017-09-08 16:15:01 -0700 | [diff] [blame] | 66 | return !RB_EMPTY_ROOT(&p->pi_waiters.rb_root); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static inline struct rt_mutex_waiter * |
| 70 | task_top_pi_waiter(struct task_struct *p) |
| 71 | { |
Davidlohr Bueso | a23ba90 | 2017-09-08 16:15:01 -0700 | [diff] [blame] | 72 | return rb_entry(p->pi_waiters.rb_leftmost, |
| 73 | struct rt_mutex_waiter, pi_tree_entry); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Nicolas Pitre | bc2eecd | 2017-08-01 00:31:32 -0400 | [diff] [blame] | 76 | #else |
| 77 | |
| 78 | static inline int rt_mutex_has_waiters(struct rt_mutex *lock) |
| 79 | { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | static inline struct rt_mutex_waiter * |
| 84 | rt_mutex_top_waiter(struct rt_mutex *lock) |
| 85 | { |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | static inline int task_has_pi_waiters(struct task_struct *p) |
| 90 | { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | static inline struct rt_mutex_waiter * |
| 95 | task_top_pi_waiter(struct task_struct *p) |
| 96 | { |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | #endif |
| 101 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 102 | /* |
| 103 | * lock->owner state tracking: |
| 104 | */ |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 105 | #define RT_MUTEX_HAS_WAITERS 1UL |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 106 | |
| 107 | static inline struct task_struct *rt_mutex_owner(struct rt_mutex *lock) |
| 108 | { |
Thomas Gleixner | 1be5d4f | 2016-11-30 21:04:42 +0000 | [diff] [blame] | 109 | unsigned long owner = (unsigned long) READ_ONCE(lock->owner); |
| 110 | |
Thomas Gleixner | b5016e8 | 2016-11-30 21:04:44 +0000 | [diff] [blame] | 111 | return (struct task_struct *) (owner & ~RT_MUTEX_HAS_WAITERS); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 114 | /* |
Thomas Gleixner | 8930ed8 | 2014-05-22 03:25:47 +0000 | [diff] [blame] | 115 | * Constants for rt mutex functions which have a selectable deadlock |
| 116 | * detection. |
| 117 | * |
| 118 | * RT_MUTEX_MIN_CHAINWALK: Stops the lock chain walk when there are |
| 119 | * no further PI adjustments to be made. |
| 120 | * |
| 121 | * RT_MUTEX_FULL_CHAINWALK: Invoke deadlock detection with a full |
| 122 | * walk of the lock chain. |
| 123 | */ |
| 124 | enum rtmutex_chainwalk { |
| 125 | RT_MUTEX_MIN_CHAINWALK, |
| 126 | RT_MUTEX_FULL_CHAINWALK, |
| 127 | }; |
| 128 | |
| 129 | /* |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 130 | * PI-futex support (proxy locking functions, etc.): |
| 131 | */ |
| 132 | extern struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock); |
| 133 | extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock, |
| 134 | struct task_struct *proxy_owner); |
| 135 | extern void rt_mutex_proxy_unlock(struct rt_mutex *lock, |
| 136 | struct task_struct *proxy_owner); |
Peter Zijlstra | 5080935 | 2017-03-22 11:35:56 +0100 | [diff] [blame] | 137 | extern void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter); |
Peter Zijlstra | 56222b2 | 2017-03-22 11:36:00 +0100 | [diff] [blame] | 138 | extern int __rt_mutex_start_proxy_lock(struct rt_mutex *lock, |
| 139 | struct rt_mutex_waiter *waiter, |
| 140 | struct task_struct *task); |
Darren Hart | 8dac456 | 2009-04-03 13:40:12 -0700 | [diff] [blame] | 141 | extern int rt_mutex_start_proxy_lock(struct rt_mutex *lock, |
| 142 | struct rt_mutex_waiter *waiter, |
Thomas Gleixner | c051b21 | 2014-05-22 03:25:50 +0000 | [diff] [blame] | 143 | struct task_struct *task); |
Peter Zijlstra | 38d589f | 2017-03-22 11:35:57 +0100 | [diff] [blame] | 144 | extern int rt_mutex_wait_proxy_lock(struct rt_mutex *lock, |
| 145 | struct hrtimer_sleeper *to, |
| 146 | struct rt_mutex_waiter *waiter); |
| 147 | extern bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock, |
| 148 | struct rt_mutex_waiter *waiter); |
Peter Zijlstra | 5293c2e | 2017-03-22 11:35:51 +0100 | [diff] [blame] | 149 | |
Peter Zijlstra | 5293c2e | 2017-03-22 11:35:51 +0100 | [diff] [blame] | 150 | extern int rt_mutex_futex_trylock(struct rt_mutex *l); |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 151 | extern int __rt_mutex_futex_trylock(struct rt_mutex *l); |
Peter Zijlstra | 5293c2e | 2017-03-22 11:35:51 +0100 | [diff] [blame] | 152 | |
| 153 | extern void rt_mutex_futex_unlock(struct rt_mutex *lock); |
| 154 | extern bool __rt_mutex_futex_unlock(struct rt_mutex *lock, |
| 155 | struct wake_q_head *wqh); |
| 156 | |
Peter Zijlstra | aa2bfe5 | 2017-03-23 15:56:10 +0100 | [diff] [blame] | 157 | extern void rt_mutex_postunlock(struct wake_q_head *wake_q); |
Thomas Gleixner | 36cf3b5 | 2007-07-15 23:41:20 -0700 | [diff] [blame] | 158 | |
| 159 | #ifdef CONFIG_DEBUG_RT_MUTEXES |
| 160 | # include "rtmutex-debug.h" |
| 161 | #else |
| 162 | # include "rtmutex.h" |
| 163 | #endif |
| 164 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 165 | #endif |