Skip to content

Commit 20d6b63

Browse files
isilenceaxboe
authored andcommitted
io_uring: refactor __io_get_cqe()
Make __io_get_cqe simpler by not grabbing the cqe from refilled cached, but letting io_get_cqe() do it for us. That's cleaner and removes some duplication. Signed-off-by: Pavel Begunkov <[email protected]> Link: https://lore.kernel.org/r/74dc8fdf2657e438b2e05e1d478a3596924604e9.1692916914.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <[email protected]>
1 parent b24c5d7 commit 20d6b63

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

io_uring/io_uring.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ void io_req_cqe_overflow(struct io_kiocb *req)
818818
* control dependency is enough as we're using WRITE_ONCE to
819819
* fill the cq entry
820820
*/
821-
struct io_uring_cqe *__io_get_cqe(struct io_ring_ctx *ctx, bool overflow)
821+
bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow)
822822
{
823823
struct io_rings *rings = ctx->rings;
824824
unsigned int off = ctx->cached_cq_tail & (ctx->cq_entries - 1);
@@ -830,15 +830,15 @@ struct io_uring_cqe *__io_get_cqe(struct io_ring_ctx *ctx, bool overflow)
830830
* Force overflow the completion.
831831
*/
832832
if (!overflow && (ctx->check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT)))
833-
return NULL;
833+
return false;
834834

835835
/* userspace may cheat modifying the tail, be safe and do min */
836836
queued = min(__io_cqring_events(ctx), ctx->cq_entries);
837837
free = ctx->cq_entries - queued;
838838
/* we need a contiguous range, limit based on the current array offset */
839839
len = min(free, ctx->cq_entries - off);
840840
if (!len)
841-
return NULL;
841+
return false;
842842

843843
if (ctx->flags & IORING_SETUP_CQE32) {
844844
off <<= 1;
@@ -847,12 +847,7 @@ struct io_uring_cqe *__io_get_cqe(struct io_ring_ctx *ctx, bool overflow)
847847

848848
ctx->cqe_cached = &rings->cqes[off];
849849
ctx->cqe_sentinel = ctx->cqe_cached + len;
850-
851-
ctx->cached_cq_tail++;
852-
ctx->cqe_cached++;
853-
if (ctx->flags & IORING_SETUP_CQE32)
854-
ctx->cqe_cached++;
855-
return &rings->cqes[off];
850+
return true;
856851
}
857852

858853
static bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, s32 res,

io_uring/io_uring.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ enum {
3838
IOU_STOP_MULTISHOT = -ECANCELED,
3939
};
4040

41-
struct io_uring_cqe *__io_get_cqe(struct io_ring_ctx *ctx, bool overflow);
41+
bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow);
4242
void io_req_cqe_overflow(struct io_kiocb *req);
4343
int io_run_task_work_sig(struct io_ring_ctx *ctx);
4444
void io_req_defer_failed(struct io_kiocb *req, s32 res);
@@ -112,19 +112,20 @@ static inline void io_req_task_work_add(struct io_kiocb *req)
112112
static inline struct io_uring_cqe *io_get_cqe_overflow(struct io_ring_ctx *ctx,
113113
bool overflow)
114114
{
115-
io_lockdep_assert_cq_locked(ctx);
115+
struct io_uring_cqe *cqe;
116116

117-
if (likely(ctx->cqe_cached < ctx->cqe_sentinel)) {
118-
struct io_uring_cqe *cqe = ctx->cqe_cached;
117+
io_lockdep_assert_cq_locked(ctx);
119118

120-
ctx->cached_cq_tail++;
121-
ctx->cqe_cached++;
122-
if (ctx->flags & IORING_SETUP_CQE32)
123-
ctx->cqe_cached++;
124-
return cqe;
119+
if (unlikely(ctx->cqe_cached >= ctx->cqe_sentinel)) {
120+
if (unlikely(!io_cqe_cache_refill(ctx, overflow)))
121+
return NULL;
125122
}
126-
127-
return __io_get_cqe(ctx, overflow);
123+
cqe = ctx->cqe_cached;
124+
ctx->cached_cq_tail++;
125+
ctx->cqe_cached++;
126+
if (ctx->flags & IORING_SETUP_CQE32)
127+
ctx->cqe_cached++;
128+
return cqe;
128129
}
129130

130131
static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)

0 commit comments

Comments
 (0)