From patchwork Fri Feb 28 10:01:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13996126 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 19F1D2561DE; Fri, 28 Feb 2025 10:01: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=1740736913; cv=none; b=ahJ8RGmi6TdYfSNWy+C4+qDaMlH+ELQvoHtFxgsu9MBvGNSA13dczhqayuDYg13GiNhbUZaLKRqLNrBFYacdbrwEP6jT4eeYHmaVylB/ZvrZIk0Xf+D/5gGmcCrCl5pNFegp2KsUOURM9AbZ+i0e2puntf+AxrTgwGrR/uETx94= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740736913; c=relaxed/simple; bh=DmbSIkOjD/LlT1o10zy3tU/eSic7bXpF/O0NJUnRgck=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=p0L2pLahhDE8uQVfL+Fg8CWbaXgGe9fBczJjK78EXZgoqTkiSGjpxFwz6iW5Vhvh9py8OhF1CA/CEjY+vwNcWtyWRq0FQmPwQbgb5DFVik2/TlBx7694SA1QRQ9S6N1mYyGvNfo0p8KufkLGQ+W+hLglblFJwqXnEjslFytdPl4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lkEgypt5; 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="lkEgypt5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 117FEC4CED6; Fri, 28 Feb 2025 10:01:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1740736912; bh=DmbSIkOjD/LlT1o10zy3tU/eSic7bXpF/O0NJUnRgck=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lkEgypt5q6eaPbWIL3ULLUb2XSQIK7g81eBZd/gneEcaBXRaREyRRo3UbE2yxegLA Jeu6NPsuNUdJjZvMdPmvEIRPl3HH8IE0/T47Hx8tZv96E64WBd7ofMLXROY3cYVIVo e8llWtCRC+evYW4SAnrdfuVdKJjG1wFVmeq06oTIxf8AdAmDGzY+5CxYHbJhEj0hDM T9fJqGgv8hOmx4vcGeI8s1HX+30cLPE2Ebwx5D6QtyYl5VYHv9hkr9OuMiGpjDBcLx iP9Lb7qMFIm549oErvOIxq4NUEpSb32S69/6Var76Dqz+vC972KsEdQFyehuhHafk7 CZgIPy/wVVwcw== From: Geliang Tang To: Eric Dumazet , Kuniyuki Iwashima , Paolo Abeni , Willem de Bruijn , "David S. Miller" , Jakub Kicinski , Simon Horman , Neal Cardwell , David Ahern , Matthieu Baerts , Mat Martineau , Marcelo Ricardo Leitner , Xin Long Cc: Geliang Tang , netdev@vger.kernel.org, mptcp@lists.linux.dev, linux-sctp@vger.kernel.org Subject: [PATCH net-next v2 1/3] sock: add sock_kmemdup helper Date: Fri, 28 Feb 2025 18:01:31 +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 This patch adds the sock version of kmemdup() helper, named sock_kmemdup(), to duplicate the input "src" memory block using the socket's option memory buffer. Signed-off-by: Geliang Tang --- include/net/sock.h | 2 ++ net/core/sock.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/net/sock.h b/include/net/sock.h index e771d99f81b0..8daf1b3b12c6 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1797,6 +1797,8 @@ 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 23bce41f7f1f..a0598518ce89 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -2836,6 +2836,22 @@ void *sock_kmalloc(struct sock *sk, int size, gfp_t priority) } EXPORT_SYMBOL(sock_kmalloc); +/* + * Duplicate the input "src" memory block using the socket's + * option memory buffer. + */ +void *sock_kmemdup(struct sock *sk, const void *src, + int size, gfp_t priority) +{ + void *mem; + + mem = sock_kmalloc(sk, size, priority); + if (mem) + memcpy(mem, src, size); + return mem; +} +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 Fri Feb 28 10:01:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13996127 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 6C26725D8E2; Fri, 28 Feb 2025 10:01: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=1740736919; cv=none; b=UFqel+powIA6rA1zQBKKfO/rx4IsjHsk4dtaUaLTryQvH3Xm8Jdv4LkaQEbdm1XTrnx/bSBFFCdk3pjEj55ESmbnJS5CKp8z0oy2axAFDS1zllvhopg7OjtnPibcnucvXzTL5W2LQDYV41DQt0OuC3ckS575Z9manIFNq6ZLXcg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740736919; c=relaxed/simple; bh=aapUC7Cjh7EbD1HOfRjYKYZWhLoYzlc5DB+Vv2Xpk0k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FV+6GK4OVuyrp7aEk53tWLXo4J+sToJEBJKOJLms90OCt7aY6RXfRp39t6LXhw9/z8wvwyMhaei8Oxho3tsDN3371mwdbuQSdr3Ii0lTX9+cllAfZ9pq8LC2fXLGWcWKAAg33EU2Y6MegGk6ADIAsOuB/dbMKuwH56xovwODgFI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=psRWxM52; 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="psRWxM52" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 377EBC4CEE5; Fri, 28 Feb 2025 10:01:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1740736918; bh=aapUC7Cjh7EbD1HOfRjYKYZWhLoYzlc5DB+Vv2Xpk0k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=psRWxM52LXrkkNIPgNcl6ruBNaJ48Y2q2X9vDBU4JP2M53lzsHuR7A3C83ElRAdqw ENnf36/TUaauNrj02Eze62CO/5ntfk4kJZr9lXobgVEqKu43gkNYmufa23bYBnC1Ag uoxQEopFk5nr/SYeT881ETbYTNsXsr855+ZOJ4S4MTa46+rMo+hzHQM8/hoHWe+2/P NQmp95Uz7R9qSZSJjQlpcBnsfbj/17/ie0puOz5Ct7NPWMkGflCAVAZfh01p6d/30t sMnhNHo9PRzSozj0RAai2BuVNrk+zYpTDfFqaHrD73cWaqLEevyicz+Ubtq2DXuvEO ylMAAmX0A0wwQ== From: Geliang Tang To: Eric Dumazet , Kuniyuki Iwashima , Paolo Abeni , Willem de Bruijn , "David S. Miller" , Jakub Kicinski , Simon Horman , Neal Cardwell , David Ahern , Matthieu Baerts , Mat Martineau , Marcelo Ricardo Leitner , Xin Long Cc: Geliang Tang , netdev@vger.kernel.org, mptcp@lists.linux.dev, linux-sctp@vger.kernel.org Subject: [PATCH net-next v2 2/3] net: use sock_kmemdup for ip_options Date: Fri, 28 Feb 2025 18:01:32 +0800 Message-ID: <91ae749d66600ec6fb679e0e518fda6acb5c3e6f.1740735165.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 sock_kmalloc() to allocate an ip_options and then immediately duplicate another ip_options to the newly allocated one in ipv6_dup_options(), mptcp_copy_ip_options() and sctp_v4_copy_ip_options(), the newly added sock_kmemdup() helper can be used to simplify the code. Signed-off-by: Geliang Tang --- net/ipv6/exthdrs.c | 3 +-- net/mptcp/protocol.c | 7 ++----- net/sctp/protocol.c | 7 ++----- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/net/ipv6/exthdrs.c b/net/ipv6/exthdrs.c index 6789623b2b0d..457de0745a33 100644 --- a/net/ipv6/exthdrs.c +++ b/net/ipv6/exthdrs.c @@ -1204,10 +1204,9 @@ ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt) { struct ipv6_txoptions *opt2; - opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC); + opt2 = sock_kmemdup(sk, opt, opt->tot_len, GFP_ATOMIC); if (opt2) { long dif = (char *)opt2 - (char *)opt; - memcpy(opt2, opt, opt->tot_len); if (opt2->hopopt) *((char **)&opt2->hopopt) += dif; if (opt2->dst0opt) diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c index 6b61b7dee33b..ec23e65ef0f1 100644 --- a/net/mptcp/protocol.c +++ b/net/mptcp/protocol.c @@ -3178,12 +3178,9 @@ static void mptcp_copy_ip_options(struct sock *newsk, const struct sock *sk) rcu_read_lock(); inet_opt = rcu_dereference(inet->inet_opt); if (inet_opt) { - newopt = sock_kmalloc(newsk, sizeof(*inet_opt) + + newopt = sock_kmemdup(newsk, inet_opt, sizeof(*inet_opt) + inet_opt->opt.optlen, GFP_ATOMIC); - if (newopt) - memcpy(newopt, inet_opt, sizeof(*inet_opt) + - inet_opt->opt.optlen); - else + if (!newopt) net_warn_ratelimited("%s: Failed to copy ip options\n", __func__); } RCU_INIT_POINTER(newinet->inet_opt, newopt); diff --git a/net/sctp/protocol.c b/net/sctp/protocol.c index 29727ed1008e..5407a3922101 100644 --- a/net/sctp/protocol.c +++ b/net/sctp/protocol.c @@ -185,12 +185,9 @@ static void sctp_v4_copy_ip_options(struct sock *sk, struct sock *newsk) rcu_read_lock(); inet_opt = rcu_dereference(inet->inet_opt); if (inet_opt) { - newopt = sock_kmalloc(newsk, sizeof(*inet_opt) + + newopt = sock_kmemdup(newsk, inet_opt, sizeof(*inet_opt) + inet_opt->opt.optlen, GFP_ATOMIC); - if (newopt) - memcpy(newopt, inet_opt, sizeof(*inet_opt) + - inet_opt->opt.optlen); - else + if (!newopt) pr_err("%s: Failed to copy ip options\n", __func__); } RCU_INIT_POINTER(newinet->inet_opt, newopt); From patchwork Fri Feb 28 10:01: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: 13996128 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 92B2025D559; Fri, 28 Feb 2025 10:02:05 +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=1740736925; cv=none; b=uXXOywi08e/ZG/8MxZujS7TtkSaZYD3fIUcglInRMDc7W/Ej7RMahqsFZzikiCp9yNJubxfkUUR4kerHM9Ox2lx2I1O45S5cDtmabfkQbasyT7er1ZsnsC9Mt6YGaBh9SNYglOgkGXylmdxm+IrYSeCwNjo8hT17cqre3ERaejE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740736925; c=relaxed/simple; bh=74yTkQnIWdu4oYvGKAnTJbzZweABw7andTw5FBDDA0U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qymdGocGvZMVLjKtiZcAWFyDUyFO+fEtfIMJsAeQ1oXk+GAqOe4gi8bkaxn23I0hXdhKFxPLkrxNrRZUoVBrjkmYT535VnfhRVGSvqjJiVWeNia+CrhZ0kDX9WJzyHWq0AxhHjxQGbXigI31Jsba7zImlohETDYQGhK8eICgjfs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=msaZkFAQ; 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="msaZkFAQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 89B2DC4CED6; Fri, 28 Feb 2025 10:01:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1740736925; bh=74yTkQnIWdu4oYvGKAnTJbzZweABw7andTw5FBDDA0U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=msaZkFAQ05HmgjecjY/WhP+zGeTMJp4HL4248ndjfD5M08oukgGTEQRe2T8GuSe9P do77QMYgbdpZ8pMI92WkCf9fCbJ0oL8jD+wlsNPg0kWDqolOXBC7+oNpSrPDyUpd6e KRFUX2HmznwZghMzLRMj17g59luy05CHwSeafwVq5PvoYgYYEEElrPLZ1BDaIe71SA 8li0sPLUpJGDCp7OwqLSLYlJEmI7Kn6MGKRc9bUwbbjptxLK+3neswa37wflVRY+Nb S6tqLlT0A6nvUoCDCr1VdUS+DtPJ1lPoJIT0mzXw2NxNlw0rbyGgdCzrcgCLC5sebd 0MpXDWDe6mYsA== From: Geliang Tang To: Eric Dumazet , Kuniyuki Iwashima , Paolo Abeni , Willem de Bruijn , "David S. Miller" , Jakub Kicinski , Simon Horman , Neal Cardwell , David Ahern , Matthieu Baerts , Mat Martineau , Marcelo Ricardo Leitner , Xin Long Cc: Geliang Tang , netdev@vger.kernel.org, mptcp@lists.linux.dev, linux-sctp@vger.kernel.org Subject: [PATCH net-next v2 3/3] mptcp: use sock_kmemdup for address entry Date: Fri, 28 Feb 2025 18:01:33 +0800 Message-ID: <3e5a307aed213038a87e44ff93b5793229b16279.1740735165.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 sock_kmalloc() to allocate an address entry "e" and then immediately duplicate the input "entry" to it, the newly added sock_kmemdup() helper can be used in mptcp_userspace_pm_append_new_local_addr() 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 6bf6a20ef7f3..7e7d01bef5d4 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,