kasan: support backing vmalloc space for riscv
v2 cover kasan: support backing vmalloc space for riscv
v1: https://lore.kernel.org/patchwork/cover/1364392/
v2:
1) Fix checkpatch issues.
2) Remove set_pmd and pmd_populate because it's not necessary.
Nylon Chen (1):
riscv/kasan: add KASAN_VMALLOC support
arch/riscv/Kconfig | 1 + arch/riscv/mm/kasan_init.c | 57 +++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-)
Signature · 7 lines
2.17.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v2 1/1 riscv/kasan: add KASAN_VMALLOC support
It references to x86/s390 architecture.
So, it doesn't map the early shadow page to cover VMALLOC space.
Prepopulate top level page table for the range that would otherwise be empty.
lower levels are filled dynamically upon memory allocation while booting.
--- arch/riscv/Kconfig | 1 + arch/riscv/mm/kasan_init.c | 57 +++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-)
arch/riscv/Kconfig +1 -0
@@ -57,6 +57,7 @@ config RISCV select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if MMU && 64BIT + select HAVE_ARCH_KASAN_VMALLOC if MMU && 64BIT select HAVE_ARCH_KGDB select HAVE_ARCH_KGDB_QXFER_PKT select HAVE_ARCH_MMAP_RND_BITS if MMU
arch/riscv/mm/kasan_init.c +56 -1
75 lines not on this page · raw message
Signature · 7 lines
2.17.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Palmer Dabbelt
maintainerRe: [PATCH v2 1/1] riscv/kasan: add KASAN_VMALLOC support
Quoted 101 lines · On Fri, 15 Jan 2021 21:58:35 PST (-0800), [email protected] wrote:
It references to x86/s390 architecture.
So, it doesn't map the early shadow page to cover VMALLOC space.
Prepopulate top level page table for the range that would otherwise be empty.
lower levels are filled dynamically upon memory allocation while booting.
Signed-off-by Nylon Chen <[email protected]>Signed-off-by Nick Hu <[email protected]>--- arch/riscv/Kconfig | 1 + arch/riscv/mm/kasan_init.c | 57 +++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-)arch/riscv/Kconfig +1
-0@@ -57,6 +57,7 @@ config RISCV select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if MMU && 64BIT + select HAVE_ARCH_KASAN_VMALLOC if MMU && 64BIT select HAVE_ARCH_KGDB select HAVE_ARCH_KGDB_QXFER_PKT select HAVE_ARCH_MMAP_RND_BITS if MMUarch/riscv/mm/kasan_init.c +49
-1@@ -9,6 +9,19 @@#include <linux/pgtable.h> #include <asm/tlbflush.h> #include <asm/fixmap.h> +#include <asm/pgalloc.h> + +static __init void *early_alloc (size_t size,int node) +{ +void *ptr =memblock_alloc_try_nid (size, size, +__pa (MAX_DMA_ADDRESS ),MEMBLOCK_ALLOC_ACCESSIBLE , node); + +if (!ptr) +panic ("%pS: Failed to allocate %zu bytes align=%zx nid=%d from=%llx\n" , + __func__, size, size, node, (u64 )__pa (MAX_DMA_ADDRESS )); + +return ptr; +}extern pgd_t early_pg_dir[PTRS_PER_PGD ]; asmlinkage void __init kasan_early_init(void)@@ -83,6 +96,40 @@ static void __init populate(void *start, void *end)memset (start,0 , end - start); } +void __init kasan_shallow_populate (void *start,void *end) +{ +unsigned long vaddr = (unsigned long )start &PAGE_MASK ; +unsigned long vend =PAGE_ALIGN ((unsigned long )end); +unsigned long pfn; +int index; +void *p; +pud_t *pud_dir, *pud_k; +pgd_t *pgd_dir, *pgd_k; +p4d_t *p4d_dir, *p4d_k; + +while (vaddr < vend) { + index =pgd_index (vaddr); + pfn =csr_read (CSR_SATP ) &SATP_PPN ; + pgd_dir = (pgd_t *)pfn_to_virt (pfn) + index; + pgd_k = init_mm.pgd + index; + pgd_dir =pgd_offset_k (vaddr); +set_pgd (pgd_dir, *pgd_k); + + p4d_dir =p4d_offset (pgd_dir, vaddr); + p4d_k =p4d_offset (pgd_k, vaddr); + + vaddr = (vaddr +PUD_SIZE ) &PUD_MASK ; + pud_dir =pud_offset (p4d_dir, vaddr); + pud_k =pud_offset (p4d_k, vaddr); + +if (pud_present (*pud_dir)) { + p =early_alloc (PAGE_SIZE ,NUMA_NO_NODE ); +pud_populate (&init_mm, pud_dir, p); + } + vaddr +=PAGE_SIZE ; + } +} +void __init kasan_init (void ) {phys_addr_t _start, _end;@@ -90,7 +137,15 @@ void __init kasan_init(void)kasan_populate_early_shadow ((void *)KASAN_SHADOW_START , (void *)kasan_mem_to_shadow ((void *) -VMALLOC_END )); +VMEMMAP_END )); +if (IS_ENABLED (CONFIG_KASAN_VMALLOC ))
arch/riscv/mm/kasan_init.c void __init kasan_init(void) · line 151
Quoted 10 lines
arch/riscv/mm/kasan_init.c +7
-0+kasan_shallow_populate ( + (void *)kasan_mem_to_shadow ((void *)VMALLOC_START ), + (void *)kasan_mem_to_shadow ((void *)VMALLOC_END )); + else + kasan_populate_early_shadow( + (void *)kasan_mem_to_shadow((void *)VMALLOC_START), + (void *)kasan_mem_to_shadow((void *)VMALLOC_END)); for_each_mem_range(i, &_start, &_end) { void *start = (void *)_start;
Thanks, this is on for-next.
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Alex Ghiti
reviewerRe: [PATCH v2 1/1] riscv/kasan: add KASAN_VMALLOC support
Hi Nylon,
Le 1/22/21 à 10:56 PM, Palmer Dabbelt a écrit :
Quoted 9 lines · On Fri, 15 Jan 2021 21:58:35 PST (-0800), [email protected] wrote:
Quoted 8 lines
It references to x86/s390 architecture.
Quoted 1 line
Quoted 1 line
So, it doesn't map the early shadow page to cover VMALLOC space.
Prepopulate top level page table for the range that would otherwise be empty.
lower levels are filled dynamically upon memory allocation while booting.
I think we can improve the changelog a bit here with something like that:
"KASAN vmalloc space used to be mapped using kasan early shadow page. KASAN_VMALLOC requires the top-level of the kernel page table to be properly populated, lower levels being filled dynamically upon memory allocation at runtime."
Quoted 49 lines
Quoted 49 lines
Signed-off-by Nylon Chen <[email protected]>Signed-off-by Nick Hu <[email protected]>---
arch/riscv/Kconfig | 1 +arch/riscv/mm/kasan_init.c| 57 +++++++++++++++++++++++++++++++++++++-2 files changed, 57 insertions(+), 1 deletion(-)arch/riscv/Kconfig +0
-0@@ -57,6 +57,7 @@ config RISCVselect
HAVE_ARCH_JUMP_LABEL
selectHAVE_ARCH_JUMP_LABEL_RELATIVEselectHAVE_ARCH_KASANif MMU && 64BIT + selectHAVE_ARCH_KASAN_VMALLOCif MMU && 64BIT selectHAVE_ARCH_KGDB
selectHAVE_ARCH_KGDB_QXFER_PKT
selectHAVE_ARCH_MMAP_RND_BITSif MMUarch/riscv/mm/kasan_init.c +13
-0@@ -9,6 +9,19 @@ #include <linux/pgtable.h> #include <asm/tlbflush.h> #include <asm/fixmap.h> +#include <asm/pgalloc.h> + +static __init void *early_alloc (size_t size,int node) +{ + void *ptr = memblock_alloc_try_nid(size, size, + __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, node); + + if (!ptr) + panic("%pS: Failed to allocate %zu bytes align=%zx nid=%d from=%llx\n", + __func__, size, size, node, (u64)__pa(MAX_DMA_ADDRESS)); + + return ptr; +} extern pgd_t early_pg_dir[PTRS_PER_PGD];asmlinkage void
__initkasan_early_init(void)arch/riscv/mm/kasan_init.c +0
-0@@ -83,6 +96,40 @@ static void __init populate(void *start, void *end) memset(start, 0, end - start); }
Quoted 14 lines
Quoted 4 lines
arch/riscv/mm/kasan_init.c +4
-0+void __init kasan_shallow_populate (void *start,void *end) +{ + unsigned long vaddr = (unsigned long)start & PAGE_MASK; + unsigned long vend = PAGE_ALIGN((unsigned long)end);arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 112
Quoted 10 lines
arch/riscv/mm/kasan_init.c +10
-0+ unsigned long pfn; + int index; + void *p; + pud_t *pud_dir, *pud_k; + pgd_t *pgd_dir, *pgd_k; + p4d_t *p4d_dir, *p4d_k; + + while (vaddr < vend) { + index = pgd_index(vaddr); + pfn = csr_read(CSR_SATP) & SATP_PPN;
At this point in the boot process, we know that we use swapper_pg_dir so no need to read SATP.
Quoted 1 line
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 113
Quoted 1 line
arch/riscv/mm/kasan_init.c +1
-0+ pgd_dir = (pgd_t *)pfn_to_virt(pfn) + index;
Here, this pgd_dir assignment is overwritten 2 lines below, so no need for it.
Quoted 2 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 115
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+ pgd_k = init_mm.pgd + index; + pgd_dir = pgd_offset_k(vaddr);
pgd_offset_k(vaddr) = init_mm.pgd + pgd_index(vaddr) so pgd_k == pgd_dir.
Quoted 6 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 121
Quoted 6 lines
arch/riscv/mm/kasan_init.c +6
-0+ set_pgd(pgd_dir, *pgd_k); + + p4d_dir = p4d_offset(pgd_dir, vaddr); + p4d_k = p4d_offset(pgd_k, vaddr); + + vaddr = (vaddr + PUD_SIZE) & PUD_MASK;
Why do you increase vaddr *before* populating the first one ? And pud_addr_end does that properly: it returns the next pud address if it does not go beyond end address to map.
Quoted 6 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 127
Quoted 6 lines
arch/riscv/mm/kasan_init.c +6
-0+ pud_dir = pud_offset(p4d_dir, vaddr); + pud_k = pud_offset(p4d_k, vaddr); + + if (pud_present(*pud_dir)) { + p = early_alloc(PAGE_SIZE, NUMA_NO_NODE); + pud_populate(&init_mm, pud_dir, p);
init_mm is not needed here.
Quoted 2 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 129
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+ } + vaddr += PAGE_SIZE;
Why do you need to add PAGE_SIZE ? vaddr already points to the next pud.
It seems like this patch tries to populate userspace page table whereas at this point in the boot process, only swapper_pg_dir is used or am I missing something ?
Thanks,
Alex
Quoted 21 lines
Quoted 21 lines
arch/riscv/mm/kasan_init.c +12
-1+ } +} + void __init kasan_init(void) { phys_addr_t _start, _end;@@ -90,7 +137,15 @@ void __init kasan_init(void) kasan_populate_early_shadow((void *)KASAN_SHADOW_START, (void *)kasan_mem_to_shadow((void *) - VMALLOC_END)); + VMEMMAP_END)); + if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) + kasan_shallow_populate( + (void *)kasan_mem_to_shadow((void *)VMALLOC_START), + (void *)kasan_mem_to_shadow((void *)VMALLOC_END)); + else + kasan_populate_early_shadow( + (void *)kasan_mem_to_shadow((void *)VMALLOC_START), + (void *)kasan_mem_to_shadow((void *)VMALLOC_END));
Quoted 8 lines
Quoted 2 lines
arch/riscv/mm/kasan_init.c +0
-0for_each_mem_range(i, &_start, &_end) {void *start = (void *)_start; >
Thanks, this is on for-next.
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Alex Ghiti
reviewerRe: [PATCH v2 1/1] riscv/kasan: add KASAN_VMALLOC support
Hi Nylon, Palmer,
Le 2/8/21 à 1:28 AM, Alex Ghiti a écrit :
Quoted 113 lines
Hi Nylon,
Le 1/22/21 à 10:56 PM, Palmer Dabbelt a écrit :
Quoted 9 lines · On Fri, 15 Jan 2021 21:58:35 PST (-0800), [email protected] wrote:
Quoted 8 lines
It references to x86/s390 architecture.
Quoted 1 line
Quoted 1 line
So, it doesn't map the early shadow page to cover VMALLOC space.
Prepopulate top level page table for the range that would otherwise be empty.
lower levels are filled dynamically upon memory allocation while booting.
I think we can improve the changelog a bit here with something like that:
"KASAN vmalloc space used to be mapped using kasan early shadow page.
KASAN_VMALLOCrequires the top-level of the kernel page table to be properly populated, lower levels being filled dynamically upon memory allocation at runtime."Quoted 49 lines
Quoted 49 lines
Signed-off-by Nylon Chen <[email protected]>Signed-off-by Nick Hu <[email protected]>---
arch/riscv/Kconfig | 1 +arch/riscv/mm/kasan_init.c| 57 +++++++++++++++++++++++++++++++++++++-2 files changed, 57 insertions(+), 1 deletion(-)arch/riscv/Kconfig +0
-0@@ -57,6 +57,7 @@ config RISCVselect
HAVE_ARCH_JUMP_LABEL
selectHAVE_ARCH_JUMP_LABEL_RELATIVEselectHAVE_ARCH_KASANif MMU && 64BIT + selectHAVE_ARCH_KASAN_VMALLOCif MMU && 64BIT selectHAVE_ARCH_KGDB
selectHAVE_ARCH_KGDB_QXFER_PKT
selectHAVE_ARCH_MMAP_RND_BITSif MMUarch/riscv/mm/kasan_init.c +13
-0@@ -9,6 +9,19 @@ #include <linux/pgtable.h> #include <asm/tlbflush.h> #include <asm/fixmap.h> +#include <asm/pgalloc.h> + +static __init void *early_alloc (size_t size,int node) +{ + void *ptr = memblock_alloc_try_nid(size, size, + __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, node); + + if (!ptr) + panic("%pS: Failed to allocate %zu bytes align=%zx nid=%d from=%llx\n", + __func__, size, size, node, (u64)__pa(MAX_DMA_ADDRESS)); + + return ptr; +} extern pgd_t early_pg_dir[PTRS_PER_PGD];asmlinkage void
__initkasan_early_init(void)arch/riscv/mm/kasan_init.c +0
-0@@ -83,6 +96,40 @@ static void __init populate(void *start, void *end) memset(start, 0, end - start); }Quoted 14 lines
Quoted 4 lines
arch/riscv/mm/kasan_init.c +4
-0+void __init kasan_shallow_populate (void *start,void *end) +{ + unsigned long vaddr = (unsigned long)start & PAGE_MASK; + unsigned long vend = PAGE_ALIGN((unsigned long)end);arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 112
Quoted 10 lines
arch/riscv/mm/kasan_init.c +10
-0+ unsigned long pfn; + int index; + void *p; + pud_t *pud_dir, *pud_k; + pgd_t *pgd_dir, *pgd_k; + p4d_t *p4d_dir, *p4d_k; + + while (vaddr < vend) { + index = pgd_index(vaddr); + pfn = csr_read(CSR_SATP) & SATP_PPN;At this point in the boot process, we know that we use
swapper_pg_dirso no need to read SATP.Quoted 1 line
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 113
Quoted 1 line
arch/riscv/mm/kasan_init.c +1
-0+ pgd_dir = (pgd_t *)pfn_to_virt(pfn) + index;Here, this
pgd_dirassignment is overwritten 2 lines below, so no need for it.Quoted 2 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 115
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+ pgd_k = init_mm.pgd + index; + pgd_dir = pgd_offset_k(vaddr);
pgd_offset_k(vaddr)= init_mm.pgd +pgd_index(vaddr)sopgd_k==pgd_dir.Quoted 6 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 121
Quoted 6 lines
arch/riscv/mm/kasan_init.c +6
-0+ set_pgd(pgd_dir, *pgd_k); + + p4d_dir = p4d_offset(pgd_dir, vaddr); + p4d_k = p4d_offset(pgd_k, vaddr); + + vaddr = (vaddr + PUD_SIZE) & PUD_MASK;Why do you increase vaddr *before* populating the first one ? And
pud_addr_enddoes that properly: it returns the next pud address if it does not go beyond end address to map.Quoted 6 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 127
Quoted 6 lines
arch/riscv/mm/kasan_init.c +6
-0+ pud_dir = pud_offset(p4d_dir, vaddr); + pud_k = pud_offset(p4d_k, vaddr); + + if (pud_present(*pud_dir)) { + p = early_alloc(PAGE_SIZE, NUMA_NO_NODE); + pud_populate(&init_mm, pud_dir, p);
init_mmis not needed here.Quoted 2 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 129
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+ } + vaddr += PAGE_SIZE;
Quoted 7 lines
Why do you need to add
PAGE_SIZE? vaddr already points to the next pud.It seems like this patch tries to populate userspace page table whereas at this point in the boot process, only
swapper_pg_diris used or am I missing something ?Thanks,
Alex
I implemented this morning a version that fixes all the comments I made earlier. I was able to insert test_kasan_module on both sv39 and sv48 without any modification: set_pgd "goes through" all the unused page table levels, whereas p*d_populate are noop for unused levels.
If you have any comment, do not hesitate.
arch/riscv/mm/kasan_init.c +0 -0
index adbf94b7e68a..d643b222167c 100644
--- a/arch/riscv/mm/kasan_init.c
+++ b/arch/riscv/mm/kasan_init.c
@@ -195,6 +195,31 @@ static void __init kasan_populate(void *start, void *end)
memset(start, KASAN_SHADOW_INIT, end - start);
}
+void __init kasan_shallow_populate_pgd(unsigned long vaddr, unsigned long end) +{
+ unsigned long next;
+ void *p;
+ pgd_t *pgd_k = pgd_offset_k(vaddr);
+
+ do {
+ next = pgd_addr_end(vaddr, end);
+ if (pgd_page_vaddr(*pgd_k) == (unsigned long)lm_alias(kasan_early_shadow_pgd_next)) {
+ p = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
+ set_pgd(pgd_k, pfn_pgd(PFN_DOWN(__pa(p)), PAGE_TABLE));
+ }
+ } while (pgd_k++, vaddr = next, vaddr != end);
+}
+
+void __init kasan_shallow_populate(void *start, void *end)
+{
+ unsigned long vaddr = (unsigned long)start & PAGE_MASK;
+ unsigned long vend = PAGE_ALIGN((unsigned long)end);
+
+ kasan_shallow_populate_pgd(vaddr, vend);
+
+ local_flush_tlb_all();
+}
+
void __init kasan_init(void)
{
phys_addr_t _start, _end;
@@ -206,7 +231,15 @@ void __init kasan_init(void)
*/
kasan_populate_early_shadow((void *)KASAN_SHADOW_START,
(void *)kasan_mem_to_shadow((void *)
- VMALLOC_END));
+ VMEMMAP_END));
+ if (IS_ENABLED(CONFIG_KASAN_VMALLOC))
+ kasan_shallow_populate(
+ (void *)kasan_mem_to_shadow((void *)VMALLOC_START),
+ (void *)kasan_mem_to_shadow((void *)VMALLOC_END));
+ else
+ kasan_populate_early_shadow(
+ (void *)kasan_mem_to_shadow((void *)VMALLOC_START),
+ (void *)kasan_mem_to_shadow((void *)VMALLOC_END));
/* Populate the linear mapping */
for_each_mem_range(i, &_start, &_end) { Signature · 5 lines
2.20.1 Thanks, Alex
Quoted 31 lines
Quoted 22 lines
Quoted 22 lines
arch/riscv/mm/kasan_init.c +12
-1+ } +} + void __init kasan_init(void) { phys_addr_t _start, _end;@@ -90,7 +137,15 @@ void __init kasan_init(void) kasan_populate_early_shadow((void *)KASAN_SHADOW_START, (void *)kasan_mem_to_shadow((void *) - VMALLOC_END)); + VMEMMAP_END)); + if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) + kasan_shallow_populate( + (void *)kasan_mem_to_shadow((void *)VMALLOC_START), + (void *)kasan_mem_to_shadow((void *)VMALLOC_END)); + else + kasan_populate_early_shadow( + (void *)kasan_mem_to_shadow((void *)VMALLOC_START), + (void *)kasan_mem_to_shadow((void *)VMALLOC_END));Quoted 8 lines
Quoted 2 lines
arch/riscv/mm/kasan_init.c +0
-0for_each_mem_range(i, &_start, &_end) {void *start = (void *)_start; >
Thanks, this is on for-next.
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 4 lines
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Alex Ghiti
reviewerRe: [PATCH v2 1/1] riscv/kasan: add KASAN_VMALLOC support
Le 2/13/21 à 5:52 AM, Alex Ghiti a écrit :
Quoted 154 lines
Hi Nylon, Palmer,
Le 2/8/21 à 1:28 AM, Alex Ghiti a écrit :
Quoted 113 lines
Hi Nylon,
Le 1/22/21 à 10:56 PM, Palmer Dabbelt a écrit :
Quoted 9 lines · On Fri, 15 Jan 2021 21:58:35 PST (-0800), [email protected] wrote:
Quoted 8 lines
It references to x86/s390 architecture.
Quoted 1 line
Quoted 1 line
So, it doesn't map the early shadow page to cover VMALLOC space.
Prepopulate top level page table for the range that would otherwise be empty.
lower levels are filled dynamically upon memory allocation while booting.
I think we can improve the changelog a bit here with something like that:
"KASAN vmalloc space used to be mapped using kasan early shadow page.
KASAN_VMALLOCrequires the top-level of the kernel page table to be properly populated, lower levels being filled dynamically upon memory allocation at runtime."Quoted 49 lines
Quoted 49 lines
Signed-off-by Nylon Chen <[email protected]>Signed-off-by Nick Hu <[email protected]>---
arch/riscv/Kconfig | 1 +arch/riscv/mm/kasan_init.c| 57 +++++++++++++++++++++++++++++++++++++-2 files changed, 57 insertions(+), 1 deletion(-)arch/riscv/Kconfig +0
-0@@ -57,6 +57,7 @@ config RISCVselect
HAVE_ARCH_JUMP_LABEL
selectHAVE_ARCH_JUMP_LABEL_RELATIVEselectHAVE_ARCH_KASANif MMU && 64BIT + selectHAVE_ARCH_KASAN_VMALLOCif MMU && 64BIT selectHAVE_ARCH_KGDB
selectHAVE_ARCH_KGDB_QXFER_PKT
selectHAVE_ARCH_MMAP_RND_BITSif MMUarch/riscv/mm/kasan_init.c +13
-0@@ -9,6 +9,19 @@ #include <linux/pgtable.h> #include <asm/tlbflush.h> #include <asm/fixmap.h> +#include <asm/pgalloc.h> + +static __init void *early_alloc (size_t size,int node) +{ + void *ptr = memblock_alloc_try_nid(size, size, + __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, node); + + if (!ptr) + panic("%pS: Failed to allocate %zu bytes align=%zx nid=%d from=%llx\n", + __func__, size, size, node, (u64)__pa(MAX_DMA_ADDRESS)); + + return ptr; +} extern pgd_t early_pg_dir[PTRS_PER_PGD];asmlinkage void
__initkasan_early_init(void)arch/riscv/mm/kasan_init.c +0
-0@@ -83,6 +96,40 @@ static void __init populate(void *start, void *end) memset(start, 0, end - start); }Quoted 14 lines
Quoted 4 lines
arch/riscv/mm/kasan_init.c +4
-0+void __init kasan_shallow_populate (void *start,void *end) +{ + unsigned long vaddr = (unsigned long)start & PAGE_MASK; + unsigned long vend = PAGE_ALIGN((unsigned long)end);arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 112
Quoted 10 lines
arch/riscv/mm/kasan_init.c +10
-0+ unsigned long pfn; + int index; + void *p; + pud_t *pud_dir, *pud_k; + pgd_t *pgd_dir, *pgd_k; + p4d_t *p4d_dir, *p4d_k; + + while (vaddr < vend) { + index = pgd_index(vaddr); + pfn = csr_read(CSR_SATP) & SATP_PPN;At this point in the boot process, we know that we use
swapper_pg_dirso no need to read SATP.Quoted 1 line
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 113
Quoted 1 line
arch/riscv/mm/kasan_init.c +1
-0+ pgd_dir = (pgd_t *)pfn_to_virt(pfn) + index;Here, this
pgd_dirassignment is overwritten 2 lines below, so no need for it.Quoted 2 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 115
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+ pgd_k = init_mm.pgd + index; + pgd_dir = pgd_offset_k(vaddr);
pgd_offset_k(vaddr)= init_mm.pgd +pgd_index(vaddr)sopgd_k==pgd_dir.Quoted 6 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 121
Quoted 6 lines
arch/riscv/mm/kasan_init.c +6
-0+ set_pgd(pgd_dir, *pgd_k); + + p4d_dir = p4d_offset(pgd_dir, vaddr); + p4d_k = p4d_offset(pgd_k, vaddr); + + vaddr = (vaddr + PUD_SIZE) & PUD_MASK;Why do you increase vaddr *before* populating the first one ? And
pud_addr_enddoes that properly: it returns the next pud address if it does not go beyond end address to map.Quoted 6 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 127
Quoted 6 lines
arch/riscv/mm/kasan_init.c +6
-0+ pud_dir = pud_offset(p4d_dir, vaddr); + pud_k = pud_offset(p4d_k, vaddr); + + if (pud_present(*pud_dir)) { + p = early_alloc(PAGE_SIZE, NUMA_NO_NODE); + pud_populate(&init_mm, pud_dir, p);
init_mmis not needed here.Quoted 2 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 129
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+ } + vaddr += PAGE_SIZE;Quoted 7 lines
Why do you need to add
PAGE_SIZE? vaddr already points to the next pud.It seems like this patch tries to populate userspace page table whereas at this point in the boot process, only
swapper_pg_diris used or am I missing something ?Thanks,
Alex
I implemented this morning a version that fixes all the comments I made earlier. I was able to insert
test_kasan_moduleon both sv39 and sv48 without any modification:set_pgd"goes through" all the unused page table levels, whereas p*d_populate are noop for unused levels.If you have any comment, do not hesitate.
arch/riscv/mm/kasan_init.c +9
-0@@ -195,6 +195,31 @@ static void __init kasan_populate(void *start, void *end) memset(start, KASAN_SHADOW_INIT, end - start); } +void __init kasan_shallow_populate_pgd (unsigned long vaddr,unsigned long end) +{ + unsigned long next; + void *p; + pgd_t *pgd_k = pgd_offset_k(vaddr); + + do { + next = pgd_addr_end(vaddr, end); + if (pgd_page_vaddr(*pgd_k) == (unsignedlong)lm_alias(kasan_early_shadow_pgd_next)) {
+ p =memblock_alloc(PAGE_SIZE, PAGE_SIZE); +set_pgd(pgd_k, pfn_pgd(PFN_DOWN(__pa(p)), PAGE_TABLE));
+ }
+ } while (pgd_k++, vaddr = next, vaddr != end);
arch/riscv/mm/kasan_init.c
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+} +
This way of going through the page table seems to be largely used across the kernel (cf KASAN population functions of arm64/x86) so I do think this patch brings value to Nylon and Nick's patch.
I can propose a real patch if you agree and I'll add a co-developed by Nylon/Nick since this only 'improves' theirs.
Thanks,
Alex
Quoted 30 lines
arch/riscv/mm/kasan_init.c +19
-1+void __init kasan_shallow_populate (void *start,void *end) +{ + unsigned long vaddr = (unsigned long)start & PAGE_MASK; + unsigned long vend = PAGE_ALIGN((unsigned long)end); + + kasan_shallow_populate_pgd(vaddr, vend); + + local_flush_tlb_all(); +} + void __init kasan_init(void) { phys_addr_t _start, _end;@@ -206,7 +231,15 @@ void __init kasan_init(void)*/ kasan_populate_early_shadow((void *)KASAN_SHADOW_START, (void *)kasan_mem_to_shadow((void *) -VMALLOC_END )); +VMEMMAP_END )); + if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) + kasan_shallow_populate( + (void *)kasan_mem_to_shadow((void *)VMALLOC_START), + (void *)kasan_mem_to_shadow((void *)VMALLOC_END)); + else + kasan_populate_early_shadow( + (void *)kasan_mem_to_shadow((void *)VMALLOC_START), + (void *)kasan_mem_to_shadow((void *)VMALLOC_END)); /* Populate the linear mapping */
Quoted 1 line
for_each_mem_range(i, &_start, &_end){
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v2 1/1] riscv/kasan: add KASAN_VMALLOC support
Hi Alex, Palmer
Sorry I missed this message.
Quoted 174 lines · Alex Ghiti
Le 2/13/21 à 5:52 AM, Alex Ghiti a écrit :
Quoted 166 lines
Hi Nylon, Palmer,
Le 2/8/21 à 1:28 AM, Alex Ghiti a écrit :
Quoted 122 lines
Hi Nylon,
Le 1/22/21 à 10:56 PM, Palmer Dabbelt a écrit :
Quoted 9 lines · On Fri, 15 Jan 2021 21:58:35 PST (-0800), [email protected] wrote:
Quoted 8 lines
It references to x86/s390 architecture.
Quoted 1 line
Quoted 1 line
So, it doesn't map the early shadow page to cover VMALLOC space.
Prepopulate top level page table for the range that would otherwise be empty.
lower levels are filled dynamically upon memory allocation while booting.
I think we can improve the changelog a bit here with something like that:
"KASAN vmalloc space used to be mapped using kasan early shadow page.
KASAN_VMALLOCrequires the top-level of the kernel page table to be properly populated, lower levels being filled dynamically upon memory allocation at runtime."Quoted 49 lines
Quoted 49 lines
Signed-off-by Nylon Chen <[email protected]>Signed-off-by Nick Hu <[email protected]>---
arch/riscv/Kconfig | 1 +arch/riscv/mm/kasan_init.c| 57 +++++++++++++++++++++++++++++++++++++-2 files changed, 57 insertions(+), 1 deletion(-)arch/riscv/Kconfig +0
-0@@ -57,6 +57,7 @@ config RISCVselect
HAVE_ARCH_JUMP_LABEL
selectHAVE_ARCH_JUMP_LABEL_RELATIVEselectHAVE_ARCH_KASANif MMU && 64BIT + selectHAVE_ARCH_KASAN_VMALLOCif MMU && 64BIT selectHAVE_ARCH_KGDB
selectHAVE_ARCH_KGDB_QXFER_PKT
selectHAVE_ARCH_MMAP_RND_BITSif MMUarch/riscv/mm/kasan_init.c +13
-0@@ -9,6 +9,19 @@ #include <linux/pgtable.h> #include <asm/tlbflush.h> #include <asm/fixmap.h> +#include <asm/pgalloc.h> + +static __init void *early_alloc (size_t size,int node) +{ + void *ptr = memblock_alloc_try_nid(size, size, + __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, node); + + if (!ptr) + panic("%pS: Failed to allocate %zu bytes align=%zx nid=%d from=%llx\n", + __func__, size, size, node, (u64)__pa(MAX_DMA_ADDRESS)); + + return ptr; +} extern pgd_t early_pg_dir[PTRS_PER_PGD];asmlinkage void
__initkasan_early_init(void)arch/riscv/mm/kasan_init.c +0
-0@@ -83,6 +96,40 @@ static void __init populate(void *start, void *end) memset(start, 0, end - start); }Quoted 14 lines
Quoted 4 lines
arch/riscv/mm/kasan_init.c +4
-0+void __init kasan_shallow_populate (void *start,void *end) +{ + unsigned long vaddr = (unsigned long)start & PAGE_MASK; + unsigned long vend = PAGE_ALIGN((unsigned long)end);arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 112
Quoted 10 lines
arch/riscv/mm/kasan_init.c +10
-0+ unsigned long pfn; + int index; + void *p; + pud_t *pud_dir, *pud_k; + pgd_t *pgd_dir, *pgd_k; + p4d_t *p4d_dir, *p4d_k; + + while (vaddr < vend) { + index = pgd_index(vaddr); + pfn = csr_read(CSR_SATP) & SATP_PPN;At this point in the boot process, we know that we use
swapper_pg_dirso no need to read SATP.Quoted 1 line
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 113
Quoted 1 line
arch/riscv/mm/kasan_init.c +1
-0+ pgd_dir = (pgd_t *)pfn_to_virt(pfn) + index;Here, this
pgd_dirassignment is overwritten 2 lines below, so no need for it.Quoted 2 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 115
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+ pgd_k = init_mm.pgd + index; + pgd_dir = pgd_offset_k(vaddr);
pgd_offset_k(vaddr)= init_mm.pgd +pgd_index(vaddr)sopgd_k==pgd_dir.Quoted 6 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 121
Quoted 6 lines
arch/riscv/mm/kasan_init.c +6
-0+ set_pgd(pgd_dir, *pgd_k); + + p4d_dir = p4d_offset(pgd_dir, vaddr); + p4d_k = p4d_offset(pgd_k, vaddr); + + vaddr = (vaddr + PUD_SIZE) & PUD_MASK;Why do you increase vaddr *before* populating the first one ? And
pud_addr_enddoes that properly: it returns the next pud address if it does not go beyond end address to map.Quoted 6 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 127
Quoted 6 lines
arch/riscv/mm/kasan_init.c +6
-0+ pud_dir = pud_offset(p4d_dir, vaddr); + pud_k = pud_offset(p4d_k, vaddr); + + if (pud_present(*pud_dir)) { + p = early_alloc(PAGE_SIZE, NUMA_NO_NODE); + pud_populate(&init_mm, pud_dir, p);
init_mmis not needed here.Quoted 2 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 129
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+ } + vaddr += PAGE_SIZE;Why do you need to add
PAGE_SIZE? vaddr already points to the next pud.Quoted 7 lines
It seems like this patch tries to populate userspace page table whereas at this point in the boot process, only
swapper_pg_diris used or am I missing something ?Thanks,
Alex
I implemented this morning a version that fixes all the comments I made earlier. I was able to insert
test_kasan_moduleon both sv39 and sv48 without any modification:set_pgd"goes through" all the unused page table levels, whereas p*d_populate are noop for unused levels.If you have any comment, do not hesitate.
arch/riscv/mm/kasan_init.c +9
-0@@ -195,6 +195,31 @@ static void __init kasan_populate(void *start, void *end) memset(start, KASAN_SHADOW_INIT, end - start); } +void __init kasan_shallow_populate_pgd (unsigned long vaddr,unsigned long end) +{ + unsigned long next; + void *p; + pgd_t *pgd_k = pgd_offset_k(vaddr); + + do { + next = pgd_addr_end(vaddr, end); + if (pgd_page_vaddr(*pgd_k) == (unsignedlong)lm_alias(kasan_early_shadow_pgd_next)) {
+ p =memblock_alloc(PAGE_SIZE, PAGE_SIZE); +set_pgd(pgd_k, pfn_pgd(PFN_DOWN(__pa(p)), PAGE_TABLE));
+ }
+ } while (pgd_k++, vaddr = next, vaddr != end);arch/riscv/mm/kasan_init.c
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+} +This way of going through the page table seems to be largely used across the kernel (cf KASAN population functions of arm64/x86) so I do think this patch brings value to Nylon and Nick's patch.
Quoted 7 lines
I can propose a real patch if you agree and I'll add a co-developed by Nylon/Nick since this only 'improves' theirs.
Thanks,
Alex
I agree with your proposal, but when I try your patch that it dosen't work because kasan_early_shadow_pgd_next function wasn't define.
Do you have complete patch? or just I missed some content?
Quoted 29 lines
Quoted 29 lines
arch/riscv/mm/kasan_init.c +19
-1+void __init kasan_shallow_populate (void *start,void *end) +{ + unsigned long vaddr = (unsigned long)start & PAGE_MASK; + unsigned long vend = PAGE_ALIGN((unsigned long)end); + + kasan_shallow_populate_pgd(vaddr, vend); + + local_flush_tlb_all(); +} + void __init kasan_init(void) { phys_addr_t _start, _end;@@ -206,7 +231,15 @@ void __init kasan_init(void)*/ kasan_populate_early_shadow((void *)KASAN_SHADOW_START, (void *)kasan_mem_to_shadow((void *) -VMALLOC_END )); +VMEMMAP_END )); + if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) + kasan_shallow_populate( + (void *)kasan_mem_to_shadow((void *)VMALLOC_START), + (void *)kasan_mem_to_shadow((void *)VMALLOC_END)); + else + kasan_populate_early_shadow( + (void *)kasan_mem_to_shadow((void *)VMALLOC_START), + (void *)kasan_mem_to_shadow((void *)VMALLOC_END));
Quoted 2 lines
Quoted 2 lines
arch/riscv/mm/kasan_init.c +0
-0/* Populate the linear mapping */
for_each_mem_range(i, &_start, &_end){
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Alex Ghiti
reviewerRe: [PATCH v2 1/1] riscv/kasan: add KASAN_VMALLOC support
Le 2/21/21 à 8:37 PM, Nylon Chen a écrit :
Quoted 181 lines
Hi Alex, Palmer
Sorry I missed this message.
Quoted 177 lines · Alex Ghiti
Le 2/13/21 à 5:52 AM, Alex Ghiti a écrit :
Quoted 166 lines
Hi Nylon, Palmer,
Le 2/8/21 à 1:28 AM, Alex Ghiti a écrit :
Quoted 122 lines
Hi Nylon,
Le 1/22/21 à 10:56 PM, Palmer Dabbelt a écrit :
Quoted 9 lines · On Fri, 15 Jan 2021 21:58:35 PST (-0800), [email protected] wrote:
Quoted 8 lines
It references to x86/s390 architecture.
Quoted 1 line
Quoted 1 line
So, it doesn't map the early shadow page to cover VMALLOC space.
Prepopulate top level page table for the range that would otherwise be empty.
lower levels are filled dynamically upon memory allocation while booting.
I think we can improve the changelog a bit here with something like that:
"KASAN vmalloc space used to be mapped using kasan early shadow page.
KASAN_VMALLOCrequires the top-level of the kernel page table to be properly populated, lower levels being filled dynamically upon memory allocation at runtime."Quoted 49 lines
Quoted 49 lines
Signed-off-by Nylon Chen <[email protected]>Signed-off-by Nick Hu <[email protected]>--- arch/riscv/Kconfig | 1 + arch/riscv/mm/kasan_init.c | 57 +++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-)arch/riscv/Kconfig +1
-0@@ -57,6 +57,7 @@ config RISCV select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if MMU && 64BIT + select HAVE_ARCH_KASAN_VMALLOC if MMU && 64BIT select HAVE_ARCH_KGDB select HAVE_ARCH_KGDB_QXFER_PKT select HAVE_ARCH_MMAP_RND_BITS if MMUarch/riscv/mm/kasan_init.c +13
-0@@ -9,6 +9,19 @@ #include <linux/pgtable.h> #include <asm/tlbflush.h> #include <asm/fixmap.h> +#include <asm/pgalloc.h> + +static __init void *early_alloc (size_t size,int node) +{ + void *ptr = memblock_alloc_try_nid(size, size, + __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, node); + + if (!ptr) + panic("%pS: Failed to allocate %zu bytes align=%zx nid=%d from=%llx\n", + __func__, size, size, node, (u64)__pa(MAX_DMA_ADDRESS)); + + return ptr; +} extern pgd_t early_pg_dir[PTRS_PER_PGD];asmlinkage void
__initkasan_early_init(void)arch/riscv/mm/kasan_init.c +0
-0@@ -83,6 +96,40 @@ static void __init populate(void *start, void *end) memset(start, 0, end - start); }Quoted 14 lines
Quoted 4 lines
arch/riscv/mm/kasan_init.c +4
-0+void __init kasan_shallow_populate (void *start,void *end) +{ + unsigned long vaddr = (unsigned long)start & PAGE_MASK; + unsigned long vend = PAGE_ALIGN((unsigned long)end);arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 112
Quoted 10 lines
arch/riscv/mm/kasan_init.c +10
-0+ unsigned long pfn; + int index; + void *p; + pud_t *pud_dir, *pud_k; + pgd_t *pgd_dir, *pgd_k; + p4d_t *p4d_dir, *p4d_k; + + while (vaddr < vend) { + index = pgd_index(vaddr); + pfn = csr_read(CSR_SATP) & SATP_PPN;At this point in the boot process, we know that we use
swapper_pg_dirso no need to read SATP.Quoted 1 line
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 113
Quoted 1 line
arch/riscv/mm/kasan_init.c +1
-0+ pgd_dir = (pgd_t *)pfn_to_virt(pfn) + index;Here, this
pgd_dirassignment is overwritten 2 lines below, so no need for it.Quoted 2 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 115
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+ pgd_k = init_mm.pgd + index; + pgd_dir = pgd_offset_k(vaddr);
pgd_offset_k(vaddr)= init_mm.pgd +pgd_index(vaddr)sopgd_k==pgd_dir.Quoted 6 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 121
Quoted 6 lines
arch/riscv/mm/kasan_init.c +6
-0+ set_pgd(pgd_dir, *pgd_k); + + p4d_dir = p4d_offset(pgd_dir, vaddr); + p4d_k = p4d_offset(pgd_k, vaddr); + + vaddr = (vaddr + PUD_SIZE) & PUD_MASK;Why do you increase vaddr *before* populating the first one ? And
pud_addr_enddoes that properly: it returns the next pud address if it does not go beyond end address to map.Quoted 6 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 127
Quoted 6 lines
arch/riscv/mm/kasan_init.c +6
-0+ pud_dir = pud_offset(p4d_dir, vaddr); + pud_k = pud_offset(p4d_k, vaddr); + + if (pud_present(*pud_dir)) { + p = early_alloc(PAGE_SIZE, NUMA_NO_NODE); + pud_populate(&init_mm, pud_dir, p);
init_mmis not needed here.Quoted 2 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void *end) · line 129
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+ } + vaddr += PAGE_SIZE;Why do you need to add
PAGE_SIZE? vaddr already points to the next pud.Quoted 7 lines
It seems like this patch tries to populate userspace page table whereas at this point in the boot process, only
swapper_pg_diris used or am I missing something ?Thanks,
Alex
I implemented this morning a version that fixes all the comments I made earlier. I was able to insert
test_kasan_moduleon both sv39 and sv48 without any modification:set_pgd"goes through" all the unused page table levels, whereas p*d_populate are noop for unused levels.If you have any comment, do not hesitate.
arch/riscv/mm/kasan_init.c +9
-0@@ -195,6 +195,31 @@ static void __init kasan_populate(void *start, void *end) memset(start, KASAN_SHADOW_INIT, end - start); } +void __init kasan_shallow_populate_pgd (unsigned long vaddr,unsigned long end) +{ + unsigned long next; + void *p; + pgd_t *pgd_k = pgd_offset_k(vaddr); + + do { + next = pgd_addr_end(vaddr, end); + if (pgd_page_vaddr(*pgd_k) == (unsignedlong)lm_alias(kasan_early_shadow_pgd_next)) {
+ p =memblock_alloc(PAGE_SIZE, PAGE_SIZE); +set_pgd(pgd_k, pfn_pgd(PFN_DOWN(__pa(p)), PAGE_TABLE));
+ }
+ } while (pgd_k++, vaddr = next, vaddr != end);arch/riscv/mm/kasan_init.c
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+} +This way of going through the page table seems to be largely used across the kernel (cf KASAN population functions of arm64/x86) so I do think this patch brings value to Nylon and Nick's patch.
I can propose a real patch if you agree and I'll add a co-developed by Nylon/Nick since this only 'improves' theirs.
Quoted 6 lines
Quoted 4 lines
Thanks,
Alex
I agree with your proposal, but when I try your patch that it dosen't work because
kasan_early_shadow_pgd_nextfunction wasn't define.
Oops, I messed up my rebase, please replace 'kasan_early_shadow_pgd_next' with 'kasan_early_shadow_pmd'.
Thank you for your feeback,
Alex
Quoted 31 lines
Do you have complete patch? or just I missed some content?
Quoted 29 lines
Quoted 29 lines
arch/riscv/mm/kasan_init.c +19
-1+void __init kasan_shallow_populate (void *start,void *end) +{ + unsigned long vaddr = (unsigned long)start & PAGE_MASK; + unsigned long vend = PAGE_ALIGN((unsigned long)end); + + kasan_shallow_populate_pgd(vaddr, vend); + + local_flush_tlb_all(); +} + void __init kasan_init(void) { phys_addr_t _start, _end;@@ -206,7 +231,15 @@ void __init kasan_init(void)*/ kasan_populate_early_shadow((void *)KASAN_SHADOW_START, (void *)kasan_mem_to_shadow((void *) -VMALLOC_END )); +VMEMMAP_END )); + if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) + kasan_shallow_populate( + (void *)kasan_mem_to_shadow((void *)VMALLOC_START), + (void *)kasan_mem_to_shadow((void *)VMALLOC_END)); + else + kasan_populate_early_shadow( + (void *)kasan_mem_to_shadow((void *)VMALLOC_START), + (void *)kasan_mem_to_shadow((void *)VMALLOC_END));
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
arch/riscv/mm/kasan_init.c +0
-0/* Populate the linear mapping */for_each_mem_range(i, &_start, &_end) {_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Alex Ghiti
reviewerRe: [PATCH v2 1/1] riscv/kasan: add KASAN_VMALLOC support
Hi Nylon,
Le 2/22/21 à 12:13 PM, Alex Ghiti a écrit :
Quoted 190 lines
Le 2/21/21 à 8:37 PM, Nylon Chen a écrit :
Quoted 189 lines
Hi Alex, Palmer
Sorry I missed this message.
Quoted 185 lines · Alex Ghiti
Le 2/13/21 à 5:52 AM, Alex Ghiti a écrit :
Quoted 170 lines
Hi Nylon, Palmer,
Le 2/8/21 à 1:28 AM, Alex Ghiti a écrit :
Quoted 125 lines
Hi Nylon,
Le 1/22/21 à 10:56 PM, Palmer Dabbelt a écrit :
Quoted 9 lines · On Fri, 15 Jan 2021 21:58:35 PST (-0800), [email protected] wrote:
Quoted 8 lines
It references to x86/s390 architecture.
Quoted 1 line
Quoted 1 line
So, it doesn't map the early shadow page to cover VMALLOC space.
Prepopulate top level page table for the range that would otherwise be empty.
lower levels are filled dynamically upon memory allocation while booting.
I think we can improve the changelog a bit here with something like that:
"KASAN vmalloc space used to be mapped using kasan early shadow page.
KASAN_VMALLOCrequires the top-level of the kernel page table to be properly populated, lower levels being filled dynamically upon memory allocation at runtime."Quoted 52 lines
Quoted 52 lines
Signed-off-by Nylon Chen <[email protected]>Signed-off-by Nick Hu <[email protected]>---
arch/riscv/Kconfig | 1 +
arch/riscv/mm/kasan_init.c | 57
+++++++++++++++++++++++++++++++++++++-2 files changed, 57 insertions(+), 1 deletion(-)arch/riscv/Kconfig +0
-0@@ -57,6 +57,7 @@ config RISCVselect
HAVE_ARCH_JUMP_LABEL
selectHAVE_ARCH_JUMP_LABEL_RELATIVEselectHAVE_ARCH_KASANif MMU && 64BIT + selectHAVE_ARCH_KASAN_VMALLOCif MMU && 64BIT selectHAVE_ARCH_KGDB
selectHAVE_ARCH_KGDB_QXFER_PKTselectHAVE_ARCH_MMAP_RND_BITSif MMUarch/riscv/mm/kasan_init.c +13
-0@@ -9,6 +9,19 @@ #include <linux/pgtable.h> #include <asm/tlbflush.h> #include <asm/fixmap.h> +#include <asm/pgalloc.h> + +static __init void *early_alloc (size_t size,int node) +{ + void *ptr = memblock_alloc_try_nid(size, size, + __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, node); + + if (!ptr) + panic("%pS: Failed to allocate %zu bytes align=%zx nid=%d from=%llx\n", + __func__, size, size, node, (u64 )__pa (MAX_DMA_ADDRESS )); + + return ptr; +}extern
pgd_tearly_pg_dir[PTRS_PER_PGD];
asmlinkage void__initkasan_early_init(void)arch/riscv/mm/kasan_init.c +0
-0@@ -83,6 +96,40 @@ static void __init populate(void *start, void *end) memset(start, 0, end - start); }Quoted 14 lines
Quoted 4 lines
arch/riscv/mm/kasan_init.c +4
-0+void __init kasan_shallow_populate (void *start,void *end) +{ + unsigned long vaddr = (unsigned long)start & PAGE_MASK; + unsigned long vend = PAGE_ALIGN((unsigned long)end);arch/riscv/mm/kasan_init.c static void __init populate(void *start, void · line 113
Quoted 10 lines
arch/riscv/mm/kasan_init.c +10
-0+ unsigned long pfn; + int index; + void *p; + pud_t *pud_dir, *pud_k; + pgd_t *pgd_dir, *pgd_k; + p4d_t *p4d_dir, *p4d_k; + + while (vaddr < vend) { + index = pgd_index(vaddr); + pfn = csr_read(CSR_SATP) & SATP_PPN;At this point in the boot process, we know that we use
swapper_pg_dirso no need to read SATP.Quoted 1 line
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void · line 114
Quoted 1 line
arch/riscv/mm/kasan_init.c +1
-0+ pgd_dir = (pgd_t *)pfn_to_virt(pfn) + index;Here, this
pgd_dirassignment is overwritten 2 lines below, so no need for it.Quoted 2 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void · line 116
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+ pgd_k = init_mm.pgd + index; + pgd_dir = pgd_offset_k(vaddr);
pgd_offset_k(vaddr)= init_mm.pgd +pgd_index(vaddr)sopgd_k==pgd_dir.Quoted 6 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void · line 122
Quoted 6 lines
arch/riscv/mm/kasan_init.c +6
-0+ set_pgd(pgd_dir, *pgd_k); + + p4d_dir = p4d_offset(pgd_dir, vaddr); + p4d_k = p4d_offset(pgd_k, vaddr); + + vaddr = (vaddr + PUD_SIZE) & PUD_MASK;Why do you increase vaddr *before* populating the first one ? And
pud_addr_enddoes that properly: it returns the next pud address if it does not go beyond end address to map.Quoted 6 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void · line 128
Quoted 6 lines
arch/riscv/mm/kasan_init.c +6
-0+ pud_dir = pud_offset(p4d_dir, vaddr); + pud_k = pud_offset(p4d_k, vaddr); + + if (pud_present(*pud_dir)) { + p = early_alloc(PAGE_SIZE, NUMA_NO_NODE); + pud_populate(&init_mm, pud_dir, p);
init_mmis not needed here.Quoted 2 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void · line 130
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+ } + vaddr += PAGE_SIZE;Why do you need to add
PAGE_SIZE? vaddr already points to the next pud.Quoted 7 lines
It seems like this patch tries to populate userspace page table whereas at this point in the boot process, only
swapper_pg_diris used or am I missing something ?Thanks,
Alex
I implemented this morning a version that fixes all the comments I made earlier. I was able to insert
test_kasan_moduleon both sv39 and sv48 without any modification:set_pgd"goes through" all the unused page table levels, whereas p*d_populate are noop for unused levels.If you have any comment, do not hesitate.
arch/riscv/mm/kasan_init.c +0
-0@@ -195,6 +195,31 @@ static void __init kasan_populate(void *start,void
*end)
memset(start, KASAN_SHADOW_INIT, end - start); }+void
__initkasan_shallow_populate_pgd(unsigned long vaddr, unsigned long end)arch/riscv/mm/kasan_init.c +8
-0+{ + unsigned long next; + void *p; + pgd_t *pgd_k = pgd_offset_k(vaddr); + + do { + next = pgd_addr_end(vaddr, end); + if (pgd_page_vaddr(*pgd_k) == (unsignedlong)lm_alias(kasan_early_shadow_pgd_next)) {
+ p =memblock_alloc(PAGE_SIZE, PAGE_SIZE); +set_pgd(pgd_k, pfn_pgd(PFN_DOWN(__pa(p)), PAGE_TABLE));
+ }
+ } while (pgd_k++, vaddr = next, vaddr != end);arch/riscv/mm/kasan_init.c
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+} +This way of going through the page table seems to be largely used across the kernel (cf KASAN population functions of arm64/x86) so I do think this patch brings value to Nylon and Nick's patch.
I can propose a real patch if you agree and I'll add a co-developed by Nylon/Nick since this only 'improves' theirs.
Thanks,
Alex
Quoted 9 lines
Quoted 2 lines
I agree with your proposal, but when I try your patch that it dosen't work because
kasan_early_shadow_pgd_nextfunction wasn't define.Oops, I messed up my rebase, please replace '
kasan_early_shadow_pgd_next' with 'kasan_early_shadow_pmd'.Thank you for your feeback,
Alex
Did you have time to test the above fix ? It would be nice to replace your current patch with the above solution before it gets merged for 5.12, I will propose something tomorrow, feel free to review and test :)
Thanks again,
Alex
Quoted 35 lines
Quoted 35 lines
Do you have complete patch? or just I missed some content?
Quoted 33 lines
Quoted 33 lines
arch/riscv/mm/kasan_init.c +10
-0+void __init kasan_shallow_populate (void *start,void *end) +{ + unsigned long vaddr = (unsigned long)start & PAGE_MASK; + unsigned long vend = PAGE_ALIGN((unsigned long)end); + + kasan_shallow_populate_pgd(vaddr, vend); + + local_flush_tlb_all(); +} +void
__initkasan_init(void)
{
phys_addr_t_start,_end;arch/riscv/mm/kasan_init.c +9
-1@@ -206,7 +231,15 @@ void __init kasan_init(void)*/ kasan_populate_early_shadow((void *)KASAN_SHADOW_START, (void *)kasan_mem_to_shadow((void *) -VMALLOC_END )); +VMEMMAP_END )); + if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) + kasan_shallow_populate( + (void *)kasan_mem_to_shadow((void *)VMALLOC_START ), + (void *)kasan_mem_to_shadow((void *)VMALLOC_END )); + else + kasan_populate_early_shadow( + (void *)kasan_mem_to_shadow((void *)VMALLOC_START ), + (void *)kasan_mem_to_shadow((void*)VMALLOC_END));
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
/* Populate the linear mapping */
for_each_mem_range(i, &_start, &_end){_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v2 1/1] riscv/kasan: add KASAN_VMALLOC support
Hi Alex, Palmer
Quoted 209 lines · Alex Ghiti
Hi Nylon,
Le 2/22/21 ?? 12:13 PM, Alex Ghiti a ??crit??:
Quoted 198 lines
Le 2/21/21 ?? 8:37 PM, Nylon Chen a ??crit??:
Quoted 189 lines
Hi Alex, Palmer
Sorry I missed this message.
Quoted 185 lines · Alex Ghiti
Le 2/13/21 ?? 5:52 AM, Alex Ghiti a ??crit??:
Quoted 174 lines
Hi Nylon, Palmer,
Le 2/8/21 ?? 1:28 AM, Alex Ghiti a ??crit??:
Quoted 129 lines
Hi Nylon,
Le 1/22/21 ?? 10:56 PM, Palmer Dabbelt a ??crit??:
Quoted 10 lines · On Fri, 15 Jan 2021 21:58:35 PST (-0800), [email protected] wrote:
Quoted 9 lines
It references to x86/s390 architecture.
Quoted 1 line
Quoted 1 line
So, it doesn't map the early shadow page to cover VMALLOC space.
Prepopulate top level page table for the range that would otherwise be
empty.lower levels are filled dynamically upon memory allocation while booting.
I think we can improve the changelog a bit here with something like that:
"KASAN vmalloc space used to be mapped using kasan early shadow page.
KASAN_VMALLOCrequires the top-level of the kernel page table to be properly populated, lower levels being filled dynamically upon memory allocation at runtime."Quoted 52 lines
Quoted 52 lines
Signed-off-by Nylon Chen <[email protected]>Signed-off-by Nick Hu <[email protected]>---
????arch/riscv/Kconfig???????????????? |?? 1 + ????arch/riscv/mm/kasan_init.c | 57
+++++++++++++++++++++++++++++++++++++-
????2 files changed, 57 insertions(+), 1 deletion(-)arch/riscv/Kconfig +0
-0@@ -57,6 +57,7 @@ config RISCV?????????? select
HAVE_ARCH_JUMP_LABEL?????????? selectHAVE_ARCH_JUMP_LABEL_RELATIVE?????????? selectHAVE_ARCH_KASANif MMU && 64BIT +?????? selectHAVE_ARCH_KASAN_VMALLOCif MMU && 64BIT ?????????? selectHAVE_ARCH_KGDB
?????????? selectHAVE_ARCH_KGDB_QXFER_PKT?????????? selectHAVE_ARCH_MMAP_RND_BITSif MMUarch/riscv/mm/kasan_init.c +0
-0@@ -9,6 +9,19 @@????#include <
linux/pgtable.h>
????#include <asm/tlbflush.h>
????#include <asm/fixmap.h>arch/riscv/mm/kasan_init.c +9
-0+#include <asm/pgalloc.h> + +static __init void *early_alloc (size_t size,int node) +{ +?????? void *ptr = memblock_alloc_try_nid(size, size, +?????????????? __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, node); + +?????? if (!ptr) +?????????????? panic("%pS: Failed to allocate %zu bytes align=%zx nid=%dfrom=%llx\n",
+??????????????????????__func__, size, size, node, (u64)__pa(MAX_DMA_ADDRESS));arch/riscv/mm/kasan_init.c +3
-0+ +?????? return ptr; +}????extern
pgd_tearly_pg_dir[PTRS_PER_PGD];
????asmlinkage void__initkasan_early_init(void)arch/riscv/mm/kasan_init.c +0
-0@@ -83,6 +96,40 @@ static void __init populate(void *start, void *end) ?????????? memset(start, 0, end - start); ????}Quoted 14 lines
Quoted 4 lines
arch/riscv/mm/kasan_init.c +4
-0+void __init kasan_shallow_populate (void *start,void *end) +{ +?????? unsigned long vaddr = (unsigned long)start & PAGE_MASK; +?????? unsigned long vend = PAGE_ALIGN((unsigned long)end);arch/riscv/mm/kasan_init.c static void __init populate(void *start, void · line 113
Quoted 10 lines
arch/riscv/mm/kasan_init.c +10
-0+?????? unsigned long pfn; +?????? int index; +?????? void *p; +?????? pud_t *pud_dir, *pud_k; +?????? pgd_t *pgd_dir, *pgd_k; +?????? p4d_t *p4d_dir, *p4d_k; + +?????? while (vaddr < vend) { +?????????????? index = pgd_index(vaddr); +?????????????? pfn = csr_read(CSR_SATP) & SATP_PPN;At this point in the boot process, we know that we use
swapper_pg_dirso no need to read SATP.Quoted 1 line
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void · line 114
Quoted 1 line
arch/riscv/mm/kasan_init.c +1
-0+?????????????? pgd_dir = (pgd_t *)pfn_to_virt(pfn) + index;Here, this
pgd_dirassignment is overwritten 2 lines below, so no need for it.Quoted 2 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void · line 116
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+?????????????? pgd_k = init_mm.pgd + index; +?????????????? pgd_dir = pgd_offset_k(vaddr);
pgd_offset_k(vaddr)= init_mm.pgd +pgd_index(vaddr)sopgd_k==pgd_dir.Quoted 6 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void · line 122
Quoted 6 lines
arch/riscv/mm/kasan_init.c +6
-0+?????????????? set_pgd(pgd_dir, *pgd_k); + +?????????????? p4d_dir = p4d_offset(pgd_dir, vaddr); +?????????????? p4d_k?? = p4d_offset(pgd_k, vaddr); + +?????????????? vaddr = (vaddr + PUD_SIZE) & PUD_MASK;Why do you increase vaddr *before* populating the first one ? And
pud_addr_enddoes that properly: it returns the next pud address if it does not go beyond end address to map.Quoted 6 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void · line 128
Quoted 6 lines
arch/riscv/mm/kasan_init.c +6
-0+?????????????? pud_dir = pud_offset(p4d_dir, vaddr); +?????????????? pud_k = pud_offset(p4d_k, vaddr); + +?????????????? if (pud_present(*pud_dir)) { +?????????????????????? p = early_alloc(PAGE_SIZE, NUMA_NO_NODE); +?????????????????????? pud_populate(&init_mm, pud_dir, p);
init_mmis not needed here.Quoted 2 lines
arch/riscv/mm/kasan_init.c static void __init populate(void *start, void · line 130
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+?????????????? } +?????????????? vaddr += PAGE_SIZE;Why do you need to add
PAGE_SIZE? vaddr already points to the next pud.Quoted 7 lines
It seems like this patch tries to populate userspace page table whereas at this point in the boot process, only
swapper_pg_diris used or am I missing something ?Thanks,
Alex
I implemented this morning a version that fixes all the comments I made earlier. I was able to insert
test_kasan_moduleon both sv39 and sv48 without any modification:set_pgd"goes through" all the unused page table levels, whereas p*d_populate are noop for unused levels.If you have any comment, do not hesitate.
arch/riscv/mm/kasan_init.c +0
-0@@ -195,6 +195,31 @@ static void __init kasan_populate(void *start,void
*end)
?? ??????????????memset(start, KASAN_SHADOW_INIT, end - start); ?? ??}+void
__initkasan_shallow_populate_pgd(unsigned long vaddr, unsigned long end)arch/riscv/mm/kasan_init.c +8
-0+{ +???????????? unsigned long next; +???????????? void *p; +???????????? pgd_t *pgd_k = pgd_offset_k(vaddr); + +???????????? do { +???????????????????????????? next = pgd_addr_end(vaddr, end); +???????????????????????????? if (pgd_page_vaddr(*pgd_k) == (unsignedlong)lm_alias(kasan_early_shadow_pgd_next)) { +???????????????????????????????????????????? p =
memblock_alloc(PAGE_SIZE, PAGE_SIZE); +????????????????????????????????????????????set_pgd(pgd_k, pfn_pgd(PFN_DOWN(__pa(p)), PAGE_TABLE));
+???????????????????????????? }
+???????????? } while (pgd_k++, vaddr = next, vaddr != end);arch/riscv/mm/kasan_init.c
Quoted 2 lines
arch/riscv/mm/kasan_init.c +2
-0+} +This way of going through the page table seems to be largely used across the kernel (cf KASAN population functions of arm64/x86) so I do think this patch brings value to Nylon and Nick's patch.
I can propose a real patch if you agree and I'll add a co-developed by Nylon/Nick since this only 'improves' theirs.
Quoted 7 lines
Quoted 4 lines
Thanks,
Alex
I agree with your proposal, but when I try your patch that it dosen't work
becausekasan_early_shadow_pgd_nextfunction wasn't define.Quoted 7 lines
Oops, I messed up my rebase, please replace '
kasan_early_shadow_pgd_next' with 'kasan_early_shadow_pmd'.Thank you for your feeback,
Alex
Quoted 8 lines
Did you have time to test the above fix ? It would be nice to replace your current patch with the above solution before it gets merged for 5.12, I will propose something tomorrow, feel free to review and test :)
Thanks again,
Alex
Today I follow your fix in our platform, it's workable.
Thank you for your fix.
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Do you have complete patch? or just I missed some content?
Quoted 34 lines
Quoted 34 lines
arch/riscv/mm/kasan_init.c +10
-0+void __init kasan_shallow_populate (void *start,void *end) +{ +???????????? unsigned long vaddr = (unsigned long)start & PAGE_MASK; +???????????? unsigned long vend = PAGE_ALIGN((unsigned long)end); + +???????????? kasan_shallow_populate_pgd(vaddr, vend); + +???????????? local_flush_tlb_all(); +} +?? ??void
__initkasan_init(void)
?? ??{
?? ??????????????phys_addr_t_start,_end;arch/riscv/mm/kasan_init.c +0
-0@@ -206,7 +231,15 @@ void __init kasan_init(void)?? ???????????????? */
?? ?????????????? kasan_populate_early_shadow((void *)KASAN_SHADOW_START, ?? ?????????????????????????????????????????????????????????????????????? (void *)kasan_mem_to_shadow((void *)
-VMALLOC_END));
+VMEMMAP_END));
+???????????? if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) +???????????????????????????? kasan_shallow_populate( +???????????????????????????????????????????? (void *)kasan_mem_to_shadow((void *)VMALLOC_START),
+???????????????????????????????????????????? (void *)kasan_mem_to_shadow((void *)VMALLOC_END));
+???????????? else
+???????????????????????????? kasan_populate_early_shadow( +???????????????????????????????????????????? (void *)kasan_mem_to_shadow((void *)VMALLOC_START),
+???????????????????????????????????????????? (void *)kasan_mem_to_shadow((void *)VMALLOC_END));
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
?? ?????????????? /* Populate the linear mapping */ ?? ??????????????
for_each_mem_range(i, &_start, &_end){_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv