Skip to contentSkip to conversation

add sock_kmemdup helper

PATCH Superseded Geliang Tang 4 participants 10 messages Feb 27, 2025

Subsystems (7)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Geliang Tang

author reviewer
#0 lore raw

cover add sock_kmemdup helper

From: Geliang Tang <[email protected]>

While developing MPTCP BPF path manager [1], I found it's useful to add a new sock_kmemdup() helper.

My use case is this:

In mptcp_userspace_pm_append_new_local_addr() function (see patch 3 in this patchset), there is a code that uses sock_kmalloc() to allocate an address entry "e", then immediately duplicate the input "entry" to it:

	e = sock_kmalloc(sk, sizeof(*e), GFP_ATOMIC);
	if (!e) {
		ret = -ENOMEM;
		goto append_err;
	}
	*e = *entry;

When I implemented MPTCP BPF path manager, I needed to implement a code similar to this in BPF.

The kfunc sock_kmalloc() can be easily invoked in BPF to allocate an entry "e", but the code "*e = *entry;" that assigns "entry" to "e" is not easy to implemented.

I had to implement such a copy entry helper in BPF:

static void mptcp_pm_copy_addr(struct mptcp_addr_info *dst,
struct mptcp_addr_info *src)

{
       dst->id = src->id;
       dst->family = src->family;
       dst->port = src->port;

       if (src->family == AF_INET) {
               dst->addr.s_addr = src->addr.s_addr;
       } else if (src->family == AF_INET6) {
               dst->addr6.s6_addr32[0] = src->addr6.s6_addr32[0];
               dst->addr6.s6_addr32[1] = src->addr6.s6_addr32[1];
               dst->addr6.s6_addr32[2] = src->addr6.s6_addr32[2];
               dst->addr6.s6_addr32[3] = src->addr6.s6_addr32[3];
       }
}

static void mptcp_pm_copy_entry(struct mptcp_pm_addr_entry *dst,
struct mptcp_pm_addr_entry *src)

{
       mptcp_pm_copy_addr(&dst->addr, &src->addr);

       dst->flags = src->flags;
       dst->ifindex = src->ifindex;
}

And add write permission for BPF to each field of mptcp_pm_addr_entry:

@@ -74,24 +74,6 @@ static int bpf_mptcp_pm_btf_struct_access(struct bpf_verifier_log *log,

               case offsetof(struct mptcp_pm_addr_entry, addr.port):
                       end = offsetofend(struct mptcp_pm_addr_entry, addr.port);
                       break;
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
               case offsetof(struct mptcp_pm_addr_entry, addr.addr6.s6_addr32[0]):
                       end = offsetofend(struct mptcp_pm_addr_entry, addr.addr6.s6_addr32[0]);
                       break;
               case offsetof(struct mptcp_pm_addr_entry, addr.addr6.s6_addr32[1]):
                       end = offsetofend(struct mptcp_pm_addr_entry, addr.addr6.s6_addr32[1]);
                       break;
               case offsetof(struct mptcp_pm_addr_entry, addr.addr6.s6_addr32[2]):
                       end = offsetofend(struct mptcp_pm_addr_entry, addr.addr6.s6_addr32[2]);
                       break;
               case offsetof(struct mptcp_pm_addr_entry, addr.addr6.s6_addr32[3]):
                       end = offsetofend(struct mptcp_pm_addr_entry, addr.addr6.s6_addr32[3]);
                       break;
#else
               case offsetof(struct mptcp_pm_addr_entry, addr.addr.s_addr):
                       end = offsetofend(struct mptcp_pm_addr_entry, addr.addr.s_addr);
                       break;

#endif

But if there's a sock_kmemdup() helper, it will become much simpler, only need to call kfunc sock_kmemdup() instead in BPF.

So this patchset adds this new helper and uses it in several places.

[1]
https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

Geliang Tang (4):
sock: add sock_kmemdup helper
net: use sock_kmemdup for ip_options
mptcp: use sock_kmemdup for address entry
net/tcp_ao: use sock_kmemdup for tcp_ao_key

 include/net/sock.h       |  2 ++
 net/core/sock.c          | 15 +++++++++++++++
 net/ipv4/tcp_ao.c        |  3 +--
 net/ipv6/exthdrs.c       |  3 +--
 net/mptcp/pm_userspace.c |  3 +--
 net/mptcp/protocol.c     |  7 ++-----
 net/sctp/protocol.c      |  7 ++-----
 7 files changed, 24 insertions(+), 16 deletions(-)
Signature · 1 line
2.43.0
author reviewer
#1 lore raw

PATCH 1/4 sock: add sock_kmemdup helper

From: Geliang Tang <[email protected]>

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.

---
 include/net/sock.h |  2 ++
 net/core/sock.c    | 15 +++++++++++++++
 2 files changed, 17 insertions(+)
include/net/sock.h +2 -0
index efc031163c33..1416c32c4695 100644
@@ -1796,6 +1796,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);
net/core/sock.c +15 -0
index 5ac445f8244b..95e81d24f4cc 100644
@@ -2819,6 +2819,21 @@ 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;
+}
+
 /* 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.
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH net-next 1/4] sock: add sock_kmemdup helper

Hi Geliang,

Quoted 5 lines · Geliang Tang

From: Geliang Tang <[email protected]>

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.

Thank you for suggesting this series.

(...)

Quoted 12 lines
net/core/sock.c +4 -0
index 5ac445f8244b..95e81d24f4cc 100644
@@ -2819,6 +2819,21 @@ 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.
+ */

