Files
nvdriver/tests/file-cleanup/fixture.c
nvidia-driver-rs 4526e6a816 Baseline: validated 30/30 tree with passing Level 7 hardware regression
- Updated tools/post_reboot.sh to check kernel oops taint bit and stuck driver tasks instead of <86400s uptime
- Level 7 hardware regression passed on 0000:b3:00.0 (module 37e6fa430835eee2b426019db3ad70eeb7a9e9070c09c84bd9b973cf56cbe529, 107 inventory words, 0 static mismatches, CUDA baseline verified before and after)
- Updated .gitignore to exclude build archives (*.a, *.rlib), __pycache__, and raw trace dumps
2026-11-23 23:48:38 +00:00

100 lines
6.5 KiB
C

#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdatomic.h>
struct work_struct {void (*run)(struct work_struct *);bool pending;};
struct delayed_work {struct work_struct work;};
struct queued {struct work_struct *work;struct queued *next;};
struct workqueue_struct {pthread_mutex_t mutex;pthread_cond_t condition;pthread_t worker;struct queued *head,*tail;bool stopping;unsigned permits;};
static pthread_mutex_t trace=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t changed=PTHREAD_COND_INITIALIZER;
static unsigned pins,attempts,destroyed;
static bool pause_put,release_put,destroy_entered,destroy_finished,allocation_failure;
#define THIS_MODULE ((void *)1)
#define WQ_MEM_RECLAIM 1
#define container_of(pointer,type,member) ((type *)((char *)(pointer)-offsetof(type,member)))
#define to_delayed_work(work) container_of(work,struct delayed_work,work)
#define INIT_DELAYED_WORK(item,callback) do{(item)->work.run=(callback);(item)->work.pending=false;}while(0)
#define WARN_ON_ONCE(value) do{assert(!(value));}while(0)
#define pr_warn_ratelimited(...) do{}while(0)
#define msecs_to_jiffies(value) (value)
static void __module_get(void *module){assert(module==THIS_MODULE);pthread_mutex_lock(&trace);pins++;pthread_cond_broadcast(&changed);pthread_mutex_unlock(&trace);}
static void module_put(void *module){assert(module==THIS_MODULE);pthread_mutex_lock(&trace);assert(pins);pins--;pthread_cond_broadcast(&changed);while(pause_put&&!release_put)pthread_cond_wait(&changed,&trace);pthread_mutex_unlock(&trace);}
static void *worker(void *opaque)
{
struct workqueue_struct *q=opaque;
for(;;){struct queued *item;struct work_struct *work;void (*run)(struct work_struct *);
pthread_mutex_lock(&q->mutex);
while(!(q->head&&q->permits)&&!(q->stopping&&!q->head))pthread_cond_wait(&q->condition,&q->mutex);
if(q->stopping&&!q->head){pthread_mutex_unlock(&q->mutex);return NULL;}
item=q->head;q->head=item->next;if(!q->head)q->tail=NULL;q->permits--;
work=item->work;run=work->run;work->pending=false;pthread_mutex_unlock(&q->mutex);
free(item);run(work); /* callback may free work; do not touch it */
}
}
static struct workqueue_struct *alloc_ordered_workqueue(const char *name,int flags)
{
struct workqueue_struct *q;(void)name;assert(flags==WQ_MEM_RECLAIM);
if(allocation_failure)return NULL;
q=calloc(1,sizeof(*q));assert(q);assert(!pthread_mutex_init(&q->mutex,NULL));assert(!pthread_cond_init(&q->condition,NULL));assert(!pthread_create(&q->worker,NULL,worker,q));return q;
}
static bool queue_delayed_work(struct workqueue_struct *q,struct delayed_work *delayed,unsigned delay)
{
struct queued *item;assert(delay==100);pthread_mutex_lock(&q->mutex);assert(!q->stopping);
if(delayed->work.pending){pthread_mutex_unlock(&q->mutex);return false;}
item=calloc(1,sizeof(*item));assert(item);item->work=&delayed->work;
delayed->work.pending=true;if(q->tail)q->tail->next=item;else q->head=item;q->tail=item;
pthread_cond_broadcast(&q->condition);pthread_mutex_unlock(&q->mutex);return true;
}
static void destroy_workqueue(struct workqueue_struct *q)
{
pthread_mutex_lock(&q->mutex);q->stopping=true;pthread_cond_broadcast(&q->condition);pthread_mutex_unlock(&q->mutex);
assert(!pthread_join(q->worker,NULL));assert(!q->head);pthread_cond_destroy(&q->condition);pthread_mutex_destroy(&q->mutex);free(q);
}
#define NV_RS_FILE_CLEANUP_HOST_FIXTURE
#include "../../kernel-open/nvidia/file_cleanup.h"
struct context {struct nv_rs_file_cleanup cleanup;unsigned failures;void *rust_state;};
static int close_context(void *opaque)
{
struct context *c=opaque;int result;
assert(c->rust_state);pthread_mutex_lock(&trace);attempts++;result=c->failures?-EBUSY:0;if(c->failures)c->failures--;pthread_cond_broadcast(&changed);pthread_mutex_unlock(&trace);return result;
}
static void destroy_context(void *opaque)
{
struct context *c=opaque;assert(c->rust_state);free(c->rust_state);free(c);
pthread_mutex_lock(&trace);destroyed++;pthread_cond_broadcast(&changed);pthread_mutex_unlock(&trace);
}
static struct context *create(struct nv_rs_file_cleanup_queue *queue,unsigned failures)
{
struct context *c=calloc(1,sizeof(*c));assert(c);c->failures=failures;c->rust_state=calloc(1,64);assert(c->rust_state);nv_rs_file_cleanup_init(&c->cleanup,queue,c,close_context,destroy_context);return c;
}
static void permit(struct nv_rs_file_cleanup_queue *queue)
{
pthread_mutex_lock(&queue->work->mutex);queue->work->permits++;pthread_cond_broadcast(&queue->work->condition);pthread_mutex_unlock(&queue->work->mutex);
}
static void *destroy_queue(void *opaque)
{
pthread_mutex_lock(&trace);destroy_entered=true;pthread_cond_broadcast(&changed);pthread_mutex_unlock(&trace);
nv_rs_file_cleanup_queue_destroy(opaque);
pthread_mutex_lock(&trace);destroy_finished=true;pthread_cond_broadcast(&changed);pthread_mutex_unlock(&trace);return NULL;
}
int main(void)
{
struct nv_rs_file_cleanup_queue queue={0};struct context *c;void *private_data;pthread_t exit_thread;
allocation_failure=true;assert(nv_rs_file_cleanup_queue_init(&queue)==-ENOMEM&&!queue.work);allocation_failure=false;
assert(!nv_rs_file_cleanup_queue_init(&queue));c=create(&queue,0);private_data=c;private_data=NULL;
assert(!nv_rs_file_cleanup_retire(&c->cleanup));assert(!private_data&&attempts==1&&destroyed==1&&pins==0);nv_rs_file_cleanup_queue_destroy(&queue);assert(!queue.work);
attempts=destroyed=0;assert(!nv_rs_file_cleanup_queue_init(&queue));c=create(&queue,2);private_data=c;private_data=NULL;
assert(nv_rs_file_cleanup_retire(&c->cleanup)==-EBUSY);assert(!private_data&&attempts==1&&destroyed==0&&pins==1);
permit(&queue);pthread_mutex_lock(&trace);while(attempts<2)pthread_cond_wait(&changed,&trace);assert(pins==1&&!destroyed);pause_put=true;pthread_mutex_unlock(&trace);
permit(&queue);pthread_mutex_lock(&trace);while(pins)pthread_cond_wait(&changed,&trace);assert(attempts==3&&destroyed==1);pthread_mutex_unlock(&trace);
assert(!pthread_create(&exit_thread,NULL,destroy_queue,&queue));pthread_mutex_lock(&trace);while(!destroy_entered)pthread_cond_wait(&changed,&trace);assert(!destroy_finished);release_put=true;pthread_cond_broadcast(&changed);pthread_mutex_unlock(&trace);
assert(!pthread_join(exit_thread,NULL));assert(destroy_finished&&!queue.work&&pins==0&&destroyed==1);
puts("file_cleanup: synchronous close, two retained failures, single pin, embedded work destruction and exit join passed");return 0;
}