Severe performance regression w/ 4.4+ on Android due to cgroup locking changes
Subsystems (6)
Hey Tejun,
So Dmitry Shmidt recently noticed that with 4.4 based systems we're
seeing quite a bit of performance overhead from __cgroup_procs_write().
With 4.4 tree as it stands, we're seeing __cgroup_procs_write() quite often take 10s of miliseconds to execute (with max times up in the 80ms range).
While with 4.1 it was quite often in the single usec range, and max time values still in in sub-milisecond range.
The majority of these performance regressions seem to come from the locking changes in:
3014dde762f6 ("cgroup: simplify threadgroup locking")
and1ed1328792ff ("sched, cgroup: replace signal_struct->group_rwsem with a global percpu_rwsem")
Dmitry has found that by reverting these two changes (which don't revert easiliy), we can get back down to tens 10-100 usec range for most calls, with max values occasionally spiking to ~18ms.
Those two commits do talk about performance regressions, that were supposedly alleviated by percpu_rwsem changes, but I'm not sure we are seeing this.
In 1ed1328792ff, the commit talks about the write path being a fairly cold path, but with Android I worry this may not actually be the case, as Android uses cpuset cgroups to group tasks into foreground and background tasks, but this means when switching applications, tasks are migrated between cgroups. Putting an additional 80 milisecond delay on this adds potentially visible latencies on task switching.
Reverting those two changes in the Android common.git tree doesn't feel like a good long term solution here, so I was wondering if you had any thoughts on how to further reduce the performance regression here?
All the credit for finding this goes to Dmitry, I just was able to reproduce his results and thoguht we should bring it up for discussion here.
thanks
-john
Peter Zijlstra
maintainerQuoted 21 lines · John Stultz
Hey Tejun,
So Dmitry Shmidt recently noticed that with 4.4 based systems we're
seeing quite a bit of performance overhead from__cgroup_procs_write().With 4.4 tree as it stands, we're seeing
__cgroup_procs_write()quite often take 10s of miliseconds to execute (with max times up in the 80ms range).While with 4.1 it was quite often in the single usec range, and max time values still in in sub-milisecond range.
The majority of these performance regressions seem to come from the locking changes in:
3014dde762f6("cgroup: simplify threadgroup locking")
and1ed1328792ff("sched, cgroup: replacesignal_struct->group_rwsemwith a globalpercpu_rwsem")
Quoted 7 lines
Dmitry has found that by reverting these two changes (which don't revert easiliy), we can get back down to tens 10-100 usec range for most calls, with max values occasionally spiking to ~18ms.
Those two commits do talk about performance regressions, that were supposedly alleviated by
percpu_rwsemchanges, but I'm not sure we are seeing this.
Do you have 'funny' RCU options that quickly force a grace period when you go idle or something?
But yes, it does not surprise me to find this commit is causing problems.
Quoted 30 lines · Peter Zijlstra
Quoted 21 lines · John Stultz
Hey Tejun,
So Dmitry Shmidt recently noticed that with 4.4 based systems we're
seeing quite a bit of performance overhead from__cgroup_procs_write().With 4.4 tree as it stands, we're seeing
__cgroup_procs_write()quite often take 10s of miliseconds to execute (with max times up in the 80ms range).While with 4.1 it was quite often in the single usec range, and max time values still in in sub-milisecond range.
The majority of these performance regressions seem to come from the locking changes in:
3014dde762f6("cgroup: simplify threadgroup locking")
and1ed1328792ff("sched, cgroup: replacesignal_struct->group_rwsemwith a globalpercpu_rwsem")Quoted 7 lines
Dmitry has found that by reverting these two changes (which don't revert easiliy), we can get back down to tens 10-100 usec range for most calls, with max values occasionally spiking to ~18ms.
Those two commits do talk about performance regressions, that were supposedly alleviated by
percpu_rwsemchanges, but I'm not sure we are seeing this.
Quoted 5 lines
Do you have 'funny' RCU options that quickly force a grace period when you go idle or something?
But yes, it does not surprise me to find this commit is causing problems.
Hmmm... Looks like RCU is present both before and after. But please do send along your .config.
Speaking of .config, is CONFIG_PREEMPT=y? If so, does the workload feature preemption and migration? If that is the case, you might be seeing contention on the per-CPU cgroup_threadgroup_rwsem, given that the second patch seems to be adding acquisitions.
Thanx, Paul
Quoted 31 lines · Paul E. McKenney
Quoted 30 lines · Peter Zijlstra
Quoted 28 lines · John Stultz
Hey Tejun,
So Dmitry Shmidt recently noticed that with 4.4 based systems we're
seeing quite a bit of performance overhead from__cgroup_procs_write().With 4.4 tree as it stands, we're seeing
__cgroup_procs_write()quite often take 10s of miliseconds to execute (with max times up in the 80ms range).While with 4.1 it was quite often in the single usec range, and max time values still in in sub-milisecond range.
The majority of these performance regressions seem to come from the locking changes in:
3014dde762f6("cgroup: simplify threadgroup locking")
and1ed1328792ff("sched, cgroup: replacesignal_struct->group_rwsemwith a globalpercpu_rwsem")Dmitry has found that by reverting these two changes (which don't revert easiliy), we can get back down to tens 10-100 usec range for most calls, with max values occasionally spiking to ~18ms.
Those two commits do talk about performance regressions, that were supposedly alleviated by
percpu_rwsemchanges, but I'm not sure we are seeing this.
Quoted 8 lines
Quoted 5 lines
Do you have 'funny' RCU options that quickly force a grace period when you go idle or something?
But yes, it does not surprise me to find this commit is causing problems.
Hmmm... Looks like RCU is present both before and after. But please do send along your .config.
Attached
Quoted 4 lines
Speaking of .config, is
CONFIG_PREEMPT=y? If so, does the workload feature preemption and migration? If that is the case, you might be seeing contention on the per-CPUcgroup_threadgroup_rwsem, given that the second patch seems to be adding acquisitions.
CONFIG_PREEMPT=y is set.
We see this issue during the boot, so it supposes to be enough CPU load to cause preemption and migration.
Quoted 2 lines
Thanx, Paul
Quoted 36 lines · Dmitry Shmidt
Quoted 34 lines · Paul E. McKenney
Quoted 33 lines · Peter Zijlstra
Quoted 21 lines · John Stultz
Hey Tejun,
So Dmitry Shmidt recently noticed that with 4.4 based systems we're
seeing quite a bit of performance overhead from__cgroup_procs_write().With 4.4 tree as it stands, we're seeing
__cgroup_procs_write()quite often take 10s of miliseconds to execute (with max times up in the 80ms range).While with 4.1 it was quite often in the single usec range, and max time values still in in sub-milisecond range.
The majority of these performance regressions seem to come from the locking changes in:
3014dde762f6("cgroup: simplify threadgroup locking")
and1ed1328792ff("sched, cgroup: replacesignal_struct->group_rwsemwith a globalpercpu_rwsem")Quoted 7 lines
Dmitry has found that by reverting these two changes (which don't revert easiliy), we can get back down to tens 10-100 usec range for most calls, with max values occasionally spiking to ~18ms.
Those two commits do talk about performance regressions, that were supposedly alleviated by
percpu_rwsemchanges, but I'm not sure we are seeing this.Do you have 'funny' RCU options that quickly force a grace period when you go idle or something?
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
But yes, it does not surprise me to find this commit is causing problems.
Hmmm... Looks like RCU is present both before and after. But please do send along your .config.
Attached
No funny RCU Kconfig options set -- vanilla preemptible RCU.
Quoted 8 lines
Quoted 4 lines
Speaking of .config, is
CONFIG_PREEMPT=y? If so, does the workload feature preemption and migration? If that is the case, you might be seeing contention on the per-CPUcgroup_threadgroup_rwsem, given that the second patch seems to be adding acquisitions.
CONFIG_PREEMPT=yis set.
We see this issue during the boot, so it supposes to be enough CPU load to cause preemption and migration.
How early during boot? Presumably after the scheduler has started...
Thanx, Paul
Tejun Heo
maintainer(cc'ing Oleg)
Hello,
Quoted 7 lines · John Stultz
So Dmitry Shmidt recently noticed that with 4.4 based systems we're
seeing quite a bit of performance overhead from__cgroup_procs_write().With 4.4 tree as it stands, we're seeing
__cgroup_procs_write()quite often take 10s of miliseconds to execute (with max times up in the 80ms range).
Yikes, that's pretty high. Does this happen only while the system is generally busy or regardless of overall load?
Quoted 19 lines
While with 4.1 it was quite often in the single usec range, and max time values still in in sub-milisecond range.
The majority of these performance regressions seem to come from the locking changes in:
3014dde762f6("cgroup: simplify threadgroup locking")
and1ed1328792ff("sched, cgroup: replacesignal_struct->group_rwsemwith a globalpercpu_rwsem")Dmitry has found that by reverting these two changes (which don't revert easiliy), we can get back down to tens 10-100 usec range for most calls, with max values occasionally spiking to ~18ms.
Those two commits do talk about performance regressions, that were supposedly alleviated by
percpu_rwsemchanges, but I'm not sure we are seeing this.
Quoted 6 lines
In
1ed1328792ff, the commit talks about the write path being a fairly cold path, but with Android I worry this may not actually be the case, as Android uses cpuset cgroups to group tasks into foreground and background tasks, but this means when switching applications, tasks are migrated between cgroups. Putting an additional 80 milisecond delay on this adds potentially visible latencies on task switching.
Switching between foreground and background isn't a hot path. It's human initiated operations after all. It taking 80 msecs sure is problematic but I'm skeptical that this is from actual contention given that the only reader side holders are fork and exit paths.
Quoted 4 lines
Reverting those two changes in the Android common.git tree doesn't feel like a good long term solution here, so I was wondering if you had any thoughts on how to further reduce the performance regression here?
One interesting thing to try would be replacing it with a regular non-percpu rwsem and see how it behaves. That should easily tell us whether this is from actual contention or artifacts from percpu_rwsem implementation.
Thanks.
Signature · 1 line
tejun
Tejun Heo
maintainerQuoted 4 lines · Tejun Heo
One interesting thing to try would be replacing it with a regular non-percpu rwsem and see how it behaves. That should easily tell us whether this is from actual contention or artifacts from
percpu_rwsemimplementation.
So, something like the following. Can you please see whether this makes any difference?
Thanks.
include/linux/cgroup-defs.h +4 -4
@@ -14,7 +14,7 @@#include <linux/mutex.h> #include <linux/rcupdate.h> #include <linux/percpu-refcount.h> -#include <linux/percpu-rwsem.h> +#include <linux/rwsem.h> #include <linux/workqueue.h> #ifdef CONFIG_CGROUPS
@@ -518,7 +518,7 @@ struct cgroup_subsys {unsigned int depends_on; }; -extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem; +extern struct rw_semaphore cgroup_threadgroup_rwsem;/** * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups
@@ -529,7 +529,7 @@ extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem;*/ static inline void cgroup_threadgroup_change_begin (struct task_struct *tsk) { -percpu_down_read (&cgroup_threadgroup_rwsem); +down_read (&cgroup_threadgroup_rwsem); }/**
@@ -541,7 +541,7 @@ static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)*/ static inline void cgroup_threadgroup_change_end (struct task_struct *tsk) { -percpu_up_read (&cgroup_threadgroup_rwsem); +up_read (&cgroup_threadgroup_rwsem); }#else /* CONFIG_CGROUPS */
kernel/cgroup.c +6 -6
@@ -113,7 +113,7 @@ static DEFINE_SPINLOCK(cgroup_file_kn_lock);*/ static DEFINE_SPINLOCK (release_agent_path_lock); -struct percpu_rw_semaphore cgroup_threadgroup_rwsem; +DECLARE_RWSEM (cgroup_threadgroup_rwsem);#define cgroup_assert_mutex_or_rcu_locked () \ RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
@@ -2899,7 +2899,7 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,if (!cgrp)return -ENODEV ; -percpu_down_write (&cgroup_threadgroup_rwsem); +down_write (&cgroup_threadgroup_rwsem);rcu_read_lock ();if (pid) { tsk =find_task_by_vpid (pid);
@@ -2937,7 +2937,7 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf, out_unlock_rcu:rcu_read_unlock (); out_unlock_threadgroup: -percpu_up_write (&cgroup_threadgroup_rwsem); +up_write (&cgroup_threadgroup_rwsem);for_each_subsys (ss, ssid)if (ss->post_attach) ss->post_attach ();
@@ -3077,7 +3077,7 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)lockdep_assert_held (&cgroup_mutex); -percpu_down_write (&cgroup_threadgroup_rwsem); +down_write (&cgroup_threadgroup_rwsem);/* look up all csses currently attached to @cgrp's subtree */ spin_lock_bh (&css_set_lock);
@@ -3112,7 +3112,7 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp) ret =cgroup_taskset_migrate (&tset, cgrp->root); out_finish:cgroup_migrate_finish (&preloaded_csets); -percpu_up_write (&cgroup_threadgroup_rwsem); +up_write (&cgroup_threadgroup_rwsem);return ret; }
@@ -5601,7 +5601,7 @@ int __init cgroup_init(void)int ssid;BUILD_BUG_ON (CGROUP_SUBSYS_COUNT >16 ); -BUG_ON (percpu_init_rwsem (&cgroup_threadgroup_rwsem)); +//BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem)); BUG_ON (cgroup_init_cftypes (NULL , cgroup_dfl_base_files));BUG_ON (cgroup_init_cftypes (NULL , cgroup_legacy_base_files));
Quoted 8 lines · Tejun Heo
Quoted 4 lines · Tejun Heo
One interesting thing to try would be replacing it with a regular non-percpu rwsem and see how it behaves. That should easily tell us whether this is from actual contention or artifacts from
percpu_rwsemimplementation.So, something like the following. Can you please see whether this makes any difference?
Yea. So this brings it down for me closer to what we're seeing with the Dmitry's patch reverting the two problematic commits, usually 10-50us with one early spike at 18ms.
thanks
-john
Tejun Heo
maintainerHello, John.
Quoted 7 lines · John Stultz
Quoted 6 lines · Tejun Heo
Quoted 4 lines · Tejun Heo
One interesting thing to try would be replacing it with a regular non-percpu rwsem and see how it behaves. That should easily tell us whether this is from actual contention or artifacts from
percpu_rwsemimplementation.
Quoted 6 lines
Quoted 2 lines
So, something like the following. Can you please see whether this makes any difference?
Yea. So this brings it down for me closer to what we're seeing with the Dmitry's patch reverting the two problematic commits, usually 10-50us with one early spike at 18ms.
So, it's a percpu rwsem issue then. I haven't really followed the perpcpu rwsem changes closely. Oleg, are multi-milisec delay expected on down write expected with the current implementation of percpu_rwsem?
Thanks.
Signature · 1 line
tejun
Peter Zijlstra
maintainerQuoted 13 lines · Tejun Heo
Hello, John.
Quoted 10 lines · John Stultz
Quoted 8 lines · Tejun Heo
Quoted 4 lines · Tejun Heo
One interesting thing to try would be replacing it with a regular non-percpu rwsem and see how it behaves. That should easily tell us whether this is from actual contention or artifacts from
percpu_rwsemimplementation.So, something like the following. Can you please see whether this makes any difference?
Quoted 8 lines
Quoted 3 lines
Yea. So this brings it down for me closer to what we're seeing with the Dmitry's patch reverting the two problematic commits, usually 10-50us with one early spike at 18ms.
So, it's a percpu rwsem issue then. I haven't really followed the perpcpu rwsem changes closely. Oleg, are multi-milisec delay expected on down write expected with the current implementation of
percpu_rwsem?
There is a synchronize_sched() in there, so sorta. That thing is heavily geared towards readers, as is the only 'sane' choice for global locks.
Tejun Heo
maintainerHello,
Quoted 7 lines · Peter Zijlstra
Quoted 4 lines
So, it's a percpu rwsem issue then. I haven't really followed the perpcpu rwsem changes closely. Oleg, are multi-milisec delay expected on down write expected with the current implementation of
percpu_rwsem?There is a
synchronize_sched()in there, so sorta. That thing is heavily geared towards readers, as is the only 'sane' choice for global locks.
It used to use the expedited variant until 001dac627ff3 ("locking/percpu-rwsem: Make use of the rcu_sync infrastructure"), so it might have been okay before then.
Skewing towards readers is fine but tens of millisecs of delays definitely can't fit some use cases. There's a balance between CPU overhead and latency here. If down writes are infrequent enough, it doesn't make sense to aggressively trade off latency for lower processing overhead for some use cases.
The options that I can see are
1. Somehow make percpu_rwsem's write behavior more responsive in a way which is acceptable all use cases. This would be great but probably impossible.
2. Add a fast-writer option to percpu_rwsem so that users which care about write latency can opt in for higher processing overhead for lower latency.
3. Implement a custom per-cpu locking construct for the particular use case.
#3 would inherently be similar to #2 in its behavior. If #1 isn't possible, #2 looks like the best course of action.
Thanks.
Signature · 1 line
tejun
Peter Zijlstra
maintainerQuoted 6 lines · Tejun Heo
Quoted 2 lines
There is a
synchronize_sched()in there, so sorta. That thing is heavily geared towards readers, as is the only 'sane' choice for global locks.It used to use the expedited variant until
001dac627ff3("locking/percpu-rwsem: Make use of thercu_syncinfrastructure"), so it might have been okay before then.
Right, but expedited stuff sprays IPIs around the entire system. That's stuff other people complain about.
Quoted 9 lines
The options that I can see are
1. Somehow make percpu_rwsem's write behavior more responsive in a way which is acceptable all use cases. This would be great but probably impossible.
2. Add a fast-writer option to
percpu_rwsemso that users which care about write latency can opt in for higher processing overhead for lower latency.
So, IIRC, the trade-off is a full memory barrier in read_lock and read_unlock() vs sync_sched() in write.
Full memory barriers are expensive and while the combined cost might well exceed the cost of the sync_sched() it doesn't suffer the latency issues.
Not sure if we can frob the two in a single codebase, but I can have a poke if Oleg or Paul doesn't beat me to it.
Tejun Heo
maintainerQuoted 7 lines · Peter Zijlstra
On Wed, Jul 13, 2016 at 04:39:44PM -0400, Tejun Heo wrote:
So, IIRC, the trade-off is a full memory barrier inread_lockandread_unlock()vssync_sched()in write.Full memory barriers are expensive and while the combined cost might well exceed the cost of the
sync_sched()it doesn't suffer the latency issues.
Given the way read side is used for percpu_rwsem, full memory barrier on reader side shouldn't matter at all. The paths are not *that* hot.
Quoted 2 lines
Not sure if we can frob the two in a single codebase, but I can have a poke if Oleg or Paul doesn't beat me to it.
At the simplest, it can be rwsem equivalence of lglock.
Signature · 1 line
tejun
Quoted 11 lines · Peter Zijlstra
Quoted 6 lines · Tejun Heo
Quoted 2 lines
There is a
synchronize_sched()in there, so sorta. That thing is heavily geared towards readers, as is the only 'sane' choice for global locks.It used to use the expedited variant until
001dac627ff3("locking/percpu-rwsem: Make use of thercu_syncinfrastructure"), so it might have been okay before then.Right, but expedited stuff sprays IPIs around the entire system. That's stuff other people complain about.
Do anyone other than the non-NO_HZ_FULL low-latency guys and the -rt guys care?
Quoted 13 lines
Quoted 9 lines
The options that I can see are
1. Somehow make percpu_rwsem's write behavior more responsive in a way which is acceptable all use cases. This would be great but probably impossible.
2. Add a fast-writer option to
percpu_rwsemso that users which care about write latency can opt in for higher processing overhead for lower latency.So, IIRC, the trade-off is a full memory barrier in
read_lockandread_unlock()vssync_sched()in write.
Quoted 6 lines
Full memory barriers are expensive and while the combined cost might well exceed the cost of the
sync_sched()it doesn't suffer the latency issues.Not sure if we can frob the two in a single codebase, but I can have a poke if Oleg or Paul doesn't beat me to it.
Take the patch that I just sent out and make the choice of normal vs. expedited depend on CONFIG_PREEMPT_RT or whatever the -rt guys are calling it these days. Is there a low-latency Kconfig option other than CONFIG_NO_HZ_FULL?
The memory-barrier approach can definitely be made to work, but is going to be more complex due to the need to wait for readers.
Thanx, Paul
Tejun Heo
maintainerQuoted 4 lines · Paul E. McKenney
Take the patch that I just sent out and make the choice of normal vs. expedited depend on
CONFIG_PREEMPT_RTor whatever the -rt guys are calling it these days. Is there a low-latencyKconfigoption other thanCONFIG_NO_HZ_FULL?
Sounds like a plan to me.
Thanks.
Signature · 1 line
tejun
Quoted 7 lines · Tejun Heo
Quoted 4 lines · Paul E. McKenney
Take the patch that I just sent out and make the choice of normal vs. expedited depend on
CONFIG_PREEMPT_RTor whatever the -rt guys are calling it these days. Is there a low-latencyKconfigoption other thanCONFIG_NO_HZ_FULL?Sounds like a plan to me.
I like the way we like each other's idea. Mutually assured laziness? ;-)
Thanx, Paul
Quoted 10 lines · Paul E. McKenney
Quoted 7 lines · Tejun Heo
Quoted 4 lines · Paul E. McKenney
Take the patch that I just sent out and make the choice of normal vs. expedited depend on
CONFIG_PREEMPT_RTor whatever the -rt guys are calling it these days. Is there a low-latencyKconfigoption other thanCONFIG_NO_HZ_FULL?Sounds like a plan to me.
I like the way we like each other's idea. Mutually assured laziness? ;-)
But here is what mine might look like. Untested, probably does not even build. Note that the default is -no- expediting, use the rcusync.expedited kernel parameter to enable it.
Thanx, Paul
------------------------------------------------------------------------
Documentation/kernel-parameters.txt +5 -0
@@ -3229,6 +3229,11 @@ bytes respectively. Such letter suffixes can also be entirely omitted. energy efficiency by requiring that the kthreads periodically wake up to do the polling. + rcusync.expedited [KNL] + Specify that the rcusync mechanism use expedited + grace periods. As of mid-2016, this affects + per-CPU rwsems. + rcutree.blimit= [KNL] Set maximum number of finished RCU callbacks to process in one batch.
kernel/rcu/sync.c +23 -1
@@ -22,6 +22,14 @@#include <linux/rcu_sync.h> #include <linux/sched.h> +#include <linux/moduleparam.h> +#include <linux/module.h> + +MODULE_ALIAS ("rcusync" ); +#ifdef MODULE_PARAM_PREFIX +#undef MODULE_PARAM_PREFIX +#endif +#define MODULE_PARAM_PREFIX "rcusync."#ifdef CONFIG_PROVE_RCU #define __INIT_HELD (func) .held = func,
@@ -29,7 +37,7 @@#define __INIT_HELD (func)#endif -static const struct { +static struct { void (*sync)(void); void (*call)(struct rcu_head *, void (*)(struct rcu_head *)); void (*wait)(void);
@@ -62,6 +70,20 @@ enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };#define rss_lock gp_wait.lock +static bool expedited; +module_param (expedited, bool,0444 ); + +static int __init rcu_sync_early_init (void ) +{ +if (expedited) { + gp_ops[RCU_SYNC ].sync = synchronize_rcu_expedited; + gp_ops[RCU_SCHED_SYNC ].sync = synchronize_sched_expedited; + gp_ops[RCU_BH_SYNC ].sync = synchronize_rcu_bh_expedited; + } +return 0 ; +} +early_initcall (rcu_sync_early_init); +#ifdef CONFIG_PROVE_RCU void rcu_sync_lockdep_assert (struct rcu_sync *rsp) {
Quoted 8 lines · Paul E. McKenney
Quoted 7 lines · Paul E. McKenney
Quoted 6 lines · Tejun Heo
Quoted 4 lines · Paul E. McKenney
Take the patch that I just sent out and make the choice of normal vs. expedited depend on
CONFIG_PREEMPT_RTor whatever the -rt guys are calling it these days. Is there a low-latencyKconfigoption other thanCONFIG_NO_HZ_FULL?
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Sounds like a plan to me.
I like the way we like each other's idea. Mutually assured laziness? ;-)
But here is what mine might look like. Untested, probably does not even build. Note that the default is -no- expediting, use the rcusync.expedited kernel parameter to enable it.
I was working on something similar, but using a config option. Would adding a config option for the default make sense here, since I'd probably prefer to have one less thing to always specify on the cmdline?
thanks
-john
Quoted 14 lines · John Stultz
Quoted 12 lines · Paul E. McKenney
Quoted 10 lines · Paul E. McKenney
Quoted 7 lines · Tejun Heo
Quoted 4 lines · Paul E. McKenney
Take the patch that I just sent out and make the choice of normal vs. expedited depend on
CONFIG_PREEMPT_RTor whatever the -rt guys are calling it these days. Is there a low-latencyKconfigoption other thanCONFIG_NO_HZ_FULL?Sounds like a plan to me.
I like the way we like each other's idea. Mutually assured laziness? ;-)
Quoted 8 lines
Quoted 3 lines
But here is what mine might look like. Untested, probably does not even build. Note that the default is -no- expediting, use the rcusync.expedited kernel parameter to enable it.
I was working on something similar, but using a config option. Would adding a config option for the default make sense here, since I'd probably prefer to have one less thing to always specify on the cmdline?
As long as you don't mind it depending on CONFIG_RCU_EXPERT, no problem.
Perhaps like the following, on top of the previous patch?
Or if you are going to put it in defconfig files only, I can make it so that it isn't changeable at menuconfig time.
Thanx, Paul
------------------------------------------------------------------------
commit 57d8274b6906fad135a74beb59d4a99552659686
Author: Paul E. McKenney <[email protected]> Date: Wed Jul 13 15:13:31 2016 -0700
rcu: Provide RCUSYNC_EXPEDITE option for rcusync.expedited default
This commit provides an RCUSYNC_EXPEDITE Kconfig option that specifies the default value for the rcusync.expedited kernel parameter. This makes it easier to use rcusync.expedited functionality in cases where specifying kernel boot parameters should be avoided.
init/Kconfig +17 -0
@@ -782,6 +782,23 @@ config RCU_EXPEDITE_BOOT Accept the default if unsure. +config RCUSYNC_EXPEDITE + bool "Expedite rcusync operations for per-CPU rwsems" + depends on RCU_EXPERT + default n + help + Use this option to speed up per-CPU rwsem operations that are + in turn used by some cgroups operations. However, note well + that specifying this option will enable expedited RCU grace + periods. These expedited grace periods can in turn introduce + OS jitter, which can interfere with real-time, low-latency, + HPC, and userspace-polling RDMA workloads. That said, this + OS jitter will not affect CPUs that are in nohz_full mode for + the duration of the per-CPU rwsem operation in question. + + Say Y here if you want fast cgroups at the expense of OS jitter. + Say N here if you are unsure. + endmenu # "RCU Subsystem" config BUILD_BIN2C
kernel/rcu/sync.c +1 -1
@@ -70,7 +70,7 @@ enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };#define rss_lock gp_wait.lock -static bool expedited; +static bool expedited =IS_ENABLED (CONFIG_RCUSYNC_EXPEDITE );module_param (expedited, bool,0444 );static int __init rcu_sync_early_init (void )
Quoted 24 lines · Paul E. McKenney
Quoted 14 lines · John Stultz
Quoted 12 lines · Paul E. McKenney
Quoted 10 lines · Paul E. McKenney
Quoted 7 lines · Tejun Heo
Quoted 4 lines · Paul E. McKenney
Take the patch that I just sent out and make the choice of normal vs. expedited depend on
CONFIG_PREEMPT_RTor whatever the -rt guys are calling it these days. Is there a low-latencyKconfigoption other thanCONFIG_NO_HZ_FULL?Sounds like a plan to me.
I like the way we like each other's idea. Mutually assured laziness? ;-)
Quoted 8 lines
Quoted 3 lines
But here is what mine might look like. Untested, probably does not even build. Note that the default is -no- expediting, use the rcusync.expedited kernel parameter to enable it.
I was working on something similar, but using a config option. Would adding a config option for the default make sense here, since I'd probably prefer to have one less thing to always specify on the cmdline?
Quoted 6 lines
As long as you don't mind it depending on
CONFIG_RCU_EXPERT, no problem.Perhaps like the following, on top of the previous patch?
Or if you are going to put it in defconfig files only, I can make it so that it isn't changeable at menuconfig time.
I think having it discoverable via menuconfig is useful, and I've got no objections to it being under RCU_EXPERT
(assuming I don't badly muck up my RCU settings accidentally :).
I only had that one nit about maybe wanting to put something in dmesg when we're using the expedited methods.
But otherwise both patches look great and are working well!
Do you mind marking them both for stable 4.4+?
Also, do make sure Dmitry gets the reported-by credit for the first patch.
thanks
-john
Quoted 28 lines · John Stultz
Quoted 26 lines · Paul E. McKenney
Quoted 14 lines · John Stultz
Quoted 12 lines · Paul E. McKenney
Quoted 10 lines · Paul E. McKenney
Quoted 7 lines · Tejun Heo
Quoted 4 lines · Paul E. McKenney
Take the patch that I just sent out and make the choice of normal vs. expedited depend on
CONFIG_PREEMPT_RTor whatever the -rt guys are calling it these days. Is there a low-latencyKconfigoption other thanCONFIG_NO_HZ_FULL?Sounds like a plan to me.
I like the way we like each other's idea. Mutually assured laziness? ;-)
Quoted 8 lines
Quoted 3 lines
But here is what mine might look like. Untested, probably does not even build. Note that the default is -no- expediting, use the rcusync.expedited kernel parameter to enable it.
I was working on something similar, but using a config option. Would adding a config option for the default make sense here, since I'd probably prefer to have one less thing to always specify on the cmdline?
As long as you don't mind it depending on
CONFIG_RCU_EXPERT, no problem.
Quoted 8 lines
Quoted 4 lines
Perhaps like the following, on top of the previous patch?
Or if you are going to put it in defconfig files only, I can make it so that it isn't changeable at menuconfig time.
I think having it discoverable via menuconfig is useful, and I've got no objections to it being under
RCU_EXPERT
(assuming I don't badly muck up my RCU settings accidentally :).
But isn't mucking up your RCU settings half of the fun? ;-)
Quoted 2 lines
I only had that one nit about maybe wanting to put something in dmesg when we're using the expedited methods.
Updated, please see below.
Quoted 3 lines
But otherwise both patches look great and are working well!
Do you mind marking them both for stable 4.4+?
OK, looks like it does qualify in the "fix a notable performance or interactivity issue" category.
Quoted 4 lines
Tested-by John Stultz <[email protected]>Acked-by John Stultz <[email protected]>Also, do make sure Dmitry gets the reported-by credit for the first patch.
Done! The updated first patch is below, and the second will follow.
Thanx, Paul
------------------------------------------------------------------------
commit 59435eb836ee73b30ed6ada525125b67b4029321
Author: Paul E. McKenney <[email protected]> Date: Wed Jul 13 14:43:46 2016 -0700
rcu: Provide rcusync.expedited kernel boot parameter
Dmitry Shmidt and John Stultz noticed that __cgroup_procs_write() sometimes incurred excessive overheads, ranging up into the tens of milliseconds. Further testing confirmed speculation that this was due to synchronize_sched() within rcusync being invoked by per-CPU rwsems. This testing also showed that substituting synchronize_sched_expedited() for synchronize_sched() greatly reduced the overheads to below 200 microseconds, with the occasional excursion into the low single digits worth of milliseconds.
This commit therefore provides a rcusync.expedited kernel boot parameter that causes rcusync to use expedited grace-period primitives.
Documentation/kernel-parameters.txt +5 -0
@@ -3229,6 +3229,11 @@ bytes respectively. Such letter suffixes can also be entirely omitted. energy efficiency by requiring that the kthreads periodically wake up to do the polling. + rcusync.expedited [KNL] + Specify that the rcusync mechanism use expedited + grace periods. As of mid-2016, this affects + per-CPU rwsems. + rcutree.blimit= [KNL] Set maximum number of finished RCU callbacks to process in one batch.
kernel/rcu/sync.c +25 -2
@@ -22,6 +22,14 @@#include <linux/rcu_sync.h> #include <linux/sched.h> +#include <linux/moduleparam.h> +#include <linux/module.h> + +MODULE_ALIAS ("rcusync" ); +#ifdef MODULE_PARAM_PREFIX +#undef MODULE_PARAM_PREFIX +#endif +#define MODULE_PARAM_PREFIX "rcusync."#ifdef CONFIG_PROVE_RCU #define __INIT_HELD (func) .held = func,
@@ -29,14 +37,14 @@#define __INIT_HELD (func)#endif -static const struct { +static struct {void (*sync)(void );void (*call)(struct rcu_head *,void (*)(struct rcu_head *));void (*wait)(void );#ifdef CONFIG_PROVE_RCU int (*held)(void );#endif -} gp_ops[] = { +} gp_ops[]__read_mostly = { [RCU_SYNC ] = { .sync = synchronize_rcu, .call = call_rcu,
@@ -62,6 +70,21 @@ enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };#define rss_lock gp_wait.lock +static bool expedited; +module_param (expedited, bool,0444 ); + +static int __init rcu_sync_early_init (void ) +{ +if (expedited) { +pr_info ("RCU_SYNC: Expedited operation in effect.\n" ); + gp_ops[RCU_SYNC ].sync = synchronize_rcu_expedited; + gp_ops[RCU_SCHED_SYNC ].sync = synchronize_sched_expedited; + gp_ops[RCU_BH_SYNC ].sync = synchronize_rcu_bh_expedited; + } +return 0 ; +} +early_initcall (rcu_sync_early_init); +#ifdef CONFIG_PROVE_RCU void rcu_sync_lockdep_assert (struct rcu_sync *rsp) {
Quoted 52 lines · Paul E. McKenney
Quoted 28 lines · John Stultz
Quoted 26 lines · Paul E. McKenney
Quoted 14 lines · John Stultz
Quoted 12 lines · Paul E. McKenney
Quoted 10 lines · Paul E. McKenney
Quoted 7 lines · Tejun Heo
Quoted 4 lines · Paul E. McKenney
Take the patch that I just sent out and make the choice of normal vs. expedited depend on
CONFIG_PREEMPT_RTor whatever the -rt guys are calling it these days. Is there a low-latencyKconfigoption other thanCONFIG_NO_HZ_FULL?Sounds like a plan to me.
I like the way we like each other's idea. Mutually assured laziness? ;-)
Quoted 8 lines
Quoted 3 lines
But here is what mine might look like. Untested, probably does not even build. Note that the default is -no- expediting, use the rcusync.expedited kernel parameter to enable it.
I was working on something similar, but using a config option. Would adding a config option for the default make sense here, since I'd probably prefer to have one less thing to always specify on the cmdline?
As long as you don't mind it depending on
CONFIG_RCU_EXPERT, no problem.Quoted 8 lines
Quoted 4 lines
Perhaps like the following, on top of the previous patch?
Or if you are going to put it in defconfig files only, I can make it so that it isn't changeable at menuconfig time.
I think having it discoverable via menuconfig is useful, and I've got no objections to it being under
RCU_EXPERT
(assuming I don't badly muck up my RCU settings accidentally :).But isn't mucking up your RCU settings half of the fun? ;-)
Quoted 2 lines
I only had that one nit about maybe wanting to put something in dmesg when we're using the expedited methods.
Updated, please see below.
Quoted 3 lines
But otherwise both patches look great and are working well!
Do you mind marking them both for stable 4.4+?
OK, looks like it does qualify in the "fix a notable performance or interactivity issue" category.
Quoted 6 lines
Quoted 4 lines
Tested-by John Stultz <[email protected]>Acked-by John Stultz <[email protected]>Also, do make sure Dmitry gets the reported-by credit for the first patch.
Done! The updated first patch is below, and the second will follow.
As promised/threatened...
Thanx, Paul
------------------------------------------------------------------------
commit b4edebb8f5664a3a51be1e3ff3d7f1cb2d3d5c88
Author: Paul E. McKenney <[email protected]> Date: Wed Jul 13 15:13:31 2016 -0700
rcu: Provide RCUSYNC_EXPEDITE option for rcusync.expedited default
This commit provides an RCUSYNC_EXPEDITE Kconfig option that specifies the default value for the rcusync.expedited kernel parameter. This makes it easier to use rcusync.expedited functionality in cases where specifying kernel boot parameters should be avoided.
init/Kconfig +17 -0
@@ -782,6 +782,23 @@ config RCU_EXPEDITE_BOOT Accept the default if unsure. +config RCUSYNC_EXPEDITE + bool "Expedite rcusync operations for per-CPU rwsems" + depends on RCU_EXPERT + default n + help + Use this option to speed up per-CPU rwsem operations that are + in turn used by some cgroups operations. However, note well + that specifying this option will enable expedited RCU grace + periods. These expedited grace periods can in turn introduce + OS jitter, which can interfere with real-time, low-latency, + HPC, and userspace-polling RDMA workloads. That said, this + OS jitter will not affect CPUs that are in nohz_full mode for + the duration of the per-CPU rwsem operation in question. + + Say Y here if you want fast cgroups at the expense of OS jitter. + Say N here if you are unsure. + endmenu # "RCU Subsystem" config BUILD_BIN2C
kernel/rcu/sync.c +1 -1
@@ -70,7 +70,7 @@ enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };#define rss_lock gp_wait.lock -static bool expedited; +static bool expedited =IS_ENABLED (CONFIG_RCUSYNC_EXPEDITE );module_param (expedited, bool,0444 );static int __init rcu_sync_early_init (void )
Tejun Heo
maintainerHello,
Quoted 11 lines · Paul E. McKenney
commit
b4edebb8f5664a3a51be1e3ff3d7f1cb2d3d5c88Author: Paul E. McKenney <[email protected]> Date: Wed Jul 13 15:13:31 2016 -0700rcu: Provide
RCUSYNC_EXPEDITEoption for rcusync.expedited defaultThis commit provides an
RCUSYNC_EXPEDITEKconfigoption that specifies the default value for the rcusync.expedited kernel parameter. This makes it easier to use rcusync.expedited functionality in cases where specifying kernel boot parameters should be avoided.
Quoted 5 lines
Reported-by John Stultz <[email protected]>Signed-off-by Paul E. McKenney <[email protected]>Tested-by John Stultz <[email protected]>Acked-by John Stultz <[email protected]>Cc <[email protected]> # 4.4.x-
I think it probably makes sense to make this the default on !RT at least with a separate patch w/o stable cc'd. While most use cases will be fine with the latency on write path, it also means that the reader side is blocked for the duration which can hurt. rwsem implies a lot more readers and thus more read lock operations than writes. It's weird to trade off higher latency for lower cpu usage when it would also slow down all readers.
Thanks.
Signature · 1 line
tejun
Quoted 20 lines · Tejun Heo
Hello,
Quoted 11 lines · Paul E. McKenney
commit
b4edebb8f5664a3a51be1e3ff3d7f1cb2d3d5c88Author: Paul E. McKenney <[email protected]> Date: Wed Jul 13 15:13:31 2016 -0700rcu: Provide
RCUSYNC_EXPEDITEoption for rcusync.expedited defaultThis commit provides an
RCUSYNC_EXPEDITEKconfigoption that specifies the default value for the rcusync.expedited kernel parameter. This makes it easier to use rcusync.expedited functionality in cases where specifying kernel boot parameters should be avoided.Quoted 5 lines
Reported-by John Stultz <[email protected]>Signed-off-by Paul E. McKenney <[email protected]>Tested-by John Stultz <[email protected]>Acked-by John Stultz <[email protected]>Cc <[email protected]> # 4.4.x-
Quoted 7 lines
I think it probably makes sense to make this the default on !RT at least with a separate patch w/o stable cc'd. While most use cases will be fine with the latency on write path, it also means that the reader side is blocked for the duration which can hurt. rwsem implies a lot more readers and thus more read lock operations than writes. It's weird to trade off higher latency for lower cpu usage when it would also slow down all readers.
NAK, no expedited muck by default. There's more than just RT that doesn't like IPI sprays.
Tejun Heo
maintainerQuoted 10 lines · Peter Zijlstra
Quoted 7 lines
I think it probably makes sense to make this the default on !RT at least with a separate patch w/o stable cc'd. While most use cases will be fine with the latency on write path, it also means that the reader side is blocked for the duration which can hurt. rwsem implies a lot more readers and thus more read lock operations than writes. It's weird to trade off higher latency for lower cpu usage when it would also slow down all readers.
NAK, no expedited muck by default. There's more than just RT that doesn't like IPI sprays.
Can you elaborate? If that's the case, we have the wrong implemention for percpu-rwsem where very long delays for writers induce the same level of delays to all readers. If expedited by default isn't workable, we should move away from rcu_sync for percpu_rwsem.
Thanks.
Signature · 1 line
tejun
Peter Zijlstra
maintainerQuoted 9 lines · Tejun Heo
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines
I think it probably makes sense to make this the default on !RT at least with a separate patch w/o stable cc'd. While most use cases will be fine with the latency on write path, it also means that the reader side is blocked for the duration which can hurt. rwsem implies a lot more readers and thus more read lock operations than writes. It's weird to trade off higher latency for lower cpu usage when it would also slow down all readers.
Quoted 4 lines
Quoted 2 lines
NAK, no expedited muck by default. There's more than just RT that doesn't like IPI sprays.
Can you elaborate?
HPC doesn't use RT but still wants to minimize jitter such that all CPUs complete their work ASAP. They use barriers to wait on the slowest CPU to complete work.
Sending random interrupts disturbs cache and other stuff and delays things unnecessarily.
Same with RDMA (or other) userspace poll loops which want minimal latency, they too don't use RT, but also very much want to avoid the kernel poking at them.
Many of this could eventually use NOHZ_FULL, but I'm not sure all of that is suitable.
In general its very bad form to spray interrupts just because and we've spend a lot of effort to reduce that.
Quoted 4 lines
If that's the case, we have the wrong implemention
for percpu-rwsem where very long delays for writers induce the same level of delays to all readers. If expedited by default isn't workable, we should move away fromrcu_syncforpercpu_rwsem.
Just because your usecase doesn't like it, doesn't mean its not good. Its a perfectly fine implementation for uprobes for example. The addition/removal of uprobes is extremely rare, as global writers should be.
And no, the writer delay isn't observed by the readers, those will continue 'undisturbed' for most of it.
Tejun Heo
maintainerQuoted 5 lines · Peter Zijlstra
Quoted 4 lines
If that's the case, we have the wrong implemention
for percpu-rwsem where very long delays for writers induce the same level of delays to all readers. If expedited by default isn't workable, we should move away fromrcu_syncforpercpu_rwsem.
Quoted 7 lines
Just because your usecase doesn't like it, doesn't mean its not good. Its a perfectly fine implementation for uprobes for example. The addition/removal of uprobes is extremely rare, as global writers should be.
And no, the writer delay isn't observed by the readers, those will continue 'undisturbed' for most of it.
How? While write lock is pending, no new reader is allowed. If reader ops are high frequency, they will surely get affected. It just isn't a good design to inject RCU grace period synchronously into a hot path.
Thanks.
Signature · 1 line
tejun
Tejun Heo
maintainerQuoted 11 lines · Tejun Heo
Quoted 10 lines · Peter Zijlstra
Quoted 4 lines
If that's the case, we have the wrong implemention
for percpu-rwsem where very long delays for writers induce the same level of delays to all readers. If expedited by default isn't workable, we should move away fromrcu_syncforpercpu_rwsem.Just because your usecase doesn't like it, doesn't mean its not good. Its a perfectly fine implementation for uprobes for example. The addition/removal of uprobes is extremely rare, as global writers should be.
Quoted 7 lines
Quoted 2 lines
And no, the writer delay isn't observed by the readers, those will continue 'undisturbed' for most of it.
How? While write lock is pending, no new reader is allowed. If reader ops are high frequency, they will surely get affected. It just isn't a good design to inject RCU grace period synchronously into a hot path.
Oops, right, the grace period is just used to switch the lock to global mode while readers are passing through. Never mind this point. It's purely about writer latency then.
Thanks.
Signature · 1 line
tejun
Peter Zijlstra
maintainerQuoted 1 line · Tejun Heo
How? While write lock is pending, no new reader is allowed.
Look at the new percpu_down_write (the old one is similar in concept):
+ void percpu_down_write(struct percpu_rw_semaphore *sem)
+ {
+ down_write(&sem->rw_sem);
+
+ /* Notify readers to take the slow path. */
+ rcu_sync_enter(&sem->rss);
+
+ /*
+ * Notify new readers to block; up until now, and thus throughout the
+ * longish rcu_sync_enter() above, new readers could still come in.
+ */
+ WRITE_ONCE(sem->state, readers_block);
Note how up until this point new readers could happen? So the 'entire' synchronize_sched() call is 'invisible' to new readers.
We need the sync_sched() to ensure all new down_read callers will go through the 'slow' down_read path and even look at sem->state.
+
+ smp_mb(); /* D matches A */
+
+ /*
+ * If they don't see our writer of readers_block to sem->state,
+ * then we are guaranteed to see their sem->refcount increment, and
+ * therefore will wait for them.
+ */
+
+ /* Wait for all now active readers to complete. */
+ wait_event(sem->writer, readers_active_check(sem));
+ }
Quoted 1 line
If reader ops are high frequency, they will surely get affected.
Before the sync_sched() a down_read (PREEMPT=n, LOCKDEP=n) is basically:
__this_cpu_inc(*sem->refcount)
if (sem->rss->gp_state) /* false */
;
This is one purely local (!atomic) RmW and one load of a shared variable. Absolute minimal overhead.
During sync_sched() gp_state is !0 and we end up doing:
__this_cpu_inc(*sem->refcount)
if (sem->rss->gp_state) {
smp_mb();
if (sem->state != readers_block) /* true */
return;}
Which is 1 smp_mb() and 1 shared state load (to the same cacheline we touched before IIRC) more. Now full barriers are really rather expensive, and do show up on profiles.
(and this is where the old and new code differ, the old code would end up doing: __down_read(&sem->rwsem), which is a shared atomic RmW and is _much_ more expensive).
Quoted 2 lines
It just isn't a good design to inject RCU grace period synchronously into a hot path.
That's just the point, the write side of a _global_ lock can never, per definition, be a hot path.
Now, I realize not everyone wants these same tradeoffs and I did you this patch to allow that.
But I really think that this Android usecase invalidates the premise of cgroups using a global lock.
Quoted 3 lines · Peter Zijlstra
But I really think that this Android usecase invalidates the premise of cgroups using a global lock.
Perhaps... but it would be nice to have a global lock for cgroups (and in fact probably unify it with dup_mmap_sem). And we can't simply revert that change now.
I am wondering if this use-case is really Android-specific or not. Because, once again, we can add a boot option or even run-time knob for cgroup_threadgroup_rwsem. If it is really Android-specific, we do not want to penalize fork/exit by default.
Oleg.
Quoted 9 lines · Paul E. McKenney
Quoted 4 lines · John Stultz
But otherwise both patches look great and are working well!
Do you mind marking them both for stable 4.4+?
OK, looks like it does qualify in the "fix a notable performance or interactivity issue" category.
Quoted 6 lines
Quoted 4 lines
Tested-by John Stultz <[email protected]>Acked-by John Stultz <[email protected]>Also, do make sure Dmitry gets the reported-by credit for the first patch.
Done! The updated first patch is below, and the second will follow.
So just as a heads up, while this patch *greatly* improved the situation, apparently the occasional 7ms spikes (again, much better then 80ms!) still seen are causing trouble when compared w/ the 4.1 or earlier kernels (which I think kept things sub-ms - Dmitry, please correct me).
So I wanted to dampen my enthusiasm a touch for this, as there may yet further improvements needed. I'll try to get some more info on this as soon as I have details.
thanks
-john
Quoted 68 lines · Paul E. McKenney
Quoted 10 lines · Paul E. McKenney
Quoted 7 lines · Tejun Heo
Quoted 4 lines · Paul E. McKenney
Take the patch that I just sent out and make the choice of normal vs. expedited depend on
CONFIG_PREEMPT_RTor whatever the -rt guys are calling it these days. Is there a low-latencyKconfigoption other thanCONFIG_NO_HZ_FULL?Sounds like a plan to me.
I like the way we like each other's idea. Mutually assured laziness? ;-)
But here is what mine might look like. Untested, probably does not even build. Note that the default is -no- expediting, use the rcusync.expedited kernel parameter to enable it.
Thanx, Paul
------------------------------------------------------------------------
Documentation/kernel-parameters.txt +5
-0@@ -3229,6 +3229,11 @@ bytes respectively. Such letter suffixes can also be entirely omitted. energy efficiency by requiring that the kthreads periodically wake up to do the polling. + rcusync.expedited [KNL] + Specify that the rcusync mechanism use expedited + grace periods. As of mid-2016, this affects + per-CPU rwsems. + rcutree.blimit= [KNL] Set maximum number of finished RCU callbacks to process in one batch.kernel/rcu/sync.c +9
-1@@ -22,6 +22,14 @@#include <linux/rcu_sync.h> #include <linux/sched.h> +#include <linux/moduleparam.h> +#include <linux/module.h> + +MODULE_ALIAS ("rcusync" ); +#ifdef MODULE_PARAM_PREFIX +#undef MODULE_PARAM_PREFIX +#endif +#define MODULE_PARAM_PREFIX "rcusync."#ifdef CONFIG_PROVE_RCU #define __INIT_HELD (func) .held = func,@@ -29,7 +37,7 @@#define __INIT_HELD (func)#endif -static const struct { +static struct { void (*sync)(void); void (*call)(struct rcu_head *, void (*)(struct rcu_head *)); void (*wait)(void);@@ -62,6 +70,20 @@ enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };#define rss_lock gp_wait.lock
kernel/rcu/sync.c enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY }; · line 82
Quoted 10 lines
kernel/rcu/sync.c +10
-0+static bool expedited; +module_param (expedited, bool,0444 ); + +static int __init rcu_sync_early_init (void ) +{ +if (expedited) { + gp_ops[RCU_SYNC ].sync = synchronize_rcu_expedited; + gp_ops[RCU_SCHED_SYNC ].sync = synchronize_sched_expedited; + gp_ops[RCU_BH_SYNC ].sync = synchronize_rcu_bh_expedited; + }
So one minor nit here, with the config based default, you might want to put some sort of informative message specifying that expidited was used. This would help narrow down if it was or wasn't enabled when folks see problems, since it wouldn't be otherwise obvious from a dmesg log.
thanks
-john
Tejun Heo
maintainerHello, Paul.
Quoted 10 lines · Paul E. McKenney
Quoted 7 lines · Tejun Heo
Quoted 4 lines · Paul E. McKenney
Take the patch that I just sent out and make the choice of normal vs. expedited depend on
CONFIG_PREEMPT_RTor whatever the -rt guys are calling it these days. Is there a low-latencyKconfigoption other thanCONFIG_NO_HZ_FULL?Sounds like a plan to me.
I like the way we like each other's idea. Mutually assured laziness? ;-)
Heh, indeed. :)
Technically, I think the lglock approach would be better here given the combination of requirements; however, it's quite a bit more code which would likely require some sophistications down the line (like blocking new readers first at the start of down_write). If we have to go there, we'll go there but for now I think it'd be simpler to conditionally switch to the expedited operations. It can be a config option which is selected by !RT as you suggested. If anyone hits an actual issue with that, we can go for the lglock thing.
Thanks.
Signature · 1 line
tejun
Quoted 16 lines · Tejun Heo
Hello, Paul.
Quoted 10 lines · Paul E. McKenney
Quoted 7 lines · Tejun Heo
Quoted 4 lines · Paul E. McKenney
Take the patch that I just sent out and make the choice of normal vs. expedited depend on
CONFIG_PREEMPT_RTor whatever the -rt guys are calling it these days. Is there a low-latencyKconfigoption other thanCONFIG_NO_HZ_FULL?Sounds like a plan to me.
I like the way we like each other's idea. Mutually assured laziness? ;-)
Heh, indeed. :)
Quoted 8 lines
Technically, I think the lglock approach would be better here given the combination of requirements; however, it's quite a bit more code which would likely require some sophistications down the line (like blocking new readers first at the start of
down_write). If we have to go there, we'll go there but for now I think it'd be simpler to conditionally switch to the expedited operations. It can be a config option which is selected by !RT as you suggested. If anyone hits an actual issue with that, we can go for the lglock thing.
Fair enough! ;-)
Thanx, Paul
Peter Zijlstra
maintainerQuoted 4 lines · Tejun Heo
Technically, I think the lglock approach would be better here given the combination of requirements; however, it's quite a bit more code which would likely require some sophistications down the line (like blocking new readers first at the start of
down_write).
So the immediate problem with lg style locks is that the 'local' lock will not stay local since these are preemptible locks we can get migrations etc..
All fixable, but still.
Quoted 5 lines
If we have to
go there, we'll go there but for now I think it'd be simpler to conditionally switch to the expedited operations. It can be a config option which is selected by !RT as you suggested. If anyone hits an actual issue with that, we can go for the lglock thing.
So the main objection I have is that this isn't a fundamental fix, this only cures things because Android only runs on small machines.
If someone with a big computer tries to do the same things we're up some creek without no paddle. There's just no way we can make a global writer 'fast'.
Tejun Heo
maintainerQuoted 7 lines · Peter Zijlstra
Quoted 4 lines · Tejun Heo
Technically, I think the lglock approach would be better here given the combination of requirements; however, it's quite a bit more code which would likely require some sophistications down the line (like blocking new readers first at the start of
down_write).
Quoted 5 lines
So the immediate problem with lg style locks is that the 'local' lock will not stay local since these are preemptible locks we can get migrations etc..
All fixable, but still.
In this case, the locks are read-locked only across operations which change process hierarchy. They'll occasionally get migrated while holding the lock for sure but not often enough to matter.
Quoted 6 lines
Quoted 5 lines
If we have to
go there, we'll go there but for now I think it'd be simpler to conditionally switch to the expedited operations. It can be a config option which is selected by !RT as you suggested. If anyone hits an actual issue with that, we can go for the lglock thing.
Quoted 6 lines
So the main objection I have is that this isn't a fundamental fix, this only cures things because Android only runs on small machines.
If someone with a big computer tries to do the same things we're up some creek without no paddle. There's just no way we can make a global writer 'fast'.
How so? As the number of cores increases, it'll get proportionally more expensive as the same operation is performed on more CPUs; however, the latency is dependent on the slowest one and it'll get higher more often with more number of CPUs but not drastically. Latency won't go up proportionally with the number of CPUs. For the most part, we're paying more in terms of processing overhead, not latency.
Thanks.
Signature · 1 line
tejun
Peter Zijlstra
maintainerQuoted 11 lines · Tejun Heo
Quoted 5 lines · Peter Zijlstra
So the immediate problem with lg style locks is that the 'local' lock will not stay local since these are preemptible locks we can get migrations etc..
All fixable, but still.
In this case, the locks are read-locked only across operations which change process hierarchy. They'll occasionally get migrated while holding the lock for sure but not often enough to matter.
Means having to change the interface to pass along what 'local' is, like srcu_read_lock().
Quoted 11 lines
Quoted 6 lines
So the main objection I have is that this isn't a fundamental fix, this only cures things because Android only runs on small machines.
If someone with a big computer tries to do the same things we're up some creek without no paddle. There's just no way we can make a global writer 'fast'.
How so? As the number of cores increases, it'll get proportionally more expensive as the same operation is performed on more CPUs; however, the latency is dependent on the slowest one and it'll get higher more often with more number of CPUs but not drastically.
A global lock on 4 or 8 socket machines with all 200+ cpus trying to use it really stinks.
Remember, they switch cgroups at really rather high rates here, because of that binder stuff. I don't see how you can defend a global lock here :/ Global locks only work when writers are extremely rare, and clearly that premise is false.
Also note that since these are preemptible locks, you can get unbounded priority inversions.
Tejun Heo
maintainerQuoted 7 lines · Peter Zijlstra
Quoted 4 lines
How so? As the number of cores increases, it'll get proportionally more expensive as the same operation is performed on more CPUs; however, the latency is dependent on the slowest one and it'll get higher more often with more number of CPUs but not drastically.
A global lock on 4 or 8 socket machines with all 200+ cpus trying to use it really stinks.
That's why we're using percpu lock here. With the right implementation the write locking latency shouldn't be proportional to the number of cores. The total processing overhead could be.
Quoted 4 lines
Remember, they switch cgroups at really rather high rates here, because of that binder stuff. I don't see how you can defend a global lock here :/ Global locks only work when writers are extremely rare, and clearly that premise is false.
This is one of the most eccentric uses and if I'm not mistaken they were even doing memcg charge immigration on fore <-> background switches which involve re-labeling each page. And even with binder, migration isn't nearly hot enough to matter for the most part even when it's emitting IPIs globally.
We can split the locking but it just isn't necessary at this point. More downsides than upsides. If we actually need that, let's go there.
Thanks.
Signature · 1 line
tejun
Peter Zijlstra
maintainerQuoted 9 lines · Peter Zijlstra
So, IIRC, the trade-off is a full memory barrier in
read_lockandread_unlock()vssync_sched()in write.Full memory barriers are expensive and while the combined cost might well exceed the cost of the
sync_sched()it doesn't suffer the latency issues.Not sure if we can frob the two in a single codebase, but I can have a poke if Oleg or Paul doesn't beat me to it.
OK, not too horrible if I say so myself :-)
The below is a compile tested only first draft so far. I'll go give it some runtime next.
--- fs/super.c | 3 +- include/linux/percpu-rwsem.h | 96 +++++++++++++++-- include/linux/rcu_sync.h | 2 +- kernel/locking/percpu-rwsem.c | 243 +++++++++++++++++++++++++----------------- kernel/rcu/sync.c | 15 +++ 5 files changed, 249 insertions(+), 110 deletions(-)
fs/super.c +2 -1
@@ -195,7 +195,8 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags)for (i =0 ; i <SB_FREEZE_LEVELS ; i++) {if (__percpu_init_rwsem (&s->s_writers.rw_sem[i], sb_writers_name[i], - &type->s_writers_key[i])) + &type->s_writers_key[i], +PERCPU_RWSEM_READER )) goto fail; }init_waitqueue_head (&s->s_writers.wait_unfrozen);
include/linux/percpu-rwsem.h +87 -9
116 lines not on this page · raw message
include/linux/rcu_sync.h +1 -1
@@ -26,7 +26,7 @@#include <linux/wait.h> #include <linux/rcupdate.h> -enum rcu_sync_type {RCU_SYNC ,RCU_SCHED_SYNC ,RCU_BH_SYNC }; +enum rcu_sync_type {RCU_SYNC ,RCU_SCHED_SYNC ,RCU_BH_SYNC ,RCU_NONE };/* Structure to mediate between updaters and fastpath-using readers. */ struct rcu_sync {
kernel/locking/percpu-rwsem.c +144 -99
296 lines not on this page · raw message
kernel/rcu/sync.c +15 -0
@@ -55,6 +55,7 @@ static const struct { .wait = rcu_barrier_bh,__INIT_HELD (rcu_read_lock_bh_held) }, + [RCU_NONE] = { }, };enum {GP_IDLE =0 ,GP_PENDING ,GP_PASSED };
@@ -65,6 +66,9 @@ enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };#ifdef CONFIG_PROVE_RCU void rcu_sync_lockdep_assert (struct rcu_sync *rsp) { +if (rsp->gp_type ==RCU_NONE ) +return ; +RCU_LOCKDEP_WARN (!gp_ops[rsp->gp_type].held (),"suspicious rcu_sync_is_idle() usage" ); }
@@ -80,6 +84,8 @@ void rcu_sync_init(struct rcu_sync *rsp, enum rcu_sync_type type)memset (rsp,0 ,sizeof (*rsp));init_waitqueue_head (&rsp->gp_wait); rsp->gp_type = type; +if (rsp->gp_type ==RCU_NONE ) + rsp->gp_state =GP_PENDING ;/* anything !0 */ }/**
@@ -101,6 +107,9 @@ void rcu_sync_enter(struct rcu_sync *rsp) {bool need_wait, need_sync; +if (rsp->gp_type ==RCU_NONE ) +return ; +spin_lock_irq (&rsp->rss_lock); need_wait = rsp->gp_count++; need_sync = rsp->gp_state ==GP_IDLE ;
@@ -188,6 +197,9 @@ static void rcu_sync_func(struct rcu_head *rcu)*/ void rcu_sync_exit (struct rcu_sync *rsp) { +if (rsp->gp_type ==RCU_NONE ) +return ; +spin_lock_irq (&rsp->rss_lock);if (!--rsp->gp_count) {if (rsp->cb_state ==CB_IDLE ) {
@@ -208,6 +220,9 @@ void rcu_sync_dtor(struct rcu_sync *rsp) {int cb_state; +if (rsp->gp_type ==RCU_NONE ) +return ; +BUG_ON (rsp->gp_count);spin_lock_irq (&rsp->rss_lock);
Peter Zijlstra
maintainerQuoted 8 lines · Peter Zijlstra
Quoted 7 lines · Peter Zijlstra
So, IIRC, the trade-off is a full memory barrier in
read_lockandread_unlock()vssync_sched()in write.Full memory barriers are expensive and while the combined cost might well exceed the cost of the
sync_sched()it doesn't suffer the latency issues.
Quoted 7 lines
Quoted 2 lines
Not sure if we can frob the two in a single codebase, but I can have a poke if Oleg or Paul doesn't beat me to it.
OK, not too horrible if I say so myself :-)
The below is a compile tested only first draft so far. I'll go give it some runtime next.
Doesn't explode if I run:
root@ivb-ep:/cgroup# mkdir ponies; for ((i=0; i<60; i++)) ; do while :; do echo $$ > tasks; echo $$ > ponies/tasks ; done & done
with cgroup using either READER or WRITER bias.
So passes light torture.
Quoted 5 lines · Peter Zijlstra
OK, not too horrible if I say so myself :-)
The below is a compile tested only first draft so far. I'll go give it some runtime next.
Yes, thanks.
But note that we do not need RCU_NONE. All we need is the trivial change below. Damn, I am trying to find my old rcu-sync patches which I didn't send, but can't... OK, this almost off-topic right now, just this "enter" is ugly and we can't switch the slow/fast modes dynamically.
The rest of you patch is "optimize the slow path" and we already discussed it before, I personally like it. Perhaps you can redo it without RCU_NONE part?
Of course, this leads to another question: do we really need rcu-sync at all, or should we change percpu-rwsem to always work in the "slow" mode which is not that slow with your change... I'd like to keep it ;)
What do you think?
Oleg.
kernel/cgroup.c +2 -0
@@ -5605,6 +5605,8 @@ int __init cgroup_init(void)BUG_ON (cgroup_init_cftypes (NULL , cgroup_dfl_base_files));BUG_ON (cgroup_init_cftypes (NULL , cgroup_legacy_base_files)); +rcu_sync_enter (&cgroup_threadgroup_rwsem.rss); +get_user_ns (init_cgroup_ns.user_ns);mutex_lock (&cgroup_mutex);
Peter Zijlstra
maintainerQuoted 6 lines · Oleg Nesterov
Of course, this leads to another question: do we really need rcu-sync at all, or should we change percpu-rwsem to always work in the "slow" mode which is not that slow with your change... I'd like to keep it ;)
What do you think?
Yes, I think we wants to keep it. There are users where the read size cost really are performance critical. I still have to repost that lglock removal series for example, which replaces the fs/locks lglock with a percpu-rwsem.
Peter Zijlstra
maintainerQuoted 3 lines · Oleg Nesterov
But note that we do not need
RCU_NONE. All we need is the trivial change below.
Hurm, maybe. So having that unbalanced keeps us in GP_PASSED state and since we'll never drop gp_count back to 0 nothing will ever happen.
Cute, yes.
Quoted 7 lines
Damn, I am trying to find my old rcu-sync patches which I didn't send, but can't... OK, this almost off-topic right now, just this "enter" is ugly and we can't switch the slow/fast modes dynamically.
The rest of you patch is "optimize the slow path" and we already discussed it before, I personally like it. Perhaps you can redo it without
RCU_NONEpart?
Indeed, I rebased that patch on top of the current tree and had to add support for down_trylock() but otherwise much the same thing.
I can send it out again.
Quoted 8 lines · Peter Zijlstra
Quoted 3 lines · Oleg Nesterov
But note that we do not need
RCU_NONE. All we need is the trivial change below.Hurm, maybe. So having that unbalanced keeps us in
GP_PASSEDstate and since we'll never dropgp_countback to 0 nothing will ever happen.
Yes, exactly. Of course, this is a bit ugly, I'll try to send some rcu-sync cleanups/simplifications later.
Perhaps we can even add a "cgroup migration is not that cold as it was supposed" boot option for this rcu_sync_enter() for now, not sure.
Quoted 4 lines
Quoted 4 lines
Damn, I am trying to find my old rcu-sync patches which I didn't send, but can't... OK, this almost off-topic right now, just this "enter" is ugly and we can't switch the slow/fast modes dynamically.
Quoted 8 lines
Quoted 3 lines
The rest of you patch is "optimize the slow path" and we already discussed it before, I personally like it. Perhaps you can redo it without
RCU_NONEpart?Indeed, I rebased that patch on top of the current tree and had to add support for
down_trylock()but otherwise much the same thing.I can send it out again.
Great, thanks! This should fix the problem.
Oleg.
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Peter Zijlstra
So, IIRC, the trade-off is a full memory barrier in
read_lockandread_unlock()vssync_sched()in write.Full memory barriers are expensive and while the combined cost might well exceed the cost of the
sync_sched()it doesn't suffer the latency issues.
Quoted 7 lines
Quoted 2 lines
Not sure if we can frob the two in a single codebase, but I can have a poke if Oleg or Paul doesn't beat me to it.
OK, not too horrible if I say so myself :-)
The below is a compile tested only first draft so far. I'll go give it some runtime next.
Hmmm... How does this handle the following sequence of events for the case where we are not biased towards the reader?
o The per-CPU rwsem is set up with RCU_NONE and readers_slow
(as opposed to readers_block). The rcu_sync ->gp_state is GP_PENDING, which means that rcu_sync_is_idle() will always return true.
o Task A on CPU 0 runs percpu_down_read() to completion, and remains
within the critical section. CPU 0's ->refcount is therefore 1.
o Task B on CPU 1 does percpu_down_write(), which write-acquires
->rw_sem, does rcu_sync_enter() (which is a no-op due to RCU_NONE), sets ->state to readers_block, and is just now going to wait for readers, which first invokes readers_active_check(), which starts summing the ->refcount for CPUs 0, 1, and 2, obtaining the value 1 so far.
o Task C CPU 2 enters percpu_down_read(), disables preemption,
increments CPU 2's ->refcount, sees rcu_sync_is_idle() return true (so skips __percpu_down_read()), enables preemption, and enters its critical section.
o Task C migrates to CPU 3 and invokes percpu_up_read(), which
disables preemption, sees rcu_sync_is_idle() return true, calls __this_cpu_dec() on CPU 3's ->refcount, and enables preemption. The value of CPU 3's ->refcount is thus (unsigned int)-1.
o Task B on CPU 1 continues execution in readers_active_check(), with
the full sum being zero.
So it looks to me like we have Task A as a writer at the same time that Task A is a reader, which would not be so good.
So what am I missing here?
And a couple of checkpatch nits below. Yes, I had to apply the patch to figure out what it was doing. ;-)
Thanx, Paul
Quoted 175 lines
--- fs/super.c | 3 +- include/linux/percpu-rwsem.h | 96 +++++++++++++++-- include/linux/rcu_sync.h | 2 +- kernel/locking/percpu-rwsem.c | 243 +++++++++++++++++++++++++----------------- kernel/rcu/sync.c | 15 +++ 5 files changed, 249 insertions(+), 110 deletions(-)fs/super.c +2
-1@@ -195,7 +195,8 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags)for (i =0 ; i <SB_FREEZE_LEVELS ; i++) {if (__percpu_init_rwsem (&s->s_writers.rw_sem[i], sb_writers_name[i], - &type->s_writers_key[i])) + &type->s_writers_key[i], +PERCPU_RWSEM_READER )) goto fail; }init_waitqueue_head (&s->s_writers.wait_unfrozen);include/linux/percpu-rwsem.h +87
-9@@ -10,29 +10,107 @@struct percpu_rw_semaphore {struct rcu_sync rss; -unsigned int __percpu *fast_read_ctr; +unsigned int __percpu *refcount;struct rw_semaphore rw_sem; -atomic_t slow_read_ctr; -wait_queue_head_t write_waitq; +wait_queue_head_t writer; +int state; }; -extern void percpu_down_read (struct percpu_rw_semaphore *); -extern int percpu_down_read_trylock (struct percpu_rw_semaphore *); -extern void percpu_up_read (struct percpu_rw_semaphore *); +extern void __percpu_down_read (struct percpu_rw_semaphore *); +extern int __percpu_down_read_trylock (struct percpu_rw_semaphore *); +extern void __percpu_up_read (struct percpu_rw_semaphore *); + +static inline void percpu_down_read (struct percpu_rw_semaphore *sem) +{ +might_sleep (); + +rwsem_acquire_read (&sem->rw_sem.dep_map,0 ,0 , _RET_IP_); + +preempt_disable (); +/* +* We are in an RCU-sched read-side critical section, so the writer +* cannot both change sem->state from readers_fast and start checking +* counters while we are here. So if we see !sem->state, we know that +* the writer won't be checking until we're past the preempt_enable() +* and that one the synchronize_sched() is done, the writer will see +* anything we did within this RCU-sched read-size critical section. +*/ +__this_cpu_inc (*sem->refcount); +if (unlikely (!rcu_sync_is_idle (&sem->rss))) +__percpu_down_read (sem);/* Unconditional memory barrier */ +preempt_enable (); +/* +* The barrier() from preempt_enable() prevents the compiler from +* bleeding the critical section out. +*/ +} + +static inline int percpu_down_read_trylock (struct percpu_rw_semaphore *sem) +{ +int ret =1 ; + +preempt_disable (); +/* +* Same as in percpu_down_read(). +*/ +__this_cpu_inc (*sem->refcount); +if (unlikely (!rcu_sync_is_idle (&sem->rss))) + ret =__percpu_down_read_trylock (sem); +preempt_enable (); +/* +* The barrier() from preempt_enable() prevents the compiler from +* bleeding the critical section out. +*/ + +if (ret) +rwsem_acquire_read (&sem->rw_sem.dep_map,0 ,1 , _RET_IP_); + +return ret; +} + +static inline void percpu_up_read (struct percpu_rw_semaphore *sem) +{ +/* +* The barrier() in preempt_disable() prevents the compiler from +* bleeding the critical section out. +*/ +preempt_disable (); +/* +* Same as in percpu_down_read(). +*/ +if (likely (rcu_sync_is_idle (&sem->rss))) +__this_cpu_dec (*sem->refcount); +else +__percpu_up_read (sem);/* Unconditional memory barrier */ +preempt_enable (); + +rwsem_release (&sem->rw_sem.dep_map,1 , _RET_IP_); +}extern void percpu_down_write (struct percpu_rw_semaphore *);extern void percpu_up_write (struct percpu_rw_semaphore *); +enum percpu_rwsem_bias {PERCPU_RWSEM_READER ,PERCPU_RWSEM_WRITER }; +extern int __percpu_init_rwsem (struct percpu_rw_semaphore *, -const char *,struct lock_class_key *); +const char *,struct lock_class_key *, +enum percpu_rwsem_bias bias); +extern void percpu_free_rwsem (struct percpu_rw_semaphore *); -#define percpu_init_rwsem (brw) \ +#define percpu_init_rwsem (sem) \ ({ \ static struct lock_class_key rwsem_key; \ - __percpu_init_rwsem(brw, #brw, &rwsem_key); \ + __percpu_init_rwsem(sem, #sem, &rwsem_key, \ + PERCPU_RWSEM_READER); \ }) +#define percpu_init_rwsem_writer (sem) \ +({ \ + static struct lock_class_key rwsem_key; \ + __percpu_init_rwsem(sem, #sem, &rwsem_key,i \ + PERCPU_RWSEM_WRITER); \ +})#define percpu_rwsem_is_held (sem) lockdep_is_held(&(sem)->rw_sem)include/linux/rcu_sync.h +1
-1@@ -26,7 +26,7 @@#include <linux/wait.h> #include <linux/rcupdate.h> -enum rcu_sync_type {RCU_SYNC ,RCU_SCHED_SYNC ,RCU_BH_SYNC }; +enum rcu_sync_type {RCU_SYNC ,RCU_SCHED_SYNC ,RCU_BH_SYNC ,RCU_NONE };/* Structure to mediate between updaters and fastpath-using readers. */ struct rcu_sync {kernel/locking/percpu-rwsem.c +6
-4@@ -8,152 +8,197 @@#include <linux/sched.h> #include <linux/errno.h> -int __percpu_init_rwsem (struct percpu_rw_semaphore *brw, -const char *name,struct lock_class_key *rwsem_key) +enum { readers_slow, readers_block }; + +int __percpu_init_rwsem (struct percpu_rw_semaphore *sem, +const char *name,struct lock_class_key *rwsem_key, +enum percpu_rwsem_bias bias) { - brw->fast_read_ctr =alloc_percpu (int); -if (unlikely (!brw->fast_read_ctr)) + sem->refcount =alloc_percpu (int);
kernel/locking/percpu-rwsem.c line 23
Quoted 10 lines
kernel/locking/percpu-rwsem.c +3
-4+if (unlikely (!sem->refcount))return -ENOMEM ;/* ->rw_sem represents the whole percpu_rw_semaphore for lockdep */ -__init_rwsem (&brw->rw_sem, name, rwsem_key); -rcu_sync_init (&brw->rss,RCU_SCHED_SYNC ); -atomic_set (&brw->slow_read_ctr,0 ); -init_waitqueue_head (&brw->write_waitq); + rcu_sync_init(&sem->rss, bias == PERCPU_RWSEM_READER ? + RCU_SCHED_SYNC :
Whitespace complaint on prior two lines.
Quoted 334 lines
kernel/locking/percpu-rwsem.c +135
-91+ RCU_NONE); +__init_rwsem (&sem->rw_sem, name, rwsem_key); +init_waitqueue_head (&sem->writer); + sem->state = readers_slow;return 0 ; }EXPORT_SYMBOL_GPL (__percpu_init_rwsem); -void percpu_free_rwsem (struct percpu_rw_semaphore *brw) +void percpu_free_rwsem (struct percpu_rw_semaphore *sem) {/* * XXX: temporary kludge. The error path in alloc_super() * assumes that percpu_free_rwsem() is safe after kzalloc(). */ -if (!brw->fast_read_ctr) +if (!sem->refcount)return ; -rcu_sync_dtor (&brw->rss); -free_percpu (brw->fast_read_ctr); - brw->fast_read_ctr =NULL ;/* catch use after free bugs */ +rcu_sync_dtor (&sem->rss); +free_percpu (sem->refcount); + sem->refcount =NULL ;/* catch use after free bugs */ }EXPORT_SYMBOL_GPL (percpu_free_rwsem); -/* -* This is the fast-path for down_read/up_read. If it succeeds we rely -* on the barriers provided by rcu_sync_enter/exit; see the comments in -* percpu_down_write() and percpu_up_write(). -* -* If this helper fails the callers rely on the normal rw_semaphore and -* atomic_dec_and_test(), so in this case we have the necessary barriers. -*/ -static bool update_fast_ctr (struct percpu_rw_semaphore *brw,unsigned int val) +void __percpu_down_read (struct percpu_rw_semaphore *sem) { -bool success; +/* +* Due to having preemption disabled the decrement happens on +* the same CPU as the increment, avoiding the +* increment-on-one-CPU-and-decrement-on-another problem. +* +* And yes, if the reader misses the writer's assignment of +* readers_block to sem->state, then the writer is +* guaranteed to see the reader's increment. Conversely, any +* readers that increment their sem->refcount after the +* writer looks are guaranteed to see the readers_block value, +* which in turn means that they are guaranteed to immediately +* decrement their sem->refcount, so that it doesn't matter +* that the writer missed them. +*/ -preempt_disable (); - success =rcu_sync_is_idle (&brw->rss); -if (likely (success)) -__this_cpu_add (*brw->fast_read_ctr, val); -preempt_enable (); +smp_mb ();/* A matches D */ -return success; -} +/* +* If !readers_block the critical section starts here, matched by the +* release in percpu_up_write(). +*/ +if (likely (smp_load_acquire (&sem->state) != readers_block)) +return ; -/* -* Like the normal down_read() this is not recursive, the writer can -* come after the first percpu_down_read() and create the deadlock. -* -* Note: returns with lock_is_held(brw->rw_sem) == T for lockdep, -* percpu_up_read() does rwsem_release(). This pairs with the usage -* of ->rw_sem in percpu_down/up_write(). -*/ -void percpu_down_read (struct percpu_rw_semaphore *brw) -{ -might_sleep (); -rwsem_acquire_read (&brw->rw_sem.dep_map,0 ,0 , _RET_IP_); +/* +* Per the above comment; we still have preemption disabled and +* will thus decrement on the same CPU as we incremented. +*/ +__percpu_up_read (sem); -if (likely (update_fast_ctr (brw,+1 ))) -return ; +/* +* We either call schedule() in the wait, or we'll fall through +* and reschedule on the preempt_enable() in percpu_down_read(). +*/ +preempt_enable_no_resched (); + +/* +* Avoid lockdep for the down/up_read() we already have them. +*/ +__down_read (&sem->rw_sem); +__this_cpu_inc (*sem->refcount); +__up_read (&sem->rw_sem); -/* Avoid rwsem_acquire_read() and rwsem_release() */ -__down_read (&brw->rw_sem); -atomic_inc (&brw->slow_read_ctr); -__up_read (&brw->rw_sem); +preempt_disable (); } -EXPORT_SYMBOL_GPL (percpu_down_read); +EXPORT_SYMBOL_GPL (__percpu_down_read); -int percpu_down_read_trylock (struct percpu_rw_semaphore *brw) +int __percpu_down_read_trylock (struct percpu_rw_semaphore *sem) { -if (unlikely (!update_fast_ctr (brw,+1 ))) { -if (!__down_read_trylock (&brw->rw_sem)) -return 0 ; -atomic_inc (&brw->slow_read_ctr); -__up_read (&brw->rw_sem); - } - -rwsem_acquire_read (&brw->rw_sem.dep_map,0 ,1 , _RET_IP_); -return 1 ; +smp_mb ();/* A matches D */ + +if (likely (smp_load_acquire (&sem->state) != readers_block)) +return 1 ; + +__percpu_up_read (sem); +return 0 ; } +EXPORT_SYMBOL_GPL (__percpu_down_read_trylock); -void percpu_up_read (struct percpu_rw_semaphore *brw) +void __percpu_up_read (struct percpu_rw_semaphore *sem) { -rwsem_release (&brw->rw_sem.dep_map,1 , _RET_IP_); - -if (likely (update_fast_ctr (brw,-1 ))) -return ; +smp_mb ();/* B matches C */ +/* +* In other words, if they see our decrement (presumably to aggregate +* zero, as that is the only time it matters) they will also see our +* critical section. +*/ +__this_cpu_dec (*sem->refcount); -/* false-positive is possible but harmless */ -if (atomic_dec_and_test (&brw->slow_read_ctr)) -wake_up_all (&brw->write_waitq); +/* Prod writer to recheck readers_active */ +wake_up (&sem->writer); } -EXPORT_SYMBOL_GPL (percpu_up_read); +EXPORT_SYMBOL_GPL (__percpu_up_read); + +#define per_cpu_sum (var) \ +({ \ + typeof(var) __sum = 0; \ + int cpu; \ + compiletime_assert_atomic_type(__sum); \ + for_each_possible_cpu(cpu) \ + __sum += per_cpu(var, cpu); \ + __sum; \ +}) -static int clear_fast_ctr (struct percpu_rw_semaphore *brw) +/* +* Return true if the modular sum of the sem->refcount per-CPU variable is +* zero. If this sum is zero, then it is stable due to the fact that if any +* newly arriving readers increment a given counter, they will immediately +* decrement that same counter. +*/ +static bool readers_active_check (struct percpu_rw_semaphore *sem) { -unsigned int sum =0 ; -int cpu; +if (per_cpu_sum (*sem->refcount) !=0 ) +return false; + +/* +* If we observed the decrement; ensure we see the entire critical +* section. +*/ -for_each_possible_cpu (cpu) { - sum +=per_cpu (*brw->fast_read_ctr, cpu); -per_cpu (*brw->fast_read_ctr, cpu) =0 ; - } +smp_mb ();/* C matches B */ -return sum; +return true; } -void percpu_down_write (struct percpu_rw_semaphore *brw) +void percpu_down_write (struct percpu_rw_semaphore *sem) { +down_write (&sem->rw_sem); + +/* Notify readers to take the slow path. */ +rcu_sync_enter (&sem->rss); +/* -* Make rcu_sync_is_idle() == F and thus disable the fast-path in -* percpu_down_read() and percpu_up_read(), and wait for gp pass. -* -* The latter synchronises us with the preceding readers which used -* the fast-past, so we can not miss the result of __this_cpu_add() -* or anything else inside their criticial sections. +* Notify new readers to block; up until now, and thus throughout the +* longish rcu_sync_enter() above, new readers could still come in. */ -rcu_sync_enter (&brw->rss); + sem->state = readers_block; -/* exclude other writers, and block the new readers completely */ -down_write (&brw->rw_sem); +smp_mb ();/* D matches A */ -/* nobody can use fast_read_ctr, move its sum into slow_read_ctr */ -atomic_add (clear_fast_ctr (brw), &brw->slow_read_ctr); +/* +* If they don't see our writer of readers_block to sem->state, +* then we are guaranteed to see their sem->refcount increment, and +* therefore will wait for them. +*/ -/* wait for all readers to complete their percpu_up_read() */ -wait_event (brw->write_waitq, !atomic_read (&brw->slow_read_ctr)); +/* Wait for all now active readers to complete. */ +wait_event (sem->writer,readers_active_check (sem)); } -EXPORT_SYMBOL_GPL (percpu_down_write); -void percpu_up_write (struct percpu_rw_semaphore *brw) +void percpu_up_write (struct percpu_rw_semaphore *sem) { -/* release the lock, but the readers can't use the fast-path */ -up_write (&brw->rw_sem);/* -* Enable the fast-path in percpu_down_read() and percpu_up_read() -* but only after another gp pass; this adds the necessary barrier -* to ensure the reader can't miss the changes done by us. +* Signal the writer is done, no fast path yet. +* +* One reason that we cannot just immediately flip to readers_fast is +* that new readers might fail to see the results of this writer's +* critical section. +* +* Therefore we force it through the slow path which guarantees an +* acquire and thereby guarantees the critical section's consistency. +*/ +smp_store_release (&sem->state, readers_slow); + +/* +* Release the write lock, this will allow readers back in the game. +*/ +up_write (&sem->rw_sem); + +/* +* Once this completes (at least one RCU grace period hence) the reader +* fast path will be available again. Safe to use outside the exclusive +* write lock because its counting. */ -rcu_sync_exit (&brw->rss); +rcu_sync_exit (&sem->rss); } -EXPORT_SYMBOL_GPL (percpu_up_write);kernel/rcu/sync.c +15
-0@@ -55,6 +55,7 @@ static const struct { .wait = rcu_barrier_bh,__INIT_HELD (rcu_read_lock_bh_held) }, + [RCU_NONE] = { }, };enum {GP_IDLE =0 ,GP_PENDING ,GP_PASSED };@@ -65,6 +66,9 @@ enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };#ifdef CONFIG_PROVE_RCU void rcu_sync_lockdep_assert (struct rcu_sync *rsp) { +if (rsp->gp_type ==RCU_NONE ) +return ; +RCU_LOCKDEP_WARN (!gp_ops[rsp->gp_type].held (),"suspicious rcu_sync_is_idle() usage" ); }@@ -80,6 +84,8 @@ void rcu_sync_init(struct rcu_sync *rsp, enum rcu_sync_type type)memset (rsp,0 ,sizeof (*rsp));init_waitqueue_head (&rsp->gp_wait); rsp->gp_type = type; +if (rsp->gp_type ==RCU_NONE ) + rsp->gp_state =GP_PENDING ;/* anything !0 */ }/** @@ -101,6 +107,9 @@ void rcu_sync_enter(struct rcu_sync *rsp) {bool need_wait, need_sync; +if (rsp->gp_type ==RCU_NONE ) +return ; +spin_lock_irq (&rsp->rss_lock); need_wait = rsp->gp_count++; need_sync = rsp->gp_state ==GP_IDLE ;@@ -188,6 +197,9 @@ static void rcu_sync_func(struct rcu_head *rcu)*/ void rcu_sync_exit (struct rcu_sync *rsp) { +if (rsp->gp_type ==RCU_NONE ) +return ; +spin_lock_irq (&rsp->rss_lock);if (!--rsp->gp_count) {if (rsp->cb_state ==CB_IDLE ) {@@ -208,6 +220,9 @@ void rcu_sync_dtor(struct rcu_sync *rsp) {int cb_state; +if (rsp->gp_type ==RCU_NONE ) +return ; +BUG_ON (rsp->gp_count);spin_lock_irq (&rsp->rss_lock);
Peter Zijlstra
maintainerQuoted 7 lines · Paul E. McKenney
Hmmm... How does this handle the following sequence of events for the case where we are not biased towards the reader?
o The per-CPU rwsem is set up with
RCU_NONEandreaders_slow(as opposed to readers_block). The rcu_sync ->gp_state is GP_PENDING, which means that rcu_sync_is_idle() will always return true.
/false/, rcu_sync_is_idle() will always be false, to force us into the slowpath.
Quoted 10 lines
o Task A on CPU 0 runs
percpu_down_read()to completion, and remainswithin the critical section. CPU 0's ->refcount is therefore 1.o Task B on CPU 1 does
percpu_down_write(), which write-acquires->rw_sem, does rcu_sync_enter() (which is a no-op due to RCU_NONE), sets ->state to readers_block, and is just now going to wait for readers, which first invokes readers_active_check(), which starts summing the ->refcount for CPUs 0, 1, and 2, obtaining the value 1 so far.
Quoted 4 lines
o Task C CPU 2 enters
percpu_down_read(), disables preemption,
increments CPU 2's ->refcount, seesrcu_sync_is_idle()return true (so skips__percpu_down_read()), enables preemption, and enters its critical section.
false, so does __percpu_down_read()
Quoted 5 lines
o Task C migrates to CPU 3 and invokes
percpu_up_read(), whichdisables preemption, sees rcu_sync_is_idle() return true, calls __this_cpu_dec() on CPU 3's ->refcount, and enables preemption. The value of CPU 3's ->refcount is thus (unsigned int)-1.
__percpu_up_read()
Quoted 8 lines
o Task B on CPU 1 continues execution in
readers_active_check(), with
the full sum being zero.So it looks to me like we have Task A as a writer at the same time that Task A is a reader, which would not be so good.
So what am I missing here?
for RCU_NONE we init rsp->gp_state to !0, which makes:
static inline rcu_sync_is_idle()'s
return !rsp->gp_state (aka. rsp->gp_state == 0)
return false.
Quoted 2 lines
And a couple of checkpatch nits below. Yes, I had to apply the patch to figure out what it was doing. ;-)
Yah, too much churn to read :-)
In any case, rest assured you've already gone over this part of the patch several times. I repurposed an old percpu-rwsem optimization, Oleg recognised it.
Quoted 11 lines · Peter Zijlstra
Quoted 7 lines · Paul E. McKenney
Hmmm... How does this handle the following sequence of events for the case where we are not biased towards the reader?
o The per-CPU rwsem is set up with
RCU_NONEandreaders_slow(as opposed to readers_block). The rcu_sync ->gp_state is GP_PENDING, which means that rcu_sync_is_idle() will always return true./false/,
rcu_sync_is_idle()will always be false, to force us into the slowpath.
Ah, got it...
Quoted 42 lines
Quoted 10 lines
o Task A on CPU 0 runs
percpu_down_read()to completion, and remainswithin the critical section. CPU 0's ->refcount is therefore 1.o Task B on CPU 1 does
percpu_down_write(), which write-acquires->rw_sem, does rcu_sync_enter() (which is a no-op due to RCU_NONE), sets ->state to readers_block, and is just now going to wait for readers, which first invokes readers_active_check(), which starts summing the ->refcount for CPUs 0, 1, and 2, obtaining the value 1 so far.Quoted 4 lines
o Task C CPU 2 enters
percpu_down_read(), disables preemption,
increments CPU 2's ->refcount, seesrcu_sync_is_idle()return true (so skips__percpu_down_read()), enables preemption, and enters its critical section.false, so does
__percpu_down_read()Quoted 5 lines
o Task C migrates to CPU 3 and invokes
percpu_up_read(), whichdisables preemption, sees rcu_sync_is_idle() return true, calls __this_cpu_dec() on CPU 3's ->refcount, and enables preemption. The value of CPU 3's ->refcount is thus (unsigned int)-1.
__percpu_up_read()Quoted 8 lines
o Task B on CPU 1 continues execution in
readers_active_check(), with
the full sum being zero.So it looks to me like we have Task A as a writer at the same time that Task A is a reader, which would not be so good.
So what am I missing here?
for
RCU_NONEwe initrsp->gp_stateto !0, which makes:static inline rcu_sync_is_idle()'s
return !rsp->gp_state (aka. rsp->gp_state == 0)return false.
Quoted 8 lines
Quoted 2 lines
And a couple of checkpatch nits below. Yes, I had to apply the patch to figure out what it was doing. ;-)
Yah, too much churn to read :-)
In any case, rest assured you've already gone over this part of the patch several times. I repurposed an old percpu-rwsem optimization, Oleg recognised it.
OK, in that case I will hold off pending John Stultz's performance checks.
Thanx, Paul
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Peter Zijlstra
So, IIRC, the trade-off is a full memory barrier in
read_lockandread_unlock()vssync_sched()in write.Full memory barriers are expensive and while the combined cost might well exceed the cost of the
sync_sched()it doesn't suffer the latency issues.
Quoted 7 lines
Quoted 2 lines
Not sure if we can frob the two in a single codebase, but I can have a poke if Oleg or Paul doesn't beat me to it.
OK, not too horrible if I say so myself :-)
The below is a compile tested only first draft so far. I'll go give it some runtime next.
Unfortunately it didn't apply cleanly to the 4.4 based tree I'm working with, so I had to manually apply the entirety of the percpu-rwsem.c changes myself. Hopefully I didn't screw it up.
So running with this, I'm still seeing some pretty large delays. 80ms peak, with lots of >20ms values as well.
So it doesn't seem to have the positive effect that Paul's change provided.
thanks
-john
Peter Zijlstra
maintainerQuoted 17 lines · John Stultz
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Peter Zijlstra
So, IIRC, the trade-off is a full memory barrier in
read_lockandread_unlock()vssync_sched()in write.Full memory barriers are expensive and while the combined cost might well exceed the cost of the
sync_sched()it doesn't suffer the latency issues.Quoted 7 lines
Quoted 2 lines
Not sure if we can frob the two in a single codebase, but I can have a poke if Oleg or Paul doesn't beat me to it.
OK, not too horrible if I say so myself :-)
The below is a compile tested only first draft so far. I'll go give it some runtime next.
Quoted 7 lines
Unfortunately it didn't apply cleanly to the 4.4 based tree I'm working with, so I had to manually apply the entirety of the
percpu-rwsem.cchanges myself. Hopefully I didn't screw it up.So running with this, I'm still seeing some pretty large delays. 80ms peak, with lots of >20ms values as well.
So it doesn't seem to have the positive effect that Paul's change provided.
Well that is weird, did you put a tracepoint/printk in synchronize_sched() to ensure we don't end up calling that?
Quoted 22 lines · Peter Zijlstra
Quoted 21 lines · John Stultz
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Peter Zijlstra
So, IIRC, the trade-off is a full memory barrier in
read_lockandread_unlock()vssync_sched()in write.Full memory barriers are expensive and while the combined cost might well exceed the cost of the
sync_sched()it doesn't suffer the latency issues.Quoted 7 lines
Quoted 2 lines
Not sure if we can frob the two in a single codebase, but I can have a poke if Oleg or Paul doesn't beat me to it.
OK, not too horrible if I say so myself :-)
The below is a compile tested only first draft so far. I'll go give it some runtime next.
Unfortunately it didn't apply cleanly to the 4.4 based tree I'm working with, so I had to manually apply the entirety of the
percpu-rwsem.cchanges myself. Hopefully I didn't screw it up.
Quoted 6 lines
Quoted 3 lines
So running with this, I'm still seeing some pretty large delays. 80ms peak, with lots of >20ms values as well.
So it doesn't seem to have the positive effect that Paul's change provided.Well that is weird, did you put a tracepoint/printk in
synchronize_sched()to ensure we don't end up calling that?
So I am seeing synchronize_sched called, and its taking the !rcu_gp_is_expedited path when I see the particularly bad latencies.
I wonder if I just mucked up applying the patch?
thanks
-john
Quoted 5 lines · John Stultz
So I am seeing
synchronize_schedcalled, and its taking the !rcu_gp_is_expedited path when I see the particularly bad latencies.I wonder if I just mucked up applying the patch?
Probably yes...
Just in case, could you try the patch below? Of course, without other optimizations from Peter, this change makes cgroup_threadgroup_rwsem much worse than a plain rw_semaphore.
Oleg.
kernel/cgroup.c +2 -0
@@ -5605,6 +5605,8 @@ int __init cgroup_init(void)BUG_ON (cgroup_init_cftypes (NULL , cgroup_dfl_base_files));BUG_ON (cgroup_init_cftypes (NULL , cgroup_legacy_base_files)); +rcu_sync_enter (&cgroup_threadgroup_rwsem.rss); +get_user_ns (init_cgroup_ns.user_ns);mutex_lock (&cgroup_mutex);
Quoted 8 lines · Oleg Nesterov
Quoted 5 lines · John Stultz
So I am seeing
synchronize_schedcalled, and its taking the !rcu_gp_is_expedited path when I see the particularly bad latencies.I wonder if I just mucked up applying the patch?
Probably yes...
Hm. So I applied peterz patch to 4.7-rc7 and then diffed it to what I had and it was just whitespace changes.
I've synched them up now, so I suspect my application isn't the issue now. Just to be clear, I'm not supposed to be applying this on-top of Paul's change, right?
Quoted 9 lines
Just in case, could you try the patch below? Of course, without other optimizations from Peter, this change makes
cgroup_threadgroup_rwsemmuch worse than a plainrw_semaphore.Oleg.
kernel/cgroup.c int __init cgroup_init(void) · line 5609
Quoted 5 lines
kernel/cgroup.c +2
-0@@ -5605,6 +5605,8 @@ int __init cgroup_init(void)BUG_ON (cgroup_init_cftypes (NULL , cgroup_dfl_base_files));BUG_ON (cgroup_init_cftypes (NULL , cgroup_legacy_base_files)); +rcu_sync_enter (&cgroup_threadgroup_rwsem.rss); +
So adding this does make a huge difference ontop of Peter's patch. I'm seeing sub 200us values for everything. The biggest spike in my basic testing has been 138us.
I'm also not seeing synchronize_sched being called nearly as often, and it doesn't seem to be being called in cgroup_procs_write path.
thanks
-john
Quoted 3 lines · John Stultz
I'm not supposed to be applying this on-top of
Paul's change, right?
Right, unless I am totally confused,
Quoted 12 lines
Quoted 12 lines
Just in case, could you try the patch below? Of course, without other optimizations from Peter, this change makes
cgroup_threadgroup_rwsemmuch worse than a plainrw_semaphore.Oleg.
kernel/cgroup.c +0
-0@@ -5605,6 +5605,8 @@ int __init cgroup_init(void)BUG_ON (cgroup_init_cftypes (NULL , cgroup_dfl_base_files));BUG_ON (cgroup_init_cftypes (NULL , cgroup_legacy_base_files));
Quoted 5 lines
kernel/cgroup.c int __init cgroup_init(void) · line 5609
Quoted 2 lines
kernel/cgroup.c +2
-0+rcu_sync_enter (&cgroup_threadgroup_rwsem.rss); +So adding this does make a huge difference ontop of Peter's patch.
Ah, sorry for confusion. I meant, you could try this one-liner without any other changes.
But we will need the "slow mode optimization" part from Peter's patch anyway, otherwise percpu_rw_semaphore simply makes no sense for cgroup_threadgroup_rwsem.
Oleg.
Quoted 19 lines · Oleg Nesterov
Quoted 3 lines · John Stultz
I'm not supposed to be applying this on-top of
Paul's change, right?Right, unless I am totally confused,
Quoted 12 lines
Quoted 12 lines
Just in case, could you try the patch below? Of course, without other optimizations from Peter, this change makes
cgroup_threadgroup_rwsemmuch worse than a plainrw_semaphore.Oleg.
kernel/cgroup.c +0
-0@@ -5605,6 +5605,8 @@ int __init cgroup_init(void)BUG_ON (cgroup_init_cftypes (NULL , cgroup_dfl_base_files));BUG_ON (cgroup_init_cftypes (NULL , cgroup_legacy_base_files));
Quoted 8 lines
Quoted 5 lines
kernel/cgroup.c int __init cgroup_init(void) · line 5609
Quoted 2 lines
kernel/cgroup.c +2
-0+rcu_sync_enter (&cgroup_threadgroup_rwsem.rss); +So adding this does make a huge difference ontop of Peter's patch.
Ah, sorry for confusion. I meant, you could try this one-liner without any other changes.
So the one-liner without other changes helps at a similar level as Paul's change. From some simple testing I've got a 3.5ms spike, but otherwise the values are under 200us.
Quoted 3 lines
But we will need the "slow mode optimization" part from Peter's patch anyway, otherwise
percpu_rw_semaphoresimply makes no sense forcgroup_threadgroup_rwsem.
Yea, with Peter's patch it is further improved.
thanks
-john
Quoted 3 lines · Peter Zijlstra
The below is a compile tested only first draft so far. I'll go give it some runtime next.
So I will wait for the new version, but at first glance this matches the code I already reviewed in the past (at least, tried hard to review ;) and it looks correct.
Just a couple of almost cosmetic nits, feel free to ignore.
include/linux/percpu-rwsem.h line 16
Quoted 13 lines
include/linux/percpu-rwsem.h +3
-3@@ -10,29 +10,107 @@struct percpu_rw_semaphore {struct rcu_sync rss; -unsigned int __percpu *fast_read_ctr; +unsigned int __percpu *refcount;struct rw_semaphore rw_sem; -atomic_t slow_read_ctr; -wait_queue_head_t write_waitq; +wait_queue_head_t writer; +int state;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I think that this "int state" and "enum { readers_slow, readers_block }" just add a bit of complication/confusion.
All we need is the simple "bool readers_block" in percpu_rw_semaphore, no?
include/linux/percpu-rwsem.h line 22
Quoted 6 lines
include/linux/percpu-rwsem.h +5
-0+void percpu_down_write (struct percpu_rw_semaphore *sem) { +down_write (&sem->rw_sem); + +/* Notify readers to take the slow path. */ +rcu_sync_enter (&sem->rss);
I'd suggest to call rcu_sync_enter() before down_write(). This can help when we wait for another writer which holds this lock.
Oleg.
Peter Zijlstra
maintainerQuoted 10 lines · Oleg Nesterov
Quoted 3 lines · Peter Zijlstra
The below is a compile tested only first draft so far. I'll go give it some runtime next.
So I will wait for the new version, but at first glance this matches the code I already reviewed in the past (at least, tried hard to review ;) and it looks correct.
Just a couple of almost cosmetic nits, feel free to ignore.
Drat, I just mailed out the patches.. I can do a second version later.
Quoted 15 lines
include/linux/percpu-rwsem.h line 16
Quoted 13 lines
include/linux/percpu-rwsem.h +3
-3@@ -10,29 +10,107 @@struct percpu_rw_semaphore {struct rcu_sync rss; -unsigned int __percpu *fast_read_ctr; +unsigned int __percpu *refcount;struct rw_semaphore rw_sem; -atomic_t slow_read_ctr; -wait_queue_head_t write_waitq; +wait_queue_head_t writer; +int state;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 5 lines
I think that this "int state" and "enum {
readers_slow,readers_block}" just add a bit of complication/confusion.All we need is the simple "bool
readers_block" inpercpu_rw_semaphore, no?
So I detest bool in structures because sizeof(bool) isn't defined. Obviously an implementation needs to pick a size, but this is typically architecture ABIs, so sizes can differ between architectures.
But yes, I suppose "int readers_block" will do just fine.
IIRC, earlier version had more states, but that all went away.
Quoted 9 lines
include/linux/percpu-rwsem.h line 22
Quoted 6 lines
include/linux/percpu-rwsem.h +5
-0+void percpu_down_write (struct percpu_rw_semaphore *sem) { +down_write (&sem->rw_sem); + +/* Notify readers to take the slow path. */ +rcu_sync_enter (&sem->rss);I'd suggest to call
rcu_sync_enter()beforedown_write(). This can help when we wait for another writer which holds this lock.
Hurm, I think I figured that might have issues, but I cannot seem to think of any just now :-), yes can do.
Peter Zijlstra
maintainerQuoted 6 lines · Peter Zijlstra
Quoted 5 lines · Oleg Nesterov
Quoted 3 lines · Peter Zijlstra
The below is a compile tested only first draft so far. I'll go give it some runtime next.
Quoted 7 lines
Quoted 5 lines
So I will wait for the new version, but at first glance this matches the code I already reviewed in the past (at least, tried hard to review ;) and it looks correct.
Just a couple of almost cosmetic nits, feel free to ignore.
Drat, I just mailed out the patches.. I can do a second version later.
How about so on top of what I sent?
include/linux/percpu-rwsem.h +4 -5
@@ -13,11 +13,10 @@ struct percpu_rw_semaphore {unsigned int __percpu *read_count;struct rw_semaphore rw_sem;wait_queue_head_t writer; -int state; +int readers_block; }; -extern void __percpu_down_read (struct percpu_rw_semaphore *); -extern int __percpu_down_read_trylock (struct percpu_rw_semaphore *); +extern int __percpu_down_read (struct percpu_rw_semaphore *,int );extern void __percpu_up_read (struct percpu_rw_semaphore *);static inline void percpu_down_read (struct percpu_rw_semaphore *sem)
@@ -37,7 +36,7 @@ static inline void percpu_down_read(stru*/ __this_cpu_inc (*sem->read_count);if (unlikely (!rcu_sync_is_idle (&sem->rss))) -__percpu_down_read (sem);/* Unconditional memory barrier */ +__percpu_down_read (sem, false);/* Unconditional memory barrier */ preempt_enable ();/* * The barrier() from preempt_enable() prevents the compiler from
@@ -55,7 +54,7 @@ static inline int percpu_down_read_trylo*/ __this_cpu_inc (*sem->read_count);if (unlikely (!rcu_sync_is_idle (&sem->rss))) - ret =__percpu_down_read_trylock (sem); + ret =__percpu_down_read (sem, true);/* Unconditional memory barrier */ preempt_enable ();/* * The barrier() from preempt_enable() prevents the compiler from
kernel/locking/percpu-rwsem.c +25 -35
135 lines not on this page · raw message
Quoted 8 lines · Tejun Heo
Hello,
Quoted 5 lines · Peter Zijlstra
Quoted 4 lines
So, it's a percpu rwsem issue then. I haven't really followed the perpcpu rwsem changes closely. Oleg, are multi-milisec delay expected on down write expected with the current implementation of
percpu_rwsem?
Quoted 6 lines
Quoted 2 lines
There is a
synchronize_sched()in there, so sorta. That thing is heavily geared towards readers, as is the only 'sane' choice for global locks.It used to use the expedited variant until
001dac627ff3("locking/percpu-rwsem: Make use of thercu_syncinfrastructure"), so it might have been okay before then.
Just as a datapoint, just reverting 001dac627ff3 ("locking/percpu-rwsem: Make use of the rcu_sync infrastructure"), doesn't seem to help as much as your prior patch going to a normal rwsem.
I'm consistently seeing values in the 100-550us range (with occasional spikes around 6ms), with a max value at 36ms.
thanks
-john
Quoted 18 lines · Peter Zijlstra
Quoted 17 lines · Tejun Heo
Hello, John.
Quoted 13 lines · John Stultz
Quoted 8 lines · Tejun Heo
Quoted 4 lines · Tejun Heo
One interesting thing to try would be replacing it with a regular non-percpu rwsem and see how it behaves. That should easily tell us whether this is from actual contention or artifacts from
percpu_rwsemimplementation.So, something like the following. Can you please see whether this makes any difference?
Yea. So this brings it down for me closer to what we're seeing with the Dmitry's patch reverting the two problematic commits, usually 10-50us with one early spike at 18ms.
Quoted 7 lines
Quoted 4 lines
So, it's a percpu rwsem issue then. I haven't really followed the perpcpu rwsem changes closely. Oleg, are multi-milisec delay expected on down write expected with the current implementation of
percpu_rwsem?There is a
synchronize_sched()in there, so sorta. That thing is heavily geared towards readers, as is the only 'sane' choice for global locks.
Then one diagnostic step to take would be to replace that synchronize_sched() with synchronize_sched_expedited(), and see if that gets rid of the delays.
Not a particularly real-time-friendly fix, but certainly a good check on our various assumptions.
Thanx, Paul
------------------------------------------------------------------------
kernel/rcu/sync.c +3 -3
@@ -38,19 +38,19 @@ static const struct {#endif } gp_ops[] = { [RCU_SYNC] = { - .sync = synchronize_rcu, + .sync = synchronize_rcu_expedited, .call = call_rcu, .wait = rcu_barrier,__INIT_HELD (rcu_read_lock_held) }, [RCU_SCHED_SYNC] = { - .sync = synchronize_sched, + .sync = synchronize_sched_expedited, .call = call_rcu_sched, .wait = rcu_barrier_sched,__INIT_HELD (rcu_read_lock_sched_held) }, [RCU_BH_SYNC] = { - .sync = synchronize_rcu_bh, + .sync = synchronize_rcu_bh_expedited, .call = call_rcu_bh, .wait = rcu_barrier_bh,__INIT_HELD (rcu_read_lock_bh_held)
Peter Zijlstra
maintainerQuoted 2 lines · Paul E. McKenney
Not a particularly real-time-friendly fix, but certainly a good check on our various assumptions.
Not only RT, but also HPC and all the other RDMA userspace polling freaks ;-)
But yes, it would confirm that this is indeed the issue.
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Paul E. McKenney
Not a particularly real-time-friendly fix, but certainly a good check on our various assumptions.
Not only RT, but also HPC and all the other RDMA userspace polling freaks ;-)
If Tejun's lglock-like approach doesn't pan out, then it would quite easy to make a boot parameter that selected expedited vs. non-expedited behavior.
Thanx, Paul
Quoted 27 lines · Paul E. McKenney
Quoted 18 lines · Peter Zijlstra
Quoted 17 lines · Tejun Heo
Hello, John.
Quoted 13 lines · John Stultz
Quoted 8 lines · Tejun Heo
Quoted 4 lines · Tejun Heo
One interesting thing to try would be replacing it with a regular non-percpu rwsem and see how it behaves. That should easily tell us whether this is from actual contention or artifacts from
percpu_rwsemimplementation.So, something like the following. Can you please see whether this makes any difference?
Yea. So this brings it down for me closer to what we're seeing with the Dmitry's patch reverting the two problematic commits, usually 10-50us with one early spike at 18ms.
Quoted 7 lines
Quoted 4 lines
So, it's a percpu rwsem issue then. I haven't really followed the perpcpu rwsem changes closely. Oleg, are multi-milisec delay expected on down write expected with the current implementation of
percpu_rwsem?There is a
synchronize_sched()in there, so sorta. That thing is heavily geared towards readers, as is the only 'sane' choice for global locks.
Quoted 6 lines
Then one diagnostic step to take would be to replace that
synchronize_sched()withsynchronize_sched_expedited(), and see if that gets rid of the delays.Not a particularly real-time-friendly fix, but certainly a good check on our various assumptions.
All delays <200 us, but one that is 3 ms.
Quoted 32 lines
Thanx, Paul
------------------------------------------------------------------------
kernel/rcu/sync.c +3
-3@@ -38,19 +38,19 @@ static const struct {#endif } gp_ops[] = { [RCU_SYNC] = { - .sync = synchronize_rcu, + .sync = synchronize_rcu_expedited, .call = call_rcu, .wait = rcu_barrier,__INIT_HELD (rcu_read_lock_held) }, [RCU_SCHED_SYNC] = { - .sync = synchronize_sched, + .sync = synchronize_sched_expedited, .call = call_rcu_sched, .wait = rcu_barrier_sched,__INIT_HELD (rcu_read_lock_sched_held) }, [RCU_BH_SYNC] = { - .sync = synchronize_rcu_bh, + .sync = synchronize_rcu_bh_expedited, .call = call_rcu_bh, .wait = rcu_barrier_bh,__INIT_HELD (rcu_read_lock_bh_held)
Quoted 29 lines · Dmitry Shmidt
Quoted 27 lines · Paul E. McKenney
Quoted 25 lines · Peter Zijlstra
Quoted 13 lines · Tejun Heo
Hello, John.
Quoted 10 lines · John Stultz
Quoted 8 lines · Tejun Heo
Quoted 4 lines · Tejun Heo
One interesting thing to try would be replacing it with a regular non-percpu rwsem and see how it behaves. That should easily tell us whether this is from actual contention or artifacts from
percpu_rwsemimplementation.So, something like the following. Can you please see whether this makes any difference?
Quoted 8 lines
Quoted 3 lines
Yea. So this brings it down for me closer to what we're seeing with the Dmitry's patch reverting the two problematic commits, usually 10-50us with one early spike at 18ms.
So, it's a percpu rwsem issue then. I haven't really followed the perpcpu rwsem changes closely. Oleg, are multi-milisec delay expected on down write expected with the current implementation of
percpu_rwsem?There is a
synchronize_sched()in there, so sorta. That thing is heavily geared towards readers, as is the only 'sane' choice for global locks.
Quoted 8 lines
Quoted 6 lines
Then one diagnostic step to take would be to replace that
synchronize_sched()withsynchronize_sched_expedited(), and see if that gets rid of the delays.Not a particularly real-time-friendly fix, but certainly a good check on our various assumptions.
All delays <200 us, but one that is 3 ms.
Yep. I'm seeing the same here too with Paul's change.
thanks
-john
Quoted 29 lines · Dmitry Shmidt
Quoted 27 lines · Paul E. McKenney
Quoted 25 lines · Peter Zijlstra
Quoted 13 lines · Tejun Heo
Hello, John.
Quoted 10 lines · John Stultz
Quoted 8 lines · Tejun Heo
Quoted 4 lines · Tejun Heo
One interesting thing to try would be replacing it with a regular non-percpu rwsem and see how it behaves. That should easily tell us whether this is from actual contention or artifacts from
percpu_rwsemimplementation.So, something like the following. Can you please see whether this makes any difference?
Quoted 8 lines
Quoted 3 lines
Yea. So this brings it down for me closer to what we're seeing with the Dmitry's patch reverting the two problematic commits, usually 10-50us with one early spike at 18ms.
So, it's a percpu rwsem issue then. I haven't really followed the perpcpu rwsem changes closely. Oleg, are multi-milisec delay expected on down write expected with the current implementation of
percpu_rwsem?There is a
synchronize_sched()in there, so sorta. That thing is heavily geared towards readers, as is the only 'sane' choice for global locks.
Quoted 8 lines
Quoted 6 lines
Then one diagnostic step to take would be to replace that
synchronize_sched()withsynchronize_sched_expedited(), and see if that gets rid of the delays.Not a particularly real-time-friendly fix, but certainly a good check on our various assumptions.
All delays <200 us, but one that is 3 ms.
That does indicate that synchronize_sched() is the latency culprit.
Tejun's lglock-like approach seems to me to be the next thing to try.
Thanx, Paul
Quoted 31 lines
Quoted 30 lines
------------------------------------------------------------------------
kernel/rcu/sync.c +3
-3@@ -38,19 +38,19 @@ static const struct {#endif } gp_ops[] = { [RCU_SYNC] = { - .sync = synchronize_rcu, + .sync = synchronize_rcu_expedited, .call = call_rcu, .wait = rcu_barrier,__INIT_HELD (rcu_read_lock_held) }, [RCU_SCHED_SYNC] = { - .sync = synchronize_sched, + .sync = synchronize_sched_expedited, .call = call_rcu_sched, .wait = rcu_barrier_sched,__INIT_HELD (rcu_read_lock_sched_held) }, [RCU_BH_SYNC] = { - .sync = synchronize_rcu_bh, + .sync = synchronize_rcu_bh_expedited, .call = call_rcu_bh, .wait = rcu_barrier_bh,__INIT_HELD (rcu_read_lock_bh_held)
Quoted 8 lines · Tejun Heo
Quoted 4 lines · Tejun Heo
One interesting thing to try would be replacing it with a regular non-percpu rwsem and see how it behaves. That should easily tell us whether this is from actual contention or artifacts from
percpu_rwsemimplementation.So, something like the following. Can you please see whether this makes any difference?
It is making difference. We need to conduct more tests, but it looks much better. I see only one 18.5 ms delay. Most of time it is <200 us delay.
Quoted 100 lines
Thanks.
include/linux/cgroup-defs.h +4
-4@@ -14,7 +14,7 @@#include <linux/mutex.h> #include <linux/rcupdate.h> #include <linux/percpu-refcount.h> -#include <linux/percpu-rwsem.h> +#include <linux/rwsem.h> #include <linux/workqueue.h> #ifdef CONFIG_CGROUPS @@ -518,7 +518,7 @@ struct cgroup_subsys {unsigned int depends_on; }; -extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem; +extern struct rw_semaphore cgroup_threadgroup_rwsem;/** * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups @@ -529,7 +529,7 @@ extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem;*/ static inline void cgroup_threadgroup_change_begin (struct task_struct *tsk) { -percpu_down_read (&cgroup_threadgroup_rwsem); +down_read (&cgroup_threadgroup_rwsem); }/** @@ -541,7 +541,7 @@ static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)*/ static inline void cgroup_threadgroup_change_end (struct task_struct *tsk) { -percpu_up_read (&cgroup_threadgroup_rwsem); +up_read (&cgroup_threadgroup_rwsem); }#else /* CONFIG_CGROUPS */ kernel/cgroup.c +6
-6@@ -113,7 +113,7 @@ static DEFINE_SPINLOCK(cgroup_file_kn_lock);*/ static DEFINE_SPINLOCK (release_agent_path_lock); -struct percpu_rw_semaphore cgroup_threadgroup_rwsem; +DECLARE_RWSEM (cgroup_threadgroup_rwsem);#define cgroup_assert_mutex_or_rcu_locked () \ RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \@@ -2899,7 +2899,7 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,if (!cgrp)return -ENODEV ; -percpu_down_write (&cgroup_threadgroup_rwsem); +down_write (&cgroup_threadgroup_rwsem);rcu_read_lock ();if (pid) { tsk =find_task_by_vpid (pid);@@ -2937,7 +2937,7 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf, out_unlock_rcu:rcu_read_unlock (); out_unlock_threadgroup: -percpu_up_write (&cgroup_threadgroup_rwsem); +up_write (&cgroup_threadgroup_rwsem);for_each_subsys (ss, ssid)if (ss->post_attach) ss->post_attach ();@@ -3077,7 +3077,7 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)lockdep_assert_held (&cgroup_mutex); -percpu_down_write (&cgroup_threadgroup_rwsem); +down_write (&cgroup_threadgroup_rwsem);/* look up all csses currently attached to @cgrp's subtree */ spin_lock_bh (&css_set_lock);@@ -3112,7 +3112,7 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp) ret =cgroup_taskset_migrate (&tset, cgrp->root); out_finish:cgroup_migrate_finish (&preloaded_csets); -percpu_up_write (&cgroup_threadgroup_rwsem); +up_write (&cgroup_threadgroup_rwsem);return ret; }@@ -5601,7 +5601,7 @@ int __init cgroup_init(void)int ssid;BUILD_BUG_ON (CGROUP_SUBSYS_COUNT >16 ); -BUG_ON (percpu_init_rwsem (&cgroup_threadgroup_rwsem)); +//BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem)); BUG_ON (cgroup_init_cftypes (NULL , cgroup_dfl_base_files));BUG_ON (cgroup_init_cftypes (NULL , cgroup_legacy_base_files));
Quoted 42 lines · Tejun Heo
(cc'ing Oleg)
Hello,
Quoted 7 lines · John Stultz
So Dmitry Shmidt recently noticed that with 4.4 based systems we're
seeing quite a bit of performance overhead from__cgroup_procs_write().With 4.4 tree as it stands, we're seeing
__cgroup_procs_write()quite often take 10s of miliseconds to execute (with max times up in the 80ms range).Yikes, that's pretty high. Does this happen only while the system is generally busy or regardless of overall load?
Quoted 19 lines
While with 4.1 it was quite often in the single usec range, and max time values still in in sub-milisecond range.
The majority of these performance regressions seem to come from the locking changes in:
3014dde762f6("cgroup: simplify threadgroup locking")
and1ed1328792ff("sched, cgroup: replacesignal_struct->group_rwsemwith a globalpercpu_rwsem")Dmitry has found that by reverting these two changes (which don't revert easiliy), we can get back down to tens 10-100 usec range for most calls, with max values occasionally spiking to ~18ms.
Those two commits do talk about performance regressions, that were supposedly alleviated by
percpu_rwsemchanges, but I'm not sure we are seeing this.Quoted 6 lines
In
1ed1328792ff, the commit talks about the write path being a fairly cold path, but with Android I worry this may not actually be the case, as Android uses cpuset cgroups to group tasks into foreground and background tasks, but this means when switching applications, tasks are migrated between cgroups. Putting an additional 80 milisecond delay on this adds potentially visible latencies on task switching.
Quoted 4 lines
Switching between foreground and background isn't a hot path. It's human initiated operations after all. It taking 80 msecs sure is problematic but I'm skeptical that this is from actual contention given that the only reader side holders are fork and exit paths.
Slight correction here, we move tasks to the foreground cgroup and back around binder IPC calls from foreground processes to background processes, so it is significantly hotter than just human initiated operations.
Quoted 14 lines
Quoted 4 lines
Reverting those two changes in the Android common.git tree doesn't feel like a good long term solution here, so I was wondering if you had any thoughts on how to further reduce the performance regression here?
One interesting thing to try would be replacing it with a regular non-percpu rwsem and see how it behaves. That should easily tell us whether this is from actual contention or artifacts from
percpu_rwsemimplementation.Thanks.
--
tejun
Tejun Heo
maintainerHello,
Quoted 9 lines · Colin Cross
Quoted 4 lines
Switching between foreground and background isn't a hot path. It's human initiated operations after all. It taking 80 msecs sure is problematic but I'm skeptical that this is from actual contention given that the only reader side holders are fork and exit paths.
Slight correction here, we move tasks to the foreground cgroup and back around binder IPC calls from foreground processes to background processes, so it is significantly hotter than just human initiated operations.
Oh I see. That's extreme and you would be paying in terms of migration overhead. It really doesn't make sense to generally optimize for migration as that often translates directly to overhead in actual hot paths in various controllers. That said, I don't think it'd be too difficult to keep things acceptable for the android case.
Thanks.
Signature · 1 line
tejun