linux-arch.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Peter Zijlstra <a.p.zijlstra@chello.nl>
To: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Juri Lelli <juri.lelli@gmail.com>, Ingo Molnar <mingo@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>
Subject: [RFC][PATCH 1/3] kernel,sched,time: Clean up gcc work-arounds
Date: Tue, 24 Apr 2012 18:10:40 +0200	[thread overview]
Message-ID: <20120424162224.454623618@chello.nl> (raw)
In-Reply-To: 20120424161039.293018424@chello.nl

[-- Attachment #1: kernel-vs-gcc-while.patch --]
[-- Type: text/plain, Size: 3381 bytes --]

We've grown various copies of a particular gcc work-around,
consolidate them into one and add a larger comment.

Cc: Ingo Molnar <mingo@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
---
 include/linux/compiler.h |   12 ++++++++++++
 include/linux/math64.h   |    4 +---
 kernel/sched/core.c      |    8 ++------
 kernel/sched/fair.c      |    8 ++------
 kernel/time.c            |   11 ++++-------
 5 files changed, 21 insertions(+), 22 deletions(-)

--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -310,4 +310,16 @@ void ftrace_likely_update(struct ftrace_
  */
 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
 
+/*
+ * Avoid gcc loop optimization by clobbering a variable, forcing a reload
+ * and invalidating the optimization.
+ *
+ * The optimization in question transforms various loops into divisions/modulo
+ * operations, this is a problem when either the resulting operation generates
+ * unimplemented libgcc functions (u64 divisions for example) or the loop is
+ * known not to contain a lot of iterations and the division is in fact more
+ * expensive.
+ */
+#define __gcc_dont_optimize_loop(var) asm("" "+rm" (var))
+
 #endif /* __LINUX_COMPILER_H */
--- a/include/linux/math64.h
+++ b/include/linux/math64.h
@@ -105,9 +105,7 @@ __iter_div_u64_rem(u64 dividend, u32 div
 	u32 ret = 0;
 
 	while (dividend >= divisor) {
-		/* The following asm() prevents the compiler from
-		   optimising this loop into a modulo operation.  */
-		asm("" : "+rm"(dividend));
+		__gcc_dont_optimize_loop(dividend);
 
 		dividend -= divisor;
 		ret++;
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -628,12 +628,8 @@ void sched_avg_update(struct rq *rq)
 	s64 period = sched_avg_period();
 
 	while ((s64)(rq->clock - rq->age_stamp) > period) {
-		/*
-		 * Inline assembly required to prevent the compiler
-		 * optimising this loop into a divmod call.
-		 * See __iter_div_u64_rem() for another example of this.
-		 */
-		asm("" : "+rm" (rq->age_stamp));
+		__gcc_dont_optimize_loop(rq->age_stamp);
+
 		rq->age_stamp += period;
 		rq->rt_avg /= 2;
 	}
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -853,12 +853,8 @@ static void update_cfs_load(struct cfs_r
 		update_cfs_rq_load_contribution(cfs_rq, global_update);
 
 	while (cfs_rq->load_period > period) {
-		/*
-		 * Inline assembly required to prevent the compiler
-		 * optimising this loop into a divmod call.
-		 * See __iter_div_u64_rem() for another example of this.
-		 */
-		asm("" : "+rm" (cfs_rq->load_period));
+		__gcc_dont_optimize_loop(cfs_rq->load_period);
+
 		cfs_rq->load_period /= 2;
 		cfs_rq->load_avg /= 2;
 	}
--- a/kernel/time.c
+++ b/kernel/time.c
@@ -349,17 +349,14 @@ EXPORT_SYMBOL(mktime);
 void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec)
 {
 	while (nsec >= NSEC_PER_SEC) {
-		/*
-		 * The following asm() prevents the compiler from
-		 * optimising this loop into a modulo operation. See
-		 * also __iter_div_u64_rem() in include/linux/time.h
-		 */
-		asm("" : "+rm"(nsec));
+		__gcc_dont_optimize_loop(nsec);
+
 		nsec -= NSEC_PER_SEC;
 		++sec;
 	}
 	while (nsec < 0) {
-		asm("" : "+rm"(nsec));
+		__gcc_dont_optimize_loop(nsec);
+
 		nsec += NSEC_PER_SEC;
 		--sec;
 	}

  reply	other threads:[~2012-04-24 16:10 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-24 16:10 [RFC][PATCH 0/3] gcc work-around and math128 Peter Zijlstra
2012-04-24 16:10 ` Peter Zijlstra [this message]
2012-04-24 16:10   ` [RFC][PATCH 1/3] kernel,sched,time: Clean up gcc work-arounds Peter Zijlstra
2012-04-24 19:39   ` Linus Torvalds
2012-04-25  0:14   ` Stephen Rothwell
2012-04-25  0:14     ` Stephen Rothwell
2012-04-24 16:10 ` [RFC][PATCH 2/3] math128: Introduce {mult,add,cmp}_u128 Peter Zijlstra
2012-04-24 16:10   ` Peter Zijlstra
2012-04-24 19:37   ` Linus Torvalds
2012-04-24 19:43     ` Peter Zijlstra
2012-04-24 19:43       ` Peter Zijlstra
2012-04-24 20:12     ` Måns Rullgård
2012-04-24 20:12       ` Måns Rullgård
2012-04-24 21:54     ` Peter Zijlstra
2012-04-25  0:09       ` H. Peter Anvin
2012-04-25  1:46       ` Linus Torvalds
2012-04-25  1:46         ` Linus Torvalds
2012-04-25  8:35         ` Peter Zijlstra
2012-04-25 10:13         ` Peter Zijlstra
2012-04-25  0:23   ` Stephen Rothwell
2012-04-25  8:11     ` Peter Zijlstra
2012-04-25 11:23       ` Geert Uytterhoeven
2012-04-25 11:58         ` Peter Zijlstra
2012-04-25 14:35           ` Geert Uytterhoeven
2012-04-25 15:09             ` Peter Zijlstra
2012-04-24 16:10 ` [RFC][PATCH 3/3] math128, x86_64: Implement {mult,add}_u128 in 64bit asm Peter Zijlstra
2012-04-24 16:10   ` Peter Zijlstra
2012-04-24 16:34   ` H. Peter Anvin
2012-04-24 16:36     ` Peter Zijlstra
2012-04-24 17:17       ` H. Peter Anvin
2012-04-24 17:19         ` Peter Zijlstra
2012-04-24 17:20           ` H. Peter Anvin
2012-04-24 22:00             ` Peter Zijlstra
2012-04-24 22:03               ` H. Peter Anvin
2012-04-24 17:22 ` [RFC][PATCH 0/3] gcc work-around and math128 H. Peter Anvin
2012-04-24 17:27   ` H. Peter Anvin
2012-04-24 21:15 ` Andy Lutomirski
2012-04-24 21:18   ` Linus Torvalds
2012-04-24 21:32   ` Peter Zijlstra
2012-04-24 21:35     ` Andy Lutomirski
2012-04-24 21:35       ` Andy Lutomirski
2012-04-24 21:51       ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120424162224.454623618@chello.nl \
    --to=a.p.zijlstra@chello.nl \
    --cc=akpm@linux-foundation.org \
    --cc=juri.lelli@gmail.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).