From a6a439243f3101e5d2f75f54b8aa6bc4c8b2e323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Wed, 9 Nov 2022 11:39:58 +0100 Subject: [PATCH] Kernel: Turn lock ranks into template parameters This step would ideally not have been necessary (increases amount of refactoring and templates necessary, which in turn increases build times), but it gives us a couple of nice properties: - SpinlockProtected inside Singleton (a very common combination) can now obtain any lock rank just via the template parameter. It was not previously possible to do this with SingletonInstanceCreator magic. - SpinlockProtected's lock rank is now mandatory; this is the majority of cases and allows us to see where we're still missing proper ranks. - The type already informs us what lock rank a lock has, which aids code readability and (possibly, if gdb cooperates) lock mismatch debugging. - The rank of a lock can no longer be dynamic, which is not something we wanted in the first place (or made use of). Locks randomly changing their rank sounds like a disaster waiting to happen. - In some places, we might be able to statically check that locks are taken in the right order (with the right lock rank checking implementation) as rank information is fully statically known. This refactoring even more exposes the fact that Mutex has no lock rank capabilites, which is not fixed here. --- AK/Singleton.h | 9 ++- Kernel/Arch/x86_64/ISABus/I8042Controller.h | 2 +- Kernel/Arch/x86_64/PageDirectory.cpp | 2 +- Kernel/Arch/x86_64/VGA/IOArbiter.h | 2 +- Kernel/Bus/PCI/Access.h | 8 +-- .../PCI/Controller/VolumeManagementDevice.h | 2 +- Kernel/Bus/USB/UHCI/UHCIController.cpp | 2 - Kernel/Bus/USB/UHCI/UHCIController.h | 4 +- Kernel/Bus/USB/UHCI/UHCIDescriptorPool.h | 3 +- Kernel/Bus/VirtIO/Queue.h | 4 +- Kernel/CMakeLists.txt | 1 - Kernel/Coredump.cpp | 4 +- Kernel/Coredump.h | 2 +- Kernel/Devices/AsyncDeviceRequest.h | 4 +- Kernel/Devices/Audio/AC97.h | 2 +- Kernel/Devices/ConsoleDevice.cpp | 2 +- Kernel/Devices/ConsoleDevice.h | 2 +- Kernel/Devices/Device.h | 2 +- Kernel/Devices/DeviceManagement.h | 4 +- Kernel/Devices/HID/HIDManagement.h | 6 +- Kernel/Devices/HID/KeyboardDevice.h | 2 +- Kernel/Devices/HID/MouseDevice.h | 2 +- Kernel/Devices/KCOVInstance.h | 4 +- Kernel/Devices/SerialDevice.h | 2 +- Kernel/FileSystem/Custody.cpp | 4 +- Kernel/FileSystem/Custody.h | 2 +- Kernel/FileSystem/FileSystem.h | 4 +- Kernel/FileSystem/Inode.cpp | 4 +- Kernel/FileSystem/Inode.h | 6 +- Kernel/FileSystem/Mount.cpp | 4 +- Kernel/FileSystem/Mount.h | 2 +- Kernel/FileSystem/OpenFileDescription.h | 2 +- Kernel/FileSystem/Plan9FS/FileSystem.h | 6 +- Kernel/FileSystem/SysFS/Component.cpp | 2 +- Kernel/FileSystem/SysFS/Component.h | 4 +- Kernel/FileSystem/SysFS/Registry.h | 2 +- .../SysFS/Subsystems/Bus/USB/BusDirectory.h | 2 +- .../Kernel/Variables/CapsLockRemap.h | 2 +- .../Kernel/Variables/DumpKmallocStack.h | 2 +- Kernel/FileSystem/VirtualFileSystem.cpp | 1 - Kernel/FileSystem/VirtualFileSystem.h | 8 +-- Kernel/Forward.h | 4 ++ .../Graphics/Console/BootFramebufferConsole.h | 2 +- .../Console/GenericFramebufferConsole.h | 2 +- Kernel/Graphics/Console/VGATextModeConsole.h | 2 +- Kernel/Graphics/DisplayConnector.h | 6 +- Kernel/Graphics/GraphicsManagement.h | 2 +- .../Graphics/Intel/NativeDisplayConnector.h | 2 +- Kernel/Graphics/VMWare/GraphicsAdapter.h | 4 +- Kernel/Graphics/VirtIOGPU/GraphicsAdapter.h | 4 +- Kernel/Heap/kmalloc.cpp | 2 +- Kernel/Jail.h | 4 +- Kernel/JailManagement.h | 2 +- Kernel/Locking/Mutex.cpp | 2 +- Kernel/Locking/Mutex.h | 7 +- Kernel/Locking/Spinlock.cpp | 66 ------------------- Kernel/Locking/Spinlock.h | 65 +++++++++++++++--- Kernel/Locking/SpinlockProtected.h | 12 ++-- Kernel/Memory/AnonymousVMObject.h | 2 +- Kernel/Memory/MemoryManager.cpp | 1 - Kernel/Memory/MemoryManager.h | 4 +- Kernel/Memory/PageDirectory.h | 4 +- Kernel/Memory/Region.cpp | 2 +- Kernel/Memory/Region.h | 3 +- Kernel/Memory/RingBuffer.h | 4 +- Kernel/Memory/SharedFramebufferVMObject.h | 2 +- Kernel/Memory/VMObject.cpp | 4 +- Kernel/Memory/VMObject.h | 4 +- Kernel/Net/NetworkAdapter.h | 2 +- Kernel/Net/NetworkingManagement.h | 2 +- Kernel/Net/Routing.cpp | 8 +-- Kernel/Net/Routing.h | 4 +- Kernel/Process.cpp | 16 ++--- Kernel/Process.h | 32 ++++----- Kernel/ProcessExposed.cpp | 2 +- Kernel/ProcessGroup.cpp | 4 +- Kernel/ProcessGroup.h | 2 +- Kernel/Random.h | 4 +- Kernel/Scheduler.cpp | 6 +- Kernel/Scheduler.h | 2 +- Kernel/Storage/ATA/AHCI/Controller.h | 2 +- Kernel/Storage/ATA/AHCI/Port.h | 2 +- Kernel/Storage/ATA/ATAPort.h | 2 +- Kernel/Storage/NVMe/NVMeQueue.h | 6 +- Kernel/Syscalls/futex.cpp | 2 +- Kernel/TTY/ConsoleManagement.h | 6 +- Kernel/TTY/PTYMultiplexer.h | 2 +- Kernel/TTY/SlavePTY.cpp | 4 +- Kernel/TTY/SlavePTY.h | 2 +- Kernel/Thread.cpp | 6 +- Kernel/Thread.h | 18 ++--- Kernel/TimerQueue.cpp | 2 +- Kernel/WorkQueue.h | 2 +- Kernel/kprintf.cpp | 2 +- 94 files changed, 235 insertions(+), 259 deletions(-) delete mode 100644 Kernel/Locking/Spinlock.cpp diff --git a/AK/Singleton.h b/AK/Singleton.h index ed104befd03..626ba07b915 100644 --- a/AK/Singleton.h +++ b/AK/Singleton.h @@ -39,12 +39,11 @@ struct SingletonInstanceCreator { #ifdef KERNEL -// FIXME: Find a nice way of injecting the lock rank into the singleton. -template -struct SingletonInstanceCreator> { - static Kernel::SpinlockProtected* create() +template +struct SingletonInstanceCreator> { + static Kernel::SpinlockProtected* create() { - return new Kernel::SpinlockProtected { Kernel::LockRank::None }; + return new Kernel::SpinlockProtected {}; } }; #endif diff --git a/Kernel/Arch/x86_64/ISABus/I8042Controller.h b/Kernel/Arch/x86_64/ISABus/I8042Controller.h index 997cf4517d8..e62b5517a30 100644 --- a/Kernel/Arch/x86_64/ISABus/I8042Controller.h +++ b/Kernel/Arch/x86_64/ISABus/I8042Controller.h @@ -153,7 +153,7 @@ private: void do_write(u8 port, u8 data); u8 do_read(u8 port); - Spinlock m_lock { LockRank::None }; + Spinlock m_lock {}; bool m_first_port_available { false }; bool m_second_port_available { false }; bool m_is_dual_channel { false }; diff --git a/Kernel/Arch/x86_64/PageDirectory.cpp b/Kernel/Arch/x86_64/PageDirectory.cpp index 1efe016508c..89bfc638135 100644 --- a/Kernel/Arch/x86_64/PageDirectory.cpp +++ b/Kernel/Arch/x86_64/PageDirectory.cpp @@ -12,7 +12,7 @@ namespace Kernel::Memory { struct CR3Map { - SpinlockProtected> map { LockRank::None }; + SpinlockProtected, LockRank::None> map {}; }; static Singleton s_cr3_map; diff --git a/Kernel/Arch/x86_64/VGA/IOArbiter.h b/Kernel/Arch/x86_64/VGA/IOArbiter.h index 711f8324d8a..70bcf4dc78e 100644 --- a/Kernel/Arch/x86_64/VGA/IOArbiter.h +++ b/Kernel/Arch/x86_64/VGA/IOArbiter.h @@ -33,7 +33,7 @@ private: void disable_vga_text_mode_console_cursor(); void enable_vga_text_mode_console_cursor(); - RecursiveSpinlock m_main_vga_lock { LockRank::None }; + RecursiveSpinlock m_main_vga_lock {}; bool m_vga_access_is_disabled { false }; }; diff --git a/Kernel/Bus/PCI/Access.h b/Kernel/Bus/PCI/Access.h index 81959c87507..e0fd8a924b4 100644 --- a/Kernel/Bus/PCI/Access.h +++ b/Kernel/Bus/PCI/Access.h @@ -41,8 +41,8 @@ public: u32 read32_field(Address address, u32 field); DeviceIdentifier get_device_identifier(Address address) const; - Spinlock const& scan_lock() const { return m_scan_lock; } - RecursiveSpinlock const& access_lock() const { return m_access_lock; } + Spinlock const& scan_lock() const { return m_scan_lock; } + RecursiveSpinlock const& access_lock() const { return m_access_lock; } ErrorOr add_host_controller_and_enumerate_attached_devices(NonnullOwnPtr, Function callback); @@ -57,8 +57,8 @@ private: Vector get_capabilities(Address); Optional get_capabilities_pointer(Address address); - mutable RecursiveSpinlock m_access_lock { LockRank::None }; - mutable Spinlock m_scan_lock { LockRank::None }; + mutable RecursiveSpinlock m_access_lock {}; + mutable Spinlock m_scan_lock {}; HashMap> m_host_controllers; Vector m_device_identifiers; diff --git a/Kernel/Bus/PCI/Controller/VolumeManagementDevice.h b/Kernel/Bus/PCI/Controller/VolumeManagementDevice.h index a5baad70d73..5e016601ff4 100644 --- a/Kernel/Bus/PCI/Controller/VolumeManagementDevice.h +++ b/Kernel/Bus/PCI/Controller/VolumeManagementDevice.h @@ -29,7 +29,7 @@ private: // Note: All read and writes must be done with a spinlock because // Linux says that CPU might deadlock otherwise if access is not serialized. - Spinlock m_config_lock { LockRank::None }; + Spinlock m_config_lock {}; }; } diff --git a/Kernel/Bus/USB/UHCI/UHCIController.cpp b/Kernel/Bus/USB/UHCI/UHCIController.cpp index 17e0df4344f..d9867968ded 100644 --- a/Kernel/Bus/USB/UHCI/UHCIController.cpp +++ b/Kernel/Bus/USB/UHCI/UHCIController.cpp @@ -90,8 +90,6 @@ UNMAP_AFTER_INIT UHCIController::UHCIController(PCI::DeviceIdentifier const& pci : PCI::Device(pci_device_identifier.address()) , IRQHandler(pci_device_identifier.interrupt_line().value()) , m_registers_io_window(move(registers_io_window)) - , m_async_lock(LockRank::None) - , m_schedule_lock(LockRank::None) { } diff --git a/Kernel/Bus/USB/UHCI/UHCIController.h b/Kernel/Bus/USB/UHCI/UHCIController.h index ccbd218e6fc..1b45461cd5b 100644 --- a/Kernel/Bus/USB/UHCI/UHCIController.h +++ b/Kernel/Bus/USB/UHCI/UHCIController.h @@ -100,8 +100,8 @@ private: NonnullOwnPtr m_registers_io_window; - Spinlock m_async_lock; - Spinlock m_schedule_lock; + Spinlock m_async_lock {}; + Spinlock m_schedule_lock {}; OwnPtr m_root_hub; OwnPtr> m_queue_head_pool; diff --git a/Kernel/Bus/USB/UHCI/UHCIDescriptorPool.h b/Kernel/Bus/USB/UHCI/UHCIDescriptorPool.h index dc9b5d87de1..e1dbb41e57e 100644 --- a/Kernel/Bus/USB/UHCI/UHCIDescriptorPool.h +++ b/Kernel/Bus/USB/UHCI/UHCIDescriptorPool.h @@ -70,7 +70,6 @@ private: UHCIDescriptorPool(NonnullOwnPtr pool_memory_block, StringView name) : m_pool_name(name) , m_pool_region(move(pool_memory_block)) - , m_pool_lock(LockRank::None) { // Go through the number of descriptors to create in the pool, and create a virtual/physical address mapping for (size_t i = 0; i < PAGE_SIZE / sizeof(T); i++) { @@ -84,7 +83,7 @@ private: StringView m_pool_name; // Name of this pool NonnullOwnPtr m_pool_region; // Memory region where descriptors actually reside Stack m_free_descriptor_stack; // Stack of currently free descriptor pointers - Spinlock m_pool_lock; + Spinlock m_pool_lock; }; } diff --git a/Kernel/Bus/VirtIO/Queue.h b/Kernel/Bus/VirtIO/Queue.h index 6563779866f..26edc622f30 100644 --- a/Kernel/Bus/VirtIO/Queue.h +++ b/Kernel/Bus/VirtIO/Queue.h @@ -47,7 +47,7 @@ public: QueueChain pop_used_buffer_chain(size_t& used); void discard_used_buffers(); - Spinlock& lock() { return m_lock; } + Spinlock& lock() { return m_lock; } bool should_notify() const; @@ -96,7 +96,7 @@ private: QueueDriver* m_driver { nullptr }; QueueDevice* m_device { nullptr }; NonnullOwnPtr m_queue_region; - Spinlock m_lock { LockRank::None }; + Spinlock m_lock {}; friend class QueueChain; }; diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index a14c4cc833e..fc1ff6795ce 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -236,7 +236,6 @@ set(KERNEL_SOURCES MiniStdLib.cpp Locking/LockRank.cpp Locking/Mutex.cpp - Locking/Spinlock.cpp Net/Intel/E1000ENetworkAdapter.cpp Net/Intel/E1000NetworkAdapter.cpp Net/NE2000/NetworkAdapter.cpp diff --git a/Kernel/Coredump.cpp b/Kernel/Coredump.cpp index 3ffd60b1334..ea36e602701 100644 --- a/Kernel/Coredump.cpp +++ b/Kernel/Coredump.cpp @@ -23,11 +23,11 @@ #define INCLUDE_USERSPACE_HEAP_MEMORY_IN_COREDUMPS 0 -static Singleton>> s_coredump_directory_path; +static Singleton, LockRank::None>> s_coredump_directory_path; namespace Kernel { -SpinlockProtected>& Coredump::directory_path() +SpinlockProtected, LockRank::None>& Coredump::directory_path() { return s_coredump_directory_path; } diff --git a/Kernel/Coredump.h b/Kernel/Coredump.h index d1acea68d8c..2b9f7127f7f 100644 --- a/Kernel/Coredump.h +++ b/Kernel/Coredump.h @@ -19,7 +19,7 @@ namespace Kernel { class Coredump { public: static ErrorOr> try_create(NonnullLockRefPtr, StringView output_path); - static SpinlockProtected>& directory_path(); + static SpinlockProtected, LockRank::None>& directory_path(); ~Coredump() = default; ErrorOr write(); diff --git a/Kernel/Devices/AsyncDeviceRequest.h b/Kernel/Devices/AsyncDeviceRequest.h index 79c94f2e86a..449b2f9d84d 100644 --- a/Kernel/Devices/AsyncDeviceRequest.h +++ b/Kernel/Devices/AsyncDeviceRequest.h @@ -62,7 +62,7 @@ public: [[nodiscard]] RequestWaitResult wait(Time* = nullptr); - void do_start(SpinlockLocker&& requests_lock) + void do_start(SpinlockLocker>&& requests_lock) { if (is_completed_result(m_result)) return; @@ -151,7 +151,7 @@ private: WaitQueue m_queue; NonnullLockRefPtr m_process; void* m_private { nullptr }; - mutable Spinlock m_lock { LockRank::None }; + mutable Spinlock m_lock {}; }; } diff --git a/Kernel/Devices/Audio/AC97.h b/Kernel/Devices/Audio/AC97.h index 9ad53a8d182..092b9115bae 100644 --- a/Kernel/Devices/Audio/AC97.h +++ b/Kernel/Devices/Audio/AC97.h @@ -147,7 +147,7 @@ private: NonnullOwnPtr m_channel_io_window; PCI::Address m_device_pci_address; - SpinlockProtected m_dma_running { LockRank::None, false }; + SpinlockProtected m_dma_running { false }; StringView m_name; }; diff --git a/Kernel/Devices/ConsoleDevice.cpp b/Kernel/Devices/ConsoleDevice.cpp index 92044f852b7..c68c8fe1ffe 100644 --- a/Kernel/Devices/ConsoleDevice.cpp +++ b/Kernel/Devices/ConsoleDevice.cpp @@ -16,7 +16,7 @@ namespace Kernel { -Spinlock g_console_lock { LockRank::None }; +Spinlock g_console_lock {}; UNMAP_AFTER_INIT NonnullLockRefPtr ConsoleDevice::must_create() { diff --git a/Kernel/Devices/ConsoleDevice.h b/Kernel/Devices/ConsoleDevice.h index 3800a58baa0..d9eb6377f7d 100644 --- a/Kernel/Devices/ConsoleDevice.h +++ b/Kernel/Devices/ConsoleDevice.h @@ -12,7 +12,7 @@ namespace Kernel { -extern Spinlock g_console_lock; +extern Spinlock g_console_lock; class ConsoleDevice final : public CharacterDevice { friend class DeviceManagement; diff --git a/Kernel/Devices/Device.h b/Kernel/Devices/Device.h index dcfb263fd7d..4a1e51c2191 100644 --- a/Kernel/Devices/Device.h +++ b/Kernel/Devices/Device.h @@ -90,7 +90,7 @@ private: State m_state { State::Normal }; - Spinlock m_requests_lock { LockRank::None }; + Spinlock m_requests_lock {}; DoublyLinkedList> m_requests; protected: diff --git a/Kernel/Devices/DeviceManagement.h b/Kernel/Devices/DeviceManagement.h index 4d4d71cca9e..d96a4d2a934 100644 --- a/Kernel/Devices/DeviceManagement.h +++ b/Kernel/Devices/DeviceManagement.h @@ -74,9 +74,9 @@ private: LockRefPtr m_console_device; LockRefPtr m_device_control_device; // FIXME: Once we have a singleton for managing many sound cards, remove this from here - SpinlockProtected> m_devices { LockRank::None }; + SpinlockProtected, LockRank::None> m_devices {}; - mutable Spinlock m_event_queue_lock { LockRank::None }; + mutable Spinlock m_event_queue_lock {}; CircularQueue m_event_queue; }; diff --git a/Kernel/Devices/HID/HIDManagement.h b/Kernel/Devices/HID/HIDManagement.h index c63c868cc49..bf1d17f5b1e 100644 --- a/Kernel/Devices/HID/HIDManagement.h +++ b/Kernel/Devices/HID/HIDManagement.h @@ -47,7 +47,7 @@ public: Keyboard::CharacterMapData character_map; }; - SpinlockProtected& keymap_data() { return m_keymap_data; } + SpinlockProtected& keymap_data() { return m_keymap_data; } u32 get_char_from_character_map(KeyEvent) const; @@ -58,7 +58,7 @@ private: size_t generate_minor_device_number_for_mouse(); size_t generate_minor_device_number_for_keyboard(); - SpinlockProtected m_keymap_data { LockRank::None }; + SpinlockProtected m_keymap_data {}; size_t m_mouse_minor_number { 0 }; size_t m_keyboard_minor_number { 0 }; KeyboardClient* m_client { nullptr }; @@ -66,7 +66,7 @@ private: LockRefPtr m_i8042_controller; #endif NonnullLockRefPtrVector m_hid_devices; - Spinlock m_client_lock { LockRank::None }; + Spinlock m_client_lock {}; }; class KeyboardClient { diff --git a/Kernel/Devices/HID/KeyboardDevice.h b/Kernel/Devices/HID/KeyboardDevice.h index 95152f42523..85896e9bf1d 100644 --- a/Kernel/Devices/HID/KeyboardDevice.h +++ b/Kernel/Devices/HID/KeyboardDevice.h @@ -45,7 +45,7 @@ public: protected: KeyboardDevice(); - mutable Spinlock m_queue_lock { LockRank::None }; + mutable Spinlock m_queue_lock {}; CircularQueue m_queue; // ^CharacterDevice virtual StringView class_name() const override { return "KeyboardDevice"sv; } diff --git a/Kernel/Devices/HID/MouseDevice.h b/Kernel/Devices/HID/MouseDevice.h index 68f57c66dc2..f32fa11d9f3 100644 --- a/Kernel/Devices/HID/MouseDevice.h +++ b/Kernel/Devices/HID/MouseDevice.h @@ -35,7 +35,7 @@ protected: // ^CharacterDevice virtual StringView class_name() const override { return "MouseDevice"sv; } - mutable Spinlock m_queue_lock { LockRank::None }; + mutable Spinlock m_queue_lock {}; CircularQueue m_queue; }; diff --git a/Kernel/Devices/KCOVInstance.h b/Kernel/Devices/KCOVInstance.h index 6958de0b8af..a5c97cca08d 100644 --- a/Kernel/Devices/KCOVInstance.h +++ b/Kernel/Devices/KCOVInstance.h @@ -46,7 +46,7 @@ public: Memory::VMObject* vmobject() { return m_vmobject; } - Spinlock& spinlock() { return m_lock; } + Spinlock& spinlock() { return m_lock; } private: ProcessID m_pid { 0 }; @@ -58,7 +58,7 @@ private: // Here to ensure it's not garbage collected at the end of open() OwnPtr m_kernel_region; - Spinlock m_lock { LockRank::None }; + Spinlock m_lock {}; }; } diff --git a/Kernel/Devices/SerialDevice.h b/Kernel/Devices/SerialDevice.h index 11448aca61b..3d1866dd95d 100644 --- a/Kernel/Devices/SerialDevice.h +++ b/Kernel/Devices/SerialDevice.h @@ -130,7 +130,7 @@ private: bool m_break_enable { false }; u8 m_modem_control { 0 }; bool m_last_put_char_was_carriage_return { false }; - Spinlock m_serial_lock { LockRank::None }; + Spinlock m_serial_lock {}; }; } diff --git a/Kernel/FileSystem/Custody.cpp b/Kernel/FileSystem/Custody.cpp index 0c40a26197f..bf949344134 100644 --- a/Kernel/FileSystem/Custody.cpp +++ b/Kernel/FileSystem/Custody.cpp @@ -14,9 +14,9 @@ namespace Kernel { -static Singleton> s_all_instances; +static Singleton> s_all_instances; -SpinlockProtected& Custody::all_instances() +SpinlockProtected& Custody::all_instances() { return s_all_instances; } diff --git a/Kernel/FileSystem/Custody.h b/Kernel/FileSystem/Custody.h index f72cf071c64..8d6edba1766 100644 --- a/Kernel/FileSystem/Custody.h +++ b/Kernel/FileSystem/Custody.h @@ -44,7 +44,7 @@ private: public: using AllCustodiesList = IntrusiveList<&Custody::m_all_custodies_list_node>; - static SpinlockProtected& all_instances(); + static SpinlockProtected& all_instances(); }; } diff --git a/Kernel/FileSystem/FileSystem.h b/Kernel/FileSystem/FileSystem.h index 39ae416ca68..4c6c1253c70 100644 --- a/Kernel/FileSystem/FileSystem.h +++ b/Kernel/FileSystem/FileSystem.h @@ -61,7 +61,7 @@ public: // Converts file types that are used internally by the filesystem to DT_* types virtual u8 internal_file_type_to_directory_entry_type(DirectoryEntryView const& entry) const { return entry.file_type; } - SpinlockProtected& mounted_count(Badge) { return m_attach_count; } + SpinlockProtected& mounted_count(Badge) { return m_attach_count; } protected: FileSystem(); @@ -79,7 +79,7 @@ private: size_t m_fragment_size { 0 }; bool m_readonly { false }; - SpinlockProtected m_attach_count { LockRank::FileSystem, 0 }; + SpinlockProtected m_attach_count { 0 }; IntrusiveListNode m_file_system_node; }; diff --git a/Kernel/FileSystem/Inode.cpp b/Kernel/FileSystem/Inode.cpp index a5581b323d5..e6b8f1ef140 100644 --- a/Kernel/FileSystem/Inode.cpp +++ b/Kernel/FileSystem/Inode.cpp @@ -22,9 +22,9 @@ namespace Kernel { -static Singleton> s_all_instances; +static Singleton> s_all_instances; -SpinlockProtected& Inode::all_instances() +SpinlockProtected& Inode::all_instances() { return s_all_instances; } diff --git a/Kernel/FileSystem/Inode.h b/Kernel/FileSystem/Inode.h index cafa6630d47..c954de927ac 100644 --- a/Kernel/FileSystem/Inode.h +++ b/Kernel/FileSystem/Inode.h @@ -132,7 +132,7 @@ private: InodeIndex m_index { 0 }; LockWeakPtr m_shared_vmobject; LockRefPtr m_bound_socket; - SpinlockProtected> m_watchers { LockRank::None }; + SpinlockProtected, LockRank::None> m_watchers {}; bool m_metadata_dirty { false }; LockRefPtr m_fifo; IntrusiveListNode m_inode_list_node; @@ -146,11 +146,11 @@ private: }; Thread::FlockBlockerSet m_flock_blocker_set; - SpinlockProtected> m_flocks { LockRank::None }; + SpinlockProtected, LockRank::None> m_flocks {}; public: using AllInstancesList = IntrusiveList<&Inode::m_inode_list_node>; - static SpinlockProtected& all_instances(); + static SpinlockProtected& all_instances(); }; } diff --git a/Kernel/FileSystem/Mount.cpp b/Kernel/FileSystem/Mount.cpp index 733147d7469..b123b78ddc9 100644 --- a/Kernel/FileSystem/Mount.cpp +++ b/Kernel/FileSystem/Mount.cpp @@ -14,7 +14,7 @@ namespace Kernel { Mount::Mount(FileSystem& guest_fs, Custody* host_custody, int flags) : m_guest(guest_fs.root_inode()) , m_guest_fs(guest_fs) - , m_host_custody(LockRank::None, host_custody) + , m_host_custody(host_custody) , m_flags(flags) { } @@ -22,7 +22,7 @@ Mount::Mount(FileSystem& guest_fs, Custody* host_custody, int flags) Mount::Mount(Inode& source, Custody& host_custody, int flags) : m_guest(source) , m_guest_fs(source.fs()) - , m_host_custody(LockRank::None, host_custody) + , m_host_custody(host_custody) , m_flags(flags) { } diff --git a/Kernel/FileSystem/Mount.h b/Kernel/FileSystem/Mount.h index 6d4dc6c6086..d0676a797e0 100644 --- a/Kernel/FileSystem/Mount.h +++ b/Kernel/FileSystem/Mount.h @@ -40,7 +40,7 @@ public: private: NonnullLockRefPtr m_guest; NonnullLockRefPtr m_guest_fs; - SpinlockProtected> m_host_custody; + SpinlockProtected, LockRank::None> m_host_custody; int m_flags; IntrusiveListNode m_vfs_list_node; diff --git a/Kernel/FileSystem/OpenFileDescription.h b/Kernel/FileSystem/OpenFileDescription.h index bb6edce3d9d..c7972c1d3f0 100644 --- a/Kernel/FileSystem/OpenFileDescription.h +++ b/Kernel/FileSystem/OpenFileDescription.h @@ -157,6 +157,6 @@ private: FIFO::Direction fifo_direction : 2 { FIFO::Direction::Neither }; }; - SpinlockProtected m_state { LockRank::None }; + SpinlockProtected m_state {}; }; } diff --git a/Kernel/FileSystem/Plan9FS/FileSystem.h b/Kernel/FileSystem/Plan9FS/FileSystem.h index 1f5c4af3222..3b4ee0c0cc8 100644 --- a/Kernel/FileSystem/Plan9FS/FileSystem.h +++ b/Kernel/FileSystem/Plan9FS/FileSystem.h @@ -63,11 +63,11 @@ private: private: Plan9FS& m_fs; - mutable Spinlock m_lock { LockRank::None }; + mutable Spinlock m_lock {}; }; struct ReceiveCompletion final : public AtomicRefCounted { - mutable Spinlock lock { LockRank::None }; + mutable Spinlock lock {}; bool completed { false }; const u16 tag; OwnPtr message; @@ -136,7 +136,7 @@ private: Plan9FSBlockerSet m_completion_blocker; HashMap> m_completions; - Spinlock m_thread_lock { LockRank::None }; + Spinlock m_thread_lock {}; LockRefPtr m_thread; Atomic m_thread_running { false }; Atomic m_thread_shutdown { false }; diff --git a/Kernel/FileSystem/SysFS/Component.cpp b/Kernel/FileSystem/SysFS/Component.cpp index a79bf062009..274216718f7 100644 --- a/Kernel/FileSystem/SysFS/Component.cpp +++ b/Kernel/FileSystem/SysFS/Component.cpp @@ -13,7 +13,7 @@ namespace Kernel { -static Spinlock s_index_lock { LockRank::None }; +static Spinlock s_index_lock {}; static InodeIndex s_next_inode_index { 0 }; static size_t allocate_inode_index() diff --git a/Kernel/FileSystem/SysFS/Component.h b/Kernel/FileSystem/SysFS/Component.h index 4b18eeaf885..3f9b5643f5d 100644 --- a/Kernel/FileSystem/SysFS/Component.h +++ b/Kernel/FileSystem/SysFS/Component.h @@ -80,14 +80,14 @@ public: virtual ErrorOr> to_inode(SysFS const& sysfs_instance) const override final; - using ChildList = SpinlockProtected>; + using ChildList = SpinlockProtected, LockRank::None>; protected: virtual bool is_root_directory() const { return false; } SysFSDirectory() {}; explicit SysFSDirectory(SysFSDirectory const& parent_directory); - ChildList m_child_components { LockRank::None }; + ChildList m_child_components {}; }; } diff --git a/Kernel/FileSystem/SysFS/Registry.h b/Kernel/FileSystem/SysFS/Registry.h index c0113eb29d3..360e29a5a58 100644 --- a/Kernel/FileSystem/SysFS/Registry.h +++ b/Kernel/FileSystem/SysFS/Registry.h @@ -32,7 +32,7 @@ public: private: NonnullLockRefPtr m_root_directory; - Spinlock m_root_directory_lock { LockRank::None }; + Spinlock m_root_directory_lock {}; }; } diff --git a/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h b/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h index 9a72bc0bee8..36761345b9b 100644 --- a/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h +++ b/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h @@ -27,7 +27,7 @@ public: private: explicit SysFSUSBBusDirectory(SysFSBusDirectory&); - mutable Spinlock m_lock { LockRank::None }; + mutable Spinlock m_lock {}; }; } diff --git a/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/CapsLockRemap.h b/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/CapsLockRemap.h index f0bd11bf9fd..a4b18c8f495 100644 --- a/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/CapsLockRemap.h +++ b/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/CapsLockRemap.h @@ -25,7 +25,7 @@ private: explicit SysFSCapsLockRemap(SysFSDirectory const&); - mutable Spinlock m_lock { LockRank::None }; + mutable Spinlock m_lock {}; }; } diff --git a/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/DumpKmallocStack.h b/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/DumpKmallocStack.h index 1bc2ad995ee..8b8ab77f0db 100644 --- a/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/DumpKmallocStack.h +++ b/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/DumpKmallocStack.h @@ -25,7 +25,7 @@ private: explicit SysFSDumpKmallocStacks(SysFSDirectory const&); - mutable Spinlock m_lock { LockRank::None }; + mutable Spinlock m_lock {}; }; } diff --git a/Kernel/FileSystem/VirtualFileSystem.cpp b/Kernel/FileSystem/VirtualFileSystem.cpp index 98621c85ec1..fb455802167 100644 --- a/Kernel/FileSystem/VirtualFileSystem.cpp +++ b/Kernel/FileSystem/VirtualFileSystem.cpp @@ -39,7 +39,6 @@ VirtualFileSystem& VirtualFileSystem::the() } UNMAP_AFTER_INIT VirtualFileSystem::VirtualFileSystem() - : m_root_custody(LockRank::None) { } diff --git a/Kernel/FileSystem/VirtualFileSystem.h b/Kernel/FileSystem/VirtualFileSystem.h index 07c5af9055f..06a8b7abc80 100644 --- a/Kernel/FileSystem/VirtualFileSystem.h +++ b/Kernel/FileSystem/VirtualFileSystem.h @@ -113,11 +113,11 @@ private: LockRefPtr m_root_inode; - SpinlockProtected> m_root_custody; + SpinlockProtected, LockRank::None> m_root_custody {}; - SpinlockProtected> m_mounts { LockRank::None }; - SpinlockProtected> m_file_backed_file_systems_list { LockRank::None }; - SpinlockProtected> m_file_systems_list { LockRank::FileSystem }; + SpinlockProtected, LockRank::None> m_mounts {}; + SpinlockProtected, LockRank::None> m_file_backed_file_systems_list {}; + SpinlockProtected, LockRank::FileSystem> m_file_systems_list {}; }; } diff --git a/Kernel/Forward.h b/Kernel/Forward.h index 279abeae1dc..88f53b2fdae 100644 --- a/Kernel/Forward.h +++ b/Kernel/Forward.h @@ -11,6 +11,8 @@ namespace Kernel { +enum class LockRank; + class BlockDevice; class CharacterDevice; class Coredump; @@ -48,6 +50,7 @@ class ProcFSSystemBoolean; class ProcFSSystemDirectory; class Process; class ProcessGroup; +template class RecursiveSpinlock; class Scheduler; class Socket; @@ -85,6 +88,7 @@ class VMObject; class VirtualRange; } +template class Spinlock; template class SpinlockLocker; diff --git a/Kernel/Graphics/Console/BootFramebufferConsole.h b/Kernel/Graphics/Console/BootFramebufferConsole.h index b9819f5e123..1236656b503 100644 --- a/Kernel/Graphics/Console/BootFramebufferConsole.h +++ b/Kernel/Graphics/Console/BootFramebufferConsole.h @@ -39,7 +39,7 @@ protected: OwnPtr m_framebuffer; u8* m_framebuffer_data {}; - mutable Spinlock m_lock { LockRank::None }; + mutable Spinlock m_lock {}; }; } diff --git a/Kernel/Graphics/Console/GenericFramebufferConsole.h b/Kernel/Graphics/Console/GenericFramebufferConsole.h index 0b38e430978..88ace335bd9 100644 --- a/Kernel/Graphics/Console/GenericFramebufferConsole.h +++ b/Kernel/Graphics/Console/GenericFramebufferConsole.h @@ -81,7 +81,7 @@ protected: virtual void clear_glyph(size_t x, size_t y) override; - mutable Spinlock m_lock { LockRank::None }; + mutable Spinlock m_lock {}; }; } diff --git a/Kernel/Graphics/Console/VGATextModeConsole.h b/Kernel/Graphics/Console/VGATextModeConsole.h index e67125b2dcb..f487b1bfae7 100644 --- a/Kernel/Graphics/Console/VGATextModeConsole.h +++ b/Kernel/Graphics/Console/VGATextModeConsole.h @@ -38,7 +38,7 @@ private: explicit VGATextModeConsole(NonnullOwnPtr); - mutable Spinlock m_vga_lock { LockRank::None }; + mutable Spinlock m_vga_lock {}; NonnullOwnPtr m_vga_window_region; VirtualAddress m_current_vga_window; diff --git a/Kernel/Graphics/DisplayConnector.h b/Kernel/Graphics/DisplayConnector.h index fd7e8830700..157dffcbb08 100644 --- a/Kernel/Graphics/DisplayConnector.h +++ b/Kernel/Graphics/DisplayConnector.h @@ -114,14 +114,14 @@ protected: ErrorOr initialize_edid_for_generic_monitor(Optional> manufacturer_id_string); - mutable Spinlock m_control_lock { LockRank::None }; + mutable Spinlock m_control_lock {}; mutable Mutex m_flushing_lock; bool m_console_mode { false }; bool m_vertical_offsetted { false }; - mutable Spinlock m_modeset_lock { LockRank::None }; + mutable Spinlock m_modeset_lock {}; ModeSetting m_current_mode_setting {}; Optional m_edid_parser; @@ -165,7 +165,7 @@ private: LockRefPtr m_shared_framebuffer_vmobject; LockWeakPtr m_responsible_process; - Spinlock m_responsible_process_lock { LockRank::None }; + Spinlock m_responsible_process_lock {}; IntrusiveListNode> m_list_node; }; diff --git a/Kernel/Graphics/GraphicsManagement.h b/Kernel/Graphics/GraphicsManagement.h index 17e17bd06b3..ec771c6528d 100644 --- a/Kernel/Graphics/GraphicsManagement.h +++ b/Kernel/Graphics/GraphicsManagement.h @@ -64,7 +64,7 @@ private: unsigned m_current_minor_number { 0 }; - SpinlockProtected> m_display_connector_nodes { LockRank::None }; + SpinlockProtected, LockRank::None> m_display_connector_nodes {}; #if ARCH(X86_64) OwnPtr m_vga_arbiter; #endif diff --git a/Kernel/Graphics/Intel/NativeDisplayConnector.h b/Kernel/Graphics/Intel/NativeDisplayConnector.h index a5d77a20b18..0cd60a4358e 100644 --- a/Kernel/Graphics/Intel/NativeDisplayConnector.h +++ b/Kernel/Graphics/Intel/NativeDisplayConnector.h @@ -154,7 +154,7 @@ private: Optional create_pll_settings(u64 target_frequency, u64 reference_clock, IntelGraphics::PLLMaxSettings const&); - mutable Spinlock m_registers_lock { LockRank::None }; + mutable Spinlock m_registers_lock {}; LockRefPtr m_framebuffer_console; const PhysicalAddress m_registers; diff --git a/Kernel/Graphics/VMWare/GraphicsAdapter.h b/Kernel/Graphics/VMWare/GraphicsAdapter.h index 9f414dadb84..705b60865ec 100644 --- a/Kernel/Graphics/VMWare/GraphicsAdapter.h +++ b/Kernel/Graphics/VMWare/GraphicsAdapter.h @@ -51,8 +51,8 @@ private: Memory::TypedMapping m_fifo_registers; LockRefPtr m_display_connector; mutable NonnullOwnPtr m_registers_io_window; - mutable Spinlock m_io_access_lock { LockRank::None }; - mutable RecursiveSpinlock m_operation_lock { LockRank::None }; + mutable Spinlock m_io_access_lock {}; + mutable RecursiveSpinlock m_operation_lock {}; }; } diff --git a/Kernel/Graphics/VirtIOGPU/GraphicsAdapter.h b/Kernel/Graphics/VirtIOGPU/GraphicsAdapter.h index 2b5bedccb62..5b93a518203 100644 --- a/Kernel/Graphics/VirtIOGPU/GraphicsAdapter.h +++ b/Kernel/Graphics/VirtIOGPU/GraphicsAdapter.h @@ -112,11 +112,11 @@ private: VirtIO::Configuration const* m_device_configuration { nullptr }; // Note: Resource ID 0 is invalid, and we must not allocate 0 as the first resource ID. Atomic m_resource_id_counter { 1 }; - SpinlockProtected m_active_context_ids { LockRank::None }; + SpinlockProtected m_active_context_ids {}; LockRefPtr m_3d_device; bool m_has_virgl_support { false }; - Spinlock m_operation_lock { LockRank::None }; + Spinlock m_operation_lock {}; NonnullOwnPtr m_scratch_space; }; } diff --git a/Kernel/Heap/kmalloc.cpp b/Kernel/Heap/kmalloc.cpp index de4a1923997..6330550570f 100644 --- a/Kernel/Heap/kmalloc.cpp +++ b/Kernel/Heap/kmalloc.cpp @@ -36,7 +36,7 @@ const nothrow_t nothrow; } // FIXME: Figure out whether this can be MemoryManager. -static RecursiveSpinlock s_lock { LockRank::None }; // needs to be recursive because of dump_backtrace() +static RecursiveSpinlock s_lock {}; // needs to be recursive because of dump_backtrace() struct KmallocSubheap { KmallocSubheap(u8* base, size_t size) diff --git a/Kernel/Jail.h b/Kernel/Jail.h index 8dc623193ae..bfbc6c833a4 100644 --- a/Kernel/Jail.h +++ b/Kernel/Jail.h @@ -34,7 +34,7 @@ public: JailIndex index() const { return m_index; } void detach(Badge); - SpinlockProtected& attach_count() { return m_attach_count; } + SpinlockProtected& attach_count() { return m_attach_count; } private: Jail(NonnullOwnPtr, JailIndex); @@ -43,7 +43,7 @@ private: JailIndex const m_index; IntrusiveListNode> m_jail_list_node; - SpinlockProtected m_attach_count { LockRank::None, 0 }; + SpinlockProtected m_attach_count { 0 }; }; } diff --git a/Kernel/JailManagement.h b/Kernel/JailManagement.h index 9f363bed661..76fe91cf82e 100644 --- a/Kernel/JailManagement.h +++ b/Kernel/JailManagement.h @@ -36,7 +36,7 @@ public: private: JailIndex generate_jail_id(); - SpinlockProtected> m_jails { LockRank::None }; + SpinlockProtected, LockRank::None> m_jails {}; }; } diff --git a/Kernel/Locking/Mutex.cpp b/Kernel/Locking/Mutex.cpp index 99f836a0777..5e4606add45 100644 --- a/Kernel/Locking/Mutex.cpp +++ b/Kernel/Locking/Mutex.cpp @@ -211,7 +211,7 @@ void Mutex::unlock() } } -void Mutex::block(Thread& current_thread, Mode mode, SpinlockLocker& lock, u32 requested_locks) +void Mutex::block(Thread& current_thread, Mode mode, SpinlockLocker>& lock, u32 requested_locks) { if constexpr (LOCK_IN_CRITICAL_DEBUG) { // There are no interrupts enabled in early boot. diff --git a/Kernel/Locking/Mutex.h b/Kernel/Locking/Mutex.h index beba56f8a0f..17d59925ec7 100644 --- a/Kernel/Locking/Mutex.h +++ b/Kernel/Locking/Mutex.h @@ -82,7 +82,8 @@ private: // FIXME: remove this after annihilating Process::m_big_lock using BigLockBlockedThreadList = IntrusiveList<&Thread::m_big_lock_blocked_threads_list_node>; - void block(Thread&, Mode, SpinlockLocker&, u32); + // FIXME: Allow any lock rank. + void block(Thread&, Mode, SpinlockLocker>&, u32); void unblock_waiters(Mode); StringView m_name; @@ -117,10 +118,10 @@ private: } }; // FIXME: Use a specific lock rank passed by constructor. - SpinlockProtected m_blocked_thread_lists { LockRank::None }; + SpinlockProtected m_blocked_thread_lists {}; // FIXME: See above. - mutable Spinlock m_lock { LockRank::None }; + mutable Spinlock m_lock {}; #if LOCK_SHARED_UPGRADE_DEBUG HashMap m_shared_holders_map; diff --git a/Kernel/Locking/Spinlock.cpp b/Kernel/Locking/Spinlock.cpp deleted file mode 100644 index 7c1f57a2a1c..00000000000 --- a/Kernel/Locking/Spinlock.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2020-2022, Andreas Kling - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include - -namespace Kernel { - -InterruptsState Spinlock::lock() -{ - InterruptsState previous_interrupts_state = processor_interrupts_state(); - Processor::enter_critical(); - Processor::disable_interrupts(); - while (m_lock.exchange(1, AK::memory_order_acquire) != 0) - Processor::wait_check(); - track_lock_acquire(m_rank); - return previous_interrupts_state; -} - -void Spinlock::unlock(InterruptsState previous_interrupts_state) -{ - VERIFY(is_locked()); - track_lock_release(m_rank); - m_lock.store(0, AK::memory_order_release); - - Processor::leave_critical(); - restore_processor_interrupts_state(previous_interrupts_state); -} - -InterruptsState RecursiveSpinlock::lock() -{ - InterruptsState previous_interrupts_state = processor_interrupts_state(); - Processor::disable_interrupts(); - Processor::enter_critical(); - auto& proc = Processor::current(); - FlatPtr cpu = FlatPtr(&proc); - FlatPtr expected = 0; - while (!m_lock.compare_exchange_strong(expected, cpu, AK::memory_order_acq_rel)) { - if (expected == cpu) - break; - Processor::wait_check(); - expected = 0; - } - if (m_recursions == 0) - track_lock_acquire(m_rank); - m_recursions++; - return previous_interrupts_state; -} - -void RecursiveSpinlock::unlock(InterruptsState previous_interrupts_state) -{ - VERIFY_INTERRUPTS_DISABLED(); - VERIFY(m_recursions > 0); - VERIFY(m_lock.load(AK::memory_order_relaxed) == FlatPtr(&Processor::current())); - if (--m_recursions == 0) { - track_lock_release(m_rank); - m_lock.store(0, AK::memory_order_release); - } - - Processor::leave_critical(); - restore_processor_interrupts_state(previous_interrupts_state); -} - -} diff --git a/Kernel/Locking/Spinlock.h b/Kernel/Locking/Spinlock.h index a7177d42099..fea5836b9ee 100644 --- a/Kernel/Locking/Spinlock.h +++ b/Kernel/Locking/Spinlock.h @@ -13,18 +13,34 @@ namespace Kernel { +template class Spinlock { AK_MAKE_NONCOPYABLE(Spinlock); AK_MAKE_NONMOVABLE(Spinlock); public: - Spinlock(LockRank rank) - : m_rank(rank) + Spinlock() = default; + + InterruptsState lock() { + InterruptsState previous_interrupts_state = processor_interrupts_state(); + Processor::enter_critical(); + Processor::disable_interrupts(); + while (m_lock.exchange(1, AK::memory_order_acquire) != 0) + Processor::wait_check(); + track_lock_acquire(m_rank); + return previous_interrupts_state; } - InterruptsState lock(); - void unlock(InterruptsState); + void unlock(InterruptsState previous_interrupts_state) + { + VERIFY(is_locked()); + track_lock_release(m_rank); + m_lock.store(0, AK::memory_order_release); + + Processor::leave_critical(); + restore_processor_interrupts_state(previous_interrupts_state); + } [[nodiscard]] ALWAYS_INLINE bool is_locked() const { @@ -38,21 +54,50 @@ public: private: Atomic m_lock { 0 }; - const LockRank m_rank; + static constexpr LockRank const m_rank { Rank }; }; +template class RecursiveSpinlock { AK_MAKE_NONCOPYABLE(RecursiveSpinlock); AK_MAKE_NONMOVABLE(RecursiveSpinlock); public: - RecursiveSpinlock(LockRank rank) - : m_rank(rank) + RecursiveSpinlock() = default; + + InterruptsState lock() { + InterruptsState previous_interrupts_state = processor_interrupts_state(); + Processor::disable_interrupts(); + Processor::enter_critical(); + auto& proc = Processor::current(); + FlatPtr cpu = FlatPtr(&proc); + FlatPtr expected = 0; + while (!m_lock.compare_exchange_strong(expected, cpu, AK::memory_order_acq_rel)) { + if (expected == cpu) + break; + Processor::wait_check(); + expected = 0; + } + if (m_recursions == 0) + track_lock_acquire(m_rank); + m_recursions++; + return previous_interrupts_state; } - InterruptsState lock(); - void unlock(InterruptsState); + void unlock(InterruptsState previous_interrupts_state) + { + VERIFY_INTERRUPTS_DISABLED(); + VERIFY(m_recursions > 0); + VERIFY(m_lock.load(AK::memory_order_relaxed) == FlatPtr(&Processor::current())); + if (--m_recursions == 0) { + track_lock_release(m_rank); + m_lock.store(0, AK::memory_order_release); + } + + Processor::leave_critical(); + restore_processor_interrupts_state(previous_interrupts_state); + } [[nodiscard]] ALWAYS_INLINE bool is_locked() const { @@ -72,7 +117,7 @@ public: private: Atomic m_lock { 0 }; u32 m_recursions { 0 }; - const LockRank m_rank; + static constexpr LockRank const m_rank { Rank }; }; template diff --git a/Kernel/Locking/SpinlockProtected.h b/Kernel/Locking/SpinlockProtected.h index e40248b7fdb..ced5e941cba 100644 --- a/Kernel/Locking/SpinlockProtected.h +++ b/Kernel/Locking/SpinlockProtected.h @@ -10,7 +10,7 @@ namespace Kernel { -template +template class SpinlockProtected { AK_MAKE_NONCOPYABLE(SpinlockProtected); AK_MAKE_NONMOVABLE(SpinlockProtected); @@ -22,7 +22,7 @@ private: AK_MAKE_NONMOVABLE(Locked); public: - Locked(U& value, RecursiveSpinlock& spinlock) + Locked(U& value, RecursiveSpinlock& spinlock) : m_value(value) , m_locker(spinlock) { @@ -39,7 +39,7 @@ private: private: U& m_value; - SpinlockLocker m_locker; + SpinlockLocker> m_locker; }; auto lock_const() const { return Locked(m_value, m_spinlock); } @@ -47,9 +47,8 @@ private: public: template - SpinlockProtected(LockRank rank, Args&&... args) + SpinlockProtected(Args&&... args) : m_value(forward(args)...) - , m_spinlock(rank) { } @@ -87,7 +86,8 @@ public: private: T m_value; - RecursiveSpinlock mutable m_spinlock; + RecursiveSpinlock mutable m_spinlock; + static constexpr LockRank const m_rank { Rank }; }; } diff --git a/Kernel/Memory/AnonymousVMObject.h b/Kernel/Memory/AnonymousVMObject.h index d880dd8b875..6bee370e4cf 100644 --- a/Kernel/Memory/AnonymousVMObject.h +++ b/Kernel/Memory/AnonymousVMObject.h @@ -78,7 +78,7 @@ private: void uncommit_one(); private: - Spinlock m_lock { LockRank::None }; + Spinlock m_lock {}; CommittedPhysicalPageSet m_committed_pages; }; diff --git a/Kernel/Memory/MemoryManager.cpp b/Kernel/Memory/MemoryManager.cpp index 5e80e9cbc1b..a0d4af14c1b 100644 --- a/Kernel/Memory/MemoryManager.cpp +++ b/Kernel/Memory/MemoryManager.cpp @@ -88,7 +88,6 @@ MemoryManager::GlobalData::GlobalData() } UNMAP_AFTER_INIT MemoryManager::MemoryManager() - : m_global_data(LockRank::None) { s_the = this; diff --git a/Kernel/Memory/MemoryManager.h b/Kernel/Memory/MemoryManager.h index 484a645acc0..e4b0b579548 100644 --- a/Kernel/Memory/MemoryManager.h +++ b/Kernel/Memory/MemoryManager.h @@ -89,7 +89,7 @@ struct PhysicalMemoryRange { struct MemoryManagerData { static ProcessorSpecificDataID processor_specific_data_id() { return ProcessorSpecificDataID::MemoryManager; } - Spinlock m_quickmap_in_use { LockRank::None }; + Spinlock m_quickmap_in_use {}; InterruptsState m_quickmap_previous_interrupts_state; }; @@ -304,7 +304,7 @@ private: Vector reserved_memory_ranges; }; - SpinlockProtected m_global_data; + SpinlockProtected m_global_data; }; inline bool is_user_address(VirtualAddress vaddr) diff --git a/Kernel/Memory/PageDirectory.h b/Kernel/Memory/PageDirectory.h index e5e3f8d043f..5ae014464cb 100644 --- a/Kernel/Memory/PageDirectory.h +++ b/Kernel/Memory/PageDirectory.h @@ -52,7 +52,7 @@ public: void set_space(Badge, AddressSpace& space) { m_space = &space; } - RecursiveSpinlock& get_lock() { return m_lock; } + RecursiveSpinlock& get_lock() { return m_lock; } // This has to be public to let the global singleton access the member pointer IntrusiveRedBlackTreeNode> m_tree_node; @@ -72,7 +72,7 @@ private: #else RefPtr m_directory_pages[4]; #endif - RecursiveSpinlock m_lock { LockRank::None }; + RecursiveSpinlock m_lock {}; }; void activate_kernel_page_directory(PageDirectory const& pgd); diff --git a/Kernel/Memory/Region.cpp b/Kernel/Memory/Region.cpp index 4dd3e6c1021..2c4d0f7ed26 100644 --- a/Kernel/Memory/Region.cpp +++ b/Kernel/Memory/Region.cpp @@ -270,7 +270,7 @@ void Region::unmap(ShouldFlushTLB should_flush_tlb) unmap_with_locks_held(should_flush_tlb, pd_locker); } -void Region::unmap_with_locks_held(ShouldFlushTLB should_flush_tlb, SpinlockLocker&) +void Region::unmap_with_locks_held(ShouldFlushTLB should_flush_tlb, SpinlockLocker>&) { if (!m_page_directory) return; diff --git a/Kernel/Memory/Region.h b/Kernel/Memory/Region.h index bc435e3841c..3955c01bca7 100644 --- a/Kernel/Memory/Region.h +++ b/Kernel/Memory/Region.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -192,7 +193,7 @@ public: void set_page_directory(PageDirectory&); ErrorOr map(PageDirectory&, ShouldFlushTLB = ShouldFlushTLB::Yes); void unmap(ShouldFlushTLB = ShouldFlushTLB::Yes); - void unmap_with_locks_held(ShouldFlushTLB, SpinlockLocker& pd_locker); + void unmap_with_locks_held(ShouldFlushTLB, SpinlockLocker>& pd_locker); void remap(); diff --git a/Kernel/Memory/RingBuffer.h b/Kernel/Memory/RingBuffer.h index 2ea0811e3ab..74ff355b29c 100644 --- a/Kernel/Memory/RingBuffer.h +++ b/Kernel/Memory/RingBuffer.h @@ -22,7 +22,7 @@ public: void reclaim_space(PhysicalAddress chunk_start, size_t chunk_size); PhysicalAddress start_of_used() const; - Spinlock& lock() { return m_lock; } + Spinlock& lock() { return m_lock; } size_t used_bytes() const { return m_num_used_bytes; } PhysicalAddress start_of_region() const { return m_region->physical_page(0)->paddr(); } VirtualAddress vaddr() const { return m_region->vaddr(); } @@ -32,7 +32,7 @@ private: RingBuffer(NonnullOwnPtr region, size_t capacity); NonnullOwnPtr m_region; - Spinlock m_lock { LockRank::None }; + Spinlock m_lock {}; size_t m_start_of_used {}; size_t m_num_used_bytes {}; size_t m_capacity_in_bytes {}; diff --git a/Kernel/Memory/SharedFramebufferVMObject.h b/Kernel/Memory/SharedFramebufferVMObject.h index 2c95d4eb6df..a43144c19a7 100644 --- a/Kernel/Memory/SharedFramebufferVMObject.h +++ b/Kernel/Memory/SharedFramebufferVMObject.h @@ -87,7 +87,7 @@ private: LockRefPtr m_fake_writes_framebuffer_vmobject; LockRefPtr m_real_writes_framebuffer_vmobject; bool m_writes_are_faked { false }; - mutable RecursiveSpinlock m_writes_state_lock { LockRank::None }; + mutable RecursiveSpinlock m_writes_state_lock {}; CommittedPhysicalPageSet m_committed_pages; }; diff --git a/Kernel/Memory/VMObject.cpp b/Kernel/Memory/VMObject.cpp index d6d09c888b3..3d241a66062 100644 --- a/Kernel/Memory/VMObject.cpp +++ b/Kernel/Memory/VMObject.cpp @@ -10,9 +10,9 @@ namespace Kernel::Memory { -static Singleton> s_all_instances; +static Singleton> s_all_instances; -SpinlockProtected& VMObject::all_instances() +SpinlockProtected& VMObject::all_instances() { return s_all_instances; } diff --git a/Kernel/Memory/VMObject.h b/Kernel/Memory/VMObject.h index d6cc4ecce4d..8dcdf7e0984 100644 --- a/Kernel/Memory/VMObject.h +++ b/Kernel/Memory/VMObject.h @@ -65,7 +65,7 @@ protected: IntrusiveListNode m_list_node; FixedArray> m_physical_pages; - mutable RecursiveSpinlock m_lock { LockRank::None }; + mutable RecursiveSpinlock m_lock {}; private: VMObject& operator=(VMObject const&) = delete; @@ -76,7 +76,7 @@ private: public: using AllInstancesList = IntrusiveList<&VMObject::m_list_node>; - static SpinlockProtected& all_instances(); + static SpinlockProtected& all_instances(); }; template diff --git a/Kernel/Net/NetworkAdapter.h b/Kernel/Net/NetworkAdapter.h index 0b0ac82a632..6e8bdd17826 100644 --- a/Kernel/Net/NetworkAdapter.h +++ b/Kernel/Net/NetworkAdapter.h @@ -111,7 +111,7 @@ private: PacketList m_packet_queue; size_t m_packet_queue_size { 0 }; - SpinlockProtected m_unused_packets { LockRank::None }; + SpinlockProtected m_unused_packets {}; NonnullOwnPtr m_name; u32 m_packets_in { 0 }; u32 m_bytes_in { 0 }; diff --git a/Kernel/Net/NetworkingManagement.h b/Kernel/Net/NetworkingManagement.h index 8b8955e9195..ab4f3bc25f9 100644 --- a/Kernel/Net/NetworkingManagement.h +++ b/Kernel/Net/NetworkingManagement.h @@ -42,7 +42,7 @@ public: private: ErrorOr> determine_network_device(PCI::DeviceIdentifier const&) const; - SpinlockProtected> m_adapters { LockRank::None }; + SpinlockProtected, LockRank::None> m_adapters {}; LockRefPtr m_loopback_adapter; }; diff --git a/Kernel/Net/Routing.cpp b/Kernel/Net/Routing.cpp index 5711cd1359b..81ca0b000cb 100644 --- a/Kernel/Net/Routing.cpp +++ b/Kernel/Net/Routing.cpp @@ -16,8 +16,8 @@ namespace Kernel { -static Singleton>> s_arp_table; -static Singleton> s_routing_table; +static Singleton, LockRank::None>> s_arp_table; +static Singleton> s_routing_table; class ARPTableBlocker final : public Thread::Blocker { public: @@ -106,7 +106,7 @@ void ARPTableBlocker::will_unblock_immediately_without_blocking(UnblockImmediate } } -SpinlockProtected>& arp_table() +SpinlockProtected, LockRank::None>& arp_table() { return *s_arp_table; } @@ -130,7 +130,7 @@ void update_arp_table(IPv4Address const& ip_addr, MACAddress const& addr, Update } } -SpinlockProtected& routing_table() +SpinlockProtected& routing_table() { return *s_routing_table; } diff --git a/Kernel/Net/Routing.h b/Kernel/Net/Routing.h index b1cd26b4d6b..8732184901f 100644 --- a/Kernel/Net/Routing.h +++ b/Kernel/Net/Routing.h @@ -66,7 +66,7 @@ enum class AllowUsingGateway { RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, LockRefPtr const through = nullptr, AllowUsingGateway = AllowUsingGateway::Yes); -SpinlockProtected>& arp_table(); -SpinlockProtected& routing_table(); +SpinlockProtected, LockRank::None>& arp_table(); +SpinlockProtected& routing_table(); } diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 51bb92217de..d2fbf3f3f34 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -46,9 +46,9 @@ static void create_signal_trampoline(); extern ProcessID g_init_pid; -RecursiveSpinlock g_profiling_lock { LockRank::None }; +RecursiveSpinlock g_profiling_lock {}; static Atomic next_pid; -static Singleton> s_all_instances; +static Singleton> s_all_instances; READONLY_AFTER_INIT Memory::Region* g_signal_trampoline_region; static Singleton>> s_hostname; @@ -58,7 +58,7 @@ MutexProtected>& hostname() return *s_hostname; } -SpinlockProtected& Process::all_instances() +SpinlockProtected& Process::all_instances() { return *s_all_instances; } @@ -319,14 +319,12 @@ ErrorOr> Process::try_create(LockRefPtr& firs Process::Process(NonnullOwnPtr name, NonnullRefPtr credentials, ProcessID ppid, bool is_kernel_process, RefPtr current_directory, RefPtr executable, TTY* tty, UnveilNode unveil_tree, UnveilNode exec_unveil_tree) : m_name(move(name)) - , m_space(LockRank::None) - , m_protected_data_lock(LockRank::None) , m_is_kernel_process(is_kernel_process) - , m_executable(LockRank::None, move(executable)) - , m_current_directory(LockRank::None, move(current_directory)) + , m_executable(move(executable)) + , m_current_directory(move(current_directory)) , m_tty(tty) - , m_unveil_data(LockRank::None, move(unveil_tree)) - , m_exec_unveil_data(LockRank::None, move(exec_unveil_tree)) + , m_unveil_data(move(unveil_tree)) + , m_exec_unveil_data(move(exec_unveil_tree)) , m_wait_blocker_set(*this) { // Ensure that we protect the process data when exiting the constructor. diff --git a/Kernel/Process.h b/Kernel/Process.h index c897d4f962c..c7613f3e1c8 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -238,7 +238,7 @@ public: return with_protected_data([](auto& protected_data) { return protected_data.ppid; }); } - SpinlockProtected>& jail() { return m_attached_jail; } + SpinlockProtected, LockRank::Process>& jail() { return m_attached_jail; } NonnullRefPtr credentials() const; @@ -568,8 +568,8 @@ public: PerformanceEventBuffer* perf_events() { return m_perf_event_buffer; } PerformanceEventBuffer const* perf_events() const { return m_perf_event_buffer; } - SpinlockProtected>& address_space() { return m_space; } - SpinlockProtected> const& address_space() const { return m_space; } + SpinlockProtected, LockRank::None>& address_space() { return m_space; } + SpinlockProtected, LockRank::None> const& address_space() const { return m_space; } VirtualAddress signal_trampoline() const { @@ -666,11 +666,11 @@ private: NonnullOwnPtr m_name; - SpinlockProtected> m_space; + SpinlockProtected, LockRank::None> m_space; LockRefPtr m_pg; - RecursiveSpinlock mutable m_protected_data_lock; + RecursiveSpinlock mutable m_protected_data_lock; AtomicEdgeAction m_protected_data_refs; void protect_data(); void unprotect_data(); @@ -849,12 +849,12 @@ public: private: ErrorOr> custody_for_dirfd(int dirfd); - SpinlockProtected& thread_list() { return m_thread_list; } - SpinlockProtected const& thread_list() const { return m_thread_list; } + SpinlockProtected& thread_list() { return m_thread_list; } + SpinlockProtected const& thread_list() const { return m_thread_list; } ErrorOr> get_thread_from_pid_or_tid(pid_t pid_or_tid, Syscall::SchedulerParametersMode mode); - SpinlockProtected m_thread_list { LockRank::None }; + SpinlockProtected m_thread_list {}; MutexProtected m_fds; @@ -864,9 +864,9 @@ private: Atomic m_is_stopped { false }; bool m_should_generate_coredump { false }; - SpinlockProtected> m_executable; + SpinlockProtected, LockRank::None> m_executable; - SpinlockProtected> m_current_directory; + SpinlockProtected, LockRank::None> m_current_directory; NonnullOwnPtrVector m_arguments; NonnullOwnPtrVector m_environment; @@ -876,7 +876,7 @@ private: LockWeakPtr m_master_tls_region; IntrusiveListNode m_jail_list_node; - SpinlockProtected> m_attached_jail { LockRank::Process }; + SpinlockProtected, LockRank::Process> m_attached_jail {}; size_t m_master_tls_size { 0 }; size_t m_master_tls_alignment { 0 }; @@ -886,8 +886,8 @@ private: LockRefPtr m_alarm_timer; - SpinlockProtected m_unveil_data; - SpinlockProtected m_exec_unveil_data; + SpinlockProtected m_unveil_data; + SpinlockProtected m_exec_unveil_data; OwnPtr m_perf_event_buffer; @@ -903,7 +903,7 @@ private: OwnPtr value; }; - SpinlockProtected> m_coredump_properties { LockRank::None }; + SpinlockProtected, LockRank::None> m_coredump_properties {}; NonnullLockRefPtrVector m_threads_for_coredump; mutable LockRefPtr m_procfs_traits; @@ -920,7 +920,7 @@ private: public: using List = IntrusiveListRelaxedConst<&Process::m_list_node>; - static SpinlockProtected& all_instances(); + static SpinlockProtected& all_instances(); }; // Note: Process object should be 2 pages of 4096 bytes each. @@ -929,7 +929,7 @@ public: // The second page is being used exclusively for write-protected values. static_assert(AssertSize()); -extern RecursiveSpinlock g_profiling_lock; +extern RecursiveSpinlock g_profiling_lock; template Callback> inline IterationDecision Process::for_each_thread(Callback callback) diff --git a/Kernel/ProcessExposed.cpp b/Kernel/ProcessExposed.cpp index d9f9315f48d..80b091e0a33 100644 --- a/Kernel/ProcessExposed.cpp +++ b/Kernel/ProcessExposed.cpp @@ -14,7 +14,7 @@ namespace Kernel { -static Spinlock s_index_lock { LockRank::None }; +static Spinlock s_index_lock {}; static InodeIndex s_next_inode_index = 0; namespace SegmentedProcFSIndex { diff --git a/Kernel/ProcessGroup.cpp b/Kernel/ProcessGroup.cpp index 7463ed41a67..f86b7c1473a 100644 --- a/Kernel/ProcessGroup.cpp +++ b/Kernel/ProcessGroup.cpp @@ -10,9 +10,9 @@ namespace Kernel { -static Singleton> s_process_groups; +static Singleton> s_process_groups; -SpinlockProtected& process_groups() +SpinlockProtected& process_groups() { return *s_process_groups; } diff --git a/Kernel/ProcessGroup.h b/Kernel/ProcessGroup.h index 8ab9b551344..69a52d2cb31 100644 --- a/Kernel/ProcessGroup.h +++ b/Kernel/ProcessGroup.h @@ -44,6 +44,6 @@ public: using List = IntrusiveList<&ProcessGroup::m_list_node>; }; -SpinlockProtected& process_groups(); +SpinlockProtected& process_groups(); } diff --git a/Kernel/Random.h b/Kernel/Random.h index b5cb9f087df..2ae6bd91f17 100644 --- a/Kernel/Random.h +++ b/Kernel/Random.h @@ -82,7 +82,7 @@ public: return is_seeded() || m_p0_len >= reseed_threshold; } - Spinlock& get_lock() { return m_lock; } + Spinlock& get_lock() { return m_lock; } private: void reseed() @@ -117,7 +117,7 @@ private: size_t m_p0_len { 0 }; ByteBuffer m_key; HashType m_pools[pool_count]; - Spinlock m_lock { LockRank::None }; + Spinlock m_lock {}; }; class KernelRng : public FortunaPRNG { diff --git a/Kernel/Scheduler.cpp b/Kernel/Scheduler.cpp index 817e02288ba..15c3d110f01 100644 --- a/Kernel/Scheduler.cpp +++ b/Kernel/Scheduler.cpp @@ -21,7 +21,7 @@ namespace Kernel { -RecursiveSpinlock g_scheduler_lock { LockRank::None }; +RecursiveSpinlock g_scheduler_lock {}; static u32 time_slice_for(Thread const& thread) { @@ -46,9 +46,9 @@ struct ThreadReadyQueues { Array queues; }; -static Singleton> g_ready_queues; +static Singleton> g_ready_queues; -static SpinlockProtected g_total_time_scheduled { LockRank::None }; +static SpinlockProtected g_total_time_scheduled {}; static void dump_thread_list(bool = false); diff --git a/Kernel/Scheduler.h b/Kernel/Scheduler.h index ef15c3eb85a..ae7b6a5783e 100644 --- a/Kernel/Scheduler.h +++ b/Kernel/Scheduler.h @@ -22,7 +22,7 @@ struct RegisterState; extern Thread* g_finalizer; extern WaitQueue* g_finalizer_wait_queue; extern Atomic g_finalizer_has_work; -extern RecursiveSpinlock g_scheduler_lock; +extern RecursiveSpinlock g_scheduler_lock; struct TotalTimeScheduled { u64 total { 0 }; diff --git a/Kernel/Storage/ATA/AHCI/Controller.h b/Kernel/Storage/ATA/AHCI/Controller.h index aacd58d181d..b4916b197dd 100644 --- a/Kernel/Storage/ATA/AHCI/Controller.h +++ b/Kernel/Storage/ATA/AHCI/Controller.h @@ -60,6 +60,6 @@ private: // Note: This lock is intended to be locked when doing changes to HBA registers // that affect its core functionality in a manner that controls all attached storage devices // to the HBA SATA ports. - mutable Spinlock m_hba_control_lock { LockRank::None }; + mutable Spinlock m_hba_control_lock {}; }; } diff --git a/Kernel/Storage/ATA/AHCI/Port.h b/Kernel/Storage/ATA/AHCI/Port.h index 5c3f91df39d..21df462b6a4 100644 --- a/Kernel/Storage/ATA/AHCI/Port.h +++ b/Kernel/Storage/ATA/AHCI/Port.h @@ -107,7 +107,7 @@ private: EntropySource m_entropy_source; LockRefPtr m_current_request; - Spinlock m_hard_lock { LockRank::None }; + Spinlock m_hard_lock {}; Mutex m_lock { "AHCIPort"sv }; mutable bool m_wait_for_completion { false }; diff --git a/Kernel/Storage/ATA/ATAPort.h b/Kernel/Storage/ATA/ATAPort.h index 0b576f403d0..bb876cad7f1 100644 --- a/Kernel/Storage/ATA/ATAPort.h +++ b/Kernel/Storage/ATA/ATAPort.h @@ -135,7 +135,7 @@ protected: } mutable Mutex m_lock; - Spinlock m_hard_lock { LockRank::None }; + Spinlock m_hard_lock {}; EntropySource m_entropy_source; diff --git a/Kernel/Storage/NVMe/NVMeQueue.h b/Kernel/Storage/NVMe/NVMeQueue.h index e58a06765ed..2c1afddf53b 100644 --- a/Kernel/Storage/NVMe/NVMeQueue.h +++ b/Kernel/Storage/NVMe/NVMeQueue.h @@ -56,10 +56,10 @@ private: } protected: - Spinlock m_cq_lock { LockRank::Interrupts }; + Spinlock m_cq_lock {}; LockRefPtr m_current_request; NonnullOwnPtr m_rw_dma_region; - Spinlock m_request_lock { LockRank::None }; + Spinlock m_request_lock {}; private: u16 m_qid {}; @@ -69,7 +69,7 @@ private: u16 m_cq_head {}; bool m_admin_queue { false }; u32 m_qdepth {}; - Spinlock m_sq_lock { LockRank::Interrupts }; + Spinlock m_sq_lock {}; OwnPtr m_cq_dma_region; NonnullRefPtrVector m_cq_dma_page; Span m_sqe_array; diff --git a/Kernel/Syscalls/futex.cpp b/Kernel/Syscalls/futex.cpp index 6b12bd584a7..b88ed5deaa9 100644 --- a/Kernel/Syscalls/futex.cpp +++ b/Kernel/Syscalls/futex.cpp @@ -13,7 +13,7 @@ namespace Kernel { -static Singleton>>> s_global_futex_queues; +static Singleton>, LockRank::None>> s_global_futex_queues; void Process::clear_futex_queues_on_exec() { diff --git a/Kernel/TTY/ConsoleManagement.h b/Kernel/TTY/ConsoleManagement.h index ed890d85a9c..675c9312281 100644 --- a/Kernel/TTY/ConsoleManagement.h +++ b/Kernel/TTY/ConsoleManagement.h @@ -34,13 +34,13 @@ public: NonnullLockRefPtr first_tty() const { return m_consoles[0]; } NonnullLockRefPtr debug_tty() const { return m_consoles[1]; } - RecursiveSpinlock& tty_write_lock() { return m_tty_write_lock; } + RecursiveSpinlock& tty_write_lock() { return m_tty_write_lock; } private: NonnullLockRefPtrVector m_consoles; VirtualConsole* m_active_console { nullptr }; - Spinlock m_lock { LockRank::None }; - RecursiveSpinlock m_tty_write_lock { LockRank::None }; + Spinlock m_lock {}; + RecursiveSpinlock m_tty_write_lock {}; }; }; diff --git a/Kernel/TTY/PTYMultiplexer.h b/Kernel/TTY/PTYMultiplexer.h index 18c21e10003..c9da6ddbe0f 100644 --- a/Kernel/TTY/PTYMultiplexer.h +++ b/Kernel/TTY/PTYMultiplexer.h @@ -35,7 +35,7 @@ private: virtual StringView class_name() const override { return "PTYMultiplexer"sv; } static constexpr size_t max_pty_pairs = 64; - SpinlockProtected> m_freelist { LockRank::None }; + SpinlockProtected, LockRank::None> m_freelist {}; }; } diff --git a/Kernel/TTY/SlavePTY.cpp b/Kernel/TTY/SlavePTY.cpp index c3865dbb059..e2b0921d514 100644 --- a/Kernel/TTY/SlavePTY.cpp +++ b/Kernel/TTY/SlavePTY.cpp @@ -12,9 +12,9 @@ namespace Kernel { -static Singleton> s_all_instances; +static Singleton> s_all_instances; -SpinlockProtected& SlavePTY::all_instances() +SpinlockProtected& SlavePTY::all_instances() { return s_all_instances; } diff --git a/Kernel/TTY/SlavePTY.h b/Kernel/TTY/SlavePTY.h index c576e0a72a2..585c2f11a2b 100644 --- a/Kernel/TTY/SlavePTY.h +++ b/Kernel/TTY/SlavePTY.h @@ -52,7 +52,7 @@ private: public: using List = IntrusiveList<&SlavePTY::m_list_node>; - static SpinlockProtected& all_instances(); + static SpinlockProtected& all_instances(); }; } diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index a9f2b794811..f7fcf51fbdc 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -33,9 +33,9 @@ namespace Kernel { -static Singleton> s_list; +static Singleton> s_list; -SpinlockProtected& Thread::all_instances() +SpinlockProtected& Thread::all_instances() { return *s_list; } @@ -215,7 +215,7 @@ Thread::BlockResult Thread::block_impl(BlockTimeout const& timeout, Blocker& blo return result; } -void Thread::block(Kernel::Mutex& lock, SpinlockLocker& lock_lock, u32 lock_count) +void Thread::block(Kernel::Mutex& lock, SpinlockLocker>& lock_lock, u32 lock_count) { VERIFY(!Processor::current_in_irq()); VERIFY(this == Thread::current()); diff --git a/Kernel/Thread.h b/Kernel/Thread.h index ab4f59fa0c3..6652094cd09 100644 --- a/Kernel/Thread.h +++ b/Kernel/Thread.h @@ -298,7 +298,7 @@ public: void set_blocker_set_raw_locked(BlockerSet* blocker_set) { m_blocker_set = blocker_set; } // FIXME: Figure out whether this can be Thread. - mutable RecursiveSpinlock m_lock { LockRank::None }; + mutable RecursiveSpinlock m_lock {}; private: BlockerSet* m_blocker_set { nullptr }; @@ -417,7 +417,7 @@ public: } // FIXME: Check whether this can be Thread. - mutable Spinlock m_lock { LockRank::None }; + mutable Spinlock m_lock {}; private: Vector m_blockers; @@ -812,7 +812,7 @@ public: } } - void block(Kernel::Mutex&, SpinlockLocker&, u32); + void block(Kernel::Mutex&, SpinlockLocker>&, u32); template BlockResult block(BlockTimeout const& timeout, Args&&... args) @@ -1003,7 +1003,7 @@ public: TrapFrame*& current_trap() { return m_current_trap; } TrapFrame const* const& current_trap() const { return m_current_trap; } - RecursiveSpinlock& get_lock() const { return m_lock; } + RecursiveSpinlock& get_lock() const { return m_lock; } #if LOCK_DEBUG void holding_lock(Mutex& lock, int refs_delta, LockLocation const& location) @@ -1164,8 +1164,8 @@ private: void relock_process(LockMode, u32); void reset_fpu_state(); - mutable RecursiveSpinlock m_lock { LockRank::Thread }; - mutable RecursiveSpinlock m_block_lock { LockRank::None }; + mutable RecursiveSpinlock m_lock {}; + mutable RecursiveSpinlock m_block_lock {}; NonnullLockRefPtr m_process; ThreadID m_tid { -1 }; ThreadRegisters m_regs {}; @@ -1199,7 +1199,7 @@ private: Kernel::Mutex* m_blocking_mutex { nullptr }; u32 m_lock_requested_count { 0 }; IntrusiveListNode m_blocked_threads_list_node; - LockRank m_lock_rank_mask { LockRank::None }; + LockRank m_lock_rank_mask {}; bool m_allocation_enabled { true }; // FIXME: remove this after annihilating Process::m_big_lock @@ -1207,7 +1207,7 @@ private: #if LOCK_DEBUG Atomic m_holding_locks { 0 }; - Spinlock m_holding_locks_lock { LockRank::None }; + Spinlock m_holding_locks_lock {}; Vector m_holding_locks_list; #endif @@ -1267,7 +1267,7 @@ public: using ListInProcess = IntrusiveList<&Thread::m_process_thread_list_node>; using GlobalList = IntrusiveList<&Thread::m_global_thread_list_node>; - static SpinlockProtected& all_instances(); + static SpinlockProtected& all_instances(); }; AK_ENUM_BITWISE_OPERATORS(Thread::FileBlocker::BlockFlags); diff --git a/Kernel/TimerQueue.cpp b/Kernel/TimerQueue.cpp index 9b788331ef4..157af5c6d05 100644 --- a/Kernel/TimerQueue.cpp +++ b/Kernel/TimerQueue.cpp @@ -14,7 +14,7 @@ namespace Kernel { static Singleton s_the; -static Spinlock g_timerqueue_lock { LockRank::None }; +static Spinlock g_timerqueue_lock {}; Time Timer::remaining() const { diff --git a/Kernel/WorkQueue.h b/Kernel/WorkQueue.h index 932d434ff6d..ab35824666a 100644 --- a/Kernel/WorkQueue.h +++ b/Kernel/WorkQueue.h @@ -63,7 +63,7 @@ private: LockRefPtr m_thread; WaitQueue m_wait_queue; - SpinlockProtected> m_items { LockRank::None }; + SpinlockProtected, LockRank::None> m_items {}; }; } diff --git a/Kernel/kprintf.cpp b/Kernel/kprintf.cpp index ce21cd6952a..6fe2d22a1f1 100644 --- a/Kernel/kprintf.cpp +++ b/Kernel/kprintf.cpp @@ -29,7 +29,7 @@ extern Atomic g_boot_console; static bool s_serial_debug_enabled; // A recursive spinlock allows us to keep writing in the case where a // page fault happens in the middle of a dbgln(), etc -static RecursiveSpinlock s_log_lock { LockRank::None }; +static RecursiveSpinlock s_log_lock {}; void set_serial_debug_enabled(bool desired_state) {