rust: task: mark Task methods inline
PATCH v3 1/1 rust: task: mark Task methods inline
When you build 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 ffffffff817b2d30 T <kernel::task::Task>::get_pid_ns ffffffff817b2d50 T <kernel::task::Task>::tgid_nr_ns ffffffff817b2c90 T <kernel::task::Task>::current_pid_ns ffffffff817b2d00 T <kernel::task::Task>::signal_pending ffffffff817b2cc0 T <kernel::task::Task>::uid ffffffff817b2ce0 T <kernel::task::Task>::euid ffffffff817b2c70 T <kernel::task::Task>::current ffffffff817b2d70 T <kernel::task::Task>::wake_up ffffffff817b2db0 T <kernel::task::Task as kernel::types::AlwaysRefCounted>::dec_ref ffffffff817b2d90 T <kernel::task::Task as kernel::types::AlwaysRefCounted>::inc_ref
These Rust symbols are trivial wrappers around the C functions get_pid_ns, task_tgid_nr_ns, task_active_pid_ns, signal_pending, uid, euid, get_current, wake_up, get_task_struct and put_task_struct. It doesn't make sense to go through a trivial wrapper for these functions, so mark them inline.
After applying this patch, the above command will produce no output.
---
Changes in v3:
- Fix typo in patch description
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Added `#[inline] to all the remaining functions of task.rs file
- Link to v1: https://lore.kernel.org/r/[email protected]
--- rust/kernel/task.rs | 10 ++++++++++ 1 file changed, 10 insertions(+)
rust/kernel/task.rs +10 -0
@@ -132,6 +132,7 @@ pub fn current_raw() -> *mut bindings::task_struct {/// # Safety /// /// Callers must ensure that the returned object doesn't outlive the current task/thread. +#[inline] pub unsafe fn current () ->impl Deref <Target =Task > {struct TaskRef <'a> { task: &'aTask ,
@@ -166,6 +167,7 @@ fn deref(&self) -> &Self::Target {/// # Safety /// /// Callers must ensure that the returned object doesn't outlive the current task/thread. +#[inline] pub unsafe fn current_pid_ns () ->impl Deref <Target =PidNamespace > {struct PidNamespaceRef <'a> { task: &'aPidNamespace ,
@@ -273,24 +275,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 ()) };
@@ -306,6 +312,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 (),
@@ -319,6 +326,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 `signal_pending` on a valid task, even if the task // running.
@@ -328,11 +336,13 @@ pub fn wake_up(&self) {// 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 ()) }
---
base-commit: 7f0e9ee5e44887272627d0fcde0b19a675daf597
change-id: 20250308-inline-c-wrappers-da83ec1c2a77
Best regards,
Signature · 1 line
Panagiotis Foliadis <[email protected]>
Boqun Feng
Reviewed-by Benno Lossin Reviewed-by Christian Schrefl Reviewed-by Charalampos Mitrodimas Reviewed-by Alice Ryhl on v3 1/1Quoted 21 lines · Panagiotis Foliadis
When you build 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 ffffffff817b2d30 T <kernel::task::Task>::get_pid_ns ffffffff817b2d50 T <kernel::task::Task>::tgid_nr_ns ffffffff817b2c90 T <kernel::task::Task>::current_pid_ns ffffffff817b2d00 T <kernel::task::Task>::signal_pending ffffffff817b2cc0 T <kernel::task::Task>::uid ffffffff817b2ce0 T <kernel::task::Task>::euid ffffffff817b2c70 T <kernel::task::Task>::current ffffffff817b2d70 T <kernel::task::Task>::wake_up ffffffff817b2db0 T <kernel::task::Task as kernel::types::AlwaysRefCounted>::dec_ref ffffffff817b2d90 T <kernel::task::Task as kernel::types::AlwaysRefCounted>::inc_refThese Rust symbols are trivial wrappers around the C functions
get_pid_ns,task_tgid_nr_ns,task_active_pid_ns,signal_pending, uid, euid,get_current,wake_up,get_task_structandput_task_struct. It doesn't make sense to go through a trivial wrapper for these functions, so mark them inline.
Quoted 8 lines
After applying this patch, the above command will produce no output.
Reviewed-by Benno Lossin <[email protected]>Reviewed-by Christian Schrefl <[email protected]>Reviewed-by Charalampos Mitrodimas <[email protected]>Reviewed-by Alice Ryhl <[email protected]>Signed-off-by Panagiotis Foliadis <[email protected]>
I queued this for v6.16 via tip and more tests and reviews, thank you all. I changed a bit in the commit log per the usual tip tree rules, please see below, and let me know if something doesn't look good to you.
Regards,
Boqun
-------------------------->8
From: Panagiotis Foliadis <[email protected]>
Date: Sat, 15 Mar 2025 12:23:01 +0000
Subject: [PATCH] rust: task: Mark Task methods inline
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 | 10 ++++++++++ 1 file changed, 10 insertions(+)
rust/kernel/task.rs +10 -0
@@ -134,6 +134,7 @@ pub fn current_raw() -> *mut bindings::task_struct {/// # Safety /// /// Callers must ensure that the returned object doesn't outlive the current task/thread. +#[inline] pub unsafe fn current () ->impl Deref <Target =Task > {struct TaskRef <'a> { task: &'aTask ,
@@ -168,6 +169,7 @@ fn deref(&self) -> &Self::Target {/// # Safety /// /// Callers must ensure that the returned object doesn't outlive the current task/thread. +#[inline] pub unsafe fn current_pid_ns () ->impl Deref <Target =PidNamespace > {struct PidNamespaceRef <'a> { task: &'aPidNamespace ,
@@ -275,24 +277,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 ()) };
@@ -308,6 +314,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 (),
@@ -321,6 +328,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 `signal_pending` on a valid task, even if the task // running.
@@ -330,11 +338,13 @@ pub fn wake_up(&self) {// 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)
[tip: sched/core] rust: task: Mark Task methods inline
In reply to Panagiotis FoliadisThe following commit has been merged into the sched/core branch of tip:
The following commit has been merged into the sched/core branch of tip:
Commit-ID: 0a41f5af19391ce55cae1f0d7a562e8694bf1fd5 Gitweb: https://git.kernel.org/tip/0a41f5af19391ce55cae1f0d7a562e8694bf1fd5 Author: Panagiotis Foliadis <[email protected]> AuthorDate: Sat, 15 Mar 2025 12:23:01 Committer: Boqun Feng <[email protected]> CommitterDate: Tue, 24 Jun 2025 10:23:48 -07:00
rust: task: Mark Task methods inline
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 @@ impl Task {/// 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 @@ impl Task { }/// 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 @@ impl Task {/// 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 @@ impl Task { }/// 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 @@ impl CurrentTask {// 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 ()) }