Linux-MIPS Archive mirror
 help / color / mirror / Atom feed
From: "Rong Zhang" <rongrong@oss.cipunited.com>
To: "Huacai Chen" <chenhuacai@kernel.org>,
	 "Jiaxun Yang" <jiaxun.yang@flygoat.com>,
	 "Thomas Bogendoerfer" <tsbogend@alpha.franken.de>
Cc: "Rong Zhang" <rongrong@oss.cipunited.com>, "Yao Zi" <me@ziyao.cc>,
	 <linux-mips@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	 "Icenowy Zheng" <uwu@icenowy.me>, "Rong Zhang" <i@rong.moe>,
	 <stable@vger.kernel.org>
Subject: [PATCH v2] MIPS: Loongson64: env: Check UARTs passed by LEFI cautiously
Date: Mon, 16 Mar 2026 01:28:22 +0800	[thread overview]
Message-ID: <20260315172824.352412-1-rongrong@oss.cipunited.com> (raw)
In-Reply-To: <20260314211336.408561-1-rongrong@oss.cipunited.com>

Some firmware does not set nr_uarts properly and passes empty items.
Iterate at most min(system->nr_uarts, MAX_UARTS) items to prevent
out-of-bounds access, and ignore UARTs with addr 0 silently.

Meanwhile, our DT only works with UPIO_MEM but theoretically firmware
may pass other IO types, so explicitly check against that.

Tested on Loongson-LS3A4000-7A1000-NUC-SE.

Fixes: 3989ed418483 ("MIPS: Loongson64: env: Fixup serial clock-frequency when using LEFI")
Cc: stable@vger.kernel.org
Reviewed-by: Yao Zi <me@ziyao.cc>
Signed-off-by: Rong Zhang <rongrong@oss.cipunited.com>
---
Changes in v2:
- Sort new includes alphabetically (thanks Yao Zi)
---
 arch/mips/loongson64/env.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/arch/mips/loongson64/env.c b/arch/mips/loongson64/env.c
index 11ddf02d6a15..7abcca7ab4ed 100644
--- a/arch/mips/loongson64/env.c
+++ b/arch/mips/loongson64/env.c
@@ -17,7 +17,9 @@
 #include <linux/dma-map-ops.h>
 #include <linux/export.h>
 #include <linux/libfdt.h>
+#include <linux/minmax.h>
 #include <linux/pci_ids.h>
+#include <linux/serial_core.h>
 #include <linux/string_choices.h>
 #include <asm/bootinfo.h>
 #include <loongson.h>
@@ -106,9 +108,23 @@ static void __init lefi_fixup_fdt(struct system_loongson *system)
 
 	is_loongson64g = (read_c0_prid() & PRID_IMP_MASK) == PRID_IMP_LOONGSON_64G;
 
-	for (i = 0; i < system->nr_uarts; i++) {
+	for (i = 0; i < min(system->nr_uarts, MAX_UARTS); i++) {
 		uartdev = &system->uarts[i];
 
+		/*
+		 * Some firmware does not set nr_uarts properly and passes empty
+		 * items. Ignore them silently.
+		 */
+		if (uartdev->uart_base == 0)
+			continue;
+
+		/* Our DT only works with UPIO_MEM. */
+		if (uartdev->iotype != UPIO_MEM) {
+			pr_warn("Ignore UART 0x%llx with iotype %u passed by firmware\n",
+				uartdev->uart_base, uartdev->iotype);
+			continue;
+		}
+
 		ret = lefi_fixup_fdt_serial(fdt_buf, uartdev->uart_base,
 					    uartdev->uartclk);
 		/*

base-commit: 267594792a71018788af69e836c52e34bb8054af
-- 
2.53.0

  parent reply	other threads:[~2026-03-15 17:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-14 21:13 [PATCH] MIPS: Loongson64: env: Check UARTs passed by LEFI cautiously Rong Zhang
2026-03-15 11:41 ` Yao Zi
2026-03-15 17:28 ` Rong Zhang [this message]
2026-03-17 10:58   ` [PATCH v2] " Jiaxun Yang
2026-04-01 20:28   ` Thomas Bogendoerfer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260315172824.352412-1-rongrong@oss.cipunited.com \
    --to=rongrong@oss.cipunited.com \
    --cc=chenhuacai@kernel.org \
    --cc=i@rong.moe \
    --cc=jiaxun.yang@flygoat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=me@ziyao.cc \
    --cc=stable@vger.kernel.org \
    --cc=tsbogend@alpha.franken.de \
    --cc=uwu@icenowy.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).