From patchwork Mon Feb 24 08:13:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13987537 X-Patchwork-Delegate: matthieu.baerts@tessares.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EEA85134AB for ; Mon, 24 Feb 2025 08:13:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740384831; cv=none; b=HnduULn1fyt70yKPFVPP57QWueURPH/j65UhNfQ/isXgEdJODPbA180KcoNcs70jSfNN7mvWnomZaFj2iX8WMlP8sa5NDww1t+vBah43sJg6dvRw0nQaRVU2eXH9bKLzMbN4Ll0UJ9nffw15vHlxN5SMZwggCDAu0ZtWPkSffrQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740384831; c=relaxed/simple; bh=NOyIDquAJI/RMBwfa2jKn/aNdesPUEYCHsZfnuqzwpo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TMKwv7i5TWLrye4m42HDigl8CTG6oeUeyI0r1AKtguk9EvjVb16hUZZn38gbSYtflOC++TmluOsJBVn9vfXjue1mwL9+xGtKnv7rS6SkAdffmpu3yF+9jzfp5EPWuFqyLeWGPvU3p492k5SqCDDSAARWe23W27ag06b8BhOZ/wQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FiqIG2GS; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FiqIG2GS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5FD4DC4CEDD; Mon, 24 Feb 2025 08:13:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1740384830; bh=NOyIDquAJI/RMBwfa2jKn/aNdesPUEYCHsZfnuqzwpo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FiqIG2GSW899YwPOs2jnoPjRF00r07htemuYhVWwFGofQoZJITqEAxUQFW91ZBnqP K5G8FhALd8hrn+hCysz0DmhVRS+DRwNSUm7SgI1lSuzIDzYzsMp6nCxTu0Cnm4yt+u YngbT2iDsNPUsibgGgR1Kxb+6H4RU0XQtgoCkr8NdxG09Z8r7K+e/66KaDHZlrF6N3 SbF7LFeFhXmrI2RgghHbhtteTibh84IjrLy0RhfeFRJ2FKJwpCwUBaX7z0vCLa01pV LlnKo44jhzJjecTf+mc0tbAHTt+s95C2RuSHVoUKUxshmpFeOnxz8EdgWNqwrs5z8W Wos6RMn7kg6Qg== From: Geliang Tang To: mptcp@lists.linux.dev Cc: Geliang Tang Subject: [PATCH mptcp-next v1 1/6] mptcp: pm: in-kernel: avoid access entry without lock Date: Mon, 24 Feb 2025 16:13:33 +0800 Message-ID: <1ccd8b5eee126272e4841635d69616ca79977ad8.1740384564.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang In mptcp_pm_nl_set_flags(), "entry" is copied to "local" when pernet->lock is held to avoid direct access to entry without pernet->lock. Therefore, "local->flags" should be passed to mptcp_nl_set_flags instead of "entry->flags" when pernet->lock is not held, so as to avoid access to entry. Signed-off-by: Geliang Tang --- net/mptcp/pm_netlink.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/mptcp/pm_netlink.c b/net/mptcp/pm_netlink.c index d4328443d844..fb83eba041f1 100644 --- a/net/mptcp/pm_netlink.c +++ b/net/mptcp/pm_netlink.c @@ -1983,7 +1983,7 @@ int mptcp_pm_nl_set_flags(struct mptcp_pm_addr_entry *local, *local = *entry; spin_unlock_bh(&pernet->lock); - mptcp_nl_set_flags(net, &local->addr, entry->flags, changed); + mptcp_nl_set_flags(net, &local->addr, local->flags, changed); return 0; } From patchwork Mon Feb 24 08:13:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13987538 X-Patchwork-Delegate: matthieu.baerts@tessares.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DD91C134AB for ; Mon, 24 Feb 2025 08:13:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740384832; cv=none; b=pLUHAE8bZnqle3Q8BbVNA6p0oiiSMjrHm0pc0vTTPwPYkjXuP3aCKgSaQBHqiy7SNer4/JDNBkAuAtYX6aqbtBqJpQRqM+ZuFmRw2GWSbwvaqEeTKnUDFFZ5ZlbYZsktSGLid9YTkRDymk2iTcW2IPQjIhaCOQykk5Wc6Fz6qmc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740384832; c=relaxed/simple; bh=cGtY4t6sdylYcA+LRDIqPy8T3u2YwXrzz1tLoyScfEo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aHNcvBjb1oOueiA0ZSVC37Iej4+7ZP845lfolHJx+iqUfYsfub3Xwl8nJD80Uz1A69ExVTKVCcTOYHOVU53HtOgVMavIRyYlz3P7sCCjS5AFcvUooKkTvJG08NbJ6fT7K08aZcvWY34ymUBMn7EmyQxhYzu3XFfVtGda1ALVcdk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=C7AL2W55; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="C7AL2W55" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1F7D5C4CED6; Mon, 24 Feb 2025 08:13:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1740384832; bh=cGtY4t6sdylYcA+LRDIqPy8T3u2YwXrzz1tLoyScfEo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C7AL2W55tLXJkYgZQj0O/9kNFJA4vw8DmAgvMFWwzLeLdydpo6KRlkz5ByLduaMlN Ijkc9npOO1qQdzgoby9h493tPkpfvMv6V6s6E1ziGMQfOV0nqBxcI1uusQS2vX3pko ingJbA7QWCzNE0bXnZxUxJYzDio27P+rtjqwIJ04ZlcKYU3/wTkj2/XhIM6gatW9WF OG+Yut61gvUSVGmaTOoh0SdO0yk3Iko5u7og5lEAHqawIQsX5U49KWOekPM8lSuY87 m4+CAERD6FMBp5l2+wDIIX8fAQfdOhz02ZI2+emiISe6eBIBeJGsUheVANpMkavfII jN1+Dd/rgJqUQ== From: Geliang Tang To: mptcp@lists.linux.dev Cc: Geliang Tang Subject: [PATCH mptcp-next v1 2/6] mptcp: pm: in-kernel: reduce parameters of set_flags Date: Mon, 24 Feb 2025 16:13:34 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang The number of parameters in mptcp_nl_set_flags() can be reduced. Only need to pass a "local" parameter to it instead of "local->addr" and "local->flags". Signed-off-by: Geliang Tang --- net/mptcp/pm_netlink.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/net/mptcp/pm_netlink.c b/net/mptcp/pm_netlink.c index fb83eba041f1..4bebc4963c42 100644 --- a/net/mptcp/pm_netlink.c +++ b/net/mptcp/pm_netlink.c @@ -1907,11 +1907,12 @@ static void mptcp_pm_nl_fullmesh(struct mptcp_sock *msk, spin_unlock_bh(&msk->pm.lock); } -static void mptcp_nl_set_flags(struct net *net, struct mptcp_addr_info *addr, - u8 flags, u8 changed) +static void mptcp_nl_set_flags(struct net *net, + struct mptcp_pm_addr_entry *local, + u8 changed) { - u8 is_subflow = !!(flags & MPTCP_PM_ADDR_FLAG_SUBFLOW); - u8 bkup = !!(flags & MPTCP_PM_ADDR_FLAG_BACKUP); + u8 is_subflow = !!(local->flags & MPTCP_PM_ADDR_FLAG_SUBFLOW); + u8 bkup = !!(local->flags & MPTCP_PM_ADDR_FLAG_BACKUP); long s_slot = 0, s_num = 0; struct mptcp_sock *msk; @@ -1926,10 +1927,10 @@ static void mptcp_nl_set_flags(struct net *net, struct mptcp_addr_info *addr, lock_sock(sk); if (changed & MPTCP_PM_ADDR_FLAG_BACKUP) - mptcp_pm_nl_mp_prio_send_ack(msk, addr, NULL, bkup); + mptcp_pm_nl_mp_prio_send_ack(msk, &local->addr, NULL, bkup); /* Subflows will only be recreated if the SUBFLOW flag is set */ if (is_subflow && (changed & MPTCP_PM_ADDR_FLAG_FULLMESH)) - mptcp_pm_nl_fullmesh(msk, addr); + mptcp_pm_nl_fullmesh(msk, &local->addr); release_sock(sk); next: @@ -1983,7 +1984,7 @@ int mptcp_pm_nl_set_flags(struct mptcp_pm_addr_entry *local, *local = *entry; spin_unlock_bh(&pernet->lock); - mptcp_nl_set_flags(net, &local->addr, local->flags, changed); + mptcp_nl_set_flags(net, local, changed); return 0; } From patchwork Mon Feb 24 08:13:35 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13987539 X-Patchwork-Delegate: matthieu.baerts@tessares.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9CFAB20AF7D for ; Mon, 24 Feb 2025 08:13:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740384834; cv=none; b=iGDUP4TejA6vLITnIraHewYmX+FXluK13G4ymADSD4RyaKwLGIE7mXVcoFjKwpUsWRTWkmRVfNxGsCqGcKLBSUlpN1NXC/x7TVTAluTfNQ8quWwU+w93LcIZQqTKBcCHXej6sJTLepaCRqUsqK5kWPgHuKH63cgqxSbtaA3Xi9U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740384834; c=relaxed/simple; bh=mR8zQ501/FwysZh5cA+ljZiZfZgamM7wV7cp/X7dyGQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SFjzBpQX65Wf8jTAVsPR6KbLGh5jhZCXnV2z+O5Pkfgr0tKdP2wWuoPBMKX5XhUg6IEDmSN5/GSVOCYxLvkHbZr/AzbaaVqA97nhM8ycmm4WybcHRIYAfcq8/dWKophbnAjkrCl7wIx58VfqUVyuvi9WloHdtWu9f8N7maLI8Ho= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ayftqkux; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Ayftqkux" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED8C6C4CEDD; Mon, 24 Feb 2025 08:13:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1740384834; bh=mR8zQ501/FwysZh5cA+ljZiZfZgamM7wV7cp/X7dyGQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ayftqkuxj9jFVCz4FZ1iCdlR1wDlFAsn56y6bRNgU2kQsXr1RG/XEm+WTSHqb9+IQ wwp23fiBgDxRlDOyTHou1ki+1IF6jl/iE58SSH5CxCZ7nI0V4IzKeX4tnmGZ7qSzhB iYNwEvkDx8YLgnSCxIgEcUJ0uHFtueKGWeLG0cGAcKy2ao+BhJdQgg607W3jYwMfTC 7l6BjgouZzi+JNYWdtarxQ+oI/cTHQ5fqWWfJidWO4FK2UlRt7joBH3kcdWt6sMA/8 4SOLFUEu3kpbvWjF7MWzJQwulx1zhG4vFLqXlbtfgFoIle+ktfnTdGbG4ggKmr+kif iyuEb8wEkRI4g== From: Geliang Tang To: mptcp@lists.linux.dev Cc: Geliang Tang Subject: [PATCH mptcp-next v1 3/6] mptcp: pm: use addr entry for get_local_id Date: Mon, 24 Feb 2025 16:13:35 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang The following code in mptcp_userspace_pm_get_local_id() that assigns "skc" to "new_entry" is not allowed in BPF if we use the same code to implement the get_local_id() interface of a BFP path manager: memset(&new_entry, 0, sizeof(struct mptcp_pm_addr_entry)); new_entry.addr = *skc; new_entry.addr.id = 0; new_entry.flags = MPTCP_PM_ADDR_FLAG_IMPLICIT; To solve the issue, this patch moves this assignment to "new_entry" forward to mptcp_pm_get_local_id(), and then passing "new_entry" as a parameter to both mptcp_pm_nl_get_local_id() and mptcp_userspace_pm_get_local_id(). Signed-off-by: Geliang Tang --- net/mptcp/pm.c | 9 ++++++--- net/mptcp/pm_netlink.c | 11 ++++------- net/mptcp/pm_userspace.c | 17 ++++++----------- net/mptcp/protocol.h | 6 ++++-- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/net/mptcp/pm.c b/net/mptcp/pm.c index 16cacce6c10f..ac7b39148bd3 100644 --- a/net/mptcp/pm.c +++ b/net/mptcp/pm.c @@ -403,7 +403,7 @@ bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining, int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc) { - struct mptcp_addr_info skc_local; + struct mptcp_pm_addr_entry skc_local = { 0 }; struct mptcp_addr_info msk_local; if (WARN_ON_ONCE(!msk)) @@ -413,10 +413,13 @@ int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc) * addr */ mptcp_local_address((struct sock_common *)msk, &msk_local); - mptcp_local_address((struct sock_common *)skc, &skc_local); - if (mptcp_addresses_equal(&msk_local, &skc_local, false)) + mptcp_local_address((struct sock_common *)skc, &skc_local.addr); + if (mptcp_addresses_equal(&msk_local, &skc_local.addr, false)) return 0; + skc_local.addr.id = 0; + skc_local.flags = MPTCP_PM_ADDR_FLAG_IMPLICIT; + if (mptcp_pm_is_userspace(msk)) return mptcp_userspace_pm_get_local_id(msk, &skc_local); return mptcp_pm_nl_get_local_id(msk, &skc_local); diff --git a/net/mptcp/pm_netlink.c b/net/mptcp/pm_netlink.c index 4bebc4963c42..033cba59023f 100644 --- a/net/mptcp/pm_netlink.c +++ b/net/mptcp/pm_netlink.c @@ -1139,7 +1139,8 @@ static int mptcp_pm_nl_create_listen_socket(struct sock *sk, return err; } -int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct mptcp_addr_info *skc) +int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, + struct mptcp_pm_addr_entry *skc) { struct mptcp_pm_addr_entry *entry; struct pm_nl_pernet *pernet; @@ -1148,7 +1149,7 @@ int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct mptcp_addr_info *skc pernet = pm_nl_get_pernet_from_msk(msk); rcu_read_lock(); - entry = __lookup_addr(pernet, skc); + entry = __lookup_addr(pernet, &skc->addr); ret = entry ? entry->addr.id : -1; rcu_read_unlock(); if (ret >= 0) @@ -1159,12 +1160,8 @@ int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct mptcp_addr_info *skc if (!entry) return -ENOMEM; - entry->addr = *skc; - entry->addr.id = 0; + *entry = *skc; entry->addr.port = 0; - entry->ifindex = 0; - entry->flags = MPTCP_PM_ADDR_FLAG_IMPLICIT; - entry->lsk = NULL; ret = mptcp_pm_nl_append_new_local_addr(pernet, entry, true); if (ret < 0) kfree(entry); diff --git a/net/mptcp/pm_userspace.c b/net/mptcp/pm_userspace.c index 6bf6a20ef7f3..5b3ee43130be 100644 --- a/net/mptcp/pm_userspace.c +++ b/net/mptcp/pm_userspace.c @@ -131,27 +131,22 @@ mptcp_userspace_pm_lookup_addr_by_id(struct mptcp_sock *msk, unsigned int id) } int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk, - struct mptcp_addr_info *skc) + struct mptcp_pm_addr_entry *skc) { - struct mptcp_pm_addr_entry *entry = NULL, new_entry; __be16 msk_sport = ((struct inet_sock *) inet_sk((struct sock *)msk))->inet_sport; + struct mptcp_pm_addr_entry *entry; spin_lock_bh(&msk->pm.lock); - entry = mptcp_userspace_pm_lookup_addr(msk, skc); + entry = mptcp_userspace_pm_lookup_addr(msk, &skc->addr); spin_unlock_bh(&msk->pm.lock); if (entry) return entry->addr.id; - memset(&new_entry, 0, sizeof(struct mptcp_pm_addr_entry)); - new_entry.addr = *skc; - new_entry.addr.id = 0; - new_entry.flags = MPTCP_PM_ADDR_FLAG_IMPLICIT; - - if (new_entry.addr.port == msk_sport) - new_entry.addr.port = 0; + if (skc->addr.port == msk_sport) + skc->addr.port = 0; - return mptcp_userspace_pm_append_new_local_addr(msk, &new_entry, true); + return mptcp_userspace_pm_append_new_local_addr(msk, skc, true); } bool mptcp_userspace_pm_is_backup(struct mptcp_sock *msk, diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h index 3d72ca155322..ef1d43406f9b 100644 --- a/net/mptcp/protocol.h +++ b/net/mptcp/protocol.h @@ -1126,8 +1126,10 @@ bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, const struct sk_buff *skb, bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining, struct mptcp_rm_list *rm_list); int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc); -int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct mptcp_addr_info *skc); -int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk, struct mptcp_addr_info *skc); +int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, + struct mptcp_pm_addr_entry *skc); +int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk, + struct mptcp_pm_addr_entry *skc); bool mptcp_pm_is_backup(struct mptcp_sock *msk, struct sock_common *skc); bool mptcp_pm_nl_is_backup(struct mptcp_sock *msk, struct mptcp_addr_info *skc); bool mptcp_userspace_pm_is_backup(struct mptcp_sock *msk, struct mptcp_addr_info *skc); From patchwork Mon Feb 24 08:13:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13987540 X-Patchwork-Delegate: geliang@kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 73F71134AB for ; Mon, 24 Feb 2025 08:13:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740384836; cv=none; b=tik9nYH8f3UdmeV91bezRauJuJRC5XQIbF0o2v5HYIrrO/OxlYq2PiNeP+g1NbaPpAZsQR0dFdqW10Cp+tCnQesnO8xrVC6L8kmMCcjtU7jg9AIKqZtLRGh6r0ofWi/j0Zrf4gdOb6ZW7hLrc6w245edQsJ34KeStYcC9E0nIw0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740384836; c=relaxed/simple; bh=N+3lTKYpkYqF/AO62aQgwdpe7jO7MvLiVRUk9xtnQTk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OZFWl+KVDLAc4hH0zIAS+KN8YeTsSi7h6NWwqnIwNISGRu0Pwu3c+HDZb0VYrCW+2iq9rxacCmxFjLMigUe2od6XRUZjJ2ni4EoxGLilsbFo1dy6+owmlGDveeKISjrek1Cf/hE0yMSRy37zTXhRDgdFCt7tvTxYhH2XLwpSjYo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=C12DWz6x; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="C12DWz6x" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AE237C4CED6; Mon, 24 Feb 2025 08:13:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1740384835; bh=N+3lTKYpkYqF/AO62aQgwdpe7jO7MvLiVRUk9xtnQTk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C12DWz6x0416X2ioe9bGD0iBPy4R26iRa7WRJ9mmrpvmaMvfWwNBwUOvhpognVUlf cSjbEMwoSiUFiCEOD/qntfp5qWGnJ+E4lN8FZ0JwODmREssh0w8ycnA0zaX6aq7ea2 OrIvYpP+uYCPgFzWFjXZeMVDyFiRA43r0owsEVSokX3VgAgBC6EE3WLsJNFGJAH4tp GvovtyzuTMTuRFCyNcpY3zwzvMXNX3eOYS3Q2JqN6AFzJmM4BMC3XoAmD2BaysIZ6C zajbiWwtbHEVFKlKC1GnI5P7ZsuYwfnWHCTzlG1BCGTrO9JJn4aV/uFBEhiUiLoE6Z aeQIiubh75uPA== From: Geliang Tang To: mptcp@lists.linux.dev Cc: Geliang Tang Subject: [PATCH mptcp-next v1 4/6] mptcp: pm: in-kernel: use kmemdup helper Date: Mon, 24 Feb 2025 16:13:36 +0800 Message-ID: <5f8d3e2b1e7dc0b91ae84288da40d24ad505f50f.1740384564.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang Instead of using kmalloc() or kzalloc() to allocate an entry and then immediately duplicate another entry to the newly allocated one, kmemdup() helper can be used to simplify the code. Signed-off-by: Geliang Tang --- net/mptcp/pm_netlink.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/net/mptcp/pm_netlink.c b/net/mptcp/pm_netlink.c index 033cba59023f..ee0cd92865cc 100644 --- a/net/mptcp/pm_netlink.c +++ b/net/mptcp/pm_netlink.c @@ -1156,11 +1156,10 @@ int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, return ret; /* address not found, add to local list */ - entry = kmalloc(sizeof(*entry), GFP_ATOMIC); + entry = kmemdup(skc, sizeof(*skc), GFP_ATOMIC); if (!entry) return -ENOMEM; - *entry = *skc; entry->addr.port = 0; ret = mptcp_pm_nl_append_new_local_addr(pernet, entry, true); if (ret < 0) @@ -1422,13 +1421,12 @@ int mptcp_pm_nl_add_addr_doit(struct sk_buff *skb, struct genl_info *info) return -EINVAL; } - entry = kzalloc(sizeof(*entry), GFP_KERNEL_ACCOUNT); + entry = kmemdup(&addr, sizeof(addr), GFP_KERNEL_ACCOUNT); if (!entry) { GENL_SET_ERR_MSG(info, "can't allocate addr"); return -ENOMEM; } - *entry = addr; if (entry->addr.port) { ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry); if (ret) { From patchwork Mon Feb 24 08:13:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13987541 X-Patchwork-Delegate: geliang@kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BCD12134AB for ; Mon, 24 Feb 2025 08:13:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740384837; cv=none; b=CO3hYAkuDQhkEbH52xPJiS4oaPh21j5qu0GuVFD+Bp/s1d/TouJmz5GDgjj86F0TbJ3J7nZrH2Xlgl0zCfQoU9Dq+LFMcLkkN8qtVe+BMpDW4nqU/BsH9lT/uRlR9aJLAbFNSN7nTIixbYMkIE4cGGEKB0GpDV3Qgj1RyF7IZNI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740384837; c=relaxed/simple; bh=oYD5ye+ngA/nO4KOXy1Lxrta+2C66xKgicBDo3y7hDg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=M8SZp/TEXxNFd7/MTH0BQAUsyqQevrgMcO1/KPjVcyJUMddIMj38IT8NVWZ0A2DXzZl3IF0WiE4Lnqmrsgg/oGtO1RhaQfwOJYfmRIW0Aub8LJ3IuGlCEkmb/jCTdLxTqHu956FhuEgDtj/Jm/pmIB6lvV8oh9WuHBI3b+Je1tw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mXX/mL6X; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="mXX/mL6X" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 887E9C4CEDD; Mon, 24 Feb 2025 08:13:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1740384837; bh=oYD5ye+ngA/nO4KOXy1Lxrta+2C66xKgicBDo3y7hDg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mXX/mL6Xt1P4REdCAUqvDvHSU/IKgj67jaUiUvgyHhyt8lagQVfS+j737A/cxoA+f IyP30xoZWp7vz2KEsfJS0kA+JsWYYagySI7oXuvXOkWhIbZBHcELPSgbbbD0WTdeNN hIjIF0bsZEb+rr0oQrRAllytJHoBXnzFAEfYzikfTyExh+vu+Ua05OdpKj5tN0WMtI ulchK+jgMmr+iuoSK/1m5xaV3iM16Ob/UP1A5NIokpWsa1+wjuWvvOPpSBZn53kLTL pkDBP0Ld064R6ipH+fRB6DoHzSOmGAui0yPUmKfyV6ANqxr8q6R2aPKyoRa/SBpYRt 9UiGuYlKU7aKw== From: Geliang Tang To: mptcp@lists.linux.dev Cc: Geliang Tang Subject: [PATCH mptcp-next v1 5/6] sock: add sock_kmemdup helper Date: Mon, 24 Feb 2025 16:13:37 +0800 Message-ID: <0a3f7a31983f0587ace333f349f3e630c49d075d.1740384564.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang This patch adds the sock version of kmemdup() helper, named sock_kmemdup(), to duplicate a memory block using the socket's option memory buffer. Signed-off-by: Geliang Tang --- include/net/sock.h | 1 + net/core/sock.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/net/sock.h b/include/net/sock.h index edbb870e3f86..ffd757e7e329 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1793,6 +1793,7 @@ static inline struct sk_buff *sock_alloc_send_skb(struct sock *sk, } void *sock_kmalloc(struct sock *sk, int size, gfp_t priority); +void *sock_kmemdup(struct sock *sk, const void *src, int size, gfp_t priority); void sock_kfree_s(struct sock *sk, void *mem, int size); void sock_kzfree_s(struct sock *sk, void *mem, int size); void sk_send_sigurg(struct sock *sk); diff --git a/net/core/sock.c b/net/core/sock.c index 0d385bf27b38..d09bd697c120 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -2805,6 +2805,29 @@ void *sock_kmalloc(struct sock *sk, int size, gfp_t priority) } EXPORT_SYMBOL(sock_kmalloc); +/* + * Duplicate a memory block using the socket's option memory buffer. + */ +void *sock_kmemdup(struct sock *sk, const void *src, int size, gfp_t priority) +{ + int optmem_max = READ_ONCE(sock_net(sk)->core.sysctl_optmem_max); + + if ((unsigned int)size <= optmem_max && + atomic_read(&sk->sk_omem_alloc) + size < optmem_max) { + void *mem; + /* First do the add, to avoid the race if kmalloc + * might sleep. + */ + atomic_add(size, &sk->sk_omem_alloc); + mem = kmemdup(src, size, priority); + if (mem) + return mem; + atomic_sub(size, &sk->sk_omem_alloc); + } + return NULL; +} +EXPORT_SYMBOL(sock_kmemdup); + /* Free an option memory block. Note, we actually want the inline * here as this allows gcc to detect the nullify and fold away the * condition entirely. From patchwork Mon Feb 24 08:13:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13987542 X-Patchwork-Delegate: geliang@kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9790F20AF7D for ; Mon, 24 Feb 2025 08:13:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740384839; cv=none; b=VRVMHxWpRn5HVF0zG5m68mmsKBNztE/NWqws4SzgtAAiPZfsa/37hhh3XQIkqrxSRN2f63+2S70K3Q0lMIampGcaOwnbGhKHLJmu33jaOa+S2A0ZSXvnlQ5TVrEDPUwn7JXVtubDPI1YzufKA/oJH1fuM7ojHqoGyEym/tIUwqQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740384839; c=relaxed/simple; bh=oHp2VhYIwfXgRGGPij/JHd8Za0s2dR6506jv0zObong=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R9RjS4MPAIk89tYqFnmwIXqQWPKlZx/HLmEfshraGy0qikH+RpwjYCtY0DiCH7CghcMTdBxwUcQxbzrMUeKhFw/iGzZpmFoa+sihKtPhmHXl6VWYOWTIv04FI9/n4FzDtQMEX641QHR24qsK1BSmiV74P9fpG4dBRjsQqeThZYg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DZZlxbmM; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DZZlxbmM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 485E6C4CED6; Mon, 24 Feb 2025 08:13:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1740384839; bh=oHp2VhYIwfXgRGGPij/JHd8Za0s2dR6506jv0zObong=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DZZlxbmM0k4wuVWqekHSOm/7r6KpkqTE8b+tVdaS2mjPwlaz8MhH3td7XK7rUBfLG W+xCZ5iVyfEUSZiyAZmS9IWplevwHns9jm3PNmg3/608+MpBqK49m4Q78ZFZNmfI+i DpsSXxwRl+EEUa5m34oEBSNtz9VXKA7YDZt0sZ8b2rm4DIdv7hg9xAM0bulJObXW47 Mogcm+6LKfE2T1pe5IuEoF3CxQT3I/PPFveo2HU4PZIQlAeaOpT40dMtxtQUG1glaC vQ2hauVtz1E/QIA9M7+jKOvXq2IY4SvmwLi0NjNbpRVZwhvTe7YaVrFAf15J+5YB8k 5Uw71seBIem3w== From: Geliang Tang To: mptcp@lists.linux.dev Cc: Geliang Tang Subject: [PATCH mptcp-next v1 6/6] mptcp: pm: userspace: use sock_kmemdup helper Date: Mon, 24 Feb 2025 16:13:38 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang Instead of using sock_kmalloc() to allocate an entry and then immediately duplicate another entry to the newly allocated one, sock_kmemdup() helper can be used to simplify the code. More importantly, the code "*e = *entry;" that assigns "entry" to "e" is not easy to implemented in BPF if we use the same code to implement an append_new_local_addr() helper of a BFP path manager. This patch avoids this type of memory assignment operation. Signed-off-by: Geliang Tang --- net/mptcp/pm_userspace.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/net/mptcp/pm_userspace.c b/net/mptcp/pm_userspace.c index 5b3ee43130be..8c45eebe9bbc 100644 --- a/net/mptcp/pm_userspace.c +++ b/net/mptcp/pm_userspace.c @@ -71,13 +71,12 @@ static int mptcp_userspace_pm_append_new_local_addr(struct mptcp_sock *msk, /* Memory for the entry is allocated from the * sock option buffer. */ - e = sock_kmalloc(sk, sizeof(*e), GFP_ATOMIC); + e = sock_kmemdup(sk, entry, sizeof(*entry), GFP_ATOMIC); if (!e) { ret = -ENOMEM; goto append_err; } - *e = *entry; if (!e->addr.id && needs_id) e->addr.id = find_next_zero_bit(id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1,