Skip to contentSkip to conversation

drm/amdgpu: use a dummy owner for sysfs triggered cleaner shaders v3

PATCH In review Christian König 2 participants 6 messages Apr 4, 2025 – Apr 7, 2025

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

PATCH 1/2 drm/amdgpu: use a dummy owner for sysfs triggered cleaner shaders v3

Otherwise triggering sysfs multiple times without other submissions in between only runs the shader once.

v2: add some comment
v3: re-add missing cast

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +11 -3
index f64675b2ab75..76237961a08f 100644
@@ -1439,9 +1439,11 @@ static int amdgpu_gfx_run_cleaner_shader_job(struct amdgpu_ring *ring)
 	struct amdgpu_device *adev = ring->adev;
 	struct drm_gpu_scheduler *sched = &ring->sched;
 	struct drm_sched_entity entity;
+	static atomic_t counter;
 	struct dma_fence *f;
 	struct amdgpu_job *job;
 	struct amdgpu_ib *ib;
+	void * owner;
 	int i, r;
 
 	/* Initialize the scheduler entity */
@@ -1452,9 +1454,15 @@ static int amdgpu_gfx_run_cleaner_shader_job(struct amdgpu_ring *ring)
 		goto err;
 	}
 
-	r = amdgpu_job_alloc_with_ib(ring->adev, &entity, NULL,
-				     64, 0,
-				     &job);
+	/*
+	 * Use some unique dummy value as the owner to make sure we execute
+	 * the cleaner shader on each submission. The value just need to change
+	 * for each submission and is otherwise meaningless.
+	 */
+	owner = (void *)(unsigned long)atomic_inc_return(&counter),
+
+	r = amdgpu_job_alloc_with_ib(ring->adev, &entity, owner,
+				     64, 0, &job);
 	if (r)
 		goto err;
 
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 drm/amdgpu: cleanup amdgpu_vm_flush v3

This reverts commit c2cc3648ba517a6c270500b5447d5a1efdad5936. Turned out that this has some negative consequences for some workloads. Instead check if the cleaner shader should run directly.

While at it remove amdgpu_vm_need_pipeline_sync(), we also check again if the VMID has seen a GPU reset since last use and the gds switch setiing can be handled more simply as well.

Also remove some duplicate checks and re-order and document the code.

v2: restructure the while function
v3: fix logic error pointed out by Srini

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c |  3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 92 +++++++++-----------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  5 +-
 3 files changed, 36 insertions(+), 64 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +1 -2
index 802743efa3b3..ff2ca984279a 100644
@@ -191,8 +191,7 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
 	need_ctx_switch = ring->current_ctx != fence_ctx;
 	if (ring->funcs->emit_pipeline_sync && job &&
 	    ((tmp = amdgpu_sync_get_fence(&job->explicit_sync)) ||
-	     need_ctx_switch || amdgpu_vm_need_pipeline_sync(ring, job))) {
-
+	     (amdgpu_sriov_vf(adev) && need_ctx_switch))) {
 		need_pipe_sync = true;
 
 		if (tmp)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +33 -59

150 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +2 -3
index daa2f9b33620..e9ecdb96bafa 100644
@@ -493,7 +493,8 @@ int amdgpu_vm_validate(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		       struct ww_acquire_ctx *ticket,
 		       int (*callback)(void *p, struct amdgpu_bo *bo),
 		       void *param);
-int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
+int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
+		    bool need_pipe_sync);
 int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
 			  struct amdgpu_vm *vm, bool immediate);
 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
@@ -550,8 +551,6 @@ void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size,
 			   uint32_t fragment_size_default, unsigned max_level,
 			   unsigned max_bits);
 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
-bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
-				  struct amdgpu_job *job);
 void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);
 
 struct amdgpu_task_info *
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 2/2] drm/amdgpu: cleanup amdgpu_vm_flush v3

Quoted 93 lines · Christian König

