From ec0cb5fd54af2e0eab1e53b1abca77dc8ad39be7 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 29 Feb 2024 11:19:10 +0200 Subject: [PATCH] Kernel: Remove CriticalScopes from memory handing code --- kernel/arch/x86_64/PageTable.cpp | 8 +++++--- kernel/include/kernel/Memory/PageTable.h | 3 +-- kernel/kernel/Memory/FileBackedRegion.cpp | 10 +++------- kernel/kernel/Memory/MemoryBackedRegion.cpp | 1 - kernel/kernel/Memory/VirtualRange.cpp | 1 - kernel/kernel/Memory/kmalloc.cpp | 16 ++++++++++++---- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/kernel/arch/x86_64/PageTable.cpp b/kernel/arch/x86_64/PageTable.cpp index e4ddbbd4de..9647c46882 100644 --- a/kernel/arch/x86_64/PageTable.cpp +++ b/kernel/arch/x86_64/PageTable.cpp @@ -17,7 +17,7 @@ extern uint8_t g_userspace_end[]; namespace Kernel { - SpinLock PageTable::s_fast_page_lock; + RecursiveSpinLock PageTable::s_fast_page_lock; static PageTable* s_kernel = nullptr; static PageTable* s_current = nullptr; @@ -209,7 +209,8 @@ namespace Kernel { ASSERT(s_kernel); ASSERT_NEQ(paddr, 0); - ASSERT(!interrupts_enabled()); + + SpinLockGuard _(s_fast_page_lock); constexpr vaddr_t uc_vaddr = uncanonicalize(fast_page()); constexpr uint64_t pml4e = (uc_vaddr >> 39) & 0x1FF; @@ -231,7 +232,8 @@ namespace Kernel void PageTable::unmap_fast_page() { ASSERT(s_kernel); - ASSERT(!interrupts_enabled()); + + SpinLockGuard _(s_fast_page_lock); constexpr vaddr_t uc_vaddr = uncanonicalize(fast_page()); constexpr uint64_t pml4e = (uc_vaddr >> 39) & 0x1FF; diff --git a/kernel/include/kernel/Memory/PageTable.h b/kernel/include/kernel/Memory/PageTable.h index e6190fb5f9..e6d7dc6507 100644 --- a/kernel/include/kernel/Memory/PageTable.h +++ b/kernel/include/kernel/Memory/PageTable.h @@ -2,7 +2,6 @@ #include #include -#include #include #include @@ -130,7 +129,7 @@ namespace Kernel private: paddr_t m_highest_paging_struct { 0 }; mutable RecursiveSpinLock m_lock; - static SpinLock s_fast_page_lock; + static RecursiveSpinLock s_fast_page_lock; }; static constexpr size_t range_page_count(vaddr_t start, size_t bytes) diff --git a/kernel/kernel/Memory/FileBackedRegion.cpp b/kernel/kernel/Memory/FileBackedRegion.cpp index 8156265d2c..09c527db7f 100644 --- a/kernel/kernel/Memory/FileBackedRegion.cpp +++ b/kernel/kernel/Memory/FileBackedRegion.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -83,12 +82,9 @@ namespace Kernel if (pages[page_index] == 0) return; - { - CriticalScope _; - PageTable::with_fast_page(pages[page_index], [&] { - memcpy(page_buffer, PageTable::fast_page_as_ptr(), PAGE_SIZE); - }); - } + PageTable::with_fast_page(pages[page_index], [&] { + memcpy(page_buffer, PageTable::fast_page_as_ptr(), PAGE_SIZE); + }); if (auto ret = inode->write(page_index * PAGE_SIZE, BAN::ConstByteSpan::from(page_buffer)); ret.is_error()) dwarnln("{}", ret.error()); diff --git a/kernel/kernel/Memory/MemoryBackedRegion.cpp b/kernel/kernel/Memory/MemoryBackedRegion.cpp index 549b79fbe2..eaf1c6df08 100644 --- a/kernel/kernel/Memory/MemoryBackedRegion.cpp +++ b/kernel/kernel/Memory/MemoryBackedRegion.cpp @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/kernel/kernel/Memory/VirtualRange.cpp b/kernel/kernel/Memory/VirtualRange.cpp index b70bb93345..3186ece8ab 100644 --- a/kernel/kernel/Memory/VirtualRange.cpp +++ b/kernel/kernel/Memory/VirtualRange.cpp @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/kernel/kernel/Memory/kmalloc.cpp b/kernel/kernel/Memory/kmalloc.cpp index 4f1c5b0a1f..62d11f2178 100644 --- a/kernel/kernel/Memory/kmalloc.cpp +++ b/kernel/kernel/Memory/kmalloc.cpp @@ -1,5 +1,4 @@ #include -#include #include #include @@ -81,6 +80,8 @@ struct kmalloc_info }; static kmalloc_info s_kmalloc_info; +static Kernel::SpinLock s_kmalloc_lock; + template struct kmalloc_fixed_node { @@ -144,6 +145,8 @@ void kmalloc_initialize() void kmalloc_dump_info() { + Kernel::SpinLockGuard _(s_kmalloc_lock); + kprintln("kmalloc: 0x{8H}->0x{8H}", s_kmalloc_info.base, s_kmalloc_info.end); kprintln(" used: 0x{8H}", s_kmalloc_info.used); kprintln(" free: 0x{8H}", s_kmalloc_info.free); @@ -155,6 +158,7 @@ void kmalloc_dump_info() static bool is_corrupted() { + Kernel::SpinLockGuard _(s_kmalloc_lock); auto& info = s_kmalloc_info; auto* temp = info.first(); for (; temp->end() <= info.end; temp = temp->after()); @@ -163,6 +167,8 @@ static bool is_corrupted() [[maybe_unused]] static void debug_dump() { + Kernel::SpinLockGuard _(s_kmalloc_lock); + auto& info = s_kmalloc_info; uint32_t used = 0; @@ -181,6 +187,8 @@ static bool is_corrupted() static void* kmalloc_fixed() { + Kernel::SpinLockGuard _(s_kmalloc_lock); + auto& info = s_kmalloc_fixed_info; if (!info.free_list_head) @@ -223,6 +231,8 @@ static void* kmalloc_impl(size_t size, size_t align) ASSERT(align % s_kmalloc_min_align == 0); ASSERT(size % s_kmalloc_min_align == 0); + Kernel::SpinLockGuard _(s_kmalloc_lock); + auto& info = s_kmalloc_info; for (auto* node = info.first(); node->end() <= info.end; node = node->after()) @@ -304,8 +314,6 @@ void* kmalloc(size_t size, size_t align, bool force_identity_map) align = s_kmalloc_min_align; ASSERT(align <= PAGE_SIZE); - Kernel::CriticalScope critical; - if (size == 0 || size >= info.size) goto no_memory; @@ -338,7 +346,7 @@ void kfree(void* address) uintptr_t address_uint = (uintptr_t)address; ASSERT(address_uint % s_kmalloc_min_align == 0); - Kernel::CriticalScope critical; + Kernel::SpinLockGuard _(s_kmalloc_lock); if (s_kmalloc_fixed_info.base <= address_uint && address_uint < s_kmalloc_fixed_info.end) {