From owner-freebsd-hackers@FreeBSD.ORG Tue May 29 23:13:59 2012 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 416641065672 for ; Tue, 29 May 2012 23:13:59 +0000 (UTC) (envelope-from rwatson@FreeBSD.org) Received: from cyrus.watson.org (cyrus.watson.org [65.122.17.42]) by mx1.freebsd.org (Postfix) with ESMTP id E233A8FC0C for ; Tue, 29 May 2012 23:13:58 +0000 (UTC) Received: from [192.168.5.76] (unknown [64.134.67.0]) by cyrus.watson.org (Postfix) with ESMTPSA id 4727346B06; Tue, 29 May 2012 19:13:58 -0400 (EDT) References: In-Reply-To: Mime-Version: 1.0 (1.0) Message-Id: X-Mailer: iPad Mail (9B206) From: "Robert N. M. Watson" Date: Tue, 29 May 2012 19:14:51 -0400 To: vasanth rao naik sabavat Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Cc: "freebsd-hackers@freebsd.org" Subject: Re: SMP: protocol control block protection for a multithreaded process (ex: udp). X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 29 May 2012 23:13:59 -0000 On 29 May 2012, at 18:06, vasanth rao naik sabavat wrote: > My main concern is about the protocol control block "inp", a reference in t= he socket structure. the udp_detach() free'es the inp but there is a potenti= al for other thread running udp_* functions to get hold of the reference? Al= so, sofree() calls SOCK_UNLOCK() which potentially may allow other thread of= the same process to enter into the udp_* functions? I am not sure if that i= s ever possible. Hi Vasanth: Are you chasing a bug, or is this a more intellectual exercise in understand= ing the stack? The scenario you are describing should be prevented by the so= cket-layer reference model and so should not occur unless a bug is manifesti= ng. I am happy to answer questions about the model, but if you are running i= nto problems it might be easier if I gave more directed advice about how to t= rack problem. When a socket system call takes place on a UDP socket, several data structur= es are involved, each with its own life cycle and reference model. Most of t= he time, calls like send() use a per-system call counted reference to the fi= le descriptor to ensure valid access down the stack through the socket and i= npcb code -- this works because the file descriptor owns a reference to the s= ocket, and the socket owns a reference to the inpcb. udp_detach is called on= ly when the reference count on the socket reaches zero, so the references do= wn the stack held by invoking threads should prevent udp_detach from being i= nvoked while they are running. The right way to view stability here is via t= he reference model, not the locking model: locks come and go as data is proc= essed and packets move around, but it is the reference counts that prevent f= reeing of the sockt and hence inpcb. The perspective is slightly different l= ooking "up" from the input side, where we sometimes do use locks to ensure s= tability, but that is rarely the case in the down path. In UDP, unlike with T= CP, socket close can only be initiated from the down path, not the up path. If you haven't already read the large comment at the top of uipc_socket.c on= the socket life cycle, it would be a good idea to do so, as it lays out som= e of the principles at play here. Robert > Thanks, > Vasanth >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 > On Tue, May 29, 2012 at 5:06 PM, Robert Watson wrote= : >=20 > On Tue, 29 May 2012, vasanth rao naik sabavat wrote: >=20 > Can somebody please reply to this email. >=20 > basically, can udp_detach() and udp_send() execute simultaneously for a pr= ocess with multiple threads? if yes, then inp reference in udp_send() will b= e stale if udp_detach() free's the inp? >=20 > You are confusing application-level close() with an actual close in the so= cket implementation. The socket will remain allocated as long as there are c= onsumers using it, which is ensured through a reference count on the socket,= regardless of close(). That isn't to say that there aren't bugs -- this st= uff is pretty complex -- but the life cycle and synchronisation models aroun= d sockets should prevent the scenario you are describing from occurring. >=20 > Robert >=20 >=20 > Thanks, > Vasanth >=20 >=20 >=20 > On Tue, May 29, 2012 at 10:53 AM, vasanth rao naik sabavat < > vasanth.raonaik@gmail.com> wrote: >=20 > Hi, >=20 > In case of a Multicore cpu system running a multithreaded process. >=20 > For protocol control blocks there is no protection provided in the FreeBSD= > 9. For example, udp_close() and udp_send() access the inp before taking th= e > lock. Couldn't this cause the inp inconsistency on a multithreaded process= > running on multicore cpu system? >=20 > Say, If the two threads of a process are concurrently executing socket > send and socket close say on a udp connection (this can happen in case of > poorly written user code.). > udp_close() will access the inp on one cpu and udp_send() will access the > inp on another cpu. it is possible that udp_close() gets the locks first > and free's the inp before udp_send() has a chance to run? >=20 > Am I missing anything? >=20 > Thanks, > Vasanth >=20 >=20 >=20 >=20 >=20 > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"= >=20 >=20