Kernel: Protect access to PerformanceEventBuffer strings with spinlock
This commit is contained in:
parent
a0bcc9dd83
commit
c184a0786f
Notes:
sideshowbarker
2024-07-17 08:13:43 +09:00
Author: https://github.com/jakubberkop Commit: https://github.com/SerenityOS/serenity/commit/c184a0786f Pull-request: https://github.com/SerenityOS/serenity/pull/19950 Reviewed-by: https://github.com/AtkinsSJ
2 changed files with 26 additions and 17 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2023, Jakub Berkop <jakub.berkop@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -192,19 +193,24 @@ template<typename Serializer>
|
|||
ErrorOr<void> PerformanceEventBuffer::to_json_impl(Serializer& object) const
|
||||
{
|
||||
{
|
||||
auto strings = TRY(object.add_array("strings"sv));
|
||||
auto strings_object = TRY(object.add_array("strings"sv));
|
||||
Vector<KString*> strings_sorted_by_index;
|
||||
TRY(strings_sorted_by_index.try_resize(m_strings.size()));
|
||||
|
||||
for (auto& entry : m_strings) {
|
||||
strings_sorted_by_index[entry.value] = const_cast<Kernel::KString*>(entry.key.ptr());
|
||||
}
|
||||
TRY(m_strings.with([&](auto& strings) -> ErrorOr<void> {
|
||||
TRY(strings_sorted_by_index.try_resize(strings.size()));
|
||||
|
||||
for (size_t i = 0; i < m_strings.size(); i++) {
|
||||
TRY(strings.add(strings_sorted_by_index[i]->view()));
|
||||
}
|
||||
for (auto& entry : strings) {
|
||||
strings_sorted_by_index[entry.value] = const_cast<Kernel::KString*>(entry.key.ptr());
|
||||
}
|
||||
|
||||
TRY(strings.finish());
|
||||
for (size_t i = 0; i < strings.size(); i++) {
|
||||
TRY(strings_object.add(strings_sorted_by_index[i]->view()));
|
||||
}
|
||||
|
||||
return {};
|
||||
}));
|
||||
|
||||
TRY(strings_object.finish());
|
||||
}
|
||||
|
||||
auto current_process_credentials = Process::current().credentials();
|
||||
|
@ -367,14 +373,16 @@ ErrorOr<void> PerformanceEventBuffer::add_process(Process const& process, Proces
|
|||
|
||||
ErrorOr<FlatPtr> PerformanceEventBuffer::register_string(NonnullOwnPtr<KString> string)
|
||||
{
|
||||
auto it = m_strings.find(string);
|
||||
if (it != m_strings.end()) {
|
||||
return it->value;
|
||||
}
|
||||
return m_strings.with([&](auto& m_strings) -> ErrorOr<FlatPtr> {
|
||||
auto it = m_strings.find(string);
|
||||
if (it != m_strings.end()) {
|
||||
return it->value;
|
||||
}
|
||||
|
||||
auto new_index = m_strings.size();
|
||||
TRY(m_strings.try_set(move(string), move(new_index)));
|
||||
return new_index;
|
||||
auto new_index = m_strings.size();
|
||||
TRY(m_strings.try_set(move(string), move(new_index)));
|
||||
return new_index;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2023, Jakub Berkop <jakub.berkop@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -145,7 +146,7 @@ private:
|
|||
size_t m_count { 0 };
|
||||
NonnullOwnPtr<KBuffer> m_buffer;
|
||||
|
||||
HashMap<NonnullOwnPtr<KString>, size_t> m_strings;
|
||||
SpinlockProtected<HashMap<NonnullOwnPtr<KString>, size_t>, LockRank::None> m_strings;
|
||||
};
|
||||
|
||||
extern bool g_profiling_all_threads;
|
||||
|
|
Loading…
Add table
Reference in a new issue