mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-05-09 04:21:03 -04:00
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>
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user