Files
nvdriver/tests/irq-mock/test.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

139 lines
7.9 KiB
C

#define NV_RS_IRQ_HOST_FIXTURE
#include "irq_mock.h"
#include "../../kernel-open/nvidia/irq_adapter.c"
bool nv_rs_pci_get(struct nv_rs_pci*p){if(!p||p->refs<=0)return false;p->refs++;return true;}
void nv_rs_pci_put(struct nv_rs_pci*p){p->refs--;assert(p->refs>=1);}
struct device*nv_rs_pci_device(struct nv_rs_pci*p){return &p->pdev.dev;}
bool nv_rs_pci_is_removed(struct nv_rs_pci*p){return !p||p->removed;}
struct ctx {int mask_error,unmask_error,top_result,masks,unmasks,tops,threads;};
static int mask(void*x){struct ctx*c=x;c->masks++;return c->mask_error;}
static int unmask(void*x){struct ctx*c=x;c->unmasks++;assert(requested_handlers>0);return c->unmask_error;}
static unsigned int top(void*x,unsigned int i){struct ctx*c=x;(void)i;c->tops++;return c->top_result;}
static void thread(void*x,unsigned int i){struct ctx*c=x;(void)i;c->threads++;}
static const struct nv_rs_irq_ops ops={mask,unmask,top,thread};
static void reset(void){assert(module_refs==0&&allocated_vectors==0&&requested_handlers==0);memset(handlers,0,sizeof(handlers));syncs=enables=disables=intx_disabled=0;attempt_count=rejected_modes=0;fail_alloc=0;fail_request=-1;vector_result=2;choose_mode=PCI_IRQ_MSIX;module_available=1;runtime_kthread=false;}
int main(void){unsigned tests=0;struct nv_rs_pci pci={.refs=1};struct ctx c={.top_result=NV_RS_IRQ_WAKE_THREAD};struct nv_rs_irq*irq=NULL;
reset();assert(!nv_rs_irq_prepare(&pci,7,4,&ops,&c,&irq));assert(c.masks==1&&pci.refs==2&&module_refs==1);assert(nv_rs_irq_vectors(irq)==2&&nv_rs_irq_mode(irq)==1);assert(handlers[0].top(100,handlers[0].cookie)==IRQ_NONE&&c.tops==0);assert(!nv_rs_irq_arm(irq)&&enables==2);assert(handlers[0].top(100,handlers[0].cookie)==IRQ_WAKE_THREAD);assert(handlers[0].thread(100,handlers[0].cookie)==IRQ_HANDLED&&c.threads==1);assert(nv_rs_irq_arm(irq)==-EBUSY);assert(!nv_rs_irq_close(irq)&&pci.refs==1&&disables==2);tests++;
reset();fail_alloc=1;assert(nv_rs_irq_prepare(&pci,7,4,&ops,&c,&irq)==-ENOSPC&&!irq&&pci.refs==1);tests++;
reset();fail_request=1;assert(nv_rs_irq_prepare(&pci,7,4,&ops,&c,&irq)==-EBUSY&&!irq&&requested_handlers==0&&syncs==1&&pci.refs==1);tests++;
reset();c.mask_error=-EIO;assert(nv_rs_irq_prepare(&pci,7,4,&ops,&c,&irq)==-EIO&&!irq&&pci.refs==1);c.mask_error=0;tests++;
reset();choose_mode=PCI_IRQ_LEGACY;vector_result=1;assert(!nv_rs_irq_prepare(&pci,7,4,&ops,&c,&irq));assert(handlers[0].flags==IRQF_SHARED);assert(!nv_rs_irq_arm(irq)&&enables==0);assert(!nv_rs_irq_quiesce(irq)&&disables==0);assert(!nv_rs_irq_close(irq));tests++;
reset();choose_mode=PCI_IRQ_MSI;vector_result=1;assert(!nv_rs_irq_prepare(&pci,7,4,&ops,&c,&irq));assert(nv_rs_irq_mode(irq)==NV_RS_IRQ_MSI);assert(!nv_rs_irq_arm(irq));c.mask_error=-EIO;assert(nv_rs_irq_close(irq)==-EIO&&pci.refs==2&&requested_handlers==1&&module_refs==1);c.mask_error=0;assert(!nv_rs_irq_close(irq));tests++;
reset();assert(!nv_rs_irq_prepare(&pci,7,4,&ops,&c,&irq));c.unmask_error=-EIO;assert(nv_rs_irq_arm(irq)==-EIO&&enables==2&&disables==2);c.unmask_error=0;assert(!nv_rs_irq_arm(irq)&&enables==4);assert(!nv_rs_irq_close(irq));tests++;
reset();assert(!nv_rs_irq_prepare(&pci,7,4,&ops,&c,&irq));c.unmask_error=-EIO;c.mask_error=-EBUSY;assert(nv_rs_irq_arm(irq)==-EBUSY&&disables==0&&requested_handlers==2);c.unmask_error=c.mask_error=0;assert(!nv_rs_irq_close(irq));tests++;
reset();assert(!nv_rs_irq_prepare(&pci,7,4,&ops,&c,&irq));assert(!nv_rs_irq_arm(irq));pci.removed=true;assert(handlers[0].top(100,handlers[0].cookie)==IRQ_NONE);c.mask_error=-EIO;assert(!nv_rs_irq_close(irq));pci.removed=false;c.mask_error=0;tests++;
reset();module_available=0;assert(nv_rs_irq_prepare(&pci,7,4,&ops,&c,&irq)==-ENODEV&&pci.refs==1);tests++;
reset();assert(nv_rs_irq_prepare(&pci,0,4,&ops,&c,&irq)==-EINVAL);assert(nv_rs_irq_prepare(&pci,7,9,&ops,&c,&irq)==-EINVAL);assert(nv_rs_irq_prepare(&pci,7,4,&ops,NULL,&irq)==-EINVAL);tests++;
reset();c.top_result=99;assert(!nv_rs_irq_prepare(&pci,7,4,&ops,&c,&irq));assert(!nv_rs_irq_arm(irq));assert(handlers[0].top(100,handlers[0].cookie)==IRQ_NONE);assert(!nv_rs_irq_close(irq));tests++;
reset();
vector_result = 8;
assert(!nv_rs_irq_prepare_cold_pci(&pci, 1, 32, true, true, &ops, &c, &irq));
assert(attempt_count == 1 && attempt_flags[0] == PCI_IRQ_MSIX);
assert(attempt_min[0] == 8 && attempt_max[0] == 8);
assert(!nv_rs_irq_close(irq));
tests++;
reset();
rejected_modes = PCI_IRQ_MSIX | PCI_IRQ_MSI;
choose_mode = PCI_IRQ_LEGACY;
vector_result = 1;
assert(!nv_rs_irq_prepare_cold_pci(&pci, 1, 4, true, true, &ops, &c, &irq));
assert(attempt_count == 3);
assert(attempt_flags[0] == PCI_IRQ_MSIX && attempt_min[0] == 4 && attempt_max[0] == 4);
assert(attempt_flags[1] == PCI_IRQ_MSI && attempt_min[1] == 1 && attempt_max[1] == 1);
assert(attempt_flags[2] == PCI_IRQ_LEGACY && attempt_min[2] == 1 && attempt_max[2] == 1);
assert(!nv_rs_irq_close(irq));
tests++;
reset();
fail_request = 1;
assert(nv_rs_irq_prepare_cold_pci(&pci, 1, 2, true, true, &ops, &c, &irq) == -EBUSY);
assert(attempt_count == 1 && !irq && !requested_handlers && !allocated_vectors);
tests++;
reset();
choose_mode = PCI_IRQ_LEGACY;
vector_result = 1;
assert(!nv_rs_irq_prepare_cold_pci(&pci, 2, 4, true, true, &ops, &c, &irq));
assert(attempt_count == 1 && attempt_flags[0] == PCI_IRQ_LEGACY);
assert(!nv_rs_irq_close(irq));
tests++;
reset();
assert(nv_rs_irq_prepare_cold_pci(&pci, 0, 4, true, false, &ops, &c, &irq) == -EIO);
assert(attempt_count == 0 && !irq);
tests++;
reset();
assert(!nv_rs_irq_register_cold_pci(&pci, 1, 2, true, true, &irq));
assert(requested_handlers == 2 && nv_rs_irq_arm(irq) == -EOPNOTSUPP);
assert(handlers[0].top(100, handlers[0].cookie) == IRQ_NONE);
assert(handlers[0].thread(100, handlers[0].cookie) == IRQ_NONE);
assert(enables == 0 && !nv_rs_irq_close(irq));
assert(pci.refs == 1 && syncs >= 2);
tests++;
reset();
assert(!nv_rs_irq_register_cold_pci(&pci, 1, 2, true, true, &irq));
c.mask_error = -EIO;
assert(nv_rs_irq_bind(irq, &ops, &c) == -EIO);
assert(nv_rs_irq_arm(irq) == -EOPNOTSUPP && enables == 0);
c.mask_error = 0;
assert(!nv_rs_irq_bind(irq, &ops, &c));
assert(nv_rs_irq_bind(irq, &ops, &c) == -EBUSY);
c.top_result = NV_RS_IRQ_WAKE_THREAD;
assert(!nv_rs_irq_arm(irq));
assert(handlers[0].top(100, handlers[0].cookie) == IRQ_WAKE_THREAD);
assert(handlers[0].thread(100, handlers[0].cookie) == IRQ_HANDLED);
assert(!nv_rs_irq_close(irq) && pci.refs == 1);
tests++;
reset();
fail_request = 1;
assert(nv_rs_irq_register_cold_pci(&pci, 1, 2, true, true, &irq) == -EBUSY);
assert(!irq && !requested_handlers && !allocated_vectors && pci.refs == 1);
tests++;
reset();
runtime_kthread = true;
choose_mode = PCI_IRQ_MSI;
vector_result = 1;
assert(!nv_rs_irq_prepare(&pci, 7, 4, &ops, &c, &irq));
assert(module_refs == 0 && pci.refs == 2 && requested_handlers == 1);
assert(!nv_rs_irq_arm(irq));
c.mask_error = -EIO;
assert(nv_rs_irq_close(irq) == -EIO && module_refs == 1 && requested_handlers == 1);
c.mask_error = 0;
assert(!nv_rs_irq_close(irq) && module_refs == 0 && pci.refs == 1 && requested_handlers == 0);
tests++;
reset();
runtime_kthread = true;
choose_mode = PCI_IRQ_MSI;
vector_result = 1;
assert(!nv_rs_irq_prepare(&pci, 7, 4, &ops, &c, &irq));
assert(module_refs == 0 && pci.refs == 2 && requested_handlers == 1);
assert(!nv_rs_irq_arm(irq));
c.mask_error = -EIO;
module_available = 0;
assert(!nv_rs_irq_close(irq) && module_refs == 0 && pci.refs == 1 && requested_handlers == 0 && allocated_vectors == 0);
c.mask_error = 0;
tests++;
reset();
runtime_kthread = true;
choose_mode = PCI_IRQ_LEGACY;
vector_result = 1;
assert(!nv_rs_irq_prepare(&pci, 7, 4, &ops, &c, &irq));
assert(module_refs == 0 && pci.refs == 2 && requested_handlers == 1);
assert(!nv_rs_irq_arm(irq));
c.mask_error = -EIO;
module_available = 0;
assert(!nv_rs_irq_close(irq) && module_refs == 0 && pci.refs == 1 && requested_handlers == 0 && allocated_vectors == 0 && intx_disabled == 1 && disables == 0);
c.mask_error = 0;
tests++;
reset();
return tests == 23 ? 0 : 1;
}