Skip to content

Commit a57de0b

Browse files
Jiri Olsadavem330
authored andcommitted
net: adding memory barrier to the poll and receive callbacks
Adding memory barrier after the poll_wait function, paired with receive callbacks. Adding fuctions sock_poll_wait and sk_has_sleeper to wrap the memory barrier. Without the memory barrier, following race can happen. The race fires, when following code paths meet, and the tp->rcv_nxt and __add_wait_queue updates stay in CPU caches. CPU1 CPU2 sys_select receive packet ... ... __add_wait_queue update tp->rcv_nxt ... ... tp->rcv_nxt check sock_def_readable ... { schedule ... if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) wake_up_interruptible(sk->sk_sleep) ... } If there was no cache the code would work ok, since the wait_queue and rcv_nxt are opposit to each other. Meaning that once tp->rcv_nxt is updated by CPU2, the CPU1 either already passed the tp->rcv_nxt check and sleeps, or will get the new value for tp->rcv_nxt and will return with new data mask. In both cases the process (CPU1) is being added to the wait queue, so the waitqueue_active (CPU2) call cannot miss and will wake up CPU1. The bad case is when the __add_wait_queue changes done by CPU1 stay in its cache, and so does the tp->rcv_nxt update on CPU2 side. The CPU1 will then endup calling schedule and sleep forever if there are no more data on the socket. Calls to poll_wait in following modules were ommited: net/bluetooth/af_bluetooth.c net/irda/af_irda.c net/irda/irnet/irnet_ppp.c net/mac80211/rc80211_pid_debugfs.c net/phonet/socket.c net/rds/af_rds.c net/rfkill/core.c net/sunrpc/cache.c net/sunrpc/rpc_pipe.c net/tipc/socket.c Signed-off-by: Jiri Olsa <jolsa@redhat.com> Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 1b614fb commit a57de0b

10 files changed

Lines changed: 85 additions & 19 deletions

File tree

‎include/net/sock.h‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
#include <linux/filter.h>
5656
#include <linux/rculist_nulls.h>
57+
#include <linux/poll.h>
5758

5859
#include <asm/atomic.h>
5960
#include <net/dst.h>
@@ -1241,6 +1242,71 @@ static inline int sk_has_allocations(const struct sock *sk)
12411242
return sk_wmem_alloc_get(sk) || sk_rmem_alloc_get(sk);
12421243
}
12431244

