From patchwork Thu Dec 21 20:04:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 13502596 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id EDC26C35274 for ; Thu, 21 Dec 2023 20:05:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 68BE66B007E; Thu, 21 Dec 2023 15:05:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 63B266B0080; Thu, 21 Dec 2023 15:05:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 52ACB6B0083; Thu, 21 Dec 2023 15:05:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 42D3D6B007E for ; Thu, 21 Dec 2023 15:05:03 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E3D8A140E12 for ; Thu, 21 Dec 2023 20:05:02 +0000 (UTC) X-FDA: 81591904044.10.277C7B7 Received: from out-188.mta1.migadu.com (out-188.mta1.migadu.com [95.215.58.188]) by imf07.hostedemail.com (Postfix) with ESMTP id F2F8B40006 for ; Thu, 21 Dec 2023 20:05:00 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ca6Xr6Os; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf07.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.188 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703189101; a=rsa-sha256; cv=none; b=KBjqjAQErATEVev2+5DV1QoVN1A6opgz4n6aJ8m+2OsA68FVNgZHxjRfHexK1mkp4peYpv aWfqiRkrdi5jtdwpioaAzTvKpr6RnhSzdGQ8GG453YkNjxoOHLmV7TNWRzPB5tkwIIRcIE 1NEJCu6vYdEHKJvdtQzIPiIARZMgBQE= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ca6Xr6Os; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf07.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.188 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1703189101; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+hJyqXb/2fG9kaa/Bl39ZO/TtJVAZ8OgDJUIBMdnwuM=; b=IHpT+h/zhhNWYf2G4bPXAKDZL9oh4/vXkso+ZHJm4zVoPjB9Q9U57FBr8MQVcGeG/F5i8h xvwKnVTufUMr0lYxCG5J+Ul+/y5nnp9dYWw0eSmxtafNwMMRw82cKwxx0YieVrj4FXTjzW aK+U1wnkmDY/IF/YaxEDjn/mJt/vZxk= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1703189099; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+hJyqXb/2fG9kaa/Bl39ZO/TtJVAZ8OgDJUIBMdnwuM=; b=ca6Xr6Os/Z3oFun+6KyDxiIacA2tRaVxTTFDHDXDXQXtiQWsYC5+HPech6L4MV3aDTlI07 j3QG0NjRC9B+YgnnxEmb3EgpisveCkpCHQQWA6eNmn+8rbL4RjMp1UGbU7g5fyeyc5HMZ8 qEqcpGuZ87c1j2vSIZxFI6Ddhg+Wa5s= From: andrey.konovalov@linux.dev To: Marco Elver Cc: Andrey Konovalov , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 01/11] kasan/arm64: improve comments for KASAN_SHADOW_START/END Date: Thu, 21 Dec 2023 21:04:43 +0100 Message-Id: <140108ca0b164648c395a41fbeecb0601b1ae9e1.1703188911.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: F2F8B40006 X-Stat-Signature: 3gxibm1gy5mtek3cr5p5muuozgj44t5p X-HE-Tag: 1703189100-894866 X-HE-Meta: U2FsdGVkX198v6d+qnHMaEadvuplQF8rSQnUvCNVeY6vPnCYJxfNDUi7sJaz3DGLB329mjSAs8CsyDZdoIR/aLGGmtj6F7VHQestvjF95bKrhU7Bvj8EMZVsmsuId+VBTbwNaa7eSlkPUKXQM+lCtprweo7AuiyyhJC/q7OcbV5/9N21kU4dk899y/F/XbUoaQTlvZZ6gj86DGkfzT0lgHlXrOou7GcXdCjgZ8ORSfkVKiHO+CfFA4e4pbYizvflVkHMlYUrVniK24CYP04M1n5vptWlcIUeEmF8t0UEsI3lakbIsF/9GIrwL3SVwl9Sy1KsLZY8zlBOb3I0BtrZUJEkr1ShEbYskp0csNXYuHs3DeCi1pN51LJBdPWTU1YiLuHCxYTGAGzTjMTRzyk8yQQRKjoKkg/qKO7aBihWIx/DtpQqcEfcEJBZnGlOgv6UHRAB5WfJV38G5i52LluNLINXgKXZCsOMOFX5ARUduJdsTOtlUVkQjNMvY3DrUj1bA06i/LCa/UfmhWsgR+FJoGRHBa0nk+2kEUQyX5uaegQa6e4qM+itrC2n7DYTDvkLRrCjsTyJpS8Fww+TXJbYGZvsrfBO83wZiaId8tLWXsPDnRK4daztqI1HRAWRRq1aOuXAJAha7FqW7BziashceHlzTml8OlrLs8fG/sZVEef5uhBfh6S0u3e3P3WuYI9wz/Jn31SczUPYUnrVjNX1PW9Mip4bHIXvTLsvuyypirF/K0oRc8g9durKM8M5k+4FNx5TJN1TtAVr71me4N8QpjbEPGRB0xEH6I5R+dm6qoFz2ky6BOVZ0cFt8aMjS/Grrv0LBryytCkx7W4hO18AzdiDFuK7rbyca/K0nNKgr0nG+RfiiFRVcMKAP8A4X9GhKmDquLZETTR54D7JvJFgW6KwpTophKyQwo8jIKcZ0qXpS08BU3Rp4h0vX7afIkMTXFo/LLwS8RACV6+MIIl 5a215E7R cZGUe7KVCQUkYlpW7p3Qb9SZvxz0dyAkXE2LqNeLZG677f5PH5FKv8fBgCnUZQTmw+kq5lwguX/GvL8A6hULFQN0KDbiOr6CkU9zPFDdxHJyZOLoUhXiQtQfNGstcaDeS0qwZSz/ejCyyvMHPLtngaGZWID7qvsUCQOWbnvPBBWHM3OU4jmUv78BqSpkMNprUT1PAssh6R5skPkritHOZP5l9gQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Unify and improve the comments for KASAN_SHADOW_START/END definitions from include/asm/kasan.h and include/asm/memory.h. Also put both definitions together in include/asm/memory.h. Also clarify the related BUILD_BUG_ON checks in mm/kasan_init.c. Signed-off-by: Andrey Konovalov --- arch/arm64/include/asm/kasan.h | 22 +------------------ arch/arm64/include/asm/memory.h | 38 +++++++++++++++++++++++++++------ arch/arm64/mm/kasan_init.c | 5 +++++ 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/arch/arm64/include/asm/kasan.h b/arch/arm64/include/asm/kasan.h index 12d5f47f7dbe..7eefc525a9df 100644 --- a/arch/arm64/include/asm/kasan.h +++ b/arch/arm64/include/asm/kasan.h @@ -15,29 +15,9 @@ #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) +asmlinkage void kasan_early_init(void); void kasan_init(void); - -/* - * KASAN_SHADOW_START: beginning of the kernel virtual addresses. - * KASAN_SHADOW_END: KASAN_SHADOW_START + 1/N of kernel virtual addresses, - * where N = (1 << KASAN_SHADOW_SCALE_SHIFT). - * - * KASAN_SHADOW_OFFSET: - * This value is used to map an address to the corresponding shadow - * address by the following formula: - * shadow_addr = (address >> KASAN_SHADOW_SCALE_SHIFT) + KASAN_SHADOW_OFFSET - * - * (1 << (64 - KASAN_SHADOW_SCALE_SHIFT)) shadow addresses that lie in range - * [KASAN_SHADOW_OFFSET, KASAN_SHADOW_END) cover all 64-bits of virtual - * addresses. So KASAN_SHADOW_OFFSET should satisfy the following equation: - * KASAN_SHADOW_OFFSET = KASAN_SHADOW_END - - * (1ULL << (64 - KASAN_SHADOW_SCALE_SHIFT)) - */ -#define _KASAN_SHADOW_START(va) (KASAN_SHADOW_END - (1UL << ((va) - KASAN_SHADOW_SCALE_SHIFT))) -#define KASAN_SHADOW_START _KASAN_SHADOW_START(vabits_actual) - void kasan_copy_shadow(pgd_t *pgdir); -asmlinkage void kasan_early_init(void); #else static inline void kasan_init(void) { } diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h index fde4186cc387..0f139cb4467b 100644 --- a/arch/arm64/include/asm/memory.h +++ b/arch/arm64/include/asm/memory.h @@ -65,15 +65,41 @@ #define KERNEL_END _end /* - * Generic and tag-based KASAN require 1/8th and 1/16th of the kernel virtual - * address space for the shadow region respectively. They can bloat the stack - * significantly, so double the (minimum) stack size when they are in use. + * Generic and Software Tag-Based KASAN modes require 1/8th and 1/16th of the + * kernel virtual address space for storing the shadow memory respectively. + * + * The mapping between a virtual memory address and its corresponding shadow + * memory address is defined based on the formula: + * + * shadow_addr = (addr >> KASAN_SHADOW_SCALE_SHIFT) + KASAN_SHADOW_OFFSET + * + * where KASAN_SHADOW_SCALE_SHIFT is the order of the number of bits that map + * to a single shadow byte and KASAN_SHADOW_OFFSET is a constant that offsets + * the mapping. Note that KASAN_SHADOW_OFFSET does not point to the start of + * the shadow memory region. + * + * Based on this mapping, we define two constants: + * + * KASAN_SHADOW_START: the start of the shadow memory region; + * KASAN_SHADOW_END: the end of the shadow memory region. + * + * KASAN_SHADOW_END is defined first as the shadow address that corresponds to + * the upper bound of possible virtual kernel memory addresses UL(1) << 64 + * according to the mapping formula. + * + * KASAN_SHADOW_START is defined second based on KASAN_SHADOW_END. The shadow + * memory start must map to the lowest possible kernel virtual memory address + * and thus it depends on the actual bitness of the address space. + * + * As KASAN inserts redzones between stack variables, this increases the stack + * memory usage significantly. Thus, we double the (minimum) stack size. */ #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL) -#define KASAN_SHADOW_END ((UL(1) << (64 - KASAN_SHADOW_SCALE_SHIFT)) \ - + KASAN_SHADOW_OFFSET) -#define PAGE_END (KASAN_SHADOW_END - (1UL << (vabits_actual - KASAN_SHADOW_SCALE_SHIFT))) +#define KASAN_SHADOW_END ((UL(1) << (64 - KASAN_SHADOW_SCALE_SHIFT)) + KASAN_SHADOW_OFFSET) +#define _KASAN_SHADOW_START(va) (KASAN_SHADOW_END - (UL(1) << ((va) - KASAN_SHADOW_SCALE_SHIFT))) +#define KASAN_SHADOW_START _KASAN_SHADOW_START(vabits_actual) +#define PAGE_END KASAN_SHADOW_START #define KASAN_THREAD_SHIFT 1 #else #define KASAN_THREAD_SHIFT 0 diff --git a/arch/arm64/mm/kasan_init.c b/arch/arm64/mm/kasan_init.c index 555285ebd5af..4c7ad574b946 100644 --- a/arch/arm64/mm/kasan_init.c +++ b/arch/arm64/mm/kasan_init.c @@ -170,6 +170,11 @@ asmlinkage void __init kasan_early_init(void) { BUILD_BUG_ON(KASAN_SHADOW_OFFSET != KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT))); + /* + * We cannot check the actual value of KASAN_SHADOW_START during build, + * as it depends on vabits_actual. As a best-effort approach, check + * potential values calculated based on VA_BITS and VA_BITS_MIN. + */ BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS), PGDIR_SIZE)); BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS_MIN), PGDIR_SIZE)); BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PGDIR_SIZE));