All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Thomas Hellström" <thomas.hellstrom@linux.intel.com>
To: intel-xe@lists.freedesktop.org
Cc: "Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Somalapuram Amaranath" <Amaranath.Somalapuram@amd.com>,
	"Christian König" <christian.koenig@amd.com>,
	"Paulo Zanoni" <paulo.r.zanoni@intel.com>,
	"Simona Vetter" <simona.vetter@ffwll.ch>,
	dri-devel@lists.freedesktop.org
Subject: [PATCH v11 6/8] drm/ttm: Add helpers for shrinking
Date: Wed, 16 Oct 2024 10:55:57 +0200	[thread overview]
Message-ID: <20241016085559.11840-7-thomas.hellstrom@linux.intel.com> (raw)
In-Reply-To: <20241016085559.11840-1-thomas.hellstrom@linux.intel.com>

Add a number of helpers for shrinking that access core TTM and
core MM functionality in a way that make them unsuitable for
driver open-coding.

v11:
- New patch (split off from previous) and additional helpers.

Signed-off-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
---
 drivers/gpu/drm/ttm/ttm_backup_shmem.c | 23 +++++++
 drivers/gpu/drm/ttm/ttm_bo_util.c      | 95 +++++++++++++++++++++++++-
 include/drm/ttm/ttm_backup.h           | 20 +++---
 include/drm/ttm/ttm_bo.h               | 21 ++++++
 4 files changed, 149 insertions(+), 10 deletions(-)

diff --git a/drivers/gpu/drm/ttm/ttm_backup_shmem.c b/drivers/gpu/drm/ttm/ttm_backup_shmem.c
index cfe4140cc59d..4046d56dcc8d 100644
--- a/drivers/gpu/drm/ttm/ttm_backup_shmem.c
+++ b/drivers/gpu/drm/ttm/ttm_backup_shmem.c
@@ -5,6 +5,7 @@
 
 #include <drm/ttm/ttm_backup.h>
 #include <linux/page-flags.h>
+#include <linux/swap.h>
 
 /**
  * struct ttm_backup_shmem - A shmem based ttm_backup subclass.
@@ -107,6 +108,28 @@ static const struct ttm_backup_ops ttm_backup_shmem_ops = {
 	.fini = ttm_backup_shmem_fini,
 };
 
+/**
+ * ttm_backup_shmem_bytes_avail() - Report the approximate number of bytes of backup space
+ * left for backup.
+ *
+ * This function is intended also for driver use to indicate whether a
+ * backup attempt is meaningful.
+ *
+ * Return: An approximate size of backup space available.
+ */
+u64 ttm_backup_shmem_bytes_avail(void)
+{
+	/*
+	 * The idea behind backing up to shmem is that shmem objects may
+	 * eventually be swapped out. So no point swapping out if there
+	 * is no or low swap-space available. But the accuracy of this
+	 * number also depends on shmem actually swapping out backed-up
+	 * shmem objects without too much buffering.
+	 */
+	return (u64)get_nr_swap_pages() << PAGE_SHIFT;
+}
+EXPORT_SYMBOL_GPL(ttm_backup_shmem_bytes_avail);
+
 /**
  * ttm_backup_shmem_create() - Create a shmem-based struct backup.
  * @size: The maximum size (in bytes) to back up.
diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c
index 0cac02a9764c..e6d88557f2c0 100644
--- a/drivers/gpu/drm/ttm/ttm_bo_util.c
+++ b/drivers/gpu/drm/ttm/ttm_bo_util.c
@@ -28,7 +28,7 @@
 /*
  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  */
-
+#include <linux/swap.h>
 #include <linux/vmalloc.h>
 
 #include <drm/ttm/ttm_bo.h>
@@ -1052,3 +1052,96 @@ struct ttm_buffer_object *ttm_bo_lru_cursor_first(struct ttm_bo_lru_cursor *curs
 	return bo ? bo : ttm_bo_lru_cursor_next(curs);
 }
 EXPORT_SYMBOL(ttm_bo_lru_cursor_first);
+
+/**
+ * ttm_bo_shrink() - Helper to shrink a ttm buffer object.
+ * @ctx: The struct ttm_operation_ctx used for the shrinking operation.
+ * @bo: The buffer object.
+ * @flags: Flags governing the shrinking behaviour.
+ *
+ * The function uses the ttm_tt_back_up functionality to back up or
+ * purge a struct ttm_tt. If the bo is not in system, it's first
+ * moved there.
+ *
+ * Return: The number of pages shrunken or purged, or
+ * negative error code on failure.
+ */
+long ttm_bo_shrink(struct ttm_operation_ctx *ctx, struct ttm_buffer_object *bo,
+		   const struct ttm_bo_shrink_flags flags)
+{
+	static const struct ttm_place sys_placement_flags = {
+		.fpfn = 0,
+		.lpfn = 0,
+		.mem_type = TTM_PL_SYSTEM,
+		.flags = 0,
+	};
+	static struct ttm_placement sys_placement = {
+		.num_placement = 1,
+		.placement = &sys_placement_flags,
+	};
+	struct ttm_tt *tt = bo->ttm;
+	long lret;
+
+	dma_resv_assert_held(bo->base.resv);
+
+	if (flags.allow_move && bo->resource->mem_type != TTM_PL_SYSTEM) {
+		int ret = ttm_bo_validate(bo, &sys_placement, ctx);
+
+		/* Consider -ENOMEM and -ENOSPC non-fatal. */
+		if (ret) {
+			if (ret == -ENOMEM || ret == -ENOSPC)
+				ret = -EBUSY;
+			return ret;
+		}
+	}
+
+	ttm_bo_unmap_virtual(bo);
+	lret = ttm_bo_wait_ctx(bo, ctx);
+	if (lret < 0)
+		return lret;
+
+	lret = ttm_tt_backup(bo->bdev, tt, (struct ttm_backup_flags)
+			     {.purge = flags.purge,
+			      .writeback = flags.writeback});
+
+	if (lret < 0 && lret != -EINTR)
+		return -EBUSY;
+
+	return lret;
+}
+EXPORT_SYMBOL(ttm_bo_shrink);
+
+/**
+ * ttm_bo_shrink_suitable() - Whether a bo is suitable for shinking
+ * @ctx: The struct ttm_operation_ctx governing the shrinking.
+ * @bo: The candidate for shrinking.
+ *
+ * Check whether the object, given the information available to TTM,
+ * is suitable for shinking, This function can and should be used
+ * before attempting to shrink an object.
+ *
+ * Return: true if suitable. false if not.
+ */
+bool ttm_bo_shrink_suitable(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
+{
+	return bo->ttm && ttm_tt_is_populated(bo->ttm) && !bo->pin_count &&
+		(!ctx->no_wait_gpu ||
+		 dma_resv_test_signaled(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP));
+}
+EXPORT_SYMBOL(ttm_bo_shrink_suitable);
+
+/**
+ * ttm_bo_shrink_avoid_wait() - Whether to avoid waiting for GPU
+ * during shrinking
+ *
+ * In some situations, like direct reclaim, waiting (in particular gpu waiting)
+ * should be avoided since it may stall a system that could otherwise make progress
+ * shrinking something else less time consuming.
+ *
+ * Return: true if gpu waiting should be avoided, false if not.
+ */
+bool ttm_bo_shrink_avoid_wait(void)
+{
+	return !current_is_kswapd();
+}
+EXPORT_SYMBOL(ttm_bo_shrink_avoid_wait);
diff --git a/include/drm/ttm/ttm_backup.h b/include/drm/ttm/ttm_backup.h
index 5f8c7d3069ef..0d38dc1fd441 100644
--- a/include/drm/ttm/ttm_backup.h
+++ b/include/drm/ttm/ttm_backup.h
@@ -110,6 +110,15 @@ struct ttm_backup_ops {
 	 * After a call to @fini, it's illegal to use the @backup pointer.
 	 */
 	void (*fini)(struct ttm_backup *backup);
+
+	/**
+	 * bytes_avail: Report the approximate number of bytes of backup space
+	 * left for backup.
+	 * @backup: Pointer to the struct backup queried.
+	 *
+	 * Return: An approximate size of backup space available.
+	 */
+	u64 (*bytes_avail)(struct ttm_backup *backup);
 };
 
 /**
@@ -123,15 +132,8 @@ struct ttm_backup {
 	const struct ttm_backup_ops *ops;
 };
 
-/**
- * ttm_backup_shmem_create() - Create a shmem-based struct backup.
- * @size: The maximum size (in bytes) to back up.
- *
- * Create a backup utilizing shmem objects.
- *
- * Return: A pointer to a struct ttm_backup on success,
- * an error pointer on error.
- */
 struct ttm_backup *ttm_backup_shmem_create(loff_t size);
 
+u64 ttm_backup_shmem_bytes_avail(void);
+
 #endif
diff --git a/include/drm/ttm/ttm_bo.h b/include/drm/ttm/ttm_bo.h
index 91ecbb64f6c8..24d8769bcb37 100644
--- a/include/drm/ttm/ttm_bo.h
+++ b/include/drm/ttm/ttm_bo.h
@@ -225,6 +225,27 @@ struct ttm_lru_walk {
 s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev,
 			   struct ttm_resource_manager *man, s64 target);
 
+/**
+ * struct ttm_bo_shrink_flags - flags to govern the bo shrinking behaviour
+ * @purge: Purge the content rather than backing it up.
+ * @writeback: Attempt to immediately write content to swap space.
+ * @allow_move: Allow moving to system before shrinking. This is typically
+ * not desired for zombie- or ghost objects (with zombie object meaning
+ * objects with a zero gem object refcount)
+ */
+struct ttm_bo_shrink_flags {
+	u32 purge : 1;
+	u32 writeback : 1;
+	u32 allow_move : 1;
+};
+
+long ttm_bo_shrink(struct ttm_operation_ctx *ctx, struct ttm_buffer_object *bo,
+		   const struct ttm_bo_shrink_flags flags);
+
+bool ttm_bo_shrink_suitable(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx);
+
+bool ttm_bo_shrink_avoid_wait(void);
+
 /**
  * ttm_bo_get - reference a struct ttm_buffer_object
  *
-- 
2.46.0


  parent reply	other threads:[~2024-10-16  8:56 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-16  8:55 [PATCH v11 0/8] TTM shrinker helpers and xe buffer object shrinker Thomas Hellström
2024-10-16  8:55 ` [PATCH v11 1/8] drm/ttm: Balance ttm_resource_cursor_init() and ttm_resource_cursor_fini() Thomas Hellström
2024-10-16  8:55 ` [PATCH v11 2/8] drm/ttm: Add a virtual base class for graphics memory backup Thomas Hellström
2024-11-08 14:32   ` Christian König
2024-11-11 14:38     ` Thomas Hellström
2024-11-11 16:13       ` Christian König
2024-10-16  8:55 ` [PATCH v11 3/8] drm/ttm/pool: Provide a helper to shrink pages Thomas Hellström
2024-10-20 18:38   ` Matthew Brost
2024-10-16  8:55 ` [PATCH v11 4/8] drm/ttm: Use fault-injection to test error paths Thomas Hellström
2024-10-16  8:55 ` [PATCH v11 5/8] drm/ttm: Add a macro to perform LRU iteration Thomas Hellström
2024-10-20 18:54   ` Matthew Brost
2024-10-20 18:57   ` Matthew Brost
2024-10-16  8:55 ` Thomas Hellström [this message]
2024-10-21 21:37   ` [PATCH v11 6/8] drm/ttm: Add helpers for shrinking Matthew Brost
2024-10-16  8:55 ` [PATCH v11 7/8] drm/xe: Add a shrinker for xe bos Thomas Hellström
2024-10-24 17:00   ` Matthew Brost
2024-10-16  8:55 ` [PATCH v11 8/8] drm/xe: Increase the XE_PL_TT watermark Thomas Hellström
2024-10-16 10:56 ` ✓ CI.Patch_applied: success for TTM shrinker helpers and xe buffer object shrinker (rev10) Patchwork
2024-10-16 10:57 ` ✗ CI.checkpatch: warning " Patchwork
2024-10-16 10:58 ` ✓ CI.KUnit: success " Patchwork
2024-10-16 11:09 ` ✓ CI.Build: " Patchwork
2024-10-16 11:11 ` ✓ CI.Hooks: " Patchwork
2024-10-16 11:13 ` ✗ CI.checksparse: warning " Patchwork
2024-10-16 11:59 ` ✗ CI.BAT: failure " Patchwork
2024-10-20 19:01   ` Matthew Brost
2024-10-21 10:50     ` Thomas Hellström
2024-10-17  1:10 ` ✗ CI.FULL: " 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=20241016085559.11840-7-thomas.hellstrom@linux.intel.com \
    --to=thomas.hellstrom@linux.intel.com \
    --cc=Amaranath.Somalapuram@amd.com \
    --cc=christian.koenig@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=matthew.brost@intel.com \
    --cc=paulo.r.zanoni@intel.com \
    --cc=simona.vetter@ffwll.ch \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.