Skip to content

Commit 8b712b8

Browse files
committed
have virtio devices offer VIRTIO_F_ACCESS_PLATFORM if swiotlb
Have all virtio devices off VIRTIO_F_PLATFORM_ACCESS, which communicates to the guest that DMA APIs should be used for allocating vrings and virtio buffers. Since we have created a dedicated swiotlb region in the geust and assigned all virtio devices to this, it means that all such allocations will go through the swiotlb region. Signed-off-by: Patrick Roy <[email protected]>
1 parent 5a1ba36 commit 8b712b8

File tree

12 files changed

+77
-7
lines changed

12 files changed

+77
-7
lines changed

src/vmm/src/builder.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,13 @@ fn attach_virtio_device<T: 'static + VirtioDevice + MutEventSubscriber + Debug>(
605605
) -> Result<(), MmioError> {
606606
event_manager.add_subscriber(device.clone());
607607

608+
// We have to enable swiotlb as part of the boot process, because the device objects
609+
// themselves are created when the corresponding PUT API calls are made, and at that
610+
// point we don't know yet whether swiotlb should be enabled or not.
611+
if vmm.vm.has_swiotlb() {
612+
device.lock().unwrap().force_swiotlb();
613+
}
614+
608615
// The device mutex mustn't be locked here otherwise it will deadlock.
609616
let device = MmioTransport::new(vmm.vm.io_memory().clone(), device, is_vhost_user);
610617
vmm.mmio_device_manager

src/vmm/src/device_manager/mmio.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,10 @@ mod tests {
600600

601601
fn set_acked_features(&mut self, _: u64) {}
602602

603+
fn force_swiotlb(&mut self) {
604+
unimplemented!()
605+
}
606+
603607
fn device_type(&self) -> u32 {
604608
0
605609
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use super::{
2525
};
2626
use crate::devices::virtio::balloon::BalloonError;
2727
use crate::devices::virtio::device::{IrqTrigger, IrqType};
28-
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
28+
use crate::devices::virtio::generated::virtio_config::{
29+
VIRTIO_F_ACCESS_PLATFORM, VIRTIO_F_VERSION_1,
30+
};
2931
use crate::logger::IncMetric;
3032
use crate::utils::u64_to_usize;
3133
use crate::vstate::memory::{Address, ByteValued, Bytes, GuestAddress, GuestMemoryMmap};
@@ -557,6 +559,10 @@ impl VirtioDevice for Balloon {
557559
self.acked_features = acked_features;
558560
}
559561

562+
fn force_swiotlb(&mut self) {
563+
self.avail_features |= 1 << VIRTIO_F_ACCESS_PLATFORM;
564+
}
565+
560566
fn device_type(&self) -> u32 {
561567
TYPE_BALLOON
562568
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ impl VirtioDevice for Block {
148148
}
149149
}
150150

151+
fn force_swiotlb(&mut self) {
152+
match self {
153+
Block::Virtio(b) => b.force_swiotlb(),
154+
Block::VhostUser(b) => b.force_swiotlb(),
155+
}
156+
}
157+
151158
fn device_type(&self) -> u32 {
152159
TYPE_BLOCK
153160
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use super::{NUM_QUEUES, QUEUE_SIZE, VhostUserBlockError};
1616
use crate::devices::virtio::block::CacheType;
1717
use crate::devices::virtio::device::{DeviceState, IrqTrigger, IrqType, VirtioDevice};
1818
use crate::devices::virtio::generated::virtio_blk::{VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_RO};
19-
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
19+
use crate::devices::virtio::generated::virtio_config::{
20+
VIRTIO_F_ACCESS_PLATFORM, VIRTIO_F_VERSION_1,
21+
};
2022
use crate::devices::virtio::generated::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
2123
use crate::devices::virtio::queue::Queue;
2224
use crate::devices::virtio::vhost_user::{VhostUserHandleBackend, VhostUserHandleImpl};
@@ -294,6 +296,10 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
294296
self.acked_features = acked_features;
295297
}
296298

299+
fn force_swiotlb(&mut self) {
300+
self.avail_features |= 1 << VIRTIO_F_ACCESS_PLATFORM;
301+
}
302+
297303
fn device_type(&self) -> u32 {
298304
TYPE_BLOCK
299305
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ use crate::devices::virtio::device::{DeviceState, IrqTrigger, IrqType, VirtioDev
2727
use crate::devices::virtio::generated::virtio_blk::{
2828
VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_RO, VIRTIO_BLK_ID_BYTES,
2929
};
30-
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
30+
use crate::devices::virtio::generated::virtio_config::{
31+
VIRTIO_F_ACCESS_PLATFORM, VIRTIO_F_VERSION_1,
32+
};
3133
use crate::devices::virtio::generated::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
3234
use crate::devices::virtio::queue::Queue;
3335
use crate::devices::virtio::{ActivateError, TYPE_BLOCK};
@@ -578,6 +580,10 @@ impl VirtioDevice for VirtioBlock {
578580
self.acked_features = acked_features;
579581
}
580582

583+
fn force_swiotlb(&mut self) {
584+
self.avail_features |= 1 << VIRTIO_F_ACCESS_PLATFORM;
585+
}
586+
581587
fn device_type(&self) -> u32 {
582588
TYPE_BLOCK
583589
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ pub trait VirtioDevice: AsAny + Send {
102102
/// - self.avail_features() & self.acked_features() = self.get_acked_features()
103103
fn set_acked_features(&mut self, acked_features: u64);
104104

105+
/// Make the virtio device offer the VIRTIO_F_ACCESS_PLATFORM feature
106+
fn force_swiotlb(&mut self);
107+
105108
/// Check if virtio device has negotiated given feature.
106109
fn has_feature(&self, feature: u64) -> bool {
107110
(self.acked_features() & (1 << feature)) != 0
@@ -259,6 +262,10 @@ pub(crate) mod tests {
259262
todo!()
260263
}
261264

265+
fn force_swiotlb(&mut self) {
266+
unimplemented!()
267+
}
268+
262269
fn device_type(&self) -> u32 {
263270
todo!()
264271
}

src/vmm/src/devices/virtio/mmio.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,10 @@ pub(crate) mod tests {
423423
self.acked_features = acked_features;
424424
}
425425

426+
fn force_swiotlb(&mut self) {
427+
unimplemented!()
428+
}
429+
426430
fn device_type(&self) -> u32 {
427431
123
428432
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use vmm_sys_util::eventfd::EventFd;
1616

1717
use super::NET_QUEUE_MAX_SIZE;
1818
use crate::devices::virtio::device::{DeviceState, IrqTrigger, IrqType, VirtioDevice};
19-
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
19+
use crate::devices::virtio::generated::virtio_config::{
20+
VIRTIO_F_ACCESS_PLATFORM, VIRTIO_F_VERSION_1,
21+
};
2022
use crate::devices::virtio::generated::virtio_net::{
2123
VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
2224
VIRTIO_NET_F_GUEST_UFO, VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_TSO6, VIRTIO_NET_F_HOST_UFO,
@@ -946,6 +948,10 @@ impl VirtioDevice for Net {
946948
self.acked_features = acked_features;
947949
}
948950

951+
fn force_swiotlb(&mut self) {
952+
self.avail_features |= 1 << VIRTIO_F_ACCESS_PLATFORM;
953+
}
954+
949955
fn device_type(&self) -> u32 {
950956
TYPE_NET
951957
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use super::metrics::METRICS;
1313
use super::{RNG_NUM_QUEUES, RNG_QUEUE};
1414
use crate::devices::DeviceError;
1515
use crate::devices::virtio::device::{DeviceState, IrqTrigger, IrqType, VirtioDevice};
16-
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
16+
use crate::devices::virtio::generated::virtio_config::{
17+
VIRTIO_F_ACCESS_PLATFORM, VIRTIO_F_VERSION_1,
18+
};
1719
use crate::devices::virtio::iov_deque::IovDequeError;
1820
use crate::devices::virtio::iovec::IoVecBufferMut;
1921
use crate::devices::virtio::queue::{FIRECRACKER_MAX_QUEUE_SIZE, Queue};
@@ -303,6 +305,10 @@ impl VirtioDevice for Entropy {
303305
self.device_state = DeviceState::Activated(mem);
304306
Ok(())
305307
}
308+
309+
fn force_swiotlb(&mut self) {
310+
self.avail_features |= 1 << VIRTIO_F_ACCESS_PLATFORM;
311+
}
306312
}
307313

308314
#[cfg(test)]

0 commit comments

Comments
 (0)