From 8001493df3d3f5c49e39e48c1575f86658302551 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 22 Apr 2023 15:46:23 +0300 Subject: [PATCH] Kernel: Fix some deadlocks in the Process --- kernel/kernel/Process.cpp | 42 ++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index f0824a9c..02a1019a 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -220,20 +220,24 @@ namespace Kernel BAN::ErrorOr Process::write(int fd, const void* buffer, size_t count) { - m_lock.lock(); - TRY(validate_fd(fd)); - auto open_fd_copy = open_file_description(fd); - m_lock.unlock(); + OpenFileDescription open_fd_copy; + + { + LockGuard _(m_lock); + TRY(validate_fd(fd)); + open_fd_copy = open_file_description(fd); + } if (!(open_fd_copy.flags & O_WRONLY)) return BAN::Error::from_errno(EBADF); size_t n_written = TRY(open_fd_copy.inode->write(open_fd_copy.offset, buffer, count)); open_fd_copy.offset += n_written; - m_lock.lock(); - MUST(validate_fd(fd)); - open_file_description(fd) = open_fd_copy; - m_lock.unlock(); + { + LockGuard _(m_lock); + MUST(validate_fd(fd)); + open_file_description(fd) = open_fd_copy; + } return n_written; } @@ -266,10 +270,13 @@ namespace Kernel BAN::ErrorOr Process::fstat(int fd, struct stat* out) { - m_lock.lock(); - TRY(validate_fd(fd)); - auto open_fd_copy = open_file_description(fd); - m_lock.unlock(); + OpenFileDescription open_fd_copy; + + { + LockGuard _(m_lock); + TRY(validate_fd(fd)); + open_fd_copy = open_file_description(fd); + } out->st_dev = open_fd_copy.inode->dev(); out->st_ino = open_fd_copy.inode->ino(); @@ -298,10 +305,13 @@ namespace Kernel BAN::ErrorOr> Process::read_directory_entries(int fd) { - m_lock.lock(); - TRY(validate_fd(fd)); - auto open_fd_copy = open_file_description(fd); - m_lock.unlock(); + OpenFileDescription open_fd_copy; + + { + LockGuard _(m_lock); + TRY(validate_fd(fd)); + open_fd_copy = open_file_description(fd); + } auto result = TRY(open_fd_copy.inode->read_directory_entries(open_fd_copy.offset)); open_fd_copy.offset++;