[PATCH v2] compiler-gcc.h: Disable __retain on gcc-11

Geert Uytterhoeven
2024-10-02T07:25:00Z

All my gcc-11 compilers (Ubuntu 11.4.0-1ubuntu1~22.04) claim to support
the __retain__ attribute, but only riscv64-linux-gnu-gcc-11 and
x86_64-linux-gnu-gcc-11 (not x86_64-linux-gnux32-gcc-11!) actually do.
The arm-linux-gnueabi-gcc-11.5.0 compiler from kernel.org crosstool
fails in the same way:

error: ‘retain’ attribute ignored [-Werror=attributes]

All my gcc-12 compilers seem to support the attribute.

Play it safe, and disable __retain unconditionally on gcc-11.

Fixes: 0a5d3258d7c97295 ("compiler_types.h: Define __retain for __attribute__((__retain__))")
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
Reviewed-by: Tony Ambardar <tony.ambardar@gmail.com>
---
Tested with the following gcc-11 compilers:

1. All compilers available on Ubuntu 22.04LTS:

aarch64-linux-gnu-gcc-11
alpha-linux-gnu-gcc-11
arm-linux-gnueabi-gcc-11
arm-linux-gnueabihf-gcc-11
hppa64-linux-gnu-gcc-11
hppa-linux-gnu-gcc-11
i686-linux-gnu-gcc-11
m68k-linux-gnu-gcc-11
powerpc64le-linux-gnu-gcc-11
powerpc64-linux-gnu-gcc-11
powerpc-linux-gnu-gcc-11
riscv64-linux-gnu-gcc-11
s390x-linux-gnu-gcc-11
sh4-linux-gnu-gcc-11
sparc64-linux-gnu-gcc-11
x86_64-linux-gnu-gcc-11
x86_64-linux-gnux32-gcc-11

2. A few from kernel.org crosstool:

ia64-linux-gcc-11.1.0
sh2eb-linux-muslfdpic-gcc-11.2.0
arm-linux-gnueabi-gcc-11.5.0

3. A compiler from the J-Core folks:

sh2eb-linux-muslfdpic-gcc-11.2.0

v2:
- Update comment in <linux/compiler_types.h>,
- Add Reviewed-by.
---
include/linux/compiler-gcc.h | 9 +++++++++
include/linux/compiler_types.h | 2 +-
2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h
index f805adaa316e9a21..bedeb76b63a34745 100644
--- a/include/linux/compiler-gcc.h
+++ b/include/linux/compiler-gcc.h
@@ -145,3 +145,12 @@
#if GCC_VERSION < 90100
#undef __alloc_size__
#endif
+
+/*
+ * Most 11.x compilers claim to support it, but only riscv64-linux-gnu-gcc and
+ * x86_64-linux-gnu-gcc actually do.
+ */
+#if GCC_VERSION < 120000
+#undef __retain
+#define __retain
+#endif
diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index 1a957ea2f4fe78ed..76ddf04a7e66f890 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -153,7 +153,7 @@ static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
* being optimized out, but operates at the compiler/IR-level and may still
* allow unintended removal of objects during linking.
*
- * Optional: only supported since gcc >= 11, clang >= 13
+ * Optional: only supported since gcc >= 11 (partial), clang >= 13
*
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-retain-function-attribute
* clang: https://clang.llvm.org/docs/AttributeReference.html#retain
--
2.34.1