2023-03-29 04:59:22

by Alexandre Ghiti

[permalink] [raw]
Subject: [PATCH v9 0/6] Introduce 64b relocatable kernel

After multiple attempts, this patchset is now based on the fact that the
64b kernel mapping was moved outside the linear mapping.

The first patch allows to build relocatable kernels but is not selected
by default. That patch is a requirement for KASLR.
The second and third patches take advantage of an already existing powerpc
script that checks relocations at compile-time, and uses it for riscv.

This patchset is rebased on top of:

riscv: Use PUD/P4D/PGD pages for the linear mapping
(https://patchwork.kernel.org/project/linux-riscv/list/?series=733603)
base-commit-tag: v6.3-rc1

Changes in v9:
* Fix gcc/llvm compilation errors by adding patch 1, thanks to Bjorn
* Move a patch to move rela.dyn outside of init (patch 2): it is a
separate patch to clearly explain why
* To effectively move rela.dyn to init, we need to add patch 6: separate patch since we may be
able at some point to revert (along with patch 2).
* Add a lot of orphan sections to the linker script

Changes in v8:
* Fix UEFI boot by moving rela.dyn section into the data so that PE/COFF
loader actually copies the relocations too
* Fix check that used PGDIR instead of PUD which was not correct
for sv48 and sv57
* Fix PE/COFF header data size definition as it led to size of 0

Changes in v7:
* Rebase on top of v5.15
* Fix LDFLAGS_vmlinux which was overriden when CONFIG_DYNAMIC_FTRACE was
set
* Make relocate_kernel static
* Add Ack from Michael

Changes in v6:
* Remove the kernel move to vmalloc zone
* Rebased on top of for-next
* Remove relocatable property from 32b kernel as the kernel is mapped in
the linear mapping and would then need to be copied physically too
* CONFIG_RELOCATABLE depends on !XIP_KERNEL
* Remove Reviewed-by from first patch as it changed a bit

Changes in v5:
* Add "static __init" to create_kernel_page_table function as reported by
Kbuild test robot
* Add reviewed-by from Zong
* Rebase onto v5.7

Changes in v4:
* Fix BPF region that overlapped with kernel's as suggested by Zong
* Fix end of module region that could be larger than 2GB as suggested by Zong
* Fix the size of the vm area reserved for the kernel as we could lose
PMD_SIZE if the size was already aligned on PMD_SIZE
* Split compile time relocations check patch into 2 patches as suggested by Anup
* Applied Reviewed-by from Zong and Anup

Changes in v3:
* Move kernel mapping to vmalloc

Changes in v2:
* Make RELOCATABLE depend on MMU as suggested by Anup
* Rename kernel_load_addr into kernel_virt_addr as suggested by Anup
* Use __pa_symbol instead of __pa, as suggested by Zong
* Rebased on top of v5.6-rc3
* Tested with sv48 patchset
* Add Reviewed/Tested-by from Zong and Anup

Alexandre Ghiti (6):
riscv: Prepare EFI header for relocatable kernels
riscv: Move .rela.dyn outside of init to avoid empty relocations
riscv: Introduce CONFIG_RELOCATABLE
powerpc: Move script to check relocations at compile time in scripts/
riscv: Check relocations at compile time
riscv: Use --emit-relocs in order to move .rela.dyn in init

arch/powerpc/tools/relocs_check.sh | 18 ++--------
arch/riscv/Kconfig | 14 ++++++++
arch/riscv/Makefile | 7 ++--
arch/riscv/Makefile.postlink | 49 ++++++++++++++++++++++++++
arch/riscv/boot/Makefile | 7 ++++
arch/riscv/include/asm/set_memory.h | 3 ++
arch/riscv/kernel/efi-header.S | 19 ++++++++--
arch/riscv/kernel/vmlinux.lds.S | 26 ++++++++++----
arch/riscv/mm/Makefile | 4 +++
arch/riscv/mm/init.c | 54 ++++++++++++++++++++++++++++-
arch/riscv/tools/relocs_check.sh | 26 ++++++++++++++
scripts/relocs_check.sh | 20 +++++++++++
12 files changed, 218 insertions(+), 29 deletions(-)
create mode 100644 arch/riscv/Makefile.postlink
create mode 100755 arch/riscv/tools/relocs_check.sh
create mode 100755 scripts/relocs_check.sh

--
2.37.2


2023-03-29 05:00:03

by Alexandre Ghiti

[permalink] [raw]
Subject: [PATCH v9 2/6] riscv: Move .rela.dyn outside of init to avoid empty relocations

This is a preparatory patch for relocatable kernels: .rela.dyn should be
in .init but doing so actually produces empty relocations, so this should
be a temporary commit until we find a solution.

This issue was reported here [1].

[1] https://lore.kernel.org/all/[email protected]/.

Signed-off-by: Alexandre Ghiti <[email protected]>
---
arch/riscv/kernel/vmlinux.lds.S | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.S
index 1c38294580c0..e05e6df44225 100644
--- a/arch/riscv/kernel/vmlinux.lds.S
+++ b/arch/riscv/kernel/vmlinux.lds.S
@@ -96,10 +96,6 @@ SECTIONS
*(.rel.dyn*)
}

- .rela.dyn : {
- *(.rela*)
- }
-
__init_data_end = .;

. = ALIGN(8);
@@ -126,6 +122,10 @@ SECTIONS
*(.sdata*)
}

+ .rela.dyn : {
+ *(.rela*)
+ }
+
#ifdef CONFIG_EFI
.pecoff_edata_padding : { BYTE(0); . = ALIGN(PECOFF_FILE_ALIGNMENT); }
__pecoff_data_raw_size = ABSOLUTE(. - __pecoff_text_end);
--
2.37.2

2023-03-29 05:00:05

by Alexandre Ghiti

[permalink] [raw]
Subject: [PATCH v9 1/6] riscv: Prepare EFI header for relocatable kernels

ld does not handle relocations correctly as explained here [1],
a fix for that was proposed by Nelson there but we have to support older
toolchains and then provide this fix.

Note that llvm does not need this fix and is then excluded.

[1] https://sourceware.org/pipermail/binutils/2023-March/126690.html

Signed-off-by: Alexandre Ghiti <[email protected]>
---
arch/riscv/include/asm/set_memory.h | 3 +++
arch/riscv/kernel/efi-header.S | 19 ++++++++++++++++---
arch/riscv/kernel/vmlinux.lds.S | 5 ++---
3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/arch/riscv/include/asm/set_memory.h b/arch/riscv/include/asm/set_memory.h
index a2c14d4b3993..ec11001c3fe0 100644
--- a/arch/riscv/include/asm/set_memory.h
+++ b/arch/riscv/include/asm/set_memory.h
@@ -56,4 +56,7 @@ bool kernel_page_present(struct page *page);
#define SECTION_ALIGN L1_CACHE_BYTES
#endif /* CONFIG_STRICT_KERNEL_RWX */

+#define PECOFF_SECTION_ALIGNMENT 0x1000
+#define PECOFF_FILE_ALIGNMENT 0x200
+
#endif /* _ASM_RISCV_SET_MEMORY_H */
diff --git a/arch/riscv/kernel/efi-header.S b/arch/riscv/kernel/efi-header.S
index 8e733aa48ba6..515b2dfbca75 100644
--- a/arch/riscv/kernel/efi-header.S
+++ b/arch/riscv/kernel/efi-header.S
@@ -6,6 +6,7 @@

#include <linux/pe.h>
#include <linux/sizes.h>
+#include <asm/set_memory.h>

.macro __EFI_PE_HEADER
.long PE_MAGIC
@@ -33,7 +34,11 @@ optional_header:
.byte 0x02 // MajorLinkerVersion
.byte 0x14 // MinorLinkerVersion
.long __pecoff_text_end - efi_header_end // SizeOfCode
- .long __pecoff_data_virt_size // SizeOfInitializedData
+#ifdef __clang__
+ .long __pecoff_data_virt_size // SizeOfInitializedData
+#else
+ .long __pecoff_data_virt_end - __pecoff_text_end // SizeOfInitializedData
+#endif
.long 0 // SizeOfUninitializedData
.long __efistub_efi_pe_entry - _start // AddressOfEntryPoint
.long efi_header_end - _start // BaseOfCode
@@ -91,9 +96,17 @@ section_table:
IMAGE_SCN_MEM_EXECUTE // Characteristics

.ascii ".data\0\0\0"
- .long __pecoff_data_virt_size // VirtualSize
+#ifdef __clang__
+ .long __pecoff_data_virt_size // VirtualSize
+#else
+ .long __pecoff_data_virt_end - __pecoff_text_end // VirtualSize
+#endif
.long __pecoff_text_end - _start // VirtualAddress
- .long __pecoff_data_raw_size // SizeOfRawData
+#ifdef __clang__
+ .long __pecoff_data_raw_size // SizeOfRawData
+#else
+ .long __pecoff_data_raw_end - __pecoff_text_end // SizeOfRawData
+#endif
.long __pecoff_text_end - _start // PointerToRawData

.long 0 // PointerToRelocations
diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.S
index 53a8ad65b255..1c38294580c0 100644
--- a/arch/riscv/kernel/vmlinux.lds.S
+++ b/arch/riscv/kernel/vmlinux.lds.S
@@ -27,9 +27,6 @@ ENTRY(_start)

jiffies = jiffies_64;

