From patchwork Mon Sep 21 13:26:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 11789853 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 81E52112C for ; Mon, 21 Sep 2020 13:26:48 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4823621D95 for ; Mon, 21 Sep 2020 13:26:48 +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="CTsRusC0" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4823621D95 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 C9EE890005F; Mon, 21 Sep 2020 09:26:45 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C289F900059; Mon, 21 Sep 2020 09:26:45 -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 A7AFC90005F; Mon, 21 Sep 2020 09:26:45 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0126.hostedemail.com [216.40.44.126]) by kanga.kvack.org (Postfix) with ESMTP id 7FF33900059 for ; Mon, 21 Sep 2020 09:26:45 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 43D91180AD801 for ; Mon, 21 Sep 2020 13:26:45 +0000 (UTC) X-FDA: 77287143570.12.snake63_0c1866127145 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin12.hostedemail.com (Postfix) with ESMTP id 193251800CFE3 for ; Mon, 21 Sep 2020 13:26:45 +0000 (UTC) X-Spam-Summary: 1,0,0,9dfcc0392b8d5b75,d41d8cd98f00b204,3e6poxwukcawqx7q3s00sxq.o0yxuz69-yyw7mow.03s@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:4118:4250:4321:4605:5007:6119:6261:6653:6742:6743:9108:9969:10004:10400:11026:11232:11473:11657:11658:11914:12043: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.160.202:@flex--elver.bounces.google.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100;04yrbd8ajyegjaeu7saa7ikx118qxopk8aimrqp7z1keg86yeyursidzk1gty79.mirkbjtb3d9k9peucbc9ujbhahe9ncn3q7bykdwnhfqnz7jp6dfr4iph1t5c4nz.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:fp,MSB L:0,DNSB X-HE-Tag: snake63_0c1866127145 X-Filterd-Recvd-Size: 7093 Received: from mail-qt1-f202.google.com (mail-qt1-f202.google.com [209.85.160.202]) by imf24.hostedemail.com (Postfix) with ESMTP for ; Mon, 21 Sep 2020 13:26:44 +0000 (UTC) Received: by mail-qt1-f202.google.com with SMTP id p43so12768869qtb.23 for ; Mon, 21 Sep 2020 06:26:44 -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=Ix66qFhKSgSVrZqQyJftp2TjE15R0Twg+sH9CEm53D4=; b=CTsRusC0VpfrH6AP4frLdYd+UeeyjPQTPYZLk3YPw3dPpyo7SVxAO26BpWE11ZWSPQ uSC4T0QWzrWUS2WxaRekysQJlBp37jl2hGNZJyQiHllh7tx90+s6iCZvxCt/Hw4sdLkj u2SP6GCtU9pcT3lGTbYBmTyFmj+ooqG0galP6OV/pxSrwP3s+/NDH1w5Y247XTomei5m PCP6mD1gKGwqIwMDIO6OhwY/Qq2TJK2Avxj5GH0yF9w8VlxUxiPzKbIM1b2V2nh3NnuU KMg8ZakQBuHdW6HsF2NcpHVedeue1I4YGmMMVdgF+74bp98dlDO3TxRI3rmhxBWz3k/X Z81g== 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=Ix66qFhKSgSVrZqQyJftp2TjE15R0Twg+sH9CEm53D4=; b=uQen3LaCLmNDCD20J6vUPYv3DMyqxrvJwoXdi38VF4ahUVVDwbGint1NxhJnHD+hN5 JOUdio2Yts2ps8kouncTZdjrF7DbRdH9X4h3hQdKpHoD5ig9HuNfwPPrcTeqVTVw7k8A bi/CJk1vD4698w3xCJLZhTMeBfLSGz/X9E0rfJ/n5IumG6OJE6/dB/EtbaU/gy6LPToi kJEkLvf2mDMB2NKGSIZPBGdN3byZ8gqAzDF8vSclC+3hPe1CsvjPIvOekuPl2DeYsBE4 /aqkNs48tozh88qW+tBfZ1E95nn6M7e5326OI6w8ypcd/vNgLvpjoSPQAxacLbNUq1FT iZrA== X-Gm-Message-State: AOAM5311PsrtkMLIoTW8GwJWkFQyU+q81cD7Q28hQZ50PuOU0UaWvgee +0S8nmO6xyiDsiXpg4PF9/+r/pCRfw== X-Google-Smtp-Source: ABdhPJxr8iWU9tcA8f9QiW5hksbb0r4GfT178XXuYvyfsn71Bb1lS4SgSL5HWA6YlCdJxbWk836ueVS2jg== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:f693:9fff:fef4:2449]) (user=elver job=sendgmr) by 2002:ad4:4b34:: with SMTP id s20mr30490550qvw.51.1600694803653; Mon, 21 Sep 2020 06:26:43 -0700 (PDT) Date: Mon, 21 Sep 2020 15:26:04 +0200 In-Reply-To: <20200921132611.1700350-1-elver@google.com> Message-Id: <20200921132611.1700350-4-elver@google.com> Mime-Version: 1.0 References: <20200921132611.1700350-1-elver@google.com> X-Mailer: git-send-email 2.28.0.681.g6f77f65b4e-goog Subject: [PATCH v3 03/10] arm64, kfence: enable KFENCE for ARM64 From: Marco Elver To: elver@google.com, akpm@linux-foundation.org, glider@google.com Cc: hpa@zytor.com, paulmck@kernel.org, andreyknvl@google.com, aryabinin@virtuozzo.com, luto@kernel.org, bp@alien8.de, catalin.marinas@arm.com, cl@linux.com, dave.hansen@linux.intel.com, rientjes@google.com, dvyukov@google.com, edumazet@google.com, gregkh@linuxfoundation.org, hdanton@sina.com, mingo@redhat.com, jannh@google.com, Jonathan.Cameron@huawei.com, corbet@lwn.net, iamjoonsoo.kim@lge.com, keescook@chromium.org, mark.rutland@arm.com, penberg@kernel.org, peterz@infradead.org, sjpark@amazon.com, tglx@linutronix.de, vbabka@suse.cz, 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-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(). Reviewed-by: Dmitry Vyukov 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";