All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Zimmermann <tzimmermann@suse.de>
To: airlied@redhat.com, sean@poorly.run, tiwai@suse.com,
	patrik.r.jakobsson@gmail.com, maarten.lankhorst@linux.intel.com,
	mripard@kernel.org, airlied@gmail.com, simona@ffwll.ch
Cc: dri-devel@lists.freedesktop.org, sashiko-reviews@lists.linux.dev,
	Thomas Zimmermann <tzimmermann@suse.de>
Subject: [PATCH 08/11] drm/udl: Track number of URBs in flight
Date: Thu, 19 Nov 2026 15:02:20 +0100	[thread overview]
Message-ID: <20261119142114.152052-9-tzimmermann@suse.de> (raw)
In-Reply-To: <20261119142114.152052-1-tzimmermann@suse.de>

Add 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.

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
---
 drivers/gpu/drm/udl/udl_drv.h  |  2 +-
 drivers/gpu/drm/udl/udl_main.c | 58 +++++++++++++++++++---------------
 2 files changed, 34 insertions(+), 26 deletions(-)

diff --git a/drivers/gpu/drm/udl/udl_drv.h b/drivers/gpu/drm/udl/udl_drv.h
index 2722625ab7a8..420166bdc4bc 100644
--- a/drivers/gpu/drm/udl/udl_drv.h
+++ b/drivers/gpu/drm/udl/udl_drv.h
@@ -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;
 };
 
diff --git a/drivers/gpu/drm/udl/udl_main.c b/drivers/gpu/drm/udl/udl_main.c
index 21b85d9c7d69..d0195bd4213d 100644
--- a/drivers/gpu/drm/udl/udl_main.c
+++ b/drivers/gpu/drm/udl/udl_main.c
@@ -9,9 +9,11 @@
  */
 
 #include <linux/device.h>
+#include <linux/limits.h>
 #include <linux/unaligned.h>
 
 #include <drm/drm.h>
+#include <drm/drm_drv.h>
 #include <drm/drm_print.h>
 #include <drm/drm_probe_helper.h>
 
@@ -252,7 +254,7 @@ static int udl_alloc_urb_list(struct udl_device *udl, int count, size_t size)
 	INIT_LIST_HEAD(&udl->urbs.list);
 	init_waitqueue_head(&udl->urbs.sleep);
 	udl->urbs.count = 0;
-	udl->urbs.available = 0;
+	udl->urbs.in_flight = 0;
 
 retry:
 	while (udl->urbs.count * size < wanted_size) {
@@ -273,7 +275,6 @@ static int udl_alloc_urb_list(struct udl_device *udl, int count, size_t size)
 		list_add_tail(&unode->entry, &udl->urbs.list);
 
 		udl->urbs.count++;
-		udl->urbs.available++;
 	}
 
 	if (!udl->urbs.count)
@@ -298,29 +299,33 @@ static struct urb *udl_get_urb_locked(struct udl_device *udl, long timeout)
 	if (!wait_event_lock_irq_timeout(udl->urbs.sleep,
 					 !udl->urbs.count ||
 					 !list_empty(&udl->urbs.list),
-					 udl->urbs.lock, timeout)) {
-		DRM_INFO("wait for urb interrupted: available: %d\n",
-			 udl->urbs.available);
+					 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);
-	udl->urbs.available--;
 
 	return unode->urb;
 }
 
 struct urb *udl_get_urb(struct udl_device *udl)
 {
+	struct drm_device *drm = &udl->drm;
 	struct urb *urb;
 
-	spin_lock_irq(&udl->urbs.lock);
-	urb = udl_get_urb_locked(udl, HZ * 2);
-	spin_unlock_irq(&udl->urbs.lock);
+	scoped_guard(spinlock_irq, &udl->urbs.lock) {
+		if (drm_WARN_ON_ONCE(drm, udl->urbs.in_flight == UINT_MAX))
+			return NULL;
+		++udl->urbs.in_flight;
+
+		urb = udl_get_urb_locked(udl, HZ * 2);
+		if (!urb)
+			--udl->urbs.in_flight;
+	}
+
 	return urb;
 }
 
@@ -328,16 +333,18 @@ void udl_put_urb(struct urb *urb)
 {
 	struct urb_node *unode = urb->context;
 	struct udl_device *udl = unode->dev;
-	unsigned long flags;
+	struct drm_device *drm = &udl->drm;
 
 	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);
