[PATCH net-next 1/4] sock: add sock_kmemdup helper

Geliang Tang posted 4 patches 2 months, 2 weeks ago
There is a newer version of this series
[PATCH net-next 1/4] sock: add sock_kmemdup helper
Posted by Geliang Tang 2 months, 2 weeks ago
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
Re: [PATCH net-next 1/4] sock: add sock_kmemdup helper
Posted by Matthieu Baerts 2 months, 2 weeks ago
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.
Re: [PATCH net-next 1/4] sock: add sock_kmemdup helper
Posted by Geliang Tang 2 months, 2 weeks ago
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