Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp2406584rdb; Fri, 8 Dec 2023 07:17:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IHUV1cNXXPNAtY0axRMr5NcCbvedzRUur2cz2AVkrbDLp8M3mN9djwc6xqDJsxG+2CJrAnb X-Received: by 2002:a05:6a20:5487:b0:18f:97c:4f48 with SMTP id i7-20020a056a20548700b0018f097c4f48mr228416pzk.84.1702048679611; Fri, 08 Dec 2023 07:17:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702048679; cv=none; d=google.com; s=arc-20160816; b=YIT1QM5ouYiUr0ADEzT3yqmRO6f731LdsSf2PDI6zLQ7xbsUHzMpukYdTtxOa63Pnr Db1hraUcIuvlkpSUI9TlmxzS0g6RV+sJg/pyJqMKZj1qpUNMwlODMjm28E8E21NLlx9Z Tv05h5nzZfjU7Fx1e3rrgxXpsLp+0dvJ0xfY+L4xI9YjaYZGDojZkPgnT5uvcprmHMVn 7i9pghI/zsmQ5KCWdWfXpwWs1YvzX2v3iqftj/KeAqxQMnq1+2Jkm57qrsHTiFoyEr5k ESiwxRSDeV/EQSvN1C8UibIBxO0CCk00KZqjUNFWlmyTOUfpZPVP2Gw4NerKqVRI4CkP iJlw== 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=5/ISMdWgChMRStmKN/NLjIE+3boUHfR2xarEpZoUhVA=; fh=YwjhpLCOMRTwN/Wpr1LWjf/FLLfc2JWkeU9++DJOII8=; b=V0JI8T5Mqfaez/5rp+gkymO3r6uvRSD9oHVdLSDYBfBtJYsva1/gwiZlWoKLcsAmMt IEkeSUSYb5SOogRtSrkUa5PwP/frlgLr8Mj0PCKV4U3mboviUFgpiiNtg/KsFHKcdBaA oVo4eoxYb/g6O3O+Yr9O4xA2Mt81Ftt6LUUn38QIlSlGzwBm2nLFODuEsvmkK2GIn0zW gUcKudceAFIUk8FBmoW1BX646jdFaqKzRztF4dqEkwAKb1AIgXpf002P8AkC96jp98nd FBIbXLVKquFnfRdit1HINJVZ7Cjyo9ozAveukGm0skrzml+1NKGOk/VtZjZKTwAPNzJa BpRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=VT8TiPU8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id v18-20020a631512000000b005c623037c10si1768644pgl.96.2023.12.08.07.17.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 07:17:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=VT8TiPU8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id D543C81CFF24; Fri, 8 Dec 2023 07:17:56 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1574280AbjLHPRh (ORCPT + 99 others); Fri, 8 Dec 2023 10:17:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1574257AbjLHPRg (ORCPT ); Fri, 8 Dec 2023 10:17:36 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D2A71996 for ; Fri, 8 Dec 2023 07:17:42 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 37B15C4339A for ; Fri, 8 Dec 2023 15:17:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1702048662; bh=kSzj0p26fXMxcGqE7PiPZ0L9JbNQNQodTYDm9B6XDgk=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=VT8TiPU8AWNNluFOQ6urgB12MMJoQ9XGqf3He6NQnqBuHXn7K0cUvfcSWnM9LtbrN koFsKbVZC5hbEyYlHLhw821WPeZDWudAIYX0FO6onGEuaQG/d70MdIYsgu2SCBi7k5 OKe9tnTEoKCaavIpoHOTCL3UkBtGcEZT0OFnv1FXed25sOc1gsczKk9v3vWFI/2/Ua bIyC8WLC2BNLV4HE1XJRpuzltph0Tl6nL2N7IKJK/EN9HnSQFK/9Wy1aphDw6w9+9K WEfRg2si2t833Q/8bi7T/1L6oADuo2Z12qAMNodWzwiW4OuKSZXkxyrPpJleLTV96+ Q23sLJSYDRK8Q== Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a1ef2f5ed01so233365366b.0 for ; Fri, 08 Dec 2023 07:17:42 -0800 (PST) X-Gm-Message-State: AOJu0YwqXzoeM8pwhwLwIvGGNBkyQHplIamf6Tm40P9Z/D1hSjxHsrzW 29oGD2ERpBqA/uElK50CGFSXxbogkDo07EdG3Rw= X-Received: by 2002:a17:907:94c9:b0:a1d:3500:6906 with SMTP id dn9-20020a17090794c900b00a1d35006906mr61156ejc.297.1702048660644; Fri, 08 Dec 2023 07:17:40 -0800 (PST) MIME-Version: 1.0 References: <20231208151036.2458921-1-guoren@kernel.org> In-Reply-To: <20231208151036.2458921-1-guoren@kernel.org> From: Guo Ren Date: Fri, 8 Dec 2023 23:17:29 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] riscv: pgtable: Enhance set_pte to prevent OoO risk To: paul.walmsley@sifive.com, palmer@dabbelt.com, akpm@linux-foundation.org, alexghiti@rivosinc.com, catalin.marinas@arm.com, willy@infradead.org, david@redhat.com, muchun.song@linux.dev, will@kernel.org, peterz@infradead.org, rppt@kernel.org, paulmck@kernel.org, atishp@atishpatra.org, anup@brainfault.org, alex@ghiti.fr, mike.kravetz@oracle.com, dfustini@baylibre.com, wefu@redhat.com, jszhang@kernel.org, falcon@tinylab.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Guo Ren Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email 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 (lipwig.vger.email [0.0.0.0]); Fri, 08 Dec 2023 07:17:57 -0800 (PST) On Fri, Dec 8, 2023 at 11:10=E2=80=AFPM wrote: > > From: Guo Ren > > When changing from an invalid pte to a valid one for a kernel page, > there is no need for tlb_flush. It's okay for the TSO memory model, but > there is an OoO risk for the Weak one. eg: Sorry, TSO has no Immunity. The above sentence should be rewritten with: When changing from an invalid pte to a valid one for a kernel page, there is no guarantee tlb_flush in Linux. So, there is an OoO risk for riscv, e.g.: > > sd t0, (a0) // a0 =3D pte address, pteval is changed from invalid to vali= d > ... > ld t1, (a1) // a1 =3D va of above pte > > If the ld instruction is executed speculatively before the sd > instruction. Then it would bring an invalid entry into the TLB, and when > the ld instruction retired, a spurious page fault occurred. Because the > vmemmap has been ignored by vmalloc_fault, the spurious page fault would > cause kernel panic. > > This patch was inspired by the commit: 7f0b1bf04511 ("arm64: Fix barriers > used for page table modifications"). For RISC-V, there is no requirement > in the spec to guarantee all tlb entries are valid and no requirement to > PTW filter out invalid entries. Of course, micro-arch could give a more > robust design, but here, use a software fence to guarantee. > > Signed-off-by: Guo Ren > Signed-off-by: Guo Ren > --- > arch/riscv/include/asm/pgtable.h | 7 +++++++ > 1 file changed, 7 insertions(+) > > diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pg= table.h > index 294044429e8e..2fae5a5438e0 100644 > --- a/arch/riscv/include/asm/pgtable.h > +++ b/arch/riscv/include/asm/pgtable.h > @@ -511,6 +511,13 @@ static inline int pte_same(pte_t pte_a, pte_t pte_b) > static inline void set_pte(pte_t *ptep, pte_t pteval) > { > *ptep =3D pteval; > + > + /* > + * Only if the new pte is present and kernel, otherwise TLB > + * maintenance or update_mmu_cache() have the necessary barriers. > + */ > + if (pte_val(pteval) & (_PAGE_PRESENT | _PAGE_GLOBAL)) > + RISCV_FENCE(rw,rw); > } > > void flush_icache_pte(pte_t pte); > -- > 2.40.1 > --=20 Best Regards Guo Ren