Skip to content

add sock_kmemdup helper

Geliang Tang linux-sctp mptcp netdev 2 versions – Applied

Status from this message: This series was applied to netdev/net-next.git (main)

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest3 patches 4 repliesApplied 3/3
  2. v14 patches 5 repliesSuperseded

Changes in v2 from the cover letter

- add "EXPORT_SYMBOL(sock_kmemdup)" as Matthieu suggested.
- drop the patch "use sock_kmemdup for tcp_ao_key".

Patches in v2 All 3 applied

#Subject and filesSizeRepliesApplied
1/3sock: add sock_kmemdup helperinclude/net/sock.h net/core/sock.c+18 -01 replyApplied
2/3net: use sock_kmemdup for ip_optionsnet/ipv6/exthdrs.c net/mptcp/protocol.c net/sctp/protocol.c+5 -121 replyApplied
3/3mptcp: use sock_kmemdup for address entrynet/mptcp/pm_userspace.c+1 -20 repliesApplied

Reviews trailers per patch

2 of 3 patches have a Reviewed-by or Acked-by of their own; the series has one for all of them.

PatchMatthieu Baerts (NGI0)Kuniyuki Iwashima
SeriesA-b
1/3R-b
2/3R-b
3/3

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH net-next v2 0/3] add sock_kmemdup helper

From: Geliang Tang <[email protected]>

v2:
- add "EXPORT_SYMBOL(sock_kmemdup)" as Matthieu suggested.
- drop the patch "use sock_kmemdup for tcp_ao_key".

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), it 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:

'''
@@ 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://lore.kernel.org/mptcp/[email protected]/

Geliang Tang (3):
sock: add sock_kmemdup helper
net: use sock_kmemdup for ip_options
mptcp: use sock_kmemdup for address entry

 include/net/sock.h       |  2 ++
 net/core/sock.c          | 16 ++++++++++++++++
 net/ipv6/exthdrs.c       |  3 +--
 net/mptcp/pm_userspace.c |  3 +--
 net/mptcp/protocol.c     |  7 ++-----
 net/sctp/protocol.c      |  7 ++-----
 6 files changed, 24 insertions(+), 14 deletions(-)
Signature · 1 line
2.43.0