Page MenuHomeFreeBSD

socket: Properly interlock when transitioning to a listening socket
ClosedPublic

Authored by markj on Aug 24 2021, 2:01 PM.
Tags
None
Referenced Files
Unknown Object (File)
Tue, Apr 30, 11:05 AM
Unknown Object (File)
Mar 10 2024, 11:11 PM
Unknown Object (File)
Mar 10 2024, 11:11 PM
Unknown Object (File)
Mar 10 2024, 11:11 PM
Unknown Object (File)
Mar 10 2024, 11:11 PM
Unknown Object (File)
Mar 10 2024, 10:59 PM
Unknown Object (File)
Feb 22 2024, 2:36 PM
Unknown Object (File)
Feb 16 2024, 5:33 AM

Details

Summary

Currently, most protocols transition to a listening socket with
something like the following:

SOCK_LOCK(so);
error = solisten_proto_check(so);
if (error) {
	SOCK_UNLOCK(so);
	return (error);
}
solisten_proto(so);
SOCK_UNLOCK(so);

solisten_proto_check() fails if the socket is connected or connecting.
However, the socket lock is not used to initiate I/O, so this pattern is
racy.

The change modifies solisten_proto_check() to additionally acquire
socket buffer locks, and the calling thread holds them until
solisten_proto() or solisten_proto_abort() is called. Now that the
socket buffer locks are preserved by listen(2), this change allows
socket I/O paths to properly interlock with listen(2). For an
as-yet-unfixed example of what I'm talking about, look at
soo_aio_queue(): it blindly assumes that it can safely lock a sockbuf
and queue asynchronous I/O without checking for a listening socket.
Without these changes, it is impossible to perform that check correctly
without also acquiring the socket lock.

The change makes listen(2) rather heavyweight, but I think this is the
right tradeoff: listen(2) is called comparatively rarely, and we don't
want to penalize I/O performance for rare cases.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

markj requested review of this revision.Aug 24 2021, 2:01 PM

Rebase on top of D31757. This fixes an LOR in sctp_listen().

This revision is now accepted and ready to land.Sep 1 2021, 8:20 AM
tuexen requested changes to this revision.Sep 1 2021, 8:22 AM
This revision now requires changes to proceed.Sep 1 2021, 8:22 AM
This revision is now accepted and ready to land.Sep 1 2021, 8:22 AM