Skip to contentSkip to conversation

RFC: drm/xe: Dynamic Xe Memory Monitor

PATCH New S Sebinraj 1 participant 5 messages Sep 7

Subsystems (3)
lore
Share & tools
lore

S Sebinraj

author
#0 lore raw

cover RFC: drm/xe: Dynamic Xe Memory Monitor

Introduce xe_mem_monitor, a periodic worker that samples xe->global_total_pages (the same counter that feeds the gpu_mem/gpu_mem_total tracepoint) and tracks GPU memory growth using an exponential moving average (EMA) over per-interval deltas:

delta(t) = max(0, mem(t) - mem(t-1))
EMA(t) = alpha * delta(t) + (1 - alpha) * EMA(t-1)

where alpha = 0.8, applied to every real sample regardless of mode.

A single alpha and a single polling cadence (mem_monitor_poll_ms) are used in both LATENCY and HIGH_THROUGHPUT mode, once HIGH_THROUGHPUT mode is entered there is little benefit to re-checking any faster, since mem_monitor_debounce_count consecutive below-threshold samples are already required before switching back, which itself provides the desired minimum dwell time (debounce_count * poll_ms) at this single cadence.

When the EMA of growth exceeds a threshold, xe->gpu_mem_mode is switched from XE_GPU_MEM_MODE_LATENCY to XE_GPU_MEM_MODE_HIGH_THROUGHPUT, and back once mem_monitor_debounce_count consecutive below-threshold samples are seen.

xe->gpu_mem_mode is intended to be read by the TTM page allocation path to trade allocation latency for throughput during GPU memory growth bursts; the GFP flag adjustment itself is added in a follow-up change.

For power, the worker sleeps after mem_monitor_idle_timeout_ms of no significant activity in LATENCY mode. The worker is re-armed on activity.

The worker is suspended/resumed across system and runtime PM transitions with notify_activity() also disabled while suspended.

All tuning is exposed as module parameters (mem_monitor_*) under /sys/module/xe/parameters/. Defaults:

mem_monitor_enabled false
mem_monitor_growth_threshold_mb 250
mem_monitor_poll_ms 2000
mem_monitor_debounce_count 5
mem_monitor_idle_timeout_ms 10000

S Sebinraj (3):
RFC: ANDROID: drm/xe: add adaptive GPU memory growth monitor
xe_mem_monitor
RFC: ANDROID: drm/xe: register ttm page alloc vendor hooks internally
RFC: ANDROID: drm/xe: Gate mem_monitor wake ups on order 9 external
fragmentation

Ryan Neph (1):
RFC: ANDROID: drm/xe: provide safe mem_monitor enable toggle via
debugfs

 drivers/gpu/drm/BUILD.bazel              |   4 +
 drivers/gpu/drm/defconfig_xe             |   1 +
 drivers/gpu/drm/xe/Kconfig               |  14 +
 drivers/gpu/drm/xe/xe_bo.c               |   3 +
 drivers/gpu/drm/xe/xe_device.c           |  12 +
 drivers/gpu/drm/xe/xe_device_types.h     |  13 +
 drivers/gpu/drm/xe/xe_mem_monitor.c      | 727 +++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_mem_monitor.h      |  95 +++
 drivers/gpu/drm/xe/xe_module.c           |  78 +++
 drivers/gpu/drm/xe/xe_module.h           |  15 +
 drivers/gpu/drm/xe/xe_pm.c               |   5 +
 drivers/gpu/drm/xe/xe_trace.h            |  16 +
 drivers/gpu/drm/xe/xe_ttm_vendor_hooks.c | 135 +++++
 drivers/gpu/drm/xe/xe_ttm_vendor_hooks.h |   9 +
 14 files changed, 1127 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/xe_mem_monitor.c
 create mode 100644 drivers/gpu/drm/xe/xe_mem_monitor.h
 create mode 100644 drivers/gpu/drm/xe/xe_ttm_vendor_hooks.c
 create mode 100644 drivers/gpu/drm/xe/xe_ttm_vendor_hooks.h
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/4 RFC: ANDROID: drm/xe: add adaptive GPU memory growth monitor xe_mem_monitor

From: S Sebinraj <[email protected]>

Introduce xe_mem_monitor, a periodic worker that samples xe->global_total_pages (the same counter that feeds the gpu_mem/gpu_mem_total tracepoint) and tracks GPU memory growth using an exponential moving average (EMA) over per-interval deltas:

delta(t) = max(0, mem(t) - mem(t-1))
EMA(t) = alpha * delta(t) + (1 - alpha) * EMA(t-1)

where alpha = 0.8, applied to every real sample regardless of mode.

A single alpha and a single polling cadence (mem_monitor_poll_ms) are used in both LATENCY and HIGH_THROUGHPUT mode, once HIGH_THROUGHPUT mode is entered there is little benefit to re-checking any faster, since mem_monitor_debounce_count consecutive below-threshold samples are already required before switching back, which itself provides the desired minimum dwell time (debounce_count * poll_ms) at this single cadence.

When the EMA of growth exceeds a threshold, xe->gpu_mem_mode is switched from XE_GPU_MEM_MODE_LATENCY to XE_GPU_MEM_MODE_HIGH_THROUGHPUT, and back once mem_monitor_debounce_count consecutive below-threshold samples are seen.

xe->gpu_mem_mode is intended to be read by the TTM page allocation path to trade allocation latency for throughput during GPU memory growth bursts; the GFP flag adjustment itself is added in a follow-up change.

