Skip to content

Commit 15d9da3

Browse files
Carlos Llamasgregkh
authored andcommitted
binder: use bitmap for faster descriptor lookup
When creating new binder references, the driver assigns a descriptor id that is shared with userspace. Regrettably, the driver needs to keep the descriptors small enough to accommodate userspace potentially using them as Vector indexes. Currently, the driver performs a linear search on the rb-tree of references to find the smallest available descriptor id. This approach, however, scales poorly as the number of references grows. This patch introduces the usage of bitmaps to boost the performance of descriptor assignments. This optimization results in notable performance gains, particularly in processes with a large number of references. The following benchmark with 100,000 references showcases the difference in latency between the dbitmap implementation and the legacy approach: [ 587.145098] get_ref_desc_olocked: 15us (dbitmap on) [ 602.788623] get_ref_desc_olocked: 47343us (dbitmap off) Note the bitmap size is dynamically adjusted in line with the number of references, ensuring efficient memory usage. In cases where growing the bitmap is not possible, the driver falls back to the slow legacy method. A previous attempt to solve this issue was proposed in [1]. However, such method involved adding new ioctls which isn't great, plus older userspace code would not have benefited from the optimizations either. Link: https://lore.kernel.org/all/[email protected]/ [1] Cc: Tim Murray <[email protected]> Cc: Arve Hjønnevåg <[email protected]> Cc: Alice Ryhl <[email protected]> Cc: Martijn Coenen <[email protected]> Cc: Todd Kjos <[email protected]> Cc: John Stultz <[email protected]> Cc: Steven Moreland <[email protected]> Suggested-by: Nick Chen <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Signed-off-by: Carlos Llamas <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 7269d76 commit 15d9da3

File tree

3 files changed

+279
-14
lines changed

3 files changed

+279
-14
lines changed

drivers/android/binder.c

Lines changed: 99 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,66 @@ static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
10451045
return NULL;
10461046
}
10471047

1048+
/* Find the smallest unused descriptor the "slow way" */
1049+
static u32 slow_desc_lookup_olocked(struct binder_proc *proc)
1050+
{
1051+
struct binder_ref *ref;
1052+
struct rb_node *n;
1053+
u32 desc;
1054+
1055+
desc = 1;
1056+
for (n = rb_first(&proc->refs_by_desc); n; n = rb_next(n)) {
1057+
ref = rb_entry(n, struct binder_ref, rb_node_desc);
1058+
if (ref->data.desc > desc)
1059+
break;
1060+
desc = ref->data.desc + 1;
1061+
}
1062+
1063+
return desc;
1064+
}
1065+
1066+
/*
1067+
* Find an available reference descriptor ID. The proc->outer_lock might
1068+
* be released in the process, in which case -EAGAIN is returned and the
1069+
* @desc should be considered invalid.
1070+
*/
1071+
static int get_ref_desc_olocked(struct binder_proc *proc,
1072+
struct binder_node *node,
1073+
u32 *desc)
1074+
{
1075+
struct dbitmap *dmap = &proc->dmap;
1076+
unsigned long *new, bit;
1077+
unsigned int nbits;
1078+
1079+
/* 0 is reserved for the context manager */
1080+
if (node == proc->context->binder_context_mgr_node) {
1081+
*desc = 0;
1082+
return 0;
1083+
}
1084+
1085+
if (!dbitmap_enabled(dmap)) {
1086+
*desc = slow_desc_lookup_olocked(proc);
1087+
return 0;
1088+
}
1089+
1090+
if (dbitmap_acquire_first_zero_bit(dmap, &bit) == 0) {
1091+
*desc = bit;
1092+
return 0;
1093+
}
1094+
1095+
/*
1096+
* The dbitmap is full and needs to grow. The proc->outer_lock
1097+
* is briefly released to allocate the new bitmap safely.
1098+
*/
1099+
nbits = dbitmap_grow_nbits(dmap);
1100+
binder_proc_unlock(proc);
1101+
new = bitmap_zalloc(nbits, GFP_KERNEL);
1102+
binder_proc_lock(proc);
1103+
dbitmap_grow(dmap, new, nbits);
1104+
1105+
return -EAGAIN;
1106+
}
1107+
10481108
/**
10491109
* binder_get_ref_for_node_olocked() - get the ref associated with given node
10501110
* @proc: binder_proc that owns the ref
@@ -1068,12 +1128,14 @@ static struct binder_ref *binder_get_ref_for_node_olocked(
10681128
struct binder_node *node,
10691129
struct binder_ref *new_ref)
10701130
{
1071-
struct binder_context *context = proc->context;
1072-
struct rb_node **p = &proc->refs_by_node.rb_node;
1073-
struct rb_node *parent = NULL;
10741131
struct binder_ref *ref;
1075-
struct rb_node *n;
1132+
struct rb_node *parent;
1133+
struct rb_node **p;
1134+
u32 desc;
10761135

1136+
retry:
1137+
p = &proc->refs_by_node.rb_node;
1138+
parent = NULL;
10771139
while (*p) {
10781140
parent = *p;
10791141
ref = rb_entry(parent, struct binder_ref, rb_node_node);
@@ -1088,21 +1150,18 @@ static struct binder_ref *binder_get_ref_for_node_olocked(
10881150
if (!new_ref)
10891151
return NULL;
10901152

1153+
/* might release the proc->outer_lock */
1154+
if (get_ref_desc_olocked(proc, node, &desc) == -EAGAIN)
1155+
goto retry;
1156+
10911157
binder_stats_created(BINDER_STAT_REF);
10921158
new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
10931159
new_ref->proc = proc;
10941160
new_ref->node = node;
10951161
rb_link_node(&new_ref->rb_node_node, parent, p);
10961162
rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
10971163

