list: change locking discipline

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-08-12 14:50:47 -04:00
commit 071a5651e0
Signed by: michaelsshaw
GPG key ID: 5A5E0FB20708A9E7
11 changed files with 61 additions and 15 deletions

View file

@ -2,5 +2,4 @@
obj-y += acpi/ obj-y += acpi/
obj-y += pci/ obj-y += pci/
obj-y += term/ obj-y += term/
obj-m += ahci.o obj-m += ahci.o

View file

@ -57,6 +57,8 @@ size_t pci_find_device(u16 major, u16 minor, list_t *out)
{ {
size_t n = 0; size_t n = 0;
list_lock(out);
list_item_t *item = pci_devices.head; list_item_t *item = pci_devices.head;
while (item) { while (item) {
@ -69,6 +71,8 @@ size_t pci_find_device(u16 major, u16 minor, list_t *out)
item = item->next; item = item->next;
} }
list_unlock(out);
return n; return n;
} }
@ -125,7 +129,9 @@ static void host_probe_devices(struct pci_host *host)
if ((bars[2] & 0x6) == 0x4) if ((bars[2] & 0x6) == 0x4)
pdev->bar2 |= ((u64)bars[3] << 32); pdev->bar2 |= ((u64)bars[3] << 32);
list_lock(&pci_devices);
list_insert(&pci_devices, pdev); list_insert(&pci_devices, pdev);
list_unlock(&pci_devices);
if (func == 0) { if (func == 0) {
uint8_t header = (pci_config_read32(host, bus, dev, 0, 0x0C) >> 16) & 0xFF; uint8_t header = (pci_config_read32(host, bus, dev, 0, 0x0C) >> 16) & 0xFF;
if (!(header & 0x80)) if (!(header & 0x80))

View file

@ -30,6 +30,8 @@ int dirent_populate_children(dirent_t *dir)
return n; return n;
} }
list_lock(&inode->dirents);
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
dirent_t *dent = dents + i; dirent_t *dent = dents + i;
dirent_t *insert = dirent_dup(dent); 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); write_unlock(&inode->rwlock);
kfree(dents); kfree(dents);
return 0; return 0;
@ -62,6 +65,7 @@ cleanup:
item = next; item = next;
} }
} }
list_unlock(&inode->dirents);
write_unlock(&inode->rwlock); write_unlock(&inode->rwlock);
kfree(dents); kfree(dents);
return -ENOMEM; return -ENOMEM;
@ -121,6 +125,8 @@ inode_t *inode_create(void)
void inode_free(inode_t *inode) void inode_free(inode_t *inode)
{ {
list_lock(&inode->dirents);
while (inode->dirents.head) { while (inode->dirents.head) {
list_item_t *item = inode->dirents.head; list_item_t *item = inode->dirents.head;
dirent_t *dent = item->ptr; dirent_t *dent = item->ptr;
@ -132,6 +138,8 @@ void inode_free(inode_t *inode)
} }
} }
list_unlock(&inode->dirents);
kfree(inode->data); kfree(inode->data);
slab_free(inode_slab, inode); slab_free(inode_slab, inode);
} }

View file

@ -128,7 +128,9 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags)
list_create(&m->children); list_create(&m->children);
list_lock(&parent->children);
m->child_link = list_insert(&parent->children, m); m->child_link = list_insert(&parent->children, m);
list_unlock(&parent->children);
if (!m->child_link) { if (!m->child_link) {
dirent_dec_ref(mp); dirent_dec_ref(mp);
kfree(m); kfree(m);
@ -181,7 +183,9 @@ int vfs_umount(const char *path)
return -EBUSY; return -EBUSY;
} }
list_lock(target->child_link->list);
list_remove(target->child_link); list_remove(target->child_link);
list_unlock(target->child_link->list);
dirent_dec_ref(target->mountpoint); dirent_dec_ref(target->mountpoint);
kfree(target); kfree(target);

View file

@ -139,10 +139,13 @@ static int link(inode_t *parent_dir, inode_t *child, const char *name, dirent_t
new->inode = child; new->inode = child;
list_lock(&parent_dir->dirents);
if (!list_insert(&parent_dir->dirents, new)) { if (!list_insert(&parent_dir->dirents, new)) {
list_unlock(&parent_dir->dirents);
dirent_free(new); dirent_free(new);
return -ENOMEM; return -ENOMEM;
} }
list_unlock(&parent_dir->dirents);
*out = new; *out = new;
return 0; return 0;
@ -153,17 +156,21 @@ static int unlink(inode_t *parent_dir, const char *name)
if (!parent_dir || !name) if (!parent_dir || !name)
return -EINVAL; return -EINVAL;
list_lock(&parent_dir->dirents);
list_item_t *item = parent_dir->dirents.head; list_item_t *item = parent_dir->dirents.head;
while (item) { while (item) {
dirent_t *dent = item->ptr; dirent_t *dent = item->ptr;
if (!strcmp(dent->name, name)) { if (!strcmp(dent->name, name)) {
list_remove(item); list_remove(item);
list_unlock(&parent_dir->dirents);
dirent_dec_ref(dent); dirent_dec_ref(dent);
return 0; return 0;
} }
item = item->next; item = item->next;
} }
list_unlock(&parent_dir->dirents);
return -ENOENT; return -ENOENT;
} }

