Skip to contentSkip to conversation

drm/amdgpu: clean up userq iVA mapping after removing userq from MES

PATCH In review Prike Liang 4 participants 11 messages May 14 – May 21

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 drm/amdgpu: clean up userq iVA mapping after removing userq from MES

User queue destroy removed the tracked queue VA mappings before removing the HW queue from MES. If the queue still had active waves, HW like as TCP could continue accessing queue backing memory after the VM mappings were removed, resulting in gfxhub page faults.

So, that needs to move queue VA cleanup after HW queue unmap. Meanwhile, if MES fails to remove the queue, then need to run reset recovery before freeing queue resources.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 43 +++++++++++++++++------
 1 file changed, 32 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +32 -11
index 83aee0810513..2e3edb6dd506 100644
@@ -625,8 +625,7 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
 	struct amdgpu_device *adev = uq_mgr->adev;
 	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
 	struct amdgpu_vm *vm = &fpriv->vm;
-
-	int r = 0;
+	int r = 0, tmp;
 
 	trace_amdgpu_userq_destroy_start(queue);
 
@@ -635,15 +634,6 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
 	/* Cancel any pending hang detection work and cleanup */
 	cancel_delayed_work_sync(&queue->hang_detect_work);
 
-	r = amdgpu_bo_reserve(vm->root.bo, false);
-	if (r) {
-		drm_file_err(uq_mgr->file, "Failed to reserve root bo during userqueue destroy\n");
-		trace_amdgpu_userq_destroy_end(queue, r);
-		return r;
-	}
-	amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
-	amdgpu_bo_unreserve(vm->root.bo);
-
 	mutex_lock(&uq_mgr->userq_mutex);
 	amdgpu_userq_wait_for_last_fence(queue);
 
@@ -651,6 +641,37 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
 	debugfs_remove_recursive(queue->debugfs_queue);
 #endif
 	r = amdgpu_userq_unmap_helper(queue);
+
+	if (r) {
+		drm_file_err(uq_mgr->file,
+			     "Failed to unmap userqueue during destroy, ret=%d\n",
+			     r);
+		amdgpu_userq_fence_driver_force_completion(queue);
+		amdgpu_reset_domain_schedule(uq_mgr->adev->reset_domain,
+					     &uq_mgr->reset_work);
+		flush_work(&uq_mgr->reset_work);
+	}
+	mutex_unlock(&uq_mgr->userq_mutex);
+	/*
+	 * Drop the queue VA mappings only after the HW queue is removed (or
+	 * reset recovery has run). Removing the mappings first lets active TCP
+	 * waves fault on queue backing memory while MES is still trying to
+	 * process REMOVE_QUEUE.
+	 */
+	tmp = amdgpu_bo_reserve(vm->root.bo, false);
+	if (tmp) {
+		drm_file_err(uq_mgr->file,
+			     "Failed to reserve root bo during userqueue destroy\n");
+		if (!r)
+			r = tmp;
+	} else {
+		tmp = amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
+		amdgpu_bo_unreserve(vm->root.bo);
+		if (tmp && !r)
+			r = tmp;
+	}
+
+	mutex_lock(&uq_mgr->userq_mutex);
 	atomic_dec(&uq_mgr->userq_count[queue->queue_type]);
 	amdgpu_userq_cleanup(queue);
 	mutex_unlock(&uq_mgr->userq_mutex);
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 drm/amdgpu: unmap userq for evicting user queue

If the driver only preempts queues, there can still be inflight waves, pending dispatch state, or resume/redispatch possibility tied to the same queue. Then the VM/TTM side may proceed to move/unmap queue related BOs during evicting the queue while shader TCP clients still need to access them.

So for eviction, unmap is safer because it makes the queue nonrunnable before memory backing is invalidated. Meanwhile, for a idle queue it's more sutiable for unmapping it rather preempt and unmapping also safe more processing time than preempt.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -2
index 2e3edb6dd506..a63dfdfa4195 100644
@@ -1048,7 +1048,7 @@ amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
 			continue;
 		}
 
