All of lore.kernel.org
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Antonio Hickey <antoniohickey99@gmail.com>
Cc: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com,
	gary@garyguo.net, bjorn3_gh@protonmail.com,
	benno.lossin@proton.me, a.hindborg@samsung.com,
	aliceryhl@google.com, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH] rust: task: add `as_raw()` to `Task`
Date: Tue, 16 Jan 2024 10:24:18 -0800	[thread overview]
Message-ID: <ZabJ0lCKDahbCHGh@boqun-archlinux> (raw)
In-Reply-To: <20240116022823.64058-1-antoniohickey99@gmail.com>

On Mon, Jan 15, 2024 at 09:28:22PM -0500, Antonio Hickey wrote:
> Added new function `Task::as_raw()` which returns the raw pointer
> for the underlying task struct. I also refactored `Task` to instead
> use the newly created function instead of `self.0.get()` as I feel
> like `self.as_raw()` is more intuitive.
> 
> Signed-off-by: Antonio Hickey <antoniohickey99@gmail.com>
> ---
>  rust/kernel/task.rs | 33 +++++++++++++++++++--------------
>  1 file changed, 19 insertions(+), 14 deletions(-)
> 
> diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
> index d2f2615fe4a1..72737f5d86ab 100644
> --- a/rust/kernel/task.rs
> +++ b/rust/kernel/task.rs
> @@ -124,11 +124,16 @@ fn deref(&self) -> &Self::Target {
>          }
>      }
>  
> +    /// Returns a raw pointer to the underlying C task struct.
> +    pub fn as_raw(&self) -> *mut bindings::task_struct {
> +        self.0.get()
> +    }
> +
>      /// Returns the group leader of the given task.
>      pub fn group_leader(&self) -> &Task {
> -        // SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always
> +        // SAFETY: By the type invariant, we know that `self.as_raw()` is a valid task. Valid tasks always

You will need to update the type invariant on `as_raw()` then, if you
want to use it here ;-)

>          // have a valid group_leader.
> -        let ptr = unsafe { *ptr::addr_of!((*self.0.get()).group_leader) };
> +        let ptr = unsafe { *ptr::addr_of!((*self.as_raw()).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
> @@ -138,43 +143,43 @@ pub fn group_leader(&self) -> &Task {
>  
>      /// Returns the PID of the given task.
>      pub fn pid(&self) -> Pid {
> -        // SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always
> +        // SAFETY: By the type invariant, we know that `self.as_raw()` is a valid task. Valid tasks always
>          // have a valid pid.
> -        unsafe { *ptr::addr_of!((*self.0.get()).pid) }
> +        unsafe { *ptr::addr_of!((*self.as_raw()).pid) }
[...]
>      /// Wakes up the task.
>      pub fn wake_up(&self) {
> -        // SAFETY: By the type invariant, we know that `self.0.get()` is non-null and valid.
> +        // SAFETY: By the type invariant, we know that `self.raw()` is non-null and valid.

While we are at it, maybe we can clarify the terminology a bit,
according to:

	https://doc.rust-lang.org/core/ptr/index.html#safety

"valid" implies "non-null", so here we can just say:

	we know that `self.raw()` is a valid task pointer.

Of course, this only applies when we are talking about the validity of
a pointer in Rust. In other words, it's OK to say "null is the valid
input for this C function".

Regards,
Boqun

>          // And `wake_up_process` is safe to be called for any valid task, even if the task is
>          // running.
> -        unsafe { bindings::wake_up_process(self.0.get()) };
> +        unsafe { bindings::wake_up_process(self.as_raw()) };
>      }
>  }
>  
> -- 
> 2.43.0
> 

  parent reply	other threads:[~2024-01-16 18:24 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-16  2:28 [PATCH] rust: task: add `as_raw()` to `Task` Antonio Hickey
2024-01-16  2:28 ` [PATCH] rust: task: use safe `current!` macro Antonio Hickey
2024-01-16  8:57   ` Alice Ryhl
2024-01-16  7:20 ` [PATCH] rust: task: add `as_raw()` to `Task` Greg KH
2024-01-16  7:29 ` Wedson Almeida Filho
2024-01-16  8:53   ` Alice Ryhl
2024-01-16 18:24 ` Boqun Feng [this message]
2024-01-16 19:03   ` Miguel Ojeda

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=ZabJ0lCKDahbCHGh@boqun-archlinux \
    --to=boqun.feng@gmail.com \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=antoniohickey99@gmail.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=gary@garyguo.net \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=wedsonaf@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.