From 2e6affc24baca5540d6c7f6406519663c15ae985 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 13 Dec 2023 14:36:09 -0500 Subject: kernel: vm: Ensure calls are serialized Signed-off-by: Ian Moffett --- sys/include/vm/vm.h | 2 ++ sys/vm/vm_dynalloc.c | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/sys/include/vm/vm.h b/sys/include/vm/vm.h index 59cf3bc..2b4ccad 100644 --- a/sys/include/vm/vm.h +++ b/sys/include/vm/vm.h @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -53,6 +54,7 @@ struct vm_ctx { uintptr_t dynalloc_pool_phys; size_t dynalloc_pool_sz; /* In bytes */ tlsf_t tlsf_ctx; + struct spinlock dynalloc_lock; }; /* diff --git a/sys/vm/vm_dynalloc.c b/sys/vm/vm_dynalloc.c index e7acd19..a656a00 100644 --- a/sys/vm/vm_dynalloc.c +++ b/sys/vm/vm_dynalloc.c @@ -40,8 +40,12 @@ dynalloc(size_t sz) { /* TODO: Per CPU */ struct vm_ctx vm_ctx = vm_get_bsp_ctx(); + void *tmp; - return tlsf_malloc(vm_ctx.tlsf_ctx, sz); + spinlock_acquire(&vm_ctx.dynalloc_lock); + tmp = tlsf_malloc(vm_ctx.tlsf_ctx, sz); + spinlock_release(&vm_ctx.dynalloc_lock); + return tmp; } /* @@ -55,8 +59,12 @@ dynrealloc(void *old_ptr, size_t newsize) { /* TODO: Per CPU */ struct vm_ctx vm_ctx = vm_get_bsp_ctx(); + void *tmp; - return tlsf_realloc(vm_ctx.tlsf_ctx, old_ptr, newsize); + spinlock_acquire(&vm_ctx.dynalloc_lock); + tmp = tlsf_realloc(vm_ctx.tlsf_ctx, old_ptr, newsize); + spinlock_release(&vm_ctx.dynalloc_lock); + return tmp; } /* @@ -70,5 +78,7 @@ dynfree(void *ptr) /* TODO: Per CPU */ struct vm_ctx vm_ctx = vm_get_bsp_ctx(); + spinlock_acquire(&vm_ctx.dynalloc_lock); tlsf_free(vm_ctx.tlsf_ctx, ptr); + spinlock_release(&vm_ctx.dynalloc_lock); } -- cgit v1.2.3