summaryrefslogtreecommitdiffstats
path: root/sys/kern/uipc_socket.c
diff options
context:
space:
mode:
authorrwatson <rwatson@FreeBSD.org>2006-04-01 15:42:02 +0000
committerrwatson <rwatson@FreeBSD.org>2006-04-01 15:42:02 +0000
commit5479e5d69217e0a6876338fc7cde604067b679ca (patch)
tree7431f8c0d78c14bc446d524dcda6a00888732015 /sys/kern/uipc_socket.c
parent68ff3be0b395955e8feac72262824b90a155a710 (diff)
downloadFreeBSD-src-5479e5d69217e0a6876338fc7cde604067b679ca.zip
FreeBSD-src-5479e5d69217e0a6876338fc7cde604067b679ca.tar.gz
Chance protocol switch method pru_detach() so that it returns void
rather than an error. Detaches do not "fail", they other occur or the protocol flags SS_PROTOREF to take ownership of the socket. soclose() no longer looks at so_pcb to see if it's NULL, relying entirely on the protocol to decide whether it's time to free the socket or not using SS_PROTOREF. so_pcb is now entirely owned and managed by the protocol code. Likewise, no longer test so_pcb in other socket functions, such as soreceive(), which have no business digging into protocol internals. Protocol detach routines no longer try to free the socket on detach, this is performed in the socket code if the protocol permits it. In rts_detach(), no longer test for rp != NULL in detach, and likewise in other protocols that don't permit a NULL so_pcb, reduce the incidence of testing for it during detach. netinet and netinet6 are not fully updated to this change, which will be in an upcoming commit. In their current state they may leak memory or panic. MFC after: 3 months
Diffstat (limited to 'sys/kern/uipc_socket.c')
-rw-r--r--sys/kern/uipc_socket.c38
1 files changed, 19 insertions, 19 deletions
diff --git a/sys/kern/uipc_socket.c b/sys/kern/uipc_socket.c
index 35bb147..dadb06d 100644
--- a/sys/kern/uipc_socket.c
+++ b/sys/kern/uipc_socket.c
@@ -387,13 +387,18 @@ solisten_proto(so, backlog)
/*
* Attempt to free a socket. This should really be sotryfree().
*
- * We free the socket if the protocol is no longer interested in the socket,
- * there's no file descriptor reference, and the refcount is 0. While the
- * calling macro sotryfree() tests the refcount, sofree() has to test it
- * again as it's possible to race with an accept()ing thread if the socket is
- * in an listen queue of a listen socket, as being in the listen queue
- * doesn't elevate the reference count. sofree() acquires the accept mutex
- * early for this test in order to avoid that race.
+ * sofree() will succeed if:
+ *
+ * - There are no outstanding file descriptor references or related consumers
+ * (so_count == 0).
+ *
+ * - The socket has been closed by user space, if ever open (SS_NOFDREF).
+ *
+ * - The protocol does not have an outstanding strong reference on the socket
+ * (SS_PROTOREF).
+ *
+ * Otherwise, it will quietly abort so that a future call to sofree(), when
+ * conditions are right, can succeed.
*/
void
sofree(so)
@@ -404,8 +409,8 @@ sofree(so)
ACCEPT_LOCK_ASSERT();
SOCK_LOCK_ASSERT(so);
- if (so->so_pcb != NULL || (so->so_state & SS_NOFDREF) == 0 ||
- so->so_count != 0 || (so->so_state & SS_PROTOREF)) {
+ if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
+ (so->so_state & SS_PROTOREF)) {
SOCK_UNLOCK(so);
ACCEPT_UNLOCK();
return;
@@ -447,6 +452,7 @@ sofree(so)
so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
SOCK_UNLOCK(so);
ACCEPT_UNLOCK();
+
SOCKBUF_LOCK(&so->so_snd);
so->so_snd.sb_flags |= SB_NOINTR;
(void)sblock(&so->so_snd, M_WAITOK);
@@ -507,8 +513,6 @@ soclose(so)
}
ACCEPT_UNLOCK();
}
- if (so->so_pcb == NULL)
- goto discard;
if (so->so_state & SS_ISCONNECTED) {
if ((so->so_state & SS_ISDISCONNECTING) == 0) {
error = sodisconnect(so);
@@ -527,13 +531,9 @@ soclose(so)
}
}
}
+
drop:
- if (so->so_pcb != NULL) {
- int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
- if (error == 0)
- error = error2;
- }
-discard:
+ (*so->so_proto->pr_usrreqs->pru_detach)(so);
ACCEPT_LOCK();
SOCK_LOCK(so);
KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
@@ -1602,7 +1602,7 @@ dontblock:
* Notify the protocol that some data has been
* drained before blocking.
*/
- if (pr->pr_flags & PR_WANTRCVD && so->so_pcb != NULL) {
+ if (pr->pr_flags & PR_WANTRCVD) {
SOCKBUF_UNLOCK(&so->so_rcv);
(*pr->pr_usrreqs->pru_rcvd)(so, flags);
SOCKBUF_LOCK(&so->so_rcv);
@@ -1646,7 +1646,7 @@ dontblock:
* ACK will be generated on return to TCP.
*/
if (!(flags & MSG_SOCALLBCK) &&
- (pr->pr_flags & PR_WANTRCVD) && so->so_pcb) {
+ (pr->pr_flags & PR_WANTRCVD)) {
SOCKBUF_UNLOCK(&so->so_rcv);
(*pr->pr_usrreqs->pru_rcvd)(so, flags);
SOCKBUF_LOCK(&so->so_rcv);
OpenPOWER on IntegriCloud