Files
nvdriver/tests/thread-state/ffi.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

56 lines
2.7 KiB
C

/* Host OS fixture: real malloc/pthread synchronization, controlled RM identity. */
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
static _Atomic unsigned storage, tasks, modules, locks;
static _Thread_local uint64_t pid = 0x9876;
static _Thread_local uint32_t cpu = 2, context, process_flags;
static _Thread_local bool allocation_fail, module_fail;
static _Thread_local unsigned held_locks;
struct snapshot { uint64_t pid; uint32_t cpu, interrupt; };
struct task { pthread_t owner; uint64_t pid; };
void fixture_context(uint64_t id, uint32_t processor, uint32_t ctx, uint32_t flags)
{ pid=id; cpu=processor; context=ctx; process_flags=flags; }
void fixture_fail(bool allocation, bool module) { allocation_fail=allocation; module_fail=module; }
uint64_t fixture_outstanding(void)
{ return atomic_load(&storage) | ((uint64_t)atomic_load(&tasks)<<16) |
((uint64_t)atomic_load(&modules)<<32) | ((uint64_t)atomic_load(&locks)<<48); }
void *nv_rs_thread_storage_alloc(size_t bytes)
{
if (allocation_fail) { allocation_fail=false; return NULL; }
void *p=calloc(1,bytes); if(p) atomic_fetch_add(&storage,1); return p;
}
void nv_rs_thread_storage_free(void *p) { assert(p); free(p); atomic_fetch_sub(&storage,1); }
void *nv_rs_thread_lock_create(void)
{
pthread_mutex_t *p=malloc(sizeof(*p));
if(p) { assert(!pthread_mutex_init(p,NULL)); atomic_fetch_add(&locks,1); }
return p;
}
void nv_rs_thread_lock_destroy(void *p)
{ assert(!pthread_mutex_destroy(p)); free(p); atomic_fetch_sub(&locks,1); }
size_t nv_rs_thread_lock(void *p)
{ assert(!pthread_mutex_lock(p)); assert(!held_locks); held_locks++; return 0x1234; }
void nv_rs_thread_unlock(void *p, size_t flags)
{ assert(flags==0x1234 && held_locks==1); held_locks--; assert(!pthread_mutex_unlock(p)); }
bool nv_rs_thread_process_context(void) { return !context && !held_locks; }
void *nv_rs_thread_task_get(uint32_t *flags)
{
if (!nv_rs_thread_process_context()) return NULL;
struct task *task=malloc(sizeof(*task)); assert(task);
task->owner=pthread_self(); task->pid=pid; *flags=process_flags;
atomic_fetch_add(&tasks,1); return task;
}
bool nv_rs_thread_task_current(void *p)
{ struct task *task=p; return task->pid==pid && pthread_equal(task->owner,pthread_self()); }
void nv_rs_thread_task_put(void *p) { free(p); atomic_fetch_sub(&tasks,1); }
int nv_rs_thread_current(struct snapshot *out)
{ out->pid=context&2 ? 0 : pid; out->cpu=cpu; out->interrupt=!!(context&2); return 0; }
bool nv_rs_gpu_module_get(void)
{ if(module_fail)return false; atomic_fetch_add(&modules,1); return true; }
void nv_rs_gpu_module_put(void) { atomic_fetch_sub(&modules,1); }
bool nv_rs_is_runtime_kthread(void) { return false; }