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

@@ -1,4 +1,3 @@
#include <kernel/LockGuard.h>
#include <kernel/Networking/ARPTable.h>
#include <kernel/Scheduler.h>
#include <kernel/Timer/Timer.h>
@@ -52,9 +51,10 @@ namespace Kernel
ipv4_address = interface.get_gateway();
{
LockGuard _(m_lock);
if (m_arp_table.contains(ipv4_address))
return m_arp_table[ipv4_address];
SpinLockGuard _(m_table_lock);
auto it = m_arp_table.find(ipv4_address);
if (it != m_arp_table.end())
return it->value;
}
ARPPacket arp_request;
@@ -74,9 +74,10 @@ namespace Kernel
while (SystemTimer::get().ms_since_boot() < timeout)
{
{
LockGuard _(m_lock);
if (m_arp_table.contains(ipv4_address))
return m_arp_table[ipv4_address];
SpinLockGuard _(m_table_lock);
auto it = m_arp_table.find(ipv4_address);
if (it != m_arp_table.end())
return it->value;
}
Scheduler::get().reschedule();
}
@@ -114,13 +115,15 @@ namespace Kernel
}
case ARPOperation::Reply:
{
LockGuard _(m_lock);
if (m_arp_table.contains(packet.spa))
SpinLockGuard _(m_table_lock);
auto it = m_arp_table.find(packet.spa);
if (it != m_arp_table.end())
{
if (m_arp_table[packet.spa] != packet.sha)
if (it->value != packet.sha)
{
dprintln("Update IPv4 {} MAC to {}", packet.spa, packet.sha);
m_arp_table[packet.spa] = packet.sha;
it->value = packet.sha;
}
}
else

View File

@@ -70,12 +70,14 @@ namespace Kernel
void IPv4Layer::unbind_socket(BAN::RefPtr<NetworkSocket> socket, uint16_t port)
{
LockGuard _(m_lock);
if (m_bound_sockets.contains(port))
{
ASSERT(m_bound_sockets[port].valid());
ASSERT(m_bound_sockets[port].lock() == socket);
m_bound_sockets.remove(port);
SpinLockGuard _(m_bound_socket_lock);
auto it = m_bound_sockets.find(port);
if (it != m_bound_sockets.end())
{
ASSERT(it->value.lock() == socket);
m_bound_sockets.remove(it);
}
}
NetworkManager::get().TmpFileSystem::remove_from_cache(socket);
}
@@ -88,7 +90,7 @@ namespace Kernel
return BAN::Error::from_errno(EAFNOSUPPORT);
auto& sockaddr_in = *reinterpret_cast<const struct sockaddr_in*>(address);
LockGuard _(m_lock);
SpinLockGuard _(m_bound_socket_lock);
uint16_t port = NetworkSocket::PORT_NONE;
for (uint32_t i = 0; i < 100 && port == NetworkSocket::PORT_NONE; i++)
@@ -124,7 +126,7 @@ namespace Kernel
auto& sockaddr_in = *reinterpret_cast<const struct sockaddr_in*>(address);
uint16_t port = BAN::host_to_network_endian(sockaddr_in.sin_port);
LockGuard _(m_lock);
SpinLockGuard _(m_bound_socket_lock);
if (m_bound_sockets.contains(port))
return BAN::Error::from_errno(EADDRINUSE);
@@ -250,13 +252,14 @@ namespace Kernel
BAN::RefPtr<Kernel::NetworkSocket> bound_socket;
{
LockGuard _(m_lock);
if (!m_bound_sockets.contains(dst_port))
SpinLockGuard _(m_bound_socket_lock);
auto it = m_bound_sockets.find(dst_port);
if (it == m_bound_sockets.end())
{
dprintln_if(DEBUG_IPV4, "no one is listening on port {}", dst_port);
return {};
}
bound_socket = m_bound_sockets[dst_port].lock();
bound_socket = it->value.lock();
}
if (!bound_socket)

View File

@@ -1,4 +1,4 @@
#include <kernel/LockGuard.h>
#include <kernel/Lock/LockGuard.h>
#include <kernel/Networking/TCPSocket.h>
#include <kernel/Random.h>
#include <kernel/Timer/Timer.h>
@@ -68,7 +68,7 @@ namespace Kernel
void TCPSocket::on_close_impl()
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (!is_bound())
return;
@@ -103,7 +103,7 @@ namespace Kernel
if (address_len > (socklen_t)sizeof(sockaddr_storage))
address_len = sizeof(sockaddr_storage);
LockGuard _(m_lock);
LockGuard _(m_mutex);
ASSERT(!m_connection_info.has_value());
@@ -139,7 +139,7 @@ namespace Kernel
uint64_t wake_time_ms = SystemTimer::get().ms_since_boot() + 5000;
while (m_state != State::Established)
{
LockFreeGuard free(m_lock);
LockFreeGuard free(m_mutex);
if (SystemTimer::get().ms_since_boot() >= wake_time_ms)
return BAN::Error::from_errno(ECONNREFUSED);
TRY(Thread::current().block_or_eintr_or_waketime(m_semaphore, wake_time_ms, true));
@@ -212,7 +212,7 @@ namespace Kernel
{
case State::Closed:
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
header.syn = 1;
add_tcp_header_option<0, TCPOption::MaximumSeqmentSize>(header, m_interface->payload_mtu() - m_network_layer.header_size());
add_tcp_header_option<4, TCPOption::WindowScale>(header, 0);
@@ -233,7 +233,7 @@ namespace Kernel
break;
case State::CloseWait:
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
header.ack = 1;
header.fin = 1;
m_state = State::LastAck;
@@ -242,7 +242,7 @@ namespace Kernel
}
case State::FinWait1:
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
header.ack = 1;
header.fin = 1;
m_state = State::FinWait2;
@@ -250,7 +250,7 @@ namespace Kernel
}
case State::FinWait2:
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
header.ack = 1;
m_state = State::TimeWait;
m_time_wait_start_ms = SystemTimer::get().ms_since_boot();
@@ -312,7 +312,7 @@ namespace Kernel
if (!header.ack || !header.syn)
break;
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (header.ack_number != m_send_window.current_seq)
{
@@ -345,7 +345,7 @@ namespace Kernel
if (!header.ack)
break;
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (header.fin)
{
@@ -436,7 +436,7 @@ namespace Kernel
set_connection_as_closed();
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (m_should_ack)
{
@@ -518,7 +518,7 @@ namespace Kernel
BAN::ErrorOr<size_t> TCPSocket::recvfrom_impl(BAN::ByteSpan buffer, sockaddr*, socklen_t*)
{
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (m_state == State::Closed)
return BAN::Error::from_errno(ENOTCONN);
@@ -542,7 +542,7 @@ namespace Kernel
case State::Closing: ASSERT_NOT_REACHED();
};
LockFreeGuard free(m_lock);
LockFreeGuard free(m_mutex);
TRY(Thread::current().block_or_eintr_indefinite(m_semaphore));
}
@@ -575,7 +575,7 @@ namespace Kernel
return message.size();
}
LockGuard _(m_lock);
LockGuard _(m_mutex);
if (m_state == State::Closed)
return BAN::Error::from_errno(ENOTCONN);
@@ -602,7 +602,7 @@ namespace Kernel
if (m_send_window.data_size + message.size() <= m_send_window.buffer->size())
break;
LockFreeGuard free(m_lock);
LockFreeGuard free(m_mutex);
TRY(Thread::current().block_or_eintr_indefinite(m_semaphore));
}
@@ -634,7 +634,7 @@ namespace Kernel
case State::Closing: ASSERT_NOT_REACHED();
};
LockFreeGuard free(m_lock);
LockFreeGuard free(m_mutex);
TRY(Thread::current().block_or_eintr_indefinite(m_semaphore));
}

