Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Igor Skalkin" <igor.skalkin@oss.qualcomm.com>
Cc: virtualization@lists.linux.dev, linux-usb@vger.kernel.org,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Eugenio Perez" <eperezma@redhat.com>
Subject: Re: [RFC PATCH v2 3/7] virtio-usb: add host role (USB Host Controller) support
Date: Tue, 01 Dec 2026 18:09:28 +0000	[thread overview]
Message-ID: <sashiko-outbox-243938@kernel.org> (raw)
In-Reply-To: <20bdd153bf4c8aa41e5364491ba81a2d62dd5a12.1796144390.git.igor.skalkin@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 19 potential
issues to consider:
- [High] virtio-usb: wrong teardown order and missing detach in
  virtio_usb_remove()
- [High] virtio-usb: missing sg_mark_end() in
  virtio_usb_hc_set_urb_sgs()
- [High] usb: virtio-usb: UAF and double list_del/kfree on enqueue error
  during stop
- [High] usb: virtio-usb: missing bounds check on actual_length in URB
  completion
- [High] virtio-usb: unconditional IRQ enable in virtio_usb_data_send()
- [High] virtio-usb: devm memory leak across virtio_usb_hc_vp_init() and
  deinit
- [Medium] virtio-usb: uninitialized work cancel on
  virtio_usb_find_vqs() error path
- [Medium] virtio-usb: missing NULL check on virtio_usb_events_alloc()
  return value
- [Medium] virtio-usb: zero-length sg queued in virtio_usb_hc_enqueue()
- [Medium] virtio-usb: invalid sg_virt() on scatterlist page in
  virtio_usb_hc_enqueue()
- [Medium] virtio-usb: port power cleared on connect in
  virtio_usb_hc_evt_process_one()
- [Medium] usb: virtio-usb: endianness and bitfield layout bug in
  GetPortStatus
- [Medium] usb: virtio-usb: wrong HCD passed via port_vhcd_get() after
  SS port disconnect
- [Medium] usb: virtio-usb: overwriting urb->interval in
  virtio_usb_hc_complete_urb()
- [Medium] virtio-usb: missing NULL check and kick_prepare in
  virtio_usb_events_populate()
- [Medium] virtio-usb: missing virtqueue_disable_cb() in
  virtio_usb_evt_work()
- [Medium] virtio-usb: unfreed unused event buffers in
  virtio_usb_evt_drain_stop_cb()
- [Low] usb: virtio-usb: unused variable iso_status in
  virtio_usb_hc_data_alloc()
- [Low] usb: virtio-usb: spurious C_PORT_ENABLE set on reset in
  virtio_usb_hs_hub_control()
--

commit 80dee502c102d65e1823bcf8b934fde93dd060f3
Author: Aiswarya Cyriac <aiswarya.cyriac@oss.qualcomm.com>

virtio-usb: add host role (USB Host Controller) support

This commit adds the common virtqueue handling code for command, event,
and data queues and implements the virtio-usb USB host controller driver.

