From: netdev-bot+sashiko@kernel.org
To: antonio@mandelbit.com
Cc: willemdebruijn.kernel@gmail.com, jasowangio@gmail.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
Subject: Re: [PATCH net v7] tun: bound the NAPI receive queue
Date: Sat, 21 Nov 2026 23:37:22 +0000 [thread overview]
Message-ID: <179530424233.3135.6226986969775563090@kernel.org> (raw)
In-Reply-To: <20261119153056.353156-1-antonio@mandelbit.com>
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 · Low: 0
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] Bounding the IFF_NAPI queue by packet count only
(net_hotdata.max_backlog skbs per tun_file, ignoring size) does not…
- [Medium] The patch silently changes behaviour for IFF_NAPI users who
set a finite send buffer with TUNSETSNDBUF, and the commit message…
--
tun: bound the NAPI receive queue
This patch caps the IFF_NAPI sk_write_queue of each tun_file at
net_hotdata.max_backlog packets. Excess packets are dropped and counted as
rx_dropped in tun_get_user() and tun_xdp_one(). It also changes
tun_napi_receive() to take at most budget skbs from the queue, so the rest
stays visible to writers.
> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> index 86fd44e107550..b7432bd4d3f03 100644
> --- a/drivers/net/tun.c
> +++ b/drivers/net/tun.c
[ ... ]
> @@ -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);
> +}
[Severity: High]
Is a packet count limit in tun_napi_queue_full() enough to prevent the
global OOM described in the commit message?
tun_get_user() takes total_len = iov_iter_count(from) with no upper bound.
tun_alloc_skb() can then build an skb with up to ~544 KB of frags plus a
linear remainder:
drivers/net/tun.c:tun_alloc_skb() {
...
if (len - linear > MAX_SKB_FRAGS * (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
linear = len - MAX_SKB_FRAGS * (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER);
skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
&err, PAGE_ALLOC_COSTLY_ORDER);
...
}
tun_chr_open() sets sk_sndbuf to INT_MAX by default. With that, a single
tun_file can still hold about max_backlog * truesize. That is tens of MB
with 64 KB writes, and much more with multi-MB writes.
The limit is also per tun_file. With IFF_MULTI_QUEUE, tun_set_iff() allows
MAX_TAP_QUEUES (256) queues per device. The only gate is ns_capable() in
the user's own namespace:
int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
MAX_TAP_QUEUES : 1;
Even with 1400-byte packets that is roughly 256 * 1000 * ~2.3 KB, or about
590 MB per device. A namespace can also create several devices.
The commit message lists this under "Still open". Even so, the same uid
1000 process limited to a 128 MB memcg looks able to reach a global OOM
with larger writes, more queues or more devices.
Would this need a truesize based limit, or charging the queued skbs to the
writer's memcg, to close the reported DoS?
[ ... ]
> @@ -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;
> + }
[Severity: Medium]
Is this behaviour change intended for IFF_NAPI users who set a finite send
buffer with TUNSETSNDBUF?
With a finite sk_sndbuf, tun_can_build_skb() refuses the build_skb path:
if (tfile->socket.sk->sk_sndbuf != INT_MAX)
return false;
So every write goes through tun_alloc_skb()->sock_alloc_send_pskb(), which
charges sk_wmem_alloc.
Before this patch, skbs on sk_write_queue kept their wmem charge until the
NAPI poll consumed them. A slow poll made a blocking writer sleep in
sock_alloc_send_pskb(), and gave a non-blocking writer or vhost-net
-EAGAIN.
After this patch, this branch fires first once sndbuf is larger than about
max_backlog * truesize. That is about 2.3 MB for 1400-byte packets with the
default max_backlog of 1000. The skb is freed, which releases its wmem, and
tun_get_user() returns total_len.
The writer then never blocks and never gets -EAGAIN. Lossless backpressure
that the user configured becomes silent packet loss. The commit message
only mentions sndbuf as being INT_MAX by default.
Could this be documented? Or could a full queue block or return -EAGAIN for
writers on the charged path? Exempting finite-sndbuf queues alone would not
be enough, since the same user could set sndbuf to INT_MAX - 1 to get
around the cap.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119153056.353156-1-antonio%40mandelbit.com
prev parent reply other threads:[~2026-11-21 23:37 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 15:30 [PATCH net v7] tun: bound the NAPI receive queue Antonio Quartulli
2026-11-19 19:36 ` Willem de Bruijn
2026-11-21 23:37 ` netdev-bot+sashiko [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179530424233.3135.6226986969775563090@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=andrew+netdev@lunn.ch \
--cc=antonio@mandelbit.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=eperezma@redhat.com \
--cc=hawk@kernel.org \
--cc=horms@kernel.org \
--cc=jasowangio@gmail.com \
--cc=john.fastabend@gmail.com \
--cc=kuba@kernel.org \
--cc=maheshb@google.com \
--cc=mst@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=peterpenkov96@gmail.com \
--cc=sdf@fomichev.me \
--cc=virtualization@lists.linux.dev \
--cc=willemdebruijn.kernel@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox