go-linux

io_uring design

Part of the accepted-design index.

A storage worker can submit independent page reads or writes on standard Go files and dispatch terminal completions by request ID. The package depends on Go and the existing x/sys syscall numbers. Its private layouts follow the Linux 7.0 UAPI. liburing’s setup and queue code is the implementation reference for the shared mappings. The basic queue fits a direct binding and needs no additional runtime or module dependency.

Each submission publishes either one SQE or one complete soft-link chain before entering the kernel. Linux needs the chain in a single enter to apply its links. IORING_SETUP_SUBMIT_ALL lets Linux consume later SQEs after one fails preparation and report each outcome. With SQ polling disabled, the observed SQ head identifies the accepted prefix. Go retains those requests and withdraws and releases the suffix after enter returns. One mutex serializes changes by Go callers, and atomic tail publication and head consumption order access with the kernel. Limiting outstanding ordinary requests to the requested depth, including unread completions, and reserving one cancellation slot fits within Linux’s default CQ capacity of twice the rounded SQ depth. Each request produces one CQE.

Each ordinary file request duplicates the file under RawConn.Control and pins any buffers until userspace observes its terminal CQE. Kernel addresses are invisible to Go’s collector. Pending pin holders have independent Go roots so abandoning an open queue cannot cause the collector to release memory the kernel may still access. Callers close queues explicitly to release those roots and kernel resources.

Scatter/gather requests build an x/sys iovec array from caller slices and pin the array and each nonempty backing buffer through completion. This keeps kernel-visible addresses stable without copying payloads or retaining caller-owned slice headers. Whole-file sync uses the same descriptor ownership and completion slots as ordinary file I/O. A caller orders standalone writes and sync by checking write completions before submitting sync.

Typed operation values share preparation and ownership with standalone submissions. Staging the chain before publishing its SQEs keeps validation and file-acquisition errors from starting an incomplete chain. Linux handles dependent execution, short-I/O failure, and downstream cancellation while preserving each completion. A linked I/O with a successor must fit within Linux’s page-aligned MAX_RW_COUNT because the kernel compares its result with the capped transfer length when deciding whether the chain continues. A partial enter can finalize and execute a prefix, so the queue reports the accepted IDs and leaves the retry decision to the caller.

Timeout SQEs preserve the selected Linux clock and report their own terminal CQEs. Linux arms a link timeout alongside the immediately preceding operation and can race with that operation’s completion. A bounded completion wait uses IORING_ENTER_EXT_ARG and tracks the remaining monotonic duration across interruptions. It occupies the existing completion lock while waiting and adds no request, leaving all buffer ownership with the queue on expiry.

Close drains using the same completion path as Wait and can block until the files complete. Closing the ring descriptor alone does not prove quiescence for a Go buffer. An enter failure during the drain leaves the ring available for retry and retains resources for the requests still outstanding. After the drain, the first close consumes the descriptor and disables I/O. The queue retains ownership of each mapping until its unmap succeeds, so a retry releases only the resources still owned.

Storage workers can put repeatedly used files and page buffers into two queue-owned tables. Positional fixed I/O takes indices into both tables. The queue probes the fixed read and write opcodes on demand. The standard submission path continues to work on runners that permit only setup and enter.

The kernel owns the fixed-file references. Go retains and pins each backing slice separately from the kernel’s page registration. Table mutation requires all fixed completions to be consumed, so the queue can release a replaced buffer immediately after Linux accepts the update without needing a second retirement mechanism. Buffer updates commit only the reported prefix. File updates leave slot occupancy with the kernel because Linux can clear a slot before its replacement fails. Closing a drained queue drops all Go pins and lets the kernel retire the registration tables with the ring context.

Cancellation uses a separate SQE and request ID so callers receive the kernel outcome for both requests. The queue releases resources only when it consumes the I/O’s terminal CQE, which keeps a successful or refused cancellation from changing buffer or file ownership. The completion consumer keeps the queue’s descriptor and mappings open while it waits in the kernel. The submission lock can then admit a cancellation from another goroutine during Wait or Close while preserving the serialization of ordinary I/O and table operations.

Readiness and completion notifications

A one-shot poll on a standard Go file fits the queue’s one-request, one-terminal-completion capacity rule. It holds the same independent file reference as positional I/O. Linux runs the initial poll on an io-wq worker because a file’s poll callback can sleep, for example while waiting for a FUSE daemon. Running that callback inline would hold the queue locks and delay returning the request ID. A linked timeout can bound the poll itself, while a completion wait timeout leaves it pending. The poll mask uses the existing completion result slot, preserving the shape of Completion for existing callers. Multishot polling needs a separate completion-capacity and resource-retirement contract.

An existing event loop can reuse its eventfd counter for completion notifications. The queue borrows the file under Go’s lifetime protection while Linux takes its own eventfd context reference. The eventfd package and queue share the internal representation, and eventfd’s epoll dependency continues in the same direction. This avoids a public descriptor accessor, a temporary duplicate, and a second counter API. Notification delivery and completion consumption have independent state. Use the Linux registration that signals for all completions so the worker can also notice operations that complete inline. Linux eventfd registration.