From patchwork Thu Jun 24 03:40:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 12340749 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1820CC48BC2 for ; Thu, 24 Jun 2021 03:41:03 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B3E80613C5 for ; Thu, 24 Jun 2021 03:41:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B3E80613C5 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=axtens.net Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B95D26B0087; Wed, 23 Jun 2021 23:41:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B6C176B0088; Wed, 23 Jun 2021 23:41:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A348F8D0007; Wed, 23 Jun 2021 23:41:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0194.hostedemail.com [216.40.44.194]) by kanga.kvack.org (Postfix) with ESMTP id 6C8416B0087 for ; Wed, 23 Jun 2021 23:41:01 -0400 (EDT) Received: from smtpin39.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 9F132181AEF31 for ; Thu, 24 Jun 2021 03:41:01 +0000 (UTC) X-FDA: 78287216322.39.0EC5F80 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf03.hostedemail.com (Postfix) with ESMTP id 3F58AC00CBE2 for ; Thu, 24 Jun 2021 03:41:01 +0000 (UTC) Received: by mail-pf1-f172.google.com with SMTP id t32so4029293pfg.2 for ; Wed, 23 Jun 2021 20:41:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sf2vC4DdejXn0TlyY78AewZhU0SgkUPuFBMazAbHZK4=; b=D8DjGwJUlXIO0TVrSG/ZeVvUPW8wAHpUnKMSvZFVHtop5C+89pYkyv+sH9hOpbRZu/ WeLrxWQk1TD4sCKNOKHTSyfqskGq5aaadsGnRaaDJz85jHEwhsY8ctxHqy/R1FXCJjVi YVLEC05DAfejjr8edszhWUuY6dHt2hgNoOS5c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sf2vC4DdejXn0TlyY78AewZhU0SgkUPuFBMazAbHZK4=; b=su8KHkBOBK3Q78iajlkPJ6NrAmm8zuS9ZDg+b7hjTfflTonVQsm9cdaw5AjONcJUA1 e7Dp1P6ABevCz2CM3P5YdUE0IApndghggrnpm9lA8ZgFiTuzySj09lVkBRcmsS/czgIG yfoEUsa7fWIt4J/bQ8lkKn+YPN5xuk1HtlYbMRjk/TGwFnxiUwWREzaryPf+3YxQnc2c MNFZElGZdFgQMKa7T8s8srPvHqo7JqYMd0rTq+0r/9liOdn8vk++TgQDuRnS7Nv2dgPR ArYr/tZZuMBCkDl8xt77o727l17Lpx91mNkt099H7KgaFJjzgp9LdY/Bs6KmQfiNv7Ad IsRg== X-Gm-Message-State: AOAM532IT6gIQ1NzFAjmPuC/CoajW1kn6GuvMWkreXxeXoGgmlWUnBeZ 6H7rbplOXfoQbBviN8PXTPNv3w== X-Google-Smtp-Source: ABdhPJxwUXidQSW66Cd7hfNHu9m1OdS3iDnW9VrYoHaD9hKo1tsw6JFDwPcDmznjLKOpOobVgqPCDg== X-Received: by 2002:a05:6a00:138f:b029:304:2af5:1e12 with SMTP id t15-20020a056a00138fb02903042af51e12mr3014900pfg.5.1624506060335; Wed, 23 Jun 2021 20:41:00 -0700 (PDT) Received: from localhost ([203.206.29.204]) by smtp.gmail.com with ESMTPSA id p6sm6789262pjh.24.2021.06.23.20.40.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Jun 2021 20:41:00 -0700 (PDT) From: Daniel Axtens To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com, elver@google.com, akpm@linux-foundation.org, andreyknvl@gmail.com Cc: linuxppc-dev@lists.ozlabs.org, christophe.leroy@csgroup.eu, aneesh.kumar@linux.ibm.com, bsingharora@gmail.com, Daniel Axtens Subject: [PATCH v16 1/4] kasan: allow an architecture to disable inline instrumentation Date: Thu, 24 Jun 2021 13:40:47 +1000 Message-Id: <20210624034050.511391-2-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210624034050.511391-1-dja@axtens.net> References: <20210624034050.511391-1-dja@axtens.net> MIME-Version: 1.0 Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=axtens.net header.s=google header.b=D8DjGwJU; spf=pass (imf03.hostedemail.com: domain of dja@axtens.net designates 209.85.210.172 as permitted sender) smtp.mailfrom=dja@axtens.net; dmarc=none X-Stat-Signature: ma19jwt85f8kkw979ftqm4xp3df6csmm X-Rspamd-Queue-Id: 3F58AC00CBE2 X-Rspamd-Server: rspam06 X-HE-Tag: 1624506061-685715 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: For annoying architectural reasons, it's very difficult to support inline instrumentation on powerpc64.* Add a Kconfig flag to allow an arch to disable inline. (It's a bit annoying to be 'backwards', but I'm not aware of any way to have an arch force a symbol to be 'n', rather than 'y'.) We also disable stack instrumentation in this case as it does things that are functionally equivalent to inline instrumentation, namely adding code that touches the shadow directly without going through a C helper. * on ppc64 atm, the shadow lives in virtual memory and isn't accessible in real mode. However, before we turn on virtual memory, we parse the device tree to determine which platform and MMU we're running under. That calls generic DT code, which is instrumented. Inline instrumentation in DT would unconditionally attempt to touch the shadow region, which we won't have set up yet, and would crash. We can make outline mode wait for the arch to be ready, but we can't change what the compiler inserts for inline mode. Reviewed-by: Marco Elver Signed-off-by: Daniel Axtens Reviewed-by: Andrey Konovalov --- lib/Kconfig.kasan | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan index cffc2ebbf185..c3b228828a80 100644 --- a/lib/Kconfig.kasan +++ b/lib/Kconfig.kasan @@ -12,6 +12,13 @@ config HAVE_ARCH_KASAN_HW_TAGS config HAVE_ARCH_KASAN_VMALLOC bool +config ARCH_DISABLE_KASAN_INLINE + bool + help + An architecture might not support inline instrumentation. + When this option is selected, inline and stack instrumentation are + disabled. + config CC_HAS_KASAN_GENERIC def_bool $(cc-option, -fsanitize=kernel-address) @@ -130,6 +137,7 @@ config KASAN_OUTLINE config KASAN_INLINE bool "Inline instrumentation" + depends on !ARCH_DISABLE_KASAN_INLINE help Compiler directly inserts code checking shadow memory before memory accesses. This is faster than outline (in some workloads @@ -141,6 +149,7 @@ endchoice config KASAN_STACK bool "Enable stack instrumentation (unsafe)" if CC_IS_CLANG && !COMPILE_TEST depends on KASAN_GENERIC || KASAN_SW_TAGS + depends on !ARCH_DISABLE_KASAN_INLINE default y if CC_IS_GCC help The LLVM stack address sanitizer has a know problem that @@ -154,6 +163,9 @@ config KASAN_STACK but clang users can still enable it for builds without CONFIG_COMPILE_TEST. On gcc it is assumed to always be safe to use and enabled by default. + If the architecture disables inline instrumentation, stack + instrumentation is also disabled as it adds inline-style + instrumentation that is run unconditionally. config KASAN_SW_TAGS_IDENTIFY bool "Enable memory corruption identification" From patchwork Thu Jun 24 03:40:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 12340751 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58A2CC48BC2 for ; Thu, 24 Jun 2021 03:41:07 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0A222613C0 for ; Thu, 24 Jun 2021 03:41:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0A222613C0 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=axtens.net Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 10E056B0089; Wed, 23 Jun 2021 23:41:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E5CC8D0007; Wed, 23 Jun 2021 23:41:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ECAC26B008C; Wed, 23 Jun 2021 23:41:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0065.hostedemail.com [216.40.44.65]) by kanga.kvack.org (Postfix) with ESMTP id BD4226B0089 for ; Wed, 23 Jun 2021 23:41:05 -0400 (EDT) Received: from smtpin34.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 01D0A1B657 for ; Thu, 24 Jun 2021 03:41:06 +0000 (UTC) X-FDA: 78287216532.34.3731838 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by imf18.hostedemail.com (Postfix) with ESMTP id A92AB2001060 for ; Thu, 24 Jun 2021 03:41:05 +0000 (UTC) Received: by mail-pg1-f181.google.com with SMTP id e20so3615906pgg.0 for ; Wed, 23 Jun 2021 20:41:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=EvVNbLnwqJAIYi/oiSLioHcxV6FtIVBfZ8nguMWCTFE=; b=gUFBIaMMZ1d3pt0GkDSe2NiIRaSGldh05CajT4om97f0wQSzL0x8j2RMIxpLd2Us0v IURILWgkklaZE0j6t8K/lCQ42QWj0/9j8vXatmOsNbzRQH3UCZFs5mfL67RPu1w3gHWK FKQl5/txcTBPVVBgLDeooy7VhwpG+jnMWjoL8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EvVNbLnwqJAIYi/oiSLioHcxV6FtIVBfZ8nguMWCTFE=; b=OqmBcLIzfNoxnqrYQY2ZVcIfq+0Qqy7CFddqbO9yNDwVe+fFAbcdj0LIEDssH6lrCW XTAzsf8Wq3qCiGLBMu4VNh9B0+eVuZk/MshUSJB/CJPcfbrgl18qi3R3c7jz4twEv7Aq 3HS/nSLQ+IcGZZXM8OadBMoyXtzCGyiJ4e2vfTxvbyxbIbRc8hu4FVcz888qQnpwsJ/r uXSuatcrkWn4UGvPwdY1XHKiYcFxnMEJcYtslmHDhr8WIV9rV7EebZpR4V20yx0NB3hf gOlIWbxvNDgRm4WhRZPg5yZMYpTSNZoWJHQ54wE0PxmsxdfsyEQsJtiVrhU3pHab6qsX IfeA== X-Gm-Message-State: AOAM532mOypGTCCYf22/AveKiUFoPnGId/sQwIjND6698oXbEW49FEXU Iwnspp02iGCeg8BQ69QtRErBvA== X-Google-Smtp-Source: ABdhPJzmWte/VMqL+/sIhBU5wbozLQoCL7Iah3oMqjhyr7uichTCCCil0qJE2gDAROVokeV7Jiagow== X-Received: by 2002:a63:501f:: with SMTP id e31mr2748949pgb.231.1624506064897; Wed, 23 Jun 2021 20:41:04 -0700 (PDT) Received: from localhost ([203.206.29.204]) by smtp.gmail.com with ESMTPSA id y7sm1137228pfy.153.2021.06.23.20.41.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Jun 2021 20:41:04 -0700 (PDT) From: Daniel Axtens To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com, elver@google.com, akpm@linux-foundation.org, andreyknvl@gmail.com Cc: linuxppc-dev@lists.ozlabs.org, christophe.leroy@csgroup.eu, aneesh.kumar@linux.ibm.com, bsingharora@gmail.com, Daniel Axtens Subject: [PATCH v16 2/4] kasan: allow architectures to provide an outline readiness check Date: Thu, 24 Jun 2021 13:40:48 +1000 Message-Id: <20210624034050.511391-3-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210624034050.511391-1-dja@axtens.net> References: <20210624034050.511391-1-dja@axtens.net> MIME-Version: 1.0 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=axtens.net header.s=google header.b=gUFBIaMM; spf=pass (imf18.hostedemail.com: domain of dja@axtens.net designates 209.85.215.181 as permitted sender) smtp.mailfrom=dja@axtens.net; dmarc=none X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: A92AB2001060 X-Stat-Signature: eq7ifisndmg9jkq4qdkkmjpgez7tnt8f X-HE-Tag: 1624506065-369093 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: Allow architectures to define a kasan_arch_is_ready() hook that bails out of any function that's about to touch the shadow unless the arch says that it is ready for the memory to be accessed. This is fairly uninvasive and should have a negligible performance penalty. This will only work in outline mode, so an arch must specify ARCH_DISABLE_KASAN_INLINE if it requires this. Cc: Balbir Singh Cc: Aneesh Kumar K.V Suggested-by: Christophe Leroy Reviewed-by: Marco Elver Signed-off-by: Daniel Axtens Reviewed-by: Andrey Konovalov --- Both previous RFCs for ppc64 - by 2 different people - have needed this trick! See: - https://lore.kernel.org/patchwork/patch/592820/ # ppc64 hash series - https://patchwork.ozlabs.org/patch/795211/ # ppc radix series Build tested on arm64 with SW_TAGS and x86 with INLINE: the error fires if I add a kasan_arch_is_ready define. --- mm/kasan/common.c | 3 +++ mm/kasan/generic.c | 3 +++ mm/kasan/kasan.h | 6 ++++++ mm/kasan/shadow.c | 6 ++++++ 4 files changed, 18 insertions(+) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 10177cc26d06..2baf121fb8c5 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -331,6 +331,9 @@ static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object, u8 tag; void *tagged_object; + if (!kasan_arch_is_ready()) + return false; + tag = get_tag(object); tagged_object = object; object = kasan_reset_tag(object); diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 53cbf28859b5..c3f5ba7a294a 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -163,6 +163,9 @@ static __always_inline bool check_region_inline(unsigned long addr, size_t size, bool write, unsigned long ret_ip) { + if (!kasan_arch_is_ready()) + return true; + if (unlikely(size == 0)) return true; diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 8f450bc28045..4dbc8def64f4 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -449,6 +449,12 @@ static inline void kasan_poison_last_granule(const void *address, size_t size) { #endif /* CONFIG_KASAN_GENERIC */ +#ifndef kasan_arch_is_ready +static inline bool kasan_arch_is_ready(void) { return true; } +#elif !defined(CONFIG_KASAN_GENERIC) || !defined(CONFIG_KASAN_OUTLINE) +#error kasan_arch_is_ready only works in KASAN generic outline mode! +#endif + /* * Exported functions for interfaces called from assembly or from generated * code. Declarations here to avoid warning about missing declarations. diff --git a/mm/kasan/shadow.c b/mm/kasan/shadow.c index 082ee5b6d9a1..8d95ee52d019 100644 --- a/mm/kasan/shadow.c +++ b/mm/kasan/shadow.c @@ -73,6 +73,9 @@ void kasan_poison(const void *addr, size_t size, u8 value, bool init) { void *shadow_start, *shadow_end; + if (!kasan_arch_is_ready()) + return; + /* * Perform shadow offset calculation based on untagged address, as * some of the callers (e.g. kasan_poison_object_data) pass tagged @@ -99,6 +102,9 @@ EXPORT_SYMBOL(kasan_poison); #ifdef CONFIG_KASAN_GENERIC void kasan_poison_last_granule(const void *addr, size_t size) { + if (!kasan_arch_is_ready()) + return; + if (size & KASAN_GRANULE_MASK) { u8 *shadow = (u8 *)kasan_mem_to_shadow(addr + size); *shadow = size & KASAN_GRANULE_MASK; From patchwork Thu Jun 24 03:40:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 12340753 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9AEFC48BC2 for ; Thu, 24 Jun 2021 03:41:11 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6EB06613C0 for ; Thu, 24 Jun 2021 03:41:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6EB06613C0 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=axtens.net Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 79D746B008C; Wed, 23 Jun 2021 23:41:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 780926B0092; Wed, 23 Jun 2021 23:41:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 616626B0093; Wed, 23 Jun 2021 23:41:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0227.hostedemail.com [216.40.44.227]) by kanga.kvack.org (Postfix) with ESMTP id 333566B008C for ; Wed, 23 Jun 2021 23:41:10 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 5881218068BCA for ; Thu, 24 Jun 2021 03:41:10 +0000 (UTC) X-FDA: 78287216700.01.2316AFF Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by imf05.hostedemail.com (Postfix) with ESMTP id 150A0E000113 for ; Thu, 24 Jun 2021 03:41:09 +0000 (UTC) Received: by mail-pg1-f181.google.com with SMTP id h4so3584063pgp.5 for ; Wed, 23 Jun 2021 20:41:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rvCzMPY1iDnXrmjh/pjQWKgOuSudghioSXufJUjyJjc=; b=J2OkXKObl6CYfnxQlkmmMaeOYyf2X6Xqhir66nx7wrcwvPBnkeeC3KAPr2dkh9bBvk dcgGqLWR2GuWsJDWbQCbhGoXzXg7rC/UvvUfZbJ3GYgvMU54Aly5ZTKyA54Wb49muTXL 7cqPneehzSNCUAVQlZrhlJusCiC4Edx4OYisY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rvCzMPY1iDnXrmjh/pjQWKgOuSudghioSXufJUjyJjc=; b=Fd90zfYZpLFUB/DH6BIL7WnAjiQolUXnsGul8QPQEjaQyYqttpUhIHPXPR3Tl51GL/ ILexpKWmTGZNx4AkrQYUsuerVPndQeGeHew1w2dvLHpD4EmcplMxW3a8vDH17wRWNO1E iurESlgd4pSlLAdGo4L0uovybUybEshIzJ7Gr7HuZ2f4N0IORuafljP06MbTZbGeIJVd L0X7/gM6rKeMcCWaEMVZnlHNa57QZJ312WqTm5V9ZxAvMrTSfx1hhhh6A3+4q6KYKTRT M6KMKB7pkOw4mpJ2yJsfKk+ZiZyIpH05HlJ8SUn4Mvc6kGHKWF5muqPzldsWgNybwOs6 ClGw== X-Gm-Message-State: AOAM530oy3q6LDF+4LoYxpnlpfQDD+GG3gSOQvVUQNlx2AVYtO+GEpaU nWxWyWGly8lLfAGuJEtoHtj+Vg== X-Google-Smtp-Source: ABdhPJxG3zy2eS/jaYiZ/EUqvgZ/Z2jIEolGittrt/Ulk+fSsAbrQb8DXATsxr4lxtZy+aRd09K8cw== X-Received: by 2002:a62:844d:0:b029:308:230c:fe3a with SMTP id k74-20020a62844d0000b0290308230cfe3amr906161pfd.34.1624506069293; Wed, 23 Jun 2021 20:41:09 -0700 (PDT) Received: from localhost ([203.206.29.204]) by smtp.gmail.com with ESMTPSA id j19sm556042pgm.44.2021.06.23.20.41.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Jun 2021 20:41:08 -0700 (PDT) From: Daniel Axtens To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com, elver@google.com, akpm@linux-foundation.org, andreyknvl@gmail.com Cc: linuxppc-dev@lists.ozlabs.org, christophe.leroy@csgroup.eu, aneesh.kumar@linux.ibm.com, bsingharora@gmail.com, Daniel Axtens Subject: [PATCH v16 3/4] mm: define default MAX_PTRS_PER_* in include/pgtable.h Date: Thu, 24 Jun 2021 13:40:49 +1000 Message-Id: <20210624034050.511391-4-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210624034050.511391-1-dja@axtens.net> References: <20210624034050.511391-1-dja@axtens.net> MIME-Version: 1.0 Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=axtens.net header.s=google header.b=J2OkXKOb; dmarc=none; spf=pass (imf05.hostedemail.com: domain of dja@axtens.net designates 209.85.215.181 as permitted sender) smtp.mailfrom=dja@axtens.net X-Rspamd-Server: rspam02 X-Stat-Signature: w7jm59na3m9eihqjqig9jgssgqjky1p4 X-Rspamd-Queue-Id: 150A0E000113 X-HE-Tag: 1624506069-657958 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: Commit c65e774fb3f6 ("x86/mm: Make PGDIR_SHIFT and PTRS_PER_P4D variable") made PTRS_PER_P4D variable on x86 and introduced MAX_PTRS_PER_P4D as a constant for cases which need a compile-time constant (e.g. fixed-size arrays). powerpc likewise has boot-time selectable MMU features which can cause other mm "constants" to vary. For KASAN, we have some static PTE/PMD/PUD/P4D arrays so we need compile-time maximums for all these constants. Extend the MAX_PTRS_PER_ idiom, and place default definitions in include/pgtable.h. These define MAX_PTRS_PER_x to be PTRS_PER_x unless an architecture has defined MAX_PTRS_PER_x in its arch headers. Clean up pgtable-nop4d.h and s390's MAX_PTRS_PER_P4D definitions while we're at it: both can just pick up the default now. Acked-by: Andrey Konovalov Reviewed-by: Christophe Leroy Reviewed-by: Marco Elver Signed-off-by: Daniel Axtens --- s390 was compile tested only. --- arch/s390/include/asm/pgtable.h | 2 -- include/asm-generic/pgtable-nop4d.h | 1 - include/linux/pgtable.h | 22 ++++++++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/arch/s390/include/asm/pgtable.h b/arch/s390/include/asm/pgtable.h index 79742f497cb5..dcac7b2df72c 100644 --- a/arch/s390/include/asm/pgtable.h +++ b/arch/s390/include/asm/pgtable.h @@ -343,8 +343,6 @@ static inline int is_module_addr(void *addr) #define PTRS_PER_P4D _CRST_ENTRIES #define PTRS_PER_PGD _CRST_ENTRIES -#define MAX_PTRS_PER_P4D PTRS_PER_P4D - /* * Segment table and region3 table entry encoding * (R = read-only, I = invalid, y = young bit): diff --git a/include/asm-generic/pgtable-nop4d.h b/include/asm-generic/pgtable-nop4d.h index 2f1d0aad645c..03b7dae47dd4 100644 --- a/include/asm-generic/pgtable-nop4d.h +++ b/include/asm-generic/pgtable-nop4d.h @@ -9,7 +9,6 @@ typedef struct { pgd_t pgd; } p4d_t; #define P4D_SHIFT PGDIR_SHIFT -#define MAX_PTRS_PER_P4D 1 #define PTRS_PER_P4D 1 #define P4D_SIZE (1UL << P4D_SHIFT) #define P4D_MASK (~(P4D_SIZE-1)) diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index fb20c57de2ce..d147480cdefc 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -1634,4 +1634,26 @@ typedef unsigned int pgtbl_mod_mask; #define pte_leaf_size(x) PAGE_SIZE #endif +/* + * Some architectures have MMUs that are configurable or selectable at boot + * time. These lead to variable PTRS_PER_x. For statically allocated arrays it + * helps to have a static maximum value. + */ + +#ifndef MAX_PTRS_PER_PTE +#define MAX_PTRS_PER_PTE PTRS_PER_PTE +#endif + +#ifndef MAX_PTRS_PER_PMD +#define MAX_PTRS_PER_PMD PTRS_PER_PMD +#endif + +#ifndef MAX_PTRS_PER_PUD +#define MAX_PTRS_PER_PUD PTRS_PER_PUD +#endif + +#ifndef MAX_PTRS_PER_P4D +#define MAX_PTRS_PER_P4D PTRS_PER_P4D +#endif + #endif /* _LINUX_PGTABLE_H */ From patchwork Thu Jun 24 03:40:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 12340755 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 34B4AC49EA5 for ; Thu, 24 Jun 2021 03:41:16 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D9523613C0 for ; Thu, 24 Jun 2021 03:41:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D9523613C0 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=axtens.net Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id E060A6B0093; Wed, 23 Jun 2021 23:41:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DDCA66B0095; Wed, 23 Jun 2021 23:41:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA5786B0096; Wed, 23 Jun 2021 23:41:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0161.hostedemail.com [216.40.44.161]) by kanga.kvack.org (Postfix) with ESMTP id 9B8166B0093 for ; Wed, 23 Jun 2021 23:41:14 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id C2CBD181AEF31 for ; Thu, 24 Jun 2021 03:41:14 +0000 (UTC) X-FDA: 78287216868.16.AD8F7DA Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by imf14.hostedemail.com (Postfix) with ESMTP id 7C5A7C00CBD7 for ; Thu, 24 Jun 2021 03:41:14 +0000 (UTC) Received: by mail-pg1-f178.google.com with SMTP id p9so3595665pgb.1 for ; Wed, 23 Jun 2021 20:41:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ctu6eGIgdooF8iPxRHw2B5UaQhrklgx8o8kc13VGL+0=; b=FooYSsrf/byfUjHr9v8Z5dcDSMVq+aOy9Zw2Y6OY+wiqjx/RpBD79cPIN4lJXSGXs3 4+vPSeNPFJMweHYJew6Xa77gKY1t6pM6NkcMtoX1pvl3TEEEFl8TL9aVfMp/WVgF0LL3 BRf01yjVSdrvlKtMN7gA78LXgbINNraud8dis= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ctu6eGIgdooF8iPxRHw2B5UaQhrklgx8o8kc13VGL+0=; b=qCjQKyjpJH0Iwz2SVF5wM5XdlpNKBGzL52hkojjXPYG0JUmy4ee5lhqCKsB7M2NB5m h8OwQe+5GLC3DIt59W5KLiQscSdd1AAuk4yaxm83v7XZG4mTyulq+3c+N+pWjzkycPKD jZpmeh7y1zLioJhVhziLEf6mJ7Qsa6m0SIOPz2f+QSceeFqWM52K1od4pRBtjR4TNdYk Iuey0sX1o0qtY0P+y1AtdIGf9cq/Z7pVEhIf/Gi/G9dcchZ/jOHL1ZeKqSLxr8mpz4sR PUw40JQvBgd7clRvyC1K+rV1GlU+co4IzVVXQ3LQsT41iBotvOekShsiLOuQPDV34Qnx +bGQ== X-Gm-Message-State: AOAM5313bxwIt1UCDB1YP392SNR55CWZyFJzdXFHdkeW1OcZMeimzdUc viuS/3Nwo3E/UErjoTjQSD6I8Q== X-Google-Smtp-Source: ABdhPJzyemWeDjYSvtIVYwsIq1X0bowWLkR1TGinCknV//VarDM6vRNV9QrEYKNeX2yQiXYPgJ/wAQ== X-Received: by 2002:a62:1657:0:b029:301:af69:5ae3 with SMTP id 84-20020a6216570000b0290301af695ae3mr2929381pfw.57.1624506073704; Wed, 23 Jun 2021 20:41:13 -0700 (PDT) Received: from localhost ([203.206.29.204]) by smtp.gmail.com with ESMTPSA id n69sm1160501pfd.132.2021.06.23.20.41.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Jun 2021 20:41:13 -0700 (PDT) From: Daniel Axtens To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com, elver@google.com, akpm@linux-foundation.org, andreyknvl@gmail.com Cc: linuxppc-dev@lists.ozlabs.org, christophe.leroy@csgroup.eu, aneesh.kumar@linux.ibm.com, bsingharora@gmail.com, Daniel Axtens Subject: [PATCH v16 4/4] kasan: use MAX_PTRS_PER_* for early shadow tables Date: Thu, 24 Jun 2021 13:40:50 +1000 Message-Id: <20210624034050.511391-5-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210624034050.511391-1-dja@axtens.net> References: <20210624034050.511391-1-dja@axtens.net> MIME-Version: 1.0 Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=axtens.net header.s=google header.b=FooYSsrf; dmarc=none; spf=pass (imf14.hostedemail.com: domain of dja@axtens.net designates 209.85.215.178 as permitted sender) smtp.mailfrom=dja@axtens.net X-Rspamd-Server: rspam02 X-Stat-Signature: oh4b6dh69dze4pn7tue1w61783dqqqgo X-Rspamd-Queue-Id: 7C5A7C00CBD7 X-HE-Tag: 1624506074-182330 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: powerpc has a variable number of PTRS_PER_*, set at runtime based on the MMU that the kernel is booted under. This means the PTRS_PER_* are no longer constants, and therefore breaks the build. Switch to using MAX_PTRS_PER_*, which are constant. Suggested-by: Christophe Leroy Suggested-by: Balbir Singh Reviewed-by: Christophe Leroy Reviewed-by: Balbir Singh Reviewed-by: Marco Elver Reviewed-by: Andrey Konovalov Signed-off-by: Daniel Axtens --- include/linux/kasan.h | 6 +++--- mm/kasan/init.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 768d7d342757..5310e217bd74 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -41,9 +41,9 @@ struct kunit_kasan_expectation { #endif extern unsigned char kasan_early_shadow_page[PAGE_SIZE]; -extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE + PTE_HWTABLE_PTRS]; -extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD]; -extern pud_t kasan_early_shadow_pud[PTRS_PER_PUD]; +extern pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE + PTE_HWTABLE_PTRS]; +extern pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD]; +extern pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD]; extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D]; int kasan_populate_early_shadow(const void *shadow_start, diff --git a/mm/kasan/init.c b/mm/kasan/init.c index 348f31d15a97..cc64ed6858c6 100644 --- a/mm/kasan/init.c +++ b/mm/kasan/init.c @@ -41,7 +41,7 @@ static inline bool kasan_p4d_table(pgd_t pgd) } #endif #if CONFIG_PGTABLE_LEVELS > 3 -pud_t kasan_early_shadow_pud[PTRS_PER_PUD] __page_aligned_bss; +pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD] __page_aligned_bss; static inline bool kasan_pud_table(p4d_t p4d) { return p4d_page(p4d) == virt_to_page(lm_alias(kasan_early_shadow_pud)); @@ -53,7 +53,7 @@ static inline bool kasan_pud_table(p4d_t p4d) } #endif #if CONFIG_PGTABLE_LEVELS > 2 -pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD] __page_aligned_bss; +pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD] __page_aligned_bss; static inline bool kasan_pmd_table(pud_t pud) { return pud_page(pud) == virt_to_page(lm_alias(kasan_early_shadow_pmd)); @@ -64,7 +64,7 @@ static inline bool kasan_pmd_table(pud_t pud) return false; } #endif -pte_t kasan_early_shadow_pte[PTRS_PER_PTE + PTE_HWTABLE_PTRS] +pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE + PTE_HWTABLE_PTRS] __page_aligned_bss; static inline bool kasan_pte_table(pmd_t pmd)