Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp1802520rwb; Sun, 6 Aug 2023 03:20:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHCW/JD49s41moXE8/cZ5VTV/o4JmohDUsREHt7S2XFaQAfz6FJMaaadNH2MbtjWriFAjQ9 X-Received: by 2002:a17:903:2349:b0:1bb:2093:efb1 with SMTP id c9-20020a170903234900b001bb2093efb1mr7209675plh.27.1691317232825; Sun, 06 Aug 2023 03:20:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691317232; cv=none; d=google.com; s=arc-20160816; b=JPTTOb1zqyFdrqqkKAsAQhF0qa0yymQsiYliNsY8ICdNNzPHHJKxedTmbATkdo6sQf Z4BkQ4fmEE2tTHJ5/oLJ3aH1nr1W+Qq+PB5em0M+TWW7/QgI3Ml1OG2UMFxCUgXBojS4 zxc898ge0pC0nHgRUBLAhVijw0UNFHsgyiMgUrF13EGrBNvDUYLqo3WBCHiN5xi+9L53 dEwoofazi6QLBmVVsr7j1V/jN9Du3l0Su8XjHjutDWjBW2o6yAH8CSANKf1ur+pSh6UZ BxITBT0UJbckd3JIQrERVLhBUhxld7PckIke3pGwXXq1rC2rlsqQPuUmZzrvGpnDdZNr FWOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=Y39qOj6t/ZqruX0F2AI2ZaHOk/nNXopT1mWnY6FtNkM=; fh=2QkKx4J+ua4xbFH4wBFGkH5IIVDetBCeGJmuTca+O18=; b=Oo9920kWhWwlVSTUFjiRMQRtFt+ibRiR77doKl80R+5MlDEiq7L7GDduzjBDFllbAo VyLhYpwM2Fu0Tnq651G1aOXnuIuWBMRtFS5KRe7myogn6i4LqjLDHsZGDzY2F8uP3Dyk mY1ZnS0jnsuZQZ67vq6od68Cr9B9fXf0OwSXGyHpn22mkbjqEWJeg9jUvKnZo/sj9aTQ zhJT+IMThsvm2t8atTSjOQ8Q9oBOQ69fxycqPhPQgnzuHfQLhD6C323KHD+h5DSQ+NC5 5/+PcL04lpJR04/gW3GPxl2yXdxCN++qBOMbZafiFU13Mh+i7wZ0xRk/52tAnCs0gCN3 2MAg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n18-20020a170903111200b001bb3bcd05bbsi4400157plh.471.2023.08.06.03.20.21; Sun, 06 Aug 2023 03:20:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229636AbjHFJDp (ORCPT + 99 others); Sun, 6 Aug 2023 05:03:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229436AbjHFJDo (ORCPT ); Sun, 6 Aug 2023 05:03:44 -0400 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [217.70.183.194]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 050051BD0; Sun, 6 Aug 2023 02:03:41 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 011BF40002; Sun, 6 Aug 2023 09:03:37 +0000 (UTC) Message-ID: Date: Sun, 6 Aug 2023 11:03:37 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [RFT 1/2] RISC-V: handle missing "no-map" properties for OpenSBI's PMP protected regions Content-Language: en-US To: Conor Dooley , palmer@dabbelt.com Cc: conor@kernel.org, Paul Walmsley , Atish Patra , Anup Patel , Alexandre Ghiti , =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , Song Shuai , JeeHeng Sia , Petr Tesarik , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20230802-purse-hydrant-6f44f77364b0@wendy> <20230802-detention-second-82ab2b53e07a@wendy> From: Alexandre Ghiti In-Reply-To: <20230802-detention-second-82ab2b53e07a@wendy> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-GND-Sasl: alex@ghiti.fr X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS, SPF_PASS 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 Hi Conor, On 02/08/2023 13:12, Conor Dooley wrote: > Add an erratum for versions [v0.8 to v1.3) of OpenSBI which fail to add > the "no-map" property to the reserved memory nodes for the regions it > has protected using PMPs. > > Our existing fix sweeping hibernation under the carpet by marking it > NONPORTABLE is insufficient as there are other ways to generate > accesses to these reserved memory regions, as Petr discovered [1] > while testing crash kernels & kdump. > > Intercede during the boot process when the afflicted versions of OpenSBI > are present & set the "no-map" property in all "mmode_resv" nodes before > the kernel does its reserved memory region initialisation. > > Reported-by: Song Shuai > Link: https://lore.kernel.org/all/CAAYs2=gQvkhTeioMmqRDVGjdtNF_vhB+vm_1dHJxPNi75YDQ_Q@mail.gmail.com/ > Reported-by: JeeHeng Sia > Link: https://groups.google.com/a/groups.riscv.org/g/sw-dev/c/ITXwaKfA6z8 > Reported-by: Petr Tesarik > Closes: https://lore.kernel.org/linux-riscv/76ff0f51-d6c1-580d-f943-061e93073306@huaweicloud.com/ [1] > CC: stable@vger.kernel.org > Signed-off-by: Conor Dooley > --- > arch/riscv/include/asm/sbi.h | 5 +++++ > arch/riscv/kernel/sbi.c | 42 +++++++++++++++++++++++++++++++++++- > arch/riscv/mm/init.c | 3 +++ > 3 files changed, 49 insertions(+), 1 deletion(-) > > diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h > index 5b4a1bf5f439..5360f3476278 100644 > --- a/arch/riscv/include/asm/sbi.h > +++ b/arch/riscv/include/asm/sbi.h > @@ -252,6 +252,9 @@ enum sbi_pmu_ctr_type { > #define SBI_ERR_ALREADY_STARTED -7 > #define SBI_ERR_ALREADY_STOPPED -8 > > +/* SBI implementation IDs */ > +#define SBI_IMP_OPENSBI 1 > + > extern unsigned long sbi_spec_version; > struct sbiret { > long error; > @@ -259,6 +262,8 @@ struct sbiret { > }; > > void sbi_init(void); > +void sbi_apply_reserved_mem_erratum(void *dtb_va); > + > struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0, > unsigned long arg1, unsigned long arg2, > unsigned long arg3, unsigned long arg4, > diff --git a/arch/riscv/kernel/sbi.c b/arch/riscv/kernel/sbi.c > index c672c8ba9a2a..aeb27263fa53 100644 > --- a/arch/riscv/kernel/sbi.c > +++ b/arch/riscv/kernel/sbi.c > @@ -5,8 +5,10 @@ > * Copyright (c) 2020 Western Digital Corporation or its affiliates. > */ > > +#include > #include > #include > +#include > #include > #include > #include > @@ -583,6 +585,40 @@ long sbi_get_mimpid(void) > } > EXPORT_SYMBOL_GPL(sbi_get_mimpid); > > +static long sbi_firmware_id; > +static long sbi_firmware_version; > + > +/* > + * For devicetrees patched by OpenSBI a "mmode_resv" node is added to cover > + * the region OpenSBI has protected by means of a PMP. Some versions of OpenSBI, > + * [v0.8 to v1.3), omitted the "no-map" property, but this trips up hibernation > + * among other things. > + */ > +void __init sbi_apply_reserved_mem_erratum(void *dtb_pa) > +{ > + int child, reserved_mem; > + > + if (sbi_firmware_id != SBI_IMP_OPENSBI) > + return; > + > + if (!acpi_disabled) > + return; > + > + if (sbi_firmware_version >= 0x10003 || sbi_firmware_version < 0x8) > + return; > + > + reserved_mem = fdt_path_offset((void *)dtb_pa, "/reserved-memory"); > + if (reserved_mem < 0) > + return; > + > + fdt_for_each_subnode(child, (void *)dtb_pa, reserved_mem) { > + const char *name = fdt_get_name((void *)dtb_pa, child, NULL); > + > + if (!strncmp(name, "mmode_resv", 10)) I would check that name != NULL before strncmp. > + fdt_setprop((void *)dtb_pa, child, "no-map", NULL, 0); > + } > +}; > + > void __init sbi_init(void) > { > int ret; > @@ -596,8 +632,12 @@ void __init sbi_init(void) > sbi_major_version(), sbi_minor_version()); > > if (!sbi_spec_is_0_1()) { > + sbi_firmware_id = sbi_get_firmware_id(); > + sbi_firmware_version = sbi_get_firmware_version(); > + > pr_info("SBI implementation ID=0x%lx Version=0x%lx\n", > - sbi_get_firmware_id(), sbi_get_firmware_version()); > + sbi_firmware_id, sbi_firmware_version); > + > if (sbi_probe_extension(SBI_EXT_TIME)) { > __sbi_set_timer = __sbi_set_timer_v02; > pr_info("SBI TIME extension detected\n"); > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c > index 70fb31960b63..cb16bfdeacdb 100644 > --- a/arch/riscv/mm/init.c > +++ b/arch/riscv/mm/init.c > @@ -29,6 +29,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -253,6 +254,8 @@ static void __init setup_bootmem(void) > * in the device tree, otherwise the allocation could end up in a > * reserved region. > */ > + > + sbi_apply_reserved_mem_erratum(dtb_early_va); > early_init_fdt_scan_reserved_mem(); > > /* Otherwise the patch looks good to me: You can add: Reviewed-by: Alexandre Ghiti I just have one question though (that we discussed privately already): should we fix openSBI in the kernel? If yes, what makes a bug worth being fixed in the kernel? Thanks,