Skip to content

Conversation

roypat
Copy link
Member

@roypat roypat commented Nov 12, 2024

Summary of the PR

This PR aim to clean up the API surface of the vm-memory crate in a way that is as unintrusive as possible. It does this in two ways

  • Remove the myriad of deprecated functions we have accumulated over the years, and
  • Improve documentation to capture some of the more quirky edge cases some functions have (completely ignoring the question of whether some of these should be changed for now)

Additionally, it fixes up a goof I made when I introduced the ReadVolatile and WriteVolatile traits in #247, where I put the read_volatile_from and friends funcions into the wrong trait. When it comes to how to implement these traits for GuestMemoryMmap and VolatileSlice, I've deferred to how the old Read and Write based functions were implemented for them.

I've compiled the vm-virtio workspace and linux-loader against this. All tests pass in both, only linux-loader needs a tiny change to import the Bytes trait now.

If one squints at the code a bit, one might ask whether all the read/read_slice functions in the Bytes trait could be default functions, implemented in terms of the volatile functions (since &mut [u8] implements the volatile access traits). I played around with this, but there are subtle functional and performance changes involved in that (although it would be a great simplification), which is why I'm holding off on doing anything there until a later PR.

Requirements

Before submitting your PR, please make sure you addressed the following
requirements:

  • All commits in this PR have Signed-Off-By trailers (with
    git commit -s), and the commit message has max 60 characters for the
    summary and max 75 characters for each description line.
  • All added/changed functionality has a corresponding unit/integration
    test.
  • All added/changed public-facing functionality has entries in the "Upcoming
    Release" section of CHANGELOG.md (if no such section exists, please create one).
  • Any newly added unsafe code is properly documented.

@TimePrinciple
Copy link

With this change, the next release should be 0.17.0 I guess 🤔

@roypat roypat force-pushed the deprecation-cleanup branch 2 times, most recently from 559c3c6 to becfd0e Compare November 13, 2024 07:23
@roypat
Copy link
Member Author

roypat commented Nov 13, 2024

With this change, the next release should be 0.17.0 I guess 🤔

Yup!

Copy link

@TimePrinciple TimePrinciple left a comment

Choose a reason for hiding this comment

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

Really good work, some thoughts 😉

@roypat roypat force-pushed the deprecation-cleanup branch 2 times, most recently from 7be0764 to 8fd12ef Compare November 13, 2024 11:53
Copy link
Member

@bonzini bonzini left a comment

Choose a reason for hiding this comment

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

I'm too out of the loop to approve anything, but it looks good.

The only comments I have are:

  • you could add methods to read/write a ByteValued from an impl Read/impl Write
  • truncate needs to be renamed, and I'd personally make it return Result but I'm not going to insiste

So the next release would be 1.0? :)

@roypat
Copy link
Member Author

roypat commented Feb 3, 2025

I'm too out of the loop to approve anything, but it looks good.

Thanks for having a look anyway!

The only comments I have are:

  • you could add methods to read/write a ByteValued from an impl Read/impl Write

  • truncate needs to be renamed, and I'd personally make it return Result but I'm not going to insiste

see inline comments :)

So the next release would be 1.0? :)

slowly but surely working towards it at least, I hope :D

@roypat roypat force-pushed the deprecation-cleanup branch from 37b015b to 32324d0 Compare February 3, 2025 12:53
This fixes a cargo warning printed in recent toolchains. Since we do not
support toolchains older than 1.38, there is no need to symlink to the
new file to the old one.

Signed-off-by: Patrick Roy <[email protected]>
The call to `.subslice()` ensures that the range `[0, total]` is a valid
subslice of the `buf: VolatileSlice` passed in. But `total =
min(buf.len(), self.len()) <= buf.len()`, and thus `[0, total]` is
definitely a valid subslice of `buf`.

