Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions eos/src/ioring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,23 @@ impl ReadRing {
fn flush(&mut self) -> io::Result<()> {
if self.queued != 0 {
let mut submitted = 0;
// A submission failure leaves all entries queued. Drop abandons those
// entries instead of allowing them to execute after buffer reuse.
let begin = Instant::now();
let status = unsafe { SubmitIoRing(self.handle, 0, 0, &mut submitted) };
self.timings.submit += begin.elapsed().as_nanos() as u64;
// Account for every entry the kernel accepted before propagating a
// submission error so Drop drains in-flight DMA before freeing buffers.
let reported = submitted as usize;
let overflowed = reported > self.queued;
let accepted = reported.min(self.queued);
self.queued -= accepted;
self.in_flight += accepted;
check(status)?;
self.queued -= submitted as usize;
self.in_flight += submitted as usize;
if overflowed {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Windows I/O ring reported more submissions than queued",
));
}
if self.queued != 0 {
return Err(io::Error::other("incomplete Windows I/O ring submission"));
}
Expand Down
133 changes: 107 additions & 26 deletions heliopause/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ struct Arena {
granularity: usize,
gpudirect: bool,
host_base: u64,
host_extent: u64,
free: Vec<(u64, usize)>,
mappings: BTreeMap<u64, usize>,
retained: RetainedBacking,
Expand Down Expand Up @@ -263,9 +264,21 @@ fn failure(error: impl ToString) {
});
}