-PECOFF_SECTION_ALIGNMENT = 0x1000;
-PECOFF_FILE_ALIGNMENT = 0x200;
-
SECTIONS
{
/* Beginning of code and text segment */
@@ -132,6 +129,7 @@ SECTIONS
#ifdef CONFIG_EFI
.pecoff_edata_padding : { BYTE(0); . = ALIGN(PECOFF_FILE_ALIGNMENT); }
__pecoff_data_raw_size = ABSOLUTE(. - __pecoff_text_end);
+ __pecoff_data_raw_end = ABSOLUTE(.);
#endif

/* End of data section */
@@ -142,6 +140,7 @@ SECTIONS
#ifdef CONFIG_EFI
. = ALIGN(PECOFF_SECTION_ALIGNMENT);
__pecoff_data_virt_size = ABSOLUTE(. - __pecoff_text_end);
+ __pecoff_data_virt_end = ABSOLUTE(.);
#endif
_end = .;

--
2.37.2

2023-03-29 05:00:19

by Alexandre Ghiti

[permalink] [raw]
Subject: [PATCH v9 3/6] riscv: Introduce CONFIG_RELOCATABLE

This config allows to compile 64b kernel as PIE and to relocate it at
any virtual address at runtime: this paves the way to KASLR.
Runtime relocation is possible since relocation metadata are embedded into
the kernel.

Note that relocating at runtime introduces an overhead even if the
kernel is loaded at the same address it was linked at and that the compiler
options are those used in arm64 which uses the same RELA relocation
format.

Signed-off-by: Alexandre Ghiti <[email protected]>
---
arch/riscv/Kconfig | 14 +++++++++
arch/riscv/Makefile | 7 +++--
arch/riscv/kernel/vmlinux.lds.S | 17 +++++++++--
arch/riscv/mm/Makefile | 4 +++
arch/riscv/mm/init.c | 54 ++++++++++++++++++++++++++++++++-
5 files changed, 91 insertions(+), 5 deletions(-)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 3c5907431081..6ff9f574195d 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -562,6 +562,20 @@ config COMPAT

If you want to execute 32-bit userspace applications, say Y.

+config RELOCATABLE
+ bool "Build a relocatable kernel"
+ depends on MMU && 64BIT && !XIP_KERNEL
+ help
+ This builds a kernel as a Position Independent Executable (PIE),
+ which retains all relocation metadata required to relocate the
+ kernel binary at runtime to a different virtual address than the
+ address it was linked at.
+ Since RISCV uses the RELA relocation format, this requires a
+ relocation pass at runtime even if the kernel is loaded at the
+ same address it was linked at.
+
+ If unsure, say N.
+
endmenu # "Kernel features"

menu "Boot options"
diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile
index 6203c3378922..860b09e409c7 100644
--- a/arch/riscv/Makefile
+++ b/arch/riscv/Makefile
@@ -7,9 +7,12 @@
#

OBJCOPYFLAGS := -O binary
-LDFLAGS_vmlinux :=
+ifeq ($(CONFIG_RELOCATABLE),y)
+ LDFLAGS_vmlinux += -shared -Bsymbolic -z notext -z norelro
+ KBUILD_CFLAGS += -fPIE
+endif
ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
- LDFLAGS_vmlinux := --no-relax
+ LDFLAGS_vmlinux += --no-relax
KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
ifeq ($(CONFIG_RISCV_ISA_C),y)
CC_FLAGS_FTRACE := -fpatchable-function-entry=4
diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.S
index e05e6df44225..615ff5842690 100644
--- a/arch/riscv/kernel/vmlinux.lds.S
+++ b/arch/riscv/kernel/vmlinux.lds.S
@@ -122,10 +122,23 @@ SECTIONS
*(.sdata*)
}

- .rela.dyn : {
- *(.rela*)
+ .rela.dyn : ALIGN(8) {
+ __rela_dyn_start = .;
+ *(.rela .rela*)
+ __rela_dyn_end = .;
}

+#ifdef CONFIG_RELOCATABLE
+ .data.rel : { *(.data.rel*) }
+ .got : { *(.got*) }
+ .plt : { *(.plt) }
+ .dynamic : { *(.dynamic) }
+ .dynsym : { *(.dynsym) }
+ .dynstr : { *(.dynstr) }
+ .hash : { *(.hash) }
+ .gnu.hash : { *(.gnu.hash) }
+#endif
+
#ifdef CONFIG_EFI
.pecoff_edata_padding : { BYTE(0); . = ALIGN(PECOFF_FILE_ALIGNMENT); }
__pecoff_data_raw_size = ABSOLUTE(. - __pecoff_text_end);
diff --git a/arch/riscv/mm/Makefile b/arch/riscv/mm/Makefile
index 2ac177c05352..b85e9e82f082 100644
--- a/arch/riscv/mm/Makefile
+++ b/arch/riscv/mm/Makefile
@@ -1,6 +1,10 @@
# SPDX-License-Identifier: GPL-2.0-only

CFLAGS_init.o := -mcmodel=medany
+ifdef CONFIG_RELOCATABLE
+CFLAGS_init.o += -fno-pie
+endif
+
ifdef CONFIG_FTRACE
CFLAGS_REMOVE_init.o = $(CC_FLAGS_FTRACE)
CFLAGS_REMOVE_cacheflush.o = $(CC_FLAGS_FTRACE)
diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index f803671d18b2..bce899b180cd 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -20,6 +20,9 @@
#include <linux/dma-map-ops.h>
#include <linux/crash_dump.h>
#include <linux/hugetlb.h>
+#ifdef CONFIG_RELOCATABLE
+#include <linux/elf.h>
+#endif

#include <asm/fixmap.h>
#include <asm/tlbflush.h>
@@ -146,7 +149,7 @@ static void __init print_vm_layout(void)
print_ml("kasan", KASAN_SHADOW_START, KASAN_SHADOW_END);
#endif

- print_ml("kernel", (unsigned long)KERNEL_LINK_ADDR,
+ print_ml("kernel", (unsigned long)kernel_map.virt_addr,
(unsigned long)ADDRESS_SPACE_END);
}
}
@@ -831,6 +834,44 @@ static __init void set_satp_mode(void)
#error "setup_vm() is called from head.S before relocate so it should not use absolute addressing."
#endif

