diff --git a/kernel-open/nvidia/rmapi_node.c b/kernel-open/nvidia/rmapi_node.c index 06d1e005..60fb1226 100644 --- a/kernel-open/nvidia/rmapi_node.c +++ b/kernel-open/nvidia/rmapi_node.c @@ -86,8 +86,9 @@ extern bool nv_rs_modeset_unregister_gpu(u32 gpu_id); * are made outside it. */ static DEFINE_MUTEX(nv_rs_rmapi_lock); /* The UVM node (uvm_node.c) and the GA10x code (src/nvidia/kernel/ga10x.rs) take this lock too where they change GPU - * state: the object tables, GSP-RM's RPC channel, video memory through the BAR0 window. Order: the UVM lock, then this - * one, never the other way round (nothing under this lock takes the UVM lock). */ + * state: the object tables, GSP-RM's RPC channel, video memory through the BAR0 window. Order: this lock, then the UVM + * lock. This lock is held across pin_user_pages (mmap_lock) and the UVM lock is taken under mmap_lock (the UVM .mmap + * and vm_ops->close), so the UVM-first order closed a cycle through mmap_lock (see uvm_node.c). */ void nv_rs_rmapi_node_lock(void); void nv_rs_rmapi_node_lock(void) { diff --git a/kernel-open/nvidia/uvm_node.c b/kernel-open/nvidia/uvm_node.c index cb4b0a84..2d03dd9c 100644 --- a/kernel-open/nvidia/uvm_node.c +++ b/kernel-open/nvidia/uvm_node.c @@ -44,8 +44,12 @@ extern u32 nv_rs_uvm_service_replayable_faults(u32 *pages_mapped_out, u32 *repla 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 */ +/* 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); @@ -86,16 +90,16 @@ static void nv_rs_uvm_fault_work_fn(struct work_struct *work) (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()) { + /* 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()) { + if (mutex_trylock(&nv_rs_uvm_lock)) { 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); } - mutex_unlock(&nv_rs_uvm_lock); + 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); @@ -445,11 +449,11 @@ static int nv_rs_uvm_release(struct inode *inode, struct file *file) (void)inode; if (!handle) return 0; - mutex_lock(&nv_rs_uvm_lock); nv_rs_rmapi_node_lock(); + mutex_lock(&nv_rs_uvm_lock); nv_rs_uvm_file_release(handle->state); - nv_rs_rmapi_node_unlock(); mutex_unlock(&nv_rs_uvm_lock); + nv_rs_rmapi_node_unlock(); kvfree(handle->state); kfree(handle); return 0; @@ -489,15 +493,15 @@ static long nv_rs_uvm_ioctl(struct file *file, unsigned int cmd, unsigned long a 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; } - 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; @@ -514,6 +518,7 @@ static long nv_rs_uvm_ioctl(struct file *file, unsigned int cmd, unsigned long a } } 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))