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 742F3606A335 for ; Thu, 3 Dec 2026 20:24:57 +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=LLoQuK7Z Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4EB70112BE6; Thu, 3 Dec 2026 20:24:51 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.20]) by gabe.freedesktop.org (Postfix) with ESMTPS id F3E2B112BBA; Thu, 3 Dec 2026 20:24:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796329476; x=1827865476; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3L2WxNotMK4S/tmQTC69ru34tur8jmgsL2GLTLPjMg8=; b=LLoQuK7ZT/VS2CwT0DZjpoGsurAw+16EmMZNfKQ2ZxeD4qFcXpjWN99F NdGtJqsCOaznuj9QxZpvHcbfIUYWN4VZcA9qQXLUJNPRytfNG6AAfTout aTJW/wxSHsBCQpkJPgg4Zdb9iwXw7Yrd/kK7LJVxYpEtgavZgNN7PLrt2 XEFAIzFy9969rIhUQuBbReYPqJSd8uaP3P+B6Yqd8X4uwboUzWJpdET6n AzdM9UpIsd3u3fw0MV3Dm/bZYwPcREuYiemGdOackHBdmujK3yVI/BcJQ 2EHCkU97xQi6A0etS11VXqNBudzuCw0Mbl3pHM7os3f79IvzR5UlkxRhE w==; X-CSE-ConnectionGUID: n1czxN2fST+jSRfOBy9ZmA== X-CSE-MsgGUID: 8M1iIp1hTli4puQlOoRMZA== X-IronPort-AV: E=McAfee;i="6800,10657,11985"; a="5351260" X-IronPort-AV: E=Sophos;i="6.29,77,1794902400"; d="scan'208";a="5351260" Received: from fmviesa013.fm.intel.com ([10.60.135.153]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Dec 2026 12:24:09 -0800 X-CSE-ConnectionGUID: wRKw8frXTbecNgYNoa518w== X-CSE-MsgGUID: R/RRt1gwSnWk/CUURHg1kw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.29,77,1794902400"; d="scan'208";a="15862545" Received: from gsse-cloud1.jf.intel.com ([10.54.39.91]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Dec 2026 12:24:08 -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 , Niranjana Vishwanathapura Subject: [PATCH v6 34/35] drm/xe: Add bo_defrag debugfs entry Date: Thu, 3 Dec 2026 12:23:51 -0800 Message-Id: <20261203202352.4153159-35-matthew.brost@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261203202352.4153159-1-matthew.brost@intel.com> References: <20261203202352.4153159-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 Reviewed-by: Niranjana Vishwanathapura Reviewed-by: Matthew Auld --- 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 d5d27727c910..a6b687042107 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 f82ae37a0a75..51ef2ca2166d 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