Commit graph

192 commits

Author SHA1 Message Date
Liav A.
2bba9411ca Kernel: Use the AK SetOnce container class in various cases
We have many places in the kernel code that we have boolean flags that
are only set once, and never reset again but are checked multiple times
before and after the time they're being set, which matches the purpose
of the SetOnce class.
2024-04-26 23:46:23 -06:00
Dan Klishch
5ed7cd6e32 Everywhere: Use east const in more places
These changes are compatible with clang-format 16 and will be mandatory
when we eventually bump clang-format version. So, since there are no
real downsides, let's commit them now.
2024-04-19 06:31:19 -04:00
Idan Horowitz
f7a1f28d7f Kernel: Add initial basic support for KASAN
This commit adds minimal support for compiler-instrumentation based
memory access sanitization.
Currently we only support detection of kmalloc redzone accesses, and
kmalloc use-after-free accesses.

Support for inline checks (for improved performance), and for stack
use-after-return and use-after-return detection is left for future PRs.
2023-12-30 13:57:10 +01:00
Sönke Holz
da88d766b2 Kernel/riscv64: Make the kernel compile
This commits inserts TODOs into all necessary places to make the kernel
compile on riscv64!
2023-11-10 15:51:31 -07:00
Timothy Flynn
c911781c21 Everywhere: Remove needless trailing semi-colons after functions
This is a new option in clang-format-16.
2023-07-08 10:32:56 +01:00
Liav A
927926b924 Kernel: Move Performance-measurement code to the Tasks subdirectory 2023-06-04 21:32:34 +02:00
Liav A
7c0540a229 Everywhere: Move global Kernel pattern code to Kernel/Library directory
This has KString, KBuffer, DoubleBuffer, KBufferBuilder, IOWindow,
UserOrKernelBuffer and ScopedCritical classes being moved to the
Kernel/Library subdirectory.

Also, move the panic and assertions handling code to that directory.
2023-06-04 21:32:34 +02:00
Andrew Kaster
ddea37b521 Kernel+LibC: Move name length constants to Kernel/API from limits.h
Reduce inclusion of limits.h as much as possible at the same time.

This does mean that kmalloc.h is now including Kernel/API/POSIX/limits.h
instead of LibC/limits.h, but the scope could be limited a lot more.
Basically every file in the kernel includes kmalloc.h, and needs the
limits.h include for PAGE_SIZE.
2023-01-21 10:43:59 -07:00
Ben Wiederhake
143a64f9a2 Kernel: Remove unused includes of Kernel/Debug.h
These instances were detected by searching for files that include
Kernel/Debug.h, but don't match the regex:
\\bdbgln_if\(|_DEBUG\\b
This regex is pessimistic, so there might be more files that don't check
for any real *_DEBUG macro. There seem to be no corner cases anyway.

In theory, one might use LibCPP to detect things like this
automatically, but let's do this one step after another.
2023-01-02 20:27:20 -05:00
kleines Filmröllchen
a6a439243f Kernel: Turn lock ranks into template parameters
This step would ideally not have been necessary (increases amount of
refactoring and templates necessary, which in turn increases build
times), but it gives us a couple of nice properties:
- SpinlockProtected inside Singleton (a very common combination) can now
  obtain any lock rank just via the template parameter. It was not
  previously possible to do this with SingletonInstanceCreator magic.
- SpinlockProtected's lock rank is now mandatory; this is the majority
  of cases and allows us to see where we're still missing proper ranks.
- The type already informs us what lock rank a lock has, which aids code
  readability and (possibly, if gdb cooperates) lock mismatch debugging.
- The rank of a lock can no longer be dynamic, which is not something we
  wanted in the first place (or made use of). Locks randomly changing
  their rank sounds like a disaster waiting to happen.
- In some places, we might be able to statically check that locks are
  taken in the right order (with the right lock rank checking
  implementation) as rank information is fully statically known.

This refactoring even more exposes the fact that Mutex has no lock rank
capabilites, which is not fixed here.
2023-01-02 18:15:27 -05:00
Liav A
5ff318cf3a Kernel: Remove i686 support 2022-12-28 11:53:41 +01:00
Timon Kruiper
344ffda8cb Kernel: Use AK::is_power_of_two instead of AK::popcount in kmalloc_impl
AK::popcount will use floating-point instructions, which in the aarch64
kernel are not allowed, and will result in an exception.
2022-12-21 08:35:14 +00:00
Tim Schumacher
2577bb8416 Kernel: Start implementing kmalloc_aligned more efficiently
This now only requires `size + alignment` bytes while searching for a
free memory location. For the actual allocation, the memory area is
properly trimmed to the required alignment.
2022-12-14 15:18:31 +00:00
Thomas Queiroz
8e8ea99bf3 Kernel: Return nullptr instead of PANICking in KmallocSlabHeap
I dared to return nullptr :^)
2022-12-07 16:31:16 +00:00
Andreas Kling
d8a3e2fc4e Kernel: Don't memset() allocated memory twice in kcalloc()
This patch adds a way to ask the allocator to skip its internal
scrubbing memset operation. Before this change, kcalloc() would scrub
twice: once internally in kmalloc() and then again in kcalloc().

The same mechanism already existed in LibC malloc, and this patch
brings it over to the kernel heap allocator as well.

This solves one FIXME in kcalloc(). :^)
2022-12-05 10:29:18 +01:00
Linus Groh
d26aabff04 Everywhere: Run clang-format 2022-12-03 23:52:23 +00:00
Timon Kruiper
8fe3c50b30 Kernel/aarch64: Force kmalloc to return 16 byte aligned pointers
KUBSAN complained about a misaligned address when trying to construct
the Thread class.
2022-10-20 23:26:32 +02:00
Brian Gianforcaro
2079728a74 Kernel: Add formal Processor::verify_no_spinlocks_held() API
In a few places we check `!Processor::in_critical()` to validate
that the current processor doesn't hold any kernel spinlocks.

Instead lets provide it a first class name for readability.
I'll also be adding more of these, so I would rather add more
usages of a nice API instead of this implicit/assumed logic.
2022-10-16 02:29:14 -06:00
Andreas Kling
c8375c51ff Kernel: Stop taking MM lock while using PD/PT quickmaps
This is no longer required as these quickmaps are now per-CPU. :^)
2022-08-22 17:56:03 +02:00
kleines Filmröllchen
4314c25cf2 Kernel: Require lock rank for Spinlock construction
All users which relied on the default constructor use a None lock rank
for now. This will make it easier to in the future remove LockRank and
actually annotate the ranks by searching for None.
2022-08-19 20:26:47 -07:00
Andreas Kling
abb84b9fcd Kernel: Fix inconsistent lock acquisition order in kmalloc
We always want to grab the page directory lock before the MM lock.
This fixes a deadlock I encountered when building DOOM with make -j4.
2022-08-18 18:52:34 +02:00
Undefine
97cc33ca47 Everywhere: Make the codebase more architecture aware 2022-07-27 21:46:42 +00:00
Liav A
e4e5fa74d0 Kernel+Userland: Rename prefix of user_physical => physical
There's no such supervisor pages concept, so there's no need to call
physical pages with the "user_physical" prefix anymore.
2022-07-14 23:27:46 +02:00
Andreas Kling
f8d798b667 Kernel: Move allocate_unbacked_region_anywhere() to MemoryManager
This didn't need to be in RegionTree, and since it's specific to kernel
VM anyway, let's move it to MemoryManager.
2022-04-05 13:45:10 +02:00
James Mintram
2b442ae44f Kernel: Add kmalloc.cpp to aarch64 2022-04-03 23:21:04 +02:00
Andreas Kling
e8f543c390 Kernel: Use intrusive RegionTree solution for kernel regions as well
This patch ports MemoryManager to RegionTree as well. The biggest
difference between this and the userspace code is that kernel regions
are owned by extant OwnPtr<Region> objects spread around the kernel,
while userspace regions are owned by the AddressSpace itself.

For kernelspace, there are a couple of situations where we need to make
large VM reservations that never get backed by regular VMObjects
(for example the kernel image reservation, or the big kmalloc range.)
Since we can't make a VM reservation without a Region object anymore,
this patch adds a way to create unbacked Region objects that can be
used for this exact purpose. They have no internal VMObject.)
2022-04-03 21:51:58 +02:00
Idan Horowitz
086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Daniel Bertalan
e3eb68dd58 AK+Kernel: Avoid double memory clearing of HashTable buckets
Since the allocated memory is going to be zeroed immediately anyway,
let's avoid redundantly scrubbing it with MALLOC_SCRUB_BYTE just before
that.

The latest versions of gcc and Clang can automatically do this malloc +
memset -> calloc optimization, but I've seen a couple of places where it
failed to be done.

This commit also adds a naive kcalloc function to the kernel that
doesn't (yet) eliminate the redundancy like the userland does.
2022-03-15 11:56:46 +01:00
Hendiadyoin1
1a739b5d6e Kernel: Try to reuse empty slabheaps before expanding the kmalloc-heap 2022-03-14 23:30:08 +01:00
Idan Horowitz
29eee390ec Kernel: Implement kmalloc_good_size for the new kmalloc
This lets kmalloc-aware data structures like Vector and HashTable use
up the extra wasted space we allocate in the slab heaps & heap chunks.
2022-03-08 00:46:25 +02:00
Andreas Kling
1d411a46c1 Kernel: Put kmalloc heap expansion debug spam behind KMALLOC_DEBUG 2022-02-05 22:50:39 +01:00
Idan Horowitz
16d69376d8 Kernel: Include slabheaps in kmalloc statistics 2022-01-24 14:09:55 +02:00
Andreas Kling
cfc9ce66d9 Kernel: Skip unnecessary TLB flush when growing kmalloc heap
When adding entirely new page table mappings, we don't need to flush
the TLB since they were not present before.
2022-01-13 11:22:11 +01:00
kleines Filmröllchen
e2c9578390 Kernel: Allow preventing kmalloc and kfree
For "destructive" disallowance of allocations throughout the system,
Thread gains a member that controls whether allocations are currently
allowed or not. kmalloc checks this member on both allocations and
deallocations (with the exception of early boot) and panics the kernel
if allocations are disabled. This will allow for critical sections that
can't be allowed to allocate to fail-fast, making for easier debugging.

