Skip to content

feat(fast_io): per-thread io_uring rings replacing shared Arc<Mutex<ring>> (#2243) - #4288

Merged
oferchen merged 1 commit into
masterfrom
feat/iouring-per-thread-rings-2243
May 17, 2026
Merged

oferchen merged 1 commit into
masterfrom
feat/iouring-per-thread-rings-2243

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

  • Adds ThreadLocalRingPool next to SessionRingPool in fast_io::io_uring. Each calling thread lazily builds its own ring on first acquire and keeps it in thread-local storage, so submit/reap holds no lock.
  • Mirrors the new types in the non-Linux stub so cross-platform call sites compile.
  • Design note docs/design/iouring-per-thread-rings.md updated to mark the primitive implemented and to record why work stealing was rejected (stolen SQEs cannot use the owning ring's registered buffers or fixed-file table).

Approach

The session-pool design from #1937 (just merged) already exposes one mutex per ring. The per-thread variant is the zero-lock alternative when a consumer can pin a ring to a worker thread. Boxing the per-pool RefCell inside the thread-local vector keeps the inner cell at a stable heap address, so sibling pool acquires on the same thread cannot invalidate an outstanding lease pointer.

Existing SharedRing and per-file ring consumers (file_writer, file_reader, disk_batch, registered_buffers) keep their current single-owner shape. There is no Arc<Mutex<RawIoUring>> to remove in production code today (the design doc confirms this); migrations of those consumers onto the new pool are gated on the bench grid in section 5 of the design note.

Test plan

  • CI fmt+clippy passes
  • CI nextest (stable) passes on Linux musl, macOS, Windows
  • cargo nextest run -p fast_io --all-features -E 'test(thread_local_pool_)' passes locally on Linux 5.6+
  • cargo nextest run -p fast_io --all-features passes on macOS (stub path)

Adds a per-thread io_uring ring primitive next to the existing
SessionRingPool. Each calling thread lazily builds its own ring on
first acquire and holds it in thread-local storage, so the submit
and reap paths hold no lock.

- ThreadLocalRingPool, ThreadLocalRingLease in fast_io::io_uring.
- Stub mirrors on non-Linux so cross-platform call sites compile.
- Six unit tests covering same-thread fd reuse, distinct rings per
  thread, reentrant acquire rejection, two pools sharing a thread,
  clone sharing per-thread state, and setup-failure fallback.
- Design note updated to mark the primitive as implemented and to
  record why work stealing was rejected (stolen SQEs cannot use
  the owning ring's registered buffers or fixed-file table).

Existing SharedRing and per-file ring consumers (file_writer,
file_reader, disk_batch, registered_buffers) keep their current
single-owner shape. Migrating those consumers is gated on the bench
grid in docs/design/iouring-per-thread-rings.md section 5.
@oferchen
oferchen merged commit b3f0de8 into master May 17, 2026
18 of 19 checks passed
oferchen added a commit that referenced this pull request May 17, 2026
Scaffold the `IORING_OP_ASYNC_CANCEL` primitive now that the async daemon
listener (#4278), session ring pool (#4275), and per-thread rings (#4288)
have landed code paths that keep SQEs in-flight across abort decision
points. Until now, every io_uring helper followed `submit + wait + drain`
and never needed cancellation; the deferred design at
`docs/design/iouring-async-cancel.md` documented the trigger condition
this commit fulfils.

- New module `crates/fast_io/src/io_uring/cancel.rs` (Linux + `io_uring`
  feature gated):
  - `cancel_by_user_data(&mut IoUring, u64) -> io::Result<CancelOutcome>`
    submits a single `IORING_OP_ASYNC_CANCEL` SQE matched against the
    target SQE's `user_data` tag and reports the kernel's terminal
    outcome.
  - `cancel_all_by_fd(&mut IoUring, RawFd) -> io::Result<usize>` uses
    `IORING_ASYNC_CANCEL_FD | IORING_ASYNC_CANCEL_ALL` (kernel 5.19+) to
    cancel every in-flight SQE that touches the given fd.
  - `enum CancelOutcome { Cancelled, NotFound, AlreadyComplete }` maps
    the three race outcomes (`0`, `-ENOENT`, `-EALREADY`) to typed
    variants so callers route into the correct buffer reclaim path.
- Added `OpTag::Cancel` so cancel CQEs are demuxable from target CQEs
  sharing the same CQ; updated `SharedRing::reap` to flag a Cancel CQE
  as an InvalidData misuse rather than silently drop it.
- Added cross-platform UAPI constants `IORING_OP_ASYNC_CANCEL`,
  `ASYNC_CANCEL_MIN_KERNEL`, `ASYNC_CANCEL_FD_MIN_KERNEL` in
  `io_uring_common`, mirrored in the non-Linux stub.
- Stub module in `io_uring_stub.rs` returns `Unsupported` so
  cross-platform callers compile without `cfg` branching.
- Re-exported the primitive from the `io_uring` module and `CancelOutcome`
  from the crate root.
- Unit coverage:
  - `cancel_by_user_data_cancels_inflight_poll` submits a PollAdd on an
    empty pipe, cancels it, asserts `CancelOutcome::Cancelled`.
  - `cancel_by_user_data_reports_not_found_for_unknown_tag` asserts
    `-ENOENT` is folded into `NotFound`, not surfaced as an error.
  - `cancel_all_by_fd_cancels_inflight_polls` cancels two PollAdds
    bulk-by-fd (skips cleanly on kernels < 5.19).
  - `CancelOutcome::from_cqe_result` decode parity for each variant
    plus error surface for other errnos.
  - Stub mirrors assert `Unsupported` and constant parity.
@github-actions github-actions Bot added the enhancement New feature or request label May 17, 2026
oferchen added a commit that referenced this pull request May 18, 2026
Adds a per-thread io_uring ring primitive next to the existing
SessionRingPool. Each calling thread lazily builds its own ring on
first acquire and holds it in thread-local storage, so the submit
and reap paths hold no lock.

- ThreadLocalRingPool, ThreadLocalRingLease in fast_io::io_uring.
- Stub mirrors on non-Linux so cross-platform call sites compile.
- Six unit tests covering same-thread fd reuse, distinct rings per
  thread, reentrant acquire rejection, two pools sharing a thread,
  clone sharing per-thread state, and setup-failure fallback.
- Design note updated to mark the primitive as implemented and to
  record why work stealing was rejected (stolen SQEs cannot use
  the owning ring's registered buffers or fixed-file table).

Existing SharedRing and per-file ring consumers (file_writer,
file_reader, disk_batch, registered_buffers) keep their current
single-owner shape. Migrating those consumers is gated on the bench
grid in docs/design/iouring-per-thread-rings.md section 5.
oferchen added a commit that referenced this pull request May 18, 2026
Scaffold the `IORING_OP_ASYNC_CANCEL` primitive now that the async daemon
listener (#4278), session ring pool (#4275), and per-thread rings (#4288)
have landed code paths that keep SQEs in-flight across abort decision
points. Until now, every io_uring helper followed `submit + wait + drain`
and never needed cancellation; the deferred design at
`docs/design/iouring-async-cancel.md` documented the trigger condition
this commit fulfils.

- New module `crates/fast_io/src/io_uring/cancel.rs` (Linux + `io_uring`
  feature gated):
  - `cancel_by_user_data(&mut IoUring, u64) -> io::Result<CancelOutcome>`
    submits a single `IORING_OP_ASYNC_CANCEL` SQE matched against the
    target SQE's `user_data` tag and reports the kernel's terminal
    outcome.
  - `cancel_all_by_fd(&mut IoUring, RawFd) -> io::Result<usize>` uses
    `IORING_ASYNC_CANCEL_FD | IORING_ASYNC_CANCEL_ALL` (kernel 5.19+) to
    cancel every in-flight SQE that touches the given fd.
  - `enum CancelOutcome { Cancelled, NotFound, AlreadyComplete }` maps
    the three race outcomes (`0`, `-ENOENT`, `-EALREADY`) to typed
    variants so callers route into the correct buffer reclaim path.
- Added `OpTag::Cancel` so cancel CQEs are demuxable from target CQEs
  sharing the same CQ; updated `SharedRing::reap` to flag a Cancel CQE
  as an InvalidData misuse rather than silently drop it.
- Added cross-platform UAPI constants `IORING_OP_ASYNC_CANCEL`,
  `ASYNC_CANCEL_MIN_KERNEL`, `ASYNC_CANCEL_FD_MIN_KERNEL` in
  `io_uring_common`, mirrored in the non-Linux stub.
- Stub module in `io_uring_stub.rs` returns `Unsupported` so
  cross-platform callers compile without `cfg` branching.
- Re-exported the primitive from the `io_uring` module and `CancelOutcome`
  from the crate root.
- Unit coverage:
  - `cancel_by_user_data_cancels_inflight_poll` submits a PollAdd on an
    empty pipe, cancels it, asserts `CancelOutcome::Cancelled`.
  - `cancel_by_user_data_reports_not_found_for_unknown_tag` asserts
    `-ENOENT` is folded into `NotFound`, not surfaced as an error.
  - `cancel_all_by_fd_cancels_inflight_polls` cancels two PollAdds
    bulk-by-fd (skips cleanly on kernels < 5.19).
  - `CancelOutcome::from_cqe_result` decode parity for each variant
    plus error surface for other errnos.
  - Stub mirrors assert `Unsupported` and constant parity.
oferchen added a commit that referenced this pull request May 18, 2026
Adds a per-thread io_uring ring primitive next to the existing
SessionRingPool. Each calling thread lazily builds its own ring on
first acquire and holds it in thread-local storage, so the submit
and reap paths hold no lock.

- ThreadLocalRingPool, ThreadLocalRingLease in fast_io::io_uring.
- Stub mirrors on non-Linux so cross-platform call sites compile.
- Six unit tests covering same-thread fd reuse, distinct rings per
  thread, reentrant acquire rejection, two pools sharing a thread,
  clone sharing per-thread state, and setup-failure fallback.
- Design note updated to mark the primitive as implemented and to
  record why work stealing was rejected (stolen SQEs cannot use
  the owning ring's registered buffers or fixed-file table).

Existing SharedRing and per-file ring consumers (file_writer,
file_reader, disk_batch, registered_buffers) keep their current
single-owner shape. Migrating those consumers is gated on the bench
grid in docs/design/iouring-per-thread-rings.md section 5.
oferchen added a commit that referenced this pull request May 18, 2026
Scaffold the `IORING_OP_ASYNC_CANCEL` primitive now that the async daemon
listener (#4278), session ring pool (#4275), and per-thread rings (#4288)
have landed code paths that keep SQEs in-flight across abort decision
points. Until now, every io_uring helper followed `submit + wait + drain`
and never needed cancellation; the deferred design at
`docs/design/iouring-async-cancel.md` documented the trigger condition
this commit fulfils.

- New module `crates/fast_io/src/io_uring/cancel.rs` (Linux + `io_uring`
  feature gated):
  - `cancel_by_user_data(&mut IoUring, u64) -> io::Result<CancelOutcome>`
    submits a single `IORING_OP_ASYNC_CANCEL` SQE matched against the
    target SQE's `user_data` tag and reports the kernel's terminal
    outcome.
  - `cancel_all_by_fd(&mut IoUring, RawFd) -> io::Result<usize>` uses
    `IORING_ASYNC_CANCEL_FD | IORING_ASYNC_CANCEL_ALL` (kernel 5.19+) to
    cancel every in-flight SQE that touches the given fd.
  - `enum CancelOutcome { Cancelled, NotFound, AlreadyComplete }` maps
    the three race outcomes (`0`, `-ENOENT`, `-EALREADY`) to typed
    variants so callers route into the correct buffer reclaim path.
- Added `OpTag::Cancel` so cancel CQEs are demuxable from target CQEs
  sharing the same CQ; updated `SharedRing::reap` to flag a Cancel CQE
  as an InvalidData misuse rather than silently drop it.
- Added cross-platform UAPI constants `IORING_OP_ASYNC_CANCEL`,
  `ASYNC_CANCEL_MIN_KERNEL`, `ASYNC_CANCEL_FD_MIN_KERNEL` in
  `io_uring_common`, mirrored in the non-Linux stub.
- Stub module in `io_uring_stub.rs` returns `Unsupported` so
  cross-platform callers compile without `cfg` branching.
- Re-exported the primitive from the `io_uring` module and `CancelOutcome`
  from the crate root.
- Unit coverage:
  - `cancel_by_user_data_cancels_inflight_poll` submits a PollAdd on an
    empty pipe, cancels it, asserts `CancelOutcome::Cancelled`.
  - `cancel_by_user_data_reports_not_found_for_unknown_tag` asserts
    `-ENOENT` is folded into `NotFound`, not surfaced as an error.
  - `cancel_all_by_fd_cancels_inflight_polls` cancels two PollAdds
    bulk-by-fd (skips cleanly on kernels < 5.19).
  - `CancelOutcome::from_cqe_result` decode parity for each variant
    plus error surface for other errnos.
  - Stub mirrors assert `Unsupported` and constant parity.
@oferchen
oferchen deleted the feat/iouring-per-thread-rings-2243 branch May 19, 2026 19:29
oferchen added a commit that referenced this pull request Aug 24, 2026
Adds a per-thread io_uring ring primitive next to the existing
SessionRingPool. Each calling thread lazily builds its own ring on
first acquire and holds it in thread-local storage, so the submit
and reap paths hold no lock.

- ThreadLocalRingPool, ThreadLocalRingLease in fast_io::io_uring.
- Stub mirrors on non-Linux so cross-platform call sites compile.
- Six unit tests covering same-thread fd reuse, distinct rings per
  thread, reentrant acquire rejection, two pools sharing a thread,
  clone sharing per-thread state, and setup-failure fallback.
- Design note updated to mark the primitive as implemented and to
  record why work stealing was rejected (stolen SQEs cannot use
  the owning ring's registered buffers or fixed-file table).

Existing SharedRing and per-file ring consumers (file_writer,
file_reader, disk_batch, registered_buffers) keep their current
single-owner shape. Migrating those consumers is gated on the bench
grid in docs/design/iouring-per-thread-rings.md section 5.
oferchen added a commit that referenced this pull request Aug 24, 2026
Scaffold the `IORING_OP_ASYNC_CANCEL` primitive now that the async daemon
listener (#4278), session ring pool (#4275), and per-thread rings (#4288)
have landed code paths that keep SQEs in-flight across abort decision
points. Until now, every io_uring helper followed `submit + wait + drain`
and never needed cancellation; the deferred design at
`docs/design/iouring-async-cancel.md` documented the trigger condition
this commit fulfils.

- New module `crates/fast_io/src/io_uring/cancel.rs` (Linux + `io_uring`
  feature gated):
  - `cancel_by_user_data(&mut IoUring, u64) -> io::Result<CancelOutcome>`
    submits a single `IORING_OP_ASYNC_CANCEL` SQE matched against the
    target SQE's `user_data` tag and reports the kernel's terminal
    outcome.
  - `cancel_all_by_fd(&mut IoUring, RawFd) -> io::Result<usize>` uses
    `IORING_ASYNC_CANCEL_FD | IORING_ASYNC_CANCEL_ALL` (kernel 5.19+) to
    cancel every in-flight SQE that touches the given fd.
  - `enum CancelOutcome { Cancelled, NotFound, AlreadyComplete }` maps
    the three race outcomes (`0`, `-ENOENT`, `-EALREADY`) to typed
    variants so callers route into the correct buffer reclaim path.
- Added `OpTag::Cancel` so cancel CQEs are demuxable from target CQEs
  sharing the same CQ; updated `SharedRing::reap` to flag a Cancel CQE
  as an InvalidData misuse rather than silently drop it.
- Added cross-platform UAPI constants `IORING_OP_ASYNC_CANCEL`,
  `ASYNC_CANCEL_MIN_KERNEL`, `ASYNC_CANCEL_FD_MIN_KERNEL` in
  `io_uring_common`, mirrored in the non-Linux stub.
- Stub module in `io_uring_stub.rs` returns `Unsupported` so
  cross-platform callers compile without `cfg` branching.
- Re-exported the primitive from the `io_uring` module and `CancelOutcome`
  from the crate root.
- Unit coverage:
  - `cancel_by_user_data_cancels_inflight_poll` submits a PollAdd on an
    empty pipe, cancels it, asserts `CancelOutcome::Cancelled`.
  - `cancel_by_user_data_reports_not_found_for_unknown_tag` asserts
    `-ENOENT` is folded into `NotFound`, not surfaced as an error.
  - `cancel_all_by_fd_cancels_inflight_polls` cancels two PollAdds
    bulk-by-fd (skips cleanly on kernels < 5.19).
  - `CancelOutcome::from_cqe_result` decode parity for each variant
    plus error surface for other errnos.
  - Stub mirrors assert `Unsupported` and constant parity.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant