Skip to contentSkip to conversation

arm64: kasan: support CONFIG_KASAN_VMALLOC

PATCH In review Lecopzer Chen 5 participants 33 messages Jan 9, 2021 – Feb 5, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover arm64: kasan: support CONFIG_KASAN_VMALLOC

Linux supports KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Acroding to how x86 ported it [1], they early allocated p4d and pgd, but in arm64 I just simulate how KAsan supports MODULES_VADDR in arm64 by not to populate the vmalloc area except for kimg address.

Test environment:
4G and 8G Qemu virt,
39-bit VA + 4k PAGE_SIZE with 3-level page table, test by lib/test_kasan.ko and lib/test_kasan_module.ko

It also works in Kaslr with CONFIG_RANDOMIZE_MODULE_REGION_FULL and randomize module region inside vmalloc area.

[1]: commit 0609ae011deb41c ("x86/kasan: support KASAN_VMALLOC")

v2 -> v1
1. kasan_init.c tweak indent
2. change Kconfig depends only on HAVE_ARCH_KASAN
3. support randomized module region.

v1:
https://lore.kernel.org/lkml/[email protected]/

Lecopzer Chen (4):
arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC
arm64: kasan: abstract _text and _end to KERNEL_START/END
arm64: Kconfig: support CONFIG_KASAN_VMALLOC
arm64: kaslr: support randomized module area with KASAN_VMALLOC

 arch/arm64/Kconfig         |  1 +
 arch/arm64/kernel/kaslr.c  | 18 ++++++++++--------
 arch/arm64/kernel/module.c | 16 +++++++++-------
 arch/arm64/mm/kasan_init.c | 29 +++++++++++++++++++++--------
 4 files changed, 41 insertions(+), 23 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/4 arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC

Linux support KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Like how the MODULES_VADDR does now, just not to early populate the VMALLOC_START between VMALLOC_END.
similarly, the kernel code mapping is now in the VMALLOC area and should keep these area populated.

---
 arch/arm64/mm/kasan_init.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
