axboe / liburing

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

[GIT PULL] Fix memory ordering/atomic access #1085

Closed shachaf closed 4 months ago

shachaf commented 4 months ago

A few memory accesses don't seem correct to me, and a few seem slightly stricter than necessary.

Here are the rules in userspace, I think:

sq.ktail is read by the kernel sq.khead is written by the kernel

cq.ktail is written by the kernel cq.khead is read by the kernel

Locations that are written concurrently by the kernel must be loaded atomically; if they specify (to the user) that some memory is now available, they must be loaded with acquire semantics before the memory is read.

Similarly, locations that are read concurrently by the kernel must be written atomically; if they specify (to the kernel) that some memory is now available, they must be stored with release semantics after the memory is written to.

Note that without SQPOLL, sq is only accessed by the kernel in response to a system call, so no synchronization is necessary.

Note that in practice, on most CPU architectures, a regular aligned load/store is already atomic; in that situation READ_ONCE/WRITE_ONCE is primarily necessary for the compiler, which would otherwise be allowed to do invalid things like load the lower half of an integer and then the upper half.


git request-pull output:

The following changes since commit 2b353a85abed9d19201bd947f3efca6a630e476a:

  test/version: fix broken test (2024-02-27 11:27:36 -0700)

are available in the Git repository at:

  https://github.com/shachaf/liburing memory-ordering-and-atomicity

for you to fetch changes up to f03becf5d7c1bc3fd9a426ba8b013346847f8b9e:

  Fix memory ordering/atomic access (2024-03-02 16:39:13 -0800)

----------------------------------------------------------------
Shachaf Ben-Kiki (1):
      Fix memory ordering/atomic access

 src/include/liburing.h |  6 ++++--
 src/queue.c            | 20 ++++++++------------
 test/helpers.c         | 20 ++++++++------------
 3 files changed, 20 insertions(+), 26 deletions(-)

Click to show/hide pull request guidelines ## Pull Request Guidelines 1. To make everyone easily filter pull request from the email notification, use `[GIT PULL]` as a prefix in your PR title. ``` [GIT PULL] Your Pull Request Title ``` 2. Follow the commit message format rules below. 3. Follow the Linux kernel coding style (see: https://github.com/torvalds/linux/blob/master/Documentation/process/coding-style.rst). ### Commit message format rules: 1. The first line is title (don't be more than 72 chars if possible). 2. Then an empty line. 3. Then a description (may be omitted for truly trivial changes). 4. Then an empty line again (if it has a description). 5. Then a `Signed-off-by` tag with your real name and email. For example: ``` Signed-off-by: Foo Bar ``` The description should be word-wrapped at 72 chars. Some things should not be word-wrapped. They may be some kind of quoted text - long compiler error messages, oops reports, Link, etc. (things that have a certain specific format). Note that all of this goes in the commit message, not in the pull request text. The pull request text should introduce what this pull request does, and each commit message should explain the rationale for why that particular change was made. The git tree is canonical source of truth, not github. Each patch should do one thing, and one thing only. If you find yourself writing an explanation for why a patch is fixing multiple issues, that's a good indication that the change should be split into separate patches. If the commit is a fix for an issue, add a `Fixes` tag with the issue URL. Don't use GitHub anonymous email like this as the commit author: ``` 123456789+username@users.noreply.github.com ``` Use a real email address! ### Commit message example: ``` src/queue: don't flush SQ ring for new wait interface If we have IORING_FEAT_EXT_ARG, then timeouts are done through the syscall instead of by posting an internal timeout. This was done to be both more efficient, but also to enable multi-threaded use the wait side. If we touch the SQ state by flushing it, that isn't safe without synchronization. Fixes: https://github.com/axboe/liburing/issues/402 Signed-off-by: Jens Axboe ```

By submitting this pull request, I acknowledge that:

  1. I have followed the above pull request guidelines.
  2. I have the rights to submit this work under the same license.
  3. I agree to a Developer Certificate of Origin (see https://developercertificate.org for more information).