-		r = amdgpu_userq_restore_helper(queue);
+		r = amdgpu_userq_map_helper(queue);
 		if (r)
 			ret = r;
 
@@ -1285,7 +1285,7 @@ amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr)
 
 	/* Try to unmap all the queues in this process ctx */
 	xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
-		r = amdgpu_userq_preempt_helper(queue);
+		r = amdgpu_userq_unmap_helper(queue);
 		if (r)
 			ret = r;
 	}
Signature · 1 line
2.34.1
#2 lore raw

RE: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

AMD General

Will resent a new version for updating the commit log.

Regards,
Prike

Quoted 50 lines

-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Thursday, May 14, 2026 8:43 PM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Liang, Prike <[email protected]>
Subject: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

If the driver only preempts queues, there can still be inflight waves, pending dispatch state, or resume/redispatch possibility tied to the same queue. Then the VM/TTM side may proceed to move/unmap queue related BOs during evicting the queue while shader TCP clients still need to access them.

So for eviction, unmap is safer because it makes the queue nonrunnable before memory backing is invalidated. Meanwhile, for a idle queue it's more sutiable for unmapping it rather preempt and unmapping also safe more processing time than preempt.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -2
index 2e3edb6dd506..a63dfdfa4195 100644
@@ -1048,7 +1048,7 @@ amdgpu_userq_restore_all(struct amdgpu_userq_mgr
 *uq_mgr)
                      continue;
              }
 
-             r = amdgpu_userq_restore_helper(queue);
+             r = amdgpu_userq_map_helper(queue);
              if (r)
                      ret = r;
@@ -1285,7 +1285,7 @@ amdgpu_userq_evict_all(struct amdgpu_userq_mgr
 *uq_mgr)
 
      /* Try to unmap all the queues in this process ctx */
      xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
-             r = amdgpu_userq_preempt_helper(queue);
+             r = amdgpu_userq_unmap_helper(queue);
              if (r)
                      ret = r;
      }

--
2.34.1

#3 lore raw

Re: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

Quoted 40 lines · Prike

AMD General

Will resent a new version for updating the commit log.

Regards,
Prike

Quoted 33 lines

-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Thursday, May 14, 2026 8:43 PM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Liang, Prike <[email protected]>
Subject: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

If the driver only preempts queues, there can still be inflight waves, pending dispatch state, or resume/redispatch possibility tied to the same queue. Then the VM/TTM side may proceed to move/unmap queue related BOs during evicting the queue while shader TCP clients still need to access them.

So for eviction, unmap is safer because it makes the queue nonrunnable before memory backing is invalidated. Meanwhile, for a idle queue it's more sutiable for unmapping it rather preempt and unmapping also safe more processing time than preempt.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 2e3edb6dd506..a63dfdfa4195 100644
@@ -1048,7 +1048,7 @@ amdgpu_userq_restore_all(struct amdgpu_userq_mgr
 *uq_mgr)
                       continue;
               }
 
Quoted 2 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr) · line 1051

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
-             r = amdgpu_userq_restore_helper(queue);
+             r = amdgpu_userq_map_helper(queue);

As per my understanding, we arent done for the queue and all the resources of the should remain intact. A queue ideally should only need " mapping/add new queue" when the queue is created and once that is done only the light process or suspend/resume should be good enough. We dont want to tear it down or rebuild again for anything transient.

Restore/evict in most of the cases is a transient stage and only a tear down should be unmapping it or during a GPU reset which reset all the hw states. So the way it is seems logical to me but i leave that to Christian to confirm.

Regards
Sunil Khatri

Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
               if (r)
                       ret = r;
@@ -1285,7 +1285,7 @@ amdgpu_userq_evict_all(struct amdgpu_userq_mgr
 *uq_mgr)
 
       /* Try to unmap all the queues in this process ctx */
       xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
-             r = amdgpu_userq_preempt_helper(queue);
+             r = amdgpu_userq_unmap_helper(queue);
               if (r)
                       ret = r;
       }

--
2.34.1

#4 lore raw

RE: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

AMD General

Regards,
Prike

Quoted 60 lines

-----Original Message-----
From: Khatri, Sunil <[email protected]>
Sent: Friday, May 15, 2026 12:53 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

Quoted 19 lines · Prike

AMD General

Will resent a new version for updating the commit log.

Regards,
Prike

Quoted 12 lines

-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Thursday, May 14, 2026 8:43 PM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Liang, Prike <[email protected]>
Subject: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

If the driver only preempts queues, there can still be inflight waves, pending dispatch state, or resume/redispatch possibility tied to the same queue. Then the VM/TTM side may proceed to move/unmap queue related BOs during evicting the queue while shader TCP clients still need

to access them.

Quoted 22 lines
Quoted 22 lines

So for eviction, unmap is safer because it makes the queue nonrunnable before memory backing is invalidated. Meanwhile, for a idle queue it's more sutiable for unmapping it rather preempt and unmapping also safe more processing time than preempt.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 2e3edb6dd506..a63dfdfa4195 100644
@@ -1048,7 +1048,7 @@ amdgpu_userq_restore_all(struct

amdgpu_userq_mgr
*uq_mgr)

                       continue;
               }
Quoted 2 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr) · line 1051

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
-             r = amdgpu_userq_restore_helper(queue);
+             r = amdgpu_userq_map_helper(queue);

As per my understanding, we arent done for the queue and all the resources of the should remain intact. A queue ideally should only need " mapping/add new queue" when the queue is created and once that is done only the light process or suspend/resume should be good enough. We dont want to tear it down or rebuild again for anything transient.

Quoted 3 lines

Restore/evict in most of the cases is a transient stage and only a tear down should be unmapping it or during a GPU reset which reset all the hw states. So the way it is seems logical to me but i leave that to Christian to confirm.

For the eviction case, it will be a problem when the userq BOs migrated during the MES trying to save or access the resource for preempting the queue. Also, the preempt operation doesn't need to wait the queue to be idle, so here choose to wait the queue to be idle then it's more suitable for unmapping.

Quoted 17 lines

Regards
Sunil Khatri

Quoted 15 lines
Quoted 15 lines
               if (r)
                       ret = r;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
@@ -1285,7 +1285,7 @@ amdgpu_userq_evict_all(struct amdgpu_userq_mgr
 *uq_mgr)
 
       /* Try to unmap all the queues in this process ctx */
       xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
-             r = amdgpu_userq_preempt_helper(queue);
+             r = amdgpu_userq_unmap_helper(queue);
               if (r)
                       ret = r;
       }

--
2.34.1

#5 lore raw

RE: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

AMD General

Regards,
Prike

Quoted 80 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Liang, Prike
Sent: Friday, May 15, 2026 3:24 PM
To: Khatri, Sunil <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: RE: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

AMD General

Regards,
Prike

Quoted 56 lines

-----Original Message-----
From: Khatri, Sunil <[email protected]>
Sent: Friday, May 15, 2026 12:53 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

Quoted 22 lines · Prike

AMD General

Will resent a new version for updating the commit log.

Regards,
Prike

Quoted 15 lines

-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Thursday, May 14, 2026 8:43 PM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Liang, Prike <[email protected]>
Subject: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

If the driver only preempts queues, there can still be inflight waves, pending dispatch state, or resume/redispatch possibility tied to the same queue. Then the VM/TTM side may proceed to move/unmap queue related BOs during evicting the queue while shader TCP clients still need

to access them.

Quoted 22 lines
Quoted 22 lines

