From 6ba236b2feee3c3d139b573fae1a251978403f27 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Thu, 12 Mar 2026 20:42:05 +0000 Subject: [PATCH] kernel: resolving a handle now increments the refcount of the corresponding object --- sched/task.c | 2 +- syscall/msg.c | 22 ---------------------- syscall/task.c | 9 ++++++--- syscall/vm-object.c | 3 --- syscall/vm-region.c | 10 ---------- 5 files changed, 7 insertions(+), 39 deletions(-) diff --git a/sched/task.c b/sched/task.c index 1f0ad6a..5c081ef 100644 --- a/sched/task.c +++ b/sched/task.c @@ -326,7 +326,7 @@ kern_status_t task_resolve_handle( } if (out_obj) { - *out_obj = handle_data->h_object; + *out_obj = object_ref(handle_data->h_object); } if (out_flags) { diff --git a/syscall/msg.c b/syscall/msg.c index d42e412..b32f823 100644 --- a/syscall/msg.c +++ b/syscall/msg.c @@ -92,9 +92,6 @@ kern_status_t sys_port_connect( return status; } - /* add a reference to the port object to make sure it isn't deleted - * while we're using it */ - object_ref(port_obj); struct port *port = port_cast(port_obj); task_unlock_irqrestore(self, flags); @@ -117,7 +114,6 @@ kern_status_t sys_port_connect( port_lock_irqsave(port, &flags); status = port_connect(port, remote); port_unlock_irqrestore(port, flags); - object_unref(port_obj); object_unref(&remote->c_base); return KERN_OK; @@ -141,9 +137,6 @@ kern_status_t sys_port_disconnect(kern_handle_t port_handle) return status; } - /* add a reference to the port object to make sure it isn't deleted - * while we're using it */ - object_ref(port_obj); task_unlock_irqrestore(self, flags); struct port *port = port_cast(port_obj); @@ -251,9 +244,6 @@ kern_status_t sys_msg_send( return status; } - /* add a reference to the port object to make sure it isn't deleted - * while we're using it */ - object_ref(port_obj); task_unlock_irqrestore(self, flags); struct port *port = port_cast(port_obj); @@ -293,9 +283,6 @@ kern_status_t sys_msg_recv(kern_handle_t channel_handle, kern_msg_t *out_msg) return status; } - /* add a reference to the port object to make sure it isn't deleted - * while we're using it */ - object_ref(channel_obj); task_unlock_irqrestore(self, flags); struct channel *channel = channel_cast(channel_obj); @@ -338,9 +325,6 @@ kern_status_t sys_msg_reply( return status; } - /* add a reference to the port object to make sure it isn't deleted - * while we're using it */ - object_ref(channel_obj); task_unlock_irqrestore(self, flags); struct channel *channel = channel_cast(channel_obj); @@ -390,9 +374,6 @@ kern_status_t sys_msg_read( return status; } - /* add a reference to the port object to make sure it isn't deleted - * while we're using it */ - object_ref(channel_obj); task_unlock_irqrestore(self, flags); struct channel *channel = channel_cast(channel_obj); @@ -450,9 +431,6 @@ kern_status_t sys_msg_write( return status; } - /* add a reference to the port object to make sure it isn't deleted - * while we're using it */ - object_ref(channel_obj); task_unlock_irqrestore(self, flags); struct channel *channel = channel_cast(channel_obj); diff --git a/syscall/task.c b/syscall/task.c index c425015..53192d5 100644 --- a/syscall/task.c +++ b/syscall/task.c @@ -84,7 +84,6 @@ kern_status_t sys_task_create( return status; } - object_ref(parent_obj); struct task *parent = task_cast(parent_obj); struct handle *child_handle_slot = NULL, *space_handle_slot = NULL; @@ -94,6 +93,7 @@ kern_status_t sys_task_create( &child_handle_slot, &child_handle); if (status != KERN_OK) { + object_unref(parent_obj); task_unlock_irqrestore(self, flags); return status; } @@ -103,6 +103,7 @@ kern_status_t sys_task_create( &space_handle_slot, &space_handle); if (status != KERN_OK) { + object_unref(parent_obj); handle_table_free_handle(self->t_handles, child_handle); task_unlock_irqrestore(self, flags); return status; @@ -169,7 +170,6 @@ kern_status_t sys_task_create_thread( return status; } - object_ref(target_obj); struct task *target = task_cast(target_obj); struct handle *target_handle = NULL; @@ -238,6 +238,7 @@ kern_status_t sys_task_get_address_space( &handle_slot, &handle); if (status != KERN_OK) { + object_unref(task_obj); task_unlock_irqrestore(self, flags); return status; } @@ -245,6 +246,7 @@ kern_status_t sys_task_get_address_space( struct task *task = task_cast(task_obj); if (!task) { + object_unref(task_obj); handle_table_free_handle(self->t_handles, handle); task_unlock_irqrestore(self, flags); return KERN_INVALID_ARGUMENT; @@ -253,6 +255,7 @@ kern_status_t sys_task_get_address_space( handle_slot->h_object = &task->t_address_space->vr_base; object_add_handle(&task->t_address_space->vr_base); task_unlock_irqrestore(self, flags); + object_unref(task_obj); *out = handle; return KERN_OK; @@ -276,11 +279,11 @@ kern_status_t sys_thread_start(kern_handle_t thread_handle) return status; } - object_ref(thread_obj); struct thread *thread = thread_cast(thread_obj); task_unlock_irqrestore(self, flags); schedule_thread_on_cpu(thread); + object_unref(thread_obj); return KERN_OK; } diff --git a/syscall/vm-object.c b/syscall/vm-object.c index 5b5baeb..7f789ae 100644 --- a/syscall/vm-object.c +++ b/syscall/vm-object.c @@ -144,9 +144,6 @@ kern_status_t sys_vm_object_copy( return status; } - object_ref(src_obj); - object_ref(dst_obj); - task_unlock_irqrestore(self, flags); struct vm_object *dst_vmo = vm_object_cast(dst_obj); diff --git a/syscall/vm-region.c b/syscall/vm-region.c index 80c9831..387db5b 100644 --- a/syscall/vm-region.c +++ b/syscall/vm-region.c @@ -61,7 +61,6 @@ kern_status_t sys_vm_region_create( return status; } - object_ref(obj); task_unlock_irqrestore(self, flags); vm_region_lock_irqsave(parent_region, &flags); @@ -116,7 +115,6 @@ kern_status_t sys_vm_region_kill(kern_handle_t region_handle) return KERN_INVALID_ARGUMENT; } - object_ref(obj); task_unlock_irqrestore(self, flags); vm_region_lock_irqsave(region, &flags); @@ -162,7 +160,6 @@ kern_status_t sys_vm_region_read( return KERN_INVALID_ARGUMENT; } - object_ref(obj); task_unlock_irqrestore(self, flags); virt_addr_t src_address = vm_region_get_base_address(region) + offset; @@ -214,7 +211,6 @@ kern_status_t sys_vm_region_write( return KERN_INVALID_ARGUMENT; } - object_ref(obj); task_unlock_irqrestore(self, flags); virt_addr_t dst_address = vm_region_get_base_address(region) + offset; @@ -283,8 +279,6 @@ kern_status_t sys_vm_region_map_absolute( return KERN_INVALID_ARGUMENT; } - object_ref(vmo_obj); - object_ref(region_obj); task_unlock_irqrestore(self, flags); off_t region_offset = VM_REGION_ANY_OFFSET; @@ -369,8 +363,6 @@ kern_status_t sys_vm_region_map_relative( return KERN_INVALID_ARGUMENT; } - object_ref(vmo_obj); - object_ref(region_obj); task_unlock_irqrestore(self, flags); status = vm_region_map_object( @@ -418,7 +410,6 @@ kern_status_t sys_vm_region_unmap_absolute( return KERN_INVALID_ARGUMENT; } - object_ref(region_obj); task_unlock_irqrestore(self, flags); off_t region_offset = address - vm_region_get_base_address(region); @@ -458,7 +449,6 @@ kern_status_t sys_vm_region_unmap_relative( return KERN_INVALID_ARGUMENT; } - object_ref(region_obj); task_unlock_irqrestore(self, flags); status = vm_region_unmap(region, offset, length);