From owner-freebsd-net@FreeBSD.ORG Wed Feb 26 22:47:35 2014 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 49E6CFC6 for ; Wed, 26 Feb 2014 22:47:35 +0000 (UTC) Received: from mx11.netapp.com (mx11.netapp.com [216.240.18.76]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 220FE159B for ; Wed, 26 Feb 2014 22:47:34 +0000 (UTC) X-IronPort-AV: E=Sophos;i="4.97,550,1389772800"; d="scan'208,217";a="105149062" Received: from vmwexceht01-prd.hq.netapp.com ([10.106.76.239]) by mx11-out.netapp.com with ESMTP; 26 Feb 2014 14:47:17 -0800 Received: from SACEXCMBX03-PRD.hq.netapp.com ([169.254.5.58]) by vmwexceht01-prd.hq.netapp.com ([10.106.76.239]) with mapi id 14.03.0123.003; Wed, 26 Feb 2014 14:47:17 -0800 From: "Son, Sonny" To: "freebsd-net (freebsd-net@freebsd.org)" Subject: socket lock Thread-Topic: socket lock Thread-Index: Ac8zQCCXLObL7+kFTXuTI0rB9CfzMw== Date: Wed, 26 Feb 2014 22:47:16 +0000 Message-ID: <3967D1AB1B4E6F4CA4E2329EEB83ED3E30D8C3A7@SACEXCMBX03-PRD.hq.netapp.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.106.53.53] MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.17 X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 26 Feb 2014 22:47:35 -0000 Hi all, Can somebody explain me how socket data structure-i.e. 'struct socket'-is p= rotected in FreeBSD? It seems that socket is accessed and modified without = lock in some places. As an instance, the following code reads and/or modifi= es various socket fields including so_error without socket lock held: int sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio, struct mbuf *top, struct mbuf *control, int flags, struct thread *td) { long space, resid; int clen =3D 0, error, dontroute; #ifdef ZERO_COPY_SOCKETS int atomic =3D sosendallatonce(so) || top; #endif KASSERT(so->so_type =3D=3D SOCK_DGRAM, ("sodgram_send: !SOCK_DGRAM"= )); KASSERT(so->so_proto->pr_flags & PR_ATOMIC, ("sodgram_send: !PR_ATOMIC")); if (uio !=3D NULL) resid =3D uio->uio_resid; else resid =3D top->m_pkthdr.len; /* * In theory resid should be unsigned. However, space must be * signed, as it might be less than 0 if we over-committed, and we * must use a signed comparison of space and resid. On the other * hand, a negative resid causes us to loop sending 0-length * segments to the protocol. */ if (resid < 0) { error =3D EINVAL; goto out; } dontroute =3D (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) =3D= =3D 0; if (td !=3D NULL) td->td_ru.ru_msgsnd++; if (control !=3D NULL) clen =3D control->m_len; SOCKBUF_LOCK(&so->so_snd); if (so->so_snd.sb_state & SBS_CANTSENDMORE) { SOCKBUF_UNLOCK(&so->so_snd); error =3D EPIPE; goto out; } if (so->so_error) { error =3D so->so_error; so->so_error =3D 0; <=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D we do have socket's send buffer lock but not socket lock (, which is so= cket recv buffer lock) SOCKBUF_UNLOCK(&so->so_snd); goto out; } I am sorry if this was already discussed before... Thank you!