* [PATCH v2] rust_binder: Reuse the free-index node in the range allocator
@ 2026-11-29 18:49 Peder Bergan via B4 Relay
0 siblings, 0 replies; only message in thread
From: Peder Bergan via B4 Relay @ 2026-11-29 18:49 UTC (permalink / raw)
To: Alice Ryhl, Carlos Llamas, Greg Kroah-Hartman
Cc: Arve Hjønnevåg, Todd Kjos, Christian Brauner,
rust-for-linux, linux-kernel
From: Peder Bergan <peder.bergan@gmail.com>
A tree reservation prepares a spare free-index node so releasing the
buffer can reinsert the free range without allocating under the
process spinlock. When the allocator selects a range, it removes and
frees that range's existing free-index node.
Keep a mutable cursor at the selected free-index node and remove the
node through that cursor after finding the corresponding descriptor.
This avoids searching the free tree again to remove the same node.
Convert the detached node into an RBTreeNodeReservation and store it
with the buffer. The release path can initialize the key after
coalescing the range. This reduces preparation from three fresh node
allocations to two and avoids one free under the process spinlock
per successful tree reservation, for both exact fits and splits.
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Peder Bergan <peder.bergan@gmail.com>
---
Changes in v2:
- Run rustfmt on drivers/android/binder/range_alloc/tree.rs.
- Remove the selected free-index node through the mutable cursor (Alice).
v1: https://lore.kernel.org/rust-for-linux/20261126-perf-binder-range-allocator-v1-1-7d924585d735@gmail.com/
OpenAI Codex assisted with coding and testing.
---
drivers/android/binder/range_alloc/tree.rs | 60 ++++++++++++------------------
1 file changed, 24 insertions(+), 36 deletions(-)
diff --git a/drivers/android/binder/range_alloc/tree.rs b/drivers/android/binder/range_alloc/tree.rs
index f0cd46f223d56..ad2b42811a560 100644
--- a/drivers/android/binder/range_alloc/tree.rs
+++ b/drivers/android/binder/range_alloc/tree.rs
@@ -139,12 +139,6 @@ pub(crate) fn debug_print(&self, m: &SeqFile) -> Result {
Ok(())
}
- fn find_best_match(&mut self, size: usize) -> Option<&mut Descriptor<T>> {
- let free_cursor = self.free_tree.cursor_lower_bound(&(size, 0))?;
- let ((_, offset), ()) = free_cursor.current();
- self.tree.get_mut(offset)
- }
-
/// Try to reserve a new buffer, using the provided allocation if necessary.
pub(crate) fn reserve_new(
&mut self,
@@ -164,32 +158,34 @@ pub(crate) fn reserve_new(
self.free_oneway_space
};
- let (found_size, found_off, tree_node, free_tree_node) = match self.find_best_match(size) {
- None => {
+ let (cursor, desc) = self
+ .free_tree
+ .cursor_lower_bound_mut(&(size, 0))
+ .and_then(|cursor| {
+ let ((_, offset), ()) = cursor.current();
+ let desc = self.tree.get_mut(offset)?;
+ Some((cursor, desc))
+ })
+ .ok_or_else(|| {
pr_warn_ratelimited!("ENOSPC from range_alloc.reserve_new - size: {}\n", size);
- return Err(ENOSPC);
- }
- Some(desc) => {
- let found_size = desc.size;
- let found_offset = desc.offset;
+ ENOSPC
+ })?;
+ let found_size = desc.size;
+ let found_off = desc.offset;
- // In case we need to break up the descriptor
- let new_desc = Descriptor::new(found_offset + size, found_size - size);
- let (tree_node, free_tree_node, desc_node_res) = alloc.initialize(new_desc);
-
- desc.state = Some((
- DescriptorState::new(is_oneway, debug_id, pid),
- desc_node_res,
- ));
- desc.size = size;
-
- (found_size, found_offset, tree_node, free_tree_node)
- }
- };
+ // Keep the removed index node so releasing this buffer can restore a free range
+ // without allocating under the lock.
+ let free_node_res = cursor.remove_current().1.into_reservation();
+ desc.state = Some((
+ DescriptorState::new(is_oneway, debug_id, pid),
+ free_node_res,
+ ));
+ desc.size = size;
self.free_oneway_space = new_oneway_space;
- self.free_tree.remove(&(found_size, found_off));
if found_size != size {
+ let remainder = Descriptor::new(found_off + size, found_size - size);
+ let (tree_node, free_tree_node) = alloc.initialize(remainder);
self.tree.insert(tree_node);
self.free_tree.insert(free_tree_node);
}
@@ -430,35 +426,27 @@ fn try_change_state<F, Data>(&mut self, f: F) -> Result<Data>
pub(crate) struct ReserveNewTreeAlloc<T> {
tree_node_res: RBTreeNodeReservation<usize, Descriptor<T>>,
free_tree_node_res: FreeNodeRes,
- desc_node_res: FreeNodeRes,
}
impl<T> ReserveNewTreeAlloc<T> {
pub(crate) fn try_new() -> Result<Self> {
let tree_node_res = RBTreeNodeReservation::new(GFP_KERNEL)?;
let free_tree_node_res = RBTreeNodeReservation::new(GFP_KERNEL)?;
- let desc_node_res = RBTreeNodeReservation::new(GFP_KERNEL)?;
Ok(Self {
tree_node_res,
free_tree_node_res,
- desc_node_res,
})
}
fn initialize(
self,
desc: Descriptor<T>,
- ) -> (
- RBTreeNode<usize, Descriptor<T>>,
- RBTreeNode<FreeKey, ()>,
- FreeNodeRes,
- ) {
+ ) -> (RBTreeNode<usize, Descriptor<T>>, RBTreeNode<FreeKey, ()>) {
let size = desc.size;
let offset = desc.offset;
(
self.tree_node_res.into_node(offset, desc),
self.free_tree_node_res.into_node((size, offset), ()),
- self.desc_node_res,
)
}
}
---
base-commit: 917fcbf033154a6c8d83cfa6c88f012b71eb37b2
change-id: 20261126-perf-binder-range-allocator-f8339da7d4d6
Best regards,
--
Peder Bergan <peder.bergan@gmail.com>
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-29 18:49 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-29 18:49 [PATCH v2] rust_binder: Reuse the free-index node in the range allocator Peder Bergan via B4 Relay
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®