Skip to contentSkip to conversation

drm/xe: count UMA shrinkable BOs as reclaimable without swap

PATCH In review aquiles 3 participants 7 messages Jun 26 – Jul 1

Subsystems (2)
lore
Share & tools
lore

aquiles

author
#0 lore raw

PATCH 1/2 drm/xe: count UMA shrinkable BOs as reclaimable without swap

On integrated (UMA) devices all GPU buffer objects reside in system RAM. xe_shrinker_count() previously gated shrinkable (non-purgeable) page counts on ttm_backup_bytes_avail(), so those pages were invisible to the kernel's memory reclaim on systems without swap configured.

On UMA, shrinkable system-memory BOs can be reclaimed independently of swap availability. Report them unconditionally so the shrinker can always request their eviction under memory pressure.

---
 drivers/gpu/drm/xe/xe_shrinker.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_shrinker.c +7 -1
index 83374cd57..3ca2de342 100644
@@ -140,16 +140,22 @@ xe_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
 {
 	struct xe_shrinker *shrinker = to_xe_shrinker(shrink);
 	unsigned long num_pages;
+	long uma_reclaimable_pages;
 	bool can_backup = !!(sc->gfp_mask & __GFP_FS);
+	bool is_uma = !IS_DGFX(shrinker->xe);
 
 	num_pages = ttm_backup_bytes_avail() >> PAGE_SHIFT;
 	read_lock(&shrinker->lock);
 
+	uma_reclaimable_pages = is_uma ? shrinker->shrinkable_pages : 0;
+
 	if (can_backup)
-		num_pages = min_t(unsigned long, num_pages, shrinker->shrinkable_pages);
+		num_pages = min_t(unsigned long, num_pages,
+				  shrinker->shrinkable_pages - uma_reclaimable_pages);
 	else
 		num_pages = 0;
 
+	num_pages += uma_reclaimable_pages;
 	num_pages += shrinker->purgeable_pages;
 	read_unlock(&shrinker->lock);
 
Signature · 1 line
2.54.0
author
#1 lore raw

PATCH 2/2 drm/xe: skip resv fence wait in xe_svm_invalidate if no ranges are bound

xe_svm_invalidate() waited on the VM's reservation object fences before processing MMU notifier events, even when no range in the affected address region had active GPU bindings (tile_present == 0).

Add a pre-scan over the affected ranges and skip dma_resv_wait_timeout() entirely when none require invalidation. This avoids stalling CPU reclaim for the common case where MMU notifier events fire on unbound SVM ranges, which is frequent on UMA systems under memory pressure.

---
 drivers/gpu/drm/xe/xe_svm.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
drivers/gpu/drm/xe/xe_svm.c +17 -5
index e1651e70c..5d071bab3 100644
@@ -233,6 +233,7 @@ static void xe_svm_invalidate(struct drm_gpusvm *gpusvm,
 	ktime_t start = xe_gt_stats_ktime_get();
 	u64 adj_start = mmu_range->start, adj_end = mmu_range->end;
 	u8 tile_mask = 0, id;
+	bool needs_inval = false;
 	long err;
 
 	xe_svm_assert_in_notifier(vm);
@@ -258,11 +259,22 @@ static void xe_svm_invalidate(struct drm_gpusvm *gpusvm,
 	if (xe_vm_is_closed(vm))
 		goto range_notifier_event_end;
 
-	/*
-	 * XXX: Less than ideal to always wait on VM's resv slots if an
-	 * invalidation is not required. Could walk range list twice to figure
-	 * out if an invalidations is need, but also not ideal.
-	 */
+	/* Pre-scan: skip fence wait if no range has active GPU bindings. */
+
+	r = first;
+	drm_gpusvm_for_each_range(r, notifier, adj_start, adj_end) {
+		struct xe_svm_range *range = to_xe_range(r);
+
+		if (!range->base.pages.flags.unmapped && range->tile_present) {
+			needs_inval = true;
+			break;
+		}
+
+	}
+	if (!needs_inval)
+		goto range_notifier_event_end;
+
+
 	err = dma_resv_wait_timeout(xe_vm_resv(vm),
 				    DMA_RESV_USAGE_BOOKKEEP,
 				    false, MAX_SCHEDULE_TIMEOUT);
Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH 2/2] drm/xe: skip resv fence wait in xe_svm_invalidate if no ranges are bound

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Skipping `dma_resv_wait_timeout` based on CPU-side state (`tile_present == 0`) causes the driver…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Skipping dma_resv_wait_timeout based on CPU-side state (tile_present == 0) causes the driver to unmap DMA addresses while a GPU unbind job is still in flight, leading to IOMMU faults and physical memory Use-After-Free. --

[Note: The format of this report has been altered to provide a free-form summary without direct code quotes, due to system recitation restrictions.]

commit 3a11397b2ceb001d3564c3c183ff54d6b34bd490
Author: aquiles <[email protected]>

drm/xe: skip resv fence wait in xe_svm_invalidate if no ranges are bound

This commit modifies xe_svm_invalidate() to pre-scan affected SVM ranges and skip the dma_resv_wait_timeout() if no ranges are currently bound to the GPU (indicated by tile_present == 0). This is intended to prevent stalling CPU reclaim when handling MMU notifier events for unbound SVM ranges.

[Severity: High]

Does skipping the dma_resv_wait_timeout() based on the CPU-side tile_present state introduce a race condition with asynchronous GPU unbind operations?

In xe_svm_invalidate(), the newly added pre-scan loop skips the reservation wait if range->tile_present is zero. However, looking at op_commit() in drivers/gpu/drm/xe/xe_pt.c, tile_present is cleared synchronously when an unbind job is submitted, not when it completes. The actual page-table update job is queued asynchronously to the GPU and tracked by a BOOKKEEP fence on the VM's reservation object.

If an MMU notifier event (like CPU page reclaim) fires concurrently, the pre-scan sees tile_present == 0, skips the fence wait, and jumps straight to cleanup. This causes drm_gpusvm_range_unmap_pages() to unmap the DMA addresses while the GPU unbind is still in flight.

Can this lead to IOMMU faults and potential use-after-free of physical memory, since the CPU will free the pages before the GPU has actually finished unmapping them?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#3 lore raw
In reply to aquiles
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] xe_shrinker_count() reports shrinkable UMA BOs regardless of __GFP_FS, but xe_shrinker_scan()…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] xe_shrinker_count() reports shrinkable UMA BOs regardless of __GFP_FS, but xe_shrinker_scan() bails out without scanning them when __GFP_FS is missing.
- [High] Bypassing ttm_backup_bytes_avail() for UMA shrinkable BOs falsely assumes they can be freed without swap space, causing useless CPU overhead yielding zero net memory reclamation. --

commit eb93ae097c20ae5f357e41557ba0c3d785cb142c
Author: aquiles <[email protected]>

drm/xe: count UMA shrinkable BOs as reclaimable without swap

This commit modifies the xe shrinker count logic to unconditionally report UMA shrinkable buffer objects as reclaimable, even without swap space. It subtracts UMA shrinkable pages from the ttm backup limit check and adds them back unconditionally.

Quoted 4 lines
drivers/gpu/drm/xe/xe_shrinker.c +0 -0
index 83374cd576608..3ca2de342dcd6 100644

[ ... ]

Quoted 7 lines
drivers/gpu/drm/xe/xe_shrinker.c +2 -0
@@ -140,16 +140,22 @@ xe_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
 {
 	struct xe_shrinker *shrinker = to_xe_shrinker(shrink);
 	unsigned long num_pages;
+	long uma_reclaimable_pages;
 	bool can_backup = !!(sc->gfp_mask & __GFP_FS);
+	bool is_uma = !IS_DGFX(shrinker->xe);

drivers/gpu/drm/xe/xe_shrinker.c xe_shrinker_count(struct shrinker *shrink, struct shrink_control *sc) · line 154

Quoted 10 lines
drivers/gpu/drm/xe/xe_shrinker.c +4 -1
 
 	num_pages = ttm_backup_bytes_avail() >> PAGE_SHIFT;
 	read_lock(&shrinker->lock);
 
+	uma_reclaimable_pages = is_uma ? shrinker->shrinkable_pages : 0;
+
 	if (can_backup)
-		num_pages = min_t(unsigned long, num_pages, shrinker->shrinkable_pages);
+		num_pages = min_t(unsigned long, num_pages,
+				  shrinker->shrinkable_pages - uma_reclaimable_pages);

[Severity: High]
Does bypassing the ttm_backup_bytes_avail() limit for UMA shrinkable BOs result in zero net memory reclamation without swap?

On UMA systems without swap, the shrinker uses ttm_bo_shrink() which backs up the BO by copying its pages to a shmem file. Because no swap space is available, the newly allocated shmem backup pages cannot be swapped to disk and remain permanently pinned in physical memory.

This deceives the core MM into trying to reclaim these pages, burning CPU cycles on copying memory that yields zero net free physical memory.

drivers/gpu/drm/xe/xe_shrinker.c xe_shrinker_count(struct shrinker *shrink, struct shrink_control *sc) · line 158

Quoted 4 lines
drivers/gpu/drm/xe/xe_shrinker.c +1 -0
 	else
 		num_pages = 0;
 
+	num_pages += uma_reclaimable_pages;

[Severity: High]
Can unconditionally returning uma_reclaimable_pages cause an infinite reclaim retry loop when __GFP_FS is missing?

When the shrinker is invoked with __GFP_FS missing (e.g., direct reclaim in the filesystem or block layer), can_backup is false, but this code adds uma_reclaimable_pages to the returned count anyway.

Looking at xe_shrinker_scan() in the same file, the scan loop explicitly bails out if !can_backup before it ever attempts to scan non-purgeable (shrinkable) pages:

	if (nr_scanned >= nr_to_scan || !can_backup)
		goto out;

	/* ... */
	shrink_flags.purge = false;

The shrinker API strictly requires the count to accurately reflect only the objects that the scan function will attempt to free under the given gfp_mask. Returning a falsely high count for objects that the scan function explicitly skips breaks the VM reclaim feedback loop, causing infinite reclaim loops.

Quoted 3 lines
drivers/gpu/drm/xe/xe_shrinker.c +0 -0
 	num_pages += shrinker->purgeable_pages;
 	read_unlock(&shrinker->lock);
 
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

Subject: Re: [PATCH 1/2] drm/xe: count UMA shrinkable BOs as reclaimable without swap
I'm withdrawing this patch , approach is wrong

Quoted 97 lines · On Fri, Jun 26, 2026 at 11:40 AM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] xe_shrinker_count() reports shrinkable UMA BOs regardless of __GFP_FS, but xe_shrinker_scan() bails out without scanning them when __GFP_FS is missing.
- [High] Bypassing ttm_backup_bytes_avail() for UMA shrinkable BOs falsely assumes they can be freed without swap space, causing useless CPU overhead yielding zero net memory reclamation.
--

