From patchwork Thu Feb 9 21:42:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 9565539 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 8EAED601C3 for ; Thu, 9 Feb 2017 21:43:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7EB5128548 for ; Thu, 9 Feb 2017 21:43:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 738A128552; Thu, 9 Feb 2017 21:43:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id ACC8328548 for ; Thu, 9 Feb 2017 21:43:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=gEeyFgCXbjjS0FqMQX74ezTDx+TVDmYDxcXIOhIhWzQ=; b=utXF1Ab/CRB72CFoVe3Rc0aNiX KcDbizBn8q+mUVj1mun6kFt4uvOmAnc1G3Qu5t41pClbubqiKs+Ev24ZS+K9SFs18JOhS3++CxFhF KM7H8oquRlBBPlOWBS8joyTiZ+07hI661QqrLJtl5GR0yhS1UBXNyU0aqW6B+wFAWdHy/xC/UMfIU Es5rirVv5RMWxr428DmZIW4tqv+SEU0XSo4alaAP3w9/p9iRhknV3YTD/N6ByVlGLglgz9xyifvmT iOQHc4hdiaI4mxvrXS4iX1V15v8CqpobCB9G85ZH9N5lv8tbl2zjwXr/Y/gGeea9QxG7oZs6i9IQF pODhP5gQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cbwVK-00079c-Rq; Thu, 09 Feb 2017 21:43:50 +0000 Received: from mail-wm0-x22a.google.com ([2a00:1450:400c:c09::22a]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cbwV5-0006x3-1q for linux-arm-kernel@lists.infradead.org; Thu, 09 Feb 2017 21:43:37 +0000 Received: by mail-wm0-x22a.google.com with SMTP id r141so32341461wmg.1 for ; Thu, 09 Feb 2017 13:43:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=mYuKBTc5IhVHeYxSIyXukNBt5xxhiI9lZb3Q6o9h4wc=; b=JYaUHQ6iLUV/KvoJ044qttb+YzPO95k0FfBBCf0ccCj76vqPdFYoWjgT6V6SRRNS09 o1IdB2ylAoZW29kplXGvRbkblpwStlLT37PTZHbVQFTfgjPbivaQCKbXaICloq75vvYA zGSNn4LwWo1HB/mMrxfLiir4GrFMbSyEzxKsE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=mYuKBTc5IhVHeYxSIyXukNBt5xxhiI9lZb3Q6o9h4wc=; b=Mq6KK8ZvPjQWVnxV4nwyUZQGoVY4xXTr7gmak5StxO2OOCf0579E+MGC/9Oq9X17Qo jTFx+17N/hyQjysaJGKOE7p8JEjGEgdoojYjeQv41tKQjUfmKH0Zw8e+fnhKvoIxTyxh QNZqx8cxE0XDhbdCqdiEsl0KI9mHOlOFmcsVfC4L87SjYeANWkJ2M9e4wij+YsZcWVnP LYWbtFZSmTgLfllbizRHCbP1ycYS1Q1nLINNCODtAErAfAMQiHayyXbBqHB+LQgPMRO8 3DYOLg1XuVi1O6PjtY1JxijbAHtT7vbcI7J3fWssVCbov4FTUTnryVWBfJkkK63k5QA/ 0wfg== X-Gm-Message-State: AMke39nz9xinuhcXBv90FWm+LkPOMM6f7SDUwxTThBOiOQW/sZTVzTnW3QTNItaSbsawFc4/ X-Received: by 10.28.186.197 with SMTP id k188mr24541825wmf.25.1486676593227; Thu, 09 Feb 2017 13:43:13 -0800 (PST) Received: from localhost.localdomain ([160.169.163.122]) by smtp.gmail.com with ESMTPSA id x25sm20493047wrx.27.2017.02.09.13.43.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 09 Feb 2017 13:43:12 -0800 (PST) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, matt@codeblueprint.co.uk, mark.rutland@arm.com Subject: [PATCH v2 2/2] efi: arm-stub: Round up FDT allocation to mapping size Date: Thu, 9 Feb 2017 21:42:53 +0000 Message-Id: <1486676573-19237-2-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1486676573-19237-1-git-send-email-ard.biesheuvel@linaro.org> References: <1486676573-19237-1-git-send-email-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170209_134335_252111_0B9D0A4B X-CRM114-Status: GOOD ( 19.14 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: jhugo@codeaurora.org, catalin.marinas@arm.com, timur@codeaurora.org, leif.lindholm@linaro.org, Ard Biesheuvel MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP The FDT is mapped via a fixmap entry that is at least 2 MB in size and 2 MB aligned on 4 KB page size kernels. On UEFI systems, the FDT allocation may share this 2 MB block with a reserved region, or another memory region that we should never map, unless we account for this in the size of the allocation (the alignment is already 2 MB) So instead of taking guesses at the needed space, simply allocate 2 MB immediately. The allocation will be recorded as a EFI_LOADER_DATA, and the kernel only memblock_reserve()'s the actual size of the FDT, so the unused space will be released to the kernel. Signed-off-by: Ard Biesheuvel Reviewed-By: Jeffrey Hugo --- arch/arm64/include/asm/efi.h | 1 + drivers/firmware/efi/libstub/fdt.c | 57 +++++++++----------- 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/arch/arm64/include/asm/efi.h b/arch/arm64/include/asm/efi.h index 342e90d6d204..f642565fc1d0 100644 --- a/arch/arm64/include/asm/efi.h +++ b/arch/arm64/include/asm/efi.h @@ -1,6 +1,7 @@ #ifndef _ASM_EFI_H #define _ASM_EFI_H +#include #include #include #include diff --git a/drivers/firmware/efi/libstub/fdt.c b/drivers/firmware/efi/libstub/fdt.c index 260c4b4b492e..41f457be64e8 100644 --- a/drivers/firmware/efi/libstub/fdt.c +++ b/drivers/firmware/efi/libstub/fdt.c @@ -206,6 +206,10 @@ static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg, return update_fdt_memmap(p->new_fdt_addr, map); } +#ifndef MAX_FDT_SIZE +#define MAX_FDT_SIZE SZ_2M +#endif + /* * Allocate memory for a new FDT, then add EFI, commandline, and * initrd related fields to the FDT. This routine increases the @@ -233,7 +237,6 @@ efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table, u32 desc_ver; unsigned long mmap_key; efi_memory_desc_t *memory_map, *runtime_map; - unsigned long new_fdt_size; efi_status_t status; int runtime_entry_count = 0; struct efi_boot_memmap map; @@ -262,41 +265,29 @@ efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table, "Exiting boot services and installing virtual address map...\n"); map.map = &memory_map; + status = efi_high_alloc(sys_table, MAX_FDT_SIZE, EFI_FDT_ALIGN, + new_fdt_addr, max_addr); + if (status != EFI_SUCCESS) { + pr_efi_err(sys_table, + "Unable to allocate memory for new device tree.\n"); + goto fail; + } + /* - * Estimate size of new FDT, and allocate memory for it. We - * will allocate a bigger buffer if this ends up being too - * small, so a rough guess is OK here. + * Now that we have done our final memory allocation (and free) + * we can get the memory map key needed for exit_boot_services(). */ - new_fdt_size = fdt_size + EFI_PAGE_SIZE; - while (1) { - status = efi_high_alloc(sys_table, new_fdt_size, EFI_FDT_ALIGN, - new_fdt_addr, max_addr); - if (status != EFI_SUCCESS) { - pr_efi_err(sys_table, "Unable to allocate memory for new device tree.\n"); - goto fail; - } - - status = update_fdt(sys_table, - (void *)fdt_addr, fdt_size, - (void *)*new_fdt_addr, new_fdt_size, - cmdline_ptr, initrd_addr, initrd_size); + status = efi_get_memory_map(sys_table, &map); + if (status != EFI_SUCCESS) + goto fail_free_new_fdt; - /* Succeeding the first time is the expected case. */ - if (status == EFI_SUCCESS) - break; + status = update_fdt(sys_table, (void *)fdt_addr, fdt_size, + (void *)*new_fdt_addr, MAX_FDT_SIZE, cmdline_ptr, + initrd_addr, initrd_size); - if (status == EFI_BUFFER_TOO_SMALL) { - /* - * We need to allocate more space for the new - * device tree, so free existing buffer that is - * too small. - */ - efi_free(sys_table, new_fdt_size, *new_fdt_addr); - new_fdt_size += EFI_PAGE_SIZE; - } else { - pr_efi_err(sys_table, "Unable to construct new device tree.\n"); - goto fail_free_new_fdt; - } + if (status != EFI_SUCCESS) { + pr_efi_err(sys_table, "Unable to construct new device tree.\n"); + goto fail_free_new_fdt; } priv.runtime_map = runtime_map; @@ -340,7 +331,7 @@ efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table, pr_efi_err(sys_table, "Exit boot services failed.\n"); fail_free_new_fdt: - efi_free(sys_table, new_fdt_size, *new_fdt_addr); + efi_free(sys_table, MAX_FDT_SIZE, *new_fdt_addr); fail: sys_table->boottime->free_pool(runtime_map);