mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
rmapi_node: uncached kernel mapping and UC/WC user mappings for non-cached system memory
RM API system memory (class 0x3e) recorded its NV_MEMORY_* cache type but ignored it: every
allocation kept a write-back kernel mapping, a PAGE_KERNEL vmap and write-back user mappings,
also for the uncached (1) and write-combined (2) types libcuda requests
(NVOS32_ATTR_COHERENCY_WRITE_COMBINE).
Stock, for an allocation that is not NV_MEMORY_CACHED:
- makes the kernel mapping of its pages uncached when it allocates them:
nv_alloc_system_pages -> nv_set_memory_type(at, NV_MEMORY_UNCACHED), 580.178.04
nv-vm.c:583-584 -> nv_set_pages_array_type, nv-vm.c:106-128 (set_pages_array_uc on x86);
- restores write-back before it frees them: nv_free_system_pages, nv-vm.c:619-620;
- vmaps them PAGE_KERNEL_NOCACHE: nv_alloc_kernel_mapping -> nv_vm_map_pages(cached =
cache_type == NV_MEMORY_CACHED), nv.c:3762-3763 -> nv_vmap, nv-vm.c:634-656;
- maps them to user space with that type: nvidia_mmap -> nv_encode_caching before
nvidia_mmap_sysmem, nv-mmap.c:328-394 and 699-708 (620.73.04 nv-mmap.c:346-363 and 617-622
use pgprot_writecombine / pgprot_noncached).
The same now happens here, on x86 (CONFIG_X86), where stock's conftest finds set_pages_array_*.
On other architectures allocations stay cached, as before (stock still applies its uncached
pgprot to user mappings there).
Deviation, on purpose: stock ignores the result of set_pages_array_uc. That call can fail after
it changed the attribute of some pages (it releases only the memtype reservations), and a
write-back user mapping must not alias an uncached page. Here a failed conversion is undone with
set_pages_array_wb and the allocation stays cached everywhere; if that restore also fails, the
allocation fails and its pages are leaked. On free and on the registry-insert failure path, pages
whose write-back attribute cannot be restored are leaked too, never pooled or freed.
The decision logic is in nv_rs_core (alloc_registry.rs: sysmem_make_uncached,
sysmem_may_release) with host tests; the kernel calls stay in rmapi_node.c
(nv_rs_rmapi_os_pages_uc / _wb), joined by two exports in rmapi_memory.rs
(nv_rs_sysmem_make_uncached, nv_rs_sysmem_may_release), added to both -u lists.
Tests:
- cargo test sysmem_memtype: 4 tests. With the 31ca25ca behaviour (no restore after a failed
set_pages_array_uc, pages freed whatever the restore returns) 3 of the 4 fail.
- EDU guest: a 16-page write-combined allocation has a UC- kernel mapping after allocation and
write-back after free, and its vmap (kernel_write) works; with set_pages_array_uc reported as
failed after it ran, the guest restores write-back and the check fails as expected.
GPU PTEs are unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -41,6 +41,7 @@
|
||||
#ifdef CONFIG_X86
|
||||
#include <asm/cpufeature.h>
|
||||
#include <asm/processor.h>
|
||||
#include <asm/set_memory.h>
|
||||
#include <asm/tsc.h>
|
||||
#endif
|
||||
#ifdef CONFIG_COMPAT
|
||||
@ -536,9 +537,10 @@ static struct nv_rs_rmapi_file *nv_rs_rmapi_context(struct file *file, int gpu)
|
||||
/* System memory of the RM API (class 0x3e): the operating-system side the Rust provider
|
||||
* src/nvidia/kernel/rmapi_memory.rs calls. Every function below runs under
|
||||
* nv_rs_rmapi_lock, from an ioctl or a release of one of this driver's RM API nodes; none
|
||||
* touches a device. The pages are ordinary kernel pages with the kernel's cached
|
||||
* attribute: the original also changes the page attributes for the uncached and
|
||||
* write-combined cache types and maps the pages for the GPU's DMA, neither is done here.
|
||||
* touches a device. The pages are ordinary kernel pages. On x86 an allocation of the
|
||||
* uncached or write-combined cache type gets an uncached kernel mapping and user mappings
|
||||
* of its type, as the original's (nv_set_memory_type, nv_encode_caching); elsewhere it
|
||||
* stays cached.
|
||||
*
|
||||
* An allocation lives while it has users: the RM object (until nv_rs_rmapi_os_free_pages)
|
||||
* and every file that carries a mapping context of it (until the file is released; the
|
||||
@ -610,6 +612,10 @@ struct nv_rs_rmapi_alloc {
|
||||
u32 cache;
|
||||
bool user_pinned;
|
||||
bool writable;
|
||||
/* `cache` is uncached or write-combined and the kernel mapping of the pages was made uncached
|
||||
* (nv_rs_sysmem_make_uncached): user and kernel mappings carry `cache`, and the pages get their
|
||||
* write-back attribute back before they are freed or pooled. */
|
||||
bool kernel_uc;
|
||||
void *kernel;
|
||||
struct page *pages[];
|
||||
};
|
||||
@ -1493,6 +1499,31 @@ static void nv_rs_rmapi_trim_page_pools_on_client_close(void)
|
||||
nv_rs_rmapi_trim_page_pools((unsigned long)retain_pages);
|
||||
}
|
||||
|
||||
/* The page-attribute changes of the original's nv_set_pages_array_type (580.178.04 nv-vm.c:106-128), which it has
|
||||
* on x86 only (conftest-guarded set_pages_array_*). nv_rs_sysmem_make_uncached and nv_rs_sysmem_may_release
|
||||
* (src/nvidia/kernel/rmapi_memory.rs) call them and decide what a failure means. set_pages_array_* take an int count:
|
||||
* a larger allocation is never changed, so it has nothing to restore. */
|
||||
int nv_rs_sysmem_make_uncached(struct page **pages, u64 count);
|
||||
bool nv_rs_sysmem_may_release(struct page **pages, u64 count, bool kernel_uc);
|
||||
bool nv_rs_rmapi_os_pages_uc(struct page **pages, u64 count);
|
||||
bool nv_rs_rmapi_os_pages_wb(struct page **pages, u64 count);
|
||||
bool nv_rs_rmapi_os_pages_uc(struct page **pages, u64 count)
|
||||
{
|
||||
#ifdef CONFIG_X86
|
||||
return count <= INT_MAX && set_pages_array_uc(pages, (int)count) == 0;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
bool nv_rs_rmapi_os_pages_wb(struct page **pages, u64 count)
|
||||
{
|
||||
#ifdef CONFIG_X86
|
||||
return count > INT_MAX || set_pages_array_wb(pages, (int)count) == 0;
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
void *nv_rs_rmapi_kvzalloc(size_t size);
|
||||
void nv_rs_rmapi_kvfree(void *ptr);
|
||||
|
||||
@ -1713,6 +1744,11 @@ static void nv_rs_rmapi_alloc_destroy(struct nv_rs_rmapi_alloc *alloc)
|
||||
nv_rs_rmapi_alloc_unmap(alloc);
|
||||
if (alloc->user_pinned) {
|
||||
unpin_user_pages_dirty_lock(alloc->pages, alloc->count, alloc->writable);
|
||||
} else if (!nv_rs_sysmem_may_release(alloc->pages, alloc->count, alloc->kernel_uc)) {
|
||||
/* nv_free_system_pages restores write-back first (580.178.04 nv-vm.c:619-620). Pages that may still be
|
||||
* uncached are leaked: never pooled, never handed back to the page allocator. */
|
||||
pr_warn_ratelimited("nvidia_rs: leaking %llu pages whose cached attribute could not be restored\n",
|
||||
alloc->count);
|
||||
} else {
|
||||
int retain_mb = READ_ONCE(system_memory_pool_retain_mb);
|
||||
for (index = 0; index < alloc->count; index++) {
|
||||
@ -1844,12 +1880,27 @@ u32 nv_rs_rmapi_os_alloc_pages(u64 count, u64 page, bool contiguous, u32 cache,
|
||||
}
|
||||
alloc->count = count;
|
||||
alloc->cache = cache;
|
||||
/* nv_alloc_system_pages: not cached -> nv_set_memory_type(at, NV_MEMORY_UNCACHED) (580.178.04 nv-vm.c:583-584).
|
||||
* If that fails the allocation stays cached everywhere, user mappings included. */
|
||||
if (cache != 0) {
|
||||
int uc = nv_rs_sysmem_make_uncached(alloc->pages, count);
|
||||
if (uc < 0) {
|
||||
pr_warn_ratelimited("nvidia_rs: leaking %llu pages whose cached attribute could not be restored\n",
|
||||
count);
|
||||
kvfree(alloc);
|
||||
return NV_RS_STATUS_NO_MEMORY;
|
||||
}
|
||||
alloc->kernel_uc = uc == 0;
|
||||
}
|
||||
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
||||
ok = nv_rs_alloc_reg_insert(alloc, count, false, &tok);
|
||||
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
||||
if (!ok) {
|
||||
for (index = 0; index < count; index++)
|
||||
__free_page(alloc->pages[index]);
|
||||
/* as on free: pages that may still be uncached are leaked */
|
||||
if (nv_rs_sysmem_may_release(alloc->pages, count, alloc->kernel_uc)) {
|
||||
for (index = 0; index < count; index++)
|
||||
__free_page(alloc->pages[index]);
|
||||
}
|
||||
kvfree(alloc);
|
||||
return NV_RS_STATUS_NO_MEMORY;
|
||||
}
|
||||
@ -1875,6 +1926,17 @@ void nv_rs_rmapi_os_free_pages(u64 token)
|
||||
nv_rs_rmapi_alloc_destroy(ptr);
|
||||
}
|
||||
|
||||
/* nv_vmap (580.178.04 nv-vm.c:634-656), which nv_alloc_kernel_mapping asks for a cached mapping only for
|
||||
* NV_MEMORY_CACHED (nv.c:3762-3763): PAGE_KERNEL_NOCACHE for pages whose kernel mapping was made uncached. */
|
||||
static pgprot_t nv_rs_rmapi_kernel_prot(const struct nv_rs_rmapi_alloc *alloc)
|
||||
{
|
||||
#ifdef CONFIG_X86
|
||||
if (alloc->kernel_uc)
|
||||
return PAGE_KERNEL_NOCACHE;
|
||||
#endif
|
||||
return PAGE_KERNEL;
|
||||
}
|
||||
|
||||
/* nv_alloc_kernel_mapping of the whole allocation: the page's own address for one page,
|
||||
* vmap for more. */
|
||||
u32 nv_rs_rmapi_os_kernel_map(u64 token, u64 size)
|
||||
@ -1888,7 +1950,8 @@ u32 nv_rs_rmapi_os_kernel_map(u64 token, u64 size)
|
||||
rc = NV_RS_STATUS_INVALID_ARGUMENT;
|
||||
} else if (!alloc->kernel) {
|
||||
alloc->kernel = alloc->count == 1 ? page_address(alloc->pages[0])
|
||||
: vmap(alloc->pages, alloc->count, VM_MAP, PAGE_KERNEL);
|
||||
: vmap(alloc->pages, alloc->count, VM_MAP,
|
||||
nv_rs_rmapi_kernel_prot(alloc));
|
||||
if (!alloc->kernel)
|
||||
rc = NV_RS_STATUS_NO_MEMORY;
|
||||
}
|
||||
@ -1928,7 +1991,8 @@ u32 nv_rs_rmapi_os_kernel_write(u64 token, u64 offset, const u8 *bytes, size_t l
|
||||
if (!alloc->kernel) {
|
||||
alloc->kernel = alloc->count == 1
|
||||
? page_address(alloc->pages[0])
|
||||
: vmap(alloc->pages, alloc->count, VM_MAP, PAGE_KERNEL);
|
||||
: vmap(alloc->pages, alloc->count, VM_MAP,
|
||||
nv_rs_rmapi_kernel_prot(alloc));
|
||||
if (!alloc->kernel)
|
||||
rc = NV_RS_STATUS_NO_MEMORY;
|
||||
}
|
||||
@ -3032,10 +3096,9 @@ static const struct vm_operations_struct nv_rs_rmapi_vm_operations = {
|
||||
* whose page references keep them alive while mapped.
|
||||
*
|
||||
* Differences from the original: a private mapping is refused with EINVAL (the original
|
||||
* turns it into a shared one by setting VM_SHARED afterwards); the pages are always mapped
|
||||
* cached (the original applies the allocation's cache type to the protection, after
|
||||
* changing the pages' kernel attributes at allocation); a read-only context is applied
|
||||
* before the pages are inserted, not after.
|
||||
* turns it into a shared one by setting VM_SHARED afterwards); the allocation's cache type
|
||||
* is applied only when its kernel mapping was made uncached, otherwise the pages are mapped
|
||||
* cached; a read-only context is applied before the pages are inserted, not after.
|
||||
*
|
||||
* The context is immutable once valid and holds a use of the allocation until the file is
|
||||
* released, so nothing is locked here. */
|
||||
@ -3177,6 +3240,11 @@ static int nv_rs_rmapi_mmap(struct file *file, struct vm_area_struct *vma)
|
||||
nv_rs_vm_flags_clear(vma, VM_WRITE | VM_MAYWRITE);
|
||||
vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
|
||||
}
|
||||
/* nvidia_mmap: nv_encode_caching(at->cache_type) before nvidia_mmap_sysmem (580.178.04 nv-mmap.c:328-394,
|
||||
* 699-708; 620.73.04 nv-mmap.c:346-363 uses pgprot_writecombine / pgprot_noncached as here) */
|
||||
if (alloc->kernel_uc)
|
||||
vma->vm_page_prot = alloc->cache == 2 ? pgprot_writecombine(vma->vm_page_prot)
|
||||
: pgprot_noncached(vma->vm_page_prot);
|
||||
nv_rs_vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
|
||||
vma->vm_ops = &nv_rs_rmapi_vm_operations;
|
||||
for (index = mapping.page_index; index < mapping.page_index + pages; index++) {
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -13,8 +13,9 @@ use core::ptr;
|
||||
use core::sync::atomic::{AtomicU64, Ordering};
|
||||
use nv_rs_abi::NvStatus;
|
||||
use nv_rs_core::rmapi::alloc_registry::{
|
||||
AllocLedger, AllocRecord, DmaMapLedger, DmaMapRecord, FileMappingLedger, FileMappingRecord,
|
||||
GpuSlotLedger, SysmemPagePool, UvmCpuDmaLedger, UvmCpuDmaRecord, UvmCpuDmaSlot,
|
||||
sysmem_make_uncached, sysmem_may_release, AllocLedger, AllocRecord, DmaMapLedger, DmaMapRecord,
|
||||
FileMappingLedger, FileMappingRecord, GpuSlotLedger, SysmemPageAttrs, SysmemPagePool,
|
||||
SysmemUncached, UvmCpuDmaLedger, UvmCpuDmaRecord, UvmCpuDmaSlot,
|
||||
};
|
||||
use nv_rs_core::rmapi::memory::{MappingContext, OsMemory, OsToken};
|
||||
use nv_rs_core::rmapi::status;
|
||||
@ -86,6 +87,8 @@ unsafe extern "C" {
|
||||
fn nv_rs_rmapi_os_page_dma(gpu_id: u32, token: u64, page_index: u64, dma_out: *mut u64) -> u32;
|
||||
fn nv_rs_rmapi_os_flush_gpu_cache();
|
||||
fn nv_rs_rmapi_os_ctl_fd_owner(descriptor: i32) -> u64;
|
||||
fn nv_rs_rmapi_os_pages_uc(pages: *mut c_void, count: u64) -> bool;
|
||||
fn nv_rs_rmapi_os_pages_wb(pages: *mut c_void, count: u64) -> bool;
|
||||
}
|
||||
|
||||
/// The kernel's memory services for the RM API nodes. Use only under the node mutex.
|
||||
@ -94,7 +97,9 @@ pub struct KernelOs;
|
||||
impl OsMemory for KernelOs {
|
||||
/// One zeroed kernel page per 4 KiB page. Big and huge granules and contiguous runs of
|
||||
/// more than one page are NOT_SUPPORTED; past the node's page budget NO_MEMORY (0x51).
|
||||
/// The cache type is recorded only: the pages keep the kernel's cached attribute.
|
||||
/// On x86 an allocation of cache type 1 (uncached) or 2 (write-combined) gets an uncached
|
||||
/// kernel mapping (`nv_rs_sysmem_make_uncached`) and user mappings of its type; elsewhere,
|
||||
/// or when that change fails and is undone, it stays cached.
|
||||
fn alloc_pages(
|
||||
&mut self,
|
||||
_gpu: u32,
|
||||
@ -763,6 +768,47 @@ pub unsafe extern "C" fn nv_rs_sysmem_pool_release_storage() {
|
||||
SYSMEM_POOL_LEN.store(0, Ordering::Release);
|
||||
}
|
||||
|
||||
/// The `struct page *` array of one allocation in `rmapi_node.c`, whose kernel mapping
|
||||
/// `nv_rs_rmapi_os_pages_uc` / `nv_rs_rmapi_os_pages_wb` change.
|
||||
struct OsPages {
|
||||
pages: *mut c_void,
|
||||
count: u64,
|
||||
}
|
||||
|
||||
impl SysmemPageAttrs for OsPages {
|
||||
fn set_uncached(&mut self) -> bool {
|
||||
// SAFETY: `pages` is the caller's array of `count` pages, which it owns for the call.
|
||||
unsafe { nv_rs_rmapi_os_pages_uc(self.pages, self.count) }
|
||||
}
|
||||
fn set_write_back(&mut self) -> bool {
|
||||
// SAFETY: as in `set_uncached`.
|
||||
unsafe { nv_rs_rmapi_os_pages_wb(self.pages, self.count) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Makes the kernel mapping of a new allocation that is not cached uncached
|
||||
/// (`sysmem_make_uncached`): 0 uncached, 1 cached everywhere (a failed change was undone),
|
||||
/// -1 the pages could not be restored and the caller leaks them.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn nv_rs_sysmem_make_uncached(pages: *mut c_void, count: u64) -> i32 {
|
||||
match sysmem_make_uncached(&mut OsPages { pages, count }) {
|
||||
SysmemUncached::Uncached => 0,
|
||||
SysmemUncached::Cached => 1,
|
||||
SysmemUncached::Leak => -1,
|
||||
}
|
||||
}
|
||||
|
||||
/// Before an allocation's pages go back to the pool or the page allocator
|
||||
/// (`sysmem_may_release`): `false` when they must be leaked instead.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn nv_rs_sysmem_may_release(
|
||||
pages: *mut c_void,
|
||||
count: u64,
|
||||
kernel_uc: bool,
|
||||
) -> bool {
|
||||
sysmem_may_release(kernel_uc, &mut OsPages { pages, count })
|
||||
}
|
||||
|
||||
static mut UVM_CPU_DMA_LEDGER: UvmCpuDmaLedger = UvmCpuDmaLedger::new();
|
||||
static mut UVM_CPU_DMA_SLOTS: KernelVec<UvmCpuDmaSlot> = KernelVec::new();
|
||||
|
||||
|
||||
@ -1146,6 +1146,136 @@ impl SysmemPagePool {
|
||||
}
|
||||
}
|
||||
|
||||
/// The two attribute changes `rmapi_node.c` makes to the kernel mapping of one allocation's pages
|
||||
/// (`set_pages_array_uc` / `set_pages_array_wb` on x86). Each returns `false` when it failed.
|
||||
pub trait SysmemPageAttrs {
|
||||
fn set_uncached(&mut self) -> bool;
|
||||
fn set_write_back(&mut self) -> bool;
|
||||
}
|
||||
|
||||
/// What became of the kernel mapping of a new allocation RM does not want cached.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum SysmemUncached {
|
||||
/// The kernel mapping is uncached; user and kernel mappings carry the allocation's cache type.
|
||||
Uncached,
|
||||
/// The change failed and was undone: the allocation stays cached everywhere.
|
||||
Cached,
|
||||
/// The change failed and could not be undone, so part of the pages may still be uncached:
|
||||
/// they are never mapped cached, pooled or freed.
|
||||
Leak,
|
||||
}
|
||||
|
||||
/// `nv_set_memory_type(at, NV_MEMORY_UNCACHED)`, which the original's `nv_alloc_system_pages`
|
||||
/// applies to every allocation that is not `NV_MEMORY_CACHED` (580.178.04 nv-vm.c:583-584,
|
||||
/// nv-vm.c:106-128). The original ignores the result. Here a failure is undone, because
|
||||
/// `set_pages_array_uc` can fail after it changed the attribute of some pages (it releases only
|
||||
/// the memtype reservations), and a cached user mapping must not alias an uncached page.
|
||||
pub fn sysmem_make_uncached(attrs: &mut impl SysmemPageAttrs) -> SysmemUncached {
|
||||
if attrs.set_uncached() {
|
||||
SysmemUncached::Uncached
|
||||
} else if attrs.set_write_back() {
|
||||
SysmemUncached::Cached
|
||||
} else {
|
||||
SysmemUncached::Leak
|
||||
}
|
||||
}
|
||||
|
||||
/// `nv_set_memory_type(at, NV_MEMORY_WRITEBACK)` before the original frees the pages of an
|
||||
/// allocation that is not cached (580.178.04 nv-vm.c:619-620). `true`: the pages may go back to
|
||||
/// the page pool or the page allocator. `false`: their attribute could not be restored and they
|
||||
/// are leaked.
|
||||
pub fn sysmem_may_release(kernel_uc: bool, attrs: &mut impl SysmemPageAttrs) -> bool {
|
||||
!kernel_uc || attrs.set_write_back()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod sysmem_memtype_tests {
|
||||
use super::*;
|
||||
|
||||
/// Page attributes as the x86 calls leave them: `set_uncached` may fail after it changed
|
||||
/// some pages, which then stay uncached until a successful `set_write_back`.
|
||||
struct Pages {
|
||||
uncached: [bool; 4],
|
||||
uc_fails_after: Option<usize>,
|
||||
wb_fails: bool,
|
||||
uc_calls: u32,
|
||||
wb_calls: u32,
|
||||
}
|
||||
|
||||
impl Pages {
|
||||
fn new(uc_fails_after: Option<usize>, wb_fails: bool) -> Self {
|
||||
Self {
|
||||
uncached: [false; 4],
|
||||
uc_fails_after,
|
||||
wb_fails,
|
||||
uc_calls: 0,
|
||||
wb_calls: 0,
|
||||
}
|
||||
}
|
||||
fn any_uncached(&self) -> bool {
|
||||
self.uncached.iter().any(|&page| page)
|
||||
}
|
||||
}
|
||||
|
||||
impl SysmemPageAttrs for Pages {
|
||||
fn set_uncached(&mut self) -> bool {
|
||||
self.uc_calls += 1;
|
||||
let changed = self.uc_fails_after.unwrap_or(self.uncached.len());
|
||||
for page in self.uncached.iter_mut().take(changed) {
|
||||
*page = true;
|
||||
}
|
||||
self.uc_fails_after.is_none()
|
||||
}
|
||||
fn set_write_back(&mut self) -> bool {
|
||||
self.wb_calls += 1;
|
||||
if self.wb_fails {
|
||||
return false;
|
||||
}
|
||||
self.uncached = [false; 4];
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conversion_that_succeeds_is_kept() {
|
||||
let mut pages = Pages::new(None, false);
|
||||
assert_eq!(sysmem_make_uncached(&mut pages), SysmemUncached::Uncached);
|
||||
assert_eq!((pages.uc_calls, pages.wb_calls), (1, 0));
|
||||
assert!(pages.uncached.iter().all(|&page| page));
|
||||
// free: write-back first, then the pages may be released
|
||||
assert!(sysmem_may_release(true, &mut pages));
|
||||
assert_eq!(pages.wb_calls, 1);
|
||||
assert!(!pages.any_uncached());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_conversion_is_undone_before_the_cached_fallback() {
|
||||
// two of four pages changed before the failure
|
||||
let mut pages = Pages::new(Some(2), false);
|
||||
assert_eq!(sysmem_make_uncached(&mut pages), SysmemUncached::Cached);
|
||||
assert_eq!((pages.uc_calls, pages.wb_calls), (1, 1));
|
||||
assert!(!pages.any_uncached());
|
||||
// a cached allocation is released without another attribute change
|
||||
assert!(sysmem_may_release(false, &mut pages));
|
||||
assert_eq!(pages.wb_calls, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_conversion_that_cannot_be_undone_leaks_the_pages() {
|
||||
let mut pages = Pages::new(Some(2), true);
|
||||
assert_eq!(sysmem_make_uncached(&mut pages), SysmemUncached::Leak);
|
||||
assert!(pages.any_uncached());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pages_whose_write_back_fails_on_free_are_leaked() {
|
||||
let mut pages = Pages::new(None, true);
|
||||
assert_eq!(sysmem_make_uncached(&mut pages), SysmemUncached::Uncached);
|
||||
assert!(!sysmem_may_release(true, &mut pages));
|
||||
assert!(pages.any_uncached());
|
||||
}
|
||||
}
|
||||
|
||||
/// Number of hash buckets in [`UvmCpuDmaLedger`], matching the 10-bit hash table
|
||||
/// (`DEFINE_HASHTABLE(nv_rs_uvm_cpu_dma_ht, 10)`) previously maintained in C.
|
||||
pub const UVM_DMA_BUCKETS: usize = 1024;
|
||||
|
||||
@ -363,24 +363,41 @@ static int rmapi_read32(void *cookie,u64 offset,u32 *value)
|
||||
}
|
||||
/* System memory of the RM API (rmapi_node.c) DMA-mapped for the listed GPU: an allocation of more than one page is
|
||||
* mapped in one scatterlist, as the original does (nv_dma_map_scatterlist), so behind a translating IOMMU its pages
|
||||
* get one IOVA range, in order; without one each page's DMA address is its own physical address. */
|
||||
* get one IOVA range, in order; without one each page's DMA address is its own physical address. An allocation that
|
||||
* is not cached (cache type 1 or 2) has an uncached (UC-) kernel mapping until it is freed (nv_set_memory_type). */
|
||||
u32 nv_rs_rmapi_os_alloc_pages(u64 count,u64 page,bool contiguous,u32 cache,u64 *token);
|
||||
void nv_rs_rmapi_os_free_pages(u64 token);
|
||||
u32 nv_rs_rmapi_os_page_phys(u64 token,u64 page_index,u64 *phys);
|
||||
u32 nv_rs_rmapi_os_page_dma(u32 gpu_id,u64 token,u64 page_index,u64 *dma_out);
|
||||
u32 nv_rs_rmapi_os_kernel_write(u64 token,u64 offset,const u8 *bytes,size_t length);
|
||||
/* The cache mode of the kernel's linear mapping of the page at `phys`, read as rom_fw_adapter.c verify_pages does. */
|
||||
static bool rmapi_page_cache_is(u64 phys,bool uncached)
|
||||
{
|
||||
unsigned int level;pteval_t value;unsigned long expected=cachemode2protval(uncached?_PAGE_CACHE_MODE_UC_MINUS:_PAGE_CACHE_MODE_WB)&_PAGE_CACHE_MASK;
|
||||
pte_t *entry=lookup_address((unsigned long)page_address(pfn_to_page(phys>>PAGE_SHIFT)),&level);
|
||||
if(!entry)return false;
|
||||
value=pte_val(*entry);
|
||||
if(level>PG_LEVEL_4K)value=protval_large_2_4k(value);
|
||||
return (value&_PAGE_CACHE_MASK)==expected;
|
||||
}
|
||||
static int rmapi_sysmem_check(struct pci_dev *pdev,u32 cache)
|
||||
{
|
||||
struct iommu_domain *domain=iommu_get_domain_for_dev(&pdev->dev);
|
||||
bool translated=domain&&(domain->type==IOMMU_DOMAIN_DMA||domain->type==IOMMU_DOMAIN_DMA_FQ);
|
||||
u32 gpu_id=((u32)(pci_domain_nr(pdev->bus)&0xffff)<<16)|((u32)pdev->bus->number<<8)|PCI_SLOT(pdev->devfn);
|
||||
u64 token=0,first=0,dma=0,phys=0;unsigned int i;int e=0;
|
||||
u64 token=0,first=0,dma=0,phys[16]={0};unsigned int i;int e=0;u8 byte=0x5a;
|
||||
if(nv_rs_rmapi_os_alloc_pages(16,PAGE_SIZE,false,cache,&token))e=-ENOMEM;
|
||||
/* the kernel mapping of the whole allocation (vmap; PAGE_KERNEL_NOCACHE when it is not cached) */
|
||||
if(!e&&nv_rs_rmapi_os_kernel_write(token,15*PAGE_SIZE+7,&byte,1))e=-EIO;
|
||||
for(i=0;i<16&&!e;i++){
|
||||
if(nv_rs_rmapi_os_page_dma(gpu_id,token,i,&dma)||nv_rs_rmapi_os_page_phys(token,i,&phys))e=-EIO;
|
||||
if(nv_rs_rmapi_os_page_dma(gpu_id,token,i,&dma)||nv_rs_rmapi_os_page_phys(token,i,&phys[i]))e=-EIO;
|
||||
else if(!i)first=dma;
|
||||
if(!e&&(translated?dma!=first+(u64)i*PAGE_SIZE:dma!=phys))e=-ERANGE;
|
||||
if(!e&&(translated?dma!=first+(u64)i*PAGE_SIZE:dma!=phys[i]))e=-ERANGE;
|
||||
if(!e&&!rmapi_page_cache_is(phys[i],cache!=0))e=-EPROTO;
|
||||
}
|
||||
if(token)nv_rs_rmapi_os_free_pages(token);
|
||||
/* freed pages are write-back again (only the direct map's entry is read) */
|
||||
for(i=0;i<16&&!e;i++)if(!rmapi_page_cache_is(phys[i],false))e=-EPROTO;
|
||||
if(e)pr_err("NV_RS_VM_FAIL rmapi system memory (cache type %u): %d\n",cache,e);
|
||||
return e;
|
||||
}
|
||||
@ -394,6 +411,7 @@ static int rmapi_start(void)
|
||||
if(e!=0){pci_dev_put(rmapi_gpu);rmapi_gpu=NULL;nv_rs_rmapi_node_exit();return e<0?e:-EINVAL;}
|
||||
e=nv_rs_rmapi_node_gpu_reader(rmapi_gpu,rmapi_read32,NULL,NULL);
|
||||
if(!e)e=rmapi_sysmem_check(rmapi_gpu,0);
|
||||
if(!e)e=rmapi_sysmem_check(rmapi_gpu,2);
|
||||
if(e){nv_rs_rmapi_node_gpu_remove(rmapi_gpu);pci_dev_put(rmapi_gpu);rmapi_gpu=NULL;nv_rs_rmapi_node_exit();return e;}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -51,7 +51,7 @@ fi
|
||||
"$RUSTC" $RUSTFLAGS --crate-name nv_rs_core --crate-type=rlib src/nvidia/src/lib.rs --extern nv_rs_abi="$OUT/libnv_rs_abi.rlib" -o "$OUT/libnv_rs_core.rlib"
|
||||
"$RUSTC" $RUSTFLAGS --crate-name vm_rust_backing --crate-type=staticlib -C lto=fat tests/vm/rust_backing.rs --extern nv_rs_abi="$OUT/libnv_rs_abi.rlib" --extern nv_rs_core="$OUT/libnv_rs_core.rlib" -L dependency="$OUT" -o "$OUT/libvm_rust_backing.a"
|
||||
python3 "$ROOT/tools/prepare_kernel_archive.py" "$OUT/libvm_rust_backing.a" "$OUT/libvm_rust_backing.kernel.a"
|
||||
ld -r --gc-sections --whole-archive "$OUT/libvm_rust_backing.kernel.a" --no-whole-archive -u vm_rust_backing -u vm_rust_backing_size -u vm_rust_backing_align -u vm_gpu_lease_size -u vm_gpu_lease_acquire -u vm_gpu_lease_ready -u vm_gpu_lease_drop -u vm_gpu_registers_size -u vm_gpu_registers_acquire -u vm_gpu_registers_read -u vm_gpu_registers_write -u vm_gpu_registers_drop -u vm_gpu_locked_size -u vm_gpu_locked_acquire -u vm_cold_msi_config -u vm_gpu_locked_read -u vm_gpu_locked_write -u vm_gpu_locked_drop -u vm_gpu_waited_acquire -u vm_native_memory -u vm_gpu_registry_probe -u vm_gpu_registry_removed -u vm_nano_timer -u vm_os_event -u vm_device_software -u vm_first_open_cpu_state -u vm_software_manager_init -u vm_software_manager_shutdown -u vm_nano_size -u vm_nano_hold -u vm_nano_removed -u vm_host_services -u vm_host_irq_services -u vm_thread_state -u nv_rs_gpu_state_size -u nv_rs_gpu_state_align -u nv_rs_gpu_prepare_owned -u nv_rs_gpu_set_probe_identity -u nv_rs_gpu_first_open -u nv_rs_gpu_registry_publish -u nv_rs_gpu_registry_retire -u nv_rs_gpu_mark_removed -u nv_rs_gpu_release_driver -u nv_rs_gpu_retry_cleanup -u nv_rs_gpu_retry_gp100_hbm_restore -u nv_rs_gpu_close -u nv_rs_sysmem_state_size -u nv_rs_sysmem_state_align -u nv_rs_sysmem_prepare -u nv_rs_sysmem_allocate -u nv_rs_sysmem_close -u nv_rs_rmapi_state_size -u nv_rs_rmapi_state_align -u nv_rs_rmapi_init -u nv_rs_rmapi_gpu_add -u nv_rs_rmapi_gpu_remove -u nv_rs_rmapi_gpu_minor -u nv_rs_rmapi_gpu_open -u nv_rs_rmapi_attach_register_count -u nv_rs_rmapi_attach_register -u nv_rs_rmapi_gpu_needs_registers -u nv_rs_rmapi_gpu_attach_registers -u nv_rs_rmapi_set_host_bridge -u nv_rs_rmapi_set_cpu_info -u nv_rs_rmapi_set_force_p2p -u nv_rs_rmapi_set_gsp_abi -u nv_rs_rmapi_set_driver_version -u nv_rs_rmapi_set_strict_version_check -u nv_rs_rmapi_get_driver_version -u nv_rs_rmapi_gpu_release -u nv_rs_rmapi_register_fd -u nv_rs_rmapi_prepare -u nv_rs_rmapi_nested_plan -u nv_rs_rmapi_execute -u nv_rs_rmapi_copy_fault -u nv_rs_rmapi_close -u nv_rs_rmapi_rc_recover_fault -u nv_rs_gpu_nice_set_policy -u nv_rs_gpu_nice_get_policy -u nv_rs_gpu_nice_refresh_fallbacks -u nv_rs_gpu_nice_get_row -u nv_rs_gpu_nice_active_count -u nv_rs_gpu_nice_has_tracked -u nv_rs_gpu_nice_list_pids -u nv_rs_gpu_nice_query_pid -u nv_rs_gpu_nice_preview_nice -u nv_rs_gpu_nice_set_pid -u nv_rs_gpu_nice_sync_pid -u nv_rs_gpu_nice_drain -u nv_rs_gpu_nice_desktop_comm -u nv_rs_gpu_nice_copy_custom_comms -u nv_rs_alloc_reg_can_admit -u nv_rs_alloc_reg_insert -u nv_rs_alloc_reg_find -u nv_rs_alloc_reg_retain -u nv_rs_alloc_reg_mark_freed_and_put -u nv_rs_alloc_reg_put_ptr -u nv_rs_alloc_reg_for_each -u nv_rs_alloc_reg_total_pages -u nv_rs_alloc_reg_pop_any -u nv_rs_alloc_reg_release_storage -u nv_rs_dma_reg_find -u nv_rs_dma_reg_insert -u nv_rs_dma_reg_pop_for_alloc -u nv_rs_dma_reg_pop_for_dev -u nv_rs_file_map_insert_alloc -u nv_rs_file_map_insert_io -u nv_rs_file_map_get -u nv_rs_file_map_remove -u nv_rs_gpu_slot_activate -u nv_rs_gpu_slot_deactivate -u nv_rs_gpu_slot_is_live_gen -u nv_rs_gpu_slot_set_numa -u nv_rs_gpu_slot_validate_bar0 -u nv_rs_gpu_slot_validate_bar1 -u nv_rs_modeset_register_gpu -u nv_rs_modeset_unregister_gpu -u nv_rs_sysmem_pool_push -u nv_rs_sysmem_pool_pop -u nv_rs_sysmem_pool_pop_for_trim -u nv_rs_sysmem_pool_len -u nv_rs_sysmem_pool_release_storage -o "$OUT/rust_backing.o_binary"
|
||||
ld -r --gc-sections --whole-archive "$OUT/libvm_rust_backing.kernel.a" --no-whole-archive -u vm_rust_backing -u vm_rust_backing_size -u vm_rust_backing_align -u vm_gpu_lease_size -u vm_gpu_lease_acquire -u vm_gpu_lease_ready -u vm_gpu_lease_drop -u vm_gpu_registers_size -u vm_gpu_registers_acquire -u vm_gpu_registers_read -u vm_gpu_registers_write -u vm_gpu_registers_drop -u vm_gpu_locked_size -u vm_gpu_locked_acquire -u vm_cold_msi_config -u vm_gpu_locked_read -u vm_gpu_locked_write -u vm_gpu_locked_drop -u vm_gpu_waited_acquire -u vm_native_memory -u vm_gpu_registry_probe -u vm_gpu_registry_removed -u vm_nano_timer -u vm_os_event -u vm_device_software -u vm_first_open_cpu_state -u vm_software_manager_init -u vm_software_manager_shutdown -u vm_nano_size -u vm_nano_hold -u vm_nano_removed -u vm_host_services -u vm_host_irq_services -u vm_thread_state -u nv_rs_gpu_state_size -u nv_rs_gpu_state_align -u nv_rs_gpu_prepare_owned -u nv_rs_gpu_set_probe_identity -u nv_rs_gpu_first_open -u nv_rs_gpu_registry_publish -u nv_rs_gpu_registry_retire -u nv_rs_gpu_mark_removed -u nv_rs_gpu_release_driver -u nv_rs_gpu_retry_cleanup -u nv_rs_gpu_retry_gp100_hbm_restore -u nv_rs_gpu_close -u nv_rs_sysmem_state_size -u nv_rs_sysmem_state_align -u nv_rs_sysmem_prepare -u nv_rs_sysmem_allocate -u nv_rs_sysmem_close -u nv_rs_rmapi_state_size -u nv_rs_rmapi_state_align -u nv_rs_rmapi_init -u nv_rs_rmapi_gpu_add -u nv_rs_rmapi_gpu_remove -u nv_rs_rmapi_gpu_minor -u nv_rs_rmapi_gpu_open -u nv_rs_rmapi_attach_register_count -u nv_rs_rmapi_attach_register -u nv_rs_rmapi_gpu_needs_registers -u nv_rs_rmapi_gpu_attach_registers -u nv_rs_rmapi_set_host_bridge -u nv_rs_rmapi_set_cpu_info -u nv_rs_rmapi_set_force_p2p -u nv_rs_rmapi_set_gsp_abi -u nv_rs_rmapi_set_driver_version -u nv_rs_rmapi_set_strict_version_check -u nv_rs_rmapi_get_driver_version -u nv_rs_rmapi_gpu_release -u nv_rs_rmapi_register_fd -u nv_rs_rmapi_prepare -u nv_rs_rmapi_nested_plan -u nv_rs_rmapi_execute -u nv_rs_rmapi_copy_fault -u nv_rs_rmapi_close -u nv_rs_rmapi_rc_recover_fault -u nv_rs_gpu_nice_set_policy -u nv_rs_gpu_nice_get_policy -u nv_rs_gpu_nice_refresh_fallbacks -u nv_rs_gpu_nice_get_row -u nv_rs_gpu_nice_active_count -u nv_rs_gpu_nice_has_tracked -u nv_rs_gpu_nice_list_pids -u nv_rs_gpu_nice_query_pid -u nv_rs_gpu_nice_preview_nice -u nv_rs_gpu_nice_set_pid -u nv_rs_gpu_nice_sync_pid -u nv_rs_gpu_nice_drain -u nv_rs_gpu_nice_desktop_comm -u nv_rs_gpu_nice_copy_custom_comms -u nv_rs_alloc_reg_can_admit -u nv_rs_alloc_reg_insert -u nv_rs_alloc_reg_find -u nv_rs_alloc_reg_retain -u nv_rs_alloc_reg_mark_freed_and_put -u nv_rs_alloc_reg_put_ptr -u nv_rs_alloc_reg_for_each -u nv_rs_alloc_reg_total_pages -u nv_rs_alloc_reg_pop_any -u nv_rs_alloc_reg_release_storage -u nv_rs_dma_reg_find -u nv_rs_dma_reg_insert -u nv_rs_dma_reg_pop_for_alloc -u nv_rs_dma_reg_pop_for_dev -u nv_rs_file_map_insert_alloc -u nv_rs_file_map_insert_io -u nv_rs_file_map_get -u nv_rs_file_map_remove -u nv_rs_gpu_slot_activate -u nv_rs_gpu_slot_deactivate -u nv_rs_gpu_slot_is_live_gen -u nv_rs_gpu_slot_set_numa -u nv_rs_gpu_slot_validate_bar0 -u nv_rs_gpu_slot_validate_bar1 -u nv_rs_modeset_register_gpu -u nv_rs_modeset_unregister_gpu -u nv_rs_sysmem_pool_push -u nv_rs_sysmem_pool_pop -u nv_rs_sysmem_pool_pop_for_trim -u nv_rs_sysmem_pool_len -u nv_rs_sysmem_pool_release_storage -u nv_rs_sysmem_make_uncached -u nv_rs_sysmem_may_release -o "$OUT/rust_backing.o_binary"
|
||||
# --strip-debug: with -C lto=fat the object carries core's DWARF, which pahole cannot encode
|
||||
# when the kernel builds module BTF (same failure #42 fixed for the production module).
|
||||
objcopy --strip-debug --remove-section=.llvmbc --remove-section=.llvmcmd --remove-section=.eh_frame --remove-section=.eh_frame_hdr --remove-section=.gcc_except_table "$OUT/rust_backing.o_binary"
|
||||
|
||||
Reference in New Issue
Block a user