From df34f7b90b0dc8a3319d67d2a75551cba225aaf1 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 16 Jan 2022 13:10:05 +0100 Subject: [PATCH] Kernel: Use an IntrusiveRedBlackTree for kernel regions We were already using a non-intrusive RedBlackTree, and since the kernel regions tree is non-owning, this is a trivial conversion that makes a bunch of the tree operations infallible (by being allocation-free.) :^) --- Kernel/Memory/MemoryManager.cpp | 13 ++++++------- Kernel/Memory/MemoryManager.h | 5 +++-- Kernel/Memory/Region.h | 7 ++++--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Kernel/Memory/MemoryManager.cpp b/Kernel/Memory/MemoryManager.cpp index 75ad1c98007..d914c723d58 100644 --- a/Kernel/Memory/MemoryManager.cpp +++ b/Kernel/Memory/MemoryManager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2021, Andreas Kling + * Copyright (c) 2018-2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -645,10 +645,10 @@ UNMAP_AFTER_INIT void MemoryManager::initialize(u32 cpu) Region* MemoryManager::kernel_region_from_vaddr(VirtualAddress vaddr) { SpinlockLocker lock(s_mm_lock); - auto* region_ptr = MM.m_kernel_regions.find_largest_not_above(vaddr.get()); - if (!region_ptr) + auto* region = MM.m_kernel_regions.find_largest_not_above(vaddr.get()); + if (!region || !region->contains(vaddr)) return nullptr; - return (*region_ptr)->contains(vaddr) ? *region_ptr : nullptr; + return region; } Region* MemoryManager::find_user_region_from_vaddr_no_lock(AddressSpace& space, VirtualAddress vaddr) @@ -1111,7 +1111,7 @@ void MemoryManager::register_kernel_region(Region& region) { VERIFY(region.is_kernel()); SpinlockLocker lock(s_mm_lock); - m_kernel_regions.insert(region.vaddr().get(), ®ion); + m_kernel_regions.insert(region.vaddr().get(), region); } void MemoryManager::unregister_kernel_region(Region& region) @@ -1132,8 +1132,7 @@ void MemoryManager::dump_kernel_regions() dbgln("BEGIN{} END{} SIZE{} ACCESS NAME", addr_padding, addr_padding, addr_padding); SpinlockLocker lock(s_mm_lock); - for (auto const* region_ptr : m_kernel_regions) { - auto const& region = *region_ptr; + for (auto const& region : m_kernel_regions) { dbgln("{:p} -- {:p} {:p} {:c}{:c}{:c}{:c}{:c}{:c} {}", region.vaddr().get(), region.vaddr().offset(region.size() - 1).get(), diff --git a/Kernel/Memory/MemoryManager.h b/Kernel/Memory/MemoryManager.h index 4a5e5e5137f..693709a4ad3 100644 --- a/Kernel/Memory/MemoryManager.h +++ b/Kernel/Memory/MemoryManager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -293,7 +294,7 @@ private: PhysicalPageEntry* m_physical_page_entries { nullptr }; size_t m_physical_page_entries_count { 0 }; - RedBlackTree m_kernel_regions; + IntrusiveRedBlackTree<&Region::m_tree_node> m_kernel_regions; Vector m_used_memory_ranges; Vector m_physical_memory_ranges; diff --git a/Kernel/Memory/Region.h b/Kernel/Memory/Region.h index b66300c241a..3c447296e72 100644 --- a/Kernel/Memory/Region.h +++ b/Kernel/Memory/Region.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -219,11 +220,11 @@ private: bool m_stack : 1 { false }; bool m_mmap : 1 { false }; bool m_syscall_region : 1 { false }; - IntrusiveListNode m_memory_manager_list_node; + + IntrusiveRedBlackTreeNode> m_tree_node; IntrusiveListNode m_vmobject_list_node; public: - using ListInMemoryManager = IntrusiveList<&Region::m_memory_manager_list_node>; using ListInVMObject = IntrusiveList<&Region::m_vmobject_list_node>; };