mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
- Add root LICENSE (MIT License, Copyright (c) 2026 QuixiAI / Eric Hartford).
- Set license = "MIT" in workspace Cargo.toml and member crates (zero non-MIT dependencies).
- Update all kernel-open/ and tests/vm/ SPDX headers to MIT and EXPORT_SYMBOL_GPL -> EXPORT_SYMBOL.
- Use MODULE_LICENSE("Dual MIT/GPL") in Linux .ko modules so Linux modpost permits linking against the kernel's own EXPORT_SYMBOL_GPL symbols while keeping the repository source 100% MIT.
Closes #12.
79 lines
3.4 KiB
C
79 lines
3.4 KiB
C
// SPDX-License-Identifier: MIT
|
|
/* Linux allocation/ordered deferred execution only. RM thread/GPU lock policy
|
|
* and event eligibility/list/callback semantics belong to the Rust owner. */
|
|
#include <linux/slab.h>
|
|
#include <linux/workqueue.h>
|
|
#include <linux/spinlock.h>
|
|
#include <linux/interrupt.h>
|
|
#include <linux/atomic.h>
|
|
#include <linux/wait.h>
|
|
#include "event_work_adapter.h"
|
|
struct nv_rs_event_work {spinlock_t lock;bool stopping;atomic_t pending;void (*callback)(void *);void *payload;};
|
|
struct message {struct work_struct work;struct nv_rs_event_work *owner;};
|
|
static struct workqueue_struct *event_queue;
|
|
static atomic_t controls=ATOMIC_INIT(0);
|
|
static DECLARE_WAIT_QUEUE_HEAD(control_wait);
|
|
static bool process_context(void){return !in_interrupt()&&!irqs_disabled()&&!in_atomic();}
|
|
static void execute(struct work_struct *work)
|
|
{
|
|
struct message *message=container_of(work,struct message,work);
|
|
struct nv_rs_event_work *owner=message->owner;
|
|
owner->callback(owner->payload);
|
|
atomic_dec(&owner->pending);
|
|
kfree(message);
|
|
}
|
|
int nv_rs_event_workqueue_init(void)
|
|
{
|
|
if(event_queue)return -EALREADY;
|
|
event_queue=alloc_ordered_workqueue("nvidia_rs_events",WQ_MEM_RECLAIM);
|
|
return event_queue?0:-ENOMEM;
|
|
}
|
|
void nv_rs_event_workqueue_destroy(void)
|
|
{
|
|
if(!event_queue)return;
|
|
/* A live Rust event holds its GPU/module pin, so normal module teardown has
|
|
* no controls. Never release code/queue underneath a retained failed owner. */
|
|
wait_event(control_wait,atomic_read(&controls)==0);
|
|
destroy_workqueue(event_queue);event_queue=NULL;
|
|
}
|
|
int nv_rs_event_work_create(void (*callback)(void *),void *payload,struct nv_rs_event_work **out)
|
|
{
|
|
struct nv_rs_event_work *owner;
|
|
if(!out)return -EINVAL;
|
|
*out=NULL;
|
|
if(!process_context())return -EWOULDBLOCK;
|
|
if(!event_queue||!callback||!payload)return -EINVAL;
|
|
owner=kzalloc(sizeof(*owner),GFP_KERNEL);if(!owner)return -ENOMEM;
|
|
spin_lock_init(&owner->lock);atomic_set(&owner->pending,0);
|
|
owner->callback=callback;owner->payload=payload;atomic_inc(&controls);*out=owner;return 0;
|
|
}
|
|
int nv_rs_event_work_enqueue(struct nv_rs_event_work *owner)
|
|
{
|
|
struct message *message;unsigned long flags;
|
|
if(!owner||!event_queue)return -EINVAL;
|
|
message=kmalloc(sizeof(*message),GFP_ATOMIC);if(!message)return -ENOMEM;
|
|
INIT_WORK(&message->work,execute);message->owner=owner;
|
|
spin_lock_irqsave(&owner->lock,flags);
|
|
if(owner->stopping){spin_unlock_irqrestore(&owner->lock,flags);kfree(message);return -EBUSY;}
|
|
atomic_inc(&owner->pending);
|
|
/* Fresh work has never been queued. Keep the acceptance decision and enqueue
|
|
* under the same lock used by close; no coalescing of distinct timer fires. */
|
|
if(!queue_work(event_queue,&message->work)){
|
|
atomic_dec(&owner->pending);spin_unlock_irqrestore(&owner->lock,flags);kfree(message);return -EIO;
|
|
}
|
|
spin_unlock_irqrestore(&owner->lock,flags);return 0;
|
|
}
|
|
int nv_rs_event_work_close(struct nv_rs_event_work *owner)
|
|
{
|
|
unsigned long flags;
|
|
if(!owner)return -EINVAL;
|
|
if(!process_context())return -EWOULDBLOCK;
|
|
/* Joining this ordered domain from one of its own callbacks deadlocks, even
|
|
* when closing a different event whose message is queued behind that callback. */
|
|
if(current_work()&¤t_work()->func==execute)return -EDEADLK;
|
|
spin_lock_irqsave(&owner->lock,flags);owner->stopping=true;spin_unlock_irqrestore(&owner->lock,flags);
|
|
flush_workqueue(event_queue);
|
|
if(WARN_ON(atomic_read(&owner->pending)!=0))return -EBUSY;
|
|
kfree(owner);if(atomic_dec_and_test(&controls))wake_up_all(&control_wait);return 0;
|
|
}
|