From owner-freebsd-net@FreeBSD.ORG Wed Feb 26 22:53:38 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 D504C212 for ; Wed, 26 Feb 2014 22:53:38 +0000 (UTC) Received: from h2.funkthat.com (gate2.funkthat.com [208.87.223.18]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 756E31641 for ; Wed, 26 Feb 2014 22:53:38 +0000 (UTC) Received: from h2.funkthat.com (localhost [127.0.0.1]) by h2.funkthat.com (8.14.3/8.14.3) with ESMTP id s1QMrauJ042402 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 26 Feb 2014 14:53:37 -0800 (PST) (envelope-from jmg@h2.funkthat.com) Received: (from jmg@localhost) by h2.funkthat.com (8.14.3/8.14.3/Submit) id s1QMrab3042401; Wed, 26 Feb 2014 14:53:36 -0800 (PST) (envelope-from jmg) Date: Wed, 26 Feb 2014 14:53:36 -0800 From: John-Mark Gurney To: "Son, Sonny" Subject: Re: socket lock Message-ID: <20140226225336.GG92037@funkthat.com> Mail-Followup-To: "Son, Sonny" , "freebsd-net (freebsd-net@freebsd.org)" References: <3967D1AB1B4E6F4CA4E2329EEB83ED3E30D8C3A7@SACEXCMBX03-PRD.hq.netapp.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3967D1AB1B4E6F4CA4E2329EEB83ED3E30D8C3A7@SACEXCMBX03-PRD.hq.netapp.com> User-Agent: Mutt/1.4.2.3i X-Operating-System: FreeBSD 7.2-RELEASE i386 X-PGP-Fingerprint: 54BA 873B 6515 3F10 9E88 9322 9CB1 8F74 6D3F A396 X-Files: The truth is out there X-URL: http://resnet.uoregon.edu/~gurney_j/ X-Resume: http://resnet.uoregon.edu/~gurney_j/resume.html X-TipJar: bitcoin:13Qmb6AeTgQecazTWph4XasEsP7nGRbAPE X-to-the-FBI-CIA-and-NSA: HI! HOW YA DOIN? can i haz chizburger? X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.2.2 (h2.funkthat.com [127.0.0.1]); Wed, 26 Feb 2014 14:53:37 -0800 (PST) Cc: "freebsd-net \(freebsd-net@freebsd.org\)" 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:53:38 -0000 Son, Sonny wrote this message on Wed, Feb 26, 2014 at 22:47 +0000: > Can somebody explain me how socket data structure-i.e. 'struct socket'-is protected in FreeBSD? It seems that socket is accessed and modified without lock in some places. As an instance, the following code reads and/or modifies various socket fields including so_error without socket lock held: Have you seen this in sys/socketvar.h? /*- * Locking key to struct socket: * (a) constant after allocation, no locking required. * (b) locked by SOCK_LOCK(so). * (c) locked by SOCKBUF_LOCK(&so->so_rcv). * (d) locked by SOCKBUF_LOCK(&so->so_snd). * (e) locked by ACCEPT_LOCK(). * (f) not locked since integer reads/writes are atomic. * (g) used only as a sleep/wakeup address, no value. * (h) locked by global mutex so_global_mtx. */ > int > sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio, > struct mbuf *top, struct mbuf *control, int flags, struct thread *td) > { [...] > if (so->so_error) { > error = so->so_error; > so->so_error = 0; <=========== we do have socket's send buffer lock but not socket lock (, which is socket recv buffer lock) > SOCKBUF_UNLOCK(&so->so_snd); > goto out; > } So, so_error is: u_short so_error; /* (f) error affecting connection */ and f is: * (f) not locked since integer reads/writes are atomic. Though, I'm not so sure u_short counts as an integer.. it maybe should be something like u_register_t, but not sure about this... -- John-Mark Gurney Voice: +1 415 225 5579 "All that I will do, has been done, All that I have, has not."