On Thu, Dec 03, 2026 at 09:35:23AM -0800, Niranjana Vishwanathapura wrote:
> On Wed, Dec 02, 2026 at 03:33:22PM -0800, Matthew Brost wrote:
> > Add a bo_defrag debugfs file to inspect the state of the BO
> > defragmentation worker. It reports whether defrag is supported, the
> > beneficial page order, the per-run size limit, the current (possibly
> > backed-off) worker interval along with its default and maximum, whether
> > the worker is pending (and when it will next run), running or idle, and
> > the number of BOs on the defrag list.
> > 
> > Each BO on the defrag list is then printed with its size, the number of
> > bytes still backed below the beneficial order (the bytes a defrag move
> > would reallocate) and whether it still needs defrag, followed by totals.
> > 
> > A reference to each listed BO is taken under the defrag list lock and
> > the BO is then locked to sample its backing, as the tt can be torn down
> > while the BO is still on the list. The per-BO output is therefore a
> > best-effort snapshot.
> > 
> > Cc: Carlos Santa <[email protected]>
> > Cc: Ryan Neph <[email protected]>
> > Cc: Matthew Auld <[email protected]>
> > Cc: Thomas Hellström <[email protected]>
> > Cc: Natalie Vock <[email protected]>
> > Cc: Arun Pravin <[email protected]>
> > Signed-off-by: Matthew Brost <[email protected]>
> > ---
> > drivers/gpu/drm/xe/xe_bo.c      | 97 +++++++++++++++++++++++++++++++++
> > drivers/gpu/drm/xe/xe_bo.h      |  1 +
> > drivers/gpu/drm/xe/xe_debugfs.c |  9 +++
> > 3 files changed, 107 insertions(+)
> > 
> > diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
> > index 65ab974c56ac..85552da80bf5 100644
> > --- a/drivers/gpu/drm/xe/xe_bo.c
> > +++ b/drivers/gpu/drm/xe/xe_bo.c
> > @@ -2476,6 +2476,103 @@ static void xe_bo_defrag_update(struct xe_bo *bo)
> >             xe_bo_defrag_remove(bo);
> > }
> > 
> > +/**
> > + * xe_bo_defrag_print() - Print the BO defragmentation state
> > + * @xe: xe device instance
> > + * @p: The drm_printer to print to
> > + *
> > + * Print the defrag worker configuration and state, followed by each BO on 
> > the
> > + * defrag list with its size and the bytes still backed below the 
> > beneficial
> > + * order (i.e. the bytes a defrag move would reallocate). A reference to 
> > each
> > + * listed BO is taken under the list lock and the BO is then locked to 
> > sample
> > + * its backing, so the per-BO output is a best-effort snapshot.
> > + *
> > + * Return: 0 on success, negative error code on failure.
> > + */
> > +int xe_bo_defrag_print(struct xe_device *xe, struct drm_printer *p)
> > +{
> > +   struct delayed_work *dwork = &xe->mem.defrag.worker;
> > +   u64 total_size = 0, total_subopt = 0;
> > +   unsigned int count = 0, nr = 0, i;
> > +   unsigned int interval_ms;
> > +   struct xe_bo **bos, *bo;
> > +   int err = 0;
> > +
> > +   drm_printf(p, "supported: %s\n",
> > +              str_yes_no(xe_bo_defrag_supported(xe)));
> > +   if (!xe_bo_defrag_supported(xe))
> > +           return 0;
> > +
> > +   scoped_guard(spinlock, &xe->mem.defrag.lock) {
> > +           interval_ms = xe->mem.defrag.interval_ms;
> > +           list_for_each_entry(bo, &xe->mem.defrag.list, defrag_link)
> > +                   count++;
> > +   }
> > +
> > +   drm_printf(p, "beneficial_order: %u\n",
> > +              ttm_pool_prealloc_order(&xe->ttm.pool));
> > +   drm_printf(p, "size_limit: %llu\n", (u64)XE_BO_DEFRAG_SIZE_LIMIT);
> > +   drm_printf(p, "interval_ms: %u (default %u, max %u)\n", interval_ms,
> > +              XE_BO_DEFRAG_INTERVAL_MS, XE_BO_DEFRAG_INTERVAL_MAX_MS);
> > +   if (delayed_work_pending(dwork)) {
> > +           long delay = (long)(READ_ONCE(dwork->timer.expires) - jiffies);
> > +
> > +           drm_printf(p, "worker: pending, next run in %u ms\n",
> > +                      jiffies_to_msecs(max(delay, 0L)));
> > +   } else {
> > +           drm_printf(p, "worker: %s\n",
> > +                      work_busy(&dwork->work) ? "running" : "idle");
> > +   }
> > +   drm_printf(p, "list_count: %u\n", count);
> > +   if (!count)
> > +           return 0;
> > +
> > +   bos = kvmalloc_array(count, sizeof(*bos), GFP_KERNEL);
> > +   if (!bos)
> > +           return -ENOMEM;
> > +
> > +   /* The list may have changed while unlocked; snapshot at most @count. */
> > +   scoped_guard(spinlock, &xe->mem.defrag.lock) {
> > +           list_for_each_entry(bo, &xe->mem.defrag.list, defrag_link) {
> > +                   if (nr == count)
> > +                           break;
> > +                   bos[nr] = xe_bo_get_unless_zero(bo);
> > +                   if (bos[nr])
> > +                           nr++;
> > +           }
> > +   }
> > +
> > +   for (i = 0; i < nr; i++) {
> > +           u64 size, subopt = 0;
> > +           bool needs;
> > +
> > +           bo = bos[i];
> > +           if (!err)
> > +                   err = xe_bo_lock(bo, true);
> > +           if (!err) {
> > +                   size = xe_bo_size(bo);
> > +                   if (bo->ttm.ttm)
> > +                           subopt = 
> > (u64)ttm_tt_suboptimal_pages(bo->ttm.ttm) <<
> > +                                   PAGE_SHIFT;
> > +                   needs = xe_bo_needs_defrag(bo);
> > +                   xe_bo_unlock(bo);
> > +
> > +                   drm_printf(p, "  bo[%u] %p: size=%llu suboptimal=%llu 
> > needs_defrag=%s\n",
> > +                              i, bo, size, subopt, str_yes_no(needs));
> > +                   total_size += size;
> > +                   total_subopt += subopt;
> > +           }
> > +           xe_bo_put(bo);
> > +   }
> > +   kvfree(bos);
> > +
> > +   if (!err)
> > +           drm_printf(p, "total: bos=%u size=%llu suboptimal=%llu\n",
> > +                      nr, total_size, total_subopt);
> > +
> 
> NIT...so, in case of a BO locking error in the middle of the above loop,
> we will endup with prining BO info of some BOs (before the error) and will
> not print the totals, which is also not clean. May be we should just skip
> the BO that ran into error and continue printing the rest and the totals?
> As any difference between 'count' printed above and 'bos' printed here will
> tell it is best-effort snapshot?
> 

The only way the locking can fail is on user signal (e.g., ctrl-c), once
this occurs we should stop what we are doing and abort. I don't think we
need to over engineer this case and partial print is fine.

Matt

> > +   return err;
> > +}
> 
> NIT...Also, the patch 36 updates some fields of this function. May be it
> is cleaner to swap the order of patches to minimize such changes.
> 
> In anycase,
> Reviewed-by: Niranjana Vishwanathapura <[email protected]>
> 
> > +
> > /*
> >  * Attempt to defragment a single BO by forcing a move that reallocates its
> >  * backing at the device's beneficial order. @budget bounds how many bytes 
> > of
> > diff --git a/drivers/gpu/drm/xe/xe_bo.h b/drivers/gpu/drm/xe/xe_bo.h
> > index 871d1aa1425c..28325341b4d1 100644
> > --- a/drivers/gpu/drm/xe/xe_bo.h
> > +++ b/drivers/gpu/drm/xe/xe_bo.h
> > @@ -241,6 +241,7 @@ int xe_bo_defrag_init(struct xe_device *xe);
> > void xe_bo_defrag_suspend(struct xe_device *xe);
> > void xe_bo_defrag_resume(struct xe_device *xe);
> > void xe_bo_defrag_kick(struct xe_device *xe);
> > +int xe_bo_defrag_print(struct xe_device *xe, struct drm_printer *p);
> > int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict,
> >                struct drm_exec *exec);
> > 
> > diff --git a/drivers/gpu/drm/xe/xe_debugfs.c 
> > b/drivers/gpu/drm/xe/xe_debugfs.c
> > index be533435097f..b070835b8d24 100644
> > --- a/drivers/gpu/drm/xe/xe_debugfs.c
> > +++ b/drivers/gpu/drm/xe/xe_debugfs.c
> > @@ -372,11 +372,20 @@ static int dma_mapped_pages_show(struct seq_file *m, 
> > void *data)
> > }
> > DEFINE_SHOW_ATTRIBUTE(dma_mapped_pages);
> > 
> > +static int bo_defrag(struct seq_file *m, void *data)
> > +{
> > +   struct xe_device *xe = node_to_xe(m->private);
> > +   struct drm_printer p = drm_seq_file_printer(m);
> > +
> > +   return xe_bo_defrag_print(xe, &p);
> > +}
> > +
> > static const struct drm_info_list debugfs_list[] = {
> >     {"info", info, 0},
> >     { .name = "sriov_info", .show = sriov_info, },
> >     { .name = "workarounds", .show = workaround_info, },
> >     { .name = "pagefault_info", .show = pagefault_info, },
> > +   { .name = "bo_defrag", .show = bo_defrag, },
> > };
> > 
> > static const struct drm_info_list pcode_info_debugfs[] = {
> > -- 
> > 2.34.1
> > 

Reply via email to