Patch "drm/xe: Fix shrinker accounting double-subtraction on nested external pins" has been added to the 6.18-stable tree
Subsystems (3)
[email protected]
AppliedThis is a note to let you know that I've just added the patch titled
drm/xe: Fix shrinker accounting double-subtraction on nested external pins
to the 6.18-stable tree which can be found at:
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary
The filename of the patch is:
drm-xe-fix-shrinker-accounting-double-subtraction-on-nested-external-pins.patch
and it can be found in the queue-6.18 subdirectory.
If you, or anyone else, feels it should not be added to the stable tree, please let <[email protected]> know about it.
From 83d5acca1f4f6132038ea88c72c5844f62e42d44 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20Hellstr=C3=B6m?= <[email protected]>
Date: Fri, 2 Oct 2026 12:08:29 +0200
Subject: drm/xe: Fix shrinker accounting double-subtraction on nested external pins
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
From: Thomas Hellström <[email protected]>
commit 83d5acca1f4f6132038ea88c72c5844f62e42d44 upstream.
xe_bo_pin_external() and xe_bo_unpin_external() support nested pins, since the underlying ttm_bo_pin()/ttm_bo_unpin() maintain a pin_count refcount rather than a boolean. However, the shrinker accounting calls xe_ttm_tt_account_subtract() and xe_ttm_tt_account_add() are invoked unconditionally on every pin_external()/unpin_external() call, instead of only on the transition into and out of the pinned state.
An external BO (dma-buf) can be pinned more than once while already pinned, for example when it has multiple attachments/importers, each independently calling xe_bo_pin_external(). Each such nested pin subtracts the BO's pages from the shrinker's accounting again, even though the BO's pages were already removed from consideration by the first pin. Symmetrically, each nested unpin adds them back again before the BO is actually unpinned. This desynchronizes the shrinker's page and object counts from reality, and since they are declared as signed long but interpreted as unsigned long in xe_shrinker_count(), the underflow can turn into an enormous shrinkable/purgeable page count, causing the shrinker to be invoked excessively under memory pressure.
Guard the accounting calls with the same pin-count transition checks already used to guard the pinned_link list maintenance, so accounting is only updated on the outermost pin and the final unpin.
While centralizing this logic into xe_bo_account_pin()/ xe_bo_account_unpin(), also close a second, related accounting gap affecting imported dma-bufs. For an imported bo's ttm_tt, xe's ttm_tt_populate hook (xe_ttm_tt_populate()) early-returns without calling xe_ttm_tt_account_add(), since TTM_TT_FLAG_EXTERNAL is set without TTM_TT_FLAG_EXTERNAL_MAPPABLE. However, TTM core's ttm_tt_populate() wrapper unconditionally marks the tt as populated on driver-hook success, so ttm_tt_is_populated() still reports true for it. xe_bo_account_pin()/xe_bo_account_unpin() trusted ttm_tt_is_populated() as a proxy for "these pages are accounted for by the shrinker", and would subtract/add pages for such a tt that were never added in the first place, again underflowing the shrinker's counts. This is reachable today via xe_bo_pin_external(), and would have become newly reachable via the fb-pin path once this patch routes __xe_pin_fb_vma() through xe_bo_pin_account() below, so fix it here rather than in a later patch, to avoid a commit in this series that introduces the regression before fixing it. Skip the accounting for imported bos, using the same EXTERNAL && !EXTERNAL_MAPPABLE test xe_ttm_tt_populate()/xe_ttm_tt_unpopulate() use to decide whether to act, via a new xe_ttm_bo_is_imported() helper (hoisted up from its only prior use site).
v2:
- Fix the critical issue caught by Sashiko AI review: the pin-count transition check introduced here was still bypassed by the fb-pin path (__xe_pin_fb_vma()/__xe_unpin_fb_vma()), which could leave a stale pinned_link entry and a permanent shrinker accounting leak when the fb-unpin performed the true last unpin. Addressed in a separate follow-up patch in this series rather than folded in here, since it is a distinct bug with its own Fixes: tag.
- Drop the unneeded local "last_unpin" variable in xe_bo_unpin_external(), restoring the original inline bo->ttm.pin_count == 1 check; it was a no-op rename not used by any of the new accounting helpers, and was going to be deleted again by the follow-up patch anyway.
v3:
- Exclude imported dma-bufs from the pin/unpin shrinker accounting, fixing another underflow caught by Sashiko AI review.
- Add a short comment on the backup-object pin site noting it's already populated/accounted at pin time, addressing (as a false-positive clarification, no code fix needed) a separate Medium-severity accounting-leak concern also caught by Sashiko AI review.
--- drivers/gpu/drm/xe/display/xe_fb_pin.c | 6 - drivers/gpu/drm/xe/xe_bo.c | 116 +++++++++++++++++++++++++-------- drivers/gpu/drm/xe/xe_bo.h | 2 3 files changed, 95 insertions(+), 29 deletions(-)
drivers/gpu/drm/xe/display/xe_fb_pin.c +3 -3
@@ -308,7 +308,7 @@ static struct i915_vma *__xe_pin_fb_vma(drm_exec_retry_on_contention (&exec);xe_validation_retry_on_oom (&ctx, &ret);if (!ret) -ttm_bo_pin (&bo->ttm); +xe_bo_pin_account (bo); }if (ret) goto err;
@@ -325,7 +325,7 @@ static struct i915_vma *__xe_pin_fb_vma( err_unpin:ttm_bo_reserve (&bo->ttm, false, false,NULL ); -ttm_bo_unpin (&bo->ttm); +xe_bo_unpin_account (bo);ttm_bo_unreserve (&bo->ttm); err:kfree (vma);
@@ -346,7 +346,7 @@ static void __xe_unpin_fb_vma(struct i91xe_ggtt_node_remove (vma->node, false);ttm_bo_reserve (&vma->bo->ttm, false, false,NULL ); -ttm_bo_unpin (&vma->bo->ttm); +xe_bo_unpin_account (vma->bo);ttm_bo_unreserve (&vma->bo->ttm);kfree (vma); }
drivers/gpu/drm/xe/xe_bo.c +90 -26
185 lines not on this page · raw message
drivers/gpu/drm/xe/xe_bo.h +2 -0
@@ -225,6 +225,8 @@ int xe_bo_pin_external(struct xe_bo *bo,int xe_bo_pin (struct xe_bo *bo,struct drm_exec *exec);void xe_bo_unpin_external (struct xe_bo *bo);void xe_bo_unpin (struct xe_bo *bo); +void xe_bo_pin_account (struct xe_bo *bo); +void xe_bo_unpin_account (struct xe_bo *bo);int xe_bo_validate (struct xe_bo *bo,struct xe_vm *vm,bool allow_res_evict,struct drm_exec *exec);
Patches currently in stable-queue which might be from [email protected] are
queue-6.18/drm-xe-fix-shrinker-accounting-double-subtraction-on-nested-external-pins.patch queue-6.18/drm-xe-fix-stale-pinned_link-entry-when-fb-pin-performs-the-final-unpin.patch
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] drm/xe: list corruption and UAF on fb unpin of externally pinned BOs --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] drm/xe: list corruption and UAF on fb unpin of externally pinned BOs --
commit 877c19cc3340fbb074cda1d52d8508b429d1bf00
Author: Thomas Hellström <[email protected]>
drm/xe: Fix shrinker accounting double-subtraction on nested external pins
This patch centralizes shrinker accounting into xe_bo_account_pin() and xe_bo_account_unpin(). It ensures accounting is only updated on the outermost pin and the final unpin, addressing a double-subtraction issue on nested external pins.
Quoted 4 lines
drivers/gpu/drm/xe/display/xe_fb_pin.c +0
-0
[ ... ]
drivers/gpu/drm/xe/display/xe_fb_pin.c static void __xe_unpin_fb_vma(struct i915_vma *vma) · line 368
Quoted 9 lines
drivers/gpu/drm/xe/display/xe_fb_pin.c +1
-1@@ -362,7 +362,7 @@ static void __xe_unpin_fb_vma(struct i915_vma *vma)xe_ggtt_node_remove (vma->node, false);ttm_bo_reserve (&vma->bo->ttm, false, false,NULL ); -ttm_bo_unpin (&vma->bo->ttm); +xe_bo_unpin_account (vma->bo);ttm_bo_unreserve (&vma->bo->ttm);kfree (vma); }
[Severity: High]
Does this code leave a stale pinned_link entry if a BO is pinned both externally and as a framebuffer?
If a BO has a pin_count of 2 from being pinned both externally and as a framebuffer, xe_bo_unpin_external() in drivers/gpu/drm/xe/xe_bo.c will reduce the count to 1 and skip list removal because of its bo->ttm.pin_count == 1 check.
When __xe_unpin_fb_vma() in drivers/gpu/drm/xe/display/xe_fb_pin.c is later called for the final unpin (dropping pin_count to 0), it only calls xe_bo_unpin_account(). It lacks the logic to acquire xe->pinned.lock and call list_del_init(&bo->pinned_link).
Could this result in the BO being freed while its pinned_link remains in xe->pinned.late.external, leading to list corruption and a Use-After-Free if the list is iterated later? I note the commit message mentions this was addressed in a separate follow-up patch, but I am including it here as it was flagged during review.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/2026101225-abacus-errand-7132@gregkh?part=1