mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peder Bergan via B4 Relay <devnull+peder.bergan.gmail.com@kernel.org>
To: Alice Ryhl <aliceryhl@google.com>,
	Carlos Llamas <cmllamas@google.com>,
	 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Arve Hjønnevåg" <arve@android.com>,
	"Todd Kjos" <tkjos@android.com>,
	"Christian Brauner" <brauner@kernel.org>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2] rust_binder: Reuse the free-index node in the range allocator
Date: Sun, 29 Nov 2026 19:49:07 +0100	[thread overview]
Message-ID: <20261129-perf-binder-range-allocator-v2-1-dfb801051c61@gmail.com> (raw)

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>



                 reply	other threads:[~2026-11-29 18:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129-perf-binder-range-allocator-v2-1-dfb801051c61@gmail.com \
    --to=devnull+peder.bergan.gmail.com@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=arve@android.com \
    --cc=brauner@kernel.org \
    --cc=cmllamas@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peder.bergan@gmail.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tkjos@android.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®