Skip to content

perf(fast_io): stop waiting for the SEND_ZC notification CQE - #7600

Merged
oferchen merged 2 commits into
masterfrom
perf/send-zc-async-notif
Sep 1, 2026
Merged

oferchen merged 2 commits into
masterfrom
perf/send-zc-async-notif

Conversation

@oferchen

@oferchen oferchen commented Sep 1, 2026

Copy link
Copy Markdown
Owner

The regression

ZeroCopyPolicy reaching IORING_OP_SEND_ZC on the daemon-sender's socket
made a 200 MB loopback daemon pull 5.8x slower, not faster.

cell median (11 alternating reps) throughput
plain socket writer (no cargo feature) 0.0866 s 2422 MB/s
SEND_ZC (iouring-send-zc) 0.5046 s 416 MB/s

Diagnosis, measured before the fix

try_send_zc submits one SQE and blocks until it has seen both
completions: 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 the
second 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):

send count = 6400
xfer_us:  sum=0.084 s  p50=12  p90=19  p99=36
notif_us: sum=0.249 s  p50=28  p90=51  p99=91
total_us: sum=0.333 s
waits: [(1, 10), (2, 6390)]
payload histogram: 32776 -> 6398, 32795 -> 1, 32796 -> 1
  • The notification wait is 75% of the time spent inside try_send_zc,
    and 0.249 s of the 0.41 s end-to-end gap.
  • Syscall count ruled out: exactly two submit_and_wait(1) entries per
    send in 6390/6400 cases - one for each CQE, never a storm.
  • SEND_ZC_MIN_BYTES ruled out: every send is 32776 bytes (a 32 KiB
    multiplex 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.
  • Per-call ring construction ruled out: a counter in
    PerThreadRing::new reported per_thread_ring_builds=1 for the whole
    transfer.
  • The transfer CQE is not the problem: 0.084 s of 0.333 s, p50 12 us,
    which is roughly what the send itself costs.

The fix

Split the two waits.

SendZcPipeline still waits for the transfer CQE. That is what carries
the byte count, surfaces -errno, handles short sends by resubmitting the
remainder, and - once observed - guarantees the bytes are in the socket's
transmit queue, which is what keeps wire ordering against submit_send_batch
and 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_buffer sends from the writer's own buffer. It hands that buffer to
    the 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_size per connection.
  • write() with a caller slice at or above buffer_size sends memory the
    caller may free the moment write returns. It stays on the synchronous
    try_send_zc. The SAFETY comments in flush_buffer and write that
    justified themselves with "try_send_zc waits for the notification CQE
    before returning" are rewritten to say which path they now describe.

Accounting: pending is incremented when an SQE is pushed and decremented
exactly once per submission - by the notification CQE, or by a transfer CQE
with IORING_CQE_F_MORE clear, which is the kernel saying no notification
will 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::drop drains every outstanding notification before any
pooled 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 kernel
holds a reference to is not. An EINTR from io_uring_enter(2) resumes the
wait (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 outlive close(2).

Why a private ring

Deferred notifications stay queued across calls. Every other io_uring
consumer on the calling thread - submit_send_batch in particular - drains
the per-thread ring's completion queue unconditionally and demultiplexes on
user_data alone, so a notification left there would be consumed by, and
misread 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.sh still passes.

RegisteredBufferGroup was evaluated and rejected as the vehicle: it is
registered against one specific ring, the SEND_ZC path never sets
buf_index so the registration would be inert, and using it forces a memcpy
into the slot - a copy that zero-copy exists to remove.

Results

Same host, same 200 MB source, 11 alternating reps per cell, /dev/shm
destination, page cache warmed once, medians reported.

A/B A B A median B median
feature-only, before plain SEND_ZC 0.0866 s 0.5046 s
feature-only, after plain SEND_ZC pipelined 0.0735 s 0.1067 s
fix isolated (both feature-on) before after 0.5125 s 0.1068 s

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:

send count = 6400
send_slot (transfer CQE waits):        sum=0.046 s  p50=5 us
acquire_free_slot (pool exhaustion):   sum=0.018 s  p50=0  p90=0
pending after each send: 0->1365 1->2470 2->1352 3->724 4->488 5->1

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 that
the copy, not the wire, is the limit.

Verification

On the Linux host (kernel 7.0, aarch64), against this branch alone:

  • cargo fmt --all -- --check
  • cargo clippy -p fast_io --all-targets -- -D warnings, and again with
    -F iouring-send-zc
  • cargo nextest run -p fast_io (1132 passed) and with -F iouring-send-zc
    (1135 passed)
  • tools/ci/check_shared_ring_removal.sh
  • A 200 MB daemon pull through the pipelined writer is byte-identical
    (cmp plus matching sha256).

New tests in send_zc_pipeline: the swap hands the kernel an allocation the
caller 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_staged returns". On loopback it usually is not - the packet reaches
the 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, so
the stock socket-send path is untouched.

`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.
@oferchen

oferchen commented Sep 1, 2026

Copy link
Copy Markdown
Owner Author

FYI yes, these are real numbers from my benchmarks.

@oferchen
oferchen merged commit 92e50ab into master Sep 1, 2026
72 checks passed
@oferchen
oferchen deleted the perf/send-zc-async-notif branch September 1, 2026 19:50
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant