From patchwork Mon Sep 7 13:40:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 11761035 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AD845746 for ; Mon, 7 Sep 2020 13:41:21 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6B4AF21775 for ; Mon, 7 Sep 2020 13:41:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="JLnpFrQD" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6B4AF21775 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id BED418E0003; Mon, 7 Sep 2020 09:41:18 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BC4038E0001; Mon, 7 Sep 2020 09:41:18 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A8A258E0003; Mon, 7 Sep 2020 09:41:18 -0400 (EDT) X-Original-To: linux-mm@kvack.org 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 9093D8E0001 for ; Mon, 7 Sep 2020 09:41:18 -0400 (EDT) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 4D6648248047 for ; Mon, 7 Sep 2020 13:41:18 +0000 (UTC) X-FDA: 77236377036.19.slip60_2d03e5e270cc Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin19.hostedemail.com (Postfix) with ESMTP id 206471AD1B4 for ; Mon, 7 Sep 2020 13:41:18 +0000 (UTC) X-Spam-Summary: 1,0,0,fb6d9f89a5bb9d5b,d41d8cd98f00b204,3fdhwxwukcecnu4n0pxxpun.lxvurw36-vvt4jlt.x0p@flex--elver.bounces.google.com,,RULES_HIT:41:152:355:379:541:800:960:973:988:989:1260:1277:1313:1314:1345:1359:1431:1437:1516:1518:1535:1543:1593:1594:1711:1730:1747:1777:1792:2194:2199:2393:2559:2562:2740:2897:3138:3139:3140:3141:3142:3152:3354:3865:3866:3867:3868:3870:3871:3872:4117:4250:4321:4605:5007:6119:6120:6261:6653:6742:6743:7901:9108:9969:10004:10400:11026:11232:11473:11657:11658:11914:12043:12048:12219:12291:12296:12297:12438:12555:12683:12895:12986:13141:13230:14181:14394:14659:14721:14819:21080:21444:21451:21627:21990:30003:30054:30074,0,RBL:209.85.219.74:@flex--elver.bounces.google.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100;04yr68rrrnxp3mn99attqqnfpyabeypk8aimrqp7z1keg86yeyh9edwxd5y3y79.mirkbjtb3d9k9peucbc9ujbha46e7w7u7xzykdwnhfqnz7jp6dfr4iph1t5c4nz.r-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF X-HE-Tag: slip60_2d03e5e270cc X-Filterd-Recvd-Size: 6971 Received: from mail-qv1-f74.google.com (mail-qv1-f74.google.com [209.85.219.74]) by imf03.hostedemail.com (Postfix) with ESMTP for ; Mon, 7 Sep 2020 13:41:17 +0000 (UTC) Received: by mail-qv1-f74.google.com with SMTP id y32so7640668qve.1 for ; Mon, 07 Sep 2020 06:41:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=jc0VYpbbwwHp06Vd4b4APbV9LFmrdZq9B0FcghhcNas=; b=JLnpFrQDFHHhtGxwd7cU7ERsS82nxZOuQ8YNkI2UfTvpbtnZ26+NTlgWOFFAb2LRAy aS1dc5kBprSvV8t8MGbx//5d7ru5muPH5hHlOPVmL2/Ajd24Wc0zxW//RtluwlFky1Kb 2UE9AbOazy+x+s728tQe2icQHfbNgYCeY6Ck15zQt0oW3OuWbgDF+ARKN9kHxs0cnr1c +bB51nd105vUY2uxWNS1Jp5hsFvwVNTO/MXr3S38P9Kr/T3hBQpqgmPMTpVcU36T7lrc tf2Cb754qEOSXnIUvscPliA2DNUhNvX/GDl9lt5tCVn0OIrB3HTBwewRFkOdEcuUpoev dzGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=jc0VYpbbwwHp06Vd4b4APbV9LFmrdZq9B0FcghhcNas=; b=Z+KxQUxbh52SyUFVnw9adr0nBjPzkaHmW9dIhceNK3/GS2XtXWvd7xkCw51+Hzueho aMTf59kCT4jWtKxHg8G1kZ8IFwDGokYmb3FuWvW9pb4KZZ9hQID4fcz0WKr6Ec0UI+US 7aWSU/X2+cFZbUK8MTrIeCkaRxVZk6Xlo7SX+bahoUzrsMz5L1yNGy696umuAlJNZ9V1 GnkJgy6i7S8qyJbgTuyNk8ZhivGtKZ2Pc8NfuezL9qFGR1NIRbFm4TvA/rF2jJvdWFVN SN/5HaLT241CV8HuvZ9osuv2ipsGBWGQbUpaPUbaFoJG6tDjqCVCmYaEWUIe991edwx2 uMTg== X-Gm-Message-State: AOAM530jtNGt3XZDv3MPxjVV6YwOo/BwxKXu6nkY22XRWjx3LIFGnxMZ MiPSc+pyZqSjRj7ow4rmGlzANAi6Kg== X-Google-Smtp-Source: ABdhPJxNUkuLAO9y2rbZdFgphW1Q+h54SGmHVNnG677jxAI1ltghY7irm/us6h8t84X9VnswP6n3DNuunA== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:f693:9fff:fef4:2449]) (user=elver job=sendgmr) by 2002:a05:6214:17ca:: with SMTP id cu10mr18279241qvb.6.1599486076914; Mon, 07 Sep 2020 06:41:16 -0700 (PDT) Date: Mon, 7 Sep 2020 15:40:48 +0200 In-Reply-To: <20200907134055.2878499-1-elver@google.com> Message-Id: <20200907134055.2878499-4-elver@google.com> Mime-Version: 1.0 References: <20200907134055.2878499-1-elver@google.com> X-Mailer: git-send-email 2.28.0.526.ge36021eeef-goog Subject: [PATCH RFC 03/10] arm64, kfence: enable KFENCE for ARM64 From: Marco Elver To: elver@google.com, glider@google.com, akpm@linux-foundation.org, catalin.marinas@arm.com, cl@linux.com, rientjes@google.com, iamjoonsoo.kim@lge.com, mark.rutland@arm.com, penberg@kernel.org Cc: hpa@zytor.com, paulmck@kernel.org, andreyknvl@google.com, aryabinin@virtuozzo.com, luto@kernel.org, bp@alien8.de, dave.hansen@linux.intel.com, dvyukov@google.com, edumazet@google.com, gregkh@linuxfoundation.org, mingo@redhat.com, jannh@google.com, corbet@lwn.net, keescook@chromium.org, peterz@infradead.org, cai@lca.pw, tglx@linutronix.de, will@kernel.org, x86@kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org X-Rspamd-Queue-Id: 206471AD1B4 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam04 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: Add architecture specific implementation details for KFENCE and enable KFENCE for the arm64 architecture. In particular, this implements the required interface in . Currently, the arm64 version does not yet use a statically allocated memory pool, at the cost of a pointer load for each is_kfence_address(). Co-developed-by: Alexander Potapenko Signed-off-by: Alexander Potapenko Signed-off-by: Marco Elver --- For ARM64, we would like to solicit feedback on what the best option is to obtain a constant address for __kfence_pool. One option is to declare a memory range in the memory layout to be dedicated to KFENCE (like is done for KASAN), however, it is unclear if this is the best available option. We would like to avoid touching the memory layout. --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/kfence.h | 39 +++++++++++++++++++++++++++++++++ arch/arm64/mm/fault.c | 4 ++++ 3 files changed, 44 insertions(+) create mode 100644 arch/arm64/include/asm/kfence.h diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 6d232837cbee..1acc6b2877c3 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -132,6 +132,7 @@ config ARM64 select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if !(ARM64_16K_PAGES && ARM64_VA_BITS_48) select HAVE_ARCH_KASAN_SW_TAGS if HAVE_ARCH_KASAN + select HAVE_ARCH_KFENCE if (!ARM64_16K_PAGES && !ARM64_64K_PAGES) select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT diff --git a/arch/arm64/include/asm/kfence.h b/arch/arm64/include/asm/kfence.h new file mode 100644 index 000000000000..608dde80e5ca --- /dev/null +++ b/arch/arm64/include/asm/kfence.h @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __ASM_KFENCE_H +#define __ASM_KFENCE_H + +#include +#include +#include + +#include + +#define KFENCE_SKIP_ARCH_FAULT_HANDLER "el1_sync" + +/* + * FIXME: Support HAVE_ARCH_KFENCE_STATIC_POOL: Use the statically allocated + * __kfence_pool, to avoid the extra pointer load for is_kfence_address(). By + * default, however, we do not have struct pages for static allocations. + */ + +static inline bool arch_kfence_initialize_pool(void) +{ + const unsigned int num_pages = ilog2(roundup_pow_of_two(KFENCE_POOL_SIZE / PAGE_SIZE)); + struct page *pages = alloc_pages(GFP_KERNEL, num_pages); + + if (!pages) + return false; + + __kfence_pool = page_address(pages); + return true; +} + +static inline bool kfence_protect_page(unsigned long addr, bool protect) +{ + set_memory_valid(addr, 1, !protect); + + return true; +} + +#endif /* __ASM_KFENCE_H */ diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index f07333e86c2f..d5b72ecbeeea 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -310,6 +311,9 @@ static void __do_kernel_fault(unsigned long addr, unsigned int esr, "Ignoring spurious kernel translation fault at virtual address %016lx\n", addr)) return; + if (kfence_handle_page_fault(addr)) + return; + if (is_el1_permission_fault(addr, esr, regs)) { if (esr & ESR_ELx_WNR) msg = "write to read-only memory";