2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-03-11 17:26:02 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <AK/Assertions.h>
|
2019-04-06 15:29:29 +03:00
|
|
|
#include <AK/Types.h>
|
2022-04-03 02:06:34 +03:00
|
|
|
#include <Kernel/Arch/PageDirectory.h>
|
2021-03-11 17:26:02 +03:00
|
|
|
#include <Kernel/Debug.h>
|
2020-08-30 01:41:30 +03:00
|
|
|
#include <Kernel/Heap/Heap.h>
|
2020-02-09 17:47:15 +03:00
|
|
|
#include <Kernel/Heap/kmalloc.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/KSyms.h>
|
2023-02-24 21:10:59 +03:00
|
|
|
#include <Kernel/Library/Panic.h>
|
|
|
|
#include <Kernel/Library/StdLib.h>
|
2021-08-22 02:37:17 +03:00
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
2021-08-06 11:45:34 +03:00
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
2021-06-22 18:40:16 +03:00
|
|
|
#include <Kernel/Sections.h>
|
2023-12-29 03:36:39 +03:00
|
|
|
#include <Kernel/Security/AddressSanitizer.h>
|
2023-02-24 21:25:52 +03:00
|
|
|
#include <Kernel/Tasks/PerformanceManager.h>
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2023-11-03 01:21:09 +03:00
|
|
|
#if ARCH(X86_64) || ARCH(AARCH64) || ARCH(RISCV64)
|
2021-10-26 11:37:36 +03:00
|
|
|
static constexpr size_t CHUNK_SIZE = 64;
|
2022-07-22 21:48:24 +03:00
|
|
|
#else
|
|
|
|
# error Unknown architecture
|
2021-10-26 11:37:36 +03:00
|
|
|
#endif
|
2022-03-08 00:38:05 +03:00
|
|
|
static_assert(is_power_of_two(CHUNK_SIZE));
|
2021-10-26 11:37:36 +03:00
|
|
|
|
2021-12-28 21:19:52 +03:00
|
|
|
static constexpr size_t INITIAL_KMALLOC_MEMORY_SIZE = 2 * MiB;
|
2022-12-07 07:02:59 +03:00
|
|
|
static constexpr size_t KMALLOC_DEFAULT_ALIGNMENT = 16;
|
2021-12-28 21:19:52 +03:00
|
|
|
|
|
|
|
// Treat the heap as logically separate from .bss
|
|
|
|
__attribute__((section(".heap"))) static u8 initial_kmalloc_memory[INITIAL_KMALLOC_MEMORY_SIZE];
|
2020-08-22 06:55:08 +03:00
|
|
|
|
AK+Kernel: Make fallible allocations compiler-agnostic
In standard C++, operators `new` and `new[]` are guaranteed to return a
valid (non-null) pointer and throw an exception if the allocation
couldn't be performed. Based on this, compilers did not check the
returned pointer before attempting to use them for object construction.
To avoid this, the allocator operators were changed to be `noexcept` in
PR #7026, which made GCC emit the desired null checks. Unfortunately,
this is a non-standard feature which meant that Clang would not accept
these function definitions, as it did not match its expected
declaration.
To make compiling using Clang possible, the special "nothrow" versions
of `new` are implemented in this commit. These take a tag type of
`std::nothrow_t` (used for disambiguating from placement new/etc.), and
are allowed by the standard to return null. There is a global variable,
`std::nothrow`, declared with this type, which is also exported into the
global namespace.
To perform fallible allocations, the following syntax should be used:
```cpp
auto ptr = new (nothrow) T;
```
As we don't support exceptions in the kernel, the only way of uphold the
"throwing" new's guarantee is to abort if the allocation couldn't be
performed. Once we have proper OOM handling in the kernel, this should
only be used for critical allocations, where we wouldn't be able to
recover from allocation failures anyway.
2021-06-20 10:39:20 +03:00
|
|
|
namespace std {
|
|
|
|
const nothrow_t nothrow;
|
|
|
|
}
|
|
|
|
|
2022-08-18 22:46:28 +03:00
|
|
|
// FIXME: Figure out whether this can be MemoryManager.
|
2022-11-09 13:39:58 +03:00
|
|
|
static RecursiveSpinlock<LockRank::None> s_lock {}; // needs to be recursive because of dump_backtrace()
|
2020-11-01 23:11:31 +03:00
|
|
|
|
2021-12-25 19:23:18 +03:00
|
|
|
struct KmallocSubheap {
|
|
|
|
KmallocSubheap(u8* base, size_t size)
|
|
|
|
: allocator(base, size)
|
|
|
|
{
|
|
|
|
}
|
2020-12-31 03:00:49 +03:00
|
|
|
|
2021-12-25 19:23:18 +03:00
|
|
|
IntrusiveListNode<KmallocSubheap> list_node;
|
2021-12-28 03:32:38 +03:00
|
|
|
using List = IntrusiveList<&KmallocSubheap::list_node>;
|
2021-12-25 19:23:18 +03:00
|
|
|
Heap<CHUNK_SIZE, KMALLOC_SCRUB_BYTE, KFREE_SCRUB_BYTE> allocator;
|
|
|
|
};
|
2020-08-30 01:41:30 +03:00
|
|
|
|
2021-12-26 20:38:22 +03:00
|
|
|
class KmallocSlabBlock {
|
|
|
|
public:
|
|
|
|
static constexpr size_t block_size = 64 * KiB;
|
|
|
|
static constexpr FlatPtr block_mask = ~(block_size - 1);
|
|
|
|
|
|
|
|
KmallocSlabBlock(size_t slab_size)
|
2022-01-23 19:28:32 +03:00
|
|
|
: m_slab_size(slab_size)
|
|
|
|
, m_slab_count((block_size - sizeof(KmallocSlabBlock)) / slab_size)
|
2021-12-26 20:38:22 +03:00
|
|
|
{
|
2022-01-23 19:28:32 +03:00
|
|
|
for (size_t i = 0; i < m_slab_count; ++i) {
|
2021-12-26 20:38:22 +03:00
|
|
|
auto* freelist_entry = (FreelistEntry*)(void*)(&m_data[i * slab_size]);
|
|
|
|
freelist_entry->next = m_freelist;
|
|
|
|
m_freelist = freelist_entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-29 03:36:39 +03:00
|
|
|
void* allocate([[maybe_unused]] size_t requested_size)
|
2021-12-26 20:38:22 +03:00
|
|
|
{
|
|
|
|
VERIFY(m_freelist);
|
2022-01-23 19:28:32 +03:00
|
|
|
++m_allocated_slabs;
|
2023-12-29 03:36:39 +03:00
|
|
|
#ifdef HAS_ADDRESS_SANITIZER
|
|
|
|
AddressSanitizer::fill_shadow((FlatPtr)m_freelist, sizeof(FreelistEntry::next), Kernel::AddressSanitizer::ShadowType::Unpoisoned8Bytes);
|
|
|
|
#endif
|
|
|
|
auto* ptr = exchange(m_freelist, m_freelist->next);
|
|
|
|
#ifdef HAS_ADDRESS_SANITIZER
|
|
|
|
AddressSanitizer::mark_region((FlatPtr)ptr, requested_size, m_slab_size, AddressSanitizer::ShadowType::Malloc);
|
|
|
|
#endif
|
|
|
|
return ptr;
|
2021-12-26 20:38:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void deallocate(void* ptr)
|
|
|
|
{
|
|
|
|
VERIFY(ptr >= &m_data && ptr < ((u8*)this + block_size));
|
2022-01-23 19:28:32 +03:00
|
|
|
--m_allocated_slabs;
|
2021-12-26 20:38:22 +03:00
|
|
|
auto* freelist_entry = (FreelistEntry*)ptr;
|
2023-12-29 03:36:39 +03:00
|
|
|
#ifdef HAS_ADDRESS_SANITIZER
|
|
|
|
AddressSanitizer::fill_shadow((FlatPtr)freelist_entry, sizeof(FreelistEntry::next), Kernel::AddressSanitizer::ShadowType::Unpoisoned8Bytes);
|
|
|
|
#endif
|
2021-12-26 20:38:22 +03:00
|
|
|
freelist_entry->next = m_freelist;
|
2023-12-29 03:36:39 +03:00
|
|
|
#ifdef HAS_ADDRESS_SANITIZER
|
|
|
|
AddressSanitizer::fill_shadow((FlatPtr)freelist_entry, m_slab_size, AddressSanitizer::ShadowType::Free);
|
|
|
|
#endif
|
2021-12-26 20:38:22 +03:00
|
|
|
m_freelist = freelist_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_full() const
|
|
|
|
{
|
|
|
|
return m_freelist == nullptr;
|
|
|
|
}
|
|
|
|
|
2022-01-23 19:28:32 +03:00
|
|
|
size_t allocated_bytes() const
|
|
|
|
{
|
|
|
|
return m_allocated_slabs * m_slab_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t free_bytes() const
|
|
|
|
{
|
|
|
|
return (m_slab_count - m_allocated_slabs) * m_slab_size;
|
|
|
|
}
|
|
|
|
|
2021-12-26 20:38:22 +03:00
|
|
|
IntrusiveListNode<KmallocSlabBlock> list_node;
|
2021-12-28 03:32:38 +03:00
|
|
|
using List = IntrusiveList<&KmallocSlabBlock::list_node>;
|
2021-12-26 20:38:22 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct FreelistEntry {
|
|
|
|
FreelistEntry* next;
|
|
|
|
};
|
|
|
|
|
|
|
|
FreelistEntry* m_freelist { nullptr };
|
|
|
|
|
2022-01-23 19:28:32 +03:00
|
|
|
size_t m_slab_size { 0 };
|
|
|
|
size_t m_slab_count { 0 };
|
|
|
|
size_t m_allocated_slabs { 0 };
|
|
|
|
|
2021-12-26 20:38:22 +03:00
|
|
|
[[gnu::aligned(16)]] u8 m_data[];
|
|
|
|
};
|
|
|
|
|
|
|
|
class KmallocSlabheap {
|
|
|
|
public:
|
|
|
|
KmallocSlabheap(size_t slab_size)
|
|
|
|
: m_slab_size(slab_size)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t slab_size() const { return m_slab_size; }
|
|
|
|
|
2023-12-29 03:36:39 +03:00
|
|
|
void* allocate(size_t requested_size, [[maybe_unused]] CallerWillInitializeMemory caller_will_initialize_memory)
|
2021-12-26 20:38:22 +03:00
|
|
|
{
|
|
|
|
if (m_usable_blocks.is_empty()) {
|
2021-12-26 20:47:20 +03:00
|
|
|
// FIXME: This allocation wastes `block_size` bytes due to the implementation of kmalloc_aligned().
|
|
|
|
// Handle this with a custom VM+page allocator instead of using kmalloc_aligned().
|
2021-12-26 20:38:22 +03:00
|
|
|
auto* slot = kmalloc_aligned(KmallocSlabBlock::block_size, KmallocSlabBlock::block_size);
|
|
|
|
if (!slot) {
|
2022-12-07 02:38:12 +03:00
|
|
|
dbgln_if(KMALLOC_DEBUG, "OOM while growing slabheap ({})", m_slab_size);
|
|
|
|
return nullptr;
|
2021-12-26 20:38:22 +03:00
|
|
|
}
|
|
|
|
auto* block = new (slot) KmallocSlabBlock(m_slab_size);
|
|
|
|
m_usable_blocks.append(*block);
|
|
|
|
}
|
|
|
|
auto* block = m_usable_blocks.first();
|
2023-12-29 03:36:39 +03:00
|
|
|
auto* ptr = block->allocate(requested_size);
|
2021-12-26 20:38:22 +03:00
|
|
|
if (block->is_full())
|
|
|
|
m_full_blocks.append(*block);
|
2021-12-26 20:53:04 +03:00
|
|
|
|
2023-12-29 03:36:39 +03:00
|
|
|
#ifndef HAS_ADDRESS_SANITIZER
|
2022-12-04 21:04:39 +03:00
|
|
|
if (caller_will_initialize_memory == CallerWillInitializeMemory::No) {
|
|
|
|
memset(ptr, KMALLOC_SCRUB_BYTE, m_slab_size);
|
|
|
|
}
|
2023-12-29 03:36:39 +03:00
|
|
|
#endif
|
2021-12-26 20:38:22 +03:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void deallocate(void* ptr)
|
|
|
|
{
|
2023-12-29 03:36:39 +03:00
|
|
|
#ifndef HAS_ADDRESS_SANITIZER
|
2021-12-26 20:53:04 +03:00
|
|
|
memset(ptr, KFREE_SCRUB_BYTE, m_slab_size);
|
2023-12-29 03:36:39 +03:00
|
|
|
#endif
|
2021-12-26 20:53:04 +03:00
|
|
|
|
2021-12-26 20:38:22 +03:00
|
|
|
auto* block = (KmallocSlabBlock*)((FlatPtr)ptr & KmallocSlabBlock::block_mask);
|
|
|
|
bool block_was_full = block->is_full();
|
|
|
|
block->deallocate(ptr);
|
|
|
|
if (block_was_full)
|
|
|
|
m_usable_blocks.append(*block);
|
|
|
|
}
|
|
|
|
|
2022-01-23 19:28:32 +03:00
|
|
|
size_t allocated_bytes() const
|
|
|
|
{
|
|
|
|
size_t total = m_full_blocks.size_slow() * KmallocSlabBlock::block_size;
|
|
|
|
for (auto const& slab_block : m_usable_blocks)
|
|
|
|
total += slab_block.allocated_bytes();
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t free_bytes() const
|
|
|
|
{
|
|
|
|
size_t total = 0;
|
|
|
|
for (auto const& slab_block : m_usable_blocks)
|
|
|
|
total += slab_block.free_bytes();
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2022-02-14 15:40:14 +03:00
|
|
|
bool try_purge()
|
|
|
|
{
|
|
|
|
bool did_purge = false;
|
|
|
|
|
|
|
|
// Note: We cannot remove children from the list when using a structured loop,
|
|
|
|
// Because we need to advance the iterator before we delete the underlying
|
|
|
|
// value, so we have to iterate manually
|
|
|
|
|
|
|
|
auto block = m_usable_blocks.begin();
|
|
|
|
while (block != m_usable_blocks.end()) {
|
|
|
|
if (block->allocated_bytes() != 0) {
|
|
|
|
++block;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto& block_to_remove = *block;
|
|
|
|
++block;
|
|
|
|
block_to_remove.list_node.remove();
|
|
|
|
block_to_remove.~KmallocSlabBlock();
|
2022-12-07 07:02:59 +03:00
|
|
|
kfree_sized(&block_to_remove, KmallocSlabBlock::block_size);
|
2022-02-14 15:40:14 +03:00
|
|
|
|
|
|
|
did_purge = true;
|
|
|
|
}
|
|
|
|
return did_purge;
|
|
|
|
}
|
|
|
|
|
2021-12-26 20:38:22 +03:00
|
|
|
private:
|
|
|
|
size_t m_slab_size { 0 };
|
|
|
|
|
2021-12-28 03:32:38 +03:00
|
|
|
KmallocSlabBlock::List m_usable_blocks;
|
|
|
|
KmallocSlabBlock::List m_full_blocks;
|
2021-12-26 20:38:22 +03:00
|
|
|
};
|
|
|
|
|
2021-12-25 19:23:18 +03:00
|
|
|
struct KmallocGlobalData {
|
2021-12-26 12:40:28 +03:00
|
|
|
static constexpr size_t minimum_subheap_size = 1 * MiB;
|
|
|
|
|
2021-12-25 19:23:18 +03:00
|
|
|
KmallocGlobalData(u8* initial_heap, size_t initial_heap_size)
|
|
|
|
{
|
|
|
|
add_subheap(initial_heap, initial_heap_size);
|
|
|
|
}
|
2020-08-30 01:41:30 +03:00
|
|
|
|
2021-12-25 19:23:18 +03:00
|
|
|
void add_subheap(u8* storage, size_t storage_size)
|
|
|
|
{
|
2022-02-05 17:43:02 +03:00
|
|
|
dbgln_if(KMALLOC_DEBUG, "Adding kmalloc subheap @ {} with size {}", storage, storage_size);
|
2021-12-26 01:52:33 +03:00
|
|
|
static_assert(sizeof(KmallocSubheap) <= PAGE_SIZE);
|
2021-12-25 19:23:18 +03:00
|
|
|
auto* subheap = new (storage) KmallocSubheap(storage + PAGE_SIZE, storage_size - PAGE_SIZE);
|
|
|
|
subheaps.append(*subheap);
|
|
|
|
}
|
2020-08-30 01:41:30 +03:00
|
|
|
|
2022-12-07 07:02:59 +03:00
|
|
|
void* allocate(size_t size, size_t alignment, CallerWillInitializeMemory caller_will_initialize_memory)
|
2021-12-25 19:23:18 +03:00
|
|
|
{
|
|
|
|
VERIFY(!expansion_in_progress);
|
2020-08-30 01:41:30 +03:00
|
|
|
|
2021-12-26 20:38:22 +03:00
|
|
|
for (auto& slabheap : slabheaps) {
|
2022-12-07 07:02:59 +03:00
|
|
|
if (size <= slabheap.slab_size() && alignment <= slabheap.slab_size())
|
2023-12-29 03:36:39 +03:00
|
|
|
return slabheap.allocate(size, caller_will_initialize_memory);
|
2021-12-26 20:38:22 +03:00
|
|
|
}
|
|
|
|
|
2021-12-25 19:23:18 +03:00
|
|
|
for (auto& subheap : subheaps) {
|
2022-12-07 07:02:59 +03:00
|
|
|
if (auto* ptr = subheap.allocator.allocate(size, alignment, caller_will_initialize_memory))
|
2021-12-25 19:23:18 +03:00
|
|
|
return ptr;
|
2020-08-30 01:41:30 +03:00
|
|
|
}
|
|
|
|
|
2022-02-14 15:40:14 +03:00
|
|
|
// NOTE: This size calculation is a mirror of kmalloc_aligned(KmallocSlabBlock)
|
|
|
|
if (size <= KmallocSlabBlock::block_size * 2 + sizeof(ptrdiff_t) + sizeof(size_t)) {
|
|
|
|
// FIXME: We should propagate a freed pointer, to find the specific subheap it belonged to
|
|
|
|
// This would save us iterating over them in the next step and remove a recursion
|
|
|
|
bool did_purge = false;
|
|
|
|
for (auto& slabheap : slabheaps) {
|
|
|
|
if (slabheap.try_purge()) {
|
|
|
|
dbgln_if(KMALLOC_DEBUG, "Kmalloc purged block(s) from slabheap of size {} to avoid expansion", slabheap.slab_size());
|
|
|
|
did_purge = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (did_purge)
|
2022-12-07 07:02:59 +03:00
|
|
|
return allocate(size, alignment, caller_will_initialize_memory);
|
2022-02-14 15:40:14 +03:00
|
|
|
}
|
|
|
|
|
2021-12-26 12:40:28 +03:00
|
|
|
if (!try_expand(size)) {
|
2022-12-07 02:38:12 +03:00
|
|
|
dbgln_if(KMALLOC_DEBUG, "OOM when trying to expand kmalloc heap");
|
|
|
|
return nullptr;
|
2021-12-25 19:23:18 +03:00
|
|
|
}
|
2020-08-30 01:41:30 +03:00
|
|
|
|
2022-12-07 07:02:59 +03:00
|
|
|
return allocate(size, alignment, caller_will_initialize_memory);
|
2021-12-25 19:23:18 +03:00
|
|
|
}
|
|
|
|
|
2021-12-26 20:12:25 +03:00
|
|
|
void deallocate(void* ptr, size_t size)
|
2021-12-25 19:23:18 +03:00
|
|
|
{
|
|
|
|
VERIFY(!expansion_in_progress);
|
2021-12-28 21:25:14 +03:00
|
|
|
VERIFY(is_valid_kmalloc_address(VirtualAddress { ptr }));
|
2021-12-25 19:23:18 +03:00
|
|
|
|
2021-12-26 20:38:22 +03:00
|
|
|
for (auto& slabheap : slabheaps) {
|
|
|
|
if (size <= slabheap.slab_size())
|
|
|
|
return slabheap.deallocate(ptr);
|
|
|
|
}
|
|
|
|
|
2021-12-25 19:23:18 +03:00
|
|
|
for (auto& subheap : subheaps) {
|
|
|
|
if (subheap.allocator.contains(ptr)) {
|
|
|
|
subheap.allocator.deallocate(ptr);
|
|
|
|
return;
|
2021-03-11 17:26:02 +03:00
|
|
|
}
|
2020-08-30 01:41:30 +03:00
|
|
|
}
|
|
|
|
|
2021-12-26 20:12:25 +03:00
|
|
|
PANIC("Bogus pointer passed to kfree_sized({:p}, {})", ptr, size);
|
2021-12-25 19:23:18 +03:00
|
|
|
}
|
2020-08-30 01:41:30 +03:00
|
|
|
|
2021-12-25 19:23:18 +03:00
|
|
|
size_t allocated_bytes() const
|
2020-08-30 01:41:30 +03:00
|
|
|
{
|
2021-12-25 19:23:18 +03:00
|
|
|
size_t total = 0;
|
|
|
|
for (auto const& subheap : subheaps)
|
|
|
|
total += subheap.allocator.allocated_bytes();
|
2022-01-23 19:28:32 +03:00
|
|
|
for (auto const& slabheap : slabheaps)
|
|
|
|
total += slabheap.allocated_bytes();
|
2021-12-25 19:23:18 +03:00
|
|
|
return total;
|
2020-08-30 01:41:30 +03:00
|
|
|
}
|
2021-12-25 19:23:18 +03:00
|
|
|
|
|
|
|
size_t free_bytes() const
|
2020-08-30 01:41:30 +03:00
|
|
|
{
|
2021-12-25 19:23:18 +03:00
|
|
|
size_t total = 0;
|
|
|
|
for (auto const& subheap : subheaps)
|
|
|
|
total += subheap.allocator.free_bytes();
|
2022-01-23 19:28:32 +03:00
|
|
|
for (auto const& slabheap : slabheaps)
|
|
|
|
total += slabheap.free_bytes();
|
2021-12-25 19:23:18 +03:00
|
|
|
return total;
|
2020-08-30 01:41:30 +03:00
|
|
|
}
|
|
|
|
|
2021-12-26 12:40:28 +03:00
|
|
|
bool try_expand(size_t allocation_request)
|
2020-08-30 01:41:30 +03:00
|
|
|
{
|
2021-12-25 19:23:18 +03:00
|
|
|
VERIFY(!expansion_in_progress);
|
|
|
|
TemporaryChange change(expansion_in_progress, true);
|
|
|
|
|
|
|
|
auto new_subheap_base = expansion_data->next_virtual_address;
|
2021-12-26 12:40:28 +03:00
|
|
|
Checked<size_t> padded_allocation_request = allocation_request;
|
|
|
|
padded_allocation_request *= 2;
|
|
|
|
padded_allocation_request += PAGE_SIZE;
|
|
|
|
if (padded_allocation_request.has_overflow()) {
|
|
|
|
PANIC("Integer overflow during kmalloc heap expansion");
|
|
|
|
}
|
2021-12-24 17:22:11 +03:00
|
|
|
auto rounded_allocation_request = Memory::page_round_up(padded_allocation_request.value());
|
|
|
|
if (rounded_allocation_request.is_error()) {
|
|
|
|
PANIC("Integer overflow computing pages for kmalloc heap expansion");
|
|
|
|
}
|
|
|
|
size_t new_subheap_size = max(minimum_subheap_size, rounded_allocation_request.value());
|
2021-12-26 12:40:28 +03:00
|
|
|
|
2022-02-05 17:43:02 +03:00
|
|
|
dbgln_if(KMALLOC_DEBUG, "Unable to allocate {}, expanding kmalloc heap", allocation_request);
|
2021-12-25 19:23:18 +03:00
|
|
|
|
|
|
|
if (!expansion_data->virtual_range.contains(new_subheap_base, new_subheap_size)) {
|
2022-12-07 02:38:12 +03:00
|
|
|
dbgln_if(KMALLOC_DEBUG, "Out of address space when expanding kmalloc heap");
|
|
|
|
return false;
|
2021-12-25 19:23:18 +03:00
|
|
|
}
|
|
|
|
|
2022-07-14 15:27:22 +03:00
|
|
|
auto physical_pages_or_error = MM.commit_physical_pages(new_subheap_size / PAGE_SIZE);
|
2021-12-25 19:23:18 +03:00
|
|
|
if (physical_pages_or_error.is_error()) {
|
2022-12-07 02:38:12 +03:00
|
|
|
dbgln_if(KMALLOC_DEBUG, "Out of address space when expanding kmalloc heap");
|
|
|
|
return false;
|
2021-12-25 19:23:18 +03:00
|
|
|
}
|
|
|
|
auto physical_pages = physical_pages_or_error.release_value();
|
|
|
|
|
|
|
|
expansion_data->next_virtual_address = expansion_data->next_virtual_address.offset(new_subheap_size);
|
|
|
|
|
2022-04-03 02:06:34 +03:00
|
|
|
auto cpu_supports_nx = Processor::current().has_nx();
|
2021-12-25 21:55:52 +03:00
|
|
|
|
2021-12-25 19:23:18 +03:00
|
|
|
SpinlockLocker pd_locker(MM.kernel_page_directory().get_lock());
|
|
|
|
|
|
|
|
for (auto vaddr = new_subheap_base; !physical_pages.is_empty(); vaddr = vaddr.offset(PAGE_SIZE)) {
|
|
|
|
// FIXME: We currently leak physical memory when mapping it into the kmalloc heap.
|
|
|
|
auto& page = physical_pages.take_one().leak_ref();
|
2021-12-26 04:42:49 +03:00
|
|
|
auto* pte = MM.pte(MM.kernel_page_directory(), vaddr);
|
|
|
|
VERIFY(pte);
|
2021-12-25 19:23:18 +03:00
|
|
|
pte->set_physical_page_base(page.paddr().get());
|
|
|
|
pte->set_global(true);
|
|
|
|
pte->set_user_allowed(false);
|
|
|
|
pte->set_writable(true);
|
2021-12-25 21:55:52 +03:00
|
|
|
if (cpu_supports_nx)
|
|
|
|
pte->set_execute_disabled(true);
|
2021-12-25 19:23:18 +03:00
|
|
|
pte->set_present(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
add_subheap(new_subheap_base.as_ptr(), new_subheap_size);
|
|
|
|
return true;
|
2020-08-30 01:41:30 +03:00
|
|
|
}
|
2021-12-25 19:23:18 +03:00
|
|
|
|
2021-12-26 04:42:49 +03:00
|
|
|
void enable_expansion()
|
|
|
|
{
|
|
|
|
// FIXME: This range can be much bigger on 64-bit, but we need to figure something out for 32-bit.
|
2022-04-05 13:40:31 +03:00
|
|
|
auto reserved_region = MUST(MM.allocate_unbacked_region_anywhere(64 * MiB, 1 * MiB));
|
2021-12-26 04:42:49 +03:00
|
|
|
|
|
|
|
expansion_data = KmallocGlobalData::ExpansionData {
|
2022-04-03 14:28:16 +03:00
|
|
|
.virtual_range = reserved_region->range(),
|
|
|
|
.next_virtual_address = reserved_region->range().base(),
|
2021-12-26 04:42:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Make sure the entire kmalloc VM range is backed by page tables.
|
|
|
|
// This avoids having to deal with lazy page table allocation during heap expansion.
|
|
|
|
SpinlockLocker pd_locker(MM.kernel_page_directory().get_lock());
|
2022-04-03 14:28:16 +03:00
|
|
|
for (auto vaddr = reserved_region->range().base(); vaddr < reserved_region->range().end(); vaddr = vaddr.offset(PAGE_SIZE)) {
|
2021-12-26 04:42:49 +03:00
|
|
|
MM.ensure_pte(MM.kernel_page_directory(), vaddr);
|
|
|
|
}
|
2022-04-03 14:28:16 +03:00
|
|
|
|
|
|
|
(void)reserved_region.leak_ptr();
|
2021-12-26 04:42:49 +03:00
|
|
|
}
|
|
|
|
|
2021-12-25 19:23:18 +03:00
|
|
|
struct ExpansionData {
|
|
|
|
Memory::VirtualRange virtual_range;
|
|
|
|
VirtualAddress next_virtual_address;
|
|
|
|
};
|
|
|
|
Optional<ExpansionData> expansion_data;
|
|
|
|
|
2021-12-28 21:25:14 +03:00
|
|
|
bool is_valid_kmalloc_address(VirtualAddress vaddr) const
|
|
|
|
{
|
|
|
|
if (vaddr.as_ptr() >= initial_kmalloc_memory && vaddr.as_ptr() < (initial_kmalloc_memory + INITIAL_KMALLOC_MEMORY_SIZE))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!expansion_data.has_value())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return expansion_data->virtual_range.contains(vaddr);
|
|
|
|
}
|
|
|
|
|
2021-12-28 03:32:38 +03:00
|
|
|
KmallocSubheap::List subheaps;
|
2021-12-25 19:23:18 +03:00
|
|
|
|
2021-12-26 20:38:22 +03:00
|
|
|
KmallocSlabheap slabheaps[6] = { 16, 32, 64, 128, 256, 512 };
|
|
|
|
|
2021-12-25 19:23:18 +03:00
|
|
|
bool expansion_in_progress { false };
|
2020-08-30 01:41:30 +03:00
|
|
|
};
|
|
|
|
|
2021-12-25 19:23:18 +03:00
|
|
|
READONLY_AFTER_INIT static KmallocGlobalData* g_kmalloc_global;
|
|
|
|
alignas(KmallocGlobalData) static u8 g_kmalloc_global_heap[sizeof(KmallocGlobalData)];
|
2020-08-30 01:41:30 +03:00
|
|
|
|
|
|
|
static size_t g_kmalloc_call_count;
|
|
|
|
static size_t g_kfree_call_count;
|
2021-05-13 14:09:00 +03:00
|
|
|
static size_t g_nested_kfree_calls;
|
2019-04-16 00:58:48 +03:00
|
|
|
bool g_dump_kmalloc_stacks;
|
2019-04-15 20:43:12 +03:00
|
|
|
|
2020-08-30 01:41:30 +03:00
|
|
|
void kmalloc_enable_expand()
|
|
|
|
{
|
2021-12-26 04:42:49 +03:00
|
|
|
g_kmalloc_global->enable_expansion();
|
2020-08-30 01:41:30 +03:00
|
|
|
}
|
|
|
|
|
2021-02-19 20:41:50 +03:00
|
|
|
UNMAP_AFTER_INIT void kmalloc_init()
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
2021-01-20 19:49:55 +03:00
|
|
|
// Zero out heap since it's placed after end_of_kernel_bss.
|
2021-12-28 21:19:52 +03:00
|
|
|
memset(initial_kmalloc_memory, 0, sizeof(initial_kmalloc_memory));
|
|
|
|
g_kmalloc_global = new (g_kmalloc_global_heap) KmallocGlobalData(initial_kmalloc_memory, sizeof(initial_kmalloc_memory));
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2020-08-30 01:41:30 +03:00
|
|
|
s_lock.initialize();
|
2018-10-16 12:01:38 +03:00
|
|
|
}
|
|
|
|
|
2022-12-07 07:02:59 +03:00
|
|
|
static void* kmalloc_impl(size_t size, size_t alignment, CallerWillInitializeMemory caller_will_initialize_memory)
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
2022-10-15 10:56:42 +03:00
|
|
|
// Catch bad callers allocating under spinlock.
|
|
|
|
if constexpr (KMALLOC_VERIFY_NO_SPINLOCK_HELD) {
|
|
|
|
Processor::verify_no_spinlocks_held();
|
|
|
|
}
|
|
|
|
|
2022-12-07 07:02:59 +03:00
|
|
|
// Alignment must be a power of two.
|
2022-12-20 17:42:48 +03:00
|
|
|
VERIFY(is_power_of_two(alignment));
|
2022-12-07 07:02:59 +03:00
|
|
|
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker lock(s_lock);
|
2019-04-15 20:43:12 +03:00
|
|
|
++g_kmalloc_call_count;
|
2018-10-24 01:51:19 +03:00
|
|
|
|
2020-04-08 14:30:50 +03:00
|
|
|
if (g_dump_kmalloc_stacks && Kernel::g_kernel_symbols_available) {
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("kmalloc({})", size);
|
2020-02-16 03:27:42 +03:00
|
|
|
Kernel::dump_backtrace();
|
2019-04-16 00:58:48 +03:00
|
|
|
}
|
|
|
|
|
2022-12-07 07:02:59 +03:00
|
|
|
void* ptr = g_kmalloc_global->allocate(size, alignment, caller_will_initialize_memory);
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2021-05-30 17:24:53 +03:00
|
|
|
Thread* current_thread = Thread::current();
|
|
|
|
if (!current_thread)
|
|
|
|
current_thread = Processor::idle_thread();
|
2022-01-09 01:36:13 +03:00
|
|
|
if (current_thread) {
|
|
|
|
// FIXME: By the time we check this, we have already allocated above.
|
|
|
|
// This means that in the case of an infinite recursion, we can't catch it this way.
|
|
|
|
VERIFY(current_thread->is_allocation_enabled());
|
2021-05-30 17:24:53 +03:00
|
|
|
PerformanceManager::add_kmalloc_perf_event(*current_thread, size, (FlatPtr)ptr);
|
2022-01-09 01:36:13 +03:00
|
|
|
}
|
2021-05-13 14:09:00 +03:00
|
|
|
|
2020-08-30 01:41:30 +03:00
|
|
|
return ptr;
|
2018-10-16 12:01:38 +03:00
|
|
|
}
|
|
|
|
|
2022-12-04 21:04:39 +03:00
|
|
|
void* kmalloc(size_t size)
|
|
|
|
{
|
2022-12-07 07:02:59 +03:00
|
|
|
return kmalloc_impl(size, KMALLOC_DEFAULT_ALIGNMENT, CallerWillInitializeMemory::No);
|
2022-12-04 21:04:39 +03:00
|
|
|
}
|
|
|
|
|
2022-03-15 01:59:16 +03:00
|
|
|
void* kcalloc(size_t count, size_t size)
|
|
|
|
{
|
|
|
|
if (Checked<size_t>::multiplication_would_overflow(count, size))
|
|
|
|
return nullptr;
|
|
|
|
size_t new_size = count * size;
|
2022-12-07 07:02:59 +03:00
|
|
|
auto* ptr = kmalloc_impl(new_size, KMALLOC_DEFAULT_ALIGNMENT, CallerWillInitializeMemory::Yes);
|
2022-03-15 01:59:16 +03:00
|
|
|
if (ptr)
|
|
|
|
memset(ptr, 0, new_size);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-07-11 14:19:33 +03:00
|
|
|
void kfree_sized(void* ptr, size_t size)
|
2020-06-06 07:01:30 +03:00
|
|
|
{
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
|
2021-12-26 20:10:35 +03:00
|
|
|
VERIFY(size > 0);
|
|
|
|
|
2022-10-15 10:56:42 +03:00
|
|
|
// Catch bad callers allocating under spinlock.
|
|
|
|
if constexpr (KMALLOC_VERIFY_NO_SPINLOCK_HELD) {
|
|
|
|
Processor::verify_no_spinlocks_held();
|
|
|
|
}
|
|
|
|
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker lock(s_lock);
|
2020-08-30 01:41:30 +03:00
|
|
|
++g_kfree_call_count;
|
2021-05-13 14:09:00 +03:00
|
|
|
++g_nested_kfree_calls;
|
|
|
|
|
|
|
|
if (g_nested_kfree_calls == 1) {
|
2021-05-30 17:24:53 +03:00
|
|
|
Thread* current_thread = Thread::current();
|
|
|
|
if (!current_thread)
|
|
|
|
current_thread = Processor::idle_thread();
|
2022-01-09 01:36:13 +03:00
|
|
|
if (current_thread) {
|
|
|
|
VERIFY(current_thread->is_allocation_enabled());
|
2021-05-30 17:24:53 +03:00
|
|
|
PerformanceManager::add_kfree_perf_event(*current_thread, 0, (FlatPtr)ptr);
|
2022-01-09 01:36:13 +03:00
|
|
|
}
|
2021-05-13 14:09:00 +03:00
|
|
|
}
|
2020-08-30 01:41:30 +03:00
|
|
|
|
2021-12-26 20:12:25 +03:00
|
|
|
g_kmalloc_global->deallocate(ptr, size);
|
2021-05-13 14:09:00 +03:00
|
|
|
--g_nested_kfree_calls;
|
2020-06-06 07:01:30 +03:00
|
|
|
}
|
|
|
|
|
2021-05-15 11:06:41 +03:00
|
|
|
size_t kmalloc_good_size(size_t size)
|
|
|
|
{
|
2022-03-08 00:38:05 +03:00
|
|
|
VERIFY(size > 0);
|
|
|
|
// NOTE: There's no need to take the kmalloc lock, as the kmalloc slab-heaps (and their sizes) are constant
|
|
|
|
for (auto const& slabheap : g_kmalloc_global->slabheaps) {
|
|
|
|
if (size <= slabheap.slab_size())
|
|
|
|
return slabheap.slab_size();
|
|
|
|
}
|
|
|
|
return round_up_to_power_of_two(size + Heap<CHUNK_SIZE>::AllocationHeaderSize, CHUNK_SIZE) - Heap<CHUNK_SIZE>::AllocationHeaderSize;
|
2021-05-15 11:06:41 +03:00
|
|
|
}
|
|
|
|
|
2021-12-26 20:08:55 +03:00
|
|
|
void* kmalloc_aligned(size_t size, size_t alignment)
|
2021-07-15 13:08:35 +03:00
|
|
|
{
|
2022-12-07 07:02:59 +03:00
|
|
|
return kmalloc_impl(size, alignment, CallerWillInitializeMemory::No);
|
2021-07-15 13:08:35 +03:00
|
|
|
}
|
|
|
|
|
AK+Kernel: Make fallible allocations compiler-agnostic
In standard C++, operators `new` and `new[]` are guaranteed to return a
valid (non-null) pointer and throw an exception if the allocation
couldn't be performed. Based on this, compilers did not check the
returned pointer before attempting to use them for object construction.
To avoid this, the allocator operators were changed to be `noexcept` in
PR #7026, which made GCC emit the desired null checks. Unfortunately,
this is a non-standard feature which meant that Clang would not accept
these function definitions, as it did not match its expected
declaration.
To make compiling using Clang possible, the special "nothrow" versions
of `new` are implemented in this commit. These take a tag type of
`std::nothrow_t` (used for disambiguating from placement new/etc.), and
are allowed by the standard to return null. There is a global variable,
`std::nothrow`, declared with this type, which is also exported into the
global namespace.
To perform fallible allocations, the following syntax should be used:
```cpp
auto ptr = new (nothrow) T;
```
As we don't support exceptions in the kernel, the only way of uphold the
"throwing" new's guarantee is to abort if the allocation couldn't be
performed. Once we have proper OOM handling in the kernel, this should
only be used for critical allocations, where we wouldn't be able to
recover from allocation failures anyway.
2021-06-20 10:39:20 +03:00
|
|
|
void* operator new(size_t size)
|
|
|
|
{
|
|
|
|
void* ptr = kmalloc(size);
|
|
|
|
VERIFY(ptr);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void* operator new(size_t size, std::nothrow_t const&) noexcept
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
|
|
|
return kmalloc(size);
|
|
|
|
}
|
|
|
|
|
2021-07-15 13:08:35 +03:00
|
|
|
void* operator new(size_t size, std::align_val_t al)
|
|
|
|
{
|
2021-12-26 20:08:55 +03:00
|
|
|
void* ptr = kmalloc_aligned(size, (size_t)al);
|
2021-07-15 13:08:35 +03:00
|
|
|
VERIFY(ptr);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void* operator new(size_t size, std::align_val_t al, std::nothrow_t const&) noexcept
|
2021-07-15 13:08:35 +03:00
|
|
|
{
|
2021-12-26 20:08:55 +03:00
|
|
|
return kmalloc_aligned(size, (size_t)al);
|
2021-07-15 13:08:35 +03:00
|
|
|
}
|
|
|
|
|
AK+Kernel: Make fallible allocations compiler-agnostic
In standard C++, operators `new` and `new[]` are guaranteed to return a
valid (non-null) pointer and throw an exception if the allocation
couldn't be performed. Based on this, compilers did not check the
returned pointer before attempting to use them for object construction.
To avoid this, the allocator operators were changed to be `noexcept` in
PR #7026, which made GCC emit the desired null checks. Unfortunately,
this is a non-standard feature which meant that Clang would not accept
these function definitions, as it did not match its expected
declaration.
To make compiling using Clang possible, the special "nothrow" versions
of `new` are implemented in this commit. These take a tag type of
`std::nothrow_t` (used for disambiguating from placement new/etc.), and
are allowed by the standard to return null. There is a global variable,
`std::nothrow`, declared with this type, which is also exported into the
global namespace.
To perform fallible allocations, the following syntax should be used:
```cpp
auto ptr = new (nothrow) T;
```
As we don't support exceptions in the kernel, the only way of uphold the
"throwing" new's guarantee is to abort if the allocation couldn't be
performed. Once we have proper OOM handling in the kernel, this should
only be used for critical allocations, where we wouldn't be able to
recover from allocation failures anyway.
2021-06-20 10:39:20 +03:00
|
|
|
void* operator new[](size_t size)
|
|
|
|
{
|
|
|
|
void* ptr = kmalloc(size);
|
|
|
|
VERIFY(ptr);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void* operator new[](size_t size, std::nothrow_t const&) noexcept
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
|
|
|
return kmalloc(size);
|
|
|
|
}
|
2020-08-30 01:41:30 +03:00
|
|
|
|
2021-07-11 14:25:42 +03:00
|
|
|
void operator delete(void*) noexcept
|
2021-05-11 11:17:04 +03:00
|
|
|
{
|
2021-07-11 14:25:42 +03:00
|
|
|
// All deletes in kernel code should have a known size.
|
|
|
|
VERIFY_NOT_REACHED();
|
2021-05-11 11:17:04 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 14:19:33 +03:00
|
|
|
void operator delete(void* ptr, size_t size) noexcept
|
2021-05-11 11:17:04 +03:00
|
|
|
{
|
2021-07-11 14:19:33 +03:00
|
|
|
return kfree_sized(ptr, size);
|
2021-05-11 11:17:04 +03:00
|
|
|
}
|
|
|
|
|
2022-12-07 07:02:59 +03:00
|
|
|
void operator delete(void* ptr, size_t size, std::align_val_t) noexcept
|
2021-07-15 13:08:35 +03:00
|
|
|
{
|
2022-12-07 07:02:59 +03:00
|
|
|
return kfree_sized(ptr, size);
|
2021-07-15 13:08:35 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 14:25:42 +03:00
|
|
|
void operator delete[](void*) noexcept
|
2021-05-11 11:17:04 +03:00
|
|
|
{
|
2021-07-11 14:25:42 +03:00
|
|
|
// All deletes in kernel code should have a known size.
|
|
|
|
VERIFY_NOT_REACHED();
|
2021-05-11 11:17:04 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 14:19:33 +03:00
|
|
|
void operator delete[](void* ptr, size_t size) noexcept
|
2021-05-11 11:17:04 +03:00
|
|
|
{
|
2021-07-11 14:19:33 +03:00
|
|
|
return kfree_sized(ptr, size);
|
2021-05-11 11:17:04 +03:00
|
|
|
}
|
|
|
|
|
2020-08-30 01:41:30 +03:00
|
|
|
void get_kmalloc_stats(kmalloc_stats& stats)
|
|
|
|
{
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker lock(s_lock);
|
2021-12-25 19:23:18 +03:00
|
|
|
stats.bytes_allocated = g_kmalloc_global->allocated_bytes();
|
|
|
|
stats.bytes_free = g_kmalloc_global->free_bytes();
|
2020-08-30 01:41:30 +03:00
|
|
|
stats.kmalloc_call_count = g_kmalloc_call_count;
|
|
|
|
stats.kfree_call_count = g_kfree_call_count;
|
|
|
|
}
|