On Tue, Feb 18, 2025 at 9:18 AM Maciej Wieczor-Retman <maciej.wieczor-retman@xxxxxxxxx> wrote: > > KASAN's software tag-based mode needs multiple macros/functions to > handle tag and pointer interactions - mainly to set and retrieve 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@xxxxxxxxx> > --- > arch/x86/include/asm/kasan.h | 32 ++++++++++++++++++++++++++++++-- > 1 file changed, 30 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/include/asm/kasan.h b/arch/x86/include/asm/kasan.h > index de75306b932e..8829337a75fa 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,33 @@ > KASAN_SHADOW_SCALE_SHIFT))) > > #ifndef __ASSEMBLY__ > +#include <linux/bitops.h> > +#include <linux/bitfield.h> > +#include <linux/bits.h> > + > +#define arch_kasan_set_tag(addr, tag) __tag_set(addr, tag) But __tag_set is defined below. I think these need to be reordered. > +#define arch_kasan_reset_tag(addr) __tag_reset(addr) > +#define arch_kasan_get_tag(addr) __tag_get(addr) > + > +#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 */ > > #ifdef CONFIG_KASAN > + > +static inline const void *__tag_set(const void *addr, u8 tag) A bit weird that __tag_set is defined under CONFIG_KASAN: CONFIG_KASAN_SW_TAGS (or no condition, like on arm64) would make more sense. > +{ > + u64 __addr = (u64)addr & ~__tag_shifted(KASAN_TAG_KERNEL); > + return (const void *)(__addr | __tag_shifted(tag)); > +} > + > 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 +61,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 /* __ASSEMBLY__ */ > > #endif > -- > 2.47.1 >