diff mbox series

RISC-V: errata: Use medany for relocatable builds

Message ID 20250326224506.27165-2-palmer@rivosinc.com (mailing list archive)
State Accepted
Commit bb58e1579f431d42469b6aed0f03eff383ba6db5
Headers show
Series RISC-V: errata: Use medany for relocatable builds | expand

Checks

Context Check Description
bjorn/pre-ci_am success Success
bjorn/build-rv32-defconfig success build-rv32-defconfig
bjorn/build-rv64-clang-allmodconfig success build-rv64-clang-allmodconfig
bjorn/build-rv64-gcc-allmodconfig success build-rv64-gcc-allmodconfig
bjorn/build-rv64-nommu-k210-defconfig success build-rv64-nommu-k210-defconfig
bjorn/build-rv64-nommu-k210-virt success build-rv64-nommu-k210-virt
bjorn/checkpatch success checkpatch
bjorn/dtb-warn-rv64 success dtb-warn-rv64
bjorn/header-inline success header-inline
bjorn/kdoc success kdoc
bjorn/module-param success module-param
bjorn/verify-fixes success verify-fixes
bjorn/verify-signedoff success verify-signedoff

Commit Message

Palmer Dabbelt March 26, 2025, 10:45 p.m. UTC
We're trying to mix non-PIC/PIE objects into the otherwise-PIE
relocatable kernels, to avoid GOT/PLT references during early boot
alternative resolution (which happens before the GOT/PLT are set up).

riscv64-unknown-linux-gnu-ld: arch/riscv/errata/sifive/errata.o: relocation R_RISCV_HI20 against `tlb_flush_all_threshold' can not be used when making a shared object; recompile with -fPIC
riscv64-unknown-linux-gnu-ld: arch/riscv/errata/thead/errata.o: relocation R_RISCV_HI20 against `riscv_cbom_block_size' can not be used when making a shared object; recompile with -fPIC

Fixes: 8dc2a7e8027f ("riscv: Fix relocatable kernels with early alternatives using -fno-pie")
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
---
I'm not 100% sure it's possible to expose this without Samuel's NOMMU
relocatable builds, see
<https://lore.kernel.org/all/mhng-517c0318-c8fb-4c4a-aa0e-0d08f8d34d78@palmer-ri-x1c9/>
---
 arch/riscv/errata/Makefile | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Comments

Alexandre Ghiti March 28, 2025, 8:18 a.m. UTC | #1
On 26/03/2025 23:45, Palmer Dabbelt wrote:
> We're trying to mix non-PIC/PIE objects into the otherwise-PIE
> relocatable kernels, to avoid GOT/PLT references during early boot
> alternative resolution (which happens before the GOT/PLT are set up).
>
> riscv64-unknown-linux-gnu-ld: arch/riscv/errata/sifive/errata.o: relocation R_RISCV_HI20 against `tlb_flush_all_threshold' can not be used when making a shared object; recompile with -fPIC
> riscv64-unknown-linux-gnu-ld: arch/riscv/errata/thead/errata.o: relocation R_RISCV_HI20 against `riscv_cbom_block_size' can not be used when making a shared object; recompile with -fPIC


