Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Himal Prasad Ghimiray" <himal.prasad.ghimiray@intel.com>,
	intel-xe@lists.freedesktop.org
Subject: Re: [PATCH] drm/xe: Exclude imported dma-buf BOs from the TT budget
Date: Wed, 04 Nov 2026 18:28:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-197846@kernel.org> (raw)
In-Reply-To: <20261104181733.779688-2-himal.prasad.ghimiray@intel.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] drm/xe: race and unsigned underflow in xe_ttm_sys_mgr_new() budget check
- [Medium] drm/xe: unsubtracted sys_mgr_sg_usage in query_mem_regions()
- [Medium] drm/xe: useless eviction of imported dma-buf BOs from XE_PL_TT on ENOSPC
--

commit a4e17a0c19f77a7394d409e5a5369673795b4c8f
Author: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com>

drm/xe: Exclude imported dma-buf BOs from the TT budget

This commit tracks the byte size of imported dma-buf BOs (ttm_bo_type_sg)
placed in the TT resource manager in a per-device atomic64_t counter,
mem.sys_mgr_sg_usage. It subtracts this counter from the resource manager
usage in xe_ttm_sys_mgr_new() so externally backed BOs do not count against
the host-RAM-sized TT budget.

> diff --git a/drivers/gpu/drm/xe/xe_ttm_sys_mgr.c b/drivers/gpu/drm/xe/xe_ttm_sys_mgr.c
> index 9d77ba605aecf..926c4372e45aa 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;
>  	}

[Severity: Medium]
Can this subtraction underflow and cause a spurious -ENOSPC when racing
with a concurrent ttm_bo_type_sg allocation or deallocation?

In xe_ttm_sys_mgr_new(), ttm_resource_manager_usage(man) reads man->usage
under man->bdev->lru_lock and returns uint64_t, while
atomic64_read(&xe->mem.sys_mgr_sg_usage) is read without lru_lock and
returns s64. Because of C usual arithmetic conversions, the subtraction
evaluates in unsigned 64-bit arithmetic.

Since ttm_resource_alloc() and ttm_resource_free() only hold the per-BO
dma_resv lock, two threads operating on different BOs can run concurrently:

1. Thread A (allocating a small normal BO of size X) calls
   ttm_resource_manager_usage(man) and observes X.
2. Before Thread A calls atomic64_read(&xe->mem.sys_mgr_sg_usage), Thread B
   (allocating a ttm_bo_type_sg BO of size Y > X) completes
   ttm_resource_init() and atomic64_add(Y, &xe->mem.sys_mgr_sg_usage).
3. Thread A now reads sys_mgr_sg_usage >= Y. The unsigned subtraction X - Y
   wraps around near U64_MAX, which is greater than (man->size << PAGE_SHIFT),
   failing Thread A's allocation with -ENOSPC.

A similar race occurs if the compiler evaluates atomic64_read() before
ttm_resource_manager_usage(man) while another thread is freeing a
ttm_bo_type_sg resource in xe_ttm_sys_mgr_del().

In addition, because ttm_resource_init() adds to man->usage before
atomic64_add() updates sys_mgr_sg_usage here (and xe_ttm_sys_mgr_del()
subtracts from sys_mgr_sg_usage before ttm_resource_fini() subtracts from
man->usage), a concurrent normal allocation can transiently observe the SG
BO in man->usage without seeing it in sys_mgr_sg_usage.

[Severity: Medium]
Should query_mem_regions() in drivers/gpu/drm/xe/xe_query.c also subtract
xe->mem.sys_mgr_sg_usage when reporting system memory usage to userspace?

Currently, query_mem_regions() populates DRM_XE_MEM_REGION_CLASS_SYSMEM as
follows:

drivers/gpu/drm/xe/xe_query.c:query_mem_regions() {
	...
	man = ttm_manager_type(&xe->ttm, XE_PL_TT);
	mem_regions->mem_regions[0].mem_class = DRM_XE_MEM_REGION_CLASS_SYSMEM;
	...
	mem_regions->mem_regions[0].total_size = man->size << PAGE_SHIFT;
	mem_regions->mem_regions[0].used = ttm_resource_manager_usage(man);
	...
}

Because ttm_resource_init() still adds ttm_bo_type_sg BOs to man->usage
while xe_ttm_sys_mgr_new() excludes them from the (man->size << PAGE_SHIFT)
limit, imported peer-GPU P2P dma-buf mappings are still reported as used
host RAM in mem_regions[0].used, and mem_regions[0].used can now exceed
mem_regions[0].total_size.

> @@ -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);

[Severity: Medium]
When a normal BO allocation hits the TT budget limit and returns -ENOSPC,
will TTM uselessly evict unpinned imported dma-buf BOs (ttm_bo_type_sg)
from XE_PL_TT?

Imported dma-buf BOs created via xe_gem_prime_import() ->
xe_dma_buf_create_obj() are unpinned ttm_bo_type_sg BOs placed in XE_PL_TT,
so ttm_resource_init() places them on man->lru.

When a normal XE_PL_TT allocation fails with -ENOSPC in
xe_ttm_sys_mgr_new(), ttm_bo_alloc_resource() -> ttm_bo_evict_alloc() walks
sys_mgr's LRU list and calls xe_bo_eviction_valuable(), which returns true
for unpinned ttm_bo_type_sg BOs. TTM then evicts those BOs to XE_PL_SYSTEM
via xe_bo_move() -> xe_bo_move_notify() -> xe_bo_trigger_rebind(), waiting
on their fences and invalidating their VMAs.

However, when xe_ttm_sys_mgr_del() frees the evicted SG BO's resource, it
subtracts res->size from xe->mem.sys_mgr_sg_usage while ttm_resource_fini()
subtracts res->size from man->usage. The net budgeted usage
(man->usage - sys_mgr_sg_usage) remains unchanged, so evicting
ttm_bo_type_sg BOs frees zero bytes of TT budget and the retried allocation
in ttm_bo_evict_cb() immediately fails with -ENOSPC again.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261104181733.779688-2-himal.prasad.ghimiray@intel.com?part=1

  parent reply	other threads:[~2026-11-04 18:28 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` sashiko-bot [this message]
2026-11-04 19:23 ` ✓ Xe.CI.BAT: " 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

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=sashiko-outbox-197846@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=himal.prasad.ghimiray@intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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