Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp5401281rdb; Sat, 30 Dec 2023 22:22:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IFN7bAd+RTvHF5xDWRTOL4QAjby9PdeJVlQdINViHMqUxYIuuuEpSfenHE5e8JN6tCYvHbh X-Received: by 2002:a05:622a:3ce:b0:425:4043:2a0c with SMTP id k14-20020a05622a03ce00b0042540432a0cmr15797706qtx.135.1704003723547; Sat, 30 Dec 2023 22:22:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704003723; cv=none; d=google.com; s=arc-20160816; b=W9/xlu0NmhdPUnaDonkxn1pi1oGHEE451qyDa2Rq/KziueUuubzfzRG6yiiGlufYbp xA2fjgjSTcWjKKnp9PD437u/IzggiTQAHyb35UkK7t4GGIVL/h0Xue2sqDJZO8gdLZog muguqrh2k3ay5o9qvVe1dwMA2PCN+CH9C+hFWZTgLD36ILpn4Iigsn9vP6NS/57C/kHz EgraSBDkqt+k7rKxQ0fSn4uFHotz6pg93un6lDQp+PGPyH2ZWs4EQp76j7C8Mp+c9rqq SBDv1NAzGb/voxEPZFGDLA4iSCd4cN6ffYG7ZAs189Ou8vPzaKjlYAAQ7G1mts3bvNJL ftzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:subject:from:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=klENvFIXQJBHDgrZTUsznpLp3PTNfAHsalIrkiuCIpU=; fh=DrC11EGBAUqo91sbESjPcJV1gpWnPt8reyQoR9d6Q9Q=; b=DhsZNDWo9wpftQzp/32kCd1/IKPLkUkhAxpieDOsIeW9phj5BGyyuUeyGzyw2bQI/+ BszFlJuj5Q5aP3ng9vU6Y+GHE6NmB2Q1opCdCgUtmi59BUTg/8wygIsKQrKdGsIs5G/u BQxOZn5GyaUFXkCPefozom417djiaI1B5S4r39uTisQxx0s96F3Wp9MB4J+975aN2VrS DnrXMqKOfDxmNS7Ka8jpW8o2eekJ+Xs/o14xF06xuwQOn0QYVAs2BlYy7g+FLJ5SAWWj beMH9CYhcnw796dJcAXJMRek6Dr6NxoX5sDrIAQ1Vjax+fFx0uhUylbJdeBX0ucNE5Tc BI9Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-13642-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13642-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id x19-20020ac85f13000000b004257ba71047si22272212qta.470.2023.12.30.22.22.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Dec 2023 22:22:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-13642-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-13642-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13642-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 4E2CE1C20C2F for ; Sun, 31 Dec 2023 06:22:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C6DDF20F2; Sun, 31 Dec 2023 06:21:54 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE66C185D; Sun, 31 Dec 2023 06:21:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ghiti.fr Received: by mail.gandi.net (Postfix) with ESMTPSA id 0AFF71C0002; Sun, 31 Dec 2023 06:21:38 +0000 (UTC) Message-ID: <08f55d3e-d68e-406a-9bc9-d62f3c86e949@ghiti.fr> Date: Sun, 31 Dec 2023 07:21:38 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Alexandre Ghiti Subject: Re: [PATCH 1/4] riscv: tlb: fix __p*d_free_tlb() To: Jisheng Zhang , Paul Walmsley , Palmer Dabbelt , Albert Ou , Will Deacon , "Aneesh Kumar K . V" , Andrew Morton , Nick Piggin , Peter Zijlstra Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org References: <20231219175046.2496-1-jszhang@kernel.org> <20231219175046.2496-2-jszhang@kernel.org> Content-Language: en-US In-Reply-To: <20231219175046.2496-2-jszhang@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-GND-Sasl: alex@ghiti.fr Hi Jisheng, On 19/12/2023 18:50, Jisheng Zhang wrote: > If non-leaf PTEs I.E pmd, pud or p4d is modified, a sfence.vma is > a must for safe, imagine if an implementation caches the non-leaf > translation in TLB, although I didn't meet this HW so far, but it's > possible in theory. > > Signed-off-by: Jisheng Zhang > --- > arch/riscv/include/asm/pgalloc.h | 20 +++++++++++++++++--- > 1 file changed, 17 insertions(+), 3 deletions(-) > > diff --git a/arch/riscv/include/asm/pgalloc.h b/arch/riscv/include/asm/pgalloc.h > index d169a4f41a2e..a12fb83fa1f5 100644 > --- a/arch/riscv/include/asm/pgalloc.h > +++ b/arch/riscv/include/asm/pgalloc.h > @@ -95,7 +95,13 @@ static inline void pud_free(struct mm_struct *mm, pud_t *pud) > __pud_free(mm, pud); > } > > -#define __pud_free_tlb(tlb, pud, addr) pud_free((tlb)->mm, pud) > +#define __pud_free_tlb(tlb, pud, addr) \ > +do { \ > + if (pgtable_l4_enabled) { \ > + pagetable_pud_dtor(virt_to_ptdesc(pud)); \ > + tlb_remove_page_ptdesc((tlb), virt_to_ptdesc(pud)); \ The specification indeed states that an sfence.vma must be emitted after a page directory modification. Your change is not enough though since eventually tlb_flush() is called and in this function we should add: if (tlb->freed_tables)     tlb_flush_mm(); otherwise we are not guaranteed that a "global" sfence.vma is called. Would you be able to benchmark this change and see the performance impact? Thanks, Alex > + } \ > +} while (0) > > #define p4d_alloc_one p4d_alloc_one > static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long addr) > @@ -124,7 +130,11 @@ static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d) > __p4d_free(mm, p4d); > } > > -#define __p4d_free_tlb(tlb, p4d, addr) p4d_free((tlb)->mm, p4d) > +#define __p4d_free_tlb(tlb, p4d, addr) \ > +do { \ > + if (pgtable_l5_enabled) \ > + tlb_remove_page_ptdesc((tlb), virt_to_ptdesc(p4d)); \ > +} while (0) > #endif /* __PAGETABLE_PMD_FOLDED */ > > static inline void sync_kernel_mappings(pgd_t *pgd) > @@ -149,7 +159,11 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm) > > #ifndef __PAGETABLE_PMD_FOLDED > > -#define __pmd_free_tlb(tlb, pmd, addr) pmd_free((tlb)->mm, pmd) > +#define __pmd_free_tlb(tlb, pmd, addr) \ > +do { \ > + pagetable_pmd_dtor(virt_to_ptdesc(pmd)); \ > + tlb_remove_page_ptdesc((tlb), virt_to_ptdesc(pmd)); \ > +} while (0) > > #endif /* __PAGETABLE_PMD_FOLDED */ >