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: 13844981 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 AC71BD17159 for ; Tue, 22 Oct 2024 02:04:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Af9tK0JLBBTeDXRGuA1c/z94Xn/abI+OOOquUfcf3I8=; b=0DhjqlegPjLU7IUsaCx1/BvPbJ 5zx28OFr4SG3ZamrBgKkCbu6rDRVfMYeGOuZ0E4p9Wc9h0z+IRt/iEXmyMuWF8GPH/5A9YQYSJt83 WILxIz+jJTA6A/pYvI+OKCp2rZ6wkBz9XnVTphBO1MVFRYhHC0M8QU8N1uDArms4hh11f3e5eLYoy yYb9pQaS2wCf1B6dgRI6qx2eTiGr4pBKXRKjWJFIhZr7M9ERyv65JkNAn5pRwW/IHQ2tACk5KtWXP LxeqqKmksROhxXbuQ6+sqLgg9DaLYKTY/Tw6uakVtEVnNEOW70T0Qd+vldMHf1tZ3UhNbE0JfvoJZ /O8i4hZA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t34Fv-00000009HH8-0LOi; Tue, 22 Oct 2024 02:04:19 +0000 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t34B7-00000009Flk-05Qt for linux-arm-kernel@lists.infradead.org; Tue, 22 Oct 2024 01:59:23 +0000 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-71ec997ad06so1150475b3a.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=QcoHA6SmDGnmR0j4yzSuCC4JT3TnWAw/Aq2e0qtb9qcpOqndEgj8+a8YMY0NzWRRgF 7Kfk9nboysXnOPP6uMHBmyIu6SyeIkbieUZQ14D1JLW3VtgA6dYew8OOCunzIrfYXmbH uUy83D0H7SPwBe9g0b0/cSlpLRKWbVs3j/ciygDJIVbdhkiSWax3iqjt1EYPGEslQmuR 3iCABHsGhVlSkjYjd5AbyDLDiEYMTbpjrW6pMSQ/l/9FJ5mev9nj/Rj0nXo06tatLw1g ki4ztL/DaMvzQz2yNhv6tLqMdM+DrrYpGaqNueSMwxqdZTgrp1EIO+OhJHSodYTl097c O3XA== X-Forwarded-Encrypted: i=1; AJvYcCWDXRkaVrR1t9ZDV1kjgKHNbq2OQ/UUkIhgAsrDMNlOEQPfwU0FqimHckpcl+bwrrogM9eN68xdwImLht3CsAsP@lists.infradead.org X-Gm-Message-State: AOJu0YyXeTGwStJyUxvJFXIMTbd4hFELFiZBqjyley4KNzMjL1PDY9aE w4qLj0GJfIPXKbRFXh4Jt6y07k16h+uLoCkuPcmyXmK3+WADrr9zNF1S6GV9QSE= 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_329798_A5D61E98 X-CRM114-Status: GOOD ( 16.13 ) X-BeenThere: linux-arm-kernel@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-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=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,