On Fri, 20 Nov 2026 at 14:45, Thomas Zimmermann <[email protected]> wrote:
>
> Remove the limit on the number of URBs in flight. Allocate URBs as
> needed. Keep once-allocated URBs in a list for reuse at later frames.
> Also remove some bogus debug output from the affected code.
>
> The existing synchronization in atomic_begin() guarantees that udl
> does not send a frame over the USB before the previous frame has been
> transferred. This avoids overloading the USB. If the USB is saturated,
> page flips delay and the compositor reduces the update frequency.
>
> The allocated URBs remain in the cache until the driver is being removed
> from the device. In a later update, a regular worker or a shrinker could
> free the URB cache when needed.
>
> Signed-off-by: Thomas Zimmermann <[email protected]>
> ---
> drivers/gpu/drm/udl/udl_drv.h | 1 -
> drivers/gpu/drm/udl/udl_main.c | 123 ++++++++++++---------------------
> 2 files changed, 44 insertions(+), 80 deletions(-)
>
> diff --git a/drivers/gpu/drm/udl/udl_drv.h b/drivers/gpu/drm/udl/udl_drv.h
> index 420166bdc4bc..68bcf0222764 100644
> --- a/drivers/gpu/drm/udl/udl_drv.h
> +++ b/drivers/gpu/drm/udl/udl_drv.h
> @@ -45,7 +45,6 @@ struct urb_list {
> spinlock_t lock;
> wait_queue_head_t sleep;
> unsigned int in_flight;
> - int count;
> };
>
> struct udl_device {
> diff --git a/drivers/gpu/drm/udl/udl_main.c b/drivers/gpu/drm/udl/udl_main.c
> index d0195bd4213d..adddcf4b3451 100644
> --- a/drivers/gpu/drm/udl/udl_main.c
> +++ b/drivers/gpu/drm/udl/udl_main.c
> @@ -25,13 +25,10 @@
> #define NR_USB_REQUEST_CHANNEL 0x12
>
> #define MAX_TRANSFER (PAGE_SIZE*16 - BULK_SIZE)
> -#define WRITES_IN_FLIGHT (20)
> #define MAX_VENDOR_DESCRIPTOR_SIZE 256
>
> #define UDL_SKU_PIXEL_LIMIT_DEFAULT 2080000
>
> -static struct urb *udl_get_urb_locked(struct udl_device *udl, long timeout);
> -
> /*
> * Try to make sense of whatever we parse. Therefore return @end on
> * errors, but don't fail hard.
> @@ -216,25 +213,41 @@ static struct urb *udl_alloc_bulk_urb(struct udl_device
> *udl, size_t size)
> return ERR_PTR(ret);
> }
>
> +static struct urb *__udl_get_cached_urb_locked(struct udl_device *udl)
> +{
> + struct urb_node *unode;
> +
> + assert_spin_locked(&udl->urbs.lock);
> +
> + if (list_empty(&udl->urbs.list))
> + return NULL;
> +
> + unode = list_first_entry(&udl->urbs.list, struct urb_node, entry);
> + list_del_init(&unode->entry);
> +
> + return unode->urb;
> +}
> +
> static void udl_free_urb_list(struct udl_device *udl)
> {
> + struct drm_device *drm = &udl->drm;
> struct urb *urb;
>
> - DRM_DEBUG("Waiting for completes and freeing all render urbs\n");
> + drm_dbg(drm, "Waiting for completes and freeing all render URBs\n");
>
> - /* keep waiting and freeing, until we've got 'em all */
> - while (udl->urbs.count) {
> - spin_lock_irq(&udl->urbs.lock);
> - urb = udl_get_urb_locked(udl, MAX_SCHEDULE_TIMEOUT);
> - udl->urbs.count--;
> - spin_unlock_irq(&udl->urbs.lock);
> - if (WARN_ON(!urb))
> - break;
> - /* Free each separately allocated piece */
> - udl_free_bulk_urb(urb);
> + /* keep waiting until we've got 'em all */
> + scoped_guard(spinlock_irq, &udl->urbs.lock) {
> + wait_event_lock_irq(udl->urbs.sleep, !udl->urbs.in_flight,
> udl->urbs.lock);
> +
> + urb = __udl_get_cached_urb_locked(udl);
> }
>
> - wake_up_all(&udl->urbs.sleep);
> + while (urb) {
> + udl_free_bulk_urb(urb);
> +
> + scoped_guard(spinlock_irq, &udl->urbs.lock)
> + urb = __udl_get_cached_urb_locked(udl);
> + }
> }
>
> static void udl_urb_list_release(void *data)
> @@ -244,71 +257,16 @@ static void udl_urb_list_release(void *data)
> udl_free_urb_list(udl);
> }
>
> -static int udl_alloc_urb_list(struct udl_device *udl, int count, size_t size)
> +static int udl_init_urb_list(struct udl_device *udl)
> {
> struct drm_device *drm = &udl->drm;
> - size_t wanted_size = count * size;
> - int ret;
>
> spin_lock_init(&udl->urbs.lock);
> INIT_LIST_HEAD(&udl->urbs.list);
> init_waitqueue_head(&udl->urbs.sleep);
> - udl->urbs.count = 0;
> udl->urbs.in_flight = 0;
>
> -retry:
> - while (udl->urbs.count * size < wanted_size) {
> - struct urb *urb;
> - struct urb_node *unode;
> -
> - urb = udl_alloc_bulk_urb(udl, size);
> - if (IS_ERR(urb)) {
> - if (PTR_ERR(urb) == -ENOBUFS && size > PAGE_SIZE) {
> - size /= 2;
> - udl_free_urb_list(udl);
> - goto retry;
> - }
> - break;
> - }
> - unode = urb->context;
> -
> - list_add_tail(&unode->entry, &udl->urbs.list);
> -
> - udl->urbs.count++;
> - }
> -
> - if (!udl->urbs.count)
> - return -ENOMEM;
> -
> - ret = devm_add_action_or_reset(drm->dev, udl_urb_list_release, udl);
> - if (ret < 0)
> - return ret;
> -
> - DRM_DEBUG("allocated %d %d byte urbs\n", udl->urbs.count, (int) size);
> -
> - return udl->urbs.count;
> -}
> -
> -static struct urb *udl_get_urb_locked(struct udl_device *udl, long timeout)
> -{
> - struct urb_node *unode;
> -
> - assert_spin_locked(&udl->urbs.lock);
> -
> - /* Wait for an in-flight buffer to complete and get re-queued */
> - if (!wait_event_lock_irq_timeout(udl->urbs.sleep,
> - !udl->urbs.count ||
> - !list_empty(&udl->urbs.list),
> - udl->urbs.lock, timeout))
> - return NULL;
> -
> - if (!udl->urbs.count)
> - return NULL;
> -
> - unode = list_first_entry(&udl->urbs.list, struct urb_node, entry);
> - list_del_init(&unode->entry);
> -
> - return unode->urb;
> + return devm_add_action_or_reset(drm->dev, udl_urb_list_release, udl);
> }
>
> struct urb *udl_get_urb(struct udl_device *udl)
> @@ -321,9 +279,18 @@ struct urb *udl_get_urb(struct udl_device *udl)
> return NULL;
> ++udl->urbs.in_flight;
>
> - urb = udl_get_urb_locked(udl, HZ * 2);
> - if (!urb)
> - --udl->urbs.in_flight;
> + urb = __udl_get_cached_urb_locked(udl);
> + }
> +
> + if (!urb) {
> + urb = udl_alloc_bulk_urb(udl, MAX_TRANSFER);
> + if (IS_ERR(urb)) {
> + scoped_guard(spinlock_irqsave, &udl->urbs.lock) {
> + if (udl->urbs.in_flight)
> + --udl->urbs.in_flight;
> + }
> + return NULL;
> + }
On a usb contention or stalled device, this might allocate memory unboundedly
if the udl_sync_pending_urbs keeps timing out.
The number of URBs could be limited based on the size of the worst-case
full frame update. The entire urb cache can be cleared on a modeset,
so that maximum memory usage scales with the frame size.
> }
>
> return urb;
> @@ -395,8 +362,6 @@ int udl_init(struct udl_device *udl)
> int ret = -ENOMEM;
> struct device *dma_dev;
>
> - DRM_DEBUG("\n");
> -
> dma_dev = usb_intf_get_dma_device(to_usb_interface(dev->dev));
> if (dma_dev) {
> drm_dev_set_dma_dev(dev, dma_dev);
> @@ -419,8 +384,8 @@ int udl_init(struct udl_device *udl)
> if (udl_select_std_channel(udl))
> DRM_ERROR("Selecting channel failed\n");
>
> - ret = udl_alloc_urb_list(udl, WRITES_IN_FLIGHT, MAX_TRANSFER);
> - if (ret < 0)
> + ret = udl_init_urb_list(udl);
> + if (ret)
> return ret;
>
> ret = udl_modeset_init(udl);
> --
> 2.56.0
>
>
Best regards,
Jie Feng