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 78858606A32A for ; Tue, 1 Dec 2026 23:50:17 +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=XzJq+RDB Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id AAA9910FE31; Tue, 1 Dec 2026 23:50:11 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.12]) by gabe.freedesktop.org (Postfix) with ESMTPS id D2D3010EB1D; Tue, 1 Dec 2026 23:50:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796169005; x=1827705005; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VcEFkJogcGiQj4xNM1CHN/ExOJ+0br7xJXfk/yJc/tk=; b=XzJq+RDBMjb/Pr4l0GjTpSYZoMLJ5w8mJ/F4p7Z8SCcjv9YCPzKfrJwe J+n4GZmBdL4MS+YO7MNaureNYoHheNDYMjQYDf8wPx1f4ArGuMLtdWpDx V6UamVx+lFAcFg7VKkKfAzbxjWKK9Ezt+X8QqZag/uXuOcUDhKSqPxHyx Nrd2qgqyRnqu4J+u2d5iaepuwakRshXGB6kS0WPfY7Wz1rUbjwEb+MmxT O8r4N/J6CE1jPATEUBD//KIMyAjiaix0fihEEqqXP4pFZqfbaPvZh6yWM gse46PdeA/N6QrbsBY8rt1nbSABsFUzoDOwlfEl9ipG+G0r7xaHa8pP1T Q==; X-CSE-ConnectionGUID: RYGzECSaRl+ocHQiind1tw== X-CSE-MsgGUID: zrzgcLJ9Sfa1AdsjM01BUQ== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="4977602" X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="4977602" 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: gDk0PwF+TeyRWHW2cCZg1w== X-CSE-MsgGUID: RUlOeSxeTBKNgd8skKbREQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="13920013" 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 , Copilot <223556219+Copilot@users.noreply.github.com> Subject: [PATCH v4 33/33] drm/xe: Make BO defrag tunables configurable via sysfs Date: Tue, 1 Dec 2026 15:49:15 -0800 Message-Id: <20261201234915.3305331-34-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=-23.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, MENTIONS_GIT_HOSTING,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 (disabling defrag) remains a build time only decision, so the attributes are only exposed when defrag is supported. Report the runtime values in the bo_defrag debugfs entry. 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 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- drivers/gpu/drm/xe/Kconfig.profile | 9 +- drivers/gpu/drm/xe/xe_bo.c | 136 +++++++++++++++++++++++---- drivers/gpu/drm/xe/xe_bo.h | 5 + drivers/gpu/drm/xe/xe_device_sysfs.c | 122 ++++++++++++++++++++++++ drivers/gpu/drm/xe/xe_device_types.h | 24 ++++- 5 files changed, 273 insertions(+), 23 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 9c218b03210a..165453328931 100644 --- a/drivers/gpu/drm/xe/xe_bo.c +++ b/drivers/gpu/drm/xe/xe_bo.c @@ -93,6 +93,12 @@ #define XE_BO_DEFRAG_INTERVAL_MAX_MS 15000 /* 15 seconds */ #endif +/* Bounds for the values tunable at runtime via sysfs. */ +#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); @@ -1689,8 +1695,6 @@ 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); - static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt, struct ttm_operation_ctx *ctx) { @@ -2239,6 +2243,9 @@ 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; INIT_DELAYED_WORK(&xe->mem.defrag.worker, xe_bo_defrag_worker); @@ -2299,7 +2306,8 @@ void xe_bo_defrag_resume(struct xe_device *xe) scoped_guard(spinlock, &xe->mem.defrag.lock) { if (!list_empty(&xe->mem.defrag.list)) { - xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; + xe->mem.defrag.interval_ms = + xe->mem.defrag.interval_default_ms; kick = true; } } @@ -2308,23 +2316,105 @@ void xe_bo_defrag_resume(struct xe_device *xe) xe_bo_defrag_schedule(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 with * CCS save/restore enabled this interacts badly with CCS metadata migration * (which is currently broken for VF migration), so disable it there: gate BOs * out of the defrag list and skip the up-front reclaim backoff that assumes a * later in-place upgrade. + * + * 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_SIZE_LIMIT >= SZ_2M && !IS_VF_CCS_READY(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; @@ -2355,7 +2445,8 @@ static void xe_bo_defrag_add(struct xe_bo *bo) /* Kick the worker when the list transitions to non-empty. */ kick = list_empty(&xe->mem.defrag.list); if (kick) - xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; + xe->mem.defrag.interval_ms = + xe->mem.defrag.interval_default_ms; list_add_tail(&bo->defrag_link, &xe->mem.defrag.list); xe_gt_stats_incr(xe_root_mmio_gt(xe), XE_GT_STATS_ID_DEFRAG_ADDED_COUNT, 1); @@ -2433,7 +2524,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; @@ -2444,15 +2535,18 @@ 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, "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); @@ -2662,6 +2756,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; @@ -2683,7 +2778,7 @@ static void xe_bo_defrag_worker(struct work_struct *w) trace_xe_bo_defrag_worker(xe, 0, true); /* - * 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 @@ -2694,7 +2789,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; @@ -2705,7 +2800,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, @@ -2728,7 +2824,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; @@ -2742,10 +2838,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); @@ -2760,7 +2857,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. * @@ -2770,13 +2867,14 @@ 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) { struct xe_bo *bo; scoped_guard(spinlock, &xe->mem.defrag.lock) { bo = list_first_entry_or_null(&xe->mem.defrag.list, struct xe_bo, defrag_link); - xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; + xe->mem.defrag.interval_ms = + xe->mem.defrag.interval_default_ms; } if (bo) diff --git a/drivers/gpu/drm/xe/xe_bo.h b/drivers/gpu/drm/xe/xe_bo.h index d0e72d244eae..ef6d21d91724 100644 --- a/drivers/gpu/drm/xe/xe_bo.h +++ b/drivers/gpu/drm/xe/xe_bo.h @@ -239,6 +239,11 @@ 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); +bool xe_bo_defrag_supported(struct xe_device *xe); +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 9b734aeefd33..c94592576408 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" @@ -56,6 +57,24 @@ * 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_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 @@ -428,6 +447,103 @@ static const struct attribute_group userptr_attr_group = { .attrs = userptr_attrs, }; +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_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; @@ -441,6 +557,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 07aa84888d4a..252dc42e063f 100644 --- a/drivers/gpu/drm/xe/xe_device_types.h +++ b/drivers/gpu/drm/xe/xe_device_types.h @@ -345,10 +345,30 @@ 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.iova_sched: Dependency scheduler used to * run the post-copy IOVA finalize job for defrag moves -- 2.34.1