From nobody Tue Feb 10 02:45:13 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