Rust for Linux List
 help / color / mirror / Atom feed
From: Markus Probst <markus.probst@posteo.de>
To: Gary Guo <gary@garyguo.net>, FUJITA Tomonori <tomo@flapping.org>,
	 dakr@kernel.org, ojeda@kernel.org
Cc: a.hindborg@kernel.org, acourbot@nvidia.com, aliceryhl@google.com,
	 bjorn3_gh@protonmail.com, boqun@kernel.org,
	daniel.almeida@collabora.com,  lossin@kernel.org,
	tamird@kernel.org, tmgross@umich.edu, work@onurozkan.dev,
		linux-serial@vger.kernel.org, rust-for-linux@vger.kernel.org,
	FUJITA Tomonori	 <fujita.tomonori@gmail.com>
Subject: Re: [PATCH v1] rust: serdev: use Delta<Jiffy> for timeouts
Date: Wed, 30 Sep 2026 14:07:21 +0000	[thread overview]
Message-ID: <d2437243001e55ef6a845c88cfb19dc162a0c1b6.camel@posteo.de> (raw)
In-Reply-To: <DLSP8RHJMYFZ.3II53ESHY15IL@garyguo.net>

[-- Attachment #1: Type: text/plain, Size: 4492 bytes --]

On Wed, 2026-09-30 at 14:52 +0100, Gary Guo wrote:
> On Wed Sep 30, 2026 at 1:59 PM BST, Markus Probst wrote:
> > On Wed, 2026-09-30 at 13:13 +0900, FUJITA Tomonori wrote:
> > > From: FUJITA Tomonori <fujita.tomonori@gmail.com>
> > > 
> > > 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.
> > > 
> > > Take the timeout as Delta<Jiffy> instead. Clamp a negative timeout to
> > > 0, as the current code does for a value larger than isize::MAX.
> > > 
> > > Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> > > ---
> > >  rust/kernel/serdev.rs | 27 ++++++++++++---------------
> > >  1 file changed, 12 insertions(+), 15 deletions(-)
> > > 
> > > diff --git a/rust/kernel/serdev.rs b/rust/kernel/serdev.rs
> > > index 17ca504b7f8d..094503f059ff 100644
> > > --- a/rust/kernel/serdev.rs
> > > +++ b/rust/kernel/serdev.rs
> > > @@ -20,7 +20,10 @@
> > >          aref::AlwaysRefCounted,
> > >          Mutex, //
> > >      },
> > > -    time::Jiffies,
> > > +    time::{
> > > +        Delta,
> > > +        Jiffy, //
> > > +    },
> > >      types::{
> > >          Opaque,
> > >          ScopeGuard, //
> > > @@ -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);
> > > +
> > >          // 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)
> > >          };
> > >          // CAST: negative return values are guaranteed to be between `-MAX_ERRNO` and `-1`,
> > >          // which always fit into a `i32`.
> > > @@ -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);
> > > +
> > >          // 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) };
> > >      }
> > >  }
> > >  
> > > 
> > > base-commit: f1850e443b0e4f2429ddf42a8d5033ea54ae8a90
> > 
> > Both functions have "Use a timeout of 0 to wait indefinitely." inside
> > the rustdoc.
> > 
> > Make sure `Delta::ZERO` is also usable for `Delta<Jiffy>` and replace
> > the 0 in the rustdoc with "[`Delta::<Jiffy>::ZERO`]". You might also
> > remove the "timeout of" part, but I don't mind if it stays.
> 
> I think the API should ideally use `Option<Delta<Jiffies>>` for this case, and
> use `None` to represent indefinite wait.
Yes, this would make it more explicit.
> 
> We might need to round 0 jiffies to 1 to avoid C API change. But ideally the C
> API should be using MAX_JIFFY_OFFSET to mean indefinite..
In both cases MAX_SCHEDULE_TIMEOUT is also accepted (even if not
documented).

At least both pass through such call:

if (timeout == 0)
        timeout = MAX_SCHEDULE_TIMEOUT;

Thanks
- Markus Probst

> 
> Best,
> Gary


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 870 bytes --]

  reply	other threads:[~2026-09-30 14:07 UTC|newest]

Thread overview: 9+ 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 12:59 ` Markus Probst
2026-09-30 13:52   ` Gary Guo
2026-09-30 14:07     ` Markus Probst [this message]
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=d2437243001e55ef6a845c88cfb19dc162a0c1b6.camel@posteo.de \
    --to=markus.probst@posteo.de \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=fujita.tomonori@gmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-serial@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=tomo@flapping.org \
    --cc=work@onurozkan.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