mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
rmapi_node: map uncached and write-combined system memory as stock does
RM allocations libcuda asks for with NVOS32_ATTR_COHERENCY_WRITE_COMBINE (the 50 MiB pushbuffer area of every context, attr 0x5a000000 in a stock 620.73.04 capture) were mapped write-back everywhere: the cache type was recorded and ignored, so user mappings were cached. Stock gives every system-memory allocation that is not NV_MEMORY_CACHED an uncached kernel linear mapping (nv_alloc_system_pages -> nv_set_memory_type(at, NV_FALSE), nv-vm.c:61-138, 1056-1059), maps it to user space with the requested type (nv_encode_caching before nvidia_mmap_sysmem, nv-mmap.c:346-372, 614-620), vmaps it PAGE_KERNEL_NOCACHE (nv_vmap), and restores write-back before the pages are freed. The same now happens here, on x86 (set_pages_array_uc/wb): - nv_rs_rmapi_os_alloc_pages makes the kernel mapping of a non-cached allocation uncached; if that fails the allocation stays cached everywhere, as before. - mmap of such an allocation uses pgprot_writecombine / pgprot_noncached. - vmap of it uses PAGE_KERNEL_NOCACHE, so no cached alias appears. - Pages get write-back again before they are freed or go back to the page pool. GPU PTEs are unchanged: system memory is SYS_COH on this platform in stock too (nvGpuOpsGetExternalAllocAperture). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -22,6 +22,9 @@
|
||||
#include <linux/poll.h>
|
||||
#include <linux/proc_fs.h>
|
||||
#include <linux/scatterlist.h>
|
||||
#ifdef CONFIG_X86
|
||||
#include <asm/set_memory.h>
|
||||
#endif
|
||||
#include <linux/seq_file.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/uaccess.h>
|
||||
@ -278,6 +281,10 @@ struct nv_rs_rmapi_alloc {
|
||||
u32 cache;
|
||||
bool user_pinned;
|
||||
bool writable;
|
||||
/* `cache` is uncached or write-combined and the kernel's own mapping of the pages was made uncached
|
||||
* (nv_rs_rmapi_pages_set_uc): user mappings then carry `cache`, and the pages get their cached type back
|
||||
* before they are freed or pooled. */
|
||||
bool kernel_uc;
|
||||
/* Uses taken by nv_rs_rmapi_os_get_pages (a GSP-RM channel's hold, the RUSD page's
|
||||
* own hold), each a reference in the Rust registry and each released by one more
|
||||
* nv_rs_rmapi_os_free_pages / nv_rs_rmapi_os_unlock_user_pages. While any is left the
|
||||
@ -462,6 +469,26 @@ static inline void nv_rs_proc_init(void) {}
|
||||
static inline void nv_rs_proc_exit(void) {}
|
||||
#endif
|
||||
|
||||
/* nv_set_memory_type (nv-vm.c:61-138), which nv_alloc_system_pages applies to every allocation RM does not want
|
||||
* cached (nv-vm.c:1056-1059): the kernel's mapping of the pages becomes uncached, so a write-combined or uncached
|
||||
* user mapping of them has no cached alias. x86 only, as stock's set_pages_array_* calls (conftest-guarded). */
|
||||
static bool nv_rs_rmapi_pages_set_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
|
||||
}
|
||||
|
||||
/* nv_set_memory_type(at, NV_TRUE) before stock frees the pages (nv_free_system_pages). */
|
||||
static void nv_rs_rmapi_pages_set_wb(struct page **pages, u64 count)
|
||||
{
|
||||
#ifdef CONFIG_X86
|
||||
(void)set_pages_array_wb(pages, (int)count);
|
||||
#endif
|
||||
}
|
||||
|
||||
static struct page *nv_rs_sysmem_pool_pop(void)
|
||||
{
|
||||
struct page *page = NULL;
|
||||
@ -741,6 +768,8 @@ static void nv_rs_rmapi_alloc_destroy(struct nv_rs_rmapi_alloc *alloc)
|
||||
unpin_user_pages_dirty_lock(alloc->pages, alloc->count, alloc->writable);
|
||||
} else {
|
||||
int retain_mb = READ_ONCE(system_memory_pool_retain_mb);
|
||||
if (alloc->kernel_uc)
|
||||
nv_rs_rmapi_pages_set_wb(alloc->pages, alloc->count);
|
||||
for (index = 0; index < alloc->count; index++) {
|
||||
if (retain_mb != 0)
|
||||
nv_rs_sysmem_pool_push(alloc->pages[index]);
|
||||
@ -888,10 +917,16 @@ 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_FALSE) (nv-vm.c:1056-1059). When that fails the
|
||||
* pages stay cached everywhere, user mappings included. */
|
||||
if (cache != 0)
|
||||
alloc->kernel_uc = nv_rs_rmapi_pages_set_uc(alloc->pages, count);
|
||||
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) {
|
||||
if (alloc->kernel_uc)
|
||||
nv_rs_rmapi_pages_set_wb(alloc->pages, count);
|
||||
for (index = 0; index < count; index++)
|
||||
__free_page(alloc->pages[index]);
|
||||
kvfree(alloc);
|
||||
@ -931,6 +966,17 @@ void nv_rs_rmapi_os_free_pages(u64 token)
|
||||
nv_rs_rmapi_alloc_destroy(ptr);
|
||||
}
|
||||
|
||||
/* nv_vmap (nv-vm.c): PAGE_KERNEL for cached memory, PAGE_KERNEL_NOCACHE otherwise, so a kernel mapping of pages
|
||||
* whose linear mapping was made uncached adds no cached alias. */
|
||||
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)
|
||||
@ -944,7 +990,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;
|
||||
}
|
||||
@ -984,7 +1031,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;
|
||||
}
|
||||
@ -1862,6 +1910,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(vma->vm_page_prot, at->cache_type) before nvidia_mmap_sysmem
|
||||
* (nv-mmap.c:614-620, 346-372) */
|
||||
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++) {
|
||||
|
||||
Reference in New Issue
Block a user