Skip to contentSkip to conversation

perf build: Revert "enable -fno-strict-aliasing"

PATCH Superseded Ian Rogers 3 participants 9 messages Sep 4, 2025 – Mar 21 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf build: Revert "enable -fno-strict-aliasing"

This reverts commit 55a18d2f3ff7 ("perf build: enable -fno-strict-aliasing"). With (get|put)_unaligned_* using memcpy -fno-strict-aliasing is no longer necessary as memcpys are assumed to possibly alias.

---
This patch needs:
https://lore.kernel.org/lkml/[email protected]/
which have been merged into the tip timers/vdso branch.

---
 tools/perf/Makefile.config | 4 ----
 1 file changed, 4 deletions(-)
tools/perf/Makefile.config +0 -4
index 5a5832ee7b53..306b8334b788 100644
@@ -19,10 +19,6 @@ detected_var = $(shell echo "$(1)=$($(1))" >> $(OUTPUT).config-detected)
 CFLAGS := $(EXTRA_CFLAGS) $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 HOSTCFLAGS := $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 
-# This is required because the kernel is built with this and some of the code
-# borrowed from kernel headers depends on it, e.g. put_unaligned_*().
-CFLAGS += -fno-strict-aliasing
-
 # Enabled Wthread-safety analysis for clang builds.
 ifeq ($(CC_NO_CLANG), 0)
   CFLAGS += -Wthread-safety
Signature · 1 line
2.51.0.355.g5224444f11-goog
maintainer
#1 lore raw
Quoted 4 lines · Ian Rogers

