Article View: linux.kernel.netdev
Article #23891[PATCH net v2] tls: avoid blocking sends from workqueue context
From: Dickson Wu via B4 Relay
Date: Sat, 28 Nov 2026 13:20
Date: Sat, 28 Nov 2026 13:20
127 lines
17009 bytes
17009 bytes
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 and leaves a partially sent record, the
work handler pushes the rest with blocking flags. If sk_sndtimeo is
infinite (the default, or an SO_SNDTIMEO cleared after the partial
send) and the peer keeps its window closed, the worker sleeps in
sk_stream_wait_memory() with no bound. 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. One case has no such callback: when the TCP write and retransmit
queues are empty, tcp_sendmsg_locked() calls sk_write_space before it
returns, while splicing_pages is still set, so tls_write_space() skips
tls_sw_write_space(). In that case the handler requeues itself one
tick later.
The close flush can block the same way. tls_sw_release_resources_tx()
usually runs in the task calling close(), where a signal ends the wait.
The last fput can also run from delayed_fput, for example when AF_UNIX
garbage collection frees an SCM_RIGHTS cycle, and then a kworker blocks
in sk_stream_wait_memory() and holds up every later delayed fput. Use
MSG_DONTWAIT for the close flush when it runs in a kthread. Records it
cannot push are freed with the rest of tx_list, as they are when a
signal interrupts the flush in a user task. A close() from a user task
keeps the blocking flush. In-kernel TLS users (sunrpc, nvme-tcp) call
kernel_sock_shutdown() before the release on their normal teardown
paths, so the flush has nothing it can send there either way.
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 MSG_DONTWAIT for the worker earlier with a new
extra_flags argument to tls_tx_records() (Link below). This version
needs no new argument.
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>
---
Changes in v2:
- Also use MSG_DONTWAIT for the close flush when it runs in a kthread.
v1 left it blocking, and a delayed_fput kworker could still hang
there (Sashiko review).
- Requeue the work one tick later after -EAGAIN with empty TCP queues,
where tls_write_space() skips the TLS callback (Sashiko review).
- Say that the worker wait is unbounded only with an infinite
sk_sndtimeo.
- v1: https://lore.kernel.org/r/20261127-ktls-close-c-v1-1-1470bb718a8d@certik.com
Tested on x86_64 UML with KASAN at mainline 7cd8495c0f16 (net/main
28a371228d4e has no change in the relevant TLS, TCP or AF_UNIX code),
as uid 1000 with no capabilities or namespaces, over loopback:
- Deferred fput: interrupt a 256 KiB send, then drop the socket
through an AF_UNIX SCM_RIGHTS cycle that GC frees. Stock and v1
leave a kworker in sk_stream_wait_memory() under delayed_fput, and a
later deferred fput waits behind it until the peer reads. v2: no
blocked worker, and the later fput completes at once.
- The v1 close-hang test: the closer stays in S state in the normal
close flush, no worker sits in tx_work_handler, and SIGKILL ends it.
- 1900 kTLS pairs, each with a partial send under a 20 ms SO_SNDTIMEO
that is then reset to infinite, 30 s hold: 0 busy events workers
(stock 1024), /proc/sys/vm/stat_refresh writes 11-12 ms throughout.
- Controls: an ordinary close delivered all 32768 accepted bytes; with
the sender idle after the worker got -EAGAIN, a slow reader still got
all 32768 bytes; an uninterrupted send delivered all 262144 bytes.
- No KASAN, BUG or WARNING in any run.
Not covered: I found the empty-queue requeue gap by reading the code
and did not hit it at runtime, since UML here has only synchronous
crypto. If TCP memory stays exhausted while both queues are empty, the
worker retries once per tick until memory frees up. Async crypto and
device offload were not exercised.
This was found and the fix prepared with AI coding tools (OpenAI Codex
agents, with review by Claude). I went over the analysis, the review
and the patch before signing off.
net/tls/tls_sw.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 226e0f176f..50dc45ad0d 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -2327,7 +2327,7 @@ void tls_sw_release_resources_tx(struct sock *sk)
/* Wait for any pending async encryptions to complete */
tls_encrypt_async_wait(ctx);
- tls_tx_records(sk, -1);
+ tls_tx_records(sk, current->flags & PF_KTHREAD ? MSG_DONTWAIT : -1);
/* Free up un-sent records in tx_list. First, free
* the partially sent record if any at head of tx_list.
@@ -2427,10 +2427,17 @@ static void tx_work_handler(struct work_struct *work)
return;
if (mutex_trylock(&tls_ctx->tx_lock)) {
+ bool retry;
+ int ret;
+
lock_sock(sk);
- tls_tx_records(sk, -1);
+ ret = tls_tx_records(sk, MSG_DONTWAIT);
+ retry = ret == -EAGAIN && tcp_rtx_and_write_queues_empty(sk);
release_sock(sk);
mutex_unlock(&tls_ctx->tx_lock);
+ if (retry &&
+ !test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
+ schedule_delayed_work(&ctx->tx_work.work, 1);
} else if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
/* Someone is holding the tx_lock, they will likely run Tx
* and cancel the work on their way out of the lock section.
--
2.43.0
Technical Details
Message-ID:
<20261128-ktls-close-v2a-v2-1-68a955cbbbcb@certik.com>Path:
mbox-mo1.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373154-linux-kernel-netdev=dewy.ruShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373154-linux-kernel-netdev=dewy.ru From: devnull+dickson.wu.certik.com@kernel.org (Dickson Wu via B4 Relay) Newsgroups: linux.kernel.netdev Subject: [PATCH net v2] tls: avoid blocking sends from workqueue context Date: Sat, 28 Nov 2026 13:20:00 +0000 Organization: Usenet Network Message-ID: <20261128-ktls-close-v2a-v2-1-68a955cbbbcb@certik.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Injection-Info: usenet.network; logging-data="963457"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:b6KuKwaN1mcyUmF30WnktPZId3U= sha256:Z3jJ9+07aR+pDDIpYml1Y8g3aNKcIuMAvHhFzYopQ/s= sha1:bPd3SCUJ6R+xivjI2cupUZXBxJo= sha256:SuuOsJDItot2Z5LH2qHyyGpYSb2whfRX8YyrQwmshI0= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id B844A125F76; Sat, 28 Nov 2026 17:14:09 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.4 X-Spam-Status: No, score=-3.4 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id aUcenEkq6wsd; Sat, 28 Nov 2026 17:14:06 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-373154-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id D7EBE125F7D; Sat, 28 Nov 2026 17:13:57 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 02C4A298F6; Sat, 28 Nov 2026 14:13:49 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795875228; b=fBaN1Y8+wG+P0lq5cxQHaltIbbSt354Punnw1gKCQbLCPI4TQxULDr9pQiLZIaR+DRi/ +pdqXcXk+wcayDPrJnb79KTkeAGK61QbYwtS8rBL0FsdGVrTzrmD7gD+LjXTmcFjTDZWN cSvL8UFj1gq9oExSRIv6jleOlr0xvfbiEyNrVkH7fqpf8C/ymd9KJ5W/BECcMOkI0IdoD R6mdyNNffEG2np6axV78lwob2LXaJKfliXblGqwa1V3lc4AEHMNPF4VdViCAxlpD3m/Y8 GcwH+akiKIYB0LiMYMLIgE1sm4ED4IZ3R4ELO2JLL8HD/rZepMwxrSkLZqwt8b98VCQ== X-ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795875228; h=X-Original-To:Received:Received:DKIM-Signature:Received: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version:Content-Type: Message-Id:X-B4-Tracking:X-Change-ID:From:Date:Subject:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=wp0b+DxQGg3ZOaDW9pYxw3lBTvNsoGI7V0WMkW40evo=; b=tR4SWwP5+Kj0ui5qMVQVsp23mSigaXk1q7kQjG+8DfHR+MkdABQmlhKujfT1BZsLbkcU npd2EriGUyuybjT8nDuw6iHjZxAPwWYF27bwkCUC0yb4whkf9sLon3KLuGBMlte7oV/vE /Hn29oyk/6rJc2kx1YRQCYkONkOCvGVD9M59rjU5tO3hReFfDoao8nvmA4JCRDWUKZx1q Njk0q7zpjkUzzj45616j1eaCG1zyPjBAj6/18vOEX/rHA09CxY4XnHk+SvLCfJQYv9Tnc o/GF5YBDDtZleYaz+SLeuHmgkisxyXEGtAwfIHz0/PgTnnsnavASQkCYbcrLy0vrvng== X-ARC-Authentication-Results: i=3; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=jUwYMwJX; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CF385606A325; Sat, 28 Nov 2026 14:13:48 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=jUwYMwJX X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795875228; b=o9J+tdhckqlDPr/+l0poyPXIwhhN8tSwmBIoPP2cpQYdpkTN54KJAy29JcziOSnxtoB0 5F/YAmZFMVJTPHomKi4YnObP69Jo7q9Jn7yURkI/BY//C9TXQVsiB8EJywjUP14n9aIae GQkMryHlms0YNyjjSGYkcQqjbanTSsGuhW8e/CaLvjgNJaXcPE1HSRy/lbPBsF7pIrWzY cSHM/bGdNPa9kFor7kZVAv1GTNd8mjWLisIDClZErRrPcsSAPqZahD6thWq7VMzDla4aS g3N9yGiUPKHmTcTX9I+PLB7QfLk3GKOw3cHrbG8GdYy74QzhCxEPz/jz7vo4ObeOMtQ== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795875228; h=Received:DKIM-Signature:Received:MIME-Version:Content-Type: Message-Id:X-B4-Tracking:X-Change-ID:From:Date:Subject:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=wp0b+DxQGg3ZOaDW9pYxw3lBTvNsoGI7V0WMkW40evo=; b=PPrzuqjU9G8VGU+IZz0pJbVpiZ4Fp04+9+KE++kh3THCjwqWXSt1haKdI2Ic66y5LTna TOkL6C8rE3I9jEzivZMW0V+9v7PNUKncNdpvMKsZu6ta3wRpbe2GChooHaBAOUkRbjvT6 37R8D0L1FC9/Y+2FC+tPTNSHAsOuObKUL1yT7f+eO7Dkc79FOnboVjIRdIGuVx/KYK5M1 pCUy6mlz7yXRtsLSDj3eH7f6OnBudj9/roWSSxTKg3qVzsN/by3Lf6OXZscLMIyIo/8ge v10a4EYB6rd0g6O0nTzeRt66lNiU8MZ/bnKYlqecoKehBtk04ezPX5BfoYpORXQ0YAQ== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=jUwYMwJX; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E8C73606A321 for <netdev@vger.kernel.org>; Sat, 28 Nov 2026 14:13:47 +0000 (UTC) X-Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 X-ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795875227; b=OGQqFYCIYP30SOiBsxmP3SPkK+UtldM9rp7eev5+Oubtt4V0EezIB0pn1rhJHOE4JsyC L0sp5cRz5aGPI83rfW0P3JB/XSyVwpGySp86ZOoDXUuUdGDbt0kZoYlcIdIZL3G0JgbnV FxaFsS78FD84AfHXk4kNGDoGS/nVX8yIP1G4pPgxZIKmbS6WE6CrPyPjbsYVyTMBlTiPB KPxu1ozZBOf1iQRbP2Cza2XjhlOZNaqlJ1S+92Ye0QRiF3Pgay/IKoQYFYD9T4sNgjpua xE318wJr2/P2AObTenYGPdYORxa3Gsza5ciqyp7ink3zbWgzObwj7IgDffl97vSpJ9Q== X-ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795875227; h=DKIM-Signature:Received:MIME-Version:Content-Type:Message-Id: X-B4-Tracking:X-Change-ID:From:Date:Subject:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=wp0b+DxQGg3ZOaDW9pYxw3lBTvNsoGI7V0WMkW40evo=; b=o0mCAYFyUQ7whCSa1+e0R58RDk1zANleXdSwRWQxxUvQy544Ic+kWkUQi3hDx+dWKG3R jwMbHPCROApjOTFZImbtXim8xG3j5AAeRvD+jPteBvxd7NMSO+t7gH2xI5dqXX6sXgEhS kejhegpI2hleYiHn9D0Jl22zRGJieYRPslXOSAI5Bk/hZj9iE9PqUVZRxgILZz5KvD/zM Tr/iuwdhP55KM40v/vxIPYTSc6N5LMk/7tvQHnM8Og8ibn1WvfKg5h1bKARtZiQSpSMx7 YHpwxAbYE3uYiTBu01PzhBd6R4GD6QJbCQ49WSNJl5jg4oQNh99ZdgVgORWAe2EUg+w== X-ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 X-Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 232A91F000C1; Sat, 28 Nov 2026 14:13:47 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795875227; bh=wp0b+DxQGg3ZOaDW9pYxw3lBTvNsoGI7V0WMkW40evo=; h=From:Date:Subject:To:Cc:Reply-To; b=jUwYMwJXjfOqDhbFqH3Tdm8ackDF6peFr6EdkfonZEtwD5fVyEsHRfTf6C75g2UuJ gCkh1HokmAR8Hna7lqRMC8GdyQTyBQ/44U3o0NG3UKkLVHR2auKpJ05qCShsl+MMPj CkBtdnSMoK2i0V0bJJBgrfvrUSC5fqNfTC8tPrxa+Ia0TZHtKmewTRcljRJ1hcp/Ok yGoga9OMOPkPgG/ul+z7sZHl6AZg2aTOu7hbM+vhpm6flf528IxIqFaX9nM1nKQkV9 72WkDMO5iPKs6nDR2eOO3esIXF3M6W+UdlKiZUkezllck+bJbrPWBVQ01HEIMWOpxA 2R+hIj3JQjIzA== X-Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id A97624E0F2; Sat, 28 Nov 2026 14:13:46 +0000 (UTC) X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list X-B4-Tracking: v=1; b=H4sIAMLgCmsC/yWMQQrCMBBFr1Jm7UA7WgleRVxM0qmNhkQysQild zfR5X/89zZQyV4ULt0GWVavPsU66NCBWzjeBf1UN1BP52Egg88SFF1IKrgS45FPM5mxZxkNVOm VZfafX/AKUQrc/lDf9iGutFS7Wa6+zRzd0pBKnEpKARuHff8Cu7YRXpcAAAA= X-Change-ID: 20261128-ktls-close-v2a-3a4f2850ae58 X-Cc: John Fastabend <john.fastabend@gmail.com>, Jakub Kicinski <kuba@kernel.org>, Sabrina Dubroca <sd@queasysnail.net>, "David S. Miller" <davem@davemloft.net>, Eric Dumazet <edumazet@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Willem de Bruijn <willemb@google.com>, Simon Horman <horms@kernel.org>, Vakul Garg <vakul.garg@nxp.com>, Jiayuan Chen <jiayuan.chen@linux.dev>, syzbot+ca1345cca66556f3d79b@syzkaller.appspotmail.com, security@kernel.org X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795875010; l=6182; i=dickson.wu@certik.com; s=dickson-b4; h=from:subject:message-id; bh=iLvFDZaO9/KgsE/Y4B9g540F2tdGfRjJr0ZiapIJSAs=; b=kzfmU9dpLLYJ5eJHQP2aoA6KNV3Nl9W0m0T0YxLvIV7k1iYE63/SFV1K3BDmnhOm6D5TdfJdG DxnMJsGtTtgDSHr2YkccZ7KRanD2ePBPRJpa1TMmuSFd2Os/nqCtBhm X-Developer-Key: i=dickson.wu@certik.com; a=ed25519; pk=fxH7FS4FdojeJSGLJzgKdfO51Rvr4EW30yinj2mc1vg= X-Endpoint-Received: by B4 Relay for dickson.wu@certik.com/dickson-b4 with auth_id=1647 X-Original-From: Dickson Wu <dickson.wu@certik.com> X-Reply-To: dickson.wu@certik.com