From patchwork Tue Oct 22 01:57:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13844964 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C266ED1714E for ; Tue, 22 Oct 2024 01:59:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3636A6B009A; Mon, 21 Oct 2024 21:59:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C0956B009B; Mon, 21 Oct 2024 21:59:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 139506B009C; Mon, 21 Oct 2024 21:59:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id A389D6B009A for ; Mon, 21 Oct 2024 21:59:23 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0D92AC1C59 for ; Tue, 22 Oct 2024 01:59:06 +0000 (UTC) X-FDA: 82699580292.08.48FF054 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by imf22.hostedemail.com (Postfix) with ESMTP id 5B6E7C001B for ; Tue, 22 Oct 2024 01:59:02 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=Wu+tZ3wA; dmarc=pass (policy=reject) header.from=sifive.com; spf=pass (imf22.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729562238; a=rsa-sha256; cv=none; b=QqPIJrbZLPi+QEvE00NficUz+n7L47+iPSC2SXur+p92ra+qGr3tGB8Yd1tGY7SJ49Q42I YCuIeo8AFENubY5XMoIWYEgXWKti6vzoQiU/UZyy12RojBsWPmw/DauZabxHzKf5oJsfG8 ODGTYBCnJ2IM/dsuWCFBM12CesG608k= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=Wu+tZ3wA; dmarc=pass (policy=reject) header.from=sifive.com; spf=pass (imf22.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729562238; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Af9tK0JLBBTeDXRGuA1c/z94Xn/abI+OOOquUfcf3I8=; b=nAIW+ltL+IRlvKkWZ3ZMrhfHk3YbazqWXOdIPtYrfBu9cKsQGjxAaM5UwP2WblnxpijaIx aehYEYTcM/uen58b5vpg4ejRaRgF2bEbsSkXxoBuNHqxFUeRjHOJIXNNsCa6dE7RKcyYVZ jG/dq4l++auptt1T8G4nLWEwimTzSis= Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-71e49ef3b2bso3629396b3a.2 for ; Mon, 21 Oct 2024 18:59:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1729562360; x=1730167160; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Af9tK0JLBBTeDXRGuA1c/z94Xn/abI+OOOquUfcf3I8=; b=Wu+tZ3wAU1QxzHMzvHPQ4QKKjoJ7gKza8WkCgeNkYst4mDFgjtzFVBdd1FIVio6nWD uUCUDm2LnYGpkRVii7YPT1CWKRIAQJ9T5hQRR2+9VxyBzGB1cIO3Mqt9Y045RKpryqrH DM8rbH8M+12Q/aOck9YrKhBgXxfz/dXzGdZbTdsTYgIm4GM0Q9v3tt+WrfUBZh3LC1IO ehrqVDzmq47kLIo2NklQXBSyVn7Rv7CO4FbFQROQ9MEH3gSpl2uYP8mTTXOTvVvUAl36 F5CPj+JRDx4vneXA2JGNKsjYr4o8RNiYuLWVNDFP/AmAsSVFir3aSydQNyKv9OkBICeU AF9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729562360; x=1730167160; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Af9tK0JLBBTeDXRGuA1c/z94Xn/abI+OOOquUfcf3I8=; b=EfOhnNCEqeHotchThPpNjXXTy91cFiZG1cy577B4K+kQd4kCTGvSnL1La0TFoUg42z 6pwjyYfZ6YTk2OYcUXOMqHUBl+1NClGrajGfiTa7T/o3PKMiAHPWOXt69X68s02dkJlp joRrNT71VUQ35ATCPCDT//95fZ8WpFPcM9c1icSADBHgkakGGWprBEaxr3fu43YaguEG Bdpt8ryuuZaJE0a5mX9sRfCQ4MftyHUVbMYTw8XoMIETPNg5rAeZkAzUdK247Vd+r55c fnGg9X0+2AjYl2gVw+2XwKhcDiTKweo6YmLQM53A6QSiIheV52j6OokP7siVR1XFWVMF fasg== X-Forwarded-Encrypted: i=1; AJvYcCVJhat4Gxo1PGckIZv/Th8AHk6HtIDJf4rVqlhtuy9LIK4Hrqm6k/6rmnuBugwe5kjjXun5Ld/IdA==@kvack.org X-Gm-Message-State: AOJu0YwNZYb1AzTkNi3bev2OSEZ7Q+mlTTnWtNM5tx2smruWgdPkpL/X cUTxR3Kf/oDyVAakO0vUTU0MXwuLzXJAvKX8AxusFdXErNbiJkuIx/yER6Smq9o= X-Google-Smtp-Source: AGHT+IH5wPAzHdVfv0MMInx9ySs4Jc9bkPSFs6F+LS8CNw/7mEjClw2+t/2vEVp2yPC3jOJm28L3Qw== X-Received: by 2002:a05:6a00:4610:b0:71e:6489:d06 with SMTP id d2e1a72fcca58-71edb972451mr3011651b3a.0.1729562359943; Mon, 21 Oct 2024 18:59:19 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71ec132ffdcsm3600710b3a.46.2024.10.21.18.59.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 18:59:19 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , kasan-dev@googlegroups.com Cc: llvm@lists.linux.dev, Catalin Marinas , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti , Will Deacon , Evgenii Stepanov , Andrew Morton , linux-arm-kernel@lists.infradead.org, Samuel Holland Subject: [PATCH v2 2/9] kasan: sw_tags: Check kasan_flag_enabled at runtime Date: Mon, 21 Oct 2024 18:57:10 -0700 Message-ID: <20241022015913.3524425-3-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241022015913.3524425-1-samuel.holland@sifive.com> References: <20241022015913.3524425-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 5B6E7C001B X-Stat-Signature: wnwq6rkspa9rdjgmpmqqrmhntzyqsamx X-Rspam-User: X-HE-Tag: 1729562342-202562 X-HE-Meta: U2FsdGVkX19M5Tzo3StSCXSIQmc6MO6GrCY0elvzod9c8KcKKabw0LiMYlpAA/NSj9xXJT7fozZzfuUkzBcxHU8Ya1ZYoZVRJQrRkhAwW7pPv7cF5/rYmiAPozjKhLTZMjN0Q5LZnBHMK19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ZLWfMakQAwt2CqTHe2gt6sw2ZCTbBm6FoiMz0eCHXgPTUnuSI8ypJr6ho3SGATjQzyxXe7mpH5SLxyIn8XlSIWWlk31AsAbzWhlPpZCPrdgmh+mBJGLTqTksIbggBzWC8rb0zPmrE7QRCrTubfJzBj3NEwMUHIffEwxrz+sTsIc4DNcc/XVLBpLSBX3CeScVK0CAUkfhJQpLKQPGw+nEP9HRrD5GsiJuPKQzy+6bX7TUC9lfw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On RISC-V, the ISA extension required to dereference tagged pointers is optional, and the interface to enable pointer masking requires firmware support. Therefore, we must detect at runtime if sw_tags is usable on a given machine. Reuse the logic from hw_tags to dynamically enable KASAN. This commit makes no functional change to the KASAN_HW_TAGS code path. Reviewed-by: Andrey Konovalov Signed-off-by: Samuel Holland --- (no changes since v1) include/linux/kasan-enabled.h | 15 +++++---------- mm/kasan/hw_tags.c | 10 ---------- mm/kasan/tags.c | 10 ++++++++++ 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/include/linux/kasan-enabled.h b/include/linux/kasan-enabled.h index 6f612d69ea0c..648bda9495b7 100644 --- a/include/linux/kasan-enabled.h +++ b/include/linux/kasan-enabled.h @@ -4,7 +4,7 @@ #include -#ifdef CONFIG_KASAN_HW_TAGS +#if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS) DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled); @@ -13,23 +13,18 @@ static __always_inline bool kasan_enabled(void) return static_branch_likely(&kasan_flag_enabled); } -static inline bool kasan_hw_tags_enabled(void) -{ - return kasan_enabled(); -} - -#else /* CONFIG_KASAN_HW_TAGS */ +#else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */ static inline bool kasan_enabled(void) { return IS_ENABLED(CONFIG_KASAN); } +#endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */ + static inline bool kasan_hw_tags_enabled(void) { - return false; + return IS_ENABLED(CONFIG_KASAN_HW_TAGS) && kasan_enabled(); } -#endif /* CONFIG_KASAN_HW_TAGS */ - #endif /* LINUX_KASAN_ENABLED_H */ diff --git a/mm/kasan/hw_tags.c b/mm/kasan/hw_tags.c index 9958ebc15d38..c3beeb94efa5 100644 --- a/mm/kasan/hw_tags.c +++ b/mm/kasan/hw_tags.c @@ -43,13 +43,6 @@ static enum kasan_arg kasan_arg __ro_after_init; static enum kasan_arg_mode kasan_arg_mode __ro_after_init; static enum kasan_arg_vmalloc kasan_arg_vmalloc __initdata; -/* - * Whether KASAN is enabled at all. - * The value remains false until KASAN is initialized by kasan_init_hw_tags(). - */ -DEFINE_STATIC_KEY_FALSE(kasan_flag_enabled); -EXPORT_SYMBOL(kasan_flag_enabled); - /* * Whether the selected mode is synchronous, asynchronous, or asymmetric. * Defaults to KASAN_MODE_SYNC. @@ -257,9 +250,6 @@ void __init kasan_init_hw_tags(void) kasan_init_tags(); - /* KASAN is now initialized, enable it. */ - static_branch_enable(&kasan_flag_enabled); - pr_info("KernelAddressSanitizer initialized (hw-tags, mode=%s, vmalloc=%s, stacktrace=%s)\n", kasan_mode_info(), kasan_vmalloc_enabled() ? "on" : "off", diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index d65d48b85f90..c111d98961ed 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -32,6 +32,13 @@ enum kasan_arg_stacktrace { static enum kasan_arg_stacktrace kasan_arg_stacktrace __initdata; +/* + * Whether KASAN is enabled at all. + * The value remains false until KASAN is initialized by kasan_init_tags(). + */ +DEFINE_STATIC_KEY_FALSE(kasan_flag_enabled); +EXPORT_SYMBOL(kasan_flag_enabled); + /* Whether to collect alloc/free stack traces. */ DEFINE_STATIC_KEY_TRUE(kasan_flag_stacktrace); @@ -92,6 +99,9 @@ void __init kasan_init_tags(void) if (WARN_ON(!stack_ring.entries)) static_branch_disable(&kasan_flag_stacktrace); } + + /* KASAN is now initialized, enable it. */ + static_branch_enable(&kasan_flag_enabled); } static void save_stack_info(struct kmem_cache *cache, void *object,