rust: Task & schedule related changes for v6.17
Hi Ingo & Peter,
This is the updated version from my pull request last cycle:
v1: https://lore.kernel.org/rust-for-linux/[email protected]/
Please take a look, thanks!
Changes since v1:
- Location::file_with_nul() is used to avoid the C changes of __might_sleep()
Regards,
Boqun
The following changes since commit 5bc34be478d09c4d16009e665e020ad0fcd0deea:
sched/core: Reorganize cgroup bandwidth control interface file writes (2025-06-18 13:59:57 +0200)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/boqun/linux.git tags/rust-sched.2025.06.24
for you to fetch changes up to 7e611710acf966df1e14bcf4e067385e38e549a1:
rust: task: Add Rust version of might_sleep() (2025-06-24 15:53:50 -0700)
----------------------------------------------------------------
Rust task & schedule changes for v6.17:
- Make Task, CondVar and PollCondVar methods inline to avoid unnecessary function calls
- Add might_sleep() support for Rust code: Rust's "#[track_caller]" mechanism is used so that Rust's might_sleep() doesn't need to be defined as a macro
----------------------------------------------------------------
Boqun Feng (1):
rust: Introduce file_from_location()
FUJITA Tomonori (1):
rust: task: Add Rust version of might_sleep()
Kunwu Chan (2):
rust: sync: Mark CondVar::notify_*() inline
rust: sync: Mark PollCondVar::drop() inline
Panagiotis Foliadis (1):
rust: task: Mark Task methods inline
init/Kconfig | 3 +++ rust/helpers/task.c | 6 +++++ rust/kernel/lib.rs | 48 +++++++++++++++++++++++++++++++++++++ rust/kernel/sync/condvar.rs | 3 +++ rust/kernel/sync/poll.rs | 1 + rust/kernel/task.rs | 33 +++++++++++++++++++++++++ 6 files changed, 94 insertions(+)
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v2 1/5 rust: sync: Mark CondVar::notify_*() inline
From: Kunwu Chan <[email protected]>
When build the kernel using the llvm-18.1.3-rust-1.85.0-x86_64 with ARCH=arm64, the following symbols are generated:
$nm vmlinux | grep ' _R'.*CondVar | rustfilt ... T <kernel::sync::condvar::CondVar>::notify_all ... T <kernel::sync::condvar::CondVar>::notify_one ... T <kernel::sync::condvar::CondVar>::notify_sync
...
These notify_*() symbols are trivial wrappers around the C functions __wake_up() and __wake_up_sync(). It doesn't make sense to go through a trivial wrapper for these functions, so mark them inline.
[boqun: Reword the commit title for consistency and reformat the commit log.]
--- rust/kernel/sync/condvar.rs | 3 +++ 1 file changed, 3 insertions(+)
rust/kernel/sync/condvar.rs +3 -0
@@ -216,6 +216,7 @@ fn notify(&self, count: c_int) {/// This method behaves like `notify_one`, except that it hints to the scheduler that the /// current thread is about to go to sleep, so it should schedule the target thread on the same /// CPU. +#[inline] pub fn notify_sync (&self) {// SAFETY: `wait_queue_head` points to valid memory. unsafe { bindings::__wake_up_sync (self.wait_queue_head.get (),TASK_NORMAL ) };
@@ -225,6 +226,7 @@ pub fn notify_sync(&self) {/// /// This is not 'sticky' in the sense that if no thread is waiting, the notification is lost /// completely (as opposed to automatically waking up the next waiter). +#[inline] pub fn notify_one (&self) { self.notify (1 ); }
@@ -233,6 +235,7 @@ pub fn notify_one(&self) {/// /// This is not 'sticky' in the sense that if no thread is waiting, the notification is lost /// completely (as opposed to automatically waking up the next waiter). +#[inline] pub fn notify_all (&self) { self.notify (0 ); }
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v2 2/5 rust: sync: Mark PollCondVar::drop() inline
In reply to Boqun FengFrom: Kunwu Chan <[email protected]>
When building the kernel using the llvm-18.1.3-rust-1.85.0-x86_64 with ARCH=arm64, the following symbols are generated:
$nm vmlinux | grep ' _R'.*PollCondVar | rustfilt
... T <kernel::sync::poll::PollCondVar as kernel::init::PinnedDrop>::drop
...
This Rust symbol is trivial wrappers around the C functions __wake_up_pollfree() and synchronize_rcu(). It doesn't make sense to go through a trivial wrapper for its functions, so mark it inline.
[boqun: Reword the commit title and re-format the commit log per tip tree's requirement, remove unnecessary information from "nm vmlinux" result.]
--- rust/kernel/sync/poll.rs | 1 + 1 file changed, 1 insertion(+)
rust/kernel/sync/poll.rs +1 -0
@@ -107,6 +107,7 @@ fn deref(&self) -> &CondVar {#[pinned_drop] impl PinnedDrop for PollCondVar { +#[inline] fn drop (self:Pin <&mut Self >) {// Clear anything registered using `register_wait`. //
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v2 3/5 rust: task: Mark Task methods inline
In reply to Boqun FengFrom: Panagiotis Foliadis <[email protected]>
When building the kernel using the llvm-18.1.3-rust-1.85.0-x86_64 toolchain provided by kernel.org, the following symbols are generated:
$ nm vmlinux | grep ' _R'.*Task | rustfilt ... T <kernel::task::Task>::get_pid_ns ... T <kernel::task::Task>::tgid_nr_ns ... T <kernel::task::Task>::current_pid_ns ... T <kernel::task::Task>::signal_pending ... T <kernel::task::Task>::uid ... T <kernel::task::Task>::euid ... T <kernel::task::Task>::current ... T <kernel::task::Task>::wake_up ... T <kernel::task::Task as kernel::types::AlwaysRefCounted>::dec_ref ... T <kernel::task::Task as kernel::types::AlwaysRefCounted>::inc_ref
These Rust symbols are trivial wrappers around the C functions. It doesn't make sense to go through a trivial wrapper for these functions, so mark them inline.
[boqun: Capitalize the title, reword a bit to avoid listing all the C functions as the code already shows them and remove the addresses of the symbols in the commit log as they are different from build to build.]
--- rust/kernel/task.rs | 9 +++++++++ 1 file changed, 9 insertions(+)
rust/kernel/task.rs +9 -0
@@ -173,6 +173,7 @@ pub fn current_raw() -> *mut bindings::task_struct {/// Callers must ensure that the returned object is only used to access a [`CurrentTask`] /// within the task context that was active when this function was called. For more details, /// see the invariants section for [`CurrentTask`]. +#[inline] pub unsafe fn current () ->impl Deref <Target =CurrentTask > {struct TaskRef { task: *const CurrentTask ,
@@ -222,24 +223,28 @@ pub fn pid(&self) -> Pid { }/// Returns the UID of the given task. +#[inline] pub fn uid (&self) ->Kuid {// SAFETY: It's always safe to call `task_uid` on a valid task. Kuid ::from_raw (unsafe { bindings::task_uid (self.as_ptr ()) }) }/// Returns the effective UID of the given task. +#[inline] pub fn euid (&self) ->Kuid {// SAFETY: It's always safe to call `task_euid` on a valid task. Kuid ::from_raw (unsafe { bindings::task_euid (self.as_ptr ()) }) }/// Determines whether the given task has pending signals. +#[inline] pub fn signal_pending (&self) ->bool {// SAFETY: It's always safe to call `signal_pending` on a valid task. unsafe { bindings::signal_pending (self.as_ptr ()) !=0 } }/// Returns task's pid namespace with elevated reference count +#[inline] pub fn get_pid_ns (&self) ->Option <ARef <PidNamespace >> {// SAFETY: By the type invariant, we know that `self.0` is valid. let ptr =unsafe { bindings::task_get_pid_ns (self.as_ptr ()) };
@@ -255,6 +260,7 @@ pub fn get_pid_ns(&self) -> Option<ARef<PidNamespace>> {/// Returns the given task's pid in the provided pid namespace. #[doc(alias = "task_tgid_nr_ns" )] +#[inline] pub fn tgid_nr_ns (&self, pidns:Option <&PidNamespace >) ->Pid {let pidns =match pidns {Some (pidns) => pidns.as_ptr (),
@@ -268,6 +274,7 @@ pub fn tgid_nr_ns(&self, pidns: Option<&PidNamespace>) -> Pid { }/// Wakes up the task. +#[inline] pub fn wake_up (&self) {// SAFETY: It's always safe to call `wake_up_process` on a valid task, even if the task // running.
@@ -341,11 +348,13 @@ pub fn active_pid_ns(&self) -> Option<&PidNamespace> {// SAFETY: The type invariants guarantee that `Task` is always refcounted. unsafe impl crate ::types::AlwaysRefCounted for Task { +#[inline] fn inc_ref (&self) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. unsafe { bindings::get_task_struct (self.as_ptr ()) }; } +#[inline] unsafe fn dec_ref (obj: ptr::NonNull <Self >) {// SAFETY: The safety requirements guarantee that the refcount is nonzero. unsafe { bindings::put_task_struct (obj.cast ().as_ptr ()) }
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v2 4/5 rust: Introduce file_from_location()
In reply to Boqun FengMost of kernel debugging facilities take a nul-terminated string for file names for a callsite (generated from __FILE__), however the Rust courterpart, Location, would return a Rust string (not nul-terminated) from method .file(). And such a string cannot be passed to C debugging function directly.
There is ongoing work to support a Location::file_with_nul() [1], which returns a nul-terminated string from a Location. Since it's still working in progress, and it will take some time before the feature finally gets stabilized and the kernel's minimal rustc version might also take a while to bump to a version that at least has that feature, introduce a file_from_location() function, which returns a warning string if Location::file_with_nul() is not available.
This should work in most cases because as for now the known usage of Location::file_with_nul() is only in debugging code (e.g. might_sleep()) and there might be other information reported by the debugging code that could help locate the problematic function, so missing the file name is fine at the moment.
--- init/Kconfig | 3 +++ rust/kernel/lib.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+)
init/Kconfig +3 -0
@@ -142,6 +142,9 @@ config RUSTC_HAS_SPAN_FILE config RUSTC_HAS_UNNECESSARY_TRANSMUTES def_bool RUSTC_VERSION >= 108800 +config RUSTC_HAS_FILE_WITH_NUL + def_bool RUSTC_VERSION >= 108900 + config PAHOLE_VERSION int default $(shell,$(srctree)/scripts/pahole-version.sh $(PAHOLE))
rust/kernel/lib.rs +48 -0
57 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v2 5/5 rust: task: Add Rust version of might_sleep()
In reply to Boqun FengFrom: FUJITA Tomonori <[email protected]>
Add a helper function equivalent to the C's might_sleep(), which serves as a debugging aid and a potential scheduling point.
Note that this function can only be used in a nonatomic context.
This will be used by Rust version of read_poll_timeout().
[boqun: Use file_from_location() to get a C string instead of changing __might_sleep()]
--- rust/helpers/task.c | 6 ++++++ rust/kernel/task.rs | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+)
rust/helpers/task.c +6 -0
@@ -1,7 +1,13 @@// SPDX-License-Identifier: GPL-2.0 +#include <linux/kernel.h> #include <linux/sched/task.h> +void rust_helper_might_resched (void ) +{ +might_resched (); +} +struct task_struct *rust_helper_get_current (void ) {return current;
rust/kernel/task.rs +24 -0
@@ -400,3 +400,27 @@ fn eq(&self, other: &Kuid) -> bool { }impl Eq for Kuid {} + +/// Annotation for functions that can sleep. +/// +/// Equivalent to the C side [`might_sleep()`], this function serves as +/// a debugging aid and a potential scheduling point. +/// +/// This function can only be used in a nonatomic context. +/// +/// [`might_sleep()`]: https://docs.kernel.org/driver-api/basics.html#c.might_sleep +#[track_caller] +#[inline] +pub fn might_sleep () { +#[cfg( CONFIG_DEBUG_ATOMIC_SLEEP )] + { +let loc = core::panic::Location ::caller (); +let file = kernel::file_from_location (loc); + +// SAFETY: `file.as_ptr()` is valid for reading and guaranteed to be nul-terminated. +unsafe {crate ::bindings::__might_sleep (file.as_ptr ().cast (), loc.line ()as i32 ) } + } + +// SAFETY: Always safe to call. +unsafe {crate ::bindings::might_resched () } +}
Signature · 1 line
2.39.5 (Apple Git-154)
Quoted 8 lines · Boqun Feng
Hi Ingo & Peter,
This is the updated version from my pull request last cycle:
v1: https://lore.kernel.org/rust-for-linux/[email protected]/
Please take a look, thanks!
Ping ;-) I forgot to add that this is a dependency for Rust version's read_poll_timeout(), which is a dependency to a few things:
* In the Tyr driver:
https://lore.kernel.org/rust-for-linux/[email protected]/
* In Nova, the gpu driver:
https://lore.kernel.org/rust-for-linux/[email protected]/
* In the qt2025 phy driver:
https://lore.kernel.org/lkml/[email protected]/
Thanks!
Regards,
Boqun
Quoted 56 lines
Changes since v1:
-
Location::file_with_nul()is used to avoid the C changes of__might_sleep()Regards,
BoqunThe following changes since commit
5bc34be478d09c4d16009e665e020ad0fcd0deea:sched/core: Reorganize cgroup bandwidth control interface file writes (2025-06-18 13:59:57 +0200)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/boqun/linux.gittags/rust-sched.2025.06.24for you to fetch changes up to
7e611710acf966df1e14bcf4e067385e38e549a1:rust: task: Add Rust version of
might_sleep()(2025-06-24 15:53:50 -0700)----------------------------------------------------------------
Rust task & schedule changes for v6.17:- Make Task, CondVar and PollCondVar methods inline to avoid unnecessary function calls
- Add
might_sleep()support for Rust code: Rust's "#[track_caller]" mechanism is used so that Rust'smight_sleep()doesn't need to be defined as a macro----------------------------------------------------------------
Boqun Feng (1):
rust: Introducefile_from_location()FUJITA Tomonori (1):
rust: task: Add Rust version ofmight_sleep()Kunwu Chan (2):
rust: sync: Mark CondVar::notify_*() inline
rust: sync: MarkPollCondVar::drop()inlinePanagiotis Foliadis (1):
rust: task: Mark Task methods inlineinit/Kconfig | 3 +++ rust/helpers/task.c | 6 +++++ rust/kernel/lib.rs | 48 +++++++++++++++++++++++++++++++++++++ rust/kernel/sync/condvar.rs | 3 +++ rust/kernel/sync/poll.rs | 1 + rust/kernel/task.rs | 33 +++++++++++++++++++++++++ 6 files changed, 94 insertions(+)Signature · 1 line
2.39.5 (Apple Git-154)
Alice Ryhl
maintainerQuoted 20 lines · Boqun Feng
Quoted 8 lines · Boqun Feng
Hi Ingo & Peter,
This is the updated version from my pull request last cycle:
v1: https://lore.kernel.org/rust-for-linux/[email protected]/
Please take a look, thanks!
Ping ;-) I forgot to add that this is a dependency for Rust version's
read_poll_timeout(), which is a dependency to a few things:* In the
Tyrdriver:https://lore.kernel.org/rust-for-linux/[email protected]/
* In Nova, the gpu driver:
Quoted 7 lines
https://lore.kernel.org/rust-for-linux/[email protected]/
* In the qt2025 phy driver:
https://lore.kernel.org/lkml/[email protected]/
Thanks!
I would love to see this land to unblock the above users. I wanted to add that I fixed the Rust language so that it NUL-terminates these filename strings, so Ingo's concern from the last cycle should not apply to this version of the PR.
Thanks!
Alice
Quoted 23 lines · Alice Ryhl
Quoted 22 lines · Boqun Feng
Quoted 8 lines · Boqun Feng
Hi Ingo & Peter,
This is the updated version from my pull request last cycle:
v1: https://lore.kernel.org/rust-for-linux/[email protected]/
Please take a look, thanks!
Ping ;-) I forgot to add that this is a dependency for Rust version's
read_poll_timeout(), which is a dependency to a few things:* In the
Tyrdriver:https://lore.kernel.org/rust-for-linux/[email protected]/
* In Nova, the gpu driver:
Quoted 7 lines
Quoted 5 lines
* In the qt2025 phy driver:
https://lore.kernel.org/lkml/[email protected]/
Thanks!
I would love to see this land to unblock the above users. I wanted to
Thanks, Alice. It's already merged in tip tree:
https://lore.kernel.org/lkml/175153176741.406.14557081807475800171.tip-bot2@tip-bot2/ https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/commit/?id=7e611710acf966df1e14bcf4e067385e38e549a1
Quoted 4 lines
add that I fixed the Rust language so that it NUL-terminates these filename strings, so Ingo's concern from the last cycle should not apply to this version of the PR.
Thanks a lot for pushing the changes at the Rust language side.
Regards,
Boqun
Quoted 3 lines
Thanks!
Alice
Alice Ryhl
maintainerQuoted 29 lines · Boqun Feng
Quoted 27 lines · Alice Ryhl
Quoted 26 lines · Boqun Feng
Quoted 8 lines · Boqun Feng
Hi Ingo & Peter,
This is the updated version from my pull request last cycle:
v1: https://lore.kernel.org/rust-for-linux/[email protected]/
Please take a look, thanks!
Ping ;-) I forgot to add that this is a dependency for Rust version's
read_poll_timeout(), which is a dependency to a few things:* In the
Tyrdriver:https://lore.kernel.org/rust-for-linux/[email protected]/
* In Nova, the gpu driver:
https://lore.kernel.org/rust-for-linux/[email protected]/
* In the qt2025 phy driver:
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Thanks!
I would love to see this land to unblock the above users. I wanted to
Thanks, Alice. It's already merged in tip tree:
https://lore.kernel.org/lkml/175153176741.406.14557081807475800171.tip-bot2@tip-bot2/ https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/commit/?id=7e611710acf966df1e14bcf4e067385e38e549a1
Thanks for clarifying.
Alice