Technical Breakdown: KCSAN Data Race on ctx->int_flags
1. Racing Field and Affected Data Structures
2. Writer Context and Execution Path
- Call Trace:
__io_sq_thread() [io_uring/sqpoll.c:252]
io_submit_sqes() [io_uring/io_uring.c:2212]
io_submit_sqe() [io_uring/io_uring.c:2044]
io_init_req() [io_uring/io_uring.c:1939]
- Mechanism: During SQE processing in
io_init_req(), if the request contains IOSQE_CQE_SKIP_SUCCESS, the kernel sets the internal flag:
if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS)
ctx->int_flags |= IO_RING_F_DRAIN_DISABLED;
This is executed under ctx->uring_lock (held by the SQPOLL thread or the task calling io_uring_enter()). This operation is a plain C non-atomic read-modify-write on ctx->int_flags.
3. Reader Context and Execution Path
- Call Trace:
io_wq_worker() [io_uring/io-wq.c:768]
io_worker_handle_work() [io_uring/io-wq.c:677]
io_wq_submit_work() [io_uring/io_uring.c:1677]
io_issue_sqe() [io_uring/io_uring.c:1546]
io_req_complete_post() [io_uring/io_uring.c:1030]
io_cq_unlock_post() [io_uring/io_uring.c:572]
io_commit_cqring_flush() [io_uring/io_uring.h:518]
__io_commit_cqring_flush() [io_uring/io_uring.c:535]
- Mechanism: When an asynchronous work item completes on an
io-wq worker thread, io_req_complete_post() calls io_cq_unlock_post(). After unlocking ctx->completion_lock, io_commit_cqring_flush() checks whether any deferred flush actions are required:
#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(data_race(ctx->int_flags) & SHOULD_FLUSH_MASK))
__io_commit_cqring_flush(ctx);
}
On rings without IO_RING_F_TASK_COMPLETE (such as IORING_SETUP_SQPOLL without IOPOLL), io_uring_create() initializes IO_RING_F_POLL_ACTIVATED into ctx->int_flags (io_uring/io_uring.c:3266-3267). Therefore, SHOULD_FLUSH_MASK matches, and __io_commit_cqring_flush(ctx) is called.
Inside __io_commit_cqring_flush():
void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
{
if (ctx->int_flags & IO_RING_F_POLL_ACTIVATED)
io_poll_wq_wake(ctx);
if (ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED)
io_flush_timeouts(ctx);
if (ctx->int_flags & IO_RING_F_HAS_EVFD)
io_eventfd_signal(ctx, true, false);
}
Unlike the caller io_commit_cqring_flush(), which wrapped its access in data_race(), __io_commit_cqring_flush() performs three unannotated, plain C reads from ctx->int_flags without holding ctx->uring_lock.
4. Root Cause
ctx->int_flags combines submission-state flags (IO_RING_F_DRAIN_DISABLED, IO_RING_F_DRAIN_ACTIVE, IO_RING_F_DRAIN_NEXT), ring-creation flags (IO_RING_F_TASK_COMPLETE, IO_RING_F_LOCKLESS_CQ, etc.), and completion flush/wake flags (IO_RING_F_POLL_ACTIVATED, IO_RING_F_OFF_TIMEOUT_USED, IO_RING_F_HAS_EVFD) into a single non-atomic unsigned int variable.
While writers modifying submission flags hold ctx->uring_lock, asynchronous completions from io-wq run concurrently without ctx->uring_lock. io_commit_cqring_flush() recognized the lockless read and used data_race(), but __io_commit_cqring_flush() subsequently re-read ctx->int_flags with plain C loads, racing against ctx->int_flags |= IO_RING_F_DRAIN_DISABLED in io_init_req().