From: Alice Ryhl <aliceryhl@google.com>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: netdev@vger.kernel.org, rust-for-linux@vger.kernel.org,
andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu,
ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net,
bjorn3_gh@protonmail.com, benno.lossin@proton.me,
a.hindborg@samsung.com, anna-maria@linutronix.de,
frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de,
jstultz@google.com, sboyd@kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH net-next v3 7/8] rust: Add read_poll_timeout functions
Date: Wed, 16 Oct 2024 10:37:46 +0200 [thread overview]
Message-ID: <CAH5fLgg1G4++B+AoXrDc-QxiNL8T4zRV3ChbwN1LsG=urcMJmw@mail.gmail.com> (raw)
In-Reply-To: <20241016035214.2229-8-fujita.tomonori@gmail.com>
On Wed, Oct 16, 2024 at 5:54 AM FUJITA Tomonori
<fujita.tomonori@gmail.com> wrote:
>
> Add read_poll_timeout functions which poll periodically until a
> condition is met or a timeout is reached.
>
> C's read_poll_timeout (include/linux/iopoll.h) is a complicated macro
> and a simple wrapper for Rust doesn't work. So this implements the
> same functionality in Rust.
>
> The C version uses usleep_range() while the Rust version uses
> fsleep(), which uses the best sleep method so it works with spans that
> usleep_range() doesn't work nicely with.
>
> Unlike the C version, __might_sleep() is used instead of might_sleep()
> to show proper debug info; the file name and line
> number. might_resched() could be added to match what the C version
> does but this function works without it.
>
> For the proper debug info, readx_poll_timeout() is implemented as a
> macro.
>
> readx_poll_timeout() can only be used in a nonatomic context. This
> requirement is not checked by these abstractions, but it is
> intended that klint [1] or a similar tool will be used to check it
> in the future.
>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> Link: https://rust-for-linux.com/klint [1]
>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Duplicated SOB? This should just be:
Link: https://rust-for-linux.com/klint [1]
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> +/// Polls periodically until a condition is met or a timeout is reached.
> +///
> +/// Public but hidden since it should only be used from public macros.
> +#[doc(hidden)]
> +pub fn read_poll_timeout<Op, Cond, T: Copy>(
> + mut op: Op,
> + cond: Cond,
> + sleep_delta: Delta,
> + timeout_delta: Delta,
> + sleep_before_read: bool,
> +) -> Result<T>
> +where
> + Op: FnMut() -> Result<T>,
> + Cond: Fn(T) -> bool,
> +{
> + let timeout = Ktime::ktime_get() + timeout_delta;
> + let sleep = !sleep_delta.is_zero();
> +
> + if sleep_before_read && sleep {
> + fsleep(sleep_delta);
> + }
You always pass `false` for `sleep_before_read` so perhaps just remove
this and the argument entirely?
> + if cond(val) {
> + break val;
> + }
This breaks out to another cond(val) check below. Perhaps just `return
Ok(val)` here to avoid the double condition check?
> + if !timeout_delta.is_zero() && Ktime::ktime_get() > timeout {
> + break op()?;
> + }
Shouldn't you just return `Err(ETIMEDOUT)` here? I don't think you're
supposed to call `op` twice without a sleep in between.
> + if sleep {
> + fsleep(sleep_delta);
> + }
> + // SAFETY: FFI call.
> + unsafe { bindings::cpu_relax() }
Should cpu_relax() be in an else branch? Also, please add a safe
wrapper function around cpu_relax.
> +macro_rules! readx_poll_timeout {
> + ($op:expr, $cond:expr, $sleep_delta:expr, $timeout_delta:expr) => {{
> + #[cfg(CONFIG_DEBUG_ATOMIC_SLEEP)]
> + if !$sleep_delta.is_zero() {
> + // SAFETY: FFI call.
> + unsafe {
> + $crate::bindings::__might_sleep(
> + ::core::file!().as_ptr() as *const i8,
> + ::core::line!() as i32,
> + )
> + }
> + }
It could be nice to introduce a might_sleep macro that does this
internally? Then we can reuse this logic in other places.
Alice
next prev parent reply other threads:[~2024-10-16 8:38 UTC|newest]
Thread overview: 64+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-16 3:52 [PATCH net-next v3 0/8] rust: Add IO polling FUJITA Tomonori
2024-10-16 3:52 ` [PATCH net-next v3 1/8] rust: time: Add PartialEq/Eq/PartialOrd/Ord trait to Ktime FUJITA Tomonori
2024-10-16 8:22 ` Alice Ryhl
2024-10-16 3:52 ` [PATCH net-next v3 2/8] rust: time: Introduce Delta type FUJITA Tomonori
2024-10-16 8:23 ` Alice Ryhl
2024-10-17 11:15 ` FUJITA Tomonori
2024-10-17 10:17 ` Fiona Behrens
2024-10-17 11:24 ` FUJITA Tomonori
2024-10-18 13:49 ` Andrew Lunn
2024-10-18 14:31 ` Miguel Ojeda
2024-10-18 16:55 ` Andrew Lunn
2024-10-19 12:21 ` Miguel Ojeda
2024-10-19 12:41 ` Miguel Ojeda
2024-10-23 11:53 ` FUJITA Tomonori
2024-10-23 13:09 ` Miguel Ojeda
2024-10-19 18:41 ` Andrew Lunn
2024-10-20 13:05 ` Miguel Ojeda
2024-10-23 12:19 ` FUJITA Tomonori
2024-10-16 3:52 ` [PATCH net-next v3 3/8] rust: time: Change output of Ktime's sub operation to Delta FUJITA Tomonori
2024-10-16 8:25 ` Alice Ryhl
2024-10-16 19:47 ` Boqun Feng
2024-10-17 7:10 ` FUJITA Tomonori
2024-10-17 8:22 ` Alice Ryhl
2024-10-17 16:45 ` Miguel Ojeda
2024-10-23 1:58 ` FUJITA Tomonori
2024-10-16 20:03 ` Boqun Feng
2024-10-17 9:17 ` FUJITA Tomonori
2024-10-16 3:52 ` [PATCH net-next v3 4/8] rust: time: Implement addition of Ktime and Delta FUJITA Tomonori
2024-10-16 8:25 ` Alice Ryhl
2024-10-16 19:54 ` Boqun Feng
2024-10-17 9:31 ` FUJITA Tomonori
2024-10-17 9:33 ` Alice Ryhl
2024-10-17 16:33 ` Miguel Ojeda
2024-10-17 17:03 ` Boqun Feng
2024-10-17 18:10 ` Miguel Ojeda
2024-10-17 19:02 ` Boqun Feng
2024-10-17 18:58 ` Miguel Ojeda
2024-10-25 20:47 ` Boqun Feng
2024-10-23 6:51 ` FUJITA Tomonori
2024-10-23 10:59 ` Miguel Ojeda
2024-10-23 11:24 ` FUJITA Tomonori
2024-10-16 3:52 ` [PATCH net-next v3 5/8] rust: time: Add wrapper for fsleep function FUJITA Tomonori
2024-10-16 8:29 ` Alice Ryhl
2024-10-16 9:42 ` Miguel Ojeda
2024-10-24 0:22 ` FUJITA Tomonori
2024-10-24 17:26 ` Miguel Ojeda
2024-10-23 12:33 ` FUJITA Tomonori
2024-10-18 14:05 ` Andrew Lunn
2024-10-16 3:52 ` [PATCH net-next v3 6/8] MAINTAINERS: rust: Add TIMEKEEPING and TIMER abstractions FUJITA Tomonori
2024-10-16 3:52 ` [PATCH net-next v3 7/8] rust: Add read_poll_timeout functions FUJITA Tomonori
2024-10-16 8:37 ` Alice Ryhl [this message]
2024-10-18 14:16 ` FUJITA Tomonori
2024-10-16 8:45 ` Alice Ryhl
2024-10-16 8:52 ` Alice Ryhl
2024-10-16 11:05 ` Miguel Ojeda
2024-10-18 8:10 ` FUJITA Tomonori
2024-10-18 8:30 ` Alice Ryhl
2024-10-18 14:15 ` Andrew Lunn
2024-10-18 14:38 ` Miguel Ojeda
2024-10-18 14:21 ` Andrew Lunn
2024-10-16 3:52 ` [PATCH net-next v3 8/8] net: phy: qt2025: Wait until PHY becomes ready FUJITA Tomonori
2024-10-16 12:00 ` Alice Ryhl
2024-10-18 14:26 ` [PATCH net-next v3 0/8] rust: Add IO polling Andrew Lunn
2024-10-23 4:01 ` 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='CAH5fLgg1G4++B+AoXrDc-QxiNL8T4zRV3ChbwN1LsG=urcMJmw@mail.gmail.com' \
--to=aliceryhl@google.com \
--cc=a.hindborg@samsung.com \
--cc=alex.gaynor@gmail.com \
--cc=andrew@lunn.ch \
--cc=anna-maria@linutronix.de \
--cc=arnd@arndb.de \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=frederic@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=hkallweit1@gmail.com \
--cc=jstultz@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=tglx@linutronix.de \
--cc=tmgross@umich.edu \
/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;
as well as URLs for NNTP newsgroup(s).