All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/1] riscv: Allow to downgrade paging mode from the command line
@ 2023-01-31 15:11 ` Alexandre Ghiti
  0 siblings, 0 replies; 16+ messages in thread
From: Alexandre Ghiti @ 2023-01-31 15:11 UTC (permalink / raw)
  To: Jonathan Corbet, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Conor Dooley, Ard Biesheuvel, linux-doc, linux-kernel,
	linux-riscv
  Cc: Alexandre Ghiti

This new version gets rid of the limitation that prevented KASAN kernels
to use the newly introduced parameters.

While looking into KASLR, I fell onto commit aacd149b6238 ("arm64: head:
avoid relocating the kernel twice for KASLR"): it allows to use the fdt
functions very early in the boot process with KASAN enabled by simply
compiling a new version of those functions without instrumentation.

I had to change the handling of the command line parsing to make the
code self-contained in kernel/pi/cmd_early.c to avoid calling too many
__pi prefixed functions from outside this file.

I'll use this approach like arm64 to handle the extraction of the random
seedi from the device tree for KASLR.

@Ard: I copied the "pi" name but I have to admit I don't know what it
means.

Alexandre Ghiti (1):
  riscv: Allow to downgrade paging mode from the command line

 .../admin-guide/kernel-parameters.txt         |  5 +-
 arch/riscv/kernel/Makefile                    |  2 +
 arch/riscv/kernel/pi/Makefile                 | 34 ++++++++++++
 arch/riscv/kernel/pi/cmdline_early.c          | 52 +++++++++++++++++++
 arch/riscv/lib/memcpy.S                       |  2 +
 arch/riscv/lib/memmove.S                      |  2 +
 arch/riscv/mm/init.c                          | 36 ++++++++++---
 7 files changed, 126 insertions(+), 7 deletions(-)
 create mode 100644 arch/riscv/kernel/pi/Makefile
 create mode 100644 arch/riscv/kernel/pi/cmdline_early.c

-- 
2.37.2


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* [PATCH v4 0/1] riscv: Allow to downgrade paging mode from the command line
@ 2023-01-31 15:11 ` Alexandre Ghiti
  0 siblings, 0 replies; 16+ messages in thread
From: Alexandre Ghiti @ 2023-01-31 15:11 UTC (permalink / raw)
  To: Jonathan Corbet, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Conor Dooley, Ard Biesheuvel, linux-doc, linux-kernel,
	linux-riscv
  Cc: Alexandre Ghiti

This new version gets rid of the limitation that prevented KASAN kernels
to use the newly introduced parameters.

While looking into KASLR, I fell onto commit aacd149b6238 ("arm64: head:
avoid relocating the kernel twice for KASLR"): it allows to use the fdt
functions very early in the boot process with KASAN enabled by simply
compiling a new version of those functions without instrumentation.

I had to change the handling of the command line parsing to make the
code self-contained in kernel/pi/cmd_early.c to avoid calling too many
__pi prefixed functions from outside this file.

I'll use this approach like arm64 to handle the extraction of the random
seedi from the device tree for KASLR.

@Ard: I copied the "pi" name but I have to admit I don't know what it
means.

Alexandre Ghiti (1):
  riscv: Allow to downgrade paging mode from the command line

 .../admin-guide/kernel-parameters.txt         |  5 +-
 arch/riscv/kernel/Makefile                    |  2 +
 arch/riscv/kernel/pi/Makefile                 | 34 ++++++++++++
 arch/riscv/kernel/pi/cmdline_early.c          | 52 +++++++++++++++++++
 arch/riscv/lib/memcpy.S                       |  2 +
 arch/riscv/lib/memmove.S                      |  2 +
 arch/riscv/mm/init.c                          | 36 ++++++++++---
 7 files changed, 126 insertions(+), 7 deletions(-)
 create mode 100644 arch/riscv/kernel/pi/Makefile
 create mode 100644 arch/riscv/kernel/pi/cmdline_early.c

-- 
2.37.2


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

* [PATCH v4 1/1] riscv: Allow to downgrade paging mode from the command line
  2023-01-31 15:11 ` Alexandre Ghiti
@ 2023-01-31 15:11   ` Alexandre Ghiti
  -1 siblings, 0 replies; 16+ messages in thread
From: Alexandre Ghiti @ 2023-01-31 15:11 UTC (permalink / raw)
  To: Jonathan Corbet, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Conor Dooley, Ard Biesheuvel, linux-doc, linux-kernel,
	linux-riscv
  Cc: Alexandre Ghiti

Add 2 early command line parameters that allow to downgrade satp mode
(using the same naming as x86):
- "no5lvl": use a 4-level page table (down from sv57 to sv48)
- "no4lvl": use a 3-level page table (down from sv57/sv48 to sv39)

Note that going through the device tree to get the kernel command line
works with ACPI too since the efi stub creates a device tree anyway with
the command line.

In KASAN kernels, we can't use the libfdt that early in the boot process
since we are not ready to execute instrumented functions. So instead of
using the "generic" libfdt, we compile our own versions of those functions
that are not instrumented and that are prefixed so that they do not
conflict with the generic ones. We also need the non-instrumented versions
of the string functions and the prefixed versions of memcpy/memmove.

This is largely inspired by commit aacd149b6238 ("arm64: head: avoid
relocating the kernel twice for KASLR") from which I removed compilation
flags that were not relevant to RISC-V at the moment (LTO, SCS, pie).

Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
---
 .../admin-guide/kernel-parameters.txt         |  5 +-
 arch/riscv/kernel/Makefile                    |  2 +
 arch/riscv/kernel/pi/Makefile                 | 34 ++++++++++++
 arch/riscv/kernel/pi/cmdline_early.c          | 52 +++++++++++++++++++
 arch/riscv/lib/memcpy.S                       |  2 +
 arch/riscv/lib/memmove.S                      |  2 +
 arch/riscv/mm/init.c                          | 36 ++++++++++---
 7 files changed, 126 insertions(+), 7 deletions(-)
 create mode 100644 arch/riscv/kernel/pi/Makefile
 create mode 100644 arch/riscv/kernel/pi/cmdline_early.c

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 6cfa6e3996cf..1d8fc66d2b0d 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3578,7 +3578,10 @@
 			emulation library even if a 387 maths coprocessor
 			is present.
 
-	no5lvl		[X86-64] Disable 5-level paging mode. Forces
+	no4lvl		[RISCV] Disable 4-level and 5-level paging modes. Forces
+			kernel to use 3-level paging instead.
+
+	no5lvl		[X86-64,RISCV] Disable 5-level paging mode. Forces
 			kernel to use 4-level paging instead.
 
 	nofsgsbase	[X86] Disables FSGSBASE instructions.
diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
index 4cf303a779ab..6756209f1ac6 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -89,3 +89,5 @@ obj-$(CONFIG_EFI)		+= efi.o
 obj-$(CONFIG_COMPAT)		+= compat_syscall_table.o
 obj-$(CONFIG_COMPAT)		+= compat_signal.o
 obj-$(CONFIG_COMPAT)		+= compat_vdso/
+
+obj-y				+= pi/
diff --git a/arch/riscv/kernel/pi/Makefile b/arch/riscv/kernel/pi/Makefile
new file mode 100644
index 000000000000..554e936ef0b6
--- /dev/null
+++ b/arch/riscv/kernel/pi/Makefile
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: GPL-2.0
+# This file was copied from arm64/kernel/pi/Makefile.
+
+KBUILD_CFLAGS	:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
+		   -Os -DDISABLE_BRANCH_PROFILING $(DISABLE_STACKLEAK_PLUGIN) \
+		   $(call cc-option,-mbranch-protection=none) \
+		   -I$(srctree)/scripts/dtc/libfdt -fno-stack-protector \
+		   -D__DISABLE_EXPORTS -ffreestanding \
+		   -fno-asynchronous-unwind-tables -fno-unwind-tables \
+		   $(call cc-option,-fno-addrsig)
+
+GCOV_PROFILE	:= n
+KASAN_SANITIZE	:= n
+KCSAN_SANITIZE	:= n
+UBSAN_SANITIZE	:= n
+KCOV_INSTRUMENT	:= n
+
+$(obj)/%.pi.o: OBJCOPYFLAGS := --prefix-symbols=__pi_ \
+			       --remove-section=.note.gnu.property \
+			       --prefix-alloc-sections=.init
+$(obj)/%.pi.o: $(obj)/%.o FORCE
+	$(call if_changed,objcopy)
+
+$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
+	$(call if_changed_rule,cc_o_c)
+
+$(obj)/string.o: $(srctree)/lib/string.c FORCE
+	$(call if_changed_rule,cc_o_c)
+
+$(obj)/ctype.o: $(srctree)/lib/ctype.c FORCE
+	$(call if_changed_rule,cc_o_c)
+
+obj-y		:= cmdline_early.pi.o string.pi.o ctype.pi.o lib-fdt.pi.o lib-fdt_ro.pi.o
+extra-y		:= $(patsubst %.pi.o,%.o,$(obj-y))
diff --git a/arch/riscv/kernel/pi/cmdline_early.c b/arch/riscv/kernel/pi/cmdline_early.c
new file mode 100644
index 000000000000..5ae7b853fa66
--- /dev/null
+++ b/arch/riscv/kernel/pi/cmdline_early.c
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <linux/types.h>
+#include <linux/init.h>
+#include <linux/libfdt.h>
+#include <linux/string.h>
+#include <asm/pgtable.h>
+#include <asm/setup.h>
+
+static char early_cmdline[COMMAND_LINE_SIZE] __initdata;
+
+static char * __init get_early_cmdline(uintptr_t dtb_pa)
+{
+	const char *fdt_cmdline = NULL;
+	unsigned int fdt_cmdline_size = 0;
+	int chosen_node;
+
+	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
+		chosen_node = fdt_path_offset((void *)dtb_pa, "/chosen");
+		if (chosen_node >= 0) {
+			fdt_cmdline = fdt_getprop((void *)dtb_pa, chosen_node,
+						  "bootargs", NULL);
+			fdt_cmdline_size = strlen(fdt_cmdline);
+			strscpy(early_cmdline, fdt_cmdline, COMMAND_LINE_SIZE);
+		}
+	}
+
+	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
+	    IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
+	    fdt_cmdline_size == 0 /* CONFIG_CMDLINE_FALLBACK */) {
+		strncat(early_cmdline, CONFIG_CMDLINE,
+			COMMAND_LINE_SIZE - fdt_cmdline_size);
+	}
+
+	return early_cmdline;
+}
+
+static u64 __init match_noXlvl(char *cmdline)
+{
+	if (strstr(cmdline, "no4lvl"))
+		return SATP_MODE_48;
+	else if (strstr(cmdline, "no5lvl"))
+		return SATP_MODE_57;
+
+	return 0;
+}
+
+u64 __init set_satp_mode_from_cmdline(uintptr_t dtb_pa)
+{
+	char *cmdline = get_early_cmdline(dtb_pa);
+
+	return match_noXlvl(cmdline);
+}
diff --git a/arch/riscv/lib/memcpy.S b/arch/riscv/lib/memcpy.S
index 51ab716253fa..1a40d01a9543 100644
--- a/arch/riscv/lib/memcpy.S
+++ b/arch/riscv/lib/memcpy.S
@@ -106,3 +106,5 @@ WEAK(memcpy)
 6:
 	ret
 END(__memcpy)
