linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] riscv: Only extend kernel reservation if mapped read-only
@ 2021-04-29 15:05 Geert Uytterhoeven
  2021-05-02 14:05 ` Alex Ghiti
  2021-05-06  6:41 ` Palmer Dabbelt
  0 siblings, 2 replies; 3+ messages in thread
From: Geert Uytterhoeven @ 2021-04-29 15:05 UTC (permalink / raw)
  To: Alexandre Ghiti, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: Damien Le Moal, Arnd Bergmann, linux-riscv, linux-kernel,
	Geert Uytterhoeven

When the kernel mapping was moved outside of the linear mapping, the
kernel memory reservation was increased, to take into account mapping
granularity.  However, this is done unconditionally, regardless of
whether the kernel memory is mapped read-only or not.

If this extension is not needed, up to 2 MiB may be lost, which has a
big impact on e.g. Canaan K210 (64-bit nommu) platforms with only 8 MiB
of RAM.

Reclaim the lost memory by only extending the reserved region when
needed, i.e. depending on a simplified version of the conditional logic
around the call to protect_kernel_linear_mapping_text_rodata().

Fixes: 2bfc6cd81bd17e43 ("riscv: Move kernel mapping outside of linear mapping")
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
---
v2:
  - Simplify the conditional, as STRICT_KERNEL_RWX depends on
    MMU && !XIP_KERNEL.

Only tested on K210 (SiPeed MAIX BiT):

    -Memory: 5852K/8192K available (1344K kernel code, 147K rwdata, 272K rodata, 106K init, 72K bss, 2340K reserved, 0K cma-reserved)
    +Memory: 5948K/8192K available (1344K kernel code, 147K rwdata, 272K rodata, 106K init, 72K bss, 2244K reserved, 0K cma-reserved)

Yes, I was lucky, as only 96 KiB was lost ;-)
---
 arch/riscv/mm/init.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index 788eb222deacf994..3ebc0f5d2b73b42b 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -136,11 +136,16 @@ void __init setup_bootmem(void)
 
 	/*
 	 * Reserve from the start of the kernel to the end of the kernel
-	 * and make sure we align the reservation on PMD_SIZE since we will
+	 */
+#if defined(CONFIG_64BIT) && defined(CONFIG_STRICT_KERNEL_RWX)
+	/*
+	 * Make sure we align the reservation on PMD_SIZE since we will
 	 * map the kernel in the linear mapping as read-only: we do not want
 	 * any allocation to happen between _end and the next pmd aligned page.
 	 */
