8596a71774e0d0ec9b6a4c0a8a1edda56d6de86e
[linux-2.6-microblaze.git] / kernel / locking / rtmutex_common.h
1 /* SPDX-License-Identifier: GPL-2.0 */
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>
17 #include <linux/sched/wake_q.h>
18
19 /*
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  *
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
25  * @task:               task reference to the blocked task
26  * @lock:               Pointer to the rt_mutex on which the waiter blocks
27  * @prio:               Priority of the waiter
28  * @deadline:           Deadline of the waiter if applicable
29  */
30 struct rt_mutex_waiter {
31         struct rb_node          tree_entry;
32         struct rb_node          pi_tree_entry;
33         struct task_struct      *task;
34         struct rt_mutex         *lock;
35         int                     prio;
36         u64                     deadline;
37 };
38
39 /*
40  * Must be guarded because this header is included from rcu/tree_plugin.h
41  * unconditionally.
42  */
43 #ifdef CONFIG_RT_MUTEXES
44 static inline int rt_mutex_has_waiters(struct rt_mutex *lock)
45 {
46         return !RB_EMPTY_ROOT(&lock->waiters.rb_root);
47 }
48
49 static inline struct rt_mutex_waiter *rt_mutex_top_waiter(struct rt_mutex *lock)
50 {
51         struct rb_node *leftmost = rb_first_cached(&lock->waiters);
52         struct rt_mutex_waiter *w = NULL;
53
54         if (leftmost) {
55                 w = rb_entry(leftmost, struct rt_mutex_waiter, tree_entry);
56                 BUG_ON(w->lock != lock);
57         }
58         return w;
59 }
60
61 static inline int task_has_pi_waiters(struct task_struct *p)
62 {
63         return !RB_EMPTY_ROOT(&p->pi_waiters.rb_root);
64 }
65
66 static inline struct rt_mutex_waiter *task_top_pi_waiter(struct task_struct *p)
67 {
68         return rb_entry(p->pi_waiters.rb_leftmost, struct rt_mutex_waiter,
69                         pi_tree_entry);
70 }
71
72 #define RT_MUTEX_HAS_WAITERS    1UL
73
74 static inline struct task_struct *rt_mutex_owner(struct rt_mutex *lock)
75 {
76         unsigned long owner = (unsigned long) READ_ONCE(lock->owner);
77
78         return (struct task_struct *) (owner & ~RT_MUTEX_HAS_WAITERS);
79 }
80 #else /* CONFIG_RT_MUTEXES */
81 /* Used in rcu/tree_plugin.h */
82 static inline struct task_struct *rt_mutex_owner(struct rt_mutex *lock)
83 {
84         return NULL;
85 }
86 #endif  /* !CONFIG_RT_MUTEXES */
87
88 /*
89  * Constants for rt mutex functions which have a selectable deadlock
90  * detection.
91  *
92  * RT_MUTEX_MIN_CHAINWALK:      Stops the lock chain walk when there are
93  *                              no further PI adjustments to be made.
94  *
95  * RT_MUTEX_FULL_CHAINWALK:     Invoke deadlock detection with a full
96  *                              walk of the lock chain.
97  */
98 enum rtmutex_chainwalk {
99         RT_MUTEX_MIN_CHAINWALK,
100         RT_MUTEX_FULL_CHAINWALK,
101 };
102
103 /*
104  * PI-futex support (proxy locking functions, etc.):
105  */
106 extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
107                                        struct task_struct *proxy_owner);
108 extern void rt_mutex_proxy_unlock(struct rt_mutex *lock);
109 extern void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter);
110 extern int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
111                                      struct rt_mutex_waiter *waiter,
112                                      struct task_struct *task);
113 extern int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
114                                      struct rt_mutex_waiter *waiter,
115                                      struct task_struct *task);
116 extern int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
117                                struct hrtimer_sleeper *to,
118                                struct rt_mutex_waiter *waiter);
119 extern bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
120                                  struct rt_mutex_waiter *waiter);
121
122 extern int rt_mutex_futex_trylock(struct rt_mutex *l);
123 extern int __rt_mutex_futex_trylock(struct rt_mutex *l);
124
125 extern void rt_mutex_futex_unlock(struct rt_mutex *lock);
126 extern bool __rt_mutex_futex_unlock(struct rt_mutex *lock,
127                                  struct wake_q_head *wqh);
128
129 extern void rt_mutex_postunlock(struct wake_q_head *wake_q);
130
131 #ifdef CONFIG_DEBUG_RT_MUTEXES
132 # include "rtmutex-debug.h"
133 #else
134 # include "rtmutex.h"
135 #endif
136
137 #endif