KASAN's software tag-based mode needs multiple macros/functions to
handle tag and pointer interactions - to set, retrieve and reset tags
from the top bits of a pointer.
Mimic functions currently used by arm64 but change the tag's position to
bits [60:57] in the pointer.
Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
---
Changelog v4:
- Rewrite __tag_set() without pointless casts and make it more readable.
Changelog v3:
- Reorder functions so that __tag_*() etc are above the
arch_kasan_*() ones.
- Remove CONFIG_KASAN condition from __tag_set()
arch/x86/include/asm/kasan.h | 36 ++++++++++++++++++++++++++++++++++--
1 file changed, 34 insertions(+), 2 deletions(-)
diff --git a/arch/x86/include/asm/kasan.h b/arch/x86/include/asm/kasan.h
index d7e33c7f096b..1963eb2fcff3 100644
--- a/arch/x86/include/asm/kasan.h
+++ b/arch/x86/include/asm/kasan.h
@@ -3,6 +3,8 @@
#define _ASM_X86_KASAN_H
#include <linux/const.h>
+#include <linux/kasan-tags.h>
+#include <linux/types.h>
#define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
#define KASAN_SHADOW_SCALE_SHIFT 3
@@ -24,8 +26,37 @@
KASAN_SHADOW_SCALE_SHIFT)))
#ifndef __ASSEMBLER__
+#include <linux/bitops.h>
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+
+#ifdef CONFIG_KASAN_SW_TAGS
+
+#define __tag_shifted(tag) FIELD_PREP(GENMASK_ULL(60, 57), tag)
+#define __tag_reset(addr) (sign_extend64((u64)(addr), 56))
+#define __tag_get(addr) ((u8)FIELD_GET(GENMASK_ULL(60, 57), (u64)addr))
+#else
+#define __tag_shifted(tag) 0UL
+#define __tag_reset(addr) (addr)
+#define __tag_get(addr) 0
+#endif /* CONFIG_KASAN_SW_TAGS */
+
+static inline void *__tag_set(const void *__addr, u8 tag)
+{
+ u64 addr = (u64)__addr;
+
+ addr &= ~__tag_shifted(KASAN_TAG_MASK);
+ addr |= __tag_shifted(tag);
+
+ return (void *)addr;
+}
+
+#define arch_kasan_set_tag(addr, tag) __tag_set(addr, tag)
+#define arch_kasan_reset_tag(addr) __tag_reset(addr)
+#define arch_kasan_get_tag(addr) __tag_get(addr)
#ifdef CONFIG_KASAN
+
void __init kasan_early_init(void);
void __init kasan_init(void);
void __init kasan_populate_shadow_for_vaddr(void *va, size_t size, int nid);
@@ -34,8 +65,9 @@ static inline void kasan_early_init(void) { }
static inline void kasan_init(void) { }
static inline void kasan_populate_shadow_for_vaddr(void *va, size_t size,
int nid) { }
-#endif
-#endif
+#endif /* CONFIG_KASAN */
+
+#endif /* __ASSEMBLER__ */
#endif
--
2.50.1
On 8/25/25 10:24 PM, Maciej Wieczor-Retman wrote: > +static inline void *__tag_set(const void *__addr, u8 tag) > +{ > + u64 addr = (u64)__addr; > + > + addr &= ~__tag_shifted(KASAN_TAG_MASK); > + addr |= __tag_shifted(tag); > + > + return (void *)addr; > +} > + This requires some ifdef magic to avoid getting this into vdso32 image build process, otherwise we'll get this warning: CC arch/x86/entry/vdso/vdso32/vclock_gettime.o In file included from ../arch/x86/include/asm/page.h:10, from ../arch/x86/include/asm/processor.h:20, from ../arch/x86/include/asm/timex.h:5, from ../include/linux/timex.h:67, from ../include/linux/time32.h:13, from ../include/linux/time.h:60, from ../arch/x86/entry/vdso/vdso32/../vclock_gettime.c:11, from ../arch/x86/entry/vdso/vdso32/vclock_gettime.c:4: ../arch/x86/include/asm/kasan.h: In function ‘__tag_set’: ../arch/x86/include/asm/kasan.h:81:20: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] 81 | u64 addr = (u64)__addr; | ^ ../arch/x86/include/asm/kasan.h:86:16: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] 86 | return (void *)addr; | ^
On 2025-09-18 at 17:52:39 +0200, Andrey Ryabinin wrote: > >On 8/25/25 10:24 PM, Maciej Wieczor-Retman wrote: > >> +static inline void *__tag_set(const void *__addr, u8 tag) >> +{ >> + u64 addr = (u64)__addr; >> + >> + addr &= ~__tag_shifted(KASAN_TAG_MASK); >> + addr |= __tag_shifted(tag); >> + >> + return (void *)addr; >> +} >> + > > >This requires some ifdef magic to avoid getting this into vdso32 image build process, >otherwise we'll get this warning: > >CC arch/x86/entry/vdso/vdso32/vclock_gettime.o >In file included from ../arch/x86/include/asm/page.h:10, > from ../arch/x86/include/asm/processor.h:20, > from ../arch/x86/include/asm/timex.h:5, > from ../include/linux/timex.h:67, > from ../include/linux/time32.h:13, > from ../include/linux/time.h:60, > from ../arch/x86/entry/vdso/vdso32/../vclock_gettime.c:11, > from ../arch/x86/entry/vdso/vdso32/vclock_gettime.c:4: >../arch/x86/include/asm/kasan.h: In function ‘__tag_set’: >../arch/x86/include/asm/kasan.h:81:20: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] > 81 | u64 addr = (u64)__addr; > | ^ >../arch/x86/include/asm/kasan.h:86:16: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] > 86 | return (void *)addr; > | ^ > Thanks for noticing that, I'll fix it :) -- Kind regards Maciej Wieczór-Retman
On Mon, Aug 25, 2025 at 10:27 PM Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com> wrote: > > KASAN's software tag-based mode needs multiple macros/functions to > handle tag and pointer interactions - to set, retrieve and reset tags > from the top bits of a pointer. > > Mimic functions currently used by arm64 but change the tag's position to > bits [60:57] in the pointer. > > Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com> > --- > Changelog v4: > - Rewrite __tag_set() without pointless casts and make it more readable. > > Changelog v3: > - Reorder functions so that __tag_*() etc are above the > arch_kasan_*() ones. > - Remove CONFIG_KASAN condition from __tag_set() > > arch/x86/include/asm/kasan.h | 36 ++++++++++++++++++++++++++++++++++-- > 1 file changed, 34 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/include/asm/kasan.h b/arch/x86/include/asm/kasan.h > index d7e33c7f096b..1963eb2fcff3 100644 > --- a/arch/x86/include/asm/kasan.h > +++ b/arch/x86/include/asm/kasan.h > @@ -3,6 +3,8 @@ > #define _ASM_X86_KASAN_H > > #include <linux/const.h> > +#include <linux/kasan-tags.h> > +#include <linux/types.h> > #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL) > #define KASAN_SHADOW_SCALE_SHIFT 3 > > @@ -24,8 +26,37 @@ > KASAN_SHADOW_SCALE_SHIFT))) > > #ifndef __ASSEMBLER__ > +#include <linux/bitops.h> > +#include <linux/bitfield.h> > +#include <linux/bits.h> > + > +#ifdef CONFIG_KASAN_SW_TAGS > + Nit: can remove this empty line. > +#define __tag_shifted(tag) FIELD_PREP(GENMASK_ULL(60, 57), tag) > +#define __tag_reset(addr) (sign_extend64((u64)(addr), 56)) > +#define __tag_get(addr) ((u8)FIELD_GET(GENMASK_ULL(60, 57), (u64)addr)) > +#else > +#define __tag_shifted(tag) 0UL > +#define __tag_reset(addr) (addr) > +#define __tag_get(addr) 0 > +#endif /* CONFIG_KASAN_SW_TAGS */ > + > +static inline void *__tag_set(const void *__addr, u8 tag) > +{ > + u64 addr = (u64)__addr; > + > + addr &= ~__tag_shifted(KASAN_TAG_MASK); > + addr |= __tag_shifted(tag); > + > + return (void *)addr; > +} > + > +#define arch_kasan_set_tag(addr, tag) __tag_set(addr, tag) > +#define arch_kasan_reset_tag(addr) __tag_reset(addr) > +#define arch_kasan_get_tag(addr) __tag_get(addr) > > #ifdef CONFIG_KASAN > + > void __init kasan_early_init(void); > void __init kasan_init(void); > void __init kasan_populate_shadow_for_vaddr(void *va, size_t size, int nid); > @@ -34,8 +65,9 @@ static inline void kasan_early_init(void) { } > static inline void kasan_init(void) { } > static inline void kasan_populate_shadow_for_vaddr(void *va, size_t size, > int nid) { } > -#endif > > -#endif > +#endif /* CONFIG_KASAN */ > + > +#endif /* __ASSEMBLER__ */ > > #endif > -- > 2.50.1 > Acked-by: Andrey Konovalov <andreyknvl@gmail.com>
On 2025-09-06 at 19:17:43 +0200, Andrey Konovalov wrote: >On Mon, Aug 25, 2025 at 10:27 PM Maciej Wieczor-Retman ><maciej.wieczor-retman@intel.com> wrote: >> >> KASAN's software tag-based mode needs multiple macros/functions to >> handle tag and pointer interactions - to set, retrieve and reset tags >> from the top bits of a pointer. >> >> Mimic functions currently used by arm64 but change the tag's position to >> bits [60:57] in the pointer. >> >> Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com> >> --- >> Changelog v4: >> - Rewrite __tag_set() without pointless casts and make it more readable. >> >> Changelog v3: >> - Reorder functions so that __tag_*() etc are above the >> arch_kasan_*() ones. >> - Remove CONFIG_KASAN condition from __tag_set() >> >> arch/x86/include/asm/kasan.h | 36 ++++++++++++++++++++++++++++++++++-- >> 1 file changed, 34 insertions(+), 2 deletions(-) >> >> diff --git a/arch/x86/include/asm/kasan.h b/arch/x86/include/asm/kasan.h >> index d7e33c7f096b..1963eb2fcff3 100644 >> --- a/arch/x86/include/asm/kasan.h >> +++ b/arch/x86/include/asm/kasan.h >> @@ -3,6 +3,8 @@ >> #define _ASM_X86_KASAN_H >> >> #include <linux/const.h> >> +#include <linux/kasan-tags.h> >> +#include <linux/types.h> >> #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL) >> #define KASAN_SHADOW_SCALE_SHIFT 3 >> >> @@ -24,8 +26,37 @@ >> KASAN_SHADOW_SCALE_SHIFT))) >> >> #ifndef __ASSEMBLER__ >> +#include <linux/bitops.h> >> +#include <linux/bitfield.h> >> +#include <linux/bits.h> >> + >> +#ifdef CONFIG_KASAN_SW_TAGS >> + > >Nit: can remove this empty line. Sure, will do, thanks. > >> +#define __tag_shifted(tag) FIELD_PREP(GENMASK_ULL(60, 57), tag) >> +#define __tag_reset(addr) (sign_extend64((u64)(addr), 56)) >> +#define __tag_get(addr) ((u8)FIELD_GET(GENMASK_ULL(60, 57), (u64)addr)) >> +#else >> +#define __tag_shifted(tag) 0UL >> +#define __tag_reset(addr) (addr) >> +#define __tag_get(addr) 0 >> +#endif /* CONFIG_KASAN_SW_TAGS */ >> + >> +static inline void *__tag_set(const void *__addr, u8 tag) >> +{ >> + u64 addr = (u64)__addr; >> + >> + addr &= ~__tag_shifted(KASAN_TAG_MASK); >> + addr |= __tag_shifted(tag); >> + >> + return (void *)addr; >> +} >> + >> +#define arch_kasan_set_tag(addr, tag) __tag_set(addr, tag) >> +#define arch_kasan_reset_tag(addr) __tag_reset(addr) >> +#define arch_kasan_get_tag(addr) __tag_get(addr) >> >> #ifdef CONFIG_KASAN >> + >> void __init kasan_early_init(void); >> void __init kasan_init(void); >> void __init kasan_populate_shadow_for_vaddr(void *va, size_t size, int nid); >> @@ -34,8 +65,9 @@ static inline void kasan_early_init(void) { } >> static inline void kasan_init(void) { } >> static inline void kasan_populate_shadow_for_vaddr(void *va, size_t size, >> int nid) { } >> -#endif >> >> -#endif >> +#endif /* CONFIG_KASAN */ >> + >> +#endif /* __ASSEMBLER__ */ >> >> #endif >> -- >> 2.50.1 >> > >Acked-by: Andrey Konovalov <andreyknvl@gmail.com> -- Kind regards Maciej Wieczór-Retman
© 2016 - 2025 Red Hat, Inc.