Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp2818688ybl; Mon, 19 Aug 2019 07:58:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqyaH0C/RhwMMcGEUKVIIJ2OgeB51kgCYAxnzt8JbdAqhSzl+FFwFJ3eF2dr7ceYr4dUFrS7 X-Received: by 2002:a65:6216:: with SMTP id d22mr18854741pgv.404.1566226709423; Mon, 19 Aug 2019 07:58:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566226709; cv=none; d=google.com; s=arc-20160816; b=QpEx/q1Kk+i5MzWCJ8aP/99Xf1XOihoxPPGA8Br2xCavKaAydh3xkVuVxuErzNWkfz XY2VQ5unCpRs/p+iD0sgaQeL+PQhorQs3i/8ujdSOYfQyh21xk0afFg6xOJNlYhikXji J0AmCkXQ6afBYQEjZHRb155qFbaYQZiOliE94Mo4cGOBpsRC1FAMVcBQLcoi6VHRZ0p5 LeFR5YfqyV97BmP8xveNoqCfb6ocKgPAmEXLUlno7TdE4dc0BwQtglKhhdUEIOSyfPsL RbNm301VgHO3Xon2H5VPBqt21Tqpt6HQzTdKC7nyN7by+IscJP3+Ro1YyiKqKI8dZEw6 rHnA== 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=GAjO3kDCKw737mzKsCDzc5iC6QhFGKmfyjzceHvXKsU=; b=PgetRRLMsCl8NPO/D87X2merAWSZToskAxJ7b66owMsLmZoq38jLUpWhIF5vJEHeQ+ 4SIld9D0er15b9RjbmnF2U63CR1zzxj6HL8AEpv9wiE7+Bex4t5vyOnLsfF1WtfxKPBu aX19PC36SoBsCcd+aUvzfk/IyLQPdQG9reSL5LEkf4P9I8VDBZWTTX0DkkqI2XyZ/hs3 2qAey0pkFtmjfpIxbP+QZmr9c9dOYlTiMAq7xbC4y4Fl09s1x77zlAItm96INw3MRWbN ety2ga6bINR3lHkJXQDUnk/rmMkk+MmLjD3i9fkMd71CT1ADveD4OfWW7JmqR3ytCLCA 7i+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="eHXy/LiF"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b35si9954925plb.249.2019.08.19.07.58.14; Mon, 19 Aug 2019 07:58:29 -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=@linaro.org header.s=google header.b="eHXy/LiF"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727341AbfHSO5I (ORCPT + 99 others); Mon, 19 Aug 2019 10:57:08 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:41873 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725536AbfHSO5I (ORCPT ); Mon, 19 Aug 2019 10:57:08 -0400 Received: by mail-wr1-f66.google.com with SMTP id j16so9040051wrr.8 for ; Mon, 19 Aug 2019 07:57:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=GAjO3kDCKw737mzKsCDzc5iC6QhFGKmfyjzceHvXKsU=; b=eHXy/LiFZh4gTgutePEYj72vYa3eraTi+gVzKrpM8z0zscSoc9UJpBuMortwZBA+yH b/qr6lPvJg2FbAmYXEVoaweL9a2Ast4QPjhwkO402tiAnBchwSQYMqsc3Ga4Xrk2Ng3W m4AS4QVgcC6tckNHGvaiyRI2W53BSLkm3JKL81fodAmYKV6ENKScDhJTmgEfalb1KuWk YnD5iWbCqtLHviBfoPgj73No1xc7M8ZMn7xj+wS23J9X6JjXWSjswI4RBuBE0WVsPtPz 7p7rA+Vacwl2wrcih/m1v58ny92ZlbO1wetTpUk7e1iDNlvUDxUuzTODDlaF4IOC+ssV 66SQ== 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=GAjO3kDCKw737mzKsCDzc5iC6QhFGKmfyjzceHvXKsU=; b=FF8l2P3nwz3ptiM0EMf/2XPE9mHIG6G4DD2BSgMxXjnEE6qbWaW9n3vsHZH8H/MPU9 w7l3tpEFbfM6Oo2GG0pmqFVz16vT+eqJvIzDh+p3rUr8KuHzgb7H6K4bVXqlFvQoghka 3sP677Md6b+J9dx8BjmnFJ+j0rnbbFVLkS9htvZJpUG5JLP8859PckeNVZikcyOdrZ+q 4RRZapyW5D54xsBIizMEr9z15JuWTz59HQjMvcL8gGe4muJrdIcKTm7Vq8SnA4q8yw9n Wsjee90dKT9S5Izm28ErRL87j4AJGXWbIEm83YtomfDLanr5ETAWlOv4eNckjwhRNwK7 KiYA== X-Gm-Message-State: APjAAAWooiaZGIdSSDvq5EDrA3R6+gTyN5cr9MuSDIU3exZPvvCZrC6/ c2uWa59Fj7KkaiVFGkpTdfxQSXMaCeGev5Vo03yECQ== X-Received: by 2002:a5d:5450:: with SMTP id w16mr15166999wrv.174.1566226625378; Mon, 19 Aug 2019 07:57:05 -0700 (PDT) MIME-Version: 1.0 References: <20190802053744.5519-1-clin@suse.com> <20190815111543.GA4728@linux-8mug> <20190815133738.GA2483@rapoport-lnx> <20190819075621.GA20595@linux-8mug> In-Reply-To: <20190819075621.GA20595@linux-8mug> From: Ard Biesheuvel Date: Mon, 19 Aug 2019 17:56:51 +0300 Message-ID: Subject: Re: [PATCH] efi/arm: fix allocation failure when reserving the kernel base To: Chester Lin Cc: Mike Rapoport , "guillaume.gardet@arm.com" , "linux@armlinux.org.uk" , "ren_guo@c-sky.com" , "mingo@kernel.org" , "akpm@linux-foundation.org" , "geert@linux-m68k.org" , "linux-arm-kernel@lists.infradead.org" , Gary Lin , Juergen Gross , Joey Lee , "linux-efi@vger.kernel.org" , "linux-kernel@vger.kernel.org" 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 Mon, 19 Aug 2019 at 11:01, Chester Lin wrote: > > Hi Mike and Ard, > > On Thu, Aug 15, 2019 at 04:37:39PM +0300, Mike Rapoport wrote: > > On Thu, Aug 15, 2019 at 02:32:50PM +0300, Ard Biesheuvel wrote: > > > (adding Mike) > > > > > > On Thu, 15 Aug 2019 at 14:28, Chester Lin wrote: > > > > > > > > Hi Ard, > > > > > > > > On Thu, Aug 15, 2019 at 10:59:43AM +0300, Ard Biesheuvel wrote: > > > > > On Sun, 4 Aug 2019 at 10:57, Ard Biesheuvel wrote: > > > > > > > > > > > > Hello Chester, > > > > > > > > > > > > On Fri, 2 Aug 2019 at 08:40, Chester Lin wrote: > > > > > > > > > > > > > > In some cases the arm32 efistub could fail to allocate memory for > > > > > > > uncompressed kernel. For example, we got the following error message when > > > > > > > verifying EFI stub on Raspberry Pi-2 [kernel-5.2.1 + grub-2.04] : > > > > > > > > > > > > > > EFI stub: Booting Linux Kernel... > > > > > > > EFI stub: ERROR: Unable to allocate memory for uncompressed kernel. > > > > > > > EFI stub: ERROR: Failed to relocate kernel > > > > > > > > > > > > > > After checking the EFI memory map we found that the first page [0 - 0xfff] > > > > > > > had been reserved by Raspberry Pi-2's firmware, and the efistub tried to > > > > > > > set the dram base at 0, which was actually in a reserved region. > > > > > > > > > > > > > > > > > > > This by itself is a violation of the Linux boot protocol for 32-bit > > > > > > ARM when using the decompressor. The decompressor rounds down its own > > > > > > base address to a multiple of 128 MB, and assumes the whole area is > > > > > > available for the decompressed kernel and related data structures. > > > > > > (The first TEXT_OFFSET bytes are no longer used in practice, which is > > > > > > why putting a reserved region of 4 KB bytes works at the moment, but > > > > > > this is fragile). Note that the decompressor does not look at any DT > > > > > > or EFI provided memory maps *at all*. > > > > > > > > > > > > So unfortunately, this is not something we can fix in the kernel, but > > > > > > we should fix it in the bootloader or in GRUB, so it does not put any > > > > > > reserved regions in the first 128 MB of memory, > > > > > > > > > > > > > > > > OK, perhaps we can fix this by taking TEXT_OFFSET into account. The > > > > > ARM boot protocol docs are unclear about whether this memory should be > > > > > used or not, but it is no longer used for its original purpose (page > > > > > tables), and the RPi loader already keeps data there. > > > > > > > > > > Can you check whether the following patch works for you? > > > > > > > > > > diff --git a/drivers/firmware/efi/libstub/Makefile > > > > > b/drivers/firmware/efi/libstub/Makefile > > > > > index 0460c7581220..ee0661ddb25b 100644 > > > > > --- a/drivers/firmware/efi/libstub/Makefile > > > > > +++ b/drivers/firmware/efi/libstub/Makefile > > > > > @@ -52,6 +52,7 @@ lib-$(CONFIG_EFI_ARMSTUB) += arm-stub.o fdt.o > > > > > string.o random.o \ > > > > > > > > > > lib-$(CONFIG_ARM) += arm32-stub.o > > > > > lib-$(CONFIG_ARM64) += arm64-stub.o > > > > > +CFLAGS_arm32-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) > > > > > CFLAGS_arm64-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) > > > > > > > > > > # > > > > > diff --git a/drivers/firmware/efi/libstub/arm32-stub.c > > > > > b/drivers/firmware/efi/libstub/arm32-stub.c > > > > > index e8f7aefb6813..66ff0c8ec269 100644 > > > > > --- a/drivers/firmware/efi/libstub/arm32-stub.c > > > > > +++ b/drivers/firmware/efi/libstub/arm32-stub.c > > > > > @@ -204,7 +204,7 @@ efi_status_t > > > > > handle_kernel_image(efi_system_table_t *sys_table, > > > > > * loaded. These assumptions are made by the decompressor, > > > > > * before any memory map is available. > > > > > */ > > > > > - dram_base = round_up(dram_base, SZ_128M); > > > > > + dram_base = round_up(dram_base, SZ_128M) + TEXT_OFFSET; > > > > > > > > > > status = reserve_kernel_base(sys_table, dram_base, reserve_addr, > > > > > reserve_size); > > > > > > > > > > > > > I tried your patch on rpi2 and got the following panic. Just a reminder that I > > > > have replaced some log messages with "......" since it might be too long to > > > > post all. > > > > > > > > > > OK. Good to know that this change helps you to get past the EFI stub boot issue. > > > > > > > In this case the kernel failed to reserve cma, which should hit the issue of > > > > memblock_limit=0x1000 as I had mentioned in my patch description. The first > > > > block [0-0xfff] was scanned in adjust_lowmem_bounds(), but it did not align > > > > with PMD_SIZE so the cma reservation failed because the memblock.current_limit > > > > was extremely low. That's why I expand the first reservation from 1 PAGESIZE to > > > > 1 PMD_SIZE in my patch in order to avoid this issue. Please kindly let me know > > > > if any suggestion, thank you. > > > > > > > This looks like it is a separate issue. The memblock/cma code should > > > not choke on a reserved page of memory at 0x0. > > > > > > Perhaps Russell or Mike (cc'ed) have an idea how to address this? > > > > Presuming that the last memblock dump comes from the end of > > arm_memblock_init() with the this memory map > > > > memory[0x0] [0x0000000000000000-0x0000000000000fff], 0x0000000000001000 bytes flags: 0x4 > > memory[0x1] [0x0000000000001000-0x0000000007ef5fff], 0x0000000007ef5000 bytes flags: 0x0 > > memory[0x2] [0x0000000007ef6000-0x0000000007f09fff], 0x0000000000014000 bytes flags: 0x4 > > memory[0x3] [0x0000000007f0a000-0x000000003cb3efff], 0x0000000034c35000 bytes flags: 0x0 > > > > adjust_lowmem_bounds() will set the memblock_limit (and respectively global > > memblock.current_limit) to 0x1000 and any further memblock_alloc*() will > > happily fail. > > > > I believe that the assumption for memblock_limit calculations was that the > > first bank has several megs at least. > > > > I wonder if this hack would help: > > > > diff --git a/arch/arm/mm/mmu.c b/arch/arm/mm/mmu.c > > index d9a0038..948e5b9 100644 > > --- a/arch/arm/mm/mmu.c > > +++ b/arch/arm/mm/mmu.c > > @@ -1206,7 +1206,7 @@ void __init adjust_lowmem_bounds(void) > > * allocated when mapping the start of bank 0, which > > * occurs before any free memory is mapped. > > */ > > - if (!memblock_limit) { > > + if (memblock_limit < PMD_SIZE) { > > if (!IS_ALIGNED(block_start, PMD_SIZE)) > > memblock_limit = block_start; > > else if (!IS_ALIGNED(block_end, PMD_SIZE)) > > > > I applied this patch as well and it works well on rpi-2 model B. > Thanks, Chester, that is good to know. However, afaict, this only affects systems where physical memory starts at address 0x0, so I think we need a better fix. I know Mike has been looking into the NOMAP stuff lately, and your original patch contains a hunk that makes this code (?) disregard nomap memblocks. That might be a better approach.