axboe / liburing

Library providing helpers for the Linux kernel io_uring support
MIT License
2.72k stars 393 forks source link

Question: In `SQPOLL` mode, how does `io_uring` avoid data race in SQ between the user thread and the kernel polling thread? #1061

Closed ZhenbangYou closed 5 months ago

ZhenbangYou commented 5 months ago

This question is all about SQ polling. Say, I want to submit a read request, so I do the following three steps:

However, since the kernel is polling, is it possible for the following two scenarios to happen:

I tried very hard to find an answer to this, but I couldn't make it. Thanks so much for answering my question!

mjasny commented 5 months ago

No, because the SQE is only visible to the kernel thread after you do io_uring_submit(ring);. This would be step 4.

Internally liburing does increment the tail of the ring which is polled by the kernel in submit using __io_uring_flush_sq()

ZhenbangYou commented 5 months ago

I see! No wonder without io_uring_submit my SQEs weren't executed in SQPOLL mode. Thank you so much!