From 4916b5c1300573c4fca94a1c0caf3a757565add4 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 11 Mar 2021 14:12:55 +0100 Subject: [PATCH] Kernel: Move process thread lists into protected data --- Kernel/FileSystem/ProcFS.cpp | 2 +- Kernel/Process.cpp | 2 ++ Kernel/Process.h | 21 ++++++++++++++++++--- Kernel/Thread.h | 1 + 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index 7829b38e2f..675c3076f7 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -1344,7 +1344,7 @@ KResult ProcFSInode::traverse_as_directory(Functionfor_each_thread([&](Thread& thread) -> IterationDecision { + process->for_each_thread([&](const Thread& thread) -> IterationDecision { int tid = thread.tid().value(); callback({ String::number(tid), to_identifier_with_stack(fsid(), tid), 0 }); return IterationDecision::Continue; diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 0a01642a2d..3d74cf4dee 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -693,6 +693,7 @@ bool Process::create_perf_events_buffer_if_needed() bool Process::remove_thread(Thread& thread) { + ProtectedDataMutationScope scope { *this }; auto thread_cnt_before = m_thread_count.fetch_sub(1, AK::MemoryOrder::memory_order_acq_rel); VERIFY(thread_cnt_before != 0); ScopedSpinLock thread_list_lock(m_thread_list_lock); @@ -702,6 +703,7 @@ bool Process::remove_thread(Thread& thread) bool Process::add_thread(Thread& thread) { + ProtectedDataMutationScope scope { *this }; bool is_first = m_thread_count.fetch_add(1, AK::MemoryOrder::memory_order_relaxed) == 0; ScopedSpinLock thread_list_lock(m_thread_list_lock); m_thread_list.append(thread); diff --git a/Kernel/Process.h b/Kernel/Process.h index 0b6be73611..a1f37a3d93 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -119,6 +119,8 @@ protected: u32 m_execpromises { 0 }; mode_t m_umask { 022 }; VirtualAddress m_signal_trampoline; + Atomic m_thread_count { 0 }; + IntrusiveList m_thread_list; }; class ProcessBase : public ProtectedProcessBase { @@ -245,8 +247,11 @@ public: static void for_each_in_pgrp(ProcessGroupID, Callback); template void for_each_child(Callback); + template - IterationDecision for_each_thread(Callback) const; + IterationDecision for_each_thread(Callback); + template + IterationDecision for_each_thread(Callback callback) const; void die(); void finalize(); @@ -576,8 +581,6 @@ private: u8 m_termination_status { 0 }; u8 m_termination_signal { 0 }; - Atomic m_thread_count { 0 }; - mutable IntrusiveList m_thread_list; mutable RecursiveSpinLock m_thread_list_lock; const bool m_is_kernel_process; @@ -669,6 +672,18 @@ inline IterationDecision Process::for_each_thread(Callback callback) const return IterationDecision::Continue; } +template +inline IterationDecision Process::for_each_thread(Callback callback) +{ + ScopedSpinLock thread_list_lock(m_thread_list_lock); + for (auto& thread : m_thread_list) { + IterationDecision decision = callback(thread); + if (decision != IterationDecision::Continue) + return decision; + } + return IterationDecision::Continue; +} + template inline void Process::for_each_in_pgrp(ProcessGroupID pgid, Callback callback) { diff --git a/Kernel/Thread.h b/Kernel/Thread.h index 71c6e1526d..19176e136c 100644 --- a/Kernel/Thread.h +++ b/Kernel/Thread.h @@ -86,6 +86,7 @@ class Thread AK_MAKE_NONMOVABLE(Thread); friend class Process; + friend class ProtectedProcessBase; friend class Scheduler; friend class ThreadReadyQueue;