From: Boqun Feng <boqun.feng@gmail.com>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: dirk.behme@de.bosch.com, andrew@lunn.ch, aliceryhl@google.com,
netdev@vger.kernel.org, rust-for-linux@vger.kernel.org,
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
Subject: Re: iopoll abstraction
Date: Thu, 3 Oct 2024 04:52:48 -0700 [thread overview]
Message-ID: <Zv6FkGIMoh6PTdKY@boqun-archlinux> (raw)
In-Reply-To: <20241002.095636.680321517586867502.fujita.tomonori@gmail.com>
On Wed, Oct 02, 2024 at 09:56:36AM +0000, FUJITA Tomonori wrote:
> On Wed, 2 Oct 2024 06:39:48 +0200
> Dirk Behme <dirk.behme@de.bosch.com> wrote:
>
> >> I generally point developers at iopoll.h, because developers nearly
> >> always get this sort of polling for something to happen wrong.
> >> The kernel sleep functions guarantee the minimum sleep time. They say
> >> nothing about the maximum sleep time. You can ask it to sleep for 1ms,
> >> and in reality, due to something stealing the CPU and not being RT
> >> friendly, it actually sleeps for 10ms. This extra long sleep time
> >> blows straight past your timeout, if you have a time based timeout.
> >> What most developers do is after the sleep() returns they check to see
> >> if the timeout has been reached and then exit with -ETIMEDOUT. They
> >> don't check the state of the hardware, which given its had a long time
> >> to do its thing, probably is now in a good state. But the function
> >> returns -ETIMEDOUT.
> >> There should always be a check of the hardware state after the sleep,
> >> in order to determine ETIMEDOUT vs 0.
> >> As i said, most C developers get this wrong. So i don't really see why
> >> Rust developers also will not get this wrong. So i like to discourage
> >> this sort of code, and have Rust implementations of iopoll.h.
> >
> >
> > Do we talk about some simple Rust wrappers for the macros in iopoll.h?
> > E.g. something like [1]?
> >
> > Or are we talking about some more complex (safety) dependencies which
> > need some more complex abstraction handling?
>
> (snip)
>
> > int rust_helper_readb_poll_timeout(const volatile void * addr,
> > u64 val, u64 cond, u64 delay_us,
> > u64 timeout_us)
> > {
> > return readb_poll_timeout(addr, val, cond, delay_us, timeout_us);
> > }
>
> I'm not sure a simple wrapper for iopoll.h works. We need to pass a
> function. I'm testing a macro like the following (not added ktime
> timeout yet):
You could use closure as a parameter to avoid macro interface, something
like:
fn read_poll_timeout<Op, Cond, T>(
op: Op,
cond: Cond,
sleep: Delta,
timeout: Delta,
) -> Result<T> where
Op: Fn() -> T,
cond: Fn() -> bool {
let __timeout = kernel::Ktime::ktime_get() + timeout;
let val = loop {
let val = op();
if cond() {
break Some(val);
}
kernel::delay::sleep(sleep);
if __timeout.after(kernel::Ktime::ktime_get()) {
break None;
}
};
if cond() {
val
} else {
Err(kernel::error::code::ETIMEDOUT)
}
}
note that you don't need the args part, because `op` is a closure that
can capature value, so for example, if in C code you need to call foo(a,
b), with closure, you can do:
<a and b are defined in the caller>
read_poll_timeout(|| { foo(a, b) }, ...);
with above API.
Regards,
Boqun
>
> macro_rules! read_poll_timeout {
> ($op:expr, $val:expr, $cond:expr, $sleep:expr, $timeout:expr, $($args:expr),*) => {{
> let _ = $val;
> loop {
> $val = $op($($args),*);
> if $cond {
> break;
> }
> kernel::delay::sleep($sleep);
> }
> if $cond {
> Ok(())
> } else {
> Err(kernel::error::code::ETIMEDOUT)
> }
> }};
> }
>
next prev parent reply other threads:[~2024-10-03 11:53 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-01 11:25 [PATCH net-next v1 0/2] add delay abstraction (sleep functions) FUJITA Tomonori
2024-10-01 11:25 ` [PATCH net-next v1 1/2] rust: add delay abstraction FUJITA Tomonori
2024-10-01 11:33 ` Alice Ryhl
2024-10-01 12:31 ` Andrew Lunn
2024-10-01 15:08 ` Miguel Ojeda
2024-10-02 11:34 ` FUJITA Tomonori
2024-10-02 12:18 ` Andrew Lunn
2024-10-02 12:35 ` Miguel Ojeda
2024-10-02 12:51 ` Andrew Lunn
2024-10-02 13:21 ` Miguel Ojeda
2024-10-02 20:04 ` Thomas Gleixner
2024-10-02 12:37 ` Alice Ryhl
2024-10-02 13:58 ` FUJITA Tomonori
2024-10-02 14:27 ` Alice Ryhl
2024-10-02 14:40 ` FUJITA Tomonori
2024-10-02 14:52 ` Miguel Ojeda
2024-10-02 19:40 ` Thomas Gleixner
2024-10-03 1:24 ` FUJITA Tomonori
2024-10-03 10:50 ` Miguel Ojeda
2024-10-03 12:33 ` Andrew Lunn
2024-10-04 12:08 ` FUJITA Tomonori
2024-10-04 14:08 ` Andrew Lunn
2024-10-01 11:25 ` [PATCH net-next v1 2/2] net: phy: qt2025: wait until PHY becomes ready FUJITA Tomonori
2024-10-01 11:36 ` Alice Ryhl
2024-10-01 12:48 ` Andrew Lunn
2024-10-02 4:39 ` iopoll abstraction (was: Re: [PATCH net-next v1 2/2] net: phy: qt2025: wait until PHY becomes ready) Dirk Behme
2024-10-02 9:56 ` iopoll abstraction FUJITA Tomonori
2024-10-03 11:52 ` Boqun Feng [this message]
2024-10-03 13:45 ` FUJITA Tomonori
2024-10-03 14:25 ` Boqun Feng
2024-10-03 16:00 ` Andrew Lunn
2024-10-04 11:54 ` FUJITA Tomonori
2024-10-03 16:09 ` Andrew Lunn
2024-10-04 11:48 ` FUJITA Tomonori
2024-10-04 13:37 ` Andrew Lunn
2024-10-02 10:13 ` [PATCH net-next v1 2/2] net: phy: qt2025: wait until PHY becomes ready FUJITA Tomonori
2024-10-02 12:31 ` Andrew Lunn
2024-10-03 5:07 ` FUJITA Tomonori
2024-10-02 11:17 ` FUJITA Tomonori
2024-10-01 11:39 ` [PATCH net-next v1 0/2] add delay abstraction (sleep functions) Alice Ryhl
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=Zv6FkGIMoh6PTdKY@boqun-archlinux \
--to=boqun.feng@gmail.com \
--cc=a.hindborg@samsung.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=andrew@lunn.ch \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=dirk.behme@de.bosch.com \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=hkallweit1@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--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).