Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit caeb5882 authored by Matt Fleming's avatar Matt Fleming Committed by Ingo Molnar
Browse files

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.

Suggested-by: default avatarPeter Zijlstra <peterz@infradead.org>
Signed-off-by: default avatarMatt Fleming <matt@codeblueprint.co.uk>
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Mike Galbraith <umgwanakikbuti@gmail.com>
Cc: Morten Rasmussen <morten.rasmussen@arm.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vincent Guittot <vincent.guittot@linaro.org>
Link: http://lkml.kernel.org/r/20170217120731.11868-3-matt@codeblueprint.co.uk


Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
parent 6e5f32f7
Loading
Loading
Loading
Loading
+11 −7
Original line number Original line Diff line number Diff line
@@ -169,7 +169,7 @@ static inline int calc_load_write_idx(void)
	 * If the folding window started, make sure we start writing in the
	 * If the folding window started, make sure we start writing in the
	 * next idle-delta.
	 * next idle-delta.
	 */
	 */
	if (!time_before(jiffies, calc_load_update))
	if (!time_before(jiffies, READ_ONCE(calc_load_update)))
		idx++;
		idx++;


	return idx & 1;
	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.
	 * 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))
	if (time_before(jiffies, this_rq->calc_load_update))
		return;
		return;


@@ -308,13 +308,15 @@ calc_load_n(unsigned long load, unsigned long exp,
 */
 */
static void calc_global_nohz(void)
static void calc_global_nohz(void)
{
{
	unsigned long sample_window;
	long delta, active, n;
	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
		 * 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);
		n = 1 + (delta / LOAD_FREQ);


		active = atomic_long_read(&calc_load_tasks);
		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[1] = calc_load_n(avenrun[1], EXP_5, active, n);
		avenrun[2] = calc_load_n(avenrun[2], EXP_15, 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)
void calc_global_load(unsigned long ticks)
{
{
	unsigned long sample_window;
	long active, delta;
	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;
		return;


	/*
	/*
@@ -371,7 +375,7 @@ void calc_global_load(unsigned long ticks)
	avenrun[1] = calc_load(avenrun[1], EXP_5, active);
	avenrun[1] = calc_load(avenrun[1], EXP_5, active);
	avenrun[2] = calc_load(avenrun[2], EXP_15, 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.
	 * In case we idled for multiple LOAD_FREQ intervals, catch up in bulk.