diff mbox series

[v2,1/2] riscv: vdso: Prevent gcc from inserting calls to memset()

Message ID 20240705060902.113294-2-alexghiti@rivosinc.com (mailing list archive)
State New
Headers show
Series Prevent dynamic relocations in vDSO | expand

Checks

Context Check Description
conchuod/vmtest-for-next-PR fail PR summary
conchuod/patch-1-test-1 fail .github/scripts/patches/tests/build_rv32_defconfig.sh
conchuod/patch-1-test-2 fail .github/scripts/patches/tests/build_rv64_clang_allmodconfig.sh
conchuod/patch-1-test-3 success .github/scripts/patches/tests/build_rv64_gcc_allmodconfig.sh
conchuod/patch-1-test-4 success .github/scripts/patches/tests/build_rv64_nommu_k210_defconfig.sh
conchuod/patch-1-test-5 success .github/scripts/patches/tests/build_rv64_nommu_virt_defconfig.sh
conchuod/patch-1-test-6 success .github/scripts/patches/tests/checkpatch.sh
conchuod/patch-1-test-7 success .github/scripts/patches/tests/dtb_warn_rv64.sh
conchuod/patch-1-test-8 success .github/scripts/patches/tests/header_inline.sh
conchuod/patch-1-test-9 success .github/scripts/patches/tests/kdoc.sh
conchuod/patch-1-test-10 success .github/scripts/patches/tests/module_param.sh
conchuod/patch-1-test-11 success .github/scripts/patches/tests/verify_fixes.sh
conchuod/patch-1-test-12 success .github/scripts/patches/tests/verify_signedoff.sh

Commit Message

Alexandre Ghiti July 5, 2024, 6:09 a.m. UTC
gcc is smart enough to insert a call to memset() in
riscv_vdso_get_cpus(), which generates a dynamic relocation.

So prevent gcc from doing that by using the
-fno-tree-loop-distribute-patterns option.

Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
---
 arch/riscv/kernel/vdso/Makefile | 1 +
 1 file changed, 1 insertion(+)

Comments

Guo Ren July 10, 2024, 1:30 a.m. UTC | #1
On Fri, Jul 5, 2024 at 2:10 PM Alexandre Ghiti <alexghiti@rivosinc.com> wrote:
>
> gcc is smart enough to insert a call to memset() in
> riscv_vdso_get_cpus(), which generates a dynamic relocation.
>
> So prevent gcc from doing that by using the
> -fno-tree-loop-distribute-patterns option.
Good catch. I think we could put a comment on
"-fno-tree-loop-distribute-patterns". eg: Perform loop distribution of
patterns that can be code generated with calls to a library.

Others, LGTM!

Reviewed-by: Guo Ren <guoren@kernel.org>

>
> Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
> ---
>  arch/riscv/kernel/vdso/Makefile | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/arch/riscv/kernel/vdso/Makefile b/arch/riscv/kernel/vdso/Makefile
> index f7ef8ad9b550..c7e40bf36371 100644
> --- a/arch/riscv/kernel/vdso/Makefile
> +++ b/arch/riscv/kernel/vdso/Makefile
> @@ -18,6 +18,7 @@ obj-vdso = $(patsubst %, %.o, $(vdso-syms)) note.o
>
>  ccflags-y := -fno-stack-protector
>  ccflags-y += -DDISABLE_BRANCH_PROFILING
> +ccflags-y += -fno-tree-loop-distribute-patterns
>
>  ifneq ($(c-gettimeofday-y),)
>    CFLAGS_vgettimeofday.o += -fPIC -include $(c-gettimeofday-y)
> --
> 2.39.2
>
Palmer Dabbelt Sept. 17, 2024, 3:41 p.m. UTC | #2
On Thu, 04 Jul 2024 23:09:01 PDT (-0700), alexghiti@rivosinc.com wrote:
> gcc is smart enough to insert a call to memset() in
> riscv_vdso_get_cpus(), which generates a dynamic relocation.
>
> So prevent gcc from doing that by using the
> -fno-tree-loop-distribute-patterns option.

This option doesn't exist on LLVM (at least on whatever version I'm 
using), and I'm not getting any memset()s generated locally so I'm not 
sure what to look for over there.

From poking around GCC it looks like we might want -fno-tree-ccp too?  
That seems to be able to convert assignments into builtins as well...

> Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
> ---
>  arch/riscv/kernel/vdso/Makefile | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/arch/riscv/kernel/vdso/Makefile b/arch/riscv/kernel/vdso/Makefile
> index f7ef8ad9b550..c7e40bf36371 100644
> --- a/arch/riscv/kernel/vdso/Makefile
> +++ b/arch/riscv/kernel/vdso/Makefile
> @@ -18,6 +18,7 @@ obj-vdso = $(patsubst %, %.o, $(vdso-syms)) note.o
>
>  ccflags-y := -fno-stack-protector
>  ccflags-y += -DDISABLE_BRANCH_PROFILING
> +ccflags-y += -fno-tree-loop-distribute-patterns
>
>  ifneq ($(c-gettimeofday-y),)
>    CFLAGS_vgettimeofday.o += -fPIC -include $(c-gettimeofday-y)
Alexandre Ghiti Oct. 14, 2024, 11:12 a.m. UTC | #3
Hi Palmer,

On 17/09/2024 17:41, Palmer Dabbelt wrote:
> On Thu, 04 Jul 2024 23:09:01 PDT (-0700), alexghiti@rivosinc.com wrote:
>> gcc is smart enough to insert a call to memset() in
>> riscv_vdso_get_cpus(), which generates a dynamic relocation.
>>
>> So prevent gcc from doing that by using the
>> -fno-tree-loop-distribute-patterns option.
>
> This option doesn't exist on LLVM (at least on whatever version I'm 
> using), and I'm not getting any memset()s generated locally so I'm not 
> sure what to look for over there.
>

When compiling a rv32_defconfig (still on 6.12), I get:

~/linux$ riscv64-unknown-elf-readelf -r 
build_defconfig_rv32/arch/riscv/kernel/vdso/vdso.so

Relocation section '.rela.dyn' at offset 0x5a0 contains 1 entry:
  Offset     Info    Type            Sym.Value  Sym. Name + Addend
00000314  00000205 R_RISCV_JUMP_SLOT 00000000   memset + 0


> From poking around GCC it looks like we might want -fno-tree-ccp too? 
> That seems to be able to convert assignments into builtins as well...


I gave a try at -fno-tree-ccp, but it did not remove this relocation.

I now see this relocation in llvm too, so it would be nice to find a 
common option or another way to do it, any idea is welcome!

Thanks,

Alex


>
>> Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
>> ---
>>  arch/riscv/kernel/vdso/Makefile | 1 +
>>  1 file changed, 1 insertion(+)
>>
>> diff --git a/arch/riscv/kernel/vdso/Makefile 
>> b/arch/riscv/kernel/vdso/Makefile
>> index f7ef8ad9b550..c7e40bf36371 100644
>> --- a/arch/riscv/kernel/vdso/Makefile
>> +++ b/arch/riscv/kernel/vdso/Makefile
>> @@ -18,6 +18,7 @@ obj-vdso = $(patsubst %, %.o, $(vdso-syms)) note.o
>>
>>  ccflags-y := -fno-stack-protector
>>  ccflags-y += -DDISABLE_BRANCH_PROFILING
>> +ccflags-y += -fno-tree-loop-distribute-patterns
>>
>>  ifneq ($(c-gettimeofday-y),)
>>    CFLAGS_vgettimeofday.o += -fPIC -include $(c-gettimeofday-y)
>
> _______________________________________________
> linux-riscv mailing list
> linux-riscv@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-riscv
diff mbox series

Patch

diff --git a/arch/riscv/kernel/vdso/Makefile b/arch/riscv/kernel/vdso/Makefile
index f7ef8ad9b550..c7e40bf36371 100644
--- a/arch/riscv/kernel/vdso/Makefile
+++ b/arch/riscv/kernel/vdso/Makefile
@@ -18,6 +18,7 @@  obj-vdso = $(patsubst %, %.o, $(vdso-syms)) note.o
 
 ccflags-y := -fno-stack-protector
 ccflags-y += -DDISABLE_BRANCH_PROFILING
+ccflags-y += -fno-tree-loop-distribute-patterns
 
 ifneq ($(c-gettimeofday-y),)
   CFLAGS_vgettimeofday.o += -fPIC -include $(c-gettimeofday-y)