Skip to content

Commit

Permalink
underhill_mem: register memory with proper private/shared bit (micros…
Browse files Browse the repository at this point in the history
…oft#257)

At some point, we stopped passing the shared bit down when registering
kernel memory. Fix this.

This fixes kernel-mode DMA from TDX VMs.

Note that there are still problems with the overall scenario: the kernel
will double buffer through the swiotlb even when the target pages are
already shared, and swiotlb is sized by openhcl_boot such that there's
not enough memory for all the IO that we may have in flight
concurrently.
  • Loading branch information
jstarks authored Nov 7, 2024
1 parent 53ad17d commit 0cbadc3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
9 changes: 7 additions & 2 deletions openhcl/hcl/src/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,9 +651,14 @@ pub struct MshvVtl {

impl MshvVtl {
/// Adds the VTL0 memory as a ZONE_DEVICE memory (I/O) to support DMA from the guest.
pub fn add_vtl0_memory(&self, mem_range: MemoryRange) -> Result<(), Error> {
pub fn add_vtl0_memory(&self, mem_range: MemoryRange, shared: bool) -> Result<(), Error> {
let flags = if shared {
MshvVtlLow::SHARED_MEMORY_FLAG / HV_PAGE_SIZE
} else {
0
};
let ram_disposition = protocol::hcl_pfn_range_t {
start_pfn: mem_range.start_4k_gpn(),
start_pfn: mem_range.start_4k_gpn() | flags,
last_pfn: mem_range.end_4k_gpn(),
};

Expand Down
4 changes: 3 additions & 1 deletion openhcl/underhill_mem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ mod mapping {
MshvVtlWithPolicy {
mshv_vtl,
ignore_registration_failure: self.ignore_registration_failure,
shared: self.shared,
},
))
} else {
Expand All @@ -308,14 +309,15 @@ mod mapping {
struct MshvVtlWithPolicy {
mshv_vtl: MshvVtl,
ignore_registration_failure: bool,
shared: bool,
}

impl crate::registrar::RegisterMemory for MshvVtlWithPolicy {
fn register_range(
&self,
range: MemoryRange,
) -> Result<(), impl 'static + std::error::Error> {
match self.mshv_vtl.add_vtl0_memory(range) {
match self.mshv_vtl.add_vtl0_memory(range, self.shared) {
Ok(()) => Ok(()),
// TODO: remove this once the kernel driver tracks registration
Err(err) if self.ignore_registration_failure => {
Expand Down
7 changes: 0 additions & 7 deletions openhcl/underhill_mem/src/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
//! initial registration for a chunk small. We track whether a given chunk has
//! been registered via a small bitmap.
use hcl::ioctl::MshvVtl;
use inspect::Inspect;
use memory_range::overlapping_ranges;
use memory_range::MemoryRange;
Expand Down Expand Up @@ -101,12 +100,6 @@ pub trait RegisterMemory {
fn register_range(&self, range: MemoryRange) -> Result<(), impl 'static + std::error::Error>;
}

impl RegisterMemory for MshvVtl {
fn register_range(&self, range: MemoryRange) -> Result<(), impl 'static + std::error::Error> {
self.add_vtl0_memory(range)
}
}

impl<T: Fn(MemoryRange) -> Result<(), E>, E: 'static + std::error::Error> RegisterMemory for T {
fn register_range(&self, range: MemoryRange) -> Result<(), impl 'static + std::error::Error> {
(self)(range)
Expand Down

0 comments on commit 0cbadc3

Please sign in to comment.