mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
- Rebased #222 (HMM ReadDuplicated OR + Host slot recycling preserving #220's NvStatus(0x1a), sync_timeline waiter overflow, CBC/NVLS/SRIOV watermark reclaim). - Rebased #239 (modeset_ffi refcount/arch/lock fixes, UVM FFI null/bounds/checked- arithmetic adapted onto #294's visit_video_range split, raw-pointer CSL AES-GCM avoiding aliasing UB). Verified with 32/32 offline/build/QEMU/gate checks (1,093 unit tests). Co-authored-by: morebrackets <morebrackets@users.noreply.github.com>
This commit is contained in:
@ -41,6 +41,9 @@ pub(crate) unsafe fn configure_live_display(boot0: u32, opt_display_fuse: u32) {
|
||||
use nv_rs_core::gpu::chip_arch::ArchFamily;
|
||||
let arch_id = (boot0 >> 20) & 0x1ff;
|
||||
let family = match arch_id {
|
||||
0x110..=0x11f => ArchFamily::Maxwell1,
|
||||
0x120..=0x12f => ArchFamily::Maxwell2,
|
||||
0x130..=0x13f => ArchFamily::Pascal,
|
||||
0x140..=0x14f => ArchFamily::Volta,
|
||||
0x160..=0x16f => ArchFamily::Turing,
|
||||
0x170..=0x17f => ArchFamily::Ampere,
|
||||
@ -48,6 +51,7 @@ pub(crate) unsafe fn configure_live_display(boot0: u32, opt_display_fuse: u32) {
|
||||
0x180..=0x18f => ArchFamily::Hopper,
|
||||
0x1a0..=0x1af => ArchFamily::Blackwell1,
|
||||
0x1b0..=0x1bf => ArchFamily::Blackwell2,
|
||||
0x1c0..=0x1cf => ArchFamily::Rubin,
|
||||
_ => ArchFamily::Volta,
|
||||
};
|
||||
// Standard 4-head NVDisplay configuration (Titan V / Quadro GV100 / Turing-Blackwell desktop).
|
||||
@ -324,10 +328,16 @@ pub unsafe extern "C" fn nv_rs_modeset_open_gpu(gpu_id: u32, _sp: *mut core::ffi
|
||||
if rc != 0 {
|
||||
return rc;
|
||||
}
|
||||
let _guard = LockGuard::acquire();
|
||||
match reg_mut().open_gpu(gpu_id) {
|
||||
let open_res = {
|
||||
let _guard = LockGuard::acquire();
|
||||
reg_mut().open_gpu(gpu_id)
|
||||
};
|
||||
match open_res {
|
||||
Ok(_) => 0,
|
||||
Err(errno) => errno,
|
||||
Err(errno) => {
|
||||
nv_rs_rmapi_kernel_close_gpu(gpu_id);
|
||||
errno
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -335,11 +345,13 @@ pub unsafe extern "C" fn nv_rs_modeset_open_gpu(gpu_id: u32, _sp: *mut core::ffi
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn nv_rs_modeset_close_gpu(gpu_id: u32, _sp: *mut core::ffi::c_void) {
|
||||
unsafe {
|
||||
{
|
||||
let was_open = {
|
||||
let _guard = LockGuard::acquire();
|
||||
let _ = reg_mut().close_gpu(gpu_id);
|
||||
reg_mut().close_gpu(gpu_id).is_ok()
|
||||
};
|
||||
if was_open {
|
||||
nv_rs_rmapi_kernel_close_gpu(gpu_id);
|
||||
}
|
||||
nv_rs_rmapi_kernel_close_gpu(gpu_id);
|
||||
}
|
||||
}
|
||||
|
||||
@ -455,6 +467,7 @@ pub unsafe extern "C" fn nv_rs_modeset_get_rm_ops(rm_ops: *mut NvModesetRmOps) -
|
||||
if ver_ptr.is_null() {
|
||||
return NvStatus(0x1f).0;
|
||||
}
|
||||
let _guard = LockGuard::acquire();
|
||||
let mut ver_buf = [0u8; 64];
|
||||
let ver_len = nv_rs_core::rmapi::client::copy_driver_version(&mut ver_buf);
|
||||
let expected = if ver_len > 0 {
|
||||
|
||||
@ -399,15 +399,22 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_memory_alloc_sys(
|
||||
if gpu_va_out.is_null() || alloc_info.is_null() {
|
||||
return NvStatus::INVALID_ARGUMENT.0;
|
||||
}
|
||||
if page_count > 0 && page_phys_list.is_null() {
|
||||
return NvStatus::INVALID_ARGUMENT.0;
|
||||
}
|
||||
let page_count_usize = match usize::try_from(page_count) {
|
||||
Ok(n) if n <= (isize::MAX as usize) / core::mem::size_of::<u64>() => n,
|
||||
_ => return NvStatus::INVALID_ARGUMENT.0,
|
||||
};
|
||||
let _guard = LockGuard::acquire();
|
||||
let iface = iface_mut();
|
||||
match iface.memory_alloc_sys(va_space, length, &mut *alloc_info) {
|
||||
Ok(va) => {
|
||||
if !page_phys_list.is_null() && page_count > 0 {
|
||||
if !page_phys_list.is_null() && page_count_usize > 0 {
|
||||
if let Some((gpu_id, _bar0, pde3, pde0)) = iface.vas_hw_info(va_space) {
|
||||
if gpu_id != 0 && pde3 != 0 && pde0 != 0 {
|
||||
let pages =
|
||||
core::slice::from_raw_parts(page_phys_list, page_count as usize);
|
||||
core::slice::from_raw_parts(page_phys_list, page_count_usize);
|
||||
let _ =
|
||||
crate::rmapi_node::uvm_hw_map_pages(gpu_id, pde3, pde0, va, pages);
|
||||
}
|
||||
@ -1198,6 +1205,9 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_bind_channel_resources(
|
||||
count: u32,
|
||||
) -> u32 {
|
||||
unsafe {
|
||||
if count > 0 && (resource_ids.is_null() || resource_vas.is_null()) {
|
||||
return NvStatus::INVALID_ARGUMENT.0;
|
||||
}
|
||||
let _guard = LockGuard::acquire();
|
||||
let iface = iface_mut();
|
||||
let st = iface.bind_channel_resources(retained_channel, count);
|
||||
@ -1475,9 +1485,17 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_get_external_alloc_ptes(
|
||||
use nv_rs_core::uvm::mmu_v2;
|
||||
let vas_gpu_id = iface.vas_hw_info(va_space).map_or(0, |(gid, _, _, _)| gid);
|
||||
if let Some((_dev, user_client, user_obj)) = iface.duped_handle_info(duped_handle) {
|
||||
if let Some((is_video, mem_gpu_id, token_or_vram, _total_sz, obj_kind)) =
|
||||
if let Some((is_video, mem_gpu_id, token_or_vram, total_sz, obj_kind)) =
|
||||
crate::rmapi_node::lookup_memory(user_client, user_obj)
|
||||
{
|
||||
if total_sz > 0 {
|
||||
let Some(req_end) = offset.checked_add(size) else {
|
||||
return NvStatus::INVALID_ARGUMENT.0;
|
||||
};
|
||||
if req_end > total_sz {
|
||||
return NvStatus::INVALID_ARGUMENT.0;
|
||||
}
|
||||
}
|
||||
let is_peer_vram =
|
||||
is_video && vas_gpu_id != 0 && mem_gpu_id != 0 && vas_gpu_id != mem_gpu_id;
|
||||
let peer_id = if is_peer_vram {
|
||||
@ -1520,7 +1538,9 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_get_external_alloc_ptes(
|
||||
return NvStatus::INVALID_ARGUMENT.0;
|
||||
}
|
||||
}
|
||||
let map_bytes = to_write * page_size;
|
||||
let Some(map_bytes) = to_write.checked_mul(page_size) else {
|
||||
return NvStatus::INVALID_ARGUMENT.0;
|
||||
};
|
||||
// Preflight every segment before writing any PTE so a rejected
|
||||
// segment never leaves partially written entries in `pte_buf`.
|
||||
if let Err(st) = crate::rmapi_node::visit_video_range(
|
||||
@ -1532,6 +1552,7 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_get_external_alloc_ptes(
|
||||
if !seg_delta.is_multiple_of(page_size)
|
||||
|| !seg_len.is_multiple_of(page_size)
|
||||
|| !seg_phys.is_multiple_of(page_size)
|
||||
|| seg_phys.checked_add(seg_len).is_none()
|
||||
{
|
||||
return Err(NvStatus::INVALID_ARGUMENT);
|
||||
}
|
||||
@ -1549,7 +1570,12 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_get_external_alloc_ptes(
|
||||
let start_idx = seg_delta / page_size;
|
||||
let count = seg_len / page_size;
|
||||
for j in 0..count {
|
||||
let phys = seg_phys + j * page_size;
|
||||
let Some(phys) = j
|
||||
.checked_mul(page_size)
|
||||
.and_then(|d| seg_phys.checked_add(d))
|
||||
else {
|
||||
return Err(NvStatus::INVALID_ARGUMENT);
|
||||
};
|
||||
let pte = if is_peer_vram {
|
||||
mmu_v2::make_pte_peer_4k_kind(
|
||||
phys, peer_id, prot, cached, kind,
|
||||
@ -1566,7 +1592,11 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_get_external_alloc_ptes(
|
||||
}
|
||||
} else {
|
||||
for i in 0..to_write {
|
||||
let byte_off = offset + i * page_size;
|
||||
let Some(byte_off) =
|
||||
i.checked_mul(page_size).and_then(|d| offset.checked_add(d))
|
||||
else {
|
||||
return NvStatus::INVALID_ARGUMENT.0;
|
||||
};
|
||||
let mut p = 0u64;
|
||||
if nv_rs_rmapi_os_page_dma(
|
||||
vas_gpu_id,
|
||||
@ -1628,9 +1658,17 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_get_external_alloc_phys_addrs(
|
||||
if to_write > 0 {
|
||||
let vas_gpu_id = iface.vas_hw_info(va_space).map_or(0, |(gid, _, _, _)| gid);
|
||||
if let Some((_dev, user_client, user_obj)) = iface.duped_handle_info(duped_handle) {
|
||||
if let Some((is_video, _mem_gpu_id, token_or_vram, _total_sz, _kind)) =
|
||||
if let Some((is_video, _mem_gpu_id, token_or_vram, total_sz, _kind)) =
|
||||
crate::rmapi_node::lookup_memory(user_client, user_obj)
|
||||
{
|
||||
if total_sz > 0 {
|
||||
let Some(req_end) = offset.checked_add(size) else {
|
||||
return NvStatus::INVALID_ARGUMENT.0;
|
||||
};
|
||||
if req_end > total_sz {
|
||||
return NvStatus::INVALID_ARGUMENT.0;
|
||||
}
|
||||
}
|
||||
if is_video {
|
||||
if let Some(alloc_page_size) =
|
||||
crate::rmapi_node::lookup_video_page_size(user_client, user_obj)
|
||||
@ -1639,7 +1677,9 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_get_external_alloc_phys_addrs(
|
||||
return NvStatus::INVALID_ARGUMENT.0;
|
||||
}
|
||||
}
|
||||
let map_bytes = to_write * page_size;
|
||||
let Some(map_bytes) = to_write.checked_mul(page_size) else {
|
||||
return NvStatus::INVALID_ARGUMENT.0;
|
||||
};
|
||||
// Preflight every segment before writing any physical address so a
|
||||
// rejected segment never leaves partial output in `phys_buf`.
|
||||
if let Err(st) = crate::rmapi_node::visit_video_range(
|
||||
@ -1651,6 +1691,7 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_get_external_alloc_phys_addrs(
|
||||
if !seg_delta.is_multiple_of(page_size)
|
||||
|| !seg_len.is_multiple_of(page_size)
|
||||
|| !seg_phys.is_multiple_of(page_size)
|
||||
|| seg_phys.checked_add(seg_len).is_none()
|
||||
{
|
||||
return Err(NvStatus::INVALID_ARGUMENT);
|
||||
}
|
||||
@ -1668,8 +1709,13 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_get_external_alloc_phys_addrs(
|
||||
let start_idx = seg_delta / page_size;
|
||||
let count = seg_len / page_size;
|
||||
for j in 0..count {
|
||||
*phys_buf.add((start_idx + j) as usize) =
|
||||
seg_phys + j * page_size;
|
||||
let Some(phys) = j
|
||||
.checked_mul(page_size)
|
||||
.and_then(|d| seg_phys.checked_add(d))
|
||||
else {
|
||||
return Err(NvStatus::INVALID_ARGUMENT);
|
||||
};
|
||||
*phys_buf.add((start_idx + j) as usize) = phys;
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
@ -1678,7 +1724,11 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_get_external_alloc_phys_addrs(
|
||||
}
|
||||
} else {
|
||||
for i in 0..to_write {
|
||||
let byte_off = offset + i * page_size;
|
||||
let Some(byte_off) =
|
||||
i.checked_mul(page_size).and_then(|d| offset.checked_add(d))
|
||||
else {
|
||||
return NvStatus::INVALID_ARGUMENT.0;
|
||||
};
|
||||
let mut p = 0u64;
|
||||
if nv_rs_rmapi_os_page_dma(
|
||||
vas_gpu_id,
|
||||
@ -1739,8 +1789,13 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_get_channel_resource_ptes(
|
||||
if to_write > 0 {
|
||||
use nv_rs_core::uvm::mmu_v2;
|
||||
use nv_rs_core::uvm::va_space::Protection;
|
||||
let Some(base) = resource_descriptor.checked_add(offset) else {
|
||||
return NvStatus::INVALID_ARGUMENT.0;
|
||||
};
|
||||
for i in 0..to_write {
|
||||
let phys = resource_descriptor + offset + i * page_size;
|
||||
let Some(phys) = i.checked_mul(page_size).and_then(|d| base.checked_add(d)) else {
|
||||
return NvStatus::INVALID_ARGUMENT.0;
|
||||
};
|
||||
let pte = mmu_v2::make_pte_4k(phys, true, Protection::ReadWriteAtomic, true);
|
||||
*pte_buf.add(i as usize) = pte;
|
||||
}
|
||||
@ -2150,15 +2205,36 @@ fn ghash_update(acc: &mut [u8; 16], h: &[u8; 16], data: &[u8]) {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
unsafe fn ghash_update_ptr(acc: &mut [u8; 16], h: &[u8; 16], data: *const u8, len: usize) {
|
||||
unsafe {
|
||||
let mut off = 0usize;
|
||||
while off < len {
|
||||
let take = (len - off).min(16);
|
||||
let mut i = 0usize;
|
||||
while i < take {
|
||||
acc[i] ^= *data.add(off + i);
|
||||
i += 1;
|
||||
}
|
||||
ghash_mul(acc, h);
|
||||
off += 16;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Full NIST SP 800-38D AES-256-GCM encryption/decryption (12-byte IV, 16-byte tag),
|
||||
/// matching `_nv042420rm` / `_nv042419rm` bit-for-bit.
|
||||
///
|
||||
/// Uses raw pointers so in-place (`input == output`) calls do not create aliasing
|
||||
/// `&[u8]` / `&mut [u8]` references.
|
||||
#[inline(never)]
|
||||
unsafe fn aes256_gcm_crypt(
|
||||
key: &[u8; 32],
|
||||
nonce: &[u8; 12],
|
||||
aad: &[u8],
|
||||
input: &[u8],
|
||||
output: &mut [u8],
|
||||
input: *const u8,
|
||||
output: *mut u8,
|
||||
len: usize,
|
||||
encrypt: bool,
|
||||
tag_out: &mut [u8; 16],
|
||||
) {
|
||||
@ -2182,7 +2258,7 @@ unsafe fn aes256_gcm_crypt(
|
||||
ghash_update(&mut ghash, &h, aad);
|
||||
|
||||
if !encrypt {
|
||||
ghash_update(&mut ghash, &h, input);
|
||||
ghash_update_ptr(&mut ghash, &h, input, len);
|
||||
}
|
||||
|
||||
// GCTR starting at inc32(J0) = counter 2
|
||||
@ -2190,17 +2266,17 @@ unsafe fn aes256_gcm_crypt(
|
||||
let mut ctr_val = 2u32;
|
||||
let mut off = 0usize;
|
||||
let mut ks = [0u8; 16];
|
||||
while off < input.len() {
|
||||
while off < len {
|
||||
let cb = ctr_val.to_be_bytes();
|
||||
ctr[12] = cb[0];
|
||||
ctr[13] = cb[1];
|
||||
ctr[14] = cb[2];
|
||||
ctr[15] = cb[3];
|
||||
aes256_encrypt_block(&scratch.aes_rk, &ctr, &mut ks);
|
||||
let take = (input.len() - off).min(16);
|
||||
let take = (len - off).min(16);
|
||||
let mut j = 0usize;
|
||||
while j < take {
|
||||
output[off + j] = input[off + j] ^ ks[j];
|
||||
*output.add(off + j) = *input.add(off + j) ^ ks[j];
|
||||
j += 1;
|
||||
}
|
||||
off += 16;
|
||||
@ -2208,13 +2284,13 @@ unsafe fn aes256_gcm_crypt(
|
||||
}
|
||||
|
||||
if encrypt {
|
||||
ghash_update(&mut ghash, &h, output);
|
||||
ghash_update_ptr(&mut ghash, &h, output as *const u8, len);
|
||||
}
|
||||
|
||||
// Final length block: len(AAD)*8 || len(C)*8 (64-bit big-endian each)
|
||||
let mut len_blk = [0u8; 16];
|
||||
let aad_bits = ((aad.len() as u64) * 8).to_be_bytes();
|
||||
let msg_bits = ((input.len() as u64) * 8).to_be_bytes();
|
||||
let msg_bits = ((len as u64) * 8).to_be_bytes();
|
||||
let mut k = 0usize;
|
||||
while k < 8 {
|
||||
len_blk[k] = aad_bits[k];
|
||||
@ -2773,8 +2849,6 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_csl_encrypt(
|
||||
if input.is_null() || output.is_null() || len == 0 {
|
||||
return CSL_NV_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
let inp = slice::from_raw_parts(input, len);
|
||||
let out = slice::from_raw_parts_mut(output, len);
|
||||
let _guard = LockGuard::acquire();
|
||||
let Some(ctx) = csl_find_mut(csl_ctx) else {
|
||||
return CSL_NV_ERR_INVALID_ARGUMENT;
|
||||
@ -2789,7 +2863,7 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_csl_encrypt(
|
||||
i += 1;
|
||||
}
|
||||
let mut tag = [0u8; 16];
|
||||
aes256_gcm_crypt(&ctx.enc_key, &nonce, &[], inp, out, true, &mut tag);
|
||||
aes256_gcm_crypt(&ctx.enc_key, &nonce, &[], input, output, len, true, &mut tag);
|
||||
let mut j = 0usize;
|
||||
while j < 16 {
|
||||
ctx.last_tag[j] = tag[j];
|
||||
@ -2813,8 +2887,6 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_csl_decrypt(
|
||||
if input.is_null() || output.is_null() || len == 0 {
|
||||
return CSL_NV_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
let inp = slice::from_raw_parts(input, len);
|
||||
let out = slice::from_raw_parts_mut(output, len);
|
||||
let _guard = LockGuard::acquire();
|
||||
let Some(ctx) = csl_find_mut(csl_ctx) else {
|
||||
return CSL_NV_ERR_INVALID_ARGUMENT;
|
||||
@ -2829,7 +2901,16 @@ pub unsafe extern "C" fn nv_rs_uvm_iface_csl_decrypt(
|
||||
i += 1;
|
||||
}
|
||||
let mut tag = [0u8; 16];
|
||||
aes256_gcm_crypt(&ctx.dec_key, &nonce, &[], inp, out, false, &mut tag);
|
||||
aes256_gcm_crypt(
|
||||
&ctx.dec_key,
|
||||
&nonce,
|
||||
&[],
|
||||
input,
|
||||
output,
|
||||
len,
|
||||
false,
|
||||
&mut tag,
|
||||
);
|
||||
let mut j = 0usize;
|
||||
while j < 16 {
|
||||
ctx.last_tag[j] = tag[j];
|
||||
|
||||
@ -95,17 +95,25 @@ impl CbcBackingStore {
|
||||
Err(NvStatus(0x1a))
|
||||
}
|
||||
|
||||
/// Releases all compbit tags owned by `owner_handle`.
|
||||
/// Releases all compbit tags owned by `owner_handle` and rewinds `next_tag` to the
|
||||
/// highest still-active watermark (or `1` when idle) so freed tags are reclaimed.
|
||||
pub fn free_tags(&mut self, owner_handle: u32) -> usize {
|
||||
let mut freed = 0usize;
|
||||
let mut watermark = 1u32;
|
||||
for slot in self.allocs.iter_mut() {
|
||||
if let Some(a) = *slot {
|
||||
if a.owner_handle == owner_handle {
|
||||
*slot = None;
|
||||
freed += 1;
|
||||
} else {
|
||||
let end = a.tag_start.saturating_add(a.tag_count);
|
||||
if end > watermark {
|
||||
watermark = end;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
self.next_tag = watermark;
|
||||
freed
|
||||
}
|
||||
}
|
||||
@ -145,5 +153,7 @@ mod tests {
|
||||
let t1 = cbc.alloc_tags(0xabc, 256).unwrap();
|
||||
assert_eq!(t1, 1);
|
||||
assert_eq!(cbc.free_tags(0xabc), 1);
|
||||
// Watermark rewinds to 1 after all allocations are freed.
|
||||
assert_eq!(cbc.alloc_tags(0xdef, 65_000).unwrap(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,6 +63,9 @@ impl NvlsMulticastTable {
|
||||
{
|
||||
return Err(NvStatus(0x1f)); // 2 MiB alignment required for NVLS FLA
|
||||
}
|
||||
if self.groups.iter().flatten().any(|grp| grp.handle == handle) {
|
||||
return Err(NvStatus(0x19)); // NV_ERR_INSERT_DUPLICATE_NAME
|
||||
}
|
||||
if self.next_fla_offset.saturating_add(size_bytes) > self.fla_window_size {
|
||||
return Err(ERR_MC_FLA_OFFSET_TABLE_FULL);
|
||||
}
|
||||
@ -107,17 +110,33 @@ impl NvlsMulticastTable {
|
||||
Err(NvStatus(0x57)) // NV_ERR_OBJECT_NOT_FOUND
|
||||
}
|
||||
|
||||
/// Destroys a multicast group and frees its FLA offset table slot.
|
||||
/// Destroys a multicast group, frees its FLA offset table slot, and rewinds
|
||||
/// `next_fla_offset` to the highest still-active watermark (or `0` when empty).
|
||||
pub fn destroy_group(&mut self, handle: u32) -> NvStatus {
|
||||
let mut found = false;
|
||||
let mut watermark = 0u64;
|
||||
for slot in self.groups.iter_mut() {
|
||||
if let Some(grp) = *slot {
|
||||
if grp.handle == handle {
|
||||
*slot = None;
|
||||
return NvStatus::OK;
|
||||
found = true;
|
||||
} else {
|
||||
let end = grp
|
||||
.mc_fla_base
|
||||
.saturating_sub(self.fla_window_base)
|
||||
.saturating_add(grp.size_bytes);
|
||||
if end > watermark {
|
||||
watermark = end;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
NvStatus(0x57)
|
||||
if found {
|
||||
self.next_fla_offset = watermark;
|
||||
NvStatus::OK
|
||||
} else {
|
||||
NvStatus(0x57)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -130,8 +149,18 @@ mod tests {
|
||||
let mut tbl = NvlsMulticastTable::default();
|
||||
let mc_va = tbl.create_group(0x100, 64 * 1024 * 1024, 2).unwrap();
|
||||
assert_eq!(mc_va, 0x4000_0000_0000);
|
||||
// Duplicate handle is rejected with NV_ERR_INSERT_DUPLICATE_NAME (0x19).
|
||||
assert_eq!(
|
||||
tbl.create_group(0x100, 64 * 1024 * 1024, 2),
|
||||
Err(NvStatus(0x19))
|
||||
);
|
||||
assert!(!tbl.attach_gpu(0x100, 0, 0x1000_0000).unwrap());
|
||||
assert!(tbl.attach_gpu(0x100, 1, 0x2000_0000).unwrap());
|
||||
assert_eq!(tbl.destroy_group(0x100), NvStatus::OK);
|
||||
// Destroying the group reclaims its FLA offset watermark back to 0.
|
||||
assert_eq!(
|
||||
tbl.create_group(0x100, 64 * 1024 * 1024, 2).unwrap(),
|
||||
0x4000_0000_0000
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -114,17 +114,29 @@ impl SriovVgpuManager {
|
||||
Err(NvStatus(0x57))
|
||||
}
|
||||
|
||||
/// Destroys a VF instance.
|
||||
/// Destroys a VF instance and rewinds `next_vram_offset` to the highest active watermark.
|
||||
pub fn destroy_vf(&mut self, gfid: u8) -> NvStatus {
|
||||
let mut found = false;
|
||||
let mut watermark = 0u64;
|
||||
for slot in self.vfs.iter_mut() {
|
||||
if let Some(vf) = *slot {
|
||||
if vf.gfid == gfid {
|
||||
*slot = None;
|
||||
return NvStatus::OK;
|
||||
found = true;
|
||||
} else {
|
||||
let end = vf.vram_offset.saturating_add(vf.vram_size);
|
||||
if end > watermark {
|
||||
watermark = end;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
NvStatus(0x57)
|
||||
if found {
|
||||
self.next_vram_offset = watermark;
|
||||
NvStatus::OK
|
||||
} else {
|
||||
NvStatus(0x57)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -143,5 +155,7 @@ mod tests {
|
||||
assert_eq!(mgr.set_dirty_page_tracking(vf1, true), NvStatus::OK);
|
||||
assert_eq!(mgr.mark_dirty_page(vf1, 3).unwrap(), 1 << 3);
|
||||
assert_eq!(mgr.destroy_vf(vf1), NvStatus::OK);
|
||||
// Destroying the sole VF reclaims VRAM watermark so a full 16 GiB VF fits.
|
||||
assert_eq!(mgr.create_vf(16 * 1024 * 1024 * 1024).unwrap(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,8 +80,11 @@ impl SyncTimeline {
|
||||
if count < signaled_out.len() {
|
||||
signaled_out[count] = w.fence_token;
|
||||
count += 1;
|
||||
*slot = None;
|
||||
} else {
|
||||
// Keep overflowed ready waiters queued for the caller's next drain.
|
||||
break;
|
||||
}
|
||||
*slot = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -108,4 +111,21 @@ mod tests {
|
||||
assert_eq!(tl.advance(10, &mut out), 1);
|
||||
assert_eq!(out[0], 0x202);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timeline_advance_preserves_ready_waiters_when_output_slice_is_full() {
|
||||
let mut tl = SyncTimeline::new();
|
||||
assert!(!tl.register_waiter(5, 0x111).unwrap());
|
||||
assert!(!tl.register_waiter(6, 0x222).unwrap());
|
||||
assert!(!tl.register_waiter(7, 0x333).unwrap());
|
||||
|
||||
let mut small = [0u64; 1];
|
||||
assert_eq!(tl.advance(10, &mut small), 1);
|
||||
assert_eq!(small[0], 0x111);
|
||||
assert_eq!(tl.advance(10, &mut small), 1);
|
||||
assert_eq!(small[0], 0x222);
|
||||
assert_eq!(tl.advance(10, &mut small), 1);
|
||||
assert_eq!(small[0], 0x333);
|
||||
assert_eq!(tl.advance(10, &mut small), 0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,7 +81,15 @@ impl HmmContext {
|
||||
for slot in self.blocks.iter_mut() {
|
||||
if let Some(blk) = slot.as_mut() {
|
||||
if blk.va_base == va_base {
|
||||
blk.residency = target;
|
||||
blk.residency = match (blk.residency, target) {
|
||||
(
|
||||
HmmResidency::ReadDuplicated { gpu_mask: existing },
|
||||
HmmResidency::ReadDuplicated { gpu_mask: added },
|
||||
) => HmmResidency::ReadDuplicated {
|
||||
gpu_mask: existing | added,
|
||||
},
|
||||
(_, next) => next,
|
||||
};
|
||||
blk.mmu_notifier_seq = seq;
|
||||
blk.dirty = !read_only;
|
||||
return Ok(seq);
|
||||
@ -99,6 +107,20 @@ impl HmmContext {
|
||||
return Ok(seq);
|
||||
}
|
||||
}
|
||||
// Recycle an already-evicted Host slot so repeated evictions never wedge HMM.
|
||||
for slot in self.blocks.iter_mut() {
|
||||
if let Some(blk) = slot.as_mut() {
|
||||
if blk.residency == HmmResidency::Host {
|
||||
*blk = HmmVaBlock {
|
||||
va_base,
|
||||
residency: target,
|
||||
mmu_notifier_seq: seq,
|
||||
dirty: !read_only,
|
||||
};
|
||||
return Ok(seq);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(NvStatus(0x1a)) // NV_ERR_INSUFFICIENT_RESOURCES
|
||||
}
|
||||
|
||||
@ -146,4 +168,25 @@ mod tests {
|
||||
assert_eq!(hmm.blocks[0].unwrap().residency, HmmResidency::Host);
|
||||
assert_eq!(hmm.global_notifier_seq, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hmm_merges_read_duplication_masks_and_recycles_evicted_host_slots() {
|
||||
let mut hmm = HmmContext::new();
|
||||
hmm.fault_migrate_to_gpu(0x1_0000_0000, 0, true).unwrap();
|
||||
hmm.fault_migrate_to_gpu(0x1_0000_0000, 2, true).unwrap();
|
||||
assert_eq!(
|
||||
hmm.blocks[0].unwrap().residency,
|
||||
HmmResidency::ReadDuplicated {
|
||||
gpu_mask: (1 << 0) | (1 << 2)
|
||||
}
|
||||
);
|
||||
|
||||
// Fill all 32 slots, evict all of them back to Host, and verify new faults succeed.
|
||||
for i in 1u64..32 {
|
||||
hmm.fault_migrate_to_gpu(0x2_0000_0000 + i * 0x20_0000, 0, false)
|
||||
.unwrap();
|
||||
}
|
||||
assert_eq!(hmm.invalidate_range(0, u64::MAX), 32);
|
||||
hmm.fault_migrate_to_gpu(0x9_0000_0000, 1, false).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user