From: "Danilo Krummrich" <dakr@kernel.org>
To: "Alice Ryhl" <aliceryhl@google.com>
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
"Lorenzo Stoakes" <lorenzo.stoakes@oracle.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Andrew Ballance" <andrewjballance@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>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
linux-kernel@vger.kernel.org, maple-tree@lists.infradead.org,
rust-for-linux@vger.kernel.org, linux-mm@kvack.org
Subject: Re: [PATCH v3 1/3] rust: maple_tree: add MapleTree
Date: Tue, 02 Sep 2025 13:11:34 +0200 [thread overview]
Message-ID: <DCI9P6V88RHN.BX0BQWFDA8DO@kernel.org> (raw)
In-Reply-To: <aLa4rNojDIeShIrw@google.com>
On Tue Sep 2, 2025 at 11:28 AM CEST, Alice Ryhl wrote:
> On Tue, Sep 02, 2025 at 11:01:19AM +0200, Danilo Krummrich wrote:
>> On Tue Sep 2, 2025 at 10:35 AM CEST, Alice Ryhl wrote:
>> > The maple tree will be used in the Tyr driver to allocate and keep track
>> > of GPU allocations created internally (i.e. not by userspace). It will
>> > likely also be used in the Nova driver eventually.
>> >
>> > This adds the simplest methods for additional and removal that do not
>> > require any special care with respect to concurrency.
>> >
>> > This implementation is based on the RFC by Andrew but with significant
>> > changes to simplify the implementation.
>> >
>> > Co-developed-by: Andrew Ballance <andrewjballance@gmail.com>
>> > Signed-off-by: Andrew Ballance <andrewjballance@gmail.com>
>> > Signed-off-by: Alice Ryhl <aliceryhl@google.com>
>>
>> One nit below, otherwise:
>>
>> Reviewed-by: Danilo Krummrich <dakr@kernel.org>
>
> Thanks!
>
>> > + pub fn insert_range<R>(&self, range: R, value: T, gfp: Flags) -> Result<(), InsertError<T>>
>> > + where
>> > + R: RangeBounds<usize>,
>> > + {
>> > + let Some((first, last)) = to_maple_range(range) else {
>> > + return Err(InsertError {
>> > + value,
>> > + cause: InsertErrorKind::InvalidRequest,
>> > + });
>> > + };
>> > +
>> > + let ptr = T::into_foreign(value);
>> > +
>> > + // SAFETY: The tree is valid, and we are passing a pointer to an owned instance of `T`.
>> > + let res = to_result(unsafe {
>> > + bindings::mtree_insert_range(self.tree.get(), first, last, ptr, gfp.as_raw())
>> > + });
>> > +
>> > + if let Err(err) = res {
>> > + // SAFETY: As `mtree_insert_range` failed, it is safe to take back ownership.
>> > + let value = unsafe { T::from_foreign(ptr) };
>> > +
>> > + let cause = if err == ENOMEM {
>> > + InsertErrorKind::AllocError(kernel::alloc::AllocError)
>> > + } else if err == EEXIST {
>> > + InsertErrorKind::Occupied
>> > + } else {
>> > + InsertErrorKind::InvalidRequest
>> > + };
>> > + Err(InsertError { value, cause })
>> > + } else {
>> > + Ok(())
>> > + }
>> > + }
>>
>> // SAFETY: The tree is valid, and we are passing a pointer to an owned instance of `T`.
>> to_result(unsafe {
>> bindings::mtree_insert_range(self.tree.get(), first, last, ptr, gfp.as_raw())
>> }).map_err(|err| {
>> // SAFETY: As `mtree_insert_range` failed, it is safe to take back ownership.
>> let value = unsafe { T::from_foreign(ptr) };
>>
>> let cause = if err == ENOMEM {
>> InsertErrorKind::AllocError(kernel::alloc::AllocError)
>> } else if err == EEXIST {
>> InsertErrorKind::Occupied
>> } else {
>> InsertErrorKind::InvalidRequest
>> };
>> Err(InsertError { value, cause })
>> })
>>
>> I think that's a bit cleaner than the above (not compile tested).
>
> I don't love it. How about a match instead of if/else?
I think if you don't like map_err(), the if/else is fine to keep as is.
Personally, I prefer map_err() over any "manual matching" in this case; it gets
us rid of `ref`, making to_result().map_err() a single statement returning
exactly what we expect. It makes it more self-contained.
next prev parent reply other threads:[~2025-09-02 11:11 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-02 8:35 [PATCH v3 0/3] Add Rust abstraction for Maple Trees Alice Ryhl
2025-09-02 8:35 ` [PATCH v3 1/3] rust: maple_tree: add MapleTree Alice Ryhl
2025-09-02 9:01 ` Danilo Krummrich
2025-09-02 9:28 ` Alice Ryhl
2025-09-02 11:11 ` Danilo Krummrich [this message]
2025-09-02 8:35 ` [PATCH v3 2/3] rust: maple_tree: add lock guard for maple tree Alice Ryhl
2025-09-02 8:35 ` [PATCH v3 3/3] rust: maple_tree: add MapleTreeAlloc 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=DCI9P6V88RHN.BX0BQWFDA8DO@kernel.org \
--to=dakr@kernel.org \
--cc=Liam.Howlett@oracle.com \
--cc=a.hindborg@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=aliceryhl@google.com \
--cc=andrewjballance@gmail.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=lossin@kernel.org \
--cc=maple-tree@lists.infradead.org \
--cc=ojeda@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;
as well as URLs for NNTP newsgroup(s).