Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec...
[linux-block.git] / net / unix / af_unix.c
index a7f707fc4cac575c95c53384685bd89402bfe4ed..321af97c7bbe586dd4c71269cd070ff6cffa5937 100644 (file)
@@ -676,6 +676,16 @@ static int unix_set_peek_off(struct sock *sk, int val)
        return 0;
 }
 
+static void unix_show_fdinfo(struct seq_file *m, struct socket *sock)
+{
+       struct sock *sk = sock->sk;
+       struct unix_sock *u;
+
+       if (sk) {
+               u = unix_sk(sock->sk);
+               seq_printf(m, "scm_fds: %u\n", READ_ONCE(u->scm_stat.nr_fds));
+       }
+}
 
 static const struct proto_ops unix_stream_ops = {
        .family =       PF_UNIX,
@@ -701,6 +711,7 @@ static const struct proto_ops unix_stream_ops = {
        .sendpage =     unix_stream_sendpage,
        .splice_read =  unix_stream_splice_read,
        .set_peek_off = unix_set_peek_off,
+       .show_fdinfo =  unix_show_fdinfo,
 };
 
 static const struct proto_ops unix_dgram_ops = {
@@ -726,6 +737,7 @@ static const struct proto_ops unix_dgram_ops = {
        .mmap =         sock_no_mmap,
        .sendpage =     sock_no_sendpage,
        .set_peek_off = unix_set_peek_off,
+       .show_fdinfo =  unix_show_fdinfo,
 };
 
 static const struct proto_ops unix_seqpacket_ops = {
@@ -751,6 +763,7 @@ static const struct proto_ops unix_seqpacket_ops = {
        .mmap =         sock_no_mmap,
        .sendpage =     sock_no_sendpage,
        .set_peek_off = unix_set_peek_off,
+       .show_fdinfo =  unix_show_fdinfo,
 };
 
 static struct proto unix_proto = {
@@ -788,6 +801,7 @@ static struct sock *unix_create1(struct net *net, struct socket *sock, int kern)
        mutex_init(&u->bindlock); /* single task binding lock */
        init_waitqueue_head(&u->peer_wait);
        init_waitqueue_func_entry(&u->peer_wake, unix_dgram_peer_wake_relay);
+       memset(&u->scm_stat, 0, sizeof(struct scm_stat));
        unix_insert_socket(unix_sockets_unbound(sk), sk);
 out:
        if (sk == NULL)
@@ -1572,6 +1586,28 @@ static bool unix_skb_scm_eq(struct sk_buff *skb,
               unix_secdata_eq(scm, skb);
 }
 
+static void scm_stat_add(struct sock *sk, struct sk_buff *skb)
+{
+       struct scm_fp_list *fp = UNIXCB(skb).fp;
+       struct unix_sock *u = unix_sk(sk);
+
+       lockdep_assert_held(&sk->sk_receive_queue.lock);
+
+       if (unlikely(fp && fp->count))
+               u->scm_stat.nr_fds += fp->count;
+}
+
+static void scm_stat_del(struct sock *sk, struct sk_buff *skb)
+{
+       struct scm_fp_list *fp = UNIXCB(skb).fp;
+       struct unix_sock *u = unix_sk(sk);
+
+       lockdep_assert_held(&sk->sk_receive_queue.lock);
+
+       if (unlikely(fp && fp->count))
+               u->scm_stat.nr_fds -= fp->count;
+}
+
 /*
  *     Send AF_UNIX data.
  */
@@ -1757,7 +1793,10 @@ restart_locked:
        if (sock_flag(other, SOCK_RCVTSTAMP))
                __net_timestamp(skb);
        maybe_add_creds(skb, sock, other);
-       skb_queue_tail(&other->sk_receive_queue, skb);
+       spin_lock(&other->sk_receive_queue.lock);
+       scm_stat_add(other, skb);
+       __skb_queue_tail(&other->sk_receive_queue, skb);
+       spin_unlock(&other->sk_receive_queue.lock);
        unix_state_unlock(other);
        other->sk_data_ready(other);
        sock_put(other);
@@ -1859,7 +1898,10 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
                        goto pipe_err_free;
 
                maybe_add_creds(skb, sock, other);
-               skb_queue_tail(&other->sk_receive_queue, skb);
+               spin_lock(&other->sk_receive_queue.lock);
+               scm_stat_add(other, skb);
+               __skb_queue_tail(&other->sk_receive_queue, skb);
+               spin_unlock(&other->sk_receive_queue.lock);
                unix_state_unlock(other);
                other->sk_data_ready(other);
                sent += size;
@@ -2059,7 +2101,7 @@ static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
 
                skip = sk_peek_offset(sk, flags);
                skb = __skb_try_recv_datagram(sk, &sk->sk_receive_queue, flags,
-                                             NULL, &skip, &err, &last);
+                                             scm_stat_del, &skip, &err, &last);
                if (skb)
                        break;
 
@@ -2354,8 +2396,12 @@ unlock:
 
                        sk_peek_offset_bwd(sk, chunk);
 
-                       if (UNIXCB(skb).fp)
+                       if (UNIXCB(skb).fp) {
+                               spin_lock(&sk->sk_receive_queue.lock);
+                               scm_stat_del(sk, skb);
+                               spin_unlock(&sk->sk_receive_queue.lock);
                                unix_detach_fds(&scm, skb);
+                       }
 
                        if (unix_skb_len(skb))
                                break;
@@ -2866,7 +2912,7 @@ static int __init af_unix_init(void)
 {
        int rc = -1;
 
-       BUILD_BUG_ON(sizeof(struct unix_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
+       BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof_field(struct sk_buff, cb));
 
        rc = proto_register(&unix_proto, 1);
        if (rc != 0) {