Synchronization and SMP coordination¶
Atomic operations and locks¶
The kernel and native runtime use naturally aligned compiler atomic operations on coherent memory. Use acquire/release for ownership transfer and publication. ISA-specific spin hints reduce pressure while rechecking a contended word. Use the device and translation barriers required by the architecture for MMIO, DMA, and TLB maintenance.
A raw spinlock protects short kernel operations with a bounded hold time. A caller must prevent local interrupt or preemption reentry when the same lock is used from those contexts. Preserve and restore the preceding interrupt state at IRQ-safe call sites. Block in scheduler context for longer waits. Dispatch lock ownership is transferred explicitly across scheduler stack switches. Debug builds check mailbox, endpoint, capability graph, page-table, and PowerPC hash lock rank and ownership on the current CPU. The scheduler checks that a continuation holds none of those locks when it blocks. Measure contention before adopting a queued spinlock.
The ranked locks follow this acquisition order.
-
enum class spin_rank : unsigned¶
Raw locks nest only in increasing rank order.
Values:
-
enumerator mailbox¶
-
enumerator endpoint¶
-
enumerator capability¶
-
enumerator page_table¶
-
enumerator powerpc_hash¶
-
enumerator mailbox¶
-
template<spin_rank Rank>
class spin_lock_t¶ The caller prevents local interrupt and preemption reentry.
The capability graph owns slot generation, derivation, and revocation. The supervisor derives capabilities outside the resident dispatch lock. It also revokes capability trees outside that lock once task teardown has closed the endpoint and sealed admission to the address space. Lookups that need an object after validation require an owner-held lifetime. A short guarded visitor can use a capability during validation without retaining the object, blocking, or calling back into the graph.
An endpoint’s atomic notification latch combines the pending badge, receiver publication, and closure. The guarded notification path can publish a badge without taking the dispatch lock when nobody waits. A receiver atomically consumes the badge or publishes its intent to wait. A sender that sees a waiting receiver enters the dispatcher path, where the receiver’s capability and deadline are checked before waking it. Closure and receiver publication exclude a late fast notification.
Each rendezvous endpoint owns its sender, receiver, and reply queues. A native reply validates its token and both capability handles and publishes the outcome under the endpoint lock. A later timeout, cancellation, or close that finds the published reply retires it with that outcome. The reply syscall can finish the same transaction when it reenters the dispatcher. A pending-reply query reads only endpoint state. The resident runtime keeps endpoint storage live across these operations, including closure and task-slot reuse. Capability generations and reply tokens validate the current instance. Take the endpoint lock before the capability graph lock. Release it before taking the dispatch lock or switching continuations.
Remote admissions, wakeups, and migrations publish an inbound list entry for the destination CPU. An idle pull or periodic balance posts a request to the source. The owner rechecks eligibility, accounts for the thread, and detaches it before assigning a new owner. The destination accepts the entry and updates its run queue and policy accounting before selecting work. A remote completion detaches the waiter before publishing its wake. The owner removes the deadline when it accepts the wake and drains arrivals before checking for timeouts. The dispatcher still serializes changes that combine waiter queues, budget donation, and task state. Explicit priority changes can also remove ready entries under that lock.
Page-table writers close admission to new activations of the target space, drain existing activations, and complete the required invalidation before releasing affected frames. Reserve the target’s lifetime while waiting outside the dispatch lock. The ordinary frame map, unmap, and protection-reduction syscalls pin their targets through teardown. Map and unmap serialize the leaf-to-frame bookkeeping per task and release the dispatch lock while mutating a leaf. Permission reduction is rechecked under the per-space lock. Private frame allocation and zeroing also run outside the dispatch lock. Copy-on-write transactions and IPC transitions that park, wake, or donate a budget use dispatcher serialization. PowerPC retains protection for the shared hardware hash.
Native word waits¶
Userspace uses atomic fast paths for uncontended mutex and semaphore operations. An aligned 32-bit word can be compared against an expected value and then parked. The comparison and enqueue share serialization with wakeup. Callers publish the new value with release ordering and recheck the predicate after waking. A wait can end through wakeup, timeout, or task cancellation. A wake is limited to the requested number of waiters.
A private key belongs to the caller’s address space. A shared key belongs to the capability-backed data frame and byte offset. A mapping confers access to the key, and a parked waiter pins its shared frame identity. The kernel reads the word through its validated resident mapping. An invalid address returns an error without recursively invoking the pager. The public interface is the POSIX mutex, condition variable, and semaphore API in libc. Servers can use libsys word waits directly.
CPU calls and rendezvous¶
Send short CPU-local operations through per-CPU work queues. The target mask is a logical CPU set and includes the caller only when requested. The architecture supplies the doorbell. Publish descriptors before signaling and acknowledge completion after the target releases the argument. The callback descriptor is the queue entry, so posting uses caller-provided storage. CPU zero drains dispatcher callbacks on its existing polling path. A synchronous wait requires forward progress on each target, so callers may hold only locks that the callbacks and their dispatch paths do not need.
A rendezvous adds explicit arrival and phase barriers for operations that require all selected CPUs to participate together. Its callbacks are bounded and do not sleep. Keep normal scheduling notifications and architecture-specific TLB invalidation on their dedicated paths. PowerVM enumerates Open Firmware CPU nodes, and PowerNV enumerates the device tree interrupt servers. Each started CPU owns a stack, firmware interrupt address, and acknowledgement record. Startup is sequential because the PowerPC firmware entry consumes a shared startup descriptor.
Deferred reclamation¶
The FreeBSD network compatibility layer currently uses a serialized userspace epoch. Adopt Concurrency Kit within the service if networking moves to concurrent readers. Keep writer exclusion and domain-specific lifetime rules. Use independently implemented kernel RCU only after a short, read-heavy lookup demonstrates a benefit. Treat kernel-to-user, idle, and dispatch transitions as quiescent only once no read section is live. Reclamation never substitutes for capability checks, transaction serialization, or TLB acknowledgement.