Linux virtualization list
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sasha Levin" <sashal@kernel.org>
Cc: virtualization@lists.linux.dev,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Eugenio Perez" <eperezma@redhat.com>
Subject: Re: [PATCH AUTOSEL 6.18-5.10] virtio_console: fix control queue race during restore
Date: Mon, 19 Oct 2026 15:11:18 +0000	[thread overview]
Message-ID: <sashiko-outbox-177637@kernel.org> (raw)
In-Reply-To: <20261019135441.1420968-390-sashal@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] virtio_console: dropped host control replies in virtcons_restore()
--

commit 896c2558dbc3180e4d84cf033ee9bf699250ed69
Author: Sungho Bae <baver.bae@lge.com>

virtio_console: fix control queue race during restore

This commit defers populating the control receive queue (portdev->c_ivq) in
virtcons_restore() until after iterating over portdev->ports to prevent
concurrent list modification from control_work_handler().

Link: https://patch.msgid.link/20260603183757.21587-4-baver.bae@gmail.com

> diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c
> index 088182e54debd..6dd0cf78bf839 100644
> --- a/drivers/char/virtio_console.c
> +++ b/drivers/char/virtio_console.c
> @@ -2136,9 +2136,6 @@ static int virtcons_restore(struct virtio_device *vdev)
>  
>  	virtio_device_ready(portdev->vdev);
>  
> -	if (use_multiport(portdev))
> -		fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
> -
>  	list_for_each_entry(port, &portdev->ports, list) {
>  		port->in_vq = portdev->in_vqs[port->id];
>  		port->out_vq = portdev->out_vqs[port->id];
> @@ -2155,6 +2152,18 @@ static int virtcons_restore(struct virtio_device *vdev)
>  		if (port->guest_connected)
>  			send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
>  	}
> +
> +	/*
> +	 * Populate the control receive queue only after the list iteration
> +	 * is complete. If we fill this queue before iterating, the host could
> +	 * immediately deliver a VIRTIO_CONSOLE_PORT_REMOVE message.
> +	 * This would trigger the control workqueue, which modifies the
> +	 * portdev->ports list concurrently with the unprotected loop above,
> +	 * leading to a Use-After-Free and list corruption.
> +	 */
> +	if (use_multiport(portdev))
> +		fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
> +
>  	return 0;
>  }
>  #endif

[Severity: High]
Won't deferring fill_queue(portdev->c_ivq, &portdev->c_ivq_lock) until after
the loop cause the host's control replies to be dropped during restore?

During freeze, virtcons_freeze() clears port->host_connected for every port
and destroys all virtqueues:

drivers/char/virtio_console.c:virtcons_freeze() {
	...
	list_for_each_entry(port, &portdev->ports, list) {
		virtqueue_disable_cb(port->in_vq);
		virtqueue_disable_cb(port->out_vq);
		/*
		 * We'll ask the host later if the new invocation has
		 * the port opened or closed.
		 */
		port->host_connected = false;
		remove_port_data(port);
	}
	remove_vqs(portdev);
	...
}

In virtcons_restore(), the list_for_each_entry() loop queries the host for
each port's open/close status via send_control_msg() -> __send_control_msg():

drivers/char/virtio_console.c:virtcons_restore() {
	...
	list_for_each_entry(port, &portdev->ports, list) {
		...
		/* Get port open/close status on the host */
		send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
		...
	}
}

Because __send_control_msg() kicks portdev->c_ovq and busy-waits
synchronously until the host consumes the VIRTIO_CONSOLE_PORT_READY request:

drivers/char/virtio_console.c:__send_control_msg() {
	...
	if (virtqueue_add_outbuf(vq, sg, 1, &portdev->cpkt, GFP_ATOMIC) == 0) {
		virtqueue_kick(vq);\
		while (!virtqueue_get_buf(vq, &len)
			&& !virtqueue_is_broken(vq))
			cpu_relax();
	}
	...
}

the host processes VIRTIO_CONSOLE_PORT_READY and immediately attempts to send
back VIRTIO_CONSOLE_PORT_OPEN (along with VIRTIO_CONSOLE_CONSOLE_PORT and
VIRTIO_CONSOLE_PORT_NAME) on portdev->c_ivq before __send_control_msg()
returns.

If fill_queue(portdev->c_ivq, &portdev->c_ivq_lock) has not been called yet,
portdev->c_ivq has zero receive buffers when the host replies, causing those
VIRTIO_CONSOLE_PORT_OPEN replies to be dropped and leaving
port->host_connected permanently stuck at false after restore (which causes
port_fops_write() to block or fail with -EAGAIN, port_fops_read() to return
EOF, and port_fops_poll() to report EPOLLHUP).

Would it work to keep fill_queue(portdev->c_ivq, &portdev->c_ivq_lock) before
the loop, but disable the c_ivq callback or defer scheduling control_work
until the loop finishes so the host has receive buffers available while list
traversal is still protected from concurrent control_work_handler() runs?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261019135441.1420968-390-sashal@kernel.org?part=1

  reply	other threads:[~2026-10-19 15:11 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261019135441.1420968-1-sashal@kernel.org>
2026-10-19 13:47 ` [PATCH AUTOSEL 6.18-5.15] virtio_ring: fix infinite loop in virtnet_poll_cleantx when device is broken Sasha Levin
2026-10-19 14:25   ` sashiko-bot
2026-10-19 13:47 ` [PATCH AUTOSEL 6.18-6.6] vdpa_sim: hold iommu_lock across dma_unmap passthrough transition Sasha Levin
2026-10-19 14:29   ` sashiko-bot
2026-10-19 13:47 ` [PATCH AUTOSEL 6.18-6.1] drm/virtio: reclaim pending vbufs before tearing down vqs Sasha Levin
2026-10-19 14:51   ` sashiko-bot
2026-10-19 13:49 ` [PATCH AUTOSEL 6.18-6.1] vdpa: alibaba: add missing MODULE_DEVICE_TABLE() Sasha Levin
2026-10-19 14:49   ` sashiko-bot
2026-10-19 13:49 ` [PATCH AUTOSEL 6.18-6.12] vdpa: octeon_ep: " Sasha Levin
2026-10-19 14:50   ` sashiko-bot
2026-10-19 13:50 ` [PATCH AUTOSEL 6.18-5.10] virtio_console: take a kref in find_port_by_vq() to fix port UAF Sasha Levin
2026-10-19 15:00   ` sashiko-bot
2026-10-19 13:50 ` [PATCH AUTOSEL 6.18-5.10] virtio_balloon: warn on failed buffer add in tell_host() Sasha Levin
2026-10-19 14:59   ` sashiko-bot
2026-10-19 13:51 ` [PATCH AUTOSEL 6.18-5.10] virtio_console: fix control queue race during restore Sasha Levin
2026-10-19 15:11   ` sashiko-bot [this message]
2026-10-19 13:51 ` [PATCH AUTOSEL 6.18] drm/virtio: abort virtqueue wait on device removal to avoid hung task Sasha Levin
2026-10-19 15:24   ` sashiko-bot
2026-10-19 13:52 ` [PATCH AUTOSEL 6.18-6.12] vdpa/mlx5: roll back MR update after VQ setup failure Sasha Levin
2026-10-19 15:17   ` 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-177637@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=eperezma@redhat.com \
    --cc=mst@redhat.com \
    --cc=sashal@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --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