All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "FUJITA Tomonori" <tomo@flapping.org>
Cc: gary@garyguo.net, linux-serial@vger.kernel.org, ojeda@kernel.org
Subject: Re: [PATCH v1] rust: serdev: use Delta<Jiffy> for timeouts
Date: Wed, 30 Sep 2026 04:21:57 +0000	[thread overview]
Message-ID: <20260930042157.913CE1F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260930041339.1551129-1-tomo@flapping.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] rust: serdev: negative timeout clamped to 0 causes infinite wait
--

commit 68348e8f0bd03a5788e4d72cf264605070a06317
Author: FUJITA Tomonori <fujita.tomonori@gmail.com>

rust: serdev: use Delta<Jiffy> for timeouts

write_all() and wait_until_sent() take the timeout as a raw Jiffies,
which is a c_ulong alias. The type does not show the unit.

> diff --git a/rust/kernel/serdev.rs b/rust/kernel/serdev.rs
> index 17ca504b7f8d6..094503f059ff5 100644
> --- a/rust/kernel/serdev.rs
> +++ b/rust/kernel/serdev.rs

[ ... ]

> @@ -481,22 +484,19 @@ pub fn set_parity(&self, parity: Parity) -> Result {
>      /// [`kernel::error::code::ETIMEDOUT`] or [`kernel::error::code::ERESTARTSYS`] if interrupted
>      /// before any bytes were written. [`kernel::error::code::EINVAL`] if `data.len() > i32::MAX`.
>      #[inline]
> -    pub fn write_all(&self, data: &[u8], timeout: Jiffies) -> Result<usize> {
> +    pub fn write_all(&self, data: &[u8], timeout: Delta<Jiffy>) -> Result<usize> {
>          if data.len() > i32::MAX as usize {
>              return Err(EINVAL);
>          }
>  
> +        let timeout = isize::max(timeout.as_jiffies(), 0);

[Severity: High]
Does this cause an unintended infinite wait when a negative timeout is passed?

When a driver calculates a deadline that is already in the past, it will
produce a negative Delta<Jiffy>, which this clamps to 0.

However, the underlying C function serdev_device_write() treats a timeout of
0 as an infinite wait:

drivers/tty/serdev/core.c:serdev_device_write() {
    ...
	if (timeout == 0)
		timeout = MAX_SCHEDULE_TIMEOUT;
    ...
}

Should negative timeouts instead return immediately or fail with -ETIMEDOUT
rather than causing the thread to hang indefinitely?

> +
>          // SAFETY:
>          // - `self.as_raw()` is guaranteed to be a pointer to a valid `serdev_device`.
>          // - `data.as_ptr()` is guaranteed to be a valid array pointer with the size of
>          //   `data.len()`.
>          let ret = unsafe {
> -            bindings::serdev_device_write(
> -                self.as_raw(),
> -                data.as_ptr(),
> -                data.len(),
> -                isize::try_from(timeout).unwrap_or_default(),
> -            )
> +            bindings::serdev_device_write(self.as_raw(), data.as_ptr(), data.len(), timeout)
>          };

[ ... ]

> @@ -547,14 +547,11 @@ pub fn write_flush(&self) {
>      ///
>      /// Use a timeout of 0 to wait indefinitely.
>      #[inline]
> -    pub fn wait_until_sent(&self, timeout: Jiffies) {
> +    pub fn wait_until_sent(&self, timeout: Delta<Jiffy>) {
> +        let timeout = isize::max(timeout.as_jiffies(), 0);

[Severity: High]
Can this also cause an unintended infinite wait?

Similar to write_all(), if wait_until_sent() is called with a negative timeout,
it is clamped to 0.

The underlying C implementation tty_wait_until_sent() treats a timeout of 0
as an infinite wait:

drivers/tty/tty_ioctl.c:tty_wait_until_sent() {
    ...
	if (!timeout)
		timeout = MAX_SCHEDULE_TIMEOUT;
    ...
}

Would it be safer to handle expired deadlines without waiting indefinitely?

> +
>          // SAFETY: `self.as_raw()` is guaranteed to be a pointer to a valid `serdev_device`.
> -        unsafe {
> -            bindings::serdev_device_wait_until_sent(
> -                self.as_raw(),
> -                isize::try_from(timeout).unwrap_or_default(),
> -            )
> -        };
> +        unsafe { bindings::serdev_device_wait_until_sent(self.as_raw(), timeout) };
>      }
>  }

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260930041339.1551129-1-tomo@flapping.org?part=1

  reply	other threads:[~2026-09-30  4:21 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-30  4:13 [PATCH v1] rust: serdev: use Delta<Jiffy> for timeouts FUJITA Tomonori
2026-09-30  4:21 ` sashiko-bot [this message]
2026-09-30 12:59 ` Markus Probst
2026-09-30 13:52   ` Gary Guo
2026-09-30 14:07     ` Markus Probst
2026-10-01  0:29     ` FUJITA Tomonori
2026-10-01  0:39       ` Gary Guo
2026-10-01  1:28         ` FUJITA Tomonori
2026-10-01  2:27           ` Gary Guo
2026-10-01  4:26             ` FUJITA Tomonori

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=20260930042157.913CE1F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-serial@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tomo@flapping.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.