net/core/sock.c void *sock_kmalloc(struct sock *sk, int size, gfp_t priority) · line 2835

Quoted 10 lines
net/core/sock.c +10 -0
+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;
+}

I think you will need to add an EXPORT_SYMBOL() here, if you plan to use it in SCTP which can be compiled as a module.

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
author reviewer
#3 lore raw

Re: [PATCH net-next 1/4] sock: add sock_kmemdup helper

Quoted 35 lines · Matthieu Baerts

Hi Geliang,

Quoted 7 lines · Geliang Tang

From: Geliang Tang <[email protected]>

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.

Thank you for suggesting this series.

(...)

Quoted 20 lines
net/core/sock.c +0 -0
index 5ac445f8244b..95e81d24f4cc 100644
@@ -2819,6 +2819,21 @@ void *sock_kmalloc(struct sock *sk, int
 size, gfp_t priority)
  }
  EXPORT_SYMBOL(sock_kmalloc);

net/core/sock.c +11 -0
+/*
+ * 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)
Quoted 8 lines

net/core/sock.c void *sock_kmalloc(struct sock *sk, int size, gfp_t priority) · line 2835

Quoted 3 lines
net/core/sock.c +3 -0
+		memcpy(mem, src, size);
+	return mem;
+}

I think you will need to add an EXPORT_SYMBOL() here, if you plan to use
it in SCTP which can be compiled as a module.

Yes, indeed. I'll add this in v2.

Thanks,
-Geliang

Quoted 3 lines

Cheers,
Matt

author reviewer
#4 lore raw

PATCH 2/4 net: use sock_kmemdup for ip_options

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

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.

---
 net/ipv6/exthdrs.c   | 3 +--
 net/mptcp/protocol.c | 7 ++-----
 net/sctp/protocol.c  | 7 ++-----
 3 files changed, 5 insertions(+), 12 deletions(-)
net/ipv6/exthdrs.c +1 -2
index 6789623b2b0d..457de0745a33 100644
@@ -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)
net/mptcp/protocol.c +2 -5
index 6b61b7dee33b..ec23e65ef0f1 100644
@@ -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);
net/sctp/protocol.c +2 -5
index 29727ed1008e..5407a3922101 100644
@@ -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);
Signature · 1 line
2.43.0
author reviewer
#5 lore raw

PATCH 3/4 mptcp: use sock_kmemdup for address entry

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

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.

---
 net/mptcp/pm_userspace.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/mptcp/pm_userspace.c +1 -2
index 6bf6a20ef7f3..7e7d01bef5d4 100644
@@ -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,
Signature · 1 line
2.43.0
author reviewer
#6 lore raw

PATCH 4/4 net/tcp_ao: use sock_kmemdup for tcp_ao_key

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Instead of using sock_kmalloc() to allocate a tcp_ao_key "new_key" and then immediately duplicate the input "key" to it in tcp_ao_copy_key(), the newly added sock_kmemdup() helper can be used to simplify the code.

---
 net/ipv4/tcp_ao.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/ipv4/tcp_ao.c +1 -2
index bbb8d5f0eae7..d21412d469cc 100644
@@ -246,12 +246,11 @@ static struct tcp_ao_key *tcp_ao_copy_key(struct sock *sk,
 {
 	struct tcp_ao_key *new_key;
 
-	new_key = sock_kmalloc(sk, tcp_ao_sizeof_key(key),
+	new_key = sock_kmemdup(sk, key, tcp_ao_sizeof_key(key),
 			       GFP_ATOMIC);
 	if (!new_key)
 		return NULL;
 
-	*new_key = *key;
 	INIT_HLIST_NODE(&new_key->node);
 	tcp_sigpool_get(new_key->tcp_sigpool_id);
 	atomic64_set(&new_key->pkt_good, 0);
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH net-next 4/4] net/tcp_ao: use sock_kmemdup for tcp_ao_key

Quoted 17 lines · Geliang Tang

From: Geliang Tang <[email protected]>

Instead of using sock_kmalloc() to allocate a tcp_ao_key "new_key" and then immediately duplicate the input "key" to it in tcp_ao_copy_key(), the newly added sock_kmemdup() helper can be used to simplify the code.

---
 net/ipv4/tcp_ao.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/ipv4/tcp_ao.c +0 -0
index bbb8d5f0eae7..d21412d469cc 100644

net/ipv4/tcp_ao.c static struct tcp_ao_key *tcp_ao_copy_key(struct sock *sk, · line 254

Quoted 10 lines
net/ipv4/tcp_ao.c +1 -2
@@ -246,12 +246,11 @@ static struct tcp_ao_key *tcp_ao_copy_key(struct sock *sk,
 {
        struct tcp_ao_key *new_key;
 
-       new_key = sock_kmalloc(sk, tcp_ao_sizeof_key(key),
+       new_key = sock_kmemdup(sk, key, tcp_ao_sizeof_key(key),
                               GFP_ATOMIC);
        if (!new_key)
                return NULL;
 
-       *new_key = *key;

Note that this only copies 'sizeof(struct tcp_ao_key)' bytes, which is smaller than tcp_ao_sizeof_key(key)

author reviewer
#8 lore raw

Re: [PATCH net-next 4/4] net/tcp_ao: use sock_kmemdup for tcp_ao_key

Quoted 32 lines · Eric Dumazet
Quoted 30 lines · Geliang Tang

From: Geliang Tang <[email protected]>

Instead of using sock_kmalloc() to allocate a tcp_ao_key "new_key" and
then immediately duplicate the input "key" to it in tcp_ao_copy_key(),
the newly added sock_kmemdup() helper can be used to simplify the code.

---
 net/ipv4/tcp_ao.c | 3 +--

 1 file changed, 1 insertion(+), 2 deletions(-)
net/ipv4/tcp_ao.c +1 -1
index bbb8d5f0eae7..d21412d469cc 100644
@@ -246,12 +246,11 @@ static struct tcp_ao_key
 *tcp_ao_copy_key(struct sock *sk,
  {
         struct tcp_ao_key *new_key;
 
-       new_key = sock_kmalloc(sk, tcp_ao_sizeof_key(key),
+       new_key = sock_kmemdup(sk, key, tcp_ao_sizeof_key(key),
                                GFP_ATOMIC);
         if (!new_key)
                 return NULL;
 
Quoted 5 lines

net/ipv4/tcp_ao.c static struct tcp_ao_key *tcp_ao_copy_key(struct sock *sk, · line 254

Quoted 1 line
net/ipv4/tcp_ao.c +0 -1
-       *new_key = *key;

Note that this only copies 'sizeof(struct tcp_ao_key)' bytes, which is
smaller than tcp_ao_sizeof_key(key)

Yes, indeed. sock_kmemdup() shouldn't be used here then. I'll drop this patch in v2.

Thanks,
-Geliang

#9 lore raw
In reply to Geliang Tang

Hi Geliang,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal: Success! ✅
- KVM Validation: debug: Success! ✅
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Success! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/13562166243

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/24afc58bb11f
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=938382

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)