Merge branch 'i2c-embedded/for-next' of git://git.pengutronix.de/git/wsa/linux
[firefly-linux-kernel-4.4.55.git] / net / unix / af_unix.c
index 641f2e47f16520603d168eccf4aa58aeee1757c0..79981d97bc9c013904062b1bedfd1ebcae97f845 100644 (file)
 #include <net/checksum.h>
 #include <linux/security.h>
 
-struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1];
+struct hlist_head unix_socket_table[2 * UNIX_HASH_SIZE];
 EXPORT_SYMBOL_GPL(unix_socket_table);
 DEFINE_SPINLOCK(unix_table_lock);
 EXPORT_SYMBOL_GPL(unix_table_lock);
 static atomic_long_t unix_nr_socks;
 
-#define unix_sockets_unbound   (&unix_socket_table[UNIX_HASH_SIZE])
 
-#define UNIX_ABSTRACT(sk)      (unix_sk(sk)->addr->hash != UNIX_HASH_SIZE)
+static struct hlist_head *unix_sockets_unbound(void *addr)
+{
+       unsigned long hash = (unsigned long)addr;
+
+       hash ^= hash >> 16;
+       hash ^= hash >> 8;
+       hash %= UNIX_HASH_SIZE;
+       return &unix_socket_table[UNIX_HASH_SIZE + hash];
+}
+
+#define UNIX_ABSTRACT(sk)      (unix_sk(sk)->addr->hash < UNIX_HASH_SIZE)
 
 #ifdef CONFIG_SECURITY_NETWORK
 static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
@@ -645,7 +654,7 @@ static struct sock *unix_create1(struct net *net, struct socket *sock)
        INIT_LIST_HEAD(&u->link);
        mutex_init(&u->readlock); /* single task reading lock */
        init_waitqueue_head(&u->peer_wait);
-       unix_insert_socket(unix_sockets_unbound, sk);
+       unix_insert_socket(unix_sockets_unbound(sk), sk);
 out:
        if (sk == NULL)
                atomic_long_dec(&unix_nr_socks);
@@ -2239,47 +2248,54 @@ static unsigned int unix_dgram_poll(struct file *file, struct socket *sock,
 }
 
 #ifdef CONFIG_PROC_FS
-static struct sock *first_unix_socket(int *i)
+
+#define BUCKET_SPACE (BITS_PER_LONG - (UNIX_HASH_BITS + 1) - 1)
+
+#define get_bucket(x) ((x) >> BUCKET_SPACE)
+#define get_offset(x) ((x) & ((1L << BUCKET_SPACE) - 1))
+#define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o))
+
+static struct sock *unix_from_bucket(struct seq_file *seq, loff_t *pos)
 {
-       for (*i = 0; *i <= UNIX_HASH_SIZE; (*i)++) {
-               if (!hlist_empty(&unix_socket_table[*i]))
-                       return __sk_head(&unix_socket_table[*i]);
+       unsigned long offset = get_offset(*pos);
+       unsigned long bucket = get_bucket(*pos);
+       struct sock *sk;
+       unsigned long count = 0;
+
+       for (sk = sk_head(&unix_socket_table[bucket]); sk; sk = sk_next(sk)) {
+               if (sock_net(sk) != seq_file_net(seq))
+                       continue;
+               if (++count == offset)
+                       break;
        }
-       return NULL;
+
+       return sk;
 }
 
-static struct sock *next_unix_socket(int *i, struct sock *s)
+static struct sock *unix_next_socket(struct seq_file *seq,
+                                    struct sock *sk,
+                                    loff_t *pos)
 {
-       struct sock *next = sk_next(s);
-       /* More in this chain? */
-       if (next)
-               return next;
-       /* Look for next non-empty chain. */
-       for ((*i)++; *i <= UNIX_HASH_SIZE; (*i)++) {
-               if (!hlist_empty(&unix_socket_table[*i]))
-                       return __sk_head(&unix_socket_table[*i]);
+       unsigned long bucket;
+
+       while (sk > (struct sock *)SEQ_START_TOKEN) {
+               sk = sk_next(sk);
+               if (!sk)
+                       goto next_bucket;
+               if (sock_net(sk) == seq_file_net(seq))
+                       return sk;
        }
-       return NULL;
-}
 
-struct unix_iter_state {
-       struct seq_net_private p;
-       int i;
-};
+       do {
+               sk = unix_from_bucket(seq, pos);
+               if (sk)
+                       return sk;
 
-static struct sock *unix_seq_idx(struct seq_file *seq, loff_t pos)
-{
-       struct unix_iter_state *iter = seq->private;
-       loff_t off = 0;
-       struct sock *s;
+next_bucket:
+               bucket = get_bucket(*pos) + 1;
+               *pos = set_bucket_offset(bucket, 1);
+       } while (bucket < ARRAY_SIZE(unix_socket_table));
 
-       for (s = first_unix_socket(&iter->i); s; s = next_unix_socket(&iter->i, s)) {
-               if (sock_net(s) != seq_file_net(seq))
-                       continue;
-               if (off == pos)
-                       return s;
-               ++off;
-       }
        return NULL;
 }
 
@@ -2287,22 +2303,20 @@ static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
        __acquires(unix_table_lock)
 {
        spin_lock(&unix_table_lock);
-       return *pos ? unix_seq_idx(seq, *pos - 1) : SEQ_START_TOKEN;
+
+       if (!*pos)
+               return SEQ_START_TOKEN;
+
+       if (get_bucket(*pos) >= ARRAY_SIZE(unix_socket_table))
+               return NULL;
+
+       return unix_next_socket(seq, NULL, pos);
 }
 
 static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 {
-       struct unix_iter_state *iter = seq->private;
-       struct sock *sk = v;
        ++*pos;
-
-       if (v == SEQ_START_TOKEN)
-               sk = first_unix_socket(&iter->i);
-       else
-               sk = next_unix_socket(&iter->i, sk);
-       while (sk && (sock_net(sk) != seq_file_net(seq)))
-               sk = next_unix_socket(&iter->i, sk);
-       return sk;
+       return unix_next_socket(seq, v, pos);
 }
 
 static void unix_seq_stop(struct seq_file *seq, void *v)
@@ -2365,7 +2379,7 @@ static const struct seq_operations unix_seq_ops = {
 static int unix_seq_open(struct inode *inode, struct file *file)
 {
        return seq_open_net(inode, file, &unix_seq_ops,
-                           sizeof(struct unix_iter_state));
+                           sizeof(struct seq_net_private));
 }
 
 static const struct file_operations unix_seq_fops = {