Skip to contentSkip to conversation

tcp: use 2-arg optimal variant of kfree_rcu()

PATCH Applied Eric Dumazet 7 participants 15 messages Dec 2, 2022 – Dec 5, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tcp: use 2-arg optimal variant of kfree_rcu()

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would
get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

Fixes 459837b522f7 ("net/tcp: Disable TCP-MD5 static key on tcp_md5sig_info destruction")
Cc Dmitry Safonov <[email protected]>
Cc Paul E. McKenney <[email protected]>
---
 net/ipv4/tcp_ipv4.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp_ipv4.c +2 -2
index 7fae586405cfb10011a0674289280bf400dfa8d8..8320d0ecb13ae1e3e259f3c13a4c2797fbd984a5 100644
@@ -1245,7 +1245,7 @@ int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
 
 			md5sig = rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_held(sk));
 			rcu_assign_pointer(tp->md5sig_info, NULL);
-			kfree_rcu(md5sig);
+			kfree_rcu(md5sig, rcu);
 			return -EUSERS;
 		}
 	}
@@ -1271,7 +1271,7 @@ int tcp_md5_key_copy(struct sock *sk, const union tcp_md5_addr *addr,
 			md5sig = rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_held(sk));
 			net_warn_ratelimited("Too many TCP-MD5 keys in the system\n");
 			rcu_assign_pointer(tp->md5sig_info, NULL);
-			kfree_rcu(md5sig);
+			kfree_rcu(md5sig, rcu);
 			return -EUSERS;
 		}
 	}
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 40 lines · Eric Dumazet

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would
get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

Fixes 459837b522f7 ("net/tcp: Disable TCP-MD5 static key on tcp_md5sig_info destruction")
Cc Dmitry Safonov <[email protected]>
Cc Paul E. McKenney <[email protected]>
---
 net/ipv4/tcp_ipv4.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp_ipv4.c +2 -3
index 7fae586405cfb10011a0674289280bf400dfa8d8..8320d0ecb13ae1e3e259f3c13a4c2797fbd984a5 100644
@@ -1245,7 +1245,7 @@ int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
 
                        md5sig = rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_held(sk));
                        rcu_assign_pointer(tp->md5sig_info, NULL);
-                       kfree_rcu(md5sig);
+                       kfree_rcu(md5sig, rcu);
                        return -EUSERS;
                }
        }
@@ -1271,7 +1271,7 @@ int tcp_md5_key_copy(struct sock *sk, const union tcp_md5_addr *addr,
                        md5sig = rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_held(sk));
                        net_warn_ratelimited("Too many TCP-MD5 keys in the system\n");
                        rcu_assign_pointer(tp->md5sig_info, NULL);
-                       kfree_rcu(md5sig);
+                       kfree_rcu(md5sig, rcu);
                        return -EUSERS;
                }
        }
--
Quoted 2 lines

2.39.0.rc0.267.gcb52ba06e7-goog

Looks good to me.

Reviewed-by on v1 1/1
#2 lore raw
In reply to Eric Dumazet
Quoted 7 lines · Eric Dumazet

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would
get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

Thanks again!

Quoted 4 lines
Fixes 459837b522f7 ("net/tcp: Disable TCP-MD5 static key on tcp_md5sig_info destruction")
Cc Dmitry Safonov <[email protected]>
Cc Paul E. McKenney <[email protected]>
Quoted 26 lines
---
 net/ipv4/tcp_ipv4.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp_ipv4.c +2 -2
index 7fae586405cfb10011a0674289280bf400dfa8d8..8320d0ecb13ae1e3e259f3c13a4c2797fbd984a5 100644
@@ -1245,7 +1245,7 @@ int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
 
 			md5sig = rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_held(sk));
 			rcu_assign_pointer(tp->md5sig_info, NULL);
-			kfree_rcu(md5sig);
+			kfree_rcu(md5sig, rcu);
 			return -EUSERS;
 		}
 	}
@@ -1271,7 +1271,7 @@ int tcp_md5_key_copy(struct sock *sk, const union tcp_md5_addr *addr,
 			md5sig = rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_held(sk));
 			net_warn_ratelimited("Too many TCP-MD5 keys in the system\n");
 			rcu_assign_pointer(tp->md5sig_info, NULL);
-			kfree_rcu(md5sig);
+			kfree_rcu(md5sig, rcu);
 			return -EUSERS;
 		}
 	}
Signature · 1 line
          Dmitry
maintainer
#3 lore raw
In reply to Eric Dumazet
Quoted 7 lines · Eric Dumazet

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would
get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

