Kernel: Fix spinlock leaks with unix sockets

This commit is contained in:
Bananymous 2025-08-17 23:58:02 +03:00
parent 9d6656451a
commit 0066b20413
1 changed files with 4 additions and 12 deletions

View File

@ -289,10 +289,10 @@ namespace Kernel
BAN::ErrorOr<void> UnixDomainSocket::add_packet(BAN::ConstByteSpan packet) BAN::ErrorOr<void> UnixDomainSocket::add_packet(BAN::ConstByteSpan packet)
{ {
auto state = m_packet_lock.lock(); SpinLockGuard guard(m_packet_lock);
while (m_packet_sizes.full() || m_packet_size_total + packet.size() > s_packet_buffer_size) while (m_packet_sizes.full() || m_packet_size_total + packet.size() > s_packet_buffer_size)
{ {
SpinLockAsMutex smutex(m_packet_lock, state); SpinLockGuardAsMutex smutex(guard);
TRY(Thread::current().block_or_eintr_indefinite(m_packet_thread_blocker, &smutex)); TRY(Thread::current().block_or_eintr_indefinite(m_packet_thread_blocker, &smutex));
} }
@ -304,7 +304,6 @@ namespace Kernel
m_packet_sizes.push(packet.size()); m_packet_sizes.push(packet.size());
m_packet_thread_blocker.unblock(); m_packet_thread_blocker.unblock();
m_packet_lock.unlock(state);
epoll_notify(EPOLLIN); epoll_notify(EPOLLIN);
@ -414,7 +413,7 @@ namespace Kernel
BAN::ErrorOr<size_t> UnixDomainSocket::recvfrom_impl(BAN::ByteSpan buffer, sockaddr*, socklen_t*) BAN::ErrorOr<size_t> UnixDomainSocket::recvfrom_impl(BAN::ByteSpan buffer, sockaddr*, socklen_t*)
{ {
auto state = m_packet_lock.lock(); SpinLockGuard guard(m_packet_lock);
while (m_packet_size_total == 0) while (m_packet_size_total == 0)
{ {
if (m_info.has<ConnectionInfo>()) if (m_info.has<ConnectionInfo>())
@ -422,18 +421,12 @@ namespace Kernel
auto& connection_info = m_info.get<ConnectionInfo>(); auto& connection_info = m_info.get<ConnectionInfo>();
bool expected = true; bool expected = true;
if (connection_info.target_closed.compare_exchange(expected, false)) if (connection_info.target_closed.compare_exchange(expected, false))
{
m_packet_lock.unlock(state);
return 0; return 0;
}
if (!connection_info.connection) if (!connection_info.connection)
{
m_packet_lock.unlock(state);
return BAN::Error::from_errno(ENOTCONN); return BAN::Error::from_errno(ENOTCONN);
}
} }
SpinLockAsMutex smutex(m_packet_lock, state); SpinLockGuardAsMutex smutex(guard);
TRY(Thread::current().block_or_eintr_indefinite(m_packet_thread_blocker, &smutex)); TRY(Thread::current().block_or_eintr_indefinite(m_packet_thread_blocker, &smutex));
} }
@ -453,7 +446,6 @@ namespace Kernel
m_packet_size_total -= nread; m_packet_size_total -= nread;
m_packet_thread_blocker.unblock(); m_packet_thread_blocker.unblock();
m_packet_lock.unlock(state);
epoll_notify(EPOLLOUT); epoll_notify(EPOLLOUT);