Skip to content

Commit 6997fbd

Browse files
Tetsuo Handakuba-moo
Tetsuo Handa
authored andcommitted
net: rds: use maybe_get_net() when acquiring refcount on TCP sockets
Eric Dumazet is reporting addition on 0 problem at rds_tcp_tune(), for delayed works queued in rds_wq might be invoked after a net namespace's refcount already reached 0. Since rds_tcp_exit_net() from cleanup_net() calls flush_workqueue(rds_wq), it is guaranteed that we can instead use maybe_get_net() from delayed work functions until rds_tcp_exit_net() returns. Note that I'm not convinced that all works which might access a net namespace are already queued in rds_wq by the moment rds_tcp_exit_net() calls flush_workqueue(rds_wq). If some race is there, rds_tcp_exit_net() will fail to wait for work functions, and kmem_cache_free() could be called from net_free() before maybe_get_net() is called from rds_tcp_tune(). Reported-by: Eric Dumazet <[email protected]> Fixes: 3a58f13 ("net: rds: acquire refcount on TCP sockets") Signed-off-by: Tetsuo Handa <[email protected]> Reviewed-by: Eric Dumazet <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 68533eb commit 6997fbd

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

net/rds/tcp.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,22 +487,27 @@ struct rds_tcp_net {
487487
/* All module specific customizations to the RDS-TCP socket should be done in
488488
* rds_tcp_tune() and applied after socket creation.
489489
*/
490-
void rds_tcp_tune(struct socket *sock)
490+
bool rds_tcp_tune(struct socket *sock)
491491
{
492492
struct sock *sk = sock->sk;
493493
struct net *net = sock_net(sk);
494-
struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
494+
struct rds_tcp_net *rtn;
495495

496496
tcp_sock_set_nodelay(sock->sk);
497497
lock_sock(sk);
498498
/* TCP timer functions might access net namespace even after
499499
* a process which created this net namespace terminated.
500500
*/
501501
if (!sk->sk_net_refcnt) {
502+
if (!maybe_get_net(net)) {
503+
release_sock(sk);
504+
return false;
505+
}
502506
sk->sk_net_refcnt = 1;
503-
get_net_track(net, &sk->ns_tracker, GFP_KERNEL);
507+
netns_tracker_alloc(net, &sk->ns_tracker, GFP_KERNEL);
504508
sock_inuse_add(net, 1);
505509
}
510+
rtn = net_generic(net, rds_tcp_netid);
506511
if (rtn->sndbuf_size > 0) {
507512
sk->sk_sndbuf = rtn->sndbuf_size;
508513
sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
@@ -512,6 +517,7 @@ void rds_tcp_tune(struct socket *sock)
512517
sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
513518
}
514519
release_sock(sk);
520+
return true;
515521
}
516522

517523
static void rds_tcp_accept_worker(struct work_struct *work)

net/rds/tcp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct rds_tcp_statistics {
4949
};
5050

5151
/* tcp.c */
52-
void rds_tcp_tune(struct socket *sock);
52+
bool rds_tcp_tune(struct socket *sock);
5353
void rds_tcp_set_callbacks(struct socket *sock, struct rds_conn_path *cp);
5454
void rds_tcp_reset_callbacks(struct socket *sock, struct rds_conn_path *cp);
5555
void rds_tcp_restore_callbacks(struct socket *sock,

net/rds/tcp_connect.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ int rds_tcp_conn_path_connect(struct rds_conn_path *cp)
124124
if (ret < 0)
125125
goto out;
126126

127-
rds_tcp_tune(sock);
127+
if (!rds_tcp_tune(sock)) {
128+
ret = -EINVAL;
129+
goto out;
130+
}
128131

129132
if (isv6) {
130133
sin6.sin6_family = AF_INET6;

net/rds/tcp_listen.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ int rds_tcp_accept_one(struct socket *sock)
133133
__module_get(new_sock->ops->owner);
134134

135135
rds_tcp_keepalive(new_sock);
136-
rds_tcp_tune(new_sock);
136+
if (!rds_tcp_tune(new_sock)) {
137+
ret = -EINVAL;
138+
goto out;
139+
}
137140

138141
inet = inet_sk(new_sock->sk);
139142

0 commit comments

Comments
 (0)