Files
nvdriver/kernel-open/nvidia/dma_adapter.c
Eric Hartford c553930fd7 wave 1: kernel/ARM64/Clang/DKMS compat, IOMMU/PCI lifecycle, and RMAPI/UVM isolation
Proactive 100% parity Wave 1 (REA-grounded vs stock 580.178.04):
- Kernel/build compat: Kbuild header probes for timer_delete_sync, vm_flags_set,
  vma_flags_set_word; remove broken UTS_UBUNTU_RELEASE_ABI guard on
  pci_resize_resource; guard x86 set_memory_uc/wb under CONFIG_X86; remove
  illegal ARM64 dma_sync_single_* on coherent DMA; gate nvidia_rs_drm on
  CONFIG_DRM; default LD=ld.lld/OBJCOPY=llvm-objcopy under LLVM=1; fix dkms.conf
  (KDIR support, all 4 modules, 0.2.0) and install.sh (all 4 modules, non-0000
  PCI domains, KREL override).
- IOMMU & PCI lifecycle: switch gsp_route error notifier to per-device DMA
  (nv_rs_rmapi_os_page_dma); add PCI .shutdown (pci_clear_master + udelay(1))
  and AER .err_handler; keep freed rmapi_allocs on list until users==0 for
  hot-unplug DMA teardown; drain nv_rs_uvm_cpu_dma_ht on exit; lock BAR0 fill
  under p->access; raise MAX_BRIDGE_GPUS to 32.
- Security & isolation (REA _nv000655rm / _nv000663rm): enforce cross-process
  share policy on NV_ESC_RM_DUP_OBJECT & NV_ESC_RM_SHARE; refcount shared memory
  backings on release (eliminating dup UAF); scope UVM ManagedFaultMapper by
  faulting GPU & SysmemPageTree.owner; pass real task_tgid_vnr into Caller.pid.
2026-12-01 16:16:09 +00:00

325 lines
13 KiB
C

