From d90d125dfe104e0031d0e08df290cfb9beb38c3b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 23 Oct 2018 15:41:55 +0200 Subject: [PATCH] Add an undertaker task that is responsible for destroying dead tasks. --- Kernel/Task.cpp | 21 ++++++++++++++------- Kernel/Task.h | 2 ++ Kernel/init.cpp | 27 ++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/Kernel/Task.cpp b/Kernel/Task.cpp index be8a57d08e8..ea979dc971a 100644 --- a/Kernel/Task.cpp +++ b/Kernel/Task.cpp @@ -17,6 +17,7 @@ Task* s_kernelTask; static pid_t next_pid; static InlineLinkedList* s_tasks; +static InlineLinkedList* s_deadTasks; static bool contextSwitch(Task*); @@ -52,6 +53,7 @@ void Task::initialize() current = nullptr; next_pid = 0; s_tasks = new InlineLinkedList; + s_deadTasks = new InlineLinkedList; s_kernelTask = new Task(0, "colonel", IPC::Handle::Any, Task::Ring0); redoKernelTaskTSS(); loadTaskRegister(s_kernelTask->selector()); @@ -213,7 +215,6 @@ Task::Task(String&& name, uid_t uid, gid_t gid) m_tss.esp = m_stackTop; // Set up a separate stack for Ring0. - // FIXME: Don't leak this stack. m_kernelStack = kmalloc(defaultStackSize); DWORD ring0StackTop = ((DWORD)m_kernelStack + defaultStackSize) & 0xffffff8; m_tss.ss0 = 0x10; @@ -336,14 +337,10 @@ Task::~Task() delete [] m_ldtEntries; m_ldtEntries = nullptr; - // FIXME: The task's kernel stack is currently leaked, because otherwise we GPF. - // This obviously needs figuring out. -#if 0 if (m_kernelStack) { kfree(m_kernelStack); m_kernelStack = nullptr; } -#endif } void Task::dumpRegions() @@ -375,7 +372,7 @@ void Task::sys$exit(int status) HANG; } - delete this; + s_deadTasks->append(this); switchNow(); } @@ -395,11 +392,21 @@ void Task::taskDidCrash(Task* crashedTask) HANG; } - delete crashedTask; + s_deadTasks->append(crashedTask); switchNow(); } +void Task::doHouseKeeping() +{ + Task* next = nullptr; + for (auto* deadTask = s_deadTasks->head(); deadTask; deadTask = next) { + next = deadTask->next(); + delete deadTask; + } + s_deadTasks->clear(); +} + void yield() { if (!current) { diff --git a/Kernel/Task.h b/Kernel/Task.h index d06024fd4fd..06c8dc8212d 100644 --- a/Kernel/Task.h +++ b/Kernel/Task.h @@ -67,6 +67,8 @@ public: FileHandle* fileHandleIfExists(int fd); + static void doHouseKeeping(); + bool acceptsMessageFrom(Task&); void block(Task::State); diff --git a/Kernel/init.cpp b/Kernel/init.cpp index 7200c199fad..46b5f2a7491 100644 --- a/Kernel/init.cpp +++ b/Kernel/init.cpp @@ -28,6 +28,7 @@ #include "ProcFileSystem.h" #define TEST_VFS +//#define STRESS_TEST_SPAWNING //#define TEST_ELF_LOADER //#define TEST_CRASHY_USER_PROCESSES @@ -95,6 +96,15 @@ void banner() kprintf("\n"); } +static void undertaker_main() NORETURN; +static void undertaker_main() +{ + for (;;) { + Task::doHouseKeeping(); + sleep(10); + } +} + static void init_stage2() NORETURN; static void init_stage2() { @@ -105,7 +115,7 @@ static void init_stage2() auto keyboard = make(); extern void panel_main(); - new Task(panel_main, "panel", IPC::Handle::PanelTask, Task::Ring0); + //new Task(panel_main, "panel", IPC::Handle::PanelTask, Task::Ring0); //new Task(led_disco, "led-disco", IPC::Handle::Any, Task::Ring0); Disk::initialize(); @@ -171,6 +181,19 @@ static void init_stage2() } #endif +#ifdef STRESS_TEST_SPAWNING + dword lastAlloc = sum_alloc; + + for (unsigned i = 0; i < 100; ++i) { + auto* shTask = Task::create("/bin/id", (uid_t)100, (gid_t)100); + kprintf("malloc stats: alloc:%u free:%u\n", sum_alloc, sum_free); + kprintf("sizeof(Task):%u\n", sizeof(Task)); + kprintf("delta:%u\n",sum_alloc - lastAlloc); + lastAlloc = sum_alloc; + sleep(600); + } +#endif + auto* shTask = Task::create("/bin/sh", (uid_t)100, (gid_t)100); //new Task(motd_main, "motd", IPC::Handle::MotdTask, Task::Ring0); @@ -221,6 +244,8 @@ void init() Task::initialize(); + new Task(undertaker_main, "undertaker", IPC::Handle::UserTask, Task::Ring0); + auto* init2 = new Task(init_stage2, "init", IPC::Handle::InitTask, Task::Ring0); scheduleNewTask();