Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/xe: Exclude imported dma-buf BOs from the TT budget
@ 2026-11-04 18:17 Himal Prasad Ghimiray
  2026-11-04 18:22 ` ✓ CI.KUnit: success for " Patchwork
                   ` (5 more replies)
  0 siblings, 6 replies; 9+ messages in thread
From: Himal Prasad Ghimiray @ 2026-11-04 18:17 UTC (permalink / raw)
  To: intel-xe
  Cc: Thomas Hellström, Rodrigo Vivi, Matthew Auld, Matthew Brost,
	Himal Prasad Ghimiray

The TT ("system") resource manager budget is sized to total host RAM
and enforced in xe_ttm_sys_mgr_new(). Imported dma-buf BOs
(ttm_bo_type_sg) placed in XE_PL_TT are backed by external pages and
never allocate from this device's TTM pool, so they consume no host
RAM. Charging them against the host-RAM-sized budget can falsely trip
it (e.g. many peer mappings on multi-GPU systems), surfacing as a
spurious -ENOSPC/OOM while memory is actually free.

Track the bytes of ttm_bo_type_sg BOs in a new per-device counter,
mem.sys_mgr_sg_usage, and subtract it from the usage compared against
the budget. The counter is maintained in xe_ttm_sys_mgr_new() and
xe_ttm_sys_mgr_del().

Signed-off-by: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com>
---

Notes:
    This fixes it xe-locally. The same issue is generic: external
    (ttm_bo_type_sg / TTM_TT_FLAG_EXTERNAL) resources occupy a manager slot
    but consume none of the manager's budgeted memory, so it could instead
    be handled in TTM core (e.g. a man->external_usage counter excluded from
    the budget). Kept driver-local here for a minimal, low-risk fix.

 drivers/gpu/drm/xe/xe_device_types.h |  7 +++++++
 drivers/gpu/drm/xe/xe_ttm_sys_mgr.c  | 19 +++++++++++++++++--
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_device_types.h b/drivers/gpu/drm/xe/xe_device_types.h
index e334cfa5b3d2..6c59bc9da1a4 100644
--- a/drivers/gpu/drm/xe/xe_device_types.h
+++ b/drivers/gpu/drm/xe/xe_device_types.h
@@ -298,6 +298,13 @@ struct xe_device {
 		struct xe_vram_region *vram;
 		/** @mem.sys_mgr: system TTM manager */
 		struct ttm_resource_manager sys_mgr;
+		/**
+		 * @mem.sys_mgr_sg_usage: Bytes of externally-backed (dma-buf
+		 * imported, e.g. peer-GPU VRAM mapped for P2P) BOs placed in
+		 * the TT manager. These consume no host RAM and are excluded
+		 * from the host-RAM-sized TT budget.
+		 */
+		atomic64_t sys_mgr_sg_usage;
 		/** @mem.shrinker: system memory shrinker. */
 		struct xe_shrinker *shrinker;
 		/** @mem.stolen_mgr: stolen memory manager. */
diff --git a/drivers/gpu/drm/xe/xe_ttm_sys_mgr.c b/drivers/gpu/drm/xe/xe_ttm_sys_mgr.c
index 9d77ba605aec..926c4372e45a 100644
--- a/drivers/gpu/drm/xe/xe_ttm_sys_mgr.c
+++ b/drivers/gpu/drm/xe/xe_ttm_sys_mgr.c
@@ -30,6 +30,7 @@ static int xe_ttm_sys_mgr_new(struct ttm_resource_manager *man,
 			      const struct ttm_place *place,
 			      struct ttm_resource **res)
 {
+	struct xe_device *xe = container_of(man, struct xe_device, mem.sys_mgr);
 	struct xe_ttm_sys_node *node;
 	int r;
 
@@ -40,8 +41,18 @@ static int xe_ttm_sys_mgr_new(struct ttm_resource_manager *man,
 	node->tbo = tbo;
 	ttm_resource_init(tbo, place, &node->base.base);
 
-	if (!(place->flags & TTM_PL_FLAG_TEMPORARY) &&
-	    ttm_resource_manager_usage(man) > (man->size << PAGE_SHIFT)) {
+	/*
+	 * Imported dma-buf BOs (e.g. peer-GPU VRAM mapped for P2P) are backed
+	 * by external pages and never allocate from this device's TTM pool, so
+	 * they consume no host RAM and must not be charged against the
+	 * host-RAM-sized TT budget.
+	 */
+	if (tbo->type == ttm_bo_type_sg) {
+		atomic64_add(node->base.base.size, &xe->mem.sys_mgr_sg_usage);
+	} else if (!(place->flags & TTM_PL_FLAG_TEMPORARY) &&
+		   ttm_resource_manager_usage(man) -
+		   atomic64_read(&xe->mem.sys_mgr_sg_usage) >
+		   (man->size << PAGE_SHIFT)) {
 		r = -ENOSPC;
 		goto err_fini;
 	}
@@ -63,8 +74,12 @@ static int xe_ttm_sys_mgr_new(struct ttm_resource_manager *man,
 static void xe_ttm_sys_mgr_del(struct ttm_resource_manager *man,
 			       struct ttm_resource *res)
 {
+	struct xe_device *xe = container_of(man, struct xe_device, mem.sys_mgr);
 	struct xe_ttm_sys_node *node = to_xe_ttm_sys_node(res);
 
+	if (node->tbo->type == ttm_bo_type_sg)
+		atomic64_sub(res->size, &xe->mem.sys_mgr_sg_usage);
+
 	ttm_resource_fini(man, res);
 	kfree(node);
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread
* [PATCH] drm/xe: Exclude imported dma-buf BOs from the TT budget
@ 2026-11-27  3:52 Himal Prasad Ghimiray
  2026-11-27  3:54 ` sashiko-bot
  0 siblings, 1 reply; 9+ messages in thread
From: Himal Prasad Ghimiray @ 2026-11-27  3:52 UTC (permalink / raw)
  To: Thomas Hellström; +Cc: intel-xe, Himal Prasad Ghimiray, Matthew Auld

Imported dma-bufs (ttm_bo_type_sg) use external pages, not host RAM,
so they should not count against the TT budget. Count only
non-imported BOs in a new mem.sys_mgr_charged counter and check the
budget against it. The memory region query reports the same value.

v2 (Matt, Sashiko)
  - Use a separate charged counter instead of subtracting import usage
    from the manager usage, avoiding an unsigned underflow race.
  - Don't dereference the BO in the free path, since the resource may
    have been transferred to a ghost object by then.
  - Make the memory region query report the charged usage.

Fixes: dd08ebf6c352 ("drm/xe: Introduce a new DRM driver for Intel GPUs")
Co-developed-by: Matthew Auld <matthew.auld@intel.com>
Signed-off-by: Matthew Auld <matthew.auld@intel.com>
Signed-off-by: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com>
---
 drivers/gpu/drm/xe/xe_device_types.h |  6 +++++
 drivers/gpu/drm/xe/xe_query.c        |  3 ++-
 drivers/gpu/drm/xe/xe_ttm_sys_mgr.c  | 38 +++++++++++++++++++++++++---
 drivers/gpu/drm/xe/xe_ttm_sys_mgr.h  |  3 +++
 4 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_device_types.h b/drivers/gpu/drm/xe/xe_device_types.h
index 9e0d7d3d07d5..450bfe07b1da 100644
--- a/drivers/gpu/drm/xe/xe_device_types.h
+++ b/drivers/gpu/drm/xe/xe_device_types.h
@@ -301,6 +301,12 @@ struct xe_device {
 		struct xe_vram_region *vram;
 		/** @mem.sys_mgr: system TTM manager */
 		struct ttm_resource_manager sys_mgr;
+		/**
+		 * @mem.sys_mgr_charged: bytes charged against the @mem.sys_mgr
+		 * budget. Excludes imported dma-bufs (e.g. peer-GPU VRAM mapped
+		 * for P2P), which consume no local system memory.
+		 */
+		atomic64_t sys_mgr_charged;
 		/** @mem.shrinker: system memory shrinker. */
 		struct xe_shrinker *shrinker;
 		/** @mem.stolen_mgr: stolen memory manager. */
diff --git a/drivers/gpu/drm/xe/xe_query.c b/drivers/gpu/drm/xe/xe_query.c
index 9cc35fc83954..06e1aa6fe128 100644
--- a/drivers/gpu/drm/xe/xe_query.c
+++ b/drivers/gpu/drm/xe/xe_query.c
@@ -26,6 +26,7 @@
 #include "xe_mmio.h"
 #include "xe_oa.h"
 #include "xe_pxp.h"
+#include "xe_ttm_sys_mgr.h"
 #include "xe_ttm_vram_mgr.h"
 #include "xe_vram_types.h"
 #include "xe_wa.h"
@@ -278,7 +279,7 @@ static int query_mem_regions(struct xe_device *xe,
 	mem_regions->mem_regions[0].instance = 0;
 	mem_regions->mem_regions[0].min_page_size = PAGE_SIZE;
 	mem_regions->mem_regions[0].total_size = man->size << PAGE_SHIFT;
-	mem_regions->mem_regions[0].used = ttm_resource_manager_usage(man);
+	mem_regions->mem_regions[0].used = xe_ttm_sys_mgr_usage(xe);
 	mem_regions->num_mem_regions = 1;
 
 	for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) {
diff --git a/drivers/gpu/drm/xe/xe_ttm_sys_mgr.c b/drivers/gpu/drm/xe/xe_ttm_sys_mgr.c
index 9d77ba605aec..24e2c690d992 100644
--- a/drivers/gpu/drm/xe/xe_ttm_sys_mgr.c
+++ b/drivers/gpu/drm/xe/xe_ttm_sys_mgr.c
@@ -16,9 +16,15 @@
 
 struct xe_ttm_sys_node {
 	struct ttm_buffer_object *tbo;
+	bool charged;
 	struct ttm_range_mgr_node base;
 };
 
+static atomic64_t *to_charged_usage(struct ttm_resource_manager *man)
+{
+	return &container_of(man, struct xe_device, mem.sys_mgr)->mem.sys_mgr_charged;
+}
+
 static inline struct xe_ttm_sys_node *
 to_xe_ttm_sys_node(struct ttm_resource *res)
 {
@@ -40,10 +46,21 @@ static int xe_ttm_sys_mgr_new(struct ttm_resource_manager *man,
 	node->tbo = tbo;
 	ttm_resource_init(tbo, place, &node->base.base);
 
-	if (!(place->flags & TTM_PL_FLAG_TEMPORARY) &&
-	    ttm_resource_manager_usage(man) > (man->size << PAGE_SHIFT)) {
-		r = -ENOSPC;
-		goto err_fini;
+	/*
+	 * Imported dma-bufs are backed by the exporter (possibly peer VRAM),
+	 * so don't charge them against the local system memory budget.
+	 */
+	if (tbo->type != ttm_bo_type_sg) {
+		u64 size = node->base.base.size;
+		u64 usage = atomic64_add_return(size, to_charged_usage(man));
+
+		if (!(place->flags & TTM_PL_FLAG_TEMPORARY) &&
+		    usage > (man->size << PAGE_SHIFT)) {
+			atomic64_sub(size, to_charged_usage(man));
+			r = -ENOSPC;
+			goto err_fini;
+		}
+		node->charged = true;
 	}
 
 	node->base.mm_nodes[0].start = 0;
@@ -65,6 +82,8 @@ static void xe_ttm_sys_mgr_del(struct ttm_resource_manager *man,
 {
 	struct xe_ttm_sys_node *node = to_xe_ttm_sys_node(res);
 
+	if (node->charged)
+		atomic64_sub(res->size, to_charged_usage(man));
 	ttm_resource_fini(man, res);
 	kfree(node);
 }
@@ -117,3 +136,14 @@ int xe_ttm_sys_mgr_init(struct xe_device *xe)
 	ttm_resource_manager_set_used(man, true);
 	return drmm_add_action_or_reset(&xe->drm, xe_ttm_sys_mgr_fini, xe);
 }
+
+/**
+ * xe_ttm_sys_mgr_usage() - System memory charged against the TT budget
+ * @xe: The xe device.
+ *
+ * Return: Bytes of system memory in use, excluding imported dma-bufs.
+ */
+u64 xe_ttm_sys_mgr_usage(struct xe_device *xe)
+{
+	return atomic64_read(&xe->mem.sys_mgr_charged);
+}
diff --git a/drivers/gpu/drm/xe/xe_ttm_sys_mgr.h b/drivers/gpu/drm/xe/xe_ttm_sys_mgr.h
index e8f5cd395b28..0b4678a05986 100644
--- a/drivers/gpu/drm/xe/xe_ttm_sys_mgr.h
+++ b/drivers/gpu/drm/xe/xe_ttm_sys_mgr.h
@@ -6,8 +6,11 @@
 #ifndef _XE_TTM_SYS_MGR_H_
 #define _XE_TTM_SYS_MGR_H_
 
+#include <linux/types.h>
+
 struct xe_device;
 
 int xe_ttm_sys_mgr_init(struct xe_device *xe);
+u64 xe_ttm_sys_mgr_usage(struct xe_device *xe);
 
 #endif
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-11-27  3:55 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-04 18:17 [PATCH] drm/xe: Exclude imported dma-buf BOs from the TT budget Himal Prasad Ghimiray
2026-11-04 18:22 ` ✓ CI.KUnit: success for " Patchwork
2026-11-04 18:28 ` [PATCH] " sashiko-bot
2026-11-04 19:23 ` ✓ Xe.CI.BAT: success for " Patchwork
2026-11-05  3:59 ` ✓ CI.KUnit: success for drm/xe: Exclude imported dma-buf BOs from the TT budget (rev2) Patchwork
2026-11-05  4:33 ` ✓ Xe.CI.BAT: " Patchwork
2026-11-05 18:32 ` ✓ Xe.CI.FULL: " Patchwork
  -- strict thread matches above, loose matches on Subject: below --
2026-11-27  3:52 [PATCH] drm/xe: Exclude imported dma-buf BOs from the TT budget Himal Prasad Ghimiray
2026-11-27  3:54 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox