On Sun, Nov 08, 2026 at 05:10:30PM -0800, Niranjana Vishwanathapura wrote:
> On Fri, Jul 10, 2026 at 07:56:07PM -0700, Matthew Brost wrote:
> > Add observability for the page defragmentation worker.
> > 
> > Introduce four primary-GT statistics counters: defrag_added_count
> > (BOs queued onto the defrag list after being backed at a sub-optimal
> > page order), defrag_success_count (BOs fully re-backed at the beneficial
> > order), defrag_partial_success_count (BOs only partially re-backed at the
> > beneficial order) defrag_failed_count (attempts that failed and left the
> > BO on its original backing) and defrag_mb_moved. The
> > ineligible/already-removed fast path is intentionally not counted as
> > either success or failure.
> > 
> > Cc: Carlos Santa <[email protected]>
> > Cc: Ryan Neph <[email protected]>
> > Cc: Christian Koenig <[email protected]>
> > Cc: Huang Rui <[email protected]>
> > Cc: Matthew Auld <[email protected]>
> > Cc: Maarten Lankhorst <[email protected]>
> > Cc: Maxime Ripard <[email protected]>
> > Cc: Thomas Zimmermann <[email protected]>
> > Cc: David Airlie <[email protected]>
> > Cc: Simona Vetter <[email protected]>
> > Cc: [email protected]
> > Cc: [email protected]
> > Cc: Thomas Hellström <[email protected]>
> > Assisted-by: GitHub_Copilot:claude-opus-4.8
> > Signed-off-by: Matthew Brost <[email protected]>
> > ---
> > drivers/gpu/drm/xe/xe_bo.c             | 18 +++++++++++++++++-
> > drivers/gpu/drm/xe/xe_gt_stats.c       |  5 +++++
> > drivers/gpu/drm/xe/xe_gt_stats_types.h | 17 +++++++++++++++++
> > 3 files changed, 39 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
> > index 907cfa57481f..073d926ef9ac 100644
> > --- a/drivers/gpu/drm/xe/xe_bo.c
> > +++ b/drivers/gpu/drm/xe/xe_bo.c
> > @@ -26,6 +26,7 @@
> > #include "xe_dma_buf.h"
> > #include "xe_drm_client.h"
> > #include "xe_ggtt.h"
> > +#include "xe_gt_stats.h"
> > #include "xe_map.h"
> > #include "xe_migrate.h"
> > #include "xe_pat.h"
> > @@ -1160,6 +1161,8 @@ static void xe_bo_defrag_add(struct xe_bo *bo)
> >                             xe->mem.defrag.interval_ms = 
> > XE_BO_DEFRAG_INTERVAL_MS;
> >                     list_add_tail(&bo->defrag_link, &xe->mem.defrag.list);
> >                     atomic_inc(&xe->mem.defrag.count);
> > +                   xe_gt_stats_incr(xe_root_mmio_gt(xe),
> > +                                    XE_GT_STATS_ID_DEFRAG_ADDED_COUNT, 1);
> 
> Assisted-by: LLM
> 

Yes.

> I am not sure if ADDED_COUNT is much helpful here. But BOs added here can get 
> silently
> dropped. Also, a silently dropped BO can get re-added.
> 

That is in fact true and if you run the xe_defrag IGT this happens a
lot. I personally find this one useful and so has the ALOS team.

> Perhaps a better count would be a 'defrag_pending byte guage'? Also, probably 
> make it
> not-resettable so that it gives the current state of fragmentation.
> 

I think for something like that debugfs would be the better choice
rather than GT stats - e.g., Take spin_lock walk the defrag list and
which shows the current state. This is a different patch though but
probably worth adding.

> >             }
> >     }
> > 
> > @@ -1274,8 +1277,21 @@ static int xe_bo_defrag_one(struct xe_device *xe, 
> > struct xe_bo *bo,
> >     xe_dbg(xe, "Defrag attempt on BO size=%zu: ret=%pe consumed=%llu\n",
> >            xe_bo_size(bo), ERR_PTR(ret), *consumed);
> > 
> > -   if (!ret && ttm_tt_is_beneficial_order_failed(bo->ttm.ttm))
> > +   if (!ret && ttm_tt_is_beneficial_order_failed(bo->ttm.ttm)) {
> >             *needs_more = true;
> > +           xe_gt_stats_incr(xe_root_mmio_gt(xe),
> > +                            XE_GT_STATS_ID_DEFRAG_PARTIAL_SUCCESS_COUNT,
> > +                            1);
> > +   } else {
> > +           xe_gt_stats_incr(xe_root_mmio_gt(xe),
> > +                            ret ? XE_GT_STATS_ID_DEFRAG_FAILED_COUNT :
> > +                                  XE_GT_STATS_ID_DEFRAG_SUCCESS_COUNT, 1);
> > +   }
> > +
> > +   if (!ret)
> > +           xe_gt_stats_incr(xe_root_mmio_gt(xe),
> > +                            XE_GT_STATS_ID_DEFRAG_MB_MOVED,
> > +                            *consumed >> 20);
> 
> This will truncate the size which can accumulate over multiple BOs and the 
> size
> here will be significantly off from real size that is moved.
> 

I'm not following. Consumed is the number of newly pruned bytes, thus
the total number of bytes copied on the migrate queue. A single BO can
prune in anywhere from 2M to DRM_XE_BO_DEFRAG_SIZE_LIMIT per
xe_bo_defrag_one call when !ret is true.

I use these stats all the time and they seem accurate.

> > 
> > unlock:
> >     xe_bo_unlock(bo);
> > diff --git a/drivers/gpu/drm/xe/xe_gt_stats.c 
> > b/drivers/gpu/drm/xe/xe_gt_stats.c
> > index 789397514f3e..68e60d9500c8 100644
> > --- a/drivers/gpu/drm/xe/xe_gt_stats.c
> > +++ b/drivers/gpu/drm/xe/xe_gt_stats.c
> > @@ -154,6 +154,11 @@ static const char *const 
> > stat_description[__XE_GT_STATS_NUM_IDS] = {
> >     DEF_STAT_STR(PRL_2M_ENTRY_COUNT, "prl_2m_entry_count"),
> >     DEF_STAT_STR(PRL_ISSUED_COUNT, "prl_issued_count"),
> >     DEF_STAT_STR(PRL_ABORTED_COUNT, "prl_aborted_count"),
> > +   DEF_STAT_STR(DEFRAG_ADDED_COUNT, "defrag_added_count"),
> > +   DEF_STAT_STR(DEFRAG_SUCCESS_COUNT, "defrag_success_count"),
> > +   DEF_STAT_STR(DEFRAG_PARTIAL_SUCCESS_COUNT, 
> > "defrag_partial_success_count"),
> > +   DEF_STAT_STR(DEFRAG_FAILED_COUNT, "defrag_failed_count"),
> > +   DEF_STAT_STR(DEFRAG_MB_MOVED, "defrag_mb_moved"),
> > };
> > 
> > /**
> > diff --git a/drivers/gpu/drm/xe/xe_gt_stats_types.h 
> > b/drivers/gpu/drm/xe/xe_gt_stats_types.h
> > index 425491bed6c4..c42b494390f8 100644
> > --- a/drivers/gpu/drm/xe/xe_gt_stats_types.h
> > +++ b/drivers/gpu/drm/xe/xe_gt_stats_types.h
> > @@ -122,6 +122,18 @@
> >  * @XE_GT_STATS_ID_PRL_ABORTED_COUNT: Times the page reclaim process was
> >  *   aborted.
> >  *
> > + * @XE_GT_STATS_ID_DEFRAG_ADDED_COUNT: Times a buffer object was added to 
> > the
> > + *   page defragmentation list after being backed at a sub-optimal page 
> > order.
> > + * @XE_GT_STATS_ID_DEFRAG_SUCCESS_COUNT: Times the defrag worker 
> > successfully
> > + *   re-backed a buffer object at the beneficial page order.
> > + * @XE_GT_STATS_ID_DEFRAG_PARTIAL_SUCCESS_COUNT: Times the defrag worker
> > + *   partially re-backed a buffer object at the beneficial page order.
> > + * @XE_GT_STATS_ID_DEFRAG_FAILED_COUNT: Times the defrag worker failed to
> > + *   re-back a buffer object at the beneficial page order.
> > + * @XE_GT_STATS_ID_DEFRAG_MB_MOVED: Data (MiB) actually (re)allocated while
> > + *   re-backing buffer objects at the beneficial page order. Chunks 
> > borrowed
> > + *   from the old backing for free are not counted.
> > + *
> 
> NIT...all GTs applied to root GT, which is fine, but probably update stats 
> documentation?
> 

Yes, let me add that.

> >  * @__XE_GT_STATS_NUM_IDS: Number of valid IDs; not a real counter.
> >  *
> >  * See Documentation/gpu/xe/xe_gt_stats.rst.
> > @@ -181,6 +193,11 @@ enum xe_gt_stats_id {
> >     XE_GT_STATS_ID_PRL_2M_ENTRY_COUNT,
> >     XE_GT_STATS_ID_PRL_ISSUED_COUNT,
> >     XE_GT_STATS_ID_PRL_ABORTED_COUNT,
> > +   XE_GT_STATS_ID_DEFRAG_ADDED_COUNT,
> > +   XE_GT_STATS_ID_DEFRAG_SUCCESS_COUNT,
> > +   XE_GT_STATS_ID_DEFRAG_PARTIAL_SUCCESS_COUNT,
> > +   XE_GT_STATS_ID_DEFRAG_FAILED_COUNT,
> > +   XE_GT_STATS_ID_DEFRAG_MB_MOVED,
> 
> NIT...Few other counts that might be helpful are,
> - defrag failure reason (DEFRAG_FAILED_BUSY/ENOMEM)

I practice ENOMEM is really the way it can fail (e.g., we ask the core
for 2M page with aggresive reclaim flags and it fails). So not sure if
this is really useful.

> - time cost of defrag (DEFRAG_US)

This is tricky one. What would DEFRAG_US measure here? The time to
allocate memory (e.g., reclaim time), the time to prune in memory under
dma-resv, or the copy job time?

You can figure out all the above via ftrace and that how I profiled /
tuned the implementation + how I believe the ALOS team if proving this
works / tuning Kconfig options so we don't miss page flips on a defrag.

> - 'removed_without_defrag' so that 'MB_MOVED + removed_without_defrag + 
> defrag_pending'
>   is the total suboptimal size added to defrag list.

Removed without a defrag is probably useful and perhaps a total of
suboptimal bytes added could be helpful too. Let me add those.

Matt

> 
> Niranjana
> 
> >     /* must be the last entry */
> >     __XE_GT_STATS_NUM_IDS,
> > };
> > -- 
> > 2.34.1
> > 

Reply via email to