From patchwork Tue Jul 23 13:51:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13740073 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 A6AA1137C34 for ; Tue, 23 Jul 2024 13:51:51 +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=1721742713; cv=none; b=JveQMNchu/H6xubi8WwYM0pbv18LvnMgAa1UqZVdIZUZ6udndvB5JTBHqAZ54IFr8SUbLwXg5ncUgG26yqzv7MQ5oWHRtxjfYrP3A3rcDiPHgr0c3NKgx+a/rQFp3rXnuTdc4nsBOXKOi4x/rPtl9f3eBkNc055b72H4DhbQIOU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721742713; c=relaxed/simple; bh=HHt4Rs59ItJk4kyCZ0wktex/LLSJEG72yOzbJlah1tA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aZVdv3zVfobt+buPGUrXCjY21nG+ETv6rSM4WiVicunvkelcXLxwbM3fePV+wbI2wEoB1mpajHB7E0yaWRT0CYGTHMN6jj8GLbd6B6Ic5wqrrUWJ0VRoaZYuUEjyWBWviyHv4+4eYomKFgxI9Q4VkmaoiHdwumIPZGm6ORVuNHk= 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=3L6hj6/I; 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="3L6hj6/I" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:47:b279:6330:ae0d]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 18E187DBBB; Tue, 23 Jul 2024 14:51:45 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1721742705; bh=HHt4Rs59ItJk4kyCZ0wktex/LLSJEG72yOzbJlah1tA=; 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[RFC=20PAT CH=2001/15]=20l2tp:=20lookup=20tunnel=20from=20socket=20without=20 using=20sk_user_data|Date:=20Tue,=2023=20Jul=202024=2014:51:29=20+ 0100|Message-Id:=20|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=3L6hj6/I3H0ZXfo37H+vv51k3yOGRZ8xPQzc13KDq0cN/ClalPJEQ97ujb2wmz/yD 4gol+pIMYQv/RxuV1WBW/cqhfBEHmfT6pWwtWUHgpfl7UQmvfzVdhF+i9ymqX9LdF3 fpJG6zOk5aGCLeRnxhyrmN1yZp4696Tt3PnRFyUQRGbncusE+QaVDUY39+JLfXzYJa AO5zPqDcsmaXh+WI52VXZrWRiQGaXvhla1h6d4es2fnm8Cm8D48O0PnrvMfSwzbHhP DLSFtzJiezp/4uRwCG6yfs9hrlVC6yMQNhI5cA2CYsRiMYKpciSFJ/fB1CxuYfsweT pdVCsdf4uBtIw== 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: [RFC PATCH 01/15] l2tp: lookup tunnel from socket without using sk_user_data Date: Tue, 23 Jul 2024 14:51:29 +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 X-Patchwork-State: RFC 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. --- 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 Tue Jul 23 13:51:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13740071 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 A6A178F70 for ; Tue, 23 Jul 2024 13:51:51 +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=1721742713; cv=none; b=tMn7WTAteXJw1z2kicg/gTeup4OdgUNYQUDZ7K4wncmGST11vpFPOkKkF2X/68RMgo6UiBDYM+rV9aeaoXMw08pk8ic71jutLfium3q/E3ay6Ig3eFwVhJBSZeqZ363d5U0KKts8Q8LH7H9xBLOiOuAIdC4UjzDmpLmxC7hvKiU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721742713; c=relaxed/simple; bh=VkIc8hikVA7RDTmY+VUiu2J2hAERnotG+D6NR7dKZqA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=n/UnqW6hyJyB7QRkBQDKFDqDqz7ocnrXQXlXmvtT6rPHKfXG/A35bh0MwW8h5ZQPS2MdUQW3IL9y/4nQPcdgNHn1loNxp5fu35awDvJJfRBFQC9mHl7VIGSfH4kMLE3I02W891eFRapkfzY7J141NIgbSGkZhULk9u+9GNtjBqQ= 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=wHsNp/KJ; 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="wHsNp/KJ" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:47:b279:6330:ae0d]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 371F87DCB5; Tue, 23 Jul 2024 14:51:45 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1721742705; bh=VkIc8hikVA7RDTmY+VUiu2J2hAERnotG+D6NR7dKZqA=; 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[RFC=20PAT CH=2002/15]=20ipv4:=20export=20ip_flush_pending_frames|Date:=20Tue ,=2023=20Jul=202024=2014:51:30=20+0100|Message-Id:=20<3506b4e72666 2dbbd6f447be054c68e31deeaf47.1721733730.git.jchapman@katalix.com>| In-Reply-To:=20|Referen ces:=20|MIME-Version:=2 01.0; b=wHsNp/KJClLN7rK6jQ3Vrw3grB5gimNJ0YiGnWOmZv2w/VuPFK7Xlbcvp5jtFhYs5 7SteljmF8XlJB9ThygkPzMNJFhwDzywR1pwCgQ46kvWZMpadoQe2SUWonr3qCa6K3A UWKuT7k2JfEC/WOBBuFo4rIfkuWOCz8bbd7K3WGkYjDxwmur6cdPw69VGtrdiAB07i NR3Pql9rphTuezU/WZJ63Z54cvc5dJHf8IpxZUeof1pA0SQzhoW41Dc79cp1wOKjzW 3CrMN24xMFUjbkh1MpcF1MLcmzv4lHYrxJVNaTgQ1WpUepa9vWiNQnfhhCRQnM7Hyv 84FZhU4YZD44w== 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: [RFC PATCH 02/15] ipv4: export ip_flush_pending_frames Date: Tue, 23 Jul 2024 14:51:30 +0100 Message-Id: <3506b4e726662dbbd6f447be054c68e31deeaf47.1721733730.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 X-Patchwork-State: RFC To avoid protocol modules implementing their own, export ip_flush_pending_frames. --- 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 Tue Jul 23 13:51:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13740072 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 A6ADC137C35 for ; Tue, 23 Jul 2024 13:51:51 +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=1721742713; cv=none; b=JDUdN3ZHcg3mOyUj007Cxsro9IsDyypPC37MHvmcq6bCkP/E9Ht2YlrHQvLpSi6BKScJGuSitEivbLfiZMP7Hu0aLvWlbzHt9kvajPDme40vSVEofIVtU50CD/GPan+iREh2lsc539jp/q1Ey6N4n/1qj3At6ihtOQ8x1h6uuGI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721742713; c=relaxed/simple; bh=VE7yj3tP2lPXlfAiG1eIKhYkzT06WlSIwoLknWtjrJE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tYUmYHYTfethYNh3C8MF4nQvlOFqhQ55H/xT2xdrJZA7cTwK5Ti+XMNoH6Y4v3Z03mIaBjry0j1jEH4dh1NLpFQQ0gz9T9oHqEoGOsBBRKt7KnmTMT5FMFL4jRw6OA61zmbFJSDIxe44vpwBoCHlaPOx6rSimoMoo9vB1wZz3XY= 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=vh7Yy0WW; 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="vh7Yy0WW" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:47:b279:6330:ae0d]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 5F5BF7DCB8; Tue, 23 Jul 2024 14:51:45 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1721742705; bh=VE7yj3tP2lPXlfAiG1eIKhYkzT06WlSIwoLknWtjrJE=; 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[RFC=20PAT CH=2003/15]=20l2tp:=20have=20l2tp_ip_destroy_sock=20use=20ip_flush _pending_frames|Date:=20Tue,=2023=20Jul=202024=2014:51:31=20+0100| Message-Id:=20<25737cb5fef11f8da3577589872944001913982c.1721733730 .git.jchapman@katalix.com>|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=vh7Yy0WWnrO/6SlvpAFGtHbc+bT9AQAvvN8EuSRrRAR29lwA2nSk+cz5JbauKG9Gg nZCrWGh2OHifBbC7t/qxOuPTz8XxPuMbjALwQYsUT1paGJ5TpsF4WGlcNitVBdh8XF jA+ohJNCgR/WfW7gWX0atIDPJU8RoBar5k9t7mJ8NOl8UrgOcJF2Y0090/sm7KNf5F xkj1Se5yILwbIJlXWK+PwqSdUkONl9jj/yOv3wG5C7zcxtBppqcH8AxtAfMbVehbPm IaSFRaEfNZsPbhnIR+FIDn8TekE50kTibLRroCnQap8utdb3BCmXVpTIIueCT3vqdT xQqKcFm0oxP1A== 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: [RFC PATCH 03/15] l2tp: have l2tp_ip_destroy_sock use ip_flush_pending_frames Date: Tue, 23 Jul 2024 14:51:31 +0100 Message-Id: <25737cb5fef11f8da3577589872944001913982c.1721733730.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 X-Patchwork-State: RFC Use the recently exported ip_flush_pending_frames instead of a free-coded version and lock the socket while we call it. --- 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 Tue Jul 23 13:51:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13740075 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 A6B2315099C for ; Tue, 23 Jul 2024 13:51:51 +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=1721742714; cv=none; b=GYi+avdW9ooPaWzBdomzkDZR78+q+nkEXaZXjfOkWNtxudXrHJzCpJlcv4aW9Qk8pT4of18ZxJfOYSwP4ZzoXIV+xz8W5fKFn48y0NeovgSw8XdyRbNgARUaJJ+2tWRlLk39U7y9W5r22JcZbzo4rfgeFjBPesRIkeGa5mFrj1Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721742714; c=relaxed/simple; bh=H5hMLIDnwTUPW/Z8Qsq/w7gocs6sU57/wJglXCqp0e4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Pd7cx8YejwHkrdW96AUzmD5sEtDfDlabKqpDE1GFYY1Rz4DqUlQO17/cg0FCPze3U1lwWOm1wZ+F9nugnZn/k15btGxcAhly3JtRdZug0kS2NMZhlkm4AeuQQXc1z1/0luMv7SuqqgD9aYCUNjv8kAECok5HBFfdNd48W2jBG/c= 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=FusVWPXZ; 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="FusVWPXZ" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:47:b279:6330:ae0d]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 87B667DCE7; Tue, 23 Jul 2024 14:51:45 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1721742705; bh=H5hMLIDnwTUPW/Z8Qsq/w7gocs6sU57/wJglXCqp0e4=; 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[RFC=20PAT CH=2004/15]=20l2tp:=20don't=20use=20tunnel=20socket=20sk_user_data =20in=20ppp=20procfs=20output|Date:=20Tue,=2023=20Jul=202024=2014: 51:32=20+0100|Message-Id:=20<88d21632787dcbcb2541cbfd090ab3f9b997b 807.1721733730.git.jchapman@katalix.com>|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=FusVWPXZilVjNdZ9qF637T7+f+nue+aNeVYe+mYQ5XP+pLqYSv4iy42ymWclWMaBz 3gmBLZdrIjKPBJzXQEzTRa1/YY+Lvw6h7tJoirsm1ubBZRjZES5HZIEsZTVMR152a7 v3rrAVoGGT7gOYXq2N3V7SdzhMXkknPPc8aHT+OYZmAzs/vdNZJTl34VX+vum0z1Dx haOG/8o/63wx9FsC9xE/yw4qYovs2sdVWJTTUEANA3PTL+FQYoV2QkPu9sb0vW6AQ3 WYu+G2++f/uJ3n87hgtS6790M9gXh8fV668yWGiDsq/jOeM8oENC0li2uYT3jpLwes bRY69asDV9FBw== 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: [RFC PATCH 04/15] l2tp: don't use tunnel socket sk_user_data in ppp procfs output Date: Tue, 23 Jul 2024 14:51:32 +0100 Message-Id: <88d21632787dcbcb2541cbfd090ab3f9b997b807.1721733730.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 X-Patchwork-State: RFC 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. --- 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 Tue Jul 23 13:51:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13740077 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 BAC09153808 for ; Tue, 23 Jul 2024 13:51:53 +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=1721742715; cv=none; b=hZ/hFK9ZBstDI7IiJybDlzG6R7oH1sQL2w/di9UJzAQOyEc64OWGDC89VLyyH4EcSYy/j/ZNOxDIPADVd/ts5N2HA+E3LxzcvdY1k/UIF5MdOt2zMFJBp5+PzMkRnCHovuH3gr87Md9FxR4+MLZeZ2iHx+nwfFZkcAPhc1Ll+nc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721742715; c=relaxed/simple; bh=qQ7Sc1IN0y1n+yXdxsGWCbcnjUEbXAMYGZJtVRdx5Ig=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QPuc9s24dt+4NfrHUi+GIllR3xa8DuKkTjfYtXCE13dZE8O/cQyz1rw4i6OKLLG+FKHUT7/ZgafwJWm5hJoQqRFwbqVGrk1xsBkZbAUAji/Cx/Ba7HsNrVQVIXazDouunHeYsBHkEbgV6Q3xZjLnYpokDpweoiBEYigCP1FHx6I= 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=UOKv+LO3; 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="UOKv+LO3" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:47:b279:6330:ae0d]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id B03717DCEC; Tue, 23 Jul 2024 14:51:45 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1721742705; bh=qQ7Sc1IN0y1n+yXdxsGWCbcnjUEbXAMYGZJtVRdx5Ig=; 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[RFC=20PAT CH=2005/15]=20l2tp:=20don't=20set=20sk_user_data=20in=20tunnel=20s ocket|Date:=20Tue,=2023=20Jul=202024=2014:51:33=20+0100|Message-Id :=20<0b51d3a4611109b3749c1acb4f09ffa9fb45bc45.1721733730.git.jchap man@katalix.com>|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=UOKv+LO3fG7vLrnmQ8uoi4Af7c4MmZgY7BT3zl+wFWT/9Yjxh+QYUtTUO5Ue8Oosk ZyX4XM/q4w50/GHMeTCYILtoIx9bUklG5tfzmKkxKGzWvmtrI7S/HBfHI+NqKLVk7n /jfsBXrOwwmrB8cPvjeBrhr+H3SKcTXd4e04apZ3azaJekZ79KSkMYt+40aF+blFVb 5SGd/xyh7DZAjZnR7IDrArsJ5/PYtWMnahxPKNKOXYeiuz18Lo8dwIP2LglmnF2wjq MM5/6CddOn7vNz3lUCfsci4OSPpjyaUh9CQ4N+fNt2As4vKjDAiIYqtsvPzrHkp77a leJODvYyA43kA== 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: [RFC PATCH 05/15] l2tp: don't set sk_user_data in tunnel socket Date: Tue, 23 Jul 2024 14:51:33 +0100 Message-Id: <0b51d3a4611109b3749c1acb4f09ffa9fb45bc45.1721733730.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 X-Patchwork-State: RFC 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. --- 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 Tue Jul 23 13:51:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13740076 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 B89E21509BF for ; Tue, 23 Jul 2024 13:51:53 +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=1721742715; cv=none; b=Id7zNVXYnL3cVxulgVZbRliwB21gfXHBr+z5mec3nao7SMD5XClmlRvJsY84i4wf9AwtZR7csHHeSawyMBTL8V10aAm4CXw68cjylgjK4lfaWbszjp/LOfLQwS6lD+23NkztqjbY+j+G6hZQG2vUmh064J5sJq52qOzBxAO+1IA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721742715; c=relaxed/simple; bh=Zl3XWrPm9nzsOmQ3jqkZ4f5PG9RSmVQ2p3Qpf6hXkK0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EDqHCqz6Px8fxsT3TCP/d4uaMpUpwMHC72xNl022/+JAt3vMQwyTtNacc31SLuNMkVRaWhBpDy1GltksLLx9Z7wXds20gMm/oWPtJPe7py85Gmolmq2IMHYWZDZidMPoQ+Ekda/mLXjHC2TJA8qUTHN2eGBnvr8R7WxV1CyLp50= 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=WKg2h/Ow; 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="WKg2h/Ow" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:47:b279:6330:ae0d]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id CE9B87DCED; Tue, 23 Jul 2024 14:51:45 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1721742705; bh=Zl3XWrPm9nzsOmQ3jqkZ4f5PG9RSmVQ2p3Qpf6hXkK0=; 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[RFC=20PAT CH=2006/15]=20l2tp:=20remove=20unused=20tunnel=20magic=20field|Dat e:=20Tue,=2023=20Jul=202024=2014:51:34=20+0100|Message-Id:=20<7736 9a8db320f2925c6c15dc8e7afda701b11fcc.1721733730.git.jchapman@katal ix.com>|In-Reply-To:=20 |References:=20|MIME-Ve rsion:=201.0; b=WKg2h/OwizIQhv3Vu4s3cgWL1U8UDhdscswNZpNrdLBtOEiDjWThvGk69riAy22a2 nnB1ZzUpmdW/u7iZvKzQ8TGVwVsESDEL4R57cInpcOgwZKz4+wlbmUxosCyXiquA07 7+O+UOgd3YwrH13PQ7SqsfOKJhQxjZxDrBzoaDTE5/9/96ZcljUSBQqrt0MtGPz/WO gSh+4FhI/Fp6Kv3R1lYwCLYGPlBCUaFmCD0LviYYCd0geMxbmmrvDsGDlgVSyGyHdN iV8ahqG296JZpwkR3IApcT8/yOiqMdVkLPqaSvc0rHN7c5lEjU+podek2j3ZMtlzjC BnMimeslfO5Ew== 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: [RFC PATCH 06/15] l2tp: remove unused tunnel magic field Date: Tue, 23 Jul 2024 14:51:34 +0100 Message-Id: <77369a8db320f2925c6c15dc8e7afda701b11fcc.1721733730.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 X-Patchwork-State: RFC 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. --- 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 Tue Jul 23 13:51:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13740080 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 ED67A153820 for ; Tue, 23 Jul 2024 13:51:53 +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=1721742715; cv=none; b=dpBPIjboT5a3YVd6rivDHhllyxbGdmX+O6QVT9Vq7I/U2WIH+6NmzvzHkPzKOpi5omAKE2f1KSqtRelD9KroC8L5ieywWErY8rHL+WwvoOWSFaaFhI1ylPUhp755i85Gqk2oHs1kcHkXzrUMZET7MgPX5g2cDqH/iwtDFff0TYM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721742715; c=relaxed/simple; bh=yOCNgNSxHlHoWueXtsVTH9lsj97a3+g38q8BwV9sQOQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=raAnhYzVi9ZQhkTtZCKIN18fK5IS8moLrkMMk/qWRVLNIChIYwlVZiIXBzZG5956ARQ4vaZqefTTPL2VZZW1bh2s8dCYn7wYM2OK5sar/Y38W7Z/N2e0uwrA0DbPvE69KjBGjpb/+Sjl8/3peru8TqdSZ+4euJ+Gsho2OEkLMxY= 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=U5cJgpTL; 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="U5cJgpTL" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:47:b279:6330:ae0d]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id ECE427DCEE; Tue, 23 Jul 2024 14:51:45 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1721742706; bh=yOCNgNSxHlHoWueXtsVTH9lsj97a3+g38q8BwV9sQOQ=; 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[RFC=20PAT CH=2007/15]=20l2tp:=20simplify=20tunnel=20and=20socket=20cleanup|D ate:=20Tue,=2023=20Jul=202024=2014:51:35=20+0100|Message-Id:=20|In-Reply-To:=20|References:=20|MIME- Version:=201.0; b=U5cJgpTLJRP5kHE8Vo1AeY4LozClK9njOOSIhKEvCHtxdekH66nkpmD3oRIRctUgO V8rFyjjOUbGrhXQQqaok3opOjSJJ+z0IAInCbj5CbJWYn+AmB+S8psnSwNv37iZKiA raSBueVzzW74Z/H45LLmqPuISJ74I0LuUSjt1E5l85s47HLtZ4ZMuMRKRN9jttS6Br BwrPV3bPY1FcEpThdkWSbR6RldTBse39ntqzZFRF17C32VteaKM81OOEvplT9AVzi2 6UGdrOXca3aDtRO7+eya/JofitcTZeRKC0pw48BlNPBHJBfFDWeHMqSC3MwT0Deowt GksEzkW3V1kyA== 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: [RFC PATCH 07/15] l2tp: simplify tunnel and socket cleanup Date: Tue, 23 Jul 2024 14:51:35 +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 X-Patchwork-State: RFC 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. --- 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 Tue Jul 23 13:51:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13740078 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 BE19615380A for ; Tue, 23 Jul 2024 13:51:53 +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=1721742715; cv=none; b=MBPjJyIyvETM5TTGMISfqn7MIMBC8wGQu21j2JF2gQmhcvJy+cNcIQeH3r2kxyHYK3PG6eC46OzmMf4jXdN4nidDidZDGYniDZq3OEDhZ9RbUBxn0bds7r7wijRchpgl+dGzQOTsUTsPn82FxrXp6oMAjy8BTDnKcMTaDekmVfQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721742715; c=relaxed/simple; bh=T6hLeXX7Sgkm5cr8ehfdSKVlUfoZynlwUDoVtbhDoqc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QkwiqX1m/bSC0kgchn+iLrr/3PqOt44qNGnojc+hJJUw5aVLr81cm8u8JwA8RSX6KE9O+z+GEv0mwKsHzYG774O84be4GvcjCDc9/TsLTgwkjmVDvo6w5OO9Vr0aCvAYBVvD62FwP4Y/MDZSQXgozN6ezGF/85/9M990irz6zd8= 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=FEGWyXTh; 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="FEGWyXTh" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:47:b279:6330:ae0d]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 1754B7DCEF; Tue, 23 Jul 2024 14:51:46 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1721742706; bh=T6hLeXX7Sgkm5cr8ehfdSKVlUfoZynlwUDoVtbhDoqc=; 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[RFC=20PAT CH=2008/15]=20l2tp:=20delete=20sessions=20using=20work=20queue|Dat e:=20Tue,=2023=20Jul=202024=2014:51:36=20+0100|Message-Id:=20<3017 b6e6df2c9591eb2e487b6e23fe509411f1c0.1721733730.git.jchapman@katal ix.com>|In-Reply-To:=20 |References:=20|MIME-Ve rsion:=201.0; b=FEGWyXThopLIeRfiLLp8s9gau+Ib+njkrMYbCf1ZRfErHLwUO6HouZPbyhXxsjZig hBz6gZuUO0QKq2iYVv/3DlBIp6fh3ed2uWIE6ZQm39WpOqIqiDJy8ynsUnGO0dAT+t nOjRjbhH/IOY3naJQtSSF8RUF2XDAyrnRFdsHtbQWvMYOmDRSYIXdcJmY1cXTxKQ8p IzSCfIjTYaHsXPeUehGSc38mhBycYSRbI8PyReYmyiSeOljiURMjwRLrZy03rvextg FN6REOT2MJzTHphw7QSkej1dTVNVxdj/7u/n5ScrbWST5DPh4e5B+Qjiam3V3JxW6i GdgNXczFQWwpQ== 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: [RFC PATCH 08/15] l2tp: delete sessions using work queue Date: Tue, 23 Jul 2024 14:51:36 +0100 Message-Id: <3017b6e6df2c9591eb2e487b6e23fe509411f1c0.1721733730.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 X-Patchwork-State: RFC 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. --- 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 Tue Jul 23 13:51:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13740079 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 4224E15383C for ; Tue, 23 Jul 2024 13:51:54 +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=1721742715; cv=none; b=gcnyWRM9Whf+nhaDSnBKOTqBo6jyQZD0FOh8bpqRsu3Qk3JsXiR28qBBmsh7UghO/IkVkGpf5fQrlYHLqprOuGXc/EN9UyR19ipisM5bktN/7xNr8FMrX5IMDe109R3loe+MSX7/apgKje7tubzCfI97h3qY6ZA9An6TkHNLIdg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721742715; c=relaxed/simple; bh=NkYocc87x4moEF9BT1w93mvfLi/41StZVb45qZQ7PAs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rKIS5bzGy7lV+wzOpnYK2HbEReGp6zBBaHw4zvYwmM67SzaMgdrrQ8yRzBGTiVyQsBpDXklzxccg2Mr6ud6//3J+4+fQ5xlwrBMi4bhUQ6BwXcpd5cAcBg1ugNibK5C1iYVP2AqEdz7hTTO/AhgCm579ddDF67x9543ogzD26V0= 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=AbVi6VO/; 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="AbVi6VO/" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:47:b279:6330:ae0d]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 354EC7DCF0; Tue, 23 Jul 2024 14:51:46 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1721742706; bh=NkYocc87x4moEF9BT1w93mvfLi/41StZVb45qZQ7PAs=; 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[RFC=20PAT CH=2009/15]=20l2tp:=20free=20sessions=20using=20rcu|Date:=20Tue,=2 023=20Jul=202024=2014:51:37=20+0100|Message-Id:=20<2cdcbbbbd84ce78 d3170750b8c8bf939353d20c1.1721733730.git.jchapman@katalix.com>|In- Reply-To:=20|References :=20|MIME-Version:=201. 0; b=AbVi6VO/YYf/qCjNk5AT+ULsNWqTJ2rwZWoaabir32D1u9Eqayb3mwggpX+lTqVLv hzTwaxLzXHoX/u/9r/95MTVXuEP4sISetrkmFN+kCHmzc3uWbV13a7pNnMZ8d9PLbY t5Kuaokjo8CFFVX+CsoQDgwlV+bCbVYbar9nHbZEpvriJzu8Me6oiPqBojJfWEqZEu +qw0hkXH+NgyFGBpHxPrC5vA7K68IlN1hKIYSO2Da/JywtB8xsAnb6lznOeyiEYiah K4jJK6w9otVAge/rS0aHmsvhfgMDlYvZlqEMIYnfmLNgS3zwAyJ3VVOxV/8n8emcYI 5cbRfMsF78okQ== 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: [RFC PATCH 09/15] l2tp: free sessions using rcu Date: Tue, 23 Jul 2024 14:51:37 +0100 Message-Id: <2cdcbbbbd84ce78d3170750b8c8bf939353d20c1.1721733730.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 X-Patchwork-State: RFC 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. --- 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 Tue Jul 23 13:51:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13740082 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 421DE15383B for ; Tue, 23 Jul 2024 13:51:54 +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=1721742716; cv=none; b=hSPlGiz49CTTlQKxlAGYcklJD7/IdlfFUK0lQmnl8szsBUj4pJFCzTk3GD2QR6JsA1P4h1ebY648Dc9sOwJ4EvwoWV9/FxiyuEJQK/ahx/OVOztqzVrEijGKuqg0BD79CR661y+uE3WnM9U/IrCacPRzQgVBRV5sZmnhlYLiYXU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721742716; c=relaxed/simple; bh=x97VvjHyADsah7+ORbYc9kTAEvcOwluqq+Jss+s6pxg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RIXYPjZlsypqULn27pymz9YR1whi985Fc0ofGMDOPORjrlbsCKAz+tHzPMkM06/Fvx0mVBeT1SJVOAqRwPHzBCdmDMuuK9IMj7txL+RtJ3/NxrsWJyU8gODKuaPSEOM24V+unux2T9uU2lhwL1sr/953hmtHAeYU7qEWn4ym3V0= 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=eMQxCsmg; 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="eMQxCsmg" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:47:b279:6330:ae0d]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 530517DCF1; Tue, 23 Jul 2024 14:51:46 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1721742706; bh=x97VvjHyADsah7+ORbYc9kTAEvcOwluqq+Jss+s6pxg=; 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[RFC=20PAT CH=2010/15]=20l2tp:=20refactor=20ppp=20socket/session=20relationsh ip|Date:=20Tue,=2023=20Jul=202024=2014:51:38=20+0100|Message-Id:=2 0<50d03c0ac3a67542fa876d53812b3d96b62e3594.1721733730.git.jchapman @katalix.com>|In-Reply-To:=20|References:=20|M IME-Version:=201.0; b=eMQxCsmgIe5z44ryTg3yFRrVvbnmbwPbSptpkaSzTBrLVRX1N02Kef8y4nQru08DX bLmhX1/wW5ULOiGsg+xa3LMvYf0CoHW4WdDsCDZX9uBnNe1e23wzTl2qsl05SWbdOZ Gxdqh+lkxDz65cI9POW205xXiPo6CiB/hjLpJT0P09VdfICSQuVLkBZF8UFBVNgI2V kgDRDMa1fisv9GLdGuWz9cbV9SkgR+rg3qmvEZJqjwy6/qpBEqZmawUOOMioDDl8wA uko7DyZEW+HHntDzQXwQXK5MlSniEqV7W+5jPicws1oL6sjO/dzxjVCKBivSKUf8K/ Eio57yJSQS0IA== 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: [RFC PATCH 10/15] l2tp: refactor ppp socket/session relationship Date: Tue, 23 Jul 2024 14:51:38 +0100 Message-Id: <50d03c0ac3a67542fa876d53812b3d96b62e3594.1721733730.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 X-Patchwork-State: RFC 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. --- 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 Tue Jul 23 13:51:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13740086 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 8F5DD153BF7 for ; Tue, 23 Jul 2024 13:51:54 +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=1721742717; cv=none; b=aBJ6EjbcfDflw4kxwcK7c1pzNyRcMn8lATwg7jh2340UF4mn0fKghUYTZkJFOmlz1pw1J9uu3xf0rL1wOzXwZ3qbxQ2xc5/lhR2UhnYzFOPWbUxzBCjVtIDWuRg4PF6TrMsQ4+foKVfmdkmZsNYBhc4c6Eh1qXif87E4Qj+7lLE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721742717; c=relaxed/simple; bh=lxUuJ0S6NAgXlvnz7Y+Q4GPQgZCPO824yfCU1XjCb6A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EfKndROey7gZ8MVySxwcyIblfJFTr//L/4UDYtESE+RbC4RR/Pps4Ypy5uJnHnZRt9gIbVk3Zs07EwCKWFMBVKeDY+lOPODRZYr0gLMCEO4Iz9JHKqWgnUgbAK0u4mBcNwJra2lC69ikEKiGWcIRasUJz9Kl0NSwSvZRPfSPcRQ= 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=n0EPFUyD; 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="n0EPFUyD" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:47:b279:6330:ae0d]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 719AC7DCF2; Tue, 23 Jul 2024 14:51:46 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1721742706; bh=lxUuJ0S6NAgXlvnz7Y+Q4GPQgZCPO824yfCU1XjCb6A=; 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[RFC=20PAT CH=2011/15]=20l2tp:=20prevent=20possible=20tunnel=20refcount=20und erflow|Date:=20Tue,=2023=20Jul=202024=2014:51:39=20+0100|Message-I d:=20<233548efc4b23193fd62ebf83685766ce7329518.1721733730.git.jcha pman@katalix.com>|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=n0EPFUyD/YxKpDjUJE4jLdLph6gAZonWzpcPG5nGqhfTmeoX7Bw+KHyPqUB6vTBtY JJLDgjJCjwR2XCHSE5SGYMGGfmt6omRlUjZUmqnnG4cwLmtMtOIgBrRqif3z2KVh6h 7/YPtGtq+mt9fFZ92CdGG3E9Hiini9/WzBiqkO19Fcl8MCVdT9scKwB3td/fgC7oKf fpjriOubSrqsL04AZo7fQaFliNW9UvndVQzMCovVHrMpbaK3hdkbsIXoJB+Mhel+MK Duo+ocQ7BjH82CTuQnmzsnRHxh+pLhVMOt1yMmUPttHtxyCiu9bqm3jPQEa6F+1Wfy sq1yZDtx7BnPA== 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: [RFC PATCH 11/15] l2tp: prevent possible tunnel refcount underflow Date: Tue, 23 Jul 2024 14:51:39 +0100 Message-Id: <233548efc4b23193fd62ebf83685766ce7329518.1721733730.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 X-Patchwork-State: RFC 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. --- 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 Tue Jul 23 13:51:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13740081 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 8DB3A153BF6 for ; Tue, 23 Jul 2024 13:51:54 +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=1721742716; cv=none; b=QnvRjNG+l/LWG4Fw4bfGxhATW/pSo4K11+1KvW7HpIbcdbz02pzZVxORGezMiabD2cd4GIedFlcaxf/MpmwUFnj+zenVANmhh588FdgrzTsk6mINCQE+aTbIQlTjGbkT0GE8rXPrCIdqoI6RCD0Dc8MOlaFKTJpa8KzhvNYGWrc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721742716; c=relaxed/simple; bh=1MnD3B6Nfn3MrslJWpGjEcsjGMYS+LyrcEuyjGnNM5w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qk++cspkghhekt4zSnm4YUem77xZkY5OjMnbSsTj9K/+6p+qXexyKqKQQnA3uFKvhFuSI5sIhsCuKDADA7nfErYBpzhnP/VIZiBH/shQwmwj0jxxSh6PZyZUk/qNZNC0r6uUyIx93q974K9gPMkPch0RiyHkv2yApRzrl+Hse1E= 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=yxlO76ec; 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="yxlO76ec" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:47:b279:6330:ae0d]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id 910477DCF3; Tue, 23 Jul 2024 14:51:46 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1721742706; bh=1MnD3B6Nfn3MrslJWpGjEcsjGMYS+LyrcEuyjGnNM5w=; 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[RFC=20PAT CH=2012/15]=20l2tp:=20use=20rcu=20list=20add/del=20when=20updating =20lists|Date:=20Tue,=2023=20Jul=202024=2014:51:40=20+0100|Message -Id:=20|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=yxlO76ecYpnHBfYTx/FNxteobY196xN/i/5OQusBv+82zUMpAAgQNgtk8Bba3WaLw U5s4dM0/tJo4vKmLZoFMQVQ8mhZHykmUzQKobwWKl8GZKDSI9E1HelzkgH4RxuClPg cSebaMlpJGUao6wt18XsEU6wpL0d2oe6tZ9o/OFUfEF6zmwHwJcA5Zw2lfJ13KMvZA 9BH+9ptoiWt35QgPTEvE1rix2u/p57CvBmGhOHRM4WUiBaLDFjR+jiDuvd4CTObKzo 1d2Bd+JYbKx7c8jkG2V5HwJzz2jzSGTb14k3l2DnsUUZhDHFU1LGzUf33XnM1xEsJs esYOgdGkHKLhg== 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: [RFC PATCH 12/15] l2tp: use rcu list add/del when updating lists Date: Tue, 23 Jul 2024 14:51:40 +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 X-Patchwork-State: RFC 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. --- 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 Tue Jul 23 13:51:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13740083 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 F1D3A137C34 for ; Tue, 23 Jul 2024 13:51:54 +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=1721742716; cv=none; b=h2QxHagMzKaYZF8wz/lhCz3EZ2rW5eunrN6NybRnyJREt9Nt/zwX6EDZ8Ra4UNfJutGp0M0rWp2E6wUmos28xAoh4w6/L9mHu16l4sKoukbquRQ5QTIeTiEyI7gLvFtu6Nvpw1UN2p8hkV1Sps4tN8LvxNZEQjQfI+14cxARV/M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721742716; c=relaxed/simple; bh=/teDg3xyCnz1X6v7zDlAnMreFJkc7/PaTO2cjjE97lQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=m7BFqyYgUYpAGrlbAWTdwROyJLZw7oOhaRokWyvIpr1otyhaOIkPDGfhUGbKTfe9jmO5l4cvZ9sWUmwA9tk9alCUd5Kykl3PhztgypbGDKDFcwN4Rhf/9EVMqcvJfnEhgmwSpulgNMosr83/KrGVTBweIqAkolJG1LvnRhLOq54= 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=B0IIUvoh; 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="B0IIUvoh" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:47:b279:6330:ae0d]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id B00127DCF4; Tue, 23 Jul 2024 14:51:46 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1721742706; bh=/teDg3xyCnz1X6v7zDlAnMreFJkc7/PaTO2cjjE97lQ=; 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[RFC=20PAT CH=2013/15]=20l2tp:=20add=20idr=20consistency=20check=20in=20sessi on_register|Date:=20Tue,=2023=20Jul=202024=2014:51:41=20+0100|Mess age-Id:=20<1857469bef821f5ef31e2f7ef586d1e904b83037.1721733730.git .jchapman@katalix.com>|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=B0IIUvohI3f2Wiom7THAbHA6UAy3CWczUMX7V7VFRSh0lnXWX9CpQswc82WI74kVa NUfB5wyRFhoCquBhd9fo4DVzszEEsnYzeUKv8vU7DorpCsInzmnqjKhBrz82npjuBj X7rHl0zFCRSfHjmDsrxWL6Gv2c1mpkB3M8eoUpT64ty3xawJPIxhwjCPJjEcZQddTw NvuqR/bsshejrXLn37A3bJaFL9Y3o72a5I9lGjucOZVQsSdlPkPjIbrHZvWjh5eyzK St10jj1Y4QGMMCzeLDkPnCG+oy09lLoXhZ5WdRsnFVos5kznmJfU8hHVc3xgbka+cG UPMspsZ9y9Q9Q== 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: [RFC PATCH 13/15] l2tp: add idr consistency check in session_register Date: Tue, 23 Jul 2024 14:51:41 +0100 Message-Id: <1857469bef821f5ef31e2f7ef586d1e904b83037.1721733730.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 X-Patchwork-State: RFC 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. --- 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 Tue Jul 23 13:51:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13740084 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 F1DA6154429 for ; Tue, 23 Jul 2024 13:51:54 +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=1721742716; cv=none; b=YfUrj5c0AcfUPos2iPCgjtvtrBOoiiog9+A0poWUlxaXdhblvAPfVaRU8qkBmy//E7nVz54A0AHQVP3U0y9ueSFcZT+YyFbCNZNduy1l3oK52326WDX1dfhE7SSyqNHPEmgbW/ie0zG9C0VqAAF/m0InUdzej2YBHZvi4o9frmM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721742716; c=relaxed/simple; bh=GdMnyF52RBPzA7i8yG/LLMp4buPCGVahtbKDGe6X0Kg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=P37XzIWucxHRXTN3DCoMGNeSGeFFon4rDWErq4jbODCUVSnMNGCwTXml9SIspRxqmqeDxXu8cucPgiQzNZZ2kQFYIthpXv0g3rXmRnpKHPEH/cwJQJhSOToJqwKMXG2rfpPB42UFcmyW+tMd6uVADVXoB74eYB1lqqWzLYupffs= 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=FEhWLIEj; 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="FEhWLIEj" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:47:b279:6330:ae0d]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id CE9AE7DCF5; Tue, 23 Jul 2024 14:51:46 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1721742706; bh=GdMnyF52RBPzA7i8yG/LLMp4buPCGVahtbKDGe6X0Kg=; 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[RFC=20PAT CH=2014/15]=20l2tp:=20cleanup=20eth/ppp=20pseudowire=20setup=20cod e|Date:=20Tue,=2023=20Jul=202024=2014:51:42=20+0100|Message-Id:=20 <682bb87ca135fd7de91fecb192b8b5db1087a773.1721733730.git.jchapman@ katalix.com>|In-Reply-To:=20|References:=20|MI ME-Version:=201.0; b=FEhWLIEjli3e6ZHyvQYGat8m+SqUUBTnHRZZABUGEAwwFfoTjHous/WaPyLQTh+AU 5hhLIVTbjYMHSgYaXfKMDnL0a9ML/IGezq9KdH3NSWDFo/WUL6NuP2YZCDXXkIlB3C Wl62A3MQGHxxyVEzNY/+cq86G5vXfFbs4oPvsRFh+OpZKxguEEJ0tkb07YRy3vU98f IrHUS4RJTxbZeNipFdivvrpTpWFLFDr+Sqpmu6JryR6222tnV0ZNiXJd0/PX7+JdsK J2U3XV2jZ3Ag530LcPz9A4OtXaYT6uDN5S6hy9fYnn5OKZXIna16cNbQYuSojz6tCR TdHiHCcKR8QOw== 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: [RFC PATCH 14/15] l2tp: cleanup eth/ppp pseudowire setup code Date: Tue, 23 Jul 2024 14:51:42 +0100 Message-Id: <682bb87ca135fd7de91fecb192b8b5db1087a773.1721733730.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 X-Patchwork-State: RFC 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. --- 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 Tue Jul 23 13:51:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Chapman X-Patchwork-Id: 13740085 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 76AEC154445 for ; Tue, 23 Jul 2024 13:51:55 +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=1721742716; cv=none; b=q4gtrNEgv0sZSlXJ1k6sXm8qZdavE3ISxPMiYa9sonMPTdJymJhR8j58Q1pUkWxoLvuQ9fY6HwtIwHmPOGJs6iIJeunGxYGmqZRCWf6S0XQgElIfrhwtjzNrVfoHjL8GKrLr7CQWuqXtZ2sjtyqz+XN3PB8fiNG7XBCAnhVj0O4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721742716; c=relaxed/simple; bh=ISsSxzGzLpmvNc3x4vGptCmW4jbr7HyVvZC9ERNLGW8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hAZ1Vz6iWeE2XN9vosbq0TUsaRkLN8fBE0idWadWiXM/n1nVIHYXztQ9743KsteKaphMmTdV2pPQNg0BX/hG/GARve1OyJ6jFT06tkv+IO8K96SPi/EwkH0Kto7wYNTaT2w9dj/Kh0LsjUT/UJnk0M/i4Dg2vaop3+CyLXxmbgA= 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=TtYnZiAF; 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="TtYnZiAF" Received: from katalix.com (unknown [IPv6:2a02:8010:6359:1:47:b279:6330:ae0d]) (Authenticated sender: james) by mail.katalix.com (Postfix) with ESMTPSA id EE55E7DCF6; Tue, 23 Jul 2024 14:51:46 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=katalix.com; s=mail; t=1721742707; bh=ISsSxzGzLpmvNc3x4vGptCmW4jbr7HyVvZC9ERNLGW8=; 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[RFC=20PAT CH=2015/15]=20l2tp:=20use=20pre_exit=20pernet=20hook=20to=20avoid= 20rcu_barrier|Date:=20Tue,=2023=20Jul=202024=2014:51:43=20+0100|Me ssage-Id:=20|In-Reply-To:=20|References:=20|MIME-Version:=201.0; b=TtYnZiAFssUXjL2tPLudaK5C9eCvGSZJ5KtmSWgUcSWfocnUIbRbjYj2cCXx230VR 3LE9ONoQcnqLC4z6S1qfhBzfHpARtQd0lYFq+wRfRy3//npHCHY0X8PksS4pK5P8Ya y+eNRQMSRk4W6spq2leca3EeGFIs4qmjYvFpRPpAeghD0ZdScbvFoeW6VCYh4U7Xyn swWp1q+lF0Iszcwf+0vharpr7dBhb2y5+2MfkLLWWGBa2lqI+RH06raPsu8Z6388qU 0Ma4YdiUe3QAr0CrSzA8sfZFL23clliQGv00l+KML6luDCViOZBbXUfd6gxLJFUXoX 68lIFje7X1lTQ== 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: [RFC PATCH 15/15] l2tp: use pre_exit pernet hook to avoid rcu_barrier Date: Tue, 23 Jul 2024 14:51:43 +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 X-Patchwork-State: RFC 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. --- 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), };