From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46F4B606A32F for ; Sat, 5 Dec 2026 06:36:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=dWAHIuya; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=JZ+VejAW DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=2qUgm3mLqogWOOXE6EFh4JUZ2Lpbw+wSILMCe/Sj0ek=; b=dWAHIuya/Lxt8X UqGemQa7Yksb9pmWHvccGZ2cmMHMe08+vsq5uSNbDSZ0YKi/3KWcLevMlwDviQa5MK7KzvI67I/Du Rps5zKa87hhEfWYMZUUDa2NCRwrspXNtPGQI7jTU/Q98pRBPlvE+fSGW5iTB+YVdEVbbKxVfd1Gw1 1TEAH/1qnNYez3ZJu0pXWGWwKBErh3gz5lVvPEwvlmA5EoyaKO82NojBuoCgvkbGNI7/StY1buOAF BZlDah3AV3rX3z/CS+rEcLODNBFkXkYjjvF5tr1TFqkK26POGQqz1wnv6Qd+U8wtAB65F40kKYLbo eLGMwV/R4ySPJcH/Or0g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZjNA-0000000249a-3oXB; Sat, 05 Dec 2026 06:35:52 +0000 Received: from mail-dy1-x1332.google.com ([2607:f8b0:4864:20::1332]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZjN8-0000000248U-2WWZ for linux-arm-kernel@lists.infradead.org; Sat, 05 Dec 2026 06:35:51 +0000 Received: by mail-dy1-x1332.google.com with SMTP id 5a478bee46e88-3282db206d3so8756eec.0 for ; Fri, 04 Dec 2026 22:35:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796452549; x=1797057349; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=2qUgm3mLqogWOOXE6EFh4JUZ2Lpbw+wSILMCe/Sj0ek=; b=JZ+VejAWhizE+33RcIqjLOXnxOe8H/dLtYBczcVL6/VR2WI2p5kZdXDLn5sCxmy2g0 Smv/o4rqKSR8RlUoPQBaJbKenW8CadPV8lCWwL/a99C4UP/0hAI2D+zQYwz/CuM2kV0X A6Sw6xDOru6rz4QsWiPEpbhHd7ZmOLnc2EskcW0XcsxX2u3/kJ41O93OsmKdEVrb4+z3 PzsaUPCx5oIsCln40XwEu6myL9AkQLuqyV394XXkJd8qhu60W7+17dsKaLSEJhuNNGZQ DuKqjt6f7woAUqwVof5FmUKmtK4hECMDJ54GegpqsWuRhwvKZmKxDVNHZsQeKSxwpm+V wEKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796452549; x=1797057349; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=2qUgm3mLqogWOOXE6EFh4JUZ2Lpbw+wSIL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fdF2fnEZYruQGLEIQEI500YQH3hDwT3XfMdcDpqIE8GpO8jdSS1Z5 lVC13p7a0+71eUQdUQyYcx2mhJ6tU6c3dj/A9Oowc7RFzZHtGeNUwLRLyhIkuEzncU4VLOnyaqN eAlj/Hcn3N9kJeIbwE8Nz63jwxAWW0HZKqX1nULBRHaM3TRxu9kyyeelzQOc/+GW0jc= X-Received: by 2002:a05:7300:33a8:b0:386:cf0c:8482 with SMTP id 5a478bee46e88-387521448d8mr4072244eec.24.1796452549507; Fri, 04 Dec 2026 22:35:49 -0800 (PST) Received: from localhost.localdomain (wood.seclab.cs.ucsb.edu. [128.111.49.37]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3875218c602sm20884183eec.7.2026.12.04.22.35.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 22:35:49 -0800 (PST) From: Praneeth Balasubramanian To: Richard Genoud , Greg Kroah-Hartman , Jiri Slaby , Nicolas Ferre , Alexandre Belloni , Claudiu Beznea Cc: Praneeth Balasubramanian , stable@vger.kernel.org, linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH 3/4] tty: atmel_serial: take the port lock in console write Date: Fri, 4 Dec 2026 22:35:35 -0800 Message-Id: <20261205063536.2519566-4-praneethb2002@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20261205063536.2519566-1-praneethb2002@gmail.com> References: <20261205063536.2519566-1-praneethb2002@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261204_223550_667151_D6BBCF2B X-CRM114-Status: GOOD ( 21.10 ) X-Spam-Score: -1.8 (-) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-16.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 atmel_console_write() touches the PDC and USART TX control registers and the tx_stopped flag without holding the port lock. atmel_stop_tx() and atmel_start_tx() run under the lock. So the console can race with a TCXONC ioctl or another path that stops the transmitter. The comment that says interrupts are disabled on entry does not hold on PREEMPT_RT. There legacy consoles are printed from a kthread, and con->write runs in task context with interrupts enabled. If atmel_stop_tx() runs while the console is between its PDC_TXTEN snapshot and the TXTEN restore, the console re-enables a transmitter that was just stopped and writes back a stale PDC_TXTEN value. The port ends up in an inconsistent state and serial output can stall. Take the port lock for the whole callback so the register accesses are serialized against the other paths. Drop the stale comment. Follow the pl011 pattern: skip the lock when a sysrq is being handled on the port, since atmel_rx_chars() calls uart_handle_sysrq_char() under the port lock, and use a trylock when an oops is in progress. This was found by an LLM-based audit that checks code against the rules stated in kernel comments. Fixes: 39d4c922b596 ("atmel_serial: fix uart/console concurrent access") Cc: stable@vger.kernel.org Signed-off-by: Praneeth Balasubramanian --- Notes: This was found by an LLM-based audit, part of research at UCSB SecLab. An LLM turned a kernel comment into a rule, and three LLM votes checked the code against it. The fix and changelog were drafted with GLM-5.3, then checked against the source and partly rewritten with Claude Opus 5.5. I reviewed the patch and take responsibility for it. Testing: builds without new warnings with W=1 (x86_64 allmodconfig), clang 23. checkpatch.pl is clean. Not runtime-tested, and there is no reproducer. I do not have this hardware. James Hilliard's serial port lock series (v14, 2026-12-04) changes other functions in atmel_serial.c, not atmel_console_write(). The two may need a rebase against each other. drivers/tty/serial/atmel_serial.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/drivers/tty/serial/atmel_serial.c b/drivers/tty/serial/atmel_serial.c index c3880259e3bc..7446c956262a 100644 --- a/drivers/tty/serial/atmel_serial.c +++ b/drivers/tty/serial/atmel_serial.c @@ -2560,15 +2560,21 @@ static void atmel_console_putchar(struct uart_port *port, unsigned char ch) atmel_uart_write_char(port, ch); } -/* - * Interrupts are disabled on entering - */ static void atmel_console_write(struct console *co, const char *s, u_int count) { struct uart_port *port = &atmel_ports[co->index].uart; struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); unsigned int status, imr; unsigned int pdc_tx; + unsigned long flags; + int locked = 1; + + if (port->sysrq) + locked = 0; + else if (oops_in_progress) + locked = uart_port_trylock_irqsave(port, &flags); + else + uart_port_lock_irqsave(port, &flags); /* * First, save IMR and then disable interrupts @@ -2601,6 +2607,9 @@ static void atmel_console_write(struct console *co, const char *s, u_int count) /* set interrupts back the way they were */ atmel_uart_writel(port, ATMEL_US_IER, imr); + + if (locked) + uart_port_unlock_irqrestore(port, flags); } /* -- 2.25.1