From: HeeSu Kim <heesu0025.kim@lge.com>
Add a new Pid abstraction in rust/kernel/pid.rs that wraps the
kernel's struct pid and provides safe Rust interfaces for:
- find_vpid_with_guard: Find a pid by number under RCU protection
- pid_task_with_guard: Get the task associated with a pid under RCU
protection
Also add init_pid_ns() helper function to pid_namespace.rs to get
a reference to the init PID namespace.
These abstractions use lifetime-bounded references tied to RCU guards
to ensure memory safety when accessing RCU-protected data structures.
Signed-off-by: HeeSu Kim <heesu0025.kim@lge.com>
---
rust/kernel/lib.rs | 1 +
rust/kernel/pid.rs | 100 +++++++++++++++++++++++++++++++++++
rust/kernel/pid_namespace.rs | 9 ++++
3 files changed, 110 insertions(+)
create mode 100644 rust/kernel/pid.rs
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index f812cf120042..60a518d65d0e 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -122,6 +122,7 @@
pub mod page;
#[cfg(CONFIG_PCI)]
pub mod pci;
+pub mod pid;
pub mod pid_namespace;
pub mod platform;
pub mod prelude;
diff --git a/rust/kernel/pid.rs b/rust/kernel/pid.rs
new file mode 100644
index 000000000000..00989b953c23
--- /dev/null
+++ b/rust/kernel/pid.rs
@@ -0,0 +1,100 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Process identifiers (PIDs).
+//!
+//! C header: [`include/linux/pid.h`](srctree/include/linux/pid.h)
+
+use crate::{bindings, ffi::c_int, sync::rcu, task::Task, types::Opaque};
+
+/// Wraps the kernel's `struct pid`.
+///
+/// This structure represents the Rust abstraction for a C `struct pid`.
+/// A `Pid` represents a process identifier that can be looked up in different
+/// PID namespaces.
+#[repr(transparent)]
+pub struct Pid {
+ inner: Opaque<bindings::pid>,
+}
+
+impl Pid {
+ /// Returns a raw pointer to the inner C struct.
+ #[inline]
+ pub fn as_ptr(&self) -> *mut bindings::pid {
+ self.inner.get()
+ }
+
+ /// Finds a `struct pid` by its pid number within the current task's PID namespace.
+ ///
+ /// Returns `None` if no such pid exists.
+ ///
+ /// The returned reference is only valid for the duration of the RCU read-side
+ /// critical section represented by the `rcu::Guard`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::pid::Pid;
+ /// use kernel::sync::rcu;
+ ///
+ /// let guard = rcu::read_lock();
+ /// if let Some(pid) = Pid::find_vpid_with_guard(1, &guard) {
+ /// pr_info!("Found pid 1\n");
+ /// }
+ /// ```
+ ///
+ /// Returns `None` for non-existent PIDs:
+ ///
+ /// ```
+ /// use kernel::pid::Pid;
+ /// use kernel::sync::rcu;
+ ///
+ /// let guard = rcu::read_lock();
+ /// // PID 0 (swapper/idle) is not visible via find_vpid.
+ /// assert!(Pid::find_vpid_with_guard(0, &guard).is_none());
+ /// ```
+ #[inline]
+ pub fn find_vpid_with_guard<'a>(nr: i32, _rcu_guard: &'a rcu::Guard) -> Option<&'a Self> {
+ // SAFETY: Called under RCU protection as guaranteed by the Guard reference.
+ let ptr = unsafe { bindings::find_vpid(nr as c_int) };
+ if ptr.is_null() {
+ None
+ } else {
+ // SAFETY: `find_vpid` returns a valid pointer under RCU protection,
+ // and `Pid` is `#[repr(transparent)]` over `bindings::pid`.
+ Some(unsafe { &*(ptr as *const Self) })
+ }
+ }
+
+ /// Gets the task associated with this PID.
+ ///
+ /// Returns `None` if no task is associated with this PID.
+ ///
+ /// The returned reference is only valid for the duration of the RCU read-side
+ /// critical section represented by the `rcu::Guard`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::pid::Pid;
+ /// use kernel::sync::rcu;
+ ///
+ /// let guard = rcu::read_lock();
+ /// if let Some(pid) = Pid::find_vpid_with_guard(1, &guard) {
+ /// if let Some(task) = pid.pid_task_with_guard(&guard) {
+ /// pr_info!("Found task for pid 1\n");
+ /// }
+ /// }
+ /// ```
+ #[inline]
+ pub fn pid_task_with_guard<'a>(&'a self, _rcu_guard: &'a rcu::Guard) -> Option<&'a Task> {
+ // SAFETY: Called under RCU protection as guaranteed by the Guard reference.
+ let task_ptr = unsafe { bindings::pid_task(self.as_ptr(), bindings::pid_type_PIDTYPE_PID) };
+ if task_ptr.is_null() {
+ None
+ } else {
+ // SAFETY: `pid_task` returns a valid pointer under RCU protection,
+ // and `Task` is `#[repr(transparent)]` over `bindings::task_struct`.
+ Some(unsafe { &*task_ptr.cast() })
+ }
+ }
+}
diff --git a/rust/kernel/pid_namespace.rs b/rust/kernel/pid_namespace.rs
index 979a9718f153..6029e3e120d0 100644
--- a/rust/kernel/pid_namespace.rs
+++ b/rust/kernel/pid_namespace.rs
@@ -63,3 +63,12 @@ unsafe impl Send for PidNamespace {}
// SAFETY: It's OK to access `PidNamespace` through shared references from other threads because
// we're either accessing properties that don't change or that are properly synchronised by C code.
unsafe impl Sync for PidNamespace {}
+
+/// Returns a reference to the init PID namespace.
+///
+/// This is the root PID namespace that exists throughout the lifetime of the kernel.
+#[inline]
+pub fn init_pid_ns() -> &'static PidNamespace {
+ // SAFETY: `init_pid_ns` is a global static that is valid for the lifetime of the kernel.
+ unsafe { PidNamespace::from_ptr(core::ptr::addr_of!(bindings::init_pid_ns)) }
+}
--
2.25.1
On Thu Jan 29, 2026 at 8:41 AM GMT, jongan.kim wrote:
> From: HeeSu Kim <heesu0025.kim@lge.com>
>
> Add a new Pid abstraction in rust/kernel/pid.rs that wraps the
> kernel's struct pid and provides safe Rust interfaces for:
> - find_vpid_with_guard: Find a pid by number under RCU protection
> - pid_task_with_guard: Get the task associated with a pid under RCU
> protection
>
> Also add init_pid_ns() helper function to pid_namespace.rs to get
> a reference to the init PID namespace.
>
> These abstractions use lifetime-bounded references tied to RCU guards
> to ensure memory safety when accessing RCU-protected data structures.
>
> Signed-off-by: HeeSu Kim <heesu0025.kim@lge.com>
> ---
> rust/kernel/lib.rs | 1 +
> rust/kernel/pid.rs | 100 +++++++++++++++++++++++++++++++++++
> rust/kernel/pid_namespace.rs | 9 ++++
> 3 files changed, 110 insertions(+)
> create mode 100644 rust/kernel/pid.rs
>
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index f812cf120042..60a518d65d0e 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -122,6 +122,7 @@
> pub mod page;
> #[cfg(CONFIG_PCI)]
> pub mod pci;
> +pub mod pid;
> pub mod pid_namespace;
> pub mod platform;
> pub mod prelude;
> diff --git a/rust/kernel/pid.rs b/rust/kernel/pid.rs
> new file mode 100644
> index 000000000000..00989b953c23
> --- /dev/null
> +++ b/rust/kernel/pid.rs
> @@ -0,0 +1,100 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Process identifiers (PIDs).
> +//!
> +//! C header: [`include/linux/pid.h`](srctree/include/linux/pid.h)
> +
> +use crate::{bindings, ffi::c_int, sync::rcu, task::Task, types::Opaque};
> +
> +/// Wraps the kernel's `struct pid`.
> +///
> +/// This structure represents the Rust abstraction for a C `struct pid`.
> +/// A `Pid` represents a process identifier that can be looked up in different
> +/// PID namespaces.
> +#[repr(transparent)]
> +pub struct Pid {
> + inner: Opaque<bindings::pid>,
> +}
> +
> +impl Pid {
> + /// Returns a raw pointer to the inner C struct.
> + #[inline]
> + pub fn as_ptr(&self) -> *mut bindings::pid {
> + self.inner.get()
> + }
> +
> + /// Finds a `struct pid` by its pid number within the current task's PID namespace.
> + ///
> + /// Returns `None` if no such pid exists.
> + ///
> + /// The returned reference is only valid for the duration of the RCU read-side
> + /// critical section represented by the `rcu::Guard`.
> + ///
> + /// # Examples
> + ///
> + /// ```
> + /// use kernel::pid::Pid;
> + /// use kernel::sync::rcu;
> + ///
> + /// let guard = rcu::read_lock();
> + /// if let Some(pid) = Pid::find_vpid_with_guard(1, &guard) {
> + /// pr_info!("Found pid 1\n");
> + /// }
> + /// ```
> + ///
> + /// Returns `None` for non-existent PIDs:
> + ///
> + /// ```
> + /// use kernel::pid::Pid;
> + /// use kernel::sync::rcu;
> + ///
> + /// let guard = rcu::read_lock();
> + /// // PID 0 (swapper/idle) is not visible via find_vpid.
> + /// assert!(Pid::find_vpid_with_guard(0, &guard).is_none());
> + /// ```
> + #[inline]
> + pub fn find_vpid_with_guard<'a>(nr: i32, _rcu_guard: &'a rcu::Guard) -> Option<&'a Self> {
> + // SAFETY: Called under RCU protection as guaranteed by the Guard reference.
> + let ptr = unsafe { bindings::find_vpid(nr as c_int) };
This `as c_int` part is not needed.
> + if ptr.is_null() {
> + None
> + } else {
> + // SAFETY: `find_vpid` returns a valid pointer under RCU protection,
> + // and `Pid` is `#[repr(transparent)]` over `bindings::pid`.
> + Some(unsafe { &*(ptr as *const Self) })
> + }
> + }
> +
> + /// Gets the task associated with this PID.
> + ///
> + /// Returns `None` if no task is associated with this PID.
> + ///
> + /// The returned reference is only valid for the duration of the RCU read-side
> + /// critical section represented by the `rcu::Guard`.
> + ///
> + /// # Examples
> + ///
> + /// ```
> + /// use kernel::pid::Pid;
> + /// use kernel::sync::rcu;
> + ///
> + /// let guard = rcu::read_lock();
> + /// if let Some(pid) = Pid::find_vpid_with_guard(1, &guard) {
> + /// if let Some(task) = pid.pid_task_with_guard(&guard) {
> + /// pr_info!("Found task for pid 1\n");
> + /// }
> + /// }
> + /// ```
> + #[inline]
> + pub fn pid_task_with_guard<'a>(&'a self, _rcu_guard: &'a rcu::Guard) -> Option<&'a Task> {
> + // SAFETY: Called under RCU protection as guaranteed by the Guard reference.
> + let task_ptr = unsafe { bindings::pid_task(self.as_ptr(), bindings::pid_type_PIDTYPE_PID) };
> + if task_ptr.is_null() {
> + None
> + } else {
> + // SAFETY: `pid_task` returns a valid pointer under RCU protection,
> + // and `Task` is `#[repr(transparent)]` over `bindings::task_struct`.
> + Some(unsafe { &*task_ptr.cast() })
> + }
> + }
> +}
> diff --git a/rust/kernel/pid_namespace.rs b/rust/kernel/pid_namespace.rs
> index 979a9718f153..6029e3e120d0 100644
> --- a/rust/kernel/pid_namespace.rs
> +++ b/rust/kernel/pid_namespace.rs
> @@ -63,3 +63,12 @@ unsafe impl Send for PidNamespace {}
> // SAFETY: It's OK to access `PidNamespace` through shared references from other threads because
> // we're either accessing properties that don't change or that are properly synchronised by C code.
> unsafe impl Sync for PidNamespace {}
> +
> +/// Returns a reference to the init PID namespace.
> +///
> +/// This is the root PID namespace that exists throughout the lifetime of the kernel.
> +#[inline]
> +pub fn init_pid_ns() -> &'static PidNamespace {
> + // SAFETY: `init_pid_ns` is a global static that is valid for the lifetime of the kernel.
> + unsafe { PidNamespace::from_ptr(core::ptr::addr_of!(bindings::init_pid_ns)) }
> +}
I would probably put this as assoc fn, so you write `PidNamespace::init_ns()`
instead of `kernel::pid_namespace::init_pid_ns()`.
Best,
Gary
On Thu Jan 29, 2026 at 2:33 PM GMT, Gary Guo wrote:
> On Thu Jan 29, 2026 at 8:41 AM GMT, jongan.kim wrote:
>> From: HeeSu Kim <heesu0025.kim@lge.com>
>>
>> Add a new Pid abstraction in rust/kernel/pid.rs that wraps the
>> kernel's struct pid and provides safe Rust interfaces for:
>> - find_vpid_with_guard: Find a pid by number under RCU protection
>> - pid_task_with_guard: Get the task associated with a pid under RCU
>> protection
>>
>> Also add init_pid_ns() helper function to pid_namespace.rs to get
>> a reference to the init PID namespace.
>>
>> These abstractions use lifetime-bounded references tied to RCU guards
>> to ensure memory safety when accessing RCU-protected data structures.
>>
>> Signed-off-by: HeeSu Kim <heesu0025.kim@lge.com>
>> ---
>> rust/kernel/lib.rs | 1 +
>> rust/kernel/pid.rs | 100 +++++++++++++++++++++++++++++++++++
>> rust/kernel/pid_namespace.rs | 9 ++++
>> 3 files changed, 110 insertions(+)
>> create mode 100644 rust/kernel/pid.rs
>>
>> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
>> index f812cf120042..60a518d65d0e 100644
>> --- a/rust/kernel/lib.rs
>> +++ b/rust/kernel/lib.rs
>> @@ -122,6 +122,7 @@
>> pub mod page;
>> #[cfg(CONFIG_PCI)]
>> pub mod pci;
>> +pub mod pid;
>> pub mod pid_namespace;
>> pub mod platform;
>> pub mod prelude;
>> diff --git a/rust/kernel/pid.rs b/rust/kernel/pid.rs
>> new file mode 100644
>> index 000000000000..00989b953c23
>> --- /dev/null
>> +++ b/rust/kernel/pid.rs
>> @@ -0,0 +1,100 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +
>> +//! Process identifiers (PIDs).
>> +//!
>> +//! C header: [`include/linux/pid.h`](srctree/include/linux/pid.h)
>> +
>> +use crate::{bindings, ffi::c_int, sync::rcu, task::Task, types::Opaque};
>> +
>> +/// Wraps the kernel's `struct pid`.
>> +///
>> +/// This structure represents the Rust abstraction for a C `struct pid`.
>> +/// A `Pid` represents a process identifier that can be looked up in different
>> +/// PID namespaces.
>> +#[repr(transparent)]
>> +pub struct Pid {
>> + inner: Opaque<bindings::pid>,
>> +}
>> +
>> +impl Pid {
>> + /// Returns a raw pointer to the inner C struct.
>> + #[inline]
>> + pub fn as_ptr(&self) -> *mut bindings::pid {
>> + self.inner.get()
>> + }
>> +
>> + /// Finds a `struct pid` by its pid number within the current task's PID namespace.
>> + ///
>> + /// Returns `None` if no such pid exists.
>> + ///
>> + /// The returned reference is only valid for the duration of the RCU read-side
>> + /// critical section represented by the `rcu::Guard`.
>> + ///
>> + /// # Examples
>> + ///
>> + /// ```
>> + /// use kernel::pid::Pid;
>> + /// use kernel::sync::rcu;
>> + ///
>> + /// let guard = rcu::read_lock();
>> + /// if let Some(pid) = Pid::find_vpid_with_guard(1, &guard) {
>> + /// pr_info!("Found pid 1\n");
>> + /// }
>> + /// ```
>> + ///
>> + /// Returns `None` for non-existent PIDs:
>> + ///
>> + /// ```
>> + /// use kernel::pid::Pid;
>> + /// use kernel::sync::rcu;
>> + ///
>> + /// let guard = rcu::read_lock();
>> + /// // PID 0 (swapper/idle) is not visible via find_vpid.
>> + /// assert!(Pid::find_vpid_with_guard(0, &guard).is_none());
>> + /// ```
>> + #[inline]
>> + pub fn find_vpid_with_guard<'a>(nr: i32, _rcu_guard: &'a rcu::Guard) -> Option<&'a Self> {
>> + // SAFETY: Called under RCU protection as guaranteed by the Guard reference.
>> + let ptr = unsafe { bindings::find_vpid(nr as c_int) };
>
> This `as c_int` part is not needed.
>
>> + if ptr.is_null() {
>> + None
>> + } else {
>> + // SAFETY: `find_vpid` returns a valid pointer under RCU protection,
>> + // and `Pid` is `#[repr(transparent)]` over `bindings::pid`.
>> + Some(unsafe { &*(ptr as *const Self) })
>> + }
>> + }
>> +
>> + /// Gets the task associated with this PID.
>> + ///
>> + /// Returns `None` if no task is associated with this PID.
>> + ///
>> + /// The returned reference is only valid for the duration of the RCU read-side
>> + /// critical section represented by the `rcu::Guard`.
>> + ///
>> + /// # Examples
>> + ///
>> + /// ```
>> + /// use kernel::pid::Pid;
>> + /// use kernel::sync::rcu;
>> + ///
>> + /// let guard = rcu::read_lock();
>> + /// if let Some(pid) = Pid::find_vpid_with_guard(1, &guard) {
>> + /// if let Some(task) = pid.pid_task_with_guard(&guard) {
>> + /// pr_info!("Found task for pid 1\n");
>> + /// }
>> + /// }
>> + /// ```
>> + #[inline]
>> + pub fn pid_task_with_guard<'a>(&'a self, _rcu_guard: &'a rcu::Guard) -> Option<&'a Task> {
>> + // SAFETY: Called under RCU protection as guaranteed by the Guard reference.
>> + let task_ptr = unsafe { bindings::pid_task(self.as_ptr(), bindings::pid_type_PIDTYPE_PID) };
>> + if task_ptr.is_null() {
>> + None
>> + } else {
>> + // SAFETY: `pid_task` returns a valid pointer under RCU protection,
>> + // and `Task` is `#[repr(transparent)]` over `bindings::task_struct`.
>> + Some(unsafe { &*task_ptr.cast() })
>> + }
>> + }
>> +}
>> diff --git a/rust/kernel/pid_namespace.rs b/rust/kernel/pid_namespace.rs
>> index 979a9718f153..6029e3e120d0 100644
>> --- a/rust/kernel/pid_namespace.rs
>> +++ b/rust/kernel/pid_namespace.rs
>> @@ -63,3 +63,12 @@ unsafe impl Send for PidNamespace {}
>> // SAFETY: It's OK to access `PidNamespace` through shared references from other threads because
>> // we're either accessing properties that don't change or that are properly synchronised by C code.
>> unsafe impl Sync for PidNamespace {}
>> +
>> +/// Returns a reference to the init PID namespace.
>> +///
>> +/// This is the root PID namespace that exists throughout the lifetime of the kernel.
>> +#[inline]
>> +pub fn init_pid_ns() -> &'static PidNamespace {
>> + // SAFETY: `init_pid_ns` is a global static that is valid for the lifetime of the kernel.
>> + unsafe { PidNamespace::from_ptr(core::ptr::addr_of!(bindings::init_pid_ns)) }
>> +}
>
> I would probably put this as assoc fn, so you write `PidNamespace::init_ns()`
> instead of `kernel::pid_namespace::init_pid_ns()`.
>
> Best,
> Gary
Thanks for review and feedback.
I will update the patch to address both suggestions in the next revision.
Best Regards,
Heesu Kim
On Thu, Jan 29, 2026 at 05:41:18PM +0900, jongan.kim@lge.com wrote:
> From: HeeSu Kim <heesu0025.kim@lge.com>
>
> Add a new Pid abstraction in rust/kernel/pid.rs that wraps the
> kernel's struct pid and provides safe Rust interfaces for:
> - find_vpid_with_guard: Find a pid by number under RCU protection
> - pid_task_with_guard: Get the task associated with a pid under RCU
> protection
>
> Also add init_pid_ns() helper function to pid_namespace.rs to get
> a reference to the init PID namespace.
>
> These abstractions use lifetime-bounded references tied to RCU guards
> to ensure memory safety when accessing RCU-protected data structures.
>
> Signed-off-by: HeeSu Kim <heesu0025.kim@lge.com>
This looks really nice, thanks!
> +//! Process identifiers (PIDs).
> +//!
> +//! C header: [`include/linux/pid.h`](srctree/include/linux/pid.h)
> +
> +use crate::{bindings, ffi::c_int, sync::rcu, task::Task, types::Opaque};
Currently we use this formatting for imports:
use crate::{
bindings,
ffi::c_int,
sync::rcu,
task::Task,
types::Opaque, //
};
> +/// Wraps the kernel's `struct pid`.
> +///
> +/// This structure represents the Rust abstraction for a C `struct pid`.
> +/// A `Pid` represents a process identifier that can be looked up in different
> +/// PID namespaces.
> +#[repr(transparent)]
> +pub struct Pid {
> + inner: Opaque<bindings::pid>,
> +}
I would implement Send, Sync, and AlwaysRefCounted for Pid too.
> + // SAFETY: `find_vpid` returns a valid pointer under RCU protection,
> + // and `Pid` is `#[repr(transparent)]` over `bindings::pid`.
> + Some(unsafe { &*(ptr as *const Self) })
It would be nice to extract this cast into a Pid::from_raw().
> + // SAFETY: `pid_task` returns a valid pointer under RCU protection,
> + // and `Task` is `#[repr(transparent)]` over `bindings::task_struct`.
> + Some(unsafe { &*task_ptr.cast() })
I think it would be nice to add a Task::from_raw() to avoid the cast
here.
Some(unsafe { Task::from_raw(task_ptr) })
> + pub fn pid_task_with_guard<'a>(&'a self, _rcu_guard: &'a rcu::Guard) -> Option<&'a Task> {
> + pub fn find_vpid_with_guard<'a>(nr: i32, _rcu_guard: &'a rcu::Guard) -> Option<&'a Self> {
I think we can drop the 'with_guard' suffixes of these.
> +/// Returns a reference to the init PID namespace.
> +///
> +/// This is the root PID namespace that exists throughout the lifetime of the kernel.
> +#[inline]
> +pub fn init_pid_ns() -> &'static PidNamespace {
> + // SAFETY: `init_pid_ns` is a global static that is valid for the lifetime of the kernel.
> + unsafe { PidNamespace::from_ptr(core::ptr::addr_of!(bindings::init_pid_ns)) }
Simplifies to:
PidNamespace::from_ptr(&raw const bindings::init_pid_ns)
Alice
On Thu, Jan 29, 2026 at 10:32:26AM +0000, Alice Ryhl wrote:
> On Thu, Jan 29, 2026 at 05:41:18PM +0900, jongan.kim@lge.com wrote:
>> From: HeeSu Kim <heesu0025.kim@lge.com>
>>
>> Add a new Pid abstraction in rust/kernel/pid.rs that wraps the
>> kernel's struct pid and provides safe Rust interfaces for:
>> - find_vpid_with_guard: Find a pid by number under RCU protection
>> - pid_task_with_guard: Get the task associated with a pid under RCU
>> protection
>>
>> Also add init_pid_ns() helper function to pid_namespace.rs to get
>> a reference to the init PID namespace.
>>
>> These abstractions use lifetime-bounded references tied to RCU guards
>> to ensure memory safety when accessing RCU-protected data structures.
>>
>> Signed-off-by: HeeSu Kim <heesu0025.kim@lge.com>
>
> This looks really nice, thanks!
>
>> +//! Process identifiers (PIDs).
>> +//!
>> +//! C header: [`include/linux/pid.h`](srctree/include/linux/pid.h)
>> +
>> +use crate::{bindings, ffi::c_int, sync::rcu, task::Task, types::Opaque};
>
> Currently we use this formatting for imports:
>
> use crate::{
> bindings,
> ffi::c_int,
> sync::rcu,
> task::Task,
> types::Opaque, //
> };
>
>> +/// Wraps the kernel's `struct pid`.
>> +///
>> +/// This structure represents the Rust abstraction for a C `struct pid`.
>> +/// A `Pid` represents a process identifier that can be looked up in different
>> +/// PID namespaces.
>> +#[repr(transparent)]
>> +pub struct Pid {
>> + inner: Opaque<bindings::pid>,
>> +}
>
> I would implement Send, Sync, and AlwaysRefCounted for Pid too.
>
>> + // SAFETY: `find_vpid` returns a valid pointer under RCU protection,
>> + // and `Pid` is `#[repr(transparent)]` over `bindings::pid`.
>> + Some(unsafe { &*(ptr as *const Self) })
>
> It would be nice to extract this cast into a Pid::from_raw().
>
>> + // SAFETY: `pid_task` returns a valid pointer under RCU protection,
>> + // and `Task` is `#[repr(transparent)]` over `bindings::task_struct`.
>> + Some(unsafe { &*task_ptr.cast() })
>
> I think it would be nice to add a Task::from_raw() to avoid the cast
> here.
>
> Some(unsafe { Task::from_raw(task_ptr) })
>
>> + pub fn pid_task_with_guard<'a>(&'a self, _rcu_guard: &'a rcu::Guard) -> Option<&'a Task> {
>> + pub fn find_vpid_with_guard<'a>(nr: i32, _rcu_guard: &'a rcu::Guard) -> Option<&'a Self> {
>
> I think we can drop the 'with_guard' suffixes of these.
>
>> +/// Returns a reference to the init PID namespace.
>> +///
>> +/// This is the root PID namespace that exists throughout the lifetime of the kernel.
>> +#[inline]
>> +pub fn init_pid_ns() -> &'static PidNamespace {
>> + // SAFETY: `init_pid_ns` is a global static that is valid for the lifetime of the kernel.
>> + unsafe { PidNamespace::from_ptr(core::ptr::addr_of!(bindings::init_pid_ns)) }
>
> Simplifies to:
>
> PidNamespace::from_ptr(&raw const bindings::init_pid_ns)
>
> Alice
Thanks for the detailed review and feedback.
I agree with your suggestions. While our immediate use case only requires
RCU-protected access, implementing Send, Sync, and AlwaysRefCounted makes
sense from the broader rust/kernel perspective, as these abstractions
will likely be used in various contexts across the kernel.
I will incorporate all suggestions in the next revision.
Best Regards,
Heesu Kim
© 2016 - 2026 Red Hat, Inc.