From: sashiko-bot@kernel.org
To: "Michael Zaidman" <michael.zaidman@gmail.com>
Cc: linux-input@vger.kernel.org, linux-serial@vger.kernel.org,
linux-gpio@vger.kernel.org, dmitry.torokhov@gmail.com
Subject: Re: [PATCH v4 02/16] HID: ft260: uart: fix serial driver
Date: Tue, 17 Nov 2026 20:49:47 +0000 [thread overview]
Message-ID: <sashiko-outbox-219657@kernel.org> (raw)
In-Reply-To: <20261117200423.19561-3-michael.zaidman@gmail.com>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] HID: ft260: stale saved termios after hotplug in
ft260_uart_port_activate()
- [Medium] HID: ft260: IRQ-unsafe spin_lock() on xmit_fifo_lock in UART
paths
- [Medium] HID: ft260: unlocked xmit_fifo reads in write_room and
chars_in_buffer
- [Medium] HID: ft260: premature port publication in ft260_uart_probe()
and missing tty_port_install()
--
commit 38fb4dc5976e5403c2faec8851d4e9985224e696
Author: Michael Zaidman <michael.zaidman@gmail.com>
HID: ft260: uart: fix serial driver
This commit fixes buffer sharing, power-save wakeup handling, report length
validation, port lifecycle management, and asynchronous TX handling in the
FT260 UART driver.
> diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c
> index 982e2ca7a2de6..869e7b924b30a 100644
> --- a/drivers/hid/hid-ft260.c
> +++ b/drivers/hid/hid-ft260.c
[ ... ]
> @@ -1207,61 +1274,56 @@ static int ft260_uart_transmit_chars(struct ft260_device *port)
> struct kfifo *xmit = &port->xmit_fifo;
> struct tty_struct *tty;
> struct ft260_uart_write_request_report *rep;
> - int len, data_len, ret = 0;
> + int len, ret = 0;
>
> tty = tty_port_tty_get(&port->port);
> + if (!tty)
> + return -EINVAL;
>
> - data_len = kfifo_len(xmit);
> - if (!tty || !data_len) {
> - ret = -EINVAL;
> - goto tty_out;
> - }
> -
> - rep = (struct ft260_uart_write_request_report *)port->write_buf;
> -
> - do {
> - len = min(data_len, FT260_WR_DATA_MAX);
> + rep = (struct ft260_uart_write_request_report *)port->uart_wr_buf;
>
> + while ((len = kfifo_out_spinlocked(xmit, rep->data,
> + FT260_WR_UART_DATA_MAX,
> + &port->xmit_fifo_lock)) > 0) {
> rep->report = FT260_UART_DATA_REPORT_ID(len);
> rep->length = len;
>
> - len = kfifo_out_locked(xmit, rep->data, len, &port->write_lock);
> -
> - ret = ft260_hid_output_report(hdev, (u8 *)rep, len + sizeof(*rep));
> + ret = ft260_hid_output_report(hdev, (u8 *)rep, len + 2);
> if (ret < 0) {
> - hid_err(hdev, "Failed to start transfer, ret %d\n", ret);
> - goto tty_out;
> + /* Nothing sends the rest, and tcdrain() waits */
> + spin_lock(&port->xmit_fifo_lock);
> + kfifo_reset(xmit);
> + spin_unlock(&port->xmit_fifo_lock);
> + break;
> }
[ ... ]
> @@ -1271,18 +1333,16 @@ static int ft260_uart_receive_chars(struct ft260_device *port,
> }
>
> static ssize_t ft260_uart_write(struct tty_struct *tty, const u8 *buf,
> - size_t count)
> + size_t cnt)
> {
> struct ft260_device *port = tty->driver_data;
> - struct hid_device *hdev = port->hdev;
> - int len, ret;
> + ssize_t len;
>
> - len = kfifo_in_locked(&port->xmit_fifo, buf, count, &port->write_lock);
> - ft260_dbg("count: %zu, len: %d", count, len);
> + len = kfifo_in_spinlocked(&port->xmit_fifo, buf, cnt,
> + &port->xmit_fifo_lock);
> + ft260_dbg("count: %zu, len: %zd", cnt, len);
>
> - ret = ft260_uart_transmit_chars(port);
> - if (ret < 0)
> - hid_dbg(hdev, "Failed to transmit chars: %d\n", ret);
> + schedule_work(&port->tx_work);
>
> return len;
> }
[Severity: Medium]
Can taking port->xmit_fifo_lock with plain spin_lock() deadlock against
ft260_uart_write()?
ft260_uart_write() and ft260_uart_transmit_chars() use
kfifo_in_spinlocked() and kfifo_out_spinlocked(), which acquire
port->xmit_fifo_lock with spin_lock_irqsave() because tty->ops->write can
be called from softirq or timer context by a line discipline. However, the
error path in ft260_uart_transmit_chars(), ft260_uart_flush_buffer(),
ft260_uart_port_activate(), and ft260_uart_port_remove() take
port->xmit_fifo_lock with plain spin_lock():
ft260_uart_flush_buffer()
spin_lock(&port->xmit_fifo_lock)
<softirq>
ft260_uart_write()
kfifo_in_spinlocked(..., &port->xmit_fifo_lock)
spin_lock_irqsave(&port->xmit_fifo_lock) <-- spins forever
> static unsigned int ft260_uart_write_room(struct tty_struct *tty)
> {
> struct ft260_device *port = tty->driver_data;
>
> - return FIFO_SIZE - kfifo_len(&port->xmit_fifo);
> + return kfifo_avail(&port->xmit_fifo);
> }
>
> static unsigned int ft260_uart_chars_in_buffer(struct tty_struct *tty)
> @@ -1301,15 +1361,33 @@ static unsigned int ft260_uart_chars_in_buffer(struct tty_struct *tty)
> return kfifo_len(&port->xmit_fifo);
> }
>
> +static void ft260_uart_flush_buffer(struct tty_struct *tty)
> +{
> + struct ft260_device *port = tty->driver_data;
> +
> + spin_lock(&port->xmit_fifo_lock);
> + kfifo_reset(&port->xmit_fifo);
> + spin_unlock(&port->xmit_fifo_lock);
> +
> + tty_wakeup(tty);
> +}
[Severity: Medium]
Can ft260_uart_write_room() and ft260_uart_chars_in_buffer() race with
kfifo_reset() and return a bogus count?
Both functions read port->xmit_fifo.kfifo.in and port->xmit_fifo.kfifo.out
without holding port->xmit_fifo_lock, while ft260_uart_flush_buffer() and
the error path in ft260_uart_transmit_chars() call kfifo_reset(), which
zeros out and in as two separate stores:
CPU0 CPU1
ft260_uart_flush_buffer() ft260_uart_chars_in_buffer()
spin_lock(&port->xmit_fifo_lock)
kfifo_reset(&port->xmit_fifo)
fifo->out = 0;
kfifo_len(&port->xmit_fifo)
fifo->in - fifo->out
fifo->in = 0;
spin_unlock(&port->xmit_fifo_lock)
Once fifo->in and fifo->out have advanced past XMIT_FIFO_SIZE, observing
one reset index alongside one non-zero index Either inflates the count past
XMIT_FIFO_SIZE or underflows unsigned int in fifo->in - fifo->out (or in
kfifo_avail()).
[ ... ]
> @@ -1503,54 +1592,79 @@ static void ft260_uart_start_wakeup(struct timer_list *t)
[ ... ]
> +static int ft260_uart_port_activate(struct tty_port *tport, struct tty_struct *tty)
> +{
> + int ret;
> + int baudrate;
> + struct ft260_get_uart_settings_report cfg;
> + struct ft260_device *port = container_of(tport, struct ft260_device, port);
> +
> + mutex_lock(&port->lock);
> + if (port->removed) {
> + mutex_unlock(&port->lock);
> + return -ENODEV;
> + }
>
> set_bit(TTY_IO_ERROR, &tty->flags);
>
> - spin_lock(&port->write_lock);
> + spin_lock(&port->xmit_fifo_lock);
> kfifo_reset(&port->xmit_fifo);
> - spin_unlock(&port->write_lock);
> + spin_unlock(&port->xmit_fifo_lock);
>
> - ft260_uart_change_speed(port, &tty->termios, NULL);
> clear_bit(TTY_IO_ERROR, &tty->flags);
>
> - if (port->reschedule_work) {
> + /*
> + * The port setting may remain intact after session termination.
> + * Then, when reopening the port without configuring the port
> + * setting, we need to retrieve the baud rate from the device to
> + * reactivate the wakeup workaround if needed.
> + */
> + ret = ft260_get_uart_settings(port->hdev, &cfg);
> + if (ret) {
> + mutex_unlock(&port->lock);
> + return ret;
> + }
> +
> + baudrate = get_unaligned_le32(&cfg.baudrate);
> + ft260_uart_wakeup_workaraund_enable(port,
> + baudrate > FT260_UART_EN_PW_SAVE_BAUD);
[Severity: Medium]
Does removing ft260_uart_change_speed() from ft260_uart_port_activate()
leave tty->termios out of sync with the hardware across an unplug and
replug?
ft260_tty_driver is allocated with TTY_DRIVER_REAL_RAW |
TTY_DRIVER_DYNAMIC_DEV, without TTY_DRIVER_RESET_SAVED_TERMIOS. When a
user configures a non-default baud rate (such as 115200), closes the tty,
and unplugs and replugs the device into the same minor index,
ft260_uart_probe() resets the chip to 9600 8N1 while
tty_port_register_device_attr() keeps the old termios in
ft260_tty_driver->termios[index]. On the next open, tty_init_termios()
restores 115200 into tty->termios, while ft260_uart_port_activate() leaves
the hardware running at 9600.
Passing TTY_DRIVER_RESET_SAVED_TERMIOS to tty_alloc_driver() resets the
saved termios to init_termios (9600 8N1) when the port is registered.
[ ... ]
> @@ -1613,38 +1736,29 @@ static int ft260_i2c_probe(struct hid_device *hdev, struct ft260_device *dev)
>
> static int ft260_uart_probe(struct hid_device *hdev, struct ft260_device *dev)
> {
> - struct ft260_configure_uart_request req;
> + struct ft260_configure_uart_request_report req;
> int ret;
> struct device *devt;
>
> INIT_WORK(&dev->wakeup_work, ft260_uart_do_wakeup);
> - // FIXME: Do I need that if I have cancel_work_sync?
> + INIT_WORK(&dev->tx_work, ft260_uart_tx_work);
> // FIXME: are all kfifo access secured by lock? with irq or not?
> - dev->reschedule_work = false;
> + ft260_uart_wakeup_workaraund_enable(dev, true);
> /* Work not started at this point */
> timer_setup(&dev->wakeup_timer, ft260_uart_start_wakeup, 0);
>
> tty_port_init(&dev->port);
> dev->port.ops = &ft260_uart_port_ops;
> + /* The destructor unlinks it even if ft260_uart_add_port() fails */
> + INIT_LIST_HEAD(&dev->device_list);
>
> ret = ft260_uart_add_port(dev);
> if (ret) {
> hid_err(hdev, "failed to add port\n");
> goto err_port;
> }
> - devt = tty_port_register_device_attr(&dev->port,
> - ft260_tty_driver,
> - dev->index, &hdev->dev,
> - dev, NULL);
> - if (IS_ERR(devt)) {
> - hid_err(hdev, "failed to register tty port\n",
> - ret = PTR_ERR(devt);
> - goto err_register_tty;
> - }
> - hid_info(hdev, "Registering device /dev/%s%d\n",
> - ft260_tty_driver->name, dev->index);
>
> - /* Send Feature Report to Configure FT260 as UART 9600-8-N-1 */
> + /* Configure UART to 9600n8 */
> req.report = FT260_SYSTEM_SETTINGS;
> req.request = FT260_SET_UART_CONFIG;
> req.flow_ctrl = FT260_UART_CFG_FLOW_CTRL_NONE;
> @@ -1656,16 +1770,25 @@ static int ft260_uart_probe(struct hid_device *hdev, struct ft260_device *dev)
>
> ret = ft260_hid_feature_report_set(hdev, (u8 *)&req, sizeof(req));
> if (ret < 0) {
> - hid_err(hdev, "ft260_hid_feature_report_set failed: %d\n",
> - ret);
> + hid_err(hdev, "failed to configure uart: %d\n", ret);
> + goto err_hid_report;
> + }
> +
> + devt = tty_port_register_device_attr(&dev->port,
> + ft260_tty_driver,
> + dev->index, &hdev->dev,
> + dev, NULL);
[Severity: Medium]
Can publishing dev in ft260_uart_add_port() before
tty_port_register_device_attr() allow a concurrent open to reach
tty_init_dev() before driver->ports[dev->index] is updated?
ft260_uart_install() looks up the port in ft260_uart_device_list via
ft260_uart_port_get() and calls tty_standard_install() instead of
tty_port_install(), leaving tty->port NULL so tty_init_dev() falls back to
driver->ports[idx]:
drivers/hid/hid-ft260.c:ft260_uart_install() {
int idx = tty->index;
struct ft260_device *port = ft260_uart_port_get(idx);
...
ret = tty_standard_install(driver, tty);
}
Because ft260_uart_add_port() adds dev to ft260_uart_device_list before the
sleeping ft260_hid_feature_report_set() transfer, a concurrent open via
tty_kopen_exclusive() or the retry_open loop in tty_open() can install the
tty while driver->ports[idx] is still NULL (triggering WARN_RATELIMIT() in
tty_init_dev()) or still points to a previously removed and freed
ft260_device (since tty_port_unregister_device() does not clear
driver->ports[idx]):
CPU0 CPU1
ft260_uart_probe()
ft260_uart_add_port(dev)
ft260_hid_feature_report_set()
tty_open_by_driver()
tty_init_dev()
ft260_uart_install()
tty_standard_install()
tty->port = driver->ports[idx];
tty->port->itty = tty;
<-- NULL deref or use-after-free
tty_port_register_device_attr()
Calling ft260_uart_add_port() after ft260_hid_feature_report_set() and
using tty_port_install(&port->port, driver, tty) in ft260_uart_install()
avoids both problems.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117200423.19561-1-michael.zaidman@gmail.com?part=2
next prev parent reply other threads:[~2026-11-17 20:49 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 20:04 [PATCH v4 00/16] HID: ft260: add UART and GPIO support, plus I2C fixes Michael Zaidman
2026-11-17 20:04 ` [PATCH v4 01/16] HID: ft260: add serial driver Michael Zaidman
2026-11-17 20:32 ` sashiko-bot
2026-11-17 20:04 ` [PATCH v4 02/16] HID: ft260: uart: fix " Michael Zaidman
2026-11-17 20:49 ` sashiko-bot [this message]
2026-11-17 20:04 ` [PATCH v4 03/16] HID: ft260: add GPIO support Michael Zaidman
2026-11-17 20:23 ` sashiko-bot
2026-11-17 20:04 ` [PATCH v4 04/16] HID: ft260: i2c: reduce driver module loading time Michael Zaidman
2026-11-17 20:11 ` sashiko-bot
2026-11-17 20:04 ` [PATCH v4 05/16] HID: ft260: fix sysfs store failure notice for big numbers Michael Zaidman
2026-11-17 20:12 ` sashiko-bot
2026-11-17 20:04 ` [PATCH v4 06/16] HID: ft260: i2c: reduce bus-error message severity Michael Zaidman
2026-11-17 20:13 ` sashiko-bot
2026-11-17 20:04 ` [PATCH v4 07/16] HID: ft260: uart: enable flow control Michael Zaidman
2026-11-17 20:21 ` sashiko-bot
2026-11-17 20:04 ` [PATCH v4 08/16] HID: ft260: uart: add modem pins control via ioctl Michael Zaidman
2026-11-17 20:24 ` sashiko-bot
2026-11-17 20:04 ` [PATCH v4 09/16] HID: ft260: group sysfs attrs per HID interface Michael Zaidman
2026-11-17 20:30 ` sashiko-bot
2026-11-17 20:04 ` [PATCH v4 10/16] HID: ft260: i2c: fix large write transaction failure Michael Zaidman
2026-11-17 20:30 ` sashiko-bot
2026-11-17 20:04 ` [PATCH v4 11/16] HID: ft260: workaround for TN_189 errata endpoint STALL after enumeration Michael Zaidman
2026-11-17 20:50 ` sashiko-bot
2026-11-17 20:04 ` [PATCH v4 12/16] HID: ft260: i2c: abort in-flight transfers with STOP before reset Michael Zaidman
2026-11-17 20:33 ` sashiko-bot
2026-11-17 20:04 ` [PATCH v4 13/16] HID: ft260: i2c: reject an I2C clock outside the supported range Michael Zaidman
2026-11-17 20:34 ` sashiko-bot
2026-11-17 20:04 ` [PATCH v4 14/16] HID: ft260: i2c: fix i2c_reset sysfs store return value Michael Zaidman
2026-11-17 20:35 ` sashiko-bot
2026-11-17 20:04 ` [PATCH v4 15/16] HID: ft260: i2c: recover a bus held by a slave Michael Zaidman
2026-11-17 20:44 ` sashiko-bot
2026-11-17 20:04 ` [PATCH v4 16/16] HID: ft260: use lock guards Michael Zaidman
2026-11-17 20:36 ` 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-219657@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=dmitry.torokhov@gmail.com \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=michael.zaidman@gmail.com \
--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