Fair point, the bug of forgetting the second argument goes unflagged, at least from contexts where blocking is OK.

Let the bikeshedding commence! ;-)

Thanx, Paul

Quoted 33 lines
Fixes 459837b522f7 ("net/tcp: Disable TCP-MD5 static key on tcp_md5sig_info destruction")
Cc Dmitry Safonov <[email protected]>
Cc Paul E. McKenney <[email protected]>
---
 net/ipv4/tcp_ipv4.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp_ipv4.c +2 -2
index 7fae586405cfb10011a0674289280bf400dfa8d8..8320d0ecb13ae1e3e259f3c13a4c2797fbd984a5 100644
@@ -1245,7 +1245,7 @@ int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
 
 			md5sig = rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_held(sk));
 			rcu_assign_pointer(tp->md5sig_info, NULL);
-			kfree_rcu(md5sig);
+			kfree_rcu(md5sig, rcu);
 			return -EUSERS;
 		}
 	}
@@ -1271,7 +1271,7 @@ int tcp_md5_key_copy(struct sock *sk, const union tcp_md5_addr *addr,
 			md5sig = rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_held(sk));
 			net_warn_ratelimited("Too many TCP-MD5 keys in the system\n");
 			rcu_assign_pointer(tp->md5sig_info, NULL);
-			kfree_rcu(md5sig);
+			kfree_rcu(md5sig, rcu);
 			return -EUSERS;
 		}
 	}
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
#4 lore raw
In reply to Eric Dumazet
Quoted 7 lines · Eric Dumazet

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

Hi Eric,
Nice to see your patch.

Paul, all, regarding Eric's concern, would the following work to warn of users? Credit to Paul/others for discussing the idea on another thread. One thing to note here is, this debugging will only be in effect on preemptible kernels, but should still help catch issues hopefully.

The other idea Paul mentioned is to introduce a new dedicated API for 1-arg sleepable cases. My concern with that was that, that being effective depends on the user using the right API in the first place.

I did not test it yet, but wanted to discuss a bit first.

Cheers,

- Joel

---8<-----------------------

include/linux/rcutiny.h +5 -0
index 9bc025aa79a3..112d230279ea 100644
@@ -106,6 +106,11 @@ static inline void __kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
 	}
 
 	// kvfree_rcu(one_arg) call.
+	if (IS_ENABLED(CONFIG_PREEMPT_COUNT) && preemptible() && !head) {
+		WARN_ONCE(1, "%s(): Please provide an rcu_head in preemptible"
+			  " contexts to avoid long waits!\n", __func__);
+	}
+
 	might_sleep();
 	synchronize_rcu();
 	kvfree((void *) func);
kernel/rcu/tree.c +5 -0
index 0ca21ac0f064..b29df1305a2e 100644
@@ -3324,6 +3324,11 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
 		 * only. For other places please embed an rcu_head to
 		 * your data.
 		 */
+		if (IS_ENABLED(CONFIG_PREEMPT_COUNT) && preemptible() && !head) {
+			WARN_ONCE(1, "%s(): Please provide an rcu_head in preemptible"
+				  " contexts to avoid long waits!\n", __func__);
+		}
+
 		might_sleep();
 		ptr = (unsigned long *) func;
 	}
maintainer
#5 lore raw
Quoted 9 lines · Joel Fernandes
Quoted 7 lines · Eric Dumazet

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

Quoted 7 lines

Hi Eric,
Nice to see your patch.

Paul, all, regarding Eric's concern, would the following work to warn of users? Credit to Paul/others for discussing the idea on another thread. One thing to note here is, this debugging will only be in effect on preemptible kernels, but should still help catch issues hopefully.

Mightn't there be some places where someone needs to invoke single-argument kfree_rcu() in a preemptible context, for example, due to the RCU-protected structure being very small and very numerous?

Quoted 3 lines

The other idea Paul mentioned is to introduce a new dedicated API for 1-arg sleepable cases. My concern with that was that, that being effective depends on the user using the right API in the first place.

Actually, Eric's idea from above. ;-)

Thanx, Paul

Quoted 40 lines

I did not test it yet, but wanted to discuss a bit first.

Cheers,

- Joel

---8<-----------------------

include/linux/rcutiny.h +5 -0
index 9bc025aa79a3..112d230279ea 100644
@@ -106,6 +106,11 @@ static inline void __kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
 	}
 
 	// kvfree_rcu(one_arg) call.
