Skip to content

Commit eaefd11

Browse files
Eric Dumazetdavem330
authored andcommitted
net: add __rcu annotations to sk_wq and wq
Add proper RCU annotations/verbs to sk_wq and wq members Fix __sctp_write_space() sk_sleep() abuse (and sock->wq access) Fix sunrpc sk_sleep() abuse too Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 04cfa85 commit eaefd11

6 files changed

Lines changed: 46 additions & 30 deletions

File tree

‎include/linux/net.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ enum sock_shutdown_cmd {
118118
};
119119

120120
struct socket_wq {
121+
/* Note: wait MUST be first field of socket_wq */
121122
wait_queue_head_t wait;
122123
struct fasync_struct *fasync_list;
123124
struct rcu_head rcu;
@@ -142,7 +143,7 @@ struct socket {
142143

143144
unsigned long flags;
144145

145-
struct socket_wq *wq;
146+
struct socket_wq __rcu *wq;
146147

147148
struct file *file;
148149
struct sock *sk;

‎include/net/sock.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ struct sock {
281281
int sk_rcvbuf;
282282

283283
struct sk_filter __rcu *sk_filter;
284-
struct socket_wq *sk_wq;
284+
struct socket_wq __rcu *sk_wq;
285285

286286
#ifdef CONFIG_NET_DMA
287287
struct sk_buff_head sk_async_wait_queue;
@@ -1266,7 +1266,8 @@ static inline void sk_set_socket(struct sock *sk, struct socket *sock)
12661266

12671267
static inline wait_queue_head_t *sk_sleep(struct sock *sk)
12681268
{
1269-
return &sk->sk_wq->wait;
1269+
BUILD_BUG_ON(offsetof(struct socket_wq, wait) != 0);
1270+
return &rcu_dereference_raw(sk->sk_wq)->wait;
12701271
}
12711272
/* Detach socket from process context.
12721273
* Announce socket dead, detach it from wait queue and inode.
@@ -1287,7 +1288,7 @@ static inline void sock_orphan(struct sock *sk)
12871288
static inline void sock_graft(struct sock *sk, struct socket *parent)
12881289
{
12891290
write_lock_bh(&sk->sk_callback_lock);
1290-
rcu_assign_pointer(sk->sk_wq, parent->wq);
1291+
sk->sk_wq = parent->wq;
12911292
parent->sk = sk;
12921293
sk_set_socket(sk, parent);
12931294
security_sock_graft(sk, parent);

‎net/sctp/socket.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6102,15 +6102,16 @@ static void __sctp_write_space(struct sctp_association *asoc)
61026102
wake_up_interruptible(&asoc->wait);
61036103

61046104
if (sctp_writeable(sk)) {
6105-
if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk)))
6106-
wake_up_interruptible(sk_sleep(sk));
6105+
wait_queue_head_t *wq = sk_sleep(sk);
6106+
6107+
if (wq && waitqueue_active(wq))
6108+
wake_up_interruptible(wq);
61076109

61086110
/* Note that we try to include the Async I/O support
61096111
* here by modeling from the current TCP/UDP code.
61106112
* We have not tested with it yet.
61116113
*/
6112-
if (sock->wq->fasync_list &&
6113-
!(sk->sk_shutdown & SEND_SHUTDOWN))
6114+
if (!(sk->sk_shutdown & SEND_SHUTDOWN))
61146115
sock_wake_async(sock,
61156116
SOCK_WAKE_SPACE, POLL_OUT);
61166117
}

‎net/socket.c‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,19 @@ static struct kmem_cache *sock_inode_cachep __read_mostly;
240240
static struct inode *sock_alloc_inode(struct super_block *sb)
241241
{
242242
struct socket_alloc *ei;
243+
struct socket_wq *wq;
243244

244245
ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
245246
if (!ei)
246247
return NULL;
247-
ei->socket.wq = kmalloc(sizeof(struct socket_wq), GFP_KERNEL);
248-
if (!ei->socket.wq) {
248+
wq = kmalloc(sizeof(*wq), GFP_KERNEL);
249+
if (!wq) {
249250
kmem_cache_free(sock_inode_cachep, ei);
250251
return NULL;
251252
}
252-
init_waitqueue_head(&ei->socket.wq->wait);
253-
ei->socket.wq->fasync_list = NULL;
253+
init_waitqueue_head(&wq->wait);
254+
wq->fasync_list = NULL;
255+
RCU_INIT_POINTER(ei->socket.wq, wq);
254256

255257
ei->socket.state = SS_UNCONNECTED;
256258
ei->socket.flags = 0;
@@ -273,9 +275,11 @@ static void wq_free_rcu(struct rcu_head *head)
273275
static void sock_destroy_inode(struct inode *inode)
274276
{
275277
struct socket_alloc *ei;
278+
struct socket_wq *wq;
276279

277280
ei = container_of(inode, struct socket_alloc, vfs_inode);
278-
call_rcu(&ei->socket.wq->rcu, wq_free_rcu);
281+
wq = rcu_dereference_protected(ei->socket.wq, 1);
282+
call_rcu(&wq->rcu, wq_free_rcu);
279283
kmem_cache_free(sock_inode_cachep, ei);
280284
}
281285

@@ -524,7 +528,7 @@ void sock_release(struct socket *sock)
524528
module_put(owner);
525529
}
526530

527-
if (sock->wq->fasync_list)
531+
if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
528532
printk(KERN_ERR "sock_release: fasync list not empty!\n");
529533

530534
percpu_sub(sockets_in_use, 1);
@@ -1108,15 +1112,16 @@ static int sock_fasync(int fd, struct file *filp, int on)
11081112
{
11091113
struct socket *sock = filp->private_data;
11101114
struct sock *sk = sock->sk;
1115+
struct socket_wq *wq;
11111116

11121117
if (sk == NULL)
11131118
return -EINVAL;
11141119

11151120
lock_sock(sk);
1121+
wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
1122+
fasync_helper(fd, filp, on, &wq->fasync_list);
11161123

1117-
fasync_helper(fd, filp, on, &sock->wq->fasync_list);
1118-
1119-
if (!sock->wq->fasync_list)
1124+
if (!wq->fasync_list)
11201125
sock_reset_flag(sk, SOCK_FASYNC);
11211126
else
11221127
sock_set_flag(sk, SOCK_FASYNC);

‎net/sunrpc/svcsock.c‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ static void svc_sock_setbufsize(struct socket *sock, unsigned int snd,
420420
static void svc_udp_data_ready(struct sock *sk, int count)
421421
{
422422
struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
423+
wait_queue_head_t *wq = sk_sleep(sk);
423424

424425
if (svsk) {
425426
dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
@@ -428,8 +429,8 @@ static void svc_udp_data_ready(struct sock *sk, int count)
428429
set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
429430
svc_xprt_enqueue(&svsk->sk_xprt);
430431
}
431-
if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk)))
432-
wake_up_interruptible(sk_sleep(sk));
432+
if (wq && waitqueue_active(wq))
433+
wake_up_interruptible(wq);
433434
}
434435

435436
/*
@@ -438,17 +439,18 @@ static void svc_udp_data_ready(struct sock *sk, int count)
438439
static void svc_write_space(struct sock *sk)
439440
{
440441
struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
442+
wait_queue_head_t *wq = sk_sleep(sk);
441443

442444
if (svsk) {
443445
dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
444446
svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
445447
svc_xprt_enqueue(&svsk->sk_xprt);
446448
}
447449

448-
if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk))) {
450+
if (wq && waitqueue_active(wq)) {
449451
dprintk("RPC svc_write_space: someone sleeping on %p\n",
450452
svsk);
451-
wake_up_interruptible(sk_sleep(sk));
453+
wake_up_interruptible(wq);
452454
}
453455
}
454456

@@ -739,6 +741,7 @@ static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
739741
static void svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
740742
{
741743
struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
744+
wait_queue_head_t *wq;
742745

743746
dprintk("svc: socket %p TCP (listen) state change %d\n",
744747
sk, sk->sk_state);
@@ -761,8 +764,9 @@ static void svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
761764
printk("svc: socket %p: no user data\n", sk);
762765
}
763766

764-
if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk)))
765-
wake_up_interruptible_all(sk_sleep(sk));
767+
wq = sk_sleep(sk);
768+
if (wq && waitqueue_active(wq))
769+
wake_up_interruptible_all(wq);
766770
}
767771

768772
/*
@@ -771,6 +775,7 @@ static void svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
771775
static void svc_tcp_state_change(struct sock *sk)
772776
{
773777
struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
778+
wait_queue_head_t *wq = sk_sleep(sk);
774779

775780
dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
776781
sk, sk->sk_state, sk->sk_user_data);
@@ -781,22 +786,23 @@ static void svc_tcp_state_change(struct sock *sk)
781786
set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
782787
svc_xprt_enqueue(&svsk->sk_xprt);
783788
}
784-
if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk)))
785-
wake_up_interruptible_all(sk_sleep(sk));
789+
if (wq && waitqueue_active(wq))
790+
wake_up_interruptible_all(wq);
786791
}
787792

788793
static void svc_tcp_data_ready(struct sock *sk, int count)
789794
{
790795
struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
796+
wait_queue_head_t *wq = sk_sleep(sk);
791797

792798
dprintk("svc: socket %p TCP data ready (svsk %p)\n",
793799
sk, sk->sk_user_data);
794800
if (svsk) {
795801
set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
796802
svc_xprt_enqueue(&svsk->sk_xprt);
797803
}
798-
if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk)))
799-
wake_up_interruptible(sk_sleep(sk));
804+
if (wq && waitqueue_active(wq))
805+
wake_up_interruptible(wq);
800806
}
801807

802808
/*
@@ -1531,6 +1537,7 @@ static void svc_sock_detach(struct svc_xprt *xprt)
15311537
{
15321538
struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
15331539
struct sock *sk = svsk->sk_sk;
1540+
wait_queue_head_t *wq;
15341541

15351542
dprintk("svc: svc_sock_detach(%p)\n", svsk);
15361543

@@ -1539,8 +1546,9 @@ static void svc_sock_detach(struct svc_xprt *xprt)
15391546
sk->sk_data_ready = svsk->sk_odata;
15401547
sk->sk_write_space = svsk->sk_owspace;
15411548

1542-
if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk)))
1543-
wake_up_interruptible(sk_sleep(sk));
1549+
wq = sk_sleep(sk);
1550+
if (wq && waitqueue_active(wq))
1551+
wake_up_interruptible(wq);
15441552
}
15451553

15461554
/*

‎net/unix/af_unix.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
11711171
newsk->sk_type = sk->sk_type;
11721172
init_peercred(newsk);
11731173
newu = unix_sk(newsk);
1174-
newsk->sk_wq = &newu->peer_wq;
1174+
RCU_INIT_POINTER(newsk->sk_wq, &newu->peer_wq);
11751175
otheru = unix_sk(other);
11761176

11771177
/* copy address information from listening to new sock*/

0 commit comments

Comments
 (0)