Skip to content

Commit 0354dcf

Browse files
author
Hui Zhu
committed
Add is_hugetlbfs() to GuestMemoryRegion
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. Signed-off-by: Hui Zhu <[email protected]>
1 parent 54c8934 commit 0354dcf

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

src/guest_memory.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ pub trait GuestMemoryRegion: Bytes<MemoryRegionAddress, E = Error> {
288288
fn as_volatile_slice(&self) -> Result<volatile_memory::VolatileSlice> {
289289
self.get_slice(MemoryRegionAddress(0), self.len() as usize)
290290
}
291+
292+
/// Returns true if the region is hugetlbfs
293+
fn is_hugetlbfs(&self) -> bool {
294+
false
295+
}
291296
}
292297

293298
/// `GuestAddressSpace` provides a way to retrieve a `GuestMemory` object.
@@ -1106,4 +1111,13 @@ mod tests {
11061111

11071112
crate::bytes::tests::check_atomic_accesses(mem, addr, bad_addr);
11081113
}
1114+
1115+
#[cfg(feature = "backend-mmap")]
1116+
#[test]
1117+
fn test_guest_memory_mmap_is_hugetlbfs() {
1118+
let addr = GuestAddress(0x1000);
1119+
let mem = GuestMemoryMmap::from_ranges(&[(addr, 0x1000)]).unwrap();
1120+
let r = mem.find_region(addr).unwrap();
1121+
assert_eq!(r.is_hugetlbfs(), false);
1122+
}
11091123
}

src/mmap.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ impl GuestMemoryRegion for GuestRegionMmap {
403403
let slice = self.mapping.get_slice(offset.raw_value() as usize, count)?;
404404
Ok(slice)
405405
}
406+
407+
fn is_hugetlbfs(&self) -> bool {
408+
self.mapping.is_hugetlbfs()
409+
}
406410
}
407411

408412
/// [`GuestMemory`](trait.GuestMemory.html) implementation that mmaps the guest's memory

src/mmap_unix.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pub struct MmapRegion {
9090
prot: i32,
9191
flags: i32,
9292
owned: bool,
93+
hugetlbfs: bool,
9394
}
9495

9596
// Send and Sync aren't automatically inherited for the raw address pointer.
@@ -172,6 +173,7 @@ impl MmapRegion {
172173
prot,
173174
flags,
174175
owned: true,
176+
hugetlbfs: false,
175177
})
176178
}
177179

@@ -213,6 +215,7 @@ impl MmapRegion {
213215
prot,
214216
flags,
215217
owned: false,
218+
hugetlbfs: false,
216219
})
217220
}
218221

@@ -272,6 +275,16 @@ impl MmapRegion {
272275
}
273276
false
274277
}
278+
279+
/// Set the hugetlbfs of the region
280+
pub fn set_hugetlbfs(&mut self, hugetlbfs: bool) {
281+
self.hugetlbfs = hugetlbfs
282+
}
283+
284+
/// Returns `true` if the region is hugetlbfs
285+
pub fn is_hugetlbfs(&self) -> bool {
286+
self.hugetlbfs
287+
}
275288
}
276289

277290
impl AsSlice for MmapRegion {
@@ -354,6 +367,24 @@ mod tests {
354367
);
355368
}
356369

370+
#[test]
371+
fn test_mmap_region_set_hugetlbfs() {
372+
assert!(MmapRegion::new(0).is_err());
373+
374+
let size = 4096;
375+
376+
let mut r = MmapRegion::new(4096).unwrap();
377+
r.set_hugetlbfs(true);
378+
assert_eq!(r.size(), size);
379+
assert!(r.file_offset().is_none());
380+
assert_eq!(r.prot(), libc::PROT_READ | libc::PROT_WRITE);
381+
assert_eq!(
382+
r.flags(),
383+
libc::MAP_ANONYMOUS | libc::MAP_NORESERVE | libc::MAP_PRIVATE
384+
);
385+
assert_eq!(r.is_hugetlbfs(), true);
386+
}
387+
357388
#[test]
358389
fn test_mmap_region_from_file() {
359390
let mut f = TempFile::new().unwrap().into_file();

src/mmap_windows.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ impl MmapRegion {
175175
pub fn file_offset(&self) -> Option<&FileOffset> {
176176
self.file_offset.as_ref()
177177
}
178+
179+
/// Windows doesn't have hugetlbfs.
180+
pub fn is_hugetlbfs(&self) -> bool {
181+
false
182+
}
178183
}
179184

180185
impl AsSlice for MmapRegion {

0 commit comments

Comments
 (0)