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 E7866606A32F for ; Thu, 3 Dec 2026 20:24:58 +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=MeBDLi46 Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 00B92112BEF; Thu, 3 Dec 2026 20:24:53 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.20]) by gabe.freedesktop.org (Postfix) with ESMTPS id DCB1C112BCF; Thu, 3 Dec 2026 20:24:35 +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=JEEmIclmeUBGrdWDULqWQwqsvJYnldbmWjixwqfou0w=; b=MeBDLi46zaZjSnWrlW/HisLW/G/ReTti9NQ9EnMVCsaxKm9MhY3I+Wqm By1EeAeD2RA4LPSXWCNSsVTzDuFO1XvSa+zfbOxTdcuEiywdoZpr6W2WU YEdtSqgD73y4sEv2VxTLt9yaio4Xwin5I5SEOnjECTYeIrQL5acWSqBfy I/Lunj7gmbsrqr2cD4erFYhC/vkysU+Y6Z/NmA4zFFbw2bwkMFnkIG3Pe PFjvW21+7qVpZyvo7Jx1rW1ZoBJRMb1a92jGAsT1IKQl6+IFjeOuhvgUb f2qYQdKePkg1LVo+1B6HkCbvSpvntYnWZkv3q6Qc6tvFdyxrZPkvSCW/B Q==; X-CSE-ConnectionGUID: 7zDEE3JdTHiwWr5ZJ9OHdA== X-CSE-MsgGUID: UXbe9R0mQHODwwdxBImDOQ== X-IronPort-AV: E=McAfee;i="6800,10657,11985"; a="5351264" X-IronPort-AV: E=Sophos;i="6.29,77,1794902400"; d="scan'208";a="5351264" 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: IMg4nNuBRn2nM4+C2Bkswg== X-CSE-MsgGUID: ZsR8V21gTiaJUhthCRM/xg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.29,77,1794902400"; d="scan'208";a="15862549" 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:09 -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 35/35] drm/xe: Make BO defrag tunables configurable via sysfs Date: Thu, 3 Dec 2026 12:23:52 -0800 Message-Id: <20261203202352.4153159-36-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 The BO defrag per-run size limit, default worker interval and maximum backed off worker interval are currently fixed at build time via Kconfig. Tuning them requires a rebuild, which makes experimenting with defrag behaviour on a running system painful. Add defrag_size_limit, defrag_interval_ms and defrag_interval_max_ms device sysfs attributes, defaulting to the Kconfig values. Writes are validated against the Kconfig ranges, the size limit must be 2M aligned, and the default interval may not exceed the maximum interval. Lowering the size limit below 2M (compiling out defrag) remains a build time only decision, so the attributes are only exposed when defrag is supported. Also add a defrag_enabled attribute to turn defrag off and on at runtime for quick A/B comparisons. Disabling stops the worker and makes new allocations take beneficial-order pages up front instead of backing off reclaim, as if defrag were not supported. BOs are still tracked while disabled, so re-enabling kicks the worker to upgrade any that remain sub-optimal. Report the runtime values in the bo_defrag debugfs entry. v6: - Add defrag_enabled to toggle defrag at runtime for A/B comparisons 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/Kconfig.profile | 9 +- drivers/gpu/drm/xe/xe_bo.c | 188 +++++++++++++++++++++++---- drivers/gpu/drm/xe/xe_bo.h | 6 + drivers/gpu/drm/xe/xe_device_sysfs.c | 157 ++++++++++++++++++++++ drivers/gpu/drm/xe/xe_device_types.h | 32 ++++- 5 files changed, 364 insertions(+), 28 deletions(-) diff --git a/drivers/gpu/drm/xe/Kconfig.profile b/drivers/gpu/drm/xe/Kconfig.profile index 32bc358e6968..ea46e34e3957 100644 --- a/drivers/gpu/drm/xe/Kconfig.profile +++ b/drivers/gpu/drm/xe/Kconfig.profile @@ -115,7 +115,9 @@ config DRM_XE_BO_DEFRAG_SIZE_LIMIT number of BOs become eligible at once, processing them all in one worker run would hold things up for a long, unbounded stretch. Instead, cap the work done per run and requeue, spreading the defrag - effort out over time. + effort out over time. This is the default; when defrag is enabled it + can be changed at runtime (2M to 1G) via the defrag_size_limit + device sysfs attribute. config DRM_XE_BO_DEFRAG_INTERVAL_MS int "Default delay before defrag worker run (ms)" @@ -123,6 +125,8 @@ config DRM_XE_BO_DEFRAG_INTERVAL_MS range 1 1000 help Default delay before (re)running the defrag worker, in milliseconds. + Can be changed at runtime via the defrag_interval_ms device sysfs + attribute. config DRM_XE_BO_DEFRAG_INTERVAL_MAX_MS int "Upper bound for defrag worker interval (ms)" @@ -131,4 +135,5 @@ config DRM_XE_BO_DEFRAG_INTERVAL_MAX_MS help Upper bound for the (exponentially backed off) defrag worker interval, in milliseconds, so repeated failures don't push the retry arbitrarily - far out. 15000 ms = 15 seconds. + far out. 15000 ms = 15 seconds. Can be changed at runtime via the + defrag_interval_max_ms device sysfs attribute. diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c index a6b687042107..70ab6f906734 100644 --- a/drivers/gpu/drm/xe/xe_bo.c +++ b/drivers/gpu/drm/xe/xe_bo.c @@ -96,6 +96,15 @@ #define XE_BO_DEFRAG_INTERVAL_MAX_MS 15000 /* 15 seconds */ #endif +/* + * Bounds for the values tunable at runtime via sysfs. These match the bounds + * for the Kconfig options in Kconfig.profile. + */ +#define XE_BO_DEFRAG_SIZE_LIMIT_MAX SZ_1G +#define XE_BO_DEFRAG_INTERVAL_MS_MAX 1000 +#define XE_BO_DEFRAG_INTERVAL_MAX_MS_MIN 100 +#define XE_BO_DEFRAG_INTERVAL_MAX_MS_MAX 1000000 + static void xe_bo_defrag_worker(struct work_struct *w); static void xe_place_from_ttm_type(u32 mem_type, struct ttm_place *place); @@ -1691,7 +1700,14 @@ static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo, return tt; } -static bool xe_bo_defrag_supported(struct xe_device *xe); +/* + * Whether allocations should back off reclaim at beneficial order and leave the + * upgrade to the defrag worker: only if defrag is supported and enabled. + */ +static bool xe_bo_defrag_active(struct xe_device *xe) +{ + return xe_bo_defrag_supported(xe) && READ_ONCE(xe->mem.defrag.enabled); +} static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt, struct ttm_operation_ctx *ctx) @@ -1710,7 +1726,7 @@ static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt, /* Only defrag will enter reclaim at higher orders */ ctx->beneficial_reclaim_backoff = !ctx->defrag && - xe_bo_defrag_supported(xe); + xe_bo_defrag_active(xe); if (ttm_tt_is_backed_up(tt) && !xe_tt->purgeable) { err = ttm_tt_restore(ttm_dev, tt, ctx); @@ -2233,7 +2249,11 @@ int xe_bo_defrag_init_early(struct xe_device *xe) spin_lock_init(&xe->mem.defrag.lock); INIT_LIST_HEAD(&xe->mem.defrag.list); + xe->mem.defrag.size_limit = XE_BO_DEFRAG_SIZE_LIMIT; + xe->mem.defrag.interval_default_ms = XE_BO_DEFRAG_INTERVAL_MS; + xe->mem.defrag.interval_max_ms = XE_BO_DEFRAG_INTERVAL_MAX_MS; xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; + xe->mem.defrag.enabled = true; INIT_DELAYED_WORK(&xe->mem.defrag.worker, xe_bo_defrag_worker); return devm_add_action_or_reset(xe->drm.dev, xe_bo_defrag_fini_early, xe); @@ -2283,7 +2303,7 @@ static void xe_bo_defrag_schedule_locked(struct xe_device *xe, bool kick) lockdep_assert_held(&xe->mem.defrag.lock); xe_assert(xe, XE_BO_DEFRAG_ENABLED); - if (xe->mem.defrag.suspended) + if (xe->mem.defrag.suspended || !xe->mem.defrag.enabled) return; delay = msecs_to_jiffies(xe->mem.defrag.interval_ms); @@ -2302,7 +2322,7 @@ static void xe_bo_defrag_kick_locked(struct xe_device *xe) if (list_empty(&xe->mem.defrag.list)) return; - xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; + xe->mem.defrag.interval_ms = xe->mem.defrag.interval_default_ms; xe_bo_defrag_schedule_locked(xe, true); } @@ -2356,11 +2376,14 @@ void xe_bo_defrag_kick(struct xe_device *xe) xe_bo_defrag_kick_locked(xe); } -/* +/** * xe_bo_defrag_supported() - Whether background defragmentation is usable * @xe: xe device instance * - * Defragmentation is not supported if XE_BO_DEFRAG_SIZE_LIMIT is less than 2M. + * Defragmentation is not supported if the default size limit + * (CONFIG_DRM_XE_BO_DEFRAG_SIZE_LIMIT) is less than 2M. The runtime size limit + * can't be lowered below 2M, so it can't change whether defragmentation is + * supported. * * Defragmentation forces TTM moves that reallocate a BO's backing. On a VF * platform that requires CCS save/restore this interacts badly with CCS @@ -2369,12 +2392,121 @@ void xe_bo_defrag_kick(struct xe_device *xe) * backoff that assumes a later in-place upgrade. This is a static platform * check so the answer can't change during probe, after BOs have been * allocated or tracked. + * + * Return: true if background defragmentation is supported, false otherwise. */ -static bool xe_bo_defrag_supported(struct xe_device *xe) +bool xe_bo_defrag_supported(struct xe_device *xe) { return XE_BO_DEFRAG_ENABLED && !IS_VF_CCS_NEEDED(xe); } +/** + * xe_bo_defrag_set_enabled() - Enable or disable defragmentation at runtime + * @xe: xe device instance + * @enable: Whether defragmentation should be enabled + * + * Intended for quick A/B comparisons. Disabling stops the defrag worker (waiting + * for a running instance to finish) and makes new allocations take + * beneficial-order pages up front instead of backing off reclaim, as if + * defragmentation were not supported. BOs are still tracked while disabled, so + * re-enabling kicks the worker to upgrade any that remain sub-optimal. + */ +void xe_bo_defrag_set_enabled(struct xe_device *xe, bool enable) +{ + scoped_guard(spinlock, &xe->mem.defrag.lock) { + WRITE_ONCE(xe->mem.defrag.enabled, enable); + if (enable) + xe_bo_defrag_kick_locked(xe); + } + + if (enable) + return; + + cancel_delayed_work_sync(&xe->mem.defrag.worker); + + /* A racing enable may have had its kick cancelled above. */ + guard(spinlock)(&xe->mem.defrag.lock); + if (xe->mem.defrag.enabled) + xe_bo_defrag_kick_locked(xe); +} + +/** + * xe_bo_defrag_set_size_limit() - Set the defrag worker per-run byte budget + * @xe: xe device instance + * @size_limit: New maximum number of bytes to defrag per worker run + * + * Takes effect from the next worker run. + * + * Return: 0 on success, -EINVAL if @size_limit is out of range or not 2M + * aligned. + */ +int xe_bo_defrag_set_size_limit(struct xe_device *xe, u64 size_limit) +{ + if (size_limit < SZ_2M || size_limit > XE_BO_DEFRAG_SIZE_LIMIT_MAX || + !IS_ALIGNED(size_limit, SZ_2M)) + return -EINVAL; + + WRITE_ONCE(xe->mem.defrag.size_limit, size_limit); + + return 0; +} + +/** + * xe_bo_defrag_set_interval_ms() - Set the default defrag worker interval + * @xe: xe device instance + * @interval_ms: New default delay before (re)running the defrag worker + * + * The new value is used the next time the worker interval is reset, i.e. on + * forward progress or when the defrag list transitions to non-empty. + * + * Return: 0 on success, -EINVAL if @interval_ms is out of range or exceeds + * the maximum interval. + */ +int xe_bo_defrag_set_interval_ms(struct xe_device *xe, unsigned int interval_ms) +{ + if (!interval_ms || interval_ms > XE_BO_DEFRAG_INTERVAL_MS_MAX) + return -EINVAL; + + guard(spinlock)(&xe->mem.defrag.lock); + + if (interval_ms > xe->mem.defrag.interval_max_ms) + return -EINVAL; + + WRITE_ONCE(xe->mem.defrag.interval_default_ms, interval_ms); + + return 0; +} + +/** + * xe_bo_defrag_set_interval_max_ms() - Set the maximum defrag worker interval + * @xe: xe device instance + * @interval_max_ms: New upper bound for the backed off worker interval + * + * The current interval is clamped to the new maximum, taking effect the next + * time the worker is scheduled. + * + * Return: 0 on success, -EINVAL if @interval_max_ms is out of range or is + * less than the default interval. + */ +int xe_bo_defrag_set_interval_max_ms(struct xe_device *xe, + unsigned int interval_max_ms) +{ + if (interval_max_ms < XE_BO_DEFRAG_INTERVAL_MAX_MS_MIN || + interval_max_ms > XE_BO_DEFRAG_INTERVAL_MAX_MS_MAX) + return -EINVAL; + + guard(spinlock)(&xe->mem.defrag.lock); + + if (interval_max_ms < xe->mem.defrag.interval_default_ms) + return -EINVAL; + + WRITE_ONCE(xe->mem.defrag.interval_max_ms, interval_max_ms); + xe->mem.defrag.interval_ms = min(xe->mem.defrag.interval_ms, + interval_max_ms); + + return 0; +} + static bool xe_bo_needs_defrag(struct xe_bo *bo) { struct ttm_buffer_object *ttm_bo = &bo->ttm; @@ -2494,7 +2626,7 @@ 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; + unsigned int interval_ms, interval_default_ms, interval_max_ms; struct xe_bo **bos, *bo; int err = 0; @@ -2505,15 +2637,20 @@ int xe_bo_defrag_print(struct xe_device *xe, struct drm_printer *p) scoped_guard(spinlock, &xe->mem.defrag.lock) { interval_ms = xe->mem.defrag.interval_ms; + interval_default_ms = xe->mem.defrag.interval_default_ms; + interval_max_ms = xe->mem.defrag.interval_max_ms; list_for_each_entry(bo, &xe->mem.defrag.list, defrag_link) count++; } + drm_printf(p, "enabled: %s\n", + str_yes_no(READ_ONCE(xe->mem.defrag.enabled))); 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, "size_limit: %llu\n", + READ_ONCE(xe->mem.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); + interval_default_ms, interval_max_ms); if (delayed_work_pending(dwork)) { long delay = (long)(READ_ONCE(dwork->timer.expires) - jiffies); @@ -2739,6 +2876,7 @@ static void xe_bo_defrag_worker(struct work_struct *w) struct delayed_work *dwork = to_delayed_work(w); struct xe_device *xe = container_of(dwork, struct xe_device, mem.defrag.worker); + u64 size_limit = READ_ONCE(xe->mem.defrag.size_limit); u64 defrag_bytes = 0; bool requeue = false; int idx; @@ -2772,14 +2910,14 @@ static void xe_bo_defrag_worker(struct work_struct *w) xe->mem.defrag.interval_ms = min(xe->mem.defrag.interval_ms * 2, - (unsigned int)XE_BO_DEFRAG_INTERVAL_MAX_MS); + xe->mem.defrag.interval_max_ms); xe_bo_defrag_schedule_locked(xe, false); } goto out; } /* - * Process at most XE_BO_DEFRAG_SIZE_LIMIT bytes of newly (re)allocated + * Process at most @mem.defrag.size_limit bytes of newly (re)allocated * backing per run rather than draining the whole list in one go. Only * the pages a move actually reallocates are charged - pages harvested * (borrowed) from the old backing are free and do not count - so the @@ -2790,7 +2928,7 @@ static void xe_bo_defrag_worker(struct work_struct *w) * active work make progress; any remaining work is handled by a * follow-up run scheduled below. */ - while (defrag_bytes < XE_BO_DEFRAG_SIZE_LIMIT) { + while (defrag_bytes < size_limit) { struct xe_bo *first, *bo; u64 consumed = 0; bool needs_more = false; @@ -2801,7 +2939,8 @@ static void xe_bo_defrag_worker(struct work_struct *w) /* Progress, reset interval */ if (defrag_bytes) - xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; + xe->mem.defrag.interval_ms = + xe->mem.defrag.interval_default_ms; do { first = list_first_entry_or_null(&xe->mem.defrag.list, @@ -2824,7 +2963,7 @@ static void xe_bo_defrag_worker(struct work_struct *w) * beneficial harvests, so each run only spends budget on new * forward progress. */ - ret = xe_bo_defrag_one(xe, bo, XE_BO_DEFRAG_SIZE_LIMIT - defrag_bytes, + ret = xe_bo_defrag_one(xe, bo, size_limit - defrag_bytes, &consumed, &needs_more); defrag_bytes += consumed; @@ -2838,10 +2977,11 @@ static void xe_bo_defrag_worker(struct work_struct *w) */ xe->mem.defrag.interval_ms = min(xe->mem.defrag.interval_ms * 2, - (unsigned int)XE_BO_DEFRAG_INTERVAL_MAX_MS); + xe->mem.defrag.interval_max_ms); else /* Progress, reset interval */ - xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; + xe->mem.defrag.interval_ms = + xe->mem.defrag.interval_default_ms; } xe_bo_put(bo); @@ -2856,7 +2996,7 @@ static void xe_bo_defrag_worker(struct work_struct *w) * Decide whether to reschedule: * * - The loop hit its per-run limit (defrag_bytes >= - * XE_BO_DEFRAG_SIZE_LIMIT): we stopped early to spread the work out, + * size_limit): we stopped early to spread the work out, * so requeue at the default interval (reset here since this run made * progress) whenever the list still has entries. * @@ -2866,10 +3006,10 @@ static void xe_bo_defrag_worker(struct work_struct *w) * - The loop drained the list (broke on the empty list): nothing left to * do, so stop scheduling. A fresh enqueue will kick the worker again. */ - if (defrag_bytes >= XE_BO_DEFRAG_SIZE_LIMIT) { + if (defrag_bytes >= size_limit) { guard(spinlock)(&xe->mem.defrag.lock); - xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; + xe->mem.defrag.interval_ms = xe->mem.defrag.interval_default_ms; if (!list_empty(&xe->mem.defrag.list)) xe_bo_defrag_schedule_locked(xe, false); } else if (requeue) { @@ -5675,12 +5815,12 @@ static bool xe_gem_create_prealloc(struct xe_device *xe, * Back off aggressive reclaim/compaction for higher-order chunks here: * Xe runs a background defragmenter that upgrades suboptimal pages to * beneficial order in place later, so there is no need to stall this - * user context reclaiming for contiguity. When defrag is disabled there - * is no such later upgrade, so do not back off reclaim here: take - * beneficial-order pages up front. + * user context reclaiming for contiguity. When defrag is unsupported or + * disabled there is no such later upgrade, so do not back off reclaim + * here: take beneficial-order pages up front. */ ttm_pool_prealloc_fill_full(&xe->ttm.pool, caching, prealloc, - num_pages, xe_bo_defrag_supported(xe)); + num_pages, xe_bo_defrag_active(xe)); return true; } diff --git a/drivers/gpu/drm/xe/xe_bo.h b/drivers/gpu/drm/xe/xe_bo.h index 28325341b4d1..675527f30919 100644 --- a/drivers/gpu/drm/xe/xe_bo.h +++ b/drivers/gpu/drm/xe/xe_bo.h @@ -241,6 +241,12 @@ 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); +bool xe_bo_defrag_supported(struct xe_device *xe); +void xe_bo_defrag_set_enabled(struct xe_device *xe, bool enable); +int xe_bo_defrag_set_size_limit(struct xe_device *xe, u64 size_limit); +int xe_bo_defrag_set_interval_ms(struct xe_device *xe, unsigned int interval_ms); +int xe_bo_defrag_set_interval_max_ms(struct xe_device *xe, + unsigned int interval_max_ms); 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_device_sysfs.c b/drivers/gpu/drm/xe/xe_device_sysfs.c index 29bdb6f08ae7..2506b03e6df0 100644 --- a/drivers/gpu/drm/xe/xe_device_sysfs.c +++ b/drivers/gpu/drm/xe/xe_device_sysfs.c @@ -11,6 +11,7 @@ #include #include "regs/xe_regs.h" +#include "xe_bo.h" #include "xe_device.h" #include "xe_device_sysfs.h" #include "xe_mmio.h" @@ -61,6 +62,30 @@ * back it up. BOs evicted to system memory are never protected. Defaults to * CONFIG_DRM_XE_SHRINKER_MIN_AGE_MS, 0 disables. This applies at every * reclaim priority, so a non-zero value can lead to OOM. + * + * defrag_enabled - Report/change whether BO defragmentation is enabled at + * runtime, for quick A/B comparisons. Writing 0 stops the defragmentation + * worker and makes new allocations take beneficial-order pages up front instead + * of backing off reclaim; writing 1 re-enables it. Defaults to 1. Exposed only + * if BO defragmentation is supported. + * + * defrag_size_limit - Report/change the maximum number of bytes the BO + * defragmentation worker reallocates per run. Valid values are 2M aligned, + * 2M to 1G. + * Defaults to CONFIG_DRM_XE_BO_DEFRAG_SIZE_LIMIT. Exposed only if BO + * defragmentation is supported. + * + * defrag_interval_ms - Report/change the default delay, in milliseconds, + * before (re)running the BO defragmentation worker. Valid values are 1 to + * 1000 and must not exceed defrag_interval_max_ms. Defaults to + * CONFIG_DRM_XE_BO_DEFRAG_INTERVAL_MS. Exposed only if BO defragmentation is + * supported. + * + * defrag_interval_max_ms - Report/change the upper bound, in milliseconds, of + * the exponentially backed off BO defragmentation worker interval. Valid + * values are 100 to 1000000 and must not be less than defrag_interval_ms. + * Defaults to CONFIG_DRM_XE_BO_DEFRAG_INTERVAL_MAX_MS. Exposed only if BO + * defragmentation is supported. */ static ssize_t @@ -485,6 +510,132 @@ static const struct attribute_group userptr_attr_group = { .attrs = userptr_attrs, }; +static ssize_t +defrag_enabled_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev)); + + return sysfs_emit(buf, "%d\n", READ_ONCE(xe->mem.defrag.enabled)); +} + +static ssize_t +defrag_enabled_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev)); + bool enable; + int ret; + + ret = kstrtobool(buf, &enable); + if (ret) + return ret; + + xe_bo_defrag_set_enabled(xe, enable); + + return count; +} + +static DEVICE_ATTR_RW(defrag_enabled); + +static ssize_t +defrag_size_limit_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev)); + + return sysfs_emit(buf, "%llu\n", READ_ONCE(xe->mem.defrag.size_limit)); +} + +static ssize_t +defrag_size_limit_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev)); + u64 size_limit; + int ret; + + ret = kstrtou64(buf, 0, &size_limit); + if (ret) + return ret; + + ret = xe_bo_defrag_set_size_limit(xe, size_limit); + + return ret ?: count; +} + +static DEVICE_ATTR_RW(defrag_size_limit); + +static ssize_t +defrag_interval_ms_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev)); + + return sysfs_emit(buf, "%u\n", + READ_ONCE(xe->mem.defrag.interval_default_ms)); +} + +static ssize_t +defrag_interval_ms_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev)); + unsigned int interval_ms; + int ret; + + ret = kstrtouint(buf, 0, &interval_ms); + if (ret) + return ret; + + ret = xe_bo_defrag_set_interval_ms(xe, interval_ms); + + return ret ?: count; +} + +static DEVICE_ATTR_RW(defrag_interval_ms); + +static ssize_t +defrag_interval_max_ms_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev)); + + return sysfs_emit(buf, "%u\n", + READ_ONCE(xe->mem.defrag.interval_max_ms)); +} + +static ssize_t +defrag_interval_max_ms_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev)); + unsigned int interval_max_ms; + int ret; + + ret = kstrtouint(buf, 0, &interval_max_ms); + if (ret) + return ret; + + ret = xe_bo_defrag_set_interval_max_ms(xe, interval_max_ms); + + return ret ?: count; +} + +static DEVICE_ATTR_RW(defrag_interval_max_ms); + +static struct attribute *defrag_attrs[] = { + &dev_attr_defrag_enabled.attr, + &dev_attr_defrag_size_limit.attr, + &dev_attr_defrag_interval_ms.attr, + &dev_attr_defrag_interval_max_ms.attr, + NULL +}; + +static const struct attribute_group defrag_attr_group = { + .attrs = defrag_attrs, +}; + int xe_device_sysfs_init(struct xe_device *xe) { struct device *dev = xe->drm.dev; @@ -498,6 +649,12 @@ int xe_device_sysfs_init(struct xe_device *xe) if (ret) return ret; + if (xe_bo_defrag_supported(xe)) { + ret = devm_device_add_group(dev, &defrag_attr_group); + if (ret) + return ret; + } + if (xe->d3cold.capable) { ret = devm_device_add_group(dev, &vram_attr_group); if (ret) diff --git a/drivers/gpu/drm/xe/xe_device_types.h b/drivers/gpu/drm/xe/xe_device_types.h index 90ab7288f8fe..e4d928e4349c 100644 --- a/drivers/gpu/drm/xe/xe_device_types.h +++ b/drivers/gpu/drm/xe/xe_device_types.h @@ -347,10 +347,38 @@ struct xe_device { */ struct delayed_work worker; /** - * @mem.defrag.interval_ms: Reschedule interval for - * @mem.defrag.worker, in milliseconds. + * @mem.defrag.interval_ms: Current (possibly backed off) + * reschedule interval for @mem.defrag.worker, in + * milliseconds. Protected by @mem.defrag.lock. */ unsigned int interval_ms; + /** + * @mem.defrag.interval_default_ms: Interval that + * @mem.defrag.interval_ms is reset to on forward progress, + * in milliseconds. Tunable via sysfs. Written under + * @mem.defrag.lock. + */ + unsigned int interval_default_ms; + /** + * @mem.defrag.interval_max_ms: Upper bound for the + * backed off @mem.defrag.interval_ms, in milliseconds. + * Tunable via sysfs. Written under @mem.defrag.lock. + */ + unsigned int interval_max_ms; + /** + * @mem.defrag.size_limit: Maximum number of newly + * (re)allocated bytes the worker defrags per run. + * Tunable via sysfs. + */ + u64 size_limit; + /** + * @mem.defrag.enabled: Defragmentation is enabled at + * runtime. When false the worker is not queued and + * allocations don't back off reclaim at beneficial + * order. Tunable via sysfs. Written under + * @mem.defrag.lock. + */ + bool enabled; /** * @mem.defrag.suspended: The device is suspending, * suspended or undergoing FLR; @mem.defrag.worker must -- 2.34.1