Inter-process communication
kernel/src/ipc/ + net/unix.rs · binder, PI futexes, SysV/POSIX MQ, AF_UNIX — mechanisms, lock models and measured numbers · Context: the OpenHarmony service mesh (~80 services) runs in full once /data is mounted · Sources: docs/architecture/binder-parity-audit.md, design-divergences.md (D-14)
§1binder (kernel/src/ipc/binder.rs, 14,144 lines)
binder is the core transport of the OpenHarmony service mesh: every system-service registration (samgr), service call, and the hilog data plane traverse it. The implementation is aligned function-by-function with Linux 6.6's binder.c semantics, with a dedicated parity audit (binder-parity-audit.md) constraining future changes: every change must cite the Linux function and line it mirrors.
- Protocol: version 8 (returned by the BINDER_VERSION ioctl); the full BC_* set (TRANSACTION/REPLY, FREE_BUFFER, INCREFS/ACQUIRE/RELEASE/DECREFS and their DONE pairs, REGISTER/ENTER/EXIT_LOOPER, DEAD_BINDER_DONE, TRANSACTION_SG/REPLY_SG); the full BR_* set (ERROR/TRANSACTION/REPLY/DEAD_REPLY/TRANSACTION_COMPLETE/NOOP/SPAWN_LOOPER/FAILED_REPLY etc.).
- flat_binder_object cross-process translation: the BINDER_TYPE_{WEAK_}BINDER/_HANDLE node/ref handshake (BR_INCREFS → BC_INCREFS_DONE) works; BINDER_TYPE_FD/FDA/PTR are partially landed — the FDA fd lifetime is an open item (install-at-send leaves ghost fds on undelivered transactions; audit on record).
- Deferred TRANSACTION_COMPLETE for sync transactions: the sender's read keeps sleeping until the reply carries the COMPLETE out — one wakeup completes the round trip; the replier side takes the non-deferred form.
- mmap buffer: silently truncated to 4 MiB (same as Linux's binder_mmap); a double mmap is rejected EBUSY before allocation; the VM_DONTFORK semantics were reversed across sc157→sc159/160 — OH appspawn children inherit the daemon's mapping, so DONTCOPY was removed.
- Lock model: a single global spinlock guards every binder object; no user-memory copies and no sleeping happen under the lock — BC payloads are copied in before locking, BR payloads are staged into a kernel Vec under the lock and copied out after release.
- thread_has_work parity: looper_need_return is a composite term of the function (process todo, node async_todo, spawned/pending counts); the poll wake fan-out and availability semantics converged four divergences in one diff.
- Failed-reply routing: BR_FAILED_REPLY/BR_DEAD_REPLY semantics plus the silent-receiver sweep sweep_silent_txns (process todo + node async_todo, the ONEWAY_SILENCE_J window) — holding buffer reclamation against services that neglect their BC_FREE_BUFFER duty; TF_UPDATE_TXN (0x40) is landed (the binder.c:2734/2832-2845 supersede-on-park parity).
- access_token registry: /dev/access_token_id + BINDER_GET_SENDER_INFO — the samgr permission-check dependency; its absence once made AddSystemAbility refuse and blocked the OH display (the b98–b112 black-screen root chain).
Runtime numbers and known costs
- At ~89k txns/s on the /data-mounted service mesh: binder REST sections n=64.4M, 592 s total (41% of the machine), average 9.2 µs.
- Watch row: a single transaction holds IF=0 for up to 73 ms — the IPC gate's SLOW-TXN class (queue ≤ 10 ms everywhere; FAIL at queue ≥ 50 ms) watches this shape.
§2PI futexes (sync/futex_pi.rs)
- Op set: FUTEX_LOCK_PI / UNLOCK_PI / TRYLOCK_PI / LOCK_PI2 + WAIT_REQUEUE_PI / CMP_REQUEUE_PI; the cmp-under-lock requeue and the robust-exit death mark are complete.
- Implementation shape: a nice floor: while a PiState has waiters, its owner runs at nice −20; wait chains are walked for deadlock detection with transitive boosting at every link; the exit path releases PI locks with the owner-died handoff.
- The semantic difference from Linux's rt_mutex (kernel-side priority elevation vs nice translation) is recorded in the implementation comments; the LTP pi family plus the OH FFRT load are the guards.
§3SysV triple and POSIX MQ
- Compile-time layout asserts: ShmidDsUapi 112 bytes (asm-generic/shmbuf.h RV64), SemidDsUapi 104 bytes (rv64) / 88 (the 32-bit path), MqAttr 64 bytes (glibc/Linux RV64); IpcPermUapi field offsets asserted item by item — UAPI struct drift is unrepresentable at compile time.
- ipc_namespace scoping: SysV objects filter key visibility and id resolution by the caller's namespace (1bde7490; LTP msg_comm "messages leak between namespaces" is the landing guard).
- POSIX MQ is fully implemented; the mqueue mount is an empty tmpfs alias carrying MQUEUE_MAGIC for systemd verification (the statfs-magic family fix).
- MAX_SOPS is enforced on sem; the SIGEV UAPI values and the notify validation order are aligned (3ba353542).
§4AF_UNIX (net/unix.rs, 4,372 lines)
- Types: SOCK_STREAM / SOCK_DGRAM / SOCK_SEQPACKET (connection mode with record boundaries, MSG_TRUNC on short reads).
- Abstract namespace: sun_path[0] == 0 means an abstract name — the address IS the name, no filesystem node, presented as @name in /proc/net/unix; autobind "\0%05x" aligned; UNIX_PATH_MAX=108.
- SCM_RIGHTS fd passing + SCM_CREDENTIALS: the cmsg payload rides UnixSeg.files/cred and is consumed by the first recv touching the segment.
- Weak peer model: peer links are Weak on both sides — the fds are the only strong owners, and "peer closed" is simply Weak::upgrade failing; reference cycles are unrepresentable in this encoding, so the leaked-socket-pair class does not exist.
- Single-lock send path: each socket's recv_queue/accept_queue/state/eof are independent spinlocks; a sender takes only the target's recv_queue lock — no AB-BA between two sockets is reachable.
- Wait discipline: the W3 wait-queue form (prepare_to_wait → re-check → schedule → finish_wait); poll semantics: listeners report only POLLIN, never POLLHUP; a closed peer with unread data = POLLHUP|POLLIN.
§5Benchmark numbers
| Metric (pbench P5, debug class, KVM) | Before D-14 | After (gate band) |
|---|---|---|
| p5a pipe 1-byte roundtrip | 19.673 µs | 18.306 µs |
| p5b unix DGRAM send/recv (74 B, the hilogd protocol shape) | 318.6 / 318.3 µs | 23.1 / 40.9 µs (band 23–41) |
| p5c unix stream write (74 B) | 276.5–277.6 µs | 24.9–26.8 µs (band 24–27) |
| p5c3 bidirectional stream | 32.7 µs | 25.0 µs |
| lmbench lat_pipe (same test platform) | 13.30 µs (Linux) | 5.11 µs (2.6×) |
| appspawn fork (the service-spawn critical path) | 130–272 ms | 85–90 ms |
Known costs (as recorded): a wake→run behind an incumbent is tick-quantized to ~10 ms (the p2f suite run, both hog regimes) — a consequence of D-14's sub-6 ms window design; the removal path is on the scheduler NOHZ section. The binder 73 ms single-transaction IF=0 max is a watch row.
Code:
kernel/src/ipc/ (binder / access_token / sysv_shm / sysv_sem / sysv_msg / posix_mq / util), kernel/src/sync/futex_pi.rs, kernel/src/net/unix.rs. Parity audit: docs/architecture/binder-parity-audit.md (a gap table and an invention-risk table, each entry required to cite the Linux function and line). Guards: the LTP sem/msg/shm/pi families + the full OH service set; the perf gate's IPC-domain p5 bands and the binder SLOW-TXN class.