summaryrefslogtreecommitdiff
path: root/Kernel/Memory
AgeCommit message (Collapse)Author
2022-01-21Kernel: Make Memory::RingBuffer construction fallibleIdan Horowitz
2022-01-16Kernel: Use an IntrusiveRedBlackTree for kernel regionsAndreas Kling
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.) :^)
2022-01-16Kernel: Don't access directory table of uninitialized PageDirectorycreator1creeper1
PageDirectory gets initialized step-by-step in PageDirectory::try_create_for_userspace(). This initialization may fail anywhere in this function - for example, we may not be able to allocate a directory table, in which case PageDirectory::try_create_for_userspace() will return a null pointer. We recognize this condition and early-return ENOMEM. However, at this point, we need to correctly destruct the only partially initialized PageDirectory. Previously, PageDirectory::~PageDirectory() would assume that the object it was destructing was always fully initialized. It now uses the new helper PageDirectory::is_cr3_initialized() to correctly recognize when the directory table was not yet initialized. This helper checks if the pointer to the directory table is null. Only if it is not null does the destructor try to fetch the directory table using PageDirectory::cr3().
2022-01-15Kernel: Remove infallible VMObject resource factory functionscreator1creeper1
These infallible resource factory functions were only there to ease the conversion to the new factory functions. Since all child classes of VMObject now use the fallible resource factory functions, we don't need the infallible versions anymore.
2022-01-15Kernel: Make SharedInodeVMObject construction OOM-awarecreator1creeper1
This commit moves the allocation of the resources required for SharedInodeVMObject from its constructors to its factory functions. We're making this change to expose the fallibility of the allocation.
2022-01-15Kernel: Make PrivateInodeVMObject construction OOM-awarecreator1creeper1
This commit moves the allocation of the resources required for PrivateInodeVMObject from its constructors to its factory functions. We're making this change to expose the fallibility of the allocation.
2022-01-15Kernel: Make InodeVMOBject construction OOM-awarecreator1creeper1
This commit moves the allocation of the resources required for InodeVMObject from its constructors to the constructors of its child classes. We're making this change to give the child classes the chance to expose the fallibility of the allocation.
2022-01-15Kernel: Make AnonymousVMObject construction OOM-awarecreator1creeper1
This commit moves the allocation of the resources required for AnonymousVMObject from its constructors to its factory functions. We're making this change to expose the fallibility of the allocation.
2022-01-15Kernel: Make VMOBject construction OOM-awarecreator1creeper1
This commit moves the allocation of the resources required for VMObject from its constructors to the constructors of its child classes. We're making this change to give the child classes the chance to expose the fallibility of the allocation.
2022-01-15Kernel: Don't remap already non-writable regions when they become CoWAndreas Kling
The only purpose of the remap() in Region::try_clone() is to ensure non-writable page table entries for CoW regions. If a region is already non-writable, there's no need to waste time updating the page tables.
2022-01-15Kernel: Don't bother with page tables for PROT_NONE mappingsAndreas Kling
When mapping or unmapping completely inaccessible memory regions, we don't need to update the page tables at all. This saves a bunch of time in some situations, most notably during dynamic linking, where we make a large VM reservation and immediately throw it away. :^)
2022-01-15Kernel: Use move() in Region::try_clone() to avoid a VMObject::unref()Andreas Kling
2022-01-15Kernel: Only register kernel regions with MemoryManagerAndreas Kling
We were already only tracking kernel regions, this patch just makes it more clear by having it reflected in the name of the registration helpers. We also stop calling them for userspace regions, avoiding some spinlock action in such cases.
2022-01-15Kernel: Remove old "region lookup cache" optimizationAndreas Kling
This optimization was added when region lookup was O(n), before we had the O(log n) RedBlackTree. Let's remove it to simplify the code, as we have no evidence that it remains valuable.
2022-01-15Kernel: Always remove PageDirectories from the cr3 map on destructionIdan Horowitz
Previously we would only remove them from the map if they were attached to an AddressSpace, even though we would always add them to the map on construction. This results in an assertion failure on destruction if the page directory was never attached to an AddressSpace. (for example, on an allocation failure of said AddressSpace)
2022-01-13Kernel: Make map_typed() & map_typed_writable() fallible using ErrorOrIdan Horowitz
This mostly just moved the problem, as a lot of the callers are not capable of propagating the errors themselves, but it's a step in the right direction.
2022-01-13Kernel: Don't flush TLB after creating brand-new mappingsAndreas Kling
The CPU will not cache TLB entries for non-present mappings, so we can simply skip flushing the TLB after creating entirely new mappings.
2022-01-13Kernel: Use StringView instead of String in RingBuffer's constructorIdan Horowitz
This String was being copied into a KString internally anyways.
2022-01-12Kernel: Actually clear page slots in Region::clear_to_zero()Andreas Kling
We were copying the RefPtr<PhysicalPage> and zeroing the copy instead of zeroing the slot itself.
2022-01-12Kernel: Don't release/relock spinlocks repeatedly during space teardownAndreas Kling
Grab the page directory and MM locks once at the start of address space teardown, then hold onto them across all the region unmapping work.
2022-01-12Kernel: Do less unnecessary work when tearing down process address spaceAndreas Kling
When deleting an entire AddressSpace, we don't need to do TLB flushes at all (since the entire page directory is going away anyway). We also don't need to deallocate VM ranges one by one, since the entire VM range allocator will be deleted anyway.
2022-01-10Kernel: Remove redundant hash map of page tables in PageDirectoryAndreas Kling
The purpose of the PageDirectory::m_page_tables map was really just to act as ref-counting storage for PhysicalPage objects that were being used for the directory's page tables. However, this was basically redundant, since we can find the physical address of each page table from the page directory, and we can find the PhysicalPage object from MemoryManager::get_physical_page_entry(). So if we just manually ref() and unref() the pages when they go in and out of the directory, we no longer need PageDirectory::m_page_tables! Not only does this remove a bunch of kmalloc() traffic, it also solves a race condition that would occur when lazily adding a new page table to a directory: Previously, when MemoryManager::ensure_pte() would call HashMap::set() to insert the new page table into m_page_tables, if the HashMap had to grow its internal storage, it would call kmalloc(). If that kmalloc() would need to perform heap expansion, it would end up calling ensure_pte() again, which would clobber the page directory mapping used by the outer invocation of ensure_pte(). The net result of the above bug would be that any invocation of MemoryManager::ensure_pte() could erroneously return a pointer into a kernel page table instead of the correct one! This whole problem goes away when we remove the HashMap, as ensure_pte() no longer does anything that allocates from the heap.
2022-01-10Kernel: Don't relock MM lock for every page when remapping regionAndreas Kling
Make sure that callers already hold the MM lock, and we don't have to worry about reacquiring it every time.
2022-01-09Kernel: Add implied auto qualifiers in MemoryHendiadyoin1
2022-01-09Kernel: Overload DMA helper without Physical Page output parameterPankaj Raghav
Not all drivers need the PhysicalPage output parameter while creating a DMA buffer. This overload will avoid creating a temporary variable for the caller
2022-01-09Kernel: Set Cacheable parameter to NO explicitly in DMA helpersPankaj Raghav
The cacheable parameter to allocate_kernel_region should be explicitly set to No as this region is used to do physical memory transfers. Even though most architectures ignore this even if it is set, it is better to make this explicit.
2022-01-08AK+Everywhere: Make FixedArray OOM-safecreator1creeper1
FixedArray now doesn't expose any infallible constructors anymore. Rather, it exposes fallible methods. Therefore, it can be used for OOM-safe code. This commit also converts the rest of the system to use the new API. However, as an example, VMObject can't take advantage of this yet, as we would have to endow VMObject with a fallible static construction method, which would require a very fundamental change to VMObject's whole inheritance hierarchy.
2022-01-08Kernel: Implement read functionality for MemoryDeviceLiav A
So far we only had mmap(2) functionality on the /dev/mem device, but now we can also do read(2) on it. The test unit was updated to check we are doing it safely.
2022-01-08Kernel: Change method name to clarify physical memory mmap validationLiav A
2022-01-07Kernel/Memory: Remove needless VERIFY in /dev/mem mmap validation methodLiav A
As it was pointed by Idan Horowitz, the rest of the method doesn't assume we have any reserved ranges to allow mmap(2) to work on them, so the VERIFY is not needed at all.
2022-01-04Kernel: Scan ACPI memory ranges for the RSDP tableTom
On some systems the ACPI RSDP table may be located in ACPI reserved memory ranges rather than in the EBDA or BIOS areas.
2022-01-04Kernel: Fix possible buffer overrun when scanning a MappedROMTom
If the length of the prefix was less than the chunk_size argument we were potentionally reading past the mapped memory region.
2022-01-01Kernel: Add DMA allocate functions that are TRY-ablePankaj Raghav
Add DMA allocate buffer helper functions in MemoryManager.
2021-12-29Kernel: Support Mutex Protected lists in ListedRefCountedIdan Horowitz
This will allow us to support Mutex Protected lists like the custodies list as well.
2021-12-28Kernel: Propagate overflow errors from Memory::page_round_upGuilherme Goncalves
Fixes #11402.
2021-12-28Kernel: Remove the kmalloc_eternal heap :^)Andreas Kling
This was a premature optimization from the early days of SerenityOS. The eternal heap was a simple bump pointer allocator over a static byte array. My original idea was to avoid heap fragmentation and improve data locality, but both ideas were rooted in cargo culting, not data. We would reserve 4 MiB at boot and only ended up using ~256 KiB, wasting the rest. This patch replaces all kmalloc_eternal() usage by regular kmalloc().
2021-12-26Kernel: Remove old SlabAllocator :^)Andreas Kling
This is no longer useful since kmalloc() does automatic slab allocation without any of the limitations of the old SlabAllocator. :^)
2021-12-26Kernel: Remove all uses of MAKE_SLAB_ALLOCATED()Andreas Kling
Objects that were previously allocated via slab_alloc()/slab_dealloc() now go through kmalloc()/kfree_sized() instead.
2021-12-25Kernel: Make kmalloc heap expansion kmalloc-freeAndreas Kling
Previously, the heap expansion logic could end up calling kmalloc recursively, which was quite messy and hard to reason about. This patch redesigns heap expansion so that it's kmalloc-free: - We make a single large virtual range allocation at startup - When expanding, we bump allocate VM from that region - When expanding, we populate page tables directly ourselves, instead of going via MemoryManager. This makes heap expansion a great deal simpler. However, do note that it introduces two new flaws that we'll need to deal with eventually: - The single virtual range allocation is limited to 64 MiB and once exhausted, kmalloc() will fail. (Actually, it will PANIC for now..) - The kmalloc heap can no longer shrink once expanded. Subheaps stay in place once constructed.
2021-12-24Kernel: Make MemoryManager::protect_ksyms_after_init UNMAP_AFTER_INITBrian Gianforcaro
The function to protect ksyms after initialization, is only used during boot of the system, so it can be UNMAP_AFTER_INIT as well. This requires we switch the order of the init sequence, so we now call `MM.protect_ksyms_after_init()` before `MM.unmap_text_after_init()`.
2021-12-23Kernel: Make msync return EINVAL when regions are too largeGuilherme Gonçalves
As a small cleanup, this also makes `page_round_up` verify its precondition with `page_round_up_would_wrap` (which callers are expected to call), rather than having its own logic. Fixes #11297.
2021-12-23Kernel: Return EEXIST in VirtualRangeAllocator::try_allocate_specific()Daniel Bertalan
This error only ever gets propagated to the userspace if MAP_FIXED_NOREPLACE is requested, as MAP_FIXED unmaps intersecting ranges beforehand, and non-fixed mmap() calls will just fall back to allocating anywhere. Linux specifies MAP_FIXED_NOREPLACE to return EEXIST when it can't allocate, we now match that behavior.
2021-12-22Kernel: Initialize PhysicalRegion::m_large_zones, remove m_small_zonesBrian Gianforcaro
Found by PVS Studio Static Analysis.
2021-12-22Kernel: Move userspace virtual address range base to 0x10000Idan Horowitz
Now that the shared bottom 2 MiB virtual address mappings are gone userspace can use lower virtual addresses.
2021-12-22Kernel: Don't share the bottom 2 MiB of kernel mappings with processesIdan Horowitz
Now that the last 2 users of these mappings (the Prekernel and the APIC ap boot environment) were removed, these are no longer used.
2021-12-22Kernel: Unmap Prekernel pages after they are no longer neededDaniel Bertalan
The Prekernel's memory is only accessed until MemoryManager has been initialized. Keeping them around afterwards is both unnecessary and bad, as it prevents the userland from using the 0x100000-0x155000 virtual address range. Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
2021-12-21AK+Everywhere: Replace __builtin bit functionsNick Johnson
In order to reduce our reliance on __builtin_{ffs, clz, ctz, popcount}, this commit removes all calls to these functions and replaces them with the equivalent functions in AK/BuiltinWrappers.h.
2021-12-18Kernel: Use ksyms in-place instead of duplicating them into eternal heapAndreas Kling
We can leave the .ksyms section mapped-but-read-only and then have the symbols index simply point into it. Note that we manually insert null-terminators into the symbols section while parsing it. This gets rid of ~950 KiB of kmalloc_eternal() at startup. :^)
2021-12-13Kernel: Replace final loop in PhysicalRegion::return_page() with mathClay Freeman
Since it's possible to determine where the small zones will start to occur for each PhysicalRegion, we can use arithmetic so that the call time for both large and small zones is identical.
2021-12-11Kernel: Remove unused String.h includesHendiadyoin1
This makes searching for not yet OOM safe interfaces a bit easier.