View File

@@ -1,4 +1,3 @@
#include <kernel/LockGuard.h>
#include <kernel/Memory/Heap.h>
#include <kernel/Networking/UDPSocket.h>
#include <kernel/Thread.h>
@@ -46,7 +45,7 @@ namespace Kernel
//auto& header = packet.as<const UDPHeader>();
auto payload = packet.slice(sizeof(UDPHeader));
LockGuard _(m_packet_lock);
SpinLockGuard _(m_packet_lock);
if (m_packets.full())
{
@@ -88,12 +87,12 @@ namespace Kernel
}
ASSERT(m_port != PORT_NONE);
LockGuard _(m_packet_lock);
auto state = m_packet_lock.lock();
while (m_packets.empty())
{
LockFreeGuard free(m_packet_lock);
m_packet_lock.unlock(state);
TRY(Thread::current().block_or_eintr_indefinite(m_packet_semaphore));
state = m_packet_lock.lock();
}
auto packet_info = m_packets.front();
@@ -115,6 +114,8 @@ namespace Kernel
m_packet_total_size -= packet_info.packet_size;
m_packet_lock.unlock(state);
if (address && address_len)
{
if (*address_len > (socklen_t)sizeof(sockaddr_storage))

View File

@@ -51,9 +51,10 @@ namespace Kernel
{
if (is_bound() && !is_bound_to_unused())
{
LockGuard _(s_bound_socket_lock);
if (s_bound_sockets.contains(m_bound_path))
s_bound_sockets.remove(m_bound_path);
SpinLockGuard _(s_bound_socket_lock);
auto it = s_bound_sockets.find(m_bound_path);
if (it != s_bound_sockets.end())
s_bound_sockets.remove(it);
}
}
@@ -71,7 +72,7 @@ namespace Kernel
BAN::RefPtr<UnixDomainSocket> pending;
{
LockGuard _(connection_info.pending_lock);
SpinLockGuard _(connection_info.pending_lock);
pending = connection_info.pending_connections.front();
connection_info.pending_connections.pop();
connection_info.pending_semaphore.unblock();
@@ -120,10 +121,11 @@ namespace Kernel
BAN::RefPtr<UnixDomainSocket> target;
{
LockGuard _(s_bound_socket_lock);
if (!s_bound_sockets.contains(file.canonical_path))
SpinLockGuard _(s_bound_socket_lock);
auto it = s_bound_sockets.find(file.canonical_path);
if (it == s_bound_sockets.end())
return BAN::Error::from_errno(ECONNREFUSED);
target = s_bound_sockets[file.canonical_path].lock();
target = it->value.lock();
if (!target)
return BAN::Error::from_errno(ECONNREFUSED);
}
@@ -150,7 +152,7 @@ namespace Kernel
{
auto& target_info = target->m_info.get<ConnectionInfo>();
{
LockGuard _(target_info.pending_lock);
SpinLockGuard _(target_info.pending_lock);
if (target_info.pending_connections.size() < target_info.pending_connections.capacity())
{
MUST(target_info.pending_connections.push(this));
@@ -205,7 +207,7 @@ namespace Kernel
O_RDWR
));
LockGuard _(s_bound_socket_lock);
SpinLockGuard _(s_bound_socket_lock);
ASSERT(!s_bound_sockets.contains(file.canonical_path));
TRY(s_bound_sockets.emplace(file.canonical_path, TRY(get_weak_ptr())));
m_bound_path = BAN::move(file.canonical_path);
@@ -229,12 +231,12 @@ namespace Kernel
BAN::ErrorOr<void> UnixDomainSocket::add_packet(BAN::ConstByteSpan packet)
{
LockGuard _(m_lock);
auto state = m_packet_lock.lock();
while (m_packet_sizes.full() || m_packet_size_total + packet.size() > s_packet_buffer_size)
{
LockFreeGuard _(m_lock);
m_packet_lock.unlock(state);
TRY(Thread::current().block_or_eintr_indefinite(m_packet_semaphore));
state = m_packet_lock.lock();
}
uint8_t* packet_buffer = reinterpret_cast<uint8_t*>(m_packet_buffer->vaddr() + m_packet_size_total);
@@ -245,6 +247,7 @@ namespace Kernel
m_packet_sizes.push(packet.size());
m_packet_semaphore.unblock();
m_packet_lock.unlock(state);
return {};
}
@@ -318,10 +321,11 @@ namespace Kernel
canonical_path = BAN::move(file.canonical_path);
}
LockGuard _(s_bound_socket_lock);
if (!s_bound_sockets.contains(canonical_path))
SpinLockGuard _(s_bound_socket_lock);
auto it = s_bound_sockets.find(canonical_path);
if (it == s_bound_sockets.end())
return BAN::Error::from_errno(EDESTADDRREQ);
auto target = s_bound_sockets[canonical_path].lock();
auto target = it->value.lock();
if (!target)
return BAN::Error::from_errno(EDESTADDRREQ);
TRY(target->add_packet(message));
@@ -338,10 +342,12 @@ namespace Kernel
return BAN::Error::from_errno(ENOTCONN);
}
auto state = m_packet_lock.lock();
while (m_packet_size_total == 0)
{
LockFreeGuard _(m_lock);
m_packet_lock.unlock(state);
TRY(Thread::current().block_or_eintr_indefinite(m_packet_semaphore));
state = m_packet_lock.lock();
}
uint8_t* packet_buffer = reinterpret_cast<uint8_t*>(m_packet_buffer->vaddr());
@@ -360,6 +366,7 @@ namespace Kernel
m_packet_size_total -= nread;
m_packet_semaphore.unblock();
m_packet_lock.unlock(state);
return nread;
}