summaryrefslogtreecommitdiff
path: root/Kernel/Heap
AgeCommit message (Collapse)Author
2021-10-02Kernel: Access Processor static methods staticallyBrian Gianforcaro
SonarCloud flagged this "Code Smell", where we are accessing these static methods as if they are instance methods. While it is technically possible, it is very confusing to read when you realize they are static functions.
2021-09-12Kernel: Zero initialize SlabAllocator member variablesBrian Gianforcaro
PVS-Studio flagged these as uninitialized. While there is no bug here, it is our policy to always initialize members to avoid potential bugs in the future.
2021-09-06Kernel: Make kernel region allocators return KResultOr<NOP<Region>>Andreas Kling
This expands the reach of error propagation greatly throughout the kernel. Sadly, it also exposes the fact that we're allocating (and doing other fallible things) in constructors all over the place. This patch doesn't attempt to address that of course. That's work for our future selves.
2021-09-05Kernel: Switch static_asserts of a type size to AK::AssertSizeBrian Gianforcaro
This will provide better debug ability when the size comparison fails.
2021-09-05Kernel: Declare type aliases with "using" instead of "typedef"Brian Gianforcaro
This is the idiomatic way to declare type aliases in modern C++. Flagged by Sonar Cloud as a "Code Smell", but I happen to agree with this particular one. :^)
2021-08-22Kernel: Rename ScopedSpinlock => SpinlockLockerAndreas Kling
This matches MutexLocker, and doesn't sound like it's a lock itself.
2021-08-22Kernel: Rename SpinLock => SpinlockAndreas Kling
2021-08-13Kernel: Allow `kfree_aligned` to be called on null pointersDaniel Bertalan
The C++ standard specifies that `free` and `operator delete` should be callable with nullptr. The non-aligned `kfree` already handles this, but because of the pointer arithmetic to obtain the allocation start pointer, the aligned version would produce undefined behavior.
2021-08-13Kernel: Allow aligned `operator new` to return nullptrDaniel Bertalan
In e7fb70b05, regular kmalloc was changed to return nullptr on allocation failure instead of crashing. The `kmalloc_aligned_cxx` wrapper used by the aligned operator new should do the same.
2021-08-13Kernel: Allow kmalloc(..) / kmalloc_aligned(..) to return nullptrBrian Gianforcaro
Now that we have a significant amount of code paths handling OOM, lets enable kmalloc and friends to actually return nullptr. This way we can start stressing these paths and validating all of they work as expected.
2021-08-10Kernel/SMP: Make entering/leaving critical sections multi-processor safeAndreas Kling
By making these functions static we close a window where we could get preempted after calling Processor::current() and move to another processor. Co-authored-by: Tom <tomut@yahoo.com>
2021-08-08Kernel: Bump eternal kmalloc range to 4 MiBDaniel Bertalan
Kernels built with Clang seem to be quite allocation-heavy compared to their GCC counterparts. We would sometimes end up crashing during boot because the eternal ranges had no free capacity.
2021-08-07Kernel: Move SpinLock.h into Locking/Jean-Baptiste Boric
2021-08-06Kernel: Add convenience values to the Memory::Region::Access enumAndreas Kling
Instead of `Memory::Region::Access::Read | Memory::Region::AccessWrite` you can now say `Memory::Region::Access::ReadWrite`.
2021-08-06Kernel: Move Kernel/Memory/ code into Kernel::Memory namespaceAndreas Kling
2021-08-06Kernel: Rename Kernel/VM/ to Kernel/Memory/Andreas Kling
This directory isn't just about virtual memory, it's about all kinds of memory management.
2021-07-16Kernel+AK: Generate compile-time error for non-sized `delete`Daniel Bertalan
This is a much more ergonomic option than getting a `VERIFY_NOT_REACHED()` failure at run-time. I encountered this issue with Clang, where sized deallocation is not the default due to ABI breakage concerns. Note that we can't simply just not declare these functions, because the C++ standard states: > If this function with size parameter is defined, the program shall > also define the version without the size parameter.
2021-07-16Kernel: Implement aligned `operator new` and use itDaniel Bertalan
The compiler will use these to allocate objects that have alignment requirements greater than that of our normal `operator new` (4/8 byte aligned). This means we can now use smart pointers for over-aligned types. Fixes a FIXME.
2021-07-14Kernel: Allow passing null pointer to deleteDaniel Bertalan
The C++ standard says that it's legal to call the `delete` operator with a null pointer argument, in which case it should be a no-op. I encountered this issue when running a kernel that's compiled with Clang. I assume this fact was used for some kind of optimization.
2021-07-11Kernel: Remove unused header includes in Heap subtreeBrian Gianforcaro
2021-07-11Kernel: Remove krealloc()Andreas Kling
This was only used by a single class (AK::ByteBuffer) in the kernel and not in an OOM-safe way. Now that ByteBuffer no longer uses it, there's no need for the kernel heap to burden itself with supporting this.
2021-07-11Kernel: VERIFY_NOT_REACHED in un-sized operator deleteAndreas Kling
All deletes in kernel code should now be of known size. :^)
2021-07-11Kernel: Add kfree_sized(), kfree() with a known allocation sizeAndreas Kling
C++14 gave us sized operator delete, but we haven't been taking advantage of it. Let's get to a point where it can help us by adding kfree_sized(void*, size_t).
2021-07-03Everywhere: Fix some alignment issuesDaniel Bertalan
When creating uninitialized storage for variables, we need to make sure that the alignment is correct. Fixes a KUBSAN failure when running kernels compiled with Clang. In `Syscalls/socket.cpp`, we can simply use local variables, as `sockaddr_un` is a POD type. Along with moving the `alignas` specifier to the correct member, `AK::Optional`'s internal buffer has been made non-zeroed by default. GCC emitted bogus uninitialized memory access warnings, so we now use `__builtin_launder` to tell the compiler that we know what we are doing. This might disable some optimizations, but judging by how GCC failed to notice that the memory's initialization is dependent on `m_has_value`, I'm not sure that's a bad thing.
2021-07-01Kernel: Only deallocate memory when alloc succeedsHendiadyoin1
Also make AllocationHeader acquisition from pointers more verbose
2021-06-28Kernel: Add a sanity check for CHUNK_SIZEGunnar Beutner
Also fixes a spelling mistake in the same file.
2021-06-28Kernel: Increase kmalloc eternal heap to 3MiBGunnar Beutner
The kernel wouldn't boot reliably on x86_64 with just 2MiB.
2021-06-26Kernel: Make addresses returned by kmalloc() properly aligned for x86_64Gunnar Beutner
2021-06-26Kernel: Add slab allocator for 256 bytesGunnar Beutner
Our types are getting a tiny bit larger for x86_64 so we need another slab allocator to deal with that.
2021-06-24AK+Kernel: Make fallible allocations compiler-agnosticDaniel Bertalan
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-24Kernel: Move special sections into Sections.hHendiadyoin1
This also removes a lot of CPU.h includes infavor for Sections.h
2021-06-24Kernel: Remove PAGE_SIZE from CPU.hHendiadyoin1
We have that information in LibC, lets use that instead
2021-06-16Kernel: Remove various other uses of ssize_tGunnar Beutner
2021-05-30Kernel: Don't log profile data before/after the process/thread lifetimeGunnar Beutner
There were a few cases where we could end up logging profiling events before or after the associated process or thread exists in the profile: After enabling profiling we might end up with CPU samples before we had a chance to synthesize process/thread creation events. After a thread exits we would still log associated kmalloc/kfree events. Instead we now just ignore those events.
2021-05-29Kernel: Don't overrun the buffer in krealloc()Gunnar Beutner
The allocation_size_in_chunks field contains the bytes necessary for the AllocationHeader so we need to subtract that when we try to figure out how much user data we have to copy. Fixes #7549.
2021-05-19Kernel: Add support for profiling kmalloc()/kfree()Gunnar Beutner
2021-05-18BitmapView: Disable mutations of the underlying BitmapLenny Maiorani
Problem: - `BitmapView` permits changing the underlying `Bitmap`. This violates the idea of a "view" since views are simply overlays which can themselves change but do not change the underlying data. Solution: - Migrate all non-`const` member functions to Bitmap.
2021-05-17Revert "BitmapView: Disable mutations of the underlying Bitmap"Andreas Kling
This reverts commit f25209113fcd15df5778938c4accf13c5139d278.
2021-05-17BitmapView: Disable mutations of the underlying BitmapLenny Maiorani
Problem: - `BitmapView` permits changing the underlying `Bitmap`. This violates the idea of a "view" since views are simply overlays which can themselves change but do not change the underlying data. Solution: - Migrate all non-`const` member functions to Bitmap.
2021-05-15AK+LibC: Implement malloc_good_size() and use it for Vector/HashTableGunnar Beutner
This implements the macOS API malloc_good_size() which returns the true allocation size for a given requested allocation size. This allows us to make use of all the available memory in a malloc chunk. For example, for a malloc request of 35 bytes our malloc would internally use a chunk of size 64, however the remaining 29 bytes would be unused. Knowing the true allocation size allows us to request more usable memory that would otherwise be wasted and make that available for Vector, HashTable and potentially other callers in the future.
2021-05-14Kernel: Add the ability to verify we don't kmalloc under spinlock.Brian Gianforcaro
Ideally we would never allocate under a spinlock, as it has many performance and potentially functionality (deadlock) pitfalls. We violate that rule in many places today, but we need a tool to track them all down and fix them. This change introduces a new macro option named `KMALLOC_VERIFY_NO_SPINLOCK_HELD` which can catch these situations at runtime via an assert.
2021-05-13Kernel: Declare operator new/delete noexcept for MAKE_SLAB_ALLOCATEDBrian Gianforcaro
2021-05-13Kernel: Declare operator new/delete noexcept for MAKE_ALIGNED_ALLOCATEDBrian Gianforcaro
2021-05-13Kernel: Declare operator new/delete as noexcept for the KernelBrian Gianforcaro
For Kernel OOM hardening to work correctly, we need to be able to call a "nothrow" version of operator new. Unfortunately the default "throwing" version of operator new assumes that the allocation will never return on failure and will always throw an exception. This isn't true in the Kernel, as we don't have exceptions. So if we call the normal/throwing new and kmalloc returns NULL, the generated code will happily go and dereference that NULL pointer by invoking the constructor before we have a chance to handle the failure. To fix this we declare operator new as noexcept in the Kernel headers, which will allow the caller to actually handle allocation failure. The delete implementations need to match the prototype of the new which allocated them, so we need define delete as noexcept as well. GCC then errors out declaring that you should implement sized delete as well, so this change provides those stubs in order to compile cleanly. Finally the new operator definitions have been standardized as being declared with [[nodiscard]] to avoid potential memory leaks. So lets declares the kernel versions that way as well.
2021-04-29Everywhere: Use "the SerenityOS developers." in copyright headersLinus Groh
We had some inconsistencies before: - Sometimes "The", sometimes "the" - Sometimes trailing ".", sometimes no trailing "." I picked the most common one (lowecase "the", trailing ".") and applied it to all copyright headers. By using the exact same string everywhere we can ensure nothing gets missed during a global search (and replace), and that these inconsistencies are not spread any further (as copyright headers are commonly copied to new files).
2021-04-22Everything: Move to SPDX license identifiers in all files.Brian Gianforcaro
SPDX License Identifiers are a more compact / standardized way of representing file license information. See: https://spdx.dev/resources/use/#identifiers This was done with the `ambr` search and replace tool. ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-09Kernel: Do some basic metadata integrity verification in kmalloc/kfreeAndreas Kling
Use BitmapView::set_range_and_verify_that_all_bits_flip() to validate the heap chunk metadata bits as we go through them in kmalloc/kfree.
2021-04-09Kernel: Add some basic double-kfree() detectionAndreas Kling
Double kfree() is exceedingly rare in our kernel since we use automatic memory management and smart pointers for almost all code. However, it doesn't hurt to do some basic checking that might one day catch bugs. This patch makes us VERIFY that we don't already consider the first chunk of a kmalloc() allocation free when kfree()'ing it.
2021-03-21Kernel::CPU: Move headers into common directoryHendiadyoin1
Alot of code is shared between i386/i686/x86 and x86_64 and a lot probably will be used for compatability modes. So we start by moving the headers into one Directory. We will probalby be able to move some cpp files aswell.
2021-03-11Kernel: Suppress logging during kmalloc heap expansionAndreas Kling
The system is extremely sensitive to heap allocations during heap expansion. This was causing frequent OOM panics under various loads. Work around the issue for now by putting the logging behind KMALLOC_DEBUG. Ideally dmesgln() & friends would not reqiure any heap allocations, but we're not there right now. Fixes #5724.