This reverts commit c2cc3648ba517a6c270500b5447d5a1efdad5936. Turned out that this has some negative consequences for some workloads. Instead check if the cleaner shader should run directly.

While at it remove amdgpu_vm_need_pipeline_sync(), we also check again if the VMID has seen a GPU reset since last use and the gds switch setiing can be handled more simply as well.

Also remove some duplicate checks and re-order and document the code.

v2: restructure the while function
v3: fix logic error pointed out by Srini

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c |  3 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 92 +++++++++-----------------
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  5 +-
  3 files changed, 36 insertions(+), 64 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +1 -2
index 802743efa3b3..ff2ca984279a 100644
@@ -191,8 +191,7 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
  	need_ctx_switch = ring->current_ctx != fence_ctx;
  	if (ring->funcs->emit_pipeline_sync && job &&
  	    ((tmp = amdgpu_sync_get_fence(&job->explicit_sync)) ||
-	     need_ctx_switch || amdgpu_vm_need_pipeline_sync(ring, job))) {
-
+	     (amdgpu_sriov_vf(adev) && need_ctx_switch))) {
  		need_pipe_sync = true;
 
  		if (tmp)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -38
index b5ddfcbbc9fc..e6d7db8d40cd 100644
@@ -596,37 +596,6 @@ void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
  	}
  }
 
-/**
- * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job.
- *
- * @ring: ring on which the job will be submitted
- * @job: job to submit
- *
- * Returns:
- * True if sync is needed.
- */
-bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
-				  struct amdgpu_job *job)
-{
-	struct amdgpu_device *adev = ring->adev;
-	unsigned vmhub = ring->vm_hub;
-	struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
-
-	if (job->vmid == 0)
-		return false;
-
-	if (job->vm_needs_flush || ring->has_compute_vm_bug)
-		return true;
-
-	if (ring->funcs->emit_gds_switch && job->gds_switch_needed)
-		return true;
-
-	if (amdgpu_vmid_had_gpu_reset(adev, &id_mgr->ids[job->vmid]))
-		return true;
-
-	return false;
-}
-
  /**
   * amdgpu_vm_flush - hardware flush the vm
   *
@@ -647,29 +616,31 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
  	unsigned vmhub = ring->vm_hub;
  	struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  	struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
-	bool spm_update_needed = job->spm_update_needed;
-	bool gds_switch_needed = ring->funcs->emit_gds_switch &&
-		job->gds_switch_needed;
-	bool vm_flush_needed = job->vm_needs_flush;
-	bool cleaner_shader_needed = false;
-	bool pasid_mapping_needed = false;
-	struct dma_fence *fence = NULL;
+	bool gds_switch_needed, vm_flush_needed, spm_update_needed,
+	     cleaner_shader_needed, pasid_mapping_needed;
+	struct dma_fence *fence;
  	unsigned int patch;
  	int r;
 

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, · line 634

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -0
+	/* First of all figure out what needs to be done */
  	if (amdgpu_vmid_had_gpu_reset(adev, id)) {
+		need_pipe_sync = true;
  		gds_switch_needed = true;
  		vm_flush_needed = true;
  		pasid_mapping_needed = true;
  		spm_update_needed = true;
+	} else {
+		need_pipe_sync |= ring->has_compute_vm_bug;
+		gds_switch_needed = job->gds_switch_needed;

*[1]:* Should we need to check along with "ring->funcs->emit_gds_switch" ie., "ring->funcs->emit_gds_switch && job->gds_switch_needed;" here?

Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7 -6
+		vm_flush_needed = job->vm_needs_flush;
+		mutex_lock(&id_mgr->lock);
+		if (id->pasid != job->pasid || !id->pasid_mapping ||
+		    !dma_fence_is_signaled(id->pasid_mapping))
+			pasid_mapping_needed = true;
+		mutex_unlock(&id_mgr->lock);
+		spm_update_needed = job->spm_update_needed;
  	}
 
