Linux virtualization list
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sergio Lopez" <slp@redhat.com>
Cc: virtualization@lists.linux.dev, kvm@vger.kernel.org,
	bpf@vger.kernel.org, linux-hyperv@vger.kernel.org,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Eugenio Perez" <eperezma@redhat.com>
Subject: Re: [PATCH RFC v4 6/8] vsock: allow datagrams to use directional transports
Date: Tue, 24 Nov 2026 12:44:18 +0000	[thread overview]
Message-ID: <sashiko-outbox-231167@kernel.org> (raw)
In-Reply-To: <20261124-vsock-dgram-support-v4-6-3607484e85e8@redhat.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] vsock: inconsistent loopback source CID check in
  vsock_dgram_get_transport()
- [Medium] vsock: missing VMADDR_CID_ANY check in
  vsock_dgram_resolve_cid()
- [Medium] vsock: per-packet global vsock_register_mutex lock on
  datagram hot paths
--

commit 21473b8a230a0146483d0435292761949197fc7c
Author: Sergio Lopez <slp@redhat.com>

vsock: allow datagrams to use directional transports

This commit enables AF_VSOCK datagram sockets to route packets across
directional (G2H/H2G) and loopback transports while retaining VMCI as a
fallback endpoint owner and unifying datagram receive queueing in the core.

Link: https://lore.kernel.org/all/sashiko-outbox-213343@kernel.org/
Link: https://lore.kernel.org/all/DMGNAV67LK3F.2FNLTD5NVHDEA@gmail.com/

> diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
> index aef5389eeca2c..1880ff8ed9ca3 100644
> --- a/net/vmw_vsock/af_vsock.c
> +++ b/net/vmw_vsock/af_vsock.c

[ ... ]

> @@ -724,6 +802,113 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk)
>  }
>  EXPORT_SYMBOL_GPL(vsock_assign_transport);
>  
> +/**
> + * vsock_dgram_get_transport - select a transport for a datagram destination
> + * @vsk: socket whose datagram state will be used
> + * @remote_addr: destination address
> + * @vmci_self: wildcard destination resolved to the VMCI owner's local CID
> + *
> + * The caller must hold the socket lock and release the returned transport's
> + * module reference after use. A datagram fallback owns its socket's binding,
> + * but a directional transport takes precedence when it can reach the peer.
> + * Parent fallback sets VMADDR_FLAG_TO_HOST to preserve that route on connect.
> + *
> + * Return: a referenced transport or an ERR_PTR on failure.
> + */
> +const struct vsock_transport *
> +vsock_dgram_get_transport(struct vsock_sock *vsk,
> +			  struct sockaddr_vm *remote_addr, bool vmci_self)
> +{

[ ... ]

> +	if (!transport)
> +		goto out;
> +	if (owner->dgram_transport_match &&
> +	    !owner->dgram_transport_match(transport)) {
> +		err = -ESOCKTNOSUPPORT;
> +		goto out;
> +	}
> +	/* Loopback source addresses must route replies back to loopback. */
> +	if (shared && transport == transport_local &&
> +	    vsk->local_addr.svm_cid != VMADDR_CID_ANY &&
> +	    !vsock_use_local_transport(vsk->local_addr.svm_cid)) {
> +		err = -EADDRNOTAVAIL;
> +		goto out;
> +	}

[Severity: Medium]
Does this loopback source check miss the VMCI exclusions that
vsock_select_transport() applies?

For a VMCI-owned datagram socket in global mode, vsock_select_transport()
routes both owner->get_local_cid() and VMADDR_CID_HOST away from
transport_local even when vsock_use_local_transport() returns true.
Checking only vsock_use_local_transport(vsk->local_addr.svm_cid) here lets
a socket explicitly bound to VMADDR_CID_HOST (when no active G2H CID is
present) or to VMCI's local CID (when VMCI is registered as transport_g2h)
send or connect over transport_local, but replies to that source CID and
connected-peer verification in virtio_transport_dgram_source_allow()
resolve to vmci_transport instead of transport_local, causing connected
loopback receivers to drop the packets.

[ ... ]

> @@ -1461,6 +1660,31 @@ static int vsock_read_skb(struct sock *sk, skb_read_actor_t read_actor)
>  	return vsk->transport->read_skb(vsk, read_actor);
>  }
>  
> +static int vsock_dgram_resolve_cid(struct vsock_sock *vsk,
> +				   struct sockaddr_vm *remote_addr,
> +				   bool *vmci_self)
> +{
> +	const struct vsock_transport *owner = vsk->transport;
> +
> +	if (remote_addr->svm_cid != VMADDR_CID_ANY)
> +		return 0;
> +
> +	mutex_lock(&vsock_register_mutex);
> +	if (owner->dgram_fallback && vsock_net_mode_global(vsk) &&
> +	    vsk->local_addr.svm_cid == owner->get_local_cid()) {
> +		remote_addr->svm_cid = vsk->local_addr.svm_cid;
> +		*vmci_self = true;
> +	} else if (transport_local || owner->dgram_transport_match) {
> +		remote_addr->svm_cid = VMADDR_CID_LOCAL;
> +	} else {
> +		remote_addr->svm_cid = owner->get_local_cid();
> +		*vmci_self = owner->dgram_fallback;
> +	}
> +	mutex_unlock(&vsock_register_mutex);
> +
> +	return remote_addr->svm_cid == VMADDR_CID_ANY ? -EADDRNOTAVAIL : 0;
> +}

[Severity: Medium]
Should the first branch also check that vsk->local_addr.svm_cid !=
VMADDR_CID_ANY?

vmci_transport_init() registers vmci_transport as the datagram fallback at
module load time even when neither VMCI guest nor host personality is
active, in which case owner->get_local_cid() returns VMCI_INVALID_ID
(VMADDR_CID_ANY).  When a wildcard-bound or auto-bound socket calls
connect() or sendmsg() with remote_addr->svm_cid == VMADDR_CID_ANY,
vsk->local_addr.svm_cid == owner->get_local_cid() matches on
VMADDR_CID_ANY, skipping the transport_local branch and failing with
-EADDRNOTAVAIL even when vsock_loopback is loaded.

>  static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
>  			       size_t len)
>  {

[ ... ]

> @@ -1493,42 +1717,34 @@ static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
>  	if (msg->msg_name &&
>  	    vsock_addr_cast(msg->msg_name, msg->msg_namelen,
>  			    &remote_addr) == 0) {
> -		/* Ensure this address is of the right type and is a valid
> -		 * destination.
> -		 */
> -
> -		if (remote_addr->svm_cid == VMADDR_CID_ANY)
> -			remote_addr->svm_cid = transport->get_local_cid();
> -
> -		if (!vsock_addr_bound(remote_addr)) {
> -			err = -EINVAL;
> -			goto out;
> -		}
> +		connected_addr = *remote_addr;
> +		remote_addr = &connected_addr;
>  	} else if (sock->state == SS_CONNECTED) {
> -		remote_addr = &vsk->remote_addr;
> -
> -		if (remote_addr->svm_cid == VMADDR_CID_ANY)
> -			remote_addr->svm_cid = transport->get_local_cid();
> -
> -		/* XXX Should connect() or this function ensure remote_addr is
> -		 * bound?
> -		 */
> -		if (!vsock_addr_bound(&vsk->remote_addr)) {
> -			err = -EINVAL;
> -			goto out;
> -		}
> +		/* A fallback must not pin subsequent sends to the host. */
> +		connected_addr = vsk->remote_addr;
> +		remote_addr = &connected_addr;
> +		vmci_self = vsk->dgram_peer_is_vmci_self;
>  	} else {
>  		err = -EINVAL;
>  		goto out;
>  	}
>  
> -	if (!transport->dgram_allow(vsk, remote_addr->svm_cid,
> -				    remote_addr->svm_port)) {
> +	err = vsock_dgram_resolve_cid(vsk, remote_addr, &vmci_self);
> +	if (err)
> +		goto out;
> +	if (!vsock_addr_bound(remote_addr)) {
>  		err = -EINVAL;
>  		goto out;
>  	}
>  
> -	err = transport->dgram_enqueue(vsk, remote_addr, msg, len);
> +	transport = vsock_dgram_get_transport(vsk, remote_addr, vmci_self);
> +	if (IS_ERR(transport)) {
> +		err = PTR_ERR(transport);
> +		goto out;
> +	}
> +
> +	err = transport->dgram_enqueue(vsk, transport, remote_addr, msg, len);
> +	module_put(transport->module);

[Severity: Medium]
Can we avoid taking vsock_register_mutex and bouncing module refcounts on
every datagram send and connected receive?

vsock_dgram_sendmsg() and virtio_transport_dgram_source_allow() (on every
incoming datagram for a connected socket, while holding the virtqueue lock
and lock_sock()) now call vsock_dgram_get_transport(), which acquires the
global vsock_register_mutex, does try_module_get()/module_put(), and
performs multiple vhost_vsock_get() lookups per packet, serializing
datagram traffic across all sockets system-wide.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124-vsock-dgram-support-v4-0-3607484e85e8@redhat.com?part=6

  reply	other threads:[~2026-11-24 12:44 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 12:20 [PATCH RFC v4 0/8] vsock: add datagram support to virtio and vhost Sergio Lopez
2026-11-24 12:20 ` [PATCH RFC v4 1/8] vsock: expose the core port allocator to transports Sergio Lopez
2026-11-24 12:20 ` [PATCH RFC v4 2/8] vsock: report datagram receive and send queue state Sergio Lopez
2026-11-24 12:20 ` [PATCH RFC v4 3/8] vsock/virtio: implement the datagram data path Sergio Lopez
2026-11-24 12:20 ` [PATCH RFC v4 4/8] vsock/virtio: negotiate datagram support on the guest Sergio Lopez
2026-11-24 12:20 ` [PATCH RFC v4 5/8] vhost/vsock: negotiate datagram support with guests Sergio Lopez
2026-11-24 12:20 ` [PATCH RFC v4 6/8] vsock: allow datagrams to use directional transports Sergio Lopez
2026-11-24 12:44   ` sashiko-bot [this message]
2026-11-24 12:20 ` [PATCH RFC v4 7/8] vsock: fragment datagrams across receive buffers Sergio Lopez
2026-11-24 12:20 ` [PATCH RFC v4 8/8] vsock: add datagram tests to vsock_test Sergio Lopez
2026-11-24 12:47   ` sashiko-bot
2026-11-24 12:47 ` [PATCH RFC v4 0/8] vsock: add datagram support to virtio and vhost sashiko-bot

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=sashiko-outbox-231167@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=eperezma@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=slp@redhat.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox