mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
- Resolve conflicts in hwmon_adapter.c, src/nvidia/Makefile, gsp_route.rs, rmapi_node.rs, and uvm_node.rs (preserving both #350 security/UVM/hwmon checks and master's #266/#349/#360 additions). - In uvm_node::destroy_gpu_va_space, retain the page tree on any failed set_vaspace_page_directory(..., None) across all builds (not only GA10x) and flush GPU TLB before tree.destroy() on success.
653 lines
21 KiB
C
653 lines
21 KiB
C
// 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 <linux/device.h>
|
|
#include <linux/dma-mapping.h>
|
|
#include <linux/file.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/miscdevice.h>
|
|
#include <linux/version.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/module.h>
|
|
#include <linux/mutex.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/uaccess.h>
|
|
#include <linux/vmalloc.h>
|
|
#include <linux/workqueue.h>
|
|
#ifdef CONFIG_COMPAT
|
|
#include <linux/compat.h>
|
|
#endif
|
|
#include "gpu_owner.h"
|
|
#include "rmapi_node.h"
|
|
#include "uvm_node.h"
|
|
|
|
#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);
|
|
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 bool nv_rs_uvm_dma_pop_any(struct nv_rs_uvm_cpu_dma_record *out);
|
|
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, 47);
|
|
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);
|
|
if (!handle)
|
|
return -ENOMEM;
|
|
handle->state = kvzalloc(nv_rs_uvm_file_size(), GFP_KERNEL);
|
|
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;
|
|
}
|
|
|
|
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;
|
|
|
|
if (!handle)
|
|
return -EBADFD;
|
|
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);
|
|
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));
|
|
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 rec;
|
|
|
|
for (;;) {
|
|
bool found;
|
|
|
|
mutex_lock(&nv_rs_uvm_cpu_dma_lock);
|
|
found = nv_rs_uvm_dma_pop_any(&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);
|
|
}
|
|
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();
|
|
}
|