For power, the worker sleeps after mem_monitor_idle_timeout_ms of no significant activity in LATENCY mode. The worker is re-armed on activity.

The worker is suspended/resumed across system and runtime PM transitions with notify_activity() also disabled while suspended.

All tuning is exposed as module parameters (mem_monitor_*) under /sys/module/xe/parameters/. Defaults:

mem_monitor_enabled false
mem_monitor_growth_threshold_mb 250
mem_monitor_poll_ms 2000
mem_monitor_debounce_count 5
mem_monitor_idle_timeout_ms 10000

Guarded by new CONFIG_DRM_XE_MEM_MONITOR (depends on DRM_XE, selects TRACE_GPU_MEM), enabled by default in defconfig_xe.

mem_monitor_enabled for fatcat is set via kernel arg xe.mem_monitor_enabled=1

Test: Verified via kernel logs while running WebGL-Aquarium / WLEU benchmark,
mode switches to HIGH_THROUGHPUT on rapid allocation bursts and back once memory stabilizes. Confirmed idle-sleep/wake, EMA decay across sleep, clean suspend/resume behavior.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Renato Pereyra <[email protected]>
---
 drivers/gpu/drm/BUILD.bazel          |   3 +
 drivers/gpu/drm/defconfig_xe         |   1 +
 drivers/gpu/drm/xe/Kconfig           |  14 +
 drivers/gpu/drm/xe/xe_bo.c           |   3 +
 drivers/gpu/drm/xe/xe_device.c       |   5 +
 drivers/gpu/drm/xe/xe_device_types.h |  12 +
 drivers/gpu/drm/xe/xe_mem_monitor.c  | 462 +++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_mem_monitor.h  |  82 +++++
 drivers/gpu/drm/xe/xe_module.c       |  55 ++++
 drivers/gpu/drm/xe/xe_module.h       |  13 +
 drivers/gpu/drm/xe/xe_pm.c           |   5 +
 11 files changed, 655 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/xe_mem_monitor.c
 create mode 100644 drivers/gpu/drm/xe/xe_mem_monitor.h
