From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796148569; b=Qe50/DbNh7J0KPlEO9D1A5qx+crJ+0Betd5mHqe5lcNa9dLPU0FSzgbvZNPQTPmyIMWk u3Sw7TUszn0AF3DkLRrmj7XLb1PMRcHqPZgsVMRPqc72fngcvs1y53baxduXqaeGyi4D4 m6tLUVXDcwGJUx+qPh76FKI3b0WvP+kFVeM4vDPHmEnjH/hMplEXurR6kgWfjor68iqKZ uBuQfvQgR+Y+YbfrFgubQgI7g5juOoYgWPrNus1a85fUbpDM86lUFfEaIp+tleQdOAH7V moyaG/KvLsi1QmdaJpL3K0KtZ1s6mtgbitRs+wQYXp/+Fk0hCbtBpBEqZkFicJdjCzw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796148569; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=vqj81HPZqIXKadxwxuNwTRiN8BFL08v6zT6N0N8/078=; b=NK/O64m0foA/F9Qsigx3tAt47jdkXKt2+FQou/okwMCF8nKxBPNPHBsb4XCd/NK1jq1j kp6vPinebaL/1kNnYqw8ENzEBKQe1gZA2EIdiI6IlCzwFTOTM/DmNNqUXaxPSckYLlQYq mRjRo1oZusluGhN1XWEI2QLosodot+rLUwV0OwCvVMjVZZbT07NAXJvnjOGkB8ipcbR9N HF4a8YBBUjlK+UsFWVUqIGm9wcSi+6A+FNqGuB6S5LygRxwnGGg0ecgE+0AhmmFJFa7n7 UYa81nSmlQzbTOILVNa6ytEiZig7RiqvLzRkITP9HLayXTb3yk5MWAhH94C9KAPkosg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=VJhNOW5M; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4EC99606A31A; Tue, 1 Dec 2026 18:09:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=VJhNOW5M Received: by smtp.kernel.org (Postfix) with ESMTPSA id A15F81F00893; Tue, 1 Dec 2026 18:09:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796148568; bh=vqj81HPZqIXKadxwxuNwTRiN8BFL08v6zT6N0N8/078=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=VJhNOW5MQxFXu3aO+zYpRHCnd8WVUH8OXwucBGwRtH1I88/X1h3Nn83w4p68N3zR1 u7S626kdfpLUjeZY/TH/g6VFTpp770T5wyBV5+6aS9yyl03DF+GAXGZw9oEKfV4Az6 qyTUTOyuLVHDvMDD4H2ad/W3mch/m5uoABX7yO1LtwRioQcuafLDoIQdbcV48FhRWw TnGnU78faniso2I+Z+EuJivonsoYrZgx36rUGEMEHvHWNCKCrPFMoKYoDCIBx2qDJ+ bTa/q56Cdl8vXL9Ly1hfBsLTpbI0PXrvcLujh6nM7uoXyCLWxnrGf4YWopstxQ1ShG UsBaIpbKbX6aA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [RFC PATCH v2 3/7] virtio-usb: add host role (USB Host Controller) support Reply-To: sashiko-reviews@lists.linux.dev To: "Igor Skalkin" Cc: virtualization@lists.linux.dev, linux-usb@vger.kernel.org, "Michael S. Tsirkin" , "Eugenio Perez" In-Reply-To: <20bdd153bf4c8aa41e5364491ba81a2d62dd5a12.1796144390.git.igor.skalkin@oss.qualcomm.com> References: <20260924160907.145405-1-igor.skalkin@oss.qualcomm.com> <20bdd153bf4c8aa41e5364491ba81a2d62dd5a12.1796144390.git.igor.skalkin@oss.qualcomm.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 18:09:28 +0000 X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 *v= dev) > =20 > vusb->nvqs =3D nvqs; > =20 > + if (vusb->host_vq_base >=3D 0) > + for (i =3D 0; i < VIRTIO_USB_VQ_HOST_MAX; i++) { > + vusb->vqueues[vusb->host_vq_base + i].name =3D > + host_vqueues[i].name; > + vusb->vqueues[vusb->host_vq_base + i].callback =3D > + host_vqueues[i].callback; > + vusb->vqueues[vusb->host_vq_base + i].process =3D > + host_vqueues[i].process; > + vusb->vqueues[vusb->host_vq_base + i].stop =3D > + host_vqueues[i].stop; > + } > + > rc =3D 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 =3D virtio_find_vqs(vusb->vdev, nvqs, vqs, vqs_info, NULL); kfree(vqs_info); for (i =3D 0; i < nvqs; i++) { vusb->vqueues[i].vqueue =3D 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 =3D 0; i < vusb->nports; i++) { > + if (vusb->vports[i].role !=3D VIRTIO_USB_ROLE_HOST) > + continue; > + rc =3D virtio_usb_hc_vp_init(vusb, i); > + if (rc) { > + dev_err(&vdev->dev, > + "%s virtio_usb_hc_vp_init() port=3D%d error(%d)\n", > + __func__, i, rc); > + goto on_error; > + } > + } > + /* Populate the shared host event queue once, after every > + * VP is initialized. > + */ > + rc =3D 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; > + } > + } > =20 > 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; > } > =20 > @@ -128,13 +201,23 @@ static void virtio_usb_remove(struct virtio_device = *vdev) > struct virtio_usb *vusb =3D vdev->priv; > int i; > =20 > - virtio_reset_device(vdev); > for (i =3D 0; i < vusb->nvqs; i++) > vusb->vqueues[i].stop(vusb, &vusb->vqueues[i]); > =20 > + if (vusb->host_role && vusb->vports) { > + for (i =3D 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) virtio_usb_host_data_notify_cb() schedule_work(&..._rx_work) virtio_usb_hc_rx_work() vurb =3D 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 =3D (u16 *)buffer; > + unsigned long iflags; > + > + memset(status, 0, wLength); > + > + port =3D virtio_usb_windex_to_port(vhcd_vp, wIndex); > + if (port) { > + spin_lock_irqsave(&port->lock, iflags); > + if (ss_mode) { > + status[0] =3D port->ss.status.value; > + status[1] =3D port->ss.change.value; > + } else { > + status[0] =3D port->hs.status.value; > + status[1] =3D 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 =3D 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 =3D 1; > + port->hs.change.bits.enable =3D 1; > + } > + port->hs.change.bits.reset =3D 1; > + break; [Severity: Low] Should USB_PORT_FEAT_RESET avoid setting port->hs.change.bits.enable =3D 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 =3D virtio_error_to_usb(status); > + urb->actual_length =3D le32_to_cpu(response->actual_length); > + if (urb->status =3D=3D -EINVAL) > + dev_err(&urb->dev->dev, > + "URB ep%02x status -EINVAL from virtio status %u actual=3D%d\n", > + urb->ep->desc.bEndpointAddress, status, > + urb->actual_length); > + > + if (usb_pipeisoc(urb->pipe) || usb_pipeint(urb->pipe)) > + urb->interval =3D le32_to_cpu(response->interval); > + > + if (usb_pipeisoc(urb->pipe)) { > + iso_urb_status =3D (void *)response + sizeof(*response); > + > + for (i =3D 0; i < urb->number_of_packets; i++) { > + struct virtio_usb_iso_status *iso_status =3D > + &iso_urb_status[i]; > + status =3D le32_to_cpu(iso_status->status); > + > + urb->iso_frame_desc[i].actual_length =3D > + 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 =3D NULL, *sgs =3D NULL; > + struct urb *urb =3D priv->urb; > + unsigned int nsgs, i =3D 0; > + unsigned int buffer_length =3D urb->transfer_buffer_length; > + unsigned int sg_length =3D 0; > + > + nsgs =3D urb->num_sgs; > + > + for_each_sg(urb->sg, sg, nsgs, i) { > + sg_length +=3D sg->length; > + } > + if (sg_length > buffer_length) { > + sgs =3D kcalloc(nsgs, sizeof(*sgs), gfp); > + if (!sgs) > + return NULL; > + > + sg_init_table(sgs, nsgs); > + > + for_each_sg(urb->sg, sg, nsgs, i) { > + sg_length =3D sg->length; > + > + if (sg_length > buffer_length) > + sg_length =3D buffer_length; > + > + sg_set_page(&sgs[i], sg_page(sg), sg_length, > + sg->offset); > + > + buffer_length -=3D sg_length; > + if (!buffer_length) > + break; > + } > + priv->sgs =3D 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) =3D=3D 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 *ur= b, > + 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 =3D (void *)request + sizeof(*request); > + iso_status =3D (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 =3D virtio_usb_hc_set_urb_sgs(priv, mem_flags); > + if (!psg_data) { > + rc =3D -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 =3D 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 =3D usb_hcd_link_urb_to_ep(port_vhcd_get(port), urb); > + if (rc) { > + spin_unlock_irqrestore(&port->lock, flags); > + goto on_exit; > + } > + urb->hcpriv =3D 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 =3D psg_data; > + else > + in_sgs =3D 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 =3D 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 =3D 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 =3D 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 =3D 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 =3D 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 =3D > + &vusb->vqueues[vusb->host_vq_base + VIRTIO_USB_VQ_EVENT_IDX]; > + struct virtio_usb_event *events; > + > + events =3D 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 =3D 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 =3D 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 =3D NULL; > + vhcd_vp->hs =3D NULL; > + > + vusb->vports[vp_idx].vhc =3D 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 =3D 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 =3D 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 =3D data->msg.queue; > + struct scatterlist *psgs[4] =3D { NULL }; > + unsigned int nouts =3D 0, nins =3D 0; > + bool notify =3D false; > + int rc =3D 0; > + > + psgs[nouts++] =3D &data->msg.sg_request; > + if (out_sgs) > + psgs[nouts++] =3D out_sgs; > + > + psgs[nouts + nins++] =3D &data->msg.sg_response; > + if (in_sgs) > + psgs[nouts + nins++] =3D in_sgs; > + > + spin_lock_irq(&queue->lock); > + rc =3D virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, data, > + GFP_ATOMIC); > + if (!rc) > + notify =3D 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 =3D 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 =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/cover.1796144390.gi= t.igor.skalkin@oss.qualcomm.com?part=3D3