-	mutex_lock(&id_mgr->lock);
-	if (id->pasid != job->pasid || !id->pasid_mapping ||
-	    !dma_fence_is_signaled(id->pasid_mapping))
-		pasid_mapping_needed = true;
-	mutex_unlock(&id_mgr->lock);
-
  	gds_switch_needed &= !!ring->funcs->emit_gds_switch;
  	vm_flush_needed &= !!ring->funcs->emit_vm_flush  &&
  			job->vm_pd_addr != AMDGPU_BO_INVALID_OFFSET;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, · line 658

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
@@ -681,15 +652,17 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
  		&job->base.s_fence->scheduled == isolation->spearhead;
 
  	if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync &&
-	    !cleaner_shader_needed)
+	    !cleaner_shader_needed && !spm_update_needed)
  		return 0;
 
+	/* Then aktually prepare the submission frame */

*[2]:* Could you pls correct the typo?

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, · line 665

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
  	amdgpu_ring_ib_begin(ring);
  	if (ring->funcs->init_cond_exec)
  		patch = amdgpu_ring_init_cond_exec(ring,
  						   ring->cond_exe_gpu_addr);
 
-	if (need_pipe_sync)
+	if (need_pipe_sync || vm_flush_needed || cleaner_shader_needed ||
+	    gds_switch_needed)

*[3]:* Can we check even for *spm_update_needed* alongside the other conditions, that might need Pipeline Sync here?, Cz in an environment where multiple jobs might be accessing or modifying shared data (especially in gang submissions), ensuring that all operations (including SPM updates) are completed before executing further commands, to prevent using stale or invalid data.

Quoted 78 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +14 -13
  		amdgpu_ring_emit_pipeline_sync(ring);
 
  	if (cleaner_shader_needed)
@@ -706,20 +679,31 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
  	if (spm_update_needed && adev->gfx.rlc.funcs->update_spm_vmid)
  		adev->gfx.rlc.funcs->update_spm_vmid(adev, ring, job->vmid);
 
-	if (ring->funcs->emit_gds_switch &&
-	    gds_switch_needed) {
+	if (gds_switch_needed)
  		amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
  					    job->gds_size, job->gws_base,
  					    job->gws_size, job->oa_base,
  					    job->oa_size);
-	}
 
  	if (vm_flush_needed || pasid_mapping_needed || cleaner_shader_needed) {
  		r = amdgpu_fence_emit(ring, &fence, NULL, 0);
  		if (r)
  			return r;
+	} else {
+		fence = NULL;
+	}
+
+	amdgpu_ring_patch_cond_exec(ring, patch);
+
+	/* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
+	if (ring->funcs->emit_switch_buffer) {
+		amdgpu_ring_emit_switch_buffer(ring);
+		amdgpu_ring_emit_switch_buffer(ring);
  	}
 
+	amdgpu_ring_ib_end(ring);
+
+	/* And finally remember what the ring has executed */
  	if (vm_flush_needed) {
  		mutex_lock(&id_mgr->lock);
  		dma_fence_put(id->last_flush);
@@ -749,16 +733,6 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
  		mutex_unlock(&adev->enforce_isolation_mutex);
  	}
  	dma_fence_put(fence);
-
-	amdgpu_ring_patch_cond_exec(ring, patch);
-
-	/* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
-	if (ring->funcs->emit_switch_buffer) {
-		amdgpu_ring_emit_switch_buffer(ring);
-		amdgpu_ring_emit_switch_buffer(ring);
-	}
-
-	amdgpu_ring_ib_end(ring);
  	return 0;
  }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +2 -3
index daa2f9b33620..e9ecdb96bafa 100644
@@ -493,7 +493,8 @@ int amdgpu_vm_validate(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  		       struct ww_acquire_ctx *ticket,
  		       int (*callback)(void *p, struct amdgpu_bo *bo),
  		       void *param);
-int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
+int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
+		    bool need_pipe_sync);
  int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
  			  struct amdgpu_vm *vm, bool immediate);
  int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
@@ -550,8 +551,6 @@ void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size,
  			   uint32_t fragment_size_default, unsigned max_level,
  			   unsigned max_bits);
  int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
-bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
-				  struct amdgpu_job *job);
  void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);
 
  struct amdgpu_task_info *
#3 lore raw

Re: [PATCH 2/2] drm/amdgpu: cleanup amdgpu_vm_flush v3

Am 05.04.25 um 09:32 schrieb SRINIVASAN SHANMUGAM:

Quoted 98 lines · Christian König
Quoted 96 lines

This reverts commit c2cc3648ba517a6c270500b5447d5a1efdad5936. Turned out that this has some negative consequences for some workloads. Instead check if the cleaner shader should run directly.

While at it remove amdgpu_vm_need_pipeline_sync(), we also check again if the VMID has seen a GPU reset since last use and the gds switch setiing can be handled more simply as well.

Also remove some duplicate checks and re-order and document the code.

v2: restructure the while function
v3: fix logic error pointed out by Srini

---

  drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c |  3 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 92 +++++++++-----------------
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  5 +-
  3 files changed, 36 insertions(+), 64 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +0 -0
index 802743efa3b3..ff2ca984279a 100644
@@ -191,8 +191,7 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
       need_ctx_switch = ring->current_ctx != fence_ctx;

      if (ring->funcs->emit_pipeline_sync && job &&
          ((tmp = amdgpu_sync_get_fence(&job->explicit_sync)) || -         need_ctx_switch || amdgpu_vm_need_pipeline_sync(ring, job))) { -
+         (amdgpu_sriov_vf(adev) && need_ctx_switch))) { need_pipe_sync = true;
            if (tmp)

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index b5ddfcbbc9fc..e6d7db8d40cd 100644
@@ -596,37 +596,6 @@ void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
       }
   }

  -/**
- * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job.
- *
- * @ring: ring on which the job will be submitted
- * @job: job to submit
- *
- * Returns:
- * True if sync is needed.
- */

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -22
-bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
-                  struct amdgpu_job *job)
-{
-    struct amdgpu_device *adev = ring->adev;
-    unsigned vmhub = ring->vm_hub;
-    struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
-
-    if (job->vmid == 0)
-        return false;
-
-    if (job->vm_needs_flush || ring->has_compute_vm_bug)
-        return true;
-
-    if (ring->funcs->emit_gds_switch && job->gds_switch_needed)
-        return true;
-
-    if (amdgpu_vmid_had_gpu_reset(adev, &id_mgr->ids[job->vmid]))
-        return true;
-
-    return false;
-}
-

  /**
   * amdgpu_vm_flush - hardware flush the vm
   *

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -7
@@ -647,29 +616,31 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
       unsigned vmhub = ring->vm_hub;
       struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
       struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
-    bool spm_update_needed = job->spm_update_needed;
-    bool gds_switch_needed = ring->funcs->emit_gds_switch &&
-        job->gds_switch_needed;
-    bool vm_flush_needed = job->vm_needs_flush;
-    bool cleaner_shader_needed = false;
-    bool pasid_mapping_needed = false;
-    struct dma_fence *fence = NULL;
+    bool gds_switch_needed, vm_flush_needed, spm_update_needed,
+         cleaner_shader_needed, pasid_mapping_needed;
+    struct dma_fence *fence;
       unsigned int patch;
       int r;

  +    /* First of all figure out what needs to be done */ if (amdgpu_vmid_had_gpu_reset(adev, id)) { +        need_pipe_sync = true;
          gds_switch_needed = true; vm_flush_needed = true; pasid_mapping_needed = true;

Quoted 8 lines
Quoted 4 lines

          spm_update_needed = true;
+    } else {
+        need_pipe_sync |= ring->has_compute_vm_bug; +        gds_switch_needed = job->gds_switch_needed;

*[1]:* Should we need to check along with "ring->funcs->emit_gds_switch" ie., "ring->funcs->emit_gds_switch && job->gds_switch_needed;" here?

No, see below.

Quoted 17 lines
Quoted 17 lines

+        vm_flush_needed = job->vm_needs_flush;
+        mutex_lock(&id_mgr->lock);
+        if (id->pasid != job->pasid || !id->pasid_mapping || +            !dma_fence_is_signaled(id->pasid_mapping)) +            pasid_mapping_needed = true;
+        mutex_unlock(&id_mgr->lock);
+        spm_update_needed = job->spm_update_needed; }
  -    mutex_lock(&id_mgr->lock);
