LoongArch: add KASAN_VMALLOC support
PATCH 1/1 LoongArch: add KASAN_VMALLOC support
From: Haoran Jiang <[email protected]>
For the page table of the VMALLOC shadow region, only a partial page table needs to be established, allowing the complete page table entries to be populated on demand during use.
---
1,Through the kasan_shallow_populate_pgd/p4d/pud/pmd/pte functions,
page tables are established for the modules and vmalloc regions,
but the page tables are not fully populated; the complete page
tables are established automatically when vmalloc is used.
If the address is pgd-aligned, the page table is established
down to the pgd level; if it is pud-aligned, it is established
down to the pud level, and so on. While establishing the
/pgd/pud/pmd levels, the page table entries must be initialized
to none. For example, a pmd entry of none is an invalid_pte_table.
Only in this way can the correct page tables be established at
use time. At the same time, boundary issues must be considered
when the address is not aligned. For example, mem_to_shadow(MALLOC_END)
and mem_to_shadow(vmemmap) are in the same pmd, and the region [mem_to_shadow(vmemmap), mem_to_shadow(VMEMMAP_END)] needs to be
mapped to page 0. To avoid affecting the mem_to_shadow(vmemmap) region,
only the pmd entries for [mem_to_shadow(MALLOC_END) & PMD_MASK, mem_to_shadow(MALLOC_END)] need to be initialized to invalid_pte_table. The remaining pmd entries are left as they are.
2,The removal of kasan_populate_early_shadow(mem_to_shadow((void *)VMALLOC_START), mem_to_shadow((void *)KFENCE_AREA_END)) achieves the same effect as kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true);
both map this region to a shared zero page. (KASAN_SHADOW_START, KASAN_SHADOW_END)
already covers (mem_to_shadow((void *)VMALLOC_START), mem_to_shadow((void *)KFENCE_AREA_END)). Other architectures such as arm64/riscv need kasan_populate_early_shadow because clear_shadow(KASAN_SHADOW_START, KASAN_SHADOW_END) is performed before
kasan_populate_early_shadow, so the reserved region must be mapped to the zero
page again. On LoongArch, however, the entire shadow space is first mapped to page 0 via kasan_pgd_populate, and then page tables are established for the required regions, which is simpler and clearer.
3,pgd_none/p4d_none/pmd_none/pte_none were added to the definitions of __pgd_none/__p4d_none/__pud_none/__pmd_none/__pte_none respectively because kasan_populate_early_vm_area_shadow requires them. kasan_populate_early_vm_area_shadow establishes complete KASAN shadow page table mappings for the early vmalloc regions before vmalloc_init. At this point, complete page tables need to be established, but kasan_populate_early_vm_area_shadow is called after step 1, at which time only incomplete page tables have been established for the vmalloc region. The condition for a page table entry to be none requires the pgd_none/p4d_none/pmd_none/pte_none checks. Only by correctly determining whether an entry is none can the complete page tables be established properly.
Test:
https://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson.git
CONFIG_KUNIT=y
CONFIG_KASAN_KUNIT_TEST=m
Add kunit.enable=1 kasan_multi_shot to the cmdline.
Test with 3-level 16K pages and 4-level 4K pages respectively.
before this patch:
...
[ 502.760009] ok 68 vmalloc_oob # SKIP Test requires CONFIG_KASAN_VMALLOC=y
...
[ 504.016000] # kasan: pass:49 fail:0 skip:26 total:75 [ 504.016092] # Totals: pass:49 fail:0 skip:26 total:75 [ 504.016171] ok 1 kasan
after this patch:
...
[ 531.630760] ok 68 vmalloc_oob
...
[ 532.891057] # kasan: pass:50 fail:0 skip:25 total:75 [ 532.891142] # Totals: pass:50 fail:0 skip:25 total:75 [ 532.891219] ok 1 kasan
--- arch/loongarch/Kconfig | 2 + arch/loongarch/mm/kasan_init.c | 173 ++++++++++++++++++++++++++++++--- 2 files changed, 164 insertions(+), 11 deletions(-)
arch/loongarch/Kconfig +2 -0
@@ -119,6 +119,7 @@ config LOONGARCH select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if 64BIT + select HAVE_ARCH_KASAN_VMALLOC select HAVE_ARCH_KFENCE if 64BIT select HAVE_ARCH_KGDB if PERF_EVENTS select HAVE_ARCH_KSTACK_ERASE
@@ -187,6 +188,7 @@ config LOONGARCH select HOTPLUG_SMT if HOTPLUG_CPU select IRQ_FORCED_THREADING select IRQ_LOONGARCH_CPU + select KASAN_VMALLOC if KASAN select LOCK_MM_AND_FIND_VMA select MMU_GATHER_MERGE_VMAS if MMU select MMU_GATHER_RCU_TABLE_FREE
arch/loongarch/mm/kasan_init.c +162 -11
214 lines not on this page · raw message
Signature · 1 line
2.43.0
Huacai Chen
maintainerHi, Haoran,
Quoted 8 lines · On Mon, Oct 12, 2026 at 4:05 PM <[email protected]> wrote:
From: Haoran Jiang <[email protected]>
For the page table of the VMALLOC shadow region, only a partial page table needs to be established, allowing the complete page table entries to be populated on demand during use.
Quoted 5 lines
---
1,Through the kasan_shallow_populate_pgd/p4d/pud/pmd/pte functions, page tables are established for the modules and vmalloc regions, but the page tables are not fully populated; the complete page tables are established automatically when vmalloc is used.
Why also change modules together with vmalloc regions? Because they can both alloc/free dynamically, and the current approch can only handle "one time" for modules?
Quoted 5 lines
If the address is pgd-aligned, the page table is established down to the pgd level; if it is pud-aligned, it is established down to the pud level, and so on. While establishing the /pgd/pud/pmd levels, the page table entries must be initialized to none. For example, a pmd entry of none is an
invalid_pte_table.
Quoted 8 lines
Only in this way can the correct page tables be established at
use time. At the same time, boundary issues must be considered
when the address is not aligned. For example,mem_to_shadow(MALLOC_END)
andmem_to_shadow(vmemmap)are in the same pmd, and the region [mem_to_shadow(vmemmap),mem_to_shadow(VMEMMAP_END)] needs to be
mapped to page 0. To avoid affecting themem_to_shadow(vmemmap)region,
only the pmd entries for [mem_to_shadow(MALLOC_END)&PMD_MASK,mem_to_shadow(MALLOC_END)] need to be initialized toinvalid_pte_table. The remaining pmd entries are left as they are.
kasan_pgd_populate(early) populate the page table to early shadow page, kasan_pgd_populate(!early) populate the page table to normal shadow page, and kasan_shallow_populate() populate the page table to invalid_{pud,pmd,pte}_table?
It seems you are do similar work as RISCV, and RISCV use three APIs for this purpose: kasan_{,early,shallow}_populate_pgd(). But LoongArch use the same API for the 1st and 2nd purpose, can we add a "shallow" parameter to kasan_pgd_populate() so we can also handle the 3th purpose?
Quoted 13 lines
2,The removal of
kasan_populate_early_shadow(mem_to_shadow((void *)VMALLOC_START), mem_to_shadow((void *)KFENCE_AREA_END))achieves the same effect askasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true);
both map this region to a shared zero page. (KASAN_SHADOW_START,KASAN_SHADOW_END)
already covers (mem_to_shadow((void *)VMALLOC_START),mem_to_shadow((void *)KFENCE_AREA_END)). Other architectures such as arm64/riscv needkasan_populate_early_shadowbecauseclear_shadow(KASAN_SHADOW_START, KASAN_SHADOW_END)is performed before
kasan_populate_early_shadow, so the reserved region must be mapped to the zero
page again. On LoongArch, however, the entire shadow space is first mapped to page 0 viakasan_pgd_populate, and then page tables are established for the required regions, which is simpler and clearer.
Quoted 7 lines
3,pgd_none/p4d_none/pmd_none/pte_none were added to the definitions of __pgd_none/__p4d_none/__pud_none/__pmd_none/__pte_none respectively because
kasan_populate_early_vm_area_shadowrequires them.kasan_populate_early_vm_area_shadowestablishes complete KASAN shadow page table mappings for the early vmalloc regions beforevmalloc_init. At this point, complete page tables need to be established, butkasan_populate_early_vm_area_shadowis called after step 1, at which time
I'm a little confusing here. What is "step 1" here?
Quoted 53 lines
only incomplete page tables have been established for the vmalloc region. The condition for a page table entry to be none requires the pgd_none/p4d_none/pmd_none/pte_none checks. Only by correctly determining whether an entry is none can the complete page tables be established properly.
Test:
https://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson.git
CONFIG_KUNIT=y
CONFIG_KASAN_KUNIT_TEST=mAdd
kunit.enable=1 kasan_multi_shotto the cmdline.Test with 3-level 16K pages and 4-level 4K pages respectively.
before this patch:
...
[ 502.760009] ok 68 vmalloc_oob # SKIP Test requires CONFIG_KASAN_VMALLOC=y...
[ 504.016000] # kasan: pass:49 fail:0 skip:26 total:75 [ 504.016092] # Totals: pass:49 fail:0 skip:26 total:75 [ 504.016171] ok 1 kasanafter this patch:
...
[ 531.630760] ok 68 vmalloc_oob...
[ 532.891057] # kasan: pass:50 fail:0 skip:25 total:75 [ 532.891142] # Totals: pass:50 fail:0 skip:25 total:75 [ 532.891219] ok 1 kasanSigned-off-by Haoran Jiang <[email protected]>--- arch/loongarch/Kconfig | 2 + arch/loongarch/mm/kasan_init.c | 173 ++++++++++++++++++++++++++++++--- 2 files changed, 164 insertions(+), 11 deletions(-)arch/loongarch/Kconfig +0
-0
arch/loongarch/Kconfig config LOONGARCH · line 122
Quoted 4 lines
arch/loongarch/Kconfig +1
-0@@ -119,6 +119,7 @@ config LOONGARCH select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if 64BIT + select HAVE_ARCH_KASAN_VMALLOC
I think this should be limited by 64BIT.
Huacai
Quoted 236 lines
arch/loongarch/Kconfig +1
-0select HAVE_ARCH_KFENCE if 64BIT select HAVE_ARCH_KGDB if PERF_EVENTS select HAVE_ARCH_KSTACK_ERASE@@ -187,6 +188,7 @@ config LOONGARCH select HOTPLUG_SMT if HOTPLUG_CPU select IRQ_FORCED_THREADING select IRQ_LOONGARCH_CPU + select KASAN_VMALLOC if KASAN select LOCK_MM_AND_FIND_VMA select MMU_GATHER_MERGE_VMAS if MMU select MMU_GATHER_RCU_TABLE_FREEarch/loongarch/mm/kasan_init.c +162
-12@@ -17,28 +17,28 @@ static pgd_t kasan_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);#define __pgd_none (early, pgd) (0)#else #define __pgd_none (early, pgd) (early ? (pgd_val(pgd) == 0) : \ -(__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d))) +((__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d)) || pgd_none(pgd)))#endif #ifdef __PAGETABLE_PUD_FOLDED#define __p4d_none (early, p4d) (0)#else #define __p4d_none (early, p4d) (early ? (p4d_val(p4d) == 0) : \ -(__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud))) +((__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud)) || p4d_none(p4d)))#endif #ifdef __PAGETABLE_PMD_FOLDED#define __pud_none (early, pud) (0)#else #define __pud_none (early, pud) (early ? (pud_val(pud) == 0) : \ -(__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd))) +((__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd)) || pud_none(pud)))#endif #define __pmd_none (early, pmd) (early ? (pmd_val(pmd) == 0) : \ -(__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte))) +((__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte)) || pmd_none(pmd)))#define __pte_none (early, pte) (early ? pte_none(pte) : \ -((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page))) +(((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page)) || pte_none(pte)))static void *mem_to_shadow (const void *addr) {@@ -242,6 +242,160 @@ static void __init kasan_map_populate(unsigned long start, unsigned long end,kasan_pgd_populate (start &PAGE_MASK ,PAGE_ALIGN (end), node, false); } +static void __init kasan_shallow_populate_pte (pmd_t *pmdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pte_t *ptep =pte_offset_kernel (pmdp, addr); + +do { + next = addr +PAGE_SIZE ; +if (__pte_none (false,ptep_get (ptep))) +pte_clear (&init_mm, addr, ptep); + }while (ptep++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pmd (pud_t *pudp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pmd_t *pmdp =pmd_offset (pudp, addr); + +do { + next =pmd_addr_end (addr, end); + +if (__pmd_none (false,pmdp_get (pmdp))) { +phys_addr_t pte_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PMD_SIZE ) && end - addr >=PMD_SIZE ) { +kernel_pte_init (__va (pte_phys)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); +continue ; + }else { +memcpy (__va (pte_phys), kasan_early_shadow_pte, +sizeof (kasan_early_shadow_pte)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); + } + } +kasan_shallow_populate_pte (pmdp, addr, next, node); + }while (pmdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pud (p4d_t *p4dp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pud_t *pudp =pud_offset (p4dp, addr); + +do { + next =pud_addr_end (addr, end); + +if (__pud_none (false,pudp_get (pudp))) { + +phys_addr_t pmd_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PUD_SIZE ) && end - addr >=PUD_SIZE ) { +#ifndef __PAGETABLE_PMD_FOLDED +pmd_init (__va (pmd_phys)); +#endif +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); +continue ; + }else { +memcpy (__va (pmd_phys), kasan_early_shadow_pmd, +sizeof (kasan_early_shadow_pmd)); +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); + + } + } + +kasan_shallow_populate_pmd (pudp, addr, next, node); + }while (pudp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_p4d (pgd_t *pgdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +p4d_t *p4dp =p4d_offset (pgdp, addr); + +do { + next =p4d_addr_end (addr, end); + +if (__p4d_none (false,p4dp_get (p4dp))) { + +phys_addr_t pud_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,P4D_SIZE ) && end - addr >=P4D_SIZE ) { +#ifndef __PAGETABLE_PUD_FOLDED +pud_init (__va (pud_phys)); +#endif +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); +continue ; + }else { +memcpy (__va (pud_phys), kasan_early_shadow_pud, +sizeof (kasan_early_shadow_pud)); +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); + } + } +kasan_shallow_populate_pud (p4dp, addr, next, node); + }while (p4dp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pgd (unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pgd_t *pgdp; + + pgdp =pgd_offset_k (addr); + +do { + next =pgd_addr_end (addr, end); + +if (__pgd_none (false,pgdp_get (pgdp))) { + +phys_addr_t p4d_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PGDIR_SIZE ) && end - addr >=PGDIR_SIZE ) { +pgd_init (__va (p4d_phys)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); +continue ; + }else { +memcpy (__va (p4d_phys), kasan_early_shadow_p4d, +sizeof (kasan_early_shadow_p4d)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); + } + } + +kasan_shallow_populate_p4d (pgdp, addr, next, node); + }while (pgdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate (unsigned long start,unsigned long end,int node) +{ +unsigned long vaddr = (unsigned long )start &PAGE_MASK ; +unsigned long vend =PAGE_ALIGN ((unsigned long )end); + +kasan_shallow_populate_pgd (vaddr, vend, node); +} + +#ifdef CONFIG_KASAN_VMALLOC +void __init kasan_populate_early_vm_area_shadow (void *start,unsigned long size) +{ +unsigned long shadow_start, shadow_end; + +if (!is_vmalloc_or_module_addr (start)) +return ; + + shadow_start = (unsigned long )mem_to_shadow (start); + shadow_start =ALIGN_DOWN (shadow_start,PAGE_SIZE ); + shadow_end = (unsigned long )mem_to_shadow (start + size); + shadow_end =ALIGN (shadow_end,PAGE_SIZE ); + +kasan_map_populate (shadow_start, shadow_end,NUMA_NO_NODE ); +} +#endif + asmlinkage void __init kasan_early_init(void) {BUILD_BUG_ON (!IS_ALIGNED (KASAN_SHADOW_START ,PGDIR_SIZE ));@@ -297,9 +451,6 @@ void __init kasan_init(void)/* Maps everything to a single page of zeroes */ kasan_pgd_populate (KASAN_SHADOW_START ,KASAN_SHADOW_END ,NUMA_NO_NODE , true); -kasan_populate_early_shadow (mem_to_shadow ((void *)VMALLOC_START ), -mem_to_shadow ((void *)KFENCE_AREA_END )); -/* Populate the linear mapping */ for_each_mem_range (i, &pa_start, &pa_end) {void *start = (void *)phys_to_virt (pa_start);@@ -309,9 +460,9 @@ void __init kasan_init(void) (unsigned long)mem_to_shadow(end), NUMA_NO_NODE); } -/* Populate modules mapping */ -kasan_map_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), - (unsigned long )mem_to_shadow ((void *)MODULES_END ),NUMA_NO_NODE ); +kasan_shallow_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), + (unsigned long )mem_to_shadow ((void *)VMALLOC_END ),NUMA_NO_NODE ); +/* * KAsan may reuse the contents of kasan_early_shadow_pte directly, so we * should make sure that it maps the zero page read-only. -- 2.43.0
2026年10月14日 12:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 19 lines
Hi, Haoran,
Quoted 8 lines · On Mon, Oct 12, 2026 at 4:05 PM <[email protected]> wrote:
From: Haoran Jiang <[email protected]>
For the page table of the VMALLOC shadow region, only a partial page table needs to be established, allowing the complete page table entries to be populated on demand during use.
Quoted 6 lines
---
1,Through the kasan_shallow_populate_pgd/p4d/pud/pmd/pte functions, page tables are established for the modules and vmalloc regions, but the page tables are not fully populated; the complete page tables are established automatically when vmalloc is used.
Quoted 4 lines
Why also change modules together with vmalloc regions? Because they can both alloc/free dynamically, and the current approch can only handle "one time" for modules?
The shadow memory for modules regions is the same as that of vmalloc regions, full page tables can be built on demand during usage. Memory is allocated when loading modules, and the shadow memory of the allocated memory establishes complete page tables via kasan_populate_vmalloc_do (load_module->execmem_alloc->kasan_populate_vmalloc_do), similar to riscv and arm64.
Quoted 20 lines
Quoted 15 lines
If the address is pgd-aligned, the page table is established
down to the pgd level; if it is pud-aligned, it is established
down to the pud level, and so on. While establishing the
/pgd/pud/pmd levels, the page table entries must be initialized
to none. For example, a pmd entry of none is aninvalid_pte_table.
Only in this way can the correct page tables be established at
use time. At the same time, boundary issues must be considered
when the address is not aligned. For example,mem_to_shadow(MALLOC_END)
andmem_to_shadow(vmemmap)are in the same pmd, and the region [mem_to_shadow(vmemmap),mem_to_shadow(VMEMMAP_END)] needs to be
mapped to page 0. To avoid affecting themem_to_shadow(vmemmap)region,
only the pmd entries for [mem_to_shadow(MALLOC_END)&PMD_MASK,mem_to_shadow(MALLOC_END)] need to be initialized toinvalid_pte_table. The remaining pmd entries are left as they are.
kasan_pgd_populate(early)populate the page table to early shadow page,kasan_pgd_populate(!early)populate the page table to normal shadow page, andkasan_shallow_populate()populate the page table to invalid_{pud,pmd,pte}_table?
Quoted 6 lines
It seems you are do similar work as RISCV, and RISCV use three APIs for this purpose: kasan_{,early,shallow}_populate_pgd(). But LoongArch use the same API for the 1st and 2nd purpose, can we add a "shallow" parameter to
kasan_pgd_populate()so we can also handle the 3th purpose?
Yes. kasan_shallow_populate() builds incomplete shadow page tables for vmalloc and modules regions and needs to handle boundaries. As an example:mem_to_shadow(VMALLOC_END) and mem_to_shadow(vmemmap) share one PTE table. The PTE entry for mem_to_shadow(VMALLOC_END) must be none, whereas the PTE entry for mem_to_shadow(vmemmap) points to early_shadow_page. The final page table layout is shown below
pte +------
+------------------+
|early_shadow_page |
pmd +------------------+
+----------------+ |early_shadow_page |
|early_shadow_pte| +------------------+
+----------------+ | global |
| ptep |-----------+------------------+
+----------------+
| ... | pte
+----------------+ +--------------+
| ... | | global |
pgd +----------------+ +--------------+
+------------------+ | ptep |---------------| global |
| pmdp +----------+----------------+ +--------------+
+------------------+ pmd
| pmdp |----------+--------------+
+------------------+ |invalid_pte |
| | +--------------+
| ... | |invalid_pte |
| | +--------------+
+------------------+
| ... |
+------------------+ pmd
| ... | +------------+
+------------------+ | invalid_pte|
| ... | +------------+
+------------------+ | invalid_pte|
| pmdp |---------- +------------+
+------------------+
| ... |
+------------------+kasan_pgd_populate() can handle the third case by adding a parameter. I will implement this in the next version.
Quoted 21 lines
Quoted 13 lines
2,The removal of kasan_populate_early_shadow(mem_to_shadow((void *)VMALLOC_START),
mem_to_shadow((void *)KFENCE_AREA_END)) achieves the same effect askasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true); both map this region to a shared zero page. (KASAN_SHADOW_START,KASAN_SHADOW_END) already covers (mem_to_shadow((void *)VMALLOC_START),mem_to_shadow((void *)KFENCE_AREA_END)). Other architectures such as arm64/riscv needkasan_populate_early_shadowbecauseclear_shadow(KASAN_SHADOW_START, KASAN_SHADOW_END)is performed beforekasan_populate_early_shadow, so the reserved region must be mapped to the zero page again. On LoongArch, however, the entire shadow space is first mapped to page 0 viakasan_pgd_populate, and then page tables are established for the required regions, which is simpler and clearer.Quoted 8 lines
3,pgd_none/p4d_none/pmd_none/pte_none were added to the definitions of __pgd_none/__p4d_none/__pud_none/__pmd_none/__pte_none respectively because
kasan_populate_early_vm_area_shadowrequires them.kasan_populate_early_vm_area_shadowestablishes complete KASAN shadow page table mappings for the early vmalloc regions beforevmalloc_init. At this point, complete page tables need to be established, butkasan_populate_early_vm_area_shadowis called after step 1, at which time
Quoted 1 line
I'm a little confusing here. What is "step 1" here?
Step 1 refers to kasan_shallow_populate(). The page tables created after kasan_shallow_populate() are shown in the figure above. We need to check for empty entries using /pgd_none/p4d_none/pmd_none/pone_pte(). Only then can kasan_map_populate() build the full page tables. kasan_populate_early_vm_area_shadow() runs after kasan_shallow_populate() but before vmalloc_init().
At this stage, page tables for vmalloc regions cannot be automatically populated on demand, so we need to build full page tables here.
Quoted 60 lines
Quoted 50 lines
only incomplete page tables have been established for the vmalloc region.
The condition for a page table entry to be none requires the pgd_none/p4d_none/pmd_none/pte_none checks. Only by correctly determining whether an entry is none can the complete page tables be established properly.Test:
https://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson.git
CONFIG_KUNIT=y
CONFIG_KASAN_KUNIT_TEST=mAdd
kunit.enable=1 kasan_multi_shotto the cmdline.Test with 3-level 16K pages and 4-level 4K pages respectively.
before this patch:
...
[ 502.760009] ok 68 vmalloc_oob # SKIP Test requires CONFIG_KASAN_VMALLOC=y...
[ 504.016000] # kasan: pass:49 fail:0 skip:26 total:75 [ 504.016092] # Totals: pass:49 fail:0 skip:26 total:75 [ 504.016171] ok 1 kasanafter this patch:
...
[ 531.630760] ok 68 vmalloc_oob...
[ 532.891057] # kasan: pass:50 fail:0 skip:25 total:75 [ 532.891142] # Totals: pass:50 fail:0 skip:25 total:75 [ 532.891219] ok 1 kasanSigned-off-by Haoran Jiang <[email protected]>---
arch/loongarch/Kconfig | 2 + arch/loongarch/mm/kasan_init.c | 173 ++++++++++++++++++++++++++++++--- 2 files changed, 164 insertions(+), 11 deletions(-)Quoted 10 lines
diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig index
2067d1f2ad7a..c95c6fd8a138100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -119,6 +119,7 @@ config LOONGARCH
selectHAVE_ARCH_JUMP_LABEL
selectHAVE_ARCH_JUMP_LABEL_RELATIVE
selectHAVE_ARCH_KASANif 64BIT
+ selectHAVE_ARCH_KASAN_VMALLOC
Quoted 3 lines
I think this should be limited by 64BIT.
Huacai
I will fix this in the next revision.
Quoted 239 lines
Quoted 238 lines
select
HAVE_ARCH_KFENCEif 64BIT
selectHAVE_ARCH_KGDBifPERF_EVENTS
selectHAVE_ARCH_KSTACK_ERASE
@@ -187,6 +188,7 @@ config LOONGARCH
selectHOTPLUG_SMTifHOTPLUG_CPU
selectIRQ_FORCED_THREADING
selectIRQ_LOONGARCH_CPU
+ selectKASAN_VMALLOCif KASAN
selectLOCK_MM_AND_FIND_VMA
selectMMU_GATHER_MERGE_VMASif MMU
selectMMU_GATHER_RCU_TABLE_FREE
diff --gita/arch/loongarch/mm/kasan_init.cb/arch/loongarch/mm/kasan_init.cindex92ca0ba86406..962c37316200100644
---a/arch/loongarch/mm/kasan_init.c
+++b/arch/loongarch/mm/kasan_init.c
@@ -17,28 +17,28 @@ staticpgd_tkasan_pg_dir[PTRS_PER_PGD]__initdata__aligned(PAGE_SIZE); #define__pgd_none(early, pgd)(0)
#else
#define__pgd_none(early, pgd)(early ? (pgd_val(pgd)== 0) : \
-(__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d))) +((__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d)) ||pgd_none(pgd))) #endif#ifdef
__PAGETABLE_PUD_FOLDED
#define__p4d_none(early, p4d)(0)
#else
#define__p4d_none(early, p4d)(early ? (p4d_val(p4d)== 0) : \
-(__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud))) +((__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud)) ||p4d_none(p4d))) #endif#ifdef
__PAGETABLE_PMD_FOLDED
#define__pud_none(early, pud)(0)
#else
#define__pud_none(early, pud)(early ? (pud_val(pud)== 0) : \
-(__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd))) +((__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd)) ||pud_none(pud))) #endif#define
__pmd_none(early, pmd)(early ? (pmd_val(pmd)== 0) : \
-(__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte))) +((__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte)) ||pmd_none(pmd)))#define
__pte_none(early, pte)(early ?pte_none(pte): \
-((pte_val(pte) &_PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page))) +(((pte_val(pte) &_PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page)) ||pte_none(pte)))static void *mem_to_shadow(const void *addr)
{
@@ -242,6 +242,160 @@ static void__initkasan_map_populate(unsigned long start, unsigned long end,kasan_pgd_populate(start & PAGE_MASK, PAGE_ALIGN(end), node, false);
}+static void
__initkasan_shallow_populate_pte(pmd_t *pmdp, unsigned long addr,
+ unsigned long end, int node)
+{
+ unsigned long next;
+pte_t*ptep =pte_offset_kernel(pmdp, addr);
+
+ do {
+ next = addr +PAGE_SIZE;
+ if (__pte_none(false, ptep_get(ptep)))
+pte_clear(&init_mm, addr, ptep);
+ } while (ptep++, addr = next, addr != end);
+}
+
+static void__initkasan_shallow_populate_pmd(pud_t *pudp, unsigned long addr,
+ unsigned long end, int node)
+{
+ unsigned long next;
+pmd_t*pmdp =pmd_offset(pudp, addr);
+
+ do {
+ next =pmd_addr_end(addr, end);
+
+ if (__pmd_none(false, pmdp_get(pmdp))) {
+phys_addr_tpte_phys=kasan_alloc_zeroed_page(node);
+
+ if (IS_ALIGNED(addr, PMD_SIZE)&& end - addr >=PMD_SIZE) {
+ kernel_pte_init(__va(pte_phys));
+pmd_populate_kernel(NULL, pmdp, (pte_t *)__va(pte_phys));
+ continue;
+ } else {
+ memcpy(__va(pte_phys),kasan_early_shadow_pte,
+sizeof(kasan_early_shadow_pte));
+pmd_populate_kernel(NULL, pmdp, (pte_t *)__va(pte_phys));
+ }
+ }
+kasan_shallow_populate_pte(pmdp, addr, next, node);
+ } while (pmdp++, addr = next, addr != end);
+}
+
+static void__initkasan_shallow_populate_pud(p4d_t *p4dp, unsigned long addr,
+ unsigned long end, int node)
+{
+ unsigned long next;
+pud_t*pudp =pud_offset(p4dp, addr);
+
+ do {
+ next =pud_addr_end(addr, end);
+
+ if (__pud_none(false, pudp_get(pudp))) {
+
+phys_addr_tpmd_phys=kasan_alloc_zeroed_page(node);
+
+ if (IS_ALIGNED(addr, PUD_SIZE)&& end - addr >=PUD_SIZE) {
+#ifndef__PAGETABLE_PMD_FOLDED
+ pmd_init(__va(pmd_phys));
+#endif
+pud_populate(&init_mm, pudp, (pmd_t *)__va(pmd_phys));
+ continue;
+ } else {
+ memcpy(__va(pmd_phys),kasan_early_shadow_pmd,
+sizeof(kasan_early_shadow_pmd));
+pud_populate(&init_mm, pudp, (pmd_t *)__va(pmd_phys));
+
+ }
+ }
+
+kasan_shallow_populate_pmd(pudp, addr, next, node);
+ } while (pudp++, addr = next, addr != end);
+}
+
+static void__initkasan_shallow_populate_p4d(pgd_t *pgdp, unsigned long addr,
+ unsigned long end, int node)
+{
+ unsigned long next;
+p4d_t*p4dp =p4d_offset(pgdp, addr);
+
+ do {
+ next =p4d_addr_end(addr, end);
+
+ if (__p4d_none(false, p4dp_get(p4dp))) {
+
+phys_addr_tpud_phys=kasan_alloc_zeroed_page(node);
+
+ if (IS_ALIGNED(addr, P4D_SIZE)&& end - addr >=P4D_SIZE) {
+#ifndef__PAGETABLE_PUD_FOLDED
+ pud_init(__va(pud_phys));
+#endif
+p4d_populate(&init_mm, p4dp, (pud_t *)__va(pud_phys));
+ continue;
+ } else {
+ memcpy(__va(pud_phys),kasan_early_shadow_pud,
+sizeof(kasan_early_shadow_pud));
+p4d_populate(&init_mm, p4dp, (pud_t *)__va(pud_phys));
+ }
+ }
+kasan_shallow_populate_pud(p4dp, addr, next, node);
+ } while (p4dp++, addr = next, addr != end);
+}
+
+static void__initkasan_shallow_populate_pgd(unsigned long addr,
+ unsigned long end, int node)
+{
+ unsigned long next;
+pgd_t*pgdp;
+
+ pgdp =pgd_offset_k(addr);
+
+ do {
+ next =pgd_addr_end(addr, end);
+
+ if (__pgd_none(false, pgdp_get(pgdp))) {
+
+phys_addr_tp4d_phys=kasan_alloc_zeroed_page(node);
+
+ if (IS_ALIGNED(addr, PGDIR_SIZE)&& end - addr >=PGDIR_SIZE) {
+ pgd_init(__va(p4d_phys));
+pgd_populate(&init_mm, pgdp, (p4d_t *)__va(p4d_phys));
+ continue;
+ } else {
+ memcpy(__va(p4d_phys),kasan_early_shadow_p4d,
+sizeof(kasan_early_shadow_p4d));
+pgd_populate(&init_mm, pgdp, (p4d_t *)__va(p4d_phys));
+ }
+ }
+
+kasan_shallow_populate_p4d(pgdp, addr, next, node);
+ } while (pgdp++, addr = next, addr != end);
+}
+
+static void__initkasan_shallow_populate(unsigned long start, unsigned long end, int node)+{
+ unsigned long vaddr = (unsigned long)start &PAGE_MASK;
+ unsigned long vend =PAGE_ALIGN((unsigned long)end);
+
+kasan_shallow_populate_pgd(vaddr, vend, node);
+}
+
+#ifdefCONFIG_KASAN_VMALLOC
+void__initkasan_populate_early_vm_area_shadow(void *start, unsigned long size)+{
+ unsigned longshadow_start,shadow_end;
+
+ if (!is_vmalloc_or_module_addr(start))
+ return;
+
+shadow_start= (unsigned long)mem_to_shadow(start);
+shadow_start=ALIGN_DOWN(shadow_start, PAGE_SIZE);
+shadow_end= (unsigned long)mem_to_shadow(start + size);
+shadow_end=ALIGN(shadow_end, PAGE_SIZE);
+
+kasan_map_populate(shadow_start, shadow_end, NUMA_NO_NODE);
+}
+#endif
+
asmlinkage void__initkasan_early_init(void)
{
BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_START, PGDIR_SIZE));
@@ -297,9 +451,6 @@ void__initkasan_init(void)
/* Maps everything to a single page of zeroes */kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true);- kasan_populate_early_shadow(mem_to_shadow((void *)VMALLOC_START),
-mem_to_shadow((void *)KFENCE_AREA_END));
-
/* Populate the linear mapping */
for_each_mem_range(i, &pa_start, &pa_end){
void *start = (void *)phys_to_virt(pa_start);
@@ -309,9 +460,9 @@ void__initkasan_init(void)(unsigned long)mem_to_shadow(end),NUMA_NO_NODE);
}- /* Populate modules mapping */
- kasan_map_populate((unsigned long)mem_to_shadow((void *)MODULES_VADDR),
- (unsigned long)mem_to_shadow((void *)MODULES_END),NUMA_NO_NODE);
+ kasan_shallow_populate((unsigned long)mem_to_shadow((void *)MODULES_VADDR),
+ (unsigned long)mem_to_shadow((void *)VMALLOC_END),NUMA_NO_NODE); +
/*
* KAsan may reuse the contents ofkasan_early_shadow_ptedirectly, so we
* should make sure that it maps the zero page read-only.
--
2.43.0
Huacai Chen
maintainerQuoted 28 lines · On Fri, Oct 16, 2026 at 11:05 PM <[email protected]> wrote:
2026年10月14日 12:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 19 lines
Hi, Haoran,
Quoted 8 lines · On Mon, Oct 12, 2026 at 4:05 PM <[email protected]> wrote:
From: Haoran Jiang <[email protected]>
For the page table of the VMALLOC shadow region, only a partial page table needs to be established, allowing the complete page table entries to be populated on demand during use.
Quoted 6 lines
---
1,Through the kasan_shallow_populate_pgd/p4d/pud/pmd/pte functions, page tables are established for the modules and vmalloc regions, but the page tables are not fully populated; the complete page tables are established automatically when vmalloc is used.Quoted 4 lines
Why also change modules together with vmalloc regions? Because they can both alloc/free dynamically, and the current approch can only handle "one time" for modules?
Quoted 5 lines
The shadow memory for modules regions is the same as that of vmalloc regions, full page tables can be built on demand during usage. Memory is allocated when loading modules, and the shadow memory of the allocated memory establishes complete page tables via
kasan_populate_vmalloc_do(load_module->execmem_alloc->kasan_populate_vmalloc_do), similar to riscv and arm64.
The current code can also handle modules, so I want to know the difference. I guess the current code can only handle a module loading once, but this patch makes it possible to load/unload modules repeatedly. But I'm not sure. Anyway, I don't want something to become worse.
Quoted 98 lines
Quoted 21 lines
Quoted 16 lines
If the address is pgd-aligned, the page table is established
down to the pgd level; if it is pud-aligned, it is established
down to the pud level, and so on. While establishing the
/pgd/pud/pmd levels, the page table entries must be initialized
to none. For example, a pmd entry of none is aninvalid_pte_table.
Only in this way can the correct page tables be established at
use time. At the same time, boundary issues must be considered
when the address is not aligned. For example,mem_to_shadow(MALLOC_END)
andmem_to_shadow(vmemmap)are in the same pmd, and the region [mem_to_shadow(vmemmap),mem_to_shadow(VMEMMAP_END)] needs to be
mapped to page 0. To avoid affecting themem_to_shadow(vmemmap)region,
only the pmd entries for [mem_to_shadow(MALLOC_END)&PMD_MASK,mem_to_shadow(MALLOC_END)] need to be initialized toinvalid_pte_table. The remaining pmd entries are left as they are.
kasan_pgd_populate(early)populate the page table to early shadow page,kasan_pgd_populate(!early)populate the page table to normal shadow page, andkasan_shallow_populate()populate the page table to invalid_{pud,pmd,pte}_table?Quoted 6 lines
It seems you are do similar work as RISCV, and RISCV use three APIs for this purpose: kasan_{,early,shallow}_populate_pgd(). But LoongArch use the same API for the 1st and 2nd purpose, can we add a "shallow" parameter to
kasan_pgd_populate()so we can also handle the 3th purpose?Yes.
kasan_shallow_populate()builds incomplete shadow page tables for vmalloc and modules regions and needs to handle boundaries. As an example:mem_to_shadow(VMALLOC_END)andmem_to_shadow(vmemmap)share one PTE table. The PTE entry formem_to_shadow(VMALLOC_END)must be none, whereas the PTE entry formem_to_shadow(vmemmap)points toearly_shadow_page. The final page table layout is shown belowpte +------ +------------------+ |early_shadow_page | pmd +------------------+ +----------------+ |early_shadow_page | |early_shadow_pte| +------------------+ +----------------+ | global | | ptep |-----------+------------------+ +----------------+ | ... | pte +----------------+ +--------------+ | ... | | global | pgd +----------------+ +--------------+ +------------------+ | ptep |---------------| global | | pmdp +----------+----------------+ +--------------+ +------------------+ pmd | pmdp |----------+--------------+ +------------------+ |invalid_pte | | | +--------------+ | ... | |invalid_pte | | | +--------------+ +------------------+ | ... | +------------------+ pmd | ... | +------------+ +------------------+ | invalid_pte| | ... | +------------+ +------------------+ | invalid_pte| | pmdp |---------- +------------+ +------------------+ | ... | +------------------+
kasan_pgd_populate()can handle the third case by adding a parameter. I will implement this in the next version.Quoted 21 lines
Quoted 13 lines
2,The removal of kasan_populate_early_shadow(mem_to_shadow((void *)VMALLOC_START),
mem_to_shadow((void *)KFENCE_AREA_END)) achieves the same effect askasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true); both map this region to a shared zero page. (KASAN_SHADOW_START,KASAN_SHADOW_END) already covers (mem_to_shadow((void *)VMALLOC_START),mem_to_shadow((void *)KFENCE_AREA_END)). Other architectures such as arm64/riscv needkasan_populate_early_shadowbecauseclear_shadow(KASAN_SHADOW_START, KASAN_SHADOW_END)is performed beforekasan_populate_early_shadow, so the reserved region must be mapped to the zero page again. On LoongArch, however, the entire shadow space is first mapped to page 0 viakasan_pgd_populate, and then page tables are established for the required regions, which is simpler and clearer.Quoted 8 lines
3,pgd_none/p4d_none/pmd_none/pte_none were added to the definitions of __pgd_none/__p4d_none/__pud_none/__pmd_none/__pte_none respectively because
kasan_populate_early_vm_area_shadowrequires them.kasan_populate_early_vm_area_shadowestablishes complete KASAN shadow page table mappings for the early vmalloc regions beforevmalloc_init. At this point, complete page tables need to be established, butkasan_populate_early_vm_area_shadowis called after step 1, at which timeQuoted 1 line
I'm a little confusing here. What is "step 1" here?
Quoted 8 lines
Step 1 refers to
kasan_shallow_populate(). The page tables created afterkasan_shallow_populate()are shown in the figure above. We need to check for empty entries using /pgd_none/p4d_none/pmd_none/pone_pte(). Only then cankasan_map_populate()build the full page tables.kasan_populate_early_vm_area_shadow()runs afterkasan_shallow_populate()but beforevmalloc_init().
At this stage, page tables for vmalloc regions cannot be automatically populated on demand, so we need to build full page tables here.
Then it is better to use kasan_shallow_populate() rather than step 1.
Huacai
Quoted 307 lines
Quoted 62 lines
Quoted 52 lines
only incomplete page tables have been established for the vmalloc region.
The condition for a page table entry to be none requires the pgd_none/p4d_none/pmd_none/pte_none checks. Only by correctly determining whether an entry is none can the complete page tables be established properly.Test:
https://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson.git
CONFIG_KUNIT=y
CONFIG_KASAN_KUNIT_TEST=mAdd
kunit.enable=1 kasan_multi_shotto the cmdline.Test with 3-level 16K pages and 4-level 4K pages respectively.
before this patch:
...
[ 502.760009] ok 68 vmalloc_oob # SKIP Test requires CONFIG_KASAN_VMALLOC=y...
[ 504.016000] # kasan: pass:49 fail:0 skip:26 total:75 [ 504.016092] # Totals: pass:49 fail:0 skip:26 total:75 [ 504.016171] ok 1 kasanafter this patch:
...
[ 531.630760] ok 68 vmalloc_oob...
[ 532.891057] # kasan: pass:50 fail:0 skip:25 total:75 [ 532.891142] # Totals: pass:50 fail:0 skip:25 total:75 [ 532.891219] ok 1 kasanSigned-off-by Haoran Jiang <[email protected]>---
arch/loongarch/Kconfig | 2 + arch/loongarch/mm/kasan_init.c | 173 ++++++++++++++++++++++++++++++--- 2 files changed, 164 insertions(+), 11 deletions(-)Quoted 10 lines
diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig index
2067d1f2ad7a..c95c6fd8a138100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -119,6 +119,7 @@ config LOONGARCH
selectHAVE_ARCH_JUMP_LABEL
selectHAVE_ARCH_JUMP_LABEL_RELATIVE
selectHAVE_ARCH_KASANif 64BIT
+ selectHAVE_ARCH_KASAN_VMALLOCQuoted 3 lines
I think this should be limited by 64BIT.
Huacai
I will fix this in the next revision.
Quoted 239 lines
Quoted 238 lines
select
HAVE_ARCH_KFENCEif 64BIT
selectHAVE_ARCH_KGDBifPERF_EVENTS
selectHAVE_ARCH_KSTACK_ERASE
@@ -187,6 +188,7 @@ config LOONGARCH
selectHOTPLUG_SMTifHOTPLUG_CPU
selectIRQ_FORCED_THREADING
selectIRQ_LOONGARCH_CPU
+ selectKASAN_VMALLOCif KASAN
selectLOCK_MM_AND_FIND_VMA
selectMMU_GATHER_MERGE_VMASif MMU
selectMMU_GATHER_RCU_TABLE_FREE
diff --gita/arch/loongarch/mm/kasan_init.cb/arch/loongarch/mm/kasan_init.cindex92ca0ba86406..962c37316200100644
---a/arch/loongarch/mm/kasan_init.c
+++b/arch/loongarch/mm/kasan_init.c
@@ -17,28 +17,28 @@ staticpgd_tkasan_pg_dir[PTRS_PER_PGD]__initdata__aligned(PAGE_SIZE); #define__pgd_none(early, pgd)(0)
#else
#define__pgd_none(early, pgd)(early ? (pgd_val(pgd)== 0) : \
-(__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d))) +((__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d)) ||pgd_none(pgd))) #endif#ifdef
__PAGETABLE_PUD_FOLDED
#define__p4d_none(early, p4d)(0)
#else
#define__p4d_none(early, p4d)(early ? (p4d_val(p4d)== 0) : \
-(__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud))) +((__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud)) ||p4d_none(p4d))) #endif#ifdef
__PAGETABLE_PMD_FOLDED
#define__pud_none(early, pud)(0)
#else
#define__pud_none(early, pud)(early ? (pud_val(pud)== 0) : \
-(__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd))) +((__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd)) ||pud_none(pud))) #endif#define
__pmd_none(early, pmd)(early ? (pmd_val(pmd)== 0) : \
-(__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte))) +((__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte)) ||pmd_none(pmd)))#define
__pte_none(early, pte)(early ?pte_none(pte): \
-((pte_val(pte) &_PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page))) +(((pte_val(pte) &_PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page)) ||pte_none(pte)))static void *mem_to_shadow(const void *addr)
{
@@ -242,6 +242,160 @@ static void__initkasan_map_populate(unsigned long start, unsigned long end,kasan_pgd_populate(start & PAGE_MASK, PAGE_ALIGN(end), node, false);
}+static void
__initkasan_shallow_populate_pte(pmd_t *pmdp, unsigned long addr,
+ unsigned long end, int node)
+{
+ unsigned long next;
+pte_t*ptep =pte_offset_kernel(pmdp, addr);
+
+ do {
+ next = addr +PAGE_SIZE;
+ if (__pte_none(false, ptep_get(ptep)))
+pte_clear(&init_mm, addr, ptep);
+ } while (ptep++, addr = next, addr != end);
+}
+
+static void__initkasan_shallow_populate_pmd(pud_t *pudp, unsigned long addr,
+ unsigned long end, int node)
+{
+ unsigned long next;
+pmd_t*pmdp =pmd_offset(pudp, addr);
+
+ do {
+ next =pmd_addr_end(addr, end);
+
+ if (__pmd_none(false, pmdp_get(pmdp))) {
+phys_addr_tpte_phys=kasan_alloc_zeroed_page(node);
+
+ if (IS_ALIGNED(addr, PMD_SIZE)&& end - addr >=PMD_SIZE) {
+ kernel_pte_init(__va(pte_phys));
+pmd_populate_kernel(NULL, pmdp, (pte_t *)__va(pte_phys));
+ continue;
+ } else {
+ memcpy(__va(pte_phys),kasan_early_shadow_pte,
+sizeof(kasan_early_shadow_pte));
+pmd_populate_kernel(NULL, pmdp, (pte_t *)__va(pte_phys));
+ }
+ }
+kasan_shallow_populate_pte(pmdp, addr, next, node);
+ } while (pmdp++, addr = next, addr != end);
+}
+
+static void__initkasan_shallow_populate_pud(p4d_t *p4dp, unsigned long addr,
+ unsigned long end, int node)
+{
+ unsigned long next;
+pud_t*pudp =pud_offset(p4dp, addr);
+
+ do {
+ next =pud_addr_end(addr, end);
+
+ if (__pud_none(false, pudp_get(pudp))) {
+
+phys_addr_tpmd_phys=kasan_alloc_zeroed_page(node);
+
+ if (IS_ALIGNED(addr, PUD_SIZE)&& end - addr >=PUD_SIZE) {
+#ifndef__PAGETABLE_PMD_FOLDED
+ pmd_init(__va(pmd_phys));
+#endif
+pud_populate(&init_mm, pudp, (pmd_t *)__va(pmd_phys));
+ continue;
+ } else {
+ memcpy(__va(pmd_phys),kasan_early_shadow_pmd,
+sizeof(kasan_early_shadow_pmd));
+pud_populate(&init_mm, pudp, (pmd_t *)__va(pmd_phys));
+
+ }
+ }
+
+kasan_shallow_populate_pmd(pudp, addr, next, node);
+ } while (pudp++, addr = next, addr != end);
+}
+
+static void__initkasan_shallow_populate_p4d(pgd_t *pgdp, unsigned long addr,
+ unsigned long end, int node)
+{
+ unsigned long next;
+p4d_t*p4dp =p4d_offset(pgdp, addr);
+
+ do {
+ next =p4d_addr_end(addr, end);
+
+ if (__p4d_none(false, p4dp_get(p4dp))) {
+
+phys_addr_tpud_phys=kasan_alloc_zeroed_page(node);
+
+ if (IS_ALIGNED(addr, P4D_SIZE)&& end - addr >=P4D_SIZE) {
+#ifndef__PAGETABLE_PUD_FOLDED
+ pud_init(__va(pud_phys));
+#endif
+p4d_populate(&init_mm, p4dp, (pud_t *)__va(pud_phys));
+ continue;
+ } else {
+ memcpy(__va(pud_phys),kasan_early_shadow_pud,
+sizeof(kasan_early_shadow_pud));
+p4d_populate(&init_mm, p4dp, (pud_t *)__va(pud_phys));
+ }
+ }
+kasan_shallow_populate_pud(p4dp, addr, next, node);
+ } while (p4dp++, addr = next, addr != end);
+}
+
+static void__initkasan_shallow_populate_pgd(unsigned long addr,
+ unsigned long end, int node)
+{
+ unsigned long next;
+pgd_t*pgdp;
+
+ pgdp =pgd_offset_k(addr);
+
+ do {
+ next =pgd_addr_end(addr, end);
+
+ if (__pgd_none(false, pgdp_get(pgdp))) {
+
+phys_addr_tp4d_phys=kasan_alloc_zeroed_page(node);
+
+ if (IS_ALIGNED(addr, PGDIR_SIZE)&& end - addr >=PGDIR_SIZE) {
+ pgd_init(__va(p4d_phys));
+pgd_populate(&init_mm, pgdp, (p4d_t *)__va(p4d_phys));
+ continue;
+ } else {
+ memcpy(__va(p4d_phys),kasan_early_shadow_p4d,
+sizeof(kasan_early_shadow_p4d));
+pgd_populate(&init_mm, pgdp, (p4d_t *)__va(p4d_phys));
+ }
+ }
+
+kasan_shallow_populate_p4d(pgdp, addr, next, node);
+ } while (pgdp++, addr = next, addr != end);
+}
+
+static void__initkasan_shallow_populate(unsigned long start, unsigned long end, int node)+{
+ unsigned long vaddr = (unsigned long)start &PAGE_MASK;
+ unsigned long vend =PAGE_ALIGN((unsigned long)end);
+
+kasan_shallow_populate_pgd(vaddr, vend, node);
+}
+
+#ifdefCONFIG_KASAN_VMALLOC
+void__initkasan_populate_early_vm_area_shadow(void *start, unsigned long size)+{
+ unsigned longshadow_start,shadow_end;
+
+ if (!is_vmalloc_or_module_addr(start))
+ return;
+
+shadow_start= (unsigned long)mem_to_shadow(start);
+shadow_start=ALIGN_DOWN(shadow_start, PAGE_SIZE);
+shadow_end= (unsigned long)mem_to_shadow(start + size);
+shadow_end=ALIGN(shadow_end, PAGE_SIZE);
+
+kasan_map_populate(shadow_start, shadow_end, NUMA_NO_NODE);
+}
+#endif
+
asmlinkage void__initkasan_early_init(void)
{
BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_START, PGDIR_SIZE));
@@ -297,9 +451,6 @@ void__initkasan_init(void)
/* Maps everything to a single page of zeroes */kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true);- kasan_populate_early_shadow(mem_to_shadow((void *)VMALLOC_START),
-mem_to_shadow((void *)KFENCE_AREA_END));
-
/* Populate the linear mapping */
for_each_mem_range(i, &pa_start, &pa_end){
void *start = (void *)phys_to_virt(pa_start);
@@ -309,9 +460,9 @@ void__initkasan_init(void)(unsigned long)mem_to_shadow(end),NUMA_NO_NODE);
}- /* Populate modules mapping */
- kasan_map_populate((unsigned long)mem_to_shadow((void *)MODULES_VADDR),
- (unsigned long)mem_to_shadow((void *)MODULES_END),NUMA_NO_NODE);
+ kasan_shallow_populate((unsigned long)mem_to_shadow((void *)MODULES_VADDR),
+ (unsigned long)mem_to_shadow((void *)VMALLOC_END),NUMA_NO_NODE); +
/*
* KAsan may reuse the contents ofkasan_early_shadow_ptedirectly, so we
* should make sure that it maps the zero page read-only.
--
2.43.0
2026年10月17日 16:24, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 34 lines · On Fri, Oct 16, 2026 at 11:05 PM <[email protected]> wrote:
Quoted 26 lines
2026年10月14日 12:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Hi, Haoran,
Quoted 8 lines · On Mon, Oct 12, 2026 at 4:05 PM <[email protected]> wrote:
From: Haoran Jiang <[email protected]>
For the page table of the VMALLOC shadow region, only a partial page table needs to be established, allowing the complete page table entries to be populated on demand during use.
Quoted 6 lines
---
1,Through the kasan_shallow_populate_pgd/p4d/pud/pmd/pte functions, page tables are established for the modules and vmalloc regions, but the page tables are not fully populated; the complete page tables are established automatically when vmalloc is used.Why also change modules together with vmalloc regions? Because they can both alloc/free dynamically, and the current approch can only handle "one time" for modules?
Quoted 6 lines
The shadow memory for modules regions is the same as that of vmalloc regions, full page tables can be built on demand during usage. Memory is allocated when loading modules, and the shadow memory of the allocated memory establishes complete page tables via
kasan_populate_vmalloc_do(load_module->execmem_alloc->kasan_populate_vmalloc_do), similar to riscv and arm64.
Quoted 6 lines
The current code can also handle modules, so I want to know the difference. I guess the current code can only handle a module loading once, but this patch makes it possible to load/unload modules repeatedly. But I'm not sure. Anyway, I don't want something to become worse.
1,With the current approach, the shadow page tables for the modules region are established via kasan_map_populate and KASAN_VMALLOC=n. A test module performs an out-of-bounds write to a global array. After repeated insmod/rmmod cycles, the test module reports false positives. This is because the current flow does not properly clean up the shadow data.
Taking x86 as an example, since x86 can be configured with KASAN_VMALLOC=n, when KASAN_VMALLOC=n, the correct flow is as follows:
execmem_arch_setup -> flags = EXECMEM_KASAN_SHADOW, the flag is set to EXECMEM_KASAN_SHADOW, and the subsequent flow should select the correct path based on the EXECMEM_KASAN_SHADOW flag.
insmod -> execmem_vmalloc -> (kasan && (kasan_alloc_module_shadow(p, size, GFP_KERNEL) -> register_global -> kasan_poison-> rmmod -> kasan_free_module_shadow -> vfree(kasan_mem_to_shadow(vm->addr));
This flow it establishes the shadow page tables, then poisons them, and cleans up the shadow data when the module is unloaded.
However, the current situation on LoongArch is as follows:
execmem_arch_setup -> does not set EXECMEM_KASAN_SHADOW
insmod -> execmem_vmalloc -> does not call kasan_alloc_module_shadow -> register_global -> kasan_poison-> rmmod -> kasan_free_module_shadow -> does not call vfree(kasan_mem_to_shadow(vm->addr));
LoongArch does not set EXECMEM_KASAN_SHADOW during initialization, which causes the shadow data not to be properly cleaned up at unload time, potentially leading to false positives the next time a module is loaded.
On RISC-V/arm64, KASAN_VMALLOC is enabled by default, and the shadow page tables for the module region are established and torn down via kasan_populate_vmalloc_do/kasan_release_vmalloc. This is exactly the approach that kasan_shallow_populate aims to implement.
2,kasan_shallow_populate requires fewer page tables than kasan_map_populate, and it can also save memory during initialization.
Quoted 410 lines
Quoted 92 lines
Quoted 16 lines
If the address is pgd-aligned, the page table is established
down to the pgd level; if it is pud-aligned, it is established
down to the pud level, and so on. While establishing the
/pgd/pud/pmd levels, the page table entries must be initialized
to none. For example, a pmd entry of none is aninvalid_pte_table.
Only in this way can the correct page tables be established at
use time. At the same time, boundary issues must be considered
when the address is not aligned. For example,mem_to_shadow(MALLOC_END)
andmem_to_shadow(vmemmap)are in the same pmd, and the region [mem_to_shadow(vmemmap),mem_to_shadow(VMEMMAP_END)] needs to be
mapped to page 0. To avoid affecting themem_to_shadow(vmemmap)region,
only the pmd entries for [mem_to_shadow(MALLOC_END)&PMD_MASK,mem_to_shadow(MALLOC_END)] need to be initialized toinvalid_pte_table. The remaining pmd entries are left as they are.
kasan_pgd_populate(early)populate the page table to early shadow page,kasan_pgd_populate(!early)populate the page table to normal shadow page, andkasan_shallow_populate()populate the page table to invalid_{pud,pmd,pte}_table?It seems you are do similar work as RISCV, and RISCV use three APIs for this purpose: kasan_{,early,shallow}_populate_pgd(). But LoongArch use the same API for the 1st and 2nd purpose, can we add a "shallow" parameter to
kasan_pgd_populate()so we can also handle the 3th purpose?Yes.
kasan_shallow_populate()builds incomplete shadow page tables for vmalloc and modules regions and needs to handle boundaries. As an example:
mem_to_shadow(VMALLOC_END)andmem_to_shadow(vmemmap)share one PTE table. The PTE entry formem_to_shadow(VMALLOC_END)must be none, whereas the PTE entry formem_to_shadow(vmemmap)points toearly_shadow_page. The final page table layout is shown belowpte +------
+------------------+ |early_shadow_page | pmd +------------------+ +----------------+ |early_shadow_page | |early_shadow_pte| +------------------+ +----------------+ | global | | ptep |-----------+------------------+ +----------------+ | ... | pte +----------------+ +--------------+ | ... | | global | pgd +----------------+ +--------------+ +------------------+ | ptep |---------------| global | | pmdp +----------+----------------+ +--------------+ +------------------+ pmd | pmdp |----------+--------------+ +------------------+ |invalid_pte | | | +--------------+ | ... | |invalid_pte | | | +--------------+ +------------------+ | ... | +------------------+ pmd | ... | +------------+ +------------------+ | invalid_pte| | ... | +------------+ +------------------+ | invalid_pte| | pmdp |---------- +------------+ +------------------+ | ... | +------------------+
kasan_pgd_populate()can handle the third case by adding a parameter. I will implement this in the next version.Quoted 13 lines
2,The removal of
kasan_populate_early_shadow(mem_to_shadow((void *)VMALLOC_START), mem_to_shadow((void *)KFENCE_AREA_END))achieves the same effect askasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true); both map this region to a shared zero page. (KASAN_SHADOW_START,KASAN_SHADOW_END) already covers (mem_to_shadow((void *)VMALLOC_START),mem_to_shadow((void *)KFENCE_AREA_END)). Other architectures such as arm64/riscv needkasan_populate_early_shadowbecauseclear_shadow(KASAN_SHADOW_START, KASAN_SHADOW_END)is performed beforekasan_populate_early_shadow, so the reserved region must be mapped to the zero page again. On LoongArch, however, the entire shadow space is first mapped to page 0 viakasan_pgd_populate, and then page tables are established for the required regions, which is simpler and clearer.Quoted 8 lines
3,pgd_none/p4d_none/pmd_none/pte_none were added to the definitions of __pgd_none/__p4d_none/__pud_none/__pmd_none/__pte_none respectively because
kasan_populate_early_vm_area_shadowrequires them.kasan_populate_early_vm_area_shadowestablishes complete KASAN shadow page table mappings for the early vmalloc regions beforevmalloc_init. At this point, complete page tables need to be established, butkasan_populate_early_vm_area_shadowis called after step 1, at which timeI'm a little confusing here. What is "step 1" here?
Quoted 9 lines
Step 1 refers to
kasan_shallow_populate(). The page tables created afterkasan_shallow_populate()are shown in the figure above. We need to check for empty entries using /pgd_none/p4d_none/pmd_none/pone_pte(). Only then cankasan_map_populate()build the full page tables.kasan_populate_early_vm_area_shadow()runs afterkasan_shallow_populate()but beforevmalloc_init().
At this stage, page tables for vmalloc regions cannot be automatically populated on demand, so we need to build full page tables here.Then it is better to use
kasan_shallow_populate()rather thanstep 1.Huacai
Quoted 304 lines
Quoted 55 lines
only incomplete page tables have been established for the vmalloc region. The condition for a page table entry to be none requires the pgd_none/p4d_none/pmd_none/pte_none checks. Only by correctly determining whether an entry is none can the complete page tables be established properly.
Test:
https://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson.git
CONFIG_KUNIT=y
CONFIG_KASAN_KUNIT_TEST=mAdd
kunit.enable=1 kasan_multi_shotto the cmdline.Test with 3-level 16K pages and 4-level 4K pages respectively.
before this patch:
...
[ 502.760009] ok 68 vmalloc_oob # SKIP Test requires CONFIG_KASAN_VMALLOC=y...
[ 504.016000] # kasan: pass:49 fail:0 skip:26 total:75 [ 504.016092] # Totals: pass:49 fail:0 skip:26 total:75 [ 504.016171] ok 1 kasanafter this patch:
...
[ 531.630760] ok 68 vmalloc_oob...
[ 532.891057] # kasan: pass:50 fail:0 skip:25 total:75 [ 532.891142] # Totals: pass:50 fail:0 skip:25 total:75 [ 532.891219] ok 1 kasanSigned-off-by Haoran Jiang <[email protected]>---
arch/loongarch/Kconfig| 2 +arch/loongarch/mm/kasan_init.c| 173 ++++++++++++++++++++++++++++++---2 files changed, 164 insertions(+), 11 deletions(-)arch/loongarch/Kconfig +0
-0@@ -119,6 +119,7 @@ config LOONGARCHQuoted 5 lines
select
HAVE_ARCH_JUMP_LABEL
selectHAVE_ARCH_JUMP_LABEL_RELATIVE
selectHAVE_ARCH_KASANif 64BIT
+ selectHAVE_ARCH_KASAN_VMALLOCI think this should be limited by 64BIT.
Huacai
I will fix this in the next revision.
Quoted 237 lines
select
HAVE_ARCH_KFENCEif 64BIT
selectHAVE_ARCH_KGDBifPERF_EVENTS
selectHAVE_ARCH_KSTACK_ERASEarch/loongarch/Kconfig +0
-0@@ -187,6 +188,7 @@ config LOONGARCHselect
HOTPLUG_SMTifHOTPLUG_CPU
selectIRQ_FORCED_THREADING
selectIRQ_LOONGARCH_CPU
+ selectKASAN_VMALLOCif KASAN
selectLOCK_MM_AND_FIND_VMA
selectMMU_GATHER_MERGE_VMASif MMU
selectMMU_GATHER_RCU_TABLE_FREEarch/loongarch/mm/kasan_init.c +162
-12@@ -17,28 +17,28 @@ static pgd_t kasan_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);#define __pgd_none (early, pgd) (0)#else #define __pgd_none (early, pgd) (early ? (pgd_val(pgd) == 0) : \ -(__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d))) +((__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d)) || pgd_none(pgd)))#endif #ifdef __PAGETABLE_PUD_FOLDED#define __p4d_none (early, p4d) (0)#else #define __p4d_none (early, p4d) (early ? (p4d_val(p4d) == 0) : \ -(__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud))) +((__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud)) || p4d_none(p4d)))#endif #ifdef __PAGETABLE_PMD_FOLDED#define __pud_none (early, pud) (0)#else #define __pud_none (early, pud) (early ? (pud_val(pud) == 0) : \ -(__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd))) +((__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd)) || pud_none(pud)))#endif #define __pmd_none (early, pmd) (early ? (pmd_val(pmd) == 0) : \ -(__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte))) +((__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte)) || pmd_none(pmd)))#define __pte_none (early, pte) (early ? pte_none(pte) : \ -((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page))) +(((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page)) || pte_none(pte)))static void *mem_to_shadow (const void *addr) {@@ -242,6 +242,160 @@ static void __init kasan_map_populate(unsigned long start, unsigned long end,kasan_pgd_populate (start &PAGE_MASK ,PAGE_ALIGN (end), node, false); } +static void __init kasan_shallow_populate_pte (pmd_t *pmdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pte_t *ptep =pte_offset_kernel (pmdp, addr); + +do { + next = addr +PAGE_SIZE ; +if (__pte_none (false,ptep_get (ptep))) +pte_clear (&init_mm, addr, ptep); + }while (ptep++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pmd (pud_t *pudp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pmd_t *pmdp =pmd_offset (pudp, addr); + +do { + next =pmd_addr_end (addr, end); + +if (__pmd_none (false,pmdp_get (pmdp))) { +phys_addr_t pte_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PMD_SIZE ) && end - addr >=PMD_SIZE ) { +kernel_pte_init (__va (pte_phys)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); +continue ; + }else { +memcpy (__va (pte_phys), kasan_early_shadow_pte, +sizeof (kasan_early_shadow_pte)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); + } + } +kasan_shallow_populate_pte (pmdp, addr, next, node); + }while (pmdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pud (p4d_t *p4dp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pud_t *pudp =pud_offset (p4dp, addr); + +do { + next =pud_addr_end (addr, end); + +if (__pud_none (false,pudp_get (pudp))) { + +phys_addr_t pmd_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PUD_SIZE ) && end - addr >=PUD_SIZE ) { +#ifndef __PAGETABLE_PMD_FOLDED +pmd_init (__va (pmd_phys)); +#endif +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); +continue ; + }else { +memcpy (__va (pmd_phys), kasan_early_shadow_pmd, +sizeof (kasan_early_shadow_pmd)); +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); + + } + } + +kasan_shallow_populate_pmd (pudp, addr, next, node); + }while (pudp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_p4d (pgd_t *pgdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +p4d_t *p4dp =p4d_offset (pgdp, addr); + +do { + next =p4d_addr_end (addr, end); + +if (__p4d_none (false,p4dp_get (p4dp))) { + +phys_addr_t pud_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,P4D_SIZE ) && end - addr >=P4D_SIZE ) { +#ifndef __PAGETABLE_PUD_FOLDED +pud_init (__va (pud_phys)); +#endif +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); +continue ; + }else { +memcpy (__va (pud_phys), kasan_early_shadow_pud, +sizeof (kasan_early_shadow_pud)); +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); + } + } +kasan_shallow_populate_pud (p4dp, addr, next, node); + }while (p4dp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pgd (unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pgd_t *pgdp; + + pgdp =pgd_offset_k (addr); + +do { + next =pgd_addr_end (addr, end); + +if (__pgd_none (false,pgdp_get (pgdp))) { + +phys_addr_t p4d_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PGDIR_SIZE ) && end - addr >=PGDIR_SIZE ) { +pgd_init (__va (p4d_phys)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); +continue ; + }else { +memcpy (__va (p4d_phys), kasan_early_shadow_p4d, +sizeof (kasan_early_shadow_p4d)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); + } + } + +kasan_shallow_populate_p4d (pgdp, addr, next, node); + }while (pgdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate (unsigned long start,unsigned long end,int node) +{ +unsigned long vaddr = (unsigned long )start &PAGE_MASK ; +unsigned long vend =PAGE_ALIGN ((unsigned long )end); + +kasan_shallow_populate_pgd (vaddr, vend, node); +} + +#ifdef CONFIG_KASAN_VMALLOC +void __init kasan_populate_early_vm_area_shadow (void *start,unsigned long size) +{ +unsigned long shadow_start, shadow_end; + +if (!is_vmalloc_or_module_addr (start)) +return ; + + shadow_start = (unsigned long )mem_to_shadow (start); + shadow_start =ALIGN_DOWN (shadow_start,PAGE_SIZE ); + shadow_end = (unsigned long )mem_to_shadow (start + size); + shadow_end =ALIGN (shadow_end,PAGE_SIZE ); + +kasan_map_populate (shadow_start, shadow_end,NUMA_NO_NODE ); +} +#endif + asmlinkage void __init kasan_early_init(void) {BUILD_BUG_ON (!IS_ALIGNED (KASAN_SHADOW_START ,PGDIR_SIZE ));@@ -297,9 +451,6 @@ void __init kasan_init(void)/* Maps everything to a single page of zeroes */ kasan_pgd_populate (KASAN_SHADOW_START ,KASAN_SHADOW_END ,NUMA_NO_NODE , true); -kasan_populate_early_shadow (mem_to_shadow ((void *)VMALLOC_START ), -mem_to_shadow ((void *)KFENCE_AREA_END )); -/* Populate the linear mapping */ for_each_mem_range(i, &pa_start, &pa_end) {void *start = (void *)phys_to_virt (pa_start);@@ -309,9 +460,9 @@ void __init kasan_init(void) (unsigned long)mem_to_shadow(end), NUMA_NO_NODE); } -/* Populate modules mapping */ -kasan_map_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), - (unsigned long )mem_to_shadow ((void *)MODULES_END ),NUMA_NO_NODE ); +kasan_shallow_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), + (unsigned long )mem_to_shadow ((void *)VMALLOC_END ),NUMA_NO_NODE ); +/* * KAsan may reuse the contents of kasan_early_shadow_pte directly, so we * should make sure that it maps the zero page read-only. -- 2.43.0
Huacai Chen
maintainerQuoted 53 lines · On Tue, Oct 20, 2026 at 10:37 AM <[email protected]> wrote:
2026年10月17日 16:24, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 34 lines · On Fri, Oct 16, 2026 at 11:05 PM <[email protected]> wrote:
Quoted 26 lines
2026年10月14日 12:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Hi, Haoran,
Quoted 8 lines · On Mon, Oct 12, 2026 at 4:05 PM <[email protected]> wrote:
From: Haoran Jiang <[email protected]>
For the page table of the VMALLOC shadow region, only a partial page table needs to be established, allowing the complete page table entries to be populated on demand during use.
Quoted 6 lines
---
1,Through the kasan_shallow_populate_pgd/p4d/pud/pmd/pte functions, page tables are established for the modules and vmalloc regions, but the page tables are not fully populated; the complete page tables are established automatically when vmalloc is used.Why also change modules together with vmalloc regions? Because they can both alloc/free dynamically, and the current approch can only handle "one time" for modules?
Quoted 6 lines
The shadow memory for modules regions is the same as that of vmalloc regions, full page tables can be built on demand during usage. Memory is allocated when loading modules, and the shadow memory of the allocated memory establishes complete page tables via
kasan_populate_vmalloc_do(load_module->execmem_alloc->kasan_populate_vmalloc_do), similar to riscv and arm64.Quoted 6 lines
The current code can also handle modules, so I want to know the difference. I guess the current code can only handle a module loading once, but this patch makes it possible to load/unload modules repeatedly. But I'm not sure. Anyway, I don't want something to become worse.
1,With the current approach, the shadow page tables for the modules region are established via
kasan_map_populateandKASAN_VMALLOC=n. A test module performs an out-of-bounds write to a global array. After repeatedinsmod/rmmodcycles, the test module reports false positives. This is because the current flow does not properly clean up the shadow data.
Quoted 2 lines
Taking x86 as an example, since x86 can be configured with KASAN_VMALLOC=n, when KASAN_VMALLOC=n, the correct flow is as follows:
x86 has some trouble implementing KASAN_VMALLOC? Or does x86's solution have its advantage?
Huacai
Quoted 448 lines
execmem_arch_setup-> flags =EXECMEM_KASAN_SHADOW, the flag is set toEXECMEM_KASAN_SHADOW, and the subsequent flow should select the correct path based on theEXECMEM_KASAN_SHADOWflag.insmod ->
execmem_vmalloc-> (kasan && (kasan_alloc_module_shadow(p, size, GFP_KERNEL)->register_global-> kasan_poison-> rmmod ->kasan_free_module_shadow-> vfree(kasan_mem_to_shadow(vm->addr));This flow it establishes the shadow page tables, then poisons them, and cleans up the shadow data when the module is unloaded.
However, the current situation on LoongArch is as follows:
execmem_arch_setup-> does not setEXECMEM_KASAN_SHADOWinsmod ->
execmem_vmalloc-> does not callkasan_alloc_module_shadow->register_global-> kasan_poison-> rmmod ->kasan_free_module_shadow-> does not call vfree(kasan_mem_to_shadow(vm->addr));LoongArch does not set
EXECMEM_KASAN_SHADOWduring initialization, which causes the shadow data not to be properly cleaned up at unload time, potentially leading to false positives the next time a module is loaded.On RISC-V/arm64,
KASAN_VMALLOCis enabled by default, and the shadow page tables for the module region are established and torn down via kasan_populate_vmalloc_do/kasan_release_vmalloc. This is exactly the approach thatkasan_shallow_populateaims to implement.2,kasan_shallow_populate requires fewer page tables than
kasan_map_populate, and it can also save memory during initialization.Quoted 410 lines
Quoted 92 lines
Quoted 16 lines
If the address is pgd-aligned, the page table is established
down to the pgd level; if it is pud-aligned, it is established
down to the pud level, and so on. While establishing the
/pgd/pud/pmd levels, the page table entries must be initialized
to none. For example, a pmd entry of none is aninvalid_pte_table.
Only in this way can the correct page tables be established at
use time. At the same time, boundary issues must be considered
when the address is not aligned. For example,mem_to_shadow(MALLOC_END)
andmem_to_shadow(vmemmap)are in the same pmd, and the region [mem_to_shadow(vmemmap),mem_to_shadow(VMEMMAP_END)] needs to be
mapped to page 0. To avoid affecting themem_to_shadow(vmemmap)region,
only the pmd entries for [mem_to_shadow(MALLOC_END)&PMD_MASK,mem_to_shadow(MALLOC_END)] need to be initialized toinvalid_pte_table. The remaining pmd entries are left as they are.
kasan_pgd_populate(early)populate the page table to early shadow page,kasan_pgd_populate(!early)populate the page table to normal shadow page, andkasan_shallow_populate()populate the page table to invalid_{pud,pmd,pte}_table?It seems you are do similar work as RISCV, and RISCV use three APIs for this purpose: kasan_{,early,shallow}_populate_pgd(). But LoongArch use the same API for the 1st and 2nd purpose, can we add a "shallow" parameter to
kasan_pgd_populate()so we can also handle the 3th purpose?Yes.
kasan_shallow_populate()builds incomplete shadow page tables for vmalloc and modules regions and needs to handle boundaries. As an example:
mem_to_shadow(VMALLOC_END)andmem_to_shadow(vmemmap)share one PTE table. The PTE entry formem_to_shadow(VMALLOC_END)must be none, whereas the PTE entry formem_to_shadow(vmemmap)points toearly_shadow_page. The final page table layout is shown belowpte +------
+------------------+ |early_shadow_page | pmd +------------------+ +----------------+ |early_shadow_page | |early_shadow_pte| +------------------+ +----------------+ | global | | ptep |-----------+------------------+ +----------------+ | ... | pte +----------------+ +--------------+ | ... | | global | pgd +----------------+ +--------------+ +------------------+ | ptep |---------------| global | | pmdp +----------+----------------+ +--------------+ +------------------+ pmd | pmdp |----------+--------------+ +------------------+ |invalid_pte | | | +--------------+ | ... | |invalid_pte | | | +--------------+ +------------------+ | ... | +------------------+ pmd | ... | +------------+ +------------------+ | invalid_pte| | ... | +------------+ +------------------+ | invalid_pte| | pmdp |---------- +------------+ +------------------+ | ... | +------------------+
kasan_pgd_populate()can handle the third case by adding a parameter. I will implement this in the next version.Quoted 13 lines
2,The removal of
kasan_populate_early_shadow(mem_to_shadow((void *)VMALLOC_START), mem_to_shadow((void *)KFENCE_AREA_END))achieves the same effect askasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true); both map this region to a shared zero page. (KASAN_SHADOW_START,KASAN_SHADOW_END) already covers (mem_to_shadow((void *)VMALLOC_START),mem_to_shadow((void *)KFENCE_AREA_END)). Other architectures such as arm64/riscv needkasan_populate_early_shadowbecauseclear_shadow(KASAN_SHADOW_START, KASAN_SHADOW_END)is performed beforekasan_populate_early_shadow, so the reserved region must be mapped to the zero page again. On LoongArch, however, the entire shadow space is first mapped to page 0 viakasan_pgd_populate, and then page tables are established for the required regions, which is simpler and clearer.Quoted 8 lines
3,pgd_none/p4d_none/pmd_none/pte_none were added to the definitions of __pgd_none/__p4d_none/__pud_none/__pmd_none/__pte_none respectively because
kasan_populate_early_vm_area_shadowrequires them.kasan_populate_early_vm_area_shadowestablishes complete KASAN shadow page table mappings for the early vmalloc regions beforevmalloc_init. At this point, complete page tables need to be established, butkasan_populate_early_vm_area_shadowis called after step 1, at which timeI'm a little confusing here. What is "step 1" here?
Quoted 9 lines
Step 1 refers to
kasan_shallow_populate(). The page tables created afterkasan_shallow_populate()are shown in the figure above. We need to check for empty entries using /pgd_none/p4d_none/pmd_none/pone_pte(). Only then cankasan_map_populate()build the full page tables.kasan_populate_early_vm_area_shadow()runs afterkasan_shallow_populate()but beforevmalloc_init().
At this stage, page tables for vmalloc regions cannot be automatically populated on demand, so we need to build full page tables here.Then it is better to use
kasan_shallow_populate()rather thanstep 1.Huacai
Quoted 304 lines
Quoted 55 lines
only incomplete page tables have been established for the vmalloc region. The condition for a page table entry to be none requires the pgd_none/p4d_none/pmd_none/pte_none checks. Only by correctly determining whether an entry is none can the complete page tables be established properly.
Test:
https://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson.git
CONFIG_KUNIT=y
CONFIG_KASAN_KUNIT_TEST=mAdd
kunit.enable=1 kasan_multi_shotto the cmdline.Test with 3-level 16K pages and 4-level 4K pages respectively.
before this patch:
...
[ 502.760009] ok 68 vmalloc_oob # SKIP Test requires CONFIG_KASAN_VMALLOC=y...
[ 504.016000] # kasan: pass:49 fail:0 skip:26 total:75 [ 504.016092] # Totals: pass:49 fail:0 skip:26 total:75 [ 504.016171] ok 1 kasanafter this patch:
...
[ 531.630760] ok 68 vmalloc_oob...
[ 532.891057] # kasan: pass:50 fail:0 skip:25 total:75 [ 532.891142] # Totals: pass:50 fail:0 skip:25 total:75 [ 532.891219] ok 1 kasanSigned-off-by Haoran Jiang <[email protected]>---
arch/loongarch/Kconfig| 2 +arch/loongarch/mm/kasan_init.c| 173 ++++++++++++++++++++++++++++++---2 files changed, 164 insertions(+), 11 deletions(-)arch/loongarch/Kconfig +0
-0@@ -119,6 +119,7 @@ config LOONGARCHQuoted 5 lines
select
HAVE_ARCH_JUMP_LABEL
selectHAVE_ARCH_JUMP_LABEL_RELATIVE
selectHAVE_ARCH_KASANif 64BIT
+ selectHAVE_ARCH_KASAN_VMALLOCI think this should be limited by 64BIT.
Huacai
I will fix this in the next revision.
Quoted 237 lines
select
HAVE_ARCH_KFENCEif 64BIT
selectHAVE_ARCH_KGDBifPERF_EVENTS
selectHAVE_ARCH_KSTACK_ERASEarch/loongarch/Kconfig +0
-0@@ -187,6 +188,7 @@ config LOONGARCHselect
HOTPLUG_SMTifHOTPLUG_CPU
selectIRQ_FORCED_THREADING
selectIRQ_LOONGARCH_CPU
+ selectKASAN_VMALLOCif KASAN
selectLOCK_MM_AND_FIND_VMA
selectMMU_GATHER_MERGE_VMASif MMU
selectMMU_GATHER_RCU_TABLE_FREEarch/loongarch/mm/kasan_init.c +162
-12@@ -17,28 +17,28 @@ static pgd_t kasan_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);#define __pgd_none (early, pgd) (0)#else #define __pgd_none (early, pgd) (early ? (pgd_val(pgd) == 0) : \ -(__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d))) +((__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d)) || pgd_none(pgd)))#endif #ifdef __PAGETABLE_PUD_FOLDED#define __p4d_none (early, p4d) (0)#else #define __p4d_none (early, p4d) (early ? (p4d_val(p4d) == 0) : \ -(__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud))) +((__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud)) || p4d_none(p4d)))#endif #ifdef __PAGETABLE_PMD_FOLDED#define __pud_none (early, pud) (0)#else #define __pud_none (early, pud) (early ? (pud_val(pud) == 0) : \ -(__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd))) +((__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd)) || pud_none(pud)))#endif #define __pmd_none (early, pmd) (early ? (pmd_val(pmd) == 0) : \ -(__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte))) +((__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte)) || pmd_none(pmd)))#define __pte_none (early, pte) (early ? pte_none(pte) : \ -((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page))) +(((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page)) || pte_none(pte)))static void *mem_to_shadow (const void *addr) {@@ -242,6 +242,160 @@ static void __init kasan_map_populate(unsigned long start, unsigned long end,kasan_pgd_populate (start &PAGE_MASK ,PAGE_ALIGN (end), node, false); } +static void __init kasan_shallow_populate_pte (pmd_t *pmdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pte_t *ptep =pte_offset_kernel (pmdp, addr); + +do { + next = addr +PAGE_SIZE ; +if (__pte_none (false,ptep_get (ptep))) +pte_clear (&init_mm, addr, ptep); + }while (ptep++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pmd (pud_t *pudp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pmd_t *pmdp =pmd_offset (pudp, addr); + +do { + next =pmd_addr_end (addr, end); + +if (__pmd_none (false,pmdp_get (pmdp))) { +phys_addr_t pte_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PMD_SIZE ) && end - addr >=PMD_SIZE ) { +kernel_pte_init (__va (pte_phys)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); +continue ; + }else { +memcpy (__va (pte_phys), kasan_early_shadow_pte, +sizeof (kasan_early_shadow_pte)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); + } + } +kasan_shallow_populate_pte (pmdp, addr, next, node); + }while (pmdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pud (p4d_t *p4dp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pud_t *pudp =pud_offset (p4dp, addr); + +do { + next =pud_addr_end (addr, end); + +if (__pud_none (false,pudp_get (pudp))) { + +phys_addr_t pmd_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PUD_SIZE ) && end - addr >=PUD_SIZE ) { +#ifndef __PAGETABLE_PMD_FOLDED +pmd_init (__va (pmd_phys)); +#endif +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); +continue ; + }else { +memcpy (__va (pmd_phys), kasan_early_shadow_pmd, +sizeof (kasan_early_shadow_pmd)); +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); + + } + } + +kasan_shallow_populate_pmd (pudp, addr, next, node); + }while (pudp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_p4d (pgd_t *pgdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +p4d_t *p4dp =p4d_offset (pgdp, addr); + +do { + next =p4d_addr_end (addr, end); + +if (__p4d_none (false,p4dp_get (p4dp))) { + +phys_addr_t pud_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,P4D_SIZE ) && end - addr >=P4D_SIZE ) { +#ifndef __PAGETABLE_PUD_FOLDED +pud_init (__va (pud_phys)); +#endif +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); +continue ; + }else { +memcpy (__va (pud_phys), kasan_early_shadow_pud, +sizeof (kasan_early_shadow_pud)); +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); + } + } +kasan_shallow_populate_pud (p4dp, addr, next, node); + }while (p4dp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pgd (unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pgd_t *pgdp; + + pgdp =pgd_offset_k (addr); + +do { + next =pgd_addr_end (addr, end); + +if (__pgd_none (false,pgdp_get (pgdp))) { + +phys_addr_t p4d_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PGDIR_SIZE ) && end - addr >=PGDIR_SIZE ) { +pgd_init (__va (p4d_phys)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); +continue ; + }else { +memcpy (__va (p4d_phys), kasan_early_shadow_p4d, +sizeof (kasan_early_shadow_p4d)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); + } + } + +kasan_shallow_populate_p4d (pgdp, addr, next, node); + }while (pgdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate (unsigned long start,unsigned long end,int node) +{ +unsigned long vaddr = (unsigned long )start &PAGE_MASK ; +unsigned long vend =PAGE_ALIGN ((unsigned long )end); + +kasan_shallow_populate_pgd (vaddr, vend, node); +} + +#ifdef CONFIG_KASAN_VMALLOC +void __init kasan_populate_early_vm_area_shadow (void *start,unsigned long size) +{ +unsigned long shadow_start, shadow_end; + +if (!is_vmalloc_or_module_addr (start)) +return ; + + shadow_start = (unsigned long )mem_to_shadow (start); + shadow_start =ALIGN_DOWN (shadow_start,PAGE_SIZE ); + shadow_end = (unsigned long )mem_to_shadow (start + size); + shadow_end =ALIGN (shadow_end,PAGE_SIZE ); + +kasan_map_populate (shadow_start, shadow_end,NUMA_NO_NODE ); +} +#endif + asmlinkage void __init kasan_early_init(void) {BUILD_BUG_ON (!IS_ALIGNED (KASAN_SHADOW_START ,PGDIR_SIZE ));@@ -297,9 +451,6 @@ void __init kasan_init(void)/* Maps everything to a single page of zeroes */ kasan_pgd_populate (KASAN_SHADOW_START ,KASAN_SHADOW_END ,NUMA_NO_NODE , true); -kasan_populate_early_shadow (mem_to_shadow ((void *)VMALLOC_START ), -mem_to_shadow ((void *)KFENCE_AREA_END )); -/* Populate the linear mapping */ for_each_mem_range(i, &pa_start, &pa_end) {void *start = (void *)phys_to_virt (pa_start);@@ -309,9 +460,9 @@ void __init kasan_init(void) (unsigned long)mem_to_shadow(end), NUMA_NO_NODE); } -/* Populate modules mapping */ -kasan_map_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), - (unsigned long )mem_to_shadow ((void *)MODULES_END ),NUMA_NO_NODE ); +kasan_shallow_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), + (unsigned long )mem_to_shadow ((void *)VMALLOC_END ),NUMA_NO_NODE ); +/* * KAsan may reuse the contents of kasan_early_shadow_pte directly, so we * should make sure that it maps the zero page read-only. -- 2.43.0
2026年10月20日 15:47, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 51 lines · On Tue, Oct 20, 2026 at 10:37 AM <[email protected]> wrote:
Quoted 49 lines
2026年10月17日 16:24, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 25 lines · On Fri, Oct 16, 2026 at 11:05 PM <[email protected]> wrote:
2026年10月14日 12:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Hi, Haoran,
Quoted 8 lines · On Mon, Oct 12, 2026 at 4:05 PM <[email protected]> wrote:
From: Haoran Jiang <[email protected]>
For the page table of the VMALLOC shadow region, only a partial page table needs to be established, allowing the complete page table entries to be populated on demand during use.
Quoted 6 lines
---
1,Through the kasan_shallow_populate_pgd/p4d/pud/pmd/pte functions, page tables are established for the modules and vmalloc regions, but the page tables are not fully populated; the complete page tables are established automatically when vmalloc is used.Why also change modules together with vmalloc regions? Because they can both alloc/free dynamically, and the current approch can only handle "one time" for modules?
Quoted 6 lines
The shadow memory for modules regions is the same as that of vmalloc regions, full page tables can be built on demand during usage. Memory is allocated when loading modules, and the shadow memory of the allocated memory establishes complete page tables via
kasan_populate_vmalloc_do(load_module->execmem_alloc->kasan_populate_vmalloc_do), similar to riscv and arm64.The current code can also handle modules, so I want to know the difference. I guess the current code can only handle a module loading once, but this patch makes it possible to load/unload modules repeatedly. But I'm not sure. Anyway, I don't want something to become worse.
1,With the current approach, the shadow page tables for the modules region are established via
kasan_map_populateandKASAN_VMALLOC=n. A test module performs an out-of-bounds write to a global array. After repeatedinsmod/rmmodcycles, the test module reports false positives. This is because the current flow does not properly clean up the shadow data.
Quoted 8 lines
Quoted 3 lines
Taking x86 as an example, since x86 can be configured with KASAN_VMALLOC=n, when KASAN_VMALLOC=n, the correct flow is as follows:
x86 has some trouble implementing
KASAN_VMALLOC? Or does x86's solution have its advantage?Huacai
On x86, KASAN_VMALLOC is a configurable option; both implementations are supported.
Quoted 442 lines
Quoted 441 lines
execmem_arch_setup-> flags =EXECMEM_KASAN_SHADOW, the flag is set toEXECMEM_KASAN_SHADOW,
and the subsequent flow should select the correct path based on theEXECMEM_KASAN_SHADOWflag.insmod ->
execmem_vmalloc-> (kasan && (kasan_alloc_module_shadow(p, size, GFP_KERNEL)->register_global-> kasan_poison-> rmmod ->kasan_free_module_shadow-> vfree(kasan_mem_to_shadow(vm->addr));This flow it establishes the shadow page tables, then poisons them, and cleans up the shadow data when the module is unloaded.
However, the current situation on LoongArch is as follows:
execmem_arch_setup-> does not setEXECMEM_KASAN_SHADOWinsmod ->
execmem_vmalloc-> does not callkasan_alloc_module_shadow->register_global-> kasan_poison-> rmmod ->kasan_free_module_shadow-> does not call vfree(kasan_mem_to_shadow(vm->addr));LoongArch does not set
EXECMEM_KASAN_SHADOWduring initialization, which causes the shadow data not to be properly cleaned up at unload time, potentially leading to false positives the next time a module is loaded.On RISC-V/arm64,
KASAN_VMALLOCis enabled by default, and the shadow page tables for the module region are established and torn down via kasan_populate_vmalloc_do/kasan_release_vmalloc. This is exactly the approach thatkasan_shallow_populateaims to implement.2,kasan_shallow_populate requires fewer page tables than
kasan_map_populate, and it can also save memory during initialization.Quoted 92 lines
Quoted 16 lines
If the address is pgd-aligned, the page table is established
down to the pgd level; if it is pud-aligned, it is established
down to the pud level, and so on. While establishing the
/pgd/pud/pmd levels, the page table entries must be initialized
to none. For example, a pmd entry of none is aninvalid_pte_table.
Only in this way can the correct page tables be established at
use time. At the same time, boundary issues must be considered
when the address is not aligned. For example,mem_to_shadow(MALLOC_END)
andmem_to_shadow(vmemmap)are in the same pmd, and the region [mem_to_shadow(vmemmap),mem_to_shadow(VMEMMAP_END)] needs to be
mapped to page 0. To avoid affecting themem_to_shadow(vmemmap)region,
only the pmd entries for [mem_to_shadow(MALLOC_END)&PMD_MASK,mem_to_shadow(MALLOC_END)] need to be initialized toinvalid_pte_table. The remaining pmd entries are left as they are.
kasan_pgd_populate(early)populate the page table to early shadow page,kasan_pgd_populate(!early)populate the page table to normal shadow page, andkasan_shallow_populate()populate the page table to invalid_{pud,pmd,pte}_table?It seems you are do similar work as RISCV, and RISCV use three APIs for this purpose: kasan_{,early,shallow}_populate_pgd(). But LoongArch use the same API for the 1st and 2nd purpose, can we add a "shallow" parameter to
kasan_pgd_populate()so we can also handle the 3th purpose?Yes.
kasan_shallow_populate()builds incomplete shadow page tables for vmalloc and modules regions and needs to handle boundaries. As an example:mem_to_shadow(VMALLOC_END)andmem_to_shadow(vmemmap)share one PTE table. The PTE entry formem_to_shadow(VMALLOC_END)must be none, whereas the PTE entry formem_to_shadow(vmemmap)points toearly_shadow_page. The final page table layout is shown belowpte +------
+------------------+ |early_shadow_page | pmd +------------------+ +----------------+ |early_shadow_page | |early_shadow_pte| +------------------+ +----------------+ | global | | ptep |-----------+------------------+ +----------------+ | ... | pte +----------------+ +--------------+ | ... | | global | pgd +----------------+ +--------------+ +------------------+ | ptep |---------------| global | | pmdp +----------+----------------+ +--------------+ +------------------+ pmd | pmdp |----------+--------------+ +------------------+ |invalid_pte | | | +--------------+ | ... | |invalid_pte | | | +--------------+ +------------------+ | ... | +------------------+ pmd | ... | +------------+ +------------------+ | invalid_pte| | ... | +------------+ +------------------+ | invalid_pte| | pmdp |---------- +------------+ +------------------+ | ... | +------------------+
kasan_pgd_populate()can handle the third case by adding a parameter. I will implement this in the next version.Quoted 13 lines
2,The removal of
kasan_populate_early_shadow(mem_to_shadow((void *)VMALLOC_START), mem_to_shadow((void *)KFENCE_AREA_END))achieves the same effect askasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true); both map this region to a shared zero page. (KASAN_SHADOW_START,KASAN_SHADOW_END) already covers (mem_to_shadow((void *)VMALLOC_START),mem_to_shadow((void *)KFENCE_AREA_END)). Other architectures such as arm64/riscv needkasan_populate_early_shadowbecauseclear_shadow(KASAN_SHADOW_START, KASAN_SHADOW_END)is performed beforekasan_populate_early_shadow, so the reserved region must be mapped to the zero page again. On LoongArch, however, the entire shadow space is first mapped to page 0 viakasan_pgd_populate, and then page tables are established for the required regions, which is simpler and clearer.Quoted 8 lines
3,pgd_none/p4d_none/pmd_none/pte_none were added to the definitions of __pgd_none/__p4d_none/__pud_none/__pmd_none/__pte_none respectively because
kasan_populate_early_vm_area_shadowrequires them.kasan_populate_early_vm_area_shadowestablishes complete KASAN shadow page table mappings for the early vmalloc regions beforevmalloc_init. At this point, complete page tables need to be established, butkasan_populate_early_vm_area_shadowis called after step 1, at which timeI'm a little confusing here. What is "step 1" here?
Quoted 9 lines
Step 1 refers to
kasan_shallow_populate(). The page tables created afterkasan_shallow_populate()are shown in the figure above. We need to check for empty entries using /pgd_none/p4d_none/pmd_none/pone_pte(). Only then cankasan_map_populate()build the full page tables.kasan_populate_early_vm_area_shadow()runs afterkasan_shallow_populate()but beforevmalloc_init().
At this stage, page tables for vmalloc regions cannot be automatically populated on demand, so we need to build full page tables here.Then it is better to use
kasan_shallow_populate()rather thanstep 1.Huacai
Quoted 304 lines
Quoted 55 lines
only incomplete page tables have been established for the vmalloc region. The condition for a page table entry to be none requires the pgd_none/p4d_none/pmd_none/pte_none checks. Only by correctly determining whether an entry is none can the complete page tables be established properly.
Test:
https://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson.git
CONFIG_KUNIT=y
CONFIG_KASAN_KUNIT_TEST=mAdd
kunit.enable=1 kasan_multi_shotto the cmdline.Test with 3-level 16K pages and 4-level 4K pages respectively.
before this patch:
...
[ 502.760009] ok 68 vmalloc_oob # SKIP Test requires CONFIG_KASAN_VMALLOC=y...
[ 504.016000] # kasan: pass:49 fail:0 skip:26 total:75 [ 504.016092] # Totals: pass:49 fail:0 skip:26 total:75 [ 504.016171] ok 1 kasanafter this patch:
...
[ 531.630760] ok 68 vmalloc_oob...
[ 532.891057] # kasan: pass:50 fail:0 skip:25 total:75 [ 532.891142] # Totals: pass:50 fail:0 skip:25 total:75 [ 532.891219] ok 1 kasanSigned-off-by Haoran Jiang <[email protected]>---
arch/loongarch/Kconfig| 2 +arch/loongarch/mm/kasan_init.c| 173 ++++++++++++++++++++++++++++++---2 files changed, 164 insertions(+), 11 deletions(-)arch/loongarch/Kconfig +0
-0@@ -119,6 +119,7 @@ config LOONGARCHQuoted 5 lines
select
HAVE_ARCH_JUMP_LABEL
selectHAVE_ARCH_JUMP_LABEL_RELATIVE
selectHAVE_ARCH_KASANif 64BIT
+ selectHAVE_ARCH_KASAN_VMALLOCI think this should be limited by 64BIT.
Huacai
I will fix this in the next revision.
Quoted 237 lines
select
HAVE_ARCH_KFENCEif 64BIT
selectHAVE_ARCH_KGDBifPERF_EVENTS
selectHAVE_ARCH_KSTACK_ERASEarch/loongarch/Kconfig +0
-0@@ -187,6 +188,7 @@ config LOONGARCHselect
HOTPLUG_SMTifHOTPLUG_CPU
selectIRQ_FORCED_THREADING
selectIRQ_LOONGARCH_CPU
+ selectKASAN_VMALLOCif KASAN
selectLOCK_MM_AND_FIND_VMA
selectMMU_GATHER_MERGE_VMASif MMU
selectMMU_GATHER_RCU_TABLE_FREEarch/loongarch/mm/kasan_init.c +162
-12@@ -17,28 +17,28 @@ static pgd_t kasan_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);#define __pgd_none (early, pgd) (0)#else #define __pgd_none (early, pgd) (early ? (pgd_val(pgd) == 0) : \ -(__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d))) +((__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d)) || pgd_none(pgd)))#endif #ifdef __PAGETABLE_PUD_FOLDED#define __p4d_none (early, p4d) (0)#else #define __p4d_none (early, p4d) (early ? (p4d_val(p4d) == 0) : \ -(__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud))) +((__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud)) || p4d_none(p4d)))#endif #ifdef __PAGETABLE_PMD_FOLDED#define __pud_none (early, pud) (0)#else #define __pud_none (early, pud) (early ? (pud_val(pud) == 0) : \ -(__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd))) +((__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd)) || pud_none(pud)))#endif #define __pmd_none (early, pmd) (early ? (pmd_val(pmd) == 0) : \ -(__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte))) +((__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte)) || pmd_none(pmd)))#define __pte_none (early, pte) (early ? pte_none(pte) : \ -((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page))) +(((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page)) || pte_none(pte)))static void *mem_to_shadow (const void *addr) {@@ -242,6 +242,160 @@ static void __init kasan_map_populate(unsigned long start, unsigned long end,kasan_pgd_populate (start &PAGE_MASK ,PAGE_ALIGN (end), node, false); } +static void __init kasan_shallow_populate_pte (pmd_t *pmdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pte_t *ptep =pte_offset_kernel (pmdp, addr); + +do { + next = addr +PAGE_SIZE ; +if (__pte_none (false,ptep_get (ptep))) +pte_clear (&init_mm, addr, ptep); + }while (ptep++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pmd (pud_t *pudp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pmd_t *pmdp =pmd_offset (pudp, addr); + +do { + next =pmd_addr_end (addr, end); + +if (__pmd_none (false,pmdp_get (pmdp))) { +phys_addr_t pte_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PMD_SIZE ) && end - addr >=PMD_SIZE ) { +kernel_pte_init (__va (pte_phys)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); +continue ; + }else { +memcpy (__va (pte_phys), kasan_early_shadow_pte, +sizeof (kasan_early_shadow_pte)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); + } + } +kasan_shallow_populate_pte (pmdp, addr, next, node); + }while (pmdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pud (p4d_t *p4dp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pud_t *pudp =pud_offset (p4dp, addr); + +do { + next =pud_addr_end (addr, end); + +if (__pud_none (false,pudp_get (pudp))) { + +phys_addr_t pmd_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PUD_SIZE ) && end - addr >=PUD_SIZE ) { +#ifndef __PAGETABLE_PMD_FOLDED +pmd_init (__va (pmd_phys)); +#endif +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); +continue ; + }else { +memcpy (__va (pmd_phys), kasan_early_shadow_pmd, +sizeof (kasan_early_shadow_pmd)); +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); + + } + } + +kasan_shallow_populate_pmd (pudp, addr, next, node); + }while (pudp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_p4d (pgd_t *pgdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +p4d_t *p4dp =p4d_offset (pgdp, addr); + +do { + next =p4d_addr_end (addr, end); + +if (__p4d_none (false,p4dp_get (p4dp))) { + +phys_addr_t pud_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,P4D_SIZE ) && end - addr >=P4D_SIZE ) { +#ifndef __PAGETABLE_PUD_FOLDED +pud_init (__va (pud_phys)); +#endif +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); +continue ; + }else { +memcpy (__va (pud_phys), kasan_early_shadow_pud, +sizeof (kasan_early_shadow_pud)); +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); + } + } +kasan_shallow_populate_pud (p4dp, addr, next, node); + }while (p4dp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pgd (unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pgd_t *pgdp; + + pgdp =pgd_offset_k (addr); + +do { + next =pgd_addr_end (addr, end); + +if (__pgd_none (false,pgdp_get (pgdp))) { + +phys_addr_t p4d_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PGDIR_SIZE ) && end - addr >=PGDIR_SIZE ) { +pgd_init (__va (p4d_phys)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); +continue ; + }else { +memcpy (__va (p4d_phys), kasan_early_shadow_p4d, +sizeof (kasan_early_shadow_p4d)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); + } + } + +kasan_shallow_populate_p4d (pgdp, addr, next, node); + }while (pgdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate (unsigned long start,unsigned long end,int node) +{ +unsigned long vaddr = (unsigned long )start &PAGE_MASK ; +unsigned long vend =PAGE_ALIGN ((unsigned long )end); + +kasan_shallow_populate_pgd (vaddr, vend, node); +} + +#ifdef CONFIG_KASAN_VMALLOC +void __init kasan_populate_early_vm_area_shadow (void *start,unsigned long size) +{ +unsigned long shadow_start, shadow_end; + +if (!is_vmalloc_or_module_addr (start)) +return ; + + shadow_start = (unsigned long )mem_to_shadow (start); + shadow_start =ALIGN_DOWN (shadow_start,PAGE_SIZE ); + shadow_end = (unsigned long )mem_to_shadow (start + size); + shadow_end =ALIGN (shadow_end,PAGE_SIZE ); + +kasan_map_populate (shadow_start, shadow_end,NUMA_NO_NODE ); +} +#endif + asmlinkage void __init kasan_early_init(void) {BUILD_BUG_ON (!IS_ALIGNED (KASAN_SHADOW_START ,PGDIR_SIZE ));@@ -297,9 +451,6 @@ void __init kasan_init(void)/* Maps everything to a single page of zeroes */ kasan_pgd_populate (KASAN_SHADOW_START ,KASAN_SHADOW_END ,NUMA_NO_NODE , true); -kasan_populate_early_shadow (mem_to_shadow ((void *)VMALLOC_START ), -mem_to_shadow ((void *)KFENCE_AREA_END )); -/* Populate the linear mapping */ for_each_mem_range(i, &pa_start, &pa_end) {void *start = (void *)phys_to_virt (pa_start);@@ -309,9 +460,9 @@ void __init kasan_init(void) (unsigned long)mem_to_shadow(end), NUMA_NO_NODE); } -/* Populate modules mapping */ -kasan_map_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), - (unsigned long )mem_to_shadow ((void *)MODULES_END ),NUMA_NO_NODE ); +kasan_shallow_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), + (unsigned long )mem_to_shadow ((void *)VMALLOC_END ),NUMA_NO_NODE ); +/* * KAsan may reuse the contents of kasan_early_shadow_pte directly, so we * should make sure that it maps the zero page read-only. -- 2.43.0
Huacai Chen
maintainerQuoted 58 lines · On Tue, Oct 20, 2026 at 5:40 PM <[email protected]> wrote:
2026年10月20日 15:47, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 54 lines · On Tue, Oct 20, 2026 at 10:37 AM <[email protected]> wrote:
Quoted 52 lines
2026年10月17日 16:24, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 25 lines · On Fri, Oct 16, 2026 at 11:05 PM <[email protected]> wrote:
2026年10月14日 12:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Hi, Haoran,
Quoted 8 lines · On Mon, Oct 12, 2026 at 4:05 PM <[email protected]> wrote:
From: Haoran Jiang <[email protected]>
For the page table of the VMALLOC shadow region, only a partial page table needs to be established, allowing the complete page table entries to be populated on demand during use.
Quoted 6 lines
---
1,Through the kasan_shallow_populate_pgd/p4d/pud/pmd/pte functions, page tables are established for the modules and vmalloc regions, but the page tables are not fully populated; the complete page tables are established automatically when vmalloc is used.Why also change modules together with vmalloc regions? Because they can both alloc/free dynamically, and the current approch can only handle "one time" for modules?
Quoted 6 lines
The shadow memory for modules regions is the same as that of vmalloc regions, full page tables can be built on demand during usage. Memory is allocated when loading modules, and the shadow memory of the allocated memory establishes complete page tables via
kasan_populate_vmalloc_do(load_module->execmem_alloc->kasan_populate_vmalloc_do), similar to riscv and arm64.The current code can also handle modules, so I want to know the difference. I guess the current code can only handle a module loading once, but this patch makes it possible to load/unload modules repeatedly. But I'm not sure. Anyway, I don't want something to become worse.
1,With the current approach, the shadow page tables for the modules region are established via
kasan_map_populateandKASAN_VMALLOC=n. A test module performs an out-of-bounds write to a global array. After repeatedinsmod/rmmodcycles, the test module reports false positives. This is because the current flow does not properly clean up the shadow data.Taking x86 as an example, since x86 can be configured with KASAN_VMALLOC=n, when KASAN_VMALLOC=n, the correct flow is as follows:
Quoted 7 lines
Quoted 5 lines
x86 has some trouble implementing
KASAN_VMALLOC? Or does x86's solution have its advantage?Huacai
On x86,
KASAN_VMALLOCis a configurable option; both implementations are supported.
OK, then is the kasan_populate_vmalloc/kasan_release_vmalloc solution better than kasan_alloc_module_shadow/kasan_free_module_shadow solution for modules?
Huacai
Quoted 443 lines
Quoted 443 lines
Quoted 442 lines
execmem_arch_setup-> flags =EXECMEM_KASAN_SHADOW, the flag is set toEXECMEM_KASAN_SHADOW,
and the subsequent flow should select the correct path based on theEXECMEM_KASAN_SHADOWflag.insmod ->
execmem_vmalloc-> (kasan && (kasan_alloc_module_shadow(p, size, GFP_KERNEL)->register_global-> kasan_poison-> rmmod ->kasan_free_module_shadow-> vfree(kasan_mem_to_shadow(vm->addr));This flow it establishes the shadow page tables, then poisons them, and cleans up the shadow data when the module is unloaded.
However, the current situation on LoongArch is as follows:
execmem_arch_setup-> does not setEXECMEM_KASAN_SHADOWinsmod ->
execmem_vmalloc-> does not callkasan_alloc_module_shadow->register_global-> kasan_poison-> rmmod ->kasan_free_module_shadow-> does not call vfree(kasan_mem_to_shadow(vm->addr));LoongArch does not set
EXECMEM_KASAN_SHADOWduring initialization, which causes the shadow data not to be properly cleaned up at unload time, potentially leading to false positives the next time a module is loaded.On RISC-V/arm64,
KASAN_VMALLOCis enabled by default, and the shadow page tables for the module region are established and torn down via kasan_populate_vmalloc_do/kasan_release_vmalloc. This is exactly the approach thatkasan_shallow_populateaims to implement.2,kasan_shallow_populate requires fewer page tables than
kasan_map_populate, and it can also save memory during initialization.Quoted 92 lines
Quoted 16 lines
If the address is pgd-aligned, the page table is established
down to the pgd level; if it is pud-aligned, it is established
down to the pud level, and so on. While establishing the
/pgd/pud/pmd levels, the page table entries must be initialized
to none. For example, a pmd entry of none is aninvalid_pte_table.
Only in this way can the correct page tables be established at
use time. At the same time, boundary issues must be considered
when the address is not aligned. For example,mem_to_shadow(MALLOC_END)
andmem_to_shadow(vmemmap)are in the same pmd, and the region [mem_to_shadow(vmemmap),mem_to_shadow(VMEMMAP_END)] needs to be
mapped to page 0. To avoid affecting themem_to_shadow(vmemmap)region,
only the pmd entries for [mem_to_shadow(MALLOC_END)&PMD_MASK,mem_to_shadow(MALLOC_END)] need to be initialized toinvalid_pte_table. The remaining pmd entries are left as they are.
kasan_pgd_populate(early)populate the page table to early shadow page,kasan_pgd_populate(!early)populate the page table to normal shadow page, andkasan_shallow_populate()populate the page table to invalid_{pud,pmd,pte}_table?It seems you are do similar work as RISCV, and RISCV use three APIs for this purpose: kasan_{,early,shallow}_populate_pgd(). But LoongArch use the same API for the 1st and 2nd purpose, can we add a "shallow" parameter to
kasan_pgd_populate()so we can also handle the 3th purpose?Yes.
kasan_shallow_populate()builds incomplete shadow page tables for vmalloc and modules regions and needs to handle boundaries. As an example:mem_to_shadow(VMALLOC_END)andmem_to_shadow(vmemmap)share one PTE table. The PTE entry formem_to_shadow(VMALLOC_END)must be none, whereas the PTE entry formem_to_shadow(vmemmap)points toearly_shadow_page. The final page table layout is shown belowpte +------
+------------------+ |early_shadow_page | pmd +------------------+ +----------------+ |early_shadow_page | |early_shadow_pte| +------------------+ +----------------+ | global | | ptep |-----------+------------------+ +----------------+ | ... | pte +----------------+ +--------------+ | ... | | global | pgd +----------------+ +--------------+ +------------------+ | ptep |---------------| global | | pmdp +----------+----------------+ +--------------+ +------------------+ pmd | pmdp |----------+--------------+ +------------------+ |invalid_pte | | | +--------------+ | ... | |invalid_pte | | | +--------------+ +------------------+ | ... | +------------------+ pmd | ... | +------------+ +------------------+ | invalid_pte| | ... | +------------+ +------------------+ | invalid_pte| | pmdp |---------- +------------+ +------------------+ | ... | +------------------+
kasan_pgd_populate()can handle the third case by adding a parameter. I will implement this in the next version.Quoted 13 lines
2,The removal of
kasan_populate_early_shadow(mem_to_shadow((void *)VMALLOC_START), mem_to_shadow((void *)KFENCE_AREA_END))achieves the same effect askasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true); both map this region to a shared zero page. (KASAN_SHADOW_START,KASAN_SHADOW_END) already covers (mem_to_shadow((void *)VMALLOC_START),mem_to_shadow((void *)KFENCE_AREA_END)). Other architectures such as arm64/riscv needkasan_populate_early_shadowbecauseclear_shadow(KASAN_SHADOW_START, KASAN_SHADOW_END)is performed beforekasan_populate_early_shadow, so the reserved region must be mapped to the zero page again. On LoongArch, however, the entire shadow space is first mapped to page 0 viakasan_pgd_populate, and then page tables are established for the required regions, which is simpler and clearer.Quoted 8 lines
3,pgd_none/p4d_none/pmd_none/pte_none were added to the definitions of __pgd_none/__p4d_none/__pud_none/__pmd_none/__pte_none respectively because
kasan_populate_early_vm_area_shadowrequires them.kasan_populate_early_vm_area_shadowestablishes complete KASAN shadow page table mappings for the early vmalloc regions beforevmalloc_init. At this point, complete page tables need to be established, butkasan_populate_early_vm_area_shadowis called after step 1, at which timeI'm a little confusing here. What is "step 1" here?
Quoted 9 lines
Step 1 refers to
kasan_shallow_populate(). The page tables created afterkasan_shallow_populate()are shown in the figure above. We need to check for empty entries using /pgd_none/p4d_none/pmd_none/pone_pte(). Only then cankasan_map_populate()build the full page tables.kasan_populate_early_vm_area_shadow()runs afterkasan_shallow_populate()but beforevmalloc_init().
At this stage, page tables for vmalloc regions cannot be automatically populated on demand, so we need to build full page tables here.Then it is better to use
kasan_shallow_populate()rather thanstep 1.Huacai
Quoted 304 lines
Quoted 55 lines
only incomplete page tables have been established for the vmalloc region. The condition for a page table entry to be none requires the pgd_none/p4d_none/pmd_none/pte_none checks. Only by correctly determining whether an entry is none can the complete page tables be established properly.
Test:
https://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson.git
CONFIG_KUNIT=y
CONFIG_KASAN_KUNIT_TEST=mAdd
kunit.enable=1 kasan_multi_shotto the cmdline.Test with 3-level 16K pages and 4-level 4K pages respectively.
before this patch:
...
[ 502.760009] ok 68 vmalloc_oob # SKIP Test requires CONFIG_KASAN_VMALLOC=y...
[ 504.016000] # kasan: pass:49 fail:0 skip:26 total:75 [ 504.016092] # Totals: pass:49 fail:0 skip:26 total:75 [ 504.016171] ok 1 kasanafter this patch:
...
[ 531.630760] ok 68 vmalloc_oob...
[ 532.891057] # kasan: pass:50 fail:0 skip:25 total:75 [ 532.891142] # Totals: pass:50 fail:0 skip:25 total:75 [ 532.891219] ok 1 kasanSigned-off-by Haoran Jiang <[email protected]>---
arch/loongarch/Kconfig| 2 +arch/loongarch/mm/kasan_init.c| 173 ++++++++++++++++++++++++++++++---2 files changed, 164 insertions(+), 11 deletions(-)arch/loongarch/Kconfig +0
-0@@ -119,6 +119,7 @@ config LOONGARCHQuoted 5 lines
select
HAVE_ARCH_JUMP_LABEL
selectHAVE_ARCH_JUMP_LABEL_RELATIVE
selectHAVE_ARCH_KASANif 64BIT
+ selectHAVE_ARCH_KASAN_VMALLOCI think this should be limited by 64BIT.
Huacai
I will fix this in the next revision.
Quoted 237 lines
select
HAVE_ARCH_KFENCEif 64BIT
selectHAVE_ARCH_KGDBifPERF_EVENTS
selectHAVE_ARCH_KSTACK_ERASEarch/loongarch/Kconfig +0
-0@@ -187,6 +188,7 @@ config LOONGARCHselect
HOTPLUG_SMTifHOTPLUG_CPU
selectIRQ_FORCED_THREADING
selectIRQ_LOONGARCH_CPU
+ selectKASAN_VMALLOCif KASAN
selectLOCK_MM_AND_FIND_VMA
selectMMU_GATHER_MERGE_VMASif MMU
selectMMU_GATHER_RCU_TABLE_FREEarch/loongarch/mm/kasan_init.c +162
-12@@ -17,28 +17,28 @@ static pgd_t kasan_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);#define __pgd_none (early, pgd) (0)#else #define __pgd_none (early, pgd) (early ? (pgd_val(pgd) == 0) : \ -(__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d))) +((__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d)) || pgd_none(pgd)))#endif #ifdef __PAGETABLE_PUD_FOLDED#define __p4d_none (early, p4d) (0)#else #define __p4d_none (early, p4d) (early ? (p4d_val(p4d) == 0) : \ -(__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud))) +((__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud)) || p4d_none(p4d)))#endif #ifdef __PAGETABLE_PMD_FOLDED#define __pud_none (early, pud) (0)#else #define __pud_none (early, pud) (early ? (pud_val(pud) == 0) : \ -(__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd))) +((__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd)) || pud_none(pud)))#endif #define __pmd_none (early, pmd) (early ? (pmd_val(pmd) == 0) : \ -(__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte))) +((__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte)) || pmd_none(pmd)))#define __pte_none (early, pte) (early ? pte_none(pte) : \ -((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page))) +(((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page)) || pte_none(pte)))static void *mem_to_shadow (const void *addr) {@@ -242,6 +242,160 @@ static void __init kasan_map_populate(unsigned long start, unsigned long end,kasan_pgd_populate (start &PAGE_MASK ,PAGE_ALIGN (end), node, false); } +static void __init kasan_shallow_populate_pte (pmd_t *pmdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pte_t *ptep =pte_offset_kernel (pmdp, addr); + +do { + next = addr +PAGE_SIZE ; +if (__pte_none (false,ptep_get (ptep))) +pte_clear (&init_mm, addr, ptep); + }while (ptep++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pmd (pud_t *pudp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pmd_t *pmdp =pmd_offset (pudp, addr); + +do { + next =pmd_addr_end (addr, end); + +if (__pmd_none (false,pmdp_get (pmdp))) { +phys_addr_t pte_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PMD_SIZE ) && end - addr >=PMD_SIZE ) { +kernel_pte_init (__va (pte_phys)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); +continue ; + }else { +memcpy (__va (pte_phys), kasan_early_shadow_pte, +sizeof (kasan_early_shadow_pte)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); + } + } +kasan_shallow_populate_pte (pmdp, addr, next, node); + }while (pmdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pud (p4d_t *p4dp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pud_t *pudp =pud_offset (p4dp, addr); + +do { + next =pud_addr_end (addr, end); + +if (__pud_none (false,pudp_get (pudp))) { + +phys_addr_t pmd_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PUD_SIZE ) && end - addr >=PUD_SIZE ) { +#ifndef __PAGETABLE_PMD_FOLDED +pmd_init (__va (pmd_phys)); +#endif +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); +continue ; + }else { +memcpy (__va (pmd_phys), kasan_early_shadow_pmd, +sizeof (kasan_early_shadow_pmd)); +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); + + } + } + +kasan_shallow_populate_pmd (pudp, addr, next, node); + }while (pudp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_p4d (pgd_t *pgdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +p4d_t *p4dp =p4d_offset (pgdp, addr); + +do { + next =p4d_addr_end (addr, end); + +if (__p4d_none (false,p4dp_get (p4dp))) { + +phys_addr_t pud_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,P4D_SIZE ) && end - addr >=P4D_SIZE ) { +#ifndef __PAGETABLE_PUD_FOLDED +pud_init (__va (pud_phys)); +#endif +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); +continue ; + }else { +memcpy (__va (pud_phys), kasan_early_shadow_pud, +sizeof (kasan_early_shadow_pud)); +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); + } + } +kasan_shallow_populate_pud (p4dp, addr, next, node); + }while (p4dp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pgd (unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pgd_t *pgdp; + + pgdp =pgd_offset_k (addr); + +do { + next =pgd_addr_end (addr, end); + +if (__pgd_none (false,pgdp_get (pgdp))) { + +phys_addr_t p4d_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PGDIR_SIZE ) && end - addr >=PGDIR_SIZE ) { +pgd_init (__va (p4d_phys)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); +continue ; + }else { +memcpy (__va (p4d_phys), kasan_early_shadow_p4d, +sizeof (kasan_early_shadow_p4d)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); + } + } + +kasan_shallow_populate_p4d (pgdp, addr, next, node); + }while (pgdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate (unsigned long start,unsigned long end,int node) +{ +unsigned long vaddr = (unsigned long )start &PAGE_MASK ; +unsigned long vend =PAGE_ALIGN ((unsigned long )end); + +kasan_shallow_populate_pgd (vaddr, vend, node); +} + +#ifdef CONFIG_KASAN_VMALLOC +void __init kasan_populate_early_vm_area_shadow (void *start,unsigned long size) +{ +unsigned long shadow_start, shadow_end; + +if (!is_vmalloc_or_module_addr (start)) +return ; + + shadow_start = (unsigned long )mem_to_shadow (start); + shadow_start =ALIGN_DOWN (shadow_start,PAGE_SIZE ); + shadow_end = (unsigned long )mem_to_shadow (start + size); + shadow_end =ALIGN (shadow_end,PAGE_SIZE ); + +kasan_map_populate (shadow_start, shadow_end,NUMA_NO_NODE ); +} +#endif + asmlinkage void __init kasan_early_init(void) {BUILD_BUG_ON (!IS_ALIGNED (KASAN_SHADOW_START ,PGDIR_SIZE ));@@ -297,9 +451,6 @@ void __init kasan_init(void)/* Maps everything to a single page of zeroes */ kasan_pgd_populate (KASAN_SHADOW_START ,KASAN_SHADOW_END ,NUMA_NO_NODE , true); -kasan_populate_early_shadow (mem_to_shadow ((void *)VMALLOC_START ), -mem_to_shadow ((void *)KFENCE_AREA_END )); -/* Populate the linear mapping */ for_each_mem_range(i, &pa_start, &pa_end) {void *start = (void *)phys_to_virt (pa_start);@@ -309,9 +460,9 @@ void __init kasan_init(void) (unsigned long)mem_to_shadow(end), NUMA_NO_NODE); } -/* Populate modules mapping */ -kasan_map_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), - (unsigned long )mem_to_shadow ((void *)MODULES_END ),NUMA_NO_NODE ); +kasan_shallow_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), + (unsigned long )mem_to_shadow ((void *)VMALLOC_END ),NUMA_NO_NODE ); +/* * KAsan may reuse the contents of kasan_early_shadow_pte directly, so we * should make sure that it maps the zero page read-only. -- 2.43.0
2026年10月20日 18:46, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 64 lines · On Tue, Oct 20, 2026 at 5:40 PM <[email protected]> wrote:
Quoted 62 lines
2026年10月20日 15:47, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 42 lines · On Tue, Oct 20, 2026 at 10:37 AM <[email protected]> wrote:
2026年10月17日 16:24, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 25 lines · On Fri, Oct 16, 2026 at 11:05 PM <[email protected]> wrote:
2026年10月14日 12:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Hi, Haoran,
Quoted 8 lines · On Mon, Oct 12, 2026 at 4:05 PM <[email protected]> wrote:
From: Haoran Jiang <[email protected]>
For the page table of the VMALLOC shadow region, only a partial page table needs to be established, allowing the complete page table entries to be populated on demand during use.
Quoted 6 lines
---
1,Through the kasan_shallow_populate_pgd/p4d/pud/pmd/pte functions, page tables are established for the modules and vmalloc regions, but the page tables are not fully populated; the complete page tables are established automatically when vmalloc is used.Why also change modules together with vmalloc regions? Because they can both alloc/free dynamically, and the current approch can only handle "one time" for modules?
Quoted 6 lines
The shadow memory for modules regions is the same as that of vmalloc regions, full page tables can be built on demand during usage. Memory is allocated when loading modules, and the shadow memory of the allocated memory establishes complete page tables via
kasan_populate_vmalloc_do(load_module->execmem_alloc->kasan_populate_vmalloc_do), similar to riscv and arm64.The current code can also handle modules, so I want to know the difference. I guess the current code can only handle a module loading once, but this patch makes it possible to load/unload modules repeatedly. But I'm not sure. Anyway, I don't want something to become worse.
Quoted 9 lines
1,With the current approach, the shadow page tables for the modules region are established via
kasan_map_populateandKASAN_VMALLOC=n. A test module performs an out-of-bounds write to a global array. After repeatedinsmod/rmmodcycles, the test module reports false positives. This is because the current flow does not properly clean up the shadow data.Taking x86 as an example, since x86 can be configured with KASAN_VMALLOC=n, when KASAN_VMALLOC=n, the correct flow is as follows:
x86 has some trouble implementing
KASAN_VMALLOC? Or does x86's solution have its advantage?Huacai
Quoted 7 lines
Quoted 2 lines
On x86,
KASAN_VMALLOCis a configurable option; both implementations are supported.OK, then is the kasan_populate_vmalloc/kasan_release_vmalloc solution better than kasan_alloc_module_shadow/kasan_free_module_shadow solution for modules?
Huacai
The key issue is whether KASAN_VMALLOC is configurable. If it is configurable, both approaches need to be supported; if it is enabled by default along with KASAN, then only the kasan_populate_vmalloc/kasan_release_vmalloc approach needs to be supported. On arm64, KASAN_VMALLOC is enabled by default when KASAN is enabled. This involves the vmap_stack feature: arm64 tends to keep vmap_stack enabled, and for KASAN to be compatible with vmap_stack, KASAN_VMALLOC must also be enabled. The community patch is as follows.
commit acc3042d62cb92c3776767f09f665511c903ef2d
Author: Lecopzer Chen <[email protected]> Date: Wed Mar 24 12:05:22 2021 +0800
...
Quoted 5 lines
From Will's suggestion [1]:
Quoted 3 lines
I would _really_ like to move to VMAP stack unconditionally, and
that would effectively force
KASAN_VMALLOCto be set if KASAN is in use
...
At the same time, also remove the kasan_alloc_module_shadow approach. The community patch is as follows.
commit 00be875879fa676a18415e32f98194db05ee93dc
Author: Mike Rapoport (IBM) <[email protected]> Date: Sun May 5 19:06:13 2024 +0300
arm64: module: remove unneeded call to kasan_alloc_module_shadow
x86 keeps KASAN_VMALLOC configurable (when vmap_stack is enabled, KASAN_VMALLOC must also be enabled in order to use KASAN), giving users an additional choice.
For LoongArch, vmap_stack has not yet been implemented. However, should we consider staying consistent with arm64 here by enabling KASAN_VMALLOC by default? If vmap_stack is implemented in the future, there will be no need to further consider the issue of enabling KASAN_VMALLOC, and kasan_populate_vmalloc can handle both the modules and vmalloc areas at the same time. This approach is also the simplest to handle.
Quoted 445 lines
Quoted 444 lines
Quoted 443 lines
execmem_arch_setup-> flags =EXECMEM_KASAN_SHADOW, the flag is set toEXECMEM_KASAN_SHADOW, and the subsequent flow should select the correct path based on theEXECMEM_KASAN_SHADOWflag.insmod ->
execmem_vmalloc-> (kasan && (kasan_alloc_module_shadow(p, size, GFP_KERNEL)->register_global-> kasan_poison-> rmmod ->kasan_free_module_shadow-> vfree(kasan_mem_to_shadow(vm->addr));This flow it establishes the shadow page tables, then poisons them, and cleans up the shadow data when the module is unloaded.
However, the current situation on LoongArch is as follows:
execmem_arch_setup-> does not setEXECMEM_KASAN_SHADOWinsmod ->
execmem_vmalloc-> does not callkasan_alloc_module_shadow->register_global-> kasan_poison-> rmmod ->kasan_free_module_shadow-> does not call vfree(kasan_mem_to_shadow(vm->addr));LoongArch does not set
EXECMEM_KASAN_SHADOWduring initialization, which causes the shadow data not to be properly cleaned up at unload time, potentially leading to false positives the next time a module is loaded.On RISC-V/arm64,
KASAN_VMALLOCis enabled by default, and the shadow page tables for the module region are established and torn down via kasan_populate_vmalloc_do/kasan_release_vmalloc. This is exactly the approach thatkasan_shallow_populateaims to implement.2,kasan_shallow_populate requires fewer page tables than
kasan_map_populate, and it can also save memory during initialization.Quoted 92 lines
Quoted 16 lines
If the address is pgd-aligned, the page table is established
down to the pgd level; if it is pud-aligned, it is established
down to the pud level, and so on. While establishing the
/pgd/pud/pmd levels, the page table entries must be initialized
to none. For example, a pmd entry of none is aninvalid_pte_table.
Only in this way can the correct page tables be established at
use time. At the same time, boundary issues must be considered
when the address is not aligned. For example,mem_to_shadow(MALLOC_END)
andmem_to_shadow(vmemmap)are in the same pmd, and the region [mem_to_shadow(vmemmap),mem_to_shadow(VMEMMAP_END)] needs to be
mapped to page 0. To avoid affecting themem_to_shadow(vmemmap)region,
only the pmd entries for [mem_to_shadow(MALLOC_END)&PMD_MASK,mem_to_shadow(MALLOC_END)] need to be initialized toinvalid_pte_table. The remaining pmd entries are left as they are.
kasan_pgd_populate(early)populate the page table to early shadow page,kasan_pgd_populate(!early)populate the page table to normal shadow page, andkasan_shallow_populate()populate the page table to invalid_{pud,pmd,pte}_table?It seems you are do similar work as RISCV, and RISCV use three APIs for this purpose: kasan_{,early,shallow}_populate_pgd(). But LoongArch use the same API for the 1st and 2nd purpose, can we add a "shallow" parameter to
kasan_pgd_populate()so we can also handle the 3th purpose?Yes.
kasan_shallow_populate()builds incomplete shadow page tables for vmalloc and modules regions and needs to handle boundaries. As an example:mem_to_shadow(VMALLOC_END)andmem_to_shadow(vmemmap)share one PTE table. The PTE entry formem_to_shadow(VMALLOC_END)must be none, whereas the PTE entry formem_to_shadow(vmemmap)points toearly_shadow_page. The final page table layout is shown belowpte +------
+------------------+ |early_shadow_page | pmd +------------------+ +----------------+ |early_shadow_page | |early_shadow_pte| +------------------+ +----------------+ | global | | ptep |-----------+------------------+ +----------------+ | ... | pte +----------------+ +--------------+ | ... | | global | pgd +----------------+ +--------------+ +------------------+ | ptep |---------------| global | | pmdp +----------+----------------+ +--------------+ +------------------+ pmd | pmdp |----------+--------------+ +------------------+ |invalid_pte | | | +--------------+ | ... | |invalid_pte | | | +--------------+ +------------------+ | ... | +------------------+ pmd | ... | +------------+ +------------------+ | invalid_pte| | ... | +------------+ +------------------+ | invalid_pte| | pmdp |---------- +------------+ +------------------+ | ... | +------------------+
kasan_pgd_populate()can handle the third case by adding a parameter. I will implement this in the next version.Quoted 13 lines
2,The removal of
kasan_populate_early_shadow(mem_to_shadow((void *)VMALLOC_START), mem_to_shadow((void *)KFENCE_AREA_END))achieves the same effect askasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true); both map this region to a shared zero page. (KASAN_SHADOW_START,KASAN_SHADOW_END) already covers (mem_to_shadow((void *)VMALLOC_START),mem_to_shadow((void *)KFENCE_AREA_END)). Other architectures such as arm64/riscv needkasan_populate_early_shadowbecauseclear_shadow(KASAN_SHADOW_START, KASAN_SHADOW_END)is performed beforekasan_populate_early_shadow, so the reserved region must be mapped to the zero page again. On LoongArch, however, the entire shadow space is first mapped to page 0 viakasan_pgd_populate, and then page tables are established for the required regions, which is simpler and clearer.Quoted 8 lines
3,pgd_none/p4d_none/pmd_none/pte_none were added to the definitions of __pgd_none/__p4d_none/__pud_none/__pmd_none/__pte_none respectively because
kasan_populate_early_vm_area_shadowrequires them.kasan_populate_early_vm_area_shadowestablishes complete KASAN shadow page table mappings for the early vmalloc regions beforevmalloc_init. At this point, complete page tables need to be established, butkasan_populate_early_vm_area_shadowis called after step 1, at which timeI'm a little confusing here. What is "step 1" here?
Quoted 9 lines
Step 1 refers to
kasan_shallow_populate(). The page tables created afterkasan_shallow_populate()are shown in the figure above. We need to check for empty entries using /pgd_none/p4d_none/pmd_none/pone_pte(). Only then cankasan_map_populate()build the full page tables.kasan_populate_early_vm_area_shadow()runs afterkasan_shallow_populate()but beforevmalloc_init().
At this stage, page tables for vmalloc regions cannot be automatically populated on demand, so we need to build full page tables here.Then it is better to use
kasan_shallow_populate()rather thanstep 1.Huacai
Quoted 304 lines
Quoted 55 lines
only incomplete page tables have been established for the vmalloc region. The condition for a page table entry to be none requires the pgd_none/p4d_none/pmd_none/pte_none checks. Only by correctly determining whether an entry is none can the complete page tables be established properly.
Test:
https://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson.git
CONFIG_KUNIT=y
CONFIG_KASAN_KUNIT_TEST=mAdd
kunit.enable=1 kasan_multi_shotto the cmdline.Test with 3-level 16K pages and 4-level 4K pages respectively.
before this patch:
...
[ 502.760009] ok 68 vmalloc_oob # SKIP Test requires CONFIG_KASAN_VMALLOC=y...
[ 504.016000] # kasan: pass:49 fail:0 skip:26 total:75 [ 504.016092] # Totals: pass:49 fail:0 skip:26 total:75 [ 504.016171] ok 1 kasanafter this patch:
...
[ 531.630760] ok 68 vmalloc_oob...
[ 532.891057] # kasan: pass:50 fail:0 skip:25 total:75 [ 532.891142] # Totals: pass:50 fail:0 skip:25 total:75 [ 532.891219] ok 1 kasanSigned-off-by Haoran Jiang <[email protected]>---
arch/loongarch/Kconfig| 2 +arch/loongarch/mm/kasan_init.c| 173 ++++++++++++++++++++++++++++++---2 files changed, 164 insertions(+), 11 deletions(-)arch/loongarch/Kconfig +0
-0@@ -119,6 +119,7 @@ config LOONGARCHQuoted 5 lines
select
HAVE_ARCH_JUMP_LABEL
selectHAVE_ARCH_JUMP_LABEL_RELATIVE
selectHAVE_ARCH_KASANif 64BIT
+ selectHAVE_ARCH_KASAN_VMALLOCI think this should be limited by 64BIT.
Huacai
I will fix this in the next revision.
Quoted 237 lines
select
HAVE_ARCH_KFENCEif 64BIT
selectHAVE_ARCH_KGDBifPERF_EVENTS
selectHAVE_ARCH_KSTACK_ERASEarch/loongarch/Kconfig +0
-0@@ -187,6 +188,7 @@ config LOONGARCHselect
HOTPLUG_SMTifHOTPLUG_CPU
selectIRQ_FORCED_THREADING
selectIRQ_LOONGARCH_CPU
+ selectKASAN_VMALLOCif KASAN
selectLOCK_MM_AND_FIND_VMA
selectMMU_GATHER_MERGE_VMASif MMU
selectMMU_GATHER_RCU_TABLE_FREEarch/loongarch/mm/kasan_init.c +162
-12@@ -17,28 +17,28 @@ static pgd_t kasan_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);#define __pgd_none (early, pgd) (0)#else #define __pgd_none (early, pgd) (early ? (pgd_val(pgd) == 0) : \ -(__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d))) +((__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d)) || pgd_none(pgd)))#endif #ifdef __PAGETABLE_PUD_FOLDED#define __p4d_none (early, p4d) (0)#else #define __p4d_none (early, p4d) (early ? (p4d_val(p4d) == 0) : \ -(__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud))) +((__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud)) || p4d_none(p4d)))#endif #ifdef __PAGETABLE_PMD_FOLDED#define __pud_none (early, pud) (0)#else #define __pud_none (early, pud) (early ? (pud_val(pud) == 0) : \ -(__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd))) +((__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd)) || pud_none(pud)))#endif #define __pmd_none (early, pmd) (early ? (pmd_val(pmd) == 0) : \ -(__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte))) +((__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte)) || pmd_none(pmd)))#define __pte_none (early, pte) (early ? pte_none(pte) : \ -((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page))) +(((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page)) || pte_none(pte)))static void *mem_to_shadow (const void *addr) {@@ -242,6 +242,160 @@ static void __init kasan_map_populate(unsigned long start, unsigned long end,kasan_pgd_populate (start &PAGE_MASK ,PAGE_ALIGN (end), node, false); } +static void __init kasan_shallow_populate_pte (pmd_t *pmdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pte_t *ptep =pte_offset_kernel (pmdp, addr); + +do { + next = addr +PAGE_SIZE ; +if (__pte_none (false,ptep_get (ptep))) +pte_clear (&init_mm, addr, ptep); + }while (ptep++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pmd (pud_t *pudp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pmd_t *pmdp =pmd_offset (pudp, addr); + +do { + next =pmd_addr_end (addr, end); + +if (__pmd_none (false,pmdp_get (pmdp))) { +phys_addr_t pte_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PMD_SIZE ) && end - addr >=PMD_SIZE ) { +kernel_pte_init (__va (pte_phys)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); +continue ; + }else { +memcpy (__va (pte_phys), kasan_early_shadow_pte, +sizeof (kasan_early_shadow_pte)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); + } + } +kasan_shallow_populate_pte (pmdp, addr, next, node); + }while (pmdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pud (p4d_t *p4dp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pud_t *pudp =pud_offset (p4dp, addr); + +do { + next =pud_addr_end (addr, end); + +if (__pud_none (false,pudp_get (pudp))) { + +phys_addr_t pmd_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PUD_SIZE ) && end - addr >=PUD_SIZE ) { +#ifndef __PAGETABLE_PMD_FOLDED +pmd_init (__va (pmd_phys)); +#endif +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); +continue ; + }else { +memcpy (__va (pmd_phys), kasan_early_shadow_pmd, +sizeof (kasan_early_shadow_pmd)); +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); + + } + } + +kasan_shallow_populate_pmd (pudp, addr, next, node); + }while (pudp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_p4d (pgd_t *pgdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +p4d_t *p4dp =p4d_offset (pgdp, addr); + +do { + next =p4d_addr_end (addr, end); + +if (__p4d_none (false,p4dp_get (p4dp))) { + +phys_addr_t pud_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,P4D_SIZE ) && end - addr >=P4D_SIZE ) { +#ifndef __PAGETABLE_PUD_FOLDED +pud_init (__va (pud_phys)); +#endif +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); +continue ; + }else { +memcpy (__va (pud_phys), kasan_early_shadow_pud, +sizeof (kasan_early_shadow_pud)); +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); + } + } +kasan_shallow_populate_pud (p4dp, addr, next, node); + }while (p4dp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pgd (unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pgd_t *pgdp; + + pgdp =pgd_offset_k (addr); + +do { + next =pgd_addr_end (addr, end); + +if (__pgd_none (false,pgdp_get (pgdp))) { + +phys_addr_t p4d_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PGDIR_SIZE ) && end - addr >=PGDIR_SIZE ) { +pgd_init (__va (p4d_phys)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); +continue ; + }else { +memcpy (__va (p4d_phys), kasan_early_shadow_p4d, +sizeof (kasan_early_shadow_p4d)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); + } + } + +kasan_shallow_populate_p4d (pgdp, addr, next, node); + }while (pgdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate (unsigned long start,unsigned long end,int node) +{ +unsigned long vaddr = (unsigned long )start &PAGE_MASK ; +unsigned long vend =PAGE_ALIGN ((unsigned long )end); + +kasan_shallow_populate_pgd (vaddr, vend, node); +} + +#ifdef CONFIG_KASAN_VMALLOC +void __init kasan_populate_early_vm_area_shadow (void *start,unsigned long size) +{ +unsigned long shadow_start, shadow_end; + +if (!is_vmalloc_or_module_addr (start)) +return ; + + shadow_start = (unsigned long )mem_to_shadow (start); + shadow_start =ALIGN_DOWN (shadow_start,PAGE_SIZE ); + shadow_end = (unsigned long )mem_to_shadow (start + size); + shadow_end =ALIGN (shadow_end,PAGE_SIZE ); + +kasan_map_populate (shadow_start, shadow_end,NUMA_NO_NODE ); +} +#endif + asmlinkage void __init kasan_early_init(void) {BUILD_BUG_ON (!IS_ALIGNED (KASAN_SHADOW_START ,PGDIR_SIZE ));@@ -297,9 +451,6 @@ void __init kasan_init(void)/* Maps everything to a single page of zeroes */ kasan_pgd_populate (KASAN_SHADOW_START ,KASAN_SHADOW_END ,NUMA_NO_NODE , true); -kasan_populate_early_shadow (mem_to_shadow ((void *)VMALLOC_START ), -mem_to_shadow ((void *)KFENCE_AREA_END )); -/* Populate the linear mapping */ for_each_mem_range(i, &pa_start, &pa_end) {void *start = (void *)phys_to_virt (pa_start);@@ -309,9 +460,9 @@ void __init kasan_init(void) (unsigned long)mem_to_shadow(end), NUMA_NO_NODE); } -/* Populate modules mapping */ -kasan_map_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), - (unsigned long )mem_to_shadow ((void *)MODULES_END ),NUMA_NO_NODE ); +kasan_shallow_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), + (unsigned long )mem_to_shadow ((void *)VMALLOC_END ),NUMA_NO_NODE ); +/* * KAsan may reuse the contents of kasan_early_shadow_pte directly, so we * should make sure that it maps the zero page read-only. -- 2.43.0
Huacai Chen
maintainerQuoted 120 lines · On Thu, Oct 22, 2026 at 11:12 AM <[email protected]> wrote:
2026年10月20日 18:46, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 64 lines · On Tue, Oct 20, 2026 at 5:40 PM <[email protected]> wrote:
Quoted 62 lines
2026年10月20日 15:47, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 42 lines · On Tue, Oct 20, 2026 at 10:37 AM <[email protected]> wrote:
2026年10月17日 16:24, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 25 lines · On Fri, Oct 16, 2026 at 11:05 PM <[email protected]> wrote:
2026年10月14日 12:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Hi, Haoran,
Quoted 8 lines · On Mon, Oct 12, 2026 at 4:05 PM <[email protected]> wrote:
From: Haoran Jiang <[email protected]>
For the page table of the VMALLOC shadow region, only a partial page table needs to be established, allowing the complete page table entries to be populated on demand during use.
Quoted 6 lines
---
1,Through the kasan_shallow_populate_pgd/p4d/pud/pmd/pte functions, page tables are established for the modules and vmalloc regions, but the page tables are not fully populated; the complete page tables are established automatically when vmalloc is used.Why also change modules together with vmalloc regions? Because they can both alloc/free dynamically, and the current approch can only handle "one time" for modules?
Quoted 6 lines
The shadow memory for modules regions is the same as that of vmalloc regions, full page tables can be built on demand during usage. Memory is allocated when loading modules, and the shadow memory of the allocated memory establishes complete page tables via
kasan_populate_vmalloc_do(load_module->execmem_alloc->kasan_populate_vmalloc_do), similar to riscv and arm64.The current code can also handle modules, so I want to know the difference. I guess the current code can only handle a module loading once, but this patch makes it possible to load/unload modules repeatedly. But I'm not sure. Anyway, I don't want something to become worse.
Quoted 9 lines
1,With the current approach, the shadow page tables for the modules region are established via
kasan_map_populateandKASAN_VMALLOC=n. A test module performs an out-of-bounds write to a global array. After repeatedinsmod/rmmodcycles, the test module reports false positives. This is because the current flow does not properly clean up the shadow data.Taking x86 as an example, since x86 can be configured with KASAN_VMALLOC=n, when KASAN_VMALLOC=n, the correct flow is as follows:
x86 has some trouble implementing
KASAN_VMALLOC? Or does x86's solution have its advantage?Huacai
Quoted 7 lines
Quoted 2 lines
On x86,
KASAN_VMALLOCis a configurable option; both implementations are supported.OK, then is the kasan_populate_vmalloc/kasan_release_vmalloc solution better than kasan_alloc_module_shadow/kasan_free_module_shadow solution for modules?
Huacai
The key issue is whether
KASAN_VMALLOCis configurable. If it is configurable, both approaches need to be supported; if it is enabled by default along with KASAN, then only thekasan_populate_vmalloc/kasan_release_vmallocapproach needs to be supported. On arm64,KASAN_VMALLOCis enabled by default when KASAN is enabled. This involves thevmap_stackfeature: arm64 tends to keepvmap_stackenabled, and for KASAN to be compatible withvmap_stack,KASAN_VMALLOCmust also be enabled. The community patch is as follows.commit
acc3042d62cb92c3776767f09f665511c903ef2dAuthor: Lecopzer Chen <[email protected]> Date: Wed Mar 24 12:05:22 2021 +0800...
Quoted 5 lines
From Will's suggestion [1]:
Quoted 3 lines
I would _really_ like to move to VMAP stack unconditionally, and
that would effectively force
KASAN_VMALLOCto be set if KASAN is in use...
At the same time, also remove the
kasan_alloc_module_shadowapproach. The community patch is as follows.commit
00be875879fa676a18415e32f98194db05ee93dcAuthor: Mike Rapoport (IBM) <[email protected]> Date: Sun May 5 19:06:13 2024 +0300arm64: module: remove unneeded call to
kasan_alloc_module_shadowx86 keeps
KASAN_VMALLOCconfigurable (whenvmap_stackis enabled,KASAN_VMALLOCmust also be enabled in order to use KASAN), giving users an additional choice.
Quoted 7 lines
For LoongArch,
vmap_stackhas not yet been implemented. However, should we consider staying consistent with arm64 here by enablingKASAN_VMALLOCby default? Ifvmap_stackis implemented in the future, there will be no need to further consider the issue of enablingKASAN_VMALLOC, andkasan_populate_vmalloccan handle both the modules and vmalloc areas at the same time. This approach is also the simplest to handle.
Enabling KASAN_VMALLOC is fine for me, I just want to know whether kasan_populate_vmalloc/kasan_release_vmalloc is better than kasan_alloc_module_shadow/kasan_free_module_shadow technically.
Huacai
Quoted 447 lines
Quoted 446 lines
Quoted 445 lines
Quoted 444 lines
execmem_arch_setup-> flags =EXECMEM_KASAN_SHADOW, the flag is set toEXECMEM_KASAN_SHADOW, and the subsequent flow should select the correct path based on theEXECMEM_KASAN_SHADOWflag.insmod ->
execmem_vmalloc-> (kasan && (kasan_alloc_module_shadow(p, size, GFP_KERNEL)->register_global-> kasan_poison-> rmmod ->kasan_free_module_shadow-> vfree(kasan_mem_to_shadow(vm->addr));This flow it establishes the shadow page tables, then poisons them, and cleans up the shadow data when the module is unloaded.
However, the current situation on LoongArch is as follows:
execmem_arch_setup-> does not setEXECMEM_KASAN_SHADOWinsmod ->
execmem_vmalloc-> does not callkasan_alloc_module_shadow->register_global-> kasan_poison-> rmmod ->kasan_free_module_shadow-> does not call vfree(kasan_mem_to_shadow(vm->addr));LoongArch does not set
EXECMEM_KASAN_SHADOWduring initialization, which causes the shadow data not to be properly cleaned up at unload time, potentially leading to false positives the next time a module is loaded.On RISC-V/arm64,
KASAN_VMALLOCis enabled by default, and the shadow page tables for the module region are established and torn down via kasan_populate_vmalloc_do/kasan_release_vmalloc. This is exactly the approach thatkasan_shallow_populateaims to implement.2,kasan_shallow_populate requires fewer page tables than
kasan_map_populate, and it can also save memory during initialization.Quoted 92 lines
Quoted 16 lines
If the address is pgd-aligned, the page table is established
down to the pgd level; if it is pud-aligned, it is established
down to the pud level, and so on. While establishing the
/pgd/pud/pmd levels, the page table entries must be initialized
to none. For example, a pmd entry of none is aninvalid_pte_table.
Only in this way can the correct page tables be established at
use time. At the same time, boundary issues must be considered
when the address is not aligned. For example,mem_to_shadow(MALLOC_END)
andmem_to_shadow(vmemmap)are in the same pmd, and the region [mem_to_shadow(vmemmap),mem_to_shadow(VMEMMAP_END)] needs to be
mapped to page 0. To avoid affecting themem_to_shadow(vmemmap)region,
only the pmd entries for [mem_to_shadow(MALLOC_END)&PMD_MASK,mem_to_shadow(MALLOC_END)] need to be initialized toinvalid_pte_table. The remaining pmd entries are left as they are.
kasan_pgd_populate(early)populate the page table to early shadow page,kasan_pgd_populate(!early)populate the page table to normal shadow page, andkasan_shallow_populate()populate the page table to invalid_{pud,pmd,pte}_table?It seems you are do similar work as RISCV, and RISCV use three APIs for this purpose: kasan_{,early,shallow}_populate_pgd(). But LoongArch use the same API for the 1st and 2nd purpose, can we add a "shallow" parameter to
kasan_pgd_populate()so we can also handle the 3th purpose?Yes.
kasan_shallow_populate()builds incomplete shadow page tables for vmalloc and modules regions and needs to handle boundaries. As an example:mem_to_shadow(VMALLOC_END)andmem_to_shadow(vmemmap)share one PTE table. The PTE entry formem_to_shadow(VMALLOC_END)must be none, whereas the PTE entry formem_to_shadow(vmemmap)points toearly_shadow_page. The final page table layout is shown belowpte +------
+------------------+ |early_shadow_page | pmd +------------------+ +----------------+ |early_shadow_page | |early_shadow_pte| +------------------+ +----------------+ | global | | ptep |-----------+------------------+ +----------------+ | ... | pte +----------------+ +--------------+ | ... | | global | pgd +----------------+ +--------------+ +------------------+ | ptep |---------------| global | | pmdp +----------+----------------+ +--------------+ +------------------+ pmd | pmdp |----------+--------------+ +------------------+ |invalid_pte | | | +--------------+ | ... | |invalid_pte | | | +--------------+ +------------------+ | ... | +------------------+ pmd | ... | +------------+ +------------------+ | invalid_pte| | ... | +------------+ +------------------+ | invalid_pte| | pmdp |---------- +------------+ +------------------+ | ... | +------------------+
kasan_pgd_populate()can handle the third case by adding a parameter. I will implement this in the next version.Quoted 13 lines
2,The removal of
kasan_populate_early_shadow(mem_to_shadow((void *)VMALLOC_START), mem_to_shadow((void *)KFENCE_AREA_END))achieves the same effect askasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true); both map this region to a shared zero page. (KASAN_SHADOW_START,KASAN_SHADOW_END) already covers (mem_to_shadow((void *)VMALLOC_START),mem_to_shadow((void *)KFENCE_AREA_END)). Other architectures such as arm64/riscv needkasan_populate_early_shadowbecauseclear_shadow(KASAN_SHADOW_START, KASAN_SHADOW_END)is performed beforekasan_populate_early_shadow, so the reserved region must be mapped to the zero page again. On LoongArch, however, the entire shadow space is first mapped to page 0 viakasan_pgd_populate, and then page tables are established for the required regions, which is simpler and clearer.Quoted 8 lines
3,pgd_none/p4d_none/pmd_none/pte_none were added to the definitions of __pgd_none/__p4d_none/__pud_none/__pmd_none/__pte_none respectively because
kasan_populate_early_vm_area_shadowrequires them.kasan_populate_early_vm_area_shadowestablishes complete KASAN shadow page table mappings for the early vmalloc regions beforevmalloc_init. At this point, complete page tables need to be established, butkasan_populate_early_vm_area_shadowis called after step 1, at which timeI'm a little confusing here. What is "step 1" here?
Quoted 9 lines
Step 1 refers to
kasan_shallow_populate(). The page tables created afterkasan_shallow_populate()are shown in the figure above. We need to check for empty entries using /pgd_none/p4d_none/pmd_none/pone_pte(). Only then cankasan_map_populate()build the full page tables.kasan_populate_early_vm_area_shadow()runs afterkasan_shallow_populate()but beforevmalloc_init().
At this stage, page tables for vmalloc regions cannot be automatically populated on demand, so we need to build full page tables here.Then it is better to use
kasan_shallow_populate()rather thanstep 1.Huacai
Quoted 304 lines
Quoted 55 lines
only incomplete page tables have been established for the vmalloc region. The condition for a page table entry to be none requires the pgd_none/p4d_none/pmd_none/pte_none checks. Only by correctly determining whether an entry is none can the complete page tables be established properly.
Test:
https://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson.git
CONFIG_KUNIT=y
CONFIG_KASAN_KUNIT_TEST=mAdd
kunit.enable=1 kasan_multi_shotto the cmdline.Test with 3-level 16K pages and 4-level 4K pages respectively.
before this patch:
...
[ 502.760009] ok 68 vmalloc_oob # SKIP Test requires CONFIG_KASAN_VMALLOC=y...
[ 504.016000] # kasan: pass:49 fail:0 skip:26 total:75 [ 504.016092] # Totals: pass:49 fail:0 skip:26 total:75 [ 504.016171] ok 1 kasanafter this patch:
...
[ 531.630760] ok 68 vmalloc_oob...
[ 532.891057] # kasan: pass:50 fail:0 skip:25 total:75 [ 532.891142] # Totals: pass:50 fail:0 skip:25 total:75 [ 532.891219] ok 1 kasanSigned-off-by Haoran Jiang <[email protected]>---
arch/loongarch/Kconfig| 2 +arch/loongarch/mm/kasan_init.c| 173 ++++++++++++++++++++++++++++++---2 files changed, 164 insertions(+), 11 deletions(-)arch/loongarch/Kconfig +0
-0@@ -119,6 +119,7 @@ config LOONGARCHQuoted 5 lines
select
HAVE_ARCH_JUMP_LABEL
selectHAVE_ARCH_JUMP_LABEL_RELATIVE
selectHAVE_ARCH_KASANif 64BIT
+ selectHAVE_ARCH_KASAN_VMALLOCI think this should be limited by 64BIT.
Huacai
I will fix this in the next revision.
Quoted 237 lines
select
HAVE_ARCH_KFENCEif 64BIT
selectHAVE_ARCH_KGDBifPERF_EVENTS
selectHAVE_ARCH_KSTACK_ERASEarch/loongarch/Kconfig +0
-0@@ -187,6 +188,7 @@ config LOONGARCHselect
HOTPLUG_SMTifHOTPLUG_CPU
selectIRQ_FORCED_THREADING
selectIRQ_LOONGARCH_CPU
+ selectKASAN_VMALLOCif KASAN
selectLOCK_MM_AND_FIND_VMA
selectMMU_GATHER_MERGE_VMASif MMU
selectMMU_GATHER_RCU_TABLE_FREEarch/loongarch/mm/kasan_init.c +162
-12@@ -17,28 +17,28 @@ static pgd_t kasan_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);#define __pgd_none (early, pgd) (0)#else #define __pgd_none (early, pgd) (early ? (pgd_val(pgd) == 0) : \ -(__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d))) +((__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d)) || pgd_none(pgd)))#endif #ifdef __PAGETABLE_PUD_FOLDED#define __p4d_none (early, p4d) (0)#else #define __p4d_none (early, p4d) (early ? (p4d_val(p4d) == 0) : \ -(__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud))) +((__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud)) || p4d_none(p4d)))#endif #ifdef __PAGETABLE_PMD_FOLDED#define __pud_none (early, pud) (0)#else #define __pud_none (early, pud) (early ? (pud_val(pud) == 0) : \ -(__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd))) +((__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd)) || pud_none(pud)))#endif #define __pmd_none (early, pmd) (early ? (pmd_val(pmd) == 0) : \ -(__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte))) +((__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte)) || pmd_none(pmd)))#define __pte_none (early, pte) (early ? pte_none(pte) : \ -((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page))) +(((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page)) || pte_none(pte)))static void *mem_to_shadow (const void *addr) {@@ -242,6 +242,160 @@ static void __init kasan_map_populate(unsigned long start, unsigned long end,kasan_pgd_populate (start &PAGE_MASK ,PAGE_ALIGN (end), node, false); } +static void __init kasan_shallow_populate_pte (pmd_t *pmdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pte_t *ptep =pte_offset_kernel (pmdp, addr); + +do { + next = addr +PAGE_SIZE ; +if (__pte_none (false,ptep_get (ptep))) +pte_clear (&init_mm, addr, ptep); + }while (ptep++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pmd (pud_t *pudp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pmd_t *pmdp =pmd_offset (pudp, addr); + +do { + next =pmd_addr_end (addr, end); + +if (__pmd_none (false,pmdp_get (pmdp))) { +phys_addr_t pte_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PMD_SIZE ) && end - addr >=PMD_SIZE ) { +kernel_pte_init (__va (pte_phys)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); +continue ; + }else { +memcpy (__va (pte_phys), kasan_early_shadow_pte, +sizeof (kasan_early_shadow_pte)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); + } + } +kasan_shallow_populate_pte (pmdp, addr, next, node); + }while (pmdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pud (p4d_t *p4dp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pud_t *pudp =pud_offset (p4dp, addr); + +do { + next =pud_addr_end (addr, end); + +if (__pud_none (false,pudp_get (pudp))) { + +phys_addr_t pmd_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PUD_SIZE ) && end - addr >=PUD_SIZE ) { +#ifndef __PAGETABLE_PMD_FOLDED +pmd_init (__va (pmd_phys)); +#endif +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); +continue ; + }else { +memcpy (__va (pmd_phys), kasan_early_shadow_pmd, +sizeof (kasan_early_shadow_pmd)); +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); + + } + } + +kasan_shallow_populate_pmd (pudp, addr, next, node); + }while (pudp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_p4d (pgd_t *pgdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +p4d_t *p4dp =p4d_offset (pgdp, addr); + +do { + next =p4d_addr_end (addr, end); + +if (__p4d_none (false,p4dp_get (p4dp))) { + +phys_addr_t pud_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,P4D_SIZE ) && end - addr >=P4D_SIZE ) { +#ifndef __PAGETABLE_PUD_FOLDED +pud_init (__va (pud_phys)); +#endif +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); +continue ; + }else { +memcpy (__va (pud_phys), kasan_early_shadow_pud, +sizeof (kasan_early_shadow_pud)); +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); + } + } +kasan_shallow_populate_pud (p4dp, addr, next, node); + }while (p4dp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pgd (unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pgd_t *pgdp; + + pgdp =pgd_offset_k (addr); + +do { + next =pgd_addr_end (addr, end); + +if (__pgd_none (false,pgdp_get (pgdp))) { + +phys_addr_t p4d_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PGDIR_SIZE ) && end - addr >=PGDIR_SIZE ) { +pgd_init (__va (p4d_phys)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); +continue ; + }else { +memcpy (__va (p4d_phys), kasan_early_shadow_p4d, +sizeof (kasan_early_shadow_p4d)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); + } + } + +kasan_shallow_populate_p4d (pgdp, addr, next, node); + }while (pgdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate (unsigned long start,unsigned long end,int node) +{ +unsigned long vaddr = (unsigned long )start &PAGE_MASK ; +unsigned long vend =PAGE_ALIGN ((unsigned long )end); + +kasan_shallow_populate_pgd (vaddr, vend, node); +} + +#ifdef CONFIG_KASAN_VMALLOC +void __init kasan_populate_early_vm_area_shadow (void *start,unsigned long size) +{ +unsigned long shadow_start, shadow_end; + +if (!is_vmalloc_or_module_addr (start)) +return ; + + shadow_start = (unsigned long )mem_to_shadow (start); + shadow_start =ALIGN_DOWN (shadow_start,PAGE_SIZE ); + shadow_end = (unsigned long )mem_to_shadow (start + size); + shadow_end =ALIGN (shadow_end,PAGE_SIZE ); + +kasan_map_populate (shadow_start, shadow_end,NUMA_NO_NODE ); +} +#endif + asmlinkage void __init kasan_early_init(void) {BUILD_BUG_ON (!IS_ALIGNED (KASAN_SHADOW_START ,PGDIR_SIZE ));@@ -297,9 +451,6 @@ void __init kasan_init(void)/* Maps everything to a single page of zeroes */ kasan_pgd_populate (KASAN_SHADOW_START ,KASAN_SHADOW_END ,NUMA_NO_NODE , true); -kasan_populate_early_shadow (mem_to_shadow ((void *)VMALLOC_START ), -mem_to_shadow ((void *)KFENCE_AREA_END )); -/* Populate the linear mapping */ for_each_mem_range(i, &pa_start, &pa_end) {void *start = (void *)phys_to_virt (pa_start);@@ -309,9 +460,9 @@ void __init kasan_init(void) (unsigned long)mem_to_shadow(end), NUMA_NO_NODE); } -/* Populate modules mapping */ -kasan_map_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), - (unsigned long )mem_to_shadow ((void *)MODULES_END ),NUMA_NO_NODE ); +kasan_shallow_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), + (unsigned long )mem_to_shadow ((void *)VMALLOC_END ),NUMA_NO_NODE ); +/* * KAsan may reuse the contents of kasan_early_shadow_pte directly, so we * should make sure that it maps the zero page read-only. -- 2.43.0
2026年10月22日 15:50, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 120 lines · On Thu, Oct 22, 2026 at 11:12 AM <[email protected]> wrote:
Quoted 110 lines
2026年10月20日 18:46, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 56 lines · On Tue, Oct 20, 2026 at 5:40 PM <[email protected]> wrote:
2026年10月20日 15:47, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 42 lines · On Tue, Oct 20, 2026 at 10:37 AM <[email protected]> wrote:
2026年10月17日 16:24, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 25 lines · On Fri, Oct 16, 2026 at 11:05 PM <[email protected]> wrote:
2026年10月14日 12:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Hi, Haoran,
Quoted 8 lines · On Mon, Oct 12, 2026 at 4:05 PM <[email protected]> wrote:
From: Haoran Jiang <[email protected]>
For the page table of the VMALLOC shadow region, only a partial page table needs to be established, allowing the complete page table entries to be populated on demand during use.
Quoted 6 lines
---
1,Through the kasan_shallow_populate_pgd/p4d/pud/pmd/pte functions, page tables are established for the modules and vmalloc regions, but the page tables are not fully populated; the complete page tables are established automatically when vmalloc is used.Why also change modules together with vmalloc regions? Because they can both alloc/free dynamically, and the current approch can only handle "one time" for modules?
Quoted 6 lines
The shadow memory for modules regions is the same as that of vmalloc regions, full page tables can be built on demand during usage. Memory is allocated when loading modules, and the shadow memory of the allocated memory establishes complete page tables via
kasan_populate_vmalloc_do(load_module->execmem_alloc->kasan_populate_vmalloc_do), similar to riscv and arm64.The current code can also handle modules, so I want to know the difference. I guess the current code can only handle a module loading once, but this patch makes it possible to load/unload modules repeatedly. But I'm not sure. Anyway, I don't want something to become worse.
Quoted 9 lines
1,With the current approach, the shadow page tables for the modules region are established via
kasan_map_populateandKASAN_VMALLOC=n. A test module performs an out-of-bounds write to a global array. After repeatedinsmod/rmmodcycles, the test module reports false positives. This is because the current flow does not properly clean up the shadow data.Taking x86 as an example, since x86 can be configured with KASAN_VMALLOC=n, when KASAN_VMALLOC=n, the correct flow is as follows:
Quoted 7 lines
x86 has some trouble implementing
KASAN_VMALLOC? Or does x86's solution have its advantage?Huacai
On x86,
KASAN_VMALLOCis a configurable option; both implementations are supported.OK, then is the kasan_populate_vmalloc/kasan_release_vmalloc solution better than kasan_alloc_module_shadow/kasan_free_module_shadow solution for modules?
Huacai
The key issue is whether
KASAN_VMALLOCis configurable. If it is configurable, both approaches need to be supported; if it is enabled by default along with KASAN, then only thekasan_populate_vmalloc/kasan_release_vmallocapproach needs to be supported. On arm64,KASAN_VMALLOCis enabled by default when KASAN is enabled. This involves thevmap_stackfeature: arm64 tends to keepvmap_stackenabled, and for KASAN to be compatible withvmap_stack,KASAN_VMALLOCmust also be enabled. The community patch is as follows.commit
acc3042d62cb92c3776767f09f665511c903ef2d
Author: Lecopzer Chen <[email protected]>
Date: Wed Mar 24 12:05:22 2021 +0800...
Quoted 5 lines
From Will's suggestion [1]:
Quoted 3 lines
I would _really_ like to move to VMAP stack unconditionally, and
that would effectively force
KASAN_VMALLOCto be set if KASAN is in use...
At the same time, also remove the
kasan_alloc_module_shadowapproach. The community patch is as follows.commit
00be875879fa676a18415e32f98194db05ee93dc
Author: Mike Rapoport (IBM) <[email protected]>
Date: Sun May 5 19:06:13 2024 +0300arm64: module: remove unneeded call to
kasan_alloc_module_shadowx86 keeps
KASAN_VMALLOCconfigurable (whenvmap_stackis enabled,KASAN_VMALLOCmust also be enabled in order to use KASAN), giving users an additional choice.Quoted 8 lines
For LoongArch,
vmap_stackhas not yet been implemented. However, should we consider staying consistent with arm64 here by enablingKASAN_VMALLOCby default? Ifvmap_stackis implemented in the future, there will be no need to further consider the issue of enablingKASAN_VMALLOC, andkasan_populate_vmalloccan handle both the modules and vmalloc areas at the same time. This approach is also the simplest to handle.
Quoted 5 lines
Enabling
KASAN_VMALLOCis fine for me, I just want to know whether kasan_populate_vmalloc/kasan_release_vmalloc is better than kasan_alloc_module_shadow/kasan_free_module_shadow technically.Huacai
kasan_populate_vmalloc is preferred:
1. kasan_populate_vmalloc can handle both the module area and the vmalloc area at the same time, whereas kasan_alloc_module_shadow can only handle the modules area.
2. kasan_populate_vmalloc is compatible with the vmap_stack feature, whereas kasan_alloc_module_shadow is not.
3. With kasan_populate_vmalloc, shadow data pages for different modules can be shared, whereas kasan_alloc_module_shadow allocates a separate shadow data page for each module, which can save a bit of memory.
Quoted 447 lines
Quoted 446 lines
Quoted 445 lines
Quoted 444 lines
execmem_arch_setup-> flags =EXECMEM_KASAN_SHADOW, the flag is set toEXECMEM_KASAN_SHADOW, and the subsequent flow should select the correct path based on theEXECMEM_KASAN_SHADOWflag.insmod ->
execmem_vmalloc-> (kasan && (kasan_alloc_module_shadow(p, size, GFP_KERNEL)->register_global-> kasan_poison-> rmmod ->kasan_free_module_shadow-> vfree(kasan_mem_to_shadow(vm->addr));This flow it establishes the shadow page tables, then poisons them, and cleans up the shadow data when the module is unloaded.
However, the current situation on LoongArch is as follows:
execmem_arch_setup-> does not setEXECMEM_KASAN_SHADOWinsmod ->
execmem_vmalloc-> does not callkasan_alloc_module_shadow->register_global-> kasan_poison-> rmmod ->kasan_free_module_shadow-> does not call vfree(kasan_mem_to_shadow(vm->addr));LoongArch does not set
EXECMEM_KASAN_SHADOWduring initialization, which causes the shadow data not to be properly cleaned up at unload time, potentially leading to false positives the next time a module is loaded.On RISC-V/arm64,
KASAN_VMALLOCis enabled by default, and the shadow page tables for the module region are established and torn down via kasan_populate_vmalloc_do/kasan_release_vmalloc. This is exactly the approach thatkasan_shallow_populateaims to implement.2,kasan_shallow_populate requires fewer page tables than
kasan_map_populate, and it can also save memory during initialization.Quoted 92 lines
Quoted 16 lines
If the address is pgd-aligned, the page table is established
down to the pgd level; if it is pud-aligned, it is established
down to the pud level, and so on. While establishing the
/pgd/pud/pmd levels, the page table entries must be initialized
to none. For example, a pmd entry of none is aninvalid_pte_table.
Only in this way can the correct page tables be established at
use time. At the same time, boundary issues must be considered
when the address is not aligned. For example,mem_to_shadow(MALLOC_END)
andmem_to_shadow(vmemmap)are in the same pmd, and the region [mem_to_shadow(vmemmap),mem_to_shadow(VMEMMAP_END)] needs to be
mapped to page 0. To avoid affecting themem_to_shadow(vmemmap)region,
only the pmd entries for [mem_to_shadow(MALLOC_END)&PMD_MASK,mem_to_shadow(MALLOC_END)] need to be initialized toinvalid_pte_table. The remaining pmd entries are left as they are.
kasan_pgd_populate(early)populate the page table to early shadow page,kasan_pgd_populate(!early)populate the page table to normal shadow page, andkasan_shallow_populate()populate the page table to invalid_{pud,pmd,pte}_table?It seems you are do similar work as RISCV, and RISCV use three APIs for this purpose: kasan_{,early,shallow}_populate_pgd(). But LoongArch use the same API for the 1st and 2nd purpose, can we add a "shallow" parameter to
kasan_pgd_populate()so we can also handle the 3th purpose?Yes.
kasan_shallow_populate()builds incomplete shadow page tables for vmalloc and modules regions and needs to handle boundaries. As an example:mem_to_shadow(VMALLOC_END)andmem_to_shadow(vmemmap)share one PTE table. The PTE entry formem_to_shadow(VMALLOC_END)must be none, whereas the PTE entry formem_to_shadow(vmemmap)points toearly_shadow_page. The final page table layout is shown belowpte +------
+------------------+ |early_shadow_page | pmd +------------------+ +----------------+ |early_shadow_page | |early_shadow_pte| +------------------+ +----------------+ | global | | ptep |-----------+------------------+ +----------------+ | ... | pte +----------------+ +--------------+ | ... | | global | pgd +----------------+ +--------------+ +------------------+ | ptep |---------------| global | | pmdp +----------+----------------+ +--------------+ +------------------+ pmd | pmdp |----------+--------------+ +------------------+ |invalid_pte | | | +--------------+ | ... | |invalid_pte | | | +--------------+ +------------------+ | ... | +------------------+ pmd | ... | +------------+ +------------------+ | invalid_pte| | ... | +------------+ +------------------+ | invalid_pte| | pmdp |---------- +------------+ +------------------+ | ... | +------------------+
kasan_pgd_populate()can handle the third case by adding a parameter. I will implement this in the next version.Quoted 13 lines
2,The removal of
kasan_populate_early_shadow(mem_to_shadow((void *)VMALLOC_START), mem_to_shadow((void *)KFENCE_AREA_END))achieves the same effect askasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true); both map this region to a shared zero page. (KASAN_SHADOW_START,KASAN_SHADOW_END) already covers (mem_to_shadow((void *)VMALLOC_START),mem_to_shadow((void *)KFENCE_AREA_END)). Other architectures such as arm64/riscv needkasan_populate_early_shadowbecauseclear_shadow(KASAN_SHADOW_START, KASAN_SHADOW_END)is performed beforekasan_populate_early_shadow, so the reserved region must be mapped to the zero page again. On LoongArch, however, the entire shadow space is first mapped to page 0 viakasan_pgd_populate, and then page tables are established for the required regions, which is simpler and clearer.Quoted 8 lines
3,pgd_none/p4d_none/pmd_none/pte_none were added to the definitions of __pgd_none/__p4d_none/__pud_none/__pmd_none/__pte_none respectively because
kasan_populate_early_vm_area_shadowrequires them.kasan_populate_early_vm_area_shadowestablishes complete KASAN shadow page table mappings for the early vmalloc regions beforevmalloc_init. At this point, complete page tables need to be established, butkasan_populate_early_vm_area_shadowis called after step 1, at which timeI'm a little confusing here. What is "step 1" here?
Quoted 9 lines
Step 1 refers to
kasan_shallow_populate(). The page tables created afterkasan_shallow_populate()are shown in the figure above. We need to check for empty entries using /pgd_none/p4d_none/pmd_none/pone_pte(). Only then cankasan_map_populate()build the full page tables.kasan_populate_early_vm_area_shadow()runs afterkasan_shallow_populate()but beforevmalloc_init().
At this stage, page tables for vmalloc regions cannot be automatically populated on demand, so we need to build full page tables here.Then it is better to use
kasan_shallow_populate()rather thanstep 1.Huacai
Quoted 304 lines
Quoted 55 lines
only incomplete page tables have been established for the vmalloc region. The condition for a page table entry to be none requires the pgd_none/p4d_none/pmd_none/pte_none checks. Only by correctly determining whether an entry is none can the complete page tables be established properly.
Test:
https://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson.git
CONFIG_KUNIT=y
CONFIG_KASAN_KUNIT_TEST=mAdd
kunit.enable=1 kasan_multi_shotto the cmdline.Test with 3-level 16K pages and 4-level 4K pages respectively.
before this patch:
...
[ 502.760009] ok 68 vmalloc_oob # SKIP Test requires CONFIG_KASAN_VMALLOC=y...
[ 504.016000] # kasan: pass:49 fail:0 skip:26 total:75 [ 504.016092] # Totals: pass:49 fail:0 skip:26 total:75 [ 504.016171] ok 1 kasanafter this patch:
...
[ 531.630760] ok 68 vmalloc_oob...
[ 532.891057] # kasan: pass:50 fail:0 skip:25 total:75 [ 532.891142] # Totals: pass:50 fail:0 skip:25 total:75 [ 532.891219] ok 1 kasanSigned-off-by Haoran Jiang <[email protected]>---
arch/loongarch/Kconfig| 2 +arch/loongarch/mm/kasan_init.c| 173 ++++++++++++++++++++++++++++++---2 files changed, 164 insertions(+), 11 deletions(-)arch/loongarch/Kconfig +0
-0@@ -119,6 +119,7 @@ config LOONGARCHQuoted 5 lines
select
HAVE_ARCH_JUMP_LABEL
selectHAVE_ARCH_JUMP_LABEL_RELATIVE
selectHAVE_ARCH_KASANif 64BIT
+ selectHAVE_ARCH_KASAN_VMALLOCI think this should be limited by 64BIT.
Huacai
I will fix this in the next revision.
Quoted 237 lines
select
HAVE_ARCH_KFENCEif 64BIT
selectHAVE_ARCH_KGDBifPERF_EVENTS
selectHAVE_ARCH_KSTACK_ERASEarch/loongarch/Kconfig +0
-0@@ -187,6 +188,7 @@ config LOONGARCHselect
HOTPLUG_SMTifHOTPLUG_CPU
selectIRQ_FORCED_THREADING
selectIRQ_LOONGARCH_CPU
+ selectKASAN_VMALLOCif KASAN
selectLOCK_MM_AND_FIND_VMA
selectMMU_GATHER_MERGE_VMASif MMU
selectMMU_GATHER_RCU_TABLE_FREEarch/loongarch/mm/kasan_init.c +162
-12@@ -17,28 +17,28 @@ static pgd_t kasan_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);#define __pgd_none (early, pgd) (0)#else #define __pgd_none (early, pgd) (early ? (pgd_val(pgd) == 0) : \ -(__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d))) +((__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d)) || pgd_none(pgd)))#endif #ifdef __PAGETABLE_PUD_FOLDED#define __p4d_none (early, p4d) (0)#else #define __p4d_none (early, p4d) (early ? (p4d_val(p4d) == 0) : \ -(__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud))) +((__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud)) || p4d_none(p4d)))#endif #ifdef __PAGETABLE_PMD_FOLDED#define __pud_none (early, pud) (0)#else #define __pud_none (early, pud) (early ? (pud_val(pud) == 0) : \ -(__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd))) +((__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd)) || pud_none(pud)))#endif #define __pmd_none (early, pmd) (early ? (pmd_val(pmd) == 0) : \ -(__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte))) +((__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte)) || pmd_none(pmd)))#define __pte_none (early, pte) (early ? pte_none(pte) : \ -((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page))) +(((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page)) || pte_none(pte)))static void *mem_to_shadow (const void *addr) {@@ -242,6 +242,160 @@ static void __init kasan_map_populate(unsigned long start, unsigned long end,kasan_pgd_populate (start &PAGE_MASK ,PAGE_ALIGN (end), node, false); } +static void __init kasan_shallow_populate_pte (pmd_t *pmdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pte_t *ptep =pte_offset_kernel (pmdp, addr); + +do { + next = addr +PAGE_SIZE ; +if (__pte_none (false,ptep_get (ptep))) +pte_clear (&init_mm, addr, ptep); + }while (ptep++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pmd (pud_t *pudp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pmd_t *pmdp =pmd_offset (pudp, addr); + +do { + next =pmd_addr_end (addr, end); + +if (__pmd_none (false,pmdp_get (pmdp))) { +phys_addr_t pte_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PMD_SIZE ) && end - addr >=PMD_SIZE ) { +kernel_pte_init (__va (pte_phys)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); +continue ; + }else { +memcpy (__va (pte_phys), kasan_early_shadow_pte, +sizeof (kasan_early_shadow_pte)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); + } + } +kasan_shallow_populate_pte (pmdp, addr, next, node); + }while (pmdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pud (p4d_t *p4dp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pud_t *pudp =pud_offset (p4dp, addr); + +do { + next =pud_addr_end (addr, end); + +if (__pud_none (false,pudp_get (pudp))) { + +phys_addr_t pmd_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PUD_SIZE ) && end - addr >=PUD_SIZE ) { +#ifndef __PAGETABLE_PMD_FOLDED +pmd_init (__va (pmd_phys)); +#endif +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); +continue ; + }else { +memcpy (__va (pmd_phys), kasan_early_shadow_pmd, +sizeof (kasan_early_shadow_pmd)); +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); + + } + } + +kasan_shallow_populate_pmd (pudp, addr, next, node); + }while (pudp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_p4d (pgd_t *pgdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +p4d_t *p4dp =p4d_offset (pgdp, addr); + +do { + next =p4d_addr_end (addr, end); + +if (__p4d_none (false,p4dp_get (p4dp))) { + +phys_addr_t pud_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,P4D_SIZE ) && end - addr >=P4D_SIZE ) { +#ifndef __PAGETABLE_PUD_FOLDED +pud_init (__va (pud_phys)); +#endif +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); +continue ; + }else { +memcpy (__va (pud_phys), kasan_early_shadow_pud, +sizeof (kasan_early_shadow_pud)); +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); + } + } +kasan_shallow_populate_pud (p4dp, addr, next, node); + }while (p4dp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pgd (unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pgd_t *pgdp; + + pgdp =pgd_offset_k (addr); + +do { + next =pgd_addr_end (addr, end); + +if (__pgd_none (false,pgdp_get (pgdp))) { + +phys_addr_t p4d_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PGDIR_SIZE ) && end - addr >=PGDIR_SIZE ) { +pgd_init (__va (p4d_phys)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); +continue ; + }else { +memcpy (__va (p4d_phys), kasan_early_shadow_p4d, +sizeof (kasan_early_shadow_p4d)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); + } + } + +kasan_shallow_populate_p4d (pgdp, addr, next, node); + }while (pgdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate (unsigned long start,unsigned long end,int node) +{ +unsigned long vaddr = (unsigned long )start &PAGE_MASK ; +unsigned long vend =PAGE_ALIGN ((unsigned long )end); + +kasan_shallow_populate_pgd (vaddr, vend, node); +} + +#ifdef CONFIG_KASAN_VMALLOC +void __init kasan_populate_early_vm_area_shadow (void *start,unsigned long size) +{ +unsigned long shadow_start, shadow_end; + +if (!is_vmalloc_or_module_addr (start)) +return ; + + shadow_start = (unsigned long )mem_to_shadow (start); + shadow_start =ALIGN_DOWN (shadow_start,PAGE_SIZE ); + shadow_end = (unsigned long )mem_to_shadow (start + size); + shadow_end =ALIGN (shadow_end,PAGE_SIZE ); + +kasan_map_populate (shadow_start, shadow_end,NUMA_NO_NODE ); +} +#endif + asmlinkage void __init kasan_early_init(void) {BUILD_BUG_ON (!IS_ALIGNED (KASAN_SHADOW_START ,PGDIR_SIZE ));@@ -297,9 +451,6 @@ void __init kasan_init(void)/* Maps everything to a single page of zeroes */ kasan_pgd_populate (KASAN_SHADOW_START ,KASAN_SHADOW_END ,NUMA_NO_NODE , true); -kasan_populate_early_shadow (mem_to_shadow ((void *)VMALLOC_START ), -mem_to_shadow ((void *)KFENCE_AREA_END )); -/* Populate the linear mapping */ for_each_mem_range(i, &pa_start, &pa_end) {void *start = (void *)phys_to_virt (pa_start);@@ -309,9 +460,9 @@ void __init kasan_init(void) (unsigned long)mem_to_shadow(end), NUMA_NO_NODE); } -/* Populate modules mapping */ -kasan_map_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), - (unsigned long )mem_to_shadow ((void *)MODULES_END ),NUMA_NO_NODE ); +kasan_shallow_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), + (unsigned long )mem_to_shadow ((void *)VMALLOC_END ),NUMA_NO_NODE ); +/* * KAsan may reuse the contents of kasan_early_shadow_pte directly, so we * should make sure that it maps the zero page read-only. -- 2.43.0
Huacai Chen
maintainerQuoted 136 lines · On Thu, Oct 22, 2026 at 6:27 PM <[email protected]> wrote:
2026年10月22日 15:50, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 120 lines · On Thu, Oct 22, 2026 at 11:12 AM <[email protected]> wrote:
Quoted 110 lines
2026年10月20日 18:46, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 56 lines · On Tue, Oct 20, 2026 at 5:40 PM <[email protected]> wrote:
2026年10月20日 15:47, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 42 lines · On Tue, Oct 20, 2026 at 10:37 AM <[email protected]> wrote:
2026年10月17日 16:24, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 25 lines · On Fri, Oct 16, 2026 at 11:05 PM <[email protected]> wrote:
2026年10月14日 12:43, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Hi, Haoran,
Quoted 8 lines · On Mon, Oct 12, 2026 at 4:05 PM <[email protected]> wrote:
From: Haoran Jiang <[email protected]>
For the page table of the VMALLOC shadow region, only a partial page table needs to be established, allowing the complete page table entries to be populated on demand during use.
Quoted 6 lines
---
1,Through the kasan_shallow_populate_pgd/p4d/pud/pmd/pte functions, page tables are established for the modules and vmalloc regions, but the page tables are not fully populated; the complete page tables are established automatically when vmalloc is used.Why also change modules together with vmalloc regions? Because they can both alloc/free dynamically, and the current approch can only handle "one time" for modules?
Quoted 6 lines
The shadow memory for modules regions is the same as that of vmalloc regions, full page tables can be built on demand during usage. Memory is allocated when loading modules, and the shadow memory of the allocated memory establishes complete page tables via
kasan_populate_vmalloc_do(load_module->execmem_alloc->kasan_populate_vmalloc_do), similar to riscv and arm64.The current code can also handle modules, so I want to know the difference. I guess the current code can only handle a module loading once, but this patch makes it possible to load/unload modules repeatedly. But I'm not sure. Anyway, I don't want something to become worse.
Quoted 9 lines
1,With the current approach, the shadow page tables for the modules region are established via
kasan_map_populateandKASAN_VMALLOC=n. A test module performs an out-of-bounds write to a global array. After repeatedinsmod/rmmodcycles, the test module reports false positives. This is because the current flow does not properly clean up the shadow data.Taking x86 as an example, since x86 can be configured with KASAN_VMALLOC=n, when KASAN_VMALLOC=n, the correct flow is as follows:
Quoted 7 lines
x86 has some trouble implementing
KASAN_VMALLOC? Or does x86's solution have its advantage?Huacai
On x86,
KASAN_VMALLOCis a configurable option; both implementations are supported.OK, then is the kasan_populate_vmalloc/kasan_release_vmalloc solution better than kasan_alloc_module_shadow/kasan_free_module_shadow solution for modules?
Huacai
The key issue is whether
KASAN_VMALLOCis configurable. If it is configurable, both approaches need to be supported; if it is enabled by default along with KASAN, then only thekasan_populate_vmalloc/kasan_release_vmallocapproach needs to be supported. On arm64,KASAN_VMALLOCis enabled by default when KASAN is enabled. This involves thevmap_stackfeature: arm64 tends to keepvmap_stackenabled, and for KASAN to be compatible withvmap_stack,KASAN_VMALLOCmust also be enabled. The community patch is as follows.commit
acc3042d62cb92c3776767f09f665511c903ef2d
Author: Lecopzer Chen <[email protected]>
Date: Wed Mar 24 12:05:22 2021 +0800...
Quoted 5 lines
From Will's suggestion [1]:
Quoted 3 lines
I would _really_ like to move to VMAP stack unconditionally, and
that would effectively force
KASAN_VMALLOCto be set if KASAN is in use...
At the same time, also remove the
kasan_alloc_module_shadowapproach. The community patch is as follows.commit
00be875879fa676a18415e32f98194db05ee93dc
Author: Mike Rapoport (IBM) <[email protected]>
Date: Sun May 5 19:06:13 2024 +0300arm64: module: remove unneeded call to
kasan_alloc_module_shadowx86 keeps
KASAN_VMALLOCconfigurable (whenvmap_stackis enabled,KASAN_VMALLOCmust also be enabled in order to use KASAN), giving users an additional choice.Quoted 8 lines
For LoongArch,
vmap_stackhas not yet been implemented. However, should we consider staying consistent with arm64 here by enablingKASAN_VMALLOCby default? Ifvmap_stackis implemented in the future, there will be no need to further consider the issue of enablingKASAN_VMALLOC, andkasan_populate_vmalloccan handle both the modules and vmalloc areas at the same time. This approach is also the simplest to handle.Quoted 5 lines
Enabling
KASAN_VMALLOCis fine for me, I just want to know whether kasan_populate_vmalloc/kasan_release_vmalloc is better than kasan_alloc_module_shadow/kasan_free_module_shadow technically.Huacai
kasan_populate_vmallocis preferred:1.
kasan_populate_vmalloccan handle both the module area and the vmalloc area at the same time, whereaskasan_alloc_module_shadowcan only handle the modules area.
Quoted 6 lines
2.
kasan_populate_vmallocis compatible with thevmap_stackfeature, whereaskasan_alloc_module_shadowis not.3. With
kasan_populate_vmalloc, shadow data pages for different modules can be shared, whereaskasan_alloc_module_shadowallocates a separate shadow data page for each module, which can save a bit of memory.
OK, go ahead, please.
Huacai
Quoted 448 lines
Quoted 448 lines
Quoted 447 lines
Quoted 446 lines
Quoted 445 lines
execmem_arch_setup-> flags =EXECMEM_KASAN_SHADOW, the flag is set toEXECMEM_KASAN_SHADOW, and the subsequent flow should select the correct path based on theEXECMEM_KASAN_SHADOWflag.insmod ->
execmem_vmalloc-> (kasan && (kasan_alloc_module_shadow(p, size, GFP_KERNEL)->register_global-> kasan_poison-> rmmod ->kasan_free_module_shadow-> vfree(kasan_mem_to_shadow(vm->addr));This flow it establishes the shadow page tables, then poisons them, and cleans up the shadow data when the module is unloaded.
However, the current situation on LoongArch is as follows:
execmem_arch_setup-> does not setEXECMEM_KASAN_SHADOWinsmod ->
execmem_vmalloc-> does not callkasan_alloc_module_shadow->register_global-> kasan_poison-> rmmod ->kasan_free_module_shadow-> does not call vfree(kasan_mem_to_shadow(vm->addr));LoongArch does not set
EXECMEM_KASAN_SHADOWduring initialization, which causes the shadow data not to be properly cleaned up at unload time, potentially leading to false positives the next time a module is loaded.On RISC-V/arm64,
KASAN_VMALLOCis enabled by default, and the shadow page tables for the module region are established and torn down via kasan_populate_vmalloc_do/kasan_release_vmalloc. This is exactly the approach thatkasan_shallow_populateaims to implement.2,kasan_shallow_populate requires fewer page tables than
kasan_map_populate, and it can also save memory during initialization.Quoted 92 lines
Quoted 16 lines
If the address is pgd-aligned, the page table is established
down to the pgd level; if it is pud-aligned, it is established
down to the pud level, and so on. While establishing the
/pgd/pud/pmd levels, the page table entries must be initialized
to none. For example, a pmd entry of none is aninvalid_pte_table.
Only in this way can the correct page tables be established at
use time. At the same time, boundary issues must be considered
when the address is not aligned. For example,mem_to_shadow(MALLOC_END)
andmem_to_shadow(vmemmap)are in the same pmd, and the region [mem_to_shadow(vmemmap),mem_to_shadow(VMEMMAP_END)] needs to be
mapped to page 0. To avoid affecting themem_to_shadow(vmemmap)region,
only the pmd entries for [mem_to_shadow(MALLOC_END)&PMD_MASK,mem_to_shadow(MALLOC_END)] need to be initialized toinvalid_pte_table. The remaining pmd entries are left as they are.
kasan_pgd_populate(early)populate the page table to early shadow page,kasan_pgd_populate(!early)populate the page table to normal shadow page, andkasan_shallow_populate()populate the page table to invalid_{pud,pmd,pte}_table?It seems you are do similar work as RISCV, and RISCV use three APIs for this purpose: kasan_{,early,shallow}_populate_pgd(). But LoongArch use the same API for the 1st and 2nd purpose, can we add a "shallow" parameter to
kasan_pgd_populate()so we can also handle the 3th purpose?Yes.
kasan_shallow_populate()builds incomplete shadow page tables for vmalloc and modules regions and needs to handle boundaries. As an example:mem_to_shadow(VMALLOC_END)andmem_to_shadow(vmemmap)share one PTE table. The PTE entry formem_to_shadow(VMALLOC_END)must be none, whereas the PTE entry formem_to_shadow(vmemmap)points toearly_shadow_page. The final page table layout is shown belowpte +------
+------------------+ |early_shadow_page | pmd +------------------+ +----------------+ |early_shadow_page | |early_shadow_pte| +------------------+ +----------------+ | global | | ptep |-----------+------------------+ +----------------+ | ... | pte +----------------+ +--------------+ | ... | | global | pgd +----------------+ +--------------+ +------------------+ | ptep |---------------| global | | pmdp +----------+----------------+ +--------------+ +------------------+ pmd | pmdp |----------+--------------+ +------------------+ |invalid_pte | | | +--------------+ | ... | |invalid_pte | | | +--------------+ +------------------+ | ... | +------------------+ pmd | ... | +------------+ +------------------+ | invalid_pte| | ... | +------------+ +------------------+ | invalid_pte| | pmdp |---------- +------------+ +------------------+ | ... | +------------------+
kasan_pgd_populate()can handle the third case by adding a parameter. I will implement this in the next version.Quoted 13 lines
2,The removal of
kasan_populate_early_shadow(mem_to_shadow((void *)VMALLOC_START), mem_to_shadow((void *)KFENCE_AREA_END))achieves the same effect askasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true); both map this region to a shared zero page. (KASAN_SHADOW_START,KASAN_SHADOW_END) already covers (mem_to_shadow((void *)VMALLOC_START),mem_to_shadow((void *)KFENCE_AREA_END)). Other architectures such as arm64/riscv needkasan_populate_early_shadowbecauseclear_shadow(KASAN_SHADOW_START, KASAN_SHADOW_END)is performed beforekasan_populate_early_shadow, so the reserved region must be mapped to the zero page again. On LoongArch, however, the entire shadow space is first mapped to page 0 viakasan_pgd_populate, and then page tables are established for the required regions, which is simpler and clearer.Quoted 8 lines
3,pgd_none/p4d_none/pmd_none/pte_none were added to the definitions of __pgd_none/__p4d_none/__pud_none/__pmd_none/__pte_none respectively because
kasan_populate_early_vm_area_shadowrequires them.kasan_populate_early_vm_area_shadowestablishes complete KASAN shadow page table mappings for the early vmalloc regions beforevmalloc_init. At this point, complete page tables need to be established, butkasan_populate_early_vm_area_shadowis called after step 1, at which timeI'm a little confusing here. What is "step 1" here?
Quoted 9 lines
Step 1 refers to
kasan_shallow_populate(). The page tables created afterkasan_shallow_populate()are shown in the figure above. We need to check for empty entries using /pgd_none/p4d_none/pmd_none/pone_pte(). Only then cankasan_map_populate()build the full page tables.kasan_populate_early_vm_area_shadow()runs afterkasan_shallow_populate()but beforevmalloc_init().
At this stage, page tables for vmalloc regions cannot be automatically populated on demand, so we need to build full page tables here.Then it is better to use
kasan_shallow_populate()rather thanstep 1.Huacai
Quoted 304 lines
Quoted 55 lines
only incomplete page tables have been established for the vmalloc region. The condition for a page table entry to be none requires the pgd_none/p4d_none/pmd_none/pte_none checks. Only by correctly determining whether an entry is none can the complete page tables be established properly.
Test:
https://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson.git
CONFIG_KUNIT=y
CONFIG_KASAN_KUNIT_TEST=mAdd
kunit.enable=1 kasan_multi_shotto the cmdline.Test with 3-level 16K pages and 4-level 4K pages respectively.
before this patch:
...
[ 502.760009] ok 68 vmalloc_oob # SKIP Test requires CONFIG_KASAN_VMALLOC=y...
[ 504.016000] # kasan: pass:49 fail:0 skip:26 total:75 [ 504.016092] # Totals: pass:49 fail:0 skip:26 total:75 [ 504.016171] ok 1 kasanafter this patch:
...
[ 531.630760] ok 68 vmalloc_oob...
[ 532.891057] # kasan: pass:50 fail:0 skip:25 total:75 [ 532.891142] # Totals: pass:50 fail:0 skip:25 total:75 [ 532.891219] ok 1 kasanSigned-off-by Haoran Jiang <[email protected]>---
arch/loongarch/Kconfig| 2 +arch/loongarch/mm/kasan_init.c| 173 ++++++++++++++++++++++++++++++---2 files changed, 164 insertions(+), 11 deletions(-)arch/loongarch/Kconfig +0
-0@@ -119,6 +119,7 @@ config LOONGARCHQuoted 5 lines
select
HAVE_ARCH_JUMP_LABEL
selectHAVE_ARCH_JUMP_LABEL_RELATIVE
selectHAVE_ARCH_KASANif 64BIT
+ selectHAVE_ARCH_KASAN_VMALLOCI think this should be limited by 64BIT.
Huacai
I will fix this in the next revision.
Quoted 237 lines
select
HAVE_ARCH_KFENCEif 64BIT
selectHAVE_ARCH_KGDBifPERF_EVENTS
selectHAVE_ARCH_KSTACK_ERASEarch/loongarch/Kconfig +0
-0@@ -187,6 +188,7 @@ config LOONGARCHselect
HOTPLUG_SMTifHOTPLUG_CPU
selectIRQ_FORCED_THREADING
selectIRQ_LOONGARCH_CPU
+ selectKASAN_VMALLOCif KASAN
selectLOCK_MM_AND_FIND_VMA
selectMMU_GATHER_MERGE_VMASif MMU
selectMMU_GATHER_RCU_TABLE_FREEarch/loongarch/mm/kasan_init.c +162
-12@@ -17,28 +17,28 @@ static pgd_t kasan_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);#define __pgd_none (early, pgd) (0)#else #define __pgd_none (early, pgd) (early ? (pgd_val(pgd) == 0) : \ -(__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d))) +((__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_p4d)) || pgd_none(pgd)))#endif #ifdef __PAGETABLE_PUD_FOLDED#define __p4d_none (early, p4d) (0)#else #define __p4d_none (early, p4d) (early ? (p4d_val(p4d) == 0) : \ -(__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud))) +((__pa(p4d_val(p4d)) == (unsigned long)__pa(kasan_early_shadow_pud)) || p4d_none(p4d)))#endif #ifdef __PAGETABLE_PMD_FOLDED#define __pud_none (early, pud) (0)#else #define __pud_none (early, pud) (early ? (pud_val(pud) == 0) : \ -(__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd))) +((__pa(pud_val(pud)) == (unsigned long)__pa(kasan_early_shadow_pmd)) || pud_none(pud)))#endif #define __pmd_none (early, pmd) (early ? (pmd_val(pmd) == 0) : \ -(__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte))) +((__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte)) || pmd_none(pmd)))#define __pte_none (early, pte) (early ? pte_none(pte) : \ -((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page))) +(((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page)) || pte_none(pte)))static void *mem_to_shadow (const void *addr) {@@ -242,6 +242,160 @@ static void __init kasan_map_populate(unsigned long start, unsigned long end,kasan_pgd_populate (start &PAGE_MASK ,PAGE_ALIGN (end), node, false); } +static void __init kasan_shallow_populate_pte (pmd_t *pmdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pte_t *ptep =pte_offset_kernel (pmdp, addr); + +do { + next = addr +PAGE_SIZE ; +if (__pte_none (false,ptep_get (ptep))) +pte_clear (&init_mm, addr, ptep); + }while (ptep++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pmd (pud_t *pudp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pmd_t *pmdp =pmd_offset (pudp, addr); + +do { + next =pmd_addr_end (addr, end); + +if (__pmd_none (false,pmdp_get (pmdp))) { +phys_addr_t pte_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PMD_SIZE ) && end - addr >=PMD_SIZE ) { +kernel_pte_init (__va (pte_phys)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); +continue ; + }else { +memcpy (__va (pte_phys), kasan_early_shadow_pte, +sizeof (kasan_early_shadow_pte)); +pmd_populate_kernel (NULL , pmdp, (pte_t *)__va (pte_phys)); + } + } +kasan_shallow_populate_pte (pmdp, addr, next, node); + }while (pmdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pud (p4d_t *p4dp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pud_t *pudp =pud_offset (p4dp, addr); + +do { + next =pud_addr_end (addr, end); + +if (__pud_none (false,pudp_get (pudp))) { + +phys_addr_t pmd_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PUD_SIZE ) && end - addr >=PUD_SIZE ) { +#ifndef __PAGETABLE_PMD_FOLDED +pmd_init (__va (pmd_phys)); +#endif +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); +continue ; + }else { +memcpy (__va (pmd_phys), kasan_early_shadow_pmd, +sizeof (kasan_early_shadow_pmd)); +pud_populate (&init_mm, pudp, (pmd_t *)__va (pmd_phys)); + + } + } + +kasan_shallow_populate_pmd (pudp, addr, next, node); + }while (pudp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_p4d (pgd_t *pgdp,unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +p4d_t *p4dp =p4d_offset (pgdp, addr); + +do { + next =p4d_addr_end (addr, end); + +if (__p4d_none (false,p4dp_get (p4dp))) { + +phys_addr_t pud_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,P4D_SIZE ) && end - addr >=P4D_SIZE ) { +#ifndef __PAGETABLE_PUD_FOLDED +pud_init (__va (pud_phys)); +#endif +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); +continue ; + }else { +memcpy (__va (pud_phys), kasan_early_shadow_pud, +sizeof (kasan_early_shadow_pud)); +p4d_populate (&init_mm, p4dp, (pud_t *)__va (pud_phys)); + } + } +kasan_shallow_populate_pud (p4dp, addr, next, node); + }while (p4dp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pgd (unsigned long addr, +unsigned long end,int node) +{ +unsigned long next; +pgd_t *pgdp; + + pgdp =pgd_offset_k (addr); + +do { + next =pgd_addr_end (addr, end); + +if (__pgd_none (false,pgdp_get (pgdp))) { + +phys_addr_t p4d_phys =kasan_alloc_zeroed_page (node); + +if (IS_ALIGNED (addr,PGDIR_SIZE ) && end - addr >=PGDIR_SIZE ) { +pgd_init (__va (p4d_phys)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); +continue ; + }else { +memcpy (__va (p4d_phys), kasan_early_shadow_p4d, +sizeof (kasan_early_shadow_p4d)); +pgd_populate (&init_mm, pgdp, (p4d_t *)__va (p4d_phys)); + } + } + +kasan_shallow_populate_p4d (pgdp, addr, next, node); + }while (pgdp++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate (unsigned long start,unsigned long end,int node) +{ +unsigned long vaddr = (unsigned long )start &PAGE_MASK ; +unsigned long vend =PAGE_ALIGN ((unsigned long )end); + +kasan_shallow_populate_pgd (vaddr, vend, node); +} + +#ifdef CONFIG_KASAN_VMALLOC +void __init kasan_populate_early_vm_area_shadow (void *start,unsigned long size) +{ +unsigned long shadow_start, shadow_end; + +if (!is_vmalloc_or_module_addr (start)) +return ; + + shadow_start = (unsigned long )mem_to_shadow (start); + shadow_start =ALIGN_DOWN (shadow_start,PAGE_SIZE ); + shadow_end = (unsigned long )mem_to_shadow (start + size); + shadow_end =ALIGN (shadow_end,PAGE_SIZE ); + +kasan_map_populate (shadow_start, shadow_end,NUMA_NO_NODE ); +} +#endif + asmlinkage void __init kasan_early_init(void) {BUILD_BUG_ON (!IS_ALIGNED (KASAN_SHADOW_START ,PGDIR_SIZE ));@@ -297,9 +451,6 @@ void __init kasan_init(void)/* Maps everything to a single page of zeroes */ kasan_pgd_populate (KASAN_SHADOW_START ,KASAN_SHADOW_END ,NUMA_NO_NODE , true); -kasan_populate_early_shadow (mem_to_shadow ((void *)VMALLOC_START ), -mem_to_shadow ((void *)KFENCE_AREA_END )); -/* Populate the linear mapping */ for_each_mem_range(i, &pa_start, &pa_end) {void *start = (void *)phys_to_virt (pa_start);@@ -309,9 +460,9 @@ void __init kasan_init(void) (unsigned long)mem_to_shadow(end), NUMA_NO_NODE); } -/* Populate modules mapping */ -kasan_map_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), - (unsigned long )mem_to_shadow ((void *)MODULES_END ),NUMA_NO_NODE ); +kasan_shallow_populate ((unsigned long )mem_to_shadow ((void *)MODULES_VADDR ), + (unsigned long )mem_to_shadow ((void *)VMALLOC_END ),NUMA_NO_NODE ); +/* * KAsan may reuse the contents of kasan_early_shadow_pte directly, so we * should make sure that it maps the zero page read-only. -- 2.43.0