mm, drm/ttm, drm/xe: Avoid reclaim/eviction loops under fragmentation
Subsystems (9)
Matthew Brost
maintainerv3 cover mm, drm/ttm, drm/xe: Avoid reclaim/eviction loops under fragmentation
TTM allocations at higher orders can drive Xe into a pathological reclaim loop when memory is fragmented:
kswapd → shrinker → eviction → rebind (exec ioctl) → repeat
In this state, reclaim is triggered despite substantial free memory, but fails to produce contiguous higher-order pages. The Xe shrinker then evicts active buffer objects, increasing faulting and rebind activity and further feeding the loop. The result is high CPU overhead and poor GPU forward progress.
This issue was first reported in [1] and independently observed internally and by Google.
A simple reproducer is:
- Boot an iGPU system with mem=8G
- Launch 10 Chrome tabs running the WebGL aquarium demo
- Configure each tab with ~5k fish
Under this workload, ftrace shows a continuous loop of:
xe_shrinker_scan (kswapd)xe_vma_rebind_exec
Performance degrades significantly, with each tab dropping to ~2 FPS on PTL (Ubuntu 24.04).
At the same time, /proc/buddyinfo shows substantial free memory but no higher-order availability. For example, the Normal zone:
Count: 4063 4595 3455 3400 3139 2762 2293 1655 643 0 0
This corresponds to ~2.8GB free memory, but no order-9 (2MB) blocks, indicating severe fragmentation.
This series addresses the issue in two ways:
TTM: Restrict direct reclaim to beneficial_order. Larger allocations use __GFP_NORETRY to fail quickly rather than triggering reclaim.
Xe: Introduce a heuristic in the shrinker to avoid eviction when running under kswapd and the system appears memory-rich but fragmented.
With these changes, the reclaim/eviction loop is eliminated. The same workload improves to ~10 FPS per tab (Ubuntu 24.04) or ~15 FPS per tab (Ubuntu 24.10), and kswapd activity subsides.
Buddyinfo after applying this series shows restored higher-order availability:
Count: 8526 7067 3092 1959 1292 660 194 28 20 13 1
Matt
v2:
- Layer with core MM / TTM helpers (Thomas)
[1] https://patchwork.freedesktop.org/patch/716404/?series=164353&rev=1
Matthew Brost (6):
mm: Wire up order in shrink_control
mm: Introduce zone_maybe_fragmented_in_shrinker() drm/ttm: Issue direct reclaim at beneficial_order drm/ttm: Introduce ttm_bo_shrink_kswap_maybe_fragmented() drm/xe: Set TTM device beneficial_order to 9 (2M) drm/xe: Avoid shrinker reclaim from kswapd under fragmentation
drivers/gpu/drm/ttm/ttm_bo_util.c | 38 +++++++++++++++++++++++++++++++ drivers/gpu/drm/ttm/ttm_pool.c | 4 ++-- drivers/gpu/drm/xe/xe_device.c | 3 ++- drivers/gpu/drm/xe/xe_shrinker.c | 3 +++ include/drm/ttm/ttm_bo.h | 2 ++ include/linux/shrinker.h | 3 +++ include/linux/vmstat.h | 12 ++++++++++ mm/internal.h | 4 ++-- mm/shrinker.c | 11 +++++---- mm/vmscan.c | 7 +++--- 10 files changed, 75 insertions(+), 12 deletions(-)
Signature · 1 line
2.34.1
Matthew Brost
maintainerPATCH v3 1/6 mm: Wire up order in shrink_control
Pass the allocation order through shrink_control so shrinkers have visibility into the order that triggered reclaim.
This allows shrinkers to implement better heuristics, such as detecting high-order allocation pressure or fragmentation and avoiding eviction of working sets when reclaim is invoked from kswapd.
--- include/linux/shrinker.h | 3 +++ mm/internal.h | 4 ++-- mm/shrinker.c | 11 +++++++---- mm/vmscan.c | 7 ++++--- 4 files changed, 16 insertions(+), 9 deletions(-)
include/linux/shrinker.h +3 -0
@@ -37,6 +37,9 @@ struct shrink_control {/* current node being shrunk (for NUMA aware shrinkers) */ int nid; +/* Allocation order we are currently trying to fulfil. */ +s8 order; +/* * How many objects scan_objects should scan and try to reclaim. * This is reset before every call, so it is safe for callees
mm/internal.h +2 -2
@@ -1759,8 +1759,8 @@ void __meminit __init_single_page(struct page *page, unsigned long pfn,void __meminit __init_page_from_nid (unsigned long pfn,int nid);/* shrinker related functions */ -unsigned long shrink_slab (gfp_t gfp_mask,int nid,struct mem_cgroup *memcg, -int priority); +unsigned long shrink_slab (gfp_t gfp_mask,int nid,s8 order, +struct mem_cgroup *memcg,int priority); int shmem_add_to_page_cache(struct folio *folio, struct address_space *mapping,
mm/shrinker.c +7 -4
@@ -466,7 +466,7 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, }#ifdef CONFIG_MEMCG -static unsigned long shrink_slab_memcg (gfp_t gfp_mask,int nid, +static unsigned long shrink_slab_memcg (gfp_t gfp_mask,int nid,s8 order,struct mem_cgroup *memcg,int priority) {struct shrinker_info *info;
@@ -528,6 +528,7 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,struct shrink_control sc = { .gfp_mask = gfp_mask, .nid = nid, + .order = order, .memcg = memcg, };struct shrinker *shrinker;
@@ -598,6 +599,7 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,* shrink_slab - shrink slab caches * @gfp_mask: allocation context * @nid: node whose slab caches to target +* @order: order of allocation * @memcg: memory cgroup whose slab caches to target * @priority: the reclaim priority *
@@ -614,8 +616,8 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,* * Returns the number of reclaimed slab objects. */ -unsigned long shrink_slab (gfp_t gfp_mask,int nid,struct mem_cgroup *memcg, -int priority) +unsigned long shrink_slab (gfp_t gfp_mask,int nid,s8 order, +struct mem_cgroup *memcg,int priority) {unsigned long ret, freed =0 ;struct shrinker *shrinker;
@@ -628,7 +630,7 @@ unsigned long shrink_slab(gfp_t gfp_mask, int nid, struct mem_cgroup *memcg,* oom. */ if (!mem_cgroup_disabled () && !mem_cgroup_is_root (memcg)) -return shrink_slab_memcg (gfp_mask, nid, memcg, priority); +return shrink_slab_memcg (gfp_mask, nid, order, memcg, priority);/* * lockless algorithm of global shrink.
@@ -656,6 +658,7 @@ unsigned long shrink_slab(gfp_t gfp_mask, int nid, struct mem_cgroup *memcg,struct shrink_control sc = { .gfp_mask = gfp_mask, .nid = nid, + .order = order, .memcg = memcg, };
mm/vmscan.c +4 -3
@@ -412,7 +412,7 @@ static unsigned long drop_slab_node(int nid) memcg =mem_cgroup_iter (NULL ,NULL ,NULL );do { - freed +=shrink_slab (GFP_KERNEL , nid, memcg,0 ); + freed +=shrink_slab (GFP_KERNEL , nid,0 , memcg,0 ); }while ((memcg =mem_cgroup_iter (NULL , memcg,NULL )) !=NULL );return freed;
@@ -5068,7 +5068,8 @@ static int shrink_one(struct lruvec *lruvec, struct scan_control *sc) success =try_to_shrink_lruvec (lruvec, sc); -shrink_slab (sc->gfp_mask, pgdat->node_id, memcg, sc->priority); +shrink_slab (sc->gfp_mask, pgdat->node_id, sc->order, memcg, + sc->priority);if (!sc->proactive) vmpressure(sc->gfp_mask, memcg, false, sc->nr_scanned - scanned,
@@ -6170,7 +6171,7 @@ static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc)shrink_lruvec (lruvec, sc); -shrink_slab (sc->gfp_mask, pgdat->node_id, memcg, +shrink_slab (sc->gfp_mask, pgdat->node_id, sc->order, memcg, sc->priority);/* Record the group's reclaim efficiency */
Signature · 1 line
2.34.1
Matthew Brost
maintainerPATCH v3 2/6 mm: Introduce zone_maybe_fragmented_in_shrinker()
In reply to Matthew BrostIntroduce zone_maybe_fragmented_in_shrinker() as a lightweight helper to allow subsystems to make coarse decisions about reclaim behavior in the presence of likely fragmentation.
The helper implements a simple heuristic: if the number of free pages in a zone exceeds twice the high watermark, the zone is considered to have ample free memory and allocation failures are more likely due to fragmentation than overall memory pressure.
This is intentionally imprecise and is not meant to replace the core MM compaction or fragmentation accounting logic. Instead, it provides a cheap signal for callers (e.g., shrinkers) that wish to avoid overly aggressive reclaim when sufficient free memory exists but high-order allocations may still fail.
No functional changes; this is a preparatory helper for future users.
---
v3: s/zone_appear_fragmented/zone_maybe_fragmented_in_shrinker (David
Hildenbrand)
--- include/linux/vmstat.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)
include/linux/vmstat.h +12 -0
@@ -483,6 +483,18 @@ static inline const char *zone_stat_name(enum zone_stat_item item)return vmstat_text[item]; } +static inline bool zone_maybe_fragmented_in_shrinker (struct zone *zone) +{ +/* +* Simple heuristic: if the number of free pages is more than twice the +* high watermark, this may suggest that the zone is heavily fragmented. +* When called from a shrinker, aggressively evicting memory in this case +* may do more harm to overall system performance than good. +*/ +return zone_page_state (zone,NR_FREE_PAGES ) > +high_wmark_pages (zone) *2 ; +} +#ifdef CONFIG_NUMA static inline const char *numa_stat_name (enum numa_stat_item item) {
Signature · 1 line
2.34.1
Matthew Brost
maintainerPATCH v3 3/6 drm/ttm: Issue direct reclaim at beneficial_order
In reply to Matthew BrostTriggering kswap at an order higher than beneficial_order makes little sense, as the driver has already indicated the optimal order at which reclaim is effective. Similarly, issuing direct reclaim or triggering kswap at a lower order than beneficial_order is ineffective, since the driver does not benefit from reclaiming lower-order pages.
As a result, direct reclaim should only be issued with __GFP_NORETRY at exactly beneficial_order, or as a fallback, direct reclaim without __GFP_NORETRY at order 0 when failure is not an option.
--- drivers/gpu/drm/ttm/ttm_pool.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/ttm/ttm_pool.c +2 -2
@@ -165,8 +165,8 @@ static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,* Do not add latency to the allocation path for allocations orders * device tolds us do not bring them additional performance gains. */ -if (beneficial_order && order > beneficial_order) - gfp_flags &= ~__GFP_DIRECT_RECLAIM; +if (order && beneficial_order && order != beneficial_order) + gfp_flags &= ~__GFP_RECLAIM;if (!ttm_pool_uses_dma_alloc (pool)) { p =alloc_pages_node (pool->nid, gfp_flags, order);
Signature · 1 line
2.34.1
Matthew Brost
maintainerPATCH v3 4/6 drm/ttm: Introduce ttm_bo_shrink_kswap_maybe_fragmented()
In reply to Matthew BrostIntroduce ttm_bo_shrink_kswap_maybe_fragmented() to allow TTM users to distinguish background reclaim from kswapd that is likely driven by high-order allocation failures under fragmentation.
The helper returns true when:
- order of shrinker invocation is zero
- reclaim is running in kswapd, and
- the target node is valid, and
- one of the relevant zones reports free pages significantly above its high watermark (via zone_appears_fragmented()).
This provides a coarse signal that overall free memory is available, and that reclaim activity may be driven by fragmentation rather than true memory pressure.
The intent is to allow drivers to adjust shrinker behavior in this case, for example by preferring purgeable or low-value objects instead of aggressively evicting active working sets in the background reclaim path.
The heuristic is intentionally simple and conservative, and is not intended to replace core MM fragmentation or compaction decisions.
No functional change; this is a preparatory helper for TTM users.
---
v3:
- s/ttm_bo_shrink_kswap_fragmented/ttm_bo_shrink_kswap_maybe_fragmented (Andi)
- Wire in order (Thomas)
--- drivers/gpu/drm/ttm/ttm_bo_util.c | 38 +++++++++++++++++++++++++++++++ include/drm/ttm/ttm_bo.h | 2 ++ 2 files changed, 40 insertions(+)
drivers/gpu/drm/ttm/ttm_bo_util.c +38 -0
@@ -1169,3 +1169,41 @@ bool ttm_bo_shrink_avoid_wait(void)return !current_is_kswapd (); }EXPORT_SYMBOL (ttm_bo_shrink_avoid_wait); + +/** +* ttm_bo_shrink_kswap_maybe_fragmented() - Whether in kswap and memory might be +* fragmented +* @nid: current node being shrunk +* @order: order of shrinker invocation +* +* Return: true if in kswap and memory appears fragmented, false is not. +*/ +bool ttm_bo_shrink_kswap_maybe_fragmented (int nid,s8 order) +{ +enum zone_type zone_type; + +if (!order) +return false; + +if (!current_is_kswapd ()) +return false; + +if (!numa_valid_node (nid)) +return false; + +#if IS_ENABLED (CONFIG_ZONE_DMA32 ) + zone_type =ZONE_DMA32 ; +#else + zone_type =ZONE_NORMAL ; +#endif + +for (; zone_type <=ZONE_NORMAL ; ++zone_type) { +struct zone *zone = &NODE_DATA (nid)->node_zones[zone_type]; + +if (zone_maybe_fragmented_in_shrinker (zone)) +return true; + } + +return false; +} +EXPORT_SYMBOL (ttm_bo_shrink_kswap_maybe_fragmented);
include/drm/ttm/ttm_bo.h +2 -0
@@ -262,6 +262,8 @@ bool ttm_bo_shrink_suitable(struct ttm_buffer_object *bo, struct ttm_operation_cbool ttm_bo_shrink_avoid_wait (void ); +bool ttm_bo_shrink_kswap_maybe_fragmented (int nid,s8 order); +/** * ttm_bo_reserve: *
Signature · 1 line
2.34.1
Matthew Brost
maintainerPATCH v3 5/6 drm/xe: Set TTM device beneficial_order to 9 (2M)
In reply to Matthew BrostSet the TTM device beneficial_order to 9 (2M), which is the sweet spot for Xe when attempting reclaim on system memory BOs, as it matches the large GPU page size. This ensures reclaim is attempted at the most effective order for the driver.
--- drivers/gpu/drm/xe/xe_device.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_device.c +2 -1
@@ -500,7 +500,8 @@ struct xe_device *xe_device_create(struct pci_dev *pdev, err =ttm_device_init (&xe->ttm, &xe_ttm_funcs, xe->drm.dev, xe->drm.anon_inode->i_mapping, - xe->drm.vma_offset_manager,0 ); + xe->drm.vma_offset_manager, +TTM_ALLOCATION_POOL_BENEFICIAL_ORDER (get_order (SZ_2M )));if (WARN_ON (err))return ERR_PTR (err);
Signature · 1 line
2.34.1
Matthew Brost
maintainerPATCH v3 6/6 drm/xe: Avoid shrinker reclaim from kswapd under fragmentation
In reply to Matthew BrostWhen the Xe shrinker is invoked from kswapd, a large amount of free memory in usable zones relative to the high watermark is a strong signal that reclaim is being driven by fragmentation rather than true memory pressure.
In this case, shrinking Xe memory is unlikely to help kswapd make forward progress. Instead it can evict active GPU memory despite the system still having substantial free memory, increasing residency churn and reducing GPU forward progress.
Detect this case and bail out early from the Xe shrinker when running in kswapd, shrinker invocation is a higher order, and any usable zone has more than 2x its high watermark free.
---
v3:
- Wire in order to heuristic (Thomas)
--- drivers/gpu/drm/xe/xe_shrinker.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/gpu/drm/xe/xe_shrinker.c +3 -0
@@ -236,6 +236,9 @@ static unsigned long xe_shrinker_scan(struct shrinker *shrink, struct shrink_conif (nr_scanned >= nr_to_scan || !can_backup) goto out; +if (ttm_bo_shrink_kswap_maybe_fragmented (sc->nid, sc->order)) + goto out; +/* If we didn't wake before, try to do it now if needed. */ if (!runtime_pm) runtime_pm =xe_shrinker_runtime_pm_get (shrinker, true,0 , can_backup);
Signature · 1 line
2.34.1
Patchwork
CI✗ CI.checkpatch: warning for mm, drm/ttm, drm/xe: Avoid reclaim/eviction loops under fragmentation (rev2)
In reply to Matthew Brost== Series Details ==
== Series Details ==
Series: mm, drm/ttm, drm/xe: Avoid reclaim/eviction loops under fragmentation (rev2)
URL : https://patchwork.freedesktop.org/series/165329/
State : warning
== Summary ==
+ KERNEL=/kernel
+ git clone https://gitlab.freedesktop.org/drm/maintainer-tools mt Cloning into 'mt'...
warning: redirecting to https://gitlab.freedesktop.org/drm/maintainer-tools.git/
+ git -C mt rev-list -n1 origin/master c8c12e558adaef7a4d125d83b6e1f8824bc13b82
+ cd /kernel
+ git config --global --add safe.directory /kernel
+ git log -n1
commit b7aea8d410b38ff13cdd77dcb31bef0f8b9b5f8b
Author: Matthew Brost <[email protected]> Date: Thu Apr 30 11:23:35 2026 -0700
drm/xe: Avoid shrinker reclaim from kswapd under fragmentation
When the Xe shrinker is invoked from kswapd, a large amount of free memory in usable zones relative to the high watermark is a strong signal that reclaim is being driven by fragmentation rather than true memory pressure.
In this case, shrinking Xe memory is unlikely to help kswapd make forward progress. Instead it can evict active GPU memory despite the system still having substantial free memory, increasing residency churn and reducing GPU forward progress.
Detect this case and bail out early from the Xe shrinker when running in kswapd, shrinker invocation is a higher order, and any usable zone has more than 2x its high watermark free.
+ /mt/dim checkpatch 6472756e8384e3945f12b6726dd9dc1ac12f3bb4 drm-intel c8517f9b1fa3 mm: Wire up order in shrink_control
-:79: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #79: FILE: mm/shrinker.c:470:
+static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, s8 order,
struct mem_cgroup *memcg, int priority)
total: 0 errors, 0 warnings, 1 checks, 91 lines checked 887652c0b41c mm: Introduce zone_maybe_fragmented_in_shrinker() 4d1f3593c173 drm/ttm: Issue direct reclaim at beneficial_order a64a875d276b drm/ttm: Introduce ttm_bo_shrink_kswap_maybe_fragmented() 892550c48b83 drm/xe: Set TTM device beneficial_order to 9 (2M) b7aea8d410b3 drm/xe: Avoid shrinker reclaim from kswapd under fragmentation
Patchwork
CI✗ CI.KUnit: failure for mm, drm/ttm, drm/xe: Avoid reclaim/eviction loops under fragmentation (rev2)
In reply to Matthew Brost== Series Details ==
== Series Details ==
Series: mm, drm/ttm, drm/xe: Avoid reclaim/eviction loops under fragmentation (rev2)
URL : https://patchwork.freedesktop.org/series/165329/
State : failure
== Summary ==
+ trap cleanup EXIT
+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/drm/xe/.kunitconfig ERROR:root:../mm/shrinker.c: In function ‘shrink_slab’:../mm/shrinker.c:633:57: warning: passing argument 3 of ‘shrink_slab_memcg’ makes pointer from integer without a cast [-Wint-conversion]
633 | return shrink_slab_memcg(gfp_mask, nid, order, memcg, priority);
| ^~~~~
| |
| s8 {aka signed char}
../mm/shrinker.c:592:44: note: expected ‘struct mem_cgroup *’ but argument is of type ‘s8’ {aka ‘signed char’}
592 | struct mem_cgroup *memcg, int priority)
| ~~~~~~~~~~~~~~~~~~~^~~~~
../mm/shrinker.c:633:64: warning: passing argument 4 of ‘shrink_slab_memcg’ makes integer from pointer without a cast [-Wint-conversion]
633 | return shrink_slab_memcg(gfp_mask, nid, order, memcg, priority);
| ^~~~~
| |
| struct mem_cgroup *../mm/shrinker.c:592:55: note: expected ‘int’ but argument is of type ‘struct mem_cgroup *’
592 | struct mem_cgroup *memcg, int priority)
| ~~~~^~~~~~~~../mm/shrinker.c:633:24: error: too many arguments to function ‘shrink_slab_memcg’
633 | return shrink_slab_memcg(gfp_mask, nid, order, memcg, priority);
| ^~~~~~~~~~~~~~~~~
../mm/shrinker.c:591:22: note: declared here
591 | static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
| ^~~~~~~~~~~~~~~~~make[4]: *** [../scripts/Makefile.build:289: mm/shrinker.o] Error 1 make[4]: *** Waiting for unfinished jobs....
make[3]: *** [../scripts/Makefile.build:548: mm] Error 2 make[3]: *** Waiting for unfinished jobs....
make[2]: *** [/kernel/Makefile:2141: .] Error 2
make[1]: *** [/kernel/Makefile:248: __sub-make] Error 2
make: *** [Makefile:248: __sub-make] Error 2
[19:06:10] Configuring KUnit Kernel ...
Generating .config ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig [19:06:15] Building KUnit Kernel ... Populating config with: $ make ARCH=um O=.kunit olddefconfig Building with: $ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=48 + cleanup ++ stat -c %u:%g /kernel + chown -R 1003:1003 /kernel