From nobody Mon Feb 9 19:54:01 2026 Received: from mail-43103.protonmail.ch (mail-43103.protonmail.ch [185.70.43.103]) (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 B3CD5347BD1 for ; Wed, 4 Feb 2026 19:20:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.103 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770232841; cv=none; b=gMSc6rl1rZg+nL8k+H2rTYYxCxdr9ly9muPLL1WFPdF2Gqc/8Da8QA7yl7I2If4d980+s4ELBrYA8QMyFALOgniZI2/WKRRKem5OvsH3oZzNdmrKKOilcdxm2zEqIUH2xoddHGS8AkuyvFr26HPuUEv9f7Wiwi0CxgSP8NJTIhg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770232841; c=relaxed/simple; bh=TS5HtBvCbt6KTCcqXsP0UzAeKBY3qi5ZazgGPQIpYGA=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=SaFHlm/bzbrSrEgJ9oDS83G43OmJCgEGKibgtRl6olGNSGAMtMdBTHi1HcsiCTqfdsIuNLpkGESaw7qDoFE/I5/jStWWZqR2vhkG+oRY7111I9opjfzb7+Jl0C6njtf3Gui76EuXiDf5+PyIQXmzSwzWa7hzDgMabwiZq2JW9rE= 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=BI+nHvXj; arc=none smtp.client-ip=185.70.43.103 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="BI+nHvXj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1770232839; x=1770492039; bh=pCJsmVtyGsdKKIuPYKpRm9fUYAKTYyOUZBJDn3h7tqU=; 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=BI+nHvXjT1Q786+BEf7U4Gp9wAhq4prsXekocJgUMDY42lD8sV9jmNhqyz4BL3a2S GY73PKeEl6vN3jJ2/yliDApPxzdm1l9UGQVkFVJ0Yg1YlF+NYdf1NmxQfk0Th5cVA3 APwHFuRkb3wNcOWYj4hYHj4m1HjTrcGarKQn3SV2FVeeqOInM8EGiDWEtrzjFx7qyK I9ObTUU+Qc3C3DafTjc9bYJijEW4d4Bz2kiEg4SdeQHakE9M6l6pan0yT1UmdLj8zg rK+JLWZ/XJbloeSrVSDU+6z1Y6gDQBJjQlykW2SxSkIw2qL+AlgbRjVCZ7svsKFqth Sht6huQE/awEw== Date: Wed, 04 Feb 2026 19:20:35 +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" From: Maciej Wieczor-Retman Cc: m.wieczorretman@pm.me, Maciej Wieczor-Retman , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org Subject: [PATCH v10 12/13] x86/kasan: Use a logical bit shift for kasan_mem_to_shadow Message-ID: <0f4fe40cd9a481e64a3337c69b4e5e925a30ac7e.1770232424.git.m.wieczorretman@pm.me> In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: 84f78230333589d3ec7afed7dc5dcd1a51c62275 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. Duplicate the generic mode check from kasan_non_canonical_hook() into the arch specific function as the calculation follows the same logic due to the same logical bit shift. Signed-off-by: Maciej Wieczor-Retman --- Changelog v9: - Rename patch title so it fits the tip standards. - Take out the x86 part from mm/kasan/report.c and put it in the arch specific function. Adjust the patch message. 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 | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/arch/x86/include/asm/kasan.h b/arch/x86/include/asm/kasan.h index c868ae734f68..90c18e30848f 100644 --- a/arch/x86/include/asm/kasan.h +++ b/arch/x86/include/asm/kasan.h @@ -31,6 +31,38 @@ #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) + +static __always_inline bool __arch_kasan_non_canonical_hook(unsigned long = addr) +{ + /* + * 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 (addr < (unsigned long)kasan_mem_to_shadow((void *)(0ULL)) || + addr > (unsigned long)kasan_mem_to_shadow((void *)(~0ULL))) + return true; + return false; +} +#define arch_kasan_non_canonical_hook(addr) __arch_kasan_non_canonical_hoo= k(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)) --=20 2.53.0