kernel: rebuild object ref-counting using atomic types

This commit is contained in:
2026-03-24 19:10:36 +00:00
parent 9faa11cddc
commit 7dc0c742fa
9 changed files with 42 additions and 97 deletions

View File

@@ -46,7 +46,7 @@ static void do_handle_table_destroy_leaf(struct handle_table *tab)
struct handle *child = &tab->t_handles.t_handle_list[index];
bitmap_clear(tab->t_subtables.t_subtable_map, index);
if (child->h_object) {
object_remove_handle(child->h_object);
object_unref(child->h_object);
child->h_object = NULL;
}
}
@@ -195,7 +195,7 @@ kern_status_t handle_table_free_handle(
= &tab->t_handles.t_handle_list[handle_index];
if (handle_entry->h_object) {
object_remove_handle(handle_entry->h_object);
object_unref(handle_entry->h_object);
}
memset(handle_entry, 0x0, sizeof *handle_entry);
@@ -307,7 +307,7 @@ kern_status_t handle_table_transfer(
dst_entry->h_object = src_entry->h_object;
dst_entry->h_flags = src_entry->h_flags;
object_add_handle(dst_entry->h_object);
object_ref(dst_entry->h_object);
handle_table_free_handle(src, src_handles[i].hnd_value);
@@ -326,7 +326,7 @@ kern_status_t handle_table_transfer(
dst_entry->h_object = src_entry->h_object;
dst_entry->h_flags = src_entry->h_flags;
object_add_handle(dst_entry->h_object);
object_ref(dst_entry->h_object);
dst_handle.hnd_mode = src_handles[i].hnd_mode;
dst_handle.hnd_value = dst_value;
@@ -371,7 +371,7 @@ kern_status_t handle_table_transfer(
struct handle *src_entry
= handle_table_get_handle(src, handle.hnd_value);
if (src_entry) {
object_remove_handle(src_entry->h_object);
object_unref(src_entry->h_object);
handle_table_free_handle(src, handle.hnd_value);
}
}

View File

@@ -74,90 +74,28 @@ struct object *object_create(struct object_type *type)
obj->ob_lock = SPIN_LOCK_INIT;
obj->ob_magic = OBJECT_MAGIC;
obj->ob_refcount = 1;
obj->ob_handles = 0;
return obj;
}
struct object *object_ref(struct object *obj)
{
obj->ob_refcount++;
atomic_add_fetch(&obj->ob_refcount, 1);
return obj;
}
static void __cleanup(struct object *obj, struct queue *queue)
{
if (HAS_OP(obj, destroy)) {
obj->ob_type->ob_ops.destroy(obj, queue);
}
vm_cache_free(&obj->ob_type->ob_cache, obj);
}
static void object_cleanup(struct object *obj, unsigned long flags)
{
if (obj->ob_refcount > 0 || obj->ob_handles > 0) {
spin_unlock_irqrestore(&obj->ob_lock, flags);
return;
}
struct queue queue = QUEUE_INIT;
__cleanup(obj, &queue);
if (!HAS_OP(obj, destroy_recurse)) {
return;
}
while (!queue_empty(&queue)) {
struct queue_entry *entry = queue_pop_front(&queue);
struct object *child = NULL;
obj->ob_type->ob_ops.destroy_recurse(entry, &child);
if (!child) {
continue;
}
if (child->ob_refcount > 1) {
child->ob_refcount--;
continue;
}
if (child->ob_refcount == 0 && child->ob_handles == 0) {
__cleanup(child, &queue);
}
}
}
void object_unref(struct object *obj)
{
unsigned long flags;
spin_lock_irqsave(&obj->ob_lock, &flags);
if (obj->ob_refcount == 0) {
spin_unlock_irqrestore(&obj->ob_lock, flags);
int ref = atomic_sub_fetch(&obj->ob_refcount, 1);
if (ref > 0) {
return;
}
obj->ob_refcount--;
object_cleanup(obj, flags);
}
void object_add_handle(struct object *obj)
{
obj->ob_handles++;
}
void object_remove_handle(struct object *obj)
{
unsigned long flags;
spin_lock_irqsave(&obj->ob_lock, &flags);
if (obj->ob_handles == 0) {
spin_unlock_irqrestore(&obj->ob_lock, flags);
return;
if (HAS_OP(obj, destroy)) {
obj->ob_type->ob_ops.destroy(obj);
}
obj->ob_handles--;
object_cleanup(obj, flags);
vm_cache_free(&obj->ob_type->ob_cache, obj);
}
void object_lock(struct object *obj)

View File

@@ -17,7 +17,7 @@ static struct object_type port_type = {
},
};
static kern_status_t port_cleanup(struct object *obj, struct queue *q)
static kern_status_t port_cleanup(struct object *obj)
{
struct port *port = PORT_CAST(obj);
port_disconnect(port);