> diff --git a/drivers/usb/virtio_usb/controller.c b/drivers/usb/virtio_usb/controller.c
> index 4fcf3d7a6107f..eb343f026a462 100644
> --- a/drivers/usb/virtio_usb/controller.c
> +++ b/drivers/usb/virtio_usb/controller.c
[ ... ]
> @@ -107,13 +133,60 @@ static int virtio_usb_probe(struct virtio_device *vdev)
>  
>  	vusb->nvqs = nvqs;
>  
> +	if (vusb->host_vq_base >= 0)
> +		for (i = 0; i < VIRTIO_USB_VQ_HOST_MAX; i++) {
> +			vusb->vqueues[vusb->host_vq_base + i].name =
> +				host_vqueues[i].name;
> +			vusb->vqueues[vusb->host_vq_base + i].callback =
> +				host_vqueues[i].callback;
> +			vusb->vqueues[vusb->host_vq_base + i].process =
> +				host_vqueues[i].process;
> +			vusb->vqueues[vusb->host_vq_base + i].stop =
> +				host_vqueues[i].stop;
> +		}
> +
>  	rc = virtio_usb_find_vqs(vusb);
> -	if (rc)
> -		goto on_exit;
> +	if (rc) {
> +		dev_err(&vdev->dev, "%s virtio_usb_find_vqs() error(%d)\n",
> +			__func__, rc);
> +		goto on_error;
> +	}
> +
> +	if (vusb->host_role) {
> +		INIT_WORK(&vusb->vq_host_data_rx_work, virtio_usb_hc_rx_work);
> +		INIT_WORK(&vusb->vq_host_evt_work, virtio_usb_hc_evt_work);

[Severity: Medium]
If virtio_usb_find_vqs() fails, jumping to on_error calls
virtio_usb_remove() before vq_host_data_rx_work and vq_host_evt_work have
been initialized with INIT_WORK().

virtio_usb_hc_dataq_stop_cb() then calls cancel_work_sync() on the
zero-initialized vq_host_data_rx_work (triggering WARN_ON(!work->func) in
__flush_work()), and because virtio_usb_find_vqs() does not check the
return value of virtio_find_vqs() before copying vqs[i] into
vusb->vqueues[i].vqueue:

drivers/usb/virtio_usb/controller.c:virtio_usb_find_vqs() {
    ...
	rc = virtio_find_vqs(vusb->vdev, nvqs, vqs, vqs_info, NULL);

	kfree(vqs_info);

	for (i = 0; i < nvqs; i++) {
		vusb->vqueues[i].vqueue = vqs[i];
		spin_lock_init(&vusb->vqueues[i].lock);
	}
    ...
}

the .stop() callbacks and vdev->config->del_vqs() in virtio_usb_remove()
can also dereference freed or ERR_PTR virtqueue pointers left in vqs[].

> +
> +		/* Initialize one HCD pair per host-role VP. */
> +		for (i = 0; i < vusb->nports; i++) {
> +			if (vusb->vports[i].role != VIRTIO_USB_ROLE_HOST)
> +				continue;
> +			rc = virtio_usb_hc_vp_init(vusb, i);
> +			if (rc) {
> +				dev_err(&vdev->dev,
> +					"%s virtio_usb_hc_vp_init() port=%d error(%d)\n",
> +					__func__, i, rc);
> +				goto on_error;
> +			}
> +		}
> +		/* Populate the shared host event queue once, after every
> +		 * VP is initialized.
> +		 */
> +		rc = virtio_usb_hc_event_populate(vusb);
> +		if (rc) {
> +			dev_err(&vdev->dev,
> +				"%s virtio_usb_hc_event_populate() error(%d)\n",
> +				__func__, rc);
> +			goto on_error;
> +		}
> +	}
>  
>  	virtio_device_ready(vdev);

[Severity: Medium]
Should virtio_usb_hc_event_populate() run after virtio_device_ready(), and
check for a NULL return from virtio_usb_events_alloc()?

In virtio_usb_hc_event_populate(), the pointer returned by
virtio_usb_events_alloc() is passed directly to
virtio_usb_events_populate(), which dereferences events[0] without a NULL
check.  virtio_usb_events_populate() also calls virtqueue_notify()
directly without virtqueue_kick_prepare() (skipping the virtio_mb()
barrier and leaving vq->num_added non-zero), and because this runs before
virtio_device_ready(), the notification is either dropped under
CONFIG_VIRTIO_HARDEN_NOTIFICATION or sent before VIRTIO_CONFIG_S_DRIVER_OK
is set.

> -on_exit:
> +	return rc;
> +
> +on_error:
> +	dev_err(&vdev->dev, "%s failed(%d)\n", __func__, rc);
> +	virtio_usb_remove(vdev);
>  	return rc;
>  }
>  
> @@ -128,13 +201,23 @@ static void virtio_usb_remove(struct virtio_device *vdev)
>  	struct virtio_usb *vusb = vdev->priv;
>  	int i;
>  
> -	virtio_reset_device(vdev);
>  	for (i = 0; i < vusb->nvqs; i++)
>  		vusb->vqueues[i].stop(vusb, &vusb->vqueues[i]);
>  
> +	if (vusb->host_role && vusb->vports) {
> +		for (i = 0; i < (int)vusb->nports; i++)
> +			virtio_usb_hc_vp_deinit(vusb, i);
> +	}
> +
> +	virtio_reset_device(vdev);
> +
>  	vdev->config->del_vqs(vdev);
>  }

[Severity: High]
Does stopping the virtqueues before virtio_usb_hc_vp_deinit() and
virtio_reset_device() cause a use-after-free and hang during teardown?

Because virtio_usb_hc_dataq_stop_cb() runs while the device is still live
and the HCDs are still registered, it frees in-flight vurbs and gives back
their URBs while their descriptors remain queued on the active device:

CPU0                                  CPU1
virtio_usb_remove()
  virtio_usb_hc_dataq_stop_cb()
    virtio_usb_dataq_stop_cb()
    cancel_work_sync(&..._rx_work)
    usb_hcd_giveback_urb(...)
    virtio_usb_data_unref(vurb)
      kfree(vurb)
                                      <vring interrupt>
                                        virtio_usb_host_data_notify_cb()
                                          schedule_work(&..._rx_work)
                                      virtio_usb_hc_rx_work()
                                        vurb = virtqueue_get_buf(...)
                                        virtio_usb_hc_complete_urb(vurb)
                                          <-- use-after-free

