Skip to contentSkip to conversation

drm/amdgpu: workaround for TLB seq race

PATCH In review Christian König 4 participants 5 messages Nov 2, 2022 – Nov 4, 2022

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

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
index 9ecb7f663e19..e51a46c9582b 100644
@@ -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
Acked-by on v1 1/1
#1 lore raw
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.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -0
index 9ecb7f663e19..e51a46c9582b 100644

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

#2 lore raw
In reply to Christian König
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.

You probably also want to add these tags:

Fixes 5255e146c99a6 ("drm/amdgpu: rework TLB flushing")
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
index 9ecb7f663e19..e51a46c9582b 100644
@@ -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);
  }
 
Acked-by on v1 1/1
#3 lore raw
In reply to Christian König
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
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 15 +++++++++++++++
  1 file changed, 15 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +15 -0
index 9ecb7f663e19..e51a46c9582b 100644
@@ -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);
  }
 
#4 lore raw

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_tlb is 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.

Quoted 33 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 15 +++++++++++++++

  1 file changed, 15 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +5 -0
index 9ecb7f663e19..e51a46c9582b 100644
@@ -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();

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);
  }