Repository navigation
perf(fast_io): stop waiting for the SEND_ZC notification CQE - #7600
Merged
Merged
Conversation
`IORING_OP_SEND_ZC` posts two completions: a transfer CQE ("the bytes are
queued for transmit") and a notification CQE ("the kernel has released your
pages"). `try_send_zc` blocks on both, which is correct but forfeits the
point of zero-copy - it keeps exactly one send in flight and pays a second
`io_uring_enter(2)` and its wakeup on every send.
Measured on this repo's daemon-pull path, 200 MB over loopback, 6400 sends
of 32776 bytes each: the notification wait was 0.249 s of the 0.333 s spent
inside `try_send_zc`, against a 0.41 s end-to-end gap to a plain socket
write. Ruled out as causes: the submit_and_wait count (exactly 2 per send,
never more), the dispatch threshold (every send was 32776 bytes; there were
no sub-threshold sends at all), and per-call ring construction (a counter in
`PerThreadRing::new` reported 1 build for the whole transfer).
Split the two waits. `SendZcPipeline` still waits for the transfer CQE -
that is what carries the byte count, surfaces `-errno`, handles short sends
and puts the bytes in the socket's transmit queue ahead of any later writer
on the same fd - but lets the notification land later. It can do that
because the caller hands it an owned buffer: `send_staged` swaps the socket
writer's staging buffer for one the kernel has already released, so no
caller ever names the memory the kernel still holds. A four-buffer pool
keeps the pipe full and bounds what an un-reaped notification can hold.
Only the buffered flush path moves. `write()` with an oversized caller slice
sends memory the caller may free on return, so it stays on the synchronous
`try_send_zc`; the SAFETY comments that justified themselves by that wait
are rewritten to say which path they now describe.
The pipeline owns a private ring. Deferred notifications stay queued across
calls, and every other consumer of the per-thread ring drains its completion
queue unconditionally and demuxes on `user_data` alone, so a stray
notification left there would be misread as somebody else's completion.
`Drop` is the load-bearing part: it drains every outstanding notification
before any pooled buffer is deallocated, and leaks the buffers it cannot
account for rather than freeing memory the kernel may still be reading.
Closing the ring fd is not a substitute - io_uring teardown is deferred to
`io_ring_exit_work`.
Result on the same 11-rep loopback A/B: SEND_ZC goes from 0.5125 s to
0.1068 s median (4.8x), against 0.0735 s for the plain socket write. The
serialisation is gone; loopback still has nothing for zero-copy to
amortise, since it swaps a kernel copy for a userspace one and the receiver
copies out either way.
oc-only: upstream rsync has no io_uring path.
Section 3.1 of the SEND_ZC design specified a pin-counted slot pool; send_zc_pipeline.rs implements it with three measured deviations (owned buffers instead of registered slots, a private ring, and a retained transfer-CQE wait). Record them next to the proposal, and mark the sentence in the IUS-2 audit that cites the now-removed notification wait on the flush path as superseded rather than rewriting the audit's record.
Owner
Author
|
FYI yes, these are real numbers from my benchmarks. |
oferchen
added a commit
that referenced
this pull request
Sep 2, 2026
`## [Unreleased]` last covered the #7304-#7522 era. Measured against the 60 most recently merged pull requests, 44 were absent; of the most recent 45, 43 were absent. Nothing from #7574 onward was recorded at all. Each entry is derived from what the pull request actually landed - the merge commit and the body - not from its title. Three cases where those differ: * #7616 ships far more than the doc-alias correction its body describes: the `upstream_version` pin moves to 3.5.0 and `--version` starts naming `quic`, `sd-notify`, `vmsplice` and `send_zc`. * #7618 also adds the pull-request rustdoc-link job its body says was filed separately, which is what closes the master-only-gate recurrence. * #7602's squash carries the closed #7597 as its first commit, so the directory-obstacle half is recorded under #7602. #7587 is deliberately not recorded. Its merge commit is empty: the change had already landed as #7563, which the section already names. Entries extend the existing Keep a Changelog groups and the bold sub-groupings. Clusters that are one story carry one entry with several references: the FERROR_XFER routing and its two-phase drain (#7576, #7581), the connection-FSM close edge and the accept loop that outlives a session error (#7579, #7590), the three detaching daemon suites (#7578, #7588, #7592), the io_uring and SEND_ZC work (#7593, #7600, #7610), the citation retargets (#7601, #7603, #7604), and the accept-loop pin flake (#7615, #7620). Only `## [Unreleased]` is touched; every released section, including the 3.4.4 statements that were correct when written, is byte-identical.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The regression
ZeroCopyPolicyreachingIORING_OP_SEND_ZCon the daemon-sender's socketmade a 200 MB loopback daemon pull 5.8x slower, not faster.
iouring-send-zc)Diagnosis, measured before the fix
try_send_zcsubmits one SQE and blocks until it has seen bothcompletions: the transfer CQE ("the bytes are queued for transmit", carrying
the byte count and
IORING_CQE_F_MORE) and the notification CQE(
IORING_CQE_F_NOTIF, "the kernel released your pages"). Waiting for thesecond is the entire cost.
Per-call instrumentation of the daemon-sender for one 200 MB pull, recorded
in memory and dumped once at the end so the trace does not perturb the run
(traced 0.447 s vs untraced ~0.50 s):
try_send_zc,and 0.249 s of the 0.41 s end-to-end gap.
submit_and_wait(1)entries persend in 6390/6400 cases - one for each CQE, never a storm.
SEND_ZC_MIN_BYTESruled out: every send is 32776 bytes (a 32 KiBmultiplex frame plus its 8-byte header). There are no sub-threshold sends
at all, so the 16 KiB / 4 KiB threshold is not selecting anything.
PerThreadRing::newreportedper_thread_ring_builds=1for the wholetransfer.
which is roughly what the send itself costs.
The fix
Split the two waits.
SendZcPipelinestill waits for the transfer CQE. That is what carriesthe byte count, surfaces
-errno, handles short sends by resubmitting theremainder, and - once observed - guarantees the bytes are in the socket's
transmit queue, which is what keeps wire ordering against
submit_send_batchand the synchronous path on the same fd. It does not wait for the
notification.
Why that is sound
Only the buffered flush path moves. The two call sites differ in who owns
the memory:
flush_buffersends from the writer's own buffer. It hands that buffer tothe pipeline by swap, getting back a buffer the kernel has already
released. No caller ever names the memory the kernel still holds, so there
is nothing to observe, refill, or free early. A four-buffer pool keeps the
pipe full and bounds what an un-reaped notification can hold at
4 * buffer_sizeper connection.write()with a caller slice at or abovebuffer_sizesends memory thecaller may free the moment
writereturns. It stays on the synchronoustry_send_zc. The SAFETY comments inflush_bufferandwritethatjustified themselves with "try_send_zc waits for the notification CQE
before returning" are rewritten to say which path they now describe.
Accounting:
pendingis incremented when an SQE is pushed and decrementedexactly once per submission - by the notification CQE, or by a transfer CQE
with
IORING_CQE_F_MOREclear, which is the kernel saying no notificationwill follow. Counting up at submit time keeps it an upper bound at every
instant, so a buffer is never considered free while the kernel might still
be reading it, and the drain always terminates.
The Drop drain
SendZcPipeline::dropdrains every outstanding notification before anypooled buffer is deallocated. It is unconditional: the field is dropped as
part of dropping the writer on the normal path, the error path and during
unwinding. If the drain itself fails, the still-pinned buffers are leaked
with
mem::forget- leaking memory is sound, freeing memory the kernelholds a reference to is not. An
EINTRfromio_uring_enter(2)resumes thewait (a syscall restart, not a retry policy), because abandoning it would
force that leak.
Closing the ring fd is not a substitute: io_uring teardown is deferred to
io_ring_exit_work, so the kernel's page references can outliveclose(2).Why a private ring
Deferred notifications stay queued across calls. Every other io_uring
consumer on the calling thread -
submit_send_batchin particular - drainsthe per-thread ring's completion queue unconditionally and demultiplexes on
user_dataalone, so a notification left there would be consumed by, andmisread as, somebody else's completion. The pipeline owns a ring nothing
else submits to or reaps from. It is one ring per socket writer, i.e. per
connection, so it does not reintroduce the shared-ring serialisation IUR-3
removed;
tools/ci/check_shared_ring_removal.shstill passes.RegisteredBufferGroupwas evaluated and rejected as the vehicle: it isregistered against one specific ring, the SEND_ZC path never sets
buf_indexso the registration would be inert, and using it forces a memcpyinto the slot - a copy that zero-copy exists to remove.
Results
Same host, same 200 MB source, 11 alternating reps per cell,
/dev/shmdestination, page cache warmed once, medians reported.
The fix is a 4.8x speedup and removes the regression. It does not make
SEND_ZC beat a plain socket write on loopback - 0.107 s vs 0.074 s, still
about 1.45x behind.
Post-fix instrumentation says the remaining gap is not pool starvation:
Time inside the send primitive drops from 0.333 s to 0.064 s. A free buffer
was available immediately in over 90% of sends, so a deeper pool would
recover at most 0.018 s.
Loopback simply has nothing for zero-copy to amortise: the writer copies the
frame into its own buffer either way, SEND_ZC replaces the kernel's copy
with page pinning, and the receiver copies out regardless. A test that could
show SEND_ZC winning needs a real NIC with scatter-gather and checksum
offload (so the kernel copy it removes is the actual bottleneck), payloads
large enough to amortise
get_user_pages_fast, and a link fast enough thatthe copy, not the wire, is the limit.
Verification
On the Linux host (kernel 7.0, aarch64), against this branch alone:
cargo fmt --all -- --checkcargo clippy -p fast_io --all-targets -- -D warnings, and again with-F iouring-send-zccargo nextest run -p fast_io(1132 passed) and with-F iouring-send-zc(1135 passed)
tools/ci/check_shared_ring_removal.sh(
cmpplus matching sha256).New tests in
send_zc_pipeline: the swap hands the kernel an allocation thecaller no longer holds; more sends than pool slots round-trip byte-exactly
and drain to zero pending (and drain is idempotent); dropping with a send
outstanding still delivers every byte.
They deliberately do not assert "the notification is still outstanding when
send_stagedreturns". On loopback it usually is not - the packet reachesthe receiving socket immediately, so the kernel posts the notification right
behind the transfer CQE and one quiet send reaps both in a single drain.
What the pipeline changes is that it never waits, which is visible only
under load; the A/B above is what pins it.
Scope
oc-only: upstream rsync has no io_uring path, so there is no upstream
behaviour to mirror. The default build does not enable
iouring-send-zc, sothe stock socket-send path is untouched.