Skip to contentSkip to conversation

drm/amdgpu: use the last IB as gang leader v2

PATCH In review Christian König 4 participants 6 messages Nov 15, 2022

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

PATCH 1/1 drm/amdgpu: use the last IB as gang leader v2

It turned out that not the last IB specified is the gang leader, but instead the last job allocated.

This is a bit unfortunate and not very intuitive for the CS interface, so try to fix this.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 23 ++++++++++++++++-------
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h |  1 +
 2 files changed, 17 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +16 -7
index 1bbd39b3b0fc..fbdf139cf497 100644
@@ -109,6 +109,7 @@ static int amdgpu_cs_p1_ib(struct amdgpu_cs_parser *p,
 		return r;
 
 	++(num_ibs[r]);
+	p->gang_leader_idx = r;
 	return 0;
 }
 
@@ -300,7 +301,7 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,
 		if (ret)
 			goto free_all_kdata;
 	}
-	p->gang_leader = p->jobs[p->gang_size - 1];
+	p->gang_leader = p->jobs[p->gang_leader_idx];
 
 	if (p->ctx->vram_lost_counter != p->gang_leader->vram_lost_counter) {
 		ret = -ECANCELED;
@@ -1194,16 +1195,18 @@ static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
 			return r;
 	}
 
-	for (i = 0; i < p->gang_size - 1; ++i) {
+	for (i = 0; i < p->gang_size; ++i) {
+		if (p->jobs[i] == leader)
+			continue;
+
 		r = amdgpu_sync_clone(&leader->sync, &p->jobs[i]->sync);
 		if (r)
 			return r;
 	}
 
-	r = amdgpu_ctx_wait_prev_fence(p->ctx, p->entities[p->gang_size - 1]);
+	r = amdgpu_ctx_wait_prev_fence(p->ctx, p->entities[p->gang_leader_idx]);
 	if (r && r != -ERESTARTSYS)
 		DRM_ERROR("amdgpu_ctx_wait_prev_fence failed.\n");
-
 	return r;
 }
 
@@ -1237,9 +1240,12 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
 	for (i = 0; i < p->gang_size; ++i)
 		drm_sched_job_arm(&p->jobs[i]->base);
 
