Kernel: Make ContiguousVMObject factory API OOM safe

This commit is contained in:
Brian Gianforcaro 2021-05-28 03:56:42 -07:00 committed by Andreas Kling
parent cb45b2c001
commit 864b1a65e3
Notes: sideshowbarker 2024-07-18 17:15:26 +09:00
3 changed files with 17 additions and 11 deletions

View File

@ -10,15 +10,17 @@
namespace Kernel {
NonnullRefPtr<ContiguousVMObject> ContiguousVMObject::create_with_size(size_t size, size_t physical_alignment)
{
return adopt_ref(*new ContiguousVMObject(size, physical_alignment));
}
ContiguousVMObject::ContiguousVMObject(size_t size, size_t physical_alignment)
: VMObject(size)
RefPtr<ContiguousVMObject> ContiguousVMObject::create_with_size(size_t size, size_t physical_alignment)
{
auto contiguous_physical_pages = MM.allocate_contiguous_supervisor_physical_pages(size, physical_alignment);
if (contiguous_physical_pages.is_empty())
return {};
return adopt_ref_if_nonnull(new ContiguousVMObject(size, contiguous_physical_pages));
}
ContiguousVMObject::ContiguousVMObject(size_t size, NonnullRefPtrVector<PhysicalPage>& contiguous_physical_pages)
: VMObject(size)
{
for (size_t i = 0; i < page_count(); i++) {
physical_pages()[i] = contiguous_physical_pages[i];
dbgln_if(CONTIGUOUS_VMOBJECT_DEBUG, "Contiguous page[{}]: {}", i, physical_pages()[i]->paddr());

View File

@ -7,18 +7,18 @@
#pragma once
#include <Kernel/PhysicalAddress.h>
#include <Kernel/VM/MemoryManager.h>
#include <Kernel/VM/VMObject.h>
namespace Kernel {
class ContiguousVMObject final : public VMObject {
public:
virtual ~ContiguousVMObject() override;
static NonnullRefPtr<ContiguousVMObject> create_with_size(size_t, size_t physical_alignment = PAGE_SIZE);
static RefPtr<ContiguousVMObject> create_with_size(size_t, size_t physical_alignment = PAGE_SIZE);
private:
explicit ContiguousVMObject(size_t, size_t physical_alignment);
explicit ContiguousVMObject(size_t, NonnullRefPtrVector<PhysicalPage>&);
explicit ContiguousVMObject(const ContiguousVMObject&);
virtual const char* class_name() const override { return "ContiguousVMObject"; }

View File

@ -464,7 +464,11 @@ OwnPtr<Region> MemoryManager::allocate_contiguous_kernel_region(size_t size, Str
if (!range.has_value())
return {};
auto vmobject = ContiguousVMObject::create_with_size(size, physical_alignment);
return allocate_kernel_region_with_vmobject(range.value(), vmobject, name, access, cacheable);
if (!vmobject) {
kernel_page_directory().range_allocator().deallocate(range.value());
return {};
}
return allocate_kernel_region_with_vmobject(range.value(), *vmobject, name, access, cacheable);
}
OwnPtr<Region> MemoryManager::allocate_kernel_region(size_t size, StringView name, Region::Access access, AllocationStrategy strategy, Region::Cacheable cacheable)