Skip to content

Commit 49a0a37

Browse files
Alexandre Ghitipalmer-dabbelt
authored andcommitted
riscv: Check the virtual alignment before choosing a map size
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: 3335068 ("riscv: Use PUD/P4D/PGD pages for the linear mapping") Reported-by: Song Shuai <[email protected]> Link: https://lore.kernel.org/linux-riscv/[email protected]/ Signed-off-by: Alexandre Ghiti <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 25abe0d commit 49a0a37

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

arch/riscv/mm/init.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,18 +660,19 @@ void __init create_pgd_mapping(pgd_t *pgdp,
660660
create_pgd_next_mapping(nextp, va, pa, sz, prot);
661661
}
662662

663-
static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
663+
static uintptr_t __init best_map_size(phys_addr_t pa, uintptr_t va,
664+
phys_addr_t size)
664665
{
665-
if (!(base & (PGDIR_SIZE - 1)) && size >= PGDIR_SIZE)
666+
if (!(pa & (PGDIR_SIZE - 1)) && !(va & (PGDIR_SIZE - 1)) && size >= PGDIR_SIZE)
666667
return PGDIR_SIZE;
667668

668-
if (!(base & (P4D_SIZE - 1)) && size >= P4D_SIZE)
669+
if (!(pa & (P4D_SIZE - 1)) && !(va & (P4D_SIZE - 1)) && size >= P4D_SIZE)
669670
return P4D_SIZE;
670671

671-
if (!(base & (PUD_SIZE - 1)) && size >= PUD_SIZE)
672+
if (!(pa & (PUD_SIZE - 1)) && !(va & (PUD_SIZE - 1)) && size >= PUD_SIZE)
672673
return PUD_SIZE;
673674

674-
if (!(base & (PMD_SIZE - 1)) && size >= PMD_SIZE)
675+
if (!(pa & (PMD_SIZE - 1)) && !(va & (PMD_SIZE - 1)) && size >= PMD_SIZE)
675676
return PMD_SIZE;
676677

677678
return PAGE_SIZE;
@@ -1177,7 +1178,7 @@ static void __init create_linear_mapping_range(phys_addr_t start,
11771178
for (pa = start; pa < end; pa += map_size) {
11781179
va = (uintptr_t)__va(pa);
11791180
map_size = fixed_map_size ? fixed_map_size :
1180-
best_map_size(pa, end - pa);
1181+
best_map_size(pa, va, end - pa);
11811182

11821183
create_pgd_mapping(swapper_pg_dir, va, pa, map_size,
11831184
pgprot_from_va(va));

0 commit comments

Comments
 (0)