Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Aug 7, 2024

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
Copy link
Collaborator

rustbot commented Aug 7, 2024

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
@rustbot
Copy link
Collaborator

rustbot commented Aug 7, 2024

The Miri subtree was changed

cc @rust-lang/miri

Copy link
Member

@Mark-Simulacrum Mark-Simulacrum left a comment

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.

library/core/src/sync/atomic.rs Outdated Show resolved Hide resolved
@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
//! 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
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
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
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 added the I-lang-nominated Nominated for discussion during a lang team meeting. label Aug 12, 2024
@RalfJung
Copy link
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, which will review and decide on the PR/issue. 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
@traviscross
Copy link
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
Copy link

rfcbot commented Aug 14, 2024

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
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
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.

@Mark-Simulacrum Mark-Simulacrum added the S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. label Aug 18, 2024
@RalfJung RalfJung force-pushed the atomic-read-read-races branch 2 times, most recently from c5b8907 to 3e5a403 Compare August 21, 2024 17:58
library/core/src/cell.rs Outdated Show resolved Hide resolved
library/core/src/cell.rs Outdated Show resolved Hide resolved
@RalfJung RalfJung force-pushed the atomic-read-read-races branch 2 times, most recently from 0e74dc5 to 1bd9763 Compare August 21, 2024 18:36
@tmandry
Copy link
Member

tmandry commented Aug 23, 2024

@rfcbot reviewed

Copy link

@lschuermann lschuermann left a comment

Choose a reason for hiding this comment

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

This greatly improves the documentation of core::sync::atomic and removes ambiguity around UnsafeCell's interplay with concurrent accesses.

src/tools/miri/src/concurrency/data_race.rs Outdated Show resolved Hide resolved
@RalfJung RalfJung force-pushed the atomic-read-read-races branch 2 times, most recently from 6be1b92 to 6877b1d Compare August 24, 2024 07:16
@RalfJung
Copy link
Member Author

@digama0 @nikomatsakis @pnkfelix @saethlin friendly FCP reminder ping :)

@RalfJung
Copy link
Member Author

@digama0 @nikomatsakis @pnkfelix @saethlin there's an FCP waiting for you here :)

@bors
Copy link
Contributor

bors commented Sep 15, 2024

☔ The latest upstream changes (presumably #130401) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Sep 15, 2024
@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Sep 17, 2024
@rfcbot
Copy link

rfcbot commented Sep 17, 2024

🔔 This is now entering its final comment period, as per the review above. 🔔

@nikomatsakis
Copy link
Contributor

@rfcbot reviewed

- UnsafeCell: mention the term "data race", and reference the data race definition
- atomic: failing RMWs are just reads, reorder and reword docs
@RalfJung
Copy link
Member Author

@Mark-Simulacrum is this ready to land from your side, assuming FCP will pass uneventful?

@bors
Copy link
Contributor

bors commented Sep 17, 2024

☔ The latest upstream changes (presumably #130483) made this pull request unmergeable. Please resolve the merge conflicts.

@Mark-Simulacrum
Copy link
Member

Yeah, r=me

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. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. T-lang Relevant to the language team, which will review and decide on the PR/issue. 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?