Add sizeof(swiotlb) to the mem size of pKVM pVMs
Previously (and even with this change), under pKVM, protected VMs
reserve some amount of memory as swiotlb. It is to share buffers for
virtio devices with the host safely. See [1] for more detail.
This however meant protected VMs, when compared to non-protected VMs,
got "effectively" smaller amount of RAM available. For VMs running
Microdroid, the swiotlb size is current 14 MB.
This CL fixes such an inconsistent experience across protected and
non-protected VMs. To counter balance the reserved memory for swiotlb,
this CL adds sizeof(swiotlb) (i.e. 14 MB currently) to protected VMs
running under pKVM.
Other hypervisors (GenieZone and Gunyah) don't need this treatment
because they statically allocate swiotlb outside of guest RAM.
Bug: 346770542
Test: watch our benchmark
[1] https://source.android.com/docs/core/virtualization/architecture#virtio
Change-Id: I82eb8796b248d4f6a189824841c245186cea2075
diff --git a/libs/hypervisor_props/src/lib.rs b/libs/hypervisor_props/src/lib.rs
index 14614fd..6665bc5 100644
--- a/libs/hypervisor_props/src/lib.rs
+++ b/libs/hypervisor_props/src/lib.rs
@@ -37,3 +37,8 @@
pub fn version() -> Result<Option<String>> {
Ok(hypervisorproperties::hypervisor_version()?)
}
+
+/// Returns if the hypervisor is pKVM
+pub fn is_pkvm() -> Result<bool> {
+ Ok(version()?.unwrap_or_default().starts_with("kvm") && is_protected_vm_supported()?)
+}
diff --git a/virtualizationmanager/src/crosvm.rs b/virtualizationmanager/src/crosvm.rs
index 4b03bac..bf01519 100644
--- a/virtualizationmanager/src/crosvm.rs
+++ b/virtualizationmanager/src/crosvm.rs
@@ -854,6 +854,8 @@
command.arg("--no-balloon");
}
+ let mut memory_mib = config.memory_mib;
+
if config.protected {
match system_properties::read(SYSPROP_CUSTOM_PVMFW_PATH)? {
Some(pvmfw_path) if !pvmfw_path.is_empty() => {
@@ -869,6 +871,12 @@
let swiotlb_size_mib = 2 * virtio_pci_device_count as u32;
command.arg("--swiotlb").arg(swiotlb_size_mib.to_string());
+ // b/346770542 for consistent "usable" memory across protected and non-protected VMs under
+ // pKVM.
+ if hypervisor_props::is_pkvm()? {
+ memory_mib = memory_mib.map(|m| m.saturating_add(swiotlb_size_mib));
+ }
+
// Workaround to keep crash_dump from trying to read protected guest memory.
// Context in b/238324526.
command.arg("--unmap-guest-memory-on-fork");
@@ -890,7 +898,7 @@
command.arg("--params").arg("console=hvc0");
}
- if let Some(memory_mib) = config.memory_mib {
+ if let Some(memory_mib) = memory_mib {
command.arg("--mem").arg(memory_mib.to_string());
}