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] rust_binder: Reuse the free-index node in the range allocator
Date: Thu, 26 Nov 2026 19:06:03 +0100 [thread overview]
Message-ID: <20261126-perf-binder-range-allocator-v1-1-7d924585d735@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.
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.
Signed-off-by: Peder Bergan <peder.bergan@gmail.com>
---
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(-)
diff --git a/drivers/android/binder/range_alloc/tree.rs b/drivers/android/binder/range_alloc/tree.rs
index f0cd46f223d56..5a225d9aa63ad 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 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,
--
Peder Bergan <peder.bergan@gmail.com>
reply other threads:[~2026-11-26 18:06 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=20261126-perf-binder-range-allocator-v1-1-7d924585d735@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®