+SYM_FUNC_ALIAS(__pi_memcpy, __memcpy)
+SYM_FUNC_ALIAS(__pi___memcpy, __memcpy)
diff --git a/arch/riscv/lib/memmove.S b/arch/riscv/lib/memmove.S
index e0609e1f0864..838ff2022fe3 100644
--- a/arch/riscv/lib/memmove.S
+++ b/arch/riscv/lib/memmove.S
@@ -314,3 +314,5 @@ return_from_memmove:
 
 SYM_FUNC_END(memmove)
 SYM_FUNC_END(__memmove)
+SYM_FUNC_ALIAS(__pi_memmove, __memmove)
+SYM_FUNC_ALIAS(__pi___memmove, __memmove)
diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index 4e482159f35d..fb19c80ac0c4 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -742,6 +742,8 @@ static __init pgprot_t pgprot_from_va(uintptr_t va)
 #endif /* CONFIG_STRICT_KERNEL_RWX */
 
 #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL)
+u64 __pi_set_satp_mode_from_cmdline(uintptr_t dtb_pa);
+
 static void __init disable_pgtable_l5(void)
 {
 	pgtable_l5_enabled = false;
@@ -756,17 +758,39 @@ static void __init disable_pgtable_l4(void)
 	satp_mode = SATP_MODE_39;
 }
 
+static int __init print_no4lvl(char *p)
+{
+	pr_info("Disabled 4-level and 5-level paging");
+	return 0;
+}
+early_param("no4lvl", print_no4lvl);
+
+static int __init print_no5lvl(char *p)
+{
+	pr_info("Disabled 5-level paging");
+	return 0;
+}
+early_param("no5lvl", print_no5lvl);
+
 /*
  * There is a simple way to determine if 4-level is supported by the
  * underlying hardware: establish 1:1 mapping in 4-level page table mode
  * then read SATP to see if the configuration was taken into account
  * meaning sv48 is supported.
  */
-static __init void set_satp_mode(void)
+static __init void set_satp_mode(uintptr_t dtb_pa)
 {
 	u64 identity_satp, hw_satp;
 	uintptr_t set_satp_mode_pmd = ((unsigned long)set_satp_mode) & PMD_MASK;
-	bool check_l4 = false;
+	u64 satp_mode_cmdline = __pi_set_satp_mode_from_cmdline(dtb_pa);
+
+	if (satp_mode_cmdline == SATP_MODE_57) {
+		disable_pgtable_l5();
+	} else if (satp_mode_cmdline == SATP_MODE_48) {
+		disable_pgtable_l5();
+		disable_pgtable_l4();
+		return;
+	}
 
 	create_p4d_mapping(early_p4d,
 			set_satp_mode_pmd, (uintptr_t)early_pud,
@@ -785,7 +809,8 @@ static __init void set_satp_mode(void)
 retry:
 	create_pgd_mapping(early_pg_dir,
 			   set_satp_mode_pmd,
-			   check_l4 ? (uintptr_t)early_pud : (uintptr_t)early_p4d,
+			   pgtable_l5_enabled ?
+				(uintptr_t)early_p4d : (uintptr_t)early_pud,
 			   PGDIR_SIZE, PAGE_TABLE);
 
 	identity_satp = PFN_DOWN((uintptr_t)&early_pg_dir) | satp_mode;
@@ -796,9 +821,8 @@ static __init void set_satp_mode(void)
 	local_flush_tlb_all();
 
 	if (hw_satp != identity_satp) {
-		if (!check_l4) {
+		if (pgtable_l5_enabled) {
 			disable_pgtable_l5();
-			check_l4 = true;
 			memset(early_pg_dir, 0, PAGE_SIZE);
 			goto retry;
 		}
@@ -989,7 +1013,7 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
 #endif
 
 #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL)
-	set_satp_mode();
+	set_satp_mode(dtb_pa);
 #endif
 
 	kernel_map.va_pa_offset = 0UL;
-- 
2.37.2


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* [PATCH v4 1/1] riscv: Allow to downgrade paging mode from the command line
@ 2023-01-31 15:11   ` Alexandre Ghiti
  0 siblings, 0 replies; 16+ messages in thread
From: Alexandre Ghiti @ 2023-01-31 15:11 UTC (permalink / raw)
  To: Jonathan Corbet, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Conor Dooley, Ard Biesheuvel, linux-doc, linux-kernel,
	linux-riscv
  Cc: Alexandre Ghiti

Add 2 early command line parameters that allow to downgrade satp mode
(using the same naming as x86):
- "no5lvl": use a 4-level page table (down from sv57 to sv48)
- "no4lvl": use a 3-level page table (down from sv57/sv48 to sv39)

Note that going through the device tree to get the kernel command line
works with ACPI too since the efi stub creates a device tree anyway with
the command line.

In KASAN kernels, we can't use the libfdt that early in the boot process
since we are not ready to execute instrumented functions. So instead of
using the "generic" libfdt, we compile our own versions of those functions
that are not instrumented and that are prefixed so that they do not
conflict with the generic ones. We also need the non-instrumented versions
of the string functions and the prefixed versions of memcpy/memmove.

This is largely inspired by commit aacd149b6238 ("arm64: head: avoid
relocating the kernel twice for KASLR") from which I removed compilation
flags that were not relevant to RISC-V at the moment (LTO, SCS, pie).

Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
---
 .../admin-guide/kernel-parameters.txt         |  5 +-
 arch/riscv/kernel/Makefile                    |  2 +
 arch/riscv/kernel/pi/Makefile                 | 34 ++++++++++++
 arch/riscv/kernel/pi/cmdline_early.c          | 52 +++++++++++++++++++
 arch/riscv/lib/memcpy.S                       |  2 +
 arch/riscv/lib/memmove.S                      |  2 +
 arch/riscv/mm/init.c                          | 36 ++++++++++---
 7 files changed, 126 insertions(+), 7 deletions(-)
 create mode 100644 arch/riscv/kernel/pi/Makefile
 create mode 100644 arch/riscv/kernel/pi/cmdline_early.c

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 6cfa6e3996cf..1d8fc66d2b0d 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3578,7 +3578,10 @@
 			emulation library even if a 387 maths coprocessor
 			is present.
 
-	no5lvl		[X86-64] Disable 5-level paging mode. Forces
+	no4lvl		[RISCV] Disable 4-level and 5-level paging modes. Forces
+			kernel to use 3-level paging instead.
+
+	no5lvl		[X86-64,RISCV] Disable 5-level paging mode. Forces
 			kernel to use 4-level paging instead.
 
 	nofsgsbase	[X86] Disables FSGSBASE instructions.
diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
index 4cf303a779ab..6756209f1ac6 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -89,3 +89,5 @@ obj-$(CONFIG_EFI)		+= efi.o
 obj-$(CONFIG_COMPAT)		+= compat_syscall_table.o
 obj-$(CONFIG_COMPAT)		+= compat_signal.o
 obj-$(CONFIG_COMPAT)		+= compat_vdso/
+
+obj-y				+= pi/
diff --git a/arch/riscv/kernel/pi/Makefile b/arch/riscv/kernel/pi/Makefile
new file mode 100644
index 000000000000..554e936ef0b6
--- /dev/null
+++ b/arch/riscv/kernel/pi/Makefile
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: GPL-2.0
+# This file was copied from arm64/kernel/pi/Makefile.
+
+KBUILD_CFLAGS	:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
+		   -Os -DDISABLE_BRANCH_PROFILING $(DISABLE_STACKLEAK_PLUGIN) \
+		   $(call cc-option,-mbranch-protection=none) \
+		   -I$(srctree)/scripts/dtc/libfdt -fno-stack-protector \
+		   -D__DISABLE_EXPORTS -ffreestanding \
+		   -fno-asynchronous-unwind-tables -fno-unwind-tables \
+		   $(call cc-option,-fno-addrsig)
+
+GCOV_PROFILE	:= n
+KASAN_SANITIZE	:= n
+KCSAN_SANITIZE	:= n
+UBSAN_SANITIZE	:= n
+KCOV_INSTRUMENT	:= n
+
+$(obj)/%.pi.o: OBJCOPYFLAGS := --prefix-symbols=__pi_ \
+			       --remove-section=.note.gnu.property \
+			       --prefix-alloc-sections=.init
+$(obj)/%.pi.o: $(obj)/%.o FORCE
+	$(call if_changed,objcopy)
+
+$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
+	$(call if_changed_rule,cc_o_c)
+
+$(obj)/string.o: $(srctree)/lib/string.c FORCE
+	$(call if_changed_rule,cc_o_c)
+
+$(obj)/ctype.o: $(srctree)/lib/ctype.c FORCE
+	$(call if_changed_rule,cc_o_c)
+
+obj-y		:= cmdline_early.pi.o string.pi.o ctype.pi.o lib-fdt.pi.o lib-fdt_ro.pi.o
+extra-y		:= $(patsubst %.pi.o,%.o,$(obj-y))
diff --git a/arch/riscv/kernel/pi/cmdline_early.c b/arch/riscv/kernel/pi/cmdline_early.c
new file mode 100644
index 000000000000..5ae7b853fa66
--- /dev/null
+++ b/arch/riscv/kernel/pi/cmdline_early.c
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <linux/types.h>
+#include <linux/init.h>
+#include <linux/libfdt.h>
+#include <linux/string.h>
+#include <asm/pgtable.h>
+#include <asm/setup.h>
+
+static char early_cmdline[COMMAND_LINE_SIZE] __initdata;
+
+static char * __init get_early_cmdline(uintptr_t dtb_pa)
+{
+	const char *fdt_cmdline = NULL;
+	unsigned int fdt_cmdline_size = 0;
+	int chosen_node;
+
+	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
+		chosen_node = fdt_path_offset((void *)dtb_pa, "/chosen");
+		if (chosen_node >= 0) {
+			fdt_cmdline = fdt_getprop((void *)dtb_pa, chosen_node,
+						  "bootargs", NULL);
+			fdt_cmdline_size = strlen(fdt_cmdline);
+			strscpy(early_cmdline, fdt_cmdline, COMMAND_LINE_SIZE);
+		}
+	}
+
+	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
+	    IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
+	    fdt_cmdline_size == 0 /* CONFIG_CMDLINE_FALLBACK */) {
+		strncat(early_cmdline, CONFIG_CMDLINE,
+			COMMAND_LINE_SIZE - fdt_cmdline_size);
+	}
+
+	return early_cmdline;
+}
+
+static u64 __init match_noXlvl(char *cmdline)
+{
+	if (strstr(cmdline, "no4lvl"))
+		return SATP_MODE_48;
+	else if (strstr(cmdline, "no5lvl"))
+		return SATP_MODE_57;
+
+	return 0;
+}
+
+u64 __init set_satp_mode_from_cmdline(uintptr_t dtb_pa)
+{
+	char *cmdline = get_early_cmdline(dtb_pa);
+
+	return match_noXlvl(cmdline);
+}
diff --git a/arch/riscv/lib/memcpy.S b/arch/riscv/lib/memcpy.S
index 51ab716253fa..1a40d01a9543 100644
--- a/arch/riscv/lib/memcpy.S
+++ b/arch/riscv/lib/memcpy.S
@@ -106,3 +106,5 @@ WEAK(memcpy)
 6:
 	ret
 END(__memcpy)
+SYM_FUNC_ALIAS(__pi_memcpy, __memcpy)
+SYM_FUNC_ALIAS(__pi___memcpy, __memcpy)
diff --git a/arch/riscv/lib/memmove.S b/arch/riscv/lib/memmove.S
index e0609e1f0864..838ff2022fe3 100644
--- a/arch/riscv/lib/memmove.S
+++ b/arch/riscv/lib/memmove.S
@@ -314,3 +314,5 @@ return_from_memmove:
 
 SYM_FUNC_END(memmove)
 SYM_FUNC_END(__memmove)
+SYM_FUNC_ALIAS(__pi_memmove, __memmove)
+SYM_FUNC_ALIAS(__pi___memmove, __memmove)
diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index 4e482159f35d..fb19c80ac0c4 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -742,6 +742,8 @@ static __init pgprot_t pgprot_from_va(uintptr_t va)
 #endif /* CONFIG_STRICT_KERNEL_RWX */
 
 #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL)
+u64 __pi_set_satp_mode_from_cmdline(uintptr_t dtb_pa);
+
 static void __init disable_pgtable_l5(void)
 {
 	pgtable_l5_enabled = false;
@@ -756,17 +758,39 @@ static void __init disable_pgtable_l4(void)
 	satp_mode = SATP_MODE_39;
 }
 
+static int __init print_no4lvl(char *p)
+{
+	pr_info("Disabled 4-level and 5-level paging");
+	return 0;
+}
+early_param("no4lvl", print_no4lvl);
+
+static int __init print_no5lvl(char *p)
+{
+	pr_info("Disabled 5-level paging");
+	return 0;
+}
+early_param("no5lvl", print_no5lvl);
+
 /*
  * There is a simple way to determine if 4-level is supported by the
  * underlying hardware: establish 1:1 mapping in 4-level page table mode
  * then read SATP to see if the configuration was taken into account
  * meaning sv48 is supported.
  */
-static __init void set_satp_mode(void)
+static __init void set_satp_mode(uintptr_t dtb_pa)
 {
 	u64 identity_satp, hw_satp;
 	uintptr_t set_satp_mode_pmd = ((unsigned long)set_satp_mode) & PMD_MASK;
-	bool check_l4 = false;
+	u64 satp_mode_cmdline = __pi_set_satp_mode_from_cmdline(dtb_pa);
+
+	if (satp_mode_cmdline == SATP_MODE_57) {
+		disable_pgtable_l5();
+	} else if (satp_mode_cmdline == SATP_MODE_48) {
+		disable_pgtable_l5();
+		disable_pgtable_l4();
+		return;
+	}
 
 	create_p4d_mapping(early_p4d,
 			set_satp_mode_pmd, (uintptr_t)early_pud,
@@ -785,7 +809,8 @@ static __init void set_satp_mode(void)
 retry:
 	create_pgd_mapping(early_pg_dir,
 			   set_satp_mode_pmd,
-			   check_l4 ? (uintptr_t)early_pud : (uintptr_t)early_p4d,
+			   pgtable_l5_enabled ?
+				(uintptr_t)early_p4d : (uintptr_t)early_pud,
 			   PGDIR_SIZE, PAGE_TABLE);
 
 	identity_satp = PFN_DOWN((uintptr_t)&early_pg_dir) | satp_mode;
@@ -796,9 +821,8 @@ static __init void set_satp_mode(void)
 	local_flush_tlb_all();
 
 	if (hw_satp != identity_satp) {
-		if (!check_l4) {
+		if (pgtable_l5_enabled) {
 			disable_pgtable_l5();
-			check_l4 = true;
 			memset(early_pg_dir, 0, PAGE_SIZE);
 			goto retry;
 		}
@@ -989,7 +1013,7 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
 #endif
 
 #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL)
-	set_satp_mode();
+	set_satp_mode(dtb_pa);
 #endif
 
 	kernel_map.va_pa_offset = 0UL;
-- 
2.37.2


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

* Re: [PATCH v4 0/1] riscv: Allow to downgrade paging mode from the command line
  2023-01-31 15:11 ` Alexandre Ghiti
@ 2023-01-31 15:34   ` Alexandre Ghiti
  -1 siblings, 0 replies; 16+ messages in thread
From: Alexandre Ghiti @ 2023-01-31 15:34 UTC (permalink / raw)
  To: Alexandre Ghiti, Jonathan Corbet, Paul Walmsley, Palmer Dabbelt,
	Albert Ou, Conor Dooley, Ard Biesheuvel, linux-doc, linux-kernel,
	linux-riscv

Sorry, I forgot the changelog again:

|v4: - Remove the kasan limitation by compiling 'local' fdt functions, 
as done in arm64 by Ard - Remove RB from Björn as the cmdline handling 
changed v3: - Massage commit log to make no4lvl clearer, as asked by 
Conor - Add a note to kernel-parameters.txt regarding the impossibility 
to use those parameters when KASAN is enabled, as suggested by Conor - 
Add RB from Björn v2: - Honor CMDLINE_EXTEND and CMDLINE_FORCE as 
noticed by Björn|

On 1/31/23 16:11, Alexandre Ghiti wrote:
> This new version gets rid of the limitation that prevented KASAN kernels
> to use the newly introduced parameters.
>
> While looking into KASLR, I fell onto commit aacd149b6238 ("arm64: head:
> avoid relocating the kernel twice for KASLR"): it allows to use the fdt
> functions very early in the boot process with KASAN enabled by simply
> compiling a new version of those functions without instrumentation.
>
> I had to change the handling of the command line parsing to make the
> code self-contained in kernel/pi/cmd_early.c to avoid calling too many
> __pi prefixed functions from outside this file.
>
> I'll use this approach like arm64 to handle the extraction of the random
> seedi from the device tree for KASLR.
>
> @Ard: I copied the "pi" name but I have to admit I don't know what it
> means.
>
> Alexandre Ghiti (1):
>    riscv: Allow to downgrade paging mode from the command line
>
>   .../admin-guide/kernel-parameters.txt         |  5 +-
>   arch/riscv/kernel/Makefile                    |  2 +
>   arch/riscv/kernel/pi/Makefile                 | 34 ++++++++++++
>   arch/riscv/kernel/pi/cmdline_early.c          | 52 +++++++++++++++++++
>   arch/riscv/lib/memcpy.S                       |  2 +
>   arch/riscv/lib/memmove.S                      |  2 +
>   arch/riscv/mm/init.c                          | 36 ++++++++++---
>   7 files changed, 126 insertions(+), 7 deletions(-)
>   create mode 100644 arch/riscv/kernel/pi/Makefile
>   create mode 100644 arch/riscv/kernel/pi/cmdline_early.c
>

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

* Re: [PATCH v4 0/1] riscv: Allow to downgrade paging mode from the command line
@ 2023-01-31 15:34   ` Alexandre Ghiti
  0 siblings, 0 replies; 16+ messages in thread
From: Alexandre Ghiti @ 2023-01-31 15:34 UTC (permalink / raw)
  To: Alexandre Ghiti, Jonathan Corbet, Paul Walmsley, Palmer Dabbelt,
	Albert Ou, Conor Dooley, Ard Biesheuvel, linux-doc, linux-kernel,
	linux-riscv

Sorry, I forgot the changelog again:

|v4: - Remove the kasan limitation by compiling 'local' fdt functions, 
as done in arm64 by Ard - Remove RB from Björn as the cmdline handling 
changed v3: - Massage commit log to make no4lvl clearer, as asked by 
Conor - Add a note to kernel-parameters.txt regarding the impossibility 
to use those parameters when KASAN is enabled, as suggested by Conor - 
Add RB from Björn v2: - Honor CMDLINE_EXTEND and CMDLINE_FORCE as 
noticed by Björn|

On 1/31/23 16:11, Alexandre Ghiti wrote:
> This new version gets rid of the limitation that prevented KASAN kernels
> to use the newly introduced parameters.
>
> While looking into KASLR, I fell onto commit aacd149b6238 ("arm64: head:
> avoid relocating the kernel twice for KASLR"): it allows to use the fdt
> functions very early in the boot process with KASAN enabled by simply
> compiling a new version of those functions without instrumentation.
>
> I had to change the handling of the command line parsing to make the
> code self-contained in kernel/pi/cmd_early.c to avoid calling too many
> __pi prefixed functions from outside this file.
>
> I'll use this approach like arm64 to handle the extraction of the random
> seedi from the device tree for KASLR.
>
> @Ard: I copied the "pi" name but I have to admit I don't know what it
> means.
>
> Alexandre Ghiti (1):
>    riscv: Allow to downgrade paging mode from the command line
>
>   .../admin-guide/kernel-parameters.txt         |  5 +-
>   arch/riscv/kernel/Makefile                    |  2 +
>   arch/riscv/kernel/pi/Makefile                 | 34 ++++++++++++
>   arch/riscv/kernel/pi/cmdline_early.c          | 52 +++++++++++++++++++
>   arch/riscv/lib/memcpy.S                       |  2 +
>   arch/riscv/lib/memmove.S                      |  2 +
>   arch/riscv/mm/init.c                          | 36 ++++++++++---
>   7 files changed, 126 insertions(+), 7 deletions(-)
>   create mode 100644 arch/riscv/kernel/pi/Makefile
>   create mode 100644 arch/riscv/kernel/pi/cmdline_early.c
>

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v4 0/1] riscv: Allow to downgrade paging mode from the command line
  2023-01-31 15:11 ` Alexandre Ghiti
@ 2023-01-31 19:24   ` Conor Dooley
  -1 siblings, 0 replies; 16+ messages in thread
From: Conor Dooley @ 2023-01-31 19:24 UTC (permalink / raw)
  To: Alexandre Ghiti
  Cc: Jonathan Corbet, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Ard Biesheuvel, linux-doc, linux-kernel, linux-riscv

[-- Attachment #1: Type: text/plain, Size: 1843 bytes --]

Hey Alex,

On Tue, Jan 31, 2023 at 04:11:14PM +0100, Alexandre Ghiti wrote:
> This new version gets rid of the limitation that prevented KASAN kernels
> to use the newly introduced parameters.

What's the base commit for this version?
Fails to apply for me on top of for-next, fixes, rc1, and linux-next :/
I also tried it on top of your KASAN series, but no luck there either!

Thanks,
Conor.

> While looking into KASLR, I fell onto commit aacd149b6238 ("arm64: head:
> avoid relocating the kernel twice for KASLR"): it allows to use the fdt
> functions very early in the boot process with KASAN enabled by simply
> compiling a new version of those functions without instrumentation.
> 
> I had to change the handling of the command line parsing to make the
> code self-contained in kernel/pi/cmd_early.c to avoid calling too many
> __pi prefixed functions from outside this file.
> 
> I'll use this approach like arm64 to handle the extraction of the random
> seedi from the device tree for KASLR.
> 
> @Ard: I copied the "pi" name but I have to admit I don't know what it
> means.
> 
> Alexandre Ghiti (1):
>   riscv: Allow to downgrade paging mode from the command line
> 
>  .../admin-guide/kernel-parameters.txt         |  5 +-
>  arch/riscv/kernel/Makefile                    |  2 +
>  arch/riscv/kernel/pi/Makefile                 | 34 ++++++++++++
>  arch/riscv/kernel/pi/cmdline_early.c          | 52 +++++++++++++++++++
>  arch/riscv/lib/memcpy.S                       |  2 +
>  arch/riscv/lib/memmove.S                      |  2 +
>  arch/riscv/mm/init.c                          | 36 ++++++++++---
>  7 files changed, 126 insertions(+), 7 deletions(-)
>  create mode 100644 arch/riscv/kernel/pi/Makefile
>  create mode 100644 arch/riscv/kernel/pi/cmdline_early.c
> 
> -- 
> 2.37.2
> 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

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

* Re: [PATCH v4 0/1] riscv: Allow to downgrade paging mode from the command line
@ 2023-01-31 19:24   ` Conor Dooley
  0 siblings, 0 replies; 16+ messages in thread
From: Conor Dooley @ 2023-01-31 19:24 UTC (permalink / raw)
  To: Alexandre Ghiti
  Cc: Jonathan Corbet, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Ard Biesheuvel, linux-doc, linux-kernel, linux-riscv


[-- Attachment #1.1: Type: text/plain, Size: 1843 bytes --]

Hey Alex,

On Tue, Jan 31, 2023 at 04:11:14PM +0100, Alexandre Ghiti wrote:
> This new version gets rid of the limitation that prevented KASAN kernels
> to use the newly introduced parameters.

What's the base commit for this version?
Fails to apply for me on top of for-next, fixes, rc1, and linux-next :/
I also tried it on top of your KASAN series, but no luck there either!

Thanks,
Conor.

> While looking into KASLR, I fell onto commit aacd149b6238 ("arm64: head:
> avoid relocating the kernel twice for KASLR"): it allows to use the fdt
> functions very early in the boot process with KASAN enabled by simply
> compiling a new version of those functions without instrumentation.
> 
> I had to change the handling of the command line parsing to make the
> code self-contained in kernel/pi/cmd_early.c to avoid calling too many
> __pi prefixed functions from outside this file.
> 
> I'll use this approach like arm64 to handle the extraction of the random
> seedi from the device tree for KASLR.
> 
> @Ard: I copied the "pi" name but I have to admit I don't know what it
> means.
> 
> Alexandre Ghiti (1):
>   riscv: Allow to downgrade paging mode from the command line
> 
>  .../admin-guide/kernel-parameters.txt         |  5 +-
>  arch/riscv/kernel/Makefile                    |  2 +
>  arch/riscv/kernel/pi/Makefile                 | 34 ++++++++++++
>  arch/riscv/kernel/pi/cmdline_early.c          | 52 +++++++++++++++++++
>  arch/riscv/lib/memcpy.S                       |  2 +
>  arch/riscv/lib/memmove.S                      |  2 +
>  arch/riscv/mm/init.c                          | 36 ++++++++++---
>  7 files changed, 126 insertions(+), 7 deletions(-)
>  create mode 100644 arch/riscv/kernel/pi/Makefile
>  create mode 100644 arch/riscv/kernel/pi/cmdline_early.c
> 
> -- 
> 2.37.2
> 

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

[-- Attachment #2: Type: text/plain, Size: 161 bytes --]

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v4 0/1] riscv: Allow to downgrade paging mode from the command line
  2023-01-31 19:24   ` Conor Dooley
@ 2023-02-02 15:07     ` Alexandre Ghiti
  -1 siblings, 0 replies; 16+ messages in thread
From: Alexandre Ghiti @ 2023-02-02 15:07 UTC (permalink / raw)
  To: Conor Dooley
  Cc: Jonathan Corbet, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Ard Biesheuvel, linux-doc, linux-kernel, linux-riscv

Hi Conor,

On Tue, Jan 31, 2023 at 8:25 PM Conor Dooley <conor@kernel.org> wrote:
>
> Hey Alex,
>
> On Tue, Jan 31, 2023 at 04:11:14PM +0100, Alexandre Ghiti wrote:
> > This new version gets rid of the limitation that prevented KASAN kernels
> > to use the newly introduced parameters.
>
> What's the base commit for this version?
> Fails to apply for me on top of for-next, fixes, rc1, and linux-next :/
> I also tried it on top of your KASAN series, but no luck there either!

Really sorry about that, that's the second time it happens, I'll
improve my workflow: I'll include the base-commit sha1/tag + a link to
all needed-but-not-yet-upstreamed patches.

Thanks for your time,

Alex

>
> Thanks,
> Conor.
>
> > While looking into KASLR, I fell onto commit aacd149b6238 ("arm64: head:
> > avoid relocating the kernel twice for KASLR"): it allows to use the fdt
> > functions very early in the boot process with KASAN enabled by simply
> > compiling a new version of those functions without instrumentation.
> >
> > I had to change the handling of the command line parsing to make the
> > code self-contained in kernel/pi/cmd_early.c to avoid calling too many
> > __pi prefixed functions from outside this file.
> >
> > I'll use this approach like arm64 to handle the extraction of the random
> > seedi from the device tree for KASLR.
> >
> > @Ard: I copied the "pi" name but I have to admit I don't know what it
> > means.
> >
> > Alexandre Ghiti (1):
> >   riscv: Allow to downgrade paging mode from the command line
> >
> >  .../admin-guide/kernel-parameters.txt         |  5 +-
> >  arch/riscv/kernel/Makefile                    |  2 +
> >  arch/riscv/kernel/pi/Makefile                 | 34 ++++++++++++
> >  arch/riscv/kernel/pi/cmdline_early.c          | 52 +++++++++++++++++++
> >  arch/riscv/lib/memcpy.S                       |  2 +
> >  arch/riscv/lib/memmove.S                      |  2 +
> >  arch/riscv/mm/init.c                          | 36 ++++++++++---
> >  7 files changed, 126 insertions(+), 7 deletions(-)
> >  create mode 100644 arch/riscv/kernel/pi/Makefile
> >  create mode 100644 arch/riscv/kernel/pi/cmdline_early.c
> >
> > --
> > 2.37.2
> >

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

* Re: [PATCH v4 0/1] riscv: Allow to downgrade paging mode from the command line
@ 2023-02-02 15:07     ` Alexandre Ghiti
  0 siblings, 0 replies; 16+ messages in thread
From: Alexandre Ghiti @ 2023-02-02 15:07 UTC (permalink / raw)
  To: Conor Dooley
  Cc: Jonathan Corbet, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Ard Biesheuvel, linux-doc, linux-kernel, linux-riscv

Hi Conor,

On Tue, Jan 31, 2023 at 8:25 PM Conor Dooley <conor@kernel.org> wrote:
>
> Hey Alex,
>
> On Tue, Jan 31, 2023 at 04:11:14PM +0100, Alexandre Ghiti wrote:
> > This new version gets rid of the limitation that prevented KASAN kernels
> > to use the newly introduced parameters.
>
> What's the base commit for this version?
> Fails to apply for me on top of for-next, fixes, rc1, and linux-next :/
> I also tried it on top of your KASAN series, but no luck there either!

Really sorry about that, that's the second time it happens, I'll
improve my workflow: I'll include the base-commit sha1/tag + a link to
all needed-but-not-yet-upstreamed patches.

Thanks for your time,

Alex

>
> Thanks,
> Conor.
>
> > While looking into KASLR, I fell onto commit aacd149b6238 ("arm64: head:
> > avoid relocating the kernel twice for KASLR"): it allows to use the fdt
> > functions very early in the boot process with KASAN enabled by simply
> > compiling a new version of those functions without instrumentation.
> >
> > I had to change the handling of the command line parsing to make the
> > code self-contained in kernel/pi/cmd_early.c to avoid calling too many
> > __pi prefixed functions from outside this file.
> >
> > I'll use this approach like arm64 to handle the extraction of the random
> > seedi from the device tree for KASLR.
> >
> > @Ard: I copied the "pi" name but I have to admit I don't know what it
> > means.
> >
> > Alexandre Ghiti (1):
> >   riscv: Allow to downgrade paging mode from the command line
> >
> >  .../admin-guide/kernel-parameters.txt         |  5 +-
> >  arch/riscv/kernel/Makefile                    |  2 +
> >  arch/riscv/kernel/pi/Makefile                 | 34 ++++++++++++
> >  arch/riscv/kernel/pi/cmdline_early.c          | 52 +++++++++++++++++++
> >  arch/riscv/lib/memcpy.S                       |  2 +
> >  arch/riscv/lib/memmove.S                      |  2 +
> >  arch/riscv/mm/init.c                          | 36 ++++++++++---
> >  7 files changed, 126 insertions(+), 7 deletions(-)
> >  create mode 100644 arch/riscv/kernel/pi/Makefile
> >  create mode 100644 arch/riscv/kernel/pi/cmdline_early.c
> >
> > --
> > 2.37.2
> >

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v4 0/1] riscv: Allow to downgrade paging mode from the command line
  2023-02-02 15:07     ` Alexandre Ghiti
@ 2023-02-02 15:13       ` Alexandre Ghiti
  -1 siblings, 0 replies; 16+ messages in thread
From: Alexandre Ghiti @ 2023-02-02 15:13 UTC (permalink / raw)
  To: Conor Dooley
  Cc: Jonathan Corbet, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Ard Biesheuvel, linux-doc, linux-kernel, linux-riscv

On Thu, Feb 2, 2023 at 4:07 PM Alexandre Ghiti <alexghiti@rivosinc.com> wrote:
>
> Hi Conor,
>
> On Tue, Jan 31, 2023 at 8:25 PM Conor Dooley <conor@kernel.org> wrote:
> >
> > Hey Alex,
> >
> > On Tue, Jan 31, 2023 at 04:11:14PM +0100, Alexandre Ghiti wrote:
> > > This new version gets rid of the limitation that prevented KASAN kernels
> > > to use the newly introduced parameters.
> >
> > What's the base commit for this version?

And to answer your question: this patch applies nicely on top of
v6.2-rc6 + my KASAN patchset
(https://patchwork.kernel.org/project/linux-riscv/list/?series=715388)

> > Fails to apply for me on top of for-next, fixes, rc1, and linux-next :/
> > I also tried it on top of your KASAN series, but no luck there either!
>
> Really sorry about that, that's the second time it happens, I'll
> improve my workflow: I'll include the base-commit sha1/tag + a link to
> all needed-but-not-yet-upstreamed patches.
>
> Thanks for your time,
>
> Alex
>
> >
> > Thanks,
> > Conor.
> >
> > > While looking into KASLR, I fell onto commit aacd149b6238 ("arm64: head:
> > > avoid relocating the kernel twice for KASLR"): it allows to use the fdt
> > > functions very early in the boot process with KASAN enabled by simply
> > > compiling a new version of those functions without instrumentation.
> > >
> > > I had to change the handling of the command line parsing to make the
> > > code self-contained in kernel/pi/cmd_early.c to avoid calling too many
> > > __pi prefixed functions from outside this file.
> > >
> > > I'll use this approach like arm64 to handle the extraction of the random
> > > seedi from the device tree for KASLR.
> > >
> > > @Ard: I copied the "pi" name but I have to admit I don't know what it
> > > means.
> > >
> > > Alexandre Ghiti (1):
> > >   riscv: Allow to downgrade paging mode from the command line
> > >
> > >  .../admin-guide/kernel-parameters.txt         |  5 +-
> > >  arch/riscv/kernel/Makefile                    |  2 +
> > >  arch/riscv/kernel/pi/Makefile                 | 34 ++++++++++++
> > >  arch/riscv/kernel/pi/cmdline_early.c          | 52 +++++++++++++++++++
> > >  arch/riscv/lib/memcpy.S                       |  2 +
> > >  arch/riscv/lib/memmove.S                      |  2 +
> > >  arch/riscv/mm/init.c                          | 36 ++++++++++---
> > >  7 files changed, 126 insertions(+), 7 deletions(-)
> > >  create mode 100644 arch/riscv/kernel/pi/Makefile
> > >  create mode 100644 arch/riscv/kernel/pi/cmdline_early.c
> > >
> > > --
> > > 2.37.2
> > >

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

* Re: [PATCH v4 0/1] riscv: Allow to downgrade paging mode from the command line
@ 2023-02-02 15:13       ` Alexandre Ghiti
  0 siblings, 0 replies; 16+ messages in thread
From: Alexandre Ghiti @ 2023-02-02 15:13 UTC (permalink / raw)
  To: Conor Dooley
  Cc: Jonathan Corbet, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Ard Biesheuvel, linux-doc, linux-kernel, linux-riscv

On Thu, Feb 2, 2023 at 4:07 PM Alexandre Ghiti <alexghiti@rivosinc.com> wrote:
>
> Hi Conor,
>
> On Tue, Jan 31, 2023 at 8:25 PM Conor Dooley <conor@kernel.org> wrote:
> >
> > Hey Alex,
> >
> > On Tue, Jan 31, 2023 at 04:11:14PM +0100, Alexandre Ghiti wrote:
> > > This new version gets rid of the limitation that prevented KASAN kernels
> > > to use the newly introduced parameters.
> >
> > What's the base commit for this version?

And to answer your question: this patch applies nicely on top of
v6.2-rc6 + my KASAN patchset
(https://patchwork.kernel.org/project/linux-riscv/list/?series=715388)

> > Fails to apply for me on top of for-next, fixes, rc1, and linux-next :/
> > I also tried it on top of your KASAN series, but no luck there either!
>
> Really sorry about that, that's the second time it happens, I'll
> improve my workflow: I'll include the base-commit sha1/tag + a link to
> all needed-but-not-yet-upstreamed patches.
>
> Thanks for your time,
>
> Alex
>
> >
> > Thanks,
> > Conor.
> >
> > > While looking into KASLR, I fell onto commit aacd149b6238 ("arm64: head:
> > > avoid relocating the kernel twice for KASLR"): it allows to use the fdt
> > > functions very early in the boot process with KASAN enabled by simply
> > > compiling a new version of those functions without instrumentation.
> > >
> > > I had to change the handling of the command line parsing to make the
> > > code self-contained in kernel/pi/cmd_early.c to avoid calling too many
> > > __pi prefixed functions from outside this file.
> > >
> > > I'll use this approach like arm64 to handle the extraction of the random
> > > seedi from the device tree for KASLR.
> > >
> > > @Ard: I copied the "pi" name but I have to admit I don't know what it
> > > means.
> > >
> > > Alexandre Ghiti (1):
> > >   riscv: Allow to downgrade paging mode from the command line
> > >
> > >  .../admin-guide/kernel-parameters.txt         |  5 +-
> > >  arch/riscv/kernel/Makefile                    |  2 +
> > >  arch/riscv/kernel/pi/Makefile                 | 34 ++++++++++++
> > >  arch/riscv/kernel/pi/cmdline_early.c          | 52 +++++++++++++++++++
> > >  arch/riscv/lib/memcpy.S                       |  2 +
> > >  arch/riscv/lib/memmove.S                      |  2 +
> > >  arch/riscv/mm/init.c                          | 36 ++++++++++---
> > >  7 files changed, 126 insertions(+), 7 deletions(-)
> > >  create mode 100644 arch/riscv/kernel/pi/Makefile
> > >  create mode 100644 arch/riscv/kernel/pi/cmdline_early.c
> > >
> > > --
> > > 2.37.2
> > >

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v4 0/1] riscv: Allow to downgrade paging mode from the command line
  2023-01-31 15:11 ` Alexandre Ghiti
@ 2023-02-17 14:48   ` Björn Töpel
  -1 siblings, 0 replies; 16+ messages in thread
From: Björn Töpel @ 2023-02-17 14:48 UTC (permalink / raw)
  To: Alexandre Ghiti, Jonathan Corbet, Paul Walmsley, Palmer Dabbelt,
	Albert Ou, Conor Dooley, Ard Biesheuvel, linux-doc, linux-kernel,
	linux-riscv
  Cc: Alexandre Ghiti

Alexandre Ghiti <alexghiti@rivosinc.com> writes:

> This new version gets rid of the limitation that prevented KASAN kernels
> to use the newly introduced parameters.
>
> While looking into KASLR, I fell onto commit aacd149b6238 ("arm64: head:
> avoid relocating the kernel twice for KASLR"): it allows to use the fdt
> functions very early in the boot process with KASAN enabled by simply
> compiling a new version of those functions without instrumentation.
>
> I had to change the handling of the command line parsing to make the
> code self-contained in kernel/pi/cmd_early.c to avoid calling too many
> __pi prefixed functions from outside this file.
>
> I'll use this approach like arm64 to handle the extraction of the random
> seedi from the device tree for KASLR.
>
> @Ard: I copied the "pi" name but I have to admit I don't know what it
> means.

__pi_ is for "position-independent" -- KASLR related.

For the (tiny ;-)) series:

Tested-by: Björn Töpel <bjorn@rivosinc.com>
Reviewed-by: Björn Töpel <bjorn@rivosinc.com>

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

* Re: [PATCH v4 0/1] riscv: Allow to downgrade paging mode from the command line
@ 2023-02-17 14:48   ` Björn Töpel
  0 siblings, 0 replies; 16+ messages in thread
From: Björn Töpel @ 2023-02-17 14:48 UTC (permalink / raw)
  To: Alexandre Ghiti, Jonathan Corbet, Paul Walmsley, Palmer Dabbelt,
	Albert Ou, Conor Dooley, Ard Biesheuvel, linux-doc, linux-kernel,
	linux-riscv
  Cc: Alexandre Ghiti

Alexandre Ghiti <alexghiti@rivosinc.com> writes:

> This new version gets rid of the limitation that prevented KASAN kernels
> to use the newly introduced parameters.
>
> While looking into KASLR, I fell onto commit aacd149b6238 ("arm64: head:
> avoid relocating the kernel twice for KASLR"): it allows to use the fdt
> functions very early in the boot process with KASAN enabled by simply
> compiling a new version of those functions without instrumentation.
>
> I had to change the handling of the command line parsing to make the
> code self-contained in kernel/pi/cmd_early.c to avoid calling too many
> __pi prefixed functions from outside this file.
>
> I'll use this approach like arm64 to handle the extraction of the random
> seedi from the device tree for KASLR.
>
> @Ard: I copied the "pi" name but I have to admit I don't know what it
> means.

__pi_ is for "position-independent" -- KASLR related.

For the (tiny ;-)) series:

Tested-by: Björn Töpel <bjorn@rivosinc.com>
Reviewed-by: Björn Töpel <bjorn@rivosinc.com>

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH v4 1/1] riscv: Allow to downgrade paging mode from the command line
  2023-01-31 15:11   ` Alexandre Ghiti
@ 2023-02-20 13:10     ` Björn Töpel
  -1 siblings, 0 replies; 16+ messages in thread
From: Björn Töpel @ 2023-02-20 13:10 UTC (permalink / raw)
  To: Alexandre Ghiti, Jonathan Corbet, Paul Walmsley, Palmer Dabbelt,
	Albert Ou, Conor Dooley, Ard Biesheuvel, linux-doc, linux-kernel,
	linux-riscv
  Cc: Alexandre Ghiti

Alexandre Ghiti <alexghiti@rivosinc.com> writes:

> Add 2 early command line parameters that allow to downgrade satp mode
> (using the same naming as x86):
> - "no5lvl": use a 4-level page table (down from sv57 to sv48)
> - "no4lvl": use a 3-level page table (down from sv57/sv48 to sv39)
>
> Note that going through the device tree to get the kernel command line
> works with ACPI too since the efi stub creates a device tree anyway with
> the command line.
>
> In KASAN kernels, we can't use the libfdt that early in the boot process
> since we are not ready to execute instrumented functions. So instead of
> using the "generic" libfdt, we compile our own versions of those functions
> that are not instrumented and that are prefixed so that they do not
> conflict with the generic ones. We also need the non-instrumented versions
> of the string functions and the prefixed versions of memcpy/memmove.
>
> This is largely inspired by commit aacd149b6238 ("arm64: head: avoid
> relocating the kernel twice for KASLR") from which I removed compilation
> flags that were not relevant to RISC-V at the moment (LTO, SCS, pie).
>
> Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
> ---
>  .../admin-guide/kernel-parameters.txt         |  5 +-
>  arch/riscv/kernel/Makefile                    |  2 +
>  arch/riscv/kernel/pi/Makefile                 | 34 ++++++++++++
>  arch/riscv/kernel/pi/cmdline_early.c          | 52 +++++++++++++++++++
>  arch/riscv/lib/memcpy.S                       |  2 +
>  arch/riscv/lib/memmove.S                      |  2 +
>  arch/riscv/mm/init.c                          | 36 ++++++++++---
>  7 files changed, 126 insertions(+), 7 deletions(-)
>  create mode 100644 arch/riscv/kernel/pi/Makefile
>  create mode 100644 arch/riscv/kernel/pi/cmdline_early.c
>
> diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
> index 6cfa6e3996cf..1d8fc66d2b0d 100644
> --- a/Documentation/admin-guide/kernel-parameters.txt
> +++ b/Documentation/admin-guide/kernel-parameters.txt
> @@ -3578,7 +3578,10 @@
>  			emulation library even if a 387 maths coprocessor
>  			is present.
>  
> -	no5lvl		[X86-64] Disable 5-level paging mode. Forces
> +	no4lvl		[RISCV] Disable 4-level and 5-level paging modes. Forces
> +			kernel to use 3-level paging instead.
> +
> +	no5lvl		[X86-64,RISCV] Disable 5-level paging mode. Forces
>  			kernel to use 4-level paging instead.
>  
>  	nofsgsbase	[X86] Disables FSGSBASE instructions.
> diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
> index 4cf303a779ab..6756209f1ac6 100644
> --- a/arch/riscv/kernel/Makefile
> +++ b/arch/riscv/kernel/Makefile
> @@ -89,3 +89,5 @@ obj-$(CONFIG_EFI)		+= efi.o
>  obj-$(CONFIG_COMPAT)		+= compat_syscall_table.o
>  obj-$(CONFIG_COMPAT)		+= compat_signal.o
>  obj-$(CONFIG_COMPAT)		+= compat_vdso/
> +
> +obj-y				+= pi/
> diff --git a/arch/riscv/kernel/pi/Makefile b/arch/riscv/kernel/pi/Makefile
> new file mode 100644
> index 000000000000..554e936ef0b6
> --- /dev/null
> +++ b/arch/riscv/kernel/pi/Makefile
> @@ -0,0 +1,34 @@
> +# SPDX-License-Identifier: GPL-2.0
> +# This file was copied from arm64/kernel/pi/Makefile.
> +
> +KBUILD_CFLAGS	:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
> +		   -Os -DDISABLE_BRANCH_PROFILING $(DISABLE_STACKLEAK_PLUGIN) \
> +		   $(call cc-option,-mbranch-protection=none) \
> +		   -I$(srctree)/scripts/dtc/libfdt -fno-stack-protector \
> +		   -D__DISABLE_EXPORTS -ffreestanding \
> +		   -fno-asynchronous-unwind-tables -fno-unwind-tables \
> +		   $(call cc-option,-fno-addrsig)
> +
> +GCOV_PROFILE	:= n
> +KASAN_SANITIZE	:= n
> +KCSAN_SANITIZE	:= n
> +UBSAN_SANITIZE	:= n
> +KCOV_INSTRUMENT	:= n
> +
> +$(obj)/%.pi.o: OBJCOPYFLAGS := --prefix-symbols=__pi_ \
> +			       --remove-section=.note.gnu.property \
> +			       --prefix-alloc-sections=.init
> +$(obj)/%.pi.o: $(obj)/%.o FORCE
> +	$(call if_changed,objcopy)
> +
> +$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
> +	$(call if_changed_rule,cc_o_c)
> +
> +$(obj)/string.o: $(srctree)/lib/string.c FORCE
> +	$(call if_changed_rule,cc_o_c)
> +
> +$(obj)/ctype.o: $(srctree)/lib/ctype.c FORCE
> +	$(call if_changed_rule,cc_o_c)
> +
> +obj-y		:= cmdline_early.pi.o string.pi.o ctype.pi.o lib-fdt.pi.o lib-fdt_ro.pi.o
> +extra-y		:= $(patsubst %.pi.o,%.o,$(obj-y))
> diff --git a/arch/riscv/kernel/pi/cmdline_early.c b/arch/riscv/kernel/pi/cmdline_early.c
> new file mode 100644
> index 000000000000..5ae7b853fa66
> --- /dev/null
> +++ b/arch/riscv/kernel/pi/cmdline_early.c
> @@ -0,0 +1,52 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +#include <linux/types.h>
> +#include <linux/init.h>
> +#include <linux/libfdt.h>
> +#include <linux/string.h>
> +#include <asm/pgtable.h>
> +#include <asm/setup.h>
> +
> +static char early_cmdline[COMMAND_LINE_SIZE] __initdata;
> +
> +static char * __init get_early_cmdline(uintptr_t dtb_pa)
> +{
> +	const char *fdt_cmdline = NULL;
> +	unsigned int fdt_cmdline_size = 0;
> +	int chosen_node;
> +
> +	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
> +		chosen_node = fdt_path_offset((void *)dtb_pa, "/chosen");
> +		if (chosen_node >= 0) {
> +			fdt_cmdline = fdt_getprop((void *)dtb_pa, chosen_node,
> +						  "bootargs", NULL);

Alex and I have been running some more tests, and fdt_cmdline is missing
a NULL-check here.

We'll need a v5 to fix this!


Björn

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

* Re: [PATCH v4 1/1] riscv: Allow to downgrade paging mode from the command line
@ 2023-02-20 13:10     ` Björn Töpel
  0 siblings, 0 replies; 16+ messages in thread
From: Björn Töpel @ 2023-02-20 13:10 UTC (permalink / raw)
  To: Alexandre Ghiti, Jonathan Corbet, Paul Walmsley, Palmer Dabbelt,
	Albert Ou, Conor Dooley, Ard Biesheuvel, linux-doc, linux-kernel,
	linux-riscv
  Cc: Alexandre Ghiti

Alexandre Ghiti <alexghiti@rivosinc.com> writes:

> Add 2 early command line parameters that allow to downgrade satp mode
> (using the same naming as x86):
> - "no5lvl": use a 4-level page table (down from sv57 to sv48)
> - "no4lvl": use a 3-level page table (down from sv57/sv48 to sv39)
>
> Note that going through the device tree to get the kernel command line
> works with ACPI too since the efi stub creates a device tree anyway with
> the command line.
>
> In KASAN kernels, we can't use the libfdt that early in the boot process
> since we are not ready to execute instrumented functions. So instead of
> using the "generic" libfdt, we compile our own versions of those functions
> that are not instrumented and that are prefixed so that they do not
> conflict with the generic ones. We also need the non-instrumented versions
> of the string functions and the prefixed versions of memcpy/memmove.
>
> This is largely inspired by commit aacd149b6238 ("arm64: head: avoid
> relocating the kernel twice for KASLR") from which I removed compilation
> flags that were not relevant to RISC-V at the moment (LTO, SCS, pie).
>
> Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
> ---
>  .../admin-guide/kernel-parameters.txt         |  5 +-
>  arch/riscv/kernel/Makefile                    |  2 +
>  arch/riscv/kernel/pi/Makefile                 | 34 ++++++++++++
>  arch/riscv/kernel/pi/cmdline_early.c          | 52 +++++++++++++++++++
>  arch/riscv/lib/memcpy.S                       |  2 +
>  arch/riscv/lib/memmove.S                      |  2 +
>  arch/riscv/mm/init.c                          | 36 ++++++++++---
>  7 files changed, 126 insertions(+), 7 deletions(-)
>  create mode 100644 arch/riscv/kernel/pi/Makefile
>  create mode 100644 arch/riscv/kernel/pi/cmdline_early.c
>
> diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
> index 6cfa6e3996cf..1d8fc66d2b0d 100644
> --- a/Documentation/admin-guide/kernel-parameters.txt
> +++ b/Documentation/admin-guide/kernel-parameters.txt
> @@ -3578,7 +3578,10 @@
>  			emulation library even if a 387 maths coprocessor
>  			is present.
>  
> -	no5lvl		[X86-64] Disable 5-level paging mode. Forces
> +	no4lvl		[RISCV] Disable 4-level and 5-level paging modes. Forces
> +			kernel to use 3-level paging instead.
> +
> +	no5lvl		[X86-64,RISCV] Disable 5-level paging mode. Forces
>  			kernel to use 4-level paging instead.
>  
>  	nofsgsbase	[X86] Disables FSGSBASE instructions.
> diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
> index 4cf303a779ab..6756209f1ac6 100644
> --- a/arch/riscv/kernel/Makefile
> +++ b/arch/riscv/kernel/Makefile
> @@ -89,3 +89,5 @@ obj-$(CONFIG_EFI)		+= efi.o
>  obj-$(CONFIG_COMPAT)		+= compat_syscall_table.o
>  obj-$(CONFIG_COMPAT)		+= compat_signal.o
>  obj-$(CONFIG_COMPAT)		+= compat_vdso/
> +
> +obj-y				+= pi/
> diff --git a/arch/riscv/kernel/pi/Makefile b/arch/riscv/kernel/pi/Makefile
> new file mode 100644
> index 000000000000..554e936ef0b6
> --- /dev/null
> +++ b/arch/riscv/kernel/pi/Makefile
> @@ -0,0 +1,34 @@
> +# SPDX-License-Identifier: GPL-2.0
> +# This file was copied from arm64/kernel/pi/Makefile.
> +
> +KBUILD_CFLAGS	:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
> +		   -Os -DDISABLE_BRANCH_PROFILING $(DISABLE_STACKLEAK_PLUGIN) \
> +		   $(call cc-option,-mbranch-protection=none) \
> +		   -I$(srctree)/scripts/dtc/libfdt -fno-stack-protector \
> +		   -D__DISABLE_EXPORTS -ffreestanding \
> +		   -fno-asynchronous-unwind-tables -fno-unwind-tables \
> +		   $(call cc-option,-fno-addrsig)
> +
> +GCOV_PROFILE	:= n
> +KASAN_SANITIZE	:= n
> +KCSAN_SANITIZE	:= n
> +UBSAN_SANITIZE	:= n
> +KCOV_INSTRUMENT	:= n
> +
> +$(obj)/%.pi.o: OBJCOPYFLAGS := --prefix-symbols=__pi_ \
> +			       --remove-section=.note.gnu.property \
> +			       --prefix-alloc-sections=.init
> +$(obj)/%.pi.o: $(obj)/%.o FORCE
> +	$(call if_changed,objcopy)
> +
> +$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
> +	$(call if_changed_rule,cc_o_c)
> +
> +$(obj)/string.o: $(srctree)/lib/string.c FORCE
> +	$(call if_changed_rule,cc_o_c)
> +
> +$(obj)/ctype.o: $(srctree)/lib/ctype.c FORCE
> +	$(call if_changed_rule,cc_o_c)
> +
> +obj-y		:= cmdline_early.pi.o string.pi.o ctype.pi.o lib-fdt.pi.o lib-fdt_ro.pi.o
> +extra-y		:= $(patsubst %.pi.o,%.o,$(obj-y))
> diff --git a/arch/riscv/kernel/pi/cmdline_early.c b/arch/riscv/kernel/pi/cmdline_early.c
> new file mode 100644
> index 000000000000..5ae7b853fa66
> --- /dev/null
> +++ b/arch/riscv/kernel/pi/cmdline_early.c
> @@ -0,0 +1,52 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +#include <linux/types.h>
> +#include <linux/init.h>
> +#include <linux/libfdt.h>
> +#include <linux/string.h>
> +#include <asm/pgtable.h>
> +#include <asm/setup.h>
> +
> +static char early_cmdline[COMMAND_LINE_SIZE] __initdata;
> +
> +static char * __init get_early_cmdline(uintptr_t dtb_pa)
> +{
> +	const char *fdt_cmdline = NULL;
> +	unsigned int fdt_cmdline_size = 0;
> +	int chosen_node;
> +
> +	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
> +		chosen_node = fdt_path_offset((void *)dtb_pa, "/chosen");
> +		if (chosen_node >= 0) {
> +			fdt_cmdline = fdt_getprop((void *)dtb_pa, chosen_node,
> +						  "bootargs", NULL);

Alex and I have been running some more tests, and fdt_cmdline is missing
a NULL-check here.

We'll need a v5 to fix this!


Björn

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

end of thread, other threads:[~2023-02-20 13:10 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-31 15:11 [PATCH v4 0/1] riscv: Allow to downgrade paging mode from the command line Alexandre Ghiti
2023-01-31 15:11 ` Alexandre Ghiti
2023-01-31 15:11 ` [PATCH v4 1/1] " Alexandre Ghiti
2023-01-31 15:11   ` Alexandre Ghiti
2023-02-20 13:10   ` Björn Töpel
2023-02-20 13:10     ` Björn Töpel
2023-01-31 15:34 ` [PATCH v4 0/1] " Alexandre Ghiti
2023-01-31 15:34   ` Alexandre Ghiti
2023-01-31 19:24 ` Conor Dooley
2023-01-31 19:24   ` Conor Dooley
2023-02-02 15:07   ` Alexandre Ghiti
2023-02-02 15:07     ` Alexandre Ghiti
2023-02-02 15:13     ` Alexandre Ghiti
2023-02-02 15:13       ` Alexandre Ghiti
2023-02-17 14:48 ` Björn Töpel
2023-02-17 14:48   ` Björn Töpel

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.