diff options
author | Brian Gianforcaro <bgianf@serenityos.org> | 2021-07-01 13:32:59 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-07-01 22:43:48 +0200 |
commit | 60efe18a3162c90a519d845e83a2f78181b9b3ef (patch) | |
tree | 828527967324e5f940dc46281aa75775713d7f1f /Kernel/Thread.cpp | |
parent | 4fb962a0b9a1a9d96f13d78eab1c626d657b77c5 (diff) | |
download | serenity-60efe18a3162c90a519d845e83a2f78181b9b3ef.zip |
Kernel: Handle OOM when allocating Thread FPUState
Move FPUState allocation to Thread::try_create so that allocation
failure can be observed properly by the caller.
Diffstat (limited to 'Kernel/Thread.cpp')
-rw-r--r-- | Kernel/Thread.cpp | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index 3eca547e4b..7794a2afdd 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -39,6 +39,12 @@ UNMAP_AFTER_INIT void Thread::initialize() KResultOr<NonnullRefPtr<Thread>> Thread::try_create(NonnullRefPtr<Process> process) { + // FIXME: Once we have aligned + nothrow operator new, we can avoid the manual kfree. + FPUState* fpu_state = (FPUState*)kmalloc_aligned<16>(sizeof(FPUState)); + if (!fpu_state) + return ENOMEM; + ArmedScopeGuard fpu_guard([fpu_state]() { kfree_aligned(fpu_state); }); + auto kernel_stack_region = MM.allocate_kernel_region(default_kernel_stack_size, {}, Region::Access::Read | Region::Access::Write, AllocationStrategy::AllocateNow); if (!kernel_stack_region) return ENOMEM; @@ -48,16 +54,18 @@ KResultOr<NonnullRefPtr<Thread>> Thread::try_create(NonnullRefPtr<Process> proce if (!block_timer) return ENOMEM; - auto thread = adopt_ref_if_nonnull(new (nothrow) Thread(move(process), kernel_stack_region.release_nonnull(), block_timer.release_nonnull())); + auto thread = adopt_ref_if_nonnull(new (nothrow) Thread(move(process), kernel_stack_region.release_nonnull(), block_timer.release_nonnull(), fpu_state)); if (!thread) return ENOMEM; + fpu_guard.disarm(); return thread.release_nonnull(); } -Thread::Thread(NonnullRefPtr<Process> process, NonnullOwnPtr<Region> kernel_stack_region, NonnullRefPtr<Timer> block_timer) +Thread::Thread(NonnullRefPtr<Process> process, NonnullOwnPtr<Region> kernel_stack_region, NonnullRefPtr<Timer> block_timer, FPUState* fpu_state) : m_process(move(process)) , m_kernel_stack_region(move(kernel_stack_region)) + , m_fpu_state(fpu_state) , m_name(m_process->name()) , m_block_timer(block_timer) , m_global_procfs_inode_index(ProcFSComponentsRegistrar::the().allocate_inode_index()) @@ -84,7 +92,6 @@ Thread::Thread(NonnullRefPtr<Process> process, NonnullOwnPtr<Region> kernel_stac if constexpr (THREAD_DEBUG) dbgln("Created new thread {}({}:{})", m_process->name(), m_process->pid().value(), m_tid.value()); - m_fpu_state = (FPUState*)kmalloc_aligned<16>(sizeof(FPUState)); reset_fpu_state(); #if ARCH(I386) |