From: Benno Lossin <benno.lossin@proton.me>
To: "Andreas Hindborg" <a.hindborg@samsung.com>,
"Maíra Canal" <mcanal@igalia.com>
Cc: "Asahi Lina" <lina@asahilina.net>,
"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>,
"Alice Ryhl" <aliceryhl@google.com>,
"Matthew Wilcox" <willy@infradead.org>,
"rust-for-linux@vger.kernel.org" <rust-for-linux@vger.kernel.org>,
"kernel-dev@igalia.com" <kernel-dev@igalia.com>
Subject: Re: [PATCH v5] rust: xarray: Add an abstraction for XArray
Date: Thu, 07 Dec 2023 19:05:31 +0000 [thread overview]
Message-ID: <fbb39b45-25d2-4ed6-a826-ded2aac56010@proton.me> (raw)
In-Reply-To: <87cyvjjuak.fsf@samsung.com>
On 12/6/23 15:10, Andreas Hindborg wrote:
> Maíra Canal <mcanal@igalia.com> writes:
>> + /// Borrow the underlying value wrapped by the `Guard`.
>> + ///
>> + /// Returns a `T::Borrowed` type for the owned `ForeignOwnable` type.
>> + pub fn borrow(&self) -> T::Borrowed<'_> {
>> + // SAFETY: The value is owned by the `XArray`, the lifetime it is borrowed for must not
>> + // outlive the `XArray` itself, nor the Guard that holds the lock ensuring the value
>> + // remains in the `XArray`.
>> + unsafe { T::borrow(self.0.as_ptr() as _) }
>
> I think specifying the target type of the cast here is preferable. Then
> the code will not break silently if something should change.
I agree.
[...]
>> + /// Replaces an entry with a new value, returning the old value (if any).
>> + pub fn replace(self: Pin<&Self>, index: usize, value: T) -> Result<Option<T>> {
>> + let new = value.into_foreign();
>> + // SAFETY: `new` just came from into_foreign(), and we dismiss this guard if
>> + // the xa_store operation succeeds and takes ownership of the pointer.
>> + let guard = ScopeGuard::new(|| unsafe {
>> + T::from_foreign(new);
>> + });
>> +
>> + // SAFETY: `self.xa` is always valid by the type invariant, and we are storing
>> + // a `T::into_foreign()` result which upholds the later invariants.
>
> T has to be 4-byte aligned according to xarray docs. All types that we
> implement `ForeignOwnable` for is aligned such, because
> `KernelAllocator` is producing allocations of alignment
> `ARCH_SLAB_MINALIGN`, which is `__alignof__(unsigned long long)`. But it
> should probably be in a safety requirement somewhere. Perhaps on
> `ForeignOwnable` and then make that an unsafe trait?
I am not sure if we want to enforce that for every `ForeignOwnable`
type, could there be anything that wants to implement `ForeignOwnable`
that does not have that alignment?
If we do not want to tag it onto `ForeignOwnable`, we should put this
not only in some safety requirement, but also just check it at compile
time. You can do this using:
build_assert!(mem::align_of::<T>() <= 4);
At some point Rust might have an `Aligned` trait [1] and then we could
use that.
[1]: https://github.com/rust-lang/rfcs/pull/3319
[...]
>> +// SAFETY: XArray is thread-safe and all mutation operations are internally locked.
>> +unsafe impl<T: Send + ForeignOwnable> Send for XArray<T> {}
>> +unsafe impl<T: Sync + ForeignOwnable> Sync for XArray<T> {}
>
> Do we actually need to specify `ForeignOwnable` here?
Yes, this is needed, since `XArray<T>` is declared with
`T: ForeignOwnable`, since that is needed for dropping an `XArray<T>`.
--
Cheers,
Benno
next prev parent reply other threads:[~2023-12-07 19:05 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-01 19:50 [PATCH v5] rust: xarray: Add an abstraction for XArray Maíra Canal
2023-12-06 11:31 ` Benno Lossin
[not found] ` <CGME20231206141047eucas1p1b6c2b022717a6b3a2e24e37b6a6d5d23@eucas1p1.samsung.com>
2023-12-06 14:10 ` Andreas Hindborg
2023-12-07 19:05 ` Benno Lossin [this message]
2023-12-08 5:21 ` Trevor Gross
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=fbb39b45-25d2-4ed6-a826-ded2aac56010@proton.me \
--to=benno.lossin@proton.me \
--cc=a.hindborg@samsung.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=kernel-dev@igalia.com \
--cc=lina@asahilina.net \
--cc=mcanal@igalia.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--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).