AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "chong li" <chongli2@amd.com>
Cc: amd-gfx@lists.freedesktop.org
Subject: Re: [PATCH] drm/amdgpu: track finished page table updates with a fence chain
Date: Tue, 24 Nov 2026 05:04:23 +0000	[thread overview]
Message-ID: <sashiko-outbox-230541@kernel.org> (raw)
In-Reply-To: <20261124044544.3561866-1-chongli2@amd.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] drm/amdgpu: O(N^2) chain walk in amdgpu_vm_tlb_flush() and
  amdgpu_vm_tlb_seq()
- [Low] drm/amdgpu: missing Fixes trailer for TLB sequence race fix
--

commit bb4d183d85e4f91ff2a4b58dd0ca99081bbe5e40
Author: chong li <chongli2@amd.com>

> drm/amdgpu: track finished page table updates with a fence chain
> 
> amdgpu_vm_tlb_seq() waits for the tlb_seq callback of a page table update
> by taking the lock of vm->last_seq_flush. But every fence has its own
> lock, and a newer update replaces vm->last_seq_flush without waiting for
> the callbacks of older updates. A job scheduled after an older fence has
> signaled, but before its callback has finished, takes the lock of the
> newer fence, misses the tlb_seq increment and can reuse a VMID without
> flushing the TLB after the page table update it waited for.
> 
> how to fix:
> Stop incrementing tlb_seq from a fence callback. Put the fences of the
> updates which need a TLB flush into a dma_fence_chain and let
> amdgpu_vm_tlb_seq() add the number of updates which have signaled. A
> dependent job then sees the sequence change together with the signaled
> bit of the fence it waited for. The chain nodes are numbered
> consecutively. The number of the newest node minus the number of
> unsignaled nodes therefore also counts the signaled nodes which were
> already pruned, in any completion order. Nothing references the VM from
> a fence callback any more, so amdgpu_vm_fini() no longer needs to wait
> for vm->last_seq_flush.
> 
> Signed-off-by: chong li <chongli2@amd.com>

[Severity: Low]
Should this bug fix include a Fixes tag?

  Fixes: 5255e146c99a ("drm/amdgpu: rework TLB flushing")

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> index 94b1cfe05e3e0..4e5a1286ffcbb 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c

[ ... ]

> @@ -1085,47 +1070,33 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
>  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 dma_fence_chain *chain)
>  {
>  	struct amdgpu_vm *vm = params->vm;
> +	struct dma_fence *prev, *iter;
>  
> -	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);
> -	}
> +	/* The reference of vm->last_seq_flush to prev moves into the chain */
> +	prev = rcu_dereference_protected(vm->last_seq_flush,
> +					 lockdep_is_held(&vm->eviction_lock));
> +	dma_fence_chain_init(chain, prev,
> +			     fence ? dma_fence_get(fence) : dma_fence_get_stub(),
> +			     prev ? prev->seqno + 1 : 1);
> +	rcu_assign_pointer(vm->last_seq_flush, &chain->base);
> +
> +	/* Prune signaled nodes, not every VM reads the sequence */
> +	dma_fence_chain_for_each(iter, &chain->base);

[ ... ]

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
> index f6285a9d9e989..6142a86a8b802 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h

[ ... ]

> @@ -554,22 +555,24 @@ bool amdgpu_vm_is_bo_always_valid(struct amdgpu_vm *vm, struct amdgpu_bo *bo);
>   */
>  static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
>  {
> -	unsigned long flags;
> -	spinlock_t *lock;
> +	u64 seq = atomic64_read(&vm->tlb_seq);
> +	struct dma_fence *head, *iter;
>  
> -	/*
> -	 * 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);
> +	head = dma_fence_get_rcu_safe(&vm->last_seq_flush);
>  	rcu_read_unlock();
> -
> -	spin_lock_irqsave(lock, flags);
> -	spin_unlock_irqrestore(lock, flags);
> -
> -	return atomic64_read(&vm->tlb_seq);
> +	if (!head)
> +		return seq;
> +
> +	/* Add the updates in the chain which have finished, in any order */
> +	seq += head->seqno;
> +	dma_fence_chain_for_each(iter, head) {
> +		if (!dma_fence_is_signaled(dma_fence_chain_contained(iter)))
> +			--seq;
> +	}
> +	dma_fence_put(head);
> +
> +	return seq;
>  }

[Severity: Medium]
Does walking vm->last_seq_flush with dma_fence_chain_for_each() in
amdgpu_vm_tlb_flush() and amdgpu_vm_tlb_seq() turn batches of N in-flight page
table updates into an O(N^2) chain walk?

dma_fence_chain_walk() only unlinks signaled predecessors and visits every
unsignaled node in the chain, acquiring and releasing a reference at each step.
When N updates requiring a TLB flush are queued while their SDMA fences are
still unsignaled (such as amdgpu_vm_clear_freed() looping over vm->freed while
SDMA jobs wait on active VM submissions), each amdgpu_vm_tlb_flush() call walks
all prior unsignaled nodes in vm->last_seq_flush.  In addition,
amdgpu_vmid_grab() calls amdgpu_vm_tlb_seq() up to twice while holding the
per-VMHUB id_mgr->lock mutex, walking all unsignaled chain nodes under the
lock, and amdgpu_vm_tlb_flush() allocates and chains a node even when fence is
NULL or already signaled.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124044544.3561866-1-chongli2@amd.com?part=1

  reply	other threads:[~2026-11-24  5:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  4:45 [PATCH] drm/amdgpu: track finished page table updates with a fence chain chong li
2026-11-24  5:04 ` sashiko-bot [this message]
2026-11-25 10:45 ` Li, Chong(Alan)
2026-11-25 13:54   ` Christian König

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-230541@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=chongli2@amd.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox