From patchwork Tue Oct 22 01:57:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13844963 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 B6691D17158 for ; Tue, 22 Oct 2024 01:59:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 383BD6B0093; Mon, 21 Oct 2024 21:59:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 334886B0096; Mon, 21 Oct 2024 21:59:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 138D16B009A; Mon, 21 Oct 2024 21:59:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E749E6B0093 for ; Mon, 21 Oct 2024 21:59:21 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8DA92C1C65 for ; Tue, 22 Oct 2024 01:59:04 +0000 (UTC) X-FDA: 82699580754.28.5884D22 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) by imf20.hostedemail.com (Postfix) with ESMTP id B6C871C0020 for ; Tue, 22 Oct 2024 01:59:00 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=KjDKq7ej; spf=pass (imf20.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729562209; 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=zwUiyMUd8hSbAOwVDn06T0GWpMaINHNufJwGBjiF7oM=; b=N8iwOA6LlK+WYRSJ7t+2rjKLO/a8BqoaxQjF4v59I3YyYtQYJFtmNoXfOwpvB0psmzyx/A uu2XGgfM2w7Gm7Ibc3LcvF2Z5QXfvSuWN8KfWICkOrJEUMaIIv3D7mnVNQzq7HLFQ6C+5H 0cypwaxlCGNdm9AstGvVuz/MNvqlFkk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729562209; a=rsa-sha256; cv=none; b=jZ+BEX48pwKGiL61vfaFHYcLkx58P6usE2M41e3jpHX5F/uVbzMgC5YuWKLmWNhnIggvPo RTW8Tf+KhF3efds+WFZE2vPhSFkBiIXWPEHBN7Bwzrdqh1BVCcadoZMUou2seK+4W5Gm0r yicxlrrNfaDhYZkSH1TJEN6uPB8uSPI= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=KjDKq7ej; spf=pass (imf20.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-71e5130832aso3535980b3a.0 for ; Mon, 21 Oct 2024 18:59:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1729562358; x=1730167158; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zwUiyMUd8hSbAOwVDn06T0GWpMaINHNufJwGBjiF7oM=; b=KjDKq7ejX6Owoq+i5B1bfMVKrlT+1gH3w2fe7Dp/KQqhr89HRx3oe8++ORlW3Ubdnp soGJoMjsa6qgyKcxlXdU8pjvupGKZOfGMt03dI0EAgRwQOxYFBljRb9xEurg7Y/k4W5i RM2J2+e3q+CvNtP6f9OsYGKPfAawivnaHE48xqDCdM8HnkXP99VVjWvxAv9a/fRlguqq Ih6pMEVh42maOMFlxIvPim7KqUKOY3h32I2LNq4S1TbJimLV7FM89S+i8XAm57d8w72l 3+mxHWAaZoHnHcEPAsbqrzG24LQCRiBb38UQJxxUcASNQdfFSVLytIWA1UW2Jmzfj9T3 r+6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729562358; x=1730167158; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zwUiyMUd8hSbAOwVDn06T0GWpMaINHNufJwGBjiF7oM=; b=Nq5wU3uzU260/rv+hHy7m4VbTYaFZBm6UEjqTRDCe+tnxjTzKlOLAjV53asx4IUwHb wJXX0ML4kAg1rldD6sW1viOnO4Q32DC+3W3Vo7QwIFirBl3+kHDvzamLm7DW9QtomicD nj22F8gDq10UJ9iB3qaWmQS9lqtr5BOuDfa1Lroqz2fxURz0GnQj6VVwaIPT6Ci+x4Wz JvCinRuVBC6gp0MAuJlC48jv72vI0f4AHpgdnXqczkCBovhAifbPFcMR+eMBMLdiAous XUan7V/0QEskLMgAgAOA3UzhjTlmdC7uo8YVdHDYf/PQdx8VOxC09IG+pN+JuN9AOnjH /XOg== X-Forwarded-Encrypted: i=1; AJvYcCVCmlG6nLMHd1wr8zZIUqmPY/kUX2iTb95itHlpNtjzn3yKzQW3mBXCgnB36iDncq8FnNYi0sVMRA==@kvack.org X-Gm-Message-State: AOJu0YxxGIl6fqaj4HGa7h5YlGq23QPBIlJ7ch4mG6/hmXy+AmUtfLjb pzIdjalQSHCfzNIV+BEXV66AH5RXp4BFceKnNuD7eur5kYob2W9IAq64KWzaKVE= X-Google-Smtp-Source: AGHT+IF/kNxKO+HoCV5OxccF94G79KMrSvpY4AWhKUS70q2xmcQQgKjPTjZVS3p5yxthRrvUotX1BA== X-Received: by 2002:a05:6a00:3d08:b0:71e:1314:899a with SMTP id d2e1a72fcca58-71ea31e4c76mr18906065b3a.20.1729562358073; Mon, 21 Oct 2024 18:59:18 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71ec132ffdcsm3600710b3a.46.2024.10.21.18.59.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 18:59:17 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , kasan-dev@googlegroups.com Cc: llvm@lists.linux.dev, Catalin Marinas , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti , Will Deacon , Evgenii Stepanov , Andrew Morton , linux-arm-kernel@lists.infradead.org, Samuel Holland Subject: [PATCH v2 1/9] kasan: sw_tags: Use arithmetic shift for shadow computation Date: Mon, 21 Oct 2024 18:57:09 -0700 Message-ID: <20241022015913.3524425-2-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241022015913.3524425-1-samuel.holland@sifive.com> References: <20241022015913.3524425-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: B6C871C0020 X-Stat-Signature: kru8w35tntkrstwmnn3fzi6ctsmmfa9n X-HE-Tag: 1729562340-471372 X-HE-Meta: U2FsdGVkX1/FoqWsJfn0A8V7R4WWJP2fhUmCWZAFl1igfg0gL+TKPOrCz09WAyAsg2Oy3R5r5raKgFAKBlldwBZ3TtkzW+sQB03I/Q1vqffQLMThnNYCocISt8sUavd9+n7Cu82xQ7ghmbjYmufKkaz9Q1c/eKJIIULP9hz6fKoHS209h0RNw+0p6BBJ9d0dtkqvzUBWuQ5Jxq3JGKf0QyQYoJyvCQoakKd7kCWdX45cDXvK22OFjQtpzNgF3yQo4PGP0kBx1Djwiyg/jhMAbDCi33H/STHDpQ+xMWZifL9cd7ZVBhb+r8XfreS+ckctQShwPys5fBytF94mL8xKGfrm9Zcy6cgqmjn2tZmXnXqIdxW3Brhw4kH2WSkX8vZ1AOtJqQ48xbgJuCNFzvlT6SvvtMVDT8eiCLt8mKVe3xiBqrWyJgPGjjnjRLYMmOjnoUcVzm+azY5qb64kOjBUJ7VzPLasdfbKrN34sni3M/rClqA1Cs0gaspm/SI3lLjVFo9kRLq+gCxHNPjecPsKLEhKFQN4wweoxC7QwSct7VQpLSGffO33otrXvtC1AwArD+PLP+xvSMYleTXQ905xzFHoWjLLLrcLCG3+HsAGDZ2nWX+WsovG/1BQT9HpuRGYfGUQmfEPRCyAIRY94In/OTrmPIZIWcWCKLFYgPXHnlA0wsLUtO5PaGR1kQrmEqaUvJ87Oq0tm3+w0Pr0h26DdM6N7D/sRBDxOkBfpjfWjb+X47ZiJwG7u80g33ZPNxp5/7a9Iqyb2eTPgcAK6gW6DB57vwwR5somkGpPIH/IxSVn0JYHe4ZZVFx8ZhLYM64EPmnhi4cqTbck5W3WPGgbg71nabV2ozhWiScK5m91cWLQvFS9Ne66MebXzqQUMLfRQ4Zc0DDn5wDvfytZ4OUgJ7ZMi266317C2tACYi3HjX9cBZRyLYh3BtpnnWSSLYH+g+cddwQivMt55mSncjQ dUAZnV2H p+G3KKMrClcD36NEM/8ApjGuy/FhuRhXv0SQ4ZE80TwZU3VQF4zKllWUquzhJlyIs6DRqYpdvgiypl+okiP4MCmnQ8TwS3j843YhuWg1eagJR3dQvny0KgplhjWm5S1jvMOtMrk5TJ+G40QOzoBbNIP7A60kjRMnghEMOJFWJLm8ZYcurrNHkWLiRisRD+wiQpJseSMWxJf15zyGRhxAGDaNSaGmUy59328FG3TrPtCRIgrPGTHnHswhP5vG7ELqP3Jpf0rRZHBx3gJ8gVuF6jBJKpiMJJRlZtHhP1Tbgyl/QYnHNvymmRlB5ghrfrsTwVgpib6Jbu2M35HTI8Q6fedFGo76187wJgXwc/EklKXNxihNDBupGmq01jwAh08LcfkVvgtmkCjB7vTCVQrTXli+JfMvxkNXHzr/eXVEiN3ftL7nmBpN1ali+njlbYR0hifI4FKdBeSUXfALFecA8XwdTKA== 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: 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 we have 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 is easier to understand. KASAN_SHADOW_OFFSET becomes a canonical memory address, and the shifted pointer becomes a negative offset, so KASAN_SHADOW_OFFSET == 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/Transforms/Instrumentation/AddressSanitizer.cpp#L1316 [1] Link: https://github.com/llvm/llvm-project/blob/llvmorg-20-init/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp#L895 [2] Link: https://github.com/llvm/llvm-project/blob/llvmorg-20-init/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp#L669 [3] Signed-off-by: Samuel Holland --- Changes in v2: - Improve the explanation for how KASAN_SHADOW_END is derived - Update the range check in kasan_non_canonical_hook() arch/arm64/Kconfig | 10 +++++----- arch/arm64/include/asm/memory.h | 17 +++++++++++++++-- arch/arm64/mm/kasan_init.c | 7 +++++-- include/linux/kasan.h | 10 ++++++++-- mm/kasan/report.c | 22 ++++++++++++++++++---- scripts/gdb/linux/mm.py | 5 +++-- 6 files changed, 54 insertions(+), 17 deletions(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index fd9df6dcc593..6a326908c941 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -418,11 +418,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) && ARM64_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) && ARM64_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 config UNWIND_TABLES diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h index 0480c61dbb4f..a93fc9dc16f3 100644 --- a/arch/arm64/include/asm/memory.h +++ b/arch/arm64/include/asm/memory.h @@ -80,7 +80,8 @@ * 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. + * the shadow memory region, since not all possible addresses have shadow + * memory allocated for them. * * Based on this mapping, we define two constants: * @@ -89,7 +90,15 @@ * * 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. + * 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_OFFSET + * 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 shadow * memory start must map to the lowest possible kernel virtual memory address @@ -100,7 +109,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)) + KASAN_SHADOW_OFFSET) +#else +#define KASAN_SHADOW_END KASAN_SHADOW_OFFSET +#endif #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 diff --git a/arch/arm64/mm/kasan_init.c b/arch/arm64/mm/kasan_init.c index b65a29440a0c..6836e571555c 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 != - KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT))); + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + BUILD_BUG_ON(KASAN_SHADOW_OFFSET != + KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT))); + else + BUILD_BUG_ON(KASAN_SHADOW_OFFSET != 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 00a3bf7c0d8f..03b440658817 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -58,8 +58,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 = (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT); + else + scaled = (void *)((long)addr >> KASAN_SHADOW_SCALE_SHIFT); + + return KASAN_SHADOW_OFFSET + scaled; } #endif diff --git a/mm/kasan/report.c b/mm/kasan/report.c index b48c768acc84..c08097715686 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -644,15 +644,29 @@ void kasan_report_async(void) */ void kasan_non_canonical_hook(unsigned long addr) { + unsigned long max_shadow_size = BIT(BITS_PER_LONG - KASAN_SHADOW_SCALE_SHIFT); unsigned long orig_addr; const char *bug_type; /* - * All addresses that came as a result of the memory-to-shadow mapping - * (even for bogus pointers) must be >= KASAN_SHADOW_OFFSET. + * With the default kasan_mem_to_shadow() algorithm, all addresses + * returned by the memory-to-shadow mapping (even for bogus pointers) + * must be within a certain displacement from KASAN_SHADOW_OFFSET. + * + * For Generic KASAN, the displacement is unsigned, so + * KASAN_SHADOW_OFFSET is the smallest possible shadow address. For + * Software Tag-Based KASAN, the displacement is signed, so + * KASAN_SHADOW_OFFSET is the center of the range. */ - if (addr < KASAN_SHADOW_OFFSET) - return; + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) { + if (addr < KASAN_SHADOW_OFFSET || + addr >= KASAN_SHADOW_OFFSET + max_shadow_size) + return; + } else { + if (addr < KASAN_SHADOW_OFFSET - max_shadow_size / 2 || + addr >= KASAN_SHADOW_OFFSET + max_shadow_size / 2) + return; + } orig_addr = (unsigned long)kasan_shadow_to_mem((void *)addr); 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 = gdb.parse_and_eval("_end") if constants.LX_CONFIG_KASAN_GENERIC or constants.LX_CONFIG_KASAN_SW_TAGS: + self.KASAN_SHADOW_OFFSET = constants.LX_CONFIG_KASAN_SHADOW_OFFSET if constants.LX_CONFIG_KASAN_GENERIC: self.KASAN_SHADOW_SCALE_SHIFT = 3 + self.KASAN_SHADOW_END = (1 << (64 - self.KASAN_SHADOW_SCALE_SHIFT)) + self.KASAN_SHADOW_OFFSET else: self.KASAN_SHADOW_SCALE_SHIFT = 4 - self.KASAN_SHADOW_OFFSET = constants.LX_CONFIG_KASAN_SHADOW_OFFSET - self.KASAN_SHADOW_END = (1 << (64 - self.KASAN_SHADOW_SCALE_SHIFT)) + self.KASAN_SHADOW_OFFSET + self.KASAN_SHADOW_END = self.KASAN_SHADOW_OFFSET self.PAGE_END = self.KASAN_SHADOW_END - (1 << (self.vabits_actual - self.KASAN_SHADOW_SCALE_SHIFT)) else: self.PAGE_END = self._PAGE_END(self.VA_BITS_MIN) From patchwork Tue Oct 22 01:57:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13844964 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 C266ED1714E for ; Tue, 22 Oct 2024 01:59:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3636A6B009A; Mon, 21 Oct 2024 21:59:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C0956B009B; Mon, 21 Oct 2024 21:59:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 139506B009C; Mon, 21 Oct 2024 21:59:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id A389D6B009A for ; Mon, 21 Oct 2024 21:59:23 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0D92AC1C59 for ; Tue, 22 Oct 2024 01:59:06 +0000 (UTC) X-FDA: 82699580292.08.48FF054 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by imf22.hostedemail.com (Postfix) with ESMTP id 5B6E7C001B for ; Tue, 22 Oct 2024 01:59:02 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=Wu+tZ3wA; dmarc=pass (policy=reject) header.from=sifive.com; spf=pass (imf22.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729562238; a=rsa-sha256; cv=none; b=QqPIJrbZLPi+QEvE00NficUz+n7L47+iPSC2SXur+p92ra+qGr3tGB8Yd1tGY7SJ49Q42I YCuIeo8AFENubY5XMoIWYEgXWKti6vzoQiU/UZyy12RojBsWPmw/DauZabxHzKf5oJsfG8 ODGTYBCnJ2IM/dsuWCFBM12CesG608k= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=Wu+tZ3wA; dmarc=pass (policy=reject) header.from=sifive.com; spf=pass (imf22.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729562238; 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=Af9tK0JLBBTeDXRGuA1c/z94Xn/abI+OOOquUfcf3I8=; b=nAIW+ltL+IRlvKkWZ3ZMrhfHk3YbazqWXOdIPtYrfBu9cKsQGjxAaM5UwP2WblnxpijaIx aehYEYTcM/uen58b5vpg4ejRaRgF2bEbsSkXxoBuNHqxFUeRjHOJIXNNsCa6dE7RKcyYVZ jG/dq4l++auptt1T8G4nLWEwimTzSis= Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-71e49ef3b2bso3629396b3a.2 for ; Mon, 21 Oct 2024 18:59:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1729562360; x=1730167160; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Af9tK0JLBBTeDXRGuA1c/z94Xn/abI+OOOquUfcf3I8=; b=Wu+tZ3wAU1QxzHMzvHPQ4QKKjoJ7gKza8WkCgeNkYst4mDFgjtzFVBdd1FIVio6nWD uUCUDm2LnYGpkRVii7YPT1CWKRIAQJ9T5hQRR2+9VxyBzGB1cIO3Mqt9Y045RKpryqrH DM8rbH8M+12Q/aOck9YrKhBgXxfz/dXzGdZbTdsTYgIm4GM0Q9v3tt+WrfUBZh3LC1IO ehrqVDzmq47kLIo2NklQXBSyVn7Rv7CO4FbFQROQ9MEH3gSpl2uYP8mTTXOTvVvUAl36 F5CPj+JRDx4vneXA2JGNKsjYr4o8RNiYuLWVNDFP/AmAsSVFir3aSydQNyKv9OkBICeU AF9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729562360; x=1730167160; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Af9tK0JLBBTeDXRGuA1c/z94Xn/abI+OOOquUfcf3I8=; b=EfOhnNCEqeHotchThPpNjXXTy91cFiZG1cy577B4K+kQd4kCTGvSnL1La0TFoUg42z 6pwjyYfZ6YTk2OYcUXOMqHUBl+1NClGrajGfiTa7T/o3PKMiAHPWOXt69X68s02dkJlp joRrNT71VUQ35ATCPCDT//95fZ8WpFPcM9c1icSADBHgkakGGWprBEaxr3fu43YaguEG Bdpt8ryuuZaJE0a5mX9sRfCQ4MftyHUVbMYTw8XoMIETPNg5rAeZkAzUdK247Vd+r55c fnGg9X0+2AjYl2gVw+2XwKhcDiTKweo6YmLQM53A6QSiIheV52j6OokP7siVR1XFWVMF fasg== X-Forwarded-Encrypted: i=1; AJvYcCVJhat4Gxo1PGckIZv/Th8AHk6HtIDJf4rVqlhtuy9LIK4Hrqm6k/6rmnuBugwe5kjjXun5Ld/IdA==@kvack.org X-Gm-Message-State: AOJu0YwNZYb1AzTkNi3bev2OSEZ7Q+mlTTnWtNM5tx2smruWgdPkpL/X cUTxR3Kf/oDyVAakO0vUTU0MXwuLzXJAvKX8AxusFdXErNbiJkuIx/yER6Smq9o= X-Google-Smtp-Source: AGHT+IH5wPAzHdVfv0MMInx9ySs4Jc9bkPSFs6F+LS8CNw/7mEjClw2+t/2vEVp2yPC3jOJm28L3Qw== X-Received: by 2002:a05:6a00:4610:b0:71e:6489:d06 with SMTP id d2e1a72fcca58-71edb972451mr3011651b3a.0.1729562359943; Mon, 21 Oct 2024 18:59:19 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71ec132ffdcsm3600710b3a.46.2024.10.21.18.59.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 18:59:19 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , kasan-dev@googlegroups.com Cc: llvm@lists.linux.dev, Catalin Marinas , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti , Will Deacon , Evgenii Stepanov , Andrew Morton , linux-arm-kernel@lists.infradead.org, Samuel Holland Subject: [PATCH v2 2/9] kasan: sw_tags: Check kasan_flag_enabled at runtime Date: Mon, 21 Oct 2024 18:57:10 -0700 Message-ID: <20241022015913.3524425-3-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241022015913.3524425-1-samuel.holland@sifive.com> References: <20241022015913.3524425-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 5B6E7C001B X-Stat-Signature: wnwq6rkspa9rdjgmpmqqrmhntzyqsamx X-Rspam-User: X-HE-Tag: 1729562342-202562 X-HE-Meta: U2FsdGVkX19M5Tzo3StSCXSIQmc6MO6GrCY0elvzod9c8KcKKabw0LiMYlpAA/NSj9xXJT7fozZzfuUkzBcxHU8Ya1ZYoZVRJQrRkhAwW7pPv7cF5/rYmiAPozjKhLTZMjN0Q5LZnBHMK19C2CQyjbt0l5MfGlx0Bk0VUg2XefYsHjTCL5/FrC7z+ApjJuX2gjGzgYSmaIkA7MFaCNPDrQXJ86zEo2NI1yqtAx5P3HI+DTkDFIoUc6i+d3ZB+MMxs69aSgVln4q70IIcb7+9PfyP4VetAkDhEvEgaQnl22MWqrPh8cbrHT0wq8m1zU41GTxB96QRTDIylC/3ieHOTcPkAGSltgZ7jU2ldjpWOAOwsUe3n/vcshskyAWd9Ef3FeMBT08l7Ho8kdiylam+bFolkwJNEYOlK5ffg8DxkLQrCFTMGv+BgEMEaNbpTOdFlKX1UfkWsTnhVCGB+vNspFnAXC5DAvJMOqQd+RMVkEj/3CDiRKfq9VSWQggLj1l4BGL0G6FmJe73sT+OdbjpK/jAwlAftKayb9a23d2N64/OV+wHo/p9isor6SR8rz5nRZxYmL/cI+DUrNVFnyE7nNtPFVXf+I+dWuyVZde7nIFZWenW7QD1Ct64+eJL9Fauqv+C1YpLSKZfdKM/djvJrdpgr7aRhs7Kwlze1yPzmfvTs+YdVYvR9O2y9N9m2xUgqdWxZ6MYJxZEV7KEdUBTexEo3WesSwGs+loPkBkbZKOYXrNabvIU2zvr+pGLKwolKDo1C2b+eoDM2t5V0uUbHwAwFEf/Gl5geVDT+E2mAJq/lsRaGrgiR4n7E/hEIgKpdCAuWJk0C03L7Kqn2G/gwTRRMuuR8b/y5lCjKoPCRe0HALd5xzvzKU18MVKBKgYeFGAQlxe9ccp47YpmegApn3/73RugVKVbu8tTCLNvvn8InqXewioPD0Q4+Z0ObiXbgSsVSmQDIsPFNK0j53V lUCTd65y K9U50EtEappKdo8lJoHpN/uJAy86q6OeZgR1m89yyR/+kSgvpVwilvK80mbpKNm/M/T0c7Zo1PYnLT7w1lywxDMIT/4vrUUbwJwng6T5P4H6IfhiMq7l0cPbpJru7lgQ7hYqqXtkHlGL1ASRbGWACyGtAatyd5F8mz0m43iGSIfmPPGmQspoNfn6KVrZwM47HuQUlRt2ITat7sN84kfLoVyx8RDW5n+mht46cHdGhVZLWfMakQAwt2CqTHe2gt6sw2ZCTbBm6FoiMz0eCHXgPTUnuSI8ypJr6ho3SGATjQzyxXe7mpH5SLxyIn8XlSIWWlk31AsAbzWhlPpZCPrdgmh+mBJGLTqTksIbggBzWC8rb0zPmrE7QRCrTubfJzBj3NEwMUHIffEwxrz+sTsIc4DNcc/XVLBpLSBX3CeScVK0CAUkfhJQpLKQPGw+nEP9HRrD5GsiJuPKQzy+6bX7TUC9lfw== 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: On RISC-V, the ISA extension required to dereference tagged pointers is optional, and the interface to enable pointer masking requires firmware support. Therefore, we must detect at runtime if sw_tags is usable on a given machine. Reuse the logic from hw_tags to dynamically enable KASAN. This commit makes no functional change to the KASAN_HW_TAGS code path. Reviewed-by: Andrey Konovalov Signed-off-by: Samuel Holland --- (no changes since v1) include/linux/kasan-enabled.h | 15 +++++---------- mm/kasan/hw_tags.c | 10 ---------- mm/kasan/tags.c | 10 ++++++++++ 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/include/linux/kasan-enabled.h b/include/linux/kasan-enabled.h index 6f612d69ea0c..648bda9495b7 100644 --- a/include/linux/kasan-enabled.h +++ b/include/linux/kasan-enabled.h @@ -4,7 +4,7 @@ #include -#ifdef CONFIG_KASAN_HW_TAGS +#if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS) DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled); @@ -13,23 +13,18 @@ static __always_inline bool kasan_enabled(void) return static_branch_likely(&kasan_flag_enabled); } -static inline bool kasan_hw_tags_enabled(void) -{ - return kasan_enabled(); -} - -#else /* CONFIG_KASAN_HW_TAGS */ +#else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */ static inline bool kasan_enabled(void) { return IS_ENABLED(CONFIG_KASAN); } +#endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */ + static inline bool kasan_hw_tags_enabled(void) { - return false; + return IS_ENABLED(CONFIG_KASAN_HW_TAGS) && kasan_enabled(); } -#endif /* CONFIG_KASAN_HW_TAGS */ - #endif /* LINUX_KASAN_ENABLED_H */ diff --git a/mm/kasan/hw_tags.c b/mm/kasan/hw_tags.c index 9958ebc15d38..c3beeb94efa5 100644 --- a/mm/kasan/hw_tags.c +++ b/mm/kasan/hw_tags.c @@ -43,13 +43,6 @@ static enum kasan_arg kasan_arg __ro_after_init; static enum kasan_arg_mode kasan_arg_mode __ro_after_init; static enum kasan_arg_vmalloc kasan_arg_vmalloc __initdata; -/* - * Whether KASAN is enabled at all. - * The value remains false until KASAN is initialized by kasan_init_hw_tags(). - */ -DEFINE_STATIC_KEY_FALSE(kasan_flag_enabled); -EXPORT_SYMBOL(kasan_flag_enabled); - /* * Whether the selected mode is synchronous, asynchronous, or asymmetric. * Defaults to KASAN_MODE_SYNC. @@ -257,9 +250,6 @@ void __init kasan_init_hw_tags(void) kasan_init_tags(); - /* KASAN is now initialized, enable it. */ - static_branch_enable(&kasan_flag_enabled); - pr_info("KernelAddressSanitizer initialized (hw-tags, mode=%s, vmalloc=%s, stacktrace=%s)\n", kasan_mode_info(), kasan_vmalloc_enabled() ? "on" : "off", diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index d65d48b85f90..c111d98961ed 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -32,6 +32,13 @@ enum kasan_arg_stacktrace { static enum kasan_arg_stacktrace kasan_arg_stacktrace __initdata; +/* + * Whether KASAN is enabled at all. + * The value remains false until KASAN is initialized by kasan_init_tags(). + */ +DEFINE_STATIC_KEY_FALSE(kasan_flag_enabled); +EXPORT_SYMBOL(kasan_flag_enabled); + /* Whether to collect alloc/free stack traces. */ DEFINE_STATIC_KEY_TRUE(kasan_flag_stacktrace); @@ -92,6 +99,9 @@ void __init kasan_init_tags(void) if (WARN_ON(!stack_ring.entries)) static_branch_disable(&kasan_flag_stacktrace); } + + /* KASAN is now initialized, enable it. */ + static_branch_enable(&kasan_flag_enabled); } static void save_stack_info(struct kmem_cache *cache, void *object, From patchwork Tue Oct 22 01:57:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13844966 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 DEA1BD17157 for ; Tue, 22 Oct 2024 01:59:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B8746B009C; Mon, 21 Oct 2024 21:59:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6417C6B009E; Mon, 21 Oct 2024 21:59:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 386AD6B00A0; Mon, 21 Oct 2024 21:59:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 4D79E6B009C for ; Mon, 21 Oct 2024 21:59:26 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D34AEA1C27 for ; Tue, 22 Oct 2024 01:58:55 +0000 (UTC) X-FDA: 82699580628.12.92FE6E2 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf11.hostedemail.com (Postfix) with ESMTP id 48C7340005 for ; Tue, 22 Oct 2024 01:59:04 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b="aoNV/hFk"; dmarc=pass (policy=reject) header.from=sifive.com; spf=pass (imf11.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729562252; a=rsa-sha256; cv=none; b=T5B9of0TDQXMqmS3CpbxADKBBPpz8P8BAHFUnAI3r8MTh1HbR/Z5Iff1/zD8IENyxLhgPi nzmmRS1NGQqg3g7Qkqs2l1V2JC+2uxevjDVU6xie59MnG5pe0M5/fL4E9remo36yChdQR7 P6bz4erHhEF2T6hU7+zraMvzFtDg5RA= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b="aoNV/hFk"; dmarc=pass (policy=reject) header.from=sifive.com; spf=pass (imf11.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729562252; 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=9FnCjcC1/gsDOd6L8rx/6dcUkVYp18eK0+PNRkpMvb8=; b=4lOgkuIPvniIy/vBKU8ON/aTlncKOuyJDWitQXWs4sDmQJoALj2WKyHeoMbcPxNtgKqTt6 kOHVYkeX3ZApJdx8bGRZd8F+eJnuIRR0sDA683A1IHAUxaTh38+qSvcr2toXNP45A1ifbv APau1G7dB1wvaq6qfPHvK13yDvJxXVA= Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-71e585ef0b3so3904963b3a.1 for ; Mon, 21 Oct 2024 18:59:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1729562362; x=1730167162; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9FnCjcC1/gsDOd6L8rx/6dcUkVYp18eK0+PNRkpMvb8=; b=aoNV/hFk3y3wieCcfeQzx3NPuwEwoHu8pGWlC4WNaLP3VOuwqJbSd7ff0J6vKPu0k0 Fxgjma2uwJGzIVIkvhuls/OhtxhaF2f1WXOmZnh9pqCVeW2MC2N3qd6Lby5hrYr2y4+o b4flp+qV00Xdtou9h51njr6umZA5BHKi8RlvGRIzKU0kTSJvR9PfTzOyk9riKfmaEQDb e6nS34H5eyikhIPdfwBF8FZM1iXiDxbe1QcyoKkG9cFphPVVkU+L3suOiWUzWPB5hQj/ TkWcII9bS5NzOx6ToFj8cl0FkxhTpBhSB4sTF+c0ATbBmLKDNGrzfjQvehEW7JJmXTku T6Vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729562362; x=1730167162; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9FnCjcC1/gsDOd6L8rx/6dcUkVYp18eK0+PNRkpMvb8=; b=Zk31erykXP/r5YacnTCyFQMuWB1huWxZBS4XgL1smdZj5MKcqaJl+uNV3Gylod4j4Z jzeJDOFYHYloyA2pfyB6jfIduSoDhZJRO+cvn1pmyyLLhLAtlZjB7btDCxPq4gmwSxfi eXoFPn532NYW1IGSgwaC1COMwTnJ3YQFwtVmRr567y56FMdPmoHi8aaoY6doSh3zigVs TUyuNGzseb+8pjAVUy4IfN9e3TeX4RHs2wrp/T5nHXIHlwPfec9rMCh56jzhq1jEHwUd sPFvtiinUZAXWw6mWTdLnOQsP8BiTRmDJhdd6mngOGvTmI2bISNdbxKjsfRNBCauxeS8 bytw== X-Forwarded-Encrypted: i=1; AJvYcCUiluiBgiIJXxYu5se36eu5GbBN+5adE92w7kmM51G6XlFegpn6TsqOWQIREMY1CjX4ZJpymvDqYA==@kvack.org X-Gm-Message-State: AOJu0Yz8d/KULEM0GyV7KZdsW50Uul9BeLE2Q1GZt7eXrrmH71FUGCWO u/VbdAU1wEawA3KTKY3c7KWkOQ1WoNmwU6koQXGVI7Et23qlwQuXKKaEKfiQZNs= X-Google-Smtp-Source: AGHT+IFrGG6oMYUOck7CBYXToXMnxG+Lx4oLmbcg4j3FeL+zQkCHcoYgP+J0xW6HIU5j3POtBJfPHg== X-Received: by 2002:a05:6a20:43a4:b0:1cc:9f25:54d4 with SMTP id adf61e73a8af0-1d96df0ed05mr1060384637.38.1729562361689; Mon, 21 Oct 2024 18:59:21 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71ec132ffdcsm3600710b3a.46.2024.10.21.18.59.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 18:59:21 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , kasan-dev@googlegroups.com Cc: llvm@lists.linux.dev, Catalin Marinas , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti , Will Deacon , Evgenii Stepanov , Andrew Morton , linux-arm-kernel@lists.infradead.org, Samuel Holland Subject: [PATCH v2 3/9] kasan: sw_tags: Support outline stack tag generation Date: Mon, 21 Oct 2024 18:57:11 -0700 Message-ID: <20241022015913.3524425-4-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241022015913.3524425-1-samuel.holland@sifive.com> References: <20241022015913.3524425-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 48C7340005 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 9fiyihcsw3kzzsx5q9bx6qm497ptkjmt X-HE-Tag: 1729562344-427978 X-HE-Meta: U2FsdGVkX1/x4/AfzAK6RPhiX8Gap9was5X25qBywG0jIq1/EBsrH23RxlQYV4F20s3aKGnxzmMOhVQlfLP0VzS/N1IOz3aXR9VH5201DzEbv/JWyRlIEGm2N3OAKlCqCKq13aJLfFoNzXKXtTVYX0eg8QjEl24f63jbn1iSjZf5FMgyO9/yLozBp9aGv2ok9B4zlBUZ5eXMgTEmwaC0MIXM05GMCyaFEunuDzb4c0GaDeiMGXtlyD86WDvPAfK055Ch5dXFiq6S39UBIIoKi67MBIIIGEDKIrlX0uK54Sce2WXHsfeL3H7lKebBbfveyhVoE9O4I89/DSLJ04u39CIkwJKIB0XZgx+1+7G9XqlmSrrDE+KWjmyiWHA9TqFdXExj5ReuWwaSi7R8HGYlpAlBBteze0u+Cpqfl69R7LG1d1NkB7CRrC4D/JZ40xiggL8/blsN+qaL9egglZzShHKFTxBrxf8BP/yMu0H1RX+4P+KxfVyLNnRJUvQF9RDuetdQgW1usMlF8CS+cpETVMCTPdw5saJEIyNKEvch/8J3uf6w2jY/j7hgFzEA/Wr/jw6LHrE3jgJButusRMfxQfdaBbEhT0uw2mClENkp+WKb+gZmsSs2OYNYYY+NZyrZnULUm4uTfcIw6ICag4D4HaRJgRzXohtSlHsQVS1Aq4yGmyfuCqUq8CTBdzggIQKR1iCVsXWXhjrC7G7NgREtf+RvrYbx4O+JRgINrEfTPNeSwnOZT9DAvBRzzLIidq6fHqh/hwCOs8ZDKkfhNKpELeqrfogVoZB2xbb8aOCHXTtWjouxPGi+LJDmaQ3l+qzK8d7QBjeIWmx0RBojv3RQfHxZIrZsL4aTWQ8h8nCrB+eSiWt5pAkzp8BYWXc69h8AHpnsLSwwVeZEMTWCcyqObmRPpF2AhSGa4HKwGuWyAx9q0QeszJTxO2XkXIsHjRRhtZK/7c4qUrkaWWVYT46 nXTyuES0 J2OTlP2MMubgVPZ+RPnax47fJPqQL/eDWv6f1GjHb0U1NEe7aiLWsyUgaKUf7zDltqGjTxmHRzIDSZm5O+s2vQSJztExHS0bwXlGPC/Wt+c00av2T90qlinFrwF/HKFEX8Vi78egjWpoHb0Tmib/hWnLpP92VN8Xxk4hZPkR2dVZOr0jNjd5BpcMS3ilkzfAAdkOEJBuaMJVn/ohHmzTMfqrVAD/awtovltslBD7lmCE6kRCHBBL21lVgVYLeDKDlMfXCrCNfVcM6dEC0aUk2AVqRESeOiJduBVAOISEuAc6AWvG9Xq8lxquDgUfWraIrciwroMLvGjaUQ7Xd7A9LLvKsYXiIc1X6NyxMglpy74BaKtrTQVyAHwyyFwzN+nGvKIxbLWen9MBx/4c+LoFlG1K2izwPgmvtKCoVVcDG/TADtvxjmsnrcv9VW4pJN9nfhqxJ 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: This allows stack tagging to be disabled at runtime by tagging all stack objects with the match-all tag. This is necessary on RISC-V, where a kernel with KASAN_SW_TAGS enabled is expected to boot on hardware without pointer masking support. Signed-off-by: Samuel Holland Reviewed-by: Andrey Konovalov --- Changes in v2: - Split the generic and RISC-V parts of stack tag generation control to avoid breaking bisectability mm/kasan/kasan.h | 2 ++ mm/kasan/sw_tags.c | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index f438a6cdc964..72da5ddcceaa 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -636,6 +636,8 @@ void *__asan_memset(void *addr, int c, ssize_t len); void *__asan_memmove(void *dest, const void *src, ssize_t len); void *__asan_memcpy(void *dest, const void *src, ssize_t len); +u8 __hwasan_generate_tag(void); + void __hwasan_load1_noabort(void *); void __hwasan_store1_noabort(void *); void __hwasan_load2_noabort(void *); diff --git a/mm/kasan/sw_tags.c b/mm/kasan/sw_tags.c index 220b5d4c6876..32435d33583a 100644 --- a/mm/kasan/sw_tags.c +++ b/mm/kasan/sw_tags.c @@ -70,6 +70,15 @@ u8 kasan_random_tag(void) return (u8)(state % (KASAN_TAG_MAX + 1)); } +u8 __hwasan_generate_tag(void) +{ + if (!kasan_enabled()) + return KASAN_TAG_KERNEL; + + return kasan_random_tag(); +} +EXPORT_SYMBOL(__hwasan_generate_tag); + bool kasan_check_range(const void *addr, size_t size, bool write, unsigned long ret_ip) { From patchwork Tue Oct 22 01:57:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13844965 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 C0307D17158 for ; Tue, 22 Oct 2024 01:59:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 13A4E6B009D; Mon, 21 Oct 2024 21:59:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 076E86B009E; Mon, 21 Oct 2024 21:59:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E31866B009F; Mon, 21 Oct 2024 21:59:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id BC2CB6B009D for ; Mon, 21 Oct 2024 21:59:26 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 87A25161C7C for ; Tue, 22 Oct 2024 01:59:08 +0000 (UTC) X-FDA: 82699580838.04.E8F6A2C Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by imf16.hostedemail.com (Postfix) with ESMTP id 6F73B18000A for ; Tue, 22 Oct 2024 01:59:09 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=bE4I61WG; spf=pass (imf16.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729562288; a=rsa-sha256; cv=none; b=SzkjGgHvBUEz2iOdBhWD6AbamkqDFz5L5eA9SunIU+G0BO6HzU32CRMqHNqGoNa3Hwqsu8 yT/mdE4MisoXzdJoyYu3AT4l0q18xUvhwppaBor8vbdSTwnZBk/Pm7wDQWpv+IaIB6M2xE N9CQpV3JideAqyUoAnJ2YKxRSLu169c= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=bE4I61WG; spf=pass (imf16.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729562288; 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=Y/iY9Y4RpcnWjc25xyItwxMbRygrVUaVLlWUhwxh2UQ=; b=qVJYsa46IJrlrPp3YRtiHfDssV+UIj7i7yqPos3GvALbQouHHc2NVoLeqa3Q1sOUiMKODX zUGMc6lW87E6Jxw29hqGmhImkogss6DECii2Q2vqympJ5Matt/azyRzn3FHUNcskwoUxqI N8Qb+3Ei6NZogW0dtDa4R/aLcG+uB0E= Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-71e52582cf8so3545717b3a.2 for ; Mon, 21 Oct 2024 18:59:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1729562363; x=1730167163; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Y/iY9Y4RpcnWjc25xyItwxMbRygrVUaVLlWUhwxh2UQ=; b=bE4I61WGLjMtUtwIdyrKV1E+so0BlEsZdW9F5rGjtZ3UU8plPS9jbqaPdU8R9LqFLs pRi6VpMNjaROsTvj0CUfQpkz1BHyub30ayj8vrMgwTri63NEG4Xi3qhGSUfxNZFG+RhZ HzjD3IxO/Hs0c7aEZb/6UM7tX1llc8ezQSQp2EDAx3QLw61X7s0FAJOSp83LR1rEGaWP 6wYNkR/aJXq7lMqHGNG/XuXA79BPj7k1mmKo4j0I7wGXfXENwd1WEELhItlEIGskFXRv STtwy37faxkLnYhQsM9G97vHhlE3tXngVQ0mbCUHC3aI4saHeJwCNFhY8G8ROBzDmeqJ LHPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729562363; x=1730167163; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Y/iY9Y4RpcnWjc25xyItwxMbRygrVUaVLlWUhwxh2UQ=; b=hDl/Db38I/8hjhyJQ82MPlHsuDrZZmxTNKHPP+GKS3kXvp0/SosOwUP4BT9Nu89Oa0 0WModlSy1EkmzGwU2xFYmmlnzw+UgvvQR4VtsPRH21XAeAZYHA++8SDOs5oCooJ7U8o/ iwc6zA9+AHeX9b7WkvXbJb0MlJKD3n5RcSqEzvCznn5G7oDhbvE57zqL8vQLJYVQcoka NfREXZvORwMCx8y1FnguUammUwUMMzKXRZElgH10zwg66Gv1Tf/xvk4Ynp436lMbSMQ0 gMJoRGY+Qtr1muRNMVlwBRUbdA3sbn7sirWvrUWKj/ze9K/ytpUyHhVlaBnw1LbKNjKU T0Gg== X-Forwarded-Encrypted: i=1; AJvYcCUKeymKlT0APIY1jjmfNi3nvUgRtLHGpy/yrcCwHEhhC7P39/lLYbX9ip0eXzBcUir75eBtKgScRQ==@kvack.org X-Gm-Message-State: AOJu0YzM95p59gUb+EMiDN0xuD92z9ckpVqeQpHFFMfpPjBfzyuM2KVl P/rqED4TJmvzF+xcoQKaIbjdYJbWvlG+GrLgILL0DxOMXyhNkIYCitTDLPKCcY4= X-Google-Smtp-Source: AGHT+IFB0AudxwQCUfX+B+Q1Nd/kKdWsIXk3CQ+Qmrq1W4iGhp3wWHffFzDvMvs7eDfgOIFmgokQfg== X-Received: by 2002:a05:6a00:9298:b0:71e:5de:ad6d with SMTP id d2e1a72fcca58-71ea323b91dmr19028480b3a.24.1729562363415; Mon, 21 Oct 2024 18:59:23 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71ec132ffdcsm3600710b3a.46.2024.10.21.18.59.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 18:59:23 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , kasan-dev@googlegroups.com Cc: llvm@lists.linux.dev, Catalin Marinas , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti , Will Deacon , Evgenii Stepanov , Andrew Morton , linux-arm-kernel@lists.infradead.org, Samuel Holland Subject: [PATCH v2 4/9] kasan: sw_tags: Support tag widths less than 8 bits Date: Mon, 21 Oct 2024 18:57:12 -0700 Message-ID: <20241022015913.3524425-5-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241022015913.3524425-1-samuel.holland@sifive.com> References: <20241022015913.3524425-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-Stat-Signature: ih7qew4audukixdyu934m6kefwpkdsio X-Rspamd-Queue-Id: 6F73B18000A X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1729562349-789250 X-HE-Meta: U2FsdGVkX18QdFejfcdeDqc/PGGa34stIZhZIO5GZcz5nIVeda5sBA/CQgaFk4HYtjmuCPVvgntQFYa+waD36p052Rd/O0bbF82ytWlj9RfmtxytS+tgdp4cZwZm9JJQbyqevK61U3yBLw6y7THblLjSIepyI7HOBssca5sBq2wuor7eOIYZ+vHlK/WbiEynv1A0lDJFYbPekFWVX2P6OhP7MS8Hwe4Ovn1ys7q+PXah37v3YcWL0ZmClNAy3DcZZSRbe1fimMuv4o8ypEJtgodS2/88vA4zG+vnAXRDet03srqz1TslwG5DACelCIa4Hro3cVKqrcJPtdBrw8Ek91+Ywh8OktRnL6O011gtII0A7CAadZi9BJipEeSlzhqxtyuufiM4Kj8eXsxJivd+YNdwiZNNqs208KivDaUjq84Mt588Qdu0H9ALnqni7poKfP+bAm+yy+HQH2ND1Fh1Qk3WDGvDey4ICMvj+o+oRTZT/U0f4+Mw7g/S+ZG/fLXNzP0Mn9iRHOLCs0hTnxeJyxjdJnTiFSmKnWLUNtjW9isH2lQ4nQgljLaPMEXbSHj37LGQ4r8DQ+/i4PUq7xqlOzZ5RyTXpcj70yWQPf2nOnsBitF2aBYrM+Je7ceGPtuGRluDD6B3komggiSv3Kg5ofNL2D4npfZcvXcGRhtGTQXLiaEn+t+k8TS9elB+r5jo5FV7G/j2yFslCGIBVyWMxiggG+W7kJzFkaNLfCFN4Hwrn7/TXGV4j62yKkA5ghsCvg0hMgZyBEzowtNqOBgEMfm9nZYVQGzwC5aZk6G6Cq1FUerB3ZXEXYNW3x/Z6hDLDX7gEEs2JSzPlluVH+4cdaxZPZBlMGLBvsRQKuwSwzHEUriB2HDrXBtyLoQOB/PNH1WgYOk/ZxIESgovScACEI1lGhJy243aWC6Pj3WADe7k+G+/JfzmdMNUW+sVTUofXXmoTPpZH2VcuZXmjQI ElRWISTF rvvSeuAym00c/7z5azhL9TXc6CMBuuUbIBieIhQMqPQDpaDk62l7PUo+GHvBypPBHIlxF+uSQNJpBIZrW0L6LpRVqsh0WcA6brzdOcfMd6N5qYkwK9ToE9s2E0Jhs9gOxqx258bCYV38N3vcMPadtrqQMl88Dtj16ock8adGRoyk0cqd/flKW2Lr5Mk0aHCvhEx8l3bjvgA5dn2dHloCkSTFsJClEpFu0miS5Fjt1QE+rNWsX3YHtjmbh5brwLfiqCAdZ13cBeA5s0bxmWRcymhjHZsNpLtUmOpfHvIUboDLQlZivv3NQ/xz7rV1or9ZEIYZeANay1r3A77neP7MI+KOBX3SrdXvkpyd7mf7U9f5wHbc0vYrUb1LUNinMRRyoc/G0Ymr6BJWjqCmH/eG7PG9rvDlef1VGv9ldfjVipXFzEqn9jSxtN8QlLWg1EaxN0BOkLJ5ZTdq0/rLPu+KtYFoSRQ== 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: Allow architectures to override KASAN_TAG_KERNEL in asm/kasan.h. This is needed on RISC-V, which supports 57-bit virtual addresses and 7-bit pointer tags. For consistency, move the arm64 MTE definition of KASAN_TAG_MIN to asm/kasan.h, since it is also architecture-dependent; RISC-V's equivalent extension is expected to support 7-bit hardware memory tags. Reviewed-by: Andrey Konovalov Signed-off-by: Samuel Holland --- (no changes since v1) arch/arm64/include/asm/kasan.h | 6 ++++-- arch/arm64/include/asm/uaccess.h | 1 + include/linux/kasan-tags.h | 13 ++++++++----- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/arch/arm64/include/asm/kasan.h b/arch/arm64/include/asm/kasan.h index e1b57c13f8a4..4ab419df8b93 100644 --- a/arch/arm64/include/asm/kasan.h +++ b/arch/arm64/include/asm/kasan.h @@ -6,8 +6,10 @@ #include #include -#include -#include + +#ifdef CONFIG_KASAN_HW_TAGS +#define KASAN_TAG_MIN 0xF0 /* minimum value for random tags */ +#endif #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/uaccess.h index 1aa4ecb73429..8f700a7dd2cd 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/include/linux/kasan-tags.h b/include/linux/kasan-tags.h index 4f85f562512c..e07c896f95d3 100644 --- a/include/linux/kasan-tags.h +++ b/include/linux/kasan-tags.h @@ -2,13 +2,16 @@ #ifndef _LINUX_KASAN_TAGS_H #define _LINUX_KASAN_TAGS_H +#include + +#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 tag */ +#define KASAN_TAG_MAX (KASAN_TAG_KERNEL - 2) /* maximum value for random tags */ -#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 From patchwork Tue Oct 22 01:57:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13844967 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 6160ED17157 for ; Tue, 22 Oct 2024 01:59:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7CA726B009F; Mon, 21 Oct 2024 21:59:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 752396B00A0; Mon, 21 Oct 2024 21:59:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 57D346B00A1; Mon, 21 Oct 2024 21:59:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 356C76B009F for ; Mon, 21 Oct 2024 21:59:28 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 97381121BA6 for ; Tue, 22 Oct 2024 01:59:13 +0000 (UTC) X-FDA: 82699580460.03.0BC2349 Received: from mail-oa1-f41.google.com (mail-oa1-f41.google.com [209.85.160.41]) by imf13.hostedemail.com (Postfix) with ESMTP id 3AB2320008 for ; Tue, 22 Oct 2024 01:59:10 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=S1vnYrfA; spf=pass (imf13.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.160.41 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729562290; 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=FpfVj+cCklKKT0meu9E3X+82meKkLqCR+uZsOKWdYis=; b=P9InAUmR1iLEvhObcoPcRirnKAyHoy02U06EW8nl1P3mEoyRZwbtgPC3xqcbAYyPoko25V sJ1mNkSPfGDWeuHhg8xnkO5zyNwRXT5/VRY9BRhbHLw2pm4xeLwa9+IfUPf7yIeTUQUb7m gENjb2F0vQmoEBg/6rdCBUDJCIiSPnU= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=S1vnYrfA; spf=pass (imf13.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.160.41 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729562290; a=rsa-sha256; cv=none; b=bwYI43IEKWR+SdqMSN9eeLb92QfQidT0SdIWtkTLHOGqj4HxvZBV+szF87H6tU52wDqeaR 4dlnNuPPBI9taKzGcmYw6SjzZf9tHkeLQk17S8wRALsaP1Bpc90seVcETDXlBAKotyD+pS 5R1eD6NDakjbUsWmU9aSujW/oyKPX8c= Received: by mail-oa1-f41.google.com with SMTP id 586e51a60fabf-288916b7fceso2547311fac.3 for ; Mon, 21 Oct 2024 18:59:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1729562365; x=1730167165; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FpfVj+cCklKKT0meu9E3X+82meKkLqCR+uZsOKWdYis=; b=S1vnYrfARDeNYFf/9CRpB4yJYBsfY7ciKMR0v6LlMxNxHTep+4UrLFWM74cSPj4JEc hKgY4a+mNrV1gsMCggxyVMFOKVYidx18mj55ITaI1sMYZk8Y/Y51sPysX5x4ToP3xQ0k 86jjPvGWd+bUQ2JZr/o1PrRQpV1qMfuDZIMCJRS1N9kifWtKh10r9CZ9j/nvd8/4JICD 4rPobfrwX2gkPln+mJhrQcSjKJc2B3qVWTNAu/TgPv4fr1+JU48yjC7V1NKKVmco01Em DOPJ6PubmjWKgtmXVHHksZUSMh5thT/3BpFjmFlEwkCyD6ycwuAU90KQ8DzQ42cRdsjY vcXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729562365; x=1730167165; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FpfVj+cCklKKT0meu9E3X+82meKkLqCR+uZsOKWdYis=; b=lifWsz9gkQ1GYmkcBw7zJ2osXDP73EhrhlLOhOrn+I55V4ScAurDNk70MgajugyVhg huGMWhZUyPa94bxVrvmp9dyNN9042frtXlEcCCh8r2s1vR/iZEjT6vAzcsUx73IMMw6w IRRPAM234NnB68PPSCAVsKpa6Y3005pbWdxPVpC44fYQECEzo7O2b/gdR3e95aCJirQn KfT90oUCPutiL+p46I8FATc1gREZ14GR3xpceMZUvKp0fQjAHZJHmv2cNnjSMlx78wnD h3jaqu/40Ksc5JBUZXGnLgkpx2xD1tDA6dBc5lsbAwuHUpzOuaVZnvqjwUfQwSCyzOvT yTIA== X-Forwarded-Encrypted: i=1; AJvYcCXIeeWF9WGufATA3Z1EJNPafyBb8KjHuTgsPBshqqoxbVLS6RHwHpO20zlQOHwd4/Ax5x5f7o5taA==@kvack.org X-Gm-Message-State: AOJu0YxTaiIwdb595vFo/EDF8jqkx7YclFS2LzAzOT9trfPKHD0OBTS3 weV9eBbSgqtRm4ciN1RaOVG8bg4GO6Z5rFXBY1iqjyWI4a4bAJ2XB5bFTmCk1v4= X-Google-Smtp-Source: AGHT+IF8mODh9IAJs797PRe4iXy3b/dNIvInfpWpmRrPkiuy4gHecBRUAsqzv6T0/gnC7v7yNiNl4Q== X-Received: by 2002:a05:6870:148f:b0:27b:61df:2160 with SMTP id 586e51a60fabf-28cb010575fmr1017968fac.31.1729562365145; Mon, 21 Oct 2024 18:59:25 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71ec132ffdcsm3600710b3a.46.2024.10.21.18.59.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 18:59:24 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , kasan-dev@googlegroups.com Cc: llvm@lists.linux.dev, Catalin Marinas , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti , Will Deacon , Evgenii Stepanov , Andrew Morton , linux-arm-kernel@lists.infradead.org, Samuel Holland Subject: [PATCH v2 5/9] riscv: mm: Log potential KASAN shadow alias Date: Mon, 21 Oct 2024 18:57:13 -0700 Message-ID: <20241022015913.3524425-6-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241022015913.3524425-1-samuel.holland@sifive.com> References: <20241022015913.3524425-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 3AB2320008 X-Stat-Signature: tw8hcnmofckjg5jq9p3x6rrzon69tyju X-HE-Tag: 1729562350-832841 X-HE-Meta: U2FsdGVkX19+PS4OlFderCQ4DVhamHg/t9CM20JjIhzTLeZy24VQ/e/OXHWbASOQTGRAKpnlwuPKEwshOHynHfCSFsDvPzWDGL9145hHrdUuwKv+ztjKsenRJNPylPy1VLvTpiiXfm0u73+GXcqDyPiSfaoo/lMnUh6HgmAm69VInlvFoMRiRlrc1zrIWjvBwF6spSuogtOEMEuBGlsMSDo1dW5kgZB1yBm2oki+rMEftWbNW/cB0AFVJjbk4bUtAf0ZDpzA2+A5+lA4kp5aLPpEVev5Fz3sFDETkBMipwch4X2cYHO48t5/Ssuo5ZUrciLDCLOoDApsbd92yR6HHuRZAL4+E5j3jaxsz3BAfr/fFvmH4zivSEnVqS2/96Nc3C12+4BsplNDMCvOzQDxs6gP5F2bfROUEqfmvTA2sKEj8MgMQFGU1LCcaKTEobLHfiq7s/bkxDYiK/rZYfeCvw9wCoFw/5iab0SP0Ssjcd0eJwguRbd5draIF+l3R/QcKAY50JqfIQMvjmRQ0jngFXgyxC2WXYWx3hsZLlQf+ud0ruPLKx9AFtikuImG2ikrlDcWVqngKkdrIILcaf0NS5YB39r7GIRBnBn283S+pfd9x4LRAuD2ixTtU1HUOBC8aYM4Uj51wTK5wecvhJx6Y//w98dSK7QN4L+PxoNydoTimbx6Osy7DNs1q20rEmNOmsntHkk2EJV2uZAkV+oEBRmpaMbH5ENjgqehTtU22l4Nd9SFzpjkqMQ7yLc3r71a5s4CZK7eWYGSQJRIb8pBOon03X7GHDAold5sOvLWH/w+A5ZEUU6T63ygiXphfi2pfP+ZpgNhyC5yVy4DinZ6vnx62WADyg+hjsnV2mjWaDQbOZ3LtWdVYFrs1n1q6Og+sJXRkCvcD5XPN0BQhS+i7bkts2sOXVU/QcWGlUtJOV77laAC0EJmaWZlP7eGtSfpFlzeu4OWDfxBlaXB/1R KsLSMYZP JF7MInRu4SHPN6dyLy5RHVZcD9r2c70iAHRYtSbX2pKcwSPjQ0F1R5Doh1lxH5O03Sj+9LNnX2CQT9LGPO7dfY6O0IcKXILDYFPKwJ1bxaF1JGdl817zzsrD3iPfXplqPaCU0l4I05Wd4uace/aQUfXWYvChUIWtjTRAGVR2I0vQFdXliLqZN/VjNi3ZtwqbuUP3ud4Z5xijSSmLVFYQ1WD9RawncQU4z5bLABvsnlHpq7eNZG8YQOquGgoKQd4PjHM4CvWIZZpHsU5AV8FMEAJyuNNJ3QaIy9iYMzwtqNUFZ5CWKSQYWFvTOfemZDuZqkzxiF/V6aFz1tV5mjiMd9e50vzBKDe85fAQ/U5A1/Uo5f8eDI+0N6OmdIGVsDpnmRtjx6ItJMTSO6hYolRmLqNokIX1sKdhoIr56eOicrqXjV+ig+ScPDgURAw== 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: When KASAN is enabled, shadow memory is allocated and mapped for all legitimate kernel addresses, but not for the entire address space. As a result, the kernel can fault when accessing a shadow address computed from a bogus pointer. This can be confusing, because the shadow address computed for (e.g.) NULL looks nothing like a NULL pointer. To assist debugging, if the faulting address might be the result of a KASAN shadow memory address computation, report the range of original memory addresses that would map to the faulting address. Signed-off-by: Samuel Holland Reviewed-by: Alexandre Ghiti --- Changes in v2: - New patch for v2 arch/riscv/mm/fault.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/riscv/mm/fault.c b/arch/riscv/mm/fault.c index a9f2b4af8f3f..dae1131221b7 100644 --- a/arch/riscv/mm/fault.c +++ b/arch/riscv/mm/fault.c @@ -8,6 +8,7 @@ #include +#include #include #include #include @@ -30,6 +31,8 @@ static void die_kernel_fault(const char *msg, unsigned long addr, pr_alert("Unable to handle kernel %s at virtual address " REG_FMT "\n", msg, addr); + kasan_non_canonical_hook(addr); + bust_spinlocks(0); die(regs, "Oops"); make_task_dead(SIGKILL); From patchwork Tue Oct 22 01:57:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13844968 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 189D6D17157 for ; Tue, 22 Oct 2024 01:59:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 644906B00A0; Mon, 21 Oct 2024 21:59:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5F2186B00A1; Mon, 21 Oct 2024 21:59:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 445A36B00A2; Mon, 21 Oct 2024 21:59:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 210C76B00A0 for ; Mon, 21 Oct 2024 21:59:30 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 21C8381BE5 for ; Tue, 22 Oct 2024 01:59:16 +0000 (UTC) X-FDA: 82699580586.08.4F8A7FB Received: from mail-oo1-f51.google.com (mail-oo1-f51.google.com [209.85.161.51]) by imf19.hostedemail.com (Postfix) with ESMTP id C21551A000B for ; Tue, 22 Oct 2024 01:59:08 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=gpWEb2yT; spf=pass (imf19.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.161.51 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729562218; 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=jOWNg37CI6wBNjlK3WTwIizp4MumcxiO1pDVtn3VcZ8=; b=IfDY1R7+yWEL+CZ0/wzPPywYX1ecYfkJgQenr3ikJ2DA2sh0Q16WoWghsYR5RGuuovKR0q /vHcvgWhonS4aQJpkKZE88N772dfMK/A8UIHLx70R5jisiOKXj+aWSC/YBKx8cKYOuDlWb TTDhtD6POU4f94PPxTfOQasAOBp003U= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729562218; a=rsa-sha256; cv=none; b=tplIlEg3pPAibM1SyPgs7gB8DwMx3vgrOgWvST3Qf0KsOvfXEBXaQBUWKiNiKhSV/3zpdk ury25pLlZ6jziNDRrsI0KcGHMfGvqCDvRkazK9pQgF6NAu+jZxtCxW22yceOKnwMwKrDFe t6OtlOYMR2NfjZlA4k1FniRnrymO75A= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=gpWEb2yT; spf=pass (imf19.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.161.51 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com Received: by mail-oo1-f51.google.com with SMTP id 006d021491bc7-5eb5be68c7dso2604800eaf.0 for ; Mon, 21 Oct 2024 18:59:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1729562367; x=1730167167; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jOWNg37CI6wBNjlK3WTwIizp4MumcxiO1pDVtn3VcZ8=; b=gpWEb2yTlrTstE0WF/Y5R1VCqy2Na9TN3x/I2aJgrYBz8NgVUh2QGNcDZ9HiARq9zR /Sks9LUGVWZqeTEEPQRt7g5GNLTXGt1UtFj5280b5R5Zsav3ciBGE7IjfIK+67eUm7uI tIOg1GFkILUHftU3/ZsTJnm0rwnfLBa/M8u6kB7bYPHKiM4JNwUnI58IBF2WIYoLPysd ime7aQ4T2kQHfg21Nr7uk8fuaS+/HkHzgFS3LkT3r8oL2GAqlDR/Kga+Coq+kHgrcQBD lOdngUojXfbb4qhuIXa+pBDnnwYjONc/lwYJ203oi2W6rwlBrUT/japl6PdYEgIlYcxp QlpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729562367; x=1730167167; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jOWNg37CI6wBNjlK3WTwIizp4MumcxiO1pDVtn3VcZ8=; b=M2+yN5JnpblfD3O/VWHk4NDkHLK7ECx5XjDEeB21yH7dTBrECLEbNiK74Q0OIhvCZ3 xec4vzGJIsbqDPTswpYwZTNPkqzOnMWdEmp5oifl9zQDGypzB8LhA3XVu/Of/Nr8SmRI kPBCITsDgzkKIQxlvzqi1eDuNgstxsZmN0Taw6KvQJPpfjxrd4gjU0V8rSaAuJVbNkWp h6bn6FzzaeQpK9ItsSJXLQW0cKohAKYrdiRgs5RXvdqoyyYCYgu7WQAHa9whohrj2jqw z6s0JmdIOF4L9d0PYSp0U+kch4aN56p/34utTKtfnrXMWc7jkZI7M0jkwlC35CUslDUx p3rw== X-Forwarded-Encrypted: i=1; AJvYcCWuJ2Nti9l/hT5wMKvkZf/1fV8TScU5SpntpQ42Pcs2GZ9HBbgNH3wqTThVdHmZ72e1rd+EFY6HaA==@kvack.org X-Gm-Message-State: AOJu0Yx5ml6xykhuVUoXDDJKVD8K5jA84rhH6z5fm7DB1R3vY/f0LaXk yaBR4RO1fVYGsIspV8gOVZYpsnr85M3wEq2eZVur6nXmDqLq/TS8e22pTTyDCPw= X-Google-Smtp-Source: AGHT+IHErzREUGw6ROMLMvLXYHp4zadEF+Dwq950EYc2048lR/Pu3YsD3Bs92QVrt5aBEWQnl13Wpw== X-Received: by 2002:a05:6871:109:b0:288:b220:a57e with SMTP id 586e51a60fabf-28cb0184ff2mr946194fac.40.1729562366823; Mon, 21 Oct 2024 18:59:26 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71ec132ffdcsm3600710b3a.46.2024.10.21.18.59.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 18:59:26 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , kasan-dev@googlegroups.com Cc: llvm@lists.linux.dev, Catalin Marinas , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti , Will Deacon , Evgenii Stepanov , Andrew Morton , linux-arm-kernel@lists.infradead.org, Samuel Holland Subject: [PATCH v2 6/9] riscv: Do not rely on KASAN to define the memory layout Date: Mon, 21 Oct 2024 18:57:14 -0700 Message-ID: <20241022015913.3524425-7-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241022015913.3524425-1-samuel.holland@sifive.com> References: <20241022015913.3524425-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: C21551A000B X-Stat-Signature: yg1gb3hariwg1n6itkoxkhjmiw5yxw3r X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1729562348-411810 X-HE-Meta: U2FsdGVkX18e2Z0lcYcJsLmfGz7H9P4dxTCZecSOt3xXCMutisX78sa64n6e5QFNw95mtHeahQHhTvBOIsu12FO3USxUydq9L/8ApwtbfHEW/zhQp5estJiIi4m4FVBfJU6KxhAydzIqXIGmPvn5X9I39iLb7MLv3YSAhjK02gbBmpbFsHr1RTBvxn/ziTfInrVk/1+TVz6YeNPhti5JP2HXqiNta1lXVweuGtAyJ0LIKMP4t8y1H34jLEODWFeTFPMr4ZmPVpDUROnVQmWinJK8gULutZyPaQc5mtsR92GMntTqsJgYudH9Dc1RiK9t6DN5IDQ682as+Lh5M33cpa0GeGsMBJiqm7GZHQmfKJJK78lpbH6eaaWPLPCJTCrO5XpFM50alx0QnQQRLKYsmy16tGLsCcFo3vtmnnKfWIhkmRhHy3ocamwmZec4i/ddqD0rS9rxkAQ1uinfuzVdyxgbC3nGOrlzOToU9lvQ9YV0dUG5YRfIptKsxLepivE4lfjGhAAC7xDl3GL/kqegccSRxxX0HKb2PCPohottPF+PdElOZqpkuIyZASTWslZWTHJ031a9dM/Db+AR5Af7A1SpyDGIfjlN+4qNjlgKrDr/6aBSCm2ppLp9w/iBcjwhYrW8vIpnOJW0+F85QQRCg+2/UfNCroK85YbdjhBEgjGJN5wxaEzBdBd3pVsKXXME76gD5AvKI4uv22lRoDNMAb0/pEJOPzhHOYcAW8arZJMvp2hPRsuRjP3Vk3Hb4XwhkWQHXRSuloD8otAxW+1uLLzl3w1KTo9G5aniQRQUJSXDdmneqroqP5/sjGbDu7vVjg8h/WY8fIxohpxW7NHyiJ2Ror5DpLnfPGGSN7Vaj1mJ7oKDSsvFxEA653qlIRXwc4U82aymxXp8vhldBSdsO97oLfQ1MVKo9efSnjPR1LVwfi9zynuUMHkQGVnblqyUtB7yCnb7SoNv7OFdaWc 4NmFEF5K 90BFEQjDiJMyYCj2uZ/6vURZmLf6PryQxIPo299SM2BgSW/oLSnMPTMereVpeXrGbdiY5iH8LmTOmmdtjc0T0u4ekpcbG8fOvlum3qLXFIzUHTF3lVCjoL2SA3oCxCrf9CzC62UttWsNg9u5bZrgkOoTD1fn9oXOo0HFx8d+nCZlc4BMnGr2IdWM8AgWHbhSN/1rsgvIQ7927oawgHHRRFjG26DkFVjw3SLB/MScQXGI+Xq19UTVKOB1Od4TlGb1f00a/wjSlBDaYCHvSp6wnoVQW1lNvB3dWyRRQdeJB0BNX2SvgWZAE/dyfjrqdPRpSUP0Hgn8+vlR0xORTWmWfBpWzPRSf4mxs5kfrmuboCThZy/dW2IQzVEttlY+VhOTj6xrgcE8v3obdzAB0cvJjrkLzAQ4upyWxoIG5V9IzXhDudoHLoMpC9tO4Wg== 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: Commit 66673099f734 ("riscv: mm: Pre-allocate vmemmap/direct map/kasan PGD entries") used the start of the KASAN shadow memory region to represent the end of the linear map, since the two memory regions were immediately adjacent. This is no longer the case for Sv39; commit 5c8405d763dc ("riscv: Extend sv39 linear mapping max size to 128G") introduced a 4 GiB hole between the regions. Introducing KASAN_SW_TAGS will cut the size of the shadow memory region in half, creating an even larger hole. Avoid wasting PGD entries on this hole by using the size of the linear map (KERN_VIRT_SIZE) to compute PAGE_END. Since KASAN_SHADOW_START/KASAN_SHADOW_END are used inside an IS_ENABLED block, it's not possible to completely hide the constants when KASAN is disabled, so provide dummy definitions for that case. Signed-off-by: Samuel Holland Reviewed-by: Alexandre Ghiti --- (no changes since v1) arch/riscv/include/asm/kasan.h | 11 +++++++++-- arch/riscv/mm/init.c | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/arch/riscv/include/asm/kasan.h b/arch/riscv/include/asm/kasan.h index e6a0071bdb56..a4e92ce9fa31 100644 --- a/arch/riscv/include/asm/kasan.h +++ b/arch/riscv/include/asm/kasan.h @@ -6,6 +6,8 @@ #ifndef __ASSEMBLY__ +#ifdef CONFIG_KASAN + /* * The following comment was copied from arm64: * KASAN_SHADOW_START: beginning of the kernel virtual addresses. @@ -33,13 +35,18 @@ #define KASAN_SHADOW_START ((KASAN_SHADOW_END - KASAN_SHADOW_SIZE) & PGDIR_MASK) #define KASAN_SHADOW_END MODULES_LOWEST_VADDR -#ifdef CONFIG_KASAN #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL) void kasan_init(void); asmlinkage void kasan_early_init(void); void kasan_swapper_init(void); -#endif +#else /* CONFIG_KASAN */ + +#define KASAN_SHADOW_START MODULES_LOWEST_VADDR +#define KASAN_SHADOW_END MODULES_LOWEST_VADDR + +#endif /* CONFIG_KASAN */ + #endif #endif /* __ASM_KASAN_H */ diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 0e8c20adcd98..1f9bb95c2169 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -1494,7 +1494,7 @@ static void __init preallocate_pgd_pages_range(unsigned long start, unsigned lon panic("Failed to pre-allocate %s pages for %s area\n", lvl, area); } -#define PAGE_END KASAN_SHADOW_START +#define PAGE_END (PAGE_OFFSET + KERN_VIRT_SIZE) void __init pgtable_cache_init(void) { From patchwork Tue Oct 22 01:57:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13844969 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 F0D60D17157 for ; Tue, 22 Oct 2024 01:59:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E81AF6B0082; Mon, 21 Oct 2024 21:59:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DE1696B00A1; Mon, 21 Oct 2024 21:59:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C36316B00A3; Mon, 21 Oct 2024 21:59:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 9E2D26B0082 for ; Mon, 21 Oct 2024 21:59:31 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4B8DF1C76DC for ; Tue, 22 Oct 2024 01:59:13 +0000 (UTC) X-FDA: 82699581006.16.1E6D246 Received: from mail-oa1-f46.google.com (mail-oa1-f46.google.com [209.85.160.46]) by imf05.hostedemail.com (Postfix) with ESMTP id 32DD5100004 for ; Tue, 22 Oct 2024 01:59:00 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=Z6f2RpYI; spf=pass (imf05.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.160.46 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729562218; 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=559wueV0PG6xFm1m5EmJll56/fKSQJhn2CptHQFJIX4=; b=L1Ol6mUC4EwvaabojlMUFysdk39JlUiRV/fvKyHw4FlSi8kbpXrsxkW5k0N2JmkQGFW2GF mH3PIq0ZFXTkFzJckDdVTkVIUDWqAK4FacV/6LIMY5d/1RgmSfZNZS7DL4Q5bm9AwFZGvt UPjVRBRdpHyOQmRsz9anvRMXunSngac= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729562218; a=rsa-sha256; cv=none; b=VwvEzhoC5TuBvpAdQDrM4lxT8Rj1nfalDTH74es/1g+VBCeUZSepO17tfb3Rg9/mv/K5ZV oLIq1zM4yfqve/G4pbW+T2UHXbq4js9DWrAD3FyIEb5EL+BZ7h/yyi+XuLSg82jMvWu+25 XfBQEIlEf0uS4SKVHAsXg4EmZ2urlYM= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=Z6f2RpYI; spf=pass (imf05.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.160.46 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com Received: by mail-oa1-f46.google.com with SMTP id 586e51a60fabf-2872134c806so1562408fac.3 for ; Mon, 21 Oct 2024 18:59:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1729562368; x=1730167168; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=559wueV0PG6xFm1m5EmJll56/fKSQJhn2CptHQFJIX4=; b=Z6f2RpYIi8WhRYSekycCvz4U0u0e8zxlQ2fxVZ25fThhXWD/OEJh+xVg1U7b7LN3Il Rx/8S74SI/zSL/Bkvs/gHrVTRPX5vWF3p1JBdwXHrya8OnRFXef2r54KGHDAJuFGQkby strXr5FAkvN5LLBRIwpMa5Wd3jFA2daTnX9KuR5w+w//h7/8AfuWuXa+eEEGzeljG2fL 5nixM7yXdUiMNu9OwJeg0midylRMbJ7Vp+tb8Qa+X6fvSsA6YDR15KF1jfj9bK9/AUEA IlF/C6odwF3moAbbLUg+tvOGT5URtQJL0gQKasglXdh4KR8IBMediYJeaW42PJIvCck8 /xWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729562368; x=1730167168; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=559wueV0PG6xFm1m5EmJll56/fKSQJhn2CptHQFJIX4=; b=FOWSGT/57vmkdSQ76BFaarOtSNMniwrA/wiWaAiPj2rqmvoZvVpw+NY3YBogUutl90 N1sMehcuMXbucrfxgBxkPPK0+kZOKQIHPqBfdk//QI6WmzCXaMQ9J1gd0qSXMkZdKaJO aT0dhsvzLd6PO/YiX0aeXuw3xbckeF7iBF+0vNttfnbawyVbXes1sSzHhyaXutn1nehc Jp+FpqARRQwOb/KvWzp7zVjGYwB8BeJgasQNig68mWIWSWqm5JsNGmFTqFaQMGUHGraT n9Xauoq+jYZOjl3vb75K231YgI+ePs0QZljZ0fdgfEz9WB3miWwxU9ZnOYiLIjSdyfYg r+nA== X-Forwarded-Encrypted: i=1; AJvYcCXV4t5EdVMzvVDctfvvejWjVdIzuErAHD0R8/3KDZY/SYyq6GIqifvly85+tikv+GfA3xzmhGdXDw==@kvack.org X-Gm-Message-State: AOJu0YxeP1Zk+9DEAWjCUJ4mIzt+7zJfp0rCC6Znxup85kcNlwbNcyWe YE9F6dkCXnw47J74iAW0TIQa+NOk/o8keuzZfxtuzAMzudQ7aH3uN4UMDGRg0GU= X-Google-Smtp-Source: AGHT+IEp2UA/bF+2FyY3t++ENshpm/xo2VESMIC3hEEwlJcPqi8vvi8Dyd6MCRyyBM+qcOZbinhyoQ== X-Received: by 2002:a05:6870:95aa:b0:277:a43a:dac2 with SMTP id 586e51a60fabf-2892c2df34amr10113787fac.17.1729562368580; Mon, 21 Oct 2024 18:59:28 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71ec132ffdcsm3600710b3a.46.2024.10.21.18.59.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 18:59:28 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , kasan-dev@googlegroups.com Cc: llvm@lists.linux.dev, Catalin Marinas , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti , Will Deacon , Evgenii Stepanov , Andrew Morton , linux-arm-kernel@lists.infradead.org, Samuel Holland Subject: [PATCH v2 7/9] riscv: Align the sv39 linear map to 16 GiB Date: Mon, 21 Oct 2024 18:57:15 -0700 Message-ID: <20241022015913.3524425-8-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241022015913.3524425-1-samuel.holland@sifive.com> References: <20241022015913.3524425-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-Stat-Signature: htnbux9zyys4hkx5maopdsbgagcb9o86 X-Rspamd-Queue-Id: 32DD5100004 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1729562340-765758 X-HE-Meta: U2FsdGVkX19FDceqCXNaVG7Rtnn6r3MuARobZ9lwMVQPYl0omNNc+O1PLKpvMQAF1n2sRAn3/n8OA1v5cUoyASds2uNMidias4lMxVkjZXhhj0Fw2J6sqqATqxPPEL+PMB053eSL35Au5lQIiAF7YtFAD9+ky6NRBjsBPiP7Y6IVhtkTSnqVdYqaXIaDNRprRbmo6AdjBxr7Yq+ENJ13fj8150QbvbgQ4a8DHawC4Wm7zgNuY0KaQc3Q2pREZcqlGWyFZcXXjYrXjIjj5quuPJR2vlEZEYMtmAVc2xdykTPI6FNDBNg1dc9nqErqRtRBYchUe+B3pSoo6aaqoVI9KpJloKmjS7L+O2vr53iPQ3UyljyOZci5UUarU9R9zolxhutE2L7Idue3KSuKPc1hi6EV/4Kkil7dfuGdze2DygkH8lyj8kggzz3yqFLxghPcKU6LVrkSaHjjm6YAzbhkOhfF8SnEQcmTtYBZklel+jpivU9mD6R9OVGuxxctJsPH+Ow8IRCJxrseXExB6mnfUnbpv0CcrvDC9rwUxsWSMuxVccbqF4GUgRwHUSmjOCtda9U4hpAO3gyMv379/uw2UuwW/Uj1ZvQ0t0ObNQ6vUtgwBmEhhbSvMGLuNxl7P5xBHn3AEcGUNr+JbUF3bKqUOvtgCX/at4KlsJhNExcMRBtg7Y6f4A+WRY6lAqWTjIPjxkKCqzlh8flUcBwWzg45Fzf+IISedolaI81iocMqUR1mvG4ehErD/gBl78Ju8e0Dw1VhMq2YTha1jeOHWlG2w+/MVtEJLd9D3yExqwAE85WbUX/KdYymBVJBK2JvS2/iL5U+Hz8qkfrwl/il20hy4rvqbYMurtF++FkBuT0SMS/W+IewuHeCfemAGZHcvwgVTEeCLoK43nTS09lozfsnY34BWBrvNjGv/QZqMlbk2ICt49Z51PBNCgMvgFqNo1O6HuPDyBLRBKcJ2cFzqwJ sAnqZbWP Bv4+xBffNTCbOpQmLg6ejB1WMTVN3nEhpmEJlv87zPU142R20RW5P42KvQktiGxlZYs+13pGdUvoVFTeo2jtdWNmYgFezD5eah1ezrfMBFAQ484VraXYBl5m9yGzgXviRGUVPkghNNSWPeWAuE+rKb9QzNlB4bgiCLweH/CP+luTRJ0n8Q6N3olfJ6G4rnlNImCSGw/AK1K+wZJ3pWsrwMqSfCCTbzGzUSs5gR/GaFAxY8DOH8lJfOP/GyhpfoOevzYMCiuSP7+LVQyIiQZsOO6jpKmEckAvgpavYZrGDxXFjYAZJocQQvkHwpjuf5/SkJnL0oXEdapG5iCCWl9z3wliNaF1BHwIAGUtkCH5CWwALNRlWwF+kB+ORqPfi0nR2WfgfKfc+Mg+UsWBF80WFzd++5w== 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: The KASAN implementation on RISC-V requires the shadow memory for the vmemmap and linear map regions to be aligned to a PMD boundary (1 GiB). For KASAN_GENERIC (KASAN_SHADOW_SCALE_SHIFT == 3), this enforces 8 GiB alignment for the memory regions themselves. KASAN_SW_TAGS uses 16-byte granules (KASAN_SHADOW_SCALE_SHIFT == 4), so now the memory regions must be aligned to a 16 GiB boundary. Signed-off-by: Samuel Holland Reviewed-by: Alexandre Ghiti --- (no changes since v1) Documentation/arch/riscv/vm-layout.rst | 10 +++++----- arch/riscv/include/asm/page.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Documentation/arch/riscv/vm-layout.rst b/Documentation/arch/riscv/vm-layout.rst index eabec99b5852..c0778c421b34 100644 --- a/Documentation/arch/riscv/vm-layout.rst +++ b/Documentation/arch/riscv/vm-layout.rst @@ -47,11 +47,11 @@ RISC-V Linux Kernel SV39 | Kernel-space virtual memory, shared between all processes: ____________________________________________________________|___________________________________________________________ | | | | - ffffffc4fea00000 | -236 GB | ffffffc4feffffff | 6 MB | fixmap - ffffffc4ff000000 | -236 GB | ffffffc4ffffffff | 16 MB | PCI io - ffffffc500000000 | -236 GB | ffffffc5ffffffff | 4 GB | vmemmap - ffffffc600000000 | -232 GB | ffffffd5ffffffff | 64 GB | vmalloc/ioremap space - ffffffd600000000 | -168 GB | fffffff5ffffffff | 128 GB | direct mapping of all physical memory + ffffffc2fea00000 | -244 GB | ffffffc2feffffff | 6 MB | fixmap + ffffffc2ff000000 | -244 GB | ffffffc2ffffffff | 16 MB | PCI io + ffffffc300000000 | -244 GB | ffffffc3ffffffff | 4 GB | vmemmap + ffffffc400000000 | -240 GB | ffffffd3ffffffff | 64 GB | vmalloc/ioremap space + ffffffd400000000 | -176 GB | fffffff3ffffffff | 128 GB | direct mapping of all physical memory | | | | fffffff700000000 | -36 GB | fffffffeffffffff | 32 GB | kasan __________________|____________|__________________|_________|____________________________________________________________ diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h index 32d308a3355f..6e2f79cf77c5 100644 --- a/arch/riscv/include/asm/page.h +++ b/arch/riscv/include/asm/page.h @@ -37,7 +37,7 @@ * define the PAGE_OFFSET value for SV48 and SV39. */ #define PAGE_OFFSET_L4 _AC(0xffffaf8000000000, UL) -#define PAGE_OFFSET_L3 _AC(0xffffffd600000000, UL) +#define PAGE_OFFSET_L3 _AC(0xffffffd400000000, UL) #else #define PAGE_OFFSET _AC(CONFIG_PAGE_OFFSET, UL) #endif /* CONFIG_64BIT */ From patchwork Tue Oct 22 01:57:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13844970 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 DC538D1714E for ; Tue, 22 Oct 2024 01:59:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E4B56B00A4; Mon, 21 Oct 2024 21:59:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4934C6B00A5; Mon, 21 Oct 2024 21:59:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2EA286B00A6; Mon, 21 Oct 2024 21:59:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0551E6B00A4 for ; Mon, 21 Oct 2024 21:59:33 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B83DCC1C6E for ; Tue, 22 Oct 2024 01:59:16 +0000 (UTC) X-FDA: 82699581258.02.4E2A90B Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) by imf13.hostedemail.com (Postfix) with ESMTP id EC57B20016 for ; Tue, 22 Oct 2024 01:59:15 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=JmHjYkox; spf=pass (imf13.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.215.176 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729562222; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bo8mvyzhNAGog51IrcbhLjSHRzC25xwCT3do7aV0R5s=; b=3QTtcruisQTCzhq8vz2jYtTyWUzwQiJk8RkYHctf067v/DdXCqwupkG0Q+F/4lHpverfe/ mz4qu0KuabtrZEx1dB4Sz2KP+psNuHxLbef9OOcEfH1bpKTlEmaMy3IVlesAlsXCtBRg92 ZpJOJjNdfzQKXKYyf6ZbI4QHI8rzvqk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729562222; a=rsa-sha256; cv=none; b=vvuaySmBfr0NWDvopUBScz9bwv/GgFdHfbFo4O6fEh4nEZ4OhGA4C98kZPlPuAr8kbqbYM CUduZ9FYCqraXWEgvMeiJcTcCkNVlmq7OhYguirGvjM8K2vYy/Rz2y+HjW15OdJetehq5M WIW2pZFdPLgCxoPa9ISD1lgWeCDBjS4= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=JmHjYkox; spf=pass (imf13.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.215.176 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-7d916b6a73aso3193581a12.1 for ; Mon, 21 Oct 2024 18:59:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1729562370; x=1730167170; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bo8mvyzhNAGog51IrcbhLjSHRzC25xwCT3do7aV0R5s=; b=JmHjYkoxG9Ai4Q3v1nCMdA89FrFUfwnR0pNt0xL7j4wTtN2G475UcfLOrTzpt/+cri cLRa1I7WBqRRnK6droxKOn7D6s49jbd+BKWjKV9PXoEymcuXYvz2YiNL8iNRx4NmSfYy LWjMHOWbPk6nmFuy9SUot4iHEnoqlY578cXHucuzX92JBpBajXTYs+iUvbNkzuwBvxuc q3oqyTSJ+ZT9frhS2RfmrS65rCMwJ+p/pj0oxhk3HTU0MfYoNZLJFqiNrKkzXW5Xo3Ut VKndHsz6qHeRG7Dv53HFT0vkMMUKB+mByJed839hGjJBucnifaqVMLEQjcvHIv6Dzp2s SeZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729562370; x=1730167170; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bo8mvyzhNAGog51IrcbhLjSHRzC25xwCT3do7aV0R5s=; b=YiX4GYaH4wTw6Ht1T791ANBh8IbdGRD7UqCJSRE/pduIfrN4BWAJo5o7exe+y5mjbP qGEy7MBfbUNCyBtcfn8gi//VLsgTccokQr+kcrWKZ+w1IGwJ12IB7LM8v/TbX7T3aMD9 5nrvMavW2K6FOd6RBkJkusH0SVnYH05qvB4zPsjgeJMR3gGM0XCLsgOzUKPwjJHgXZYK WFCkv8FmrYh27/z31KStWetf5c6VHuWJI26t0ZkqfuRhhhzAsfjTGR5AhLqt2oM2s9P8 uiIsrgGW6zVbB76hbAPvLkBLbRHEbRKQ/yfufEyU7lkU18bnw9SBOMWi3Fp97Sco4WS/ pByw== X-Forwarded-Encrypted: i=1; AJvYcCUsW1lBnAURK8Jqt+H6c54CHXSBPppHp+2zPQwOtWpRy/c4ywFjOLaydkkXkftTbe1S0TzcsUZkcA==@kvack.org X-Gm-Message-State: AOJu0YxxvB5Qkb2kMmiXZ2hhjM5t55zlti2y3CIWatXbL3TGXW5h16cH MQApXT0CusizecS7GNDUBCcyml+Dn3qq/wQ9gUrQDf0NwdTjtjhP0H1KTnxirtvLoyOIPxGqaTz 8 X-Google-Smtp-Source: AGHT+IGIGEeeGnC+KScU7RsGDXnOT3F+tOuXMVMoo+fGP2CQIBFtA2wtlRRoG48p3wyuBbKw6oUq4g== X-Received: by 2002:a05:6a20:b40b:b0:1d9:b78:2dd3 with SMTP id adf61e73a8af0-1d92c5100b6mr19225561637.26.1729562370603; Mon, 21 Oct 2024 18:59:30 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71ec132ffdcsm3600710b3a.46.2024.10.21.18.59.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 18:59:29 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , kasan-dev@googlegroups.com Cc: llvm@lists.linux.dev, Catalin Marinas , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti , Will Deacon , Evgenii Stepanov , Andrew Morton , linux-arm-kernel@lists.infradead.org, =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , Samuel Holland Subject: [PATCH v2 8/9] riscv: Add SBI Firmware Features extension definitions Date: Mon, 21 Oct 2024 18:57:16 -0700 Message-ID: <20241022015913.3524425-9-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241022015913.3524425-1-samuel.holland@sifive.com> References: <20241022015913.3524425-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: EC57B20016 X-Stat-Signature: pcctgy1ru8qnm7u4xbfbghtw8os9e1yo X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1729562355-672779 X-HE-Meta: U2FsdGVkX1/urSmgoL6P9gH69ejlVOrVMm+WefEjTTMlXHc1jie11NA3t3joBloyJCX0Z36OBakUERA3Y4hNskkHkTZtMIlAdFz+EOqSnakgrj242lEADkLShT6F15xV6Po0RZBT3uljZiWuowWqvEreCYBBtGOTIfTb9G4RpwSavC4BvdXmtg1whdhRGMJ1EVNmHymlRrSldKx40rhl8RXDUT7GaOZAystPRAirBaWDTVtSbzV//Vky70yjdCnhbmrrAcJaXk11TPNPLoKJQghUjwTShDI/qGOolVumMNl2+EhxyRXej/xKXhF+iaT1y0ohrjfPqmo3bJA5h5TvfL496SFS6NvKJa/7avaNmpzaIxB8vDlhlVAFdMvpbndySuZqJHsNz7PePUvTXGd979Uy4I5Z5JvEgS8KhiGupqK7xThFt69Khbm3cJCeUuzMo9sY9GabdOCKlCdSTRJb0wF7MIZ4/OkGrAit1MxD3ogrUWZgf1+6+KtNh5dxJc4+NJ84G6Jsw8hRYsuFNpQi0pE4V6Es6X4d9hYjY3ANTAtRUY4hOGRIzDX495Dz+MPf13qARF1WiNdgUG4bjHlVF5uoOgrDnEsao6n4ogG1dylwEh7/PEfrnzT7CBRi064MYaC2Ekef2XQrJYFv/q8qS0E847cQsrkPQtsCW853yIjdbPEFvFZQzrGI6lLPfD9fQi4fcu5eJEzdYaxZVtdiS0kC73OPo8URWRnrZhQ2l8oj8g7GLEiXBnkryGBxUFRN5M49k0FX4eeQnzDn/2OxWwRhY4mBfX6QMwd/V2VrAzol5HYXvroQgw9jGxf3Z8DKa1RlkdIRMbtbXimyD2fWhetiSXjbbyyRDuQx6MKMALhI0s13MbhkgiC+wjny9NCkfvryQXt6Nz7IbAyQ0tls6C8hOQi+hGPwn7R/8Rk0OsMQnoDGM0iemyV+KW6lg3SMXapqUDiS0mpMAPeZzka su+iftlE J9H7gOUgjM9h+F0qAtPGm4cBbx30NXO0wYjC0X4jVH+5y48thhBou39t57UWkLUuUKS1VyJCHOMRBU8TBQUI+i2LQUNVhaFNdgKRXe7mqYTH4FKsgN6YkxNP46MOodjrnaBvG+/uM8J9Y6mNdJBoFKi4zFh50847jLOp4zE2YwNY1fW59sP5RTZPHFBndqUgSQGx/2VpS+B+xzHreQ7E+t141dBB5FqE677Ancf+4bMqeVev2Pz+mlz1r/PU/pEzdbLLioIJ6appyg072O65JGmQMJajICUR+nGNHrA5++BDu6lpLh5H0uAxXRkN97/hL/2AtozXRL9HHzrGrBQqCxDsA8iu7xY3cHzPQOnegpY6QqVKSXxnZ3PuKko+lKzImZyilKidVS0YRdLHSo1ELZTYyUjEkkghxQwSMSPCubPyAalmljKpmpikrRPOesLeIuaQ7sRqe7+yAMl68aDPkwX8zH8JKTjpQveVHXcvy5yObakQ= 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: Clément Léger Add necessary SBI definitions to use the FWFT extension. [Samuel: Add SBI_FWFT_POINTER_MASKING_PMLEN] Signed-off-by: Clément Léger Signed-off-by: Samuel Holland --- Changes in v2: - New patch for v2 arch/riscv/include/asm/sbi.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index 98f631b051db..4a35c6ffe49f 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -34,6 +34,7 @@ enum sbi_ext_id { SBI_EXT_PMU = 0x504D55, SBI_EXT_DBCN = 0x4442434E, SBI_EXT_STA = 0x535441, + SBI_EXT_FWFT = 0x46574654, /* Experimentals extensions must lie within this range */ SBI_EXT_EXPERIMENTAL_START = 0x08000000, @@ -281,6 +282,33 @@ struct sbi_sta_struct { #define SBI_SHMEM_DISABLE -1 +/* SBI function IDs for FW feature extension */ +#define SBI_EXT_FWFT_SET 0x0 +#define SBI_EXT_FWFT_GET 0x1 + +enum sbi_fwft_feature_t { + SBI_FWFT_MISALIGNED_EXC_DELEG = 0x0, + SBI_FWFT_LANDING_PAD = 0x1, + SBI_FWFT_SHADOW_STACK = 0x2, + SBI_FWFT_DOUBLE_TRAP = 0x3, + SBI_FWFT_PTE_AD_HW_UPDATING = 0x4, + SBI_FWFT_POINTER_MASKING_PMLEN = 0x5, + SBI_FWFT_LOCAL_RESERVED_START = 0x6, + SBI_FWFT_LOCAL_RESERVED_END = 0x3fffffff, + SBI_FWFT_LOCAL_PLATFORM_START = 0x40000000, + SBI_FWFT_LOCAL_PLATFORM_END = 0x7fffffff, + + SBI_FWFT_GLOBAL_RESERVED_START = 0x80000000, + SBI_FWFT_GLOBAL_RESERVED_END = 0xbfffffff, + SBI_FWFT_GLOBAL_PLATFORM_START = 0xc0000000, + SBI_FWFT_GLOBAL_PLATFORM_END = 0xffffffff, +}; + +#define SBI_FWFT_GLOBAL_FEATURE_BIT (1 << 31) +#define SBI_FWFT_PLATFORM_FEATURE_BIT (1 << 30) + +#define SBI_FWFT_SET_FLAG_LOCK (1 << 0) + /* SBI spec version fields */ #define SBI_SPEC_VERSION_DEFAULT 0x1 #define SBI_SPEC_VERSION_MAJOR_SHIFT 24 From patchwork Tue Oct 22 01:57:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13844971 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 66FF7D17158 for ; Tue, 22 Oct 2024 01:59:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AAFC66B00A6; Mon, 21 Oct 2024 21:59:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A37E76B00A7; Mon, 21 Oct 2024 21:59:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 865AB6B00A8; Mon, 21 Oct 2024 21:59:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 4F8666B00A6 for ; Mon, 21 Oct 2024 21:59:36 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A4AB6120E45 for ; Tue, 22 Oct 2024 01:59:21 +0000 (UTC) X-FDA: 82699581048.10.07808FB Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf17.hostedemail.com (Postfix) with ESMTP id 0B1EA4000E for ; Tue, 22 Oct 2024 01:59:21 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=SZJtKdg0; spf=pass (imf17.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729562297; a=rsa-sha256; cv=none; b=gkCDiCBB8txhU/T9WfulqpNUICwsjoIUfMCNxdNzE9UNaj/GF5dKWsqb1VnyyWL97CmRH3 w6HExA5otnj9W9nnQjW24e9FpG+ZvVnZt25S02tYF9lp9zSWcr1UKNnNitQjVQFYwcoAa4 pLEQeLIAwRt6XMdOg1svvcnEc0OXcac= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=SZJtKdg0; spf=pass (imf17.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729562297; 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=71aQqKWMHXYc2akd1hnznLqXP6CqCFxBPNX0wi4YbCg=; b=tRppwpRKOQbheGGuwTAEJW/nJavh/fjAtFk3Ft7G44IuqpbRcxrur0rxD4sJ0gNIoH73rW L9VEPPXVzQFUsFGT2TqZ5VdYpU+BKnaRgT2rdL42wXg69xoxkpYeDiXPZvdprI2ynma6sr zKe2dxGVVETfwewvkh9i2QbLD90GhMY= Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-20ca1b6a80aso47740885ad.2 for ; Mon, 21 Oct 2024 18:59:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1729562373; x=1730167173; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=71aQqKWMHXYc2akd1hnznLqXP6CqCFxBPNX0wi4YbCg=; b=SZJtKdg05Z5hV3SByle1shFlegDcuZoTnbT3KWY/RCXHvz2NgrUtFug/8peh11+4RU Hl7+4zyM5RuQF4sCD45pfodvD2q1Vsp4Y5li1VT7W6Z5tumBIIxLttaum12ZFZtvMlwI TpD5G+6kcJF1REIlWu4eQoTwpnpaGI1d9ybE58cofcrbrPf/wJO76cF5HcZohmoWDFft //r671e2z1c3vuLEM6Ishd7A7WN5ZAC8oO5A26+sVk+ZP5L7zXO/en3XBqdAefFi8hf0 0O9ZTSN4HaP2MS2pXRZUXO/y30BsYDgdmFvbfI6UiqBBSi0KY9Qlg3XQNwp4BhYTaLr6 Nd2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729562373; x=1730167173; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=71aQqKWMHXYc2akd1hnznLqXP6CqCFxBPNX0wi4YbCg=; b=uTr2jip3AuvBkGB2jBijBnxwNNThQG4ReT1bWlFy/uv/DxR51lLFohd7bTZWwOC4Gc 16v1j71WSpNSgyciAPoCwoQP02OlHDwaInmgdSN8xREa63S57f7YrrbNv023KhHy4tE4 laK3r2O6LCIt3GZSdoZf4cYAKt/R46KCvLfbeLRTWLl2LHMpbqk17wZzWZh6R0R1PzCc phtuvWdfRlZrnMgQ+lKOiTT20fkng1ArCq2IevM2cDQEujtuzUhXGnOUEq3JtoGeYdmd 7hHZTuXJXSEsH7NMO3PH0o/Yh5bD3RHzP5r1U/JTdRdgi4KPsPpOriLdJkezPDVU/CpY xYVw== X-Forwarded-Encrypted: i=1; AJvYcCUHtW9ssztjkNIx9qZQiS+YO+spC9qQxpXeQJjm8Ab5WqdhZpfz2iuOVlQUKqVXa253V92jQjtGAQ==@kvack.org X-Gm-Message-State: AOJu0YylRN9TSGp7Wh2HEebKaYWe/YRuQM2/sbQry4UI5EGlKLlA9EOM XCVejUkwb9PxJmIlXfzeIGIX3nO4xyc9u+NaPfnBuRdtzmI4EdhYgpdlHBMQDOs= X-Google-Smtp-Source: AGHT+IFV2z0GV96s/ePkXg1tapAZlwikiDMmkPYrz9DE1sLSgHPnQXvAoedURgE4MOGtScwinFMLZA== X-Received: by 2002:a05:6a21:2d08:b0:1d9:1906:a68e with SMTP id adf61e73a8af0-1d92c56cd23mr19203945637.34.1729562372514; Mon, 21 Oct 2024 18:59:32 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71ec132ffdcsm3600710b3a.46.2024.10.21.18.59.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 18:59:32 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , kasan-dev@googlegroups.com Cc: llvm@lists.linux.dev, Catalin Marinas , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti , Will Deacon , Evgenii Stepanov , Andrew Morton , linux-arm-kernel@lists.infradead.org, Samuel Holland Subject: [PATCH v2 9/9] riscv: Implement KASAN_SW_TAGS Date: Mon, 21 Oct 2024 18:57:17 -0700 Message-ID: <20241022015913.3524425-10-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241022015913.3524425-1-samuel.holland@sifive.com> References: <20241022015913.3524425-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-Stat-Signature: g9zzd1pyd3ayngesqxutkikctbmhu1qn X-Rspamd-Queue-Id: 0B1EA4000E X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1729562361-80478 X-HE-Meta: U2FsdGVkX19I1xK2HSqX66ttAEuBLFLcsQu5omzYDDJ8OG2btEf4HzKIVz9cmHSUZ9dRzpmWtz4N7SzgTwqyyr57Iixm+Pca6Mm+7p8JRGUkZsqJlMj+shQlw7rPB6cr8coi0I57lpKqA43HPsDM++Wc6ndOvGsqKVq+dPAQEC7Vi1o2bQemv7VY3+FCU3eviVBKJT0+VCOA5pn6XsWXTSt5550CaC17VWILu2KQsHdTgyDPq+/wLoXGmdJo5GiHiu6TOosw0beUWxCKhe5cVINxsl/S9Qa42sIqaLRavdBoib4qlnKLQPdGR/tZMN7xMZqHLw8fDCqsH5EWv1uBIUbHXJuOdJrUEzDAC853sxTYvl1vdVaeoT6zim/Rwga/Y0cc6N9RWk5g7h4rykvHAU/Rts9OEVMRXoohejHNzuNf4MnFrvGb9s+tTMhgGIk7b/Xg+ED4ewP9eVn671Nx594N3q5G6x3TnkGtC6O5O1si2xq/Ip8xIBpMtmHK+Y9I2noo+Q/GziqPZbo3F4zmA9RNzHpVA+pHV+sLZc5Ab2kFKxlPx5Aq21Kj/UaJ59fIbqVKX03O1koOrcougCIzUPQvCNomZItwoGmB/S3Hlk8T7WhBgIB8FSijGrwtw+7yV0Jx3BRApWjz5t7pZQICW+k8mCYmXJCP5Qd+LFMOSCHNIEs/mT4nLjPWnsASdxW6PB9U11U8+Tj8UtB4eKA9/w64Efa5E39B69TkpJvsgwkvbCtOhF683Z/AMMnWC1X8hHrOy/KlQtDpR5RYpZKUjWElky4jUGIlLWPCUcx9CbljiygDnGrYEipmMxsN5BxFS7rzJu+9BmQ2Hn7JrJYgmbz2v0RKy3yvbqe0b8J7ZROVWHQyI+imV+JC01Agx586wpb/tOQvaSDUfMsiFvKiy6YHcXBU5DvH5KxfpueK0fGy2ILF/c+jZ/hmsim0gPp+QJWwq1vlejbPteasjSG ZVMYHcHQ HTsriNLlmVebNjcVoQU2rrz1jLEKm3vJ86SWFT6IydfocD/IS/GAsWvF7A74ePuSJ3WfTT87hzb1DTv5PDRrGqmFW/e6Z2pGysRMo8xIzN4fhc4+2F4JDgIq8XzrEZnNGOxO4t9FSsFwHUMTopA5CAFTk6tS5Tq5nkZHONis5Lxz7wFu0NTmLJv85LstZ7wgX0sLNvqaVgg6QzIqcuDiJD4XTb4NGZM2dy9mbgt6EDdS6H7TvN+2gZjFt6rjNRsymgv/lhXF5cSr8/QG/jL2xvfbjzc5l6bWm8gFHir1awe4NGmnvkRDGe3L1uq8ns9NXyDQK9MGkDHqIAg25V4PTw/GZjxsl/iC81NGspCabk00aUdoewnDgLG2VdvQMuqtiSJl0wbVd6CyMbOEqsjqsLKNyFUHUrfN+cPaez5cqYglTDWhCGlaPhiOA0BASfTJaY0Lu 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: Implement support for software tag-based KASAN using the RISC-V pointer masking extension, which supports 7 and/or 16-bit tags. This implemen- tation uses 7-bit tags, so it is compatible with either hardware mode. Pointer masking is an optional ISA extension, and it must be enabled using an SBI call to firmware on each CPU. This SBI call must be made very early in smp_callin(), as dereferencing any tagged pointers before that point will crash the kernel. If the SBI call fails on the boot CPU, then KASAN is globally disabled, and the kernel boots normally (unless stack tagging is enabled). If the SBI call fails on any other CPU, that CPU is excluded from the system. When pointer masking is enabled for the kernel's privilege mode, it must be more careful about accepting tagged pointers from userspace. Normally, __access_ok() accepts tagged aliases of kernel memory as long as the MSB is zero, since those addresses cannot be dereferenced -- they will cause a page fault in the uaccess routines. But when the kernel is using pointer masking, those addresses are dereferenceable, so __access_ok() must specifically check the most-significant non-tag bit instead of the MSB. Pointer masking does not apply to the operands of fence instructions, so software is responsible for untagging those addresses. Signed-off-by: Samuel Holland --- Changes in v2: - Fix build error with KASAN_GENERIC - Use symbolic definitons for SBI firmware features call - Update indentation in scripts/Makefile.kasan - Use kasan_params to set hwasan-generate-tags-with-calls=1 Documentation/dev-tools/kasan.rst | 14 ++++--- arch/riscv/Kconfig | 4 +- arch/riscv/include/asm/cache.h | 4 ++ arch/riscv/include/asm/kasan.h | 20 ++++++++++ arch/riscv/include/asm/page.h | 19 ++++++++-- arch/riscv/include/asm/pgtable.h | 6 +++ arch/riscv/include/asm/tlbflush.h | 4 +- arch/riscv/kernel/setup.c | 6 +++ arch/riscv/kernel/smpboot.c | 8 +++- arch/riscv/lib/Makefile | 2 + arch/riscv/lib/kasan_sw_tags.S | 61 +++++++++++++++++++++++++++++++ arch/riscv/mm/kasan_init.c | 32 +++++++++++++++- arch/riscv/mm/physaddr.c | 4 ++ scripts/Makefile.kasan | 5 +++ 14 files changed, 174 insertions(+), 15 deletions(-) create mode 100644 arch/riscv/lib/kasan_sw_tags.S diff --git a/Documentation/dev-tools/kasan.rst b/Documentation/dev-tools/kasan.rst index d7de44f5339d..6548aebac57f 100644 --- a/Documentation/dev-tools/kasan.rst +++ b/Documentation/dev-tools/kasan.rst @@ -22,8 +22,8 @@ architectures, but it has significant performance and memory overheads. Software Tag-Based KASAN or SW_TAGS KASAN, enabled with CONFIG_KASAN_SW_TAGS, can be used for both debugging and dogfood testing, similar to userspace HWASan. -This mode is only supported for arm64, but its moderate memory overhead allows -using it for testing on memory-restricted devices with real workloads. +This mode is only supported on arm64 and riscv, but its moderate memory overhead +allows using it for testing on memory-restricted devices with real workloads. Hardware Tag-Based KASAN or HW_TAGS KASAN, enabled with CONFIG_KASAN_HW_TAGS, is the mode intended to be used as an in-field memory bug detector or as a @@ -340,12 +340,14 @@ Software Tag-Based KASAN ~~~~~~~~~~~~~~~~~~~~~~~~ Software Tag-Based KASAN uses a software memory tagging approach to checking -access validity. It is currently only implemented for the arm64 architecture. +access validity. It is currently only implemented for the arm64 and riscv +architectures. Software Tag-Based KASAN uses the Top Byte Ignore (TBI) feature of arm64 CPUs -to store a pointer tag in the top byte of kernel pointers. It uses shadow memory -to store memory tags associated with each 16-byte memory cell (therefore, it -dedicates 1/16th of the kernel memory for shadow memory). +or the pointer masking (Sspm) feature of RISC-V CPUs to store a pointer tag in +the top byte of kernel pointers. It uses shadow memory to store memory tags +associated with each 16-byte memory cell (therefore, it dedicates 1/16th of the +kernel memory for shadow memory). On each memory allocation, Software Tag-Based KASAN generates a random tag, tags the allocated memory with this tag, and embeds the same tag into the returned diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 62545946ecf4..d08b99f6bf76 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -121,6 +121,7 @@ config RISCV select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL select HAVE_ARCH_JUMP_LABEL_RELATIVE if !XIP_KERNEL select HAVE_ARCH_KASAN if MMU && 64BIT + select HAVE_ARCH_KASAN_SW_TAGS if MMU && 64BIT select HAVE_ARCH_KASAN_VMALLOC if MMU && 64BIT select HAVE_ARCH_KFENCE if MMU && 64BIT select HAVE_ARCH_KGDB if !XIP_KERNEL @@ -291,7 +292,8 @@ config PAGE_OFFSET config KASAN_SHADOW_OFFSET hex - depends on KASAN_GENERIC + depends on KASAN + default 0xffffffff00000000 if KASAN_SW_TAGS default 0xdfffffff00000000 if 64BIT default 0xffffffff if 32BIT diff --git a/arch/riscv/include/asm/cache.h b/arch/riscv/include/asm/cache.h index 570e9d8acad1..232288a060c6 100644 --- a/arch/riscv/include/asm/cache.h +++ b/arch/riscv/include/asm/cache.h @@ -16,6 +16,10 @@ #define ARCH_KMALLOC_MINALIGN (8) #endif +#ifdef CONFIG_KASAN_SW_TAGS +#define ARCH_SLAB_MINALIGN (1ULL << KASAN_SHADOW_SCALE_SHIFT) +#endif + /* * RISC-V requires the stack pointer to be 16-byte aligned, so ensure that * the flat loader aligns it accordingly. diff --git a/arch/riscv/include/asm/kasan.h b/arch/riscv/include/asm/kasan.h index a4e92ce9fa31..f6b378ba936d 100644 --- a/arch/riscv/include/asm/kasan.h +++ b/arch/riscv/include/asm/kasan.h @@ -25,7 +25,11 @@ * KASAN_SHADOW_OFFSET = KASAN_SHADOW_END - * (1ULL << (64 - KASAN_SHADOW_SCALE_SHIFT)) */ +#if defined(CONFIG_KASAN_GENERIC) #define KASAN_SHADOW_SCALE_SHIFT 3 +#elif defined(CONFIG_KASAN_SW_TAGS) +#define KASAN_SHADOW_SCALE_SHIFT 4 +#endif #define KASAN_SHADOW_SIZE (UL(1) << ((VA_BITS - 1) - KASAN_SHADOW_SCALE_SHIFT)) /* @@ -37,6 +41,14 @@ #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL) +#ifdef CONFIG_KASAN_SW_TAGS +#define KASAN_TAG_KERNEL 0x7f /* native kernel pointers tag */ +#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) + void kasan_init(void); asmlinkage void kasan_early_init(void); void kasan_swapper_init(void); @@ -48,5 +60,13 @@ void kasan_swapper_init(void); #endif /* CONFIG_KASAN */ +#ifdef CONFIG_KASAN_SW_TAGS +bool kasan_boot_cpu_enabled(void); +int kasan_cpu_enable(void); +#else +static inline bool kasan_boot_cpu_enabled(void) { return false; } +static inline int kasan_cpu_enable(void) { return 0; } +#endif + #endif #endif /* __ASM_KASAN_H */ diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h index 6e2f79cf77c5..43c625a4894d 100644 --- a/arch/riscv/include/asm/page.h +++ b/arch/riscv/include/asm/page.h @@ -89,6 +89,16 @@ typedef struct page *pgtable_t; #define PTE_FMT "%08lx" #endif +#ifdef CONFIG_KASAN_SW_TAGS +#define __tag_set(addr, tag) ((void *)((((u64)(addr) << 7) >> 7) | ((u64)(tag) << 57))) +#define __tag_reset(addr) ((void *)((s64)((u64)(addr) << 7) >> 7)) +#define __tag_get(addr) ((u8)((u64)(addr) >> 57)) +#else +#define __tag_set(addr, tag) (addr) +#define __tag_reset(addr) (addr) +#define __tag_get(addr) 0 +#endif + #if defined(CONFIG_64BIT) && defined(CONFIG_MMU) /* * We override this value as its generic definition uses __pa too early in @@ -168,7 +178,7 @@ phys_addr_t linear_mapping_va_to_pa(unsigned long x); #endif #define __va_to_pa_nodebug(x) ({ \ - unsigned long _x = x; \ + unsigned long _x = (unsigned long)__tag_reset(x); \ is_linear_mapping(_x) ? \ linear_mapping_va_to_pa(_x) : kernel_mapping_va_to_pa(_x); \ }) @@ -192,7 +202,10 @@ extern phys_addr_t __phys_addr_symbol(unsigned long x); #define pfn_to_virt(pfn) (__va(pfn_to_phys(pfn))) #define virt_to_page(vaddr) (pfn_to_page(virt_to_pfn(vaddr))) -#define page_to_virt(page) (pfn_to_virt(page_to_pfn(page))) +#define page_to_virt(page) ({ \ + __typeof__(page) __page = page; \ + __tag_set(pfn_to_virt(page_to_pfn(__page)), page_kasan_tag(__page)); \ +}) #define page_to_phys(page) (pfn_to_phys(page_to_pfn(page))) #define phys_to_page(paddr) (pfn_to_page(phys_to_pfn(paddr))) @@ -209,7 +222,7 @@ static __always_inline void *pfn_to_kaddr(unsigned long pfn) #endif /* __ASSEMBLY__ */ #define virt_addr_valid(vaddr) ({ \ - unsigned long _addr = (unsigned long)vaddr; \ + unsigned long _addr = (unsigned long)__tag_reset(vaddr); \ (unsigned long)(_addr) >= PAGE_OFFSET && pfn_valid(virt_to_pfn(_addr)); \ }) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index e79f15293492..ae6fa9dba0fc 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -916,7 +916,13 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte) */ #ifdef CONFIG_64BIT #define TASK_SIZE_64 (PGDIR_SIZE * PTRS_PER_PGD / 2) +/* + * When pointer masking is enabled for the kernel's privilege mode, + * __access_ok() must reject tagged aliases of kernel memory. + */ +#ifndef CONFIG_KASAN_SW_TAGS #define TASK_SIZE_MAX LONG_MAX +#endif #ifdef CONFIG_COMPAT #define TASK_SIZE_32 (_AC(0x80000000, UL) - PAGE_SIZE) diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h index 72e559934952..68b3a85c6960 100644 --- a/arch/riscv/include/asm/tlbflush.h +++ b/arch/riscv/include/asm/tlbflush.h @@ -31,14 +31,14 @@ static inline void local_flush_tlb_all_asid(unsigned long asid) /* Flush one page from local TLB */ static inline void local_flush_tlb_page(unsigned long addr) { - ALT_SFENCE_VMA_ADDR(addr); + ALT_SFENCE_VMA_ADDR(__tag_reset(addr)); } static inline void local_flush_tlb_page_asid(unsigned long addr, unsigned long asid) { if (asid != FLUSH_TLB_NO_ASID) - ALT_SFENCE_VMA_ADDR_ASID(addr, asid); + ALT_SFENCE_VMA_ADDR_ASID(__tag_reset(addr), asid); else local_flush_tlb_page(addr); } diff --git a/arch/riscv/kernel/setup.c b/arch/riscv/kernel/setup.c index a2cde65b69e9..fdc72edc4857 100644 --- a/arch/riscv/kernel/setup.c +++ b/arch/riscv/kernel/setup.c @@ -299,6 +299,12 @@ void __init setup_arch(char **cmdline_p) riscv_user_isa_enable(); } +void __init smp_prepare_boot_cpu(void) +{ + if (kasan_boot_cpu_enabled()) + kasan_init_sw_tags(); +} + bool arch_cpu_is_hotpluggable(int cpu) { return cpu_has_hotplug(cpu); diff --git a/arch/riscv/kernel/smpboot.c b/arch/riscv/kernel/smpboot.c index 0f8f1c95ac38..a1cc555691b0 100644 --- a/arch/riscv/kernel/smpboot.c +++ b/arch/riscv/kernel/smpboot.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -210,7 +211,11 @@ void __init smp_cpus_done(unsigned int max_cpus) asmlinkage __visible void smp_callin(void) { struct mm_struct *mm = &init_mm; - unsigned int curr_cpuid = smp_processor_id(); + unsigned int curr_cpuid; + + /* Must be called first, before referencing any dynamic allocations */ + if (kasan_boot_cpu_enabled() && kasan_cpu_enable()) + return; if (has_vector()) { /* @@ -225,6 +230,7 @@ asmlinkage __visible void smp_callin(void) mmgrab(mm); current->active_mm = mm; + curr_cpuid = smp_processor_id(); store_cpu_topology(curr_cpuid); notify_cpu_starting(curr_cpuid); diff --git a/arch/riscv/lib/Makefile b/arch/riscv/lib/Makefile index 8eec6b69a875..ae36616fe1f5 100644 --- a/arch/riscv/lib/Makefile +++ b/arch/riscv/lib/Makefile @@ -20,3 +20,5 @@ lib-$(CONFIG_RISCV_ISA_ZBC) += crc32.o obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o lib-$(CONFIG_RISCV_ISA_V) += xor.o lib-$(CONFIG_RISCV_ISA_V) += riscv_v_helpers.o + +obj-$(CONFIG_KASAN_SW_TAGS) += kasan_sw_tags.o diff --git a/arch/riscv/lib/kasan_sw_tags.S b/arch/riscv/lib/kasan_sw_tags.S new file mode 100644 index 000000000000..f7d3e0acba6a --- /dev/null +++ b/arch/riscv/lib/kasan_sw_tags.S @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2020 Google LLC + * Copyright (C) 2024 SiFive + */ + +#include + +/* + * Report a tag mismatch detected by tag-based KASAN. + * + * A compiler-generated thunk calls this with a custom calling convention. + * Upon entry to this function, the following registers have been modified: + * + * x1/ra: clobbered by call to this function + * x2/sp: decremented by 256 + * x6/t1: tag from shadow memory + * x7/t2: tag from pointer + * x10/a0: fault address + * x11/a1: fault description + * x28/t3: clobbered by thunk + * x29/t4: clobbered by thunk + * x30/t5: clobbered by thunk + * x31/t6: clobbered by thunk + * + * The caller has decremented the SP by 256 bytes, and stored the following + * registers in slots on the stack according to their number (sp + 8 * xN): + * + * x1/ra: return address to user code + * x8/s0/fp: saved value from user code + * x10/a0: saved value from user code + * x11/a1: saved value from user code + */ +SYM_CODE_START(__hwasan_tag_mismatch) + /* Store the remaining unclobbered caller-saved regs */ + sd t0, (8 * 5)(sp) + sd a2, (8 * 12)(sp) + sd a3, (8 * 13)(sp) + sd a4, (8 * 14)(sp) + sd a5, (8 * 15)(sp) + sd a6, (8 * 16)(sp) + sd a7, (8 * 17)(sp) + + /* a0 and a1 are already set by the thunk */ + ld a2, (8 * 1)(sp) + call kasan_tag_mismatch + + ld ra, (8 * 1)(sp) + ld t0, (8 * 5)(sp) + ld a0, (8 * 10)(sp) + ld a1, (8 * 11)(sp) + ld a2, (8 * 12)(sp) + ld a3, (8 * 13)(sp) + ld a4, (8 * 14)(sp) + ld a5, (8 * 15)(sp) + ld a6, (8 * 16)(sp) + ld a7, (8 * 17)(sp) + addi sp, sp, 256 + ret +SYM_CODE_END(__hwasan_tag_mismatch) +EXPORT_SYMBOL(__hwasan_tag_mismatch) diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c index c301c8d291d2..50f0e7a03cc8 100644 --- a/arch/riscv/mm/kasan_init.c +++ b/arch/riscv/mm/kasan_init.c @@ -11,6 +11,10 @@ #include #include +#ifdef CONFIG_KASAN_SW_TAGS +static bool __kasan_boot_cpu_enabled __ro_after_init; +#endif + /* * Kasan shadow region must lie at a fixed address across sv39, sv48 and sv57 * which is right before the kernel. @@ -323,8 +327,11 @@ asmlinkage void __init kasan_early_init(void) { uintptr_t i; - BUILD_BUG_ON(KASAN_SHADOW_OFFSET != - KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT))); + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + BUILD_BUG_ON(KASAN_SHADOW_OFFSET != + KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT))); + else + BUILD_BUG_ON(KASAN_SHADOW_OFFSET != KASAN_SHADOW_END); for (i = 0; i < PTRS_PER_PTE; ++i) set_pte(kasan_early_shadow_pte + i, @@ -356,6 +363,10 @@ asmlinkage void __init kasan_early_init(void) KASAN_SHADOW_START, KASAN_SHADOW_END); local_flush_tlb_all(); + +#ifdef CONFIG_KASAN_SW_TAGS + __kasan_boot_cpu_enabled = !kasan_cpu_enable(); +#endif } void __init kasan_swapper_init(void) @@ -534,3 +545,20 @@ void __init kasan_init(void) csr_write(CSR_SATP, PFN_DOWN(__pa(swapper_pg_dir)) | satp_mode); local_flush_tlb_all(); } + +#ifdef CONFIG_KASAN_SW_TAGS +bool kasan_boot_cpu_enabled(void) +{ + return __kasan_boot_cpu_enabled; +} + +int kasan_cpu_enable(void) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_FWFT, SBI_EXT_FWFT_SET, + SBI_FWFT_POINTER_MASKING_PMLEN, 7, 0, 0, 0, 0); + + return sbi_err_map_linux_errno(ret.error); +} +#endif diff --git a/arch/riscv/mm/physaddr.c b/arch/riscv/mm/physaddr.c index 18706f457da7..6d1cf6ffd54e 100644 --- a/arch/riscv/mm/physaddr.c +++ b/arch/riscv/mm/physaddr.c @@ -8,6 +8,8 @@ phys_addr_t __virt_to_phys(unsigned long x) { + x = __tag_reset(x); + /* * Boundary checking aginst the kernel linear mapping space. */ @@ -24,6 +26,8 @@ phys_addr_t __phys_addr_symbol(unsigned long x) unsigned long kernel_start = kernel_map.virt_addr; unsigned long kernel_end = kernel_start + kernel_map.size; + x = __tag_reset(x); + /* * Boundary checking aginst the kernel image mapping. * __pa_symbol should only be used on kernel symbol addresses. diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan index 693dbbebebba..72a8c9d5fb0e 100644 --- a/scripts/Makefile.kasan +++ b/scripts/Makefile.kasan @@ -91,6 +91,11 @@ ifeq ($(call clang-min-version, 150000)$(call gcc-min-version, 130000),y) kasan_params += hwasan-kernel-mem-intrinsic-prefix=1 endif +# RISC-V requires dynamically determining if stack tagging can be enabled. +ifdef CONFIG_RISCV + kasan_params += hwasan-generate-tags-with-calls=1 +endif + endif # CONFIG_KASAN_SW_TAGS # Add all as-supported KASAN LLVM parameters requested by the configuration.