thead erratas are already compiled with medany if 
CONFIG_RISCV_ALTERNATIVE_EARLY is set 
(https://elixir.bootlin.com/linux/v6.14-rc6/source/arch/riscv/errata/thead/Makefile#L2) 
so why isn't it enough?


>
> Fixes: 8dc2a7e8027f ("riscv: Fix relocatable kernels with early alternatives using -fno-pie")
> Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
> ---
> I'm not 100% sure it's possible to expose this without Samuel's NOMMU
> relocatable builds, see
> <https://lore.kernel.org/all/mhng-517c0318-c8fb-4c4a-aa0e-0d08f8d34d78@palmer-ri-x1c9/>
> ---
>   arch/riscv/errata/Makefile | 6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/arch/riscv/errata/Makefile b/arch/riscv/errata/Makefile
> index f0da9d7b39c3..bc6c77ba837d 100644
> --- a/arch/riscv/errata/Makefile
> +++ b/arch/riscv/errata/Makefile
> @@ -1,5 +1,9 @@
>   ifdef CONFIG_RELOCATABLE
> -KBUILD_CFLAGS += -fno-pie
> +# We can't use PIC/PIE when handling early-boot errata parsing, as the kernel
> +# doesn't have a GOT setup at that point.  So instead just use medany: it's
> +# usually position-independent, so it should be good enough for the errata
> +# handling.
> +KBUILD_CFLAGS += -fno-pie -mcmodel=medany
>   endif
>   
>   ifdef CONFIG_RISCV_ALTERNATIVE_EARLY
Alexandre Ghiti March 28, 2025, 12:24 p.m. UTC | #2
On 28/03/2025 09:18, Alexandre Ghiti wrote:
> On 26/03/2025 23:45, Palmer Dabbelt wrote:
>> We're trying to mix non-PIC/PIE objects into the otherwise-PIE
>> relocatable kernels, to avoid GOT/PLT references during early boot
>> alternative resolution (which happens before the GOT/PLT are set up).
>>
>> riscv64-unknown-linux-gnu-ld: arch/riscv/errata/sifive/errata.o: 
>> relocation R_RISCV_HI20 against `tlb_flush_all_threshold' can not be 
>> used when making a shared object; recompile with -fPIC
>> riscv64-unknown-linux-gnu-ld: arch/riscv/errata/thead/errata.o: 
>> relocation R_RISCV_HI20 against `riscv_cbom_block_size' can not be 
>> used when making a shared object; recompile with -fPIC
>
>
> thead erratas are already compiled with medany if 
> CONFIG_RISCV_ALTERNATIVE_EARLY is set 
> (https://elixir.bootlin.com/linux/v6.14-rc6/source/arch/riscv/errata/thead/Makefile#L2) 
> so why isn't it enough?
>

FYI, this config without early boot alternatives trigger the same 
relocation issue with thead erratas: 
https://download.01.org/0day-ci/archive/20250328/202503281836.8pntHm6I-lkp@intel.com/config


>
>>
>> Fixes: 8dc2a7e8027f ("riscv: Fix relocatable kernels with early 
>> alternatives using -fno-pie")
>> Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
>> ---
>> I'm not 100% sure it's possible to expose this without Samuel's NOMMU
>> relocatable builds, see
>> <https://lore.kernel.org/all/mhng-517c0318-c8fb-4c4a-aa0e-0d08f8d34d78@palmer-ri-x1c9/> 
>>
>> ---
>>   arch/riscv/errata/Makefile | 6 +++++-
>>   1 file changed, 5 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/riscv/errata/Makefile b/arch/riscv/errata/Makefile
>> index f0da9d7b39c3..bc6c77ba837d 100644
>> --- a/arch/riscv/errata/Makefile
>> +++ b/arch/riscv/errata/Makefile
>> @@ -1,5 +1,9 @@
>>   ifdef CONFIG_RELOCATABLE
>> -KBUILD_CFLAGS += -fno-pie
>> +# We can't use PIC/PIE when handling early-boot errata parsing, as 
>> the kernel
>> +# doesn't have a GOT setup at that point.  So instead just use 
>> medany: it's
>> +# usually position-independent, so it should be good enough for the 
>> errata
>> +# handling.
>> +KBUILD_CFLAGS += -fno-pie -mcmodel=medany
>>   endif
>>     ifdef CONFIG_RISCV_ALTERNATIVE_EARLY
>
> _______________________________________________
> linux-riscv mailing list
> linux-riscv@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-riscv
Palmer Dabbelt March 30, 2025, 7:41 p.m. UTC | #3
On Fri, 28 Mar 2025 01:18:01 PDT (-0700), Alexandre Ghiti wrote:
> On 26/03/2025 23:45, Palmer Dabbelt wrote:
>> We're trying to mix non-PIC/PIE objects into the otherwise-PIE
>> relocatable kernels, to avoid GOT/PLT references during early boot
>> alternative resolution (which happens before the GOT/PLT are set up).
>>
>> riscv64-unknown-linux-gnu-ld: arch/riscv/errata/sifive/errata.o: relocation R_RISCV_HI20 against `tlb_flush_all_threshold' can not be used when making a shared object; recompile with -fPIC
>> riscv64-unknown-linux-gnu-ld: arch/riscv/errata/thead/errata.o: relocation R_RISCV_HI20 against `riscv_cbom_block_size' can not be used when making a shared object; recompile with -fPIC
>
>
> thead erratas are already compiled with medany if
> CONFIG_RISCV_ALTERNATIVE_EARLY is set
> (https://elixir.bootlin.com/linux/v6.14-rc6/source/arch/riscv/errata/thead/Makefile#L2)
> so why isn't it enough?

Those are linker errors, we can't pull the medlow code into PIC objects.  
So any objects in relocatable kernels need to either be PIC/PIE or be 
medany (which can't reference any absolute symbols).

>> Fixes: 8dc2a7e8027f ("riscv: Fix relocatable kernels with early alternatives using -fno-pie")
>> Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
>> ---
>> I'm not 100% sure it's possible to expose this without Samuel's NOMMU
>> relocatable builds, see
>> <https://lore.kernel.org/all/mhng-517c0318-c8fb-4c4a-aa0e-0d08f8d34d78@palmer-ri-x1c9/>
>> ---
>>   arch/riscv/errata/Makefile | 6 +++++-
>>   1 file changed, 5 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/riscv/errata/Makefile b/arch/riscv/errata/Makefile
>> index f0da9d7b39c3..bc6c77ba837d 100644
>> --- a/arch/riscv/errata/Makefile
>> +++ b/arch/riscv/errata/Makefile
>> @@ -1,5 +1,9 @@
>>   ifdef CONFIG_RELOCATABLE
>> -KBUILD_CFLAGS += -fno-pie
>> +# We can't use PIC/PIE when handling early-boot errata parsing, as the kernel
>> +# doesn't have a GOT setup at that point.  So instead just use medany: it's
>> +# usually position-independent, so it should be good enough for the errata
>> +# handling.
>> +KBUILD_CFLAGS += -fno-pie -mcmodel=medany
>>   endif
>>
>>   ifdef CONFIG_RISCV_ALTERNATIVE_EARLY
>
> _______________________________________________
> linux-riscv mailing list
> linux-riscv@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-riscv
patchwork-bot+linux-riscv@kernel.org March 31, 2025, 3 a.m. UTC | #4
Hello:

This patch was applied to riscv/linux.git (for-next)
by Palmer Dabbelt <palmer@rivosinc.com>:

On Wed, 26 Mar 2025 15:45:07 -0700 you wrote:
> We're trying to mix non-PIC/PIE objects into the otherwise-PIE
> relocatable kernels, to avoid GOT/PLT references during early boot
> alternative resolution (which happens before the GOT/PLT are set up).
> 
> riscv64-unknown-linux-gnu-ld: arch/riscv/errata/sifive/errata.o: relocation R_RISCV_HI20 against `tlb_flush_all_threshold' can not be used when making a shared object; recompile with -fPIC
> riscv64-unknown-linux-gnu-ld: arch/riscv/errata/thead/errata.o: relocation R_RISCV_HI20 against `riscv_cbom_block_size' can not be used when making a shared object; recompile with -fPIC
> 
> [...]

Here is the summary with links:
  - RISC-V: errata: Use medany for relocatable builds
    https://git.kernel.org/riscv/c/bb58e1579f43

You are awesome, thank you!
diff mbox series

Patch

diff --git a/arch/riscv/errata/Makefile b/arch/riscv/errata/Makefile
index f0da9d7b39c3..bc6c77ba837d 100644
--- a/arch/riscv/errata/Makefile
+++ b/arch/riscv/errata/Makefile
@@ -1,5 +1,9 @@ 
 ifdef CONFIG_RELOCATABLE
-KBUILD_CFLAGS += -fno-pie
+# We can't use PIC/PIE when handling early-boot errata parsing, as the kernel
+# doesn't have a GOT setup at that point.  So instead just use medany: it's
+# usually position-independent, so it should be good enough for the errata
+# handling.
+KBUILD_CFLAGS += -fno-pie -mcmodel=medany
 endif
 
 ifdef CONFIG_RISCV_ALTERNATIVE_EARLY