* [PATCH net] tls: use non-blocking sends in tx_work_handler
@ 2026-11-27 10:08 Dickson Wu via B4 Relay
2026-11-28 11:53 ` netdev-bot+sashiko
0 siblings, 1 reply; 3+ messages in thread
From: Dickson Wu via B4 Relay @ 2026-11-27 10:08 UTC (permalink / raw)
To: netdev
Cc: John Fastabend, Jakub Kicinski, Sabrina Dubroca, David S. Miller,
Eric Dumazet, Paolo Abeni, Willem de Bruijn, Simon Horman,
Vakul Garg, Jiayuan Chen, syzbot+ca1345cca66556f3d79b, security
From: Dickson Wu <dickson.wu@certik.com>
tx_work_handler() calls tls_tx_records(sk, -1), so each record is
pushed with the flags of the sendmsg() call that created it. When a
blocking send returns early (signal or SO_SNDTIMEO) and leaves a
partially sent record, the work handler pushes the rest with blocking
flags. If the peer keeps its window closed, the worker sleeps in
sk_stream_wait_memory() with no timeout. tls_sk_proto_close() then
waits for it in disable_delayed_work_sync(), so the closing task sits
in D state and ignores SIGKILL.
Each such socket holds a system_wq worker. With the tls module loaded,
an unprivileged local user can occupy all events workers on a CPU this
way (1024 on a one-CPU test guest), which stalls
schedule_on_each_cpu() callers.
Pass MSG_DONTWAIT from the work handler. On -EAGAIN the record stays on
tx_list and tls_sw_write_space() schedules the work again when TCP has
room. The close path does not change: tls_sw_release_resources_tx()
still calls tls_tx_records(sk, -1) from process context, so data
queued before close() is still flushed with the socket's own blocking
semantics and the FIN follows it. The worker no longer applies the
saved per-record flags, such as MSG_MORE or MSG_NOSIGNAL, which belong
to a sendmsg() call that has already returned.
Jiayuan Chen proposed the same approach earlier with a new extra_flags
argument to tls_tx_records() (Link below). This version needs no new
argument, and the close flush that review of that patch focused on is
unchanged.
Reported-by: syzbot+ca1345cca66556f3d79b@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=ca1345cca66556f3d79b
Link: https://lore.kernel.org/netdev/20260227063231.168520-1-jiayuan.chen@linux.dev/
Fixes: a42055e8d2c3 ("net/tls: Add support for async encryption of records for performance")
Signed-off-by: Dickson Wu <dickson.wu@certik.com>
---
Tested on x86_64 UML with KASAN at v7.4-rc4 (9437ee8b6cf6), as uid 1000
with no capabilities or namespaces, over loopback:
- Interrupted 256 KiB send, peer reads 1 KiB and stops, then close():
stock and reverted, the closer stays in D state 33 s after SIGKILL
with the worker in sk_stream_wait_memory(). Patched, the closer is in
S state in the normal close flush and SIGKILL ends it within 3 s.
- 1900 kTLS pairs, each with a partial send after a 20 ms SO_SNDTIMEO:
stock and reverted, 1024 busy events workers and a
/proc/sys/vm/stat_refresh write over 10 s. Patched, 0 busy workers
and 11 ms.
- Patched controls: an immediate close after an interrupted send
delivered and decrypted all 32768 accepted bytes. With the sender
idle (no further send, no close) after the worker got -EAGAIN, the
next write_space rescheduled it and a slow reader still got all
32768 bytes. An uninterrupted send delivered all 262144 bytes, and
plain TCP is unchanged.
- No KASAN, BUG or WARNING in any run. The patch applies unchanged to
6.1.y, 6.6.y, 6.12.y and 6.18.y.
A reproducer is available if it helps.
This was found and the fix prepared with AI coding tools (OpenAI Codex
agents, with review by Claude). I went over the analysis, the earlier
thread and the patch before signing off.
net/tls/tls_sw.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 226e0f176..16dcd134e 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -2428,7 +2428,7 @@ static void tx_work_handler(struct work_struct *work)
if (mutex_trylock(&tls_ctx->tx_lock)) {
lock_sock(sk);
- tls_tx_records(sk, -1);
+ tls_tx_records(sk, MSG_DONTWAIT);
release_sock(sk);
mutex_unlock(&tls_ctx->tx_lock);
} else if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH net] tls: use non-blocking sends in tx_work_handler
2026-11-27 10:08 [PATCH net] tls: use non-blocking sends in tx_work_handler Dickson Wu via B4 Relay
@ 2026-11-28 11:53 ` netdev-bot+sashiko
2026-11-28 14:14 ` Dickson Wu
0 siblings, 1 reply; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28 11:53 UTC (permalink / raw)
To: dickson.wu
Cc: netdev, john.fastabend, kuba, sd, davem, edumazet, pabeni,
willemb, horms, vakul.garg, jiayuan.chen,
syzbot+ca1345cca66556f3d79b, security
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 · Low: 1
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] Incomplete fix: the TLS close-time flush can still sleep forever
in a system_wq kworker.
- [Medium] Lost reschedule (TX stall) after the worker gets -EAGAIN with
empty TCP queues.
- [Low] Commit message inaccuracy.
--
tls: use non-blocking sends in tx_work_handler
This patch makes tx_work_handler() push pending TLS records with
MSG_DONTWAIT instead of the per-record flags saved from sendmsg(). This
stops a system_wq worker from sleeping forever in sk_stream_wait_memory()
when the peer keeps its window closed. The close path flush in
tls_sw_release_resources_tx() is unchanged.
Link: https://lore.kernel.org/netdev/20260227063231.168520-1-jiayuan.chen@linux.dev/
> When a
> blocking send returns early (signal or SO_SNDTIMEO) and leaves a
> partially sent record, the work handler pushes the rest with blocking
> flags. If the peer keeps its window closed, the worker sleeps in
> sk_stream_wait_memory() with no timeout.
[Severity: Low]
Is this accurate for the SO_SNDTIMEO case?
Before this patch the worker pushed with rec->tx_flags, which do not
include MSG_DONTWAIT. tcp_sendmsg_locked() takes the wait time from the
socket:
net/ipv4/tcp.c:tcp_sendmsg_locked() {
...
timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
...
}
So the worker inherited sk->sk_sndtimeo. With SO_SNDTIMEO set,
sk_stream_wait_memory() returns -EAGAIN when the timeout expires, and
tls_tx_records() treats -EAGAIN as non-fatal.
The wait has no limit only when sk_sndtimeo is infinite at the time the
worker runs. That is the signal case, or SO_SNDTIMEO cleared after the
partial send. The 20 ms SO_SNDTIMEO test in the notes would only match
this description if the timeout was cleared afterwards.
Could the commit message say more precisely which configurations hit
this?
> diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
> index 226e0f176fdc8..16dcd134e572f 100644
> --- a/net/tls/tls_sw.c
> +++ b/net/tls/tls_sw.c
> @@ -2428,7 +2428,7 @@ static void tx_work_handler(struct work_struct *work)
>
> if (mutex_trylock(&tls_ctx->tx_lock)) {
> lock_sock(sk);
> - tls_tx_records(sk, -1);
> + tls_tx_records(sk, MSG_DONTWAIT);
[Severity: High]
The commit message says tls_sw_release_resources_tx() "still calls
tls_tx_records(sk, -1) from process context". Is that always true?
net/tls/tls_sw.c:tls_sw_release_resources_tx() {
...
tls_encrypt_async_wait(ctx);
tls_tx_records(sk, -1);
...
}
When a kernel thread drops the last reference to the kTLS socket file,
__fput_deferred() hands the file to the global delayed_fput_work:
fs/file_table.c:__fput_deferred() {
...
if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
...
}
if (llist_add(&file->f_llist, &delayed_fput_list))
schedule_delayed_work(&delayed_fput_work, 1);
}
An unprivileged user can set this up. They put the kTLS fd in an
AF_UNIX SCM_RIGHTS cycle and close every fd. unix_gc() runs from
unix_gc_work. When it purges the dead skbs, unix_destruct_scm() calls
__scm_destroy(), which calls fput() in kworker context.
The kworker then runs:
delayed_fput()
__fput()
tls_sk_proto_close()
tls_sk_proto_cleanup()
tls_sw_release_resources_tx()
tls_tx_records(sk, -1)
tls_push_sg()
tcp_sendmsg_locked()
sk_stream_wait_memory()
The saved rec->tx_flags do not include MSG_DONTWAIT, so timeo is
sk_sndtimeo. By default that is MAX_SCHEDULE_TIMEOUT. If the peer keeps
its window closed, the signal_pending() check never fires because
kworkers ignore signals.
delayed_fput() releases its list one file at a time, and the work item
never runs concurrently with itself. Would every later deferred fput in
the system then stall behind this socket? Those files, their dentries
and their mounts would stay pinned until the peer reads.
This is the same kind of system_wq worker hang that this patch fixes.
Should the close flush also be bounded when it runs in a kthread, for
example by forcing MSG_DONTWAIT or a bounded timeout there?
[Severity: Medium]
Every worker push is now non-blocking. Can tx_work miss its only
reschedule when the TCP rtx and write queues are empty?
tls_push_sg() sets ctx->splicing_pages = true before it calls
tcp_sendmsg_locked(). Suppose the queues are empty and
tcp_stream_alloc_skb() fails, or tcp_wmem_schedule() returns 0. Then
sk_stream_wait_memory() returns -EAGAIN at once because timeo is 0.
tcp_sendmsg_locked() then issues the only wakeup it gives for this case:
net/ipv4/tcp.c:tcp_sendmsg_locked() {
...
if (unlikely(tcp_rtx_and_write_queues_empty(sk) && err == -EAGAIN)) {
READ_ONCE(sk->sk_write_space)(sk);
...
}
That callback is tls_write_space(). It returns early while
splicing_pages is still set:
net/tls/tls_main.c:tls_write_space() {
...
if (ctx->splicing_pages) {
ctx->sk_write_space(sk);
return;
}
...
}
So tls_sw_write_space() is never called. Nothing is in flight, so no
ACK arrives to run tcp_check_space(), and tx_work is not queued again.
The record, possibly partially sent, then stays on tx_list until the
application calls sendmsg(), splice_eof or close(), or until the peer
sends something. Before this patch, records from blocking sends were
pushed with a non-zero timeo. sk_stream_wait_memory() then slept for a
short random vm_wait and retried by itself.
Records from MSG_DONTWAIT sends already had this gap. This change
extends it to records left by blocking sends that were interrupted by a
signal or a timeout, and to records from blocking sends with async
crypto. Is a fallback reschedule needed for this case?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127-ktls-close-c-v1-1-1470bb718a8d%40certik.com
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH net] tls: use non-blocking sends in tx_work_handler
2026-11-28 11:53 ` netdev-bot+sashiko
@ 2026-11-28 14:14 ` Dickson Wu
0 siblings, 0 replies; 3+ messages in thread
From: Dickson Wu @ 2026-11-28 14:14 UTC (permalink / raw)
To: netdev-bot+sashiko
Cc: netdev, john.fastabend, kuba, sd, davem, edumazet, pabeni,
willemb, horms, vakul.garg, jiayuan.chen,
syzbot+ca1345cca66556f3d79b, security
On Sat, 28 Nov 2026 11:53:30 +0000, Sashiko wrote:
> - [High] Incomplete fix: the TLS close-time flush can still sleep forever
> in a system_wq kworker.
Agreed, and it reproduces. I put the kTLS fd in an SCM_RIGHTS cycle
after an interrupted send and let unix_gc free it. On stock and on v1
the delayed_fput kworker sits in sk_stream_wait_memory(), and a later
deferred fput waits behind it until the peer reads. v2 uses
MSG_DONTWAIT for the close flush when it runs in a kthread. Records it
can't push are freed with tx_list, same as when a signal interrupts
the flush in a user task, and close() from a user task still blocks
as before.
> - [Medium] Lost reschedule (TX stall) after the worker gets -EAGAIN with
> empty TCP queues.
Agreed from the code. I didn't hit it at runtime since this UML setup
only has sync crypto. v2 checks tcp_rtx_and_write_queues_empty() after
-EAGAIN, still under the socket lock, and requeues the work one tick
later. The cost is a retry every tick while TCP memory stays exhausted
with both queues empty.
> - [Low] Commit message inaccuracy.
Fair. The wait is only unbounded with an infinite sk_sndtimeo. The
saturation test reset SO_SNDTIMEO to infinite after the partial send,
and v2 says that.
v2: https://lore.kernel.org/r/20261128-ktls-close-v2a-v2-1-68a955cbbbcb@certik.com
Thanks,
Dickson
pw-bot: cr
On Sat, 28 Nov 2026 11:53:30 +0000, netdev-bot+sashiko@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 3 potential
> issue(s) to consider.
>
> Critical: 0 · High: 1 · Medium: 1 · Low: 1
>
> 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] Incomplete fix: the TLS close-time flush can still sleep forever
> in a system_wq kworker.
> - [Medium] Lost reschedule (TX stall) after the worker gets -EAGAIN with
> empty TCP queues.
> - [Low] Commit message inaccuracy.
>
> --
>
> tls: use non-blocking sends in tx_work_handler
>
> This patch makes tx_work_handler() push pending TLS records with
> MSG_DONTWAIT instead of the per-record flags saved from sendmsg(). This
> stops a system_wq worker from sleeping forever in sk_stream_wait_memory()
> when the peer keeps its window closed. The close path flush in
> tls_sw_release_resources_tx() is unchanged.
>
> Link: https://lore.kernel.org/netdev/20260227063231.168520-1-jiayuan.chen@linux.dev/
>
> > When a
> > blocking send returns early (signal or SO_SNDTIMEO) and leaves a
> > partially sent record, the work handler pushes the rest with blocking
> > flags. If the peer keeps its window closed, the worker sleeps in
> > sk_stream_wait_memory() with no timeout.
>
> [Severity: Low]
> Is this accurate for the SO_SNDTIMEO case?
>
> Before this patch the worker pushed with rec->tx_flags, which do not
> include MSG_DONTWAIT. tcp_sendmsg_locked() takes the wait time from the
> socket:
>
> net/ipv4/tcp.c:tcp_sendmsg_locked() {
> ...
> timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
> ...
> }
>
> So the worker inherited sk->sk_sndtimeo. With SO_SNDTIMEO set,
> sk_stream_wait_memory() returns -EAGAIN when the timeout expires, and
> tls_tx_records() treats -EAGAIN as non-fatal.
>
> The wait has no limit only when sk_sndtimeo is infinite at the time the
> worker runs. That is the signal case, or SO_SNDTIMEO cleared after the
> partial send. The 20 ms SO_SNDTIMEO test in the notes would only match
> this description if the timeout was cleared afterwards.
>
> Could the commit message say more precisely which configurations hit
> this?
>
> > diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
> > index 226e0f176fdc8..16dcd134e572f 100644
> > --- a/net/tls/tls_sw.c
> > +++ b/net/tls/tls_sw.c
> > @@ -2428,7 +2428,7 @@ static void tx_work_handler(struct work_struct *work)
> >
> > if (mutex_trylock(&tls_ctx->tx_lock)) {
> > lock_sock(sk);
> > - tls_tx_records(sk, -1);
> > + tls_tx_records(sk, MSG_DONTWAIT);
>
> [Severity: High]
> The commit message says tls_sw_release_resources_tx() "still calls
> tls_tx_records(sk, -1) from process context". Is that always true?
>
> net/tls/tls_sw.c:tls_sw_release_resources_tx() {
> ...
> tls_encrypt_async_wait(ctx);
>
> tls_tx_records(sk, -1);
> ...
> }
>
> When a kernel thread drops the last reference to the kTLS socket file,
> __fput_deferred() hands the file to the global delayed_fput_work:
>
> fs/file_table.c:__fput_deferred() {
> ...
> if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
> ...
> }
>
> if (llist_add(&file->f_llist, &delayed_fput_list))
> schedule_delayed_work(&delayed_fput_work, 1);
> }
>
> An unprivileged user can set this up. They put the kTLS fd in an
> AF_UNIX SCM_RIGHTS cycle and close every fd. unix_gc() runs from
> unix_gc_work. When it purges the dead skbs, unix_destruct_scm() calls
> __scm_destroy(), which calls fput() in kworker context.
>
> The kworker then runs:
>
> delayed_fput()
> __fput()
> tls_sk_proto_close()
> tls_sk_proto_cleanup()
> tls_sw_release_resources_tx()
> tls_tx_records(sk, -1)
> tls_push_sg()
> tcp_sendmsg_locked()
> sk_stream_wait_memory()
>
> The saved rec->tx_flags do not include MSG_DONTWAIT, so timeo is
> sk_sndtimeo. By default that is MAX_SCHEDULE_TIMEOUT. If the peer keeps
> its window closed, the signal_pending() check never fires because
> kworkers ignore signals.
>
> delayed_fput() releases its list one file at a time, and the work item
> never runs concurrently with itself. Would every later deferred fput in
> the system then stall behind this socket? Those files, their dentries
> and their mounts would stay pinned until the peer reads.
>
> This is the same kind of system_wq worker hang that this patch fixes.
> Should the close flush also be bounded when it runs in a kthread, for
> example by forcing MSG_DONTWAIT or a bounded timeout there?
>
> [Severity: Medium]
> Every worker push is now non-blocking. Can tx_work miss its only
> reschedule when the TCP rtx and write queues are empty?
>
> tls_push_sg() sets ctx->splicing_pages = true before it calls
> tcp_sendmsg_locked(). Suppose the queues are empty and
> tcp_stream_alloc_skb() fails, or tcp_wmem_schedule() returns 0. Then
> sk_stream_wait_memory() returns -EAGAIN at once because timeo is 0.
> tcp_sendmsg_locked() then issues the only wakeup it gives for this case:
>
> net/ipv4/tcp.c:tcp_sendmsg_locked() {
> ...
> if (unlikely(tcp_rtx_and_write_queues_empty(sk) && err == -EAGAIN)) {
> READ_ONCE(sk->sk_write_space)(sk);
> ...
> }
>
> That callback is tls_write_space(). It returns early while
> splicing_pages is still set:
>
> net/tls/tls_main.c:tls_write_space() {
> ...
> if (ctx->splicing_pages) {
> ctx->sk_write_space(sk);
> return;
> }
> ...
> }
>
> So tls_sw_write_space() is never called. Nothing is in flight, so no
> ACK arrives to run tcp_check_space(), and tx_work is not queued again.
>
> The record, possibly partially sent, then stays on tx_list until the
> application calls sendmsg(), splice_eof or close(), or until the peer
> sends something. Before this patch, records from blocking sends were
> pushed with a non-zero timeo. sk_stream_wait_memory() then slept for a
> short random vm_wait and retried by itself.
>
> Records from MSG_DONTWAIT sends already had this gap. This change
> extends it to records left by blocking sends that were interrupted by a
> signal or a timeout, and to records from blocking sends with async
> crypto. Is a fallback reschedule needed for this case?
>
> --
> Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127-ktls-close-c-v1-1-1470bb718a8d%40certik.com
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-28 14:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 10:08 [PATCH net] tls: use non-blocking sends in tx_work_handler Dickson Wu via B4 Relay
2026-11-28 11:53 ` netdev-bot+sashiko
2026-11-28 14:14 ` Dickson Wu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox