Skip to content

Commit fb00aca

Browse files
Peter Zijlstraingomolnar
authored andcommitted
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>
1 parent af6ace7 commit fb00aca

9 files changed

Lines changed: 157 additions & 65 deletions

File tree

‎include/linux/init_task.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/user_namespace.h>
1212
#include <linux/securebits.h>
1313
#include <linux/seqlock.h>
14+
#include <linux/rbtree.h>
1415
#include <net/net_namespace.h>
1516
#include <linux/sched/rt.h>
1617

@@ -154,6 +155,14 @@ extern struct task_group root_task_group;
154155

155156
#define INIT_TASK_COMM "swapper"
156157

158+
#ifdef CONFIG_RT_MUTEXES
159+
# define INIT_RT_MUTEXES(tsk) \
160+
.pi_waiters = RB_ROOT, \
161+
.pi_waiters_leftmost = NULL,
162+
#else
163+
# define INIT_RT_MUTEXES(tsk)
164+
#endif
165+
157166
/*
158167
* INIT_TASK is used to set up the first task table, touch at
159168
* your own risk!. Base=0, limit=0x1fffff (=2MB)
@@ -221,6 +230,7 @@ extern struct task_group root_task_group;
221230
INIT_TRACE_RECURSION \
222231
INIT_TASK_RCU_PREEMPT(tsk) \
223232
INIT_CPUSET_SEQ(tsk) \
233+
INIT_RT_MUTEXES(tsk) \
224234
INIT_VTIME(tsk) \
225235
}
226236

‎include/linux/rtmutex.h‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#define __LINUX_RT_MUTEX_H
1414

1515
#include <linux/linkage.h>
16-
#include <linux/plist.h>
16+
#include <linux/rbtree.h>
1717
#include <linux/spinlock_types.h>
1818

1919
extern int max_lock_depth; /* for sysctl */
@@ -22,12 +22,14 @@ extern int max_lock_depth; /* for sysctl */
2222
* The rt_mutex structure
2323
*
2424
* @wait_lock: spinlock to protect the structure
25-
* @wait_list: pilist head to enqueue waiters in priority order
25+
* @waiters: rbtree root to enqueue waiters in priority order
26+
* @waiters_leftmost: top waiter
2627
* @owner: the mutex owner
2728
*/
2829
struct rt_mutex {
2930
raw_spinlock_t wait_lock;
30-
struct plist_head wait_list;
31+
struct rb_root waiters;
32+
struct rb_node *waiters_leftmost;
3133
struct task_struct *owner;
3234
#ifdef CONFIG_DEBUG_RT_MUTEXES
3335
int save_state;
@@ -66,7 +68,7 @@ struct hrtimer_sleeper;
6668

6769
#define __RT_MUTEX_INITIALIZER(mutexname) \
6870
{ .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
69-
, .wait_list = PLIST_HEAD_INIT(mutexname.wait_list) \
71+
, .waiters = RB_ROOT \
7072
, .owner = NULL \
7173
__DEBUG_RT_MUTEX_INITIALIZER(mutexname)}
7274

@@ -98,12 +100,4 @@ extern int rt_mutex_trylock(struct rt_mutex *lock);
98100

99101
extern void rt_mutex_unlock(struct rt_mutex *lock);
100102

101-
#ifdef CONFIG_RT_MUTEXES
102-
# define INIT_RT_MUTEXES(tsk) \
103-
.pi_waiters = PLIST_HEAD_INIT(tsk.pi_waiters), \
104-
INIT_RT_MUTEX_DEBUG(tsk)
105-
#else
106-
# define INIT_RT_MUTEXES(tsk)
107-
#endif
108-
109103
#endif

‎include/linux/sched.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct sched_param {
1616
#include <linux/types.h>
1717
#include <linux/timex.h>
1818
#include <linux/jiffies.h>
19+
#include <linux/plist.h>
1920
#include <linux/rbtree.h>
2021
#include <linux/thread_info.h>
2122
#include <linux/cpumask.h>
@@ -1354,7 +1355,8 @@ struct task_struct {
13541355

13551356
#ifdef CONFIG_RT_MUTEXES
13561357
/* PI waiters blocked on a rt_mutex held by this task */
1357-
struct plist_head pi_waiters;
1358+
struct rb_root pi_waiters;
1359+
struct rb_node *pi_waiters_leftmost;
13581360
/* Deadlock detection and priority inheritance handling */
13591361
struct rt_mutex_waiter *pi_blocked_on;
13601362
#endif

‎kernel/fork.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,8 @@ static void rt_mutex_init_task(struct task_struct *p)
10871087
{
10881088
raw_spin_lock_init(&p->pi_lock);
10891089
#ifdef CONFIG_RT_MUTEXES
1090-
plist_head_init(&p->pi_waiters);
1090+
p->pi_waiters = RB_ROOT;
1091+
p->pi_waiters_leftmost = NULL;
10911092
p->pi_blocked_on = NULL;
10921093
#endif
10931094
}

‎kernel/futex.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,8 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
23162316
* code while we sleep on uaddr.
23172317
*/
23182318
debug_rt_mutex_init_waiter(&rt_waiter);
2319+
RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
2320+
RB_CLEAR_NODE(&rt_waiter.tree_entry);
23192321
rt_waiter.task = NULL;
23202322

23212323
ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);

‎kernel/locking/rtmutex-debug.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <linux/kallsyms.h>
2525
#include <linux/syscalls.h>
2626
#include <linux/interrupt.h>
27-
#include <linux/plist.h>
27+
#include <linux/rbtree.h>
2828
#include <linux/fs.h>
2929
#include <linux/debug_locks.h>
3030

@@ -57,7 +57,7 @@ static void printk_lock(struct rt_mutex *lock, int print_owner)
5757

5858
void rt_mutex_debug_task_free(struct task_struct *task)
5959
{
60-
DEBUG_LOCKS_WARN_ON(!plist_head_empty(&task->pi_waiters));
60+
DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters));
6161
DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
6262
}
6363

@@ -154,16 +154,12 @@ void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
154154
void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
155155
{
156156
memset(waiter, 0x11, sizeof(*waiter));
157-
plist_node_init(&waiter->list_entry, MAX_PRIO);
158-
plist_node_init(&waiter->pi_list_entry, MAX_PRIO);
159157
waiter->deadlock_task_pid = NULL;
160158
}
161159

162160
void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
163161
{
164162
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));
167163
memset(waiter, 0x22, sizeof(*waiter));
168164
}
169165

0 commit comments

Comments
 (0)