Skip to content

Commit 5293c2e

Browse files
Peter ZijlstraKAGA-KOKO
authored andcommitted
futex,rt_mutex: Provide futex specific rt_mutex API
Part of what makes futex_unlock_pi() intricate is that rt_mutex_futex_unlock() -> rt_mutex_slowunlock() can drop rt_mutex::wait_lock. This means it cannot rely on the atomicy of wait_lock, which would be preferred in order to not rely on hb->lock so much. The reason rt_mutex_slowunlock() needs to drop wait_lock is because it can race with the rt_mutex fastpath, however futexes have their own fast path. Since futexes already have a bunch of separate rt_mutex accessors, complete that set and implement a rt_mutex variant without fastpath for them. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: juri.lelli@arm.com Cc: bigeasy@linutronix.de Cc: xlpang@redhat.com Cc: rostedt@goodmis.org Cc: mathieu.desnoyers@efficios.com Cc: jdesfossez@efficios.com Cc: dvhart@infradead.org Cc: bristot@redhat.com Link: http://lkml.kernel.org/r/20170322104151.702962446@infradead.org Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent fffa954 commit 5293c2e

3 files changed

Lines changed: 62 additions & 32 deletions

File tree

‎kernel/futex.c‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ void exit_pi_state_list(struct task_struct *curr)
916916
pi_state->owner = NULL;
917917
raw_spin_unlock_irq(&curr->pi_lock);
918918

919-
rt_mutex_unlock(&pi_state->pi_mutex);
919+
rt_mutex_futex_unlock(&pi_state->pi_mutex);
920920

921921
spin_unlock(&hb->lock);
922922

@@ -1364,20 +1364,18 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *top_waiter
13641364
pi_state->owner = new_owner;
13651365
raw_spin_unlock(&new_owner->pi_lock);
13661366

1367-
raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
1368-
1369-
deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
1370-
13711367
/*
1372-
* First unlock HB so the waiter does not spin on it once he got woken
1373-
* up. Second wake up the waiter before the priority is adjusted. If we
1374-
* deboost first (and lose our higher priority), then the task might get
1375-
* scheduled away before the wake up can take place.
1368+
* We've updated the uservalue, this unlock cannot fail.
13761369
*/
1370+
deboost = __rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
1371+
1372+
raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
13771373
spin_unlock(&hb->lock);
1378-
wake_up_q(&wake_q);
1379-
if (deboost)
1374+
1375+
if (deboost) {
1376+
wake_up_q(&wake_q);
13801377
rt_mutex_adjust_prio(current);
1378+
}
13811379

