From patchwork Tue Nov 6 11:37:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 10670153 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7DFAC15A6 for ; Tue, 6 Nov 2018 11:38:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6BEFE2A26B for ; Tue, 6 Nov 2018 11:38:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 607C12A29B; Tue, 6 Nov 2018 11:38:10 +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=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.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 008952A26B for ; Tue, 6 Nov 2018 11:38:09 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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=dU2JEoQ9AFa0+nUfw1nn44PDCfQgcOotSGB4oocdIA8=; b=t4u+ySPZ6nwsEs n/Z9Os6fn/GNq2+SHyYpfEY4RQjwXmOUwj0oJFI/h1DbgSK3VbzXJULJiICX4/AI4BpW6XasN87om aSHrIWJF809pGrvDsh11RQcn2z/NR6qVAIv/T6UgSv1xOU4buMwG/b/r4tFLKDvW/vT8bVQHQ9BJm M3xekglEspI76QUi+t1uxSmEBIYej4oTvGBqXxdWBMuyYwN+vlG4nLmnYINPwJJbQxOcjKI75FK+u 6xU9phLcXwc96zu2u7tden5GaNqJxhTz13+B3LveLhFnV9lxbJ9IG1nzqBbtYhm5HIfwFD0A33apZ lqCIO5kkpRckdE3pSJ0A==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gJzgJ-0007o7-E4; Tue, 06 Nov 2018 11:38:03 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gJzg9-0007bh-Bm for linux-arm-kernel@lists.infradead.org; Tue, 06 Nov 2018 11:37:55 +0000 Received: by mail-wr1-x442.google.com with SMTP id o15-v6so9463586wrv.4 for ; Tue, 06 Nov 2018 03:37:43 -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 :mime-version:content-transfer-encoding; bh=ZGPqz/MYWANycuG9Tb+ISa8UHJfaVM9dxj3YR/jxCQc=; b=iXexW/biYTNUhWdmJoVHrYaLBUxS9dw8I/G3tucCH+p95OGNX6ijjWO59gjeD2n/2U RnsyiQbZYJgQ3PkIgAL8/IYxhU6McBwd/IKXMhIghu/XQmf5H/+gX6PA/0LVj0mIKY+E m+UcNC0MIescacXAFTK7Go1h1P/7YxFNSJnhI= 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:mime-version:content-transfer-encoding; bh=ZGPqz/MYWANycuG9Tb+ISa8UHJfaVM9dxj3YR/jxCQc=; b=RJLB5/Xvv/d7D27+I3I8R6KnNk9drLFVRVOeKoqOiRpTtoGUoGtoOWbq/oafY2Vj11 B1/IibnG5zMhXtbACI4ZfRXCtTgsIXnDtLnDQBPkLWwbIX9m5hv+GYNhHK60ZETyL0vh hXM6DZse1Cv+5b99C5WKZFaCqlet5E00jrjxPWS/9h3LC0hYCUi47/qPWSBhMab50Krc 6LBi/cuZvBq2WqfF9kElmZMg+D2MgTvRGC+66whqfqU2xqO7gd2BdZkfhZKeiZ9+JE8Z /SL2E1W8vyH0y05We1kULln2fB6Gr6Yb5c1PYbrNhKUcuV7hzl+XNQLZokRbR6+bHNj2 /E7A== X-Gm-Message-State: AGRZ1gJgD+1VI366ZymC4YFD1OhgoA45Ei8nLSYnjAlF6i+g0eJ5ymVY 1Y0K/x2h08wSkVoetmLohUblv/LWz8o= X-Google-Smtp-Source: AJdET5eIX6q3C5TxP9+WdZ1YCLB1SnrDAcikjrtJHzCa4mYyVXpJutKo+Bbn9IRXZk3f6s7ZpLkUHw== X-Received: by 2002:adf:c550:: with SMTP id s16-v6mr22802200wrf.264.1541504261269; Tue, 06 Nov 2018 03:37:41 -0800 (PST) Received: from harold.home ([2a01:cb1d:112:6f00:3117:2b29:5ef7:a9e5]) by smtp.gmail.com with ESMTPSA id t187-v6sm1431529wmt.45.2018.11.06.03.37.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Nov 2018 03:37:40 -0800 (PST) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Subject: [PATCH 1/4] arm64: memblock: don't permit memblock resizing until linear mapping is up Date: Tue, 6 Nov 2018 12:37:29 +0100 Message-Id: <20181106113732.16351-2-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181106113732.16351-1-ard.biesheuvel@linaro.org> References: <20181106113732.16351-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181106_033753_398184_02B157AE X-CRM114-Status: GOOD ( 13.02 ) 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: mark.rutland@arm.com, linux-efi@vger.kernel.org, Ard Biesheuvel , marc.zyngier@arm.com, bhsharma@redhat.com, will.deacon@arm.com 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 Bhupesh reports that having numerous memblock reservations at early boot may result in the following crash: Unable to handle kernel paging request at virtual address ffff80003ffe0000 ... Call trace: __memcpy+0x110/0x180 memblock_add_range+0x134/0x2e8 memblock_reserve+0x70/0xb8 memblock_alloc_base_nid+0x6c/0x88 __memblock_alloc_base+0x3c/0x4c memblock_alloc_base+0x28/0x4c memblock_alloc+0x2c/0x38 early_pgtable_alloc+0x20/0xb0 paging_init+0x28/0x7f8 This is caused by the fact that we permit memblock resizing before the linear mapping is up, and so the memblock_reserved() array is moved into memory that is not mapped yet. So let's ensure that this crash can no longer occur, by deferring to call to memblock_allow_resize() to after the linear mapping has been created. Reported-by: Bhupesh Sharma Signed-off-by: Ard Biesheuvel Acked-by: Will Deacon --- arch/arm64/mm/init.c | 2 -- arch/arm64/mm/mmu.c | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index 9d9582cac6c4..9b432d9fcada 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -483,8 +483,6 @@ void __init arm64_memblock_init(void) high_memory = __va(memblock_end_of_DRAM() - 1) + 1; dma_contiguous_reserve(arm64_dma_phys_limit); - - memblock_allow_resize(); } void __init bootmem_init(void) diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c index 394b8d554def..d1d6601b385d 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -659,6 +659,8 @@ void __init paging_init(void) memblock_free(__pa_symbol(init_pg_dir), __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir)); + + memblock_allow_resize(); } /* From patchwork Tue Nov 6 11:37:30 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 10670155 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 80F0D15A6 for ; Tue, 6 Nov 2018 11:38:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6ED612A29B for ; Tue, 6 Nov 2018 11:38:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 62FF32A2B0; Tue, 6 Nov 2018 11:38:23 +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=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.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 EBBFC2A29B for ; Tue, 6 Nov 2018 11:38:22 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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=ujTCtWmxiMWgThKNXHofz6c5kmYP7Kve2KY/K4gCnXg=; b=q2ycZKIed/oiVv wquPLTWOB4xNVdj0q4WDP+fNHij6uhcc3s/4R2Wm1xBeVUhWJ2XGN8QVy+d0ewTfSZqqI9GTK5Hg9 kiiRX70Bp221Bc80MtAP4XoPDDylCwzA5KTIIDqFoVlEIsdziCuT6wEZU9rqG8KaVsK0K/HjB+3JQ DlDg54VXlqU09A8Z1UiWvhhbsVw4IQs+C4QEimTTnZ8WRvgA/hT9wkO7YfCynsV7eMqqIBHNszf3b XH18SpLejDhL5s/6B6x3OlnxP9Tlbt8x66QdVH0zwTYVFzXHLUNmlLXyW6Xwo1cgDsnqCDYRY2rFN Qh4kresHc8Fd2txeoIQg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gJzgT-00082k-Ga; Tue, 06 Nov 2018 11:38:13 +0000 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gJzgA-0007bj-UW for linux-arm-kernel@lists.infradead.org; Tue, 06 Nov 2018 11:37:56 +0000 Received: by mail-wm1-x342.google.com with SMTP id r63-v6so11122847wma.4 for ; Tue, 06 Nov 2018 03:37:44 -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 :mime-version:content-transfer-encoding; bh=JxerrVzSB3nQMNgEEqnbh4b0SvNJgKQEb0pTSsx3VlE=; b=LEkis6IbqL13P9CYwk0pSb3DMuyibP8zOWT0xJQkjqheSP+YuIxivZH9wFPPr30nA3 n1FEhjV6SlrxNUHB8pXZ/eqtGjL+trEycYiTubzOAfLM/ZCQDl37uHszNLM+2qSo5Jr2 +1/F9gnVTqlrTJ/AY1AKCbRvpp8zcEOWcaYN0= 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:mime-version:content-transfer-encoding; bh=JxerrVzSB3nQMNgEEqnbh4b0SvNJgKQEb0pTSsx3VlE=; b=R0XA9GEOfVkVxs7LtQDRi9RDvKy5Yb58YLF2tzD0lj+hVS5GITCoTfZQ/u2rGEefYy ACBUFHC4aUzjRKb1fitKScfqCbUPQOxEvuX6G+83gz7aLqQZyg76iPJEPY9gRgpcShdB zoAnh2YdEDBUNzY5edoSsL54KAjYEDV01Y8LLpcsDcmZQ9FH1O0PE13d9pzNfb6ChxfU cRe4j1biUNi1FF00LO+cI5/fbpXZ11HS64JKFT8B2OirOI2ojBqgfFL+LfnWecaxX0Vc C54d6Q8FDC8FGnCHQrg4bBU+6cu+UPlkKZpxfdq0av3w/k4l9aaw6qVARihycVd0yW7J r+EA== X-Gm-Message-State: AGRZ1gIRPvWL+Gu3s/AMTInjpccMRM6bksfGwFdztkvs0Rq1UFMeJZ20 B6DdT6Fn5wuj599G5/xrg1bB076Puwo= X-Google-Smtp-Source: AJdET5csbEhKR5a7lryR/vrpo9dVbamvM0ScDKF5vfZS5VDpTm/ZUmdd836oLLSdXqqzx9nSNWSA7w== X-Received: by 2002:a1c:187:: with SMTP id 129-v6mr1635260wmb.55.1541504262987; Tue, 06 Nov 2018 03:37:42 -0800 (PST) Received: from harold.home ([2a01:cb1d:112:6f00:3117:2b29:5ef7:a9e5]) by smtp.gmail.com with ESMTPSA id t187-v6sm1431529wmt.45.2018.11.06.03.37.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Nov 2018 03:37:42 -0800 (PST) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Subject: [PATCH 2/4] efi/arm: defer persistent reservations until after paging_init() Date: Tue, 6 Nov 2018 12:37:30 +0100 Message-Id: <20181106113732.16351-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181106113732.16351-1-ard.biesheuvel@linaro.org> References: <20181106113732.16351-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181106_033754_986303_63D6D6D6 X-CRM114-Status: GOOD ( 15.41 ) 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: mark.rutland@arm.com, linux-efi@vger.kernel.org, Ard Biesheuvel , marc.zyngier@arm.com, bhsharma@redhat.com, will.deacon@arm.com, Russell King 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 new memory EFI reservation feature we introduced to allow memory reservations to persist across kexec may trigger an unbounded number of calls to memblock_reserve(). The memblock subsystem can deal with this fine, but not before memblock resizing is enabled, which we can only do after paging_init(), when the memory we reallocate the array into is actually mapped. So break out the memreserve table processing into a separate function and call if after paging_init() on both arm64 and ARM. Cc: Russell King Signed-off-by: Ard Biesheuvel --- arch/arm/kernel/setup.c | 1 + arch/arm64/kernel/setup.c | 1 + drivers/firmware/efi/efi.c | 8 ++++++-- include/linux/efi.h | 7 +++++++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/arch/arm/kernel/setup.c b/arch/arm/kernel/setup.c index ac7e08886863..e99f12eaf390 100644 --- a/arch/arm/kernel/setup.c +++ b/arch/arm/kernel/setup.c @@ -1117,6 +1117,7 @@ void __init setup_arch(char **cmdline_p) early_ioremap_reset(); paging_init(mdesc); + efi_apply_persistent_mem_reservations(); request_standard_resources(mdesc); if (mdesc->restart) diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c index 953e316521fc..f4fc1e0544b7 100644 --- a/arch/arm64/kernel/setup.c +++ b/arch/arm64/kernel/setup.c @@ -313,6 +313,7 @@ void __init setup_arch(char **cmdline_p) arm64_memblock_init(); paging_init(); + efi_apply_persistent_mem_reservations(); acpi_table_upgrade(); diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c index 249eb70691b0..55e4ea20bdc3 100644 --- a/drivers/firmware/efi/efi.c +++ b/drivers/firmware/efi/efi.c @@ -592,7 +592,11 @@ int __init efi_config_parse_tables(void *config_tables, int count, int sz, early_memunmap(tbl, sizeof(*tbl)); } + return 0; +} +int __init efi_apply_persistent_mem_reservations(void) +{ if (efi.mem_reserve != EFI_INVALID_TABLE_ADDR) { unsigned long prsv = efi.mem_reserve; @@ -602,7 +606,7 @@ int __init efi_config_parse_tables(void *config_tables, int count, int sz, /* reserve the entry itself */ memblock_reserve(prsv, sizeof(*rsv)); - rsv = early_memremap(prsv, sizeof(*rsv)); + rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB); if (rsv == NULL) { pr_err("Could not map UEFI memreserve entry!\n"); return -ENOMEM; @@ -612,7 +616,7 @@ int __init efi_config_parse_tables(void *config_tables, int count, int sz, memblock_reserve(rsv->base, rsv->size); prsv = rsv->next; - early_memunmap(rsv, sizeof(*rsv)); + memunmap(rsv); } } diff --git a/include/linux/efi.h b/include/linux/efi.h index 845174e113ce..100ce4a4aff6 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -1167,6 +1167,8 @@ static inline bool efi_enabled(int feature) extern void efi_reboot(enum reboot_mode reboot_mode, const char *__unused); extern bool efi_is_table_address(unsigned long phys_addr); + +extern int efi_apply_persistent_mem_reservations(void); #else static inline bool efi_enabled(int feature) { @@ -1185,6 +1187,11 @@ static inline bool efi_is_table_address(unsigned long phys_addr) { return false; } + +static inline int efi_apply_persistent_mem_reservations(void) +{ + return 0; +} #endif extern int efi_status_to_err(efi_status_t status); From patchwork Tue Nov 6 11:37:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 10670157 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5A92115A6 for ; Tue, 6 Nov 2018 11:38:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 44ABC291DF for ; Tue, 6 Nov 2018 11:38:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 33193291F0; Tue, 6 Nov 2018 11:38:29 +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=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.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 ADE2E291DF for ; Tue, 6 Nov 2018 11:38:27 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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=beIzQJJj5qFKHwVEva4XJzj0gz2fSPVNrlEhTwmyTRI=; b=tk3zLO6+ymFNQv 1FnifbOTwfXPvVtK3soDlb0g0z+9vNrS8Mpklw/+110bqv841RGrDaDcXEwbqdG/f2Qcr6Karwo3+ fViblaDVZk4W5p3Evjw3uEOnZ7mUttoF3ViP3XFvWTS8gxxUkl7ArUHlPW9CFEc8E5OEOFwz5aqHp KH6+07m41wi6m+Az6xlB/K6wq8ORU4y42eJGRvcmmD1P9kcFa0KwoQA2bXl2wZmKCnB9s6eqU+7zK G5J+iDOo0GTzQjzmojwKN0AkeMboFwe02TreYTKFQNTG5i3oSjQlqoep3gUxWsMmIGs+mHjwyFeGm lhgt4WOZlg3SbisFJDCw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gJzge-0008Iy-Ag; Tue, 06 Nov 2018 11:38:24 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gJzgC-0007bs-07 for linux-arm-kernel@lists.infradead.org; Tue, 06 Nov 2018 11:37:57 +0000 Received: by mail-wr1-x444.google.com with SMTP id o15-v6so9463772wrv.4 for ; Tue, 06 Nov 2018 03:37:45 -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 :mime-version:content-transfer-encoding; bh=E9tfc28PkEiOPLpRre63HbM1KC12woQQWyYxHBbdomk=; b=XfZFPYRSDzBpggZ+0Oil676DIvkhi+Ybm/5GykEioe5k+w15aqu9n5wTJspx2HzuQo OyURY2Grn7dmE7g5TzDYMZFxn2N+mOHYkMDLa7P254vdkEfwps0FKgPh0eMbWW+OsnxQ jm3wKSaWtnQ2CTIpttSF+cRALQxyimeOuUgHE= 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:mime-version:content-transfer-encoding; bh=E9tfc28PkEiOPLpRre63HbM1KC12woQQWyYxHBbdomk=; b=Al/RrMCV9d+W2jnHHfoPx/qch5JgDIn4G/oEGFYB2DFWiK9jnceJT/6fs8IMu9MiEr sqdQc0LWsud1WxrKXf1XruP1J8JPFqGNnx8mtcgkT1MWnd3lruyaXXZX2GdGhJaVjxs7 w6woUakiaKF53x3ZHEuZJve1+3KvvV1ESN35gqqFgkGaqKT4duJXQ9IupMxInUAZ+DKM tDOC2rudG7y8jdv6sQUIQlNswD0YgYAWJFVCWpRdhkVq/YeE0UFkCcOwhLPlrsult4di 6YKankKQ+l+zwOSGxvWAGMA/z4BFnDFPtzvv3XdbjZKOHo2cFOHcUcVC8nsu1zjJydss JceQ== X-Gm-Message-State: AGRZ1gI1tFOvuMOzmJieWRlcY27NrgTsKTueaTWHoBEipodcZQ6ZKH85 d/0wskgy7OkBuLD0Dohgtavh2yP9ByM= X-Google-Smtp-Source: AJdET5eaQasPwDjocGwWp2dVBhie9dQk0ev3IBAuQEBg8qfSK7DjF0kus0qd5EqqOTCR0EVuQc/8/Q== X-Received: by 2002:a5d:48cc:: with SMTP id p12-v6mr23582923wrs.122.1541504264174; Tue, 06 Nov 2018 03:37:44 -0800 (PST) Received: from harold.home ([2a01:cb1d:112:6f00:3117:2b29:5ef7:a9e5]) by smtp.gmail.com with ESMTPSA id t187-v6sm1431529wmt.45.2018.11.06.03.37.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Nov 2018 03:37:43 -0800 (PST) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Subject: [PATCH 3/4] efi: permit multiple entries in persistent memreserve data structure Date: Tue, 6 Nov 2018 12:37:31 +0100 Message-Id: <20181106113732.16351-4-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181106113732.16351-1-ard.biesheuvel@linaro.org> References: <20181106113732.16351-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181106_033756_043240_0C40ECCC X-CRM114-Status: GOOD ( 17.16 ) 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: mark.rutland@arm.com, linux-efi@vger.kernel.org, Ard Biesheuvel , marc.zyngier@arm.com, bhsharma@redhat.com, will.deacon@arm.com 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 In preparation of updating efi_mem_reserve_persistent() to cause less fragmentation when dealing with many persistent reservations, update the struct definition and the code that handles it currently so it can describe an arbitrary number of reservations using a single linked list entry. The actual optimization will be implemented in a subsequent patch. Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/efi.c | 30 +++++++++++++------- drivers/firmware/efi/libstub/arm-stub.c | 2 +- include/linux/efi.h | 13 +++++++-- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c index 55e4ea20bdc3..85d2ec532816 100644 --- a/drivers/firmware/efi/efi.c +++ b/drivers/firmware/efi/efi.c @@ -602,18 +602,25 @@ int __init efi_apply_persistent_mem_reservations(void) while (prsv) { struct linux_efi_memreserve *rsv; + u8 *p; + int i; - /* reserve the entry itself */ - memblock_reserve(prsv, sizeof(*rsv)); - - rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB); - if (rsv == NULL) { + p = memremap(ALIGN_DOWN(prsv, PAGE_SIZE), PAGE_SIZE, + MEMREMAP_WB); + if (p == NULL) { pr_err("Could not map UEFI memreserve entry!\n"); return -ENOMEM; } - if (rsv->size) - memblock_reserve(rsv->base, rsv->size); + rsv = (void *)(p + prsv % PAGE_SIZE); + + /* reserve the entry itself */ + memblock_reserve(prsv, EFI_MEMRESERVE_SIZE(rsv->size)); + + for (i = 0; i < atomic_read(&rsv->count); i++) { + memblock_reserve(rsv->entry[i].base, + rsv->entry[i].size); + } prsv = rsv->next; memunmap(rsv); @@ -971,11 +978,12 @@ static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock); int efi_mem_reserve_persistent(phys_addr_t addr, u64 size) { struct linux_efi_memreserve *rsv, *parent; + int rsvsize = EFI_MEMRESERVE_SIZE(1); if (efi.mem_reserve == EFI_INVALID_TABLE_ADDR) return -ENODEV; - rsv = kmalloc(sizeof(*rsv), GFP_KERNEL); + rsv = kmalloc(rsvsize, GFP_KERNEL); if (!rsv) return -ENOMEM; @@ -985,8 +993,10 @@ int efi_mem_reserve_persistent(phys_addr_t addr, u64 size) return -ENOMEM; } - rsv->base = addr; - rsv->size = size; + rsv->size = 1; + atomic_set(&rsv->count, 1); + rsv->entry[0].base = addr; + rsv->entry[0].size = size; spin_lock(&efi_mem_reserve_persistent_lock); rsv->next = parent->next; diff --git a/drivers/firmware/efi/libstub/arm-stub.c b/drivers/firmware/efi/libstub/arm-stub.c index 30ac0c975f8a..5bcfa08e8bb1 100644 --- a/drivers/firmware/efi/libstub/arm-stub.c +++ b/drivers/firmware/efi/libstub/arm-stub.c @@ -83,8 +83,8 @@ void install_memreserve_table(efi_system_table_t *sys_table_arg) } rsv->next = 0; - rsv->base = 0; rsv->size = 0; + atomic_set(&rsv->count, 0); status = efi_call_early(install_configuration_table, &memreserve_table_guid, diff --git a/include/linux/efi.h b/include/linux/efi.h index 100ce4a4aff6..dfce82b2ca8a 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -1715,9 +1715,16 @@ extern struct efi_runtime_work efi_rts_work; extern struct workqueue_struct *efi_rts_wq; struct linux_efi_memreserve { - phys_addr_t next; - phys_addr_t base; - phys_addr_t size; + int size; // allocated size of the array + atomic_t count; // number of entries used + phys_addr_t next; // pa of next struct instance + struct { + phys_addr_t base; + phys_addr_t size; + } entry[0]; }; +#define EFI_MEMRESERVE_SIZE(count) (sizeof(struct linux_efi_memreserve) + \ + (count) * sizeof(((struct linux_efi_memreserve *)0)->entry[0])) + #endif /* _LINUX_EFI_H */ From patchwork Tue Nov 6 11:37:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 10670159 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 48CEC15A6 for ; Tue, 6 Nov 2018 11:38:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 357FE29BD8 for ; Tue, 6 Nov 2018 11:38:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 24BF429BFC; Tue, 6 Nov 2018 11:38:51 +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=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.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 B601429BD8 for ; Tue, 6 Nov 2018 11:38:50 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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=zyztgnk7kI3ubmiJzQF0kInubDiY61PF87YapgbrRN0=; b=hiAg6aRYm+TCiR /klkvGB6B9TJoq+G2A/ewVx3AntvhwNZ0NkWJaislRCSwUjxXUTTSErqa40Kdpf/uXP/luz9zBCqe tEKK+jpybiijGLPY4nhjWOrFRUxdC1rAoTj5FH6QvOAo9bmhzmWPf2AewJOGcYGog1y1hD7tSkmmk MoEO5WZe5Ihw1fSMM+JkPl9PIyQxl9jJlwpfU/oN9yX/TRXF8x7l3oa7wYlkZXpbInD65X31mEH0A tNoJ8hHJSA2SzwshAC3Q9S25dEqZ3KAC9tLRqQ/wyow+chTAZ915f9fDU1XfW6uN0jk6YLmOR3iV3 c+SN3jQDJWOaL03t5xgQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gJzgv-00008y-VU; Tue, 06 Nov 2018 11:38:42 +0000 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gJzgA-0007cg-UV for linux-arm-kernel@lists.infradead.org; Tue, 06 Nov 2018 11:37:57 +0000 Received: by mail-wm1-x342.google.com with SMTP id r63-v6so11122929wma.4 for ; Tue, 06 Nov 2018 03:37:47 -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 :mime-version:content-transfer-encoding; bh=+krWoVZRI0iFhA51rBwg9mzqq8rHQsi9J/F6ndnPhTE=; b=LY32axisnVrJ52ft8GtQ1HXEOK3HG5+SaYH34pZO7VoGsrPQWsn+L5GYi+7q4U7biC PwLkfQDamH54JYt19X6KAhkOR5/ddpkLgy3Dz3uBf7SRzdjxGr8omludHJaYHN33zN59 YEeZiCBNOSIIJ0a08OyVMs8dOwl9kdKjSpSDE= 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:mime-version:content-transfer-encoding; bh=+krWoVZRI0iFhA51rBwg9mzqq8rHQsi9J/F6ndnPhTE=; b=jB55RWqhdQ12n7wCY+gbni5jltT5BGMG/d+oWVZwlqGyfoHmGlQ5/XUJMRFBR21X6T nal93mDxoqvVi7ADAopJ5pi2Ab5fJDCmboKASkv+2lEWcW5M3O1zQ8RT8pDs7qby/Olp UCc4RHApTcK5uYOWF1wskVYM3cXOl63oSYA/AVw+SVTLGRV9960nSKRGQMOXec7AbS1O pfGOP7KQ/z0BHUczqtc4rSAvAoZU/f4TqSKMClsKKjo+hSHXLH1Cyjpucl0o9568r7yg J0LYyoauZCAQFsPpd3hRTCmGKjeIC41oG/zdocyIPmXzvLe5NCSO3D0qoAmy/8nZmstp q4dw== X-Gm-Message-State: AGRZ1gIjqcrRGXVjKftzps6Ex1lsgjeXflOT1QRDG2wjFxyFDhisKpBn 0tUhfRXBUKKHO8kjIeeIwMkk9yRHABs= X-Google-Smtp-Source: AJdET5dmblwAjcwZQJw8GPZUJXev9PUfW2AwbT9ayu866c3HSxwYnNQhq9PDGd3xQkXNQAZnbhK0Rw== X-Received: by 2002:a1c:8a11:: with SMTP id m17-v6mr1635656wmd.15.1541504265543; Tue, 06 Nov 2018 03:37:45 -0800 (PST) Received: from harold.home ([2a01:cb1d:112:6f00:3117:2b29:5ef7:a9e5]) by smtp.gmail.com with ESMTPSA id t187-v6sm1431529wmt.45.2018.11.06.03.37.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Nov 2018 03:37:44 -0800 (PST) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Subject: [PATCH 4/4] efi: reduce the amount of memblock reservations for persistent allocations Date: Tue, 6 Nov 2018 12:37:32 +0100 Message-Id: <20181106113732.16351-5-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181106113732.16351-1-ard.biesheuvel@linaro.org> References: <20181106113732.16351-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181106_033755_015414_F93CEAE0 X-CRM114-Status: GOOD ( 16.64 ) 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: mark.rutland@arm.com, linux-efi@vger.kernel.org, Ard Biesheuvel , marc.zyngier@arm.com, bhsharma@redhat.com, will.deacon@arm.com 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 current implementation of efi_mem_reserve_persistent() is rather naive, in the sense that for each invocation, it creates a separate linked list entry to describe the reservation. Since the linked list entries themselves need to persist across subsequent kexec reboots, every reservation created this way results in two memblock_reserve() calls at the next boot. On arm64 systems with 100s of CPUs, this may result in a excessive number of memblock reservations, and needless fragmentation. So instead, make use of the newly updated struct linux_efi_memreserve layout to put multiple reservations into a single linked list entry. This should get rid of the numerous tiny memblock reservations, and effectively cut the total number of reservations in half on arm64 systems with many CPUs. Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/efi.c | 29 ++++++++++++++------ include/linux/efi.h | 3 ++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c index 85d2ec532816..2ca5a59f7568 100644 --- a/drivers/firmware/efi/efi.c +++ b/drivers/firmware/efi/efi.c @@ -978,22 +978,35 @@ static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock); int efi_mem_reserve_persistent(phys_addr_t addr, u64 size) { struct linux_efi_memreserve *rsv, *parent; - int rsvsize = EFI_MEMRESERVE_SIZE(1); + unsigned long prsv; + int index; if (efi.mem_reserve == EFI_INVALID_TABLE_ADDR) return -ENODEV; - rsv = kmalloc(rsvsize, GFP_KERNEL); - if (!rsv) - return -ENOMEM; - parent = memremap(efi.mem_reserve, sizeof(*rsv), MEMREMAP_WB); - if (!parent) { - kfree(rsv); + if (!parent) return -ENOMEM; + + /* first try to find a slot in an existing linked list entry */ + for (prsv = parent->next; prsv; prsv = rsv->next) { + rsv = __va(prsv); + index = atomic_fetch_add_unless(&rsv->count, 1, rsv->size); + if (index < rsv->size) { + rsv->entry[index].base = addr; + rsv->entry[index].size = size; + + memunmap(parent); + return 0; + } } - rsv->size = 1; + /* no slot found - allocate a new linked list entry */ + rsv = (struct linux_efi_memreserve *)__get_free_page(GFP_KERNEL); + if (!rsv) + return -ENOMEM; + + rsv->size = EFI_MEMRESERVE_COUNT(PAGE_SIZE); atomic_set(&rsv->count, 1); rsv->entry[0].base = addr; rsv->entry[0].size = size; diff --git a/include/linux/efi.h b/include/linux/efi.h index dfce82b2ca8a..1a1a081f7244 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -1727,4 +1727,7 @@ struct linux_efi_memreserve { #define EFI_MEMRESERVE_SIZE(count) (sizeof(struct linux_efi_memreserve) + \ (count) * sizeof(((struct linux_efi_memreserve *)0)->entry[0])) +#define EFI_MEMRESERVE_COUNT(size) (((size) - sizeof(struct linux_efi_memreserve)) \ + / sizeof(((struct linux_efi_memreserve *)0)->entry[0])) + #endif /* _LINUX_EFI_H */