Skip to contentSkip to conversation

drm/amdgpu: fix page fault by not skipping VM flush before tlb_seq update

PATCH In review chong li 3 participants 6 messages Nov 9 – Nov 16

Subsystems (2)
lore
Share & tools
lore

chong li

author
#0 lore raw

PATCH 1/1 drm/amdgpu: fix page fault by not skipping VM flush before tlb_seq update

amdgpu_vm_tlb_seq() waits for the tlb_seq callback by taking the lock of vm->last_tlb_flush. Newer page table updates replace vm->last_tlb_flush immediately, without waiting for older callbacks. A job scheduled after an older fence signals but before its callback finishes takes the newer fence's lock, while the callback runs under the older fence's own lock. The job misses that tlb_seq increment and can reuse a VMID whose TLB was last flushed before the older page table update completed, without flushing the TLB again.

how to fix:
Count the flush-requiring page table updates whose tlb_seq callback has not run yet, and request a VM flush when a job reuses a VMID while this count is non-zero.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 10 ++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 12 +++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h  | 10 ++++++++++
 3 files changed, 29 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +8 -2
index 9c879ac68352..b9e058da162b 100644
@@ -393,7 +393,8 @@ static int amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,
 	unsigned vmhub = ring->vm_hub;
 	uint64_t fence_context = adev->fence_context + ring->idx;
 	bool needs_flush = vm->use_cpu_for_update;
-	uint64_t updates = amdgpu_vm_tlb_seq(vm);
+	bool tlb_pending;
+	uint64_t updates = amdgpu_vm_tlb_seq_pending(vm, &tlb_pending);
 	int r;
 
 	*id = vm->reserved_vmid[vmhub];
@@ -405,6 +406,8 @@ static int amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,
 	     !dma_fence_is_signaled((*id)->last_flush)))
 		needs_flush = true;
 
+	needs_flush |= tlb_pending;
+
 	if ((*id)->owner != vm->immediate.fence_context ||
 	    (!adev->vm_manager.concurrent_flush && needs_flush)) {
 		struct dma_fence *tmp;
@@ -456,7 +459,8 @@ static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,
 	unsigned vmhub = ring->vm_hub;
 	struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
 	uint64_t fence_context = adev->fence_context + ring->idx;
-	uint64_t updates = amdgpu_vm_tlb_seq(vm);
+	bool tlb_pending;
+	uint64_t updates = amdgpu_vm_tlb_seq_pending(vm, &tlb_pending);
 	int r;
 
 	job->vm_needs_flush = vm->use_cpu_for_update;
@@ -480,6 +484,8 @@ static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,
 		if ((*id)->flushed_updates < updates)
 			needs_flush = true;
 
+		needs_flush |= tlb_pending;
+
 		if (needs_flush && !adev->vm_manager.concurrent_flush)
 			continue;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -1
index 2ba33212d507..6d15fd001a42 100644
@@ -1095,6 +1095,9 @@ static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
 
 	tlb_cb = container_of(cb, typeof(*tlb_cb), cb);
 	atomic64_inc(&tlb_cb->vm->tlb_seq);
+	/* Pairs with atomic_read_acquire() in amdgpu_vm_tlb_seq_pending() */
+	smp_mb__before_atomic();
+	atomic_dec(&tlb_cb->vm->tlb_pending);
 	kfree(tlb_cb);
 }
 
@@ -1281,9 +1284,16 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		start = tmp;
 	}
 
+	/* Must be visible before the update fence can signal */
+	if (params.needs_flush)
+		atomic_inc(&vm->tlb_pending);
+
 	r = vm->update_funcs->commit(&params, fence);
-	if (r)
+	if (r) {
+		if (params.needs_flush)
+			atomic_dec(&vm->tlb_pending);
 		goto error_free;
+	}
 
 	if (params.needs_flush) {
 		amdgpu_vm_tlb_flush(&params, fence, tlb_cb);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +10 -0
index d8e17f493f61..312fd0d268ce 100644
@@ -337,6 +337,8 @@ struct amdgpu_vm {
 
 	/* Last finished delayed update */
 	atomic64_t		tlb_seq;
+	/* Flush updates whose tlb_seq callback has not run yet */
+	atomic_t		tlb_pending;
 	struct dma_fence	*last_tlb_flush;
 	atomic64_t		kfd_last_flushed_seq;
 	uint64_t		tlb_fence_context;
@@ -578,6 +580,14 @@ static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
 	return atomic64_read(&vm->tlb_seq);
 }
 
+/* Read tlb_pending first; pairs with the barrier in amdgpu_vm_tlb_seq_cb() */
+static inline uint64_t amdgpu_vm_tlb_seq_pending(struct amdgpu_vm *vm,
+						 bool *pending)
+{
+	*pending = atomic_read_acquire(&vm->tlb_pending) != 0;
+	return amdgpu_vm_tlb_seq(vm);
+}
+
 void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
 				  unsigned int pasid,
 				  uint64_t addr,
Signature · 1 line
2.48.1
maintainer
#1 lore raw
Quoted 8 lines · chong li

amdgpu_vm_tlb_seq() waits for the tlb_seq callback by taking the lock of vm->last_tlb_flush. Newer page table updates replace vm->last_tlb_flush immediately, without waiting for older callbacks. A job scheduled after an older fence signals but before its callback finishes takes the newer fence's lock, while the callback runs under the older fence's own lock. The job misses that tlb_seq increment and can reuse a VMID whose TLB was last flushed before the older page table update completed, without flushing the TLB again.

That's not even remotely correct.

All fences use the same lock, that's why this works correctly.

Regards,
Christian.

Quoted 115 lines

how to fix:
Count the flush-requiring page table updates whose tlb_seq callback has not run yet, and request a VM flush when a job reuses a VMID while this count is non-zero.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 10 ++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 12 +++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h  | 10 ++++++++++
 3 files changed, 29 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +8 -2
index 9c879ac68352..b9e058da162b 100644
@@ -393,7 +393,8 @@ static int amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,
 	unsigned vmhub = ring->vm_hub;
 	uint64_t fence_context = adev->fence_context + ring->idx;
 	bool needs_flush = vm->use_cpu_for_update;
-	uint64_t updates = amdgpu_vm_tlb_seq(vm);
+	bool tlb_pending;
+	uint64_t updates = amdgpu_vm_tlb_seq_pending(vm, &tlb_pending);
 	int r;
 
 	*id = vm->reserved_vmid[vmhub];
@@ -405,6 +406,8 @@ static int amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,
 	     !dma_fence_is_signaled((*id)->last_flush)))
 		needs_flush = true;
 
+	needs_flush |= tlb_pending;
+
 	if ((*id)->owner != vm->immediate.fence_context ||
 	    (!adev->vm_manager.concurrent_flush && needs_flush)) {
 		struct dma_fence *tmp;
@@ -456,7 +459,8 @@ static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,
 	unsigned vmhub = ring->vm_hub;
 	struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
 	uint64_t fence_context = adev->fence_context + ring->idx;
-	uint64_t updates = amdgpu_vm_tlb_seq(vm);
+	bool tlb_pending;
+	uint64_t updates = amdgpu_vm_tlb_seq_pending(vm, &tlb_pending);
 	int r;
 
 	job->vm_needs_flush = vm->use_cpu_for_update;
@@ -480,6 +484,8 @@ static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,
 		if ((*id)->flushed_updates < updates)
 			needs_flush = true;
 
+		needs_flush |= tlb_pending;
+
 		if (needs_flush && !adev->vm_manager.concurrent_flush)
 			continue;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -1
index 2ba33212d507..6d15fd001a42 100644
@@ -1095,6 +1095,9 @@ static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
 
 	tlb_cb = container_of(cb, typeof(*tlb_cb), cb);
 	atomic64_inc(&tlb_cb->vm->tlb_seq);
+	/* Pairs with atomic_read_acquire() in amdgpu_vm_tlb_seq_pending() */
+	smp_mb__before_atomic();
+	atomic_dec(&tlb_cb->vm->tlb_pending);
 	kfree(tlb_cb);
 }
 
@@ -1281,9 +1284,16 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		start = tmp;
 	}
 
+	/* Must be visible before the update fence can signal */
+	if (params.needs_flush)
+		atomic_inc(&vm->tlb_pending);
+
 	r = vm->update_funcs->commit(&params, fence);
-	if (r)
+	if (r) {
+		if (params.needs_flush)
+			atomic_dec(&vm->tlb_pending);
 		goto error_free;
+	}
 
 	if (params.needs_flush) {
 		amdgpu_vm_tlb_flush(&params, fence, tlb_cb);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +10 -0
index d8e17f493f61..312fd0d268ce 100644
@@ -337,6 +337,8 @@ struct amdgpu_vm {
 
 	/* Last finished delayed update */
 	atomic64_t		tlb_seq;
+	/* Flush updates whose tlb_seq callback has not run yet */
+	atomic_t		tlb_pending;
 	struct dma_fence	*last_tlb_flush;
 	atomic64_t		kfd_last_flushed_seq;
 	uint64_t		tlb_fence_context;
@@ -578,6 +580,14 @@ static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
 	return atomic64_read(&vm->tlb_seq);
 }
 
+/* Read tlb_pending first; pairs with the barrier in amdgpu_vm_tlb_seq_cb() */
+static inline uint64_t amdgpu_vm_tlb_seq_pending(struct amdgpu_vm *vm,
+						 bool *pending)
+{
+	*pending = atomic_read_acquire(&vm->tlb_pending) != 0;
+	return amdgpu_vm_tlb_seq(vm);
+}
+
 void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
 				  unsigned int pasid,
 				  uint64_t addr,
#2 lore raw

AMD General

Hi, Christian.

The lock of each fence indeed never changes.
Each PT update fence has its own lock.

The issue is in the fence locking process.

When a fence signals, dma_fence_signal() holds the fence's lock and calls amdgpu_vm_tlb_seq_cb(), which increments vm->tlb_seq. amdgpu_vm_tlb_seq() waits on the lock of vm->last_tlb_flush until amdgpu_vm_tlb_seq_cb() completes. The lock of vm->last_tlb_flush ensures that amdgpu_vm_tlb_seq() returns the incremented vm->tlb_seq. This is the normal flow.

But vm->last_tlb_flush may already point to a new fence:
amdgpu_vm_tlb_flush() replaces it as soon as a new PT update is submitted, usually before the old fence signals. amdgpu_vm_tlb_seq() then takes the new fence's lock, which is not held while the old fence's callback runs, so it is not blocked and returns vm->tlb_seq before it is incremented.

Finally, updates = amdgpu_vm_tlb_seq() misses the increment, the condition (*id)->flushed_updates < updates is not satisfied, and needs_flush is false, so the job reuses the VMID without a VM flush.

Please let me know if anything is unclear or if I'm missing something.

Thanks,
Chong.

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, November 9, 2026 10:08 PM
To: Li, Chong(Alan) <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: fix page fault by not skipping VM flush before tlb_seq update

Quoted 10 lines · chong li

amdgpu_vm_tlb_seq() waits for the tlb_seq callback by taking the lock of
vm->last_tlb_flush. Newer page table updates replace vm->vm->last_tlb_flush
immediately, without waiting for older callbacks. A job scheduled after an older fence signals but before its callback finishes takes the newer fence's lock, while the callback runs under the older fence's own lock. The job misses that tlb_seq increment and can reuse a VMID whose TLB was last flushed before the older page table update completed, without flushing the TLB again.

That's not even remotely correct.

All fences use the same lock, that's why this works correctly.

Regards,
Christian.

Quoted 119 lines

how to fix:
Count the flush-requiring page table updates whose tlb_seq callback has not run yet, and request a VM flush when a job reuses a VMID while this count is non-zero.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 10 ++++++++--
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 12 +++++++++++-
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h  | 10 ++++++++++
 3 files changed, 29 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +6 -2
index 9c879ac68352..b9e058da162b 100644
@@ -393,7 +393,8 @@ static int amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,
      unsigned vmhub = ring->vm_hub;
      uint64_t fence_context = adev->fence_context + ring->idx;
      bool needs_flush = vm->use_cpu_for_update;
-     uint64_t updates = amdgpu_vm_tlb_seq(vm);
+     bool tlb_pending;
+     uint64_t updates = amdgpu_vm_tlb_seq_pending(vm, &tlb_pending);
      int r;
 
      *id = vm->reserved_vmid[vmhub];
@@ -405,6 +406,8 @@ static int amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,
           !dma_fence_is_signaled((*id)->last_flush)))
              needs_flush = true;
 
+     needs_flush |= tlb_pending;
+
      if ((*id)->owner != vm->immediate.fence_context ||
          (!adev->vm_manager.concurrent_flush && needs_flush)) {
              struct dma_fence *tmp;
@@ -456,7 +459,8 @@ static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,
      unsigned vmhub = ring->vm_hub;
      struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
      uint64_t fence_context = adev->fence_context + ring->idx;
-     uint64_t updates = amdgpu_vm_tlb_seq(vm);
+     bool tlb_pending;
+     uint64_t updates = amdgpu_vm_tlb_seq_pending(vm, &tlb_pending);
      int r;
 
      job->vm_needs_flush = vm->use_cpu_for_update; @@ -480,6 +484,8 @@

static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,

              if ((*id)->flushed_updates < updates)
                      needs_flush = true;
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +2 -0
+             needs_flush |= tlb_pending;
+
              if (needs_flush && !adev->vm_manager.concurrent_flush)
                      continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -1
index 2ba33212d507..6d15fd001a42 100644
@@ -1095,6 +1095,9 @@ static void amdgpu_vm_tlb_seq_cb(struct
 dma_fence *fence,
 
      tlb_cb = container_of(cb, typeof(*tlb_cb), cb);
      atomic64_inc(&tlb_cb->vm->tlb_seq);
+     /* Pairs with atomic_read_acquire() in amdgpu_vm_tlb_seq_pending() */
+     smp_mb__before_atomic();
+     atomic_dec(&tlb_cb->vm->tlb_pending);
      kfree(tlb_cb);
 }
@@ -1281,9 +1284,16 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
              start = tmp;
      }
 
+     /* Must be visible before the update fence can signal */
+     if (params.needs_flush)
+             atomic_inc(&vm->tlb_pending);
+
      r = vm->update_funcs->commit(&params, fence);
-     if (r)
+     if (r) {
+             if (params.needs_flush)
+                     atomic_dec(&vm->tlb_pending);
              goto error_free;
+     }
 
      if (params.needs_flush) {
              amdgpu_vm_tlb_flush(&params, fence, tlb_cb); diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
index d8e17f493f61..312fd0d268ce 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +10 -0
@@ -337,6 +337,8 @@ struct amdgpu_vm {
 
      /* Last finished delayed update */
      atomic64_t              tlb_seq;
+     /* Flush updates whose tlb_seq callback has not run yet */
+     atomic_t                tlb_pending;
      struct dma_fence        *last_tlb_flush;
      atomic64_t              kfd_last_flushed_seq;
      uint64_t                tlb_fence_context;
@@ -578,6 +580,14 @@ static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
      return atomic64_read(&vm->tlb_seq);
 }
 
+/* Read tlb_pending first; pairs with the barrier in
+amdgpu_vm_tlb_seq_cb() */ static inline uint64_t amdgpu_vm_tlb_seq_pending(struct amdgpu_vm *vm,
+                                              bool *pending)
+{
+     *pending = atomic_read_acquire(&vm->tlb_pending) != 0;
+     return amdgpu_vm_tlb_seq(vm);
+}
+
 void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
                                unsigned int pasid,
                                uint64_t addr,
maintainer
#3 lore raw

Hi Chong,

Quoted 16 lines · Chong(Alan)

AMD General

Hi, Christian.

The lock of each fence indeed never changes.
Each PT update fence has its own lock.

The issue is in the fence locking process.

When a fence signals, dma_fence_signal() holds the fence's lock and calls amdgpu_vm_tlb_seq_cb(), which increments vm->tlb_seq. amdgpu_vm_tlb_seq() waits on the lock of vm->last_tlb_flush until amdgpu_vm_tlb_seq_cb() completes. The lock of vm->last_tlb_flush ensures that amdgpu_vm_tlb_seq() returns the incremented vm->tlb_seq. This is the normal flow.

Quoted 6 lines

But vm->last_tlb_flush may already point to a new fence:
amdgpu_vm_tlb_flush() replaces it as soon as a new PT update is submitted, usually before the old fence signals. amdgpu_vm_tlb_seq() then takes the new fence's lock, which is not held while the old fence's callback runs, so it is not blocked and returns vm->tlb_seq before it is incremented.

No you are mixing things up, vm->last_tlb_flush was something completely different.

vm->last_seq_flush is the fence where we install the callback on and yes that is replaced with the next installed callback, but that doesn't matter the lock is always the same.

vm->last_tlb_inv is the TLB invalidation fence which actively invalidates the TLB for a passid.

That are two completely different things.

Regards,
Christian.

Quoted 160 lines

Finally, updates = amdgpu_vm_tlb_seq() misses the increment, the condition (*id)->flushed_updates < updates is not satisfied, and needs_flush is false, so the job reuses the VMID without a VM flush.

Please let me know if anything is unclear or if I'm missing something.

Thanks,
Chong.

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, November 9, 2026 10:08 PM
To: Li, Chong(Alan) <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: fix page fault by not skipping VM flush before tlb_seq update

Quoted 10 lines · chong li

amdgpu_vm_tlb_seq() waits for the tlb_seq callback by taking the lock of
vm->last_tlb_flush. Newer page table updates replace vm->vm->last_tlb_flush
immediately, without waiting for older callbacks. A job scheduled after an older fence signals but before its callback finishes takes the newer fence's lock, while the callback runs under the older fence's own lock. The job misses that tlb_seq increment and can reuse a VMID whose TLB was last flushed before the older page table update completed, without flushing the TLB again.

That's not even remotely correct.

All fences use the same lock, that's why this works correctly.

Regards,
Christian.

Quoted 119 lines

how to fix:
Count the flush-requiring page table updates whose tlb_seq callback has not run yet, and request a VM flush when a job reuses a VMID while this count is non-zero.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 10 ++++++++--
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 12 +++++++++++-
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h  | 10 ++++++++++
 3 files changed, 29 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +6 -2
index 9c879ac68352..b9e058da162b 100644
@@ -393,7 +393,8 @@ static int amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,
      unsigned vmhub = ring->vm_hub;
      uint64_t fence_context = adev->fence_context + ring->idx;
      bool needs_flush = vm->use_cpu_for_update;
-     uint64_t updates = amdgpu_vm_tlb_seq(vm);
+     bool tlb_pending;
+     uint64_t updates = amdgpu_vm_tlb_seq_pending(vm, &tlb_pending);
      int r;
 
      *id = vm->reserved_vmid[vmhub];
@@ -405,6 +406,8 @@ static int amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,
           !dma_fence_is_signaled((*id)->last_flush)))
              needs_flush = true;
 
+     needs_flush |= tlb_pending;
+
      if ((*id)->owner != vm->immediate.fence_context ||
          (!adev->vm_manager.concurrent_flush && needs_flush)) {
              struct dma_fence *tmp;
@@ -456,7 +459,8 @@ static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,
      unsigned vmhub = ring->vm_hub;
      struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
      uint64_t fence_context = adev->fence_context + ring->idx;
-     uint64_t updates = amdgpu_vm_tlb_seq(vm);
+     bool tlb_pending;
+     uint64_t updates = amdgpu_vm_tlb_seq_pending(vm, &tlb_pending);
      int r;
 
      job->vm_needs_flush = vm->use_cpu_for_update; @@ -480,6 +484,8 @@

static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,

              if ((*id)->flushed_updates < updates)
                      needs_flush = true;
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +2 -0
+             needs_flush |= tlb_pending;
+
              if (needs_flush && !adev->vm_manager.concurrent_flush)
                      continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -1
index 2ba33212d507..6d15fd001a42 100644
@@ -1095,6 +1095,9 @@ static void amdgpu_vm_tlb_seq_cb(struct
 dma_fence *fence,
 
      tlb_cb = container_of(cb, typeof(*tlb_cb), cb);
      atomic64_inc(&tlb_cb->vm->tlb_seq);
+     /* Pairs with atomic_read_acquire() in amdgpu_vm_tlb_seq_pending() */
+     smp_mb__before_atomic();
+     atomic_dec(&tlb_cb->vm->tlb_pending);
      kfree(tlb_cb);
 }
@@ -1281,9 +1284,16 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
              start = tmp;
      }
 
+     /* Must be visible before the update fence can signal */
+     if (params.needs_flush)
+             atomic_inc(&vm->tlb_pending);
+
      r = vm->update_funcs->commit(&params, fence);