PS: My first proper Kernel commit :^)
2022-01-11 00:08:58 +01:00
Guilherme Goncalves
33b78915d3 Kernel: Propagate overflow errors from Memory::page_round_up
Fixes #11402.
2021-12-28 23:08:50 +01:00
Andreas Kling
987b5adf2a Kernel: Remove old comment about kmalloc() being Q&D :^)
We've finally gotten kmalloc to a point where it feels decent enough
to drop this comment.

There's still a lot of room for improvement, and we'll continue working
on it.
2021-12-28 21:02:38 +01:00
Andreas Kling
9dffcc9752 Kernel: VERIFY that addresses passed to kfree_sized() look valid
Let's do some simple pointer arithmetic to verify that the address being
freed is at least within one of the two valid kmalloc VM ranges.
2021-12-28 21:02:38 +01:00
Andreas Kling
9111376d70 Kernel: Rename kmalloc_pool_heap => initial_kmalloc_memory 2021-12-28 21:02:38 +01:00
Andreas Kling
ac7ce12123 Kernel: Remove the kmalloc_eternal heap :^)
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-28 21:02:38 +01:00
Brian Gianforcaro
8b99fb26d9 Kernel: Use type alias for Kmalloc SubHeap and SlabBlock list types
We've moved to this pattern for the majority of usages of IntrusiveList
in the Kernel, might as well be consistent. :^)
2021-12-28 09:17:06 +01:00
Andreas Kling
63e1b904a4 Kernel: Scrub kmalloc slabs when allocated and deallocated
This matches the behavior of the generic subheaps (and the old slab
allocator implementation.)
2021-12-26 21:22:59 +01:00
Andreas Kling
3399b6c57f Kernel: Remove old SlabAllocator :^)
This is no longer useful since kmalloc() does automatic slab allocation
without any of the limitations of the old SlabAllocator. :^)
2021-12-26 21:22:59 +01:00
Andreas Kling
66d35f2936 Kernel: Add FIXME about allocation waste in kmalloc slabheap 2021-12-26 21:22:59 +01:00
Andreas Kling
2a5cff232b Kernel: Use slab allocation automagically for small kmalloc() requests
This patch adds generic slab allocators to kmalloc. In this initial
version, the slab sizes are 16, 32, 64, 128, 256 and 512 bytes.

Slabheaps are backed by 64 KiB block-aligned blocks with freelists,
similar to what we do in LibC malloc and LibJS Heap.
2021-12-26 21:22:59 +01:00
Andreas Kling
f6c594fa29 Kernel: Remove arbitrary alignment requirement from kmalloc_aligned()
We were not allowing alignments greater than PAGE_SIZE for some reason.
2021-12-26 21:22:59 +01:00
Andreas Kling
9182653a0f Kernel: Log purported size of bogus kfree_sized() requests 2021-12-26 21:22:59 +01:00
Andreas Kling
c6c786c992 Kernel: Remove kfree(), leaving only kfree_sized() :^)
There are no more users of the C-style kfree() API in the kernel,
so let's get rid of it and enjoy the new world where we always know
how much memory we are freeing. :^)
2021-12-26 21:22:59 +01:00
Andreas Kling
6eb48f7df6 Kernel: Consolidate kmalloc_aligned() and use kfree_sized() within
This patch does two things:

- Combines kmalloc_aligned() and kmalloc_aligned_cxx(). Templatizing
  the alignment parameter doesn't seem like a valuable enough
  optimization to justify having two almost-identical implementations.

- Stores the real allocation size of an aligned allocation along with
  the other alignment metadata, and uses it to call kfree_sized()
  instead of kfree().
2021-12-26 21:22:59 +01:00
Andreas Kling
83dd93ff13 Kernel: Use kfree_sized() in SlabAllocator 2021-12-26 21:22:59 +01:00
Idan Horowitz
7757d874ad Kernel: Assert that a KmallocSubheap fits inside a page
Since we allocate the subheap in the first page of the given storage
let's assert that the subheap can actually fit in a single page, to
prevent the possible future headache of trying to debug the cause of
random kernel memory corruption :^)
2021-12-26 11:26:39 +01:00