aboutsummaryrefslogtreecommitdiffstats
path: root/net/unix/af_unix.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/unix/af_unix.c')
-rw-r--r--net/unix/af_unix.c127
1 files changed, 80 insertions, 47 deletions
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index fc12ba51c1fc..87c794d8fa2d 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -174,11 +174,11 @@ static struct sock *unix_peer_get(struct sock *s)
174{ 174{
175 struct sock *peer; 175 struct sock *peer;
176 176
177 unix_state_rlock(s); 177 unix_state_lock(s);
178 peer = unix_peer(s); 178 peer = unix_peer(s);
179 if (peer) 179 if (peer)
180 sock_hold(peer); 180 sock_hold(peer);
181 unix_state_runlock(s); 181 unix_state_unlock(s);
182 return peer; 182 return peer;
183} 183}
184 184
@@ -369,7 +369,7 @@ static int unix_release_sock (struct sock *sk, int embrion)
369 unix_remove_socket(sk); 369 unix_remove_socket(sk);
370 370
371 /* Clear state */ 371 /* Clear state */
372 unix_state_wlock(sk); 372 unix_state_lock(sk);
373 sock_orphan(sk); 373 sock_orphan(sk);
374 sk->sk_shutdown = SHUTDOWN_MASK; 374 sk->sk_shutdown = SHUTDOWN_MASK;
375 dentry = u->dentry; 375 dentry = u->dentry;
@@ -378,7 +378,7 @@ static int unix_release_sock (struct sock *sk, int embrion)
378 u->mnt = NULL; 378 u->mnt = NULL;
379 state = sk->sk_state; 379 state = sk->sk_state;
380 sk->sk_state = TCP_CLOSE; 380 sk->sk_state = TCP_CLOSE;
381 unix_state_wunlock(sk); 381 unix_state_unlock(sk);
382 382
383 wake_up_interruptible_all(&u->peer_wait); 383 wake_up_interruptible_all(&u->peer_wait);
384 384
@@ -386,12 +386,12 @@ static int unix_release_sock (struct sock *sk, int embrion)
386 386
387 if (skpair!=NULL) { 387 if (skpair!=NULL) {
388 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) { 388 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
389 unix_state_wlock(skpair); 389 unix_state_lock(skpair);
390 /* No more writes */ 390 /* No more writes */
391 skpair->sk_shutdown = SHUTDOWN_MASK; 391 skpair->sk_shutdown = SHUTDOWN_MASK;
392 if (!skb_queue_empty(&sk->sk_receive_queue) || embrion) 392 if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
393 skpair->sk_err = ECONNRESET; 393 skpair->sk_err = ECONNRESET;
394 unix_state_wunlock(skpair); 394 unix_state_unlock(skpair);
395 skpair->sk_state_change(skpair); 395 skpair->sk_state_change(skpair);
396 read_lock(&skpair->sk_callback_lock); 396 read_lock(&skpair->sk_callback_lock);
397 sk_wake_async(skpair,1,POLL_HUP); 397 sk_wake_async(skpair,1,POLL_HUP);
@@ -448,7 +448,7 @@ static int unix_listen(struct socket *sock, int backlog)
448 err = -EINVAL; 448 err = -EINVAL;
449 if (!u->addr) 449 if (!u->addr)
450 goto out; /* No listens on an unbound socket */ 450 goto out; /* No listens on an unbound socket */
451 unix_state_wlock(sk); 451 unix_state_lock(sk);
452 if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN) 452 if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
453 goto out_unlock; 453 goto out_unlock;
454 if (backlog > sk->sk_max_ack_backlog) 454 if (backlog > sk->sk_max_ack_backlog)
@@ -462,7 +462,7 @@ static int unix_listen(struct socket *sock, int backlog)
462 err = 0; 462 err = 0;
463 463
464out_unlock: 464out_unlock:
465 unix_state_wunlock(sk); 465 unix_state_unlock(sk);
466out: 466out:
467 return err; 467 return err;
468} 468}
@@ -858,6 +858,31 @@ out_mknod_parent:
858 goto out_up; 858 goto out_up;
859} 859}
860 860
861static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)
862{
863 if (unlikely(sk1 == sk2) || !sk2) {
864 unix_state_lock(sk1);
865 return;
866 }
867 if (sk1 < sk2) {
868 unix_state_lock(sk1);
869 unix_state_lock_nested(sk2);
870 } else {
871 unix_state_lock(sk2);
872 unix_state_lock_nested(sk1);
873 }
874}
875
876static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2)
877{
878 if (unlikely(sk1 == sk2) || !sk2) {
879 unix_state_unlock(sk1);
880 return;
881 }
882 unix_state_unlock(sk1);
883 unix_state_unlock(sk2);
884}
885
861static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr, 886static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
862 int alen, int flags) 887 int alen, int flags)
863{ 888{
@@ -877,11 +902,19 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
877 !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0) 902 !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0)
878 goto out; 903 goto out;
879 904
905restart:
880 other=unix_find_other(sunaddr, alen, sock->type, hash, &err); 906 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
881 if (!other) 907 if (!other)
882 goto out; 908 goto out;
883 909
884 unix_state_wlock(sk); 910 unix_state_double_lock(sk, other);
911
912 /* Apparently VFS overslept socket death. Retry. */
913 if (sock_flag(other, SOCK_DEAD)) {
914 unix_state_double_unlock(sk, other);
915 sock_put(other);
916 goto restart;
917 }
885 918
886 err = -EPERM; 919 err = -EPERM;
887 if (!unix_may_send(sk, other)) 920 if (!unix_may_send(sk, other))
@@ -896,7 +929,7 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
896 * 1003.1g breaking connected state with AF_UNSPEC 929 * 1003.1g breaking connected state with AF_UNSPEC
897 */ 930 */
898 other = NULL; 931 other = NULL;
899 unix_state_wlock(sk); 932 unix_state_double_lock(sk, other);
900 } 933 }
901 934
902 /* 935 /*
@@ -905,19 +938,19 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
905 if (unix_peer(sk)) { 938 if (unix_peer(sk)) {
906 struct sock *old_peer = unix_peer(sk); 939 struct sock *old_peer = unix_peer(sk);
907 unix_peer(sk)=other; 940 unix_peer(sk)=other;
908 unix_state_wunlock(sk); 941 unix_state_double_unlock(sk, other);
909 942
910 if (other != old_peer) 943 if (other != old_peer)
911 unix_dgram_disconnected(sk, old_peer); 944 unix_dgram_disconnected(sk, old_peer);
912 sock_put(old_peer); 945 sock_put(old_peer);
913 } else { 946 } else {
914 unix_peer(sk)=other; 947 unix_peer(sk)=other;
915 unix_state_wunlock(sk); 948 unix_state_double_unlock(sk, other);
916 } 949 }
917 return 0; 950 return 0;
918 951
919out_unlock: 952out_unlock:
920 unix_state_wunlock(sk); 953 unix_state_double_unlock(sk, other);
921 sock_put(other); 954 sock_put(other);
922out: 955out:
923 return err; 956 return err;
@@ -936,7 +969,7 @@ static long unix_wait_for_peer(struct sock *other, long timeo)
936 (skb_queue_len(&other->sk_receive_queue) > 969 (skb_queue_len(&other->sk_receive_queue) >
937 other->sk_max_ack_backlog); 970 other->sk_max_ack_backlog);
938 971
939 unix_state_runlock(other); 972 unix_state_unlock(other);
940 973
941 if (sched) 974 if (sched)
942 timeo = schedule_timeout(timeo); 975 timeo = schedule_timeout(timeo);
@@ -994,11 +1027,11 @@ restart:
994 goto out; 1027 goto out;
995 1028
996 /* Latch state of peer */ 1029 /* Latch state of peer */
997 unix_state_rlock(other); 1030 unix_state_lock(other);
998 1031
999 /* Apparently VFS overslept socket death. Retry. */ 1032 /* Apparently VFS overslept socket death. Retry. */
1000 if (sock_flag(other, SOCK_DEAD)) { 1033 if (sock_flag(other, SOCK_DEAD)) {
1001 unix_state_runlock(other); 1034 unix_state_unlock(other);
1002 sock_put(other); 1035 sock_put(other);
1003 goto restart; 1036 goto restart;
1004 } 1037 }
@@ -1048,18 +1081,18 @@ restart:
1048 goto out_unlock; 1081 goto out_unlock;
1049 } 1082 }
1050 1083
1051 unix_state_wlock_nested(sk); 1084 unix_state_lock_nested(sk);
1052 1085
1053 if (sk->sk_state != st) { 1086 if (sk->sk_state != st) {
1054 unix_state_wunlock(sk); 1087 unix_state_unlock(sk);
1055 unix_state_runlock(other); 1088 unix_state_unlock(other);
1056 sock_put(other); 1089 sock_put(other);
1057 goto restart; 1090 goto restart;
1058 } 1091 }
1059 1092
1060 err = security_unix_stream_connect(sock, other->sk_socket, newsk); 1093 err = security_unix_stream_connect(sock, other->sk_socket, newsk);
1061 if (err) { 1094 if (err) {
1062 unix_state_wunlock(sk); 1095 unix_state_unlock(sk);
1063 goto out_unlock; 1096 goto out_unlock;
1064 } 1097 }
1065 1098
@@ -1096,7 +1129,7 @@ restart:
1096 smp_mb__after_atomic_inc(); /* sock_hold() does an atomic_inc() */ 1129 smp_mb__after_atomic_inc(); /* sock_hold() does an atomic_inc() */
1097 unix_peer(sk) = newsk; 1130 unix_peer(sk) = newsk;
1098 1131
1099 unix_state_wunlock(sk); 1132 unix_state_unlock(sk);
1100 1133
1101 /* take ten and and send info to listening sock */ 1134 /* take ten and and send info to listening sock */
1102 spin_lock(&other->sk_receive_queue.lock); 1135 spin_lock(&other->sk_receive_queue.lock);
@@ -1105,14 +1138,14 @@ restart:
1105 * is installed to listening socket. */ 1138 * is installed to listening socket. */
1106 atomic_inc(&newu->inflight); 1139 atomic_inc(&newu->inflight);
1107 spin_unlock(&other->sk_receive_queue.lock); 1140 spin_unlock(&other->sk_receive_queue.lock);
1108 unix_state_runlock(other); 1141 unix_state_unlock(other);
1109 other->sk_data_ready(other, 0); 1142 other->sk_data_ready(other, 0);
1110 sock_put(other); 1143 sock_put(other);
1111 return 0; 1144 return 0;
1112 1145
1113out_unlock: 1146out_unlock:
1114 if (other) 1147 if (other)
1115 unix_state_runlock(other); 1148 unix_state_unlock(other);
1116 1149
1117out: 1150out:
1118 if (skb) 1151 if (skb)
@@ -1178,10 +1211,10 @@ static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
1178 wake_up_interruptible(&unix_sk(sk)->peer_wait); 1211 wake_up_interruptible(&unix_sk(sk)->peer_wait);
1179 1212
1180 /* attach accepted sock to socket */ 1213 /* attach accepted sock to socket */
1181 unix_state_wlock(tsk); 1214 unix_state_lock(tsk);
1182 newsock->state = SS_CONNECTED; 1215 newsock->state = SS_CONNECTED;
1183 sock_graft(tsk, newsock); 1216 sock_graft(tsk, newsock);
1184 unix_state_wunlock(tsk); 1217 unix_state_unlock(tsk);
1185 return 0; 1218 return 0;
1186 1219
1187out: 1220out:
@@ -1208,7 +1241,7 @@ static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_
1208 } 1241 }
1209 1242
1210 u = unix_sk(sk); 1243 u = unix_sk(sk);
1211 unix_state_rlock(sk); 1244 unix_state_lock(sk);
1212 if (!u->addr) { 1245 if (!u->addr) {
1213 sunaddr->sun_family = AF_UNIX; 1246 sunaddr->sun_family = AF_UNIX;
1214 sunaddr->sun_path[0] = 0; 1247 sunaddr->sun_path[0] = 0;
@@ -1219,7 +1252,7 @@ static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_
1219 *uaddr_len = addr->len; 1252 *uaddr_len = addr->len;
1220 memcpy(sunaddr, addr->name, *uaddr_len); 1253 memcpy(sunaddr, addr->name, *uaddr_len);
1221 } 1254 }
1222 unix_state_runlock(sk); 1255 unix_state_unlock(sk);
1223 sock_put(sk); 1256 sock_put(sk);
1224out: 1257out:
1225 return err; 1258 return err;
@@ -1337,7 +1370,7 @@ restart:
1337 goto out_free; 1370 goto out_free;
1338 } 1371 }
1339 1372
1340 unix_state_rlock(other); 1373 unix_state_lock(other);
1341 err = -EPERM; 1374 err = -EPERM;
1342 if (!unix_may_send(sk, other)) 1375 if (!unix_may_send(sk, other))
1343 goto out_unlock; 1376 goto out_unlock;
@@ -1347,20 +1380,20 @@ restart:
1347 * Check with 1003.1g - what should 1380 * Check with 1003.1g - what should
1348 * datagram error 1381 * datagram error
1349 */ 1382 */
1350 unix_state_runlock(other); 1383 unix_state_unlock(other);
1351 sock_put(other); 1384 sock_put(other);
1352 1385
1353 err = 0; 1386 err = 0;
1354 unix_state_wlock(sk); 1387 unix_state_lock(sk);
1355 if (unix_peer(sk) == other) { 1388 if (unix_peer(sk) == other) {
1356 unix_peer(sk)=NULL; 1389 unix_peer(sk)=NULL;
1357 unix_state_wunlock(sk); 1390 unix_state_unlock(sk);
1358 1391
1359 unix_dgram_disconnected(sk, other); 1392 unix_dgram_disconnected(sk, other);
1360 sock_put(other); 1393 sock_put(other);
1361 err = -ECONNREFUSED; 1394 err = -ECONNREFUSED;
1362 } else { 1395 } else {
1363 unix_state_wunlock(sk); 1396 unix_state_unlock(sk);
1364 } 1397 }
1365 1398
1366 other = NULL; 1399 other = NULL;
@@ -1397,14 +1430,14 @@ restart:
1397 } 1430 }
1398 1431
1399 skb_queue_tail(&other->sk_receive_queue, skb); 1432 skb_queue_tail(&other->sk_receive_queue, skb);
1400 unix_state_runlock(other); 1433 unix_state_unlock(other);
1401 other->sk_data_ready(other, len); 1434 other->sk_data_ready(other, len);
1402 sock_put(other); 1435 sock_put(other);
1403 scm_destroy(siocb->scm); 1436 scm_destroy(siocb->scm);
1404 return len; 1437 return len;
1405 1438
1406out_unlock: 1439out_unlock:
1407 unix_state_runlock(other); 1440 unix_state_unlock(other);
1408out_free: 1441out_free:
1409 kfree_skb(skb); 1442 kfree_skb(skb);
1410out: 1443out:
@@ -1494,14 +1527,14 @@ static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
1494 goto out_err; 1527 goto out_err;
1495 } 1528 }
1496 1529
1497 unix_state_rlock(other); 1530 unix_state_lock(other);
1498 1531
1499 if (sock_flag(other, SOCK_DEAD) || 1532 if (sock_flag(other, SOCK_DEAD) ||
1500 (other->sk_shutdown & RCV_SHUTDOWN)) 1533 (other->sk_shutdown & RCV_SHUTDOWN))
1501 goto pipe_err_free; 1534 goto pipe_err_free;
1502 1535
1503 skb_queue_tail(&other->sk_receive_queue, skb); 1536 skb_queue_tail(&other->sk_receive_queue, skb);
1504 unix_state_runlock(other); 1537 unix_state_unlock(other);
1505 other->sk_data_ready(other, size); 1538 other->sk_data_ready(other, size);
1506 sent+=size; 1539 sent+=size;
1507 } 1540 }
@@ -1512,7 +1545,7 @@ static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
1512 return sent; 1545 return sent;
1513 1546
1514pipe_err_free: 1547pipe_err_free:
1515 unix_state_runlock(other); 1548 unix_state_unlock(other);
1516 kfree_skb(skb); 1549 kfree_skb(skb);
1517pipe_err: 1550pipe_err:
1518 if (sent==0 && !(msg->msg_flags&MSG_NOSIGNAL)) 1551 if (sent==0 && !(msg->msg_flags&MSG_NOSIGNAL))
@@ -1641,7 +1674,7 @@ static long unix_stream_data_wait(struct sock * sk, long timeo)
1641{ 1674{
1642 DEFINE_WAIT(wait); 1675 DEFINE_WAIT(wait);
1643 1676
1644 unix_state_rlock(sk); 1677 unix_state_lock(sk);
1645 1678
1646 for (;;) { 1679 for (;;) {
1647 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); 1680 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
@@ -1654,14 +1687,14 @@ static long unix_stream_data_wait(struct sock * sk, long timeo)
1654 break; 1687 break;
1655 1688
1656 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); 1689 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1657 unix_state_runlock(sk); 1690 unix_state_unlock(sk);
1658 timeo = schedule_timeout(timeo); 1691 timeo = schedule_timeout(timeo);
1659 unix_state_rlock(sk); 1692 unix_state_lock(sk);
1660 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); 1693 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1661 } 1694 }
1662 1695
1663 finish_wait(sk->sk_sleep, &wait); 1696 finish_wait(sk->sk_sleep, &wait);
1664 unix_state_runlock(sk); 1697 unix_state_unlock(sk);
1665 return timeo; 1698 return timeo;
1666} 1699}
1667 1700
@@ -1816,12 +1849,12 @@ static int unix_shutdown(struct socket *sock, int mode)
1816 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN); 1849 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1817 1850
1818 if (mode) { 1851 if (mode) {
1819 unix_state_wlock(sk); 1852 unix_state_lock(sk);
1820 sk->sk_shutdown |= mode; 1853 sk->sk_shutdown |= mode;
1821 other=unix_peer(sk); 1854 other=unix_peer(sk);
1822 if (other) 1855 if (other)
1823 sock_hold(other); 1856 sock_hold(other);
1824 unix_state_wunlock(sk); 1857 unix_state_unlock(sk);
1825 sk->sk_state_change(sk); 1858 sk->sk_state_change(sk);
1826 1859
1827 if (other && 1860 if (other &&
@@ -1833,9 +1866,9 @@ static int unix_shutdown(struct socket *sock, int mode)
1833 peer_mode |= SEND_SHUTDOWN; 1866 peer_mode |= SEND_SHUTDOWN;
1834 if (mode&SEND_SHUTDOWN) 1867 if (mode&SEND_SHUTDOWN)
1835 peer_mode |= RCV_SHUTDOWN; 1868 peer_mode |= RCV_SHUTDOWN;
1836 unix_state_wlock(other); 1869 unix_state_lock(other);
1837 other->sk_shutdown |= peer_mode; 1870 other->sk_shutdown |= peer_mode;
1838 unix_state_wunlock(other); 1871 unix_state_unlock(other);
1839 other->sk_state_change(other); 1872 other->sk_state_change(other);
1840 read_lock(&other->sk_callback_lock); 1873 read_lock(&other->sk_callback_lock);
1841 if (peer_mode == SHUTDOWN_MASK) 1874 if (peer_mode == SHUTDOWN_MASK)
@@ -1973,7 +2006,7 @@ static int unix_seq_show(struct seq_file *seq, void *v)
1973 else { 2006 else {
1974 struct sock *s = v; 2007 struct sock *s = v;
1975 struct unix_sock *u = unix_sk(s); 2008 struct unix_sock *u = unix_sk(s);
1976 unix_state_rlock(s); 2009 unix_state_lock(s);
1977 2010
1978 seq_printf(seq, "%p: %08X %08X %08X %04X %02X %5lu", 2011 seq_printf(seq, "%p: %08X %08X %08X %04X %02X %5lu",
1979 s, 2012 s,
@@ -2001,7 +2034,7 @@ static int unix_seq_show(struct seq_file *seq, void *v)
2001 for ( ; i < len; i++) 2034 for ( ; i < len; i++)
2002 seq_putc(seq, u->addr->name->sun_path[i]); 2035 seq_putc(seq, u->addr->name->sun_path[i]);
2003 } 2036 }
2004 unix_state_runlock(s); 2037 unix_state_unlock(s);
2005 seq_putc(seq, '\n'); 2038 seq_putc(seq, '\n');
2006 } 2039 }
2007 2040