Skip to content

Commit 802ab58

Browse files
Sebastian Andrzej SiewiorKAGA-KOKO
authored andcommitted
futex: Lower the lock contention on the HB lock during wake up
wake_futex_pi() wakes the task before releasing the hash bucket lock (HB). The first thing the woken up task usually does is to acquire the lock which requires the HB lock. On SMP Systems this leads to blocking on the HB lock which is released by the owner shortly after. This patch rearranges the unlock path by first releasing the HB lock and then waking up the task. [ tglx: Fixed up the rtmutex unlock path ] Originally-from: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Mike Galbraith <umgwanakikbuti@gmail.com> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Link: http://lkml.kernel.org/r/20150617083350.GA2433@linutronix.de Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 45ab4ef commit 802ab58

3 files changed

Lines changed: 73 additions & 18 deletions

File tree

‎kernel/futex.c‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,11 +1117,14 @@ static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
11171117
q->lock_ptr = NULL;
11181118
}
11191119

1120-
static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
1120+
static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
1121+
struct futex_hash_bucket *hb)
11211122
{
11221123
struct task_struct *new_owner;
11231124
struct futex_pi_state *pi_state = this->pi_state;
11241125
u32 uninitialized_var(curval), newval;
1126+
WAKE_Q(wake_q);
1127+
bool deboost;
11251128
int ret = 0;
11261129

11271130
if (!pi_state)
@@ -1173,7 +1176,19 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
11731176
raw_spin_unlock_irq(&new_owner->pi_lock);
11741177

11751178
raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
1176-
rt_mutex_unlock(&pi_state->pi_mutex);
1179+
1180+
deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
1181+
1182+
/*
1183+
* First unlock HB so the waiter does not spin on it once he got woken
1184+
* up. Second wake up the waiter before the priority is adjusted. If we
1185+
* deboost first (and lose our higher priority), then the task might get
1186+
* scheduled away before the wake up can take place.
1187+
*/
1188+
spin_unlock(&hb->lock);
1189+
wake_up_q(&wake_q);
1190+
if (deboost)
1191+
rt_mutex_adjust_prio(current);
11771192

11781193
return 0;
11791194
}
@@ -2413,13 +2428,23 @@ static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
24132428
*/
24142429
match = futex_top_waiter(hb, &key);
24152430
if (match) {
2416-
ret = wake_futex_pi(uaddr, uval, match);
2431+
ret = wake_futex_pi(uaddr, uval, match, hb);
2432+
/*
2433+
* In case of success wake_futex_pi dropped the hash
2434+
* bucket lock.
2435+
*/
2436+
if (!ret)
2437+
goto out_putkey;
24172438
/*
24182439
* The atomic access to the futex value generated a
24192440
* pagefault, so retry the user-access and the wakeup:
24202441
*/
24212442
if (ret == -EFAULT)
24222443
goto pi_faulted;
2444+
/*
2445+
* wake_futex_pi has detected invalid state. Tell user
2446+
* space.
2447+
*/
24232448
goto out_unlock;
24242449
}
24252450

@@ -2440,6 +2465,7 @@ static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
24402465

24412466
out_unlock:
24422467
spin_unlock(&hb->lock);
2468+
out_putkey:
24432469
put_futex_key(&key);
24442470
return ret;
24452471

‎kernel/locking/rtmutex.c‎

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ static void __rt_mutex_adjust_prio(struct task_struct *task)
300300
* of task. We do not use the spin_xx_mutex() variants here as we are
301301
* outside of the debug path.)
302302
*/
303-
static void rt_mutex_adjust_prio(struct task_struct *task)
303+
void rt_mutex_adjust_prio(struct task_struct *task)
304304
{
305305
unsigned long flags;
306306

@@ -1247,13 +1247,12 @@ static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
12471247
}
12481248