+#ifdef CONFIG_RELOCATABLE
+extern unsigned long __rela_dyn_start, __rela_dyn_end;
+
+static void __init relocate_kernel(void)
+{
+ Elf64_Rela *rela = (Elf64_Rela *)&__rela_dyn_start;
+ /*
+ * This holds the offset between the linked virtual address and the
+ * relocated virtual address.
+ */
+ uintptr_t reloc_offset = kernel_map.virt_addr - KERNEL_LINK_ADDR;
+ /*
+ * This holds the offset between kernel linked virtual address and
+ * physical address.
+ */
+ uintptr_t va_kernel_link_pa_offset = KERNEL_LINK_ADDR - kernel_map.phys_addr;
+
+ for ( ; rela < (Elf64_Rela *)&__rela_dyn_end; rela++) {
+ Elf64_Addr addr = (rela->r_offset - va_kernel_link_pa_offset);
+ Elf64_Addr relocated_addr = rela->r_addend;
+
+ if (rela->r_info != R_RISCV_RELATIVE)
+ continue;
+
+ /*
+ * Make sure to not relocate vdso symbols like rt_sigreturn
+ * which are linked from the address 0 in vmlinux since
+ * vdso symbol addresses are actually used as an offset from
+ * mm->context.vdso in VDSO_OFFSET macro.
+ */
+ if (relocated_addr >= KERNEL_LINK_ADDR)
+ relocated_addr += reloc_offset;
+
+ *(Elf64_Addr *)addr = relocated_addr;
+ }
+}
+#endif /* CONFIG_RELOCATABLE */
+
#ifdef CONFIG_XIP_KERNEL
static void __init create_kernel_page_table(pgd_t *pgdir,
__always_unused bool early)
@@ -1029,6 +1070,17 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
BUG_ON((kernel_map.virt_addr + kernel_map.size) > ADDRESS_SPACE_END - SZ_4K);
#endif

+#ifdef CONFIG_RELOCATABLE
+ /*
+ * Early page table uses only one PUD, which makes it possible
+ * to map PUD_SIZE aligned on PUD_SIZE: if the relocation offset
+ * makes the kernel cross over a PUD_SIZE boundary, raise a bug
+ * since a part of the kernel would not get mapped.
+ */
+ BUG_ON(PUD_SIZE - (kernel_map.virt_addr & (PUD_SIZE - 1)) < kernel_map.size);
+ relocate_kernel();
+#endif
+
apply_early_boot_alternatives();
pt_ops_set_early();

--
2.37.2

2023-03-29 05:00:42

by Alexandre Ghiti

[permalink] [raw]
Subject: [PATCH v9 4/6] powerpc: Move script to check relocations at compile time in scripts/

From: Alexandre Ghiti <[email protected]>

Relocating kernel at runtime is done very early in the boot process, so
it is not convenient to check for relocations there and react in case a
relocation was not expected.

Powerpc architecture has a script that allows to check at compile time
for such unexpected relocations: extract the common logic to scripts/
so that other architectures can take advantage of it.

Signed-off-by: Alexandre Ghiti <[email protected]>
Reviewed-by: Anup Patel <[email protected]>
Acked-by: Michael Ellerman <[email protected]> (powerpc)
---
arch/powerpc/tools/relocs_check.sh | 18 ++----------------
scripts/relocs_check.sh | 20 ++++++++++++++++++++
2 files changed, 22 insertions(+), 16 deletions(-)
create mode 100755 scripts/relocs_check.sh

diff --git a/arch/powerpc/tools/relocs_check.sh b/arch/powerpc/tools/relocs_check.sh
index 63792af00417..6b350e75014c 100755
--- a/arch/powerpc/tools/relocs_check.sh
+++ b/arch/powerpc/tools/relocs_check.sh
@@ -15,21 +15,8 @@ if [ $# -lt 3 ]; then
exit 1
fi

-# Have Kbuild supply the path to objdump and nm so we handle cross compilation.
-objdump="$1"
-nm="$2"
-vmlinux="$3"
-
-# Remove from the bad relocations those that match an undefined weak symbol
-# which will result in an absolute relocation to 0.
-# Weak unresolved symbols are of that form in nm output:
-# " w _binary__btf_vmlinux_bin_end"
-undef_weak_symbols=$($nm "$vmlinux" | awk '$1 ~ /w/ { print $2 }')
-
bad_relocs=$(
-$objdump -R "$vmlinux" |
- # Only look at relocation lines.
- grep -E '\<R_' |
+${srctree}/scripts/relocs_check.sh "$@" |
# These relocations are okay
# On PPC64:
# R_PPC64_RELATIVE, R_PPC64_NONE
@@ -44,8 +31,7 @@ R_PPC_ADDR16_LO
R_PPC_ADDR16_HI
R_PPC_ADDR16_HA
R_PPC_RELATIVE
-R_PPC_NONE' |
- ([ "$undef_weak_symbols" ] && grep -F -w -v "$undef_weak_symbols" || cat)
+R_PPC_NONE'
)

if [ -z "$bad_relocs" ]; then
diff --git a/scripts/relocs_check.sh b/scripts/relocs_check.sh
new file mode 100755
index 000000000000..137c660499f3
--- /dev/null
+++ b/scripts/relocs_check.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+# Get a list of all the relocations, remove from it the relocations
+# that are known to be legitimate and return this list to arch specific
+# script that will look for suspicious relocations.
+
+objdump="$1"
+nm="$2"
+vmlinux="$3"
+
+# Remove from the possible bad relocations those that match an undefined
+# weak symbol which will result in an absolute relocation to 0.
+# Weak unresolved symbols are of that form in nm output:
+# " w _binary__btf_vmlinux_bin_end"
+undef_weak_symbols=$($nm "$vmlinux" | awk '$1 ~ /w/ { print $2 }')
+
+$objdump -R "$vmlinux" |
+ grep -E '\<R_' |
+ ([ "$undef_weak_symbols" ] && grep -F -w -v "$undef_weak_symbols" || cat)
--
2.37.2

2023-03-29 05:01:11

by Alexandre Ghiti

[permalink] [raw]
Subject: [PATCH v9 5/6] riscv: Check relocations at compile time

From: Alexandre Ghiti <[email protected]>

Relocating kernel at runtime is done very early in the boot process, so
it is not convenient to check for relocations there and react in case a
relocation was not expected.

There exists a script in scripts/ that extracts the relocations from
vmlinux that is then used at postlink to check the relocations.

Signed-off-by: Alexandre Ghiti <[email protected]>
Reviewed-by: Anup Patel <[email protected]>
---
arch/riscv/Makefile.postlink | 36 ++++++++++++++++++++++++++++++++
arch/riscv/tools/relocs_check.sh | 26 +++++++++++++++++++++++
2 files changed, 62 insertions(+)
create mode 100644 arch/riscv/Makefile.postlink
create mode 100755 arch/riscv/tools/relocs_check.sh

diff --git a/arch/riscv/Makefile.postlink b/arch/riscv/Makefile.postlink
new file mode 100644
index 000000000000..d5de8d520d3e
--- /dev/null
+++ b/arch/riscv/Makefile.postlink
@@ -0,0 +1,36 @@
+# SPDX-License-Identifier: GPL-2.0
+# ===========================================================================
+# Post-link riscv pass
+# ===========================================================================
+#
+# Check that vmlinux relocations look sane
+
+PHONY := __archpost
+__archpost:
+
+-include include/config/auto.conf
+include $(srctree)/scripts/Kbuild.include
+
+quiet_cmd_relocs_check = CHKREL $@
+cmd_relocs_check = \
+ $(CONFIG_SHELL) $(srctree)/arch/riscv/tools/relocs_check.sh "$(OBJDUMP)" "$(NM)" "$@"
+
+# `@true` prevents complaint when there is nothing to be done
+
+vmlinux: FORCE
+ @true
+ifdef CONFIG_RELOCATABLE
+ $(call if_changed,relocs_check)
+endif
+
+%.ko: FORCE
+ @true
+
+clean:
+ @true
+
+PHONY += FORCE clean
+
+FORCE:
+
+.PHONY: $(PHONY)
diff --git a/arch/riscv/tools/relocs_check.sh b/arch/riscv/tools/relocs_check.sh
new file mode 100755
index 000000000000..baeb2e7b2290
--- /dev/null
+++ b/arch/riscv/tools/relocs_check.sh
@@ -0,0 +1,26 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Based on powerpc relocs_check.sh
+
+# This script checks the relocations of a vmlinux for "suspicious"
+# relocations.
+
+if [ $# -lt 3 ]; then
+ echo "$0 [path to objdump] [path to nm] [path to vmlinux]" 1>&2
+ exit 1
+fi
+
+bad_relocs=$(
+${srctree}/scripts/relocs_check.sh "$@" |
+ # These relocations are okay
+ # R_RISCV_RELATIVE
+ grep -F -w -v 'R_RISCV_RELATIVE'
+)
+
+if [ -z "$bad_relocs" ]; then
+ exit 0
+fi
+
+num_bad=$(echo "$bad_relocs" | wc -l)
+echo "WARNING: $num_bad bad relocations"
+echo "$bad_relocs"
--
2.37.2

2023-03-29 05:01:17

by Alexandre Ghiti

[permalink] [raw]
Subject: [PATCH v9 6/6] riscv: Use --emit-relocs in order to move .rela.dyn in init

To circumvent an issue where placing the relocations inside the init
sections produces empty relocations, use --emit-relocs. But to avoid
carrying those relocations in vmlinux, use an intermediate
vmlinux.relocs file which is a copy of vmlinux *before* stripping its
relocations.

Suggested-by: Björn Töpel <[email protected]>
Suggested-by: Nick Desaulniers <[email protected]>
Signed-off-by: Alexandre Ghiti <[email protected]>
---
arch/riscv/Makefile | 2 +-
arch/riscv/Makefile.postlink | 13 +++++++++++++
arch/riscv/boot/Makefile | 7 +++++++
3 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile
index 860b09e409c7..7dc6904a6836 100644
--- a/arch/riscv/Makefile
+++ b/arch/riscv/Makefile
@@ -8,7 +8,7 @@

OBJCOPYFLAGS := -O binary
ifeq ($(CONFIG_RELOCATABLE),y)
- LDFLAGS_vmlinux += -shared -Bsymbolic -z notext -z norelro
+ LDFLAGS_vmlinux += -shared -Bsymbolic -z notext -z norelro --emit-relocs
KBUILD_CFLAGS += -fPIE
endif
ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
diff --git a/arch/riscv/Makefile.postlink b/arch/riscv/Makefile.postlink
index d5de8d520d3e..a46fc578b30b 100644
--- a/arch/riscv/Makefile.postlink
+++ b/arch/riscv/Makefile.postlink
@@ -15,12 +15,25 @@ quiet_cmd_relocs_check = CHKREL $@
cmd_relocs_check = \
$(CONFIG_SHELL) $(srctree)/arch/riscv/tools/relocs_check.sh "$(OBJDUMP)" "$(NM)" "$@"

+ifdef CONFIG_RELOCATABLE
+quiet_cmd_cp_vmlinux_relocs = CPREL vmlinux.relocs
+cmd_cp_vmlinux_relocs = cp vmlinux vmlinux.relocs
+
+quiet_cmd_relocs_strip = STRIPREL $@
+cmd_relocs_strip = $(OBJCOPY) --remove-section='.rel.*' \
+ --remove-section='.rel__*' \
+ --remove-section='.rela.*' \
+ --remove-section='.rela__*' $@
+endif
+
# `@true` prevents complaint when there is nothing to be done

vmlinux: FORCE
@true
ifdef CONFIG_RELOCATABLE
$(call if_changed,relocs_check)
+ $(call if_changed,cp_vmlinux_relocs)
+ $(call if_changed,relocs_strip)
endif

%.ko: FORCE
diff --git a/arch/riscv/boot/Makefile b/arch/riscv/boot/Makefile
index c72de7232abb..22b13947bd13 100644
--- a/arch/riscv/boot/Makefile
+++ b/arch/riscv/boot/Makefile
@@ -33,7 +33,14 @@ $(obj)/xipImage: vmlinux FORCE

endif

+ifdef CONFIG_RELOCATABLE
+vmlinux.relocs: vmlinux
+ @ (! [ -f vmlinux.relocs ] && echo "vmlinux.relocs can't be found, please remove vmlinux and try again") || true
+
+$(obj)/Image: vmlinux.relocs FORCE
+else
$(obj)/Image: vmlinux FORCE
+endif
$(call if_changed,objcopy)

$(obj)/Image.gz: $(obj)/Image FORCE
--
2.37.2

Subject: Re: [PATCH v9 0/6] Introduce 64b relocatable kernel

Hello:

This series was applied to riscv/linux.git (for-next)
by Palmer Dabbelt <[email protected]>:

On Wed, 29 Mar 2023 06:53:23 +0200 you wrote:
> After multiple attempts, this patchset is now based on the fact that the
> 64b kernel mapping was moved outside the linear mapping.
>
> The first patch allows to build relocatable kernels but is not selected
> by default. That patch is a requirement for KASLR.
> The second and third patches take advantage of an already existing powerpc
> script that checks relocations at compile-time, and uses it for riscv.
>
> [...]

Here is the summary with links:
- [v9,1/6] riscv: Prepare EFI header for relocatable kernels
https://git.kernel.org/riscv/c/55de1e4ad43b
- [v9,2/6] riscv: Move .rela.dyn outside of init to avoid empty relocations
https://git.kernel.org/riscv/c/69a90d2fe107
- [v9,3/6] riscv: Introduce CONFIG_RELOCATABLE
https://git.kernel.org/riscv/c/39b33072941f
- [v9,4/6] powerpc: Move script to check relocations at compile time in scripts/
https://git.kernel.org/riscv/c/47981b5cc687
- [v9,5/6] riscv: Check relocations at compile time
https://git.kernel.org/riscv/c/c2dea0bc5339
- [v9,6/6] riscv: Use --emit-relocs in order to move .rela.dyn in init
https://git.kernel.org/riscv/c/559d1e45a16d

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html


2023-04-21 19:06:35

by Palmer Dabbelt

[permalink] [raw]
Subject: Re: [PATCH v9 0/6] Introduce 64b relocatable kernel

On Tue, 28 Mar 2023 21:53:23 PDT (-0700), [email protected] wrote:
> After multiple attempts, this patchset is now based on the fact that the
> 64b kernel mapping was moved outside the linear mapping.
>
> The first patch allows to build relocatable kernels but is not selected
> by default. That patch is a requirement for KASLR.
> The second and third patches take advantage of an already existing powerpc
> script that checks relocations at compile-time, and uses it for riscv.
>
> This patchset is rebased on top of:
>
> riscv: Use PUD/P4D/PGD pages for the linear mapping
> (https://patchwork.kernel.org/project/linux-riscv/list/?series=733603)
> base-commit-tag: v6.3-rc1

The QEMU CI has some way to say "this depends on an un-merged patch set
sent as $MESSAGE_ID", not sure if that's a b4-ism but it's a bit less
confusing.

I merged this on top of the pre-merge hugepage linear mapping changes,
it's on for-next.

> Changes in v9:
> * Fix gcc/llvm compilation errors by adding patch 1, thanks to Bjorn
> * Move a patch to move rela.dyn outside of init (patch 2): it is a
> separate patch to clearly explain why
> * To effectively move rela.dyn to init, we need to add patch 6: separate patch since we may be
> able at some point to revert (along with patch 2).
> * Add a lot of orphan sections to the linker script
>
> Changes in v8:
> * Fix UEFI boot by moving rela.dyn section into the data so that PE/COFF
> loader actually copies the relocations too
> * Fix check that used PGDIR instead of PUD which was not correct
> for sv48 and sv57
> * Fix PE/COFF header data size definition as it led to size of 0
>
> Changes in v7:
> * Rebase on top of v5.15
> * Fix LDFLAGS_vmlinux which was overriden when CONFIG_DYNAMIC_FTRACE was
> set
> * Make relocate_kernel static
> * Add Ack from Michael
>
> Changes in v6:
> * Remove the kernel move to vmalloc zone
> * Rebased on top of for-next
> * Remove relocatable property from 32b kernel as the kernel is mapped in
> the linear mapping and would then need to be copied physically too
> * CONFIG_RELOCATABLE depends on !XIP_KERNEL
> * Remove Reviewed-by from first patch as it changed a bit
>
> Changes in v5:
> * Add "static __init" to create_kernel_page_table function as reported by
> Kbuild test robot
> * Add reviewed-by from Zong
> * Rebase onto v5.7
>
> Changes in v4:
> * Fix BPF region that overlapped with kernel's as suggested by Zong
> * Fix end of module region that could be larger than 2GB as suggested by Zong
> * Fix the size of the vm area reserved for the kernel as we could lose
> PMD_SIZE if the size was already aligned on PMD_SIZE
> * Split compile time relocations check patch into 2 patches as suggested by Anup
> * Applied Reviewed-by from Zong and Anup
>
> Changes in v3:
> * Move kernel mapping to vmalloc
>
> Changes in v2:
> * Make RELOCATABLE depend on MMU as suggested by Anup
> * Rename kernel_load_addr into kernel_virt_addr as suggested by Anup
> * Use __pa_symbol instead of __pa, as suggested by Zong
> * Rebased on top of v5.6-rc3
> * Tested with sv48 patchset
> * Add Reviewed/Tested-by from Zong and Anup
>
> Alexandre Ghiti (6):
> riscv: Prepare EFI header for relocatable kernels
> riscv: Move .rela.dyn outside of init to avoid empty relocations
> riscv: Introduce CONFIG_RELOCATABLE
> powerpc: Move script to check relocations at compile time in scripts/
> riscv: Check relocations at compile time
> riscv: Use --emit-relocs in order to move .rela.dyn in init
>
> arch/powerpc/tools/relocs_check.sh | 18 ++--------
> arch/riscv/Kconfig | 14 ++++++++
> arch/riscv/Makefile | 7 ++--
> arch/riscv/Makefile.postlink | 49 ++++++++++++++++++++++++++
> arch/riscv/boot/Makefile | 7 ++++
> arch/riscv/include/asm/set_memory.h | 3 ++
> arch/riscv/kernel/efi-header.S | 19 ++++++++--
> arch/riscv/kernel/vmlinux.lds.S | 26 ++++++++++----
> arch/riscv/mm/Makefile | 4 +++
> arch/riscv/mm/init.c | 54 ++++++++++++++++++++++++++++-
> arch/riscv/tools/relocs_check.sh | 26 ++++++++++++++
> scripts/relocs_check.sh | 20 +++++++++++
> 12 files changed, 218 insertions(+), 29 deletions(-)
> create mode 100644 arch/riscv/Makefile.postlink
> create mode 100755 arch/riscv/tools/relocs_check.sh
> create mode 100755 scripts/relocs_check.sh

2023-04-21 19:06:35

by Palmer Dabbelt

[permalink] [raw]
Subject: Re: [PATCH v9 0/6] Introduce 64b relocatable kernel


On Wed, 29 Mar 2023 06:53:23 +0200, Alexandre Ghiti wrote:
> After multiple attempts, this patchset is now based on the fact that the
> 64b kernel mapping was moved outside the linear mapping.
>
> The first patch allows to build relocatable kernels but is not selected
> by default. That patch is a requirement for KASLR.
> The second and third patches take advantage of an already existing powerpc
> script that checks relocations at compile-time, and uses it for riscv.
>
> [...]

Applied, thanks!

[1/6] riscv: Prepare EFI header for relocatable kernels
https://git.kernel.org/palmer/c/55de1e4ad43b
[2/6] riscv: Move .rela.dyn outside of init to avoid empty relocations
https://git.kernel.org/palmer/c/69a90d2fe107
[3/6] riscv: Introduce CONFIG_RELOCATABLE
https://git.kernel.org/palmer/c/39b33072941f
[4/6] powerpc: Move script to check relocations at compile time in scripts/
https://git.kernel.org/palmer/c/47981b5cc687
[5/6] riscv: Check relocations at compile time
https://git.kernel.org/palmer/c/c2dea0bc5339
[6/6] riscv: Use --emit-relocs in order to move .rela.dyn in init
https://git.kernel.org/palmer/c/559d1e45a16d

Best regards,
--
Palmer Dabbelt <[email protected]>

2023-04-21 19:15:39

by Konstantin Ryabitsev

[permalink] [raw]
Subject: Re: [PATCH v9 0/6] Introduce 64b relocatable kernel

April 21, 2023 2:59 PM, "Palmer Dabbelt" <[email protected]> wrote:
>> riscv: Use PUD/P4D/PGD pages for the linear mapping
>> (https://patchwork.kernel.org/project/linux-riscv/list/?series=733603)
>> base-commit-tag: v6.3-rc1
>
> The QEMU CI has some way to say "this depends on an un-merged patch set sent as $MESSAGE_ID", not
> sure if that's a b4-ism but it's a bit less confusing.

I think it's patchwork-ism, actually. B4 will eventually learn to be able to include dependent series info and automatically retrieve/apply them in the proper order on "shazam", but it can't do that yet.

-K

2023-04-21 19:48:09

by Conor Dooley

[permalink] [raw]
Subject: Re: [PATCH v9 0/6] Introduce 64b relocatable kernel

On Fri, Apr 21, 2023 at 07:10:14PM +0000, Konstantin Ryabitsev wrote:
> April 21, 2023 2:59 PM, "Palmer Dabbelt" <[email protected]> wrote:
> >> riscv: Use PUD/P4D/PGD pages for the linear mapping
> >> (https://patchwork.kernel.org/project/linux-riscv/list/?series=733603)
> >> base-commit-tag: v6.3-rc1
> >
> > The QEMU CI has some way to say "this depends on an un-merged patch set sent as $MESSAGE_ID", not
> > sure if that's a b4-ism but it's a bit less confusing.
>
> I think it's patchwork-ism, actually. B4 will eventually learn to be
> able to include dependent series info and automatically retrieve/apply
> them in the proper order on "shazam", but it can't do that yet.

A patchwork-ism or a patchew-ism? Drew Jones was my source for this, but
he had said the thing to do in QEMU-land was put a:
Based-on: $message-id
in your cover letter for each thing that you depend on. I'm not entirely
sure if that meant each series or each patch. I think patchew picks that
up and dumps in it on a patchew github account that the CI might pick up
on. From the QEMU docs:
<quote>
It is also okay to base patches on top of other on-going work that is
not yet part of the git master branch. To aid continuous integration
tools, such as `patchew <http://patchew.org/QEMU/>`__, you should `add a
tag <https://lists.gnu.org/archive/html/qemu-devel/2017-08/msg01288.html>`__
line ``Based-on: $MESSAGE_ID`` to your cover letter to make the series
dependency obvious.
<\quote>

FWIW, my vote is for something with a message-id, rather than those
patchwork series links that you can't dump into b4!


Attachments:
(No filename) (1.59 kB)
signature.asc (235.00 B)
Download all attachments

2023-09-13 02:22:04

by Fangrui Song

[permalink] [raw]
Subject: Re: [PATCH v9 6/6] riscv: Use --emit-relocs in order to move .rela.dyn in init

On 2023-03-29, Alexandre Ghiti wrote:
>To circumvent an issue where placing the relocations inside the init
>sections produces empty relocations, use --emit-relocs. But to avoid
>carrying those relocations in vmlinux, use an intermediate
>vmlinux.relocs file which is a copy of vmlinux *before* stripping its
>relocations.
>
>Suggested-by: Bj?rn T?pel <[email protected]>
>Suggested-by: Nick Desaulniers <[email protected]>
>Signed-off-by: Alexandre Ghiti <[email protected]>
>---
> arch/riscv/Makefile | 2 +-
> arch/riscv/Makefile.postlink | 13 +++++++++++++
> arch/riscv/boot/Makefile | 7 +++++++
> 3 files changed, 21 insertions(+), 1 deletion(-)
>
>diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile
>index 860b09e409c7..7dc6904a6836 100644
>--- a/arch/riscv/Makefile
>+++ b/arch/riscv/Makefile
>@@ -8,7 +8,7 @@
>
> OBJCOPYFLAGS := -O binary
> ifeq ($(CONFIG_RELOCATABLE),y)
>- LDFLAGS_vmlinux += -shared -Bsymbolic -z notext -z norelro
>+ LDFLAGS_vmlinux += -shared -Bsymbolic -z notext -z norelro --emit-relocs
> KBUILD_CFLAGS += -fPIE
> endif
> ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
>[...]

(Not subscribed.
b4 am https://lore.kernel.org/all/[email protected]/
neomutt -f v9_20230329_alexghiti_introduce_64b_relocatable_kernel.mbx)

This commit 559d1e45a16dcf1542e430ea3dce9ab625be98d0 introduced --emit-relocs to arch/riscv/.
I am concerned that --emit-relocs's relocation-type-changing behavior may not be desired and
any new use could become problematic.

https://sourceware.org/bugzilla/show_bug.cgi?id=30844 "ld riscv: --emit-relocs does not retain the original relocation type"

If either -mno-relax or --no-relax is used, --emit-relocs should be fine.