Skip to content

Commit c9a9cfb

Browse files
committed
use userspace bounce buffers if secret freedom is enabled
Needed because we cannot do I/O straight into secret hidden memory - the host kernel cannot access it. Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
1 parent 841ac04 commit c9a9cfb

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

src/vmm/src/builder.rs

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,24 @@ pub fn build_microvm_for_boot(
291291
&mut boot_cmdline,
292292
vm_resources.block.devices.iter(),
293293
event_manager,
294+
vm_resources.machine_config.secret_free,
294295
)?;
295296
attach_net_devices(
296297
&mut vmm,
297298
&mut boot_cmdline,
298299
vm_resources.net_builder.iter(),
299300
event_manager,
301+
vm_resources.machine_config.secret_free,
300302
)?;
301303

302304
if let Some(unix_vsock) = vm_resources.vsock.get() {
303-
attach_unixsock_vsock_device(&mut vmm, &mut boot_cmdline, unix_vsock, event_manager)?;
305+
attach_unixsock_vsock_device(
306+
&mut vmm,
307+
&mut boot_cmdline,
308+
unix_vsock,
309+
event_manager,
310+
vm_resources.machine_config.secret_free,
311+
)?;
304312
}
305313

306314
if let Some(entropy) = vm_resources.entropy.get() {
@@ -617,9 +625,14 @@ fn attach_virtio_device<T: 'static + VirtioDevice + MutEventSubscriber + Debug>(
617625
device: Arc<Mutex<T>>,
618626
cmdline: &mut LoaderKernelCmdline,
619627
is_vhost_user: bool,
628+
secret_free: bool,
620629
) -> Result<(), MmioError> {
621630
event_manager.add_subscriber(device.clone());
622631

632+
if secret_free {
633+
device.lock().unwrap().force_userspace_bounce_buffers();
634+
}
635+
623636
// The device mutex mustn't be locked here otherwise it will deadlock.
624637
let device = MmioTransport::new(vmm.vm.guest_memory().clone(), device, is_vhost_user);
625638
vmm.mmio_device_manager
@@ -675,6 +688,7 @@ fn attach_entropy_device(
675688
entropy_device.clone(),
676689
cmdline,
677690
false,
691+
false,
678692
)
679693
}
680694

