From: "Thomas Hellström" <thomas.hellstrom@linux.intel.com>
To: intel-xe@lists.freedesktop.org
Subject: [CI v3 7/8] drm/xe, drm/ttm: Provide a generic LRU walker helper
Date: Thu, 28 Mar 2024 10:44:53 +0100 [thread overview]
Message-ID: <20240328094454.272050-8-thomas.hellstrom@linux.intel.com> (raw)
In-Reply-To: <20240328094454.272050-1-thomas.hellstrom@linux.intel.com>
Export the needed functions from TTM and provide a generic LRU
walker in xe, in the spirit of drm_gem_lru_scan() but building
on the restartable TTM LRU functionality.
The LRU walker optionally supports locking objects as part of
a drm_exec locking transaction, and can thus be used for both
exhaustive eviction and shrinking. And, in fact, direct
shrinking in the case where we fail to populate system memory
objects and want to retry by shrinking purgeable or evictable
local objects, which a shrinker is not capable of doing.
The LRU walker helper can easily be moved to TTM when / if used
by other drivers.
Signed-off-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
---
drivers/gpu/drm/ttm/ttm_resource.c | 3 +
drivers/gpu/drm/xe/Makefile | 1 +
drivers/gpu/drm/xe/xe_ttm_helpers.c | 106 ++++++++++++++++++++++++++++
drivers/gpu/drm/xe/xe_ttm_helpers.h | 33 +++++++++
4 files changed, 143 insertions(+)
create mode 100644 drivers/gpu/drm/xe/xe_ttm_helpers.c
create mode 100644 drivers/gpu/drm/xe/xe_ttm_helpers.h
diff --git a/drivers/gpu/drm/ttm/ttm_resource.c b/drivers/gpu/drm/ttm/ttm_resource.c
index 89f19fb6e2b3..4cad467983f8 100644
--- a/drivers/gpu/drm/ttm/ttm_resource.c
+++ b/drivers/gpu/drm/ttm/ttm_resource.c
@@ -106,6 +106,7 @@ void ttm_resource_cursor_fini(struct ttm_resource_cursor *cursor)
ttm_resource_cursor_fini_locked(cursor);
spin_unlock(lru_lock);
}
+EXPORT_SYMBOL(ttm_resource_cursor_fini);
/**
* ttm_lru_bulk_move_init - initialize a bulk move structure
@@ -635,6 +636,7 @@ ttm_resource_manager_next(struct ttm_resource_cursor *cursor)
return NULL;
}
+EXPORT_SYMBOL(ttm_resource_manager_next);
/**
* ttm_resource_manager_first() - Start iterating over the resources
@@ -661,6 +663,7 @@ ttm_resource_manager_first(struct ttm_resource_manager *man,
return ttm_resource_manager_next(cursor);
}
+EXPORT_SYMBOL(ttm_resource_manager_first);
static void ttm_kmap_iter_iomap_map_local(struct ttm_kmap_iter *iter,
struct iosys_map *dmap,
diff --git a/drivers/gpu/drm/xe/Makefile b/drivers/gpu/drm/xe/Makefile
index 705c0eaf6e71..ecd7dde9cbbe 100644
--- a/drivers/gpu/drm/xe/Makefile
+++ b/drivers/gpu/drm/xe/Makefile
@@ -133,6 +133,7 @@ xe-y += xe_bb.o \
xe_tile.o \
xe_tile_sysfs.o \
xe_trace.o \
+ xe_ttm_helpers.o \
xe_ttm_sys_mgr.o \
xe_ttm_stolen_mgr.o \
xe_ttm_vram_mgr.o \
diff --git a/drivers/gpu/drm/xe/xe_ttm_helpers.c b/drivers/gpu/drm/xe/xe_ttm_helpers.c
new file mode 100644
index 000000000000..056f836ce9e4
--- /dev/null
+++ b/drivers/gpu/drm/xe/xe_ttm_helpers.c
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <drm/drm_exec.h>
+
+#include "xe_ttm_helpers.h"
+
+#include <drm/ttm/ttm_bo.h>
+#include <drm/ttm/ttm_device.h>
+
+static bool xe_ttm_lru_walk_trylock(struct xe_ttm_lru_walk *walk,
+ struct ttm_buffer_object *bo,
+ bool *needs_unlock)
+{
+ struct ttm_operation_ctx *ctx = walk->ctx;
+
+ *needs_unlock = false;
+
+ if (!walk->exec && dma_resv_trylock(bo->base.resv)) {
+ *needs_unlock = true;
+ return true;
+ }
+
+ if (bo->base.resv == ctx->resv && ctx->allow_res_evict) {
+ dma_resv_assert_held(bo->base.resv);
+ return true;
+ }
+
+ return false;
+}
+
+static void xe_ttm_lru_walk_unlock(struct ttm_buffer_object *bo, bool locked)
+{
+ if (locked)
+ dma_resv_unlock(bo->base.resv);
+}
+
+long xe_ttm_lru_walk_for_evict(struct xe_ttm_lru_walk *walk, struct ttm_device *bdev,
+ struct ttm_resource_manager *man, unsigned int mem_type,
+ long target)
+{
+ struct drm_exec *exec = walk->exec;
+ struct ttm_resource_cursor cursor;
+ struct ttm_resource *res;
+ long sofar = 0;
+ long lret;
+ int ret;
+
+ spin_lock(&bdev->lru_lock);
+ ttm_resource_manager_for_each_res(man, &cursor, res) {
+ struct ttm_buffer_object *bo = res->bo;
+ bool bo_needs_unlock = false;
+ bool bo_locked = false;
+
+ if (!bo || bo->resource != res)
+ continue;
+
+ if (xe_ttm_lru_walk_trylock(walk, bo, &bo_needs_unlock))
+ bo_locked = true;
+ else if (!exec)
+ continue;
+
+ if (!ttm_bo_get_unless_zero(bo)) {
+ xe_ttm_lru_walk_unlock(bo, bo_needs_unlock);
+ continue;
+ }
+
+ spin_unlock(&bdev->lru_lock);
+
+ if (!bo_locked) {
+ ret = drm_exec_lock_obj(exec, &bo->base);
+ if (ret)
+ ttm_bo_put(bo);
+ }
+
+ lret = 0;
+
+ /*
+ * Note that in between the release of the lru lock and the
+ * drm_exec_lock_obj, the bo may have switched resource,
+ * and also memory type. In that case we just skip it.
+ */
+ if (bo->resource == res && res->mem_type == mem_type &&
+ walk->ops->allow_bo(walk, bo, mem_type))
+ lret = walk->ops->process_bo(walk, bo);
+
+ xe_ttm_lru_walk_unlock(bo, bo_needs_unlock);
+ ttm_bo_put(bo);
+ if (lret < 0) {
+ sofar = lret;
+ goto out;
+ }
+
+ sofar += lret;
+ if (sofar >= target)
+ goto out;
+
+ spin_lock(&bdev->lru_lock);
+ }
+ spin_unlock(&bdev->lru_lock);
+out:
+ ttm_resource_cursor_fini(&cursor);
+ return sofar;
+}
diff --git a/drivers/gpu/drm/xe/xe_ttm_helpers.h b/drivers/gpu/drm/xe/xe_ttm_helpers.h
new file mode 100644
index 000000000000..41d82eb7de30
--- /dev/null
+++ b/drivers/gpu/drm/xe/xe_ttm_helpers.h
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#ifndef _XE_TTM_HELPERS_H_
+#define _XE_TTM_HELPERS_H_
+
+#include <linux/types.h>
+
+struct drm_exec;
+struct ttm_device;
+struct ttm_buffer_object;
+struct ttm_operation_ctx;
+struct ttm_resource_manager;
+
+struct xe_ttm_lru_walk;
+struct xe_ttm_lru_walk_ops {
+ bool (*allow_bo)(struct xe_ttm_lru_walk *walk, struct ttm_buffer_object *bo,
+ unsigned int mem_type);
+ long (*process_bo)(struct xe_ttm_lru_walk *walk, struct ttm_buffer_object *bo);
+};
+
+struct xe_ttm_lru_walk {
+ const struct xe_ttm_lru_walk_ops *ops;
+ struct ttm_operation_ctx *ctx;
+ struct drm_exec *exec;
+};
+
+long xe_ttm_lru_walk_for_evict(struct xe_ttm_lru_walk *walk, struct ttm_device *bdev,
+ struct ttm_resource_manager *man, unsigned int mem_type,
+ long target);
+#endif
--
2.44.0
next prev parent reply other threads:[~2024-03-28 9:45 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-28 9:44 [CI v3 0/8] drm/ttm, drm/xe: Shrinker support Thomas Hellström
2024-03-28 9:44 ` [CI v3 1/8] drm/ttm: Allow TTM LRU list nodes of different types Thomas Hellström
2024-03-28 9:44 ` [CI v3 2/8] drm/ttm: Use LRU hitches Thomas Hellström
2024-03-28 9:44 ` [CI v3 3/8] drm/ttm, drm/amdgpu, drm/xe: Consider hitch moves within bulk sublist moves Thomas Hellström
2024-03-28 9:44 ` [CI v3 4/8] drm/ttm: Allow continued swapout after -ENOSPC falure Thomas Hellström
2024-03-28 9:44 ` [CI v3 5/8] drm/ttm: Add a virtual base class for graphics memory backup Thomas Hellström
2024-03-28 9:44 ` [CI v3 6/8] drm/ttm/pool: Provide a helper to shrink pages Thomas Hellström
2024-03-28 9:44 ` Thomas Hellström [this message]
2024-03-28 9:44 ` [CI v3 8/8] drm/xe: Add a shrinker for xe bos Thomas Hellström
2024-03-28 9:51 ` ✓ CI.Patch_applied: success for drm/ttm, drm/xe: Shrinker support (rev3) Patchwork
2024-03-28 9:51 ` ✗ CI.checkpatch: warning " Patchwork
2024-03-28 9:52 ` ✓ CI.KUnit: success " Patchwork
2024-03-28 10:04 ` ✓ CI.Build: " Patchwork
2024-03-28 10:06 ` ✓ CI.Hooks: " Patchwork
2024-03-28 10:08 ` ✗ CI.checksparse: warning " Patchwork
2024-03-28 10:36 ` ✓ CI.BAT: success " Patchwork
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240328094454.272050-8-thomas.hellstrom@linux.intel.com \
--to=thomas.hellstrom@linux.intel.com \
--cc=intel-xe@lists.freedesktop.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).