From 473112b1541cf81fa3670e0d1cb6de1c4a3281de Mon Sep 17 00:00:00 2001 From: dzwdz Date: Tue, 16 Jul 2024 23:55:32 +0200 Subject: kernel: make kmalloc accept a numeric "tag" instead of a freeform description This will both let me save space in the allocation header, and make the debugprint more readable. --- src/kernel/arch/amd64/driver/fsroot.c | 2 +- src/kernel/arch/amd64/driver/time.c | 2 +- src/kernel/handle.c | 2 +- src/kernel/handleset.c | 2 +- src/kernel/malloc.c | 26 +++++++++++++++++++++++++- src/kernel/malloc.h | 28 +++++++++++++++++++++++++--- src/kernel/proc.c | 6 +++--- src/kernel/syscalls.c | 11 +++++------ src/kernel/vfs/mount.c | 4 ++-- src/kernel/vfs/procfs.c | 4 ++-- src/kernel/vfs/request.c | 2 +- 11 files changed, 67 insertions(+), 22 deletions(-) diff --git a/src/kernel/arch/amd64/driver/fsroot.c b/src/kernel/arch/amd64/driver/fsroot.c index 228232e..a283daa 100644 --- a/src/kernel/arch/amd64/driver/fsroot.c +++ b/src/kernel/arch/amd64/driver/fsroot.c @@ -53,7 +53,7 @@ handle(VfsReq *req) case Hdev: if (!dev) { dev_len = get_dev(NULL); - dev = kmalloc(dev_len, "/ cache"); + dev = kmalloc(dev_len, TagRootCache); get_dev(dev); } lst = dev; diff --git a/src/kernel/arch/amd64/driver/time.c b/src/kernel/arch/amd64/driver/time.c index 8ae6fb2..f47cb74 100644 --- a/src/kernel/arch/amd64/driver/time.c +++ b/src/kernel/arch/amd64/driver/time.c @@ -24,7 +24,7 @@ handle(VfsReq *req) TimeObj *h; if (req->type == VFSOP_OPEN) { if (reqpathcmp(req, "")) { - h = kmalloc(sizeof *h, "dev/time"); + h = kmalloc(sizeof *h, TagDevTime); h->base = uptime_ns(); return (uintptr_t)h; } else { diff --git a/src/kernel/handle.c b/src/kernel/handle.c index 06a810c..0703cac 100644 --- a/src/kernel/handle.c +++ b/src/kernel/handle.c @@ -8,7 +8,7 @@ #include Handle *handle_init(enum handle_type type) { - Handle *h = kzalloc(sizeof *h, "handle"); + Handle *h = kzalloc(sizeof *h, TagHandle); h->type = type; h->refcount = 1; return h; diff --git a/src/kernel/handleset.c b/src/kernel/handleset.c index fc2d426..cf8160b 100644 --- a/src/kernel/handleset.c +++ b/src/kernel/handleset.c @@ -7,7 +7,7 @@ HandleSet * hs_init(void) { - HandleSet *hs = kzalloc(sizeof *hs, "handles"); + HandleSet *hs = kzalloc(sizeof *hs, TagHandleset); hs->refcount = 1; return hs; } diff --git a/src/kernel/malloc.c b/src/kernel/malloc.c index 3c10108..ca44cfe 100644 --- a/src/kernel/malloc.c +++ b/src/kernel/malloc.c @@ -11,6 +11,27 @@ #define SCMIN 6 /* 1<<6 == 64 */ #define SCMAX 12 /* 1<<11 == 2048 */ +const char *tagnames[] = { + "TagInvalid", + "TagKernelFs", + "TagProcFs", + "TagUserFs", + "TagOpenPath", + "TagMountPath", + "TagMountUser", + "TagMountRoot", + "TagExecbuf", + "TagVfsReq", + "TagHandleset", + "TagHandle", + "TagProcess", + "TagProcessHandle", + "TagDevTime", + "TagRootCache", + "TagPageRefcount", +}; +static_assert(sizeof(tagnames) == sizeof(const char *) * TagLast); + typedef struct Slab Slab; struct Slab { /* The slab is divided up into 1<state = PS_RUNNING; proc_first->pages = pagedir_new(); proc_first->mount = vfs_mount_seed(); @@ -47,11 +47,11 @@ Proc *proc_seed(void *data, size_t datalen) { } Proc *proc_fork(Proc *parent, int flags) { - Proc *child = kzalloc(sizeof *child, "proc"); + Proc *child = kzalloc(sizeof *child, TagProcess); if (flags & FORK_SHAREMEM) { if (!parent->pages_refcount) { - parent->pages_refcount = kmalloc(sizeof *parent->pages_refcount, "pagerefs"); + parent->pages_refcount = kmalloc(sizeof *parent->pages_refcount, TagPageRefcount); *parent->pages_refcount = 1; } *parent->pages_refcount += 1; diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index 1e1cd2f..3fc09fa 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -38,7 +38,7 @@ long _sys_fork(int flags, hid_t __user *fs_front) { SYSCALL_RETURN(-EMFILE); } - h->backend = kzalloc(sizeof *h->backend, "user fs"); + h->backend = kzalloc(sizeof *h->backend, TagUserFs); h->backend->usehcnt = 1; /* handle */ h->backend->is_user = true; h->backend->user.provhcnt = 1; @@ -68,7 +68,7 @@ hid_t _sys_open(const char __user *path, long len, int flags) { /* Doesn't check for free handles. Another thread could use up all * handles in the meantime anyways, or free some up. */ - path_buf = kmalloc(len, "path opn"); + path_buf = kmalloc(len, TagOpenPath); if (pcpy_from(proc_cur, path_buf, path, len) < (size_t)len) { goto fail; } @@ -111,7 +111,7 @@ long _sys_mount(hid_t hid, const char __user *path, long len) { if (PATH_MAX < len) SYSCALL_RETURN(-1); - path_buf = kmalloc(len, "path mnt"); + path_buf = kmalloc(len, TagMountPath); if (pcpy_from(proc_cur, path_buf, path, len) < (size_t)len) { goto fail; } @@ -138,7 +138,7 @@ long _sys_mount(hid_t hid, const char __user *path, long len) { // append to mount list // TODO move to kernel/vfs/mount.c - mount = kmalloc(sizeof *mount, "user mnt"); + mount = kmalloc(sizeof *mount, TagMountUser); mount->prev = proc_cur->mount; mount->prefix = path_buf; mount->prefix_owned = true; @@ -463,9 +463,8 @@ long _sys_execbuf(void __user *ubuf, size_t len) { SYSCALL_RETURN(-1); if (proc_cur->execbuf.buf) SYSCALL_RETURN(-1); - // TODO consider supporting nesting execbufs - char *kbuf = kmalloc(len, "execbuf"); + char *kbuf = kmalloc(len, TagExecbuf); if (pcpy_from(proc_cur, kbuf, ubuf, len) < len) { kfree(kbuf); SYSCALL_RETURN(-1); diff --git a/src/kernel/vfs/mount.c b/src/kernel/vfs/mount.c index fa5d65b..bd47dd8 100644 --- a/src/kernel/vfs/mount.c +++ b/src/kernel/vfs/mount.c @@ -10,8 +10,8 @@ VfsMount *vfs_mount_seed(void) { } void vfs_root_register(const char *path, void (*accept)(VfsReq *)) { - VfsBackend *backend = kmalloc(sizeof *backend, "root bck"); - VfsMount *mount = kmalloc(sizeof *mount, "root mnt"); + VfsBackend *backend = kmalloc(sizeof *backend, TagKernelFs); + VfsMount *mount = kmalloc(sizeof *mount, TagMountRoot); *backend = (VfsBackend) { .is_user = false, .usehcnt = 1, diff --git a/src/kernel/vfs/procfs.c b/src/kernel/vfs/procfs.c index 86042cd..ab57628 100644 --- a/src/kernel/vfs/procfs.c +++ b/src/kernel/vfs/procfs.c @@ -73,7 +73,7 @@ openpath(const char *path, size_t len, Proc *root) return NULL; } - h = kmalloc(sizeof *h, "proc fd"); + h = kmalloc(sizeof *h, TagProcessHandle); h->gid = gid; h->type = type; return h; @@ -182,7 +182,7 @@ isdigit(int c) { VfsBackend * procfs_backend(Proc *proc) { - VfsBackend *be = kzalloc(sizeof(VfsBackend), "kern fs"); + VfsBackend *be = kzalloc(sizeof(VfsBackend), TagProcFs); *be = (VfsBackend) { .is_user = false, .usehcnt = 1, diff --git a/src/kernel/vfs/request.c b/src/kernel/vfs/request.c index 8e6f14a..92b904f 100644 --- a/src/kernel/vfs/request.c +++ b/src/kernel/vfs/request.c @@ -34,7 +34,7 @@ vfsreq_dispatchcopy(VfsReq tmpl) VfsBackend *backend; /* allocate memory for the request and move it there */ - req = kmalloc(sizeof *req, "VfsReq"); + req = kmalloc(sizeof *req, TagVfsReq); memcpy(req, &tmpl, sizeof *req); backend = req->backend; if (backend) { -- cgit v1.2.3