@@ -683,6 +697,7 @@ fn attach_block_devices<'a, I: Iterator<Item = &'a Arc<Mutex<Block>>> + Debug>(
683697
cmdline: &mut LoaderKernelCmdline,
684698
blocks: I,
685699
event_manager: &mut EventManager,
700+
secret_free: bool,
686701
) -> Result<(), StartMicrovmError> {
687702
for block in blocks {
688703
let (id, is_vhost_user) = {
@@ -707,6 +722,7 @@ fn attach_block_devices<'a, I: Iterator<Item = &'a Arc<Mutex<Block>>> + Debug>(
707722
block.clone(),
708723
cmdline,
709724
is_vhost_user,
725+
secret_free,
710726
)?;
711727
}
712728
Ok(())
@@ -717,11 +733,20 @@ fn attach_net_devices<'a, I: Iterator<Item = &'a Arc<Mutex<Net>>> + Debug>(
717733
cmdline: &mut LoaderKernelCmdline,
718734
net_devices: I,
719735
event_manager: &mut EventManager,
736+
secret_free: bool,
720737
) -> Result<(), StartMicrovmError> {
721738
for net_device in net_devices {
722739
let id = net_device.lock().expect("Poisoned lock").id().clone();
723740
// The device mutex mustn't be locked here otherwise it will deadlock.
724-
attach_virtio_device(event_manager, vmm, id, net_device.clone(), cmdline, false)?;
741+
attach_virtio_device(
742+
event_manager,
743+
vmm,
744+
id,
745+
net_device.clone(),
746+
cmdline,
747+
false,
748+
secret_free,
749+
)?;
725750
}
726751
Ok(())
727752
}
@@ -731,10 +756,19 @@ fn attach_unixsock_vsock_device(
731756
cmdline: &mut LoaderKernelCmdline,
732757
unix_vsock: &Arc<Mutex<Vsock<VsockUnixBackend>>>,
733758
event_manager: &mut EventManager,
759+
secret_free: bool,
734760
) -> Result<(), MmioError> {
735761
let id = String::from(unix_vsock.lock().expect("Poisoned lock").id());
736762
// The device mutex mustn't be locked here otherwise it will deadlock.
737-
attach_virtio_device(event_manager, vmm, id, unix_vsock.clone(), cmdline, false)
763+
attach_virtio_device(
764+
event_manager,
765+
vmm,
766+
id,
767+
unix_vsock.clone(),
768+
cmdline,
769+
false,
770+
secret_free,
771+
)
738772
}
739773

740774
fn attach_balloon_device(
@@ -745,7 +779,15 @@ fn attach_balloon_device(
745779
) -> Result<(), MmioError> {
746780
let id = String::from(balloon.lock().expect("Poisoned lock").id());
747781
// The device mutex mustn't be locked here otherwise it will deadlock.
748-
attach_virtio_device(event_manager, vmm, id, balloon.clone(), cmdline, false)
782+
attach_virtio_device(
783+
event_manager,
784+
vmm,
785+
id,
786+
balloon.clone(),
787+
cmdline,
788+
false,
789+
false,
790+
)
749791
}
750792

751793
// Adds `O_NONBLOCK` to the stdout flags.
@@ -921,6 +963,7 @@ pub(crate) mod tests {
921963
cmdline,
922964
block_dev_configs.devices.iter(),
923965
event_manager,
966+
false,
924967
)
925968
.unwrap();
926969
block_files
@@ -935,7 +978,7 @@ pub(crate) mod tests {
935978
let mut net_builder = NetBuilder::new();
936979
net_builder.build(net_config).unwrap();
937980

938-
let res = attach_net_devices(vmm, cmdline, net_builder.iter(), event_manager);
981+
let res = attach_net_devices(vmm, cmdline, net_builder.iter(), event_manager, false);
939982
res.unwrap();
940983
}
941984

@@ -956,7 +999,7 @@ pub(crate) mod tests {
956999
Arc::new(Mutex::new(mmds)),
9571000
);
9581001

959-
attach_net_devices(vmm, cmdline, net_builder.iter(), event_manager).unwrap();
1002+
attach_net_devices(vmm, cmdline, net_builder.iter(), event_manager, false).unwrap();
9601003
}
9611004

9621005
pub(crate) fn insert_vsock_device(
@@ -969,7 +1012,7 @@ pub(crate) mod tests {
9691012
let vsock = VsockBuilder::create_unixsock_vsock(vsock_config).unwrap();
9701013
let vsock = Arc::new(Mutex::new(vsock));
9711014

972-
attach_unixsock_vsock_device(vmm, cmdline, &vsock, event_manager).unwrap();
1015+
attach_unixsock_vsock_device(vmm, cmdline, &vsock, event_manager, false).unwrap();
9731016

9741017
assert!(
9751018
vmm.mmio_device_manager

src/vmm/src/devices/virtio/block/vhost_user/device.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
296296

297297
fn force_userspace_bounce_buffers(&mut self) {
298298
// Nothing Firecracker can do about this, the backend would need to do the bouncing
299+
panic!("vhost-user-blk is incompatible with userspace bounce buffers")
299300
}
300301

301302
fn userspace_bounce_buffers(&self) -> bool {

src/vmm/src/devices/virtio/block/virtio/device.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,9 @@ impl VirtioDevice for VirtioBlock {
580580

581581
fn force_userspace_bounce_buffers(&mut self) {
582582
match self.disk.file_engine {
583-
FileEngine::Async(_) => panic!("No idea how this is supposed to work for io_uring"),
583+
FileEngine::Async(_) => {
584+
panic!("async engine is incompatible with userspace bounce buffers")
585+
}
584586
FileEngine::Sync(ref mut engine) => engine.start_bouncing(),
585587
}
586588
}

0 commit comments

Comments
 (0)