From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 643BEC3DA6E for ; Sun, 31 Dec 2023 06:21:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 577976B0194; Sun, 31 Dec 2023 01:21:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5266D6B0195; Sun, 31 Dec 2023 01:21:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4164D6B0196; Sun, 31 Dec 2023 01:21:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 32BF56B0194 for ; Sun, 31 Dec 2023 01:21:48 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 05719A041F for ; Sun, 31 Dec 2023 06:21:48 +0000 (UTC) X-FDA: 81626117496.24.A574127 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by imf21.hostedemail.com (Postfix) with ESMTP id 474381C000A for ; Sun, 31 Dec 2023 06:21:46 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; spf=pass (imf21.hostedemail.com: domain of alex@ghiti.fr designates 217.70.183.197 as permitted sender) smtp.mailfrom=alex@ghiti.fr; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1704003706; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=klENvFIXQJBHDgrZTUsznpLp3PTNfAHsalIrkiuCIpU=; b=3ChFzJuJgzTP/4Ky5SFzBQ/JIN22zRx/Udtlcx4FLtuz2T5McUc2q/YMhCmX8ZBCc4svib 3+AJVyBOvGyFBCXCTMBcYdNDnTdcPMVapuKXPCv+YbTetTJhIpi8RQebJbeM5DEFRI9OO1 epLZJ8/jXJ1++6bBODMDzqQFvX63CMY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704003706; a=rsa-sha256; cv=none; b=2VXhxtunm/f0pfFckfsF9pvxQD4OMxVOYBa+a9Y4BGF8tOfgPl2LCVZliBXTpGyRntSlG2 zPdPs0L5e1sBLD8ZGJxzajEBbgPuDhn4ziQ1POxhpzEX9Pb2YPbOhlAVtnYmXOfy1i4vwS tHq6nHM7snUTeA/zwER4yev4ifnNaU0= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; spf=pass (imf21.hostedemail.com: domain of alex@ghiti.fr designates 217.70.183.197 as permitted sender) smtp.mailfrom=alex@ghiti.fr; dmarc=none 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 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 X-Stat-Signature: m7bwy4gy3s6zgya5fio7kk18me6ncujq X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 474381C000A X-Rspam-User: X-HE-Tag: 1704003706-839278 X-HE-Meta: U2FsdGVkX19mBe/MSy0sl8T0UEOqKEZs6Rc7kbVC/IfFeosjtglcXZkX+ScKUHfprSj7T3y32gtuTNGIAkvi9cdIB4/yGYm5ZLmkYYQnRSVBnwKotNckm7BpmBqwGwAElGQzzRHajXsAvXU6/UJm1NHbM/jKOGYOuXUtbiTN54U7gpMOrc6MpOe2fhembulGAfPGG9SvAe/F5TtfdA9QFNVwV8c4izMbtXNfeyVcxmzLaxYsq9KmRzpbPAjXDpaH5LlFqFQSNykplbhDBLOwFdrpA2T8l/u2sBrGQTPRbcTEdMg1YP/j5aN3SS+eOROhgl+Gv9Rwg7O/Kq5SvGo1Pfb3vw2imj40q7eC5iQgkQ84OZfskgcty+LD8ycgnjewD3zVKbiietRQjoxssxxecif4h80ZhGybtaUB0SIMTBYXktbmMUTZauLzRtd41dQj8JcXc/gU8gySKoXlyDBZVHt32dlgHtMSskVNJVSAXNurkK9vaEZ9h6GJWdrFDdXx5SThuyuJf2XLMqDlifO14yGcrDO8fw1yh4aezdTYVK4i17y/rE9NpqXN5s9iK+nqnQfOg2bfRjh3BAnNYWNVGoqdIuOVbSl98v9LaoSZBhKHFZRVsyOh3pm6XKLEHD0jmp7Au9LqqxcgxHMUwUwDzGFzGyo8W6RtYYzR09VSdsmpOTVimfQmrjCE93HMD9GqI8RYds7JKxs2mX5qoE0H589BK24+/MTPlyf014O/AoTeyXdrlssemeLtd+G1mHk+Fa+sX123Gl1iUa64jX8Y45fUSBv7BtCMyuIhagoLzGbgA2Dxi96YATaOtUT6GQLxz/fboWbdkWw9vH3enjX+STidGhiREq4xvNfpptvSrUwt+iFP0BUD3dBuIrkiHYQ8 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: List-Subscribe: List-Unsubscribe: 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 */ >