mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
Proactive 100% parity Wave 1 (REA-grounded vs stock 580.178.04): - Kernel/build compat: Kbuild header probes for timer_delete_sync, vm_flags_set, vma_flags_set_word; remove broken UTS_UBUNTU_RELEASE_ABI guard on pci_resize_resource; guard x86 set_memory_uc/wb under CONFIG_X86; remove illegal ARM64 dma_sync_single_* on coherent DMA; gate nvidia_rs_drm on CONFIG_DRM; default LD=ld.lld/OBJCOPY=llvm-objcopy under LLVM=1; fix dkms.conf (KDIR support, all 4 modules, 0.2.0) and install.sh (all 4 modules, non-0000 PCI domains, KREL override). - IOMMU & PCI lifecycle: switch gsp_route error notifier to per-device DMA (nv_rs_rmapi_os_page_dma); add PCI .shutdown (pci_clear_master + udelay(1)) and AER .err_handler; keep freed rmapi_allocs on list until users==0 for hot-unplug DMA teardown; drain nv_rs_uvm_cpu_dma_ht on exit; lock BAR0 fill under p->access; raise MAX_BRIDGE_GPUS to 32. - Security & isolation (REA _nv000655rm / _nv000663rm): enforce cross-process share policy on NV_ESC_RM_DUP_OBJECT & NV_ESC_RM_SHARE; refcount shared memory backings on release (eliminating dup UAF); scope UVM ManagedFaultMapper by faulting GPU & SysmemPageTree.owner; pass real task_tgid_vnr into Caller.pid.
637 lines
19 KiB
C
637 lines
19 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/hashtable.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/spinlock.h>
|
|
#include <linux/uaccess.h>
|
|
#include <linux/version.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 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 after 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 */
|
|
void nv_rs_rmapi_node_lock(void);
|
|
int nv_rs_rmapi_node_trylock(void);
|
|
void nv_rs_rmapi_node_unlock(void);
|
|
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;
|
|
if (mutex_trylock(&nv_rs_uvm_lock)) {
|
|
/* only tried: the RM API side may stop this worker while it holds the lock */
|
|
if (nv_rs_rmapi_node_trylock()) {
|
|
raw = nv_rs_uvm_service_replayable_faults(&mapped, &replays,
|
|
&cancelled_gpu_id,
|
|
&cancelled_inst_ptr,
|
|
&cancelled_va);
|
|
nv_rs_rmapi_node_unlock();
|
|
}
|
|
mutex_unlock(&nv_rs_uvm_lock);
|
|
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) {
|
|
nv_rs_rmapi_handle_rc_fault(cancelled_gpu_id, cancelled_inst_ptr,
|
|
cancelled_va, 31);
|
|
}
|
|
}
|
|
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);
|
|
}
|
|
|
|
struct nv_rs_uvm_cpu_dma_entry {
|
|
struct hlist_node node;
|
|
void *cpu_ptr;
|
|
struct page *page;
|
|
u32 gpu_id;
|
|
struct device *dev;
|
|
dma_addr_t dma;
|
|
};
|
|
|
|
static DEFINE_HASHTABLE(nv_rs_uvm_cpu_dma_ht, 10);
|
|
static DEFINE_SPINLOCK(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)
|
|
{
|
|
unsigned long flags;
|
|
struct nv_rs_uvm_cpu_dma_entry *e, *fresh;
|
|
struct device *dev;
|
|
struct page *page;
|
|
dma_addr_t dma;
|
|
u64 key;
|
|
|
|
if (!cpu_ptr)
|
|
return 0;
|
|
key = (u64)(unsigned long)cpu_ptr;
|
|
|
|
spin_lock_irqsave(&nv_rs_uvm_cpu_dma_lock, flags);
|
|
hash_for_each_possible(nv_rs_uvm_cpu_dma_ht, e, node, key) {
|
|
if (e->cpu_ptr == cpu_ptr && e->gpu_id == gpu_id) {
|
|
dma = e->dma;
|
|
spin_unlock_irqrestore(&nv_rs_uvm_cpu_dma_lock, flags);
|
|
return (u64)dma;
|
|
}
|
|
}
|
|
spin_unlock_irqrestore(&nv_rs_uvm_cpu_dma_lock, flags);
|
|
|
|
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;
|
|
}
|
|
fresh = kmalloc(sizeof(*fresh), GFP_KERNEL);
|
|
if (!fresh) {
|
|
dma_unmap_page(dev, dma, PAGE_SIZE, DMA_BIDIRECTIONAL);
|
|
put_page(page);
|
|
put_device(dev);
|
|
return 0;
|
|
}
|
|
fresh->cpu_ptr = cpu_ptr;
|
|
fresh->page = page;
|
|
fresh->gpu_id = gpu_id;
|
|
fresh->dev = dev;
|
|
fresh->dma = dma;
|
|
|
|
spin_lock_irqsave(&nv_rs_uvm_cpu_dma_lock, flags);
|
|
hash_for_each_possible(nv_rs_uvm_cpu_dma_ht, e, node, key) {
|
|
if (e->cpu_ptr == cpu_ptr && e->gpu_id == gpu_id) {
|
|
dma_addr_t existing = e->dma;
|
|
spin_unlock_irqrestore(&nv_rs_uvm_cpu_dma_lock, flags);
|
|
dma_unmap_page(dev, dma, PAGE_SIZE, DMA_BIDIRECTIONAL);
|
|
put_page(page);
|
|
put_device(dev);
|
|
kfree(fresh);
|
|
return (u64)existing;
|
|
}
|
|
}
|
|
hash_add(nv_rs_uvm_cpu_dma_ht, &fresh->node, key);
|
|
spin_unlock_irqrestore(&nv_rs_uvm_cpu_dma_lock, flags);
|
|
return (u64)dma;
|
|
}
|
|
|
|
static void nv_rs_uvm_unmap_cpu_dma(void *cpu_ptr)
|
|
{
|
|
unsigned long flags;
|
|
struct nv_rs_uvm_cpu_dma_entry *e;
|
|
struct hlist_node *tmp;
|
|
HLIST_HEAD(dead);
|
|
u64 key = (u64)(unsigned long)cpu_ptr;
|
|
|
|
spin_lock_irqsave(&nv_rs_uvm_cpu_dma_lock, flags);
|
|
hash_for_each_possible_safe(nv_rs_uvm_cpu_dma_ht, e, tmp, node, key) {
|
|
if (e->cpu_ptr == cpu_ptr) {
|
|
hash_del(&e->node);
|
|
hlist_add_head(&e->node, &dead);
|
|
}
|
|
}
|
|
spin_unlock_irqrestore(&nv_rs_uvm_cpu_dma_lock, flags);
|
|
|
|
hlist_for_each_entry_safe(e, tmp, &dead, node) {
|
|
dma_unmap_page(e->dev, e->dma, PAGE_SIZE, DMA_BIDIRECTIONAL);
|
|
put_page(e->page);
|
|
put_device(e->dev);
|
|
kfree(e);
|
|
}
|
|
}
|
|
|
|
void nv_rs_uvm_unmap_gpu_dev(struct device *dev)
|
|
{
|
|
unsigned long flags;
|
|
struct nv_rs_uvm_cpu_dma_entry *e;
|
|
struct hlist_node *tmp;
|
|
HLIST_HEAD(dead);
|
|
unsigned int bkt;
|
|
|
|
if (!dev)
|
|
return;
|
|
spin_lock_irqsave(&nv_rs_uvm_cpu_dma_lock, flags);
|
|
hash_for_each_safe(nv_rs_uvm_cpu_dma_ht, bkt, tmp, e, node) {
|
|
if (e->dev == dev) {
|
|
hash_del(&e->node);
|
|
hlist_add_head(&e->node, &dead);
|
|
}
|
|
}
|
|
spin_unlock_irqrestore(&nv_rs_uvm_cpu_dma_lock, flags);
|
|
|
|
hlist_for_each_entry_safe(e, tmp, &dead, node) {
|
|
dma_unmap_page(e->dev, e->dma, PAGE_SIZE, DMA_BIDIRECTIONAL);
|
|
put_page(e->page);
|
|
put_device(e->dev);
|
|
kfree(e);
|
|
}
|
|
}
|
|
|
|
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))
|
|
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;
|
|
mutex_lock(&nv_rs_uvm_lock);
|
|
nv_rs_rmapi_node_lock();
|
|
nv_rs_uvm_file_release(handle->state);
|
|
nv_rs_rmapi_node_unlock();
|
|
mutex_unlock(&nv_rs_uvm_lock);
|
|
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;
|
|
|
|
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;
|
|
}
|
|
}
|
|
if (mutex_lock_interruptible(&nv_rs_uvm_lock)) {
|
|
if (target_file)
|
|
fput(target_file);
|
|
kvfree(buf);
|
|
return -ERESTARTSYS;
|
|
}
|
|
nv_rs_rmapi_node_lock();
|
|
rc = nv_rs_uvm_file_ioctl(handle->state, cmd, buf, size, mm_same, mm_other);
|
|
nv_rs_rmapi_node_unlock();
|
|
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);
|
|
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)
|
|
{
|
|
unsigned long flags;
|
|
struct nv_rs_uvm_cpu_dma_entry *e;
|
|
struct hlist_node *tmp;
|
|
HLIST_HEAD(dead);
|
|
unsigned int bkt;
|
|
|
|
spin_lock_irqsave(&nv_rs_uvm_cpu_dma_lock, flags);
|
|
hash_for_each_safe(nv_rs_uvm_cpu_dma_ht, bkt, tmp, e, node) {
|
|
hash_del(&e->node);
|
|
hlist_add_head(&e->node, &dead);
|
|
}
|
|
spin_unlock_irqrestore(&nv_rs_uvm_cpu_dma_lock, flags);
|
|
|
|
hlist_for_each_entry_safe(e, tmp, &dead, node) {
|
|
dma_unmap_page(e->dev, e->dma, PAGE_SIZE, DMA_BIDIRECTIONAL);
|
|
put_page(e->page);
|
|
put_device(e->dev);
|
|
kfree(e);
|
|
}
|
|
}
|
|
|
|
void nv_rs_uvm_node_exit(void)
|
|
{
|
|
nv_rs_uvm_fault_poll_stop();
|
|
misc_deregister(&nv_rs_uvm_device);
|
|
mutex_lock(&nv_rs_uvm_lock);
|
|
nv_rs_uvm_node_cleanup();
|
|
mutex_unlock(&nv_rs_uvm_lock);
|
|
nv_rs_uvm_drain_cpu_dma_ht();
|
|
}
|