[PATCH v2 3/5] drm/ttm: Introduce ttm_bo_shrink_kswap_fragmented()

Matthew Brost
2026-04-23T05:57:12Z

Introduce ttm_bo_shrink_kswap_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:
- 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.

Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: Carlos Santa <carlos.santa@intel.com>
Cc: Christian Koenig <christian.koenig@amd.com>
Cc: Huang Rui <ray.huang@amd.com>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Thomas Zimmermann <tzimmermann@suse.de>
Cc: David Airlie <airlied@gmail.com>
Cc: Simona Vetter <simona@ffwll.ch>
CC: dri-devel@lists.freedesktop.org
Cc: Daniel Colascione <dancol@dancol.org>
Signed-off-by: Matthew Brost <matthew.brost@intel.com>
---
drivers/gpu/drm/ttm/ttm_bo_util.c | 34 +++++++++++++++++++++++++++++++
include/drm/ttm/ttm_bo.h | 2 ++
2 files changed, 36 insertions(+)

diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c
index f83b7d5ec6c6..46adcb7d9a0c 100644
--- a/drivers/gpu/drm/ttm/ttm_bo_util.c
+++ b/drivers/gpu/drm/ttm/ttm_bo_util.c
@@ -1169,3 +1169,37 @@ bool ttm_bo_shrink_avoid_wait(void)
return !current_is_kswapd();
}
EXPORT_SYMBOL(ttm_bo_shrink_avoid_wait);
+
+/**
+ * ttm_bo_shrink_kswap_fragmented() - Whether in kswap and memory appears
+ * fragmented
+ * @nid: current node being shrunk
+ *
+ * Return: true if in kswap and memory appears fragmented, false is not.
+ */
+bool ttm_bo_shrink_kswap_fragmented(int nid)
+{
+ enum zone_type zone_type;
+
+ 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_appears_fragmented(zone))
+ return true;
+ }
+
+ return false;
+}
+EXPORT_SYMBOL(ttm_bo_shrink_kswap_fragmented);
diff --git a/include/drm/ttm/ttm_bo.h b/include/drm/ttm/ttm_bo.h
index 8310bc3d55f9..9a457fd667bb 100644
--- a/include/drm/ttm/ttm_bo.h
+++ b/include/drm/ttm/ttm_bo.h
@@ -262,6 +262,8 @@ bool ttm_bo_shrink_suitable(struct ttm_buffer_object *bo, struct ttm_operation_c

bool ttm_bo_shrink_avoid_wait(void);

+bool ttm_bo_shrink_kswap_fragmented(int nid);
+
/**
* ttm_bo_reserve:
*
--
2.34.1