Linux Serial subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Markus Probst" <markus.probst@posteo.de>
Cc: linux-serial@vger.kernel.org, ojeda@kernel.org, gary@garyguo.net
Subject: Re: [PATCH RESEND v6 1/4] tty: serdev: Add mutex lock
Date: Fri, 04 Dec 2026 17:22:45 +0000	[thread overview]
Message-ID: <sashiko-outbox-250302@kernel.org> (raw)
In-Reply-To: <20261204-rust_serdev_probe_refactor-v6-1-0e12b6e6853b@posteo.de>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] tty: serdev: returning -EALREADY in ttyport_open() breaks
  hci_mrvl probe
- [Medium] tty: serdev: fallback close runs after serdev free and misses
  driver unbind
- [Medium] tty: serdev: serdev_tty_port_register() calls mutex_init()
  after client_ops
- [Low] tty: serdev: data race from __test_and_clear_bit() in
  ttyport_close()
- [Low] tty: serdev: dev_WARN() used instead of dev_warn() in
  serdev_device_remove()
--

commit 7655d9d3e9b00ecf625ad8092540311e1db4b89e
Author: Markus Probst <markus.probst@posteo.de>

tty: serdev: Add mutex lock

This commit adds mutexes to serdev-ttyport to serialize open, close, and
receive paths, returns -EALREADY on duplicate open, makes close
idempotent, and closes leaked devices in serdev_device_remove().

> diff --git a/drivers/tty/serdev/core.c b/drivers/tty/serdev/core.c
> index 7500efcdfc218..86d9cf8215edc 100644
> --- a/drivers/tty/serdev/core.c
> +++ b/drivers/tty/serdev/core.c
> @@ -142,6 +142,13 @@ void serdev_device_remove(struct serdev_device *serdev)
>  	struct serdev_controller *ctrl = serdev->ctrl;
>  
>  	device_unregister(&serdev->dev);
> +
> +	/* Warn if driver did not close the serial device. */
> +	if (ctrl->ops->close && ctrl->ops->close(ctrl)) {
> +		dev_WARN(&ctrl->dev, "Child driver did not close the serial device");

[Severity: Low]
As reported by Greg Kroah-Hartman on v4 [1], should this use dev_warn()
instead of dev_WARN()?  dev_WARN() expands to WARN(1, ...), which prints a
backtrace and triggers panic_on_warn; dev_warn() (with a trailing newline)
logs a normal warning.

> +		pm_runtime_put(&ctrl->dev);
> +	}
> +
>  	ctrl->serdev = NULL;
>  }

[Severity: Medium]
Can this trigger a use-after-free on serdev when a child driver leaves the
port open?

For direct callers of serdev_device_remove() such as dell-uart-backlight,
device_unregister(&serdev->dev) drops the last reference and frees serdev
via serdev_device_release() before ctrl->ops->close(ctrl) clears
SERPORT_ACTIVE and before ctrl->serdev is set to NULL:

CPU0                              CPU1
serdev_device_remove()
  device_unregister(&serdev->dev)
    device_del(&serdev->dev)
    put_device(&serdev->dev)
      kfree(serdev)
                                  ttyport_receive_buf()
                                    serdev_controller_receive_buf()
                                      serdev->ops->receive_buf(...)
                                      <-- use-after-free
  ctrl->ops->close(ctrl)
  ctrl->serdev = NULL

Splitting device_unregister() into device_del(), the fallback close and
ctrl->serdev = NULL, and finally put_device() keeps serdev alive until
callbacks stop.

Separately, driver unbind (such as sysfs unbind or rmmod) runs
serdev_drv_remove() and device_unbind_cleanup() without calling
serdev_device_remove(), so an unclosed port stays active and subsequent
re-probes fail permanently with -EALREADY.  Handling the fallback on
BUS_NOTIFY_UNBOUND_DRIVER would run after devres_release_all() on unbind
as well.

[ ... ]

> diff --git a/drivers/tty/serdev/serdev-ttyport.c b/drivers/tty/serdev/serdev-ttyport.c
> index b6638f9f40d2d..17c18d4b0cb8f 100644
> --- a/drivers/tty/serdev/serdev-ttyport.c
> +++ b/drivers/tty/serdev/serdev-ttyport.c

[ ... ]

> @@ -99,14 +103,23 @@ static int ttyport_open(struct serdev_controller *ctrl)
>  	struct ktermios ktermios;
>  	int ret;
>  
> +	mutex_lock(&serport->lock);
> +
> +	if (test_bit(SERPORT_ACTIVE, &serport->flags)) {
> +		ret = -EALREADY;
> +		goto err_flags_unlock;
> +	}

[Severity: High]
Does returning -EALREADY here break probing of Marvell serdev Bluetooth
devices in drivers/bluetooth/hci_mrvl.c?

During mrvl_serdev_probe(), hci_uart_register_device_priv() calls
serdev_device_open(hu->serdev) and then invokes mrvl_open(), which calls
serdev_device_open(hu->serdev) a second time and fails probe on any error.
hci_mrvl.c (including its duplicate close in mrvl_close()) would need to
be updated before rejecting duplicate opens with -EALREADY.

[ ... ]

> -static void ttyport_close(struct serdev_controller *ctrl)
> +static bool ttyport_close(struct serdev_controller *ctrl)
>  {
>  	struct serport *serport = serdev_controller_get_drvdata(ctrl);
> -	struct tty_struct *tty = serport->tty;
> +	struct tty_struct *tty;
> +
> +	guard(mutex)(&serport->lock);
>  
> -	clear_bit(SERPORT_ACTIVE, &serport->flags);
> +	tty = serport->tty;
> +
> +	scoped_guard(mutex, &serport->receive_lock)
> +		if (!__test_and_clear_bit(SERPORT_ACTIVE, &serport->flags))
> +			return false;

[Severity: Low]
Does switching to non-atomic __test_and_clear_bit() introduce a data race
on serport->flags?

ttyport_close() clears SERPORT_ACTIVE before tty->ops->close() stops TX
interrupts, while ttyport_write_wakeup() and ttyport_write_buf() read
serport->flags with test_bit() without holding serport->lock or
serport->receive_lock.  This triggers KCSAN data-race reports under
CONFIG_KCSAN_STRICT=y and performs a plain non-atomic write on
architectures using generic non-atomic bitops.  Using test_and_clear_bit()
avoids the race and matches set_bit() in ttyport_open().

[ ... ]

> @@ -290,6 +317,9 @@ struct device *serdev_tty_port_register(struct tty_port *port,
>  	port->client_ops = &client_ops;
>  	port->client_data = ctrl;
>  
> +	mutex_init(&serport->lock);
> +	mutex_init(&serport->receive_lock);

[Severity: Medium]
Can ttyport_receive_buf() lock serport->receive_lock before mutex_init()
runs?

UART drivers such as ip22zilog_probe() register their RX IRQ before
uart_add_one_port(), and uart_configure_port() enables RX interrupts
before serdev_tty_port_register() runs.  Incoming RX bytes schedule
flush_to_ldisc(), which only holds buf->lock (not port->mutex) and enters
ttyport_receive_buf() before SERPORT_ACTIVE is checked:

CPU0                              CPU1
serdev_tty_port_register()
  port->client_ops = &client_ops
  port->client_data = ctrl
                                  flush_to_ldisc()
                                    ttyport_receive_buf()
                                      mutex_lock(&serport->receive_lock)
                                      <-- uninitialized mutex
  mutex_init(&serport->lock)
  mutex_init(&serport->receive_lock)

Locking an uninitialized mutex or racing with mutex_init() triggers
lockdep/debug-mutex warnings and can corrupt mutex state.  Initializing
both mutexes (and setting port->client_data) before assigning
port->client_ops avoids the race.

[1] https://lore.kernel.org/r/2026101758-confidant-comprised-e1c0@gregkh

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-rust_serdev_probe_refactor-v6-0-0e12b6e6853b@posteo.de?part=1

  reply	other threads:[~2026-12-04 17:22 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 16:58 [PATCH RESEND v6 0/4] rust: serdev: Refactor Markus Probst
2026-12-04 16:58 ` [PATCH RESEND v6 1/4] tty: serdev: Add mutex lock Markus Probst
2026-12-04 17:22   ` sashiko-bot [this message]
2026-12-04 17:45     ` Markus Probst
2026-12-04 16:58 ` [PATCH RESEND v6 2/4] tty: serdev: Export functions to pause receive_buf callback calls Markus Probst
2026-12-04 17:23   ` sashiko-bot
2026-12-04 16:58 ` [PATCH RESEND v6 3/4] rust: serdev: Replace `active` mutex with receive pause Markus Probst
2026-12-04 16:58 ` [PATCH RESEND v6 4/4] rust: serdev: Simplify callbacks Markus Probst
2026-12-04 17:13   ` sashiko-bot
2026-12-04 17:37     ` Markus Probst
2026-12-04 17:23 ` [PATCH RESEND v6 0/4] rust: serdev: Refactor sashiko-bot

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=sashiko-outbox-250302@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-serial@vger.kernel.org \
    --cc=markus.probst@posteo.de \
    --cc=ojeda@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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