In addition, any URB queued after virtio_usb_hc_dataq_stop_cb() (for
example, resubmitted from a completion callback or during usb_remove_hcd()
disconnect) is never drained, so usb_kill_urb() inside usb_remove_hcd()
hangs forever waiting for completion.  Finally, virtio_usb_cmdq_stop_cb()
calls virtqueue_detach_unused_buf() before virtio_reset_device(), while
virtio_usb_hc_dataq_stop_cb() relies on virtqueue_detach_unused_buf()
never being called on the data queue, leaking DMA mappings and indirect
descriptor tables at del_vqs().

Removing the HCDs first via virtio_usb_hc_vp_deinit(), then calling
virtio_reset_device(vdev), and then stopping and detaching remaining
buffers from the virtqueues before del_vqs() would avoid these issues.

> diff --git a/drivers/usb/virtio_usb/host.c b/drivers/usb/virtio_usb/host.c
> new file mode 100644
> index 0000000000000..b072c32ac0ee9
> --- /dev/null
> +++ b/drivers/usb/virtio_usb/host.c
[ ... ]
> +	case GetPortStatus: {
> +		u16 *status = (u16 *)buffer;
> +		unsigned long iflags;
> +
> +		memset(status, 0, wLength);
> +
> +		port = virtio_usb_windex_to_port(vhcd_vp, wIndex);
> +		if (port) {
> +			spin_lock_irqsave(&port->lock, iflags);
> +			if (ss_mode) {
> +				status[0] = port->ss.status.value;
> +				status[1] = port->ss.change.value;
> +			} else {
> +				status[0] = port->hs.status.value;
> +				status[1] = port->hs.change.value;
> +			}
> +			spin_unlock_irqrestore(&port->lock, iflags);
> +		}
> +
> +		return 0;
> +	}

[Severity: Medium]
Are port->hs and port->ss bitfields broken on big-endian kernels?

In host.h, struct virtio_usb_port_hs_status, virtio_usb_port_hs_change,
virtio_usb_port_ss_status, and virtio_usb_port_ss_change define USB port
status bits as C bitfields in a union with a native u16 value.  On
big-endian architectures the compiler lays out bitfields from bit 15 down
to bit 0, and GetPortStatus copies those native u16 values into buffer
without cpu_to_le16(), while USB core's hub_ext_port_status() reads buffer
with le16_to_cpu() and tests standard USB_PORT_STAT_* masks.  Also, if
virtio_usb_windex_to_port() returns NULL here, GetPortStatus returns 0
instead of -EPIPE.

[ ... ]
> +	case SetPortFeature:
> +		port = virtio_usb_windex_to_port(vhcd_vp, wIndex);
> +		if (!port)
> +			return -EPIPE;
> +
> +		spin_lock_irqsave(&port->lock, iflags);
> +		switch (wValue) {
> +		case USB_PORT_FEAT_RESET:
> +			if (!port->hs.status.bits.enable) {
> +				port->hs.status.bits.enable = 1;
> +				port->hs.change.bits.enable = 1;
> +			}
> +			port->hs.change.bits.reset = 1;
> +			break;

[Severity: Low]
Should USB_PORT_FEAT_RESET avoid setting port->hs.change.bits.enable = 1?

Per the USB 2.0 specification (section 11.24.2.7.2.2), C_PORT_ENABLE is
only set when a port transitions to disabled due to a port error, not when
reset enables the port.  Because hub_port_reset() only clears C_RESET when
a reset completes, C_PORT_ENABLE stays set and causes
virtio_usb_hub_status_data() to report a spurious status change to
port_event().

[ ... ]
> +	urb->status = virtio_error_to_usb(status);
> +	urb->actual_length = le32_to_cpu(response->actual_length);
> +	if (urb->status == -EINVAL)
> +		dev_err(&urb->dev->dev,
> +			"URB ep%02x status -EINVAL from virtio status %u actual=%d\n",
> +			urb->ep->desc.bEndpointAddress, status,
> +			urb->actual_length);
> +
> +	if (usb_pipeisoc(urb->pipe) || usb_pipeint(urb->pipe))
> +		urb->interval = le32_to_cpu(response->interval);
> +
> +	if (usb_pipeisoc(urb->pipe)) {
> +		iso_urb_status = (void *)response + sizeof(*response);
> +
> +		for (i = 0; i < urb->number_of_packets; i++) {
> +			struct virtio_usb_iso_status *iso_status =
> +				&iso_urb_status[i];
> +			status = le32_to_cpu(iso_status->status);
> +
> +			urb->iso_frame_desc[i].actual_length =
> +				le32_to_cpu(iso_status->actual_length);

[Severity: High]
Can a buggy or untrusted virtio backend return a response->actual_length
larger than urb->transfer_buffer_length (or an iso_status->actual_length
larger than urb->iso_frame_desc[i].length)?  Neither
virtio_usb_hc_complete_urb() nor usb_hcd_giveback_urb() bounds-checks
these lengths, which can cause buffer over-reads in USB class drivers,
usbfs, or usbmon.

[Severity: Medium]
Also, why does virtio_usb_hc_complete_urb() overwrite urb->interval with
le32_to_cpu(response->interval) on interrupt and isochronous completions?
When a periodic URB completes (or is cancelled with response->interval
left at 0) and the completion handler resubmits the same URB via
usb_submit_urb(), usb_submit_urb() will reject the clobbered urb->interval
with -EINVAL.

[ ... ]
> +static struct scatterlist *
> +virtio_usb_hc_set_urb_sgs(struct virtio_usb_hc_priv *priv, gfp_t gfp)
> +{
> +	struct scatterlist *sg = NULL, *sgs = NULL;
> +	struct urb *urb = priv->urb;
> +	unsigned int nsgs, i = 0;
> +	unsigned int buffer_length = urb->transfer_buffer_length;
> +	unsigned int sg_length = 0;
> +
> +	nsgs = urb->num_sgs;
> +
> +	for_each_sg(urb->sg, sg, nsgs, i) {
> +		sg_length += sg->length;
> +	}
> +	if (sg_length > buffer_length) {
> +		sgs = kcalloc(nsgs, sizeof(*sgs), gfp);
> +		if (!sgs)
> +			return NULL;
> +
> +		sg_init_table(sgs, nsgs);
> +
> +		for_each_sg(urb->sg, sg, nsgs, i) {
> +			sg_length = sg->length;
> +
> +			if (sg_length > buffer_length)
> +				sg_length = buffer_length;
> +
> +			sg_set_page(&sgs[i], sg_page(sg), sg_length,
> +				    sg->offset);
> +
> +			buffer_length -= sg_length;
> +			if (!buffer_length)
> +				break;
> +		}
> +		priv->sgs = sgs;
> +		return sgs;
> +	}
> +	return urb->sg;
> +}

[Severity: High]
If buffer_length reaches 0 before the last scatterlist entry (i < nsgs - 1),
does breaking out of the loop without calling sg_mark_end(&sgs[i]) leave
the trimmed scatterlist unterminated at sgs[i]?  virtqueue_add_sgs() will
then walk past sgs[i] into zeroed entries with sg_page(sg) == NULL and
crash during DMA mapping.

> +
> +/**
> + * virtio_usb_hc_data_alloc() - Allocates a virtio usb data for the host
> + * @port: VirtIO USB HC port
> + * @urb: The urb request
> + * @gfp: Kernel flags for memory allocation.
> + *
> + * Return: the allocated data message, or NULL on failure.
> + */
> +static struct virtio_usb_data *
> +virtio_usb_hc_data_alloc(struct virtio_usb_hc_port *port, struct urb *urb,
> +			 gfp_t gfp)
> +{
> +	struct virtio_usb_data *vurb;
> +	struct virtio_usb_hc_priv *priv;
> +	struct virtio_usb_request *request;
> +	struct virtio_usb_response *response;
> +	struct virtio_usb_iso_packet *iso_packet;
> +	struct virtio_usb_iso_status *iso_status;
[ ... ]
> +	if (usb_pipeisoc(urb->pipe)) {
> +		iso_packet = (void *)request + sizeof(*request);
> +		iso_status = (void *)response + sizeof(*response);
> +	}

[Severity: Low]
iso_status is assigned here but never read in virtio_usb_hc_data_alloc(),
which triggers a -Wunused-but-set-variable warning (and build failure with
CONFIG_WERROR).

[ ... ]
> +	if (urb->transfer_buffer) {
> +		sg_init_one(psg_data, urb->transfer_buffer,
> +			    urb->transfer_buffer_length);
> +	} else if (urb->num_sgs > 1) {
> +		psg_data = virtio_usb_hc_set_urb_sgs(priv, mem_flags);
> +		if (!psg_data) {
> +			rc = -ENOMEM;
> +			goto on_exit;
> +		}
> +	} else if (urb->transfer_buffer_length && urb->sg) {
> +		sg_init_one(psg_data, sg_virt(urb->sg),
> +			    urb->transfer_buffer_length);
> +	} else {
> +		psg_data = NULL;
> +	}

[Severity: Medium]
What happens when urb->transfer_buffer_length is 0 but
urb->transfer_buffer is non-NULL (or urb->num_sgs > 1)?  The first two
branches do not check urb->transfer_buffer_length > 0, so
virtio_usb_hc_enqueue() queues a 0-length scatterlist entry onto the
virtqueue (and calls virt_to_page() on urb->transfer_buffer, which hits
BUG_ON(!virt_addr_valid(buf)) if it is ZERO_SIZE_PTR).

[Severity: Medium]
In the third branch (urb->transfer_buffer_length && urb->sg), can calling
sg_init_one(psg_data, sg_virt(urb->sg), ...) crash when urb->sg points to
a highmem page where page_address() is NULL?  Using sg_init_table() and
sg_set_page(psg_data, sg_page(urb->sg), urb->transfer_buffer_length,
urb->sg->offset) as virtio_usb_hc_set_urb_sgs() does would avoid
round-tripping through sg_virt().

> +
> +	spin_lock_irqsave(&port->lock, flags);
> +	rc = usb_hcd_link_urb_to_ep(port_vhcd_get(port), urb);
> +	if (rc) {
> +		spin_unlock_irqrestore(&port->lock, flags);
> +		goto on_exit;
> +	}
> +	urb->hcpriv = vurb;
> +	list_add_tail(&vurb->list, &port->pending_urb_list);
> +	spin_unlock_irqrestore(&port->lock, flags);

[Severity: Medium]
Why do virtio_usb_hc_enqueue() and virtio_usb_hc_dequeue() pass
port_vhcd_get(port) instead of the hcd argument to
usb_hcd_link_urb_to_ep(), usb_hcd_unlink_urb_from_ep(), and
usb_hcd_check_unlink_urb()?

When VIRTIO_USB_EVT_HOST_PORT_DISCONNECTED is processed in
virtio_usb_hc_evt_process_one(), port->speed is immediately reset to
USB_SPEED_UNKNOWN, making port_vhcd_get(port) return vhcd_vp->hs even for
a SuperSpeed device on vhcd_vp->ss.  Any enqueue or dequeue before USB
core finishes disconnecting the SuperSpeed device then checks
HCD_RH_RUNNING on the HS root hub instead of the SS root hub.

> +
> +	if (usb_pipeout(urb->pipe))
> +		out_sgs = psg_data;
> +	else
> +		in_sgs = psg_data;
> +
> +	/*
> +	 * virtio_usb_hc_dequeue() may run as soon as urb->hcpriv is visible,
> +	 * i.e. before the request reaches the data virtqueue. A HOST_CANCEL
> +	 * sent that early would be unmatched on the device side, replied to
> +	 * with success and dropped, and the URB submitted right after would
> +	 * never be cancelled - usb_kill_urb() would wait forever. In that
> +	 * case dequeue only records the cancel and this path sends it once
> +	 * the request is actually queued.
> +	 *
> +	 * Hold a reference across the window: without it the request could
> +	 * be completed and freed between virtio_usb_data_send() and the
> +	 * check below.
> +	 */
> +	virtio_usb_data_ref(vurb);
> +
> +	rc = virtio_usb_data_send(vusb, vurb, out_sgs, in_sgs);
> +	if (rc) {
> +		virtio_usb_data_unref(vurb);
> +		goto on_error_vq;
> +	}
> +
> +	spin_lock_irqsave(&port->lock, flags);
> +	priv->submitted = true;
> +	/* Only cancel if the request has not been completed yet: once it has,
> +	 * the URB is already given back and the device side has nothing left
> +	 * to cancel.
> +	 */
> +	cancel_pending = priv->cancel_pending && !priv->completed;
> +	spin_unlock_irqrestore(&port->lock, flags);
> +
> +	if (cancel_pending)
> +		(void)virtio_usb_hc_cancel_send(vusb, port, vhcd_vp->vp_idx,
> +						priv->tag, GFP_ATOMIC);
> +
> +	virtio_usb_data_unref(vurb);
> +
> +	return 0;
> +
> +on_error_vq:
> +	spin_lock_irqsave(&port->lock, flags);
> +	usb_hcd_unlink_urb_from_ep(port_vhcd_get(port), urb);
> +	urb->hcpriv = NULL;
> +	list_del(&vurb->list);
> +	spin_unlock_irqrestore(&port->lock, flags);
> +
> +on_exit:
> +	kfree(priv->sgs);
> +	virtio_usb_data_unref(vurb);
> +	return rc;
> +}

[Severity: High]
Can virtio_usb_hc_dataq_stop_cb() race with virtio_usb_hc_enqueue() after
vurb is added to port->pending_urb_list, causing a use-after-free and
double free if virtio_usb_data_send() fails?

CPU0                                  CPU1
virtio_usb_hc_enqueue()
  spin_lock_irqsave(&port->lock, ...)
  list_add_tail(&vurb->list,
                &port->pending_urb_list)
  spin_unlock_irqrestore(&port->lock, ...)
                                      virtio_usb_hc_dataq_stop_cb()
                                        list_move_tail(&vurb->list,
                                                       &giveback_list)
                                        list_del(&vurb->list)
                                        usb_hcd_giveback_urb(...)
                                        kfree(priv->sgs)
                                        virtio_usb_data_unref(vurb)
  virtio_usb_data_ref(vurb)
  rc = virtio_usb_data_send(...)
  if (rc)
    virtio_usb_data_unref(vurb)
    goto on_error_vq;
  list_del(&vurb->list)  <-- double list_del
  kfree(priv->sgs)       <-- double kfree
  virtio_usb_data_unref(vurb)  <-- use-after-free

If CPU1 runs before virtio_usb_data_ref(vurb), vurb is already freed when
CPU0 calls virtio_usb_data_ref(vurb).

[ ... ]
> +int virtio_usb_hc_event_populate(struct virtio_usb *vusb)
> +{
> +	/* The host event queue is shared across every host-role VP and
> +	 * lives at the struct virtio_usb level - not owned by any single
> +	 * VP - since a host-role VP may not exist yet (e.g. a dual-role
> +	 * instance where every port currently reports device role).
> +	 */
> +	struct virtio_usb_queue *evt_queue =
> +		&vusb->vqueues[vusb->host_vq_base + VIRTIO_USB_VQ_EVENT_IDX];
> +	struct virtio_usb_event *events;
> +
> +	events = virtio_usb_events_alloc(
> +		vusb, evt_queue, sizeof(struct virtio_usb_host_port_event));
> +
> +	return virtio_usb_events_populate(events);
> +}

[Severity: Medium]
If virtio_usb_events_alloc() returns NULL when devm_kcalloc() fails,
passing events to virtio_usb_events_populate() causes an immediate NULL
pointer dereference on events[0].

[ ... ]
> +int virtio_usb_hc_vp_init(struct virtio_usb *vusb, unsigned int vp_idx)
> +{
> +	struct virtio_usb_hc_vp *vhcd_vp;
> +	unsigned int i;
> +	int rc;
> +
> +	vhcd_vp = devm_kzalloc(&vusb->vdev->dev, sizeof(*vhcd_vp), GFP_KERNEL);
> +	if (!vhcd_vp)
> +		return -ENOMEM;
[ ... ]
> +int virtio_usb_hc_vp_deinit(struct virtio_usb *vusb, unsigned int vp_idx)
> +{
> +	struct virtio_usb_hc_vp *vhcd_vp = vusb->vports[vp_idx].vhc;
> +
> +	if (!vhcd_vp)
> +		return 0;
> +
> +	usb_remove_hcd(vhcd_vp->ss);
> +	usb_put_hcd(vhcd_vp->ss);
> +	usb_remove_hcd(vhcd_vp->hs);
> +	usb_put_hcd(vhcd_vp->hs);
> +
> +	vhcd_vp->ss = NULL;
> +	vhcd_vp->hs = NULL;
> +
> +	vusb->vports[vp_idx].vhc = NULL;
> +	return 0;
> +}

[Severity: High]
Does using devm_kzalloc() for vhcd_vp in virtio_usb_hc_vp_init() and
devm_kasprintf() for the HCD bus name in virtio_usb_add_hcd() leak memory
across virtio_usb_hc_vp_init() / virtio_usb_hc_vp_deinit() cycles?

virtio_usb_hc_vp_deinit() sets vusb->vports[vp_idx].vhc = NULL without
freeing either devm allocation (which stay attached to &vusb->vdev->dev
until the virtio driver unbinds), so every runtime role switch into host
role and back will accumulate another leaked struct virtio_usb_hc_vp and
bus name string.

[ ... ]
> +	spin_lock_irqsave(&port->lock, iflags);
> +	switch (le32_to_cpu(evt->code)) {
> +	case VIRTIO_USB_EVT_HOST_PORT_CONNECTED:
> +		/* Reinitialize the pre-allocated port struct in place */
> +		memset(&port->hs, 0, sizeof(port->hs));
> +		memset(&port->ss, 0, sizeof(port->ss));
> +		port->ss.status.bits.link_state = 0x05; /* RX_DETECT */

[Severity: Medium]
Does zeroing port->hs and port->ss here clear status.bits.power back to 0?

The hub driver powers on root-hub ports at registration time via
SetPortFeature(USB_PORT_FEAT_POWER), and clearing status.bits.power on
VIRTIO_USB_EVT_HOST_PORT_CONNECTED without restoring it makes GetPortStatus
report the connected port as powered off, causing usb_port_is_power_on()
to return false and breaking port resume.

> diff --git a/drivers/usb/virtio_usb/vq_common.c b/drivers/usb/virtio_usb/vq_common.c
> new file mode 100644
> index 0000000000000..c45f86982a2ef
> --- /dev/null
> +++ b/drivers/usb/virtio_usb/vq_common.c
[ ... ]
> +int virtio_usb_data_send(struct virtio_usb *vusb, struct virtio_usb_data *data,
> +			 struct scatterlist *out_sgs,
> +			 struct scatterlist *in_sgs)
> +{
> +	struct virtio_usb_queue *queue = data->msg.queue;
> +	struct scatterlist *psgs[4] = { NULL };
> +	unsigned int nouts = 0, nins = 0;
> +	bool notify = false;
> +	int rc = 0;
> +
> +	psgs[nouts++] = &data->msg.sg_request;
> +	if (out_sgs)
> +		psgs[nouts++] = out_sgs;
> +
> +	psgs[nouts + nins++] = &data->msg.sg_response;
> +	if (in_sgs)
> +		psgs[nouts + nins++] = in_sgs;
> +
> +	spin_lock_irq(&queue->lock);
> +	rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, data,
> +			       GFP_ATOMIC);
> +	if (!rc)
> +		notify = virtqueue_kick_prepare(queue->vqueue);
> +	spin_unlock_irq(&queue->lock);

[Severity: High]
Does spin_unlock_irq(&queue->lock) unconditionally enable local hardware
interrupts when virtio_usb_data_send() is called from
virtio_usb_hc_enqueue() with IRQs disabled?

hcd->driver->urb_enqueue can be called in any context, including from atomic
or IRQ-disabled callers (and virtio_usb_hc_enqueue() itself uses
spin_lock_irqsave()).  Both virtio_usb_data_send() and
virtio_usb_dataq_stop_cb() should use spin_lock_irqsave() /
spin_unlock_irqrestore().

[ ... ]
> +void virtio_usb_evt_work(struct virtio_usb_queue *evtq,
> +			 void (*process_one)(struct virtio_usb_event *event))
> +{
> +	u32 length;
> +	struct virtio_usb_event *event;
> +
> +	spin_lock(&evtq->lock);
> +	do {
> +		while ((event = virtqueue_get_buf(evtq->vqueue, &length))) {
> +			spin_unlock(&evtq->lock);
> +			process_one(event);
> +			spin_lock(&evtq->lock);
> +			virtio_usb_event_send(event);
> +		}
> +		if (unlikely(virtqueue_is_broken(evtq->vqueue)))
> +			break;
> +	} while (!virtqueue_enable_cb(evtq->vqueue));
> +	spin_unlock(&evtq->lock);
> +}

[Severity: Medium]
Should virtio_usb_evt_work() call virtqueue_disable_cb(evtq->vqueue) at the
start of the do-while loop, matching virtio_usb_hc_rx_work() and
virtio_usb_cmd_notify_cb()?

Without virtqueue_disable_cb(), callbacks remain enabled while draining
the queue, forcing a virtio_store_mb() barrier on every
virtqueue_get_buf() call and allowing duplicate vring interrupts while the
worker is already running.  In addition, virtio_usb_event_send() calls
virtqueue_notify() while evtq->lock is held instead of dropping the lock
after virtqueue_kick_prepare().

> +
> +/**
> + * virtio_usb_evt_drain_stop_cb() - Generic event queue stop callback.
> + * @vq: The event virtqueue to stop.
> + * @work: The work item that drains @vq via virtio_usb_evt_work(), to
> + *        be cancelled before draining (may be NULL if the caller has
> + *        already cancelled it, or must defer cancellation itself).
> + *
> + * Do not process events during teardown - whatever state process_one()
> + * would touch may already be partially torn down (probe failure) or
> + * gone (remove path). Just drain the used ring without processing, so
> + * virtio core's del_vqs() finds it empty.
> + *
> + * Context: Any context that permits to sleep (if @work is non-NULL).
> + */
> +void virtio_usb_evt_drain_stop_cb(struct virtio_usb_queue *vq,
> +				  struct work_struct *work)
> +{
> +	unsigned long flags;
> +	u32 length;
> +	void *buf;
> +
> +	if (!vq->vqueue)
> +		return;
> +
> +	if (work)
> +		cancel_work_sync(work);
> +
> +	spin_lock_irqsave(&vq->lock, flags);
> +	virtqueue_disable_cb(vq->vqueue);
> +	while ((buf = virtqueue_get_buf(vq->vqueue, &length)))
> +		;
> +	spin_unlock_irqrestore(&vq->lock, flags);
> +}

[Severity: Medium]
Does draining only the used ring with virtqueue_get_buf() leave all
remaining pre-posted event buffers attached in the available ring?

Because virtio_usb_evt_drain_stop_cb() never calls
virtqueue_detach_unused_buf(vq->vqueue) after device reset,
vdev->config->del_vqs() destroys the event virtqueue without unmapping the
DMA descriptors of the unused event buffers.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796144390.git.igor.skalkin@oss.qualcomm.com?part=3

  reply	other threads:[~2026-12-01 18:09 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-24 16:08 [PATCH 0/8] virtio-usb: add dual-role virtio USB driver Igor Skalkin
2026-09-24 16:09 ` [PATCH 1/8] virtio-usb: add protocol header and skeleton dual-role driver Igor Skalkin
2026-09-25  5:14   ` Greg Kroah-Hartman
2026-09-28 14:19     ` Igor Skalkin
2026-09-25  5:21   ` Greg Kroah-Hartman
2026-09-28 14:14     ` Igor Skalkin
2026-09-24 16:09 ` [PATCH 2/8] virtio-usb: add host role (USB Host Controller) support Igor Skalkin
2026-09-25  5:18   ` Greg Kroah-Hartman
2026-09-28 14:01     ` Igor Skalkin
2026-09-24 16:09 ` [PATCH 3/8] virtio-usb: add device role (USB Device " Igor Skalkin
2026-09-24 16:09 ` [PATCH 4/8] virtio-usb: add OTG role query support Igor Skalkin
2026-09-24 16:09 ` [PATCH 5/8] virtio-usb: add USB On-The-Go role-switching support Igor Skalkin
2026-09-24 16:09 ` [PATCH 6/8] virtio-usb: rework endpoint lifecycle to an async split-phase state machine Igor Skalkin
2026-09-24 16:09 ` [PATCH 7/8] virtio-usb: add SuperSpeed device-role support Igor Skalkin
2026-09-24 16:09 ` [PATCH 8/8] virtio-usb: support a guest UDC name prefix from the bind event Igor Skalkin
2026-09-25  5:17 ` [PATCH 0/8] virtio-usb: add dual-role virtio USB driver Greg Kroah-Hartman
2026-09-28 13:55   ` Igor Skalkin
2026-09-28 14:44     ` Greg Kroah-Hartman
2026-09-28 15:56       ` Igor Skalkin
2026-09-28 16:10         ` Greg Kroah-Hartman
2026-09-29  9:47     ` Michael S. Tsirkin
2026-09-29 16:01       ` Greg Kroah-Hartman
2026-09-29 19:02         ` Vasilii Ianikeev
2026-09-29 19:58         ` Vasilii Ianikeev
2026-12-01 17:35 ` [RFC PATCH v2 0/7] " Igor Skalkin
2026-12-01 17:35   ` [RFC PATCH v2 1/7] uapi: virtio_ids: reserve ID for virtio USB device Igor Skalkin
2026-12-01 17:35   ` [RFC PATCH v2 2/7] virtio-usb: add protocol header and skeleton dual-role driver Igor Skalkin
2026-12-01 17:52     ` sashiko-bot
2026-12-01 17:35   ` [RFC PATCH v2 3/7] virtio-usb: add host role (USB Host Controller) support Igor Skalkin
2026-12-01 18:09     ` sashiko-bot [this message]
2026-12-01 17:35   ` [RFC PATCH v2 4/7] virtio-usb: add device role (USB Device " Igor Skalkin
2026-12-01 18:13     ` sashiko-bot
2026-12-01 17:35   ` [RFC PATCH v2 5/7] virtio-usb: add per-port role query support Igor Skalkin
2026-12-01 17:54     ` sashiko-bot
2026-12-01 17:35   ` [RFC PATCH v2 6/7] virtio-usb: add host/device role switch support Igor Skalkin
2026-12-01 18:00     ` sashiko-bot
2026-12-01 17:35   ` [RFC PATCH v2 7/7] virtio-usb: support a guest UDC name prefix from the bind event Igor Skalkin
2026-12-01 17:50     ` sashiko-bot
2026-12-01 17:49   ` [RFC PATCH v2 0/7] virtio-usb: add dual-role virtio USB driver Michael S. Tsirkin
2026-12-01 18:14   ` 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-243938@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=eperezma@redhat.com \
    --cc=igor.skalkin@oss.qualcomm.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=mst@redhat.com \
    --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