-     if (r)
+     if (r) {
+             if (params.needs_flush)
+                     atomic_dec(&vm->tlb_pending);
              goto error_free;
+     }
 
      if (params.needs_flush) {
              amdgpu_vm_tlb_flush(&params, fence, tlb_cb); diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
index d8e17f493f61..312fd0d268ce 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +10 -0
@@ -337,6 +337,8 @@ struct amdgpu_vm {
 
      /* Last finished delayed update */
      atomic64_t              tlb_seq;
+     /* Flush updates whose tlb_seq callback has not run yet */
+     atomic_t                tlb_pending;
      struct dma_fence        *last_tlb_flush;
      atomic64_t              kfd_last_flushed_seq;
      uint64_t                tlb_fence_context;
@@ -578,6 +580,14 @@ static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
      return atomic64_read(&vm->tlb_seq);
 }
 
+/* Read tlb_pending first; pairs with the barrier in
+amdgpu_vm_tlb_seq_cb() */ static inline uint64_t amdgpu_vm_tlb_seq_pending(struct amdgpu_vm *vm,
+                                              bool *pending)
+{
+     *pending = atomic_read_acquire(&vm->tlb_pending) != 0;
+     return amdgpu_vm_tlb_seq(vm);
+}
+
 void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
                                unsigned int pasid,
                                uint64_t addr,
#4 lore raw

AMD General

Hi, Christian.

Sorry for the naming confusion. My patch is based on a tree before your commit 27911d89ae30 ("drm/amdgpu: fix TLB invalidation fence handling v4"), which renamed vm->last_tlb_flush to vm->last_seq_flush.

vm->last_tlb_inv is not involved: in my reproducer ('Sascha Willems' Vulkan examples in benchmark mode), logging shows that vm->need_tlb_fence stays false (user queues not enabled, no KFD) and vm->last_tlb_inv stays the signaled stub from dma_fence_get_stub().

The root cause is a window between the fence being signaled and its callback completing tlb_seq++, which the lock in amdgpu_vm_tlb_seq() does not always cover.

If a job that depends on this page table update executes this line during that window:

    uint64_t updates = amdgpu_vm_tlb_seq(vm);

updates still holds the previous tlb_seq, because the callback has not had a chance to increment tlb_seq yet. If another job flushed this VMID after the update was submitted but before it completed, (*id)->flushed_updates already equals updates. So the condition (*id)->flushed_updates < updates is not met, and needs_flush is false.

Moreover, taking the lock in amdgpu_vm_tlb_seq():

    lock = dma_fence_spinlock(vm->last_seq_flush);

does not always wait for that callback. Each scheduler fence has its own lock, and by the time the signaled fence's callback runs, a new page table update may have been submitted, so vm->last_seq_flush is already the new fence, with a different lock.

48689.629231: amdgpu_vmid_grab: tlb_seq: vm 00000000f0406e73 last_seq_flush 00000000ed6892e2 lock 0000000070a2e519 unlocked at 48689629229784 ns, tlb_seq 34 48689.629232: amdgpu_vmid_grab: tlb_seq_vmid: vm 00000000f0406e73 pasid 24366 vmhub 0 vmid 7 ring comp_1.1.0 sched_fence 201888:7 updates 34 flushed_updates 34 needs_flush 0 vm_needs_flush 0 48689.629235: amdgpu_vm_flush: tlb_seq_emit: pasid 24366 vmhub 0 vmid 7 ring comp_1.1.0 sched_fence 201888:7 vm_needs_flush 0 vm_flush_needed 0
48689.629410: amdgpu_vm_tlb_seq_cb: tlb_seq_cb: vm 00000000f0406e73 fence 00000000f48cae4d lock 00000000fcae5430 signaled at 48689629208744 ns, last_seq_flush 00000000ed6892e2, tlb_seq 34 -> 35 48689.629615: amdgpu_vm_tlb_seq_cb: tlb_seq_cb: vm 00000000f0406e73 fence 00000000ed6892e2 lock 0000000070a2e519 signaled at 48689629413834 ns, last_seq_flush 00000000ed6892e2, tlb_seq 35 -> 36 48689.694858: console: amdgpu 0000:05:00.0: [gfxhub] page fault (src_id:0 ring:8 vmid:7 pasid:24366)
amdgpu 0000:05:00.0: in page starting at address 0x0000000401400000 from client 10

- Fence A (00000000f48cae4d) is signaled at 48689629208744 ns, the "signaled at" value in its tlb_seq_cb line at 48689.629410. Its callback runs under lock 00000000fcae5430.
- 21 us later, at 48689629229784 ns, job J (sched_fence 201888:7) reads tlb_seq 34 under the lock of fence B (00000000ed6892e2, lock 0000000070a2e519), not the lock of fence A (00000000fcae5430). Job J sees updates == flushed_updates == 34 and skips the flush.
- The callback of fence A increments tlb_seq 34 -> 35 only at 48689.629410, after job J's decision.
- Job J faults at 0x0000000401400000 on vmid 7

Thanks,
Chong.

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, November 10, 2026 6:13 PM
To: Li, Chong(Alan) <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: fix page fault by not skipping VM flush before tlb_seq update

Hi Chong,

Quoted 16 lines · Chong(Alan)

AMD General

Hi, Christian.

The lock of each fence indeed never changes.
Each PT update fence has its own lock.

The issue is in the fence locking process.

When a fence signals, dma_fence_signal() holds the fence's lock and calls amdgpu_vm_tlb_seq_cb(), which increments vm->tlb_seq. amdgpu_vm_tlb_seq() waits on the lock of vm->last_tlb_flush until amdgpu_vm_tlb_seq_cb() completes. The lock of vm->last_tlb_flush ensures that amdgpu_vm_tlb_seq() returns the incremented vm->tlb_seq. This is the normal flow.

Quoted 6 lines

But vm->last_tlb_flush may already point to a new fence:
amdgpu_vm_tlb_flush() replaces it as soon as a new PT update is submitted, usually before the old fence signals. amdgpu_vm_tlb_seq() then takes the new fence's lock, which is not held while the old fence's callback runs, so it is not blocked and returns vm->tlb_seq before it is incremented.

No you are mixing things up, vm->last_tlb_flush was something completely different.

vm->last_seq_flush is the fence where we install the callback on and yes that is replaced with the next installed callback, but that doesn't matter the lock is always the same.

vm->last_tlb_inv is the TLB invalidation fence which actively invalidates the TLB for a passid.

That are two completely different things.

Regards,
Christian.

Quoted 159 lines

Finally, updates = amdgpu_vm_tlb_seq() misses the increment, the condition (*id)->flushed_updates < updates is not satisfied, and needs_flush is false, so the job reuses the VMID without a VM flush.

Please let me know if anything is unclear or if I'm missing something.

Thanks,
Chong.

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, November 9, 2026 10:08 PM
To: Li, Chong(Alan) <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: fix page fault by not skipping VM flush before tlb_seq update

Quoted 10 lines · chong li

amdgpu_vm_tlb_seq() waits for the tlb_seq callback by taking the lock of
vm->last_tlb_flush. Newer page table updates replace vm->vm->last_tlb_flush
immediately, without waiting for older callbacks. A job scheduled after an older fence signals but before its callback finishes takes the newer fence's lock, while the callback runs under the older fence's own lock. The job misses that tlb_seq increment and can reuse a VMID whose TLB was last flushed before the older page table update completed, without flushing the TLB again.

That's not even remotely correct.

All fences use the same lock, that's why this works correctly.

Regards,
Christian.

Quoted 117 lines

how to fix:
Count the flush-requiring page table updates whose tlb_seq callback has not run yet, and request a VM flush when a job reuses a VMID while this count is non-zero.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 10 ++++++++--
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 12 +++++++++++-
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h  | 10 ++++++++++
 3 files changed, 29 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +2 -1
index 9c879ac68352..b9e058da162b 100644
@@ -393,7 +393,8 @@ static int amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,
      unsigned vmhub = ring->vm_hub;
      uint64_t fence_context = adev->fence_context + ring->idx;
      bool needs_flush = vm->use_cpu_for_update;
-     uint64_t updates = amdgpu_vm_tlb_seq(vm);
+     bool tlb_pending;
+     uint64_t updates = amdgpu_vm_tlb_seq_pending(vm, &tlb_pending);
      int r;
 
      *id = vm->reserved_vmid[vmhub]; @@ -405,6 +406,8 @@ static int

amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,

           !dma_fence_is_signaled((*id)->last_flush)))
              needs_flush = true;
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +4 -1
+     needs_flush |= tlb_pending;
+
      if ((*id)->owner != vm->immediate.fence_context ||
          (!adev->vm_manager.concurrent_flush && needs_flush)) {
              struct dma_fence *tmp;
@@ -456,7 +459,8 @@ static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,
      unsigned vmhub = ring->vm_hub;
      struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
      uint64_t fence_context = adev->fence_context + ring->idx;
-     uint64_t updates = amdgpu_vm_tlb_seq(vm);
+     bool tlb_pending;
+     uint64_t updates = amdgpu_vm_tlb_seq_pending(vm, &tlb_pending);
      int r;
 
      job->vm_needs_flush = vm->use_cpu_for_update; @@ -480,6 +484,8

@@ static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,

              if ((*id)->flushed_updates < updates)
                      needs_flush = true;
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +2 -0
+             needs_flush |= tlb_pending;
+
              if (needs_flush && !adev->vm_manager.concurrent_flush)
                      continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -1
index 2ba33212d507..6d15fd001a42 100644
@@ -1095,6 +1095,9 @@ static void amdgpu_vm_tlb_seq_cb(struct
 dma_fence *fence,
 
      tlb_cb = container_of(cb, typeof(*tlb_cb), cb);
      atomic64_inc(&tlb_cb->vm->tlb_seq);
+     /* Pairs with atomic_read_acquire() in amdgpu_vm_tlb_seq_pending() */
+     smp_mb__before_atomic();
+     atomic_dec(&tlb_cb->vm->tlb_pending);
      kfree(tlb_cb);
 }
@@ -1281,9 +1284,16 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
              start = tmp;
      }
 
+     /* Must be visible before the update fence can signal */
+     if (params.needs_flush)
+             atomic_inc(&vm->tlb_pending);
+
      r = vm->update_funcs->commit(&params, fence);
-     if (r)
+     if (r) {
+             if (params.needs_flush)
+                     atomic_dec(&vm->tlb_pending);
              goto error_free;
+     }
 
      if (params.needs_flush) {
              amdgpu_vm_tlb_flush(&params, fence, tlb_cb); diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
index d8e17f493f61..312fd0d268ce 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +9 -0
@@ -337,6 +337,8 @@ struct amdgpu_vm {
 
      /* Last finished delayed update */
      atomic64_t              tlb_seq;
+     /* Flush updates whose tlb_seq callback has not run yet */
+     atomic_t                tlb_pending;
      struct dma_fence        *last_tlb_flush;
      atomic64_t              kfd_last_flushed_seq;
      uint64_t                tlb_fence_context;
@@ -578,6 +580,14 @@ static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
      return atomic64_read(&vm->tlb_seq);  }
 
+/* Read tlb_pending first; pairs with the barrier in
+amdgpu_vm_tlb_seq_cb() */ static inline uint64_t amdgpu_vm_tlb_seq_pending(struct amdgpu_vm *vm,
+                                              bool *pending) {
+     *pending = atomic_read_acquire(&vm->tlb_pending) != 0;
+     return amdgpu_vm_tlb_seq(vm);
+}
+
 void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
                                unsigned int pasid,
                                uint64_t addr,
maintainer
#5 lore raw
Quoted 29 lines · Chong(Alan)

AMD General

Hi, Christian.

Sorry for the naming confusion. My patch is based on a tree before your commit 27911d89ae30 ("drm/amdgpu: fix TLB invalidation fence handling v4"), which renamed vm->last_tlb_flush to vm->last_seq_flush.

vm->last_tlb_inv is not involved: in my reproducer ('Sascha Willems' Vulkan examples in benchmark mode), logging shows that vm->need_tlb_fence stays false (user queues not enabled, no KFD) and vm->last_tlb_inv stays the signaled stub from dma_fence_get_stub().

The root cause is a window between the fence being signaled and its callback completing tlb_seq++, which the lock in amdgpu_vm_tlb_seq() does not always cover.

If a job that depends on this page table update executes this line during that window:

    uint64_t updates = amdgpu_vm_tlb_seq(vm);

updates still holds the previous tlb_seq, because the callback has not had a chance to increment tlb_seq yet. If another job flushed this VMID after the update was submitted but before it completed, (*id)->flushed_updates already equals updates. So the condition (*id)->flushed_updates < updates is not met, and needs_flush is false.

Quoted 6 lines

Moreover, taking the lock in amdgpu_vm_tlb_seq():

    lock = dma_fence_spinlock(vm->last_seq_flush);

does not always wait for that callback. Each scheduler fence has its own lock

Well that is an intersting point.

, and by the time the signaled fence's callback runs, a new

Quoted 2 lines

page table update may have been submitted, so vm->last_seq_flush is already the new fence, with a different lock.

Ah! You mean that the callbacks are still processing because we wait for the lock of the scheduler fence and not the HW fence.

Yeah that could indeed be a problem.

I tried to make the sequence number of writeback a few years ago to avoid such issues, but that never worked correctly either.

I think it is probably better to switch the whole handling to fence chains instead of the hack you came up below.

Regards,
Christian.

Quoted 226 lines

48689.629231: amdgpu_vmid_grab: tlb_seq: vm 00000000f0406e73 last_seq_flush 00000000ed6892e2 lock 0000000070a2e519 unlocked at 48689629229784 ns, tlb_seq 34 48689.629232: amdgpu_vmid_grab: tlb_seq_vmid: vm 00000000f0406e73 pasid 24366 vmhub 0 vmid 7 ring comp_1.1.0 sched_fence 201888:7 updates 34 flushed_updates 34 needs_flush 0 vm_needs_flush 0 48689.629235: amdgpu_vm_flush: tlb_seq_emit: pasid 24366 vmhub 0 vmid 7 ring comp_1.1.0 sched_fence 201888:7 vm_needs_flush 0 vm_flush_needed 0
48689.629410: amdgpu_vm_tlb_seq_cb: tlb_seq_cb: vm 00000000f0406e73 fence 00000000f48cae4d lock 00000000fcae5430 signaled at 48689629208744 ns, last_seq_flush 00000000ed6892e2, tlb_seq 34 -> 35 48689.629615: amdgpu_vm_tlb_seq_cb: tlb_seq_cb: vm 00000000f0406e73 fence 00000000ed6892e2 lock 0000000070a2e519 signaled at 48689629413834 ns, last_seq_flush 00000000ed6892e2, tlb_seq 35 -> 36 48689.694858: console: amdgpu 0000:05:00.0: [gfxhub] page fault (src_id:0 ring:8 vmid:7 pasid:24366)
amdgpu 0000:05:00.0: in page starting at address 0x0000000401400000 from client 10

- Fence A (00000000f48cae4d) is signaled at 48689629208744 ns, the "signaled at" value in its tlb_seq_cb line at 48689.629410. Its callback runs under lock 00000000fcae5430.
- 21 us later, at 48689629229784 ns, job J (sched_fence 201888:7) reads tlb_seq 34 under the lock of fence B (00000000ed6892e2, lock 0000000070a2e519), not the lock of fence A (00000000fcae5430). Job J sees updates == flushed_updates == 34 and skips the flush.
- The callback of fence A increments tlb_seq 34 -> 35 only at 48689.629410, after job J's decision.
- Job J faults at 0x0000000401400000 on vmid 7

Thanks,
Chong.

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, November 10, 2026 6:13 PM
To: Li, Chong(Alan) <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: fix page fault by not skipping VM flush before tlb_seq update

Hi Chong,

Quoted 16 lines · Chong(Alan)

AMD General

Hi, Christian.

The lock of each fence indeed never changes.
Each PT update fence has its own lock.

The issue is in the fence locking process.

When a fence signals, dma_fence_signal() holds the fence's lock and calls amdgpu_vm_tlb_seq_cb(), which increments vm->tlb_seq. amdgpu_vm_tlb_seq() waits on the lock of vm->last_tlb_flush until amdgpu_vm_tlb_seq_cb() completes. The lock of vm->last_tlb_flush ensures that amdgpu_vm_tlb_seq() returns the incremented vm->tlb_seq. This is the normal flow.

Quoted 6 lines

But vm->last_tlb_flush may already point to a new fence:
amdgpu_vm_tlb_flush() replaces it as soon as a new PT update is submitted, usually before the old fence signals. amdgpu_vm_tlb_seq() then takes the new fence's lock, which is not held while the old fence's callback runs, so it is not blocked and returns vm->tlb_seq before it is incremented.

No you are mixing things up, vm->last_tlb_flush was something completely different.

vm->last_seq_flush is the fence where we install the callback on and yes that is replaced with the next installed callback, but that doesn't matter the lock is always the same.

vm->last_tlb_inv is the TLB invalidation fence which actively invalidates the TLB for a passid.

That are two completely different things.

Regards,
Christian.

Quoted 159 lines

Finally, updates = amdgpu_vm_tlb_seq() misses the increment, the condition (*id)->flushed_updates < updates is not satisfied, and needs_flush is false, so the job reuses the VMID without a VM flush.

Please let me know if anything is unclear or if I'm missing something.

Thanks,
Chong.

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, November 9, 2026 10:08 PM
To: Li, Chong(Alan) <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: fix page fault by not skipping VM flush before tlb_seq update

Quoted 10 lines · chong li

amdgpu_vm_tlb_seq() waits for the tlb_seq callback by taking the lock of
vm->last_tlb_flush. Newer page table updates replace vm->vm->last_tlb_flush
immediately, without waiting for older callbacks. A job scheduled after an older fence signals but before its callback finishes takes the newer fence's lock, while the callback runs under the older fence's own lock. The job misses that tlb_seq increment and can reuse a VMID whose TLB was last flushed before the older page table update completed, without flushing the TLB again.

That's not even remotely correct.

All fences use the same lock, that's why this works correctly.

Regards,
Christian.

Quoted 117 lines

how to fix:
Count the flush-requiring page table updates whose tlb_seq callback has not run yet, and request a VM flush when a job reuses a VMID while this count is non-zero.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 10 ++++++++--
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 12 +++++++++++-
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h  | 10 ++++++++++
 3 files changed, 29 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +2 -1
index 9c879ac68352..b9e058da162b 100644
@@ -393,7 +393,8 @@ static int amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,
      unsigned vmhub = ring->vm_hub;
      uint64_t fence_context = adev->fence_context + ring->idx;
      bool needs_flush = vm->use_cpu_for_update;
-     uint64_t updates = amdgpu_vm_tlb_seq(vm);
+     bool tlb_pending;
+     uint64_t updates = amdgpu_vm_tlb_seq_pending(vm, &tlb_pending);
      int r;
 
      *id = vm->reserved_vmid[vmhub]; @@ -405,6 +406,8 @@ static int

amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,

           !dma_fence_is_signaled((*id)->last_flush)))
              needs_flush = true;
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +4 -1
+     needs_flush |= tlb_pending;
+
      if ((*id)->owner != vm->immediate.fence_context ||
          (!adev->vm_manager.concurrent_flush && needs_flush)) {
              struct dma_fence *tmp;
@@ -456,7 +459,8 @@ static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,
      unsigned vmhub = ring->vm_hub;
      struct amdgpu_kq_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
      uint64_t fence_context = adev->fence_context + ring->idx;
-     uint64_t updates = amdgpu_vm_tlb_seq(vm);
+     bool tlb_pending;
+     uint64_t updates = amdgpu_vm_tlb_seq_pending(vm, &tlb_pending);
      int r;
 
      job->vm_needs_flush = vm->use_cpu_for_update; @@ -480,6 +484,8

@@ static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,

              if ((*id)->flushed_updates < updates)
                      needs_flush = true;
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +2 -0
+             needs_flush |= tlb_pending;
+
              if (needs_flush && !adev->vm_manager.concurrent_flush)
                      continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -1
index 2ba33212d507..6d15fd001a42 100644
@@ -1095,6 +1095,9 @@ static void amdgpu_vm_tlb_seq_cb(struct
 dma_fence *fence,
 
      tlb_cb = container_of(cb, typeof(*tlb_cb), cb);
      atomic64_inc(&tlb_cb->vm->tlb_seq);
+     /* Pairs with atomic_read_acquire() in amdgpu_vm_tlb_seq_pending() */
+     smp_mb__before_atomic();
+     atomic_dec(&tlb_cb->vm->tlb_pending);
      kfree(tlb_cb);
 }
@@ -1281,9 +1284,16 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
              start = tmp;
      }
 
