Files
nvdriver/tests/dma-mock/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

180 lines
9.6 KiB
C

#define NV_RS_DMA_HOST_FIXTURE
#include <stdatomic.h>
#define nv_rs_dma_context_close fixture_dma_context_close_inner
#include "../../kernel-open/nvidia/dma_adapter.c"
#undef nv_rs_dma_context_close
static unsigned fixture_close_failures;
int nv_rs_dma_context_close(struct nv_rs_dma_context *context)
{
if (fixture_close_failures) { fixture_close_failures--; return -EBUSY; }
return fixture_dma_context_close_inner(context);
}
void fixture_fail_context_closes(unsigned count){fixture_close_failures=count;}
void fixture_pci_remove(void *value){((struct nv_rs_pci*)value)->removed=true;}
static bool fixture_dma_pending;
void fixture_set_dma_pending(bool pending){fixture_dma_pending=pending;}
void nv_rs_pci_retain_devinit_dma(struct nv_rs_pci *pci){assert(!pci->removed);}
int nv_rs_pci_drain_removed_dma(struct nv_rs_pci *pci)
{
return pci->removed && !fixture_dma_pending ? 0 : -EBUSY;
}
static atomic_uint fixture_module_references;
static atomic_uint fixture_failed_module_gets;
bool nv_rs_gpu_module_get(void)
{
if (atomic_load(&fixture_failed_module_gets)) {
atomic_fetch_sub(&fixture_failed_module_gets,1);return false;
}
atomic_fetch_add(&fixture_module_references,1);return true;
}
void nv_rs_gpu_module_put(void){atomic_fetch_sub(&fixture_module_references,1);}
bool nv_rs_is_runtime_kthread(void){return false;}
unsigned fixture_module_pins(void){return atomic_load(&fixture_module_references);}
void fixture_fail_module_gets(unsigned n){atomic_store(&fixture_failed_module_gets,n);}
void *fixture_pci_create(void){struct nv_rs_pci*p=calloc(1,sizeof(*p));if(!p)return NULL;p->refs=1;p->mask=DMA_BIT_MASK(40);p->dev.dma_mask=&p->mask;p->dev.coherent_dma_mask=p->mask;mock_pci=p;return p;}
int fixture_pci_close(void *value){struct nv_rs_pci*p=value;if(p->refs!=1||p->dma_users||mock_live_allocations)return -EBUSY;free(p);return 0;}
unsigned fixture_live_dma(void){return mock_live_allocations;}
unsigned fixture_full_barriers(void){return mock_full_barriers;}
/* MMIO dispatch mocks for GPU lifetime integration. Actual PCI adapter access
* and removal serialization are exercised in the EDU guest instead. */
static unsigned fixture_register_calls;
static u32 fixture_register_value;
static bool fixture_fault_size_enabled, fixture_fault_size_held;
static u32 fixture_fault_size_value, fixture_fault_size_default;
static unsigned fixture_fault_size_writes, fixture_fault_size_fail_at;
void fixture_fault_size_config(u32 value,u32 capacity,unsigned fail_at)
{ fixture_fault_size_enabled=true;fixture_fault_size_held=false;mock_require_op_allocation=true;fixture_fault_size_value=value;
fixture_fault_size_default=capacity;fixture_fault_size_writes=0;fixture_fault_size_fail_at=fail_at; }
int fixture_fault_size_lock(void)
{ if(fixture_fault_size_held)return -EBUSY;fixture_fault_size_held=true;mock_op_allocation_held=true;return 0; }
void fixture_fault_size_unlock(void){assert(fixture_fault_size_held);fixture_fault_size_held=false;mock_op_allocation_held=false;}
unsigned fixture_fault_size_issued(void){return fixture_fault_size_writes;}
bool fixture_fault_size_locked(void){return fixture_fault_size_held;}
void fixture_fault_size_disable(void){assert(!fixture_fault_size_held);fixture_fault_size_enabled=false;mock_require_op_allocation=false;}
static bool fixture_bind_enabled,fixture_bind_fail_write;
static u32 fixture_bind_word,fixture_bind_states[8];
static unsigned fixture_bind_count,fixture_bind_cursor,fixture_bind_reads,fixture_bind_fence;
void fixture_bind_config(const u32 *states,unsigned count,bool fail_write)
{assert(count&&count<=8);fixture_bind_enabled=true;fixture_bind_fail_write=fail_write;
memcpy(fixture_bind_states,states,count*sizeof(u32));fixture_bind_count=count;
fixture_bind_cursor=fixture_bind_reads=0;fixture_bind_word=0;fixture_bind_fence=mock_full_barriers;}
u32 fixture_bind_last_word(void){return fixture_bind_word;}
unsigned fixture_bind_status_reads(void){return fixture_bind_reads;}
void fixture_bind_disable(void){assert(!fixture_fault_size_held);fixture_bind_enabled=false;}
static bool fixture_tlb_enabled,fixture_tlb_fail,fixture_tlb_busy;
static unsigned fixture_tlb_stage,fixture_tlb_flushes;
static u32 fixture_tlb_low,fixture_tlb_high,fixture_tlb_command;
void fixture_tlb_config(bool fail,bool busy) {fixture_tlb_enabled=true;fixture_tlb_fail=fail;fixture_tlb_busy=busy;fixture_tlb_stage=fixture_tlb_flushes=0;fixture_tlb_low=fixture_tlb_high=fixture_tlb_command=0;}
void fixture_tlb_disable(void){assert(!fixture_fault_size_held);fixture_tlb_enabled=false;}
unsigned fixture_tlb_flush_count(void){return fixture_tlb_flushes;}
u32 fixture_tlb_target_low(void){return fixture_tlb_low;}
u32 fixture_tlb_target_high(void){return fixture_tlb_high;}
u32 fixture_tlb_trigger(void){return fixture_tlb_command;}
void fixture_register_reset(void){fixture_register_calls=0;fixture_register_value=0;}
unsigned fixture_register_accesses(void){return fixture_register_calls;}
#include "vram_model.h"
/* No PCI Device Serial Number capability in the fixture; pci_get_dsn() returns 0 in that case. */
u64 nv_rs_pci_dsn(struct nv_rs_pci *p){(void)p;return 0;}
int nv_rs_pci_read32(struct nv_rs_pci *p,u64 offset,u32 *value)
{
fixture_register_calls++;
if(p->removed)return -ENODEV;
if(fixture_vram_enabled)return fixture_vram_read(offset,value);
if(fixture_fault_size_enabled && offset==0x100e34) {
if(!fixture_fault_size_held)return -EPERM;
*value=fixture_fault_size_value;return 0;
}
if(fixture_bind_enabled && offset==0x1710) {
if(!fixture_fault_size_held || mock_full_barriers<=fixture_bind_fence)return -EPERM;
fixture_bind_reads++;*value=fixture_bind_states[fixture_bind_cursor];
if(fixture_bind_cursor+1<fixture_bind_count)fixture_bind_cursor++;
return 0;
}
if(fixture_tlb_enabled && (offset==0x100c80 || offset==0x70000)) {
if(!fixture_fault_size_held)return -EPERM;
if(offset==0x100c80){assert(fixture_tlb_stage==0 || fixture_tlb_stage==3);*value=fixture_tlb_stage==0?0x10000:0x8000;}
else {assert(fixture_tlb_flushes>0);*value=fixture_tlb_busy?3:0;}
return 0;
}
if(offset!=0x50)return -EINVAL;
*value=fixture_register_value;return 0;
}
int nv_rs_pci_write32(struct nv_rs_pci *p,u64 offset,u32 value)
{
fixture_register_calls++;
if(p->removed)return -ENODEV;
if(fixture_vram_enabled)return fixture_vram_write(offset,value);
if(fixture_fault_size_enabled && offset==0x100e34) {
if(!fixture_fault_size_held)return -EPERM;
fixture_fault_size_writes++;
if(fixture_fault_size_writes==fixture_fault_size_fail_at)return -EIO;
fixture_fault_size_value=(value&0x40000000)?((value&0xfff00000)|fixture_fault_size_default):value;
return 0;
}
if(fixture_bind_enabled && offset==0x1714) {
if(!fixture_fault_size_held)return -EPERM;
fixture_bind_fence=mock_full_barriers;
if(fixture_bind_fail_write)return -EIO;
fixture_bind_word=value;return 0;
}
if(fixture_tlb_enabled && (offset==0x100cb8 || offset==0x100cec || offset==0x100cbc || offset==0x70000)) {
if(!fixture_fault_size_held)return -EPERM;
if(offset==0x100cb8){assert(fixture_tlb_stage==0);fixture_tlb_low=value;fixture_tlb_stage=1;}
else if(offset==0x100cec){assert(fixture_tlb_stage==1);fixture_tlb_high=value;fixture_tlb_stage=2;}
else if(offset==0x100cbc){assert(fixture_tlb_stage==2);fixture_tlb_command=value;fixture_tlb_stage=3;if(fixture_tlb_fail)return -EIO;}
else {assert(fixture_tlb_stage==3);assert(value==1);fixture_tlb_flushes++;}
return 0;
}
if(offset!=0x50)return -EINVAL;
fixture_register_value=value;return 0;
}
/* Device producer model targets the last actual coherent allocation. The test
* writes payload, then valid; it is not an independent fake Rust backing. */
int fixture_fault_produce(unsigned index, const u32 *packet)
{
size_t offset=(size_t)index*32;
if(!mock_last_cpu || offset>mock_last_bytes || mock_last_bytes-offset<32)return -EINVAL;
memcpy((char*)mock_last_cpu+offset,packet,28);
dma_wmb();
WRITE_ONCE(*(u32*)((char*)mock_last_cpu+offset+28),packet[7]);
return 0;
}
int fixture_fault_word(unsigned index, unsigned word, u32 *value)
{
size_t offset=(size_t)index*32+(size_t)word*4;
if(!mock_last_cpu || word>=8 || offset>mock_last_bytes || mock_last_bytes-offset<4)return -EINVAL;
*value=READ_ONCE(*(u32*)((char*)mock_last_cpu+offset));return 0;
}
unsigned fixture_dma_acquires(void){return mock_cpu_barriers;}
unsigned fixture_dma_releases(void){return mock_device_barriers;}
static bool fault_cpu_alloc_fail;
static unsigned fault_cpu_free_fail, fault_cpu_live;
void fixture_fault_cpu_fail_alloc(bool fail){fault_cpu_alloc_fail=fail;}
void fixture_fault_cpu_fail_free(unsigned count){fault_cpu_free_fail=count;}
unsigned fixture_fault_cpu_live(void){return fault_cpu_live;}
void *nv_rs_clock_store_alloc(size_t bytes){void*p;if(fault_cpu_alloc_fail)return NULL;p=calloc(1,bytes);if(p)fault_cpu_live++;return p;}
int nv_rs_clock_store_free(void *p){if(fault_cpu_free_fail){fault_cpu_free_fail--;return -EAGAIN;}if(p){assert(fault_cpu_live>0);fault_cpu_live--;free(p);}return 0;}
static bool event_process=true;
void fixture_event_process(bool value){event_process=value;}
bool nv_rs_clock_store_process_context(void){return event_process;}
/* Device-producer model writes the real coherent allocation identified by its
* DMA mapping, independently of Rust's unpublished CPU writer API. */
int fixture_small_vm_produce(u64 address, unsigned index, u32 value)
{
for(unsigned i=0;i<256;i++) {
struct mock_dma_record *r=&mock_dma_records[i];
if(r->cpu && r->address==address && (u64)index*4+4<=r->bytes) {
WRITE_ONCE(((u32*)r->cpu)[index],value);dma_wmb();return 0;
}
}
return -EINVAL;
}
void fixture_fail_nth_dma(unsigned n){mock_alloc_fail_at=n?mock_alloc_calls+n:0;}