Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp888150pxj; Sat, 8 May 2021 01:30:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz071+VG8azEmK+rFSpPahXHXSKja2NpTI66EXUcE6B7IF/TCZzSXG3Etl4IeATjaFhPkXh X-Received: by 2002:a62:6202:0:b029:208:f11c:2143 with SMTP id w2-20020a6262020000b0290208f11c2143mr14702585pfb.32.1620462655585; Sat, 08 May 2021 01:30:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620462655; cv=none; d=google.com; s=arc-20160816; b=NReM2p6+mGi1nsVVhnRti1WXbnv1NYQj+lN9bqTpvth2ax/f6rOFRSxLhh+ejJtNxb 3JU8AY8VJP49tFyvvf4JIykkbpioaqKvj3kRQojXhlEbSwM4ryAG5IIO5EYtF4XUMKgW JQ5OpDf8mPx3xO9pyW14WNKiXmBzAcg6VCVuA7zMpm4tT6Eqh/zfpap7PWpn3PXFsuTU QMW2IdVVMzsv99jStB16UnGUidX4DGWubLt8FGv84+gw0mZWXhV91uNFU/Xir7Y2WT7L V2ISiJAKW7bpCiIxYL19G6NXT5NmR6yTkZ1BTJlY9fHXnvqYl9Wvui/vYQeBGU4Pb7ZQ FW3Q== 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=QHWD9J6ofLEJEBPQ1owqvKysj/6TUwLubW0zYntQWn4=; b=M9BDBptNUtREJkdeQq12dJbWf8QJBKg366hXmFSBBGjYw7dbiS/eKqnNEjrFOYqMIN PsVy19UiyuC1BK1xyFb7IfHoFDAzton7W7ZLgUwY+Uo3PHLHezOjHyKF4AD1WWXxNupB oyp4IywSrvxnyZ/dYXzQjGgByzbRvtIYW6F/+r+EO2u56YlfQyRBRHWDTeLz8mm/o8yU TdbGjUgPpH8PUn5QuW4R7tAzK3vxpXJduWWo3YAzTHPs/OjWFNq8gGnSvtomyUs+bDLN LHWz2Dvm3XgziTlJ1P5O1MMRnIG/UHX4LFwSckCYfh5oqKBBlBNe32T6SgTqEZHu//00 QB3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=dZdBMLYg; 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 q7si687834pgb.70.2021.05.08.01.30.43; Sat, 08 May 2021 01:30:55 -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=dZdBMLYg; 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 S229947AbhEHIbL (ORCPT + 99 others); Sat, 8 May 2021 04:31:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229583AbhEHIbK (ORCPT ); Sat, 8 May 2021 04:31:10 -0400 Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C81AC061574 for ; Sat, 8 May 2021 01:30:09 -0700 (PDT) Received: by mail-ot1-x32e.google.com with SMTP id 36-20020a9d0ba70000b02902e0a0a8fe36so3696974oth.8 for ; Sat, 08 May 2021 01:30:09 -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=QHWD9J6ofLEJEBPQ1owqvKysj/6TUwLubW0zYntQWn4=; b=dZdBMLYgAigD1CqNxf6YBFHDAPSBKHRMkHGbzd3aLFVUccCVbaX2zvdoOIGP3gH4sQ Ua9OJC0czYfg/l4IvsLCzHBdz681/Vpe/4Ww2cVSjrSPv84/152BRCkcmbJZQeijqg+Q csm9TUMgBZ3IxQgHZkRP1buVQAdlmX2ARVeyiwbg8wIRHbS4L2WG0TUjU/7xSIb9u18t viwUnlZfEVTj/m2VEWC19lqt0BjhOaFif6xNuR0E2yub//sjCd0Z9ZCljcGBVcacSg1j ix9EVtmEtY0JBPinRwJOiL8WeBtMZZH+y9+3DcTuAe+cPMiY4iiohsnBgR1q4O3wtEsT 9+yQ== 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=QHWD9J6ofLEJEBPQ1owqvKysj/6TUwLubW0zYntQWn4=; b=Pj6pujhM8fvg6y+MURvvnPZKGyXazhlBkJa8m7XWglhkGhDjdKJOvghMZdoZJyXnp/ WluitcC4dujr7Wh4EPRHyb4Mp3EyfL9RGl6RZwvev64zaw0WAV84SZLLofjmlc3Id5yz 2GZuDZQH1C50Sb42IgKt/bCGv4z2cYYiGAm0yQBo0pTBNaJEC4MxgUhLN92Oua15YiOk KlsFqZKYfizUKbudPIoHpV+FZphLrvAuWplsxjze63xPnAMSvlHSt69AvSChrEWBGeHi tZ0veVYV7LpBHVwZNoaaPcVH0b+Mu0Bgst7JrhdqbkUzHEwtwFZP2V+t2QgbSHVIGU5d JWtA== X-Gm-Message-State: AOAM530Dn4F9hVKni1pGZzzdfP58B39+RWs9johtzAFO74+v/onUO1xZ 8yRI8feeZZzm2TMOgwAv0h1PhgfbPHJlFHMXiLUhCQ== X-Received: by 2002:a9d:1ea9:: with SMTP id n38mr12276751otn.233.1620462608813; Sat, 08 May 2021 01:30:08 -0700 (PDT) MIME-Version: 1.0 References: <20210508032912.2693212-1-liushixin2@huawei.com> In-Reply-To: <20210508032912.2693212-1-liushixin2@huawei.com> From: Marco Elver Date: Sat, 8 May 2021 10:29:56 +0200 Message-ID: Subject: Re: [RFC] 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 Sat, 8 May 2021 at 04:56, 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 . Nice to see KFENCE on more architectures. > 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 [...] > diff --git a/arch/riscv/include/asm/kfence.h b/arch/riscv/include/asm/kfence.h > new file mode 100644 > index 000000000000..590c5b7e3514 > --- /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 = kmalloc(PAGE_SIZE, GFP_ATOMIC); Using kmalloc() to allocate pte looks weird. Does riscv have helpers for allocating pte? Otherwise, __get_free_page() perhaps? > + 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; > +} [...]