Re: [PATCH] rust: task: adjust safety comments in Task methods

From: Alice Ryhl
Date: Tue Oct 15 2024 - 14:50:58 EST


On 10/15/24 8:45 PM, Boqun Feng wrote:
On Tue, Oct 15, 2024 at 08:37:36PM +0200, Alice Ryhl wrote:
On 10/15/24 8:24 PM, Boqun Feng wrote:
On Tue, Oct 15, 2024 at 02:02:12PM +0000, Alice Ryhl wrote:
The `Task` struct has several safety comments that aren't so great. For
example, the reason that it's okay to read the `pid` is that the field
is immutable, so there is no data race, which is not what the safety
comment says.

Thus, improve the safety comments. Also add an `as_ptr` helper. This
makes it easier to read the various accessors on Task, as `self.0` may
be confusing syntax for new Rust users.

Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
---
This is based on top of vfs.rust.file as the file series adds some new
task methods. Christian, can you take this through that tree?
---
rust/kernel/task.rs | 43 ++++++++++++++++++++++++-------------------
1 file changed, 24 insertions(+), 19 deletions(-)

diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
index 1a36a9f19368..080599075875 100644
--- a/rust/kernel/task.rs
+++ b/rust/kernel/task.rs
@@ -145,11 +145,17 @@ fn deref(&self) -> &Self::Target {
}
}
+ /// Returns a raw pointer to the task.
+ #[inline]
+ pub fn as_ptr(&self) -> *mut bindings::task_struct {

FWIW, I think the name convention is `as_raw()` for a wrapper type of
`Opaque<T>` to return `*mut T`, e.g. `kernel::device::Device`.

Otherwise this looks good to me.
Both names are in use. See e.g. Page and File that use as_ptr.


`Page` is a different case because it currently is a pointer.

In fact, I was asked to change the name on File *to* as_ptr.


I'm not able to find the discussion on that ask. Appreciate it if you
can share a link.

Hmm, it's possible that I misremember. I can't find it either. But it is called as_ptr on File.

Anyway, this is not important for now, and might not be in the future.
So:

Reviewed-by: Boqun Feng <boqun.feng@xxxxxxxxx>

Thanks!

Alice