1098-
new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
1099-
for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1100-
ref = rb_entry(n, struct binder_ref, rb_node_desc);
1101-
if (ref->data.desc > new_ref->data.desc)
1102-
break;
1103-
new_ref->data.desc = ref->data.desc + 1;
1104-
}
1105-
1164+
new_ref->data.desc = desc;
11061165
p = &proc->refs_by_desc.rb_node;
11071166
while (*p) {
11081167
parent = *p;
@@ -1131,13 +1190,16 @@ static struct binder_ref *binder_get_ref_for_node_olocked(
11311190

11321191
static void binder_cleanup_ref_olocked(struct binder_ref *ref)
11331192
{
1193+
struct dbitmap *dmap = &ref->proc->dmap;
11341194
bool delete_node = false;
11351195

11361196
binder_debug(BINDER_DEBUG_INTERNAL_REFS,
11371197
"%d delete ref %d desc %d for node %d\n",
11381198
ref->proc->pid, ref->data.debug_id, ref->data.desc,
11391199
ref->node->debug_id);
11401200

1201+
if (dbitmap_enabled(dmap))
1202+
dbitmap_clear_bit(dmap, ref->data.desc);
11411203
rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
11421204
rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
11431205

@@ -1298,6 +1360,25 @@ static void binder_free_ref(struct binder_ref *ref)
12981360
kfree(ref);
12991361
}
13001362

1363+
/* shrink descriptor bitmap if needed */
1364+
static void try_shrink_dmap(struct binder_proc *proc)
1365+
{
1366+
unsigned long *new;
1367+
int nbits;
1368+
1369+
binder_proc_lock(proc);
1370+
nbits = dbitmap_shrink_nbits(&proc->dmap);
1371+
binder_proc_unlock(proc);
1372+
1373+
if (!nbits)
1374+
return;
1375+
1376+
new = bitmap_zalloc(nbits, GFP_KERNEL);
1377+
binder_proc_lock(proc);
1378+
dbitmap_shrink(&proc->dmap, new, nbits);
1379+
binder_proc_unlock(proc);
1380+
}
1381+
13011382
/**
13021383
* binder_update_ref_for_handle() - inc/dec the ref for given handle
13031384
* @proc: proc containing the ref
@@ -1334,8 +1415,10 @@ static int binder_update_ref_for_handle(struct binder_proc *proc,
13341415
*rdata = ref->data;
13351416
binder_proc_unlock(proc);
13361417

1337-
if (delete_ref)
1418+
if (delete_ref) {
13381419
binder_free_ref(ref);
1420+
try_shrink_dmap(proc);
1421+
}
13391422
return ret;
13401423

13411424
err_no_ref:
@@ -4931,6 +5014,7 @@ static void binder_free_proc(struct binder_proc *proc)
49315014
put_task_struct(proc->tsk);
49325015
put_cred(proc->cred);
49335016
binder_stats_deleted(BINDER_STAT_PROC);
5017+
dbitmap_free(&proc->dmap);
49345018
kfree(proc);
49355019
}
49365020

@@ -5634,6 +5718,8 @@ static int binder_open(struct inode *nodp, struct file *filp)
56345718
proc = kzalloc(sizeof(*proc), GFP_KERNEL);
56355719
if (proc == NULL)
56365720
return -ENOMEM;
5721+
5722+
dbitmap_init(&proc->dmap);
56375723
spin_lock_init(&proc->inner_lock);
56385724
spin_lock_init(&proc->outer_lock);
56395725
get_task_struct(current->group_leader);

drivers/android/binder_internal.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/uidgid.h>
1515
#include <uapi/linux/android/binderfs.h>
1616
#include "binder_alloc.h"
17+
#include "dbitmap.h"
1718

1819
struct binder_context {
1920
struct binder_node *binder_context_mgr_node;
@@ -368,6 +369,8 @@ struct binder_ref {
368369
* @freeze_wait: waitqueue of processes waiting for all outstanding
369370
* transactions to be processed
370371
* (protected by @inner_lock)
372+
* @dmap dbitmap to manage available reference descriptors
373+
* (protected by @outer_lock)
371374
* @todo: list of work for this process
372375
* (protected by @inner_lock)
373376
* @stats: per-process binder statistics
@@ -417,7 +420,7 @@ struct binder_proc {
417420
bool sync_recv;
418421
bool async_recv;
419422
wait_queue_head_t freeze_wait;
420-
423+
struct dbitmap dmap;
421424
struct list_head todo;
422425
struct binder_stats stats;
423426
struct list_head delivered_death;

drivers/android/dbitmap.h

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright 2024 Google LLC
4+
*
5+
* dbitmap - dynamically sized bitmap library.
6+
*
7+
* Used by the binder driver to optimize the allocation of the smallest
8+
* available descriptor ID. Each bit in the bitmap represents the state
9+
* of an ID, with the exception of BIT(0) which is used exclusively to
10+
* reference binder's context manager.
11+
*
12+
* A dbitmap can grow or shrink as needed. This part has been designed
13+
* considering that users might need to briefly release their locks in
14+
* order to allocate memory for the new bitmap. These operations then,
15+
* are verified to determine if the grow or shrink is sill valid.
16+
*
17+
* This library does not provide protection against concurrent access
18+
* by itself. Binder uses the proc->outer_lock for this purpose.
19+
*/
20+
21+
#ifndef _LINUX_DBITMAP_H
22+
#define _LINUX_DBITMAP_H
23+
#include <linux/bitmap.h>
24+
25+
#define NBITS_MIN BITS_PER_TYPE(unsigned long)
26+
27+
struct dbitmap {
28+
unsigned int nbits;
29+
unsigned long *map;
30+
};
31+
32+
static inline int dbitmap_enabled(struct dbitmap *dmap)
33+
{
34+
return !!dmap->nbits;
35+
}
36+
37+
static inline void dbitmap_free(struct dbitmap *dmap)
38+
{
39+
dmap->nbits = 0;
40+
kfree(dmap->map);
41+
}
42+
43+
/* Returns the nbits that a dbitmap can shrink to, 0 if not possible. */
44+
static inline unsigned int dbitmap_shrink_nbits(struct dbitmap *dmap)
45+
{
46+
unsigned int bit;
47+
48+
if (dmap->nbits <= NBITS_MIN)
49+
return 0;
50+
51+
/*
52+
* Determine if the bitmap can shrink based on the position of
53+
* its last set bit. If the bit is within the first quarter of
54+
* the bitmap then shrinking is possible. In this case, the
55+
* bitmap should shrink to half its current size.
56+
*/
57+
bit = find_last_bit(dmap->map, dmap->nbits);
58+
if (bit < (dmap->nbits >> 2))
59+
return dmap->nbits >> 1;
60+
61+
/*
62+
* Note that find_last_bit() returns dmap->nbits when no bits
63+
* are set. While this is technically not possible here since
64+
* BIT(0) is always set, this check is left for extra safety.
65+
*/
66+
if (bit == dmap->nbits)
67+
return NBITS_MIN;
68+
69+
return 0;
70+
}
71+
72+
/* Replace the internal bitmap with a new one of different size */
73+
static inline void
74+
dbitmap_replace(struct dbitmap *dmap, unsigned long *new, unsigned int nbits)
75+
{
76+
bitmap_copy(new, dmap->map, min(dmap->nbits, nbits));
77+
kfree(dmap->map);
78+
dmap->map = new;
79+
dmap->nbits = nbits;
80+
}
81+
82+
static inline void
83+
dbitmap_shrink(struct dbitmap *dmap, unsigned long *new, unsigned int nbits)
84+
{
85+
if (!new)
86+
return;
87+
88+
/*
89+
* Verify that shrinking to @nbits is still possible. The @new
90+
* bitmap might have been allocated without locks, so this call
91+
* could now be outdated. In this case, free @new and move on.
92+
*/
93+
if (!dbitmap_enabled(dmap) || dbitmap_shrink_nbits(dmap) != nbits) {
94+
kfree(new);
95+
return;
96+
}
97+
98+
dbitmap_replace(dmap, new, nbits);
99+
}
100+
101+
/* Returns the nbits that a dbitmap can grow to. */
102+
static inline unsigned int dbitmap_grow_nbits(struct dbitmap *dmap)
103+
{
104+
return dmap->nbits << 1;
105+
}
106+
107+
static inline void
108+
dbitmap_grow(struct dbitmap *dmap, unsigned long *new, unsigned int nbits)
109+
{
110+
/*
111+
* Verify that growing to @nbits is still possible. The @new
112+
* bitmap might have been allocated without locks, so this call
113+
* could now be outdated. In this case, free @new and move on.
114+
*/
115+
if (!dbitmap_enabled(dmap) || nbits <= dmap->nbits) {
116+
kfree(new);
117+
return;
118+
}
119+
120+
/*
121+
* Check for ENOMEM after confirming the grow operation is still
122+
* required. This ensures we only disable the dbitmap when it's
123+
* necessary. Once the dbitmap is disabled, binder will fallback
124+
* to slow_desc_lookup_olocked().
125+
*/
126+
if (!new) {
127+
dbitmap_free(dmap);
128+
return;
129+
}
130+
131+
dbitmap_replace(dmap, new, nbits);
132+
}
133+
134+
/*
135+
* Finds and sets the first zero bit in the bitmap. Upon success @bit
136+
* is populated with the index and 0 is returned. Otherwise, -ENOSPC
137+
* is returned to indicate that a dbitmap_grow() is needed.
138+
*/
139+
static inline int
140+
dbitmap_acquire_first_zero_bit(struct dbitmap *dmap, unsigned long *bit)
141+
{
142+
unsigned long n;
143+
144+
n = find_first_zero_bit(dmap->map, dmap->nbits);
145+
if (n == dmap->nbits)
146+
return -ENOSPC;
147+
148+
*bit = n;
149+
set_bit(n, dmap->map);
150+
151+
return 0;
152+
}
153+
154+
static inline void
155+
dbitmap_clear_bit(struct dbitmap *dmap, unsigned long bit)
156+
{
157+
/* BIT(0) should always set for the context manager */
158+
if (bit)
159+
clear_bit(bit, dmap->map);
160+
}
161+
162+
static inline int dbitmap_init(struct dbitmap *dmap)
163+
{
164+
dmap->map = bitmap_zalloc(NBITS_MIN, GFP_KERNEL);
165+
if (!dmap->map) {
166+
dmap->nbits = 0;
167+
return -ENOMEM;
168+
}
169+
170+
dmap->nbits = NBITS_MIN;
171+
/* BIT(0) is reserved for the context manager */
172+
set_bit(0, dmap->map);
173+
174+
return 0;
175+
}
176+
#endif

0 commit comments

Comments
 (0)