summaryrefslogtreecommitdiff
path: root/Kernel/Bus/USB
diff options
context:
space:
mode:
authorb14ckcat <b14ckcat@protonmail.com>2022-07-13 19:02:58 -0400
committerLinus Groh <mail@linusgroh.de>2022-07-19 11:29:58 +0100
commit6aea13e229f05fb17054a844ffd8ff08869e09f8 (patch)
treef6c98ca4e896470f38f6ec0ca41f90b20b8f3c14 /Kernel/Bus/USB
parent00e13b5b27282fabbef31430f158dcf5bd321aa9 (diff)
downloadserenity-6aea13e229f05fb17054a844ffd8ff08869e09f8.zip
Kernel/USB: Make UHCI descriptor pool thread-safe
Right now the TD and QH descriptor pools look to be susceptible to a race condition in the event they are accessed simultaneously by separate threads making USB transfers. This fix does not seem to add any noticeable overhead.
Diffstat (limited to 'Kernel/Bus/USB')
-rw-r--r--Kernel/Bus/USB/UHCI/UHCIDescriptorPool.h7
1 files changed, 7 insertions, 0 deletions
diff --git a/Kernel/Bus/USB/UHCI/UHCIDescriptorPool.h b/Kernel/Bus/USB/UHCI/UHCIDescriptorPool.h
index 007fffc4da..ad15ffd8c7 100644
--- a/Kernel/Bus/USB/UHCI/UHCIDescriptorPool.h
+++ b/Kernel/Bus/USB/UHCI/UHCIDescriptorPool.h
@@ -9,6 +9,7 @@
#include <AK/NonnullOwnPtr.h>
#include <AK/OwnPtr.h>
#include <AK/Stack.h>
+#include <Kernel/Locking/Spinlock.h>
#include <Kernel/Memory/MemoryManager.h>
#include <Kernel/Memory/Region.h>
#include <Kernel/StdLib.h>
@@ -38,6 +39,8 @@ public:
[[nodiscard]] T* try_take_free_descriptor()
{
+ SpinlockLocker locker(m_pool_lock);
+
// We're out of descriptors!
if (m_free_descriptor_stack.is_empty())
return nullptr;
@@ -51,6 +54,8 @@ public:
void release_to_pool(T* ptr)
{
+ SpinlockLocker locker(m_pool_lock);
+
dbgln_if(UHCI_VERBOSE_DEBUG, "Returning descriptor @ {} to pool {}", ptr, m_pool_name);
if (!m_free_descriptor_stack.push(ptr))
dbgln("Failed to return descriptor to pool {}. Stack overflow!", m_pool_name);
@@ -78,5 +83,7 @@ private:
StringView m_pool_name; // Name of this pool
NonnullOwnPtr<Memory::Region> m_pool_region; // Memory region where descriptors actually reside
Stack<T*, PAGE_SIZE / sizeof(T)> m_free_descriptor_stack; // Stack of currently free descriptor pointers
+ Spinlock m_pool_lock;
};
+
}