rust: serdev: use Delta<Jiffy> for timeouts
Subsystems (3)
PATCH v3 1/1 rust: serdev: use Delta<Jiffy> for timeouts
From: FUJITA Tomonori <[email protected]>
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.
The C side treats a timeout of 0 as "wait indefinitely". The Rust API does not keep that meaning. A timeout of 0 or negative is rounded up to one jiffy. A caller that wants to wait indefinitely passes Delta::from_jiffies(isize::MAX). That value is MAX_SCHEDULE_TIMEOUT. The C side replaces 0 with MAX_SCHEDULE_TIMEOUT before it waits, so this gives the same wait as a timeout of 0 did before.
---
v3:
- Fix the serdev::Driver doctest example, which still passed 0
(sashiko, Markus)
- Add Markus' Reviewed-by
v2: https://lore.kernel.org/rust-for-linux/[email protected]/
- Round a timeout of 0 or negative up to one jiffy instead of passing 0, which the C side treats as "wait indefinitely" (Gary)
- Document Delta::from_jiffies(isize::MAX) for an indefinite wait
v1: https://lore.kernel.org/rust-for-linux/[email protected]/
--- rust/kernel/serdev.rs | 48 ++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 19 deletions(-)
rust/kernel/serdev.rs +29 -19
@@ -20,7 +20,10 @@ aref::AlwaysRefCounted, Mutex, // }, - time::Jiffies, + time::{ + Delta, + Jiffy, // + }, types::{Opaque ,ScopeGuard ,//
@@ -316,7 +319,8 @@ macro_rules! module_serdev_device_driver {/// Core, // /// }, /// of, -/// serdev, // +/// serdev, +/// time::Delta, // /// }; /// /// struct MyDriver;
@@ -349,7 +353,7 @@ macro_rules! module_serdev_device_driver {/// _id_info: Option<&'bound Self::IdInfo>, /// ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound { /// sdev.set_baudrate(115200); -/// sdev.write_all(b"Hello\n", 0)?; +/// sdev.write_all(b"Hello\n", Delta::from_jiffies(isize::MAX))?; /// Ok(MyDriver) /// } /// }
@@ -433,6 +437,16 @@ fn as_raw(&self) -> *mut bindings::serdev_device { } } +/// Convert a timeout to the `long` the C side takes. +/// +/// The C side treats a timeout of 0 as "wait indefinitely", so a timeout of 0 or negative is +/// rounded up to one jiffy. `isize::MAX` is `MAX_SCHEDULE_TIMEOUT`, so it is passed through +/// unchanged and the C side waits indefinitely. +#[inline] +fn timeout_jiffies (timeout:Delta <Jiffy >) ->isize { + isize::max (timeout.as_jiffies (),1 ) +} +impl Device <device::Bound > {/// Set the baudrate in bits per second. ///
@@ -475,28 +489,26 @@ pub fn set_parity(&self, parity: Parity) -> Result {/// [`Device::wait_until_sent`] to make sure the controller write buffer has actually been /// emptied. /// -/// Use a timeout of 0 to wait indefinitely. +/// A `timeout` of 0 or negative is rounded up to one jiffy. Pass +/// `Delta::from_jiffies(isize::MAX)` to wait indefinitely. /// /// Returns the number of bytes written (less than `data.len()` if interrupted). /// [`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 =timeout_jiffies (timeout); +// 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`.
@@ -545,16 +557,14 @@ pub fn write_flush(&self) {/// After this function, the write buffer of the controller should be empty or the timeout /// elapsed. /// -/// Use a timeout of 0 to wait indefinitely. +/// A `timeout` of 0 or negative is rounded up to one jiffy. Pass +/// `Delta::from_jiffies(isize::MAX)` to wait indefinitely. #[inline] -pub fn wait_until_sent (&self, timeout:Jiffies ) { +pub fn wait_until_sent (&self, timeout:Delta <Jiffy >) { +let timeout =timeout_jiffies (timeout); +// 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: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.43.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 14 lines
From: FUJITA Tomonori <[email protected]>
write_all()andwait_until_sent()take the timeout as a raw Jiffies, which is ac_ulongalias. The type does not show the unit.Take the timeout as Delta<Jiffy> instead.
The C side treats a timeout of 0 as "wait indefinitely". The Rust API does not keep that meaning. A timeout of 0 or negative is rounded up to one jiffy. A caller that wants to wait indefinitely passes
Delta::from_jiffies(isize::MAX). That value isMAX_SCHEDULE_TIMEOUT. The C side replaces 0 withMAX_SCHEDULE_TIMEOUTbefore it waits, so this gives the same wait as a timeout of 0 did before.
Quoted 2 lines
Reviewed-by Markus Probst <[email protected]>Signed-off-by FUJITA Tomonori <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 14 lines · On Tue, 3 Nov 2026 10:11:43 +0900 FUJITA Tomonori <[email protected]> wrote:
From: FUJITA Tomonori <[email protected]>
write_all()andwait_until_sent()take the timeout as a raw Jiffies, which is ac_ulongalias. The type does not show the unit.Take the timeout as Delta<Jiffy> instead.
The C side treats a timeout of 0 as "wait indefinitely". The Rust API does not keep that meaning. A timeout of 0 or negative is rounded up to one jiffy. A caller that wants to wait indefinitely passes
Delta::from_jiffies(isize::MAX). That value isMAX_SCHEDULE_TIMEOUT. The C side replaces 0 withMAX_SCHEDULE_TIMEOUTbefore it waits, so this gives the same wait as a timeout of 0 did before.
Quoted 2 lines
Reviewed-by Markus Probst <[email protected]>Signed-off-by FUJITA Tomonori <[email protected]>
Gentle ping.
Danilo, could you take this through driver-core? It applies cleanly to driver-core-next.
Danilo Krummrich
reviewer(Cc: Greg, Jiri)
Quoted 17 lines · FUJITA Tomonori
Quoted 14 lines · On Tue, 3 Nov 2026 10:11:43 +0900 FUJITA Tomonori <[email protected]> wrote:
From: FUJITA Tomonori <[email protected]>
write_all()andwait_until_sent()take the timeout as a raw Jiffies, which is ac_ulongalias. The type does not show the unit.Take the timeout as Delta<Jiffy> instead.
The C side treats a timeout of 0 as "wait indefinitely". The Rust API does not keep that meaning. A timeout of 0 or negative is rounded up to one jiffy. A caller that wants to wait indefinitely passes
Delta::from_jiffies(isize::MAX). That value isMAX_SCHEDULE_TIMEOUT. The C side replaces 0 withMAX_SCHEDULE_TIMEOUTbefore it waits, so this gives the same wait as a timeout of 0 did before.
Quoted 7 lines
Quoted 2 lines
Reviewed-by Markus Probst <[email protected]>Signed-off-by FUJITA Tomonori <[email protected]>Gentle ping.
Danilo, could you take this through driver-core? It applies cleanly to driver-core-next.
I think serdev stuff usually goes through TTY.
Greg, can you please pick this up?
Thanks,
Danilo
Greg Kroah-Hartman
maintainerQuoted 23 lines · Danilo Krummrich
(Cc: Greg, Jiri)
Quoted 20 lines · FUJITA Tomonori
Quoted 16 lines · On Tue, 3 Nov 2026 10:11:43 +0900 FUJITA Tomonori <[email protected]> wrote:
From: FUJITA Tomonori <[email protected]>
write_all()andwait_until_sent()take the timeout as a raw Jiffies, which is ac_ulongalias. The type does not show the unit.Take the timeout as Delta<Jiffy> instead.
The C side treats a timeout of 0 as "wait indefinitely". The Rust API does not keep that meaning. A timeout of 0 or negative is rounded up to one jiffy. A caller that wants to wait indefinitely passes
Delta::from_jiffies(isize::MAX). That value isMAX_SCHEDULE_TIMEOUT. The C side replaces 0 withMAX_SCHEDULE_TIMEOUTbefore it waits, so this gives the same wait as a timeout of 0 did before.Reviewed-by Markus Probst <[email protected]>Signed-off-by FUJITA Tomonori <[email protected]>
Quoted 8 lines
Quoted 4 lines
Gentle ping.
Danilo, could you take this through driver-core? It applies cleanly to driver-core-next.
I think serdev stuff usually goes through TTY.
Greg, can you please pick this up?
Will do when I catch up on serial patches, thanks.
greg k-h