Skip to content

Commit 4ee806d

Browse files
ddstreetdavem330
authored andcommitted
net: tcp: close sock if net namespace is exiting
When a tcp socket is closed, if it detects that its net namespace is exiting, close immediately and do not wait for FIN sequence. For normal sockets, a reference is taken to their net namespace, so it will never exit while the socket is open. However, kernel sockets do not take a reference to their net namespace, so it may begin exiting while the kernel socket is still open. In this case if the kernel socket is a tcp socket, it will stay open trying to complete its close sequence. The sock's dst(s) hold a reference to their interface, which are all transferred to the namespace's loopback interface when the real interfaces are taken down. When the namespace tries to take down its loopback interface, it hangs waiting for all references to the loopback interface to release, which results in messages like: unregister_netdevice: waiting for lo to become free. Usage count = 1 These messages continue until the socket finally times out and closes. Since the net namespace cleanup holds the net_mutex while calling its registered pernet callbacks, any new net namespace initialization is blocked until the current net namespace finishes exiting. After this change, the tcp socket notices the exiting net namespace, and closes immediately, releasing its dst(s) and their reference to the loopback interface, which lets the net namespace continue exiting. Link: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1711407 Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=97811 Signed-off-by: Dan Streetman <ddstreet@canonical.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 5b7d279 commit 4ee806d

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

include/net/net_namespace.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ int net_eq(const struct net *net1, const struct net *net2)
223223
return net1 == net2;
224224
}
225225

226+
static inline int check_net(const struct net *net)
227+
{
228+
return atomic_read(&net->count) != 0;
229+
}
230+
226231
void net_drop_ns(void *);
227232

228233
#else
@@ -247,6 +252,11 @@ int net_eq(const struct net *net1, const struct net *net2)
247252
return 1;
248253
}
249254

255+
static inline int check_net(const struct net *net)
256+
{
257+
return 1;
258+
}
259+
250260
#define net_drop_ns NULL
251261
#endif
252262

net/ipv4/tcp.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,6 +2298,9 @@ void tcp_close(struct sock *sk, long timeout)
22982298
tcp_send_active_reset(sk, GFP_ATOMIC);
22992299
__NET_INC_STATS(sock_net(sk),
23002300
LINUX_MIB_TCPABORTONMEMORY);
2301+
} else if (!check_net(sock_net(sk))) {
2302+
/* Not possible to send reset; just close */
2303+
tcp_set_state(sk, TCP_CLOSE);
23012304
}
23022305
}
23032306

net/ipv4/tcp_timer.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,19 @@ static void tcp_write_err(struct sock *sk)
4848
* to prevent DoS attacks. It is called when a retransmission timeout
4949
* or zero probe timeout occurs on orphaned socket.
5050
*
51+
* Also close if our net namespace is exiting; in that case there is no
52+
* hope of ever communicating again since all netns interfaces are already
53+
* down (or about to be down), and we need to release our dst references,
54+
* which have been moved to the netns loopback interface, so the namespace
55+
* can finish exiting. This condition is only possible if we are a kernel
56+
* socket, as those do not hold references to the namespace.
57+
*
5158
* Criteria is still not confirmed experimentally and may change.
5259
* We kill the socket, if:
5360
* 1. If number of orphaned sockets exceeds an administratively configured
5461
* limit.
5562
* 2. If we have strong memory pressure.
63+
* 3. If our net namespace is exiting.
5664
*/
5765
static int tcp_out_of_resources(struct sock *sk, bool do_reset)
5866
{
@@ -81,6 +89,13 @@ static int tcp_out_of_resources(struct sock *sk, bool do_reset)
8189
__NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY);
8290
return 1;
8391
}
92+
93+
if (!check_net(sock_net(sk))) {
94+
/* Not possible to send reset; just close */
95+
tcp_done(sk);
96+
return 1;
97+
}
98+
8499
return 0;
85100
}
86101

0 commit comments

Comments
 (0)