From patchwork Thu Jun 17 06:39:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 12326861 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 9980AC2B9F4 for ; Thu, 17 Jun 2021 06:40:11 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 10E5A6101A for ; Thu, 17 Jun 2021 06:40:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 10E5A6101A 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 A2D6A6B0071; Thu, 17 Jun 2021 02:40:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A04436B0072; Thu, 17 Jun 2021 02:40:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 85A4B6B0073; Thu, 17 Jun 2021 02:40:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0209.hostedemail.com [216.40.44.209]) by kanga.kvack.org (Postfix) with ESMTP id 4E4DC6B0071 for ; Thu, 17 Jun 2021 02:40:10 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id D6ED019B2D for ; Thu, 17 Jun 2021 06:40:09 +0000 (UTC) X-FDA: 78262266138.14.47CF9E0 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf18.hostedemail.com (Postfix) with ESMTP id 944662001097 for ; Thu, 17 Jun 2021 06:39:59 +0000 (UTC) Received: by mail-pl1-f174.google.com with SMTP id c15so2314319pls.13 for ; Wed, 16 Jun 2021 23:40: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=FhcxQma+NItwG5Mp37m/KF4tp14MA8dMkKZlGGIww7w=; b=NX/qvxr8dhuE37o1z5sgt2XG3iamjEnXiWs3Cjuob6iS4N7i54VKlfizgYe8xGGkPG ECD5YCGaTs51KaCl4epayh0dHJ+o8LndpifLgVU7jrsZXsK+fxf956QA+f9kaEQexM97 ESKPsh4uIbJJ1pXHDwjoTPF3Wsz2Knn+hfVYc= 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=FhcxQma+NItwG5Mp37m/KF4tp14MA8dMkKZlGGIww7w=; b=Gb/WBKsyZE4izt1XPYts2ZmIlVz5kTyamhdE4ZSkAZ+p0NxWjeYPmpRLHzSIGnaDvq P5h035aV57OXRcax2aseBWU4NySgvBToh9Q12P9TIXWwIA2cuVG2o4CMcutNtEoJU5i6 di1D4o+68KDODrJTS83zzO0kaXcqnKGnKChp5GTL4dhnB5i/xwbb9fOMky2WbuOmedHm XGkPAKJsAoYvIehbI00AMIkLxLkeD9yGTH3eCb/UI0sSFimVC3p0xzOrCmibkb7fM+im /fqTRxaD7l1UHyMvPzTjPMdp7XAGn97JN+H42FFCjiqiLAMR7EFMtYkcyZ1LukW7ZBYV ZJ9A== X-Gm-Message-State: AOAM533vq4rwogHW7gdPVsZdlziISozaVOkWooc52mSot7/VHMdORlLE SjEajIkKK7693M1zrIrw6nO5mw== X-Google-Smtp-Source: ABdhPJzfeN5byN4YxzyNu/GjCulyBlU25iQrctXCoCFZvJ5L0B+A23QQj4gHY87HwYK24nhSnOkqEg== X-Received: by 2002:a17:90b:38ca:: with SMTP id nn10mr4147573pjb.127.1623912008754; Wed, 16 Jun 2021 23:40:08 -0700 (PDT) Received: from localhost ([203.206.29.204]) by smtp.gmail.com with ESMTPSA id d12sm3987316pfo.113.2021.06.16.23.40.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Jun 2021 23:40: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 v14 1/4] kasan: allow an architecture to disable inline instrumentation Date: Thu, 17 Jun 2021 16:39:53 +1000 Message-Id: <20210617063956.94061-2-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210617063956.94061-1-dja@axtens.net> References: <20210617063956.94061-1-dja@axtens.net> MIME-Version: 1.0 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=axtens.net header.s=google header.b="NX/qvxr8"; spf=pass (imf18.hostedemail.com: domain of dja@axtens.net designates 209.85.214.174 as permitted sender) smtp.mailfrom=dja@axtens.net; dmarc=none X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 944662001097 X-Stat-Signature: 7w31f1x4s84mk3uq3er9upcmwseyhboy X-HE-Tag: 1623911999-693540 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. Signed-off-by: Daniel Axtens Reviewed-by: Marco Elver --- lib/Kconfig.kasan | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan index cffc2ebbf185..cb5e02d09e11 100644 --- a/lib/Kconfig.kasan +++ b/lib/Kconfig.kasan @@ -12,6 +12,15 @@ config HAVE_ARCH_KASAN_HW_TAGS config HAVE_ARCH_KASAN_VMALLOC bool +config ARCH_DISABLE_KASAN_INLINE + bool + help + Sometimes an architecture might not be able to support inline + instrumentation but might be able to support outline instrumentation. + This option allows an architecture to prevent inline and stack + instrumentation from being enabled. + + config CC_HAS_KASAN_GENERIC def_bool $(cc-option, -fsanitize=kernel-address) @@ -130,6 +139,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 +151,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 +165,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, this 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 17 06:39:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 12326863 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 99263C2B9F4 for ; Thu, 17 Jun 2021 06:40:15 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4A689613D5 for ; Thu, 17 Jun 2021 06:40:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4A689613D5 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 DFEEC6B0072; Thu, 17 Jun 2021 02:40:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DAEAD6B0073; Thu, 17 Jun 2021 02:40:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C00A46B0074; Thu, 17 Jun 2021 02:40:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0066.hostedemail.com [216.40.44.66]) by kanga.kvack.org (Postfix) with ESMTP id 8B8A46B0072 for ; Thu, 17 Jun 2021 02:40:14 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 29127180AD806 for ; Thu, 17 Jun 2021 06:40:14 +0000 (UTC) X-FDA: 78262266348.04.B239343 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by imf23.hostedemail.com (Postfix) with ESMTP id 6BB6CA00024C for ; Thu, 17 Jun 2021 06:40:04 +0000 (UTC) Received: by mail-pf1-f180.google.com with SMTP id q25so4184603pfh.7 for ; Wed, 16 Jun 2021 23:40:13 -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=ly/BuIfXTXWQRb2ER2SUgvo52sNSO+fWpTpraSlxuJA=; b=aX8fvAkw0NnLjptOJGjsWgLjfBnZuxomdyEksoWvEoE2hfLu1Ni1jgdXsXXy0bv8iK BmedfPkZ6W8YTgn8JBNTY9jU/jGtvx5bl8BdVJTSNYGbgU6zPe0e8GgguPAdHmMb5kXX smhvtwv/OHiSSjfVGI3g+8gd6NxHg8cKRtoSo= 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=ly/BuIfXTXWQRb2ER2SUgvo52sNSO+fWpTpraSlxuJA=; b=twMi+fZUnA5jXSStaSLQMITEpV3504BiULucbVfiDo/ZOtxqQ1/G2hT2jo4MovUg2H VbADVAiG6FAuJNl6tItb5MuwPVlXVwUQMblOyTsGqKUnnEmkirkB8XNmr1quNr3KJ78r EYB1pc95eptie+AZQxZDYDqOioptDP7ykZltWy4ddgO3hz7BjuwBpogo0LyS9gQyBMWC 5KgjBVe40cVRn6NBNMY2B+Iem496yTuActlfhGpX0iSDPvPjPvX7IXjgbLKcFiz77nnU b6Qv7aOVxFH0dwFNkZw0Q1Ugvz8wtSboCSzSzD/XgCyOCHBca15MZUjDZ57/7OWGbxCG A9gg== X-Gm-Message-State: AOAM533Rm7LB+u/hb82AHtK1Pv3gYYWHrW3p+3EIKBWOHwV7X7pjOY4+ 5zlrAVjgfHY54UZ2cXdQVnDhdw== X-Google-Smtp-Source: ABdhPJziIh9pak7ePgn640GXrKi8OmoFxF9HwxgtphDZ7oQ/aKdnyEK0aHD02f8BwURqgGolU13O3g== X-Received: by 2002:a65:434c:: with SMTP id k12mr3551339pgq.17.1623912012898; Wed, 16 Jun 2021 23:40:12 -0700 (PDT) Received: from localhost ([203.206.29.204]) by smtp.gmail.com with ESMTPSA id 65sm3950520pfu.159.2021.06.16.23.40.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Jun 2021 23:40:12 -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 , "Aneesh Kumar K . V" Subject: [PATCH v14 2/4] kasan: allow architectures to provide an outline readiness check Date: Thu, 17 Jun 2021 16:39:54 +1000 Message-Id: <20210617063956.94061-3-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210617063956.94061-1-dja@axtens.net> References: <20210617063956.94061-1-dja@axtens.net> MIME-Version: 1.0 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 6BB6CA00024C Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=axtens.net header.s=google header.b=aX8fvAkw; spf=pass (imf23.hostedemail.com: domain of dja@axtens.net designates 209.85.210.180 as permitted sender) smtp.mailfrom=dja@axtens.net; dmarc=none X-Stat-Signature: ncznijkrmxkbcyonxxpiqnmun75taxn6 X-HE-Tag: 1623912004-794611 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 Signed-off-by: Daniel Axtens Reviewed-by: Marco Elver --- 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 I haven't been able to exercise the arch hook error for !GENERIC as I don't have a particularly modern aarch64 toolchain or a lot of experience cross-compiling with clang. But it does fire for GENERIC + INLINE on x86. --- mm/kasan/common.c | 4 ++++ mm/kasan/generic.c | 3 +++ mm/kasan/kasan.h | 8 ++++++++ mm/kasan/shadow.c | 8 ++++++++ 4 files changed, 23 insertions(+) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 10177cc26d06..0ad615f3801d 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -331,6 +331,10 @@ static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object, u8 tag; void *tagged_object; + /* Bail if the arch isn't ready */ + 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..b18abaf8c78e 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -449,6 +449,14 @@ 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; } +#else +#if !defined(CONFIG_KASAN_GENERIC) || !defined(CONFIG_KASAN_OUTLINE) +#error kasan_arch_is_ready only works in KASAN generic outline mode! +#endif +#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..3c7f7efe6f68 100644 --- a/mm/kasan/shadow.c +++ b/mm/kasan/shadow.c @@ -73,6 +73,10 @@ void kasan_poison(const void *addr, size_t size, u8 value, bool init) { void *shadow_start, *shadow_end; + /* Don't touch the shadow memory if arch isn't ready */ + 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 +103,10 @@ EXPORT_SYMBOL(kasan_poison); #ifdef CONFIG_KASAN_GENERIC void kasan_poison_last_granule(const void *addr, size_t size) { + /* Don't touch the shadow memory if arch isn't ready */ + 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 17 06:39:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 12326865 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 C039DC49361 for ; Thu, 17 Jun 2021 06:40:19 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 70982613D5 for ; Thu, 17 Jun 2021 06:40:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 70982613D5 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 12D8D6B0073; Thu, 17 Jun 2021 02:40:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0B6766B0074; Thu, 17 Jun 2021 02:40:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E98DC6B0075; Thu, 17 Jun 2021 02:40:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0226.hostedemail.com [216.40.44.226]) by kanga.kvack.org (Postfix) with ESMTP id B85156B0073 for ; Thu, 17 Jun 2021 02:40:18 -0400 (EDT) Received: from smtpin39.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 5ADF5180AD806 for ; Thu, 17 Jun 2021 06:40:18 +0000 (UTC) X-FDA: 78262266516.39.604FFA7 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by imf27.hostedemail.com (Postfix) with ESMTP id F0BCD80192FE for ; Thu, 17 Jun 2021 06:40:06 +0000 (UTC) Received: by mail-pg1-f169.google.com with SMTP id g22so4145000pgk.1 for ; Wed, 16 Jun 2021 23:40:17 -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=b1Pi1RzyfsWNKu+C0AG0URkCeCwQ85qxvHvYbK7X+Gk=; b=D0qWqK+j9bAxupl/GWzyDACuzv1h1iWk4UZlczb/9+tQhGbi7xxPWBaUpi4kDTbDey H/kqsOFeduz6TKC/7oGW33vzxgZ7OKxRZDJ2UBJzYRKsnKluSBNjyXvhB6lbvBoS9lnA VNJehADe90JM5Ksgr2/aC4d0Kloqzdw/174pE= 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=b1Pi1RzyfsWNKu+C0AG0URkCeCwQ85qxvHvYbK7X+Gk=; b=V+ZNlfLQ/X53+qeyQKUUhKBV7YggneUjqIfdtR6cXTefqnfbSj+a2OCQU2CkgxOANK Tr8/SJr5T62j2tTZkH0l7XAwofEHVxAsmETVLQIi7Fu83urvW+StHvZVDHRKFI4asgWd IXnTjwto9hTRNkl4aRXvbdWzan1IUUGGqvbvMwEOjB9d+NU0H92UUwT47rl1My1+Id0R SHgGseQd81bOCXqUpMP1y04ui02H76BExmiGbAFBehbrhuVk3P888Nq7AAXIVwHzLZB2 xmutlAu274nj41YmF4qDAv2/Dv/utLxmjG3epcYPPH1uyOQ9hF06kXgS7W4x1wXJqK6m xlRw== X-Gm-Message-State: AOAM5305zDYRK8vsd3rATvxb7gsWmWD6voe9/yD59AGnAKGz+pwYJfRF FgDsymXTWNQM+k7wmzSJWuN9TPsGrhXkFQ== X-Google-Smtp-Source: ABdhPJw1k3C0RQW96NTpSA3/18qlSWZth0xfR3FedqjMvvE1Q/rgMD0EE6zlpgi6W++lsBaQb4dL9Q== X-Received: by 2002:a63:1e55:: with SMTP id p21mr3505606pgm.412.1623912017140; Wed, 16 Jun 2021 23:40:17 -0700 (PDT) Received: from localhost ([203.206.29.204]) by smtp.gmail.com with ESMTPSA id o3sm3981688pfd.41.2021.06.16.23.40.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Jun 2021 23:40:16 -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 v14 3/4] mm: define default MAX_PTRS_PER_* in include/pgtable.h Date: Thu, 17 Jun 2021 16:39:55 +1000 Message-Id: <20210617063956.94061-4-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210617063956.94061-1-dja@axtens.net> References: <20210617063956.94061-1-dja@axtens.net> MIME-Version: 1.0 Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=axtens.net header.s=google header.b=D0qWqK+j; spf=pass (imf27.hostedemail.com: domain of dja@axtens.net designates 209.85.215.169 as permitted sender) smtp.mailfrom=dja@axtens.net; dmarc=none X-Stat-Signature: zwsgikwwnj3b4hcqmrr4ugsq6iruaxdo X-Rspamd-Queue-Id: F0BCD80192FE X-Rspamd-Server: rspam06 X-HE-Tag: 1623912006-255304 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. Signed-off-by: Daniel Axtens Reviewed-by: Christophe Leroy Reviewed-by: Marco Elver --- 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 7c66ae5d7e32..cf05954ce013 100644 --- a/arch/s390/include/asm/pgtable.h +++ b/arch/s390/include/asm/pgtable.h @@ -342,8 +342,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 ce2cbb3c380f..2f6b1befb129 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 9e6f71265f72..69700e3e615f 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -1625,4 +1625,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 17 06:39:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 12326867 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 D2A98C2B9F4 for ; Thu, 17 Jun 2021 06:40:23 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8630F613D5 for ; Thu, 17 Jun 2021 06:40:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8630F613D5 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 3023E6B0074; Thu, 17 Jun 2021 02:40:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2B2596B0075; Thu, 17 Jun 2021 02:40:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 106856B0078; Thu, 17 Jun 2021 02:40:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D28186B0074 for ; Thu, 17 Jun 2021 02:40:22 -0400 (EDT) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 66C7F181AEF00 for ; Thu, 17 Jun 2021 06:40:22 +0000 (UTC) X-FDA: 78262266684.03.9B3193A Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) by imf24.hostedemail.com (Postfix) with ESMTP id 4DDA6A00025C for ; Thu, 17 Jun 2021 06:40:11 +0000 (UTC) Received: by mail-pg1-f171.google.com with SMTP id w31so4128601pga.6 for ; Wed, 16 Jun 2021 23:40:21 -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=eRdt/wFzvgUcAph19a7+La8R0pqDnXPYkPybGzP3TFs=; b=LW8mQ1aUFkj03n/RiqJV2xkT3exIY4LtDgccB7rozmA2OQfAiuVQxboDoQGry9XmzT Rw6MVtymYZEzC5bYdmJ+4EYgpjgqjhkzeSU5agu/yHfWJlU7hk6D7ZBbs+2zPDD71A5N pfCmsHWyxSHDaUWlivIYJVqE4KttoIxL2Gvx8= 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=eRdt/wFzvgUcAph19a7+La8R0pqDnXPYkPybGzP3TFs=; b=kvGjBNNeqDcPv1sZG3nLnl72D71zlxmEX2GJtIDJbr6P1rxUKflWnS+S3iY/pxh4xQ 26BcveyZj4LcufAk+BWJLQ97fb+zzG3vgWjJxJYvkl9R/Sv+iJZYaPwbMhm4Vc8fojw2 qBPB3cFn0ooVnRLqHy462mRiexC+jiaEqvTs8l17t1PEKLcQDDDmoI5p7RJPrz1Vr/fS woaoZ0oYfIgHj2sEGjxssgD2cKlxEhOB8tBSKEMbvbNFkjbKxb58V106qvGXLTvKWmX3 0afm2a7Q8KrvQ8cl19cfqKwauvoRPriv5xNujUrE4v3jBBOFnOEOM8gtyniNzv793oRF yA8Q== X-Gm-Message-State: AOAM531EjQMOkY623RYL0g8IftaxncoiJBg84G7iXmOy/D57HvNWW0t4 X2WpxgCIIZoqb1NFASm6TIRzzA== X-Google-Smtp-Source: ABdhPJxZ8JWJ4Mjr5ash2IXA6R5rV4gGGXHwovDZyw0LOK/tt3IT1XtCKUzvjMkiK2XZ7DVnqYtOEA== X-Received: by 2002:a63:64a:: with SMTP id 71mr3578146pgg.360.1623912021307; Wed, 16 Jun 2021 23:40:21 -0700 (PDT) Received: from localhost ([203.206.29.204]) by smtp.gmail.com with ESMTPSA id o34sm717337pgm.6.2021.06.16.23.40.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Jun 2021 23:40:21 -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 v14 4/4] kasan: use MAX_PTRS_PER_* for early shadow tables Date: Thu, 17 Jun 2021 16:39:56 +1000 Message-Id: <20210617063956.94061-5-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210617063956.94061-1-dja@axtens.net> References: <20210617063956.94061-1-dja@axtens.net> MIME-Version: 1.0 Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=axtens.net header.s=google header.b=LW8mQ1aU; dmarc=none; spf=pass (imf24.hostedemail.com: domain of dja@axtens.net designates 209.85.215.171 as permitted sender) smtp.mailfrom=dja@axtens.net X-Rspamd-Server: rspam02 X-Stat-Signature: 8bkd4y5b4oppkr7xgpjdx7su5gyfhjqz X-Rspamd-Queue-Id: 4DDA6A00025C X-HE-Tag: 1623912011-72035 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 Signed-off-by: Daniel Axtens Reviewed-by: Marco Elver --- 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)