Skip to content

atomics: allow atomic and non-atomic reads to race - #128778

Merged
bors merged 4 commits into
rust-lang:masterfrom
RalfJung:atomic-read-read-races
Sep 28, 2024
Merged

atomics: allow atomic and non-atomic reads to race#128778
bors merged 4 commits into
rust-lang:masterfrom
RalfJung:atomic-read-read-races

Conversation

@RalfJung

@RalfJung RalfJung commented Aug 7, 2024

Copy link
Copy Markdown
Member

We currently define our atomics in terms of C++ atomic_ref. That has the unfortunate side-effect of making it UB for an atomic and a non-atomic read to race (concretely, this code has UB). There's really no good reason for this, all the academic models of the C++ memory model I am aware of allow this -- C++ just disallows this because of their insistence on an "object model" with typed memory, where atomic_ref temporarily creates an "atomic object" that may not be accesses via regular non-atomic operations.

So instead of tying our operations to atomic_ref, let us tie them directly to the underlying C++ memory model. I am not sure what is the best way to phrase this, so here's a first attempt.

We also carve out an exception from the "no mixed-size atomic accesses" rule to permit mixed-size atomic reads -- given that we permit mixed-size non-atomic reads, it seems odd that this would be disallowed for atomic reads. However, when an atomic write races with any other atomic operation, they must use the same size.

With this change, it is finally the case that every non-atomic access can be replaced by an atomic access without introducing UB.

Cc @rust-lang/opsem @chorman0773 @m-ou-se @WaffleLapkin @Amanieu

Fixes rust-lang/unsafe-code-guidelines#483

@rustbot

rustbot commented Aug 7, 2024

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 7, 2024
@RalfJung
RalfJung force-pushed the atomic-read-read-races branch from 4c2bc31 to 24c19b8 Compare August 7, 2024 13:03
@rustbot

rustbot commented Aug 7, 2024

Copy link
Copy Markdown
Collaborator

The Miri subtree was changed

cc @rust-lang/miri

@Mark-Simulacrum Mark-Simulacrum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably this will want T-opsem and/or T-lang FCP. Left one comment on the current proposed wording though.

Comment thread library/core/src/sync/atomic.rs Outdated
@RalfJung
RalfJung force-pushed the atomic-read-read-races branch 2 times, most recently from e5b0694 to eae3ecc Compare August 10, 2024 17:47
@rust-log-analyzer

This comment has been minimized.

@RalfJung
RalfJung force-pushed the atomic-read-read-races branch 4 times, most recently from 4382807 to e219737 Compare August 10, 2024 20:44
Comment thread library/core/src/sync/atomic.rs Outdated
//! Undefined Behavior unless both accesses are atomic. Here, accesses are *conflicting* if they
//! affect overlapping regions of memory and at least one of them is a write. They are
//! *non-synchronized* if neither of them *happens-before* the other, according to the
//! happens-before order of the memory model.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously in this documentation we used the term "non-synchronized" for accesses which are not happens-before ordered. I wonder if "unordered" would be a better term?

The C++ memory model does not define a term for this, they just spell out "not happens-before ordered".

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"unordered" sounds better to me, but 🤷🏻

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rust-lang/opsem @rust-lang/lang any opinion on this -- should we rename "non-synchronized" to "unordered"?

@RalfJung
RalfJung force-pushed the atomic-read-read-races branch from e219737 to 5eeb272 Compare August 10, 2024 20:52
Comment thread library/core/src/sync/atomic.rs Outdated
Comment thread src/tools/miri/tests/pass/concurrency/data_race.rs
@RalfJung RalfJung added the I-lang-nominated Nominated for discussion during a lang team meeting. label Aug 12, 2024
@RalfJung

Copy link
Copy Markdown
Member Author

Nominating for t-lang to get their take on this, and to ask them who should be included in the FCP -- just t-opsem, or also t-lang?

@RalfJung RalfJung added T-lang Relevant to the language team T-opsem Relevant to the opsem team and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 12, 2024
@RalfJung
RalfJung force-pushed the atomic-read-read-races branch from 9a0aa41 to 3af35af Compare August 12, 2024 17:44
@traviscross

Copy link
Copy Markdown
Contributor

@rfcbot fcp merge

We discussed this in triage today. This sounded right to us. We'll do this via FCP with T-opsem.

@rfcbot

rfcbot commented Aug 14, 2024

Copy link
Copy Markdown

Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Aug 14, 2024
@scottmcm

Copy link
Copy Markdown
Member

I find the argument that it should always be ok to make a non-atomic read into an atomic read persuasive, so it sounds good from an intent perspective. So as long as the experts agree with how we're formally saying that, sounds good.

@rfcbot reviewed

(I wonder if it's possible to write a codegen test that would have a reasonable chance of noticing LLVM deciding to turn such a read-race into unreachable. That's not blocking here, though.)

@RalfJung

Copy link
Copy Markdown
Member Author

LLVM doesn't have a notion of "atomic object", their entire memory model is access-based, so I can't imagine they'd ever make this UB. I also don't think LLVM turns any obvious data races into unreachable, it "just" performs transformations that exploit the no-data-race assumption, so I can't think of a way to write a test like that.

bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 28, 2024
…iaskrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#128778 (atomics: allow atomic and non-atomic reads to race)
 - rust-lang#130918 (simplify LLVM submodule handling)
 - rust-lang#130960 (Only add an automatic SONAME for Rust dylibs)
 - rust-lang#130973 (compiletest: rename "runtest/crash.rs" to "runtest/crashes.rs" to be in line with the test directory)
 - rust-lang#130976 (remove couple redundant clones)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit 5e4eab4 into rust-lang:master Sep 28, 2024
@rustbot rustbot added this to the 1.83.0 milestone Sep 28, 2024
@bors

ghost commented Sep 28, 2024

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 96be76b with merge e6eb451...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. T-lang Relevant to the language team T-opsem Relevant to the opsem team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How can we allow read-read races between atomic and non-atomic accesses?