<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    Am 07.03.22 um 16:40 schrieb Andrey Grodzovsky:<br>
    <blockquote type="cite" cite="mid:6174d4b6-7a02-ffef-ce68-ca2f13dae337@amd.com">
      
      <p>On 2022-03-05 13:40, Christian König wrote:<br>
      </p>
      <blockquote type="cite" cite="mid:5c573f4d-0fa8-5c01-67fe-c33a79a6f89c@gmail.com">Am
        04.03.22 um 18:10 schrieb Andrey Grodzovsky: <br>
        <blockquote type="cite"> <br>
          On 2022-03-03 03:23, Christian König wrote: <br>
          <blockquote type="cite">Allows submitting jobs as gang which
            needs to run on multiple <br>
            engines at the same time. <br>
            <br>
            Basic idea is that we have a global gang submit fence
            representing when the <br>
            gang leader is finally pushed to run on the hardware last. <br>
            <br>
            Jobs submitted as gang are never re-submitted in case of a
            GPU reset since this <br>
            won't work and will just deadlock the hardware immediately
            again. <br>
            <br>
            Signed-off-by: Christian König <a class="moz-txt-link-rfc2396E" href="mailto:christian.koenig@amd.com" moz-do-not-send="true"><christian.koenig@amd.com></a>
            <br>
            --- <br>
              drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  3 ++ <br>
              drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 34
            ++++++++++++++++++++++ <br>
              drivers/gpu/drm/amd/amdgpu/amdgpu_job.c    | 28
            ++++++++++++++++-- <br>
              drivers/gpu/drm/amd/amdgpu/amdgpu_job.h    |  3 ++ <br>
              4 files changed, 66 insertions(+), 2 deletions(-) <br>
            <br>
            diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h
            b/drivers/gpu/drm/amd/amdgpu/amdgpu.h <br>
            index 7f447ed7a67f..a664d43d7502 100644 <br>
            --- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h <br>
            +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h <br>
            @@ -852,6 +852,7 @@ struct amdgpu_device { <br>
                  u64                fence_context; <br>
                  unsigned            num_rings; <br>
                  struct amdgpu_ring        *rings[AMDGPU_MAX_RINGS]; <br>
            +    struct dma_fence __rcu        *gang_submit; <br>
                  bool                ib_pool_ready; <br>
                  struct amdgpu_sa_manager   
            ib_pools[AMDGPU_IB_POOL_MAX]; <br>
                  struct amdgpu_sched
            gpu_sched[AMDGPU_HW_IP_NUM][AMDGPU_RING_PRIO_MAX]; <br>
            @@ -1233,6 +1234,8 @@ void
            amdgpu_device_invalidate_hdp(struct amdgpu_device *adev, <br>
                      struct amdgpu_ring *ring); <br>
                void amdgpu_device_halt(struct amdgpu_device *adev); <br>
            +struct dma_fence *amdgpu_device_switch_gang(struct
            amdgpu_device *adev, <br>
            +                        struct dma_fence *gang); <br>
                /* atpx handler */ <br>
              #if defined(CONFIG_VGA_SWITCHEROO) <br>
            diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
            b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c <br>
            index d78141e2c509..a116b8c08827 100644 <br>
            --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c <br>
            +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c <br>
            @@ -3512,6 +3512,7 @@ int amdgpu_device_init(struct
            amdgpu_device *adev, <br>
                  adev->gmc.gart_size = 512 * 1024 * 1024; <br>
                  adev->accel_working = false; <br>
                  adev->num_rings = 0; <br>
            +    RCU_INIT_POINTER(adev->gang_submit,
            dma_fence_get_stub()); <br>
                  adev->mman.buffer_funcs = NULL; <br>
                  adev->mman.buffer_funcs_ring = NULL; <br>
                  adev->vm_manager.vm_pte_funcs = NULL; <br>
            @@ -3989,6 +3990,7 @@ void amdgpu_device_fini_sw(struct
            amdgpu_device *adev) <br>
                  release_firmware(adev->firmware.gpu_info_fw); <br>
                  adev->firmware.gpu_info_fw = NULL; <br>
                  adev->accel_working = false; <br>
            +
            dma_fence_put(rcu_dereference_protected(adev->gang_submit,
            true)); <br>
                    amdgpu_reset_fini(adev); <br>
              @@ -5744,3 +5746,35 @@ void amdgpu_device_halt(struct
            amdgpu_device *adev) <br>
                  pci_disable_device(pdev); <br>
                  pci_wait_for_pending_transaction(pdev); <br>
              } <br>
            + <br>
            +/** <br>
            + * amdgpu_device_switch_gang - switch to a new gang <br>
            + * @adev: amdgpu_device pointer <br>
            + * @gang: the gang to switch to <br>
            + * <br>
            + * Try to switch to a new gang or return a reference to the
            current gang if that <br>
            + * isn't possible. <br>
            + * Returns: Either NULL if we switched correctly or a
            reference to the existing <br>
            + * gang. <br>
            + */ <br>
            +struct dma_fence *amdgpu_device_switch_gang(struct
            amdgpu_device *adev, <br>
            +                        struct dma_fence *gang) <br>
            +{ <br>
            +    struct dma_fence *old = NULL; <br>
            + <br>
            +    do { <br>
            +        dma_fence_put(old); <br>
            +        old =
            dma_fence_get_rcu_safe(&adev->gang_submit); <br>
            + <br>
            +        if (old == gang) <br>
            +            break; <br>
            + <br>
            +        if (!dma_fence_is_signaled(old)) <br>
            +            return old; <br>
            + <br>
            +    } while (cmpxchg((struct dma_fence __force
            **)&adev->gang_submit, <br>
            +             old, gang) != old); <br>
            + <br>
            +    dma_fence_put(old); <br>
            +    return NULL; <br>
            +} <br>
            diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c
            b/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c <br>
            index e07ceae36a5c..059e11c7898c 100644 <br>
            --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c <br>
            +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c <br>
            @@ -169,11 +169,29 @@ static void amdgpu_job_free_cb(struct
            drm_sched_job *s_job) <br>
                      kfree(job); <br>
              } <br>
              +void amdgpu_job_set_gang_leader(struct amdgpu_job *job, <br>
            +                struct amdgpu_job *leader) <br>
            +{ <br>
            +    struct dma_fence *fence =
            &leader->base.s_fence->scheduled; <br>
            + <br>
            +    WARN_ON(job->gang_submit); <br>
            + <br>
            +    /* <br>
            +     * Don't add a reference when we are the gang leader to
            avoid circle <br>
            +     * dependency. <br>
            +     */ <br>
            +    if (job != leader) <br>
            +        dma_fence_get(fence); <br>
            +    job->gang_submit = fence; <br>
            +} <br>
            + <br>
              void amdgpu_job_free(struct amdgpu_job *job) <br>
              { <br>
                  amdgpu_job_free_resources(job); <br>
                  amdgpu_sync_free(&job->sync); <br>
                  amdgpu_sync_free(&job->sched_sync); <br>
            +    if (job->gang_submit !=
            &job->base.s_fence->scheduled) <br>
            +        dma_fence_put(job->gang_submit); <br>
                    /* only put the hw fence if has embedded fence */ <br>
                  if (job->hw_fence.ops != NULL) <br>
            @@ -247,12 +265,16 @@ static struct dma_fence
            *amdgpu_job_dependency(struct drm_sched_job *sched_job, <br>
                      fence = amdgpu_sync_get_fence(&job->sync);
            <br>
                  } <br>
              +    if (!fence && !job->gang_submit) <br>
            +        fence = amdgpu_device_switch_gang(ring->adev,
            job->gang_submit); <br>
            + <br>
          </blockquote>
          <br>
          <br>
          Why job->gang_submit should be NULL in the check above ?
          Don't you want to switch to an actual new gang fence here ? <br>
          Jobs that don't have gang_submit fence set are not gang jobs
          anyway and we don't care for this dependency <br>
          for them right ? <br>
        </blockquote>
        <br>
        Well exactly that's the point. That a job is not part of a gang
        submit is signaled by setting the pointer to NULL. <br>
      </blockquote>
      <p><br>
      </p>
      <p>No dispute on this</p>
      <p><br>
      </p>
      <blockquote type="cite" cite="mid:5c573f4d-0fa8-5c01-67fe-c33a79a6f89c@gmail.com"> <br>
        If we don't check for NULL here we would just crash. <br>
      </blockquote>
      <p><br>
      </p>
      <p>But you go into the 'if clause' if job->gang_submit is equal
        to NULL, i would expect to see here<br>
        if (!fence &&<b> job->gang_submit</b>) because you
        want to switch to an actual new gang (not to NULL)<br>
      </p>
    </blockquote>
    <br>
    WTF? I'm like 100% that I've fixed that before sending it out.<br>
    <br>
    Thanks for point it out, but yeah I've already stumbled over that
    typo as well.<br>
    <br>
    Christian.<br>
    <br>
    <blockquote type="cite" cite="mid:6174d4b6-7a02-ffef-ce68-ca2f13dae337@amd.com">
      <p> </p>
      <p>Andrey</p>
      <p><br>
      </p>
      <blockquote type="cite" cite="mid:5c573f4d-0fa8-5c01-67fe-c33a79a6f89c@gmail.com"> <br>
        Christian. <br>
      </blockquote>
      <blockquote type="cite" cite="mid:5c573f4d-0fa8-5c01-67fe-c33a79a6f89c@gmail.com"> <br>
        <blockquote type="cite"> <br>
          Andrey <br>
          <br>
          <br>
          <blockquote type="cite">      return fence; <br>
              } <br>
                static struct dma_fence *amdgpu_job_run(struct
            drm_sched_job *sched_job) <br>
              { <br>
                  struct amdgpu_ring *ring =
            to_amdgpu_ring(sched_job->sched); <br>
            +    struct amdgpu_device *adev = ring->adev; <br>
                  struct dma_fence *fence = NULL, *finished; <br>
                  struct amdgpu_job *job; <br>
                  int r = 0; <br>
            @@ -264,8 +286,10 @@ static struct dma_fence
            *amdgpu_job_run(struct drm_sched_job *sched_job) <br>
                    trace_amdgpu_sched_run_job(job); <br>
              -    if (job->vram_lost_counter !=
            atomic_read(&ring->adev->vram_lost_counter)) <br>
            -        dma_fence_set_error(finished, -ECANCELED);/* skip
            IB as well if VRAM lost */ <br>
            +    /* Skip job if VRAM is lost and never resubmit gangs */
            <br>
            +    if (job->vram_lost_counter !=
            atomic_read(&adev->vram_lost_counter) || <br>
            +        (job->job_run_counter &&
            job->gang_submit)) <br>
            +        dma_fence_set_error(finished, -ECANCELED); <br>
                    if (finished->error < 0) { <br>
                      DRM_INFO("Skip scheduling IBs!\n"); <br>
            diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_job.h
            b/drivers/gpu/drm/amd/amdgpu/amdgpu_job.h <br>
            index 0bab8fe0d419..615328130615 100644 <br>
            --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_job.h <br>
            +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_job.h <br>
            @@ -51,6 +51,7 @@ struct amdgpu_job { <br>
                  struct amdgpu_sync    sched_sync; <br>
                  struct dma_fence    hw_fence; <br>
                  struct dma_fence    *external_hw_fence; <br>
            +    struct dma_fence    *gang_submit; <br>
                  uint32_t        preamble_status; <br>
                  uint32_t                preemption_status; <br>
                  bool                    vm_needs_flush; <br>
            @@ -80,6 +81,8 @@ int amdgpu_job_alloc_with_ib(struct
            amdgpu_device *adev, unsigned size, <br>
              void amdgpu_job_set_resources(struct amdgpu_job *job,
            struct amdgpu_bo *gds, <br>
                                struct amdgpu_bo *gws, struct amdgpu_bo
            *oa); <br>
              void amdgpu_job_free_resources(struct amdgpu_job *job); <br>
            +void amdgpu_job_set_gang_leader(struct amdgpu_job *job, <br>
            +                struct amdgpu_job *leader); <br>
              void amdgpu_job_free(struct amdgpu_job *job); <br>
              int amdgpu_job_submit(struct amdgpu_job *job, struct
            drm_sched_entity *entity, <br>
                            void *owner, struct dma_fence **f); <br>
          </blockquote>
        </blockquote>
        <br>
      </blockquote>
    </blockquote>
    <br>
  </body>
</html>