Files
nvdriver/kernel-open/nvidia/diagnostic.c
morebrackets 6435d65112 fix(rmapi/modeset): rmmod refcnt leak (#491), modeset RM ops (#466), runtime logs/fw warning (#501) (#513)
* fix(ga10x/rmapi/modeset): R620 control translation, rmmod refcnt, modeset RM ops (#504, #491, #466, #501)

* chore(pr513): drop gsp_route R620 hunk in favor of live-verified #514

* fix(gpu_owner): balance get_task_struct with put_task_struct after kthread_stop

* fix(gpu_owner/irq): wait for runtime worker join & pin/teardown failed runtime IRQ close

Addresses CodeRabbit review findings on PR #513 (and #491/#528):
- gpu_owner.c: Wait for runtime_stopped in a 10s warning loop and always join
  via kthread_stop() + put_task_struct() so an unpinned runtime worker can
  never outlive module text during PCI removal / rmmod.
- irq_adapter.c: When nv_rs_irq_close() fails to quiesce an unpinned
  runtime-kthread IRQ owner, acquire THIS_MODULE before returning the error
  (or force-disable/synchronize/free handlers if the module is already
  unloading so no callback can outlive module text).
- ga10x_events.rs: Check nv_rs_irq_close() status in stop() and restore e.irq
  on failure instead of discarding the handle.
- tests/irq-mock: Cover both live-pin and unloading-force-teardown paths.

* fix(pci,irq): disable PCI INTx on forced IRQ close and bound AER/shutdown runtime stop

Address follow-up review findings on #513:
- In nv_rs_irq_close() forced teardown (unpinned runtime worker after mask
  failure), call pci_intx(owner->pdev, 0) for legacy INTx mode so the
  device stops asserting its level-triggered line without disabling a
  shared IRQ line for other devices, and add an irq-mock regression test.
- Add nv_rs_gpu_owner_runtime_stop_bounded(owner, timeout_ms) preserving
  runtime_task and runtime_stopping on timeout (preventing restart races
  and allowing a later nv_rs_remove() stop to complete the join).
- In nv_rs_shutdown() and nv_rs_aer_error_detected(), call pci_clear_master()
  first for immediate DMA containment, then use a bounded runtime stop so
  shutdown and PCIe AER callbacks never wedge indefinitely.

* fix(pci,pm): bound runtime_lock wait, reclaim retired worker on start/AER, and reassert BME clear

Address follow-up review findings on #513:
- Include runtime_lock acquisition inside nv_rs_gpu_owner_runtime_stop_bounded()'s
  deadline via bounded mutex_trylock() polling so shutdown/AER cannot hang when
  startup holds runtime_lock waiting for runtime_ready.
- Reclaim a retired worker in nv_rs_gpu_owner_runtime_start() via
  nv_rs_gpu_owner_runtime_reclaim_locked() and in nv_rs_aer_resume() so a
  timed-out bounded stop never strands runtime engines unavailable after recovery.
- Reassert pci_clear_master(pdev) after the serialized stop attempt in
  nv_rs_shutdown() and nv_rs_aer_error_detected() so in-flight GPU bring-up
  cannot leave Bus Master enabled.
- Use nv_rs_gpu_owner_runtime_stop_bounded() in PM suspend/runtime_suspend with
  full state rollback on timeout so suspend cannot wedge indefinitely while
  nv_rs_remove() keeps the unconditional join required before module unload.

* fix(pci): keep Bus Master enabled during bounded GSP unload on shutdown

Address final review comment on #513:
- In nv_rs_shutdown(), invoke nv_rs_gpu_owner_runtime_stop_bounded(owner, 10000)
  before pci_clear_master(pdev) so an active GSP session can complete its
  DMA unload RPC during clean shutdown, then clear Bus Master Enable once
  the stop finishes or hits its bounded deadline.

* fix(pci,pm): return DISCONNECT on AER stop timeout and keep full PM suspend stop

Address review findings on #513:
- In nv_rs_aer_error_detected(), return PCI_ERS_RESULT_DISCONNECT rather than
  PCI_ERS_RESULT_NEED_RESET if nv_rs_gpu_owner_runtime_stop_bounded() times
  out, preventing a PCIe slot reset while a live runtime worker could still
  issue MMIO or re-enable Bus Master.
- In nv_rs_pm_do_suspend() and nv_rs_pmops_runtime_suspend(), wait for worker
  retirement via nv_rs_gpu_owner_runtime_stop() before entering D3hot rather
  than half-aborting after modeset suspend and USERD VMA revocation.

---------

Co-authored-by: morebrackets <19673572+morebrackets@users.noreply.github.com>
2026-12-04 19:48:23 -08:00

275 lines
11 KiB
C

// SPDX-License-Identifier: MIT
/* Native control protocol with opt-in owned PCI/GPU preparation. */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/version.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#ifdef CONFIG_COMPAT
#include <linux/compat.h>
#endif
#include "ioctl_policy.h"
#include "pci_driver.h"
#include "gpu_owner.h"
#include "file_cleanup.h"
#include "event_work_adapter.h"
#include "rmapi_node.h"
#include "uvm_node.h"
extern u32 nv_rs_protocol_version(void);
extern u64 nv_rs_gpu_capabilities(void);
extern int nv_rs_event_selftest(void);
extern size_t nv_rs_device_size(void);
extern size_t nv_rs_file_size(void);
extern size_t nv_rs_state_align(void);
extern int nv_rs_device_init(void *state);
extern int nv_rs_file_open(void *device, void *file);
extern int nv_rs_file_close(void *device, void *file);
extern int nv_rs_nested_copy_error(u32 command, u8 *bytes, size_t length, bool output);
extern int nv_rs_rm_preflight(void *device, void *file, u32 command, u8 *bytes, size_t length);
extern int nv_rs_nested_plan(u32 command, const u8 *bytes, size_t length, u64 *pointer, size_t *size);
extern int nv_rs_rm_ioctl(void *device, void *file, u32 command, u8 *bytes, size_t length, u8 *nested, size_t nested_length);
extern int nv_rs_ioctl(void *device, void *file, u32 command, u8 *bytes, size_t length);
static unsigned int protocol_version;
module_param(protocol_version, uint, 0444);
MODULE_PARM_DESC(protocol_version, "Control protocol version; no GPU/CUDA support");
static bool run_selftest = true;
module_param(run_selftest, bool, 0444);
static DEFINE_MUTEX(nv_rs_device_lock);
static void *nv_rs_device_state;
static struct nv_rs_file_cleanup_queue nv_rs_file_cleanup_queue;
struct nv_rs_file { struct mutex lock; void *state; struct nv_rs_file_cleanup cleanup; };
static int nv_rs_close_context(void *opaque)
{
struct nv_rs_file *context=opaque;
int result;
mutex_lock(&context->lock);
mutex_lock(&nv_rs_device_lock);
result=nv_rs_file_close(nv_rs_device_state,context->state);
mutex_unlock(&nv_rs_device_lock);
mutex_unlock(&context->lock);
return result;
}
static void nv_rs_destroy_context(void *opaque)
{
struct nv_rs_file *context=opaque;
kfree(context->state);
kfree(context);
}
static int nv_rs_open(struct inode *inode, struct file *file)
{
struct nv_rs_file *context;
int result;
context = kzalloc(sizeof(*context), GFP_KERNEL);
if (!context)
return -ENOMEM;
context->state = kzalloc(nv_rs_file_size(), GFP_KERNEL);
if (!context->state) { kfree(context); return -ENOMEM; }
mutex_init(&context->lock);
nv_rs_file_cleanup_init(&context->cleanup,&nv_rs_file_cleanup_queue,context,nv_rs_close_context,nv_rs_destroy_context);
mutex_lock(&nv_rs_device_lock);
result = nv_rs_file_open(nv_rs_device_state, context->state);
mutex_unlock(&nv_rs_device_lock);
if (result) { kfree(context->state); kfree(context); return result; }
file->private_data = context;
return nonseekable_open(inode, file);
}
static int nv_rs_release(struct inode *inode, struct file *file)
{
struct nv_rs_file *context=file->private_data;
(void)inode;
file->private_data=NULL;
if (!context)
return 0;
return nv_rs_file_cleanup_retire(&context->cleanup);
}
static long nv_rs_unlocked_ioctl(struct file *file, unsigned int command,
unsigned long argument)
{
struct nv_rs_file *context = file->private_data;
struct nv_rs_request request;
struct nv_rs_xfer xfer;
void __user *user_pointer;
u8 *bytes, *nested = NULL;
u64 nested_pointer = 0;
size_t nested_size = 0;
bool rm_request;
int result;
if (!context)
return -EBADFD;
result = nv_rs_plan(command, argument, NULL, &request);
if (result == 1) {
if (copy_from_user(&xfer, (void __user *)argument, sizeof(xfer)))
return -EFAULT;
result = nv_rs_plan(command, argument, &xfer, &request);
}
if (result)
return result;
user_pointer = u64_to_user_ptr(request.pointer);
bytes = kmalloc(request.size ? request.size : 1, GFP_KERNEL);
if (!bytes)
return -ENOMEM;
if (copy_from_user(bytes, user_pointer, request.size)) {
kfree(bytes);
return -EFAULT;
}
rm_request = request.command == 0x29 || request.command == 0x2a || request.command == 0x2b;
if (rm_request) {
/* Match original invalid-client/metadata error ordering before reading
* nested userspace. Dispatch rechecks after copies because another
* thread can mutate file state while these locks are released. */
if (mutex_lock_interruptible(&context->lock)) { result = -ERESTARTSYS; goto out; }
if (mutex_lock_interruptible(&nv_rs_device_lock)) {
mutex_unlock(&context->lock); result = -ERESTARTSYS; goto out;
}
result = nv_rs_rm_preflight(nv_rs_device_state, context->state, request.command, bytes, request.size);
mutex_unlock(&nv_rs_device_lock);
mutex_unlock(&context->lock);
if (result < 0) goto out;
if (result == 1) { result = 0; goto copy_output; }
result = nv_rs_nested_plan(request.command, bytes, request.size, &nested_pointer, &nested_size);
if (result == -EOVERFLOW) {
result = nv_rs_nested_copy_error(request.command, bytes, request.size, false);
goto copy_output;
}
if (result) goto out;
/* Explicit adapter allocation bound, not a claimed hardware ABI limit. */
if (nested_size > 1024 * 1024) { result = -ENOMEM; goto out; }
if (nested_size) {
nested = kmalloc(nested_size, GFP_KERNEL);
if (!nested) { result = -ENOMEM; goto out; }
if (copy_from_user(nested, u64_to_user_ptr(nested_pointer), nested_size)) {
result = nv_rs_nested_copy_error(request.command, bytes, request.size, false);
nested_size = 0; /* Never expose uninitialized failed-input data. */
goto copy_output;
}
}
}
/* Copy occurs before locks; no userspace pointer reaches Rust. Lock order
* is always per-file -> device. No pointer-bearing RM payload is followed. */
if (mutex_lock_interruptible(&context->lock)) { result = -ERESTARTSYS; goto out; }
if (mutex_lock_interruptible(&nv_rs_device_lock)) {
mutex_unlock(&context->lock); result = -ERESTARTSYS; goto out;
}
if (rm_request)
result = nv_rs_rm_ioctl(nv_rs_device_state, context->state, request.command,
bytes, request.size, nested, nested_size);
else
result = nv_rs_ioctl(nv_rs_device_state, context->state, request.command,
bytes, request.size);
mutex_unlock(&nv_rs_device_lock);
mutex_unlock(&context->lock);
/* Match native version mismatch behavior: reply/version copy back even
* when the protocol returns EINVAL. A copy-out fault takes precedence. */
copy_output:
if (nested_size && copy_to_user(u64_to_user_ptr(nested_pointer), nested, nested_size))
result = nv_rs_nested_copy_error(request.command, bytes, request.size, true);
if (copy_to_user(user_pointer, bytes, request.size))
result = -EFAULT;
out:
kfree(nested);
kfree(bytes);
return result;
}
#ifdef CONFIG_COMPAT
static long nv_rs_compat_ioctl(struct file *file, unsigned int command, unsigned long arg)
{
/* Exact shipped structs use fixed-width/aligned NvP64 fields. Top-level
* compat syscall argument is a compat pointer; nested wire pointers remain
* 64-bit fields and are checked by normal copy_from_user access rules. */
return nv_rs_unlocked_ioctl(file, command, (unsigned long)compat_ptr(arg));
}
#endif
static const struct file_operations nv_rs_operations = {
.owner = THIS_MODULE, .open = nv_rs_open, .release = nv_rs_release,
.unlocked_ioctl = nv_rs_unlocked_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = nv_rs_compat_ioctl,
#endif
/* Match stock 580.178.04 nvidia_fops: omit .llseek (NULL -> -ESPIPE on all kernels). */
};
static struct miscdevice nv_rs_control = {
.minor = MISC_DYNAMIC_MINOR, .name = "nvidia-rsctl",
.fops = &nv_rs_operations, .mode = 0600,
};
extern int nv_rs_software_manager_init(void);
extern int nv_rs_software_manager_shutdown(void);
static void drain_software_manager(void)
{
int error;
while ((error = nv_rs_software_manager_shutdown()) != 0) {
pr_warn_ratelimited("nvidia_rs: retaining software manager until cleanup succeeds (%d)\n", error);
msleep(20);
}
}
static int __init nvidia_rs_init(void)
{
int status;
protocol_version = nv_rs_protocol_version();
if (nv_rs_gpu_capabilities() != 0 || nv_rs_state_align() > ARCH_KMALLOC_MINALIGN ||
nv_rs_device_size() > 1048576 || nv_rs_file_size() > 65536)
return -EINVAL;
if (run_selftest && (status = nv_rs_event_selftest()))
return status;
nv_rs_device_state = kzalloc(nv_rs_device_size(), GFP_KERNEL);
if (!nv_rs_device_state)
return -ENOMEM;
status = nv_rs_device_init(nv_rs_device_state);
if (status) { kfree(nv_rs_device_state); return status; }
status=nv_rs_file_cleanup_queue_init(&nv_rs_file_cleanup_queue);
if (status) { kfree(nv_rs_device_state); return status; }
status=nv_rs_event_workqueue_init();
if(status)goto fail_queue;
status = nv_rs_software_manager_init();
if (status) {
int unwind_err = nv_rs_software_manager_shutdown();
if (unwind_err)
pr_warn("nvidia_rs: partial software manager unwind failed (%d)\n", unwind_err);
goto fail_event_wq;
}
/* RM API control node and UVM node: registered before the PCI driver. */
status = nv_rs_rmapi_node_init();
if (status) goto fail_software_manager;
status = nv_rs_uvm_node_init();
if (status) { nv_rs_rmapi_node_exit(); goto fail_software_manager; }
status = nv_rs_pci_driver_register();
if (status) { nv_rs_uvm_node_exit(); nv_rs_rmapi_node_exit(); goto fail_software_manager; }
/* Publish fops only after every owning subsystem is initialized. */
status=misc_register(&nv_rs_control);
if (status) { nv_rs_pci_driver_unregister(); nv_rs_uvm_node_exit(); nv_rs_rmapi_node_exit(); goto fail_software_manager; }
pr_info("nvidia_rs: /dev/nvidia-rsctl protocol %u (runtime_engines=%u)\n",
protocol_version, nv_rs_runtime_engines() ? 1u : 0u);
return 0;
fail_software_manager:
drain_software_manager();
fail_event_wq:
nv_rs_event_workqueue_destroy();
fail_queue:
nv_rs_file_cleanup_queue_destroy(&nv_rs_file_cleanup_queue);
kfree(nv_rs_device_state);
return status;
}
static void __exit nvidia_rs_exit(void)
{
misc_deregister(&nv_rs_control);
nv_rs_pci_driver_unregister();
nv_rs_uvm_node_exit();
nv_rs_rmapi_node_exit();
nv_rs_event_workqueue_destroy();
/* No retained delayed item can coexist with module exit: each owns a pin.
* Join a last worker that has put its pin but has not returned yet. */
nv_rs_file_cleanup_queue_destroy(&nv_rs_file_cleanup_queue);
drain_software_manager();
kfree(nv_rs_device_state);
}
module_init(nvidia_rs_init);
module_exit(nvidia_rs_exit);
MODULE_LICENSE("Dual MIT/GPL");
MODULE_DESCRIPTION("Clean-room Rust NVIDIA GPU driver (Maxwell-Blackwell)");
MODULE_AUTHOR("nvidia-driver-rs reconstruction project");
MODULE_VERSION("0.2.0");