drm/amdgpu: workaround for TLB seq race
Subsystems (2)
PATCH 1/1 drm/amdgpu: workaround for TLB seq race
It can happen that we query the sequence value before the callback had a chance to run.
Work around that by grabbing the fence lock and releasing it again. Should be replaced by hw handling soon.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +15 -0
@@ -485,6 +485,21 @@ void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m);*/ static inline uint64_t amdgpu_vm_tlb_seq (struct amdgpu_vm *vm) { +unsigned long flags; +spinlock_t *lock; + +/* +* Work around to stop racing between the fence signaling and handling +* the cb. The lock is static after initially setting it up, just make +* sure that the dma_fence structure isn't freed up. +*/ +rcu_read_lock (); + lock = vm->last_tlb_flush->lock; +rcu_read_unlock (); + +spin_lock_irqsave (lock, flags); +spin_unlock_irqrestore (lock, flags); +return atomic64_read (&vm->tlb_seq); }
Signature · 1 line
2.34.1
Quoted 5 lines · Christian König
It can happen that we query the sequence value before the callback had a chance to run.
Work around that by grabbing the fence lock and releasing it again.
workaround
Quoted 12 lines
Should be replaced by hw handling soon.
Signed-off-by Christian König <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m); · line 492
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +5
-0@@ -485,6 +485,21 @@ void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m);*/ static inline uint64_t amdgpu_vm_tlb_seq (struct amdgpu_vm *vm) { +unsigned long flags; +spinlock_t *lock; + +/* +* Work around to stop racing between the fence signaling and handling
Workaround
WIth that fixed up, the patch is:
Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +10
-1+* the cb. The lock is static after initially setting it up, just make +* sure that the dma_fence structure isn't freed up. +*/ +rcu_read_lock (); + lock = vm->last_tlb_flush->lock; +rcu_read_unlock (); + +spin_lock_irqsave (lock, flags); +spin_unlock_irqrestore (lock, flags); +return atomic64_read (&vm->tlb_seq); } --2.34.1
Quoted 8 lines · Christian König
It can happen that we query the sequence value before the callback had a chance to run.
Work around that by grabbing the fence lock and releasing it again. Should be replaced by hw handling soon.
Signed-off-by Christian König <[email protected]>Acked-by Alex Deucher <[email protected]>
You probably also want to add these tags:
Quoted 30 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +15
-0@@ -485,6 +485,21 @@ void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m);*/ static inline uint64_t amdgpu_vm_tlb_seq (struct amdgpu_vm *vm) { +unsigned long flags; +spinlock_t *lock; + +/* +* Work around to stop racing between the fence signaling and handling +* the cb. The lock is static after initially setting it up, just make +* sure that the dma_fence structure isn't freed up. +*/ +rcu_read_lock (); + lock = vm->last_tlb_flush->lock; +rcu_read_unlock (); + +spin_lock_irqsave (lock, flags); +spin_unlock_irqrestore (lock, flags); +return atomic64_read (&vm->tlb_seq); }
Quoted 5 lines · Christian König
It can happen that we query the sequence value before the callback had a chance to run.
Work around that by grabbing the fence lock and releasing it again. Should be replaced by hw handling soon.
kfd_flush_tlb is always called after waiting for map/unmap to GPU fence signalled, that means the callback is already executed and the sequence is increased if tlb flush is needed, so no such race from KFD.
I am not sure but seems the race does exist for amdgpu to grab vm and schedule job.
Quoted 31 lines
Signed-off-by Christian König <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +15
-0@@ -485,6 +485,21 @@ void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m);*/ static inline uint64_t amdgpu_vm_tlb_seq (struct amdgpu_vm *vm) { +unsigned long flags; +spinlock_t *lock; + +/* +* Work around to stop racing between the fence signaling and handling +* the cb. The lock is static after initially setting it up, just make +* sure that the dma_fence structure isn't freed up. +*/ +rcu_read_lock (); + lock = vm->last_tlb_flush->lock; +rcu_read_unlock (); + +spin_lock_irqsave (lock, flags); +spin_unlock_irqrestore (lock, flags); +return atomic64_read (&vm->tlb_seq); }
Am 03.11.22 um 22:18 schrieb Philip Yang:
Quoted 9 lines · Christian König
Quoted 5 lines
It can happen that we query the sequence value before the callback had a chance to run.
Work around that by grabbing the fence lock and releasing it again. Should be replaced by hw handling soon.
kfd_flush_tlbis always called after waiting for map/unmap to GPU fence signalled, that means the callback is already executed
And exactly that's incorrect.
Waiting for the fence to signal means that the callback has started executing, but it doesn't mean that it is finished.
This can then result in one CPU racing with the callback handler and because of this you see the wrong TLB seq.
Regards,
Christian.
Quoted 39 lines
and the sequence is increased if tlb flush is needed, so no such race from KFD.
I am not sure but seems the race does exist for amdgpu to grab vm and schedule job.
Acked-by Philip Yang <[email protected]>Quoted 33 lines
Signed-off-by Christian König <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h| 15 +++++++++++++++1 file changed, 15 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +5
-0@@ -485,6 +485,21 @@ void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm*vm, struct seq_file *m); */ static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm) { + unsigned long flags; + spinlock_t *lock; + + /* +* Work around to stop racing between the fence signaling and handling
+ * the cb. The lock is static after initially setting it up, just make
+ * sure that thedma_fencestructure isn't freed up. + */
+rcu_read_lock();
+ lock =vm->last_tlb_flush->lock;
+rcu_read_unlock();drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +4
-0+ + spin_lock_irqsave(lock, flags); + spin_unlock_irqrestore(lock, flags); +return
atomic64_read(&vm->tlb_seq);
}