commit eb93ae097c20ae5f357e41557ba0c3d785cb142c
Author: aquiles <[email protected]>

drm/xe: count UMA shrinkable BOs as reclaimable without swap

This commit modifies the xe shrinker count logic to unconditionally report UMA shrinkable buffer objects as reclaimable, even without swap space. It subtracts UMA shrinkable pages from the ttm backup limit check and adds them back unconditionally.

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_shrinker.c

Quoted 3 lines

index 83374cd576608..3ca2de342dcd6 100644

drivers/gpu/drm/xe/xe_shrinker.c +0 -0

[ ... ]

drivers/gpu/drm/xe/xe_shrinker.c xe_shrinker_count(struct shrinker *shrink, struct

Quoted 1 line
drivers/gpu/drm/xe/xe_shrinker.c +0 -0
@@ -140,16 +140,22 @@ xe_shrinker_count(struct shrinker *shrink, struct

shrink_control *sc)

Quoted 4 lines
drivers/gpu/drm/xe/xe_shrinker.c +1 -0
 {
      struct xe_shrinker *shrinker = to_xe_shrinker(shrink);
      unsigned long num_pages;
+     long uma_reclaimable_pages;

drivers/gpu/drm/xe/xe_shrinker.c xe_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)

Quoted 10 lines
drivers/gpu/drm/xe/xe_shrinker.c +3 -1
      bool can_backup = !!(sc->gfp_mask & __GFP_FS);
+     bool is_uma = !IS_DGFX(shrinker->xe);
 
      num_pages = ttm_backup_bytes_avail() >> PAGE_SHIFT;
      read_lock(&shrinker->lock);
 
+     uma_reclaimable_pages = is_uma ? shrinker->shrinkable_pages : 0;
+
      if (can_backup)
-             num_pages = min_t(unsigned long, num_pages,

shrinker->shrinkable_pages);

drivers/gpu/drm/xe/xe_shrinker.c xe_shrinker_count(struct shrinker *shrink, struct · line 154

Quoted 2 lines
drivers/gpu/drm/xe/xe_shrinker.c +2 -0
+             num_pages = min_t(unsigned long, num_pages,
+                               shrinker->shrinkable_pages -

uma_reclaimable_pages);

[Severity: High]
Does bypassing the ttm_backup_bytes_avail() limit for UMA shrinkable BOs result in zero net memory reclamation without swap?

On UMA systems without swap, the shrinker uses ttm_bo_shrink() which backs up the BO by copying its pages to a shmem file. Because no swap space is available, the newly allocated shmem backup pages cannot be swapped to disk and remain permanently pinned in physical memory.

This deceives the core MM into trying to reclaim these pages, burning CPU cycles on copying memory that yields zero net free physical memory.

drivers/gpu/drm/xe/xe_shrinker.c xe_shrinker_count(struct shrinker *shrink, struct shrink_control *sc) · line 158

Quoted 4 lines
drivers/gpu/drm/xe/xe_shrinker.c +1 -0
      else
              num_pages = 0;
 
+     num_pages += uma_reclaimable_pages;

[Severity: High]
Can unconditionally returning uma_reclaimable_pages cause an infinite reclaim retry loop when __GFP_FS is missing?

When the shrinker is invoked with __GFP_FS missing (e.g., direct reclaim in the filesystem or block layer), can_backup is false, but this code adds uma_reclaimable_pages to the returned count anyway.

Looking at xe_shrinker_scan() in the same file, the scan loop explicitly bails out if !can_backup before it ever attempts to scan non-purgeable (shrinkable) pages:

        if (nr_scanned >= nr_to_scan || !can_backup)
                goto out;

        /* ... */
        shrink_flags.purge = false;

The shrinker API strictly requires the count to accurately reflect only the objects that the scan function will attempt to free under the given gfp_mask.
Returning a falsely high count for objects that the scan function explicitly
skips breaks the VM reclaim feedback loop, causing infinite reclaim loops.

drivers/gpu/drm/xe/xe_shrinker.c xe_shrinker_count(struct shrinker *shrink, struct shrink_control *sc) · line 161

Quoted 3 lines
drivers/gpu/drm/xe/xe_shrinker.c +0 -0
      num_pages += shrinker->purgeable_pages;
      read_unlock(&shrinker->lock);
 

--
Sashiko AI review ·
https://sashiko.dev/#/patchset/[email protected]?part=1

#6 lore raw

✗ LGCI.VerificationFailed: failure for series starting with [1/2] drm/xe: count UMA shrinkable BOs as reclaimable without swap

In reply to aquiles
== Series Details ==

== Series Details ==

Series: series starting with [1/2] drm/xe: count UMA shrinkable BOs as reclaimable without swap
URL : https://patchwork.freedesktop.org/series/169600/
State : failure

== Summary ==

Series author address '[email protected]' is not on the allowlist, which prevents CI from being automatically triggered. If you want CI to run for this series, ask Patchwork project owners to click 'retest' on the series in Patchwork. Exception occurred during validation, bailing out!
Build URL: http://intel-gfx-ci-public.igk.intel.com:8080/job/xe_pw_trigger/1202089/ (on master)