Files
nvdriver/kernel-open/nvidia/irq_adapter.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

362 lines
13 KiB
C

// SPDX-License-Identifier: MIT
/* Linux IRQ lifetime adapter. Actual GPU interrupt semantics are HAL callbacks. */
#ifdef NV_RS_IRQ_HOST_FIXTURE
#include "irq_mock.h"
#else
#include <linux/pci.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/module.h>
#include <linux/version.h>
#include "gpu_owner.h"
#endif
#include "pci_adapter.h"
#include "irq_adapter.h"
/* PCI_IRQ_INTX replaced PCI_IRQ_LEGACY in 6.10 and was backported to RHEL 9.8 (5.14);
* both are the same bit (1 << 0). Keying on macro presence works on mainline and vendor trees. */
#ifndef PCI_IRQ_INTX
#define PCI_IRQ_INTX PCI_IRQ_LEGACY
#endif
#define NV_RS_PCI_IRQ_INTX PCI_IRQ_INTX
struct nv_rs_irq_vector { struct nv_rs_irq *owner; unsigned int index; int linux_irq; bool requested; };
struct nv_rs_irq {
struct nv_rs_pci *pci;
struct pci_dev *pdev;
struct mutex transition;
struct nv_rs_irq_ops ops;
void *context;
unsigned int count, mode;
bool armed, lines_enabled, pinned_module;
struct nv_rs_irq_vector vectors[];
};
static irqreturn_t nv_rs_irq_top(int number, void *cookie)
{
struct nv_rs_irq_vector *v = cookie;
struct nv_rs_irq *owner = v->owner;
unsigned int result;
(void)number;
if (!smp_load_acquire(&owner->armed) || nv_rs_pci_is_removed(owner->pci))
return IRQ_NONE;
result = owner->ops.top(owner->context, v->index);
if (result == NV_RS_IRQ_HANDLED) return IRQ_HANDLED;
if (result == NV_RS_IRQ_WAKE_THREAD) return IRQ_WAKE_THREAD;
return IRQ_NONE;
}
static irqreturn_t nv_rs_irq_thread(int number, void *cookie)
{
struct nv_rs_irq_vector *v = cookie;
struct nv_rs_irq *owner = v->owner;
(void)number;
if (!smp_load_acquire(&owner->armed) || nv_rs_pci_is_removed(owner->pci)) return IRQ_NONE;
owner->ops.thread(owner->context, v->index);
return IRQ_HANDLED;
}
static void nv_rs_irq_free_handlers(struct nv_rs_irq *owner)
{
unsigned int i;
for (i = 0; i < owner->count; i++) {
struct nv_rs_irq_vector *v = &owner->vectors[i];
if (v->requested) {
synchronize_irq(v->linux_irq);
free_irq(v->linux_irq, v);
v->requested = false;
}
}
}
/* Cold Linux PCI startup tries exact MSI-X, single MSI, then INTx.
* Handler installation happens only after a vector allocation succeeds. */
static int nv_rs_irq_alloc_cold(struct nv_rs_irq *owner, unsigned int allowed,
unsigned int msix_vectors)
{
int result;
if (allowed & NV_RS_IRQ_MSIX) {
result = pci_alloc_irq_vectors(owner->pdev, msix_vectors, msix_vectors, PCI_IRQ_MSIX);
if (result >= 0)
return result;
}
if (allowed & NV_RS_IRQ_MSI) {
result = pci_alloc_irq_vectors(owner->pdev, 1, 1, PCI_IRQ_MSI);
if (result >= 0)
return result;
}
if (allowed & NV_RS_IRQ_INTX)
return pci_alloc_irq_vectors(owner->pdev, 1, 1, NV_RS_PCI_IRQ_INTX);
return -EIO;
}
static bool valid_callbacks(const struct nv_rs_irq_ops *ops, void *context)
{
return context && ops && ops->mask && ops->unmask && ops->top && ops->thread;
}
static int nv_rs_irq_prepare_internal(struct nv_rs_pci *pci, unsigned int allowed,
unsigned int max_vectors, bool cold_pci, bool unbound, const struct nv_rs_irq_ops *ops,
void *context, struct nv_rs_irq **out)
{
struct nv_rs_irq *owner;
unsigned int i, flags = 0;
bool pin_module;
int error;
if (!out) return -EINVAL;
*out = NULL;
if (!pci || (!unbound && !valid_callbacks(ops, context)) ||
!max_vectors || max_vectors > NV_RS_IRQ_MAX_VECTORS || !allowed || (allowed & ~7U)) return -EINVAL;
if (!nv_rs_pci_get(pci)) return -ENODEV;
if (nv_rs_pci_is_removed(pci)) { error = -ENODEV; goto fail_lease; }
pin_module = !nv_rs_is_runtime_kthread();
if (pin_module && !try_module_get(THIS_MODULE)) { error = -ENODEV; goto fail_lease; }
owner = kzalloc(sizeof(*owner) + max_vectors * sizeof(owner->vectors[0]), GFP_KERNEL);
if (!owner) { error = -ENOMEM; goto fail_module; }
owner->pinned_module = pin_module;
owner->pci = pci;
owner->pdev = to_pci_dev(nv_rs_pci_device(pci));
mutex_init(&owner->transition);
if (!unbound) {
owner->ops = *ops;
owner->context = context;
error = ops->mask(context);
if (error) goto fail_owner;
}
if (allowed & NV_RS_IRQ_MSIX) flags |= PCI_IRQ_MSIX;
if (allowed & NV_RS_IRQ_MSI) flags |= PCI_IRQ_MSI;
if (allowed & NV_RS_IRQ_INTX) flags |= NV_RS_PCI_IRQ_INTX;
if (cold_pci)
error = nv_rs_irq_alloc_cold(owner, allowed, max_vectors);
else
error = pci_alloc_irq_vectors(owner->pdev, 1, max_vectors, flags);
if (error < 0) goto fail_owner;
owner->count = error;
owner->mode = owner->pdev->msix_enabled ? NV_RS_IRQ_MSIX : owner->pdev->msi_enabled ? NV_RS_IRQ_MSI : NV_RS_IRQ_INTX;
if (owner->mode == NV_RS_IRQ_INTX && owner->count != 1) { error = -EINVAL; goto fail_vectors; }
for (i = 0; i < owner->count; i++) {
struct nv_rs_irq_vector *v = &owner->vectors[i];
unsigned long irqflags = owner->mode == NV_RS_IRQ_INTX ? IRQF_SHARED : IRQF_NO_AUTOEN;
v->owner = owner; v->index = i;
v->linux_irq = pci_irq_vector(owner->pdev, i);
if (v->linux_irq < 0) { error = v->linux_irq; goto fail_handlers; }
error = request_threaded_irq(v->linux_irq, nv_rs_irq_top, nv_rs_irq_thread,
irqflags, "nvidia_rs", v);
if (error) goto fail_handlers;
v->requested = true;
}
if (nv_rs_pci_is_removed(pci)) { error = -ENODEV; goto fail_handlers; }
*out = owner;
return 0;
fail_handlers:
nv_rs_irq_free_handlers(owner);
fail_vectors:
pci_free_irq_vectors(owner->pdev);
fail_owner:
kfree(owner);
fail_module:
if (pin_module)
module_put(THIS_MODULE);
fail_lease:
nv_rs_pci_put(pci);
return error;
}
int nv_rs_irq_prepare(struct nv_rs_pci *pci, unsigned int allowed,
unsigned int max_vectors, const struct nv_rs_irq_ops *ops,
void *context, struct nv_rs_irq **out)
{
return nv_rs_irq_prepare_internal(pci, allowed, max_vectors, false, false, ops, context, out);
}
static int prepare_cold_pci(struct nv_rs_pci *pci, unsigned int msi_config,
unsigned int admitted_msix_vectors, bool msi_capable,
bool legacy_irq_present, bool unbound, const struct nv_rs_irq_ops *ops,
void *context, struct nv_rs_irq **out)
{
unsigned int allowed = legacy_irq_present ? NV_RS_IRQ_INTX : 0;
unsigned int capacity = 1;
if (!out)
return -EINVAL;
*out = NULL;
if (msi_config == 1) {
if (admitted_msix_vectors) {
allowed |= NV_RS_IRQ_MSIX;
capacity = admitted_msix_vectors > NV_RS_IRQ_MAX_VECTORS
? NV_RS_IRQ_MAX_VECTORS : admitted_msix_vectors;
}
if (msi_capable)
allowed |= NV_RS_IRQ_MSI;
}
if (!allowed)
return -EIO;
return nv_rs_irq_prepare_internal(pci, allowed, capacity, true, unbound, ops, context, out);
}
int nv_rs_irq_prepare_cold_pci(struct nv_rs_pci *pci, unsigned int msi_config,
unsigned int admitted_msix_vectors, bool msi_capable,
bool legacy_irq_present, const struct nv_rs_irq_ops *ops,
void *context, struct nv_rs_irq **out)
{
return prepare_cold_pci(pci, msi_config, admitted_msix_vectors, msi_capable,
legacy_irq_present, false, ops, context, out);
}
int nv_rs_irq_register_cold_pci(struct nv_rs_pci *pci, unsigned int msi_config,
unsigned int admitted_msix_vectors, bool msi_capable,
bool legacy_irq_present, struct nv_rs_irq **out)
{
return prepare_cold_pci(pci, msi_config, admitted_msix_vectors, msi_capable,
legacy_irq_present, true, NULL, NULL, out);
}
int nv_rs_irq_bind(struct nv_rs_irq *owner, const struct nv_rs_irq_ops *ops, void *context)
{
int error;
if (!owner || !valid_callbacks(ops, context))
return -EINVAL;
mutex_lock(&owner->transition);
if (owner->context)
error = -EBUSY;
else if (nv_rs_pci_is_removed(owner->pci))
error = -ENODEV;
else {
error = ops->mask(context);
if (!error) {
owner->ops = *ops;
owner->context = context;
}
}
mutex_unlock(&owner->transition);
return error;
}
#ifndef NV_RS_IRQ_HOST_FIXTURE
int nv_rs_irq_cold_capabilities(struct nv_rs_pci *pci, bool query_msix,
unsigned int *msix_vectors,
bool *msi_capable, bool *legacy_irq_present)
{
struct pci_dev *pdev;
int count;
if (!pci || !msix_vectors || !msi_capable || !legacy_irq_present)
return -EINVAL;
if (nv_rs_pci_is_removed(pci))
return -ENODEV;
pdev = to_pci_dev(nv_rs_pci_device(pci));
count = query_msix && pdev->msix_cap ? pci_msix_vec_count(pdev) : 0;
if (count < 0)
return count;
*msix_vectors = count;
*msi_capable = pdev->msi_cap != 0;
*legacy_irq_present = pdev->irq != 0;
return 0;
}
void nv_rs_irq_msix_boot_timeout(struct nv_rs_pci *pci, u8 progress)
{
if (!pci)
return;
dev_warn_ratelimited(nv_rs_pci_device(pci),
"MSI-X admission timed out waiting for GFW boot (progress %#x)\n",
(unsigned int)progress);
}
#endif
int nv_rs_irq_arm(struct nv_rs_irq *owner)
{
unsigned int i;
int error = 0;
if (!owner) return -EINVAL;
mutex_lock(&owner->transition);
if (owner->armed) { error = -EBUSY; goto done; }
if (!owner->context) { error = -EOPNOTSUPP; goto done; }
if (nv_rs_pci_is_removed(owner->pci)) { error = -ENODEV; goto done; }
if (owner->mode != NV_RS_IRQ_INTX) {
for (i = 0; i < owner->count; i++) enable_irq(owner->vectors[i].linux_irq);
owner->lines_enabled = true;
}
/* IRQ handlers must be armed before a source can interrupt on unmask. */
smp_store_release(&owner->armed, true);
error = owner->ops.unmask(owner->context);
if (error) {
int mask_error = owner->ops.mask(owner->context);
if (mask_error && !nv_rs_pci_is_removed(owner->pci)) {
/* Hardware may still interrupt: retain enabled live handlers. */
error = mask_error; goto done;
}
WRITE_ONCE(owner->armed, false);
if (owner->lines_enabled) {
for (i = 0; i < owner->count; i++) disable_irq(owner->vectors[i].linux_irq);
owner->lines_enabled = false;
}
}
done:
mutex_unlock(&owner->transition);
return error;
}
int nv_rs_irq_quiesce(struct nv_rs_irq *owner)
{
unsigned int i;
int error = 0;
if (!owner) return -EINVAL;
mutex_lock(&owner->transition);
if (owner->armed && !nv_rs_pci_is_removed(owner->pci)) {
error = owner->ops.mask(owner->context);
if (error) goto done;
}
WRITE_ONCE(owner->armed, false);
if (owner->lines_enabled) {
for (i = 0; i < owner->count; i++) disable_irq(owner->vectors[i].linux_irq);
owner->lines_enabled = false;
}
for (i = 0; i < owner->count; i++) synchronize_irq(owner->vectors[i].linux_irq);
done:
mutex_unlock(&owner->transition);
return error;
}
int nv_rs_irq_close(struct nv_rs_irq *owner)
{
int error;
bool pinned_module;
if (!owner) return -EINVAL;
error = nv_rs_irq_quiesce(owner);
if (error) {
bool forced_teardown = false;
mutex_lock(&owner->transition);
if (!owner->pinned_module) {
if (try_module_get(THIS_MODULE)) {
owner->pinned_module = true;
} else {
/* Module is already unloading so a pin cannot be acquired;
* silence the device and free handlers so no callback can
* outlive module text. For shared INTx, disable INTx at the
* PCI device command register without touching the shared line. */
unsigned int i;
WRITE_ONCE(owner->armed, false);
if (owner->mode == NV_RS_IRQ_INTX) {
pci_intx(owner->pdev, 0);
} else if (owner->lines_enabled) {
for (i = 0; i < owner->count; i++)
disable_irq(owner->vectors[i].linux_irq);
owner->lines_enabled = false;
}
for (i = 0; i < owner->count; i++)
synchronize_irq(owner->vectors[i].linux_irq);
forced_teardown = true;
}
}
mutex_unlock(&owner->transition);
if (!forced_teardown)
return error;
}
pinned_module = owner->pinned_module;
nv_rs_irq_free_handlers(owner);
pci_free_irq_vectors(owner->pdev);
nv_rs_pci_put(owner->pci);
kfree(owner);
if (pinned_module)
module_put(THIS_MODULE);
return 0;
}
unsigned int nv_rs_irq_vectors(const struct nv_rs_irq *owner) { return owner ? owner->count : 0; }
unsigned int nv_rs_irq_mode(const struct nv_rs_irq *owner) { return owner ? owner->mode : 0; }