mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] LoongArch: Remove a redundant checking in relocator
@ 2024-07-06  7:38 Xi Ruoyao
  2024-07-06  7:38 ` [PATCH 2/2] LoongArch: Add support for relocating the kernel with RELR relocation Xi Ruoyao
  2024-07-06  9:44 ` [PATCH 1/2] LoongArch: Remove a redundant checking in relocator Markus Elfring
  0 siblings, 2 replies; 7+ messages in thread
From: Xi Ruoyao @ 2024-07-06  7:38 UTC (permalink / raw)
  To: Huacai Chen, WANG Xuerui
  Cc: Jinyang He, Youling Tang, Tiezhu Yang, Fangrui Song,
	Nathan Chancellor, Nick Desaulniers, loongarch, linux-kernel,
	llvm, Xi Ruoyao

With our linker script "relocated_addr >= VMLINUX_LOAD_ADDRESS" should
be always true.

Signed-off-by: Xi Ruoyao <xry111@xry111.site>
---
 arch/loongarch/kernel/relocate.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/arch/loongarch/kernel/relocate.c b/arch/loongarch/kernel/relocate.c
index 1acfa704c8d0..69d73dc7326a 100644
--- a/arch/loongarch/kernel/relocate.c
+++ b/arch/loongarch/kernel/relocate.c
@@ -34,9 +34,7 @@ static inline void __init relocate_relative(void)
 		if (rela->r_info != R_LARCH_RELATIVE)
 			continue;
 
-		if (relocated_addr >= VMLINUX_LOAD_ADDRESS)
-			relocated_addr = (Elf64_Addr)RELOCATED(relocated_addr);
-
+		relocated_addr = (Elf64_Addr)RELOCATED(relocated_addr);
 		*(Elf64_Addr *)RELOCATED(addr) = relocated_addr;
 	}
 }
-- 
2.45.2


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/2] LoongArch: Add support for relocating the kernel with RELR relocation
  2024-07-06  7:38 [PATCH 1/2] LoongArch: Remove a redundant checking in relocator Xi Ruoyao
@ 2024-07-06  7:38 ` Xi Ruoyao
  2024-07-06 10:29   ` Huacai Chen
  2024-07-06  9:44 ` [PATCH 1/2] LoongArch: Remove a redundant checking in relocator Markus Elfring
  1 sibling, 1 reply; 7+ messages in thread
From: Xi Ruoyao @ 2024-07-06  7:38 UTC (permalink / raw)
  To: Huacai Chen, WANG Xuerui
  Cc: Jinyang He, Youling Tang, Tiezhu Yang, Fangrui Song,
	Nathan Chancellor, Nick Desaulniers, loongarch, linux-kernel,
	llvm, Xi Ruoyao

RELR as a relocation packing format for relative relocations for
reducing the size of relative relocation records.  In a position
independent executable there are often many relative relocation
records, and our vmlinux is a PIE.

The LLD linker (since 17.0.0) and the BFD linker (since 2.43) supports
packing the relocations in the RELR format for LoongArch, with the flag
-z pack-relative-relocs.

Commits 5cf896fb6be3
("arm64: Add support for relocating the kernel with RELR relocations")
and ccb2d173b983
("Makefile: use -z pack-relative-relocs") have already added the
framework to use RELR.  We just need to wire it up and process the RELR
relocation records in relocate_relative() in addition to the RELA
relocation records.

A ".p2align 3" directive is added to la_abs macro or the BFD linker
cannot pack the relocation records against the .la_abs section (the
". = ALIGN(8);" directive in vmlinux.lds.S is too late in the linking
process).

With defconfig and CONFIG_RELR vmlinux.efi is 2.1 MiB (6%) smaller, and
vmlinuz.efi (using gzip compression) is 384 KiB (2.8%) smaller.

Link: https://groups.google.com/d/topic/generic-abi/bX460iggiKg
Link: https://reviews.llvm.org/D138135#4531389
Link: https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=d89ecf33ab6d
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
---
 arch/loongarch/Kconfig                |  1 +
 arch/loongarch/include/asm/asmmacro.h |  1 +
 arch/loongarch/include/asm/setup.h    |  5 +++++
 arch/loongarch/kernel/relocate.c      | 18 ++++++++++++++++++
 arch/loongarch/kernel/vmlinux.lds.S   |  8 ++++++++
 5 files changed, 33 insertions(+)

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index ddc042895d01..03b3ef5edd24 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -607,6 +607,7 @@ config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
 
 config RELOCATABLE
 	bool "Relocatable kernel"
+	select ARCH_HAS_RELR
 	help
 	  This builds the kernel as a Position Independent Executable (PIE),
 	  which retains all relocation metadata required, so as to relocate
diff --git a/arch/loongarch/include/asm/asmmacro.h b/arch/loongarch/include/asm/asmmacro.h
index 655db7d7a427..8d7f501b0a12 100644
--- a/arch/loongarch/include/asm/asmmacro.h
+++ b/arch/loongarch/include/asm/asmmacro.h
@@ -609,6 +609,7 @@
 	lu32i.d	\reg, 0
 	lu52i.d	\reg, \reg, 0
 	.pushsection ".la_abs", "aw", %progbits
+	.p2align 3
 	.dword	766b
 	.dword	\sym
 	.popsection
diff --git a/arch/loongarch/include/asm/setup.h b/arch/loongarch/include/asm/setup.h
index ee52fb1e9963..3c2fb16b11b6 100644
--- a/arch/loongarch/include/asm/setup.h
+++ b/arch/loongarch/include/asm/setup.h
@@ -34,6 +34,11 @@ extern long __la_abs_end;
 extern long __rela_dyn_begin;
 extern long __rela_dyn_end;
 
+#ifdef CONFIG_RELR
+extern long __relr_dyn_begin;
+extern long __relr_dyn_end;
+#endif
+
 extern unsigned long __init relocate_kernel(void);
 
 #endif
diff --git a/arch/loongarch/kernel/relocate.c b/arch/loongarch/kernel/relocate.c
index 69d73dc7326a..6abb9c91b255 100644
--- a/arch/loongarch/kernel/relocate.c
+++ b/arch/loongarch/kernel/relocate.c
@@ -37,6 +37,24 @@ static inline void __init relocate_relative(void)
 		relocated_addr = (Elf64_Addr)RELOCATED(relocated_addr);
 		*(Elf64_Addr *)RELOCATED(addr) = relocated_addr;
 	}
+
+#ifdef CONFIG_RELR
+	u64 *relr = (u64 *)&__relr_dyn_begin;
+	u64 *relr_end = (u64 *)&__relr_dyn_end;
+	u64 *addr = NULL;
+
+	for ( ; relr < relr_end; relr++) {
+		if ((*relr & 1) == 0) {
+			addr = (u64 *)(*relr + reloc_offset);
+			*addr++ += reloc_offset;
+		} else {
+			for (u64 *p = addr, r = *relr >> 1; r; p++, r >>= 1)
+				if (r & 1)
+					*p += reloc_offset;
+			addr += 63;
+		}
+	}
+#endif
 }
 
 static inline void __init relocate_absolute(long random_offset)
diff --git a/arch/loongarch/kernel/vmlinux.lds.S b/arch/loongarch/kernel/vmlinux.lds.S
index 3c7595342730..08ea921cdec1 100644
--- a/arch/loongarch/kernel/vmlinux.lds.S
+++ b/arch/loongarch/kernel/vmlinux.lds.S
@@ -113,6 +113,14 @@ SECTIONS
 		__rela_dyn_end = .;
 	}
 
+#ifdef CONFIG_RELR
+	.relr.dyn : ALIGN(8) {
+		__relr_dyn_begin = .;
+		 *(.relr.dyn)
+		__relr_dyn_end = .;
+	}
+#endif
+
 	.data.rel : { *(.data.rel*) }
 
 #ifdef CONFIG_RELOCATABLE
-- 
2.45.2


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] LoongArch: Remove a redundant checking in relocator
  2024-07-06  7:38 [PATCH 1/2] LoongArch: Remove a redundant checking in relocator Xi Ruoyao
  2024-07-06  7:38 ` [PATCH 2/2] LoongArch: Add support for relocating the kernel with RELR relocation Xi Ruoyao
@ 2024-07-06  9:44 ` Markus Elfring
  1 sibling, 0 replies; 7+ messages in thread
From: Markus Elfring @ 2024-07-06  9:44 UTC (permalink / raw)
  To: Xi Ruoyao, loongarch, llvm, Huacai Chen, Wang Xuerui
  Cc: LKML, Fangrui Song, Jinyang He, Nathan Chancellor,
	Nick Desaulniers, Tiezhu Yang, Youling Tang

> With our linker script "relocated_addr >= VMLINUX_LOAD_ADDRESS" should
> be always true.

* I find cover letters helpful for patch series.

* Under which circumstances would you care more for remaining patch review concerns
  also according to better change descriptions (or changelogs)?
  https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.10-rc6#n94


Regards,
Markus

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] LoongArch: Add support for relocating the kernel with RELR relocation
  2024-07-06  7:38 ` [PATCH 2/2] LoongArch: Add support for relocating the kernel with RELR relocation Xi Ruoyao
@ 2024-07-06 10:29   ` Huacai Chen
  2024-07-06 10:33     ` Xi Ruoyao
  0 siblings, 1 reply; 7+ messages in thread
From: Huacai Chen @ 2024-07-06 10:29 UTC (permalink / raw)
  To: Xi Ruoyao
  Cc: WANG Xuerui, Jinyang He, Youling Tang, Tiezhu Yang, Fangrui Song,
	Nathan Chancellor, Nick Desaulniers, loongarch, linux-kernel,
	llvm

Hi, Ruoyao,

On Sat, Jul 6, 2024 at 3:39 PM Xi Ruoyao <xry111@xry111.site> wrote:
>
> RELR as a relocation packing format for relative relocations for
> reducing the size of relative relocation records.  In a position
> independent executable there are often many relative relocation
> records, and our vmlinux is a PIE.
>
> The LLD linker (since 17.0.0) and the BFD linker (since 2.43) supports
> packing the relocations in the RELR format for LoongArch, with the flag
> -z pack-relative-relocs.
>
> Commits 5cf896fb6be3
> ("arm64: Add support for relocating the kernel with RELR relocations")
> and ccb2d173b983
> ("Makefile: use -z pack-relative-relocs") have already added the
> framework to use RELR.  We just need to wire it up and process the RELR
> relocation records in relocate_relative() in addition to the RELA
> relocation records.
>
> A ".p2align 3" directive is added to la_abs macro or the BFD linker
> cannot pack the relocation records against the .la_abs section (the
> ". = ALIGN(8);" directive in vmlinux.lds.S is too late in the linking
> process).
>
> With defconfig and CONFIG_RELR vmlinux.efi is 2.1 MiB (6%) smaller, and
> vmlinuz.efi (using gzip compression) is 384 KiB (2.8%) smaller.
>
> Link: https://groups.google.com/d/topic/generic-abi/bX460iggiKg
> Link: https://reviews.llvm.org/D138135#4531389
> Link: https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=d89ecf33ab6d
> Signed-off-by: Xi Ruoyao <xry111@xry111.site>
> ---
>  arch/loongarch/Kconfig                |  1 +
>  arch/loongarch/include/asm/asmmacro.h |  1 +
>  arch/loongarch/include/asm/setup.h    |  5 +++++
>  arch/loongarch/kernel/relocate.c      | 18 ++++++++++++++++++
>  arch/loongarch/kernel/vmlinux.lds.S   |  8 ++++++++
>  5 files changed, 33 insertions(+)
>
> diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
> index ddc042895d01..03b3ef5edd24 100644
> --- a/arch/loongarch/Kconfig
> +++ b/arch/loongarch/Kconfig
> @@ -607,6 +607,7 @@ config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
>
>  config RELOCATABLE
>         bool "Relocatable kernel"
> +       select ARCH_HAS_RELR
Why is this selection under RELOCATABLE? I know ARM64 is the same, but why?

Huacai

