aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/locking
diff options
context:
space:
mode:
authorPeter Zijlstra <peterz@infradead.org>2013-11-07 08:43:43 -0500
committerIngo Molnar <mingo@kernel.org>2014-01-13 07:41:50 -0500
commitfb00aca474405f4fa8a8519c3179fed722eabd83 (patch)
tree8a779629a771dd3340d5a3ba0ba16b732b8de1c8 /kernel/locking
parentaf6ace764d03900524e9b1ac621a1c520ee49fc6 (diff)
rtmutex: Turn the plist into an rb-tree
Turn the pi-chains from plist to rb-tree, in the rt_mutex code, and provide a proper comparison function for -deadline and -priority tasks. This is done mainly because: - classical prio field of the plist is just an int, which might not be enough for representing a deadline; - manipulating such a list would become O(nr_deadline_tasks), which might be to much, as the number of -deadline task increases. Therefore, an rb-tree is used, and tasks are queued in it according to the following logic: - among two -priority (i.e., SCHED_BATCH/OTHER/RR/FIFO) tasks, the one with the higher (lower, actually!) prio wins; - among a -priority and a -deadline task, the latter always wins; - among two -deadline tasks, the one with the earliest deadline wins. Queueing and dequeueing functions are changed accordingly, for both the list of a task's pi-waiters and the list of tasks blocked on a pi-lock. Signed-off-by: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Dario Faggioli <raistlin@linux.it> Signed-off-by: Juri Lelli <juri.lelli@gmail.com> Signed-off-again-by: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/1383831828-15501-10-git-send-email-juri.lelli@gmail.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
Diffstat (limited to 'kernel/locking')
-rw-r--r--kernel/locking/rtmutex-debug.c8
-rw-r--r--kernel/locking/rtmutex.c151
-rw-r--r--kernel/locking/rtmutex_common.h22
3 files changed, 134 insertions, 47 deletions
diff --git a/kernel/locking/rtmutex-debug.c b/kernel/locking/rtmutex-debug.c
index 13b243a323fa..49b2ed3dced8 100644
--- a/kernel/locking/rtmutex-debug.c
+++ b/kernel/locking/rtmutex-debug.c
@@ -24,7 +24,7 @@
24#include <linux/kallsyms.h> 24#include <linux/kallsyms.h>
25#include <linux/syscalls.h> 25#include <linux/syscalls.h>
26#include <linux/interrupt.h> 26#include <linux/interrupt.h>
27#include <linux/plist.h> 27#include <linux/rbtree.h>
28#include <linux/fs.h> 28#include <linux/fs.h>
29#include <linux/debug_locks.h> 29#include <linux/debug_locks.h>
30 30
@@ -57,7 +57,7 @@ static void printk_lock(struct rt_mutex *lock, int print_owner)
57 57
58void rt_mutex_debug_task_free(struct task_struct *task) 58void rt_mutex_debug_task_free(struct task_struct *task)
59{ 59{
60 DEBUG_LOCKS_WARN_ON(!plist_head_empty(&task->pi_waiters)); 60 DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters));
61 DEBUG_LOCKS_WARN_ON(task->pi_blocked_on); 61 DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
62} 62}
63 63
@@ -154,16 +154,12 @@ void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
154void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter) 154void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
155{ 155{
156 memset(waiter, 0x11, sizeof(*waiter)); 156 memset(waiter, 0x11, sizeof(*waiter));
157 plist_node_init(&waiter->list_entry, MAX_PRIO);
158 plist_node_init(&waiter->pi_list_entry, MAX_PRIO);
159 waiter->deadlock_task_pid = NULL; 157 waiter->deadlock_task_pid = NULL;
160} 158}
161 159
162void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter) 160void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
163{ 161{
164 put_pid(waiter->deadlock_task_pid); 162 put_pid(waiter->deadlock_task_pid);
165 DEBUG_LOCKS_WARN_ON(!plist_node_empty(&waiter->list_entry));
166 DEBUG_LOCKS_WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
167 memset(waiter, 0x22, sizeof(*waiter)); 163 memset(waiter, 0x22, sizeof(*waiter));
168} 164}
169 165
diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c
index 0dd6aec1cb6a..3bf0aa68dd3f 100644
--- a/kernel/locking/rtmutex.c
+++ b/kernel/locking/rtmutex.c
@@ -14,6 +14,7 @@
14#include <linux/export.h> 14#include <linux/export.h>
15#include <linux/sched.h> 15#include <linux/sched.h>
16#include <linux/sched/rt.h> 16#include <linux/sched/rt.h>
17#include <linux/sched/deadline.h>
17#include <linux/timer.h> 18#include <linux/timer.h>
18 19
19#include "rtmutex_common.h" 20#include "rtmutex_common.h"
@@ -91,10 +92,104 @@ static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
91} 92}
92#endif 93#endif
93 94
95static inline int
96rt_mutex_waiter_less(struct rt_mutex_waiter *left,
97 struct rt_mutex_waiter *right)
98{
99 if (left->task->prio < right->task->prio)
100 return 1;
101
102 /*
103 * If both tasks are dl_task(), we check their deadlines.
104 */
105 if (dl_prio(left->task->prio) && dl_prio(right->task->prio))
106 return (left->task->dl.deadline < right->task->dl.deadline);
107
108 return 0;
109}
110
111static void
112rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
113{
114 struct rb_node **link = &lock->waiters.rb_node;
115 struct rb_node *parent = NULL;
116 struct rt_mutex_waiter *entry;
117 int leftmost = 1;
118
119 while (*link) {
120 parent = *link;
121 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
122 if (rt_mutex_waiter_less(waiter, entry)) {
123 link = &parent->rb_left;
124 } else {
125 link = &parent->rb_right;
126 leftmost = 0;
127 }
128 }
129
130 if (leftmost)
131 lock->waiters_leftmost = &waiter->tree_entry;
132
133 rb_link_node(&waiter->tree_entry, parent, link);
134 rb_insert_color(&waiter->tree_entry, &lock->waiters);
135}
136
137static void
138rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
139{
140 if (RB_EMPTY_NODE(&waiter->tree_entry))
141 return;
142
143 if (lock->waiters_leftmost == &waiter->tree_entry)
144 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
145
146 rb_erase(&waiter->tree_entry, &lock->waiters);
147 RB_CLEAR_NODE(&waiter->tree_entry);
148}
149
150static void
151rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
152{
153 struct rb_node **link = &task->pi_waiters.rb_node;
154 struct rb_node *parent = NULL;
155 struct rt_mutex_waiter *entry;
156 int leftmost = 1;
157
158 while (*link) {
159 parent = *link;
160 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
161 if (rt_mutex_waiter_less(waiter, entry)) {
162 link = &parent->rb_left;
163 } else {
164 link = &parent->rb_right;
165 leftmost = 0;
166 }
167 }
168
169 if (leftmost)
170 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
171
172 rb_link_node(&waiter->pi_tree_entry, parent, link);
173 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
174}
175
176static void
177rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
178{
179 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
180 return;
181
182 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
183 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
184
185 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
186 RB_CLEAR_NODE(&waiter->pi_tree_entry);
187}
188
94/* 189/*
95 * Calculate task priority from the waiter list priority 190 * Calculate task priority from the waiter tree priority
96 * 191 *
97 * Return task->normal_prio when the waiter list is empty or when 192 * Return task->normal_prio when the waiter tree is empty or when
98 * the waiter is not allowed to do priority boosting 193 * the waiter is not allowed to do priority boosting
99 */ 194 */
100int rt_mutex_getprio(struct task_struct *task) 195int rt_mutex_getprio(struct task_struct *task)
@@ -102,7 +197,7 @@ int rt_mutex_getprio(struct task_struct *task)
102 if (likely(!task_has_pi_waiters(task))) 197 if (likely(!task_has_pi_waiters(task)))
103 return task->normal_prio; 198 return task->normal_prio;
104 199
105 return min(task_top_pi_waiter(task)->pi_list_entry.prio, 200 return min(task_top_pi_waiter(task)->task->prio,
106 task->normal_prio); 201 task->normal_prio);
107} 202}
108 203
@@ -233,7 +328,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
233 * When deadlock detection is off then we check, if further 328 * When deadlock detection is off then we check, if further
234 * priority adjustment is necessary. 329 * priority adjustment is necessary.
235 */ 330 */
236 if (!detect_deadlock && waiter->list_entry.prio == task->prio) 331 if (!detect_deadlock && waiter->task->prio == task->prio)