From patchwork Tue Feb 2 00:30:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Salter X-Patchwork-Id: 8185311 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 3C0E2BEEE5 for ; Tue, 2 Feb 2016 00:33:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5686B2026D for ; Tue, 2 Feb 2016 00:33:15 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 72E1C20218 for ; Tue, 2 Feb 2016 00:33:14 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1aQOs7-0000Ui-21; Tue, 02 Feb 2016 00:31:07 +0000 Received: from mx1.redhat.com ([209.132.183.28]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1aQOs4-0000SN-IR for linux-arm-kernel@lists.infradead.org; Tue, 02 Feb 2016 00:31:05 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 890ED8F302; Tue, 2 Feb 2016 00:30:42 +0000 (UTC) Received: from deneb.redhat.com.localdomain (ovpn-113-36.phx2.redhat.com [10.3.113.36]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u120UfGC011654; Mon, 1 Feb 2016 19:30:41 -0500 From: Mark Salter To: Catalin Marinas , Will Deacon Subject: [PATCH] arm64: handle unmapped pages in initrd relocation Date: Mon, 1 Feb 2016 19:30:31 -0500 Message-Id: <1454373031-24218-1-git-send-email-msalter@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160201_163104_676386_DE81A650 X-CRM114-Status: GOOD ( 17.97 ) X-Spam-Score: -7.5 (-------) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ard Biesheuvel , Mark Langsdorf , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Mark Salter MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.7 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Commit 4dffbfc48d65 ("arm64/efi: mark UEFI reserved regions as MEMBLOCK_NOMAP") causes a potential problem in arm64 initrd relocation code. If the kernel uses a pagesize greater than the 4k pagesize used by UEFI, pagesize rounding may lead to one or both ends of the initrd image to be marked unmapped. This leads to a panic when the kernel goes to unpack it. This patch looks for unmapped pages at beginning and end of the initrd image and if seen, relocated the initrd to a new area completely covered by the kernel linear map. Signed-off-by: Mark Salter Cc: Ard Biesheuvel --- arch/arm64/kernel/setup.c | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c index d22c5fc..849566e 100644 --- a/arch/arm64/kernel/setup.c +++ b/arch/arm64/kernel/setup.c @@ -235,24 +235,29 @@ static void __init relocate_initrd(void) phys_addr_t ram_end = memblock_end_of_DRAM(); phys_addr_t new_start; unsigned long size, to_free = 0; + unsigned long unmapped_start = 0, unmapped_end = 0; void *dest; - if (orig_end <= ram_end) + size = orig_end - orig_start; + if (!size) return; /* - * Any of the original initrd which overlaps the linear map should - * be freed after relocating. + * If kernel pagesize > 4K, pagesize rounding may have placed + * part of either end of initrd in an unmapped page. + * + * Find any unmapped bytes at start or end of initrd. */ - if (orig_start < ram_end) - to_free = ram_end - orig_start; + if (!memblock_is_map_memory(orig_start)) + unmapped_start = PAGE_SIZE - (orig_start & (PAGE_SIZE - 1)); + if (!memblock_is_map_memory(orig_end - 1)) + unmapped_end = ((orig_end - 1) & (PAGE_SIZE - 1)) + 1; - size = orig_end - orig_start; - if (!size) + if (unmapped_start == 0 && unmapped_end == 0 && orig_end <= ram_end) return; /* initrd needs to be relocated completely inside linear mapping */ - new_start = memblock_find_in_range(0, PFN_PHYS(max_pfn), + new_start = memblock_find_in_range(0, ram_end, size, PAGE_SIZE); if (!new_start) panic("Cannot relocate initrd of size %ld\n", size); @@ -267,7 +272,30 @@ static void __init relocate_initrd(void) dest = (void *)initrd_start; - if (to_free) { + if (unmapped_end) { + copy_from_early_mem(dest + size - unmapped_end, + orig_start + size - unmapped_end, + unmapped_end); + size -= unmapped_end; + if (size == 0) + return; + } + + if (unmapped_start) { + copy_from_early_mem(dest, orig_start, unmapped_start); + dest += unmapped_start; + orig_start += unmapped_start; + size -= unmapped_start; + if (size == 0) + return; + } + + /* + * Any of the remaining original initrd which overlaps the linear map + * should be freed after relocating. + */ + if (orig_start < ram_end) { + to_free = min(size, (unsigned long)(ram_end - orig_start)); memcpy(dest, (void *)__phys_to_virt(orig_start), to_free); dest += to_free; }