Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3801126pxk; Tue, 29 Sep 2020 06:40:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwiF4L3LCnA/1KC0o/RRIPYVoIQ1MiYFk91PEuSsI9ahXOV531xXGoWUSrXdtnhYQSDFarM X-Received: by 2002:a05:6402:13d3:: with SMTP id a19mr3194363edx.255.1601386842429; Tue, 29 Sep 2020 06:40:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601386842; cv=none; d=google.com; s=arc-20160816; b=zXB4/ZQd5qsRINyyCa9r4gH4eW/uohIUObuMjJ6YDY3Veo2u7bEzoJWACa1PRXhs6L N7vDl9wLS2eWoPO0nQnzz0Wunuenv6/sHowcca+YdlWoFdWPG5R2e7IJSnpo012En1bd 05SwxILGrUi/BgwniK9zIAbxouyKNQRERoABwpGbMvLu5Xsm7EcSWOuwjX/t1M4oX2XW YBTs0oBDZRX7nMbSdyhhLVXNppnErMAPFN/D6Isy//Faj9WsCMQiViyhr4A6kCTOT/WK UcjfUuT/D9d/9HEcvPYifHsH/M8qA3641VaLV89VOOJD2HKZPeRz2IU5clAjrRe2gEr2 6Pgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=qLrQI/JwUn36fq2luHZslhJ1uvvVQDF6ByQHZzaen98=; b=Qjdmi2jt+iqlIUdHbtfKDaDkBVWBUjMFnMgLOtmEZLfWCHhvsyonvFcnJrN7OHNu8o RkGCM8DnntUQ91fcZNok2YT9q3XL0UfzGh1mg/crdesu2xirr5aQ+xZjsvBwbgBI0oGn OW5hOmC0jgEthp1tDwoA62HcJTcZlHzbJoVCZqSTQr5s6ooGuun78b676FTD5C1vpycS Q2mlEAxG2+9bouaBNqbLWTF+bi0VQQ8KcILDu3IrDliK7Rytvvse5qWrx2YNq8LgF7lZ hG1/HFyiZxRHA7Od9HebOMDXNAsqb7LjpTMSJzuhbqDX8IN1U6/i7zdgdMMU0CnAOPkR 9b8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=M3qvQTHw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lz22si768300ejb.71.2020.09.29.06.40.19; Tue, 29 Sep 2020 06:40:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=M3qvQTHw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730082AbgI2NjB (ORCPT + 99 others); Tue, 29 Sep 2020 09:39:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729958AbgI2Niu (ORCPT ); Tue, 29 Sep 2020 09:38:50 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F63FC0613D2 for ; Tue, 29 Sep 2020 06:38:50 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id n13so4755975ybk.9 for ; Tue, 29 Sep 2020 06:38:50 -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=qLrQI/JwUn36fq2luHZslhJ1uvvVQDF6ByQHZzaen98=; b=M3qvQTHw0ChvKUDYIO9LGJMJDfrv76Yw8g+RTUrAtNznvcAfD2rNKtYxQCCf6YpkCV KT2a+CF4xytL226EtBBFX+zWfmy6mjJY7Yhc3WmwpNwAxkQpunDayb+K/6/NKXNbC7Cv gOCHoEvMf/MUgk/yXMKSXiSoPSOlCkzV3VCyY4NgyjxYu0uS6yM5MYPAFgFE1Yi+sian yIvxBARbczR0xEUgdyOldIId00Qp9rMOFHn0ft4BZq/dQCdadtZX06TDuIUnecdRClHt G/ryNX2lwtSzl1gZwcei/FmTQBRkkNroTemj4ThZCJmTZnOhqG5qW/bFkXEH6QtHmmml mhcA== 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=qLrQI/JwUn36fq2luHZslhJ1uvvVQDF6ByQHZzaen98=; b=gvCnek6cajkPyzZYS+pwxzuF0lYh17yJg/zBhhnyCQGEpizPROkE/Xe4ipuVc6QKs8 k/WSFmnrJaGaJPwnqKCosN9XOzlb+sA4+qp9YJwnwWdGUKpb0UYEiDbPGfvr92WAemyx +FZ0JYpTrYge3QN0Xz+cwwts9U/Fn8+3neN5qsvNQtkmF+0BXe0a8j/M7FSNrxklRTzk /NGXHkD4wiLFB+VHqwP9AUWjkuiKhhq+RKVhQ+7/L78hZs1l1xKSxLzQ4s0X7Lw143dK +Fb1c8X0DYlytpeSyA1FMSxjE/zByRcjt8Cnoq5OHbULFmUwrRJ0u6nqFMuZlbcZRH1H 7Q1Q== X-Gm-Message-State: AOAM5320Th0fx5iwr+UvJoK7Q541mJG0XwgfoJUB7pYJbP/cshQ5Gfnu TK5AA7DliupH2uFGpl1ghgOMlwDcSw== Sender: "elver via sendgmr" X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:f693:9fff:fef4:2449]) (user=elver job=sendgmr) by 2002:a5b:d43:: with SMTP id f3mr5632739ybr.46.1601386729429; Tue, 29 Sep 2020 06:38:49 -0700 (PDT) Date: Tue, 29 Sep 2020 15:38:06 +0200 In-Reply-To: <20200929133814.2834621-1-elver@google.com> Message-Id: <20200929133814.2834621-4-elver@google.com> Mime-Version: 1.0 References: <20200929133814.2834621-1-elver@google.com> X-Mailer: git-send-email 2.28.0.709.gb0816b6eb0-goog Subject: [PATCH v4 03/11] 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 Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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"; -- 2.28.0.709.gb0816b6eb0-goog