-    if (id->pasid != job->pasid || !id->pasid_mapping || -        !dma_fence_is_signaled(id->pasid_mapping)) -        pasid_mapping_needed = true;
-    mutex_unlock(&id_mgr->lock);
-
      gds_switch_needed &= !!ring->funcs->emit_gds_switch;

That check is here.

Quoted 4 lines
Quoted 4 lines

      vm_flush_needed &= !!ring->funcs->emit_vm_flush  && job->vm_pd_addr != AMDGPU_BO_INVALID_OFFSET;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
@@ -681,15 +652,17 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
           &job->base.s_fence->scheduled == isolation->spearhead;
Quoted 8 lines
Quoted 5 lines

        if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync && -        !cleaner_shader_needed)
+        !cleaner_shader_needed && !spm_update_needed) return 0;
  +    /* Then aktually prepare the submission frame */

*[2]:* Could you pls correct the typo?

Mhm, I thought I already did that.

Ok, going to fix it now.

Quoted 11 lines
Quoted 9 lines

      amdgpu_ring_ib_begin(ring);
      if (ring->funcs->init_cond_exec)
          patch = amdgpu_ring_init_cond_exec(ring, ring->cond_exe_gpu_addr);
  -    if (need_pipe_sync)
+    if (need_pipe_sync || vm_flush_needed || cleaner_shader_needed || +        gds_switch_needed)

Quoted 1 line

*[3]:* Can we check even for *spm_update_needed* alongside the other conditions, that might need Pipeline Sync here?, Cz in an environment where multiple jobs might be accessing or modifying shared data (especially in gang submissions), ensuring that all operations (including SPM updates) are completed before executing further commands, to prevent using stale or invalid data.

No, the SPM update is intentionally left out here since those shouldn't interfere with the pipeline sync.

Regards,
Christian.

Quoted 74 lines
Quoted 74 lines

          amdgpu_ring_emit_pipeline_sync(ring);
        if (cleaner_shader_needed)

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
@@ -706,20 +679,31 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
       if (spm_update_needed && adev->gfx.rlc.funcs->update_spm_vmid)
           adev->gfx.rlc.funcs->update_spm_vmid(adev, ring, job->vmid);

  -    if (ring->funcs->emit_gds_switch &&
-        gds_switch_needed) {
+    if (gds_switch_needed)
          amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base, job->gds_size, job->gws_base, job->gws_size, job->oa_base, job->oa_size);
-    }
        if (vm_flush_needed || pasid_mapping_needed || cleaner_shader_needed) { r = amdgpu_fence_emit(ring, &fence, NULL, 0);
          if (r)
              return r;
+    } else {
+        fence = NULL;
+    }

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7 -0
+
+    amdgpu_ring_patch_cond_exec(ring, patch);
+
+    /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
+    if (ring->funcs->emit_switch_buffer) {
+        amdgpu_ring_emit_switch_buffer(ring);
+        amdgpu_ring_emit_switch_buffer(ring);

      }
  +    amdgpu_ring_ib_end(ring);
+
+    /* And finally remember what the ring has executed */ if (vm_flush_needed) { mutex_lock(&id_mgr->lock); dma_fence_put(id->last_flush);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -10
@@ -749,16 +733,6 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
           mutex_unlock(&adev->enforce_isolation_mutex);
       }
       dma_fence_put(fence);
-
-    amdgpu_ring_patch_cond_exec(ring, patch);
-
-    /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
-    if (ring->funcs->emit_switch_buffer) {
-        amdgpu_ring_emit_switch_buffer(ring);
-        amdgpu_ring_emit_switch_buffer(ring);
-    }
-
-    amdgpu_ring_ib_end(ring);
       return 0;
   }

  diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h index daa2f9b33620..e9ecdb96bafa 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +2 -3
@@ -493,7 +493,8 @@ int amdgpu_vm_validate(struct amdgpu_device *adev, struct amdgpu_vm *vm,
                  struct ww_acquire_ctx *ticket,
                  int (*callback)(void *p, struct amdgpu_bo *bo),
                  void *param);
-int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
+int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
+            bool need_pipe_sync);
   int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
                 struct amdgpu_vm *vm, bool immediate);
   int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
@@ -550,8 +551,6 @@ void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size,
                  uint32_t fragment_size_default, unsigned max_level,
                  unsigned max_bits);
   int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
-bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
-                  struct amdgpu_job *job);
   void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);

    struct amdgpu_task_info *

#4 lore raw

Re: [PATCH 2/2] drm/amdgpu: cleanup amdgpu_vm_flush v3

Quoted 220 lines · Christian König

Am 05.04.25 um 09:32 schrieb SRINIVASAN SHANMUGAM:

Quoted 96 lines · Christian König
Quoted 95 lines

This reverts commit c2cc3648ba517a6c270500b5447d5a1efdad5936. Turned out that this has some negative consequences for some workloads. Instead check if the cleaner shader should run directly.

While at it remove amdgpu_vm_need_pipeline_sync(), we also check again if the VMID has seen a GPU reset since last use and the gds switch setiing can be handled more simply as well.

Also remove some duplicate checks and re-order and document the code.

v2: restructure the while function
v3: fix logic error pointed out by Srini

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c |  3 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 92 +++++++++-----------------
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  5 +-
   3 files changed, 36 insertions(+), 64 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +1 -2
index 802743efa3b3..ff2ca984279a 100644
@@ -191,8 +191,7 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
       need_ctx_switch = ring->current_ctx != fence_ctx;
       if (ring->funcs->emit_pipeline_sync && job &&
           ((tmp = amdgpu_sync_get_fence(&job->explicit_sync)) ||
-         need_ctx_switch || amdgpu_vm_need_pipeline_sync(ring, job))) {
-
+         (amdgpu_sriov_vf(adev) && need_ctx_switch))) {
           need_pipe_sync = true;
             if (tmp)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -37
index b5ddfcbbc9fc..e6d7db8d40cd 100644
@@ -596,37 +596,6 @@ void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
       }
   }
   -/**
- * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job.
- *
- * @ring: ring on which the job will be submitted
- * @job: job to submit
- *
- * Returns:
- * True if sync is needed.
- */
-bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
-                  struct amdgpu_job *job)
-{
-    struct amdgpu_device *adev = ring->adev;
-    unsigned vmhub = ring->vm_hub;
-    struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
-
-    if (job->vmid == 0)
-        return false;
-
-    if (job->vm_needs_flush || ring->has_compute_vm_bug)
-        return true;
-
-    if (ring->funcs->emit_gds_switch && job->gds_switch_needed)
-        return true;
-
-    if (amdgpu_vmid_had_gpu_reset(adev, &id_mgr->ids[job->vmid]))
-        return true;
-
-    return false;
-}
-
   /**
    * amdgpu_vm_flush - hardware flush the vm
    *
@@ -647,29 +616,31 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
       unsigned vmhub = ring->vm_hub;
       struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
       struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
-    bool spm_update_needed = job->spm_update_needed;
-    bool gds_switch_needed = ring->funcs->emit_gds_switch &&
-        job->gds_switch_needed;
-    bool vm_flush_needed = job->vm_needs_flush;
-    bool cleaner_shader_needed = false;
-    bool pasid_mapping_needed = false;
-    struct dma_fence *fence = NULL;
+    bool gds_switch_needed, vm_flush_needed, spm_update_needed,
+         cleaner_shader_needed, pasid_mapping_needed;
+    struct dma_fence *fence;
       unsigned int patch;
       int r;
   +    /* First of all figure out what needs to be done */
       if (amdgpu_vmid_had_gpu_reset(adev, id)) {
+        need_pipe_sync = true;
           gds_switch_needed = true;
           vm_flush_needed = true;
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, · line 634

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -0
           pasid_mapping_needed = true;
           spm_update_needed = true;
+    } else {
+        need_pipe_sync |= ring->has_compute_vm_bug;
+        gds_switch_needed = job->gds_switch_needed;

*[1]:* Should we need to check along with "ring->funcs->emit_gds_switch" ie., "ring->funcs->emit_gds_switch && job->gds_switch_needed;" here?

No, see below.

Quoted 16 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -0
+        vm_flush_needed = job->vm_needs_flush;
+        mutex_lock(&id_mgr->lock);
+        if (id->pasid != job->pasid || !id->pasid_mapping ||
+            !dma_fence_is_signaled(id->pasid_mapping))
+            pasid_mapping_needed = true;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, · line 644

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -5
+        mutex_unlock(&id_mgr->lock);
+        spm_update_needed = job->spm_update_needed;
       }
   -    mutex_lock(&id_mgr->lock);
