* [PATCH net-next 1/4] sock: add sock_kmemdup helper
2025-02-27 8:23 [PATCH net-next 0/4] add sock_kmemdup helper Geliang Tang
@ 2025-02-27 8:23 ` Geliang Tang
2025-02-27 8:45 ` Matthieu Baerts
2025-02-27 8:23 ` [PATCH net-next 2/4] net: use sock_kmemdup for ip_options Geliang Tang
` (2 subsequent siblings)
3 siblings, 1 reply; 9+ messages in thread
From: Geliang Tang @ 2025-02-27 8:23 UTC (permalink / raw)
To: Eric Dumazet, Kuniyuki Iwashima, Paolo Abeni, Willem de Bruijn,
David S. Miller, Jakub Kicinski, Simon Horman, Neal Cardwell,
David Ahern, Matthieu Baerts, Mat Martineau,
Marcelo Ricardo Leitner, Xin Long
Cc: Geliang Tang, netdev, mptcp, linux-sctp
From: Geliang Tang <tanggeliang@kylinos.cn>
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.
Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
include/net/sock.h | 2 ++
net/core/sock.c | 15 +++++++++++++++
2 files changed, 17 insertions(+)
diff --git a/include/net/sock.h b/include/net/sock.h
index efc031163c33..1416c32c4695 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -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);
diff --git a/net/core/sock.c b/net/core/sock.c
index 5ac445f8244b..95e81d24f4cc 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -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.
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH net-next 1/4] sock: add sock_kmemdup helper
2025-02-27 8:23 ` [PATCH net-next 1/4] sock: " Geliang Tang
@ 2025-02-27 8:45 ` Matthieu Baerts
2025-02-27 9:05 ` Geliang Tang
0 siblings, 1 reply; 9+ messages in thread
From: Matthieu Baerts @ 2025-02-27 8:45 UTC (permalink / raw)
To: Geliang Tang
Cc: Geliang Tang, netdev, mptcp, linux-sctp, Eric Dumazet,
Kuniyuki Iwashima, Paolo Abeni, Willem de Bruijn, Jakub Kicinski,
David S. Miller, Simon Horman, David Ahern, Neal Cardwell,
Mat Martineau, Marcelo Ricardo Leitner, Xin Long
Hi Geliang,
On 27/02/2025 09:23, Geliang Tang wrote:
> From: Geliang Tang <tanggeliang@kylinos.cn>
>
> 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.
(...)
> diff --git a/net/core/sock.c b/net/core/sock.c
> index 5ac445f8244b..95e81d24f4cc 100644
> --- a/net/core/sock.c
> +++ b/net/core/sock.c
> @@ -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;
> +}
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
--
Sponsored by the NGI0 Core fund.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH net-next 1/4] sock: add sock_kmemdup helper
2025-02-27 8:45 ` Matthieu Baerts
@ 2025-02-27 9:05 ` Geliang Tang
0 siblings, 0 replies; 9+ messages in thread
From: Geliang Tang @ 2025-02-27 9:05 UTC (permalink / raw)
To: Matthieu Baerts
Cc: Geliang Tang, netdev, mptcp, linux-sctp, Eric Dumazet,
Kuniyuki Iwashima, Paolo Abeni, Willem de Bruijn, Jakub Kicinski,
David S. Miller, Simon Horman, David Ahern, Neal Cardwell,
Mat Martineau, Marcelo Ricardo Leitner, Xin Long
On Thu, 2025-02-27 at 09:45 +0100, Matthieu Baerts wrote:
> Hi Geliang,
>
> On 27/02/2025 09:23, Geliang Tang wrote:
> > From: Geliang Tang <tanggeliang@kylinos.cn>
> >
> > 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.
>
> (...)
>
> > diff --git a/net/core/sock.c b/net/core/sock.c
> > index 5ac445f8244b..95e81d24f4cc 100644
> > --- a/net/core/sock.c
> > +++ b/net/core/sock.c
> > @@ -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;
> > +}
>
>
> 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
>
> Cheers,
> Matt
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH net-next 2/4] net: use sock_kmemdup for ip_options
2025-02-27 8:23 [PATCH net-next 0/4] add sock_kmemdup helper Geliang Tang
2025-02-27 8:23 ` [PATCH net-next 1/4] sock: " Geliang Tang
@ 2025-02-27 8:23 ` 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
3 siblings, 0 replies; 9+ messages in thread
From: Geliang Tang @ 2025-02-27 8:23 UTC (permalink / raw)
To: Eric Dumazet, Kuniyuki Iwashima, Paolo Abeni, Willem de Bruijn,
David S. Miller, Jakub Kicinski, Simon Horman, Neal Cardwell,
David Ahern, Matthieu Baerts, Mat Martineau,
Marcelo Ricardo Leitner, Xin Long
Cc: Geliang Tang, netdev, mptcp, linux-sctp
From: Geliang Tang <tanggeliang@kylinos.cn>
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.
Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
net/ipv6/exthdrs.c | 3 +--
net/mptcp/protocol.c | 7 ++-----
net/sctp/protocol.c | 7 ++-----
3 files changed, 5 insertions(+), 12 deletions(-)
diff --git a/net/ipv6/exthdrs.c b/net/ipv6/exthdrs.c
index 6789623b2b0d..457de0745a33 100644
--- a/net/ipv6/exthdrs.c
+++ b/net/ipv6/exthdrs.c
@@ -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)
diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c
index 6b61b7dee33b..ec23e65ef0f1 100644
--- a/net/mptcp/protocol.c
+++ b/net/mptcp/protocol.c
@@ -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);
diff --git a/net/sctp/protocol.c b/net/sctp/protocol.c
index 29727ed1008e..5407a3922101 100644
--- a/net/sctp/protocol.c
+++ b/net/sctp/protocol.c
@@ -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);
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH net-next 3/4] mptcp: use sock_kmemdup for address entry
2025-02-27 8:23 [PATCH net-next 0/4] add sock_kmemdup helper Geliang Tang
2025-02-27 8:23 ` [PATCH net-next 1/4] sock: " 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 ` Geliang Tang
2025-02-27 8:23 ` [PATCH net-next 4/4] net/tcp_ao: use sock_kmemdup for tcp_ao_key Geliang Tang
3 siblings, 0 replies; 9+ messages in thread
From: Geliang Tang @ 2025-02-27 8:23 UTC (permalink / raw)
To: Eric Dumazet, Kuniyuki Iwashima, Paolo Abeni, Willem de Bruijn,
David S. Miller, Jakub Kicinski, Simon Horman, Neal Cardwell,
David Ahern, Matthieu Baerts, Mat Martineau,
Marcelo Ricardo Leitner, Xin Long
Cc: Geliang Tang, netdev, mptcp, linux-sctp
From: Geliang Tang <tanggeliang@kylinos.cn>
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.
Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
net/mptcp/pm_userspace.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/net/mptcp/pm_userspace.c b/net/mptcp/pm_userspace.c
index 6bf6a20ef7f3..7e7d01bef5d4 100644
--- a/net/mptcp/pm_userspace.c
+++ b/net/mptcp/pm_userspace.c
@@ -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,
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH net-next 4/4] net/tcp_ao: use sock_kmemdup for tcp_ao_key
2025-02-27 8:23 [PATCH net-next 0/4] add sock_kmemdup helper Geliang Tang
` (2 preceding siblings ...)
2025-02-27 8:23 ` [PATCH net-next 3/4] mptcp: use sock_kmemdup for address entry Geliang Tang
@ 2025-02-27 8:23 ` Geliang Tang
2025-02-27 8:35 ` Eric Dumazet
3 siblings, 1 reply; 9+ messages in thread
From: Geliang Tang @ 2025-02-27 8:23 UTC (permalink / raw)
To: Eric Dumazet, Kuniyuki Iwashima, Paolo Abeni, Willem de Bruijn,
David S. Miller, Jakub Kicinski, Simon Horman, Neal Cardwell,
David Ahern, Matthieu Baerts, Mat Martineau,
Marcelo Ricardo Leitner, Xin Long
Cc: Geliang Tang, netdev, mptcp, linux-sctp
From: Geliang Tang <tanggeliang@kylinos.cn>
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.
Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
net/ipv4/tcp_ao.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c
index bbb8d5f0eae7..d21412d469cc 100644
--- a/net/ipv4/tcp_ao.c
+++ b/net/ipv4/tcp_ao.c
@@ -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);
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH net-next 4/4] net/tcp_ao: use sock_kmemdup for tcp_ao_key
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
0 siblings, 1 reply; 9+ messages in thread
From: Eric Dumazet @ 2025-02-27 8:35 UTC (permalink / raw)
To: Geliang Tang
Cc: Kuniyuki Iwashima, Paolo Abeni, Willem de Bruijn, David S. Miller,
Jakub Kicinski, Simon Horman, Neal Cardwell, David Ahern,
Matthieu Baerts, Mat Martineau, Marcelo Ricardo Leitner, Xin Long,
Geliang Tang, netdev, mptcp, linux-sctp
On Thu, Feb 27, 2025 at 9:24 AM Geliang Tang <geliang@kernel.org> wrote:
>
> From: Geliang Tang <tanggeliang@kylinos.cn>
>
> 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.
>
> Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
> ---
> net/ipv4/tcp_ao.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c
> index bbb8d5f0eae7..d21412d469cc 100644
> --- a/net/ipv4/tcp_ao.c
> +++ b/net/ipv4/tcp_ao.c
> @@ -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)
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH net-next 4/4] net/tcp_ao: use sock_kmemdup for tcp_ao_key
2025-02-27 8:35 ` Eric Dumazet
@ 2025-02-27 9:04 ` Geliang Tang
0 siblings, 0 replies; 9+ messages in thread
From: Geliang Tang @ 2025-02-27 9:04 UTC (permalink / raw)
To: Eric Dumazet
Cc: Kuniyuki Iwashima, Paolo Abeni, Willem de Bruijn, David S. Miller,
Jakub Kicinski, Simon Horman, Neal Cardwell, David Ahern,
Matthieu Baerts, Mat Martineau, Marcelo Ricardo Leitner, Xin Long,
Geliang Tang, netdev, mptcp, linux-sctp
On Thu, 2025-02-27 at 09:35 +0100, Eric Dumazet wrote:
> On Thu, Feb 27, 2025 at 9:24 AM Geliang Tang <geliang@kernel.org>
> wrote:
> >
> > From: Geliang Tang <tanggeliang@kylinos.cn>
> >
> > 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.
> >
> > Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
> > ---
> > net/ipv4/tcp_ao.c | 3 +--
> > 1 file changed, 1 insertion(+), 2 deletions(-)
> >
> > diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c
> > index bbb8d5f0eae7..d21412d469cc 100644
> > --- a/net/ipv4/tcp_ao.c
> > +++ b/net/ipv4/tcp_ao.c
> > @@ -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)
Yes, indeed. sock_kmemdup() shouldn't be used here then. I'll drop this
patch in v2.
Thanks,
-Geliang
^ permalink raw reply [flat|nested] 9+ messages in thread