Received: by 2002:a05:7412:2a8a:b0:fc:a2b0:25d7 with SMTP id u10csp140149rdh; Tue, 6 Feb 2024 23:28:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IEdgOStyX9XgnrahkCHDlRxDLrc9ZU6utKI4adRHVnQTNygw2eJKox+lX9YgfYc9yaqsbXs X-Received: by 2002:ad4:44ac:0:b0:68c:7946:2cb8 with SMTP id n12-20020ad444ac000000b0068c79462cb8mr18841073qvt.7.1707290894129; Tue, 06 Feb 2024 23:28:14 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707290894; cv=pass; d=google.com; s=arc-20160816; b=e4+3CzTWyzVDLtNOh2JsGTGgGdYPteL8YN2WXEkM/jEAiTGUFcMtSQ+KFZCES5Qle1 5M3ZXrrpNkmzEo0I/1ooaeRJmYxl6bhvTK/V5DwbXQAGPIsoEkElUdMyX2BJv8HilHsR dkOdU4iLzO1sg7vW/Dxt9E0amwRyew04uD0/PIpAJBBJkQvQAzJSJjtKo2VgFZmoUJAL 2RoAcMVGdPftKs4gENqRTx6/iZEX6tYYlt60dMZBgUMU706swJ8LkBd0AehF+8ptR4af jCHvyjU6MuXBsLUrtAZAvvkIH7i/sdNNQCi0mwSohsBGurjqadHuF2t9NUux5NQSUYFm QIFA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:references:cc:to:from :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=BaBlNAm33H6Vz9uH6TgncznSQoduFk0xqTMYqtgodLg=; fh=s0B2mNquPz+PYAJYV0+QL23V2m9roWLqv8Q8rVk3I0o=; b=MIQr3wz3MRd7sFtKwAHO/btvovgDlIfBh89DJd7BCq4Sbs29BD1EdDFzcS1914xVmh Vpd8brhFX1m4NKjSj6H4n7wvF1ElMjmngRPh12FcA/DHHnbaG/3gjxhvRyhWhP2eT2F0 hUqwbqOB1tCurlO95R5m8tJCWWRgDhtzLN6vs07W/ZplbwLqYALvtrOBEXdU63Q7l/m8 /Zrqo/TIb7vQctpShIsaPrMht7Ber1+bLL1DxLNXsNYLofeRzSDlQwHD3CQkvQm3dCyL M58UrybuNh/Uo2pNJAK4jD0nAZk/qSFJVPj1iP4YAjs2au4dlHnzSRv6MNJgi0riEYM/ 5nTA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=ghiti.fr); spf=pass (google.com: domain of linux-kernel+bounces-56071-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56071-linux.lists.archive=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=2; AJvYcCUh5vPaxWgqzbbNW3Zbqjoksh0XZDOn30TuX3db3Myn4sL2y4O7vrckVa87uiSeWBaASDZqwaEukE1VaNBrhQSdav0X8RrVZwwQKW5ajQ== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id t15-20020a05620a450f00b00784026af7aasi638696qkp.424.2024.02.06.23.28.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 23:28:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56071-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=ghiti.fr); spf=pass (google.com: domain of linux-kernel+bounces-56071-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56071-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 DDA541C24018 for ; Wed, 7 Feb 2024 07:28:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F12F8200CB; Wed, 7 Feb 2024 07:28:09 +0000 (UTC) Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) (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 D0694200BE; Wed, 7 Feb 2024 07:28:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707290889; cv=none; b=WlfoHBLVFnfGrCe3S6hC53tKdKy/oPibKlmPhtMbKWXoXswVCYPuph602TuyFWtHjGMmnOJgv58iQZqyc3LdDeZz/4hs0oIqA3ORgDmYc/NfcQPYBcn4wYRyrkJAJE9TwkdUwE1veo5pDT/IsMCC9BxZNhqsP2oFCQwcQoOsR+4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707290889; c=relaxed/simple; bh=v53BvGI6MATxvwyGvG+vzoCjgo7+/H08AEf8iaK8lok=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=DQsJN/yxRUP2NQuLu4OlpEHrg+c0rwEoEkpk02H7n1GiHLjvbmQBtQKP6DdQkdMG7dSgq5PtsmgTgOEUI9dGHMYHlLrdYL7xERCM5he/rsrnqmsrxtaANWQvn5Ce7IjAeMy9pCR/3AzbR9Uj3NwUgLD43Kqm5mmWhA/l7UdcQ0Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr; spf=pass smtp.mailfrom=ghiti.fr; arc=none smtp.client-ip=217.70.183.201 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 61C0D1BF204; Wed, 7 Feb 2024 07:28:01 +0000 (UTC) Message-ID: <6d03704c-e0b3-40e8-a0b1-6ad357a775d1@ghiti.fr> Date: Wed, 7 Feb 2024 08:28:01 +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 Subject: Re: [PATCH 1/4] riscv: tlb: fix __p*d_free_tlb() Content-Language: en-US From: Alexandre Ghiti 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> <08f55d3e-d68e-406a-9bc9-d62f3c86e949@ghiti.fr> In-Reply-To: <08f55d3e-d68e-406a-9bc9-d62f3c86e949@ghiti.fr> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-GND-Sasl: alex@ghiti.fr Hi Jisheng, On 31/12/2023 07:21, Alexandre Ghiti wrote: > 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(); I sent a patch for that here https://lore.kernel.org/linux-riscv/20240128120405.25876-1-alexghiti@rivosinc.com/ You can add: Reviewed-by: Alexandre Ghiti Thanks, Alex > > 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 */ >