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 591C3606A32D for ; Wed, 2 Dec 2026 23:34:31 +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=IA5Ex7MQ Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 71348112679; Wed, 2 Dec 2026 23:34:25 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) by gabe.freedesktop.org (Postfix) with ESMTPS id 4B27711264E; Wed, 2 Dec 2026 23:34:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796254452; x=1827790452; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kpnQHhHbuxMY/ureeqY+W1R71rzXAurcJgT3Ee+mHEY=; b=IA5Ex7MQ25bRaNhufqoUgEyeXnz2vAMIGBMeqrHyJdb/9+MjdTWjkIZG 2OYOGbFxCCJ4oQ7WmbvrlOoKtnnw+ap+dKbEE6QEmSvJ5xOAfv5B4Uh4Y nBbsYw3aM4fmOwrotfeqQqYy8yAtHCivD57hTZAy/iKYGPlKBy1sX5I5t Y1FvR+noFTcnMbvkyF6LVwvwbTgUu+qADtsSiO3NirVflO1Ps2U1bznjN W155/w1LUvfK783RRzXgdSzBPEUArh+f+ZNe9HS5ZnPTGNCdarQ4RVP1N 7rVNbIHUcg7qfSkw5zw+3LxmEUZa6zAs5Mol5vRSm0GWVMP16zUPJGOzK Q==; X-CSE-ConnectionGUID: N8l7woRjQXeZ+6Uhn5uz0w== X-CSE-MsgGUID: mxlqskJtTjmG4QuFMtccPA== X-IronPort-AV: E=McAfee;i="6800,10657,11984"; a="5203337" X-IronPort-AV: E=Sophos;i="6.27,247,1787036400"; d="scan'208";a="5203337" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2026 15:33:32 -0800 X-CSE-ConnectionGUID: Hp4Vb8AHR3yimZpH98yMQw== X-CSE-MsgGUID: AKfiarSOQXSuHmznEW/tQQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,247,1787036400"; d="scan'208";a="13484124" Received: from gsse-cloud1.jf.intel.com ([10.54.39.91]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2026 15:33:30 -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 v5 35/36] drm/xe: Add bo_defrag debugfs entry Date: Wed, 2 Dec 2026 15:33:22 -0800 Message-Id: <20261202233323.3743190-36-matthew.brost@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261202233323.3743190-1-matthew.brost@intel.com> References: <20261202233323.3743190-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: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "dri-devel" 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 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 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); + + 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 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