@@ -676,6 +676,16 @@ static int unix_set_peek_off(struct sock *sk, int val)
676
676
return 0 ;
677
677
}
678
678
679
+ static void unix_show_fdinfo (struct seq_file * m , struct socket * sock )
680
+ {
681
+ struct sock * sk = sock -> sk ;
682
+ struct unix_sock * u ;
683
+
684
+ if (sk ) {
685
+ u = unix_sk (sock -> sk );
686
+ seq_printf (m , "scm_fds: %u\n" , READ_ONCE (u -> scm_stat .nr_fds ));
687
+ }
688
+ }
679
689
680
690
static const struct proto_ops unix_stream_ops = {
681
691
.family = PF_UNIX ,
@@ -701,6 +711,7 @@ static const struct proto_ops unix_stream_ops = {
701
711
.sendpage = unix_stream_sendpage ,
702
712
.splice_read = unix_stream_splice_read ,
703
713
.set_peek_off = unix_set_peek_off ,
714
+ .show_fdinfo = unix_show_fdinfo ,
704
715
};
705
716
706
717
static const struct proto_ops unix_dgram_ops = {
@@ -726,6 +737,7 @@ static const struct proto_ops unix_dgram_ops = {
726
737
.mmap = sock_no_mmap ,
727
738
.sendpage = sock_no_sendpage ,
728
739
.set_peek_off = unix_set_peek_off ,
740
+ .show_fdinfo = unix_show_fdinfo ,
729
741
};
730
742
731
743
static const struct proto_ops unix_seqpacket_ops = {
@@ -751,6 +763,7 @@ static const struct proto_ops unix_seqpacket_ops = {
751
763
.mmap = sock_no_mmap ,
752
764
.sendpage = sock_no_sendpage ,
753
765
.set_peek_off = unix_set_peek_off ,
766
+ .show_fdinfo = unix_show_fdinfo ,
754
767
};
755
768
756
769
static struct proto unix_proto = {
@@ -788,6 +801,7 @@ static struct sock *unix_create1(struct net *net, struct socket *sock, int kern)
788
801
mutex_init (& u -> bindlock ); /* single task binding lock */
789
802
init_waitqueue_head (& u -> peer_wait );
790
803
init_waitqueue_func_entry (& u -> peer_wake , unix_dgram_peer_wake_relay );
804
+ memset (& u -> scm_stat , 0 , sizeof (struct scm_stat ));
791
805
unix_insert_socket (unix_sockets_unbound (sk ), sk );
792
806
out :
793
807
if (sk == NULL )
@@ -1572,6 +1586,28 @@ static bool unix_skb_scm_eq(struct sk_buff *skb,
1572
1586
unix_secdata_eq (scm , skb );
1573
1587
}
1574
1588
1589
+ static void scm_stat_add (struct sock * sk , struct sk_buff * skb )
1590
+ {
1591
+ struct scm_fp_list * fp = UNIXCB (skb ).fp ;
1592
+ struct unix_sock * u = unix_sk (sk );
1593
+
1594
+ lockdep_assert_held (& sk -> sk_receive_queue .lock );
1595
+
1596
+ if (unlikely (fp && fp -> count ))
1597
+ u -> scm_stat .nr_fds += fp -> count ;
1598
+ }
1599
+
1600
+ static void scm_stat_del (struct sock * sk , struct sk_buff * skb )
1601
+ {
1602
+ struct scm_fp_list * fp = UNIXCB (skb ).fp ;
1603
+ struct unix_sock * u = unix_sk (sk );
1604
+
1605
+ lockdep_assert_held (& sk -> sk_receive_queue .lock );
1606
+
1607
+ if (unlikely (fp && fp -> count ))
1608
+ u -> scm_stat .nr_fds -= fp -> count ;
1609
+ }
1610
+
1575
1611
/*
1576
1612
* Send AF_UNIX data.
1577
1613
*/
@@ -1757,7 +1793,10 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
1757
1793
if (sock_flag (other , SOCK_RCVTSTAMP ))
1758
1794
__net_timestamp (skb );
1759
1795
maybe_add_creds (skb , sock , other );
1760
- skb_queue_tail (& other -> sk_receive_queue , skb );
1796
+ spin_lock (& other -> sk_receive_queue .lock );
1797
+ scm_stat_add (other , skb );
1798
+ __skb_queue_tail (& other -> sk_receive_queue , skb );
1799
+ spin_unlock (& other -> sk_receive_queue .lock );
1761
1800
unix_state_unlock (other );
1762
1801
other -> sk_data_ready (other );
1763
1802
sock_put (other );
@@ -1859,7 +1898,10 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
1859
1898
goto pipe_err_free ;
1860
1899
1861
1900
maybe_add_creds (skb , sock , other );
1862
- skb_queue_tail (& other -> sk_receive_queue , skb );
1901
+ spin_lock (& other -> sk_receive_queue .lock );
1902
+ scm_stat_add (other , skb );
1903
+ __skb_queue_tail (& other -> sk_receive_queue , skb );
1904
+ spin_unlock (& other -> sk_receive_queue .lock );
1863
1905
unix_state_unlock (other );
1864
1906
other -> sk_data_ready (other );
1865
1907
sent += size ;
@@ -2058,8 +2100,8 @@ static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
2058
2100
mutex_lock (& u -> iolock );
2059
2101
2060
2102
skip = sk_peek_offset (sk , flags );
2061
- skb = __skb_try_recv_datagram (sk , flags , NULL , & skip , & err ,
2062
- & last );
2103
+ skb = __skb_try_recv_datagram (sk , flags , scm_stat_del ,
2104
+ & skip , & err , & last );
2063
2105
if (skb )
2064
2106
break ;
2065
2107
@@ -2353,8 +2395,12 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
2353
2395
2354
2396
sk_peek_offset_bwd (sk , chunk );
2355
2397
2356
- if (UNIXCB (skb ).fp )
2398
+ if (UNIXCB (skb ).fp ) {
2399
+ spin_lock (& sk -> sk_receive_queue .lock );
2400
+ scm_stat_del (sk , skb );
2401
+ spin_unlock (& sk -> sk_receive_queue .lock );
2357
2402
unix_detach_fds (& scm , skb );
2403
+ }
2358
2404
2359
2405
if (unix_skb_len (skb ))
2360
2406
break ;
0 commit comments