Skip to contentSkip to conversation

drm/amdgpu: switch vm->tlb_seq to dma_fence_chain handling v2

PATCH In review Christian König 4 participants 5 messages Nov 25 – Nov 27

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

PATCH 1/1 drm/amdgpu: switch vm->tlb_seq to dma_fence_chain handling v2

The vm->tlb_seq handling for per VMID based TLB invalidation was always rather fragile and relied on grabbing the dma_fence spinlock to make sure that callbacks executed in the right order.

Alan now pointed out that even this doesn't work correctly because different PTDE/PTE updates might use different locks and their signaling is not necessarily in the same order and on the same GPU.

Ditch the whole approach, instead use a dma_fence_chain to track all PDE/PTE updates and just iterate over them to get the last completed one when asked. Also make sure that PDE updates use the same path as PTE updates for consistency.

This not only fixes rare GPU page faults in the kernel queues but also potentially issues with the user queues should a PDE update make a TLB invalidation necessary.

v2: also handle the case where the chain head is already signaled as
pointed out by Alan.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 173 +++++++++---------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  32 +---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c    |   3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c   |   3 -
 .../gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c  |   2 +-
 5 files changed, 94 insertions(+), 119 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +89 -84
index a627a30929903..2e15116e790f4 100644
@@ -27,6 +27,7 @@
  */
 
 #include <linux/dma-fence-array.h>
+#include <linux/dma-fence-chain.h>
 #include <linux/interval_tree_generic.h>
 #include <linux/idr.h>
 #include <linux/dma-buf.h>
@@ -114,21 +115,6 @@ struct amdgpu_prt_cb {
 	struct dma_fence_cb cb;
 };
 
-/**
- * struct amdgpu_vm_tlb_seq_struct - Helper to increment the TLB flush sequence
- */
-struct amdgpu_vm_tlb_seq_struct {
-	/**
-	 * @vm: pointer to the amdgpu_vm structure to set the fence sequence on
-	 */
-	struct amdgpu_vm *vm;
-
-	/**
-	 * @cb: callback
-	 */
-	struct dma_fence_cb cb;
-};
-
 /**
  * amdgpu_vm_assert_locked - check if VM is correctly locked
  * @vm: the VM which schould be tested
@@ -1026,6 +1012,71 @@ uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
 	return result;
 }
 
+/**
+ * amdgpu_vm_tlb_seq - return tlb flush sequence number
+ * @vm: the amdgpu_vm structure to query
+ *
+ * Returns the tlb flush sequence number which indicates that the VM TLBs needs
+ * to be invalidated whenever the sequence number change.
+ */
+uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
+{
+	struct dma_fence *fence;
+	u64 tlb_seq = 0;
+
+	rcu_read_lock();
+	fence = dma_fence_get_rcu_safe(&vm->last_seq_flush);
+	rcu_read_unlock();
+
+	if (dma_fence_is_signaled(dma_fence_chain_contained(fence))) {
+		tlb_seq = fence->seqno;
+		dma_fence_put(fence);
+	} else {
+		do {
+			tlb_seq = to_dma_fence_chain(fence)->prev_seqno;
+			fence = dma_fence_chain_walk(fence);
+		} while (fence);
+	}
+	return tlb_seq;
+}
+
+/**
+ * amdgpu_vm_tlb_flush - prepare TLB flush
+ *
+ * @params: parameters for update
+ * @chain: dma_fence_chain for the signaling callback
+ * @fence: input fence to sync TLB flush with
+ *
+ * Increments the tlb sequence to make sure that future CS execute a VM flush.
+ */
+static void
+amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
+		    struct dma_fence_chain *chain,
+		    struct dma_fence *fence)
+{
+	struct amdgpu_vm *vm = params->vm;
+	struct dma_fence *prev;
+
+	++vm->tlb_seq;
+	prev = rcu_dereference_protected(vm->last_seq_flush,
+					 lockdep_is_held(&vm->eviction_lock));
+	dma_fence_chain_init(chain, prev, dma_fence_get(fence), vm->tlb_seq);
+	rcu_assign_pointer(vm->last_seq_flush, &chain->base);
+
+	/* Prepare a TLB flush fence to be attached to PTs */
+	/* The check for need_tlb_fence should be dropped once we
+	 * sort out the issues with KIQ/MES TLB invalidation timeouts.
+	 */
+	if (!params->unlocked && vm->need_tlb_fence) {
+		amdgpu_vm_tlb_fence_create(params->adev, vm, fence,
+					   &vm->last_tlb_inv);
+
+		/* Makes sure no PD/PT is freed before the flush */
+		dma_resv_add_fence(vm->root.bo->tbo.base.resv,
+				   vm->last_tlb_inv, DMA_RESV_USAGE_BOOKKEEP);
+	}
+}
+
 /**
  * amdgpu_vm_update_pdes - make sure that all directories are valid
  *
@@ -1043,6 +1094,7 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
 {
 	struct amdgpu_vm_update_params params;
 	struct amdgpu_vm_bo_base *entry, *tmp;
+	struct dma_fence_chain *chain;
 	bool flush_tlb_needed = false;
 	int r, idx;
 
@@ -1054,11 +1106,21 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
 	if (!drm_dev_enter(adev_to_drm(adev), &idx))
 		return -ENODEV;
 
+	chain = dma_fence_chain_alloc();
+	if (!chain) {
+		r = -ENOMEM;
+		goto error_dev_exit;
+	}
+
 	memset(&params, 0, sizeof(params));
 	params.adev = adev;
 	params.vm = vm;
 	params.immediate = immediate;
 
+	r = amdgpu_vm_begin_critical(&params);
+	if (r)
+		goto error;
+
 	r = vm->update_funcs->prepare(&params, NULL,
 				      AMDGPU_KERNEL_JOB_ID_VM_UPDATE_PDES);
 	if (r)
@@ -1078,73 +1140,22 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
 		goto error;
 
 	if (flush_tlb_needed)
-		atomic64_inc(&vm->tlb_seq);
+		amdgpu_vm_tlb_flush(&params, chain, vm->last_update);
+	else
+		dma_fence_chain_free(chain);
 
 	list_for_each_entry_safe(entry, tmp, &vm->kernel.needs_update,
 				 vm_status)
 		amdgpu_vm_bo_idle(entry);
 
 error:
+	amdgpu_vm_end_critical(&params);
+
+error_dev_exit:
 	drm_dev_exit(idx);
 	return r;
 }
 
-/**
- * amdgpu_vm_tlb_seq_cb - make sure to increment tlb sequence
- * @fence: unused
- * @cb: the callback structure
- *
- * Increments the tlb sequence to make sure that future CS execute a VM flush.
- */
-static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
-				 struct dma_fence_cb *cb)
-{
-	struct amdgpu_vm_tlb_seq_struct *tlb_cb;
-
-	tlb_cb = container_of(cb, typeof(*tlb_cb), cb);
-	atomic64_inc(&tlb_cb->vm->tlb_seq);
-	kfree(tlb_cb);
-}
-
-/**
- * amdgpu_vm_tlb_flush - prepare TLB flush
- *
- * @params: parameters for update
- * @fence: input fence to sync TLB flush with
- * @tlb_cb: the callback structure
- *
- * Increments the tlb sequence to make sure that future CS execute a VM flush.
- */
-static void
-amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
-		    struct dma_fence *fence,
-		    struct amdgpu_vm_tlb_seq_struct *tlb_cb)
-{
-	struct amdgpu_vm *vm = params->vm;
-
-	tlb_cb->vm = vm;
-	if (fence && !dma_fence_add_callback(fence, &tlb_cb->cb,
-					     amdgpu_vm_tlb_seq_cb)) {
-		dma_fence_put(vm->last_seq_flush);
-		vm->last_seq_flush = dma_fence_get(fence);
-	} else {
-		amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
-	}
-
-	/* Prepare a TLB flush fence to be attached to PTs */
-	/* The check for need_tlb_fence should be dropped once we
-	 * sort out the issues with KIQ/MES TLB invalidation timeouts.
-	 */
-	if (!params->unlocked && vm->need_tlb_fence) {
-		amdgpu_vm_tlb_fence_create(params->adev, vm, fence,
-					   &vm->last_tlb_inv);
-
-		/* Makes sure no PD/PT is freed before the flush */
-		dma_resv_add_fence(vm->root.bo->tbo.base.resv,
-				   vm->last_tlb_inv, DMA_RESV_USAGE_BOOKKEEP);
-	}
-}
-
 /**
  * amdgpu_vm_update_range - update a range in the vm page table
  *
@@ -1177,16 +1188,16 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			   struct ttm_resource *res, dma_addr_t *pages_addr,
 			   struct dma_fence **fence)
 {
-	struct amdgpu_vm_tlb_seq_struct *tlb_cb;
 	struct amdgpu_vm_update_params params;
 	struct amdgpu_res_cursor cursor;
+	struct dma_fence_chain *chain;
 	int r, idx;
 
 	if (!drm_dev_enter(adev_to_drm(adev), &idx))
 		return -ENODEV;
 
-	tlb_cb = kmalloc_obj(*tlb_cb);
-	if (!tlb_cb) {
+	chain = dma_fence_chain_alloc();
+	if (!chain) {
 		drm_dev_exit(idx);
 		return -ENOMEM;
 	}
@@ -1291,14 +1302,14 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		goto error_free;
 
 	if (params.needs_flush) {
-		amdgpu_vm_tlb_flush(&params, *fence, tlb_cb);
-		tlb_cb = NULL;
+		amdgpu_vm_tlb_flush(&params, chain, *fence);
+		chain = NULL;
 	}
 
 	amdgpu_vm_pt_free_list(adev, &params);
 
 error_free:
-	kfree(tlb_cb);
+	dma_fence_chain_free(chain);
 	amdgpu_vm_end_critical(&params);
 	drm_dev_exit(idx);
 	return r;
@@ -2685,7 +2696,6 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 	vm->last_update = dma_fence_get_stub();
 	vm->last_unlocked = dma_fence_get_stub();
-	vm->last_seq_flush = dma_fence_get_stub();
 	vm->last_tlb_inv = dma_fence_get_stub();
 	vm->generation = amdgpu_vm_generation(adev, NULL);
 
@@ -2863,7 +2873,6 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 	struct amdgpu_bo_va_mapping *mapping, *tmp;
 	bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
 	struct amdgpu_bo *root;
-	unsigned long flags;
 	int i;
 
 	amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);
@@ -2876,10 +2885,6 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 				      false, MAX_SCHEDULE_TIMEOUT);
 	dma_fence_wait(vm->last_unlocked, false);
 	dma_fence_put(vm->last_unlocked);
-	dma_fence_wait(vm->last_seq_flush, false);
-	/* Make sure that all fence callbacks have completed */
-	dma_fence_lock_irqsave(vm->last_seq_flush, flags);
-	dma_fence_unlock_irqrestore(vm->last_seq_flush, flags);
 	dma_fence_put(vm->last_seq_flush);
 	dma_fence_put(vm->last_tlb_inv);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -29
index f6285a9d9e989..452409e95a5ea 100644
@@ -329,8 +329,8 @@ struct amdgpu_vm {
 	struct drm_sched_entity	delayed;
 
 	/* Last TLB seq/fence */
-	atomic64_t		tlb_seq;
-	struct dma_fence	*last_seq_flush;
+	u64			tlb_seq;
+	struct dma_fence __rcu	*last_seq_flush;
 	atomic64_t		kfd_last_flushed_seq;
 	uint64_t		tlb_fence_context;
 	struct dma_fence	*last_tlb_inv;
@@ -545,38 +545,12 @@ int amdgpu_vm_pt_map_tables(struct amdgpu_device *adev, struct amdgpu_vm *vm);
 
 bool amdgpu_vm_is_bo_always_valid(struct amdgpu_vm *vm, struct amdgpu_bo *bo);
 
-/**
- * amdgpu_vm_tlb_seq - return tlb flush sequence number
- * @vm: the amdgpu_vm structure to query
- *
- * Returns the tlb flush sequence number which indicates that the VM TLBs needs
- * to be invalidated whenever the sequence number change.
- */
-static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
-{
-	unsigned long flags;
-	spinlock_t *lock;
-
-	/*
-	 * Workaround 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 = dma_fence_spinlock(vm->last_seq_flush);
-	rcu_read_unlock();
-
-	spin_lock_irqsave(lock, flags);
-	spin_unlock_irqrestore(lock, flags);
-
-	return atomic64_read(&vm->tlb_seq);
-}
-
 void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
 				  unsigned int pasid,
 				  uint64_t addr,
 				  uint32_t status,
 				  unsigned int vmhub);
+uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm);
 void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
 				struct amdgpu_vm *vm,
 				struct dma_fence *dep,
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c +1 -2
index d03b9bbe20c7c..17a579dbf7b90 100644
@@ -122,8 +122,7 @@ static int amdgpu_vm_cpu_commit(struct amdgpu_vm_update_params *p,
 {
 	struct amdgpu_device *adev = p->adev;
 
-	if (p->needs_flush)
-		atomic64_inc(&p->vm->tlb_seq);
+	*fence = dma_fence_get_stub();
 
 	mb();
 	/* A reset flushed the HDP anyway, so that here can be skipped when a reset is ongoing */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c +0 -3
index d3a5e8487419e..26af3d54895b1 100644
@@ -117,9 +117,6 @@ static int amdgpu_vm_sdma_commit(struct amdgpu_vm_update_params *p,
 	WARN_ON(ib->length_dw == 0);
 	amdgpu_ring_pad_ib(ring, ib);
 
-	if (p->needs_flush)
-		atomic64_inc(&p->vm->tlb_seq);
-
 	WARN_ON(ib->length_dw > p->num_dw_left);
 	f = amdgpu_job_submit(p->job);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +1 -1
index 180e02de73e9b..06e0fe3ee18a9 100644
@@ -112,7 +112,7 @@ void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
 	spin_lock_init(&f->lock);
 
 	dma_fence_init64(&f->base, &amdgpu_tlb_fence_ops, &f->lock,
-			 vm->tlb_fence_context, atomic64_read(&vm->tlb_seq));
+			 vm->tlb_fence_context, vm->tlb_seq);
 
 	/* TODO: We probably need a separate wq here */
 	dma_fence_get(&f->base);
Signature · 1 line
2.43.0
#1 lore raw

AMD General

Hi, Christian.

1.

Review

Quoted 10 lines
(quoted diff) +10 -0
+uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
+{
+     struct dma_fence *fence;
+     u64 tlb_seq = 0;
+
+     rcu_read_lock();
+     fence = dma_fence_get_rcu_safe(&vm->last_seq_flush);
+     rcu_read_unlock();
+
+     if (dma_fence_is_signaled(dma_fence_chain_contained(fence))) {

amdgpu_vm_tlb_seq() dereferences NULL when the chain is empty. The first job of a new process oopses:

   BUG: kernel NULL pointer dereference, address: 0000000000000030
   RIP: 0010:amdgpu_vm_tlb_seq+0x8b/0x230 [amdgpu]
   Call Trace:
    amdgpu_vmid_grab+0x4ef/0xc60 [amdgpu]
    amdgpu_job_prepare_job+0xcb/0x130 [amdgpu]
    drm_sched_entity_pop_job+0x75/0x420 [gpu_sched]
    drm_sched_run_job_work+0x6a/0x2e0 [gpu_sched]

vm->last_seq_flush is only set in amdgpu_vm_tlb_flush() and is still NULL at the first VMID grab, so dma_fence_is_signaled(dma_fence_chain_contained(fence)) reads fence->flags from a NULL pointer.
patch v1 was fine because dma_fence_chain_for_each() accepts NULL.

We can check NULL before checking whether fence has signaled:

    if (!fence)
        return 0;

2.

Review static int amdgpu_vm_cpu_commit(struct amdgpu_vm_update_params *p, · line 125

Quoted 7 lines
(quoted diff) +1 -2
@@ -122,8 +122,7 @@ static int amdgpu_vm_cpu_commit(struct amdgpu_vm_update_params *p,
 {
      struct amdgpu_device *adev = p->adev;
 
-     if (p->needs_flush)
-             atomic64_inc(&p->vm->tlb_seq);
+     *fence = dma_fence_get_stub();

amdgpu_vm_cpu_commit() writes *fence unconditionally, but amdgpu_vm_handle_fault() passes NULL as fence, so the CPU update path dereferences NULL. The fence which *fence held before is also overwritten without dma_fence_put().

We can guard it with if (fence) and drop the old reference first:

    if (fence) {
        dma_fence_put(*fence);
        *fence = dma_fence_get_stub();
    }

Thanks,
Chong.

-----Original Message-----
From: Christian König <[email protected]>
Sent: Wednesday, November 25, 2026 11:31 PM
To: Li, Chong(Alan) <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: [PATCH] drm/amdgpu: switch vm->tlb_seq to dma_fence_chain handling v2

The vm->tlb_seq handling for per VMID based TLB invalidation was always rather fragile and relied on grabbing the dma_fence spinlock to make sure that callbacks executed in the right order.

Alan now pointed out that even this doesn't work correctly because different PTDE/PTE updates might use different locks and their signaling is not necessarily in the same order and on the same GPU.

Ditch the whole approach, instead use a dma_fence_chain to track all PDE/PTE updates and just iterate over them to get the last completed one when asked. Also make sure that PDE updates use the same path as PTE updates for consistency.

This not only fixes rare GPU page faults in the kernel queues but also potentially issues with the user queues should a PDE update make a TLB invalidation necessary.

v2: also handle the case where the chain head is already signaled as
pointed out by Alan.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 173 +++++++++---------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  32 +---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c    |   3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c   |   3 -
 .../gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c  |   2 +-
 5 files changed, 94 insertions(+), 119 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
index a627a30929903..2e15116e790f4 100644
@@ -27,6 +27,7 @@
  */
 
 #include <linux/dma-fence-array.h>
+#include <linux/dma-fence-chain.h>
 #include <linux/interval_tree_generic.h>  #include <linux/idr.h>  #include <linux/dma-buf.h> @@ -114,21 +115,6 @@ struct amdgpu_prt_cb {
        struct dma_fence_cb cb;
 };

-/**
- * struct amdgpu_vm_tlb_seq_struct - Helper to increment the TLB flush sequence
- */
-struct amdgpu_vm_tlb_seq_struct {
- /**
- * @vm: pointer to the amdgpu_vm structure to set the fence sequence on
- */
- struct amdgpu_vm *vm;
-
- /**
- * @cb: callback
- */
- struct dma_fence_cb cb;
-};
-
/**
* amdgpu_vm_assert_locked - check if VM is correctly locked
* @vm: the VM which schould be tested
@@ -1026,6 +1012,71 @@ uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)

        return result;

}

+/**
+ * amdgpu_vm_tlb_seq - return tlb flush sequence number
+ * @vm: the amdgpu_vm structure to query
+ *
+ * Returns the tlb flush sequence number which indicates that the VM +TLBs needs
+ * to be invalidated whenever the sequence number change.
+ */
+uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm) {
+ struct dma_fence *fence;
+ u64 tlb_seq = 0;
+

+       rcu_read_lock();
+       fence = dma_fence_get_rcu_safe(&vm->last_seq_flush);
+       rcu_read_unlock();

+
+ if (dma_fence_is_signaled(dma_fence_chain_contained(fence))) {
+ tlb_seq = fence->seqno;
+ dma_fence_put(fence);
+ } else {
+ do {
+ tlb_seq = to_dma_fence_chain(fence)->prev_seqno;
+ fence = dma_fence_chain_walk(fence);
+ } while (fence);
+ }
+ return tlb_seq;
+}
+
+/**
+ * amdgpu_vm_tlb_flush - prepare TLB flush
+ *
+ * @params: parameters for update
+ * @chain: dma_fence_chain for the signaling callback
+ * @fence: input fence to sync TLB flush with
+ *
+ * Increments the tlb sequence to make sure that future CS execute a VM flush.
+ */
+static void
+amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
+ struct dma_fence_chain *chain,
+ struct dma_fence *fence)
+{
+ struct amdgpu_vm *vm = params->vm;
+ struct dma_fence *prev;
+
+ ++vm->tlb_seq;
+ prev = rcu_dereference_protected(vm->last_seq_flush,
+ lockdep_is_held(&vm->eviction_lock));
+ dma_fence_chain_init(chain, prev, dma_fence_get(fence), vm->tlb_seq);
+ rcu_assign_pointer(vm->last_seq_flush, &chain->base);
+
+ /* Prepare a TLB flush fence to be attached to PTs */
+ /* The check for need_tlb_fence should be dropped once we
+ * sort out the issues with KIQ/MES TLB invalidation timeouts.
+ */
+ if (!params->unlocked && vm->need_tlb_fence) {
+ amdgpu_vm_tlb_fence_create(params->adev, vm, fence,
+ &vm->last_tlb_inv);
+
+ /* Makes sure no PD/PT is freed before the flush */
+ dma_resv_add_fence(vm->root.bo->tbo.base.resv,
+ vm->last_tlb_inv, DMA_RESV_USAGE_BOOKKEEP);
+ }
+}
+
/**
* amdgpu_vm_update_pdes - make sure that all directories are valid *

@@ -1043,6 +1094,7 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,  {
        struct amdgpu_vm_update_params params;
        struct amdgpu_vm_bo_base *entry, *tmp;
+       struct dma_fence_chain *chain;
        bool flush_tlb_needed = false;
        int r, idx;

@@ -1054,11 +1106,21 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,

        if (!drm_dev_enter(adev_to_drm(adev), &idx))
                return -ENODEV;

+ chain = dma_fence_chain_alloc();
+ if (!chain) {
+ r = -ENOMEM;
+ goto error_dev_exit;
+ }
+

        memset(&params, 0, sizeof(params));
        params.adev = adev;
        params.vm = vm;
        params.immediate = immediate;

+ r = amdgpu_vm_begin_critical(&params);
+ if (r)
+ goto error;
+

        r = vm->update_funcs->prepare(&params, NULL,
                                      AMDGPU_KERNEL_JOB_ID_VM_UPDATE_PDES);
        if (r)
@@ -1078,73 +1140,22 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
                goto error;

        if (flush_tlb_needed)

- atomic64_inc(&vm->tlb_seq);
+ amdgpu_vm_tlb_flush(&params, chain, vm->last_update);
+ else
+ dma_fence_chain_free(chain);

        list_for_each_entry_safe(entry, tmp, &vm->kernel.needs_update,
                                 vm_status)
                amdgpu_vm_bo_idle(entry);

error:
+ amdgpu_vm_end_critical(&params);
+
+error_dev_exit:

        drm_dev_exit(idx);
        return r;

}

-/**
- * amdgpu_vm_tlb_seq_cb - make sure to increment tlb sequence
- * @fence: unused
- * @cb: the callback structure
- *
- * Increments the tlb sequence to make sure that future CS execute a VM flush.
- */
-static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
- struct dma_fence_cb *cb)
-{
- struct amdgpu_vm_tlb_seq_struct *tlb_cb;
-

-       tlb_cb = container_of(cb, typeof(*tlb_cb), cb);
-       atomic64_inc(&tlb_cb->vm->tlb_seq);
-       kfree(tlb_cb);

-}
-
-/**
- * amdgpu_vm_tlb_flush - prepare TLB flush
- *
- * @params: parameters for update
- * @fence: input fence to sync TLB flush with
- * @tlb_cb: the callback structure
- *
- * Increments the tlb sequence to make sure that future CS execute a VM flush.
- */
-static void
-amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
- struct dma_fence *fence,
- struct amdgpu_vm_tlb_seq_struct *tlb_cb)
-{
- struct amdgpu_vm *vm = params->vm;
-
- tlb_cb->vm = vm;
- if (fence && !dma_fence_add_callback(fence, &tlb_cb->cb,
- amdgpu_vm_tlb_seq_cb)) {
- dma_fence_put(vm->last_seq_flush);
- vm->last_seq_flush = dma_fence_get(fence);
- } else {
- amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
- }
-
- /* Prepare a TLB flush fence to be attached to PTs */
- /* The check for need_tlb_fence should be dropped once we
- * sort out the issues with KIQ/MES TLB invalidation timeouts.
- */
- if (!params->unlocked && vm->need_tlb_fence) {
- amdgpu_vm_tlb_fence_create(params->adev, vm, fence,
- &vm->last_tlb_inv);
-
- /* Makes sure no PD/PT is freed before the flush */
- dma_resv_add_fence(vm->root.bo->tbo.base.resv,
- vm->last_tlb_inv, DMA_RESV_USAGE_BOOKKEEP);
- }
-}
-
/**
* amdgpu_vm_update_range - update a range in the vm page table
*
@@ -1177,16 +1188,16 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
struct ttm_resource *res, dma_addr_t *pages_addr,
struct dma_fence **fence)
{

-       struct amdgpu_vm_tlb_seq_struct *tlb_cb;
        struct amdgpu_vm_update_params params;
        struct amdgpu_res_cursor cursor;
+       struct dma_fence_chain *chain;
        int r, idx;

        if (!drm_dev_enter(adev_to_drm(adev), &idx))
                return -ENODEV;
-       tlb_cb = kmalloc_obj(*tlb_cb);
-       if (!tlb_cb) {
+       chain = dma_fence_chain_alloc();
+       if (!chain) {
                drm_dev_exit(idx);
                return -ENOMEM;
        }
@@ -1291,14 +1302,14 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
                goto error_free;

        if (params.needs_flush) {
-               amdgpu_vm_tlb_flush(&params, *fence, tlb_cb);
-               tlb_cb = NULL;
+               amdgpu_vm_tlb_flush(&params, chain, *fence);
+               chain = NULL;
        }

        amdgpu_vm_pt_free_list(adev, &params);

error_free:
- kfree(tlb_cb);

+       dma_fence_chain_free(chain);
        amdgpu_vm_end_critical(&params);
        drm_dev_exit(idx);
        return r;

@@ -2685,7 +2696,6 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,

        vm->last_update = dma_fence_get_stub();
        vm->last_unlocked = dma_fence_get_stub();
-       vm->last_seq_flush = dma_fence_get_stub();
        vm->last_tlb_inv = dma_fence_get_stub();
        vm->generation = amdgpu_vm_generation(adev, NULL);

@@ -2863,7 +2873,6 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)

        struct amdgpu_bo_va_mapping *mapping, *tmp;
        bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
        struct amdgpu_bo *root;
-       unsigned long flags;
        int i;

        amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm); @@ -2876,10 +2885,6 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
                                      false, MAX_SCHEDULE_TIMEOUT);
        dma_fence_wait(vm->last_unlocked, false);
        dma_fence_put(vm->last_unlocked);
-       dma_fence_wait(vm->last_seq_flush, false);
-       /* Make sure that all fence callbacks have completed */
-       dma_fence_lock_irqsave(vm->last_seq_flush, flags);
-       dma_fence_unlock_irqrestore(vm->last_seq_flush, flags);
        dma_fence_put(vm->last_seq_flush);
        dma_fence_put(vm->last_tlb_inv);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -28
index f6285a9d9e989..452409e95a5ea 100644
@@ -329,8 +329,8 @@ struct amdgpu_vm {
        struct drm_sched_entity delayed;
 
        /* Last TLB seq/fence */
-       atomic64_t              tlb_seq;
-       struct dma_fence        *last_seq_flush;
+       u64                     tlb_seq;
+       struct dma_fence __rcu  *last_seq_flush;
        atomic64_t              kfd_last_flushed_seq;
        uint64_t                tlb_fence_context;
        struct dma_fence        *last_tlb_inv;
@@ -545,38 +545,12 @@ int amdgpu_vm_pt_map_tables(struct amdgpu_device *adev, struct amdgpu_vm *vm);
 
 bool amdgpu_vm_is_bo_always_valid(struct amdgpu_vm *vm, struct amdgpu_bo *bo);
 
-/**
- * amdgpu_vm_tlb_seq - return tlb flush sequence number
- * @vm: the amdgpu_vm structure to query
- *
- * Returns the tlb flush sequence number which indicates that the VM TLBs needs
- * to be invalidated whenever the sequence number change.
- */
-static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm) -{
-       unsigned long flags;
-       spinlock_t *lock;
-
-       /*
-        * Workaround 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 = dma_fence_spinlock(vm->last_seq_flush);
-       rcu_read_unlock();
-
-       spin_lock_irqsave(lock, flags);
-       spin_unlock_irqrestore(lock, flags);
-
-       return atomic64_read(&vm->tlb_seq);
-}
-
 void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
                                  unsigned int pasid,
                                  uint64_t addr,
                                  uint32_t status,
                                  unsigned int vmhub);
+uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm);
 void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
                                struct amdgpu_vm *vm,
                                struct dma_fence *dep,
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c +1 -2
index d03b9bbe20c7c..17a579dbf7b90 100644
@@ -122,8 +122,7 @@ static int amdgpu_vm_cpu_commit(struct amdgpu_vm_update_params *p,  {
        struct amdgpu_device *adev = p->adev;
 
-       if (p->needs_flush)
-               atomic64_inc(&p->vm->tlb_seq);
+       *fence = dma_fence_get_stub();
 
        mb();
        /* A reset flushed the HDP anyway, so that here can be skipped when a reset is ongoing */ diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c

index d3a5e8487419e..26af3d54895b1 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c +0 -3
@@ -117,9 +117,6 @@ static int amdgpu_vm_sdma_commit(struct amdgpu_vm_update_params *p,
        WARN_ON(ib->length_dw == 0);
        amdgpu_ring_pad_ib(ring, ib);
 
-       if (p->needs_flush)
-               atomic64_inc(&p->vm->tlb_seq);
-
        WARN_ON(ib->length_dw > p->num_dw_left);
        f = amdgpu_job_submit(p->job);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +1 -1
index 180e02de73e9b..06e0fe3ee18a9 100644
@@ -112,7 +112,7 @@ void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
        spin_lock_init(&f->lock);
 
        dma_fence_init64(&f->base, &amdgpu_tlb_fence_ops, &f->lock,
-                        vm->tlb_fence_context, atomic64_read(&vm->tlb_seq));
+                        vm->tlb_fence_context, vm->tlb_seq);
 
        /* TODO: We probably need a separate wq here */
        dma_fence_get(&f->base);

--
2.43.0

maintainer
#2 lore raw

Hi Alan,

Quoted 29 lines · Chong(Alan)

AMD General

Hi, Christian.

1.

Review

Quoted 10 lines
(quoted diff) +10 -0
+uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
+{
+     struct dma_fence *fence;
+     u64 tlb_seq = 0;
+
+     rcu_read_lock();
+     fence = dma_fence_get_rcu_safe(&vm->last_seq_flush);
+     rcu_read_unlock();
+
+     if (dma_fence_is_signaled(dma_fence_chain_contained(fence))) {
   amdgpu_vm_tlb_seq() dereferences NULL when the chain is empty.
   The first job of a new process oopses:

   BUG: kernel NULL pointer dereference, address: 0000000000000030
   RIP: 0010:amdgpu_vm_tlb_seq+0x8b/0x230 [amdgpu]
   Call Trace:
    amdgpu_vmid_grab+0x4ef/0xc60 [amdgpu]
    amdgpu_job_prepare_job+0xcb/0x130 [amdgpu]
    drm_sched_entity_pop_job+0x75/0x420 [gpu_sched]
    drm_sched_run_job_work+0x6a/0x2e0 [gpu_sched]

   vm->last_seq_flush is only set in amdgpu_vm_tlb_flush() and is still
   NULL at the first VMID grab, so
Quoted 8 lines
   dma_fence_is_signaled(dma_fence_chain_contained(fence)) reads
   fence->flags from a NULL pointer.
   patch v1 was fine because dma_fence_chain_for_each() accepts NULL.

   We can check NULL before checking whether fence has signaled:

    if (!fence)
        return 0;

Good point.

We usually use an always signaled stub fence for such use cases but I removed setting that in version #1 of the patch because dma_fence_for_each() is NULL save, but we now of course need to bring it back or otherwise run into problems.

Quoted 14 lines

2.

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c static int amdgpu_vm_cpu_commit(struct amdgpu_vm_update_params *p, · line 125

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c +1 -2
@@ -122,8 +122,7 @@ static int amdgpu_vm_cpu_commit(struct amdgpu_vm_update_params *p,
 {
      struct amdgpu_device *adev = p->adev;
 
-     if (p->needs_flush)
-             atomic64_inc(&p->vm->tlb_seq);
+     *fence = dma_fence_get_stub();

amdgpu_vm_cpu_commit() writes *fence unconditionally, but amdgpu_vm_handle_fault() passes NULL as fence, so the CPU update path dereferences NULL. The fence which *fence held before is also overwritten without dma_fence_put().

Quoted 6 lines
   We can guard it with `if (fence)` and drop the old reference first:

    if (fence) {
        dma_fence_put(*fence);
        *fence = dma_fence_get_stub();
    }

Yeah good point as well. We are about to remove that stuff but this patch needs to be able to be backported so it is necessary for old kernels.

Thanks,
Christian.

Quoted 406 lines

Thanks,
Chong.

-----Original Message-----
From: Christian König <[email protected]>
Sent: Wednesday, November 25, 2026 11:31 PM
To: Li, Chong(Alan) <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: [PATCH] drm/amdgpu: switch vm->tlb_seq to dma_fence_chain handling v2

The vm->tlb_seq handling for per VMID based TLB invalidation was always rather fragile and relied on grabbing the dma_fence spinlock to make sure that callbacks executed in the right order.

Alan now pointed out that even this doesn't work correctly because different PTDE/PTE updates might use different locks and their signaling is not necessarily in the same order and on the same GPU.

Ditch the whole approach, instead use a dma_fence_chain to track all PDE/PTE updates and just iterate over them to get the last completed one when asked. Also make sure that PDE updates use the same path as PTE updates for consistency.

This not only fixes rare GPU page faults in the kernel queues but also potentially issues with the user queues should a PDE update make a TLB invalidation necessary.

v2: also handle the case where the chain head is already signaled as
pointed out by Alan.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 173 +++++++++---------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  32 +---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c    |   3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c   |   3 -
 .../gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c  |   2 +-
 5 files changed, 94 insertions(+), 119 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +89 -80
index a627a30929903..2e15116e790f4 100644
@@ -27,6 +27,7 @@
  */
 
 #include <linux/dma-fence-array.h>
+#include <linux/dma-fence-chain.h>
 #include <linux/interval_tree_generic.h>  #include <linux/idr.h>  #include <linux/dma-buf.h> @@ -114,21 +115,6 @@ struct amdgpu_prt_cb {
        struct dma_fence_cb cb;
 };
 
-/**
- * struct amdgpu_vm_tlb_seq_struct - Helper to increment the TLB flush sequence
- */
-struct amdgpu_vm_tlb_seq_struct {
-       /**
-        * @vm: pointer to the amdgpu_vm structure to set the fence sequence on
-        */
-       struct amdgpu_vm *vm;
-
-       /**
-        * @cb: callback
-        */
-       struct dma_fence_cb cb;
-};
-
 /**
  * amdgpu_vm_assert_locked - check if VM is correctly locked
  * @vm: the VM which schould be tested
@@ -1026,6 +1012,71 @@ uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
        return result;
 }
 
+/**
+ * amdgpu_vm_tlb_seq - return tlb flush sequence number
+ * @vm: the amdgpu_vm structure to query
+ *
+ * Returns the tlb flush sequence number which indicates that the VM
+TLBs needs
+ * to be invalidated whenever the sequence number change.
+ */
+uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm) {
+       struct dma_fence *fence;
+       u64 tlb_seq = 0;
+
+       rcu_read_lock();
+       fence = dma_fence_get_rcu_safe(&vm->last_seq_flush);
+       rcu_read_unlock();
+
+       if (dma_fence_is_signaled(dma_fence_chain_contained(fence))) {
+               tlb_seq = fence->seqno;
+               dma_fence_put(fence);
+       } else {
+               do {
+                       tlb_seq = to_dma_fence_chain(fence)->prev_seqno;
+                       fence = dma_fence_chain_walk(fence);
+               } while (fence);
+       }
+       return tlb_seq;
+}
+
+/**
+ * amdgpu_vm_tlb_flush - prepare TLB flush
+ *
+ * @params: parameters for update
+ * @chain: dma_fence_chain for the signaling callback
+ * @fence: input fence to sync TLB flush with
+ *
+ * Increments the tlb sequence to make sure that future CS execute a VM flush.
+ */
+static void
+amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
+                   struct dma_fence_chain *chain,
+                   struct dma_fence *fence)
+{
+       struct amdgpu_vm *vm = params->vm;
+       struct dma_fence *prev;
+
+       ++vm->tlb_seq;
+       prev = rcu_dereference_protected(vm->last_seq_flush,
+                                        lockdep_is_held(&vm->eviction_lock));
+       dma_fence_chain_init(chain, prev, dma_fence_get(fence), vm->tlb_seq);
+       rcu_assign_pointer(vm->last_seq_flush, &chain->base);
+
+       /* Prepare a TLB flush fence to be attached to PTs */
+       /* The check for need_tlb_fence should be dropped once we
+        * sort out the issues with KIQ/MES TLB invalidation timeouts.
+        */
+       if (!params->unlocked && vm->need_tlb_fence) {
+               amdgpu_vm_tlb_fence_create(params->adev, vm, fence,
+                                          &vm->last_tlb_inv);
+
+               /* Makes sure no PD/PT is freed before the flush */
+               dma_resv_add_fence(vm->root.bo->tbo.base.resv,
+                                  vm->last_tlb_inv, DMA_RESV_USAGE_BOOKKEEP);
+       }
+}
+
 /**
  * amdgpu_vm_update_pdes - make sure that all directories are valid
  *
@@ -1043,6 +1094,7 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,  {
        struct amdgpu_vm_update_params params;
        struct amdgpu_vm_bo_base *entry, *tmp;
+       struct dma_fence_chain *chain;
        bool flush_tlb_needed = false;
        int r, idx;
 
@@ -1054,11 +1106,21 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
        if (!drm_dev_enter(adev_to_drm(adev), &idx))
                return -ENODEV;
 
+       chain = dma_fence_chain_alloc();
+       if (!chain) {
+               r = -ENOMEM;
+               goto error_dev_exit;
+       }
+
        memset(&params, 0, sizeof(params));
        params.adev = adev;
        params.vm = vm;
        params.immediate = immediate;
 
+       r = amdgpu_vm_begin_critical(&params);
+       if (r)
+               goto error;
+
        r = vm->update_funcs->prepare(&params, NULL,
                                      AMDGPU_KERNEL_JOB_ID_VM_UPDATE_PDES);
        if (r)
@@ -1078,73 +1140,22 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
                goto error;
 
        if (flush_tlb_needed)
-               atomic64_inc(&vm->tlb_seq);
+               amdgpu_vm_tlb_flush(&params, chain, vm->last_update);
+       else
+               dma_fence_chain_free(chain);
 
        list_for_each_entry_safe(entry, tmp, &vm->kernel.needs_update,
                                 vm_status)
                amdgpu_vm_bo_idle(entry);
 
 error:
+       amdgpu_vm_end_critical(&params);
+
+error_dev_exit:
        drm_dev_exit(idx);
        return r;
 }
 
-/**
- * amdgpu_vm_tlb_seq_cb - make sure to increment tlb sequence
- * @fence: unused
- * @cb: the callback structure
- *
- * Increments the tlb sequence to make sure that future CS execute a VM flush.
- */
-static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
-                                struct dma_fence_cb *cb)
-{
-       struct amdgpu_vm_tlb_seq_struct *tlb_cb;
-
-       tlb_cb = container_of(cb, typeof(*tlb_cb), cb);
-       atomic64_inc(&tlb_cb->vm->tlb_seq);
-       kfree(tlb_cb);
-}
-
-/**
- * amdgpu_vm_tlb_flush - prepare TLB flush
- *
- * @params: parameters for update
- * @fence: input fence to sync TLB flush with
- * @tlb_cb: the callback structure
- *
- * Increments the tlb sequence to make sure that future CS execute a VM flush.
- */
-static void
-amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
-                   struct dma_fence *fence,
-                   struct amdgpu_vm_tlb_seq_struct *tlb_cb)
-{
-       struct amdgpu_vm *vm = params->vm;
-
-       tlb_cb->vm = vm;
-       if (fence && !dma_fence_add_callback(fence, &tlb_cb->cb,
-                                            amdgpu_vm_tlb_seq_cb)) {
-               dma_fence_put(vm->last_seq_flush);
-               vm->last_seq_flush = dma_fence_get(fence);
-       } else {
-               amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
-       }
-
-       /* Prepare a TLB flush fence to be attached to PTs */
-       /* The check for need_tlb_fence should be dropped once we
-        * sort out the issues with KIQ/MES TLB invalidation timeouts.
-        */
-       if (!params->unlocked && vm->need_tlb_fence) {
-               amdgpu_vm_tlb_fence_create(params->adev, vm, fence,
-                                          &vm->last_tlb_inv);
-
-               /* Makes sure no PD/PT is freed before the flush */
-               dma_resv_add_fence(vm->root.bo->tbo.base.resv,
-                                  vm->last_tlb_inv, DMA_RESV_USAGE_BOOKKEEP);
-       }
-}
-
 /**
  * amdgpu_vm_update_range - update a range in the vm page table
  *
@@ -1177,16 +1188,16 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
                           struct ttm_resource *res, dma_addr_t *pages_addr,
                           struct dma_fence **fence)
 {
-       struct amdgpu_vm_tlb_seq_struct *tlb_cb;
        struct amdgpu_vm_update_params params;
        struct amdgpu_res_cursor cursor;
+       struct dma_fence_chain *chain;
        int r, idx;
 
        if (!drm_dev_enter(adev_to_drm(adev), &idx))
                return -ENODEV;
 
-       tlb_cb = kmalloc_obj(*tlb_cb);
-       if (!tlb_cb) {
+       chain = dma_fence_chain_alloc();
+       if (!chain) {
                drm_dev_exit(idx);
                return -ENOMEM;
        }
@@ -1291,14 +1302,14 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
                goto error_free;
 
        if (params.needs_flush) {
-               amdgpu_vm_tlb_flush(&params, *fence, tlb_cb);
-               tlb_cb = NULL;
+               amdgpu_vm_tlb_flush(&params, chain, *fence);
+               chain = NULL;
        }
 
        amdgpu_vm_pt_free_list(adev, &params);
 
 error_free:
-       kfree(tlb_cb);
+       dma_fence_chain_free(chain);
        amdgpu_vm_end_critical(&params);
        drm_dev_exit(idx);
        return r;
@@ -2685,7 +2696,6 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
        vm->last_update = dma_fence_get_stub();
        vm->last_unlocked = dma_fence_get_stub();
-       vm->last_seq_flush = dma_fence_get_stub();
        vm->last_tlb_inv = dma_fence_get_stub();
        vm->generation = amdgpu_vm_generation(adev, NULL);
 
@@ -2863,7 +2873,6 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
        struct amdgpu_bo_va_mapping *mapping, *tmp;
        bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
        struct amdgpu_bo *root;
-       unsigned long flags;
        int i;
 
        amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm); @@ -2876,10 +2885,6 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
                                      false, MAX_SCHEDULE_TIMEOUT);
        dma_fence_wait(vm->last_unlocked, false);
        dma_fence_put(vm->last_unlocked);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -4
-       dma_fence_wait(vm->last_seq_flush, false);
-       /* Make sure that all fence callbacks have completed */
-       dma_fence_lock_irqsave(vm->last_seq_flush, flags);
-       dma_fence_unlock_irqrestore(vm->last_seq_flush, flags);
        dma_fence_put(vm->last_seq_flush);
        dma_fence_put(vm->last_tlb_inv);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -28
index f6285a9d9e989..452409e95a5ea 100644
@@ -329,8 +329,8 @@ struct amdgpu_vm {
        struct drm_sched_entity delayed;
 
        /* Last TLB seq/fence */
-       atomic64_t              tlb_seq;
-       struct dma_fence        *last_seq_flush;
+       u64                     tlb_seq;
+       struct dma_fence __rcu  *last_seq_flush;
        atomic64_t              kfd_last_flushed_seq;
        uint64_t                tlb_fence_context;
        struct dma_fence        *last_tlb_inv;
@@ -545,38 +545,12 @@ int amdgpu_vm_pt_map_tables(struct amdgpu_device *adev, struct amdgpu_vm *vm);
 
 bool amdgpu_vm_is_bo_always_valid(struct amdgpu_vm *vm, struct amdgpu_bo *bo);
 
-/**
- * amdgpu_vm_tlb_seq - return tlb flush sequence number
- * @vm: the amdgpu_vm structure to query
- *
- * Returns the tlb flush sequence number which indicates that the VM TLBs needs
- * to be invalidated whenever the sequence number change.
- */
-static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm) -{
-       unsigned long flags;
-       spinlock_t *lock;
-
-       /*
-        * Workaround 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 = dma_fence_spinlock(vm->last_seq_flush);
-       rcu_read_unlock();
-
-       spin_lock_irqsave(lock, flags);
-       spin_unlock_irqrestore(lock, flags);
-
-       return atomic64_read(&vm->tlb_seq);
-}
-
 void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
                                  unsigned int pasid,
                                  uint64_t addr,
                                  uint32_t status,
                                  unsigned int vmhub);
+uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm);
 void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
                                struct amdgpu_vm *vm,
                                struct dma_fence *dep,
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c +1 -2
index d03b9bbe20c7c..17a579dbf7b90 100644
@@ -122,8 +122,7 @@ static int amdgpu_vm_cpu_commit(struct amdgpu_vm_update_params *p,  {
        struct amdgpu_device *adev = p->adev;
 
-       if (p->needs_flush)
-               atomic64_inc(&p->vm->tlb_seq);
+       *fence = dma_fence_get_stub();
 
        mb();
        /* A reset flushed the HDP anyway, so that here can be skipped when a reset is ongoing */ diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c

index d3a5e8487419e..26af3d54895b1 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c +0 -3
@@ -117,9 +117,6 @@ static int amdgpu_vm_sdma_commit(struct amdgpu_vm_update_params *p,
        WARN_ON(ib->length_dw == 0);
        amdgpu_ring_pad_ib(ring, ib);
 
-       if (p->needs_flush)
-               atomic64_inc(&p->vm->tlb_seq);
-
        WARN_ON(ib->length_dw > p->num_dw_left);
        f = amdgpu_job_submit(p->job);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +1 -2
index 180e02de73e9b..06e0fe3ee18a9 100644
@@ -112,7 +112,7 @@ void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
        spin_lock_init(&f->lock);
 
        dma_fence_init64(&f->base, &amdgpu_tlb_fence_ops, &f->lock,
-                        vm->tlb_fence_context, atomic64_read(&vm->tlb_seq));
+                        vm->tlb_fence_context, vm->tlb_seq);
 
        /* TODO: We probably need a separate wq here */
        dma_fence_get(&f->base);
--

2.43.0

#3 lore raw
In reply to Christian König
Quoted 9 lines · Christian König

This not only fixes rare GPU page faults in the kernel queues but also potentially issues with the user queues should a PDE update make a TLB invalidation necessary.

v2: also handle the case where the chain head is already signaled as pointed out by Alan.

You mention "fixes" above and there was a "report". Cc stable?

Quoted 7 lines

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 173 +++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  32 +--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c    |   3 +- drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c   |   3 - .../gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c  |   2 +-

 5 files changed, 94 insertions(+), 119 deletions(-)

Do you think it might be possible to split this rework up into smaller commits that are more atomic and, thus, easier to review / understand?

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) · line 2882

Quoted 10 lines


  amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -4
@@ -2876,10 +2885,6 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  				      false, MAX_SCHEDULE_TIMEOUT);
  	dma_fence_wait(vm->last_unlocked, false);
  	dma_fence_put(vm->last_unlocked);
-	dma_fence_wait(vm->last_seq_flush, false);
-	/* Make sure that all fence callbacks have completed */
-	dma_fence_lock_irqsave(vm->last_seq_flush, flags);
-	dma_fence_unlock_irqrestore(vm->last_seq_flush, flags);

Certainly nice to see, but I can't derive from the commit message why it's now ensured that the fence callbacks have ran?

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
  	dma_fence_put(vm->last_seq_flush);
  	dma_fence_put(vm->last_tlb_inv);

[…]

Quoted 12 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c +0 -0
index d03b9bbe20c7c..17a579dbf7b90 100644
@@ -122,8 +122,7 @@ static int amdgpu_vm_cpu_commit(struct amdgpu_vm_update_params *p,
  {
  	struct amdgpu_device *adev = p->adev;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c +1 -2
-	if (p->needs_flush)
-		atomic64_inc(&p->vm->tlb_seq);
+	*fence = dma_fence_get_stub();
Quoted 2 lines


  mb();

Hey look, an undocumented mb! ^_^

Would be a good opportunity to document why it exists. Does it order against dma_fence_get_stub()? Seems unlikely…

maintainer
#4 lore raw
Quoted 5 lines · Philipp Stanner
Quoted 4 lines · Christian König

This not only fixes rare GPU page faults in the kernel queues but also potentially issues with the user queues should a PDE update make a TLB invalidation necessary.

Quoted 7 lines
Quoted 5 lines

v2: also handle the case where the chain head is already signaled as pointed out by Alan.

You mention "fixes" above and there was a "report". Cc stable?

We usually add that while discussing the patch, but yeah I agree that we need to backport this.

Quoted 11 lines
Quoted 8 lines

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 173 +++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  32 +--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c    |   3 +- drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c   |   3 - .../gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c  |   2 +-

 5 files changed, 94 insertions(+), 119 deletions(-)

Do you think it might be possible to split this rework up into smaller commits that are more atomic and, thus, easier to review / understand?

I already tried that and always ended up with something which was broken in between patches.

Quoted 5 lines
Quoted 5 lines


  amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
@@ -2876,10 +2885,6 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  				      false, MAX_SCHEDULE_TIMEOUT);
  	dma_fence_wait(vm->last_unlocked, false);
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) · line 2882

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -4
  	dma_fence_put(vm->last_unlocked);
-	dma_fence_wait(vm->last_seq_flush, false);
-	/* Make sure that all fence callbacks have completed */
-	dma_fence_lock_irqsave(vm->last_seq_flush, flags);
-	dma_fence_unlock_irqrestore(vm->last_seq_flush, flags);

Certainly nice to see, but I can't derive from the commit message why it's now ensured that the fence callbacks have ran?

The point is the callback which increases the sequence number after the fences signaled is removed.

So there is no chance that they can race any more. I will update the commit message.

Quoted 7 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
  	dma_fence_put(vm->last_seq_flush);
  	dma_fence_put(vm->last_tlb_inv);

[…]

Quoted 16 lines
Quoted 12 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c +0 -0
index d03b9bbe20c7c..17a579dbf7b90 100644
@@ -122,8 +122,7 @@ static int amdgpu_vm_cpu_commit(struct amdgpu_vm_update_params *p,
  {
  	struct amdgpu_device *adev = p->adev;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c +1 -2
-	if (p->needs_flush)
-		atomic64_inc(&p->vm->tlb_seq);
+	*fence = dma_fence_get_stub();
Quoted 2 lines


  mb();

Hey look, an undocumented mb! ^_^

Yeah the comment directly below explains why it is here. It is just not showing up in this diff and the technical relationship is not so straight forward to understand.

Quoted 2 lines

Would be a good opportunity to document why it exists. Does it order against dma_fence_get_stub()? Seems unlikely…

No, most mb() in amdgpu should actually be a dma_wmb() which needs to be used so that the HW sees written values.

It's just that the code is older than the dma_wmb() function and nobody had the time to update all mb() occasions in the driver and test if nothing was missed.

Regards,
Christian.

Quoted 2 lines