Skip to content

Commit 4980965

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 453ae4c commit 4980965

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
@@ -296,16 +296,24 @@ pub fn build_microvm_for_boot(
296296
&mut boot_cmdline,
297297
vm_resources.block.devices.iter(),
298298
event_manager,
299+
vm_resources.machine_config.secret_free,
299300
)?;
300301
attach_net_devices(
301302
&mut vmm,
302303
&mut boot_cmdline,
303304
vm_resources.net_builder.iter(),
304305
event_manager,
306+
vm_resources.machine_config.secret_free,
305307
)?;
306308

307309
if let Some(unix_vsock) = vm_resources.vsock.get() {
308-
attach_unixsock_vsock_device(&mut vmm, &mut boot_cmdline, unix_vsock, event_manager)?;
310+
attach_unixsock_vsock_device(
311+
&mut vmm,
312+
&mut boot_cmdline,
313+
unix_vsock,
314+
event_manager,
315+
vm_resources.machine_config.secret_free,
316+
)?;
309317
}
310318

311319
if let Some(entropy) = vm_resources.entropy.get() {
@@ -677,9 +685,14 @@ fn attach_virtio_device<T: 'static + VirtioDevice + MutEventSubscriber + Debug>(
677685
device: Arc<Mutex<T>>,
678686
cmdline: &mut LoaderKernelCmdline,
679687
is_vhost_user: bool,
688+
secret_free: bool,
680689
) -> Result<(), MmioError> {
681690
event_manager.add_subscriber(device.clone());
682691

692+
if secret_free {
693+
device.lock().unwrap().force_userspace_bounce_buffers();
694+
}
695+
683696
// The device mutex mustn't be locked here otherwise it will deadlock.
684697
let device = MmioTransport::new(vmm.vm.guest_memory().clone(), device, is_vhost_user);
685698
vmm.mmio_device_manager
@@ -735,6 +748,7 @@ fn attach_entropy_device(
735748
entropy_device.clone(),
736749
cmdline,
737750
false,
751+
false,
738752
)
739753
}
740754

@@ -743,6 +757,7 @@ fn attach_block_devices<'a, I: Iterator<Item = &'a Arc<Mutex<Block>>> + Debug>(
743757
cmdline: &mut LoaderKernelCmdline,
744758
blocks: I,
745759
event_manager: &mut EventManager,
760+
secret_free: bool,
746761
) -> Result<(), StartMicrovmError> {
747762
for block in blocks {
748763
let (id, is_vhost_user) = {
@@ -767,6 +782,7 @@ fn attach_block_devices<'a, I: Iterator<Item = &'a Arc<Mutex<Block>>> + Debug>(
767782
block.clone(),
768783
cmdline,
769784
is_vhost_user,
785+
secret_free,
770786
)?;
771787
}
772788
Ok(())
@@ -777,11 +793,20 @@ fn attach_net_devices<'a, I: Iterator<Item = &'a Arc<Mutex<Net>>> + Debug>(
777793
cmdline: &mut LoaderKernelCmdline,
778794
net_devices: I,
779795
event_manager: &mut EventManager,
796+
secret_free: bool,
780797
) -> Result<(), StartMicrovmError> {
781798
for net_device in net_devices {
782799
let id = net_device.lock().expect("Poisoned lock").id().clone();
783800
// The device mutex mustn't be locked here otherwise it will deadlock.
784-
attach_virtio_device(event_manager, vmm, id, net_device.clone(), cmdline, false)?;
801+
attach_virtio_device(
802+
event_manager,
803+
vmm,
804+
id,
805+
net_device.clone(),
806+
cmdline,
807+
false,
808+
secret_free,
809+
)?;
785810
}
786811
Ok(())
787812
}
@@ -791,10 +816,19 @@ fn attach_unixsock_vsock_device(
791816
cmdline: &mut LoaderKernelCmdline,
792817
unix_vsock: &Arc<Mutex<Vsock<VsockUnixBackend>>>,
793818
event_manager: &mut EventManager,
819+
secret_free: bool,
794820
) -> Result<(), MmioError> {
795821
let id = String::from(unix_vsock.lock().expect("Poisoned lock").id());
796822
// The device mutex mustn't be locked here otherwise it will deadlock.
797-
attach_virtio_device(event_manager, vmm, id, unix_vsock.clone(), cmdline, false)
823+
attach_virtio_device(
824+
event_manager,
825+
vmm,
826+
id,
827+
unix_vsock.clone(),
828+
cmdline,
829+
false,
830+
secret_free,
831+
)
798832
}
799833

800834
fn attach_balloon_device(
@@ -805,7 +839,15 @@ fn attach_balloon_device(
805839
) -> Result<(), MmioError> {
806840
let id = String::from(balloon.lock().expect("Poisoned lock").id());
807841
// The device mutex mustn't be locked here otherwise it will deadlock.
808-
attach_virtio_device(event_manager, vmm, id, balloon.clone(), cmdline, false)
842+
attach_virtio_device(
843+
event_manager,
844+
vmm,
845+
id,
846+
balloon.clone(),
847+
cmdline,
848+
false,
849+
false,
850+
)
809851
}
810852

811853
// Adds `O_NONBLOCK` to the stdout flags.
@@ -981,6 +1023,7 @@ pub(crate) mod tests {
9811023
cmdline,
9821024
block_dev_configs.devices.iter(),
9831025
event_manager,
1026+
false,
9841027
)
9851028
.unwrap();
9861029
block_files
@@ -995,7 +1038,7 @@ pub(crate) mod tests {
9951038
let mut net_builder = NetBuilder::new();
9961039
net_builder.build(net_config).unwrap();
9971040

998-
let res = attach_net_devices(vmm, cmdline, net_builder.iter(), event_manager);
1041+
let res = attach_net_devices(vmm, cmdline, net_builder.iter(), event_manager, false);
9991042
res.unwrap();
10001043
}
10011044

@@ -1016,7 +1059,7 @@ pub(crate) mod tests {
10161059
Arc::new(Mutex::new(mmds)),
10171060
);
10181061

1019-
attach_net_devices(vmm, cmdline, net_builder.iter(), event_manager).unwrap();
1062+
attach_net_devices(vmm, cmdline, net_builder.iter(), event_manager, false).unwrap();
10201063
}
10211064

10221065
pub(crate) fn insert_vsock_device(
@@ -1029,7 +1072,7 @@ pub(crate) mod tests {
10291072
let vsock = VsockBuilder::create_unixsock_vsock(vsock_config).unwrap();
10301073
let vsock = Arc::new(Mutex::new(vsock));
10311074

1032-
attach_unixsock_vsock_device(vmm, cmdline, &vsock, event_manager).unwrap();
1075+
attach_unixsock_vsock_device(vmm, cmdline, &vsock, event_manager, false).unwrap();
10331076

10341077
assert!(
10351078
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)