// SPDX-License-Identifier: MIT
#ifdef NV_RS_DMA_HOST_FIXTURE
#include "dma_mock.h"
#else
#include <linux/atomic.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/mutex.h>
#include <linux/overflow.h>
#include <linux/slab.h>
#include "pci_adapter.h"
#endif
#include "dma_adapter.h"
#define NV_RS_DMA_SLOTS 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
* 580.178.04 TU10x container and the 30,469,792-byte `.fwimage` is requested in
* 2 MiB runs whose addresses fill the level-2 PTEs, so the largest single request
* on that path is one 2 MiB run. The cap is kept as a general upper sanity bound
* for every caller of this adapter; the real limits remain the device DMA mask and
* host fragmentation, which can still fail a request with -ENOMEM.
*/
#define NV_RS_DMA_MAX_BYTES (64UL * 1024 * 1024)
struct nv_rs_dma_slot {
u64 token, bytes, alignment;
size_t allocated;
void *cpu;
dma_addr_t dma;
bool mapped;
};
struct nv_rs_dma_context {
struct device *device;
struct nv_rs_pci *pci;
struct mutex lock;
u64 mask;
struct nv_rs_dma_slot slots[NV_RS_DMA_SLOTS];
};
static atomic64_t nv_rs_dma_next_token = ATOMIC64_INIT(1);
static u64 allocate_token(void)
{
s64 old = atomic64_read(&nv_rs_dma_next_token);
for (;;) {
s64 previous;
if (old <= 0 || old == S64_MAX) return 0;
previous = atomic64_cmpxchg(&nv_rs_dma_next_token, old, old + 1);
if (previous == old) return (u64)old;
old = previous;
}
}
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);
if (!result) return -EINVAL;
*result = NULL;
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 || !result || 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;
}
int nv_rs_dma_context_close(struct nv_rs_dma_context *ctx)
{
unsigned int i;
if (!ctx) return -EINVAL;
mutex_lock(&ctx->lock);
for (i = 0; i < NV_RS_DMA_SLOTS; ++i)
if (ctx->slots[i].cpu) { mutex_unlock(&ctx->lock); return -EBUSY; }
mutex_unlock(&ctx->lock);
nv_rs_pci_dma_release(ctx->pci);
nv_rs_pci_put(ctx->pci);
kfree(ctx);
return 0;
}
int nv_rs_dma_context_force_close(struct nv_rs_dma_context *ctx, unsigned int *freed)
{
unsigned int i, count = 0;
if (!ctx) return -EINVAL;
mutex_lock(&ctx->lock);
for (i = 0; i < NV_RS_DMA_SLOTS; ++i) {
struct nv_rs_dma_slot *s = &ctx->slots[i];
if (!s->cpu) continue;
dma_free_coherent(ctx->device, s->allocated, s->cpu, s->dma);
memset(s, 0, sizeof(*s));
++count;
}
mutex_unlock(&ctx->lock);
if (freed) *freed = count;
nv_rs_pci_dma_release(ctx->pci);
nv_rs_pci_put(ctx->pci);
kfree(ctx);
return 0;
}
unsigned int nv_rs_dma_context_live(struct nv_rs_dma_context *ctx)
{
unsigned int i, count = 0;
if (!ctx) return 0;
mutex_lock(&ctx->lock);
for (i = 0; i < NV_RS_DMA_SLOTS; ++i)
if (ctx->slots[i].cpu) ++count;
mutex_unlock(&ctx->lock);
return count;
}
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",
nv_rs_dma_context_live(ctx));
}
static struct nv_rs_dma_slot *lookup(struct nv_rs_dma_context *ctx,
const struct nv_rs_dma_backing *backing)
{
unsigned int i;
if (!backing || backing->token == 0 || backing->reserved != 0 || backing->coherent != 1)
return NULL;
for (i = 0; i < NV_RS_DMA_SLOTS; ++i) {
struct nv_rs_dma_slot *s = &ctx->slots[i];
if (s->cpu && s->token == backing->token &&
backing->cpu_token == s->token && s->bytes == backing->bytes &&
s->alignment == backing->alignment)
return s;
}
return NULL;
}
static bool mapping_matches(const struct nv_rs_dma_slot *s,
const struct nv_rs_dma_mapping *mapping)
{
return mapping && mapping->token == s->token && mapping->address == s->dma &&
mapping->bytes == s->bytes;
}
int nv_rs_dma_allocate(struct nv_rs_dma_context *ctx, u64 bytes, u64 alignment,
struct nv_rs_dma_backing *result)
{
unsigned int i;
size_t allocated;
u64 last, token;
struct nv_rs_dma_slot *s;
if (!ctx || !result || bytes == 0 || bytes > NV_RS_DMA_MAX_BYTES ||
alignment < PAGE_SIZE || alignment > NV_RS_DMA_MAX_BYTES ||
!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; }
token = allocate_token();
if (!token) { mutex_unlock(&ctx->lock); return -ENOSPC; }
for (i = 0; i < NV_RS_DMA_SLOTS && ctx->slots[i].cpu; ++i) {}
if (i == NV_RS_DMA_SLOTS) { mutex_unlock(&ctx->lock); return -ENOSPC; }
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)) {
dma_free_coherent(ctx->device, allocated, s->cpu, s->dma);
memset(s, 0, sizeof(*s)); mutex_unlock(&ctx->lock); return -ENODEV;
}
if (((u64)s->dma & (alignment - 1)) ||
check_add_overflow((u64)s->dma, bytes - 1, &last) || last > ctx->mask) {
dma_free_coherent(ctx->device, allocated, s->cpu, s->dma);
memset(s, 0, sizeof(*s)); mutex_unlock(&ctx->lock); return -ERANGE;
}
s->token = token;
s->bytes = bytes; s->alignment = alignment; s->allocated = allocated; s->mapped = false;
/* Memory may contain data from earlier allocations; initialize before any
* caller can publish mappings or DMA descriptors referencing this backing. */
memset(s->cpu, 0, allocated);
*result = (struct nv_rs_dma_backing){ .token = s->token, .cpu_token = s->token,
.bytes = bytes, .alignment = alignment, .coherent = 1 };
mutex_unlock(&ctx->lock); return 0;
}
int nv_rs_dma_map(struct nv_rs_dma_context *ctx, const struct nv_rs_dma_backing *b,
struct nv_rs_dma_mapping *result)
{
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; }
s = lookup(ctx, b);
if (!s || s->mapped) { mutex_unlock(&ctx->lock); return s ? -EBUSY : -EINVAL; }
/* dma_alloc_coherent already established the actual mapping. */
dma_wmb(); /* Publish initialized coherent backing before handing out DMA identity. */
s->mapped = true;
*result = (struct nv_rs_dma_mapping){.token = s->token, .address = s->dma, .bytes = s->bytes};
mutex_unlock(&ctx->lock); return 0;
}
static int sync_range(struct nv_rs_dma_context *ctx, const struct nv_rs_dma_backing *b,
const struct nv_rs_dma_mapping *mapping, u64 offset, u64 bytes,
bool for_device)
{
struct nv_rs_dma_slot *s;
u64 end;
if (!ctx || bytes == 0 || check_add_overflow(offset, bytes, &end)) return -EINVAL;
mutex_lock(&ctx->lock); s = lookup(ctx, b);
if (!s || !s->mapped || !mapping_matches(s, mapping) || end > s->bytes) {
mutex_unlock(&ctx->lock); return -EINVAL;
}
/* Coherent memory needs no streaming DMA sync, but ordering remains explicit.
* Neither barrier waits for GPU work; caller must retain in-flight pins. */
if (for_device) dma_wmb(); else dma_rmb();
mutex_unlock(&ctx->lock); return 0;
}
int nv_rs_dma_sync_device(struct nv_rs_dma_context *c, const struct nv_rs_dma_backing *b,
const struct nv_rs_dma_mapping *m, u64 o, u64 n)
{ return sync_range(c, b, m, o, n, true); }
int nv_rs_dma_sync_cpu(struct nv_rs_dma_context *c, const struct nv_rs_dma_backing *b,
const struct nv_rs_dma_mapping *m, u64 o, u64 n)
{ return sync_range(c, b, m, o, n, false); }
int nv_rs_dma_unmap(struct nv_rs_dma_context *ctx, const struct nv_rs_dma_backing *b,
const struct nv_rs_dma_mapping *mapping)
{
struct nv_rs_dma_slot *s;
if (!ctx) return -EINVAL;
mutex_lock(&ctx->lock); s = lookup(ctx, b);
if (!s || !s->mapped || !mapping_matches(s, mapping)) {
mutex_unlock(&ctx->lock); return -EINVAL;
}
/* Logical unmap only: coherent mapping exists until release. No GPU or IOMMU
* quiescence is implied. The owner must remove GPU PTEs and drain work first. */
s->mapped = false;
mutex_unlock(&ctx->lock); return 0;
}
int nv_rs_dma_release(struct nv_rs_dma_context *ctx, const struct nv_rs_dma_backing *b)
{
struct nv_rs_dma_slot *s;
if (!ctx) return -EINVAL;
mutex_lock(&ctx->lock); s = lookup(ctx, b);
if (!s || s->mapped) { mutex_unlock(&ctx->lock); return s ? -EBUSY : -EINVAL; }
dma_free_coherent(ctx->device, s->allocated, s->cpu, s->dma);
memset(s, 0, sizeof(*s)); mutex_unlock(&ctx->lock); return 0;
}
int nv_rs_dma_read(struct nv_rs_dma_context *ctx, const struct nv_rs_dma_backing *b,
u64 offset, void *destination, u64 bytes)
{
struct nv_rs_dma_slot *s;
u64 end;
if (!ctx || (!destination && bytes) || check_add_overflow(offset, bytes, &end))
return -EINVAL;
mutex_lock(&ctx->lock); s = lookup(ctx, b);
if (!s || end > s->bytes) { mutex_unlock(&ctx->lock); return -EINVAL; }
if (bytes) {
/* Coherent DMA allocations are already mapped non-cacheable/coherent on
* non-coherent ARM64; only memory barriers are valid (never dma_sync_single_*). */
dma_rmb();
memcpy(destination, (char *)s->cpu + offset, (size_t)bytes);
}
mutex_unlock(&ctx->lock); return 0;
}
int nv_rs_dma_write(struct nv_rs_dma_context *ctx, const struct nv_rs_dma_backing *b,
u64 offset, const void *source, u64 bytes)
{
struct nv_rs_dma_slot *s;
u64 end;
if (!ctx || (!source && bytes) || check_add_overflow(offset, bytes, &end))
return -EINVAL;
mutex_lock(&ctx->lock); s = lookup(ctx, b);
if (!s || end > s->bytes) { mutex_unlock(&ctx->lock); return -EINVAL; }
if (bytes) {
memcpy((char *)s->cpu + offset, source, (size_t)bytes);
dma_wmb();
}
mutex_unlock(&ctx->lock); return 0;
}
/* No implicit DMA ordering here: the owner must order valid/payload/GET using
* sync_cpu/sync_device at the protocol boundaries, not before header reads. */
static int shared_word(struct nv_rs_dma_context *ctx, const struct nv_rs_dma_backing *b,
const struct nv_rs_dma_mapping *mapping, u64 offset,
u32 *value, bool write)
{
struct nv_rs_dma_slot *s;
u64 end;
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; }
s = lookup(ctx, b);
if (!s || !s->mapped || !mapping_matches(s, mapping) || end > s->bytes) {
mutex_unlock(&ctx->lock); return -EINVAL;
}
if (write) WRITE_ONCE(*(u32 *)((char *)s->cpu + offset), *value);
else *value = READ_ONCE(*(u32 *)((char *)s->cpu + offset));
mutex_unlock(&ctx->lock); return 0;
}
int nv_rs_dma_read_word(struct nv_rs_dma_context *ctx, const struct nv_rs_dma_backing *b,
const struct nv_rs_dma_mapping *m, u64 offset, u32 *value)
{ return shared_word(ctx, b, m, offset, value, false); }
int nv_rs_dma_write_word(struct nv_rs_dma_context *ctx, const struct nv_rs_dma_backing *b,
const struct nv_rs_dma_mapping *m, u64 offset, u32 value)
{ return shared_word(ctx, b, m, offset, &value, true); }
int nv_rs_dma_flush_sysmem(struct nv_rs_dma_context *ctx, const struct nv_rs_dma_backing *b)
{
struct nv_rs_dma_slot *s;
if (!ctx) return -EINVAL;
mutex_lock(&ctx->lock); s = lookup(ctx, b);
if (!s || !s->mapped) { mutex_unlock(&ctx->lock); return -EINVAL; }
/* Exact580 SYSTEM_MEMORY kbusFlushSingle path: full CPU fence. Coherent DMA
* allocations are already non-cacheable/coherent on ARM64. */
mb();
mutex_unlock(&ctx->lock); return 0;
}