net: tcp: un-pin tw timer
Subsystems (3)
v8 cover net: tcp: un-pin tw timer
Changes since previous iteration:
- Patch 1: update a comment, I copied Erics v7 RvB tag.
- Patch 2: move bh off/on into hashdance_schedule and get rid of comment mentioning pinned tw timer.
I did not copy Erics RvB tag over from v7 because of the change.
- Patch 3 is unchanged, so I kept Erics RvB tag.
This is v8 of the series where the tw_timer is un-pinned to get rid of interferences in isolated CPUs setups.
First patch makes necessary preparations, existing code relies on TIMER_PINNED to avoid races.
Second patch un-pins the TW timer. Could be folded into the first one, but it might help wrt. bisection.
Third patch is a minor cleanup to move a helper from .h to the only remaining compilation unit.
Tested with iperf3 and stress-ng socket mode.
Florian Westphal (2):
net: tcp: un-pin the tw_timer
tcp: move inet_twsk_schedule helper out of header
Valentin Schneider (1):
net: tcp/dccp: prepare for tw_timer un-pinning
include/net/inet_timewait_sock.h | 11 ++---- net/dccp/minisocks.c | 9 +---- net/ipv4/inet_timewait_sock.c | 63 +++++++++++++++++++++++++++----- net/ipv4/tcp_ipv4.c | 2 +- net/ipv4/tcp_minisocks.c | 9 +---- 5 files changed, 61 insertions(+), 33 deletions(-)
Signature · 1 line
2.44.2
PATCH v8 1/3 net: tcp/dccp: prepare for tw_timer un-pinning
From: Valentin Schneider <[email protected]>
The TCP timewait timer is proving to be problematic for setups where scheduler CPU isolation is achieved at runtime via cpusets (as opposed to statically via isolcpus=domains).
What happens there is a CPU goes through tcp_time_wait(), arming the time_wait timer, then gets isolated. TCP_TIMEWAIT_LEN later, the timer fires, causing interference for the now-isolated CPU. This is conceptually similar to the issue described in commit e02b93124855 ("workqueue: Unbind kworkers before sending them to exit()")
Move inet_twsk_schedule() to within inet_twsk_hashdance(), with the ehash lock held. Expand the lock's critical section from inet_twsk_kill() to inet_twsk_deschedule_put(), serializing the scheduling vs descheduling of the timer. IOW, this prevents the following race:
tcp_time_wait() inet_twsk_hashdance()
inet_twsk_deschedule_put()
del_timer_sync()
inet_twsk_schedule()
Thanks to Paolo Abeni for suggesting to leverage the ehash lock.
This also restores a comment from commit ec94c2696f0b ("tcp/dccp: avoid one atomic operation for timewait hashdance") as inet_twsk_hashdance() had a "Step 1" and "Step 3" comment, but the "Step 2" had gone missing.
inet_twsk_deschedule_put() now acquires the ehash spinlock to synchronize with inet_twsk_hashdance_schedule().
To ease possible regression search, actual un-pin is done in next patch.
---
Change since last version:
- adjust a comment in tcp_twsk_unique().
include/net/inet_timewait_sock.h | 6 ++-- net/dccp/minisocks.c | 3 +- net/ipv4/inet_timewait_sock.c | 52 +++++++++++++++++++++++++++----- net/ipv4/tcp_ipv4.c | 2 +- net/ipv4/tcp_minisocks.c | 3 +- 5 files changed, 52 insertions(+), 14 deletions(-)
include/net/inet_timewait_sock.h +4 -2
@@ -93,8 +93,10 @@ struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk,struct inet_timewait_death_row *dr, const int state); -void inet_twsk_hashdance (struct inet_timewait_sock *tw,struct sock *sk, -struct inet_hashinfo *hashinfo); +void inet_twsk_hashdance_schedule (struct inet_timewait_sock *tw, +struct sock *sk, +struct inet_hashinfo *hashinfo, +int timeo);void __inet_twsk_schedule (struct inet_timewait_sock *tw,int timeo,bool rearm);
net/dccp/minisocks.c +1 -2
@@ -59,11 +59,10 @@ void dccp_time_wait(struct sock *sk, int state, int timeo)* we complete the initialization. */ local_bh_disable (); -inet_twsk_schedule (tw, timeo);/* Linkage updates. * Note that access to tw after this point is illegal. */ -inet_twsk_hashdance (tw, sk, &dccp_hashinfo); +inet_twsk_hashdance_schedule (tw, sk, &dccp_hashinfo, timeo);local_bh_enable (); }else {/* Sorry, if we're out of memory, just CLOSE this
net/ipv4/inet_timewait_sock.c +45 -7
87 lines not on this page · raw message
net/ipv4/tcp_ipv4.c +1 -1
@@ -157,7 +157,7 @@ int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)if (ts_recent_stamp && (!twp || (reuse &&time_after32 (ktime_get_seconds (), ts_recent_stamp)))) { -/* inet_twsk_hashdance() sets sk_refcnt after putting twsk +/* inet_twsk_hashdance_schedule() sets sk_refcnt after putting twsk * and releasing the bucket lock. */ if (unlikely (!refcount_inc_not_zero (&sktw->sk_refcnt)))
net/ipv4/tcp_minisocks.c +1 -2
@@ -350,11 +350,10 @@ void tcp_time_wait(struct sock *sk, int state, int timeo)* we complete the initialization. */ local_bh_disable (); -inet_twsk_schedule (tw, timeo);/* Linkage updates. * Note that access to tw after this point is illegal. */ -inet_twsk_hashdance (tw, sk, net->ipv4.tcp_death_row.hashinfo); +inet_twsk_hashdance_schedule (tw, sk, net->ipv4.tcp_death_row.hashinfo, timeo);local_bh_enable (); }else {/* Sorry, if we're out of memory, just CLOSE this
Signature · 1 line
2.44.2
PATCH v8 2/3 net: tcp: un-pin the tw_timer
In reply to Florian WestphalAfter previous patch, even if timer fires immediately on another CPU, context that schedules the timer now holds the ehash spinlock, so timer cannot reap tw socket until ehash lock is released.
BH disable is moved into hashdance_schedule.
---
Changes since last version:
Move local_bh_enable/disable into inet_twsk_hashdance_schedule and remove the comments mentioning timer_pinned in the two callers.
net/dccp/minisocks.c | 6 ------ net/ipv4/inet_timewait_sock.c | 6 ++++-- net/ipv4/tcp_minisocks.c | 6 ------ 3 files changed, 4 insertions(+), 14 deletions(-)
net/dccp/minisocks.c +0 -6
@@ -54,16 +54,10 @@ void dccp_time_wait(struct sock *sk, int state, int timeo)if (state ==DCCP_TIME_WAIT ) timeo =DCCP_TIMEWAIT_LEN ; -/* tw_timer is pinned, so we need to make sure BH are disabled -* in following section, otherwise timer handler could run before -* we complete the initialization. -*/ -local_bh_disable ();/* Linkage updates. * Note that access to tw after this point is illegal. */ inet_twsk_hashdance_schedule (tw, sk, &dccp_hashinfo, timeo); -local_bh_enable (); }else {/* Sorry, if we're out of memory, just CLOSE this * socket up. We've got bigger problems than
net/ipv4/inet_timewait_sock.c +4 -2
@@ -93,7 +93,7 @@ static void inet_twsk_add_node_rcu(struct inet_timewait_sock *tw, }/* -* Enter the time wait state. This is called with locally disabled BH. +* Enter the time wait state. * Essentially we whip up a timewait bucket, copy the relevant info into it * from the SK, and mess with hash chains and list linkage. *
@@ -118,6 +118,7 @@ void inet_twsk_hashdance_schedule(struct inet_timewait_sock *tw, hashinfo->bhash_size)]; bhead2 =inet_bhashfn_portaddr (hashinfo, sk,twsk_net (tw), inet->inet_num); +local_bh_disable ();spin_lock (&bhead->lock);spin_lock (&bhead2->lock);
@@ -158,6 +159,7 @@ void inet_twsk_hashdance_schedule(struct inet_timewait_sock *tw,inet_twsk_schedule (tw, timeo);spin_unlock (lock); +local_bh_enable (); }EXPORT_SYMBOL_GPL (inet_twsk_hashdance_schedule);
@@ -203,7 +205,7 @@ struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, tw->tw_prot = sk->sk_prot_creator;atomic64_set (&tw->tw_cookie,atomic64_read (&sk->sk_cookie));twsk_net_set (tw,sock_net (sk)); -timer_setup (&tw->tw_timer, tw_timer_handler,TIMER_PINNED ); +timer_setup (&tw->tw_timer, tw_timer_handler,0 );/* * Because we use RCU lookups, we should not set tw_refcnt * to a non null value before everything is setup for this
net/ipv4/tcp_minisocks.c +0 -6
@@ -345,16 +345,10 @@ void tcp_time_wait(struct sock *sk, int state, int timeo)if (state ==TCP_TIME_WAIT ) timeo =TCP_TIMEWAIT_LEN ; -/* tw_timer is pinned, so we need to make sure BH are disabled -* in following section, otherwise timer handler could run before -* we complete the initialization. -*/ -local_bh_disable ();/* Linkage updates. * Note that access to tw after this point is illegal. */ inet_twsk_hashdance_schedule (tw, sk, net->ipv4.tcp_death_row.hashinfo, timeo); -local_bh_enable (); }else {/* Sorry, if we're out of memory, just CLOSE this * socket up. We've got bigger problems than
Signature · 1 line
2.44.2
Re: [PATCH net-next v8 2/3] net: tcp: un-pin the tw_timer
Quoted 10 lines · Florian Westphal
After previous patch, even if timer fires immediately on another CPU, context that schedules the timer now holds the ehash spinlock, so timer cannot reap tw socket until ehash lock is released.
BH disable is moved into
hashdance_schedule.Signed-off-by Florian Westphal <[email protected]>---
PATCH v8 3/3 tcp: move inet_twsk_schedule helper out of header
In reply to Florian WestphalIts no longer used outside inet_timewait_sock.c, so move it there.
---
No changes since last version.
include/net/inet_timewait_sock.h | 5 ----- net/ipv4/inet_timewait_sock.c | 5 +++++ 2 files changed, 5 insertions(+), 5 deletions(-)
include/net/inet_timewait_sock.h +0 -5
@@ -101,11 +101,6 @@ void inet_twsk_hashdance_schedule(struct inet_timewait_sock *tw,void __inet_twsk_schedule (struct inet_timewait_sock *tw,int timeo,bool rearm); -static inline void inet_twsk_schedule (struct inet_timewait_sock *tw,int timeo) -{ -__inet_twsk_schedule (tw, timeo, false); -} -static inline void inet_twsk_reschedule (struct inet_timewait_sock *tw,int timeo) {__inet_twsk_schedule (tw, timeo, true);
net/ipv4/inet_timewait_sock.c +5 -0
@@ -92,6 +92,11 @@ static void inet_twsk_add_node_rcu(struct inet_timewait_sock *tw,hlist_nulls_add_head_rcu (&tw->tw_node, list); } +static void inet_twsk_schedule (struct inet_timewait_sock *tw,int timeo) +{ +__inet_twsk_schedule (tw, timeo, false); +} +/* * Enter the time wait state. * Essentially we whip up a timewait bucket, copy the relevant info into it
Signature · 1 line
2.44.2
Quoted 1 line · Florian Westphal
Changes since previous iteration:
Sebastian
This series was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:
Quoted 8 lines · On Thu, 6 Jun 2024 17:11:36 +0200 you wrote:
Changes since previous iteration:
- Patch 1: update a comment, I copied Erics v7 RvB tag.
- Patch 2: move bh off/on intohashdance_scheduleand get rid of comment mentioning pinned tw timer.
I did not copy Erics RvB tag over from v7 because of the change.
- Patch 3 is unchanged, so I kept Erics RvB tag.[...]
Here is the summary with links:
- [net-next,v8,1/3] net: tcp/dccp: prepare for tw_timer un-pinning
https://git.kernel.org/netdev/net-next/c/b334b924c9b7 - [net-next,v8,2/3] net: tcp: un-pin the tw_timer https://git.kernel.org/netdev/net-next/c/c75ad7c759cc - [net-next,v8,3/3] tcp: move inet_twsk_schedule helper out of header https://git.kernel.org/netdev/net-next/c/f81d0dd2fde3
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html