Article View: linux.kernel.netdev
Article #9350Re: [PATCH net v3] tun: bound the NAPI receive queue
From: Antonio Quartulli
Date: Sun, 01 Nov 2026 20:17
Date: Sun, 01 Nov 2026 20:17
177 lines
16047 bytes
16047 bytes
Hi,
On 01/11/2026 03:08, Willem de Bruijn wrote:
> Antonio Quartulli wrote:
>> With IFF_NAPI, tun_get_user() queues every written packet for NAPI on
>> an unbounded list. When the poll runs asynchronously (threaded NAPI)
>> 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 cannot bound this: tun sets it to
>> INT_MAX, and the build_skb() and XDP batch paths do not check it.
>>
>> Drop packets once more than netdev_max_backlog of them are queued, like
>> enqueue_to_backlog() does, and count them as rx_dropped: before
>> allocating and copying them where possible, and again under the queue
>> lock before queueing. This covers write(), sendmsg() and the batches of
>> XDP buffers vhost-net passes to tun_sendmsg(), which share the queue.
>> Zerocopy buffers of packets dropped early are completed.
>>
>> 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 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
>>
>> Tested with the reproducer in an x86-64 VM: memory use stays flat
>> and the forwarding rate is unchanged. The vhost-net paths are only
>> build-tested, and the kselftests have not been run yet.
>
> Let's make sure the tun selftests pass.
I went through the tun selftest and it passed.
However, there was no coverage for vhost-net, so I took care of that
with an extra patch (it was actually useful because I caught a hitch in
the actual fix).
>
>> Still open: the limit counts packets rather than bytes and applies per
>> queue, the NAPI poll splices the queue out while it runs so up to about
>> twice the limit can be in flight, and the queued skbs are still not
>> charged to the writer's memcg.
>>
>> Found, fixed and tested with the help of an AI coding assistant
>> (Claude Code).
>>
>> Fixes: 943170998b20 ("tun: enable NAPI for TUN/TAP driver")
>> Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
>> ---
>> v3:
>> - Also bound the sendmsg() paths, which share the queue: zerocopy
>> buffers are completed when dropped early, and the batched XDP buffers
>> of tun_xdp_one() are covered too (Willem)
>> - Drop the IFF_NAPI_FRAGS exclusion: tun_get_user() never queues in
>> that mode (napi_gro_frags() is synchronous), so the early check only
>> fires if tun_xdp_one() filled the same queue, and dropping is fine
>> then too (Willem)
>> - Recheck under the queue lock before queueing (Willem)
>> - Compare with > like enqueue_to_backlog() (Willem)
>> - Keep the includes sorted (Willem)
>> - Add the reproducer and a testing statement to the commit message
>>
>> v2: https://lore.kernel.org/netdev/20261030152540.1409884-1-antonio@mandelbit.com/
>> - Cc netdev and the maintainers
>>
>> Notes:
>> - Affected since 943170998b20 (v4.15). Reproduced on mainline
>> 7be4bc52fd9e; the code is unchanged in net/main 94cea342e891, which
>> this patch is based on and was build-tested (W=1) against.
>> - Mitigation: disallow unprivileged user namespaces or restrict
>> access to /dev/net/tun.
>>
>> drivers/net/tun.c | 39 +++++++++++++++++++++++++++++++++++++++
>> 1 file changed, 39 insertions(+)
>>
>> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
>> index 770b8fe085c3..df5f0cbd1b16 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,15 @@ 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 of netif_rx().
>> + */
>> +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);
>> @@ -1856,6 +1866,20 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
>> zerocopy = true;
>> }
>>
>> + /* Drop before allocating and copying when the NAPI queue is full. The
>> + * check is repeated under the queue lock before queueing.
>> + */
>> + if (tfile->napi_enabled &&
>> + unlikely(tun_napi_queue_full(&tfile->sk.sk_write_queue))) {
>> + if (msg_control) {
>> + struct ubuf_info *uarg = msg_control;
>> +
>> + uarg->ops->complete(NULL, uarg, false);
>> + }
>> + dev_core_stats_rx_dropped_inc(tun->dev);
>> + return total_len;
>> + }
>> +
>> if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
>> /* For the packet that is not easy to be processed
>> * (e.g gso or jumbo packet), we will do it at after
>> @@ -2045,6 +2069,14 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
>> goto free_skb;
>> }
>>
>> + if (unlikely(tun_napi_queue_full(queue))) {
>> + spin_unlock_bh(&queue->lock);
>> + rcu_read_unlock();
>> + kfree_skb_reason(skb, SKB_DROP_REASON_FULL_RING);
>> + dev_core_stats_rx_dropped_inc(tun->dev);
>> + return total_len;
>
> consider goto free_skb instead of open coding.
ACK.
Regards,
>> + }
>> +
>> __skb_queue_tail(queue, skb);
>> queue_len = skb_queue_len(queue);
>> spin_unlock(&queue->lock);
>> @@ -2649,6 +2681,13 @@ static int tun_xdp_one(struct tun_struct *tun,
>> return -EBUSY;
>> }
>>
>> + if (unlikely(tun_napi_queue_full(queue))) {
>> + spin_unlock(&queue->lock);
>> + kfree_skb_reason(skb, SKB_DROP_REASON_FULL_RING);
>> + dev_core_stats_rx_dropped_inc(tun->dev);
>> + return 0;
>> + }
>> +
>> __skb_queue_tail(queue, skb);
>> spin_unlock(&queue->lock);
>> ret = 1;
>> --
>> 2.56.0
>>
>
--
Antonio Quartulli
CEO and Co-Founder
Mandelbit Srl
https://www.mandelbit.com
Technical Details
Message-ID:
<21b92f81-e3f1-44ba-872e-7e860ee92518@mandelbit.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-354330-linux-kernel-netdev=dewy.ruReferences:
<20261031231444.1528758-1-antonio@mandelbit.com> <willemdebruijn.kernel.10cf6c1afbcd@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-354330-linux-kernel-netdev=dewy.ru From: antonio@mandelbit.com (Antonio Quartulli) Newsgroups: linux.kernel.netdev Subject: Re: [PATCH net v3] tun: bound the NAPI receive queue Date: Sun, 1 Nov 2026 21:17:50 +0100 Organization: Usenet Network Message-ID: <21b92f81-e3f1-44ba-872e-7e860ee92518@mandelbit.com> References: <20261031231444.1528758-1-antonio@mandelbit.com> <willemdebruijn.kernel.10cf6c1afbcd@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: usenet.network; logging-data="2760594"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:I92STFHncrl/mgqKzyPIM0Qh0zg= sha256:Xe8n1Ja9l3+2TFBQp0bwTR4+DWn5atOjD1rYqJqeG2c= sha1:dpjDtJWODoS5rc7zs1uTILrhO2A= sha256:zbiZotbj2S942GqZQZs9X8HFHmhgi+4am5vfEpk9nkw= Content-Language: en-US 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 359F0112391; Sun, 1 Nov 2026 23:19:54 +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.25, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-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 VKhFleqP6N7c; Sun, 1 Nov 2026 23:19:46 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-354330-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 495CC11235C; Sun, 1 Nov 2026 23:19:35 +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 9096E29DDC; Sun, 1 Nov 2026 20:18:11 +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=1793564291; b=n1XFrSoh5I2BDk8heB1hfuFFDL4+Q0FzM8JvjvrlWslCeqWat7Z3ICSUpp01ZOsjfxg8 IwPHEOyrt2M2QPC2FHlRm1kvAZ6djvYLm/YdlUvi5I9kkBssnFC4nTrPh8Z0l9IrsSzoT 7YjZPUAjpsETZKHsD24dOofMzf3uiVdrTbFsd1Lp99Dig/rluKUYJvrH8Ck06f3xW5ZwV 1BXj12g12h2y/3aEwSSjC3vabnvJb3+PB686YD5PK7CRpsLW32IyOwpXExs2bY9qqm0GZ LuTQQEfJjSdiIUH5SWgK+4Nmovg5St/w6qXT3Y83+1oGdgWzqFh+b90EqPml807+wOg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793564291; h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature: Message-ID:Date:X-Mailing-List:List-Id:List-Post:List-Help: List-Subscribe:List-Unsubscribe:List-Owner:Precedence:MIME-Version: User-Agent:Subject:To:Cc:References:Content-Language:From: Organization:In-Reply-To:Content-Type:Content-Transfer-Encoding: X-Rspamd-Queue-Id; bh=T+DAZYhHZ3A1qaqacQjdw2FiIWYF+HluMV1uZpQB+W4=; b=rcQWn0WongQk8zxiFNvh0Kr4Ck4UXA9OQAxG7uT3onS7/LU7mEqmpblNE6M+ma8tt8V7 IF3ZOxftW6tluMdxb0jRp1Et+WEfIbhmBSU/KqLdxHbMIKwiVEKmVNIFxYzhCkBesCh42 WzciDWjt7xHSkX8wEDEXMR0i5OVaLYTMNe+4OlKs0t/xizORBnXBtSbBUsV45gn0UfDVb ShtWgvdHnDsRS8akDm1drCtBrk12T6R5SdiwhW1BF2pk2fkCSyu5QSEFqCZuZAZMBDMt2 km+ZXafsqtodoZv7Vw4oGytdThvv27JXH+zIaK2Mgdc3fhhmisAhKT+q6rz8tP9k2Xg== 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=JgN6JTws; 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 67B91606A324; Sun, 1 Nov 2026 20:18:11 +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=JgN6JTws X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=195.10.208.55 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793564284; b=3ukp3rpUdxU3AvQVEGGYXpePqg8W6sEQKuSdVOqH9DoNSo6xWHqo9lt/NoLLb4y/oFF8 MVRYVmJngH8lWcAR1gWVHPvzr3O/2ZbS5XVhq1qMqlS9R/J3X04nLvWvJN+BOLRj0Rjbr oJhs/F3i0WGhXSuZvtPyFoHt27GB6gYwpbTI+EmDSTD6fNSilX9iRvXzpM/ecnx/++R18 eJaCcAytCqML9cQBP5Ez14HeyhnHe7kHfr/J+kOgFO4xwJ5Cb+Rw/JSXxpnBBnMp2InR7 g19hD6d1fkhxuqcNHqGbUjMMGlc9NOEyxi6HgfT168U6MoPhQLMs0p4DeCx5QPtF7ZQ== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793564284; h=DMARC-Filter:Received:DKIM-Signature:Message-ID:Date:MIME-Version: User-Agent:Subject:To:Cc:References:Content-Language:From: Organization:In-Reply-To:Content-Type:Content-Transfer-Encoding: X-Rspamd-Queue-Id; bh=T+DAZYhHZ3A1qaqacQjdw2FiIWYF+HluMV1uZpQB+W4=; b=c3uQM9ZDkcEg9oLQ0Ix9kH8CHmSgdH1s6pVFx9W52sYjlxePO+jUt0ePCJ8PFHJ5Yo9Z QanbcOuWL7ZK5an1y4HY4EiUaUQsvc6ynL6i5iyPAPc2knjzcBvLUDNaiivGDwSU9p1+w sQFR6W4NwV8/MXejOMFP+OxrMBfwMSqM9136azJzKYw3S2COXUTwc6sQMWyH/x21B83cv MT+ntrtQawqxjAKB+nFuOFbVfhTeQNe1MsyUkCVIl/bOyRpuNSGDCXUj6gXU6nG+bEcUc FHKghvLKUn5b8Pc941jeEHIXTR7iERmDyt1SWrMmSHJ6rqhf2szv/8dPyQjWmH/7+lw== 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=JgN6JTws; arc=none smtp.remote-ip=195.10.208.55 X-Received: from mout-b-110.mailbox.org (mout-b-110.mailbox.org [195.10.208.55]) (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 AADF6606A327 for <netdev@vger.kernel.org>; Sun, 1 Nov 2026 20:18:02 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AADF6606A327 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 smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (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-110.mailbox.org (Postfix) with ESMTPS id 4jHmNc4jvyzNlST; Sun, 01 Nov 2026 21:17:52 +0100 (CET) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandelbit.com; s=MBO0001; t=1793564272; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=T+DAZYhHZ3A1qaqacQjdw2FiIWYF+HluMV1uZpQB+W4=; b=JgN6JTwspkmM1n6hl1C8hIlyXw5GbR+4FfbZWS2/m9ua/tM2KRahJ8pOtJTtpN7378pmF0 HDXDvwT1onmFVrRhsJeJSrP5hOFYeRP0vVodDE4dzlWf1OTEuhRu11SM+WhAP8NG/8PYLQ yvi3aVb+qj86TAh1OAh3I042IzJj8nKKk2ADF7JI4tNs2xZVcxudsrh27RYrSmyINSd4ZC cX01oUfsoh2WeXFGfAcRtUkd4B/r7pUCg1NErgZWfHz1WCrgKJGCsIxn/Ec3SuV4SESkQ8 4tE+qKCwlZEgHHan3A09RVLUZBaxexlOsqYbLQbjeoq53RYGcv3yYvawfL896g== X-Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of antonio@mandelbit.com designates 2001:67c:2050:b231:465::202 as permitted sender) smtp.mailfrom=antonio@mandelbit.com 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-User-Agent: Mozilla Thunderbird X-Cc: andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, netdev@vger.kernel.org X-Organization: Mandelbit SRL X-In-Reply-To: <willemdebruijn.kernel.10cf6c1afbcd@gmail.com> X-Rspamd-Queue-Id: 4jHmNc4jvyzNlST