Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Brost <matthew.brost@intel.com>
To: intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org
Cc: "Carlos Santa" <carlos.santa@intel.com>,
	"Ryan Neph" <ryanneph@google.com>,
	"Matthew Auld" <matthew.auld@intel.com>,
	"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
	"Natalie Vock" <nat@pixelcluster.dev>,
	"Arun Pravin" <arunpravin.paneerselvam@amd.com>
Subject: [PATCH v4 32/33] drm/xe: Add bo_defrag debugfs entry
Date: Tue,  1 Dec 2026 15:49:14 -0800	[thread overview]
Message-ID: <20261201234915.3305331-33-matthew.brost@intel.com> (raw)
In-Reply-To: <20261201234915.3305331-1-matthew.brost@intel.com>

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 <carlos.santa@intel.com>
Cc: Ryan Neph <ryanneph@google.com>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: Natalie Vock <nat@pixelcluster.dev>
Cc: Arun Pravin <arunpravin.paneerselvam@amd.com>
Assisted-by: LLM
Signed-off-by: Matthew Brost <matthew.brost@intel.com>
---
 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 db9a0c7bd44e..9c218b03210a 100644
--- a/drivers/gpu/drm/xe/xe_bo.c
+++ b/drivers/gpu/drm/xe/xe_bo.c
@@ -2415,6 +2415,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);
+
+	return err;
+}
+
 /*
  * 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 32bca8eaefb5..d0e72d244eae 100644
--- a/drivers/gpu/drm/xe/xe_bo.h
+++ b/drivers/gpu/drm/xe/xe_bo.h
@@ -239,6 +239,7 @@ void xe_bo_unpin_account(struct xe_bo *bo);
 int xe_bo_defrag_init_early(struct xe_device *xe);
 int xe_bo_defrag_init(struct xe_device *xe);
 void xe_bo_defrag_resume(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


  parent reply	other threads:[~2026-12-01 23:50 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 23:48 [PATCH v4 00/33] drm/ttm, drm/xe: Avoid foreground high-order reclaim and defragment sub-optimally backed BOs Matthew Brost
2026-12-01 23:48 ` [PATCH v4 01/33] drm/ttm/pool: Allow backing off reclaim at the beneficial order Matthew Brost
2026-12-01 23:48 ` [PATCH v4 02/33] drm/ttm/pool: Add ttm_pool_page_order_nodma() helper Matthew Brost
2026-12-01 23:48 ` [PATCH v4 03/33] drm/ttm: Record sub-optimal page order allocations in ttm_tt Matthew Brost
2026-12-01 23:48 ` [PATCH v4 04/33] drm/ttm: Introduce ttm_pool_alloc_iter for __ttm_pool_alloc() Matthew Brost
2026-12-01 23:59   ` sashiko-bot
2026-12-01 23:48 ` [PATCH v4 05/33] drm/ttm: Support defragmentation moves Matthew Brost
2026-12-01 23:48 ` [PATCH v4 06/33] drm/ttm: Add fault injection for beneficial-order allocation failures Matthew Brost
2026-12-01 23:48 ` [PATCH v4 07/33] drm/ttm: Harvest beneficial-order pages on defragmentation moves Matthew Brost
2026-12-01 23:48 ` [PATCH v4 08/33] drm/ttm: Bound page (re)allocation per defragmentation move Matthew Brost
2026-12-01 23:48 ` [PATCH v4 09/33] drm/ttm: Preallocate beneficial-order defrag pages outside the lock Matthew Brost
2026-12-01 23:48 ` [PATCH v4 10/33] drm/ttm: Add full out-of-lock preallocation for ttm_pool_alloc() Matthew Brost
2026-12-01 23:48 ` [PATCH v4 11/33] drm/gpusvm: Add a DMA-mapping accounting callback Matthew Brost
2026-12-01 23:48 ` [PATCH v4 12/33] drm/xe: Add debugfs stats for DMA-mapped pages per order Matthew Brost
2026-12-01 23:48 ` [PATCH v4 13/33] drm/xe: Flush L2 asynchronously in xe_bo_trigger_rebind() Matthew Brost
2026-12-01 23:48 ` [PATCH v4 14/33] drm/xe: Track BOs backed at a sub-optimal page order Matthew Brost
2026-12-01 23:48 ` [PATCH v4 15/33] drm/xe: Add xe_migrate_copy_defrag() for on-GPU defrag copies Matthew Brost
2026-12-01 23:48 ` [PATCH v4 16/33] drm/xe: Handle defrag moves in xe_bo_move() Matthew Brost
2026-12-01 23:48 ` [PATCH v4 17/33] drm/xe: Skip self-copies for borrowed pages on defrag moves Matthew Brost
2026-12-01 23:49 ` [PATCH v4 18/33] drm/xe: Add a page defragmentation worker Matthew Brost
2026-12-01 23:49 ` [PATCH v4 19/33] drm/xe: Add defrag GT stats Matthew Brost
2026-12-01 23:49 ` [PATCH v4 20/33] drm/xe: Add Kconfig.profile options for BO defrag configuration Matthew Brost
2026-12-01 23:49 ` [PATCH v4 21/33] drm/xe: Defrag using out-of-lock page preallocation Matthew Brost
2026-12-01 23:49 ` [PATCH v4 22/33] drm/xe: Add defrag profiling tracepoints Matthew Brost
2026-12-01 23:49 ` [PATCH v4 23/33] drm/xe: Preallocate system BO backing outside the dma-resv lock Matthew Brost
2026-12-01 23:49 ` [PATCH v4 24/33] drm/xe: Add tracepoint for xe_gem_create_ioctl Matthew Brost
2026-12-01 23:49 ` [PATCH v4 25/33] drm/xe: Add IOVA-based xe_res_cursor variant Matthew Brost
2026-12-01 23:49 ` [PATCH v4 26/33] drm/xe: Use IOVA-based DMA mapping for eligible tt BOs Matthew Brost
2026-12-01 23:49 ` [PATCH v4 27/33] drm/xe: Add per-device dependency scheduler for IOVA defrag finalize Matthew Brost
2026-12-01 23:49 ` [PATCH v4 28/33] drm/xe: Add packed copy-step IOVA mapping for defrag Matthew Brost
2026-12-01 23:49 ` [PATCH v4 29/33] drm/xe: Blit src-natural to dst-packed for defrag-IOVA copies Matthew Brost
2026-12-01 23:49 ` [PATCH v4 30/33] drm/xe: Finalize defrag-IOVA moves with post-copy job Matthew Brost
2026-12-01 23:49 ` [PATCH v4 31/33] drm/xe: Grow the USM batch pool to 2M and map it via a single PDE Matthew Brost
2026-12-01 23:49 ` Matthew Brost [this message]
2026-12-01 23:49 ` [PATCH v4 33/33] drm/xe: Make BO defrag tunables configurable via sysfs Matthew Brost
2026-12-01 23:58 ` ✗ CI.checkpatch: warning for drm/ttm, drm/xe: Avoid foreground high-order reclaim and defragment sub-optimally backed BOs Patchwork
2026-12-02  0:00 ` ✓ CI.KUnit: success " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201234915.3305331-33-matthew.brost@intel.com \
    --to=matthew.brost@intel.com \
    --cc=arunpravin.paneerselvam@amd.com \
    --cc=carlos.santa@intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=matthew.auld@intel.com \
    --cc=nat@pixelcluster.dev \
    --cc=ryanneph@google.com \
    --cc=thomas.hellstrom@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox