On 11/17/26 12:39, Mu, Zihao wrote:
> From: Zihao Mu <[email protected]>
>
> Retain the latest SDMA finished fence independently of the optional
> public output. A prefix can be submitted before PT allocation fails,
> leaving no final job to replace the caller's older output fence. TLB
> finalization must still wait for that submitted prefix.
>
> Every phase of a range and its nested PT clear preserves immediate,
> so sdma_alloc_job() selects the same VM scheduler entity throughout.
> The entity keeps its engine while work is unfinished, and submissions
> execute in order there. amdgpu_job_submit() returns the scheduler's
> finished fence. Thus the last submitted finished fence covers the
> prefix, clear and resumed phase; no cross-entity ordering is assumed.
>
> The context owns last_update. On submission, put the old reference and
> get the new finished fence. Missing/empty commits keep the previous
> completion. A nested clear transfers its reference to the outer context
> by swapping outputs, then puts the displaced reference at exit. Range,
> PDE and standalone-clear owners put their private reference at teardown;
> TLB finalization obtains its own reference before that put.
>
> Keep the public !immediate output and DONT_PIPELINE policy unchanged.
> Track completion before adding partial-range finalization, so the
> latter can order its TLB work even after replacement allocation fails.
>
> Signed-off-by: Zihao Mu <[email protected]>
> ---
> drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 10 +++++++---
> drivers/gpu/drm/amd/amdgpu/amdgpu_vm_internal.h | 8 +++++++-
> drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 12 +++++++++---
> drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c | 2 ++
> 4 files changed, 25 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> index 7199cec1f95..3b4df22c5ed 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> @@ -658,7 +658,9 @@ int amdgpu_vm_validate(struct amdgpu_device *adev, struct
> amdgpu_vm *vm,
> bo_base->moved = true;
>
> if (vm->generation != new_vm_generation) {
> - r = amdgpu_vm_pt_clear(adev, vm,
> to_amdgpu_bo_vm(bo_base->bo), false);
> + r = amdgpu_vm_pt_clear(adev, vm,
> + to_amdgpu_bo_vm(bo_base->bo),
> + false, NULL);
> if (r)
> return r;
> }
> @@ -1079,6 +1081,7 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
> amdgpu_vm_bo_idle(entry);
>
> error:
> + dma_fence_put(params.last_update);
> drm_dev_exit(idx);
> return r;
> }
> @@ -1282,7 +1285,7 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev,
> struct amdgpu_vm *vm,
> vm->update_funcs->commit(¶ms, fence);
>
> if (params.needs_flush) {
> - amdgpu_vm_tlb_flush(¶ms, *fence, tlb_cb);
> + amdgpu_vm_tlb_flush(¶ms, params.last_update, tlb_cb);
> tlb_cb = NULL;
> }
>
> @@ -1290,6 +1293,7 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev,
> struct amdgpu_vm *vm,
>
> error_free:
> kfree(tlb_cb);
> + dma_fence_put(params.last_update);
> amdgpu_vm_end_critical(¶ms);
> drm_dev_exit(idx);
> return r;
> @@ -2706,7 +2710,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct
> amdgpu_vm *vm,
> if (r)
> goto error_free_root;
>
> - r = amdgpu_vm_pt_clear(adev, vm, root, false);
> + r = amdgpu_vm_pt_clear(adev, vm, root, false, NULL);
> if (r)
> goto error_free_root;
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_internal.h
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_internal.h
> index ea52be053d8..9bd71f4d5b9 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_internal.h
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_internal.h
> @@ -82,6 +82,11 @@ struct amdgpu_vm_update_params {
> */
> u64 k_job_id;
>
> + /**
> + * @last_update: owned reference to the last SDMA completion fence
> + */
> + struct dma_fence *last_update;
> +
> /**
> * @num_dw_left: number of dw left for the IB
> */
> @@ -121,7 +126,8 @@ struct amdgpu_vm_update_funcs {
> };
>
> int amdgpu_vm_pt_clear(struct amdgpu_device *adev, struct amdgpu_vm *vm,
> - struct amdgpu_bo_vm *vmbo, bool immediate);
> + struct amdgpu_bo_vm *vmbo, bool immediate,
> + struct dma_fence **last_update);
> int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
> int level, bool immediate, struct amdgpu_bo_vm **vmbo,
> int32_t xcp_id);
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c
> index 0ff2d06bf53..7b7a576d95f 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c
> @@ -353,6 +353,7 @@ static void amdgpu_vm_pt_next_dfs(struct amdgpu_device
> *adev,
> * @vm: VM to clear BO from
> * @vmbo: BO to clear
> * @immediate: use an immediate update
> + * @last_update: optional output for the internal clear completion fence
> *
> * Root PD needs to be reserved when calling this.
> *
> @@ -360,7 +361,8 @@ static void amdgpu_vm_pt_next_dfs(struct amdgpu_device
> *adev,
> * 0 on success, errno otherwise.
> */
> int amdgpu_vm_pt_clear(struct amdgpu_device *adev, struct amdgpu_vm *vm,
> - struct amdgpu_bo_vm *vmbo, bool immediate)
> + struct amdgpu_bo_vm *vmbo, bool immediate,
> + struct dma_fence **last_update)
> {
> unsigned int level = adev->vm_manager.root_level;
> struct ttm_operation_ctx ctx = { true, false };
> @@ -390,11 +392,11 @@ int amdgpu_vm_pt_clear(struct amdgpu_device *adev,
> struct amdgpu_vm *vm,
> if (!drm_dev_enter(adev_to_drm(adev), &idx))
> return -ENODEV;
>
> + memset(¶ms, 0, sizeof(params));
> r = vm->update_funcs->map_table(vmbo);
> if (r)
> goto exit;
>
> - memset(¶ms, 0, sizeof(params));
> params.adev = adev;
> params.vm = vm;
> params.immediate = immediate;
> @@ -429,6 +431,9 @@ int amdgpu_vm_pt_clear(struct amdgpu_device *adev, struct
> amdgpu_vm *vm,
>
> vm->update_funcs->commit(¶ms, &vm->last_update);
> exit:
> + if (last_update && params.last_update)
> + swap(*last_update, params.last_update);
> + dma_fence_put(params.last_update);
> drm_dev_exit(idx);
> return r;
> }
> @@ -545,7 +550,8 @@ static int amdgpu_vm_pt_alloc(struct
> amdgpu_vm_update_params *p,
> */
> pt_bo->parent = amdgpu_bo_ref(cursor->parent->bo);
> amdgpu_vm_bo_base_init(entry, p->vm, pt_bo);
> - r = amdgpu_vm_pt_clear(p->adev, p->vm, pt, p->immediate);
> + r = amdgpu_vm_pt_clear(p->adev, p->vm, pt, p->immediate,
> + &p->last_update);
The code you work on here is outdated, I'm about to remove immediate updates
from amdgpu_vm_pt_clear() and only give the parameters to that function as well.
Apart from that the patch looks good to me.
Regards,
Christian.
> if (r) {
> /* The entry points to the PT now,
So what take it out again */
> amdgpu_vm_pt_free(entry);
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c
> index 38ca2d78b38..d2d9a16a7ef 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c
> @@ -130,6 +130,8 @@ static void amdgpu_vm_sdma_commit(struct
> amdgpu_vm_update_params *p,
> WARN_ON(ib->length_dw > p->num_dw_left);
> f = amdgpu_job_submit(p->job);
> p->job = NULL;
> + dma_fence_put(p->last_update);
> + p->last_update = dma_fence_get(f);
>
> if (p->unlocked) {
> struct dma_fence *tmp = dma_fence_get(f);