Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp663945pxj; Fri, 14 May 2021 12:29:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw0/pSYYeahu/o6dTdUw+9UFRzpl0pPEM9xB8hvdnuOsnx0nMfG2MQXHnBWsRFdt8t6db+L X-Received: by 2002:a05:6402:14c1:: with SMTP id f1mr15026538edx.334.1621020562989; Fri, 14 May 2021 12:29:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621020562; cv=none; d=google.com; s=arc-20160816; b=XZD4zR/Jo7QhAG1o3JBXUgRyVECqWX4opFcROSMBLZlWeejJDAaJz+yIC03WHhD5l1 0/0z2167YDhxJMx/iDbdvzsyykmLQc7nBDypvYDqAAZbVR2QSPz3O70silEmmCIlaYPy rjPpz2Arq93WOBrAao9886M8FwxxeUHQauT29V8wUQOhWI09TdG/FmM12JURGZxFks8O WK502czp2GStZQKkqbScVjkAOJasBU8pD6i1SA7N14DLuY85x3RrIE+VhBhEJvZ5r+/j 6wfM9s7ccmAo+W3Pn+fKsldZ21NQyXUv5PaKv0PKBmP4KNoA16Jo5W+Hhy9WPiR50har /ivw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=OUtJdEoTp2E5reOLxrwFg0FEHgUh9mJq9am3px70vxo=; b=NN8u4XODnWSwb615akWib2yVZ9RscubJxvK+Q0oBTDzR4Q8pHoJWGr1D/YZldboV/H Fv3HVyEgQtp9h/AXBbKkIIUa23FFxI4kjU6Plhl71vcK1UVHRylU2RX0bvSSbQuF+URX GICrWyvsCheQq27wfE24lW5nX/RC72/HcDcvEXJ1ktFAyCRiRbyG4fL4XH6dJXHjYKcR v1UOKoaf7tpbw9ALdT3KM/tTZ6sc/Q3kLJA3g2jRZGi374SY7S9hJxPQzWbCNrj84ifl vnQH4H7t3q8C1M3m683KHRCrJrs56GTJiC7jb4p34/j5HufvZL/wFj6XMX6jn3zLBhk5 rarQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ExnpX06y; 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 dn27si336257edb.326.2021.05.14.12.28.59; Fri, 14 May 2021 12:29:22 -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=ExnpX06y; 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 S233593AbhENPVf (ORCPT + 99 others); Fri, 14 May 2021 11:21:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232430AbhENPVe (ORCPT ); Fri, 14 May 2021 11:21:34 -0400 Received: from mail-ot1-x329.google.com (mail-ot1-x329.google.com [IPv6:2607:f8b0:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 753B2C061574 for ; Fri, 14 May 2021 08:20:23 -0700 (PDT) Received: by mail-ot1-x329.google.com with SMTP id d3-20020a9d29030000b029027e8019067fso26674289otb.13 for ; Fri, 14 May 2021 08:20:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=OUtJdEoTp2E5reOLxrwFg0FEHgUh9mJq9am3px70vxo=; b=ExnpX06y3XJ21mt9usdh2O3KZNtrIa6RUkQ9fHy94A4+viP5UjetMSS6qHA1EYo66k i3ygvXWEryAT4ug2cUz7O5I3cRN9h85yc1iA8P5WpI+TywttwjJp3dTbVi8ziigv3mmW RWYFEH9bVv8rraoGl0YqpfkswuWNysT0tqf8F+1IpVJy+ixAxk0vvyN4SZM4QCTF6tqX ZCXiPDZ4L4Av5adpYkwF6OktOOEZe6r85PFRplgCSAkJRlaDn9+73axA7LVTUrlM2q+k 71YSAy7s63SKfNcb0NqTsSXlO8xqBdvfbip66DSOd+pc2EKVfr0iMSNC2mi38uzwiFl8 eYxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=OUtJdEoTp2E5reOLxrwFg0FEHgUh9mJq9am3px70vxo=; b=lDuWpB3qatsPVVth/9DpTl9oOV9VUJLXw7NIMplToLH7EUQKjAb7bVrfqFXQx7ickj b2xHEPr1YF1r+ClgJeAyExqJmxToZy7ZbmQWlgcrnXF/FW70OtYxOBfYCEck8m4jc1CH LdzhciTUZtUA2eYkhuJbh6YuWUtCKEX2vi+Mjtq0XVORLsV5XMtecClPPhpzIEZ8isV+ gz9/i3o6cL9MPi1DpQmBtZL3O4/24SlAhzAsn6fypKe+/d6kfxWwyGXI3ENNAx73oaIW sU9eA05QfO1h6a06argjndOc2910flGC9VA/s4QTHarD2LS3K3G3O8L7Urs03uAjtXXh 9nbQ== X-Gm-Message-State: AOAM531vZ2eBK6Z8ZiFlDA72dQ9OWMR9lSS6sY9anMarx76mFEoxfVc4 SzcYkYu/A2usDJ69drG5x7VaAIUGPL72GOcLM6vlEw== X-Received: by 2002:a9d:7a54:: with SMTP id z20mr25363513otm.17.1621005622590; Fri, 14 May 2021 08:20:22 -0700 (PDT) MIME-Version: 1.0 References: <20210514034432.2004082-1-liushixin2@huawei.com> In-Reply-To: <20210514034432.2004082-1-liushixin2@huawei.com> From: Marco Elver Date: Fri, 14 May 2021 17:20:10 +0200 Message-ID: Subject: Re: [PATCH RFC v2] riscv: Enable KFENCE for riscv64 To: Liu Shixin Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexander Potapenko , Dmitry Vyukov , linux-riscv@lists.infradead.org, LKML , kasan-dev Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 14 May 2021 at 05:11, Liu Shixin wrote: > Add architecture specific implementation details for KFENCE and enable > KFENCE for the riscv64 architecture. In particular, this implements the > required interface in . > > KFENCE requires that attributes for pages from its memory pool can > individually be set. Therefore, force the kfence pool to be mapped at > page granularity. > > I tested this patch using the testcases in kfence_test.c and all passed. > > Signed-off-by: Liu Shixin Acked-by: Marco Elver > --- > v1->v2: Change kmalloc() to pte_alloc_one_kernel() for allocating pte. > > arch/riscv/Kconfig | 1 + > arch/riscv/include/asm/kfence.h | 51 +++++++++++++++++++++++++++++++++ > arch/riscv/mm/fault.c | 11 ++++++- > 3 files changed, 62 insertions(+), 1 deletion(-) > create mode 100644 arch/riscv/include/asm/kfence.h > > diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig > index c426e7d20907..000d8aba1030 100644 > --- a/arch/riscv/Kconfig > +++ b/arch/riscv/Kconfig > @@ -64,6 +64,7 @@ config RISCV > select HAVE_ARCH_JUMP_LABEL_RELATIVE > select HAVE_ARCH_KASAN if MMU && 64BIT > select HAVE_ARCH_KASAN_VMALLOC if MMU && 64BIT > + select HAVE_ARCH_KFENCE if MMU && 64BIT > select HAVE_ARCH_KGDB > select HAVE_ARCH_KGDB_QXFER_PKT > select HAVE_ARCH_MMAP_RND_BITS if MMU > diff --git a/arch/riscv/include/asm/kfence.h b/arch/riscv/include/asm/kfence.h > new file mode 100644 > index 000000000000..c25d67e0b8ba > --- /dev/null > +++ b/arch/riscv/include/asm/kfence.h > @@ -0,0 +1,51 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > + > +#ifndef _ASM_RISCV_KFENCE_H > +#define _ASM_RISCV_KFENCE_H > + > +#include > +#include > +#include > +#include > + > +static inline bool arch_kfence_init_pool(void) > +{ > + int i; > + unsigned long addr; > + pte_t *pte; > + pmd_t *pmd; > + > + for (addr = (unsigned long)__kfence_pool; is_kfence_address((void *)addr); > + addr += PAGE_SIZE) { > + pte = virt_to_kpte(addr); > + pmd = pmd_off_k(addr); > + > + if (!pmd_leaf(*pmd) && pte_present(*pte)) > + continue; > + > + pte = pte_alloc_one_kernel(&init_mm); > + for (i = 0; i < PTRS_PER_PTE; i++) > + set_pte(pte + i, pfn_pte(PFN_DOWN(__pa((addr & PMD_MASK) + i * PAGE_SIZE)), PAGE_KERNEL)); > + > + set_pmd(pmd, pfn_pmd(PFN_DOWN(__pa(pte)), PAGE_TABLE)); > + flush_tlb_kernel_range(addr, addr + PMD_SIZE); > + } > + > + return true; > +} > + > +static inline bool kfence_protect_page(unsigned long addr, bool protect) > +{ > + pte_t *pte = virt_to_kpte(addr); > + > + if (protect) > + set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT)); > + else > + set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT)); > + > + flush_tlb_kernel_range(addr, addr + PAGE_SIZE); > + > + return true; > +} > + > +#endif /* _ASM_RISCV_KFENCE_H */ > diff --git a/arch/riscv/mm/fault.c b/arch/riscv/mm/fault.c > index 096463cc6fff..aa08dd2f8fae 100644 > --- a/arch/riscv/mm/fault.c > +++ b/arch/riscv/mm/fault.c > @@ -14,6 +14,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -45,7 +46,15 @@ static inline void no_context(struct pt_regs *regs, unsigned long addr) > * Oops. The kernel tried to access some bad page. We'll have to > * terminate things with extreme prejudice. > */ > - msg = (addr < PAGE_SIZE) ? "NULL pointer dereference" : "paging request"; > + if (addr < PAGE_SIZE) > + msg = "NULL pointer dereference"; > + else { > + if (kfence_handle_page_fault(addr, regs->cause == EXC_STORE_PAGE_FAULT, regs)) > + return; > + > + msg = "paging request"; > + } > + > die_kernel_fault(msg, addr, regs); > } > > -- > 2.18.0.huawei.25 >