Skip to content
This repository has been archived by the owner on Mar 17, 2019. It is now read-only.

Commit

Permalink
UPSTREAM: sched/loadavg: Use {READ,WRITE}_ONCE() for sample window
Browse files Browse the repository at this point in the history
Commit caeb5882979bc6f3c8766fcf59c6269b38f521bc upstream.

'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.

Suggested-by: Peter Zijlstra <[email protected]>
Signed-off-by: Matt Fleming <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Cc: Frederic Weisbecker <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Mike Galbraith <[email protected]>
Cc: Mike Galbraith <[email protected]>
Cc: Morten Rasmussen <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Vincent Guittot <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
Signed-off-by: Harsh Shandilya <[email protected]>
  • Loading branch information
mfleming authored and Harsh Shandilya committed Jun 22, 2018
1 parent 43aed94 commit 3004fd6
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions kernel/sched/loadavg.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,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;
Expand Down Expand Up @@ -304,13 +304,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);
Expand All @@ -320,7 +322,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);
}

/*
Expand Down Expand Up @@ -348,9 +350,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;

/*
Expand All @@ -367,7 +371,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.
Expand Down

3 comments on commit 3004fd6

@ahmedradaideh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Harsh
This commit not affect.
loadavg.c file mismerge by CAF
See:
ahmedradaideh/OnePlus3T@9944329
ahmedradaideh/OnePlus3T@5c160d8

of course there is more :)

@msfjarvis
Copy link
Owner

@msfjarvis msfjarvis commented on 3004fd6 Jun 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sigh, thanks Qualcomm. I've queued for the revert the next build. Thanks!

Edit: I've also picked up the two commits you linked.

@ahmedradaideh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.