summaryrefslogtreecommitdiffstats
path: root/sys/kern/uipc_socket.c
diff options
context:
space:
mode:
authorrwatson <rwatson@FreeBSD.org>2004-10-18 22:19:43 +0000
committerrwatson <rwatson@FreeBSD.org>2004-10-18 22:19:43 +0000
commit4b81ce6dd2658abba782e835143f8008092c1c6f (patch)
treee18ee35e276065c19e56111431141fc111be57fb /sys/kern/uipc_socket.c
parentd2f67f65f7a477ab4e37f6a25e978bdd763f830f (diff)
downloadFreeBSD-src-4b81ce6dd2658abba782e835143f8008092c1c6f.zip
FreeBSD-src-4b81ce6dd2658abba782e835143f8008092c1c6f.tar.gz
Push acquisition of the accept mutex out of sofree() into the caller
(sorele()/sotryfree()): - This permits the caller to acquire the accept mutex before the socket mutex, avoiding sofree() having to drop the socket mutex and re-order, which could lead to races permitting more than one thread to enter sofree() after a socket is ready to be free'd. - This also covers clearing of the so_pcb weak socket reference from the protocol to the socket, preventing races in clearing and evaluation of the reference such that sofree() might be called more than once on the same socket. This appears to close a race I was able to easily trigger by repeatedly opening and resetting TCP connections to a host, in which the tcp_close() code called as a result of the RST raced with the close() of the accepted socket in the user process resulting in simultaneous attempts to de-allocate the same socket. The new locking increases the overhead for operations that may potentially free the socket, so we will want to revise the synchronization strategy here as we normalize the reference counting model for sockets. The use of the accept mutex in freeing of sockets that are not listen sockets is primarily motivated by the potential need to remove the socket from the incomplete connection queue on its parent (listen) socket, so cleaning up the reference model here may allow us to substantially weaken the synchronization requirements. RELENG_5_3 candidate. MFC after: 3 days Reviewed by: dwhite Discussed with: gnn, dwhite, green Reported by: Marc UBM Bocklet <ubm at u-boot-man dot de> Reported by: Vlad <marchenko at gmail dot com>
Diffstat (limited to 'sys/kern/uipc_socket.c')
-rw-r--r--sys/kern/uipc_socket.c7
1 files changed, 4 insertions, 3 deletions
diff --git a/sys/kern/uipc_socket.c b/sys/kern/uipc_socket.c
index 17ecd6a..1a74cff 100644
--- a/sys/kern/uipc_socket.c
+++ b/sys/kern/uipc_socket.c
@@ -227,6 +227,7 @@ socreate(dom, aso, type, proto, cred, td)
SOCK_UNLOCK(so);
error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
if (error) {
+ ACCEPT_LOCK();
SOCK_LOCK(so);
so->so_state |= SS_NOFDREF;
sorele(so);
@@ -333,9 +334,8 @@ sofree(so)
{
struct socket *head;
- SOCK_UNLOCK(so);
- ACCEPT_LOCK();
- SOCK_LOCK(so);
+ ACCEPT_LOCK_ASSERT();
+ SOCK_LOCK_ASSERT(so);
if (so->so_pcb != NULL || (so->so_state & SS_NOFDREF) == 0 ||
so->so_count != 0) {
@@ -467,6 +467,7 @@ drop:
error = error2;
}
discard:
+ ACCEPT_LOCK();
SOCK_LOCK(so);
KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
so->so_state |= SS_NOFDREF;
OpenPOWER on IntegriCloud