Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp2375797rdb; Fri, 8 Dec 2023 06:30:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IHSRfCwlisA2j/ofGwUecIvwdQ7/eghiD96mNlEUJfU3bv8CGhrWcwdeu1fuXUQzfbXUk7P X-Received: by 2002:a05:6e02:1445:b0:35d:5995:798e with SMTP id p5-20020a056e02144500b0035d5995798emr339708ilo.40.1702045835019; Fri, 08 Dec 2023 06:30:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702045834; cv=none; d=google.com; s=arc-20160816; b=XPMNAIQ7NIl4QWYEGx7wdqscLSNmZqDw4k2gbCE6J6dw3jkFcq7I8d2aj2V2ZFsVwx 47M9bZvcGlOTpl516/e+ovy6u9ZXWYor3+qxGWmWpmNLTRLcEyFdUbrAJrZieHTn+KVY V5svPiIgazgVUJq0aMnNQufyMZlU5yzF0KXtSj42H0Mps1jMsALQVbGy4BEYcNbCsCpc FXNBuyjsJdcY9JoK4THBWHF65xfq4A44B4ft8rimRf6tXQP0cxrRoZVlAIQl2waiUqL1 XgdgjxZBA1sRG8nxLR8ZMWcfSVHLlSAoM06b8j2pGlcwf0cGo9JAJaNlYW9OIl44RhXQ 8jcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=9X8HQehrxqqdotEA60rYgF/5Z0BWv62JqBcRTG61sQ8=; fh=LZ/kPJHwdpSujTRklo57y4hLM6TNFfUligE0HScPCOQ=; b=tgUjK/r3eUtm/g6ncAsVfjZYUm3d3eEQ8B2xlPUBF1+PrRAv1ld59igyeK8TECND11 iXvB0TS48nNFumnzQbdYJk5RVOxxxv+AkpCbp21vPDPWSPsVxzacQq1GTp5EwjBn10md pBWd6si0na/H81coHVQipC/CiIXeB6lyPnER8E27kCr44lE6Vi0La8dzmRMudfDWmVhG 2i9SpnL88tCIXBB78I4DQaZDmalGKzcocL545yp6HS6vwYU0/xYoCuFTz7BgUDYcCmMp xdbXgAgJRchbLJ7Xa2JpvdrUibzrT9jQ5OrYyB4GEIFF81IPSNKL+buvlzh2sS6LvCWA FYvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ae9T3e1C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id b13-20020a630c0d000000b005bd66bfc253si1645959pgl.148.2023.12.08.06.30.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 06:30:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ae9T3e1C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id D6533805BC95; Fri, 8 Dec 2023 06:30:33 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1573955AbjLHOaY (ORCPT + 99 others); Fri, 8 Dec 2023 09:30:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1573929AbjLHOaW (ORCPT ); Fri, 8 Dec 2023 09:30:22 -0500 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 632481723 for ; Fri, 8 Dec 2023 06:30:28 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-40c0fc1cf3dso22567195e9.0 for ; Fri, 08 Dec 2023 06:30:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1702045827; x=1702650627; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=9X8HQehrxqqdotEA60rYgF/5Z0BWv62JqBcRTG61sQ8=; b=ae9T3e1Cne24CyqIheMbKwefld08kr8LfEtB9Jb2ladRXJ/zge8FEhm2SmgyMlCD80 xpFDAG6RVeK43Jx58jYD+RWuZUj3qrIG3bLYc6aeP9kv9OnHxk7XTG71mJmMD8Ak/k81 fUfXdfkyFn6Ltylt3DI2vCjZd5kIYEOZgvzuL503y5mBNLCwZnf9QdmhkDQFL4yHcvpJ LpUXbgoYRejeBxH4+1/hEVPbIQpQR4XVh6aC/uL4H4DEKq7rskTVvdlJ9ecihNwuP486 L7xSo6dPvCDV/tXzg9YrGauVLmFdmD7pD2gr8nv6kbsM+UZnbNLvxvnXfSKns5Ij46UI OOwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702045827; x=1702650627; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9X8HQehrxqqdotEA60rYgF/5Z0BWv62JqBcRTG61sQ8=; b=LAGYAE11F3ugxxMBzaqJSH9SLA9junmnob92JqgbEseCalH+K4Pa69Xm6j+PBC6Cqg vwdlIOjf9sa3nbe/lgHxlvQeb6XEU33rxqYRMzmY5d7ko05wDmOCKkoZwWrWk06rqsln WH1Xcd12QvC5Lga4RP1SiT+7yR4v/LQk9FgCfSE91Kpkxo5wav6eFSqO7rBqDAw0YR7X KN1Gy7u7JFZ04NGWispSSxjkRlI6ycmztkDho60ZNF8kQ3dnUyp/HlMjwWfk/x3JG/R9 z5xTenGkgL+5z/2xZLCnryEF2zRmYeKcorHvjAYJlJQa/TSFbDMk8A67M66pyyY3FLVl przg== X-Gm-Message-State: AOJu0YwO8r/K0GakWyetDqd+Qjuo4Zba6m9I1gBc+8JgxeHpSdog4o2D +IIZnppqIAbMPPV+IhI3KPluQlHoVyT4EhvjDOe18g== X-Received: by 2002:a05:600c:4d0e:b0:40b:5e1e:cf1 with SMTP id u14-20020a05600c4d0e00b0040b5e1e0cf1mr33402wmp.44.1702045826774; Fri, 08 Dec 2023 06:30:26 -0800 (PST) MIME-Version: 1.0 References: <20231207150348.82096-1-alexghiti@rivosinc.com> <20231207150348.82096-3-alexghiti@rivosinc.com> <6e3fb7b0-f47d-4d2f-b1b8-3ecc047b9ebf@csgroup.eu> In-Reply-To: <6e3fb7b0-f47d-4d2f-b1b8-3ecc047b9ebf@csgroup.eu> From: Alexandre Ghiti Date: Fri, 8 Dec 2023 15:30:16 +0100 Message-ID: Subject: Re: [PATCH RFC/RFT 2/4] riscv: Add a runtime detection of invalid TLB entries caching To: Christophe Leroy Cc: Catalin Marinas , Will Deacon , Thomas Bogendoerfer , Michael Ellerman , Nicholas Piggin , Paul Walmsley , Palmer Dabbelt , Albert Ou , Andrew Morton , Ved Shanbhogue , Matt Evans , Dylan Jhong , "linux-arm-kernel@lists.infradead.org" , "linux-kernel@vger.kernel.org" , "linux-mips@vger.kernel.org" , "linuxppc-dev@lists.ozlabs.org" , "linux-riscv@lists.infradead.org" , "linux-mm@kvack.org" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 08 Dec 2023 06:30:34 -0800 (PST) On Thu, Dec 7, 2023 at 4:55=E2=80=AFPM Christophe Leroy wrote: > > > > Le 07/12/2023 =C3=A0 16:03, Alexandre Ghiti a =C3=A9crit : > > This mechanism allows to completely bypass the sfence.vma introduced by > > the previous commit for uarchs that do not cache invalid TLB entries. > > > > Signed-off-by: Alexandre Ghiti > > --- > > arch/riscv/mm/init.c | 124 ++++++++++++++++++++++++++++++++++++++++++= + > > 1 file changed, 124 insertions(+) > > > > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c > > index 379403de6c6f..2e854613740c 100644 > > --- a/arch/riscv/mm/init.c > > +++ b/arch/riscv/mm/init.c > > @@ -56,6 +56,8 @@ bool pgtable_l5_enabled =3D IS_ENABLED(CONFIG_64BIT) = && !IS_ENABLED(CONFIG_XIP_KER > > EXPORT_SYMBOL(pgtable_l4_enabled); > > EXPORT_SYMBOL(pgtable_l5_enabled); > > > > +bool tlb_caching_invalid_entries; > > + > > phys_addr_t phys_ram_base __ro_after_init; > > EXPORT_SYMBOL(phys_ram_base); > > > > @@ -750,6 +752,18 @@ static void __init disable_pgtable_l4(void) > > satp_mode =3D SATP_MODE_39; > > } > > > > +static void __init enable_pgtable_l5(void) > > +{ > > + pgtable_l5_enabled =3D true; > > + satp_mode =3D SATP_MODE_57; > > +} > > + > > +static void __init enable_pgtable_l4(void) > > +{ > > + pgtable_l4_enabled =3D true; > > + satp_mode =3D SATP_MODE_48; > > +} > > + > > static int __init print_no4lvl(char *p) > > { > > pr_info("Disabled 4-level and 5-level paging"); > > @@ -826,6 +840,112 @@ static __init void set_satp_mode(uintptr_t dtb_pa= ) > > memset(early_pud, 0, PAGE_SIZE); > > memset(early_pmd, 0, PAGE_SIZE); > > } > > + > > +/* Determine at runtime if the uarch caches invalid TLB entries */ > > +static __init void set_tlb_caching_invalid_entries(void) > > +{ > > +#define NR_RETRIES_CACHING_INVALID_ENTRIES 50 > > Looks odd to have macros nested in the middle of a function. > > > + uintptr_t set_tlb_caching_invalid_entries_pmd =3D ((unsigned long= )set_tlb_caching_invalid_entries) & PMD_MASK; > > + // TODO the test_addr as defined below could go into another pud.= .. > > + uintptr_t test_addr =3D set_tlb_caching_invalid_entries_pmd + 2 *= PMD_SIZE; > > + pmd_t valid_pmd; > > + u64 satp; > > + int i =3D 0; > > + > > + /* To ease the page table creation */ > > + disable_pgtable_l5(); > > + disable_pgtable_l4(); > > + > > + /* Establish a mapping for set_tlb_caching_invalid_entries() in s= v39 */ > > + create_pgd_mapping(early_pg_dir, > > + set_tlb_caching_invalid_entries_pmd, > > + (uintptr_t)early_pmd, > > + PGDIR_SIZE, PAGE_TABLE); > > + > > + /* Handle the case where set_tlb_caching_invalid_entries straddle= s 2 PMDs */ > > + create_pmd_mapping(early_pmd, > > + set_tlb_caching_invalid_entries_pmd, > > + set_tlb_caching_invalid_entries_pmd, > > + PMD_SIZE, PAGE_KERNEL_EXEC); > > + create_pmd_mapping(early_pmd, > > + set_tlb_caching_invalid_entries_pmd + PMD_SIZE= , > > + set_tlb_caching_invalid_entries_pmd + PMD_SIZE= , > > + PMD_SIZE, PAGE_KERNEL_EXEC); > > + > > + /* Establish an invalid mapping */ > > + create_pmd_mapping(early_pmd, test_addr, 0, PMD_SIZE, __pgprot(0)= ); > > + > > + /* Precompute the valid pmd here because the mapping for pfn_pmd(= ) won't exist */ > > + valid_pmd =3D pfn_pmd(PFN_DOWN(set_tlb_caching_invalid_entries_pm= d), PAGE_KERNEL); > > + > > + local_flush_tlb_all(); > > + satp =3D PFN_DOWN((uintptr_t)&early_pg_dir) | SATP_MODE_39; > > + csr_write(CSR_SATP, satp); > > + > > + /* > > + * Set stvec to after the trapping access, access this invalid ma= pping > > + * and legitimately trap > > + */ > > + // TODO: Should I save the previous stvec? > > +#define ASM_STR(x) __ASM_STR(x) > > Looks odd to have macros nested in the middle of a function. > > > > + asm volatile( > > + "la a0, 1f \n" > > + "csrw " ASM_STR(CSR_TVEC) ", a0 \n" > > + "ld a0, 0(%0) \n" > > + ".align 2 \n" > > + "1: \n" > > + : > > + : "r" (test_addr) > > + : "a0" > > + ); > > + > > + /* Now establish a valid mapping to check if the invalid one is c= ached */ > > + early_pmd[pmd_index(test_addr)] =3D valid_pmd; > > + > > + /* > > + * Access the valid mapping multiple times: indeed, we can't use > > + * sfence.vma as a barrier to make sure the cpu did not reorder a= ccesses > > + * so we may trap even if the uarch does not cache invalid entrie= s. By > > + * trying a few times, we make sure that those uarchs will see th= e right > > + * mapping at some point. > > + */ > > + > > + i =3D NR_RETRIES_CACHING_INVALID_ENTRIES; > > + > > +#define ASM_STR(x) __ASM_STR(x) > > Deplicate define ? > > > + asm_volatile_goto( > > + "la a0, 1f \n" > > + "csrw " ASM_STR(CSR_TVEC) ", a0 \n" > > + ".align 2 \n" > > + "1: \n" > > + "addi %0, %0, -1 \n" > > + "blt %0, zero, %l[caching_invalid_entries] \n" > > + "ld a0, 0(%1) \n" > > + : > > + : "r" (i), "r" (test_addr) > > + : "a0" > > + : caching_invalid_entries > > + ); > > + > > + csr_write(CSR_SATP, 0ULL); > > + local_flush_tlb_all(); > > + > > + /* If we don't trap, the uarch does not cache invalid entries! */ > > + tlb_caching_invalid_entries =3D false; > > + goto clean; > > + > > +caching_invalid_entries: > > + csr_write(CSR_SATP, 0ULL); > > + local_flush_tlb_all(); > > + > > + tlb_caching_invalid_entries =3D true; > > +clean: > > + memset(early_pg_dir, 0, PAGE_SIZE); > > + memset(early_pmd, 0, PAGE_SIZE); > > Use clear_page() instead ? > > > + > > + enable_pgtable_l4(); > > + enable_pgtable_l5(); > > +} > > #endif > > > > /* > > @@ -1072,6 +1192,7 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa) > > #endif > > > > #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL) > > + set_tlb_caching_invalid_entries(); > > set_satp_mode(dtb_pa); > > #endif > > > > @@ -1322,6 +1443,9 @@ static void __init setup_vm_final(void) > > local_flush_tlb_all(); > > > > pt_ops_set_late(); > > + > > + pr_info("uarch caches invalid entries: %s", > > + tlb_caching_invalid_entries ? "yes" : "no"); > > } > > #else > > asmlinkage void __init setup_vm(uintptr_t dtb_pa) I left this patch so that people can easily test this without knowing what their uarch is actually doing, but it will very likely be dropped as a new extension has just been proposed for that. Thanks anyway, I should have been more clear in the patch title, Alex