From 071a5651e01397c62e1adbe6faea3132afc6f18f Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Wed, 12 Aug 2026 14:50:47 -0400 Subject: [PATCH] list: change locking discipline Signed-off-by: Michael Shaw --- drivers/Makefile | 1 - drivers/pci/pci.c | 6 ++++++ fs/vfs/inode.c | 8 ++++++++ fs/vfs/mount.c | 4 ++++ fs/vfs/ramfs.c | 7 +++++++ fs/vfs/vfs.c | 7 ++++++- include/lib/list.h | 4 +++- kernel/driver.c | 4 ++++ kernel/module/modload.c | 2 ++ kernel/tty/tee.c | 9 +++++++++ lib/list.c | 24 ++++++++++++------------ 11 files changed, 61 insertions(+), 15 deletions(-) diff --git a/drivers/Makefile b/drivers/Makefile index d6a6576..25f80aa 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -2,5 +2,4 @@ obj-y += acpi/ obj-y += pci/ obj-y += term/ - obj-m += ahci.o diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 9b5a448..43a9d1c 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -57,6 +57,8 @@ size_t pci_find_device(u16 major, u16 minor, list_t *out) { size_t n = 0; + list_lock(out); + list_item_t *item = pci_devices.head; while (item) { @@ -69,6 +71,8 @@ size_t pci_find_device(u16 major, u16 minor, list_t *out) item = item->next; } + list_unlock(out); + return n; } @@ -125,7 +129,9 @@ static void host_probe_devices(struct pci_host *host) if ((bars[2] & 0x6) == 0x4) pdev->bar2 |= ((u64)bars[3] << 32); + list_lock(&pci_devices); list_insert(&pci_devices, pdev); + list_unlock(&pci_devices); if (func == 0) { uint8_t header = (pci_config_read32(host, bus, dev, 0, 0x0C) >> 16) & 0xFF; if (!(header & 0x80)) diff --git a/fs/vfs/inode.c b/fs/vfs/inode.c index 051bd75..4069eab 100644 --- a/fs/vfs/inode.c +++ b/fs/vfs/inode.c @@ -30,6 +30,8 @@ int dirent_populate_children(dirent_t *dir) return n; } + list_lock(&inode->dirents); + for (size_t i = 0; i < n; i++) { dirent_t *dent = dents + i; dirent_t *insert = dirent_dup(dent); @@ -45,6 +47,7 @@ int dirent_populate_children(dirent_t *dir) } } + list_unlock(&inode->dirents); write_unlock(&inode->rwlock); kfree(dents); return 0; @@ -62,6 +65,7 @@ cleanup: item = next; } } + list_unlock(&inode->dirents); write_unlock(&inode->rwlock); kfree(dents); return -ENOMEM; @@ -121,6 +125,8 @@ inode_t *inode_create(void) void inode_free(inode_t *inode) { + list_lock(&inode->dirents); + while (inode->dirents.head) { list_item_t *item = inode->dirents.head; dirent_t *dent = item->ptr; @@ -132,6 +138,8 @@ void inode_free(inode_t *inode) } } + list_unlock(&inode->dirents); + kfree(inode->data); slab_free(inode_slab, inode); } diff --git a/fs/vfs/mount.c b/fs/vfs/mount.c index b3e64fe..418b395 100644 --- a/fs/vfs/mount.c +++ b/fs/vfs/mount.c @@ -128,7 +128,9 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags) list_create(&m->children); + list_lock(&parent->children); m->child_link = list_insert(&parent->children, m); + list_unlock(&parent->children); if (!m->child_link) { dirent_dec_ref(mp); kfree(m); @@ -181,7 +183,9 @@ int vfs_umount(const char *path) return -EBUSY; } + list_lock(target->child_link->list); list_remove(target->child_link); + list_unlock(target->child_link->list); dirent_dec_ref(target->mountpoint); kfree(target); diff --git a/fs/vfs/ramfs.c b/fs/vfs/ramfs.c index eaed320..e2d4759 100644 --- a/fs/vfs/ramfs.c +++ b/fs/vfs/ramfs.c @@ -139,10 +139,13 @@ static int link(inode_t *parent_dir, inode_t *child, const char *name, dirent_t new->inode = child; + list_lock(&parent_dir->dirents); if (!list_insert(&parent_dir->dirents, new)) { + list_unlock(&parent_dir->dirents); dirent_free(new); return -ENOMEM; } + list_unlock(&parent_dir->dirents); *out = new; return 0; @@ -153,17 +156,21 @@ static int unlink(inode_t *parent_dir, const char *name) if (!parent_dir || !name) return -EINVAL; + list_lock(&parent_dir->dirents); + list_item_t *item = parent_dir->dirents.head; while (item) { dirent_t *dent = item->ptr; if (!strcmp(dent->name, name)) { list_remove(item); + list_unlock(&parent_dir->dirents); dirent_dec_ref(dent); return 0; } item = item->next; } + list_unlock(&parent_dir->dirents); return -ENOENT; } diff --git a/fs/vfs/vfs.c b/fs/vfs/vfs.c index de6e00e..194f6b6 100644 --- a/fs/vfs/vfs.c +++ b/fs/vfs/vfs.c @@ -313,8 +313,13 @@ void vfs_register(struct fs_driver *fs) return; memcpy(ins, fs, sizeof(*fs)); - if (!list_insert(&fs_type_list, ins)) + list_lock(&fs_type_list); + if (!list_insert(&fs_type_list, ins)) { + list_unlock(&fs_type_list); kfree(ins); + } else { + list_unlock(&fs_type_list); + } } void vfs_init(void) diff --git a/include/lib/list.h b/include/lib/list.h index 66d5bca..6d9128b 100644 --- a/include/lib/list.h +++ b/include/lib/list.h @@ -21,9 +21,11 @@ typedef struct _list_head { } list_t; void list_create(list_t *list); -void list_destroy(list_t * list); +void list_destroy(list_t *list); list_item_t *list_insert(list_t *list, void *item); void list_remove(list_item_t *item); +void list_lock(list_t *list); +void list_unlock(list_t *list); void list_init(); #endif /* _LIST_H_ */ diff --git a/kernel/driver.c b/kernel/driver.c index 4dd6752..353401b 100644 --- a/kernel/driver.c +++ b/kernel/driver.c @@ -15,6 +15,8 @@ struct driver *driver_register(const char *name, u32 type, void *init, void *dei struct driver *driver = NULL; + list_lock(&pending_drivers); + list_item_t *item = pending_drivers.head; while (item) { struct driver *d = item->ptr; @@ -26,6 +28,8 @@ struct driver *driver_register(const char *name, u32 type, void *init, void *dei item = item->next; } + list_unlock(&pending_drivers); + if (!driver) { driver = kzalloc(sizeof(struct driver), ALLOC_KERN); strncpy(driver->name, name, sizeof(driver->name) - 1); diff --git a/kernel/module/modload.c b/kernel/module/modload.c index fbdbef4..dc0cda4 100644 --- a/kernel/module/modload.c +++ b/kernel/module/modload.c @@ -260,7 +260,9 @@ static int elf_load(const char *name, const char *file, size_t fsize) d->base = base; d->size = total; d->n_users = 1; + list_lock(&pending_drivers); list_insert(&pending_drivers, d); + list_unlock(&pending_drivers); } } diff --git a/kernel/tty/tee.c b/kernel/tty/tee.c index 967a662..edf51e8 100644 --- a/kernel/tty/tee.c +++ b/kernel/tty/tee.c @@ -33,7 +33,9 @@ struct tty *tty_tee_create(const char *name) struct tty *ret = tty_create(name, list); if (!ret) { + list_lock(list); list_destroy(list); + list_unlock(list); kfree(list); return NULL; } @@ -49,7 +51,9 @@ void tty_tee_add(struct tty *tee, struct tty *child) return; list_t *list = tee->priv; + list_lock(list); list_insert(list, child); + list_unlock(list); } void tty_tee_remove(struct tty *tee, struct tty *child) @@ -58,13 +62,18 @@ void tty_tee_remove(struct tty *tee, struct tty *child) if (!list) return; + list_lock(list); + list_item_t *item = list->head; while (item) { if (item->ptr == child) { list_remove(item); + list_unlock(list); return; } item = item->next; } + + list_unlock(list); } diff --git a/lib/list.c b/lib/list.c index ac51975..fb6a542 100644 --- a/lib/list.c +++ b/lib/list.c @@ -15,9 +15,7 @@ void list_create(list_t *list) void list_destroy(list_t *list) { - spinlock_acquire(&list->lock); list_item_t *item = list->head; - while (item) { list_item_t *next = item->next; slab_free(list_cache, item); @@ -25,17 +23,14 @@ void list_destroy(list_t *list) } list->head = NULL; list->tail = NULL; - spinlock_release(&list->lock); } list_item_t *list_insert(list_t *list, void *item) { - spinlock_acquire(&list->lock); list_item_t *ret = slab_alloc(list_cache); - if (!ret) { - spinlock_release(&list->lock); + if (!ret) return NULL; - } + ret->ptr = item; ret->list = list; ret->next = NULL; @@ -50,8 +45,6 @@ list_item_t *list_insert(list_t *list, void *item) } list->tail = ret; - spinlock_release(&list->lock); - return ret; } @@ -59,7 +52,6 @@ void list_remove(list_item_t *item) { list_t *list = item->list; - spinlock_acquire(&list->lock); if (item == list->head) list->head = item->next; if (item == list->tail) @@ -67,14 +59,22 @@ void list_remove(list_item_t *item) if (item->next) item->next->prev = item->prev; - if (item->prev) item->prev->next = item->next; - spinlock_release(&list->lock); slab_free(list_cache, item); } +void list_lock(list_t *list) +{ + spinlock_acquire(&list->lock); +} + +void list_unlock(list_t *list) +{ + spinlock_release(&list->lock); +} + void list_init() { list_cache = slab_create("list_cache", sizeof(list_item_t), 64 * KB, 0x8);