mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH -fixes] riscv: Check the virtual alignment before choosing a map size
@ 2023-06-07 12:58 Alexandre Ghiti
  2023-06-07 13:09 ` Jessica Clarke
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Alexandre Ghiti @ 2023-06-07 12:58 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Andrew Jones,
	Alexandre Ghiti, Rob Herring, Anup Patel, linux-riscv,
	linux-kernel
  Cc: Song Shuai

We used to only check the alignment of the physical address to decide
which mapping would fit for a certain region of the linear mapping, but
it is not enough since the virtual address must also be aligned, so check
that too.

Fixes: 3335068f8721 ("riscv: Use PUD/P4D/PGD pages for the linear mapping")
Reported-by: Song Shuai <songshuaishuai@tinylab.org>
Link: https://lore.kernel.org/linux-riscv/tencent_7C3B580B47C1B17C16488EC1@qq.com/
Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
---
 arch/riscv/mm/init.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index d42ea31c7de0..5143eef4c074 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -660,18 +660,19 @@ void __init create_pgd_mapping(pgd_t *pgdp,
 	create_pgd_next_mapping(nextp, va, pa, sz, prot);
 }
 
-static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
+static uintptr_t __init best_map_size(phys_addr_t pa, uintptr_t va,
+				      phys_addr_t size)
 {
-	if (!(base & (PGDIR_SIZE - 1)) && size >= PGDIR_SIZE)
+	if (!(pa & (PGDIR_SIZE - 1)) && !(va & (PGDIR_SIZE - 1)) && size >= PGDIR_SIZE)
 		return PGDIR_SIZE;
 
-	if (!(base & (P4D_SIZE - 1)) && size >= P4D_SIZE)
+	if (!(pa & (P4D_SIZE - 1)) && !(va & (P4D_SIZE - 1)) && size >= P4D_SIZE)
 		return P4D_SIZE;
 
-	if (!(base & (PUD_SIZE - 1)) && size >= PUD_SIZE)
+	if (!(pa & (PUD_SIZE - 1)) && !(va & (PUD_SIZE - 1)) && size >= PUD_SIZE)
 		return PUD_SIZE;
 
-	if (!(base & (PMD_SIZE - 1)) && size >= PMD_SIZE)
+	if (!(pa & (PMD_SIZE - 1)) && !(va & (PMD_SIZE - 1)) && size >= PMD_SIZE)
 		return PMD_SIZE;
 
 	return PAGE_SIZE;
@@ -1177,7 +1178,7 @@ static void __init create_linear_mapping_range(phys_addr_t start,
 	for (pa = start; pa < end; pa += map_size) {
 		va = (uintptr_t)__va(pa);
 		map_size = fixed_map_size ? fixed_map_size :
-					    best_map_size(pa, end - pa);
+					    best_map_size(pa, va, end - pa);
 
 		create_pgd_mapping(swapper_pg_dir, va, pa, map_size,
 				   pgprot_from_va(va));
-- 
2.39.2


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

* Re: [PATCH -fixes] riscv: Check the virtual alignment before choosing a map size
  2023-06-07 12:58 [PATCH -fixes] riscv: Check the virtual alignment before choosing a map size Alexandre Ghiti
@ 2023-06-07 13:09 ` Jessica Clarke
  2023-06-08 14:04 ` Palmer Dabbelt
  2023-06-08 14:10 ` patchwork-bot+linux-riscv
  2 siblings, 0 replies; 4+ messages in thread
From: Jessica Clarke @ 2023-06-07 13:09 UTC (permalink / raw)
  To: Alexandre Ghiti
  Cc: Paul Walmsley, Palmer Dabbelt, Albert Ou, Andrew Jones,
	Rob Herring, Anup Patel, linux-riscv, linux-kernel, Song Shuai

