Skip to content

Commit

Permalink
Kernel: Add locks around RangeAllocator
Browse files Browse the repository at this point in the history
We need to keep multiple processors from changing it at the same time.
  • Loading branch information
tomuta authored and awesomekling committed Nov 11, 2020
1 parent 66f46d0 commit 2b25a89
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Kernel/VM/RangeAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ void RangeAllocator::initialize_with_range(VirtualAddress base, size_t size)
m_total_range = { base, size };
m_available_ranges.append({ base, size });
#ifdef VRA_DEBUG
ScopedSpinLock lock(m_lock);
dump();
#endif
}

void RangeAllocator::initialize_from_parent(const RangeAllocator& parent_allocator)
{
ScopedSpinLock lock(parent_allocator.m_lock);
m_total_range = parent_allocator.m_total_range;
m_available_ranges = parent_allocator.m_available_ranges;
}
Expand All @@ -60,6 +62,7 @@ RangeAllocator::~RangeAllocator()

void RangeAllocator::dump() const
{
ASSERT(m_lock.is_locked());
dbg() << "RangeAllocator{" << this << "}";
for (auto& range : m_available_ranges) {
dbg() << " " << String::format("%x", range.base().get()) << " -> " << String::format("%x", range.end().get() - 1);
Expand All @@ -85,6 +88,7 @@ Vector<Range, 2> Range::carve(const Range& taken)

void RangeAllocator::carve_at_index(int index, const Range& range)
{
ASSERT(m_lock.is_locked());
auto remaining_parts = m_available_ranges[index].carve(range);
ASSERT(remaining_parts.size() >= 1);
m_available_ranges[index] = remaining_parts[0];
Expand All @@ -106,6 +110,7 @@ Range RangeAllocator::allocate_anywhere(size_t size, size_t alignment)
size_t offset_from_effective_base = 0;
#endif

ScopedSpinLock lock(m_lock);
for (size_t i = 0; i < m_available_ranges.size(); ++i) {
auto& available_range = m_available_ranges[i];
// FIXME: This check is probably excluding some valid candidates when using a large alignment.
Expand Down Expand Up @@ -140,6 +145,7 @@ Range RangeAllocator::allocate_specific(VirtualAddress base, size_t size)
return {};

Range allocated_range(base, size);
ScopedSpinLock lock(m_lock);
for (size_t i = 0; i < m_available_ranges.size(); ++i) {
auto& available_range = m_available_ranges[i];
if (!available_range.contains(base, size))
Expand All @@ -161,6 +167,7 @@ Range RangeAllocator::allocate_specific(VirtualAddress base, size_t size)

void RangeAllocator::deallocate(Range range)
{
ScopedSpinLock lock(m_lock);
ASSERT(m_total_range.contains(range));
ASSERT(range.size());
ASSERT(range.base() < range.end());
Expand Down
3 changes: 3 additions & 0 deletions Kernel/VM/RangeAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <AK/String.h>
#include <AK/Traits.h>
#include <AK/Vector.h>
#include <Kernel/SpinLock.h>
#include <Kernel/VirtualAddress.h>

namespace Kernel {
Expand Down Expand Up @@ -92,6 +93,7 @@ class RangeAllocator {

bool contains(const Range& range) const
{
ScopedSpinLock lock(m_lock);
return m_total_range.contains(range);
}

Expand All @@ -100,6 +102,7 @@ class RangeAllocator {

Vector<Range> m_available_ranges;
Range m_total_range;
mutable SpinLock<u8> m_lock;
};

inline const LogStream& operator<<(const LogStream& stream, const Range& value)
Expand Down

0 comments on commit 2b25a89

Please sign in to comment.