| 0/0 |
2026/11/19 05:36 |
flow |
assessment-kcsan |
15m
Results: map[Benign:true Explanation:### Race Summary
A data race occurs between `io_init_req()` (writing to `ctx->int_flags`) and
`__io_commit_cqring_flush()` (reading from `ctx->int_flags`).
- **Writer (Task 3754, SQPOLL thread):** Executes `io_submit_sqes() ->
io_init_req()`, which sets `ctx->int_flags |= IO_RING_F_DRAIN_DISABLED` (bit 10)
under `ctx->uring_lock`.
- **Reader (Task 3758, io-wq worker thread):** Executes `io_issue_sqe() ->
io_req_complete_post() -> io_cq_unlock_post() -> io_commit_cqring_flush() ->
__io_commit_cqring_flush()`, reading `ctx->int_flags` to test
`IO_RING_F_OFF_TIMEOUT_USED` (bit 3) as well as `IO_RING_F_POLL_ACTIVATED` (bit
9) and `IO_RING_F_HAS_EVFD` (bit 5).
---
### Synchronization Context
- **Writer:** Holds `mutex_lock(&ctx->uring_lock)`. In `io_uring`, all writes to
`ctx->int_flags` are strictly serialized by `ctx->uring_lock`. Consequently,
there is only ever a single concurrent writer modifying `ctx->int_flags`,
precluding any concurrent write clobbering.
- **Reader:** In `io_cq_unlock_post()`, completions are posted and
`ctx->completion_lock` is unlocked before calling `io_commit_cqring_flush()`.
Neither `ctx->completion_lock` nor `ctx->uring_lock` is held. The fast-path
check `io_commit_cqring_flush()` already uses `data_race(ctx->int_flags) &
SHOULD_FLUSH_MASK` (added in commit `8c557449199e`), but the invoked out-of-line
helper `__io_commit_cqring_flush()` reads `ctx->int_flags` using plain
unannotated reads.
---
### Final Classification: BENIGN
---
### Reasoning
1. **Single-Writer Word-Sized Scalar Access:** `ctx->int_flags` is an aligned
32-bit `unsigned int`. Writes are serialized by `ctx->uring_lock`. Loads and
stores of aligned 32-bit values are single-copy atomic and will not tear on
supported architectures.
2. **Independent Bitfield Bits:** The writer is updating
`IO_RING_F_DRAIN_DISABLED` (bit 10). The reader in `__io_commit_cqring_flush()`
tests `IO_RING_F_POLL_ACTIVATED` (bit 9), `IO_RING_F_OFF_TIMEOUT_USED` (bit 3),
and `IO_RING_F_HAS_EVFD` (bit 5). The value of bit 10 is completely independent
and does not affect the flags tested by the reader.
3. **Safe Downstream Synchronization:** Even when the bits tested by
`__io_commit_cqring_flush()` change:
- `IO_RING_F_POLL_ACTIVATED`: Wakes `ctx->poll_wq`, which internally
serializes on the waitqueue spinlock.
- `IO_RING_F_OFF_TIMEOUT_USED`: Calls `io_flush_timeouts()`, which takes
`raw_spin_lock_irq(&ctx->timeout_lock)`.
- `IO_RING_F_HAS_EVFD`: Calls `io_eventfd_signal()`, which safely validates
`ctx->io_ev_fd` under `rcu_read_lock()`.
4. **No Loop or Hoisting Hazard:** The read is not inside a polling or spin
loop; it is a straight-line sequence of checks after CQ ring events are
committed. Stale reads simply defer flushing to the next completion or task-work
cycle.
Commit `8c557449199e` ("io_uring: mark known and harmless racy ctx->int_flags
uses") intentionally annotated `ctx->int_flags` reads in
`io_commit_cqring_flush()`, `io_uring_poll()`, and `io_req_local_work_add()`,
but omitted annotating the re-reads inside `__io_commit_cqring_flush()`.
---
### Recommended Annotations
Pass the flags read in `io_commit_cqring_flush()` into
`__io_commit_cqring_flush()`, or wrap the reads in `__io_commit_cqring_flush()`
with `data_race()`:
```diff
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -530,11 +530,13 @@ static inline bool io_drain_req_completed(struct
io_ring_ctx *ctx)
void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
{
- if (ctx->int_flags & IO_RING_F_POLL_ACTIVATED)
+ unsigned int flags = data_race(ctx->int_flags);
+
+ if (flags & IO_RING_F_POLL_ACTIVATED)
io_poll_wq_wake(ctx);
- if (ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED)
+ if (flags & IO_RING_F_OFF_TIMEOUT_USED)
io_flush_timeouts(ctx);
- if (ctx->int_flags & IO_RING_F_HAS_EVFD)
+ if (flags & IO_RING_F_HAS_EVFD)
io_eventfd_signal(ctx, true, false);
}
``` FailureDetectableBy:]
|
| 1/1 |
2026/11/19 05:36 |
action |
kernel-checkouter |
0m
Results: map[KernelSrc:/app/workdir/cache/src/6d0c4d912ac4c28457116fbfe153047fb3052961/data]
|
| 2/1 |
2026/11/19 05:36 |
action |
kernel-builder |
0m
Results: map[KernelObj:/app/workdir/cache/build/4fc9cb093f0faa8b5951a9949e7cc5ff4ce76564/data]
|
| 3/1 |
2026/11/19 05:36 |
action |
codesearch-prepare |
0m
Results: map[Index:codesearch-index]
|
| 4/1 |
2026/11/19 05:36 |
agent |
expert |
15m
Model:
Results: map[Benign:true FailureDetectableBy:]
Instruction: # KCSAN Data Race Severity Analysis Protocol
You are an expert Linux kernel concurrency engineer analyzing a Kernel
Concurrency Sanitizer (KCSAN) data race report to determine if it is
**BENIGN** or **HARMFUL**.
## 1. CLASSIFICATIONS
### **BENIGN (Truly Benign)**
The logic is sound and structurally tolerant to compiler optimizations or
stale reads (or benign tearing on diagnostics/stats).
- **Diagnostics/Stats:** Reads or non-critical updates used only for `/proc`,
`/sys`, counters, timestamps, or `pr_info`.
- **Heuristic Hints:** A "hint" flag or threshold where an old value only
causes a slightly delayed update or a sub-optimal but safe fast-path.
- **Single-Writer Flag Updates:** A single writer updating flags where the
concurrent read is a simple bitwise check (e.g., `flags & MASK`). These are
historically tolerated, assuming neither "Fused Accesses" nor "Ordering
Violations" are relevant in this context.
- **Marked Reloads & Lock Re-checks:** A load feeding into a `cmpxchg()` loop,
checked against a later `READ_ONCE()` reload, or re-verified under a lock
(assuming no "Ordering Violations" on the lockless fast-path).
- **Safe Overwrites & Plain Scalar Accesses:** Writing the same value already
present, or aligned word-sized scalar accesses (e.g., `read (marked)` vs
plain `write`) where observing either the old or new value is safe.
### **HARMFUL (Logic Bug or Marking Required)**
The race causes incorrect behavior due to a synchronization failure or
because missing annotations allow the compiler or CPU to break the algorithm.
**Marking Required for Correctness:**
Do **not** classify a race as HARMFUL merely because an access lacks
`READ_ONCE()`/`WRITE_ONCE()`; you must prove one of these concrete hazards:
- **Fused Accesses:** The compiler hoists a plain load out of a tight
barrier-free spin loop causing a livelock (note: non-inlined calls,
`schedule()`, and locks act as compiler barriers), or reloads a pointer/index
between check and use (TOCTOU).
- **Torn Accesses:** An access larger than word size (e.g., 64-bit on 32-bit
arch, or multi-word structs; aligned `<= sizeof(long)` scalars and pointers
do not tear) is split into multiple non-atomic accesses. Note that
`READ_ONCE()` does **not** guarantee atomicity for 64-bit variables on 32-bit
architectures.
- **Ordering Violations:** The race breaks a required "happens-before"
relationship between dependent memory accesses (requires primitives with
implied or explicit memory barriers).
**Logic Bugs:**
A fundamental synchronization failure. Marking accesses will **not** fix it;
the logic itself must change.
- **Pointers/Lifecycle:** The racing variable is a pointer being dereferenced
or a refcount governing object lifecycle (Use-After-Free risk).
- **Control Flow:** The variable guards a critical section, memory allocation,
or hardware command.
- **Bitfields:** Concurrent writes to different bits in the same word.
Compilers often use non-atomic read-modify-write sequences, meaning a
write to `bit_A` can "clobber" a concurrent write to `bit_B`. However,
do not blindly assume all bitfield accesses are harmful; you must prove
that a concurrent write actually clobbers another in a way that breaks
logic.
- **Complex Structures:** Races on shared lists, trees, or hashmaps.
- **Lossy Updates:** Concurrent plain RMW operations (e.g., `var++`) on
non-diagnostic variables where every increment must be preserved.
- **State Machines:** Races allowing a state machine to bypass transitions
or enter an invalid state.
- **Adjacent Unsynchronized Operations:** Consider races happening at the
same time. For example, if both threads execute `struct->has_elements = true;
list_add(node, &struct->list);`, the race on `has_elements` implies an
adjacent race on `list_head`, which is HARMFUL.
## 2. RESEARCH & ANALYSIS WORKFLOW
1. **Locate the Race:** Find the exact variables and functions in the stack
traces. Do not speculate about hypothetical compiler
behaviors or theoretical dangers (e.g., dismissing something as
"fundamentally unsafe") without tracing the actual data flow to a crash.
2. **Contextualize:** Identify held locks, RCU sections, or interrupt
contexts. Explain why the race is possible (e.g., "Thread A holds `lock`,
but Thread B is a lockless reader").
3. **Data Flow:** Follow the racing variable into its subsequent uses. If
the reader sees a stale or torn value, what branch is taken? Does it lead
to a pointer dereference or `BUG_ON()`? **You must prove harm via this
data flow.**
4. **Failure Sequencing (if HARMFUL):** Construct a two-column execution trace
demonstrating the failure.
*Example format:*
```text
CPU0 CPU1
function_a()
// lockless read
ptr = global_ptr
<PREEMPT>
function_b()
lock(&my_lock)
global_ptr = NULL
unlock(&my_lock)
kfree(ptr)
if (ptr)
*ptr = 1; // -> Use-After-Free!
```
## 3. OUTPUT FORMAT
- **Race Summary:** `[Function A]` vs `[Function B]` on `[Variable/Field]`.
- **Synchronization context:** List held locks and explain the lack of mutual
exclusion.
- **Final Classification:** **BENIGN** or **HARMFUL**.
**If BENIGN:**
- **Reasoning:** Briefly explain why the race is structurally tolerant to stale
reads (or benign tearing on diagnostics/stats).
- **Recommended Annotations:** Suggest appropriate annotations (e.g.,
`READ_ONCE()`, `WRITE_ONCE()`, `data_race()`).
**If HARMFUL:**
- **Failure Reasoning:** Explain and provide a two-column interleaving showing
exactly how the race leads to a failure such as corruption or kernel crash.
- **FailureDetectableBy:** One of:
- `"kasan"`: memory safety bug (use-after-free, out-of-bounds, double-free).
- `"kmsan"`: uninitialized memory read/exposure (e.g. missing publish/subscribe ordering).
- `"any"`: kernel crash/hang (`BUG_ON`, `WARN_ON`, NULL deref, GPF, deadlock).
- `"user"`: user-visible ABI/semantic corruption detectable by userspace without a kernel crash.
- `"none"`: purely internal kernel state/accounting bug without crash or user-visible invariant violation.
- **Recommended Fix (ONLY if fix is trivial):** Suggest a structural fix (e.g.,
"Hold `mapping->i_pages` lock", "Convert to `atomic_t`") or required memory
ordering annotations (e.g., "Wrap in `READ_ONCE()`", "Use
`smp_load_acquire()`").
Don't make assumptions about the kernel source code (it may be different from what you assume it is).
Extensively use the provided code access tools (codesearch-*, git-*, grepper, etc)
to examine the actual source code, and confirm any assumptions.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt:
The data race report is:
==================================================================
BUG: KCSAN: data-race in __io_commit_cqring_flush / io_submit_sqes
write to 0xffff88811c6db004 of 4 bytes by task 3754 on cpu 1:
io_init_req io_uring/io_uring.c:1939 [inline]
io_submit_sqe io_uring/io_uring.c:2044 [inline]
io_submit_sqes+0xcf3/0x12b0 io_uring/io_uring.c:2212
__io_sq_thread io_uring/sqpoll.c:252 [inline]
io_sq_thread+0xaaa/0x16f0 io_uring/sqpoll.c:369
ret_from_fork+0x146/0x330 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
read to 0xffff88811c6db004 of 4 bytes by task 3758 on cpu 0:
__io_commit_cqring_flush+0x8c/0x100 io_uring/io_uring.c:535
io_commit_cqring_flush io_uring/io_uring.h:519 [inline]
io_cq_unlock_post io_uring/io_uring.c:572 [inline]
io_req_complete_post io_uring/io_uring.c:1030 [inline]
io_issue_sqe+0xc28/0xd20 io_uring/io_uring.c:1546
io_wq_submit_work+0x64d/0x8f0 io_uring/io_uring.c:1677
io_worker_handle_work+0x688/0xd30 io_uring/io-wq.c:677
io_wq_worker+0x2be/0xcb0 io_uring/io-wq.c:768
ret_from_fork+0x146/0x330 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
Reported by Kernel Concurrency Sanitizer on:
CPU: 0 UID: 0 PID: 3758 Comm: iou-wrk-3754 Not tainted syzkaller #0 PREEMPT(lazy)
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================
Reply: ### Race Summary
A data race occurs between `io_init_req()` (writing to `ctx->int_flags`) and `__io_commit_cqring_flush()` (reading from `ctx->int_flags`).
- **Writer (Task 3754, SQPOLL thread):** Executes `io_submit_sqes() -> io_init_req()`, which sets `ctx->int_flags |= IO_RING_F_DRAIN_DISABLED` (bit 10) under `ctx->uring_lock`.
- **Reader (Task 3758, io-wq worker thread):** Executes `io_issue_sqe() -> io_req_complete_post() -> io_cq_unlock_post() -> io_commit_cqring_flush() -> __io_commit_cqring_flush()`, reading `ctx->int_flags` to test `IO_RING_F_OFF_TIMEOUT_USED` (bit 3) as well as `IO_RING_F_POLL_ACTIVATED` (bit 9) and `IO_RING_F_HAS_EVFD` (bit 5).
---
### Synchronization Context
- **Writer:** Holds `mutex_lock(&ctx->uring_lock)`. In `io_uring`, all writes to `ctx->int_flags` are strictly serialized by `ctx->uring_lock`. Consequently, there is only ever a single concurrent writer modifying `ctx->int_flags`, precluding any concurrent write clobbering.
- **Reader:** In `io_cq_unlock_post()`, completions are posted and `ctx->completion_lock` is unlocked before calling `io_commit_cqring_flush()`. Neither `ctx->completion_lock` nor `ctx->uring_lock` is held. The fast-path check `io_commit_cqring_flush()` already uses `data_race(ctx->int_flags) & SHOULD_FLUSH_MASK` (added in commit `8c557449199e`), but the invoked out-of-line helper `__io_commit_cqring_flush()` reads `ctx->int_flags` using plain unannotated reads.
---
### Final Classification: BENIGN
---
### Reasoning
1. **Single-Writer Word-Sized Scalar Access:** `ctx->int_flags` is an aligned 32-bit `unsigned int`. Writes are serialized by `ctx->uring_lock`. Loads and stores of aligned 32-bit values are single-copy atomic and will not tear on supported architectures.
2. **Independent Bitfield Bits:** The writer is updating `IO_RING_F_DRAIN_DISABLED` (bit 10). The reader in `__io_commit_cqring_flush()` tests `IO_RING_F_POLL_ACTIVATED` (bit 9), `IO_RING_F_OFF_TIMEOUT_USED` (bit 3), and `IO_RING_F_HAS_EVFD` (bit 5). The value of bit 10 is completely independent and does not affect the flags tested by the reader.
3. **Safe Downstream Synchronization:** Even when the bits tested by `__io_commit_cqring_flush()` change:
- `IO_RING_F_POLL_ACTIVATED`: Wakes `ctx->poll_wq`, which internally serializes on the waitqueue spinlock.
- `IO_RING_F_OFF_TIMEOUT_USED`: Calls `io_flush_timeouts()`, which takes `raw_spin_lock_irq(&ctx->timeout_lock)`.
- `IO_RING_F_HAS_EVFD`: Calls `io_eventfd_signal()`, which safely validates `ctx->io_ev_fd` under `rcu_read_lock()`.
4. **No Loop or Hoisting Hazard:** The read is not inside a polling or spin loop; it is a straight-line sequence of checks after CQ ring events are committed. Stale reads simply defer flushing to the next completion or task-work cycle.
Commit `8c557449199e` ("io_uring: mark known and harmless racy ctx->int_flags uses") intentionally annotated `ctx->int_flags` reads in `io_commit_cqring_flush()`, `io_uring_poll()`, and `io_req_local_work_add()`, but omitted annotating the re-reads inside `__io_commit_cqring_flush()`.
---
### Recommended Annotations
Pass the flags read in `io_commit_cqring_flush()` into `__io_commit_cqring_flush()`, or wrap the reads in `__io_commit_cqring_flush()` with `data_race()`:
```diff
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -530,11 +530,13 @@ static inline bool io_drain_req_completed(struct io_ring_ctx *ctx)
void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
{
- if (ctx->int_flags & IO_RING_F_POLL_ACTIVATED)
+ unsigned int flags = data_race(ctx->int_flags);
+
+ if (flags & IO_RING_F_POLL_ACTIVATED)
io_poll_wq_wake(ctx);
- if (ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED)
+ if (flags & IO_RING_F_OFF_TIMEOUT_USED)
io_flush_timeouts(ctx);
- if (ctx->int_flags & IO_RING_F_HAS_EVFD)
+ if (flags & IO_RING_F_HAS_EVFD)
io_eventfd_signal(ctx, true, false);
}
```
|
| 5/2 |
2026/11/19 05:36 |
llm |
expert |
0m
Model:
Tokens:
input: 6591
output: 112
thoughts: 613
Thoughts: **KCSAN Data Race in io_uring: A Deep Dive**
Okay, so KCSAN has flagged a data race. My initial thought is to dive straight into the code. This report is pointing to a race condition between `__io_commit_cqring_flush` and `io_submit_sqes`. Specifically, there's a 4-byte write to address `0xffff88811c6db004` by task 3754 on CPU 1, traced back through the `io_uring` submission path. We've got `io_init_req`, `io_submit_sqe`, eventually culminating in `io_submit_sqes`. This looks like the main submission thread is doing the dirty work.
Simultaneously, we have a 4-byte read from the *same* address by task 3758 on CPU 0. The read traces back through the completion path, starting at `__io_commit_cqring_flush` and flowing through `io_cq_unlock_post`, then into `io_req_complete_post`, and finally to `io_issue_sqe`. Looks like a worker thread is trying to complete this submission.
My understanding of the code tells me that `__io_commit_cqring_flush` handles the finalization of entries on the completion queue, while `io_submit_sqes` is deeply involved in the submission queue handling. The fact that the same memory location is being touched by both routines, especially in a concurrent manner, screams race condition. I suspect there's a lack of proper synchronization around this memory location. Either a missing lock, or possibly an incorrect memory barrier.
The next step is to carefully examine the source code around those specific lines, specifically in `io_uring/io_uring.c`, `io_uring/sqpoll.c`, `io_uring/io-wq.c`, and `io_uring/io_uring.h` to see exactly what's going on. I'll be looking for shared data structures, read-write patterns, and any potential atomicity issues. The challenge here is to determine *why* this memory is shared in this manner and what the intended access pattern *should* be. Then, I can pinpoint the necessary fix, which will likely involve a spinlock, mutex, or proper usage of atomic operations, or maybe even a memory barrier. Time to get my hands dirty.
|
| 6/2 |
2026/11/19 05:36 |
tool |
read-file |
0m
Args: map[File:io_uring/io_uring.c FirstLine:1925 LineCount:35]
Results: map[Contents:1925: */
1926: ctx->cached_sq_head++;
1927: }
1928:
1929: if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
1930: /* enforce forwards compatibility on users */
1931: if (sqe_flags & ~SQE_VALID_FLAGS)
1932: return io_init_fail_req(req, -EINVAL);
1933: if (sqe_flags & IOSQE_BUFFER_SELECT) {
1934: if (!def->buffer_select)
1935: return io_init_fail_req(req, -EOPNOTSUPP);
1936: req->buf_index = READ_ONCE(sqe->buf_group);
1937: }
1938: if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS)
1939: ctx->int_flags |= IO_RING_F_DRAIN_DISABLED;
1940: if (sqe_flags & IOSQE_IO_DRAIN) {
1941: if (ctx->int_flags & IO_RING_F_DRAIN_DISABLED)
1942: return io_init_fail_req(req, -EOPNOTSUPP);
1943: io_init_drain(ctx);
1944: }
1945: }
1946: if (unlikely(ctx->int_flags & (IO_RING_F_OP_RESTRICTED | IO_RING_F_DRAIN_ACTIVE | IO_RING_F_DRAIN_NEXT))) {
1947: if (!io_check_restriction(ctx, req, sqe_flags))
1948: return io_init_fail_req(req, -EACCES);
1949: /* knock it to the slow queue path, will be drained there */
1950: if (ctx->int_flags & IO_RING_F_DRAIN_ACTIVE)
1951: req->flags |= REQ_F_FORCE_ASYNC;
1952: /* if there is no link, we're at "next" request and need to drain */
1953: if (unlikely(ctx->int_flags & IO_RING_F_DRAIN_NEXT) && !ctx->submit_state.link.head) {
1954: ctx->int_flags &= ~IO_RING_F_DRAIN_NEXT;
1955: ctx->int_flags |= IO_RING_F_DRAIN_ACTIVE;
1956: req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
1957: }
1958: }
1959:
]
|
| 7/2 |
2026/11/19 05:36 |
tool |
read-file |
0m
Args: map[File:io_uring/io_uring.c FirstLine:520 LineCount:40]
Results: map[Contents: 520: /* it runs alone, track its completion unless it's a link */
521: if (!de->req->link)
522: ctx->drain_req = de->req;
523: }
524: io_req_task_queue(de->req);
525: kfree(de);
526: first = false;
527: }
528: return drain_queued;
529: }
530:
531: void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
532: {
533: if (ctx->int_flags & IO_RING_F_POLL_ACTIVATED)
534: io_poll_wq_wake(ctx);
535: if (ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED)
536: io_flush_timeouts(ctx);
537: if (ctx->int_flags & IO_RING_F_HAS_EVFD)
538: io_eventfd_signal(ctx, true, false);
539: }
540:
541: static inline void __io_cq_lock(struct io_ring_ctx *ctx)
542: {
543: if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ))
544: spin_lock(&ctx->completion_lock);
545: }
546:
547: static inline void io_cq_lock(struct io_ring_ctx *ctx)
548: __acquires(ctx->completion_lock)
549: {
550: spin_lock(&ctx->completion_lock);
551: }
552:
553: static inline void __io_cq_unlock_post(struct io_ring_ctx *ctx)
554: {
555: io_commit_cqring(ctx);
556: if (!(ctx->int_flags & IO_RING_F_TASK_COMPLETE)) {
557: if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ))
558: spin_unlock(&ctx->completion_lock);
559: /* IOPOLL rings only need to wake up if it's also SQPOLL */
]
|
| 8/2 |
2026/11/19 05:36 |
tool |
read-file |
0m
Args: map[File:io_uring/io_uring.h FirstLine:510 LineCount:30]
Results: map[Contents: 510: wq_list_add_tail(&req->comp_list, &state->compl_reqs);
511: }
512:
513: #define SHOULD_FLUSH_MASK (IO_RING_F_OFF_TIMEOUT_USED | \
514: IO_RING_F_HAS_EVFD | IO_RING_F_POLL_ACTIVATED)
515:
516: static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx)
517: {
518: if (unlikely(data_race(ctx->int_flags) & SHOULD_FLUSH_MASK))
519: __io_commit_cqring_flush(ctx);
520: }
521:
522: static inline void io_get_task_refs(int nr)
523: {
524: struct io_uring_task *tctx = current->io_uring;
525:
526: tctx->cached_refs -= nr;
527: if (unlikely(tctx->cached_refs < 0))
528: io_task_refs_refill(tctx);
529: }
530:
531: static inline bool io_req_cache_empty(struct io_ring_ctx *ctx)
532: {
533: return !ctx->submit_state.free_list.next;
534: }
535:
536: extern struct kmem_cache *req_cachep;
537:
538: static inline struct io_kiocb *io_extract_req(struct io_ring_ctx *ctx)
539: {
]
|
| 9/2 |
2026/11/19 05:36 |
llm |
expert |
0m
Model:
Tokens:
input: 9705
output: 27
thoughts: 659
Thoughts: **Analyzing a Potential Data Race in `io_uring`'s Internal Flags**
Okay, so I'm looking at this `io_uring` code, and something's bugging me. Specifically, I see this structure `io_ring_ctx` with an `int_flags` field. Now, the initial thought process is to understand when the CQ ring needs flushing: the macro `SHOULD_FLUSH_MASK` includes flags like `IO_RING_F_OFF_TIMEOUT_USED`. `io_commit_cqring_flush` checks a potentially data-raced version of `int_flags`, and if a bit is set in the `SHOULD_FLUSH_MASK`, it calls `__io_commit_cqring_flush`. Inside that function, we have these conditional checks based on `int_flags`, triggering actions like waking a polling workqueue, flushing timeouts, or signaling an eventfd.
The "aha!" moment is that I see a write to `ctx->int_flags` where `IO_RING_F_DRAIN_DISABLED` is set, triggered by `IOSQE_CQE_SKIP_SUCCESS` in line 1939 of `io_uring.c`.
And I've noticed a potential data race. The flush function, `io_commit_cqring_flush` does use `data_race` when checking, but `__io_commit_cqring_flush` *directly* reads `ctx->int_flags` without a data race annotation, when checking for `IO_RING_F_OFF_TIMEOUT_USED`. This seems like a potential memory synchronization issue – a write is happening, and a read is happening without appropriate synchronization!
So, I'm thinking, is this just a missing annotation, or is there a genuine data race on `ctx->int_flags`? What other places modify `ctx->int_flags`? Are there concurrent writes happening to this variable that could lead to inconsistent behavior? This needs investigation. I need to exhaustively search for every access and write of `ctx->int_flags` in the code base, for example by using `codesearch-find-references` or `grepper`, to understand the whole picture.
|
| 10/2 |
2026/11/19 05:37 |
tool |
grepper |
0m
Args: map[Expression:int_flags PathPrefixes:[io_uring]]
Results: map[Output:
Full output is too long, showing 200 out of 251 lines.
Use more precise expression or PathPrefixes if possible.
Number of matching lines per file (8 files in total):
io_uring/eventfd.c:2
io_uring/io_uring.c:42
io_uring/io_uring.h:3
io_uring/msg_ring.c:1
io_uring/register.c:5
io_uring/tctx.c:1
io_uring/timeout.c:2
io_uring/tw.c:2
io_uring/eventfd.c=120=int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg,
--
io_uring/eventfd.c-151- ev_fd->eventfd_async = eventfd_async;
io_uring/eventfd.c:152: ctx->int_flags |= IO_RING_F_HAS_EVFD;
io_uring/eventfd.c-153- refcount_set(&ev_fd->refs, 1);
--
io_uring/eventfd.c=159=int io_eventfd_unregister(struct io_ring_ctx *ctx)
--
io_uring/eventfd.c-165- if (ev_fd) {
io_uring/eventfd.c:166: ctx->int_flags &= ~IO_RING_F_HAS_EVFD;
io_uring/eventfd.c-167- rcu_assign_pointer(ctx->io_ev_fd, NULL);
--
io_uring/io_uring.c=531=void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
io_uring/io_uring.c-532-{
io_uring/io_uring.c:533: if (ctx->int_flags & IO_RING_F_POLL_ACTIVATED)
io_uring/io_uring.c-534- io_poll_wq_wake(ctx);
io_uring/io_uring.c:535: if (ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED)
io_uring/io_uring.c-536- io_flush_timeouts(ctx);
io_uring/io_uring.c:537: if (ctx->int_flags & IO_RING_F_HAS_EVFD)
io_uring/io_uring.c-538- io_eventfd_signal(ctx, true, false);
--
io_uring/io_uring.c=541=static inline void __io_cq_lock(struct io_ring_ctx *ctx)
io_uring/io_uring.c-542-{
io_uring/io_uring.c:543: if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ))
io_uring/io_uring.c-544- spin_lock(&ctx->completion_lock);
--
io_uring/io_uring.c=553=static inline void __io_cq_unlock_post(struct io_ring_ctx *ctx)
--
io_uring/io_uring.c-555- io_commit_cqring(ctx);
io_uring/io_uring.c:556: if (!(ctx->int_flags & IO_RING_F_TASK_COMPLETE)) {
io_uring/io_uring.c:557: if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ))
io_uring/io_uring.c-558- spin_unlock(&ctx->completion_lock);
io_uring/io_uring.c-559- /* IOPOLL rings only need to wake up if it's also SQPOLL */
io_uring/io_uring.c:560: if (!(ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL))
io_uring/io_uring.c-561- io_cqring_wake(ctx);
--
io_uring/io_uring.c=914=void io_add_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags)
--
io_uring/io_uring.c-916- lockdep_assert_held(&ctx->uring_lock);
io_uring/io_uring.c:917: lockdep_assert(ctx->int_flags & IO_RING_F_LOCKLESS_CQ);
io_uring/io_uring.c-918-
--
io_uring/io_uring.c=931=bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags)
--
io_uring/io_uring.c-946-
io_uring/io_uring.c:947: if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) {
io_uring/io_uring.c-948- spin_lock(&ctx->completion_lock);
--
io_uring/io_uring.c=963=bool io_req_post_cqe32(struct io_kiocb *req, struct io_uring_cqe cqe[2])
--
io_uring/io_uring.c-971- cqe[0].user_data = req->cqe.user_data;
io_uring/io_uring.c:972: if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) {
io_uring/io_uring.c-973- spin_lock(&ctx->completion_lock);
--
io_uring/io_uring.c=1000=static void io_req_complete_post(struct io_kiocb *req, unsigned issue_flags)
--
io_uring/io_uring.c-1018- */
io_uring/io_uring.c:1019: if ((ctx->int_flags & IO_RING_F_LOCKLESS_CQ) ||
io_uring/io_uring.c-1020- (req->flags & (REQ_F_REISSUE | REQ_F_IO_DRAIN))) {
--
io_uring/io_uring.c=1244=void __io_submit_flush_completions(struct io_ring_ctx *ctx)
--
io_uring/io_uring.c-1275- unlikely(!io_fill_cqe_req(ctx, req))) {
io_uring/io_uring.c:1276: if (ctx->int_flags & IO_RING_F_LOCKLESS_CQ)
io_uring/io_uring.c-1277- io_cqe_overflow(ctx, &req->cqe, &req->big_cqe);
--
io_uring/io_uring.c-1283-
io_uring/io_uring.c:1284: if (unlikely(ctx->int_flags & IO_RING_F_DRAIN_ACTIVE) && ctx->drain_req)
io_uring/io_uring.c-1285- drain_done = io_drain_req_completed(ctx);
--
io_uring/io_uring.c-1291-
io_uring/io_uring.c:1292: if (unlikely(ctx->int_flags & IO_RING_F_DRAIN_ACTIVE)) {
io_uring/io_uring.c-1293- /* a drain request started from the list keeps the drain going */
--
io_uring/io_uring.c-1296- if (drain_done && list_empty(&ctx->defer_list))
io_uring/io_uring.c:1297: ctx->int_flags &= ~IO_RING_F_DRAIN_ACTIVE;
io_uring/io_uring.c-1298- }
--
io_uring/io_uring.c=1470=static __cold void io_drain_req(struct io_kiocb *req)
--
io_uring/io_uring.c-1490- if (!drain && list_empty(&ctx->defer_list))
io_uring/io_uring.c:1491: ctx->int_flags &= ~IO_RING_F_DRAIN_ACTIVE;
io_uring/io_uring.c-1492-}
--
io_uring/io_uring.c=1804=static void io_queue_sqe_fallback(struct io_kiocb *req)
--
io_uring/io_uring.c-1817- io_req_sqe_copy(req, IO_URING_F_INLINE);
io_uring/io_uring.c:1818: if (unlikely(req->ctx->int_flags & IO_RING_F_DRAIN_ACTIVE))
io_uring/io_uring.c-1819- io_drain_req(req);
--
io_uring/io_uring.c=1830=static inline bool io_check_restriction(struct io_ring_ctx *ctx,
--
io_uring/io_uring.c-1833-{
io_uring/io_uring.c:1834: if (!(ctx->int_flags & IO_RING_F_OP_RESTRICTED))
io_uring/io_uring.c-1835- return true;
--
io_uring/io_uring.c=1850=static void io_init_drain(struct io_ring_ctx *ctx)
--
io_uring/io_uring.c-1853-
io_uring/io_uring.c:1854: ctx->int_flags |= IO_RING_F_DRAIN_ACTIVE;
io_uring/io_uring.c-1855- if (head) {
--
io_uring/io_uring.c-1863- head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
io_uring/io_uring.c:1864: ctx->int_flags |= IO_RING_F_DRAIN_NEXT;
io_uring/io_uring.c-1865- }
--
io_uring/io_uring.c=1875=static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
--
io_uring/io_uring.c-1938- if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS)
io_uring/io_uring.c:1939: ctx->int_flags |= IO_RING_F_DRAIN_DISABLED;
io_uring/io_uring.c-1940- if (sqe_flags & IOSQE_IO_DRAIN) {
io_uring/io_uring.c:1941: if (ctx->int_flags & IO_RING_F_DRAIN_DISABLED)
io_uring/io_uring.c-1942- return io_init_fail_req(req, -EOPNOTSUPP);
--
io_uring/io_uring.c-1945- }
io_uring/io_uring.c:1946: if (unlikely(ctx->int_flags & (IO_RING_F_OP_RESTRICTED | IO_RING_F_DRAIN_ACTIVE | IO_RING_F_DRAIN_NEXT))) {
io_uring/io_uring.c-1947- if (!io_check_restriction(ctx, req, sqe_flags))
--
io_uring/io_uring.c-1949- /* knock it to the slow queue path, will be drained there */
io_uring/io_uring.c:1950: if (ctx->int_flags & IO_RING_F_DRAIN_ACTIVE)
io_uring/io_uring.c-1951- req->flags |= REQ_F_FORCE_ASYNC;
io_uring/io_uring.c-1952- /* if there is no link, we're at "next" request and need to drain */
io_uring/io_uring.c:1953: if (unlikely(ctx->int_flags & IO_RING_F_DRAIN_NEXT) && !ctx->submit_state.link.head) {
io_uring/io_uring.c:1954: ctx->int_flags &= ~IO_RING_F_DRAIN_NEXT;
io_uring/io_uring.c:1955: ctx->int_flags |= IO_RING_F_DRAIN_ACTIVE;
io_uring/io_uring.c-1956- req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
--
io_uring/io_uring.c=2372=static __cold void io_activate_pollwq_cb(struct callback_head *cb)
--
io_uring/io_uring.c-2377- mutex_lock(&ctx->uring_lock);
io_uring/io_uring.c:2378: ctx->int_flags |= IO_RING_F_POLL_ACTIVATED;
io_uring/io_uring.c-2379- mutex_unlock(&ctx->uring_lock);
--
io_uring/io_uring.c=2389=__cold void io_activate_pollwq(struct io_ring_ctx *ctx)
--
io_uring/io_uring.c-2392- /* already activated or in progress */
io_uring/io_uring.c:2393: if ((ctx->int_flags & IO_RING_F_POLL_ACTIVATED) || ctx->poll_wq_task_work.func)
io_uring/io_uring.c-2394- goto out;
io_uring/io_uring.c:2395: if (WARN_ON_ONCE(!(ctx->int_flags & IO_RING_F_TASK_COMPLETE)))
io_uring/io_uring.c-2396- goto out;
--
io_uring/io_uring.c=2411=static __poll_t io_uring_poll(struct file *file, poll_table *wait)
--
io_uring/io_uring.c-2415-
io_uring/io_uring.c:2416: if (unlikely(!(data_race(ctx->int_flags) & IO_RING_F_POLL_ACTIVATED)))
io_uring/io_uring.c-2417- io_activate_pollwq(ctx);
--
io_uring/io_uring.c=2852=SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
--
io_uring/io_uring.c-2908- if (flags & IORING_ENTER_GETEVENTS) {
io_uring/io_uring.c:2909: if (ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL)
io_uring/io_uring.c-2910- goto iopoll_locked;
--
io_uring/io_uring.c-2923-
io_uring/io_uring.c:2924: if (ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL) {
io_uring/io_uring.c-2925- /*
--
io_uring/io_uring.c=3217=static void io_ctx_restriction_clone(struct io_ring_ctx *ctx,
--
io_uring/io_uring.c-3225- if (dst->op_registered)
io_uring/io_uring.c:3226: ctx->int_flags |= IO_RING_F_OP_RESTRICTED;
io_uring/io_uring.c-3227- if (dst->reg_registered)
io_uring/io_uring.c:3228: ctx->int_flags |= IO_RING_F_REG_RESTRICTED |
io_uring/io_uring.c-3229- IO_RING_F_RESTRICT_INHERITED;
--
io_uring/io_uring.c=3232=static __cold int io_uring_create(struct io_ctx_config *config)
--
io_uring/io_uring.c-3255- !(ctx->flags & IORING_SETUP_IOPOLL))
io_uring/io_uring.c:3256: ctx->int_flags |= IO_RING_F_TASK_COMPLETE;
io_uring/io_uring.c-3257-
io_uring/io_uring.c:3258: if ((ctx->int_flags & IO_RING_F_TASK_COMPLETE) ||
io_uring/io_uring.c-3259- (ctx->flags & IORING_SETUP_IOPOLL))
io_uring/io_uring.c:3260: ctx->int_flags |= IO_RING_F_LOCKLESS_CQ;
io_uring/io_uring.c-3261-
--
io_uring/io_uring.c-3265- */
io_uring/io_uring.c:3266: if (!(ctx->int_flags & IO_RING_F_TASK_COMPLETE))
io_uring/io_uring.c:3267: ctx->int_flags |= IO_RING_F_POLL_ACTIVATED;
io_uring/io_uring.c-3268-
--
io_uring/io_uring.c-3276- !(ctx->flags & IORING_SETUP_SQPOLL))
io_uring/io_uring.c:3277: ctx->int_flags |= IO_RING_F_SYSCALL_IOPOLL;
io_uring/io_uring.c-3278-
io_uring/io_uring.c-3279- if (in_compat_syscall())
io_uring/io_uring.c:3280: ctx->int_flags |= IO_RING_F_COMPAT;
io_uring/io_uring.c-3281- if (!ns_capable_noaudit(&init_user_ns, CAP_IPC_LOCK))
--
io_uring/io_uring.h=222=static inline void io_lockdep_assert_cq_locked(struct io_ring_ctx *ctx)
--
io_uring/io_uring.h-231- lockdep_assert_held(&ctx->uring_lock);
io_uring/io_uring.h:232: } else if (!(ctx->int_flags & IO_RING_F_TASK_COMPLETE)) {
io_uring/io_uring.h-233- lockdep_assert_held(&ctx->completion_lock);
--
io_uring/io_uring.h=247=static inline bool io_is_compat(struct io_ring_ctx *ctx)
io_uring/io_uring.h-248-{
io_uring/io_uring.h:249: return IS_ENABLED(CONFIG_COMPAT) && unlikely(ctx->int_flags & IO_RING_F_COMPAT);
io_uring/io_uring.h-250-}
--
io_uring/io_uring.h=516=static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx)
io_uring/io_uring.h-517-{
]
|
| 11/2 |
2026/11/19 05:37 |
llm |
expert |
0m
Model:
Tokens:
input: 15750
output: 74
thoughts: 30
Thoughts: Here's my take on the remaining `io_uring` matches, thinking as the source text would:
**Checking Remaining `io_uring` Matches**
Okay, time to run through the remaining files. I need to make sure I've got a comprehensive picture. Let's dig into `io_uring/register.c`, `timeout.c`, `tw.c`, and `msg_ring.c`. I need to ensure there are no loose ends, no potential regressions or overlooked corner cases within these remaining key areas. Let's see what gems they might be hiding!
|
| 12/2 |
2026/11/19 05:37 |
tool |
grepper |
0m
Args: map[Expression:int_flags PathPrefixes:[io_uring/register.c io_uring/timeout.c io_uring/tw.c io_uring/msg_ring.c io_uring/tctx.c]]
Results: map[Output:io_uring/msg_ring.c=68=static inline bool io_msg_need_remote(struct io_ring_ctx *target_ctx)
io_uring/msg_ring.c-69-{
io_uring/msg_ring.c:70: return target_ctx->int_flags & IO_RING_F_TASK_COMPLETE;
io_uring/msg_ring.c-71-}
--
io_uring/register.c=167=static __cold int io_register_restrictions(struct io_ring_ctx *ctx,
--
io_uring/register.c-194- if (ctx->restrictions.op_registered)
io_uring/register.c:195: ctx->int_flags |= IO_RING_F_OP_RESTRICTED;
io_uring/register.c-196- if (ctx->restrictions.reg_registered)
io_uring/register.c:197: ctx->int_flags |= IO_RING_F_REG_RESTRICTED;
io_uring/register.c-198- return 0;
--
io_uring/register.c=352=static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
--
io_uring/register.c-394- ctx->iowq_limits[i] = new_count[i];
io_uring/register.c:395: ctx->int_flags |= IO_RING_F_IOWQ_LIMITS_SET;
io_uring/register.c-396-
--
io_uring/register.c=800=static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
--
io_uring/register.c-821- */
io_uring/register.c:822: if ((ctx->int_flags & IO_RING_F_REG_RESTRICTED) &&
io_uring/register.c-823- (!(ctx->flags & IORING_SETUP_R_DISABLED) ||
io_uring/register.c:824: (ctx->int_flags & IO_RING_F_RESTRICT_INHERITED))) {
io_uring/register.c-825- opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
--
io_uring/tctx.c=145=int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
--
io_uring/tctx.c-156-
io_uring/tctx.c:157: if (data_race(ctx->int_flags) & IO_RING_F_IOWQ_LIMITS_SET) {
io_uring/tctx.c-158- unsigned int limits[2];
--
io_uring/timeout.c=600=static int __io_timeout_prep(struct io_kiocb *req,
--
io_uring/timeout.c-630- timeout->off = off;
io_uring/timeout.c:631: if (unlikely(off && !(req->ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED)))
io_uring/timeout.c:632: req->ctx->int_flags |= IO_RING_F_OFF_TIMEOUT_USED;
io_uring/timeout.c-633- /*
--
io_uring/tw.c=182=void io_req_local_work_add(struct io_kiocb *req, unsigned flags)
--
io_uring/tw.c-205- io_ctx_mark_taskrun(ctx);
io_uring/tw.c:206: if (data_race(ctx->int_flags) & IO_RING_F_HAS_EVFD)
io_uring/tw.c-207- io_eventfd_signal(ctx, false, flags & IOU_F_TWQ_IN_WAKE);
io_uring/tw.c-208- /* whoever sets the flag wakes ->poll_wq afterwards */
io_uring/tw.c:209: if (data_race(ctx->int_flags) & IO_RING_F_POLL_ACTIVATED)
io_uring/tw.c-210- io_local_work_wake_poll(ctx, flags);
]
|
| 13/2 |
2026/11/19 05:37 |
llm |
expert |
0m
Model:
Tokens:
input: 17064
output: 24
thoughts: 182
Thoughts: **Analyzing the Flushing Mechanism in io_uring**
Okay, let's dive into this `io_uring` flushing logic. First, I'm examining the `SHOULD_FLUSH_MASK` definition. It's a bitmask comprising `IO_RING_F_OFF_TIMEOUT_USED`, `IO_RING_F_HAS_EVFD`, and `IO_RING_F_POLL_ACTIVATED`. Seems like these flags collectively trigger a flush operation. The core is in `io_commit_cqring_flush` - it checks if the `data_race` of `ctx->int_flags` has any of the `SHOULD_FLUSH_MASK` bits set. If so, it calls the internal `__io_commit_cqring_flush` function. Makes sense, it's an optimization that only does the flush if necessary.
Now I'll grab the git log. I want to understand *why* this mask and these functions were introduced and how they've evolved. The commit history for `SHOULD_FLUSH_MASK`, `io_commit_cqring_flush`, and `IO_RING_F_OFF_TIMEOUT_USED` is key. It will highlight the reasoning behind the design decisions and potential performance considerations.
|
| 14/2 |
2026/11/19 05:37 |
tool |
git-log |
10m
Error:
Args: map[CodeRegexp:SHOULD_FLUSH_MASK]
Results:
|
| 15/2 |
2026/11/19 05:47 |
llm |
expert |
0m
Model:
Tokens:
input: 17576
output: 33
thoughts: 18
Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience:
**Adding `PathPrefix="io_uring"` to `git-log`: Considerations and Rationale**
Alright, I'm chewing on this. Let's add `PathPrefix="io_uring"` to `git-log`. This feels like a good, targeted way to zero in on changes related to that subsystem. It's not a generic wildcard, and it's not a complete file path (which could be too verbose). `io_uring` itself is pretty self-explanatory, and this should give me a tight, focused search space. I'm imagining this'll be especially useful for pinpointing commits where the core `io_uring` code, build processes, or even documentation relevant to this subsystem were touched. We can then easily navigate the code, understand when changes were made, and how this relates to our understanding. This isn't just about finding the initial commit or the biggest structural change; this allows us to trace any and all history relevant to it.
|
| 16/2 |
2026/11/19 05:47 |
tool |
git-log |
0m
Args: map[CodeRegexp:SHOULD_FLUSH_MASK PathPrefix:io_uring]
Results: map[Output:8c557449199e io_uring: mark known and harmless racy ctx->int_flags uses
]
|
| 17/2 |
2026/11/19 05:47 |
llm |
expert |
0m
Model:
Tokens:
input: 17891
output: 27
thoughts: 62
Thoughts: Here's my take on that commit, presented as a summary of my internal thought process:
**Diving into that io_uring Commit**
Alright, let's take a look at this commit, `8c557449199e` – that's a familiar-looking commit hash, and it mentions `io_uring`, which always grabs my attention. I should use `git show` to examine the full context of this commit. I need to understand what's happening. I expect this commit addresses some concurrency concerns. I'm going to carefully review the code changes and the rationale behind marking specific `ctx->int_flags` accesses as "harmlessly racy." That phrase itself is interesting; it suggests a trade-off. We're probably dealing with a performance optimization where the potential for a race condition is deemed acceptable in the specific circumstances. I need to understand *why* it's acceptable. Is it because the window for the race is incredibly narrow? Or perhaps the consequences of the race are benign enough to warrant the optimization? Time to dig into the details and the surrounding code. My expertise tells me there's usually a cost to these types of changes, so understanding the trade-off is paramount.
|
| 18/2 |
2026/11/19 05:47 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 8c557449199e8267bc969ae7e1d70b343b6a646d
Author: Jens Axboe <axboe@kernel.dk>
Date: Sat Mar 14 08:46:17 2026 -0600
io_uring: mark known and harmless racy ctx->int_flags uses
There are a few of these, where flags are read outside of the
uring_lock, yet it's harmless to race on them.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index bfeb3bc3849d..fb5a263706be 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -2242,7 +2242,7 @@ static __poll_t io_uring_poll(struct file *file, poll_table *wait)
struct io_ring_ctx *ctx = file->private_data;
__poll_t mask = 0;
- if (unlikely(!(ctx->int_flags & IO_RING_F_POLL_ACTIVATED)))
+ if (unlikely(!(data_race(ctx->int_flags) & IO_RING_F_POLL_ACTIVATED)))
io_activate_pollwq(ctx);
/*
* provides mb() which pairs with barrier from wq_has_sleeper
diff --git a/io_uring/io_uring.h b/io_uring/io_uring.h
index 5cb1983043cd..91cf67b5d85b 100644
--- a/io_uring/io_uring.h
+++ b/io_uring/io_uring.h
@@ -470,11 +470,12 @@ static inline void io_req_complete_defer(struct io_kiocb *req)
wq_list_add_tail(&req->comp_list, &state->compl_reqs);
}
+#define SHOULD_FLUSH_MASK (IO_RING_F_OFF_TIMEOUT_USED | \
+ IO_RING_F_HAS_EVFD | IO_RING_F_POLL_ACTIVATED)
+
static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx)
{
- if (unlikely(ctx->int_flags & (IO_RING_F_OFF_TIMEOUT_USED |
- IO_RING_F_HAS_EVFD |
- IO_RING_F_POLL_ACTIVATED)))
+ if (unlikely(data_race(ctx->int_flags) & SHOULD_FLUSH_MASK))
__io_commit_cqring_flush(ctx);
}
diff --git a/io_uring/tw.c b/io_uring/tw.c
index 022fe8753c19..fdff81eebc95 100644
--- a/io_uring/tw.c
+++ b/io_uring/tw.c
@@ -222,7 +222,7 @@ void io_req_local_work_add(struct io_kiocb *req, unsigned flags)
if (!head) {
io_ctx_mark_taskrun(ctx);
- if (ctx->int_flags & IO_RING_F_HAS_EVFD)
+ if (data_race(ctx->int_flags) & IO_RING_F_HAS_EVFD)
io_eventfd_signal(ctx, false);
}
]
|
| 19/2 |
2026/11/19 05:47 |
llm |
expert |
0m
Model:
Tokens:
input: 19189
output: 38
thoughts: 585
Thoughts: **Analyzing Potential Data Races in io_uring's `int_flags`**
Okay, so I'm looking at commit `8c557449199e`, specifically the part where the author is marking certain uses of `ctx->int_flags` as harmless races. The commit message mentions this is due to flags being read outside the `uring_lock`. The code snippet in `io_uring.h` shows `io_commit_cqring_flush()` using `data_race()` with `ctx->int_flags` before calling `__io_commit_cqring_flush()`. My initial thought is, "Huh? They're using `data_race()` in the first function, but not the second? That seems inconsistent."
Specifically, in `__io_commit_cqring_flush()`, it seems to be reading `ctx->int_flags` directly: `if (ctx->int_flags & ...)` multiple times. This feels like a direct race, as it isn't protected by `data_race()`.
I'm thinking, "Is this just a missing annotation, or is there an actual problem?" I need to dig deeper. My thought process is to methodically analyze this. I'll need to understand:
1. **What** actually writes to `ctx->int_flags`? I need to track down all the potential modification points.
2. **What** locks or synchronization primitives, if any, are used to protect `ctx->int_flags`? Are they present where the writes occur?
3. **Could** these writes be concurrent? If so, what is the impact of those concurrent writes to `ctx->int_flags`?
4. **What** are the implications when `ctx->int_flags` is read concurrently with these writes? Do the reads cause a problem by observing partially modified flags, or worse?
Now, I'll need to go find the `io_ring_ctx` definition, most likely in `io_uring/io_uring.h`, to give me a head start on this. Then I will start tracing the writes.
|
| 20/2 |
2026/11/19 05:47 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:io_uring/io_uring.c Name:io_ring_ctx]
Results: map[SourceCode: 335: struct io_ring_ctx {
336: /* const or read-mostly hot data */
337: struct {
338: /* ring setup flags */
339: unsigned int flags;
340: /* internal state flags IO_RING_F_* flags , mostly read-only */
341: unsigned int int_flags;
342:
343: struct task_struct *submitter_task;
344: struct io_rings *rings;
345: /* cache of ->restrictions.bpf_filters->filters */
346: struct io_bpf_filter __rcu **bpf_filters;
347: struct percpu_ref refs;
348:
349: clockid_t clockid;
350: enum tk_offsets clock_offset;
351:
352: enum task_work_notify_mode notify_method;
353: unsigned sq_thread_idle;
354: } ____cacheline_aligned_in_smp;
355:
356: /* submission data */
357: struct {
358: struct mutex uring_lock;
359:
360: /*
361: * Ring buffer of indices into array of io_uring_sqe, which is
362: * mmapped by the application using the IORING_OFF_SQES offset.
363: *
364: * This indirection could e.g. be used to assign fixed
365: * io_uring_sqe entries to operations and only submit them to
366: * the queue when needed.
367: *
368: * The kernel modifies neither the indices array nor the entries
369: * array.
370: */
371: u32 *sq_array;
372: struct io_uring_sqe *sq_sqes;
373: unsigned cached_sq_head;
374: unsigned sq_entries;
375:
376: /*
377: * Fixed resources fast path, should be accessed only under
378: * uring_lock, and updated through io_uring_register(2)
379: */
380: atomic_t cancel_seq;
381:
382: /*
383: * ->iopoll_list is protected by the ctx->uring_lock for
384: * io_uring instances that don't use IORING_SETUP_SQPOLL.
385: * For SQPOLL, only the single threaded io_sq_thread() will
386: * manipulate the list, hence no extra locking is needed there.
387: */
388: bool poll_multi_queue;
389: struct list_head iopoll_list;
390:
391: /*
392: * Consumer cursor for ->work_list, protected by ->uring_lock.
393: * Deliberately kept away from the producer side of the queue,
394: * as it's written for every popped entry, and the producer
395: * cacheline is contended enough as it is.
396: */
397: struct llist_node *work_head;
398:
399: struct io_file_table file_table;
400: struct io_rsrc_data buf_table;
401: struct io_alloc_cache node_cache;
402: struct io_alloc_cache imu_cache;
403:
404: struct io_submit_state submit_state;
405:
406: /*
407: * Modifications are protected by ->uring_lock and ->mmap_lock.
408: * The buffer list's io mapped region should be stable once
409: * published.
410: */
411: struct xarray io_bl_xa;
412:
413: struct io_hash_table cancel_table;
414: struct io_alloc_cache apoll_cache;
415: struct io_alloc_cache netmsg_cache;
416: struct io_alloc_cache rw_cache;
417: struct io_alloc_cache cmd_cache;
418:
419: int (*loop_step)(struct iou_ctx *,
420: struct iou_loop_params *);
421:
422: /*
423: * Any cancelable uring_cmd is added to this list in
424: * ->uring_cmd() by io_uring_cmd_insert_cancelable()
425: */
426: struct hlist_head cancelable_uring_cmd;
427: /*
428: * For Hybrid IOPOLL, runtime in hybrid polling, without
429: * scheduling time
430: */
431: u64 hybrid_poll_time;
432: } ____cacheline_aligned_in_smp;
433:
434: struct {
435: /*
436: * We cache a range of free CQEs we can use, once exhausted it
437: * should go through a slower range setup, see __io_get_cqe()
438: */
439: struct io_uring_cqe *cqe_cached;
440: struct io_uring_cqe *cqe_sentinel;
441:
442: unsigned cached_cq_tail;
443: unsigned cq_entries;
444: struct io_ev_fd __rcu *io_ev_fd;
445:
446: void *cq_wait_arg;
447: size_t cq_wait_size;
448: } ____cacheline_aligned_in_smp;
449:
450: /*
451: * task_work and async notification delivery cacheline. Expected to
452: * regularly bounce b/w CPUs.
453: */
454: struct {
455: struct io_rings __rcu *rings_rcu;
456: struct mpscq work_list;
457: unsigned long check_cq;
458: atomic_t cq_wait_nr;
459: atomic_t cq_timeouts;
460: struct wait_queue_head cq_wait;
461: } ____cacheline_aligned_in_smp;
462:
463: /* timeouts */
464: struct {
465: raw_spinlock_t timeout_lock;
466: struct list_head timeout_list;
467: struct list_head ltimeout_list;
468: unsigned cq_last_tm_flush;
469: } ____cacheline_aligned_in_smp;
470:
471: spinlock_t completion_lock;
472:
473: struct list_head cq_overflow_list;
474:
475: struct hlist_head waitid_list;
476:
477: #ifdef CONFIG_FUTEX
478: struct hlist_head futex_list;
479: struct io_alloc_cache futex_cache;
480: #endif
481:
482: const struct cred *sq_creds; /* cred used for __io_sq_thread() */
483: struct io_sq_data *sq_data; /* if using sq thread polling */
484:
485: struct wait_queue_head sqo_sq_wait;
486: struct list_head sqd_list;
487:
488: unsigned int file_alloc_start;
489: unsigned int file_alloc_end;
490:
491: /* Keep this last, we don't need it for the fast path */
492: struct wait_queue_head poll_wq;
493: struct io_restriction restrictions;
494:
495: /* Stores zcrx object pointers of type struct io_zcrx_ifq */
496: struct xarray zcrx_ctxs;
497:
498: /* Used for accounting references on pages in registered buffers */
499: struct xarray hpage_acct;
500:
501: u32 pers_next;
502: struct xarray personalities;
503:
504: /* hashed buffered write serialization */
505: struct io_wq_hash *hash_map;
506:
507: /* Only used for accounting purposes */
508: struct user_struct *user;
509: struct mm_struct *mm_account;
510:
511: /*
512: * List of tctx nodes for this ctx, protected by tctx_lock. For
513: * cancelation purposes, nests under uring_lock.
514: */
515: struct list_head tctx_list;
516: struct mutex tctx_lock;
517:
518: /* ctx exit and cancelation */
519: struct work_struct exit_work;
520: struct completion ref_comp;
521:
522: /* io-wq management, e.g. thread count */
523: u32 iowq_limits[2];
524:
525: struct callback_head poll_wq_task_work;
526: /* deferred ->poll_wq wake up for local task_work */
527: struct rcu_head poll_wq_rcu;
528: atomic_t poll_wq_rcu_pending;
529: struct list_head defer_list;
530: unsigned nr_drained;
531: /* drain request dispatched from ->defer_list, until it completes */
532: struct io_kiocb *drain_req;
533:
534: /* protected by ->completion_lock */
535: unsigned nr_req_allocated;
536: /* pending SEND_ZC notifications, protected by ->uring_lock */
537: unsigned nr_notifs;
538:
539: #ifdef CONFIG_NET_RX_BUSY_POLL
540: struct list_head napi_list; /* track busy poll napi_id */
541: spinlock_t napi_lock; /* napi_list lock */
542:
543: /* napi busy poll default timeout */
544: ktime_t napi_busy_poll_dt;
545: bool napi_prefer_busy_poll;
546: u8 napi_track_mode;
547:
548: DECLARE_HASHTABLE(napi_ht, 4);
549: #endif
550:
551: struct io_uring_bpf_ops *bpf_ops;
552:
553: /*
554: * Protection for resize vs mmap races - both the mmap and resize
555: * side will need to grab this lock, to prevent either side from
556: * being run concurrently with the other.
557: */
558: struct mutex mmap_lock;
559:
560: struct io_mapped_region sq_region;
561: struct io_mapped_region ring_region;
562: /* used for optimised request parameter and wait argument passing */
563: struct io_mapped_region param_region;
564:
565: struct kcov_common_handle_id kcov_handle;
566: };
SourceFile:include/linux/io_uring_types.h]
|
| 21/2 |
2026/11/19 05:47 |
llm |
expert |
0m
Model:
Tokens:
input: 23713
output: 55
thoughts: 53
Thoughts: Okay, let's dive into this.
**Investigating the `IO_RING_F_*` Flags**
Alright, I need to get a clear understanding of these `IO_RING_F_*` flags. I'm going to track down their definitions. My first step is to locate the primary source – where they are *actually* defined. Since we're dealing with Linux kernel internals, I'll start by searching for those definitions within the `include/linux/` directory, specifically focusing on `io_uring_types.h`. That's where I'd expect to find the fundamental building blocks related to io_uring. If they aren't there, I'll broaden the search, but `io_uring_types.h` is the prime suspect. I'll need to know what they signify to do any real work.
|
| 22/2 |
2026/11/19 05:47 |
tool |
grepper |
0m
Args: map[Expression:enum.*io_ring_ctx_flags|IO_RING_F_ PathPrefixes:[include/linux/io_uring_types.h io_uring/]]
Results: map[Output:
Full output is too long, showing 200 out of 279 lines.
Use more precise expression or PathPrefixes if possible.
Number of matching lines per file (9 files in total):
include/linux/io_uring_types.h:15
io_uring/eventfd.c:2
io_uring/io_uring.c:43
io_uring/io_uring.h:4
io_uring/msg_ring.c:1
io_uring/register.c:5
io_uring/tctx.c:1
io_uring/timeout.c:2
io_uring/tw.c:2
include/linux/io_uring_types.h=314=enum {
include/linux/io_uring_types.h:315: IO_RING_F_DRAIN_NEXT = BIT(0),
include/linux/io_uring_types.h:316: IO_RING_F_OP_RESTRICTED = BIT(1),
include/linux/io_uring_types.h:317: IO_RING_F_REG_RESTRICTED = BIT(2),
include/linux/io_uring_types.h:318: IO_RING_F_OFF_TIMEOUT_USED = BIT(3),
include/linux/io_uring_types.h:319: IO_RING_F_DRAIN_ACTIVE = BIT(4),
include/linux/io_uring_types.h:320: IO_RING_F_HAS_EVFD = BIT(5),
include/linux/io_uring_types.h-321- /* all CQEs should be posted only by the submitter task */
include/linux/io_uring_types.h:322: IO_RING_F_TASK_COMPLETE = BIT(6),
include/linux/io_uring_types.h:323: IO_RING_F_LOCKLESS_CQ = BIT(7),
include/linux/io_uring_types.h:324: IO_RING_F_SYSCALL_IOPOLL = BIT(8),
include/linux/io_uring_types.h:325: IO_RING_F_POLL_ACTIVATED = BIT(9),
include/linux/io_uring_types.h:326: IO_RING_F_DRAIN_DISABLED = BIT(10),
include/linux/io_uring_types.h:327: IO_RING_F_COMPAT = BIT(11),
include/linux/io_uring_types.h:328: IO_RING_F_IOWQ_LIMITS_SET = BIT(12),
include/linux/io_uring_types.h-329- /* restrictions were inherited from the task that created the ring */
include/linux/io_uring_types.h:330: IO_RING_F_RESTRICT_INHERITED = BIT(13),
include/linux/io_uring_types.h-331-};
--
include/linux/io_uring_types.h=335=struct io_ring_ctx {
--
include/linux/io_uring_types.h-339- unsigned int flags;
include/linux/io_uring_types.h:340: /* internal state flags IO_RING_F_* flags , mostly read-only */
include/linux/io_uring_types.h-341- unsigned int int_flags;
--
io_uring/eventfd.c=120=int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg,
--
io_uring/eventfd.c-151- ev_fd->eventfd_async = eventfd_async;
io_uring/eventfd.c:152: ctx->int_flags |= IO_RING_F_HAS_EVFD;
io_uring/eventfd.c-153- refcount_set(&ev_fd->refs, 1);
--
io_uring/eventfd.c=159=int io_eventfd_unregister(struct io_ring_ctx *ctx)
--
io_uring/eventfd.c-165- if (ev_fd) {
io_uring/eventfd.c:166: ctx->int_flags &= ~IO_RING_F_HAS_EVFD;
io_uring/eventfd.c-167- rcu_assign_pointer(ctx->io_ev_fd, NULL);
--
io_uring/io_uring.c=531=void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
io_uring/io_uring.c-532-{
io_uring/io_uring.c:533: if (ctx->int_flags & IO_RING_F_POLL_ACTIVATED)
io_uring/io_uring.c-534- io_poll_wq_wake(ctx);
io_uring/io_uring.c:535: if (ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED)
io_uring/io_uring.c-536- io_flush_timeouts(ctx);
io_uring/io_uring.c:537: if (ctx->int_flags & IO_RING_F_HAS_EVFD)
io_uring/io_uring.c-538- io_eventfd_signal(ctx, true, false);
--
io_uring/io_uring.c=541=static inline void __io_cq_lock(struct io_ring_ctx *ctx)
io_uring/io_uring.c-542-{
io_uring/io_uring.c:543: if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ))
io_uring/io_uring.c-544- spin_lock(&ctx->completion_lock);
--
io_uring/io_uring.c=553=static inline void __io_cq_unlock_post(struct io_ring_ctx *ctx)
--
io_uring/io_uring.c-555- io_commit_cqring(ctx);
io_uring/io_uring.c:556: if (!(ctx->int_flags & IO_RING_F_TASK_COMPLETE)) {
io_uring/io_uring.c:557: if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ))
io_uring/io_uring.c-558- spin_unlock(&ctx->completion_lock);
io_uring/io_uring.c-559- /* IOPOLL rings only need to wake up if it's also SQPOLL */
io_uring/io_uring.c:560: if (!(ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL))
io_uring/io_uring.c-561- io_cqring_wake(ctx);
--
io_uring/io_uring.c=914=void io_add_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags)
--
io_uring/io_uring.c-916- lockdep_assert_held(&ctx->uring_lock);
io_uring/io_uring.c:917: lockdep_assert(ctx->int_flags & IO_RING_F_LOCKLESS_CQ);
io_uring/io_uring.c-918-
--
io_uring/io_uring.c=931=bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags)
--
io_uring/io_uring.c-946-
io_uring/io_uring.c:947: if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) {
io_uring/io_uring.c-948- spin_lock(&ctx->completion_lock);
--
io_uring/io_uring.c=963=bool io_req_post_cqe32(struct io_kiocb *req, struct io_uring_cqe cqe[2])
--
io_uring/io_uring.c-971- cqe[0].user_data = req->cqe.user_data;
io_uring/io_uring.c:972: if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) {
io_uring/io_uring.c-973- spin_lock(&ctx->completion_lock);
--
io_uring/io_uring.c=1000=static void io_req_complete_post(struct io_kiocb *req, unsigned issue_flags)
--
io_uring/io_uring.c-1018- */
io_uring/io_uring.c:1019: if ((ctx->int_flags & IO_RING_F_LOCKLESS_CQ) ||
io_uring/io_uring.c-1020- (req->flags & (REQ_F_REISSUE | REQ_F_IO_DRAIN))) {
--
io_uring/io_uring.c=1244=void __io_submit_flush_completions(struct io_ring_ctx *ctx)
--
io_uring/io_uring.c-1275- unlikely(!io_fill_cqe_req(ctx, req))) {
io_uring/io_uring.c:1276: if (ctx->int_flags & IO_RING_F_LOCKLESS_CQ)
io_uring/io_uring.c-1277- io_cqe_overflow(ctx, &req->cqe, &req->big_cqe);
--
io_uring/io_uring.c-1283-
io_uring/io_uring.c:1284: if (unlikely(ctx->int_flags & IO_RING_F_DRAIN_ACTIVE) && ctx->drain_req)
io_uring/io_uring.c-1285- drain_done = io_drain_req_completed(ctx);
--
io_uring/io_uring.c-1291-
io_uring/io_uring.c:1292: if (unlikely(ctx->int_flags & IO_RING_F_DRAIN_ACTIVE)) {
io_uring/io_uring.c-1293- /* a drain request started from the list keeps the drain going */
--
io_uring/io_uring.c-1296- if (drain_done && list_empty(&ctx->defer_list))
io_uring/io_uring.c:1297: ctx->int_flags &= ~IO_RING_F_DRAIN_ACTIVE;
io_uring/io_uring.c-1298- }
--
io_uring/io_uring.c=1470=static __cold void io_drain_req(struct io_kiocb *req)
--
io_uring/io_uring.c-1490- if (!drain && list_empty(&ctx->defer_list))
io_uring/io_uring.c:1491: ctx->int_flags &= ~IO_RING_F_DRAIN_ACTIVE;
io_uring/io_uring.c-1492-}
--
io_uring/io_uring.c=1804=static void io_queue_sqe_fallback(struct io_kiocb *req)
--
io_uring/io_uring.c-1817- io_req_sqe_copy(req, IO_URING_F_INLINE);
io_uring/io_uring.c:1818: if (unlikely(req->ctx->int_flags & IO_RING_F_DRAIN_ACTIVE))
io_uring/io_uring.c-1819- io_drain_req(req);
--
io_uring/io_uring.c=1830=static inline bool io_check_restriction(struct io_ring_ctx *ctx,
--
io_uring/io_uring.c-1833-{
io_uring/io_uring.c:1834: if (!(ctx->int_flags & IO_RING_F_OP_RESTRICTED))
io_uring/io_uring.c-1835- return true;
--
io_uring/io_uring.c=1850=static void io_init_drain(struct io_ring_ctx *ctx)
--
io_uring/io_uring.c-1853-
io_uring/io_uring.c:1854: ctx->int_flags |= IO_RING_F_DRAIN_ACTIVE;
io_uring/io_uring.c-1855- if (head) {
--
io_uring/io_uring.c-1863- head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
io_uring/io_uring.c:1864: ctx->int_flags |= IO_RING_F_DRAIN_NEXT;
io_uring/io_uring.c-1865- }
--
io_uring/io_uring.c=1875=static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
--
io_uring/io_uring.c-1938- if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS)
io_uring/io_uring.c:1939: ctx->int_flags |= IO_RING_F_DRAIN_DISABLED;
io_uring/io_uring.c-1940- if (sqe_flags & IOSQE_IO_DRAIN) {
io_uring/io_uring.c:1941: if (ctx->int_flags & IO_RING_F_DRAIN_DISABLED)
io_uring/io_uring.c-1942- return io_init_fail_req(req, -EOPNOTSUPP);
--
io_uring/io_uring.c-1945- }
io_uring/io_uring.c:1946: if (unlikely(ctx->int_flags & (IO_RING_F_OP_RESTRICTED | IO_RING_F_DRAIN_ACTIVE | IO_RING_F_DRAIN_NEXT))) {
io_uring/io_uring.c-1947- if (!io_check_restriction(ctx, req, sqe_flags))
--
io_uring/io_uring.c-1949- /* knock it to the slow queue path, will be drained there */
io_uring/io_uring.c:1950: if (ctx->int_flags & IO_RING_F_DRAIN_ACTIVE)
io_uring/io_uring.c-1951- req->flags |= REQ_F_FORCE_ASYNC;
io_uring/io_uring.c-1952- /* if there is no link, we're at "next" request and need to drain */
io_uring/io_uring.c:1953: if (unlikely(ctx->int_flags & IO_RING_F_DRAIN_NEXT) && !ctx->submit_state.link.head) {
io_uring/io_uring.c:1954: ctx->int_flags &= ~IO_RING_F_DRAIN_NEXT;
io_uring/io_uring.c:1955: ctx->int_flags |= IO_RING_F_DRAIN_ACTIVE;
io_uring/io_uring.c-1956- req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
--
io_uring/io_uring.c=2372=static __cold void io_activate_pollwq_cb(struct callback_head *cb)
--
io_uring/io_uring.c-2377- mutex_lock(&ctx->uring_lock);
io_uring/io_uring.c:2378: ctx->int_flags |= IO_RING_F_POLL_ACTIVATED;
io_uring/io_uring.c-2379- mutex_unlock(&ctx->uring_lock);
--
io_uring/io_uring.c=2389=__cold void io_activate_pollwq(struct io_ring_ctx *ctx)
--
io_uring/io_uring.c-2392- /* already activated or in progress */
io_uring/io_uring.c:2393: if ((ctx->int_flags & IO_RING_F_POLL_ACTIVATED) || ctx->poll_wq_task_work.func)
io_uring/io_uring.c-2394- goto out;
io_uring/io_uring.c:2395: if (WARN_ON_ONCE(!(ctx->int_flags & IO_RING_F_TASK_COMPLETE)))
io_uring/io_uring.c-2396- goto out;
--
io_uring/io_uring.c=2411=static __poll_t io_uring_poll(struct file *file, poll_table *wait)
--
io_uring/io_uring.c-2415-
io_uring/io_uring.c:2416: if (unlikely(!(data_race(ctx->int_flags) & IO_RING_F_POLL_ACTIVATED)))
io_uring/io_uring.c-2417- io_activate_pollwq(ctx);
--
io_uring/io_uring.c=2852=SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
--
io_uring/io_uring.c-2908- if (flags & IORING_ENTER_GETEVENTS) {
io_uring/io_uring.c:2909: if (ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL)
io_uring/io_uring.c-2910- goto iopoll_locked;
--
io_uring/io_uring.c-2923-
io_uring/io_uring.c:2924: if (ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL) {
io_uring/io_uring.c-2925- /*
--
io_uring/io_uring.c=3217=static void io_ctx_restriction_clone(struct io_ring_ctx *ctx,
--
io_uring/io_uring.c-3225- if (dst->op_registered)
io_uring/io_uring.c:3226: ctx->int_flags |= IO_RING_F_OP_RESTRICTED;
io_uring/io_uring.c-3227- if (dst->reg_registered)
io_uring/io_uring.c:3228: ctx->int_flags |= IO_RING_F_REG_RESTRICTED |
io_uring/io_uring.c:3229: IO_RING_F_RESTRICT_INHERITED;
io_uring/io_uring.c-3230-}
--
io_uring/io_uring.c=3232=static __cold int io_uring_create(struct io_ctx_config *config)
--
io_uring/io_uring.c-3255- !(ctx->flags & IORING_SETUP_IOPOLL))
io_uring/io_uring.c:3256: ctx->int_flags |= IO_RING_F_TASK_COMPLETE;
io_uring/io_uring.c-3257-
io_uring/io_uring.c:3258: if ((ctx->int_flags & IO_RING_F_TASK_COMPLETE) ||
io_uring/io_uring.c-3259- (ctx->flags & IORING_SETUP_IOPOLL))
io_uring/io_uring.c:3260: ctx->int_flags |= IO_RING_F_LOCKLESS_CQ;
io_uring/io_uring.c-3261-
]
|
| 23/2 |
2026/11/19 05:47 |
llm |
expert |
0m
Model:
Tokens:
input: 29031
output: 37
thoughts: 698
Thoughts: **Analyzing a KCSAN Report: A Deep Dive into io_uring Concurrency**
Okay, let's break down this KCSAN report. The crux of the matter seems to be a data race involving a 4-byte write and read to the same memory location, `0xffff88811c6db004`. The report provides stack traces for both sides of the access. This is going to be interesting.
On the write side, task 3754 on CPU 1 is the culprit. The trace shows a familiar `io_uring` sequence: It starts with `io_init_req` (likely initializing the request), proceeds through `io_submit_sqe`, and ultimately lands in `io_sq_thread`. This looks like the submission path. The last bit before the fork is pretty standard.
The read side, triggered by task 3758 on CPU 0, is the intriguing one. The trace reveals a sequence that seems to involve completion: `io_issue_sqe` (submitting the I/O request) leads to `io_req_complete_post` (indicating completion posting) and then down the line to `io_cq_unlock_post` and finally to `__io_commit_cqring_flush`. The final function here is where I need to focus: it is the last step on the read side.
My immediate inclination is to examine the code around `io_commit_cqring_flush` and `io_req_complete_post` in `io_uring/io_uring.c`. I need to understand the locking mechanisms or lack thereof in these functions and how they relate to the CQ ring, and I/O submission. Specifically, lines 560-580 and 1010-1040 are the target regions. I need to figure out exactly how the read side's completion logic interacts with the write side's submission logic, to see what could lead to a race. I need to be sure that the memory location accessed is guarded properly.
|
| 24/2 |
2026/11/19 05:47 |
tool |
read-file |
0m
Args: map[File:io_uring/io_uring.c FirstLine:560 LineCount:30]
Results: map[Contents: 560: if (!(ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL))
561: io_cqring_wake(ctx);
562: }
563: io_commit_cqring_flush(ctx);
564: }
565:
566: static void io_cq_unlock_post(struct io_ring_ctx *ctx)
567: __releases(ctx->completion_lock)
568: {
569: io_commit_cqring(ctx);
570: spin_unlock(&ctx->completion_lock);
571: io_cqring_wake(ctx);
572: io_commit_cqring_flush(ctx);
573: }
574:
575: static void __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool dying)
576: {
577: lockdep_assert_held(&ctx->uring_lock);
578:
579: /* don't abort if we're dying, entries must get freed */
580: if (!dying && __io_cqring_events(ctx) == ctx->cq_entries)
581: return;
582:
583: io_cq_lock(ctx);
584: while (!list_empty(&ctx->cq_overflow_list)) {
585: size_t cqe_size = sizeof(struct io_uring_cqe);
586: struct io_uring_cqe *cqe;
587: struct io_overflow_cqe *ocqe;
588: bool is_cqe32 = false;
589:
]
|
| 25/2 |
2026/11/19 05:47 |
llm |
expert |
0m
Model:
Tokens:
input: 30693
output: 38
thoughts: 2325
Thoughts: **Analyzing a Potential Data Race in `io_uring`'s CQ Flush Logic**
Okay, so I'm digging into `io_cq_unlock_post`, the function responsible for completing the completion queue ring in `io_uring`. I see that it calls `io_commit_cqring_flush`, which in turn, based on certain internal flags, decides whether to call `__io_commit_cqring_flush`. Specifically, the decision hinges on whether any of `IO_RING_F_OFF_TIMEOUT_USED`, `IO_RING_F_HAS_EVFD`, or `IO_RING_F_POLL_ACTIVATED` are set in `ctx->int_flags`. The critical point here is that `io_commit_cqring_flush` uses `data_race(ctx->int_flags)` when checking these flags, and based on this, it conditionally calls `__io_commit_cqring_flush`.
Now, here's where it gets interesting: *within* `__io_commit_cqring_flush`, the code checks these same flags again, but this time, it does a *plain* read of `ctx->int_flags` – no `data_race()` annotation! This inconsistency immediately throws up a red flag. Why would the check *before* the function call be race-protected but the checks *inside* not? Is this a genuine oversight, or is there an intentional rationale I'm missing?
I remember commit `8c557449199e` which seems to address this. The commit message indicates that `data_race()` was added to mark some "harmless racy" reads of `ctx->int_flags`. The author, Jens Axboe, thought these were harmless races outside of the `uring_lock`. However, did Jens forgot to wrap the internal flags checks within `__io_commit_cqring_flush`? Or did he miss something important?
My concern is: is this actually harmless? I know better than to blindly trust a commit message, even one from someone as experienced as Jens. I can't just assume it's benign because the author *thought* it was. I need to apply the concurrency protocol. I have to look carefully at the read and write paths to determine if any of the concrete hazards of the concurrency protocol are present. I have to rule out things like fused accesses, torn accesses, ordering violations, logic bugs, bitfield issues (concurrent writes to different bits in the same word), and state machine violations.
I can see from looking at the write path that the critical write side comes from the `io_sq_thread` which is calling `io_init_req`. In `io_init_req`, the flag `IO_RING_F_DRAIN_DISABLED` could be set, which is setting the flags potentially raced upon.
I must now carefully analyze the code around where these flags are set and read to determine if the lack of `data_race()` in `__io_commit_cqring_flush` introduces a real concurrency issue. This requires a much closer look at all the contexts in which these flags are modified and accessed. This could be a subtle but serious bug if not handled correctly.
|
| 26/2 |
2026/11/19 05:48 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:io_uring/sqpoll.c Name:__io_sq_thread]
Results: map[SourceCode: 223: static int __io_sq_thread(struct io_ring_ctx *ctx, struct io_sq_data *sqd,
224: bool cap_entries, struct io_sq_time *ist)
225: {
226: unsigned int to_submit;
227: int ret = 0;
228:
229: to_submit = io_sqring_entries(ctx);
230: /* if we're handling multiple rings, cap submit size for fairness */
231: if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
232: to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
233:
234: if (to_submit || !list_empty(&ctx->iopoll_list)) {
235: const struct cred *creds = NULL;
236:
237: io_sq_start_worktime(ist);
238:
239: if (ctx->sq_creds != current_cred())
240: creds = override_creds(ctx->sq_creds);
241:
242: mutex_lock(&ctx->uring_lock);
243: if (!list_empty(&ctx->iopoll_list))
244: io_do_iopoll(ctx, true);
245:
246: /*
247: * Don't submit if refs are dying, good for io_uring_register(),
248: * but also it is relied upon by io_ring_exit_work()
249: */
250: if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
251: !(ctx->flags & IORING_SETUP_R_DISABLED))
252: ret = io_submit_sqes(ctx, to_submit);
253: mutex_unlock(&ctx->uring_lock);
254:
255: if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
256: wake_up(&ctx->sqo_sq_wait);
257: if (creds)
258: revert_creds(creds);
259: }
260:
261: return ret;
262: }
SourceFile:io_uring/sqpoll.c]
|
| 27/2 |
2026/11/19 05:48 |
llm |
expert |
0m
Model:
Tokens:
input: 34441
output: 38
thoughts: 508
Thoughts: **Analyzing the io_uring Completion Path**
Okay, so I'm looking at this `io_sq_thread` which, as I understand it, holds `ctx->uring_lock`. That's the submission side, right? Now, I'm trying to trace the completion path, and I see a read, specifically to `0xffff88811c6db004` of 4 bytes, by task 3758, on CPU 0. It looks like this read is triggered through the standard completion path: `__io_commit_cqring_flush` -> `io_cq_unlock_post` -> `io_req_complete_post` -> `io_issue_sqe` -> `io_wq_submit_work` -> `io_worker_handle_work`, all the way back to the `io_wq_worker` thread.
This task, 3758, is an `io-wq` worker thread, which means it's handling asynchronous work. This is the crucial point for me, here. I can clearly trace it up to `io_req_complete_post` in the chain of operations. I'm especially interested in whether that worker thread, the one doing the actual completion, holds the `ctx->uring_lock`.
The answer, as I know it to be, is NO! The workers and the submission thread work concurrently on the ring. The completion path isn't serialized by that lock. This is important to remember because it has implications for the overall locking model and potential race conditions within io_uring.
Okay, let's zoom in on `io_req_complete_post` in `io_uring/io_uring.c`, around lines 1000-1040. I know this is where the critical interactions happen. I need to refresh my understanding of how the worker interacts with the completion queue and the submission queue without the submission side lock.
|
| 28/2 |
2026/11/19 05:48 |
tool |
read-file |
0m
Args: map[File:io_uring/io_uring.c FirstLine:1000 LineCount:45]
Results: map[Contents:1000: static void io_req_complete_post(struct io_kiocb *req, unsigned issue_flags)
1001: {
1002: struct io_ring_ctx *ctx = req->ctx;
1003: bool completed = true;
1004:
1005: /*
1006: * All execution paths but io-wq use the deferred completions by
1007: * passing IO_URING_F_COMPLETE_DEFER and thus should not end up here.
1008: */
1009: if (WARN_ON_ONCE(!(issue_flags & IO_URING_F_IOWQ)))
1010: return;
1011:
1012: io_req_put_file(req, true);
1013:
1014: /*
1015: * Handle special CQ sync cases via task_work. DEFER_TASKRUN requires
1016: * the submitter task context, IOPOLL protects with uring_lock. A drain
1017: * request ends the drain under uring_lock as its CQE is posted.
1018: */
1019: if ((ctx->int_flags & IO_RING_F_LOCKLESS_CQ) ||
1020: (req->flags & (REQ_F_REISSUE | REQ_F_IO_DRAIN))) {
1021: defer_complete:
1022: req->io_task_work.func = io_req_task_complete;
1023: io_req_task_work_add(req);
1024: return;
1025: }
1026:
1027: io_cq_lock(ctx);
1028: if (!(req->flags & REQ_F_CQE_SKIP))
1029: completed = io_fill_cqe_req(ctx, req);
1030: io_cq_unlock_post(ctx);
1031:
1032: if (!completed)
1033: goto defer_complete;
1034:
1035: /*
1036: * Request not freed here because we know it's called from io-wq only,
1037: * which holds a reference. Hence it can't be the last put. The CQE
1038: * has been posted, last put frees it.
1039: */
1040: req->flags |= REQ_F_CQE_SKIP;
1041: req_ref_put(req);
1042: }
1043:
1044: void io_req_defer_failed(struct io_kiocb *req, s32 res)
]
|
| 29/2 |
2026/11/19 05:48 |
llm |
expert |
0m
Model:
Tokens:
input: 36139
output: 43
thoughts: 1758
Thoughts: **Analyzing Potential Data Races in the io_uring Completion Path**
Alright, let's break this down. I'm focusing on the completion path in `io_uring` and potential data races. I start by examining the `io_req_complete_post` function. Inside that, I see `io_cq_unlock_post(ctx)`, and I immediately zero in on the `io_commit_cqring_flush(ctx)` call within it. My concern is with `io_commit_cqring_flush`, specifically its potential interaction with other threads, and whether locks are held when this is called.
The code shows `io_commit_cqring_flush` indirectly calls `__io_commit_cqring_flush`, which checks `ctx->int_flags` to potentially trigger work-queue wake-ups, timeout handling, or eventfd signaling. That's where I have a red flag. What locks are held when `__io_commit_cqring_flush` is called? The code shows `spin_unlock(&ctx->completion_lock)` has already happened. The `uring_lock` *isn't* held by the worker thread. So there's a window of opportunity for concurrency issues if the `ctx->int_flags` is modified by other threads.
I then think about the SQPOLL thread, (task 3754) and how it's holding `ctx->uring_lock` while executing `io_submit_sqes()`. I note that in `io_init_req`, `ctx->int_flags` can be modified based on `IOSQE_CQE_SKIP_SUCCESS` flag. I know that `ctx->int_flags` is an `unsigned int` (32-bit), which is the important part here. CPU 1 might be writing it, while CPU 0 is reading it. This leads me to question if there are any *concurrent* writers to `ctx->int_flags`. This is a critical point.
I embark on a methodical search. I'm going to look for every single instance where `ctx->int_flags` is written to in the code. I found this list, which shows a lot of code paths that are protected by the `uring_lock`:
* `io_eventfd_register` and `io_eventfd_unregister`: These are fine, they use `uring_lock`.
* `__io_submit_flush_completions`, and `io_drain_req`: The call paths are also protected by the `uring_lock`.
* `io_init_drain`, and `io_init_req`: This involves submission and drain logic under `uring_lock`.
* `io_activate_pollwq_cb`: The call path is protected by `uring_lock`.
* `io_ctx_restriction_clone`, `io_uring_create`, `io_register_restrictions`, and `io_register_iowq_max_workers`: These are during ring creation or registration so are also fine.
* `__io_timeout_prep`: Here I need to examine the callers of `__io_timeout_prep`...
...So, the only case that I could see a race condition would be: from `io_req_complete_post`, to `__io_commit_cqring_flush` where `ctx->int_flags` is read, while CPU 0 and CPU 1 could be writing to it at the same time. The way to mitigate would be a memory barrier to read and write correctly.
|
| 30/2 |
2026/11/19 05:48 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:io_uring/timeout.c IncludeSnippetLines:5 Name:__io_timeout_prep]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:io_link_timeout_prep SourceFile:io_uring/timeout.c SourceLine:679 SourceSnippet: 677: int io_link_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
678: {
679: return __io_timeout_prep(req, sqe, true);
680: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:io_timeout_prep SourceFile:io_uring/timeout.c SourceLine:674 SourceSnippet: 672: int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
673: {
674: return __io_timeout_prep(req, sqe, false);
675: }
]] TruncatedOutput:false]
|
| 31/2 |
2026/11/19 05:48 |
llm |
expert |
0m
Model:
Tokens:
input: 38984
output: 44
thoughts: 96
Thoughts: Okay, here's my breakdown of this question, focusing on the potential implications and my line of reasoning:
**Analyzing Potential Concurrency Issues in io_uring Request Handling**
Alright, I'm thinking about `io_link_timeout_prep` and `io_timeout_prep` now. The core point is that they're `.prep` handlers, which means they're involved in the initial setup of a request *before* it gets sent to the kernel via `io_uring`. The critical thing here is the statement that request *submission* is *always* protected by `uring_lock`. That's the key.
So, if these `.prep` handlers are part of the *submission* process, then the writes to their internal state *should* be thread-safe. That's the expectation. But this is the kind of area where a seemingly obvious assumption can be hiding a bug, and my experience says to never trust assumptions, no matter how obvious!
The next step is to verify that assumption. Specifically, I need to confirm that *all* writes to `ctx->int_flags` are indeed serialized by `uring_lock`. That's the crucial data race possibility. If there's an unchecked write, we've got a problem. So, I need to meticulously trace how `ctx->int_flags` is modified. I'll need to go beyond the `.prep` handlers themselves.
I need to expand my search. The obvious candidates for investigation are `io_drain_req` and `__io_submit_flush_completions`. They're involved in handling potentially completed or drained requests and flushing completions, so they're in the right part of the code base to be a problem. I'll need to see if they access and modify `ctx->int_flags` and if they do so, whether or not the access is protected by `uring_lock`. This is a pretty standard debugging approach, and it's the right way to approach this type of problem.
|
| 32/2 |
2026/11/19 05:48 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:io_uring/io_uring.c IncludeSnippetLines:5 Name:io_drain_req]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:io_queue_sqe_fallback SourceFile:io_uring/io_uring.c SourceLine:1819 SourceSnippet:1814: io_req_defer_failed(req, req->cqe.res);
1815: } else {
1816: /* can't fail with IO_URING_F_INLINE */
1817: io_req_sqe_copy(req, IO_URING_F_INLINE);
1818: if (unlikely(req->ctx->int_flags & IO_RING_F_DRAIN_ACTIVE))
1819: io_drain_req(req);
1820: else
1821: io_queue_iowq(req);
1822: }
1823: }
]] TruncatedOutput:false]
|
| 33/2 |
2026/11/19 05:48 |
llm |
expert |
0m
Model:
Tokens:
input: 39799
output: 49
thoughts: 50
Thoughts: Here's my analysis of what I'm thinking, rendered in the first person, as requested:
**Navigating the IO Submission Landscape**
Okay, so I'm focusing on the io_uring submission process right now, and `io_queue_sqe_fallback` is clearly a key function involved during submission. My immediate next step is to understand its relationship with other components. That prompts the question: What's the connection with `__io_submit_flush_completions`? I know I've encountered that name before, and it feels like an important piece of the puzzle. To clarify the role of `__io_submit_flush_completions`, I need to cross-reference it and check where this function is called from, to ascertain how it interfaces.
|
| 34/2 |
2026/11/19 05:48 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:io_uring/io_uring.c IncludeSnippetLines:5 Name:__io_submit_flush_completions]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:io_do_iopoll SourceFile:io_uring/rw.c SourceLine:1386 SourceSnippet:1381: req->cqe.flags |= io_put_kbuf(req, max(req->cqe.res, 0), NULL);
1382: if (!io_is_uring_cmd(req))
1383: io_req_rw_cleanup(req, 0);
1384: }
1385: if (nr_events)
1386: __io_submit_flush_completions(ctx);
1387: return nr_events;
1388: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:io_req_post_cqe SourceFile:io_uring/io_uring.c SourceLine:942 SourceSnippet: 937: * If multishot has already posted deferred completions, ensure that
938: * those are flushed first before posting this one. If not, CQEs
939: * could get reordered.
940: */
941: if (!wq_list_empty(&ctx->submit_state.compl_reqs))
942: __io_submit_flush_completions(ctx);
943:
944: lockdep_assert(!io_wq_current_is_worker());
945: lockdep_assert_held(&ctx->uring_lock);
946:
947: if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) {
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:io_submit_flush_completions SourceFile:io_uring/io_uring.h SourceLine:256 SourceSnippet: 252: static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
253: {
254: if (!wq_list_empty(&ctx->submit_state.compl_reqs) ||
255: ctx->submit_state.cq_flush)
256: __io_submit_flush_completions(ctx);
257: }
]] TruncatedOutput:false]
|
| 35/2 |
2026/11/19 05:48 |
llm |
expert |
0m
Model:
Tokens:
input: 40701
output: 25
thoughts: 785
Thoughts: **Analyzing Concurrency in io_uring Context Flags**
Okay, let's dive into this `ctx->int_flags` situation within the io_uring context. First, I noticed that `lockdep_assert_held(&ctx->uring_lock);` assertion. That immediately suggests a pattern: every single write to `ctx->int_flags` *should* be protected by `ctx->uring_lock`. This means no concurrent writers, which is a good starting point for ensuring data integrity.
To confirm this, I need to methodically check all write accesses:
1. `io_eventfd_register`: Protected by `uring_lock`.
2. `io_eventfd_unregister`: Protected by `uring_lock`.
3. `__io_submit_flush_completions`: Protected by `uring_lock`.
4. `io_drain_req`: Protected by `uring_lock`.
5. `io_init_drain`: Protected by `uring_lock`.
6. `io_init_req`: Protected by `uring_lock`.
7. `io_activate_pollwq_cb`: Acquires `mutex_lock(&ctx->uring_lock)`.
8. `io_ctx_restriction_clone` & `io_uring_create`: During object creation, protected.
9. `io_register_restrictions`: Protected by `uring_lock`.
10. `io_register_iowq_max_workers`: Protected by `uring_lock`.
11. `__io_timeout_prep`: Protected by `uring_lock`.
Looks like everything is indeed serialized. There is only ever a single writer at any given moment. That's good!
Now, the interesting question: where are the *readers* that might access `ctx->int_flags` *without* holding `ctx->uring_lock`? If there are any, we have a potential data race.
My focus goes to `io_commit_cqring_flush` and `__io_commit_cqring_flush`. The inline function checks `data_race(ctx->int_flags) & SHOULD_FLUSH_MASK`. `__io_commit_cqring_flush` then proceeds to read various flags within `ctx->int_flags` conditionally:
Okay, `IO_RING_F_POLL_ACTIVATED`, `IO_RING_F_OFF_TIMEOUT_USED`, `IO_RING_F_HAS_EVFD` are flags being read without the lock held, and they are critical, as `io_poll_wq_wake`, `io_flush_timeouts`, and `io_eventfd_signal` are called based on these flags. Let's dig deeper:
* **`IO_RING_F_POLL_ACTIVATED`**: I need to understand when this is set. Looking at `io_activate_pollwq`, it sets `ctx->int_flags |= IO_RING_F_POLL_ACTIVATED;`. The next thing is to ask, is it ever cleared? I'll grep for a `~IO_RING_F_POLL_ACTIVATED` pattern, or any code that clears it. We need to be confident that there isn't a race between the flag being set and read. I need to grep the entire codebase.
|
| 36/2 |
2026/11/19 05:49 |
tool |
grepper |
0m
Args: map[Expression:IO_RING_F_POLL_ACTIVATED]
Results: map[Output:include/linux/io_uring_types.h=314=enum {
--
include/linux/io_uring_types.h-324- IO_RING_F_SYSCALL_IOPOLL = BIT(8),
include/linux/io_uring_types.h:325: IO_RING_F_POLL_ACTIVATED = BIT(9),
include/linux/io_uring_types.h-326- IO_RING_F_DRAIN_DISABLED = BIT(10),
--
io_uring/io_uring.c=531=void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
io_uring/io_uring.c-532-{
io_uring/io_uring.c:533: if (ctx->int_flags & IO_RING_F_POLL_ACTIVATED)
io_uring/io_uring.c-534- io_poll_wq_wake(ctx);
--
io_uring/io_uring.c=2372=static __cold void io_activate_pollwq_cb(struct callback_head *cb)
--
io_uring/io_uring.c-2377- mutex_lock(&ctx->uring_lock);
io_uring/io_uring.c:2378: ctx->int_flags |= IO_RING_F_POLL_ACTIVATED;
io_uring/io_uring.c-2379- mutex_unlock(&ctx->uring_lock);
--
io_uring/io_uring.c=2389=__cold void io_activate_pollwq(struct io_ring_ctx *ctx)
--
io_uring/io_uring.c-2392- /* already activated or in progress */
io_uring/io_uring.c:2393: if ((ctx->int_flags & IO_RING_F_POLL_ACTIVATED) || ctx->poll_wq_task_work.func)
io_uring/io_uring.c-2394- goto out;
--
io_uring/io_uring.c=2411=static __poll_t io_uring_poll(struct file *file, poll_table *wait)
--
io_uring/io_uring.c-2415-
io_uring/io_uring.c:2416: if (unlikely(!(data_race(ctx->int_flags) & IO_RING_F_POLL_ACTIVATED)))
io_uring/io_uring.c-2417- io_activate_pollwq(ctx);
--
io_uring/io_uring.c=3232=static __cold int io_uring_create(struct io_ctx_config *config)
--
io_uring/io_uring.c-3266- if (!(ctx->int_flags & IO_RING_F_TASK_COMPLETE))
io_uring/io_uring.c:3267: ctx->int_flags |= IO_RING_F_POLL_ACTIVATED;
io_uring/io_uring.c-3268-
--
io_uring/io_uring.h=503=static inline void io_req_complete_defer(struct io_kiocb *req)
--
io_uring/io_uring.h-513-#define SHOULD_FLUSH_MASK (IO_RING_F_OFF_TIMEOUT_USED | \
io_uring/io_uring.h:514: IO_RING_F_HAS_EVFD | IO_RING_F_POLL_ACTIVATED)
io_uring/io_uring.h-515-
--
io_uring/tw.c=182=void io_req_local_work_add(struct io_kiocb *req, unsigned flags)
--
io_uring/tw.c-208- /* whoever sets the flag wakes ->poll_wq afterwards */
io_uring/tw.c:209: if (data_race(ctx->int_flags) & IO_RING_F_POLL_ACTIVATED)
io_uring/tw.c-210- io_local_work_wake_poll(ctx, flags);
]
|
| 37/2 |
2026/11/19 05:49 |
llm |
expert |
0m
Model:
Tokens:
input: 43268
output: 25
thoughts: 58
Thoughts: Okay, here's my interpretation of those thoughts, tailored for an expert audience, as if they were my own:
**Investigating a Potentially Persistent IO_URING Behavior**
Right, *that* flag... `IO_RING_F_POLL_ACTIVATED` is set and, as far as I can see, it's *never* cleared. That's immediately interesting, possibly a bug, or maybe a design choice with subtle implications. I need to understand the side effects of that better.
My immediate follow-up thought is about another related flag, specifically `IO_RING_F_OFF_TIMEOUT_USED`. I have a feeling there might be a relationship, perhaps an interaction or even a dependency, that I need to explore. Let's see how that timeout flag is used.
Time to reach for the trusty `grep`. I need to track down every instance of `IO_RING_F_OFF_TIMEOUT_USED` in the codebase and understand under which conditions, and how, that flag is set, checked, and used within the io_uring system. A thorough grep will be the first step to understanding the interplay between those two flags.
|
| 38/2 |
2026/11/19 05:49 |
tool |
grepper |
0m
Args: map[Expression:IO_RING_F_OFF_TIMEOUT_USED]
Results: map[Output:include/linux/io_uring_types.h=314=enum {
--
include/linux/io_uring_types.h-317- IO_RING_F_REG_RESTRICTED = BIT(2),
include/linux/io_uring_types.h:318: IO_RING_F_OFF_TIMEOUT_USED = BIT(3),
include/linux/io_uring_types.h-319- IO_RING_F_DRAIN_ACTIVE = BIT(4),
--
io_uring/io_uring.c=531=void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
--
io_uring/io_uring.c-534- io_poll_wq_wake(ctx);
io_uring/io_uring.c:535: if (ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED)
io_uring/io_uring.c-536- io_flush_timeouts(ctx);
--
io_uring/io_uring.h=503=static inline void io_req_complete_defer(struct io_kiocb *req)
--
io_uring/io_uring.h-512-
io_uring/io_uring.h:513:#define SHOULD_FLUSH_MASK (IO_RING_F_OFF_TIMEOUT_USED | \
io_uring/io_uring.h-514- IO_RING_F_HAS_EVFD | IO_RING_F_POLL_ACTIVATED)
--
io_uring/timeout.c=600=static int __io_timeout_prep(struct io_kiocb *req,
--
io_uring/timeout.c-630- timeout->off = off;
io_uring/timeout.c:631: if (unlikely(off && !(req->ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED)))
io_uring/timeout.c:632: req->ctx->int_flags |= IO_RING_F_OFF_TIMEOUT_USED;
io_uring/timeout.c-633- /*
]
|
| 39/2 |
2026/11/19 05:49 |
llm |
expert |
0m
Model:
Tokens:
input: 44135
output: 24
thoughts: 57
Thoughts: Okay, here's my take on those thoughts, framed as my own:
**Digging into the io_uring flags**
Right, let's see, that `IO_RING_F_OFF_TIMEOUT_USED` flag... that's set only *once* and then never cleared? Interesting. I'll need to figure out what implications that has. I suspect it's about a specific edge case. Now, I want to pivot to investigating `IO_RING_F_HAS_EVFD`. That seems like it could be important for event handling, right? Let's get the grep out and see where `IO_RING_F_HAS_EVFD` is being used and modified throughout the codebase. Time to follow the breadcrumbs.
|
| 40/2 |
2026/11/19 05:49 |
tool |
grepper |
0m
Args: map[Expression:IO_RING_F_HAS_EVFD]
Results: map[Output:include/linux/io_uring_types.h=314=enum {
--
include/linux/io_uring_types.h-319- IO_RING_F_DRAIN_ACTIVE = BIT(4),
include/linux/io_uring_types.h:320: IO_RING_F_HAS_EVFD = BIT(5),
include/linux/io_uring_types.h-321- /* all CQEs should be posted only by the submitter task */
--
io_uring/eventfd.c=120=int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg,
--
io_uring/eventfd.c-151- ev_fd->eventfd_async = eventfd_async;
io_uring/eventfd.c:152: ctx->int_flags |= IO_RING_F_HAS_EVFD;
io_uring/eventfd.c-153- refcount_set(&ev_fd->refs, 1);
--
io_uring/eventfd.c=159=int io_eventfd_unregister(struct io_ring_ctx *ctx)
--
io_uring/eventfd.c-165- if (ev_fd) {
io_uring/eventfd.c:166: ctx->int_flags &= ~IO_RING_F_HAS_EVFD;
io_uring/eventfd.c-167- rcu_assign_pointer(ctx->io_ev_fd, NULL);
--
io_uring/io_uring.c=531=void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
--
io_uring/io_uring.c-536- io_flush_timeouts(ctx);
io_uring/io_uring.c:537: if (ctx->int_flags & IO_RING_F_HAS_EVFD)
io_uring/io_uring.c-538- io_eventfd_signal(ctx, true, false);
--
io_uring/io_uring.h=503=static inline void io_req_complete_defer(struct io_kiocb *req)
--
io_uring/io_uring.h-513-#define SHOULD_FLUSH_MASK (IO_RING_F_OFF_TIMEOUT_USED | \
io_uring/io_uring.h:514: IO_RING_F_HAS_EVFD | IO_RING_F_POLL_ACTIVATED)
io_uring/io_uring.h-515-
--
io_uring/tw.c=182=void io_req_local_work_add(struct io_kiocb *req, unsigned flags)
--
io_uring/tw.c-205- io_ctx_mark_taskrun(ctx);
io_uring/tw.c:206: if (data_race(ctx->int_flags) & IO_RING_F_HAS_EVFD)
io_uring/tw.c-207- io_eventfd_signal(ctx, false, flags & IOU_F_TWQ_IN_WAKE);
]
|
| 41/2 |
2026/11/19 05:49 |
llm |
expert |
0m
Model:
Tokens:
input: 45163
output: 38
thoughts: 75
Thoughts: Okay, let's break this down.
**Investigating the IO Eventfd Signaling Mechanism**
Right, so I'm focusing on how the `io_eventfd` functionality interacts with the io_uring system, specifically the eventfd signaling. I see that the `IO_RING_F_HAS_EVFD` flag is being set in `io_eventfd_register` – that makes sense, as that's where you'd expect to establish the connection, and conversely, cleared in `io_eventfd_unregister` to tear it down.
Now, the crucial part is what actually *triggers* the signal. I need to understand the signaling path. Therefore, my immediate thought is to delve into the `io_eventfd_signal` function. I'm going to carefully scrutinize what's happening there with `ctx->io_ev_fd`. That's where I expect to find the actual mechanism for the eventfd signaling when an event completion occurs. The flag tells us *when* to signal, but not *how*. So, let's see what the function does with the context-specific file descriptor!
|
| 42/2 |
2026/11/19 05:49 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:io_uring/eventfd.c Name:io_eventfd_signal]
Results: map[SourceCode: 76: void io_eventfd_signal(struct io_ring_ctx *ctx, bool cqe_event, bool defer)
77: {
78: bool skip = false;
79: struct io_ev_fd *ev_fd;
80: struct io_rings *rings;
81:
82: guard(rcu)();
83:
84: rings = rcu_dereference(ctx->rings_rcu);
85: if (!rings)
86: return;
87: if (READ_ONCE(rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
88: return;
89: ev_fd = rcu_dereference(ctx->io_ev_fd);
90: /*
91: * Check again if ev_fd exists in case an io_eventfd_unregister call
92: * completed between the NULL check of ctx->io_ev_fd at the start of
93: * the function and rcu_read_lock.
94: */
95: if (!ev_fd)
96: return;
97: if (!io_eventfd_trigger(ev_fd) || !refcount_inc_not_zero(&ev_fd->refs))
98: return;
99:
100: if (cqe_event) {
101: /*
102: * Eventfd should only get triggered when at least one event
103: * has been posted. Some applications rely on the eventfd
104: * notification count only changing IFF a new CQE has been
105: * added to the CQ ring. There's no dependency on 1:1
106: * relationship between how many times this function is called
107: * (and hence the eventfd count) and number of CQEs posted to
108: * the CQ ring.
109: */
110: spin_lock(&ctx->completion_lock);
111: skip = ctx->cached_cq_tail == ev_fd->last_cq_tail;
112: ev_fd->last_cq_tail = ctx->cached_cq_tail;
113: spin_unlock(&ctx->completion_lock);
114: }
115:
116: if (skip || __io_eventfd_signal(ev_fd, defer))
117: io_eventfd_put(ev_fd);
118: }
SourceFile:io_uring/eventfd.c]
|
| 43/2 |
2026/11/19 05:49 |
llm |
expert |
0m
Model:
Tokens:
input: 46219
output: 40
thoughts: 263
Thoughts: **My Analysis of Eventfd Synchronization in the IO_uring Context**
Okay, let's break down this `io_eventfd` synchronization strategy. The use of RCU in `io_eventfd_signal` is immediately apparent: `rcu_dereference(ctx->io_ev_fd)` is the key. This is how they're ensuring safe access to the `io_ev_fd` pointer. The unregister function, `io_eventfd_unregister`, is where things get interesting. `rcu_replace_pointer` is used to atomically swap out the `io_ev_fd` with NULL. The important thing is that the old value is protected. Even if a reader observes a stale `IO_RING_F_HAS_EVFD` flag, `io_eventfd_signal` immediately re-checks the pointer with `rcu_dereference` and exits gracefully if it's NULL. This is critical for preventing use-after-free conditions. I need to now see how `io_poll_wq_wake` and `io_flush_timeouts` interact with this mechanism. This helps me understand the overall memory model.
|
| 44/2 |
2026/11/19 05:49 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:io_uring/io_uring.c Name:io_poll_wq_wake]
Results: map[SourceCode: 446: static inline void io_poll_wq_wake(struct io_ring_ctx *ctx)
447: {
448: __io_wq_wake(&ctx->poll_wq);
449: }
SourceFile:io_uring/io_uring.h]
|
| 45/2 |
2026/11/19 05:49 |
llm |
expert |
0m
Model:
Tokens:
input: 46868
output: 36
thoughts: 31
Thoughts: Okay, here's my interpretation of those thoughts, framed as my own:
**Waking Up & Flushing Out**
Right, so `io_poll_wq_wake` is the function that gets `ctx->poll_wq` going. That much is clear. It's the mechanism for getting the context's polling workqueue to, well, *wake up*. Seems straightforward.
But the real question now is, what's `io_flush_timeouts` all about? That's the next piece I need to understand. I'm guessing it's related to clearing out or processing any pending timeout events associated with this I/O polling setup. Maybe cancelling timers, handling expired events, something along those lines. I need to dig into its implementation to see how it ties into the broader I/O management strategy. Knowing how these timeouts are handled is critical for performance and responsiveness, especially in concurrent systems like this.
|
| 46/2 |
2026/11/19 05:49 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:io_uring/timeout.c Name:io_flush_timeouts]
Results: map[SourceCode: 165: __cold void io_flush_timeouts(struct io_ring_ctx *ctx)
166: {
167: struct io_timeout *timeout, *tmp;
168: LIST_HEAD(list);
169: u32 seq;
170:
171: raw_spin_lock_irq(&ctx->timeout_lock);
172: seq = READ_ONCE(ctx->cached_cq_tail) - atomic_read(&ctx->cq_timeouts);
173:
174: /*
175: * A fired timeout bumps ->cq_timeouts before its CQE is posted, which
176: * leaves the sequence behind the last flush until it is. No event has
177: * happened then, see io_timeout().
178: */
179: if ((s32)(seq - ctx->cq_last_tm_flush) < 0)
180: seq = ctx->cq_last_tm_flush;
181:
182: list_for_each_entry_safe(timeout, tmp, &ctx->timeout_list, list) {
183: struct io_kiocb *req = cmd_to_io_kiocb(timeout);
184: u32 events_needed, events_got;
185:
186: if (io_is_timeout_noseq(req))
187: break;
188:
189: /*
190: * Since seq can easily wrap around over time, subtract
191: * the last seq at which timeouts were flushed before comparing.
192: * Assuming not more than 2^31-1 events have happened since,
193: * these subtractions won't have wrapped, so we can check if
194: * target is in [last_seq, current_seq] by comparing the two.
195: */
196: events_needed = timeout->target_seq - ctx->cq_last_tm_flush;
197: events_got = seq - ctx->cq_last_tm_flush;
198: if (events_got < events_needed)
199: break;
200:
201: io_kill_timeout(req, &list);
202: }
203: ctx->cq_last_tm_flush = seq;
204: raw_spin_unlock_irq(&ctx->timeout_lock);
205: io_flush_killed_timeouts(&list, 0);
206: }
SourceFile:io_uring/timeout.c]
|
| 47/2 |
2026/11/19 05:49 |
llm |
expert |
0m
Model:
Tokens:
input: 47859
output: 43
thoughts: 1792
Thoughts: **Analyzing a Potential Data Race in io_uring Flushing**
Okay, here's what's running through my head. I'm looking at this KCSAN report, and it seems like there's a potential race condition involving `io_flush_timeouts`. I see that it acquires a `raw_spin_lock_irq(&ctx->timeout_lock)`. So, the actions are synchronized, but let's see how things are structured. `io_poll_wq_wake`, `io_flush_timeouts`, and `io_eventfd_signal` all have their own synchronization mechanisms (`poll_wq`, `timeout_lock`, and RCU, respectively).
Now, let's zoom in on what KCSAN flagged. It's flagging an issue where task 3754 is running `io_init_req`, and it conditionally sets `ctx->int_flags |= IO_RING_F_DRAIN_DISABLED;`. Meanwhile, task 3758, in `__io_commit_cqring_flush`, checks if `ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED`.
The reason task 3758 is even *in* `__io_commit_cqring_flush` is because the `data_race(ctx->int_flags) & SHOULD_FLUSH_MASK` check in `io_commit_cqring_flush` was true. Specifically, `data_race()` returned a value that had one of the bits set in `SHOULD_FLUSH_MASK` (e.g., `IO_RING_F_POLL_ACTIVATED`, `IO_RING_F_OFF_TIMEOUT_USED`, or `IO_RING_F_HAS_EVFD`). That's how it ended up calling `__io_commit_cqring_flush`.
Inside `__io_commit_cqring_flush`, there are a few `if` statements that check the same flags *again*, but this time without the `data_race()` wrapper. It's the `if (ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED)` check that triggered KCSAN.
Now, why did the warning hit line 535 and not 533? It's likely because of timing. Either the `IO_RING_F_POLL_ACTIVATED` bit was read first or the watchpoint was set on the *writer*'s write. Remember that KCSAN placed a watchpoint on the read at line 535 (or possibly on the write itself) and the write (or read, respectively) on the other CPU triggered it. KCSAN sees the read and write to `0xffff88811c6db004` (which is `&ctx->int_flags`).
Thinking back, I recall Jens Axboe adding the `data_race()` call in commit 8c557449199e to `io_commit_cqring_flush`. Before that commit, the check was just a direct bitwise AND. So, KCSAN *was* flagging this before, but that commit *silenced* the warning by adding `data_race()` to `io_commit_cqring_flush(ctx)`, but it then proceeds into the critical section, `__io_commit_cqring_flush(ctx)`! But, the critical section doesn't have the `data_race()`! This is weird, `__io_commit_cqring_flush` really should have the `data_race()` check, too.
So, is this a real problem? Let's analyze.
From the "BENIGN" criteria, this seems like it *might* fit the "Single-Writer Flag Updates" case, where a concurrent read is a simple bitwise check. However, the logic *might* be sound, but let's check it:
The writer (task 3754) is setting `IO_RING_F_DRAIN_DISABLED` (bit 10). The reader is checking `IO_RING_F_POLL_ACTIVATED` (bit 9), `IO_RING_F_OFF_TIMEOUT_USED` (bit 3), and `IO_RING_F_HAS_EVFD` (bit 5). Those bits are independent, which is good. But, this check is still subject to the race, so now it is a question if either the old or new bit value can cause a problem.
Now, I need to check if `ctx->int_flags` is a properly aligned 32-bit integer, by checking the structure with `codesearch-struct-layout`.
|
| 48/2 |
2026/11/19 05:49 |
tool |
codesearch-struct-layout |
0m
Args: map[ContextFile:io_uring/io_uring.c FieldOffset:4 Name:io_ring_ctx]
Results: map[Fields:[map[Name: OffsetBits:0 SizeBits:512]]]
|
| 49/2 |
2026/11/19 05:49 |
llm |
expert |
0m
Model:
Tokens:
input: 50737
output: 43
thoughts: 11
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person:
**Analyzing Struct/Field Layout - A Quick Deep Dive**
Alright, before I dive too deep into this specific issue, my first instinct is to get a holistic view. I need to understand the complete structure – or even just the relevant field within the struct – and how it's laid out. Jumping directly to a specific function or piece of code, before confirming this baseline, could be a waste of time and could lead me down a wrong path, if I'm misunderstanding memory allocation or data alignment. Let me just take a moment to confirm how everything fits together.
|
| 50/2 |
2026/11/19 05:49 |
tool |
codesearch-struct-layout |
0m
Args: map[ContextFile:io_uring/io_uring.c FieldOffset:0 Name:io_ring_ctx]
Results: map[Fields:[map[Name: OffsetBits:0 SizeBits:512]]]
|
| 51/2 |
2026/11/19 05:49 |
llm |
expert |
0m
Model:
Tokens:
input: 50966
output: 38
thoughts: 16
Thoughts: Here's the summary, tailored for an expert audience:
**Analyzing Struct Layout: Initial Inspection**
Okay, so before I start optimizing this struct, I want a clean baseline. Let's see how the compiler, without any specific `FieldOffset` attributes, is laying out these fields. This initial look will be critical in understanding the default alignment and any implicit padding the compiler is inserting. I need to be sure I understand the natural alignment characteristics before I start mucking about with explicit offsets. It's the best way to determine the extent to which I can improve its layout by using `FieldOffset`, and also a quick sanity check before assuming `FieldOffset` is *actually* necessary.
|
| 52/2 |
2026/11/19 05:50 |
tool |
codesearch-struct-layout |
0m
Args: map[ContextFile:io_uring/io_uring.c Name:io_ring_ctx]
Results: map[Fields:[map[Name: OffsetBits:0 SizeBits:512] map[Name: OffsetBits:512 SizeBits:3584] map[Name: OffsetBits:4096 SizeBits:512] map[Name: OffsetBits:4608 SizeBits:512] map[Name: OffsetBits:5120 SizeBits:512] map[Name:completion_lock OffsetBits:5632 SizeBits:32] map[Name:cq_overflow_list OffsetBits:5696 SizeBits:128] map[Name:waitid_list OffsetBits:5824 SizeBits:64] map[Name:futex_list OffsetBits:5888 SizeBits:64] map[Name:futex_cache OffsetBits:5952 SizeBits:192] map[Name:sq_creds OffsetBits:6144 SizeBits:64] map[Name:sq_data OffsetBits:6208 SizeBits:64] map[Name:sqo_sq_wait OffsetBits:6272 SizeBits:192] map[Name:sqd_list OffsetBits:6464 SizeBits:128] map[Name:file_alloc_start OffsetBits:6592 SizeBits:32] map[Name:file_alloc_end OffsetBits:6624 SizeBits:32] map[Name:poll_wq OffsetBits:6656 SizeBits:192] map[Name:restrictions OffsetBits:6848 SizeBits:320] map[Name:zcrx_ctxs OffsetBits:7168 SizeBits:128] map[Name:hpage_acct OffsetBits:7296 SizeBits:128] map[Name:pers_next OffsetBits:7424 SizeBits:32] map[Name:personalities OffsetBits:7488 SizeBits:128] map[Name:hash_map OffsetBits:7616 SizeBits:64] map[Name:user OffsetBits:7680 SizeBits:64] map[Name:mm_account OffsetBits:7744 SizeBits:64] map[Name:tctx_list OffsetBits:7808 SizeBits:128] map[Name:tctx_lock OffsetBits:7936 SizeBits:192] map[Name:exit_work OffsetBits:8128 SizeBits:256] map[Name:ref_comp OffsetBits:8384 SizeBits:256] map[Name:iowq_limits OffsetBits:8640 SizeBits:64] map[Name:poll_wq_task_work OffsetBits:8704 SizeBits:128] map[Name:poll_wq_rcu OffsetBits:8832 SizeBits:128] map[Name:poll_wq_rcu_pending OffsetBits:8960 SizeBits:32] map[Name:defer_list OffsetBits:9024 SizeBits:128] map[Name:nr_drained OffsetBits:9152 SizeBits:32] map[Name:drain_req OffsetBits:9216 SizeBits:64] map[Name:nr_req_allocated OffsetBits:9280 SizeBits:32] map[Name:nr_notifs OffsetBits:9312 SizeBits:32] map[Name:napi_list OffsetBits:9344 SizeBits:128] map[Name:napi_lock OffsetBits:9472 SizeBits:32] map[Name:napi_busy_poll_dt OffsetBits:9536 SizeBits:64] map[Name:napi_prefer_busy_poll OffsetBits:9600 SizeBits:8] map[Name:napi_track_mode OffsetBits:9608 SizeBits:8] map[Name:napi_ht OffsetBits:9664 SizeBits:1024] map[Name:bpf_ops OffsetBits:10688 SizeBits:64] map[Name:mmap_lock OffsetBits:10752 SizeBits:192] map[Name:sq_region OffsetBits:10944 SizeBits:192] map[Name:ring_region OffsetBits:11136 SizeBits:192] map[Name:param_region OffsetBits:11328 SizeBits:192] map[Name:kcov_handle OffsetBits:11520 SizeBits:64]]]
|
| 53/2 |
2026/11/19 05:50 |
llm |
expert |
0m
Model:
Tokens:
input: 52345
output: 24
thoughts: 1673
Thoughts: **Analyzing the io_uring KCSAN Report: A Deep Dive into Memory Ordering**
Okay, let's break this down. First, I see the KCSAN report showing a write and read of a 32-bit integer at the same 4-byte aligned address. Given that it's aligned and a 32-bit value on a modern architecture, I'm confident that it's a single-copy atomic operation. So, no tearing. My immediate concern is, could there be a compiler optimization or reordering of operations causing an issue?
I'll start by looking at the code around the reported accesses. The report points to `__io_commit_cqring_flush`. This function checks a few flags in `ctx->int_flags` to perform different actions: waking a poll wait queue, flushing timeouts, and signaling an eventfd. No loops here, and it's called after CQEs are posted.
Now, let's dissect the potential for ordering violations, which seems like the most likely scenario.
1. **`io_poll_wq_wake(ctx)`**: This wakes up `poll_wq` if the `IO_RING_F_POLL_ACTIVATED` flag is set. The setting of this flag involves a mutex lock, a setting of the flag, and a barrier. The crucial part is that the function that sets this flag *also* wakes up the waitqueue. Critically, `IO_RING_F_POLL_ACTIVATED` is *never* cleared once set. So, if `__io_commit_cqring_flush` reads a stale value (0) before the write, `io_activate_pollwq_cb` wakes up the `poll_wq` anyway. If it reads the current value, it's safe. Therefore, this branch is safe either way.
2. **`io_flush_timeouts(ctx)`**: This handles timeout flushing. `IO_RING_F_OFF_TIMEOUT_USED` indicates the use of offset-based timeouts. This flag is set in `__io_timeout_prep` and never cleared. A stale read here would mean no timeout flushing, which is fine since the timeout wasn't used. And, it's impossible for an offset timeout to be completed *before* the setup is done. Crucially, the code takes `ctx->timeout_lock` when manipulating the timeout list. Further, the KCSAN report shows the write is *not* even related to this flag. The write is setting `IO_RING_F_DRAIN_DISABLED`, an unrelated flag!
3. **`io_eventfd_signal(ctx, true, false)`**: This signals an eventfd if `IO_RING_F_HAS_EVFD` is set. The code uses RCU reads. If I read a stale value after an unregister, the pointer is NULL, and no action is taken. If I read a stale value *during* registration, it may miss *one* signal. But it has just been called and any following CQE flush will get it.
4. **The Actual Write**: The report shows the write is `ctx->int_flags |= IO_RING_F_DRAIN_DISABLED;`. `IO_RING_F_DRAIN_DISABLED` is bit 10. Does anything clear this flag? (Need to check). It seems like the core issue lies in the possibility of seeing a stale value for *any* of those three flags. But I don't see how it can cause a problem.
The report suggests a potential issue, but after a thorough examination, I can't identify a definite problem with the memory ordering. The flags are handled in a way that handles the case of reading stale values.
|
| 54/2 |
2026/11/19 05:50 |
tool |
grepper |
0m
Args: map[Expression:IO_RING_F_DRAIN_DISABLED]
Results: map[Output:include/linux/io_uring_types.h=314=enum {
--
include/linux/io_uring_types.h-325- IO_RING_F_POLL_ACTIVATED = BIT(9),
include/linux/io_uring_types.h:326: IO_RING_F_DRAIN_DISABLED = BIT(10),
include/linux/io_uring_types.h-327- IO_RING_F_COMPAT = BIT(11),
--
io_uring/io_uring.c=1875=static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
--
io_uring/io_uring.c-1938- if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS)
io_uring/io_uring.c:1939: ctx->int_flags |= IO_RING_F_DRAIN_DISABLED;
io_uring/io_uring.c-1940- if (sqe_flags & IOSQE_IO_DRAIN) {
io_uring/io_uring.c:1941: if (ctx->int_flags & IO_RING_F_DRAIN_DISABLED)
io_uring/io_uring.c-1942- return io_init_fail_req(req, -EOPNOTSUPP);
]
|
| 55/2 |
2026/11/19 05:50 |
llm |
expert |
0m
Model:
Tokens:
input: 55198
output: 38
thoughts: 1284
Thoughts: **Analyzing `IO_RING_F_DRAIN_DISABLED` and Concurrency in io_uring**
Okay, so I'm digging into how `IO_RING_F_DRAIN_DISABLED` is handled in io_uring. The key point is that it's set once and never cleared, which immediately suggests potential concurrency concerns. If a request with `IOSQE_CQE_SKIP_SUCCESS` is submitted, then the drain feature can't be used with this ring, and `IO_RING_F_DRAIN_DISABLED` is flipped on. This write, crucially, is protected by `ctx->uring_lock`.
Initially, I was wondering why the write even happened again if the flag was already set. But then, I remembered that every SQE with `IOSQE_CQE_SKIP_SUCCESS` unconditionally executes `ctx->int_flags |= IO_RING_F_DRAIN_DISABLED`. It's a safe overwrite in the case that the bit is already set.
Then, the bit clobbering issue hit me! Could two threads be writing to `ctx->int_flags` concurrently, potentially corrupting the value? This warrants a deeper look.
I've examined *every* writer to `ctx->int_flags` and found that each access is guarded by `ctx->uring_lock`. I need to be absolutely certain. Let's list those writes and confirm:
1. `io_eventfd_register`: called from `io_uring_register` -> holds `uring_lock`.
2. `io_eventfd_unregister`: called from `io_uring_register` -> holds `uring_lock`.
3. `__io_submit_flush_completions`: called with `uring_lock` held (we saw `lockdep_assert_held(&ctx->uring_lock)`).
4. `io_drain_req`: called from `io_queue_sqe_fallback` during submission under `uring_lock`.
5. `io_init_drain`: called from `io_init_req` under `uring_lock`.
6. `io_init_req`: called from `io_submit_sqes` under `uring_lock`.
7. `io_activate_pollwq_cb`: takes `mutex_lock(&ctx->uring_lock)`.
8. `io_ctx_restriction_clone` & `io_uring_create`: creation time.
9. `io_register_restrictions`: under `uring_lock`.
10. `io_register_iowq_max_workers`: under `uring_lock`.
11. `__io_timeout_prep`: called from prep during submission under `uring_lock`.
Bingo! Every single write to `ctx->int_flags` is protected by `uring_lock`. This means there can *never* be concurrent writers to this flag. No bit clobbering is possible between concurrent writes, because the concurrent writes do not exist!
Now, what about the possibility of *readers* racing with the writer? The writers are all protected by the lock. But I found a reader, `io_commit_cqring_flush()`, which, *thankfully*, correctly uses `data_race(ctx->int_flags)`! This indicates a potential data race with the writer. The `data_race()` check, if true, calls `__io_commit_cqring_flush(ctx)`. But, in examining this function, I found that it reads `ctx->int_flags` too!
Here is the problem. It seems like the inline function `io_commit_cqring_flush()` was updated with `data_race()`. However, the function that it *calls*, `__io_commit_cqring_flush()`, *also* reads `ctx->int_flags`, and it is *missing* `data_race()`. This is a bug! Jens Axboe's change in commit 8c557449199e added `data_race()` to `io_commit_cqring_flush()`, but didn't realize that the called function needed it too. It looks like `__io_commit_cqring_flush()`'s git history needs to be checked to see how it was developed.
|
| 56/2 |
2026/11/19 05:50 |
tool |
git-log |
0m
Error:
Args: map[Count:5 PathPrefix:io_uring SymbolName:__io_commit_cqring_flush]
Results:
|
| 57/2 |
2026/11/19 05:50 |
llm |
expert |
0m
Model:
Tokens:
input: 57474
output: 44
thoughts: 28
Thoughts: Okay, here's my interpretation of that thought process, assuming I'm an expert in the field and considering how I'd approach it:
**Investigating a Kernel Module's History with Git**
Alright, I'm thinking I need to trace the evolution of that `io_uring` code, specifically focusing on `io_uring.c`. My instinct is to use `git log`, the workhorse. I want to be *precise* in what I get back. Just seeing a general "io_uring.c" file path might not cut it. I think adding `SymbolName` will help with focusing on relevant changes, and I'll specify the `SourcePath` to ensure I'm targeting the right file within that module. My goal is a clear, concise history of modifications, ideally tied to specific function calls or variable declarations. I'm expecting this will give me exactly the information I need.
|
| 58/2 |
2026/11/19 05:50 |
tool |
git-log |
0m
Args: map[Count:5 SourcePath:io_uring/io_uring.c SymbolName:__io_commit_cqring_flush]
Results: map[Output:cd305ee3633a io_uring: defer eventfd signaling when queued from a wakeup handler
f1a424e21c15 io_uring: switch struct io_ring_ctx internal bitfields to flags
8fb7aee05591 io_uring: drain based on allocates reqs
62f666df765e io_uring/eventfd: dedup signalling helpers
d62c2f0d8275 io_uring: ensure io_queue_deferred() is out-of-line
]
|
| 59/2 |
2026/11/19 05:50 |
llm |
expert |
0m
Model:
Tokens:
input: 57880
output: 27
thoughts: 43
Thoughts: Okay, let's dive into this.
**Examining the `io_uring` Commit: `f1a424e21c15`**
Alright, I'm taking a look at commit `f1a424e21c15`, specifically the one dealing with `io_uring` and the change from internal bitfields to flags within the `io_ring_ctx` structure. This is interesting. I'm immediately thinking about the potential implications. Replacing bitfields with flags often indicates an intention to improve readability, maintainability, or potentially introduce more flexibility down the line. I need to understand what specific bitfields they're targeting within `io_ring_ctx` to gauge the significance of this change. Is this a straightforward cleanup, or are they subtly setting the stage for something bigger? Are we talking about performance, API improvements, or perhaps addressing some underlying complexity? Let's dig into the code and see what's changed and why. I'm expecting to see a clear rationale in the commit message that either explicitly details the benefits or points toward the next logical step in the evolution of `io_uring`. I want to see if the flags are being handled carefully to avoid regressions or unintended side effects. Time to get to work!
|
| 60/2 |
2026/11/19 05:50 |
tool |
git-show |
0m
Args:
Results: map[Output:commit f1a424e21c15993db0f9594cda17ef5d516ab3e9
Author: Jens Axboe <axboe@kernel.dk>
Date: Sat Mar 14 08:41:04 2026 -0600
io_uring: switch struct io_ring_ctx internal bitfields to flags
Bitfields cannot be set and checked atomically, and this makes it more
clear that these are indeed in shared storage and must be checked and
set in a sane fashion. This is in preparation for annotating a few of
the known racy, but harmless, flags checking.
No intended functional changes in this patch.
Reviewed-by: Gabriel Krisman Bertazi <krisman@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h
index dd1420bfcb73..0b3f08adc217 100644
--- a/include/linux/io_uring_types.h
+++ b/include/linux/io_uring_types.h
@@ -268,24 +268,30 @@ struct io_alloc_cache {
unsigned int init_clear;
};
+enum {
+ IO_RING_F_DRAIN_NEXT = BIT(0),
+ IO_RING_F_OP_RESTRICTED = BIT(1),
+ IO_RING_F_REG_RESTRICTED = BIT(2),
+ IO_RING_F_OFF_TIMEOUT_USED = BIT(3),
+ IO_RING_F_DRAIN_ACTIVE = BIT(4),
+ IO_RING_F_HAS_EVFD = BIT(5),
+ /* all CQEs should be posted only by the submitter task */
+ IO_RING_F_TASK_COMPLETE = BIT(6),
+ IO_RING_F_LOCKLESS_CQ = BIT(7),
+ IO_RING_F_SYSCALL_IOPOLL = BIT(8),
+ IO_RING_F_POLL_ACTIVATED = BIT(9),
+ IO_RING_F_DRAIN_DISABLED = BIT(10),
+ IO_RING_F_COMPAT = BIT(11),
+ IO_RING_F_IOWQ_LIMITS_SET = BIT(12),
+};
+
struct io_ring_ctx {
/* const or read-mostly hot data */
struct {
+ /* ring setup flags */
unsigned int flags;
- unsigned int drain_next: 1;
- unsigned int op_restricted: 1;
- unsigned int reg_restricted: 1;
- unsigned int off_timeout_used: 1;
- unsigned int drain_active: 1;
- unsigned int has_evfd: 1;
- /* all CQEs should be posted only by the submitter task */
- unsigned int task_complete: 1;
- unsigned int lockless_cq: 1;
- unsigned int syscall_iopoll: 1;
- unsigned int poll_activated: 1;
- unsigned int drain_disabled: 1;
- unsigned int compat: 1;
- unsigned int iowq_limits_set : 1;
+ /* internal state flags IO_RING_F_* flags , mostly read-only */
+ unsigned int int_flags;
struct task_struct *submitter_task;
struct io_rings *rings;
diff --git a/io_uring/eventfd.c b/io_uring/eventfd.c
index 7482a7dc6b38..3da028500f76 100644
--- a/io_uring/eventfd.c
+++ b/io_uring/eventfd.c
@@ -148,7 +148,7 @@ int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg,
spin_unlock(&ctx->completion_lock);
ev_fd->eventfd_async = eventfd_async;
- ctx->has_evfd = true;
+ ctx->int_flags |= IO_RING_F_HAS_EVFD;
refcount_set(&ev_fd->refs, 1);
atomic_set(&ev_fd->ops, 0);
rcu_assign_pointer(ctx->io_ev_fd, ev_fd);
@@ -162,7 +162,7 @@ int io_eventfd_unregister(struct io_ring_ctx *ctx)
ev_fd = rcu_dereference_protected(ctx->io_ev_fd,
lockdep_is_held(&ctx->uring_lock));
if (ev_fd) {
- ctx->has_evfd = false;
+ ctx->int_flags &= ~IO_RING_F_HAS_EVFD;
rcu_assign_pointer(ctx->io_ev_fd, NULL);
io_eventfd_put(ev_fd);
return 0;
diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index 9a37035e76c0..bfeb3bc3849d 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -477,17 +477,17 @@ static __cold noinline void io_queue_deferred(struct io_ring_ctx *ctx)
void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
{
- if (ctx->poll_activated)
+ if (ctx->int_flags & IO_RING_F_POLL_ACTIVATED)
io_poll_wq_wake(ctx);
- if (ctx->off_timeout_used)
+ if (ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED)
io_flush_timeouts(ctx);
- if (ctx->has_evfd)
+ if (ctx->int_flags & IO_RING_F_HAS_EVFD)
io_eventfd_signal(ctx, true);
}
static inline void __io_cq_lock(struct io_ring_ctx *ctx)
{
- if (!ctx->lockless_cq)
+ if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ))
spin_lock(&ctx->completion_lock);
}
@@ -500,11 +500,11 @@ static inline void io_cq_lock(struct io_ring_ctx *ctx)
static inline void __io_cq_unlock_post(struct io_ring_ctx *ctx)
{
io_commit_cqring(ctx);
- if (!ctx->task_complete) {
- if (!ctx->lockless_cq)
+ if (!(ctx->int_flags & IO_RING_F_TASK_COMPLETE)) {
+ if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ))
spin_unlock(&ctx->completion_lock);
/* IOPOLL rings only need to wake up if it's also SQPOLL */
- if (!ctx->syscall_iopoll)
+ if (!(ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL))
io_cqring_wake(ctx);
}
io_commit_cqring_flush(ctx);
@@ -830,7 +830,7 @@ bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags
void io_add_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags)
{
lockdep_assert_held(&ctx->uring_lock);
- lockdep_assert(ctx->lockless_cq);
+ lockdep_assert(ctx->int_flags & IO_RING_F_LOCKLESS_CQ);
if (!io_fill_cqe_aux(ctx, user_data, res, cflags)) {
struct io_cqe cqe = io_init_cqe(user_data, res, cflags);
@@ -860,7 +860,7 @@ bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags)
lockdep_assert(!io_wq_current_is_worker());
lockdep_assert_held(&ctx->uring_lock);
- if (!ctx->lockless_cq) {
+ if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) {
spin_lock(&ctx->completion_lock);
posted = io_fill_cqe_aux(ctx, req->cqe.user_data, res, cflags);
spin_unlock(&ctx->completion_lock);
@@ -885,7 +885,7 @@ bool io_req_post_cqe32(struct io_kiocb *req, struct io_uring_cqe cqe[2])
lockdep_assert_held(&ctx->uring_lock);
cqe[0].user_data = req->cqe.user_data;
- if (!ctx->lockless_cq) {
+ if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) {
spin_lock(&ctx->completion_lock);
posted = io_fill_cqe_aux32(ctx, cqe);
spin_unlock(&ctx->completion_lock);
@@ -913,7 +913,7 @@ static void io_req_complete_post(struct io_kiocb *req, unsigned issue_flags)
* Handle special CQ sync cases via task_work. DEFER_TASKRUN requires
* the submitter task context, IOPOLL protects with uring_lock.
*/
- if (ctx->lockless_cq || (req->flags & REQ_F_REISSUE)) {
+ if ((ctx->int_flags & IO_RING_F_LOCKLESS_CQ) || (req->flags & REQ_F_REISSUE)) {
defer_complete:
req->io_task_work.func = io_req_task_complete;
io_req_task_work_add(req);
@@ -1135,7 +1135,7 @@ void __io_submit_flush_completions(struct io_ring_ctx *ctx)
*/
if (!(req->flags & (REQ_F_CQE_SKIP | REQ_F_REISSUE)) &&
unlikely(!io_fill_cqe_req(ctx, req))) {
- if (ctx->lockless_cq)
+ if (ctx->int_flags & IO_RING_F_LOCKLESS_CQ)
io_cqe_overflow(ctx, &req->cqe, &req->big_cqe);
else
io_cqe_overflow_locked(ctx, &req->cqe, &req->big_cqe);
@@ -1148,7 +1148,7 @@ void __io_submit_flush_completions(struct io_ring_ctx *ctx)
INIT_WQ_LIST(&state->compl_reqs);
}
- if (unlikely(ctx->drain_active))
+ if (unlikely(ctx->int_flags & IO_RING_F_DRAIN_ACTIVE))
io_queue_deferred(ctx);
ctx->submit_state.cq_flush = false;
@@ -1344,7 +1344,7 @@ static __cold void io_drain_req(struct io_kiocb *req)
list_add_tail(&de->list, &ctx->defer_list);
io_queue_deferred(ctx);
if (!drain && list_empty(&ctx->defer_list))
- ctx->drain_active = false;
+ ctx->int_flags &= ~IO_RING_F_DRAIN_ACTIVE;
}
static bool io_assign_file(struct io_kiocb *req, const struct io_issue_def *def,
@@ -1655,7 +1655,7 @@ static void io_queue_sqe_fallback(struct io_kiocb *req)
} else {
/* can't fail with IO_URING_F_INLINE */
io_req_sqe_copy(req, IO_URING_F_INLINE);
- if (unlikely(req->ctx->drain_active))
+ if (unlikely(req->ctx->int_flags & IO_RING_F_DRAIN_ACTIVE))
io_drain_req(req);
else
io_queue_iowq(req);
@@ -1671,7 +1671,7 @@ static inline bool io_check_restriction(struct io_ring_ctx *ctx,
struct io_kiocb *req,
unsigned int sqe_flags)
{
- if (!ctx->op_restricted)
+ if (!(ctx->int_flags & IO_RING_F_OP_RESTRICTED))
return true;
if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
return false;
@@ -1691,7 +1691,7 @@ static void io_init_drain(struct io_ring_ctx *ctx)
{
struct io_kiocb *head = ctx->submit_state.link.head;
- ctx->drain_active = true;
+ ctx->int_flags |= IO_RING_F_DRAIN_ACTIVE;
if (head) {
/*
* If we need to drain a request in the middle of a link, drain
@@ -1701,7 +1701,7 @@ static void io_init_drain(struct io_ring_ctx *ctx)
* link.
*/
head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
- ctx->drain_next = true;
+ ctx->int_flags |= IO_RING_F_DRAIN_NEXT;
}
}
@@ -1767,23 +1767,23 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
req->buf_index = READ_ONCE(sqe->buf_group);
}
if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS)
- ctx->drain_disabled = true;
+ ctx->int_flags |= IO_RING_F_DRAIN_DISABLED;
if (sqe_flags & IOSQE_IO_DRAIN) {
- if (ctx->drain_disabled)
+ if (ctx->int_flags & IO_RING_F_DRAIN_DISABLED)
return io_init_fail_req(req, -EOPNOTSUPP);
io_init_drain(ctx);
}
}
- if (unlikely(ctx->op_restricted || ctx->drain_active || ctx->drain_next)) {
+ if (unlikely(ctx->int_flags & (IO_RING_F_OP_RESTRICTED | IO_RING_F_DRAIN_ACTIVE | IO_RING_F_DRAIN_NEXT))) {
if (!io_check_restriction(ctx, req, sqe_flags))
return io_init_fail_req(req, -EACCES);
/* knock it to the slow queue path, will be drained there */
- if (ctx->drain_active)
+ if (ctx->int_flags & IO_RING_F_DRAIN_ACTIVE)
req->flags |= REQ_F_FORCE_ASYNC;
/* if there is no link, we're at "next" request and need to drain */
- if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) {
- ctx->drain_next = false;
- ctx->drain_active = true;
+ if (unlikely(ctx->int_flags & IO_RING_F_DRAIN_NEXT) && !ctx->submit_state.link.head) {
+ ctx->int_flags &= ~IO_RING_F_DRAIN_NEXT;
+ ctx->int_flags |= IO_RING_F_DRAIN_ACTIVE;
req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
}
}
@@ -2204,7 +2204,7 @@ static __cold void io_activate_pollwq_cb(struct callback_head *cb)
poll_wq_task_work);
mutex_lock(&ctx->uring_lock);
- ctx->poll_activated = true;
+ ctx->int_flags |= IO_RING_F_POLL_ACTIVATED;
mutex_unlock(&ctx->uring_lock);
/*
@@ -2219,9 +2219,9 @@ __cold void io_activate_pollwq(struct io_ring_ctx *ctx)
{
spin_lock(&ctx->completion_lock);
/* already activated or in progress */
- if (ctx->poll_activated || ctx->poll_wq_task_work.func)
+ if ((ctx->int_flags & IO_RING_F_POLL_ACTIVATED) || ctx->poll_wq_task_work.func)
goto out;
- if (WARN_ON_ONCE(!ctx->task_complete))
+ if (WARN_ON_ONCE(!(ctx->int_flags & IO_RING_F_TASK_COMPLETE)))
goto out;
if (!ctx->submitter_task)
goto out;
@@ -2242,7 +2242,7 @@ static __poll_t io_uring_poll(struct file *file, poll_table *wait)
struct io_ring_ctx *ctx = file->private_data;
__poll_t mask = 0;
- if (unlikely(!ctx->poll_activated))
+ if (unlikely(!(ctx->int_flags & IO_RING_F_POLL_ACTIVATED)))
io_activate_pollwq(ctx);
/*
* provides mb() which pairs with barrier from wq_has_sleeper
@@ -2607,7 +2607,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
goto out;
}
if (flags & IORING_ENTER_GETEVENTS) {
- if (ctx->syscall_iopoll)
+ if (ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL)
goto iopoll_locked;
/*
* Ignore errors, we'll soon call io_cqring_wait() and
@@ -2622,7 +2622,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
if (flags & IORING_ENTER_GETEVENTS) {
int ret2;
- if (ctx->syscall_iopoll) {
+ if (ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL) {
/*
* We disallow the app entering submit/complete with
* polling, but we still need to lock the ring to
@@ -2923,9 +2923,9 @@ static void io_ctx_restriction_clone(struct io_ring_ctx *ctx,
if (dst->bpf_filters)
WRITE_ONCE(ctx->bpf_filters, dst->bpf_filters->filters);
if (dst->op_registered)
- ctx->op_restricted = 1;
+ ctx->int_flags |= IO_RING_F_OP_RESTRICTED;
if (dst->reg_registered)
- ctx->reg_restricted = 1;
+ ctx->int_flags |= IO_RING_F_REG_RESTRICTED;
}
static __cold int io_uring_create(struct io_ctx_config *config)
@@ -2952,17 +2952,18 @@ static __cold int io_uring_create(struct io_ctx_config *config)
if ((ctx->flags & IORING_SETUP_DEFER_TASKRUN) &&
!(ctx->flags & IORING_SETUP_IOPOLL))
- ctx->task_complete = true;
+ ctx->int_flags |= IO_RING_F_TASK_COMPLETE;
- if (ctx->task_complete || (ctx->flags & IORING_SETUP_IOPOLL))
- ctx->lockless_cq = true;
+ if ((ctx->int_flags & IO_RING_F_TASK_COMPLETE) ||
+ (ctx->flags & IORING_SETUP_IOPOLL))
+ ctx->int_flags |= IO_RING_F_LOCKLESS_CQ;
/*
* lazy poll_wq activation relies on ->task_complete for synchronisation
* purposes, see io_activate_pollwq()
*/
- if (!ctx->task_complete)
- ctx->poll_activated = true;
+ if (!(ctx->int_flags & IO_RING_F_TASK_COMPLETE))
+ ctx->int_flags |= IO_RING_F_POLL_ACTIVATED;
/*
* When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
@@ -2972,9 +2973,10 @@ static __cold int io_uring_create(struct io_ctx_config *config)
*/
if (ctx->flags & IORING_SETUP_IOPOLL &&
!(ctx->flags & IORING_SETUP_SQPOLL))
- ctx->syscall_iopoll = 1;
+ ctx->int_flags |= IO_RING_F_SYSCALL_IOPOLL;
- ctx->compat = in_compat_syscall();
+ if (in_compat_syscall())
+ ctx->int_flags |= IO_RING_F_COMPAT;
if (!ns_capable_noaudit(&init_user_ns, CAP_IPC_LOCK))
ctx->user = get_uid(current_user());
diff --git a/io_uring/io_uring.h b/io_uring/io_uring.h
index 0fa844faf287..5cb1983043cd 100644
--- a/io_uring/io_uring.h
+++ b/io_uring/io_uring.h
@@ -211,7 +211,7 @@ static inline void io_lockdep_assert_cq_locked(struct io_ring_ctx *ctx)
if (ctx->flags & IORING_SETUP_IOPOLL) {
lockdep_assert_held(&ctx->uring_lock);
- } else if (!ctx->task_complete) {
+ } else if (!(ctx->int_flags & IO_RING_F_TASK_COMPLETE)) {
lockdep_assert_held(&ctx->completion_lock);
} else if (ctx->submitter_task) {
/*
@@ -228,7 +228,7 @@ static inline void io_lockdep_assert_cq_locked(struct io_ring_ctx *ctx)
static inline bool io_is_compat(struct io_ring_ctx *ctx)
{
- return IS_ENABLED(CONFIG_COMPAT) && unlikely(ctx->compat);
+ return IS_ENABLED(CONFIG_COMPAT) && unlikely(ctx->int_flags & IO_RING_F_COMPAT);
}
static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
@@ -472,8 +472,9 @@ static inline void io_req_complete_defer(struct io_kiocb *req)
static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx)
{
- if (unlikely(ctx->off_timeout_used ||
- ctx->has_evfd || ctx->poll_activated))
+ if (unlikely(ctx->int_flags & (IO_RING_F_OFF_TIMEOUT_USED |
+ IO_RING_F_HAS_EVFD |
+ IO_RING_F_POLL_ACTIVATED)))
__io_commit_cqring_flush(ctx);
}
diff --git a/io_uring/msg_ring.c b/io_uring/msg_ring.c
index 57ad0085869a..3ff9098573db 100644
--- a/io_uring/msg_ring.c
+++ b/io_uring/msg_ring.c
@@ -67,7 +67,7 @@ void io_msg_ring_cleanup(struct io_kiocb *req)
static inline bool io_msg_need_remote(struct io_ring_ctx *target_ctx)
{
- return target_ctx->task_complete;
+ return target_ctx->int_flags & IO_RING_F_TASK_COMPLETE;
}
static void io_msg_tw_complete(struct io_tw_req tw_req, io_tw_token_t tw)
diff --git a/io_uring/register.c b/io_uring/register.c
index 0148735f7711..489a6feaf228 100644
--- a/io_uring/register.c
+++ b/io_uring/register.c
@@ -184,9 +184,9 @@ static __cold int io_register_restrictions(struct io_ring_ctx *ctx,
return ret;
}
if (ctx->restrictions.op_registered)
- ctx->op_restricted = 1;
+ ctx->int_flags |= IO_RING_F_OP_RESTRICTED;
if (ctx->restrictions.reg_registered)
- ctx->reg_restricted = 1;
+ ctx->int_flags |= IO_RING_F_REG_RESTRICTED;
return 0;
}
@@ -384,7 +384,7 @@ static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
for (i = 0; i < ARRAY_SIZE(new_count); i++)
if (new_count[i])
ctx->iowq_limits[i] = new_count[i];
- ctx->iowq_limits_set = true;
+ ctx->int_flags |= IO_RING_F_IOWQ_LIMITS_SET;
if (tctx && tctx->io_wq) {
ret = io_wq_max_workers(tctx->io_wq, new_count);
@@ -725,7 +725,7 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
if (ctx->submitter_task && ctx->submitter_task != current)
return -EEXIST;
- if (ctx->reg_restricted && !(ctx->flags & IORING_SETUP_R_DISABLED)) {
+ if ((ctx->int_flags & IO_RING_F_REG_RESTRICTED) && !(ctx->flags & IORING_SETUP_R_DISABLED)) {
opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
if (!test_bit(opcode, ctx->restrictions.register_op))
return -EACCES;
diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c
index 4fa59bf89bba..52554ed89b11 100644
--- a/io_uring/rsrc.c
+++ b/io_uring/rsrc.c
@@ -295,7 +295,7 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
u64 tag = 0;
uvec = u64_to_user_ptr(user_data);
- iov = iovec_from_user(uvec, 1, 1, &fast_iov, ctx->compat);
+ iov = iovec_from_user(uvec, 1, 1, &fast_iov, io_is_compat(ctx));
if (IS_ERR(iov)) {
err = PTR_ERR(iov);
break;
@@ -319,7 +319,7 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
i = array_index_nospec(up->offset + done, ctx->buf_table.nr);
io_reset_rsrc_node(ctx, &ctx->buf_table, i);
ctx->buf_table.nodes[i] = node;
- if (ctx->compat)
+ if (io_is_compat(ctx))
user_data += sizeof(struct compat_iovec);
else
user_data += sizeof(struct iovec);
@@ -883,12 +883,12 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
if (arg) {
uvec = (struct iovec __user *) arg;
- iov = iovec_from_user(uvec, 1, 1, &fast_iov, ctx->compat);
+ iov = iovec_from_user(uvec, 1, 1, &fast_iov, io_is_compat(ctx));
if (IS_ERR(iov)) {
ret = PTR_ERR(iov);
break;
}
- if (ctx->compat)
+ if (io_is_compat(ctx))
arg += sizeof(struct compat_iovec);
else
arg += sizeof(struct iovec);
diff --git a/io_uring/tctx.c b/io_uring/tctx.c
index 7cbcb82aedfb..143de8e990eb 100644
--- a/io_uring/tctx.c
+++ b/io_uring/tctx.c
@@ -121,7 +121,7 @@ int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
return ret;
tctx = current->io_uring;
- if (ctx->iowq_limits_set) {
+ if (ctx->int_flags & IO_RING_F_IOWQ_LIMITS_SET) {
unsigned int limits[2] = { ctx->iowq_limits[0],
ctx->iowq_limits[1], };
diff --git a/io_uring/timeout.c b/io_uring/timeout.c
index 8eddf8add7a2..579fdddac71a 100644
--- a/io_uring/timeout.c
+++ b/io_uring/timeout.c
@@ -566,8 +566,8 @@ static int __io_timeout_prep(struct io_kiocb *req,
INIT_LIST_HEAD(&timeout->list);
timeout->off = off;
- if (unlikely(off && !req->ctx->off_timeout_used))
- req->ctx->off_timeout_used = true;
+ if (unlikely(off && !(req->ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED)))
+ req->ctx->int_flags |= IO_RING_F_OFF_TIMEOUT_USED;
/*
* for multishot reqs w/ fixed nr of repeats, repeats tracks the
* remaining nr
diff --git a/io_uring/tw.c b/io_uring/tw.c
index 2f2b4ac4b126..022fe8753c19 100644
--- a/io_uring/tw.c
+++ b/io_uring/tw.c
@@ -222,7 +222,7 @@ void io_req_local_work_add(struct io_kiocb *req, unsigned flags)
if (!head) {
io_ctx_mark_taskrun(ctx);
- if (ctx->has_evfd)
+ if (ctx->int_flags & IO_RING_F_HAS_EVFD)
io_eventfd_signal(ctx, false);
}
]
|
| 61/2 |
2026/11/19 05:50 |
llm |
expert |
0m
Model:
Tokens:
input: 66504
output: 29
thoughts: 516
Thoughts: **Analyzing a Concurrency Issue in io_uring**
Okay, so `f1a424e21c15` refactored some bitfields in `struct io_ring_ctx` into a single `unsigned int int_flags`. The motivation, as the commit message states, was to clarify that these flags reside in shared storage and necessitate proper synchronization. It explicitly mentions the need for "sane" checking and setting, which suggests that race conditions were a concern. This refactoring was immediately followed by `8c557449199e`, where `data_race()` was introduced in several functions, namely `io_uring_poll()`, `io_commit_cqring_flush()`, and `io_req_local_work_add()`.
Now, the inclusion of `data_race()` in `io_commit_cqring_flush()` is what piques my interest. The commit message implies this is because `io_commit_cqring_flush()` is invoked in the completion path without holding `uring_lock`. The crux of the issue lies in how `io_commit_cqring_flush()` detects flags like `SHOULD_FLUSH_MASK` and then calls `__io_commit_cqring_flush()` if necessary. Inside `__io_commit_cqring_flush()`, based on the `int_flags`, actions are taken: waking up a poll wait queue (`IO_RING_F_POLL_ACTIVATED`), flushing timeouts (`IO_RING_F_OFF_TIMEOUT_USED`), and signaling an eventfd (`IO_RING_F_HAS_EVFD`).
However, and here's the critical point, it seems the author forgot to apply `data_race()` or at least preserve the values of flags read from `ctx->int_flags` in `io_commit_cqring_flush()`! This means there's a potential race condition. If, between the check of `SHOULD_FLUSH_MASK` and the subsequent actions in `__io_commit_cqring_flush()`, these flags could change.
To confirm and assess the impact, I need to check the history. Let's run a `git log` on `io_uring/io_uring.c` from after `8c557449199e`. This will help determine if any downstream commits have addressed or exacerbated this potential issue.
|
| 62/2 |
2026/11/19 05:50 |
tool |
git-log |
0m
Args: map[Count:5 PathPrefix:io_uring/io_uring.c]
Results: map[Output:cf6542493e35 io_uring: only wait for in-flight requests if the closing task uses the ring
3de16279b43d io_uring: defer foreign io-wq enqueues to task_work
9f5315d3f436 io_uring: free the task context after a grace period
51bab09b7ca1 io_uring: free XArray nodes left by a failed store
0b34e7b20a48 io_uring: charge tctx nodes and personalities
]
|
| 63/2 |
2026/11/19 05:50 |
llm |
expert |
0m
Model:
Tokens:
input: 67708
output: 35
thoughts: 31
Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience:
**Analyzing Commit History for I/O Flushing Logic**
Okay, let's dive in. My immediate goal is to understand the recent changes, if any, around the commit queue ring flushing within the I/O subsystem. Specifically, I'm focusing on two key elements: the `__io_commit_cqring_flush` function and the `SHOULD_FLUSH_MASK` macro. These are the usual suspects when debugging or optimizing I/O commit path performance, or when investigating potential related regressions. I need to quickly identify if any commits have modified the core flushing logic. Knowing this will give me a head start on understanding the current behavior or any recent instability. I'm thinking I'll start by searching the commit history for any mentions of these identifiers.
|
| 64/2 |
2026/11/19 05:50 |
tool |
git-log |
0m
Args: map[CodeRegexp:__io_commit_cqring_flush PathPrefix:io_uring]
Results: map[Output:46929b086886 io_uring: add io_commit_cqring_flush()
a830ffd28780 io_uring: move io_eventfd_signal()
9046c6415be6 io_uring: reshuffle io_uring/io_uring.h
f3b44f92e59a io_uring: move read/write related opcodes to its own file
ed29b0b4fd83 io_uring: move to separate directory
]
|
| 65/2 |
2026/11/19 05:50 |
llm |
expert |
0m
Model:
Tokens:
input: 68089
output: 27
thoughts: 19
Thoughts: Okay, here's my interpretation of what's going on, formatted as you requested:
**Deconstructing Commit 46929b086886**
Alright, let's dive in. First, I need to get my head around commit `46929b086886`. I see this commit hash, and immediately my mind starts building a context. I'm going to assume this is coming from a well-maintained repository, and I need to determine the scope of changes, the intent of the author, and any potential downstream effects. The most efficient way to proceed is to begin with the diff. I'll need to rapidly parse the code changes, looking for patterns. I'm especially interested in things like:
* **New Dependencies:** Are there any new libraries, packages, or frameworks being introduced? I'll need to assess their potential impact on performance, security, and compatibility.
* **API Changes:** Are existing APIs modified or deprecated? This is critical for assessing backward compatibility and potential migration efforts in dependent projects.
* **Data Structures and Algorithms:** Any significant changes in data structures or algorithms? This could have a big impact on resource usage and performance in certain scenarios.
* **Concurrency/Threading:** Any additions or modifications to concurrent code? This requires special attention to ensure thread safety and avoid introducing subtle bugs.
* **Error Handling:** Are there improvements to error handling or logging? This can give me insights into the robustness and maintainability of the code.
* **Testing:** How is this commit tested? Are there new unit tests, integration tests, or end-to-end tests? A strong test suite is essential for maintaining code quality.
Once I've got a grasp of the "what," I need to look for the "why". Examining the commit message will give me the reason behind the change. It is necessary to consider this in light of the larger context, such as bug reports, feature requests, or project milestones. Any associated issue trackers are key. What problem is this solving? What new functionality is being introduced? What were the tradeoffs that were made?
Finally, I'll need to consider this commit in the larger context of the project's evolution. Does it align with the project's architecture and design principles? Does it introduce any technical debt? Does it set the stage for future improvements or refactoring? The goal is to determine if this change represents a net positive for the codebase.
|
| 66/2 |
2026/11/19 05:51 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 46929b086886ade8302cff6e85ccea66bce0cf98
Author: Pavel Begunkov <asml.silence@gmail.com>
Date: Mon Jun 20 01:25:57 2022 +0100
io_uring: add io_commit_cqring_flush()
Since __io_commit_cqring_flush users moved to different files, introduce
io_commit_cqring_flush() helper and encapsulate all flags testing details
inside.
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Link: https://lore.kernel.org/r/0da03887435dd9869ffe46dcd3962bf104afcca3.1655684496.git.asml.silence@gmail.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index 0db73d01455d..3e65e04915a7 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -529,10 +529,7 @@ void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
static inline void io_cqring_ev_posted(struct io_ring_ctx *ctx)
{
- if (unlikely(ctx->off_timeout_used || ctx->drain_active ||
- ctx->has_evfd))
- __io_commit_cqring_flush(ctx);
-
+ io_commit_cqring_flush(ctx);
io_cqring_wake(ctx);
}
diff --git a/io_uring/io_uring.h b/io_uring/io_uring.h
index 738fb96575ab..afca7ff8956c 100644
--- a/io_uring/io_uring.h
+++ b/io_uring/io_uring.h
@@ -229,4 +229,10 @@ static inline void io_req_add_compl_list(struct io_kiocb *req)
wq_list_add_tail(&req->comp_list, &state->compl_reqs);
}
+static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx)
+{
+ if (unlikely(ctx->off_timeout_used || ctx->drain_active || ctx->has_evfd))
+ __io_commit_cqring_flush(ctx);
+}
+
#endif
diff --git a/io_uring/rw.c b/io_uring/rw.c
index f8b42f2265df..0028e95e6633 100644
--- a/io_uring/rw.c
+++ b/io_uring/rw.c
@@ -1016,10 +1016,7 @@ int io_write(struct io_kiocb *req, unsigned int issue_flags)
static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
{
- if (unlikely(ctx->off_timeout_used || ctx->drain_active ||
- ctx->has_evfd))
- __io_commit_cqring_flush(ctx);
-
+ io_commit_cqring_flush(ctx);
if (ctx->flags & IORING_SETUP_SQPOLL)
io_cqring_wake(ctx);
}
]
|
| 67/2 |
2026/11/19 05:51 |
llm |
expert |
0m
Model:
Tokens:
input: 69614
output: 15
thoughts: 2372
Thoughts: **Analysis of io_commit_cqring_flush Race Condition**
Right, let's break this down. We're looking at `io_commit_cqring_flush()` and the race condition introduced when bitfields were converted to `int_flags`. Specifically, the concern is the lockless fast-path check: `if (unlikely(data_race(ctx->int_flags) & SHOULD_FLUSH_MASK))`. The aim here is optimization; avoid the overhead of calling `__io_commit_cqring_flush()` unless necessary.
The key observation is that `io_commit_cqring_flush` reads `ctx->int_flags` without holding `uring_lock`, while the submission path, which *does* hold the lock, might concurrently write to it (e.g., when setting `IO_RING_F_DRAIN_DISABLED`). So, the critical question becomes: is this race benign?
Let's dissect this with the single-writer flag updates in mind, as there are no fused access hazards or torn accesses, since it's an aligned 32-bit access. The writes to `ctx->int_flags` are *always* protected by `uring_lock`. The reads, performed in `__io_commit_cqring_flush()`, are just bitwise checks against the flags (`IO_RING_F_POLL_ACTIVATED`, `IO_RING_F_OFF_TIMEOUT_USED`, `IO_RING_F_HAS_EVFD`).
Each of these flags' implications needs scrutiny:
* `IO_RING_F_POLL_ACTIVATED`: Activation under `uring_lock`, followed by a memory barrier (`smp_mb()`), and waking up waiters. If the read sees 0, it doesn't wake unnecessarily, if 1, the safe wake call proceeds.
* `IO_RING_F_OFF_TIMEOUT_USED`: Set under `uring_lock`. `io_flush_timeouts` uses `timeout_lock`. Sees 0, no timeouts flushed by this invocation, but no count based timeout is complete yet. Sees 1, safe list check under `timeout_lock`.
* `IO_RING_F_HAS_EVFD`: Set under `uring_lock`. `io_eventfd_signal` uses RCU.
The reads only *decide* whether to call `io_poll_wq_wake()`, `io_flush_timeouts()`, or `io_eventfd_signal()`. These callees all have their own synchronization mechanisms, so there's no control flow hazard and no memory corruption. Even if we read a stale value, the write being observed (like setting `IO_RING_F_DRAIN_DISABLED`) doesn't affect the bits being checked. Observing either the old or the new value is harmless.
Now, a point for optimization: `data_race()` could be used in `__io_commit_cqring_flush()`, or passing an already read flags value to it, to silence KCSAN and prevent redundant reads.
So, is there a scenario where this race could cause problems? No. Writes are locked, there's no tearing, no compiler hazards. The called functions themselves are synchronized. And looking at the relevant commits from Jens Axboe, the intention was explicitly to allow these potentially racy but harmless flag checks.
This is a textbook "BENIGN" single-writer flag update, and is completely safe under the design constraints of the code. We can call the set-results tool.
|
| 68/2 |
2026/11/19 05:51 |
tool |
set-results |
0m
Args:
Results: map[Benign:true FailureDetectableBy:]
|
| 69/2 |
2026/11/19 05:51 |
llm |
expert |
0m
Model:
Tokens:
input: 72783
output: 1328
thoughts: 45
Thoughts: Okay, here's my interpretation of the situation, tailored for a subject matter expert like myself:
**Finalizing the Response: Protocol Implemented**
Right, `set-results` has been called. The groundwork is laid. We're at the point where we can construct the final, polished response. We're adhering to the established protocol, which means a specific and structured output. We'll start with the Race Summary, a concise overview of the event. Then we'll delve into the Synchronization context, highlighting any critical timings or dependencies. The Final Classification, of course, will provide the definitive ranking. The Reasoning section is where we'll justify the results, backed by solid evidence and analysis. Finally, we'll offer Recommended Annotations; perhaps clarifications, potential areas for further investigation, or contextual notes that will be valuable for the reader. The overall goal is to deliver a complete, robust, and well-reasoned conclusion. Let's get to it.
|
| 70/1 |
2026/11/19 05:51 |
action |
format-explanation |
0m
Results: map[Explanation:### Race Summary
A data race occurs between `io_init_req()` (writing to `ctx->int_flags`) and
`__io_commit_cqring_flush()` (reading from `ctx->int_flags`).
- **Writer (Task 3754, SQPOLL thread):** Executes `io_submit_sqes() ->
io_init_req()`, which sets `ctx->int_flags |= IO_RING_F_DRAIN_DISABLED` (bit 10)
under `ctx->uring_lock`.
- **Reader (Task 3758, io-wq worker thread):** Executes `io_issue_sqe() ->
io_req_complete_post() -> io_cq_unlock_post() -> io_commit_cqring_flush() ->
__io_commit_cqring_flush()`, reading `ctx->int_flags` to test
`IO_RING_F_OFF_TIMEOUT_USED` (bit 3) as well as `IO_RING_F_POLL_ACTIVATED` (bit
9) and `IO_RING_F_HAS_EVFD` (bit 5).
---
### Synchronization Context
- **Writer:** Holds `mutex_lock(&ctx->uring_lock)`. In `io_uring`, all writes to
`ctx->int_flags` are strictly serialized by `ctx->uring_lock`. Consequently,
there is only ever a single concurrent writer modifying `ctx->int_flags`,
precluding any concurrent write clobbering.
- **Reader:** In `io_cq_unlock_post()`, completions are posted and
`ctx->completion_lock` is unlocked before calling `io_commit_cqring_flush()`.
Neither `ctx->completion_lock` nor `ctx->uring_lock` is held. The fast-path
check `io_commit_cqring_flush()` already uses `data_race(ctx->int_flags) &
SHOULD_FLUSH_MASK` (added in commit `8c557449199e`), but the invoked out-of-line
helper `__io_commit_cqring_flush()` reads `ctx->int_flags` using plain
unannotated reads.
---
### Final Classification: BENIGN
---
### Reasoning
1. **Single-Writer Word-Sized Scalar Access:** `ctx->int_flags` is an aligned
32-bit `unsigned int`. Writes are serialized by `ctx->uring_lock`. Loads and
stores of aligned 32-bit values are single-copy atomic and will not tear on
supported architectures.
2. **Independent Bitfield Bits:** The writer is updating
`IO_RING_F_DRAIN_DISABLED` (bit 10). The reader in `__io_commit_cqring_flush()`
tests `IO_RING_F_POLL_ACTIVATED` (bit 9), `IO_RING_F_OFF_TIMEOUT_USED` (bit 3),
and `IO_RING_F_HAS_EVFD` (bit 5). The value of bit 10 is completely independent
and does not affect the flags tested by the reader.
3. **Safe Downstream Synchronization:** Even when the bits tested by
`__io_commit_cqring_flush()` change:
- `IO_RING_F_POLL_ACTIVATED`: Wakes `ctx->poll_wq`, which internally
serializes on the waitqueue spinlock.
- `IO_RING_F_OFF_TIMEOUT_USED`: Calls `io_flush_timeouts()`, which takes
`raw_spin_lock_irq(&ctx->timeout_lock)`.
- `IO_RING_F_HAS_EVFD`: Calls `io_eventfd_signal()`, which safely validates
`ctx->io_ev_fd` under `rcu_read_lock()`.
4. **No Loop or Hoisting Hazard:** The read is not inside a polling or spin
loop; it is a straight-line sequence of checks after CQ ring events are
committed. Stale reads simply defer flushing to the next completion or task-work
cycle.
Commit `8c557449199e` ("io_uring: mark known and harmless racy ctx->int_flags
uses") intentionally annotated `ctx->int_flags` reads in
`io_commit_cqring_flush()`, `io_uring_poll()`, and `io_req_local_work_add()`,
but omitted annotating the re-reads inside `__io_commit_cqring_flush()`.
---
### Recommended Annotations
Pass the flags read in `io_commit_cqring_flush()` into
`__io_commit_cqring_flush()`, or wrap the reads in `__io_commit_cqring_flush()`
with `data_race()`:
```diff
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -530,11 +530,13 @@ static inline bool io_drain_req_completed(struct
io_ring_ctx *ctx)
void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
{
- if (ctx->int_flags & IO_RING_F_POLL_ACTIVATED)
+ unsigned int flags = data_race(ctx->int_flags);
+
+ if (flags & IO_RING_F_POLL_ACTIVATED)
io_poll_wq_wake(ctx);
- if (ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED)
+ if (flags & IO_RING_F_OFF_TIMEOUT_USED)
io_flush_timeouts(ctx);
- if (ctx->int_flags & IO_RING_F_HAS_EVFD)
+ if (flags & IO_RING_F_HAS_EVFD)
io_eventfd_signal(ctx, true, false);
}
```]
|