[PATCH v2 2/2] rtmutex: Kill pi_waiters_leftmost from task_struct

From: Xunlei Pang
Date: Wed Apr 06 2016 - 08:59:57 EST


Current code use pi_waiters_leftmost to record the leftmost waiter,
but actually it can be get directly from task_struct::pi_waiters
using rb_first(). The performance penalty introduced by rb_first()
should be fine, because normally there aren't that many rtmutexes
chained together for one task.

We don't remove rt_mutex:pi_waiters_leftmost, as it is quite possible
for many tasks sharing one rtmutex.

Thus, hereby remove pi_waiters_leftmost from task_struct.

Signed-off-by: Xunlei Pang <xlpang@xxxxxxxxxx>
---
include/linux/init_task.h | 1 -
include/linux/sched.h | 1 -
kernel/fork.c | 1 -
kernel/locking/rtmutex.c | 13 ++-----------
kernel/locking/rtmutex_common.h | 2 +-
5 files changed, 3 insertions(+), 15 deletions(-)

diff --git a/include/linux/init_task.h b/include/linux/init_task.h
index de834f3..a967dbf 100644
--- a/include/linux/init_task.h
+++ b/include/linux/init_task.h
@@ -162,7 +162,6 @@ extern struct task_group root_task_group;
#ifdef CONFIG_RT_MUTEXES
# define INIT_RT_MUTEXES(tsk) \
.pi_waiters = RB_ROOT, \
- .pi_waiters_leftmost = NULL, \
.pi_top_task = NULL,
#else
# define INIT_RT_MUTEXES(tsk)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index b4d9347..6477e22 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1620,7 +1620,6 @@ struct task_struct {
#ifdef CONFIG_RT_MUTEXES
/* PI waiters blocked on a rt_mutex held by this task */
struct rb_root pi_waiters;
- struct rb_node *pi_waiters_leftmost;
struct task_struct *pi_top_task;
/* Deadlock detection and priority inheritance handling */
struct rt_mutex_waiter *pi_blocked_on;
diff --git a/kernel/fork.c b/kernel/fork.c
index 3ad84c9..fc6e5d8 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1207,7 +1207,6 @@ static void rt_mutex_init_task(struct task_struct *p)
raw_spin_lock_init(&p->pi_lock);
#ifdef CONFIG_RT_MUTEXES
p->pi_waiters = RB_ROOT;
- p->pi_waiters_leftmost = NULL;
p->pi_blocked_on = NULL;
p->pi_top_task = NULL;
#endif
diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c
index 6c3a806..b8e9300 100644
--- a/kernel/locking/rtmutex.c
+++ b/kernel/locking/rtmutex.c
@@ -223,22 +223,16 @@ rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
struct rb_node **link = &task->pi_waiters.rb_node;
struct rb_node *parent = NULL;
struct rt_mutex_waiter *entry;
- int leftmost = 1;

while (*link) {
parent = *link;
entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
- if (rt_mutex_waiter_less(waiter, entry)) {
+ if (rt_mutex_waiter_less(waiter, entry))
link = &parent->rb_left;
- } else {
+ else
link = &parent->rb_right;
- leftmost = 0;
- }
}

- if (leftmost)
- task->pi_waiters_leftmost = &waiter->pi_tree_entry;
-
rb_link_node(&waiter->pi_tree_entry, parent, link);
rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
}
@@ -249,9 +243,6 @@ rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
return;

- if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
- task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
-
rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
RB_CLEAR_NODE(&waiter->pi_tree_entry);
}
diff --git a/kernel/locking/rtmutex_common.h b/kernel/locking/rtmutex_common.h
index 93b0924..ccfa34a 100644
--- a/kernel/locking/rtmutex_common.h
+++ b/kernel/locking/rtmutex_common.h
@@ -63,7 +63,7 @@ static inline int task_has_pi_waiters(struct task_struct *p)
static inline struct rt_mutex_waiter *
task_top_pi_waiter(struct task_struct *p)
{
- return rb_entry(p->pi_waiters_leftmost, struct rt_mutex_waiter,
+ return rb_entry(rb_first(&p->pi_waiters), struct rt_mutex_waiter,
pi_tree_entry);
}

--
1.8.3.1