Re: [PATCH v6 10/12] drm/xe/bo: Add purgeable shrinker state helpers

Thomas Hellström
2026-03-10T10:01:14Z

On Tue, 2026-03-03 at 20:50 +0530, Arvind Yadav wrote:
> Encapsulate TTM purgeable flag updates and shrinker page accounting
> into helper functions. This prevents desynchronization between the
> TTM tt->purgeable flag and the shrinker's page bucket counters.
>
> Without these helpers, direct manipulation of xe_ttm_tt->purgeable
> risks forgetting to update the corresponding shrinker counters,
> leading to incorrect memory pressure calculations.
>
> Add xe_bo_set_purgeable_shrinker() and
> xe_bo_clear_purgeable_shrinker()
> which atomically update both the TTM flag and transfer pages between
> the shrinkable and purgeable buckets.
>
> Update purgeable BO state to PURGED after successful shrinker purge
> for DONTNEED BOs.
>
> v4:
>   - @madv_purgeable atomic_t → u32 change across all relevant
>     patches (Matt)
>
> v5:
>   - Update purgeable BO state to PURGED after a successful shrinker
>     purge for DONTNEED BOs.
>   - Split ghost BO and zero-refcount handling in xe_bo_shrink()
> (Thomas)
>
> v6:
>   - Create separate patch for 'Split ghost BO and zero-refcount
>     handling'. (Thomas)
>
> Cc: Matthew Brost <matthew.brost@intel.com>
> Cc: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com>
> Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
> Signed-off-by: Arvind Yadav <arvind.yadav@intel.com>
> ---
>  drivers/gpu/drm/xe/xe_bo.c         | 63
> ++++++++++++++++++++++++++++++
>  drivers/gpu/drm/xe/xe_bo.h         |  2 +
>  drivers/gpu/drm/xe/xe_vm_madvise.c |  8 +++-
>  3 files changed, 71 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
> index 3a4965bdadf2..598d4463baf3 100644
> --- a/drivers/gpu/drm/xe/xe_bo.c
> +++ b/drivers/gpu/drm/xe/xe_bo.c
> @@ -863,6 +863,66 @@ void xe_bo_set_purgeable_state(struct xe_bo *bo,
>   bo->madv_purgeable = new_state;
>  }
>
> +/**
> + * xe_bo_set_purgeable_shrinker() - Mark BO purgeable and update
> shrinker
> + * @bo: Buffer object
> + *
> + * Transfers pages from shrinkable to purgeable bucket. Shrinker can
> now
> + * discard pages immediately without swapping. Caller holds BO lock.
> + */
> +void xe_bo_set_purgeable_shrinker(struct xe_bo *bo)
> +{
> + struct ttm_buffer_object *ttm_bo = &bo->ttm;
> + struct ttm_tt *tt = ttm_bo->ttm;
> + struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
> + struct xe_ttm_tt *xe_tt;
> +
> + xe_bo_assert_held(bo);
> +
> + if (!tt || !ttm_tt_is_populated(tt))
> + return;
> +
> + xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
> +
> + if (!xe_tt->purgeable) {
> + xe_tt->purgeable = true;
> + /* Transfer pages from shrinkable to purgeable count
> */
> + xe_shrinker_mod_pages(xe->mem.shrinker,
> +       -(long)tt->num_pages,
> +       tt->num_pages);
> + }
> +}
> +
> +/**
> + * xe_bo_clear_purgeable_shrinker() - Mark BO non-purgeable and
> update shrinker
> + * @bo: Buffer object
> + *
> + * Transfers pages from purgeable to shrinkable bucket. Shrinker
> must now
> + * swap pages instead of discarding. Caller holds BO lock.
> + */
> +void xe_bo_clear_purgeable_shrinker(struct xe_bo *bo)
> +{
> + struct ttm_buffer_object *ttm_bo = &bo->ttm;
> + struct ttm_tt *tt = ttm_bo->ttm;
> + struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
> + struct xe_ttm_tt *xe_tt;
> +
> + xe_bo_assert_held(bo);
> +
> + if (!tt || !ttm_tt_is_populated(tt))
> + return;
> +
> + xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
> +
> + if (xe_tt->purgeable) {
> + xe_tt->purgeable = false;
> + /* Transfer pages from purgeable to shrinkable count
> */
> + xe_shrinker_mod_pages(xe->mem.shrinker,
> +       tt->num_pages,
> +       -(long)tt->num_pages);
> + }
> +}
> +
>  /**
>   * xe_ttm_bo_purge() - Purge buffer object backing store
>   * @ttm_bo: The TTM buffer object to purge
> @@ -1243,6 +1303,9 @@ long xe_bo_shrink(struct ttm_operation_ctx
> *ctx, struct ttm_buffer_object *bo,
>   lret = xe_bo_move_notify(xe_bo, ctx);
>   if (!lret)
>   lret = xe_bo_shrink_purge(ctx, bo, scanned);
> + if (lret > 0 && xe_bo_madv_is_dontneed(xe_bo))
> + xe_bo_set_purgeable_state(xe_bo,
> +
> XE_MADV_PURGEABLE_PURGED);
>   goto out_unref;
>   }
>
> diff --git a/drivers/gpu/drm/xe/xe_bo.h b/drivers/gpu/drm/xe/xe_bo.h
> index 0d9f25b51eb2..46d1fff10e4f 100644
> --- a/drivers/gpu/drm/xe/xe_bo.h
> +++ b/drivers/gpu/drm/xe/xe_bo.h
> @@ -272,6 +272,8 @@ static inline bool xe_bo_madv_is_dontneed(struct
> xe_bo *bo)
>  }
>
>  void xe_bo_set_purgeable_state(struct xe_bo *bo, enum
> xe_madv_purgeable_state new_state);
> +void xe_bo_set_purgeable_shrinker(struct xe_bo *bo);
> +void xe_bo_clear_purgeable_shrinker(struct xe_bo *bo);
>
>  static inline void xe_bo_unpin_map_no_vm(struct xe_bo *bo)
>  {
> diff --git a/drivers/gpu/drm/xe/xe_vm_madvise.c
> b/drivers/gpu/drm/xe/xe_vm_madvise.c
> index 8acc19e25aa5..ab83e94980e4 100644
> --- a/drivers/gpu/drm/xe/xe_vm_madvise.c
> +++ b/drivers/gpu/drm/xe/xe_vm_madvise.c
> @@ -312,12 +312,16 @@ void xe_bo_recompute_purgeable_state(struct
> xe_bo *bo)
>
>   if (vma_state == XE_BO_VMAS_STATE_DONTNEED) {
>   /* All VMAs are DONTNEED - mark BO purgeable */
> - if (bo->madv_purgeable !=
> XE_MADV_PURGEABLE_DONTNEED)
> + if (bo->madv_purgeable !=
> XE_MADV_PURGEABLE_DONTNEED) {
>   xe_bo_set_purgeable_state(bo,
> XE_MADV_PURGEABLE_DONTNEED);
> + xe_bo_set_purgeable_shrinker(bo);
> + }
>   } else if (vma_state == XE_BO_VMAS_STATE_WILLNEED) {
>   /* At least one VMA is WILLNEED - BO must not be
> purgeable */
> - if (bo->madv_purgeable !=
> XE_MADV_PURGEABLE_WILLNEED)
> + if (bo->madv_purgeable !=
> XE_MADV_PURGEABLE_WILLNEED) {
>   xe_bo_set_purgeable_state(bo,
> XE_MADV_PURGEABLE_WILLNEED);
> + xe_bo_clear_purgeable_shrinker(bo);
> + }
>   }
>   /* XE_BO_VMAS_STATE_NO_VMAS: Preserve existing BO state */
>  }

