🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #19225

[PATCH net v7] tun: bound the NAPI receive queue

#19225
From: Antonio Quartulli
Date: Thu, 19 Nov 2026 15:30
183 lines
16509 bytes
With IFF_NAPI, tun_get_user() queues every written packet for NAPI on
an unbounded list. When the poll runs asynchronously (threaded NAPI or
ksoftirqd) and is slower than the writer, the queue grows until the
system runs out of memory. An unprivileged user can do this from its own
user and network namespaces, and the queued skbs are not charged to its
memcg: in a test VM, a uid 1000 process limited to 128 MB caused a
global OOM in ~6 seconds. The socket send buffer does not help: tun sets
it to INT_MAX by default, the build_skb() path is only used while it is
INT_MAX, and the XDP batch path never checks it.

Drop packets once more than netdev_max_backlog of them are queued, like
enqueue_to_backlog() does, and count them as rx_dropped; the write still
reports success, as for a full backlog. The check sits where packets are
queued for NAPI, in tun_get_user() and in tun_xdp_one() for the batches
vhost-net passes, so packets consumed by XDP or delivered through
IFF_NAPI_FRAGS are not affected. A dropped zerocopy skb completes its
buffer when freed. The check is lockless, so concurrent writers can each
still add one packet.

The NAPI poll moved the whole queue out, processed up to its budget and
put the rest back: while it ran, the queue looked empty to writers, so
with the poll on another CPU the backlog could still grow without limit.
Take at most budget packets instead: the rest stays on the queue writers
check.

Dropping also schedules NAPI. vhost-net sends with MSG_MORE while it has
more packets, and tun_get_user() schedules NAPI only for a packet
without it or once more than NAPI_POLL_WEIGHT are queued. With a
netdev_max_backlog below that, the queue fills first and the packet
without MSG_MORE is dropped too: nothing would schedule NAPI, and the
queued packets, with zerocopy also their buffers, would wait forever.

Reproducer, as an unprivileged user in a new user and network
namespace, without kernel modifications or fault injection:
  ip tuntap add t0 mode tun; ip addr add 10.11.0.1/24 dev t0
  ip link add d0 type dummy; ip addr add 10.20.0.1/24 dev d0
  ip link set t0 up; ip link set d0 up; sysctl net.ipv4.ip_forward=1
  install ~500 iptables FORWARD rules, to slow down the poll, and a
  MASQUERADE rule on d0
  open t0 with IFF_TUN | IFF_NO_PI | IFF_NAPI
  echo 1 > /sys/class/net/t0/threaded
  write() 1400-byte IPv4 packets for 10.20.0.2 in a loop

Found by code inspection while analysing the performance of the tun
write path. Tested in an x86-64 VM on net, with 1 and 2 vCPUs: the
reproducer causes a global OOM without the patch, while with it memory
use stays flat and the forwarding rate is unchanged; the tun kselftest
passes; a local vhost-net test of the batch, copy and zerocopy paths
with netdev_max_backlog set to 8 passes, and fails without the patch;
an XDP program on the device sees every packet while the excess is
dropped.

Still open: neither the packet size nor the queued memory is bounded,
and the skbs are not charged to the writer's memcg. A single write can
create an skb of several hundred KB, so even one queue can hold a lot,
and IFF_MULTI_QUEUE (allowed in a user namespace) and more devices
multiply it. This bounds the queue as enqueue_to_backlog() bounds the
per-CPU backlog; charging the skbs to the writer is left to a follow-up.

Fixes: 943170998b20 ("tun: enable NAPI for TUN/TAP driver")
Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---
v7:
- Check where packets are queued, after XDP and not for IFF_NAPI_FRAGS
  (Sashiko); a dropped zerocopy skb now completes its buffer when freed
- Fix the poll comment and the commit message (Sashiko)
- Drop the vhost-net selftest (Willem, Sashiko)

v6: https://lore.kernel.org/netdev/20261113212237.1742984-1-antonio@mandelbit.com/
v5: https://lore.kernel.org/netdev/20261111014208.1795777-1-antonio@mandelbit.com/
v4: https://lore.kernel.org/netdev/20261103142232.1869185-1-antonio@mandelbit.com/
v3: https://lore.kernel.org/netdev/20261031231444.1528758-1-antonio@mandelbit.com/
v2: https://lore.kernel.org/netdev/20261030152540.1409884-1-antonio@mandelbit.com/

