From: Maciej Wieczor-Retman <m.wieczorretman@pm.me>
To: akpm@linux-foundation.org,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Andrey Ryabinin <ryabinin.a.a@gmail.com>,
Alexander Potapenko <glider@google.com>,
Andrey Konovalov <andreyknvl@gmail.com>,
Dmitry Vyukov <dvyukov@google.com>,
Vincenzo Frascino <vincenzo.frascino@arm.com>,
David Hildenbrand <david@kernel.org>,
Lorenzo Stoakes <ljs@kernel.org>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>
Cc: m.wieczorretman@pm.me,
Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com,
linux-mm@kvack.org
Subject: [PATCH v11 06/15] kasan: arm64: x86: Make page_to_virt() KASAN aware
Date: Tue, 10 Mar 2026 17:54:09 +0000 [thread overview]
Message-ID: <b51b60f82bbd99cdcd7f6d0d2bac1d5f3bed0fe0.1773164688.git.m.wieczorretman@pm.me> (raw)
In-Reply-To: <cover.1773164688.git.m.wieczorretman@pm.me>
From: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
Special page_to_virt() implementation is needed if an architecture wants
to enable KASAN software tag-based mode.
Make page_to_virt() KASAN aware in arch-independent code so
architectures implementing the software tag-based mode don't have to
define their own implementations anymore. When KASAN is disabled or for
architectures that don't implement the software tag-based mode
page_to_virt() will be optimized to it's previous form.
Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
---
Changelog v11:
- Redo the patch to work on the page_to_virt macro. Split off changes
about virt to phys conversion to an earlier patch.
- Remove Alexander's acked-by due to bigger changes.
Changelog v7:
- Add Alexander's Acked-by tag.
Changelog v5:
- Move __tag_reset() calls into __phys_addr_nodebug() and
__virt_addr_valid() instead of calling it on the arguments of higher
level functions.
Changelog v4:
- Simplify page_to_virt() by removing pointless casts.
- Remove change in __is_canonical_address() because it's taken care of
in a later patch due to a LAM compatible definition of canonical.
arch/arm64/include/asm/memory.h | 5 -----
include/linux/kasan.h | 10 ++++++++++
include/linux/mm.h | 5 ++++-
3 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h
index 875c0bd0d85a..39dd0071d3ec 100644
--- a/arch/arm64/include/asm/memory.h
+++ b/arch/arm64/include/asm/memory.h
@@ -411,11 +411,6 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
*/
#if defined(CONFIG_DEBUG_VIRTUAL)
-#define page_to_virt(x) ({ \
- __typeof__(x) __page = x; \
- void *__addr = __va(page_to_phys(__page)); \
- (void *)__tag_set((const void *)__addr, page_kasan_tag(__page));\
-})
#define virt_to_page(x) pfn_to_page(virt_to_pfn(x))
#else
#define page_to_virt(x) ({ \
diff --git a/include/linux/kasan.h b/include/linux/kasan.h
index c6febd1362e8..58cf02af4fa5 100644
--- a/include/linux/kasan.h
+++ b/include/linux/kasan.h
@@ -527,6 +527,11 @@ static inline void *kasan_reset_tag(const void *addr)
return (void *)arch_kasan_reset_tag(addr);
}
+static inline void *kasan_set_tag(const void *addr, u8 tag)
+{
+ return (void *)arch_kasan_set_tag(addr, tag);
+}
+
/**
* kasan_report - print a report about a bad memory access detected by KASAN
* @addr: address of the bad access
@@ -544,6 +549,11 @@ static inline void *kasan_reset_tag(const void *addr)
return (void *)addr;
}
+static inline void *kasan_set_tag(const void *addr, u8 tag)
+{
+ return (void *)addr;
+}
+
#endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS*/
#ifdef CONFIG_KASAN_HW_TAGS
diff --git a/include/linux/mm.h b/include/linux/mm.h
index b2245b1a25d8..044425db766e 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -117,7 +117,10 @@ extern int mmap_rnd_compat_bits __read_mostly;
#endif
#ifndef page_to_virt
-#define page_to_virt(x) __va(PFN_PHYS(page_to_pfn(x)))
+#define page_to_virt(x) ({ \
+ void *__addr = __va(PFN_PHYS(page_to_pfn((struct page *)x))); \
+ kasan_set_tag(__addr, page_kasan_tag(x)); \
+})
#endif
#ifndef lm_alias
--
2.53.0
next prev parent reply other threads:[~2026-03-10 17:54 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-10 17:51 [PATCH v11 00/15] kasan: x86: arm64: KASAN tag-based mode for x86 Maciej Wieczor-Retman
2026-03-10 17:53 ` [PATCH v11 01/15] kasan: sw_tags: Use arithmetic shift for shadow computation Maciej Wieczor-Retman
2026-03-10 17:53 ` [PATCH v11 02/15] kasan: arm64: x86: Make special tags arch specific Maciej Wieczor-Retman
2026-03-10 17:53 ` [PATCH v11 04/15] x86/kasan: Add arch specific kasan functions Maciej Wieczor-Retman
2026-03-10 17:54 ` Maciej Wieczor-Retman [this message]
2026-03-10 17:54 ` [PATCH v11 07/15] mm/execmem: Untag addresses in EXECMEM_ROX related pointer arithmetic Maciej Wieczor-Retman
2026-03-10 18:24 ` [PATCH v11 00/15] kasan: x86: arm64: KASAN tag-based mode for x86 Andrew Morton
2026-03-10 19:17 ` Maciej Wieczor-Retman
2026-03-10 19:00 ` Peter Zijlstra
2026-03-10 19:30 ` Maciej Wieczor-Retman
2026-03-11 11:05 ` Peter Zijlstra
2026-03-11 11:13 ` Maciej Wieczor-Retman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b51b60f82bbd99cdcd7f6d0d2bac1d5f3bed0fe0.1773164688.git.m.wieczorretman@pm.me \
--to=m.wieczorretman@pm.me \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=andreyknvl@gmail.com \
--cc=catalin.marinas@arm.com \
--cc=david@kernel.org \
--cc=dvyukov@google.com \
--cc=glider@google.com \
--cc=kasan-dev@googlegroups.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=maciej.wieczor-retman@intel.com \
--cc=mhocko@suse.com \
--cc=rppt@kernel.org \
--cc=ryabinin.a.a@gmail.com \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
--cc=vincenzo.frascino@arm.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox