kernel: Don't create a new kernel stack on exec()
All checks were successful
continuous-integration/drone/push Build is passing

The old one was not getting freed, creating a memory leak every exec(),
which can get huge over time.
Plus, there was no need for a new stack.
And we couldn't just free the old one, since sys_execve() runs on the old stack...
This commit is contained in:
apio 2023-07-12 16:06:56 +02:00
parent f629e17ff4
commit 5f698b4774
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 8 additions and 14 deletions

View File

@ -6,24 +6,20 @@
static constexpr usize DEFAULT_USER_STACK_PAGES = 6;
static constexpr usize DEFAULT_USER_STACK_SIZE = DEFAULT_USER_STACK_PAGES * ARCH_PAGE_SIZE;
static constexpr u64 THREAD_STACK_BASE = 0x10000;
static Result<void> create_stacks(Stack& user_stack, Stack& kernel_stack, AddressSpace* space)
static Result<void> create_user_stack(Stack& user_stack, AddressSpace* space)
{
const u64 THREAD_STACK_BASE = 0x10000;
if (!TRY(space->test_and_alloc_region(THREAD_STACK_BASE, DEFAULT_USER_STACK_PAGES, true))) return err(ENOMEM);
TRY(MemoryManager::alloc_at_zeroed(THREAD_STACK_BASE, DEFAULT_USER_STACK_PAGES,
MMU::ReadWrite | MMU::NoExecute | MMU::User));
auto guard = make_scope_guard([&] { MemoryManager::unmap_owned(THREAD_STACK_BASE, 4); });
auto guard = make_scope_guard([] { MemoryManager::unmap_owned(THREAD_STACK_BASE, DEFAULT_USER_STACK_PAGES); });
const u64 kernel_stack_base = TRY(MemoryManager::alloc_for_kernel(4, MMU::ReadWrite | MMU::NoExecute));
if (!TRY(space->test_and_alloc_region(THREAD_STACK_BASE, DEFAULT_USER_STACK_PAGES, true))) return err(ENOMEM);
guard.deactivate();
user_stack = { THREAD_STACK_BASE, DEFAULT_USER_STACK_SIZE };
kernel_stack = { kernel_stack_base, 4 * ARCH_PAGE_SIZE };
return {};
}
@ -43,12 +39,10 @@ Result<OwnedPtr<ThreadImage>> ThreadImage::try_load_from_elf(SharedPtr<VFS::Inod
const ELFData data = TRY(ELFLoader::load(inode, address_space.ptr()));
Stack user_stack;
Stack kernel_stack;
TRY(create_stacks(user_stack, kernel_stack, address_space.ptr()));
TRY(create_user_stack(user_stack, address_space.ptr()));
guard.deactivate();
image->m_kernel_stack = kernel_stack;
image->m_user_stack = user_stack;
image->m_loaded_image_data = data;
image->m_address_space = move(address_space);
@ -111,7 +105,7 @@ void ThreadImage::apply(Thread* thread)
thread->set_ip(m_loaded_image_data.entry);
thread->kernel_stack = m_kernel_stack;
if (m_kernel_stack.bottom()) thread->kernel_stack = m_kernel_stack;
thread->stack = m_user_stack;
thread->set_sp(align_down<16>(m_sp));

View File

@ -19,6 +19,6 @@ struct Stack
}
private:
u64 m_base;
usize m_bytes;
u64 m_base { 0 };
usize m_bytes { 0 };
};