arch/arm64/mm/kasan_init.c +18 -5
index d8e66c78440e..39b218a64279 100644
@@ -214,6 +214,7 @@ static void __init kasan_init_shadow(void)
 {
 	u64 kimg_shadow_start, kimg_shadow_end;
 	u64 mod_shadow_start, mod_shadow_end;
+	u64 vmalloc_shadow_start, vmalloc_shadow_end;
 	phys_addr_t pa_start, pa_end;
 	u64 i;
 
@@ -223,6 +224,9 @@ static void __init kasan_init_shadow(void)
 	mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
 	mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
 
+	vmalloc_shadow_start = (u64)kasan_mem_to_shadow((void *)VMALLOC_START);
+	vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END);
+
 	/*
 	 * We are going to perform proper setup of shadow memory.
 	 * At first we should unmap early shadow (clear_pgds() call below).
@@ -241,12 +245,21 @@ static void __init kasan_init_shadow(void)
 
 	kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
 				   (void *)mod_shadow_start);
-	kasan_populate_early_shadow((void *)kimg_shadow_end,
-				   (void *)KASAN_SHADOW_END);
+	if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {
+		kasan_populate_early_shadow((void *)vmalloc_shadow_end,
+					    (void *)KASAN_SHADOW_END);
+		if (vmalloc_shadow_start > mod_shadow_end)
+			kasan_populate_early_shadow((void *)mod_shadow_end,
+						    (void *)vmalloc_shadow_start);
+
+	} else {
+		kasan_populate_early_shadow((void *)kimg_shadow_end,
+					    (void *)KASAN_SHADOW_END);
+		if (kimg_shadow_start > mod_shadow_end)
+			kasan_populate_early_shadow((void *)mod_shadow_end,
+						    (void *)kimg_shadow_start);
+	}
 
-	if (kimg_shadow_start > mod_shadow_end)
-		kasan_populate_early_shadow((void *)mod_shadow_end,
-					    (void *)kimg_shadow_start);
 
 	for_each_mem_range(i, &pa_start, &pa_end) {
 		void *start = (void *)__phys_to_virt(pa_start);
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v2 1/4] arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC

Quoted 10 lines · Lecopzer Chen

Linux support KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Like how the MODULES_VADDR does now, just not to early populate the VMALLOC_START between VMALLOC_END.
similarly, the kernel code mapping is now in the VMALLOC area and should keep these area populated.

This commit log text is a bit hard to follow. You are saying that the vmalloc region is *not* backed with zero shadow or any default mapping at all, right, and everything gets allocated on demand, just like is the case for modules?

Quoted 17 lines
---
 arch/arm64/mm/kasan_init.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
arch/arm64/mm/kasan_init.c +1 -0
index d8e66c78440e..39b218a64279 100644
@@ -214,6 +214,7 @@ static void __init kasan_init_shadow(void)
 {
        u64 kimg_shadow_start, kimg_shadow_end;
        u64 mod_shadow_start, mod_shadow_end;
+       u64 vmalloc_shadow_start, vmalloc_shadow_end;
        phys_addr_t pa_start, pa_end;
        u64 i;
 

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 229

Quoted 6 lines
arch/arm64/mm/kasan_init.c +3 -0
@@ -223,6 +224,9 @@ static void __init kasan_init_shadow(void)
        mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
        mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
 
+       vmalloc_shadow_start = (u64)kasan_mem_to_shadow((void *)VMALLOC_START);
+       vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END);
+

This and the below seems overly complicated, given that VMALLOC_START == MODULES_END. Can we simplify this?

Quoted 33 lines
arch/arm64/mm/kasan_init.c +14 -6
        /*
         * We are going to perform proper setup of shadow memory.
         * At first we should unmap early shadow (clear_pgds() call below).
@@ -241,12 +245,21 @@ static void __init kasan_init_shadow(void)
 
        kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
                                   (void *)mod_shadow_start);
-       kasan_populate_early_shadow((void *)kimg_shadow_end,
-                                  (void *)KASAN_SHADOW_END);
+       if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {
+               kasan_populate_early_shadow((void *)vmalloc_shadow_end,
+                                           (void *)KASAN_SHADOW_END);
+               if (vmalloc_shadow_start > mod_shadow_end)
+                       kasan_populate_early_shadow((void *)mod_shadow_end,
+                                                   (void *)vmalloc_shadow_start);
+
+       } else {
+               kasan_populate_early_shadow((void *)kimg_shadow_end,
+                                           (void *)KASAN_SHADOW_END);
+               if (kimg_shadow_start > mod_shadow_end)
+                       kasan_populate_early_shadow((void *)mod_shadow_end,
+                                                   (void *)kimg_shadow_start);
+       }
 
-       if (kimg_shadow_start > mod_shadow_end)
-               kasan_populate_early_shadow((void *)mod_shadow_end,
-                                           (void *)kimg_shadow_start);
 
        for_each_mem_range(i, &pa_start, &pa_end) {
                void *start = (void *)__phys_to_virt(pa_start);
--

2.25.1

#3 lore raw

Re: [PATCH v2 1/4] arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC

Quoted 10 lines · Lecopzer Chen
Quoted 9 lines

Linux support KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Like how the MODULES_VADDR does now, just not to early populate the VMALLOC_START between VMALLOC_END.
similarly, the kernel code mapping is now in the VMALLOC area and should keep these area populated.

Quoted 7 lines
Quoted 1 line

This commit log text is a bit hard to follow. You are saying that the vmalloc region is *not* backed with zero shadow or any default mapping at all, right, and everything gets allocated on demand, just like is the case for modules?

It's much more like:

before:

MODULE_VADDR: no mapping, no zoreo shadow at init
VMALLOC_VADDR: backed with zero shadow at init

after:

MODULE_VADDR: no mapping, no zoreo shadow at init
VMALLOC_VADDR: no mapping, no zoreo shadow at init

So it should be both "not backed with zero shadow" and "not any mapping and everything gets allocated on demand".

And the "not backed with zero shadow" is like a subset of "not any mapping ...".

Is that being more clear if the commit revises to:

----------------------
Like how the MODULES_VADDR does now, just not to early populate the VMALLOC_START between VMALLOC_END.

Before:

MODULE_VADDR: no mapping, no zoreo shadow at init
VMALLOC_VADDR: backed with zero shadow at init

After:

VMALLOC_VADDR: no mapping, no zoreo shadow at init

Thus the mapping will get allocate on demand by the core function of KASAN vmalloc.

similarly, the kernel code mapping is now in the VMALLOC area and should keep these area populated.
--------------------

Or would you have any suggestion?

Thanks a lot for your review!

BRs,
Lecopzer

maintainer
#4 lore raw

Re: [PATCH v2 1/4] arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC

In reply to Lecopzer Chen
Quoted 36 lines · Lecopzer Chen

Linux support KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Like how the MODULES_VADDR does now, just not to early populate the VMALLOC_START between VMALLOC_END.
similarly, the kernel code mapping is now in the VMALLOC area and should keep these area populated.

---
 arch/arm64/mm/kasan_init.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
arch/arm64/mm/kasan_init.c +4 -0
index d8e66c78440e..39b218a64279 100644
@@ -214,6 +214,7 @@ static void __init kasan_init_shadow(void)
 {
 	u64 kimg_shadow_start, kimg_shadow_end;
 	u64 mod_shadow_start, mod_shadow_end;
+	u64 vmalloc_shadow_start, vmalloc_shadow_end;
 	phys_addr_t pa_start, pa_end;
 	u64 i;
 
@@ -223,6 +224,9 @@ static void __init kasan_init_shadow(void)
 	mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
 	mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
 
+	vmalloc_shadow_start = (u64)kasan_mem_to_shadow((void *)VMALLOC_START);
+	vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END);
+
 	/*
 	 * We are going to perform proper setup of shadow memory.
 	 * At first we should unmap early shadow (clear_pgds() call below).

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 248

Quoted 6 lines
arch/arm64/mm/kasan_init.c +1 -2
@@ -241,12 +245,21 @@ static void __init kasan_init_shadow(void)
 
 	kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
 				   (void *)mod_shadow_start);
-	kasan_populate_early_shadow((void *)kimg_shadow_end,
-				   (void *)KASAN_SHADOW_END);
+	if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {

Do we really need yet another CONFIG option for KASAN? What's the use-case for *not* enabling this if you're already enabling one of the KASAN backends?

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 251

Quoted 3 lines
arch/arm64/mm/kasan_init.c +3 -0
+		kasan_populate_early_shadow((void *)vmalloc_shadow_end,
+					    (void *)KASAN_SHADOW_END);
+		if (vmalloc_shadow_start > mod_shadow_end)

To echo Ard's concern: when is the above 'if' condition true?

Will

#5 lore raw

Re: [PATCH v2 1/4] arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC

Quoted 38 lines · Lecopzer Chen
Quoted 37 lines

Linux support KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Like how the MODULES_VADDR does now, just not to early populate the VMALLOC_START between VMALLOC_END.
similarly, the kernel code mapping is now in the VMALLOC area and should keep these area populated.

---
 arch/arm64/mm/kasan_init.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
arch/arm64/mm/kasan_init.c +4 -0
index d8e66c78440e..39b218a64279 100644
@@ -214,6 +214,7 @@ static void __init kasan_init_shadow(void)
 {
 	u64 kimg_shadow_start, kimg_shadow_end;
 	u64 mod_shadow_start, mod_shadow_end;
+	u64 vmalloc_shadow_start, vmalloc_shadow_end;
 	phys_addr_t pa_start, pa_end;
 	u64 i;
 
@@ -223,6 +224,9 @@ static void __init kasan_init_shadow(void)
 	mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
 	mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
 
+	vmalloc_shadow_start = (u64)kasan_mem_to_shadow((void *)VMALLOC_START);
+	vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END);
+
 	/*
 	 * We are going to perform proper setup of shadow memory.
 	 * At first we should unmap early shadow (clear_pgds() call below).
@@ -241,12 +245,21 @@ static void __init kasan_init_shadow(void)
 
Quoted 9 lines

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 248

Quoted 5 lines
arch/arm64/mm/kasan_init.c +1 -2
 	kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
 				   (void *)mod_shadow_start);
-	kasan_populate_early_shadow((void *)kimg_shadow_end,
-				   (void *)KASAN_SHADOW_END);
+	if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {

Do we really need yet another CONFIG option for KASAN? What's the use-case for *not* enabling this if you're already enabling one of the KASAN backends?

As I know, KASAN_VMALLOC now only supports KASAN_GENERIC and also KASAN_VMALLOC uses more memory to map real shadow memory (1/8 of vmalloc va).

There should be someone can enable KASAN_GENERIC but can't use VMALLOC due to memory issue.

Quoted 5 lines

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 251

Quoted 3 lines
arch/arm64/mm/kasan_init.c +3 -0
+		kasan_populate_early_shadow((void *)vmalloc_shadow_end,
+					    (void *)KASAN_SHADOW_END);
+		if (vmalloc_shadow_start > mod_shadow_end)

To echo Ard's concern: when is the above 'if' condition true?

After reviewing this code,
since VMALLOC_STAR is a compiler defined macro of MODULES_END, this if-condition will never be true.

I also test it with removing this and works fine.

I'll remove this in the next version patch,
thanks a lot for pointing out this.

BRs,
Lecopzer

maintainer
#6 lore raw

Re: [PATCH v2 1/4] arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC

Quoted 48 lines · Lecopzer Chen
Quoted 38 lines · Lecopzer Chen
Quoted 37 lines

Linux support KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Like how the MODULES_VADDR does now, just not to early populate the VMALLOC_START between VMALLOC_END.
similarly, the kernel code mapping is now in the VMALLOC area and should keep these area populated.

---
 arch/arm64/mm/kasan_init.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
arch/arm64/mm/kasan_init.c +4 -0
index d8e66c78440e..39b218a64279 100644
@@ -214,6 +214,7 @@ static void __init kasan_init_shadow(void)
 {
 	u64 kimg_shadow_start, kimg_shadow_end;
 	u64 mod_shadow_start, mod_shadow_end;
+	u64 vmalloc_shadow_start, vmalloc_shadow_end;
 	phys_addr_t pa_start, pa_end;
 	u64 i;
 
@@ -223,6 +224,9 @@ static void __init kasan_init_shadow(void)
 	mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
 	mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
 
+	vmalloc_shadow_start = (u64)kasan_mem_to_shadow((void *)VMALLOC_START);
+	vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END);
+
 	/*
 	 * We are going to perform proper setup of shadow memory.
 	 * At first we should unmap early shadow (clear_pgds() call below).
@@ -241,12 +245,21 @@ static void __init kasan_init_shadow(void)
 
Quoted 9 lines

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 248

Quoted 5 lines
arch/arm64/mm/kasan_init.c +1 -2
 	kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
 				   (void *)mod_shadow_start);
-	kasan_populate_early_shadow((void *)kimg_shadow_end,
-				   (void *)KASAN_SHADOW_END);
+	if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {

Do we really need yet another CONFIG option for KASAN? What's the use-case for *not* enabling this if you're already enabling one of the KASAN backends?

Quoted 2 lines

As I know, KASAN_VMALLOC now only supports KASAN_GENERIC and also KASAN_VMALLOC uses more memory to map real shadow memory (1/8 of vmalloc va).

The shadow is allocated dynamically though, isn't it?

Quoted 2 lines

There should be someone can enable KASAN_GENERIC but can't use VMALLOC due to memory issue.

That doesn't sound particularly realistic to me. The reason I'm pushing here is because I would _really_ like to move to VMAP stack unconditionally, and that would effectively force KASAN_VMALLOC to be set if KASAN is in use.

So unless there's a really good reason not to do that, please can we make this unconditional for arm64? Pretty please?

Will

#7 lore raw

Re: [PATCH v2 1/4] arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC

Quoted 50 lines · Lecopzer Chen
Quoted 48 lines
Quoted 47 lines · Lecopzer Chen
Quoted 36 lines

Linux support KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Like how the MODULES_VADDR does now, just not to early populate the VMALLOC_START between VMALLOC_END.
similarly, the kernel code mapping is now in the VMALLOC area and should keep these area populated.

---
 arch/arm64/mm/kasan_init.c | 23 ++++++++++++++++++-----

 1 file changed, 18 insertions(+), 5 deletions(-)
arch/arm64/mm/kasan_init.c +4 -0
index d8e66c78440e..39b218a64279 100644
@@ -214,6 +214,7 @@ static void __init kasan_init_shadow(void)
  {
   u64 kimg_shadow_start, kimg_shadow_end;
   u64 mod_shadow_start, mod_shadow_end;
+ u64 vmalloc_shadow_start, vmalloc_shadow_end;
   phys_addr_t pa_start, pa_end;
   u64 i;
 
@@ -223,6 +224,9 @@ static void __init kasan_init_shadow(void)
   mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
   mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
 
+ vmalloc_shadow_start = (u64)kasan_mem_to_shadow((void *)VMALLOC_START);
+ vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END);
+
   /*
    * We are going to perform proper setup of shadow memory.
    * At first we should unmap early shadow (clear_pgds() call below).

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 248

Quoted 6 lines
arch/arm64/mm/kasan_init.c +1 -2
@@ -241,12 +245,21 @@ static void __init kasan_init_shadow(void)
 
   kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
                              (void *)mod_shadow_start);
- kasan_populate_early_shadow((void *)kimg_shadow_end,
-                            (void *)KASAN_SHADOW_END);
+ if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {

Do we really need yet another CONFIG option for KASAN? What's the use-case for *not* enabling this if you're already enabling one of the KASAN backends?

Quoted 4 lines
Quoted 2 lines

As I know, KASAN_VMALLOC now only supports KASAN_GENERIC and also KASAN_VMALLOC uses more memory to map real shadow memory (1/8 of vmalloc va).

The shadow is allocated dynamically though, isn't it?

Yes, but It's still a cost.

Quoted 9 lines
Quoted 2 lines

There should be someone can enable KASAN_GENERIC but can't use VMALLOC due to memory issue.

That doesn't sound particularly realistic to me. The reason I'm pushing here is because I would _really_ like to move to VMAP stack unconditionally, and that would effectively force KASAN_VMALLOC to be set if KASAN is in use.

So unless there's a really good reason not to do that, please can we make this unconditional for arm64? Pretty please?

I think it's fine since we have a good reason. Also if someone have memory issue in KASAN_VMALLOC, they can use SW_TAG, right?

However the SW_TAG/HW_TAG is not supported VMALLOC yet. So the code would be like

	if (IS_ENABLED(CONFIG_KASAN_GENERIC))
		/* explain the relationship between 
		 * KASAN_GENERIC and KASAN_VMALLOC in arm64
		 * XXX: because we want VMAP stack....
		 */
		kasan_populate_early_shadow((void *)vmalloc_shadow_end,
					    (void *)KASAN_SHADOW_END);
	else {
		kasan_populate_early_shadow((void *)kimg_shadow_end,
					    (void *)KASAN_SHADOW_END);
		if (kimg_shadow_start > mod_shadow_end)
			kasan_populate_early_shadow((void *)mod_shadow_end,
						    (void *)kimg_shadow_start);
	}

and the arch/arm64/Kconfig will add
select KASAN_VMALLOC if KASAN_GENERIC

Is this code same as your thought?

BRs,
Lecopzer

maintainer
#8 lore raw

Re: [PATCH v2 1/4] arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC

Quoted 67 lines · Lecopzer Chen
Quoted 50 lines · Lecopzer Chen
Quoted 48 lines
Quoted 47 lines · Lecopzer Chen
Quoted 36 lines

Linux support KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Like how the MODULES_VADDR does now, just not to early populate the VMALLOC_START between VMALLOC_END.
similarly, the kernel code mapping is now in the VMALLOC area and should keep these area populated.

---
 arch/arm64/mm/kasan_init.c | 23 ++++++++++++++++++-----

 1 file changed, 18 insertions(+), 5 deletions(-)
arch/arm64/mm/kasan_init.c +4 -0
index d8e66c78440e..39b218a64279 100644
@@ -214,6 +214,7 @@ static void __init kasan_init_shadow(void)
  {
   u64 kimg_shadow_start, kimg_shadow_end;
   u64 mod_shadow_start, mod_shadow_end;
+ u64 vmalloc_shadow_start, vmalloc_shadow_end;
   phys_addr_t pa_start, pa_end;
   u64 i;
 
@@ -223,6 +224,9 @@ static void __init kasan_init_shadow(void)
   mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
   mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
 
+ vmalloc_shadow_start = (u64)kasan_mem_to_shadow((void *)VMALLOC_START);
+ vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END);
+
   /*
    * We are going to perform proper setup of shadow memory.
    * At first we should unmap early shadow (clear_pgds() call below).

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 248

Quoted 6 lines
arch/arm64/mm/kasan_init.c +1 -2
@@ -241,12 +245,21 @@ static void __init kasan_init_shadow(void)
 
   kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
                              (void *)mod_shadow_start);
- kasan_populate_early_shadow((void *)kimg_shadow_end,
-                            (void *)KASAN_SHADOW_END);
+ if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {

Do we really need yet another CONFIG option for KASAN? What's the use-case for *not* enabling this if you're already enabling one of the KASAN backends?

Quoted 4 lines
Quoted 2 lines

As I know, KASAN_VMALLOC now only supports KASAN_GENERIC and also KASAN_VMALLOC uses more memory to map real shadow memory (1/8 of vmalloc va).

The shadow is allocated dynamically though, isn't it?

Yes, but It's still a cost.

Quoted 9 lines
Quoted 2 lines

There should be someone can enable KASAN_GENERIC but can't use VMALLOC due to memory issue.

That doesn't sound particularly realistic to me. The reason I'm pushing here is because I would _really_ like to move to VMAP stack unconditionally, and that would effectively force KASAN_VMALLOC to be set if KASAN is in use.

So unless there's a really good reason not to do that, please can we make this unconditional for arm64? Pretty please?

Quoted 8 lines

I think it's fine since we have a good reason. Also if someone have memory issue in KASAN_VMALLOC, they can use SW_TAG, right?

However the SW_TAG/HW_TAG is not supported VMALLOC yet. So the code would be like

if (IS_ENABLED(CONFIG_KASAN_GENERIC))

Just make this CONFIG_KASAN_VMALLOC, since that depends on KASAN_GENERIC.

Quoted 4 lines
		/* explain the relationship between 
		 * KASAN_GENERIC and KASAN_VMALLOC in arm64
		 * XXX: because we want VMAP stack....
		 */

I don't understand the relation with SW_TAGS. The VMAP_STACK dependency is:

	depends on !KASAN || KASAN_HW_TAGS || KASAN_VMALLOC

which doesn't mention SW_TAGS at all. So that seems to imply that SW_TAGS and VMAP_STACK are mutually exclusive :(

Will

_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek

#9 lore raw

Re: [PATCH v2 1/4] arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC

Quoted 80 lines · Will Deacon
Quoted 67 lines · Lecopzer Chen
Quoted 50 lines · Lecopzer Chen
Quoted 48 lines
Quoted 47 lines · Lecopzer Chen
Quoted 36 lines

Linux support KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Like how the MODULES_VADDR does now, just not to early populate the VMALLOC_START between VMALLOC_END.
similarly, the kernel code mapping is now in the VMALLOC area and should keep these area populated.

---
 arch/arm64/mm/kasan_init.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
arch/arm64/mm/kasan_init.c +4 -0
index d8e66c78440e..39b218a64279 100644
@@ -214,6 +214,7 @@ static void __init kasan_init_shadow(void)
 {
  u64 kimg_shadow_start, kimg_shadow_end;
  u64 mod_shadow_start, mod_shadow_end;
+ u64 vmalloc_shadow_start, vmalloc_shadow_end;
  phys_addr_t pa_start, pa_end;
  u64 i;
 
@@ -223,6 +224,9 @@ static void __init kasan_init_shadow(void)
  mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
  mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
 
+ vmalloc_shadow_start = (u64)kasan_mem_to_shadow((void *)VMALLOC_START);
+ vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END);
+
  /*
   * We are going to perform proper setup of shadow memory.
   * At first we should unmap early shadow (clear_pgds() call below).

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 248

Quoted 6 lines
arch/arm64/mm/kasan_init.c +1 -2
@@ -241,12 +245,21 @@ static void __init kasan_init_shadow(void)
 
  kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
                             (void *)mod_shadow_start);
- kasan_populate_early_shadow((void *)kimg_shadow_end,
-                            (void *)KASAN_SHADOW_END);
+ if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {

Do we really need yet another CONFIG option for KASAN? What's the use-case for *not* enabling this if you're already enabling one of the KASAN backends?

Quoted 4 lines
Quoted 2 lines

As I know, KASAN_VMALLOC now only supports KASAN_GENERIC and also KASAN_VMALLOC uses more memory to map real shadow memory (1/8 of vmalloc va).

The shadow is allocated dynamically though, isn't it?

Yes, but It's still a cost.

Quoted 9 lines
Quoted 2 lines

There should be someone can enable KASAN_GENERIC but can't use VMALLOC due to memory issue.

That doesn't sound particularly realistic to me. The reason I'm pushing here is because I would _really_ like to move to VMAP stack unconditionally, and that would effectively force KASAN_VMALLOC to be set if KASAN is in use.

So unless there's a really good reason not to do that, please can we make this unconditional for arm64? Pretty please?

Quoted 8 lines

I think it's fine since we have a good reason. Also if someone have memory issue in KASAN_VMALLOC, they can use SW_TAG, right?

However the SW_TAG/HW_TAG is not supported VMALLOC yet. So the code would be like

if (IS_ENABLED(CONFIG_KASAN_GENERIC))

Just make this CONFIG_KASAN_VMALLOC, since that depends on KASAN_GENERIC.

Quoted 8 lines
Quoted 4 lines

/* explain the relationship between
* KASAN_GENERIC and KASAN_VMALLOC in arm64
* XXX: because we want VMAP stack.... */

I don't understand the relation with SW_TAGS. The VMAP_STACK dependency is:

        depends on !KASAN || KASAN_HW_TAGS || KASAN_VMALLOC

This means that VMAP_STACK can be only enabled if KASAN_HW_TAGS=y or if KASAN_VMALLOC=y for other modes.

Quoted 3 lines

which doesn't mention SW_TAGS at all. So that seems to imply that SW_TAGS and VMAP_STACK are mutually exclusive :(

SW_TAGS doesn't yet have vmalloc support, so it's not compatible with VMAP_STACK. Once vmalloc support is added to SW_TAGS, KASAN_VMALLOC should be allowed to be enabled with SW_TAGS. This series is a step towards having that support, but doesn't implement it. That will be a separate effort.

_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek

maintainer
#10 lore raw

Re: [PATCH v2 1/4] arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC

Quoted 94 lines · Andrey Konovalov
Quoted 80 lines · Will Deacon
Quoted 67 lines · Lecopzer Chen
Quoted 50 lines · Lecopzer Chen
Quoted 48 lines
Quoted 47 lines · Lecopzer Chen
Quoted 36 lines

