From patchwork Fri Jan 19 20:12:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 13524141 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 D7ACEC47DD3 for ; Fri, 19 Jan 2024 20:14:46 +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=oaFPf3RM+bV7wExR/R9VCOnx89ioY6nRCVr383ytcsA=; b=uzGWU9VzCPYI38 XK/5ZwwIhe3OcOcJ0IzN9uCDz86GxAcZAd9ttxy6a3/DLteXqOldmm5eOQUPdTc07Pi4cy0c5TTPg FeT/OVVCbsqmsjQzIJTqrKmzTyYLR1BAGeiP2jpim8SDVt7CDZ3mK4oJluih4olgVDyP0RUcmrRzu uOx8vr16e+G/R52WqZ0ewLIYoSYO+Kh9X45HeiIAX18TnGj0h1o5gYDzQ5IOrlE2KtpwGeZ/Mrihb 8YGvMGWH7EMG4dEzYGKX/hhfebomss4qzZlovwMpmvk+jhvddmpDgLEQvdxGZ6Lq/ChBf80337GsG kv2nlKhWhs79i4Nm4tAw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rQvFs-006Xfm-0M; Fri, 19 Jan 2024 20:14:20 +0000 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rQvFl-006XdB-2H for linux-arm-kernel@lists.infradead.org; Fri, 19 Jan 2024 20:14:15 +0000 Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-3376555b756so721372f8f.0 for ; Fri, 19 Jan 2024 12:14:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705695250; x=1706300050; 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=e35XnQv7+JeMDazmnVaku5/bw/ATFIlRQ/6xz7OKU3s=; b=gv2yl4IuwOWSRC3KwHmskYHAPcSw1Hk+qD1axjBaci2xT5WsxwGu3Y0zDRJCAEGLIR nXKatFKCwWi45PgJB4LIV1HhYMwigvZsxkFKA62yNjiLIpbcN5lTbp9nJJnz7mlTUccl AiMDrYG6bde8qv/hlN+5UZ2ObkKp/B5nokTsDGgS+faU8+M0Df2lQZ/zdp5G6TMJCXrr StHc+Amj1vCt49HIPYWS4035yj9aEtDAAmx6mPJuKIGUTmMSrpr8VqOC5QHjYD1PaqkX +ggNUHzJCeaUxlOX3cjOWB3l+PjqLOYGPWU6zdJDDVb74Z2F1TManQ8r1h38cVUTL3z8 QnPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705695250; x=1706300050; 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=e35XnQv7+JeMDazmnVaku5/bw/ATFIlRQ/6xz7OKU3s=; b=r+fG8y82fclglF6qHozhK0lFigtZyx0knEDEw0Olqmwo7U3LuavOwTxxM6DBt8ZLVn KX0NHbxwspUBAzJMNgqD5GnVeYn2Y4oeT3f98d/sr7aV5MfdjCZBBp3q5aE9y++fiNSe +3YwrBFEQY9PnuHNuMaiaf6hboFoLDB/keoq0engmfbQ1SwlnoBUZyWjdu/tE924q1Wz QT8Cvk8FPdSksOvRRqXMgbP3e0Oy3MMOVlwQJlIvxymL5885vPR7qsEmJx63jx9YZpqm PndNIlHPATHL3xWQkD245d351y1jWDmt2ohyUwyVWIFWKOKqANoFY/ha6dMsNlhlxeFc wjUQ== X-Gm-Message-State: AOJu0Yx6ofq0H2vh4BhMkvltCQzK/VYI7GkTsCIkMafXBvJzb7tp5cB5 O8vusY4l+reb44m0G/26PdZzcQgkhoBf0yiwggmdd9hVbv+57geg X-Google-Smtp-Source: AGHT+IFwN1VEXA51pQLlXmt/K6R/WGzdCtBOcilbAPksqwEOjCe5BbskxHNB7jfp5oXi4o+U0z8Wrw== X-Received: by 2002:adf:fdcd:0:b0:337:d85d:87f2 with SMTP id i13-20020adffdcd000000b00337d85d87f2mr977177wrs.8.1705695249639; Fri, 19 Jan 2024 12:14:09 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id q5-20020adff505000000b00339214d70b5sm2084788wro.85.2024.01.19.12.14.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 12:14:09 -0800 (PST) From: Christian Marangi To: Russell King , Arnd Bergmann , Andrew Morton , Geert Uytterhoeven , "Kirill A. Shutemov" , Jonathan Corbet , Thomas Gleixner , Randy Dunlap , Christian Marangi , "Mike Rapoport (IBM)" , Eric DeVolder , Nathan Chancellor , "Russell King (Oracle)" , Kees Cook , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Bjorn Andersson , Konrad Dybcio , Linus Walleij , John Crispin Subject: [PATCH 1/2] ARM: decompressor: support memory start validation for appended DTB Date: Fri, 19 Jan 2024 21:12:44 +0100 Message-ID: <20240119201356.7903-2-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240119201356.7903-1-ansuelsmth@gmail.com> References: <20240119201356.7903-1-ansuelsmth@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240119_121413_742396_12F032F9 X-CRM114-Status: GOOD ( 27.30 ) 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 Fri Jan 19 20:12:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 13524140 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 94A24C47DD3 for ; Fri, 19 Jan 2024 20:14:44 +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=fu4JQaIOiOYhU+kNBW8bkVYzGh2QGvRKwkyzWij8BrM=; b=bdLhmPgbfJltsl KEjh3EmQXIEmX5pVNvVTQeUNdx1XJmTYcpQxrz7rqc5NZ/NYn8yDROJKWU27dOdMQhA66xxtzwuMl RBAvsdbBHXhVW85Tz6Kh8eZvWIZTRf4MPL90SOUl4qeq840Qj0iQZ7BoZRTe3TU4YcntxfOHRY145 2ois+Jx7ch6FyJba2VBojiEYhcx0g2HdkB3lSmhIn0w+b9hrmi3/bf4rop+e/GWZ+qP9A0Vx8lZ3Y FyaJVPUDbp8yg/XmyWtY0FqXTInW0CMU6D4AHdoBk+LIQRg0P5AaXtAt7rHy/K2dnt/YjMvif2gRf Y/pDtXtpHKfXSxfOojag==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rQvFs-006Xg7-29; Fri, 19 Jan 2024 20:14:20 +0000 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rQvFm-006XdF-2p for linux-arm-kernel@lists.infradead.org; Fri, 19 Jan 2024 20:14:16 +0000 Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-40ea084ec14so5785385e9.2 for ; Fri, 19 Jan 2024 12:14:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705695251; x=1706300051; 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=cslHUuioJKZUdWZhA350tTceu4m4Qgt6DILKRrYZ0CA=; b=WKcEy1eFdnY+eTJ1Dr7yJC9tfjY+2ViDOnGv9Qfs88HWy90G81vykxOTuK3PJGxQv8 zdCc/uVrnF5a1px3yxxrW9GGZ9ZB/KwEQUhfNQweNyLWBn2jIcRqjJmg7KxPbm7iA7Mq zQXyCcyfZZ9CE1LjLOlPSkwtiKgtnJqdtq+CcYymnxnpYD+ig7pnTmjpEOEwC0PQyiCv Af0m070s0uvpZUzelIo84kL5pxHni7LvHsmFeeflIvEvkD9ZmtLpHGL5ShgOGtXv2dzl cAnGLwTAoJ/smEbGl14KWT6NJY0CFf5dUE54fTsYUi/NOacVSMGqtQy9GKUrbSKUqkMY hx2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705695251; x=1706300051; 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=cslHUuioJKZUdWZhA350tTceu4m4Qgt6DILKRrYZ0CA=; b=GJ+Y/CadkGKFrRzMtrZfaJfPAzDd8JmfGVCVLzJ41xAUEpX8Tf2fqnrRp433dWB9Wy ftuxZZS8a/r8zr7bjz/yn0WlvHvjEFo5gHXkppqCLLh15P/ykgZxKFU9RG6wgPqFcCPp 3b6yPQGwb6HPMI8qMEhUzcCH6mq3/yuK9wQbkJZ/78hw5/KLpe2ceGzqIHUi9Y4LzPft xWLgifticy00xumdF8YF4XVsIEfVSTPaLHnP74LeNKD2ALUvTueDAIA5cTsvC9jwzEOj XUf7RBHbS8tJIwZ7FnNCIQvnekJoKUZQvSack5eQ2c9tGYDqDNnfw/nin5SdfqSHC/yT CAsw== X-Gm-Message-State: AOJu0Ywf48Uci66mIxxM6oKJtNfZ9NKtX70znY4984bdljDWopmHVp+2 KBan2kecZ7hf2TNpWpOvvo9h373zbX3ohZWs0fOMkFVGe0yNvn8a X-Google-Smtp-Source: AGHT+IFOTbdL41cPmnp9k4iSwF+mjvZLoMetREkJ8ghXgjbgzt/oHLxO7qTkVIPVPM4SNAVG2Qk5Pw== X-Received: by 2002:a1c:7404:0:b0:40e:6b8e:5ab2 with SMTP id p4-20020a1c7404000000b0040e6b8e5ab2mr189320wmc.106.1705695250892; Fri, 19 Jan 2024 12:14:10 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id q5-20020adff505000000b00339214d70b5sm2084788wro.85.2024.01.19.12.14.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 12:14:10 -0800 (PST) From: Christian Marangi To: Russell King , Arnd Bergmann , Andrew Morton , Geert Uytterhoeven , "Kirill A. Shutemov" , Jonathan Corbet , Thomas Gleixner , Randy Dunlap , Christian Marangi , "Mike Rapoport (IBM)" , Eric DeVolder , Nathan Chancellor , "Russell King (Oracle)" , Kees Cook , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Bjorn Andersson , Konrad Dybcio , Linus Walleij , John Crispin Subject: [PATCH 2/2] ARM: decompressor: add option to ignore MEM ATAGs Date: Fri, 19 Jan 2024 21:12:45 +0100 Message-ID: <20240119201356.7903-3-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240119201356.7903-1-ansuelsmth@gmail.com> References: <20240119201356.7903-1-ansuelsmth@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240119_121414_919240_485EB247 X-CRM114-Status: GOOD ( 18.31 ) 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 | 10 ++++++++++ 2 files changed, 22 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..189db9fc7fea 100644 --- a/arch/arm/boot/compressed/atags_to_fdt.c +++ b/arch/arm/boot/compressed/atags_to_fdt.c @@ -10,6 +10,12 @@ #define do_extend_cmdline 0 #endif +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_IGNORE_MEM) +#define do_ignore_mem 1 +#else +#define do_ignore_mem 0 +#endif + #define NR_BANKS 16 static int node_offset(void *fdt, const char *node_path) @@ -170,6 +176,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 (do_ignore_mem) + continue; + if (memcount >= sizeof(mem_reg_property)/4) continue; if (!atag->u.mem.size)