From: Daniel Almeida <daniel.almeida@collabora.com>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
"Bjorn Helgaas" <bhelgaas@google.com>,
"Mika Westerberg" <mika.westerberg@linux.intel.com>,
"Ying Huang" <huang.ying.caritas@gmail.com>,
"Benno Lossin" <lossin@kernel.org>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
"Fiona Behrens" <me@kloenk.dev>
Subject: Re: [PATCH v12 1/3] rust: io: add resource abstraction
Date: Tue, 8 Jul 2025 14:43:33 -0300 [thread overview]
Message-ID: <F1EA22CF-1C01-495D-97ED-59D51A45A8B0@collabora.com> (raw)
In-Reply-To: <aGt6CZAUeuK0XnmP@google.com>
Hi Alice,
[…]
>> +
>> +impl Resource {
>> + /// Creates a reference to a [`Resource`] from a valid pointer.
>> + ///
>> + /// # Safety
>> + ///
>> + /// The caller must ensure that for the duration of 'a, the pointer will
>> + /// point at a valid `bindings::resource`.
>> + ///
>> + /// The caller must also ensure that the [`Resource`] is only accessed via the
>> + /// returned reference for the duration of 'a.
>> + pub(crate) const unsafe fn as_ref<'a>(ptr: *mut bindings::resource) -> &'a Self {
>
> We usually call this method `from_raw`.
Hmm, pretty sure I have seen the exact opposite being asked. In fact, this was
discussed a bit at length a while ago. See the thread starting at [0] for context.
>
>> + )
>> + };
>> +
>> + Some(Region(NonNull::new(region)?))
>> + }
>> +
>> + /// Returns the size of the resource.
>> + pub fn size(&self) -> ResourceSize {
>> + let inner = self.0.get();
>> + // SAFETY: safe as per the invariants of `Resource`.
>> + unsafe { bindings::resource_size(inner) }
>> + }
>> +
>> + /// Returns the start address of the resource.
>> + pub fn start(&self) -> u64 {
>> + let inner = self.0.get();
>> + // SAFETY: safe as per the invariants of `Resource`.
>> + unsafe { *inner }.start
>
> This needs to be
>
> unsafe { (*inner).start }
>
> What you wrote is not equivalent. (*inner) is a place expression, but
> when you write `unsafe { <place expr> }` that turns it into a value
> expression by reading the value. So this code copies the entire struct
> to the stack, and then you read `start` from the copy on the stack.
To be honest, I've seen a bug where the opposite was going on, a field was
being written on the value that was copied to the stack, leaving the original unchanged.
In any case, I thought this would be optimized away by the compiler. Also, IMHO
there should be a lint for this, because it does not make sense to copy a struct
just to read a field if we can just read the original location.
Thanks for catching that though, I will fix it on the next iteration :)
[…]
Thanks for the other comments,
— Daniel
[0] https://lore.kernel.org/rust-for-linux/B3564D77-9E26-4019-9B75-B0C53B26B64F@collabora.com/
next prev parent reply other threads:[~2025-07-08 17:44 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-04 16:25 [PATCH v12 0/3] rust: platform: add Io support Daniel Almeida
2025-07-04 16:25 ` [PATCH v12 1/3] rust: io: add resource abstraction Daniel Almeida
2025-07-05 17:28 ` Danilo Krummrich
2025-07-07 7:30 ` Alice Ryhl
2025-07-07 7:40 ` Alice Ryhl
2025-07-08 17:43 ` Daniel Almeida [this message]
2025-07-09 13:36 ` Alice Ryhl
2025-07-10 7:18 ` Alice Ryhl
2025-07-10 13:16 ` Daniel Almeida
2025-07-10 13:24 ` Alice Ryhl
2025-07-10 13:33 ` Daniel Almeida
2025-07-10 13:40 ` Alice Ryhl
2025-07-04 16:25 ` [PATCH v12 2/3] rust: io: mem: add a generic iomem abstraction Daniel Almeida
2025-07-05 17:24 ` Danilo Krummrich
2025-07-07 7:46 ` Alice Ryhl
2025-07-10 13:58 ` Daniel Almeida
2025-07-14 11:51 ` Alice Ryhl
2025-07-14 12:09 ` Danilo Krummrich
2025-07-14 23:39 ` Daniel Almeida
2025-07-04 16:25 ` [PATCH v12 3/3] rust: platform: add resource accessors Daniel Almeida
2025-07-05 17:34 ` Danilo Krummrich
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=F1EA22CF-1C01-495D-97ED-59D51A45A8B0@collabora.com \
--to=daniel.almeida@collabora.com \
--cc=a.hindborg@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=bhelgaas@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=huang.ying.caritas@gmail.com \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=me@kloenk.dev \
--cc=mika.westerberg@linux.intel.com \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/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®