Stable: kernels before v6.9 have no net_hotdata; a backport reads
netdev_max_backlog instead.

 drivers/net/tun.c | 52 +++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 44 insertions(+), 8 deletions(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 86fd44e10755..b7432bd4d3f0 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -58,6 +58,7 @@
 #include <linux/nsproxy.h>
 #include <linux/virtio_net.h>
 #include <linux/rcupdate.h>
+#include <net/hotdata.h>
 #include <net/net_namespace.h>
 #include <net/netns/generic.h>
 #include <net/rtnetlink.h>
@@ -227,6 +228,16 @@ struct veth {
 static void tun_flow_init(struct tun_struct *tun);
 static void tun_flow_uninit(struct tun_struct *tun);
 
+/* In IFF_NAPI mode written packets wait in the tfile's sk_write_queue for the
+ * NAPI poll, which may run later in a kthread or ksoftirqd. Bound that queue
+ * like the per-CPU backlog.
+ */
+static bool tun_napi_queue_full(const struct sk_buff_head *queue)
+{
+	return skb_queue_len_lockless(queue) >
+	       READ_ONCE(net_hotdata.max_backlog);
+}
+
 static int tun_napi_receive(struct napi_struct *napi, int budget)
 {
 	struct tun_file *tfile = container_of(napi, struct tun_file, napi);
@@ -237,21 +248,22 @@ static int tun_napi_receive(struct napi_struct *napi, int budget)
 
 	__skb_queue_head_init(&process_queue);
 
+	/* At most budget skbs: the rest stays on the queue writers check */
 	spin_lock(&queue->lock);
-	skb_queue_splice_tail_init(queue, &process_queue);
+	if (skb_queue_len(queue) <= budget) {
+		skb_queue_splice_tail_init(queue, &process_queue);
+	} else {
+		while (skb_queue_len(&process_queue) < budget &&
+		       (skb = __skb_dequeue(queue)))
+			__skb_queue_tail(&process_queue, skb);
+	}
 	spin_unlock(&queue->lock);
 
-	while (received < budget && (skb = __skb_dequeue(&process_queue))) {
+	while ((skb = __skb_dequeue(&process_queue))) {
 		napi_gro_receive(napi, skb);
 		++received;
 	}
 
-	if (!skb_queue_empty(&process_queue)) {
-		spin_lock(&queue->lock);
-		skb_queue_splice(&process_queue, queue);
-		spin_unlock(&queue->lock);
-	}
-
 	return received;
 }
 
@@ -2043,6 +2055,19 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 		struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
 		int queue_len;
 
+		/* Queue full: drop, and schedule NAPI, which a MSG_MORE sender
+		 * may not have done yet. Lockless: concurrent writers can each
+		 * still add one.
+		 */
+		if (unlikely(tun_napi_queue_full(queue))) {
+			local_bh_disable();
+			napi_schedule(&tfile->napi);
+			local_bh_enable();
+			rcu_read_unlock();
+			drop_reason = SKB_DROP_REASON_FULL_RING;
+			goto drop;
+		}
+
 		spin_lock_bh(&queue->lock);
 
 		if (unlikely(tfile->detached)) {
@@ -2658,6 +2683,17 @@ static int tun_xdp_one(struct tun_struct *tun,
 
 	if (tfile->napi_enabled) {
 		queue = &tfile->sk.sk_write_queue;
+
+		/* Queue full: drop, and schedule NAPI so that it drains even if
+		 * nothing in this batch gets queued.
+		 */
+		if (unlikely(tun_napi_queue_full(queue))) {
+			napi_schedule(&tfile->napi);
+			kfree_skb_reason(skb, SKB_DROP_REASON_FULL_RING);
+			dev_core_stats_rx_dropped_inc(tun->dev);
+			return 0;
+		}
+
 		spin_lock(&queue->lock);
 
 		if (unlikely(tfile->detached)) {

base-commit: ead6fa9a0ab194f02a2f36d634b193e9c5f9a1c6
-- 
2.56.1
Technical Details
Message-ID: <20261119153056.353156-1-antonio@mandelbit.com>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-367115-linux-kernel-netdev=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-367115-linux-kernel-netdev=dewy.ru
From: antonio@mandelbit.com (Antonio Quartulli)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net v7] tun: bound the NAPI receive queue
Date: Thu, 19 Nov 2026 16:30:55 +0100
Organization: Usenet Network
Message-ID: <20261119153056.353156-1-antonio@mandelbit.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2911777"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:PRmvBGNPYRtMu7UF+h+KQOX+gf0= sha256:3ntdh15hVMOIIRbEiy5/SyAK2XGrTgUnabW0hXrkxlQ=
	sha1:hUuodmAl9PgMl6N7q80JrtlpoCQ= sha256:qOlv2axepSpglCLRXjczPHSXYUnf96gDdaG/sbzExuY=
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 1F32A1156C0; Thu, 19 Nov 2026 18:31:38 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.05
X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	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=mandelbit.com
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 OS1BnrEf-h-B;
	Thu, 19 Nov 2026 18:31:31 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-367115-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=mandelbit.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 87A5A1156BA; Thu, 19 Nov 2026 18:31:21 +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 7653F246529; Thu, 19 Nov 2026 15:31:16 +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=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1795102275;
	b=Udi6DBj9I1pYdLGX9td8WkJigOeHnwvUzqCXO9J0JaTKCkeVq54aK5IhO2BGTeDoNV8b
	 wNJlo2U4gii0JTtPJ295tEwEs3wrKzIAt8krQLgjL0u7ZHhthsSoZ11uGqrvgEu+fbRUF
	 gRFCa8z5RKs8qnw6hJFJql3RUBvdDgx/UHpcjtkYnrOhO/X6Utfp5tAta3DBPhf74ZrLZ
	 2Ej8ycFsyeD8NGiyaqO1TvrliuZA6XyD44KJI/hpKCI7QXEm/qy0+vpESNQJnJcDUeYky
	 ZYhvOc3inX83rlpEWcpdBi20sjPY2WdhLDeZUEqvaHKTqjM6jQPI7JqVQggEa3F9W6w==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795102275;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:From:To:
	 Cc:Subject:Date:Message-ID:X-Mailer:X-Mailing-List:List-Id:List-Post:
	 List-Help:List-Subscribe:List-Unsubscribe:List-Owner:Precedence:
	 MIME-Version:Content-Transfer-Encoding:X-Rspamd-Queue-Id;
	bh=0k8H3WhqvZLrLdrKeN3pxsTCp80tQHj2NUfIp60honc=;
	b=jyMgpHDIu1BSKP4DJKmdZUPa4thVGet6sG1cxfnUX3XyTuG++qmR6MUaOinYWSWihRlE
	 TwlHKfC/Ol4VCIRKb9ZXgTr4FzMWnPY37SSMJ+oKQZokkY9d0ndM5385XRxKIxg9PXN/V
	 GyZ+HET31VM7+XprPHqScmGASNaETVODrj9F761SOMaYXyZXMBYIg42+5sr4atEaDqNrm
	 dbLxkGz6ITN9ByHHuMzC2DLbiw+coapKvquTvRCR4IR62T8pCao4u9QRF7IpOYFqx5JFY
	 T1f9uvegwngZm9a7Ar4MwkCPc4/3ArRdVwIy0c8AWQaRTpcLoqijQU+56WWrPpVi6uA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=PfH2lDzH;
	dmarc=none header.from=mandelbit.com;
	spf=pass smtp.mailfrom=mandelbit.com;
	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 814DC606A334;
	Thu, 19 Nov 2026 15:31:15 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=PfH2lDzH
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=195.10.208.47
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795102274;
	b=pn7e8cmwJvIa58UpOC4/nlnenhj7az06ewa5FlnAr/TkNQgqtlYCk1Ygjg1rJs2AvMGs
	 8ikU/SGHsOzEVZhyxO+jMqVbMZhlObec7EswOe9gwfJ/bg7mp46Jdl/Tt0jRRCEurqsgB
	 HUtNJoq6lZ/fCJobTXa2nUTUDCmzaUrjwtSru4Lm64AAL0iu4PfYSkU/oxs4hcem6XElF
	 jXY4iWmiYLO15gfPbeZuF/F/l6MT/Qu2RU1O/ep4+5/dXetSBSILT6QD1shviNg+vSfIV
	 GVND8M36xlGdyzfj6PPA4MZnYZxvwSLfVE6lZvqSJDH4ETsKka5uhP6CxTkcQq1iHiQ==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795102274;
	h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date:
	 Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding:
	 X-Rspamd-Queue-Id;
	bh=0k8H3WhqvZLrLdrKeN3pxsTCp80tQHj2NUfIp60honc=;
	b=5aNLV5P4bhky9pzJlbhcWMOp8KiF5Vbvt583s9JhBTP/f+3KU2DKsVM3sWD6nZ/3QgCs
	 ntOHcYfzob8Po1AZuqL/EUcBS57tzDs5yUxIxGgVBsUUPsvfbIrgPacDi9aUNqFC2uaAn
	 pRct60TCs2VH9W0WdU9jAGF69wh9t/oeO1rjbZCfLxPUHkIqwS3JTGs8SGqWSewt6tSYh
	 6zZej0TTzwxq9yCcnJhPPmCGO7XdARFULbMolW1YVlK++lo8ag4HI5CJWaEf2tPTqp1MB
	 N32KuIeJOKagLc7KU/rfloBV8FUm5tcTQzAhvd6UgskmruMrrF5xH6WESloIsl6lWrg==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=none header.from=mandelbit.com;
	spf=pass smtp.mailfrom=mandelbit.com;
	dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=PfH2lDzH;
	arc=none smtp.remote-ip=195.10.208.47
X-Received: from mout-b-107.mailbox.org (mout-b-107.mailbox.org [195.10.208.47])
	(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.subspace.kernel.org (Postfix) with ESMTPS id 795B8606A320;
	Thu, 19 Nov 2026 15:31:12 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 795B8606A320
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=mandelbit.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mandelbit.com
X-Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by mout-b-107.mailbox.org (Postfix) with ESMTPS id 4jVj9S4FRdz3xTD;
	Thu, 19 Nov 2026 16:31:08 +0100 (CET)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandelbit.com;
	s=MBO0001; t=1795102268;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:cc:mime-version:mime-version:
	 content-transfer-encoding:content-transfer-encoding;
	bh=0k8H3WhqvZLrLdrKeN3pxsTCp80tQHj2NUfIp60honc=;
	b=PfH2lDzH7DFYDEO0JjZgj9hFIaMAF2Eb93achjUr7i8Mz7TOZJXikkZn28r6p7MxEmJS+4
	ekR8jN9JR40ZU59TtJL5/ZOTSaUMEcJ5syiRglTNxY2Yl9m9Cc9bjqqnYi+3xjikcxsJiS
	EiY0eUbHFYcwfhU2uCHqVtGwQeClGukb6kpLRaYlwzGOwv4WXoxS+DFFfEar8o2CbsVXpA
	iQlykGs5jTzL2lHlrMobPlARUtLqk3cVgzHa+GLoBYNj1bDYi1UFX6KwwDt6z74fNPGLHO
	YIDQgQfHKOD6R72bhHPVYXU0JuFCR/2QZAiW2HKcw5njxb072JM8v4GRc6g7fQ==
X-Authentication-Results: outgoing_mbo_mout;
	dkim=none;
	spf=pass (outgoing_mbo_mout: domain of antonio@mandelbit.com designates 2001:67c:2050:b231:465::102 as permitted sender) smtp.mailfrom=antonio@mandelbit.com
X-Cc: Antonio Quartulli <antonio@mandelbit.com>,
	andrew+netdev@lunn.ch,
	davem@davemloft.net,
	edumazet@kernel.org,
	kuba@kernel.org,
	pabeni@redhat.com,
	horms@kernel.org,
	mst@redhat.com,
	eperezma@redhat.com,
	ast@kernel.org,
	daniel@iogearbox.net,
	hawk@kernel.org,
	john.fastabend@gmail.com,
	sdf@fomichev.me,
	maheshb@google.com,
	peterpenkov96@gmail.com,
	netdev@vger.kernel.org,
	virtualization@lists.linux.dev,
	bpf@vger.kernel.org
X-Mailer: git-send-email 2.56.1
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-Rspamd-Queue-Id: 4jVj9S4FRdz3xTD