From: Jiri Olsa <jolsa@redhat•com>
To: Vinson Lee <vlee@twopensource•com>,
Arnaldo Carvalho de Melo <acme@kernel•org>
Cc: linux-next@vger•kernel.org,
"Peter Zijlstra" <a.p.zijlstra@chello•nl>,
"Ingo Molnar" <mingo@redhat•com>,
"Andi Kleen" <ak@linux•intel.com>,
"Adrian Hunter" <adrian.hunter@intel•com>,
"Jiri Olsa" <jolsa@kernel•org>, "Rabin Vincent" <rabin@rab•in>,
"Martin Liška" <mliska@suse•cz>
Subject: [PATCH] tools include: Fix strict-aliasing rules breakage "tools/include/linux/compiler.h:66: error: dereferencing pointer ‘res.41’ does break strict-aliasing rules"
Date: Tue, 13 Oct 2015 10:52:14 +0200 [thread overview]
Message-ID: <20151013085214.GB2705@krava.brq.redhat.com> (raw)
In-Reply-To: <CAHTgTXVQRdsXjsHfjY8hMXXLMWyZDMEuJKAd+eVhfEN5jUBY5Q@mail.gmail.com>
On Mon, Oct 12, 2015 at 05:48:17PM -0700, Vinson Lee wrote:
SNIP
> > __builtin_memcpy((void *)res, (const void *)p, size);
> > @@ -59,11 +64,14 @@ static __always_inline void __read_once_size(const volatile void *p, void *res,
> >
> > static __always_inline void __write_once_size(volatile void *p, void *res, int size)
> > {
> > + u64_alias_t *u64_p = (u64_alias_t*) p;
> > + u64_alias_t *u64_res = (u64_alias_t*) res;
> > +
> > switch (size) {
> > case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
> > case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
> > case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
> > - case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
> > + case 8: *u64_p = *u64_res; break;
> > default:
> > barrier();
> > __builtin_memcpy((void *)p, (const void *)res, size);
>
>
> This patch fixes my perf build error with GCC 4.4.
>
> Vinson
posting complete patch
thanks,
jirka
---
Vinson reported build breakage with gcc 4.4 due to strict-aliasing.
CC util/annotate.o
cc1: warnings being treated as errors
util/annotate.c: In function ‘disasm__purge’:
linux-next/tools/include/linux/compiler.h:66: error: dereferencing
pointer ‘res.41’ does break strict-aliasing rules
The reason is READ_ONCE/WRITE_ONCE code we took from kernel sources.
They intentionaly break aliasing rules. While this is ok for kernel
because it's built with -fno-strict-aliasing, it breaks perf which
is build with -Wstrict-aliasing=3.
Using extra __may_alias__ type to allow aliasing in this case.
Reported-by: Vinson Lee <vlee@twopensource•com>
Link: http://lkml.kernel.org/n/tip-ffqyehdzx3w41j6smao15t0a@git.kernel.org
Signed-off-by: Jiri Olsa <jolsa@kernel•org>
---
tools/include/linux/compiler.h | 32 ++++++++++++++++++++++++--------
1 file changed, 24 insertions(+), 8 deletions(-)
diff --git a/tools/include/linux/compiler.h b/tools/include/linux/compiler.h
index 9098083869c8..fa7208a32d76 100644
--- a/tools/include/linux/compiler.h
+++ b/tools/include/linux/compiler.h
@@ -43,13 +43,29 @@
#include <linux/types.h>
+/*
+ * Following functions are taken from kernel sources and
+ * break aliasing rules in their original form.
+ *
+ * While kernel is compiled with -fno-strict-aliasing,
+ * perf uses -Wstrict-aliasing=3 which makes build fail
+ * under gcc 4.4.
+ *
+ * Using extra __may_alias__ type to allow aliasing
+ * in this case.
+ */
+typedef __u8 __attribute__((__may_alias__)) __u8_alias_t;
+typedef __u16 __attribute__((__may_alias__)) __u16_alias_t;
+typedef __u32 __attribute__((__may_alias__)) __u32_alias_t;
+typedef __u64 __attribute__((__may_alias__)) __u64_alias_t;
+
static __always_inline void __read_once_size(const volatile void *p, void *res, int size)
{
switch (size) {
- case 1: *(__u8 *)res = *(volatile __u8 *)p; break;
- case 2: *(__u16 *)res = *(volatile __u16 *)p; break;
- case 4: *(__u32 *)res = *(volatile __u32 *)p; break;
- case 8: *(__u64 *)res = *(volatile __u64 *)p; break;
+ case 1: *(__u8_alias_t *) res = *(volatile __u8_alias_t *) p; break;
+ case 2: *(__u16_alias_t *) res = *(volatile __u16_alias_t *) p; break;
+ case 4: *(__u32_alias_t *) res = *(volatile __u32_alias_t *) p; break;
+ case 8: *(__u64_alias_t *) res = *(volatile __u64_alias_t *) p; break;
default:
barrier();
__builtin_memcpy((void *)res, (const void *)p, size);
@@ -60,10 +76,10 @@ static __always_inline void __read_once_size(const volatile void *p, void *res,
static __always_inline void __write_once_size(volatile void *p, void *res, int size)
{
switch (size) {
- case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
- case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
- case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
- case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
+ case 1: *(volatile __u8_alias_t *) p = *(__u8_alias_t *) res; break;
+ case 2: *(volatile __u16_alias_t *) p = *(__u16_alias_t *) res; break;
+ case 4: *(volatile __u32_alias_t *) p = *(__u32_alias_t *) res; break;
+ case 8: *(volatile __u64_alias_t *) p = *(__u64_alias_t *) res; break;
default:
barrier();
__builtin_memcpy((void *)p, (const void *)res, size);
--
2.4.3
next prev parent reply other threads:[~2015-10-13 8:52 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-07 18:28 next-20151007 perf build error "tools/include/linux/compiler.h:66: error: dereferencing pointer ‘res.41’ does break strict-aliasing rules" Vinson Lee
2015-10-07 18:47 ` Arnaldo Carvalho de Melo
2015-10-08 9:12 ` Jiri Olsa
2015-10-13 0:48 ` Vinson Lee
2015-10-13 8:52 ` Jiri Olsa [this message]
2015-10-13 14:44 ` [PATCH] tools include: Fix strict-aliasing rules breakage " Arnaldo Carvalho de Melo
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=20151013085214.GB2705@krava.brq.redhat.com \
--to=jolsa@redhat$(echo .)com \
--cc=a.p.zijlstra@chello$(echo .)nl \
--cc=acme@kernel$(echo .)org \
--cc=adrian.hunter@intel$(echo .)com \
--cc=ak@linux$(echo .)intel.com \
--cc=jolsa@kernel$(echo .)org \
--cc=linux-next@vger$(echo .)kernel.org \
--cc=mingo@redhat$(echo .)com \
--cc=mliska@suse$(echo .)cz \
--cc=rabin@rab$(echo .)in \
--cc=vlee@twopensource$(echo .)com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
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