+	if (IS_ENABLED(CONFIG_PREEMPT_COUNT) && preemptible() && !head) {
+		WARN_ONCE(1, "%s(): Please provide an rcu_head in preemptible"
+			  " contexts to avoid long waits!\n", __func__);
+	}
+
 	might_sleep();
 	synchronize_rcu();
 	kvfree((void *) func);
kernel/rcu/tree.c +5 -0
index 0ca21ac0f064..b29df1305a2e 100644
@@ -3324,6 +3324,11 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
 		 * only. For other places please embed an rcu_head to
 		 * your data.
 		 */
+		if (IS_ENABLED(CONFIG_PREEMPT_COUNT) && preemptible() && !head) {
+			WARN_ONCE(1, "%s(): Please provide an rcu_head in preemptible"
+				  " contexts to avoid long waits!\n", __func__);
+		}
+
 		might_sleep();
 		ptr = (unsigned long *) func;
 	}
#6 lore raw
Quoted 14 lines · Paul E. McKenney
Quoted 12 lines · Joel Fernandes
Quoted 7 lines · Eric Dumazet

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

Hi Eric,
Nice to see your patch.

Quoted 8 lines
Quoted 4 lines

Paul, all, regarding Eric's concern, would the following work to warn of users? Credit to Paul/others for discussing the idea on another thread. One thing to note here is, this debugging will only be in effect on preemptible kernels, but should still help catch issues hopefully.

Mightn't there be some places where someone needs to invoke single-argument kfree_rcu() in a preemptible context, for example, due to the RCU-protected structure being very small and very numerous?

This could be possible but I am not able to find examples of such cases, at the moment. Another approach could be to introduce a dedicated API for such cases, where the warning will not fire. And keep the warning otherwise.

Example: kfree_rcu_headless()
With a big comment saying, use only if you are calling from a preemptible context and cannot absolutely embed an rcu_head. :-)

Thoughts?

Cheers,

- Joel

#7 lore raw
Quoted 30 lines · Joel Fernandes
Quoted 14 lines · Paul E. McKenney
Quoted 12 lines · Joel Fernandes
Quoted 7 lines · Eric Dumazet

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

Hi Eric,
Nice to see your patch.

Quoted 8 lines
Quoted 4 lines

Paul, all, regarding Eric's concern, would the following work to warn of users? Credit to Paul/others for discussing the idea on another thread. One thing to note here is, this debugging will only be in effect on preemptible kernels, but should still help catch issues hopefully.

Mightn't there be some places where someone needs to invoke single-argument kfree_rcu() in a preemptible context, for example, due to the RCU-protected structure being very small and very numerous?

This could be possible but I am not able to find examples of such cases, at the moment. Another approach could be to introduce a dedicated API for such cases, where the warning will not fire. And keep the warning otherwise.

Quoted 6 lines

Example: kfree_rcu_headless()
With a big comment saying, use only if you are calling from a preemptible context and cannot absolutely embed an rcu_head. :-)

Thoughts?

Just to clarify, where I was getting at was to combine both ideas:
1. new API with suppression of the new warning mentioned above.
2. old API but add new warning mentioned above.

Cheers,

- Joel

#8 lore raw

+rcu for archives

Quoted 47 lines · Joel Fernandes
Quoted 30 lines · Joel Fernandes
Quoted 16 lines · Paul E. McKenney
Quoted 12 lines · Joel Fernandes
Quoted 7 lines · Eric Dumazet

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

Hi Eric,
Nice to see your patch.

Quoted 8 lines
Quoted 4 lines

Paul, all, regarding Eric's concern, would the following work to warn of users? Credit to Paul/others for discussing the idea on another thread. One thing to note here is, this debugging will only be in effect on preemptible kernels, but should still help catch issues hopefully.

Mightn't there be some places where someone needs to invoke single-argument kfree_rcu() in a preemptible context, for example, due to the RCU-protected structure being very small and very numerous?

This could be possible but I am not able to find examples of such cases, at the moment. Another approach could be to introduce a dedicated API for such cases, where the warning will not fire. And keep the warning otherwise.

Quoted 6 lines

Example: kfree_rcu_headless()
With a big comment saying, use only if you are calling from a preemptible context and cannot absolutely embed an rcu_head. :-)

Thoughts?

Just to clarify, where I was getting at was to combine both ideas:
1. new API with suppression of the new warning mentioned above.
2. old API but add new warning mentioned above.

Cheers,

- Joel

maintainer
#9 lore raw

Hello, Eric.

Quoted 18 lines

+rcu for archives

Quoted 15 lines · Joel Fernandes
Quoted 12 lines · Joel Fernandes
Quoted 12 lines · Paul E. McKenney
Quoted 8 lines · Joel Fernandes
Quoted 7 lines · Eric Dumazet

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

<snip>
tcp: use 2-arg optimal variant of kfree_rcu()
Date: Fri, 2 Dec 2022 05:28:47 +0000 [thread overview]
Message-ID: <[email protected]> (raw)

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would
get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

Fixes 459837b522f7 ("net/tcp: Disable TCP-MD5 static key on tcp_md5sig_info destruction")
Cc Dmitry Safonov <[email protected]>
Cc Paul E. McKenney <[email protected]>

<snip>

Could you please clarify a little bit about why/how have you came up with a patch that you posted with "Fixes" tag? I mean you run into:
- performance degrade;
- simple typo;
- etc.

Thank you.

--
Uladzislau Rezki

#10 lore raw
Quoted 38 lines · Uladzislau Rezki

Hello, Eric.

Quoted 18 lines

+rcu for archives

Quoted 15 lines · Joel Fernandes
Quoted 12 lines · Joel Fernandes
Quoted 12 lines · Paul E. McKenney
Quoted 8 lines · Joel Fernandes
Quoted 7 lines · Eric Dumazet

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

<snip>
tcp: use 2-arg optimal variant of kfree_rcu()
Date: Fri, 2 Dec 2022 05:28:47 +0000 [thread overview]
Message-ID: <[email protected]> (raw)

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would
get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

Fixes 459837b522f7 ("net/tcp: Disable TCP-MD5 static key on tcp_md5sig_info destruction")
Cc Dmitry Safonov <[email protected]>
Cc Paul E. McKenney <[email protected]>
Quoted 8 lines

<snip>

Could you please clarify a little bit about why/how have you came up with a patch that you posted with "Fixes" tag? I mean you run into:
- performance degrade;
- simple typo;
- etc.

Bug was added in the blamed commit, we use Fixes: tag to clearly identify bug origin.

tcp_md5_key_copy() is called from softirq context, there is no way it could sleep in synchronize_rcu()

maintainer
#11 lore raw
Quoted 48 lines · Uladzislau Rezki
Quoted 38 lines

Hello, Eric.

Quoted 18 lines

+rcu for archives

Quoted 15 lines · Joel Fernandes
Quoted 12 lines · Joel Fernandes
Quoted 12 lines · Paul E. McKenney
Quoted 8 lines · Joel Fernandes
Quoted 7 lines · Eric Dumazet

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

<snip>
tcp: use 2-arg optimal variant of kfree_rcu()
Date: Fri, 2 Dec 2022 05:28:47 +0000 [thread overview]
Message-ID: <[email protected]> (raw)

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would
get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

Fixes 459837b522f7 ("net/tcp: Disable TCP-MD5 static key on tcp_md5sig_info destruction")
Cc Dmitry Safonov <[email protected]>
Cc Paul E. McKenney <[email protected]>
Quoted 8 lines

<snip>

Could you please clarify a little bit about why/how have you came up with a patch that you posted with "Fixes" tag? I mean you run into:
- performance degrade;
- simple typo;
- etc.

Quoted 6 lines

Bug was added in the blamed commit, we use Fixes: tag to clearly identify bug origin.

tcp_md5_key_copy() is called from softirq context, there is no way it could sleep in synchronize_rcu()

So it was a typo then. How did you identify that BUG? Simple go through the code? Or some test coverage?

Thank you!

--
Uladzislau Rezki

#12 lore raw
Quoted 2 lines · Uladzislau Rezki

So it was a typo then. How did you identify that BUG? Simple go through the code? Or some test coverage?

Code review. I am the TCP maintainer, in case you do not know.

Quoted 5 lines

Thank you!

--
Uladzislau Rezki

maintainer
#13 lore raw
Quoted 7 lines · Uladzislau Rezki
Quoted 3 lines

So it was a typo then. How did you identify that BUG? Simple go through the code? Or some test coverage?

Code review. I am the TCP maintainer, in case you do not know.

OK, thank you.

--
Uladzislau Rezki

#14 lore raw
In reply to Eric Dumazet
This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Fri, 2 Dec 2022 05:28:47 +0000 you wrote:

kfree_rcu(1-arg) should be avoided as much as possible, since this is only possible from sleepable contexts, and incurr extra rcu barriers.

I wish the 1-arg variant of kfree_rcu() would get a distinct name, like kfree_rcu_slow()
to avoid it being abused.

[...]

Here is the summary with links:
- [net-next] tcp: use 2-arg optimal variant of kfree_rcu()

    https://git.kernel.org/netdev/net-next/c/55fb80d518c7

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html