Skip to contentSkip to conversation

Fix igt pci unplug regressions

PATCH In review Benjamin Cheng 3 participants 11 messages Nov 5 – Nov 10

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

cover Fix igt pci unplug regressions

The commit 009f401fb98b ("drm/amdgpu: move amdgpu_vm_bo_update() call into correct location") unintentionally uncovered two cases of incorrect error handling, which
leads to random failures in the igt pci unplug tests.

Both of the issues follow the same pattern:
- One user thread is submitting a CS while the other is unplugging the device
- The VA op starts while the device is still plugged, and mid-way the unplug starts, causing the next (nested) drm_dev_enter() to fail with -ENODEV.
- Patch 1 addresses a leaked job when this race occurs.
- Patch 2 addresses a use-after-free when this race occurs.

This was uncovered by the referenced commit because the initial seq64 page table allocation only happened on the first CS instead of when the drm fd is opened.

Benjamin Cheng (2):
drm/amdgpu/vm: free pte update job on error
drm/amdgpu/vm_pt: free PT on error

 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    | 5 ++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 8 ++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)
Signature · 1 line
2.56.0
#1 lore raw

PATCH 1/2 drm/amdgpu/vm: free pte update job on error

In the happy path, the job gets freed when the job is submitted. However, if amdgpu_vm_ptes_update failed, we don't submit the job, so we would leak the job.

Fixes 009f401fb98b ("drm/amdgpu: move amdgpu_vm_bo_update() call into correct location")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -1
index 2ba33212d507..f6cb2b5854dd 100644
@@ -1274,8 +1274,11 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 		tmp = start + num_entries;
 		r = amdgpu_vm_ptes_update(&params, start, tmp, addr, flags);
-		if (r)
+		if (r) {
+			if (params.job)
+				amdgpu_job_free(params.job);
 			goto error_free;
+		}
 
 		amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE);
 		start = tmp;
Signature · 1 line
2.56.0
maintainer
#2 lore raw

Re: [PATCH 1/2] drm/amdgpu/vm: free pte update job on error

Quoted 3 lines · Benjamin Cheng

In the happy path, the job gets freed when the job is submitted. However, if amdgpu_vm_ptes_update failed, we don't submit the job, so we would leak the job.

Yeah that was already brought up upstream as well, but that here is the wrong approach to fixing this.

Thadeu Lima de Souza Cascardo <[email protected]> came up with a few patches on the public mailing list to tackle this as well.

Essentially we need to commit PTE updates before we allocate any new PT, background is not only that allocating PTs can fail but also that we need to drop the lock for this and that can make the PTEs we already constructed in the SDMA IB invalid.

I wanted to get that fixed for weeks but couldn't find time for it. Can you guys together look into that?

Thanks in advance,
Christian.

Quoted 24 lines
Fixes 009f401fb98b ("drm/amdgpu: move amdgpu_vm_bo_update() call into correct location")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -1
index 2ba33212d507..f6cb2b5854dd 100644
@@ -1274,8 +1274,11 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 		tmp = start + num_entries;
 		r = amdgpu_vm_ptes_update(&params, start, tmp, addr, flags);
-		if (r)
+		if (r) {
+			if (params.job)
+				amdgpu_job_free(params.job);
 			goto error_free;
+		}
 
 		amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE);
 		start = tmp;
#3 lore raw

Re: [PATCH 1/2] drm/amdgpu/vm: free pte update job on error

Quoted 11 lines · Christian König
Quoted 3 lines · Benjamin Cheng

In the happy path, the job gets freed when the job is submitted. However, if amdgpu_vm_ptes_update failed, we don't submit the job, so we would leak the job.

Yeah that was already brought up upstream as well, but that here is the wrong approach to fixing this.

Thadeu Lima de Souza Cascardo <[email protected]> came up with a few patches on the public mailing list to tackle this as well.

Essentially we need to commit PTE updates before we allocate any new PT, background is not only that allocating PTs can fail but also that we need to drop the lock for this and that can make the PTEs we already constructed in the SDMA IB invalid.

I wanted to get that fixed for weeks but couldn't find time for it. Can you guys together look into that?

I see, thanks I'll take a look at the previous patches.

Should we consider reverting commit 009f401fb98b ("drm/amdgpu: move amdgpu_vm_bo_update() call into correct location") in the meantime then? As I wrote in the cover letter it exposes this issue and can now be detected by IGT tests, which makes our CI flaky.

Quoted 26 lines

Thanks in advance,
Christian.

Quoted 23 lines
Fixes 009f401fb98b ("drm/amdgpu: move amdgpu_vm_bo_update() call into correct location")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -1
index 2ba33212d507..f6cb2b5854dd 100644
@@ -1274,8 +1274,11 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 		tmp = start + num_entries;
 		r = amdgpu_vm_ptes_update(&params, start, tmp, addr, flags);
-		if (r)
+		if (r) {
+			if (params.job)
+				amdgpu_job_free(params.job);
 			goto error_free;
+		}
 
 		amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE);
 		start = tmp;
maintainer
#4 lore raw

Re: [PATCH 1/2] drm/amdgpu/vm: free pte update job on error

Quoted 10 lines · Benjamin Cheng
Quoted 8 lines · Christian König
Quoted 3 lines · Benjamin Cheng

In the happy path, the job gets freed when the job is submitted. However, if amdgpu_vm_ptes_update failed, we don't submit the job, so we would leak the job.

Yeah that was already brought up upstream as well, but that here is the wrong approach to fixing this.

Thadeu Lima de Souza Cascardo <[email protected]> came up with a few patches on the public mailing list to tackle this as well.

Quoted 9 lines
Quoted 3 lines

Essentially we need to commit PTE updates before we allocate any new PT, background is not only that allocating PTs can fail but also that we need to drop the lock for this and that can make the PTEs we already constructed in the SDMA IB invalid.

I wanted to get that fixed for weeks but couldn't find time for it. Can you guys together look into that?

I see, thanks I'll take a look at the previous patches.

Should we consider reverting commit 009f401fb98b ("drm/amdgpu: move amdgpu_vm_bo_update() call into correct location") in the meantime then? As I wrote in the cover letter it exposes this issue and can now be detected by IGT tests, which makes our CI flaky.

What is the rational that "drm/amdgpu: move amdgpu_vm_bo_update() call into correct location" should make this flaky?

As far as I can see that is completely unrelated.

Thanks,
Christian.

Quoted 27 lines
Quoted 27 lines

Thanks in advance,
Christian.

Quoted 23 lines
Fixes 009f401fb98b ("drm/amdgpu: move amdgpu_vm_bo_update() call into correct location")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -1
index 2ba33212d507..f6cb2b5854dd 100644
@@ -1274,8 +1274,11 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 		tmp = start + num_entries;
 		r = amdgpu_vm_ptes_update(&params, start, tmp, addr, flags);
-		if (r)
+		if (r) {
+			if (params.job)
+				amdgpu_job_free(params.job);
 			goto error_free;
+		}
 
 		amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE);
 		start = tmp;
#5 lore raw

Re: [PATCH 1/2] drm/amdgpu/vm: free pte update job on error

Quoted 15 lines · Christian König
Quoted 13 lines · Benjamin Cheng
Quoted 11 lines · Christian König
Quoted 3 lines · Benjamin Cheng

In the happy path, the job gets freed when the job is submitted. However, if amdgpu_vm_ptes_update failed, we don't submit the job, so we would leak the job.

Yeah that was already brought up upstream as well, but that here is the wrong approach to fixing this.

Thadeu Lima de Souza Cascardo <[email protected]> came up with a few patches on the public mailing list to tackle this as well.

Essentially we need to commit PTE updates before we allocate any new PT, background is not only that allocating PTs can fail but also that we need to drop the lock for this and that can make the PTEs we already constructed in the SDMA IB invalid.

I wanted to get that fixed for weeks but couldn't find time for it. Can you guys together look into that?

Quoted 7 lines
Quoted 4 lines

I see, thanks I'll take a look at the previous patches.

Should we consider reverting commit 009f401fb98b ("drm/amdgpu: move amdgpu_vm_bo_update() call into correct location") in the meantime then? As I wrote in the cover letter it exposes this issue and can now be detected by IGT tests, which makes our CI flaky.

What is the rational that "drm/amdgpu: move amdgpu_vm_bo_update() call into correct location" should make this flaky?

As far as I can see that is completely unrelated.

The amdgpu_hotunplug_with_cs test submits a CS while another thread unplugs the device.

The commit made the seq64 mapping happen lazily on the first CS, and during the PT handling for that, the unplug happens, causing amdgpu_vm_pt_clear() to fail, triggering the use-after-free and leak of the SDMA job.

Quoted 29 lines

Thanks,
Christian.

Quoted 26 lines
Quoted 26 lines

Thanks in advance,
Christian.

Quoted 23 lines
Fixes 009f401fb98b ("drm/amdgpu: move amdgpu_vm_bo_update() call into correct location")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -1
index 2ba33212d507..f6cb2b5854dd 100644
@@ -1274,8 +1274,11 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 		tmp = start + num_entries;
 		r = amdgpu_vm_ptes_update(&params, start, tmp, addr, flags);
-		if (r)
+		if (r) {
+			if (params.job)
+				amdgpu_job_free(params.job);
 			goto error_free;
+		}
 
 		amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE);
 		start = tmp;
#6 lore raw

Re: [PATCH 1/2] drm/amdgpu/vm: free pte update job on error

In reply to Christian König
Quoted 9 lines · Christian König
Quoted 3 lines · Benjamin Cheng

In the happy path, the job gets freed when the job is submitted. However, if amdgpu_vm_ptes_update failed, we don't submit the job, so we would leak the job.

Yeah that was already brought up upstream as well, but that here is the wrong approach to fixing this.

Thadeu Lima de Souza Cascardo <[email protected]> came up with a few patches on the public mailing list to tackle this as well.

Essentially we need to commit PTE updates before we allocate any new PT, background is not only that allocating PTs can fail but also that we need to drop the lock for this and that can make the PTEs we already constructed in the SDMA IB invalid.

I gave this some more thought. I think these two fixes are still needed.

If we commit the PTE updates before allocating PTs, the pt_alloc could still fail and we would run into the same leak/use-after-free that we do now.

I think we need to both commit the PTE updates before allocating and have these two fixes?

Best,

Ben

Quoted 28 lines

I wanted to get that fixed for weeks but couldn't find time for it. Can you guys together look into that?

Thanks in advance,
Christian.

Quoted 23 lines
Fixes 009f401fb98b ("drm/amdgpu: move amdgpu_vm_bo_update() call into correct location")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -1
index 2ba33212d507..f6cb2b5854dd 100644
@@ -1274,8 +1274,11 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 		tmp = start + num_entries;
 		r = amdgpu_vm_ptes_update(&params, start, tmp, addr, flags);
-		if (r)
+		if (r) {
+			if (params.job)
+				amdgpu_job_free(params.job);
 			goto error_free;
+		}
 
 		amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE);
 		start = tmp;
maintainer
#7 lore raw

Re: [PATCH 1/2] drm/amdgpu/vm: free pte update job on error

Quoted 10 lines · Benjamin Cheng
Quoted 8 lines · Christian König
Quoted 3 lines · Benjamin Cheng

In the happy path, the job gets freed when the job is submitted. However, if amdgpu_vm_ptes_update failed, we don't submit the job, so we would leak the job.

Yeah that was already brought up upstream as well, but that here is the wrong approach to fixing this.

Thadeu Lima de Souza Cascardo <[email protected]> came up with a few patches on the public mailing list to tackle this as well.

Quoted 8 lines
Quoted 1 line

Essentially we need to commit PTE updates before we allocate any new PT, background is not only that allocating PTs can fail but also that we need to drop the lock for this and that can make the PTEs we already constructed in the SDMA IB invalid.

I gave this some more thought. I think these two fixes are still needed.

If we commit the PTE updates before allocating PTs, the pt_alloc could still fail and we would run into the same leak/use-after-free that we do now.

I think we need to both commit the PTE updates before allocating and have these two fixes?

Not quite, when the SDMA work is committed before the PT allocation we need to allocate a new SDMA IB after the PT is allocated.

Regards,
Christian.

Quoted 33 lines

Best,

Ben

Quoted 28 lines

I wanted to get that fixed for weeks but couldn't find time for it. Can you guys together look into that?

Thanks in advance,
Christian.

Quoted 23 lines
Fixes 009f401fb98b ("drm/amdgpu: move amdgpu_vm_bo_update() call into correct location")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -1
index 2ba33212d507..f6cb2b5854dd 100644
@@ -1274,8 +1274,11 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 		tmp = start + num_entries;
 		r = amdgpu_vm_ptes_update(&params, start, tmp, addr, flags);
-		if (r)
+		if (r) {
+			if (params.job)
+				amdgpu_job_free(params.job);
 			goto error_free;
+		}
 
 		amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE);
 		start = tmp;
#8 lore raw

Re: [PATCH 1/2] drm/amdgpu/vm: free pte update job on error

In reply to Christian König
Quoted 7 lines · Christian König
Quoted 3 lines · Benjamin Cheng

In the happy path, the job gets freed when the job is submitted. However, if amdgpu_vm_ptes_update failed, we don't submit the job, so we would leak the job.

Yeah that was already brought up upstream as well, but that here is the wrong approach to fixing this.

Quoted 9 lines

Thadeu Lima de Souza Cascardo <[email protected]> came up with a few patches on the public mailing list to tackle this as well.

