Link a NOP to a FUTEX_WAITV, then cancel the wait and wake its futex in the same submit. The wait and the NOP must agree: both succeed, or both are cancelled. With IOSQE_CQE_SKIP_SUCCESS, a successful wait posts no CQE, and a cancelled one posts its CQE but none for the NOP. This fails on kernels that have neither "io_uring/futex: fail soft links when cancelling a wait" nor "io_uring/futex: ensure partial wakes are appropriately dequeued", where the cancelled wait doesn't cancel the NOP, and on kernels that have both but not "io_uring/futex: don't fail a cancelled futexv wait that got woken", where the woken wait still cancels it. Written and tested by Claude Code, I handled the review process. Signed-off-by: Hang Zhengyang --- test/futex.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/test/futex.c b/test/futex.c index 4dc5a305..356fde80 100644 --- a/test/futex.c +++ b/test/futex.c @@ -392,6 +392,117 @@ static int test_wake_zero(void) return 0; } +/* + * A FUTEX_WAITV linked to a NOP, then a cancel of the wait and a wake of + * its futex in the same submit. The cancel claims the wait first; on some + * kernels the wake still wakes the futex and the wait completes with its + * index, on others the wait stays cancelled. Either way, the wait and the + * linked NOP must agree: both succeed, or both are cancelled. With + * IOSQE_CQE_SKIP_SUCCESS, a successful wait posts no CQE, and a cancelled + * one posts its CQE but none for the NOP. + */ +static int test_cancel_wake_link(int skip) +{ + struct io_uring_sqe *sqe; + struct io_uring_cqe *cqe; + struct io_uring ring; + struct futex_waitv fw = { }; + unsigned int *futex; + struct __kernel_timespec ts = { .tv_sec = 1, }; + int ret, wait_res = 0, nop_res = 0, cancel_res = 0, wake_res = 0; + int wait_seen, nop_seen; + unsigned int seen = 0; + + ret = io_uring_queue_init(8, &ring, 0); + if (ret) + return ret; + + futex = malloc(sizeof(*futex)); + *futex = 0; + fw.uaddr = (unsigned long) futex; + fw.flags = FUTEX2_SIZE_U32; + + sqe = io_uring_get_sqe(&ring); + io_uring_prep_futex_waitv(sqe, &fw, 1, 0); + sqe->flags |= IOSQE_IO_LINK; + if (skip) + sqe->flags |= IOSQE_CQE_SKIP_SUCCESS; + sqe->user_data = 1; + sqe = io_uring_get_sqe(&ring); + io_uring_prep_nop(sqe); + sqe->user_data = 2; + io_uring_submit(&ring); + + sqe = io_uring_get_sqe(&ring); + io_uring_prep_cancel64(sqe, 1, 0); + sqe->user_data = 3; + sqe = io_uring_get_sqe(&ring); + io_uring_prep_futex_wake(sqe, futex, 1, FUTEX_BITSET_MATCH_ANY, + FUTEX2_SIZE_U32, 0); + sqe->user_data = 4; + io_uring_submit(&ring); + + /* + * The NOP completes last. If its link was cut under skip, it posts no + * CQE at all, so don't wait for it forever. + */ + while ((seen & 0x1c) != 0x1c) { + ret = io_uring_wait_cqe_timeout(&ring, &cqe, &ts); + if (ret == -ETIME) + break; + if (ret) { + fprintf(stderr, "wait: %d\n", ret); + return 1; + } + if (cqe->user_data == 1) + wait_res = cqe->res; + else if (cqe->user_data == 2) + nop_res = cqe->res; + else if (cqe->user_data == 3) + cancel_res = cqe->res; + else if (cqe->user_data == 4) + wake_res = cqe->res; + seen |= 1U << cqe->user_data; + io_uring_cqe_seen(&ring, cqe); + } + + /* make sure the cancel found the wait and the wake reached its futex */ + if (cancel_res < 0 || wake_res != 1) { + fprintf(stderr, "cancel %d, wake %d\n", cancel_res, wake_res); + return 1; + } + wait_seen = seen & 0x2; + nop_seen = seen & 0x4; + if (skip && wait_seen && wait_res >= 0) { + fprintf(stderr, "skipped wait posted %d\n", wait_res); + return 1; + } + if (!skip && !wait_seen) { + fprintf(stderr, "no CQE for the wait\n"); + return 1; + } + /* under skip, a wait without a CQE succeeded */ + if (!wait_seen || wait_res >= 0) { + if (!nop_seen || nop_res) { + fprintf(stderr, "wait succeeded, but linked NOP %s %d\n", + nop_seen ? "posted" : "missing", nop_res); + return 1; + } + } else { + /* a failed link posts no CQEs under skip */ + if (skip ? nop_seen : (!nop_seen || nop_res != -ECANCELED)) { + fprintf(stderr, "wait %d, but linked NOP %s %d\n", + wait_res, nop_seen ? "posted" : "missing", + nop_res); + return 1; + } + } + + io_uring_queue_exit(&ring); + free(futex); + return 0; +} + /* * Test invalid wait/wake/waitv flags */ @@ -579,5 +690,17 @@ int main(int argc, char *argv[]) return T_EXIT_FAIL; } + ret = test_cancel_wake_link(0); + if (ret) { + fprintf(stderr, "cancel_wake_link 0 failed\n"); + return T_EXIT_FAIL; + } + + ret = test_cancel_wake_link(1); + if (ret) { + fprintf(stderr, "cancel_wake_link 1 failed\n"); + return T_EXIT_FAIL; + } + return T_EXIT_PASS; } base-commit: ed312b3c94f16aea679f689d4950284a954d3b5c -- 2.53.0