I think this can be simplified a bit using something like the below
applied after the above patch: (untested).

diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
index 07acce383cb1..9f0885cd3cfd 100644
--- a/drivers/gpu/drm/xe/xe_bo.c
+++ b/drivers/gpu/drm/xe/xe_bo.c
@@ -835,47 +835,14 @@ static int xe_bo_move_notify(struct xe_bo *bo,
return 0;
}

-/**
- * xe_bo_set_purgeable_state() - Set BO purgeable state with
validation
- * @bo: Buffer object
- * @new_state: New purgeable state
- *
- * Sets the purgeable state with lockdep assertions and validates
state
- * transitions. Once a BO is PURGED, it cannot transition to any other
state.
- * Invalid transitions are caught with xe_assert().
- */
-void xe_bo_set_purgeable_state(struct xe_bo *bo,
- enum xe_madv_purgeable_state new_state)
-{
- struct xe_device *xe = xe_bo_device(bo);
-
- xe_bo_assert_held(bo);
-
- /* Validate state is one of the known values */
- xe_assert(xe, new_state == XE_MADV_PURGEABLE_WILLNEED ||
- new_state == XE_MADV_PURGEABLE_DONTNEED ||
- new_state == XE_MADV_PURGEABLE_PURGED);
-
- /* Once purged, always purged - cannot transition out */
- xe_assert(xe, !(bo->madv_purgeable == XE_MADV_PURGEABLE_PURGED
&&
- new_state != XE_MADV_PURGEABLE_PURGED));
-
- bo->madv_purgeable = new_state;
-}
-
-/**
- * xe_bo_set_purgeable_shrinker() - Mark BO purgeable and update
shrinker
- * @bo: Buffer object
- *
- * Transfers pages from shrinkable to purgeable bucket. Shrinker can
now
- * discard pages immediately without swapping. Caller holds BO lock.
- */
-void xe_bo_set_purgeable_shrinker(struct xe_bo *bo)
+static void xe_bo_set_purgeable_shrinker(struct xe_bo *bo, enum
xe_madv_purgeable_state new_state)
+
{
struct ttm_buffer_object *ttm_bo = &bo->ttm;
struct ttm_tt *tt = ttm_bo->ttm;
struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
struct xe_ttm_tt *xe_tt;
+ long int tt_pages;

xe_bo_assert_held(bo);

@@ -883,44 +850,44 @@ void xe_bo_set_purgeable_shrinker(struct xe_bo
*bo)
return;

xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
-
- if (!xe_tt->purgeable) {
+ tt_pages = tt->num_pages;
+
+ if (!xe_tt->purgeable && new_state ==
XE_MADV_PURGEABLE_DONTNEED) {
xe_tt->purgeable = true;
- /* Transfer pages from shrinkable to purgeable count
*/
- xe_shrinker_mod_pages(xe->mem.shrinker,
- -(long)tt->num_pages,
- tt->num_pages);
+ xe_shrinker_mod_pages(xe->mem.shrinker, -tt_pages,
tt_pages);
+ } else if (xe_tt->purgeable && new_state ==
XE_MADV_PURGEABLE_WILLNEED) {
+ xe_tt->purgeable = false;
+ xe_shrinker_mod_pages(xe->mem.shrinker, tt_pages, -
tt_pages);
}
}

/**
- * xe_bo_clear_purgeable_shrinker() - Mark BO non-purgeable and update
shrinker
+ * xe_bo_set_purgeable_state() - Set BO purgeable state with
validation
* @bo: Buffer object
+ * @new_state: New purgeable state
*
- * Transfers pages from purgeable to shrinkable bucket. Shrinker must
now
- * swap pages instead of discarding. Caller holds BO lock.
+ * Sets the purgeable state with lockdep assertions and validates
state
+ * transitions. Once a BO is PURGED, it cannot transition to any other
state.
+ * Invalid transitions are caught with xe_assert().
*/
-void xe_bo_clear_purgeable_shrinker(struct xe_bo *bo)
+void xe_bo_set_purgeable_state(struct xe_bo *bo
+, enum xe_madv_purgeable_state new_state)
{
- struct ttm_buffer_object *ttm_bo = &bo->ttm;
- struct ttm_tt *tt = ttm_bo->ttm;
- struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
- struct xe_ttm_tt *xe_tt;
+ struct xe_device *xe = xe_bo_device(bo);

xe_bo_assert_held(bo);

- if (!tt || !ttm_tt_is_populated(tt))
- return;
+ /* Validate state is one of the known values */
+ xe_assert(xe, new_state == XE_MADV_PURGEABLE_WILLNEED ||
+ new_state == XE_MADV_PURGEABLE_DONTNEED ||
+ new_state == XE_MADV_PURGEABLE_PURGED);

- xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+ /* Once purged, always purged - cannot transition out */
+ xe_assert(xe, !(bo->madv_purgeable == XE_MADV_PURGEABLE_PURGED
&&
+ new_state != XE_MADV_PURGEABLE_PURGED));

- if (xe_tt->purgeable) {
- xe_tt->purgeable = false;
- /* Transfer pages from purgeable to shrinkable count
*/
- xe_shrinker_mod_pages(xe->mem.shrinker,
- tt->num_pages,
- -(long)tt->num_pages);
- }
+ bo->madv_purgeable = new_state;
+ xe_bo_set_purgeable_shrinker(bo, new_state);
}

/**
diff --git a/drivers/gpu/drm/xe/xe_bo.h b/drivers/gpu/drm/xe/xe_bo.h
index 46d1fff10e4f..0d9f25b51eb2 100644
--- a/drivers/gpu/drm/xe/xe_bo.h
+++ b/drivers/gpu/drm/xe/xe_bo.h
@@ -272,8 +272,6 @@ static inline bool xe_bo_madv_is_dontneed(struct
xe_bo *bo)
}

void xe_bo_set_purgeable_state(struct xe_bo *bo, enum
xe_madv_purgeable_state new_state);
-void xe_bo_set_purgeable_shrinker(struct xe_bo *bo);
-void xe_bo_clear_purgeable_shrinker(struct xe_bo *bo);

static inline void xe_bo_unpin_map_no_vm(struct xe_bo *bo)
{