[PATCH 6/8] LoongArch: Use model("extreme") attribute for per-CPU variables in module if CONFIG_CC_HAS_EXPLICIT_RELOCS

From: Xi Ruoyao
Date: Sat Aug 27 2022 - 13:56:26 EST


On LoongArch, The "address" of a per-CPU variable symbol is actually an
offset from $r21. The value is nearing the loading address of main
kernel image, but far from the address of modules. We need to tell the
compiler that a PC-relative addressing with 32-bit offset is not
sufficient for local per-CPU variables.

After some discussion with GCC maintainers, we implemented this
attribute to indicate that a PC-relative addressing with 64-bit offset
should be used.

This attribute should be available in GCC 13 release. Some early GCC 13
snapshots may support -mexplicit-relocs but lack this attribute, we
simply reject them.

Link: https://gcc.gnu.org/r13-2199
Signed-off-by: Xi Ruoyao <xry111@xxxxxxxxxxx>
---
arch/loongarch/include/asm/percpu.h | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/arch/loongarch/include/asm/percpu.h b/arch/loongarch/include/asm/percpu.h
index 0bd6b0110198..91d4026b074e 100644
--- a/arch/loongarch/include/asm/percpu.h
+++ b/arch/loongarch/include/asm/percpu.h
@@ -8,6 +8,19 @@
#include <asm/cmpxchg.h>
#include <asm/loongarch.h>

+#if defined(MODULE) && defined(CONFIG_CC_HAS_EXPLICIT_RELOCS)
+# if __has_attribute(model)
+/* The "address" (in fact, offset from $r21) of a per-CPU variable is close
+ * to the load address of main kernel image, but far from where the modules are
+ * loaded. Tell the compiler this fact.
+ */
+# define PER_CPU_ATTRIBUTES __attribute__((model("extreme")))
+# else /* __has_attribute(model) */
+/* This should only happen with early GCC 13 snapshots. */
+# error "Compiler with explicit relocs but no model attribute is not supported"
+# endif /* __has_attribute(model) */
+#endif
+
/* Use r21 for fast access */
register unsigned long __my_cpu_offset __asm__("$r21");

--
2.37.0