Skip to content

Commit 2f38934

Browse files
isilenceaxboe
authored andcommitted
io_uring: cmpxchg for poll arm refs release
Replace atomically substracting the ownership reference at the end of arming a poll with a cmpxchg. We try to release ownership by setting 0 assuming that poll_refs didn't change while we were arming. If it did change, we keep the ownership and use it to queue a tw, which is fully capable to process all events and (even tolerates spurious wake ups). It's a bit more elegant as we reduce races b/w setting the cancellation flag and getting refs with this release, and with that we don't have to worry about any kinds of underflows. It's not the fastest path for polling. The performance difference b/w cmpxchg and atomic dec is usually negligible and it's not the fastest path. Cc: [email protected] Fixes: aa43477 ("io_uring: poll rework") Signed-off-by: Pavel Begunkov <[email protected]> Link: https://lore.kernel.org/r/0c95251624397ea6def568ff040cad2d7926fd51.1668963050.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <[email protected]>
1 parent 7fdbc5f commit 2f38934

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

io_uring/poll.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,6 @@ static int __io_arm_poll_handler(struct io_kiocb *req,
518518
unsigned issue_flags)
519519
{
520520
struct io_ring_ctx *ctx = req->ctx;
521-
int v;
522521

523522
INIT_HLIST_NODE(&req->hash_node);
524523
req->work.cancel_seq = atomic_read(&ctx->cancel_seq);
@@ -586,11 +585,10 @@ static int __io_arm_poll_handler(struct io_kiocb *req,
586585

587586
if (ipt->owning) {
588587
/*
589-
* Release ownership. If someone tried to queue a tw while it was
590-
* locked, kick it off for them.
588+
* Try to release ownership. If we see a change of state, e.g.
589+
* poll was waken up, queue up a tw, it'll deal with it.
591590
*/
592-
v = atomic_dec_return(&req->poll_refs);
593-
if (unlikely(v & IO_POLL_REF_MASK))
591+
if (atomic_cmpxchg(&req->poll_refs, 1, 0) != 1)
594592
__io_poll_execute(req, 0);
595593
}
596594
return 0;

0 commit comments

Comments
 (0)