Skip to contentSkip to conversation

LoongArch: Allow building with kcov

PATCH Superseded Feiyang Chen 4 participants 8 messages Jul 4, 2023 – Sep 10, 2023 LoongArch

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover LoongArch: Allow building with kcov

Provide kaslr_offset() and allow building with kcov.

Feiyang Chen (2):
LoongArch: relocatable: Provide kaslr_offset() to get the kernel
offset
LoongArch: Allow building with kcov coverage

 arch/loongarch/Kconfig             |  1 +
 arch/loongarch/include/asm/setup.h |  6 ++++++
 arch/loongarch/kernel/relocate.c   | 18 ++++++++----------
 arch/loongarch/kernel/setup.c      |  3 +++
 arch/loongarch/vdso/Makefile       |  2 ++
 5 files changed, 20 insertions(+), 10 deletions(-)
Signature · 1 line
2.39.3
#1 lore raw

PATCH 1/2 LoongArch: relocatable: Provide kaslr_offset() to get the kernel offset

Provide kaslr_offset() to get the kernel offset when KASLR is enabled. Rename reloc_offset to __reloc_offset and export it.

---
 arch/loongarch/include/asm/setup.h |  6 ++++++
 arch/loongarch/kernel/relocate.c   | 18 ++++++++----------
 arch/loongarch/kernel/setup.c      |  3 +++
 3 files changed, 17 insertions(+), 10 deletions(-)
arch/loongarch/include/asm/setup.h +6 -0
index 2dca0d1dd90a..39f9964bbdd4 100644
@@ -37,4 +37,10 @@ extern unsigned long __init relocate_kernel(void);
 
 #endif
 
+extern unsigned long __reloc_offset;
+static inline unsigned long kaslr_offset(void)
+{
+	return __reloc_offset;
+}
+
 #endif /* __SETUP_H */
arch/loongarch/kernel/relocate.c +8 -10
index 6c3eff9af9fb..9ba560d514e1 100644
@@ -16,11 +16,9 @@
 #include <asm/sections.h>
 #include <asm/setup.h>
 
-#define RELOCATED(x) ((void *)((long)x + reloc_offset))
+#define RELOCATED(x) ((void *)((long)x + __reloc_offset))
 #define RELOCATED_KASLR(x) ((void *)((long)x + random_offset))
 
