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

130 lines
5.0 KiB
C

/* SPDX-License-Identifier: MIT */
/* Linux primitives only. Callback data and GPU/module lifetime belong to Rust. */
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/version.h>
#include "nano_timer_adapter.h"
struct nv_rs_nano_timer {
struct hrtimer timer;
struct timer_list jiffy_timer;
bool jiffy;
struct mutex stop_lock;
spinlock_t state_lock;
nv_rs_nano_callback callback;
void *payload;
bool stopping,closed;
};
static inline int nv_rs_timer_delete_sync(struct timer_list *t)
{
#if defined(NV_RS_HAVE_TIMER_DELETE_SYNC) || LINUX_VERSION_CODE >= KERNEL_VERSION(6, 2, 0)
return timer_delete_sync(t);
#else
return del_timer_sync(t);
#endif
}
static bool process_context(void)
{ return !in_interrupt() && !irqs_disabled() && !in_atomic(); }
void *nv_rs_nano_payload_alloc(size_t bytes)
{
if (!process_context() || !bytes) return NULL;
return kzalloc(bytes,GFP_KERNEL);
}
void nv_rs_nano_payload_free(void *payload) { kfree(payload); }
static enum hrtimer_restart fire(struct hrtimer *timer)
{
struct nv_rs_nano_timer *owner=container_of(timer,struct nv_rs_nano_timer,timer);
/* No state lock is held through client code. cancel/destroy joins this
* callback before payload ownership can be returned to Rust. */
owner->callback(owner->payload);
return HRTIMER_NORESTART;
}
static void fire_jiffy(struct timer_list *timer)
{
/* Match stock 580.178.04 common/inc/nv-timer.h: container_of works on all kernel versions. */
struct nv_rs_nano_timer *owner=container_of(timer,struct nv_rs_nano_timer,jiffy_timer);
owner->callback(owner->payload);
}
static int create(nv_rs_nano_callback callback,void *payload,
struct nv_rs_nano_timer **out,bool jiffy)
{
struct nv_rs_nano_timer *owner;
if (!out) return -EINVAL;
*out=NULL;
if (!callback || !payload) return -EINVAL;
if (!process_context()) return -EWOULDBLOCK;
owner=kzalloc(sizeof(*owner),GFP_KERNEL);
if (!owner) return -ENOMEM;
mutex_init(&owner->stop_lock);
spin_lock_init(&owner->state_lock);
owner->callback=callback;owner->payload=payload;
owner->jiffy=jiffy;
if(jiffy)timer_setup(&owner->jiffy_timer,fire_jiffy,0);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 15, 0)
else hrtimer_setup(&owner->timer,fire,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
#else
else {hrtimer_init(&owner->timer,CLOCK_MONOTONIC,HRTIMER_MODE_REL);owner->timer.function=fire;}
#endif
*out=owner;return 0;
}
int nv_rs_nano_create(nv_rs_nano_callback callback,void *payload,struct nv_rs_nano_timer **out)
{return create(callback,payload,out,false);}
int nv_rs_nano_create_jiffy(nv_rs_nano_callback callback,void *payload,struct nv_rs_nano_timer **out)
{return create(callback,payload,out,true);}
int nv_rs_nano_start(struct nv_rs_nano_timer *owner,u64 ns)
{
unsigned long flags;
if (!owner) return -EINVAL;
spin_lock_irqsave(&owner->state_lock,flags);
if (owner->closed || owner->stopping) {
spin_unlock_irqrestore(&owner->state_lock,flags);return -EBUSY;
}
/* Closed580 selects jiffies: truncate quotient to u32 BEFORE converting.
* The separate hrtimer primitive retains its own original ktime semantics. */
if(owner->jiffy)mod_timer(&owner->jiffy_timer,jiffies+usecs_to_jiffies((u32)(ns/1000)));
else hrtimer_start(&owner->timer,ktime_set(0,ns),HRTIMER_MODE_REL);
spin_unlock_irqrestore(&owner->state_lock,flags);return 0;
}
int nv_rs_nano_cancel(struct nv_rs_nano_timer *owner,u32 *was_active)
{
unsigned long flags;
int active;
if (!owner || !was_active) return -EINVAL;
if (!process_context()) return -EWOULDBLOCK;
mutex_lock(&owner->stop_lock);
spin_lock_irqsave(&owner->state_lock,flags);
if (owner->closed) {
spin_unlock_irqrestore(&owner->state_lock,flags);
mutex_unlock(&owner->stop_lock);return -EINVAL;
}
owner->stopping=true;
spin_unlock_irqrestore(&owner->state_lock,flags);
active=owner->jiffy?nv_rs_timer_delete_sync(&owner->jiffy_timer):hrtimer_cancel(&owner->timer);
spin_lock_irqsave(&owner->state_lock,flags);
owner->stopping=false;
spin_unlock_irqrestore(&owner->state_lock,flags);
mutex_unlock(&owner->stop_lock);
*was_active=!!active;return 0;
}
int nv_rs_nano_destroy(struct nv_rs_nano_timer *owner)
{
unsigned long flags;
if (!owner) return -EINVAL;
if (!process_context()) return -EWOULDBLOCK;
/* Owning Rust requires exclusive access and prevents further API calls
* after this point. Serialized cancel/destroy must not run from fire(). */
mutex_lock(&owner->stop_lock);
spin_lock_irqsave(&owner->state_lock,flags);
owner->closed=true;owner->stopping=true;
spin_unlock_irqrestore(&owner->state_lock,flags);
if(owner->jiffy)nv_rs_timer_delete_sync(&owner->jiffy_timer);else hrtimer_cancel(&owner->timer);
mutex_unlock(&owner->stop_lock);
kfree(owner);return 0;
}