Commit 1fd05ba5 authored by Miklos Szeredi's avatar Miklos Szeredi Committed by David S. Miller

[AF_UNIX]: Rewrite garbage collector, fixes race.

Throw out the old mark & sweep garbage collector and put in a
refcounting cycle detecting one.

The old one had a race with recvmsg, that resulted in false positives
and hence data loss.  The old algorithm operated on all unix sockets
in the system, so any additional locking would have meant performance
problems for all users of these.

The new algorithm instead only operates on "in flight" sockets, which
are very rare, and the additional locking for these doesn't negatively
impact the vast majority of users.

In fact it's probable, that there weren't *any* heavy senders of
sockets over sockets, otherwise the above race would have been
discovered long ago.

The patch works OK with the app that exposed the race with the old
code.  The garbage collection has also been verified to work in a few
simple cases.
Signed-off-by: default avatarMiklos Szeredi <mszeredi@suse.cz>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 99d24ede
...@@ -79,9 +79,10 @@ struct unix_sock { ...@@ -79,9 +79,10 @@ struct unix_sock {
struct mutex readlock; struct mutex readlock;
struct sock *peer; struct sock *peer;
struct sock *other; struct sock *other;
struct sock *gc_tree; struct list_head link;
atomic_t inflight; atomic_t inflight;
spinlock_t lock; spinlock_t lock;
unsigned int gc_candidate : 1;
wait_queue_head_t peer_wait; wait_queue_head_t peer_wait;
}; };
#define unix_sk(__sk) ((struct unix_sock *)__sk) #define unix_sk(__sk) ((struct unix_sock *)__sk)
......
...@@ -592,7 +592,8 @@ static struct sock * unix_create1(struct socket *sock) ...@@ -592,7 +592,8 @@ static struct sock * unix_create1(struct socket *sock)
u->dentry = NULL; u->dentry = NULL;
u->mnt = NULL; u->mnt = NULL;
spin_lock_init(&u->lock); spin_lock_init(&u->lock);
atomic_set(&u->inflight, sock ? 0 : -1); atomic_set(&u->inflight, 0);
INIT_LIST_HEAD(&u->link);
mutex_init(&u->readlock); /* single task reading lock */ mutex_init(&u->readlock); /* single task reading lock */
init_waitqueue_head(&u->peer_wait); init_waitqueue_head(&u->peer_wait);
unix_insert_socket(unix_sockets_unbound, sk); unix_insert_socket(unix_sockets_unbound, sk);
...@@ -1134,9 +1135,6 @@ restart: ...@@ -1134,9 +1135,6 @@ restart:
/* take ten and and send info to listening sock */ /* take ten and and send info to listening sock */
spin_lock(&other->sk_receive_queue.lock); spin_lock(&other->sk_receive_queue.lock);
__skb_queue_tail(&other->sk_receive_queue, skb); __skb_queue_tail(&other->sk_receive_queue, skb);
/* Undo artificially decreased inflight after embrion
* is installed to listening socket. */
atomic_inc(&newu->inflight);
spin_unlock(&other->sk_receive_queue.lock); spin_unlock(&other->sk_receive_queue.lock);
unix_state_unlock(other); unix_state_unlock(other);
other->sk_data_ready(other, 0); other->sk_data_ready(other, 0);
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment