Kernel: Fix all broken locks from new mutexes

This commit is contained in:
2024-02-28 22:39:02 +02:00
parent 1971813336
commit d94f6388b7
66 changed files with 681 additions and 647 deletions

View File

@@ -5,7 +5,7 @@
#include <kernel/Device/ZeroDevice.h>
#include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/FS/TmpFS/Inode.h>
#include <kernel/LockGuard.h>
#include <kernel/Lock/LockGuard.h>
#include <kernel/Process.h>
#include <kernel/Scheduler.h>
#include <kernel/Storage/StorageDevice.h>
@@ -56,12 +56,9 @@ namespace Kernel
sync_process->add_thread(MUST(Thread::create_kernel(
[](void*)
{
// NOTE: we lock the device lock here and unlock
// it only while semaphore is blocking
s_instance->m_device_lock.lock();
while (true)
{
LockGuard _(s_instance->m_device_lock);
while (!s_instance->m_should_sync)
{
LockFreeGuard _(s_instance->m_device_lock);

View File

@@ -1,6 +1,6 @@
#include <BAN/ScopeGuard.h>
#include <kernel/FS/Ext2/FileSystem.h>
#include <kernel/LockGuard.h>
#include <kernel/Lock/LockGuard.h>
#define EXT2_DEBUG_PRINT 0
#define EXT2_VERIFY_INODE 0
@@ -139,7 +139,7 @@ namespace Kernel
BAN::ErrorOr<uint32_t> Ext2FS::create_inode(const Ext2::Inode& ext2_inode)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
ASSERT(ext2_inode.size == 0);
@@ -218,7 +218,7 @@ namespace Kernel
void Ext2FS::delete_inode(uint32_t ino)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
ASSERT(ino >= superblock().first_ino);
ASSERT(ino <= superblock().inodes_count);
@@ -271,7 +271,7 @@ namespace Kernel
void Ext2FS::read_block(uint32_t block, BlockBufferWrapper& buffer)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
const uint32_t sector_size = m_block_device->blksize();
const uint32_t block_size = this->block_size();
@@ -284,7 +284,7 @@ namespace Kernel
void Ext2FS::write_block(uint32_t block, const BlockBufferWrapper& buffer)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
const uint32_t sector_size = m_block_device->blksize();
const uint32_t block_size = this->block_size();
@@ -297,7 +297,7 @@ namespace Kernel
void Ext2FS::sync_superblock()
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
const uint32_t sector_size = m_block_device->blksize();
ASSERT(1024 % sector_size == 0);
@@ -322,13 +322,13 @@ namespace Kernel
Ext2FS::BlockBufferWrapper Ext2FS::get_block_buffer()
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
return m_buffer_manager.get_buffer();
}
BAN::ErrorOr<uint32_t> Ext2FS::reserve_free_block(uint32_t primary_bgd)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (m_superblock.r_blocks_count >= m_superblock.free_blocks_count)
return BAN::Error::from_errno(ENOSPC);
@@ -389,7 +389,7 @@ namespace Kernel
void Ext2FS::release_block(uint32_t block)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
ASSERT(block >= m_superblock.first_data_block);
ASSERT(block < m_superblock.blocks_count);
@@ -422,7 +422,7 @@ namespace Kernel
Ext2FS::BlockLocation Ext2FS::locate_inode(uint32_t ino)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
ASSERT(ino <= superblock().inodes_count);
@@ -464,7 +464,7 @@ namespace Kernel
Ext2FS::BlockLocation Ext2FS::locate_block_group_descriptior(uint32_t group_index)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
const uint32_t block_size = this->block_size();

View File

@@ -1,5 +1,5 @@
#include <kernel/FS/Inode.h>
#include <kernel/LockGuard.h>
#include <kernel/Lock/LockGuard.h>
#include <fcntl.h>
@@ -58,13 +58,13 @@ namespace Kernel
void Inode::on_close()
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
on_close_impl();
}
BAN::ErrorOr<BAN::RefPtr<Inode>> Inode::find_inode(BAN::StringView name)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (!mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
return find_inode_impl(name);
@@ -72,7 +72,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::list_next_inodes(off_t offset, DirectoryEntryList* list, size_t list_len)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (!mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
return list_next_inodes_impl(offset, list, list_len);
@@ -80,7 +80,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::create_file(BAN::StringView name, mode_t mode, uid_t uid, gid_t gid)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (!this->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
if (Mode(mode).ifdir())
@@ -90,7 +90,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::create_directory(BAN::StringView name, mode_t mode, uid_t uid, gid_t gid)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (!this->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
if (!Mode(mode).ifdir())
@@ -100,7 +100,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::unlink(BAN::StringView name)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (!mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
if (name == "."sv || name == ".."sv)
@@ -110,7 +110,7 @@ namespace Kernel
BAN::ErrorOr<BAN::String> Inode::link_target()
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (!mode().iflnk())
return BAN::Error::from_errno(EINVAL);
return link_target_impl();
@@ -118,7 +118,7 @@ namespace Kernel
BAN::ErrorOr<long> Inode::accept(sockaddr* address, socklen_t* address_len)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return accept_impl(address, address_len);
@@ -126,7 +126,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::bind(const sockaddr* address, socklen_t address_len)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return bind_impl(address, address_len);
@@ -134,7 +134,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::connect(const sockaddr* address, socklen_t address_len)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return connect_impl(address, address_len);
@@ -142,7 +142,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::listen(int backlog)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return listen_impl(backlog);
@@ -150,7 +150,7 @@ namespace Kernel
BAN::ErrorOr<size_t> Inode::sendto(BAN::ConstByteSpan message, const sockaddr* address, socklen_t address_len)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return sendto_impl(message, address, address_len);
@@ -158,7 +158,7 @@ namespace Kernel
BAN::ErrorOr<size_t> Inode::recvfrom(BAN::ByteSpan buffer, sockaddr* address, socklen_t* address_len)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return recvfrom_impl(buffer, address, address_len);
@@ -166,7 +166,7 @@ namespace Kernel
BAN::ErrorOr<size_t> Inode::read(off_t offset, BAN::ByteSpan buffer)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (mode().ifdir())
return BAN::Error::from_errno(EISDIR);
return read_impl(offset, buffer);
@@ -174,7 +174,7 @@ namespace Kernel
BAN::ErrorOr<size_t> Inode::write(off_t offset, BAN::ConstByteSpan buffer)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (mode().ifdir())
return BAN::Error::from_errno(EISDIR);
return write_impl(offset, buffer);
@@ -182,7 +182,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::truncate(size_t size)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (mode().ifdir())
return BAN::Error::from_errno(EISDIR);
return truncate_impl(size);
@@ -191,37 +191,37 @@ namespace Kernel
BAN::ErrorOr<void> Inode::chmod(mode_t mode)
{
ASSERT((mode & Inode::Mode::TYPE_MASK) == 0);
LockGuard _(m_lock);
LockGuard _(m_mutex);
return chmod_impl(mode);
}
BAN::ErrorOr<void> Inode::chown(uid_t uid, gid_t gid)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
return chown_impl(uid, gid);
}
bool Inode::can_read() const
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
return can_read_impl();
}
bool Inode::can_write() const
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
return can_write_impl();
}
bool Inode::has_error() const
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
return has_error_impl();
}
BAN::ErrorOr<long> Inode::ioctl(int request, void* arg)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
return ioctl_impl(request, arg);
}

View File

@@ -1,5 +1,5 @@
#include <kernel/FS/Pipe.h>
#include <kernel/LockGuard.h>
#include <kernel/Lock/LockGuard.h>
#include <kernel/Thread.h>
#include <kernel/Timer/Timer.h>
@@ -26,14 +26,14 @@ namespace Kernel
void Pipe::clone_writing()
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
ASSERT(m_writing_count > 0);
m_writing_count++;
}
void Pipe::close_writing()
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
ASSERT(m_writing_count > 0);
m_writing_count--;
if (m_writing_count == 0)
@@ -42,12 +42,12 @@ namespace Kernel
BAN::ErrorOr<size_t> Pipe::read_impl(off_t, BAN::ByteSpan buffer)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
while (m_buffer.empty())
{
if (m_writing_count == 0)
return 0;
LockFreeGuard lock_free(m_lock);
LockFreeGuard lock_free(m_mutex);
TRY(Thread::current().block_or_eintr_indefinite(m_semaphore));
}
@@ -66,7 +66,7 @@ namespace Kernel
BAN::ErrorOr<size_t> Pipe::write_impl(off_t, BAN::ConstByteSpan buffer)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
size_t old_size = m_buffer.size();

View File

@@ -1,6 +1,6 @@
#include <kernel/FS/ProcFS/FileSystem.h>
#include <kernel/FS/ProcFS/Inode.h>
#include <kernel/LockGuard.h>
#include <kernel/Lock/LockGuard.h>
namespace Kernel
{

View File

@@ -66,7 +66,7 @@ namespace Kernel
BAN::ErrorOr<BAN::RefPtr<TmpInode>> TmpFileSystem::open_inode(ino_t ino)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (m_inode_cache.contains(ino))
return m_inode_cache[ino];
@@ -85,7 +85,7 @@ namespace Kernel
BAN::ErrorOr<void> TmpFileSystem::add_to_cache(BAN::RefPtr<TmpInode> inode)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (!m_inode_cache.contains(inode->ino()))
TRY(m_inode_cache.insert(inode->ino(), inode));
@@ -94,7 +94,7 @@ namespace Kernel
void TmpFileSystem::remove_from_cache(BAN::RefPtr<TmpInode> inode)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
ASSERT(m_inode_cache.contains(inode->ino()));
m_inode_cache.remove(inode->ino());
@@ -102,7 +102,7 @@ namespace Kernel
void TmpFileSystem::read_inode(ino_t ino, TmpInodeInfo& out)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
auto inode_location = find_inode(ino);
PageTable::with_fast_page(inode_location.paddr, [&] {
@@ -112,7 +112,7 @@ namespace Kernel
void TmpFileSystem::write_inode(ino_t ino, const TmpInodeInfo& info)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
auto inode_location = find_inode(ino);
PageTable::with_fast_page(inode_location.paddr, [&] {
@@ -123,7 +123,7 @@ namespace Kernel
void TmpFileSystem::delete_inode(ino_t ino)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
auto inode_location = find_inode(ino);
PageTable::with_fast_page(inode_location.paddr, [&] {
@@ -138,7 +138,7 @@ namespace Kernel
BAN::ErrorOr<ino_t> TmpFileSystem::allocate_inode(const TmpInodeInfo& info)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
constexpr size_t inodes_per_page = PAGE_SIZE / sizeof(TmpInodeInfo);
@@ -164,7 +164,7 @@ namespace Kernel
TmpFileSystem::InodeLocation TmpFileSystem::find_inode(ino_t ino)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
ASSERT_GTE(ino, first_inode);
ASSERT_LT(ino, max_inodes);
@@ -182,7 +182,7 @@ namespace Kernel
void TmpFileSystem::free_block(size_t index)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
constexpr size_t addresses_per_page = PAGE_SIZE / sizeof(PageInfo);
@@ -204,7 +204,7 @@ namespace Kernel
BAN::ErrorOr<size_t> TmpFileSystem::allocate_block()
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
size_t result = first_data_page;
TRY(for_each_indirect_paddr_allocating(m_data_pages, [&] (paddr_t, bool allocated) {
@@ -218,7 +218,7 @@ namespace Kernel
paddr_t TmpFileSystem::find_block(size_t index)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
ASSERT_GT(index, 0);
return find_indirect(m_data_pages, index - first_data_page, 3);
@@ -226,7 +226,7 @@ namespace Kernel
paddr_t TmpFileSystem::find_indirect(PageInfo root, size_t index, size_t depth)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
ASSERT(root.flags() & PageInfo::Flags::Present);
if (depth == 0)
@@ -257,7 +257,7 @@ namespace Kernel
template<TmpFuncs::for_each_indirect_paddr_allocating_callback F>
BAN::ErrorOr<BAN::Iteration> TmpFileSystem::for_each_indirect_paddr_allocating_internal(PageInfo page_info, F callback, size_t depth)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
ASSERT(page_info.flags() & PageInfo::Flags::Present);
if (depth == 0)
@@ -316,7 +316,7 @@ namespace Kernel
template<TmpFuncs::for_each_indirect_paddr_allocating_callback F>
BAN::ErrorOr<void> TmpFileSystem::for_each_indirect_paddr_allocating(PageInfo page_info, F callback, size_t depth)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
BAN::Iteration result = TRY(for_each_indirect_paddr_allocating_internal(page_info, callback, depth));
ASSERT(result == BAN::Iteration::Break);

View File

@@ -5,7 +5,7 @@
#include <kernel/FS/ProcFS/FileSystem.h>
#include <kernel/FS/TmpFS/FileSystem.h>
#include <kernel/FS/VirtualFileSystem.h>
#include <kernel/LockGuard.h>
#include <kernel/Lock/LockGuard.h>
#include <fcntl.h>
namespace Kernel
@@ -63,15 +63,14 @@ namespace Kernel
if (!file.inode->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
LockGuard _(m_lock);
LockGuard _(m_mutex);
TRY(m_mount_points.push_back({ file, file_system }));
return {};
}
VirtualFileSystem::MountPoint* VirtualFileSystem::mount_from_host_inode(BAN::RefPtr<Inode> inode)
{
ASSERT(m_lock.is_locked());
LockGuard _(m_mutex);
for (MountPoint& mount : m_mount_points)
if (*mount.host.inode == *inode)
return &mount;
@@ -80,7 +79,7 @@ namespace Kernel
VirtualFileSystem::MountPoint* VirtualFileSystem::mount_from_root_inode(BAN::RefPtr<Inode> inode)
{
ASSERT(m_lock.is_locked());
LockGuard _(m_mutex);
for (MountPoint& mount : m_mount_points)
if (*mount.target->root_inode() == *inode)
return &mount;
@@ -89,7 +88,7 @@ namespace Kernel
BAN::ErrorOr<VirtualFileSystem::File> VirtualFileSystem::file_from_absolute_path(const Credentials& credentials, BAN::StringView path, int flags)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
ASSERT(path.front() == '/');