Linux support KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Like how the MODULES_VADDR does now, just not to early populate the VMALLOC_START between VMALLOC_END.
similarly, the kernel code mapping is now in the VMALLOC area and should keep these area populated.

---
 arch/arm64/mm/kasan_init.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
arch/arm64/mm/kasan_init.c +4 -0
index d8e66c78440e..39b218a64279 100644
@@ -214,6 +214,7 @@ static void __init kasan_init_shadow(void)
 {
  u64 kimg_shadow_start, kimg_shadow_end;
  u64 mod_shadow_start, mod_shadow_end;
+ u64 vmalloc_shadow_start, vmalloc_shadow_end;
  phys_addr_t pa_start, pa_end;
  u64 i;
 
@@ -223,6 +224,9 @@ static void __init kasan_init_shadow(void)
  mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
  mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
 
+ vmalloc_shadow_start = (u64)kasan_mem_to_shadow((void *)VMALLOC_START);
+ vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END);
+
  /*
   * We are going to perform proper setup of shadow memory.
   * At first we should unmap early shadow (clear_pgds() call below).

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 248

Quoted 6 lines
arch/arm64/mm/kasan_init.c +1 -2
@@ -241,12 +245,21 @@ static void __init kasan_init_shadow(void)
 
  kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
                             (void *)mod_shadow_start);
- kasan_populate_early_shadow((void *)kimg_shadow_end,
-                            (void *)KASAN_SHADOW_END);
+ if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {

Do we really need yet another CONFIG option for KASAN? What's the use-case for *not* enabling this if you're already enabling one of the KASAN backends?

Quoted 4 lines
Quoted 2 lines

As I know, KASAN_VMALLOC now only supports KASAN_GENERIC and also KASAN_VMALLOC uses more memory to map real shadow memory (1/8 of vmalloc va).

The shadow is allocated dynamically though, isn't it?

Yes, but It's still a cost.

Quoted 9 lines
Quoted 2 lines

There should be someone can enable KASAN_GENERIC but can't use VMALLOC due to memory issue.

That doesn't sound particularly realistic to me. The reason I'm pushing here is because I would _really_ like to move to VMAP stack unconditionally, and that would effectively force KASAN_VMALLOC to be set if KASAN is in use.

So unless there's a really good reason not to do that, please can we make this unconditional for arm64? Pretty please?

Quoted 8 lines

I think it's fine since we have a good reason. Also if someone have memory issue in KASAN_VMALLOC, they can use SW_TAG, right?

However the SW_TAG/HW_TAG is not supported VMALLOC yet. So the code would be like

if (IS_ENABLED(CONFIG_KASAN_GENERIC))

Just make this CONFIG_KASAN_VMALLOC, since that depends on KASAN_GENERIC.

Quoted 8 lines
Quoted 4 lines

/* explain the relationship between
* KASAN_GENERIC and KASAN_VMALLOC in arm64
* XXX: because we want VMAP stack.... */

I don't understand the relation with SW_TAGS. The VMAP_STACK dependency is:

        depends on !KASAN || KASAN_HW_TAGS || KASAN_VMALLOC

This means that VMAP_STACK can be only enabled if KASAN_HW_TAGS=y or if KASAN_VMALLOC=y for other modes.

Quoted 1 line
Quoted 8 lines
Quoted 2 lines

which doesn't mention SW_TAGS at all. So that seems to imply that SW_TAGS and VMAP_STACK are mutually exclusive :(

SW_TAGS doesn't yet have vmalloc support, so it's not compatible with VMAP_STACK. Once vmalloc support is added to SW_TAGS, KASAN_VMALLOC should be allowed to be enabled with SW_TAGS. This series is a step towards having that support, but doesn't implement it. That will be a separate effort.

Ok, thanks. Then I think we should try to invert the dependency here, if possible, so that the KASAN backends depend on !VMAP_STACK if they don't support it, rather than silently disabling VMAP_STACK when they are selected.

Will

#11 lore raw

Re: [PATCH v2 1/4] arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC

Quoted 105 lines · Will Deacon
Quoted 94 lines · Andrey Konovalov
Quoted 80 lines · Will Deacon
Quoted 67 lines · Lecopzer Chen
Quoted 50 lines · Lecopzer Chen
Quoted 48 lines
Quoted 47 lines · Lecopzer Chen
Quoted 36 lines

Linux support KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Like how the MODULES_VADDR does now, just not to early populate the VMALLOC_START between VMALLOC_END.
similarly, the kernel code mapping is now in the VMALLOC area and should keep these area populated.

---
 arch/arm64/mm/kasan_init.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
arch/arm64/mm/kasan_init.c +4 -0
index d8e66c78440e..39b218a64279 100644
@@ -214,6 +214,7 @@ static void __init kasan_init_shadow(void)
 {
  u64 kimg_shadow_start, kimg_shadow_end;
  u64 mod_shadow_start, mod_shadow_end;
+ u64 vmalloc_shadow_start, vmalloc_shadow_end;
  phys_addr_t pa_start, pa_end;
  u64 i;
 
@@ -223,6 +224,9 @@ static void __init kasan_init_shadow(void)
  mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
  mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
 
+ vmalloc_shadow_start = (u64)kasan_mem_to_shadow((void *)VMALLOC_START);
+ vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END);
+
  /*
   * We are going to perform proper setup of shadow memory.
   * At first we should unmap early shadow (clear_pgds() call below).

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 248

Quoted 6 lines
arch/arm64/mm/kasan_init.c +1 -2
@@ -241,12 +245,21 @@ static void __init kasan_init_shadow(void)
 
  kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
                             (void *)mod_shadow_start);
- kasan_populate_early_shadow((void *)kimg_shadow_end,
-                            (void *)KASAN_SHADOW_END);
+ if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {

Do we really need yet another CONFIG option for KASAN? What's the use-case for *not* enabling this if you're already enabling one of the KASAN backends?

Quoted 4 lines
Quoted 2 lines

As I know, KASAN_VMALLOC now only supports KASAN_GENERIC and also KASAN_VMALLOC uses more memory to map real shadow memory (1/8 of vmalloc va).

The shadow is allocated dynamically though, isn't it?

Yes, but It's still a cost.

Quoted 9 lines
Quoted 2 lines

There should be someone can enable KASAN_GENERIC but can't use VMALLOC due to memory issue.

That doesn't sound particularly realistic to me. The reason I'm pushing here is because I would _really_ like to move to VMAP stack unconditionally, and that would effectively force KASAN_VMALLOC to be set if KASAN is in use.

So unless there's a really good reason not to do that, please can we make this unconditional for arm64? Pretty please?

Quoted 8 lines

I think it's fine since we have a good reason. Also if someone have memory issue in KASAN_VMALLOC, they can use SW_TAG, right?

However the SW_TAG/HW_TAG is not supported VMALLOC yet. So the code would be like

if (IS_ENABLED(CONFIG_KASAN_GENERIC))

Just make this CONFIG_KASAN_VMALLOC, since that depends on KASAN_GENERIC.

Quoted 8 lines
Quoted 4 lines

/* explain the relationship between
* KASAN_GENERIC and KASAN_VMALLOC in arm64
* XXX: because we want VMAP stack.... */

I don't understand the relation with SW_TAGS. The VMAP_STACK dependency is:

        depends on !KASAN || KASAN_HW_TAGS || KASAN_VMALLOC

This means that VMAP_STACK can be only enabled if KASAN_HW_TAGS=y or if KASAN_VMALLOC=y for other modes.

Quoted 1 line
Quoted 8 lines
Quoted 2 lines

which doesn't mention SW_TAGS at all. So that seems to imply that SW_TAGS and VMAP_STACK are mutually exclusive :(

SW_TAGS doesn't yet have vmalloc support, so it's not compatible with VMAP_STACK. Once vmalloc support is added to SW_TAGS, KASAN_VMALLOC should be allowed to be enabled with SW_TAGS. This series is a step towards having that support, but doesn't implement it. That will be a separate effort.

Quoted 4 lines

Ok, thanks. Then I think we should try to invert the dependency here, if possible, so that the KASAN backends depend on !VMAP_STACK if they don't support it, rather than silently disabling VMAP_STACK when they are selected.

SGTM. Not sure if I will get to this in the nearest future, so I filed a bug: https://bugzilla.kernel.org/show_bug.cgi?id=211581

#12 lore raw

Re: [PATCH v2 1/4] arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC

In reply to Will Deacon

Will Deacon <[email protected]> 於 2021年2月6日 週六 上午1:19寫道:

Quoted 73 lines · Lecopzer Chen
Quoted 71 lines
Quoted 49 lines · Lecopzer Chen
Quoted 47 lines
Quoted 47 lines · Lecopzer Chen
Quoted 36 lines

Linux support KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Like how the MODULES_VADDR does now, just not to early populate the VMALLOC_START between VMALLOC_END.
similarly, the kernel code mapping is now in the VMALLOC area and should keep these area populated.

---
 arch/arm64/mm/kasan_init.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