1245+
/**
1246+
* sk_has_sleeper - check if there are any waiting processes
1247+
* @sk: socket
1248+
*
1249+
* Returns true if socket has waiting processes
1250+
*
1251+
* The purpose of the sk_has_sleeper and sock_poll_wait is to wrap the memory
1252+
* barrier call. They were added due to the race found within the tcp code.
1253+
*
1254+
* Consider following tcp code paths:
1255+
*
1256+
* CPU1 CPU2
1257+
*
1258+
* sys_select receive packet
1259+
* ... ...
1260+
* __add_wait_queue update tp->rcv_nxt
1261+
* ... ...
1262+
* tp->rcv_nxt check sock_def_readable
1263+
* ... {
1264+
* schedule ...
1265+
* if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1266+
* wake_up_interruptible(sk->sk_sleep)
1267+
* ...
1268+
* }
1269+
*
1270+
* The race for tcp fires when the __add_wait_queue changes done by CPU1 stay
1271+
* in its cache, and so does the tp->rcv_nxt update on CPU2 side. The CPU1
1272+
* could then endup calling schedule and sleep forever if there are no more
1273+
* data on the socket.
1274+
*/
1275+
static inline int sk_has_sleeper(struct sock *sk)
1276+
{
1277+
/*
1278+
* We need to be sure we are in sync with the
1279+
* add_wait_queue modifications to the wait queue.
1280+
*
1281+
* This memory barrier is paired in the sock_poll_wait.
1282+
*/
1283+
smp_mb();
1284+
return sk->sk_sleep && waitqueue_active(sk->sk_sleep);
1285+
}
1286+
1287+
/**
1288+
* sock_poll_wait - place memory barrier behind the poll_wait call.
1289+
* @filp: file
1290+
* @wait_address: socket wait queue
1291+
* @p: poll_table
1292+
*
1293+
* See the comments in the sk_has_sleeper function.
1294+
*/
1295+
static inline void sock_poll_wait(struct file *filp,
1296+
wait_queue_head_t *wait_address, poll_table *p)
1297+
{
1298+
if (p && wait_address) {
1299+
poll_wait(filp, wait_address, p);
1300+
/*
1301+
* We need to be sure we are in sync with the
1302+
* socket flags modification.
1303+
*
1304+
* This memory barrier is paired in the sk_has_sleeper.
1305+
*/
1306+
smp_mb();
1307+
}
1308+
}
1309+
12441310
/*
12451311
* Queue a received datagram if it will fit. Stream and sequenced
12461312
* protocols can't normally use this as they need to fit buffers in

‎net/atm/common.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static void vcc_sock_destruct(struct sock *sk)
9292
static void vcc_def_wakeup(struct sock *sk)
9393
{
9494
read_lock(&sk->sk_callback_lock);
95-
if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
95+
if (sk_has_sleeper(sk))
9696
wake_up(sk->sk_sleep);
9797
read_unlock(&sk->sk_callback_lock);
9898
}
@@ -110,7 +110,7 @@ static void vcc_write_space(struct sock *sk)
110110
read_lock(&sk->sk_callback_lock);
111111

112112
if (vcc_writable(sk)) {
113-
if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
113+
if (sk_has_sleeper(sk))
114114
wake_up_interruptible(sk->sk_sleep);
115115

116116
sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
@@ -594,7 +594,7 @@ unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
594594
struct atm_vcc *vcc;
595595
unsigned int mask;
596596

597-
poll_wait(file, sk->sk_sleep, wait);
597+
sock_poll_wait(file, sk->sk_sleep, wait);
598598
mask = 0;
599599

600600
vcc = ATM_SD(sock);

‎net/core/datagram.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ unsigned int datagram_poll(struct file *file, struct socket *sock,
712712
struct sock *sk = sock->sk;
713713
unsigned int mask;
714714

715-
poll_wait(file, sk->sk_sleep, wait);
715+
sock_poll_wait(file, sk->sk_sleep, wait);
716716
mask = 0;
717717

718718
/* exceptional events? */

‎net/core/sock.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,15 +1715,15 @@ EXPORT_SYMBOL(sock_no_sendpage);
17151715
static void sock_def_wakeup(struct sock *sk)
17161716
{
17171717
read_lock(&sk->sk_callback_lock);
1718-
if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1718+
if (sk_has_sleeper(sk))
17191719
wake_up_interruptible_all(sk->sk_sleep);
17201720
read_unlock(&sk->sk_callback_lock);
17211721
}
17221722

