dma: device-owned coherent DMA contexts for GV11B platform GPUs (#606)

Add nv_rs_dma_context_create_dev(struct device *, bits, **) and
LinuxDma::from_device so Tegra194 GV11B platform GPUs, which have no
nv_rs_pci lease, can own a coherent DMA context.

ctx->pci is now optional. Creation, close and force_close share
context_init/context_destroy/context_release_owner: a PCI-owned context
releases its DMA user count and PCI reference exactly as before, a
device-owned context drops its get_device() reference with put_device().
Removal checks go through context_removed(), which consults
nv_rs_pci_is_removed only when ctx->pci is set. The PCI path keeps its
check order and errno values (-EINVAL, -ENODEV, -ERANGE, -ENOMEM).

The dma-mock fixture models get_device/put_device and covers the
device-owned lifecycle, every create failure path, force_close, and
independence from PCI removal state.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
BlivionIaG
2026-12-05 16:20:57 +01:00
committed by GitHub
parent cb60725ce0
commit dfda0a29a4
5 changed files with 162 additions and 50 deletions

View File

@ -15,6 +15,7 @@
#endif
#include "dma_adapter.h"
#define NV_RS_DMA_SLOTS 64
#define NV_RS_DMA_MAX_MASK_BITS 64
/*
* Per-allocation ceiling. No GSP boot request needs one huge coherent buffer: the
* `kgspCreateRadix3` page tables are a separate 69,632-byte allocation for the
@ -51,12 +52,70 @@ static u64 allocate_token(void)
old = previous;
}
}
/*
* A context is owned either through a PCI lease (ctx->pci != NULL: DMA user
* count plus PCI reference) or, for platform GPUs such as Tegra194 GV11B,
* through a plain struct device reference (ctx->pci == NULL).
*/
static void context_release_owner(struct device *dev, struct nv_rs_pci *pci)
{
if (pci) {
nv_rs_pci_dma_release(pci);
nv_rs_pci_put(pci);
} else {
put_device(dev);
}
}
static bool context_removed(const struct nv_rs_dma_context *ctx)
{
return ctx->pci && nv_rs_pci_is_removed(ctx->pci);
}
/* Consumes the owner reference on failure. */
static int context_init(struct device *dev, struct nv_rs_pci *pci, unsigned int bits,
struct nv_rs_dma_context **result)
{
struct nv_rs_dma_context *ctx;
u64 mask;
int error;
if (!dev || bits == 0 || bits > NV_RS_DMA_MAX_MASK_BITS || !dev->dma_mask) {
error = -EINVAL;
goto fail;
}
if (pci && nv_rs_pci_is_removed(pci)) {
error = -ENODEV;
goto fail;
}
mask = DMA_BIT_MASK(bits);
if (mask > *dev->dma_mask || mask > dev->coherent_dma_mask) {
error = -ERANGE;
goto fail;
}
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
if (!ctx) {
error = -ENOMEM;
goto fail;
}
ctx->device = dev;
ctx->pci = pci;
mutex_init(&ctx->lock);
ctx->mask = mask;
*result = ctx;
return 0;
fail:
context_release_owner(dev, pci);
return error;
}
static void context_destroy(struct nv_rs_dma_context *ctx)
{
#ifndef NV_RS_DMA_HOST_FIXTURE
mutex_destroy(&ctx->lock);
#endif
context_release_owner(ctx->device, ctx->pci);
kfree(ctx);
}
int nv_rs_dma_context_create(struct nv_rs_pci *pci, unsigned int bits,
struct nv_rs_dma_context **result)
{
struct nv_rs_dma_context *ctx;
struct device *dev;
u64 mask;
int error;
BUILD_BUG_ON(sizeof(struct nv_rs_dma_backing) != 40);
BUILD_BUG_ON(sizeof(struct nv_rs_dma_mapping) != 24);
@ -65,22 +124,15 @@ int nv_rs_dma_context_create(struct nv_rs_pci *pci, unsigned int bits,
if (!pci || !nv_rs_pci_get(pci)) return -ENODEV;
error = nv_rs_pci_dma_acquire(pci, bits);
if (error) { nv_rs_pci_put(pci); return error; }
dev = nv_rs_pci_device(pci);
if (!dev || bits == 0 || bits > 64 || !dev->dma_mask)
{ nv_rs_pci_dma_release(pci); nv_rs_pci_put(pci); return -EINVAL; }
if (nv_rs_pci_is_removed(pci)) { nv_rs_pci_dma_release(pci); nv_rs_pci_put(pci); return -ENODEV; }
mask = DMA_BIT_MASK(bits);
if (mask > *dev->dma_mask || mask > dev->coherent_dma_mask)
{ nv_rs_pci_dma_release(pci); nv_rs_pci_put(pci); return -ERANGE; }
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
if (!ctx)
{ nv_rs_pci_dma_release(pci); nv_rs_pci_put(pci); return -ENOMEM; }
ctx->device = dev;
ctx->pci = pci;
mutex_init(&ctx->lock);
ctx->mask = mask;
*result = ctx;
return 0;
return context_init(nv_rs_pci_device(pci), pci, bits, result);
}
int nv_rs_dma_context_create_dev(struct device *dev, unsigned int bits,
struct nv_rs_dma_context **result)
{
if (!result) return -EINVAL;
*result = NULL;
if (!dev) return -ENODEV;
return context_init(get_device(dev), NULL, bits, result);
}
int nv_rs_dma_context_close(struct nv_rs_dma_context *ctx)
{
@ -90,12 +142,7 @@ int nv_rs_dma_context_close(struct nv_rs_dma_context *ctx)
for (i = 0; i < NV_RS_DMA_SLOTS; ++i)
if (ctx->slots[i].cpu) { mutex_unlock(&ctx->lock); return -EBUSY; }
mutex_unlock(&ctx->lock);
#ifndef NV_RS_DMA_HOST_FIXTURE
mutex_destroy(&ctx->lock);
#endif
nv_rs_pci_dma_release(ctx->pci);
nv_rs_pci_put(ctx->pci);
kfree(ctx);
context_destroy(ctx);
return 0;
}
int nv_rs_dma_context_force_close(struct nv_rs_dma_context *ctx, unsigned int *freed)
@ -111,13 +158,8 @@ int nv_rs_dma_context_force_close(struct nv_rs_dma_context *ctx, unsigned int *f
++count;
}
mutex_unlock(&ctx->lock);
#ifndef NV_RS_DMA_HOST_FIXTURE
mutex_destroy(&ctx->lock);
#endif
if (freed) *freed = count;
nv_rs_pci_dma_release(ctx->pci);
nv_rs_pci_put(ctx->pci);
kfree(ctx);
context_destroy(ctx);
return 0;
}
unsigned int nv_rs_dma_context_live(struct nv_rs_dma_context *ctx)
@ -134,7 +176,7 @@ void nv_rs_dma_context_leaked(struct nv_rs_dma_context *ctx)
{
if (!ctx) return;
dev_warn(ctx->device,
"nvidia_rs: a DMA context was dropped open with %u allocations; its memory and its PCI reference are kept\n",
"nvidia_rs: a DMA context was dropped open with %u allocations; its memory and its device reference are kept\n",
nv_rs_dma_context_live(ctx));
}
static struct nv_rs_dma_slot *lookup(struct nv_rs_dma_context *ctx,
@ -170,7 +212,7 @@ int nv_rs_dma_allocate(struct nv_rs_dma_context *ctx, u64 bytes, u64 alignment,
!is_power_of_2(alignment)) return -EINVAL;
allocated = max_t(size_t, (size_t)bytes, (size_t)alignment);
mutex_lock(&ctx->lock);
if (nv_rs_pci_is_removed(ctx->pci)) { mutex_unlock(&ctx->lock); return -ENODEV; }
if (context_removed(ctx)) { mutex_unlock(&ctx->lock); return -ENODEV; }
token = allocate_token();
if (!token) { mutex_unlock(&ctx->lock); return -ENOSPC; }
for (i = 0; i < NV_RS_DMA_SLOTS && ctx->slots[i].cpu; ++i) {}
@ -178,7 +220,7 @@ int nv_rs_dma_allocate(struct nv_rs_dma_context *ctx, u64 bytes, u64 alignment,
s = &ctx->slots[i];
s->cpu = dma_alloc_coherent(ctx->device, allocated, &s->dma, GFP_KERNEL);
if (!s->cpu) { mutex_unlock(&ctx->lock); return -ENOMEM; }
if (nv_rs_pci_is_removed(ctx->pci)) {
if (context_removed(ctx)) {
dma_free_coherent(ctx->device, allocated, s->cpu, s->dma);
memset(s, 0, sizeof(*s)); mutex_unlock(&ctx->lock); return -ENODEV;
}
@ -202,7 +244,7 @@ int nv_rs_dma_map(struct nv_rs_dma_context *ctx, const struct nv_rs_dma_backing
struct nv_rs_dma_slot *s;
if (!ctx || !result) return -EINVAL;
mutex_lock(&ctx->lock);
if (nv_rs_pci_is_removed(ctx->pci)) { mutex_unlock(&ctx->lock); return -ENODEV; }
if (context_removed(ctx)) { mutex_unlock(&ctx->lock); return -ENODEV; }
s = lookup(ctx, b);
if (!s || s->mapped) { mutex_unlock(&ctx->lock); return s ? -EBUSY : -EINVAL; }
/* dma_alloc_coherent already established the actual mapping. */
@ -301,7 +343,7 @@ static int shared_word(struct nv_rs_dma_context *ctx, const struct nv_rs_dma_bac
if (!ctx || !value || (offset & 3) || check_add_overflow(offset, 4ULL, &end))
return -EINVAL;
mutex_lock(&ctx->lock);
if (nv_rs_pci_is_removed(ctx->pci)) { mutex_unlock(&ctx->lock); return -ENODEV; }
if (context_removed(ctx)) { mutex_unlock(&ctx->lock); return -ENODEV; }
s = lookup(ctx, b);
if (!s || !s->mapped || !mapping_matches(s, mapping) || end > s->bytes) {
mutex_unlock(&ctx->lock); return -EINVAL;
@ -326,7 +368,7 @@ int nv_rs_dma_cpu_ptr(struct nv_rs_dma_context *ctx, const struct nv_rs_dma_back
*cpu_out = NULL;
*bytes_out = 0;
mutex_lock(&ctx->lock);
if (nv_rs_pci_is_removed(ctx->pci)) {
if (context_removed(ctx)) {
mutex_unlock(&ctx->lock);
return -ENODEV;
}

View File

@ -2,6 +2,7 @@
#ifndef NV_RS_DMA_ADAPTER_H
#define NV_RS_DMA_ADAPTER_H
#include <linux/types.h>
struct device;
struct nv_rs_pci;
struct nv_rs_dma_context;
struct nv_rs_dma_backing {
@ -18,6 +19,12 @@ struct nv_rs_dma_mapping { u64 token, address, bytes; };
* keep code loaded until the context is closed. */
int nv_rs_dma_context_create(struct nv_rs_pci *, unsigned int dma_bits,
struct nv_rs_dma_context **);
/* Platform GPUs without a PCI lease (Tegra194 GV11B). The caller has already
* negotiated dev's DMA masks; the context holds a get_device() reference that
* close drops with put_device(). There is no removal state: allocation and
* mapping never report -ENODEV, so the owner must close before unbinding. */
int nv_rs_dma_context_create_dev(struct device *dev, unsigned int dma_bits,
struct nv_rs_dma_context **);
int nv_rs_dma_context_close(struct nv_rs_dma_context *);
/* Last resort when close returns -EBUSY: frees every allocation still held,
* mapped or not, then closes. Only for an owner that has established that the

View File

@ -1,5 +1,7 @@
//! Linux coherent DMA backend for core::dma::Manager. Context creation requires
//! a valid owned PCI lease; no adapter is constructed from a userspace address.
//! a valid owned PCI lease or, for platform GPUs (Tegra194 GV11B), a live
//! driver-bound `struct device`; no adapter is constructed from a userspace
//! address.
use core::{ffi::c_void, ptr::NonNull};
use nv_rs_core::dma::{Backend, BackendError, Backing, DeviceMapping};
#[repr(C)]
@ -44,6 +46,7 @@ unsafe extern "C" {
fn nv_rs_dma_flush_sysmem(context: *mut c_void, backing: *const RawBacking) -> i32;
fn nv_rs_dma_context_create(pci: *mut c_void, bits: u32, result: *mut *mut c_void) -> i32;
fn nv_rs_dma_context_create_dev(dev: *mut c_void, bits: u32, result: *mut *mut c_void) -> i32;
fn nv_rs_dma_read(
ctx: *mut c_void,
backing: *const RawBacking,
@ -113,10 +116,14 @@ fn mapping(m: &DeviceMapping) -> RawMapping {
bytes: m.bytes,
}
}
/// Owns a C context holding its PCI lease. Use Manager::shutdown, then close.
/// Dropping it open frees nothing: outstanding mappings must not be freed
/// implicitly, because the device may still reach them. The drop reports the
/// leak (allocations and the PCI reference stay) in the kernel log.
const EINVAL: i32 = -22;
const ENODEV: i32 = -19;
const EIO: i32 = -5;
/// Owns a C context holding its PCI lease or device reference. Use
/// Manager::shutdown, then close. Dropping it open frees nothing: outstanding
/// mappings must not be freed implicitly, because the device may still reach
/// them. The drop reports the leak (allocations and the PCI lease or device
/// reference stay) in the kernel log.
pub struct LinuxDma {
context: Option<NonNull<c_void>>,
}
@ -141,16 +148,35 @@ impl LinuxDma {
pub unsafe fn from_owned_pci(pci: *mut c_void, bits: u32) -> Result<Self, BackendError> {
unsafe {
if pci.is_null() {
return Err(BackendError(-22));
return Err(BackendError(EINVAL));
}
let mut raw = core::ptr::null_mut();
status(nv_rs_dma_context_create(pci, bits, &mut raw))?;
Ok(Self {
context: Some(NonNull::new(raw).ok_or(BackendError(-5))?),
})
Self::from_raw(raw)
}
}
/// # Safety
/// `dev` must be a live `struct device` of a platform GPU bound to this
/// driver, with its DMA masks already negotiated and wide enough for
/// `bits`. C takes its own `get_device` reference, dropped on close. There
/// is no removal tracking: the owner closes the context before unbinding.
/// The caller keeps this module's code alive until close.
pub unsafe fn from_device(dev: *mut c_void, bits: u32) -> Result<Self, BackendError> {
unsafe {
if dev.is_null() {
return Err(BackendError(ENODEV));
}
let mut raw = core::ptr::null_mut();
status(nv_rs_dma_context_create_dev(dev, bits, &mut raw))?;
Self::from_raw(raw)
}
}
fn from_raw(raw: *mut c_void) -> Result<Self, BackendError> {
Ok(Self {
context: Some(NonNull::new(raw).ok_or(BackendError(EIO))?),
})
}
/// # Safety
/// Caller must establish exclusive CPU ownership of the range or a verified
/// GPU-coherent protocol. No concurrent device writes may race this copy;
/// CPU/device barriers alone do not drain in-flight work.
@ -256,7 +282,7 @@ impl LinuxDma {
&mut bytes,
))?;
if cpu.is_null() {
return Err(BackendError(-5));
return Err(BackendError(EIO));
}
Ok((cpu.cast::<u8>(), bytes as usize))
}
@ -267,7 +293,9 @@ impl LinuxDma {
status(unsafe { nv_rs_dma_flush_sysmem(self.context()?, &backing(b)) })
}
fn context(&self) -> Result<*mut c_void, BackendError> {
self.context.map(NonNull::as_ptr).ok_or(BackendError(-19))
self.context
.map(NonNull::as_ptr)
.ok_or(BackendError(ENODEV))
}
pub fn close_in_place(&mut self) -> Result<(), BackendError> {
let context = self.context()?;

View File

@ -28,7 +28,9 @@ static inline void mutex_lock(struct mutex *m){assert(!pthread_mutex_lock(&m->va
static inline void mutex_unlock(struct mutex *m){assert(!pthread_mutex_unlock(&m->value));}
static inline void *kzalloc(size_t n,int flags){(void)flags;return calloc(1,n);}
static inline void kfree(void *p){free(p);}
struct device {u64 *dma_mask,coherent_dma_mask;};
struct device {u64 *dma_mask,coherent_dma_mask;unsigned refs;};
static inline struct device *get_device(struct device *d){if(d)d->refs++;return d;}
static inline void put_device(struct device *d){if(d){assert(d->refs);d->refs--;}}
struct nv_rs_pci {struct device dev;u64 mask;unsigned refs,dma_users,bits;bool removed;};
static inline bool nv_rs_pci_get(struct nv_rs_pci *p){if(!p||!p->refs)return false;p->refs++;return true;}
static inline void nv_rs_pci_put(struct nv_rs_pci *p){assert(p->refs);p->refs--;}

View File

@ -41,6 +41,39 @@ static void shared_words(void)
}
static void capacity_and_generation(void){struct nv_rs_dma_context*c=open_context();struct nv_rs_dma_backing b[64],other;struct nv_rs_dma_mapping m;for(unsigned i=0;i<64;i++)CHECK(nv_rs_dma_allocate(c,4096,4096,&b[i])==0);CHECK(nv_rs_dma_allocate(c,4096,4096,&other)==-ENOSPC);CHECK(nv_rs_dma_release(c,&b[0])==0);CHECK(nv_rs_dma_allocate(c,4096,4096,&other)==0);CHECK(other.token!=b[0].token);CHECK(nv_rs_dma_map(c,&b[0],&m)==-EINVAL);CHECK(nv_rs_dma_release(c,&other)==0);for(unsigned i=1;i<64;i++)CHECK(nv_rs_dma_release(c,&b[i])==0);atomic_store(&nv_rs_dma_next_token.value,S64_MAX);CHECK(nv_rs_dma_allocate(c,4096,4096,&other)==-ENOSPC);CHECK(nv_rs_dma_context_close(c)==0);}
static void forced_close(void){struct nv_rs_dma_context*c=open_context();struct nv_rs_dma_backing a,b;struct nv_rs_dma_mapping m;unsigned freed=99,before=mock_live_allocations;CHECK(nv_rs_dma_allocate(c,4096,4096,&a)==0);CHECK(nv_rs_dma_allocate(c,8192,4096,&b)==0);CHECK(nv_rs_dma_map(c,&b,&m)==0);CHECK(nv_rs_dma_context_live(c)==2);CHECK(nv_rs_dma_context_close(c)==-EBUSY);CHECK(device.refs==2&&device.dma_users==1);nv_rs_dma_context_leaked(c);CHECK(mock_leak_reports==1&&mock_leak_reported_allocations==2);CHECK(mock_live_allocations==before+2);CHECK(nv_rs_dma_context_force_close(NULL,&freed)==-EINVAL);CHECK(nv_rs_dma_context_force_close(c,&freed)==0);CHECK(freed==2);CHECK(mock_live_allocations==before);CHECK(device.refs==1&&device.dma_users==0);c=open_context();CHECK(nv_rs_dma_context_force_close(c,NULL)==0);CHECK(device.refs==1&&device.dma_users==0);CHECK(nv_rs_dma_context_live(NULL)==0);}
static void platform_device(void)
{
struct device dev;
u64 mask=DMA_BIT_MASK(40);
struct nv_rs_dma_context *c=(void*)&dev;
struct nv_rs_dma_backing b;struct nv_rs_dma_mapping m;unsigned freed=99,before=mock_live_allocations;
memset(&dev,0,sizeof dev);dev.dma_mask=&mask;dev.coherent_dma_mask=mask;
CHECK(nv_rs_dma_context_create_dev(&dev,40,NULL)==-EINVAL);CHECK(dev.refs==0);
CHECK(nv_rs_dma_context_create_dev(NULL,40,&c)==-ENODEV);CHECK(c==NULL);
CHECK(nv_rs_dma_context_create_dev(&dev,0,&c)==-EINVAL);CHECK(c==NULL&&dev.refs==0);
CHECK(nv_rs_dma_context_create_dev(&dev,65,&c)==-EINVAL);CHECK(dev.refs==0);
CHECK(nv_rs_dma_context_create_dev(&dev,41,&c)==-ERANGE);CHECK(c==NULL&&dev.refs==0);
dev.coherent_dma_mask=DMA_BIT_MASK(32);
CHECK(nv_rs_dma_context_create_dev(&dev,40,&c)==-ERANGE);CHECK(dev.refs==0);
dev.coherent_dma_mask=mask;dev.dma_mask=NULL;
CHECK(nv_rs_dma_context_create_dev(&dev,40,&c)==-EINVAL);CHECK(dev.refs==0);
dev.dma_mask=&mask;
CHECK(nv_rs_dma_context_create_dev(&dev,40,&c)==0);CHECK(c!=NULL&&c->pci==NULL&&c->device==&dev&&dev.refs==1);
CHECK(device.refs==1&&device.dma_users==0);
/* A PCI removal elsewhere never affects a device-owned context. */
device.removed=true;
CHECK(nv_rs_dma_allocate(c,4096,4096,&b)==0);CHECK(nv_rs_dma_map(c,&b,&m)==0);
CHECK(nv_rs_dma_write_word(c,&b,&m,0,0x1234)==0);
device.removed=false;
CHECK(nv_rs_dma_context_close(c)==-EBUSY);CHECK(dev.refs==1);
nv_rs_dma_context_leaked(c);CHECK(mock_leak_reported_allocations==1);
CHECK(nv_rs_dma_unmap(c,&b,&m)==0);CHECK(nv_rs_dma_release(c,&b)==0);
CHECK(nv_rs_dma_context_close(c)==0);CHECK(dev.refs==0&&mock_live_allocations==before);
CHECK(nv_rs_dma_context_create_dev(&dev,32,&c)==0);CHECK(dev.refs==1);
CHECK(nv_rs_dma_allocate(c,8192,4096,&b)==0);
CHECK(nv_rs_dma_context_force_close(c,&freed)==0);CHECK(freed==1&&dev.refs==0&&mock_live_allocations==before);
CHECK(device.refs==1&&device.dma_users==0);
}
static void atomic_compare_exchange_contract(void)
{
atomic64_t value;
@ -53,4 +86,4 @@ static void atomic_compare_exchange_contract(void)
CHECK(atomic64_cmpxchg(&value, INT64_MIN, INT64_MAX)==INT64_MIN);
CHECK(atomic64_read(&value)==INT64_MAX);
}
int main(void){atomic_compare_exchange_contract();init();lifecycle();failures();cross_context();cpu_copy();shared_words();forced_close();capacity_and_generation();CHECK(device.refs==1&&device.dma_users==0&&mock_live_allocations==0);printf("DMA actual C adapter fixture: %u checks passed; allocations=%u frees=%u\n",assertions,mock_alloc_calls,mock_free_calls);return 0;}
int main(void){atomic_compare_exchange_contract();init();lifecycle();failures();cross_context();cpu_copy();shared_words();forced_close();platform_device();capacity_and_generation();CHECK(device.refs==1&&device.dma_users==0&&mock_live_allocations==0);printf("DMA actual C adapter fixture: %u checks passed; allocations=%u frees=%u\n",assertions,mock_alloc_calls,mock_free_calls);return 0;}