All of lore.kernel.org
 help / color / mirror / Atom feed
From: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
To: Antonio Quartulli <antonio@mandelbit.com>,
	 willemdebruijn.kernel@gmail.com,  jasowangio@gmail.com
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
Subject: Re: [PATCH net v7] tun: bound the NAPI receive queue
Date: Thu, 19 Nov 2026 14:36:42 -0500	[thread overview]
Message-ID: <willemdebruijn.kernel.24840892273e@gmail.com> (raw)
In-Reply-To: <20261119153056.353156-1-antonio@mandelbit.com>

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>

      parent reply	other threads:[~2026-11-19 19:36 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 15:40 ` sashiko-bot
2026-11-19 19:36 ` Willem de Bruijn [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=willemdebruijn.kernel.24840892273e@gmail.com \
    --to=willemdebruijn.kernel@gmail.com \
    --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 \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.