From c65b7c440cebdbdcade5084b9f80e00d1b5edd1d Mon Sep 17 00:00:00 2001 From: Arsenova Date: Wed, 2 Dec 2026 05:56:59 +0800 Subject: [PATCH] uvm_node: take the RM API lock before the UVM lock nv_rs_uvm_ioctl() and nv_rs_uvm_release() took nv_rs_uvm_lock and then nv_rs_rmapi_lock. Two other orders already exist: rmapi -> mmap_lock nv_rs_rmapi_os_lock_user_pages() calls pin_user_pages_fast() with the RM API lock held (NV01_MEMORY_SYSTEM_OS_DESCRIPTOR, cudaHostRegister) mmap_lock -> uvm the kernel calls nv_rs_uvm_mmap() and nv_rs_uvm_vma_close() with mmap_lock held so uvm -> rmapi closes a cycle. lockdep reports it as soon as a CUDA process pins host memory while another thread mmaps the UVM node, and a multi-threaded process can really deadlock on it: WARNING: possible circular locking dependency detected cuda_dmabuf_exp/4377 is trying to acquire lock: (&mm->mmap_lock){++++}-{4:4}, at: gup_fast_fallback+0x51f/0x2bc0 but task is already holding lock: (nv_rs_rmapi_lock){+.+.}-{4:4}, at: nv_rs_rmapi_ioctl+0x64c/0x2f50 Chain exists of: &mm->mmap_lock --> nv_rs_uvm_lock --> nv_rs_rmapi_lock Take the RM API lock first in both places. Nothing under the RM API lock takes the UVM lock (only the UVM file operations and module init/exit do), so the order is now rmapi -> mmap_lock -> uvm everywhere. The fault worker only trylocks, so it could not deadlock, but it now tries the two locks in the same order as well. The other way out would be to pin the pages before taking the RM API lock in the OS-descriptor path; this keeps the change local to the UVM node. Observed on an RTX 3050 Laptop (GA107) under a lockdep kernel (7.4.0-rc2) running a CUDA dma-buf test. --- kernel-open/nvidia/rmapi_node.c | 5 +++-- kernel-open/nvidia/uvm_node.c | 27 ++++++++++++++++----------- 2 files changed, 19 insertions(+), 13 deletions(-) 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))