// SPDX-License-Identifier: MIT /* Character-device adapter for /dev/nvidia-rs-uvm. User memory is copied here; * all UVM ioctl and VA-space rules live in Rust (src/nvidia/src/uvm). */ #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef CONFIG_COMPAT #include #endif #include "gpu_owner.h" #include "dmabuf_p2p.h" #include "rmapi_node.h" #include "uvm_node.h" #define NV_RS_UVM_MAP_EXTERNAL_ALLOCATION 0x21 struct nv_rs_external_map_diagnostic { const char *stage; s32 gpu_minor; u64 mapped_bytes; }; #define NV_RS_UVM_MM_INITIALIZE 0x4b extern size_t nv_rs_uvm_file_size(void); extern size_t nv_rs_uvm_file_align(void); extern int nv_rs_uvm_file_init(void *file); extern int nv_rs_uvm_route_size(u32 command, size_t *size); extern int nv_rs_uvm_route_status_offset(u32 command, size_t *offset); extern int nv_rs_uvm_file_ioctl(void *file, u32 command, u8 *block, size_t length, bool mm_same, void *mm_other, struct nv_rs_external_map_diagnostic *diagnostic); extern int nv_rs_uvm_file_mmap(void *file, void *vma, u64 start, u64 end, u64 offset, bool shared_rw, bool same_mm); extern void nv_rs_uvm_file_vma_closed(void *file, u64 start); extern void nv_rs_uvm_file_release(void *file); extern void nv_rs_uvm_node_cleanup(void); extern void nv_rs_uvm_drain_deferred_frees(void); extern u32 nv_rs_uvm_service_replayable_faults(u32 *pages_mapped_out, u32 *replays_out, u32 *cancelled_gpu_id_out, u64 *cancelled_inst_ptr_out, u64 *cancelled_va_out); static DEFINE_MUTEX(nv_rs_uvm_lock); /* rmapi_node.c's lock, taken BEFORE nv_rs_uvm_lock where GPU state changes: UVM calls reach the RM objects of a GPU * run by GSP-RM (src/nvidia/kernel/gsp_route.rs) and GSP-RM's RPC channel, which the RM API node uses under its lock. * The RM API node pins user pages under its lock (nv_rs_rmapi_os_lock_user_pages: rmapi -> mmap_lock), and the kernel * calls our .mmap / vm_ops->close under mmap_lock (mmap_lock -> uvm), so taking the UVM lock first and then the RM API * lock (uvm -> rmapi) closes a cycle that lockdep reports and that deadlocks a multi-threaded CUDA process. Order: * rmapi, then uvm; the fault worker trylocks both in that order too. */ void nv_rs_rmapi_node_lock(void); int nv_rs_rmapi_node_trylock(void); void nv_rs_rmapi_node_unlock(void); static void nv_rs_uvm_drain_work_fn(struct work_struct *work) { (void)work; nv_rs_rmapi_node_lock(); mutex_lock(&nv_rs_uvm_lock); nv_rs_uvm_drain_deferred_frees(); mutex_unlock(&nv_rs_uvm_lock); nv_rs_rmapi_node_unlock(); } static DECLARE_WORK(nv_rs_uvm_drain_work, nv_rs_uvm_drain_work_fn); void nv_rs_uvm_schedule_deferred_drain(void) { schedule_work(&nv_rs_uvm_drain_work); } static atomic_t nv_rs_uvm_fault_poll_active = ATOMIC_INIT(0); static void nv_rs_uvm_fault_work_fn(struct work_struct *work); static DECLARE_DELAYED_WORK(nv_rs_uvm_fault_work, nv_rs_uvm_fault_work_fn); static const struct file_operations nv_rs_uvm_fops; /* Coherent zeroed 4 KiB sysmem page helpers for the 5-level GV100 GPU page-table tree * and UVM semaphore pools, plus 16 KiB contiguous ring for GV100 replayable faults. */ void *nv_rs_uvm_dev_get(u32 gpu_id); void nv_rs_uvm_dev_put(void *dev_ptr); int nv_rs_uvm_alloc_pt_page_for_dev(void *dev_ptr, u64 *dma_out, void **cpu_out); void nv_rs_uvm_free_pt_page_for_dev(void *dev_ptr, void *cpu_ptr, u64 dma_addr); int nv_rs_uvm_alloc_pt_page(u64 *phys_out, void **cpu_out); void nv_rs_uvm_free_pt_page(void *cpu_ptr); int nv_rs_uvm_alloc_fault_ring(u32 gpu_id, u64 *dma_out, void **cpu_out, void **dev_out, u32 *entries_out); void nv_rs_uvm_free_fault_ring(void *dev_ptr, void *cpu_ptr, u64 dma_addr); u64 nv_rs_uvm_cpu_to_phys(void *cpu_ptr); u64 nv_rs_uvm_cpu_to_dma(u32 gpu_id, void *cpu_ptr); void nv_rs_uvm_fault_poll_start(void); void nv_rs_uvm_fault_poll_stop(void); void nv_rs_uvm_write64(void *cpu_ptr, u32 offset, u64 val); void nv_rs_uvm_write32(void *cpu_ptr, u32 offset, u32 val); int nv_rs_uvm_insert_page(void *vma_ptr, u64 addr, void *cpu_ptr); void *nv_rs_uvm_kvzalloc(size_t size); void nv_rs_uvm_kvfree(void *ptr); static void nv_rs_uvm_fault_work_fn(struct work_struct *work) { u32 mapped = 0; u32 replays = 0; u32 raw = 0; u32 cancelled_gpu_id = 0; u64 cancelled_inst_ptr = 0; u64 cancelled_va = 0; (void)work; if (!atomic_read(&nv_rs_uvm_fault_poll_active)) return; /* only tried: the RM API side may stop this worker while it holds the lock (same order as elsewhere) */ if (nv_rs_rmapi_node_trylock()) { u32 chid = 0, tsg_id = 0, diag[3] = {0, 0, 0}, notified = 0; if (mutex_trylock(&nv_rs_uvm_lock)) { raw = nv_rs_uvm_service_replayable_faults(&mapped, &replays, &cancelled_gpu_id, &cancelled_inst_ptr, &cancelled_va); mutex_unlock(&nv_rs_uvm_lock); } if (cancelled_inst_ptr != 0) { extern u32 nv_rs_rmapi_rc_recover_fault(u32 gpu_id, u64 inst_ptr, u32 xid_code, u32 *ch_out, u32 *tsg_out, u32 *diag_out); notified = nv_rs_rmapi_rc_recover_fault(cancelled_gpu_id, cancelled_inst_ptr, 31, &chid, &tsg_id, diag); } nv_rs_rmapi_node_unlock(); if (raw > 0) { pr_info("nvidia_rs: UVM replayable fault batch serviced: raw=%u mapped=%u replays=%u\n", raw, mapped, replays); } if (cancelled_inst_ptr != 0) { pr_err("NVRM: Xid (PCI:%04x:%02x:%02x.0): 31, pid=%d, name=%s, Channel %08x, TSG %u, FaultVA 0x%llx (inst=0x%llx notified=%u gr_st=0x%x ctx=0x%x eng=0x%x)\n", (cancelled_gpu_id >> 16) & 0xffffu, (cancelled_gpu_id >> 8) & 0xffu, cancelled_gpu_id & 0xffu, task_pid_nr(current), current->comm, chid, tsg_id, (unsigned long long)cancelled_va, (unsigned long long)cancelled_inst_ptr, notified, diag[0], diag[1], diag[2]); } } if (atomic_read(&nv_rs_uvm_fault_poll_active)) schedule_delayed_work(&nv_rs_uvm_fault_work, 1); } void nv_rs_uvm_fault_poll_start(void) { atomic_set(&nv_rs_uvm_fault_poll_active, 1); schedule_delayed_work(&nv_rs_uvm_fault_work, 1); } void nv_rs_uvm_fault_poll_stop(void) { atomic_set(&nv_rs_uvm_fault_poll_active, 0); cancel_delayed_work_sync(&nv_rs_uvm_fault_work); } /* Mirrors `UvmCpuDmaRecord` in `src/nvidia/src/rmapi/alloc_registry.rs`. */ struct nv_rs_uvm_cpu_dma_record { void *cpu_ptr; struct page *page; u32 gpu_id; struct device *dev; u64 dma; }; extern bool nv_rs_uvm_dma_find(void *cpu_ptr, u32 gpu_id, u64 *dma_out); extern int nv_rs_uvm_dma_insert(void *cpu_ptr, struct page *page, u32 gpu_id, struct device *dev, u64 dma, u64 *existing_dma_out); extern bool nv_rs_uvm_dma_pop_cpu(void *cpu_ptr, struct nv_rs_uvm_cpu_dma_record *out); extern u32 nv_rs_uvm_dma_detach_dev(struct device *dev); extern bool nv_rs_uvm_dma_pop_detached(u32 *head, struct nv_rs_uvm_cpu_dma_record *out); extern u32 nv_rs_uvm_dma_pop_batch(struct nv_rs_uvm_cpu_dma_record *out, u32 capacity); extern void nv_rs_uvm_dma_release_storage(void); /* Leaf mutex serializing Rust's `UvmCpuDmaLedger` (never acquires uvm or rmapi locks). */ static DEFINE_MUTEX(nv_rs_uvm_cpu_dma_lock); void *nv_rs_uvm_dev_get(u32 gpu_id) { struct device *dev = nv_rs_rmapi_gpu_device_get(gpu_id); if (dev) nv_rs_rmapi_gpu_negotiate_dma(gpu_id, nv_rs_rmapi_gpu_dma_bits(gpu_id)); return dev; } void nv_rs_uvm_dev_put(void *dev_ptr) { if (dev_ptr) put_device(dev_ptr); } int nv_rs_uvm_alloc_pt_page_for_dev(void *dev_ptr, u64 *dma_out, void **cpu_out) { struct device *dev = dev_ptr; dma_addr_t dma = 0; void *cpu; if (!dma_out || !cpu_out) return -EINVAL; if (dev) { cpu = dma_alloc_coherent(dev, PAGE_SIZE, &dma, GFP_KERNEL); if (!cpu) return -ENOMEM; memset(cpu, 0, PAGE_SIZE); *dma_out = (u64)dma; *cpu_out = cpu; return 0; } return nv_rs_uvm_alloc_pt_page(dma_out, cpu_out); } void nv_rs_uvm_free_pt_page_for_dev(void *dev_ptr, void *cpu_ptr, u64 dma_addr) { struct device *dev = dev_ptr; if (!cpu_ptr) return; if (dev) dma_free_coherent(dev, PAGE_SIZE, cpu_ptr, (dma_addr_t)dma_addr); else free_page((unsigned long)cpu_ptr); } int nv_rs_uvm_alloc_fault_ring(u32 gpu_id, u64 *dma_out, void **cpu_out, void **dev_out, u32 *entries_out) { struct device *dev; dma_addr_t dma = 0; void *cpu; struct page *p; if (!dma_out || !cpu_out || !dev_out || !entries_out) return -EINVAL; dev = nv_rs_uvm_dev_get(gpu_id); if (dev) { cpu = dma_alloc_coherent(dev, 4 * PAGE_SIZE, &dma, GFP_KERNEL); if (!cpu) { put_device(dev); return -ENOMEM; } memset(cpu, 0, 4 * PAGE_SIZE); *dma_out = (u64)dma; *cpu_out = cpu; *dev_out = dev; *entries_out = (4 * PAGE_SIZE) / 32; return 0; } /* Order 2 = 4 pages = 16 KiB = 512 x 32-byte NVC369_BUF_ENTRY slots. */ p = alloc_pages(GFP_KERNEL | __GFP_ZERO, 2); if (!p) return -ENOMEM; *dma_out = (u64)PFN_PHYS(page_to_pfn(p)); *cpu_out = page_address(p); *dev_out = NULL; *entries_out = (4 * PAGE_SIZE) / 32; return 0; } void nv_rs_uvm_free_fault_ring(void *dev_ptr, void *cpu_ptr, u64 dma_addr) { struct device *dev = dev_ptr; if (!cpu_ptr) { if (dev) put_device(dev); return; } if (dev) { dma_free_coherent(dev, 4 * PAGE_SIZE, cpu_ptr, (dma_addr_t)dma_addr); put_device(dev); } else { __free_pages(virt_to_page(cpu_ptr), 2); } } u64 nv_rs_uvm_cpu_to_phys(void *cpu_ptr) { if (!cpu_ptr) return 0; return (u64)PFN_PHYS(page_to_pfn(virt_to_page(cpu_ptr))); } u64 nv_rs_uvm_cpu_to_dma(u32 gpu_id, void *cpu_ptr) { struct device *dev; struct page *page; dma_addr_t dma; u64 existing = 0; int rc; if (!cpu_ptr) return 0; mutex_lock(&nv_rs_uvm_cpu_dma_lock); if (nv_rs_uvm_dma_find(cpu_ptr, gpu_id, &existing)) { mutex_unlock(&nv_rs_uvm_cpu_dma_lock); return existing; } mutex_unlock(&nv_rs_uvm_cpu_dma_lock); page = virt_to_page(cpu_ptr); dev = nv_rs_uvm_dev_get(gpu_id); if (!dev) return (u64)PFN_PHYS(page_to_pfn(page)); /* Hold a page reference across the lifetime of the DMA mapping so concurrent * page release cannot reuse the page before dma_unmap_page() completes. */ get_page(page); dma = dma_map_page(dev, page, 0, PAGE_SIZE, DMA_BIDIRECTIONAL); if (dma_mapping_error(dev, dma)) { put_page(page); put_device(dev); return 0; } mutex_lock(&nv_rs_uvm_cpu_dma_lock); rc = nv_rs_uvm_dma_insert(cpu_ptr, page, gpu_id, dev, (u64)dma, &existing); mutex_unlock(&nv_rs_uvm_cpu_dma_lock); if (rc == 0) return (u64)dma; dma_unmap_page(dev, dma, PAGE_SIZE, DMA_BIDIRECTIONAL); put_page(page); put_device(dev); return rc == 1 ? existing : 0; } static void nv_rs_uvm_unmap_cpu_dma(void *cpu_ptr) { struct nv_rs_uvm_cpu_dma_record rec; if (!cpu_ptr) return; for (;;) { bool found; mutex_lock(&nv_rs_uvm_cpu_dma_lock); found = nv_rs_uvm_dma_pop_cpu(cpu_ptr, &rec); mutex_unlock(&nv_rs_uvm_cpu_dma_lock); if (!found) break; dma_unmap_page(rec.dev, (dma_addr_t)rec.dma, PAGE_SIZE, DMA_BIDIRECTIONAL); put_page(rec.page); put_device(rec.dev); } } void nv_rs_uvm_unmap_gpu_dev(struct device *dev) { struct nv_rs_uvm_cpu_dma_record rec; u32 dead_head; if (!dev) return; mutex_lock(&nv_rs_uvm_cpu_dma_lock); dead_head = nv_rs_uvm_dma_detach_dev(dev); mutex_unlock(&nv_rs_uvm_cpu_dma_lock); while (dead_head) { bool found; mutex_lock(&nv_rs_uvm_cpu_dma_lock); found = nv_rs_uvm_dma_pop_detached(&dead_head, &rec); mutex_unlock(&nv_rs_uvm_cpu_dma_lock); if (!found) break; dma_unmap_page(rec.dev, (dma_addr_t)rec.dma, PAGE_SIZE, DMA_BIDIRECTIONAL); put_page(rec.page); put_device(rec.dev); } } void nv_rs_uvm_write32(void *cpu_ptr, u32 offset, u32 val) { if (cpu_ptr && offset <= PAGE_SIZE - sizeof(u32)) { smp_wmb(); WRITE_ONCE(*(u32 *)((u8 *)cpu_ptr + offset), val); } } void *nv_rs_uvm_kvzalloc(size_t size) { return kvzalloc(size, GFP_KERNEL); } void nv_rs_uvm_kvfree(void *ptr) { kvfree(ptr); } int nv_rs_uvm_alloc_pt_page(u64 *phys_out, void **cpu_out) { struct page *p; if (!phys_out || !cpu_out) return -EINVAL; p = alloc_page(GFP_KERNEL | __GFP_ZERO); if (!p) return -ENOMEM; *phys_out = (u64)PFN_PHYS(page_to_pfn(p)); *cpu_out = page_address(p); return 0; } void nv_rs_uvm_free_pt_page(void *cpu_ptr) { if (!cpu_ptr) return; nv_rs_uvm_unmap_cpu_dma(cpu_ptr); free_page((unsigned long)cpu_ptr); } void nv_rs_uvm_write64(void *cpu_ptr, u32 offset, u64 val) { if (cpu_ptr && offset <= PAGE_SIZE - sizeof(u64)) { smp_wmb(); WRITE_ONCE(*(u64 *)((u8 *)cpu_ptr + offset), val); } } int nv_rs_uvm_insert_page(void *vma_ptr, u64 addr, void *cpu_ptr) { struct vm_area_struct *vma = vma_ptr; struct page *page; if (!vma || !cpu_ptr) return -EINVAL; page = virt_to_page(cpu_ptr); return vm_insert_page(vma, (unsigned long)addr, page); } struct nv_rs_uvm_handle { void *state; }; static int nv_rs_uvm_open(struct inode *inode, struct file *file) { struct nv_rs_uvm_handle *handle; int err; (void)inode; if (nv_rs_uvm_file_align() > ARCH_KMALLOC_MINALIGN || nv_rs_uvm_file_size() > 4 * 1024 * 1024) return -EINVAL; handle = kzalloc(sizeof(*handle), GFP_KERNEL_ACCOUNT); if (!handle) return -ENOMEM; handle->state = kvzalloc(nv_rs_uvm_file_size(), GFP_KERNEL_ACCOUNT); if (!handle->state) { kfree(handle); return -ENOMEM; } err = nv_rs_uvm_file_init(handle->state); if (err) { kvfree(handle->state); kfree(handle); return err; } file->private_data = handle; return 0; } static int nv_rs_uvm_release(struct inode *inode, struct file *file) { struct nv_rs_uvm_handle *handle = file->private_data; (void)inode; if (!handle) return 0; nv_rs_rmapi_node_lock(); mutex_lock(&nv_rs_uvm_lock); nv_rs_uvm_file_release(handle->state); mutex_unlock(&nv_rs_uvm_lock); nv_rs_rmapi_node_unlock(); kvfree(handle->state); kfree(handle); return 0; } /* nvidia_rs extension (not in NVIDIA's UVM ABI): export the video memory behind a CUDA device pointer as a * dma-buf fd. libcuda's own cuMemGetHandleForAddressRange(DMA_BUF_FD) refuses this GPU in user space * (CU_DEVICE_ATTRIBUTE_DMA_BUF_SUPPORTED=0) before asking the kernel; this asks the kernel directly. Issued on * the process's own /dev/nvidia-uvm VA-space fd (the one libcuda opened): [va, va + size) must lie inside one * cuMemAlloc / cuMemMap external mapping of it, page aligned, and the memory's RM client must belong to rm_fd * (an RM file the caller holds, as for NV_ESC_EXPORT_TO_DMABUF_FD). status is the NV_STATUS (0 = fd is valid). */ #define NV_RS_UVM_EXPORT_DMABUF 0x4e520001u struct nv_rs_uvm_export_dmabuf_params { u64 va; /* IN: CUDA device pointer */ u64 size; /* IN: bytes */ u32 flags; /* IN: bit 0 = the dma-buf may be mmap()ed by the CPU (write-combined BAR1); others 0 */ s32 fd; /* OUT */ u32 status; /* OUT */ s32 rm_fd; /* IN: the RM control (or device) fd whose client owns the memory, as UVM's rmCtrlFd */ }; extern int nv_rs_uvm_file_external_object(void *file, u64 va, u64 len, u32 *client, u32 *object, u64 *offset); extern int nv_rs_rmapi_export_dmabuf_object(int rm_fd, u32 h_client, u32 h_memory, u64 offset, u64 size, bool allow_mmap, u32 *status_out, struct nv_rs_dmabuf_pending *pending); static long nv_rs_uvm_export_dmabuf(struct nv_rs_uvm_handle *handle, unsigned long arg) { struct nv_rs_uvm_export_dmabuf_params p; struct nv_rs_dmabuf_pending pending = { 0 }; u32 client = 0, object = 0; u64 offset = 0; int rc; if (!handle) return -EBADFD; if (copy_from_user(&p, (void __user *)arg, sizeof(p))) return -EFAULT; p.fd = -1; p.status = 0; if (p.flags & ~1u) { p.status = 0x0000001fu; /* NV_ERR_INVALID_ARGUMENT */ goto out; } /* the lookup only reads this file's VA space; the export resolves the memory again under * the RM API lock, so a mapping that changes in between exports memory the caller still * owns or fails */ if (mutex_lock_interruptible(&nv_rs_uvm_lock)) return -ERESTARTSYS; rc = nv_rs_uvm_file_external_object(handle->state, p.va, p.size, &client, &object, &offset); mutex_unlock(&nv_rs_uvm_lock); if (rc != 0) { p.status = 0x0000001eu; /* NV_ERR_INVALID_ADDRESS: no external mapping covers [va, va + size) */ } else { rc = nv_rs_rmapi_export_dmabuf_object(p.rm_fd, client, object, offset, p.size, p.flags & 1u, &p.status, &pending); if (rc == -ERESTARTSYS) return rc; /* a signal, not a bad argument: restart the call */ if (rc >= 0) p.fd = rc; } if (p.status != 0) pr_info_ratelimited("nvidia_rs: UVM export dmabuf va=0x%llx size=0x%llx (client 0x%x memory 0x%x offset 0x%llx): status 0x%x\n", p.va, p.size, client, object, offset, p.status); out: /* the fd is installed only once the reply carrying it has arrived */ if (copy_to_user((void __user *)arg, &p, sizeof(p))) { nv_rs_dmabuf_publish_fd(&pending, false); return -EFAULT; } nv_rs_dmabuf_publish_fd(&pending, true); return 0; } static long nv_rs_uvm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct nv_rs_uvm_handle *handle = file->private_data; struct file *target_file = NULL; void *mm_other = NULL; bool mm_same = false; size_t size = 0; u8 *buf = NULL; int route, rc; struct nv_rs_external_map_diagnostic diagnostic = { 0 }; if (!handle) return -EBADFD; if (cmd == NV_RS_UVM_EXPORT_DMABUF) return nv_rs_uvm_export_dmabuf(handle, arg); route = nv_rs_uvm_route_size(cmd, &size); if (route < 0) return route; if (route == 1) return 0; buf = kvmalloc(size ? size : 1, GFP_KERNEL); if (!buf) return -ENOMEM; if (size && copy_from_user(buf, (void __user *)arg, size)) { kvfree(buf); return -EFAULT; } if (cmd == NV_RS_UVM_MM_INITIALIZE && size >= 4) { s32 target_fd; memcpy(&target_fd, buf, sizeof(target_fd)); target_file = fget(target_fd); if (target_file && target_file->f_op == &nv_rs_uvm_fops) { struct nv_rs_uvm_handle *other = target_file->private_data; if (other == handle) mm_same = true; else if (other) mm_other = other->state; } } nv_rs_rmapi_node_lock(); if (mutex_lock_interruptible(&nv_rs_uvm_lock)) { nv_rs_rmapi_node_unlock(); if (target_file) fput(target_file); kvfree(buf); return -ERESTARTSYS; } rc = nv_rs_uvm_file_ioctl(handle->state, cmd, buf, size, mm_same, mm_other, &diagnostic); if (nv_rs_runtime_engines()) { size_t status_off = 0; u32 rm_st = 0; if (nv_rs_uvm_route_status_offset(cmd, &status_off) == 0 && status_off + 4 <= size) memcpy(&rm_st, buf + status_off, sizeof(rm_st)); if (rc != 0 || rm_st != 0) { u64 a0 = 0, a1 = 0; if (size >= 8) memcpy(&a0, buf, sizeof(a0)); if (size >= 16) memcpy(&a1, buf + 8, sizeof(a1)); if (cmd == NV_RS_UVM_MAP_EXTERNAL_ALLOCATION) { if (diagnostic.stage) pr_info_ratelimited("nvidia_rs: rt UVM cmd=0x%x rc=%d rmStatus=0x%x stage=%s gpu_minor=%d mapped_bytes=%llu requested_bytes=%llu\n", cmd, rc, rm_st, diagnostic.stage, diagnostic.gpu_minor, diagnostic.mapped_bytes, a1); else pr_info_ratelimited("nvidia_rs: rt UVM cmd=0x%x rc=%d rmStatus=0x%x requested_bytes=%llu\n", cmd, rc, rm_st, a1); } else pr_info("nvidia_rs: rt UVM cmd=0x%x rc=%d rmStatus=0x%x a0=0x%llx a1=0x%llx\n", cmd, rc, rm_st, (unsigned long long)a0, (unsigned long long)a1); } } mutex_unlock(&nv_rs_uvm_lock); nv_rs_rmapi_node_unlock(); if (target_file) fput(target_file); if (rc == 0 && size && copy_to_user((void __user *)arg, buf, size)) rc = -EFAULT; kvfree(buf); return rc; } #ifdef CONFIG_COMPAT static long nv_rs_uvm_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { return nv_rs_uvm_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); } #endif static void nv_rs_uvm_vma_close(struct vm_area_struct *vma) { struct nv_rs_uvm_handle *handle; if (!vma || !vma->vm_file) return; handle = vma->vm_file->private_data; if (!handle) return; mutex_lock(&nv_rs_uvm_lock); nv_rs_uvm_file_vma_closed(handle->state, (u64)vma->vm_start); mutex_unlock(&nv_rs_uvm_lock); } static const struct vm_operations_struct nv_rs_uvm_vm_ops = { .close = nv_rs_uvm_vma_close, }; static int nv_rs_uvm_mmap(struct file *file, struct vm_area_struct *vma) { struct nv_rs_uvm_handle *handle = file->private_data; bool shared_rw; int rc; if (!handle) return -EBADFD; shared_rw = (vma->vm_flags & (VM_SHARED | VM_READ | VM_WRITE)) == (VM_SHARED | VM_READ | VM_WRITE); #if defined(NV_RS_HAVE_VMA_FLAGS_SET_WORD) vma_flags_set_word(&vma->flags, VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTCOPY); #elif defined(NV_RS_HAVE_VM_FLAGS_SET) || LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) vm_flags_set(vma, VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTCOPY); #else vma->vm_flags |= VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTCOPY; #endif vma->vm_ops = &nv_rs_uvm_vm_ops; if (mutex_lock_interruptible(&nv_rs_uvm_lock)) return -ERESTARTSYS; rc = nv_rs_uvm_file_mmap(handle->state, vma, (u64)vma->vm_start, (u64)vma->vm_end, (u64)vma->vm_pgoff << PAGE_SHIFT, shared_rw, true); mutex_unlock(&nv_rs_uvm_lock); if (nv_rs_runtime_engines() && rc != 0) pr_info("nvidia_rs: rt UVM mmap [0x%lx, 0x%lx) off=0x%lx rc=%d\n", vma->vm_start, vma->vm_end, vma->vm_pgoff << PAGE_SHIFT, rc); return rc; } static const struct file_operations nv_rs_uvm_fops = { .owner = THIS_MODULE, .open = nv_rs_uvm_open, .release = nv_rs_uvm_release, .mmap = nv_rs_uvm_mmap, .unlocked_ioctl = nv_rs_uvm_ioctl, #ifdef CONFIG_COMPAT .compat_ioctl = nv_rs_uvm_compat_ioctl, #endif /* Match stock 580.178.04 uvm_fops: omit .llseek (NULL -> -ESPIPE on all kernels). */ }; static struct miscdevice nv_rs_uvm_device = { .minor = MISC_DYNAMIC_MINOR, .name = "nvidia-rs-uvm", .fops = &nv_rs_uvm_fops, .mode = 0666, }; int nv_rs_uvm_node_init(void) { return misc_register(&nv_rs_uvm_device); } static void nv_rs_uvm_drain_cpu_dma_ht(void) { struct nv_rs_uvm_cpu_dma_record records[16] = {}; for (;;) { u32 count, i; mutex_lock(&nv_rs_uvm_cpu_dma_lock); count = nv_rs_uvm_dma_pop_batch(records, ARRAY_SIZE(records)); mutex_unlock(&nv_rs_uvm_cpu_dma_lock); if (!count) break; for (i = 0; i < count; i++) { dma_unmap_page(records[i].dev, (dma_addr_t)records[i].dma, PAGE_SIZE, DMA_BIDIRECTIONAL); put_page(records[i].page); put_device(records[i].dev); } } mutex_lock(&nv_rs_uvm_cpu_dma_lock); nv_rs_uvm_dma_release_storage(); mutex_unlock(&nv_rs_uvm_cpu_dma_lock); } void nv_rs_uvm_node_exit(void) { nv_rs_uvm_fault_poll_stop(); misc_deregister(&nv_rs_uvm_device); cancel_work_sync(&nv_rs_uvm_drain_work); nv_rs_rmapi_node_lock(); mutex_lock(&nv_rs_uvm_lock); nv_rs_uvm_node_cleanup(); mutex_unlock(&nv_rs_uvm_lock); nv_rs_rmapi_node_unlock(); cancel_work_sync(&nv_rs_uvm_drain_work); nv_rs_uvm_drain_cpu_dma_ht(); }