mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dirk Behme <dirk.behme@de.bosch.com>
To: Benno Lossin <benno.lossin@proton.me>,
	Andreas Hindborg <a.hindborg@kernel.org>,
	Miguel Ojeda <ojeda@kernel.org>,
	Alex Gaynor <alex.gaynor@gmail.com>,
	Anna-Maria Behnsen <anna-maria@linutronix.de>,
	Frederic Weisbecker <frederic@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>
Cc: "Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Alice Ryhl" <aliceryhl@google.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 03/14] rust: sync: add `Arc::as_ptr`
Date: Tue, 1 Oct 2024 06:56:13 +0200	[thread overview]
Message-ID: <ad3e4f4c-883b-49a5-ba4b-562c13eee08e@de.bosch.com> (raw)
In-Reply-To: <e7e42ff2-1543-48b3-9bd3-bdef5ce66348@proton.me>

On 19.09.2024 16:03, Benno Lossin wrote:
> On 18.09.24 00:27, Andreas Hindborg wrote:
>> Add a method to get a pointer to the data contained in an `Arc`.
>>
>> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
>> ---
>>   rust/kernel/sync/arc.rs | 8 ++++++++
>>   1 file changed, 8 insertions(+)
>>
>> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
>> index 3673496c2363..a57ea3e2b44c 100644
>> --- a/rust/kernel/sync/arc.rs
>> +++ b/rust/kernel/sync/arc.rs
>> @@ -258,6 +258,14 @@ pub fn into_raw(self) -> *const T {
>>           unsafe { core::ptr::addr_of!((*ptr).data) }
>>       }
>>
>> +    /// Return a raw pointer to the data in this arc.
>> +    pub fn as_ptr(&self) -> *const T {
> 
> I don't know if we have a convention for this, but shouldn't this be an
> associated function? Because if `T` also has an `as_ptr` function, it
> will be shadowed by this one.

Yes. In Fabien's out of tree regmap we have an as_ptr() for Regmap [1] 
which operates on &Arc<Regmap> [2]. Once this patch is applied to arc.rs 
the compilation fails as then Arc.as_ptr() is used, not the 
Regmap.as_ptr() any more [3]. Switching this to something like [4] makes 
the compiler happy.

Thanks,

Dirk

P.S.: Just to learn something: For the unmodified, failing case: Is 
there a rule when which as_ptr() will be used? Is there an order rule 
for the shadowing? Any documentation link?


[1] 
https://github.com/Fabo/linux/blob/fparent/rust-ncv6336/rust/kernel/regmap.rs#L71

[2] 
https://github.com/Fabo/linux/blob/fparent/rust-ncv6336/rust/kernel/regulator/driver.rs#L418

[3]

error[E0308]: mismatched types
    --> rust/kernel/regulator/driver.rs:420:33
     |
420 |             config.cfg.regmap = regmap.as_ptr();
     |                                 ^^^^^^^^^^^^^^^ types differ in 
mutability
     |
     = note: expected raw pointer `*mut bindings::regmap`
                found raw pointer `*const Regmap`

error: aborting due to 1 previous error

[4]

diff --git a/rust/kernel/hrtimer/arc.rs b/rust/kernel/hrtimer/arc.rs
index ff04b0b75bb39..7c39ab440e1c6 100644
--- a/rust/kernel/hrtimer/arc.rs
+++ b/rust/kernel/hrtimer/arc.rs
@@ -25,7 +25,7 @@ unsafe impl<U> TimerHandle for ArcTimerHandle<U>
      U: HasTimer<U>,
  {
      fn cancel(&mut self) -> bool {
-        let self_ptr = self.inner.as_ptr();
+        let self_ptr = Arc::as_ptr(&self.inner);

          // SAFETY: As we obtained `self_ptr` from a valid reference 
above, it
          // must point to a valid `U`.
@@ -57,7 +57,7 @@ impl<U> TimerPointer for Arc<U>
      fn schedule(self, expires: Ktime) -> ArcTimerHandle<U> {
          // SAFETY: Since we generate the pointer passed to `schedule` 
from a
          // valid reference, it is a valid pointer.
-        unsafe { U::schedule(self.as_ptr(), expires) };
+        unsafe { U::schedule(Arc::as_ptr(&self), expires) };

          ArcTimerHandle { inner: self }
      }
diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index 1466d9cd41652..0a314c2f4c5ea 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -259,8 +259,8 @@ pub fn into_raw(self) -> *const T {
      }

      /// Return a raw pointer to the data in this arc.
-    pub fn as_ptr(&self) -> *const T {
-        let ptr = self.ptr.as_ptr();
+    pub fn as_ptr(arc: &Self) -> *const T {
+        let ptr = arc.ptr.as_ptr();
          // SAFETY: As we derive the pointer from a reference above, 
the pointer
          // must be valid.
          unsafe { core::ptr::addr_of!((*ptr).data) }



  parent reply	other threads:[~2024-10-01  4:56 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-17 22:27 [PATCH v2 00/14] hrtimer Rust API Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 01/14] rust: time: Add Ktime::from_ns() Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 02/14] rust: hrtimer: introduce hrtimer support Andreas Hindborg
2024-09-18 18:13   ` Benno Lossin
2024-09-19  5:43     ` Andreas Hindborg
2024-09-19 14:09       ` Benno Lossin
2024-09-23 16:35         ` Andreas Hindborg
2024-09-23 16:59           ` Benno Lossin
2024-10-10 12:24             ` Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 03/14] rust: sync: add `Arc::as_ptr` Andreas Hindborg
2024-09-19 14:03   ` Benno Lossin
2024-09-21 15:58     ` Gary Guo
2024-09-21 18:17       ` Benno Lossin
2024-09-23  8:14       ` Alice Ryhl
2024-10-01  4:56     ` Dirk Behme [this message]
2024-10-01  8:39       ` Benno Lossin
2024-09-17 22:27 ` [PATCH v2 04/14] rust: sync: add `Arc::clone_from_raw` Andreas Hindborg
2024-09-18 18:19   ` Benno Lossin
2024-09-18 20:12     ` Gary Guo
2024-09-18 21:09       ` Benno Lossin
2024-09-19  6:00       ` Andreas Hindborg
2024-09-19 14:15         ` Benno Lossin
2024-09-20  8:25           ` Andreas Hindborg
2024-09-19  5:54     ` Andreas Hindborg
2024-09-19  6:19       ` Andreas Hindborg
2024-09-19  6:41         ` Alice Ryhl
2024-09-17 22:27 ` [PATCH v2 05/14] rust: hrtimer: implement `TimerPointer` for `Arc` Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 06/14] rust: hrtimer: allow timer restart from timer handler Andreas Hindborg
2024-09-20 14:25   ` kernel test robot
2024-09-17 22:27 ` [PATCH v2 07/14] rust: hrtimer: add `UnsafeTimerPointer` Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 08/14] rust: hrtimer: implement `UnsafeTimerPointer` for `Pin<&T>` Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 09/14] rust: hrtimer: implement `UnsafeTimerPointer` for `Pin<&mut T>` Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 10/14] rust: hrtimer: add `hrtimer::ScopedTimerPointer` Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 11/14] rust: hrtimer: allow specifying a distinct callback parameter Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 12/14] rust: hrtimer: implement `TimerPointer` for `Pin<Box<T>>` Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 13/14] rust: hrtimer: add `schedule_function` to schedule closures Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 14/14] rust: hrtimer: add maintainer entry Andreas Hindborg
2024-10-12 15:19   ` Boqun Feng
2024-09-30  9:36 ` [PATCH v2 00/14] hrtimer Rust API Anna-Maria Behnsen
2024-10-04 10:47   ` Andreas Hindborg
2024-10-01 12:37 ` Dirk Behme
2024-10-01 14:42   ` Boqun Feng
2024-10-03  8:14     ` Dirk Behme
2024-10-03 13:03       ` Boqun Feng
2024-10-03 16:18         ` Dirk Behme
2024-10-11 14:52     ` Andreas Hindborg
2024-10-11 15:43       ` Dirk Behme
2024-10-11 23:21         ` Boqun Feng
2024-10-12  5:19           ` Dirk Behme
2024-10-12  7:41             ` Boqun Feng
2024-10-12  7:50               ` Dirk Behme
2024-10-12 22:26                 ` Boqun Feng
2024-10-13 17:39                   ` Dirk Behme
2024-10-13 21:06                     ` Boqun Feng
2024-10-14  6:58                       ` Dirk Behme
2024-10-14  9:17                         ` Andreas Hindborg
2024-10-14  9:38                         ` Alice Ryhl
2024-10-14 11:53                           ` Dirk Behme
2024-10-14 11:58                             ` 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=ad3e4f4c-883b-49a5-ba4b-562c13eee08e@de.bosch.com \
    --to=dirk.behme@de.bosch.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=anna-maria@linutronix.de \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=frederic@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tglx@linutronix.de \
    /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

all inboxes | Powered by JetHome®