From patchwork Mon Jul 29 15:38:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13745239 X-Patchwork-Delegate: kuba@kernel.org Received: from mail.katalix.com (mail.katalix.com [3.9.82.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C648413BC3F for ; Mon, 29 Jul 2024 15:38:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.9.82.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267497; cv=none; b=qteSk9iSLPCbyO+mBQJd5+f1VPcFqU7AAnwKSKWwZP8qtPGzDKtdkFkQZsOOYl5kLCXrQGYXXiqGbmTJE4Ve4rwYydFwrQldXtiR8nAeEAgCPf8hI3wpnEnhoPTG6AzlIeYocQnzccljdMm2Iu6cara/GmeIyn57fsq/aUS9BWY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267497; c=relaxed/simple; bh=jOrD3Ywonye7ccVKUoLIgonb8s5USPSg2WdfwCA3xi4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=i5kOz6cj4LCD6i3RpZeiPr8G8o9vH4x5psyE9fLo2ESe5JRnNthhVwxX5nVZbcbN52yfQPg3fCCEMxelEQx0MDpWpDXZzIaQPQr2sBeLYs1LkFbGPvPPUg+iY3+39J5kY14yceMoH0otOc23HE2XfBRE2Q3GPnH7odUP1T3HGN4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com; spf=pass smtp.mailfrom=katalix.com; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b=woRidNEJ; arc=none smtp.client-ip=3.9.82.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=katalix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b="woRidNEJ" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:6c24:bf58:f1fe:91c1]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 1301A7DAD9; Mon, 29 Jul 2024 16:38:15 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1722267495; bh=jOrD3Ywonye7ccVKUoLIgonb8s5USPSg2WdfwCA3xi4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:From; z=From:=20James=20Chapman=20|To:=20netdev@vge r.kernel.org|Cc:=20davem@davemloft.net,=0D=0A=09edumazet@google.co m,=0D=0A=09kuba@kernel.org,=0D=0A=09pabeni@redhat.com,=0D=0A=09dsa hern@kernel.org,=0D=0A=09tparkin@katalix.com|Subject:=20[PATCH=20n et-next=2001/15]=20l2tp:=20lookup=20tunnel=20from=20socket=20witho ut=20using=20sk_user_data|Date:=20Mon,=2029=20Jul=202024=2016:38:0 0=20+0100|Message-Id:=20<892b98b875da7c42cebbc952305ad913468d581e. 1722265212.git.jchapman@katalix.com>|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=woRidNEJwHXix958WCsWXIzCVf5zGEIuuoQmZbmxYvJp80nEF4UsZ+jQ96UTWyxC9 ZTZUazYaYK9L15QWcyX3D62IsuCvx3yB1u6WuA+KKubtfIhrJdbi+lnVQmtvGEwZv+ QpZQ4Ou3GWS5ZMLb/EONE6ptohsM3JfalqEIzQIAg7AwYo0Ibmy5dllgf4qAkdECnm uUAG+inEErbt/SJmKHneQoOwRCKp//XRirYm0g27jvsNFUfwA9Igq/qyHdoM9jMyr9 vBc32eN+6CpH9zYvuGAIxKQVMxu8TJhhdpl/N10bEkCdMkrKXzjzggy182wlHcx4RH iDfr596IaqtfQ== From: James Chapman To: netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, tparkin@katalix.com Subject: [PATCH net-next 01/15] l2tp: lookup tunnel from socket without using sk_user_data Date: Mon, 29 Jul 2024 16:38:00 +0100 Message-Id: <892b98b875da7c42cebbc952305ad913468d581e.1722265212.git.jchapman@katalix.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org l2tp_sk_to_tunnel derives the tunnel from sk_user_data. Instead, lookup the tunnel by walking the tunnel IDR for a tunnel using the indicated sock. This is slow but l2tp_sk_to_tunnel is not used in the datapath so performance isn't critical. l2tp_tunnel_destruct needs a variant of l2tp_sk_to_tunnel which does not bump the tunnel refcount since the tunnel refcount is already 0. Change l2tp_sk_to_tunnel sk arg to const since it does not modify sk. Signed-off-by: James Chapman Signed-off-by: Tom Parkin --- net/l2tp/l2tp_core.c | 52 ++++++++++++++++++++++++++++++++++++-------- net/l2tp/l2tp_core.h | 5 +---- net/l2tp/l2tp_ip.c | 7 ++++-- net/l2tp/l2tp_ip6.c | 7 ++++-- 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c index c80ab3f26084..c97cd0fd8514 100644 --- a/net/l2tp/l2tp_core.c +++ b/net/l2tp/l2tp_core.c @@ -150,15 +150,43 @@ static void l2tp_session_free(struct l2tp_session *session) kfree(session); } -struct l2tp_tunnel *l2tp_sk_to_tunnel(struct sock *sk) +static struct l2tp_tunnel *__l2tp_sk_to_tunnel(const struct sock *sk) { - struct l2tp_tunnel *tunnel = sk->sk_user_data; + const struct net *net = sock_net(sk); + unsigned long tunnel_id, tmp; + struct l2tp_tunnel *tunnel; + struct l2tp_net *pn; + + WARN_ON_ONCE(!rcu_read_lock_bh_held()); + pn = l2tp_pernet(net); + idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) { + if (tunnel && tunnel->sock == sk) + return tunnel; + } + + return NULL; +} - if (tunnel) - if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC)) - return NULL; +struct l2tp_tunnel *l2tp_sk_to_tunnel(const struct sock *sk) +{ + const struct net *net = sock_net(sk); + unsigned long tunnel_id, tmp; + struct l2tp_tunnel *tunnel; + struct l2tp_net *pn; + + rcu_read_lock_bh(); + pn = l2tp_pernet(net); + idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) { + if (tunnel && + tunnel->sock == sk && + refcount_inc_not_zero(&tunnel->ref_count)) { + rcu_read_unlock_bh(); + return tunnel; + } + } + rcu_read_unlock_bh(); - return tunnel; + return NULL; } EXPORT_SYMBOL_GPL(l2tp_sk_to_tunnel); @@ -1213,8 +1241,10 @@ EXPORT_SYMBOL_GPL(l2tp_xmit_skb); */ static void l2tp_tunnel_destruct(struct sock *sk) { - struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk); + struct l2tp_tunnel *tunnel; + rcu_read_lock_bh(); + tunnel = __l2tp_sk_to_tunnel(sk); if (!tunnel) goto end; @@ -1242,6 +1272,7 @@ static void l2tp_tunnel_destruct(struct sock *sk) kfree_rcu(tunnel, rcu); end: + rcu_read_unlock_bh(); return; } @@ -1308,10 +1339,13 @@ static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) /* Tunnel socket destroy hook for UDP encapsulation */ static void l2tp_udp_encap_destroy(struct sock *sk) { - struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk); + struct l2tp_tunnel *tunnel; - if (tunnel) + tunnel = l2tp_sk_to_tunnel(sk); + if (tunnel) { l2tp_tunnel_delete(tunnel); + l2tp_tunnel_dec_refcount(tunnel); + } } static void l2tp_tunnel_remove(struct net *net, struct l2tp_tunnel *tunnel) diff --git a/net/l2tp/l2tp_core.h b/net/l2tp/l2tp_core.h index 8ac81bc1bc6f..a41cf6795df0 100644 --- a/net/l2tp/l2tp_core.h +++ b/net/l2tp/l2tp_core.h @@ -273,10 +273,7 @@ void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type); /* IOCTL helper for IP encap modules. */ int l2tp_ioctl(struct sock *sk, int cmd, int *karg); -/* Extract the tunnel structure from a socket's sk_user_data pointer, - * validating the tunnel magic feather. - */ -struct l2tp_tunnel *l2tp_sk_to_tunnel(struct sock *sk); +struct l2tp_tunnel *l2tp_sk_to_tunnel(const struct sock *sk); static inline int l2tp_get_l2specific_len(struct l2tp_session *session) { diff --git a/net/l2tp/l2tp_ip.c b/net/l2tp/l2tp_ip.c index e48aa177d74c..78243f993cda 100644 --- a/net/l2tp/l2tp_ip.c +++ b/net/l2tp/l2tp_ip.c @@ -235,14 +235,17 @@ static void l2tp_ip_close(struct sock *sk, long timeout) static void l2tp_ip_destroy_sock(struct sock *sk) { - struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk); + struct l2tp_tunnel *tunnel; struct sk_buff *skb; while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) kfree_skb(skb); - if (tunnel) + tunnel = l2tp_sk_to_tunnel(sk); + if (tunnel) { l2tp_tunnel_delete(tunnel); + l2tp_tunnel_dec_refcount(tunnel); + } } static int l2tp_ip_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) diff --git a/net/l2tp/l2tp_ip6.c b/net/l2tp/l2tp_ip6.c index d217ff1f229e..3b0465f2d60d 100644 --- a/net/l2tp/l2tp_ip6.c +++ b/net/l2tp/l2tp_ip6.c @@ -246,14 +246,17 @@ static void l2tp_ip6_close(struct sock *sk, long timeout) static void l2tp_ip6_destroy_sock(struct sock *sk) { - struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk); + struct l2tp_tunnel *tunnel; lock_sock(sk); ip6_flush_pending_frames(sk); release_sock(sk); - if (tunnel) + tunnel = l2tp_sk_to_tunnel(sk); + if (tunnel) { l2tp_tunnel_delete(tunnel); + l2tp_tunnel_dec_refcount(tunnel); + } } static int l2tp_ip6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) From patchwork Mon Jul 29 15:38:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13745236 X-Patchwork-Delegate: kuba@kernel.org Received: from mail.katalix.com (mail.katalix.com [3.9.82.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F0319145354 for ; Mon, 29 Jul 2024 15:38:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.9.82.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267497; cv=none; b=H3IzbehvukTve5D0xLCAlmcrV8GlGNdNLUmVuxD3hh5WCNfldCGeL1oXNqSSizuh1VbCwtZm5xYY7LleTSO4Kzr6hZrVqgapPI/+cb0VAF9/zrVwmzvMBAsouJ1gOU7MNea1M/l/vw6dJVjK63RgMfXEVI5GniuBZkLF97OZo5I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267497; c=relaxed/simple; bh=yyBzlPTZBApoUoOjHBBI5TZlyTFp2+vVtb0I5ttQVAI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tkgbVmYgMS+s91N+jI4pWbfONWaDLKi8TSAZfz29J4p0PC1hfAfcn4ftWytVPXcT0DYGlppc28JzDjKIA/63ziBDyi2SKLE2Sr84ewBde5Th9vVnRimkkIUcjykR0CvPWZZ9G+Kt6HNqS3GuLk0C5hzaUG52VqgwFLUQZDs0SsM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com; spf=pass smtp.mailfrom=katalix.com; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b=aztqdyBL; arc=none smtp.client-ip=3.9.82.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=katalix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b="aztqdyBL" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:6c24:bf58:f1fe:91c1]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 30D117DCAE; Mon, 29 Jul 2024 16:38:15 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1722267495; bh=yyBzlPTZBApoUoOjHBBI5TZlyTFp2+vVtb0I5ttQVAI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:From; z=From:=20James=20Chapman=20|To:=20netdev@vge r.kernel.org|Cc:=20davem@davemloft.net,=0D=0A=09edumazet@google.co m,=0D=0A=09kuba@kernel.org,=0D=0A=09pabeni@redhat.com,=0D=0A=09dsa hern@kernel.org,=0D=0A=09tparkin@katalix.com|Subject:=20[PATCH=20n et-next=2002/15]=20ipv4:=20export=20ip_flush_pending_frames|Date:= 20Mon,=2029=20Jul=202024=2016:38:01=20+0100|Message-Id:=20<1ff6ff4 9a7f0f84e23e851eb236f0cbefc5a0dd0.1722265212.git.jchapman@katalix. com>|In-Reply-To:=20|Re ferences:=20|MIME-Versi on:=201.0; b=aztqdyBL6Wihe+nMnwBHWOps4wNNDmX5b1dU81fahY6lff0QTItZgoxkMRKrpviEj zV3XkQGT9b9EckxZPRvLApZXkk5iQFU1r+iiEwM/GemJm5rkY5ENTepiSMtrhXah8h q1lgeYeQQ6odiW9kYEogxc8M6ft+HduDCBcRk4cfIY4VitudcfQSARvy206FP+xxfY Pd8xQiLBMw8wnJfYUX4amYQoAJHUS21rZ6AvMnOaYAyObXBgmCe4Cm1B+Qv3B3Xs/s x3CmgaUEv051DIRpTBS8mHW25orH528QHegScmqixHKCMxR8LqZ0kbh3k6SxK8/neY 8htwWLerzulng== From: James Chapman To: netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, tparkin@katalix.com Subject: [PATCH net-next 02/15] ipv4: export ip_flush_pending_frames Date: Mon, 29 Jul 2024 16:38:01 +0100 Message-Id: <1ff6ff49a7f0f84e23e851eb236f0cbefc5a0dd0.1722265212.git.jchapman@katalix.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org To avoid protocol modules implementing their own, export ip_flush_pending_frames. Signed-off-by: James Chapman Signed-off-by: Tom Parkin --- net/ipv4/ip_output.c | 1 + 1 file changed, 1 insertion(+) diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c index b90d0f78ac80..8a10a7c67834 100644 --- a/net/ipv4/ip_output.c +++ b/net/ipv4/ip_output.c @@ -1534,6 +1534,7 @@ void ip_flush_pending_frames(struct sock *sk) { __ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base); } +EXPORT_SYMBOL_GPL(ip_flush_pending_frames); struct sk_buff *ip_make_skb(struct sock *sk, struct flowi4 *fl4, From patchwork Mon Jul 29 15:38:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13745237 X-Patchwork-Delegate: kuba@kernel.org Received: from mail.katalix.com (mail.katalix.com [3.9.82.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 272A7158A30 for ; Mon, 29 Jul 2024 15:38:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.9.82.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267497; cv=none; b=RnElnnNouI9hDecxcn+ZGgrnu3vO/j4VW4vOhnf5M/UovfRcKZ6tR71dDPllIMSskbgFfdCICEhwiNUWbmqaDBy7Bwvzg/l5/tKi0DPze8Ov+9adXqqdlG+M7fVyvnzQvgy/9Gm8ymQNpptsZ5X6Z8cmJwfph98gGdYW5qX+nhc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267497; c=relaxed/simple; bh=Ew0XeQ+fh2/XZSpPp/yD0eYvnngadodiRmK9HwWtnbQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=V46KeyHw5LrF7VNoR5bJR/pSbEeRX4B4xYnufev0XmQ9+HJlcM5EeARijFWaWJq5JidAUs6qErtBmdo9bwwfWq13C8TPX5Aexru4m1i4RoxxQGAW1XWfiQ0EncK4TZTSEgUiqmniKtI18qmU5JckbtoaQSUKQpl9kmTjl27KDmU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com; spf=pass smtp.mailfrom=katalix.com; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b=jqs2I9Mc; arc=none smtp.client-ip=3.9.82.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=katalix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b="jqs2I9Mc" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:6c24:bf58:f1fe:91c1]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 5A6B87DCAF; Mon, 29 Jul 2024 16:38:15 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1722267495; bh=Ew0XeQ+fh2/XZSpPp/yD0eYvnngadodiRmK9HwWtnbQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:From; z=From:=20James=20Chapman=20|To:=20netdev@vge r.kernel.org|Cc:=20davem@davemloft.net,=0D=0A=09edumazet@google.co m,=0D=0A=09kuba@kernel.org,=0D=0A=09pabeni@redhat.com,=0D=0A=09dsa hern@kernel.org,=0D=0A=09tparkin@katalix.com|Subject:=20[PATCH=20n et-next=2003/15]=20l2tp:=20have=20l2tp_ip_destroy_sock=20use=20ip_ flush_pending_frames|Date:=20Mon,=2029=20Jul=202024=2016:38:02=20+ 0100|Message-Id:=20<8491d89e8ae68206971f35c572190ac8b7882c1d.17222 65212.git.jchapman@katalix.com>|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=jqs2I9McRnxePHSIgueVl+DnJy0t1hOyo7ddA2c4xPyNzfgjbk/SoYizAgdvft57f Zcu+JjpbEjd6lA2mvk53dNe1HJTzJ/n5iYog7K5yBqXd9+qmahUklE4Ff20O1Zanlz Y35jo0ZPgmaBcjTth3LmzIB1nVuY5D49mTuv8j8K67kY9rS7tX+bwjTwV3NR06I2qO qr8Ietj0MMHmqg7CxjyMgUZ2kQeQfeZ/2rqauWGaTQTzWO5sLB4pFO84GhsJfb/U4D tvSryi0OINTc04gaK8suNIi/BtK7hPtDmRTNeEFUH9mecnhA5CF1lVsFyCjAgUY19k VKCfQyka5tLnw== From: James Chapman To: netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, tparkin@katalix.com Subject: [PATCH net-next 03/15] l2tp: have l2tp_ip_destroy_sock use ip_flush_pending_frames Date: Mon, 29 Jul 2024 16:38:02 +0100 Message-Id: <8491d89e8ae68206971f35c572190ac8b7882c1d.1722265212.git.jchapman@katalix.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Use the recently exported ip_flush_pending_frames instead of a free-coded version and lock the socket while we call it. Signed-off-by: James Chapman Signed-off-by: Tom Parkin --- net/l2tp/l2tp_ip.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/net/l2tp/l2tp_ip.c b/net/l2tp/l2tp_ip.c index 78243f993cda..f21dcbf3efd5 100644 --- a/net/l2tp/l2tp_ip.c +++ b/net/l2tp/l2tp_ip.c @@ -236,10 +236,10 @@ static void l2tp_ip_close(struct sock *sk, long timeout) static void l2tp_ip_destroy_sock(struct sock *sk) { struct l2tp_tunnel *tunnel; - struct sk_buff *skb; - while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) - kfree_skb(skb); + lock_sock(sk); + ip_flush_pending_frames(sk); + release_sock(sk); tunnel = l2tp_sk_to_tunnel(sk); if (tunnel) { From patchwork Mon Jul 29 15:38:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13745238 X-Patchwork-Delegate: kuba@kernel.org Received: from mail.katalix.com (mail.katalix.com [3.9.82.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 51003158A3C for ; Mon, 29 Jul 2024 15:38:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.9.82.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267497; cv=none; b=c+Ud94FGOGFcifu4VL8/p5nn0XhTJsSqrHlUxjrDrbQTzvKfvo+slFcHcny2DBtUpVFszCSODY0FecyWrs8/yazaQkFbH9WBXlc5Ru81yXF1xrEBCg1rQ36UFpYIrm9cbZ+2prW4cKHs/6uT7yYVCygklbj1yChsI7LpnP8uq/o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267497; c=relaxed/simple; bh=iTT+MBvvVbh60MfgyqrVLxu2PYAR87uA1Il2SSsa7KU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BFWnVX7UpsH8S3CH4qfL7+pOpKfgfolCTZ+oIcrvZUGOhd31nZhdabBlGPMDPN/D537O0BidEp8eT0R/UqPusUkUlLlDVNg0U7Aa6kiXytt/8e05jL0Hzp6JOi87HhZsq61adVd0PiDDd012taAmCLwtc8smzgylLcBCrZUkhDQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com; spf=pass smtp.mailfrom=katalix.com; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b=HwAt/1Ok; arc=none smtp.client-ip=3.9.82.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=katalix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b="HwAt/1Ok" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:6c24:bf58:f1fe:91c1]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 795787DCB6; Mon, 29 Jul 2024 16:38:15 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1722267495; bh=iTT+MBvvVbh60MfgyqrVLxu2PYAR87uA1Il2SSsa7KU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:From; z=From:=20James=20Chapman=20|To:=20netdev@vge r.kernel.org|Cc:=20davem@davemloft.net,=0D=0A=09edumazet@google.co m,=0D=0A=09kuba@kernel.org,=0D=0A=09pabeni@redhat.com,=0D=0A=09dsa hern@kernel.org,=0D=0A=09tparkin@katalix.com|Subject:=20[PATCH=20n et-next=2004/15]=20l2tp:=20don't=20use=20tunnel=20socket=20sk_user _data=20in=20ppp=20procfs=20output|Date:=20Mon,=2029=20Jul=202024= 2016:38:03=20+0100|Message-Id:=20<4ee33c3696f7d2d75d4b553b977c8aff 8193477c.1722265212.git.jchapman@katalix.com>|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=HwAt/1OkAVJvU+hWgs5k+0ywOPtUSGor77R3aXJXYFDoINGIIKxj1WtOBQuy89YjT ykYJaLS4eUAmvpWz6VYrE7puF8ki8+xmJR7eg32K6EIoRibAllmLsQU9lQpv8lWQmZ dNBsDWWEJF4zdh9SjUdOq/PX119/T6Uz3fG32EXWUPKDP/ZTH0yMOA2Q3j4z9syxuh KU/42LKRFf08Kb2Au+iVJw5NjPx30qQHOesc34nXuY1/BnxIw395F7X1kS0ry3cLht uMup3lmnpGxUl/0QmwDy/wTbTVkqasFl+9JdGl6vIV86INst0jissYasTormMaeQay leKSjdOl1l2Og== From: James Chapman To: netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, tparkin@katalix.com Subject: [PATCH net-next 04/15] l2tp: don't use tunnel socket sk_user_data in ppp procfs output Date: Mon, 29 Jul 2024 16:38:03 +0100 Message-Id: <4ee33c3696f7d2d75d4b553b977c8aff8193477c.1722265212.git.jchapman@katalix.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org l2tp's ppp procfs output can be used to show internal state of pppol2tp. It includes a 'user-data-ok' field, which is derived from the tunnel socket's sk_user_data being non-NULL. Use tunnel->sock being non-NULL to indicate this instead. Signed-off-by: James Chapman Signed-off-by: Tom Parkin --- net/l2tp/l2tp_ppp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/l2tp/l2tp_ppp.c b/net/l2tp/l2tp_ppp.c index 3596290047b2..0844b86cd0a6 100644 --- a/net/l2tp/l2tp_ppp.c +++ b/net/l2tp/l2tp_ppp.c @@ -1513,7 +1513,7 @@ static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v) seq_printf(m, "\nTUNNEL '%s', %c %d\n", tunnel->name, - (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N', + tunnel->sock ? 'Y' : 'N', refcount_read(&tunnel->ref_count) - 1); seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n", 0, From patchwork Mon Jul 29 15:38:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13745242 X-Patchwork-Delegate: kuba@kernel.org Received: from mail.katalix.com (mail.katalix.com [3.9.82.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E0DF415A87C for ; Mon, 29 Jul 2024 15:38:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.9.82.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267499; cv=none; b=EFm4sn33CbjI5/WwgKDxJxAWFVSWvmNFNnRPB1IilHazFcfhNPEyKjKTIUD5RsSYvebFO9NpTa6i9oiXYNMmATGbynay26RHUhMb/1tt56zEd+wIjhXwZGIRrhCkUYksPKfCT/gaZ9O/wEi7HLEieUNMRmBl7ihOAZ2hr3mJlxg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267499; c=relaxed/simple; bh=lDt9orHvUGdX7CcMCF4+I9gxWcVU6a7LDDMuvDMMK/M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Nd9Epy/FwpCPbosjTELRt8xqvF4Ztj3cKyS0MGxC3s/7IYBoc8hQePVhJugiWrgScX9C4Iq3/uhAFXuIW/Ib3OIyBnZWk7C+qKdHDf7gCmoH7qu8Z1tZlldmWnTy7FSRa2wp85wZqURta6oKbmmumfYJWLOdYV9e08wUNiFjuN8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com; spf=pass smtp.mailfrom=katalix.com; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b=yrw27ek1; arc=none smtp.client-ip=3.9.82.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=katalix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b="yrw27ek1" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:6c24:bf58:f1fe:91c1]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 97F277DCD1; Mon, 29 Jul 2024 16:38:15 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1722267495; bh=lDt9orHvUGdX7CcMCF4+I9gxWcVU6a7LDDMuvDMMK/M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:From; z=From:=20James=20Chapman=20|To:=20netdev@vge r.kernel.org|Cc:=20davem@davemloft.net,=0D=0A=09edumazet@google.co m,=0D=0A=09kuba@kernel.org,=0D=0A=09pabeni@redhat.com,=0D=0A=09dsa hern@kernel.org,=0D=0A=09tparkin@katalix.com|Subject:=20[PATCH=20n et-next=2005/15]=20l2tp:=20don't=20set=20sk_user_data=20in=20tunne l=20socket|Date:=20Mon,=2029=20Jul=202024=2016:38:04=20+0100|Messa ge-Id:=20<9d5db3aeb2cac90f5f40d2cbf199a05837a50dd9.1722265212.git. jchapman@katalix.com>|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=yrw27ek1Msa8il0281SUgQ0tvEb+RUhvVkhXTBOoi29s9tPyzUKvL7FBG+WdbYrih GsEfwGc05fFQ/vrs7+c78roiDAmK2MQPYBAtMdrXRSopihUbDmztmj0ntZuTQOmNVG 5PKsw+eCsDL31eI0qbSh8obgTNa2tbEdhDISJv39/O0CJKBM7zMy7Uz58UX/cygMeO rOEepBfllfAvwM30ParT/SQQO4wMVrGPzCeBpNdYLulTegfPxIvu0kw5RQULpf99JP vjgGXJ66ucAqhr8xhU6VGoupjoWTptg2WRpv+jbMrVvvC2KVDzHBjIiqrIiA7Wlewu ni8eZWe0UfeEA== From: James Chapman To: netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, tparkin@katalix.com Subject: [PATCH net-next 05/15] l2tp: don't set sk_user_data in tunnel socket Date: Mon, 29 Jul 2024 16:38:04 +0100 Message-Id: <9d5db3aeb2cac90f5f40d2cbf199a05837a50dd9.1722265212.git.jchapman@katalix.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org l2tp no longer uses the tunnel socket's sk_user_data so drop the code which sets it. In l2tp_validate_socket use l2tp_sk_to_tunnel to check whether a given socket is already attached to an l2tp tunnel since we can no longer use non-null sk_user_data to indicate this. Signed-off-by: James Chapman Signed-off-by: Tom Parkin --- net/l2tp/l2tp_core.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c index c97cd0fd8514..59a171fa1a39 100644 --- a/net/l2tp/l2tp_core.c +++ b/net/l2tp/l2tp_core.c @@ -1263,7 +1263,6 @@ static void l2tp_tunnel_destruct(struct sock *sk) /* Remove hooks into tunnel socket */ write_lock_bh(&sk->sk_callback_lock); sk->sk_destruct = tunnel->old_sk_destruct; - sk->sk_user_data = NULL; write_unlock_bh(&sk->sk_callback_lock); /* Call the original destructor */ @@ -1554,6 +1553,8 @@ EXPORT_SYMBOL_GPL(l2tp_tunnel_create); static int l2tp_validate_socket(const struct sock *sk, const struct net *net, enum l2tp_encap_type encap) { + struct l2tp_tunnel *tunnel; + if (!net_eq(sock_net(sk), net)) return -EINVAL; @@ -1567,8 +1568,11 @@ static int l2tp_validate_socket(const struct sock *sk, const struct net *net, (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP)) return -EPROTONOSUPPORT; - if (sk->sk_user_data) + tunnel = l2tp_sk_to_tunnel(sk); + if (tunnel) { + l2tp_tunnel_dec_refcount(tunnel); return -EBUSY; + } return 0; } @@ -1607,12 +1611,10 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net, ret = l2tp_validate_socket(sk, net, tunnel->encap); if (ret < 0) goto err_inval_sock; - rcu_assign_sk_user_data(sk, tunnel); write_unlock_bh(&sk->sk_callback_lock); if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { struct udp_tunnel_sock_cfg udp_cfg = { - .sk_user_data = tunnel, .encap_type = UDP_ENCAP_L2TPINUDP, .encap_rcv = l2tp_udp_encap_recv, .encap_err_rcv = l2tp_udp_encap_err_recv, From patchwork Mon Jul 29 15:38:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13745241 X-Patchwork-Delegate: kuba@kernel.org Received: from mail.katalix.com (mail.katalix.com [3.9.82.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E0E3815AAC1 for ; Mon, 29 Jul 2024 15:38:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.9.82.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267499; cv=none; b=UXLrNWoK6s7bOZ2rn2veSC8IsdIJO2f/QAGoZHFQ/Gj6FP87z1x369962uIG3VuOyNUHf5MZxY2SQuCfdjqhH47vzQpTv3okYL0wqMrE3S0L4pW/T2j6EeI4QhMtjG/yVuPNA4r392E5L/5GB/I2E7Oobd1mD2BlsLlg2NotA68= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267499; c=relaxed/simple; bh=R97B9UZCbYzxtNIksHSmdO1l6y7EIS+8RcIWm+rKigE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QHOkfP6iT4DQWIcKUjaDyMBd1pjYDLv5WjcT5nQoqLSpUPXkx2+PlaBlUgo8RIaZLepaWU4lhLeq+9YFclUxoSe5xND0pI5i6QQRIwRcTxr8TpHDIqAlkDJl/x5+gXOqB1Wvmw53kjEf6WBxC2v5+NrPe8NDfifX2HEjv+9MXVE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com; spf=pass smtp.mailfrom=katalix.com; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b=uOFBDt8q; arc=none smtp.client-ip=3.9.82.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=katalix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b="uOFBDt8q" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:6c24:bf58:f1fe:91c1]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id B7B447DCF6; Mon, 29 Jul 2024 16:38:15 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1722267495; bh=R97B9UZCbYzxtNIksHSmdO1l6y7EIS+8RcIWm+rKigE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:From; z=From:=20James=20Chapman=20|To:=20netdev@vge r.kernel.org|Cc:=20davem@davemloft.net,=0D=0A=09edumazet@google.co m,=0D=0A=09kuba@kernel.org,=0D=0A=09pabeni@redhat.com,=0D=0A=09dsa hern@kernel.org,=0D=0A=09tparkin@katalix.com|Subject:=20[PATCH=20n et-next=2006/15]=20l2tp:=20remove=20unused=20tunnel=20magic=20fiel d|Date:=20Mon,=2029=20Jul=202024=2016:38:05=20+0100|Message-Id:=20 |In-Reply-To:=20|References:=20|MI ME-Version:=201.0; b=uOFBDt8qG39oQQKRveGngqPKSXBXD+ZP4P2dk/rPeawgpHWTB74iZAd3fl6kNy11j WRFafa2tObN3KAbvmzdqigAuHQx/45WYs/+EY60NmKsZubMLWmunAPKtiF0x0O+ZgW +Fj8I9g2Epit8KYJQ8ITKBL4T8kCaYu/7ypjpjSz9dXRhICFEUG7aCmlRlmnPUJfbT 1GlPO1aY9cGVvFsTJyQDCa+2pIM/r/imFHidXysbqz5rz7dv3MXu4ZkMeGwiPSPpaL TvH6pxjHHwn3JO93W41tB28+rrx5zoHBxmFzL6863WBvqII1mLrUhju+mQ4a3N7nSs Zz6lTooDJLGhg== From: James Chapman To: netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, tparkin@katalix.com Subject: [PATCH net-next 06/15] l2tp: remove unused tunnel magic field Date: Mon, 29 Jul 2024 16:38:05 +0100 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Since l2tp no longer derives tunnel pointers directly via sk_user_data, it is no longer useful for l2tp to check tunnel pointers using a magic feather. Drop the tunnel's magic field. Signed-off-by: James Chapman Signed-off-by: Tom Parkin --- net/l2tp/l2tp_core.c | 1 - net/l2tp/l2tp_core.h | 3 --- 2 files changed, 4 deletions(-) diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c index 59a171fa1a39..1ef14f99e78c 100644 --- a/net/l2tp/l2tp_core.c +++ b/net/l2tp/l2tp_core.c @@ -1527,7 +1527,6 @@ int l2tp_tunnel_create(int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, tunnel->tunnel_id = tunnel_id; tunnel->peer_tunnel_id = peer_tunnel_id; - tunnel->magic = L2TP_TUNNEL_MAGIC; sprintf(&tunnel->name[0], "tunl %u", tunnel_id); spin_lock_init(&tunnel->list_lock); tunnel->acpt_newsess = true; diff --git a/net/l2tp/l2tp_core.h b/net/l2tp/l2tp_core.h index a41cf6795df0..50107531fe3b 100644 --- a/net/l2tp/l2tp_core.h +++ b/net/l2tp/l2tp_core.h @@ -16,7 +16,6 @@ #endif /* Random numbers used for internal consistency checks of tunnel and session structures */ -#define L2TP_TUNNEL_MAGIC 0x42114DDA #define L2TP_SESSION_MAGIC 0x0C04EB7D struct sk_buff; @@ -155,8 +154,6 @@ struct l2tp_tunnel_cfg { */ #define L2TP_TUNNEL_NAME_MAX 20 struct l2tp_tunnel { - int magic; /* Should be L2TP_TUNNEL_MAGIC */ - unsigned long dead; struct rcu_head rcu; From patchwork Mon Jul 29 15:38:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13745244 X-Patchwork-Delegate: kuba@kernel.org Received: from mail.katalix.com (mail.katalix.com [3.9.82.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 197A015ADB2 for ; Mon, 29 Jul 2024 15:38:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.9.82.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267499; cv=none; b=HQ6+9irAjU9Q6oSdr/b9W/h5Lw7ELo8+gCmdTEX8iQ57qD8ocYTjvXPZok8XN0YNdQM3fIRAEEOk+CTkYfmm1Z8pgV8DsDGcjoPqq1sODZIDpOrOgadRBfNNlGKykyexp7kj5x/n4UXNxt1cxe0cuq+cRz29TjVPymAjPgBCj0M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267499; c=relaxed/simple; bh=DN0FCBLErOUKWe9+oA26vb1CyClrbnIdJx1UjjQjigU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LDh4t/YnOPbfum+UKf60yE4ylEHPhn2kFdhkkpFH9JPCPw1xMS/MlFnizsz9HFUuys2kqiwPFDMxC5OynoRZfTEMwZ+cyUnPHysQAA4qX/ZuiFQLBu6wfa5tbs3jrBomtr8VIab3JCP/n11NbjanUTTOeCcS6JNthQDSUnJNiU4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com; spf=pass smtp.mailfrom=katalix.com; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b=PFWZbrsQ; arc=none smtp.client-ip=3.9.82.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=katalix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b="PFWZbrsQ" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:6c24:bf58:f1fe:91c1]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id D69547DCF7; Mon, 29 Jul 2024 16:38:15 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1722267495; bh=DN0FCBLErOUKWe9+oA26vb1CyClrbnIdJx1UjjQjigU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:From; z=From:=20James=20Chapman=20|To:=20netdev@vge r.kernel.org|Cc:=20davem@davemloft.net,=0D=0A=09edumazet@google.co m,=0D=0A=09kuba@kernel.org,=0D=0A=09pabeni@redhat.com,=0D=0A=09dsa hern@kernel.org,=0D=0A=09tparkin@katalix.com|Subject:=20[PATCH=20n et-next=2007/15]=20l2tp:=20simplify=20tunnel=20and=20socket=20clea nup|Date:=20Mon,=2029=20Jul=202024=2016:38:06=20+0100|Message-Id:= 20<56813a06aa837737870ce5a8c9c5b7c50d2f3246.1722265212.git.jchapma n@katalix.com>|In-Reply-To:=20|References:=20| MIME-Version:=201.0; b=PFWZbrsQPuTs3rT8uqP1FeFQKhoJZdjGNBqp7UxkVejSkMF//DAsbazlSiwOb+6rM 0paw4tBGfPIWEbP/EvLRJqjhahVbFr9/q4kbLrENcQ7lfcdcX6e5Agx4kVPrAr2YB7 Lr1LiloC5Pn3amGNbQf1w35w9gnUsZzQgsh1MgL+ckApdN1dvWFM+PKs6F+fhOkH0/ MVvsA4//SrY4qJuxt0LvVBFZeFd0I8Nq4Yg/kd3Z0EUg/5RM24kg0hFOBMVKFKaV6g tauezLq+XFeZawLK72hfZmwIG+9q0nnmLhwUhmoJfukNs2YI+UgO079W5RBfmgjhhA ml7lOrVQZQ7pg== From: James Chapman To: netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, tparkin@katalix.com Subject: [PATCH net-next 07/15] l2tp: simplify tunnel and socket cleanup Date: Mon, 29 Jul 2024 16:38:06 +0100 Message-Id: <56813a06aa837737870ce5a8c9c5b7c50d2f3246.1722265212.git.jchapman@katalix.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org When the l2tp tunnel socket used sk_user_data to point to its associated l2tp tunnel, socket and tunnel cleanup had to make use of the socket's destructor to free the tunnel only when the socket could no longer be accessed. Now that sk_user_data is no longer used, we can simplify socket and tunnel cleanup: * If the tunnel closes first, it cleans up and drops its socket ref when the tunnel refcount drops to zero. If its socket was provided by userspace, the socket is closed and freed asynchronously, when userspace closes it. If its socket is a kernel socket, the tunnel closes the socket itself during cleanup and drops its socket ref when the tunnel's refcount drops to zero. * If the socket closes first, we initiate the closing of its associated tunnel. For UDP sockets, this is via the socket's encap_destroy hook. For L2TPIP sockets, this is via the socket's destroy callback. The tunnel holds a socket ref while it references the sock. When the tunnel is freed, it drops its socket ref and the socket will be cleaned up when its own refcount drops to zero, asynchronous to the tunnel free. * The tunnel socket destructor is no longer needed since the tunnel is no longer freed through the socket destructor. Signed-off-by: James Chapman Signed-off-by: Tom Parkin --- net/l2tp/l2tp_core.c | 82 ++++++++++++-------------------------------- net/l2tp/l2tp_core.h | 1 - 2 files changed, 21 insertions(+), 62 deletions(-) diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c index 1ef14f99e78c..a01dd891639b 100644 --- a/net/l2tp/l2tp_core.c +++ b/net/l2tp/l2tp_core.c @@ -137,9 +137,28 @@ static inline struct l2tp_net *l2tp_pernet(const struct net *net) static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel) { + struct sock *sk = tunnel->sock; + trace_free_tunnel(tunnel); - sock_put(tunnel->sock); - /* the tunnel is freed in the socket destructor */ + + if (sk) { + /* Disable udp encapsulation */ + switch (tunnel->encap) { + case L2TP_ENCAPTYPE_UDP: + /* No longer an encapsulation socket. See net/ipv4/udp.c */ + WRITE_ONCE(udp_sk(sk)->encap_type, 0); + udp_sk(sk)->encap_rcv = NULL; + udp_sk(sk)->encap_destroy = NULL; + break; + case L2TP_ENCAPTYPE_IP: + break; + } + + tunnel->sock = NULL; + sock_put(sk); + } + + kfree_rcu(tunnel, rcu); } static void l2tp_session_free(struct l2tp_session *session) @@ -150,23 +169,6 @@ static void l2tp_session_free(struct l2tp_session *session) kfree(session); } -static struct l2tp_tunnel *__l2tp_sk_to_tunnel(const struct sock *sk) -{ - const struct net *net = sock_net(sk); - unsigned long tunnel_id, tmp; - struct l2tp_tunnel *tunnel; - struct l2tp_net *pn; - - WARN_ON_ONCE(!rcu_read_lock_bh_held()); - pn = l2tp_pernet(net); - idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) { - if (tunnel && tunnel->sock == sk) - return tunnel; - } - - return NULL; -} - struct l2tp_tunnel *l2tp_sk_to_tunnel(const struct sock *sk) { const struct net *net = sock_net(sk); @@ -1235,46 +1237,6 @@ EXPORT_SYMBOL_GPL(l2tp_xmit_skb); * Tinnel and session create/destroy. *****************************************************************************/ -/* Tunnel socket destruct hook. - * The tunnel context is deleted only when all session sockets have been - * closed. - */ -static void l2tp_tunnel_destruct(struct sock *sk) -{ - struct l2tp_tunnel *tunnel; - - rcu_read_lock_bh(); - tunnel = __l2tp_sk_to_tunnel(sk); - if (!tunnel) - goto end; - - /* Disable udp encapsulation */ - switch (tunnel->encap) { - case L2TP_ENCAPTYPE_UDP: - /* No longer an encapsulation socket. See net/ipv4/udp.c */ - WRITE_ONCE(udp_sk(sk)->encap_type, 0); - udp_sk(sk)->encap_rcv = NULL; - udp_sk(sk)->encap_destroy = NULL; - break; - case L2TP_ENCAPTYPE_IP: - break; - } - - /* Remove hooks into tunnel socket */ - write_lock_bh(&sk->sk_callback_lock); - sk->sk_destruct = tunnel->old_sk_destruct; - write_unlock_bh(&sk->sk_callback_lock); - - /* Call the original destructor */ - if (sk->sk_destruct) - (*sk->sk_destruct)(sk); - - kfree_rcu(tunnel, rcu); -end: - rcu_read_unlock_bh(); - return; -} - /* Remove an l2tp session from l2tp_core's lists. */ static void l2tp_session_unhash(struct l2tp_session *session) { @@ -1623,8 +1585,6 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net, setup_udp_tunnel_sock(net, sock, &udp_cfg); } - tunnel->old_sk_destruct = sk->sk_destruct; - sk->sk_destruct = &l2tp_tunnel_destruct; sk->sk_allocation = GFP_ATOMIC; release_sock(sk); diff --git a/net/l2tp/l2tp_core.h b/net/l2tp/l2tp_core.h index 50107531fe3b..6c62d02a0ae6 100644 --- a/net/l2tp/l2tp_core.h +++ b/net/l2tp/l2tp_core.h @@ -173,7 +173,6 @@ struct l2tp_tunnel { struct net *l2tp_net; /* the net we belong to */ refcount_t ref_count; - void (*old_sk_destruct)(struct sock *sk); struct sock *sock; /* parent socket */ int fd; /* parent fd, if tunnel socket was created * by userspace From patchwork Mon Jul 29 15:38:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13745250 X-Patchwork-Delegate: kuba@kernel.org Received: from mail.katalix.com (mail.katalix.com [3.9.82.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1974A15ADB1 for ; Mon, 29 Jul 2024 15:38:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.9.82.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267500; cv=none; b=jhgHLjEgjQLIC5bUoncEKKL0V5w0q0TCTpsgexYnCJJchrptmOunZpza5kTgaFnFH8f5NunuhyCo2llg0vpIusRnHVwwu/BTdLBpIOT7PL8l7/wKEWep17Uqxx+TgwzabikJSa/G9KGiusT1NiCBGPGMSkG7b1DhLNXiyr1yXBk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267500; c=relaxed/simple; bh=RcNEQmfbJQvSX3y2wP3FnOrLIv+h4CO0FbMl/1UPY18=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QTWMmf8p2unRJ2cToJHyi2E7xytnso0ljK1chs2VeFkn2R1Lg11ceiQImSjU1Y+SKh88lTm7Z9Fcgl2ZwmzAdnZwCgUSQ2/st4honYUl7TB04SMvDOIiBAaRzRdkcCOGYtodvQynT0gwS4K45bZ0hO+2jnIDbltO/7goncWLJ6s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com; spf=pass smtp.mailfrom=katalix.com; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b=RHRBxTnw; arc=none smtp.client-ip=3.9.82.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=katalix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b="RHRBxTnw" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:6c24:bf58:f1fe:91c1]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 007217DCF8; Mon, 29 Jul 2024 16:38:15 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1722267496; bh=RcNEQmfbJQvSX3y2wP3FnOrLIv+h4CO0FbMl/1UPY18=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:From; z=From:=20James=20Chapman=20|To:=20netdev@vge r.kernel.org|Cc:=20davem@davemloft.net,=0D=0A=09edumazet@google.co m,=0D=0A=09kuba@kernel.org,=0D=0A=09pabeni@redhat.com,=0D=0A=09dsa hern@kernel.org,=0D=0A=09tparkin@katalix.com|Subject:=20[PATCH=20n et-next=2008/15]=20l2tp:=20delete=20sessions=20using=20work=20queu e|Date:=20Mon,=2029=20Jul=202024=2016:38:07=20+0100|Message-Id:=20 <60b5d6dd8aab4e2045d2bb42545a1eaea273e134.1722265212.git.jchapman@ katalix.com>|In-Reply-To:=20|References:=20|MI ME-Version:=201.0; b=RHRBxTnwXOqGmfXiIZMqJN0zKWnTypmtL2SYVvTYxhBYEDDeJq93/3OjdXxPymQGe liOM3OmgUIQpLIFkdZhl+3jA1hCu8frA3hRF/XY7T/7YPq8EwUW454kYsrspZ1ODMo LlRZrZueBDUphPaOHzM7igg2ce5LgBU/HLN8LWqeOY9IGvYTXgK1zU+uwXvGA6hXRs EwRYb6s9B1oobIOtUgmUVBuzeETTRiQB2O76c1GoEO0NB45Gnh80m0W1GHRGMSQ/m7 9ZTeeXg7bLzA0XTq+56pepO6/2Ee8iHEm/pk7wSuYKLnTxpFPDhJ/PZLtJwarjDa0E xEhUWUJ2oD3bg== From: James Chapman To: netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, tparkin@katalix.com Subject: [PATCH net-next 08/15] l2tp: delete sessions using work queue Date: Mon, 29 Jul 2024 16:38:07 +0100 Message-Id: <60b5d6dd8aab4e2045d2bb42545a1eaea273e134.1722265212.git.jchapman@katalix.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org When a tunnel is closed, l2tp_tunnel_closeall closes all sessions in the tunnel. Move the work of deleting each session to the work queue so that sessions are deleted using the same codepath whether they are closed by user API request or their parent tunnel is closing. This also avoids the locking dance in l2tp_tunnel_closeall where the tunnel's session list lock was unlocked and relocked in the loop. In l2tp_exit_net, use drain_workqueue instead of flush_workqueue because the processing of tunnel_delete work may queue session_delete work items which must also be processed. Signed-off-by: James Chapman Signed-off-by: Tom Parkin --- net/l2tp/l2tp_core.c | 36 ++++++++++++++++++++---------------- net/l2tp/l2tp_core.h | 1 + 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c index a01dd891639b..f6ae18c180bf 100644 --- a/net/l2tp/l2tp_core.c +++ b/net/l2tp/l2tp_core.c @@ -1282,18 +1282,8 @@ static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) spin_lock_bh(&tunnel->list_lock); tunnel->acpt_newsess = false; - for (;;) { - session = list_first_entry_or_null(&tunnel->session_list, - struct l2tp_session, list); - if (!session) - break; - l2tp_session_inc_refcount(session); - list_del_init(&session->list); - spin_unlock_bh(&tunnel->list_lock); + list_for_each_entry(session, &tunnel->session_list, list) l2tp_session_delete(session); - spin_lock_bh(&tunnel->list_lock); - l2tp_session_dec_refcount(session); - } spin_unlock_bh(&tunnel->list_lock); } @@ -1631,18 +1621,31 @@ EXPORT_SYMBOL_GPL(l2tp_tunnel_delete); void l2tp_session_delete(struct l2tp_session *session) { - if (test_and_set_bit(0, &session->dead)) - return; + if (!test_and_set_bit(0, &session->dead)) { + trace_delete_session(session); + l2tp_session_inc_refcount(session); + queue_work(l2tp_wq, &session->del_work); + } +} +EXPORT_SYMBOL_GPL(l2tp_session_delete); + +/* Workqueue session deletion function */ +static void l2tp_session_del_work(struct work_struct *work) +{ + struct l2tp_session *session = container_of(work, struct l2tp_session, + del_work); - trace_delete_session(session); l2tp_session_unhash(session); l2tp_session_queue_purge(session); if (session->session_close) (*session->session_close)(session); + /* drop initial ref */ + l2tp_session_dec_refcount(session); + + /* drop workqueue ref */ l2tp_session_dec_refcount(session); } -EXPORT_SYMBOL_GPL(l2tp_session_delete); /* We come here whenever a session's send_seq, cookie_len or * l2specific_type parameters are set. @@ -1694,6 +1697,7 @@ struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunn INIT_HLIST_NODE(&session->hlist); INIT_LIST_HEAD(&session->clist); INIT_LIST_HEAD(&session->list); + INIT_WORK(&session->del_work, l2tp_session_del_work); if (cfg) { session->pwtype = cfg->pw_type; @@ -1751,7 +1755,7 @@ static __net_exit void l2tp_exit_net(struct net *net) rcu_read_unlock_bh(); if (l2tp_wq) - flush_workqueue(l2tp_wq); + drain_workqueue(l2tp_wq); rcu_barrier(); idr_destroy(&pn->l2tp_v2_session_idr); diff --git a/net/l2tp/l2tp_core.h b/net/l2tp/l2tp_core.h index 6c62d02a0ae6..8d7a589ccd2a 100644 --- a/net/l2tp/l2tp_core.h +++ b/net/l2tp/l2tp_core.h @@ -102,6 +102,7 @@ struct l2tp_session { int reorder_skip; /* set if skip to next nr */ enum l2tp_pwtype pwtype; struct l2tp_stats stats; + struct work_struct del_work; /* Session receive handler for data packets. * Each pseudowire implementation should implement this callback in order to From patchwork Mon Jul 29 15:38:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13745243 X-Patchwork-Delegate: kuba@kernel.org Received: from mail.katalix.com (mail.katalix.com [3.9.82.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1CD8D158D92 for ; Mon, 29 Jul 2024 15:38:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.9.82.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267499; cv=none; b=oFShUAi8q1Syd44OMmacqhJAEPW6NKzuT8vHch6Z3O2cnVwchAtYGTYoDPzKk2Trq0WsswIMYPG9itj+IEPl9MPecqN7VV0/NkwmtFu6MfpTKf+FlNmiK2DjScAbr7q+PdQKwSSEL41hCPuKTtSq4lZg0tThzHUzCsb8Ropp2yg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267499; c=relaxed/simple; bh=eg9F/FDiHVx3s1ThnwBiHWCoM9DAnG154rBPFm+q2NA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LUexFWthGgGAbxlTfkkEgOWWmcn+qs4Z3jSKuqqfgsaKmvWpQDoiviV8HjgD+SKWBfHJ/JonOJCs0MGBBBsOoLNSmqFXdsCJi2YWqCv3ZpzvFs3XEtzNxaInqm+3rD4d8ABOlM5ljR5WGNj4wawhDa4s0obCZU9JRQ2MzguiVqk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com; spf=pass smtp.mailfrom=katalix.com; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b=U/2zMNgJ; arc=none smtp.client-ip=3.9.82.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=katalix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b="U/2zMNgJ" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:6c24:bf58:f1fe:91c1]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 1E6F47DCF9; Mon, 29 Jul 2024 16:38:16 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1722267496; bh=eg9F/FDiHVx3s1ThnwBiHWCoM9DAnG154rBPFm+q2NA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:From; z=From:=20James=20Chapman=20|To:=20netdev@vge r.kernel.org|Cc:=20davem@davemloft.net,=0D=0A=09edumazet@google.co m,=0D=0A=09kuba@kernel.org,=0D=0A=09pabeni@redhat.com,=0D=0A=09dsa hern@kernel.org,=0D=0A=09tparkin@katalix.com|Subject:=20[PATCH=20n et-next=2009/15]=20l2tp:=20free=20sessions=20using=20rcu|Date:=20M on,=2029=20Jul=202024=2016:38:08=20+0100|Message-Id:=20<1d9c903d17 d8ec6e1e4f8a674693526a14a904ee.1722265212.git.jchapman@katalix.com >|In-Reply-To:=20|Refer ences:=20|MIME-Version: =201.0; b=U/2zMNgJO7RJY9YbIdtE1mYq9x1WUjbr2x1WDmBd/l7aQMl38NzaBckLpnikAaWor kyYPBHtn1o0WqQGIjmybySEnD0CDx6BhHcOIbEADJ0jgSUQcT3Ff0eC7nsRhMeizkM nyMC3aLOLCuL+9BAkHX/16DyrpUpX3mKIq2rc1EDfR71Y47o1n3hhcwD4MZsf5U7In FwI1xIM/LKqQH52bbuIVlQqNz3yGhOp/P6EP/r7a1PcQwXZXRosa+UApz9gC6DNW9d 9Lfvm6+/oWQ7nGKgru4PYjXmnkMwy8bm4F2lxjBwZJckyJlNDkMfRL+TywzNVW25Nt o4gkaHj0QzhDQ== From: James Chapman To: netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, tparkin@katalix.com Subject: [PATCH net-next 09/15] l2tp: free sessions using rcu Date: Mon, 29 Jul 2024 16:38:08 +0100 Message-Id: <1d9c903d17d8ec6e1e4f8a674693526a14a904ee.1722265212.git.jchapman@katalix.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org l2tp sessions may be accessed under an rcu read lock. Have them freed via rcu and remove the now unneeded synchronize_rcu when a session is removed. Signed-off-by: James Chapman Signed-off-by: Tom Parkin --- net/l2tp/l2tp_core.c | 4 +--- net/l2tp/l2tp_core.h | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c index f6ae18c180bf..4cf4aa271353 100644 --- a/net/l2tp/l2tp_core.c +++ b/net/l2tp/l2tp_core.c @@ -166,7 +166,7 @@ static void l2tp_session_free(struct l2tp_session *session) trace_free_session(session); if (session->tunnel) l2tp_tunnel_dec_refcount(session->tunnel); - kfree(session); + kfree_rcu(session, rcu); } struct l2tp_tunnel *l2tp_sk_to_tunnel(const struct sock *sk) @@ -1269,8 +1269,6 @@ static void l2tp_session_unhash(struct l2tp_session *session) spin_unlock_bh(&pn->l2tp_session_idr_lock); spin_unlock_bh(&tunnel->list_lock); - - synchronize_rcu(); } } diff --git a/net/l2tp/l2tp_core.h b/net/l2tp/l2tp_core.h index 8d7a589ccd2a..58d3977870de 100644 --- a/net/l2tp/l2tp_core.h +++ b/net/l2tp/l2tp_core.h @@ -66,6 +66,7 @@ struct l2tp_session_coll_list { struct l2tp_session { int magic; /* should be L2TP_SESSION_MAGIC */ long dead; + struct rcu_head rcu; struct l2tp_tunnel *tunnel; /* back pointer to tunnel context */ u32 session_id; From patchwork Mon Jul 29 15:38:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13745245 X-Patchwork-Delegate: kuba@kernel.org Received: from mail.katalix.com (mail.katalix.com [3.9.82.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5100815B113 for ; Mon, 29 Jul 2024 15:38:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.9.82.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267500; cv=none; b=KACMIhjSVHt65wzt4EqKBEyNJcMqkJtpfXdCR593PaIOgBIwAAIAsUE4E1LvLWSd220Fmwmq3LV0fcV57M/IeS2SAKtUU9m3yCF2vrpboeRvFSLBFsw+wSt2OY5gM2+YfWkf2AnB1rNv/5+wqjpS/2aGwMPntzuh+9Eq6yieByY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267500; c=relaxed/simple; bh=4i3YPtKNcKzpujXehcOWVjfw4wfenFb0UN4K9GbcbcU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NXM2DNBCxXPDBWnmH/FamW+PY9vU66vep8W60rUcSWVR04KzUDaFj0H/Edt+gomDii8s9G8R1n4eaDLAuMfry2I8451nlYjs8aGwWnDLsuwIaW+k0cDVs10HN4g4/lowjSz6Uhx82JhKoMZQC7XhVu+iROzE8wRvfzrSgzAoT6U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com; spf=pass smtp.mailfrom=katalix.com; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b=bPuf1a/y; arc=none smtp.client-ip=3.9.82.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=katalix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b="bPuf1a/y" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:6c24:bf58:f1fe:91c1]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 3D13D7DCFA; Mon, 29 Jul 2024 16:38:16 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1722267496; bh=4i3YPtKNcKzpujXehcOWVjfw4wfenFb0UN4K9GbcbcU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:From; z=From:=20James=20Chapman=20|To:=20netdev@vge r.kernel.org|Cc:=20davem@davemloft.net,=0D=0A=09edumazet@google.co m,=0D=0A=09kuba@kernel.org,=0D=0A=09pabeni@redhat.com,=0D=0A=09dsa hern@kernel.org,=0D=0A=09tparkin@katalix.com|Subject:=20[PATCH=20n et-next=2010/15]=20l2tp:=20refactor=20ppp=20socket/session=20relat ionship|Date:=20Mon,=2029=20Jul=202024=2016:38:09=20+0100|Message- Id:=20<444e08a9ef4f955b6fb4893c3e59fc7240de8f68.1722265212.git.jch apman@katalix.com>|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=bPuf1a/ynlfbIAE0Ny+0j7gM88v/BNMrQABA2SEh0K646pMH/ehbh4qF8BkcHHt2e sR1rVmn5bm8efX7XtI0GmXE0F7RwWCbnpyGUlpkFj9pFkSiD2jk8s1iixOVxsBClX2 5LBCaHo17ECrvHcVD2CuqDqNQkgbLUPfGuFoFSrH1Kt6+0EOO9RyDznxLoFR3yEAFF sSvq/nTaix5eBYgcLZSjMKox2GqTh0b5eXWR2deWprL9W7XQubWKIEe85IdKh8HOZF lhEWYaiJ4oD7mwTQCWduZJYew9719+k2r/OOhxZFuAcRSBZOFmE5rasItn2l9OF4g0 F8lmGlsHcAoyw== From: James Chapman To: netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, tparkin@katalix.com Subject: [PATCH net-next 10/15] l2tp: refactor ppp socket/session relationship Date: Mon, 29 Jul 2024 16:38:09 +0100 Message-Id: <444e08a9ef4f955b6fb4893c3e59fc7240de8f68.1722265212.git.jchapman@katalix.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Each l2tp ppp session has an associated pppox socket. l2tp_ppp uses the session's pppox socket refcount to manage session lifetimes; the pppox socket holds a ref on the session which is dropped by the socket destructor. This complicates session cleanup. Given l2tp sessions are refcounted, it makes more sense to reverse this relationship such that the session keeps the socket alive, not the other way around. So refactor l2tp_ppp to have the session hold a ref on its socket while it references it. When the session is closed, it drops its socket ref when it detaches from its socket. If the socket is closed first, it initiates the closing of its session, if one is attached. The socket/session can then be freed asynchronously when their refcounts drop to 0. Use the session's session_close callback to detach the pppox socket since this will be done on the work queue together with the rest of the session cleanup via l2tp_session_delete. Also, since l2tp_ppp uses the pppox socket's sk_user_data, use the rcu sk_user_data access helpers when accessing it and set the socket's SOCK_RCU_FREE flag to have pppox sockets freed by rcu. Signed-off-by: James Chapman Signed-off-by: Tom Parkin --- net/l2tp/l2tp_ppp.c | 94 +++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 55 deletions(-) diff --git a/net/l2tp/l2tp_ppp.c b/net/l2tp/l2tp_ppp.c index 0844b86cd0a6..12a0a7162870 100644 --- a/net/l2tp/l2tp_ppp.c +++ b/net/l2tp/l2tp_ppp.c @@ -119,7 +119,6 @@ struct pppol2tp_session { struct mutex sk_lock; /* Protects .sk */ struct sock __rcu *sk; /* Pointer to the session PPPoX socket */ struct sock *__sk; /* Copy of .sk, for cleanup */ - struct rcu_head rcu; /* For asynchronous release */ }; static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb); @@ -157,20 +156,16 @@ static inline struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk) if (!sk) return NULL; - sock_hold(sk); - session = (struct l2tp_session *)(sk->sk_user_data); - if (!session) { - sock_put(sk); - goto out; - } - if (WARN_ON(session->magic != L2TP_SESSION_MAGIC)) { - session = NULL; - sock_put(sk); - goto out; + rcu_read_lock(); + session = rcu_dereference_sk_user_data(sk); + if (session && refcount_inc_not_zero(&session->ref_count)) { + rcu_read_unlock(); + WARN_ON_ONCE(session->magic != L2TP_SESSION_MAGIC); + return session; } + rcu_read_unlock(); -out: - return session; + return NULL; } /***************************************************************************** @@ -318,12 +313,12 @@ static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m, l2tp_xmit_skb(session, skb); local_bh_enable(); - sock_put(sk); + l2tp_session_dec_refcount(session); return total_len; error_put_sess: - sock_put(sk); + l2tp_session_dec_refcount(session); error: return error; } @@ -377,12 +372,12 @@ static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb) l2tp_xmit_skb(session, skb); local_bh_enable(); - sock_put(sk); + l2tp_session_dec_refcount(session); return 1; abort_put_sess: - sock_put(sk); + l2tp_session_dec_refcount(session); abort: /* Free the original skb */ kfree_skb(skb); @@ -393,28 +388,31 @@ static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb) * Session (and tunnel control) socket create/destroy. *****************************************************************************/ -static void pppol2tp_put_sk(struct rcu_head *head) -{ - struct pppol2tp_session *ps; - - ps = container_of(head, typeof(*ps), rcu); - sock_put(ps->__sk); -} - /* Really kill the session socket. (Called from sock_put() if * refcnt == 0.) */ static void pppol2tp_session_destruct(struct sock *sk) { - struct l2tp_session *session = sk->sk_user_data; - skb_queue_purge(&sk->sk_receive_queue); skb_queue_purge(&sk->sk_write_queue); +} - if (session) { - sk->sk_user_data = NULL; - if (WARN_ON(session->magic != L2TP_SESSION_MAGIC)) - return; +static void pppol2tp_session_close(struct l2tp_session *session) +{ + struct pppol2tp_session *ps; + + ps = l2tp_session_priv(session); + mutex_lock(&ps->sk_lock); + ps->__sk = rcu_dereference_protected(ps->sk, + lockdep_is_held(&ps->sk_lock)); + RCU_INIT_POINTER(ps->sk, NULL); + mutex_unlock(&ps->sk_lock); + if (ps->__sk) { + /* detach socket */ + rcu_assign_sk_user_data(ps->__sk, NULL); + sock_put(ps->__sk); + + /* drop ref taken when we referenced socket via sk_user_data */ l2tp_session_dec_refcount(session); } } @@ -444,30 +442,13 @@ static int pppol2tp_release(struct socket *sock) session = pppol2tp_sock_to_session(sk); if (session) { - struct pppol2tp_session *ps; - l2tp_session_delete(session); - - ps = l2tp_session_priv(session); - mutex_lock(&ps->sk_lock); - ps->__sk = rcu_dereference_protected(ps->sk, - lockdep_is_held(&ps->sk_lock)); - RCU_INIT_POINTER(ps->sk, NULL); - mutex_unlock(&ps->sk_lock); - call_rcu(&ps->rcu, pppol2tp_put_sk); - - /* Rely on the sock_put() call at the end of the function for - * dropping the reference held by pppol2tp_sock_to_session(). - * The last reference will be dropped by pppol2tp_put_sk(). - */ + /* drop ref taken by pppol2tp_sock_to_session */ + l2tp_session_dec_refcount(session); } release_sock(sk); - /* This will delete the session context via - * pppol2tp_session_destruct() if the socket's refcnt drops to - * zero. - */ sock_put(sk); return 0; @@ -506,6 +487,7 @@ static int pppol2tp_create(struct net *net, struct socket *sock, int kern) goto out; sock_init_data(sock, sk); + sock_set_flag(sk, SOCK_RCU_FREE); sock->state = SS_UNCONNECTED; sock->ops = &pppol2tp_ops; @@ -542,6 +524,7 @@ static void pppol2tp_session_init(struct l2tp_session *session) struct pppol2tp_session *ps; session->recv_skb = pppol2tp_recv; + session->session_close = pppol2tp_session_close; if (IS_ENABLED(CONFIG_L2TP_DEBUGFS)) session->show = pppol2tp_show; @@ -830,12 +813,13 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, out_no_ppp: /* This is how we get the session context from the socket. */ - sk->sk_user_data = session; + sock_hold(sk); + rcu_assign_sk_user_data(sk, session); rcu_assign_pointer(ps->sk, sk); mutex_unlock(&ps->sk_lock); /* Keep the reference we've grabbed on the session: sk doesn't expect - * the session to disappear. pppol2tp_session_destruct() is responsible + * the session to disappear. pppol2tp_session_close() is responsible * for dropping it. */ drop_refcnt = false; @@ -1002,7 +986,7 @@ static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr, error = len; - sock_put(sk); + l2tp_session_dec_refcount(session); end: return error; } @@ -1274,7 +1258,7 @@ static int pppol2tp_setsockopt(struct socket *sock, int level, int optname, err = pppol2tp_session_setsockopt(sk, session, optname, val); } - sock_put(sk); + l2tp_session_dec_refcount(session); end: return err; } @@ -1395,7 +1379,7 @@ static int pppol2tp_getsockopt(struct socket *sock, int level, int optname, err = 0; end_put_sess: - sock_put(sk); + l2tp_session_dec_refcount(session); end: return err; } From patchwork Mon Jul 29 15:38:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13745247 X-Patchwork-Delegate: kuba@kernel.org Received: from mail.katalix.com (mail.katalix.com [3.9.82.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7033C15B13C for ; Mon, 29 Jul 2024 15:38:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.9.82.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267500; cv=none; b=lR/tm/ks4YFVsxMXSlX5s5tm0zE5wuvmBa6zir5N4NPnuAVjr3MFvNq+fVfwKKuFYVt7OAcnZQryC2GV4IUDojUmIKwtQmK3GBD+n6RskH9Jknkofj59NDD6ws1uasXlXY4niQ7kCf4AvU+tCSeesd5sKHZigbbIEvYDO5mye1U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267500; c=relaxed/simple; bh=HU1Jpf+T2dUov3K5TBhbMvV98ArKtLsdpZviyxFo2is=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VJlo73BPmmkw5jr6mukawPHX/sTHYryxciZPAI1g91tq3wBBJXNR8gPShnFqSUyviWlO5TNgaUo2nrfB6TqpRetg0RP7BjyXAcjwysh0SX20pTFdTYyJ7R37E8qEsUltV8CPfHneDE03HOmzd5ShDavRIzHLjn8U2fhgcIi9FEc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com; spf=pass smtp.mailfrom=katalix.com; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b=JPhssaJA; arc=none smtp.client-ip=3.9.82.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=katalix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b="JPhssaJA" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:6c24:bf58:f1fe:91c1]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 5BB5C7DCFB; Mon, 29 Jul 2024 16:38:16 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1722267496; bh=HU1Jpf+T2dUov3K5TBhbMvV98ArKtLsdpZviyxFo2is=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:From; z=From:=20James=20Chapman=20|To:=20netdev@vge r.kernel.org|Cc:=20davem@davemloft.net,=0D=0A=09edumazet@google.co m,=0D=0A=09kuba@kernel.org,=0D=0A=09pabeni@redhat.com,=0D=0A=09dsa hern@kernel.org,=0D=0A=09tparkin@katalix.com|Subject:=20[PATCH=20n et-next=2011/15]=20l2tp:=20prevent=20possible=20tunnel=20refcount= 20underflow|Date:=20Mon,=2029=20Jul=202024=2016:38:10=20+0100|Mess age-Id:=20<4af28001f1fe201c3cff73b62c7cacaf98a513b2.1722265212.git .jchapman@katalix.com>|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=JPhssaJA8A06EMueBC1rMNrhR9mmHa8VzDDvUmHMyJN9pp8SFZWAn/cnVhln0JFA/ QEPbIFlzCJUoNkMwasHb6xykCg5Hu2+bMjF/igPl+QihsfZy0LYWXiC9sdBd6nb5l7 HswXk1Rttxl+B+c7pPa2CuOuDa/N/UFhrZ+qLtOQN1JPINXzRzcu9RpR1ePiRAUBCH iFUocwV2zrFJnVeEpJWrWpX3cQD3f1p8au0qDARMju9CF5OCpVedwvTBa5kswUpUMw tOtiqencyY2yki/m2Tj8phrRC4y7cSMLWo4puZbpTm6dMb9oFQV7OtwBpVzkq86Kqw Uj4PUk3iphh/A== From: James Chapman To: netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, tparkin@katalix.com Subject: [PATCH net-next 11/15] l2tp: prevent possible tunnel refcount underflow Date: Mon, 29 Jul 2024 16:38:10 +0100 Message-Id: <4af28001f1fe201c3cff73b62c7cacaf98a513b2.1722265212.git.jchapman@katalix.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org When a session is created, it sets a backpointer to its tunnel. When the session refcount drops to 0, l2tp_session_free drops the tunnel refcount if session->tunnel is non-NULL. However, session->tunnel is set in l2tp_session_create, before the tunnel refcount is incremented by l2tp_session_register, which leaves a small window where session->tunnel is non-NULL when the tunnel refcount hasn't been bumped. Moving the assignment to l2tp_session_register is trivial but l2tp_session_create calls l2tp_session_set_header_len which uses session->tunnel to get the tunnel's encap. Add an encap arg to l2tp_session_set_header_len to avoid using session->tunnel. If l2tpv3 sessions have colliding IDs, it is possible for l2tp_v3_session_get to race with l2tp_session_register and fetch a session which doesn't yet have session->tunnel set. Add a check for this case. Signed-off-by: James Chapman Signed-off-by: Tom Parkin --- net/l2tp/l2tp_core.c | 24 +++++++++++++++++------- net/l2tp/l2tp_core.h | 3 ++- net/l2tp/l2tp_netlink.c | 4 +++- net/l2tp/l2tp_ppp.c | 3 ++- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c index 4cf4aa271353..cbf117683fab 100644 --- a/net/l2tp/l2tp_core.c +++ b/net/l2tp/l2tp_core.c @@ -279,7 +279,14 @@ struct l2tp_session *l2tp_v3_session_get(const struct net *net, struct sock *sk, hash_for_each_possible_rcu(pn->l2tp_v3_session_htable, session, hlist, key) { - if (session->tunnel->sock == sk && + /* session->tunnel may be NULL if another thread is in + * l2tp_session_register and has added an item to + * l2tp_v3_session_htable but hasn't yet added the + * session to its tunnel's session_list. + */ + struct l2tp_tunnel *tunnel = READ_ONCE(session->tunnel); + + if (tunnel && tunnel->sock == sk && refcount_inc_not_zero(&session->ref_count)) { rcu_read_unlock_bh(); return session; @@ -507,6 +514,7 @@ int l2tp_session_register(struct l2tp_session *session, } l2tp_tunnel_inc_refcount(tunnel); + WRITE_ONCE(session->tunnel, tunnel); list_add(&session->list, &tunnel->session_list); if (tunnel->version == L2TP_HDR_VER_3) { @@ -822,7 +830,8 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, if (!session->lns_mode && !session->send_seq) { trace_session_seqnum_lns_enable(session); session->send_seq = 1; - l2tp_session_set_header_len(session, tunnel->version); + l2tp_session_set_header_len(session, tunnel->version, + tunnel->encap); } } else { /* No sequence numbers. @@ -843,7 +852,8 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, if (!session->lns_mode && session->send_seq) { trace_session_seqnum_lns_disable(session); session->send_seq = 0; - l2tp_session_set_header_len(session, tunnel->version); + l2tp_session_set_header_len(session, tunnel->version, + tunnel->encap); } else if (session->send_seq) { pr_debug_ratelimited("%s: recv data has no seq numbers when required. Discarding.\n", session->name); @@ -1648,7 +1658,8 @@ static void l2tp_session_del_work(struct work_struct *work) /* We come here whenever a session's send_seq, cookie_len or * l2specific_type parameters are set. */ -void l2tp_session_set_header_len(struct l2tp_session *session, int version) +void l2tp_session_set_header_len(struct l2tp_session *session, int version, + enum l2tp_encap_type encap) { if (version == L2TP_HDR_VER_2) { session->hdr_len = 6; @@ -1657,7 +1668,7 @@ void l2tp_session_set_header_len(struct l2tp_session *session, int version) } else { session->hdr_len = 4 + session->cookie_len; session->hdr_len += l2tp_get_l2specific_len(session); - if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP) + if (encap == L2TP_ENCAPTYPE_UDP) session->hdr_len += 4; } } @@ -1671,7 +1682,6 @@ struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunn session = kzalloc(sizeof(*session) + priv_size, GFP_KERNEL); if (session) { session->magic = L2TP_SESSION_MAGIC; - session->tunnel = tunnel; session->session_id = session_id; session->peer_session_id = peer_session_id; @@ -1710,7 +1720,7 @@ struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunn memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len); } - l2tp_session_set_header_len(session, tunnel->version); + l2tp_session_set_header_len(session, tunnel->version, tunnel->encap); refcount_set(&session->ref_count, 1); diff --git a/net/l2tp/l2tp_core.h b/net/l2tp/l2tp_core.h index 58d3977870de..c907687705b9 100644 --- a/net/l2tp/l2tp_core.h +++ b/net/l2tp/l2tp_core.h @@ -258,7 +258,8 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb); /* Transmit path helpers for sending packets over the tunnel socket. */ -void l2tp_session_set_header_len(struct l2tp_session *session, int version); +void l2tp_session_set_header_len(struct l2tp_session *session, int version, + enum l2tp_encap_type encap); int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb); /* Pseudowire management. diff --git a/net/l2tp/l2tp_netlink.c b/net/l2tp/l2tp_netlink.c index d105030520f9..fc43ecbd128c 100644 --- a/net/l2tp/l2tp_netlink.c +++ b/net/l2tp/l2tp_netlink.c @@ -692,8 +692,10 @@ static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *inf session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]); if (info->attrs[L2TP_ATTR_SEND_SEQ]) { + struct l2tp_tunnel *tunnel = session->tunnel; + session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]); - l2tp_session_set_header_len(session, session->tunnel->version); + l2tp_session_set_header_len(session, tunnel->version, tunnel->encap); } if (info->attrs[L2TP_ATTR_LNS_MODE]) diff --git a/net/l2tp/l2tp_ppp.c b/net/l2tp/l2tp_ppp.c index 12a0a7162870..1b79a36d5756 100644 --- a/net/l2tp/l2tp_ppp.c +++ b/net/l2tp/l2tp_ppp.c @@ -1189,7 +1189,8 @@ static int pppol2tp_session_setsockopt(struct sock *sk, po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ : PPPOL2TP_L2TP_HDR_SIZE_NOSEQ; } - l2tp_session_set_header_len(session, session->tunnel->version); + l2tp_session_set_header_len(session, session->tunnel->version, + session->tunnel->encap); break; case PPPOL2TP_SO_LNSMODE: From patchwork Mon Jul 29 15:38:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13745246 X-Patchwork-Delegate: kuba@kernel.org Received: from mail.katalix.com (mail.katalix.com [3.9.82.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 702DA15B11F for ; Mon, 29 Jul 2024 15:38:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.9.82.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267500; cv=none; b=uZEa0k1EscHUj3GMpyEgspvWMbUFM7q9gd2rvOWlMMCDCj7LjP4wz141w38U+dlcOM+XFaXrc3VDTQ/Fe1ZUNB0QGVMDcy346muHwPkTa8a8OiPElnxzQrezp042ZaEGNKUDRJZQu1zqpahHSq1HDP+UYwG/0UgwVRXBuCkiI0c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267500; c=relaxed/simple; bh=qQBFnd27KeNrXqfpSNskAfFAQuKll9xfPCeJvgeNMSA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=X/Qt4oVnRyTC6R8Ra6EBa/H1U2sVwhEsF+pVVMEBCfQMhmTRHEStYnjkZsFqgQ9ds5z3EWOyBnikKLgajwDZdEj/g7paOIXC4HM3JbzHsgKppbAtmzj0JUTVRXoIZgmP67GwYghfj3TuQKpvDLn+DdS62BgV2lpA/uhw7xiO9Jo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com; spf=pass smtp.mailfrom=katalix.com; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b=eQiOFtRQ; arc=none smtp.client-ip=3.9.82.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=katalix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b="eQiOFtRQ" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:6c24:bf58:f1fe:91c1]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 79BAB7DCFC; Mon, 29 Jul 2024 16:38:16 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1722267496; bh=qQBFnd27KeNrXqfpSNskAfFAQuKll9xfPCeJvgeNMSA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:From; z=From:=20James=20Chapman=20|To:=20netdev@vge r.kernel.org|Cc:=20davem@davemloft.net,=0D=0A=09edumazet@google.co m,=0D=0A=09kuba@kernel.org,=0D=0A=09pabeni@redhat.com,=0D=0A=09dsa hern@kernel.org,=0D=0A=09tparkin@katalix.com|Subject:=20[PATCH=20n et-next=2012/15]=20l2tp:=20use=20rcu=20list=20add/del=20when=20upd ating=20lists|Date:=20Mon,=2029=20Jul=202024=2016:38:11=20+0100|Me ssage-Id:=20<4d4bd391f8895d46339691803bc96ad64d84723e.1722265212.g it.jchapman@katalix.com>|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=eQiOFtRQAuv7bd2Po090sENyXNIjFqEfqRZTg55Kt6S7B0nl/lb1OvKHTNtR5I0I/ S1bviLKI4ovriSSpc0KvIXPLma76bhigM7HEeL0i1Isykx0f0UVVkn+4jaPTPMykoP Ly9wK2OuUtuy4Myd8DnAiSpj0UL26VBXHD4gVBCE/5j+Xq+9xRr53KgDqolXYUk/DQ gOf96t6frdq8gh1CzCOjHyyQRD9cB5TmaPHNKIDbX88UMCenprfjD39xHgMex2zt3P UGs5XrjY2sZeMxZG61TgmGPboiO+1mSgTtRR0XuCX4QKSIZRkjWPYrS4BkpgzsfGy3 sNZmZT6HyOAfQ== From: James Chapman To: netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, tparkin@katalix.com Subject: [PATCH net-next 12/15] l2tp: use rcu list add/del when updating lists Date: Mon, 29 Jul 2024 16:38:11 +0100 Message-Id: <4d4bd391f8895d46339691803bc96ad64d84723e.1722265212.git.jchapman@katalix.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org l2tp_v3_session_htable and tunnel->session_list are read by lockless getters using RCU. Use rcu list variants when adding or removing list items. Signed-off-by: James Chapman Signed-off-by: Tom Parkin --- net/l2tp/l2tp_core.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c index cbf117683fab..cd9b157e8b4d 100644 --- a/net/l2tp/l2tp_core.c +++ b/net/l2tp/l2tp_core.c @@ -419,12 +419,12 @@ static int l2tp_session_collision_add(struct l2tp_net *pn, /* If existing session isn't already in the session hlist, add it. */ if (!hash_hashed(&session2->hlist)) - hash_add(pn->l2tp_v3_session_htable, &session2->hlist, - session2->hlist_key); + hash_add_rcu(pn->l2tp_v3_session_htable, &session2->hlist, + session2->hlist_key); /* Add new session to the hlist and collision list */ - hash_add(pn->l2tp_v3_session_htable, &session1->hlist, - session1->hlist_key); + hash_add_rcu(pn->l2tp_v3_session_htable, &session1->hlist, + session1->hlist_key); refcount_inc(&clist->ref_count); l2tp_session_coll_list_add(clist, session1); @@ -440,7 +440,7 @@ static void l2tp_session_collision_del(struct l2tp_net *pn, lockdep_assert_held(&pn->l2tp_session_idr_lock); - hash_del(&session->hlist); + hash_del_rcu(&session->hlist); if (clist) { /* Remove session from its collision list. If there @@ -515,7 +515,7 @@ int l2tp_session_register(struct l2tp_session *session, l2tp_tunnel_inc_refcount(tunnel); WRITE_ONCE(session->tunnel, tunnel); - list_add(&session->list, &tunnel->session_list); + list_add_rcu(&session->list, &tunnel->session_list); if (tunnel->version == L2TP_HDR_VER_3) { if (!other_session) From patchwork Mon Jul 29 15:38:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13745248 X-Patchwork-Delegate: kuba@kernel.org Received: from mail.katalix.com (mail.katalix.com [3.9.82.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E2E9915B99F for ; Mon, 29 Jul 2024 15:38:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.9.82.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267500; cv=none; b=HPVQJOnL6htDQDasLTHWbIKAzXMJ5aXrNaQoW7IDDNsuXuHvylre4hXYfUejFPUFhVvGwWssdory7KuofeaIpuZn+iEoM6HH0AMCwzBlYC1ZlclgrW+5JfDuJzc5npFBJN34jtUOnGHkNcXozo/MAkJWQa2Y2z8TVDIFm+cZn0I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267500; c=relaxed/simple; bh=Lmf8AwTaDzZptf4cWriAsE/Jek3Uv0srHGOHsfMh4Ek=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=O3i6A9yAtEuWqFMd99zt341jx1muxPLhuDmzmPxzJ/5XEYGx/T69tvUFNwXg8NS0jh1Qd0tVBexXTdxAfQIRaXfHiBVxvhbhNF1/Y3xqxQUvoiLH/D8iIZraph74mb8TLo+4S/uxiByyV7O7agfuPoXwekxWJGDkxX/3pfQK4u0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com; spf=pass smtp.mailfrom=katalix.com; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b=KwaxuRlC; arc=none smtp.client-ip=3.9.82.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=katalix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b="KwaxuRlC" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:6c24:bf58:f1fe:91c1]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 981107DCFD; Mon, 29 Jul 2024 16:38:16 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1722267496; bh=Lmf8AwTaDzZptf4cWriAsE/Jek3Uv0srHGOHsfMh4Ek=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:From; z=From:=20James=20Chapman=20|To:=20netdev@vge r.kernel.org|Cc:=20davem@davemloft.net,=0D=0A=09edumazet@google.co m,=0D=0A=09kuba@kernel.org,=0D=0A=09pabeni@redhat.com,=0D=0A=09dsa hern@kernel.org,=0D=0A=09tparkin@katalix.com|Subject:=20[PATCH=20n et-next=2013/15]=20l2tp:=20add=20idr=20consistency=20check=20in=20 session_register|Date:=20Mon,=2029=20Jul=202024=2016:38:12=20+0100 |Message-Id:=20|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=KwaxuRlCnUGcFroEtjrYd83Z2UHjaiplSRS67GCY9KH2gNp8/I/whuLI+P67S8URV bGOrAHBUfarvbij8a/xDtva2mhVyLLiMRlGW/TGTHPeKO2o0i9NX4LJIzXxxkOkKfR /RRivC/6C1xKUyzkCCqF9Z9F7TIF3HPmECICaWU+Fr7qG2MBACaYak3BwZ1Y0McJN9 mjW4ROIhdsMciUZwn7NOsbI//MgVH15/F+v18up3I+0MkyW5GBes85Kek3waPVk3z2 YCpXhlWQP6WCpIZRuHzxqbXkDXvi6HDgHUj8IUeTksC85YMuvjkYcoj1Q0HIamuLiN HvZo8wZxdHf6g== From: James Chapman To: netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, tparkin@katalix.com Subject: [PATCH net-next 13/15] l2tp: add idr consistency check in session_register Date: Mon, 29 Jul 2024 16:38:12 +0100 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org l2tp_session_register uses an idr_alloc then idr_replace pattern to insert sessions into the session IDR. To catch invalid locking, add a WARN_ON_ONCE if the IDR entry is modified by another thread between alloc and replace steps. Also add comments to make expectations clear. Signed-off-by: James Chapman Signed-off-by: Tom Parkin --- net/l2tp/l2tp_core.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c index cd9b157e8b4d..fd03c17dd20c 100644 --- a/net/l2tp/l2tp_core.c +++ b/net/l2tp/l2tp_core.c @@ -474,6 +474,7 @@ int l2tp_session_register(struct l2tp_session *session, { struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); struct l2tp_session *other_session = NULL; + void *old = NULL; u32 session_key; int err; @@ -517,13 +518,19 @@ int l2tp_session_register(struct l2tp_session *session, WRITE_ONCE(session->tunnel, tunnel); list_add_rcu(&session->list, &tunnel->session_list); + /* this makes session available to lockless getters */ if (tunnel->version == L2TP_HDR_VER_3) { if (!other_session) - idr_replace(&pn->l2tp_v3_session_idr, session, session_key); + old = idr_replace(&pn->l2tp_v3_session_idr, session, session_key); } else { - idr_replace(&pn->l2tp_v2_session_idr, session, session_key); + old = idr_replace(&pn->l2tp_v2_session_idr, session, session_key); } + /* old should be NULL, unless something removed or modified + * the IDR entry after our idr_alloc_32 above (which shouldn't + * happen). + */ + WARN_ON_ONCE(old); out: spin_unlock_bh(&pn->l2tp_session_idr_lock); spin_unlock_bh(&tunnel->list_lock); From patchwork Mon Jul 29 15:38:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13745249 X-Patchwork-Delegate: kuba@kernel.org Received: from mail.katalix.com (mail.katalix.com [3.9.82.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E0F5715B999 for ; Mon, 29 Jul 2024 15:38:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.9.82.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267500; cv=none; b=n51LLUn8UQ2KvqKqOK2J8uwCeD0nB5CSR2eZLGyj4pPE3jnfp1lpmjv7wLiamrlRu9KwEfLzUCJ0J6F83C+6vgAq3eRKmPLicCclUG62+B1bbdcZHKbtq3KcC6NeSEPOdFi0v2ExpgBfEGTtfIolETRt2BEb8cPWFppTj873CII= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267500; c=relaxed/simple; bh=iwrAV0Oy+haf8BFZfT0YQGvqarR+1GJIRsKV+hO2FVY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ij+jyi9OVTuS4vkiNxp3lnxO/zANao9+ofXnXyfaxhpyedSK0g2FqgV8WnJprAdXNBnk0xdwjAWSI3Bl72OTYHnQHC42QwC2LwoCQkR6DniAdeYCNpYCD9lHWDLW1PuyFlbtY1F/HlzDREEULV1W8o95AL2XvwP+sgH23+O1vIs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com; spf=pass smtp.mailfrom=katalix.com; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b=UJEDcbQC; arc=none smtp.client-ip=3.9.82.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=katalix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b="UJEDcbQC" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:6c24:bf58:f1fe:91c1]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id B6F5F7DCFE; Mon, 29 Jul 2024 16:38:16 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1722267496; bh=iwrAV0Oy+haf8BFZfT0YQGvqarR+1GJIRsKV+hO2FVY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:From; z=From:=20James=20Chapman=20|To:=20netdev@vge r.kernel.org|Cc:=20davem@davemloft.net,=0D=0A=09edumazet@google.co m,=0D=0A=09kuba@kernel.org,=0D=0A=09pabeni@redhat.com,=0D=0A=09dsa hern@kernel.org,=0D=0A=09tparkin@katalix.com|Subject:=20[PATCH=20n et-next=2014/15]=20l2tp:=20cleanup=20eth/ppp=20pseudowire=20setup= 20code|Date:=20Mon,=2029=20Jul=202024=2016:38:13=20+0100|Message-I d:=20<89b46dd2057cefe7758af4167b0b6fc327979bf4.1722265212.git.jcha pman@katalix.com>|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=UJEDcbQCyUUxpMdNRCjXLPVGO7FaMc/4T/purvpYgS5cdUiGzVzBlycCBaIgrYRwJ Sfcceas59+VeD4OzaCAFv+HiUrAbHXdVDar/WFcdn4INipS0c4PCoR6DHP1Qfermk6 CTmen1OpezXwSoJkOXOTTG7LHRXCr5dh90jeVDlbMtRUAPG7g4LH7/m8ykZQ6nqZ01 bRgBGmxTWgZhjfBCv0wIPIVa0jVQCMq3/+VbHUF5gMv0qFQ3xtgEoNH5iqC4sjEH3A 1W0qhaVuEe0yrXaCzXqrSdHtenPUEiK1yoJ+HATQMZxJ0xVWFupNonffGUheZbJW/D mtIF7hS77WMmw== From: James Chapman To: netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, tparkin@katalix.com Subject: [PATCH net-next 14/15] l2tp: cleanup eth/ppp pseudowire setup code Date: Mon, 29 Jul 2024 16:38:13 +0100 Message-Id: <89b46dd2057cefe7758af4167b0b6fc327979bf4.1722265212.git.jchapman@katalix.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org l2tp eth/ppp pseudowire setup/cleanup uses kfree() in some error paths. Drop the refcount instead such that the session object is always freed when the refcount reaches 0. Signed-off-by: James Chapman Signed-off-by: Tom Parkin --- net/l2tp/l2tp_eth.c | 2 +- net/l2tp/l2tp_ppp.c | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/net/l2tp/l2tp_eth.c b/net/l2tp/l2tp_eth.c index 8ba00ad433c2..cc8a3ce716e9 100644 --- a/net/l2tp/l2tp_eth.c +++ b/net/l2tp/l2tp_eth.c @@ -322,7 +322,7 @@ static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel, l2tp_session_dec_refcount(session); free_netdev(dev); err_sess: - kfree(session); + l2tp_session_dec_refcount(session); err: return rc; } diff --git a/net/l2tp/l2tp_ppp.c b/net/l2tp/l2tp_ppp.c index 1b79a36d5756..90bf3a8ccab6 100644 --- a/net/l2tp/l2tp_ppp.c +++ b/net/l2tp/l2tp_ppp.c @@ -770,6 +770,8 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, goto end; } + drop_refcnt = true; + pppol2tp_session_init(session); ps = l2tp_session_priv(session); l2tp_session_inc_refcount(session); @@ -778,10 +780,10 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, error = l2tp_session_register(session, tunnel); if (error < 0) { mutex_unlock(&ps->sk_lock); - kfree(session); + l2tp_session_dec_refcount(session); goto end; } - drop_refcnt = true; + new_session = true; } @@ -875,7 +877,7 @@ static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel, return 0; err_sess: - kfree(session); + l2tp_session_dec_refcount(session); err: return error; } From patchwork Mon Jul 29 15:38:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13745251 X-Patchwork-Delegate: kuba@kernel.org Received: from mail.katalix.com (mail.katalix.com [3.9.82.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A44A415D5B8 for ; Mon, 29 Jul 2024 15:38:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.9.82.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267501; cv=none; b=fWNOmAIk0w5br17k1puCvb9smF5z1qKZTUI6eCnYrUtn2ge9bEFYetNm7AbACjUXG1KaMKUdnmVNYlOQ7kiKRRlpfia5PTBRMO6wulbOgjdvN8adpL0Tx3+yQl287GfH9DlMz85JRX6OLc37afWtPNC6vxZSsqFLxBuTIfwAC3Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722267501; c=relaxed/simple; bh=kaQKpcmpxVYLHS4c4dyDW2zYNcGS3qVbvPfXad1XE6E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aQ1RTM10uJ776WpChQAlUkoldNOQt3e+UapqlBKI2T9B7lSANT7ah4lqEduqGrlxLbriN2u35ccRLQ5xzL9Mzo3KqLVjwotqCgpm9taknMVSf2SdIFtCkf9QWnxu61yyVbhEll2ieSkTkZaAFiIG8lz3sJPWxiq3qTtwLP84niw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com; spf=pass smtp.mailfrom=katalix.com; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b=ngha7114; arc=none smtp.client-ip=3.9.82.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=katalix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=katalix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=katalix.com header.i=@katalix.com header.b="ngha7114" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:6c24:bf58:f1fe:91c1]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id D55BC7DCFF; Mon, 29 Jul 2024 16:38:16 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1722267496; bh=kaQKpcmpxVYLHS4c4dyDW2zYNcGS3qVbvPfXad1XE6E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:From; z=From:=20James=20Chapman=20|To:=20netdev@vge r.kernel.org|Cc:=20davem@davemloft.net,=0D=0A=09edumazet@google.co m,=0D=0A=09kuba@kernel.org,=0D=0A=09pabeni@redhat.com,=0D=0A=09dsa hern@kernel.org,=0D=0A=09tparkin@katalix.com|Subject:=20[PATCH=20n et-next=2015/15]=20l2tp:=20use=20pre_exit=20pernet=20hook=20to=20a void=20rcu_barrier|Date:=20Mon,=2029=20Jul=202024=2016:38:14=20+01 00|Message-Id:=20<4cde928ca968c3ec17a5ba314f9544f8475e06b6.1722265 212.git.jchapman@katalix.com>|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=ngha7114VpSuYoduJt6IBbUZFtfhl3huYlBwP3ss2dOfLWse2I+rNFH6fBeC+pKHE v9vJnE37CQ26KxScbcDfv3LSPbw2mNXOR6nIWwaMk+Ldo15WXmeCgBd4dCiAt0uGha EL5XT84lYBjVnoqNba53K7n5Tfi2jkWqXd0+Kp0WLh6r9rlITWVLHUbz1k8b15xRuY BhoU0vozmy56OtYH3KAoNuoPm7pe76mFPB5uNL3G9sZquw1Ib/uxuUqurHg43HkHQO m9u03jL+m6Ff7JCDscj4EONJIcKql8tLPrRutXxD3JpheU1nsMkYAZlJ7HQGGVfcQa GtTSIbZhinTug== From: James Chapman To: netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, tparkin@katalix.com Subject: [PATCH net-next 15/15] l2tp: use pre_exit pernet hook to avoid rcu_barrier Date: Mon, 29 Jul 2024 16:38:14 +0100 Message-Id: <4cde928ca968c3ec17a5ba314f9544f8475e06b6.1722265212.git.jchapman@katalix.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Move the work of closing all tunnels from the pernet exit hook to pre_exit since the core does rcu synchronisation between these steps and we can therefore remove rcu_barrier from l2tp code. Signed-off-by: James Chapman Signed-off-by: Tom Parkin --- net/l2tp/l2tp_core.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c index fd03c17dd20c..5d2068b6c778 100644 --- a/net/l2tp/l2tp_core.c +++ b/net/l2tp/l2tp_core.c @@ -1756,7 +1756,7 @@ static __net_init int l2tp_init_net(struct net *net) return 0; } -static __net_exit void l2tp_exit_net(struct net *net) +static __net_exit void l2tp_pre_exit_net(struct net *net) { struct l2tp_net *pn = l2tp_pernet(net); struct l2tp_tunnel *tunnel = NULL; @@ -1771,7 +1771,11 @@ static __net_exit void l2tp_exit_net(struct net *net) if (l2tp_wq) drain_workqueue(l2tp_wq); - rcu_barrier(); +} + +static __net_exit void l2tp_exit_net(struct net *net) +{ + struct l2tp_net *pn = l2tp_pernet(net); idr_destroy(&pn->l2tp_v2_session_idr); idr_destroy(&pn->l2tp_v3_session_idr); @@ -1781,6 +1785,7 @@ static __net_exit void l2tp_exit_net(struct net *net) static struct pernet_operations l2tp_net_ops = { .init = l2tp_init_net, .exit = l2tp_exit_net, + .pre_exit = l2tp_pre_exit_net, .id = &l2tp_net_id, .size = sizeof(struct l2tp_net), };