Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1583370

[PATCH v2 2/2] sched/loadavg: Use {READ,WRITE}_ONCE() for sample window

Path csiph.com!eternal-september.org!feeder.eternal-september.org!aioe.org!bofh.it!news.nic.it!robomod
From Matt Fleming <matt@codeblueprint.co.uk>
Newsgroups linux.kernel
Subject [PATCH v2 2/2] sched/loadavg: Use {READ,WRITE}_ONCE() for sample window
Date Fri, 17 Feb 2017 13:10:01 +0100
Message-ID <tbPwl-47n-3@gated-at.bofh.it> (permalink)
References <tbPwl-47n-5@gated-at.bofh.it>
Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=codeblueprint-co-uk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Hx7jRx7IYG0Fcb6rA1cRjd5sux5HvFU7WzEQgp9F7rw=; b=FfS77P8INzjHAhRKu9sV3eLsSBI1GyN/xJ1f1ehqR/6+wohADaXaLobErqDZla3Jeo yT9PqFtJNUiRWzVzLfGUEasOSAq27zGBOAMZoHb0vda2GJu/0SYYGjF2VoUL+gwycIn4 T6vJMz6d2A5xtgOR6+AUlCVaewagpNNWdbRr91bWjhp4K5c/QRv1CW0Op+sMEp7f+32w OMNFipL1axKUJFJbBbbg9vZhbOp52eB4W9IL1qZnysz8pMuBUWibguOLOPTWIbOdggZj qL9tpKQPqga1ORp5IrubQmQdZ41lqeXCK4gZ7YjbAvz2AYeTX5CqdIj9dNw6IwhdvZnj y2/Q==
X-Google-Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Hx7jRx7IYG0Fcb6rA1cRjd5sux5HvFU7WzEQgp9F7rw=; b=ov3CCJN+FAvWAs1igW7jO3GkyCl5XAy0C96ORSeMXvOOW0tbFNFjnyeRjLjT9bKZs3 L8QlEE7pP1DrLcE2WBPlY9IIhYl8BNrgdJanpQCqfCCkSQfKKSuGAi1xo0bFXcMDM8MY a09imhZUrmyvzc5+p46ZIjsWa3uN2dajrh0FBy3smpZjXgKDazAjC/kIDltaf00YE240 TriBQaC85PvDbPPEG3EnXix9ARCH+tVYhk/D1YL8XTooCoKWGndLAGCWa0oFN+u6vNfU ekC6z03v/r7HV+foVsQdTjXn3JZMQXcq8ZMgb2dYfxG/TtBJ/2Ut4GMOhgAP3RqVdmiO XSaA==
X-Gm-Message-State AMke39nQHqC3uJCHDdbim9nWsmWBhaPLhWyXVqUF+JrePx1kNz8BChMZX8ZDrzEhTX4hvw==
X-Received by 10.223.128.202 with SMTP id 68mr6851675wrl.92.1487333255518; Fri, 17 Feb 2017 04:07:35 -0800 (PST)
X-Mailer git-send-email 2.10.0
Sender robomod@news.nic.it
List-ID <linux-kernel.vger.kernel.org>
X-Mailing-List linux-kernel@vger.kernel.org
Approved robomod@news.nic.it
Lines 95
Organization linux.* mail to news gateway
X-Original-Cc linux-kernel@vger.kernel.org, Matt Fleming <matt@codeblueprint.co.uk>, Mike Galbraith <umgwanakikbuti@gmail.com>, Morten Rasmussen <morten.rasmussen@arm.com>, Vincent Guittot <vincent.guittot@linaro.org>
X-Original-Date Fri, 17 Feb 2017 12:07:31 +0000
X-Original-Message-ID <20170217120731.11868-3-matt@codeblueprint.co.uk>
X-Original-References <20170217120731.11868-1-matt@codeblueprint.co.uk>
X-Original-Sender linux-kernel-owner@vger.kernel.org
Xref csiph.com linux.kernel:1583370

Show key headers only | View raw


'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 <peterz@infradead.org>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Mike Galbraith <umgwanakikbuti@gmail.com>
Cc: Morten Rasmussen <morten.rasmussen@arm.com>
Cc: Vincent Guittot <vincent.guittot@linaro.org>
Signed-off-by: Matt Fleming <matt@codeblueprint.co.uk>
---
 kernel/sched/loadavg.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/kernel/sched/loadavg.c b/kernel/sched/loadavg.c
index ec91fcc09bfe..3f96a7d014ce 100644
--- a/kernel/sched/loadavg.c
+++ b/kernel/sched/loadavg.c
@@ -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.
-- 
2.10.0

Back to linux.kernel | Previous | Next | Find similar | Unroll thread


Thread

[PATCH v2 2/2] sched/loadavg: Use {READ,WRITE}_ONCE() for sample window Matt Fleming <matt@codeblueprint.co.uk> - 2017-02-17 13:10 +0100

csiph-web