blob: 8d039b928d610932206f49babe739bacda5b0c15 [file] [log] [blame]
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001/*
2 * RT Mutexes: blocking mutual exclusion locks with PI support
3 *
4 * started by Ingo Molnar and Thomas Gleixner:
5 *
6 * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
7 * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
8 *
9 * This file contains the private data structure and API definitions.
10 */
11
12#ifndef __KERNEL_RTMUTEX_COMMON_H
13#define __KERNEL_RTMUTEX_COMMON_H
14
15#include <linux/rtmutex.h>
Ingo Molnar84f001e2017-02-01 16:36:40 +010016#include <linux/sched/wake_q.h>
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070017
18/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070019 * This is the control structure for tasks blocked on a rt_mutex,
20 * which is allocated on the kernel stack on of the blocked task.
21 *
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010022 * @tree_entry: pi node to enqueue into the mutex waiters tree
23 * @pi_tree_entry: pi node to enqueue into the mutex owner waiters tree
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070024 * @task: task reference to the blocked task
25 */
26struct rt_mutex_waiter {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010027 struct rb_node tree_entry;
28 struct rb_node pi_tree_entry;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070029 struct task_struct *task;
30 struct rt_mutex *lock;
31#ifdef CONFIG_DEBUG_RT_MUTEXES
32 unsigned long ip;
Pavel Emelyanov48d13e42008-02-08 04:21:53 -080033 struct pid *deadlock_task_pid;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070034 struct rt_mutex *deadlock_lock;
35#endif
Dario Faggioli2d3d8912013-11-07 14:43:44 +010036 int prio;
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +010037 u64 deadline;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070038};
39
40/*
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010041 * Various helpers to access the waiters-tree:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070042 */
Nicolas Pitrebc2eecd2017-08-01 00:31:32 -040043
44#ifdef CONFIG_RT_MUTEXES
45
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070046static inline int rt_mutex_has_waiters(struct rt_mutex *lock)
47{
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010048 return !RB_EMPTY_ROOT(&lock->waiters);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070049}
50
51static inline struct rt_mutex_waiter *
52rt_mutex_top_waiter(struct rt_mutex *lock)
53{
54 struct rt_mutex_waiter *w;
55
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010056 w = rb_entry(lock->waiters_leftmost, struct rt_mutex_waiter,
57 tree_entry);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070058 BUG_ON(w->lock != lock);
59
60 return w;
61}
62
63static inline int task_has_pi_waiters(struct task_struct *p)
64{
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010065 return !RB_EMPTY_ROOT(&p->pi_waiters);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070066}
67
68static inline struct rt_mutex_waiter *
69task_top_pi_waiter(struct task_struct *p)
70{
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010071 return rb_entry(p->pi_waiters_leftmost, struct rt_mutex_waiter,
72 pi_tree_entry);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070073}
74
Nicolas Pitrebc2eecd2017-08-01 00:31:32 -040075#else
76
77static inline int rt_mutex_has_waiters(struct rt_mutex *lock)
78{
79 return false;
80}
81
82static inline struct rt_mutex_waiter *
83rt_mutex_top_waiter(struct rt_mutex *lock)
84{
85 return NULL;
86}
87
88static inline int task_has_pi_waiters(struct task_struct *p)
89{
90 return false;
91}
92
93static inline struct rt_mutex_waiter *
94task_top_pi_waiter(struct task_struct *p)
95{
96 return NULL;
97}
98
99#endif
100
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700101/*
102 * lock->owner state tracking:
103 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800104#define RT_MUTEX_HAS_WAITERS 1UL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700105
106static inline struct task_struct *rt_mutex_owner(struct rt_mutex *lock)
107{
Thomas Gleixner1be5d4f2016-11-30 21:04:42 +0000108 unsigned long owner = (unsigned long) READ_ONCE(lock->owner);
109
Thomas Gleixnerb5016e82016-11-30 21:04:44 +0000110 return (struct task_struct *) (owner & ~RT_MUTEX_HAS_WAITERS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700111}
112
Ingo Molnarc87e2832006-06-27 02:54:58 -0700113/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000114 * Constants for rt mutex functions which have a selectable deadlock
115 * detection.
116 *
117 * RT_MUTEX_MIN_CHAINWALK: Stops the lock chain walk when there are
118 * no further PI adjustments to be made.
119 *
120 * RT_MUTEX_FULL_CHAINWALK: Invoke deadlock detection with a full
121 * walk of the lock chain.
122 */
123enum rtmutex_chainwalk {
124 RT_MUTEX_MIN_CHAINWALK,
125 RT_MUTEX_FULL_CHAINWALK,
126};
127
128/*
Ingo Molnarc87e2832006-06-27 02:54:58 -0700129 * PI-futex support (proxy locking functions, etc.):
130 */
131extern struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock);
132extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
133 struct task_struct *proxy_owner);
134extern void rt_mutex_proxy_unlock(struct rt_mutex *lock,
135 struct task_struct *proxy_owner);
Peter Zijlstra50809352017-03-22 11:35:56 +0100136extern void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter);
Peter Zijlstra56222b22017-03-22 11:36:00 +0100137extern int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
138 struct rt_mutex_waiter *waiter,
139 struct task_struct *task);
Darren Hart8dac4562009-04-03 13:40:12 -0700140extern int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
141 struct rt_mutex_waiter *waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +0000142 struct task_struct *task);
Peter Zijlstra38d589f2017-03-22 11:35:57 +0100143extern int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
144 struct hrtimer_sleeper *to,
145 struct rt_mutex_waiter *waiter);
146extern bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
147 struct rt_mutex_waiter *waiter);
Peter Zijlstra5293c2e2017-03-22 11:35:51 +0100148
Peter Zijlstra5293c2e2017-03-22 11:35:51 +0100149extern int rt_mutex_futex_trylock(struct rt_mutex *l);
150
151extern void rt_mutex_futex_unlock(struct rt_mutex *lock);
152extern bool __rt_mutex_futex_unlock(struct rt_mutex *lock,
153 struct wake_q_head *wqh);
154
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +0100155extern void rt_mutex_postunlock(struct wake_q_head *wake_q);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700156
157#ifdef CONFIG_DEBUG_RT_MUTEXES
158# include "rtmutex-debug.h"
159#else
160# include "rtmutex.h"
161#endif
162
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700163#endif