Re: [PATCH v2 1/2] rust: add udelay() function
From: FUJITA Tomonori
Date: Wed Oct 22 2025 - 06:32:37 EST
On Tue, 21 Oct 2025 17:20:41 +0200
"Danilo Krummrich" <dakr@xxxxxxxxxx> wrote:
> On Tue Oct 21, 2025 at 5:13 PM CEST, Miguel Ojeda wrote:
>> i.e. if they aren't sure what the value is, then I would prefer they
>> clamp it explicitly on the callee side (or we provide an explicitly
>> clamped version if it is a common case, but it seems to me runtime
>> values are already the minority).
>
> Absolutely! Especially given the context udelay() is introduced
> (read_poll_timeout_atomic()), the compile time checked version is what we really
> want.
>
> Maybe we should even defer a runtime checked / clamped version until it is
> actually needed.
Then perhaps something like this?
#[inline(always)]
pub fn udelay(delta: Delta) {
build_assert!(
delta.as_nanos() >= 0 && delta.as_nanos() <= i64::from(bindings::MAX_UDELAY_MS) * 1_000_000
);
// SAFETY: It is always safe to call `udelay()` with any duration.
// Note that the kernel is compiled with `-fno-strict-overflow`
// so any out-of-range value could lead to unexpected behavior
// but won't lead to undefined behavior.
unsafe {
// Convert the duration to microseconds and round up to preserve
// the guarantee; `udelay()` inserts a delay for at least
// the provided duration, but that it may delay for longer
// under some circumstances.
bindings::udelay(delta.as_micros_ceil() as c_ulong)
}
}