Check for overflow in udelay/usleep_range use in __xe_mmio_wait32
and pick the correct helper accordingly. Fix sign-overflow and avoid
growing delays becoming intollerably large by capping the in-loop
wait time. Simplify sleep_min assignment by removing redundant cast.

Fixes: 5c09bd6ccd41 ("drm/xe/mmio: Move xe_mmio_wait32() to xe_mmio.c")
Signed-off-by: Alan Previn <[email protected]>
Assisted-by: Github-Copilot:Claude-Sonnet-4-6
---
 drivers/gpu/drm/xe/xe_mmio.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_mmio.c b/drivers/gpu/drm/xe/xe_mmio.c
index 7fa18dfcb5a2..cf2e05aaa4b2 100644
--- a/drivers/gpu/drm/xe/xe_mmio.c
+++ b/drivers/gpu/drm/xe/xe_mmio.c
@@ -349,11 +349,23 @@ static int __xe_mmio_wait32(struct xe_mmio *mmio, struct 
xe_reg reg, u32 mask, u
                if (ktime_after(ktime_add_us(cur, wait), end))
                        wait = ktime_us_delta(end, cur);
 
-               if (atomic)
-                       udelay(wait);
-               else
-                       usleep_range(wait, wait << 1);
-               wait <<= 1;
+#define __XE_MMIO_WAIT_MAX_INLOOP_TIME (100 * USEC_PER_MSEC)
+               if (atomic) {
+                       if (wait <= MAX_UDELAY_MS * USEC_PER_MSEC)
+                               udelay(wait);
+                       else
+                               mdelay(DIV_ROUND_UP(wait, USEC_PER_MSEC));
+               } else {
+                       usleep_range(wait, __XE_MMIO_WAIT_MAX_INLOOP_TIME);
+               }
+               /*
+                * As we keep doubling the wait time for every check that 
fails, cap the
+                * in-loop delay-or-sleep to less than 2x 100 miliseconds to 
prevent from
+                * expanding 'wait' into exponentially longer wait times per 
loop that
+                * end up delaying the next completion check way later than 
tolerable.
+                */
+               wait = wait < __XE_MMIO_WAIT_MAX_INLOOP_TIME >> 1 ?
+                      wait << 1 : __XE_MMIO_WAIT_MAX_INLOOP_TIME;
        }
 
        if (ret != 0) {

base-commit: 71bc3b7cc55631a9b3807da98e2b2880838b9623
-- 
2.43.0

Reply via email to