Skip to content

Commit

Permalink
arch/x86: Free handles on process exit
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwinci committed Oct 19, 2023
1 parent b84d138 commit ee63ced
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
26 changes: 25 additions & 1 deletion src/arch/x86/sched/x86_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "sched/sched_internals.h"
#include "string.h"
#include "sched/process.h"
#include "sys/dev.h"
#include "fs/vfs.h"

typedef struct {
u64 r15, r14, r13, r12, rbp, rbx;
Expand Down Expand Up @@ -169,8 +171,30 @@ void arch_destroy_task(Task* task) {
mapping = next;
}

// todo release handles
for (size_t i = 0; i < task->process->handle_table.cap; ++i) {
HandleEntry* entry = &task->process->handle_table.table[i];
if (entry->handle & FREED_HANDLE) {
continue;
}
HandleType type = entry->type;
void* data = entry->data;

switch (type) {
case HANDLE_TYPE_THREAD:
kfree(data, sizeof(ThreadHandle));
break;
case HANDLE_TYPE_DEVICE:
((GenericDevice*) data)->refcount -= 1;
break;
case HANDLE_TYPE_VNODE:
((VNode*) data)->release((VNode*) data);
break;
case HANDLE_TYPE_KERNEL_GENERIC:
continue;
}
}

handle_tab_destroy(&task->process->handle_table);
vm_user_free(task->process);
kfree(process, sizeof(Process));
}
Expand Down
7 changes: 5 additions & 2 deletions src/utils/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#include "mem/allocator.h"
#include "string.h"

#define FREED_HANDLE (1ULL << (sizeof(usize) * 8 - 1))

Handle handle_tab_insert(HandleTable* self, void* data, HandleType type) {
mutex_lock(&self->lock);

Expand Down Expand Up @@ -42,6 +40,11 @@ Handle handle_tab_insert(HandleTable* self, void* data, HandleType type) {
}
}

void handle_tab_destroy(HandleTable* self) {
kfree(self->table, self->cap * sizeof(HandleEntry));
*self = (HandleTable) {};
}

HandleEntry* handle_tab_get(HandleTable* self, Handle handle) {
mutex_lock(&self->lock);
if (handle >= self->size) {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ typedef struct {
Mutex lock;
} HandleTable;

#define FREED_HANDLE (1ULL << (sizeof(usize) * 8 - 1))

Handle handle_tab_insert(HandleTable* self, void* data, HandleType type);
HandleEntry* handle_tab_get(HandleTable* self, Handle handle);
bool handle_tab_close(HandleTable* self, Handle handle);
void* handle_tab_open(HandleTable* self, Handle handle);
void handle_tab_destroy(HandleTable* self);

0 comments on commit ee63ced

Please sign in to comment.