sched/loadavg: Fix loadavg spikes and sprinkle {READ,WRITE}_ONCE()
Subsystems (2)
v2 cover sched/loadavg: Fix loadavg spikes and sprinkle {READ,WRITE}_ONCE()
Peter, Frederic,
Here's a v2 of the patch series to fix the loadavg spikes I'm seeing on a v3.12 based kernel, caused by delaying load sampling if a sample period was crossed while in NO_HZ idle.
I tried to make the changelog for PATCH 1 clearer this time around by incorporating suggestions from both of you. Please let me know if it's still unclear.
PATCH 2 addresses Peter's comment:
"Irrespective the above though; should we not make this:
+ this_rq->calc_load_update = READ_ONCE(calc_load_update);
because if for some reason we do a double load of calc_load_update and see two different values, weird stuff could happen.
And because, on general principle, a READ_ONCE() should be paired with a WRITE_ONCE(), that should be done too I suppose."
The v1 of the patch can be found here:
https://lkml.kernel.org/r/[email protected]
Matt Fleming (2):
sched/loadavg: Avoid loadavg spikes caused by delayed NO_HZ accounting sched/loadavg: Use {READ,WRITE}_ONCE() for sample window
kernel/sched/loadavg.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
Signature · 1 line
2.10.0
PATCH v2 1/2 sched/loadavg: Avoid loadavg spikes caused by delayed NO_HZ accounting
If we crossed a sample window while in NO_HZ we will add LOAD_FREQ to the pending sample window time on exit, setting the next update not one window into the future, but two.
This situation on exiting NO_HZ is described by:
this_rq->calc_load_update < jiffies < calc_load_update
In this scenario, what we should be doing is:
this_rq->calc_load_update = calc_load_update [ next window ]
But what we actually do is:
this_rq->calc_load_update = calc_load_update + LOAD_FREQ [ next+1 window ]
This has the effect of delaying load average updates for potentially up to ~9seconds.
This can result in huge spikes in the load average values due to per-cpu uninterruptible task counts being out of sync when accumulated across all CPUs.
It's safe to update the per-cpu active count if we wake between sample windows because any load that we left in 'calc_load_idle' will have been zero'd when the idle load was folded in calc_global_load().
This issue is easy to reproduce before,
commit 9d89c257dfb9 ("sched/fair: Rewrite runnable load and utilization average tracking")
just by forking short-lived process pipelines built from ps(1) and grep(1) in a loop. I'm unable to reproduce the spikes after that commit, but the bug still seems to be present from code review.
--- kernel/sched/loadavg.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Changes in v2:
- Folded in Peter's suggestion for how to fix this.
- Tried to clairfy the changelog based on feedback from Peter and Frederic
kernel/sched/loadavg.c +2 -2
@@ -201,8 +201,9 @@ void calc_load_exit_idle(void)struct rq *this_rq =this_rq ();/* -* If we're still before the sample window, we're done. +* If we're still before the pending sample window, we're done. */ + this_rq->calc_load_update = calc_load_update;if (time_before (jiffies, this_rq->calc_load_update))return ;
@@ -211,7 +212,6 @@ void calc_load_exit_idle(void)* accounted through the nohz accounting, so skip the entire deal and * sync up for the next window. */ - this_rq->calc_load_update = calc_load_update;if (time_before (jiffies, this_rq->calc_load_update +10 )) this_rq->calc_load_update +=LOAD_FREQ ; }
Signature · 1 line
2.10.0
Re: [PATCH v2 1/2] sched/loadavg: Avoid loadavg spikes caused by delayed NO_HZ accounting
Quoted 35 lines · Matt Fleming
If we crossed a sample window while in
NO_HZwe will addLOAD_FREQto the pending sample window time on exit, setting the next update not one window into the future, but two.This situation on exiting
NO_HZis described by:
this_rq->calc_load_update< jiffies <calc_load_updateIn this scenario, what we should be doing is:
this_rq->calc_load_update=calc_load_update[ next window ]But what we actually do is:
this_rq->calc_load_update=calc_load_update+LOAD_FREQ[ next+1 window ]This has the effect of delaying load average updates for potentially up to ~9seconds.
This can result in huge spikes in the load average values due to per-cpu uninterruptible task counts being out of sync when accumulated across all CPUs.
It's safe to update the per-cpu active count if we wake between sample windows because any load that we left in '
calc_load_idle' will have been zero'd when the idle load was folded incalc_global_load().This issue is easy to reproduce before,
commit
9d89c257dfb9("sched/fair: Rewrite runnable load and utilization average tracking")just by forking short-lived process pipelines built from ps(1) and grep(1) in a loop. I'm unable to reproduce the spikes after that commit, but the bug still seems to be present from code review.
Quoted 5 lines
Fixes commit 5167e8d ("sched/nohz: Rewrite and fix load-avg computation -- again")Cc Peter Zijlstra <[email protected]>Cc Mike Galbraith <[email protected]>Cc Morten Rasmussen <[email protected]>Cc Vincent Guittot <[email protected]>
Thanks it's much clearer now!
Re: [PATCH v2 1/2] sched/loadavg: Avoid loadavg spikes caused by delayed NO_HZ accounting
In reply to Matt Fleming2017-02-17 20:07 GMT+08:00 Matt Fleming <[email protected]>:
Quoted 35 lines
If we crossed a sample window while in
NO_HZwe will addLOAD_FREQto the pending sample window time on exit, setting the next update not one window into the future, but two.This situation on exiting
NO_HZis described by:
this_rq->calc_load_update< jiffies <calc_load_updateIn this scenario, what we should be doing is:
this_rq->calc_load_update=calc_load_update[ next window ]But what we actually do is:
this_rq->calc_load_update=calc_load_update+LOAD_FREQ[ next+1 window ]This has the effect of delaying load average updates for potentially up to ~9seconds.
This can result in huge spikes in the load average values due to per-cpu uninterruptible task counts being out of sync when accumulated across all CPUs.
It's safe to update the per-cpu active count if we wake between sample windows because any load that we left in '
calc_load_idle' will have been zero'd when the idle load was folded incalc_global_load().This issue is easy to reproduce before,
commit
9d89c257dfb9("sched/fair: Rewrite runnable load and utilization average tracking")just by forking short-lived process pipelines built from ps(1) and grep(1) in a loop. I'm unable to reproduce the spikes after that commit, but the bug still seems to be present from code review.
Quoted 8 lines
Fixes commit 5167e8d ("sched/nohz: Rewrite and fix load-avg computation -- again")Cc Peter Zijlstra <[email protected]>Cc Mike Galbraith <[email protected]>Cc Morten Rasmussen <[email protected]>Cc Vincent Guittot <[email protected]>Cc Frederic Weisbecker <[email protected]>Cc <[email protected]> # v3.5+Signed-off-by Matt Fleming <[email protected]>
Quoted 37 lines
--- kernel/sched/loadavg.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Changes in v2:
- Folded in Peter's suggestion for how to fix this.
- Tried to clairfy the changelog based on feedback from Peter and Frederic
kernel/sched/loadavg.c +2
-3@@ -201,8 +201,9 @@ void calc_load_exit_idle(void)struct rq *this_rq =this_rq ();/* -* If we're still before the sample window, we're done. +* If we're still before the pending sample window, we're done. */ + this_rq->calc_load_update = calc_load_update;if (time_before (jiffies, this_rq->calc_load_update))return ;@@ -211,7 +212,6 @@ void calc_load_exit_idle(void)* accounted through the nohz accounting, so skip the entire deal and * sync up for the next window. */ - this_rq->calc_load_update = calc_load_update;if (time_before (jiffies, this_rq->calc_load_update +10 )) this_rq->calc_load_update +=LOAD_FREQ ; } --2.10.0
tip-bot for Matt Fleming
merge bot[tip:sched/core] sched/loadavg: Avoid loadavg spikes caused by delayed NO_HZ accounting
In reply to Matt Flemingsched/loadavg: Avoid loadavg spikes caused by delayed NO_HZ accounting
Commit-ID: 6e5f32f7a43f45ee55c401c0b9585eb01f9629a8 Gitweb: http://git.kernel.org/tip/6e5f32f7a43f45ee55c401c0b9585eb01f9629a8 Author: Matt Fleming <[email protected]> AuthorDate: Fri, 17 Feb 2017 12:07:30 +0000 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 16 Mar 2017 09:21:00 +0100
sched/loadavg: Avoid loadavg spikes caused by delayed NO_HZ accounting
If we crossed a sample window while in NO_HZ we will add LOAD_FREQ to the pending sample window time on exit, setting the next update not one window into the future, but two.
This situation on exiting NO_HZ is described by:
this_rq->calc_load_update < jiffies < calc_load_update
In this scenario, what we should be doing is:
this_rq->calc_load_update = calc_load_update [ next window ]
But what we actually do is:
this_rq->calc_load_update = calc_load_update + LOAD_FREQ [ next+1 window ]
This has the effect of delaying load average updates for potentially up to ~9seconds.
This can result in huge spikes in the load average values due to per-cpu uninterruptible task counts being out of sync when accumulated across all CPUs.
It's safe to update the per-cpu active count if we wake between sample windows because any load that we left in 'calc_load_idle' will have been zero'd when the idle load was folded in calc_global_load().
This issue is easy to reproduce before,
commit 9d89c257dfb9 ("sched/fair: Rewrite runnable load and utilization average tracking")
just by forking short-lived process pipelines built from ps(1) and grep(1) in a loop. I'm unable to reproduce the spikes after that commit, but the bug still seems to be present from code review.
--- kernel/sched/loadavg.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/loadavg.c +2 -2
@@ -202,8 +202,9 @@ void calc_load_exit_idle(void)struct rq *this_rq =this_rq ();/* -* If we're still before the sample window, we're done. +* If we're still before the pending sample window, we're done. */ + this_rq->calc_load_update = calc_load_update;if (time_before (jiffies, this_rq->calc_load_update))return ;
@@ -212,7 +213,6 @@ void calc_load_exit_idle(void)* accounted through the nohz accounting, so skip the entire deal and * sync up for the next window. */ - this_rq->calc_load_update = calc_load_update;if (time_before (jiffies, this_rq->calc_load_update +10 )) this_rq->calc_load_update +=LOAD_FREQ ; }
PATCH v2 2/2 sched/loadavg: Use {READ,WRITE}_ONCE() for sample window
In reply to Matt Fleming'calc_load_update' is accessed without any kind of locking and there's a clear assumption in the code that only a single value is read or written.
Make this explicit by using READ_ONCE() and WRITE_ONCE(), and avoid unintentionally seeing multiple values, or having the load/stores split.
Technically the loads in calc_global_*() don't require this since those are the only functions that update 'calc_load_update', but I've added the READ_ONCE() for consistency.
--- kernel/sched/loadavg.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
kernel/sched/loadavg.c +11 -7
@@ -168,7 +168,7 @@ static inline int calc_load_write_idx(void)* If the folding window started, make sure we start writing in the * next idle-delta. */ -if (!time_before (jiffies, calc_load_update)) +if (!time_before (jiffies,READ_ONCE (calc_load_update))) idx++;return idx &1 ;
@@ -203,7 +203,7 @@ void calc_load_exit_idle(void)/* * If we're still before the pending sample window, we're done. */ - this_rq->calc_load_update = calc_load_update; + this_rq->calc_load_update =READ_ONCE (calc_load_update);if (time_before (jiffies, this_rq->calc_load_update))return ;
@@ -307,13 +307,15 @@ calc_load_n(unsigned long load, unsigned long exp,*/ static void calc_global_nohz (void ) { +unsigned long sample_window;long delta, active, n; -if (!time_before (jiffies, calc_load_update +10 )) { + sample_window =READ_ONCE (calc_load_update); +if (!time_before (jiffies, sample_window +10 )) {/* * Catch-up, fold however many we are behind still */ - delta = jiffies - calc_load_update -10 ; + delta = jiffies - sample_window -10 ; n =1 + (delta /LOAD_FREQ ); active =atomic_long_read (&calc_load_tasks);
@@ -323,7 +325,7 @@ static void calc_global_nohz(void) avenrun[1 ] =calc_load_n (avenrun[1 ],EXP_5 , active, n); avenrun[2 ] =calc_load_n (avenrun[2 ],EXP_15 , active, n); - calc_load_update += n *LOAD_FREQ ; +WRITE_ONCE (calc_load_update, sample_window + n *LOAD_FREQ ); }/*
@@ -351,9 +353,11 @@ static inline void calc_global_nohz(void) { }*/ void calc_global_load (unsigned long ticks) { +unsigned long sample_window;long active, delta; -if (time_before (jiffies, calc_load_update +10 )) + sample_window =READ_ONCE (calc_load_update); +if (time_before (jiffies, sample_window +10 ))return ;/*
@@ -370,7 +374,7 @@ void calc_global_load(unsigned long ticks) avenrun[1 ] =calc_load (avenrun[1 ],EXP_5 , active); avenrun[2 ] =calc_load (avenrun[2 ],EXP_15 , active); - calc_load_update +=LOAD_FREQ ; +WRITE_ONCE (calc_load_update, sample_window +LOAD_FREQ );/* * In case we idled for multiple LOAD_FREQ intervals, catch up in bulk.
Signature · 1 line
2.10.0
tip-bot for Matt Fleming
merge bot[tip:sched/core] sched/loadavg: Use {READ,WRITE}_ONCE() for sample window
sched/loadavg: Use {READ,WRITE}_ONCE() for sample window
Commit-ID: caeb5882979bc6f3c8766fcf59c6269b38f521bc Gitweb: http://git.kernel.org/tip/caeb5882979bc6f3c8766fcf59c6269b38f521bc Author: Matt Fleming <[email protected]> AuthorDate: Fri, 17 Feb 2017 12:07:31 +0000 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 16 Mar 2017 09:21:01 +0100
sched/loadavg: Use {READ,WRITE}_ONCE() for sample window
'calc_load_update' is accessed without any kind of locking and there's a clear assumption in the code that only a single value is read or written.
Make this explicit by using READ_ONCE() and WRITE_ONCE(), and avoid unintentionally seeing multiple values, or having the load/stores split.
Technically the loads in calc_global_*() don't require this since those are the only functions that update 'calc_load_update', but I've added the READ_ONCE() for consistency.
--- kernel/sched/loadavg.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
kernel/sched/loadavg.c +11 -7
@@ -169,7 +169,7 @@ static inline int calc_load_write_idx(void)* If the folding window started, make sure we start writing in the * next idle-delta. */ -if (!time_before (jiffies, calc_load_update)) +if (!time_before (jiffies,READ_ONCE (calc_load_update))) idx++;return idx &1 ;
@@ -204,7 +204,7 @@ void calc_load_exit_idle(void)/* * If we're still before the pending sample window, we're done. */ - this_rq->calc_load_update = calc_load_update; + this_rq->calc_load_update =READ_ONCE (calc_load_update);if (time_before (jiffies, this_rq->calc_load_update))return ;
@@ -308,13 +308,15 @@ calc_load_n(unsigned long load, unsigned long exp,*/ static void calc_global_nohz (void ) { +unsigned long sample_window;long delta, active, n; -if (!time_before (jiffies, calc_load_update +10 )) { + sample_window =READ_ONCE (calc_load_update); +if (!time_before (jiffies, sample_window +10 )) {/* * Catch-up, fold however many we are behind still */ - delta = jiffies - calc_load_update -10 ; + delta = jiffies - sample_window -10 ; n =1 + (delta /LOAD_FREQ ); active =atomic_long_read (&calc_load_tasks);
@@ -324,7 +326,7 @@ static void calc_global_nohz(void) avenrun[1 ] =calc_load_n (avenrun[1 ],EXP_5 , active, n); avenrun[2 ] =calc_load_n (avenrun[2 ],EXP_15 , active, n); - calc_load_update += n *LOAD_FREQ ; +WRITE_ONCE (calc_load_update, sample_window + n *LOAD_FREQ ); }/*
@@ -352,9 +354,11 @@ static inline void calc_global_nohz(void) { }*/ void calc_global_load (unsigned long ticks) { +unsigned long sample_window;long active, delta; -if (time_before (jiffies, calc_load_update +10 )) + sample_window =READ_ONCE (calc_load_update); +if (time_before (jiffies, sample_window +10 ))return ;/*
@@ -371,7 +375,7 @@ void calc_global_load(unsigned long ticks) avenrun[1 ] =calc_load (avenrun[1 ],EXP_5 , active); avenrun[2 ] =calc_load (avenrun[2 ],EXP_15 , active); - calc_load_update +=LOAD_FREQ ; +WRITE_ONCE (calc_load_update, sample_window +LOAD_FREQ );/* * In case we idled for multiple LOAD_FREQ intervals, catch up in bulk.