From: Oleg Nesterov <oleg@redhat.com>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Panagiotis Foliadis" <pfoliadis@posteo.net>,
"Shankari Anand" <shankari.ak0208@gmail.com>,
"FUJITA Tomonori" <fujita.tomonori@gmail.com>
Cc: Alexey Gladkov <legion@kernel.org>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: rust: wrong SAFETY comments in group_leader() and pid() + questions
Date: Fri, 5 Dec 2025 15:08:23 +0100 [thread overview]
Message-ID: <aTLnV-5jlgfk1aRK@redhat.com> (raw)
From rust/kernel/task.rs:
pub fn group_leader(&self) -> &Task {
// SAFETY: The group leader of a task never changes after initialization, so reading this
// field is not a data race.
let ptr = unsafe { *ptr::addr_of!((*self.as_ptr()).group_leader) };
// SAFETY: The lifetime of the returned task reference is tied to the lifetime of `self`,
// and given that a task has a reference to its group leader, we know it must be valid for
// the lifetime of the returned task reference.
unsafe { &*ptr.cast() }
}
/// Returns the PID of the given task.
pub fn pid(&self) -> Pid {
// SAFETY: The pid of a task never changes after initialization, so reading this field is
// not a data race.
unsafe { *ptr::addr_of!((*self.as_ptr()).pid) }
}
The comments look wrong. Unless same_thread_group(current, task) == T, task->group_leader
and/or task->pid can change if a non-leader task's sub-thread execs. This also means that
in general it is not safe to dereference group_leader, for example this C code is not safe:
rcu_read_lock();
task = find_task_by_vpid(vpid);
if (task)
get_task_struct(task);
rcu_read_unlock();
if (task)
pid = task->group_leader->pid; // BUG! ->group_leader can be already freed
Now the questions. Sorry! I don't know rust.
1. Can I simply remove these misleading comments? Or SAFETY comment is mandatory?
2. I am working on the patch(es) which move ->group_leader from task_struct to
signal_struct, so the 1st change adds the new trivial helper in preparation:
struct task_struct *task_group_leader(struct task_struct *task)
{
return task->group_leader; // will be updated
}
Now, how can I change group_leader() to use it? I guess I need to add
struct task_struct *rust_helper_task_group_leader(struct task_struct *task)
{
return task_group_leader(task);
}
into rust/helpers/task.c, but will something like
pub fn group_leader(&self) -> &Task {
unsafe { bindings::task_group_leader(self.as_ptr()) }
}
work? I'm afraid it won't ;)
Oleg.
next reply other threads:[~2025-12-05 14:08 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-05 14:08 Oleg Nesterov [this message]
2025-12-05 14:19 ` rust: wrong SAFETY comments in group_leader() and pid() + questions Alice Ryhl
2025-12-05 14:29 ` Alice Ryhl
2025-12-05 17:21 ` Oleg Nesterov
2025-12-05 18:17 ` Alice Ryhl
2025-12-08 14:30 ` Oleg Nesterov
2025-12-11 1:20 ` Alice Ryhl
2025-12-08 15:43 ` Gary Guo
2025-12-11 1:16 ` Alice Ryhl
2025-12-05 15:35 ` Oleg Nesterov
2025-12-05 16:28 ` Alice Ryhl
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=aTLnV-5jlgfk1aRK@redhat.com \
--to=oleg@redhat.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=legion@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=pfoliadis@posteo.net \
--cc=rust-for-linux@vger.kernel.org \
--cc=shankari.ak0208@gmail.com \
--cc=tmgross@umich.edu \
/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).