The `copy_{from,to}_volatile_slice` functions do not actually care about
the length of the `VolatileSlice` passed in - it relies on the safety
invariant to ensure that the passed slice has length at least `total`.
Thus it doesn't matter if we pass a slice of length `total`, or of a
length greater than `total`. It will simply access the first `total`
bytes in the slice.

Also clarify the safety comment, as some slightly mistakes seemingly
snuck in when copying them.

Signed-off-by: Patrick Roy <[email protected]>
Fix the doc comment, and add a unit test for this functions.
Particularly the "allow length 0 accesses at the end of the slice"
behavior was undocumented before.

Signed-off-by: Patrick Roy <[email protected]>
The comment on the assertion was wrong, as we are not even doing
anything unsafe in the first place. Additionally, the `offset` variable
is unused by these functions, so the assertion is at best a sanity check
that the `try_access` implementation is correct, although I don't
particularly see the value of that. Remove the assertion to prevent
confusion.

Signed-off-by: Patrick Roy <[email protected]>
The two functions contain exactly the same body, so just have one call
the other.

No functional change intended.

Signed-off-by: Patrick Roy <[email protected]>
Add a macro that automatically retries a given I/O operation if EINTR is
returned. This is a fairly common pattern in vm-memory.

Signed-off-by: Patrick Roy <[email protected]>
The old `Read`/`Write` based APIs for operating on guest memory have
been deprecated for a fairly long time now, and are superseceded by
their `ReadVolatile`/`WriteVolatile` equivalents.

The `fold` and friends functions have been deprecated for way longer.

Various `.as_ptr` APIs in volatile_memory.rs are deprecated in favor of
pointer guards.

Let's clean up the crate and remove all of these.

Signed-off-by: Patrick Roy <[email protected]>
@roypat roypat force-pushed the deprecation-cleanup branch from 32324d0 to 496dfa3 Compare February 3, 2025 12:57
The `read_[exact_]_volatile_from` and `write_[all_]volatile_to`
functions were intended to be replacements for their `Read` and `Write`
based counterparts. However, those used to live in the `Bytes` trait,
not the `GuestMemory` trait. Fix up this goof on my part by moving them
to the `Bytes` trait.

Signed-off-by: Patrick Roy <[email protected]>
Capture the actual behavior of various edge cases around empty buffers
and containers in the doc comment.

Signed-off-by: Patrick Roy <[email protected]>
Signed-off-by: Patrick Roy <[email protected]>
It's the same cast that happens, but like this we can use the `From<&mut
[u8]>` impl for VolatileSlice and avoid an unsafe block.

Signed-off-by: Patrick Roy <[email protected]>
Without this, the impl of `ByteValued` was technically undefined, although
we had some const asserts that ensured rustc didn't muck us over by
adding padding or anything.

Signed-off-by: Patrick Roy <[email protected]>
@roypat roypat force-pushed the deprecation-cleanup branch from 496dfa3 to d59e1f6 Compare February 3, 2025 13:42
andreeaflorescu
andreeaflorescu previously approved these changes Feb 7, 2025
Copy link
Member

@bonzini bonzini left a comment

Choose a reason for hiding this comment

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

Sorry, one more small request.

Suggested-by: Paolo Bonzini <[email protected]>
Signed-off-by: Patrick Roy <[email protected]>
We had the same unsafe code for constructing an all-zeroes `ByteValued`
in two locations. Unify these into a single `ByteValued::zeroed()`
function.

Suggested-by: Paolo Bonzini <[email protected]>
Signed-off-by: Patrick Roy <[email protected]>
@roypat roypat force-pushed the deprecation-cleanup branch from 41b9edd to 1ab6cd6 Compare February 7, 2025 10:18
@bonzini
Copy link
Member

bonzini commented Feb 7, 2025

Thanks and sorry for the late feedback.

@bonzini bonzini self-requested a review February 7, 2025 10:39
@bonzini bonzini merged commit 6c559cb into rust-vmm:main Feb 7, 2025
2 checks passed
@bonzini bonzini added this to the vm-memory 0.17.0 milestone Sep 8, 2025
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.

5 participants