linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@samsung.com>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Alexander Viro" <viro@zeniv.linux.org.uk>,
	"Christian Brauner" <brauner@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Arve Hjønnevåg" <arve@android.com>,
	"Todd Kjos" <tkjos@android.com>,
	"Martijn Coenen" <maco@android.com>,
	"Joel Fernandes" <joel@joelfernandes.org>,
	"Carlos Llamas" <cmllamas@google.com>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Dan Williams" <dan.j.williams@intel.com>,
	"Kees Cook" <keescook@chromium.org>,
	"Matthew Wilcox" <willy@infradead.org>,
	"Thomas Gleixner" <tglx@linutronix.de>
Cc: Daniel Xu <dxu@dxuuu.xyz>, Alice Ryhl <aliceryhl@google.com>,
	linux-kernel@vger.kernel.org,  rust-for-linux@vger.kernel.org,
	linux-fsdevel@vger.kernel.org
Subject: [PATCH v4 2/9] rust: task: add `Task::current_raw`
Date: Fri,  2 Feb 2024 10:55:36 +0000	[thread overview]
Message-ID: <20240202-alice-file-v4-2-fc9c2080663b@google.com> (raw)
In-Reply-To: <20240202-alice-file-v4-0-fc9c2080663b@google.com>

Introduces a safe function for getting a raw pointer to the current
task.

When writing bindings that need to access the current task, it is often
more convenient to call a method that directly returns a raw pointer
than to use the existing `Task::current` method. However, the only way
to do that is `bindings::get_current()` which is unsafe since it calls
into C. By introducing `Task::current_raw()`, it becomes possible to
obtain a pointer to the current task without using unsafe.

Link: https://lore.kernel.org/all/CAH5fLgjT48X-zYtidv31mox3C4_Ogoo_2cBOCmX0Ang3tAgGHA@mail.gmail.com/
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
 rust/kernel/task.rs | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
index 148a4f4eb7a8..b579367fb923 100644
--- a/rust/kernel/task.rs
+++ b/rust/kernel/task.rs
@@ -97,6 +97,15 @@ unsafe impl Sync for Task {}
 type Pid = bindings::pid_t;
 
 impl Task {
+    /// Returns a raw pointer to the current task.
+    ///
+    /// It is up to the user to use the pointer correctly.
+    #[inline]
+    pub fn current_raw() -> *mut bindings::task_struct {
+        // SAFETY: Getting the current pointer is always safe.
+        unsafe { bindings::get_current() }
+    }
+
     /// Returns a task reference for the currently executing task/thread.
     ///
     /// The recommended way to get the current task/thread is to use the
@@ -119,14 +128,12 @@ pub unsafe fn current() -> impl Deref<Target = Task> {
             }
         }
 
-        // SAFETY: Just an FFI call with no additional safety requirements.
-        let ptr = unsafe { bindings::get_current() };
-
+        let current = Task::current_raw();
         TaskRef {
             // SAFETY: If the current thread is still running, the current task is valid. Given
             // that `TaskRef` is not `Send`, we know it cannot be transferred to another thread
             // (where it could potentially outlive the caller).
-            task: unsafe { &*ptr.cast() },
+            task: unsafe { &*current.cast() },
             _not_send: NotThreadSafe,
         }
     }
-- 
2.43.0.594.gd9cf4e227d-goog


  parent reply	other threads:[~2024-02-02 10:55 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 ` Alice Ryhl [this message]
2024-02-06  2:18   ` [PATCH v4 2/9] rust: task: add `Task::current_raw` 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
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=20240202-alice-file-v4-2-fc9c2080663b@google.com \
    --to=aliceryhl@google.com \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.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).