Skip to contentSkip to conversation

io_uring/poll: fix signed comparison in io_poll_get_ownership()

PATCH Applied Ren Wei 4 participants 6 messages Apr 12 – Apr 16 1 message not in the archive io_uring

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Ren Wei

author
#1 lore raw

PATCH 1/1 io_uring/poll: fix signed comparison in io_poll_get_ownership()

From: Longxuan Yu <[email protected]>

io_poll_get_ownership() uses a signed comparison to check whether poll_refs has reached the threshold for the slowpath:

    if (unlikely(atomic_read(&req->poll_refs) >= IO_POLL_REF_BIAS))

atomic_read() returns int (signed). When IO_POLL_CANCEL_FLAG (BIT(31)) is set in poll_refs, the value becomes negative in signed arithmetic, so the >= 128 comparison always evaluates to false and the slowpath is never taken.

Fix this by casting the atomic_read() result to unsigned int before the comparison, so that the cancel flag is treated as a large positive value and correctly triggers the slowpath.

---
 io_uring/poll.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
io_uring/poll.c +1 -1
index 2e9ee47d7..b98439107 100644
@@ -93,7 +93,7 @@ static bool io_poll_get_ownership_slowpath(struct io_kiocb *req)
  */
 static inline bool io_poll_get_ownership(struct io_kiocb *req)
 {
-	if (unlikely(atomic_read(&req->poll_refs) >= IO_POLL_REF_BIAS))
+	if (unlikely((unsigned int)atomic_read(&req->poll_refs) >= IO_POLL_REF_BIAS))
 		return io_poll_get_ownership_slowpath(req);
 	return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK);
 }
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#2 lore raw
Quoted 7 lines · Ren Wei

From: Longxuan Yu <[email protected]>

io_poll_get_ownership() uses a signed comparison to check whether poll_refs has reached the threshold for the slowpath:

     if (unlikely(atomic_read(&req->poll_refs) >= IO_POLL_REF_BIAS))
Quoted 8 lines

atomic_read() returns int (signed). When IO_POLL_CANCEL_FLAG (BIT(31)) is set in poll_refs, the value becomes negative in signed arithmetic, so the >= 128 comparison always evaluates to false and the slowpath is never taken.

Fix this by casting the atomic_read() result to unsigned int before the comparison, so that the cancel flag is treated as a large positive value and correctly triggers the slowpath.

Looks good, thanks for the patch

Signature · 1 line
Pavel Begunkov
maintainer Applied
#3 lore raw
In reply to Ren Wei
Quoted 11 lines · Ren Wei

io_poll_get_ownership() uses a signed comparison to check whether poll_refs has reached the threshold for the slowpath:

    if (unlikely(atomic_read(&req->poll_refs) >= IO_POLL_REF_BIAS))

atomic_read() returns int (signed). When IO_POLL_CANCEL_FLAG (BIT(31)) is set in poll_refs, the value becomes negative in signed arithmetic, so the >= 128 comparison always evaluates to false and the slowpath is never taken.

[...]

Applied, thanks!

[1/1] io_uring/poll: fix signed comparison in io_poll_get_ownership()

      commit: 326941b22806cbf2df1fbfe902b7908b368cce42

Best regards,

Signature · 1 line
Jens Axboe
maintainer
#4 lore raw
In reply to Ren Wei
Quoted 12 lines · Ren Wei

From: Longxuan Yu <[email protected]>

io_poll_get_ownership() uses a signed comparison to check whether poll_refs has reached the threshold for the slowpath:

    if (unlikely(atomic_read(&req->poll_refs) >= IO_POLL_REF_BIAS))

atomic_read() returns int (signed). When IO_POLL_CANCEL_FLAG (BIT(31)) is set in poll_refs, the value becomes negative in signed arithmetic, so the >= 128 comparison always evaluates to false and the slowpath is never taken.

Quoted 5 lines

Fix this by casting the atomic_read() result to unsigned int before the comparison, so that the cancel flag is treated as a large positive value and correctly triggers the slowpath.

Fixes aa43477b0402 ("io_uring: poll rework")

Is this correct? Seems it should be:

Fixes a26a35e9019f ("io_uring: make poll refs more robust")
Signature · 1 line
Jens Axboe
#5 lore raw
Quoted 13 lines · Jens Axboe
Quoted 12 lines · Ren Wei

From: Longxuan Yu <[email protected]>

io_poll_get_ownership() uses a signed comparison to check whether poll_refs has reached the threshold for the slowpath:

    if (unlikely(atomic_read(&req->poll_refs) >= IO_POLL_REF_BIAS))

atomic_read() returns int (signed). When IO_POLL_CANCEL_FLAG (BIT(31)) is set in poll_refs, the value becomes negative in signed arithmetic, so the >= 128 comparison always evaluates to false and the slowpath is never taken.

Quoted 9 lines
Quoted 5 lines

Fix this by casting the atomic_read() result to unsigned int before the comparison, so that the cancel flag is treated as a large positive value and correctly triggers the slowpath.

Fixes aa43477b0402 ("io_uring: poll rework")

Is this correct? Seems it should be:

Fixes a26a35e9019f ("io_uring: make poll refs more robust")

I just double check it. Yes we were wrong. Correct bug inducing commit is

a26a35e9019f ("io_uring: make poll refs more robust").

Thanks for pointing it out.

Do we need to send a v2 to fix this?

maintainer
#6 lore raw
Quoted 24 lines · Yuan Tan
Quoted 13 lines · Jens Axboe
Quoted 12 lines · Ren Wei

From: Longxuan Yu <[email protected]>

io_poll_get_ownership() uses a signed comparison to check whether poll_refs has reached the threshold for the slowpath:

    if (unlikely(atomic_read(&req->poll_refs) >= IO_POLL_REF_BIAS))

atomic_read() returns int (signed). When IO_POLL_CANCEL_FLAG (BIT(31)) is set in poll_refs, the value becomes negative in signed arithmetic, so the >= 128 comparison always evaluates to false and the slowpath is never taken.

Quoted 9 lines
Quoted 5 lines

Fix this by casting the atomic_read() result to unsigned int before the comparison, so that the cancel flag is treated as a large positive value and correctly triggers the slowpath.

Fixes aa43477b0402 ("io_uring: poll rework")

Is this correct? Seems it should be:

Fixes a26a35e9019f ("io_uring: make poll refs more robust")
Quoted 8 lines

I just double check it. Yes we were wrong. Correct bug inducing commit is

a26a35e9019f ("io_uring: make poll refs more robust").

Thanks for pointing it out.

Do we need to send a v2 to fix this?

No need, I corrected it already while applying yesterday.

Signature · 1 line
Jens Axboe