Skip to contentSkip to conversation

sock: add sock_krfree_s helper

PATCH In review Geliang Tang 2 participants 3 messages Apr 30, 2025 – May 6, 2025

Subsystems (3)
lore
Share & tools
lore

Geliang Tang

author reviewer
#0 lore raw

PATCH 1/1 sock: add sock_krfree_s helper

This patch adds the rcu version of sock_kfree_s() helper, named sock_krfree_s(), to free the memory using kfree_rcu_mightsleep() and adjust the socket's option memory buffer.

This new helper is first used in MPTCP.

---
 include/net/sock.h       |  1 +
 net/core/sock.c          | 14 +++++++++++---
 net/mptcp/pm_userspace.c |  3 +--
 3 files changed, 13 insertions(+), 5 deletions(-)
include/net/sock.h +1 -0
index e223102337c7..94f72f5c8754 100644
@@ -1836,6 +1836,7 @@ 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 sock_krfree_s(struct sock *sk, void *mem, int size);
 void sk_send_sigurg(struct sock *sk);
 
 static inline void sock_replace_proto(struct sock *sk, struct proto *proto)
net/core/sock.c +11 -3
index b64df2463300..f9ee0b44ab2a 100644
@@ -2862,12 +2862,14 @@ EXPORT_SYMBOL(sock_kmemdup);
  * condition entirely.
  */
 static inline void __sock_kfree_s(struct sock *sk, void *mem, int size,
-				  const bool nullify)
+				  const bool nullify, const bool rcu)
 {
 	if (WARN_ON_ONCE(!mem))
 		return;
 	if (nullify)
 		kfree_sensitive(mem);
+	else if (rcu)
+		kfree_rcu_mightsleep(mem);
 	else
 		kfree(mem);
 	atomic_sub(size, &sk->sk_omem_alloc);
@@ -2875,16 +2877,22 @@ static inline void __sock_kfree_s(struct sock *sk, void *mem, int size,
 
 void sock_kfree_s(struct sock *sk, void *mem, int size)
 {
-	__sock_kfree_s(sk, mem, size, false);
+	__sock_kfree_s(sk, mem, size, false, false);
 }
 EXPORT_SYMBOL(sock_kfree_s);
 
 void sock_kzfree_s(struct sock *sk, void *mem, int size)
 {
-	__sock_kfree_s(sk, mem, size, true);
+	__sock_kfree_s(sk, mem, size, true, false);
 }
 EXPORT_SYMBOL(sock_kzfree_s);
 
+void sock_krfree_s(struct sock *sk, void *mem, int size)
+{
+	__sock_kfree_s(sk, mem, size, false, true);
+}
+EXPORT_SYMBOL(sock_krfree_s);
+
 /* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
    I think, these locks should be removed for datagram sockets.
  */
net/mptcp/pm_userspace.c +1 -2
index 1911fe1799fa..60ddf4889c51 100644
@@ -337,11 +337,10 @@ int mptcp_pm_nl_remove_doit(struct sk_buff *skb, struct genl_info *info)
 
 	release_sock(sk);
 
-	kfree_rcu_mightsleep(match);
 	/* Adjust sk_omem_alloc like sock_kfree_s() does, to match
 	 * with allocation of this memory by sock_kmemdup()
 	 */
-	atomic_sub(sizeof(*match), &sk->sk_omem_alloc);
+	sock_krfree_s(sk, match, sizeof(*match));
 
 	err = 0;
 out:
Signature · 1 line
2.43.0
maintainer
#1 lore raw

Hi Geliang,

Quoted 32 lines · Geliang Tang

This patch adds the rcu version of sock_kfree_s() helper, named sock_krfree_s(), to free the memory using kfree_rcu_mightsleep() and adjust the socket's option memory buffer.

This new helper is first used in MPTCP.

---
 include/net/sock.h       |  1 +
 net/core/sock.c          | 14 +++++++++++---
 net/mptcp/pm_userspace.c |  3 +--
 3 files changed, 13 insertions(+), 5 deletions(-)
include/net/sock.h +1 -0
index e223102337c7..94f72f5c8754 100644
@@ -1836,6 +1836,7 @@ 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 sock_krfree_s(struct sock *sk, void *mem, int size);
 void sk_send_sigurg(struct sock *sk);
 
 static inline void sock_replace_proto(struct sock *sk, struct proto *proto)
net/core/sock.c +0 -0
index b64df2463300..f9ee0b44ab2a 100644
@@ -2862,12 +2862,14 @@ EXPORT_SYMBOL(sock_kmemdup);
  * condition entirely.
  */

net/core/sock.c EXPORT_SYMBOL(sock_kmemdup); · line 2872

Quoted 10 lines
net/core/sock.c +3 -1
 static inline void __sock_kfree_s(struct sock *sk, void *mem, int size,
-				  const bool nullify)
+				  const bool nullify, const bool rcu)
 {
 	if (WARN_ON_ONCE(!mem))
 		return;
 	if (nullify)
 		kfree_sensitive(mem);
+	else if (rcu)
+		kfree_rcu_mightsleep(mem);

I'm not sure about that: in some cases, kfree_rcu() might be called. kfree_rcu_mightsleep() is more an exception to me, I think. Also, it looks like there are many other places where the free and atomic_sub() is done "manually", that's not specific to MPTCP.

I don't know what Mat thinks about that, but I don't think this is needed (or it should replace some code in different places in the kernel).

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
author reviewer
#2 lore raw

Hi Matt,

Quoted 56 lines · Matthieu Baerts

Hi Geliang,

Quoted 44 lines · Geliang Tang

This patch adds the rcu version of sock_kfree_s() helper, named sock_krfree_s(), to free the memory using kfree_rcu_mightsleep() and adjust the socket's option memory buffer.

This new helper is first used in MPTCP.

---
 include/net/sock.h       |  1 +
 net/core/sock.c          | 14 +++++++++++---
 net/mptcp/pm_userspace.c |  3 +--

 3 files changed, 13 insertions(+), 5 deletions(-)
include/net/sock.h +1 -0
index e223102337c7..94f72f5c8754 100644
@@ -1836,6 +1836,7 @@ 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 sock_krfree_s(struct sock *sk, void *mem, int size);
  void sk_send_sigurg(struct sock *sk);


 static inline void sock_replace_proto(struct sock *sk, struct proto *proto)

net/core/sock.c +0 -0
index b64df2463300..f9ee0b44ab2a 100644
@@ -2862,12 +2862,14 @@ EXPORT_SYMBOL(sock_kmemdup);
   * condition entirely.
   */

 static inline void __sock_kfree_s(struct sock *sk, void *mem, int size,

net/core/sock.c +1 -1
-				  const bool nullify)
+				  const bool nullify, const bool

rcu)
 {
  if (WARN_ON_ONCE(!mem))
  return;
  if (nullify)
  kfree_sensitive(mem);

net/core/sock.c EXPORT_SYMBOL(sock_kmemdup); · line 2872

Quoted 2 lines
net/core/sock.c +2 -0
+	else if (rcu)
+		kfree_rcu_mightsleep(mem);

I'm not sure about that: in some cases, kfree_rcu() might be called. kfree_rcu_mightsleep() is more an exception to me, I think. Also, it looks like there are many other places where the free and atomic_sub()
is done "manually", that's not specific to MPTCP.

Quoted 3 lines

I don't know what Mat thinks about that, but I don't think this is needed (or it should replace some code in different places in the kernel).

Indeed, thanks for your reminder. I didn't notice that there are many other places that use free() and atomic_sub() too. I will reconsider whether to drop this patch or replace some code in different places.

Thanks,
-Geliang

Quoted 3 lines

Cheers,
Matt