Skip to contentSkip to conversation

perf tools: Fix strict alias issue for find_first_bit

RFC In review Jiri Olsa 3 participants 4 messages Feb 26, 2014 – Mar 2, 2014 perf events

lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

RFC 1/1 perf tools: Fix strict alias issue for find_first_bit

hi,
got issue below when compiling perf tool on i686 with gcc 4.4, but not sure the patch is correct workaround here.

thanks for comments,
jirka

---
When compiling perf tool code with gcc 4.4.7 I'm getting following error:

CC util/session.o
cc1: warnings being treated as errors
util/session.c: In function ‘perf_session_deliver_event’:
/root/linux/tools/perf/util/include/linux/bitops.h:109: error: dereferencing pointer ‘p’ does break strict-aliasing rules /root/linux/tools/perf/util/include/linux/bitops.h:101: error: dereferencing pointer ‘p’ does break strict-aliasing rules util/session.c:697: note: initialized from here /root/linux/tools/perf/util/include/linux/bitops.h:101: note: initialized from here
make[1]: *** [util/session.o] Error 1
make: *** [util/session.o] Error 2

The aliased types here are u64 and unsigned long pointers, which is safe for the find_first_bit processing.

This error shows up for me only for gcc 4.4 on 32bit x86, even for -Wstrict-aliasing=3, while newer gcc are quiet and scream here for -Wstrict-aliasing={2,1}. Looks like newer gcc changed the rules for strict alias warnings.

The gcc documentation offers workaround for valid
aliasing by using __may_alias__ attribute:
http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Type-Attributes.html

Using this workaround for the find_first_bit function.

Cc Corey Ashford <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc David Ahern <[email protected]>
---
 tools/perf/util/include/linux/bitops.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/util/include/linux/bitops.h +3 -1
index 45cf10a..dadfa7e 100644
@@ -87,13 +87,15 @@ static __always_inline unsigned long __ffs(unsigned long word)
 	return num;
 }
 
+typedef const unsigned long __attribute__((__may_alias__)) long_alias_t;
+
 /*
  * Find the first set bit in a memory region.
  */
 static inline unsigned long
 find_first_bit(const unsigned long *addr, unsigned long size)
 {
-	const unsigned long *p = addr;
+	long_alias_t *p = (long_alias_t *) addr;
 	unsigned long result = 0;
 	unsigned long tmp;
 
Signature · 1 line
1.8.3.1
maintainer
#1 lore raw
Quoted 25 lines · Jiri Olsa

hi,
got issue below when compiling perf tool on i686 with gcc 4.4, but not sure the patch is correct workaround here.

thanks for comments,
jirka

---
When compiling perf tool code with gcc 4.4.7 I'm getting following error:

CC util/session.o
cc1: warnings being treated as errors
util/session.c: In function ‘perf_session_deliver_event’:
/root/linux/tools/perf/util/include/linux/bitops.h:109: error: dereferencing pointer ‘p’ does break strict-aliasing rules /root/linux/tools/perf/util/include/linux/bitops.h:101: error: dereferencing pointer ‘p’ does break strict-aliasing rules util/session.c:697: note: initialized from here /root/linux/tools/perf/util/include/linux/bitops.h:101: note: initialized from here
make[1]: *** [util/session.o] Error 1
make: *** [util/session.o] Error 2

The aliased types here are u64 and unsigned long pointers, which is safe for the find_first_bit processing.

Quoted 9 lines

This error shows up for me only for gcc 4.4 on 32bit x86, even for -Wstrict-aliasing=3, while newer gcc are quiet and scream here for -Wstrict-aliasing={2,1}. Looks like newer gcc changed the rules for strict alias warnings.

The gcc documentation offers workaround for valid
aliasing by using __may_alias__ attribute:
http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Type-Attributes.html

The kernel builds with -fno-strict-aliasing because the C aliasing rules are a bunch of monkey poo. Perf tool should probably do the same.

author
#2 lore raw

PATCH 1/1 perf tools: Do not compile with -fstrict-aliasing

Quoted 31 lines · Peter Zijlstra
Quoted 30 lines · Jiri Olsa

hi,
got issue below when compiling perf tool on i686 with gcc 4.4, but not sure the patch is correct workaround here.

thanks for comments,
jirka

---
When compiling perf tool code with gcc 4.4.7 I'm getting following error:

CC util/session.o
cc1: warnings being treated as errors
util/session.c: In function ‘perf_session_deliver_event’:
/root/linux/tools/perf/util/include/linux/bitops.h:109: error: dereferencing pointer ‘p’ does break strict-aliasing rules /root/linux/tools/perf/util/include/linux/bitops.h:101: error: dereferencing pointer ‘p’ does break strict-aliasing rules util/session.c:697: note: initialized from here /root/linux/tools/perf/util/include/linux/bitops.h:101: note: initialized from here
make[1]: *** [util/session.o] Error 1
make: *** [util/session.o] Error 2

The aliased types here are u64 and unsigned long pointers, which is safe for the find_first_bit processing.

This error shows up for me only for gcc 4.4 on 32bit x86, even for -Wstrict-aliasing=3, while newer gcc are quiet and scream here for -Wstrict-aliasing={2,1}. Looks like newer gcc changed the rules for strict alias warnings.

Quoted 7 lines
Quoted 4 lines

The gcc documentation offers workaround for valid
aliasing by using __may_alias__ attribute:
http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Type-Attributes.html

The kernel builds with -fno-strict-aliasing because the C aliasing rules are a bunch of monkey poo. Perf tool should probably do the same.

my pleasure ;-)

thanks,
jirka

---
Switching off the strict aliasing for perf tool compilation, because it causes compilation issues/errors for older gcc, and to quote Peter:
"C aliasing rules are a bunch of monkey poo"

We don't enable strict aliasing actively, it is forced by -Ox option. Using -fno-strict-aliasing option at the same time we use -O.

Removing -Wstrict-aliasing=3 option as well.

Cc Corey Ashford <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc David Ahern <[email protected]>
---
 tools/perf/config/Makefile     | 2 +-
 tools/scripts/Makefile.include | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)
tools/perf/config/Makefile +1 -1
index f7c81d3..e7786c7 100644
@@ -94,7 +94,7 @@ ifndef DEBUG
 endif
 
 ifeq ($(DEBUG),0)
-  CFLAGS += -O6
+  CFLAGS += -O6 -fno-strict-aliasing
 endif
 
 ifdef PARSER_DEBUG
tools/scripts/Makefile.include +0 -1
index 8abbef1..3667b5b 100644
@@ -32,7 +32,6 @@ EXTRA_WARNINGS += -Wold-style-definition
 EXTRA_WARNINGS += -Wpacked
 EXTRA_WARNINGS += -Wredundant-decls
 EXTRA_WARNINGS += -Wshadow
-EXTRA_WARNINGS += -Wstrict-aliasing=3
 EXTRA_WARNINGS += -Wstrict-prototypes
 EXTRA_WARNINGS += -Wswitch-default
 EXTRA_WARNINGS += -Wswitch-enum
Signature · 1 line
1.8.3.1
#3 lore raw
In reply to Jiri Olsa
perf tools: Fix strict alias issue for find_first_bit
Commit-ID:  b39c2a57a00a841f057a75b41df4c26173288b66
Gitweb:     http://git.kernel.org/tip/b39c2a57a00a841f057a75b41df4c26173288b66
Author:     Jiri Olsa <[email protected]>
AuthorDate: Wed, 26 Feb 2014 18:14:26 +0100
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 28 Feb 2014 10:39:40 -0300

perf tools: Fix strict alias issue for find_first_bit

When compiling perf tool code with gcc 4.4.7 I'm getting following error:

CC util/session.o
cc1: warnings being treated as errors
util/session.c: In function ‘perf_session_deliver_event’:
tools/perf/util/include/linux/bitops.h:109: error: dereferencing pointer ‘p’ does break strict-aliasing rules tools/perf/util/include/linux/bitops.h:101: error: dereferencing pointer ‘p’ does break strict-aliasing rules util/session.c:697: note: initialized from here
tools/perf/util/include/linux/bitops.h:101: note: initialized from here
make[1]: *** [util/session.o] Error 1
make: *** [util/session.o] Error 2

The aliased types here are u64 and unsigned long pointers, which is safe for the find_first_bit processing.

This error shows up for me only for gcc 4.4 on 32bit x86, even for -Wstrict-aliasing=3, while newer gcc are quiet and scream here for -Wstrict-aliasing={2,1}. Looks like newer gcc changed the rules for strict alias warnings.

The gcc documentation offers workaround for valid aliasing by using __may_alias__ attribute:

http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Type-Attributes.html

Using this workaround for the find_first_bit function.

Cc Corey Ashford <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 tools/perf/util/include/linux/bitops.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/util/include/linux/bitops.h +3 -1
index 45cf10a..dadfa7e 100644
@@ -87,13 +87,15 @@ static __always_inline unsigned long __ffs(unsigned long word)
 	return num;
 }
 
+typedef const unsigned long __attribute__((__may_alias__)) long_alias_t;
+
 /*
  * Find the first set bit in a memory region.
  */
 static inline unsigned long
 find_first_bit(const unsigned long *addr, unsigned long size)
 {
-	const unsigned long *p = addr;
+	long_alias_t *p = (long_alias_t *) addr;
 	unsigned long result = 0;
 	unsigned long tmp;