arch/arm64/mm/kasan_init.c +4 -0
index d8e66c78440e..39b218a64279 100644
@@ -214,6 +214,7 @@ static void __init kasan_init_shadow(void)
 {
  u64 kimg_shadow_start, kimg_shadow_end;
  u64 mod_shadow_start, mod_shadow_end;
+ u64 vmalloc_shadow_start, vmalloc_shadow_end;
  phys_addr_t pa_start, pa_end;
  u64 i;
 
@@ -223,6 +224,9 @@ static void __init kasan_init_shadow(void)
  mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
  mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
 
+ vmalloc_shadow_start = (u64)kasan_mem_to_shadow((void *)VMALLOC_START);
+ vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END);
+
  /*
   * We are going to perform proper setup of shadow memory.
   * At first we should unmap early shadow (clear_pgds() call below).

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 248

Quoted 6 lines
arch/arm64/mm/kasan_init.c +1 -2
@@ -241,12 +245,21 @@ static void __init kasan_init_shadow(void)
 
  kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
                             (void *)mod_shadow_start);
- kasan_populate_early_shadow((void *)kimg_shadow_end,
-                            (void *)KASAN_SHADOW_END);
+ if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {

Do we really need yet another CONFIG option for KASAN? What's the use-case for *not* enabling this if you're already enabling one of the KASAN backends?

Quoted 5 lines
Quoted 3 lines

commit message
As I know, KASAN_VMALLOC now only supports KASAN_GENERIC and also KASAN_VMALLOC uses more memory to map real shadow memory (1/8 of vmalloc va).

The shadow is allocated dynamically though, isn't it?

Yes, but It's still a cost.

Quoted 9 lines
Quoted 2 lines

There should be someone can enable KASAN_GENERIC but can't use VMALLOC due to memory issue.

That doesn't sound particularly realistic to me. The reason I'm pushing here is because I would _really_ like to move to VMAP stack unconditionally, and that would effectively force KASAN_VMALLOC to be set if KASAN is in use.

So unless there's a really good reason not to do that, please can we make this unconditional for arm64? Pretty please?

I think it's fine since we have a good reason. Also if someone have memory issue in KASAN_VMALLOC, they can use SW_TAG, right?

Quoted 6 lines
Quoted 4 lines

However the SW_TAG/HW_TAG is not supported VMALLOC yet. So the code would be like

if (IS_ENABLED(CONFIG_KASAN_GENERIC))

Just make this CONFIG_KASAN_VMALLOC, since that depends on KASAN_GENERIC.

OK, this also make sense.
My first thought was that selecting KASAN_GENERIC implies VMALLOC in arm64 is a special case so this need well documented. I'll document this in the commit message of Kconfig patch to avoid messing up the code here.

I'm going to send V3 patch, thanks again for your review.

BRs,
Lecopzer

#13 lore raw

PATCH v2 2/4 arm64: kasan: abstract _text and _end to KERNEL_START/END

In reply to Lecopzer Chen

Arm64 provide defined macro for KERNEL_START and KERNEL_END, thus replace them by the abstration instead of using _text and _end.

---
 arch/arm64/mm/kasan_init.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/mm/kasan_init.c +3 -3
index 39b218a64279..fa8d7ece895d 100644
@@ -218,8 +218,8 @@ static void __init kasan_init_shadow(void)
 	phys_addr_t pa_start, pa_end;
 	u64 i;
 
-	kimg_shadow_start = (u64)kasan_mem_to_shadow(_text) & PAGE_MASK;
-	kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(_end));
+	kimg_shadow_start = (u64)kasan_mem_to_shadow(KERNEL_START) & PAGE_MASK;
+	kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(KERNEL_END));
 
 	mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
 	mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
@@ -241,7 +241,7 @@ static void __init kasan_init_shadow(void)
 	clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
 
 	kasan_map_populate(kimg_shadow_start, kimg_shadow_end,
-			   early_pfn_to_nid(virt_to_pfn(lm_alias(_text))));
+			   early_pfn_to_nid(virt_to_pfn(lm_alias(KERNEL_START))));
 
 	kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
 				   (void *)mod_shadow_start);
Signature · 1 line
2.25.1
maintainer
#14 lore raw

Re: [PATCH v2 2/4] arm64: kasan: abstract _text and _end to KERNEL_START/END

Quoted 24 lines · Lecopzer Chen

Arm64 provide defined macro for KERNEL_START and KERNEL_END, thus replace them by the abstration instead of using _text and _end.

---
 arch/arm64/mm/kasan_init.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/mm/kasan_init.c +2 -2
index 39b218a64279..fa8d7ece895d 100644
@@ -218,8 +218,8 @@ static void __init kasan_init_shadow(void)
 	phys_addr_t pa_start, pa_end;
 	u64 i;
 
-	kimg_shadow_start = (u64)kasan_mem_to_shadow(_text) & PAGE_MASK;
-	kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(_end));
+	kimg_shadow_start = (u64)kasan_mem_to_shadow(KERNEL_START) & PAGE_MASK;
+	kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(KERNEL_END));
 
 	mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
 	mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 244

Quoted 5 lines
arch/arm64/mm/kasan_init.c +1 -1
@@ -241,7 +241,7 @@ static void __init kasan_init_shadow(void)
 	clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
 
 	kasan_map_populate(kimg_shadow_start, kimg_shadow_end,
-			   early_pfn_to_nid(virt_to_pfn(lm_alias(_text))));
+			   early_pfn_to_nid(virt_to_pfn(lm_alias(KERNEL_START))));

To be honest, I think this whole line is pointless. We should be able to pass NUMA_NO_NODE now that we're not abusing the vmemmap() allocator to populate the shadow.

Will

#15 lore raw

Re: [PATCH v2 2/4] arm64: kasan: abstract _text and _end to KERNEL_START/END

Quoted 27 lines · Lecopzer Chen
Quoted 26 lines

Arm64 provide defined macro for KERNEL_START and KERNEL_END, thus replace them by the abstration instead of using _text and _end.

---
 arch/arm64/mm/kasan_init.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/mm/kasan_init.c +2 -2
index 39b218a64279..fa8d7ece895d 100644
@@ -218,8 +218,8 @@ static void __init kasan_init_shadow(void)
 	phys_addr_t pa_start, pa_end;
 	u64 i;
 
-	kimg_shadow_start = (u64)kasan_mem_to_shadow(_text) & PAGE_MASK;
-	kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(_end));
+	kimg_shadow_start = (u64)kasan_mem_to_shadow(KERNEL_START) & PAGE_MASK;
+	kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(KERNEL_END));
 
 	mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
 	mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
@@ -241,7 +241,7 @@ static void __init kasan_init_shadow(void)
 	clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
 
Quoted 7 lines

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 244

Quoted 3 lines
arch/arm64/mm/kasan_init.c +1 -1
 	kasan_map_populate(kimg_shadow_start, kimg_shadow_end,
-			   early_pfn_to_nid(virt_to_pfn(lm_alias(_text))));
+			   early_pfn_to_nid(virt_to_pfn(lm_alias(KERNEL_START))));

To be honest, I think this whole line is pointless. We should be able to pass NUMA_NO_NODE now that we're not abusing the vmemmap() allocator to populate the shadow.

Do we need to fix this in this series? it seems another topic. If not, should this patch be removed in this series?

Thanks,
Lecopzer

maintainer
#16 lore raw

Re: [PATCH v2 2/4] arm64: kasan: abstract _text and _end to KERNEL_START/END

Quoted 35 lines · Lecopzer Chen
Quoted 27 lines · Lecopzer Chen
Quoted 26 lines

Arm64 provide defined macro for KERNEL_START and KERNEL_END, thus replace them by the abstration instead of using _text and _end.

---
 arch/arm64/mm/kasan_init.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/mm/kasan_init.c +2 -2
index 39b218a64279..fa8d7ece895d 100644
@@ -218,8 +218,8 @@ static void __init kasan_init_shadow(void)
 	phys_addr_t pa_start, pa_end;
 	u64 i;
 
-	kimg_shadow_start = (u64)kasan_mem_to_shadow(_text) & PAGE_MASK;
-	kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(_end));
+	kimg_shadow_start = (u64)kasan_mem_to_shadow(KERNEL_START) & PAGE_MASK;
+	kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(KERNEL_END));
 
 	mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
 	mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
@@ -241,7 +241,7 @@ static void __init kasan_init_shadow(void)
 	clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
 
Quoted 7 lines

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 244

Quoted 3 lines
arch/arm64/mm/kasan_init.c +1 -1
 	kasan_map_populate(kimg_shadow_start, kimg_shadow_end,
-			   early_pfn_to_nid(virt_to_pfn(lm_alias(_text))));
+			   early_pfn_to_nid(virt_to_pfn(lm_alias(KERNEL_START))));

To be honest, I think this whole line is pointless. We should be able to pass NUMA_NO_NODE now that we're not abusing the vmemmap() allocator to populate the shadow.

Quoted 2 lines

Do we need to fix this in this series? it seems another topic. If not, should this patch be removed in this series?

Since you're reposting anyway, you may as well include a patch doing that. If you don't, then I will.

Will

#17 lore raw

Re: [PATCH v2 2/4] arm64: kasan: abstract _text and _end to KERNEL_START/END

I think it would be better to leave this for you since I'm not familiar with the relationship
between vmemmap() and NUMA_NO_NODE.

So I would just keep this patch in next version, is this fine with you?

Thanks for your help:)

Lecopzer

Will Deacon <[email protected]> 於 2021年2月4日 週四 下午10:55寫道:

Quoted 44 lines · Lecopzer Chen
Quoted 35 lines
Quoted 27 lines · Lecopzer Chen
Quoted 26 lines

Arm64 provide defined macro for KERNEL_START and KERNEL_END, thus replace them by the abstration instead of using _text and _end.

---
 arch/arm64/mm/kasan_init.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/mm/kasan_init.c +2 -2
index 39b218a64279..fa8d7ece895d 100644
@@ -218,8 +218,8 @@ static void __init kasan_init_shadow(void)
  phys_addr_t pa_start, pa_end;
  u64 i;
 
- kimg_shadow_start = (u64)kasan_mem_to_shadow(_text) & PAGE_MASK;
- kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(_end));
+ kimg_shadow_start = (u64)kasan_mem_to_shadow(KERNEL_START) & PAGE_MASK;
+ kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(KERNEL_END));
 
  mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
  mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
@@ -241,7 +241,7 @@ static void __init kasan_init_shadow(void)
  clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
 
Quoted 7 lines

arch/arm64/mm/kasan_init.c static void __init kasan_init_shadow(void) · line 244

Quoted 3 lines
arch/arm64/mm/kasan_init.c +1 -1
  kasan_map_populate(kimg_shadow_start, kimg_shadow_end,
-                    early_pfn_to_nid(virt_to_pfn(lm_alias(_text))));
+                    early_pfn_to_nid(virt_to_pfn(lm_alias(KERNEL_START))));

To be honest, I think this whole line is pointless. We should be able to pass NUMA_NO_NODE now that we're not abusing the vmemmap() allocator to populate the shadow.

Quoted 2 lines

Do we need to fix this in this series? it seems another topic. If not, should this patch be removed in this series?

Since you're reposting anyway, you may as well include a patch doing that. If you don't, then I will.

Will

maintainer
#18 lore raw

Re: [PATCH v2 2/4] arm64: kasan: abstract _text and _end to KERNEL_START/END

Quoted 5 lines · Lecopzer Chen

I think it would be better to leave this for you since I'm not familiar with the relationship
between vmemmap() and NUMA_NO_NODE.

So I would just keep this patch in next version, is this fine with you?

Yes, ok.

Will

_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek

#19 lore raw

PATCH v2 3/4 arm64: Kconfig: support CONFIG_KASAN_VMALLOC

In reply to Lecopzer Chen

now we can backed shadow memory in vmalloc area,
thus support KASAN_VMALLOC in KASAN_GENERIC mode.

---
 arch/arm64/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/Kconfig +1 -0
index 05e17351e4f3..ba03820402ee 100644
@@ -136,6 +136,7 @@ config ARM64
 	select HAVE_ARCH_JUMP_LABEL
 	select HAVE_ARCH_JUMP_LABEL_RELATIVE
 	select HAVE_ARCH_KASAN if !(ARM64_16K_PAGES && ARM64_VA_BITS_48)
+	select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN
 	select HAVE_ARCH_KASAN_SW_TAGS if HAVE_ARCH_KASAN
 	select HAVE_ARCH_KASAN_HW_TAGS if (HAVE_ARCH_KASAN && ARM64_MTE)
 	select HAVE_ARCH_KGDB
Signature · 1 line
2.25.1
#20 lore raw

PATCH v2 4/4 arm64: kaslr: support randomized module area with KASAN_VMALLOC

In reply to Lecopzer Chen

After KASAN_VMALLOC works in arm64, we can randomize module region into vmalloc area now.

Test:

	VMALLOC area ffffffc010000000 fffffffdf0000000

	before the patch:
		module_alloc_base/end ffffffc008b80000 ffffffc010000000
	after the patch:
		module_alloc_base/end ffffffdcf4bed000 ffffffc010000000

	And the function that insmod some modules is fine.
---
 arch/arm64/kernel/kaslr.c  | 18 ++++++++++--------
 arch/arm64/kernel/module.c | 16 +++++++++-------
 2 files changed, 19 insertions(+), 15 deletions(-)
arch/arm64/kernel/kaslr.c +10 -8
index 1c74c45b9494..a2858058e724 100644
@@ -161,15 +161,17 @@ u64 __init kaslr_early_init(u64 dt_phys)
 	/* use the top 16 bits to randomize the linear region */
 	memstart_offset_seed = seed >> 48;
 
