1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//! 0.9.4 — Linux io_uring kernel-feature probe.
//!
//! Probes which of the elite-tier setup flags
//! (`IORING_SETUP_COOP_TASKRUN`, `IORING_SETUP_SINGLE_ISSUER`,
//! `IORING_SETUP_DEFER_TASKRUN`) the running kernel accepts, then
//! caches the result for the lifetime of the process. Ring
//! constructors elsewhere in the crate consult [`features()`] to
//! decide which flags to set on `io_uring::IoUring::builder()`
//! before calling `.build(queue_depth)`.
//!
//! ## Why probe once, cache forever
//!
//! Each `io_uring_setup(2)` call is a syscall — cheap, but
//! probing every time a ring is constructed wastes work. The
//! kernel cannot change feature support over the process
//! lifetime (a hot kernel upgrade would require a restart), so a
//! single probe at first ring construction is sufficient.
//!
//! ## Probe strategy
//!
//! We try a single ring construction with the most aggressive
//! flag set first. On `EINVAL` we strip the highest-version flag
//! and retry. The walk is:
//!
//! 1. `DEFER_TASKRUN | SINGLE_ISSUER | COOP_TASKRUN` (≥ 6.1)
//! 2. `SINGLE_ISSUER | COOP_TASKRUN` (≥ 6.0)
//! 3. `COOP_TASKRUN` (≥ 5.19)
//! 4. (no elite flags) (≤ 5.18)
//!
//! `DEFER_TASKRUN` is documented to **require** `SINGLE_ISSUER`,
//! so the two are tested together — there's no useful intermediate.
//!
//! ## What this is not
//!
//! - **Not** a probe for `IORING_SETUP_SQPOLL` or
//! `IORING_SETUP_IOPOLL`. Both require dedicated cores /
//! privilege configurations that vary too much per deployment
//! to enable by default; future patches may add opt-in
//! `Builder` knobs.
//! - **Not** a probe for `IORING_REGISTER_FILES` /
//! `IORING_REGISTER_BUFFERS`. Those are register-time, not
//! setup-time; the ring construction succeeds regardless and
//! the registration call decides feature support at use time.
//!
//! ## Test surface
//!
//! `cargo test --lib platform::iouring_features` validates the
//! probe runs without panicking and produces a coherent
//! [`IoUringFeatures`] value (every probed flag is independently
//! `bool`-typed; no impossible combinations are produced because
//! `defer_taskrun ⟹ single_issuer` is enforced by the probe).
use OnceLock;
/// Cached snapshot of which io_uring kernel features the host
/// supports. Populated on first call to [`features`]; immutable
/// thereafter.
pub
/// Returns the cached io_uring kernel-feature snapshot, probing
/// on first call.
///
/// The probe runs a single `io_uring_setup(2)` call with the
/// most aggressive flag set the kernel might accept, then strips
/// flags on `EINVAL` and retries. The probe itself opens and
/// immediately closes the ring; no resources are held across
/// calls.
///
/// Returns [`IoUringFeatures::default`] (all-false) on hosts
/// where every probed flag is rejected — the fallback behaviour
/// is identical to pre-0.9.4 (vanilla `IoUring::new`).
pub
/// Synchronous probe. Tries the most aggressive flag combination
/// first; strips on `EINVAL`. Always returns within microseconds
/// (each `io_uring_setup` is a single syscall).
/// Tries building a tiny (queue-depth 4) ring with the flags
/// applied by `cfg`. Returns `true` if construction succeeded,
/// `false` otherwise. The ring is dropped immediately.
/// Ring usage mode — selects which elite flags are safe to apply.
///
/// The 0.9.4 elite-flag set (`COOP_TASKRUN` / `SINGLE_ISSUER` /
/// `DEFER_TASKRUN`) was originally applied unconditionally, but
/// **two** of them turned out to be incompatible with the async
/// substrate's design:
///
/// 1. **`DEFER_TASKRUN`** (kernel ≥ 6.1) requires the application
/// to drive completion processing via explicit
/// `io_uring_enter(IORING_ENTER_GETEVENTS)` calls — the kernel
/// will NOT process completions in background task work. The
/// async substrate submits via `ring.submit()` (no GETEVENTS)
/// and sleeps on `AsyncFd::readable()` waiting for eventfd
/// signalling. Under `DEFER_TASKRUN`, the kernel never posts
/// CQEs, eventfd never fires, the async loop hangs forever.
///
/// 2. **`SINGLE_ISSUER`** (kernel ≥ 6.0) is kernel-enforced —
/// only the task that owns the ring may submit. The kernel's
/// notion of "task" is an OS-level thread (TID). Under tokio's
/// `current_thread` runtime this is safe — the owner-task
/// always runs on the same OS thread. Under tokio's
/// `multi_thread` runtime, work-stealing migrates tasks
/// between worker threads at every yield point. After
/// migration, the owner task submits from a different TID
/// than the one that called `io_uring_setup`, the kernel
/// returns `-EEXIST`, the SQE is never processed, the CQE is
/// never posted, and the async loop hangs.
///
/// The sync owner-thread ring (`linux_iouring.rs`) is unaffected
/// by either: it uses `submit_and_wait(n)` (= `io_uring_enter(GETEVENTS=n)`)
/// which drives completions explicitly, and runs on a dedicated
/// `std::thread::spawn`-ed OS thread that never migrates.
///
/// Both bugs existed since 0.9.4 but were undetected until the
/// 0.9.6 feature-matrix CI exercised the async tests on a kernel
/// with the flags supported (≥ 6.1).
// Variant usage is feature-gated: Sync used by
// linux_iouring.rs (cfg target_os=linux); Async used by
// completion_driver.rs (cfg target_os=linux + feature=async).
// The default-features Linux build constructs Sync only,
// so dead_code would fire on Async without this allow.
pub
/// Applies the cached feature set to an `io_uring::Builder`,
/// enabling exactly the flags that the host kernel supports AND
/// that are safe for the requested [`RingMode`].
///
/// Callers use this from their ring constructors:
///
/// ```text
/// let mut b = io_uring::IoUring::builder();
/// iouring_features::apply(&mut b, RingMode::Sync);
/// let ring = b.build(queue_depth)?;
/// ```
///
/// The builder is mutated in place; the caller retains
/// ownership and may chain additional setup methods after this
/// call. Idempotent — calling `apply` twice is a no-op (each
/// flag is set once at the bit level).
pub