>         help
>           This builds the kernel as a Position Independent Executable (PIE),
>           which retains all relocation metadata required, so as to relocate
> diff --git a/arch/loongarch/include/asm/asmmacro.h b/arch/loongarch/include/asm/asmmacro.h
> index 655db7d7a427..8d7f501b0a12 100644
> --- a/arch/loongarch/include/asm/asmmacro.h
> +++ b/arch/loongarch/include/asm/asmmacro.h
> @@ -609,6 +609,7 @@
>         lu32i.d \reg, 0
>         lu52i.d \reg, \reg, 0
>         .pushsection ".la_abs", "aw", %progbits
> +       .p2align 3
>         .dword  766b
>         .dword  \sym
>         .popsection
> diff --git a/arch/loongarch/include/asm/setup.h b/arch/loongarch/include/asm/setup.h
> index ee52fb1e9963..3c2fb16b11b6 100644
> --- a/arch/loongarch/include/asm/setup.h
> +++ b/arch/loongarch/include/asm/setup.h
> @@ -34,6 +34,11 @@ extern long __la_abs_end;
>  extern long __rela_dyn_begin;
>  extern long __rela_dyn_end;
>
> +#ifdef CONFIG_RELR
> +extern long __relr_dyn_begin;
> +extern long __relr_dyn_end;
> +#endif
> +
>  extern unsigned long __init relocate_kernel(void);
>
>  #endif
> diff --git a/arch/loongarch/kernel/relocate.c b/arch/loongarch/kernel/relocate.c
> index 69d73dc7326a..6abb9c91b255 100644
> --- a/arch/loongarch/kernel/relocate.c
> +++ b/arch/loongarch/kernel/relocate.c
> @@ -37,6 +37,24 @@ static inline void __init relocate_relative(void)
>                 relocated_addr = (Elf64_Addr)RELOCATED(relocated_addr);
>                 *(Elf64_Addr *)RELOCATED(addr) = relocated_addr;
>         }
> +
> +#ifdef CONFIG_RELR
> +       u64 *relr = (u64 *)&__relr_dyn_begin;
> +       u64 *relr_end = (u64 *)&__relr_dyn_end;
> +       u64 *addr = NULL;
> +
> +       for ( ; relr < relr_end; relr++) {
> +               if ((*relr & 1) == 0) {
> +                       addr = (u64 *)(*relr + reloc_offset);
> +                       *addr++ += reloc_offset;
> +               } else {
> +                       for (u64 *p = addr, r = *relr >> 1; r; p++, r >>= 1)
> +                               if (r & 1)
> +                                       *p += reloc_offset;
> +                       addr += 63;
> +               }
> +       }
> +#endif
>  }
>
>  static inline void __init relocate_absolute(long random_offset)
> diff --git a/arch/loongarch/kernel/vmlinux.lds.S b/arch/loongarch/kernel/vmlinux.lds.S
> index 3c7595342730..08ea921cdec1 100644
> --- a/arch/loongarch/kernel/vmlinux.lds.S
> +++ b/arch/loongarch/kernel/vmlinux.lds.S
> @@ -113,6 +113,14 @@ SECTIONS
>                 __rela_dyn_end = .;
>         }
>
> +#ifdef CONFIG_RELR
> +       .relr.dyn : ALIGN(8) {
> +               __relr_dyn_begin = .;
> +                *(.relr.dyn)
> +               __relr_dyn_end = .;
> +       }
> +#endif
> +
>         .data.rel : { *(.data.rel*) }
>
>  #ifdef CONFIG_RELOCATABLE
> --
> 2.45.2
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] LoongArch: Add support for relocating the kernel with RELR relocation
  2024-07-06 10:29   ` Huacai Chen
@ 2024-07-06 10:33     ` Xi Ruoyao
  2024-07-06 13:49       ` Huacai Chen
  0 siblings, 1 reply; 7+ messages in thread
From: Xi Ruoyao @ 2024-07-06 10:33 UTC (permalink / raw)
  To: Huacai Chen
  Cc: WANG Xuerui, Jinyang He, Youling Tang, Tiezhu Yang, Fangrui Song,
	Nathan Chancellor, Nick Desaulniers, loongarch, linux-kernel,
	llvm

On Sat, 2024-07-06 at 18:29 +0800, Huacai Chen wrote:

/* snip */

> > diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
> > index ddc042895d01..03b3ef5edd24 100644
> > --- a/arch/loongarch/Kconfig
> > +++ b/arch/loongarch/Kconfig
> > @@ -607,6 +607,7 @@ config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
> > 
> >  config RELOCATABLE
> >         bool "Relocatable kernel"
> > +       select ARCH_HAS_RELR
> Why is this selection under RELOCATABLE? I know ARM64 is the same, but
> why?

Because if we just select it in CONFIG_LOONGARCH instead of
CONFIG_RELOCATABLE, the users who have disabled CONFIG_RELOCATABLE will
still see the entry for RELR in their configuration interface.  And
they'll ask "hey what's this for?  Why my kernel needs relocation?"

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] LoongArch: Add support for relocating the kernel with RELR relocation
  2024-07-06 10:33     ` Xi Ruoyao
