From nobody Sun Apr 5 04:25:36 2026 Received: from mail-244123.protonmail.ch (mail-244123.protonmail.ch [109.224.244.123]) (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 839003D47D9 for ; Tue, 10 Mar 2026 17:54:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=109.224.244.123 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773165280; cv=none; b=lnDj+tZqRyqejQc34GnQGeASD0x8m3waO0n3IXBMiQ5Hd6zrziHAa6jBI2yQoNjIcUYym2bCfnvec+MXEAluZglOiDuUCuUD8fW2I8fyN3a9XwjcZqUxFQm+1hYhORtdX5Zoi76Umt58uZIBHONccNRY4sWbhv+J6S/kMYnhK7s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773165280; c=relaxed/simple; bh=V+RVEeTIiyKE2lGOZ60G1elPpCREDpubZZJqoJxO+jY=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=GEYvFkHxzFLo862/N0gNerUyVC/q9SUtiVxDJ+NtlTgjEVVWOK8zp4LaGINZT0+LNvgIIKlH2SN9UAF9bW+ipiUkBJR1ceGoIxYuWrNcXaYw4CzTbQZUwOoDmQI+g4f+XN06MUWfc1pquRvoLTlUlFfehjNKaq+Lmtm85tqGrEk= 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=Nen7Z1t0; arc=none smtp.client-ip=109.224.244.123 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="Nen7Z1t0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1773165270; x=1773424470; bh=svfiyvtEE/lqQ8xsx5lT5DL7oiLrvzGxpb3VchVEZvw=; 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=Nen7Z1t0M7fJ3iFzrDj2t5PUZSfD18+/ZHvqQecnizVXpYnj7r19JvVBfSkCojqnv ksfYwaP+ASraiJKUqcx9Uy3B1RHU/3ep2D6M0jeNkekqK086jnOiQEaUIpMc4tMHgN xWZZihkhXFr99y+Gis1eOtBq8+f1WM0XnW++9orfm/08pOQLV6jTr3fgfbwzdZ084r MhqasV3kzxV+h8t/fsLbIHHUDWnFny4kao9kDznxqBJDdLPZzxfzS8nMzc8DgYa3Av 6ZcGOHFuixL+Y0yBHRbX9H8j/n9wFxMB1ziPw0q0ePwSUO/YhjoADnJa/aLU6kFNuN GJLipxQYLTQBw== Date: Tue, 10 Mar 2026 17:54:25 +0000 To: akpm@linux-foundation.org, 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 v11 09/15] x86/kasan: Initialize KASAN raw shadow memory Message-ID: <0720e3a668219f11cd11e46eefb0717469829ee8.1773164688.git.m.wieczorretman@pm.me> In-Reply-To: References: Feedback-ID: 164464600:user:proton X-Pm-Message-ID: 55e15ba340631198bdb8c85e0cd8788689b7a3a0 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 v9: - Rename patch title so it fits the tip standards. - Add Andrey Ryabinin's Reviewed-by tag. 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.53.0