rmapi_node: no write-back alias when a pinned range maps uncached system memory

Since da41c9ca a class 0x3e allocation of cache type 1 or 2 has an uncached kernel mapping and UC/WC user
mappings. A class 0x71 object can pin a range of such a user mapping. Its record kept cache type 0, so its
vmap (PAGE_KERNEL) and its user mappings were write-back over pages whose linear mapping is uncached
(CodeRabbit on #388).

Stock gives every pinned range NV_MEMORY_UNCACHED (nv_register_user_pages, 580.178.04 nv.c:3390), so its
kernel mapping is PAGE_KERNEL_NOCACHE (nv_alloc_kernel_mapping, nv.c:3762-3763 -> nv-vm.c:647) and its user
mappings are uncached (nv_encode_caching). It never maps such pages write-back.

Changes:
- nv_rs_rmapi_os_lock_user_pages pins under the mmap read lock with pin_user_pages, as os_lock_user_pages
  does (580.178.04 os-mlock.c:252-296), instead of pin_user_pages_fast. Under the same lock it looks at the
  areas of the range. If one is a user mapping of this node whose allocation has an uncached kernel mapping,
  the record gets cache type 1 and kernel_uc: vmap PAGE_KERNEL_NOCACHE, user mappings pgprot_noncached.
  Other ranges stay cached, as before. The pages' attribute stays their allocation's.
- nv_rs_rmapi_mmap sets vm_private_data to the allocation, as stock sets NV_VMA_PRIVATE(vma) = at
  (nv-mmap.c:706). The area holds its file, whose mapping context holds the allocation.
- pin_user_pages lost its vmas argument in 6.5. kernel-open/compat_probes.mk defines
  NV_RS_PIN_USER_PAGES_HAS_VMAS from the declaration in mm.h, as stock's NV_PIN_USER_PAGES_HAS_ARGS_VMAS
  (nv-mm.h:53-59). docs/KERNEL_COMPAT.md lists it.
- EDU guest: rmapi_start pins 16 pages of an anonymous mapping of the insmod process, writes one byte
  through the kernel mapping of the pinned range and reads it back from user space, then checks that the
  unmapped range is refused with 0x1e. With the expected byte changed, the module load fails with
  "NV_RS_VM_FAIL rmapi pinned user pages: -71" as it should.

Not tested: a pinned range inside a UC/WC mapping of this node. The guest's user-space RM API test stops at
rmapi_user_test.c:318 on this host (master too) before its memory slice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ChrisMythos
2026-12-03 11:17:42 +01:00
parent da41c9cadf
commit d3a6cb7a3e
4 changed files with 91 additions and 16 deletions

View File

@ -21,6 +21,7 @@ Where stock NVIDIA `580.178.04` avoids `#if LINUX_VERSION_CODE` guards altogethe
| `fb_is_primary_device()` → `video_is_primary_device()` in `<asm/video.h>` | declaration in `asm/video.h` (**6.9**); before it `vga_default_device()` | `ga10x_adapter.c` (via `NV_RS_HAVE_VIDEO_IS_PRIMARY_DEVICE` from `kernel-open/Kbuild`) |
| `pci_resize_resource(..., exclude_bars)` | declaration in `pci.h` (6.19, backported to 6.18 stable), else **6.19** / `UTS_UBUNTU_RELEASE_ABI` | `pci_adapter.c` ×2 (via `NV_RS_PCI_RESIZE_EXCLUDE_BARS` from `kernel-open/Kbuild`) |
| `drm_driver.date` (field removed) | **6.14** | `nvidia-drm/nvidia_rs_drm.c` |
| `pin_user_pages(..., vmas)` → without `vmas` | declaration in `mm.h` (**6.5**), as stock's `NV_PIN_USER_PAGES_HAS_ARGS_VMAS` | `rmapi_node.c` (via `NV_RS_PIN_USER_PAGES_HAS_VMAS` from `kernel-open/compat_probes.mk`) |
- Host-fixture builds (`NV_RS_*_HOST_FIXTURE`) do not pull `<linux/version.h>`. `tests/irq-mock/irq_mock.h` and `tests/rom-fw-mock/rom_fw_mock.h` define `LINUX_VERSION_CODE 0` and `KERNEL_VERSION(a,b,c)` so the guards preprocess cleanly on the host-fixture side.

View File

@ -22,6 +22,10 @@ endif
ifeq ($(shell grep -qs 'vma_start_write' $(NV_RS_MM_HEADERS) && echo y),y)
ccflags-y += -DNV_RS_HAVE_VMA_START_WRITE
endif
# pin_user_pages() lost its `vmas` argument in 6.5 (stock conftest: NV_PIN_USER_PAGES_HAS_ARGS_VMAS)
ifeq ($(shell grep -A2 'long pin_user_pages.unsigned long start' $(srctree)/include/linux/mm.h 2>/dev/null | grep -q 'vm_area_struct' && echo y),y)
ccflags-y += -DNV_RS_PIN_USER_PAGES_HAS_VMAS
endif
# video_is_primary_device() moved to <asm/video.h> in 6.9 (fb_is_primary_device() before)
ifeq ($(shell grep -qs 'video_is_primary_device' $(srctree)/arch/$(SRCARCH)/include/asm/video.h $(srctree)/include/asm-generic/video.h && echo y),y)

View File

@ -540,7 +540,8 @@ static struct nv_rs_rmapi_file *nv_rs_rmapi_context(struct file *file, int gpu)
* 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.
* stays cached. Pinned user pages (0x71) whose range maps such an allocation get uncached
* kernel and user mappings.
*
* 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
@ -614,11 +615,15 @@ struct nv_rs_rmapi_alloc {
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. */
* write-back attribute back before they are freed or pooled. For pinned user pages (`user_pinned`,
* nv_rs_rmapi_os_lock_user_pages): the range maps pages of such an allocation, `cache` is uncached and
* only the mappings follow it; the pages' attribute stays their allocation's. */
bool kernel_uc;
void *kernel;
struct page *pages[];
};
/* The user mappings of system memory (nv_rs_rmapi_mmap); vm_private_data is the allocation. */
static const struct vm_operations_struct nv_rs_rmapi_vm_operations;
static unsigned long nv_rs_rmapi_gpu_cache_flushes;
static atomic64_t nv_rs_rmapi_kvalloc_live = ATOMIC64_INIT(0);
@ -1771,23 +1776,47 @@ static void nv_rs_rmapi_alloc_put(struct nv_rs_rmapi_alloc *alloc)
nv_rs_rmapi_alloc_destroy(alloc);
}
/* NV_PIN_USER_PAGES (580.178.04 nv-mm.h:53-59): pin_user_pages lost its vmas argument in 6.5. */
#if defined(NV_RS_PIN_USER_PAGES_HAS_VMAS)
#define nv_rs_pin_user_pages(start, nr_pages, gup_flags, pages) \
pin_user_pages(start, nr_pages, gup_flags, pages, NULL)
#else
#define nv_rs_pin_user_pages pin_user_pages
#endif
/* Whether [start, end) of `mm` holds a user mapping (nv_rs_rmapi_mmap) of an allocation whose kernel mapping was
* made uncached. Called with the mmap lock held: the areas cannot change, and each holds its file, whose mapping
* context holds the allocation. */
static bool nv_rs_rmapi_range_maps_uncached(struct mm_struct *mm, unsigned long start, unsigned long end)
{
struct vm_area_struct *vma;
for (vma = find_vma(mm, start); vma && vma->vm_start < end; vma = find_vma(mm, vma->vm_end)) {
const struct nv_rs_rmapi_alloc *owner = vma->vm_private_data;
if (vma->vm_ops == &nv_rs_rmapi_vm_operations && owner && owner->kernel_uc)
return true;
}
return false;
}
/* Pins `count` user-space 4 KiB pages starting at `address` for class 0x71
* (NV01_MEMORY_SYSTEM_OS_DESCRIPTOR) so UVM/GPU page tables can reference their physical
* addresses directly. */
u32 nv_rs_rmapi_os_lock_user_pages(u64 address, u64 count, bool writable, u64 *token)
{
struct nv_rs_rmapi_alloc *alloc;
struct mm_struct *mm = current->mm;
u64 pinned = 0;
u64 pool;
u64 tok = 0;
bool ok;
bool ok, uncached = false;
unsigned int gup_flags = writable ? FOLL_WRITE : 0;
#ifdef FOLL_LONGTERM
/* Match stock 580.178.04 os-mlock.c: long-term GPU DMA pins must migrate out of CMA/movable zones. */
gup_flags |= FOLL_LONGTERM;
#endif
if (!count || !token || (address & (PAGE_SIZE - 1)))
if (!count || !token || !mm || (address & (PAGE_SIZE - 1)))
return NV_RS_STATUS_INVALID_ARGUMENT;
pool = nv_rs_sysmem_pool_len();
mutex_lock(&nv_rs_rmapi_alloc_lock);
@ -1801,22 +1830,35 @@ u32 nv_rs_rmapi_os_lock_user_pages(u64 address, u64 count, bool writable, u64 *t
if (!alloc)
return NV_RS_STATUS_NO_MEMORY;
/* As os_lock_user_pages (580.178.04 os-mlock.c:252-296): pinned under the mmap lock, which also keeps the areas
* the pages were pinned through for the check below. */
mmap_read_lock(mm);
while (pinned < count) {
u64 remain = count - pinned;
int chunk = remain > 1024 ? 1024 : (int)remain;
int got = pin_user_pages_fast((unsigned long)(address + (pinned << PAGE_SHIFT)),
chunk, gup_flags, &alloc->pages[pinned]);
if (got <= 0) {
if (pinned > 0)
unpin_user_pages(alloc->pages, pinned);
kvfree(alloc);
return NV_RS_STATUS_INVALID_ADDRESS;
}
unsigned long chunk = remain > 1024 ? 1024 : (unsigned long)remain;
long got = nv_rs_pin_user_pages((unsigned long)(address + (pinned << PAGE_SHIFT)),
chunk, gup_flags, &alloc->pages[pinned]);
if (got <= 0)
break;
pinned += (u64)got;
}
if (pinned == count)
uncached = nv_rs_rmapi_range_maps_uncached(mm, (unsigned long)address,
(unsigned long)(address + (count << PAGE_SHIFT)));
mmap_read_unlock(mm);
if (pinned < count) {
if (pinned > 0)
unpin_user_pages(alloc->pages, pinned);
kvfree(alloc);
return NV_RS_STATUS_INVALID_ADDRESS;
}
alloc->count = count;
alloc->cache = 0;
/* No write-back alias of uncached pages: a range that maps such an allocation gets uncached kernel and user
* mappings (NV_MEMORY_UNCACHED). The original gives every pinned range that type (nv_register_user_pages,
* 580.178.04 nv.c:3390; nv_alloc_kernel_mapping, nv.c:3762-3763); here other ranges stay cached. */
alloc->cache = uncached ? 1 : 0;
alloc->kernel_uc = uncached;
alloc->user_pinned = true;
alloc->writable = writable;
mutex_lock(&nv_rs_rmapi_alloc_lock);
@ -3097,8 +3139,9 @@ static const struct vm_operations_struct nv_rs_rmapi_vm_operations = {
*
* 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 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.
* is applied only when its kernel mapping was made uncached (for pinned user pages: when
* their range maps such an allocation), 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. */
@ -3247,6 +3290,8 @@ static int nv_rs_rmapi_mmap(struct file *file, struct vm_area_struct *vma)
: pgprot_noncached(vma->vm_page_prot);
nv_rs_vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
vma->vm_ops = &nv_rs_rmapi_vm_operations;
/* NV_VMA_PRIVATE(vma) = at (580.178.04 nv-mmap.c:706); read by nv_rs_rmapi_range_maps_uncached */
vma->vm_private_data = alloc;
for (index = mapping.page_index; index < mapping.page_index + pages; index++) {
/* On failure the caller removes the area and with it the pages inserted so far. */
if (vm_insert_page(vma, address, alloc->pages[index]))

View File

@ -7,6 +7,8 @@
#include <linux/jiffies.h>
#include <linux/iommu.h>
#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/kthread.h>
#include <linux/fs.h>
@ -401,6 +403,28 @@ static int rmapi_sysmem_check(struct pci_dev *pdev,u32 cache)
if(e)pr_err("NV_RS_VM_FAIL rmapi system memory (cache type %u): %d\n",cache,e);
return e;
}
/* Pinned user pages (class 0x71) of anonymous memory, pinned under the mmap lock as os_lock_user_pages does: the
* kernel mapping of the pinned range writes the process's own pages; a range that is not mapped is refused with
* NV_ERR_INVALID_ADDRESS (0x1e). Module init runs in the insmod process, whose address space this borrows. */
u32 nv_rs_rmapi_os_lock_user_pages(u64 address,u64 count,bool writable,u64 *token);
void nv_rs_rmapi_os_unlock_user_pages(u64 token);
static int rmapi_user_pages_check(void)
{
unsigned long user=vm_mmap(NULL,0,16*PAGE_SIZE,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,0);
u64 token=0;u8 byte=0x3c;int e=0;
if(IS_ERR_VALUE(user)){pr_err("NV_RS_VM_FAIL rmapi pinned user pages: %ld\n",(long)user);return (int)(long)user;}
if(nv_rs_rmapi_os_lock_user_pages(user,16,true,&token))e=-ENOMEM;
if(!e&&nv_rs_rmapi_os_kernel_write(token,9*PAGE_SIZE+1,&byte,1))e=-EIO;
byte=0;
if(!e&&(copy_from_user(&byte,(const void __user *)(user+9*PAGE_SIZE+1),1)||byte!=0x3c))e=-EPROTO;
if(token)nv_rs_rmapi_os_unlock_user_pages(token);
token=0;
vm_munmap(user,16*PAGE_SIZE);
if(!e&&nv_rs_rmapi_os_lock_user_pages(user,16,true,&token)!=0x1e)e=-EFAULT;
if(token)nv_rs_rmapi_os_unlock_user_pages(token);
if(e)pr_err("NV_RS_VM_FAIL rmapi pinned user pages: %d\n",e);
return e;
}
static int rmapi_start(void)
{
int e=nv_rs_rmapi_node_init();
@ -412,6 +436,7 @@ static int rmapi_start(void)
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)e=rmapi_user_pages_check();
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;
}