Merge changes I1b22a8e3,I06bec55d into main
* changes:
libavf: add API to configure swiotlb size
libavf: use MiB for mebibytes in API function names
diff --git a/android/virtmgr/src/aidl.rs b/android/virtmgr/src/aidl.rs
index 15a80a6..6a268f9 100644
--- a/android/virtmgr/src/aidl.rs
+++ b/android/virtmgr/src/aidl.rs
@@ -774,6 +774,7 @@
.ok()
.and_then(NonZeroU32::new)
.unwrap_or(NonZeroU32::new(256).unwrap()),
+ swiotlb_mib: config.swiotlbMib.try_into().ok().and_then(NonZeroU32::new),
cpus,
host_cpu_topology,
console_out_fd,
diff --git a/android/virtmgr/src/crosvm.rs b/android/virtmgr/src/crosvm.rs
index 2bfa4e1..affd430 100644
--- a/android/virtmgr/src/crosvm.rs
+++ b/android/virtmgr/src/crosvm.rs
@@ -112,6 +112,7 @@
pub protected: bool,
pub debug_config: DebugConfig,
pub memory_mib: NonZeroU32,
+ pub swiotlb_mib: Option<NonZeroU32>,
pub cpus: Option<NonZeroU32>,
pub host_cpu_topology: bool,
pub console_out_fd: Option<File>,
@@ -1000,11 +1001,18 @@
_ => command.arg("--protected-vm"),
};
- // 3 virtio-console devices + vsock = 4.
- let virtio_pci_device_count = 4 + config.disks.len();
- // crosvm virtio queue has 256 entries, so 2 MiB per device (2 pages per entry) should be
- // enough.
- let swiotlb_size_mib = 2 * virtio_pci_device_count as u32;
+ let swiotlb_size_mib = config.swiotlb_mib.map(u32::from).unwrap_or({
+ // 3 virtio-console devices + vsock = 4.
+ // TODO: Count more device types, like balloon, input, and sound.
+ let virtio_pci_device_count = 4 + config.disks.len();
+ // crosvm virtio queue has 256 entries, so 2 MiB per device (2 pages per entry) should
+ // be enough.
+ // NOTE: The above explanation isn't completely accurate, e.g., circa 2024q4, each
+ // virtio-block has 16 queues with 256 entries each and each virito-console has 2
+ // queues of 256 entries each. So, it is allocating less than 2 pages per entry, but
+ // seems to work well enough in practice.
+ 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.
diff --git a/android/virtualizationservice/aidl/android/system/virtualizationservice/VirtualMachineRawConfig.aidl b/android/virtualizationservice/aidl/android/system/virtualizationservice/VirtualMachineRawConfig.aidl
index d98fdcc..9ebb7fe 100644
--- a/android/virtualizationservice/aidl/android/system/virtualizationservice/VirtualMachineRawConfig.aidl
+++ b/android/virtualizationservice/aidl/android/system/virtualizationservice/VirtualMachineRawConfig.aidl
@@ -62,6 +62,9 @@
/** The amount of RAM to give the VM, in MiB. 0 or negative to use the default. */
int memoryMib;
+ /** The amount of swiotlb to give the VM, in MiB. 0 or negative to use the default. */
+ int swiotlbMib;
+
/** The vCPU topology that will be generated for the VM. Default to 1 vCPU. */
CpuTopology cpuTopology = CpuTopology.ONE_CPU;
diff --git a/libs/libavf/include/android/virtualization.h b/libs/libavf/include/android/virtualization.h
index 7ab7431..3a710f3 100644
--- a/libs/libavf/include/android/virtualization.h
+++ b/libs/libavf/include/android/virtualization.h
@@ -33,7 +33,7 @@
*
* This only creates the raw config object. `name` and `kernel` must be set with
* calls to {@link AVirtualMachineRawConfig_setName} and {@link AVirtualMachineRawConfig_setKernel}.
- * Other properties, set by {@link AVirtualMachineRawConfig_setMemoryMib},
+ * Other properties, set by {@link AVirtualMachineRawConfig_setMemoryMiB},
* {@link AVirtualMachineRawConfig_setInitRd}, {@link AVirtualMachineRawConfig_addDisk},
* {@link AVirtualMachineRawConfig_setProtectedVm}, and {@link AVirtualMachineRawConfig_setBalloon}
* are optional.
@@ -121,12 +121,30 @@
/**
* Set how much memory will be given to a virtual machine.
*
+ * When `AVirtualMachineRawConfig_setProtectedVm(..., true)` is set, the memory
+ * size provided here will be automatically augmented with the swiotlb size.
+ *
* \param config a virtual machine config object.
- * \param memoryMib the amount of RAM to give the virtual machine, in MiB. 0 or negative to use the
+ * \param memoryMiB the amount of RAM to give the virtual machine, in MiB. 0 or negative to use the
* default.
*/
-void AVirtualMachineRawConfig_setMemoryMib(AVirtualMachineRawConfig* _Nonnull config,
- int32_t memoryMib) __INTRODUCED_IN(36);
+void AVirtualMachineRawConfig_setMemoryMiB(AVirtualMachineRawConfig* _Nonnull config,
+ int32_t memoryMiB) __INTRODUCED_IN(36);
+
+/**
+ * Set how much swiotlb will be given to a virtual machine.
+ *
+ * Only applicable when `AVirtualMachineRawConfig_setProtectedVm(..., true)` is
+ * set.
+ *
+ * For information on swiotlb, see https://docs.kernel.org/core-api/swiotlb.html.
+ *
+ * \param config a virtual machine config object.
+ * \param memoryMiB the amount of swiotlb to give the virtual machine, in MiB.
+ * 0 or negative to use the default.
+ */
+void AVirtualMachineRawConfig_setSwiotlbMiB(AVirtualMachineRawConfig* _Nonnull config,
+ int32_t swiotlbMiB) __INTRODUCED_IN(36);
/**
* Set whether the virtual machine's memory will be protected from the host, so the host can't
diff --git a/libs/libavf/libavf.map.txt b/libs/libavf/libavf.map.txt
index efc368a..1590753 100644
--- a/libs/libavf/libavf.map.txt
+++ b/libs/libavf/libavf.map.txt
@@ -7,7 +7,8 @@
AVirtualMachineRawConfig_setKernel; # apex llndk
AVirtualMachineRawConfig_setInitRd; # apex llndk
AVirtualMachineRawConfig_addDisk; # apex llndk
- AVirtualMachineRawConfig_setMemoryMib; # apex llndk
+ AVirtualMachineRawConfig_setMemoryMiB; # apex llndk
+ AVirtualMachineRawConfig_setSwiotlbMiB; # apex llndk
AVirtualMachineRawConfig_setProtectedVm; # apex llndk
AVirtualMachineRawConfig_setHypervisorSpecificAuthMethod; # apex llndk
AVirtualMachineRawConfig_addCustomMemoryBackingFile; # apex llndk
diff --git a/libs/libavf/src/lib.rs b/libs/libavf/src/lib.rs
index 56cdfb7..98db246 100644
--- a/libs/libavf/src/lib.rs
+++ b/libs/libavf/src/lib.rs
@@ -180,7 +180,7 @@
/// # Safety
/// `config` must be a pointer returned by `AVirtualMachineRawConfig_create`.
#[no_mangle]
-pub unsafe extern "C" fn AVirtualMachineRawConfig_setMemoryMib(
+pub unsafe extern "C" fn AVirtualMachineRawConfig_setMemoryMiB(
config: *mut VirtualMachineRawConfig,
memory_mib: i32,
) {
@@ -190,6 +190,21 @@
config.memoryMib = memory_mib;
}
+/// Set how much swiotlb will be given to a virtual machine.
+///
+/// # Safety
+/// `config` must be a pointer returned by `AVirtualMachineRawConfig_create`.
+#[no_mangle]
+pub unsafe extern "C" fn AVirtualMachineRawConfig_setSwiotlbMiB(
+ config: *mut VirtualMachineRawConfig,
+ swiotlb_mib: i32,
+) {
+ // SAFETY: `config` is assumed to be a valid, non-null pointer returned by
+ // AVirtualMachineRawConfig_create. It's the only reference to the object.
+ let config = unsafe { &mut *config };
+ config.swiotlbMib = swiotlb_mib;
+}
+
/// Set whether a virtual machine is protected or not.
///
/// # Safety