socket lock
John-Mark Gurney
jmg at funkthat.com
Wed Feb 26 22:53:38 UTC 2014
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."
More information about the freebsd-net
mailing list