Skip to contentSkip to conversation

kasan: vmalloc: Fix incorrect tag assignment with multiple vm_structs

PATCH In review Maciej Wieczor-Retman 4 participants 12 messages Dec 2, 2025 – Dec 4, 2025

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

v2 cover kasan: vmalloc: Fix incorrect tag assignment with multiple vm_structs

A KASAN tag mismatch, possibly resulting in a kernel panic, can be observed on systems with a tag-based KASAN enabled and with multiple NUMA nodes. Initially it was only noticed on x86 [1] but later a similar issue was also reported on arm64 [2].

Specifically the problem is related to how vm_structs interact with pcpu_chunks - both when they are allocated, assigned and when pcpu_chunk addresses are derived.

When vm_structs are allocated they are tagged if vmalloc support is enabled along the KASAN mode. Later when first pcpu chunk is allocated it gets its 'base_addr' field set to the first allocated vm_struct. With that it inherits that vm_struct's tag.

When pcpu_chunk addresses are later derived (by pcpu_chunk_addr(), for example in pcpu_alloc_noprof()) the base_addr field is used and offsets are added to it. If the initial conditions are satisfied then some of the offsets will point into memory allocated with a different vm_struct. So while the lower bits will get accurately derived the tag bits in the top of the pointer won't match the shadow memory contents.

The solution (proposed at v2 of the x86 KASAN series [3]) is to tag the vm_structs the same when allocating them for the per cpu allocator (in pcpu_get_vm_areas()).

Originally these patches were part of the x86 KASAN series [4].

The series is based on 6.18.

[1] https://lore.kernel.org/all/e7e04692866d02e6d3b32bb43b998e5d17092ba4.1738686764.git.maciej.wieczor-retman@intel.com/
[2] https://lore.kernel.org/all/aMUrW1Znp1GEj7St@MiWiFi-R3L-srv/
[3] https://lore.kernel.org/all/CAPAsAGxDRv_uFeMYu9TwhBVWHCCtkSxoWY4xmFB_vowMbi8raw@mail.gmail.com/
[4] https://lore.kernel.org/all/[email protected]/

Changes v2:
- Redid the patches since last version wasn't an actual refactor as the patch promised.
- Also fixed multiple mistakes and retested everything.

Maciej Wieczor-Retman (2):
kasan: Refactor pcpu kasan vmalloc unpoison
kasan: Unpoison vms[area] addresses with a common tag

 include/linux/kasan.h | 16 +++++++++++++---
 mm/kasan/common.c     | 18 ++++++++++++++++++
 mm/kasan/hw_tags.c    | 21 ++++++++++++++++++---
 mm/kasan/shadow.c     | 25 ++++++++++++++++++++++---
 mm/vmalloc.c          |  4 +---
 5 files changed, 72 insertions(+), 12 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v2 1/2 kasan: Refactor pcpu kasan vmalloc unpoison

From: Maciej Wieczor-Retman <[email protected]>

A KASAN tag mismatch, possibly causing a kernel panic, can be observed on systems with a tag-based KASAN enabled and with multiple NUMA nodes. It was reported on arm64 and reproduced on x86. It can be explained in the following points:

1. There can be more than one virtual memory chunk.
2. Chunk's base address has a tag.
3. The base address points at the first chunk and thus inherits the tag of the first chunk.
4. The subsequent chunks will be accessed with the tag from the first chunk.
5. Thus, the subsequent chunks need to have their tag set to match that of the first chunk.

Refactor code by reusing __kasan_unpoison_vmalloc in a new helper in preparation for the actual fix.

Changelog v1 (after splitting of from the KASAN series):
- Rewrite first paragraph of the patch message to point at the user impact of the issue.
- Move helper to common.c so it can be compiled in all KASAN modes.

Fixes 1d96320f8d53 ("kasan, vmalloc: add vmalloc tagging for SW_TAGS")
Cc <[email protected]> # 6.1+

---
Changelog v2:
- Redo the whole patch so it's an actual refactor.

 include/linux/kasan.h | 16 +++++++++++++---
 mm/kasan/common.c     | 17 +++++++++++++++++
 mm/kasan/hw_tags.c    | 15 +++++++++++++--
 mm/kasan/shadow.c     | 16 ++++++++++++++--
 mm/vmalloc.c          |  4 +---
 5 files changed, 58 insertions(+), 10 deletions(-)
include/linux/kasan.h +13 -3
index d12e1a5f5a9a..4a3d3dba9764 100644
@@ -595,14 +595,14 @@ static inline void kasan_release_vmalloc(unsigned long start,
 
 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
 
-void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
-			       kasan_vmalloc_flags_t flags);
+void *__kasan_random_unpoison_vmalloc(const void *start, unsigned long size,
+				      kasan_vmalloc_flags_t flags);
 static __always_inline void *kasan_unpoison_vmalloc(const void *start,
 						unsigned long size,
 						kasan_vmalloc_flags_t flags)
 {
 	if (kasan_enabled())
-		return __kasan_unpoison_vmalloc(start, size, flags);
+		return __kasan_random_unpoison_vmalloc(start, size, flags);
 	return (void *)start;
 }
 
@@ -614,6 +614,11 @@ static __always_inline void kasan_poison_vmalloc(const void *start,
 		__kasan_poison_vmalloc(start, size);
 }
 
+void *__kasan_unpoison_vmap_areas(void *addr, unsigned long size,
+				  kasan_vmalloc_flags_t flags, u8 tag);
+void kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms,
+			       kasan_vmalloc_flags_t flags);
+
 #else /* CONFIG_KASAN_VMALLOC */
 
 static inline void kasan_populate_early_vm_area_shadow(void *start,
@@ -638,6 +643,11 @@ static inline void *kasan_unpoison_vmalloc(const void *start,
 static inline void kasan_poison_vmalloc(const void *start, unsigned long size)
 { }
 
+static __always_inline void
+kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms,
+			  kasan_vmalloc_flags_t flags)
+{ }
+
 #endif /* CONFIG_KASAN_VMALLOC */
 
 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
mm/kasan/common.c +17 -0
index d4c14359feaf..7884ea7d13f9 100644
@@ -28,6 +28,7 @@
 #include <linux/string.h>
 #include <linux/types.h>
 #include <linux/bug.h>
+#include <linux/vmalloc.h>
 
 #include "kasan.h"
 #include "../slab.h"
@@ -582,3 +583,19 @@ bool __kasan_check_byte(const void *address, unsigned long ip)
 	}
 	return true;
 }
+
+#ifdef CONFIG_KASAN_VMALLOC
+void kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms,
+			       kasan_vmalloc_flags_t flags)
+{
+	unsigned long size;
+	void *addr;
+	int area;
+
+	for (area = 0 ; area < nr_vms ; area++) {
+		size = vms[area]->size;
+		addr = vms[area]->addr;
+		vms[area]->addr = __kasan_unpoison_vmap_areas(addr, size, flags);
+	}
+}
+#endif
mm/kasan/hw_tags.c +13 -2
index 1c373cc4b3fa..4b7936a2bd6f 100644
@@ -316,8 +316,8 @@ static void init_vmalloc_pages(const void *start, unsigned long size)
 	}
 }
 
-void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
-				kasan_vmalloc_flags_t flags)
+static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
+				      kasan_vmalloc_flags_t flags)
 {
 	u8 tag;
 	unsigned long redzone_start, redzone_size;
@@ -387,6 +387,12 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
 	return (void *)start;
 }
 
+void *__kasan_random_unpoison_vmalloc(const void *start, unsigned long size,
+				      kasan_vmalloc_flags_t flags)
+{
+	return __kasan_unpoison_vmalloc(start, size, flags);
+}
+
 void __kasan_poison_vmalloc(const void *start, unsigned long size)
 {
 	/*
@@ -396,6 +402,11 @@ void __kasan_poison_vmalloc(const void *start, unsigned long size)
 	 */
 }
 
+void *__kasan_unpoison_vmap_areas(void *addr, unsigned long size,
+				  kasan_vmalloc_flags_t flags, u8 tag)
+{
+	return __kasan_unpoison_vmalloc(addr, size, flags);
+}
 #endif
 
 void kasan_enable_hw_tags(void)
mm/kasan/shadow.c +14 -2
index 5d2a876035d6..0a8d8bf6e9cf 100644
@@ -624,8 +624,8 @@ void kasan_release_vmalloc(unsigned long start, unsigned long end,
 	}
 }
 
-void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
-			       kasan_vmalloc_flags_t flags)
+static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
+				      kasan_vmalloc_flags_t flags)
 {
 	/*
 	 * Software KASAN modes unpoison both VM_ALLOC and non-VM_ALLOC
@@ -653,6 +653,18 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
 	return (void *)start;
 }
 
+void *__kasan_random_unpoison_vmalloc(const void *start, unsigned long size,
+				      kasan_vmalloc_flags_t flags)
+{
+	return __kasan_unpoison_vmalloc(start, size, flags);
+}
+
+void *__kasan_unpoison_vmap_areas(void *addr, unsigned long size,
+				  kasan_vmalloc_flags_t flags, u8 tag)
+{
+	return __kasan_unpoison_vmalloc(addr, size, flags);
+}
+
 /*
  * Poison the shadow for a vmalloc region. Called as part of the
  * freeing process at the time the region is freed.
mm/vmalloc.c +1 -3
index 798b2ed21e46..32ecdb8cd4b8 100644
@@ -4870,9 +4870,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
 	 * With hardware tag-based KASAN, marking is skipped for
 	 * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc().
 	 */
-	for (area = 0; area < nr_vms; area++)
-		vms[area]->addr = kasan_unpoison_vmalloc(vms[area]->addr,
-				vms[area]->size, KASAN_VMALLOC_PROT_NORMAL);
+	kasan_unpoison_vmap_areas(vms, nr_vms, KASAN_VMALLOC_PROT_NORMAL);
 
 	kfree(vas);
 	return vms;
Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH v2 1/2] kasan: Refactor pcpu kasan vmalloc unpoison

kernel test robot noticed the following build errors:

Hi Maciej,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.18]
[cannot apply to akpm-mm/mm-everything next-20251203]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Maciej-Wieczor-Retman/kasan-Refactor-pcpu-kasan-vmalloc-unpoison/20251202-223111
base:   linus/master
patch link:    https://lore.kernel.org/r/3907c330d802e5b86bfe003485220de972aaac18.1764685296.git.m.wieczorretman%40pm.me
patch subject: [PATCH v2 1/2] kasan: Refactor pcpu kasan vmalloc unpoison
config: x86_64-randconfig-004-20251203 (https://download.01.org/0day-ci/archive/20251203/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251203/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> mm/kasan/common.c:598:66: error: too few arguments to function call, expected 4, have 3
     598 |                 vms[area]->addr = __kasan_unpoison_vmap_areas(addr, size, flags);
         |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~                  ^
   include/linux/kasan.h:617:7: note: '__kasan_unpoison_vmap_areas' declared here
     617 | void *__kasan_unpoison_vmap_areas(void *addr, unsigned long size,
         |       ^                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     618 |                                   kasan_vmalloc_flags_t flags, u8 tag);
         |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error generated.

vim +598 mm/kasan/common.c

586
587 #ifdef CONFIG_KASAN_VMALLOC
588 void kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms,
589 kasan_vmalloc_flags_t flags)
590 {
591 unsigned long size;
592 void *addr;
593 int area;
594
595 for (area = 0 ; area < nr_vms ; area++) {
596 size = vms[area]->size;
597 addr = vms[area]->addr;

 > 598			vms[area]->addr = __kasan_unpoison_vmap_areas(addr, size, flags);
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw

Re: [PATCH v2 1/2] kasan: Refactor pcpu kasan vmalloc unpoison

In reply to Maciej Wieczor-Retman
Quoted 98 lines · Maciej Wieczor-Retman

From: Maciej Wieczor-Retman <[email protected]>

A KASAN tag mismatch, possibly causing a kernel panic, can be observed on systems with a tag-based KASAN enabled and with multiple NUMA nodes. It was reported on arm64 and reproduced on x86. It can be explained in the following points:

1. There can be more than one virtual memory chunk.
2. Chunk's base address has a tag.
3. The base address points at the first chunk and thus inherits the tag of the first chunk.
4. The subsequent chunks will be accessed with the tag from the first chunk.
5. Thus, the subsequent chunks need to have their tag set to match that of the first chunk.

Refactor code by reusing __kasan_unpoison_vmalloc in a new helper in preparation for the actual fix.

Changelog v1 (after splitting of from the KASAN series):
- Rewrite first paragraph of the patch message to point at the user impact of the issue.
- Move helper to common.c so it can be compiled in all KASAN modes.

Fixes 1d96320f8d53 ("kasan, vmalloc: add vmalloc tagging for SW_TAGS")
Cc <[email protected]> # 6.1+

---
Changelog v2:
- Redo the whole patch so it's an actual refactor.

 include/linux/kasan.h | 16 +++++++++++++---
 mm/kasan/common.c     | 17 +++++++++++++++++
 mm/kasan/hw_tags.c    | 15 +++++++++++++--
 mm/kasan/shadow.c     | 16 ++++++++++++++--
 mm/vmalloc.c          |  4 +---
 5 files changed, 58 insertions(+), 10 deletions(-)
include/linux/kasan.h +13 -3
index d12e1a5f5a9a..4a3d3dba9764 100644
@@ -595,14 +595,14 @@ static inline void kasan_release_vmalloc(unsigned long start,
 
 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
 
-void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
-                              kasan_vmalloc_flags_t flags);
+void *__kasan_random_unpoison_vmalloc(const void *start, unsigned long size,
+                                     kasan_vmalloc_flags_t flags);
 static __always_inline void *kasan_unpoison_vmalloc(const void *start,
                                                unsigned long size,
                                                kasan_vmalloc_flags_t flags)
 {
        if (kasan_enabled())
-               return __kasan_unpoison_vmalloc(start, size, flags);
+               return __kasan_random_unpoison_vmalloc(start, size, flags);
        return (void *)start;
 }
 
@@ -614,6 +614,11 @@ static __always_inline void kasan_poison_vmalloc(const void *start,
                __kasan_poison_vmalloc(start, size);
 }
 
+void *__kasan_unpoison_vmap_areas(void *addr, unsigned long size,
+                                 kasan_vmalloc_flags_t flags, u8 tag);
+void kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms,
+                              kasan_vmalloc_flags_t flags);
+
 #else /* CONFIG_KASAN_VMALLOC */
 
 static inline void kasan_populate_early_vm_area_shadow(void *start,
@@ -638,6 +643,11 @@ static inline void *kasan_unpoison_vmalloc(const void *start,
 static inline void kasan_poison_vmalloc(const void *start, unsigned long size)
 { }
 
+static __always_inline void
+kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms,
+                         kasan_vmalloc_flags_t flags)
+{ }
+
 #endif /* CONFIG_KASAN_VMALLOC */
 
 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
mm/kasan/common.c +1 -0
index d4c14359feaf..7884ea7d13f9 100644
@@ -28,6 +28,7 @@
 #include <linux/string.h>
 #include <linux/types.h>
 #include <linux/bug.h>
+#include <linux/vmalloc.h>
 
 #include "kasan.h"
 #include "../slab.h"

mm/kasan/common.c bool __kasan_check_byte(const void *address, unsigned long ip) · line 589

Quoted 7 lines
mm/kasan/common.c +4 -0
@@ -582,3 +583,19 @@ bool __kasan_check_byte(const void *address, unsigned long ip)
        }
        return true;
 }
+
+#ifdef CONFIG_KASAN_VMALLOC
+void kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms,
+                              kasan_vmalloc_flags_t flags)

kasan_unpoison_vmap_areas() needs to be defined in inclunde/linux/kasan.h and call __kasan_unpoison_vmap_areas() when kasan_enabled() == true, similar to the other wrappers.

And check my comment for patch #2: with that, you should not need to add so many new __helpers: just __kasan_unpoison_vmalloc and __kasan_unpoison_vmap_areas should suffice.

