rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>
To: peterz@infradead.org, mingo@redhat.com, will@kernel.org,
	boqun.feng@gmail.com, longman@redhat.com, ojeda@kernel.org,
	alex.gaynor@gmail.com, gary@garyguo.net,
	bjorn3_gh@protonmail.com, benno.lossin@proton.me,
	a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu
Cc: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: [PATCH] rust: sync: lock: Add Lock::get_mut()
Date: Fri, 31 Jan 2025 12:59:39 -0300	[thread overview]
Message-ID: <20250131155940.305403-1-trintaeoitogc@gmail.com> (raw)

At initialization where we can guarantee that we do not have multiple
threads accessing the protected resource, blocking the resource in
addition to being redundant, can cause unnecessary overhead.
Add the Lock::get_mut() function for access the data without lock.
Receive a mutable instance of Lock, and return a mutable reference to
data because if the instance is mutable, the rust compiler guarantee the
access control.

Suggested-by: Björn Roy Baron <bjorn3_gh@protonmail.com>
Signed-off-by: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>
---
 rust/kernel/sync/lock.rs | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index eb80048e0110..f1e29820ce99 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -140,6 +140,43 @@ pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni
             }),
         })
     }
+
+    /// Get a mutable reference to data
+    ///
+    /// ```
+    /// use kernel::sync::{new_mutex, Mutex};
+    ///
+    /// struct Inner {
+    ///     a: u32,
+    /// }
+    ///
+    /// #[pin_data]
+    /// struct Example {
+    ///     #[pin]
+    ///     d: Mutex<Inner>,
+    /// }
+    ///
+    /// impl Example {
+    ///     fn new() -> impl PinInit<Self> {
+    ///         pin_init!(Self {
+    ///             // This new_mutex! can be anothers locks like new_spinlock!()
+    ///             d <- new_mutex!(Inner { a: 20 })
+    ///         })
+    ///     }
+    /// }
+    ///
+    /// let mut pin = KBox::pin_init(Example::new(), GFP_KERNEL)?;
+    /// let mut_pin = pin.as_mut();
+    ///
+    /// let data = unsafe { Pin::get_unchecked_mut(mut_pin).d.get_mut() };
+    /// assert_eq!(data.a, 20);
+    /// ```
+    pub fn get_mut(&mut self) -> &mut T {
+        // SAFETY: the UnsafeCell guarantee that the self.data is not null.
+        // The `&mut self` guarantees the exclusive access to the underlying data, therefore it's
+        // safe to reborrow the inner data.
+        unsafe { &mut *self.data.get() }
+    }
 }
 
 impl<B: Backend> Lock<(), B> {
-- 
2.34.1


             reply	other threads:[~2025-01-31 15:59 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-31 15:59 Guilherme Giacomo Simoes [this message]
2025-02-03  9:21 ` [PATCH] rust: sync: lock: Add Lock::get_mut() Alice Ryhl
2025-02-03 17:55   ` Guilherme Giacomo Simoes

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=20250131155940.305403-1-trintaeoitogc@gmail.com \
    --to=trintaeoitogc@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=will@kernel.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).