summaryrefslogtreecommitdiff
path: root/Kernel/Scheduler.cpp
AgeCommit message (Collapse)Author
2021-09-06Kernel: Make Threads always have a nameAndreas Kling
We previously allowed Thread to exist in a state where its m_name was null, and had to work around that in various places. This patch removes that possibility and forces those who would create a thread (or change the name of one) to provide a NonnullOwnPtr<KString> with the name.
2021-08-29Kernel: Rename Spinlock::is_owned_by_current_thread()Andreas Kling
...to is_owned_by_current_processor(). As Tom pointed out, this is much more accurate. :^)
2021-08-29Kernel: {Mutex,Spinlock}::own_lock() => is_locked_by_current_thread()Andreas Kling
Rename these API's to make it more clear what they are checking.
2021-08-29Kernel: Move "in-scheduler" flag from SchedulerData to ProcessorAndreas Kling
This avoids a race between getting the processor-specific SchedulerData and accessing it. (Switching to a different CPU in that window means that we're operating on the wrong SchedulerData.) Co-authored-by: Tom <tomut@yahoo.com>
2021-08-23Kernel: Rename Processor::id() => current_id()Andreas Kling
And let id() be the non-static version that gives you the ID of a Processor object.
2021-08-23Kernel: Convert Processor::in_irq() to static current_in_irq()Andreas Kling
This closes the race window between Processor::current() and a context switch happening before in_irq().
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-22Kernel: Rename SpinLockProtectedValue<T> => SpinLockProtected<T>Andreas Kling
2021-08-15Kernel: Lock thread list while in Thread::unref()Andreas Kling
This patch does three things: - Convert the global thread list from a HashMap to an IntrusiveList - Combine the thread list and its lock into a SpinLockProtectedValue - Customize Thread::unref() so it locks the list while unreffing This closes the same race window for Thread as @sin-ack's recent changes did for Process. Note that the HashMap->IntrusiveList conversion means that we lose O(1) lookups, but the majority of clients of this list are doing traversal, not lookup. Once we have an intrusive hashing solution, we should port this to use that, but for now, this gets rid of heap allocations during a sensitive time.
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-09Kernel: Only get register dump when we have a trapAndreas Kling
Co-authored-by: Tom <tomut@yahoo.com>
2021-08-08Kernel: Rename queue_runnable_thread() => enqueue_runnable_thread()Andreas Kling
2021-08-08Kernel: Port the scheduler's time tracking to SpinLockProtectedValueAndreas Kling
2021-08-08Kernel: Port the scheduler's ready queues to SpinLockProtectedValueAndreas Kling
2021-08-08Kernel: Simplify the per-CPU SchedulerData structAndreas Kling
2021-08-06Kernel: Move Kernel/Memory/ code into Kernel::Memory namespaceAndreas Kling
2021-08-06Kernel: Store Thread name as a KStringAndreas Kling
2021-07-27Kernel: Introduce ProcessorSpecific<T> for per-CPU data structuresAndreas Kling
To add a new per-CPU data structure, add an ID for it to the ProcessorSpecificDataID enum. Then call ProcessorSpecific<T>::initialize() when you are ready to construct the per-CPU data structure on the current CPU. It can then be accessed via ProcessorSpecific<T>::get(). This patch replaces the existing hard-coded mechanisms for Scheduler and MemoryManager per-CPU data structure.
2021-07-26Kernel: Make some debug logging in Scheduler CPU agnosticAndreas Kling
2021-07-26Kernel: Remove unused Scheduler::yield_from_critical()Andreas Kling
2021-07-26Kernel: Fix handful of clang-tidy warnings in SchedulerAndreas Kling
All of them "static member accessed through instance".
2021-07-22Everywhere: Prefix hexadecimal numbers with 0xGunnar Beutner
Depending on the values it might be difficult to figure out whether a value is decimal or hexadecimal. So let's make this more obvious. Also this allows copying and pasting those numbers into GNOME calculator and probably also other apps which auto-detect the base.
2021-07-19Kernel: Push ARCH specific ifdef's down into RegisterState functionsBrian Gianforcaro
The non CPU specific code of the kernel shouldn't need to deal with architecture specific registers, and should instead deal with an abstract view of the machine. This allows us to remove a variety of architecture specific ifdefs and helps keep the code slightly more portable. We do this by exposing the abstract representation of instruction pointer, stack pointer, base pointer, return register, etc on the RegisterState struct.
2021-07-18Everywhere: Make tracking cpu usage independent from system ticksTom
This switches tracking CPU usage to more accurately measure time in user and kernel land using either the TSC or another time source. This will also come in handy when implementing a tickless kernel mode.
2021-07-18Everywhere: Improve CPU usage calculationTom
As threads come and go, we can't simply account for how many time slices the threads at any given point may have been using. We need to also account for threads that have since disappeared. This means we also need to track how many time slices we have expired globally. However, because this doesn't account for context switches outside of the system timer tick values may still be under-reported. To solve this we will need to track more accurate time information on each context switch. This also fixes top's cpu usage calculation which was still based on the number of context switches. Fixes #6473
2021-07-18Kernel: Make SCHEDULER_DEBUG work on x86_64Gunnar Beutner
2021-07-15Kernel: Optionally dump scheduler state with stack tracesTom
This will dump stack traces of all threads when pressing Ctrl+Shift+Alt+F12
2021-07-13Kernel: Kill user mode threads that are marked to dieTom
Threads that don't make syscalls still need to be killed, and we can do that at any time we want so long the thread is in user mode and not somehow blocked (e.g. page fault).
2021-07-13Revert "Kernel: Make sure threads which don't do any syscalls are t..."Tom
This reverts commit 3c3a1726df847aff9db73862040d9f7a3b9fc907. We cannot blindly kill threads just because they're not executing in a system call. Being blocked (including in a page fault) needs proper unblocking and potentially kernel stack cleanup before we can mark a thread as Dying. Fixes #8691
2021-07-12Kernel: Initialize threading and process management earlierTom
This re-arranges the order of how things are initialized so that we try to initialize process and thread management earlier. This is neccessary because a lot of the code uses the Lock class, which really needs to have a running scheduler in place so that we can properly preempt. This also enables us to potentially initialize some things in parallel.
2021-07-12Kernel: Avoid unnecessary context switch when no other thread is readyTom
If no other thread is ready to be run we don't need to switch to the idle thread and wait for the next timer interrupt. We can just give the thread another timeslice and keep it running.
2021-07-11Kernel: Remove unused header includes in root kernel treeBrian Gianforcaro
2021-07-05Kernel+LibC: Remove sys$donate()Andreas Kling
This was an old SerenityOS-specific syscall for donating the remainder of the calling thread's time-slice to another thread within the same process. Now that Threading::Lock uses a pthread_mutex_t internally, we no longer need this syscall, which allows us to get rid of a surprising amount of unnecessary scheduler logic. :^)
2021-07-03Kernel: Fix always-true comparison warningsDaniel Bertalan
2021-06-28Kernel: Get Alt-Shift-F12 to work on x86_64Gunnar Beutner
2021-06-28Kernel: Fix spelling mistakeGunnar Beutner
2021-06-28Kernel: Implement more x86_64 context switching functionalityGunnar Beutner
2021-06-27Kernel: Rename Thread::tss to Thread::regs and add x86_64 supportGunnar Beutner
We're using software context switches so calling this struct tss is somewhat misleading.
2021-06-27Kernel+Userland: Add x86_64 registers to RegisterState/PtraceRegistersGunnar Beutner
2021-06-25Kernel: Add Scheduler::is_initializedSahan Fernando
2021-06-24Kernel: Add stubs for missing x86_64 functionalityGunnar Beutner
This adds just enough stubs to make the kernel compile on x86_64. Obviously it won't do anything useful - in fact it won't even attempt to boot because Multiboot doesn't support ELF64 binaries - but it gets those compiler errors out of the way so more progress can be made getting all the missing functionality in place.
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: Pull apart CPU.hHendiadyoin1
This does not add any functional changes
2021-06-19Kernel: Make sure threads which don't do any syscalls are terminatedGunnar Beutner
Steps to reproduce: $ cat loop.c int main() { for (;;); } $ gcc -o loop loop.c $ ./loop Terminating this process wasn't previously possible because we only checked whether the thread should be terminated on syscall exit.
2021-06-06Kernel: Don't finalize a thread while it still has code runningGunnar Beutner
After marking a thread for death we might end up finalizing the thread while it still has code to run, e.g. via: Thread::block -> Thread::dispatch_one_pending_signal -> Thread::dispatch_signal -> Process::terminate_due_to_signal -> Process::die -> Process::kill_all_threads -> Thread::set_should_die This marks the thread for death. It isn't destroyed at this point though. The scheduler then gets invoked via: Thread::block -> Thread::relock_process At that point we still have a registered blocker on the stack frame which belongs to Thread::block. Thread::relock_process drops the critical section which allows the scheduler to run. When the thread is then scheduled out the scheduler sets the thread state to Thread::Dying which allows the finalizer to destroy the Thread object and its associated resources including the kernel stack. This probably also affects objects other than blockers which rely on their destructor to be run, however the problem was most noticible because blockers are allocated on the stack of the dying thread and cause an access violation when another thread touches the blocker which belonged to the now-dead thread. Fixes #7823.
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-29Everywhere: Sort out superfluous QuickSort.h importsBen Wiederhake
They were sorta unneeded. :^)
2021-05-25Kernel: Validate we don't hold s_mm_lock during context switchBrian Gianforcaro
Since `s_mm_lock` is a RecursiveSpinlock, if a kernel thread gets preempted while accidentally hold the lock during switch_context, another thread running on the same processor could end up manipulating the state of the memory manager even though they should not be able to. It will just bump the recursion count and keep going. This appears to be the root cause of weird bugs like: #7359 Where page protection magically appears to be wrong during execution. To avoid these cases lets guard this specific unfortunate case and make sure it can never go unnoticed ever again. The assert was Tom's idea to help debug this, so I am going to tag him as co-author of this commit. Co-Authored-By: Tom <tomut@yahoo.com>
2021-05-19Kernel: Add support for profiling kmalloc()/kfree()Gunnar Beutner