From patchwork Thu Jun 17 09:30:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 12327105 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 0797BC48BE5 for ; Thu, 17 Jun 2021 09:30:45 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9F0CE610CA for ; Thu, 17 Jun 2021 09:30:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9F0CE610CA 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 43D9F6B0071; Thu, 17 Jun 2021 05:30:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 414376B0072; Thu, 17 Jun 2021 05:30:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2B52F6B0073; Thu, 17 Jun 2021 05:30:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0128.hostedemail.com [216.40.44.128]) by kanga.kvack.org (Postfix) with ESMTP id EFB186B0071 for ; Thu, 17 Jun 2021 05:30:43 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 9573E180AD804 for ; Thu, 17 Jun 2021 09:30:43 +0000 (UTC) X-FDA: 78262695966.16.3133706 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by imf23.hostedemail.com (Postfix) with ESMTP id D3251A0001CF for ; Thu, 17 Jun 2021 09:30:33 +0000 (UTC) Received: by mail-pf1-f180.google.com with SMTP id x73so4519983pfc.8 for ; Thu, 17 Jun 2021 02:30:43 -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=1nSYxJY0N3aZr/pdi8HOrkpkeyyKzwB00+Ef/RsvCEU=; b=VxBhETKa1BkglHL6eiGuDKau5qIlvO2SE6buqTTYwaSC4Qq5gYa8/0Iu8HTz88B0+o bv8FuQFbSiqr5YoaoJu0h/DwAkJzkeYN4j9I34Y3qNg1dnVP4NtMb2C4vKeoWXhrcSFi LAMbptK+3YwoGNurib7Mg7XZAtIEpcnkIpe6s= 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=1nSYxJY0N3aZr/pdi8HOrkpkeyyKzwB00+Ef/RsvCEU=; b=tBzzhdwVDtdPJLbMGVhYH+e0H7uGd/geEVYs0AOqzn/FtgD+NrzfoAI/QsK81iC6hX 2kgKSH8Oe/5OYuSuXcG81vMltifCxbdhen7a9X8EqhhccVPCiTLRxLHe6eDMvwpxQqfW ywMC/sst3OxhtFviD0h67zCVJjozvL2s0jV2aST0OD5djTCH0CR/2lNrBswZMbrW5do0 rC2zEQwGOADwaIvpQbxaHZpTR5qSBm7Be7o3AzCyVr1oUWXbP2pAIQsB/r9gZUCS7jx0 aF4Ae2gn2LLK0thpYZKWF/6FaNbGsCv5coMOGraRR+fxmV27TacC2MSyLDyuAwTWvGIi cGHw== X-Gm-Message-State: AOAM530inE7aM2cgG+I0HmwWiRILJJ8gjfT+zJyO1tc3xDDwu1x3gk+C wkFhOQ8vvsVFbFKNNRT5FvW9pw== X-Google-Smtp-Source: ABdhPJyV1oWZvDdmYRFVr5QxoEcMf74PgidkzSk77dFrjXAyQzrlIf5IqVEJJ6SfYAnO1F6jO2BI8A== X-Received: by 2002:a62:380c:0:b029:2f7:4057:c3ed with SMTP id f12-20020a62380c0000b02902f74057c3edmr4363473pfa.21.1623922242435; Thu, 17 Jun 2021 02:30:42 -0700 (PDT) Received: from localhost ([203.206.29.204]) by smtp.gmail.com with ESMTPSA id a23sm4404876pff.43.2021.06.17.02.30.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 02:30:42 -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 v15 1/4] kasan: allow an architecture to disable inline instrumentation Date: Thu, 17 Jun 2021 19:30:29 +1000 Message-Id: <20210617093032.103097-2-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210617093032.103097-1-dja@axtens.net> References: <20210617093032.103097-1-dja@axtens.net> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: D3251A0001CF X-Stat-Signature: 6tze55wzzbrw95w1r7x14hsn5jpfwi7u Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=axtens.net header.s=google header.b=VxBhETKa; dmarc=none; spf=pass (imf23.hostedemail.com: domain of dja@axtens.net designates 209.85.210.180 as permitted sender) smtp.mailfrom=dja@axtens.net X-HE-Tag: 1623922233-924254 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 --- 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 09:30:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 12327107 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 33AA5C49361 for ; Thu, 17 Jun 2021 09:30:49 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D3972610CA for ; Thu, 17 Jun 2021 09:30:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D3972610CA 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 74AD86B0072; Thu, 17 Jun 2021 05:30:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7215F6B0073; Thu, 17 Jun 2021 05:30:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C41B6B0074; Thu, 17 Jun 2021 05:30:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0183.hostedemail.com [216.40.44.183]) by kanga.kvack.org (Postfix) with ESMTP id 25BB16B0072 for ; Thu, 17 Jun 2021 05:30:48 -0400 (EDT) Received: from smtpin38.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id C71A81A4BE for ; Thu, 17 Jun 2021 09:30:47 +0000 (UTC) X-FDA: 78262696134.38.EB5F839 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf08.hostedemail.com (Postfix) with ESMTP id B50C5801934C for ; Thu, 17 Jun 2021 09:30:36 +0000 (UTC) Received: by mail-pl1-f179.google.com with SMTP id v12so2608986plo.10 for ; Thu, 17 Jun 2021 02:30:47 -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=jAXfPAXKGHI5d5AfauBRh2rw8y0WXn7UCziFxxBa2G8=; b=J9G3pN4FhbqRJPqASQ8qt+CEBkWuvvRU/rw38HT/3meNPQk+HthB+65dgOlLVPsG/+ OgtxlTlJ26xmGHZ34aiyGW1dqvAoOBnFAsJcoqPGmRFN49OrSQjpIAEfsPIblsbQsb8R sfesjLQh+52EJAox9Ost1FtWIufCsCNqgf0yI= 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=jAXfPAXKGHI5d5AfauBRh2rw8y0WXn7UCziFxxBa2G8=; b=g772Lgi2Nn0XVE9t7yzJDNndNw7QfqG3BzAo/eq6BjW1MEUR9uLZnMWHh9nAIlbVdv SKSW3OuyWaCdLuZbCH1wolOuSnth+A7DKl/D0ePTi48hAivInNTQSVb4s2AFYJQbMD8D jAs35Pcl5fmqtdKSeNImdj1zCCgtTVfwy30gfHVkfm6k64XGsCD3aFjLu3LrLjfL+Usu spqyQztHsy2TlygZAQucpELVFLVK1Wf3VSyydBkSSWg1M2/2uFjuee2VfTu3Gsvc7/tF fOy9CKwc9KxBhu4EWoQPyJzwCRhSduOJGXwSTFVDUJ67GMlbVqzPae7C9EkpXRkBAk7i r23w== X-Gm-Message-State: AOAM533Nyr8mDzJ2w+iGWPG8KaTSHymX30VzrRkqjY6NPpIqvBhPxmGw 8FILunbnWunYg8Eohj7vG6iSDA== X-Google-Smtp-Source: ABdhPJziDsm92Q8ZeF77qbXKOrLgOBSfL1/bSWAlt+9ZfcRv3k6RL55+fUErfACWDJuxtEoYMzljXQ== X-Received: by 2002:a17:90a:f094:: with SMTP id cn20mr4668681pjb.157.1623922246569; Thu, 17 Jun 2021 02:30:46 -0700 (PDT) Received: from localhost ([203.206.29.204]) by smtp.gmail.com with ESMTPSA id t14sm5692272pgm.9.2021.06.17.02.30.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 02:30:46 -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 v15 2/4] kasan: allow architectures to provide an outline readiness check Date: Thu, 17 Jun 2021 19:30:30 +1000 Message-Id: <20210617093032.103097-3-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210617093032.103097-1-dja@axtens.net> References: <20210617093032.103097-1-dja@axtens.net> MIME-Version: 1.0 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: B50C5801934C Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=axtens.net header.s=google header.b=J9G3pN4F; spf=pass (imf08.hostedemail.com: domain of dja@axtens.net designates 209.85.214.179 as permitted sender) smtp.mailfrom=dja@axtens.net; dmarc=none X-Stat-Signature: yrdzmwd31iadtzcafc1w9sxgg8h5rfxr X-HE-Tag: 1623922236-545240 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 --- 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 | 4 ++++ mm/kasan/generic.c | 3 +++ mm/kasan/kasan.h | 6 ++++++ mm/kasan/shadow.c | 8 ++++++++ 4 files changed, 21 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..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..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 09:30:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 12327109 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 D2BCFC49EA3 for ; Thu, 17 Jun 2021 09:30:53 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 780EE610CA for ; Thu, 17 Jun 2021 09:30:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 780EE610CA 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 1CCB56B0073; Thu, 17 Jun 2021 05:30:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A4106B0074; Thu, 17 Jun 2021 05:30:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 01ED76B0075; Thu, 17 Jun 2021 05:30:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0156.hostedemail.com [216.40.44.156]) by kanga.kvack.org (Postfix) with ESMTP id BF64F6B0073 for ; Thu, 17 Jun 2021 05:30:52 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 62731181AC9CC for ; Thu, 17 Jun 2021 09:30:52 +0000 (UTC) X-FDA: 78262696344.01.9716F43 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf27.hostedemail.com (Postfix) with ESMTP id B96648019364 for ; Thu, 17 Jun 2021 09:30:40 +0000 (UTC) Received: by mail-pl1-f174.google.com with SMTP id c15so2535044pls.13 for ; Thu, 17 Jun 2021 02:30:51 -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=Nxkv0O/k/UB4v+Xu5eY5paN7/JzM4aFGdvV8fAfzbmY=; b=MHJxWIovP6QPbQfpNJwTtkJ1N8BPMstXj33UgXdcn6tm9In+reJtsZtOQpYslq8mPE NyoONyT9vRCtwke7GYQmV9voyDhxZXQGhIwgzsNmp8PXFAlDUJQRoSKRcMK63LapHgfX YaPm3/CAm6p6Nau9KHQPAHQ4oYbFQWDnFSQAI= 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=Nxkv0O/k/UB4v+Xu5eY5paN7/JzM4aFGdvV8fAfzbmY=; b=IeFj9ZH9YvSenKljpxct+UFNVDa7troL8pHQGOktDHkx3YoDfzNsza7wtupX6A1obr wpuCybFYyFy11LClQl2FTi2esZTvqE93tiVjDl41d+JUjYnieOUpKAcezdYsWIO7bDoY MThsF60pRuSik/CoAbpRDcul0NMnqc1PCH0pinSE1wD+nbxWSlTQi3P7SXu4UZ48H02I Mqu4g5kLZ/mZaDVqhaKPBkPRNWPxdSzZfsreJxc2jfIWvY7+0rBIxmlu+Q6se0lsgxaX +VwGdqJRMh6afgLkuR8QtAsYI0IebC1Ql25EV3W8nn7Tgh8zwKVoDCKLi9qSotgjN5dv JhLg== X-Gm-Message-State: AOAM530USuqz5c5l7YAtxBV+M4nCSw59HtK6ulsDj/JhzUboMUp8eDPY RYxRWbPX4jMhBVjQ5uXIlm4a7g== X-Google-Smtp-Source: ABdhPJzOOCMkqO+yzl7cQxSeb9G1HmppdYlcsLp9lVbxpXhoQzl4HpWo9T4rPKjf0rRmqmkeYB3Saw== X-Received: by 2002:a17:902:dac2:b029:110:994a:abc3 with SMTP id q2-20020a170902dac2b0290110994aabc3mr3626941plx.78.1623922251151; Thu, 17 Jun 2021 02:30:51 -0700 (PDT) Received: from localhost ([203.206.29.204]) by smtp.gmail.com with ESMTPSA id v8sm4557656pff.34.2021.06.17.02.30.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 02:30:50 -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 v15 3/4] mm: define default MAX_PTRS_PER_* in include/pgtable.h Date: Thu, 17 Jun 2021 19:30:31 +1000 Message-Id: <20210617093032.103097-4-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210617093032.103097-1-dja@axtens.net> References: <20210617093032.103097-1-dja@axtens.net> MIME-Version: 1.0 Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=axtens.net header.s=google header.b=MHJxWIov; spf=pass (imf27.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: B96648019364 X-Stat-Signature: g3sqpdbxhrxjrh6twyu3xygmijmgd49b X-HE-Tag: 1623922240-749350 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. Reviewed-by: Christophe Leroy Reviewed-by: Marco Elver Signed-off-by: Daniel Axtens Acked-by: Andrey Konovalov --- 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 09:30:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 12327111 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 33990C2B9F4 for ; Thu, 17 Jun 2021 09:30:58 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D0BE7610CA for ; Thu, 17 Jun 2021 09:30:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D0BE7610CA 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 74B496B0074; Thu, 17 Jun 2021 05:30:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 721766B0075; Thu, 17 Jun 2021 05:30:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C2A86B0078; Thu, 17 Jun 2021 05:30:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0151.hostedemail.com [216.40.44.151]) by kanga.kvack.org (Postfix) with ESMTP id 2E2156B0074 for ; Thu, 17 Jun 2021 05:30:57 -0400 (EDT) Received: from smtpin40.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id B902D180AD804 for ; Thu, 17 Jun 2021 09:30:56 +0000 (UTC) X-FDA: 78262696512.40.B450DB8 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by imf17.hostedemail.com (Postfix) with ESMTP id 9268B4202A0E for ; Thu, 17 Jun 2021 09:30:48 +0000 (UTC) Received: by mail-pf1-f173.google.com with SMTP id k15so4537040pfp.6 for ; Thu, 17 Jun 2021 02:30:56 -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=lssK+06OrqYXMI/RYJk+NygLCQ6hQhXYMHdalKVel+U=; b=ApqW+b4wR3tXMa534EPaBXG3/a/Hp08qg47DiHJtK0mgfEzFsiG1Ao4n68XSbDDzxZ 9xXHIEqvn94lOxSRnTRB/+vsb0DyGcdYekj3LvN+7sN7No/pDmeLXun8PSncAf7BVTvs 3fWwD8exglZRS+hl5MRe5QjV1DWRuF2+sp7mc= 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=lssK+06OrqYXMI/RYJk+NygLCQ6hQhXYMHdalKVel+U=; b=HCnkft0qkSSIDb9Wicj3TEQuwOwb3abpQjHHi+w7NBhi3AHEg+bODfcRDTSzzbeReP LLQ4atLoxgCjc3xMaZGbK/ba+/b43qWrjPFxX501SSnfFWbB9BhEi3O3gYcY94D5Oepc 3io9E/ioeGQ++lX5ewGGiibFSwTTZR/hDlkEBs6aC0ggTiwpREh1GQineQg3xq+GIrhT Vhr0DVzqFwXRMB+uK+UCvBFly4O5ZOFdBBYcEXNmP75XyO+luHmoaaKLEfPYRLan+4cp fWx1h2BiS4Z/p3AxvolB2W2j1fD2yCtBCdIwESx0V+Gugu0W2qpr90emN/YKfdnF9tGv Rwqw== X-Gm-Message-State: AOAM532cXg9l++nrx2U2WSL2kswv/egfDBHBeoJeiIDdSK9+oB21O4mN vLxXjzrfagCSvJr/hC75KTB1Eg== X-Google-Smtp-Source: ABdhPJz6OUBNln7eU/91Mj+ScyWERfC5hrYLY7B9BZavgiJtBCoiFIgMEfAytW77+pj2ssxxHF0OIA== X-Received: by 2002:a63:db01:: with SMTP id e1mr4204540pgg.38.1623922255699; Thu, 17 Jun 2021 02:30:55 -0700 (PDT) Received: from localhost ([203.206.29.204]) by smtp.gmail.com with ESMTPSA id o14sm4847028pgk.82.2021.06.17.02.30.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 02:30:55 -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 v15 4/4] kasan: use MAX_PTRS_PER_* for early shadow tables Date: Thu, 17 Jun 2021 19:30:32 +1000 Message-Id: <20210617093032.103097-5-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210617093032.103097-1-dja@axtens.net> References: <20210617093032.103097-1-dja@axtens.net> MIME-Version: 1.0 Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=axtens.net header.s=google header.b=ApqW+b4w; spf=pass (imf17.hostedemail.com: domain of dja@axtens.net designates 209.85.210.173 as permitted sender) smtp.mailfrom=dja@axtens.net; dmarc=none X-Stat-Signature: wcn9fi57zb4qbgypejsfmimg8ifrrdgy X-Rspamd-Queue-Id: 9268B4202A0E X-Rspamd-Server: rspam06 X-HE-Tag: 1623922248-402892 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 Signed-off-by: Daniel Axtens Reviewed-by: Andrey Konovalov --- 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)