sched: Introduce rcuwait
Subsystems (3)
v2 cover sched: Introduce rcuwait
Changes from v1:
- Renamed trywake to wake_up.
- Added Oleg's review tags.
Hi,
Here's an updated version of the pcpu rwsem writer wait/wake changes with the abstractions wanted by Oleg. Patch 1 adds rcuwait (for a lack of better name), and patch 2 trivially makes use of it.
Has survived torture testing, which is actually very handy in this case particularly dealing with equal amount of reader and writer threads.
Applies on top of Linus' tree (4.10-rc3).
Thanks.
Davidlohr Bueso (2):
sched: Introduce rcuwait machinery locking/percpu-rwsem: Replace waitqueue with rcuwait
include/linux/percpu-rwsem.h | 8 +++--- include/linux/rcuwait.h | 63 +++++++++++++++++++++++++++++++++++++++++++ kernel/exit.c | 30 +++++++++++++++++++++ kernel/locking/percpu-rwsem.c | 7 +++-- 4 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 include/linux/rcuwait.h
Signature · 1 line
2.6.6
PATCH v2 1/2 sched: Introduce rcuwait machinery
rcuwait provides support for (single) rcu-safe task wait/wake functionality, with the caveat that it must not be called after exit_notify(), such that we avoid racing with rcu delayed_put_task_struct callbacks, task_struct being rcu unaware in this context -- for which we similarly have task_rcu_dereference() magic, but with different return semantics, which can conflict with the wakeup side.
The interfaces are quite straightforward:
rcuwait_wait_event()rcuwait_wake_up()
More details are in the comments, but it's perhaps worth mentioning at least, that users must provide proper serialization when waiting on a condition, and avoid corrupting a concurrent waiter. Also care must be taken between the task and the condition for when calling the wakeup -- we cannot miss wakeups. When porting users, this is for example, a given when using waitqueues in that everything is done under the q->lock. As such, it can remove sources of non preemptable unbounded work for realtime.
--- include/linux/rcuwait.h | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ kernel/exit.c | 30 +++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 include/linux/rcuwait.h
include/linux/rcuwait.h new file +63 -0
@@ -0,0 +1,63 @@ +#ifndef _LINUX_RCUWAIT_H_ +#define _LINUX_RCUWAIT_H_ + +#include <linux/rcupdate.h> + +/* +* rcuwait provides a way of blocking and waking up a single +* task in an rcu-safe manner; where it is forbidden to use +* after exit_notify(). task_struct is not properly rcu protected, +* unless dealing with rcu-aware lists, ie: find_task_by_*(). +* +* Alternatively we have task_rcu_dereference(), but the return +* semantics have different implications which would break the +* wakeup side. The only time @task is non-nil is when a user is +* blocked (or checking if it needs to) on a condition, and reset +* as soon as we know that the condition has succeeded and are +* awoken. +*/ +struct rcuwait { +struct task_struct *task; +}; + +#define __RCUWAIT_INITIALIZER (name) \ + { .task = NULL, } + +static inline void rcuwait_init (struct rcuwait *w) +{ + w->task =NULL ; +} + +extern void rcuwait_wake_up (struct rcuwait *w); + +/* +* The caller is responsible for locking around rcuwait_wait_event(), +* such that writes to @task are properly serialized. +*/ +#define rcuwait_wait_event (w, condition) \ +({ \ +/* \ +* Complain if we are called after do_exit()/exit_notify(), \ +* as we cannot rely on the rcu critical region for the \ +* wakeup side. \ +*/ \ +WARN_ON (current->exit_state); \ + \ +rcu_assign_pointer ((w)->task, current); \ +for (;;) { \ +/* \ +* Implicit barrier (A) pairs with (B) in \ +* rcuwait_trywake(). \ +*/ \ +set_current_state (TASK_UNINTERRUPTIBLE ); \ +if (condition) \ +break ; \ + \ +schedule (); \ + } \ + \ +WRITE_ONCE ((w)->task,NULL ); \ +__set_current_state (TASK_RUNNING ); \ +}) + +#endif /* _LINUX_RCUWAIT_H_ */
kernel/exit.c +30 -0
@@ -55,6 +55,7 @@#include <linux/shm.h> #include <linux/kcov.h> #include <linux/random.h> +#include <linux/rcuwait.h> #include <linux/uaccess.h> #include <asm/unistd.h>
@@ -282,6 +283,35 @@ struct task_struct *task_rcu_dereference(struct task_struct **ptask)return task; } +void rcuwait_wake_up (struct rcuwait *w) +{ +struct task_struct *task; + +rcu_read_lock (); + +/* +* Order condition vs @task, such that everything prior to the load +* of @task is visible. This is the condition as to why the user called +* rcuwait_trywake() in the first place. Pairs with set_current_state() +* barrier (A) in rcuwait_wait_event(). +* +* WAIT WAKE +* [S] tsk = current [S] cond = true +* MB (A) MB (B) +* [L] cond [L] tsk +*/ +smp_rmb ();/* (B) */ + +/* +* Avoid using task_rcu_dereference() magic as long as we are careful, +* see comment in rcuwait_wait_event() regarding ->exit_state. +*/ + task =rcu_dereference (w->task); +if (task) +wake_up_process (task); +rcu_read_unlock (); +} +struct task_struct *try_get_task_struct (struct task_struct **ptask) {struct task_struct *task;
Signature · 1 line
2.6.6
tip-bot for Davidlohr Bueso
merge bot[tip:locking/core] sched/wait, RCU: Introduce rcuwait machinery
sched/wait, RCU: Introduce rcuwait machinery
Commit-ID: 8f95c90ceb541a38ac16fec48c05142ef1450c25 Gitweb: http://git.kernel.org/tip/8f95c90ceb541a38ac16fec48c05142ef1450c25 Author: Davidlohr Bueso <[email protected]> AuthorDate: Wed, 11 Jan 2017 07:22:25 -0800 Committer: Ingo Molnar <[email protected]> CommitDate: Sat, 14 Jan 2017 11:14:33 +0100
sched/wait, RCU: Introduce rcuwait machinery
rcuwait provides support for (single) RCU-safe task wait/wake functionality, with the caveat that it must not be called after exit_notify(), such that we avoid racing with rcu delayed_put_task_struct callbacks, task_struct being rcu unaware in this context -- for which we similarly have task_rcu_dereference() magic, but with different return semantics, which can conflict with the wakeup side.
The interfaces are quite straightforward:
rcuwait_wait_event()
rcuwait_wake_up()
More details are in the comments, but it's perhaps worth mentioning at least, that users must provide proper serialization when waiting on a condition, and avoid corrupting a concurrent waiter. Also care must be taken between the task and the condition for when calling the wakeup -- we cannot miss wakeups. When porting users, this is for example, a given when using waitqueues in that everything is done under the q->lock. As such, it can remove sources of non preemptable unbounded work for realtime.
--- include/linux/rcuwait.h | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ kernel/exit.c | 30 +++++++++++++++++++++++ 2 files changed, 93 insertions(+)
include/linux/rcuwait.h new file +63 -0
@@ -0,0 +1,63 @@ +#ifndef _LINUX_RCUWAIT_H_ +#define _LINUX_RCUWAIT_H_ + +#include <linux/rcupdate.h> + +/* +* rcuwait provides a way of blocking and waking up a single +* task in an rcu-safe manner; where it is forbidden to use +* after exit_notify(). task_struct is not properly rcu protected, +* unless dealing with rcu-aware lists, ie: find_task_by_*(). +* +* Alternatively we have task_rcu_dereference(), but the return +* semantics have different implications which would break the +* wakeup side. The only time @task is non-nil is when a user is +* blocked (or checking if it needs to) on a condition, and reset +* as soon as we know that the condition has succeeded and are +* awoken. +*/ +struct rcuwait { +struct task_struct *task; +}; + +#define __RCUWAIT_INITIALIZER (name) \ + { .task = NULL, } + +static inline void rcuwait_init (struct rcuwait *w) +{ + w->task =NULL ; +} + +extern void rcuwait_wake_up (struct rcuwait *w); + +/* +* The caller is responsible for locking around rcuwait_wait_event(), +* such that writes to @task are properly serialized. +*/ +#define rcuwait_wait_event (w, condition) \ +({ \ +/* \ +* Complain if we are called after do_exit()/exit_notify(), \ +* as we cannot rely on the rcu critical region for the \ +* wakeup side. \ +*/ \ +WARN_ON (current->exit_state); \ + \ +rcu_assign_pointer ((w)->task, current); \ +for (;;) { \ +/* \ +* Implicit barrier (A) pairs with (B) in \ +* rcuwait_trywake(). \ +*/ \ +set_current_state (TASK_UNINTERRUPTIBLE ); \ +if (condition) \ +break ; \ + \ +schedule (); \ + } \ + \ +WRITE_ONCE ((w)->task,NULL ); \ +__set_current_state (TASK_RUNNING ); \ +}) + +#endif /* _LINUX_RCUWAIT_H_ */
kernel/exit.c +30 -0
@@ -55,6 +55,7 @@#include <linux/shm.h> #include <linux/kcov.h> #include <linux/random.h> +#include <linux/rcuwait.h> #include <linux/uaccess.h> #include <asm/unistd.h>
@@ -282,6 +283,35 @@ retry:return task; } +void rcuwait_wake_up (struct rcuwait *w) +{ +struct task_struct *task; + +rcu_read_lock (); + +/* +* Order condition vs @task, such that everything prior to the load +* of @task is visible. This is the condition as to why the user called +* rcuwait_trywake() in the first place. Pairs with set_current_state() +* barrier (A) in rcuwait_wait_event(). +* +* WAIT WAKE +* [S] tsk = current [S] cond = true +* MB (A) MB (B) +* [L] cond [L] tsk +*/ +smp_rmb ();/* (B) */ + +/* +* Avoid using task_rcu_dereference() magic as long as we are careful, +* see comment in rcuwait_wait_event() regarding ->exit_state. +*/ + task =rcu_dereference (w->task); +if (task) +wake_up_process (task); +rcu_read_unlock (); +} +struct task_struct *try_get_task_struct (struct task_struct **ptask) {struct task_struct *task;
PATCH v2 2/2 locking/percpu-rwsem: Replace waitqueue with rcuwait
In reply to Davidlohr BuesoThe use of any kind of wait queue is an overkill for pcpu-rwsems. While one option would be to use the less heavy simple (swait) flavor, this is still too much for what pcpu-rwsems needs. For one, we do not care about any sort of queuing in that the only (rare) time writers (and readers, for that matter) are queued is when trying to acquire the regular contended rw_sem. There cannot be any further queuing as writers are serialized by the rw_sem in the first place.
Given that percpu_down_write() must not be called after exit_notify(), we can replace the bulky waitqueue with rcuwait such that a writer can wait for its turn to take the lock. As such, we can avoid the queue handling and locking overhead.
--- include/linux/percpu-rwsem.h | 8 ++++---- kernel/locking/percpu-rwsem.c | 7 +++---- 2 files changed, 7 insertions(+), 8 deletions(-)
include/linux/percpu-rwsem.h +4 -4
@@ -4,15 +4,15 @@#include <linux/atomic.h> #include <linux/rwsem.h> #include <linux/percpu.h> -#include <linux/wait.h> +#include <linux/rcuwait.h> #include <linux/rcu_sync.h> #include <linux/lockdep.h> struct percpu_rw_semaphore {struct rcu_sync rss;unsigned int __percpu *read_count; -struct rw_semaphore rw_sem; -wait_queue_head_t writer; +struct rw_semaphore rw_sem;/* slowpath */ +struct rcuwait writer;/* blocked writer */ int readers_block; };
@@ -22,7 +22,7 @@ static struct percpu_rw_semaphore name = { \ .rss = __RCU_SYNC_INITIALIZER(name.rss, RCU_SCHED_SYNC), \ .read_count = &__percpu_rwsem_rc_##name, \ .rw_sem = __RWSEM_INITIALIZER(name.rw_sem), \ - .writer = __WAIT_QUEUE_HEAD_INITIALIZER(name.writer), \ + .writer = __RCUWAIT_INITIALIZER(name.writer), \ }extern int __percpu_down_read (struct percpu_rw_semaphore *,int );
kernel/locking/percpu-rwsem.c +3 -4
@@ -1,7 +1,6 @@#include <linux/atomic.h> #include <linux/rwsem.h> #include <linux/percpu.h> -#include <linux/wait.h> #include <linux/lockdep.h> #include <linux/percpu-rwsem.h> #include <linux/rcupdate.h>
@@ -18,7 +17,7 @@ int __percpu_init_rwsem(struct percpu_rw_semaphore *sem,/* ->rw_sem represents the whole percpu_rw_semaphore for lockdep */ rcu_sync_init (&sem->rss,RCU_SCHED_SYNC );__init_rwsem (&sem->rw_sem, name, rwsem_key); -init_waitqueue_head (&sem->writer); +rcuwait_init (&sem->writer); sem->readers_block =0 ;return 0 ; }
@@ -103,7 +102,7 @@ void __percpu_up_read(struct percpu_rw_semaphore *sem)__this_cpu_dec (*sem->read_count);/* Prod writer to recheck readers_active */ -wake_up (&sem->writer); +rcuwait_wake_up (&sem->writer); }EXPORT_SYMBOL_GPL (__percpu_up_read);
@@ -160,7 +159,7 @@ void percpu_down_write(struct percpu_rw_semaphore *sem)*/ /* Wait for all now active readers to complete. */ -wait_event (sem->writer,readers_active_check (sem)); +rcuwait_wait_event (&sem->writer,readers_active_check (sem)); }EXPORT_SYMBOL_GPL (percpu_down_write);
Signature · 1 line
2.6.6
tip-bot for Davidlohr Bueso
merge bot[tip:locking/core] locking/percpu-rwsem: Replace waitqueue with rcuwait
locking/percpu-rwsem: Replace waitqueue with rcuwait
Commit-ID: 52b94129f274937e4c25dd17b76697664a3c43c9 Gitweb: http://git.kernel.org/tip/52b94129f274937e4c25dd17b76697664a3c43c9 Author: Davidlohr Bueso <[email protected]> AuthorDate: Wed, 11 Jan 2017 07:22:26 -0800 Committer: Ingo Molnar <[email protected]> CommitDate: Sat, 14 Jan 2017 11:14:35 +0100
locking/percpu-rwsem: Replace waitqueue with rcuwait
The use of any kind of wait queue is an overkill for pcpu-rwsems. While one option would be to use the less heavy simple (swait) flavor, this is still too much for what pcpu-rwsems needs. For one, we do not care about any sort of queuing in that the only (rare) time writers (and readers, for that matter) are queued is when trying to acquire the regular contended rw_sem. There cannot be any further queuing as writers are serialized by the rw_sem in the first place.
Given that percpu_down_write() must not be called after exit_notify(), we can replace the bulky waitqueue with rcuwait such that a writer can wait for its turn to take the lock. As such, we can avoid the queue handling and locking overhead.
--- include/linux/percpu-rwsem.h | 8 ++++---- kernel/locking/percpu-rwsem.c | 7 +++---- 2 files changed, 7 insertions(+), 8 deletions(-)
include/linux/percpu-rwsem.h +4 -4
@@ -4,15 +4,15 @@#include <linux/atomic.h> #include <linux/rwsem.h> #include <linux/percpu.h> -#include <linux/wait.h> +#include <linux/rcuwait.h> #include <linux/rcu_sync.h> #include <linux/lockdep.h> struct percpu_rw_semaphore {struct rcu_sync rss;unsigned int __percpu *read_count; -struct rw_semaphore rw_sem; -wait_queue_head_t writer; +struct rw_semaphore rw_sem;/* slowpath */ +struct rcuwait writer;/* blocked writer */ int readers_block; };
@@ -22,7 +22,7 @@ static struct percpu_rw_semaphore name = { \ .rss = __RCU_SYNC_INITIALIZER(name.rss, RCU_SCHED_SYNC), \ .read_count = &__percpu_rwsem_rc_##name, \ .rw_sem = __RWSEM_INITIALIZER(name.rw_sem), \ - .writer = __WAIT_QUEUE_HEAD_INITIALIZER(name.writer), \ + .writer = __RCUWAIT_INITIALIZER(name.writer), \ }extern int __percpu_down_read (struct percpu_rw_semaphore *,int );
kernel/locking/percpu-rwsem.c +3 -4
@@ -1,7 +1,6 @@#include <linux/atomic.h> #include <linux/rwsem.h> #include <linux/percpu.h> -#include <linux/wait.h> #include <linux/lockdep.h> #include <linux/percpu-rwsem.h> #include <linux/rcupdate.h>
@@ -18,7 +17,7 @@ int __percpu_init_rwsem(struct percpu_rw_semaphore *sem,/* ->rw_sem represents the whole percpu_rw_semaphore for lockdep */ rcu_sync_init (&sem->rss,RCU_SCHED_SYNC );__init_rwsem (&sem->rw_sem, name, rwsem_key); -init_waitqueue_head (&sem->writer); +rcuwait_init (&sem->writer); sem->readers_block =0 ;return 0 ; }
@@ -103,7 +102,7 @@ void __percpu_up_read(struct percpu_rw_semaphore *sem)__this_cpu_dec (*sem->read_count);/* Prod writer to recheck readers_active */ -wake_up (&sem->writer); +rcuwait_wake_up (&sem->writer); }EXPORT_SYMBOL_GPL (__percpu_up_read);
@@ -160,7 +159,7 @@ void percpu_down_write(struct percpu_rw_semaphore *sem)*/ /* Wait for all now active readers to complete. */ -wait_event (sem->writer,readers_active_check (sem)); +rcuwait_wait_event (&sem->writer,readers_active_check (sem)); }EXPORT_SYMBOL_GPL (percpu_down_write);