Article View: linux.kernel.netdev
Article #19341Re: [PATCH net v7] tun: bound the NAPI receive queue
From: Willem de Bruijn
Date: Thu, 19 Nov 2026 19:36
Date: Thu, 19 Nov 2026 19:36
65 lines
14973 bytes
14973 bytes
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 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>
Reviewed-by: Willem de Bruijn <willemb@google.com>
Technical Details
Message-ID:
<willemdebruijn.kernel.24840892273e@gmail.com>Path:
mbox-mo1.pugleaf.net!news-lax.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-367291-linux-kernel-netdev=dewy.ruReferences:
<20261119153056.353156-1-antonio@mandelbit.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.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-367291-linux-kernel-netdev=dewy.ru
From: willemdebruijn.kernel@gmail.com (Willem de Bruijn)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net v7] tun: bound the NAPI receive queue
Date: Thu, 19 Nov 2026 14:36:42 -0500
Organization: Usenet Network
Message-ID: <willemdebruijn.kernel.24840892273e@gmail.com>
References: <20261119153056.353156-1-antonio@mandelbit.com>
MIME-Version: 1.0
Content-Type: text/plain;
charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: usenet.network;
logging-data="2962577"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:hLKYVdAMHcdpmStONzV+KchFAiY= sha256:qOiivxuFJyTO9IsRwc88x5RA+GtkFlNfRubFm+xLnmg=
sha1:y2CAo9BbSk0y561M0UpGDn0iBXI= sha256:lTkDh61RJ6KrLDXrCVYtFENDDUaBPoVmct5OkbbGqko=
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 5A754115753; Thu, 19 Nov 2026 22:37:09 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.149
X-Spam-Status: No, score=-3.149 tagged_above=-9999 required=6.31
tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,
FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001,
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]
autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
header.d=gmail.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 AvamGF_cZPqU;
Thu, 19 Nov 2026 22:37:01 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=netdev+bounces-367291-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74])
by mail.dewy.ru (Postfix) with ESMTPS id CEBDE1156AA; Thu, 19 Nov 2026 22:36:51 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by sto.lore.kernel.org (Postfix) with ESMTP id 20EA660D593; Thu, 19 Nov 2026 19:36:51 +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=1795117006;
b=D6YfR/PdIKOIzZR8C65eDnYYxDyP/5fjiog4JrBDV6kOVQS9XsKNVYx+bz7gailrxPBd
tWgb+RrzbNGnNWJUpetLj6lDdy73Zc7XcbIRXOXM3Sqj9K8Ge+gQmlc5/J5p/uwgeev3I
/bMzhueTfvcA30stxvTAegTPcNR/hnVCkFHxncmRsmDK2uvM6Yo5OLwv6gU5uRKJRcTWT
hVH9x7X0GRh0THe2ph0YwGzvZFrtpja5oJs7rlGcOdLjEGRtrYanF/eZohp85/JsZ+z8l
YiZWNmmySckwFQ26Pz4mP+jxYvsygZcCsD8ZR6k+xfdNfg4ecdUQTiwTFHAgBh4ok9Q==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1795117006;
h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
X-Google-DKIM-Signature:X-Forwarded-Encrypted:X-Gm-Message-State:
X-Gm-Gg:X-Received:Received:Date:From:To:Cc:Message-ID:In-Reply-To:
References:Subject:X-Mailing-List:List-Id:List-Post:List-Help:
List-Subscribe:List-Unsubscribe:List-Owner:Precedence:MIME-Version:
Content-Type:Content-Transfer-Encoding;
bh=Z/2L9IovMD4zBIgZcsGeAkwPGqc72zduF1TmecAsP2U=;
b=VjT0uBfXG5oi3+H8qhWywfrjNL1a11MzdJ1B0/yT+c1cNZY38kJsyRvBh7M3HIBIfU6f
sLCb6DKOpyB0LHzJ2qsgMxDNcdqxenUgk7S4/KMs8R9XhegJ3HwKQzKji04a8XzI6n3G2
IVE918S7Q7lNRWlGJL8lO8i6ZhQtZhFZAxZwm4f/pHPTA+koHn1FvOarCTTL3S4urRG7L
Q6BWjgUGMacPbBhWn/rRwZabU8CchcBAT/r0cce4lWgnJIuVtS2hZecPTGcT6szju/SBK
9Vp+//+0hdrrW43ya4TZqq01iVhmRtxB4X4EM8D0niR4KXye5SDOuKvC2yuN4qlJqLA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=WUKTHXmP;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.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 82631606A32C;
Thu, 19 Nov 2026 19:36:46 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=WUKTHXmP
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.41
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1795117005;
b=PRXmvczakIwE/kyD1XK9kYPwrlOELBk9FoH4njbLO5Jth4WCj6cr81roL2BeVIwFXNXB
9DQ1nPxw688DkfsPpjahyIL9OnjT9AEW7vZ9OH2F91O8i5BrT9jPCNtT3vTkj3Fq0Si6D
0zNyEddalXvShabH8e1ybqNYM072e14WHaWCOEwZNdjSnvRbII3FR9amAH0Y/jtCUky70
kUm3iJX4/FtpnenvAWGITkkdwhXdt7zcxwja4waYSJEyMiBKdru9y/HrBN2C8aJccMH/C
oNaU09vPDOl8nT7pjokJPP/mPImw4JC0C/XcbCiwhUU1kxabL0raLK/pP6HGQuWsbfg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1795117005;
h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received:
Date:From:To:Cc:Message-ID:In-Reply-To:References:Subject:
MIME-Version:Content-Type:Content-Transfer-Encoding;
bh=Z/2L9IovMD4zBIgZcsGeAkwPGqc72zduF1TmecAsP2U=;
b=JuglhaRonAMVQNXWQWcysjFUvipr5ck+SL9aWaq75wRd8X6XaF145tJVpCQmks03P6xg
/lvZzR6LF4MpXngAk5IjsmTi2HESitUTDcptQBwSE/9Hi7wQc/rcG/+dLKZ9GGCTch7vt
wKuihA7ACN+R/oqH8m6rvz1JTihrATHDZFol0RVTQ9OqxyLvZppAvpC5bFujk6N7eYDM+
Ufs4HLPxFIVooCk507mBJh++wn1Ql2Bx8j0YbXGDxZjP3iPPvT6pKhcxh8TJrZzlOSXKi
zZzlyhCd5nrYyeUfuaB9NXEZryU0nLQQoASvPKmK3YhkNr6rljMItWoP5nWBEticP1w==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=WUKTHXmP;
arc=none smtp.remote-ip=209.85.161.41
X-Received: from mail-oo1-f41.google.com (mail-oo1-f41.google.com [209.85.161.41])
(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id E9EFD606A325
for <netdev@vger.kernel.org>; Thu, 19 Nov 2026 19:36:44 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E9EFD606A325
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-oo1-f41.google.com with SMTP id 006d021491bc7-716d052dc23so57380eaf.1
for <netdev@vger.kernel.org>; Thu, 19 Nov 2026 11:36:44 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20260707; t=1795117004; x=1795721804; darn=vger.kernel.org;
h=content-transfer-encoding:content-type:mime-version:subject
:references:in-reply-to:message-id:cc:to:from:date:from:to:cc
:subject:date:message-id:reply-to:content-type;
bh=Z/2L9IovMD4zBIgZcsGeAkwPGqc72zduF1TmecAsP2U=;
b=WUKTHXmPDsd8bmISSW+LqyZEPEQsRE4kJ67FPXvLSxtN1Gmsu2ddKXNZgsXZUWUwlX
hTP0Lu5Przzo9ho8j1uFMOKolRcvpszRuWOMtitvNE/J1tAcwKrhu0FSRXVHewX0bpZZ
MiI5q131r/I2/UIgTUYGB9lP3lSqudnWHs3z44w403i5AG6QNxSi9KLbJluult3ciAPo
EyC+BJGtrDCIm4Ic2a4gLUJfDPHa0JxqcSU+gbl9CWU9kLRa+YzsMc1xfQeqrH9Ug+XP
Flg/hRH7BCgRObF84+fpRBD3MaLKpk8+hIE/dzkyEXUIeDrdOhUEKV+ZkaCdvAeJkX9H
xsrw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1795117004; x=1795721804;
h=content-transfer-encoding:content-type:mime-version:subject
:references:in-reply-to:message-id:cc:to:from:date:x-gm-gg
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to
:content-type;
bh=Z/2L9IovMD4zBIgZcsGeAkwPGqc72zduF1TmecAsP2U=;
b=IWszIJ7aH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vbWBsQPfRHNOYPgf1uU2/rby57qN
Kmerv2/FDK+uI+7hxkKc736dgRbPSKMcQRGXlyIdDMqx2v2rWG0kRLV889HVwpF3JEQsREa2Hrq
deP+sSQIK84aJtE6n7jDAB2zkSOcmSwTJ/SZMQEjus62GB7WaoPU+0fKRXo9uYaIXTa0g2WaE5c
i3W+cz2bWS7Tj5RLFNpcsnJyD8H9bF+ZX6BCOnQgYdsf+tGr2jKKgvhu3lsKEiY6VsH+wHtUlHN
9s74g1+Z1iMg==
X-Received: by 2002:a05:6820:1c98:b0:6b1:bb82:f7a3 with SMTP id 006d021491bc7-71b39209557mr740286eaf.5.1795117003758;
Thu, 19 Nov 2026 11:36:43 -0800 (PST)
X-Received: from gmail.com (15.60.86.34.bc.googleusercontent.com. [34.86.60.15])
by smtp.gmail.com with ESMTPSA id 46e09a7af769-863168dd6e7sm435223a34.12.2026.11.19.11.36.43
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Thu, 19 Nov 2026 11:36:43 -0800 (PST)
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-In-Reply-To: <20261119153056.353156-1-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