Files
nvdriver/kernel-open/nvidia/thread_state_adapter.c

59 lines
1.7 KiB
C

/* SPDX-License-Identifier: MIT */
/* OS allocation, IRQ-saving lock and task references only. Registry policy is Rust. */
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/sched/task.h>
#include "thread_state_adapter.h"
void *nv_rs_thread_storage_alloc(size_t bytes)
{
if (!bytes || !nv_rs_thread_process_context())
return NULL;
return kzalloc(bytes, GFP_KERNEL);
}
void nv_rs_thread_storage_free(void *allocation) { kfree(allocation); }
void *nv_rs_thread_lock_create(void)
{
spinlock_t *lock;
if (!nv_rs_thread_process_context())
return NULL;
lock = kmalloc(sizeof(*lock), GFP_KERNEL);
if (lock)
spin_lock_init(lock);
return lock;
}
void nv_rs_thread_lock_destroy(void *lock) { kfree(lock); }
unsigned long nv_rs_thread_lock(void *lock)
{
unsigned long flags;
spin_lock_irqsave((spinlock_t *)lock, flags);
return flags;
}
void nv_rs_thread_unlock(void *lock, unsigned long flags)
{
spin_unlock_irqrestore((spinlock_t *)lock, flags);
}
void *nv_rs_thread_task_get(u32 *flags)
{
if (!flags || in_interrupt() || irqs_disabled() || in_atomic())
return NULL;
*flags = ((current->flags & PF_KTHREAD) ? 1 : 0) |
((current->flags & PF_EXITING) ? 2 : 0);
get_task_struct(current);
return current;
}
void nv_rs_thread_task_put(void *task)
{
if (task)
put_task_struct((struct task_struct *)task);
}
bool nv_rs_thread_task_current(void *task)
{
/* Identity-only: registry lookup holds an IRQ-saving spinlock. */
return task == current;
}
bool nv_rs_thread_process_context(void)
{
return !in_interrupt() && !irqs_disabled() && !in_atomic();
}