Skip to contentSkip to conversation

drm/amd/amdgpu: fix potential bad job hw_fence underflow

PATCH In review Jingwen Chen 3 participants 9 messages Oct 22, 2021 – Oct 28, 2021

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

PATCH 1/1 drm/amd/amdgpu: fix potential bad job hw_fence underflow

[Why]
In advance tdr mode, the real bad job will be resubmitted twice, while in drm_sched_resubmit_jobs_ext, there's a dma_fence_put, so the bad job is put one more time than other jobs.

[How]
Adding dma_fence_get before resbumit job in amdgpu_device_recheck_guilty_jobs and put the fence for normal jobs

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -0
index 41ce86244144..975f069f6fe8 100644
@@ -4841,6 +4841,9 @@ static void amdgpu_device_recheck_guilty_jobs(
 
 		/* clear job's guilty and depend the folowing step to decide the real one */
 		drm_sched_reset_karma(s_job);
+		/* for the real bad job, it will be resubmitted twice, adding a dma_fence_get
+		 * to make sure fence is balanced */
+		dma_fence_get(s_job->s_fence->parent);
 		drm_sched_resubmit_jobs_ext(&ring->sched, 1);
 
 		ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
@@ -4876,6 +4879,7 @@ static void amdgpu_device_recheck_guilty_jobs(
 
 		/* got the hw fence, signal finished fence */
 		atomic_dec(ring->sched.score);
+		dma_fence_put(s_job->s_fence->parent);
 		dma_fence_get(&s_job->s_fence->finished);
 		dma_fence_signal(&s_job->s_fence->finished);
 		dma_fence_put(&s_job->s_fence->finished);
Signature · 1 line
2.30.2
#1 lore raw

ping

Quoted 36 lines · Jingwen Chen

[Why]
In advance tdr mode, the real bad job will be resubmitted twice, while in drm_sched_resubmit_jobs_ext, there's a dma_fence_put, so the bad job is put one more time than other jobs.

[How]
Adding dma_fence_get before resbumit job in amdgpu_device_recheck_guilty_jobs and put the fence for normal jobs

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -0
index 41ce86244144..975f069f6fe8 100644
@@ -4841,6 +4841,9 @@ static void amdgpu_device_recheck_guilty_jobs(
 
 		/* clear job's guilty and depend the folowing step to decide the real one */
 		drm_sched_reset_karma(s_job);
+		/* for the real bad job, it will be resubmitted twice, adding a dma_fence_get
+		 * to make sure fence is balanced */
+		dma_fence_get(s_job->s_fence->parent);
 		drm_sched_resubmit_jobs_ext(&ring->sched, 1);
 
 		ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
@@ -4876,6 +4879,7 @@ static void amdgpu_device_recheck_guilty_jobs(
 
 		/* got the hw fence, signal finished fence */
 		atomic_dec(ring->sched.score);
+		dma_fence_put(s_job->s_fence->parent);
 		dma_fence_get(&s_job->s_fence->finished);
 		dma_fence_signal(&s_job->s_fence->finished);
 		dma_fence_put(&s_job->s_fence->finished);
#2 lore raw

What do you mean by underflow in this case ? You mean use after free because of extra dma_fence_put() ?

Quoted 23 lines · JingWen Chen

ping

Quoted 20 lines · Jingwen Chen

[Why]
In advance tdr mode, the real bad job will be resubmitted twice, while in drm_sched_resubmit_jobs_ext, there's a dma_fence_put, so the bad job is put one more time than other jobs.

[How]
Adding dma_fence_get before resbumit job in amdgpu_device_recheck_guilty_jobs and put the fence for normal jobs

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 4 ++++
  1 file changed, 4 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
index 41ce86244144..975f069f6fe8 100644
@@ -4841,6 +4841,9 @@ static void amdgpu_device_recheck_guilty_jobs(
 
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static void amdgpu_device_recheck_guilty_jobs( · line 4845

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
  		/* clear job's guilty and depend the folowing step to decide the real one */
  		drm_sched_reset_karma(s_job);
+		/* for the real bad job, it will be resubmitted twice, adding a dma_fence_get
+		 * to make sure fence is balanced */

But that put in drm_sched_resubmit_jobs_ext is for the previous parent fence.
fence = sched->ops->run_job(s_job); returns a new HW fence and the put drops the refcount on the old one.

Andrey

Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
+		dma_fence_get(s_job->s_fence->parent);
  		drm_sched_resubmit_jobs_ext(&ring->sched, 1);
 
  		ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
@@ -4876,6 +4879,7 @@ static void amdgpu_device_recheck_guilty_jobs(
 
  		/* got the hw fence, signal finished fence */
  		atomic_dec(ring->sched.score);
+		dma_fence_put(s_job->s_fence->parent);
  		dma_fence_get(&s_job->s_fence->finished);
  		dma_fence_signal(&s_job->s_fence->finished);
  		dma_fence_put(&s_job->s_fence->finished);
#3 lore raw
Quoted 2 lines · Andrey Grodzovsky

What do you mean by underflow in this case ? You mean use after free because of extra dma_fence_put() ?

yes

Quoted 33 lines · JingWen Chen
Quoted 17 lines

ping

Quoted 14 lines · Jingwen Chen

[Why]
In advance tdr mode, the real bad job will be resubmitted twice, while in drm_sched_resubmit_jobs_ext, there's a dma_fence_put, so the bad job is put one more time than other jobs.

[How]
Adding dma_fence_get before resbumit job in amdgpu_device_recheck_guilty_jobs and put the fence for normal jobs

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 4 ++++

  1 file changed, 4 insertions(+)
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static void amdgpu_device_recheck_guilty_jobs( · line 4844

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
index 41ce86244144..975f069f6fe8 100644
@@ -4841,6 +4841,9 @@ static void amdgpu_device_recheck_guilty_jobs(
             /* clear job's guilty and depend the folowing step to decide the real one */
           drm_sched_reset_karma(s_job);
+        /* for the real bad job, it will be resubmitted twice, adding a dma_fence_get
+         * to make sure fence is balanced */

But that put in drm_sched_resubmit_jobs_ext is for the previous parent fence.
fence = sched->ops->run_job(s_job); returns a new HW fence and the put drops the refcount on the old one.

Quoted 3 lines

Andrey

Hi Andrey,

If I remember correctly, after we embedded the hw_fence into amdgpu_job, there will be not fence replacement in amdgpu_job_run.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
+        dma_fence_get(s_job->s_fence->parent);
           drm_sched_resubmit_jobs_ext(&ring->sched, 1);
             ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
@@ -4876,6 +4879,7 @@ static void amdgpu_device_recheck_guilty_jobs(
             /* got the hw fence, signal finished fence */
           atomic_dec(ring->sched.score);
+        dma_fence_put(s_job->s_fence->parent);
           dma_fence_get(&s_job->s_fence->finished);
           dma_fence_signal(&s_job->s_fence->finished);
           dma_fence_put(&s_job->s_fence->finished);
#4 lore raw
Quoted 3 lines · JingWen Chen
Quoted 1 line · Andrey Grodzovsky

What do you mean by underflow in this case ? You mean use after free because of extra dma_fence_put() ?

yes

Then maybe update the description  because 'underflow' is very confusing

Quoted 31 lines
Quoted 31 lines · JingWen Chen
Quoted 17 lines

ping

Quoted 14 lines · Jingwen Chen

[Why]
In advance tdr mode, the real bad job will be resubmitted twice, while in drm_sched_resubmit_jobs_ext, there's a dma_fence_put, so the bad job is put one more time than other jobs.

[How]
Adding dma_fence_get before resbumit job in amdgpu_device_recheck_guilty_jobs and put the fence for normal jobs

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 4 ++++
   1 file changed, 4 insertions(+)
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static void amdgpu_device_recheck_guilty_jobs( · line 4844

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
index 41ce86244144..975f069f6fe8 100644
@@ -4841,6 +4841,9 @@ static void amdgpu_device_recheck_guilty_jobs(
             /* clear job's guilty and depend the folowing step to decide the real one */
           drm_sched_reset_karma(s_job);
+        /* for the real bad job, it will be resubmitted twice, adding a dma_fence_get
+         * to make sure fence is balanced */

But that put in drm_sched_resubmit_jobs_ext is for the previous parent fence.
fence = sched->ops->run_job(s_job); returns a new HW fence and the put drops the refcount on the old one.

Quoted 6 lines
Quoted 3 lines

Andrey

Hi Andrey,

If I remember correctly, after we embedded the hw_fence into amdgpu_job, there will be not fence replacement in amdgpu_job_run.

Right, I forgot that... What about removing line https://elixir.bootlin.com/linux/v5.15-rc6/source/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c#L265 ? What if you make dma_get_fence unconditional instead ?

Andrey

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
+        dma_fence_get(s_job->s_fence->parent);
           drm_sched_resubmit_jobs_ext(&ring->sched, 1);
             ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
@@ -4876,6 +4879,7 @@ static void amdgpu_device_recheck_guilty_jobs(
             /* got the hw fence, signal finished fence */
           atomic_dec(ring->sched.score);
+        dma_fence_put(s_job->s_fence->parent);
           dma_fence_get(&s_job->s_fence->finished);
           dma_fence_signal(&s_job->s_fence->finished);
           dma_fence_put(&s_job->s_fence->finished);
#5 lore raw
Quoted 9 lines · Andrey Grodzovsky
Quoted 3 lines · JingWen Chen
Quoted 1 line · Andrey Grodzovsky

What do you mean by underflow in this case ? You mean use after free because of extra dma_fence_put() ?

yes

Then maybe update the description  because 'underflow' is very confusing

will do

Quoted 37 lines
Quoted 37 lines
Quoted 32 lines · JingWen Chen
Quoted 17 lines

ping

Quoted 14 lines · Jingwen Chen

[Why]
In advance tdr mode, the real bad job will be resubmitted twice, while in drm_sched_resubmit_jobs_ext, there's a dma_fence_put, so the bad job is put one more time than other jobs.

[How]
Adding dma_fence_get before resbumit job in amdgpu_device_recheck_guilty_jobs and put the fence for normal jobs

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 4 ++++

   1 file changed, 4 insertions(+)
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static void amdgpu_device_recheck_guilty_jobs( · line 4844

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
index 41ce86244144..975f069f6fe8 100644
@@ -4841,6 +4841,9 @@ static void amdgpu_device_recheck_guilty_jobs(
              /* clear job's guilty and depend the folowing step to decide the real one */
            drm_sched_reset_karma(s_job);
+        /* for the real bad job, it will be resubmitted twice, adding a dma_fence_get
+         * to make sure fence is balanced */

But that put in drm_sched_resubmit_jobs_ext is for the previous parent fence.
fence = sched->ops->run_job(s_job); returns a new HW fence and the put drops the refcount on the old one.

Quoted 3 lines

Andrey

Hi Andrey,

Quoted 9 lines
Quoted 1 line

If I remember correctly, after we embedded the hw_fence into amdgpu_job, there will be not fence replacement in amdgpu_job_run.

Right, I forgot that... What about removing line https://elixir.bootlin.com/linux/v5.15-rc6/source/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c#L265 ? What if you make dma_get_fence unconditional instead ?

Andrey

Hi Andrey,

I have tried this and this will cause normal jobs cannot be free(lacks a dma_fence_put). I have figured out all the get/put

for sched_jobs and only the bad job lacks a dma_fence_get, other jobs are just fine.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
+        dma_fence_get(s_job->s_fence->parent);
            drm_sched_resubmit_jobs_ext(&ring->sched, 1);
              ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
@@ -4876,6 +4879,7 @@ static void amdgpu_device_recheck_guilty_jobs(
              /* got the hw fence, signal finished fence */
            atomic_dec(ring->sched.score);
+        dma_fence_put(s_job->s_fence->parent);
            dma_fence_get(&s_job->s_fence->finished);
            dma_fence_signal(&s_job->s_fence->finished);
            dma_fence_put(&s_job->s_fence->finished);
#6 lore raw
Quoted 49 lines · JingWen Chen
Quoted 7 lines · Andrey Grodzovsky
Quoted 3 lines · JingWen Chen
Quoted 1 line · Andrey Grodzovsky

What do you mean by underflow in this case ? You mean use after free because of extra dma_fence_put() ?

yes

Then maybe update the description  because 'underflow' is very confusing

will do

Quoted 40 lines
Quoted 30 lines
Quoted 30 lines · JingWen Chen
Quoted 17 lines

ping

Quoted 14 lines · Jingwen Chen

[Why]
In advance tdr mode, the real bad job will be resubmitted twice, while in drm_sched_resubmit_jobs_ext, there's a dma_fence_put, so the bad job is put one more time than other jobs.

[How]
Adding dma_fence_get before resbumit job in amdgpu_device_recheck_guilty_jobs and put the fence for normal jobs

---
    drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 4 ++++
    1 file changed, 4 insertions(+)
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static void amdgpu_device_recheck_guilty_jobs( · line 4844

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
index 41ce86244144..975f069f6fe8 100644
@@ -4841,6 +4841,9 @@ static void amdgpu_device_recheck_guilty_jobs(
              /* clear job's guilty and depend the folowing step to decide the real one */
            drm_sched_reset_karma(s_job);
+        /* for the real bad job, it will be resubmitted twice, adding a dma_fence_get
+         * to make sure fence is balanced */

But that put in drm_sched_resubmit_jobs_ext is for the previous parent fence.
fence = sched->ops->run_job(s_job); returns a new HW fence and the put drops the refcount on the old one.

Quoted 6 lines
Quoted 3 lines

Andrey

Hi Andrey,

If I remember correctly, after we embedded the hw_fence into amdgpu_job, there will be not fence replacement in amdgpu_job_run.

Right, I forgot that... What about removing line https://elixir.bootlin.com/linux/v5.15-rc6/source/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c#L265 ? What if you make dma_get_fence unconditional instead ?

Quoted 6 lines
Quoted 3 lines

Andrey

Hi Andrey,

I have tried this and this will cause normal jobs cannot be free(lacks a dma_fence_put).

I can't see it  - can you point me where in that case you get unbalanced refcount ? As far as I see for a a normal job being ran in amdgpu_device_recheck_guilty_jobs the refcount on hw_fence is  -

drm_sched_resubmit_jobs_ext->dma_fence_put -> refcount decrease by 1 drm_sched_resubmit_jobs_ext->amdgpu_job_run->dma_fence_get increase by 1

In total refcount didn't change until now

Next,  dma_fence_wait_timeout completed successfully because the job is normal and then you delete that job from pending list and call the free_job cb which drops remaining refcounts on the hw_fence.

I am probably missing some  dma_fence_get since you checked it on a device but I wonder where is my mistake ?

Andrey

Quoted 14 lines

I have figured out all the get/put

for sched_jobs and only the bad job lacks a dma_fence_get, other jobs are just fine.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
+        dma_fence_get(s_job->s_fence->parent);
            drm_sched_resubmit_jobs_ext(&ring->sched, 1);
              ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
@@ -4876,6 +4879,7 @@ static void amdgpu_device_recheck_guilty_jobs(
              /* got the hw fence, signal finished fence */
            atomic_dec(ring->sched.score);
+        dma_fence_put(s_job->s_fence->parent);
            dma_fence_get(&s_job->s_fence->finished);
            dma_fence_signal(&s_job->s_fence->finished);
            dma_fence_put(&s_job->s_fence->finished);
#7 lore raw
Quoted 65 lines · Andrey Grodzovsky
Quoted 49 lines · JingWen Chen
Quoted 7 lines · Andrey Grodzovsky
Quoted 3 lines · JingWen Chen
Quoted 1 line · Andrey Grodzovsky

What do you mean by underflow in this case ? You mean use after free because of extra dma_fence_put() ?

yes

Then maybe update the description  because 'underflow' is very confusing

will do

Quoted 40 lines
Quoted 30 lines
Quoted 30 lines · JingWen Chen
Quoted 17 lines

ping

Quoted 14 lines · Jingwen Chen

[Why]
In advance tdr mode, the real bad job will be resubmitted twice, while in drm_sched_resubmit_jobs_ext, there's a dma_fence_put, so the bad job is put one more time than other jobs.

[How]
Adding dma_fence_get before resbumit job in amdgpu_device_recheck_guilty_jobs and put the fence for normal jobs

---
    drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 4 ++++

    1 file changed, 4 insertions(+)
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static void amdgpu_device_recheck_guilty_jobs( · line 4844

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
index 41ce86244144..975f069f6fe8 100644
@@ -4841,6 +4841,9 @@ static void amdgpu_device_recheck_guilty_jobs(
               /* clear job's guilty and depend the folowing step to decide the real one */
             drm_sched_reset_karma(s_job);
+        /* for the real bad job, it will be resubmitted twice, adding a dma_fence_get
+         * to make sure fence is balanced */

But that put in drm_sched_resubmit_jobs_ext is for the previous parent fence.
fence = sched->ops->run_job(s_job); returns a new HW fence and the put drops the refcount on the old one.

Quoted 6 lines
Quoted 3 lines

Andrey

Hi Andrey,

If I remember correctly, after we embedded the hw_fence into amdgpu_job, there will be not fence replacement in amdgpu_job_run.

Right, I forgot that... What about removing line https://elixir.bootlin.com/linux/v5.15-rc6/source/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c#L265 ? What if you make dma_get_fence unconditional instead ?

Quoted 6 lines
Quoted 3 lines

Andrey

Hi Andrey,

I have tried this and this will cause normal jobs cannot be free(lacks a dma_fence_put).

I can't see it  - can you point me where in that case you get unbalanced refcount ? As far as I see for a a normal job being ran in amdgpu_device_recheck_guilty_jobs the refcount on hw_fence is  -

drm_sched_resubmit_jobs_ext->dma_fence_put -> refcount decrease by 1 drm_sched_resubmit_jobs_ext->amdgpu_job_run->dma_fence_get increase by 1

Quoted 10 lines

In total refcount didn't change until now

Next,  dma_fence_wait_timeout completed successfully because the job is normal and then you delete that job from pending list and call the free_job cb which drops remaining refcounts on the hw_fence.

I am probably missing some  dma_fence_get since you checked it on a device but I wonder where is my mistake ?

Andrey

Hi Andrey,

The thing is the put/get is balanced right now for normal jobs in TDR. Changing this dma_fence_get to unconditional simply adds 1 dma_fence_get but there's no corresponding dma_fence_put for normal jobs.

And if this can be helpful, I try to find all dma_fence_get/put for a normal job in advance TDR based on the latest drm-next.

amdgpu_fence_emit -> dma_fence_init    ref_count = 1​ amdgpu_fence_emit -> add into rcu    ref_count = 2​ amdgpu_job_run->get after ib_schedule    ref_count = 3​ drm_sched_main-> add fence callback get    ref_count = 4​ drm_sched_main-> add fence callback put    ref_count = 3​ drm_sched_resubmit_jobs_ext    ref_count = 2 amdgpu_fence_emit -> add into rcu    ref_count = 3​ amdgpu_fence_process-> put after signal    ref_count = 2​ drm_sched_main->free_job    ref_count = 1​ drm_sched_fence_release_finished    ref_count = 0​

If we do unconditional get, this sequence will turn into:

amdgpu_fence_emit -> dma_fence_init    ref_count = 1​ amdgpu_fence_emit -> add into rcu    ref_count = 2​ amdgpu_job_run->get after ib_schedule    ref_count = 3​ drm_sched_main-> add fence callback get    ref_count = 4​ drm_sched_main-> add fence callback put    ref_count = 3​ drm_sched_resubmit_jobs_ext    ref_count = 2 amdgpu_fence_emit -> add into rcu    ref_count = 3​ +  amdgpu_job_run->get after ib_schedule    ref_count = 4 amdgpu_fence_process-> put after signal    ref_count = 3 drm_sched_main->free_job    ref_count = 2 drm_sched_fence_release_finished    ref_count = 1

Quoted 15 lines
Quoted 15 lines

I have figured out all the get/put

for sched_jobs and only the bad job lacks a dma_fence_get, other jobs are just fine.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
+        dma_fence_get(s_job->s_fence->parent);
             drm_sched_resubmit_jobs_ext(&ring->sched, 1);
               ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
@@ -4876,6 +4879,7 @@ static void amdgpu_device_recheck_guilty_jobs(
               /* got the hw fence, signal finished fence */
             atomic_dec(ring->sched.score);
+        dma_fence_put(s_job->s_fence->parent);
             dma_fence_get(&s_job->s_fence->finished);
             dma_fence_signal(&s_job->s_fence->finished);
             dma_fence_put(&s_job->s_fence->finished);
#8 lore raw
Quoted 78 lines · JingWen Chen
Quoted 61 lines · Andrey Grodzovsky
Quoted 47 lines · JingWen Chen
Quoted 6 lines · Andrey Grodzovsky
Quoted 3 lines · JingWen Chen
Quoted 1 line · Andrey Grodzovsky

What do you mean by underflow in this case ? You mean use after free because of extra dma_fence_put() ?

yes

Then maybe update the description  because 'underflow' is very confusing

will do

Quoted 39 lines
Quoted 30 lines
Quoted 30 lines · JingWen Chen
Quoted 17 lines

ping

Quoted 14 lines · Jingwen Chen

[Why]
In advance tdr mode, the real bad job will be resubmitted twice, while in drm_sched_resubmit_jobs_ext, there's a dma_fence_put, so the bad job is put one more time than other jobs.

[How]
Adding dma_fence_get before resbumit job in amdgpu_device_recheck_guilty_jobs and put the fence for normal jobs

---
     drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 4 ++++
     1 file changed, 4 insertions(+)
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static void amdgpu_device_recheck_guilty_jobs( · line 4844

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
index 41ce86244144..975f069f6fe8 100644
@@ -4841,6 +4841,9 @@ static void amdgpu_device_recheck_guilty_jobs(
               /* clear job's guilty and depend the folowing step to decide the real one */
             drm_sched_reset_karma(s_job);
+        /* for the real bad job, it will be resubmitted twice, adding a dma_fence_get
+         * to make sure fence is balanced */

But that put in drm_sched_resubmit_jobs_ext is for the previous parent fence.
fence = sched->ops->run_job(s_job); returns a new HW fence and the put drops the refcount on the old one.

Quoted 6 lines
Quoted 3 lines

Andrey

Hi Andrey,

If I remember correctly, after we embedded the hw_fence into amdgpu_job, there will be not fence replacement in amdgpu_job_run.

Right, I forgot that... What about removing line https://elixir.bootlin.com/linux/v5.15-rc6/source/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c#L265 ? What if you make dma_get_fence unconditional instead ?

Quoted 6 lines
Quoted 3 lines

Andrey

Hi Andrey,

I have tried this and this will cause normal jobs cannot be free(lacks a dma_fence_put).

I can't see it  - can you point me where in that case you get unbalanced refcount ? As far as I see for a a normal job being ran in amdgpu_device_recheck_guilty_jobs the refcount on hw_fence is  -

drm_sched_resubmit_jobs_ext->dma_fence_put -> refcount decrease by 1 drm_sched_resubmit_jobs_ext->amdgpu_job_run->dma_fence_get increase by 1

Quoted 10 lines

In total refcount didn't change until now

Next,  dma_fence_wait_timeout completed successfully because the job is normal and then you delete that job from pending list and call the free_job cb which drops remaining refcounts on the hw_fence.

I am probably missing some  dma_fence_get since you checked it on a device but I wonder where is my mistake ?

Andrey

Hi Andrey,

The thing is the put/get is balanced right now for normal jobs in TDR. Changing this dma_fence_get to unconditional simply adds 1 dma_fence_get but there's no corresponding dma_fence_put for normal jobs.

And if this can be helpful, I try to find all dma_fence_get/put for a normal job in advance TDR based on the latest drm-next.

Quoted 7 lines

amdgpu_fence_emit -> dma_fence_init    ref_count = 1​ amdgpu_fence_emit -> add into rcu    ref_count = 2​ amdgpu_job_run->get after ib_schedule    ref_count = 3​ drm_sched_main-> add fence callback get    ref_count = 4​ drm_sched_main-> add fence callback put    ref_count = 3​ drm_sched_resubmit_jobs_ext    ref_count = 2 amdgpu_fence_emit -> add into rcu    ref_count = 3​

Now I see that that put in drm_sched_resubmit_jobs_ext is for dropping the refcount for the previous 'amdgpu_fence_emit -> add into rcu' get... This is all very convoluted and confusing. Probably requires some rework to make the code more clear but for now we need the bug fixed so with the title changed the patch is Reviewed-by: Andrey Grodzovsky <[email protected]>

Andrey

Quoted 31 lines

amdgpu_fence_process-> put after signal    ref_count = 2​ drm_sched_main->free_job    ref_count = 1​ drm_sched_fence_release_finished    ref_count = 0​

If we do unconditional get, this sequence will turn into:

amdgpu_fence_emit -> dma_fence_init    ref_count = 1​ amdgpu_fence_emit -> add into rcu    ref_count = 2​ amdgpu_job_run->get after ib_schedule    ref_count = 3​ drm_sched_main-> add fence callback get    ref_count = 4​ drm_sched_main-> add fence callback put    ref_count = 3​ drm_sched_resubmit_jobs_ext    ref_count = 2 amdgpu_fence_emit -> add into rcu    ref_count = 3​ +  amdgpu_job_run->get after ib_schedule    ref_count = 4 amdgpu_fence_process-> put after signal    ref_count = 3 drm_sched_main->free_job    ref_count = 2 drm_sched_fence_release_finished    ref_count = 1

Quoted 14 lines
Quoted 14 lines

I have figured out all the get/put

for sched_jobs and only the bad job lacks a dma_fence_get, other jobs are just fine.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
+        dma_fence_get(s_job->s_fence->parent);
             drm_sched_resubmit_jobs_ext(&ring->sched, 1);
               ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
@@ -4876,6 +4879,7 @@ static void amdgpu_device_recheck_guilty_jobs(
               /* got the hw fence, signal finished fence */
             atomic_dec(ring->sched.score);
+        dma_fence_put(s_job->s_fence->parent);
             dma_fence_get(&s_job->s_fence->finished);
             dma_fence_signal(&s_job->s_fence->finished);
             dma_fence_put(&s_job->s_fence->finished);