From nobody Sat Feb 7 23:11:08 2026 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 28C9437BE6B; Mon, 12 Jan 2026 17:27:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238858; cv=none; b=WKEtxDsOcyg1oupuOrR6uKrh4MZqPY6g3tCjp3DBzzf1Wak+THkuBWN+2WO2UoM+X+bdRQmZIp2g45O/sFCBoZtX5W6lyQ0LYE/Ixih9+DuHn9bCxYJ2wWkbZh9mqV2k7jhqT5N9WlGz4kv2Oz96DBd79Ss29BVMn50joBLy7GE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238858; c=relaxed/simple; bh=85OiS0mJ//0fLEsMtVCkU9dTieVO7LDDyX3yzvgNRZY=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lesjGnkzR/PBMK5G40JR7379mfKXrhoydqwkjGhErhOYtOrgHaGI/3QRXh3Mz4xrYkL5++I8IbYGqomsucTXzgdhVCohm004fZZFoLJ/vYvq0ZXNfKGNsMzmIzbFCuxKPadPuDGOk0f3cj+RDlL5DW1Z1+a8i7qYbuzMjlQZziE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=LjxjrDuC; arc=none smtp.client-ip=185.70.43.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="LjxjrDuC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1768238846; x=1768498046; bh=JiMOOEA6qNaiQDDGgdthf6TZH3O0GT1Wjo585EHcW4E=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=LjxjrDuCoGf0G2mwtHa5/HV7S/hBCta7EcOVZERcqDUqFwE0FNnq96D8jZFg2S/fK OIygr+dLmg5ndkEUU/pCV4KSopBovq+rLu8BnJHFuSBjdWxC5E+VEKphqlx/vObzEs lWYzLRBkT+vP/B9e114ATzUgVhAqtxlx13mvk+d97a4ycPwikhexcdCjCVo3gb6yhw dKMr0iX8j6mRfpKSeO3L5kX0HaJRJPpgs/rV7U/88zDYkjOxscSNbBC/TE75sYeJ0e T8uK8+pDljbYd2VSI45c1/2Y+LEbCcMkDveADsvGSrtuwd0hAK6ngQ1PQl5qGmvsH8 E4hwHdMcHDtnA== Date: Mon, 12 Jan 2026 17:27:22 +0000 To: Catalin Marinas , Will Deacon , Jonathan Corbet , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Jan Kiszka , Kieran Bingham , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Samuel Holland , Maciej Wieczor-Retman , linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, llvm@lists.linux.dev Subject: [PATCH v8 01/14] kasan: sw_tags: Use arithmetic shift for shadow computation Message-ID: <4f31939d55d886f21c91272398fe43a32ea36b3f.1768233085.git.m.wieczorretman@pm.me> In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: 70a6fe20ee18bc60426e6e68f454786f4fa58ddd Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Samuel Holland Currently, kasan_mem_to_shadow() uses a logical right shift, which turns canonical kernel addresses into non-canonical addresses by clearing the high KASAN_SHADOW_SCALE_SHIFT bits. The value of KASAN_SHADOW_OFFSET is then chosen so that the addition results in a canonical address for the shadow memory. For KASAN_GENERIC, this shift/add combination is ABI with the compiler, because KASAN_SHADOW_OFFSET is used in compiler-generated inline tag checks[1], which must only attempt to dereference canonical addresses. However, for KASAN_SW_TAGS there is some freedom to change the algorithm without breaking the ABI. Because TBI is enabled for kernel addresses, the top bits of shadow memory addresses computed during tag checks are irrelevant, and so likewise are the top bits of KASAN_SHADOW_OFFSET. This is demonstrated by the fact that LLVM uses a logical right shift in the tag check fast path[2] but a sbfx (signed bitfield extract) instruction in the slow path[3] without causing any issues. Using an arithmetic shift in kasan_mem_to_shadow() provides a number of benefits: 1) The memory layout doesn't change but is easier to understand. KASAN_SHADOW_OFFSET becomes a canonical memory address, and the shifted pointer becomes a negative offset, so KASAN_SHADOW_OFFSET =3D=3D KASAN_SHADOW_END regardless of the shift amount or the size of the virtual address space. 2) KASAN_SHADOW_OFFSET becomes a simpler constant, requiring only one instruction to load instead of two. Since it must be loaded in each function with a tag check, this decreases kernel text size by 0.5%. 3) This shift and the sign extension from kasan_reset_tag() can be combined into a single sbfx instruction. When this same algorithm change is applied to the compiler, it removes an instruction from each inline tag check, further reducing kernel text size by an additional 4.6%. These benefits extend to other architectures as well. On RISC-V, where the baseline ISA does not shifted addition or have an equivalent to the sbfx instruction, loading KASAN_SHADOW_OFFSET is reduced from 3 to 2 instructions, and kasan_mem_to_shadow(kasan_reset_tag(addr)) similarly combines two consecutive right shifts. Link: https://github.com/llvm/llvm-project/blob/llvmorg-20-init/llvm/lib/Tr= ansforms/Instrumentation/AddressSanitizer.cpp#L1316 [1] Link: https://github.com/llvm/llvm-project/blob/llvmorg-20-init/llvm/lib/Tr= ansforms/Instrumentation/HWAddressSanitizer.cpp#L895 [2] Link: https://github.com/llvm/llvm-project/blob/llvmorg-20-init/llvm/lib/Ta= rget/AArch64/AArch64AsmPrinter.cpp#L669 [3] Signed-off-by: Samuel Holland Co-developed-by: Maciej Wieczor-Retman Signed-off-by: Maciej Wieczor-Retman Acked-by: Catalin Marinas --- Changelog v7: (Maciej) - Change UL to ULL in report.c to fix some compilation warnings. Changelog v6: (Maciej) - Add Catalin's acked-by. - Move x86 gdb snippet here from the last patch. Changelog v5: (Maciej) - (u64) -> (unsigned long) in report.c Changelog v4: (Maciej) - Revert x86 to signed mem_to_shadow mapping. - Remove last two paragraphs since they were just poorer duplication of the comments in kasan_non_canonical_hook(). Changelog v3: (Maciej) - Fix scripts/gdb/linux/kasan.py so the new signed mem_to_shadow() is reflected there. - Fix Documentation/arch/arm64/kasan-offsets.sh to take new offsets into account. - Made changes to the kasan_non_canonical_hook() according to upstream discussion. Settled on overflow on both ranges and separate checks for x86 and arm. Changelog v2: (Maciej) - Correct address range that's checked in kasan_non_canonical_hook(). Adjust the comment inside. - Remove part of comment from arch/arm64/include/asm/memory.h. - Append patch message paragraph about the overflow in kasan_non_canonical_hook(). Documentation/arch/arm64/kasan-offsets.sh | 8 +++-- arch/arm64/Kconfig | 10 +++---- arch/arm64/include/asm/memory.h | 14 ++++++++- arch/arm64/mm/kasan_init.c | 7 +++-- include/linux/kasan.h | 10 +++++-- mm/kasan/report.c | 36 ++++++++++++++++++++--- scripts/gdb/linux/kasan.py | 5 +++- scripts/gdb/linux/mm.py | 5 ++-- 8 files changed, 76 insertions(+), 19 deletions(-) diff --git a/Documentation/arch/arm64/kasan-offsets.sh b/Documentation/arch= /arm64/kasan-offsets.sh index 2dc5f9e18039..ce777c7c7804 100644 --- a/Documentation/arch/arm64/kasan-offsets.sh +++ b/Documentation/arch/arm64/kasan-offsets.sh @@ -5,8 +5,12 @@ =20 print_kasan_offset () { printf "%02d\t" $1 - printf "0x%08x00000000\n" $(( (0xffffffff & (-1 << ($1 - 1 - 32))) \ - - (1 << (64 - 32 - $2)) )) + if [[ $2 -ne 4 ]] then + printf "0x%08x00000000\n" $(( (0xffffffff & (-1 << ($1 - 1 - 32))) \ + - (1 << (64 - 32 - $2)) )) + else + printf "0x%08x00000000\n" $(( (0xffffffff & (-1 << ($1 - 1 - 32))) )) + fi } =20 echo KASAN_SHADOW_SCALE_SHIFT =3D 3 diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 93173f0a09c7..c1b7261cdb96 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -434,11 +434,11 @@ config KASAN_SHADOW_OFFSET default 0xdffffe0000000000 if ARM64_VA_BITS_42 && !KASAN_SW_TAGS default 0xdfffffc000000000 if ARM64_VA_BITS_39 && !KASAN_SW_TAGS default 0xdffffff800000000 if ARM64_VA_BITS_36 && !KASAN_SW_TAGS - default 0xefff800000000000 if (ARM64_VA_BITS_48 || (ARM64_VA_BITS_52 && != ARM64_16K_PAGES)) && KASAN_SW_TAGS - default 0xefffc00000000000 if (ARM64_VA_BITS_47 || ARM64_VA_BITS_52) && A= RM64_16K_PAGES && KASAN_SW_TAGS - default 0xeffffe0000000000 if ARM64_VA_BITS_42 && KASAN_SW_TAGS - default 0xefffffc000000000 if ARM64_VA_BITS_39 && KASAN_SW_TAGS - default 0xeffffff800000000 if ARM64_VA_BITS_36 && KASAN_SW_TAGS + default 0xffff800000000000 if (ARM64_VA_BITS_48 || (ARM64_VA_BITS_52 && != ARM64_16K_PAGES)) && KASAN_SW_TAGS + default 0xffffc00000000000 if (ARM64_VA_BITS_47 || ARM64_VA_BITS_52) && A= RM64_16K_PAGES && KASAN_SW_TAGS + default 0xfffffe0000000000 if ARM64_VA_BITS_42 && KASAN_SW_TAGS + default 0xffffffc000000000 if ARM64_VA_BITS_39 && KASAN_SW_TAGS + default 0xfffffff800000000 if ARM64_VA_BITS_36 && KASAN_SW_TAGS default 0xffffffffffffffff =20 config UNWIND_TABLES diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memor= y.h index 9d54b2ea49d6..f127fbf691ac 100644 --- a/arch/arm64/include/asm/memory.h +++ b/arch/arm64/include/asm/memory.h @@ -89,7 +89,15 @@ * * KASAN_SHADOW_END is defined first as the shadow address that correspond= s to * the upper bound of possible virtual kernel memory addresses UL(1) << 64 - * according to the mapping formula. + * according to the mapping formula. For Generic KASAN, the address in the + * mapping formula is treated as unsigned (part of the compiler's ABI), so= the + * end of the shadow memory region is at a large positive offset from + * KASAN_SHADOW_OFFSET. For Software Tag-Based KASAN, the address in the + * formula is treated as signed. Since all kernel addresses are negative, = they + * map to shadow memory below KASAN_SHADOW_OFFSET, making KASAN_SHADOW_OFF= SET + * itself the end of the shadow memory region. (User pointers are positive= and + * would map to shadow memory above KASAN_SHADOW_OFFSET, but shadow memory= is + * not allocated for them.) * * KASAN_SHADOW_START is defined second based on KASAN_SHADOW_END. The sha= dow * memory start must map to the lowest possible kernel virtual memory addr= ess @@ -100,7 +108,11 @@ */ #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL) +#ifdef CONFIG_KASAN_GENERIC #define KASAN_SHADOW_END ((UL(1) << (64 - KASAN_SHADOW_SCALE_SHIFT)) + KAS= AN_SHADOW_OFFSET) +#else +#define KASAN_SHADOW_END KASAN_SHADOW_OFFSET +#endif #define _KASAN_SHADOW_START(va) (KASAN_SHADOW_END - (UL(1) << ((va) - KASA= N_SHADOW_SCALE_SHIFT))) #define KASAN_SHADOW_START _KASAN_SHADOW_START(vabits_actual) #define PAGE_END KASAN_SHADOW_START diff --git a/arch/arm64/mm/kasan_init.c b/arch/arm64/mm/kasan_init.c index abeb81bf6ebd..937f6eb8115b 100644 --- a/arch/arm64/mm/kasan_init.c +++ b/arch/arm64/mm/kasan_init.c @@ -198,8 +198,11 @@ static bool __init root_level_aligned(u64 addr) /* The early shadow maps everything to a single page of zeroes */ asmlinkage void __init kasan_early_init(void) { - BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=3D - KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT))); + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=3D + KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT))); + else + BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=3D KASAN_SHADOW_END); BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS), SHADOW_ALIGN)); BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS_MIN), SHADOW_ALIGN)); BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, SHADOW_ALIGN)); diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 9c6ac4b62eb9..0f65e88cc3f6 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -62,8 +62,14 @@ int kasan_populate_early_shadow(const void *shadow_start, #ifndef kasan_mem_to_shadow static inline void *kasan_mem_to_shadow(const void *addr) { - return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) - + KASAN_SHADOW_OFFSET; + void *scaled; + + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + scaled =3D (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT); + else + scaled =3D (void *)((long)addr >> KASAN_SHADOW_SCALE_SHIFT); + + return KASAN_SHADOW_OFFSET + scaled; } #endif =20 diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 62c01b4527eb..b5beb1b10bd2 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -642,11 +642,39 @@ void kasan_non_canonical_hook(unsigned long addr) const char *bug_type; =20 /* - * All addresses that came as a result of the memory-to-shadow mapping - * (even for bogus pointers) must be >=3D KASAN_SHADOW_OFFSET. + * For Generic KASAN, kasan_mem_to_shadow() uses the logical right shift + * and never overflows with the chosen KASAN_SHADOW_OFFSET values (on + * both x86 and arm64). Thus, the possible shadow addresses (even for + * bogus pointers) belong to a single contiguous region that is the + * result of kasan_mem_to_shadow() applied to the whole address space. */ - if (addr < KASAN_SHADOW_OFFSET) - return; + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) { + if (addr < (unsigned long)kasan_mem_to_shadow((void *)(0ULL)) || + addr > (unsigned long)kasan_mem_to_shadow((void *)(~0ULL))) + return; + } + + /* + * For Software Tag-Based KASAN, kasan_mem_to_shadow() uses the + * arithmetic shift. Normally, this would make checking for a possible + * shadow address complicated, as the shadow address computation + * operation would overflow only for some memory addresses. However, due + * to the chosen KASAN_SHADOW_OFFSET values and the fact the + * kasan_mem_to_shadow() only operates on pointers with the tag reset, + * the overflow always happens. + * + * For arm64, the top byte of the pointer gets reset to 0xFF. Thus, the + * possible shadow addresses belong to a region that is the result of + * kasan_mem_to_shadow() applied to the memory range + * [0xFF000000000000, 0xFFFFFFFFFFFFFFFF]. Despite the overflow, the + * resulting possible shadow region is contiguous, as the overflow + * happens for both 0xFF000000000000 and 0xFFFFFFFFFFFFFFFF. + */ + if (IS_ENABLED(CONFIG_KASAN_SW_TAGS) && IS_ENABLED(CONFIG_ARM64)) { + if (addr < (unsigned long)kasan_mem_to_shadow((void *)(0xFFULL << 56)) || + addr > (unsigned long)kasan_mem_to_shadow((void *)(~0ULL))) + return; + } =20 orig_addr =3D (unsigned long)kasan_shadow_to_mem((void *)addr); =20 diff --git a/scripts/gdb/linux/kasan.py b/scripts/gdb/linux/kasan.py index 56730b3fde0b..4b86202b155f 100644 --- a/scripts/gdb/linux/kasan.py +++ b/scripts/gdb/linux/kasan.py @@ -7,7 +7,8 @@ # =20 import gdb -from linux import constants, mm +from linux import constants, utils, mm +from ctypes import c_int64 as s64 =20 def help(): t =3D """Usage: lx-kasan_mem_to_shadow [Hex memory addr] @@ -39,6 +40,8 @@ class KasanMemToShadow(gdb.Command): else: help() def kasan_mem_to_shadow(self, addr): + if constants.CONFIG_KASAN_SW_TAGS and not utils.is_target_arch('x8= 6'): + addr =3D s64(addr) return (addr >> self.p_ops.KASAN_SHADOW_SCALE_SHIFT) + self.p_ops.= KASAN_SHADOW_OFFSET =20 KasanMemToShadow() diff --git a/scripts/gdb/linux/mm.py b/scripts/gdb/linux/mm.py index 7571aebbe650..2e63f3dedd53 100644 --- a/scripts/gdb/linux/mm.py +++ b/scripts/gdb/linux/mm.py @@ -110,12 +110,13 @@ class aarch64_page_ops(): self.KERNEL_END =3D gdb.parse_and_eval("_end") =20 if constants.LX_CONFIG_KASAN_GENERIC or constants.LX_CONFIG_KASAN_= SW_TAGS: + self.KASAN_SHADOW_OFFSET =3D constants.LX_CONFIG_KASAN_SHADOW_= OFFSET if constants.LX_CONFIG_KASAN_GENERIC: self.KASAN_SHADOW_SCALE_SHIFT =3D 3 + self.KASAN_SHADOW_END =3D (1 << (64 - self.KASAN_SHADOW_SC= ALE_SHIFT)) + self.KASAN_SHADOW_OFFSET else: self.KASAN_SHADOW_SCALE_SHIFT =3D 4 - self.KASAN_SHADOW_OFFSET =3D constants.LX_CONFIG_KASAN_SHADOW_= OFFSET - self.KASAN_SHADOW_END =3D (1 << (64 - self.KASAN_SHADOW_SCALE_= SHIFT)) + self.KASAN_SHADOW_OFFSET + self.KASAN_SHADOW_END =3D self.KASAN_SHADOW_OFFSET self.PAGE_END =3D self.KASAN_SHADOW_END - (1 << (self.vabits_a= ctual - self.KASAN_SHADOW_SCALE_SHIFT)) else: self.PAGE_END =3D self._PAGE_END(self.VA_BITS_MIN) --=20 2.52.0 From nobody Sat Feb 7 23:11:08 2026 Received: from mail-24418.protonmail.ch (mail-24418.protonmail.ch [109.224.244.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 45B4E2EE5FD for ; Mon, 12 Jan 2026 17:27:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=109.224.244.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238859; cv=none; b=Nj6WToORHqLHYalnxkF3s3+57wUO9YMAndTGlXypnFMxhd6hKDcBAAlclcQEv7YFZAy+X4lp/qYHuhmrvZsgnb32B+DwOEPwepTnOz7bzP0kXtAuFpp9cgeoNo2No0W39Q8VySuDkCsMK3LLvCAQjpwZ7IDMV8gO2ZIV9ztxJFI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238859; c=relaxed/simple; bh=F05NyezmE+q5VPUc0OvJv6X8bH2Me0nLUM4kikfE0YA=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=U7kKrpIkMb+BBcBYowRFkjeGmyli6N7fPIqivbJMwixnuNq3bS64adgpbCyIfd5heyK3HHyCLnldRB9bkRgeh2Y1E8uglaljW8Rio9II7kTtEX8ImJ9nttHtIP3ycXO4oudNdmDvrlMnzxeWrrF0R8sz1numEDZvgr4oJ7YvZnU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=eXbCNbAs; arc=none smtp.client-ip=109.224.244.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="eXbCNbAs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1768238854; x=1768498054; bh=zYoTsocZClxknlYhcC3iOmUo2Nj+iM7h4tlwilxbfpM=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=eXbCNbAsbfdTYUOtfEjPHxxoETutjpDCzmgl06KFlLBhrQyTYUDrn0FUs9KxtfbKd uDE7RAxmPd5QBI7fTEI7OgST0f47TY0TpzWjEeMkG8A6t6IAY859VKl5dUnywazVdd Qgkp0btL0PFZsvdFmoovITuaA48JC+uUMT7MhJnMwn+wC8er0Km7k1q+nrjOvip7Dk jFQvYxmM3ktl+TbjWUiyVLWv0efEFyJCB9uSqL3FWiv+8AXnze84P9SbFazuEM2Zc4 j1Ij2f9MwDb8+CO6tjcyhSq9EtjyJOipI8TOVO1m8ItFKi7V6+xmyhYwceTCerIUBY q7/L5u0H2Gt2g== Date: Mon, 12 Jan 2026 17:27:29 +0000 To: Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Catalin Marinas , Will Deacon , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Samuel Holland , Maciej Wieczor-Retman , linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org Subject: [PATCH v8 02/14] kasan: arm64: x86: Make special tags arch specific Message-ID: In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: f03de1003023037700aab2cb9b05c2dac6dc7c44 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Samuel Holland KASAN's tag-based mode defines multiple special tag values. They're reserved for: - Native kernel value. On arm64 it's 0xFF and it causes an early return in the tag checking function. - Invalid value. 0xFE marks an area as freed / unallocated. It's also the value that is used to initialize regions of shadow memory. - Min and max values. 0xFD is the highest value that can be randomly generated for a new tag. 0 is the minimal value with the exception of arm64's hardware mode where it is equal to 0xF0. Metadata macro is also defined: - Tag width equal to 8. Tag-based mode on x86 is going to use 4 bit wide tags so all the above values need to be changed accordingly. Make tag width and native kernel tag arch specific for x86 and arm64. Base the invalid tag value and the max value on the native kernel tag since they follow the same pattern on both mentioned architectures. Also generalize KASAN_SHADOW_INIT and 0xff used in various page_kasan_tag* helpers. Give KASAN_TAG_MIN the default value of zero, and move the special value for hw_tags arm64 to its arch specific kasan-tags.h. Signed-off-by: Samuel Holland Co-developed-by: Maciej Wieczor-Retman Signed-off-by: Maciej Wieczor-Retman Acked-by: Will Deacon (for the arm part) Reviewed-by: Andrey Konovalov Reviewed-by: Andrey Ryabinin --- Changelog v7: - Reorder defines of arm64 tag width to prevent redefinition warnings. - Remove KASAN_TAG_MASK so it's only defined in mmzone.h (Andrey Konovalov) - Merge the 'support tag widths less than 8 bits' with this patch since they do similar things and overwrite each other. (Alexander) Changelog v6: - Add hardware tags KASAN_TAG_WIDTH value to the arm64 arch file. - Keep KASAN_TAG_MASK in the mmzone.h. - Remove ifndef from KASAN_SHADOW_INIT. Changelog v5: - Move KASAN_TAG_MIN to the arm64 kasan-tags.h for the hardware KASAN mode case. Changelog v4: - Move KASAN_TAG_MASK to kasan-tags.h. Changelog v2: - Remove risc-v from the patch. MAINTAINERS | 2 +- arch/arm64/include/asm/kasan-tags.h | 14 ++++++++++++++ arch/arm64/include/asm/kasan.h | 2 -- arch/arm64/include/asm/uaccess.h | 1 + arch/x86/include/asm/kasan-tags.h | 9 +++++++++ include/linux/kasan-tags.h | 19 ++++++++++++++----- include/linux/kasan.h | 3 +-- include/linux/mm.h | 6 +++--- include/linux/page-flags-layout.h | 9 +-------- 9 files changed, 44 insertions(+), 21 deletions(-) create mode 100644 arch/arm64/include/asm/kasan-tags.h create mode 100644 arch/x86/include/asm/kasan-tags.h diff --git a/MAINTAINERS b/MAINTAINERS index 0d044a58cbfe..84fdf497a97c 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13581,7 +13581,7 @@ L: kasan-dev@googlegroups.com S: Maintained B: https://bugzilla.kernel.org/buglist.cgi?component=3DSanitizers&product= =3DMemory%20Management F: Documentation/dev-tools/kasan.rst -F: arch/*/include/asm/*kasan.h +F: arch/*/include/asm/*kasan*.h F: arch/*/mm/kasan_init* F: include/linux/kasan*.h F: lib/Kconfig.kasan diff --git a/arch/arm64/include/asm/kasan-tags.h b/arch/arm64/include/asm/k= asan-tags.h new file mode 100644 index 000000000000..259952677443 --- /dev/null +++ b/arch/arm64/include/asm/kasan-tags.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ASM_KASAN_TAGS_H +#define __ASM_KASAN_TAGS_H + +#define KASAN_TAG_KERNEL 0xFF /* native kernel pointers tag */ + +#ifdef CONFIG_KASAN_HW_TAGS +#define KASAN_TAG_MIN 0xF0 /* minimum value for random tags */ +#define KASAN_TAG_WIDTH 4 +#else +#define KASAN_TAG_WIDTH 8 +#endif + +#endif /* ASM_KASAN_TAGS_H */ diff --git a/arch/arm64/include/asm/kasan.h b/arch/arm64/include/asm/kasan.h index b167e9d3da91..fd4a8557d736 100644 --- a/arch/arm64/include/asm/kasan.h +++ b/arch/arm64/include/asm/kasan.h @@ -6,8 +6,6 @@ =20 #include #include -#include -#include =20 #define arch_kasan_set_tag(addr, tag) __tag_set(addr, tag) #define arch_kasan_reset_tag(addr) __tag_reset(addr) diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uacc= ess.h index 6490930deef8..ccd41a39e3a1 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include diff --git a/arch/x86/include/asm/kasan-tags.h b/arch/x86/include/asm/kasan= -tags.h new file mode 100644 index 000000000000..68ba385bc75c --- /dev/null +++ b/arch/x86/include/asm/kasan-tags.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ASM_KASAN_TAGS_H +#define __ASM_KASAN_TAGS_H + +#define KASAN_TAG_KERNEL 0xF /* native kernel pointers tag */ + +#define KASAN_TAG_WIDTH 4 + +#endif /* ASM_KASAN_TAGS_H */ diff --git a/include/linux/kasan-tags.h b/include/linux/kasan-tags.h index 4f85f562512c..ad5c11950233 100644 --- a/include/linux/kasan-tags.h +++ b/include/linux/kasan-tags.h @@ -2,13 +2,22 @@ #ifndef _LINUX_KASAN_TAGS_H #define _LINUX_KASAN_TAGS_H =20 +#if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS) +#include +#endif + +#ifndef KASAN_TAG_WIDTH +#define KASAN_TAG_WIDTH 0 +#endif + +#ifndef KASAN_TAG_KERNEL #define KASAN_TAG_KERNEL 0xFF /* native kernel pointers tag */ -#define KASAN_TAG_INVALID 0xFE /* inaccessible memory tag */ -#define KASAN_TAG_MAX 0xFD /* maximum value for random tags */ +#endif + +#define KASAN_TAG_INVALID (KASAN_TAG_KERNEL - 1) /* inaccessible memory ta= g */ +#define KASAN_TAG_MAX (KASAN_TAG_KERNEL - 2) /* maximum value for random = tags */ =20 -#ifdef CONFIG_KASAN_HW_TAGS -#define KASAN_TAG_MIN 0xF0 /* minimum value for random tags */ -#else +#ifndef KASAN_TAG_MIN #define KASAN_TAG_MIN 0x00 /* minimum value for random tags */ #endif =20 diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 0f65e88cc3f6..1c7acdb5f297 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -40,8 +40,7 @@ typedef unsigned int __bitwise kasan_vmalloc_flags_t; /* Software KASAN implementations use shadow memory. */ =20 #ifdef CONFIG_KASAN_SW_TAGS -/* This matches KASAN_TAG_INVALID. */ -#define KASAN_SHADOW_INIT 0xFE +#define KASAN_SHADOW_INIT KASAN_TAG_INVALID #else #define KASAN_SHADOW_INIT 0 #endif diff --git a/include/linux/mm.h b/include/linux/mm.h index 6f959d8ca4b4..8ba91f38a794 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1949,7 +1949,7 @@ static inline u8 page_kasan_tag(const struct page *pa= ge) =20 if (kasan_enabled()) { tag =3D (page->flags.f >> KASAN_TAG_PGSHIFT) & KASAN_TAG_MASK; - tag ^=3D 0xff; + tag ^=3D KASAN_TAG_KERNEL; } =20 return tag; @@ -1962,7 +1962,7 @@ static inline void page_kasan_tag_set(struct page *pa= ge, u8 tag) if (!kasan_enabled()) return; =20 - tag ^=3D 0xff; + tag ^=3D KASAN_TAG_KERNEL; old_flags =3D READ_ONCE(page->flags.f); do { flags =3D old_flags; @@ -1981,7 +1981,7 @@ static inline void page_kasan_tag_reset(struct page *= page) =20 static inline u8 page_kasan_tag(const struct page *page) { - return 0xff; + return KASAN_TAG_KERNEL; } =20 static inline void page_kasan_tag_set(struct page *page, u8 tag) { } diff --git a/include/linux/page-flags-layout.h b/include/linux/page-flags-l= ayout.h index 760006b1c480..b2cc4cb870e0 100644 --- a/include/linux/page-flags-layout.h +++ b/include/linux/page-flags-layout.h @@ -3,6 +3,7 @@ #define PAGE_FLAGS_LAYOUT_H =20 #include +#include #include =20 /* @@ -72,14 +73,6 @@ #define NODE_NOT_IN_PAGE_FLAGS 1 #endif =20 -#if defined(CONFIG_KASAN_SW_TAGS) -#define KASAN_TAG_WIDTH 8 -#elif defined(CONFIG_KASAN_HW_TAGS) -#define KASAN_TAG_WIDTH 4 -#else -#define KASAN_TAG_WIDTH 0 -#endif - #ifdef CONFIG_NUMA_BALANCING #define LAST__PID_SHIFT 8 #define LAST__PID_MASK ((1 << LAST__PID_SHIFT)-1) --=20 2.52.0 From nobody Sat Feb 7 23:11:08 2026 Received: from mail-43102.protonmail.ch (mail-43102.protonmail.ch [185.70.43.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BEF6E37C0E7; Mon, 12 Jan 2026 17:27:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.102 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238871; cv=none; b=ROuqnZcrIe7VOuSR/1/TgRn8FAYOuQU7vruV8QMpPpJBWHif7iZcOqKFVx4ITvxmI3/Qjdeu6nIZWK2Wr54jM49+n9TRWRbD0LDocqfsnrBq5lF/Uvy9JArchCaTuky6xBkdqTvY1dF5HJonWw0BkE20jJvQLSSpKxAgVKHffrU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238871; c=relaxed/simple; bh=MOW+FEXSkjqoA9gO0vl1FWTSXtEjOD+DXsStw9lDKJE=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=a1hFlCXw/8cr/nKoB22SmBfypo76nCPY+1XSieNII+ds7O6bTM3tvdzGQXHW9uCjK0ydUzCUoa8HFoGhKXQPplzPjHlP2IdCdevQvkMvC5xDRP7tP/NkfnDBrQZRmaNihHznjzNKI2PsOSsIw3VzIV1GYPcxWri/jhpeg8qFYm4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=rp7WD2hz; arc=none smtp.client-ip=185.70.43.102 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="rp7WD2hz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1768238858; x=1768498058; bh=i82hVLnXXbAVfgARSkNfTSungiQEgJywgSx/RsX/wSc=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=rp7WD2hzYG+/fEcnXAYWwaq16gfF91DR+4hlcwz8ePNMqLJeSviu3xQH6/dUjJIMC wkHKU+oTtl5PB8Tj6vu9LfKAfpWhuGldwh34+z9y6Qj4+000LRC6/50Ql4la2bIkqE moV4n0OMJbVFFztW4DV/NDkKTe2rhGZceMaYYCt0IgIRJ2y2eaMHDS9AvH+hQKG3fM hx+9cipW/u/ZlCnCIbDrzrOVhwWgs7OvfA8jTsRDKcrgWz26fF13DYVu4jeI+vFIWK pk/KfdqXsbXP3S0Nf9IgwlDxQjtOSPLXlMP8bj4gEzq1VF1IF4N9fQiSYWtOVpN4LD 1qbso/EPHZzHQ== Date: Mon, 12 Jan 2026 17:27:35 +0000 To: Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Maciej Wieczor-Retman , kasan-dev@googlegroups.com, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v8 03/14] kasan: Fix inline mode for x86 tag-based mode Message-ID: <1598e2bb7d45902fb0dc4d0d8e218f61b0c1a0f6.1768233085.git.m.wieczorretman@pm.me> In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: ca3467c5c544f1474f2a10b1278334b95255c44c Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maciej Wieczor-Retman The LLVM compiler uses hwasan-instrument-with-calls parameter to setup inline or outline mode in tag-based KASAN. If zeroed, it means the instrumentation implementation will be pasted into each relevant location along with KASAN related constants during compilation. If set to one all function instrumentation will be done with function calls instead. The default hwasan-instrument-with-calls value for the x86 architecture in the compiler is "1", which is not true for other architectures. Because of this, enabling inline mode in software tag-based KASAN doesn't work on x86 as the kernel script doesn't zero out the parameter and always sets up the outline mode. Explicitly zero out hwasan-instrument-with-calls when enabling inline mode in tag-based KASAN. Signed-off-by: Maciej Wieczor-Retman Reviewed-by: Andrey Konovalov Reviewed-by: Alexander Potapenko Reviewed-by: Andrey Ryabinin --- Changelog v7: - Add Alexander's Reviewed-by tag. Changelog v6: - Add Andrey's Reviewed-by tag. Changelog v3: - Add this patch to the series. scripts/Makefile.kasan | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan index 0ba2aac3b8dc..e485814df3e9 100644 --- a/scripts/Makefile.kasan +++ b/scripts/Makefile.kasan @@ -76,8 +76,11 @@ CFLAGS_KASAN :=3D -fsanitize=3Dkernel-hwaddress RUSTFLAGS_KASAN :=3D -Zsanitizer=3Dkernel-hwaddress \ -Zsanitizer-recover=3Dkernel-hwaddress =20 +# LLVM sets hwasan-instrument-with-calls to 1 on x86 by default. Set it to= 0 +# when inline mode is enabled. ifdef CONFIG_KASAN_INLINE kasan_params +=3D hwasan-mapping-offset=3D$(KASAN_SHADOW_OFFSET) + kasan_params +=3D hwasan-instrument-with-calls=3D0 else kasan_params +=3D hwasan-instrument-with-calls=3D1 endif --=20 2.52.0 From nobody Sat Feb 7 23:11:08 2026 Received: from mail-07.mail-europe.com (mail-0701.mail-europe.com [51.83.17.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1C4B634DCF7 for ; Mon, 12 Jan 2026 17:27:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=51.83.17.38 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238880; cv=none; b=paaAiZGZRDziXrOpGyxDkLHQQVYvzh6m8SKcXqdMQJET37EGdmtMc/OznCg0uZ1h4D1XSbkHw8ODVFGgnh/0TAW9v9CNpG7iGeamPCMZK4K4jJBOx/Ggk55tk7AT6AB4dnEc6WLjOhj+PILbZZ92pyMUc5mp3l1bKkXzzz7+MMM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238880; c=relaxed/simple; bh=GEz+2VQvEoIvaf/5b06kvAVrpBdGWmz3kFBnPleUPQw=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=mmJg3Jy3ORKGav3BHBgfn1D3u2ehwyTy1pIMUqVDRfNFvM8VWs2yr+gHMcP3JQ20fgNz+xUMmUH5i4lArPjSt36SMlzrpSDT6IBr/vVePoPJ1s9I3QM06PhlOCXRtPne//c8G1QVclScPFHLw/9zBVpbtWA24BPEGFSsMEZgU8A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=fail smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=E0ml2UWO; arc=none smtp.client-ip=51.83.17.38 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="E0ml2UWO" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1768238867; x=1768498067; bh=yESMxyUg3nEjWhMgjSTmmglxLz0NhSPoNJMpCNZiqMs=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=E0ml2UWO0gaxz3PTlf5/zQnrEZwbNK1fh90WlTa6MRRxNcNMudGUpmXCGS3M43yvJ v3HX/WOpOlX9oJbHzJhPhS4APnzjFNJnoy1deGRnXpXtkUFzZniYhHHp/FaXdEMc7E 0sMy/Msr7YzONOEnaGMVxr1Nhvt90ikcXoMs0GlLRKyqoQuyehKnbo8xm49ydX/HWd ScRWr5adodhfyXmAcnMPre9jyC8WWhwh2P9Pbn0O4Zf4qszckK5DporB8Ci6CFVYJU 6/86ZdNwyYkqKpqAJRGc1G2BOUxMPRw8XN6LT218d003Ct6KHLbq7ghHQ73zaGQzYj vDbo+AfBRb5Dg== Date: Mon, 12 Jan 2026 17:27:43 +0000 To: Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Axel Rasmussen , Yuanchu Xie , Wei Xu From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Maciej Wieczor-Retman , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v8 04/14] x86/kasan: Add arch specific kasan functions Message-ID: <785eb728e2cc897e05ee709d42214172be481ab9.1768233085.git.m.wieczorretman@pm.me> In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: 10432f028dc5aec035899d14c4ef5f6e6dbe8005 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maciej Wieczor-Retman 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 Reviewed-by: Andrey Ryabinin --- Changelog v7: - Add KASAN_TAG_BYTE_MASK to avoid circular includes and avoid removing KASAN_TAG_MASK from mmzone.h. - Remove Andrey's Acked-by tag. Changelog v6: - Remove empty line after ifdef CONFIG_KASAN_SW_TAGS - Add ifdef 64 bit to avoid problems in vdso32. - Add Andrey's Acked-by tag. 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 | 42 ++++++++++++++++++++++++++++++++++-- include/linux/kasan-tags.h | 2 ++ include/linux/mmzone.h | 2 +- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kasan.h b/arch/x86/include/asm/kasan.h index d7e33c7f096b..eab12527ed7f 100644 --- a/arch/x86/include/asm/kasan.h +++ b/arch/x86/include/asm/kasan.h @@ -3,6 +3,8 @@ #define _ASM_X86_KASAN_H =20 #include +#include +#include #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL) #define KASAN_SHADOW_SCALE_SHIFT 3 =20 @@ -24,8 +26,43 @@ KASAN_SHADOW_SCALE_SHIFT))) =20 #ifndef __ASSEMBLER__ +#include +#include +#include + +#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_64BIT +static inline void *__tag_set(const void *__addr, u8 tag) +{ + u64 addr =3D (u64)__addr; + + addr &=3D ~__tag_shifted(KASAN_TAG_BYTE_MASK); + addr |=3D __tag_shifted(tag & KASAN_TAG_BYTE_MASK); + + return (void *)addr; +} +#else +static inline void *__tag_set(void *__addr, u8 tag) +{ + return __addr; +} +#endif + +#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) =20 #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 +71,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 =20 -#endif +#endif /* CONFIG_KASAN */ + +#endif /* __ASSEMBLER__ */ =20 #endif diff --git a/include/linux/kasan-tags.h b/include/linux/kasan-tags.h index ad5c11950233..e4f26bec3673 100644 --- a/include/linux/kasan-tags.h +++ b/include/linux/kasan-tags.h @@ -10,6 +10,8 @@ #define KASAN_TAG_WIDTH 0 #endif =20 +#define KASAN_TAG_BYTE_MASK ((1UL << KASAN_TAG_WIDTH) - 1) + #ifndef KASAN_TAG_KERNEL #define KASAN_TAG_KERNEL 0xFF /* native kernel pointers tag */ #endif diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 75ef7c9f9307..3839052121d4 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -1177,7 +1177,7 @@ static inline bool zone_is_empty(const struct zone *z= one) #define NODES_MASK ((1UL << NODES_WIDTH) - 1) #define SECTIONS_MASK ((1UL << SECTIONS_WIDTH) - 1) #define LAST_CPUPID_MASK ((1UL << LAST_CPUPID_SHIFT) - 1) -#define KASAN_TAG_MASK ((1UL << KASAN_TAG_WIDTH) - 1) +#define KASAN_TAG_MASK KASAN_TAG_BYTE_MASK #define ZONEID_MASK ((1UL << ZONEID_SHIFT) - 1) =20 static inline enum zone_type memdesc_zonenum(memdesc_flags_t flags) --=20 2.52.0 From nobody Sat Feb 7 23:11:08 2026 Received: from mail-10631.protonmail.ch (mail-10631.protonmail.ch [79.135.106.31]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 832D437C0F4 for ; Mon, 12 Jan 2026 17:28:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=79.135.106.31 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238884; cv=none; b=eJI/2QMF0WQS2VK+y6eP/oB7r7o84wSGiLPJZ2kGbWj/rdkXDby/qovBG+EA6Ljr9BoTIiha2TYUP+t+7hg6r8974+IilyWNauOoI8VrtlN9NYr2dqgZkMOzl9CdODAFN43SfhONcx2URgmTieC7CHpD72vMny2uxeJB7qQ8Li0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238884; c=relaxed/simple; bh=ts+xE8M7HMWR9p7O4iVVulk7XJz3wdjMc067AGvwts4=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QXM3nlelT1uSaHxpJtsBTD0afV0o14peOOIVyFW10Pl5YvjVO7nxmyUOg44vbpU/2GsEmb5aE3USFW1JctZ+DU1kHfWPqyoC52ibEv7UckB3gvBzQ+b+KQOiQktkb0ma6UbXdwzsTP2YAOyvH/B6vrTqgVx7n0LsKECjRZjvasg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=ZyDrqeGI; arc=none smtp.client-ip=79.135.106.31 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="ZyDrqeGI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1768238875; x=1768498075; bh=EzPOKUh/SBkSmXklvPbupvxDq5oPzebM5okvJ6u0Weg=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=ZyDrqeGIlsoLhu7PFqDtit4yjoRD2SYT0M3pLpQpVd3IyqJuaNYjG0x6Me4ypqD4p E8GtLAm9Jkd8tRBxcLr8zDd6SBXrI6K1WhjweSbJG2kTyNHXM4j4WOmbw624ltTGg4 Vgf/GOodGj/fykQCaEcLIQosWjAROwO2I3lCSaeiYeVV+I7PnozLRWh0KjhdP2JENB G2ZWoVmls9xswIz9ddAA+IUJvyQb8mu/4wK1HJbqoI2H/Db6/uoMi8z3GbI2kC+Jr2 mdeTk7SQYYoDsda6cly8f59MP2aq2nyquf8QOCRKU3hlIgFvldtJQsxSHsoCzd0rRk zT9YJJv4Ulvbw== Date: Mon, 12 Jan 2026 17:27:49 +0000 To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Maciej Wieczor-Retman , Alexander Potapenko , linux-kernel@vger.kernel.org Subject: [PATCH v8 05/14] x86/mm: Reset tag for virtual to physical address conversions Message-ID: <883fe20553ddd1f54ccc23a67f4a27318f1c1a4a.1768233085.git.m.wieczorretman@pm.me> In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: 1664433c8d15095bbf1b2fbfa2d0350f8a4b5d5e Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maciej Wieczor-Retman Any place where pointer arithmetic is used to convert a virtual address into a physical one can raise errors if the virtual address is tagged. Reset the pointer's tag by sign extending the tag bits in macros that do pointer arithmetic in address conversions. There will be no change in compiled code with KASAN disabled since the compiler will optimize the __tag_reset() out. Signed-off-by: Maciej Wieczor-Retman Acked-by: Alexander Potapenko --- 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/x86/include/asm/page.h | 8 ++++++++ arch/x86/include/asm/page_64.h | 1 + arch/x86/mm/physaddr.c | 2 ++ 3 files changed, 11 insertions(+) diff --git a/arch/x86/include/asm/page.h b/arch/x86/include/asm/page.h index 9265f2fca99a..bcf5cad3da36 100644 --- a/arch/x86/include/asm/page.h +++ b/arch/x86/include/asm/page.h @@ -7,6 +7,7 @@ #ifdef __KERNEL__ =20 #include +#include =20 #ifdef CONFIG_X86_64 #include @@ -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 =3D __va(page_to_pfn((struct page *)x) << PAGE_SHIFT); \ + __tag_set(__addr, page_kasan_tag(x)); \ +}) +#endif #define virt_to_page(kaddr) pfn_to_page(__pa(kaddr) >> PAGE_SHIFT) extern bool __virt_addr_valid(unsigned long kaddr); #define virt_addr_valid(kaddr) __virt_addr_valid((unsigned long) (kaddr)) diff --git a/arch/x86/include/asm/page_64.h b/arch/x86/include/asm/page_64.h index 2f0e47be79a4..01f9e6233bba 100644 --- a/arch/x86/include/asm/page_64.h +++ b/arch/x86/include/asm/page_64.h @@ -22,6 +22,7 @@ extern unsigned long direct_map_physmem_end; =20 static __always_inline unsigned long __phys_addr_nodebug(unsigned long x) { + x =3D __tag_reset(x); unsigned long y =3D x - __START_KERNEL_map; =20 /* use the carry flag to determine if x was < __START_KERNEL_map */ diff --git a/arch/x86/mm/physaddr.c b/arch/x86/mm/physaddr.c index 8d31c6b9e184..8f18273be0d2 100644 --- a/arch/x86/mm/physaddr.c +++ b/arch/x86/mm/physaddr.c @@ -14,6 +14,7 @@ #ifdef CONFIG_DEBUG_VIRTUAL unsigned long __phys_addr(unsigned long x) { + x =3D __tag_reset(x); unsigned long y =3D x - __START_KERNEL_map; =20 /* use the carry flag to determine if x was < __START_KERNEL_map */ @@ -35,6 +36,7 @@ EXPORT_SYMBOL(__phys_addr); =20 bool __virt_addr_valid(unsigned long x) { + x =3D __tag_reset(x); unsigned long y =3D x - __START_KERNEL_map; =20 /* use the carry flag to determine if x was < __START_KERNEL_map */ --=20 2.52.0 From nobody Sat Feb 7 23:11:08 2026 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 523E82EE5FD for ; Mon, 12 Jan 2026 17:28:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.22 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238882; cv=none; b=vAQmBq0MsIzRl+mYhDoXtG0JYHRT4PCooUDThKh6ibogNsjsN9o6RVQXdf50v3hFZbNskkr8Ly4a+4iIwA2R6mmB1P5Ez6GP27v7chL7eNddqgZH43GLZ5bofj5CWbBYc5RH3crCUb2wm8l+yO63MypYDdUxCSUL0zfuK4tfgiM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238882; c=relaxed/simple; bh=HozmWRLNXRQDGBv8x6jqMPokmlVjrkMuiWae6Oy0W8o=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Wk/VduQwNyWZs8LmCdH2fntI5jOLkjQ/pzq2Db/ezaNBEcErKYihMc39n8PtHx9JfhUSapOqwOcdjs86hbtYvLMX0sJFQtqR9XZsG8Z7fszdScH0o5KL5BInHq/TP5FPFKqtizU1L7+EaTLxYeo4DM/a2ULvDN57INsmOgBPllw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=SJ/9KK3F; arc=none smtp.client-ip=185.70.43.22 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="SJ/9KK3F" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1768238878; x=1768498078; bh=9kx3Q9Q/4s8TjRty/4a8EQIu+e1jQRULqlVoz9Vq2gY=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=SJ/9KK3FVuvcxbDVZc0Twvcm6cH/PyT5nrZk8k+Bj1j2njXRGTHy86r2ExjWqB4Dx +1e+4khek0QJTkwr28gBG6/z8bKHFrbixMrp62NFXTTZ2LHKiXUeyJdQTRQxVWON3t eICiz/InvhM3q4jofLf32ofxR+HeE4ShviFXYnS5fcq2VVc712Wp4Js6+MTNRe7jqr A2Auo1M9QtUw+6cxB40DstxU1zn1SoplpnsHWjUgDMp/f/SgFGK7dQ688eX6Cwfesr KlxxZ4m+c+dw18gPTvPFs+yHhfH9cnvX2l+z4iB7YdcmE7RZaGLUA4P+RyDV3Tg8wt zW70UPDuR+cbA== Date: Mon, 12 Jan 2026 17:27:54 +0000 To: Andrew Morton , Mike Rapoport , Uladzislau Rezki From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Maciej Wieczor-Retman , Alexander Potapenko , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v8 06/14] mm/execmem: Untag addresses in EXECMEM_ROX related pointer arithmetic Message-ID: <09f5b8d31aaf87cf2ba30a29c56524fdd4958bcc.1768233085.git.m.wieczorretman@pm.me> In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: 9683f198d95c8ee0943e4cd044d514066bc7e9ca Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maciej Wieczor-Retman ARCH_HAS_EXECMEM_ROX was re-enabled in x86 at Linux 6.14 release. vm_reset_perms() calculates range's start and end addresses using min() and max() functions. To do that it compares pointers but, with KASAN software tags mode enabled, some are tagged - addr variable is, while start and end variables aren't. This can cause the wrong address to be chosen and result in various errors in different places. Reset tags in the address used as function argument in min(), max(). execmem_cache_add() adds tagged pointers to a maple tree structure, which then are incorrectly compared when walking the tree. That results in different pointers being returned later and page permission violation errors panicking the kernel. Reset tag of the address range inserted into the maple tree inside execmem_vmalloc() which then gets propagated to execmem_cache_add(). Signed-off-by: Maciej Wieczor-Retman Acked-by: Alexander Potapenko --- Changelog v7: - Add Alexander's acked-by tag. - Add comments on why these tag resets are needed (Alexander) Changelog v6: - Move back the tag reset from execmem_cache_add() to execmem_vmalloc() (Mike Rapoport) - Rewrite the changelogs to match the code changes from v6 and v5. Changelog v5: - Remove the within_range() change. - arch_kasan_reset_tag -> kasan_reset_tag. Changelog v4: - Add patch to the series. mm/execmem.c | 9 ++++++++- mm/vmalloc.c | 7 ++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/mm/execmem.c b/mm/execmem.c index 810a4ba9c924..dc7422222cf7 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -59,7 +59,14 @@ static void *execmem_vmalloc(struct execmem_range *range= , size_t size, return NULL; } =20 - return p; + /* + * Resetting the tag here is necessary to avoid the tagged address + * ending up in the maple tree structure. There it's linear address + * can be incorrectly compared with other addresses which can result in + * a wrong address being picked down the line and for example a page + * permission violation error panicking the kernel. + */ + return kasan_reset_tag(p); } =20 struct vm_struct *execmem_vmap(size_t size) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 41dd01e8430c..6ea8d48f70c2 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3354,7 +3354,12 @@ static void vm_reset_perms(struct vm_struct *area) * the vm_unmap_aliases() flush includes the direct map. */ for (i =3D 0; i < area->nr_pages; i +=3D 1U << page_order) { - unsigned long addr =3D (unsigned long)page_address(area->pages[i]); + /* + * Addresses' tag needs resetting so it can be properly used in + * the min() and max() below. Otherwise the start or end values + * might be favoured. + */ + unsigned long addr =3D (unsigned long)kasan_reset_tag(page_address(area-= >pages[i])); =20 if (addr) { unsigned long page_size; --=20 2.52.0 From nobody Sat Feb 7 23:11:08 2026 Received: from mail-10630.protonmail.ch (mail-10630.protonmail.ch [79.135.106.30]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EAC8537B416 for ; Mon, 12 Jan 2026 17:28:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=79.135.106.30 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238897; cv=none; b=hUNCdxVQOC1v1MBPsu87/xqOlvHhWT2Y50N4PT+EDScBmEbh/5fxddZmE9p61Yt10Yc0kzrCUpjI6N/5flNm1WbKASFgOkZ1p9XJ0czmPk+a+WzR0Mmkm+PhV51NODgEjDUvyl27OgnRiT1Wx8zMDXXfSa6rL7PEHJBngtJSGIk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238897; c=relaxed/simple; bh=47M4NsXJnk1Akk4qBVQSxP7chvb20amdiM+dtKSoyGA=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=dg6/UB/VeHi3aekILjGq1oI1qryIDJMKh4Xt9pDnvZfhxjcDXCOOT/L5YEiLpMjjXqHZXqfyS8j+2owG2FLsLiNRKAeT1PvNb1vCkzi+Pe4sw/EcEL1ZzoxrqtjRlWaAqzAu+HHvuLaqQFyXB+zK0kUAZIrNxnJ4GLk88izDFYI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=X0ote1UU; arc=none smtp.client-ip=79.135.106.30 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="X0ote1UU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1768238884; x=1768498084; bh=XdTBhC2iW7XDS/QU6d7OIfEWFihfNkSbQ5BEBrToeak=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=X0ote1UU+btlsIgwfVU06vXq1rzLMFGYLJMj0j9WaUGdwOpw8ozL+9b3BZAI+KYeV 4XvUITPRJGMeSS1BdDDvuVgtCFteTIRbIhw7T0tIghgvDRLQXbLgNh6Wz/9h3+VmOg zUfl5zkLY9v+cgwsm4DF/+vCKhj7JAAWh0Yg2yhzCZRyU+eB0IQLjC9epfHBfF9diD gD/i9GbhNBFEHig16Azh50BU7I4WWXNX56cletOL3LMDJhqAZ1H8EBV1chEpmt33hD 3QaACPFiU3irbu5ToERSQNnEm/UyQu0Be38TUbU6DXVdkYzXruLHZnoCj/O5r7d2yM yJIht9645I++w== Date: Mon, 12 Jan 2026 17:27:59 +0000 To: Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Maciej Wieczor-Retman , linux-kernel@vger.kernel.org Subject: [PATCH v8 07/14] x86/mm: Physical address comparisons in fill_p*d/pte Message-ID: <3775e34b030fe5a8e3306026654f51d7b7ca33f7.1768233085.git.m.wieczorretman@pm.me> In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: 638953a32932a352a22a9c14fb5e1c3838832fe3 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maciej Wieczor-Retman Calculating page offset returns a pointer without a tag. When comparing the calculated offset to a tagged page pointer an error is raised because they are not equal. Change pointer comparisons to physical address comparisons as to avoid issues with tagged pointers that pointer arithmetic would create. Open code pte_offset_kernel(), pmd_offset(), pud_offset() and p4d_offset(). Because one parameter is always zero and the rest of the function insides are enclosed inside __va(), removing that layer lowers the complexity of final assembly. Signed-off-by: Maciej Wieczor-Retman --- Changelog v7: - Swap ternary operator outcomes in fill_p4d since the order was incorrect. Changelog v2: - Open code *_offset() to avoid it's internal __va(). arch/x86/mm/init_64.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index 9983017ecbe0..cee14855acaf 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -269,7 +269,10 @@ static p4d_t *fill_p4d(pgd_t *pgd, unsigned long vaddr) if (pgd_none(*pgd)) { p4d_t *p4d =3D (p4d_t *)spp_getpage(); pgd_populate(&init_mm, pgd, p4d); - if (p4d !=3D p4d_offset(pgd, 0)) + + if (__pa(p4d) !=3D (pgtable_l5_enabled() ? + (unsigned long)pgd_val(*pgd) & PTE_PFN_MASK : + __pa(pgd))) printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n", p4d, p4d_offset(pgd, 0)); } @@ -281,7 +284,7 @@ static pud_t *fill_pud(p4d_t *p4d, unsigned long vaddr) if (p4d_none(*p4d)) { pud_t *pud =3D (pud_t *)spp_getpage(); p4d_populate(&init_mm, p4d, pud); - if (pud !=3D pud_offset(p4d, 0)) + if (__pa(pud) !=3D (p4d_val(*p4d) & p4d_pfn_mask(*p4d))) printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n", pud, pud_offset(p4d, 0)); } @@ -293,7 +296,7 @@ static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr) if (pud_none(*pud)) { pmd_t *pmd =3D (pmd_t *) spp_getpage(); pud_populate(&init_mm, pud, pmd); - if (pmd !=3D pmd_offset(pud, 0)) + if (__pa(pmd) !=3D (pud_val(*pud) & pud_pfn_mask(*pud))) printk(KERN_ERR "PAGETABLE BUG #02! %p <-> %p\n", pmd, pmd_offset(pud, 0)); } @@ -305,7 +308,7 @@ static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr) if (pmd_none(*pmd)) { pte_t *pte =3D (pte_t *) spp_getpage(); pmd_populate_kernel(&init_mm, pmd, pte); - if (pte !=3D pte_offset_kernel(pmd, 0)) + if (__pa(pte) !=3D (pmd_val(*pmd) & pmd_pfn_mask(*pmd))) printk(KERN_ERR "PAGETABLE BUG #03!\n"); } return pte_offset_kernel(pmd, vaddr); --=20 2.52.0 From nobody Sat Feb 7 23:11:08 2026 Received: from mail-05.mail-europe.com (mail-05.mail-europe.com [85.9.206.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9411A37BE8F for ; Mon, 12 Jan 2026 17:28:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=85.9.206.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238909; cv=none; b=BtDciKg459yH83FKcfr94oRpFz8IFCMdDVF5Vl/o/VvUcanMP1FalFm77dkwifLLP9Y2IgVeUWEf0v8Z4769vSR1JNkGfBsI49BGm21BbKJ8K5iapOZB8aUHWeY5Ycq6g+M5tZTfus3BUa40KtyFD44LcUWzvwo2skbadb9l944= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238909; c=relaxed/simple; bh=dYaBvJLaMOl2hIKSIKTJzMVgY84HJ2TIqjD7lEozQg8=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QM8w2F43Mfv/UTuU/jXlOEfRVDOLS3R8msae1siB/3wu/+Ff1gUaFDyBKQMUJmckzvGykETI5S4bv6otOC5qy27XfhYqoQEMsyBXmQhVdYghOZwb4Jzi0GpZ3xiJsyey9BGrJTYqwstAjBesiK4NHsmQyImJYss4ml7/EZuliXA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=d5+JR31P; arc=none smtp.client-ip=85.9.206.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="d5+JR31P" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1768238888; x=1768498088; bh=3onoTwDkEsWsTTZEKyNLGTuXan36WSJnDCZpzSRbbSc=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=d5+JR31P8rzC5x+/wdwl3V/QZVjiNrIbl/yWbTr+7ed7zhAzTFbHYJQ2pe3uzfKnR hVFtLyWENLFm/xcHdyMBrw8tN/aTNTLZrblzD43RvjTHTOdlLy3K0p5yayIX4EobQ4 GE8GRu1H5yDJ9YgDCRDwbXN23gqQH/TNqXiZ3eNxjVEug9GcdTFfdG0hEcykoqTVtY v8kJUCilEfpAtZMth5UBVhiYV7d7uNeFhS23ArqWe4EN83kwrl8fIjQSPocewO7KrC 28r8cqz9jg4DAcjGYWmM0xlrp+sq9VmEWiIVycvZy3+Neno2FMHhFhO8IRwgj+h+RI gPss85DcGhkGw== Date: Mon, 12 Jan 2026 17:28:05 +0000 To: Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Maciej Wieczor-Retman , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org Subject: [PATCH v8 08/14] x86/kasan: KASAN raw shadow memory PTE init Message-ID: <9968297ee3c83a73e1fb05c6415b024d1d2d6a04.1768233085.git.m.wieczorretman@pm.me> In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: 10ab74b16ddac3f72093d110909467bea85605dd Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maciej Wieczor-Retman In KASAN's generic mode the default value in shadow memory is zero. During initialization of shadow memory pages they are allocated and zeroed. In KASAN's tag-based mode the default tag for the arm64 architecture is 0xFE which corresponds to any memory that should not be accessed. On x86 (where tags are 4-bit wide instead of 8-bit wide) that tag is 0xE so during the initializations all the bytes in shadow memory pages should be filled with it. Use memblock_alloc_try_nid_raw() instead of memblock_alloc_try_nid() to avoid zeroing out the memory so it can be set with the KASAN invalid tag. Signed-off-by: Maciej Wieczor-Retman Reviewed-by: Alexander Potapenko Reviewed-by: Andrey Ryabinin --- Changelog v7: - Fix flipped arguments in memset(). - Add Alexander's reviewed-by tag. Changelog v2: - Remove dense mode references, use memset() instead of kasan_poison(). arch/x86/mm/kasan_init_64.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/arch/x86/mm/kasan_init_64.c b/arch/x86/mm/kasan_init_64.c index 998b6010d6d3..7f5c11328ec1 100644 --- a/arch/x86/mm/kasan_init_64.c +++ b/arch/x86/mm/kasan_init_64.c @@ -34,6 +34,18 @@ static __init void *early_alloc(size_t size, int nid, bo= ol should_panic) return ptr; } =20 +static __init void *early_raw_alloc(size_t size, int nid, bool should_pani= c) +{ + void *ptr =3D memblock_alloc_try_nid_raw(size, size, + __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, nid); + + if (!ptr && should_panic) + panic("%pS: Failed to allocate page, nid=3D%d from=3D%lx\n", + (void *)_RET_IP_, nid, __pa(MAX_DMA_ADDRESS)); + + return ptr; +} + static void __init kasan_populate_pmd(pmd_t *pmd, unsigned long addr, unsigned long end, int nid) { @@ -63,8 +75,9 @@ static void __init kasan_populate_pmd(pmd_t *pmd, unsigne= d long addr, if (!pte_none(*pte)) continue; =20 - p =3D early_alloc(PAGE_SIZE, nid, true); - entry =3D pfn_pte(PFN_DOWN(__pa(p)), PAGE_KERNEL); + p =3D early_raw_alloc(PAGE_SIZE, nid, true); + memset(p, KASAN_SHADOW_INIT, PAGE_SIZE); + entry =3D pfn_pte(PFN_DOWN(__pa_nodebug(p)), PAGE_KERNEL); set_pte_at(&init_mm, addr, pte, entry); } while (pte++, addr +=3D PAGE_SIZE, addr !=3D end); } @@ -436,7 +449,7 @@ void __init kasan_init(void) * it may contain some garbage. Now we can clear and write protect it, * since after the TLB flush no one should write to it. */ - memset(kasan_early_shadow_page, 0, PAGE_SIZE); + memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE); for (i =3D 0; i < PTRS_PER_PTE; i++) { pte_t pte; pgprot_t prot; --=20 2.52.0 From nobody Sat Feb 7 23:11:08 2026 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5352D34DCF7 for ; Mon, 12 Jan 2026 17:28:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238898; cv=none; b=F3+6xZUS9V8liVYghqeifaiVvt3QbRrC8PJPxf8bbB5giSGvoKhAje+hc8q2BE8kfkTJ0vA13WJk6Qcq9z3urynR1ZJ3hKonxNQOMg/g7UoCzG0a51CD72MxS5JYRtX5iCwU1sh0R1D3yQxiGxljWVjBXnr9xQAz8O+jz1eEshQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238898; c=relaxed/simple; bh=gvUa1176eKZ4v7OZs0Fzg0JdmM+FZNuNE6etRDnNF6U=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=G1kCRGZthbtbNn6YTg9HtmeUlw7bqjijxlwXGxOd//qmP1JKk+VoPROZBUbUdoMRHW8yoYfMNuuWOAYcg1Yff3UARBXAULLrjz2WFjtFjgiFlFVhWQ5MqbNFI8jld6pdt4fLcQFz/YeNMB8cuoh+11HNE6mv+cAiRaSaX82jixg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=IPvUbkma; arc=none smtp.client-ip=185.70.43.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="IPvUbkma" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1768238892; x=1768498092; bh=1QI8O92VtcxKJXXMBmUWFQrJdTexY9lSWgo4BYwXQn4=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=IPvUbkmagBrXgadDoUy1iN9dEQxmINGxIsgkhPn3iI7MKW3M/imed8vA57zM5uLmH Ywo6G8wWDLJey0t9Hf6giY0IMIldp1sL6poz+lPVWoWOhaVy7kpcboQXdR7revHS5V MeZbkZI3SYZVByUDoY9a7nVh5u0TFFS1dyY60wyUo111KjOv1KjrlyRnVxKhkf8xIP sqiNXMDVRcFZyXposvASqz22lWdFq8XVruJ8TbP3yNduazzrEbUs0rNRJGehkR2I5m VXqPHpzjH09UYb6jty5jR+5tpP0UbmbNnuwW6uUJ3yssPwTuaCM4Dagvcbq8cgPRcg CABFcBKY+QxyA== Date: Mon, 12 Jan 2026 17:28:09 +0000 To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Maciej Wieczor-Retman , Alexander Potapenko , linux-kernel@vger.kernel.org Subject: [PATCH v8 09/14] x86/mm: LAM compatible non-canonical definition Message-ID: <0347c61eccf739ad15ec62600f009c212d52e761.1768233085.git.m.wieczorretman@pm.me> In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: c9da2d21fdfd6db8d91ee99caa379b58290232b9 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maciej Wieczor-Retman For an address to be canonical it has to have its top bits equal to each other. The number of bits depends on the paging level and whether they're supposed to be ones or zeroes depends on whether the address points to kernel or user space. With Linear Address Masking (LAM) enabled, the definition of linear address canonicality is modified. Not all of the previously required bits need to be equal, only the first and last from the previously equal bitmask. So for example a 5-level paging kernel address needs to have bits [63] and [56] set. Change the canonical checking function to use bit masks instead of bit shifts. Signed-off-by: Maciej Wieczor-Retman Acked-by: Alexander Potapenko --- Changelog v7: - Add Alexander's acked-by tag. - Add parentheses around vaddr_bits as suggested by checkpatch. - Apply the bitmasks to the __canonical_address() function which is used in kvm code. Changelog v6: - Use bitmasks to check both kernel and userspace addresses in the __is_canonical_address() (Dave Hansen and Samuel Holland). Changelog v4: - Add patch to the series. arch/x86/include/asm/page.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/page.h b/arch/x86/include/asm/page.h index bcf5cad3da36..b7940fa49e64 100644 --- a/arch/x86/include/asm/page.h +++ b/arch/x86/include/asm/page.h @@ -82,9 +82,22 @@ static __always_inline void *pfn_to_kaddr(unsigned long = pfn) return __va(pfn << PAGE_SHIFT); } =20 +#ifdef CONFIG_KASAN_SW_TAGS +#define CANONICAL_MASK(vaddr_bits) (BIT_ULL(63) | BIT_ULL((vaddr_bits) - 1= )) +#else +#define CANONICAL_MASK(vaddr_bits) GENMASK_ULL(63, vaddr_bits) +#endif + +/* + * To make an address canonical either set or clear the bits defined by the + * CANONICAL_MASK(). Clear the bits for userspace addresses if the top add= ress + * bit is a zero. Set the bits for kernel addresses if the top address bit= is a + * one. + */ static __always_inline u64 __canonical_address(u64 vaddr, u8 vaddr_bits) { - return ((s64)vaddr << (64 - vaddr_bits)) >> (64 - vaddr_bits); + return (vaddr & BIT_ULL(63)) ? vaddr | CANONICAL_MASK(vaddr_bits) : + vaddr & ~CANONICAL_MASK(vaddr_bits); } =20 static __always_inline u64 __is_canonical_address(u64 vaddr, u8 vaddr_bits) --=20 2.52.0 From nobody Sat Feb 7 23:11:08 2026 Received: from mail-244122.protonmail.ch (mail-244122.protonmail.ch [109.224.244.122]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5766534EF0D for ; Mon, 12 Jan 2026 17:28:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=109.224.244.122 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238912; cv=none; b=JmMSO06ZtPozFg2gYPo6w9TnHnaZAfQz5DvUx0ZY7lQiZ0BIc8nUT0o/gU8xjSUt2RKRame4+E6ii8L7OLFfkLF2Xbk6tF4QNJNp5OtI2GmbEanWbP26Bdc/PF/Jxjg/Zxd5KwLhq5Ac3tElaTQ1ibbDBNugMpTRDoJalhs3Ea0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238912; c=relaxed/simple; bh=cLU1yDlyIufiMciF88eGTnMUwfesXy9yINZ4eeqk2+I=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JbpYa1RHS3rP9xWyKHlpUEUVERS7KfcISJT1zBzliLg950UMEBfEAGExV8g8dgNVwnRiw9049KnYM6U6LXcXCzX8LdoVEPKeMoKtj1Wc25VkZtG6JAKVBDkD2SghE4g60KhWK9KydYE5Zjc0n795ZX58Wnt6o45tX31geCpPhNU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=bI4dr47+; arc=none smtp.client-ip=109.224.244.122 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="bI4dr47+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1768238898; x=1768498098; bh=tF6uZeqRtsc0uhEqsykdQtAU19tdI7CeSMEK1HyPxRA=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=bI4dr47+xRCLvtcsrhtgqXqw2ZjCjbd2gZQ6cVMPKcNGuo1WJHUcJI/jkmKU4lcyg pG7F82zV/I2yyJP4eQyRRkzVbuzOk54izHwiIqO41dOI/ap4FoVG2+ORE5Z39H0wZk TmnmNZ76D/nrR+aZWj8Tv6tPVb5y8LAIJJAd7cnwQPIDHhqkD/sRl/e43XZ1Nss18l idBOQvCCKRSo2nQ7EG3BpmE6CjnNFZYD38dkAVjlqduYm7PqZEx+Kwazrv0aZF3N3J u9SI6l780MyFBed/IRpJrWR52k6pqTKkSbnCVLJm2qyMaL7ei/PYPnm0+kouzTtj3z pTv/y+lqw6j+g== Date: Mon, 12 Jan 2026 17:28:14 +0000 To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Maciej Wieczor-Retman , Alexander Potapenko , linux-kernel@vger.kernel.org Subject: [PATCH v8 10/14] x86/mm: LAM initialization Message-ID: <4ff09ec1753ea22798eb323584c42c1b531795e6.1768233085.git.m.wieczorretman@pm.me> In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: 5af161bc566890375aff104786603ef8f466963e Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maciej Wieczor-Retman To make use of KASAN's tag based mode on x86, Linear Address Masking (LAM) needs to be enabled. To do that the 28th bit in CR4 has to be set. Set the bit in early memory initialization. When launching secondary CPUs the LAM bit gets lost. To avoid this add it in a mask in head_64.S. The bitmask permits some bits of CR4 to pass from the primary CPU to the secondary CPUs without being cleared. Signed-off-by: Maciej Wieczor-Retman Acked-by: Alexander Potapenko --- Changelog v7: - Add Alexander's acked-by tag. Changelog v6: - boot_cpu_has() -> cpu_feature_enabled() arch/x86/kernel/head_64.S | 3 +++ arch/x86/mm/init.c | 3 +++ 2 files changed, 6 insertions(+) diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index 21816b48537c..c5a0bfbe280d 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -209,6 +209,9 @@ SYM_INNER_LABEL(common_startup_64, SYM_L_LOCAL) * there will be no global TLB entries after the execution." */ movl $(X86_CR4_PAE | X86_CR4_LA57), %edx +#ifdef CONFIG_ADDRESS_MASKING + orl $X86_CR4_LAM_SUP, %edx +#endif #ifdef CONFIG_X86_MCE /* * Preserve CR4.MCE if the kernel will enable #MC support. diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c index 8bf6ad4b9400..a8442b255481 100644 --- a/arch/x86/mm/init.c +++ b/arch/x86/mm/init.c @@ -764,6 +764,9 @@ void __init init_mem_mapping(void) probe_page_size_mask(); setup_pcid(); =20 + if (cpu_feature_enabled(X86_FEATURE_LAM) && IS_ENABLED(CONFIG_KASAN_SW_TA= GS)) + cr4_set_bits_and_update_boot(X86_CR4_LAM_SUP); + #ifdef CONFIG_X86_64 end =3D max_pfn << PAGE_SHIFT; #else --=20 2.52.0 From nobody Sat Feb 7 23:11:08 2026 Received: from mail-10628.protonmail.ch (mail-10628.protonmail.ch [79.135.106.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 44B3137BE7E for ; Mon, 12 Jan 2026 17:28:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=79.135.106.28 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238918; cv=none; b=ipmmxAPu0n0mrCoM3wgY2gV6oFa+3i3fKiiiq1r/Xpd7ghPCMc3slhuw4X8xGYK+qD/cDyrfT52UdEA7AznChvXYS+jwPUCXCcxHAPHM8/tK8bFd5qNImNz6kWIB+iAE/waTE/WGqLdtDU+JPErfclwZuLkhK8wMdBgutTeTYfU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238918; c=relaxed/simple; bh=mdgk9M23toUSSdVjaABscn4nritKLnqb4cFgULxtoho=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=nwW4kIanSnuqgaqaoPJPvuRmOci44yCEbUdEXRGpm4JHpsT42hj5I9m2EvJQ5+pwEzcy+51PXQIahi3XBdHvTejDYuqY971IiaZBCI25JQxnSnCout56NR0PldVyLazFjNlYExWm70B9S/hxNuqX9vKTwYfuC+LHaAivyurwqqI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=Mz3lwR5F; arc=none smtp.client-ip=79.135.106.28 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="Mz3lwR5F" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1768238905; x=1768498105; bh=aRRLcM1Q5eZzMoX1A6R6c/uTaz4KswX3LOgQlrenxtQ=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=Mz3lwR5FN5Jg6SuNhhhuaft0NwPkVkAb4k+T8GF5Nb0VEMlQubO5AMjPwPzoXZ3+j vvBucl3JF2xYYGLuN5YwyDUaSEsZS0UqtSOvEDb/Cnjd9JEFBoSn3hhFupSbvkwk7J snmkB2nqD3tFqGqaYKS3Lli2HKktDp5A9NuTooxCN5KuEYfkfriF8wZUo3ca1HKHpL 1QTfcWg/XqM7AbH7+zWNvXNypzRTIx9uj0XW+IB8NyqbszjYY1CQnZeXaWY6T9rC2y cXVx6V2goWqSzHHfMXayVdUoXMKoZijzbUkW7UdFA4d9OwAZnEwEhjjcJxAccBirDd XyzdM7oGcTnGQ== Date: Mon, 12 Jan 2026 17:28:19 +0000 To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Maciej Wieczor-Retman , Andrey Konovalov , linux-kernel@vger.kernel.org Subject: [PATCH v8 11/14] x86: Minimal SLAB alignment Message-ID: <1e1283ac47effe841f37b6a877842a7bf1948abe.1768233085.git.m.wieczorretman@pm.me> In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: 664d84d79fc31bfc3bbc0bbed45ff5fc4e06d605 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maciej Wieczor-Retman 8 byte minimal SLAB alignment interferes with KASAN's granularity of 16 bytes. It causes a lot of out-of-bounds errors for unaligned 8 byte allocations. Compared to a kernel with KASAN disabled, the memory footprint increases because all kmalloc-8 allocations now are realized as kmalloc-16, which has twice the object size. But more meaningfully, when compared to a kernel with generic KASAN enabled, there is no difference. Because of redzones in generic KASAN, kmalloc-8' and kmalloc-16' object size is the same (48 bytes). So changing the minimal SLAB alignment of the tag-based mode doesn't have any negative impact when compared to the other software KASAN mode. Adjust x86 minimal SLAB alignment to match KASAN granularity size. Signed-off-by: Maciej Wieczor-Retman Reviewed-by: Andrey Konovalov --- Changelog v6: - Add Andrey's Reviewed-by tag. Changelog v4: - Extend the patch message with some more context and impact information. Changelog v3: - Fix typo in patch message 4 -> 16. - Change define location to arch/x86/include/asm/cache.c. arch/x86/include/asm/cache.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/include/asm/cache.h b/arch/x86/include/asm/cache.h index 69404eae9983..3232583b5487 100644 --- a/arch/x86/include/asm/cache.h +++ b/arch/x86/include/asm/cache.h @@ -21,4 +21,8 @@ #endif #endif =20 +#ifdef CONFIG_KASAN_SW_TAGS +#define ARCH_SLAB_MINALIGN (1ULL << KASAN_SHADOW_SCALE_SHIFT) +#endif + #endif /* _ASM_X86_CACHE_H */ --=20 2.52.0 From nobody Sat Feb 7 23:11:08 2026 Received: from mail-10629.protonmail.ch (mail-10629.protonmail.ch [79.135.106.29]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 887B037BE9F for ; Mon, 12 Jan 2026 17:28:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=79.135.106.29 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238913; cv=none; b=Fq1x3ltZk7z7kRRNzZaOs0BhoKJtJMXMo73iR+kkV8X3Zrtq3bA4j231iOvmB9xifLxbrosxSGVVatqVxIL/kH6N0jZeH/2oJfkr8CemWIlvZs2rA6UFAJVdgYvg0A7BabXGlDTPFRtVGwYs/JTMQjsJVoaDs4Se6OuEf5vLy5o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238913; c=relaxed/simple; bh=bg/1Xgfn1E0DEXNbS9d/rzxNmu9VvpCFlIYq9KHMtu4=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=upICIwXwd6rkTJ+3lD6bMiy0H2JmOXs81u6+eavIAmS/hGKhEFvP2roGJWu7kTh6FngKY0kOTO/NFOwm/sMQAuNO9Q55qChHS0ZMXfUmlJ1HXsJzZ4bYtAgEsKKo3D3XMbMNRVEkxYIg5vYFJBVJFj73rRkG2VjWUVHz3X/cFNY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=neR07E+J; arc=none smtp.client-ip=79.135.106.29 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="neR07E+J" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1768238909; x=1768498109; bh=bg/1Xgfn1E0DEXNbS9d/rzxNmu9VvpCFlIYq9KHMtu4=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=neR07E+Jnlg+uKT0Q4Qubayl3FT+ONbfw/RQtxLwbd2s/UeKOK7bBvXT/+o/gc3tB kbhvs41hlEgd8Uj7zNq5O6gkUMYnsaA3PvC2ZeX4P2vC+ylJDxfhd9fO/ZjxTYLtsV 8fY3cwV5lOUDRQlUFEkA++Ghk4lynTDT/ftHTlcD0ZFc7Lv8otiP7h34gTOw2wpaw2 J5/VeWyUpG0URq+RWwtmDmfQkGvubSkNW2g6nTHXgTCm+BagDnnsBTP8vZP1lnqcpS Q/kKJWf/DTAeAPMq5km6HFPyUOaM5ih1ovjMRpVw0OxJE1DGb3rYQwaoJhb1UeHQqM Bv/t3y4sDB1DQ== Date: Mon, 12 Jan 2026 17:28:24 +0000 To: Catalin Marinas , Will Deacon From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Maciej Wieczor-Retman , Alexander Potapenko , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v8 12/14] arm64: Unify software tag-based KASAN inline recovery path Message-ID: <85ee02ae536a4c2e95f7d2609c5893d99cd64a00.1768233085.git.m.wieczorretman@pm.me> In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: abd5dd7e650f6c0c9201b312154c351ebdd0b398 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maciej Wieczor-Retman To avoid having a copy of a long comment explaining the intricacies of the inline KASAN recovery system and issues for every architecture that uses the software tag-based mode, a unified kasan_die_unless_recover() function was added. Use kasan_die_unless_recover() in the kasan brk handler to cleanup the long comment, that's kept in the non-arch KASAN code. Signed-off-by: Maciej Wieczor-Retman Acked-by: Catalin Marinas Acked-by: Alexander Potapenko --- Changelog v7: - Add Alexander's Acked-by tag. Changelog v6: - Add Catalin's Acked-by tag. Changelog v5: - Split arm64 portion of patch 13/18 into this one. (Peter Zijlstra) arch/arm64/kernel/traps.c | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/arch/arm64/kernel/traps.c b/arch/arm64/kernel/traps.c index 914282016069..e076753576e0 100644 --- a/arch/arm64/kernel/traps.c +++ b/arch/arm64/kernel/traps.c @@ -1071,22 +1071,7 @@ int kasan_brk_handler(struct pt_regs *regs, unsigned= long esr) =20 kasan_report(addr, size, write, pc); =20 - /* - * The instrumentation allows to control whether we can proceed after - * a crash was detected. This is done by passing the -recover flag to - * the compiler. Disabling recovery allows to generate more compact - * code. - * - * Unfortunately disabling recovery doesn't work for the kernel right - * now. KASAN reporting is disabled in some contexts (for example when - * the allocator accesses slab object metadata; this is controlled by - * current->kasan_depth). All these accesses are detected by the tool, - * even though the reports for them are not printed. - * - * This is something that might be fixed at some point in the future. - */ - if (!recover) - die("Oops - KASAN", regs, esr); + kasan_die_unless_recover(recover, "Oops - KASAN", regs, esr, die); =20 /* If thread survives, skip over the brk instruction and continue: */ arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE); --=20 2.52.0 From nobody Sat Feb 7 23:11:08 2026 Received: from mail-24416.protonmail.ch (mail-24416.protonmail.ch [109.224.244.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 192B737BE85 for ; Mon, 12 Jan 2026 17:28:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=109.224.244.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238918; cv=none; b=YJLnFX7BvyB5m75dQ4xJ8LGZ1nHGlRX5mQDiMe4/35LyW5+sMw2Z12nY1v/pJtlfwngPCQix7bwg/YtasFUAF/M0l943EXaUNC9I2OwsTAU1cvBtP8HZWzcFTOL1cyyIvhx9+4Md6NTc2p2vXNzW0mStL1mCGQve/M58nWOOZyA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238918; c=relaxed/simple; bh=ZhcnD9Qe+0mN7XIFemCzbh7j08mLcyGZjKS1+S1cDfY=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QyUp2s6p+EyNm6SPT9rgQI4+jzD0vq8NPwvmDTXdchVI2dc+SeuVuCjD51LXSRnNcelhG0cbjHqLHWapY8JeOuVf5Z2Go20sHDr5ID0OvQ/1msK4rqOlTNpj3s+vNNfxULboXFUUoe2j1g/qnymSYABHBduRCn7TNf30AdcgizA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=G4blWAFk; arc=none smtp.client-ip=109.224.244.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="G4blWAFk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1768238912; x=1768498112; bh=/xH0MFGMknW13Vj90S8r/ENniLRp7vgZ605IVSOXpxk=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=G4blWAFk5byNfXW6sc8JClrQD1Yo2joQbK9NwSd9/F+bao7hBWObosKIhX0h+Dzm+ l7NnKw1ApP3F/gjuBosm6jHB3W1LMFULfRIbQ/thydJ3OPRh0CsvfB1qxJDMFknnfp y2K4tUyJ8k63pdYGoQt4FtIhqa7Y9Us1pSZCf7Qqoe80joX+Inqsy2f9OOdKYcbJld U8Y++Psvuv+5YoSxY6GVJ7IDbiS9b9x8F4reEedI2UgMdZQg4/8uPbSxJIbhwADE00 2Qs1U9Ygtp/XXT0oeDwuIWKs5jW0kG00mCIc4+9GHqTxjo7exCxEgveBBWh4grh4Y4 UpkbwRx2UxEBw== Date: Mon, 12 Jan 2026 17:28:29 +0000 To: Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Maciej Wieczor-Retman , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v8 13/14] x86/kasan: Logical bit shift for kasan_mem_to_shadow Message-ID: In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: ebf057f553a2e357d38ef3e18edf71ca7fe2c8c0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maciej Wieczor-Retman The tag-based KASAN adopts an arithemitc bit shift to convert a memory address to a shadow memory address. While it makes a lot of sense on arm64, it doesn't work well for all cases on x86 - either the non-canonical hook becomes quite complex for different paging levels, or the inline mode would need a lot more adjustments. Thus the best working scheme is the logical bit shift and non-canonical shadow offset that x86 uses for generic KASAN, of course adjusted for the increased granularity from 8 to 16 bytes. Add an arch specific implementation of kasan_mem_to_shadow() that uses the logical bit shift. The non-canonical hook tries to calculate whether an address came from kasan_mem_to_shadow(). First it checks whether this address fits into the legal set of values possible to output from the mem to shadow function. Tie both generic and tag-based x86 KASAN modes to the address range check associated with generic KASAN. Signed-off-by: Maciej Wieczor-Retman --- Changelog v7: - Redo the patch message and add a comment to __kasan_mem_to_shadow() to provide better explanation on why x86 doesn't work well with the arithemitc bit shift approach (Marco). Changelog v4: - Add this patch to the series. arch/x86/include/asm/kasan.h | 15 +++++++++++++++ mm/kasan/report.c | 5 +++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kasan.h b/arch/x86/include/asm/kasan.h index eab12527ed7f..9b7951a79753 100644 --- a/arch/x86/include/asm/kasan.h +++ b/arch/x86/include/asm/kasan.h @@ -31,6 +31,21 @@ #include =20 #ifdef CONFIG_KASAN_SW_TAGS +/* + * Using the non-arch specific implementation of __kasan_mem_to_shadow() w= ith a + * arithmetic bit shift can cause high code complexity in KASAN's non-cano= nical + * hook for x86 or might not work for some paging level and KASAN mode + * combinations. The inline mode compiler support could also suffer from h= igher + * complexity for no specific benefit. Therefore the generic mode's logical + * shift implementation is used. + */ +static inline void *__kasan_mem_to_shadow(const void *addr) +{ + return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) + + KASAN_SHADOW_OFFSET; +} + +#define kasan_mem_to_shadow(addr) __kasan_mem_to_shadow(addr) #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)) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index b5beb1b10bd2..db6a9a3d01b2 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -642,13 +642,14 @@ void kasan_non_canonical_hook(unsigned long addr) const char *bug_type; =20 /* - * For Generic KASAN, kasan_mem_to_shadow() uses the logical right shift + * For Generic KASAN and Software Tag-Based mode on the x86 + * architecture, kasan_mem_to_shadow() uses the logical right shift * and never overflows with the chosen KASAN_SHADOW_OFFSET values (on * both x86 and arm64). Thus, the possible shadow addresses (even for * bogus pointers) belong to a single contiguous region that is the * result of kasan_mem_to_shadow() applied to the whole address space. */ - if (IS_ENABLED(CONFIG_KASAN_GENERIC)) { + if (IS_ENABLED(CONFIG_KASAN_GENERIC) || IS_ENABLED(CONFIG_X86_64)) { if (addr < (unsigned long)kasan_mem_to_shadow((void *)(0ULL)) || addr > (unsigned long)kasan_mem_to_shadow((void *)(~0ULL))) return; --=20 2.52.0 From nobody Sat Feb 7 23:11:08 2026 Received: from mail-24418.protonmail.ch (mail-24418.protonmail.ch [109.224.244.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2B39037C0E6 for ; Mon, 12 Jan 2026 17:28:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=109.224.244.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238925; cv=none; b=m8/rh/2e8ARWdANGrUr+P62x328sjCUTTxqeVcsT8Lo4JGIhP9zrIhGSUqWy8Mjja1GVEDpbgcDBZXinxfm5u4KFjoepXQf3lk2RiFyNlYtKfPx4+pzayFVbdsk/wY1U9VRhUlyAhuJspU/U4HQvER5PEuRqRylFVUkcBnTQIVk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768238925; c=relaxed/simple; bh=TqoqDWh1Xv/snyqJ7tgj5VnvpvnAsaidNe08mTewDDw=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lVsNi0moTCOVoIOvcNutjhYphvJzvSO/q9SixT650PK6Mv7fAOlM6q5QF7EzoXm5e+K9tnaOBmgEeMzKUSrXwBI7mSNAJmRdUPEcYvoskykA5PFskfP+/gvSAN06MWRdN0gj8GC37ev3L5dkHPJg25Cckkk5/GJyCoWNhKgPL5Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=YVS1R785; arc=none smtp.client-ip=109.224.244.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="YVS1R785" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1768238917; x=1768498117; bh=smEYUjE4TiZwGsUIUGvN4uRam6KETrr7zjszlbfCqLA=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=YVS1R785rxnN/IS8mi4l8XQCGj6g5l5DzG6ohgtyWfpjIByzqp4gVMXLp0t3y884x fLX9bO3h4Z9aAdZVDTOotoEjPvv2bnUuDCM8Vng9Gpr83EDl7i4SCfu3FnrAxKnu34 bSNDah2R3Bwgr2X6UJnPMh5XjzKAsiFYc+vpqdPSanYb8M/TqT29zf1ui1vfP6OSRZ FcCZjR8Scb0pDe98nB9zBpHCG0434xvMaXrYM4sF7l6NdOylMLL5GQwjtECg4YOSqV ClwudsMgMcArBIRODBbIxtKKtENjQp3cnkpM1q/g2O9kJ45I0zZA9VqK/3YGY/m9zH y3JKlIr4h8e6Q== Date: Mon, 12 Jan 2026 17:28:35 +0000 To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Jonathan Corbet , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andy Lutomirski , Peter Zijlstra , Andrew Morton From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Maciej Wieczor-Retman , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kasan-dev@googlegroups.com Subject: [PATCH v8 14/14] x86/kasan: Make software tag-based kasan available Message-ID: <5b46822936bf9bf7e5cf5d1b57f936345c45a140.1768233085.git.m.wieczorretman@pm.me> In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: 7114c3fdb473fc3c6e894749edc0df69710932f4 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maciej Wieczor-Retman Make CONFIG_KASAN_SW_TAGS available for x86 machines if they have ADDRESS_MASKING enabled (LAM) as that works similarly to Top-Byte Ignore (TBI) that allows the software tag-based mode on arm64 platform. The value for sw_tags KASAN_SHADOW_OFFSET was calculated by rearranging the formulas for KASAN_SHADOW_START and KASAN_SHADOW_END from arch/x86/include/asm/kasan.h - the only prerequisites being KASAN_SHADOW_SCALE_SHIFT of 4, and KASAN_SHADOW_END equal to the one from KASAN generic mode. Set scale macro based on KASAN mode: in software tag-based mode 16 bytes of memory map to one shadow byte and 8 in generic mode. Disable CONFIG_KASAN_INLINE and CONFIG_KASAN_STACK when CONFIG_KASAN_SW_TAGS is enabled on x86 until the appropriate compiler support is available. Signed-off-by: Maciej Wieczor-Retman --- Changelog v7: - Add a paragraph to the patch message explaining how the various addresses and the KASAN_SHADOW_OFFSET were calculated. Changelog v6: - Don't enable KASAN if LAM is not supported. - Move kasan_init_tags() to kasan_init_64.c to not clutter the setup.c file. - Move the #ifdef for the KASAN scale shift here. - Move the gdb code to patch "Use arithmetic shift for shadow computation". - Return "depends on KASAN" line to Kconfig. - Add the defer kasan config option so KASAN can be disabled on hardware that doesn't have LAM. Changelog v4: - Add x86 specific kasan_mem_to_shadow(). - Revert x86 to the older unsigned KASAN_SHADOW_OFFSET. Do the same to KASAN_SHADOW_START/END. - Modify scripts/gdb/linux/kasan.py to keep x86 using unsigned offset. - Disable inline and stack support when software tags are enabled on x86. Changelog v3: - Remove runtime_const from previous patch and merge the rest here. - Move scale shift definition back to header file. - Add new kasan offset for software tag based mode. - Fix patch message typo 32 -> 16, and 16 -> 8. - Update lib/Kconfig.kasan with x86 now having software tag-based support. Changelog v2: - Remove KASAN dense code. Documentation/arch/x86/x86_64/mm.rst | 6 ++++-- arch/x86/Kconfig | 4 ++++ arch/x86/boot/compressed/misc.h | 1 + arch/x86/include/asm/kasan.h | 5 +++++ arch/x86/mm/kasan_init_64.c | 6 ++++++ lib/Kconfig.kasan | 3 ++- 6 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Documentation/arch/x86/x86_64/mm.rst b/Documentation/arch/x86/= x86_64/mm.rst index a6cf05d51bd8..ccbdbb4cda36 100644 --- a/Documentation/arch/x86/x86_64/mm.rst +++ b/Documentation/arch/x86/x86_64/mm.rst @@ -60,7 +60,8 @@ Complete virtual memory map with 4-level page tables ffffe90000000000 | -23 TB | ffffe9ffffffffff | 1 TB | ... unused= hole ffffea0000000000 | -22 TB | ffffeaffffffffff | 1 TB | virtual me= mory map (vmemmap_base) ffffeb0000000000 | -21 TB | ffffebffffffffff | 1 TB | ... unused= hole - ffffec0000000000 | -20 TB | fffffbffffffffff | 16 TB | KASAN shad= ow memory + ffffec0000000000 | -20 TB | fffffbffffffffff | 16 TB | KASAN shad= ow memory (generic mode) + fffff40000000000 | -8 TB | fffffbffffffffff | 8 TB | KASAN shad= ow memory (software tag-based mode) __________________|____________|__________________|_________|___________= _________________________________________________ | | Identical = layout to the 56-bit one from here on: @@ -130,7 +131,8 @@ Complete virtual memory map with 5-level page tables ffd2000000000000 | -11.5 PB | ffd3ffffffffffff | 0.5 PB | ... unused= hole ffd4000000000000 | -11 PB | ffd5ffffffffffff | 0.5 PB | virtual me= mory map (vmemmap_base) ffd6000000000000 | -10.5 PB | ffdeffffffffffff | 2.25 PB | ... unused= hole - ffdf000000000000 | -8.25 PB | fffffbffffffffff | ~8 PB | KASAN shad= ow memory + ffdf000000000000 | -8.25 PB | fffffbffffffffff | ~8 PB | KASAN shad= ow memory (generic mode) + ffeffc0000000000 | -6 PB | fffffbffffffffff | 4 PB | KASAN shad= ow memory (software tag-based mode) __________________|____________|__________________|_________|___________= _________________________________________________ | | Identical = layout to the 47-bit one from here on: diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 80527299f859..21c71d9e0698 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -67,6 +67,7 @@ config X86 select ARCH_CLOCKSOURCE_INIT select ARCH_CONFIGURES_CPU_MITIGATIONS select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE + select ARCH_DISABLE_KASAN_INLINE if X86_64 && KASAN_SW_TAGS select ARCH_ENABLE_HUGEPAGE_MIGRATION if X86_64 && HUGETLB_PAGE && MIGRAT= ION select ARCH_ENABLE_MEMORY_HOTPLUG if X86_64 select ARCH_ENABLE_MEMORY_HOTREMOVE if MEMORY_HOTPLUG @@ -196,6 +197,8 @@ config X86 select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if X86_64 select HAVE_ARCH_KASAN_VMALLOC if X86_64 + select HAVE_ARCH_KASAN_SW_TAGS if ADDRESS_MASKING + select ARCH_NEEDS_DEFER_KASAN if ADDRESS_MASKING select HAVE_ARCH_KFENCE select HAVE_ARCH_KMSAN if X86_64 select HAVE_ARCH_KGDB @@ -410,6 +413,7 @@ config AUDIT_ARCH config KASAN_SHADOW_OFFSET hex depends on KASAN + default 0xeffffc0000000000 if KASAN_SW_TAGS default 0xdffffc0000000000 =20 config HAVE_INTEL_TXT diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/mis= c.h index fd855e32c9b9..ba70036c2abd 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -13,6 +13,7 @@ #undef CONFIG_PARAVIRT_SPINLOCKS #undef CONFIG_KASAN #undef CONFIG_KASAN_GENERIC +#undef CONFIG_KASAN_SW_TAGS =20 #define __NO_FORTIFY =20 diff --git a/arch/x86/include/asm/kasan.h b/arch/x86/include/asm/kasan.h index 9b7951a79753..b38a1a83af96 100644 --- a/arch/x86/include/asm/kasan.h +++ b/arch/x86/include/asm/kasan.h @@ -6,7 +6,12 @@ #include #include #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL) + +#ifdef CONFIG_KASAN_SW_TAGS +#define KASAN_SHADOW_SCALE_SHIFT 4 +#else #define KASAN_SHADOW_SCALE_SHIFT 3 +#endif =20 /* * Compiler uses shadow offset assuming that addresses start diff --git a/arch/x86/mm/kasan_init_64.c b/arch/x86/mm/kasan_init_64.c index 7f5c11328ec1..3a5577341805 100644 --- a/arch/x86/mm/kasan_init_64.c +++ b/arch/x86/mm/kasan_init_64.c @@ -465,4 +465,10 @@ void __init kasan_init(void) =20 init_task.kasan_depth =3D 0; kasan_init_generic(); + pr_info("KernelAddressSanitizer initialized\n"); + + if (boot_cpu_has(X86_FEATURE_LAM)) + kasan_init_sw_tags(); + else + pr_info("KernelAddressSanitizer not initialized (sw-tags): hardware does= n't support LAM\n"); } diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan index a4bb610a7a6f..d13ea8da7bfd 100644 --- a/lib/Kconfig.kasan +++ b/lib/Kconfig.kasan @@ -112,7 +112,8 @@ config KASAN_SW_TAGS =20 Requires GCC 11+ or Clang. =20 - Supported only on arm64 CPUs and relies on Top Byte Ignore. + Supported on arm64 CPUs that support Top Byte Ignore and on x86 CPUs + that support Linear Address Masking. =20 Consumes about 1/16th of available memory at kernel start and add an overhead of ~20% for dynamic allocations. --=20 2.52.0