From patchwork Mon Jun 9 07:43:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunhui Cui X-Patchwork-Id: 895736 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8AB9C280337 for ; Mon, 9 Jun 2025 07:44:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749455050; cv=none; b=mTVZFpNbqR3yWVBn5tiJwDh3oM2Jvzvv327983oTJW9RAPeNh8NfvQz4QS4TA6LXLzBYNcce6P1r9BsvPE/WzmYptHeafLHpEGA1LNT4+Ckk+zq37aLORQyTgN0UxSuTSpHQvpy/dpewjyanJisE2dNDmaO+u+C4zfgCsy7Znqg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749455050; c=relaxed/simple; bh=Yay0E1IktJ5U3dv9Jr5sXBr5qazP/zu1WzMA4ShslGg=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=EGG6wY0HwNS5iRXikTPpaoPS1XTNu3nO9ob7HbP6EYnCEqahZY2s9ajNEjcbQcLf++oxJqfqK1l8Wdp86u08wgTYJCH65IfBp6yHrG3g7pMPeWIgekB3mmfTBQPZ0v/DjOgddtd67wrHB0iz+FR6p8FAmA2PaxWxCQ+nZm1TjbI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=Zjw2bvIG; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="Zjw2bvIG" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-235f9e87f78so35360105ad.2 for ; Mon, 09 Jun 2025 00:44:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1749455048; x=1750059848; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=Inkjt4Nq2XV942wyB2oCSN3ntPshT/X6vryIjMwcT1A=; b=Zjw2bvIGPVF1gEa3KglU/w+QSyYvW9e6HVircu3B5lvU5QaEglyDT7CIAtU85a6lKV 2F0u5ll9HNhfs6Onpryu4q3uee3ZOOMLPV+3CgSDyoFTXw8hdTDrwXZw10mmikQ1vLJN dCvIKgOcKU9ujVqSb/Wlgy5I+WyYL5J4mkTEyDj7GEx2ptth9Et+xTs/8TRpW0kjwnYv fzKhFa8Kt7jSTJq8pVjw6vKamI2WTqH0xOqIT7E8oaKdQEzV/wHwTl/wUs7YRh2Qhe9K Ocn+ee7mY2NvmSlSHl+8aJtHjAYOSRiFRfEaSFL4GygTEOe1fojJ/uvniohKKzSIIgdc kU+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749455048; x=1750059848; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Inkjt4Nq2XV942wyB2oCSN3ntPshT/X6vryIjMwcT1A=; b=LGm30u9M7lm60c/lRp/pt85uelWOdTwD4s1CCsYCTo4xXOaig7VHb7Lgj5DvOljKsh Vbdr8BVHFD6eLT/IKA66Tu3E4tNW2X6CwxkoI14sfaI+epeizg8T9TxO9QO7UniLFHOb SDBGRUmxrEK2vu+s3c4ECpTTurx/B0B3nyFQjMCwfMLT9kRNtM+qZX4nqHYx33BXREoM 3WILiKiq4N6aNFxn9YnRMt0AlBv9xVc/ezP8mfvQslap5/xlTsZEwDg02oDR3FZIZCNg 6AzhOlYGxefgV7aYQ9EmSUHyU9e4qKgyxNcqqzFxZnXtIq0llVDF6YxjQSa8FqFWKv3S 5iDg== X-Forwarded-Encrypted: i=1; AJvYcCUnUGZ/CvVmn5UJjfxIROkqgQm4+KHtpO9BXrvYwsbcrFNG+Ccw2b2wiMG58wJwYvywt5nUHvW23jICVf0=@vger.kernel.org X-Gm-Message-State: AOJu0YyViC3iBLN9vrxLv906Tkbks/navGqPUioNzW/NN4BPWFdTEsvq zwxVQmRi5ly8iSdziOjk2n0Aw+nis+nrgYwWo5G8DDx/3gplMGICK2CZcXa7A1QFxCc= X-Gm-Gg: ASbGnct6nvD+gEE/y1wiC/SotOaxq8C3FmxHT03+Hb3HlvFFuik2iqBUnYB51yd3/++ 6sR51VtCjpYYw7C0KmL+Uuv5sFEMfKQok6qT3QVoGKrA9myxCnxIHsIu00wEO5bZfV0Np5+7KmN HkB1Xugue0/8/TW66Z8AY9HtS8R1rgSyUhaVsflv5+7gD398cG4R4U8dwM5qSF/D+cpwvIXko0b UqkuRCDeFUjVAXe3J+LFd9zKHKkVKgBwLibH+8wAKwS70QIqr8m2KFPxQHPdZScByXUutuK0I1V re8D6PIy3l2/XHoBjhwMguN0lZsraZzOZ/kpdrukR+Dg1ZXuxSTfzPFVAAx3GBCGEXDbfLAoOO7 FnVlLFTLPWAZMVBZOQYt2LvXmMBtE+wgly5sEl4VIwA== X-Google-Smtp-Source: AGHT+IGfhLJOOr7W048JUz9Br7St9/rblBkEeqaO1eS84wCT9RHG7Sx6FmIfAdyIRIjtaw9ZkajVEg== X-Received: by 2002:a17:902:d50c:b0:234:de0a:b36e with SMTP id d9443c01a7336-23601da6138mr183133125ad.49.1749455047769; Mon, 09 Jun 2025 00:44:07 -0700 (PDT) Received: from L6YN4KR4K9.bytedance.net ([61.213.176.56]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-236030969ebsm48573715ad.72.2025.06.09.00.44.00 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 09 Jun 2025 00:44:06 -0700 (PDT) From: Yunhui Cui To: arnd@arndb.de, andriy.shevchenko@linux.intel.com, benjamin.larsson@genexis.eu, cuiyunhui@bytedance.com, gregkh@linuxfoundation.org, heikki.krogerus@linux.intel.com, ilpo.jarvinen@linux.intel.com, jirislaby@kernel.org, jkeeping@inmusicbrands.com, john.ogness@linutronix.de, linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, markus.mayer@linaro.org, matt.porter@linaro.org, namcao@linutronix.de, paulmck@kernel.org, pmladek@suse.com, schnelle@linux.ibm.com, sunilvl@ventanamicro.com, tim.kryger@linaro.org, stable@vger.kernel.org Subject: [PATCH v8 1/4] serial: 8250: fix panic due to PSLVERR Date: Mon, 9 Jun 2025 15:43:45 +0800 Message-Id: <20250609074348.54899-1-cuiyunhui@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) Precedence: bulk X-Mailing-List: linux-serial@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When the PSLVERR_RESP_EN parameter is set to 1, the device generates an error response if an attempt is made to read an empty RBR (Receive Buffer Register) while the FIFO is enabled. In serial8250_do_startup(), calling serial_port_out(port, UART_LCR, UART_LCR_WLEN8) triggers dw8250_check_lcr(), which invokes dw8250_force_idle() and serial8250_clear_and_reinit_fifos(). The latter function enables the FIFO via serial_out(p, UART_FCR, p->fcr). Execution proceeds to the serial_port_in(port, UART_RX). This satisfies the PSLVERR trigger condition. When another CPU (e.g., using printk()) is accessing the UART (UART is busy), the current CPU fails the check (value & ~UART_LCR_SPAR) == (lcr & ~UART_LCR_SPAR) in dw8250_check_lcr(), causing it to enter dw8250_force_idle(). Put serial_port_out(port, UART_LCR, UART_LCR_WLEN8) under the port->lock to fix this issue. Panic backtrace: [ 0.442336] Oops - unknown exception [#1] [ 0.442343] epc : dw8250_serial_in32+0x1e/0x4a [ 0.442351] ra : serial8250_do_startup+0x2c8/0x88e ... [ 0.442416] console_on_rootfs+0x26/0x70 Fixes: c49436b657d0 ("serial: 8250_dw: Improve unwritable LCR workaround") Link: https://lore.kernel.org/all/84cydt5peu.fsf@jogness.linutronix.de/T/ Signed-off-by: Yunhui Cui Cc: stable@vger.kernel.org --- drivers/tty/serial/8250/8250_port.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c index 6d7b8c4667c9c..07fe818dffa34 100644 --- a/drivers/tty/serial/8250/8250_port.c +++ b/drivers/tty/serial/8250/8250_port.c @@ -2376,9 +2376,10 @@ int serial8250_do_startup(struct uart_port *port) /* * Now, initialize the UART */ - serial_port_out(port, UART_LCR, UART_LCR_WLEN8); uart_port_lock_irqsave(port, &flags); + serial_port_out(port, UART_LCR, UART_LCR_WLEN8); + if (up->port.flags & UPF_FOURPORT) { if (!up->port.irq) up->port.mctrl |= TIOCM_OUT1; From patchwork Mon Jun 9 07:43:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunhui Cui X-Patchwork-Id: 895118 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20F0E281365 for ; Mon, 9 Jun 2025 07:44:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749455058; cv=none; b=HMJ2d1ANZl3zCWJzCtnRVYzJJ6E5X5CkjXwD2tohwalD1924/78LU+CZCa9Vm38LLFbnVp0RNBE0jqPEfCjMH1TBbNcEekEasKC6QLnhHsYOH47bN8fG0EfEGC8eNzwnwuzxk3XUrJk1P+8cdh8wrMutytdrx249Ge0XKhZGW3Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749455058; c=relaxed/simple; bh=qGjOULSQfrhZNYJbcbq9J/bx7G4cga14Ksr4xHqMKco=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=n/cOXO0UJQUvAIAisUKb5KDFZhru0VtxTW9rvTMh15r+G7kI5Z6duLXo8lRygHNXK6Hh5jJgnJ3ItN1Hq7Oli38TX0lHC5HsxLxI3iMooQOv7360SFj8iLv0V5PHAtTzHzNw7+n1JL181igb8fdaPE5wjTiElBmintDuSLBixT4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=bK/ExMvO; arc=none smtp.client-ip=209.85.214.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="bK/ExMvO" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2350b1b9129so24874915ad.0 for ; Mon, 09 Jun 2025 00:44:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1749455054; x=1750059854; darn=vger.kernel.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=4MgSetHRD9od3OJ7iz2rDkPKMWGhLjKM4sQsvvkn2HM=; b=bK/ExMvOYEHrzxPVkf7YubFWv90BGP5KCfyqE0uIsWuBIBHYHoMr6JYcJYRpTcX/33 jFtp6eiFQKWizVGYr0wmcfyzXYw9JLB2RKZnsDKIAauDjlJI2yidl5xTxD/4lnmYFsAc /ppUCLLl8PVdu3AUOFLkNDeX5cDCI1By2CzAXzZ/h+JFb8O6EhlXyG+q2pa3UhTf+ToZ lICK1volWOxsm1rR8aLYGZWeHWZcN1h/POo5BbkXkDz3J6qCIIexgLngATA0wQsYX9dV uBk7Z6yDKcQO+mJEA8ulXjsQ0ebHVaONDvOzTqT1RDd7n5JaiNxxTPT+ooO8ZJJWj23r QmRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749455054; x=1750059854; 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=4MgSetHRD9od3OJ7iz2rDkPKMWGhLjKM4sQsvvkn2HM=; b=RxK+5ATZlaUfno9txfSXaTJLx1Cp9qRFx+cfl0pkeShJSEG60WN0qY5rwRV0xdBCjJ JYhExdDtlBThQAM4/QyjW3OEzO1dxpPdhTMXfS18Pby+1AL5KCQW16k/LtYaXQeMVV6w 6LRn6XzvwJB0J/ASWA14zDudqsCF8Vw2qHVNBoyiLt2E8l5mPPTTPPGDiJD1Re21NGub b5FnH4vVlIhaz3tGfxlrqZfQQOEnAVIjENEeh8gqCWh8ztRqJYEMMc01HaoLy7oNTj8r jbSFn5e3jHteCkyZa+/2hDhxWs9jMQzM38xKuba6B9b+Ay9u6EwjNYnDZlqcKx+iANiE aCRw== X-Forwarded-Encrypted: i=1; AJvYcCVkQPKB1WdXMHiRBr66y/a2aHEgP58+2E2HoLqF6ICPB9AqrDhKA1szvpqneWK0KMtdEaQtueenQSFvsdE=@vger.kernel.org X-Gm-Message-State: AOJu0Yy684pY3frXQ8aBHetNrgMRK5D69wiGiVqdr/62W2/Bqwo5sLT9 NDJH12efUiy+0QZM+lC/fzsNtJhUNUq6euD9bV/fgVhjp6y03hQuklwlgAhNBNrZwfA= X-Gm-Gg: ASbGncvhaFuEiIlPXhA4NdOyHW6hJoj+A4TSBYdue8Q5AxwjfNXCPhle1DK5pI5TRa9 tiYCunn1TSxkD9FyYumUgRsB+uaA/Uu/SfTcxH1Y+DePnIBYRrc/EPvdSXmnYpDS/KhwZDFuJVd 9LPMTAl5e30E4oArHDS1nrgAY9FsqwfVpYzmK4smeH1E4GIztvuQR/t8vugVtLWnEcT2oU7Xctz +oG/1pjaVVfvx+6izR7B9dDVy4gxdoC7PQj0wnB5V/SPrmLMZxlSX8toPpH4MllgjDEQtjzcdoE qU5A2ZJViulFjy4KsWXai/LpJwaJeGpk+OxIk2qZtn3zK1YWFp1FsI31cjDmNw5ppx8tehYdiH8 5gNrhK+ivxjjEIHU57AdbMLDVUxk4V9LiInx762ao2Snxxksc2EDM X-Google-Smtp-Source: AGHT+IGF35bl6kHfVcpqficVcBv0i2lmdc98JjLaSkjXXef5nwbwLjsouU+PXGgR5AeX/b1tmHYsMw== X-Received: by 2002:a17:902:db07:b0:234:a139:1203 with SMTP id d9443c01a7336-23601d82d1bmr193428465ad.32.1749455054477; Mon, 09 Jun 2025 00:44:14 -0700 (PDT) Received: from L6YN4KR4K9.bytedance.net ([61.213.176.56]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-236030969ebsm48573715ad.72.2025.06.09.00.44.08 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 09 Jun 2025 00:44:13 -0700 (PDT) From: Yunhui Cui To: arnd@arndb.de, andriy.shevchenko@linux.intel.com, benjamin.larsson@genexis.eu, cuiyunhui@bytedance.com, gregkh@linuxfoundation.org, heikki.krogerus@linux.intel.com, ilpo.jarvinen@linux.intel.com, jirislaby@kernel.org, jkeeping@inmusicbrands.com, john.ogness@linutronix.de, linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, markus.mayer@linaro.org, matt.porter@linaro.org, namcao@linutronix.de, paulmck@kernel.org, pmladek@suse.com, schnelle@linux.ibm.com, sunilvl@ventanamicro.com, tim.kryger@linaro.org, stable@vger.kernel.org Subject: [PATCH v8 2/4] serial: 8250: avoid potential PSLVERR issue Date: Mon, 9 Jun 2025 15:43:46 +0800 Message-Id: <20250609074348.54899-2-cuiyunhui@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20250609074348.54899-1-cuiyunhui@bytedance.com> References: <20250609074348.54899-1-cuiyunhui@bytedance.com> Precedence: bulk X-Mailing-List: linux-serial@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When the PSLVERR_RESP_EN parameter is set to 1, reading UART_RX while the FIFO is enabled and UART_LSR_DR is not set will generate a PSLVERR error. Failure to check the UART_LSR_DR before reading UART_RX, or the non- atomic nature of clearing the FIFO and reading UART_RX, poses potential risks that could lead to PSLVERR. PSLVERR is addressed through two methods. One is to introduce serial8250_discard_data() to check whether UART_LSR_DR is set before reading UART_RX, thus solving the PSLVERR issue when the FIFO is enabled. The other is to place FIFO clearing and reading of UART_RX under port->lock. Signed-off-by: Yunhui Cui --- drivers/tty/serial/8250/8250.h | 13 +++++++++++++ drivers/tty/serial/8250/8250_port.c | 26 +++++++++++++++----------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/drivers/tty/serial/8250/8250.h b/drivers/tty/serial/8250/8250.h index 18530c31a5981..b3fb8a550db35 100644 --- a/drivers/tty/serial/8250/8250.h +++ b/drivers/tty/serial/8250/8250.h @@ -162,6 +162,19 @@ static inline u16 serial_lsr_in(struct uart_8250_port *up) return lsr; } +/* + * To avoid PSLVERR, check UART_LSR_DR in UART_LSR before + * reading UART_RX. + */ +static inline void serial8250_discard_data(struct uart_8250_port *up) +{ + u16 lsr; + + lsr = serial_in(up, UART_LSR); + if (lsr & UART_LSR_DR) + serial_in(up, UART_RX); +} + /* * For the 16C950 */ diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c index 07fe818dffa34..0560df9b064f9 100644 --- a/drivers/tty/serial/8250/8250_port.c +++ b/drivers/tty/serial/8250/8250_port.c @@ -764,8 +764,6 @@ static void disable_rsa(struct uart_8250_port *up) if (up->port.type == PORT_RSA && up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) { - uart_port_lock_irq(&up->port); - mode = serial_in(up, UART_RSA_MSR); result = !(mode & UART_RSA_MSR_FIFO); @@ -777,7 +775,6 @@ static void disable_rsa(struct uart_8250_port *up) if (result) up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16; - uart_port_unlock_irq(&up->port); } } #endif /* CONFIG_SERIAL_8250_RSA */ @@ -1353,9 +1350,8 @@ static void autoconfig_irq(struct uart_8250_port *up) /* Synchronize UART_IER access against the console. */ uart_port_lock_irq(port); serial_out(up, UART_IER, UART_IER_ALL_INTR); + serial8250_discard_data(up); uart_port_unlock_irq(port); - serial_in(up, UART_LSR); - serial_in(up, UART_RX); serial_in(up, UART_IIR); serial_in(up, UART_MSR); serial_out(up, UART_TX, 0xFF); @@ -2260,13 +2256,20 @@ int serial8250_do_startup(struct uart_port *port) * Clear the FIFO buffers and disable them. * (they will be reenabled in set_termios()) */ + uart_port_lock_irqsave(port, &flags); serial8250_clear_fifos(up); /* - * Clear the interrupt registers. + * Read UART_RX to clear interrupts (e.g., Character Timeout). + * To prevent PSLVERR, we can either disable the FIFO before reading + * UART_RX or read UART_RX only when UART_LSR_DR is set while the FIFO + * remains enabled. If using the latter approach to avoid PSLVERR, it + * creates a contradiction with the interrupt-clearing (see the + * rx_timeout handling in dw8250_handle_irq()). */ serial_port_in(port, UART_LSR); serial_port_in(port, UART_RX); + uart_port_unlock_irqrestore(port, flags); serial_port_in(port, UART_IIR); serial_port_in(port, UART_MSR); @@ -2423,15 +2426,13 @@ int serial8250_do_startup(struct uart_port *port) } } - uart_port_unlock_irqrestore(port, flags); - /* * Clear the interrupt registers again for luck, and clear the * saved flags to avoid getting false values from polling * routines or the previous session. */ - serial_port_in(port, UART_LSR); - serial_port_in(port, UART_RX); + serial8250_discard_data(up); + uart_port_unlock_irqrestore(port, flags); serial_port_in(port, UART_IIR); serial_port_in(port, UART_MSR); up->lsr_saved_flags = 0; @@ -2513,7 +2514,6 @@ void serial8250_do_shutdown(struct uart_port *port) port->mctrl &= ~TIOCM_OUT2; serial8250_set_mctrl(port, port->mctrl); - uart_port_unlock_irqrestore(port, flags); /* * Disable break condition and FIFOs @@ -2532,8 +2532,12 @@ void serial8250_do_shutdown(struct uart_port *port) /* * Read data port to reset things, and then unlink from * the IRQ chain. + * + * Since reading UART_RX clears interrupts, doing so with + * FIFO disabled won't trigger PSLVERR. */ serial_port_in(port, UART_RX); + uart_port_unlock_irqrestore(port, flags); serial8250_rpm_put(up); up->ops->release_irq(up); From patchwork Mon Jun 9 07:43:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunhui Cui X-Patchwork-Id: 895735 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C0BA1281358 for ; Mon, 9 Jun 2025 07:44:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749455064; cv=none; b=rQjCLyDjKCrxFhVFytt6luuRGGhqCrg7nh0Rl4gKuIdKwcDgflECGbx2ye9/y+TRAYxEvYvRozeNjuppTSO7LAyTz/f1zLYj49ED1C8nBe04QsdeUd2M/WmOVzfvwBPQW958iVl5GTSl7qggFaQcvYMlXmD+QHLcvByojtPyg58= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749455064; c=relaxed/simple; bh=sh1cGUiYYVGEGi8YqLowXFl/GRJaaw2ICSbcRxFVUtk=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eJ1UXwwXvMUi8uOKiZ1iLvyTenpIunnQfJJ+ed5lC2rv5OTJiIEM7ni1DQqI88kMW8lQa7LQnx4P6toTb4//96fd3Bo73K1YAXy+gniCb+V16ZMGCxGOJW3RPmNugJG+e6iZOvdQbUIQCZxUO+Nw85ghJuzus8gl1zUTWC8H8FI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=grPvD35x; arc=none smtp.client-ip=209.85.214.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="grPvD35x" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-235f9ea8d08so30281045ad.1 for ; Mon, 09 Jun 2025 00:44:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1749455062; x=1750059862; darn=vger.kernel.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=Tf3d4NxJrcGNbUN64elIQLb/pmeQW1fbqHye+etV/RM=; b=grPvD35xZq64cksE0uDYky34vJBZ4bByr2m+BlV3839saYONI7H2WaTe379QIXyo+L zSGy+12JIA2r3h2umsPDHjPox1dRycrlE3+CxcbBPZS3z5/35hRCvbLQn3NRJnfyP/V/ HyOrgBjnlsHVGwMMUHGMdBvhZeM28xldjdxh+O06K6gHSfPYHPR32bWcZZDqzQxecuZD mMd/CD2K/jf4iJc+XyA7mhOBuOMApCLvhAyTFb7sTckOr0rXPM9S/pOdlBnyopEVw9n5 eR3BtNegPzUxpVEvyhDIE4bmk1bTs2qLhprpgEowDy9ZrqtYUFkI/gLbViRM/Nqsb2P1 NSQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749455062; x=1750059862; 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=Tf3d4NxJrcGNbUN64elIQLb/pmeQW1fbqHye+etV/RM=; b=DYfGXOqJ7EvUyuX69FNJZpYNtO7lGWsyrdqbx9BFQKryjkPpTJfHisXX+MVNYkLT2s 8+1lpwOa/0aL2ptDaWm7vuTqYhS/LvPtLIajZyZ0GSMMj3+RlAgBy5sLfg05sXIEsIoV ZXQS0P9Q2aL+W9hen0i/QcpIPoWjS6qeQNvQaNSgD9KOwC6p41HN7B4xhUCfVcxkRqwx 2q36a1ZsZRVG0X8u53hCLIsXCJQ8ube8TAKlzSvYOqCQ4qd2Dn+sdnnIT1ueWCQ1ZXCf cSWEak7rafsV0gJe5Kw8EOlwlQkGDMgZ1q99MmkmHJLu88x6oLfbyRYVOPIH361b/MDu 6XEQ== X-Forwarded-Encrypted: i=1; AJvYcCVAk3PFHCZl0epo05UO1ySa6aACZcWc8e2pc1zr1tYib0gl2BiQRA0tGgmsm00A1T7Dy3ySckgHS7RnhEY=@vger.kernel.org X-Gm-Message-State: AOJu0YymRDvlbsCddSzwzR36L3qS6IGLKBb3qf+5w2eOfGlpAvI+6cWH 7ej/heBJHeF+6KMzDWCCq4YV+WUVICPvtKMrQHYEamF/QR1uVAilIU+tnq51lxnlS34= X-Gm-Gg: ASbGncsSWO5AIUcF9zxyS7Qw0MJ8mW3ARusyk7k1cmUvHzNcswG21Eh2KNRevvD0Zrm b00xPz5Aj6RMJtrq1Crg7cUuTg+uofjRBArSSGsIrbA3rxYAM3u1t5cjdITuay9EdxWoP0xGId4 ZT5NJGRc4UX6cCPgbRtfIICnOh5J7YthsfchFsGOO0yOisM7syEB7Dr/aT7tIjKcQhjHvCCYS3A JdEA2oN9kxadGmE9cCB3VPuz+5w3qxugQKGG3ETQ7cVPB64DjiUM3IrgM5I8pyE2WQLO5aWxAtk KxbrhSl3wkFw67YHBHJi4SLqObEO8h3pn2Dvv333fJJ2nTbpqK72CsZItEiuWSNuCJjnn0zSsPK VyIUGu403PnJP+YQiJzmPteOGoieT7/ksb1pEZXBItQ== X-Google-Smtp-Source: AGHT+IEQ7dRItoYtft7Z73E1bG0AQUocTaShtV8m/XxHYF/+UtJ1i4g9HW/VM1Ga0ii/WT7qcpPUAw== X-Received: by 2002:a17:902:d48e:b0:224:c46:d167 with SMTP id d9443c01a7336-23601e44edemr172981645ad.16.1749455062090; Mon, 09 Jun 2025 00:44:22 -0700 (PDT) Received: from L6YN4KR4K9.bytedance.net ([61.213.176.56]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-236030969ebsm48573715ad.72.2025.06.09.00.44.14 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 09 Jun 2025 00:44:20 -0700 (PDT) From: Yunhui Cui To: arnd@arndb.de, andriy.shevchenko@linux.intel.com, benjamin.larsson@genexis.eu, cuiyunhui@bytedance.com, gregkh@linuxfoundation.org, heikki.krogerus@linux.intel.com, ilpo.jarvinen@linux.intel.com, jirislaby@kernel.org, jkeeping@inmusicbrands.com, john.ogness@linutronix.de, linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, markus.mayer@linaro.org, matt.porter@linaro.org, namcao@linutronix.de, paulmck@kernel.org, pmladek@suse.com, schnelle@linux.ibm.com, sunilvl@ventanamicro.com, tim.kryger@linaro.org, stable@vger.kernel.org Subject: [PATCH v8 3/4] serial: 8250_dw: assert port->lock is held in dw8250_force_idle() Date: Mon, 9 Jun 2025 15:43:47 +0800 Message-Id: <20250609074348.54899-3-cuiyunhui@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20250609074348.54899-1-cuiyunhui@bytedance.com> References: <20250609074348.54899-1-cuiyunhui@bytedance.com> Precedence: bulk X-Mailing-List: linux-serial@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Reading UART_RX and checking whether UART_LSR_DR is set should be atomic. Ensure the caller of dw8250_force_idle() holds port->lock. Signed-off-by: Yunhui Cui --- drivers/tty/serial/8250/8250_dw.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/tty/serial/8250/8250_dw.c b/drivers/tty/serial/8250/8250_dw.c index 1902f29444a1c..8b0018fadccea 100644 --- a/drivers/tty/serial/8250/8250_dw.c +++ b/drivers/tty/serial/8250/8250_dw.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -117,6 +118,9 @@ static void dw8250_force_idle(struct uart_port *p) struct uart_8250_port *up = up_to_u8250p(p); unsigned int lsr; + /* Reading UART_LSR and UART_RX should be atomic. */ + lockdep_assert_held_once(&p->lock); + /* * The following call currently performs serial_out() * against the FCR register. Because it differs to LCR From patchwork Mon Jun 9 07:43:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunhui Cui X-Patchwork-Id: 895117 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 54DDB280CD9 for ; Mon, 9 Jun 2025 07:44:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749455072; cv=none; b=V52GS83me54zigvd2ZyHNZjX7DN9ihfQcmDyFZzaYQaVcZj3+PBzIoBxRpPEKfgUG+Dw/6eiNNOhdwHccozev3FAN+shG3XiRxh4qiOLWxMop489VylNi/IKkwNXif1rLahrFFoClLfOqPZie8KllMxbJpx1iiDekeuVrTa4FwY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749455072; c=relaxed/simple; bh=Aw74MEKOMSP3CtxTaewVFE8Ntfz0jyoHZgR+tmeysak=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Vnfs+8SFWEinyQ2YvxdGZH0LaOQ3Et7h4bd6PFb8u4sKvAQzoNJ8Kzrgy/cOwDY69Ren7dDXKQTE+8CLbxIIAul8itMxtXLM538dtS6dGsiPFAmCYpuruma0XLXXS3dgyoofhDSgi6IYxSgH6qk4e+PLmENwrDhFKiJ8YS11gSM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=D9T/nIOB; arc=none smtp.client-ip=209.85.214.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="D9T/nIOB" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-231e8553248so39725795ad.1 for ; Mon, 09 Jun 2025 00:44:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1749455070; x=1750059870; darn=vger.kernel.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=w2r8WQzb+JVgSyOxnE+i/6qtzBR+LExX67w7PlIfP4M=; b=D9T/nIOBr5rD/jqydN6oggzesNBzh6UoD+sv4Kbdx9WnnMuxasFCMbDoAvnsmT2Jhe z8h2A1BBRDjs64nxHHtHaj1lkdWgHmIEH/dWEU1hDS2VZ4ysNIGHLP8vViy/ccf7hrKz HP7674AFLHJ28q/3tweJKae+Y6VgtkNnE2XpfTQrIlvKxbVIDAI6ikR8SN/ymfsT5bIX D8YaJZDs7J0RX8Akqbhj97YAPuCXtAfB5BYlk/EDTZFYg0X+n9BYtZVSrOxaEufANs9z bnWzGaJ/nAabwI3iL7TZdVnS8au0bFN8L2b4yJm4NzZ/Wk+f0CdO8WmKA5qLZm/Melo0 +Ltw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749455070; x=1750059870; 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=w2r8WQzb+JVgSyOxnE+i/6qtzBR+LExX67w7PlIfP4M=; b=gV3JWKx+jEeNX/LrQgNjkd+n0I1nqaHhuw3kxM817L51jwv01/q56a010vru6yRrhd X4iPuX7uS6VOAvW0iqFwhV8K7KNw2VouQDLTNpLS+jO81LJ3tciwsrskarfDpMdXg+Gt c2QqNHbC4C2lnmnbDDSR1m1IV7JNdwEmmpriacyFlQb1APLqQ9Bw8eiHLALZ9mrOBlO+ c8kHuSM7YU0hMuKtrZQOsEwCRjdWLbujQ1gVKWKyKe4WDkvwn/u4C4yRAkcN7xpmV379 42Xoy2Sky67s03NuxyFR/6CoqfgZiRXyuDJAlUuz+Sbb9TnnaGXyIaZjiCSci7jHs+JJ VO9w== X-Forwarded-Encrypted: i=1; AJvYcCWcdq8F+YluHl6WfBuI9NkKTyPvmnoU9NGXn6q3NNygLLjNh+YAUQZP8GvNkoQuky5UuSmfB/6ywGnnuZU=@vger.kernel.org X-Gm-Message-State: AOJu0YziouIPsmzgbD3uULfH4OHyA4uDC5P7SMNrsQ6VnUw+7PKtJ/Vv yke4d6W9DrTZ7FPU1/r4AL0isifBABEQrILebfqsJJODzWfKOTglrSb4e+LWRTbAzgw= X-Gm-Gg: ASbGncu0TxZ1r6lBiYa7rcRMmwqP1HvpdX1KGCN9mrYdwa8gTPJAYsbJ5zTNyT1tKDk s23wG094Sanp5YTBMLCFq7PZM0t7/HKmuEhjATZF1ZZjMH4nmbrD8LZTpLbbrTYXYxJ58whS8P4 3rTL5zsDpCdTKbL1JsNSVyGEGPYre0Pib9QdmNhQ8XBmdmxvV9weO7Psbzf/5d6NSt7XdK5eBQt 9qG1xlxiGEKamAV/HIqE1hhoemXvf8myemuuKglW8AIC0ijgfKEL01QPrPEy/wCVD+x1ino+9NJ SaFzR3MIgDaLjCFUBNBhD2xVRhd8zjmk6ML8uLmQMFk6DV7EHZbQOMQCQTSxNjwODD5eJpQ0zjd rV9nbJ0tmrIsdCsiSE5n1/aZA/2MUER2TC5gSjok5CQ== X-Google-Smtp-Source: AGHT+IGpx0fPdgm1EKp0dazP5RH9EWCW35YAP9lCiJDVJ2NhKqFRJ8viahaNntjBxyQhSeZ1tKrS8A== X-Received: by 2002:a17:903:32cf:b0:235:6f7:b918 with SMTP id d9443c01a7336-23601d225cdmr180747485ad.28.1749455069674; Mon, 09 Jun 2025 00:44:29 -0700 (PDT) Received: from L6YN4KR4K9.bytedance.net ([61.213.176.56]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-236030969ebsm48573715ad.72.2025.06.09.00.44.22 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 09 Jun 2025 00:44:28 -0700 (PDT) From: Yunhui Cui To: arnd@arndb.de, andriy.shevchenko@linux.intel.com, benjamin.larsson@genexis.eu, cuiyunhui@bytedance.com, gregkh@linuxfoundation.org, heikki.krogerus@linux.intel.com, ilpo.jarvinen@linux.intel.com, jirislaby@kernel.org, jkeeping@inmusicbrands.com, john.ogness@linutronix.de, linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, markus.mayer@linaro.org, matt.porter@linaro.org, namcao@linutronix.de, paulmck@kernel.org, pmladek@suse.com, schnelle@linux.ibm.com, sunilvl@ventanamicro.com, tim.kryger@linaro.org, stable@vger.kernel.org Subject: [PATCH v8 4/4] serial: 8250_dw: fix PSLVERR on RX_TIMEOUT Date: Mon, 9 Jun 2025 15:43:48 +0800 Message-Id: <20250609074348.54899-4-cuiyunhui@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20250609074348.54899-1-cuiyunhui@bytedance.com> References: <20250609074348.54899-1-cuiyunhui@bytedance.com> Precedence: bulk X-Mailing-List: linux-serial@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The DW UART may trigger the RX_TIMEOUT interrupt without data present and remain stuck in this state indefinitely. The dw8250_handle_irq() function detects this condition by checking if the UART_LSR_DR bit is not set when RX_TIMEOUT occurs. When detected, it performs a "dummy read" to recover the DW UART from this state. When the PSLVERR_RESP_EN parameter is set to 1, reading the UART_RX while the FIFO is enabled and UART_LSR_DR is not set will generate a PSLVERR error, which may lead to a system panic. There are two methods to prevent PSLVERR: one is to check if UART_LSR_DR is set before reading UART_RX when the FIFO is enabled, and the other is to read UART_RX when the FIFO is disabled. Given these two scenarios, the FIFO must be disabled before the "dummy read" operation and re-enabled afterward to maintain normal UART functionality. Fixes: 424d79183af0 ("serial: 8250_dw: Avoid "too much work" from bogus rx timeout interrupt") Signed-off-by: Yunhui Cui Cc: stable@vger.kernel.org --- drivers/tty/serial/8250/8250_dw.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/tty/serial/8250/8250_dw.c b/drivers/tty/serial/8250/8250_dw.c index 8b0018fadccea..686f9117a3339 100644 --- a/drivers/tty/serial/8250/8250_dw.c +++ b/drivers/tty/serial/8250/8250_dw.c @@ -301,9 +301,17 @@ static int dw8250_handle_irq(struct uart_port *p) uart_port_lock_irqsave(p, &flags); status = serial_lsr_in(up); - if (!(status & (UART_LSR_DR | UART_LSR_BI))) + if (!(status & (UART_LSR_DR | UART_LSR_BI))) { + /* To avoid PSLVERR, disable the FIFO first. */ + if (up->fcr & UART_FCR_ENABLE_FIFO) + serial_out(up, UART_FCR, 0); + serial_port_in(p, UART_RX); + if (up->fcr & UART_FCR_ENABLE_FIFO) + serial_out(up, UART_FCR, up->fcr); + } + uart_port_unlock_irqrestore(p, flags); }