sock: add sock_kmemdup helper
authorGeliang Tang <tanggeliang@kylinos.cn>
Fri, 28 Feb 2025 10:01:31 +0000 (18:01 +0800)
committerJakub Kicinski <kuba@kernel.org>
Tue, 4 Mar 2025 01:16:34 +0000 (17:16 -0800)
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>
Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Acked-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
Link: https://patch.msgid.link/f828077394c7d1f3560123497348b438c875b510.1740735165.git.tanggeliang@kylinos.cn
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/net/sock.h
net/core/sock.c

index e771d99f81b0def2ee9bfa6d61da133fb56ef6be..8daf1b3b12c607d81920682139b53fee935c9bb5 100644 (file)
@@ -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);
index 23bce41f7f1ffc9cfb456f311d0f467bd5952dd7..a0598518ce898f53825f15ec78249103a3ff8306 100644 (file)
@@ -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.