Manage int IDs via id_alloc
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m31s

This commit is contained in:
2026-02-22 20:40:12 +01:00
parent 8fc5418915
commit 084809ac99
13 changed files with 203 additions and 70 deletions

View File

@@ -64,6 +64,9 @@ void bootmain (void) {
spin (); spin ();
} }
proc_pid_alloc_init ();
procgroup_pgid_alloc_init ();
smp_init (); smp_init ();
proc_init (); proc_init ();

View File

@@ -17,8 +17,6 @@
#include <sys/debug.h> #include <sys/debug.h>
#include <sys/proc.h> #include <sys/proc.h>
static atomic_int pids = 0;
struct proc* proc_from_elf (uint8_t* elf_contents) { struct proc* proc_from_elf (uint8_t* elf_contents) {
struct limine_hhdm_response* hhdm = limine_hhdm_request.response; struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
@@ -30,7 +28,12 @@ struct proc* proc_from_elf (uint8_t* elf_contents) {
proc->lock = SPIN_LOCK_INIT; proc->lock = SPIN_LOCK_INIT;
proc->state = PROC_READY; proc->state = PROC_READY;
proc->pid = atomic_fetch_add (&pids, 1); proc->pid = proc_alloc_pid ();
if (proc->pid < 0) {
free (proc);
return NULL;
}
proc->procgroup = procgroup_create (); proc->procgroup = procgroup_create ();
if (proc->procgroup == NULL) { if (proc->procgroup == NULL) {
@@ -72,7 +75,12 @@ struct proc* proc_clone (struct proc* proto, uintptr_t vstack_top, uintptr_t ent
proc->lock = SPIN_LOCK_INIT; proc->lock = SPIN_LOCK_INIT;
proc->state = PROC_READY; proc->state = PROC_READY;
proc->pid = atomic_fetch_add (&pids, 1); proc->pid = proc_alloc_pid ();
if (proc->pid < 0) {
free (proc);
return NULL;
}
spin_lock (&proto->lock); spin_lock (&proto->lock);
@@ -108,6 +116,7 @@ void proc_cleanup (struct proc* proc, struct reschedule_ctx* rctx) {
procgroup_detach (proc->procgroup, proc, rctx); procgroup_detach (proc->procgroup, proc, rctx);
proc_free_pid (proc->pid);
/* clean the process */ /* clean the process */
free (proc); free (proc);
} }

View File

@@ -1,6 +1,7 @@
#include <device/device.h> #include <device/device.h>
#include <fs/tarfs.h> #include <fs/tarfs.h>
#include <fs/vfs.h> #include <fs/vfs.h>
#include <id/id_alloc.h>
#include <libk/fieldsizeof.h> #include <libk/fieldsizeof.h>
#include <libk/hash.h> #include <libk/hash.h>
#include <libk/lengthof.h> #include <libk/lengthof.h>
@@ -97,7 +98,7 @@ int vfs_open (struct procgroup* procgroup, const char* mountpoint, const char* p
spin_lock (&procgroup->lock); spin_lock (&procgroup->lock);
int id = handle->id = procgroup->sys_vfs_handles++; int id = handle->id = id_alloc (&procgroup->vfs_handle_id_alloc);
rbtree_insert (struct vfs_handle, &procgroup->vfs_handle_tree, &handle->handle_tree_link, rbtree_insert (struct vfs_handle, &procgroup->vfs_handle_tree, &handle->handle_tree_link,
handle_tree_link, id); handle_tree_link, id);
@@ -119,8 +120,13 @@ int vfs_close (struct procgroup* procgroup, int id) {
return -ST_NOT_FOUND; return -ST_NOT_FOUND;
} }
spin_lock (&handle->lock);
rbtree_delete (&procgroup->vfs_handle_tree, &handle->handle_tree_link); rbtree_delete (&procgroup->vfs_handle_tree, &handle->handle_tree_link);
id_free (&procgroup->vfs_handle_id_alloc, handle->id);
spin_unlock (&handle->lock);
spin_unlock (&procgroup->lock); spin_unlock (&procgroup->lock);
free (handle); free (handle);
@@ -191,7 +197,6 @@ int vfs_kernel_describe (const char* mountpoint, const char* path, struct desc*
} }
void vfs_procgroup_cleanup (struct procgroup* procgroup) { void vfs_procgroup_cleanup (struct procgroup* procgroup) {
struct list_node_link* handle_cleanup_list = NULL;
struct vfs_handle* handle; struct vfs_handle* handle;
struct rb_node_link* node; struct rb_node_link* node;
@@ -202,14 +207,8 @@ void vfs_procgroup_cleanup (struct procgroup* procgroup) {
rbtree_next (node, next); rbtree_next (node, next);
handle = rbtree_entry (node, struct vfs_handle, handle_tree_link); handle = rbtree_entry (node, struct vfs_handle, handle_tree_link);
node = next; node = next;
list_append (handle_cleanup_list, &handle->handle_cleanup_link);
}
struct list_node_link *cleanup_link, *cleanup_tmp_link; vfs_close (procgroup, handle->id);
list_foreach (handle_cleanup_list, cleanup_link, cleanup_tmp_link) {
handle = list_entry (cleanup_link, struct vfs_handle, handle_cleanup_link);
list_remove (handle_cleanup_list, &handle->handle_cleanup_link);
free (handle);
} }
} }

View File

@@ -23,7 +23,6 @@ struct vfs_handle {
char path[MAX_PATH]; char path[MAX_PATH];
struct procgroup* ownerpg; struct procgroup* ownerpg;
struct rb_node_link handle_tree_link; struct rb_node_link handle_tree_link;
struct list_node_link handle_cleanup_link;
spin_lock_t lock; spin_lock_t lock;
}; };

1
kernel/id/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.o

52
kernel/id/id_alloc.c Normal file
View File

@@ -0,0 +1,52 @@
#include <id/id_alloc.h>
#include <libk/bm.h>
#include <libk/std.h>
#include <mm/liballoc.h>
#include <status.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
bool id_alloc_init (struct id_alloc* ida, size_t nbits) {
size_t buffer_size = (nbits + 7) / 8;
uint8_t* buffer = malloc (buffer_size);
if (buffer == NULL)
return false;
bm_init (&ida->bm, buffer, nbits);
ida->lock = SPIN_LOCK_INIT;
return true;
}
void id_alloc_fini (struct id_alloc* ida) {
free (ida->bm.base);
ida->bm.base = NULL;
ida->bm.nbits = 0;
}
int id_alloc (struct id_alloc* ida) {
spin_lock (&ida->lock);
for (size_t bit = 0; bit < ida->bm.nbits; bit++) {
if (!bm_test (&ida->bm, bit)) {
bm_set (&ida->bm, bit);
spin_unlock (&ida->lock);
return (int)bit;
}
}
spin_unlock (&ida->lock);
return -ST_OOM_ERROR;
}
void id_free (struct id_alloc* ida, int id) {
if (id < 0)
return;
spin_lock (&ida->lock);
bm_clear (&ida->bm, (size_t)id);
spin_unlock (&ida->lock);
}

21
kernel/id/id_alloc.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef _KERNEL_ID_ID_ALLOC_H
#define _KERNEL_ID_ID_ALLOC_H
#include <libk/bm.h>
#include <libk/std.h>
#include <sync/spin_lock.h>
struct id_alloc {
struct bm bm;
spin_lock_t lock;
};
int id_alloc (struct id_alloc* ida);
void id_free (struct id_alloc* ida, int id);
bool id_alloc_init (struct id_alloc* ida, size_t nbits);
void id_alloc_fini (struct id_alloc* ida);
#endif // _KERNEL_ID_ID_ALLOC_H

3
kernel/id/src.mk Normal file
View File

@@ -0,0 +1,3 @@
c += id/id_alloc.c
o += id/id_alloc.o

View File

@@ -9,6 +9,7 @@ include syscall/src.mk
include fs/src.mk include fs/src.mk
include device/src.mk include device/src.mk
include Flanterm/src.mk include Flanterm/src.mk
include id/src.mk
ifeq ($(PLATFORM_ACPI),1) ifeq ($(PLATFORM_ACPI),1)
include uACPI/src.mk include uACPI/src.mk

View File

@@ -2,6 +2,7 @@
#include <aux/elf.h> #include <aux/elf.h>
#include <desc.h> #include <desc.h>
#include <fs/vfs.h> #include <fs/vfs.h>
#include <id/id_alloc.h>
#include <irq/irq.h> #include <irq/irq.h>
#include <libk/align.h> #include <libk/align.h>
#include <libk/list.h> #include <libk/list.h>
@@ -29,6 +30,8 @@
#include <amd64/intr_defs.h> #include <amd64/intr_defs.h>
#endif #endif
#define PIDS_MAX 1024
#define SCHED_REAP_FREQ 10 #define SCHED_REAP_FREQ 10
static struct rb_node_link* proc_tree = NULL; static struct rb_node_link* proc_tree = NULL;
@@ -36,6 +39,14 @@ static spin_lock_t proc_tree_lock = SPIN_LOCK_INIT;
static atomic_int sched_cycles = 0; static atomic_int sched_cycles = 0;
static struct id_alloc pid_alloc;
int proc_alloc_pid (void) { return id_alloc (&pid_alloc); }
void proc_free_pid (int pid) { id_free (&pid_alloc, pid); }
void proc_pid_alloc_init (void) { id_alloc_init (&pid_alloc, PIDS_MAX); }
static bool proc_check_elf (uint8_t* elf) { static bool proc_check_elf (uint8_t* elf) {
if (!((elf[0] == 0x7F) && (elf[1] == 'E') && (elf[2] == 'L') && (elf[3] == 'F'))) if (!((elf[0] == 0x7F) && (elf[1] == 'E') && (elf[2] == 'L') && (elf[3] == 'F')))
return false; return false;

View File

@@ -59,6 +59,12 @@ struct proc* proc_find_pid (int pid);
struct proc* proc_from_file (struct procgroup* procgroup, const char* mountpoint, const char* path); struct proc* proc_from_file (struct procgroup* procgroup, const char* mountpoint, const char* path);
void proc_free_pid (int pid);
int proc_alloc_pid (void);
void proc_pid_alloc_init (void);
void proc_init (void); void proc_init (void);
#endif // _KERNEL_PROC_PROC_H #endif // _KERNEL_PROC_PROC_H

View File

@@ -1,4 +1,5 @@
#include <fs/vfs.h> #include <fs/vfs.h>
#include <id/id_alloc.h>
#include <libk/rbtree.h> #include <libk/rbtree.h>
#include <libk/std.h> #include <libk/std.h>
#include <libk/string.h> #include <libk/string.h>
@@ -11,9 +12,14 @@
#include <sys/debug.h> #include <sys/debug.h>
#include <sys/mm.h> #include <sys/mm.h>
#define PGIDS_MAX 1024
static struct rb_node_link* procgroup_tree = NULL; static struct rb_node_link* procgroup_tree = NULL;
static spin_lock_t procgroup_tree_lock = SPIN_LOCK_INIT; static spin_lock_t procgroup_tree_lock = SPIN_LOCK_INIT;
static atomic_int pgids = 0;
static struct id_alloc pgid_alloc;
void procgroup_pgid_alloc_init (void) { id_alloc_init (&pgid_alloc, PGIDS_MAX); }
struct procgroup* procgroup_find (int pgid) { struct procgroup* procgroup_find (int pgid) {
struct procgroup* procgroup = NULL; struct procgroup* procgroup = NULL;
@@ -145,9 +151,20 @@ struct procgroup* procgroup_create (void) {
memset (procgroup, 0, sizeof (*procgroup)); memset (procgroup, 0, sizeof (*procgroup));
if (!id_alloc_init (&procgroup->vfs_handle_id_alloc, PROCGROUP_VFS_HANDLES_MAX)) {
free (procgroup);
return NULL;
}
procgroup->pgid = id_alloc (&pgid_alloc);
if (procgroup->pgid < 0) {
free (procgroup);
return NULL;
}
procgroup->memb_proc_tree = NULL; procgroup->memb_proc_tree = NULL;
procgroup->lock = SPIN_LOCK_INIT; procgroup->lock = SPIN_LOCK_INIT;
procgroup->pgid = atomic_fetch_add (&pgids, 1);
procgroup->pd.cr3_paddr = mm_alloc_user_pd_phys (); procgroup->pd.cr3_paddr = mm_alloc_user_pd_phys ();
procgroup->map_base = PROC_MAP_BASE; procgroup->map_base = PROC_MAP_BASE;
@@ -175,18 +192,7 @@ void procgroup_attach (struct procgroup* procgroup, struct proc* proc) {
spin_unlock (&procgroup->lock); spin_unlock (&procgroup->lock);
} }
void procgroup_detach (struct procgroup* procgroup, struct proc* proc, static void procgroup_delete (struct procgroup* procgroup, struct reschedule_ctx* rctx) {
struct reschedule_ctx* rctx) {
spin_lock (&procgroup->lock);
spin_lock (&proc->lock);
rbtree_delete (&procgroup->memb_proc_tree, &proc->procgroup_memb_tree_link);
struct rb_node_link* memb_tree = procgroup->memb_proc_tree;
spin_unlock (&proc->lock);
spin_unlock (&procgroup->lock);
if (memb_tree == NULL) {
spin_lock (&procgroup_tree_lock); spin_lock (&procgroup_tree_lock);
spin_lock (&procgroup->lock); spin_lock (&procgroup->lock);
@@ -204,8 +210,7 @@ void procgroup_detach (struct procgroup* procgroup, struct proc* proc,
struct rb_node_link* next; struct rb_node_link* next;
rbtree_next (rnode, next); rbtree_next (rnode, next);
struct proc_resource* resource = struct proc_resource* resource = rbtree_entry (rnode, struct proc_resource, resource_tree_link);
rbtree_entry (rnode, struct proc_resource, resource_tree_link);
rnode = next; rnode = next;
@@ -239,6 +244,24 @@ void procgroup_detach (struct procgroup* procgroup, struct proc* proc,
free (procgroup->tls.tls_tmpl); free (procgroup->tls.tls_tmpl);
id_alloc_fini (&procgroup->vfs_handle_id_alloc);
id_free (&pgid_alloc, procgroup->pgid);
free (procgroup); free (procgroup);
}
void procgroup_detach (struct procgroup* procgroup, struct proc* proc,
struct reschedule_ctx* rctx) {
spin_lock (&procgroup->lock);
spin_lock (&proc->lock);
rbtree_delete (&procgroup->memb_proc_tree, &proc->procgroup_memb_tree_link);
struct rb_node_link* memb_tree = procgroup->memb_proc_tree;
spin_unlock (&proc->lock);
spin_unlock (&procgroup->lock);
if (memb_tree == NULL) {
procgroup_delete (procgroup, rctx);
} }
} }

View File

@@ -1,6 +1,7 @@
#ifndef _KERNEL_PROC_PROCGROUP_H #ifndef _KERNEL_PROC_PROCGROUP_H
#define _KERNEL_PROC_PROCGROUP_H #define _KERNEL_PROC_PROCGROUP_H
#include <id/id_alloc.h>
#include <libk/list.h> #include <libk/list.h>
#include <libk/rbtree.h> #include <libk/rbtree.h>
#include <libk/std.h> #include <libk/std.h>
@@ -9,6 +10,8 @@
#include <sys/mm.h> #include <sys/mm.h>
#include <sys/procgroup.h> #include <sys/procgroup.h>
#define PROCGROUP_VFS_HANDLES_MAX 256
struct proc; struct proc;
struct reschedule_ctx; struct reschedule_ctx;
@@ -32,7 +35,7 @@ struct procgroup {
struct procgroup_tls tls; struct procgroup_tls tls;
uint64_t capabilities; uint64_t capabilities;
struct rb_node_link* vfs_handle_tree; struct rb_node_link* vfs_handle_tree;
int sys_vfs_handles; struct id_alloc vfs_handle_id_alloc;
}; };
struct procgroup* procgroup_create (void); struct procgroup* procgroup_create (void);
@@ -48,4 +51,6 @@ bool procgroup_unmap (struct procgroup* procgroup, uintptr_t start_vaddr, size_t
struct procgroup* procgroup_find (int pgid); struct procgroup* procgroup_find (int pgid);
void procgroup_pgid_alloc_init (void);
#endif // _KERNEL_PROC_PROCGROUP_H #endif // _KERNEL_PROC_PROCGROUP_H