13821380
return 0;
13831381
}
@@ -2253,7 +2251,7 @@ static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
22532251
* task acquired the rt_mutex after we removed ourself from the
22542252
* rt_mutex waiters list.
22552253
*/
2256-
if (rt_mutex_trylock(&q->pi_state->pi_mutex)) {
2254+
if (rt_mutex_futex_trylock(&q->pi_state->pi_mutex)) {
22572255
locked = 1;
22582256
goto out;
22592257
}
@@ -2568,7 +2566,7 @@ static int futex_lock_pi(u32 __user *uaddr, unsigned int flags,
25682566
if (!trylock) {
25692567
ret = rt_mutex_timed_futex_lock(&q.pi_state->pi_mutex, to);
25702568
} else {
2571-
ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
2569+
ret = rt_mutex_futex_trylock(&q.pi_state->pi_mutex);
25722570
/* Fixup the trylock return value: */
25732571
ret = ret ? 0 : -EWOULDBLOCK;
25742572
}
@@ -2591,7 +2589,7 @@ static int futex_lock_pi(u32 __user *uaddr, unsigned int flags,
25912589
* it and return the fault to userspace.
25922590
*/
25932591
if (ret && (rt_mutex_owner(&q.pi_state->pi_mutex) == current))
2594-
rt_mutex_unlock(&q.pi_state->pi_mutex);
2592+
rt_mutex_futex_unlock(&q.pi_state->pi_mutex);
25952593

25962594
/* Unqueue and drop the lock */
25972595
unqueue_me_pi(&q);
@@ -2898,7 +2896,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
28982896
spin_lock(q.lock_ptr);
28992897
ret = fixup_pi_state_owner(uaddr2, &q, current);
29002898
if (ret && rt_mutex_owner(&q.pi_state->pi_mutex) == current)
2901-
rt_mutex_unlock(&q.pi_state->pi_mutex);
2899+
rt_mutex_futex_unlock(&q.pi_state->pi_mutex);
29022900
/*
29032901
* Drop the reference to the pi state which
29042902
* the requeue_pi() code acquired for us.
@@ -2938,7 +2936,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
29382936
* userspace.
29392937
*/
29402938
if (ret && rt_mutex_owner(pi_mutex) == current)
2941-
rt_mutex_unlock(pi_mutex);
2939+
rt_mutex_futex_unlock(pi_mutex);
29422940

29432941
/* Unqueue and drop the lock. */
29442942
unqueue_me_pi(&q);

‎kernel/locking/rtmutex.c‎

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,15 +1488,23 @@ EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
14881488

14891489
/*
14901490
* Futex variant with full deadlock detection.
1491+
* Futex variants must not use the fast-path, see __rt_mutex_futex_unlock().
14911492
*/
1492-
int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
1493+
int __sched rt_mutex_timed_futex_lock(struct rt_mutex *lock,
14931494
struct hrtimer_sleeper *timeout)
14941495
{
14951496
might_sleep();
14961497

1497-
return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1498-
RT_MUTEX_FULL_CHAINWALK,
1499-
rt_mutex_slowlock);
1498+
return rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE,
1499+
timeout, RT_MUTEX_FULL_CHAINWALK);
1500+
}
1501+
1502+
/*
1503+
* Futex variant, must not use fastpath.
1504+
*/
1505+
int __sched rt_mutex_futex_trylock(struct rt_mutex *lock)
1506+
{
1507+
return rt_mutex_slowtrylock(lock);
15001508
}
15011509

15021510
/**
@@ -1555,19 +1563,38 @@ void __sched rt_mutex_unlock(struct rt_mutex *lock)
15551563
EXPORT_SYMBOL_GPL(rt_mutex_unlock);
15561564

15571565
/**
1558-
* rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
1559-
* @lock: the rt_mutex to be unlocked
1560-
*
1561-
* Returns: true/false indicating whether priority adjustment is
1562-
* required or not.
1566+
* Futex variant, that since futex variants do not use the fast-path, can be
1567+
* simple and will not need to retry.
15631568
*/
1564-
bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
1565-
struct wake_q_head *wqh)
1569+
bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
1570+
struct wake_q_head *wake_q)
15661571
{
1567-
if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1568-
return false;
1572+
lockdep_assert_held(&lock->wait_lock);
1573+
1574+
debug_rt_mutex_unlock(lock);
1575+
1576+
if (!rt_mutex_has_waiters(lock)) {
1577+
lock->owner = NULL;
1578+
return false; /* done */
1579+
}
1580+
1581+
mark_wakeup_next_waiter(wake_q, lock);
1582+
return true; /* deboost and wakeups */
1583+
}
15691584

1570-
return rt_mutex_slowunlock(lock, wqh);
1585+
void __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
1586+
{
1587+
DEFINE_WAKE_Q(wake_q);
1588+
bool deboost;
1589+
1590+
raw_spin_lock_irq(&lock->wait_lock);
1591+
deboost = __rt_mutex_futex_unlock(lock, &wake_q);
1592+
raw_spin_unlock_irq(&lock->wait_lock);
1593+
1594+
if (deboost) {
1595+
wake_up_q(&wake_q);
1596+
rt_mutex_adjust_prio(current);
1597+
}
15711598
}
15721599

15731600
/**

‎kernel/locking/rtmutex_common.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,14 @@ extern int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
109109
extern int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
110110
struct hrtimer_sleeper *to,
111111
struct rt_mutex_waiter *waiter);
112+
112113
extern int rt_mutex_timed_futex_lock(struct rt_mutex *l, struct hrtimer_sleeper *to);
113-
extern bool rt_mutex_futex_unlock(struct rt_mutex *lock,
114-
struct wake_q_head *wqh);
114+
extern int rt_mutex_futex_trylock(struct rt_mutex *l);
115+
116+
extern void rt_mutex_futex_unlock(struct rt_mutex *lock);
117+
extern bool __rt_mutex_futex_unlock(struct rt_mutex *lock,
118+
struct wake_q_head *wqh);
119+
115120
extern void rt_mutex_adjust_prio(struct task_struct *task);
116121

117122
#ifdef CONFIG_DEBUG_RT_MUTEXES

0 commit comments

Comments
 (0)