Skip to content

fine-grained locking in binder driver

Todd Kjos lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest37 patches 20 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/37Revert "android: binder: Sanity check at binder ioctl"drivers/android/binder.c+0 -52 replies
2/37binder: use group leader instead of open threaddrivers/android/binder.c+2 -214 replies
3/37binder: Use wake up hint for synchronous transactions.drivers/android/binder.c+6 -21 reply
4/37binder: separate binder allocator structure from binder procdrivers/android/binder.c drivers/android/binder_trace.h+129 -850 replies
5/37binder: remove unneeded cleanup codedrivers/android/binder.c+2 -90 replies
6/37binder: separate out binder_alloc functionsdrivers/android/binder.c drivers/android/binder_trace.h+410 -2480 replies
7/37binder: move binder_alloc to separate filedrivers/android/Makefile drivers/android/binder.c drivers/android/binder_alloc.c +1 more+923 -7630 replies
8/37binder: remove binder_debug_no_lock mechanismdrivers/android/binder.c+8 -230 replies
9/37binder: add protection for non-perf casesdrivers/android/binder.c+63 -180 replies
10/37binder: change binder_stats to atomicsdrivers/android/binder.c+28 -200 replies
11/37binder: make binder_last_id an atomicdrivers/android/binder.c+4 -40 replies
12/37binder: add log information for binder transaction failuresdrivers/android/binder.c drivers/android/binder_alloc.c+88 -190 replies
13/37binder: refactor queue management in binder_thread_readdrivers/android/binder.c+94 -570 replies
14/37binder: avoid race conditions when enqueuing txndrivers/android/binder.c+4 -30 replies
15/37binder: don't modify thread->looper from other threadsdrivers/android/binder.c+11 -110 replies
16/37binder: remove dead code in binder_get_ref_for_nodedrivers/android/binder.c+5 -110 replies
17/37binder: protect against two threads freeing bufferdrivers/android/binder.c drivers/android/binder_alloc.c drivers/android/binder_alloc.h+23 -100 replies
18/37binder: add more debug info when allocation fails.drivers/android/binder_alloc.c+30 -10 replies
19/37binder: use atomic for transaction_log indexdrivers/android/binder.c+59 -160 replies
20/37binder: refactor binder_pop_transactiondrivers/android/binder.c+15 -150 replies
21/37binder: guarantee txn complete / errors delivered in-orderdrivers/android/binder.c+73 -540 replies
22/37binder: make sure target_node has strong refdrivers/android/binder.c+17 -50 replies
23/37binder: make sure accesses to proc/thread are safedrivers/android/binder.c+206 -270 replies
24/37binder: refactor binder ref inc/dec for thread safetydrivers/android/binder.c drivers/android/binder_trace.h+379 -1370 replies
25/37binder: use node->tmp_refs to ensure node safetydrivers/android/binder.c+104 -200 replies
26/37binder: introduce locking helper functionsdrivers/android/binder.c+238 -00 replies
27/37binder: use inner lock to sync work dq and node countsdrivers/android/binder.c+198 -510 replies
28/37binder: add spinlocks to protect todo listsdrivers/android/binder.c+269 -860 replies
29/37binder: add spinlock to protect binder_nodedrivers/android/binder.c+165 -550 replies
30/37binder: protect proc->nodes with inner lockdrivers/android/binder.c+89 -230 replies
31/37binder: protect proc->threads with inner_lockdrivers/android/binder.c+63 -240 replies
32/37binder: protect transaction_stack with inner lock.drivers/android/binder.c+79 -170 replies
33/37binder: use inner lock to protect thread accountingdrivers/android/binder.c+23 -50 replies
34/37binder: protect binder_ref with outer lockdrivers/android/binder.c+83 -500 replies
35/37binder: protect against stale pointers in print_binder_transactiondrivers/android/binder.c+40 -200 replies
36/37binder: fix death race conditionsdrivers/android/binder.c+32 -321 reply
37/37binder: remove global binder lockdrivers/android/binder.c+3 -430 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 00/37] fine-grained locking in binder driver

The binder driver uses a global mutex to serialize access to state in a multi-threaded environment. This global lock has been increasingly problematic as Android devices have scaled to more cores. The problem is not so much contention for the global lock which still remains relatively low, but the priority inversion which occurs regularly when a lower priority thread is preempted while holding the lock and a higher priority thread becomes blocked on it. These cases can be especially painful if the lower priority thread runs in the background on a slow core at a low frequency. This often manifests as missed frames or other glitches.

For several years, a hacky solution has been used in many Android devices which disables preemption for most of the time the global mutex is held. This dramatically decreased the cases of glitches induced by priority inversion and increased the average throughput for binder transactions.

Moving to fine-grained locking in this patchset results is a cleaner and more scalable solution than the preempt disable hack. Priority inversion is decreased significantly.

Here is a comparison of the binder throughputs for the 3 cases with no payload (using binderThroughputTest on a 4-core Pixel device):

1 Client/Server Pair (iterations/s):
Global Mutex: 4267
+ No-Preempt: 69688
Fine-Grained: 52313

2 Client/Server Pairs (iterations/s):
Global Mutex: 5608
+ No-Preempt: 111346
Fine-Grained: 117039

4 Client/Server Pairs (iterations/s):
Global Mutex: 12839
+ No-Preempt: 118049
Fine-Grained: 189805

8 Client/Server Pairs (iterations/s):
Global Mutex: 12991
+ No-Preempt: 111780
Fine-Grained: 203607

16 Client/Server Pairs (iterations/s):
Global Mutex: 14467
+ No-Preempt: 106763
Fine-Grained: 202942

Note that global lock performance without preempt disable seems to perform significantly worse on Pixel than on some other devices. This run used the 4.4 version of the binder driver that is currently upstream (and there have been few lines changed since then which wouldn't explain the poor performance).

The no-preempt version has better throughput in the single threaded case where the new locking overhead adds to the transacton latency. However with multiple concurent transactions, the lack of contention results in better throughput for the fine-grained case.

In the patchset, the binder allocator is moved to a separate file and protected with its own per-process mutex.

Most of the binder driver is now protected by 3 spinlocks which must be acquired in the order shown:
1) proc->outer_lock : protects binder_ref binder_proc_lock() and binder_proc_unlock() are used to acq/rel.
2) node->lock : protects most fields of binder_node. binder_node_lock() and binder_node_unlock() are used to acq/rel
3) proc->inner_lock : protects the thread and node lists (proc->threads, proc->waiting_threads, proc->nodes) and all todo lists associated with the binder_proc (proc->todo, thread->todo, proc->delivered_death and node->async_todo), as well as thread->transaction_stack binder_inner_proc_lock() and binder_inner_proc_unlock() are used to acq/rel

Any lock under procA must never be nested under any lock at the same level or below on procB.

There was significant refactoring needed to implement the locking so there are 37 patches in the set.

Here are the patches grouped into 4 categories:

1) bugfixes: 3 patches to fix behavior and are
needed for fine-grained locking implementation
Revert "binder: Sanity check at binder ioctl"
- note: introduces kernel race to fix userspace bug. An attempt to fix this was submitted in
"[PATCH v2] android: binder: fix dangling pointer comparison" however that discussion concluded that this patch should be reverted and the problem fixed in userspace. Doing the revert now since this patch conflicts with some of the fine-grained locking patches.
binder: use group leader instead of open thread
binder: Use wake up hint for synchronous transactions.

2) Separate binder allocator into a separate file from binder driver
binder: separate binder allocator structure from binder proc
binder: remove unneeded cleanup code
binder: separate out binder_alloc functions
binder: move binder_alloc to separate file

3) Refactor binder driver to support locking
binder: remove binder_debug_no_lock mechanism
binder: add protection for non-perf cases
binder: change binder_stats to atomics
binder: make binder_last_id an atomic
binder: add log information for binder transaction failures
binder: refactor queue management in binder_thread_read
binder: avoid race conditions when enqueuing txn
binder: don't modify thread->looper from other threads
binder: remove dead code in binder_get_ref_for_node
binder: protect against two threads freeing buffer
binder: add more debug info when allocation fails.
binder: use atomic for transaction_log index
binder: refactor binder_pop_transaction
binder: guarantee txn complete / errors delivered in-order
binder: make sure target_node has strong ref
binder: make sure accesses to proc/thread are safe
binder: refactor binder ref inc/dec for thread safety
binder: use node->tmp_refs to ensure node safety

4) Add the locks and remove the global lock
binder: introduce locking helper functions
binder: use inner lock to sync work dq and node counts
binder: add spinlocks to protect todo lists
binder: add spinlock to protect binder_node
binder: protect proc->nodes with inner lock
binder: protect proc->threads with inner_lock
binder: protect transaction_stack with inner lock.
binder: use inner lock to protect thread accounting
binder: protect binder_ref with outer lock
binder: protect against stale pointers in print_binder_transaction
binder: fix death race conditions
binder: remove global binder lock

drivers/android/Makefile       |    2 +-
drivers/android/binder.c       | 3467 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------
drivers/android/binder_alloc.c |  802 ++++++++++++++++++++++++++++++++
drivers/android/binder_alloc.h |  163 +++++++
drivers/android/binder_trace.h |   41 +-
5 files changed, 3235 insertions(+), 1240 deletions(-)