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: 13844974 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 86C9ED17157 for ; Tue, 22 Oct 2024 02:01:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=wocK7TQzkoi26+gJDFAk5P/7Zsxrr7khGMBEFTOLFds=; b=LdzSpBacIUzyc8 Nwefrjr4HXkzomIZWaOjcl86wfrgqV5erCuGuP2PH8PSVfs/U7hX6DuhywU7oLWCTJO9s7oQw0qDv MwFiowVyBCzbeaIBansCwILrR+BOj7cseIqiiGgcXO1YjbB/AWFqVN1xrj+dDlPxuxb1tiB7Cgbyw uQhkMxZnZAT70cjBYNS0vdZYQYZRgxCqjuPVDJY8kkWS5QsXvlRK5qCHebMRVhKq7cDeezMAsPQLR MZdafgDCvnxUTXNNx7Jrs9E4Id6GY9azHKMcQoApwPtKX1+kgk/UB7cWHHvYrZmctTGHK5FOGxMUS n9QeEIJtLxbbmlsB4flg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t34Cr-00000009GTy-1NWT; Tue, 22 Oct 2024 02:01:09 +0000 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t34B5-00000009Fku-0y4Y for linux-riscv@lists.infradead.org; Tue, 22 Oct 2024 01:59:20 +0000 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-71e953f4e7cso3383999b3a.3 for ; Mon, 21 Oct 2024 18:59:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1729562358; x=1730167158; darn=lists.infradead.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=Znmu8H+/F0rgiwfmtG4fUrAiRxvpRior8S4Mh8W0pPeYXFEBgCe0SWa55uFyNLAhw6 75coeO2Dqem6m+dmxCWovEOghP3dcyRlKI01cV4ycp3AAijVt+z5FUgxQxDHW5r6tJqw gKfjncnKAGa0tYzYaiKWFJJABwu4llNnphzMqF6IdTBd2+4YMtDCyOHj3Iq6i6KqTljP 4g5Zs6eyKvv6vA7pJ/py7VFbudFb450vf+nSYgWwl9zJ7aiUy8qV1pWt7ONebw0VdyKN nbPq6+pkLfaoNaX+iIZYrd0/Hn3ejcgc5bVn8/k6bgDNsYWJQVDKvJTLrYsyfkb1W+Dm MtBQ== 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=p+tweCsO1w1PIEnR7VAMl5aLuXoe4/7xcVrCPcSgxpBbhYOHPSMKsOaacK8Tk67rVv nhEss1bv+AKzSbQxPtAmpOgmmwkJiuBCsl2XzH7MGa9UIb7nG9c5c5C7lo9c76zR3Fkl RF7SgsOIlC78avRLmE1iVIabd7hWTCTTWtI6q/lMr7a+QNMvetZb8e9EFW+V/yjAD8Ef 1yKtloAebjRSrVCnAtAGRKMZzRCveYu8alYZiMKu6w3qs5Em2KpTOdfLvMhbEEctPHbc HmEMPoQLeuJuGJDJjl+pWCWudDUQp2yohKIZUZdXkG0HSRqv6SMl6qQm+Jv9nzCs8U7y 28Dg== X-Forwarded-Encrypted: i=1; AJvYcCXS7HntZsyNwKGgLBsoT8o162Z+ThjCOPjOraI6xXfPRU5DwPzssRkWlz0gdjYu0noIiPwsWwnR0sqy0g==@lists.infradead.org X-Gm-Message-State: AOJu0Yzea7YyeMGmz5EmffyLFiBny1WB+ZVUICEpYF25IkO1vdHVrW3D kf8qAV+8Kz/c6j7HglAp4Waf78V+ggRzTQDgTZikBTt5u7sk6MyHGcDvHE4xICM= 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241021_185919_301860_3A53A7F9 X-CRM114-Status: GOOD ( 29.14 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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: 13844983 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 727D6D17157 for ; Tue, 22 Oct 2024 02:02:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=i96ZrBbL2l3AnIKUOE/L4tq4mIx5GbHwRRgQ3zQ00kM=; b=z2impER4z3OAV1 LE2oTNGukVYs7Ku6b9dGjYZFAPrbSaCDH/iZc3hKCZ1iscNZFTZlDBrqIAu73tmqPG+9ux81amsP7 vW8CCpOwBmsNH1OUeAKuDpsVeYUpAiLOIfJnNNq/hibZySQMSWppBKwqbxvPEKe7clgOQdRUqNq7B 7Bogk9uAxfn7+pJEjY9qQR5avPTpdVyhJ0Np7QPNu9DjsriHWCWkjvMdc02X6T/chsowzMQmtkunF qUXSzEudF3RovzhDBFrElVJTNZKoMK4SYp+9FFoT7wLfDjfaIA8deZY5QWrT0EHWlJ87fATVDNcz1 1nyuVZ4EI+Inw/RU17XQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t34ES-00000009GwE-0P3C; Tue, 22 Oct 2024 02:02:48 +0000 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t34B7-00000009Fll-07iM for linux-riscv@lists.infradead.org; Tue, 22 Oct 2024 01:59:22 +0000 Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-71ec997ad06so1150476b3a.3 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=lists.infradead.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=DqwkLSwxMIblcwfWpRPw5WwCNEG+IbfUq1ikaezry4wC05ecdGHM6DcTVYNfuMFKOJ r/LTQh/cqxiuYpa/xgixv0uDf+U8acSkCp6OcgDHJ80Z0DpRsSFxIJoc61Yt8PvaAabA H4P6rqn/qPnZpsE79y1XXxdAw/Czib2kxJb92qOkLNgwGCja80K+I72iQTkNb4i23ftS K8+weE06u8Lu/lzNyiXg3vft2VgySxdY5ASzEftLwYBdM4EO0cmBKkyOmR9BSek3rgpi 5/mTPX36FOa41l3DDd6RvPJT9+GUipC0DNrezzVpwSQ68EpSqmTmu+dp5G96Ry6nM27b GDcg== 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=uVrH4O5vpGdhHSYJV7WNPY9Fv25qSpOEZXq6JagkeBZ6B0zhd9of+/X+t9ixKq3g5k WTPtlNCm/dENDOI+snrc5mgDU5IS/NVaG+qURY/vxKt3e2zdKOhCt6Nt+WGYZ8TBIDGE oOlGYMKEBjApSy19imxMUlOIcvpfWOGtwui0ETTKnxJzDZBI6nv7b91PTVhkUqThqlhI HpY6Lyvcus7vjHgyIpnmldrtNwhButYrfErpqlI/ix4Qy4cn3BKHIE8qP3rtsVC1DRJ1 ZumV2XZPJV97RdMehS6YazbRN/ENjJN4YIPFML4usfn2g4OLEsNslafBAmiaDd7cKq/5 wpTg== X-Forwarded-Encrypted: i=1; AJvYcCUXFiG1Z1cs7gEPdUjCIiq2WXyVoxmZHn3aEeL+fX3ByEQZSXAWC4a36JolcXFbgcDf15XZWXpZmJNb3g==@lists.infradead.org X-Gm-Message-State: AOJu0YyZQ5IhVE91f+kuxUXgb/ThklMXpEEYj7ZnESSAGxZ85jL8i31a PwOZzSXvLwvpvFBu+vEm4/Kyps2YtWx8FXKzJG82SLf0lOHgx7Qkp1o2g5nMhF7SOGI3ZykWU8a p 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241021_185921_261839_77211230 X-CRM114-Status: GOOD ( 14.49 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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: 13844984 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0AF3BD17157 for ; Tue, 22 Oct 2024 02:05:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=y4AviXPkJY6euKJnCI6tFQpPC4kJpYcVX6VLTaWoyac=; b=C7Qw1EbGce5Sak BRqcBODr/HrhJhy5UQIqyTBLpmdjE1hm39NYaJqokca9vB1jgahgH8JM7WQbCUbtGvJ62NHZBWGtC fwDpTXBMgfuuEYkcOU42w+xPumFg4scnwUDK1TJt421KYeQDJET5eLnr4ElVORB4GpBA5fTmJc4s6 5GgBdHSSu9CCs9E10XAyTMRlm6gDVrpDszQ832AJUGwGjDGTwjtjwKsMDmYDISaj9XbcNH92kU2az dSw+torLPNgLGn+lSMvABT5cgWFxE5VoTnRhp0p2PCFcMhuUYYjFYEtPppSO9qe5/7pUQDapxrUon gU2XHG2J77SXUt4MU16w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t34HO-00000009HRs-1E7f; Tue, 22 Oct 2024 02:05:50 +0000 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t34B8-00000009Fmf-2po9 for linux-riscv@lists.infradead.org; Tue, 22 Oct 2024 01:59:24 +0000 Received: by mail-pg1-x533.google.com with SMTP id 41be03b00d2f7-7ea8de14848so3078741a12.2 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=lists.infradead.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=RjU0oPD7oXD+tWMcDuFQbnpYbwgcS/6n5ftq+xWErEa7IsRrNRv7HKxV+ZEc7ef2qO FwjvUdnbTN1W44f4PmRcHDronghY+wxKbSEpbCVRddKIkv/i5gVgstIKzLLt9g2itoCx rze5GfTMydmioM8/Z23DEqsoZxolfRQ6GsWy3y5flhHtWv9NigwvX/bj6odFJdSzsDHF SsruXeZBQND+Yn+6S9+8mcwjp1xziuEJkQyhfu/tuDdXyDZFXrI+8CHI0Wlz8KJ+bnAc k4zxALwozIFD6uRqe2nDZqylyRO/SBvrl7nxsRvHxWTUoHuz+Lq/ZQqPFCaP0kHMvX+q Bduw== 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=B9eGJK+50SWoMTp6CRerp6NjW0FMliRXjpgydr1TGzt+SACF3mBdLREt37Jeg7nSpg nDyHCCVWZfGe3gEA0kc6BhB2CVgbHYVJhExpOzYklz0gkkJMIPztWLFNdYiuV3RuqWtc uQJ57R6xXofCes1BiRWHRqJB6nTIpWLmoIG98ofkcWqfT5Wjz3YNTnZd7mfd2dvt8xm+ xEqlEq3Flu9OkzadDXJjPW/O1lkowCRGvYcfWIPgLhdXj3K1uIppznkaSs2kzCiwHuC9 Bo0Rwa2PpQn/NXEaB4ff7hYPMqU6QFg03kmIcndsZ2UHmoy4TekYaLsRcsNQv+wDIwyG 5AJA== X-Forwarded-Encrypted: i=1; AJvYcCX9NJEUohEH2qIXDWeF9PxfTKSgaqshhhYTnC/Cm5Pr7Eev2I1wH92XY1E7TJhwGWPEDxp8Qey8Aryb5Q==@lists.infradead.org X-Gm-Message-State: AOJu0Yy9LkFahK/fKjOWp7mfZl8WUbEsx671XiAmyD+YEs4wzuMl9LEk Pt6Q27AEcRRfU8AkJ1OYtwNJupr0IlRwgp9BMQO++CEDmm4bUf43avKLox1FSOA= 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241021_185922_810586_CC337D4D X-CRM114-Status: GOOD ( 10.01 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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: 13844985 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E12D1D17157 for ; Tue, 22 Oct 2024 02:06:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=3buJizr6zp+20ueZuq4KJMoa/fq9056gyhwQuKUI93Q=; b=1vSLN01j9oWblb ElZi1vfHWBp+Iagycmvxk9QMKhUHfuVUYHW5l/R9yaFf82XkZ5qQSs83Z5EpcNyB7h36ZYpssttfy V0qRGUVLfb7Ulw5Pg0figgAgO2U+GpScb4AnIA+FrZqKVaCCxby6SOINc/5X+QK6NcTI4N+3mQnKI KF/+PSx4Rlzc+Fxh9P7K6ngpTGemak9ohvwCLHcTPCfEXkorp2yzHsxqs4JEGcBjGOimVtJv3pmxM eQiOPygmaAeogCXui5FlO1fNKNfh3YXWNzRB+xZFQ3AcizRJn1e2v7sGb9Ns1yBQgRmNVpUVJAkuq XvTeQRCn0DPMlLLrNq9g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t34HO-00000009HRy-3k1n; Tue, 22 Oct 2024 02:05:50 +0000 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t34BA-00000009Fnw-151e for linux-riscv@lists.infradead.org; Tue, 22 Oct 2024 01:59:26 +0000 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-71e592d7f6eso3282621b3a.3 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=lists.infradead.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=MC0lpBtVc/H1rPTiBpCclnBzMwb69GoX0JsjIJjBqRm6djKgawCf2w21i7BBbMN6Rd jjdt97OPhVAPadhplc9SqQQu5kxTjqV2WQX66+KV4Niy8Y7RyLMl6p13uWVmpNNys/Jd +pBsQAv+W2VMPnOquk9ovVy1NIe6zfs6JRD9qDlJyWaf74eGIkRv9f8vIJNoaMcDtqKC txDhoKGQ1TvOJXjCwI81ZFl/Wo9va2BvHlfTxXy9eJ/CCAutWQyWm8jMaX5dfbOyCler JjPGbLAb654oEI1lHfDBh5t5j97nJ5XhMRqggeEoHUUdIR4E897LxZf28SKvkMbWEsup XJRw== 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=Id2NufWVFaKv0C3l8Bkdd34UMFYMJ3MqBuxjWiqnbAcWqPdIDSsyFTh/L/AeFLbi80 Xao4UAPUb3fm9lzRd4+4b91eAZkkkgCUmDShBGFDoy4ACCrH0l9s+q4gRKuE+a/PEoOW pZ9/S4G4uwJwA+ctd3uB+lxbfMoI3G3aBRRJreMPWCnG3z+APyDG7Y6jjwEBg1y4bCQ7 8HjepdUE0NvFT/N8EUTMEx0Zf8bNs4hvGTdGBT9Tqj5QUL6n6qIb4QEdBEVAP8hIzi4U eaAUFpQFdjzwnaKDwONP01zT2b4rNSZ6d6WPut80jLqPzhjO8Xd7g7ce1gzVbGscQym2 MLew== X-Forwarded-Encrypted: i=1; AJvYcCU1I1iTM5M1xGy4W8JsuZsnRhFCnTk2t01/XUK9Xj/SJXVua4PrgYgFTrzPyNzft0omRUM+jWFjEOI2xg==@lists.infradead.org X-Gm-Message-State: AOJu0YwdJtZS3Jizg2a/uEkypK6kUhenxdx6bS0kc/CoiAehsZ3V78zK Cues2s6FD9h4XSiTLmCbs2R7D8TdVk4rNR9bHBdq/BCTG0wxpzhiJJ6hQ2Q/5RU= 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241021_185924_379692_F210174F X-CRM114-Status: GOOD ( 11.11 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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: 13844986 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 62A7DD17159 for ; Tue, 22 Oct 2024 02:06:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=G9LawshEgGECAB7BBSg+rr5kZDt9hXm5iCKZOa6q5sk=; b=BJGBmE8LJTI7ai d/otIssufbA3GZ6HmgJZSen3igFJtonolGZx+VING6WY84EP2VkCg+xnMmBoHxzkh7xBwsN2xuulP uf7PZasK368AoovzjKkFkzrp7KjrUjGVnnbt8QRlExR/LFHp8tM5dJuHiW2iJOm8ktJF8IFTB5by6 WQ8XHIkRWiDkBY2KhzrvJlspYgyMqEjNNovH0tmEZol3NJrM1yJTu6UKUPzXLc9wnBuv2DYIrXqpV EtPI+nypZsZ1y+jIsZ8yRAqb+RpsFy3/tRM4iwjBU8lLx8ErEMmCJ8rY3X/Lk3R+ke9yzX8SaXCl9 iFzXyGMQj5lK3zKqLi7w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t34HY-00000009HTe-39xN; Tue, 22 Oct 2024 02:06:00 +0000 Received: from mail-oa1-x2c.google.com ([2001:4860:4864:20::2c]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t34BC-00000009Fpg-1xB3 for linux-riscv@lists.infradead.org; Tue, 22 Oct 2024 01:59:30 +0000 Received: by mail-oa1-x2c.google.com with SMTP id 586e51a60fabf-28854674160so2183061fac.2 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=lists.infradead.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=jCEEWbD+pBmj/uOGMfuv0a8K+j5ICzpIS/uqQ4INYdwT0THATDzH7LS5pxNOjUQ56N IQ6T5Bt+YHECRommxvBNb4LDuG0/MZf41725nhVzjLYYUQkTO/StNcRGeo7NkVhJpa0+ O7KWdhWuF6C+Ny9apF3XliwXeqgyE/alSPu/kAYPbv2B1YrJV+GOt26a/NLVy/xT9R1v 4DiylQKiKV7dR+HqwPfbfc74RvkztHQAXm6Cnfptmv10ywShAqtzxWAFcv6JypRuM3VK 4jaAw95CvOAMAHka9KqevNcu9mIpFIUHY+fEU0+Jk8jw4rUxLFIEXrkhsPD7mAQvK6xu LP6A== 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=iFK7t5UBPhv3nklopbLWWVcCfd3TQ4Oo6Tfkjy3q3U3WdQeBLkj1UGMnj40oeNOfrG gYAhDUBKpvYsGjm1kbaYjiA/dp4QRJzYsmSTb4xXJJfYQaNuHl5AxESCPvLzFYTTD9xa hcdeSN+7vU5/aaGsm7lpwjSxEAkQw9EOLKrWTdK9JPjErbIbn7DqehOpe8Rhk5wGAcX2 rr6rDNsZAn+wG+5z6YB8KJEz4ZcCDwkJQoKe57gemxmIZmy0M73G3cNDThdycoBHRhqr CREsZTAVMdJVLgplGGdopGebUn556Q/Lz2uJan05fw7CWHp4W4Z0ijdY6wSMLdGaMkmm n/5Q== X-Forwarded-Encrypted: i=1; AJvYcCUk1JJTde03ss0CTNx2Qq2Z0XdA+u+bwQJY7D0ThSYBY9Qo1aXgw7q8S7+TYJ0CuYVT+iBTMJur0TkN/Q==@lists.infradead.org X-Gm-Message-State: AOJu0YzdN9uTT6aaRa4SnCj10/y4GxP3/QXaQ9efZgSUTh8cTT09edfY LWaAJek5qHYEyRjc79BED7QOxwhCb5IGXVqs5DTmXHHUMMo+fq2egD1bTjko+So= 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241021_185926_721151_7EF59601 X-CRM114-Status: GOOD ( 11.73 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- 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: 13844987 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BAA50D17159 for ; Tue, 22 Oct 2024 02:06:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=KgRRVPaEwTbMYsRqJN7RUywLAbzv9QsnD6xeGuhf+1M=; b=UKoWN/Qo+vkpJl 32xMOkSkdCB8bHAhFDYCjHROysfokkWSnJbbYKESEGgiivJQmDbv8PP7MNQEzxBGqeaBn+Rh9rt/H r6jh1N+UFoaTJRde/aTi5NaPdqMORV/NV5ZnfhgWUxhdJYO1Koxs6h0OOdzvFXiEmiU6a4vJ8UjkS MVv5VKfq2LWEjqHnFnsQJpD8N4W8BtI2tScSk2aj8gSdffo+WcbIcj7rwQApW82INSyn3Hc3tTpcV BHpZqgmHGAvvsLPCkCgi0NYyriaiEdfBrP6i5ab5dOKN0/q0TPTok4EnaKLEjzT8wg4eUpMwGqd+m cCwPDxr6p2pQwSRVnBVg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t34Hj-00000009HXm-0BqR; Tue, 22 Oct 2024 02:06:11 +0000 Received: from mail-oo1-f47.google.com ([209.85.161.47]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t34BE-00000009FrF-2B9m for linux-riscv@lists.infradead.org; Tue, 22 Oct 2024 01:59:31 +0000 Received: by mail-oo1-f47.google.com with SMTP id 006d021491bc7-5ebc22e6362so832261eaf.2 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=lists.infradead.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=ekj/O/m+TegZFBB9wzrogI7SheMvGKivaDHcESFVBLmbPcbNJV91/6CQqYseETVWJq aU0PwRV8+ATkJSkhadXaY89uQEYqTYs+TT7sV8kDIF9wyu7w9hYOzTcMEeizbnGbuQh/ 5CGsMOcaAPo/NAC+AWKnyAwJQtmL9RFwpT+M05qd6ifVe8qcwURWCDoHyEVfKewBBSVo o5fF4wh7QnO2QQS7qKGdFawxR5W8xFUDp2HquE+sBUQ0k+gyzB5JhyM9Z4hwYs0fohAp RhpiHlZZ8T1B19X2Hd3Eo2Fp3h1xM5+V4/N4SlLNzAbpOhlI5iTn2eRwtPoZZoJeGBEE WA2A== 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=Gqmkp6g95pGfhMHudLQ2D9atDHD6caZmRTc6O6OI8JrgUJFb2NiDSWZUWqEVsPomHu YRtcWKrsMgjZvooexBg4FwGDEn8dFVly1gEfMh2yz1j+rDtO3OQyC459V0TR79rOISQw /WWFoiltybqMOP120BWkhP11yBdXWxBJAeoooggm9/cvYFPh92LU0wbGtc+SdeLg7bKZ RJLHKAYzFnOJtpgR83LvOEGt1IcUdmm6L1p8dn58+vOd6+3EkQxtdniANp3074cFFoTW MZiuuuJLLkYHLElFr7DL02CGT55bjus9xn/QiGynv9DhiP7XZkAxFSplnwt0ZujRf5lR hUQQ== X-Forwarded-Encrypted: i=1; AJvYcCXRa6yUjhhk7xXg+KdymnXkmS8dMW0fpb5A5iELuuuhYjQSU9+jVUsRXD6mEDL0jy1wJiJBOd/tvJE+bg==@lists.infradead.org X-Gm-Message-State: AOJu0YyA4umBa4oBZYUQRhtqSrI8Maym1lFpS+NGnUdI44Wi8RnOeR21 Ku7XQ9c1kUAIW119ub+n1fjT0oaBgo9S/22Coa+S6n2VffmBxxJGktm9zbRBjXs= 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241021_185928_683658_544BDD69 X-CRM114-Status: GOOD ( 16.58 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- (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: 13844995 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 83D1ED17159 for ; Tue, 22 Oct 2024 02:12:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=sbN/VwVtTsPVZ7BANxyVr6UM+zVuTmZtdYYVqPZugvo=; b=oGWDqJwgKUkZwc pRZ35i53YZ8VEamKTRTgI50JVQqjqPB8z+cmnJdJmH4ysQ7hP1FMp6rDLYsDhvC9yjhbUatvR1LeD f+OHO7QDD9LhY+FSXGDt3lTi8RHqYwH3+GB/+a+OlGEtP+6GSukt4Q5MDfQGxknznbrRcJL7SzNDi aqfyVDN+pdXWMixXpro7pr8TM1asBsHOerIHU13lgC1/tx9mHZi/w0LO3gFyrX2WgO7FmJ0YD3TMt N+vG4Nm7Nmh5AdK1AbF9NE2NCCatfg6jcwH7mtU8oJH1ZkCeGGWBVll7OqwX3TCuNenKXV/UIo3nF +2Ts5Gkmlh2XAWIoeAug==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t34No-00000009IUH-2Am2; Tue, 22 Oct 2024 02:12:28 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t34BR-00000009G02-1fPi for linux-riscv@bombadil.infradead.org; Tue, 22 Oct 2024 01:59:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=559wueV0PG6xFm1m5EmJll56/fKSQJhn2CptHQFJIX4=; b=o/lTLAq8qYOjGTMS6mBUbtTDLX CryYKwcsw/KUYIr+XchkAKxeVhnWVEJr2aKbjFaUiFNoQl3dwLPv3B4so9r+fkQkWGGYjvvKqIpHo G5scXxN6mEcCNyydhcTy8KoCYQIwjUYPAO/gCoiYj7YsNCRa8DKwqvYvRMuNyyslOXh16pWIsXkSu rlWNNDa/LK/QQrEb+L04+39ip9d60zXR8tyDOlipzzDWpsBG4htSv7yLbOCr+F2WhUi7E+gLK6XMy YMfPhAowQ66ZlHTBWtbAEQEvaJH/ZAIHPz2QfZSkPgp/gvvVjbTRfE0AWhYCKcLvdalIAhK+VODsC tdrWOBgw==; Received: from mail-oa1-x2b.google.com ([2001:4860:4864:20::2b]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t34BL-000000085ON-1oU1 for linux-riscv@lists.infradead.org; Tue, 22 Oct 2024 01:59:39 +0000 Received: by mail-oa1-x2b.google.com with SMTP id 586e51a60fabf-288b392b8daso1816894fac.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=1729562368; x=1730167168; darn=lists.infradead.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=fOkES0EpzB6Ws7ZmIHr75OtpsKOOHrDJvKY/ye1Y7eqolDMW8RBZgPLVivEjCG8H+l LcgNS8zMYji24Zn3qejheS/O8+AEV/CsKMuYFTxysh0gVSrJ7ytt1kslSgRldluzMnhF lsnYUM9MtRTEakKlIPhEeWa+gvdMKr426Q+I3j+cOXol+tgd1jiUZgNru6QgxtgrePDI pW+tSIcaambaGfGC+yupiFsqPO7A6KvAaZ+3vRpFlSKoo19LD6sYlOdIkJkaybl69KVq G/4U20WrotkBdQqJlbYuAyxJhsmUjFvSYR1vahnSUbOU2FhljwPpxC7sOtkrE/5wk57L +wyg== 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=HKyTVoeZn6ueyzdYvoXsOpJwYcl6N3dQyBOd9jDsd/YafDhUxgGvP+knsw+Lmn8Guc t6ElWJDek0wlUZmia/uE3dPFeM5wKNOANM/eGc5kZydQ5EhlCyxSOw8xy3YC3bSeCLf/ 6HtdJFElSiuQ3A15/oZGoonaqzXgAv2Du/bLSOtYF8ly8eJpCt5E6Bq4KlVwt9Dweyvl NTpJyIR44gpWhsdwafdxoFKThWvJem+Cxvswv9Ocn6aUIeNROR+dpQYZpLkPIw+t/rtX 3IJtApfwDYvFpmjJCB61QlO3vgIa8lDYJpbkQV3aLZIzmlMGi74QefrDSUAKLSb4064t ZPuQ== X-Forwarded-Encrypted: i=1; AJvYcCWsWBS49NGoKEpDWTrNiltt46MirNXHmW8Yf6tYRPJ981RBz0NIU7vZsC6r1br0KbqAHBqLqRSV1KsVVw==@lists.infradead.org X-Gm-Message-State: AOJu0YzdsUZupENEu5aaXlpCkEzOYxLbBGK9eOoa29YWrDq53e02GXGs N5qcI69SZJjZ5rEQY2k8ta2sPUYYkoUVbZq4Y9zubMal8CFUI8mk/Lxc/OOCsUw= 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241022_025935_828883_5647049C X-CRM114-Status: GOOD ( 12.31 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- (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: 13844994 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 661F1D17159 for ; Tue, 22 Oct 2024 02:08:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=GAziCDfKeydxDQEp89RdD6Oy6g5NFDSPX6GGYp/2w3k=; b=JaQ0m6aJ+rY3/R OK9JOLnLWK0y9WNEmw1VBDOIUXNfsVx2ckaQ3dO5rXlz2l9S8Y63isfP9cCE82m99EETshFtoiog2 UFVxr0zxLSJbeCk6CPzqbOXx+1g7xX+9YE0AwphdHValq7CUrQILD0Q3CX9Heceek/TesGDVp7mAw RwW7mqwn+e/Dk91R6ycUlk2z40sFHoezwdAbN29HNxr8LB85rkB3TgAks2BE6Z6uyCwVoSDm1Qesg +QMl2iPU61/j1Iz/IC37uO7nudQZTWtHxllH75In3lmTvD/g2NIwz1bZQHQLmzeJzgSSvtpe+TGMS JMegs8NiUwiQKpCCUZ7A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t34JR-00000009Hzp-1xmQ; Tue, 22 Oct 2024 02:07:57 +0000 Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t34BH-00000009Ftl-19dg for linux-riscv@lists.infradead.org; Tue, 22 Oct 2024 01:59:33 +0000 Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-7ea79711fd4so3504809a12.0 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=lists.infradead.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=b8lCl3PaGZ7t44U8j81jNmUsLQtPG0XmyVyKCrZrNezBcNyTFayM83Mvz3paw45XVo 9ctQQxryoI43Vn9jv+w3CdCfKeNt/FcV+KHqWAKofKRjnCrvHGT9RhY/d/CSFYzDXIjV PNesvVUDEcWQn23URGWue6cARWtMNFZ+J6S8kC+SrRtMwzWyhEVQQw/xEFIUoDACsRji XRwLVVEjdboCPyckWQTdOa/WmNfysl2gyLPkN3b6U2ePIWutDVqRcII1HaUhwvCK+faF pbhHHUVzC8aCnghIwIiN/IwSRmUhRpOdfSCTkh//ESFtGaDLViq9yWTtocWDDBtwde06 /wNA== 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=GEpsGgYBtSQ7Xmdmfw9xaZt8p8DceBvRLGlBwnrUl1n56j5X1Lzij76NIGFJ75z6J3 2Uy9E8BZ53V1ODk7NMxi+V+10aq3EZpqlWBTp+g/nasjmClBMIscheGaI9RErB4OT6oT LqzDH+1pL5596T+rDwYWIaEp7tfEC7tJkkO9aMRTGoGFcPgB18qv8EFm5hqUkvbisaYO S2fHJqqRkgnFKaUyGcGtEN5vPprOWk7QVtqgJyhg/HTb/HHAC04rG4yg6HpVK1oelR5x Gq6CV1Dt1jkUG1VjefVPMdEVOtOEy2M3YA2DSqvlzSPAjyB84EJRxlIt9z5olROcj2j4 NyOA== X-Forwarded-Encrypted: i=1; AJvYcCUpKfPHOXaq4hlHZO4DqMZu1Qx8YPcOoss8TxzzYr2Gygt3b1EeWlbQA8QhWO8JxCI90UhtfaoVqQae+Q==@lists.infradead.org X-Gm-Message-State: AOJu0YxvI8/L2kc9/7AHwe4NlCK+1TS8OIkAdPlANiLUIcxVAW2DQjTN 0JxNfrsjTcxZ6125up/O4eakmbQ/ga7dbpZyDlHywFfEcXsgR7dRkDmw0AZtqJ0= 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241021_185931_543126_B45FA779 X-CRM114-Status: GOOD ( 10.20 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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: 13844996 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5379DD17159 for ; Tue, 22 Oct 2024 02:12:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=8/QbyCqBRX9GdHKWJZOLfsG53SjeQAGpo7dCmUKobZE=; b=FrOje/qnFL4pCo i64WB5TtJ6MnbeIsmd+YIxPtyqWl7yPobG/DV8fUhRJdbo/RDgRDa8o+knB2B2q5Yin4Wi0pRxruY o326c/8oaH9Ls3TeWSDEucYphwjbE3CpU3S12TvVmVC+E54iPxoPH59veHmZcOouaVJgkwKjnoMVZ 8mdAl26MzPHMloPQb5OPJGtjg9Y2YwGSQczRypalb064v8+Q4Qt3omozH9MUKQrdkwJnB4CFXAANq 1RdNXVlIhvp82Xop7sWegXYzR6EtCnyJ6SpZiD951O9glIfy1WLWLKtEsqAQGcYBa6b59Np+Z3Dbc repyjCmDjf0i6TEsJnwQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t34Nn-00000009IUB-3jti; Tue, 22 Oct 2024 02:12:27 +0000 Received: from mail-pg1-x530.google.com ([2607:f8b0:4864:20::530]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t34BJ-00000009Fvg-3e3A for linux-riscv@lists.infradead.org; Tue, 22 Oct 2024 01:59:40 +0000 Received: by mail-pg1-x530.google.com with SMTP id 41be03b00d2f7-7ea8ecacf16so3480523a12.1 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=lists.infradead.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=V++WUq8zJRddTI1tTy4vRdjuzsBLw2n3tIRGdCjqqTXciJDGlOxepi8XS9bt0gNsD9 kmQmCfVQaD8/8/QPQVdrQH0kndpqrQ1uN9rBwog0zBNyQanBcB5LTcyVILljkom25bvV c/X2Ai6RucabkZ4nmqasdPo6YRZgk8qBhyTCl4RQSibAdwIXAdhEM0eEYwhIOdtKos6r y+Ul/q2QGk7LxVB7Rv0vfFPZ6kbDbhgEWW+SGvCFmiMrzf53ylXkzeU5egSO1/HJ5sns UbfPmhIg4cT1Wqb96uoxol+y/X2gwK7hQBNpo8jByWYK+Y0YD5s1Y0CrehTv/LVNhc+a dYng== 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=dxGUfaxP0LwmNDlxJTFHNL9YoooKTTh3gYBiH89JHYW1Mi4NRTqfEocp8z5kOTlUIn eGf29zM9hAoLUpdbyPNXhsyYtA49s2Jc1pi9KU3DNLq8W6KZ5jS5TTmZcCvt3URYmS7v wY+JwT5Wzpl1fqYmnM3k/YhAK9sM51r6f8JuxYJ5ifkge/04lz1Jg77dqWyBQvxpGZQG j8n1efZikv2WJqtmp8IBg27YtaGk9LRB4v6vBPVJGjNjFBZhpL8m0grS+UeZ+WSmU1tT kNd0yU9kXocw8fBBVW8W4f40noN1hZdS5hP4vxglX7vRhhlEtaY9xcJwA+q7jp3ki8Up UhCg== X-Forwarded-Encrypted: i=1; AJvYcCWVPfaEMISSpY+neZF/GDcWq2hWQwaGtv2L8cOudmjJ5jRJgKzQ+NmoBkfeAswT21DlXNc//qVpb9qWtQ==@lists.infradead.org X-Gm-Message-State: AOJu0YzTUpiNBVNt6Dx9aigVFghWOyZg7Sklz7v4dukHI2Lq0fR4+Wxs eAkidgrrLFpj+tky/f2Fc06/1+quUe96PlQnDIpw0sagr1TGFJv1b0XkI9Liu6cr7BNcxqjf/mT b 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241021_185934_184522_6171A3FC X-CRM114-Status: GOOD ( 31.65 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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.