rwsem/rt: Lift the single reader restriction
Subsystems (2)
cover rwsem/rt: Lift the single reader restriction
R/W semaphores in RT do not allow multiple readers because a writer blocking on the sempahore would have deal with all the readers in terms of priority or budget inheritance. While multi reader priority boosting would be possible (it has been attempted before), multi reader budget inheritance is impossible.
It's obvious that the single reader restriction has severe performance problems for situations with heavy reader contention.
A typical issue is the contention of mmap_sem. The main issue with mmap_sem vs. process shared futexes has been cured for some architectures by switching to fast GUP, but it still persists for those architectures which do not (yet) implement it.
Non-RT workloads suffer also from mmap_sem contention when they trigger a massive amount of page faults on multiple threads.
There is another issue with R/W semaphores.
The single reader restriction is not violating the !RT semantics of R/W sempahores, because on !RT R/W sempahores are writer fair. That means, that when a writer blocks on a contended R/W semaphore newly incoming readers block behind the writer. This prevents writer starvation.
So the following scenario is resulting in a deadlock independent of RT:
T1down_read(sem);wait_for_event();
schedule()
T2down_write(sem);
blocks_on(sem);
schedule();T3down_read(sem); <- T3 cannot take sem for read and blocks behind T2
... ===> DEADLOCK!wake_waiters();
Though there is a very subtle semantical difference on RT versus the following scenario:
T1down_read(sem);wait_for_event();
schedule()
T2
if (down_write_trylock(sem))
do_something()
T3down_read(sem);
...wake_waiters();
That works on mainline, but breaks on RT due to the single reader restriction.
Yes, that's ugly and should be forbidden, but there is code in the mainline kernel which relies on that (e.g. Radeon driver).
Finding and fixing such constructs is not an easy task and aside of that the single reader restriction is a performance bottleneck.
After analyzing the writer sides of R/W semaphores I came to the conclusion that down_writes() happen in expensive code pathes which should not be invoked in high priority tasks anyway. And if user space is stupid enough to do so, then it's nothing we should worry about. Doing mmap() in your high priority task is stupid to begin with.
The following patch series changes the RT implementation of R/W sempahores to a multi reader model, which is not writer fair. That means writers have to wait until the last reader left the critical section and readers are allowed to take the semaphore for read even when a writer is blocked.
This means there is a risk of writer starvation, but the pathological workloads which trigger it, are not necessarily the typical RT workloads.
It cures the Radeon mess, lowers the contention on mmap_sem for certain workloads and did not have any negative impact in our initial testing on RT behaviour.
I think it's worth to expose it to a wider audience of users for testing, so we can figure out it whether there are dragons lurking.
Thanks,
tglx
PATCH 1/4 rtmutex: Make lock_killable work
Locking an rt mutex killable does not work because signal handling is restricted to TASK_INTERRUPTIBLE.
Use signal_pending_state() unconditionaly.
--- kernel/locking/rtmutex.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-)
kernel/locking/rtmutex.c +7 -12
@@ -1633,18 +1633,13 @@ static int __schedif (try_to_take_rt_mutex (lock, current, waiter))break ; -/* -* TASK_INTERRUPTIBLE checks for signals and -* timeout. Ignored otherwise. -*/ -if (unlikely (state ==TASK_INTERRUPTIBLE )) { -/* Signal pending? */ -if (signal_pending (current)) - ret = -EINTR ; -if (timeout && !timeout->task) - ret = -ETIMEDOUT ; -if (ret) -break ; +if (timeout && !timeout->task) { + ret = -ETIMEDOUT ; +break ; + } +if (signal_pending_state (state, current)) { + ret = -EINTR ; +break ; }if (ww_ctx && ww_ctx->acquired >0 ) {
PATCH 2/4 rtmutex: Provide rt_mutex_lock_state()
In reply to Thomas GleixnerAllow rtmutex to be locked with arbitrary states. Preparatory patch for the rt rwsem rework.
--- include/linux/rtmutex.h | 1 + kernel/locking/rtmutex.c | 44 +++++++++++++++++++++++++------------------- 2 files changed, 26 insertions(+), 19 deletions(-)
include/linux/rtmutex.h +1 -0
@@ -105,6 +105,7 @@ extern void __rt_mutex_init(struct rt_muextern void rt_mutex_destroy (struct rt_mutex *lock);extern void rt_mutex_lock (struct rt_mutex *lock); +extern int rt_mutex_lock_state (struct rt_mutex *lock,int state);extern int rt_mutex_lock_interruptible (struct rt_mutex *lock);extern int rt_mutex_lock_killable (struct rt_mutex *lock); extern int rt_mutex_timed_lock(struct rt_mutex *lock,
kernel/locking/rtmutex.c +25 -19
82 lines not on this page · raw message
PATCH 3/4 rtmutex: Provide locked slowpath
In reply to Thomas GleixnerThe new rt rwsem implementation needs rtmutex::wait_lock to protect struct rw_semaphore. Dropping the lock and reaquiring it for locking the rtmutex would open a race window.
Split out the inner workings of the locked slowpath so it can be called with wait_lock held.
--- kernel/locking/rtmutex.c | 72 +++++++++++++++++++++++----------------- kernel/locking/rtmutex_common.h | 9 +++++ 2 files changed, 51 insertions(+), 30 deletions(-)
kernel/locking/rtmutex.c +42 -30
@@ -1758,36 +1758,18 @@ static void ww_mutex_account_lock(struct }#endif -/* -* Slow path lock function: -*/ -static int __sched -rt_mutex_slowlock (struct rt_mutex *lock,int state, -struct hrtimer_sleeper *timeout, -enum rtmutex_chainwalk chwalk, -struct ww_acquire_ctx *ww_ctx) +int __sched rt_mutex_slowlock_locked(struct rt_mutex *lock, int state, +struct hrtimer_sleeper *timeout, +enum rtmutex_chainwalk chwalk, +struct ww_acquire_ctx *ww_ctx, +struct rt_mutex_waiter *waiter) { -struct rt_mutex_waiter waiter; -unsigned long flags; -int ret =0 ; - -rt_mutex_init_waiter (&waiter, false); - -/* -* Technically we could use raw_spin_[un]lock_irq() here, but this can -* be called in early boot if the cmpxchg() fast path is disabled -* (debug, no architecture support). In this case we will acquire the -* rtmutex with lock->wait_lock held. But we cannot unconditionally -* enable interrupts in that early boot case. So we need to use the -* irqsave/restore variants. -*/ -raw_spin_lock_irqsave (&lock->wait_lock, flags); +int ret;/* Try to acquire the lock again: */ if (try_to_take_rt_mutex (lock, current,NULL )) {if (ww_ctx)ww_mutex_account_lock (lock, ww_ctx); -raw_spin_unlock_irqrestore (&lock->wait_lock, flags);return 0 ; }
@@ -1797,13 +1779,13 @@ rt_mutex_slowlock(struct rt_mutex *lock,if (unlikely (timeout))hrtimer_start_expires (&timeout->timer,HRTIMER_MODE_ABS ); - ret =task_blocks_on_rt_mutex (lock, &waiter, current, chwalk); + ret =task_blocks_on_rt_mutex (lock, waiter, current, chwalk); -if (likely (!ret)) +if (likely (!ret)) {/* sleep on the mutex */ - ret =__rt_mutex_slowlock (lock, state, timeout, &waiter, + ret =__rt_mutex_slowlock (lock, state, timeout, waiter, ww_ctx); -else if (ww_ctx) { + }else if (ww_ctx) {/* ww_mutex received EDEADLK, let it become EALREADY */ ret =__mutex_lock_check_stamp (lock, ww_ctx);BUG_ON (!ret);
@@ -1812,10 +1794,10 @@ rt_mutex_slowlock(struct rt_mutex *lock,if (unlikely (ret)) {__set_current_state (TASK_RUNNING );if (rt_mutex_has_waiters (lock)) -remove_waiter (lock, &waiter); +remove_waiter (lock, waiter);/* ww_mutex want to report EDEADLK/EALREADY, let them */ if (!ww_ctx) -rt_mutex_handle_deadlock (ret, chwalk, &waiter); +rt_mutex_handle_deadlock (ret, chwalk, waiter); }else if (ww_ctx) {ww_mutex_account_lock (lock, ww_ctx); }
@@ -1825,6 +1807,36 @@ rt_mutex_slowlock(struct rt_mutex *lock,* unconditionally. We might have to fix that up. */ fixup_rt_mutex_waiters (lock); +return ret; +} + +/* +* Slow path lock function: +*/ +static int __sched +rt_mutex_slowlock (struct rt_mutex *lock,int state, +struct hrtimer_sleeper *timeout, +enum rtmutex_chainwalk chwalk, +struct ww_acquire_ctx *ww_ctx) +{ +struct rt_mutex_waiter waiter; +unsigned long flags; +int ret =0 ; + +rt_mutex_init_waiter (&waiter, false); + +/* +* Technically we could use raw_spin_[un]lock_irq() here, but this can +* be called in early boot if the cmpxchg() fast path is disabled +* (debug, no architecture support). In this case we will acquire the +* rtmutex with lock->wait_lock held. But we cannot unconditionally +* enable interrupts in that early boot case. So we need to use the +* irqsave/restore variants. +*/ +raw_spin_lock_irqsave (&lock->wait_lock, flags); + + ret =rt_mutex_slowlock_locked (lock, state, timeout, chwalk, ww_ctx, + &waiter);raw_spin_unlock_irqrestore (&lock->wait_lock, flags);
kernel/locking/rtmutex_common.h +9 -0
@@ -129,6 +129,15 @@ extern bool __rt_mutex_futex_unlock(struextern void rt_mutex_adjust_prio (struct task_struct *task); +/* RW semaphore special interface */ +struct ww_acquire_ctx ; + +int __sched rt_mutex_slowlock_locked(struct rt_mutex *lock, int state, +struct hrtimer_sleeper *timeout, +enum rtmutex_chainwalk chwalk, +struct ww_acquire_ctx *ww_ctx, +struct rt_mutex_waiter *waiter); +#ifdef CONFIG_DEBUG_RT_MUTEXES # include "rtmutex-debug.h" #else
PATCH 4/4 rwsem/rt: Lift single reader restriction
In reply to Thomas GleixnerThe RT specific R/W semaphore implementation restricts the number of readers to one because a writer cannot block on multiple readers and inherit its priority or budget.
The single reader restricting is painful in various ways:
- Performance bottleneck for multi-threaded applications in the page fault path (mmap sem)
- Progress blocker for drivers which are carefully crafted to avoid the potential reader/writer deadlock in mainline.
The analysis of the writer code pathes shows, that properly written RT tasks should not take them. Syscalls like mmap(), file access which take mmap sem write locked have unbound latencies which are completely unrelated to mmap sem. Other R/W sem users like graphics drivers are not suitable for RT tasks either.
So there is little risk to hurt RT tasks when the RT rwsem implementation is changed in the following way:
- Allow concurrent readers
- Make writers block until the last reader left the critical section. This blocking is not subject to priority/budget inheritance.
- Readers blocked on a writer inherit their priority/budget in the normal way.
There is a drawback with this scheme. R/W semaphores become writer unfair though the applications which have triggered writer starvation (mostly on mmap_sem) in the past are not really the typical workloads running on a RT system. So while it's unlikely to hit writer starvation, it's possible. If there are unexpected workloads on RT systems triggering it, we need to rethink the approach.
--- include/linux/rwsem.h | 9 + include/linux/rwsem_rt.h | 166 +++++----------------------- kernel/locking/Makefile | 4 kernel/locking/rt.c | 167 ---------------------------- kernel/locking/rwsem-rt.c | 268 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 310 insertions(+), 304 deletions(-)
include/linux/rwsem.h +7 -2
@@ -110,6 +110,13 @@ static inline int rwsem_is_contended(str return !list_empty(&sem->wait_list); } +#endif /* !PREEMPT_RT_FULL */ + +/* +* The functions below are the same for all rwsem implementations including +* the RT specific variant. +*/ +/* * lock for reading */
@@ -188,6 +195,4 @@ extern void up_read_non_owner(struct rw_# define up_read_non_owner (sem) up_read(sem)#endif -#endif /* !PREEMPT_RT_FULL */ -#endif /* _LINUX_RWSEM_H */
include/linux/rwsem_rt.h +33 -133
196 lines not on this page · raw message
kernel/locking/Makefile +2 -2
@@ -14,8 +14,8 @@ endif ifneq ($(CONFIG_PREEMPT_RT_FULL),y) obj-y += mutex.o obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o -obj-y += rwsem.o endif +obj-y += rwsem.o obj-$(CONFIG_LOCKDEP) += lockdep.o ifeq ($(CONFIG_PROC_FS),y) obj-$(CONFIG_LOCKDEP) += lockdep_proc.o
@@ -32,6 +32,6 @@ ifneq ($(CONFIG_PREEMPT_RT_FULL),y) obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem-xadd.o endif -obj-$(CONFIG_PREEMPT_RT_FULL) += rt.o +obj-$(CONFIG_PREEMPT_RT_FULL) += rt.o rwsem-rt.o obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o
kernel/locking/rt.c +0 -167
173 lines not on this page · raw message
kernel/locking/rwsem-rt.c new file +268 -0
268 lines not on this page · raw message
Re: [patch RT 1/4] rtmutex: Make lock_killable work
In reply to Thomas GleixnerQuoted 4 lines · On Sat, 01 Apr 2017 12:50:59 +0200 Thomas Gleixner <[email protected]> wrote:
Locking an rt mutex killable does not work because signal handling is restricted to
TASK_INTERRUPTIBLE.Use
signal_pending_state()unconditionaly.
Does this mean rt mutex killable is not INTERRUPTIBLE? because the change log seems to just assume that.
In other words, mortals reading this have no idea what you are talking about ;-)
-- Steve
Quoted 36 lines
Signed-off-by Thomas Gleixner <[email protected]>--- kernel/locking/rtmutex.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-)kernel/locking/rtmutex.c +7
-12@@ -1633,18 +1633,13 @@ static int __schedif (try_to_take_rt_mutex (lock, current, waiter))break ; -/* -* TASK_INTERRUPTIBLE checks for signals and -* timeout. Ignored otherwise. -*/ -if (unlikely (state ==TASK_INTERRUPTIBLE )) { -/* Signal pending? */ -if (signal_pending (current)) - ret = -EINTR ; -if (timeout && !timeout->task) - ret = -ETIMEDOUT ; -if (ret) -break ; +if (timeout && !timeout->task) { + ret = -ETIMEDOUT ; +break ; + } +if (signal_pending_state (state, current)) { + ret = -EINTR ; +break ; }if (ww_ctx && ww_ctx->acquired >0 ) {
Peter Zijlstra
maintainerRe: [patch RT 1/4] rtmutex: Make lock_killable work
Quoted 6 lines · Steven Rostedt
Quoted 3 lines · On Sat, 01 Apr 2017 12:50:59 +0200 Thomas Gleixner <[email protected]> wrote:
Locking an rt mutex killable does not work because signal handling is restricted to
TASK_INTERRUPTIBLE.
Quoted 6 lines
Quoted 1 line
Use
signal_pending_state()unconditionaly.Does this mean rt mutex killable is not INTERRUPTIBLE? because the change log seems to just assume that.
kernel/locking/rtmutex.c static int __sched · line 1640
Quoted 1 line
kernel/locking/rtmutex.c +0
-1-if (unlikely (state ==TASK_INTERRUPTIBLE )) {
#define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
I don't think we need to consider people who don't know where to find the TASK_state definitions.
Re: [patch RT 1/4] rtmutex: Make lock_killable work
Quoted 12 lines · On Tue, 4 Apr 2017 09:13:02 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 10 lines · Steven Rostedt
Quoted 4 lines · On Sat, 01 Apr 2017 12:50:59 +0200 Thomas Gleixner <[email protected]> wrote:
Locking an rt mutex killable does not work because signal handling is restricted to
TASK_INTERRUPTIBLE.Use
signal_pending_state()unconditionaly.Does this mean rt mutex killable is not INTERRUPTIBLE? because the change log seems to just assume that.
Quoted 7 lines
Quoted 1 line
kernel/locking/rtmutex.c static int __sched · line 1640
Quoted 1 line
kernel/locking/rtmutex.c +0
-1-if (unlikely (state ==TASK_INTERRUPTIBLE )) {#define
TASK_KILLABLE(TASK_WAKEKILL|TASK_UNINTERRUPTIBLE)I don't think we need to consider people who don't know where to find the
TASK_statedefinitions.
No where in the change log did it mention TASK_KILLABLE. It only talked about "rt mutex killable". Yeah, I can figure this out, but that doesn't change that the fact that it was a weak change log.
Thomas has yelled at me for some of my change logs in the past that were better than this ;-)
-- Steve
Quoted 5 lines · Thomas Gleixner
R/W semaphores in RT do not allow multiple readers because a writer blocking on the sempahore would have deal with all the readers in terms of priority or budget inheritance. While multi reader priority boosting would be possible (it has been attempted before), multi reader budget inheritance is impossible.
…
Quoted 6 lines
It cures the Radeon mess, lowers the contention on
mmap_semfor certain workloads and did not have any negative impact in our initial testing on RT behaviour.I think it's worth to expose it to a wider audience of users for testing, so we can figure out it whether there are dragons lurking.
series applied.
Quoted 3 lines
Thanks,
tglx
Sebastian