-	memblock_reserve(vmlinux_start, (vmlinux_end - vmlinux_start + PMD_SIZE - 1) & PMD_MASK);
+	vmlinux_end = (vmlinux_end + PMD_SIZE - 1) & PMD_MASK;
+#endif
+	memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
 
 	/*
 	 * memblock allocator is not aware of the fact that last 4K bytes of
-- 
2.25.1


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

* Re: [PATCH v2] riscv: Only extend kernel reservation if mapped read-only
  2021-04-29 15:05 [PATCH v2] riscv: Only extend kernel reservation if mapped read-only Geert Uytterhoeven
@ 2021-05-02 14:05 ` Alex Ghiti
  2021-05-06  6:41 ` Palmer Dabbelt
  1 sibling, 0 replies; 3+ messages in thread
From: Alex Ghiti @ 2021-05-02 14:05 UTC (permalink / raw)
  To: Geert Uytterhoeven, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: Damien Le Moal, Arnd Bergmann, linux-riscv, linux-kernel

Le 4/29/21 à 11:05 AM, Geert Uytterhoeven a écrit :
> When the kernel mapping was moved outside of the linear mapping, the
> kernel memory reservation was increased, to take into account mapping
> granularity.  However, this is done unconditionally, regardless of
> whether the kernel memory is mapped read-only or not.
> 
> If this extension is not needed, up to 2 MiB may be lost, which has a
> big impact on e.g. Canaan K210 (64-bit nommu) platforms with only 8 MiB
> of RAM.
> 
> Reclaim the lost memory by only extending the reserved region when
> needed, i.e. depending on a simplified version of the conditional logic
> around the call to protect_kernel_linear_mapping_text_rodata().
> 
> Fixes: 2bfc6cd81bd17e43 ("riscv: Move kernel mapping outside of linear mapping")
> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> ---
> v2:
>    - Simplify the conditional, as STRICT_KERNEL_RWX depends on
>      MMU && !XIP_KERNEL.
> 
> Only tested on K210 (SiPeed MAIX BiT):
> 
>      -Memory: 5852K/8192K available (1344K kernel code, 147K rwdata, 272K rodata, 106K init, 72K bss, 2340K reserved, 0K cma-reserved)
>      +Memory: 5948K/8192K available (1344K kernel code, 147K rwdata, 272K rodata, 106K init, 72K bss, 2244K reserved, 0K cma-reserved)
> 
> Yes, I was lucky, as only 96 KiB was lost ;-)
> ---
>   arch/riscv/mm/init.c | 9 +++++++--
>   1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
> index 788eb222deacf994..3ebc0f5d2b73b42b 100644
> --- a/arch/riscv/mm/init.c
> +++ b/arch/riscv/mm/init.c
> @@ -136,11 +136,16 @@ void __init setup_bootmem(void)
>   
>   	/*
>   	 * Reserve from the start of the kernel to the end of the kernel
> -	 * and make sure we align the reservation on PMD_SIZE since we will
> +	 */
> +#if defined(CONFIG_64BIT) && defined(CONFIG_STRICT_KERNEL_RWX)
> +	/*
> +	 * Make sure we align the reservation on PMD_SIZE since we will
>   	 * map the kernel in the linear mapping as read-only: we do not want
>   	 * any allocation to happen between _end and the next pmd aligned page.
>   	 */
> -	memblock_reserve(vmlinux_start, (vmlinux_end - vmlinux_start + PMD_SIZE - 1) & PMD_MASK);
> +	vmlinux_end = (vmlinux_end + PMD_SIZE - 1) & PMD_MASK;
> +#endif
> +	memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
>   
>   	/*
>   	 * memblock allocator is not aware of the fact that last 4K bytes of
> 

I tested this on the following configs:

- rv32_defconfig (build and valid on qemu)
- defconfig (with and without CONFIG_STRICT_KERNEL_RWX) (build and valid 
on qemu)
- xip kernel (build and valid on qemu)
- nommu_k210_defconfig (build only)

so you can add:

Tested-by: Alexandre Ghiti <alex@ghiti.fr>

Thank you again for that,

Alex

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

* Re: [PATCH v2] riscv: Only extend kernel reservation if mapped read-only
  2021-04-29 15:05 [PATCH v2] riscv: Only extend kernel reservation if mapped read-only Geert Uytterhoeven
  2021-05-02 14:05 ` Alex Ghiti
@ 2021-05-06  6:41 ` Palmer Dabbelt
  1 sibling, 0 replies; 3+ messages in thread
From: Palmer Dabbelt @ 2021-05-06  6:41 UTC (permalink / raw)
  To: geert+renesas
  Cc: alex, Paul Walmsley, aou, Damien Le Moal, Arnd Bergmann,
	linux-riscv, linux-kernel, geert+renesas

On Thu, 29 Apr 2021 08:05:00 PDT (-0700), geert+renesas@glider.be wrote:
> When the kernel mapping was moved outside of the linear mapping, the
> kernel memory reservation was increased, to take into account mapping
> granularity.  However, this is done unconditionally, regardless of
> whether the kernel memory is mapped read-only or not.
>
> If this extension is not needed, up to 2 MiB may be lost, which has a
> big impact on e.g. Canaan K210 (64-bit nommu) platforms with only 8 MiB
> of RAM.
>
> Reclaim the lost memory by only extending the reserved region when
> needed, i.e. depending on a simplified version of the conditional logic
> around the call to protect_kernel_linear_mapping_text_rodata().
>
> Fixes: 2bfc6cd81bd17e43 ("riscv: Move kernel mapping outside of linear mapping")
> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> ---
> v2:
>   - Simplify the conditional, as STRICT_KERNEL_RWX depends on
>     MMU && !XIP_KERNEL.
>
> Only tested on K210 (SiPeed MAIX BiT):
>
>     -Memory: 5852K/8192K available (1344K kernel code, 147K rwdata, 272K rodata, 106K init, 72K bss, 2340K reserved, 0K cma-reserved)
>     +Memory: 5948K/8192K available (1344K kernel code, 147K rwdata, 272K rodata, 106K init, 72K bss, 2244K reserved, 0K cma-reserved)
>
> Yes, I was lucky, as only 96 KiB was lost ;-)
> ---
>  arch/riscv/mm/init.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
> index 788eb222deacf994..3ebc0f5d2b73b42b 100644
> --- a/arch/riscv/mm/init.c
> +++ b/arch/riscv/mm/init.c
> @@ -136,11 +136,16 @@ void __init setup_bootmem(void)
>
>  	/*
>  	 * Reserve from the start of the kernel to the end of the kernel
> -	 * and make sure we align the reservation on PMD_SIZE since we will
> +	 */
> +#if defined(CONFIG_64BIT) && defined(CONFIG_STRICT_KERNEL_RWX)
> +	/*
> +	 * Make sure we align the reservation on PMD_SIZE since we will
>  	 * map the kernel in the linear mapping as read-only: we do not want
>  	 * any allocation to happen between _end and the next pmd aligned page.
>  	 */
> -	memblock_reserve(vmlinux_start, (vmlinux_end - vmlinux_start + PMD_SIZE - 1) & PMD_MASK);
> +	vmlinux_end = (vmlinux_end + PMD_SIZE - 1) & PMD_MASK;
> +#endif
> +	memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
>
>  	/*
>  	 * memblock allocator is not aware of the fact that last 4K bytes of

Thanks, this is on for-next

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

end of thread, other threads:[~2021-05-06  6:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-29 15:05 [PATCH v2] riscv: Only extend kernel reservation if mapped read-only Geert Uytterhoeven
2021-05-02 14:05 ` Alex Ghiti
2021-05-06  6:41 ` Palmer Dabbelt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).