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 <[email protected]>
Cc: Ryan Neph <[email protected]>
Cc: Matthew Auld <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Natalie Vock <[email protected]>
Cc: Arun Pravin <[email protected]>
Signed-off-by: Matthew Brost <[email protected]>
---
drivers/gpu/drm/xe/Kconfig.profile | 9 +-
drivers/gpu/drm/xe/xe_bo.c | 131 +++++++++++++++++++++++----
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, 269 insertions(+), 22 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 85552da80bf5..321e74e03e3c 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);
@@ -1698,8 +1704,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)
{
@@ -2240,6 +2244,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);
@@ -2310,7 +2317,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);
}
@@ -2364,11 +2371,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
@@ -2377,12 +2387,91 @@ 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_SIZE_LIMIT >= SZ_2M && !IS_VF_CCS_NEEDED(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 +2583,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 +2594,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);
@@ -2739,6 +2831,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;
@@ -2761,7 +2854,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
@@ -2772,7 +2865,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;
@@ -2783,7 +2876,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,
@@ -2806,7 +2900,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;
@@ -2820,10 +2914,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);
@@ -2838,7 +2933,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.
*
@@ -2848,10 +2943,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) {
diff --git a/drivers/gpu/drm/xe/xe_bo.h b/drivers/gpu/drm/xe/xe_bo.h
index 28325341b4d1..281a863c1702 100644
--- a/drivers/gpu/drm/xe/xe_bo.h
+++ b/drivers/gpu/drm/xe/xe_bo.h
@@ -241,6 +241,11 @@ 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);
+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 <linux/sysfs.h>
#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 423a868d4605..051e29a6f00c 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.suspended: The device is suspending,
* suspended or undergoing FLR; @mem.defrag.worker must
--
2.34.1