-	if (IS_ENABLED(CONFIG_KASAN_GENERIC) ||
-	    IS_ENABLED(CONFIG_KASAN_SW_TAGS))
+	if (!IS_ENABLED(CONFIG_KASAN_VMALLOC) &&
+	    (IS_ENABLED(CONFIG_KASAN_GENERIC) ||
+	     IS_ENABLED(CONFIG_KASAN_SW_TAGS)))
 		/*
-		 * KASAN does not expect the module region to intersect the
-		 * vmalloc region, since shadow memory is allocated for each
-		 * module at load time, whereas the vmalloc region is shadowed
-		 * by KASAN zero pages. So keep modules out of the vmalloc
-		 * region if KASAN is enabled, and put the kernel well within
-		 * 4 GB of the module region.
+		 * KASAN without KASAN_VMALLOC does not expect the module region
+		 * to intersect the vmalloc region, since shadow memory is
+		 * allocated for each module at load time, whereas the vmalloc
+		 * region is shadowed by KASAN zero pages. So keep modules
+		 * out of the vmalloc region if KASAN is enabled without
+		 * KASAN_VMALLOC, and put the kernel well within 4 GB of the
+		 * module region.
 		 */
 		return offset % SZ_2G;
 
arch/arm64/kernel/module.c +9 -7
index fe21e0f06492..b5ec010c481f 100644
@@ -40,14 +40,16 @@ void *module_alloc(unsigned long size)
 				NUMA_NO_NODE, __builtin_return_address(0));
 
 	if (!p && IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
-	    !IS_ENABLED(CONFIG_KASAN_GENERIC) &&
-	    !IS_ENABLED(CONFIG_KASAN_SW_TAGS))
+	    (IS_ENABLED(CONFIG_KASAN_VMALLOC) ||
+	     (!IS_ENABLED(CONFIG_KASAN_GENERIC) &&
+	      !IS_ENABLED(CONFIG_KASAN_SW_TAGS))))
 		/*
-		 * KASAN can only deal with module allocations being served
-		 * from the reserved module region, since the remainder of
-		 * the vmalloc region is already backed by zero shadow pages,
-		 * and punching holes into it is non-trivial. Since the module
-		 * region is not randomized when KASAN is enabled, it is even
+		 * KASAN without KASAN_VMALLOC can only deal with module
+		 * allocations being served from the reserved module region,
+		 * since the remainder of the vmalloc region is already
+		 * backed by zero shadow pages, and punching holes into it
+		 * is non-trivial. Since the module region is not randomized
+		 * when KASAN is enabled without KASAN_VMALLOC, it is even
 		 * less likely that the module region gets exhausted, so we
 		 * can simply omit this fallback in that case.
 		 */
Signature · 1 line
2.25.1
maintainer
#21 lore raw

Re: [PATCH v2 4/4] arm64: kaslr: support randomized module area with KASAN_VMALLOC

Quoted 25 lines · Lecopzer Chen

After KASAN_VMALLOC works in arm64, we can randomize module region into vmalloc area now.

Test:

	VMALLOC area ffffffc010000000 fffffffdf0000000

	before the patch:
		module_alloc_base/end ffffffc008b80000 ffffffc010000000
	after the patch:
		module_alloc_base/end ffffffdcf4bed000 ffffffc010000000

	And the function that insmod some modules is fine.
---
 arch/arm64/kernel/kaslr.c  | 18 ++++++++++--------
 arch/arm64/kernel/module.c | 16 +++++++++-------
 2 files changed, 19 insertions(+), 15 deletions(-)
arch/arm64/kernel/kaslr.c +0 -0
index 1c74c45b9494..a2858058e724 100644

arch/arm64/kernel/kaslr.c u64 __init kaslr_early_init(u64 dt_phys) · line 165

Quoted 7 lines
arch/arm64/kernel/kaslr.c +2 -2
@@ -161,15 +161,17 @@ u64 __init kaslr_early_init(u64 dt_phys)
 	/* use the top 16 bits to randomize the linear region */
 	memstart_offset_seed = seed >> 48;
 
