mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] riscv: Allow to downgrade paging mode from the command line
@ 2022-12-14 14:33 Alexandre Ghiti
  2022-12-14 15:38 ` Björn Töpel
  0 siblings, 1 reply; 3+ messages in thread
From: Alexandre Ghiti @ 2022-12-14 14:33 UTC (permalink / raw)
  To: Jonathan Corbet, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	linux-doc, linux-kernel, linux-riscv
  Cc: Alexandre Ghiti

Add 2 early command line parameters called "no5lvl" and "no4lvl" (using
the same naming as x86) to allow a user to downgrade from sv57 (the
default mode if the hardware supports it) to sv48 or 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.

Also, as those params are treated very early in the boot process and we
use standard device tree functions that may be kasan instrumented, we
only enable them for !KASAN configurations.

Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
---

Tested on both DT/ACPI kernel successfully

 .../admin-guide/kernel-parameters.txt         |  5 ++-
 arch/riscv/mm/init.c                          | 43 ++++++++++++++++++-
 2 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index a465d5242774..6741524aa980 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3548,7 +3548,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 paging mode. 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/mm/init.c b/arch/riscv/mm/init.c
index b56a0a75533f..8140fefe0e57 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -752,12 +752,35 @@ static void __init disable_pgtable_l4(void)
  * 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;
 
+#ifndef CONFIG_KASAN
+	/*
+	 * The below fdt functions are kasan instrumented, since at this point
+	 * there is no mapping for the kasan shadow memory, this can't be used
+	 * when kasan is enabled.
+	 */
+	int chosen_node;
+
+	chosen_node = fdt_path_offset((void *)dtb_pa, "/chosen");
+	if (chosen_node >= 0) {
+		const char *cmdline = fdt_getprop((void *)dtb_pa, chosen_node,
+						  "bootargs", NULL);
+		if (strstr(cmdline, "no5lvl")) {
+			disable_pgtable_l5();
+			check_l4 = true;
+		} else if (strstr(cmdline, "no4lvl")) {
+			disable_pgtable_l5();
+			disable_pgtable_l4();
+			return;
+		}
+	}
+#endif
+
 	create_p4d_mapping(early_p4d,
 			set_satp_mode_pmd, (uintptr_t)early_pud,
 			P4D_SIZE, PAGE_TABLE);
@@ -800,6 +823,22 @@ static __init void set_satp_mode(void)
 	memset(early_pud, 0, PAGE_SIZE);
 	memset(early_pmd, 0, PAGE_SIZE);
 }
+
+#ifndef CONFIG_KASAN
+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);
+#endif
 #endif
 
 /*
@@ -979,7 +1018,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 = PAGE_OFFSET - kernel_map.phys_addr;
-- 
2.37.2


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

* Re: [PATCH] riscv: Allow to downgrade paging mode from the command line
  2022-12-14 14:33 [PATCH] riscv: Allow to downgrade paging mode from the command line Alexandre Ghiti
@ 2022-12-14 15:38 ` Björn Töpel
  2022-12-14 16:38   ` Alexandre Ghiti
  0 siblings, 1 reply; 3+ messages in thread
From: Björn Töpel @ 2022-12-14 15:38 UTC (permalink / raw)
  To: Alexandre Ghiti, Jonathan Corbet, Paul Walmsley, Palmer Dabbelt,
	Albert Ou, linux-doc, linux-kernel, linux-riscv
  Cc: Alexandre Ghiti

Alexandre Ghiti <alexghiti@rivosinc.com> writes:

> diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
> index b56a0a75533f..8140fefe0e57 100644
> --- a/arch/riscv/mm/init.c
> +++ b/arch/riscv/mm/init.c
> @@ -752,12 +752,35 @@ static void __init disable_pgtable_l4(void)
>   * 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;
>  
> +#ifndef CONFIG_KASAN
> +	/*
> +	 * The below fdt functions are kasan instrumented, since at this point
> +	 * there is no mapping for the kasan shadow memory, this can't be used
> +	 * when kasan is enabled.
> +	 */
> +	int chosen_node;
> +
> +	chosen_node = fdt_path_offset((void *)dtb_pa, "/chosen");
> +	if (chosen_node >= 0) {
> +		const char *cmdline = fdt_getprop((void *)dtb_pa, chosen_node,
> +						  "bootargs", NULL);

The command line handling needs to honor CONFIG_CMDLINE_FORCE and
CONFIG_CMDLINE_EXTEND (which makes it possible to use no{4,5}lvl *and*
KASAN ;-)).


Björn

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

* Re: [PATCH] riscv: Allow to downgrade paging mode from the command line
  2022-12-14 15:38 ` Björn Töpel
@ 2022-12-14 16:38   ` Alexandre Ghiti
  0 siblings, 0 replies; 3+ messages in thread
From: Alexandre Ghiti @ 2022-12-14 16:38 UTC (permalink / raw)
  To: Björn Töpel
  Cc: Jonathan Corbet, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	linux-doc, linux-kernel, linux-riscv

On Wed, Dec 14, 2022 at 4:39 PM Björn Töpel <bjorn@kernel.org> wrote:
>
> Alexandre Ghiti <alexghiti@rivosinc.com> writes:
>
> > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
> > index b56a0a75533f..8140fefe0e57 100644
> > --- a/arch/riscv/mm/init.c
> > +++ b/arch/riscv/mm/init.c
> > @@ -752,12 +752,35 @@ static void __init disable_pgtable_l4(void)
> >   * 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;
> >
> > +#ifndef CONFIG_KASAN
> > +     /*
> > +      * The below fdt functions are kasan instrumented, since at this point
> > +      * there is no mapping for the kasan shadow memory, this can't be used
> > +      * when kasan is enabled.
> > +      */
> > +     int chosen_node;
> > +
> > +     chosen_node = fdt_path_offset((void *)dtb_pa, "/chosen");
> > +     if (chosen_node >= 0) {
> > +             const char *cmdline = fdt_getprop((void *)dtb_pa, chosen_node,
> > +                                               "bootargs", NULL);
>
> The command line handling needs to honor CONFIG_CMDLINE_FORCE and
> CONFIG_CMDLINE_EXTEND (which makes it possible to use no{4,5}lvl *and*
> KASAN ;-)).
>

Indeed, I forgot that, thanks for noticing! I'll prepare the "real"
command line before matching the new parameters.

Thanks again,

Alex

>
> Björn

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

end of thread, other threads:[~2022-12-14 16:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-14 14:33 [PATCH] riscv: Allow to downgrade paging mode from the command line Alexandre Ghiti
2022-12-14 15:38 ` Björn Töpel
2022-12-14 16:38   ` Alexandre Ghiti

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®