Received: by 2002:a25:b323:0:0:0:0:0 with SMTP id l35csp1803740ybj; Sun, 22 Sep 2019 12:16:49 -0700 (PDT) X-Google-Smtp-Source: APXvYqxIzm9UU1aEem/GmazDmKwcggZIOu93GD86BiLy76GCWqrxv0IpD1FfH6WdR1f2aTDUjHOb X-Received: by 2002:a50:ac0d:: with SMTP id v13mr32897469edc.189.1569179809773; Sun, 22 Sep 2019 12:16:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569179809; cv=none; d=google.com; s=arc-20160816; b=GYxYAb6nTmoy1b/kDO6OpIUq8y+Qt1+e82yPJpt3ddFZFf6vzgOY34fNuZ3CByOv/v LmcdyZI6RgAt3bhAzDaj6n7Aq30qnAOPhEyhFW+cizKc+GBZ1wgKKGIVsHVdz6g9Xgqt ATQmFwjvdZVchxgQGQ8phJSmnUCr7PnEWpYYGY1xaAkcxn9lLeDQgYqFWkZ+nZInrFx2 ccUR8OAeoZfWIQfRFKwzUNJmnX51iD1aHFlpntKjJntx4c8W+Ar0bE3gUn8pK9n+QX2w foUZ+z48LHwDGQNy3+QpDrZN89uDjZFPZLU41XPT5mejxOVRMGMWs46rnd/9WzoRtQG6 HoBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=pGKiDGF7pcSXfE/kXL5zm3CUq0K5jLTeFExSKLR0vc0=; b=QTngnazyvFPAqdOjOzhQXm5ONfO+GxGP68NVYD2tDdxefluIRrbPE5PzQSizDHECe+ Wvr6rXAxg+q7zlQQCpzE5SN9I4Hotl7C2IC8mnuT8TwGC3ssQugxY+1Mlxxhlyhgx4GB UHnh9B5RiSA+70T2tvlSxzmE855jzfTzY6JWhZCyMw0UhXeK4T6IUvlhNZurcv3FbSTp GljFkoObQ4q8b5ujVxh3IYLsGQDrGsxLni5q8wrtUfqtWRe36qVYP8ygH6NyCVmLCseP s0Qgb/m8NNJiVUF/jVGBk33HoGZJtgA+eANXQjsMitOENXrKGHtBlr+qEfV6+4MVkOwY nRZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=yqJ3L9P0; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id nj7si4084836ejb.262.2019.09.22.12.16.26; Sun, 22 Sep 2019 12:16:49 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=yqJ3L9P0; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726177AbfIUEfO (ORCPT + 99 others); Sat, 21 Sep 2019 00:35:14 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:53006 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725870AbfIUEfN (ORCPT ); Sat, 21 Sep 2019 00:35:13 -0400 Received: by mail-wm1-f67.google.com with SMTP id x2so4433876wmj.2 for ; Fri, 20 Sep 2019 21:35:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brainfault-org.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=pGKiDGF7pcSXfE/kXL5zm3CUq0K5jLTeFExSKLR0vc0=; b=yqJ3L9P0c2mDdv0+ntW2WM0jo5HFKquFuU36IwxqO8xkVoiET+QUZ9DAmfBIPONPxZ Zy7dSSNhnzJeRAuplQwrb7c5am6JBUou1wSYba8U6qoeMZEgqaduhJpGxLUAOcDtHsHS by7/5mjC1/ioJHGEPU2PplfjqD9t1TawkOZeLxG8oFL0EGdNh1OD4FDoboE4542o16bx DBw6St20spsXZ1Eiw2m3nWIi/NTDLsNAUe0e+qn0XQxImOHhLhVsFxaVhK8A6i/Lft6F +osnK/bjjn8LSs4xb6Jxu3XxBPXob9XLMZPc0kc2Oz8oJtJwxmMAVqYu0+UgqSXp5UWj h/4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=pGKiDGF7pcSXfE/kXL5zm3CUq0K5jLTeFExSKLR0vc0=; b=AKWD/UVGQap08EjeNGB518UUR94PZvR8oHz+E/oP30Mkazch5KlHG9HSDSYP14q/Le ra9UhL44SUWYifXwMI+nysu59f3AHHU4G1IZIEM5CgJ/bNyjsm3GwDTOF4d1TarQhL4s c0rsxDlkW+tuD/pcHjSkGvr0oF00ylF1KY4pwsvBoVqidFIy6OA1Y651xvaB2bnMiV2c aFkRLaQHy7Uw6VY/MGz5iRkzoiYIsYXA0Kjn4jEfx8G1WJLeJRGqB1f3YLbPUIHyO8Nl MfCren7E/H4PDT4PyJ+lZahtQF48nGlrUwSXA0DLekOzaAktGB46Aspz3vzPEISgN1/L RwEQ== X-Gm-Message-State: APjAAAWFfc1vvXEos87wFUWLfZwZ78ZW+VKryamJvHG75NUwxoMDAvNh Tfv1mSK80/Tn7yHa2s/UnSDNV7tD8R2O4Ix09ic/Aw== X-Received: by 2002:a1c:80ca:: with SMTP id b193mr5263971wmd.171.1569040509087; Fri, 20 Sep 2019 21:35:09 -0700 (PDT) MIME-Version: 1.0 References: <20190921010002.61006-1-aou@eecs.berkeley.edu> In-Reply-To: <20190921010002.61006-1-aou@eecs.berkeley.edu> From: Anup Patel Date: Sat, 21 Sep 2019 10:04:57 +0530 Message-ID: Subject: Re: [PATCH] riscv: Fix memblock reservation for device tree blob To: Albert Ou Cc: Palmer Dabbelt , Paul Walmsley , Anup Patel , linux-riscv , "linux-kernel@vger.kernel.org List" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Sep 21, 2019 at 6:30 AM Albert Ou wrote: > > This fixes an error with how the FDT blob is reserved in memblock. > An incorrect physical address calculation exposed the FDT header to > unintended corruption, which typically manifested with of_fdt_raw_init() > faulting during late boot after fdt_totalsize() returned a wrong value. > Systems with smaller physical memory sizes more frequently trigger this > issue, as the kernel is more likely to allocate from the DMA32 zone > where bbl places the DTB after the kernel image. > > Commit 671f9a3e2e24 ("RISC-V: Setup initial page tables in two stages") > changed the mapping of the DTB to reside in the fixmap area. > Consequently, early_init_fdt_reserve_self() cannot be used anymore in > setup_bootmem() since it relies on __pa() to derive a physical address, > which does not work with dtb_early_va that is no longer a valid kernel > logical address. > > The reserved[0x1] region shows the effect of the pointer underflow > resulting from the __pa(initial_boot_params) offset subtraction: > > [ 0.000000] MEMBLOCK configuration: > [ 0.000000] memory size = 0x000000001fe00000 reserved size = 0x0000000000a2e514 > [ 0.000000] memory.cnt = 0x1 > [ 0.000000] memory[0x0] [0x0000000080200000-0x000000009fffffff], 0x000000001fe00000 bytes flags: 0x0 > [ 0.000000] reserved.cnt = 0x2 > [ 0.000000] reserved[0x0] [0x0000000080200000-0x0000000080c2dfeb], 0x0000000000a2dfec bytes flags: 0x0 > [ 0.000000] reserved[0x1] [0xfffffff080100000-0xfffffff080100527], 0x0000000000000528 bytes flags: 0x0 > > With the fix applied: > > [ 0.000000] MEMBLOCK configuration: > [ 0.000000] memory size = 0x000000001fe00000 reserved size = 0x0000000000a2e514 > [ 0.000000] memory.cnt = 0x1 > [ 0.000000] memory[0x0] [0x0000000080200000-0x000000009fffffff], 0x000000001fe00000 bytes flags: 0x0 > [ 0.000000] reserved.cnt = 0x2 > [ 0.000000] reserved[0x0] [0x0000000080200000-0x0000000080c2dfeb], 0x0000000000a2dfec bytes flags: 0x0 > [ 0.000000] reserved[0x1] [0x0000000080e00000-0x0000000080e00527], 0x0000000000000528 bytes flags: 0x0 Thanks for catching this issue. Most of us did not notice this issue most likely because: 1. We generally have good enough RAM on QEMU and SiFive Unleashed 2. Most of people use OpenSBI FW_JUMP on QEMU and U-Boot on SiFive Unleashed to boot in Linux which places FDT quite far away from Linux kernel end Linux ARM64 kernel also uses FIXMAP to access FDT and over there as well early_init_fdt_reserve_self() is not used. > > Fixes: 671f9a3e2e24 ("RISC-V: Setup initial page tables in two stages") > Signed-off-by: Albert Ou > --- > arch/riscv/mm/init.c | 13 ++++++++++++- > 1 file changed, 12 insertions(+), 1 deletion(-) > > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c > index f0ba713..52d007c 100644 > --- a/arch/riscv/mm/init.c > +++ b/arch/riscv/mm/init.c > @@ -11,6 +11,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -82,6 +83,8 @@ static void __init setup_initrd(void) > } > #endif /* CONFIG_BLK_DEV_INITRD */ > > +static phys_addr_t __dtb_pa __initdata; May be dtb_early_pa will be more consistent name instead of __dtb_pa because it matches dtb_early_va used below. > + > void __init setup_bootmem(void) > { > struct memblock_region *reg; > @@ -117,7 +120,12 @@ void __init setup_bootmem(void) > setup_initrd(); > #endif /* CONFIG_BLK_DEV_INITRD */ > > - early_init_fdt_reserve_self(); > + /* > + * Avoid using early_init_fdt_reserve_self() since __pa() does > + * not work for DTB pointers that are fixmap addresses > + */ > + memblock_reserve(__dtb_pa, fdt_totalsize(dtb_early_va)); > + > early_init_fdt_scan_reserved_mem(); > memblock_allow_resize(); > memblock_dump_all(); > @@ -333,6 +341,7 @@ static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size) > "not use absolute addressing." > #endif > > + Please remove this newline addition. > asmlinkage void __init setup_vm(uintptr_t dtb_pa) > { > uintptr_t va, end_va; > @@ -393,6 +402,8 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa) > > /* Save pointer to DTB for early FDT parsing */ > dtb_early_va = (void *)fix_to_virt(FIX_FDT) + (dtb_pa & ~PAGE_MASK); > + /* Save physical address for memblock reservation */ > + __dtb_pa = dtb_pa; > } > > static void __init setup_vm_final(void) > -- > 2.7.4 > > > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv This deserves to be stable kernel fix as well. You should add: Cc: stable@vger.kernel.org in your commit description. Apart from minor nits above. Reviewed-by: Anup Patel I tried this patch for both RV64 and RV32 on QEMU with Yocto rootfs. Tested-by: Anup Patel Regards, Anup