This reverts commit 55a18d2f3ff7 ("perf build: enable

(quoted diff) +0 -2
-fno-strict-aliasing"). With (get|put)_unaligned_* using memcpy
-fno-strict-aliasing is no longer necessary as memcpys are assumed to

possibly alias.

I don't think this is a good idea. Much of tools/ includes kernel headers and various kernel code, all of which is written in the understanding that this (often called broken) C language feature does not exist.

As such, I would strongly suggest all of tools is built with -fno-strict-aliasing.

Quoted 27 lines

---
This patch needs:
https://lore.kernel.org/lkml/[email protected]/
which have been merged into the tip timers/vdso branch.

---
 tools/perf/Makefile.config | 4 ----
 1 file changed, 4 deletions(-)
tools/perf/Makefile.config +0 -4
index 5a5832ee7b53..306b8334b788 100644
@@ -19,10 +19,6 @@ detected_var = $(shell echo "$(1)=$($(1))" >> $(OUTPUT).config-detected)
 CFLAGS := $(EXTRA_CFLAGS) $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 HOSTCFLAGS := $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 
-# This is required because the kernel is built with this and some of the code
-# borrowed from kernel headers depends on it, e.g. put_unaligned_*().
-CFLAGS += -fno-strict-aliasing
-
 # Enabled Wthread-safety analysis for clang builds.
 ifeq ($(CC_NO_CLANG), 0)
   CFLAGS += -Wthread-safety
Signature · 1 line
2.51.0.355.g5224444f11-goog
maintainer
#2 lore raw
Quoted 11 lines · Peter Zijlstra
Quoted 4 lines · Ian Rogers

This reverts commit 55a18d2f3ff7 ("perf build: enable

(quoted diff) +0 -2
-fno-strict-aliasing"). With (get|put)_unaligned_* using memcpy
-fno-strict-aliasing is no longer necessary as memcpys are assumed to

possibly alias.

I don't think this is a good idea. Much of tools/ includes kernel headers and various kernel code, all of which is written in the understanding that this (often called broken) C language feature does not exist.

Quoted 2 lines

As such, I would strongly suggest all of tools is built with -fno-strict-aliasing.

Similarly I would strongly suggest having -fwrapv on all code that includes kernel headers.

Quoted 27 lines
Quoted 27 lines

---
This patch needs:
https://lore.kernel.org/lkml/[email protected]/
which have been merged into the tip timers/vdso branch.

---
 tools/perf/Makefile.config | 4 ----
 1 file changed, 4 deletions(-)
tools/perf/Makefile.config +0 -4
index 5a5832ee7b53..306b8334b788 100644
@@ -19,10 +19,6 @@ detected_var = $(shell echo "$(1)=$($(1))" >> $(OUTPUT).config-detected)
 CFLAGS := $(EXTRA_CFLAGS) $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 HOSTCFLAGS := $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 
-# This is required because the kernel is built with this and some of the code
-# borrowed from kernel headers depends on it, e.g. put_unaligned_*().
-CFLAGS += -fno-strict-aliasing
-
 # Enabled Wthread-safety analysis for clang builds.
 ifeq ($(CC_NO_CLANG), 0)
   CFLAGS += -Wthread-safety
Signature · 1 line
2.51.0.355.g5224444f11-goog
author reviewer
#3 lore raw
Quoted 13 lines · Peter Zijlstra
Quoted 11 lines · Peter Zijlstra
Quoted 4 lines · Ian Rogers

This reverts commit 55a18d2f3ff7 ("perf build: enable

(quoted diff) +0 -2
-fno-strict-aliasing"). With (get|put)_unaligned_* using memcpy
-fno-strict-aliasing is no longer necessary as memcpys are assumed to

possibly alias.

I don't think this is a good idea. Much of tools/ includes kernel headers and various kernel code, all of which is written in the understanding that this (often called broken) C language feature does not exist.

Quoted 5 lines
Quoted 2 lines

As such, I would strongly suggest all of tools is built with -fno-strict-aliasing.

Similarly I would strongly suggest having -fwrapv on all code that includes kernel headers.

Given we can build and run with sanitizers, ubsan covers fwrapv and type sanitizer is in development to detect strict aliasing violations. So we can have correct code without hamstringing the compiler.

There's lots wrong with C, a particular favorite of mine is:

```
void foo(const int *p) {
   int x = *p;
   if (x == 10) {
      mutex_lock(&global_mutex);
      if (x == 10) {
          // Code should always run ...
```

The compiler can save registers by re-loading from p in the code above, meaning the second "if" may not always run.

Anyway, the intent with adding -fno-strict-aliasing was that it was temporary as part of removing the libcrypto dependency and when the unaligned code landed we'd do this revert.

Wrt the kernel headers, I think we may be better breaking away from them. I can see sense in using stdint types rather than __u32 or u32, using stdatomic.h rather than linux/atomic.h, etc. This wasn't an option 15 years ago, but things have progressed a little since then.

Thanks,
Ian

Quoted 27 lines
Quoted 27 lines
Quoted 27 lines

---
This patch needs:
https://lore.kernel.org/lkml/[email protected]/
which have been merged into the tip timers/vdso branch.

---
 tools/perf/Makefile.config | 4 ----
 1 file changed, 4 deletions(-)
tools/perf/Makefile.config +0 -5
index 5a5832ee7b53..306b8334b788 100644
@@ -19,10 +19,6 @@ detected_var = $(shell echo "$(1)=$($(1))" >> $(OUTPUT).config-detected)
 CFLAGS := $(EXTRA_CFLAGS) $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 HOSTCFLAGS := $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 
-# This is required because the kernel is built with this and some of the code
-# borrowed from kernel headers depends on it, e.g. put_unaligned_*().
-CFLAGS += -fno-strict-aliasing
-
 # Enabled Wthread-safety analysis for clang builds.
 ifeq ($(CC_NO_CLANG), 0)
   CFLAGS += -Wthread-safety
--

2.51.0.355.g5224444f11-goog

author reviewer
#4 lore raw
Quoted 3 lines · Ian Rogers

Anyway, the intent with adding -fno-strict-aliasing was that it was temporary as part of removing the libcrypto dependency and when the unaligned code landed we'd do this revert.

Ping.

Thanks,
Ian

maintainer
#5 lore raw
Quoted 4 lines · Ian Rogers
Quoted 3 lines · Ian Rogers

Anyway, the intent with adding -fno-strict-aliasing was that it was temporary as part of removing the libcrypto dependency and when the unaligned code landed we'd do this revert.

I don't remember what's going on here but as it's not there originally, I think we can revert to the change for now.

But it doesn't apply, care to rebase?

Thanks,
Namhyung

author reviewer
#6 lore raw

PATCH v2 1/1 perf build: Revert "enable -fno-strict-aliasing"

This reverts commit 55a18d2f3ff7 ("perf build: enable -fno-strict-aliasing"). With (get|put)_unaligned_* using memcpy -fno-strict-aliasing is no longer necessary as memcpys are assumed to possibly alias.

---
 tools/perf/Makefile.config | 4 ----
 1 file changed, 4 deletions(-)
tools/perf/Makefile.config +0 -4
index a8dc72cfe48e..1d3dcefd8c95 100644
@@ -19,10 +19,6 @@ detected_var = $(shell echo "$(1)=$($(1))" >> $(OUTPUT).config-detected)
 CFLAGS := $(EXTRA_CFLAGS) $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 HOSTCFLAGS := $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 
-# This is required because the kernel is built with this and some of the code
-# borrowed from kernel headers depends on it, e.g. put_unaligned_*().
-CFLAGS += -fno-strict-aliasing
-
 # Set target flag and options when using clang as compiler.
 ifeq ($(CC_NO_CLANG), 0)
   CLANG_TARGET_FLAGS_arm	:= arm-linux-gnueabi
Signature · 1 line
2.53.0.959.g497ff81fa9-goog
author reviewer
#7 lore raw
Quoted 5 lines · Ian Rogers

This reverts commit 55a18d2f3ff7 ("perf build: enable

(quoted diff) +0 -2
-fno-strict-aliasing"). With (get|put)_unaligned_* using memcpy
-fno-strict-aliasing is no longer necessary as memcpys are assumed to

possibly alias.

Sashiko noted strict aliasing violations in hashmap, so I sent out a fix [1]:

The commit message notes that put_unaligned_*() no longer requires this flag, but does this change safely account for the hashmap API's type punning?

The hashmap API heavily uses type punning via hashmap_cast_ptr(), casting pointer-to-pointer arguments (e.g., struct syscall_stats **stats or struct hashmap_entry **entry) into long * for functions like hashmap_find().

Inside hashmap_find(), the pointer is written via *value = entry->value; where value is of type long *. Under C strict aliasing rules, a long and a pointer type are incompatible. When -fstrict-aliasing is active (the default at -O2 or -O3), the compiler is allowed to assume that the write through long * does not modify the caller's pointer variable.

If perf is compiled with Link-Time Optimization (LTO), the compiler can analyze the cross-module call and conclude that hashmap_find() cannot modify the caller's pointer variable.

Could this cause the compiler to optimize out the assignment, leading to silent NULL pointer dereferences or the use of uninitialized memory?

[1] https://lore.kernel.org/lkml/[email protected]/T/#t

Thanks,
Ian

Quoted 23 lines
---
 tools/perf/Makefile.config | 4 ----
 1 file changed, 4 deletions(-)
tools/perf/Makefile.config +0 -5
index a8dc72cfe48e..1d3dcefd8c95 100644
@@ -19,10 +19,6 @@ detected_var = $(shell echo "$(1)=$($(1))" >> $(OUTPUT).config-detected)
 CFLAGS := $(EXTRA_CFLAGS) $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 HOSTCFLAGS := $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 
-# This is required because the kernel is built with this and some of the code
-# borrowed from kernel headers depends on it, e.g. put_unaligned_*().
-CFLAGS += -fno-strict-aliasing
-
 # Set target flag and options when using clang as compiler.
 ifeq ($(CC_NO_CLANG), 0)
   CLANG_TARGET_FLAGS_arm       := arm-linux-gnueabi
--

2.53.0.959.g497ff81fa9-goog

maintainer
#8 lore raw
In reply to Ian Rogers
Quoted 33 lines · Ian Rogers
Quoted 13 lines · Peter Zijlstra
Quoted 11 lines · Peter Zijlstra
Quoted 4 lines · Ian Rogers

This reverts commit 55a18d2f3ff7 ("perf build: enable

(quoted diff) +0 -2
-fno-strict-aliasing"). With (get|put)_unaligned_* using memcpy
-fno-strict-aliasing is no longer necessary as memcpys are assumed to

possibly alias.

I don't think this is a good idea. Much of tools/ includes kernel headers and various kernel code, all of which is written in the understanding that this (often called broken) C language feature does not exist.

Quoted 5 lines
Quoted 2 lines

As such, I would strongly suggest all of tools is built with -fno-strict-aliasing.

Similarly I would strongly suggest having -fwrapv on all code that includes kernel headers.

Given we can build and run with sanitizers, ubsan covers fwrapv and type sanitizer is in development to detect strict aliasing violations. So we can have correct code without hamstringing the compiler.

There's lots wrong with C, a particular favorite of mine is:

```
void foo(const int *p) {
   int x = *p;
   if (x == 10) {
      mutex_lock(&global_mutex);
      if (x == 10) {
          // Code should always run ...
```
Quoted 2 lines

The compiler can save registers by re-loading from p in the code above, meaning the second "if" may not always run.

mutex_lock() should imply barrier() which should very much inhibit this.