Skip to content

Commit f11b1a4

Browse files
committed
fix: do mib_to_bytes conversions in [swiotlb|memory]_size fns
Fold this in to the previous PR Signed-off-by: Patrick Roy <[email protected]>
1 parent 414b6c7 commit f11b1a4

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/vmm/src/resources.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -467,25 +467,25 @@ impl VmResources {
467467

468468
/// The size of the swiotlb region requested, in MiB
469469
#[cfg(target_arch = "aarch64")]
470-
pub fn swiotlb_size_mib(&self) -> usize {
471-
self.machine_config.mem_config.initial_swiotlb_size
470+
pub fn swiotlb_size(&self) -> usize {
471+
mib_to_bytes(self.machine_config.mem_config.initial_swiotlb_size)
472472
}
473473

474474
/// The size of the swiotlb region requested, in MiB
475475
#[cfg(target_arch = "x86_64")]
476-
pub fn swiotlb_size_mib(&self) -> usize {
476+
pub fn swiotlb_size(&self) -> usize {
477477
0
478478
}
479479

480480
/// Gets the size of the "traditional" memory region, e.g. total memory excluding the swiotlb
481481
/// region.
482482
pub fn memory_size(&self) -> usize {
483-
self.machine_config.mem_size_mib - self.swiotlb_size_mib()
483+
mib_to_bytes(self.machine_config.mem_size_mib) - self.swiotlb_size()
484484
}
485485

486486
/// Whether the use of swiotlb was requested
487487
pub fn swiotlb_used(&self) -> bool {
488-
self.swiotlb_size_mib() > 0
488+
self.swiotlb_size() > 0
489489
}
490490

491491
fn allocate_memory(
@@ -546,7 +546,7 @@ impl VmResources {
546546
// that would not be worth the effort.
547547
self.allocate_memory(
548548
0,
549-
mib_to_bytes(self.memory_size()),
549+
self.memory_size(),
550550
self.vhost_user_devices_used() && !self.swiotlb_used(),
551551
guest_memfd,
552552
)
@@ -558,12 +558,18 @@ impl VmResources {
558558
return Ok(None);
559559
}
560560

561-
let swiotlb_size = mib_to_bytes(self.swiotlb_size_mib());
562-
let start = mib_to_bytes(self.machine_config.mem_size_mib) - swiotlb_size;
561+
// We already allocated at least self.memory_size() bytes of non swiotlb memory
562+
let start = self.memory_size();
563+
// Ensure that swiotlb region gets placed after the mmio gap, to avoid the possibility
564+
// of its getting split (which we cannot handle).
563565
let start = start.max(crate::arch::bytes_before_last_gap());
564566

565-
let mut mem =
566-
self.allocate_memory(start, swiotlb_size, self.vhost_user_devices_used(), None)?;
567+
let mut mem = self.allocate_memory(
568+
start,
569+
self.swiotlb_size(),
570+
self.vhost_user_devices_used(),
571+
None,
572+
)?;
567573

568574
assert_eq!(mem.len(), 1);
569575

0 commit comments

Comments
 (0)