Skip to content

Commit d37823c

Browse files
chleroympe
authored andcommitted
powerpc/32s: Fix kasan_init_region() for KASAN
It has been reported some configuration where the kernel doesn't boot with KASAN enabled. This is due to wrong BAT allocation for the KASAN area: ---[ Data Block Address Translation ]--- 0: 0xc0000000-0xcfffffff 0x00000000 256M Kernel rw m 1: 0xd0000000-0xdfffffff 0x10000000 256M Kernel rw m 2: 0xe0000000-0xefffffff 0x20000000 256M Kernel rw m 3: 0xf8000000-0xf9ffffff 0x2a000000 32M Kernel rw m 4: 0xfa000000-0xfdffffff 0x2c000000 64M Kernel rw m A BAT must have both virtual and physical addresses alignment matching the size of the BAT. This is not the case for BAT 4 above. Fix kasan_init_region() by using block_size() function that is in book3s32/mmu.c. To be able to reuse it here, make it non static and change its name to bat_block_size() in order to avoid name conflict with block_size() defined in <linux/blkdev.h> Also reuse find_free_bat() to avoid an error message from setbat() when no BAT is available. And allocate memory outside of linear memory mapping to avoid wasting that precious space. With this change we get correct alignment for BATs and KASAN shadow memory is allocated outside the linear memory space. ---[ Data Block Address Translation ]--- 0: 0xc0000000-0xcfffffff 0x00000000 256M Kernel rw 1: 0xd0000000-0xdfffffff 0x10000000 256M Kernel rw 2: 0xe0000000-0xefffffff 0x20000000 256M Kernel rw 3: 0xf8000000-0xfbffffff 0x7c000000 64M Kernel rw 4: 0xfc000000-0xfdffffff 0x7a000000 32M Kernel rw Fixes: 7974c47 ("powerpc/32s: Implement dedicated kasan_init_region()") Cc: [email protected] Reported-by: Maxime Bizon <[email protected]> Signed-off-by: Christophe Leroy <[email protected]> Tested-by: Maxime Bizon <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/7a50ef902494d1325227d47d33dada01e52e5518.1641818726.git.christophe.leroy@csgroup.eu
1 parent 87b9d74 commit d37823c

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

arch/powerpc/include/asm/book3s/32/mmu-hash.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ static __always_inline void update_user_segments(u32 val)
223223
update_user_segment(15, val);
224224
}
225225

226+
int __init find_free_bat(void);
227+
unsigned int bat_block_size(unsigned long base, unsigned long top);
226228
#endif /* !__ASSEMBLY__ */
227229

228230
/* We happily ignore the smaller BATs on 601, we don't actually use

arch/powerpc/mm/book3s32/mmu.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ unsigned long p_block_mapped(phys_addr_t pa)
7676
return 0;
7777
}
7878

79-
static int __init find_free_bat(void)
79+
int __init find_free_bat(void)
8080
{
8181
int b;
8282
int n = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
@@ -100,7 +100,7 @@ static int __init find_free_bat(void)
100100
* - block size has to be a power of two. This is calculated by finding the
101101
* highest bit set to 1.
102102
*/
103-
static unsigned int block_size(unsigned long base, unsigned long top)
103+
unsigned int bat_block_size(unsigned long base, unsigned long top)
104104
{
105105
unsigned int max_size = SZ_256M;
106106
unsigned int base_shift = (ffs(base) - 1) & 31;
@@ -145,7 +145,7 @@ static unsigned long __init __mmu_mapin_ram(unsigned long base, unsigned long to
145145
int idx;
146146

147147
while ((idx = find_free_bat()) != -1 && base != top) {
148-
unsigned int size = block_size(base, top);
148+
unsigned int size = bat_block_size(base, top);
149149

150150
if (size < 128 << 10)
151151
break;
@@ -201,12 +201,12 @@ void mmu_mark_initmem_nx(void)
201201
unsigned long size;
202202

203203
for (i = 0; i < nb - 1 && base < top;) {
204-
size = block_size(base, top);
204+
size = bat_block_size(base, top);
205205
setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT);
206206
base += size;
207207
}
208208
if (base < top) {
209-
size = block_size(base, top);
209+
size = bat_block_size(base, top);
210210
if ((top - base) > size) {
211211
size <<= 1;
212212
if (strict_kernel_rwx_enabled() && base + size > border)

arch/powerpc/mm/kasan/book3s_32.c

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,51 @@ int __init kasan_init_region(void *start, size_t size)
1010
{
1111
unsigned long k_start = (unsigned long)kasan_mem_to_shadow(start);
1212
unsigned long k_end = (unsigned long)kasan_mem_to_shadow(start + size);
13-
unsigned long k_cur = k_start;
14-
int k_size = k_end - k_start;
15-
int k_size_base = 1 << (ffs(k_size) - 1);
13+
unsigned long k_nobat = k_start;
14+
unsigned long k_cur;
15+
phys_addr_t phys;
1616
int ret;
17-
void *block;
1817

19-
block = memblock_alloc(k_size, k_size_base);
20-
21-
if (block && k_size_base >= SZ_128K && k_start == ALIGN(k_start, k_size_base)) {
22-
int shift = ffs(k_size - k_size_base);
23-
int k_size_more = shift ? 1 << (shift - 1) : 0;
24-
25-
setbat(-1, k_start, __pa(block), k_size_base, PAGE_KERNEL);
26-
if (k_size_more >= SZ_128K)
27-
setbat(-1, k_start + k_size_base, __pa(block) + k_size_base,
28-
k_size_more, PAGE_KERNEL);
29-
if (v_block_mapped(k_start))
30-
k_cur = k_start + k_size_base;
31-
if (v_block_mapped(k_start + k_size_base))
32-
k_cur = k_start + k_size_base + k_size_more;
33-
34-
update_bats();
18+
while (k_nobat < k_end) {
19+
unsigned int k_size = bat_block_size(k_nobat, k_end);
20+
int idx = find_free_bat();
21+
22+
if (idx == -1)
23+
break;
24+
if (k_size < SZ_128K)
25+
break;
26+
phys = memblock_phys_alloc_range(k_size, k_size, 0,
27+
MEMBLOCK_ALLOC_ANYWHERE);
28+
if (!phys)
29+
break;
30+
31+
setbat(idx, k_nobat, phys, k_size, PAGE_KERNEL);
32+
k_nobat += k_size;
3533
}
34+
if (k_nobat != k_start)
35+
update_bats();
3636

37-
if (!block)
38-
block = memblock_alloc(k_size, PAGE_SIZE);
39-
if (!block)
40-
return -ENOMEM;
37+
if (k_nobat < k_end) {
38+
phys = memblock_phys_alloc_range(k_end - k_nobat, PAGE_SIZE, 0,
39+
MEMBLOCK_ALLOC_ANYWHERE);
40+
if (!phys)
41+
return -ENOMEM;
42+
}
4143

4244
ret = kasan_init_shadow_page_tables(k_start, k_end);
4345
if (ret)
4446
return ret;
4547

46-
kasan_update_early_region(k_start, k_cur, __pte(0));
48+
kasan_update_early_region(k_start, k_nobat, __pte(0));
4749

48-
for (; k_cur < k_end; k_cur += PAGE_SIZE) {
50+
for (k_cur = k_nobat; k_cur < k_end; k_cur += PAGE_SIZE) {
4951
pmd_t *pmd = pmd_off_k(k_cur);
50-
void *va = block + k_cur - k_start;
51-
pte_t pte = pfn_pte(PHYS_PFN(__pa(va)), PAGE_KERNEL);
52+
pte_t pte = pfn_pte(PHYS_PFN(phys + k_cur - k_nobat), PAGE_KERNEL);
5253

5354
__set_pte_at(&init_mm, k_cur, pte_offset_kernel(pmd, k_cur), pte, 0);
5455
}
5556
flush_tlb_kernel_range(k_start, k_end);
57+
memset(kasan_mem_to_shadow(start), 0, k_end - k_start);
58+
5659
return 0;
5760
}

0 commit comments

Comments
 (0)