Re: [PATCH v8 6/7] rust: Add read_poll_timeout functions

From: Alice Ryhl
Date: Thu Jan 16 2025 - 06:43:19 EST


On Thu, Jan 16, 2025 at 12:32 PM FUJITA Tomonori
<fujita.tomonori@xxxxxxxxx> wrote:
>
> On Thu, 16 Jan 2025 10:45:00 +0100
> Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
>
> >> +void __might_sleep(const char *file, int line)
> >> +{
> >> + long len = strlen(file);
> >> +
> >> + __might_sleep_precision(file, len, line);
> >> }
> >> EXPORT_SYMBOL(__might_sleep);
> >
> > I think these strlen() calls could be pretty expensive. You run them
> > every time might_sleep() runs even if the check does not fail.
>
> Ah, yes.
>
> > How about changing __might_resched_precision() to accept a length of
> > -1 for nul-terminated strings, and having it compute the length with
> > strlen only *if* we know that we actually need the length?
> >
> > if (len < 0) len = strlen(file);
> > pr_err("BUG: sleeping function called from invalid context at %.*s:%d\n",
> > len, file, line);
>
> Works for me.
>
> > Another option might be to compile the lengths at compile-time by
> > having the macros use sizeof on __FILE__, but that sounds more tricky
> > to get right.
>
> Yeah.
>
> By the way, from what I saw in the discussion about Location::file(),
> I got the impression that the feature for a null-terminated string
> seems likely to be supported in the near future. Am I correct?

There's a good chance, but it's also not guaranteed.

> If so, rather than adding a Rust-specific helper function to the C
> side, it would be better to solve the problem on the Rust side like
> the previous versions with c_str()! and file()! for now?

I would be okay with a scenario where older compilers just reference
the read_poll_timeout() function in the error message, and only newer
compilers reference the location of the caller. Of course, right now,
only older compilers exist. But if we don't get nul-terminated
location strings, then I do think we should make the change you're
currently making.

Alice