solve static percpu symbol issue in module and refine code model of module
cover solve static percpu symbol issue in module and refine code model of module
The compiler uses the PIC-relative method to access static variables instead of GOT when the code model is PIC. Therefore, the limitation of the access range from the instruction to the symbol address is +-2GB. Under this circumstance, the kernel cannot load a kernel module if this module has static per-CPU symbols declared by DEFINE_PER_CPU(). The reason is that kernel relocates the .data..percpu section of the kernel module to the end of kernel's .data..percpu. Hence, the distance between the per-CPU symbols and the instruction will exceed the 2GB limits. To solve this problem, the kernel should place the loaded module in the memory area [&_end-2G, VMALLOC_END].
Because the loaded module locates in the region [&_end-2G,VMALLOC_END] at runtime, the distance from the module start to the end of the kernel image does not exceed 2GB. Hence, the second patch changes the code model of the kernel module from PIC to medany to improve the performance of data access.
Changes from v1->v2
1. Unify the definition of VMALLOC_MODULE_START
2. Modify the indent
Vincent Chen (2):
riscv: avoid the PIC offset of static percpu data in module beyond 2G
limits
riscv: Replace PIC with medany to improve data accessing in module
arch/riscv/Makefile | 6 ++++-- arch/riscv/kernel/module.c | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-)
Signature · 1 line
2.7.4
PATCH v2 1/2 riscv: avoid the PIC offset of static percpu data in module beyond 2G limits
The compiler uses the PIC-relative method to access static variables instead of GOT when the code model is PIC. Therefore, the limitation of the access range from the instruction to the symbol address is +-2GB. Under this circumstance, the kernel cannot load a kernel module if this module has static per-CPU symbols declared by DEFINE_PER_CPU(). The reason is that kernel relocates the .data..percpu section of the kernel module to the end of kernel's .data..percpu. Hence, the distance between the per-CPU symbols and the instruction will exceed the 2GB limits. To solve this problem, the kernel should place the loaded module in the memory area [&_end-2G, VMALLOC_END].
--- arch/riscv/kernel/module.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
arch/riscv/kernel/module.c +16 -0
@@ -8,6 +8,10 @@#include <linux/err.h> #include <linux/errno.h> #include <linux/moduleloader.h> +#include <linux/vmalloc.h> +#include <linux/sizes.h> +#include <asm/pgtable.h> +#include <asm/sections.h> static int apply_r_riscv_32_rela (struct module *me,u32 *location,Elf_Addr v) {
@@ -386,3 +390,15 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,return 0 ; } + +#if defined(CONFIG_MMU ) && defined(CONFIG_64BIT ) +#define VMALLOC_MODULE_START \ + max(PFN_ALIGN((unsigned long)&_end - SZ_2G), VMALLOC_START) +void *module_alloc (unsigned long size) +{ +return __vmalloc_node_range (size,1 ,VMALLOC_MODULE_START , +VMALLOC_END ,GFP_KERNEL , +PAGE_KERNEL_EXEC ,0 ,NUMA_NO_NODE , +__builtin_return_address (0 )); +} +#endif
Signature · 1 line
2.7.4
PATCH v2 2/2 riscv: Change code model of module to medany to improve data accessing
In reply to Vincent ChenAll the loaded module locates in the region [&_end-2G,VMALLOC_END] at runtime, so the distance from the module start to the end of the kernel image does not exceed 2GB. Hence, the code model of the kernel module can be changed to medany to improve the performance data access.
--- arch/riscv/Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
arch/riscv/Makefile +4 -2
@@ -13,8 +13,10 @@ LDFLAGS_vmlinux := ifeq ($(CONFIG_DYNAMIC_FTRACE),y) LDFLAGS_vmlinux := --no-relax endif -KBUILD_AFLAGS_MODULE += -fPIC -KBUILD_CFLAGS_MODULE += -fPIC + +ifeq ($(CONFIG_64BIT)$(CONFIG_CMODEL_MEDLOW),yy) +KBUILD_CFLAGS_MODULE += -mcmodel=medany +endif export BITS ifeq ($(CONFIG_ARCH_RV64I),y)
Signature · 1 line
2.7.4
Palmer Dabbelt
maintainerQuoted 21 lines · On Thu, 20 Feb 2020 18:47:53 PST (-0800), [email protected] wrote:
The compiler uses the PIC-relative method to access static variables instead of GOT when the code model is PIC. Therefore, the limitation of the access range from the instruction to the symbol address is +-2GB. Under this circumstance, the kernel cannot load a kernel module if this module has static per-CPU symbols declared by
DEFINE_PER_CPU(). The reason is that kernel relocates the .data..percpu section of the kernel module to the end of kernel's .data..percpu. Hence, the distance between the per-CPU symbols and the instruction will exceed the 2GB limits. To solve this problem, the kernel should place the loaded module in the memory area [&_end-2G,VMALLOC_END].Because the loaded module locates in the region [&_end-2G,VMALLOC_END] at runtime, the distance from the module start to the end of the kernel image does not exceed 2GB. Hence, the second patch changes the code model of the kernel module from PIC to medany to improve the performance of data access.
Changes from
v1->v2
1. Unify the definition ofVMALLOC_MODULE_START
2. Modify the indent
Quoted 8 lines
Vincent Chen (2):
riscv: avoid the PIC offset of static percpu data in module beyond 2G
limits
riscv: Replace PIC with medany to improve data accessing in modulearch/riscv/Makefile | 6 ++++-- arch/riscv/kernel/module.c | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-)
Looking at this again, I think this is actually a good candidate for fixes. Unless there's any opposition I'll target it for rc5. It's on fixes.
Thanks!