-
Notifications
You must be signed in to change notification settings - Fork 102
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 is_hugetlbfs() to GuestMemoryRegion #120
Conversation
src/guest_memory.rs
Outdated
@@ -288,6 +288,11 @@ pub trait GuestMemoryRegion: Bytes<MemoryRegionAddress, E = Error> { | |||
fn as_volatile_slice(&self) -> Result<volatile_memory::VolatileSlice> { | |||
self.get_slice(MemoryRegionAddress(0), self.len() as usize) | |||
} | |||
|
|||
/// Returns true if the region is hugepages | |||
fn is_hugepages(&self) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does hugepage here mean Hugetlbfs? Or it also includes Transparent Huge Pages?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, hugepage means Hugetlbfs.
THPs can be release as normal pages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hugepage is a little ambiguous, should we clearly state that it's hugetlbfs?
Maybe we lack of some method to attaching some label information to memory regions. For the |
What about add a new u64 flags to MmapRegion to store the label of a region? |
98d0b34
to
0354dcf
Compare
@jiangliu Pushed a new version according to your comments. |
Hi! Are you using something like However, if you know you're using |
Yes, I am working on virtio-balloon free page reporting of cloud-hypervisor. Its areas without backing file are based on memfds. The areas will setup with MFD_HUGETLB if need.
Thanks! I will use it instead of "set_hugetlbfs" in the next version.
I would like to use MADV_FREE as a option but not default.
Best, |
0354dcf
to
9428858
Compare
In addition to mmap(MFD_HUGETLB), there are other ways to use hugetlbfs backend. So detecting the MFD_HUGETLB flag may not be concise. |
9428858
to
bf4d32d
Compare
It's true there are multiple ways to leverage huge pages in general, but specifically within @teawater, just wondering, why is lazy free worse? Also, with respect to point number 2, you'd use That being said, having a high level api to provide page size information sounds interesting. For example I'm wondering whether more information than a simple boolean flag is useful/necessary (i.e. the actual page size). Also, looks like Windows supports something similar but they're called large pages. I would def like to think about his more. @teawater is your use case blocked the existing |
Yes.
lazy free is not suitable for many environments because the pages will be released when memory pressure.
Thanks for your reminding.
I think it is not about page size but page type. |
Thanks for the details! I'll have a better look at these things and return to the thread afterwards. Meanwhile, it would be great if other folks chime in as well. |
This makes sense, but for this specific issue I'm not understanding the purpose behind having |
Our current solution is to create file descriptors by the vmm and build memory regions by using the MmapRegion::build() interface. In other words, we have moved the dirty work into vmm:) |
Hi again! Seems like the current iteration of the PR is the most straightforward thing we can do right now to provide the desired functionality. Like @EmeraldShift mentioned, the Just to clarify the semantics here a bit, if a region is backed by transparent huge pages, the |
Yes.
What about add the description to https://github.com/rust-vmm/vm-memory/blob/master/DESIGN.md#backend-implementation-based-on-mmap ?
|
Sounds good! |
Hi @teawater, we're having some trouble with the CI, so builds might be failing or hanging. We' re sorry for the inconvenience, we'll update you here once it's back up and running. |
@aghecenco OK. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi everyone and apologies for the delay in circling back. Left two more comments that I recently thought about; would be great to hear your opinion on them. After we have a conclusion, we should also add a changelog entry for the additions and decide whether we want the is_hugetlbs
method to be conditionally compiled/included depending on the OS, and we're good to go.
DESIGN.md
Outdated
@@ -122,6 +122,18 @@ let buf = &mut [0u8; 5]; | |||
let result = guest_memory_mmap.write(buf, addr); | |||
``` | |||
|
|||
One of the responsibilities of `GuestRegionMmap` is to provide an API |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, it's probably best to move something like this description and example to the doc comments of the is_hugetlbfs
method from the GuestMemory
interface, where it gets better visibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
src/guest_memory.rs
Outdated
@@ -288,6 +288,11 @@ pub trait GuestMemoryRegion: Bytes<MemoryRegionAddress, E = Error> { | |||
fn as_volatile_slice(&self) -> Result<volatile_memory::VolatileSlice> { | |||
self.get_slice(MemoryRegionAddress(0), self.len() as usize) | |||
} | |||
|
|||
/// Returns true if the region is backed by hugetlbfs | |||
fn is_hugetlbfs(&self) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, would it actually make sense to return an Option<bool>
, where None
represents that no information is available? In our particular situation (where is_hugetlbfs
has to be set via the set_hugetlbfs
accesor), this would also be a natural fit since the field starts out as None
, and only becomes true
or false
if it's explicitly set. Just wondering what other folks think about this approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
6ff201f
to
1dd3577
Compare
src/mmap_unix.rs
Outdated
@@ -172,6 +173,7 @@ impl MmapRegion { | |||
prot, | |||
flags, | |||
owned: true, | |||
hugetlbfs: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have the hugetlbfs
field of MmapRegion
as an Option<bool>
which is None
initially? (since there's no available information right now, until we explicitly set it with set_hugetlbfs
)
/// # Examples | ||
/// | ||
/// ``` | ||
/// # #[cfg(feature = "backend-mmap")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We just merged a PR that updated all the code examples that needed features. Can you please update this example to follow that template?
Changes that would be needed:
- specify in the header that this example is based on the the mmap feature. Here is an example:
Line 266 in 576a406
/// # Examples (uses the `backend-mmap` feature) - declare the
[cfg(feature = "backend-mmap")]
only one at the begginging of the example, and get rid of thetest_guest_memory_mmap_is_hugetlbfs
function. The code can sit directly in the example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test_guest_memory_mmap_is_hugetlbs
function is still there. To get rid of it, and also declare the #[cfg(feature = "backend-mmap")]
, we would need to start a code block (and have it all conditionally compiled only for that feature).
/// # #[cfg(feature = "backend-mmap")]
/// # {
/// # use vm_memory::{GuestAddress, GuestMemory, GuestMemoryMmap, GuestRegionMmap};
///
/// let addr = GuestAddress(0x1000);
/// let mem = GuestMemoryMmap::from_ranges(&[(addr, 0x1000)]).unwrap();
/// let r = mem.find_region(addr).unwrap();
/// assert_eq!(r.is_hugetlbfs(), None);
/// # }
With this approach, you no longer need the function test_guest_memory_mmap_is_hugetlbfs
, and all the cfg macros.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cargo fmt
fails because of alignment. The code needs to be aligned as in my previous comment. We can fix it in a subsequent PR.
Virtio-balloon can release the unused host memory to decrease the memory usage of the VMM. Release normal pages and hugetlbfs pages requiring different operations. (madvise MADV_DONTNEED and fallocate64 FALLOC_FL_PUNCH_HOLE) This commit add Add is_hugetlbfs() to GuestMemoryRegion to help VMM decide if this is a hugetlbfs address or not. It returns None represents that no information is available. Signed-off-by: Hui Zhu <[email protected]>
|
||
#[cfg(feature = "backend-mmap")] | ||
#[test] | ||
fn test_guest_memory_mmap_is_hugetlbfs() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicates the test in comment. (as pointed out before)
Why not add a |
/// # Examples | ||
/// | ||
/// ``` | ||
/// # #[cfg(feature = "backend-mmap")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cargo fmt
fails because of alignment. The code needs to be aligned as in my previous comment. We can fix it in a subsequent PR.
/// assert_eq!(r.is_hugetlbfs(), None); | ||
/// # } | ||
/// ``` | ||
fn is_hugetlbfs(&self) -> Option<bool> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late comment, but can we actually just use conditional compilation here and make it available only on Linux, instead of having a blank implementation for Windows?
I think it might makes sense to have it part of the interface in case this information is needed by crates that do not take a dependency on the mmap backend feature. |
I see. But can't we remove the need for set_hugetlbfs() and query the underlying memory instead? (via some trait) |
This is an interesting point, that would make the implementation nicer. I do not really know how could we achieve this, so any ideas would be welcome. @bonzini @jiangliu @alexandruag do you have a suggestion? |
I don't know any easy way to figure out whether a fd is backed by hugetlbfs:( |
I was thinking of simply relying on the flags that were given to mmap(). |
It works for anonymous memory backed by hugettlbfs by detecting the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a long standing PR, let's just merge it, and submit a PR with the required changes afterwards.
Virtio-balloon can release the unused host memory to decrease the memory usage of the VMM.
Release normal pages and hugetlbfs pages require different operations. (madvise MADV_DONTNEED and fallocate64 FALLOC_FL_PUNCH_HOLE)
This commit add Add is_hugetlbfs() to GuestMemoryRegion to help VMM decide if this is a hugetlbfs address or not.