hmm_range_fault_unlocked_timeout() starts its retry deadline before mmu_interval_read_begin() and mmap_read_lock_killable(), and checks it before calling hmm_range_fault_locked(). Both calls can sleep for a long time if another thread is invalidating or holds mmap_lock for write. Today, if that sleep is longer than the timeout, we return -EBUSY without even trying the fault, although nothing invalidated our range. This is seen with igt@xe_exec_threads@threads-hang-*userptr-invalidate*: one thread's mmap(MAP_FIXED) holds mmap_lock for write while the xe notifier waits for a hung job to time out. Another thread repinning its own userptr waits behind it for more than HMM_RANGE_DEFAULT_TIMEOUT, and DRM_IOCTL_XE_EXEC fails with -EBUSY: xe/xe_ioctl-CRITICAL: Failed assertion: __xe_exec(fd, exec) == 0 xe/xe_ioctl-CRITICAL: error: -16 != 0 Time spent waiting in mmu_interval_read_begin() and mmap_read_lock_killable() still counts toward the deadline, but only check for expiry after hmm_range_fault_locked() returns -EBUSY with mmap_lock still held, i.e. after an actual invalidation collision. A long wait therefore no longer fails on its own; the fault is always attempted at least once. The deadline is still reset when a fault drops mmap_lock. For drm_gpusvm_get_pages() this restores the behaviour of the open-coded loop the helper replaced, which only checked the deadline after hmm_range_fault() returned -EBUSY. v2: (Stanislav) - Clarify that notifier/mmap_lock wait time still counts toward the deadline and that expiry is only checked after an invalidation collision - Scope the "restores previous behaviour" statement to drm_gpusvm_get_pages(); the old drm_gpusvm_range_evict() loop could time out before faulting (Stanislav) Assisted-by: LLM Closes: https://gitlab.freedesktop.org/drm/xe/kernel/-/work_items/9096 Fixes: 121170831228 ("mm/hmm: add hmm_range_fault_unlocked_timeout() for mmap lock-drop support") Reviewed-by: Stanislav Kinsburskii Cc: Stanislav Kinsburskii Cc: Jason Gunthorpe Cc: Leon Romanovsky Cc: Andrew Morton Cc: Matthew Brost Cc: Thomas Hellstrom Cc: linux-mm@kvack.org Cc: dri-devel@lists.freedesktop.org Signed-off-by: Nitin Gote --- mm/hmm.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mm/hmm.c b/mm/hmm.c index 9871d3c3cd07..dbaedd5e4c1b 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -815,15 +815,15 @@ int hmm_range_fault_unlocked_timeout(struct hmm_range *range, return -EFAULT; } - if (timeout && time_after(jiffies, deadline)) { - mmap_read_unlock(mm); - return -EBUSY; - } - locked = true; ret = hmm_range_fault_locked(range, &locked); - if (locked) + if (locked) { mmap_read_unlock(mm); + /* Only act on an expired deadline after an invalidation collision */ + if (ret == -EBUSY && timeout && + time_after(jiffies, deadline)) + return -EBUSY; + } } while (ret == -EBUSY); return ret; -- 2.50.1