12491249
/*
1250-
* Slow path to release a rt-mutex:
1250+
* Slow path to release a rt-mutex.
1251+
* Return whether the current task needs to undo a potential priority boosting.
12511252
*/
1252-
static void __sched
1253-
rt_mutex_slowunlock(struct rt_mutex *lock)
1253+
static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1254+
struct wake_q_head *wake_q)
12541255
{
1255-
WAKE_Q(wake_q);
1256-
12571256
raw_spin_lock(&lock->wait_lock);
12581257

12591258
debug_rt_mutex_unlock(lock);
@@ -1294,7 +1293,7 @@ rt_mutex_slowunlock(struct rt_mutex *lock)
12941293
while (!rt_mutex_has_waiters(lock)) {
12951294
/* Drops lock->wait_lock ! */
12961295
if (unlock_rt_mutex_safe(lock) == true)
1297-
return;
1296+
return false;
12981297
/* Relock the rtmutex and try again */
12991298
raw_spin_lock(&lock->wait_lock);
13001299
}
@@ -1305,13 +1304,12 @@ rt_mutex_slowunlock(struct rt_mutex *lock)
13051304
*
13061305
* Queue the next waiter for wakeup once we release the wait_lock.
13071306
*/
1308-
mark_wakeup_next_waiter(&wake_q, lock);
1307+
mark_wakeup_next_waiter(wake_q, lock);
13091308

13101309
raw_spin_unlock(&lock->wait_lock);
1311-
wake_up_q(&wake_q);
13121310

1313-
/* Undo pi boosting if necessary: */
1314-
rt_mutex_adjust_prio(current);
1311+
/* check PI boosting */
1312+
return true;
13151313
}
13161314

13171315
/*
@@ -1362,12 +1360,23 @@ rt_mutex_fasttrylock(struct rt_mutex *lock,
13621360

13631361
static inline void
13641362
rt_mutex_fastunlock(struct rt_mutex *lock,
1365-
void (*slowfn)(struct rt_mutex *lock))
1363+
bool (*slowfn)(struct rt_mutex *lock,
1364+
struct wake_q_head *wqh))
13661365
{
1367-
if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
1366+
WAKE_Q(wake_q);
1367+
1368+
if (likely(rt_mutex_cmpxchg(lock, current, NULL))) {
13681369
rt_mutex_deadlock_account_unlock(current);
1369-
else
1370-
slowfn(lock);
1370+
1371+
} else {
1372+
bool deboost = slowfn(lock, &wake_q);
1373+
1374+
wake_up_q(&wake_q);
1375+
1376+
/* Undo pi boosting if necessary: */
1377+
if (deboost)
1378+
rt_mutex_adjust_prio(current);
1379+
}
13711380
}
13721381

13731382
/**
@@ -1461,6 +1470,23 @@ void __sched rt_mutex_unlock(struct rt_mutex *lock)
14611470
}
14621471
EXPORT_SYMBOL_GPL(rt_mutex_unlock);
14631472

1473+
/**
1474+
* rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
1475+
* @lock: the rt_mutex to be unlocked
1476+
*
1477+
* Returns: true/false indicating whether priority adjustment is
1478+
* required or not.
1479+
*/
1480+
bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
1481+
struct wake_q_head *wqh)
1482+
{
1483+
if (likely(rt_mutex_cmpxchg(lock, current, NULL))) {
1484+
rt_mutex_deadlock_account_unlock(current);
1485+
return false;
1486+
}
1487+
return rt_mutex_slowunlock(lock, wqh);
1488+
}
1489+
14641490
/**
14651491
* rt_mutex_destroy - mark a mutex unusable
14661492
* @lock: the mutex to be destroyed

‎kernel/locking/rtmutex_common.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ extern int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
131131
struct hrtimer_sleeper *to,
132132
struct rt_mutex_waiter *waiter);
133133
extern int rt_mutex_timed_futex_lock(struct rt_mutex *l, struct hrtimer_sleeper *to);
134+
extern bool rt_mutex_futex_unlock(struct rt_mutex *lock,
135+
struct wake_q_head *wqh);
136+
extern void rt_mutex_adjust_prio(struct task_struct *task);
134137

135138
#ifdef CONFIG_DEBUG_RT_MUTEXES
136139
# include "rtmutex-debug.h"

0 commit comments

Comments
 (0)