Quoted 105 lines
mm/kasan/common.c +12 -0
+{
+       unsigned long size;
+       void *addr;
+       int area;
+
+       for (area = 0 ; area < nr_vms ; area++) {
+               size = vms[area]->size;
+               addr = vms[area]->addr;
+               vms[area]->addr = __kasan_unpoison_vmap_areas(addr, size, flags);
+       }
+}
+#endif
mm/kasan/hw_tags.c +13 -2
index 1c373cc4b3fa..4b7936a2bd6f 100644
@@ -316,8 +316,8 @@ static void init_vmalloc_pages(const void *start, unsigned long size)
        }
 }
 
-void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
-                               kasan_vmalloc_flags_t flags)
+static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
+                                     kasan_vmalloc_flags_t flags)
 {
        u8 tag;
        unsigned long redzone_start, redzone_size;
@@ -387,6 +387,12 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
        return (void *)start;
 }
 
+void *__kasan_random_unpoison_vmalloc(const void *start, unsigned long size,
+                                     kasan_vmalloc_flags_t flags)
+{
+       return __kasan_unpoison_vmalloc(start, size, flags);
+}
+
 void __kasan_poison_vmalloc(const void *start, unsigned long size)
 {
        /*
@@ -396,6 +402,11 @@ void __kasan_poison_vmalloc(const void *start, unsigned long size)
         */
 }
 
+void *__kasan_unpoison_vmap_areas(void *addr, unsigned long size,
+                                 kasan_vmalloc_flags_t flags, u8 tag)
+{
+       return __kasan_unpoison_vmalloc(addr, size, flags);
+}
 #endif
 
 void kasan_enable_hw_tags(void)
mm/kasan/shadow.c +14 -2
index 5d2a876035d6..0a8d8bf6e9cf 100644
@@ -624,8 +624,8 @@ void kasan_release_vmalloc(unsigned long start, unsigned long end,
        }
 }
 
-void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
-                              kasan_vmalloc_flags_t flags)
+static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
+                                     kasan_vmalloc_flags_t flags)
 {
        /*
         * Software KASAN modes unpoison both VM_ALLOC and non-VM_ALLOC
@@ -653,6 +653,18 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
        return (void *)start;
 }
 
+void *__kasan_random_unpoison_vmalloc(const void *start, unsigned long size,
+                                     kasan_vmalloc_flags_t flags)
+{
+       return __kasan_unpoison_vmalloc(start, size, flags);
+}
+
+void *__kasan_unpoison_vmap_areas(void *addr, unsigned long size,
+                                 kasan_vmalloc_flags_t flags, u8 tag)
+{
+       return __kasan_unpoison_vmalloc(addr, size, flags);
+}
+
 /*
  * Poison the shadow for a vmalloc region. Called as part of the
  * freeing process at the time the region is freed.
mm/vmalloc.c +1 -4
index 798b2ed21e46..32ecdb8cd4b8 100644
@@ -4870,9 +4870,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
         * With hardware tag-based KASAN, marking is skipped for
         * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc().
         */
-       for (area = 0; area < nr_vms; area++)
-               vms[area]->addr = kasan_unpoison_vmalloc(vms[area]->addr,
-                               vms[area]->size, KASAN_VMALLOC_PROT_NORMAL);
+       kasan_unpoison_vmap_areas(vms, nr_vms, KASAN_VMALLOC_PROT_NORMAL);
 
        kfree(vas);
        return vms;
--

2.52.0

#4 lore raw

Re: [PATCH v2 1/2] kasan: Refactor pcpu kasan vmalloc unpoison

Quoted 5 lines · Andrey Konovalov
Quoted 3 lines · Maciej Wieczor-Retman

From: Maciej Wieczor-Retman <[email protected]>

...

Quoted 25 lines
Quoted 13 lines
mm/kasan/common.c +1 -0
index d4c14359feaf..7884ea7d13f9 100644
@@ -28,6 +28,7 @@
 #include <linux/string.h>
 #include <linux/types.h>
 #include <linux/bug.h>
+#include <linux/vmalloc.h>
 
 #include "kasan.h"
 #include "../slab.h"

mm/kasan/common.c bool __kasan_check_byte(const void *address, unsigned long ip) · line 589

Quoted 7 lines
mm/kasan/common.c +4 -0
@@ -582,3 +583,19 @@ bool __kasan_check_byte(const void *address, unsigned long ip)
        }
        return true;
 }
+
+#ifdef CONFIG_KASAN_VMALLOC
+void kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms,
+                              kasan_vmalloc_flags_t flags)

kasan_unpoison_vmap_areas() needs to be defined in inclunde/linux/kasan.h and call __kasan_unpoison_vmap_areas() when kasan_enabled() == true, similar to the other wrappers.

Quoted 3 lines

And check my comment for patch #2: with that, you should not need to add so many new __helpers: just __kasan_unpoison_vmalloc and __kasan_unpoison_vmap_areas should suffice.

Okay, I think I see what you mean. I was trying to avoid using __kasan_unpoison_vmalloc() here so that it compiled properly, but that was before I added the ifdef guard. Now there is not reason not to use it here.

I'll make the changes you mentioned.

Kind regards
Maciej Wieczór-Retman

Quoted 13 lines
Quoted 13 lines
mm/kasan/common.c +12 -0
+{
+       unsigned long size;
+       void *addr;
+       int area;
+
+       for (area = 0 ; area < nr_vms ; area++) {
+               size = vms[area]->size;
+               addr = vms[area]->addr;
+               vms[area]->addr = __kasan_unpoison_vmap_areas(addr, size, flags);
+       }
+}
+#endif
#5 lore raw

