From 498e74de1c361403bd7f5055249d56f45e1b7ee0 Mon Sep 17 00:00:00 2001 From: Matt Gilbride Date: Tue, 5 Dec 2023 15:45:06 +0000 Subject: [PATCH] Red-black tree abstraction needed by Rust Binder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patchset contains the red-black tree abstractions needed by the Rust implementation of the Binder driver. Binder driver benefits from O(log n) search/insertion/deletion of key/value mappings in various places, including `process.rs` and `range_alloc.rs`. In `range_alloc.rs`, the ability to store and search by a generic key type is also useful. Please see the Rust Binder RFC for usage examples [1]. Note that the `container_of` macro is currently used only by `rbtree` itself. Users of "rust: rbtree: add red-black tree implementation backed by the C version" [PATCH RFC 03/20] rust_binder: add threading support [PATCH RFC 05/20] rust_binder: add nodes and context managers [PATCH RFC 06/20] rust_binder: add oneway transactions Users of "rust: rbtree: add iterator" [PATCH RFC 17/20] rust_binder: add oneway spam detection Users of "rust: rbtree: add mutable iterator" [PATCH RFC 06/20] rust_binder: add oneway transactions Users of "rust: rbtree: add `RBTreeCursor`" [PATCH RFC 06/20] rust_binder: add oneway transactions Users of "rust: rbtree: add RBTree::entry" Not used in the original RFC, but introduced after further code review. See: https://r.android.com/2849906 The Rust Binder RFC addresses the upstream deprecation of red-black tree. Quoted here for convenience: "This RFC uses the kernel's red-black tree for key/value mappings, but we are aware that the red-black tree is deprecated. We did this to make the performance comparison more fair, since C binder also uses rbtree for this. We intend to replace these with XArrays instead. That said, we don't think that XArray is a good fit for the range allocator, and we propose to continue using the red-black tree for the range allocator." Link: https://lore.kernel.org/rust-for-linux/20231101-rust-binder-v1-0-08ba9197f637@google.com/ [1] To: Miguel Ojeda To: Alex Gaynor To: Wedson Almeida Filho To: Boqun Feng To: Gary Guo To: Björn Roy Baron To: Benno Lossin To: Andreas Hindborg To: Alice Ryhl To: Greg Kroah-Hartman To: Arve Hjønnevåg To: Todd Kjos To: Martijn Coenen To: Joel Fernandes To: Christian Brauner To: Carlos Llamas To: Suren Baghdasaryan Cc: Rob Landley Cc: Davidlohr Bueso Cc: Michel Lespinasse Cc: rust-for-linux@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Matt Gilbride --- Changes in v10: - Rebase on top of rust-for-linux/rust-next (e26fa546042add70944d018b930530d16b3cf626) - Adapt to changes from "rust: kbuild: split up helpers.c" (876346536c1b59a5b1b5e44477b1b3ece77647fd) - Rebase on top of "rust: kernel: add `drop_contents` to `BoxExt`" instead of including it in the patch series - https://lore.kernel.org/all/CAH5fLgj2urZ6OD2ki6E=6EuPqW3Z8BGg8jd6Bgo4OOrNiptnDw@mail.gmail.com/ - Link to v9: https://lore.kernel.org/r/20240816-b4-rbtree-v9-0-425b442af7e5@google.com Changes in v9: - Rebase on top of Linux 6.11-rc2 - Address feedback from v8 - Link to v8: https://lore.kernel.org/r/20240727-b4-rbtree-v8-0-951600ada434@google.com Changes in v8: - Fix small style nit (use ? operator) - Fix doc comment pointing at a private item - Link to v7: https://lore.kernel.org/r/20240726-b4-rbtree-v7-0-aee88caaf97c@google.com Changes in v7: - make `RawVacantEntry.rbtree` a raw pointer like `RawVacantEntry.child_field_of_parent`, since the latter can technically point at a field of the former. We prefer that the implementation be explicit about the safety guarantees of both because of the relationship between them. - Link to v6: https://lore.kernel.org/r/20240711-b4-rbtree-v6-0-14bef1a8cdba@google.com Changes in v6: - Minimize usage of `*mut bindings::rb_node`, replacing with `NonNull`. Specifically, changing `RBTreeCursor.current` to be `NonNull` and updating the corresponding functions. - Update `RBTreeCursor:to_key_value` helpers to have their own lifetime (they are not instance methods, using a different lifetime than that of the `impl` block they are in makes things more clear. - Fix misplaced semicolon in `cursor_lower_bound`. - Link to v5: https://lore.kernel.org/r/20240606-b4-rbtree-v5-0-96fe1a0e97c0@google.com Changes in v5: - Used `Box::write` in `RBTreeNodeReservation::into_node`, removing unnecessary `unsafe` blocks. - Updated `RBTreeCursor::remove_current` to return the removed node. - Link to v4: https://lore.kernel.org/r/20240603-b4-rbtree-v4-0-308e43d6abfc@google.com Changes in v4: - rebased onto the tip of rust-for-linux/rust-next (97ab3e8eec0ce79d9e265e6c9e4c480492180409) - addressed comments from draft PR on GitHub: https://github.com/Rust-for-Linux/linux/pull/1081 - Link to v3: https://lore.kernel.org/r/20240418-b4-rbtree-v3-0-323e134390ce@google.com Changes in v3: - Address various feedback re: SAFETY and INVARIANT comments from v2. - Update variable naming and add detailed comments for the `RBTree::insert` (later moved to `RBTree::raw_entry`) implementation. - Link to v2: https://lore.kernel.org/r/20240219-b4-rbtree-v2-0-0b113aab330d@google.com Changes in v2: - Update documentation link to the C header file - Use `core::convert::Infallible` in try_reserve_node - Link to v1: https://lore.kernel.org/r/20240205-b4-rbtree-v1-0-995e3eee38c0@google.com --- b4-submit-tracking --- # This section is used internally by b4 prep for tracking purposes. { "series": { "revision": 10, "change-id": "20231205-b4-rbtree-abb1a016f0a0", "prefixes": [], "history": { "v1": [ "20240205-b4-rbtree-v1-0-995e3eee38c0@google.com" ], "v2": [ "20240219-b4-rbtree-v2-0-0b113aab330d@google.com" ], "v3": [ "20240418-b4-rbtree-v3-0-323e134390ce@google.com" ], "v4": [ "20240603-b4-rbtree-v4-0-308e43d6abfc@google.com" ], "v5": [ "20240606-b4-rbtree-v5-0-96fe1a0e97c0@google.com" ], "v6": [ "20240711-b4-rbtree-v6-0-14bef1a8cdba@google.com" ], "v7": [ "20240726-b4-rbtree-v7-0-aee88caaf97c@google.com" ], "v8": [ "20240727-b4-rbtree-v8-0-951600ada434@google.com" ], "v9": [ "20240816-b4-rbtree-v9-0-425b442af7e5@google.com" ] } } }