-	for (i = 0; i < (p->gang_size - 1); ++i) {
+	for (i = 0; i < p->gang_size; ++i) {
 		struct dma_fence *fence;
 
+		if (p->jobs[i] == leader)
+			continue;
+
 		fence = &p->jobs[i]->base.s_fence->scheduled;
 		r = amdgpu_sync_fence(&leader->sync, fence);
 		if (r)
@@ -1275,7 +1281,10 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
 	list_for_each_entry(e, &p->validated, tv.head) {
 
 		/* Everybody except for the gang leader uses READ */
-		for (i = 0; i < (p->gang_size - 1); ++i) {
+		for (i = 0; i < p->gang_size; ++i) {
+			if (p->jobs[i] == leader)
+				continue;
+
 			dma_resv_add_fence(e->tv.bo->base.resv,
 					   &p->jobs[i]->base.s_fence->finished,
 					   DMA_RESV_USAGE_READ);
@@ -1285,7 +1294,7 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
 		e->tv.num_shared = 0;
 	}
 
-	seq = amdgpu_ctx_add_fence(p->ctx, p->entities[p->gang_size - 1],
+	seq = amdgpu_ctx_add_fence(p->ctx, p->entities[p->gang_leader_idx],
 				   p->fence);
 	amdgpu_cs_post_dependencies(p);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h +1 -0
index cbaa19b2b8a3..f80adf9069ec 100644
@@ -54,6 +54,7 @@ struct amdgpu_cs_parser {
 
 	/* scheduler job objects */
 	unsigned int		gang_size;
+	unsigned int		gang_leader_idx;
 	struct drm_sched_entity	*entities[AMDGPU_CS_GANG_SIZE];
 	struct amdgpu_job	*jobs[AMDGPU_CS_GANG_SIZE];
 	struct amdgpu_job	*gang_leader;
Signature · 1 line
2.34.1
maintainer
#1 lore raw

Am 15.11.22 um 10:42 schrieb Christian König:

Quoted 5 lines

It turned out that not the last IB specified is the gang leader, but instead the last job allocated.

This is a bit unfortunate and not very intuitive for the CS interface, so try to fix this.

Alex could you take a look at this? I would really like to get this into the next -rc.

Thanks,
Christian.

Quoted 97 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 23 ++++++++++++++++-------
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h |  1 +
  2 files changed, 17 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +16 -7
index 1bbd39b3b0fc..fbdf139cf497 100644
@@ -109,6 +109,7 @@ static int amdgpu_cs_p1_ib(struct amdgpu_cs_parser *p,
  		return r;
 
  	++(num_ibs[r]);
+	p->gang_leader_idx = r;
  	return 0;
  }
 
@@ -300,7 +301,7 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,
  		if (ret)
  			goto free_all_kdata;
  	}
-	p->gang_leader = p->jobs[p->gang_size - 1];
+	p->gang_leader = p->jobs[p->gang_leader_idx];
 
  	if (p->ctx->vram_lost_counter != p->gang_leader->vram_lost_counter) {
  		ret = -ECANCELED;
@@ -1194,16 +1195,18 @@ static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  			return r;
  	}
 
-	for (i = 0; i < p->gang_size - 1; ++i) {
+	for (i = 0; i < p->gang_size; ++i) {
+		if (p->jobs[i] == leader)
+			continue;
+
  		r = amdgpu_sync_clone(&leader->sync, &p->jobs[i]->sync);
  		if (r)
  			return r;
  	}
 
-	r = amdgpu_ctx_wait_prev_fence(p->ctx, p->entities[p->gang_size - 1]);
+	r = amdgpu_ctx_wait_prev_fence(p->ctx, p->entities[p->gang_leader_idx]);
  	if (r && r != -ERESTARTSYS)
  		DRM_ERROR("amdgpu_ctx_wait_prev_fence failed.\n");
-
  	return r;
  }
 
@@ -1237,9 +1240,12 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  	for (i = 0; i < p->gang_size; ++i)
  		drm_sched_job_arm(&p->jobs[i]->base);
 
-	for (i = 0; i < (p->gang_size - 1); ++i) {
+	for (i = 0; i < p->gang_size; ++i) {
  		struct dma_fence *fence;
 
+		if (p->jobs[i] == leader)
+			continue;
+
  		fence = &p->jobs[i]->base.s_fence->scheduled;
  		r = amdgpu_sync_fence(&leader->sync, fence);
  		if (r)
@@ -1275,7 +1281,10 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  	list_for_each_entry(e, &p->validated, tv.head) {
 
  		/* Everybody except for the gang leader uses READ */
-		for (i = 0; i < (p->gang_size - 1); ++i) {
+		for (i = 0; i < p->gang_size; ++i) {
+			if (p->jobs[i] == leader)
+				continue;
+
  			dma_resv_add_fence(e->tv.bo->base.resv,
  					   &p->jobs[i]->base.s_fence->finished,
  					   DMA_RESV_USAGE_READ);
@@ -1285,7 +1294,7 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  		e->tv.num_shared = 0;
  	}
 
-	seq = amdgpu_ctx_add_fence(p->ctx, p->entities[p->gang_size - 1],
+	seq = amdgpu_ctx_add_fence(p->ctx, p->entities[p->gang_leader_idx],
  				   p->fence);
  	amdgpu_cs_post_dependencies(p);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h +1 -0
index cbaa19b2b8a3..f80adf9069ec 100644
@@ -54,6 +54,7 @@ struct amdgpu_cs_parser {
 
  	/* scheduler job objects */
  	unsigned int		gang_size;
+	unsigned int		gang_leader_idx;
  	struct drm_sched_entity	*entities[AMDGPU_CS_GANG_SIZE];
  	struct amdgpu_job	*jobs[AMDGPU_CS_GANG_SIZE];
  	struct amdgpu_job	*gang_leader;
Tested-by Acked-by
#2 lore raw

I can confirm this patch solves an issue with gang submit. It removes the necessity to sort IBs by IP type in user space. Now only the IP type of the last IB matters, as was intended.

Quoted 129 lines · Christian König

Am 15.11.22 um 10:42 schrieb Christian König:

Quoted 5 lines

It turned out that not the last IB specified is the gang leader, but instead the last job allocated.

This is a bit unfortunate and not very intuitive for the CS interface, so try to fix this.

Alex could you take a look at this? I would really like to get this into
the next -rc.

Thanks,
Christian.

Quoted 114 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 23 ++++++++++++++++----- --
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h |  1 +

  2 files changed, 17 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
index 1bbd39b3b0fc..fbdf139cf497 100644
@@ -109,6 +109,7 @@ static int amdgpu_cs_p1_ib(struct
 amdgpu_cs_parser *p,
                 return r;


        ++(num_ibs[r]);
+       p->gang_leader_idx = r;
        return 0;
  }

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -1
@@ -300,7 +301,7 @@ static int amdgpu_cs_pass1(struct
 amdgpu_cs_parser *p,
                 if (ret)
                         goto free_all_kdata;
         }
-       p->gang_leader = p->jobs[p->gang_size - 1];
+       p->gang_leader = p->jobs[p->gang_leader_idx];


        if (p->ctx->vram_lost_counter != p->gang_leader-

Quoted 1 line

vram_lost_counter) {

                ret = -ECANCELED;

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
@@ -1194,16 +1195,18 @@ static int amdgpu_cs_sync_rings(struct
 amdgpu_cs_parser *p)
                         return r;
         }


-       for (i = 0; i < p->gang_size - 1; ++i) {
+       for (i = 0; i < p->gang_size; ++i) {
+               if (p->jobs[i] == leader)
+                       continue;
+
                r = amdgpu_sync_clone(&leader->sync, &p->jobs[i]-

Quoted 1 line

sync);

                if (r)
                        return r;
        }

-       r = amdgpu_ctx_wait_prev_fence(p->ctx, p->entities[p-

Quoted 1 line

gang_size - 1]);

+       r = amdgpu_ctx_wait_prev_fence(p->ctx, p->entities[p-

Quoted 1 line

gang_leader_idx]);

        if (r && r != -ERESTARTSYS) DRM_ERROR("amdgpu_ctx_wait_prev_fence failed.\n"); -
        return r;
  }

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
@@ -1237,9 +1240,12 @@ static int amdgpu_cs_submit(struct
 amdgpu_cs_parser *p,
         for (i = 0; i < p->gang_size; ++i)
                 drm_sched_job_arm(&p->jobs[i]->base);


-       for (i = 0; i < (p->gang_size - 1); ++i) { +       for (i = 0; i < p->gang_size; ++i) {
                struct dma_fence *fence;

+               if (p->jobs[i] == leader)
+                       continue;
+
                fence = &p->jobs[i]->base.s_fence->scheduled; r = amdgpu_sync_fence(&leader->sync, fence); if (r)

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
@@ -1275,7 +1281,10 @@ static int amdgpu_cs_submit(struct
 amdgpu_cs_parser *p,
         list_for_each_entry(e, &p->validated, tv.head) {


                /* Everybody except for the gang leader uses READ */
-               for (i = 0; i < (p->gang_size - 1); ++i) { +               for (i = 0; i < p->gang_size; ++i) {
+                       if (p->jobs[i] == leader)
+                               continue;
+
                        dma_resv_add_fence(e->tv.bo->base.resv, &p->jobs[i]-

Quoted 1 line

base.s_fence->finished,

                                           DMA_RESV_USAGE_READ);

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
@@ -1285,7 +1294,7 @@ static int amdgpu_cs_submit(struct
 amdgpu_cs_parser *p,
                 e->tv.num_shared = 0;
         }


-       seq = amdgpu_ctx_add_fence(p->ctx, p->entities[p->gang_size
- 1],
+       seq = amdgpu_ctx_add_fence(p->ctx, p->entities[p-

Quoted 1 line

gang_leader_idx],

                                   p->fence);
        amdgpu_cs_post_dependencies(p);

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h +0 -0
index cbaa19b2b8a3..f80adf9069ec 100644
@@ -54,6 +54,7 @@ struct amdgpu_cs_parser {


        /* scheduler job objects */
        unsigned int            gang_size;
+       unsigned int            gang_leader_idx; struct drm_sched_entity *entities[AMDGPU_CS_GANG_SIZE]; struct amdgpu_job       *jobs[AMDGPU_CS_GANG_SIZE]; struct amdgpu_job       *gang_leader;

Reviewed-by
#3 lore raw
In reply to Christian König
Quoted 112 lines · Christian König

Am 15.11.22 um 10:42 schrieb Christian König:

Quoted 5 lines

It turned out that not the last IB specified is the gang leader, but instead the last job allocated.

This is a bit unfortunate and not very intuitive for the CS interface, so try to fix this.

Alex could you take a look at this? I would really like to get this into the next -rc.

Thanks,
Christian.

Quoted 97 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 23 ++++++++++++++++-------
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h |  1 +
  2 files changed, 17 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +16 -7
index 1bbd39b3b0fc..fbdf139cf497 100644
@@ -109,6 +109,7 @@ static int amdgpu_cs_p1_ib(struct amdgpu_cs_parser *p,
              return r;
 
      ++(num_ibs[r]);
+     p->gang_leader_idx = r;
      return 0;
  }
 
@@ -300,7 +301,7 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,
              if (ret)
                      goto free_all_kdata;
      }
-     p->gang_leader = p->jobs[p->gang_size - 1];
+     p->gang_leader = p->jobs[p->gang_leader_idx];
 
      if (p->ctx->vram_lost_counter != p->gang_leader->vram_lost_counter) {
              ret = -ECANCELED;
@@ -1194,16 +1195,18 @@ static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
                      return r;
      }
 
-     for (i = 0; i < p->gang_size - 1; ++i) {
+     for (i = 0; i < p->gang_size; ++i) {
+             if (p->jobs[i] == leader)
+                     continue;
+
              r = amdgpu_sync_clone(&leader->sync, &p->jobs[i]->sync);
              if (r)
                      return r;
      }
 
-     r = amdgpu_ctx_wait_prev_fence(p->ctx, p->entities[p->gang_size - 1]);
+     r = amdgpu_ctx_wait_prev_fence(p->ctx, p->entities[p->gang_leader_idx]);
      if (r && r != -ERESTARTSYS)
              DRM_ERROR("amdgpu_ctx_wait_prev_fence failed.\n");
-
      return r;
  }
 
@@ -1237,9 +1240,12 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
      for (i = 0; i < p->gang_size; ++i)
              drm_sched_job_arm(&p->jobs[i]->base);
 
-     for (i = 0; i < (p->gang_size - 1); ++i) {
+     for (i = 0; i < p->gang_size; ++i) {
              struct dma_fence *fence;
 
+             if (p->jobs[i] == leader)
+                     continue;
+
              fence = &p->jobs[i]->base.s_fence->scheduled;
              r = amdgpu_sync_fence(&leader->sync, fence);
              if (r)
@@ -1275,7 +1281,10 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
      list_for_each_entry(e, &p->validated, tv.head) {
 
              /* Everybody except for the gang leader uses READ */
-             for (i = 0; i < (p->gang_size - 1); ++i) {
+             for (i = 0; i < p->gang_size; ++i) {
+                     if (p->jobs[i] == leader)
+                             continue;
+
                      dma_resv_add_fence(e->tv.bo->base.resv,
                                         &p->jobs[i]->base.s_fence->finished,
                                         DMA_RESV_USAGE_READ);
@@ -1285,7 +1294,7 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
              e->tv.num_shared = 0;
      }
 
-     seq = amdgpu_ctx_add_fence(p->ctx, p->entities[p->gang_size - 1],
+     seq = amdgpu_ctx_add_fence(p->ctx, p->entities[p->gang_leader_idx],
                                 p->fence);
      amdgpu_cs_post_dependencies(p);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h +1 -0
index cbaa19b2b8a3..f80adf9069ec 100644
@@ -54,6 +54,7 @@ struct amdgpu_cs_parser {
 
      /* scheduler job objects */
      unsigned int            gang_size;
+     unsigned int            gang_leader_idx;
      struct drm_sched_entity *entities[AMDGPU_CS_GANG_SIZE];
      struct amdgpu_job       *jobs[AMDGPU_CS_GANG_SIZE];
      struct amdgpu_job       *gang_leader;
#4 lore raw

Would be good to add a Fixes tag.

Alex

Quoted 117 lines · Alex Deucher
Quoted 112 lines · Christian König

Am 15.11.22 um 10:42 schrieb Christian König:

Quoted 5 lines

It turned out that not the last IB specified is the gang leader, but instead the last job allocated.

This is a bit unfortunate and not very intuitive for the CS interface, so try to fix this.

Alex could you take a look at this? I would really like to get this into the next -rc.

Thanks,
Christian.

Quoted 97 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 23 ++++++++++++++++-------
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h |  1 +
  2 files changed, 17 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +16 -7
index 1bbd39b3b0fc..fbdf139cf497 100644
@@ -109,6 +109,7 @@ static int amdgpu_cs_p1_ib(struct amdgpu_cs_parser *p,
              return r;
 
      ++(num_ibs[r]);
+     p->gang_leader_idx = r;
      return 0;
  }
 
@@ -300,7 +301,7 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,
              if (ret)
                      goto free_all_kdata;
      }
-     p->gang_leader = p->jobs[p->gang_size - 1];
+     p->gang_leader = p->jobs[p->gang_leader_idx];
 
      if (p->ctx->vram_lost_counter != p->gang_leader->vram_lost_counter) {
              ret = -ECANCELED;
@@ -1194,16 +1195,18 @@ static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
                      return r;
      }
 
-     for (i = 0; i < p->gang_size - 1; ++i) {
+     for (i = 0; i < p->gang_size; ++i) {
+             if (p->jobs[i] == leader)
+                     continue;
+
              r = amdgpu_sync_clone(&leader->sync, &p->jobs[i]->sync);
              if (r)
                      return r;
      }
 
-     r = amdgpu_ctx_wait_prev_fence(p->ctx, p->entities[p->gang_size - 1]);
+     r = amdgpu_ctx_wait_prev_fence(p->ctx, p->entities[p->gang_leader_idx]);
      if (r && r != -ERESTARTSYS)
              DRM_ERROR("amdgpu_ctx_wait_prev_fence failed.\n");
-
      return r;
  }
 
@@ -1237,9 +1240,12 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
      for (i = 0; i < p->gang_size; ++i)
              drm_sched_job_arm(&p->jobs[i]->base);
 
-     for (i = 0; i < (p->gang_size - 1); ++i) {
+     for (i = 0; i < p->gang_size; ++i) {
              struct dma_fence *fence;
 
+             if (p->jobs[i] == leader)
+                     continue;
+
              fence = &p->jobs[i]->base.s_fence->scheduled;
              r = amdgpu_sync_fence(&leader->sync, fence);
              if (r)
@@ -1275,7 +1281,10 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
      list_for_each_entry(e, &p->validated, tv.head) {
 
              /* Everybody except for the gang leader uses READ */
-             for (i = 0; i < (p->gang_size - 1); ++i) {
+             for (i = 0; i < p->gang_size; ++i) {
+                     if (p->jobs[i] == leader)
+                             continue;
+
                      dma_resv_add_fence(e->tv.bo->base.resv,
                                         &p->jobs[i]->base.s_fence->finished,
                                         DMA_RESV_USAGE_READ);
@@ -1285,7 +1294,7 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
              e->tv.num_shared = 0;
      }
 
-     seq = amdgpu_ctx_add_fence(p->ctx, p->entities[p->gang_size - 1],
+     seq = amdgpu_ctx_add_fence(p->ctx, p->entities[p->gang_leader_idx],
                                 p->fence);
      amdgpu_cs_post_dependencies(p);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h +1 -0
index cbaa19b2b8a3..f80adf9069ec 100644
@@ -54,6 +54,7 @@ struct amdgpu_cs_parser {
 
      /* scheduler job objects */
      unsigned int            gang_size;
+     unsigned int            gang_leader_idx;
      struct drm_sched_entity *entities[AMDGPU_CS_GANG_SIZE];
      struct amdgpu_job       *jobs[AMDGPU_CS_GANG_SIZE];
      struct amdgpu_job       *gang_leader;
#5 lore raw

I wanted to push this directly to drm-misc-fixes.

But going to add one in case someone tries to backport the gang submit changes.

Regards,
Christian.

Am 15.11.22 um 15:10 schrieb Alex Deucher:

Quoted 117 lines

Would be good to add a Fixes tag.

Alex

Quoted 112 lines · Alex Deucher
Quoted 108 lines · Christian König

Am 15.11.22 um 10:42 schrieb Christian König:

Quoted 5 lines

It turned out that not the last IB specified is the gang leader, but instead the last job allocated.

This is a bit unfortunate and not very intuitive for the CS interface, so try to fix this.

Alex could you take a look at this? I would really like to get this into the next -rc.

Thanks,
Christian.

Quoted 96 lines
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 23 ++++++++++++++++-------
   drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h |  1 +
   2 files changed, 17 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +16 -7
index 1bbd39b3b0fc..fbdf139cf497 100644
@@ -109,6 +109,7 @@ static int amdgpu_cs_p1_ib(struct amdgpu_cs_parser *p,
               return r;
 
       ++(num_ibs[r]);
+     p->gang_leader_idx = r;
       return 0;
   }
 
@@ -300,7 +301,7 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,
               if (ret)
                       goto free_all_kdata;
       }
-     p->gang_leader = p->jobs[p->gang_size - 1];
+     p->gang_leader = p->jobs[p->gang_leader_idx];
 
       if (p->ctx->vram_lost_counter != p->gang_leader->vram_lost_counter) {
               ret = -ECANCELED;
@@ -1194,16 +1195,18 @@ static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
                       return r;
       }
 
-     for (i = 0; i < p->gang_size - 1; ++i) {
+     for (i = 0; i < p->gang_size; ++i) {
+             if (p->jobs[i] == leader)
+                     continue;
+
               r = amdgpu_sync_clone(&leader->sync, &p->jobs[i]->sync);
               if (r)
                       return r;
       }
 
-     r = amdgpu_ctx_wait_prev_fence(p->ctx, p->entities[p->gang_size - 1]);
+     r = amdgpu_ctx_wait_prev_fence(p->ctx, p->entities[p->gang_leader_idx]);
       if (r && r != -ERESTARTSYS)
               DRM_ERROR("amdgpu_ctx_wait_prev_fence failed.\n");
-
       return r;
   }
 
@@ -1237,9 +1240,12 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
       for (i = 0; i < p->gang_size; ++i)
               drm_sched_job_arm(&p->jobs[i]->base);
 
-     for (i = 0; i < (p->gang_size - 1); ++i) {
+     for (i = 0; i < p->gang_size; ++i) {
               struct dma_fence *fence;
 
+             if (p->jobs[i] == leader)
+                     continue;
+
               fence = &p->jobs[i]->base.s_fence->scheduled;
               r = amdgpu_sync_fence(&leader->sync, fence);
               if (r)
@@ -1275,7 +1281,10 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
       list_for_each_entry(e, &p->validated, tv.head) {
 
               /* Everybody except for the gang leader uses READ */
-             for (i = 0; i < (p->gang_size - 1); ++i) {
+             for (i = 0; i < p->gang_size; ++i) {
+                     if (p->jobs[i] == leader)
+                             continue;
+
                       dma_resv_add_fence(e->tv.bo->base.resv,
                                          &p->jobs[i]->base.s_fence->finished,
                                          DMA_RESV_USAGE_READ);
@@ -1285,7 +1294,7 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
               e->tv.num_shared = 0;
       }
 
-     seq = amdgpu_ctx_add_fence(p->ctx, p->entities[p->gang_size - 1],
+     seq = amdgpu_ctx_add_fence(p->ctx, p->entities[p->gang_leader_idx],
                                  p->fence);
       amdgpu_cs_post_dependencies(p);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h +1 -0
index cbaa19b2b8a3..f80adf9069ec 100644
@@ -54,6 +54,7 @@ struct amdgpu_cs_parser {
 
       /* scheduler job objects */
       unsigned int            gang_size;
+     unsigned int            gang_leader_idx;
       struct drm_sched_entity *entities[AMDGPU_CS_GANG_SIZE];
       struct amdgpu_job       *jobs[AMDGPU_CS_GANG_SIZE];
       struct amdgpu_job       *gang_leader;