/// The host base for `world` device windows of `extent` bytes from `base`,
/// rejecting empty or overflowing windows and windows that overlap the host ranges.
fn window_host_base(base: u64, extent: u64, world: u32) -> Result<u64> {
/// Computes `(host_base, host_extent)` for `world` ranks given a device window
/// `[base, base + world * extent)` and an optional WDDM GPU virtual address width.
///
/// Under WDDM, mapped host allocations also reserve fixed GPU virtual addresses,
/// so the host range must fit within `1 << bits`. When `HOST_BASE` exceeds a
/// narrow adapter's virtual address space, the host range is placed immediately
/// after the device windows inside `1 << bits` (aligned to `granule`) instead of
/// handing back an unreservable address that would hang `cuMemAddressReserve`.
fn window_host_layout(
base: u64,
extent: u64,
world: u32,
granule: u64,
bits: Option<u32>,
) -> Result<(u64, u64)> {
let span = u64::from(world)
.checked_mul(extent)
.filter(|span| base != 0 && extent != 0 && base.checked_add(*span).is_some())
Expand All @@ -275,18 +288,72 @@ fn window_host_base(base: u64, extent: u64, world: u32) -> Result<u64> {
"invalid native core device window {base:#x}+{extent:#x}"
))
})?;
let host_base = if cfg!(target_os = "windows") {
let device_end = base + span;
for peer in 0..world {
let start = base + u64::from(peer) * extent;
address_space::check_fits("native core device window", start, extent, bits)
.map_err(invalid)?;
}
let candidate_base = if cfg!(target_os = "windows") {
HOST_BASE
} else {
host_base(base, world, extent)
};
let host_end = host_base + u64::from(world) * EXTENT as u64;
if base < host_end && host_base < base + span {
return Err(invalid(
"core host reservations would overlap device reservations",
));
let default_extent = EXTENT as u64;
let default_span = u64::from(world).checked_mul(default_extent);
let default_end = default_span.and_then(|s| candidate_base.checked_add(s));
let default_fits_bits = default_span.is_some_and(|s| {
address_space::check_fits("native core host window", candidate_base, s, bits).is_ok()
});
if let Some(host_end) = default_end
&& default_fits_bits
{
if base < host_end && candidate_base < device_end {
return Err(invalid(
"core host reservations would overlap device reservations",
));
}
return Ok((candidate_base, default_extent));
}
if let Some(bits) = bits {
let limit = 1u64 << bits;
let aligned_start = if granule > 1 {
device_end
.checked_add(granule - 1)
.map(|n| n & !(granule - 1))
} else {
Some(device_end)
};
if let Some(host_start) = aligned_start
&& host_start < limit
{
let remaining = limit - host_start;
let raw_per_rank = (remaining / u64::from(world)).min(default_extent);
let per_rank = if granule > 1 {
raw_per_rank & !(granule - 1)
} else {
raw_per_rank
};
if per_rank > 0 {
for peer in 0..world {
let start = host_start + u64::from(peer) * per_rank;
address_space::check_fits("native core host window", start, per_rank, Some(bits))
.map_err(invalid)?;
}
return Ok((host_start, per_rank));
}
}
address_space::check_fits(
"native core host window",
candidate_base,
u64::from(world).saturating_mul(default_extent),
Some(bits),
)
.map_err(invalid)?;
}
Ok(host_base)
Err(invalid(
"core host reservations would overlap or overflow the virtual address space",
))
}

#[unsafe(no_mangle)]
Expand Down Expand Up @@ -315,11 +382,10 @@ extern "C" fn flare_core_initialize_window(
if world == 0 || rank >= world || device < 0 {
return Err(invalid("invalid core rank or device"));
}
let host_base = window_host_base(base, extent, world)?;
let extent_bytes = extent as usize;
let driver = Driver::load()?;
let (mut address, mut granularity, mut gpudirect, mut integrated) = (0, 0, 0, 0);
unsafe {
let (host_base, host_extent) = unsafe {
driver.check(
"cuDeviceGetAttribute(INTEGRATED)",
(driver.device_attribute)(&mut integrated, 18, device),
Expand Down Expand Up @@ -348,11 +414,8 @@ extern "C" fn flare_core_initialize_window(
)));
}
let bits = address_space::virtual_address_bits(device);
for peer in 0..world {
let start = base + u64::from(peer) * extent;
address_space::check_fits("native core device window", start, extent, bits)
.map_err(invalid)?;
}
let (host_base, host_extent) =
window_host_layout(base, extent, world, granule, bits)?;
let mut reserved = Vec::new();
for peer in 0..world {
let start = base + u64::from(peer) * extent;
Expand All @@ -371,13 +434,15 @@ extern "C" fn flare_core_initialize_window(
}
reserved.push(address);
}
}
(host_base, host_extent)
};
*arena = Some(Arena {
driver,
device,
granularity,
gpudirect: gpudirect != 0 && integrated == 0,
host_base: host_base + u64::from(rank) * EXTENT as u64,
host_base: host_base + u64::from(rank) * host_extent,
host_extent,
free: vec![(base + u64::from(rank) * extent, extent_bytes / 2)],
mappings: BTreeMap::new(),
retained: RetainedBacking::default(),
Expand Down Expand Up @@ -650,7 +715,8 @@ extern "C" fn flare_core_snapshot() -> *mut c_char {
return null_mut();
};
let value = serde_json::json!({
"granularity": a.granularity, "mappings": a.mappings, "host_base": a.host_base,
"granularity": a.granularity, "mappings": a.mappings,
"host_base": a.host_base, "host_extent": a.host_extent,
"retained_reference_bytes": a.retained.reference_bytes,
"retained_handles": a.retained.handles.len(),
"retained_reusable_handles": a.retained_mappings.len(),
Expand Down Expand Up @@ -1151,6 +1217,7 @@ mod tests {
granularity: 4096,
gpudirect: false,
host_base: 0,
host_extent: 0,
free: Vec::new(),
mappings: BTreeMap::new(),
retained,
Expand Down Expand Up @@ -1229,6 +1296,7 @@ mod tests {
granularity: 4096,
gpudirect: false,
host_base: 0,
host_extent: 0,
free: Vec::new(),
mappings: BTreeMap::new(),
retained,
Expand Down Expand Up @@ -1272,6 +1340,7 @@ mod tests {
granularity: 4096,
gpudirect: false,
host_base: 0,
host_extent: 0,
free: Vec::new(),
mappings: BTreeMap::from([(0x10000, 4096)]),
retained: RetainedBacking::default(),
Expand Down Expand Up @@ -1335,26 +1404,38 @@ mod tests {
#[test]
fn explicit_device_windows_are_validated() {
assert_eq!(
window_host_base(BASE, EXTENT as u64, 1).unwrap(),
window_host_layout(BASE, EXTENT as u64, 1, 1, None).unwrap().0,
if cfg!(target_os = "windows") {
HOST_BASE
} else {
host_base(BASE, 1, EXTENT as u64)
}
);
// A 64 GiB window at 256 GiB fits below the host ranges.
assert!(window_host_base(0x40_0000_0000, 64 << 30, 2).is_ok());
assert!(window_host_base(0, 1 << 30, 1).is_err());
assert!(window_host_base(1 << 40, 0, 1).is_err());
assert!(window_host_base(u64::MAX - (1 << 30), 1 << 30, 2).is_err());
assert!(window_host_layout(0x40_0000_0000, 64 << 30, 2, 1, None).is_ok());
assert!(window_host_layout(0, 1 << 30, 1, 1, None).is_err());
assert!(window_host_layout(1 << 40, 0, 1, 1, None).is_err());
assert!(window_host_layout(u64::MAX - (1 << 30), 1 << 30, 2, 1, None).is_err());
// A window reaching the host range is rejected. Linux moves its host range to
// 32 TiB when device windows reach 12 TiB, so only one reaching 32 TiB overlaps it.
let host = if cfg!(target_os = "windows") {
HOST_BASE
} else {
0x200000000000
};
assert!(window_host_base(host - (1 << 30), 2 << 30, 1).is_err());
assert!(window_host_layout(host - (1 << 30), 2 << 30, 1, 1, None).is_err());

// Narrow WDDM address space (40-bit = 1 TiB): HOST_BASE (12 TiB) does not fit,
// so host ranges are placed after the device windows inside 1 << 40.
let (narrow_host, narrow_extent) =
window_host_layout(0x40_0000_0000, 64 << 30, 2, 2 << 20, Some(40)).unwrap();
assert_eq!(narrow_host, 0x40_0000_0000 + (128 << 30));
assert!(narrow_extent > 0);
assert!(narrow_host + 2 * narrow_extent <= (1u64 << 40));

// If device windows fill the entire WDDM address space, reject cleanly instead of
// returning an unreservable host base.
assert!(window_host_layout(0x80_0000_0000, 512 << 30, 1, 2 << 20, Some(40)).is_err());
}

#[test]
Expand Down
Loading
Loading