@ 2024-07-06 13:49       ` Huacai Chen
  2024-07-06 14:46         ` Xi Ruoyao
  0 siblings, 1 reply; 7+ messages in thread
From: Huacai Chen @ 2024-07-06 13:49 UTC (permalink / raw)
  To: Xi Ruoyao
  Cc: WANG Xuerui, Jinyang He, Youling Tang, Tiezhu Yang, Fangrui Song,
	Nathan Chancellor, Nick Desaulniers, loongarch, linux-kernel,
	llvm

On Sat, Jul 6, 2024 at 6:34 PM Xi Ruoyao <xry111@xry111.site> wrote:
>
> On Sat, 2024-07-06 at 18:29 +0800, Huacai Chen wrote:
>
> /* snip */
>
> > > diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
> > > index ddc042895d01..03b3ef5edd24 100644
> > > --- a/arch/loongarch/Kconfig
> > > +++ b/arch/loongarch/Kconfig
> > > @@ -607,6 +607,7 @@ config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
> > >
> > >  config RELOCATABLE
> > >         bool "Relocatable kernel"
> > > +       select ARCH_HAS_RELR
> > Why is this selection under RELOCATABLE? I know ARM64 is the same, but
> > why?
>
> Because if we just select it in CONFIG_LOONGARCH instead of
> CONFIG_RELOCATABLE, the users who have disabled CONFIG_RELOCATABLE will
> still see the entry for RELR in their configuration interface.  And
> they'll ask "hey what's this for?  Why my kernel needs relocation?"
RELR is not a similar conception to RELA, it is only used for
relocatable kernel?

Huacai

>
> --
> Xi Ruoyao <xry111@xry111.site>
> School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] LoongArch: Add support for relocating the kernel with RELR relocation
  2024-07-06 13:49       ` Huacai Chen
@ 2024-07-06 14:46         ` Xi Ruoyao
  0 siblings, 0 replies; 7+ messages in thread
From: Xi Ruoyao @ 2024-07-06 14:46 UTC (permalink / raw)
  To: Huacai Chen
  Cc: WANG Xuerui, Jinyang He, Youling Tang, Tiezhu Yang, Fangrui Song,
	Nathan Chancellor, Nick Desaulniers, loongarch, linux-kernel,
	llvm

On Sat, 2024-07-06 at 21:49 +0800, Huacai Chen wrote:
> On Sat, Jul 6, 2024 at 6:34 PM Xi Ruoyao <xry111@xry111.site> wrote:
> > 
> > On Sat, 2024-07-06 at 18:29 +0800, Huacai Chen wrote:
> > 
> > /* snip */
> > 
> > > > diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
> > > > index ddc042895d01..03b3ef5edd24 100644
> > > > --- a/arch/loongarch/Kconfig
> > > > +++ b/arch/loongarch/Kconfig
> > > > @@ -607,6 +607,7 @@ config
> > > > ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
> > > > 
> > > >  config RELOCATABLE
> > > >         bool "Relocatable kernel"
> > > > +       select ARCH_HAS_RELR
> > > Why is this selection under RELOCATABLE? I know ARM64 is the same,
> > > but
> > > why?
> > 
> > Because if we just select it in CONFIG_LOONGARCH instead of
> > CONFIG_RELOCATABLE, the users who have disabled CONFIG_RELOCATABLE
> > will
> > still see the entry for RELR in their configuration interface.  And
> > they'll ask "hey what's this for?  Why my kernel needs relocation?"
> RELR is not a similar conception to RELA, it is only used for
> relocatable kernel?

It replaces R_LARCH_RELATIVE relocs in RELA (basically represent the
same info in a much more compact form).  For non-relocatable kernel both
RELR and RELA shouldn't exist.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-07-06 14:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-06  7:38 [PATCH 1/2] LoongArch: Remove a redundant checking in relocator Xi Ruoyao
2024-07-06  7:38 ` [PATCH 2/2] LoongArch: Add support for relocating the kernel with RELR relocation Xi Ruoyao
2024-07-06 10:29   ` Huacai Chen
2024-07-06 10:33     ` Xi Ruoyao
2024-07-06 13:49       ` Huacai Chen
2024-07-06 14:46         ` Xi Ruoyao
2024-07-06  9:44 ` [PATCH 1/2] LoongArch: Remove a redundant checking in relocator Markus Elfring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®