drivers/gpu/drm/BUILD.bazel +3 -0
index ac32f5cdecb9..274a1aa4ca93 100644
@@ -154,6 +154,9 @@ ddk_module(
         "CONFIG_DRM_XE_GPUFREQTRACER": {
             True: ["xe/xe_gpufreqtracer.c"],
         },
+        "CONFIG_DRM_XE_MEM_MONITOR": {
+            True: ["xe/xe_mem_monitor.c"],
+        },
         "CONFIG_DRM_XE_GPUSVM": {
             True: ["xe/xe_svm.c"],
         },
drivers/gpu/drm/defconfig_xe +1 -0
index c6e1c69fa663..4dc505580a37 100644
@@ -3,3 +3,4 @@ CONFIG_DRM_XE_DISPLAY=y
 CONFIG_DRM_XE_DP_TUNNEL=y
 CONFIG_DRM_XE_FORCE_PROBE="*"
 CONFIG_DRM_XE_GPUFREQTRACER=y
+CONFIG_DRM_XE_MEM_MONITOR=y
drivers/gpu/drm/xe/Kconfig +14 -0
index 1521975c35fc..d0a0fcb33d64 100644
@@ -150,6 +150,20 @@ config DRM_XE_GPUFREQTRACER
 
 	  If unsure, say N.
 
+config DRM_XE_MEM_MONITOR
+        bool "Enable XE GPU memory growth monitor"
+        depends on DRM_XE
+        select TRACE_GPU_MEM
+        default n
+        help
+          Enable adaptive GPU memory growth monitoring for the Intel XE driver.
+          Polls xe->global_total_pages at 500ms (active) or 2s (idle) intervals
+          and applies an exponential moving average to detect rapid allocation
+          bursts (>200 MB/interval).  When a burst is detected the TTM vendor
+          hook is disabled so that the kernel's normal direct-reclaim path can
+          apply backpressure on high-order page allocations, preventing stalls
+          from an unresponsive page cache.
+
 menu "drm/Xe Debugging"
 depends on DRM_XE
 depends on EXPERT
drivers/gpu/drm/xe/xe_bo.c +3 -0
index 493619229376..f2634d048cd4 100644
@@ -27,6 +27,7 @@
 #include "xe_ggtt.h"
 #include "xe_gt.h"
 #include "xe_map.h"
+#include "xe_mem_monitor.h"
 #include "xe_migrate.h"
 #include "xe_pm.h"
 #include "xe_preempt_fence.h"
@@ -436,6 +437,8 @@ static void update_global_total_pages(struct ttm_device *ttm_dev,
 
 	trace_gpu_mem_total(xe->drm.primary->index, 0,
 			    global_total_pages << PAGE_SHIFT);
+
+	xe_mem_monitor_notify_activity(xe);
 #endif
 }
 
drivers/gpu/drm/xe/xe_device.c +5 -0
index e405dbedd6b8..676ffc258b94 100644
@@ -36,6 +36,7 @@
 #include "xe_force_wake.h"
 #include "xe_ggtt.h"
 #include "xe_gpufreqtracer.h"
+#include "xe_mem_monitor.h"
 #include "xe_gsc_proxy.h"
 #include "xe_gt.h"
 #include "xe_gt_mcr.h"
@@ -931,6 +932,10 @@ int xe_device_probe(struct xe_device *xe)
 	if (err)
 		return err;
 
+	err = xe_mem_monitor_init(xe);
+	if (err)
+		return err;
+
 	err = xe_late_bind_init(&xe->late_bind);
 	if (err)
 		return err;
drivers/gpu/drm/xe/xe_device_types.h +12 -0
index 7b6c4e9a12b1..48aa9d8f6f56 100644
@@ -39,6 +39,7 @@ struct intel_display;
 struct intel_dg_nvm_dev;
 struct xe_ggtt;
 struct xe_gpufreqtracer_data;
+struct xe_mem_monitor_data;
 struct xe_i2c;
 struct xe_pat_ops;
 struct xe_pxp;
@@ -583,6 +584,9 @@ struct xe_device {
 	/** @gpufreqtracer_data: GPU frequency tracer data */
 	struct xe_gpufreqtracer_data *gpufreqtracer_data;
 
+	/** @mem_monitor: GPU memory growth monitor data */
+	struct xe_mem_monitor_data *mem_monitor;
+
 	/** @needs_flr_on_fini: requests function-reset on fini */
 	bool needs_flr_on_fini;
 
@@ -630,6 +634,14 @@ struct xe_device {
 	 */
 	atomic64_t global_total_pages;
 #endif
+
+	/**
+	 * @gpu_mem_mode: current GPU memory allocation mode (enum xe_gpu_mem_mode),
+	 * switched between latency and high-throughput by xe_mem_monitor based on
+	 * GPU memory growth rate.
+	 */
+	atomic_t gpu_mem_mode;
+
 	/** @val: The domain for exhaustive eviction, which is currently per device. */
 	struct xe_validation_device val;
 
drivers/gpu/drm/xe/xe_mem_monitor.c new file +462 -0

462 lines not on this page · raw message

drivers/gpu/drm/xe/xe_mem_monitor.h new file +82 -0

82 lines not on this page · raw message

drivers/gpu/drm/xe/xe_module.c +55 -0

73 lines not on this page · raw message

drivers/gpu/drm/xe/xe_module.h +13 -0
index 6a64ee221da6..24e0b52e48ca 100644
@@ -26,6 +26,19 @@ struct xe_modparam {
 #ifdef CONFIG_DRM_XE_GPUFREQTRACER
 	u32 gpufreq_monitoring_interval_ms;
 #endif
+#ifdef CONFIG_DRM_XE_MEM_MONITOR
+	/*
+	 * Master enable switch for the GPU memory growth monitor and its
+	 * associated vendor hooks. Defaults is set in xe_mem_monitor.h
+	 * (XE_MEM_MONITOR_DEFAULT_ENABLED). Intended to be
+	 * settable as a kernel boot argument
+	 */
+	bool mem_monitor_enabled;
+	u32 mem_monitor_growth_threshold_mb;
+	u32 mem_monitor_poll_ms;
+	u32 mem_monitor_debounce_count;
+	u32 mem_monitor_idle_timeout_ms;
+#endif
 };
 
 extern struct xe_modparam xe_modparam;
drivers/gpu/drm/xe/xe_pm.c +5 -0
index b291265d18fc..a330a42ea187 100644
@@ -24,6 +24,7 @@
 #include "xe_i2c.h"
 #include "xe_irq.h"
 #include "xe_late_bind_fw.h"
+#include "xe_mem_monitor.h"
 #include "xe_pcode.h"
 #include "xe_pxp.h"
 #include "xe_sriov_vf_ccs.h"
@@ -129,6 +130,7 @@ int xe_pm_suspend(struct xe_device *xe)
 	trace_xe_pm_suspend(xe, __builtin_return_address(0));
 
 	xe_gpufreqtracer_suspend_workers(xe);
+	xe_mem_monitor_suspend(xe);
 
 	err = xe_pxp_pm_suspend(xe->pxp);
 	if (err)
@@ -219,6 +221,7 @@ int xe_pm_resume(struct xe_device *xe)
 		goto err;
 
 	xe_gpufreqtracer_resume_workers(xe);
+	xe_mem_monitor_resume(xe);
 
 	xe_pxp_pm_resume(xe->pxp);
 
@@ -515,6 +518,7 @@ int xe_pm_runtime_suspend(struct xe_device *xe)
 	xe_rpm_lockmap_acquire(xe);
 
 	xe_gpufreqtracer_suspend_workers(xe);
+	xe_mem_monitor_suspend(xe);
 
 	err = xe_pxp_pm_suspend(xe->pxp);
 	if (err)
@@ -616,6 +620,7 @@ int xe_pm_runtime_resume(struct xe_device *xe)
 	}
 
 	xe_gpufreqtracer_resume_workers(xe);
+	xe_mem_monitor_resume(xe);
 
 	xe_pxp_pm_resume(xe->pxp);
 
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/4 RFC: ANDROID: drm/xe: register ttm page alloc vendor hooks internally

In reply to S Sebinraj

From: S Sebinraj <[email protected]>

Register the TTM page allocation vendor hooks (android_vh_ttm_pool_alloc_max_page_order, android_vh_ttm_pool_alloc_page_flags) directly from Xe, rather than via the standalone intel_ttm_vendor_hooks.ko module, for direct, low overhead access to xe_device state.

xe_register_android_vendor_hooks()/xe_unregister_android_vendor_hooks() are called from xe_device_create()/xe_device_destroy() respectively. Both are gated on the xe_modparam.mem_monitor_enabled master switch (shared with xe_mem_monitor): if disabled, the hooks are never registered at all, leaving TTM's default GFP flag behavior completely untouched.

The hook caps the max TTM page order at 9 (2M pages) and adjusts GFP flags for that order only based on xe->gpu_mem_mode, which is maintained by xe_mem_monitor:

XE_GPU_MEM_MODE_LATENCY (default):
strip __GFP_DIRECT_RECLAIM and __GFP_KSWAPD_RECLAIM so 2M page allocations fail fast rather than stalling on reclaim. __GFP_NORETRY (already set by TTM for order>0) is left untouched.

XE_GPU_MEM_MODE_HIGH_THROUGHPUT:
explicitly set __GFP_DIRECT_RECLAIM and __GFP_KSWAPD_RECLAIM, clear __GFP_NORETRY, and add __GFP_RETRY_MAYFAIL so the allocator retries harder (including shrinking the page cache) to satisfy 2M page allocations during bursts of GPU memory growth.

__GFP_NORETRY and __GFP_RETRY_MAYFAIL represent mutually exclusive points on the same "costly allocation" retry spectrum (see Documentation / gfp_types.h); __GFP_NORETRY is cleared explicitly here since TTM sets it unconditionally for order>0 allocations and leaving it set would blunt the effect of __GFP_RETRY_MAYFAIL.

- order-0: leave flags unmodified
- order-9: select based on gpu_mem_mode
- all others: ~gfp_flags &= ~(__GFP_DIRECT_RECLAIM | __GFP_KSWAPD_RECLAIM)

A message (rate-limited to mode transitions only, not per allocation) is logged whenever the effective mode changes, to aid verification. A new xe_gpu_mem_mode_switch tracepoint is also emitted on every mode transition (from xe_mem_monitor, see xe_trace.h) so the switching behavior can be recorded in Perfetto/ftrace traces for debugging and parameter tuning.

Test: confirmed hook fires and GFP flags change correctly across mode
transitions via dmesg while running WebGL Aquarium / WLEU benchmark. Confirmed xe_gpu_mem_mode_switch tracepoint fires on each transition via ftrace. Confirmed hooks are not registered when mem_monitor_enabled=0 is passed as a boot argument.

---
 drivers/gpu/drm/BUILD.bazel              |   1 +
 drivers/gpu/drm/xe/xe_device.c           |   6 +
 drivers/gpu/drm/xe/xe_mem_monitor.c      |  15 ++-
 drivers/gpu/drm/xe/xe_trace.h            |  16 +++
 drivers/gpu/drm/xe/xe_ttm_vendor_hooks.c | 135 +++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_ttm_vendor_hooks.h |   9 ++
 6 files changed, 178 insertions(+), 4 deletions(-)
 create mode 100644 drivers/gpu/drm/xe/xe_ttm_vendor_hooks.c
 create mode 100644 drivers/gpu/drm/xe/xe_ttm_vendor_hooks.h
drivers/gpu/drm/BUILD.bazel +1 -0
index 274a1aa4ca93..f4fc4b94f71f 100644
@@ -127,6 +127,7 @@ ddk_module(
         "xe/xe_trace_lrc.c",
         "xe/xe_ttm_stolen_mgr.c",
         "xe/xe_ttm_sys_mgr.c",
+        "xe/xe_ttm_vendor_hooks.c",
         "xe/xe_ttm_vram_mgr.c",
         "xe/xe_tuning.c",
         "xe/xe_uc.c",
drivers/gpu/drm/xe/xe_device.c +6 -0
index 676ffc258b94..d21068c1effc 100644
@@ -69,6 +69,7 @@
 #include "xe_tile.h"
 #include "xe_ttm_stolen_mgr.h"
 #include "xe_ttm_sys_mgr.h"
+#include "xe_ttm_vendor_hooks.h"
 #include "xe_vm.h"
 #include "xe_vm_madvise.h"
 #include "xe_vram.h"
@@ -419,6 +420,7 @@ static void xe_device_destroy(struct drm_device *dev, void *dummy)
 {
 	struct xe_device *xe = to_xe_device(dev);
 
+	xe_unregister_android_vendor_hooks(xe);
 	xe_bo_dev_fini(&xe->bo_device);
 
 	if (xe->preempt_fence_wq)
@@ -464,6 +466,10 @@ struct xe_device *xe_device_create(struct pci_dev *pdev,
 	if (err)
 		goto err;
 
+	err = xe_register_android_vendor_hooks(xe);
+	if (err)
+		goto err;
+
 	err = xe_shrinker_create(xe);
 	if (err)
 		goto err;
drivers/gpu/drm/xe/xe_mem_monitor.c +11 -4
index 06fbd0624da7..9205417bfb0b 100644
@@ -69,6 +69,7 @@
 #include "xe_device.h"
 #include "xe_device_types.h"
 #include "xe_module.h"
+#include "xe_trace.h"
 
 /*
  * EMA smoothing factor. alpha (13/16 = 0.8125) applies to every real
@@ -157,18 +158,24 @@ struct xe_mem_monitor_data {
 
 static void xe_mem_monitor_set_high_throughput_mode(struct xe_mem_monitor_data *mon)
 {
-	/* TODO: wire up to the actual vendor hook (adds __GFP_RETRY_MAYFAIL, TBD) */
+	/*
+	 * The TTM vendor hook (xe_ttm_vendor_hooks.c) reads xe->gpu_mem_mode
+	 * directly on the next 2M page allocation, so updating the mode here
+	 * is sufficient - no separate hook call is needed.
+	 */
 	drm_dbg(&mon->xe->drm,
-		 "xe_mem_monitor: switching to HIGH_THROUGHPUT mode (TBD)\n");
+		 "xe_mem_monitor: switching to HIGH_THROUGHPUT mode\n");
 	atomic_set(&mon->xe->gpu_mem_mode, XE_GPU_MEM_MODE_HIGH_THROUGHPUT);
+	trace_xe_gpu_mem_mode_switch(XE_GPU_MEM_MODE_HIGH_THROUGHPUT, mon->ema_growth_bytes);
 }
 
 static void xe_mem_monitor_set_latency_mode(struct xe_mem_monitor_data *mon)
 {
-	/* TODO: wire up to the actual vendor hook (TBD) */
+	/* See xe_mem_monitor_set_high_throughput_mode() above. */
 	drm_dbg(&mon->xe->drm,
-		 "xe_mem_monitor: switching to LATENCY mode (TBD)\n");
+		 "xe_mem_monitor: switching to LATENCY mode\n");
 	atomic_set(&mon->xe->gpu_mem_mode, XE_GPU_MEM_MODE_LATENCY);
+	trace_xe_gpu_mem_mode_switch(XE_GPU_MEM_MODE_LATENCY, mon->ema_growth_bytes);
 }
 
 static void xe_mem_monitor_work(struct work_struct *work)
drivers/gpu/drm/xe/xe_trace.h +16 -0
index 314f42fcbcbd..77bfc08613b9 100644
@@ -441,6 +441,22 @@ TRACE_EVENT(xe_eu_stall_data_read,
 		      __entry->read_size, __entry->total_size)
 );
 
+TRACE_EVENT(xe_gpu_mem_mode_switch,
+	    TP_PROTO(int mode, u64 growth_bytes),
+	    TP_ARGS(mode, growth_bytes),
+
+	    TP_STRUCT__entry(__field(int, mode)
+			     __field(u64, growth_bytes)
+			     ),
+
+	    TP_fast_assign(__entry->mode = mode;
+			   __entry->growth_bytes = growth_bytes;
+			   ),
+
+	    TP_printk("mode=%d, growth_bytes=%llu", __entry->mode,
+		      __entry->growth_bytes)
+);
+
 #endif
 
 /* This part must be outside protection */
drivers/gpu/drm/xe/xe_ttm_vendor_hooks.c new file +135 -0

135 lines not on this page · raw message

drivers/gpu/drm/xe/xe_ttm_vendor_hooks.h new file +9 -0
new file mode 100644
index 000000000000..f2059a3822ae
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2026 Google LLC.
+ */
+
+struct xe_device;
+
+int xe_register_android_vendor_hooks(struct xe_device *xe);
+void xe_unregister_android_vendor_hooks(struct xe_device *xe);
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 3/4 RFC: ANDROID: drm/xe: provide safe mem_monitor enable toggle via debugfs

In reply to S Sebinraj

From: Ryan Neph <[email protected]>

Provide a way to safely enable/disable mem_monitor at runtime and modify its parameters with the sequence:

```
su
echo 0 > /sys/kernel/debug/dri/0/mem_monitor/enabled
; # modify params: /sys/module/xe/parameters/mem_monitor_*
echo 1 > /sys/kernel/debug/dri/0/mem_monitor/enabled
```

Also adds to /sys/kernel/debug/dri/0/mem_monitor/:
- ./info: to show state of the mem_monitor
- ./mode: to show/set the current mode

Set the mode by writing "latency" or "throughput" to the file. If mem_monitor is:
- disabled, this mode is persistent.
- enabled, this mode may be changed by the mem_monitor

Test: echo 0 > /sys/kernel/dri/0/mem_monitor/enabled # disables worker
Test: echo 1 > /sys/kernel/dri/0/mem_monitor/enabled # re-enables worker

---
 drivers/gpu/drm/xe/xe_device.c       |   1 +
 drivers/gpu/drm/xe/xe_device_types.h |   1 +
 drivers/gpu/drm/xe/xe_mem_monitor.c  | 153 ++++++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_mem_monitor.h  |  11 ++
 drivers/gpu/drm/xe/xe_module.c       |   1 +
 5 files changed, 166 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_device.c +1 -0
index d21068c1effc..c8cec778cc97 100644
@@ -941,6 +941,7 @@ int xe_device_probe(struct xe_device *xe)
 	err = xe_mem_monitor_init(xe);
 	if (err)
 		return err;
+	xe_mem_monitor_debugfs_register(xe);
 
 	err = xe_late_bind_init(&xe->late_bind);
 	if (err)
drivers/gpu/drm/xe/xe_device_types.h +1 -0
index 48aa9d8f6f56..b920a412026b 100644
@@ -586,6 +586,7 @@ struct xe_device {
 
 	/** @mem_monitor: GPU memory growth monitor data */
 	struct xe_mem_monitor_data *mem_monitor;
+	struct dentry *mem_monitor_debugfs_root;
 
 	/** @needs_flr_on_fini: requests function-reset on fini */
 	bool needs_flr_on_fini;
drivers/gpu/drm/xe/xe_mem_monitor.c +152 -1

191 lines not on this page · raw message

drivers/gpu/drm/xe/xe_mem_monitor.h +11 -0
index 846c0628ed93..c617c10c1a12 100644
@@ -39,6 +39,8 @@ enum xe_gpu_mem_mode {
 #ifdef CONFIG_DRM_XE_MEM_MONITOR
 
 int xe_mem_monitor_init(struct xe_device *xe);
+void xe_mem_monitor_fini(struct xe_device *xe);
+int xe_mem_monitor_debugfs_register(struct xe_device *xe);
 
 /*
  * Notify the monitor that GPU allocation activity occurred (called from the
@@ -77,6 +79,15 @@ static inline void xe_mem_monitor_resume(struct xe_device *xe)
 {
 }
 
+static inline void xe_mem_monitor_fini(struct xe_device *xe)
+{
+}
+
+static inline int xe_mem_monitor_debugfs_register(struct xe_device *xe)
+{
+	return 0;
+}
+
 #endif /* CONFIG_DRM_XE_MEM_MONITOR */
 
 #endif /* _XE_MEM_MONITOR_H_ */
drivers/gpu/drm/xe/xe_module.c +1 -0
index a49b0c0f958b..a38776734544 100644
@@ -5,6 +5,7 @@
 
 #include "xe_module.h"
 
+#include "linux/log2.h"
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/stringify.h>
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH 4/4 RFC: ANDROID: drm/xe: Gate mem_monitor wake ups on order 9 external fragmentation

In reply to S Sebinraj

From: S Sebinraj <[email protected]>

xe_mem_monitor_notify_activity() previously woke the polling worker based only on a raw byte-level delta (ACTIVITY_MIN_DELTA_BYTES), which doesn't capture whether order 9 (2M) TTM allocations are actually under any pressure.

Add xe_mem_monitor_zone_order9_free_bytes(), which reads the amount of order 9 or larger free memory directly from struct zone's free_area counts.

Add xe_mem_monitor_order9_supply_low(), which walks the populated zones and returns true if any zone's order 9 free supply has dropped below an adaptive floor. The floor is mon->max_delta_bytes, the largest growth sample xe_mem_monitor_work() has ever observed on this device clamped between the mem_monitor_order9_supply_min_mb and mem_monitor_order9_supply_max_mb module parameters (in MiB).

Adapting the floor to the largest growth sample actually observed avoids false positives from a single large but otherwise harmless allocation burst when overall order 9 supply is still healthy. The upper clamp (mem_monitor_order9_supply_max_mb) stops a single anomalously large allocation from permanently pinning the floor too high.

Use this as an additional guard in xe_mem_monitor_notify_activity both the existing byte-delta check and this free-supply check must now indicate pressure before the worker is woken.

Test: Tested by running WLEU on a fragmented system and confirmed the
worker is only woken when some populated zone's order-9 free supply drops below ORDER9_SUPPLY_MIN_PCT of total RAM.

Cc Carlos Santa <[email protected]>
Cc Erin Park <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Renato Pereyra <[email protected]>
---
 drivers/gpu/drm/xe/xe_mem_monitor.c | 115 +++++++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_mem_monitor.h |   2 +
 drivers/gpu/drm/xe/xe_module.c      |  22 ++++++
 drivers/gpu/drm/xe/xe_module.h      |   2 +
 4 files changed, 137 insertions(+), 4 deletions(-)
drivers/gpu/drm/xe/xe_mem_monitor.c +111 -4
index a34cafe56976..24458af2700c 100644
@@ -59,9 +59,11 @@
 #include "xe_mem_monitor.h"
 
 #include <linux/atomic.h>
+#include <linux/compiler.h>
 #include <linux/debugfs.h>
 #include <linux/jiffies.h>
 #include <linux/minmax.h>
+#include <linux/mmzone.h>
 #include <linux/seq_file.h>
 #include <linux/slab.h>
 #include <linux/string.h>
@@ -129,6 +131,13 @@
  * @last_poll_jiffies:     jiffies value of the most recent real sample taken
  *                         by xe_mem_monitor_work(), used to detect and decay
  *                         the EMA across skipped (slept-through) intervals
+ * @max_delta_bytes:       largest clamped_delta (growth-only, per real
+ *                         sample) ever observed by xe_mem_monitor_work(),
+ *                         used by xe_mem_monitor_order9_supply_low() to
+ *                         adapt its floor upward to this workload's
+ *                         demonstrated behaviour (clamped between
+ *                         mem_monitor_order9_supply_min_mb and
+ *                         mem_monitor_order9_supply_max_mb there).
  * @suspended:             set while the device is suspended (system or
  *                         runtime PM); xe_mem_monitor_notify_activity() is a
  *                         no-op while set, so that BO eviction/restore
@@ -155,6 +164,7 @@ struct xe_mem_monitor_data {
 	unsigned long last_activity_jiffies;
 	unsigned long last_poll_jiffies;
 	bool is_high_throughput;
+	u64 max_delta_bytes;
 	atomic_t suspended;
 	bool was_pending_before_suspend;
 
@@ -186,6 +196,26 @@ static void xe_mem_monitor_set_latency_mode(struct xe_mem_monitor_data *mon)
 	trace_xe_gpu_mem_mode_switch(XE_GPU_MEM_MODE_LATENCY, mon->ema_growth_bytes);
 }
 
+/*
+ * Returns the total amount of memory (bytes) available as order-9 (2M) or
+ * larger contiguous blocks in the given zone. Computed directly from
+ * struct zone's free_area[] counts (see <linux/mmzone.h>)
+ */
+static u64 xe_mem_monitor_zone_order9_free_bytes(struct zone *zone)
+{
+	unsigned long free_blocks_suitable = 0;
+	unsigned int order;
+
+	for (order = 9; order < NR_PAGE_ORDERS; order++) {
+		/* nr_free is lockless/diagnostic-only */
+		unsigned long blocks = data_race(zone->free_area[order].nr_free);
+
+		free_blocks_suitable += blocks << (order - 9);
+	}
+
+	return (u64)free_blocks_suitable << (9 + PAGE_SHIFT);
+}
+
 static void xe_mem_monitor_work(struct work_struct *work)
 {
 	struct xe_mem_monitor_data *mon =
@@ -255,6 +285,8 @@ static void xe_mem_monitor_work(struct work_struct *work)
 				 ALPHA_RETAINED * mon->ema_growth_bytes)
 				>> ALPHA_SHIFT;
 
+	mon->max_delta_bytes = max_t(u64, mon->max_delta_bytes, clamped_delta);
+
 	atomic64_set(&mon->info.current_bytes, current_bytes);
 	atomic64_set(&mon->info.delta_signed, delta_signed);
 
@@ -348,6 +380,58 @@ void xe_mem_monitor_fini(struct xe_device *xe)
 	drm_info(&xe->drm, "xe_mem_monitor: stopped");
 }
 
+/*
+ * Companion check to ACTIVITY_MIN_DELTA_BYTES for
+ * xe_mem_monitor_notify_activity(): a large byte-level delta doesn't
+ * necessarily mean order-9 (2M) allocations are under any real pressure, so
+ * this looks instead at whether any single populated zone's absolute
+ * order-9-and-larger free supply has dropped below the adaptive floor
+ * clamp_t(mon->max_delta_bytes, mem_monitor_order9_supply_min_mb,
+ * mem_monitor_order9_supply_max_mb) independent of the size of whatever
+ * delta triggered this call, since a single large delta is not itself
+ * evidence that supply is actually running low. Taking mon->max_delta_bytes
+ * (the largest growth sample ever observed on this device, see
+ * xe_mem_monitor_work()) as the basis, clamped between the two module
+ * parameters, means the floor adapts upward to this workload's own
+ * demonstrated behaviour without being able to grow unbounded from a
+ * single anomalous allocation.
+ */
+#if IS_ENABLED(CONFIG_COMPACTION)
+static bool xe_mem_monitor_order9_supply_low(struct xe_mem_monitor_data *mon)
+{
+	struct xe_device *xe = mon->xe;
+	struct pglist_data *pgdat = NODE_DATA(0);
+	u64 min_free_bytes = clamp_t(u64, mon->max_delta_bytes,
+				     (u64)xe_modparam.mem_monitor_order9_supply_min_mb << 20,
+				     (u64)xe_modparam.mem_monitor_order9_supply_max_mb << 20);
+	unsigned int z;
+
+	for (z = 0; z < MAX_NR_ZONES; z++) {
+		struct zone *zone = &pgdat->node_zones[z];
+
+		if (!populated_zone(zone))
+			continue;
+
+		if (xe_mem_monitor_zone_order9_free_bytes(zone) < min_free_bytes) {
+			drm_dbg(&xe->drm,
+				"xe_mem_monitor: zone=%s order9_free=%llu MB < min=%llu MB\n",
+				zone->name,
+				xe_mem_monitor_zone_order9_free_bytes(zone) >> 20,
+				min_free_bytes >> 20);
+			return true;
+		}
+	}
+
+	return false;
+}
+#else
+static inline bool xe_mem_monitor_order9_supply_low(struct xe_mem_monitor_data *mon)
+{
+	/* No zone data available - don't gate on it. */
+	return true;
+}
+#endif
+
 /**
  * xe_mem_monitor_notify_activity - notify the monitor of GPU memory changes
  * @xe: the Xe device
@@ -367,6 +451,12 @@ void xe_mem_monitor_fini(struct xe_device *xe)
  * cross. If the worker is already scheduled (delayed_work_pending()), it
  * will read the live counter fresh when it runs and pick up whatever
  * accumulated, so there's no need to re-schedule again here.
+ *
+ * As a further guard, even a significant byte-level change is ignored unless
+ * xe_mem_monitor_order9_supply_low() also reports that some populated
+ * zone's order-9-and-larger free supply has dropped below the adaptive
+ * floor described there. A byte-level delta alone doesn't imply 2M
+ * allocations are under any real pressure.
  */
 void xe_mem_monitor_notify_activity(struct xe_device *xe)
 {
@@ -387,11 +477,14 @@ void xe_mem_monitor_notify_activity(struct xe_device *xe)
 	if (abs_delta < ACTIVITY_MIN_DELTA_BYTES)
 		return;
 
-	mon->last_activity_jiffies = jiffies;
-
 	if (delayed_work_pending(&mon->work))
 		return;
 
+	if (!xe_mem_monitor_order9_supply_low(mon))
+		return;
+
+	mon->last_activity_jiffies = jiffies;
+
 	schedule_delayed_work(&mon->work, 0);
 }
 
@@ -525,15 +618,29 @@ static int info_show(struct seq_file *s, void *data)
 {
 	struct xe_device *xe = s->private;
 	struct xe_mem_monitor_data *mon = xe->mem_monitor;
+	int mode = atomic_read(&xe->gpu_mem_mode);
+	struct pglist_data *pgdat = NODE_DATA(0);
+	unsigned long long order9_free_bytes = 0;
 
 	if (!mon)
 		return 1;
 
-	seq_printf(s, "current=%llu MB, delta=%lld MB, EMA=%llu MB mode=%d\n",
+	for (int z = 0; z < MAX_NR_ZONES; z++) {
+		struct zone *zone = &pgdat->node_zones[z];
+
+		if (!populated_zone(zone))
+			continue;
+		order9_free_bytes += xe_mem_monitor_zone_order9_free_bytes(zone);
+	}
+
+	seq_printf(s, "current=%llu MB, delta=%lld MB, EMA=%llu MB mode=%s, alpha=0.%03u, "
+		   "order9_free_total=%llu MB\n",
 		   atomic64_read(&mon->info.current_bytes) >> 20,
 		   atomic64_read(&mon->info.delta_signed) >> 20,
 		   mon->ema_growth_bytes >> 20,
-		   atomic_read(&xe->gpu_mem_mode));
+		   mode == XE_GPU_MEM_MODE_LATENCY ? "latency" : "throughput",
+		   (ALPHA_NUM * 1000U + (1U << (ALPHA_SHIFT - 1))) >> ALPHA_SHIFT,
+		   order9_free_bytes >> 20);
 	return 0;
 }
 DEFINE_SHOW_ATTRIBUTE(info);
drivers/gpu/drm/xe/xe_mem_monitor.h +2 -0
index c617c10c1a12..a30fabd5a8ea 100644
@@ -28,6 +28,8 @@ enum xe_gpu_mem_mode {
 #define XE_MEM_MONITOR_DEFAULT_POLL_MS				2000
 #define XE_MEM_MONITOR_DEFAULT_DEBOUNCE_COUNT			5
 #define XE_MEM_MONITOR_DEFAULT_IDLE_TIMEOUT_MS			10000
+#define XE_MEM_MONITOR_DEFAULT_ORDER9_SUPPLY_MIN_MB		512
+#define XE_MEM_MONITOR_DEFAULT_ORDER9_SUPPLY_MAX_MB		1024
 
 /*
  * Minimum enforced value for mem_monitor_poll_ms (no maximum is
drivers/gpu/drm/xe/xe_module.c +22 -0
index a38776734544..69825baf100c 100644
@@ -54,6 +54,8 @@ struct xe_modparam xe_modparam = {
 	.mem_monitor_poll_ms = XE_MEM_MONITOR_DEFAULT_POLL_MS,
 	.mem_monitor_debounce_count = XE_MEM_MONITOR_DEFAULT_DEBOUNCE_COUNT,
 	.mem_monitor_idle_timeout_ms = XE_MEM_MONITOR_DEFAULT_IDLE_TIMEOUT_MS,
+	.mem_monitor_order9_supply_min_mb = XE_MEM_MONITOR_DEFAULT_ORDER9_SUPPLY_MIN_MB,
+	.mem_monitor_order9_supply_max_mb = XE_MEM_MONITOR_DEFAULT_ORDER9_SUPPLY_MAX_MB,
 #endif
 	/* the rest are 0 by default */
 };
@@ -162,6 +164,26 @@ MODULE_PARM_DESC(mem_monitor_idle_timeout_ms,
 		 "mode after which the monitor's polling worker goes to sleep "
 		 "(woken again on the next allocation) [default="
 		 __stringify(XE_MEM_MONITOR_DEFAULT_IDLE_TIMEOUT_MS) "])");
+
+module_param_named(mem_monitor_order9_supply_min_mb,
+		   xe_modparam.mem_monitor_order9_supply_min_mb, uint, 0644);
+MODULE_PARM_DESC(mem_monitor_order9_supply_min_mb,
+		 "Minimum order-9-and-larger free supply, in MiB, required per "
+		 "populated zone before a significant GPU memory change wakes the "
+		 "monitor's polling worker. The effective floor actually used is "
+		 "the largest single growth sample ever observed by the worker, "
+		 "clamped between this value and mem_monitor_order9_supply_max_mb, "
+		 "so it adapts to this workload's own demonstrated behaviour without "
+		 "growing unbounded from a single anomalous allocation "
+		 "[default=" __stringify(XE_MEM_MONITOR_DEFAULT_ORDER9_SUPPLY_MIN_MB) "])");
+
+module_param_named(mem_monitor_order9_supply_max_mb,
+		   xe_modparam.mem_monitor_order9_supply_max_mb, uint, 0644);
+MODULE_PARM_DESC(mem_monitor_order9_supply_max_mb,
+		 "Upper bound, in MiB, on the adaptive order-9 free supply floor "
+		 "described under mem_monitor_order9_supply_min_mb - caps how high "
+		 "a single unusually large growth sample can permanently push that "
+		 "floor [default=" __stringify(XE_MEM_MONITOR_DEFAULT_ORDER9_SUPPLY_MAX_MB) "])");
 #endif
 
 static int xe_check_nomodeset(void)
drivers/gpu/drm/xe/xe_module.h +2 -0
index 24e0b52e48ca..19d0de96e078 100644
@@ -38,6 +38,8 @@ struct xe_modparam {
 	u32 mem_monitor_poll_ms;
 	u32 mem_monitor_debounce_count;
 	u32 mem_monitor_idle_timeout_ms;
+	u32 mem_monitor_order9_supply_min_mb;
+	u32 mem_monitor_order9_supply_max_mb;
 #endif
 };
 
Signature · 1 line
2.34.1