Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B4FEE606A329 for ; Tue, 1 Dec 2026 23:50:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=H4M2KQgf Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 697EC10FE21; Tue, 1 Dec 2026 23:50:10 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.12]) by gabe.freedesktop.org (Postfix) with ESMTPS id A6A2410FE22; Tue, 1 Dec 2026 23:50:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796169004; x=1827705004; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=P+tTnXo4nQFr91a2HwTMK31XSZ3Usd1kA/56czF5veQ=; b=H4M2KQgfpKgXA2OPidSVONAziSncN2cv4QCUuq0GCwaP3efUw/zSeQ0j q/NTlfcWQsmBKKFW93kggJ/fPuIBFzYXcA0dc6oXlGu7aDgN6rJ8EyMe0 egB8tPPx/dJ10/HKMlRd25v7CwUt6e7acp0W978FeTrowA6E0JT7ykTh2 s0kPwy09jX7hfUsyTZJS336YtmQDryUY9BYhHasCFSp6YnjZv759fI2eM +jcDA7nqWnwUReGh00+g+xsiiUfZPKpvWgRbOp7ncBcuu/cUihcFgclqa 4RA1gCvrQE0u43IanU309IjkMiIKFg9uqiJZDqb4Uo0onb5VJv8/1sgn1 g==; X-CSE-ConnectionGUID: mrC+LUMzSf+LJFVun5zDGQ== X-CSE-MsgGUID: ciivNBAxT7Cdi+67AFKZlg== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="4977599" X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="4977599" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by fmvoesa106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 15:49:35 -0800 X-CSE-ConnectionGUID: fE6Nz4kcSzOWVt0gMxO5/Q== X-CSE-MsgGUID: VBsT/40nSq6VFPAyReDBMw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="13920010" Received: from gsse-cloud1.jf.intel.com ([10.54.39.91]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 15:49:35 -0800 From: Matthew Brost To: intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org Cc: Carlos Santa , Ryan Neph , Matthew Auld , =?UTF-8?q?Thomas=20Hellstr=C3=B6m?= , Natalie Vock , Arun Pravin Subject: [PATCH v4 32/33] drm/xe: Add bo_defrag debugfs entry Date: Tue, 1 Dec 2026 15:49:14 -0800 Message-Id: <20261201234915.3305331-33-matthew.brost@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261201234915.3305331-1-matthew.brost@intel.com> References: <20261201234915.3305331-1-matthew.brost@intel.com> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: intel-xe@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel Xe graphics driver List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Intel-xe" X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 Cc: Ryan Neph Cc: Matthew Auld Cc: Thomas Hellström Cc: Natalie Vock Cc: Arun Pravin Assisted-by: LLM Signed-off-by: Matthew Brost --- 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