17231723
static void sock_def_error_report(struct sock *sk)
17241724
{
17251725
read_lock(&sk->sk_callback_lock);
1726-
if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1726+
if (sk_has_sleeper(sk))
17271727
wake_up_interruptible_poll(sk->sk_sleep, POLLERR);
17281728
sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
17291729
read_unlock(&sk->sk_callback_lock);
@@ -1732,7 +1732,7 @@ static void sock_def_error_report(struct sock *sk)
17321732
static void sock_def_readable(struct sock *sk, int len)
17331733
{
17341734
read_lock(&sk->sk_callback_lock);
1735-
if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1735+
if (sk_has_sleeper(sk))
17361736
wake_up_interruptible_sync_poll(sk->sk_sleep, POLLIN |
17371737
POLLRDNORM | POLLRDBAND);
17381738
sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
@@ -1747,7 +1747,7 @@ static void sock_def_write_space(struct sock *sk)
17471747
* progress. --DaveM
17481748
*/
17491749
if ((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
1750-
if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1750+
if (sk_has_sleeper(sk))
17511751
wake_up_interruptible_sync_poll(sk->sk_sleep, POLLOUT |
17521752
POLLWRNORM | POLLWRBAND);
17531753

‎net/dccp/output.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void dccp_write_space(struct sock *sk)
196196
{
197197
read_lock(&sk->sk_callback_lock);
198198

199-
if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
199+
if (sk_has_sleeper(sk))
200200
wake_up_interruptible(sk->sk_sleep);
201201
/* Should agree with poll, otherwise some programs break */
202202
if (sock_writeable(sk))

‎net/dccp/proto.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ unsigned int dccp_poll(struct file *file, struct socket *sock,
311311
unsigned int mask;
312312
struct sock *sk = sock->sk;
313313

314-
poll_wait(file, sk->sk_sleep, wait);
314+
sock_poll_wait(file, sk->sk_sleep, wait);
315315
if (sk->sk_state == DCCP_LISTEN)
316316
return inet_csk_listen_poll(sk);
317317

‎net/ipv4/tcp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ unsigned int tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
339339
struct sock *sk = sock->sk;
340340
struct tcp_sock *tp = tcp_sk(sk);
341341

342-
poll_wait(file, sk->sk_sleep, wait);
342+
sock_poll_wait(file, sk->sk_sleep, wait);
343343
if (sk->sk_state == TCP_LISTEN)
344344
return inet_csk_listen_poll(sk);
345345

‎net/iucv/af_iucv.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ static inline int iucv_below_msglim(struct sock *sk)
306306
static void iucv_sock_wake_msglim(struct sock *sk)
307307
{
308308
read_lock(&sk->sk_callback_lock);
309-
if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
309+
if (sk_has_sleeper(sk))
310310
wake_up_interruptible_all(sk->sk_sleep);
311311
sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
312312
read_unlock(&sk->sk_callback_lock);
@@ -1256,7 +1256,7 @@ unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
12561256
struct sock *sk = sock->sk;
12571257
unsigned int mask = 0;
12581258

1259-
poll_wait(file, sk->sk_sleep, wait);
1259+
sock_poll_wait(file, sk->sk_sleep, wait);
12601260

12611261
if (sk->sk_state == IUCV_LISTEN)
12621262
return iucv_accept_poll(sk);

‎net/rxrpc/af_rxrpc.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static void rxrpc_write_space(struct sock *sk)
6363
_enter("%p", sk);
6464
read_lock(&sk->sk_callback_lock);
6565
if (rxrpc_writable(sk)) {
66-
if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
66+
if (sk_has_sleeper(sk))
6767
wake_up_interruptible(sk->sk_sleep);
6868
sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
6969
}
@@ -588,7 +588,7 @@ static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
588588
unsigned int mask;
589589
struct sock *sk = sock->sk;
590590

591-
poll_wait(file, sk->sk_sleep, wait);
591+
sock_poll_wait(file, sk->sk_sleep, wait);
592592
mask = 0;
593593

594594
/* the socket is readable if there are any messages waiting on the Rx

‎net/unix/af_unix.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ static void unix_write_space(struct sock *sk)
315315
{
316316
read_lock(&sk->sk_callback_lock);
317317
if (unix_writable(sk)) {
318-
if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
318+
if (sk_has_sleeper(sk))
319319
wake_up_interruptible_sync(sk->sk_sleep);
320320
sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
321321
}
@@ -1985,7 +1985,7 @@ static unsigned int unix_poll(struct file *file, struct socket *sock, poll_table
19851985
struct sock *sk = sock->sk;
19861986
unsigned int mask;
19871987

1988-
poll_wait(file, sk->sk_sleep, wait);
1988+
sock_poll_wait(file, sk->sk_sleep, wait);
19891989
mask = 0;
19901990

19911991
/* exceptional events? */
@@ -2022,7 +2022,7 @@ static unsigned int unix_dgram_poll(struct file *file, struct socket *sock,
20222022
struct sock *sk = sock->sk, *other;
20232023
unsigned int mask, writable;
20242024

2025-
poll_wait(file, sk->sk_sleep, wait);
2025+
sock_poll_wait(file, sk->sk_sleep, wait);
20262026
mask = 0;
20272027

20282028
/* exceptional events? */
@@ -2053,7 +2053,7 @@ static unsigned int unix_dgram_poll(struct file *file, struct socket *sock,
20532053
other = unix_peer_get(sk);
20542054
if (other) {
20552055
if (unix_peer(other) != sk) {
2056-
poll_wait(file, &unix_sk(other)->peer_wait,
2056+
sock_poll_wait(file, &unix_sk(other)->peer_wait,
20572057
wait);
20582058
if (unix_recvq_full(other))
20592059
writable = 0;

0 commit comments

Comments
 (0)