-static unsigned long reloc_offset;
-
 static inline void __init relocate_relative(void)
 {
 	Elf64_Rela *rela, *rela_end;
@@ -154,7 +152,7 @@ static inline void __init update_reloc_offset(unsigned long *addr, long random_o
 {
 	unsigned long *new_addr = (unsigned long *)RELOCATED_KASLR(addr);
 
-	*new_addr = (unsigned long)reloc_offset;
+	*new_addr = (unsigned long)__reloc_offset;
 }
 
 unsigned long __init relocate_kernel(void)
@@ -173,7 +171,7 @@ unsigned long __init relocate_kernel(void)
 	if (relocation_addr_valid(location_new))
 		random_offset = (unsigned long)location_new - (unsigned long)(_text);
 #endif
-	reloc_offset = (unsigned long)_text - VMLINUX_LOAD_ADDRESS;
+	__reloc_offset = (unsigned long)_text - VMLINUX_LOAD_ADDRESS;
 
 	if (random_offset) {
 		kernel_length = (long)(_end) - (long)(_text);
@@ -187,15 +185,15 @@ unsigned long __init relocate_kernel(void)
 			"dbar 0 \t\n"
 			::: "memory");
 
-		reloc_offset += random_offset;
+		__reloc_offset += random_offset;
 
 		/* The current thread is now within the relocated kernel */
 		__current_thread_info = RELOCATED_KASLR(__current_thread_info);
 
-		update_reloc_offset(&reloc_offset, random_offset);
+		update_reloc_offset(&__reloc_offset, random_offset);
 	}
 
-	if (reloc_offset)
+	if (__reloc_offset)
 		relocate_relative();
 
 	relocate_absolute(random_offset);
@@ -208,9 +206,9 @@ unsigned long __init relocate_kernel(void)
  */
 static void show_kernel_relocation(const char *level)
 {
-	if (reloc_offset > 0) {
+	if (__reloc_offset > 0) {
 		printk(level);
-		pr_cont("Kernel relocated by 0x%lx\n", reloc_offset);
+		pr_cont("Kernel relocated by 0x%lx\n", __reloc_offset);
 		pr_cont(" .text @ 0x%px\n", _text);
 		pr_cont(" .data @ 0x%px\n", _sdata);
 		pr_cont(" .bss  @ 0x%px\n", __bss_start);
arch/loongarch/kernel/setup.c +3 -0
index 95e6b579dfdd..d7bda711824f 100644
@@ -65,6 +65,9 @@ struct cpuinfo_loongarch cpu_data[NR_CPUS] __read_mostly;
 
 EXPORT_SYMBOL(cpu_data);
 
+unsigned long __reloc_offset __ro_after_init;
+EXPORT_SYMBOL(__reloc_offset);
+
 struct loongson_board_info b_info;
 static const char dmi_empty_string[] = "        ";
 
Signature · 1 line
2.39.3
#3 lore raw

PATCH 2/2 LoongArch: Allow building with kcov coverage

In reply to Feiyang Chen

Add ARCH_HAS_KCOV to the LoongArch Kconfig. Also disable instrumentation of vdso.

---
 arch/loongarch/Kconfig       | 1 +
 arch/loongarch/vdso/Makefile | 2 ++
 2 files changed, 3 insertions(+)
arch/loongarch/Kconfig +1 -0
index ed9a148cdcde..4c21a961ab88 100644
@@ -14,6 +14,7 @@ config LOONGARCH
 	select ARCH_HAS_ACPI_TABLE_UPGRADE	if ACPI
 	select ARCH_HAS_CPU_FINALIZE_INIT
 	select ARCH_HAS_FORTIFY_SOURCE
+	select ARCH_HAS_KCOV
 	select ARCH_HAS_NMI_SAFE_THIS_CPU_OPS
 	select ARCH_HAS_PTE_SPECIAL
 	select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
arch/loongarch/vdso/Makefile +2 -0
index 7bb794604af3..7dc87377688b 100644
@@ -5,6 +5,8 @@ ifdef CONFIG_KASAN
 KASAN_SANITIZE := n
 endif
 
+KCOV_INSTRUMENT := n
+
 # Include the generic Makefile to check the built vdso.
 include $(srctree)/lib/vdso/Makefile
 
Signature · 1 line
2.39.3
maintainer
#2 lore raw

Re: [PATCH 1/2] LoongArch: relocatable: Provide kaslr_offset() to get the kernel offset

In reply to Feiyang Chen

Hi, Feiyang,

Quoted 16 lines · Feiyang Chen

Provide kaslr_offset() to get the kernel offset when KASLR is enabled. Rename reloc_offset to __reloc_offset and export it.

---
 arch/loongarch/include/asm/setup.h |  6 ++++++
 arch/loongarch/kernel/relocate.c   | 18 ++++++++----------
 arch/loongarch/kernel/setup.c      |  3 +++
 3 files changed, 17 insertions(+), 10 deletions(-)
arch/loongarch/include/asm/setup.h +0 -0
index 2dca0d1dd90a..39f9964bbdd4 100644

arch/loongarch/include/asm/setup.h extern unsigned long __init relocate_kernel(void); · line 43

Quoted 7 lines
arch/loongarch/include/asm/setup.h +4 -0
@@ -37,4 +37,10 @@ extern unsigned long __init relocate_kernel(void);
 
 #endif
 
+extern unsigned long __reloc_offset;
+static inline unsigned long kaslr_offset(void)
+{
+       return __reloc_offset;

I doubt that we should return random_offset here.

Huacai

Quoted 86 lines
arch/loongarch/include/asm/setup.h +2 -0
+}
+
 #endif /* __SETUP_H */
arch/loongarch/kernel/relocate.c +8 -10
index 6c3eff9af9fb..9ba560d514e1 100644
@@ -16,11 +16,9 @@
 #include <asm/sections.h>
 #include <asm/setup.h>
 
-#define RELOCATED(x) ((void *)((long)x + reloc_offset))
+#define RELOCATED(x) ((void *)((long)x + __reloc_offset))
 #define RELOCATED_KASLR(x) ((void *)((long)x + random_offset))
 
-static unsigned long reloc_offset;
-
 static inline void __init relocate_relative(void)
 {
        Elf64_Rela *rela, *rela_end;
@@ -154,7 +152,7 @@ static inline void __init update_reloc_offset(unsigned long *addr, long random_o
 {
        unsigned long *new_addr = (unsigned long *)RELOCATED_KASLR(addr);
 
-       *new_addr = (unsigned long)reloc_offset;
+       *new_addr = (unsigned long)__reloc_offset;
 }
 
 unsigned long __init relocate_kernel(void)
@@ -173,7 +171,7 @@ unsigned long __init relocate_kernel(void)
        if (relocation_addr_valid(location_new))
                random_offset = (unsigned long)location_new - (unsigned long)(_text);
 #endif
-       reloc_offset = (unsigned long)_text - VMLINUX_LOAD_ADDRESS;
+       __reloc_offset = (unsigned long)_text - VMLINUX_LOAD_ADDRESS;
 
        if (random_offset) {
                kernel_length = (long)(_end) - (long)(_text);
@@ -187,15 +185,15 @@ unsigned long __init relocate_kernel(void)
                        "dbar 0 \t\n"
                        ::: "memory");
 
-               reloc_offset += random_offset;
+               __reloc_offset += random_offset;
 
                /* The current thread is now within the relocated kernel */
                __current_thread_info = RELOCATED_KASLR(__current_thread_info);
 
-               update_reloc_offset(&reloc_offset, random_offset);
+               update_reloc_offset(&__reloc_offset, random_offset);
        }
 
-       if (reloc_offset)
+       if (__reloc_offset)
                relocate_relative();
 
        relocate_absolute(random_offset);
@@ -208,9 +206,9 @@ unsigned long __init relocate_kernel(void)
  */
 static void show_kernel_relocation(const char *level)
 {
-       if (reloc_offset > 0) {
+       if (__reloc_offset > 0) {
                printk(level);
-               pr_cont("Kernel relocated by 0x%lx\n", reloc_offset);
+               pr_cont("Kernel relocated by 0x%lx\n", __reloc_offset);
                pr_cont(" .text @ 0x%px\n", _text);
                pr_cont(" .data @ 0x%px\n", _sdata);
                pr_cont(" .bss  @ 0x%px\n", __bss_start);
arch/loongarch/kernel/setup.c +3 -1
index 95e6b579dfdd..d7bda711824f 100644
@@ -65,6 +65,9 @@ struct cpuinfo_loongarch cpu_data[NR_CPUS] __read_mostly;
 
 EXPORT_SYMBOL(cpu_data);
 
+unsigned long __reloc_offset __ro_after_init;
+EXPORT_SYMBOL(__reloc_offset);
+
 struct loongson_board_info b_info;
 static const char dmi_empty_string[] = "        ";
 
--

2.39.3

maintainer
#4 lore raw

Re: [PATCH 2/2] LoongArch: Allow building with kcov coverage

In reply to Feiyang Chen

Hi, Feiyang,

Please update Documentation/features/debug/kcov/arch-support.txt, thanks.

Huacai

Quoted 38 lines · Feiyang Chen

Add ARCH_HAS_KCOV to the LoongArch Kconfig. Also disable instrumentation of vdso.

---
 arch/loongarch/Kconfig       | 1 +
 arch/loongarch/vdso/Makefile | 2 ++
 2 files changed, 3 insertions(+)
arch/loongarch/Kconfig +1 -0
index ed9a148cdcde..4c21a961ab88 100644
@@ -14,6 +14,7 @@ config LOONGARCH
        select ARCH_HAS_ACPI_TABLE_UPGRADE      if ACPI
        select ARCH_HAS_CPU_FINALIZE_INIT
        select ARCH_HAS_FORTIFY_SOURCE
+       select ARCH_HAS_KCOV
        select ARCH_HAS_NMI_SAFE_THIS_CPU_OPS
        select ARCH_HAS_PTE_SPECIAL
        select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
arch/loongarch/vdso/Makefile +2 -1
index 7bb794604af3..7dc87377688b 100644
@@ -5,6 +5,8 @@ ifdef CONFIG_KASAN
 KASAN_SANITIZE := n
 endif
 
+KCOV_INSTRUMENT := n
+
 # Include the generic Makefile to check the built vdso.
 include $(srctree)/lib/vdso/Makefile
 
--

2.39.3

#5 lore raw

Re: [PATCH 2/2] LoongArch: Allow building with kcov coverage

In reply to Feiyang Chen

Hi,

Quoted 4 lines · Feiyang Chen

Add ARCH_HAS_KCOV to the LoongArch Kconfig. Also disable instrumentation of vdso.

When trying to build loongarch:allmodconfig, this patch results in

Error log:
In file included from /opt/kernel/gcc-12.2.0-2.39-nolibc/loongarch64-linux-gnu/bin/../lib/gcc/loongarch64-linux-gnu/12.2.0/plugin/include/options.h:8,

                 from /opt/kernel/gcc-12.2.0-2.39-nolibc/loongarch64-linux-gnu/bin/../lib/gcc/loongarch64-linux-gnu/12.2.0/plugin/include/tm.h:46,
                 from /opt/kernel/gcc-12.2.0-2.39-nolibc/loongarch64-linux-gnu/bin/../lib/gcc/loongarch64-linux-gnu/12.2.0/plugin/include/backend.h:28,
                 from /opt/kernel/gcc-12.2.0-2.39-nolibc/loongarch64-linux-gnu/bin/../lib/gcc/loongarch64-linux-gnu/12.2.0/plugin/include/gcc-plugin.h:30,
                 from scripts/gcc-plugins/gcc-common.h:7,
                 from scripts/gcc-plugins/latent_entropy_plugin.c:78:

/opt/kernel/gcc-12.2.0-2.39-nolibc/loongarch64-linux-gnu/bin/../lib/gcc/loongarch64-linux-gnu/12.2.0/plugin/include/config/loongarch/loongarch-opts.h:31:10: fatal error: loongarch-def.h: No such file or directory
31 | #include "loongarch-def.h"

for me. I tried with gcc 12.2 / binutils 2.39 and gcc 13.1 / binutils 2.40.

Reverting the patch or explicitly disabling CONFIG_GCC_PLUGINS fixes the problem.

What compiler / binutils version combination is needed for this to work, or, alternatively, how would I have to configure the compiler ?

Thanks,
Guenter

Quoted 33 lines
---
 arch/loongarch/Kconfig       | 1 +
 arch/loongarch/vdso/Makefile | 2 ++
 2 files changed, 3 insertions(+)
arch/loongarch/Kconfig +1 -0
index ed9a148cdcde..4c21a961ab88 100644
@@ -14,6 +14,7 @@ config LOONGARCH
 	select ARCH_HAS_ACPI_TABLE_UPGRADE	if ACPI
 	select ARCH_HAS_CPU_FINALIZE_INIT
 	select ARCH_HAS_FORTIFY_SOURCE
+	select ARCH_HAS_KCOV
 	select ARCH_HAS_NMI_SAFE_THIS_CPU_OPS
 	select ARCH_HAS_PTE_SPECIAL
 	select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
arch/loongarch/vdso/Makefile +2 -0
index 7bb794604af3..7dc87377688b 100644
@@ -5,6 +5,8 @@ ifdef CONFIG_KASAN
 KASAN_SANITIZE := n
 endif
 
+KCOV_INSTRUMENT := n
+
 # Include the generic Makefile to check the built vdso.
 include $(srctree)/lib/vdso/Makefile
 
Signature · 1 line
2.39.3
#6 lore raw

Re: [PATCH 2/2] LoongArch: Allow building with kcov coverage

Quoted 20 lines · Guenter Roeck

Hi,

Quoted 4 lines · Feiyang Chen

Add ARCH_HAS_KCOV to the LoongArch Kconfig. Also disable instrumentation of vdso.

When trying to build loongarch:allmodconfig, this patch results in

Error log:
In file included from /opt/kernel/gcc-12.2.0-2.39-nolibc/loongarch64-linux-gnu/bin/../lib/gcc/loongarch64-linux-gnu/12.2.0/plugin/include/options.h:8,
                 from /opt/kernel/gcc-12.2.0-2.39-nolibc/loongarch64-linux-gnu/bin/../lib/gcc/loongarch64-linux-gnu/12.2.0/plugin/include/tm.h:46,
                 from /opt/kernel/gcc-12.2.0-2.39-nolibc/loongarch64-linux-gnu/bin/../lib/gcc/loongarch64-linux-gnu/12.2.0/plugin/include/backend.h:28,
                 from /opt/kernel/gcc-12.2.0-2.39-nolibc/loongarch64-linux-gnu/bin/../lib/gcc/loongarch64-linux-gnu/12.2.0/plugin/include/gcc-plugin.h:30,
                 from scripts/gcc-plugins/gcc-common.h:7,
                 from scripts/gcc-plugins/latent_entropy_plugin.c:78:
/opt/kernel/gcc-12.2.0-2.39-nolibc/loongarch64-linux-gnu/bin/../lib/gcc/loongarch64-linux-gnu/12.2.0/plugin/include/config/loongarch/loongarch-opts.h:31:10: fatal error: loongarch-def.h: No such file or directory 31 | #include "loongarch-def.h"

Quoted 7 lines

for me. I tried with gcc 12.2 / binutils 2.39 and gcc 13.1 / binutils 2.40.

Reverting the patch or explicitly disabling CONFIG_GCC_PLUGINS fixes the problem.

What compiler / binutils version combination is needed for this to work, or, alternatively, how would I have to configure the compiler ?

Hi Guenter,

This is a GCC bug. It's fixed in GCC trunk and the fix has been backported to 12/13 release branches, so GCC 14.1, 13.3, and 12.4 will contain the fix.

The fix is available at https://gcc.gnu.org/r14-3331, you can apply the patch building the compiler.

Sorry for the inconvenience.

Signature · 2 lines
Xi Ruoyao <[email protected]>
School of Aerospace Science and Technology, Xidian University
#7 lore raw

Re: [PATCH 2/2] LoongArch: Allow building with kcov coverage

Quoted 31 lines · Xi Ruoyao
Quoted 20 lines · Guenter Roeck

Hi,

Quoted 4 lines · Feiyang Chen

Add ARCH_HAS_KCOV to the LoongArch Kconfig. Also disable instrumentation of vdso.

When trying to build loongarch:allmodconfig, this patch results in

Error log:
In file included from /opt/kernel/gcc-12.2.0-2.39-nolibc/loongarch64-linux-gnu/bin/../lib/gcc/loongarch64-linux-gnu/12.2.0/plugin/include/options.h:8,
                 from /opt/kernel/gcc-12.2.0-2.39-nolibc/loongarch64-linux-gnu/bin/../lib/gcc/loongarch64-linux-gnu/12.2.0/plugin/include/tm.h:46,
                 from /opt/kernel/gcc-12.2.0-2.39-nolibc/loongarch64-linux-gnu/bin/../lib/gcc/loongarch64-linux-gnu/12.2.0/plugin/include/backend.h:28,
                 from /opt/kernel/gcc-12.2.0-2.39-nolibc/loongarch64-linux-gnu/bin/../lib/gcc/loongarch64-linux-gnu/12.2.0/plugin/include/gcc-plugin.h:30,
                 from scripts/gcc-plugins/gcc-common.h:7,
                 from scripts/gcc-plugins/latent_entropy_plugin.c:78:
/opt/kernel/gcc-12.2.0-2.39-nolibc/loongarch64-linux-gnu/bin/../lib/gcc/loongarch64-linux-gnu/12.2.0/plugin/include/config/loongarch/loongarch-opts.h:31:10: fatal error: loongarch-def.h: No such file or directory
   31 | #include "loongarch-def.h"

Quoted 7 lines

for me. I tried with gcc 12.2 / binutils 2.39 and gcc 13.1 / binutils 2.40.

Reverting the patch or explicitly disabling CONFIG_GCC_PLUGINS fixes the problem.

What compiler / binutils version combination is needed for this to work, or, alternatively, how would I have to configure the compiler ?

Hi Guenter,

Quoted 9 lines

This is a GCC bug. It's fixed in GCC trunk and the fix has been backported to 12/13 release branches, so GCC 14.1, 13.3, and 12.4 will contain the fix.

The fix is available at https://gcc.gnu.org/r14-3331, you can apply the patch building the compiler.

Sorry for the inconvenience.

Thanks for the information. I'll add a note to my builders and just disable gcc plugins for now until the new compiler versions are available.

Guenter