Skip to content

Commit 20c0b38

Browse files
anadavaxboe
authored andcommitted
io_uring: Use WRITE_ONCE() when writing to sq_flags
The compiler should be forbidden from any strange optimization for async writes to user visible data-structures. Without proper protection, the compiler can cause write-tearing or invent writes that would confuse the userspace. However, there are writes to sq_flags which are not protected by WRITE_ONCE(). Use WRITE_ONCE() for these writes. This is purely a theoretical issue. Presumably, any compiler is very unlikely to do such optimizations. Fixes: 75b28af ("io_uring: allocate the two rings together") Cc: Jens Axboe <[email protected]> Cc: Pavel Begunkov <[email protected]> Signed-off-by: Nadav Amit <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent ef98eb0 commit 20c0b38

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

fs/io_uring.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,8 @@ static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
15001500
all_flushed = list_empty(&ctx->cq_overflow_list);
15011501
if (all_flushed) {
15021502
clear_bit(0, &ctx->check_cq_overflow);
1503-
ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1503+
WRITE_ONCE(ctx->rings->sq_flags,
1504+
ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
15041505
}
15051506

15061507
if (posted)
@@ -1579,7 +1580,9 @@ static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
15791580
}
15801581
if (list_empty(&ctx->cq_overflow_list)) {
15811582
set_bit(0, &ctx->check_cq_overflow);
1582-
ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
1583+
WRITE_ONCE(ctx->rings->sq_flags,
1584+
ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1585+
15831586
}
15841587
ocqe->cqe.user_data = user_data;
15851588
ocqe->cqe.res = res;
@@ -6804,14 +6807,16 @@ static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
68046807
{
68056808
/* Tell userspace we may need a wakeup call */
68066809
spin_lock_irq(&ctx->completion_lock);
6807-
ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6810+
WRITE_ONCE(ctx->rings->sq_flags,
6811+
ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
68086812
spin_unlock_irq(&ctx->completion_lock);
68096813
}
68106814

68116815
static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
68126816
{
68136817
spin_lock_irq(&ctx->completion_lock);
6814-
ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6818+
WRITE_ONCE(ctx->rings->sq_flags,
6819+
ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
68156820
spin_unlock_irq(&ctx->completion_lock);
68166821
}
68176822

0 commit comments

Comments
 (0)