From patchwork Sun Jan 21 20:29:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 13524673 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 88659C47DB7 for ; Sun, 21 Jan 2024 20:31:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type: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:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=nDoB+RYgAwfhAzu47MOPyYEplYa4vq6euLQzUOdQveQ=; b=JbmDFtSi01hQKI +aP59X8nrIn/yZhnCxpsI53/oGy49ZKyVn/iuC9N0JFQZLaTrLYtEaXVUDN9lodWulM9YSBP6m2XY uAesHM5TJhOOG0csH1UmdiYSeSoK579t5oh35bRX6Ge8+98HYdSyqLL/3FC7ZAzk6DATyCrXn5fUn bPmQnxzcfY+PKEmn0wB7PdAtMlR1APpA5hc5i/4N1wDux5jxDTAcxXsOs4vQRrmZ99SRLLv3VFx0H M2zUjs+Oq9VnmDrdFBou2a78GA4MdGdFNvi7Ey6yY8JYZ1eoRmc/7faM/Cw5iKOToriIvmSRxR/0E 1/W49wiqWtSOmoiChe0A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rReSh-00A0CB-1d; Sun, 21 Jan 2024 20:30:35 +0000 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rReSb-00A0AT-1m for linux-arm-kernel@lists.infradead.org; Sun, 21 Jan 2024 20:30:30 +0000 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-40e779f030aso14472825e9.0 for ; Sun, 21 Jan 2024 12:30:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705869024; x=1706473824; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=MFOh+wFZ3gZL48mISx80VE5NCNzEFuaISyyYFrZmaso=; b=Y8chlGSjYcOr0QCnxtMwUKIUJZnKmpCSrU5fpwO6nuqaNlX5bsHCM5OzkXZtqsmvEc htZD+8SSiPo6Miui8tql6mdd8ohkzndAwfdFCPPgOkljLLg6SviDD69zxhs2nwlDnzKz uEyzxl+wMqT2zLKPNnNw4Odz1CdcRwgzTEhTIC8Exdurfl+rqOqCAIdQPJ9zraMyhMIA lMRTPHTysBdt768OX4WTlgiF9r1uZyHWsh2U/YKp5FglsBzZQWKF2PJM5gOa4rXWCGsB Fu/aH0JROv7OYYFzy/w8ORZGtnmWRaUobc19PaFP74FZaFTJ/oiSCx+7TegGkmZh5ckV Qbwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705869024; x=1706473824; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MFOh+wFZ3gZL48mISx80VE5NCNzEFuaISyyYFrZmaso=; b=HZjm+vlI7Bk0JNi/kuF5bBdQMQd941lf8N9xcKmjbd8BdWuZJwKiDGoa8nrkuOwRqg g044oOJynmY3F8NN/4jP+1D2dEnEFidCKi2KFAk4oVTvJLVFREx4lg7pXXvNKRGHg2NK J1RRO/+PWFKTNVyXl/urfarjOwt+dvM4HzA3O11DDIQe9HQBgf2Aytxj6reeQpaB2bN+ y8Jsa/vHDK+QcdedJXS0ikwnhkreAKZXvtIUrR+/7ksU5KXtY7YjNmUnMyKCE9ltDN92 dSzA0jLsI8dNlcPauKsN/g860pDX15ohJ38czrV0bHZdVAiwpMTquz8a2HEkVLLFnrp6 mB5g== X-Gm-Message-State: AOJu0Yy6hyUphiEgJo7przdXsierKH/toIUUT8hxRFIhq4ThOOrg+OcY 8WquFDAI79Eid5hGf19jC3t7bSJIojfl5f+NIp+DuOk/NdnclApn X-Google-Smtp-Source: AGHT+IGa17ieGCLLHAfKtw0851dG8rPii45/t1n8te6xVLjBjl/hUzn13sgCBWZnrl1l/bWLAvPPLw== X-Received: by 2002:a05:600c:a06:b0:40e:44bd:dc39 with SMTP id z6-20020a05600c0a0600b0040e44bddc39mr1314341wmp.84.1705869024443; Sun, 21 Jan 2024 12:30:24 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id j5-20020a05600c1c0500b0040e9f7dadc6sm9021967wms.25.2024.01.21.12.30.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 12:30:24 -0800 (PST) From: Christian Marangi To: Russell King , Arnd Bergmann , Andrew Morton , Geert Uytterhoeven , Linus Walleij , "Kirill A. Shutemov" , Thomas Gleixner , Jonathan Corbet , Marc Zyngier , Christian Marangi , "Mike Rapoport (IBM)" , Eric DeVolder , Nathan Chancellor , Kees Cook , "Russell King (Oracle)" , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Bjorn Andersson , Konrad Dybcio , John Crispin Subject: [PATCH v2 1/2] ARM: decompressor: support memory start validation for appended DTB Date: Sun, 21 Jan 2024 21:29:33 +0100 Message-ID: <20240121203009.9257-2-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240121203009.9257-1-ansuelsmth@gmail.com> References: <20240121203009.9257-1-ansuelsmth@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240121_123029_591553_C82C9329 X-CRM114-Status: GOOD ( 27.18 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org There is currently a problem with a very specific sets of kernel config and AUTO_ZRELADDR. For the most common case AUTO_ZRELADDR check the PC register and calculate the start of the physical memory. Then fdt_check_mem_start is called to make sure the detected value makes sense by comparing it with what is present in DTB in the memory nodes and if additional fixup are required with the use of linux,usable-memory-range in the chosen node to hardcode usable memory range in case some reserved space needs to be addressed. With the help of this function the right address is calculated and the kernel correctly decompress and loads. Things starts to become problematic when in the mix, CONFIG_ARM_APPENDED_DTB is used. This is a particular kernel config is used when legacy systems doesn't support passing a DTB directly and a DTB is appended at the end of the image. In such case, fdt_check_mem_start is skipped in AUTO_ZRELADDR iteration as the appended DTB can be augumented later with ATAGS passed from the bootloader (if CONFIG_ARM_ATAG_DTB_COMPAT is enabled). The main problem and what this patch address is the fact that fdt_check_mem_start is never called later when the appended DTB is augumented, hence any fixup and validation is not done making AUTO_ZRELADDR detection inconsistent and most of the time wrong. Add support in head.S for this by checking if AUTO_ZRELADDR is enabled and calling fdt_check_mem_start with the appended DTB and the augumented values permitting legacy device to provide info in DTB instead of disabling AUTO_ZRELADDR and hardcoding the physical address offsets. Signed-off-by: Christian Marangi Reviewed-by: Geert Uytterhoeven Reviewed-by: Linus Walleij --- arch/arm/boot/compressed/head.S | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S index 9f406e9c0ea6..2ff38a8df1f0 100644 --- a/arch/arm/boot/compressed/head.S +++ b/arch/arm/boot/compressed/head.S @@ -443,6 +443,28 @@ restart: adr r0, LC1 add r6, r6, r5 add r10, r10, r5 add sp, sp, r5 + +#ifdef CONFIG_AUTO_ZRELADDR + /* + * Validate calculated start of physical memory with appended DTB. + * In the first iteration for physical memory start calculation, + * we skipped validating it as it could have been augumented by + * ATAGs stored at an offset from the same start of physical memory. + * + * We now have parsed them and augumented the appended DTB if asked + * so we can finally validate the start of physical memory. + * + * This is needed to apply additional fixup with + * linux,usable-memory-range or to make sure AUTO_ZRELADDR detected + * the correct value. + */ + sub r0, r4, #TEXT_OFFSET @ revert to base address + mov r1, r8 @ use appended DTB + bl fdt_check_mem_start + + /* Determine final kernel image address. */ + add r4, r0, #TEXT_OFFSET +#endif dtb_check_done: #endif From patchwork Sun Jan 21 20:29:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 13524672 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8D695C47422 for ; Sun, 21 Jan 2024 20:30:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type: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:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=/BY7pOqOEbV8CVztHHajZZ7IyqPdIpfiIRbvsLhVprQ=; b=zDG5P3oxi30fF6 vMiV57HQ3O8wnNOzfpq+lala40RO987jLptXU7SY2VuaODNIvI/xDAAiuc9w/lF78Lvk136bAVoVi jEFt1Uhl0XMymGmHCBWzkRlCMSKzk3Eoee+JJmWeU//iLBl+6pRf+QpQSNRqHesFfVKwUQJMrEs99 r9ZBKzim72JN8iSi0YIJEKJfSl10FxHKzewP76w2T3dkATQnk/hexKerocPEopq/wEfx7c4vcW2Y4 4XYCpUjlPWfAGQuN3ct9+8zJ4Q21l3iZh5MznH0lk/lVQlCO0KrpyV8HEkyhY3mASRWcRDlt8yf6g 1LuYRDdRHjeDVAMkVMYw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rReSg-00A0Bw-2w; Sun, 21 Jan 2024 20:30:34 +0000 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rReSb-00A0AW-1m for linux-arm-kernel@lists.infradead.org; Sun, 21 Jan 2024 20:30:30 +0000 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-40e8fec0968so33533365e9.1 for ; Sun, 21 Jan 2024 12:30:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705869026; x=1706473826; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=njA8TuiX4/CIlcjU6qTOSBsn0Z7EKlFnGPhxFsmpsIY=; b=TuVyPseIsJr5+D8fd370xYnuPebYJ0tGIz/JyQTQn9xPlaiKmt5Sy1eNACvHLuterx HgL+wN+PSh4BJZzvXeYgodLXw1jQgRJAtVBfEdqXefB5o4rs+FfndFRFAAq+ohBUK69a PK0ffSAPLsfzT88hsaF2J0+PfYDWxb4DRB/NBYVi3+wlEntn1v8qlsq43pHzS8dKWmBC TbPnxlPnaubtA1r42ZL52bjxfvvPEQCaTrp4+7vzjzq9Rz68yOMhNKwqtweKwnL+6xnw Bgelpapf24Y9rcVBRzLnwRzHCxYMQpWc4KLEx9sjRdEMepTtzNdrgIvY+VeejH8F5AZ/ 4fSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705869026; x=1706473826; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=njA8TuiX4/CIlcjU6qTOSBsn0Z7EKlFnGPhxFsmpsIY=; b=T5Qf3Avx3CqGkIgTuPW2o8e3Ms/1yjtufebCy1ZD7+dl6lvOAW34FG6h9l8WQsImjE KkMEBbg4FdN4yxoJR84nGzyPTq8XtTKkchhcSFf258XUhf6xxtc3poBXBI2ByjDR4Zwk sPTy6/wFcTq9LKgjEsPwqaRZgba4eNrFZUokbqDRRnCtV40kq7eS/ax/lBGcLuLAvLAE I9Lk1HAK/67Qk7fUWviDtHn5WgFCw/Y0eN8aXEJ5rL/jFvJu716OSZITiC0DztNTTHf6 jRT+KErvejOY7TnxE/Q2fc+OJm6owuOLkshzCXrB+Ypl9u2c/alpGwP8uGiVr4IDA216 T0xg== X-Gm-Message-State: AOJu0Yy7oDzyXumQfuVQy14GhvpKNy1Lwb7v6TWGLbmeLF5zNMSz/bFD ATMHzL25j5NRsAxr+1OJCssnpJo8CteioJ4D32SlNtHwUM2Sujw2bTpjcrfQ X-Google-Smtp-Source: AGHT+IE4DRxICmPiz2zCdXgFL6qIV+pc0m2t7s6qhqFbkIpFndXOczB4KLmQjjL0M52jhmwTacoSHg== X-Received: by 2002:a05:600c:6907:b0:40e:88a8:2185 with SMTP id fo7-20020a05600c690700b0040e88a82185mr1825036wmb.176.1705869025687; Sun, 21 Jan 2024 12:30:25 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id j5-20020a05600c1c0500b0040e9f7dadc6sm9021967wms.25.2024.01.21.12.30.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 12:30:25 -0800 (PST) From: Christian Marangi To: Russell King , Arnd Bergmann , Andrew Morton , Geert Uytterhoeven , Linus Walleij , "Kirill A. Shutemov" , Thomas Gleixner , Jonathan Corbet , Marc Zyngier , Christian Marangi , "Mike Rapoport (IBM)" , Eric DeVolder , Nathan Chancellor , Kees Cook , "Russell King (Oracle)" , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Bjorn Andersson , Konrad Dybcio , John Crispin Subject: [PATCH v2 2/2] ARM: decompressor: add option to ignore MEM ATAGs Date: Sun, 21 Jan 2024 21:29:34 +0100 Message-ID: <20240121203009.9257-3-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240121203009.9257-1-ansuelsmth@gmail.com> References: <20240121203009.9257-1-ansuelsmth@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240121_123029_591471_58A4DED2 X-CRM114-Status: GOOD ( 17.84 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Some bootloaders can pass broken MEM ATAGs that provide hardcoded information about mounted RAM size and physical location. Example booloader provide RAM of size 1.7Gb but actual mounted RAM size is 512Mb causing kernel panic. Add option CONFIG_ARM_ATAG_DTB_COMPAT_IGNORE_MEM to ignore these ATAG and not augument appended DTB memory node. Signed-off-by: Christian Marangi Acked-by: Linus Walleij --- arch/arm/Kconfig | 12 ++++++++++++ arch/arm/boot/compressed/atags_to_fdt.c | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index b2ab8db63c4b..6bb5c6b28106 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -1549,6 +1549,18 @@ config ARM_ATAG_DTB_COMPAT bootloaders, this option allows zImage to extract the information from the ATAG list and store it at run time into the appended DTB. +config ARM_ATAG_DTB_COMPAT_IGNORE_MEM + bool "Ignore MEM ATAG information from bootloader" + depends on ARM_ATAG_DTB_COMPAT + help + Some bootloaders can pass broken MEM ATAGs that provide hardcoded + information about mounted RAM size and physical location. + Example booloader provide RAM of size 1.7Gb but actual mounted RAM + size is 512Mb causing kernel panic. + + Enable this option if MEM ATAGs should be ignored and the memory + node in the appended DTB should NOT be augumented. + choice prompt "Kernel command line type" if ARM_ATAG_DTB_COMPAT default ARM_ATAG_DTB_COMPAT_CMDLINE_FROM_BOOTLOADER diff --git a/arch/arm/boot/compressed/atags_to_fdt.c b/arch/arm/boot/compressed/atags_to_fdt.c index 627752f18661..b5bce4dad321 100644 --- a/arch/arm/boot/compressed/atags_to_fdt.c +++ b/arch/arm/boot/compressed/atags_to_fdt.c @@ -170,6 +170,10 @@ int atags_to_fdt(void *atag_list, void *fdt, int total_space) setprop_string(fdt, "/chosen", "bootargs", atag->u.cmdline.cmdline); } else if (atag->hdr.tag == ATAG_MEM) { + /* Bootloader MEM ATAG are broken and should be ignored */ + if (IS_ENABLED(CONFIG_ARM_ATAG_DTB_COMPAT_IGNORE_MEM)) + continue; + if (memcount >= sizeof(mem_reg_property)/4) continue; if (!atag->u.mem.size)