Skip to content

Commit 482b644

Browse files
committed
perf(virtq): use weaker memory barriers
1 parent 8378af1 commit 482b644

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/drivers/virtio/virtqueue/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use core::mem::MaybeUninit;
1919
use core::{mem, ptr};
2020

2121
use enum_dispatch::enum_dispatch;
22+
use mem_barrier::{BarrierKind, BarrierType};
2223
use smallvec::SmallVec;
2324
use virtio::{le32, le64, pvirtq, virtq};
2425

@@ -27,6 +28,17 @@ use crate::drivers::virtio::virtqueue::packed::PackedVq;
2728
use crate::drivers::virtio::virtqueue::split::SplitVq;
2829
use crate::mm::device_alloc::DeviceAlloc;
2930

31+
#[inline]
32+
pub fn virtio_mem_barrier(ty: BarrierType, order_platform: bool) {
33+
let class = if order_platform {
34+
BarrierKind::Smp
35+
} else {
36+
BarrierKind::Dma
37+
};
38+
39+
mem_barrier::mem_barrier(class, ty);
40+
}
41+
3042
// Public interface of Virtq
3143

3244
/// The Virtq trait unifies access to the two different Virtqueue types

src/drivers/virtio/virtqueue/split.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use alloc::vec::Vec;
1212
use core::cell::UnsafeCell;
1313
use core::mem::{self, MaybeUninit};
1414

15+
use mem_barrier::BarrierType;
1516
#[cfg(not(feature = "pci"))]
1617
use virtio::mmio::NotificationData;
1718
#[cfg(feature = "pci")]
@@ -25,7 +26,6 @@ use super::super::transport::pci::{ComCfg, NotifCfg, NotifCtrl};
2526
use super::error::VirtqError;
2627
use super::index_alloc::IndexAlloc;
2728
use super::{AvailBufferToken, BufferType, TransferToken, UsedBufferToken, Virtq, VirtqPrivate};
28-
use crate::arch::memory_barrier;
2929
use crate::mm::device_alloc::DeviceAlloc;
3030

3131
struct DescrRing {
@@ -36,6 +36,7 @@ struct DescrRing {
3636
descr_table_cell: Box<UnsafeCell<[MaybeUninit<virtq::Desc>]>, DeviceAlloc>,
3737
avail_ring_cell: Box<UnsafeCell<virtq::Avail>, DeviceAlloc>,
3838
used_ring_cell: Box<UnsafeCell<virtq::Used>, DeviceAlloc>,
39+
order_platform: bool,
3940
}
4041

4142
impl DescrRing {
@@ -88,14 +89,15 @@ impl DescrRing {
8889
self.avail_ring_mut().ring_mut(true)[idx as usize % len] =
8990
le16::from_ne(index.try_into().unwrap());
9091

91-
memory_barrier();
92+
super::virtio_mem_barrier(BarrierType::Write, self.order_platform);
9293
let next_idx = idx.wrapping_add(1);
9394
self.avail_ring_mut().idx = next_idx.into();
9495

9596
Ok(next_idx)
9697
}
9798

9899
fn try_recv(&mut self) -> Result<UsedBufferToken, VirtqError> {
100+
super::virtio_mem_barrier(BarrierType::Read, self.order_platform);
99101
if self.read_idx == self.used_ring().idx.to_ne() {
100102
return Err(VirtqError::NoNewUsed);
101103
}
@@ -123,7 +125,6 @@ impl DescrRing {
123125
}
124126
}
125127

126-
memory_barrier();
127128
self.read_idx = self.read_idx.wrapping_add(1);
128129
Ok(UsedBufferToken::from_avail_buffer_token(
129130
tkn.buff_tkn,
@@ -291,6 +292,8 @@ impl SplitVq {
291292
vq_handler.set_drv_ctrl_addr(DeviceAlloc.phys_addr_from(avail_ring_cell.as_mut()));
292293
vq_handler.set_dev_ctrl_addr(DeviceAlloc.phys_addr_from(used_ring_cell.as_mut()));
293294

295+
let order_platform = features.contains(virtio::F::ORDER_PLATFORM);
296+
294297
let descr_ring = DescrRing {
295298
read_idx: 0,
296299
token_ring: core::iter::repeat_with(|| None)
@@ -302,6 +305,7 @@ impl SplitVq {
302305
descr_table_cell,
303306
avail_ring_cell,
304307
used_ring_cell,
308+
order_platform,
305309
};
306310

307311
let mut notif_ctrl = NotifCtrl::new(notif_cfg.notification_location(&mut vq_handler));

0 commit comments

Comments
 (0)