+     /* Must be visible before the update fence can signal */
+     if (params.needs_flush)
+             atomic_inc(&vm->tlb_pending);
+
      r = vm->update_funcs->commit(&params, fence);
-     if (r)
+     if (r) {
+             if (params.needs_flush)
+                     atomic_dec(&vm->tlb_pending);
              goto error_free;
+     }
 
      if (params.needs_flush) {
              amdgpu_vm_tlb_flush(&params, fence, tlb_cb); diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
index d8e17f493f61..312fd0d268ce 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +9 -0
@@ -337,6 +337,8 @@ struct amdgpu_vm {
 
      /* Last finished delayed update */
      atomic64_t              tlb_seq;
+     /* Flush updates whose tlb_seq callback has not run yet */
+     atomic_t                tlb_pending;
      struct dma_fence        *last_tlb_flush;
      atomic64_t              kfd_last_flushed_seq;
      uint64_t                tlb_fence_context;
@@ -578,6 +580,14 @@ static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
      return atomic64_read(&vm->tlb_seq);  }
 
+/* Read tlb_pending first; pairs with the barrier in
+amdgpu_vm_tlb_seq_cb() */ static inline uint64_t amdgpu_vm_tlb_seq_pending(struct amdgpu_vm *vm,
+                                              bool *pending) {
+     *pending = atomic_read_acquire(&vm->tlb_pending) != 0;
+     return amdgpu_vm_tlb_seq(vm);
+}
+
 void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
                                unsigned int pasid,
                                uint64_t addr,