-	if (IS_ENABLED(CONFIG_KASAN_GENERIC) ||
-	    IS_ENABLED(CONFIG_KASAN_SW_TAGS))
+	if (!IS_ENABLED(CONFIG_KASAN_VMALLOC) &&
+	    (IS_ENABLED(CONFIG_KASAN_GENERIC) ||

CONFIG_KASAN_VMALLOC depends on CONFIG_KASAN_GENERIC so why is this necessary?

Will

#22 lore raw

Re: [PATCH v2 4/4] arm64: kaslr: support randomized module area with KASAN_VMALLOC

Quoted 38 lines · Lecopzer Chen
Quoted 25 lines

After KASAN_VMALLOC works in arm64, we can randomize module region into vmalloc area now.

Test:

	VMALLOC area ffffffc010000000 fffffffdf0000000

	before the patch:
		module_alloc_base/end ffffffc008b80000 ffffffc010000000
	after the patch:
		module_alloc_base/end ffffffdcf4bed000 ffffffc010000000

	And the function that insmod some modules is fine.
---
 arch/arm64/kernel/kaslr.c  | 18 ++++++++++--------
 arch/arm64/kernel/module.c | 16 +++++++++-------
 2 files changed, 19 insertions(+), 15 deletions(-)
arch/arm64/kernel/kaslr.c +0 -0
index 1c74c45b9494..a2858058e724 100644

arch/arm64/kernel/kaslr.c u64 __init kaslr_early_init(u64 dt_phys) · line 165

Quoted 7 lines
arch/arm64/kernel/kaslr.c +2 -2
@@ -161,15 +161,17 @@ u64 __init kaslr_early_init(u64 dt_phys)
 	/* use the top 16 bits to randomize the linear region */
 	memstart_offset_seed = seed >> 48;
 
-	if (IS_ENABLED(CONFIG_KASAN_GENERIC) ||
-	    IS_ENABLED(CONFIG_KASAN_SW_TAGS))
+	if (!IS_ENABLED(CONFIG_KASAN_VMALLOC) &&
+	    (IS_ENABLED(CONFIG_KASAN_GENERIC) ||

CONFIG_KASAN_VMALLOC depends on CONFIG_KASAN_GENERIC so why is this necessary?

Quoted 1 line

Will

CONFIG_KASAN_VMALLOC=y means CONFIG_KASAN_GENERIC=y but CONFIG_KASAN_GENERIC=y doesn't means CONFIG_KASAN_VMALLOC=y

So this if-condition allows only KASAN rather than KASAN + KASAN_VMALLOC enabled.

Please correct me if I'm wrong.

thanks,
Lecopzer

maintainer
#23 lore raw

Re: [PATCH v2 4/4] arm64: kaslr: support randomized module area with KASAN_VMALLOC

Quoted 40 lines · Lecopzer Chen
Quoted 38 lines · Lecopzer Chen
Quoted 25 lines

After KASAN_VMALLOC works in arm64, we can randomize module region into vmalloc area now.

Test:

	VMALLOC area ffffffc010000000 fffffffdf0000000

	before the patch:
		module_alloc_base/end ffffffc008b80000 ffffffc010000000
	after the patch:
		module_alloc_base/end ffffffdcf4bed000 ffffffc010000000

	And the function that insmod some modules is fine.
---
 arch/arm64/kernel/kaslr.c  | 18 ++++++++++--------
 arch/arm64/kernel/module.c | 16 +++++++++-------
 2 files changed, 19 insertions(+), 15 deletions(-)
arch/arm64/kernel/kaslr.c +0 -0
index 1c74c45b9494..a2858058e724 100644

arch/arm64/kernel/kaslr.c u64 __init kaslr_early_init(u64 dt_phys) · line 165

Quoted 7 lines
arch/arm64/kernel/kaslr.c +2 -2
@@ -161,15 +161,17 @@ u64 __init kaslr_early_init(u64 dt_phys)
 	/* use the top 16 bits to randomize the linear region */
 	memstart_offset_seed = seed >> 48;
 
-	if (IS_ENABLED(CONFIG_KASAN_GENERIC) ||
-	    IS_ENABLED(CONFIG_KASAN_SW_TAGS))
+	if (!IS_ENABLED(CONFIG_KASAN_VMALLOC) &&
+	    (IS_ENABLED(CONFIG_KASAN_GENERIC) ||

CONFIG_KASAN_VMALLOC depends on CONFIG_KASAN_GENERIC so why is this necessary?

Quoted 1 line

Will

Quoted 7 lines

CONFIG_KASAN_VMALLOC=y means CONFIG_KASAN_GENERIC=y but CONFIG_KASAN_GENERIC=y doesn't means CONFIG_KASAN_VMALLOC=y

So this if-condition allows only KASAN rather than KASAN + KASAN_VMALLOC enabled.

Please correct me if I'm wrong.

Sorry, you're completely right -- I missed the '!' when I read this initially.

Will

#24 lore raw
In reply to Lecopzer Chen

Dear reviewers and maintainers,

Could we have chance to upstream this in 5.12-rc?

So if these patches have any problem I can fix as soon as possible before next -rc comming.

thanks!

BRs,
Lecopzer

#25 lore raw
In reply to Lecopzer Chen
Quoted 38 lines · Lecopzer Chen

Linux supports KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Acroding to how x86 ported it [1], they early allocated p4d and pgd, but in arm64 I just simulate how KAsan supports MODULES_VADDR in arm64 by not to populate the vmalloc area except for kimg address.

Test environment:
4G and 8G Qemu virt,
39-bit VA + 4k PAGE_SIZE with 3-level page table, test by lib/test_kasan.ko and lib/test_kasan_module.ko

It also works in Kaslr with CONFIG_RANDOMIZE_MODULE_REGION_FULL and randomize module region inside vmalloc area.

[1]: commit 0609ae011deb41c ("x86/kasan: support KASAN_VMALLOC")

v2 -> v1
1. kasan_init.c tweak indent
2. change Kconfig depends only on HAVE_ARCH_KASAN
3. support randomized module region.

v1:
https://lore.kernel.org/lkml/[email protected]/

Lecopzer Chen (4):
arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC
arm64: kasan: abstract _text and _end to KERNEL_START/END
arm64: Kconfig: support CONFIG_KASAN_VMALLOC
arm64: kaslr: support randomized module area with KASAN_VMALLOC

Quoted 9 lines
 arch/arm64/Kconfig         |  1 +
 arch/arm64/kernel/kaslr.c  | 18 ++++++++++--------
 arch/arm64/kernel/module.c | 16 +++++++++-------
 arch/arm64/mm/kasan_init.c | 29 +++++++++++++++++++++--------
 4 files changed, 41 insertions(+), 23 deletions(-)

--
2.25.1

Hi Will,

Could you PTAL at the arm64 changes?

Thanks!

maintainer
#26 lore raw
Quoted 45 lines · Andrey Konovalov
Quoted 44 lines · Lecopzer Chen

Linux supports KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Acroding to how x86 ported it [1], they early allocated p4d and pgd, but in arm64 I just simulate how KAsan supports MODULES_VADDR in arm64 by not to populate the vmalloc area except for kimg address.

Test environment:
4G and 8G Qemu virt,
39-bit VA + 4k PAGE_SIZE with 3-level page table, test by lib/test_kasan.ko and lib/test_kasan_module.ko

It also works in Kaslr with CONFIG_RANDOMIZE_MODULE_REGION_FULL and randomize module region inside vmalloc area.

[1]: commit 0609ae011deb41c ("x86/kasan: support KASAN_VMALLOC")

v2 -> v1
1. kasan_init.c tweak indent
2. change Kconfig depends only on HAVE_ARCH_KASAN
3. support randomized module region.

v1:
https://lore.kernel.org/lkml/[email protected]/

Lecopzer Chen (4):
arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC
arm64: kasan: abstract _text and _end to KERNEL_START/END
arm64: Kconfig: support CONFIG_KASAN_VMALLOC
arm64: kaslr: support randomized module area with KASAN_VMALLOC

 arch/arm64/Kconfig         |  1 +
 arch/arm64/kernel/kaslr.c  | 18 ++++++++++--------
 arch/arm64/kernel/module.c | 16 +++++++++-------
 arch/arm64/mm/kasan_init.c | 29 +++++++++++++++++++++--------
 4 files changed, 41 insertions(+), 23 deletions(-)
Quoted 7 lines
Quoted 3 lines

--
2.25.1

Hi Will,

Could you PTAL at the arm64 changes?

Sorry, wanted to get to this today but I ran out of time in the end. On the list for next week!

Will

Tested-by on the v2 series
#27 lore raw
In reply to Lecopzer Chen
Quoted 29 lines · Lecopzer Chen

Linux supports KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Acroding to how x86 ported it [1], they early allocated p4d and pgd, but in arm64 I just simulate how KAsan supports MODULES_VADDR in arm64 by not to populate the vmalloc area except for kimg address.

Test environment:
4G and 8G Qemu virt,
39-bit VA + 4k PAGE_SIZE with 3-level page table, test by lib/test_kasan.ko and lib/test_kasan_module.ko

It also works in Kaslr with CONFIG_RANDOMIZE_MODULE_REGION_FULL and randomize module region inside vmalloc area.

[1]: commit 0609ae011deb41c ("x86/kasan: support KASAN_VMALLOC")

v2 -> v1
1. kasan_init.c tweak indent
2. change Kconfig depends only on HAVE_ARCH_KASAN
3. support randomized module region.

Quoted 9 lines

v1:
https://lore.kernel.org/lkml/[email protected]/

Lecopzer Chen (4):
arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC
arm64: kasan: abstract _text and _end to KERNEL_START/END
arm64: Kconfig: support CONFIG_KASAN_VMALLOC
arm64: kaslr: support randomized module area with KASAN_VMALLOC

I failed to realize that VMAP_STACK and KASAN are currently mutually exclusive on arm64, and that this series actually fixes that, which is a big improvement, so it would make sense to call that out.

This builds and runs fine for me on a VM running under KVM.

maintainer
#28 lore raw
In reply to Lecopzer Chen
Quoted 6 lines · Lecopzer Chen

Linux supports KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Acroding to how x86 ported it [1], they early allocated p4d and pgd, but in arm64 I just simulate how KAsan supports MODULES_VADDR in arm64 by not to populate the vmalloc area except for kimg address.

The one thing I've failed to grok from your series is how you deal with vmalloc allocations where the shadow overlaps with the shadow which has already been allocated for the kernel image. Please can you explain?

Thanks,

Will

#29 lore raw
Quoted 11 lines · Lecopzer Chen
Quoted 6 lines

Linux supports KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Acroding to how x86 ported it [1], they early allocated p4d and pgd, but in arm64 I just simulate how KAsan supports MODULES_VADDR in arm64 by not to populate the vmalloc area except for kimg address.

The one thing I've failed to grok from your series is how you deal with vmalloc allocations where the shadow overlaps with the shadow which has already been allocated for the kernel image. Please can you explain?

The most key point is we don't map anything in the vmalloc shadow address. So we don't care where the kernel image locate inside vmalloc area.

kasan_map_populate(kimg_shadow_start, kimg_shadow_end,...)

Kernel image was populated with real mapping in its shadow address. I `bypass' the whole shadow of vmalloc area, the only place you can find about vmalloc_shadow is

	kasan_populate_early_shadow((void *)vmalloc_shadow_end,
			(void *)KASAN_SHADOW_END);

	-----------  vmalloc_shadow_start
 |           |
 |           | 
 |           | <= non-mapping
 |           |
 |           |
 |-----------|
 |///////////|<- kimage shadow with page table mapping.
 |-----------|
 |           |
 |           | <= non-mapping
 |           |
 ------------- vmalloc_shadow_end
 |00000000000|
 |00000000000| <= Zero shadow
 |00000000000|

------------- KASAN_SHADOW_END

vmalloc shadow will be mapped 'ondemend', see kasan_populate_vmalloc() in mm/vmalloc.c in detail.
So the shadow of vmalloc will be allocated later if anyone use its va.

BRs,
Lecopzer

maintainer
#30 lore raw
Quoted 40 lines · Lecopzer Chen
Quoted 11 lines · Lecopzer Chen
Quoted 6 lines

Linux supports KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Acroding to how x86 ported it [1], they early allocated p4d and pgd, but in arm64 I just simulate how KAsan supports MODULES_VADDR in arm64 by not to populate the vmalloc area except for kimg address.

The one thing I've failed to grok from your series is how you deal with vmalloc allocations where the shadow overlaps with the shadow which has already been allocated for the kernel image. Please can you explain?

The most key point is we don't map anything in the vmalloc shadow address. So we don't care where the kernel image locate inside vmalloc area.

kasan_map_populate(kimg_shadow_start, kimg_shadow_end,...)

Kernel image was populated with real mapping in its shadow address. I `bypass' the whole shadow of vmalloc area, the only place you can find about vmalloc_shadow is

	kasan_populate_early_shadow((void *)vmalloc_shadow_end,
			(void *)KASAN_SHADOW_END);

	-----------  vmalloc_shadow_start
 |           |
 |           | 
 |           | <= non-mapping
 |           |
 |           |
 |-----------|
 |///////////|<- kimage shadow with page table mapping.
 |-----------|
 |           |
 |           | <= non-mapping
 |           |
 ------------- vmalloc_shadow_end
 |00000000000|
 |00000000000| <= Zero shadow
 |00000000000|
Quoted 5 lines

------------- KASAN_SHADOW_END

vmalloc shadow will be mapped 'ondemend', see kasan_populate_vmalloc() in mm/vmalloc.c in detail.
So the shadow of vmalloc will be allocated later if anyone use its va.

Indeed, but the question I'm asking is what happens when an on-demand shadow allocation from vmalloc overlaps with the shadow that we allocated early for the kernel image?

Sounds like I have to go and read the code...

Will

#31 lore raw
Quoted 48 lines · Lecopzer Chen
Quoted 6 lines
Quoted 6 lines · Lecopzer Chen
Quoted 5 lines

Linux supports KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Acroding to how x86 ported it [1], they early allocated p4d and pgd, but in arm64 I just simulate how KAsan supports MODULES_VADDR in

arm64

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

by not to populate the vmalloc area except for kimg address.

The one thing I've failed to grok from your series is how you deal with vmalloc allocations where the shadow overlaps with the shadow which has already been allocated for the kernel image. Please can you explain?

The most key point is we don't map anything in the vmalloc shadow

address.

Quoted 26 lines

So we don't care where the kernel image locate inside vmalloc area.

kasan_map_populate(kimg_shadow_start, kimg_shadow_end,...)

Kernel image was populated with real mapping in its shadow address. I `bypass' the whole shadow of vmalloc area, the only place you can find about vmalloc_shadow is

      kasan_populate_early_shadow((void *)vmalloc_shadow_end,
                      (void *)KASAN_SHADOW_END);

      -----------  vmalloc_shadow_start
 |           |
 |           |
 |           | <= non-mapping
 |           |
 |           |
 |-----------|
 |///////////|<- kimage shadow with page table mapping.
 |-----------|
 |           |
 |           | <= non-mapping
 |           |
 ------------- vmalloc_shadow_end
 |00000000000|
 |00000000000| <= Zero shadow
 |00000000000|
Quoted 5 lines

------------- KASAN_SHADOW_END

vmalloc shadow will be mapped 'ondemend', see kasan_populate_vmalloc() in mm/vmalloc.c in detail.
So the shadow of vmalloc will be allocated later if anyone use its va.

Quoted 8 lines

Indeed, but the question I'm asking is what happens when an on-demand shadow
allocation from vmalloc overlaps with the shadow that we allocated early for
the kernel image?

Sounds like I have to go and read the code...

oh, sorry I misunderstood your question.

FWIW,
I think this won't happend because this mean vmalloc() provides va which already allocated by kimg, as I know, vmalloc_init() will insert early allocated vma into its vmalloc rb tree

Quoted 1 line

, and this early allocated vma will include kernel image.

After quick review of mm init code,
this early allocated for vma is at map_kernel() in arch/arm64/mm/mmu.c

BRs
Lecopzer

#32 lore raw
In reply to Will Deacon
Quoted 47 lines · Lecopzer Chen
Quoted 40 lines
Quoted 11 lines · Lecopzer Chen
Quoted 6 lines

Linux supports KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Acroding to how x86 ported it [1], they early allocated p4d and pgd, but in arm64 I just simulate how KAsan supports MODULES_VADDR in arm64 by not to populate the vmalloc area except for kimg address.

The one thing I've failed to grok from your series is how you deal with vmalloc allocations where the shadow overlaps with the shadow which has already been allocated for the kernel image. Please can you explain?

The most key point is we don't map anything in the vmalloc shadow address. So we don't care where the kernel image locate inside vmalloc area.

kasan_map_populate(kimg_shadow_start, kimg_shadow_end,...)

Kernel image was populated with real mapping in its shadow address. I `bypass' the whole shadow of vmalloc area, the only place you can find about vmalloc_shadow is

      kasan_populate_early_shadow((void *)vmalloc_shadow_end,
                      (void *)KASAN_SHADOW_END);

      -----------  vmalloc_shadow_start
 |           |
 |           |
 |           | <= non-mapping
 |           |
 |           |
 |-----------|
 |///////////|<- kimage shadow with page table mapping.
 |-----------|
 |           |
 |           | <= non-mapping
 |           |
 ------------- vmalloc_shadow_end
 |00000000000|
 |00000000000| <= Zero shadow
 |00000000000|
Quoted 5 lines

------------- KASAN_SHADOW_END

vmalloc shadow will be mapped 'ondemend', see kasan_populate_vmalloc() in mm/vmalloc.c in detail.
So the shadow of vmalloc will be allocated later if anyone use its va.

Quoted 5 lines

Indeed, but the question I'm asking is what happens when an on-demand shadow allocation from vmalloc overlaps with the shadow that we allocated early for the kernel image?

Sounds like I have to go and read the code...

oh, sorry I misunderstood your question.

FWIW,
I think this won't happend because this mean vmalloc() provides va which already allocated by kimg, as I know, vmalloc_init() will insert early allocated vma into its vmalloc rb tree

, and this early allocated vma will include kernel image.

After quick review of mm init code,
this early allocated for vma is at map_kernel() in arch/arm64/mm/mmu.c

BRs
Lecopzer