Re: [PATCH v4 2/3] rust: pid: add Pid abstraction and init_ns helper

From: Alice Ryhl

Date: Wed Feb 11 2026 - 05:46:50 EST


On Fri, Feb 06, 2026 at 05:53:35PM +0900, jongan.kim@xxxxxxx wrote:
> From: HeeSu Kim <heesu0025.kim@xxxxxxx>
>
> 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: Find a pid by number under RCU protection
> - pid_task: Get the task associated with a pid under RCU protection
>
> Also add init_ns() associated function to PidNamespace 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.
>
> Suggested-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> Link: https://lore.kernel.org/lkml/aXs3OjlGzQVABAwR@xxxxxxxxxx/
> Suggested-by: Gary Guo <gary@xxxxxxxxxxx>
> Link: https://lore.kernel.org/lkml/DG15B78C8IK4.ITL5HKRZ1QKP@xxxxxxxxxxx/
> Signed-off-by: HeeSu Kim <heesu0025.kim@xxxxxxx>
> Reviewed-by: Gary Guo <gary@xxxxxxxxxxx>

> +#ifndef __rust_helper
> +#define __rust_helper
> +#endif

This should not be included. __rust_helper is always defined.

> + pub fn find_vpid<'a>(nr: i32, _rcu_guard: &'a rcu::Guard) -> Option<&'a Self> {

This should use the typedef of pid_t (in task.rs) instead of i32.

> diff --git a/rust/kernel/pid_namespace.rs b/rust/kernel/pid_namespace.rs
> index 979a9718f153..fc815945d614 100644
> --- a/rust/kernel/pid_namespace.rs
> +++ b/rust/kernel/pid_namespace.rs
> @@ -38,6 +38,15 @@ pub unsafe fn from_ptr<'a>(ptr: *const bindings::pid_namespace) -> &'a Self {
> // `PidNamespace` type being transparent makes the cast ok.
> unsafe { &*ptr.cast() }
> }
> +
> + /// 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_ns() -> &'static Self {
> + // SAFETY: `init_pid_ns` is a global static that is valid for the lifetime of the kernel.
> + unsafe { Self::from_ptr(&raw const bindings::init_pid_ns) }
> + }

This is no longer used anywhere.

> }
>
> // SAFETY: Instances of `PidNamespace` are always reference-counted.
> @@ -63,3 +72,11 @@ 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 {}
> +
> +impl PartialEq for PidNamespace {
> + fn eq(&self, other: &Self) -> bool {
> + self.as_ptr() == other.as_ptr()
> + }
> +}
> +impl Eq for PidNamespace {}

This is not used anywhere either.

Alice