Received: by 2002:a25:b323:0:0:0:0:0 with SMTP id l35csp2604750ybj; Mon, 23 Sep 2019 06:36:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqw4AMkgTBuFMcAl9F2qWwdz/slOvCECmR2pGu74lQAmnAvpUU/Xa9eiS111DTKcl4EcUzmO X-Received: by 2002:a17:906:f82:: with SMTP id q2mr30650302ejj.143.1569245769550; Mon, 23 Sep 2019 06:36:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569245769; cv=none; d=google.com; s=arc-20160816; b=0J1BAhXF7dqPWWzpiMi94gf9rMhMY36CxDNoJrxUXMV7fpjD4F0T/Om2QDmHvRISgy rxxuwobI0KXsEUzhHQEVat6ixsdlRQ5oIUZMj3DX1RHJLRbC9mSfjm0mkcpQyn4mHKK8 8DA38G+8FKVzztAVCcYi9Ql3wZNq93iY70jhwzMgIMyTDd3wIgCVxqP7qmqqRGbkRXf6 r1GEdzVOGlEItDRFpMpe9P7GRX2Kz3qNX/BhKrd1uCiW/vdUV8P8/gA/Jt1hvj6tAOZ3 z24pomNbE5tKjpJeL1AAXoupP8gtWPqkpZ5U5wU+4hUUnOA84Xax2xlvfe6JIFJaOgeZ whSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:to:from:dkim-signature; bh=akrFEhXOEhjjNsFA2n/lMwPsnj11zAdFvp8IXUT8xe4=; b=yi2hunhpPM3C1qFQjwAh8N8y5NHTc/pNjnA4xc3JCxYnIx1Zw94qxCpKEc8HlKVrdA jFt2n+68oOhoxw7LlGf7zKK93cHngfxWt05qQzZdIjpeEO8Gmo49z/uNuvuFjaUNuMhc TAuMPvY6TTb5uPXmAD4SiBFu0dWC+hlhWxhVYuR72LtVDmg1mT5zkOFVa1grmAY2lCRR ZK+g4eVN3Dcn+pUQ6UJQwrinn3H6+y2m/ig3v80+r3nALuClWkLUdmveOGiCtp+zdm/O r5XABEtYN3WxT1bed0H/05+pXP6yN1sjMpQE0ztiVDCGuTt/tPjaINYRmLe76d9UKF9x 6iFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@eecs-berkeley-edu.20150623.gappssmtp.com header.s=20150623 header.b=TWPxe23o; 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 z6si6813720edm.316.2019.09.23.06.35.45; Mon, 23 Sep 2019 06:36:09 -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=@eecs-berkeley-edu.20150623.gappssmtp.com header.s=20150623 header.b=TWPxe23o; 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 S2394108AbfIUBAH (ORCPT + 99 others); Fri, 20 Sep 2019 21:00:07 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:36072 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2394077AbfIUBAG (ORCPT ); Fri, 20 Sep 2019 21:00:06 -0400 Received: by mail-pf1-f196.google.com with SMTP id y22so5689383pfr.3 for ; Fri, 20 Sep 2019 18:00:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eecs-berkeley-edu.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=akrFEhXOEhjjNsFA2n/lMwPsnj11zAdFvp8IXUT8xe4=; b=TWPxe23oSrRfaWMw7X3pg1d8S7BsqmV/cTrGYIVKKy5TYTrmNuWtFFzhialiNBvIt8 wO9LR4ked246/nnwkOvzHi4lcErObALKlZLBKByeG/4O02vWKT6nRi0/zuMA2Q/ZDzPA SefiLs0FA6an72vmJWbPKrFUP+oi8MAfrWC3WywJyoQqjExxzAE5ojSsQwpD3HylDKDJ WZsDeG5ZILOxFHJ0tyIcd/k2G9Qd1gOAG03dDmhvqGrN1R8QSuWvoUFtFnBLW6L8A9QH r1RSK2ihA5p2Bli+BHXBN8c3J/YrBaUJ2aC2CwNYtUh8eGJyESJ7SWMoVLjeAToRLFs4 2aQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=akrFEhXOEhjjNsFA2n/lMwPsnj11zAdFvp8IXUT8xe4=; b=VKmi3yzIe7hCugN9eKaucxYBL+nNQuRhM7ArAqvwKxgG7Njr/zOZt30mGTBCojB76T PR0efTEJpolKSx/l4azABPTQ0/gj95XjRDBLDK1blDfaYYxBXc5IGeEVquVMIWAwRdqG /pOKrFKGiPT8Dea7QFuLbD1gYXU+pdlacXs3SE5G9ZifZdLU4BMH+8E9WKccD6hgpjBh zeHSoeWc6oyJIH+XMjB2OH8y2oVTR5LRbaMct+oUUoErMjruBlGwxPqQAIvoVwrJxH2K yBYeoLGN790FnQFlALCub6WVQDb95a+eh3Ctuxeb5cH4yfZpoF7nCaAQXxmNVS71vxTp c46g== X-Gm-Message-State: APjAAAW7/qv/px4/R3Yl3/3P3wx0YiPt1mVL+ATNRFpPcC2fIE72av9N /69IjVAGYVH/cHi4Hcvns4NjOA== X-Received: by 2002:aa7:8edd:: with SMTP id b29mr20586708pfr.138.1569027604422; Fri, 20 Sep 2019 18:00:04 -0700 (PDT) Received: from localhost (dhcp-35-38.EECS.Berkeley.EDU. [128.32.35.38]) by smtp.gmail.com with ESMTPSA id d10sm3549344pfh.8.2019.09.20.18.00.03 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 20 Sep 2019 18:00:03 -0700 (PDT) From: Albert Ou To: Palmer Dabbelt , Paul Walmsley , Anup Patel , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH] riscv: Fix memblock reservation for device tree blob Date: Fri, 20 Sep 2019 18:00:02 -0700 Message-Id: <20190921010002.61006-1-aou@eecs.berkeley.edu> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 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; + 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 + 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