[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <aB3I62o8hWSULGBm@google.com>
Date: Fri, 9 May 2025 09:20:43 +0000
From: Alice Ryhl <aliceryhl@...gle.com>
To: Ingo Molnar <mingo@...nel.org>
Cc: Boqun Feng <boqun.feng@...il.com>, Peter Zijlstra <peterz@...radead.org>,
Ingo Molnar <mingo@...hat.com>, Juri Lelli <juri.lelli@...hat.com>,
Vincent Guittot <vincent.guittot@...aro.org>, Dietmar Eggemann <dietmar.eggemann@....com>,
Steven Rostedt <rostedt@...dmis.org>, Ben Segall <bsegall@...gle.com>, Mel Gorman <mgorman@...e.de>,
Valentin Schneider <vschneid@...hat.com>, Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>, Gary Guo <gary@...yguo.net>,
"Björn Roy Baron" <bjorn3_gh@...tonmail.com>, Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...nel.org>, Trevor Gross <tmgross@...ch.edu>,
Danilo Krummrich <dakr@...nel.org>, Nathan Chancellor <nathan@...nel.org>,
Nick Desaulniers <nick.desaulniers+lkml@...il.com>, Bill Wendling <morbo@...gle.com>,
Justin Stitt <justinstitt@...gle.com>, FUJITA Tomonori <fujita.tomonori@...il.com>,
Tamir Duberstein <tamird@...il.com>, Kunwu Chan <kunwu.chan@...mail.com>,
Mitchell Levy <levymitchell0@...il.com>, Martin Rodriguez Reboredo <yakoyoku@...il.com>,
Borys Tyran <borys.tyran@...tonmail.com>, Christian Brauner <brauner@...nel.org>,
Panagiotis Foliadis <pfoliadis@...teo.net>, linux-kernel@...r.kernel.org,
rust-for-linux@...r.kernel.org, llvm@...ts.linux.dev,
Daniel Almeida <daniel.almeida@...labora.com>,
Linus Torvalds <torvalds@...ux-foundation.org>
Subject: Re: [PATCH 4/5] sched/core: Add __might_sleep_precision()
On Fri, May 09, 2025 at 08:00:32AM +0200, Ingo Molnar wrote:
>
> * Boqun Feng <boqun.feng@...il.com> wrote:
>
> > From: FUJITA Tomonori <fujita.tomonori@...il.com>
> >
> > Add __might_sleep_precision(), Rust friendly version of
> > __might_sleep(), which takes a pointer to a string with the length
> > instead of a null-terminated string.
> >
> > Rust's core::panic::Location::file(), which gives the file name of a
> > caller, doesn't provide a null-terminated
> > string. __might_sleep_precision() uses a precision specifier in the
> > printk format, which specifies the length of a string; a string
> > doesn't need to be a null-terminated.
> >
> > Modify __might_sleep() to call __might_sleep_precision() but the
> > impact should be negligible. When printing the error (sleeping
> > function called from invalid context), the precision string format is
> > used instead of the simple string format; the precision specifies the
> > the maximum length of the displayed string.
> >
> > Note that Location::file() providing a null-terminated string for
> > better C interoperability is under discussion [1].
> >
> > [1]: https://github.com/rust-lang/libs-team/issues/466
> >
> > Tested-by: Daniel Almeida <daniel.almeida@...labora.com>
> > Reviewed-by: Alice Ryhl <aliceryhl@...gle.com>
> > Co-developed-by: Boqun Feng <boqun.feng@...il.com>
> > Signed-off-by: Boqun Feng <boqun.feng@...il.com>
> > Signed-off-by: FUJITA Tomonori <fujita.tomonori@...il.com>
> > Signed-off-by: Boqun Feng <boqun.feng@...il.com>
> > Link: https://lore.kernel.org/r/20250410225623.152616-2-fujita.tomonori@gmail.com
> > ---
> > include/linux/kernel.h | 2 ++
> > kernel/sched/core.c | 62 ++++++++++++++++++++++++++++--------------
> > 2 files changed, 43 insertions(+), 21 deletions(-)
> >
> > diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> > index be2e8c0a187e..086ee1dc447e 100644
> > --- a/include/linux/kernel.h
> > +++ b/include/linux/kernel.h
> > @@ -87,6 +87,7 @@ extern int dynamic_might_resched(void);
> > #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
> > extern void __might_resched(const char *file, int line, unsigned int offsets);
> > extern void __might_sleep(const char *file, int line);
> > +extern void __might_sleep_precision(const char *file, int len, int line);
>
> Ugh.
>
> Firstly, '_precision' is really ambiguous in this context and suggests
> 'precise sleep' or something like that, which this is not about at all.
> So the naming here is all sorts of bad already.
>
> But more importantly, this is really a Rust problem. Does Rust really
> have no NUL-terminated strings? It should hide them in shame and
> construct proper, robust strings, instead of spreading this disease to
> the rest of the kernel, IMHO ...
Rust does have NUL-terminated strings, but they aren't the default. In
most circumstances, obtaining a NUL-terminated string is possible, but
we can't do it in this particular case.
Specifically, it's because we're relying on the #[track_caller] language
feature. When this annotation is placed on a function, it implicitly
adds an extra hidden argument to the function signature containing a
pointer to a location struct that holds the file, line, and column of
the caller. This works recursively until it hits a function without the
annotation, so code like this:
#[track_caller]
fn schedule() {
might_sleep();
// Call into C implementation of schedule.
unsafe { bindings::schedule() };
}
would report a line number in the *caller* of schedule() rather than
just reporting the line number inside the schedule() function.
The problem is that the location struct is generated by the compiler,
and we don't have any control over how its generated. Unfortunately, the
compiler does not place a NUL terminator in the file name.
> Rust is supposed to be about increased security, right? How does extra,
> nonsensical complexity for simple concepts such as strings achieve
> that? If the Rust runtime wants to hook into debug facilities of the
> Linux kernel then I have bad news: almost all strings used by kernel
> debugging facilities are NUL-terminated.
>
> So I really don't like this patch. Is there no other way to do this?
I filed a proposal to add a NUL-terminator to the file names used by
rustc-generated location structs:
https://github.com/rust-lang/libs-team/issues/466
But I have not had success with landing it, unfortunately.
Alice
Powered by blists - more mailing lists