Re: [PATCH 2/6] rust: rbtree: add red-black tree implementation backed by the C version

From: Alice Ryhl
Date: Fri Feb 09 2024 - 08:04:43 EST


> From: Wedson Almeida Filho <wedsonaf@xxxxxxxxx>
>
> The rust rbtree exposes a map-like interface over keys and values,
> backed by the kernel red-black tree implementation. Values can be
> inserted, deleted, and retrieved from a `RBTree` by key.
>
> This base abstraction is used by binder to store key/value
> pairs and perform lookups, for example the patch
> "[PATCH RFC 03/20] rust_binder: add threading support"
> in the binder RFC [1].
>
> Link: https://lore.kernel.org/rust-for-linux/20231101-rust-binder-v1-3-08ba9197f637@xxxxxxxxxx/ [1]
> Signed-off-by: Wedson Almeida Filho <wedsonaf@xxxxxxxxx>
> Signed-off-by: Matt Gilbride <mattgilbride@xxxxxxxxxx>

I have looked at these bindings many times over the past year. They
look good to me, modulo a few nits included in this email.

Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>

These abstractions have been very heavily exercised by the Rust Binder
driver.

Tested-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>

> +//! Red-black trees.
> +//!
> +//! C header: [`include/linux/rbtree.h`](../../../../include/linux/rbtree.h)
> +//!
> +//! Reference: <https://www.kernel.org/doc/html/latest/core-api/rbtree.html>

We changed these links, so it should say this instead:

[`include/linux/rbtree.h`](srctree/include/linux/rbtree.h)

> + /// Allocates memory for a node to be eventually initialised and inserted into the tree via a
> + /// call to [`RBTree::insert`].
> + pub fn try_reserve_node() -> Result<RBTreeNodeReservation<K, V>> {
> + Ok(RBTreeNodeReservation {
> + node: Box::try_new(MaybeUninit::uninit())?,
> + })
> + }

This can be:

use crate::prelude::*;
use core::convert::Infallible;

Ok(RBTreeNodeReservation {
node: Box::init::<Infallible>(crate::init::uninit())?,
})

It might be slightly more verbose, but guarantees that we don't allocate
space for the value on the stack.

Alice