-    if (id->pasid != job->pasid || !id->pasid_mapping ||
-        !dma_fence_is_signaled(id->pasid_mapping))
-        pasid_mapping_needed = true;
-    mutex_unlock(&id_mgr->lock);
-
       gds_switch_needed &= !!ring->funcs->emit_gds_switch;

That check is here.

Quoted 11 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, · line 656

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
       vm_flush_needed &= !!ring->funcs->emit_vm_flush  &&
               job->vm_pd_addr != AMDGPU_BO_INVALID_OFFSET;
@@ -681,15 +652,17 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
           &job->base.s_fence->scheduled == isolation->spearhead;
         if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync &&
-        !cleaner_shader_needed)
+        !cleaner_shader_needed && !spm_update_needed)
           return 0;
   +    /* Then aktually prepare the submission frame */

*[2]:* Could you pls correct the typo?

Mhm, I thought I already did that.

Ok, going to fix it now.

Quoted 10 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, · line 665

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
       amdgpu_ring_ib_begin(ring);
       if (ring->funcs->init_cond_exec)
           patch = amdgpu_ring_init_cond_exec(ring,
                              ring->cond_exe_gpu_addr);
   -    if (need_pipe_sync)
+    if (need_pipe_sync || vm_flush_needed || cleaner_shader_needed ||
+        gds_switch_needed)

*[3]:* Can we check even for *spm_update_needed* alongside the other conditions, that might need Pipeline Sync here?, Cz in an environment where multiple jobs might be accessing or modifying shared data (especially in gang submissions), ensuring that all operations (including SPM updates) are completed before executing further commands, to prevent using stale or invalid data.

No, the SPM update is intentionally left out here since those shouldn't interfere with the pipeline sync.

Regards,
Christian.

Quoted 65 lines
Quoted 65 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +13 -12
           amdgpu_ring_emit_pipeline_sync(ring);
         if (cleaner_shader_needed)
@@ -706,20 +679,31 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
       if (spm_update_needed && adev->gfx.rlc.funcs->update_spm_vmid)
           adev->gfx.rlc.funcs->update_spm_vmid(adev, ring, job->vmid);
   -    if (ring->funcs->emit_gds_switch &&
-        gds_switch_needed) {
+    if (gds_switch_needed)
           amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
                           job->gds_size, job->gws_base,
                           job->gws_size, job->oa_base,
                           job->oa_size);
