linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Valentin Obst <kernel@valentinobst.de>
To: gregkh@linuxfoundation.org
Cc: a.hindborg@samsung.com, alex.gaynor@gmail.com,
	aliceryhl@google.com, arve@android.com, benno.lossin@proton.me,
	bjorn3_gh@protonmail.com, boqun.feng@gmail.com,
	brauner@kernel.org, cmllamas@google.com,
	dan.j.williams@intel.com, dxu@dxuuu.xyz, gary@garyguo.net,
	joel@joelfernandes.org, keescook@chromium.org,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	maco@android.com, ojeda@kernel.org, peterz@infradead.org,
	rust-for-linux@vger.kernel.org, surenb@google.com,
	tglx@linutronix.de, tkjos@android.com, viro@zeniv.linux.org.uk,
	wedsonaf@gmail.com, willy@infradead.org,
	Valentin Obst <kernel@valentinobst.de>
Subject: Re: [PATCH v4 7/9] rust: file: add `Kuid` wrapper
Date: Mon,  5 Feb 2024 23:06:30 +0100	[thread overview]
Message-ID: <20240205220630.16170-1-kernel@valentinobst.de> (raw)
In-Reply-To: <2024020214-concierge-rework-2ac5@gregkh>

> > +    /// Returns the given task's pid in the current pid namespace.
> > +    pub fn pid_in_current_ns(&self) -> Pid {
> > +        let current = Task::current_raw();
> > +        // SAFETY: Calling `task_active_pid_ns` with the current task is always safe.
> > +        let namespace = unsafe { bindings::task_active_pid_ns(current) };
> > +        // SAFETY: We know that `self.0.get()` is valid by the type invariant, and the namespace
> > +        // pointer is not dangling since it points at this task's namespace.
> > +        unsafe { bindings::task_tgid_nr_ns(self.0.get(), namespace) }
> > +    }
>
> pids are reference counted in the kernel, how does this deal with that?
> Are they just ignored somehow?  Where is the reference count given back?
>
> thanks,
>
> greg k-h

As far as I can see, neither `task_active_pid_ns` nor `task_active_pid_ns`
return with an incremented refcount. However, looking at the above code,
it looks like it could be simplified to:

```rust
pub fn pid_in_current_ns(&self) -> Pid {
    // SAFETY: We know that `self.0.get()` is valid by the type invariant.
    // Passing a null pointer in the second argument defaults to the current ns.
    unsafe { bindings::task_tgid_nr_ns(self.0.get(), ptr::null_mut()) }
}
```

Since:

```C
static inline pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
{
	return __task_pid_nr_ns(tsk, PIDTYPE_TGID, ns);
}
...
pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
			struct pid_namespace *ns)
{
	pid_t nr = 0;

	rcu_read_lock();
	if (!ns)
		ns = task_active_pid_ns(current);
	nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns);
	rcu_read_unlock();

	return nr;
}
EXPORT_SYMBOL(__task_pid_nr_ns);
```

anyway defaults to current's pid ns (plus some RCU lock protection, not sure if
that is relevant here).

	- Best Valentin

  parent reply	other threads:[~2024-02-05 22:22 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-02 10:55 [PATCH v4 0/9] File abstractions needed by Rust Binder Alice Ryhl
2024-02-02 10:55 ` [PATCH v4 1/9] rust: types: add `NotThreadSafe` Alice Ryhl
2024-02-06  2:14   ` Trevor Gross
2024-02-07 13:22   ` Martin Rodriguez Reboredo
2024-02-02 10:55 ` [PATCH v4 2/9] rust: task: add `Task::current_raw` Alice Ryhl
2024-02-06  2:18   ` Trevor Gross
2024-02-07 13:24   ` Martin Rodriguez Reboredo
2024-02-02 10:55 ` [PATCH v4 3/9] rust: file: add Rust abstraction for `struct file` Alice Ryhl
2024-02-05 12:33   ` Benno Lossin
2024-02-06  2:48   ` Trevor Gross
2024-02-08 15:05     ` Alice Ryhl
2024-02-07 18:33   ` Martin Rodriguez Reboredo
2024-02-08 15:06     ` Alice Ryhl
2024-02-02 10:55 ` [PATCH v4 4/9] rust: cred: add Rust abstraction for `struct cred` Alice Ryhl
2024-02-05 12:20   ` Benno Lossin
2024-02-06  2:57   ` Trevor Gross
2024-02-07 18:58   ` Martin Rodriguez Reboredo
2024-02-08 15:07     ` Alice Ryhl
2024-02-02 10:55 ` [PATCH v4 5/9] rust: security: add abstraction for secctx Alice Ryhl
2024-02-06  3:04   ` Trevor Gross
2024-02-07 19:01   ` Martin Rodriguez Reboredo
2024-02-02 10:55 ` [PATCH v4 6/9] rust: file: add `FileDescriptorReservation` Alice Ryhl
2024-02-07 19:03   ` Martin Rodriguez Reboredo
2024-02-02 10:55 ` [PATCH v4 7/9] rust: file: add `Kuid` wrapper Alice Ryhl
2024-02-02 15:36   ` Greg Kroah-Hartman
2024-02-02 15:56     ` Alice Ryhl
2024-02-05 22:06     ` Valentin Obst [this message]
2024-02-07 19:05   ` Martin Rodriguez Reboredo
2024-02-02 10:55 ` [PATCH v4 8/9] rust: file: add `DeferredFdCloser` Alice Ryhl
2024-02-05 12:18   ` Benno Lossin
2024-02-08  0:26   ` Martin Rodriguez Reboredo
2024-02-02 10:55 ` [PATCH v4 9/9] rust: file: add abstraction for `poll_table` Alice Ryhl
2024-02-05 12:27   ` Benno Lossin
2024-02-08  0:32   ` Martin Rodriguez Reboredo

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=20240205220630.16170-1-kernel@valentinobst.de \
    --to=kernel@valentinobst.de \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=arve@android.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=brauner@kernel.org \
    --cc=cmllamas@google.com \
    --cc=dan.j.williams@intel.com \
    --cc=dxu@dxuuu.xyz \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=joel@joelfernandes.org \
    --cc=keescook@chromium.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maco@android.com \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=surenb@google.com \
    --cc=tglx@linutronix.de \
    --cc=tkjos@android.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=wedsonaf@gmail.com \
    --cc=willy@infradead.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).