From 508444b9f6fc9f2862a08fffd6bebfb790cd0dc4 Mon Sep 17 00:00:00 2001 From: Himal Prasad Ghimiray Date: Thu, 1 Oct 2026 09:06:06 +0530 Subject: [PATCH] drm/xe: Bump prefetch BO LRU via GPUSVM devmem callback Add xe_svm_devmem_lru_bump() and set it as drm_gpusvm_ctx.devmem_fn in the prefetch worker so each backing BO is moved to the LRU tail during get_pages. v2: - Guard xe_svm_devmem_lru_bump() against foreign device-memory allocations before casting to xe_bo: return early when devmem_allocation->ops != &dpagemap_devmem_ops. A shared device_private_page_owner can hand us another driver's allocation. (Matthew Brost / Sashiko AI review) - Add stub for function (CI) v3: - Guard xe_svm_devmem_lru_bump() body with CONFIG_DRM_XE_PAGEMAP; it references dpagemap_devmem_ops and to_xe_bo(), which only exist when PAGEMAP is enabled. Fixes the build with GPUSVM=y, PAGEMAP=n. (Sashiko AI review) v4: - Fix xe_svm_devmem_lru_bump() kernel-doc to explain the locking (Matt) Suggested-by: Matthew Brost Reviewed-by: Matthew Brost Link: https://patch.msgid.link/20261001033602.3166274-9-himal.prasad.ghimiray@intel.com Signed-off-by: Himal Prasad Ghimiray Signed-off-by: Rodrigo Vivi --- drivers/gpu/drm/xe/xe_svm.c | 34 ++++++++++++++++++++++++++++++++++ drivers/gpu/drm/xe/xe_svm.h | 7 +++++++ drivers/gpu/drm/xe/xe_vm.c | 1 + 3 files changed, 42 insertions(+) diff --git a/drivers/gpu/drm/xe/xe_svm.c b/drivers/gpu/drm/xe/xe_svm.c index f39e647512ad..d07dd95ab451 100644 --- a/drivers/gpu/drm/xe/xe_svm.c +++ b/drivers/gpu/drm/xe/xe_svm.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "xe_bo.h" #include "xe_exec_queue_types.h" @@ -1625,6 +1626,39 @@ int xe_svm_range_get_pages(struct xe_vm *vm, struct xe_svm_range *range, return err; } +/** + * xe_svm_devmem_lru_bump() - Move a range's backing BO to the TTM LRU tail + * @devmem_allocation: The device-memory allocation backing the range's pages + * + * Intended as a &drm_gpusvm_ctx.devmem_fn, so drm_gpusvm_get_pages() calls it + * under the GPUSVM notifier lock. That lock sits in the reclaim path, so + * dma-resv cannot be acquired blocking beneath it; dma-resv is also the outer + * lock of the notifier when memory is allocated and migrated, so a blocking + * acquire here would invert that order. Hence the trylock. The BO is private + * (dedicated dma-resv), so the trylock succeeds unless the BO is being moved + * elsewhere (e.g. eviction); a contended BO is simply skipped. + * + * A shared device_private_page_owner can hand us allocations backed by a + * foreign driver; skip anything that is not an Xe BO before casting. + */ +void xe_svm_devmem_lru_bump(struct drm_pagemap_devmem *devmem_allocation) +{ +#if IS_ENABLED(CONFIG_DRM_XE_PAGEMAP) + struct xe_bo *bo; + + if (devmem_allocation->ops != &dpagemap_devmem_ops) + return; + + bo = to_xe_bo(devmem_allocation); + + if (!dma_resv_trylock(bo->ttm.base.resv)) + return; + + ttm_bo_move_to_lru_tail_unlocked(&bo->ttm); + dma_resv_unlock(bo->ttm.base.resv); +#endif +} + /** * xe_svm_ranges_zap_ptes_in_range - clear ptes of svm ranges in input range * @vm: Pointer to the xe_vm structure diff --git a/drivers/gpu/drm/xe/xe_svm.h b/drivers/gpu/drm/xe/xe_svm.h index 2ef4ef026ccd..585ad64b672b 100644 --- a/drivers/gpu/drm/xe/xe_svm.h +++ b/drivers/gpu/drm/xe/xe_svm.h @@ -128,6 +128,8 @@ struct xe_svm_range *xe_svm_range_find_or_insert(struct xe_vm *vm, u64 addr, int xe_svm_range_get_pages(struct xe_vm *vm, struct xe_svm_range *range, struct drm_gpusvm_ctx *ctx); +void xe_svm_devmem_lru_bump(struct drm_pagemap_devmem *devmem_allocation); + bool xe_svm_range_needs_migrate_to_vram(struct xe_svm_range *range, struct xe_vma *vma, const struct drm_pagemap *dpagemap); @@ -356,6 +358,11 @@ int xe_svm_range_get_pages(struct xe_vm *vm, struct xe_svm_range *range, return -EINVAL; } +static inline +void xe_svm_devmem_lru_bump(struct drm_pagemap_devmem *devmem_allocation) +{ +} + static inline struct xe_svm_range *to_xe_range(struct drm_gpusvm_range *r) { return NULL; diff --git a/drivers/gpu/drm/xe/xe_vm.c b/drivers/gpu/drm/xe/xe_vm.c index ea5bfb307db1..986e0ebfdacd 100644 --- a/drivers/gpu/drm/xe/xe_vm.c +++ b/drivers/gpu/drm/xe/xe_vm.c @@ -3246,6 +3246,7 @@ static int prefetch_ranges(struct xe_vm *vm, struct xe_vma_ops *vops, ctx.devmem_possible = devmem_possible; ctx.check_pages_threshold = devmem_possible ? SZ_64K : 0; ctx.device_private_page_owner = xe_svm_private_page_owner(vm, !dpagemap); + ctx.devmem_fn = xe_svm_devmem_lru_bump; skip_threads = op->prefetch_range.ranges_count == 1 || (!dpagemap && !(vops->flags &