From 5fd26b4ea83eafc35eb6120584fda29faa13fbb3 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 24 Mar 2023 01:17:39 +0200 Subject: [PATCH] Kernel: Lock process functions instead of the ata controller Process has to use locks at least on some functions so multithreaded disk io is safe. This seemed to fix corrupted reads from disk --- kernel/include/kernel/Process.h | 3 +++ kernel/include/kernel/Storage/ATAController.h | 2 -- kernel/kernel/Process.cpp | 15 ++++++++++++++- kernel/kernel/Storage/ATAController.cpp | 7 +------ 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index 5c580fbe00..0a74a38027 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -4,6 +4,7 @@ #include #include #include +#include #include namespace Kernel @@ -60,6 +61,8 @@ namespace Kernel BAN::Vector m_open_files; + mutable SpinLock m_lock; + pid_t m_pid = 0; BAN::String m_working_directory; BAN::Vector> m_threads; diff --git a/kernel/include/kernel/Storage/ATAController.h b/kernel/include/kernel/Storage/ATAController.h index fc6c8e9c9b..6fc054993f 100644 --- a/kernel/include/kernel/Storage/ATAController.h +++ b/kernel/include/kernel/Storage/ATAController.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include namespace Kernel @@ -68,7 +67,6 @@ namespace Kernel BAN::ErrorOr write(ATADevice*, uint64_t, uint8_t, const uint8_t*); private: - SpinLock m_lock; ATABus m_buses[2]; const PCIDevice& m_pci_device; diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 7e1a7e0062..8b3e6fc7e3 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -1,7 +1,8 @@ #include +#include +#include #include #include -#include #include @@ -37,6 +38,8 @@ namespace Kernel BAN::ErrorOr Process::open(BAN::StringView path, int flags) { + LockGuard _(m_lock); + if (flags != O_RDONLY) return BAN::Error::from_errno(ENOTSUP); @@ -56,6 +59,8 @@ namespace Kernel BAN::ErrorOr Process::close(int fd) { + LockGuard _(m_lock); + TRY(validate_fd(fd)); auto& open_file_description = this->open_file_description(fd); open_file_description.inode = nullptr; @@ -64,6 +69,8 @@ namespace Kernel BAN::ErrorOr Process::read(int fd, void* buffer, size_t count) { + LockGuard _(m_lock); + TRY(validate_fd(fd)); auto& open_file_description = this->open_file_description(fd); if (open_file_description.offset >= open_file_description.inode->size()) @@ -74,6 +81,8 @@ namespace Kernel BAN::ErrorOr Process::creat(BAN::StringView path, mode_t mode) { + LockGuard _(m_lock); + auto absolute_path = TRY(absolute_path_of(path)); while (absolute_path.sv().back() != '/') absolute_path.pop_back(); @@ -86,12 +95,16 @@ namespace Kernel Inode& Process::inode_for_fd(int fd) { + LockGuard _(m_lock); + MUST(validate_fd(fd)); return *open_file_description(fd).inode; } BAN::ErrorOr Process::set_working_directory(BAN::StringView path) { + LockGuard _(m_lock); + BAN::String absolute_path = TRY(absolute_path_of(path)); auto file = TRY(VirtualFileSystem::get().file_from_absolute_path(absolute_path)); diff --git a/kernel/kernel/Storage/ATAController.cpp b/kernel/kernel/Storage/ATAController.cpp index 06023e2fce..492fafef62 100644 --- a/kernel/kernel/Storage/ATAController.cpp +++ b/kernel/kernel/Storage/ATAController.cpp @@ -1,5 +1,4 @@ #include -#include #include #define ATA_PRIMARY 0 @@ -191,8 +190,6 @@ namespace Kernel if (lba + sector_count > device->lba_count) return BAN::Error::from_c_string("Attempted to read outside of the device boundaries"); - LockGuard _(m_lock); - if (lba < (1 << 28)) { // LBA28 @@ -223,8 +220,6 @@ namespace Kernel if (lba + sector_count > device->lba_count) return BAN::Error::from_c_string("Attempted to read outside of the device boundaries"); - LockGuard _(m_lock); - if (lba < (1 << 28)) { // LBA28 @@ -247,8 +242,8 @@ namespace Kernel ASSERT(false); } - device->bus->write(ATA_PORT_COMMAND, ATA_COMMAND_CACHE_FLUSH); TRY(device->bus->wait(false)); + device->bus->write(ATA_PORT_COMMAND, ATA_COMMAND_CACHE_FLUSH); return {}; }