From: zihaomu <[email protected]>

The CPU backend checks KERNEL dependencies before every PTE batch. The
generic reservation waiter uses an unlocked iterator, which takes and
drops fence references and polls their state before filtering by usage.
A reserved VM with only BOOKKEEP entries repeats this work for every
batch even though those entries are not dependencies of this query.

Page tables share the root reservation, which is held for updates with
unlocked=false. Use the locked iterator to check whether any KERNEL
entry exists. If none exists, return the original waiter's positive
success value without acquiring temporary references or polling
unrelated fences.

Keep the full generic wait when any KERNEL entry exists, including an
already signaled one, and for unlocked updates. Check each batch afresh
so dependencies added between calls remain visible. The PTE write,
flags and commit code is unchanged.

This adds a locked lookup before the generic wait when KERNEL entries
are present. It targets the no-KERNEL case rather than reducing the
number of batches or caching dependency checks.

Signed-off-by: zihaomu <[email protected]>
---
RFC: this is an independent performance proposal, based on
drm-amdgpu-next 5349075eac146ae0511d8929fc24451e4d9a4b7e.

I would appreciate feedback on whether the no-KERNEL case justifies
the extra locked lookup on calls that do contain KERNEL entries, and
whether this optimization belongs here or in the reservation helper.

The locked iterator is used only when unlocked=false and the shared
root reservation is held. The full waiter is retained for every other
case, including a signaled KERNEL entry. Each batch checks the current
list again, so no result is cached across PTE batches.

Offline operation counts illustrate both sides of the tradeoff:
- 64 batches, one BOOKKEEP entry: list reads remain 64; fence get,
  put and poll operations each fall from 64 to 0.
- 64 batches, four mixed entries including a signaled KERNEL entry:
  list reads rise from 256 to 512; the original 256 get/put/poll
  operations are retained.
These are model operation counts, not a measured speedup. The fast
path still scans entries and does not reduce the number of batches.

Local validation using functions extracted from this native tree:
- 28 deterministic scenarios.
- 128000 bounded modeled schedules / 256000 actual-function runs,
  40 checks adding dependencies between batches, and two deliberately
  broken controls that were detected. ASan/UBSan reported no errors.
- amdgpu_vm_cpu.o compiled with GCC 13 and CONFIG_DRM_AMDGPU=m in an
  isolated build directory. Strict checkpatch: 0 errors/warnings/checks.

This has not been linked into a full module, loaded, or tested in a
live candidate-driver A/B. The models do not establish real kernel
concurrency correctness or wall-clock benefit. There is no claim that
this eliminates the original multi-second GPU queue stalls.

The implementation is available on my fork (no GitHub PR opened):
https://github.com/zihaomu/amdgpu/tree/perf/vm-cpu-no-kernel-fastpath
It is separate from the PTE backend error-propagation RFC.

 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c
index d03b9bbe2..5712ec48a 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c
@@ -58,6 +58,28 @@ static int amdgpu_vm_cpu_prepare(struct 
amdgpu_vm_update_params *p,
        return amdgpu_sync_wait(sync, true);
 }
 
+static long amdgpu_vm_cpu_wait(struct amdgpu_vm_update_params *p,
+                              struct amdgpu_bo_vm *vmbo)
+{
+       struct dma_resv *resv = vmbo->bo.tbo.base.resv;
+       struct dma_resv_iter cursor;
+
+       if (!p->unlocked) {
+               /*
+                * Page tables share the locked root reservation. The locked
+                * iterator borrows its references and filters by usage without
+                * polling unrelated fences. Check each batch afresh, since the
+                * owner may add new dependencies between calls.
+                */
+               dma_resv_iter_begin(&cursor, resv, DMA_RESV_USAGE_KERNEL);
+               if (!dma_resv_iter_first(&cursor))
+                       return MAX_SCHEDULE_TIMEOUT;
+       }
+
+       return dma_resv_wait_timeout(resv, DMA_RESV_USAGE_KERNEL,
+                                    true, MAX_SCHEDULE_TIMEOUT);
+}
+
 /**
  * amdgpu_vm_cpu_update - helper to update page tables via CPU
  *
@@ -80,8 +102,7 @@ static int amdgpu_vm_cpu_update(struct 
amdgpu_vm_update_params *p,
        uint64_t value;
        long r;
 
-       r = dma_resv_wait_timeout(vmbo->bo.tbo.base.resv, DMA_RESV_USAGE_KERNEL,
-                                 true, MAX_SCHEDULE_TIMEOUT);
+       r = amdgpu_vm_cpu_wait(p, vmbo);
        if (r < 0)
                return r;
 
-- 
2.43.0

Reply via email to