-    }
         if (vm_flush_needed || pasid_mapping_needed || cleaner_shader_needed) {
           r = amdgpu_fence_emit(ring, &fence, NULL, 0);
           if (r)
               return r;
+    } else {
+        fence = NULL;
+    }
+
+    amdgpu_ring_patch_cond_exec(ring, patch);
+
+    /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
+    if (ring->funcs->emit_switch_buffer) {
+        amdgpu_ring_emit_switch_buffer(ring);
+        amdgpu_ring_emit_switch_buffer(ring);
       }
   +    amdgpu_ring_ib_end(ring);
+
+    /* And finally remember what the ring has executed */
       if (vm_flush_needed) {
           mutex_lock(&id_mgr->lock);
           dma_fence_put(id->last_flush);
@@ -749,16 +733,6 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
           mutex_unlock(&adev->enforce_isolation_mutex);
       }
       dma_fence_put(fence);
-
-    amdgpu_ring_patch_cond_exec(ring, patch);
-
-    /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
-    if (ring->funcs->emit_switch_buffer) {
-        amdgpu_ring_emit_switch_buffer(ring);
-        amdgpu_ring_emit_switch_buffer(ring);
-    }
-
-    amdgpu_ring_ib_end(ring);
       return 0;
   }
   diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h

index daa2f9b33620..e9ecdb96bafa 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +2 -1
@@ -493,7 +493,8 @@ int amdgpu_vm_validate(struct amdgpu_device *adev, struct amdgpu_vm *vm,
                  struct ww_acquire_ctx *ticket,
                  int (*callback)(void *p, struct amdgpu_bo *bo),
                  void *param);
-int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
+int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
+            bool need_pipe_sync);
   int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
                 struct amdgpu_vm *vm, bool immediate);
   int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size, · line 555

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -2
@@ -550,8 +551,6 @@ void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size,
                  uint32_t fragment_size_default, unsigned max_level,
                  unsigned max_bits);
   int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
-bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
-                  struct amdgpu_job *job);
   void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);
     struct amdgpu_task_info *

Ok thanks for your feedbacks, based on this with that comment fixed, this patch is:

Reviewed-by on v1 1/2
#5 lore raw
In reply to Christian König
Quoted 36 lines · Christian König

Otherwise triggering sysfs multiple times without other submissions in between only runs the shader once.

v2: add some comment
v3: re-add missing cast

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 14 +++++++++++---
  1 file changed, 11 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +4 -3
index f64675b2ab75..76237961a08f 100644
@@ -1439,9 +1439,11 @@ static int amdgpu_gfx_run_cleaner_shader_job(struct amdgpu_ring *ring)
  	struct amdgpu_device *adev = ring->adev;
  	struct drm_gpu_scheduler *sched = &ring->sched;
  	struct drm_sched_entity entity;
+	static atomic_t counter;
  	struct dma_fence *f;
  	struct amdgpu_job *job;
  	struct amdgpu_ib *ib;
+	void * owner;
  	int i, r;
 
  	/* Initialize the scheduler entity */
@@ -1452,9 +1454,15 @@ static int amdgpu_gfx_run_cleaner_shader_job(struct amdgpu_ring *ring)
  		goto err;
  	}
 
-	r = amdgpu_job_alloc_with_ib(ring->adev, &entity, NULL,
-				     64, 0,
-				     &job);
+	/*
+	 * Use some unique dummy value as the owner to make sure we execute

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c static int amdgpu_gfx_run_cleaner_shader_job(struct amdgpu_ring *ring) · line 1468

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +7 -0
+	 * the cleaner shader on each submission. The value just need to change
+	 * for each submission and is otherwise meaningless.
+	 */
+	owner = (void *)(unsigned long)atomic_inc_return(&counter),
+
+	r = amdgpu_job_alloc_with_ib(ring->adev, &entity, owner,
+				     64, 0, &job);
  	if (r)
  		goto err;