PATCH v2 2/2 kasan: Unpoison vms[area] addresses with a common tag

In reply to Maciej Wieczor-Retman

From: Maciej Wieczor-Retman <[email protected]>

A KASAN tag mismatch, possibly causing a kernel panic, can be observed on systems with a tag-based KASAN enabled and with multiple NUMA nodes. It was reported on arm64 and reproduced on x86. It can be explained in the following points:

1. There can be more than one virtual memory chunk.
2. Chunk's base address has a tag.
3. The base address points at the first chunk and thus inherits the tag of the first chunk.
4. The subsequent chunks will be accessed with the tag from the first chunk.
5. Thus, the subsequent chunks need to have their tag set to match that of the first chunk.

Use the modified __kasan_unpoison_vmalloc() to pass the tag of the first vm_struct's address when vm_structs are unpoisoned in pcpu_get_vm_areas(). Assigning a common tag resolves the pcpu chunk address mismatch.

Fixes 1d96320f8d53 ("kasan, vmalloc: add vmalloc tagging for SW_TAGS")
Cc <[email protected]> # 6.1+

---
Changelog v2:
- Revise the whole patch to match the fixed refactorization from the first patch.

Changelog v1:
- Rewrite the patch message to point at the user impact of the issue.
- Move helper to common.c so it can be compiled in all KASAN modes.

 mm/kasan/common.c  |  3 ++-
 mm/kasan/hw_tags.c | 12 ++++++++----
 mm/kasan/shadow.c  | 15 +++++++++++----
 3 files changed, 21 insertions(+), 9 deletions(-)
mm/kasan/common.c +2 -1
index 7884ea7d13f9..e5a867a5670b 100644
@@ -591,11 +591,12 @@ void kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms,
 	unsigned long size;
 	void *addr;
 	int area;
+	u8 tag = get_tag(vms[0]->addr);
 
 	for (area = 0 ; area < nr_vms ; area++) {
 		size = vms[area]->size;
 		addr = vms[area]->addr;
-		vms[area]->addr = __kasan_unpoison_vmap_areas(addr, size, flags);
+		vms[area]->addr = __kasan_unpoison_vmap_areas(addr, size, flags, tag);
 	}
 }
 #endif
mm/kasan/hw_tags.c +8 -4
index 4b7936a2bd6f..2a02b898b9d8 100644
@@ -317,7 +317,7 @@ static void init_vmalloc_pages(const void *start, unsigned long size)
 }
 
 static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
-				      kasan_vmalloc_flags_t flags)
+				      kasan_vmalloc_flags_t flags, int unpoison_tag)
 {
 	u8 tag;
 	unsigned long redzone_start, redzone_size;
@@ -361,7 +361,11 @@ static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
 		return (void *)start;
 	}
 
-	tag = kasan_random_tag();
+	if (unpoison_tag < 0)
+		tag = kasan_random_tag();
+	else
+		tag = unpoison_tag;
+
 	start = set_tag(start, tag);
 
 	/* Unpoison and initialize memory up to size. */
@@ -390,7 +394,7 @@ static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
 void *__kasan_random_unpoison_vmalloc(const void *start, unsigned long size,
 				      kasan_vmalloc_flags_t flags)
 {
-	return __kasan_unpoison_vmalloc(start, size, flags);
+	return __kasan_unpoison_vmalloc(start, size, flags, -1);
 }
 
 void __kasan_poison_vmalloc(const void *start, unsigned long size)
@@ -405,7 +409,7 @@ void __kasan_poison_vmalloc(const void *start, unsigned long size)
 void *__kasan_unpoison_vmap_areas(void *addr, unsigned long size,
 				  kasan_vmalloc_flags_t flags, u8 tag)
 {
-	return __kasan_unpoison_vmalloc(addr, size, flags);
+	return __kasan_unpoison_vmalloc(addr, size, flags, tag);
 }
 #endif
 
mm/kasan/shadow.c +11 -4
index 0a8d8bf6e9cf..7a66ffc1d5b3 100644
@@ -625,8 +625,10 @@ void kasan_release_vmalloc(unsigned long start, unsigned long end,
 }
 
 static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
-				      kasan_vmalloc_flags_t flags)
+				      kasan_vmalloc_flags_t flags, int unpoison_tag)
 {
+	u8 tag;
+
 	/*
 	 * Software KASAN modes unpoison both VM_ALLOC and non-VM_ALLOC
 	 * mappings, so the KASAN_VMALLOC_VM_ALLOC flag is ignored.
@@ -648,7 +650,12 @@ static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
 	    !(flags & KASAN_VMALLOC_PROT_NORMAL))
 		return (void *)start;
 
-	start = set_tag(start, kasan_random_tag());
+	if (unpoison_tag < 0)
+		tag = kasan_random_tag();
+	else
+		tag = unpoison_tag;
+
+	start = set_tag(start, tag);
 	kasan_unpoison(start, size, false);
 	return (void *)start;
 }
@@ -656,13 +663,13 @@ static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
 void *__kasan_random_unpoison_vmalloc(const void *start, unsigned long size,
 				      kasan_vmalloc_flags_t flags)
 {
-	return __kasan_unpoison_vmalloc(start, size, flags);
+	return __kasan_unpoison_vmalloc(start, size, flags, -1);
 }
 
 void *__kasan_unpoison_vmap_areas(void *addr, unsigned long size,
 				  kasan_vmalloc_flags_t flags, u8 tag)
 {
-	return __kasan_unpoison_vmalloc(addr, size, flags);
+	return __kasan_unpoison_vmalloc(addr, size, flags, tag);
 }
 
 /*
Signature · 1 line
2.52.0
#6 lore raw

Re: [PATCH v2 2/2] kasan: Unpoison vms[area] addresses with a common tag

Quoted 44 lines · Maciej Wieczor-Retman

From: Maciej Wieczor-Retman <[email protected]>

A KASAN tag mismatch, possibly causing a kernel panic, can be observed on systems with a tag-based KASAN enabled and with multiple NUMA nodes. It was reported on arm64 and reproduced on x86. It can be explained in the following points:

1. There can be more than one virtual memory chunk.
2. Chunk's base address has a tag.
3. The base address points at the first chunk and thus inherits the tag of the first chunk.
4. The subsequent chunks will be accessed with the tag from the first chunk.
5. Thus, the subsequent chunks need to have their tag set to match that of the first chunk.

Use the modified __kasan_unpoison_vmalloc() to pass the tag of the first vm_struct's address when vm_structs are unpoisoned in pcpu_get_vm_areas(). Assigning a common tag resolves the pcpu chunk address mismatch.

Fixes 1d96320f8d53 ("kasan, vmalloc: add vmalloc tagging for SW_TAGS")
Cc <[email protected]> # 6.1+

---
Changelog v2:
- Revise the whole patch to match the fixed refactorization from the first patch.

Changelog v1:
- Rewrite the patch message to point at the user impact of the issue.
- Move helper to common.c so it can be compiled in all KASAN modes.

 mm/kasan/common.c  |  3 ++-
 mm/kasan/hw_tags.c | 12 ++++++++----
 mm/kasan/shadow.c  | 15 +++++++++++----
 3 files changed, 21 insertions(+), 9 deletions(-)
mm/kasan/common.c +0 -0
index 7884ea7d13f9..e5a867a5670b 100644

mm/kasan/common.c void kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms, · line 599

Quoted 10 lines
mm/kasan/common.c +2 -1
@@ -591,11 +591,12 @@ void kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms,
        unsigned long size;
        void *addr;
        int area;
+       u8 tag = get_tag(vms[0]->addr);
 
        for (area = 0 ; area < nr_vms ; area++) {
                size = vms[area]->size;
                addr = vms[area]->addr;
-               vms[area]->addr = __kasan_unpoison_vmap_areas(addr, size, flags);
+               vms[area]->addr = __kasan_unpoison_vmap_areas(addr, size, flags, tag);

I'm thinking what you can do here is:

vms[area]->addr = set_tag(addr, tag); __kasan_unpoison_vmalloc(addr, size, flags | KASAN_VMALLOC_KEEP_TAG);

This is with the assumption that Jiayuan's patch is changed to add KASAN_VMALLOC_KEEP_TAG to kasan_vmalloc_flags_t.

Then you should not need that extra __kasan_random_unpoison_vmalloc helper.

Quoted 97 lines
mm/kasan/common.c +0 -0
        }
 }
 #endif
mm/kasan/hw_tags.c +8 -4
index 4b7936a2bd6f..2a02b898b9d8 100644
@@ -317,7 +317,7 @@ static void init_vmalloc_pages(const void *start, unsigned long size)
 }
 
 static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
-                                     kasan_vmalloc_flags_t flags)
+                                     kasan_vmalloc_flags_t flags, int unpoison_tag)
 {
        u8 tag;
        unsigned long redzone_start, redzone_size;
@@ -361,7 +361,11 @@ static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
                return (void *)start;
        }
 
-       tag = kasan_random_tag();
+       if (unpoison_tag < 0)
+               tag = kasan_random_tag();
+       else
+               tag = unpoison_tag;
+
        start = set_tag(start, tag);
 
        /* Unpoison and initialize memory up to size. */
@@ -390,7 +394,7 @@ static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
 void *__kasan_random_unpoison_vmalloc(const void *start, unsigned long size,
                                      kasan_vmalloc_flags_t flags)
 {
-       return __kasan_unpoison_vmalloc(start, size, flags);
+       return __kasan_unpoison_vmalloc(start, size, flags, -1);
 }
 
 void __kasan_poison_vmalloc(const void *start, unsigned long size)
@@ -405,7 +409,7 @@ void __kasan_poison_vmalloc(const void *start, unsigned long size)
 void *__kasan_unpoison_vmap_areas(void *addr, unsigned long size,
                                  kasan_vmalloc_flags_t flags, u8 tag)
 {
-       return __kasan_unpoison_vmalloc(addr, size, flags);
+       return __kasan_unpoison_vmalloc(addr, size, flags, tag);
 }
 #endif
 
mm/kasan/shadow.c +11 -5
index 0a8d8bf6e9cf..7a66ffc1d5b3 100644
@@ -625,8 +625,10 @@ void kasan_release_vmalloc(unsigned long start, unsigned long end,
 }
 
 static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
-                                     kasan_vmalloc_flags_t flags)
+                                     kasan_vmalloc_flags_t flags, int unpoison_tag)
 {
+       u8 tag;
+
        /*
         * Software KASAN modes unpoison both VM_ALLOC and non-VM_ALLOC
         * mappings, so the KASAN_VMALLOC_VM_ALLOC flag is ignored.
@@ -648,7 +650,12 @@ static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
            !(flags & KASAN_VMALLOC_PROT_NORMAL))
                return (void *)start;
 
-       start = set_tag(start, kasan_random_tag());
+       if (unpoison_tag < 0)
+               tag = kasan_random_tag();
+       else
+               tag = unpoison_tag;
+
+       start = set_tag(start, tag);
        kasan_unpoison(start, size, false);
        return (void *)start;
 }
@@ -656,13 +663,13 @@ static void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
 void *__kasan_random_unpoison_vmalloc(const void *start, unsigned long size,
                                      kasan_vmalloc_flags_t flags)
 {
-       return __kasan_unpoison_vmalloc(start, size, flags);
+       return __kasan_unpoison_vmalloc(start, size, flags, -1);
 }
 
 void *__kasan_unpoison_vmap_areas(void *addr, unsigned long size,
                                  kasan_vmalloc_flags_t flags, u8 tag)
 {
-       return __kasan_unpoison_vmalloc(addr, size, flags);
+       return __kasan_unpoison_vmalloc(addr, size, flags, tag);
 }
 
 /*
--

2.52.0

#7 lore raw

Re: [PATCH v2 2/2] kasan: Unpoison vms[area] addresses with a common tag

Quoted 59 lines · Andrey Konovalov
Quoted 44 lines · Maciej Wieczor-Retman

From: Maciej Wieczor-Retman <[email protected]>

A KASAN tag mismatch, possibly causing a kernel panic, can be observed on systems with a tag-based KASAN enabled and with multiple NUMA nodes. It was reported on arm64 and reproduced on x86. It can be explained in the following points:

1. There can be more than one virtual memory chunk.
2. Chunk's base address has a tag.
3. The base address points at the first chunk and thus inherits the tag of the first chunk.
4. The subsequent chunks will be accessed with the tag from the first chunk.
5. Thus, the subsequent chunks need to have their tag set to match that of the first chunk.

Use the modified __kasan_unpoison_vmalloc() to pass the tag of the first vm_struct's address when vm_structs are unpoisoned in pcpu_get_vm_areas(). Assigning a common tag resolves the pcpu chunk address mismatch.

Fixes 1d96320f8d53 ("kasan, vmalloc: add vmalloc tagging for SW_TAGS")
Cc <[email protected]> # 6.1+

---
Changelog v2:
- Revise the whole patch to match the fixed refactorization from the first patch.

Changelog v1:
- Rewrite the patch message to point at the user impact of the issue.
- Move helper to common.c so it can be compiled in all KASAN modes.

 mm/kasan/common.c  |  3 ++-
 mm/kasan/hw_tags.c | 12 ++++++++----
 mm/kasan/shadow.c  | 15 +++++++++++----
 3 files changed, 21 insertions(+), 9 deletions(-)
mm/kasan/common.c +0 -0
index 7884ea7d13f9..e5a867a5670b 100644

mm/kasan/common.c void kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms, · line 599

Quoted 10 lines
mm/kasan/common.c +2 -1
@@ -591,11 +591,12 @@ void kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms,
        unsigned long size;
        void *addr;
        int area;
+       u8 tag = get_tag(vms[0]->addr);
 
        for (area = 0 ; area < nr_vms ; area++) {
                size = vms[area]->size;
                addr = vms[area]->addr;
-               vms[area]->addr = __kasan_unpoison_vmap_areas(addr, size, flags);
+               vms[area]->addr = __kasan_unpoison_vmap_areas(addr, size, flags, tag);

I'm thinking what you can do here is:

Quoted 2 lines

vms[area]->addr = set_tag(addr, tag); __kasan_unpoison_vmalloc(addr, size, flags | KASAN_VMALLOC_KEEP_TAG);

I noticed that something like this wouldn't work once I started trying to rebase my work onto Jiayuan's. The line:
+ u8 tag = get_tag(vms[0]->addr);
is wrong and should be
+ u8 tag = kasan_random_tag();
I was sure the vms[0]->addr was already tagged (I recall checking this so I'm not sure if something changed or my previous check was wrong) but the problem here is that vms[0]->addr, vms[1]->addr ... were unpoisoned with random addresses, specifically different random addresses. So then later in the pcpu chunk code vms[1] related pointers would get the tag from vms[0]->addr.

So I think we still need a separate way to do __kasan_unpoison_vmalloc with a specific tag.

Quoted 5 lines

This is with the assumption that Jiayuan's patch is changed to add KASAN_VMALLOC_KEEP_TAG to kasan_vmalloc_flags_t.

Then you should not need that extra __kasan_random_unpoison_vmalloc helper.

I already rewrote the patch rebased onto Jiayuan's patch. I was able to ditch the __kasan_random_unpoison_vmalloc but I needed to add __kasan_unpoison_vrealloc - so I can pass the tag of the start pointer to __kasan_unpoison_vmalloc. I was hoping to post it today/tomorrow so Jiayuan can check my changes don't break his solution. I'm just waiting to check it compiles against all the fun kernel configs.

Signature · 2 lines
kind regards
Maciej Wieczór-Retman
#8 lore raw

Re: [PATCH v2 2/2] kasan: Unpoison vms[area] addresses with a common tag

Quoted 7 lines · Maciej Wieczór-Retman
Quoted 5 lines

I'm thinking what you can do here is:

vms[area]->addr = set_tag(addr, tag); __kasan_unpoison_vmalloc(addr, size, flags | KASAN_VMALLOC_KEEP_TAG);

Quoted 5 lines

I noticed that something like this wouldn't work once I started trying to rebase my work onto Jiayuan's. The line:
+ u8 tag = get_tag(vms[0]->addr);
is wrong and should be
+ u8 tag = kasan_random_tag();

Ah, right.

Quoted 9 lines

I was sure the vms[0]->addr was already tagged (I recall checking this so I'm not sure if something changed or my previous check was wrong) but the problem here is that vms[0]->addr, vms[1]->addr ... were unpoisoned with random addresses, specifically different random addresses. So then later in the pcpu chunk code vms[1] related pointers would get the tag from vms[0]->addr.

So I think we still need a separate way to do __kasan_unpoison_vmalloc with a specific tag.

Why?

Assuming KASAN_VMALLOC_KEEP_TAG takes the tag from the pointer, just do:

tag = kasan_random_tag();

for (area = 0; ...) {
    vms[area]->addr = set_tag(vms[area]->addr, tag);
    __kasan_unpoison_vmalloc(vms[area]->addr, vms[area]->size, flags |

KASAN_VMALLOC_KEEP_TAG);
}

Or maybe even better:

vms[0]->addr = __kasan_unpoison_vmalloc(vms[0]->addr, vms[0]->size, flags); tag = get_tag(vms[0]->addr);

for (area = 1; ...) {
    vms[area]->addr = set_tag(vms[area]->addr, tag);
    __kasan_unpoison_vmalloc(vms[area]->addr, vms[area]->size, flags |

KASAN_VMALLOC_KEEP_TAG);
}

This way we won't assign a random tag unless it's actually needed (i.e. when KASAN_VMALLOC_PROT_NORMAL is not provided; assuming we care to support that case).

#9 lore raw

Re: [PATCH v2 2/2] kasan: Unpoison vms[area] addresses with a common tag

Quoted 30 lines · Andrey Konovalov
Quoted 9 lines · Maciej Wieczór-Retman

I was sure the vms[0]->addr was already tagged (I recall checking this so I'm not sure if something changed or my previous check was wrong) but the problem here is that vms[0]->addr, vms[1]->addr ... were unpoisoned with random addresses, specifically different random addresses. So then later in the pcpu chunk code vms[1] related pointers would get the tag from vms[0]->addr.

So I think we still need a separate way to do __kasan_unpoison_vmalloc with a specific tag.

Why?

Assuming KASAN_VMALLOC_KEEP_TAG takes the tag from the pointer, just do:

tag = kasan_random_tag();

for (area = 0; ...) {
   vms[area]->addr = set_tag(vms[area]->addr, tag);
   __kasan_unpoison_vmalloc(vms[area]->addr, vms[area]->size, flags |

KASAN_VMALLOC_KEEP_TAG);
}

Or maybe even better:

vms[0]->addr = __kasan_unpoison_vmalloc(vms[0]->addr, vms[0]->size, flags); tag = get_tag(vms[0]->addr);

for (area = 1; ...) {
   vms[area]->addr = set_tag(vms[area]->addr, tag);
   __kasan_unpoison_vmalloc(vms[area]->addr, vms[area]->size, flags |
Quoted 6 lines

KASAN_VMALLOC_KEEP_TAG);
}

This way we won't assign a random tag unless it's actually needed (i.e. when KASAN_VMALLOC_PROT_NORMAL is not provided; assuming we care to support that case).

Oh, right yes, that would work nicely. I thought putting these behind helpers would end up clean but this is very neat too.

I suppose I'll wait for Jiayuan to update his patch and then I'll make these changes on top of that.

Thanks! :)

Signature · 2 lines
Kind regards
Maciej Wieczór-Retman
maintainer
#10 lore raw
In reply to Maciej Wieczor-Retman
Quoted 21 lines · On Tue, 02 Dec 2025 14:27:56 +0000 Maciej Wieczor-Retman…

A KASAN tag mismatch, possibly resulting in a kernel panic, can be observed on systems with a tag-based KASAN enabled and with multiple NUMA nodes. Initially it was only noticed on x86 [1] but later a similar issue was also reported on arm64 [2].

Specifically the problem is related to how vm_structs interact with pcpu_chunks - both when they are allocated, assigned and when pcpu_chunk addresses are derived.

When vm_structs are allocated they are tagged if vmalloc support is enabled along the KASAN mode. Later when first pcpu chunk is allocated it gets its 'base_addr' field set to the first allocated vm_struct. With that it inherits that vm_struct's tag.

When pcpu_chunk addresses are later derived (by pcpu_chunk_addr(), for example in pcpu_alloc_noprof()) the base_addr field is used and offsets are added to it. If the initial conditions are satisfied then some of the offsets will point into memory allocated with a different vm_struct. So while the lower bits will get accurately derived the tag bits in the top of the pointer won't match the shadow memory contents.

Quoted 7 lines

The solution (proposed at v2 of the x86 KASAN series [3]) is to tag the vm_structs the same when allocating them for the per cpu allocator (in pcpu_get_vm_areas()).

Originally these patches were part of the x86 KASAN series [4].

The series is based on 6.18.

This series overlaps a lot with

https://lkml.kernel.org/r/[email protected]

Please discuss!

Quoted 5 lines

[1] https://lore.kernel.org/all/e7e04692866d02e6d3b32bb43b998e5d17092ba4.1738686764.git.maciej.wieczor-retman@intel.com/
[2] https://lore.kernel.org/all/aMUrW1Znp1GEj7St@MiWiFi-R3L-srv/
[3] https://lore.kernel.org/all/CAPAsAGxDRv_uFeMYu9TwhBVWHCCtkSxoWY4xmFB_vowMbi8raw@mail.gmail.com/
[4] https://lore.kernel.org/all/[email protected]/