Kernel/Net: Iron out the locking mechanism across the subsystem

There is a big mix of LockRefPtrs all over the Networking subsystem, as
well as lots of room for improvements with our locking patterns, which
this commit will not pursue, but will give a good start for such work.

To deal with this situation, we change the following things:
- Creating instances of NetworkAdapter should always yield a non-locking
  NonnullRefPtr. Acquiring an instance from the NetworkingManagement
  should give a simple RefPtr,as giving LockRefPtr does not really
  protect from concurrency problems in such case.
- Since NetworkingManagement works with normal RefPtrs we should
  protect all instances of RefPtr<NetworkAdapter> with SpinlockProtected
  to ensure references are gone unexpectedly.
- Protect the so_error class member with a proper spinlock. This happens
  to be important because the clear_so_error() method lacked any proper
  locking measures. It also helps preventing a possible TOCTOU when we
  might do a more fine-grained locking in the Socket code, so this could
  be definitely a start for this.
- Change unnecessary LockRefPtr<PacketWithTimestamp> in the structure
  of OutgoingPacket to a simple RefPtr<PacketWithTimestamp> as the whole
  list should be MutexProtected.
This commit is contained in:
Liav A 2023-04-11 03:50:15 +03:00 committed by Linus Groh
parent bd7d4513bf
commit 7c1f645e27
Notes: sideshowbarker 2024-07-17 00:53:02 +09:00
13 changed files with 93 additions and 83 deletions

View file

@ -35,7 +35,7 @@ UNMAP_AFTER_INIT NetworkingManagement::NetworkingManagement()
{
}
NonnullLockRefPtr<NetworkAdapter> NetworkingManagement::loopback_adapter() const
NonnullRefPtr<NetworkAdapter> NetworkingManagement::loopback_adapter() const
{
return *m_loopback_adapter;
}
@ -56,13 +56,13 @@ ErrorOr<void> NetworkingManagement::try_for_each(Function<ErrorOr<void>(NetworkA
});
}
LockRefPtr<NetworkAdapter> NetworkingManagement::from_ipv4_address(IPv4Address const& address) const
RefPtr<NetworkAdapter> NetworkingManagement::from_ipv4_address(IPv4Address const& address) const
{
if (address[0] == 0 && address[1] == 0 && address[2] == 0 && address[3] == 0)
return m_loopback_adapter;
if (address[0] == 127)
return m_loopback_adapter;
return m_adapters.with([&](auto& adapters) -> LockRefPtr<NetworkAdapter> {
return m_adapters.with([&](auto& adapters) -> RefPtr<NetworkAdapter> {
for (auto& adapter : adapters) {
if (adapter->ipv4_address() == address || adapter->ipv4_broadcast() == address)
return adapter;
@ -71,9 +71,9 @@ LockRefPtr<NetworkAdapter> NetworkingManagement::from_ipv4_address(IPv4Address c
});
}
LockRefPtr<NetworkAdapter> NetworkingManagement::lookup_by_name(StringView name) const
RefPtr<NetworkAdapter> NetworkingManagement::lookup_by_name(StringView name) const
{
return m_adapters.with([&](auto& adapters) -> LockRefPtr<NetworkAdapter> {
return m_adapters.with([&](auto& adapters) -> RefPtr<NetworkAdapter> {
for (auto& adapter : adapters) {
if (adapter->name() == name)
return adapter;