Received: by 10.223.176.5 with SMTP id f5csp3449779wra; Mon, 29 Jan 2018 13:21:36 -0800 (PST) X-Google-Smtp-Source: AH8x226bsZlygEZNlsQgqLhvoXrmAaIaRceeGNTqKMQK5jnP8jZxXKJXYErsroeXvoDcR1ltv52B X-Received: by 10.101.97.12 with SMTP id z12mr21292952pgu.92.1517260896843; Mon, 29 Jan 2018 13:21:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517260896; cv=none; d=google.com; s=arc-20160816; b=Yta/oX8dVPxrEaQguj5P5clBM2wJyYGudEn08Q6kDHvrI/GaM2X6VLruvqmcm+HXRU VXlftmCHZkyHp4095hCR/Q/q2s8iH7t7yYul4ZwC4R4+t9D+9LPGUH6wYS70zdkxOoEN i9mygOOG+rKddGYTsvFvzwiMmPnxLSqc3AW1fLDg35I2l2CgyO407dvUKtYXDSMsx7kW KW8bHgnJZcm6hhpg6sUX55k60xx4fJUrFa8oClMbVJMjflD9BKquCR+Q/HT2FtHmcKnL 0PlpYBECG7gaaWdz8JoUvtlBDIQ313AKNhLDUvsYuF1lqkKlXYakLSFZ0GLIwgex1CuQ lNjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=4tyGAietUSl5+SaepT0SBM3tInWN5ySH69BvdvNHBAM=; b=CqlFTpP6MbwCJaaV7WjaWUTZFkanYZokmEj3hbT/UstFZfUaC7YbS9DdhwRYBe202C 9UNWpWNfQzNdRBAFuo+U+aSgsnONFh0Kt6r2Ji9Who2zjRZNXjEkU/4gg88XMK4aRg+d vvur5xGh+GcpZm9X8miXLGCkKPV2otlHPLD9RDWB8QMtm/Qm44n3ZNcucaTus1bpVzZc uBrk3+840qp0JR2NbrX/uycb5sVZMrmCkR79SavkjV4bLmmOadXQ86gW7iAKxfujmpww RBc/il2+6vv7P26Xm95EBXFGQtv76HRP7PBS5hZlKkIpq5a/a40BHFKUgv990jWwYDj4 GF5Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 43-v6si3444127plc.167.2018.01.29.13.21.22; Mon, 29 Jan 2018 13:21:36 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752249AbeA2VVB (ORCPT + 99 others); Mon, 29 Jan 2018 16:21:01 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:41474 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751663AbeA2UEG (ORCPT ); Mon, 29 Jan 2018 15:04:06 -0500 Received: from localhost (LFbn-1-12258-90.w90-92.abo.wanadoo.fr [90.92.71.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 225CD2EB7; Mon, 29 Jan 2018 12:59:39 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dan Streetman , "David S. Miller" Subject: [PATCH 3.18 41/52] net: tcp: close sock if net namespace is exiting Date: Mon, 29 Jan 2018 13:56:59 +0100 Message-Id: <20180129123629.983111406@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180129123628.168904217@linuxfoundation.org> References: <20180129123628.168904217@linuxfoundation.org> User-Agent: quilt/0.65 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dan Streetman [ Upstream commit 4ee806d51176ba7b8ff1efd81f271d7252e03a1d ] 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 Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- include/net/net_namespace.h | 10 ++++++++++ net/ipv4/tcp.c | 3 +++ net/ipv4/tcp_timer.c | 15 +++++++++++++++ 3 files changed, 28 insertions(+) --- a/include/net/net_namespace.h +++ b/include/net/net_namespace.h @@ -200,6 +200,11 @@ int net_eq(const struct net *net1, const return net1 == net2; } +static inline int check_net(const struct net *net) +{ + return atomic_read(&net->count) != 0; +} + void net_drop_ns(void *); #else @@ -223,6 +228,11 @@ int net_eq(const struct net *net1, const { return 1; } + +static inline int check_net(const struct net *net) +{ + return 1; +} #define net_drop_ns NULL #endif --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -2182,6 +2182,9 @@ adjudge_to_death: tcp_send_active_reset(sk, GFP_ATOMIC); NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY); + } else if (!check_net(sock_net(sk))) { + /* Not possible to send reset; just close */ + tcp_set_state(sk, TCP_CLOSE); } } --- a/net/ipv4/tcp_timer.c +++ b/net/ipv4/tcp_timer.c @@ -46,11 +46,19 @@ static void tcp_write_err(struct sock *s * to prevent DoS attacks. It is called when a retransmission timeout * or zero probe timeout occurs on orphaned socket. * + * Also close if our net namespace is exiting; in that case there is no + * hope of ever communicating again since all netns interfaces are already + * down (or about to be down), and we need to release our dst references, + * which have been moved to the netns loopback interface, so the namespace + * can finish exiting. This condition is only possible if we are a kernel + * socket, as those do not hold references to the namespace. + * * Criteria is still not confirmed experimentally and may change. * We kill the socket, if: * 1. If number of orphaned sockets exceeds an administratively configured * limit. * 2. If we have strong memory pressure. + * 3. If our net namespace is exiting. */ static int tcp_out_of_resources(struct sock *sk, bool do_reset) { @@ -79,6 +87,13 @@ static int tcp_out_of_resources(struct s NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY); return 1; } + + if (!check_net(sock_net(sk))) { + /* Not possible to send reset; just close */ + tcp_done(sk); + return 1; + } + return 0; }