All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: jongan.kim@lge.com
Cc: arve@android.com, brauner@kernel.org, cmllamas@google.com,
	 gregkh@linuxfoundation.org, tkjos@android.com, ojeda@kernel.org,
	 boqun.feng@gmail.com, gary@garyguo.net,
	bjorn3_gh@protonmail.com,  lossin@kernel.org,
	a.hindborg@kernel.org, tmgross@umich.edu, dakr@kernel.org,
	 yury.norov@gmail.com, vitaly.wool@konsulko.se, tamird@gmail.com,
	 viresh.kumar@linaro.org, daniel.almeida@collabora.com,
	 linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	 heesu0025.kim@lge.com, ht.hong@lge.com, jungsu.hwang@lge.com,
	 kernel-team@android.com, sanghun.lee@lge.com,
	seulgi.lee@lge.com,  sunghoon.kim@lge.com
Subject: Re: [PATCH v2 2/3] rust: pid: add Pid abstraction and init_pid_ns helper
Date: Thu, 29 Jan 2026 10:32:26 +0000	[thread overview]
Message-ID: <aXs3OjlGzQVABAwR@google.com> (raw)
In-Reply-To: <20260129084119.32994-3-jongan.kim@lge.com>

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

  reply	other threads:[~2026-01-29 10:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-29  8:41 [PATCH v2 0/3] binder: handle PID namespace conversion for freeze operation jongan.kim
2026-01-29  8:41 ` [PATCH v2 1/3] " jongan.kim
2026-01-29 10:41   ` Alice Ryhl
2026-01-30  1:54     ` jongan.kim
2026-01-29  8:41 ` [PATCH v2 2/3] rust: pid: add Pid abstraction and init_pid_ns helper jongan.kim
2026-01-29 10:32   ` Alice Ryhl [this message]
2026-01-30 11:34     ` heesu0025.kim
2026-01-29 14:33   ` Gary Guo
2026-01-30  4:57     ` heesu0025.kim
2026-01-29  8:41 ` [PATCH v2 3/3] rust_binder: handle PID namespace conversion for freeze operation jongan.kim
2026-01-29 10:35   ` Alice Ryhl
2026-01-30  5:22     ` heesu0025.kim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aXs3OjlGzQVABAwR@google.com \
    --to=aliceryhl@google.com \
    --cc=a.hindborg@kernel.org \
    --cc=arve@android.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=brauner@kernel.org \
    --cc=cmllamas@google.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=heesu0025.kim@lge.com \
    --cc=ht.hong@lge.com \
    --cc=jongan.kim@lge.com \
    --cc=jungsu.hwang@lge.com \
    --cc=kernel-team@android.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sanghun.lee@lge.com \
    --cc=seulgi.lee@lge.com \
    --cc=sunghoon.kim@lge.com \
    --cc=tamird@gmail.com \
    --cc=tkjos@android.com \
    --cc=tmgross@umich.edu \
    --cc=viresh.kumar@linaro.org \
    --cc=vitaly.wool@konsulko.se \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.