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

Add methods for atomic access to Bytes #115

Merged
merged 4 commits into from Sep 29, 2020

Conversation

alexandruag
Copy link
Collaborator

@alexandruag alexandruag commented Sep 22, 2020

This PR adds the load and store methods to the Bytes interface, which can be used to perform atomic accesses based on a specified Ordering argument. It's a simplified variant of #104, which does not include the abstractions for aligned accesses.

The "integer-atomics" feature can be replaced by using intrinsic `cfg`
annotations (such as detecting different platforms). Also removed the
implementation of `AtomicValued` for `AtomicBool` because we cannot
safely reinterpret locations in guest memory as `bool` (values other
than 0 and 1 may lead to undefined behaviour). Renamed `AtomicValued`
to `AtomicInteger`, which seems like a more accurate description.

Signed-off-by: Alexandru Agache <aagch@amazon.com>
@@ -12,7 +12,6 @@ autobenches = false

[features]
default = []
integer-atomics = []
Copy link
Member

Choose a reason for hiding this comment

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

This seems like a breaking change, should we bump the minor version number for next release?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sounds good. This also reminds me I have to update the changelog.

@@ -0,0 +1,86 @@
use std::sync::atomic::Ordering;
Copy link
Member

Choose a reason for hiding this comment

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

Could you please help to add a license header?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done


/// Objects that implement this trait must consist exclusively of atomic types
/// from [`std::sync::atomic`](https://doc.rust-lang.org/std/sync/atomic/), except for
/// [`AtomicPtr<T>`](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicPtr.html).
Copy link
Member

Choose a reason for hiding this comment

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

How about AtomicBool?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

AtomicBool is dangerous in this context (i.e. we're essentially casting some memory from the guest to a type), because values other than 0 and 1 for a bool value may lead to undefined behaviour.

Copy link
Member

Choose a reason for hiding this comment

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

So it may be better to refine the comments as "except for [AtomicPtr<T>] and AtomicBool".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's totally right. There was no mention of AtomicBool initially, but I've added except for AtomicPtr and AtomicBool ... as part of the commit which expands the AtomicInteger trait. The message from the commit that removes the AtomicInteger for AtomicBool impl also offers a bit more details abou the why.

Turn `AtomicInteger` into a trait that abstracts away atomic
operations (strange that Rust does not have this already). We only
include `load` and `store` for now, but others can be added later.

Signed-off-by: Alexandru Agache <aagch@amazon.com>
Signed-off-by: Alexandru Agache <aagch@amazon.com>
Signed-off-by: Alexandru Agache <aagch@amazon.com>
@bonzini
Copy link
Member

bonzini commented Sep 25, 2020

Looks good to me!

@jiangliu
Copy link
Member

LGTM

@aghecenco aghecenco merged commit eb93387 into rust-vmm:master Sep 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants