* [PATCH net] tcp: fix scheduler starvation in tcp_sendmsg_locked()
@ 2026-12-05 1:21 Tahsin Erdogan
2026-12-06 2:13 ` netdev-bot+sashiko
0 siblings, 1 reply; 2+ messages in thread
From: Tahsin Erdogan @ 2026-12-05 1:21 UTC (permalink / raw)
To: netdev
Cc: Eric Dumazet, Neal Cardwell, Kuniyuki Iwashima, David S. Miller,
Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman,
Ray Zhang, Joe Damato, stable
On a non-preemptible kernel, a large TCP send can keep a CPU executing
within tcp_sendmsg_locked() long enough to trigger the soft-lockup
watchdog:
watchdog: BUG: soft lockup - CPU#0 stuck for 22s! [repro:139]
Call trace:
skb_free_head
...
virtnet_poll_tx
__napi_poll
net_rx_action
handle_softirqs
...
__local_bh_enable_ip
__release_sock
__sk_flush_backlog
tcp_sendmsg_locked
tcp_sendmsg
...
__sys_sendto
Every 16 newly allocated SKBs, tcp_sendmsg_locked() calls
sk_flush_backlog(). Incoming ACKs processed there release enough send
memory for the send loop to continue, but flushing the backlog does not
guarantee a scheduling point.
Commit 16c610162d1f ("net: call cond_resched() less often in
__release_sock()") reduced the cond_resched() checks from every backlog
SKB to once every 16 SKBs, but left two gaps. First, the final SKB of
each backlog pass was not counted. A pass containing a single SKB
therefore did not advance the counter. Second, the counter was local to
one __release_sock() invocation, so separate invocations that each
processed fewer than 16 counted SKBs could not call cond_resched().
Commit c4e4056b2135 ("net: count last skb of each sk_backlog pass in
__release_sock()") fixed the first gap by counting every SKB across all
passes within one invocation. However, the counter still resets between
separate sk_flush_backlog() calls. If each call processes fewer than 16
SKBs, none reaches the 16-SKB count needed to call cond_resched().
Repeated calls that each consume a short backlog can avoid cond_resched()
indefinitely. Fix this by calling cond_resched() at the existing 16-SKB
checkpoint, after flushing the pending backlog.
We have observed this soft lockup repeatedly in production and reproduced
it on demand on arm64 hardware. It is also reproducible on v6.18.66 with
CONFIG_PREEMPT_NONE in an arm64 QEMU guest using two TCP connections over
separate virtio NICs. A single 1.5 GiB send runs on CPU 0 while the second
connection continuously sends from CPU 1. RX interrupts for both NICs are
delivered to CPU 0, and the QEMU peers ACK every received segment. The
watchdog reports CPU 0 stuck for 22 seconds, with 84-85% of its time spent
in softirq context. Since v6.18.66 includes the backlog-pass fix, this
confirms that fix alone does not prevent the lockup. With this change, the
1.5 GiB send completes without triggering the watchdog.
PREEMPT_NONE is unavailable on arm64 starting with v7.0, so this arm64
reproducer does not apply to current upstream. The fix remains relevant to
affected stable branches.
Fixes: 16c610162d1f ("net: call cond_resched() less often in __release_sock()")
Cc: stable@vger.kernel.org
Signed-off-by: Tahsin Erdogan <tahsin@meta.com>
---
net/ipv4/tcp.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 80de376a9d2e..5b9e07db6ee2 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -247,6 +247,7 @@
#include <crypto/utils.h>
#include <linux/kernel.h>
#include <linux/module.h>
+#include <linux/sched.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/poll.h>
@@ -1262,8 +1263,13 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
goto wait_for_space;
if (unlikely(process_backlog >= 16)) {
+ bool restart_needed;
+
process_backlog = 0;
- if (sk_flush_backlog(sk))
+ restart_needed = sk_flush_backlog(sk);
+ /* Check for rescheduling every 16 newly allocated skbs. */
+ cond_resched();
+ if (restart_needed)
goto restart;
}
first_skb = tcp_rtx_and_write_queues_empty(sk);
base-commit: e9d78fc6741ac06d438cf64e314c28e57014432a
--
2.56.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH net] tcp: fix scheduler starvation in tcp_sendmsg_locked()
2026-12-05 1:21 [PATCH net] tcp: fix scheduler starvation in tcp_sendmsg_locked() Tahsin Erdogan
@ 2026-12-06 2:13 ` netdev-bot+sashiko
0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-12-06 2:13 UTC (permalink / raw)
To: tahsin
Cc: netdev, edumazet, ncardwell, kuniyu, davem, kuba, pabeni, willemb,
horms, sgzhang, joe, stable
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The patch adds an unconditional cond_resched() in
tcp_sendmsg_locked() (net/ipv4/tcp.c:1271).
- [Medium] The commit message puts the root cause in __release_sock():
its 16-skb cond_resched() counter is the local `int nb = 0`…
--
tcp: fix scheduler starvation in tcp_sendmsg_locked()
This patch adds a cond_resched() call at the existing 16-skb checkpoint in
tcp_sendmsg_locked(), right after sk_flush_backlog(). Without it, repeated
short backlog flushes on PREEMPT_NONE kernels never reach the cond_resched()
inside __release_sock(), and the CPU hits a soft lockup.
> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
> index 80de376a9d2ef..5b9e07db6ee26 100644
> --- a/net/ipv4/tcp.c
> +++ b/net/ipv4/tcp.c
[ ... ]
> @@ -1262,8 +1263,13 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
> goto wait_for_space;
>
> if (unlikely(process_backlog >= 16)) {
> + bool restart_needed;
> +
> process_backlog = 0;
> - if (sk_flush_backlog(sk))
> + restart_needed = sk_flush_backlog(sk);
> + /* Check for rescheduling every 16 newly allocated skbs. */
> + cond_resched();
[Severity: High]
Is tcp_sendmsg_locked() always called from a context that can sleep? This
cond_resched() now runs every time process_backlog reaches 16, even when
sk_flush_backlog() found no backlog.
Some in-tree callers seem to reach tcp_sendmsg_locked() with BH disabled.
One is kTLS device offload from the RX softirq:
tcp_rcv_established()
tcp_check_space()
tcp_new_space()
sk->sk_write_space() == tls_write_space()
tls_device_write_space()
tls_push_partial_record()
tls_push_sg()
tcp_sendmsg_locked()
tls_device_write_space() switches to GFP_ATOMIC because it runs in that
context:
net/tls/tls_device.c:tls_device_write_space() {
...
sk->sk_allocation = GFP_ATOMIC;
tls_push_partial_record(sk, ctx,
MSG_DONTWAIT | MSG_NOSIGNAL |
MSG_SENDPAGE_DECRYPTED);
...
}
Another is ESP-in-TCP, reached from xfrm_trans_reinject() under
local_bh_disable() and rcu_read_lock():
xfrm_trans_reinject()
esp_output_tcp_finish() /* rcu_read_lock() + bh_lock_sock() */
espintcp_push_skb()
espintcp_sendskb_locked()
skb_send_sock_locked()
__skb_send_sock()
tcp_sendmsg_locked()
net/ipv6/esp6.c has the same code. espintcp_release() is also registered as
sk_prot->release_cb, and release_sock() calls it with sk_lock.slock held
through spin_lock_bh(). It pushes queued ESP skbs into tcp_sendmsg_locked()
as well.
Before this patch, the only scheduling point on these paths was inside
__release_sock(), which is reached through sk_flush_backlog(). The socket is
not user-owned in these contexts, so the backlog is empty and
sk_flush_backlog() returns false.
Now any single call that allocates 17 or more skbs reaches cond_resched().
A peer can cause that by advertising a small window, because
tcp_bound_to_half_wnd() limits size_goal to max_window/2. MSG_DONTWAIT
avoids the wait_for_space sleep but does not avoid this checkpoint.
With CONFIG_DEBUG_ATOMIC_SLEEP, will __might_resched() print "BUG: sleeping
function called from invalid context" here?
On !CONFIG_PREEMPTION kernels, should_resched(0) is false in softirq
context, so __cond_resched() falls through to rcu_all_qs():
kernel/sched/core.c:__cond_resched() {
if (should_resched(0) && !irqs_disabled()) {
preempt_schedule_common();
return 1;
}
...
#ifndef CONFIG_PREEMPT_RCU
rcu_all_qs();
#endif
return 0;
}
Could this report a quiescent state while still inside the rcu_read_lock()
sections held by xfrm_trans_reinject() and esp_output_tcp_finish(), or the
NET_RX softirq? If so, a grace period could end while objects such as the
RX skb's noref dst are still in use.
Would it be safer to call cond_resched() only when sk_flush_backlog()
returned true, or to move it into __sk_flush_backlog() after the unlock? A
non-empty backlog implies the socket is user-owned in process context.
> + if (restart_needed)
> goto restart;
> }
> first_skb = tcp_rtx_and_write_queues_empty(sk);
[Severity: Medium]
The commit message traces the root cause to __release_sock(), whose 16-skb
counter is local to each invocation:
net/core/sock.c:__release_sock() {
...
int nb = 0;
...
if (!(++nb & 15))
cond_resched();
...
}
The patch only changes one caller, tcp_sendmsg_locked(). Do other loops
that flush the backlog repeatedly still have the same gap?
tcp_recvmsg_locked() keeps looping while the backlog is non-empty, and it
calls __sk_flush_backlog() directly:
net/ipv4/tcp.c:tcp_recvmsg_locked() {
...
if (copied >= target && !READ_ONCE(sk->sk_backlog.tail))
break;
...
if (copied >= target) {
/* Do not sleep, just process backlog. */
__sk_flush_backlog(sk);
} else {
tcp_cleanup_rbuf(sk, copied);
err = sk_wait_data(sk, &timeo, last);
...
}
When copied < target, sk_wait_data() goes through sk_wait_event() and
release_sock()->__release_sock(), which resets nb again. If data has
already arrived, wait_woken() is skipped and only lock_sock() runs.
tls_rx_rec_wait() and tls_read_flush_backlog() in net/tls/tls_sw.c also
call sk_flush_backlog() inside receive loops.
None of these loops has its own cond_resched(). With a large len, could the
pattern the commit message describes ("Repeated calls that each consume a
short backlog can avoid cond_resched() indefinitely") also starve the
scheduler on the receive side?
Putting the cond_resched() in __sk_flush_backlog() after spin_unlock_bh()
would cover all of these callers. Could the commit message explain why a
fix in only this caller was chosen?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261205012134.1218271-1-tahsin%40meta.com
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-06 2:13 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 1:21 [PATCH net] tcp: fix scheduler starvation in tcp_sendmsg_locked() Tahsin Erdogan
2026-12-06 2:13 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox