drm/udl: Rework URB allocation
Subsystems (3)
Thomas Zimmermann
maintainercover drm/udl: Rework URB allocation
Udl caches once-allocated URBs for later use. By default, it has a fixed number of 20 URBs in flight at most. Depending on the USB, this can either consume too much bandwidth or keep free bandwidth unused.
Replace the fixed-size cache with dynamic allocation of the URBs required. Adapt page-flip rate according to the actually available bandwidth on the USB.
Patches 1 to 6 clean up the code, fix a few minor issue and provide a clean interface for allocating and freeing URBs.
Patch 7 switches the URB cache to managed release. As before, the cache collects all outstanding URBs as part of the USB device removal.
Patches 8 to 11 implement dynamic allocation of URBs. The URB cache tracks the number of URBs in flight and allocates new ones of the cache has been exhausted. The udl driver now sends out as many URBs as necessary for the current frame without limitting itself to 20 URBs at a time. To avoid overloading the USB, udl waits in atomic_begin() for the previous frame's URBs to complete. This adapts page-flip rate to the bandwidth available on the USB.
Patch 11 contains a minor improvement of the cache locality.
Tested with superttuxkart and vkcube on weston.
Note that display output might not happen faster in any way. This series only addresses one of many bottlenecks (e.g., message packing, software rendering).
Thomas Zimmermann (11):
drm/udl: Release URB in udl_put_urb()
drm/udl: Do not use URB completion handler for error cleanup drm/udl: Store per-URB allocation length in struct urb_node drm/udl: Allocate bulk URBs in helper functions drm/udl: Free bulk URBs in helper functions drm/udl: Rework the error handling in udl_submit_urb() drm/udl: Managed cleanup of URB cache
drm/udl: Track number of URBs in flight
drm/udl: Synchronize pending URBs on each frame drm/udl: Allocate URBs on demand
drm/udl: Implement URB cache as LIFO stack
drivers/gpu/drm/udl/udl_drv.c | 2 - drivers/gpu/drm/udl/udl_drv.h | 8 +- drivers/gpu/drm/udl/udl_main.c | 280 +++++++++++++++-------------- drivers/gpu/drm/udl/udl_modeset.c | 20 ++- drivers/gpu/drm/udl/udl_transfer.c | 2 +- 5 files changed, 168 insertions(+), 144 deletions(-)
base-commit: 7d502136b31169f6b5575f7f89c3da60cdf7891d
Signature · 1 line
2.56.0
Thomas Zimmermann
maintainerPATCH 1/11 drm/udl: Release URB in udl_put_urb()
Move URB cleanup from udl_urb_completion() to udl_put_urb(). udl_put_urb() is the inverse of udl_get_urb(). The new helper will allow for further fixes.
--- drivers/gpu/drm/udl/udl_drv.h | 1 + drivers/gpu/drm/udl/udl_main.c | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 12 deletions(-)
drivers/gpu/drm/udl/udl_drv.h +1 -0
@@ -73,6 +73,7 @@ int udl_modeset_init(struct udl_device *udl);struct drm_connector *udl_connector_init (struct drm_device *dev);struct urb *udl_get_urb (struct udl_device *udl); +void udl_put_urb (struct urb *urb);int udl_submit_urb (struct udl_device *udl,struct urb *urb,size_t len);void udl_sync_pending_urbs (struct udl_device *udl);
drivers/gpu/drm/udl/udl_main.c +17 -12
@@ -149,10 +149,6 @@ int udl_select_std_channel(struct udl_device *udl)void udl_urb_completion (struct urb *urb) { -struct urb_node *unode = urb->context; -struct udl_device *udl = unode->dev; -unsigned long flags; -/* sync/async unlink faults aren't errors */ if (urb->status) { if (!(urb->status == -ENOENT ||
@@ -164,14 +160,7 @@ void udl_urb_completion(struct urb *urb) } } - urb->transfer_buffer_length = udl->urbs.size;/* reset to actual */ - -spin_lock_irqsave (&udl->urbs.lock, flags); -list_add_tail (&unode->entry, &udl->urbs.list); - udl->urbs.available++; -spin_unlock_irqrestore (&udl->urbs.lock, flags); - -wake_up (&udl->urbs.sleep); +udl_put_urb (urb); }static void udl_free_urb_list (struct udl_device *udl)
@@ -295,6 +284,22 @@ struct urb *udl_get_urb(struct udl_device *udl)return urb; } +void udl_put_urb (struct urb *urb) +{ +struct urb_node *unode = urb->context; +struct udl_device *udl = unode->dev; +unsigned long flags; + + urb->transfer_buffer_length = udl->urbs.size;/* reset to actual */ + +spin_lock_irqsave (&udl->urbs.lock, flags); +list_add_tail (&unode->entry, &udl->urbs.list); + udl->urbs.available++; +spin_unlock_irqrestore (&udl->urbs.lock, flags); + +wake_up (&udl->urbs.sleep); +} +int udl_submit_urb (struct udl_device *udl,struct urb *urb,size_t len) {int ret;
Signature · 1 line
2.56.0
Re: [PATCH 01/11] drm/udl: Release URB in udl_put_urb()
Quoted 62 lines · Thomas Zimmermann
Move URB cleanup from
udl_urb_completion()toudl_put_urb().udl_put_urb()is the inverse ofudl_get_urb(). The new helper will allow for further fixes.Signed-off-by Thomas Zimmermann <[email protected]>--- drivers/gpu/drm/udl/udl_drv.h | 1 + drivers/gpu/drm/udl/udl_main.c | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 12 deletions(-)drivers/gpu/drm/udl/udl_drv.h +1
-0@@ -73,6 +73,7 @@ int udl_modeset_init(struct udl_device *udl);struct drm_connector *udl_connector_init (struct drm_device *dev);struct urb *udl_get_urb (struct udl_device *udl); +void udl_put_urb (struct urb *urb);int udl_submit_urb (struct udl_device *udl,struct urb *urb,size_t len);void udl_sync_pending_urbs (struct udl_device *udl);drivers/gpu/drm/udl/udl_main.c +5
-12@@ -149,10 +149,6 @@ int udl_select_std_channel(struct udl_device *udl)void udl_urb_completion (struct urb *urb) { -struct urb_node *unode = urb->context; -struct udl_device *udl = unode->dev; -unsigned long flags; -/* sync/async unlink faults aren't errors */ if (urb->status) { if (!(urb->status == -ENOENT ||@@ -164,14 +160,7 @@ void udl_urb_completion(struct urb *urb) } } - urb->transfer_buffer_length = udl->urbs.size;/* reset to actual */ - -spin_lock_irqsave (&udl->urbs.lock, flags); -list_add_tail (&unode->entry, &udl->urbs.list); - udl->urbs.available++; -spin_unlock_irqrestore (&udl->urbs.lock, flags); - -wake_up (&udl->urbs.sleep); +udl_put_urb (urb); }static void udl_free_urb_list (struct udl_device *udl)@@ -295,6 +284,22 @@ struct urb *udl_get_urb(struct udl_device *udl)return urb; } +void udl_put_urb (struct urb *urb) +{ +struct urb_node *unode = urb->context; +struct udl_device *udl = unode->dev;
drivers/gpu/drm/udl/udl_main.c struct urb *udl_get_urb(struct udl_device *udl) · line 300
Quoted 10 lines
drivers/gpu/drm/udl/udl_main.c +10
-0+unsigned long flags; + + urb->transfer_buffer_length = udl->urbs.size;/* reset to actual */ + +spin_lock_irqsave (&udl->urbs.lock, flags); +list_add_tail (&unode->entry, &udl->urbs.list); + udl->urbs.available++; +spin_unlock_irqrestore (&udl->urbs.lock, flags); + +wake_up (&udl->urbs.sleep);
Using a completion might be cleaner than using a counter and a waitqueue? P
Quoted 9 lines
drivers/gpu/drm/udl/udl_main.c +2
-1+} +int udl_submit_urb (struct udl_device *udl,struct urb *urb,size_t len) {int ret; --2.56.0
Best regards,
Jie Feng
Thomas Zimmermann
maintainerPATCH 2/11 drm/udl: Do not use URB completion handler for error cleanup
In reply to Thomas ZimmermannCall udl_put_urb() to release an URB back into the URB cache. Replace udl_urb_completion() in error-handling code.
The URB completion handler runs in interrupt context after the USB subsystem has processed an URB. Do not call it from udl's error-handling logic.
--- drivers/gpu/drm/udl/udl_drv.h | 1 - drivers/gpu/drm/udl/udl_main.c | 4 ++-- drivers/gpu/drm/udl/udl_modeset.c | 2 +- drivers/gpu/drm/udl/udl_transfer.c | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-)
drivers/gpu/drm/udl/udl_drv.h +0 -1
@@ -77,7 +77,6 @@ void udl_put_urb(struct urb *urb);int udl_submit_urb (struct udl_device *udl,struct urb *urb,size_t len);void udl_sync_pending_urbs (struct udl_device *udl); -void udl_urb_completion (struct urb *urb);int udl_init (struct udl_device *udl);
drivers/gpu/drm/udl/udl_main.c +2 -2
@@ -147,7 +147,7 @@ int udl_select_std_channel(struct udl_device *udl)return ret <0 ? ret :0 ; } -void udl_urb_completion (struct urb *urb) +static void udl_urb_completion (struct urb *urb) {/* sync/async unlink faults aren't errors */ if (urb->status) {
@@ -312,7 +312,7 @@ int udl_submit_urb(struct udl_device *udl, struct urb *urb, size_t len) ret =usb_submit_urb (urb,GFP_ATOMIC ); error:if (ret) { -udl_urb_completion (urb);/* because no one else will */ +udl_put_urb (urb);DRM_ERROR ("usb_submit_urb error %x\n" , ret); }return ret;
drivers/gpu/drm/udl/udl_modeset.c +1 -1
@@ -243,7 +243,7 @@ static int udl_handle_damage(struct drm_framebuffer *fb, len = cmd - (char *)urb->transfer_buffer; ret =udl_submit_urb (udl, urb, len); }else { -udl_urb_completion (urb); +udl_put_urb (urb); }return 0 ;
drivers/gpu/drm/udl/udl_transfer.c +1 -1
@@ -183,7 +183,7 @@ int udl_render_hline(struct udl_device *udl, int log_bpp, struct urb **urb_ptr,if (WARN_ON (!(log_bpp ==1 || log_bpp ==2 ))) {/* need to finish URB at error from this function */ -udl_urb_completion (urb); +udl_put_urb (urb);return -EINVAL ; }
Signature · 1 line
2.56.0
Thomas Zimmermann
maintainerPATCH 3/11 drm/udl: Store per-URB allocation length in struct urb_node
In reply to Thomas ZimmermannStore the length of an URB's data buffer in the URB's instance of struct urb_node. Use the value for free'ing the buffer. Remove the obsolete size field from struct urb_list.
With the URB cache in place, all URBs have the same buffer size. At some point we want URBs to have an individual length for their allocated buffer. Hence, the buffer length needs to be stored separately.
--- drivers/gpu/drm/udl/udl_drv.h | 2 +- drivers/gpu/drm/udl/udl_main.c | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-)
drivers/gpu/drm/udl/udl_drv.h +1 -1
@@ -37,6 +37,7 @@ struct urb_node {struct list_head entry;struct udl_device *dev;struct urb *urb; +size_t alloc_size; };struct urb_list {
@@ -45,7 +46,6 @@ struct urb_list {wait_queue_head_t sleep;int available;int count; -size_t size; };struct udl_device {
drivers/gpu/drm/udl/udl_main.c +7 -6
@@ -180,7 +180,7 @@ static void udl_free_urb_list(struct udl_device *udl)break ; unode = urb->context;/* Free each separately allocated piece */ -usb_free_coherent (urb->dev, udl->urbs.size, +usb_free_coherent (urb->dev, unode->alloc_size, urb->transfer_buffer, urb->transfer_dma);usb_free_urb (urb);kfree (unode);
@@ -204,8 +204,6 @@ static int udl_alloc_urb_list(struct udl_device *udl, int count, size_t size) udl->urbs.available =0 ; retry: - udl->urbs.size = size; -while (udl->urbs.count * size < wanted_size) { unode = kzalloc_obj(struct urb_node);if (!unode)
@@ -218,8 +216,9 @@ static int udl_alloc_urb_list(struct udl_device *udl, int count, size_t size)break ; } unode->urb = urb; + unode->alloc_size = size; - buf =usb_alloc_coherent (udev, size,GFP_KERNEL , + buf =usb_alloc_coherent (udev, unode->alloc_size,GFP_KERNEL , &urb->transfer_dma);if (!buf) {kfree (unode);
@@ -290,7 +289,7 @@ void udl_put_urb(struct urb *urb)struct udl_device *udl = unode->dev;unsigned long flags; - urb->transfer_buffer_length = udl->urbs.size;/* reset to actual */ + urb->transfer_buffer_length = unode->alloc_size;/* reset to actual */ spin_lock_irqsave (&udl->urbs.lock, flags);list_add_tail (&unode->entry, &udl->urbs.list);
@@ -302,9 +301,11 @@ void udl_put_urb(struct urb *urb)int udl_submit_urb (struct udl_device *udl,struct urb *urb,size_t len) { +struct drm_device *drm = &udl->drm; +struct urb_node *unode = urb->context;int ret; -if (WARN_ON (len > udl->urbs.size)) { +if (drm_WARN_ON_ONCE (drm, len > unode->alloc_size)) { ret = -EINVAL ; goto error; }
Signature · 1 line
2.56.0
Thomas Zimmermann
maintainerPATCH 4/11 drm/udl: Allocate bulk URBs in helper functions
In reply to Thomas ZimmermannMove the code for allocating an URB into a separate helper function. Use the helper for initializing the URB cache.
--- drivers/gpu/drm/udl/udl_main.c | 74 +++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 27 deletions(-)
drivers/gpu/drm/udl/udl_main.c +47 -27
@@ -163,6 +163,47 @@ static void udl_urb_completion(struct urb *urb)udl_put_urb (urb); } +static struct urb *udl_alloc_bulk_urb (struct udl_device *udl,size_t size) +{ +struct usb_device *udev =udl_to_usb_device (udl); +struct urb_node *unode; +struct urb *urb; +char *buf; +int ret; + + urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!urb) +return ERR_PTR (-ENOMEM ); + + unode = kzalloc_obj(struct urb_node); +if (!unode) { + ret = -ENOMEM ; + goto err_usb_free_urb; + } + unode->dev = udl; + unode->urb = urb; + + buf =usb_alloc_coherent (udev, size,GFP_KERNEL , &urb->transfer_dma); +if (!buf) { + ret = -ENOBUFS ; + goto err_kfree; + } + unode->alloc_size = size; + +/* urb->transfer_buffer_length set to actual before submit */ +usb_fill_bulk_urb (urb, udev,usb_sndbulkpipe (udev,1 ), buf, unode->alloc_size, + udl_urb_completion, unode); + urb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; + +return urb; + +err_kfree: +kfree (unode); +err_usb_free_urb: +usb_free_urb (urb); +return ERR_PTR (ret); +} +static void udl_free_urb_list (struct udl_device *udl) {struct urb_node *unode;
@@ -191,11 +232,7 @@ static void udl_free_urb_list(struct udl_device *udl)static int udl_alloc_urb_list (struct udl_device *udl,int count,size_t size) { -struct urb *urb; -struct urb_node *unode; -char *buf;size_t wanted_size = count * size; -struct usb_device *udev =udl_to_usb_device (udl);spin_lock_init (&udl->urbs.lock);INIT_LIST_HEAD (&udl->urbs.list);
@@ -205,36 +242,19 @@ static int udl_alloc_urb_list(struct udl_device *udl, int count, size_t size) retry:while (udl->urbs.count * size < wanted_size) { - unode = kzalloc_obj(struct urb_node); -if (!unode) -break ; - unode->dev = udl; +struct urb *urb; +struct urb_node *unode; - urb =usb_alloc_urb (0 ,GFP_KERNEL ); -if (!urb) { -kfree (unode); -break ; - } - unode->urb = urb; - unode->alloc_size = size; - - buf =usb_alloc_coherent (udev, unode->alloc_size,GFP_KERNEL , - &urb->transfer_dma); -if (!buf) { -kfree (unode); -usb_free_urb (urb); -if (size >PAGE_SIZE ) { + 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 ; } - -/* urb->transfer_buffer_length set to actual before submit */ -usb_fill_bulk_urb (urb, udev,usb_sndbulkpipe (udev,1 ), - buf, size, udl_urb_completion, unode); - urb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; + unode = urb->context;list_add_tail (&unode->entry, &udl->urbs.list);
Signature · 1 line
2.56.0
Re: [PATCH 04/11] drm/udl: Allocate bulk URBs in helper functions
Quoted 28 lines · Thomas Zimmermann
Move the code for allocating an URB into a separate helper function. Use the helper for initializing the URB cache.
Signed-off-by Thomas Zimmermann <[email protected]>--- drivers/gpu/drm/udl/udl_main.c | 74 +++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 27 deletions(-)drivers/gpu/drm/udl/udl_main.c +11
-0@@ -163,6 +163,47 @@ static void udl_urb_completion(struct urb *urb)udl_put_urb (urb); } +static struct urb *udl_alloc_bulk_urb (struct udl_device *udl,size_t size) +{ +struct usb_device *udev =udl_to_usb_device (udl); +struct urb_node *unode; +struct urb *urb; +char *buf; +int ret; + + urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!urb) +return ERR_PTR (-ENOMEM );
drivers/gpu/drm/udl/udl_main.c static void udl_urb_completion(struct urb *urb) · line 186
Quoted 10 lines
drivers/gpu/drm/udl/udl_main.c +10
-0+ + unode = kzalloc_obj(struct urb_node); +if (!unode) { + ret = -ENOMEM ; + goto err_usb_free_urb; + } + unode->dev = udl; + unode->urb = urb; + + buf =usb_alloc_coherent (udev, size,GFP_KERNEL , &urb->transfer_dma);
usb_alloc_coherent might map uncached memory on platforms such as ARM. This might be slow for moving large amounts of frame data. Would using kmalloc/usb_alloc_noncoherent be better?
Quoted 79 lines
drivers/gpu/drm/udl/udl_main.c +26
-28+if (!buf) { + ret = -ENOBUFS ; + goto err_kfree; + } + unode->alloc_size = size; + +/* urb->transfer_buffer_length set to actual before submit */ +usb_fill_bulk_urb (urb, udev,usb_sndbulkpipe (udev,1 ), buf, unode->alloc_size, + udl_urb_completion, unode); + urb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; + +return urb; + +err_kfree: +kfree (unode); +err_usb_free_urb: +usb_free_urb (urb); +return ERR_PTR (ret); +} +static void udl_free_urb_list (struct udl_device *udl) {struct urb_node *unode;@@ -191,11 +232,7 @@ static void udl_free_urb_list(struct udl_device *udl)static int udl_alloc_urb_list (struct udl_device *udl,int count,size_t size) { -struct urb *urb; -struct urb_node *unode; -char *buf;size_t wanted_size = count * size; -struct usb_device *udev =udl_to_usb_device (udl);spin_lock_init (&udl->urbs.lock);INIT_LIST_HEAD (&udl->urbs.list);@@ -205,36 +242,19 @@ static int udl_alloc_urb_list(struct udl_device *udl, int count, size_t size) retry:while (udl->urbs.count * size < wanted_size) { - unode = kzalloc_obj(struct urb_node); -if (!unode) -break ; - unode->dev = udl; +struct urb *urb; +struct urb_node *unode; - urb =usb_alloc_urb (0 ,GFP_KERNEL ); -if (!urb) { -kfree (unode); -break ; - } - unode->urb = urb; - unode->alloc_size = size; - - buf =usb_alloc_coherent (udev, unode->alloc_size,GFP_KERNEL , - &urb->transfer_dma); -if (!buf) { -kfree (unode); -usb_free_urb (urb); -if (size >PAGE_SIZE ) { + 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 ; } - -/* urb->transfer_buffer_length set to actual before submit */ -usb_fill_bulk_urb (urb, udev,usb_sndbulkpipe (udev,1 ), - buf, size, udl_urb_completion, unode); - urb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; + unode = urb->context;list_add_tail (&unode->entry, &udl->urbs.list); --
Quoted 3 lines
2.56.0
Best regards,
Jie Feng
Thomas Zimmermann
maintainerRe: [PATCH 04/11] drm/udl: Allocate bulk URBs in helper functions
Hi
Am 19.11.26 um 17:38 schrieb Jie Feng:
Quoted 33 lines · Thomas Zimmermann
Quoted 32 lines
Move the code for allocating an URB into a separate helper function. Use the helper for initializing the URB cache.
Signed-off-by Thomas Zimmermann <[email protected]>--- drivers/gpu/drm/udl/udl_main.c | 74 +++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 27 deletions(-)drivers/gpu/drm/udl/udl_main.c +16
-0@@ -163,6 +163,47 @@ static void udl_urb_completion(struct urb *urb)udl_put_urb (urb); } +static struct urb *udl_alloc_bulk_urb (struct udl_device *udl,size_t size) +{ +struct usb_device *udev =udl_to_usb_device (udl); +struct urb_node *unode; +struct urb *urb; +char *buf; +int ret; + + urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!urb) +return ERR_PTR (-ENOMEM ); + + unode = kzalloc_obj(struct urb_node); +if (!unode) { + ret = -ENOMEM ; + goto err_usb_free_urb;
Quoted 8 lines
drivers/gpu/drm/udl/udl_main.c static void udl_urb_completion(struct urb *urb) · line 186
Quoted 5 lines
drivers/gpu/drm/udl/udl_main.c +5
-0+ } + unode->dev = udl; + unode->urb = urb; + + buf =usb_alloc_coherent (udev, size,GFP_KERNEL , &urb->transfer_dma);
usb_alloc_coherentmight map uncached memory on platforms such as ARM. This might be slow for moving large amounts of frame data. Would using kmalloc/usb_alloc_noncoherent be better?
It's worth a try, but i cannot say for sure. The advantage of _coherent is that is makes DMA easy. And we don't read from these buffers from the CPU, so caching them might not help much for performance. In any case, it's a separate patch series.
Best regards
Thomas
Quoted 84 lines
Quoted 79 lines
drivers/gpu/drm/udl/udl_main.c +26
-28+if (!buf) { + ret = -ENOBUFS ; + goto err_kfree; + } + unode->alloc_size = size; + +/* urb->transfer_buffer_length set to actual before submit */ +usb_fill_bulk_urb (urb, udev,usb_sndbulkpipe (udev,1 ), buf, unode->alloc_size, + udl_urb_completion, unode); + urb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; + +return urb; + +err_kfree: +kfree (unode); +err_usb_free_urb: +usb_free_urb (urb); +return ERR_PTR (ret); +} +static void udl_free_urb_list (struct udl_device *udl) {struct urb_node *unode;@@ -191,11 +232,7 @@ static void udl_free_urb_list(struct udl_device *udl)static int udl_alloc_urb_list (struct udl_device *udl,int count,size_t size) { -struct urb *urb; -struct urb_node *unode; -char *buf;size_t wanted_size = count * size; -struct usb_device *udev =udl_to_usb_device (udl);spin_lock_init (&udl->urbs.lock);INIT_LIST_HEAD (&udl->urbs.list);@@ -205,36 +242,19 @@ static int udl_alloc_urb_list(struct udl_device *udl, int count, size_t size) retry:while (udl->urbs.count * size < wanted_size) { - unode = kzalloc_obj(struct urb_node); -if (!unode) -break ; - unode->dev = udl; +struct urb *urb; +struct urb_node *unode; - urb =usb_alloc_urb (0 ,GFP_KERNEL ); -if (!urb) { -kfree (unode); -break ; - } - unode->urb = urb; - unode->alloc_size = size; - - buf =usb_alloc_coherent (udev, unode->alloc_size,GFP_KERNEL , - &urb->transfer_dma); -if (!buf) { -kfree (unode); -usb_free_urb (urb); -if (size >PAGE_SIZE ) { + 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 ; } - -/* urb->transfer_buffer_length set to actual before submit */ -usb_fill_bulk_urb (urb, udev,usb_sndbulkpipe (udev,1 ), - buf, size, udl_urb_completion, unode); - urb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; + unode = urb->context;list_add_tail (&unode->entry, &udl->urbs.list); --Quoted 3 lines
2.56.0
Best regards,
Jie Feng
Signature · 6 lines
-- Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)
Re: [PATCH 04/11] drm/udl: Allocate bulk URBs in helper functions
Quoted 46 lines · Thomas Zimmermann
Hi
Am 19.11.26 um 17:38 schrieb Jie Feng:
Quoted 33 lines · Thomas Zimmermann
Quoted 32 lines
Move the code for allocating an URB into a separate helper function. Use the helper for initializing the URB cache.
Signed-off-by Thomas Zimmermann <[email protected]>--- drivers/gpu/drm/udl/udl_main.c | 74 +++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 27 deletions(-)drivers/gpu/drm/udl/udl_main.c +16
-0@@ -163,6 +163,47 @@ static void udl_urb_completion(struct urb *urb)udl_put_urb (urb); } +static struct urb *udl_alloc_bulk_urb (struct udl_device *udl,size_t size) +{ +struct usb_device *udev =udl_to_usb_device (udl); +struct urb_node *unode; +struct urb *urb; +char *buf; +int ret; + + urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!urb) +return ERR_PTR (-ENOMEM ); + + unode = kzalloc_obj(struct urb_node); +if (!unode) { + ret = -ENOMEM ; + goto err_usb_free_urb;Quoted 8 lines
drivers/gpu/drm/udl/udl_main.c static void udl_urb_completion(struct urb *urb) · line 186
Quoted 5 lines
drivers/gpu/drm/udl/udl_main.c +5
-0+ } + unode->dev = udl; + unode->urb = urb; + + buf =usb_alloc_coherent (udev, size,GFP_KERNEL , &urb->transfer_dma);
usb_alloc_coherentmight map uncached memory on platforms such as ARM. This might be slow for moving large amounts of frame data. Would using kmalloc/usb_alloc_noncoherent be better?
Quoted 4 lines
It's worth a try, but i cannot say for sure. The advantage of
_coherentis that is makes DMA easy. And we don't read from these buffers from the CPU, so caching them might not help much for performance. In any case, it's a separate patch series.
If I'm not wrong, uncached memory also means that the CPU sends each memory write as a separate transaction, so it does not wait a while to flush the entire cache line
at once unlike normal/write-combined memory. Therefore it still makes sense to use cacheable memory for the write path.
I agree that it should be a separate patch series.
Most of the uses of these USB display adapters should be on x86, where there isn't
a performance impact, but given that ARM laptops has been rising in popularity, I think looking into this might be a good idea.
Quoted 88 lines
Best regards
ThomasQuoted 76 lines
Quoted 76 lines
drivers/gpu/drm/udl/udl_main.c +26
-27+if (!buf) { + ret = -ENOBUFS ; + goto err_kfree; + } + unode->alloc_size = size; + +/* urb->transfer_buffer_length set to actual before submit */ +usb_fill_bulk_urb (urb, udev,usb_sndbulkpipe (udev,1 ), buf, unode->alloc_size, + udl_urb_completion, unode); + urb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; + +return urb; + +err_kfree: +kfree (unode); +err_usb_free_urb: +usb_free_urb (urb); +return ERR_PTR (ret); +} +static void udl_free_urb_list (struct udl_device *udl) {struct urb_node *unode;@@ -191,11 +232,7 @@ static void udl_free_urb_list(struct udl_device *udl)static int udl_alloc_urb_list (struct udl_device *udl,int count,size_t size) { -struct urb *urb; -struct urb_node *unode; -char *buf;size_t wanted_size = count * size; -struct usb_device *udev =udl_to_usb_device (udl);spin_lock_init (&udl->urbs.lock);INIT_LIST_HEAD (&udl->urbs.list);@@ -205,36 +242,19 @@ static int udl_alloc_urb_list(struct udl_device *udl, int count, size_t size) retry:while (udl->urbs.count * size < wanted_size) { - unode = kzalloc_obj(struct urb_node); -if (!unode) -break ; - unode->dev = udl; +struct urb *urb; +struct urb_node *unode; - urb =usb_alloc_urb (0 ,GFP_KERNEL ); -if (!urb) { -kfree (unode); -break ; - } - unode->urb = urb; - unode->alloc_size = size; - - buf =usb_alloc_coherent (udev, unode->alloc_size,GFP_KERNEL , - &urb->transfer_dma); -if (!buf) { -kfree (unode); -usb_free_urb (urb); -if (size >PAGE_SIZE ) { + 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 ; } - -/* urb->transfer_buffer_length set to actual before submit */ -usb_fill_bulk_urb (urb, udev,usb_sndbulkpipe (udev,1 ), - buf, size, udl_urb_completion, unode); - urb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; + unode = urb->context;Quoted 8 lines
Quoted 6 lines
drivers/gpu/drm/udl/udl_main.c +0
-1list_add_tail (&unode->entry, &udl->urbs.list); --2.56.0
Best regards,
Jie Feng
Quoted 9 lines
--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)
Best regards,
Jie Feng
Thomas Zimmermann
maintainerPATCH 5/11 drm/udl: Free bulk URBs in helper functions
In reply to Thomas ZimmermannMove the code for freeing an URB into a separate helper function. Use the helper for cleaning up the URB cache.
--- drivers/gpu/drm/udl/udl_main.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
drivers/gpu/drm/udl/udl_main.c +10 -6
@@ -147,6 +147,15 @@ int udl_select_std_channel(struct udl_device *udl)return ret <0 ? ret :0 ; } +static void udl_free_bulk_urb (struct urb *urb) +{ +struct urb_node *unode = urb->context; + +usb_free_coherent (urb->dev, unode->alloc_size, urb->transfer_buffer, urb->transfer_dma); +kfree (unode); +usb_free_urb (urb); +} +static void udl_urb_completion (struct urb *urb) {/* sync/async unlink faults aren't errors */
@@ -206,7 +215,6 @@ static struct urb *udl_alloc_bulk_urb(struct udl_device *udl, size_t size)static void udl_free_urb_list (struct udl_device *udl) { -struct urb_node *unode;struct urb *urb;DRM_DEBUG ("Waiting for completes and freeing all render urbs\n" );
@@ -219,12 +227,8 @@ static void udl_free_urb_list(struct udl_device *udl)spin_unlock_irq (&udl->urbs.lock);if (WARN_ON (!urb))break ; - unode = urb->context;/* Free each separately allocated piece */ -usb_free_coherent (urb->dev, unode->alloc_size, - urb->transfer_buffer, urb->transfer_dma); -usb_free_urb (urb); -kfree (unode); +udl_free_bulk_urb (urb); }wake_up_all (&udl->urbs.sleep);
Signature · 1 line
2.56.0
Thomas Zimmermann
maintainerPATCH 6/11 drm/udl: Rework the error handling in udl_submit_urb()
In reply to Thomas ZimmermannRefactor the error handling in udl_submit_urb(). Replace DRM_ERROR() macro with drm_err_ratelimited(). No functional changes.
--- drivers/gpu/drm/udl/udl_main.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/gpu/drm/udl/udl_main.c +10 -4
@@ -331,15 +331,21 @@ int udl_submit_urb(struct udl_device *udl, struct urb *urb, size_t len)if (drm_WARN_ON_ONCE (drm, len > unode->alloc_size)) { ret = -EINVAL ; - goto error; + goto err_udl_put_urb; } + urb->transfer_buffer_length = len;/* set to actual payload len */ + ret =usb_submit_urb (urb,GFP_ATOMIC ); - error:if (ret) { -udl_put_urb (urb); -DRM_ERROR ("usb_submit_urb error %x\n" , ret); +drm_err_ratelimited (drm,"usb_submit_urb() failed with error %d\n" , ret); + goto err_udl_put_urb; } + +return 0 ; + + err_udl_put_urb: +udl_put_urb (urb);return ret; }
Signature · 1 line
2.56.0
Thomas Zimmermann
maintainerPATCH 7/11 drm/udl: Managed cleanup of URB cache
In reply to Thomas ZimmermannClean up the URB cache as part of the USB device cleanup with managed helpers. Remove udl_drop_usb() and the clean-up code from udl_init(). All happens automatically.
--- drivers/gpu/drm/udl/udl_drv.c | 2 -- drivers/gpu/drm/udl/udl_drv.h | 1 - drivers/gpu/drm/udl/udl_main.c | 43 +++++++++++++++++----------------- 3 files changed, 22 insertions(+), 24 deletions(-)
drivers/gpu/drm/udl/udl_drv.c +0 -2
@@ -110,10 +110,8 @@ static int udl_usb_probe(struct usb_interface *interface,static void udl_usb_disconnect (struct usb_interface *interface) {struct drm_device *dev =usb_get_intfdata (interface); -struct udl_device *udl =to_udl (dev);drm_dev_unplug (dev); -udl_drop_usb (udl); }/*
drivers/gpu/drm/udl/udl_drv.h +0 -1
@@ -84,7 +84,6 @@ int udl_render_hline(struct udl_device *udl, int log_bpp, struct urb **urb_ptr, const char *front, char **urb_buf_ptr, u32 byte_offset, u32 device_byte_offset, u32 byte_width); -int udl_drop_usb (struct udl_device *udl);int udl_select_std_channel (struct udl_device *udl);#endif
drivers/gpu/drm/udl/udl_main.c +22 -21
77 lines not on this page · raw message
Signature · 1 line
2.56.0
Thomas Zimmermann
maintainerPATCH 8/11 drm/udl: Track number of URBs in flight
In reply to Thomas ZimmermannAdd a counter to track the number of URBs that are in flight. On suspend, wait for all URBs to complete before suspending the device. Replace the existing available counter.
Waiting on suspend used to be done by comparing the number of URBs available to the overall number of allocated URBs in the cache. Track the in-flight URBs instead to be flexible with the number of URBs available.
Udl increases the in-flight counter when it takes an URB from the URB cache and decreases the counter when the URB's completion handler puts it back into the cache. There are overflow and underflow checks to avoid getting out of sync.
--- drivers/gpu/drm/udl/udl_drv.h | 2 +- drivers/gpu/drm/udl/udl_main.c | 58 +++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 26 deletions(-)
drivers/gpu/drm/udl/udl_drv.h +1 -1
@@ -44,7 +44,7 @@ struct urb_list {struct list_head list;spinlock_t lock;wait_queue_head_t sleep; -int available; +unsigned int in_flight;int count; };
drivers/gpu/drm/udl/udl_main.c +33 -25
117 lines not on this page · raw message
Signature · 1 line
2.56.0
Thomas Zimmermann
maintainerPATCH 9/11 drm/udl: Synchronize pending URBs on each frame
In reply to Thomas ZimmermannWait for in-flight URBs from the previous frame before sending out the current frame. Throttles frame rate to USB limits.
PC display usually runs with a refresh rate of 60 Hz or more. Sending out URBs for each frame at this rate easily overloads the USB. This results in display lag up to several seconds. Udl currently avoids this by limiting the number of URBs to 20 at a time. This prevents overloading the USB, but also prevents udl from using all the bandwidth available.
Solving this requires more flexibility in the number of in-flight URBs. The exact value depends on the framebuffer configuration and the available bandwidth on the USB.
Before removing the limit on the URB count, add another means of synchronizing the frame rate to the USB. Do this in atomic_begin, so that the USB has the maximum time between sending two frames.
--- drivers/gpu/drm/udl/udl_modeset.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
drivers/gpu/drm/udl/udl_modeset.c +18 -0
@@ -329,6 +329,23 @@ static const struct drm_plane_funcs udl_primary_plane_funcs = {* CRTC */ +static void udl_crtc_helper_atomic_begin (struct drm_crtc *crtc,struct drm_atomic_commit *state) +{ +struct drm_device *dev = crtc->dev; +struct udl_device *udl =to_udl (dev); +int idx; + +if (!drm_dev_enter (dev, &idx)) +return ; + +/* +* It is acceptable to briefly block in this call, but +* having it timeout frequently indicates a problem. +*/ +udl_sync_pending_urbs (udl); +drm_dev_exit (idx); +} +static void udl_crtc_helper_atomic_enable (struct drm_crtc *crtc,struct drm_atomic_commit *state) {struct drm_device *dev = crtc->dev;
@@ -395,6 +412,7 @@ static void udl_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atostatic const struct drm_crtc_helper_funcs udl_crtc_helper_funcs = { .atomic_check = drm_crtc_helper_atomic_check, + .atomic_begin = udl_crtc_helper_atomic_begin, .atomic_enable = udl_crtc_helper_atomic_enable, .atomic_disable = udl_crtc_helper_atomic_disable, };
Signature · 1 line
2.56.0
Re: [PATCH 09/11] drm/udl: Synchronize pending URBs on each frame
Quoted 36 lines · Thomas Zimmermann
Wait for in-flight URBs from the previous frame before sending out the current frame. Throttles frame rate to USB limits.
PC display usually runs with a refresh rate of 60 Hz or more. Sending out URBs for each frame at this rate easily overloads the USB. This results in display lag up to several seconds. Udl currently avoids this by limiting the number of URBs to 20 at a time. This prevents overloading the USB, but also prevents udl from using all the bandwidth available.
Solving this requires more flexibility in the number of in-flight URBs. The exact value depends on the framebuffer configuration and the available bandwidth on the USB.
Before removing the limit on the URB count, add another means of synchronizing the frame rate to the USB. Do this in
atomic_begin, so that the USB has the maximum time between sending two frames.Signed-off-by Thomas Zimmermann <[email protected]>--- drivers/gpu/drm/udl/udl_modeset.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)drivers/gpu/drm/udl/udl_modeset.c +4
-0@@ -329,6 +329,23 @@ static const struct drm_plane_funcs udl_primary_plane_funcs = {* CRTC */ +static void udl_crtc_helper_atomic_begin (struct drm_crtc *crtc,struct drm_atomic_commit *state) +{ +struct drm_device *dev = crtc->dev; +struct udl_device *udl =to_udl (dev);
drivers/gpu/drm/udl/udl_modeset.c static const struct drm_plane_funcs udl_primary_plane_funcs = { · line 345
Quoted 10 lines
drivers/gpu/drm/udl/udl_modeset.c +10
-0+int idx; + +if (!drm_dev_enter (dev, &idx)) +return ; + +/* +* It is acceptable to briefly block in this call, but +* having it timeout frequently indicates a problem. +*/ +udl_sync_pending_urbs (udl);
Using usb anchors might work better instead of the completion I suggested in the other message. usb_wait_anchor_empty_timeout() could be used.
Quoted 15 lines
drivers/gpu/drm/udl/udl_modeset.c +4
-1+drm_dev_exit (idx); +} +static void udl_crtc_helper_atomic_enable (struct drm_crtc *crtc,struct drm_atomic_commit *state) {struct drm_device *dev = crtc->dev;@@ -395,6 +412,7 @@ static void udl_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atostatic const struct drm_crtc_helper_funcs udl_crtc_helper_funcs = { .atomic_check = drm_crtc_helper_atomic_check, + .atomic_begin = udl_crtc_helper_atomic_begin, .atomic_enable = udl_crtc_helper_atomic_enable, .atomic_disable = udl_crtc_helper_atomic_disable, }; --
Quoted 3 lines
2.56.0
Best regards,
Jie Feng
Thomas Zimmermann
maintainerRe: [PATCH 09/11] drm/udl: Synchronize pending URBs on each frame
Hi
Am 20.11.26 um 07:50 schrieb Jie Feng:
Quoted 32 lines · Thomas Zimmermann
Quoted 31 lines
Wait for in-flight URBs from the previous frame before sending out the current frame. Throttles frame rate to USB limits.
PC display usually runs with a refresh rate of 60 Hz or more. Sending out URBs for each frame at this rate easily overloads the USB. This results in display lag up to several seconds. Udl currently avoids this by limiting the number of URBs to 20 at a time. This prevents overloading the USB, but also prevents udl from using all the bandwidth available.
Solving this requires more flexibility in the number of in-flight URBs. The exact value depends on the framebuffer configuration and the available bandwidth on the USB.
Before removing the limit on the URB count, add another means of synchronizing the frame rate to the USB. Do this in
atomic_begin, so that the USB has the maximum time between sending two frames.Signed-off-by Thomas Zimmermann <[email protected]>--- drivers/gpu/drm/udl/udl_modeset.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)drivers/gpu/drm/udl/udl_modeset.c +0
-0@@ -329,6 +329,23 @@ static const struct drm_plane_funcs udl_primary_plane_funcs = {* CRTC */
Quoted 16 lines
Quoted 4 lines
drivers/gpu/drm/udl/udl_modeset.c +4
-0+static void udl_crtc_helper_atomic_begin (struct drm_crtc *crtc,struct drm_atomic_commit *state) +{ +struct drm_device *dev = crtc->dev; +struct udl_device *udl =to_udl (dev);drivers/gpu/drm/udl/udl_modeset.c static const struct drm_plane_funcs udl_primary_plane_funcs = { · line 345
Quoted 10 lines
drivers/gpu/drm/udl/udl_modeset.c +10
-0+int idx; + +if (!drm_dev_enter (dev, &idx)) +return ; + +/* +* It is acceptable to briefly block in this call, but +* having it timeout frequently indicates a problem. +*/ +udl_sync_pending_urbs (udl);Using usb anchors might work better instead of the completion I suggested in the other message.
usb_wait_anchor_empty_timeout()could be used.
Nice, I wasn't aware of this helper. I briefly looked over it and it seems useful. It would also mean that there are two lists: one for the cache and one for the anchor. That might be a good thing in the longer term. I'll see if I can use anchors to the benefit of this code.
Best regards
Thomas
Quoted 21 lines
Quoted 16 lines
drivers/gpu/drm/udl/udl_modeset.c +4
-1+drm_dev_exit (idx); +} +static void udl_crtc_helper_atomic_enable (struct drm_crtc *crtc,struct drm_atomic_commit *state) {struct drm_device *dev = crtc->dev;@@ -395,6 +412,7 @@ static void udl_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atostatic const struct drm_crtc_helper_funcs udl_crtc_helper_funcs = { .atomic_check = drm_crtc_helper_atomic_check, + .atomic_begin = udl_crtc_helper_atomic_begin, .atomic_enable = udl_crtc_helper_atomic_enable, .atomic_disable = udl_crtc_helper_atomic_disable, }; --Quoted 3 lines
2.56.0
Best regards,
Jie Feng
Signature · 6 lines
-- Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)
Thomas Zimmermann
maintainerPATCH 10/11 drm/udl: Allocate URBs on demand
In reply to Thomas ZimmermannRemove 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.
--- drivers/gpu/drm/udl/udl_drv.h | 1 - drivers/gpu/drm/udl/udl_main.c | 123 ++++++++++++--------------------- 2 files changed, 44 insertions(+), 80 deletions(-)
drivers/gpu/drm/udl/udl_drv.h +0 -1
@@ -45,7 +45,6 @@ struct urb_list {spinlock_t lock;wait_queue_head_t sleep;unsigned int in_flight; -int count; };struct udl_device {
drivers/gpu/drm/udl/udl_main.c +44 -79
178 lines not on this page · raw message
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH 10/11] drm/udl: Allocate URBs on demand
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/udl: missing size fallback and in-flight wait in udl_get_urb() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/udl: missing size fallback and in-flight wait in udl_get_urb()
--
commit 2b67449c9bc54fc48767cd1733efcaed21d924b2
Author: Thomas Zimmermann <[email protected]>
drm/udl: Allocate URBs on demand
This commit removes the fixed upfront allocation of URBs and instead allocates URBs on demand in udl_get_urb(), caching them for reuse across frames.
Quoted 4 lines
drivers/gpu/drm/udl/udl_main.c +0
-0
[ ... ]
Quoted 37 lines
drivers/gpu/drm/udl/udl_main.c +1
-24@@ -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++; - }
[ ... ]
Quoted 12 lines
drivers/gpu/drm/udl/udl_main.c +5
-3@@ -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 );
drivers/gpu/drm/udl/udl_main.c struct urb *udl_get_urb(struct udl_device *udl) · line 296
Quoted 10 lines
drivers/gpu/drm/udl/udl_main.c +7
-0+if (IS_ERR (urb)) { + scoped_guard(spinlock_irqsave, &udl->urbs.lock) { +if (udl->urbs.in_flight) + --udl->urbs.in_flight; + } +return NULL ; + } }return urb;
[Severity: Medium]
Should udl_get_urb() fall back to smaller allocation sizes or wait for an in-flight URB when udl_alloc_bulk_urb(udl, MAX_TRANSFER) fails?
MAX_TRANSFER is PAGE_SIZE * 16 - BULK_SIZE, which requires an order-4 contiguous coherent DMA allocation via usb_alloc_coherent(). Previously, udl_alloc_urb_list() handled -ENOBUFS by halving the buffer size down to PAGE_SIZE (added in 542bb9788a1f ("udl-kms: handle allocation failure")), and udl_alloc_bulk_urb() still records unode->alloc_size and returns -ENOBUFS for this purpose.
Because URBs are now allocated on demand during modesets and frame updates when memory may be fragmented, a single order-4 allocation failure causes udl_get_urb() to return NULL immediately (even when other URBs are already in flight), aborting udl_crtc_helper_atomic_enable() or udl_handle_damage() and leaving the display blank or partially updated.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Thomas Zimmermann
maintainerRe: [PATCH 10/11] drm/udl: Allocate URBs on demand
Am 19.11.26 um 15:47 schrieb [email protected]:
Quoted 79 lines
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/udl: missing size fallback and in-flight wait inudl_get_urb()
--commit
2b67449c9bc54fc48767cd1733efcaed21d924b2
Author: Thomas Zimmermann <[email protected]>drm/udl: Allocate URBs on demand
This commit removes the fixed upfront allocation of URBs and instead allocates URBs on demand in
udl_get_urb(), caching them for reuse across frames.Quoted 4 lines
drivers/gpu/drm/udl/udl_main.c +0
-0[ ... ]
Quoted 37 lines
drivers/gpu/drm/udl/udl_main.c +1
-24@@ -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++; - }[ ... ]
Quoted 21 lines
drivers/gpu/drm/udl/udl_main.c +12
-3@@ -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 ; + } }
Quoted 4 lines
drivers/gpu/drm/udl/udl_main.c struct urb *udl_get_urb(struct udl_device *udl) · line 296
Quoted 1 line
drivers/gpu/drm/udl/udl_main.c +0
-0return urb;[Severity: Medium]
Shouldudl_get_urb()fall back to smaller allocation sizes or wait for an in-flight URB whenudl_alloc_bulk_urb(udl, MAX_TRANSFER)fails?
The was deliberate to reduce complexity. I assume that we shouldn't run that display driver if these allocations fail. But if the removal turns out to be a problem, the logic can also remain.
Quoted 14 lines
MAX_TRANSFERisPAGE_SIZE* 16 -BULK_SIZE, which requires an order-4 contiguous coherent DMA allocation viausb_alloc_coherent(). Previously,udl_alloc_urb_list()handled-ENOBUFSby halving the buffer size down toPAGE_SIZE(added in542bb9788a1f("udl-kms: handle allocation failure")), andudl_alloc_bulk_urb()still recordsunode->alloc_sizeand returns-ENOBUFSfor this purpose.Because URBs are now allocated on demand during modesets and frame updates when memory may be fragmented, a single order-4 allocation failure causes
udl_get_urb()to return NULL immediately (even when other URBs are already in flight), abortingudl_crtc_helper_atomic_enable()orudl_handle_damage()and leaving the display blank or partially updated.
Signature · 6 lines
-- Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)
Re: [PATCH 10/11] drm/udl: Allocate URBs on demand
In reply to Thomas ZimmermannQuoted 187 lines · Thomas Zimmermann
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(-)drivers/gpu/drm/udl/udl_drv.h +0
-1@@ -45,7 +45,6 @@ struct urb_list {spinlock_t lock;wait_queue_head_t sleep;unsigned int in_flight; -int count; };struct udl_device {drivers/gpu/drm/udl/udl_main.c +32
-75@@ -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); + }
drivers/gpu/drm/udl/udl_main.c struct urb *udl_get_urb(struct udl_device *udl) · line 293
Quoted 10 lines
drivers/gpu/drm/udl/udl_main.c +10
-0+ +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.
Quoted 24 lines
drivers/gpu/drm/udl/udl_main.c +2
-5}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); --
Quoted 3 lines
2.56.0
Best regards,
Jie Feng
Thomas Zimmermann
maintainerRe: [PATCH 10/11] drm/udl: Allocate URBs on demand
Hi
Am 20.11.26 um 07:54 schrieb Jie Feng:
Quoted 194 lines · Thomas Zimmermann
Quoted 193 lines
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(-)drivers/gpu/drm/udl/udl_drv.h +0
-1@@ -45,7 +45,6 @@ struct urb_list {spinlock_t lock;wait_queue_head_t sleep;unsigned int in_flight; -int count; };struct udl_device {drivers/gpu/drm/udl/udl_main.c +39
-75@@ -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;
Quoted 8 lines
drivers/gpu/drm/udl/udl_main.c struct urb *udl_get_urb(struct udl_device *udl) · line 293
Quoted 3 lines
drivers/gpu/drm/udl/udl_main.c +3
-0+ } +return NULL ; + }On a usb contention or stalled device, this might allocate memory unboundedly if the
udl_sync_pending_urbskeeps 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.
I wouldn't worry about this problem. Only compositors can send page flips. If the sync times out, the compositor should throttle down the page-flip rate.So it's unlikely to happen in practice. And a malicious compositor can already bring down the system today.
For the cleaning up unused allocations, I think a shrinker would be best. It's the kernel's data structure for clearing such caches when needed. It runs when needed and can also be tested via debugfs. That would be another patch on top, I think.
Best regards
Thomas
Quoted 31 lines
Quoted 25 lines
drivers/gpu/drm/udl/udl_main.c +2
-5}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); --Quoted 3 lines
2.56.0
Best regards,
Jie Feng
Signature · 6 lines
-- Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)
Thomas Zimmermann
maintainerPATCH 11/11 drm/udl: Implement URB cache as LIFO stack
In reply to Thomas ZimmermannInsert completed URBs at the list head of the URB cache instead of the tail. Improves locality in the CPU cache.
--- drivers/gpu/drm/udl/udl_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/udl/udl_main.c +1 -1
@@ -305,7 +305,7 @@ void udl_put_urb(struct urb *urb) urb->transfer_buffer_length = unode->alloc_size;/* reset to actual */ scoped_guard(spinlock_irqsave, &udl->urbs.lock) { -list_add_tail (&unode->entry, &udl->urbs.list); +list_add (&unode->entry, &udl->urbs.list);if (!drm_WARN_ON_ONCE (drm, !udl->urbs.in_flight)) --udl->urbs.in_flight;
Signature · 1 line
2.56.0