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.
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Link: https://gcc.gnu.org/r13-2199
---
arch/loongarch/Makefile | 3 +++
arch/loongarch/include/asm/percpu.h | 8 ++++++++
2 files changed, 11 insertions(+)
diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile
index 1563747c4fa8..593818a61741 100644
--- a/arch/loongarch/Makefile
+++ b/arch/loongarch/Makefile
@@ -53,6 +53,9 @@ LDFLAGS_vmlinux += -G0 -static -n -nostdlib
# combination of a "new" assembler and "old" compiler is not supported. Either
# upgrade the compiler or downgrade the assembler.
ifdef CONFIG_AS_HAS_EXPLICIT_RELOCS
+ifeq ($(shell echo '__has_attribute(model)' | $(CC) -E -P - 2> /dev/null), 0)
+$(error "C compiler must support model attribute if using explicit relocs")
+endif
cflags-y += -mexplicit-relocs
else
cflags-y += $(call cc-option,-mno-explicit-relocs)
diff --git a/arch/loongarch/include/asm/percpu.h b/arch/loongarch/include/asm/percpu.h
index 0bd6b0110198..dd7fcc553efa 100644
--- a/arch/loongarch/include/asm/percpu.h
+++ b/arch/loongarch/include/asm/percpu.h
@@ -8,6 +8,14 @@
#include <asm/cmpxchg.h>
#include <asm/loongarch.h>
+#if defined(MODULE) && defined(CONFIG_AS_HAS_EXPLICIT_RELOCS)
+/* 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")))
+#endif
+
/* Use r21 for fast access */
register unsigned long __my_cpu_offset __asm__("$r21");
--
2.37.0
On Mon, 2022-08-29 at 21:31 +0800, Xi Ruoyao wrote: > diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile > index 1563747c4fa8..593818a61741 100644 > --- a/arch/loongarch/Makefile > +++ b/arch/loongarch/Makefile > @@ -53,6 +53,9 @@ LDFLAGS_vmlinux += -G0 -static -n -nostdlib > # combination of a "new" assembler and "old" compiler is not supported. Either > # upgrade the compiler or downgrade the assembler. > ifdef CONFIG_AS_HAS_EXPLICIT_RELOCS > +ifeq ($(shell echo '__has_attribute(model)' | $(CC) -E -P - 2> /dev/null), 0) > +$(error "C compiler must support model attribute if using explicit relocs") > +endif Self review: I'm wondering if we really need this thing... There won't be a GCC version released with explicit relocation but without model attribute (GCC 13 starts to support them both). But without a check, if someone uses an early GCC 13 snapshot and ignores the -Wattributes warning, the system will suddenly blow up loading a module with per-CPU variable defined. Maybe "-Werror=attributes" is better, but is it OK to add a -Werror= option for entire Linux tree? > cflags-y += -mexplicit-relocs > else > cflags-y += $(call cc-option,-mno-explicit-relocs) -- Xi Ruoyao <xry111@xry111.site> School of Aerospace Science and Technology, Xidian University
Hi, Ruoyao, On Mon, Aug 29, 2022 at 11:02 PM Xi Ruoyao <xry111@xry111.site> wrote: > > On Mon, 2022-08-29 at 21:31 +0800, Xi Ruoyao wrote: > > diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile > > index 1563747c4fa8..593818a61741 100644 > > --- a/arch/loongarch/Makefile > > +++ b/arch/loongarch/Makefile > > @@ -53,6 +53,9 @@ LDFLAGS_vmlinux += -G0 -static -n -nostdlib > > # combination of a "new" assembler and "old" compiler is not supported. Either > > # upgrade the compiler or downgrade the assembler. > > ifdef CONFIG_AS_HAS_EXPLICIT_RELOCS > > +ifeq ($(shell echo '__has_attribute(model)' | $(CC) -E -P - 2> /dev/null), 0) > > +$(error "C compiler must support model attribute if using explicit relocs") > > +endif > > Self review: > > I'm wondering if we really need this thing... There won't be a GCC > version released with explicit relocation but without model attribute > (GCC 13 starts to support them both). > > But without a check, if someone uses an early GCC 13 snapshot and > ignores the -Wattributes warning, the system will suddenly blow up > loading a module with per-CPU variable defined. > > Maybe "-Werror=attributes" is better, but is it OK to add a -Werror= > option for entire Linux tree? I think we can remove it entirely, and then this patch seems can be squashed into patch 2. Huacai > > > cflags-y += -mexplicit-relocs > > else > > cflags-y += $(call cc-option,-mno-explicit-relocs) > > -- > Xi Ruoyao <xry111@xry111.site> > School of Aerospace Science and Technology, Xidian University >
© 2016 - 2026 Red Hat, Inc.