Essentially we need to commit PTE updates before we allocate any new PT, background is not only that allocating PTs can fail but also that we need to drop the lock for this and that can make the PTEs we already constructed in the SDMA IB invalid.

I wanted to get that fixed for weeks but couldn't find time for it. Can you guys together look into that?

Thanks in advance,
Christian.

Hey, Christian.

I have been looking at this and trying to find out the best shape of the vm hooks and other helpers we might want to introduce here.

But first I tried to work on a reproducer and I realized that we have two situations:

1) In the case of DRM, the BO should be locked, so there should not be any eviction of move, so the entries in the IB should still be valid.

2) In the case of SVM, there is the prange lock. Does it still protect the range, and, then, would the entries be still valid?

Is this related to what you propose at [1], where you mention:

"
This allows to remove the buggy SVM range lock, but that's not part of this patch set.
"

Please, let me know if I am missing something or am working with the wrong assumptions.

Thanks.
Cascardo.

[1] https://lore.kernel.org/amd-gfx/[email protected]/

Quoted 25 lines
Quoted 24 lines
Fixes 009f401fb98b ("drm/amdgpu: move amdgpu_vm_bo_update() call into correct location")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -1
index 2ba33212d507..f6cb2b5854dd 100644
@@ -1274,8 +1274,11 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 		tmp = start + num_entries;
 		r = amdgpu_vm_ptes_update(&params, start, tmp, addr, flags);
-		if (r)
+		if (r) {
+			if (params.job)
+				amdgpu_job_free(params.job);
 			goto error_free;
+		}
 
 		amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE);
 		start = tmp;
maintainer
#9 lore raw

Re: [PATCH 1/2] drm/amdgpu/vm: free pte update job on error

Quoted 2 lines · Thadeu Lima de Souza Cascardo
Quoted 1 line · Christian König

On 11/5/26 22:00, Benjamin Cheng wrote:

...

Quoted 11 lines

Hey, Christian.

I have been looking at this and trying to find out the best shape of the vm hooks and other helpers we might want to introduce here.

But first I tried to work on a reproducer and I realized that we have two situations:

1) In the case of DRM, the BO should be locked, so there should not be any eviction of move, so the entries in the IB should still be valid.

Correct, but that is actually the easy case. Updates to the VA space through the DRM render node should always look the VM root PD and so should be synchronized.

Quoted 2 lines

2) In the case of SVM, there is the prange lock. Does it still protect the range, and, then, would the entries be still valid?

Nope, that is an often made mistake.

MMU notifier update which invalidate a certain VA address range can happen with quite a bunch of different locks held.

But in general PTE updates are opportunistic and we can only use the sequence number while holding the VM critical section lock to determine if the PTE entries are still valid or not.

Quoted 6 lines

Is this related to what you propose at [1], where you mention:

"
This allows to remove the buggy SVM range lock, but that's not part of this patch set.
"

Yes exactly that.

The whole idea of the SVM range lock was to prevent that race condition, but that approach doesn't work because you can only allocate minimal amount of memory while holding that lock. So allocating page tables is an absolutely no-go.

That's also the reason why unmap operations can't fail and MMU notifier can't return an error code.

Regards,
Christian.

Quoted 8 lines

Please, let me know if I am missing something or am working with the wrong assumptions.

Thanks.
Cascardo.

[1] https://lore.kernel.org/amd-gfx/[email protected]/

#10 lore raw

PATCH 2/2 drm/amdgpu/vm_pt: free PT on error

In reply to Benjamin Cheng

On error the bo would still be attached to the entry, while the bo gets unref'ed. Later when the VM is torn down the bo would be accessed again in order to attempt a cleanup, resulting in a use-after-free.

Fixes 009f401fb98b ("drm/amdgpu: move amdgpu_vm_bo_update() call into correct location")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +6 -2
index c73eb416c0e2..a66aac73e835 100644
@@ -502,6 +502,8 @@ int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	return 0;
 }
 
+static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry);
+
 /**
  * amdgpu_vm_pt_alloc - Allocate a specific page table
  *
@@ -544,8 +546,10 @@ static int amdgpu_vm_pt_alloc(struct amdgpu_vm_update_params *p,
 	pt_bo->parent = amdgpu_bo_ref(cursor->parent->bo);
 	amdgpu_vm_bo_base_init(entry, p->vm, pt_bo);
 	r = amdgpu_vm_pt_clear(p->adev, p->vm, pt, p->immediate);
-	if (r)
-		goto error_unpin;
+	if (r) {
+		amdgpu_vm_pt_free(entry);
+		return r;
+	}
 
 	return 0;
 
Signature · 1 line
2.56.0