Re: [PATCH v9 05/13] x86/mm: Reset tag for virtual to physical address conversions

From: Dave Hansen

Date: Wed Feb 25 2026 - 09:49:25 EST


On 2/25/26 00:17, Maciej Wieczor-Retman wrote:
> On 2026-02-23 at 12:33:01 -0800, Dave Hansen wrote:
>>> #ifdef CONFIG_X86_64
>>> #include <asm/page_64.h>
>>> @@ -65,6 +66,13 @@ static inline void copy_user_page(void *to, void *from, unsigned long vaddr,
>>> * virt_to_page(kaddr) returns a valid pointer if and only if
>>> * virt_addr_valid(kaddr) returns true.
>>> */
>>> +
>>> +#ifdef CONFIG_KASAN_SW_TAGS
>>> +#define page_to_virt(x) ({ \
>>> + void *__addr = __va(page_to_pfn((struct page *)x) << PAGE_SHIFT); \
>>> + __tag_set(__addr, page_kasan_tag(x)); \
>>> +})
>>> +#endif
>>
>> Can we pretty please keep this in arch-independent code?
>>
>> The idea of tags is not x86-specific and I can almost guarantee that x86
>> won't be the last one needing this.
>
> Do you mean adding a KASAN oriented page_to_virt() that's arch independent? I'd
> guess next to the regular one, in include/linux/mm.h?

Close.

There should be one and one only arch-independent page_to_virt() that
uses __tag_set(). Then, since __tag_set() gets compiled out when KASAN
is off, you get to keep a single page_to_virt() implementation that
compiles down to the original implementation when KASAN is off.

BTW, you can get __tag_set() down to a single implementation too:

static inline void *__tag_set(const void *__addr, u8 tag)
{
u64 addr = (u64)__addr;

if (IS_ENABLED(CONFIG_KASAN_SW_TAGS)) {
addr &= ~__tag_shifted(KASAN_TAG_BITS_MASK);
addr |= __tag_shifted(tag & KASAN_TAG_BITS_MASK);
}

return (void *)addr;
}

That shifts the actual #ifdeffery all the way back to the constants. At
this level it doesn't really matter much though. Just do whatever is
easy to read.