Re: [PATCH v3 21/33] drm/xe: Add defrag GT stats

From: Matthew Brost

Date: Mon Nov 30 2026 - 18:04:50 EST


On Mon, Nov 30, 2026 at 03:02:29PM -0800, Niranjana Vishwanathapura wrote:
> On Tue, Nov 24, 2026 at 12:13:30PM -0800, Matthew Brost wrote:
> > 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 <carlos.santa@xxxxxxxxx>
> > > > Cc: Ryan Neph <ryanneph@xxxxxxxxxx>
> > > > Cc: Christian Koenig <christian.koenig@xxxxxxx>
> > > > Cc: Huang Rui <ray.huang@xxxxxxx>
> > > > Cc: Matthew Auld <matthew.auld@xxxxxxxxx>
> > > > Cc: Maarten Lankhorst <maarten.lankhorst@xxxxxxxxxxxxxxx>
> > > > Cc: Maxime Ripard <mripard@xxxxxxxxxx>
> > > > Cc: Thomas Zimmermann <tzimmermann@xxxxxxx>
> > > > Cc: David Airlie <airlied@xxxxxxxxx>
> > > > Cc: Simona Vetter <simona@xxxxxxxx>
> > > > Cc: dri-devel@xxxxxxxxxxxxxxxxxxxxx
> > > > Cc: linux-kernel@xxxxxxxxxxxxxxx
> > > > Cc: Thomas Hellström <thomas.hellstrom@xxxxxxxxxxxxxxx>
> > > > Assisted-by: GitHub_Copilot:claude-opus-4.8
> > > > Signed-off-by: Matthew Brost <matthew.brost@xxxxxxxxx>
> > > > ---
> > > > 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.
> >
>
> The concern is that we do the MB truncation (>> 20) per-call, before


consumed is also 2M aligned.

Matt

> adding to the running counter, rather than accumulating raw bytes and
> truncating once at report time. Each successful call can lose up to
> just under 1MB to truncation (e.g. consumed=2.4M becomes 2, not 2.4),
> and since xe_gt_stats_incr() only stores the already-truncated int,
> that lost remainder is gone for good. Over many defrag calls these
> per-call losses add up, so the counter can drift further from the real
> total the longer the system runs - not a one-time rounding fuzz.
>
> Would tracking the running total in bytes (u64) internally, and only
> right-shifting to MB at the point of the stats increment (or report
> time), avoid this?
>
> Niranjana
>
> > > >
> > > > 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
> > > >