14 min read
Kernel DevelopmentJune 20, 2026

The Binder Tax: What Android's IPC Actually Does to Your Kernel

A deep dive into Android's Binder IPC driver: what it actually does at the kernel level, why it hurts when you're porting, and what we changed in Nova-Kernels to stop hwservicemanager from dying at 3am.

android · binder · IPC · kernel · nova-kernels · begonia · VIENNA · drivers

The Binder Tax: What Android's IPC Actually Does to Your Kernel

If you've ever ported a custom Android kernel and watched hwservicemanager die silently, or had the camera HAL refuse to open with no useful log output, Binder probably ate your afternoon. This post is a proper technical look at what Binder does inside the kernel, why it's the kind of thing that looks simple from the outside and is genuinely not, and what specific changes we made in Nova-Kernels to stop it from being a problem.


What Binder Actually Is

The marketing version: "Android's high-performance IPC mechanism." The accurate version: a kernel driver that provides shared-memory IPC with object passing, reference counting, death notifications, and thread pool management, all crammed into a single character device.

Binder did not originate at Google. It was written by Dianne Hackborn for Be, Inc.'s BeOS in the late 90s, where it was called the OpenBinder system. Google acquired the codebase as part of the Android acquisition in 2005, rewrote the kernel driver in C, and it has lived at drivers/android/binder.c ever since. The current mainline driver is around 10,000 lines.

There are three Binder domains, each a separate device node:

DevicePurpose
/dev/binderApp-to-framework and app-to-app IPC
/dev/hwbinderFramework-to-HAL IPC (hardware services)
/dev/vndbinderVendor-to-vendor IPC (HIDL/AIDL across partitions)

The split exists because Android 8 (Project Treble) separated the vendor partition from the system partition. You can't have app code directly calling HAL code across a security boundary, so hwbinder sits in the middle with its own context manager (hwservicemanager).


The Protocol: BC and BR Commands

Binder communication is driven by two sets of commands: BC (Binder Command) sent from userspace to the kernel, and BR (Binder Return) sent from the kernel back to userspace. When you call a remote method through Binder, this is what actually happens at the fd level:

The caller opens /dev/binder, mmap's a memory region, then writes BC_TRANSACTION via ioctl(fd, BINDER_WRITE_READ, ...). The kernel processes it, finds the target thread, and writes BR_TRANSACTION into that thread's read buffer. The target processes it, writes BC_REPLY, and the kernel writes BR_REPLY back to the original caller.

Here's the relevant enum from include/uapi/linux/android/binder.h:

enum binder_driver_command_protocol {
    BC_TRANSACTION         = _IOW('c', 0, struct binder_transaction_data),
    BC_REPLY               = _IOW('c', 1, struct binder_transaction_data),
    BC_ACQUIRE_RESULT      = _IOW('c', 2, __s32),
    BC_FREE_BUFFER         = _IOW('c', 3, binder_uintptr_t),
    BC_INCREFS             = _IOW('c', 4, __u32),
    BC_ACQUIRE             = _IOW('c', 5, __u32),
    BC_RELEASE             = _IOW('c', 6, __u32),
    BC_DECREFS             = _IOW('c', 7, __u32),
    BC_INCREFS_DONE        = _IOW('c', 8, struct binder_ptr_cookie),
    BC_ACQUIRE_DONE        = _IOW('c', 9, struct binder_ptr_cookie),
    BC_REGISTER_LOOPER     = _IO('c', 11),
    BC_ENTER_LOOPER        = _IO('c', 12),
    BC_EXIT_LOOPER         = _IO('c', 13),
    BC_REQUEST_DEATH_NOTIFICATION  = _IOW('c', 14, struct binder_handle_cookie),
    BC_CLEAR_DEATH_NOTIFICATION    = _IOW('c', 15, struct binder_handle_cookie),
    BC_DEAD_BINDER_DONE            = _IOW('c', 16, binder_uintptr_t),
    BC_TRANSACTION_SG      = _IOW('c', 17, struct binder_transaction_data_sg),
    BC_REPLY_SG            = _IOW('c', 18, struct binder_transaction_data_sg),
};

Most of those commands are reference counting. Binder tracks every object reference across process boundaries, and the INCREFS/DECREFS/ACQUIRE/RELEASE pairs manage the lifecycle. If a service process dies, Binder sends BR_DEAD_BINDER to every client that registered a death notification. That's how the system recovers from crashed services without needing a separate heartbeat mechanism.

App Process PID 1234 / user Binder Driver drivers/android/binder.c Shared mmap region transaction data offsets array flat_binder_object[] System Service PID 5678 / system BC_TRANSACTION BR_TRANSACTION BC_REPLY BR_REPLY binder_open() mmap() ioctl(BINDER_WRITE_READ) BC_REGISTER_LOOPER BC_ENTER_LOOPER read() blocks... binder_transaction() binder_alloc_new_buf() binder_thread_write() binder_thread_read() wake_up_interruptible() request path (BC_ / BR_TRANSACTION) reply path (BC_ / BR_REPLY)

The Memory Model: Zero-Copy and What It Costs You

The reason Binder is fast compared to sockets is the memory model. When you call mmap() on a Binder fd, the kernel allocates a virtual memory area in the calling process and maps it to a physical buffer managed by the driver. When a transaction arrives, the kernel copies the data from the sender directly into this buffer. The receiver reads from the same physical pages. No second copy.

The default buffer size is defined in drivers/android/binder_alloc.h:

#define SZ_1M                  0x00100000
#define BINDER_MIN_ALLOC       PAGE_SIZE
#ifdef CONFIG_ANDROID_BINDER_IPC_SELFTEST
#define BINDER_MAX_USER_BUFFER_SIZE (8 * SZ_1M)
#else
#define BINDER_MAX_USER_BUFFER_SIZE (4 * SZ_1M)
#endif

/* Default per-process mmap size (1MB for app binder) */

The actual limit per process is set when mmap() is called. For /dev/binder, it's 1MB. For /dev/hwbinder, it's also 1MB by default, but the allocator uses BINDER_VM_SIZE internally. The 1MB limit is per process, not per connection.

Inside that 1MB, the binder_alloc subsystem maintains two lists: one for allocated buffers and one for free buffers. When a transaction arrives, binder_alloc_new_buf_locked() finds the best-fit free block. When the transaction completes, BC_FREE_BUFFER returns it to the free list. Fragmentation builds up over time, especially in services that handle large transactions (like AudioFlinger or SurfaceFlinger).

The part that bites you during porting: when the buffer is full, binder_alloc_new_buf_locked() returns BR_FAILED_REPLY. There is no backpressure, no blocking, no useful error message. The caller gets DEAD_REPLY. logcat shows Binder reply failed and nothing else.

Binder Memory Layout App Virtual AS (user space) Physical RAM (shared pages) Kernel Virtual AS (binder driver) stack / heap (normal memory) binder mmap region VA 0x7f000000 size: 1MB read-only for receiver zero-copy alias mmap(fd, 1MB) other VAS regions Binder Buffer Pool struct binder_buffer header transaction data object offsets array free space kernel code / data kernel mapping same physical pages write-accessible binder_alloc_copy_to() writes sender data here 1 copy total (not 2) maps to maps to When buffer fills: BR_FAILED_REPLY no backpressure, caller gets DEAD_REPLY with no context

Thread Pool and Scheduler Interaction

This is where porting gets genuinely unpleasant.

Each process that uses Binder has a thread pool managed by the Binder driver. When the process starts, it calls BC_ENTER_LOOPER to register its main looper thread. As transaction load increases, the driver spawns additional threads via BR_SPAWN_LOOPER (up to the limit set by BINDER_SET_MAX_THREADS). The default maximum is 15 threads per process.

The critical part for kernel developers: Binder inherits the calling thread's priority for the duration of a transaction. This is called "scheduler priority propagation" and it exists so that a high-priority caller (like SurfaceFlinger) doesn't get stuck behind a low-priority service thread. The kernel code for this is in binder_transaction():

/* in drivers/android/binder.c */
static void binder_transaction(struct binder_proc *proc,
                                struct binder_thread *thread,
                                struct binder_transaction_data *tr,
                                int reply,
                                binder_size_t extra_buffers_size)
{
    ...
    /* Save and inherit priority */
    binder_set_priority(target_thread, &proc->default_priority);

    if (!(t->flags & TF_ONE_WAY)) {
        /* sync transaction: inherit caller priority */
        binder_set_priority(target_thread,
                            &thread->transaction_stack->priority);
    }
    ...
}

On stock kernels this works fine. On a MediaTek device like begonia (Helio G90T), MTK ships scheduler patches that modify set_user_nice() behavior to interact with their ACAO (All CPUs At Once) and FPSGO game optimization frameworks. When Binder tries to set the target thread's priority, it goes through the vendor scheduler path instead of the stock CFS path. The result: binder priority inheritance races with the vendor scheduler's own priority management, and the target thread sometimes ends up with a lower priority than the caller intended.

The symptoms on begonia before we patched this:

  • hwservicemanager startup latency spiking to 2-3 seconds on cold boot
  • Camera HAL taking 4-8 seconds to open (priority-starved binder thread)
  • Occasional system_server Binder:XXX_N thread stuck in D state (uninterruptible wait)
Binder Thread Pool State Machine IDLE no looper REGISTERED BC_REGISTER_LOOPER LOOPING BC_ENTER_LOOPER WAITING blocked in read() EXECUTING handling transaction RETURNING BC_REPLY sent spawned by BR_SPAWN_LOOPER main thread calls ioctl read() on /dev/binder BR_TRANSACTION wake_up_interruptible() BC_REPLY back to read() max 15 threads (BINDER_SET_MAX_THREADS)

Async Transactions and the Todo Queue

A lot of Binder traffic is ONEWAY (flagged with TF_ONE_WAY). The caller fires and forgets: no blocking, no return value. SurfaceFlinger uses this heavily for vsync signals and buffer queue notifications. So does the audio subsystem.

For ONEWAY transactions, the kernel does not put the work on the target thread's sync todo list. It goes to proc->async_todo instead, which is a separate queue. The target thread drains it when it loops back to read().

The problem: there is no depth limit on the async todo queue and no backpressure mechanism. If a high-frequency caller hammers a ONEWAY interface faster than the target can drain it (common with animation callbacks going to a slow service), the queue depth grows unbounded, memory usage spikes, and eventually the kernel OOM killer gets involved. There is no way for the caller to know this is happening.

In binder.c, the async queue work is inserted here:

if (reply || !(t->flags & TF_ONE_WAY)) {
    /* sync: put on thread's todo */
    BUG_ON(t->buffer->async_transaction != 0);
    binder_pop_transaction_ilocked(target_thread, t);
    if (list_empty(&target_thread->todo))
        wake_up_interruptible_sync(&target_thread->wait);
} else {
    /* async: put on proc's async_todo */
    BUG_ON(target_node == NULL);
    BUG_ON(t->buffer->async_transaction != 1);
    if (list_empty(&target_proc->async_todo))
        wake_up_interruptible(&target_proc->wait);
    list_add_tail(&t->work.entry, &target_proc->async_todo);
}

Notice list_add_tail with no length check. We added a configurable per-process async queue depth limit in Nova-Kernels. When the limit is hit, binder_alloc_new_buf_locked() returns BR_FAILED_REPLY for the new async transaction instead of queueing it. It's a lossy solution, but losing a vsync callback is better than running out of memory.

Async Transaction Queue Overflow Caller proc.async_todo (kernel) Callee (slow) ONEWAY tx #1 ONEWAY tx #2 ONEWAY tx #3 q[0] q[1] q[2] drain q[0] (slow) q[3] q[4] OVERFLOW ONEWAY tx #N (dropped) X no BR_FAILED_REPLY in stock kernel, silent drop after OOM. Nova-Kernels adds depth limit + BR_FAILED_REPLY.

Binder Freeze: Doze Mode's Hidden Tax

Android 12 added the ability to freeze Binder transactions to processes in the background, as part of improving Doze mode's battery savings. When the system decides a process should be fully suspended, it calls BC_FREEZE_BINDER on that process's binder context. While frozen, incoming transactions accumulate in a pending queue. When the process is thawed (BC_THAW_BINDER), the pending transactions are delivered.

The problem that bit us on begonia during backporting 6.x binder bits to a 4.14 base:

The freeze/thaw mechanism uses proc->is_frozen combined with a deferred work struct. In newer kernels, there's a careful locking sequence to ensure no in-flight transaction completes after the freeze is confirmed. In 4.14, binder_thread_read() doesn't check is_frozen between wakeup and work processing. The result is a TOCTOU: the process is frozen, but a transaction already pulled from the todo list gets processed by a thread that should have been asleep.

/* Simplified -- the race in 4.14 binder_thread_read() */
wait_event_freezable(thread->wait, binder_has_work(proc, thread, wait));

/* Between wait_event returning and here, proc->is_frozen could be set */

binder_transaction = binder_get_and_clean_thread_todo(thread);
/* ... processes transaction that should not have run ... */

The mainline fix is commit a23a03a ("binder: freeze: check pending work items") which adds the frozen check inside the work dispatch loop, not just at the wait_event level. We cherry-picked it and the surrounding dependency chain (7 commits) rather than trying to backport the isolated fix.


Debugging Binder Issues

When binder goes wrong, the kernel doesn't tell you why. These are the actual tools:

# Overall state: procs, threads, transaction stats
adb shell cat /sys/kernel/debug/binder/state

# Per-process stats: transactions in/out, buffer usage
adb shell cat /sys/kernel/debug/binder/stats

# Failed transactions (the useful one)
adb shell cat /sys/kernel/debug/binder/failed_transaction_log

# Successful recent transactions
adb shell cat /sys/kernel/debug/binder/transaction_log

A healthy stats output for system_server looks like:

proc 1234
  threads: 15
  requested_threads: 0
  requested_threads_started: 12
  ready_threads: 3
  free_async_space: 974848  <-- should be close to 1MB
  ...
  bc_transaction: 15482
  bc_reply: 15480
  br_transaction: 8921
  br_reply: 8920
  br_failed_reply: 0  <-- should be 0
  br_dead_reply: 0    <-- should be 0

br_failed_reply above zero means buffer exhaustion. br_dead_reply means the target process died. free_async_space dropping below 200KB is a warning sign.

For system-level view without root:

adb shell dumpsys activity service hwservicemanager
adb shell dumpsys -l  # list all services with binder

What We Changed in Nova-Kernels

Three patches for begonia, two of which will apply to VIENNA:

1. Raised hwbinder buffer size to 2MB

/* In binder_alloc.c, for /dev/hwbinder context */
#define HWBINDER_VM_SIZE (2 * SZ_1M)

HALs transfer larger payloads than apps (camera frames staging, audio buffers in some configurations). 1MB was regularly exhausting on cold boot when multiple HALs initialize concurrently.

2. Async queue depth limit

Added max_async_transaction_depth as a per-proc limit (configurable via sysfs, default 256). When exceeded, new async transactions return BR_FAILED_REPLY immediately rather than queuing. Callers that handle DEAD_REPLY gracefully (which well-written AIDL clients do) see this as a transient failure and retry. Callers that don't handle it... have a bigger problem.

3. Binder thread affinity hint

MediaTek's scheduler on begonia aggressively migrates threads to efficiency cores during medium load. Binder threads for high-priority processes (priority < 0 in setpriority terms) now get an affinity hint toward the big cluster. This is a hint, not a hard pinning. The scheduler can override it. But it prevents the common case of a hwservicemanager thread being migrated to a core running at 576MHz when it needs to respond to a camera HAL request in under 16ms.


This is the kind of thing that doesn't show up in any documentation. You learn it by reading dmesg, staring at binder/failed_transaction_log, and rebuilding the kernel at 2am until the symptoms go away. If you're working on a new port and binder is misbehaving, start with the debugfs files. The numbers in there are more honest than anything in logcat.