On 7 Jun 2023, at 13:58, Alexandre Ghiti <alexghiti@rivosinc.com> wrote:
> 
> We used to only check the alignment of the physical address to decide
> which mapping would fit for a certain region of the linear mapping, but
> it is not enough since the virtual address must also be aligned, so check
> that too.
> 
> Fixes: 3335068f8721 ("riscv: Use PUD/P4D/PGD pages for the linear mapping")
> Reported-by: Song Shuai <songshuaishuai@tinylab.org>
> Link: https://lore.kernel.org/linux-riscv/tencent_7C3B580B47C1B17C16488EC1@qq.com/
> Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
> ---
> arch/riscv/mm/init.c | 13 +++++++------
> 1 file changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
> index d42ea31c7de0..5143eef4c074 100644
> --- a/arch/riscv/mm/init.c
> +++ b/arch/riscv/mm/init.c
> @@ -660,18 +660,19 @@ void __init create_pgd_mapping(pgd_t *pgdp,
> create_pgd_next_mapping(nextp, va, pa, sz, prot);
> }
> 
> -static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
> +static uintptr_t __init best_map_size(phys_addr_t pa, uintptr_t va,

Why on earth is this returning uintptr_t? It’s a size not a pointer.

Jess

> +      phys_addr_t size)
> {
> - if (!(base & (PGDIR_SIZE - 1)) && size >= PGDIR_SIZE)
> + if (!(pa & (PGDIR_SIZE - 1)) && !(va & (PGDIR_SIZE - 1)) && size >= PGDIR_SIZE)
> return PGDIR_SIZE;
> 
> - if (!(base & (P4D_SIZE - 1)) && size >= P4D_SIZE)
> + if (!(pa & (P4D_SIZE - 1)) && !(va & (P4D_SIZE - 1)) && size >= P4D_SIZE)
> return P4D_SIZE;
> 
> - if (!(base & (PUD_SIZE - 1)) && size >= PUD_SIZE)
> + if (!(pa & (PUD_SIZE - 1)) && !(va & (PUD_SIZE - 1)) && size >= PUD_SIZE)
> return PUD_SIZE;
> 
> - if (!(base & (PMD_SIZE - 1)) && size >= PMD_SIZE)
> + if (!(pa & (PMD_SIZE - 1)) && !(va & (PMD_SIZE - 1)) && size >= PMD_SIZE)
> return PMD_SIZE;
> 
> return PAGE_SIZE;
> @@ -1177,7 +1178,7 @@ static void __init create_linear_mapping_range(phys_addr_t start,
> for (pa = start; pa < end; pa += map_size) {
> va = (uintptr_t)__va(pa);
> map_size = fixed_map_size ? fixed_map_size :
> -    best_map_size(pa, end - pa);
> +    best_map_size(pa, va, end - pa);
> 
> create_pgd_mapping(swapper_pg_dir, va, pa, map_size,
>   pgprot_from_va(va));
> -- 
> 2.39.2
> 
> 
> _______________________________________________
> linux-riscv mailing list
> linux-riscv@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-riscv


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

* Re: [PATCH -fixes] riscv: Check the virtual alignment before choosing a map size
  2023-06-07 12:58 [PATCH -fixes] riscv: Check the virtual alignment before choosing a map size Alexandre Ghiti
  2023-06-07 13:09 ` Jessica Clarke
@ 2023-06-08 14:04 ` Palmer Dabbelt
  2023-06-08 14:10 ` patchwork-bot+linux-riscv
  2 siblings, 0 replies; 4+ messages in thread
From: Palmer Dabbelt @ 2023-06-08 14:04 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Andrew Jones,
	Rob Herring, Anup Patel, linux-riscv, linux-kernel,
	Alexandre Ghiti
  Cc: Song Shuai


On Wed, 07 Jun 2023 14:58:51 +0200, Alexandre Ghiti wrote:
> We used to only check the alignment of the physical address to decide
> which mapping would fit for a certain region of the linear mapping, but
> it is not enough since the virtual address must also be aligned, so check
> that too.
> 
> 

Applied, thanks!

[1/1] riscv: Check the virtual alignment before choosing a map size
      https://git.kernel.org/palmer/c/49a0a3731596

Best regards,
-- 
Palmer Dabbelt <palmer@rivosinc.com>


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

* Re: [PATCH -fixes] riscv: Check the virtual alignment before choosing a map size
  2023-06-07 12:58 [PATCH -fixes] riscv: Check the virtual alignment before choosing a map size Alexandre Ghiti
  2023-06-07 13:09 ` Jessica Clarke
  2023-06-08 14:04 ` Palmer Dabbelt
@ 2023-06-08 14:10 ` patchwork-bot+linux-riscv
  2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+linux-riscv @ 2023-06-08 14:10 UTC (permalink / raw)
  To: Alexandre Ghiti
  Cc: linux-riscv, paul.walmsley, palmer, aou, ajones, robh, anup,
	linux-kernel, songshuaishuai

Hello:

This patch was applied to riscv/linux.git (fixes)
by Palmer Dabbelt <palmer@rivosinc.com>:

On Wed,  7 Jun 2023 14:58:51 +0200 you wrote:
> We used to only check the alignment of the physical address to decide
> which mapping would fit for a certain region of the linear mapping, but
> it is not enough since the virtual address must also be aligned, so check
> that too.
> 
> Fixes: 3335068f8721 ("riscv: Use PUD/P4D/PGD pages for the linear mapping")
> Reported-by: Song Shuai <songshuaishuai@tinylab.org>
> Link: https://lore.kernel.org/linux-riscv/tencent_7C3B580B47C1B17C16488EC1@qq.com/
> Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
> 
> [...]

Here is the summary with links:
  - [-fixes] riscv: Check the virtual alignment before choosing a map size
    https://git.kernel.org/riscv/c/49a0a3731596

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2023-06-08 14:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-07 12:58 [PATCH -fixes] riscv: Check the virtual alignment before choosing a map size Alexandre Ghiti
2023-06-07 13:09 ` Jessica Clarke
2023-06-08 14:04 ` Palmer Dabbelt
2023-06-08 14:10 ` patchwork-bot+linux-riscv

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®