lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <DCI9P6V88RHN.BX0BQWFDA8DO@kernel.org>
Date: Tue, 02 Sep 2025 13:11:34 +0200
From: "Danilo Krummrich" <dakr@...nel.org>
To: "Alice Ryhl" <aliceryhl@...gle.com>
Cc: "Andrew Morton" <akpm@...ux-foundation.org>, "Liam R. Howlett"
 <Liam.Howlett@...cle.com>, "Lorenzo Stoakes" <lorenzo.stoakes@...cle.com>,
 "Miguel Ojeda" <ojeda@...nel.org>, "Andrew Ballance"
 <andrewjballance@...il.com>, "Boqun Feng" <boqun.feng@...il.com>, "Gary
 Guo" <gary@...yguo.net>, Björn Roy Baron
 <bjorn3_gh@...tonmail.com>, "Benno Lossin" <lossin@...nel.org>, "Andreas
 Hindborg" <a.hindborg@...nel.org>, "Trevor Gross" <tmgross@...ch.edu>,
 <linux-kernel@...r.kernel.org>, <maple-tree@...ts.infradead.org>,
 <rust-for-linux@...r.kernel.org>, <linux-mm@...ck.org>
Subject: Re: [PATCH v3 1/3] rust: maple_tree: add MapleTree

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@...il.com>
>> > Signed-off-by: Andrew Ballance <andrewjballance@...il.com>
>> > Signed-off-by: Alice Ryhl <aliceryhl@...gle.com>
>> 
>> One nit below, otherwise:
>> 
>> 	Reviewed-by: Danilo Krummrich <dakr@...nel.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.

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