mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Stefano Garzarella <sgarzare@redhat.com>
To: Jia Jia <physicalmtea@gmail.com>
Cc: netdev@vger.kernel.org, stefanha@redhat.com,
	bobbyeshleman@gmail.com,  mst@redhat.com, jasowangio@gmail.com,
	eperezma@redhat.com,  xuanzhuo@linux.alibaba.com,
	davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
	 pabeni@redhat.com, horms@kernel.org, kvm@vger.kernel.org,
	 virtualization@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: Re: [PATCH net-next v5 3/5] vsock/virtio: amortize RX socket locking for stream packets
Date: Fri, 13 Nov 2026 18:13:34 +0100	[thread overview]
Message-ID: <avdAV2RholA4ana_@sgarzare-redhat> (raw)
In-Reply-To: <CAJwVaM+SEhbFtORYgVYNayiQED63sCLO5ibwgkcjBRVvMd=KsA@mail.gmail.com>

On Fri, Nov 13, 2026 at 04:09:48PM +0800, Jia Jia wrote:
> >
> > On Fri, Nov 06, 2026 at 08:27:51PM +0800, Jia Jia wrote:
> > >>
> > >> On Tue, Nov 03, 2026 at 12:11:32PM +0800, Jia Jia wrote:

[...]

> > >> >+
> > >> >+/* The caller holds sk's socket lock. */
> > >> >+static bool
> > >> >+virtio_transport_rx_batch_can_continue(struct virtio_transport *t,
> > >> >+                                     struct sock *sk, u32 len)
> > >> >+{
> > >> >+      struct virtio_vsock_sock *vvs;
> > >> >+      struct vsock_sock *vsk;
> > >> >+
> > >> >+      vsk = vsock_sk(sk);
> > >> >+      vvs = vsk->trans;
> > >> >+
> > >> >+      /*
> > >> >+       * Keep a connected lookup reference only for the native, established
> > >> >+       * STREAM path. Sockets outside ESTABLISHED can change table membership;
> > >> >+       * a completed or reassigned socket must resume the per-packet path.
> > >> >+       */
> > >> >+      if (sk->sk_state != TCP_ESTABLISHED ||
> > >> >+          sk->sk_type != SOCK_STREAM ||
> > >> >+          sock_flag(sk, SOCK_DONE) ||
> > >> >+          vsk->transport != &t->transport || !vvs)
> > >> >+              return false;
> > >> >+
> > >> >+      /*
> > >> >+       * Do not admit a packet that would hit an existing RX limit while the
> > >> >+       * worker owns the socket lock. The caller holds that lock, so no reader can
> > >> >+       * dequeue until the batch is released.
> > >> >+       */
> > >> >+      return (u64)vvs->buf_used + len <= vvs->buf_alloc &&
> > >> >+             !virtio_transport_rx_buf_full(vvs);
> > >>
> > >
> > >Hi Stefano,
> > >
> > >> I don't understand this. We recently introduce the collapse, so when we
> > >> will call virtio_transport_recv_enqueue(), all the queue will be
> > >> collapsed, including packets in the batch.
> > >>
> > >> Or am I missing something?
> > >>
> > >
> > >Yes, collapse operates on the same receive queue, which also contains
> > >skbs queued earlier in the current batch.
> > >
> > >The rx_buf_full() check is not intended to bypass collapse. It prevents
> > >the batch from retaining the socket lock while processing a packet when
> > >there is no metadata room for another skb. Collapse is best effort:
> > >some skbs cannot be merged, and allocation of a replacement skb can
> >
> > IIUC batches are enabled only for SOCK_STREAM, so, which skbs can't be
> > merged?
> 
> My wording in the previous reply may not have been precise. What I meant
> is that collapse is not guaranteed to free the skb slot needed for the
> incoming packet for every valid receive-buffer configuration.
> 
> With the default 256K receive buffer, a queue of small STREAM skbs will
> normally contain multiple collapse candidates by the time it reaches
> the metadata limit, so collapse will normally make room.
> 
> However, SO_VM_SOCKETS_BUFFER_SIZE is configurable through setsockopt().
> A sufficiently small but valid receive buffer can make queue_max equal
> to one. After the first small packet has been queued, that skb is
> eligible for collapse, but there is no adjacent skb to merge it with.
> virtio_transport_collapse_size() therefore returns only that skb's own
> length, and the skb is kept unchanged.
> 
> The incoming second skb is not in rx_queue yet. Provided that the two
> packets combined payload still fits within buf_alloc, the payload check
> passes, but virtio_transport_inc_rx_pkt() rejects the second packet on
> the metadata check. This happens before the later tail-copy optimization
> is reached.

I'm lost to be honest, why the secondo skb is not collapsed?

> 
> Therefore, this case does not require an ineligible STREAM skb or an
> allocation failure. Collapse simply cannot reduce a one-element queue.
> 
> If my understanding is wrong, please correct me. Thanks.
> 
> >
> > If allocation fails, I guess we have other issues, no?
> >
> 
> Yes. I thought about that again, and allocation failure is not the
> reason for this check. Thanks
> 
> > >fail. If the packet remained in the batch and collapse did not make
> > >enough room, virtio_transport_inc_rx_pkt() would fail and
> > >virtio_transport_recv_connected() would reset the socket with ENOBUFS
> > >while a waiting reader was still unable to drain the queue.
> > >
> > >When the check makes can_continue() return false before the current
> > >packet has been handled, the caller releases the socket lock and passes
> > >the packet to virtio_transport_recv_pkt(). This gives a waiting reader a
> > >chance to make progress. If the queue is still at the metadata limit,
> > >the normal enqueue path attempts collapse. If that still cannot make
> > >enough room, the existing ENOBUFS reset occurs.
> >
> > I really don't see the difference without batching :-(
> >
> > I may be missing something.
> >
> 
> The difference is the release_sock() boundary between packets.
> 
> In the regular per-packet path, the socket lock is released after the
> first packet. A reader woken by that packet can compete for the lock and
> fully dequeue the only skb before RX locks the socket for the second
> packet. If it succeeds, the second packet can be accepted. If it does
> not acquire the lock in time, or only partially consumes the skb, the
> queue length remains one and the regular path can still reach the
> existing ENOBUFS reset.
> 
> Without the rx_buf_full() check, two same-flow packets processed in the
> same active batch remain under one uninterrupted socket-lock ownership
> interval. Once the first packet leaves the only skb in the queue, the
> reader cannot dequeue it before the second packet is processed.

Why?

> Collapse cannot reduce that one-element queue, so the second packet
> fails the metadata check and resets the connection.

What is unclear to me is why this is not happening without batching.

And, why we can't collapse the batch it self...

Thanks,
Stefano

> 
> The check detects this metadata boundary after the first packet and
> finishes the batch. It therefore restores the lock-release opportunity
> that the regular per-packet path had at the same point. It does not
> guarantee that the reader acquires the lock first; it preserves the
> original opportunity to do so.
> 
> Without this check, batching would remove a successful interleaving
> available to the regular path. For this valid receive-buffer
> configuration, when both packets are processed in the same uninterrupted
> batch, an otherwise avoidable reset would become unavoidable. That
> would be a functional regression introduced by batching.
> 
> Thanks,
> Jia
> 
> > Thanks,
> > Stefano
> >
> 


  reply	other threads:[~2026-11-13 17:13 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03  4:11 [PATCH net-next v5 0/5] vsock/virtio: reduce RX per-packet socket overhead Jia Jia
2026-11-03  4:11 ` [PATCH net-next v5 1/5] vsock/virtio: split socket lookup from locked RX processing Jia Jia
2026-11-05  9:04   ` Stefano Garzarella
2026-11-03  4:11 ` [PATCH net-next v5 2/5] vsock/virtio: factor RX skb metadata budget check Jia Jia
2026-11-05  9:06   ` Stefano Garzarella
2026-11-06 12:28     ` Jia Jia
2026-11-03  4:11 ` [PATCH net-next v5 3/5] vsock/virtio: amortize RX socket locking for stream packets Jia Jia
2026-11-04 19:26   ` Bobby Eshleman
2026-11-05  8:16     ` Jia Jia
2026-11-05  8:57   ` Stefano Garzarella
2026-11-06 12:27     ` Jia Jia
2026-11-12 16:25       ` Stefano Garzarella
2026-11-13  8:09         ` Jia Jia
2026-11-13 17:13           ` Stefano Garzarella [this message]
2026-11-03  4:11 ` [PATCH net-next v5 4/5] vsock/virtio: coalesce native RX write-space notifications Jia Jia
2026-11-05  9:21   ` Stefano Garzarella
2026-11-06 12:49     ` Jia Jia
2026-11-12 17:23       ` Bobby Eshleman
2026-11-03  4:11 ` [PATCH net-next v5 5/5] vsock/virtio: defer native RX readiness notifications until batch unlock Jia Jia
2026-11-05  9:38   ` Stefano Garzarella
2026-11-06 13:33     ` Jia Jia
2026-11-12 18:31   ` Bobby Eshleman
  -- strict thread matches above, loose matches on Subject: below --
2026-10-23  7:51 [PATCH net-next v5 0/5] vsock/virtio: reduce RX per-packet socket overhead Jia Jia
2026-10-23  7:51 ` [PATCH net-next v5 3/5] vsock/virtio: amortize RX socket locking for stream packets Jia Jia

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=avdAV2RholA4ana_@sgarzare-redhat \
    --to=sgarzare@redhat.com \
    --cc=bobbyeshleman@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=eperezma@redhat.com \
    --cc=horms@kernel.org \
    --cc=jasowangio@gmail.com \
    --cc=kuba@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=physicalmtea@gmail.com \
    --cc=stefanha@redhat.com \
    --cc=virtualization@lists.linux.dev \
    --cc=xuanzhuo@linux.alibaba.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

all inboxes | Powered by JetHome®