So for eviction, unmap is safer because it makes the queue nonrunnable before memory backing is invalidated. Meanwhile, for a idle queue it's more sutiable for unmapping it rather preempt and unmapping also safe more processing time than preempt.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 2e3edb6dd506..a63dfdfa4195 100644
@@ -1048,7 +1048,7 @@ amdgpu_userq_restore_all(struct

amdgpu_userq_mgr
*uq_mgr)

                       continue;
               }
Quoted 4 lines
Quoted 2 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr) · line 1051

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
-             r = amdgpu_userq_restore_helper(queue);
+             r = amdgpu_userq_map_helper(queue);

As per my understanding, we arent done for the queue and all the resources of the should remain intact. A queue ideally should only need "

mapping/add new queue"

Quoted 6 lines

when the queue is created and once that is done only the light process or suspend/resume should be good enough. We dont want to tear it down or rebuild again for anything transient.

Restore/evict in most of the cases is a transient stage and only a tear down should be unmapping it or during a GPU reset which reset all the hw

Quoted 7 lines

states.

Quoted 1 line

So the way it is seems logical to me but i leave that to Christian to confirm.

For the eviction case, it will be a problem when the userq BOs migrated during the MES trying to save or access the resource for preempting the queue. Also, the preempt operation doesn't need to wait the queue to be idle, so here choose to wait the queue to be idle then it's more suitable for unmapping.

Except for most cases the unbound wait + unmap should be fine, and this should avoid the SQ error, but for the long shader run the preempt should better and that need to draft solution for handling the userq wave context validation before restoring the userq.

Hi @Deucher, Alexander Can we use userq unmap to handle the SQ error issue for now? And leave the userq internal object validation during restore as a TODO item?

Quoted 18 lines
Quoted 18 lines

Regards
Sunil Khatri

Quoted 16 lines
Quoted 16 lines
               if (r)
                       ret = r;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
@@ -1285,7 +1285,7 @@ amdgpu_userq_evict_all(struct

amdgpu_userq_mgr
*uq_mgr)

       /* Try to unmap all the queues in this process ctx */
       xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -2
-             r = amdgpu_userq_preempt_helper(queue);
+             r = amdgpu_userq_unmap_helper(queue);
               if (r)
                       ret = r;
       }
--

2.34.1

maintainer Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

In reply to Prike Liang
Quoted 6 lines · Prike Liang

If the driver only preempts queues, there can still be inflight waves, pending dispatch state, or resume/redispatch possibility tied to the same queue. Then the VM/TTM side may proceed to move/unmap queue related BOs during evicting the queue while shader TCP clients still need to access them.

Quoted 6 lines

So for eviction, unmap is safer because it makes the queue nonrunnable before memory backing is invalidated. Meanwhile, for a idle queue it's more sutiable for unmapping it rather preempt and unmapping also safe more processing time than preempt.

I was already wondering when we switched to preempt if that is correct or not.

This patch here pretty much confirms that it was not correct. I need to discuss with Alex what to do here, but for now the patch is clearly a bug fix:

Thanks,
Christian.

Quoted 26 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -2
index 2e3edb6dd506..a63dfdfa4195 100644
@@ -1048,7 +1048,7 @@ amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
 			continue;
 		}
 
-		r = amdgpu_userq_restore_helper(queue);
+		r = amdgpu_userq_map_helper(queue);
 		if (r)
 			ret = r;
 
@@ -1285,7 +1285,7 @@ amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr)
 
 	/* Try to unmap all the queues in this process ctx */
 	xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
-		r = amdgpu_userq_preempt_helper(queue);
+		r = amdgpu_userq_unmap_helper(queue);
 		if (r)
 			ret = r;
 	}
maintainer Reviewed-by
#7 lore raw

RE: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

Public

Quoted 21 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, May 20, 2026 7:26 AM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

Quoted 6 lines · Prike Liang

If the driver only preempts queues, there can still be inflight waves, pending dispatch state, or resume/redispatch possibility tied to the same queue. Then the VM/TTM side may proceed to move/unmap queue related BOs during evicting the queue while shader TCP clients still need to access them.

Quoted 6 lines

So for eviction, unmap is safer because it makes the queue nonrunnable before memory backing is invalidated. Meanwhile, for a idle queue it's more sutiable for unmapping it rather preempt and unmapping also safe more processing time than preempt.

Quoted 7 lines

I was already wondering when we switched to preempt if that is correct or not.

This patch here pretty much confirms that it was not correct. I need to discuss with Alex what to do here, but for now the patch is clearly a bug fix:

What's the point of having a preempt operation if it doesn't actually preempt the work? Seems like a firmware bug.

Alex

Quoted 34 lines

Thanks,
Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_restore_all(struct

Quoted 10 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 2e3edb6dd506..a63dfdfa4195 100644
@@ -1048,7 +1048,7 @@ amdgpu_userq_restore_all(struct

amdgpu_userq_mgr *uq_mgr)

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_evict_all(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
                    continue;
            }
 
-           r = amdgpu_userq_restore_helper(queue);
+           r = amdgpu_userq_map_helper(queue);
            if (r)
                    ret = r;
 
@@ -1285,7 +1285,7 @@ amdgpu_userq_evict_all(struct

amdgpu_userq_mgr

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
 *uq_mgr)
 
    /* Try to unmap all the queues in this process ctx */
    xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
-           r = amdgpu_userq_preempt_helper(queue);
+           r = amdgpu_userq_unmap_helper(queue);
            if (r)
                    ret = r;
    }
#8 lore raw

RE: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

Public

Regards,
Prike

Quoted 34 lines

-----Original Message-----
From: Deucher, Alexander <[email protected]>
Sent: Thursday, May 21, 2026 6:01 AM
To: Koenig, Christian <[email protected]>; Liang, Prike <[email protected]>; [email protected]
Subject: RE: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

Public

Quoted 25 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, May 20, 2026 7:26 AM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: unmap userq for evicting user queue

Quoted 6 lines · Prike Liang

If the driver only preempts queues, there can still be inflight waves, pending dispatch state, or resume/redispatch possibility tied to the same queue. Then the VM/TTM side may proceed to move/unmap queue related BOs during evicting the queue while shader TCP clients still need to access them.

Quoted 6 lines

So for eviction, unmap is safer because it makes the queue nonrunnable before memory backing is invalidated. Meanwhile, for a idle queue it's more sutiable for unmapping it rather preempt and unmapping also safe more processing time than preempt.

I was already wondering when we switched to preempt if that is correct or not.

Quoted 7 lines
Quoted 4 lines

This patch here pretty much confirms that it was not correct. I need to discuss with Alex what to do here, but for now the patch is clearly a bug fix:

What's the point of having a preempt operation if it doesn't actually preempt the work? Seems like a firmware bug.

Maybe the MES executes the preempt operation correctly, but the driver may need to further handle the following things.
1) remove the unbound waiting for the queue preempt.
2) need to validate the user queue internal BOs like as CSA/WPTR/EOP etc as well before restoring the preempted queue
3) use the unmap the queue at the first place, but for the long shader context better to fallback to the preempt operation for simplifying save/restore latency and avoiding the unbound wait timeout.

Quoted 41 lines

Alex

Quoted 34 lines

Thanks,
Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_restore_all(struct

Quoted 10 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 2e3edb6dd506..a63dfdfa4195 100644
@@ -1048,7 +1048,7 @@ amdgpu_userq_restore_all(struct

amdgpu_userq_mgr *uq_mgr)

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_evict_all(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
                    continue;
            }
 
-           r = amdgpu_userq_restore_helper(queue);
+           r = amdgpu_userq_map_helper(queue);
            if (r)
                    ret = r;
 
@@ -1285,7 +1285,7 @@ amdgpu_userq_evict_all(struct

amdgpu_userq_mgr

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
 *uq_mgr)
 
    /* Try to unmap all the queues in this process ctx */
    xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
-           r = amdgpu_userq_preempt_helper(queue);
+           r = amdgpu_userq_unmap_helper(queue);
            if (r)
                    ret = r;
    }
#9 lore raw
In reply to Prike Liang
Quoted 45 lines · Prike Liang

User queue destroy removed the tracked queue VA mappings before removing the HW queue from MES. If the queue still had active waves, HW like as TCP could continue accessing queue backing memory after the VM mappings were removed, resulting in gfxhub page faults.

So, that needs to move queue VA cleanup after HW queue unmap. Meanwhile, if MES fails to remove the queue, then need to run reset recovery before freeing queue resources.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 43 +++++++++++++++++------
  1 file changed, 32 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -11
index 83aee0810513..2e3edb6dd506 100644
@@ -625,8 +625,7 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
  	struct amdgpu_device *adev = uq_mgr->adev;
  	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
  	struct amdgpu_vm *vm = &fpriv->vm;
-
-	int r = 0;
+	int r = 0, tmp;
 
  	trace_amdgpu_userq_destroy_start(queue);
 
@@ -635,15 +634,6 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
  	/* Cancel any pending hang detection work and cleanup */
  	cancel_delayed_work_sync(&queue->hang_detect_work);
 
-	r = amdgpu_bo_reserve(vm->root.bo, false);
-	if (r) {
-		drm_file_err(uq_mgr->file, "Failed to reserve root bo during userqueue destroy\n");
-		trace_amdgpu_userq_destroy_end(queue, r);
-		return r;
-	}
-	amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
-	amdgpu_bo_unreserve(vm->root.bo);
-
  	mutex_lock(&uq_mgr->userq_mutex);
  	amdgpu_userq_wait_for_last_fence(queue);
 

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que · line 649

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
@@ -651,6 +641,37 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
  	debugfs_remove_recursive(queue->debugfs_queue);
  #endif
  	r = amdgpu_userq_unmap_helper(queue);
+
+	if (r) {
+		drm_file_err(uq_mgr->file,
+			     "Failed to unmap userqueue during destroy, ret=%d\n",
+			     r);
+		amdgpu_userq_fence_driver_force_completion(queue);

We should be calling this before amdgpu_userq_unmap_helper because we are in tear down and we want to finish all the work and their corresponding fences and if unmap fails or not we should be cleaning up the resources.

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que · line 651

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+		amdgpu_reset_domain_schedule(uq_mgr->adev->reset_domain,
+					     &uq_mgr->reset_work);

reset at this stage during tear down seems like a bad idea, we are infact waiting for if any reset thread if already scheduled.

regards
Sunil khatri

Quoted 26 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +23 -0
+		flush_work(&uq_mgr->reset_work);
+	}
+	mutex_unlock(&uq_mgr->userq_mutex);
+	/*
+	 * Drop the queue VA mappings only after the HW queue is removed (or
+	 * reset recovery has run). Removing the mappings first lets active TCP
+	 * waves fault on queue backing memory while MES is still trying to
+	 * process REMOVE_QUEUE.
+	 */
+	tmp = amdgpu_bo_reserve(vm->root.bo, false);
+	if (tmp) {
+		drm_file_err(uq_mgr->file,
+			     "Failed to reserve root bo during userqueue destroy\n");
+		if (!r)
+			r = tmp;
+	} else {
+		tmp = amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
+		amdgpu_bo_unreserve(vm->root.bo);
+		if (tmp && !r)
+			r = tmp;
+	}
+
+	mutex_lock(&uq_mgr->userq_mutex);
  	atomic_dec(&uq_mgr->userq_count[queue->queue_type]);
  	amdgpu_userq_cleanup(queue);
  	mutex_unlock(&uq_mgr->userq_mutex);
#10 lore raw

AMD General

Regards,
Prike

From: Khatri, Sunil <[email protected]>
Sent: Friday, May 15, 2026 1:08 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: clean up userq iVA mapping after removing userq from MES

On 14-05-2026 06:12 pm, Prike Liang wrote:

User queue destroy removed the tracked queue VA mappings before removing

the HW queue from MES. If the queue still had active waves, HW like as TCP

could continue accessing queue backing memory after the VM mappings were

removed, resulting in gfxhub page faults.

So, that needs to move queue VA cleanup after HW queue unmap. Meanwhile,

if MES fails to remove the queue, then need to run reset recovery before

freeing queue resources.

---

 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 43 +++++++++++++++++------
 1 file changed, 32 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0

index 83aee0810513..2e3edb6dd506 100644

--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c

+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c

@@ -625,8 +625,7 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que

        struct amdgpu_device *adev = uq_mgr->adev;

        struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);

        struct amdgpu_vm *vm = &fpriv->vm;

-

- int r = 0;

+ int r = 0, tmp;

  trace_amdgpu_userq_destroy_start(queue);

@@ -635,15 +634,6 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que

        /* Cancel any pending hang detection work and cleanup */
  cancel_delayed_work_sync(&queue->hang_detect_work);

- r = amdgpu_bo_reserve(vm->root.bo, false);

- if (r) {

- drm_file_err(uq_mgr->file, "Failed to reserve root bo during userqueue destroy\n");

- trace_amdgpu_userq_destroy_end(queue, r);

- return r;

- }

- amdgpu_userq_buffer_vas_list_cleanup(adev, queue);

- amdgpu_bo_unreserve(vm->root.bo);

-

        mutex_lock(&uq_mgr->userq_mutex);
  amdgpu_userq_wait_for_last_fence(queue);

@@ -651,6 +641,37 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que

  debugfs_remove_recursive(queue->debugfs_queue);

#endif

        r = amdgpu_userq_unmap_helper(queue);

+

+ if (r) {

+ drm_file_err(uq_mgr->file,

+ "Failed to unmap userqueue during destroy, ret=%d\n",

+ r);

+ amdgpu_userq_fence_driver_force_completion(queue);
We should be calling this before amdgpu_userq_unmap_helper because we are in tear down and we want to finish all the work and their corresponding fences and if unmap fails or not we should be cleaning up the resources.

When run the userq tearn down and most likely the user queue already idea, but if there is invalid free operation from user space and while the MES fail to unmap such case then here need to force the fence done. But if place the force fence completed before unmap the queue then it seems not correct since it possible to mark a normal signaled fence as error fence and also overhead for forcing signaling the fence in the most normal free cases.

+ amdgpu_reset_domain_schedule(uq_mgr->adev->reset_domain,

+ &uq_mgr->reset_work);

reset at this stage during tear down seems like a bad idea, we are infact waiting for if any reset thread if already scheduled.

As far as I can If there doesn’t trigger the gpu reset work explicitly then this case will not schedule the gpu reset detect work even the unmap hang up.

regards
Sunil khatri

+ flush_work(&uq_mgr->reset_work);

+ }

+ mutex_unlock(&uq_mgr->userq_mutex);

+ /*

+ * Drop the queue VA mappings only after the HW queue is removed (or

+ * reset recovery has run). Removing the mappings first lets active TCP

+ * waves fault on queue backing memory while MES is still trying to

+ * process REMOVE_QUEUE.

+ */

+ tmp = amdgpu_bo_reserve(vm->root.bo, false);

+ if (tmp) {

+ drm_file_err(uq_mgr->file,

+ "Failed to reserve root bo during userqueue destroy\n");

+ if (!r)

+ r = tmp;

+ } else {

+ tmp = amdgpu_userq_buffer_vas_list_cleanup(adev, queue);

+ amdgpu_bo_unreserve(vm->root.bo);

+ if (tmp && !r)

+ r = tmp;

+ }

+

+ mutex_lock(&uq_mgr->userq_mutex);

  atomic_dec(&uq_mgr->userq_count[queue->queue_type]);
        amdgpu_userq_cleanup(queue);

        mutex_unlock(&uq_mgr->userq_mutex);