1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:47:35 +00:00

Kernel: Make self-contained locking smart pointers their own classes

Until now, our kernel has reimplemented a number of AK classes to
provide automatic internal locking:

- RefPtr
- NonnullRefPtr
- WeakPtr
- Weakable

This patch renames the Kernel classes so that they can coexist with
the original AK classes:

- RefPtr => LockRefPtr
- NonnullRefPtr => NonnullLockRefPtr
- WeakPtr => LockWeakPtr
- Weakable => LockWeakable

The goal here is to eventually get rid of the Lock* classes in favor of
using external locking.
This commit is contained in:
Andreas Kling 2022-08-19 20:53:40 +02:00
parent e475263113
commit 11eee67b85
360 changed files with 1703 additions and 1672 deletions

View file

@ -31,7 +31,7 @@ ErrorOr<FlatPtr> Process::sys$alarm(unsigned seconds)
auto deadline = TimeManagement::the().current_time(CLOCK_REALTIME_COARSE);
deadline = deadline + Time::from_seconds(seconds);
if (!m_alarm_timer) {
m_alarm_timer = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Timer));
m_alarm_timer = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) Timer));
}
auto timer_was_added = TimerQueue::the().add_timer_without_id(*m_alarm_timer, CLOCK_REALTIME_COARSE, deadline, [this]() {
MUST(send_signal(SIGALRM, nullptr));

View file

@ -15,10 +15,10 @@ ErrorOr<FlatPtr> Process::sys$chdir(Userspace<char const*> user_path, size_t pat
VERIFY_NO_PROCESS_BIG_LOCK(this);
TRY(require_promise(Pledge::rpath));
auto path = TRY(get_syscall_path_argument(user_path, path_length));
auto current_directory = m_current_directory.with([](auto& current_directory) -> NonnullRefPtr<Custody> {
auto current_directory = m_current_directory.with([](auto& current_directory) -> NonnullLockRefPtr<Custody> {
return *current_directory;
});
RefPtr<Custody> new_directory = TRY(VirtualFileSystem::the().open_directory(path->view(), *current_directory));
LockRefPtr<Custody> new_directory = TRY(VirtualFileSystem::the().open_directory(path->view(), *current_directory));
m_current_directory.with([&](auto& current_directory) {
// NOTE: We use swap() here to avoid manipulating the ref counts while holding the lock.
swap(current_directory, new_directory);

View file

@ -18,7 +18,7 @@ ErrorOr<FlatPtr> Process::sys$chmod(Userspace<Syscall::SC_chmod_params const*> u
auto params = TRY(copy_typed_from_user(user_params));
auto path = TRY(get_syscall_path_argument(params.path));
RefPtr<Custody> base;
LockRefPtr<Custody> base;
if (params.dirfd == AT_FDCWD) {
base = current_directory();
} else {

View file

@ -4,9 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/NonnullRefPtrVector.h>
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/OpenFileDescription.h>
#include <Kernel/Library/NonnullLockRefPtrVector.h>
#include <Kernel/Process.h>
namespace Kernel {
@ -27,7 +27,7 @@ ErrorOr<FlatPtr> Process::sys$chown(Userspace<Syscall::SC_chown_params const*> u
auto params = TRY(copy_typed_from_user(user_params));
auto path = TRY(get_syscall_path_argument(params.path));
RefPtr<Custody> base;
LockRefPtr<Custody> base;
if (params.dirfd == AT_FDCWD) {
base = current_directory();
} else {

View file

@ -7,10 +7,10 @@
#include <AK/ScopeGuard.h>
#include <AK/TemporaryChange.h>
#include <AK/WeakPtr.h>
#include <Kernel/Debug.h>
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/OpenFileDescription.h>
#include <Kernel/Library/LockWeakPtr.h>
#include <Kernel/Memory/AllocationStrategy.h>
#include <Kernel/Memory/MemoryManager.h>
#include <Kernel/Memory/PageDirectory.h>
@ -36,10 +36,10 @@ struct LoadResult {
FlatPtr load_base { 0 };
FlatPtr entry_eip { 0 };
size_t size { 0 };
WeakPtr<Memory::Region> tls_region;
LockWeakPtr<Memory::Region> tls_region;
size_t tls_size { 0 };
size_t tls_alignment { 0 };
WeakPtr<Memory::Region> stack_region;
LockWeakPtr<Memory::Region> stack_region;
};
static constexpr size_t auxiliary_vector_size = 15;
@ -410,8 +410,8 @@ static ErrorOr<LoadResult> load_elf_object(NonnullOwnPtr<Memory::AddressSpace> n
}
ErrorOr<LoadResult>
Process::load(NonnullRefPtr<OpenFileDescription> main_program_description,
RefPtr<OpenFileDescription> interpreter_description, const ElfW(Ehdr) & main_program_header)
Process::load(NonnullLockRefPtr<OpenFileDescription> main_program_description,
LockRefPtr<OpenFileDescription> interpreter_description, const ElfW(Ehdr) & main_program_header)
{
auto new_space = TRY(Memory::AddressSpace::try_create(nullptr));
@ -458,8 +458,8 @@ void Process::clear_signal_handlers_for_exec()
}
}
ErrorOr<void> Process::do_exec(NonnullRefPtr<OpenFileDescription> main_program_description, NonnullOwnPtrVector<KString> arguments, NonnullOwnPtrVector<KString> environment,
RefPtr<OpenFileDescription> interpreter_description, Thread*& new_main_thread, u32& prev_flags, const ElfW(Ehdr) & main_program_header)
ErrorOr<void> Process::do_exec(NonnullLockRefPtr<OpenFileDescription> main_program_description, NonnullOwnPtrVector<KString> arguments, NonnullOwnPtrVector<KString> environment,
LockRefPtr<OpenFileDescription> interpreter_description, Thread*& new_main_thread, u32& prev_flags, const ElfW(Ehdr) & main_program_header)
{
VERIFY(is_user_process());
VERIFY(!Processor::in_critical());
@ -726,7 +726,7 @@ static ErrorOr<NonnullOwnPtrVector<KString>> find_shebang_interpreter_for_execut
return ENOEXEC;
}
ErrorOr<RefPtr<OpenFileDescription>> Process::find_elf_interpreter_for_executable(StringView path, ElfW(Ehdr) const& main_executable_header, size_t main_executable_header_size, size_t file_size)
ErrorOr<LockRefPtr<OpenFileDescription>> Process::find_elf_interpreter_for_executable(StringView path, ElfW(Ehdr) const& main_executable_header, size_t main_executable_header_size, size_t file_size)
{
// Not using ErrorOr here because we'll want to do the same thing in userspace in the RTLD
StringBuilder interpreter_path_builder;

View file

@ -17,7 +17,7 @@ ErrorOr<FlatPtr> Process::sys$fork(RegisterState& regs)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
TRY(require_promise(Pledge::proc));
RefPtr<Thread> child_first_thread;
LockRefPtr<Thread> child_first_thread;
ArmedScopeGuard thread_finalizer_guard = [&child_first_thread]() {
SpinlockLocker lock(g_scheduler_lock);

View file

@ -13,7 +13,7 @@
namespace Kernel {
static Singleton<SpinlockProtected<HashMap<GlobalFutexKey, NonnullRefPtr<FutexQueue>>>> s_global_futex_queues;
static Singleton<SpinlockProtected<HashMap<GlobalFutexKey, NonnullLockRefPtr<FutexQueue>>>> s_global_futex_queues;
void Process::clear_futex_queues_on_exec()
{
@ -122,16 +122,16 @@ ErrorOr<FlatPtr> Process::sys$futex(Userspace<Syscall::SC_futex_params const*> u
}
}
auto find_futex_queue = [&](GlobalFutexKey futex_key, bool create_if_not_found, bool* did_create = nullptr) -> ErrorOr<RefPtr<FutexQueue>> {
auto find_futex_queue = [&](GlobalFutexKey futex_key, bool create_if_not_found, bool* did_create = nullptr) -> ErrorOr<LockRefPtr<FutexQueue>> {
VERIFY(!create_if_not_found || did_create != nullptr);
return s_global_futex_queues->with([&](auto& queues) -> ErrorOr<RefPtr<FutexQueue>> {
return s_global_futex_queues->with([&](auto& queues) -> ErrorOr<LockRefPtr<FutexQueue>> {
auto it = queues.find(futex_key);
if (it != queues.end())
return it->value;
if (!create_if_not_found)
return nullptr;
*did_create = true;
auto futex_queue = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) FutexQueue));
auto futex_queue = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) FutexQueue));
auto result = TRY(queues.try_set(futex_key, futex_queue));
VERIFY(result == AK::HashSetResult::InsertedNewEntry);
return futex_queue;
@ -169,7 +169,7 @@ ErrorOr<FlatPtr> Process::sys$futex(Userspace<Syscall::SC_futex_params const*> u
auto do_wait = [&](u32 bitset) -> ErrorOr<FlatPtr> {
bool did_create;
RefPtr<FutexQueue> futex_queue;
LockRefPtr<FutexQueue> futex_queue;
auto futex_key = TRY(get_futex_key(user_address, shared));
do {
auto user_value = user_atomic_load_relaxed(params.userspace_address);
@ -216,7 +216,7 @@ ErrorOr<FlatPtr> Process::sys$futex(Userspace<Syscall::SC_futex_params const*> u
if (!futex_queue)
return 0;
RefPtr<FutexQueue> target_futex_queue;
LockRefPtr<FutexQueue> target_futex_queue;
bool is_empty = false;
bool is_target_empty = false;
auto futex_key2 = TRY(get_futex_key(user_address2, shared));

View file

@ -203,7 +203,7 @@ ErrorOr<FlatPtr> Process::sys$mmap(Userspace<Syscall::SC_mmap_params const*> use
if (map_anonymous) {
auto strategy = map_noreserve ? AllocationStrategy::None : AllocationStrategy::Reserve;
RefPtr<Memory::AnonymousVMObject> vmobject;
LockRefPtr<Memory::AnonymousVMObject> vmobject;
if (flags & MAP_PURGEABLE) {
vmobject = TRY(Memory::AnonymousVMObject::try_create_purgeable_with_size(rounded_size, strategy));
} else {
@ -459,7 +459,7 @@ ErrorOr<FlatPtr> Process::sys$mremap(Userspace<Syscall::SC_mremap_params const*>
auto range = old_region->range();
auto old_prot = region_access_flags_to_prot(old_region->access());
auto old_offset = old_region->offset_in_vmobject();
NonnullRefPtr inode = static_cast<Memory::SharedInodeVMObject&>(old_region->vmobject()).inode();
NonnullLockRefPtr inode = static_cast<Memory::SharedInodeVMObject&>(old_region->vmobject()).inode();
auto new_vmobject = TRY(Memory::PrivateInodeVMObject::try_create_with_inode(inode));
auto old_name = old_region->take_name();

View file

@ -24,8 +24,8 @@ struct FileSystemInitializer {
bool requires_open_file_description { false };
bool requires_block_device { false };
bool requires_seekable_file { false };
ErrorOr<NonnullRefPtr<FileSystem>> (*create_with_fd)(OpenFileDescription&) = nullptr;
ErrorOr<NonnullRefPtr<FileSystem>> (*create)(void) = nullptr;
ErrorOr<NonnullLockRefPtr<FileSystem>> (*create_with_fd)(OpenFileDescription&) = nullptr;
ErrorOr<NonnullLockRefPtr<FileSystem>> (*create)(void) = nullptr;
};
static constexpr FileSystemInitializer s_initializers[] = {
@ -39,14 +39,14 @@ static constexpr FileSystemInitializer s_initializers[] = {
{ "iso9660"sv, "ISO9660FS"sv, true, true, true, ISO9660FS::try_create, {} },
};
static ErrorOr<NonnullRefPtr<FileSystem>> create_filesystem_instance(StringView fs_type, OpenFileDescription* possible_description)
static ErrorOr<NonnullLockRefPtr<FileSystem>> create_filesystem_instance(StringView fs_type, OpenFileDescription* possible_description)
{
for (auto& initializer_entry : s_initializers) {
if (fs_type != initializer_entry.short_name && fs_type != initializer_entry.name)
continue;
if (!initializer_entry.requires_open_file_description) {
VERIFY(initializer_entry.create);
NonnullRefPtr<FileSystem> fs = TRY(initializer_entry.create());
NonnullLockRefPtr<FileSystem> fs = TRY(initializer_entry.create());
return fs;
}
VERIFY(initializer_entry.create_with_fd);
@ -60,7 +60,7 @@ static ErrorOr<NonnullRefPtr<FileSystem>> create_filesystem_instance(StringView
dbgln("mount: this is not a seekable file");
return ENODEV;
}
NonnullRefPtr<FileSystem> fs = TRY(initializer_entry.create_with_fd(description));
NonnullLockRefPtr<FileSystem> fs = TRY(initializer_entry.create_with_fd(description));
return fs;
}
return ENODEV;
@ -107,7 +107,7 @@ ErrorOr<FlatPtr> Process::sys$mount(Userspace<Syscall::SC_mount_params const*> u
return 0;
}
RefPtr<FileSystem> fs;
LockRefPtr<FileSystem> fs;
if (!description_or_error.is_error()) {
auto description = description_or_error.release_value();

View file

@ -54,7 +54,7 @@ ErrorOr<FlatPtr> Process::sys$open(Userspace<Syscall::SC_open_params const*> use
dbgln_if(IO_DEBUG, "sys$open(dirfd={}, path='{}', options={}, mode={})", dirfd, path->view(), options, mode);
auto fd_allocation = TRY(allocate_fd());
RefPtr<Custody> base;
LockRefPtr<Custody> base;
if (dirfd == AT_FDCWD) {
base = current_directory();
} else {

View file

@ -50,7 +50,7 @@ ErrorOr<FlatPtr> Process::sys$poll(Userspace<Syscall::SC_poll_params const*> use
TRY(m_fds.with_shared([&](auto& fds) -> ErrorOr<void> {
for (size_t i = 0; i < params.nfds; i++) {
auto& pfd = fds_copy[i];
RefPtr<OpenFileDescription> description;
LockRefPtr<OpenFileDescription> description;
auto description_or_error = fds.open_file_description(pfd.fd);
if (!description_or_error.is_error())
description = description_or_error.release_value();

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/NonnullRefPtrVector.h>
#include <Kernel/Library/NonnullLockRefPtrVector.h>
#include <Kernel/Memory/AnonymousVMObject.h>
#include <Kernel/Memory/InodeVMObject.h>
#include <Kernel/Memory/MemoryManager.h>
@ -20,7 +20,7 @@ ErrorOr<FlatPtr> Process::sys$purge(int mode)
return EPERM;
size_t purged_page_count = 0;
if (mode & PURGE_ALL_VOLATILE) {
NonnullRefPtrVector<Memory::AnonymousVMObject> vmobjects;
NonnullLockRefPtrVector<Memory::AnonymousVMObject> vmobjects;
{
ErrorOr<void> result;
Memory::MemoryManager::for_each_vmobject([&](auto& vmobject) {
@ -43,7 +43,7 @@ ErrorOr<FlatPtr> Process::sys$purge(int mode)
}
}
if (mode & PURGE_ALL_CLEAN_INODE) {
NonnullRefPtrVector<Memory::InodeVMObject> vmobjects;
NonnullLockRefPtrVector<Memory::InodeVMObject> vmobjects;
{
ErrorOr<void> result;
Memory::MemoryManager::for_each_vmobject([&](auto& vmobject) {

View file

@ -13,7 +13,7 @@ namespace Kernel {
using BlockFlags = Thread::FileBlocker::BlockFlags;
static ErrorOr<NonnullRefPtr<OpenFileDescription>> open_readable_file_description(auto& fds, int fd)
static ErrorOr<NonnullLockRefPtr<OpenFileDescription>> open_readable_file_description(auto& fds, int fd)
{
auto description = TRY(fds.with_shared([&](auto& fds) { return fds.open_file_description(fd); }));
if (!description->is_readable())

View file

@ -19,7 +19,7 @@ namespace Kernel {
TRY(require_promise(Pledge::unix)); \
} while (0)
static void setup_socket_fd(Process::OpenFileDescriptions& fds, int fd, NonnullRefPtr<OpenFileDescription> description, int type)
static void setup_socket_fd(Process::OpenFileDescriptions& fds, int fd, NonnullLockRefPtr<OpenFileDescription> description, int type)
{
description->set_readable(true);
description->set_writable(true);
@ -92,7 +92,7 @@ ErrorOr<FlatPtr> Process::sys$accept4(Userspace<Syscall::SC_accept4_params const
TRY(copy_from_user(&address_size, static_ptr_cast<socklen_t const*>(user_address_size)));
ScopedDescriptionAllocation fd_allocation;
RefPtr<OpenFileDescription> accepting_socket_description;
LockRefPtr<OpenFileDescription> accepting_socket_description;
TRY(m_fds.with_exclusive([&](auto& fds) -> ErrorOr<void> {
fd_allocation = TRY(fds.allocate());
@ -103,7 +103,7 @@ ErrorOr<FlatPtr> Process::sys$accept4(Userspace<Syscall::SC_accept4_params const
return ENOTSOCK;
auto& socket = *accepting_socket_description->socket();
RefPtr<Socket> accepted_socket;
LockRefPtr<Socket> accepted_socket;
for (;;) {
accepted_socket = socket.accept();
if (accepted_socket)

View file

@ -4,9 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/NonnullRefPtrVector.h>
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/Library/NonnullLockRefPtrVector.h>
#include <Kernel/Process.h>
namespace Kernel {
@ -29,7 +29,7 @@ ErrorOr<FlatPtr> Process::sys$stat(Userspace<Syscall::SC_stat_params const*> use
auto path = TRY(get_syscall_path_argument(params.path));
RefPtr<Custody> base;
LockRefPtr<Custody> base;
if (params.dirfd == AT_FDCWD) {
base = current_directory();
} else {

View file

@ -19,7 +19,7 @@ ErrorOr<FlatPtr> Process::sys$unlink(int dirfd, Userspace<char const*> user_path
if (flags & ~AT_REMOVEDIR)
return Error::from_errno(EINVAL);
RefPtr<Custody> base;
LockRefPtr<Custody> base;
if (dirfd == AT_FDCWD) {
base = current_directory();
} else {

View file

@ -79,7 +79,7 @@ ErrorOr<FlatPtr> Process::sys$unveil(Userspace<Syscall::SC_unveil_params const*>
// However, if the user specified unveil() with "c" permissions, we don't set errno if ENOENT is encountered,
// because they most likely intend the program to create the file for them later on.
// If this case is encountered, the parent node of the path is returned and the custody of that inode is used instead.
RefPtr<Custody> parent_custody; // Parent inode in case of ENOENT
LockRefPtr<Custody> parent_custody; // Parent inode in case of ENOENT
OwnPtr<KString> new_unveiled_path;
auto custody_or_error = VirtualFileSystem::the().resolve_path_without_veil(path->view(), VirtualFileSystem::the().root_custody(), &parent_custody);
if (!custody_or_error.is_error()) {

View file

@ -40,8 +40,8 @@ ErrorOr<FlatPtr> Process::sys$utimensat(Userspace<Syscall::SC_utimensat_params c
}
OwnPtr<KString> path;
RefPtr<OpenFileDescription> description;
RefPtr<Custody> base;
LockRefPtr<OpenFileDescription> description;
LockRefPtr<Custody> base;
auto path_or_error = get_syscall_path_argument(params.path);
if (path_or_error.is_error()) {

View file

@ -10,7 +10,7 @@
namespace Kernel {
ErrorOr<siginfo_t> Process::do_waitid(Variant<Empty, NonnullRefPtr<Process>, NonnullRefPtr<ProcessGroup>> waitee, int options)
ErrorOr<siginfo_t> Process::do_waitid(Variant<Empty, NonnullLockRefPtr<Process>, NonnullLockRefPtr<ProcessGroup>> waitee, int options)
{
ErrorOr<siginfo_t> result = siginfo_t {};
if (Thread::current()->block<Thread::WaitBlocker>({}, options, move(waitee), result).was_interrupted())
@ -25,7 +25,7 @@ ErrorOr<FlatPtr> Process::sys$waitid(Userspace<Syscall::SC_waitid_params const*>
TRY(require_promise(Pledge::proc));
auto params = TRY(copy_typed_from_user(user_params));
Variant<Empty, NonnullRefPtr<Process>, NonnullRefPtr<ProcessGroup>> waitee;
Variant<Empty, NonnullLockRefPtr<Process>, NonnullLockRefPtr<ProcessGroup>> waitee;
switch (params.idtype) {
case P_ALL:
break;