-	udl->urbs.available++;
-	spin_unlock_irqrestore(&udl->urbs.lock, flags);
+	scoped_guard(spinlock_irqsave, &udl->urbs.lock) {
+		list_add_tail(&unode->entry, &udl->urbs.list);
+
+		if (!drm_WARN_ON_ONCE(drm, !udl->urbs.in_flight))
+			--udl->urbs.in_flight;
 
-	wake_up(&udl->urbs.sleep);
+		wake_up(&udl->urbs.sleep);
+	}
 }
 
 int udl_submit_urb(struct udl_device *udl, struct urb *urb, size_t len)
@@ -369,16 +376,17 @@ int udl_submit_urb(struct udl_device *udl, struct urb *urb, size_t len)
 /* wait until all pending URBs have been processed */
 void udl_sync_pending_urbs(struct udl_device *udl)
 {
-	struct drm_device *dev = &udl->drm;
+	struct drm_device *drm = &udl->drm;
+	long ret;
 
-	spin_lock_irq(&udl->urbs.lock);
-	/* 2 seconds as a sane timeout */
-	if (!wait_event_lock_irq_timeout(udl->urbs.sleep,
-					 udl->urbs.available == udl->urbs.count,
-					 udl->urbs.lock,
-					 msecs_to_jiffies(2000)))
-		drm_err(dev, "Timeout for syncing pending URBs\n");
-	spin_unlock_irq(&udl->urbs.lock);
+	scoped_guard(spinlock_irq, &udl->urbs.lock) {
+		/* 2 seconds as a sane timeout */
+		ret = wait_event_lock_irq_timeout(udl->urbs.sleep, !udl->urbs.in_flight,
+						  udl->urbs.lock, msecs_to_jiffies(2000));
+	}
+
+	if (!ret)
+		drm_warn_ratelimited(drm, "Timeout for syncing pending URBs\n");
 }
 
 int udl_init(struct udl_device *udl)
-- 
2.56.0


  parent reply	other threads:[~2026-11-19 14:23 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 14:02 [PATCH 00/11] drm/udl: Rework URB allocation Thomas Zimmermann
2026-11-19 14:02 ` [PATCH 01/11] drm/udl: Release URB in udl_put_urb() Thomas Zimmermann
2026-11-19 17:03   ` Jie Feng
2026-11-19 14:02 ` [PATCH 02/11] drm/udl: Do not use URB completion handler for error cleanup Thomas Zimmermann
2026-11-19 14:02 ` [PATCH 03/11] drm/udl: Store per-URB allocation length in struct urb_node Thomas Zimmermann
2026-11-19 14:02 ` [PATCH 04/11] drm/udl: Allocate bulk URBs in helper functions Thomas Zimmermann
2026-11-19 16:38   ` Jie Feng
2026-11-20 12:49     ` Thomas Zimmermann
2026-11-20 16:15       ` Jie Feng
2026-11-19 14:02 ` [PATCH 05/11] drm/udl: Free " Thomas Zimmermann
2026-11-19 14:02 ` [PATCH 06/11] drm/udl: Rework the error handling in udl_submit_urb() Thomas Zimmermann
2026-11-19 14:02 ` [PATCH 07/11] drm/udl: Managed cleanup of URB cache Thomas Zimmermann
2026-11-19 14:02 ` Thomas Zimmermann [this message]
2026-11-19 14:02 ` [PATCH 09/11] drm/udl: Synchronize pending URBs on each frame Thomas Zimmermann
2026-11-20  6:50   ` Jie Feng
2026-11-20 12:52     ` Thomas Zimmermann
2026-11-19 14:02 ` [PATCH 10/11] drm/udl: Allocate URBs on demand Thomas Zimmermann
2026-11-19 14:47   ` sashiko-bot
2026-11-19 18:05     ` Thomas Zimmermann
2026-11-20  6:54   ` Jie Feng
2026-11-20 13:01     ` Thomas Zimmermann
2026-11-19 14:02 ` [PATCH 11/11] drm/udl: Implement URB cache as LIFO stack Thomas Zimmermann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119142114.152052-9-tzimmermann@suse.de \
    --to=tzimmermann@suse.de \
    --cc=airlied@gmail.com \
    --cc=airlied@redhat.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=patrik.r.jakobsson@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sean@poorly.run \
    --cc=simona@ffwll.ch \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.