rust_binder: Reuse the free-index node in the range allocator
Subsystems (2)
PATCH 1/1 rust_binder: Reuse the free-index node in the range allocator
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.
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.
---
I compared the disassembly of the original and patched kernels. I traced queued oneway transactions in x86-64 KVM guests. The traces confirmed node reuse in both exact-fit and split reservations. Local KUnit tests covered reservation and release, coalescing with both neighbors, quota and no-fit rejection, and promotion and demotion.
OpenAI Codex assisted with coding and testing.
--- drivers/android/binder/range_alloc/tree.rs | 55 +++++++++++++----------------- 1 file changed, 23 insertions(+), 32 deletions(-)
drivers/android/binder/range_alloc/tree.rs +23 -32
@@ -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 desc = self + .free_tree + .cursor_lower_bound (&(size,0 )) + .and_then (|cursor| { +let ((_, offset), ()) = cursor.current (); + self.tree.get_mut (offset) + }) + .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 = self + .free_tree + .remove_node (&(found_size, found_off)) + .ok_or (EINVAL )? + .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,18 +426,15 @@ 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, }) }
@@ -451,14 +444,12 @@ fn initialize( ) -> (RBTreeNode <usize ,Descriptor <T >>,RBTreeNode <FreeKey , ()>, -FreeNodeRes , ) {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,
Signature · 1 line
Peder Bergan <[email protected]>