0024-net-tcp-close-sock-if-net-namespace-is-exiting.patch 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Dan Streetman <[email protected]>
  3. Date: Thu, 18 Jan 2018 16:14:26 -0500
  4. Subject: [PATCH] net: tcp: close sock if net namespace is exiting
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. When a tcp socket is closed, if it detects that its net namespace is
  9. exiting, close immediately and do not wait for FIN sequence.
  10. For normal sockets, a reference is taken to their net namespace, so it will
  11. never exit while the socket is open. However, kernel sockets do not take a
  12. reference to their net namespace, so it may begin exiting while the kernel
  13. socket is still open. In this case if the kernel socket is a tcp socket,
  14. it will stay open trying to complete its close sequence. The sock's dst(s)
  15. hold a reference to their interface, which are all transferred to the
  16. namespace's loopback interface when the real interfaces are taken down.
  17. When the namespace tries to take down its loopback interface, it hangs
  18. waiting for all references to the loopback interface to release, which
  19. results in messages like:
  20. unregister_netdevice: waiting for lo to become free. Usage count = 1
  21. These messages continue until the socket finally times out and closes.
  22. Since the net namespace cleanup holds the net_mutex while calling its
  23. registered pernet callbacks, any new net namespace initialization is
  24. blocked until the current net namespace finishes exiting.
  25. After this change, the tcp socket notices the exiting net namespace, and
  26. closes immediately, releasing its dst(s) and their reference to the
  27. loopback interface, which lets the net namespace continue exiting.
  28. Link: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1711407
  29. Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=97811
  30. Signed-off-by: Dan Streetman <[email protected]>
  31. Signed-off-by: David S. Miller <[email protected]>
  32. Signed-off-by: Fabian Grünbichler <[email protected]>
  33. ---
  34. include/net/net_namespace.h | 10 ++++++++++
  35. net/ipv4/tcp.c | 3 +++
  36. net/ipv4/tcp_timer.c | 15 +++++++++++++++
  37. 3 files changed, 28 insertions(+)
  38. diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h
  39. index 1c401bd4c2e0..a5d023fa78db 100644
  40. --- a/include/net/net_namespace.h
  41. +++ b/include/net/net_namespace.h
  42. @@ -221,6 +221,11 @@ int net_eq(const struct net *net1, const struct net *net2)
  43. return net1 == net2;
  44. }
  45. +static inline int check_net(const struct net *net)
  46. +{
  47. + return atomic_read(&net->count) != 0;
  48. +}
  49. +
  50. void net_drop_ns(void *);
  51. #else
  52. @@ -245,6 +250,11 @@ int net_eq(const struct net *net1, const struct net *net2)
  53. return 1;
  54. }
  55. +static inline int check_net(const struct net *net)
  56. +{
  57. + return 1;
  58. +}
  59. +
  60. #define net_drop_ns NULL
  61. #endif
  62. diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
  63. index a3e91b552edc..fd2a086da910 100644
  64. --- a/net/ipv4/tcp.c
  65. +++ b/net/ipv4/tcp.c
  66. @@ -2258,6 +2258,9 @@ void tcp_close(struct sock *sk, long timeout)
  67. tcp_send_active_reset(sk, GFP_ATOMIC);
  68. __NET_INC_STATS(sock_net(sk),
  69. LINUX_MIB_TCPABORTONMEMORY);
  70. + } else if (!check_net(sock_net(sk))) {
  71. + /* Not possible to send reset; just close */
  72. + tcp_set_state(sk, TCP_CLOSE);
  73. }
  74. }
  75. diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c
  76. index e906014890b6..ec1e5de41653 100644
  77. --- a/net/ipv4/tcp_timer.c
  78. +++ b/net/ipv4/tcp_timer.c
  79. @@ -50,11 +50,19 @@ static void tcp_write_err(struct sock *sk)
  80. * to prevent DoS attacks. It is called when a retransmission timeout
  81. * or zero probe timeout occurs on orphaned socket.
  82. *
  83. + * Also close if our net namespace is exiting; in that case there is no
  84. + * hope of ever communicating again since all netns interfaces are already
  85. + * down (or about to be down), and we need to release our dst references,
  86. + * which have been moved to the netns loopback interface, so the namespace
  87. + * can finish exiting. This condition is only possible if we are a kernel
  88. + * socket, as those do not hold references to the namespace.
  89. + *
  90. * Criteria is still not confirmed experimentally and may change.
  91. * We kill the socket, if:
  92. * 1. If number of orphaned sockets exceeds an administratively configured
  93. * limit.
  94. * 2. If we have strong memory pressure.
  95. + * 3. If our net namespace is exiting.
  96. */
  97. static int tcp_out_of_resources(struct sock *sk, bool do_reset)
  98. {
  99. @@ -83,6 +91,13 @@ static int tcp_out_of_resources(struct sock *sk, bool do_reset)
  100. __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY);
  101. return 1;
  102. }
  103. +
  104. + if (!check_net(sock_net(sk))) {
  105. + /* Not possible to send reset; just close */
  106. + tcp_done(sk);
  107. + return 1;
  108. + }
  109. +
  110. return 0;
  111. }
  112. --
  113. 2.14.2