From: Tamir Duberstein <tamird@gmail.com>
To: Andreas Hindborg <a.hindborg@kernel.org>
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>,
"Benno Lossin" <lossin@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Matthew Wilcox" <willy@infradead.org>,
"Andrew Morton" <akpm@linux-foundation.org>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org, linux-mm@kvack.org,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Janne Grunau" <j@jannau.net>
Subject: Re: [PATCH v2 3/3] rust: xarray: add `insert` and `reserve`
Date: Mon, 11 Aug 2025 09:42:26 -0400 [thread overview]
Message-ID: <CAJ-ks9kTacXO_PbcH8c-60Ae88vJ_w6_pbmXLzOpzTKgRjiXPw@mail.gmail.com> (raw)
In-Reply-To: <87o6smf0no.fsf@t14s.mail-host-address-is-not-set>
On Mon, Aug 11, 2025 at 9:29 AM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>
> "Tamir Duberstein" <tamird@gmail.com> writes:
>
> > Add `Guard::{insert,reserve}` and `Guard::{insert,reserve}_limit`, which
> > are akin to `__xa_{alloc,insert}` in C.
> >
> > Note that unlike `xa_reserve` which only ensures that memory is
> > allocated, the semantics of `Reservation` are stricter and require
> > precise management of the reservation. Indices which have been reserved
> > can still be overwritten with `Guard::store`, which allows for C-like
> > semantics if desired.
> >
> > `__xa_cmpxchg_raw` is exported to facilitate the semantics described
> > above.
> >
> > Tested-by: Janne Grunau <j@jannau.net>
> > Reviewed-by: Janne Grunau <j@jannau.net>
> > Signed-off-by: Tamir Duberstein <tamird@gmail.com>
>
> <cut>
>
> > + /// Stores an element somewhere in the given range of indices.
> > + ///
> > + /// On success, takes ownership of `ptr`.
> > + ///
> > + /// On failure, ownership returns to the caller.
> > + ///
> > + /// # Safety
> > + ///
> > + /// `ptr` must be `NULL` or have come from a previous call to `T::into_foreign`.
> > + unsafe fn alloc(
>
>
> The naming of this method in C is confusing. Could we call it
> insert_limit_raw on the Rust side?
I think I prefer to hew close to the C naming. Is there prior art
where Rust names deviate from C names?
> Even though this is private, I think we should also document that the
> effect of inserting NULL is to reserve the entry.
👍
> > + &mut self,
> > + limit: impl ops::RangeBounds<u32>,
> > + ptr: *mut T::PointedTo,
> > + gfp: alloc::Flags,
> > + ) -> Result<usize> {
> > + // NB: `xa_limit::{max,min}` are inclusive.
> > + let limit = bindings::xa_limit {
> > + max: match limit.end_bound() {
> > + ops::Bound::Included(&end) => end,
> > + ops::Bound::Excluded(&end) => end - 1,
> > + ops::Bound::Unbounded => u32::MAX,
> > + },
> > + min: match limit.start_bound() {
> > + ops::Bound::Included(&start) => start,
> > + ops::Bound::Excluded(&start) => start + 1,
> > + ops::Bound::Unbounded => 0,
> > + },
> > + };
> > +
> > + let mut index = u32::MAX;
> > +
> > + // SAFETY:
> > + // - `self.xa` is always valid by the type invariant.
> > + // - `self.xa` was initialized with `XA_FLAGS_ALLOC` or `XA_FLAGS_ALLOC1`.
> > + //
> > + // INVARIANT: `ptr` is either `NULL` or came from `T::into_foreign`.
> > + match unsafe {
> > + bindings::__xa_alloc(
> > + self.xa.xa.get(),
> > + &mut index,
> > + ptr.cast(),
> > + limit,
> > + gfp.as_raw(),
> > + )
> > + } {
> > + 0 => Ok(to_usize(index)),
> > + errno => Err(Error::from_errno(errno)),
> > + }
> > + }
> > +
> > + /// Allocates an entry somewhere in the array.
>
> Should we rephrase this to match `alloc`?
>
> Stores an entry somewhere in the given range of indices.
👍
> <cut>
>
> > +impl<T: ForeignOwnable> Reservation<'_, T> {
> > + /// Returns the index of the reservation.
> > + pub fn index(&self) -> usize {
> > + self.index
> > + }
> > +
> > + /// Replaces the reserved entry with the given entry.
> > + ///
> > + /// # Safety
> > + ///
> > + /// `ptr` must be `NULL` or have come from a previous call to `T::into_foreign`.
>
> We should document the effect of replacing with NULL.
👍
> Best regards,
> Andreas Hindborg
>
>
next prev parent reply other threads:[~2025-08-11 13:43 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-13 12:05 [PATCH v2 0/3] rust: xarray: add `insert` and `reserve` Tamir Duberstein
2025-07-13 12:05 ` [PATCH v2 1/3] rust: xarray: use the prelude Tamir Duberstein
2025-08-11 11:06 ` Andreas Hindborg
2025-07-13 12:05 ` [PATCH v2 2/3] rust: xarray: implement Default for AllocKind Tamir Duberstein
2025-08-11 11:07 ` Andreas Hindborg
2025-07-13 12:05 ` [PATCH v2 3/3] rust: xarray: add `insert` and `reserve` Tamir Duberstein
2025-08-11 12:56 ` Beata Michalska
2025-08-11 13:09 ` Tamir Duberstein
2025-08-11 14:34 ` Beata Michalska
2025-08-11 18:02 ` Tamir Duberstein
2025-08-13 8:00 ` Beata Michalska
2025-08-11 13:28 ` Andreas Hindborg
2025-08-11 13:42 ` Tamir Duberstein [this message]
2025-08-11 13:56 ` Miguel Ojeda
2025-07-23 1:38 ` [PATCH v2 0/3] " Daniel Almeida
2025-07-24 18:50 ` Daniel Almeida
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=CAJ-ks9kTacXO_PbcH8c-60Ae88vJ_w6_pbmXLzOpzTKgRjiXPw@mail.gmail.com \
--to=tamird@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=j@jannau.net \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--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).