View file

@ -313,8 +313,13 @@ void vfs_register(struct fs_driver *fs)
return; return;
memcpy(ins, fs, sizeof(*fs)); 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); kfree(ins);
} else {
list_unlock(&fs_type_list);
}
} }
void vfs_init(void) void vfs_init(void)

View file

@ -21,9 +21,11 @@ typedef struct _list_head {
} list_t; } list_t;
void list_create(list_t *list); 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); list_item_t *list_insert(list_t *list, void *item);
void list_remove(list_item_t *item); void list_remove(list_item_t *item);
void list_lock(list_t *list);
void list_unlock(list_t *list);
void list_init(); void list_init();
#endif /* _LIST_H_ */ #endif /* _LIST_H_ */

View file

@ -15,6 +15,8 @@ struct driver *driver_register(const char *name, u32 type, void *init, void *dei
struct driver *driver = NULL; struct driver *driver = NULL;
list_lock(&pending_drivers);
list_item_t *item = pending_drivers.head; list_item_t *item = pending_drivers.head;
while (item) { while (item) {
struct driver *d = item->ptr; 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; item = item->next;
} }
list_unlock(&pending_drivers);
if (!driver) { if (!driver) {
driver = kzalloc(sizeof(struct driver), ALLOC_KERN); driver = kzalloc(sizeof(struct driver), ALLOC_KERN);
strncpy(driver->name, name, sizeof(driver->name) - 1); strncpy(driver->name, name, sizeof(driver->name) - 1);

View file

@ -260,7 +260,9 @@ static int elf_load(const char *name, const char *file, size_t fsize)
d->base = base; d->base = base;
d->size = total; d->size = total;
d->n_users = 1; d->n_users = 1;
list_lock(&pending_drivers);
list_insert(&pending_drivers, d); list_insert(&pending_drivers, d);
list_unlock(&pending_drivers);
} }
} }

View file

@ -33,7 +33,9 @@ struct tty *tty_tee_create(const char *name)
struct tty *ret = tty_create(name, list); struct tty *ret = tty_create(name, list);
if (!ret) { if (!ret) {
list_lock(list);
list_destroy(list); list_destroy(list);
list_unlock(list);
kfree(list); kfree(list);
return NULL; return NULL;
} }
@ -49,7 +51,9 @@ void tty_tee_add(struct tty *tee, struct tty *child)
return; return;
list_t *list = tee->priv; list_t *list = tee->priv;
list_lock(list);
list_insert(list, child); list_insert(list, child);
list_unlock(list);
} }
void tty_tee_remove(struct tty *tee, struct tty *child) 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) if (!list)
return; return;
list_lock(list);
list_item_t *item = list->head; list_item_t *item = list->head;
while (item) { while (item) {
if (item->ptr == child) { if (item->ptr == child) {
list_remove(item); list_remove(item);
list_unlock(list);
return; return;
} }
item = item->next; item = item->next;
} }
list_unlock(list);
} }

View file

@ -15,9 +15,7 @@ void list_create(list_t *list)
void list_destroy(list_t *list) void list_destroy(list_t *list)
{ {
spinlock_acquire(&list->lock);
list_item_t *item = list->head; list_item_t *item = list->head;
while (item) { while (item) {
list_item_t *next = item->next; list_item_t *next = item->next;
slab_free(list_cache, item); slab_free(list_cache, item);
@ -25,17 +23,14 @@ void list_destroy(list_t *list)
} }
list->head = NULL; list->head = NULL;
list->tail = NULL; list->tail = NULL;
spinlock_release(&list->lock);
} }
list_item_t *list_insert(list_t *list, void *item) list_item_t *list_insert(list_t *list, void *item)
{ {
spinlock_acquire(&list->lock);
list_item_t *ret = slab_alloc(list_cache); list_item_t *ret = slab_alloc(list_cache);
if (!ret) { if (!ret)
spinlock_release(&list->lock);
return NULL; return NULL;
}
ret->ptr = item; ret->ptr = item;
ret->list = list; ret->list = list;
ret->next = NULL; ret->next = NULL;
@ -50,8 +45,6 @@ list_item_t *list_insert(list_t *list, void *item)
} }
list->tail = ret; list->tail = ret;
spinlock_release(&list->lock);
return ret; return ret;
} }
@ -59,7 +52,6 @@ void list_remove(list_item_t *item)
{ {
list_t *list = item->list; list_t *list = item->list;
spinlock_acquire(&list->lock);
if (item == list->head) if (item == list->head)
list->head = item->next; list->head = item->next;
if (item == list->tail) if (item == list->tail)
@ -67,14 +59,22 @@ void list_remove(list_item_t *item)
if (item->next) if (item->next)
item->next->prev = item->prev; item->next->prev = item->prev;
if (item->prev) if (item->prev)
item->prev->next = item->next; item->prev->next = item->next;
spinlock_release(&list->lock);
slab_free(list_cache, item); 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() void list_init()
{ {
list_cache = slab_create("list_cache", sizeof(list_item_t), 64 * KB, 0x8); list_cache = slab_create("list_cache", sizeof(list_item_t), 64 * KB, 0x8);