From: "Danilo Krummrich" <dakr@kernel.org>
To: "FUJITA Tomonori" <fujita.tomonori@gmail.com>
Cc: <a.hindborg@kernel.org>, <alex.gaynor@gmail.com>,
<ojeda@kernel.org>, <aliceryhl@google.com>,
<anna-maria@linutronix.de>, <bjorn3_gh@protonmail.com>,
<boqun.feng@gmail.com>, <frederic@kernel.org>, <gary@garyguo.net>,
<jstultz@google.com>, <linux-kernel@vger.kernel.org>,
<lossin@kernel.org>, <lyude@redhat.com>,
<rust-for-linux@vger.kernel.org>, <sboyd@kernel.org>,
<tglx@linutronix.de>, <tmgross@umich.edu>, <acourbot@nvidia.com>,
<daniel.almeida@collabora.com>
Subject: Re: [PATCH v1 2/2] rust: Add read_poll_timeout_atomic function
Date: Wed, 27 Aug 2025 11:00:36 +0200 [thread overview]
Message-ID: <DCD35NEEPLYB.2PBCLR8FWFGKD@kernel.org> (raw)
In-Reply-To: <20250827.091427.1081669324737480994.fujita.tomonori@gmail.com>
On Wed Aug 27, 2025 at 2:14 AM CEST, FUJITA Tomonori wrote:
> On Tue, 26 Aug 2025 16:12:44 +0200
> "Danilo Krummrich" <dakr@kernel.org> wrote:
>
>> On Thu Aug 21, 2025 at 5:57 AM CEST, FUJITA Tomonori wrote:
>>> +pub fn read_poll_timeout_atomic<Op, Cond, T>(
>>> + mut op: Op,
>>> + mut cond: Cond,
>>> + delay_delta: Delta,
>>> + timeout_delta: Delta,
>>> +) -> Result<T>
>>> +where
>>> + Op: FnMut() -> Result<T>,
>>> + Cond: FnMut(&T) -> bool,
>>> +{
>>> + let mut left_ns = timeout_delta.as_nanos();
>>> + let delay_ns = delay_delta.as_nanos();
>>> +
>>> + loop {
>>> + let val = op()?;
>>> + if cond(&val) {
>>> + // Unlike the C version, we immediately return.
>>> + // We know the condition is met so we don't need to check again.
>>> + return Ok(val);
>>> + }
>>> +
>>> + if left_ns < 0 {
>>> + // Unlike the C version, we immediately return.
>>> + // We have just called `op()` so we don't need to call it again.
>>> + return Err(ETIMEDOUT);
>>> + }
>>> +
>>> + if !delay_delta.is_zero() {
>>> + udelay(delay_delta);
>>> + left_ns -= delay_ns;
>>> + }
>>> +
>>> + cpu_relax();
>>> + left_ns -= 1;
>>
>> How do we know that each iteration costs 1ns? To make it even more obvious, we
>> don't control the implementation of cond(). Shouldn't we use ktime for this?
>
> The C version used to use ktime but it has been changed not to:
>
> 7349a69cf312 ("iopoll: Do not use timekeeping in read_poll_timeout_atomic()")
Ick! That's pretty unfortunate -- no ktime then.
But regardless of that, the current implementation (this and the C one) lack
clarity.
The nanosecond decrement is rather negligible, the real timeout reduction comes
from the delay_delta. Given that, and the fact that we can't use ktime, this
function shouldn't take a raw timeout value, since we can't guarantee the
timeout anyways.
Instead, I think it makes much more sense to provide a retry count as function
argument, such that the user can specify "I want a dealy of 100us, try it 100
times".
This way it is transparent to the caller that the timeout may be significantly
more than 10ms depending on the user's implementation.
As for doing this in C vs Rust: I don't think things have to align in every
implementation detail. If we can improve things on the Rust side from the
get-go, we should not stop ourselves from doing so, just because a similar C
implementation is hard to refactor, due to having a lot of users already.
next prev parent reply other threads:[~2025-08-27 9:00 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-21 3:57 [PATCH v1 0/2] Add read_poll_timeout_atomic support FUJITA Tomonori
2025-08-21 3:57 ` [PATCH v1 1/2] rust: add udelay() function FUJITA Tomonori
2025-08-26 9:09 ` Andreas Hindborg
2025-08-26 11:59 ` FUJITA Tomonori
2025-08-26 18:03 ` Miguel Ojeda
2025-08-27 7:12 ` Andreas Hindborg
2025-08-26 12:44 ` Daniel Almeida
2025-08-27 2:43 ` FUJITA Tomonori
2025-08-21 3:57 ` [PATCH v1 2/2] rust: Add read_poll_timeout_atomic function FUJITA Tomonori
2025-08-26 14:02 ` Daniel Almeida
2025-08-27 0:35 ` FUJITA Tomonori
2025-08-27 4:32 ` FUJITA Tomonori
2025-08-26 14:12 ` Danilo Krummrich
2025-08-26 16:59 ` Daniel Almeida
2025-08-26 17:15 ` Danilo Krummrich
2025-08-27 0:14 ` FUJITA Tomonori
2025-08-27 9:00 ` Danilo Krummrich [this message]
2025-08-27 10:29 ` Danilo Krummrich
2025-08-27 12:14 ` Daniel Almeida
2025-08-27 12:19 ` Danilo Krummrich
2025-08-27 12:22 ` Daniel Almeida
2025-08-27 12:36 ` Danilo Krummrich
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=DCD35NEEPLYB.2PBCLR8FWFGKD@kernel.org \
--to=dakr@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=anna-maria@linutronix.de \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=daniel.almeida@collabora.com \
--cc=frederic@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=jstultz@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--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).