From patchwork Wed May 28 06:26:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: yunhui cui X-Patchwork-Id: 893554 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (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 932991A23A6 for ; Wed, 28 May 2025 06:26:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748413583; cv=none; b=YZm88bu/lV/h5Zd29zOOomBFivFV4nu0okxaK5wzh/iUZyWeBCTlSfQC4JINb110zhNcpUtKuoiePVUzZvu2xauSADBANSvjcDYaY7PpjEoeRo9uJRiwdM4manhNLlT+6B0WyXgz9yaqXhjrYzw9+0T0cNsOKR4OVKtwZSVddeU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748413583; c=relaxed/simple; bh=kVXBJy2waXc8Zm3U1zeBkK0QCDtIv0VCdcWVejgqIaw=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=Y9tZS/qWlXh5aevirA0/91aadPKPMC3XFOhnx0dlu+V9vOB5wMJ0FQPB5KrFzaRb/JtPV885/yL7TTwIHRVwfcu94cDUbiFaAwkFku4tvnlW8J9CEUW1vXrrvluWfShSYczk5WjGCz03TI/rFwxZVR9oNV58MGcQFx5wthYfSwI= 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=ewSpP0y/; arc=none smtp.client-ip=209.85.210.177 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="ewSpP0y/" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-7406c6dd2b1so439754b3a.0 for ; Tue, 27 May 2025 23:26:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1748413581; x=1749018381; 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=pJQtmBORsLul2V6MNygZMp10lRlI52BK+PKHssMFRlI=; b=ewSpP0y/zWP6dzyhIJbggeIjxYShxz9NcHfqBwijmiCUiVlpPSEUZscjH3SXlKHHaP jwuKUo8j8vUl1+OTjcce97wf94Dlb/RZDU8dMDiiGfI20QzMh8+adwq13Vt7+/H+KDaZ zh0o6vIBJuHWAuz8jIH3p/H5mbbjJ8PIx1ByguzXqIVk6hcnqR4Oq2Qio5/S8Hf+KE2B yngoFAhIVy5dPjBD3nyJa6wdxqbvJ+wuyHg6yNtVgHofiYoBzuRVtwxvPjWVBGiiqXNd oqAxY3d7Zb5NBlZV1lF63Y1B6exbZsRzjVL45dmJLAbgjocS8135RRnYCVLMX6DbPjoR 7PLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748413581; x=1749018381; 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=pJQtmBORsLul2V6MNygZMp10lRlI52BK+PKHssMFRlI=; b=sY89JcEAMAqtXZP3rKOmU42onFrDtQl1sCljahnVtExjNA3mQlXS5Ca2uHgxbf3quq a8LO9Xzr+ODQBH6pTrtRLaTXTx2dIYKe/4c4HtsCX84eFWgz5pX0UAjE6HhKtuXD6nHi 0Dx9WKNPq5seCyWj/ypaWj4svK0oynzEk7wq1guL9N3iujIQGtcOQ2HoduKbpsBz5sfJ pNcjooBtxv2AVKkrylF2i1yHe9u5HvE1iUNkW3oGUE7zgUiez5JeA+iVza2ZL/ci6T2g icHntvdNxKqUTvS7gRytisMvTGwWmvWLMCcZ1rEpnYBmXGm1xFRy+84Iw/kEo/asz/af G+hQ== X-Forwarded-Encrypted: i=1; AJvYcCWbZz3KaFZVsqvDhUCfLMQNpFj0VXQasNy3WE0E4AKZ0RUB8pTNQUHAetnbUEX/jL5w/Q0wHBU/SXBMJp8=@vger.kernel.org X-Gm-Message-State: AOJu0YwxgpbuWKNoEhwqWaZJPzNyk8C+i8b28dooKb4bXDNiyOZPq6Nf vfXRZgmtPJh15gwYrs+ILirkSoPj938ACrFUccqN90hNVRm+TLzHeX6Tyad7Qw2ZKq4= X-Gm-Gg: ASbGnct35LtgclyeJMJqvNgBaDtaQcA6AGufi/ZbsGbd4Rv6HHSg/v36poBaa9+GJsb NShSZBJq8TpdY33TMyIYR3N+OMs1JnuYZIJ/dSHL5dOvRxcma+70A9kpjTNbsEeax5UDG8EibCw mIODjsEkhWGJhd1neOshajjYrQjWCvCi1v5/YHIjURVI5i0/XUP0W3eBOwLl3EFh65niNKbjqjR OqnslsY3niSbQMS7ND2U4DpyLdsu9Ytf+JK9mFeaOuGvkMni0kkljfC7H5QQxf8GCofn+1YrvNH TbLFjRCNFTyDLaSNN0iU+jQP8xvyDWvHPANWuDQB4D31UgSbuIJYaoqDedTzu/JGUGDwvjucoI9 sgOv878CQ8A== X-Google-Smtp-Source: AGHT+IG1r91+7tI2/CIrBwPliEl4ShQJtjrlA4mz6gCB/b9+fF0Qvdsp0MdfXtmq8J21fWN5WqE8Qw== X-Received: by 2002:a17:903:3d0b:b0:234:6b1f:6356 with SMTP id d9443c01a7336-234b74b4c95mr53731085ad.22.1748413580666; Tue, 27 May 2025 23:26:20 -0700 (PDT) Received: from L6YN4KR4K9.bytedance.net ([139.177.225.234]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-234d35acf6dsm4619515ad.201.2025.05.27.23.26.13 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 27 May 2025 23:26: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 Subject: [PATCH v7 1/4] serial: 8250: fix panic due to PSLVERR Date: Wed, 28 May 2025 14:26:06 +0800 Message-Id: <20250528062609.25104-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 --- 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 Wed May 28 06:26:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: yunhui cui X-Patchwork-Id: 893094 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 EF8B8229B0B for ; Wed, 28 May 2025 06:26:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748413590; cv=none; b=HtZbqBSPH6yBitfEOZ/tB+v/AM1ZI1iR5osm3lp60qauYCXH0PQIBIW+dRgDQbDdo2u8jekAgv7uqgOqSXXPZLhLrZbT+JCyzt1HSvQpgivtIdR668xeHNgN7WI11O50Um8nxK5Rqc8J4smdJPjfxqMRdxv8Sy3GDPBLYapnF68= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748413590; c=relaxed/simple; bh=qGjOULSQfrhZNYJbcbq9J/bx7G4cga14Ksr4xHqMKco=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EONs6Os6ITRfMvAbJ1LrG8sh/OpBimch6SNEvs3AadJmXJkPXGLP9RR6dODEE2sqF7g6KbfGVONlk+qweys+tMs2J9LXUwJZu2FJj4U+u+efxqObVIdjo65Zc1wrLEjI/TT6tdSlX62NTGVqgamSVz0+m2WwMFiPUjdwg3apXFw= 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=BANbHvaF; arc=none smtp.client-ip=209.85.214.179 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="BANbHvaF" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-231e8553248so32111545ad.1 for ; Tue, 27 May 2025 23:26:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1748413588; x=1749018388; 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=BANbHvaFuZb3wPlbhHaPzWrBgU3MH5Tcb8ioHlAPVidFx0LDHqP/nt8b6Mhvspgq15 6xLa/9VdBbp4aJ9pLU07S1pwFjG6WwrstIbFCb5vuh4jhhxnJ4M1MAlSVJmHVARL3NXU F+0MnyO0MDXj6d+Vr3Yy4iNzad5RxN8FDTEni4eARXjIFOSg/S1lUchAqD+xi4G0QbdV bRS0Qm7r8n1r/2+KcpWKjLM8okHCPOkUpUFHz8qxOUqZSlwg1O2Fp9hJ5JRktogUVqss hf+4O3LXOuuewXaE0ZGUcRacqEq5CHp4j3wwM3b4yDXwIb90Z+VL4z7AWRrrH+3rijto bs0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748413588; x=1749018388; 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=OGFLlXrB3b5VFKn8UXBTryi8T0oN3CbrrMEcBOw6Rx79mLac+1QdpTKqAtOa64wx2R RsnwI/WDRBGS1POK7ZTTTQ66GsNJyv0BrcvWRnFBtZzmlv3qrDF0oMtdm8ex/sNq02Lf OsQRHUIH79yTmrywEnPFvglPJsBsWdBC2vQH35BEGML+BSMSSVlVmEus7Vrxh+WfMTdT gIByzkRYvT5wFkZiFHEM/RpT0P8YRwVeU4lOma5cU7vm2NovzA3OCOCbcyG8cs4wj2X0 jCrTBDVIOuH3wfE3mcYQ/SEAuQwJmWNc88afPg94Vxu/jPoaWoZvx+jDXSVtnZczgumm UHVw== X-Forwarded-Encrypted: i=1; AJvYcCUkthXeZG95XvUxZ+T6gG+1wqLy/4D0wtQlXqwUnji5UGq4byfxl+l9Samhsnqv+tocJKJHW7L6FquJq3Q=@vger.kernel.org X-Gm-Message-State: AOJu0YxzW0wZHK0oGh7gnZ5kleq9+e73B1/6f44e6znP7CemHnFclko3 f1QYVeF8Df2x6Zna7WjXyFgiZYX5adclURXWICjzAX7GMQCdqi2iLUsWj4BKCx6C06E= X-Gm-Gg: ASbGncvkTjmhkyhyriXOaqJhHeS42sbspH9kt82OOxcEQiH8XWhQi0nOBCu+dy28CLe 832zp4e8OrkYLilcbDJ/T2Nmf9o7SbY9U5UXare9jIhAMnE25MXy/oxVyMzwwTu74VP/7dwlRFD 9ixzt2NWKa3/tZVW3O3XmTYUFQJIdHpEd8dt6okX510GQIVx5x6Fp9f80tLUCi6LcHAeO89wIm3 kG34ZTeL96NGNnBv9z2WuAEtJKXXJxjke1NYyN/qjzcASOOmPz41iiqG7W+nRN4G2Lg2vBLL39Q egS1y/BGNHJLP+SrTJOp1QelcL7+T9VOBD+pmUARGG5Y6H4V2gbogD6UMFq/AdKDysKcFtItwlK 6dKAYFgsjWw== X-Google-Smtp-Source: AGHT+IF325pPEY0R+/zinfzdIgswAy892RWSaAg07IWccXaRc3y73zytZGgiOq5DoEa+ManNhR1Afw== X-Received: by 2002:a17:902:d490:b0:234:aa9a:9e0f with SMTP id d9443c01a7336-234aa9a9e76mr57107955ad.23.1748413588026; Tue, 27 May 2025 23:26:28 -0700 (PDT) Received: from L6YN4KR4K9.bytedance.net ([139.177.225.234]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-234d35acf6dsm4619515ad.201.2025.05.27.23.26.21 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 27 May 2025 23:26:27 -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 Subject: [PATCH v7 2/4] serial: 8250: avoid potential PSLVERR issue Date: Wed, 28 May 2025 14:26:07 +0800 Message-Id: <20250528062609.25104-2-cuiyunhui@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20250528062609.25104-1-cuiyunhui@bytedance.com> References: <20250528062609.25104-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 Wed May 28 06:26:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: yunhui cui X-Patchwork-Id: 893553 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 2A7F3228C8D for ; Wed, 28 May 2025 06:26:35 +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=1748413597; cv=none; b=T8r8ZRIp39/BVu19wvhPfahWmbPMSGwKGFy1WY7fVOC9bh7yqMVkchW8q99KnS2vQizXkex+y/9BWoVOmWSM94x32N6V2V2aGHgkKMoZM0gCaqk7yNoVDF298PDxEiGA47D/2ARWRa9+c+ED3XJsQI/QCtHhhVIyYuik1x/kHog= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748413597; c=relaxed/simple; bh=sh1cGUiYYVGEGi8YqLowXFl/GRJaaw2ICSbcRxFVUtk=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kH/7lXKriaRggEoE51pGGDZT/n86TW61ew5PapyiSQAUTDJDSZl2vL/yWGXY0zEYNFPV10Ncw6eyY6zVu5S3SdbC/2frAQbrH/gLlpwQ+DFglXqheeIvTOSN74NukKoetTbUNVCQhxRa/X6/dF8UGFux64ydvLRgXa03SjB1zjc= 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=QhbjGgX4; 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="QhbjGgX4" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2347b7d6aeeso26626655ad.2 for ; Tue, 27 May 2025 23:26:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1748413595; x=1749018395; 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=QhbjGgX4fiBmX5P9KVykKiyVB88EhJgWGY27uWUonc1g2hwGg/HuVpZcSbh0gvI+/f fohlZWwZFVuHl0cLlC4KxpGe6jmmgfyexUaccS8AFAJgjX1Wr9YA8SUxZ0nqenTew5ng WFpw54iG3Lp8xosOD5mOuHocdcZyRPJB4rdhMCqnoua8z7RApiDI5lKF5lAk+I2OntwT 3oXf4W2S/YKnFUA4faw3NijOvtQgs4rvHNH5K8b/A8isFcSWzQ9XVDhWbP1+aFH7efbq udtAdsrnO63k215nYXJuFp25oDYB3vA8qNglqQQ58wYwoZ7zVwLpbP+p/1DBl8TxBrhl sSlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748413595; x=1749018395; 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=DPdBAlglOj/aYK2Z0i0hlvUthdLd3nAAc1sRUZTBgCrdEiM/rrbC5gmZO+nukQI+Fz VU4futl9kYi6tswnTy2jLkAVMWH+rIxCWD/k6mw7zdHOImw6wwABa0o6GtPzdHv791R9 u3aoowul9cEwPQLMYN07FlWjHHkS9Mg6qAUyTkmd//k85kOQywCj6N1xRCkZMBZLHv2X LZZQ2Gr3Xjl6ebQ7hWpykCCbKG69qsO45OlfTE1FEoBmSDE6yGXVwpRF+UAWo4uowQu6 nEex/9rJ2KVzARCbqdKnPmmIcPOfTZP8E9/a1jxJ68xJ3CbjeY4mYd8bBs3JY/NgC80M 8KMg== X-Forwarded-Encrypted: i=1; AJvYcCXftX/FNSn/bsEh5+Y7AkFJ3YRlCYFyOD19W64CHBZgAUAnxK+xLicd+ndHnOJLcObaw4BKRD4xqoif0zs=@vger.kernel.org X-Gm-Message-State: AOJu0Yzw4sfYY6ZeE3I3ud1UlHGk5uf+bo0JjKVBanpPwO6gvHqUUtmS 052nKAAe7+lBTNgVc+8+Z3w1K6Aqgoxsbgvj09XCkr+UxkVpp392AyP0Jd+Y29Iwx7s= X-Gm-Gg: ASbGnctzk2wZlZbj9A9MGr68t/Lg9i2mN+lI9mIJgzKpJS3MHr8IMSOdOPtZrzvnyfW bFjgp8Q2wleqSf3usrNJzf59XOqHzGfvQWTNAY7c7Wv7K3Puk8rVNbZ6JV6z8kqjaECDw8A2epV 4Uzc++ONEW4ZeakP7+u7FS95HVeyIDQbLbwqykUwsyFIFrxP0IusmzZolPGJnV1BVfbpk3U7Sz2 ztvHUEqdu2iMQE7S9oLLPXIxrG5RIvtaif96lFtWVnDhRv4pScv9FM1V5phBeUvtxWT2B/rhq8Y ZYQvAKW+csJPugmjD86FF54RG5gylTFJWM0jN2r3XNCv5vrY6TQO5i0FLiMjvblKToQQnuyfDDG a/7IGhp1nrA== X-Google-Smtp-Source: AGHT+IHMwV9pu4i8J+GgFkOBOpyMXF805fnm3igvxyjsK4pKznBH1Watf3hetuV0tb6o3s9sRcJvDA== X-Received: by 2002:a17:903:fa4:b0:220:e655:d77 with SMTP id d9443c01a7336-23414fcd21bmr292803245ad.36.1748413595413; Tue, 27 May 2025 23:26:35 -0700 (PDT) Received: from L6YN4KR4K9.bytedance.net ([139.177.225.234]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-234d35acf6dsm4619515ad.201.2025.05.27.23.26.28 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 27 May 2025 23:26:35 -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 Subject: [PATCH v7 3/4] serial: 8250_dw: assert port->lock is held in dw8250_force_idle() Date: Wed, 28 May 2025 14:26:08 +0800 Message-Id: <20250528062609.25104-3-cuiyunhui@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20250528062609.25104-1-cuiyunhui@bytedance.com> References: <20250528062609.25104-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 Wed May 28 06:26:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: yunhui cui X-Patchwork-Id: 893093 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 D996B229B16 for ; Wed, 28 May 2025 06:26:42 +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=1748413604; cv=none; b=bxjjrwkZBemZq4oTeoF+MUyHayTSCJYSlgRclvEOUVLddcIXzbth/YnHUIvdQK/3J8lN8cBooOo3MkA2ren3aN9iQwpnckIIlWPdKCJIJwK8EtlMWoMrZQ3zJSh3VDROolVBaSL2DmnxDz9Z5ybc150rWn+qpxM+48EJp2kkq6I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748413604; c=relaxed/simple; bh=Hl0w23mrzQVVxb+O6fUHIAinrRyuTfuw7GdMc0h4XGI=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UjbwtJz95DM+kWFQOwKyG/T8wkq+pfnpga+AvOVDwPcmgQPZZvuRIYuRMy5YOR4MjJW264UuUW1ektBxerJNZBiRGluGDgk2L8wsPcfLM18nUvP4CL8AcsyFU6y0MHBECisKDvDg/WkuD5MyuRdUc4C6KvilmvZ4vVtr/FzOCsE= 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=FW3JdXuT; 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="FW3JdXuT" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2345c60507bso19693705ad.0 for ; Tue, 27 May 2025 23:26:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1748413602; x=1749018402; 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=egKi337a/czANHevIeQRx3Ni+h1cnqrDQFS+UirVS+M=; b=FW3JdXuTBJ/YJzb0HQkHGhSolA7EsopwF8zH6ZZ1bM3MrMpifKi0ux3cfh1B+yP5Ug L4DEcjTIpifzCKlp7UGNyO4rAWbJhPM0bQO27ID6s0mKaQCZo9cqZgkG+Pm098gi/lxu FlB7urvhqu54RguIy65oiWGA7SVViuFW4Zc/wQT0OFxACeJXHMukfup+sVDAAQmVmFYb A59hc1nucpDJWfAKfUBJBC/jhUAYhZ648GC20TOY4qLFV991tlR3fwsBElg/WeOSuIcC 2WuFhQ82SmhkNqUVdX0eCiahfIhPnOzlXC/LRufcH1MSGJxcYQmb78u3hEKBJrugS79h atSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748413602; x=1749018402; 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=egKi337a/czANHevIeQRx3Ni+h1cnqrDQFS+UirVS+M=; b=i3FCT35e6Vy1O/YrcU9NQETKs3VHESAvhS9E3tROTpnfaNYSXeex+ZytQjKNtygAKU PdEzh6cxvKucYropzciYO6IEOyRzojiF2CvhxXxYTOfVmNwC04OxtGMpyuz7K/tb2pe7 lpPrRhHiC2e0HAbslEB53oFrFJRSJ/NeWsIh8lbF/bJ8UMzucaa3reagF0jteBi166Wf qOmO6XlpX+kVG0ii8fTBUVR6x1Bb4tLi2BDUQ0qJiGHP4YlETtHPlVVSUE7LzfBQeob0 wAjS5DKcgBNjlHiSP6ReBEqj+fk5p0GuSJ4ogE0bo52yoLDItTcXZDAb4iXXwtdK+Te+ UZOA== X-Forwarded-Encrypted: i=1; AJvYcCW1cOxEb8UvlTLpojTl013iEfBbUZkWzooRAOxd08WCNtczGxqAj4rxU0yl5A7IwyeMB/UCtIRr+YMXjWY=@vger.kernel.org X-Gm-Message-State: AOJu0Yyy4ZgROAs+9qDiedEvoHVAplEHSWQe7YhWnTY2/eSLNroOn3KH M/6rSnPXo1sRIhqnfpkL8byW59/ReebiucQyDDIM3ztcFF2WlcFN9RrrwAYrONYN1ek= X-Gm-Gg: ASbGncuiZ77xBGZpcSSDx9BD503IgOw5JiqPT2ecBTSir0g3GwqJRJMK6aC6M4/uxuH oyM/nYLYoww9TZTbMF1+JsTzwIyS2vHa24rMSPFkYlNvvDI7HAt4e7xxBW0KjTLx0IOXr8QAIFb X6zf1qBnjDTnxV60eLivBXb1mM2/NUW309Rmuq2FtcBqZVPyBu+Dc62H+p/5CrgZk+w6Mdmlsmw KtvVbBxWhRcBEPTbgkMprd3TEbUNY9ACsI23Lx/yiylmXbVM+4OPU5hOgpvhoOA4jLKeJDDkhXl CmA6hlec7J109BcxLJqlE/+4DO+Hpl+541zRPDnRdvcjA36QMoOpmzcpbQ9ijN6q1l9asUub6L5 nDgx91hEyLIm9TgNmUWey X-Google-Smtp-Source: AGHT+IHHp2wXUQjEdDIJNL7mrh4ujlV3e+koW9KDIE/Yo3qAyu0LXQJ4yBHqBGF03QD+PyHMJ88zuQ== X-Received: by 2002:a17:902:ce0f:b0:234:bfe3:c4a5 with SMTP id d9443c01a7336-234bfe3c615mr41162865ad.2.1748413602186; Tue, 27 May 2025 23:26:42 -0700 (PDT) Received: from L6YN4KR4K9.bytedance.net ([139.177.225.234]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-234d35acf6dsm4619515ad.201.2025.05.27.23.26.35 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 27 May 2025 23:26:41 -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 Subject: [PATCH v7 4/4] serial: 8250_dw: fix PSLVERR on RX_TIMEOUT Date: Wed, 28 May 2025 14:26:09 +0800 Message-Id: <20250528062609.25104-4-cuiyunhui@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20250528062609.25104-1-cuiyunhui@bytedance.com> References: <20250528062609.25104-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 --- 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); }