Re: [PATCH v2 1/2] rust: add udelay() function
From: Andreas Hindborg
Date: Fri Oct 24 2025 - 04:26:09 EST
"FUJITA Tomonori" <fujita.tomonori@xxxxxxxxx> writes:
> On Wed, 22 Oct 2025 14:11:53 +0000
> Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
>
>> On Wed, Oct 22, 2025 at 07:32:30PM +0900, FUJITA Tomonori wrote:
>>> 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
>>> );
>>
>> This is a bad idea. Using build_assert! assert for range checks works
>> poorly, as we found for register index bounds checks.
>
> Oh, I didn’t know about that. Do you have a pointer or some details I
> could look at?
>
>
>> If you really want to check it at compile-time, you'll need a wrapper
>> type around Delta that can only be constructed with delays in the right
>> range.
>
> You meant that introducing a new type like UdelayDelta, right?
>
> read_poll_timeout() and read_poll_timeout_atomic() use different Delta
> types... I'm not sure it's a good idea.
I would assume we keep this type private and only construct it in
`udelay`. @Alice, could you give a pointer on this approach?
Best regards,
Andreas Hindborg