From nobody Mon Feb 9 04:56:42 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 23A2A15B107; Mon, 5 Aug 2024 10:21:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722853261; cv=none; b=KPf0UE0Dx6ZyQMrYSlSwaXOutYiY6hWFRa1AIR7cYxfvpKRp0ePeT7RsQQJIodLYkQXTpt5JdGkJ8giR4AE0F8VwEn0sbDBTogqOfUde5I6BMUqlBaMjPCqDls+CFIZGghuBFPzIebZ1TQzmhAXKoLs0rja9wK/tfsdeBjf8F9c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722853261; c=relaxed/simple; bh=/UDtlDDaO6xaKNk6bwmSQ1jG91hzPcY2kjgBsyxYblM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CGEkpgxNm2qQRpdKgbFz2lke/7P57SjK/qb1+y1pBSP/WA+1hCvT8fHPQbn1QAHSRxfKE9x5cb2b5UIcN4VViASfMQG3FVN6XDUJfKdglnBfTVPuDa3lzXyzl8dG/IqH0q0R63z+b20Zl+ae2UoJT71zrDfBoNCUeo5rMjfXUF8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MnvY6WmM; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MnvY6WmM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ACD6EC4AF0D; Mon, 5 Aug 2024 10:20:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722853260; bh=/UDtlDDaO6xaKNk6bwmSQ1jG91hzPcY2kjgBsyxYblM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MnvY6WmM6Nm8+7yvH46ze/JLkbHzyJKPRaQVXXQQU74TyDSHHgnwV0VU7Au2micqj Hhm5O/QAMHcoxyzNR8ncAcPRrFfo7chPtpG2d/X5zm6hztstT5gyTbD0cmA5BgqIep OMOj26xmqAx8a6txvaU8W9Ie1QdbdC3rJ67xRLV7asbvLvQwl780wBp4Gyf1qCLjdm 5AR2h3Q77B+s9b+E+szvKciJd2SP/SqnR1hDoK7VdW5ttV2mbLVfzpvDv2M24VhpHE kw5i1xWGi4tS/+tC8f6lwBkvD+XYWwZtpguHtW30SuZ0G4JCwgXl8ZqUMoPxhz/yDI kXQCeCTIGoNFQ== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 05/13] serial: use guards for simple mutex locks Date: Mon, 5 Aug 2024 12:20:38 +0200 Message-ID: <20240805102046.307511-6-jirislaby@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240805102046.307511-1-jirislaby@kernel.org> References: <20240805102046.307511-1-jirislaby@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Guards can help to make the code more readable. So use it wherever they do so. On many places labels and 'ret' locals are eliminated completely. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/serial/serial_core.c | 111 +++++++++++++------------------ 1 file changed, 46 insertions(+), 65 deletions(-) diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_c= ore.c index d63e9b636e02..0e6c1e51da99 100644 --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -1096,21 +1096,19 @@ static int uart_tiocmget(struct tty_struct *tty) struct uart_state *state =3D tty->driver_data; struct tty_port *port =3D &state->port; struct uart_port *uport; - int result =3D -EIO; + int result; + + guard(mutex)(&port->mutex); =20 - mutex_lock(&port->mutex); uport =3D uart_port_check(state); - if (!uport) - goto out; + if (!uport || tty_io_error(tty)) + return -EIO; + + uart_port_lock_irq(uport); + result =3D uport->mctrl; + result |=3D uport->ops->get_mctrl(uport); + uart_port_unlock_irq(uport); =20 - if (!tty_io_error(tty)) { - uart_port_lock_irq(uport); - result =3D uport->mctrl; - result |=3D uport->ops->get_mctrl(uport); - uart_port_unlock_irq(uport); - } -out: - mutex_unlock(&port->mutex); return result; } =20 @@ -1120,20 +1118,16 @@ uart_tiocmset(struct tty_struct *tty, unsigned int = set, unsigned int clear) struct uart_state *state =3D tty->driver_data; struct tty_port *port =3D &state->port; struct uart_port *uport; - int ret =3D -EIO; =20 - mutex_lock(&port->mutex); + guard(mutex)(&port->mutex); + uport =3D uart_port_check(state); - if (!uport) - goto out; + if (!uport || tty_io_error(tty)) + return -EIO; =20 - if (!tty_io_error(tty)) { - uart_update_mctrl(uport, set, clear); - ret =3D 0; - } -out: - mutex_unlock(&port->mutex); - return ret; + uart_update_mctrl(uport, set, clear); + + return 0; } =20 static int uart_break_ctl(struct tty_struct *tty, int break_state) @@ -1141,19 +1135,17 @@ static int uart_break_ctl(struct tty_struct *tty, i= nt break_state) struct uart_state *state =3D tty->driver_data; struct tty_port *port =3D &state->port; struct uart_port *uport; - int ret =3D -EIO; =20 - mutex_lock(&port->mutex); + guard(mutex)(&port->mutex); + uport =3D uart_port_check(state); if (!uport) - goto out; + return -EIO; =20 if (uport->type !=3D PORT_UNKNOWN && uport->ops->break_ctl) uport->ops->break_ctl(uport, break_state); - ret =3D 0; -out: - mutex_unlock(&port->mutex); - return ret; + + return 0; } =20 static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *s= tate) @@ -1170,17 +1162,14 @@ static int uart_do_autoconfig(struct tty_struct *tt= y, struct uart_state *state) * changing, and hence any extra opens of the port while * we're auto-configuring. */ - if (mutex_lock_interruptible(&port->mutex)) - return -ERESTARTSYS; + scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &port->mutex) { + uport =3D uart_port_check(state); + if (!uport) + return -EIO; =20 - uport =3D uart_port_check(state); - if (!uport) { - ret =3D -EIO; - goto out; - } + if (tty_port_users(port) !=3D 1) + return -EBUSY; =20 - ret =3D -EBUSY; - if (tty_port_users(port) =3D=3D 1) { uart_shutdown(tty, state); =20 /* @@ -1201,14 +1190,15 @@ static int uart_do_autoconfig(struct tty_struct *tt= y, struct uart_state *state) uport->ops->config_port(uport, flags); =20 ret =3D uart_startup(tty, state, true); - if (ret =3D=3D 0) - tty_port_set_initialized(port, true); + if (ret < 0) + return ret; if (ret > 0) - ret =3D 0; + return 0; + + tty_port_set_initialized(port, true); } -out: - mutex_unlock(&port->mutex); - return ret; + + return 0; } =20 static void uart_enable_ms(struct uart_port *uport) @@ -1703,10 +1693,11 @@ static void uart_set_termios(struct tty_struct *tty, unsigned int iflag_mask =3D IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK; bool sw_changed =3D false; =20 - mutex_lock(&state->port.mutex); + guard(mutex)(&state->port.mutex); + uport =3D uart_port_check(state); if (!uport) - goto out; + return; =20 /* * Drivers doing software flow control also need to know @@ -1729,9 +1720,8 @@ static void uart_set_termios(struct tty_struct *tty, tty->termios.c_ospeed =3D=3D old_termios->c_ospeed && tty->termios.c_ispeed =3D=3D old_termios->c_ispeed && ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) =3D=3D 0= && - !sw_changed) { - goto out; - } + !sw_changed) + return; =20 uart_change_line_settings(tty, state, old_termios); /* reload cflag from termios; port driver may have overridden flags */ @@ -1748,8 +1738,6 @@ static void uart_set_termios(struct tty_struct *tty, mask |=3D TIOCM_RTS; uart_set_mctrl(uport, mask); } -out: - mutex_unlock(&state->port.mutex); } =20 /* @@ -2043,10 +2031,11 @@ static void uart_line_info(struct seq_file *m, stru= ct uart_driver *drv, int i) unsigned int status; int mmio; =20 - mutex_lock(&port->mutex); + guard(mutex)(&port->mutex); + uport =3D uart_port_check(state); if (!uport) - goto out; + return; =20 mmio =3D uport->iotype >=3D UPIO_MEM; seq_printf(m, "%d: uart:%s %s%08llX irq:%d", @@ -2058,7 +2047,7 @@ static void uart_line_info(struct seq_file *m, struct= uart_driver *drv, int i) =20 if (uport->type =3D=3D PORT_UNKNOWN) { seq_putc(m, '\n'); - goto out; + return; } =20 if (capable(CAP_SYS_ADMIN)) { @@ -2109,8 +2098,6 @@ static void uart_line_info(struct seq_file *m, struct= uart_driver *drv, int i) seq_putc(m, '\n'); #undef STATBIT #undef INFOBIT -out: - mutex_unlock(&port->mutex); } =20 static int uart_proc_show(struct seq_file *m, void *v) @@ -2387,13 +2374,12 @@ int uart_suspend_port(struct uart_driver *drv, stru= ct uart_port *uport) struct device *tty_dev; struct uart_match match =3D {uport, drv}; =20 - mutex_lock(&port->mutex); + guard(mutex)(&port->mutex); =20 tty_dev =3D device_find_child(&uport->port_dev->dev, &match, serial_match= _port); if (tty_dev && device_may_wakeup(tty_dev)) { enable_irq_wake(uport->irq); put_device(tty_dev); - mutex_unlock(&port->mutex); return 0; } put_device(tty_dev); @@ -2454,8 +2440,6 @@ int uart_suspend_port(struct uart_driver *drv, struct= uart_port *uport) console_stop(uport->cons); =20 uart_change_pm(state, UART_PM_STATE_OFF); -unlock: - mutex_unlock(&port->mutex); =20 return 0; } @@ -2469,14 +2453,13 @@ int uart_resume_port(struct uart_driver *drv, struc= t uart_port *uport) struct uart_match match =3D {uport, drv}; struct ktermios termios; =20 - mutex_lock(&port->mutex); + guard(mutex)(&port->mutex); =20 tty_dev =3D device_find_child(&uport->port_dev->dev, &match, serial_match= _port); if (!uport->suspended && device_may_wakeup(tty_dev)) { if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq)))) disable_irq_wake(uport->irq); put_device(tty_dev); - mutex_unlock(&port->mutex); return 0; } put_device(tty_dev); @@ -2549,8 +2532,6 @@ int uart_resume_port(struct uart_driver *drv, struct = uart_port *uport) tty_port_set_suspended(port, false); } =20 - mutex_unlock(&port->mutex); - return 0; } EXPORT_SYMBOL(uart_resume_port); --=20 2.46.0