Jakub Kicinski cd170ca993 Merge branch 'add-sock_kmemdup-helper'
Geliang Tang says:

====================
add sock_kmemdup helper

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/cover.1738924875.git.tanggeliang@kylinos.cn/
====================

Link: https://patch.msgid.link/cover.1740735165.git.tanggeliang@kylinos.cn
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2025-03-03 17:16:36 -08:00
2025-03-03 17:16:34 -08:00
2024-09-01 20:43:24 -07:00
2025-02-04 11:27:45 -05:00
2025-02-17 22:40:03 -08:00
2022-09-28 09:02:20 +02:00
2025-02-23 12:32:57 -08:00
2024-03-18 03:36:32 -06:00

Linux kernel
============

There are several guides for kernel developers and users. These guides can
be rendered in a number of formats, like HTML and PDF. Please read
Documentation/admin-guide/README.rst first.

In order to build the documentation, use ``make htmldocs`` or
``make pdfdocs``.  The formatted documentation can also be read online at:

    https://www.kernel.org/doc/html/latest/

There are various text files in the Documentation/ subdirectory,
several of them using the reStructuredText markup notation.

Please read the Documentation/process/changes.rst file, as it contains the
requirements for building and running the kernel, and information about
the problems which may result by upgrading your kernel.
Description
No description provided
Readme 3.4 GiB
Languages
C 97%
Assembly 1%
Shell 0.6%
Rust 0.5%
Python 0.4%
Other 0.3%