linux-sctp.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Geliang Tang <geliang@kernel.org>
To: Eric Dumazet <edumazet@google.com>,
	Kuniyuki Iwashima <kuniyu@amazon.com>,
	Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	"David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>, Simon Horman <horms@kernel.org>,
	Neal Cardwell <ncardwell@google.com>,
	David Ahern <dsahern@kernel.org>,
	Matthieu Baerts <matttbe@kernel.org>,
	Mat Martineau <martineau@kernel.org>,
	Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>,
	Xin Long <lucien.xin@gmail.com>
Cc: Geliang Tang <tanggeliang@kylinos.cn>,
	netdev@vger.kernel.org, mptcp@lists.linux.dev,
	linux-sctp@vger.kernel.org
Subject: [PATCH net-next 0/4] add sock_kmemdup helper
Date: Thu, 27 Feb 2025 16:23:22 +0800	[thread overview]
Message-ID: <cover.1740643844.git.tanggeliang@kylinos.cn> (raw)

From: Geliang Tang <tanggeliang@kylinos.cn>

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

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(-)

-- 
2.43.0


             reply	other threads:[~2025-02-27  8:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-27  8:23 Geliang Tang [this message]
2025-02-27  8:23 ` [PATCH net-next 1/4] sock: add sock_kmemdup helper Geliang Tang
2025-02-27  8:45   ` Matthieu Baerts
2025-02-27  9:05     ` Geliang Tang
2025-02-27  8:23 ` [PATCH net-next 2/4] net: use sock_kmemdup for ip_options Geliang Tang
2025-02-27  8:23 ` [PATCH net-next 3/4] mptcp: use sock_kmemdup for address entry Geliang Tang
2025-02-27  8:23 ` [PATCH net-next 4/4] net/tcp_ao: use sock_kmemdup for tcp_ao_key Geliang Tang
2025-02-27  8:35   ` Eric Dumazet
2025-02-27  9:04     ` Geliang Tang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cover.1740643844.git.tanggeliang@kylinos.cn \
    --to=geliang@kernel.org \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@amazon.com \
    --cc=linux-sctp@vger.kernel.org \
    --cc=lucien.xin@gmail.com \
    --cc=marcelo.leitner@gmail.com \
    --cc=martineau@kernel.org \
    --cc=matttbe@kernel.org \
    --cc=mptcp@lists.linux.dev \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=tanggeliang@kylinos.cn \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).